-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
79 lines (68 loc) · 1.14 KB
/
utils.c
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include <errno.h>
#include <math.h>
#include "utils.h"
extern char *progname;
static void verr(char *fmt, va_list ap);
void err(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
verr(fmt, ap);
}
void syserr(void)
{
perror(progname);
exit(-1);
}
int intparse(char *s, char **next)
{
long buf;
char *e;
errno = 0;
buf = strtol(s, &e, 10);
if (errno)
syserr();
if (e == s || !next && *e)
err("Invalid integer\n");
if (next)
*next = e;
#if LONG_MAX > INT_MAX
if (buf < INT_MIN || buf > INT_MAX)
err("Integer out of range: %ld\n", buf);
#endif
return buf;
}
double lfparse(char *s)
{
double buf;
char *e;
errno = 0;
buf = strtod(s, &e);
if (errno)
syserr();
if (e == s || *e || !isfinite(buf))
err("Invalid number: %s\n", s);
return buf;
}
void *alloc2(int m, int n, int w)
{
char **p;
int i;
CALLOC(p, m);
if (!(p[0] = calloc(m*n, w)))
syserr();
for (i = 1; i < m; i++)
p[i] = p[i-1] + w*n;
return p;
}
static void verr(char *fmt, va_list ap)
{
fprintf(stderr, "%s: ", progname);
vfprintf(stderr, fmt, ap);
exit(-1);
}