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:
34
lib/strutils.c
Normal file
34
lib/strutils.c
Normal 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 */
|
Reference in New Issue
Block a user