From b260b11a3b9c944193227a3b1eb8ca29acc304c7 Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Tue, 18 Oct 2011 20:54:30 +0200 Subject: [PATCH] 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 --- Makefile.am | 1 + configure.ac | 1 + include/strutils.h | 6 ++++++ lib/.gitignore | 1 + lib/Makefile.am | 7 +++++++ lib/strutils.c | 34 ++++++++++++++++++++++++++++++++++ 6 files changed, 50 insertions(+) create mode 100644 include/strutils.h create mode 100644 lib/.gitignore create mode 100644 lib/Makefile.am create mode 100644 lib/strutils.c diff --git a/Makefile.am b/Makefile.am index 4ea2e321..2005aaf2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -3,6 +3,7 @@ AM_CPPFLAGS = -include $(top_builddir)/config.h -I$(top_srcdir)/include ACLOCAL_AMFLAGS = -I m4 SUBDIRS = \ include \ + lib \ po \ proc \ ps \ diff --git a/configure.ac b/configure.ac index 109cc2c9..b130cea8 100644 --- a/configure.ac +++ b/configure.ac @@ -188,6 +188,7 @@ AC_SUBST(DEJAGNU) AC_CONFIG_FILES([ Makefile include/Makefile + lib/Makefile po/Makefile.in proc/Makefile proc/libprocfs.pc diff --git a/include/strutils.h b/include/strutils.h new file mode 100644 index 00000000..cf521d3a --- /dev/null +++ b/include/strutils.h @@ -0,0 +1,6 @@ +#ifndef PROCPS_NG_STRUTILS +#define PROCPS_NG_STRUTILS + +extern long strtol_or_err(const char *str, const char *errmesg); + +#endif diff --git a/lib/.gitignore b/lib/.gitignore new file mode 100644 index 00000000..88ee4c9a --- /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 00000000..4216a252 --- /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 00000000..064bdc06 --- /dev/null +++ b/lib/strutils.c @@ -0,0 +1,34 @@ +#include + +#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 */ -- 2.40.0