summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSami Kerola <kerolasa@iki.fi>2011-10-18 20:54:30 +0200
committerSami Kerola <kerolasa@iki.fi>2011-12-20 17:30:52 +0100
commitb260b11a3b9c944193227a3b1eb8ca29acc304c7 (patch)
tree6057c820b22a5a42b0b5818585dae7b80e4a9507
parent130895b02ed32c89208b634a4c8411a34cf79ddc (diff)
downloadprocps-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>
-rw-r--r--Makefile.am1
-rw-r--r--configure.ac1
-rw-r--r--include/strutils.h6
-rw-r--r--lib/.gitignore1
-rw-r--r--lib/Makefile.am7
-rw-r--r--lib/strutils.c34
6 files changed, 50 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am
index 4ea2e32..2005aaf 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 109cc2c..b130cea 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 0000000..cf521d3
--- /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 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 */