From db0650cf10027170e29b690fc6ebdebd03b890d5 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Wed, 3 Jan 2024 00:30:37 +0200 Subject: [PATCH] LibC: Implement basic atexit. This allows clean exit from doom (soon) --- libc/stdlib.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/libc/stdlib.cpp b/libc/stdlib.cpp index 721f5d7581..72133d9a7b 100644 --- a/libc/stdlib.cpp +++ b/libc/stdlib.cpp @@ -13,6 +13,9 @@ extern "C" char** environ; extern "C" void _fini(); +static void (*at_exit_funcs[64])(); +static uint32_t at_exit_funcs_count = 0; + void abort(void) { fflush(nullptr); @@ -22,6 +25,8 @@ void abort(void) void exit(int status) { + for (uint32_t i = at_exit_funcs_count; i > 0; i--) + at_exit_funcs[i - 1](); fflush(nullptr); __cxa_finalize(nullptr); _fini(); @@ -34,9 +39,15 @@ int abs(int val) return val < 0 ? -val : val; } -int atexit(void(*)(void)) +int atexit(void (*func)(void)) { - return -1; + if (at_exit_funcs_count > sizeof(at_exit_funcs) / sizeof(*at_exit_funcs)) + { + errno = ENOBUFS; + return -1; + } + at_exit_funcs[at_exit_funcs_count++] = func; + return 0; } int atoi(const char* str)