From dad6aa5b168f109d1a2651812571fcb4de861257 Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Fri, 2 Jan 2015 15:57:10 +0100 Subject: sfeed_frames: get rid of atexit() --- sfeed_frames.c | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/sfeed_frames.c b/sfeed_frames.c index 138d7d7..1994ba8 100644 --- a/sfeed_frames.c +++ b/sfeed_frames.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -36,6 +37,20 @@ cleanup(void) fpcontent = NULL; } +static void +xerr(int eval, const char *fmt, ...) +{ + int saved_errno = errno; + va_list ap; + + cleanup(); + + errno = saved_errno; + va_start(ap, fmt); + verr(eval, fmt, ap); + va_end(ap); +} + /* print text, ignore tabs, newline and carriage return etc * print some HTML 2.0 / XML 1.0 as normal text */ static void @@ -99,7 +114,6 @@ main(int argc, char *argv[]) struct stat st; struct utimbuf contenttime; - atexit(cleanup); memset(&contenttime, 0, sizeof(contenttime)); if(argc > 1 && argv[1][0] != '\0') @@ -112,17 +126,17 @@ main(int argc, char *argv[]) /* write main index page */ if(snprintf(dirpath, sizeof(dirpath), "%s/index.html", basepath) <= 0) - err(1, "snprintf"); + xerr(1, "snprintf"); if(!(fpindex = fopen(dirpath, "w+b"))) - err(1, "fopen"); + xerr(1, "fopen"); if(snprintf(dirpath, sizeof(dirpath), "%s/menu.html", basepath) <= 0) - err(1, "snprintf"); + xerr(1, "snprintf"); if(!(fpmenu = fopen(dirpath, "w+b"))) - err(1, "fopen: can't write menu.html"); + xerr(1, "fopen: can't write menu.html"); if(snprintf(dirpath, sizeof(dirpath), "%s/items.html", basepath) <= 0) - err(1, "snprintf"); + xerr(1, "snprintf"); if(!(fpitems = fopen(dirpath, "w+b"))) - err(1, "fopen"); + xerr(1, "fopen"); fputs("" "" "
", fpitems); @@ -145,17 +159,15 @@ main(int argc, char *argv[]) continue; if(snprintf(dirpath, sizeof(dirpath), "%s/%s", basepath, name) <= 0) - err(1, "snprintf"); + xerr(1, "snprintf"); /* directory doesn't exist: try to create it. */ - if(stat(dirpath, &st) == -1) { - if(mkdir(dirpath, S_IRWXU) == -1) - err(1, "sfeed_frames: can't make directory '%s'", dirpath); - } + if(stat(dirpath, &st) == -1 && mkdir(dirpath, S_IRWXU) == -1) + xerr(1, "sfeed_frames: can't make directory '%s'", dirpath); strlcpy(reldirpath, name, sizeof(reldirpath)); /* TODO: check truncation */ if(!(f = calloc(1, sizeof(struct feed)))) - err(1, "calloc"); + xerr(1, "calloc"); if(totalfeeds) { /* end previous one. */ fputs("\n", fpitems); @@ -168,7 +180,7 @@ main(int argc, char *argv[]) } /* write menu link if new. */ if(!(fcur->name = strdup(feedname))) - err(1, "strdup"); + xerr(1, "strdup"); if(fields[FieldFeedName][0] != '\0') { fputs("

name, fpitems); @@ -185,9 +197,9 @@ main(int argc, char *argv[]) if(!(namelen = makepathname(fields[FieldTitle], name, sizeof(name)))) continue; if(snprintf(filepath, sizeof(filepath), "%s/%s.html", dirpath, name) <= 0) - err(1, "snprintf"); + xerr(1, "snprintf"); if(snprintf(relfilepath, sizeof(relfilepath), "%s/%s.html", reldirpath, name) <= 0) - err(1, "snprintf"); + xerr(1, "snprintf"); /* file doesn't exist yet and has write access */ if(access(filepath, F_OK) != 0 && (fpcontent = fopen(filepath, "w+b"))) { @@ -286,5 +298,7 @@ main(int argc, char *argv[]) "\n" "", fpindex); + cleanup(); + return 0; } -- cgit v1.2.3