diff options
author | Johannes Löthberg <johannes@kyriasis.com> | 2014-11-10 23:02:05 +0100 |
---|---|---|
committer | Johannes Löthberg <johannes@kyriasis.com> | 2014-11-10 23:41:17 +0100 |
commit | 657a5d2cdad645ea94a0970262efa5bfa864af25 (patch) | |
tree | 835823d84e7135bbe138b14fee072936f017c5f7 /src/utils.c | |
parent | 04a926ec1e79eba4bda31e19ef58aed79e10ff46 (diff) | |
download | yawa-657a5d2cdad645ea94a0970262efa5bfa864af25.tar.xz |
Handle all arguments, split out int parsing function
Diffstat (limited to 'src/utils.c')
-rw-r--r-- | src/utils.c | 34 |
1 files changed, 34 insertions, 0 deletions
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 <bsd/string.h> +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> +#include <stdio.h> +#include <errno.h> + +#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; +} |