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) --- thumbs.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'thumbs.c') diff --git a/thumbs.c b/thumbs.c index 4790ed9..f10a1aa 100644 --- a/thumbs.c +++ b/thumbs.c @@ -16,6 +16,7 @@ * along with sxiv. If not, see . */ +#include #include #include #include @@ -122,7 +123,7 @@ void tns_clean_cache(tns_t *tns) r_dir_t dir; if (r_opendir(&dir, cache_dir) < 0) { - warn("could not open thumbnail cache directory: %s", cache_dir); + error(0, errno, "%s", cache_dir); return; } @@ -140,7 +141,7 @@ void tns_clean_cache(tns_t *tns) } if (delete) { if (unlink(cfile) < 0) - warn("could not delete cache file: %s", cfile); + error(0, errno, "%s", cfile); } free(cfile); } @@ -181,11 +182,11 @@ void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel, cache_dir = (char*) emalloc(len); snprintf(cache_dir, len, "%s%s/sxiv", homedir, dsuffix); } else { - warn("could not locate thumbnail cache directory"); + error(0, 0, "Cache directory not found"); } } -void tns_free(tns_t *tns) +CLEANUP void tns_free(tns_t *tns) { int i; @@ -222,7 +223,7 @@ Imlib_Image tns_scale_down(Imlib_Image im, int dim) im = imlib_create_cropped_scaled_image(0, 0, w, h, MAX(z * w, 1), MAX(z * h, 1)); if (im == NULL) - die("could not allocate memory"); + error(EXIT_FAILURE, ENOMEM, NULL); imlib_free_image_and_decache(); } return im; @@ -316,7 +317,7 @@ bool tns_load(tns_t *tns, int n, bool force, bool cache_only) } if (w >= maxwh || h >= maxwh) { if ((im = imlib_create_cropped_image(x, y, w, h)) == NULL) - die("could not allocate memory"); + error(EXIT_FAILURE, ENOMEM, NULL); } imlib_free_image_and_decache(); } @@ -332,7 +333,7 @@ bool tns_load(tns_t *tns, int n, bool force, bool cache_only) (im = imlib_load_image(file->path)) == NULL)) { if (file->flags & FF_WARN) - warn("could not open image: %s", file->name); + error(0, 0, "%s: Error opening image", file->name); return false; } imlib_context_set_image(im); -- cgit v1.2.3-54-g00ecf