Luna/libc/src/atexit.cpp
apio cfb0ead2d9
libc: Flush all open streams on exit
This does nothing for now, but prepares for buffering.
2023-07-21 22:56:03 +02:00

32 lines
583 B
C++

#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);
}
}