]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
strutils: new wrapper function strtoll_or_err
authorSami Kerola <kerolasa@iki.fi>
Sat, 19 Feb 2011 23:00:37 +0000 (00:00 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 21 Feb 2011 14:32:50 +0000 (15:32 +0100)
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
include/strutils.h
lib/strutils.c

index 462332d9574ad8284c35060f929420c6dd9d2f25..99d8acd2c1d2fee3da075921fe0e4a5ffb7e7154 100644 (file)
@@ -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);
index 39c9bdf022a84f4b91374918e21ff7ae61fd9abb..acb7139dc22a2e9f3cecb82fcc4152fd083e864f 100644 (file)
@@ -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