diff options
Diffstat (limited to 'src/stdlib/atexit.c')
-rw-r--r-- | src/stdlib/atexit.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/stdlib/atexit.c b/src/stdlib/atexit.c new file mode 100644 index 00000000..180dc5e2 --- /dev/null +++ b/src/stdlib/atexit.c @@ -0,0 +1,38 @@ +#include <stdlib.h> +#include "errno.h" +#include "nonstd/internal.h" + +/** register a function to run at program exit **/ + +int atexit(void (*func)(void)) +{ + struct atexit *ae = __libc(ATEXIT); + while (ae->nfns == sizeof(ae->fns) / sizeof(ae->fns[0])) { + if (ae->next == NULL) { + ae->next = calloc(1, sizeof(*ae->next)); + if (ae->next == NULL) { + #ifdef ENOMEM + errno = ENOMEM; + #endif + return 1; + } + ae->next->prev = ae; + } + ae = ae->next; + } + ae->fns[ae->nfns++] = func; + return 0; +} + +/*** +registers the function ARGUMENT(func) to be called when the program +exits normally by calling FUNCTION(exit) or returning from FUNCTION(main). The +function must take no parameters and return no value. +***/ + +/* +IMPLEMENTATION(The number of registrations allowed (at least 32)) +RETURN_FAILURE(NONZERO) +RETURN_SUCCESS(0) +STDC(1) +*/ |