diff --git a/src/kernel/Jamfile b/src/kernel/Jamfile index b9a95f5a61..32dece9a43 100644 --- a/src/kernel/Jamfile +++ b/src/kernel/Jamfile @@ -255,6 +255,7 @@ KernelStaticLibraryObjects libc.a : <$(SOURCE_GRIST)!libroot!posix!stdlib>atoi.o <$(SOURCE_GRIST)!libroot!posix!stdlib>bsearch.o <$(SOURCE_GRIST)!libroot!posix!stdlib>env.o + <$(SOURCE_GRIST)!libroot!posix!stdlib>exit.o <$(SOURCE_GRIST)!libroot!posix!stdlib>heapsort.o <$(SOURCE_GRIST)!libroot!posix!stdlib>merge.o <$(SOURCE_GRIST)!libroot!posix!stdlib>multibyte.o @@ -399,6 +400,7 @@ KernelLd libroot.so : <$(SOURCE_GRIST)!libroot!posix!stdlib>atoi.o <$(SOURCE_GRIST)!libroot!posix!stdlib>bsearch.o <$(SOURCE_GRIST)!libroot!posix!stdlib>env.o + <$(SOURCE_GRIST)!libroot!posix!stdlib>exit.o <$(SOURCE_GRIST)!libroot!posix!stdlib>heapsort.o <$(SOURCE_GRIST)!libroot!posix!stdlib>merge.o <$(SOURCE_GRIST)!libroot!posix!stdlib>multibyte.o diff --git a/src/kernel/libroot/posix/stdlib/Jamfile b/src/kernel/libroot/posix/stdlib/Jamfile index 82721fe7ef..6f5414eabb 100644 --- a/src/kernel/libroot/posix/stdlib/Jamfile +++ b/src/kernel/libroot/posix/stdlib/Jamfile @@ -5,6 +5,7 @@ KernelObjects <$(SOURCE_GRIST)>atoi.c <$(SOURCE_GRIST)>bsearch.c <$(SOURCE_GRIST)>env.c + <$(SOURCE_GRIST)>exit.c <$(SOURCE_GRIST)>heapsort.c <$(SOURCE_GRIST)>merge.c <$(SOURCE_GRIST)>multibyte.c diff --git a/src/kernel/libroot/posix/stdlib/exit.c b/src/kernel/libroot/posix/stdlib/exit.c new file mode 100644 index 0000000000..7a5b077c5a --- /dev/null +++ b/src/kernel/libroot/posix/stdlib/exit.c @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2002, OpenBeOS Project + * + * This file is part of the OpenBeOS system interface. + * It is distributed under the terms of the OpenBeOS license. + * + * + * Author(s): + * Daniel Reinhold (danielre@users.sf.net) + * + */ + +#include +#include +#include + +// ToDo: move this puppy to a more standard location +#include "../stdio/local.h" + +// ToDo: this is supposed to be declared in +int atexit(void (*func)(void)); + + + +#define _EXIT_STACK_SIZE_ 32 + +static void (*_Exit_Stack[_EXIT_STACK_SIZE_])(void) = {0}; +static int _Exit_SP = 0; + + +int +atexit(void (*func)(void)) +{ + // push the function pointer onto the exit stack + + if (_Exit_SP < _EXIT_STACK_SIZE_) { + _Exit_Stack[_Exit_SP++] = func; + return 0; + } + + return -1; +} + + +void +exit(int status) +{ + // unwind the exit stack, calling the registered functions + while (_Exit_SP > 0) + (*_Exit_Stack[--_Exit_SP])(); + + // close all open files + (*__cleanup)(); + + // exit with status code + sys_exit(status); +}