diff options
author | Karel Zak | 2012-03-30 16:11:31 +0200 |
---|---|---|
committer | Karel Zak | 2012-03-30 16:11:31 +0200 |
commit | a99c913091cdf0c392a959ecfe3505bc74a56155 (patch) | |
tree | a93b1d1afed6e0482e0e16fb2788bf108fe17235 /lib | |
parent | docs: clarify KiB vs. KB in man pages (diff) | |
download | kernel-qcow2-util-linux-a99c913091cdf0c392a959ecfe3505bc74a56155.tar.gz kernel-qcow2-util-linux-a99c913091cdf0c392a959ecfe3505bc74a56155.tar.xz kernel-qcow2-util-linux-a99c913091cdf0c392a959ecfe3505bc74a56155.zip |
lib/strutils: add strtosize_or_err, clean up
* add strtosize_or_err(), we use strtosize() + err() on many places
* add STRTOXX_EXIT_CODE to overwrite the default EXIT_FAILURE
* remove else-after-noreturn (e.g. if (foo) err(...); else err(...))
* clean up indent...
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/strutils.c | 82 |
1 files changed, 45 insertions, 37 deletions
diff --git a/lib/strutils.c b/lib/strutils.c index c40daf200..db1511023 100644 --- a/lib/strutils.c +++ b/lib/strutils.c @@ -184,60 +184,56 @@ double strtod_or_err(const char *str, const char *errmesg) goto err; return num; - err: +err: if (errno) - err(EXIT_FAILURE, "%s: '%s'", errmesg, str); - else - errx(EXIT_FAILURE, "%s: '%s'", errmesg, str); - return 0; + err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); + + errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); } /* * same as strtol(3) but exit on failure instead of returning crap */ long strtol_or_err(const char *str, const char *errmesg) { - long num; - char *end = NULL; + long num; + char *end = NULL; - if (str == NULL || *str == '\0') - goto err; - errno = 0; - num = strtol(str, &end, 10); + if (str == NULL || *str == '\0') + goto err; + errno = 0; + num = strtol(str, &end, 10); - if (errno || str == end || (end && *end)) - goto err; + if (errno || str == end || (end && *end)) + goto err; - return num; + return num; err: - if (errno) - err(EXIT_FAILURE, "%s: '%s'", errmesg, str); - else - errx(EXIT_FAILURE, "%s: '%s'", errmesg, str); - return 0; + if (errno) + err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); + errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); } /* * same as strtoll(3) but exit on failure instead of returning crap */ long long strtoll_or_err(const char *str, const char *errmesg) { - long long num; - char *end = NULL; + long long num; + char *end = NULL; - if (str == NULL || *str == '\0') - goto err; - errno = 0; - num = strtoll(str, &end, 10); + if (str == NULL || *str == '\0') + goto err; + errno = 0; + num = strtoll(str, &end, 10); - if (errno || str == end || (end && *end)) - goto err; + if (errno || str == end || (end && *end)) + goto err; - return num; + return num; err: - if (errno) - err(EXIT_FAILURE, "%s: '%s'", errmesg, str); - else - errx(EXIT_FAILURE, "%s: '%s'", errmesg, str); - return 0; + if (errno) + err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); + + errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); } /* * same as strtoul(3) but exit on failure instead of returning crap @@ -258,10 +254,22 @@ unsigned long strtoul_or_err(const char *str, const char *errmesg) return num; err: if (errno) - err(EXIT_FAILURE, "%s: '%s'", errmesg, str); - else - errx(EXIT_FAILURE, "%s: '%s'", errmesg, str); - return 0; + err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); + + errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); +} + +uintmax_t strtosize_or_err(const char *str, const char *errmesg) +{ + uintmax_t num; + + if (strtosize(str, &num) == 0) + return num; + + if (errno) + err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); + + errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); } /* |