25 lines
553 B
C++
25 lines
553 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;
|
||
|
}
|
||
|
|
||
|
noreturn void exit(int status)
|
||
|
{
|
||
|
for (int i = 0; i < atexit_function_count; i++) { atexit_functions[i](); }
|
||
|
_exit(status);
|
||
|
}
|
||
|
}
|