diff options
author | Sami Kerola <kerolasa@iki.fi> | 2011-10-18 20:54:30 +0200 |
---|---|---|
committer | Sami Kerola <kerolasa@iki.fi> | 2011-12-20 17:30:52 +0100 |
commit | b260b11a3b9c944193227a3b1eb8ca29acc304c7 (patch) | |
tree | 6057c820b22a5a42b0b5818585dae7b80e4a9507 /lib | |
parent | 130895b02ed32c89208b634a4c8411a34cf79ddc (diff) | |
download | procps-ng-b260b11a3b9c944193227a3b1eb8ca29acc304c7.tar.gz |
lib: add strtol into utility library
The utility library is for functions which are shared in commands,
but that does not belong to libproc-ng. The first function is a
wrapper for strtol that performs error checking, and exists if such
happen.
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/.gitignore | 1 | ||||
-rw-r--r-- | lib/Makefile.am | 7 | ||||
-rw-r--r-- | lib/strutils.c | 34 |
3 files changed, 42 insertions, 0 deletions
diff --git a/lib/.gitignore b/lib/.gitignore new file mode 100644 index 0000000..88ee4c9 --- /dev/null +++ b/lib/.gitignore @@ -0,0 +1 @@ +test_strutils diff --git a/lib/Makefile.am b/lib/Makefile.am new file mode 100644 index 0000000..4216a25 --- /dev/null +++ b/lib/Makefile.am @@ -0,0 +1,7 @@ +AM_CPPFLAGS = -include $(top_builddir)/config.h -I$(top_srcdir)/include + +AM_CPPFLAGS += -DTEST_PROGRAM + +noinst_PROGRAMS = test_strutils + +test_strutils_SOURCES = strutils.c diff --git a/lib/strutils.c b/lib/strutils.c new file mode 100644 index 0000000..064bdc0 --- /dev/null +++ b/lib/strutils.c @@ -0,0 +1,34 @@ +#include <stdlib.h> + +#include "c.h" +#include "strutils.h" + +/* + * 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; + + if (str == NULL || *str == '\0') + goto err; + errno = 0; + num = strtol(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); +} + +#ifdef TEST_PROGRAM +int main(int argc, char *argv[]) +{ + return EXIT_FAILURE; +} +#endif /* TEST_PROGRAM */ |