diff options
author | Sami Kerola | 2011-02-20 00:00:37 +0100 |
---|---|---|
committer | Karel Zak | 2011-02-21 15:32:50 +0100 |
commit | 94d32126bf1e7a41cadaa8740ee76b79f1eb59ab (patch) | |
tree | bfad90f648d201c9873bc713b7c72e039e0fc46b | |
parent | mkswap: use xalloc (diff) | |
download | kernel-qcow2-util-linux-94d32126bf1e7a41cadaa8740ee76b79f1eb59ab.tar.gz kernel-qcow2-util-linux-94d32126bf1e7a41cadaa8740ee76b79f1eb59ab.tar.xz kernel-qcow2-util-linux-94d32126bf1e7a41cadaa8740ee76b79f1eb59ab.zip |
strutils: new wrapper function strtoll_or_err
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
-rw-r--r-- | include/strutils.h | 1 | ||||
-rw-r--r-- | lib/strutils.c | 24 |
2 files changed, 25 insertions, 0 deletions
diff --git a/include/strutils.h b/include/strutils.h index 462332d95..99d8acd2c 100644 --- a/include/strutils.h +++ b/include/strutils.h @@ -7,6 +7,7 @@ extern int strtosize(const char *str, uintmax_t *res); extern long strtol_or_err(const char *str, const char *errmesg); +extern long long strtoll_or_err(const char *str, const char *errmesg); #ifndef HAVE_STRNLEN extern size_t strnlen(const char *s, size_t maxlen); diff --git a/lib/strutils.c b/lib/strutils.c index 39c9bdf02..acb7139dc 100644 --- a/lib/strutils.c +++ b/lib/strutils.c @@ -188,6 +188,30 @@ err: errx(EXIT_FAILURE, "%s: '%s'", errmesg, str); return 0; } +/* + * 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; + + if (str == NULL || *str == '\0') + goto err; + errno = 0; + num = strtoll(str, &end, 10); + + if (errno || (end && *end)) + goto err; + + return num; +err: + if (errno) + err(EXIT_FAILURE, "%s: '%s'", errmesg, str); + else + errx(EXIT_FAILURE, "%s: '%s'", errmesg, str); + return 0; +} /* * Converts stat->st_mode to ls(1)-like mode string. The size of "str" must |