Added atexit

This commit is contained in:
NeGate 2022-06-07 22:17:57 -04:00
parent 454a2cc78d
commit 911841be30
7 changed files with 76 additions and 11 deletions

View File

@ -74,9 +74,11 @@ void mainCRTStartup() {
srand(0);
setlocale(LC_ALL, "C");
_os_init_eh();
int exit_code = main(arg_count, args);
ExitProcess(exit_code);
// we call exit because we want atexit routines run
exit(exit_code);
}
// This symbol is required to be present if we're using floating-point

View File

@ -10,5 +10,3 @@ void _os_file_write(void* ctx, size_t n, const char str[]) {
DWORD written = 0;
WriteFile((HANDLE) ctx, str, n, &written, NULL);
}

View File

@ -4,18 +4,38 @@
#include <_os.h>
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;
_Noreturn void abort(void) {
raise(SIGABRT);
_os_exit(-69);
}
// TODO: at_exit handling
int atexit(void (*func)(void)) {
if (_exit_routine_count >= COUNTOF(_exit_routines)) {
return 0;
}
_exit_routines[_exit_routine_count++] = func;
return 1;
}
_Noreturn void exit(int status) {
// doing them in reverse seems nicer
for (int i = _exit_routine_count; i--;) {
_exit_routines[i]();
}
_os_exit(status);
}
_Noreturn void _Exit(int status) {
// doesn't run atexit routines
_os_exit(status);
}

View File

@ -3,13 +3,13 @@
#include <stddef.h>
#if !defined(__STDC_LIB_EXT1__)
#define __STDC_LIB_EXT1__
#define __STDC_LIB_EXT1__
#endif
#ifdef __STDC_WANT_SECURE_LIB__
#if !defined(__STDC_WANT_LIB_EXT1__)
#define __STDC_WANT_LIB_EXT1__ 1
#endif
#if !defined(__STDC_WANT_LIB_EXT1__)
#define __STDC_WANT_LIB_EXT1__ 1
#endif
#endif
#define EXIT_SUCCESS 0
@ -36,6 +36,11 @@
#define RAND_MAX 65536
// #define MB_CUR_MAX 5
// Microsoft extension, COUNTOF(x) counts array elements
#ifndef COUNTOF
#define COUNTOF(...) (sizeof(__VA_ARGS__) / sizeof((__VA_ARGS__)[0]))
#endif
double atof(const char *nptr);
int atoi(const char *nptr);
long int atol(const char *nptr);
@ -59,7 +64,7 @@ void *malloc(size_t size);
void *realloc(void *ptr, size_t size);
_Noreturn void abort(void);
// int atexit(void (*func)(void));
int atexit(void (*func)(void));
// int at_quick_exit(void (*func)(void));
_Noreturn void exit(int status);
_Noreturn void _Exit(int status);

View File

@ -16,7 +16,13 @@ int test() {
return a;
}
void wack() {
printf("BYE!!!\n");
}
int main(int argc, char** argv) {
atexit(wack);
test();
char input[] = "A bird came down the walk";
printf("Parsing the input string '%s'\n", input);

25
test/test2.c Normal file
View File

@ -0,0 +1,25 @@
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
int main(void) {
double v0 = strtod("0X1.BC70A3D70A3D7P+6", NULL);
// parsing with error handling
const char *p = "111.11 -2.22 Nan nan(2) inF 0X1.BC70A3D70A3D7P+6 1.18973e+4932zzz";
char *end;
for (double f = strtod(p, &end); p != end; f = strtod(p, &end)) {
// printf("'%.*s' -> ", (int)(end-p), p);
p = end;
if (errno == ERANGE){
// printf("range error, got ");
errno = 0;
}
// printf("%f\n", f);
}
// parsing without error handling
double v1 = strtod(" -0.0000000123junk", NULL);
double v2 = strtod("junk", NULL);
}

9
test/test3.c Normal file
View File

@ -0,0 +1,9 @@
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
int main(int argc, char** argv) {
for (int i = 0; i < argv; i++) {
printf("[%d] = %s\n", i, argv[i]);
}
}