Luna/libs/libc/src/atexit.cpp
apio 8398b2e2e4 libc: Change bits/macros.h to use a __lc_ prefix for internal macros
That way, we don't pollute user programs with our own 'noreturn' and 'deprecated' macros
2022-10-15 09:52:37 +02:00

25 lines
558 B
C++

#include <stdlib.h>
#include <unistd.h>
#define ATEXIT_MAX_FUNCS 32
typedef void (*atexit_func_t)(void);
atexit_func_t atexit_functions[ATEXIT_MAX_FUNCS];
int atexit_function_count = 0;
extern "C"
{
int atexit(atexit_func_t handler)
{
if (atexit_function_count >= ATEXIT_MAX_FUNCS) return -1;
atexit_functions[atexit_function_count++] = handler;
return 0;
}
__lc_noreturn void exit(int status)
{
for (int i = 0; i < atexit_function_count; i++) { atexit_functions[i](); }
_exit(status);
}
}