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) --- options.c | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) (limited to 'options.c') diff --git a/options.c b/options.c index 882ac49..f7aac23 100644 --- a/options.c +++ b/options.c @@ -47,6 +47,9 @@ void parse_options(int argc, char **argv) char *end, *s; const char *scalemodes = "dfwh"; + progname = strrchr(argv[0], '/'); + progname = progname ? progname + 1 : argv[0]; + _options.from_stdin = false; _options.to_stdout = false; _options.recursive = false; @@ -86,10 +89,8 @@ void parse_options(int argc, char **argv) break; case 'G': n = strtol(optarg, &end, 0); - if (*end != '\0') { - fprintf(stderr, "sxiv: invalid argument for option -G: %s\n", optarg); - exit(EXIT_FAILURE); - } + if (*end != '\0') + error(EXIT_FAILURE, 0, "Invalid argument for option -G: %s", optarg); _options.gamma = n; break; case 'g': @@ -103,10 +104,8 @@ void parse_options(int argc, char **argv) break; case 'n': n = strtol(optarg, &end, 0); - if (*end != '\0' || n <= 0) { - fprintf(stderr, "sxiv: invalid argument for option -n: %s\n", optarg); - exit(EXIT_FAILURE); - } + if (*end != '\0' || n <= 0) + error(EXIT_FAILURE, 0, "Invalid argument for option -n: %s", optarg); _options.startnum = n - 1; break; case 'N': @@ -123,18 +122,14 @@ void parse_options(int argc, char **argv) break; case 'S': n = strtol(optarg, &end, 0); - if (*end != '\0' || n <= 0) { - fprintf(stderr, "sxiv: invalid argument for option -S: %s\n", optarg); - exit(EXIT_FAILURE); - } + if (*end != '\0' || n <= 0) + error(EXIT_FAILURE, 0, "Invalid argument for option -S: %s", optarg); _options.slideshow = n; break; case 's': s = strchr(scalemodes, optarg[0]); - if (s == NULL || *s == '\0' || strlen(optarg) != 1) { - fprintf(stderr, "sxiv: invalid argument for option -s: %s\n", optarg); - exit(EXIT_FAILURE); - } + if (s == NULL || *s == '\0' || strlen(optarg) != 1) + error(EXIT_FAILURE, 0, "Invalid argument for option -s: %s", optarg); _options.scalemode = s - scalemodes; break; case 't': @@ -149,10 +144,8 @@ void parse_options(int argc, char **argv) break; case 'z': n = strtol(optarg, &end, 0); - if (*end != '\0' || n <= 0) { - fprintf(stderr, "sxiv: invalid argument for option -z: %s\n", optarg); - exit(EXIT_FAILURE); - } + if (*end != '\0' || n <= 0) + error(EXIT_FAILURE, 0, "Invalid argument for option -z: %s", optarg); _options.scalemode = SCALE_ZOOM; _options.zoom = (float) n / 100.0; break; -- cgit v1.2.3-70-g09d2