ciabatta/code/stdlib/env.c

41 lines
780 B
C
Raw Normal View History

2022-06-07 06:15:47 +00:00
#include <stdlib.h>
#include <signal.h>
#include <_os.h>
2022-06-08 02:17:57 +00:00
typedef void (*ExitRoutine)(void);
// The implementation shall support the registration
// of at least 32 functions.
static ExitRoutine _exit_routines[64];
static int _exit_routine_count;
2022-06-07 06:15:47 +00:00
_Noreturn void abort(void) {
raise(SIGABRT);
_os_exit(-69);
}
2022-06-08 02:17:57 +00:00
int atexit(void (*func)(void)) {
if (_exit_routine_count >= COUNTOF(_exit_routines)) {
return 0;
}
_exit_routines[_exit_routine_count++] = func;
return 1;
}
2022-06-07 06:15:47 +00:00
_Noreturn void exit(int status) {
2022-06-08 02:17:57 +00:00
// doing them in reverse seems nicer
for (int i = _exit_routine_count; i--;) {
_exit_routines[i]();
}
2022-06-07 06:15:47 +00:00
_os_exit(status);
}
_Noreturn void _Exit(int status) {
2022-06-08 02:17:57 +00:00
// doesn't run atexit routines
2022-06-07 06:15:47 +00:00
_os_exit(status);
}