From 657a5d2cdad645ea94a0970262efa5bfa864af25 Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Mon, 10 Nov 2014 23:02:05 +0100 Subject: Handle all arguments, split out int parsing function --- src/utils.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/utils.c (limited to 'src/utils.c') diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..2425576 --- /dev/null +++ b/src/utils.c @@ -0,0 +1,34 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include + +#include "yawa.h" +#include "utils.h" + +int +parse_int(char *string, char *arg) +{ + errno = 0; + char *endptr; + long val = strtol(string, &endptr, 10); + if (errno != 0) + { + char *errormsg; + asprintf(&errormsg, "parse_int: failed to parse %s", arg); + + perror(errormsg); + free(errormsg); + exit(-2); + } + + if (endptr == string) + { + fprintf(stderr, "Valid %s not found\n", arg); + exit(-2); + } + return (int)val; +} -- cgit v1.2.3-54-g00ecf