From 94ee3156b7a22b02f8b21e6db3fcdf7d1ad51d0e Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 16 May 2018 10:18:21 -0400 Subject: errusage --- src/cow-dedupe-range.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/cow-dedupe-range.c b/src/cow-dedupe-range.c index 33d62b0..bffa8f1 100644 --- a/src/cow-dedupe-range.c +++ b/src/cow-dedupe-range.c @@ -7,12 +7,19 @@ #include /* for bool, true, false */ #include /* for uint64_t */ #include /* for printf(3gnu), fprintf(3p), stderr */ -#include /* for strtoll(3p), calloc(3p), free(3p) */ +#include /* for strtoll(3p), calloc(3p), free(3p), exit(3p), EXIT_SUCCESS */ #include /* for strlen(3p) */ #include /* for dup2(3p) */ #include "dedupe-range.h" /* for dedupe_range, struct range */ +#define EXIT_INVALIDARGUMENT 2 +#define errusage(format, ...) do { \ + error(0, 0, format, ## __VA_ARGS__); \ + fprintf(stderr, "Try '%s --help' for more information.\n", program_invocation_name); \ + exit(EXIT_INVALIDARGUMENT); \ +} while(0) + bool atou64(const char *str, uint64_t *ret) { assert(sizeof(uint64_t) <= sizeof(unsigned long long int)); if (!('0' <= str[0] && str[0] <= '9')) @@ -59,24 +66,18 @@ int main(int argc, char *argv[]) { break; case 'h': usage(); - return 0; + return EXIT_SUCCESS; case '?': fprintf(stderr, "Try '%s --help' for more information.\n", program_invocation_name); - return 2; + return EXIT_INVALIDARGUMENT; default: assert(false); } } - if (argc - optind < 5) { - error(0, 0, "too few arguments"); - fprintf(stderr, "Try '%s --help' for more information.\n", program_invocation_name); - return 2; - } - if ((argc - optind - 3) % 2 != 0) { - error(0, 0, "wrong number of arguments"); - fprintf(stderr, "Try '%s --help' for more information.\n", program_invocation_name); - return 2; - } + if (argc - optind < 5) + errusage("too few arguments"); + if ((argc - optind - 3) % 2 != 0) + errusage("wrong number of arguments"); struct range src; src.filename = argv[optind]; @@ -97,5 +98,5 @@ int main(int argc, char *argv[]) { dedupe_range(src, dsts); free(dsts); - return 0; + return EXIT_SUCCESS; } -- cgit v1.2.3