aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils.c
blob: 2425576d953253b56861b2660c72803a337ed202 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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;
}