From d3a70a285d03224fde9e6ef36eba9de21b773f39 Mon Sep 17 00:00:00 2001 From: Bert Münnich Date: Wed, 28 Oct 2015 23:03:37 +0100 Subject: Revised error handling - Functions warn() and die() replaced by GNU-like error(3) function - Register cleanup() with atexit(3) - Functions called by cleanup() are marked with CLEANUP and are not allowed to call exit(3) --- util.c | 55 +++++++++++++++++++++---------------------------------- 1 file changed, 21 insertions(+), 34 deletions(-) (limited to 'util.c') diff --git a/util.c b/util.c index 4569489..620c8b6 100644 --- a/util.c +++ b/util.c @@ -26,7 +26,7 @@ #include "options.h" #include "util.h" -void cleanup(void); +const char *progname; void* emalloc(size_t size) { @@ -34,7 +34,7 @@ void* emalloc(size_t size) ptr = malloc(size); if (ptr == NULL) - die("could not allocate memory"); + error(EXIT_FAILURE, errno, NULL); return ptr; } @@ -42,7 +42,7 @@ void* erealloc(void *ptr, size_t size) { ptr = realloc(ptr, size); if (ptr == NULL) - die("could not allocate memory"); + error(EXIT_FAILURE, errno, NULL); return ptr; } @@ -53,40 +53,27 @@ char* estrdup(const char *s) d = malloc(n); if (d == NULL) - die("could not allocate memory"); + error(EXIT_FAILURE, errno, NULL); memcpy(d, s, n); return d; } -void warn(const char* fmt, ...) +void error(int eval, int err, const char* fmt, ...) { - va_list args; - - if (fmt == NULL || options->quiet) - return; - - va_start(args, fmt); - fprintf(stderr, "sxiv: warning: "); - vfprintf(stderr, fmt, args); - fprintf(stderr, "\n"); - va_end(args); -} - -void die(const char* fmt, ...) -{ - va_list args; - - if (fmt == NULL) - return; - - va_start(args, fmt); - fprintf(stderr, "sxiv: error: "); - vfprintf(stderr, fmt, args); - fprintf(stderr, "\n"); - va_end(args); - - cleanup(); - exit(EXIT_FAILURE); + va_list ap; + + fflush(stdout); + fprintf(stderr, "%s: ", progname); + va_start(ap, fmt); + if (fmt != NULL) + vfprintf(stderr, fmt, ap); + va_end(ap); + if (err != 0) + fprintf(stderr, "%s%s", fmt != NULL ? ": " : "", strerror(err)); + fputc('\n', stderr); + + if (eval != 0) + exit(eval); } void size_readable(float *size, const char **unit) @@ -185,7 +172,7 @@ char* r_readdir(r_dir_t *rdir) rdir->name = rdir->stack[--rdir->stlen]; rdir->d = 1; if ((rdir->dir = opendir(rdir->name)) == NULL) - warn("could not open directory: %s", rdir->name); + error(0, errno, "%s", rdir->name); continue; } /* no more entries */ @@ -215,7 +202,7 @@ int r_mkdir(const char *path) *d = '\0'; if (access(dir, F_OK) < 0 && errno == ENOENT) { if (mkdir(dir, 0755) < 0) { - warn("could not create directory: %s", dir); + error(0, errno, "%s", dir); err = -1; } } else if (stat(dir, &stats) < 0 || !S_ISDIR(stats.st_mode)) { -- cgit v1.2.3-54-g00ecf