-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.c
142 lines (125 loc) · 2.78 KB
/
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "util.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
void die(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
putc('\n', stderr);
exit(1);
}
static void die_errno(const char *fmt, ...)
{
int saved_errno = errno;
char buf[1024];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
die("%s: %s", buf, strerror(saved_errno));
}
static void die_nomem(void)
{
die("out of memory");
}
static void chomp(char *s)
{
int len = strlen(s) - 1;
while (s[len] == '\n')
s[len--] = '\0';
}
const char *util_line(FILE *in)
{
static char buf[4096];
if (!fgets(buf, sizeof(buf), in))
return NULL;
chomp(buf);
return buf;
}
void util_trie_insert(struct trie *t, const char *s)
{
char *v = strdup(s);
if (!v) die_nomem();
if (trie_insert(t, v, v) < 0) die_nomem();
}
FILE *util_open(const char *fn, const char *mode) {
FILE *fh = fopen(fn, mode);
if (!fh)
die_errno("unable to open %s", fn);
return fh;
}
int util_limit_opt(struct util_limit *ul, char opt, const char *arg)
{
switch (opt) {
case 'l':
ul->limit = atoi(arg);
ul->dynamic = 0;
return 1;
case 'd':
ul->limit = atoi(arg);
ul->dynamic = 1;
return 1;
case 'm':
ul->max = atoi(arg);
return 1;
}
return 0;
}
int util_limit(struct util_limit *ul, const char *s)
{
int limit = ul->dynamic ?
((strlen(s) - 1 + ul->limit) / ul->limit) :
ul->limit;
if (limit > ul->max)
limit = ul->max;
return limit;
}
void util_array_push(struct util_array *ua, const char *v)
{
if (ua->len == ua->alloc) {
ua->alloc = (ua->alloc+16)*3/2;
ua->d = realloc(ua->d, ua->alloc * sizeof(*ua->d));
if (!ua->d) die_nomem();
}
ua->d[ua->len++] = v;
}
void util_trie_read(struct trie *t, FILE *in)
{
const char *line;
while ((line = util_line(in))) {
if (!*line) break;
util_trie_insert(t, line);
}
}
void util_atomicfile_open(struct util_atomicfile *af, const char *base)
{
int fd;
struct stat st;
snprintf(af->fn, sizeof(af->fn), "%s.XXXXXX", base);
fd = mkstemp(af->fn);
if (fd < 0)
die_errno("unable to create tempfile '%s.XXXXXX'", base);
af->fh = fdopen(fd, "w");
if (stat(base, &st) < 0) {
if (errno != ENOENT)
die_errno("unable to stat %s", base);
}
else {
if (chmod(af->fn, st.st_mode) < 0)
die_errno("unable to chmod %s", af->fn);
}
}
void util_atomicfile_close(struct util_atomicfile *af, const char *dest)
{
if (fclose(af->fh) == EOF)
die_errno("error writing to %s", af->fn);
if (rename(af->fn, dest) < 0)
die_errno("unable to rename %s to %s", af->fn, dest);
}