-
Notifications
You must be signed in to change notification settings - Fork 6
/
util.c
60 lines (48 loc) · 895 Bytes
/
util.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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ncurses.h>
void eprintf(const char *fmt, ...) {
va_list ap;
endwin();
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
void *emalloc(size_t size) {
void *p;
p = malloc(size);
if (!p)
eprintf("Out of memory\n");
return p;
}
void *ecalloc(size_t nmemb, size_t size) {
void *p;
p = calloc(nmemb, size);
if (!p)
eprintf("Out of memory\n");
return p;
}
double estrtod(const char *str) {
char *ep;
double d;
d = strtod(str, &ep);
if (!d || *ep != '\0' || ep == str)
eprintf("Invalid number: %s\n", str);
return d;
}
size_t strlcpy(char *dest, const char *src, size_t size) {
size_t len;
len = strlen(src);
if (size) {
if (len >= size)
size -= 1;
else
size = len;
strncpy(dest, src, size);
dest[size] = '\0';
}
return size;
}