From: Michael D. Lowis Date: Fri, 1 Jan 2016 20:56:27 +0000 (-0500) Subject: Fixed cleanup handling X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=4fa4970f58119560669307a175d009b808b86407;p=projs%2Ftide.git Fixed cleanup handling --- diff --git a/source/main.c b/source/main.c index bbfc224..7d7070b 100644 --- a/source/main.c +++ b/source/main.c @@ -1,5 +1,7 @@ /* Includes *****************************************************************************/ +static void cleanup(void); +#define CLEANUP_HOOK cleanup #include #include @@ -27,7 +29,6 @@ int Cursor_X = 0, Cursor_Y = 0; /* Declarations *****************************************************************************/ static void setup(void); -static void cleanup(void); static void load(char* fname); static void edit(void); @@ -121,6 +122,7 @@ static void edit(void) *****************************************************************************/ int main(int argc, char** argv) { + atexit(cleanup); setup(); if (argc > 1) { load(argv[1]); @@ -128,6 +130,5 @@ int main(int argc, char** argv) } else { die("no filename provided"); } - cleanup(); return EXIT_SUCCESS; } diff --git a/source/util.h b/source/util.h index 49f81c9..d32bcef 100644 --- a/source/util.h +++ b/source/util.h @@ -7,7 +7,7 @@ #include #include -/* Usefule Standard Functions */ +/* Useful Standard Functions */ #include #include #include @@ -17,13 +17,16 @@ /* Generic Death Function */ static void die(const char* msgfmt, ...) { - va_list args; - va_start(args, msgfmt); - fprintf(stderr, "Error: "); - vfprintf(stderr, msgfmt, args); - fprintf(stderr, "\n"); - va_end(args); - exit(EXIT_FAILURE); + va_list args; + va_start(args, msgfmt); + #ifdef CLEANUP_HOOK + CLEANUP_HOOK(); + #endif + fprintf(stderr, "Error: "); + vfprintf(stderr, msgfmt, args); + fprintf(stderr, "\n"); + va_end(args); + exit(EXIT_FAILURE); } /* Signal Handling */ @@ -88,7 +91,7 @@ static char* efreadline(FILE* input) free(str); return NULL; } - while(true) { + while (true) { char ch = fgetc(input); if (ch == EOF) break; str[index++] = ch;