summaryrefslogtreecommitdiffstats
path: root/lib/strutils.c
diff options
context:
space:
mode:
authorSami Kerola2011-05-24 22:56:38 +0200
committerKarel Zak2011-06-01 09:57:43 +0200
commite53bc9604c14b10c34a1e8d057d2e741e8d7ea1b (patch)
treeee1986aca50d3f9da15f335ef309cf6a02ff7875 /lib/strutils.c
parentcol: coding style changes (diff)
downloadkernel-qcow2-util-linux-e53bc9604c14b10c34a1e8d057d2e741e8d7ea1b.tar.gz
kernel-qcow2-util-linux-e53bc9604c14b10c34a1e8d057d2e741e8d7ea1b.tar.xz
kernel-qcow2-util-linux-e53bc9604c14b10c34a1e8d057d2e741e8d7ea1b.zip
lib: add strtoul_or_err() function
Signed-off-by: Sami Kerola <kerolasa@iki.fi> Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'lib/strutils.c')
-rw-r--r--lib/strutils.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/strutils.c b/lib/strutils.c
index 07010f32c..e7a2454a0 100644
--- a/lib/strutils.c
+++ b/lib/strutils.c
@@ -213,6 +213,30 @@ err:
errx(EXIT_FAILURE, "%s: '%s'", errmesg, str);
return 0;
}
+/*
+ * same as strtoul(3) but exit on failure instead of returning crap
+ */
+unsigned long strtoul_or_err(const char *str, const char *errmesg)
+{
+ unsigned long num;
+ char *end = NULL;
+
+ if (str == NULL || *str == '\0')
+ goto err;
+ errno = 0;
+ num = strtoul(str, &end, 10);
+
+ if (errno || str == end || (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