From 25db1abb6e143f0b93cfb3df4231829395e5e75f Mon Sep 17 00:00:00 2001 From: bumbread Date: Fri, 24 Jun 2022 12:38:54 +1100 Subject: [PATCH] close all streams on exit --- src/win/win.h | 1 + src/win/win_environment.c | 23 ++++++++++++----------- src/win/win_stdio.c | 19 ++++++++++++++++++- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/win/win.h b/src/win/win.h index 649b68e..c0d23c1 100644 --- a/src/win/win.h +++ b/src/win/win.h @@ -11,3 +11,4 @@ void _setup_timer(void); void _setup_eh(); void _setup_heap(); void _setup_io(); +void _close_io(); diff --git a/src/win/win_environment.c b/src/win/win_environment.c index ad86a73..b053ee9 100644 --- a/src/win/win_environment.c +++ b/src/win/win_environment.c @@ -87,9 +87,21 @@ void mainCRTStartup() { int exit_code = main(argc, args); // we call exit because we want atexit routines run + // and all the file handles to be freed 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) { raise(SIGABRT); @@ -104,17 +116,6 @@ int atexit(void (*func)(void)) { 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) { // The string pointed to shall not be modified by the program, but may be // overwritten by a subsequent call to the getenv function diff --git a/src/win/win_stdio.c b/src/win/win_stdio.c index 37498df..6cabc13 100644 --- a/src/win/win_stdio.c +++ b/src/win/win_stdio.c @@ -100,7 +100,7 @@ void _setup_io() { HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE); HANDLE hstderr = GetStdHandle(STD_ERROR_HANDLE); HANDLE hstdin = GetStdHandle(STD_INPUT_HANDLE); - + char *out_buf = calloc(BUFSIZ, sizeof(char)); char *err_buf = out_buf; if(hstdout != hstderr) { @@ -112,6 +112,13 @@ void _setup_io() { 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( FILE *restrict stream, char *restrict buf, @@ -139,6 +146,16 @@ int setvbuf( return 0; } +int fflush(FILE *stream) { + if(mode == _IOFBF) { + + } + else if(mode == _IONBF) { + + } + return 0; +} + void setbuf(FILE *restrict stream, char *restrict buf) { if(buf != NULL) setvbuf(stream, buf, _IOFBF, BUFSIZ);