Luna/libc/src/atexit.cpp

32 lines
583 B
C++
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
typedef void (*atexit_func_t)(void);
const int ATEXIT_MAX_FUNCS = 48;
int atexit_registered_funcs = 0;
atexit_func_t atexit_funcs[ATEXIT_MAX_FUNCS];
extern "C"
{
int atexit(atexit_func_t func)
{
if (atexit_registered_funcs == ATEXIT_MAX_FUNCS) return -1;
atexit_funcs[atexit_registered_funcs++] = func;
return 0;
}
__noreturn void exit(int status)
{
while (atexit_registered_funcs--) { atexit_funcs[atexit_registered_funcs](); }
fflush(NULL);
_Exit(status);
}
}