remove atexit and replace exit() with cleanup() - gramscii - A simple editor fo… | |
Log | |
Files | |
Refs | |
Tags | |
README | |
LICENSE | |
--- | |
commit 9127db5e322c1b0cd57adf90e8cfefad66020fe2 | |
parent 6bf74b8e63315aeeac6fe4307e9e2679d334aca3 | |
Author: KatolaZ <[email protected]> | |
Date: Mon, 5 Aug 2019 08:06:19 +0100 | |
remove atexit and replace exit() with cleanup() | |
Diffstat: | |
M gramscii.c | 11 +++-------- | |
M gramscii.h | 2 ++ | |
M lineset.c | 8 ++++---- | |
M screen.c | 2 +- | |
4 files changed, 10 insertions(+), 13 deletions(-) | |
--- | |
diff --git a/gramscii.c b/gramscii.c | |
@@ -39,11 +39,7 @@ void cleanup(int s){ | |
dump_lines(screen, stdout); | |
tcsetattr(0, TCSANOW, &t1); | |
fflush(stdout); | |
- exit(0); | |
-} | |
- | |
-void exit_cleanup(void){ | |
- cleanup(0); | |
+ exit(s); | |
} | |
/*** Initialisation ***/ | |
@@ -54,7 +50,6 @@ void init(){ | |
signal(SIGINT, cleanup); | |
signal(SIGTERM, cleanup); | |
signal(SIGQUIT, cleanup); | |
- atexit(exit_cleanup); | |
tcgetattr(0, &t1); | |
t2 = t1; | |
@@ -132,7 +127,7 @@ void commands(FILE *fc){ | |
case 'q': | |
check_modified(fc);/** FALLTHROUGH **/ | |
case 'Q': | |
- exit(0); | |
+ cleanup(0); | |
break; | |
} | |
} | |
@@ -147,7 +142,7 @@ void commands(FILE *fc){ | |
void usage(){ | |
fprintf(stderr, "Usage: %s [-s] [-h] [file ...]\n", argv0); | |
- exit(1); | |
+ cleanup(1); | |
} | |
diff --git a/gramscii.h b/gramscii.h | |
@@ -193,4 +193,6 @@ void copy_lines_to_ring(int y1, int y2, int which); | |
void invalidate_undo(); | |
/**/ | |
+void cleanup(int); | |
+ | |
#endif | |
diff --git a/lineset.c b/lineset.c | |
@@ -14,7 +14,7 @@ void ensure_line_length(line_t *l, int len){ | |
tmp = realloc(l->s, (len+1) * 2 * sizeof(char)); | |
if (!tmp){ | |
fprintf(stderr, "Unable to allocate string\n"); | |
- exit(1); | |
+ cleanup(-1); | |
} | |
l->s = tmp; | |
l->sz = (len + 1) * 2; | |
@@ -29,7 +29,7 @@ void alloc_line(line_t *l){ | |
tmp = malloc((l->sz) * sizeof(char)); | |
if (tmp == NULL){ | |
fprintf(stderr, "unable to allocate line\n"); | |
- exit(1); | |
+ cleanup(-1); | |
} | |
l->s = tmp; | |
memset(l->s, BG, l->sz); | |
@@ -46,7 +46,7 @@ void ensure_num_lines(lineset_t *ls, int n){ | |
tmp = realloc(ls->l, (n + LONG_STEP) * sizeof(line_t)); | |
if (tmp == NULL){ | |
fprintf(stderr, "Unable to allocate memory for more li… | |
- exit(1); | |
+ cleanup(-1); | |
} | |
else { | |
ls->l = tmp; | |
@@ -147,7 +147,7 @@ void copy_lines_to_ring(int y1, int y2, int which){ | |
tmp = realloc(undo, (undo_sz + 10) * sizeof(lineset_t)); | |
if (tmp == NULL){ | |
fprintf(stderr, "Error allocating undo buffer"); | |
- exit(1); | |
+ cleanup(-1); | |
} | |
undo = tmp; | |
for (i=0; i<10; i++){ | |
diff --git a/screen.c b/screen.c | |
@@ -400,7 +400,7 @@ void init_screen(){ | |
screen.num = HEIGHT; | |
if (screen.l == NULL){ | |
perror("allocating screen"); | |
- exit(1); | |
+ cleanup(-1); | |
} | |
for (i=0; i<HEIGHT; i++){ | |
alloc_line(&(screen.l[i])); |