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>
This commit is contained in:
Sami Kerola 2011-10-18 20:54:30 +02:00
parent 130895b02e
commit b260b11a3b
6 changed files with 50 additions and 0 deletions

View File

@ -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 \

View File

@ -188,6 +188,7 @@ AC_SUBST(DEJAGNU)
AC_CONFIG_FILES([
Makefile
include/Makefile
lib/Makefile
po/Makefile.in
proc/Makefile
proc/libprocfs.pc

6
include/strutils.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef PROCPS_NG_STRUTILS
#define PROCPS_NG_STRUTILS
extern long strtol_or_err(const char *str, const char *errmesg);
#endif

1
lib/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
test_strutils

7
lib/Makefile.am Normal file
View File

@ -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

34
lib/strutils.c Normal file
View File

@ -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 */