close all streams on exit

This commit is contained in:
bumbread 2022-06-24 12:38:54 +11:00
parent 673a363d4b
commit 25db1abb6e
3 changed files with 31 additions and 12 deletions

View File

@ -11,3 +11,4 @@ void _setup_timer(void);
void _setup_eh(); void _setup_eh();
void _setup_heap(); void _setup_heap();
void _setup_io(); void _setup_io();
void _close_io();

View File

@ -87,9 +87,21 @@ void mainCRTStartup() {
int exit_code = main(argc, args); int exit_code = main(argc, args);
// we call exit because we want atexit routines run // we call exit because we want atexit routines run
// and all the file handles to be freed
exit(exit_code); exit(exit_code);
} }
_Noreturn void exit(int status) {
for (int i = atexit_func_count; i--;) {
atexit_funcs[i]();
}
_close_io();
ExitProcess(status);
}
_Noreturn void _Exit(int status) {
ExitProcess(status);
}
_Noreturn void abort(void) { _Noreturn void abort(void) {
raise(SIGABRT); raise(SIGABRT);
@ -104,17 +116,6 @@ int atexit(void (*func)(void)) {
return 1; return 1;
} }
_Noreturn void exit(int status) {
for (int i = atexit_func_count; i--;) {
atexit_funcs[i]();
}
ExitProcess(status);
}
_Noreturn void _Exit(int status) {
ExitProcess(status);
}
char *getenv(const char *name) { char *getenv(const char *name) {
// The string pointed to shall not be modified by the program, but may be // The string pointed to shall not be modified by the program, but may be
// overwritten by a subsequent call to the getenv function // overwritten by a subsequent call to the getenv function

View File

@ -100,7 +100,7 @@ void _setup_io() {
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE); HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE hstderr = GetStdHandle(STD_ERROR_HANDLE); HANDLE hstderr = GetStdHandle(STD_ERROR_HANDLE);
HANDLE hstdin = GetStdHandle(STD_INPUT_HANDLE); HANDLE hstdin = GetStdHandle(STD_INPUT_HANDLE);
char *out_buf = calloc(BUFSIZ, sizeof(char)); char *out_buf = calloc(BUFSIZ, sizeof(char));
char *err_buf = out_buf; char *err_buf = out_buf;
if(hstdout != hstderr) { if(hstdout != hstderr) {
@ -112,6 +112,13 @@ void _setup_io() {
stdin = new_file(hstdin, NULL, STR_R, 0, 0, _IONBF, 0, NULL); stdin = new_file(hstdin, NULL, STR_R, 0, 0, _IONBF, 0, NULL);
} }
void _close_io() {
while(file_list_last != NULL) {
fflush(file_list_last);
dispose_file(file_list_last);
}
}
int setvbuf( int setvbuf(
FILE *restrict stream, FILE *restrict stream,
char *restrict buf, char *restrict buf,
@ -139,6 +146,16 @@ int setvbuf(
return 0; return 0;
} }
int fflush(FILE *stream) {
if(mode == _IOFBF) {
}
else if(mode == _IONBF) {
}
return 0;
}
void setbuf(FILE *restrict stream, char *restrict buf) { void setbuf(FILE *restrict stream, char *restrict buf) {
if(buf != NULL) if(buf != NULL)
setvbuf(stream, buf, _IOFBF, BUFSIZ); setvbuf(stream, buf, _IOFBF, BUFSIZ);