-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
293 lines (244 loc) · 5.64 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* SPDX-License-Identifier: GPL-2.0+
* Copyright (C) 2018 Abinav Puthan Purayil
* */
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdarg.h>
#include <limits.h>
#include <unistd.h>
#include <stdio.h>
#include "util.h"
void NORETURN err_exit(int status, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
_exit(status);
}
void NORETURN die(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
_exit(EXIT_FAILURE);
}
/* all allocation functions are fatal on failure */
void *umalloc(size_t size)
{
void *alloc;
alloc = malloc(size);
if (!alloc) {
die("malloc failed!");
}
return alloc;
}
void *urealloc(void *ptr, size_t size)
{
void *alloc;
alloc = realloc(ptr, size);
if (!alloc) {
die("malloc failed!");
}
return alloc;
}
/* returns zeroed allocation of size bytes */
void *zalloc(size_t size)
{
void *alloc;
alloc = calloc(size, 1);
if (!alloc) {
die("malloc failed!");
}
return alloc;
}
void *stralloc(const char *str)
{
void *alloc;
size_t len = strlen(str);
alloc = umalloc(len + 1); /* +1 for '\0' */
return alloc;
}
size_t ustrlen(const char *str)
{
if (str)
return strlen(str);
else
return 0;
}
char *ultoa(unsigned long inp)
{
unsigned long n = snprintf(NULL, 0, "%lu", inp);
char *str = (char *)umalloc(n + 1);
snprintf(str, n + 1, "%lu", inp);
return str;
}
/* returns hash value of a string of n bytes modulo mod */
long hash_simfn(const void *bytes, size_t n, size_t mod)
{
int base = UCHAR_MAX + 1;
unsigned long hash = 0;
size_t i;
char *chars = (char *)bytes;
for (i = 0; i < n; i++) {
hash = (base * hash + chars[i]) % mod;
}
return hash;
}
/* squeeze @me of length @sz_me at @mem of length sz_mem */
void memsqz(void *mem, size_t sz_mem, const void *me, size_t sz_me)
{
memmove((char *)mem + sz_me, mem, sz_mem);
memcpy(mem, me, sz_me);
}
mode_t stat_mode(const char *path)
{
struct stat st;
if (stat(path, &st) == -1) {
return -1; /* all bits set = crazy file */
}
return st.st_mode;
}
void cleanup(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
while (*fmt) {
switch (*fmt++) {
case 'f':
close(va_arg(ap, int));
break;
case 'a':
free(va_arg(ap, void *));
break;
default :
break;
}
}
va_end(ap);
}
/*
* pretty prints @n columns of text where each column corresponds
* to each string args @... after @n. Column sizes(fixed) correspond to @width[].
* Make sure @width[] has 1-1 correspondence with @... and that their
* cardinality = n.
* */
void printc(size_t width[], size_t n, ...)
{
char arg[1024], fmt[1024];
const char **argv;
uint8_t *argdone;
va_list ap;
size_t done = 0, i;
va_start(ap, n);
/* tracking "where we left off" for each arg when printing lines */
argv = (const char **)umalloc(n * sizeof(void *));
/* argdone[i] == 0 ? pending chars to be printed in argv[i] : argv[i] is
* fully printed. Num of argdone[] set = done */
argdone = (uint8_t *)zalloc(n * sizeof(uint8_t));
for (i = 0; i < n; i++) {
argv[i] = va_arg(ap, char *);
}
va_end(ap);
while (done != n) {
for (i = 0; i < n; i++) {
strncpy(arg, argv[i], width[i]);
arg[width[i]] = 0;
sprintf(fmt, "%%-%zus ", width[i]);
printf(fmt, arg);
/* the messy update argv section */
if (strlen(argv[i]) < width[i]) {
/* don't skip by width[i] if argv[i] is not long enough,
* else we'll start printing garbage */
argv[i] = "";
} else {
argv[i] = argv[i] + width[i];
}
if (!strlen(argv[i]) && !argdone[i]) {
argdone[i] = 1;
done++;
}
}
printf("\n");
}
free(argv);
free(argdone);
}
void optusage(struct options opt[])
{
char opt_short[8];
char opt_long[1024];
size_t width[] = {4, 20, 75};
size_t i;
for (i = 0; (opt[i].name_short || opt[i].name_long)
&& opt[i].desc; i++) {
/* init opt_x strings as empty */
opt_short[0] = 0;
opt_long[0] = 0;
if (opt[i].name_short)
sprintf(opt_short, " -%c", opt[i].name_short[0]);
if (opt[i].name_long)
sprintf(opt_long, "--%s", opt[i].name_long);
if (opt[i].has_arg)
strcat(opt_long, " <arg>");
printc(width, 3, opt_short, opt_long, opt[i].desc);
}
}
int parseopt(char **argvp[], char **optarg, const struct options opt[])
{
char **argv = *argvp;
const char *arg = *argv;
int ret = PARSEOPT_UNREC, has_optarg = 0;
size_t i;
if (!arg || arg[0] != '-' || !arg[1])
return PARSEOPT_DONE;
if ((*optarg = strchr(arg, '='))) {
/*
* nullify '=', so that strcmping on arg works. This is why we have
* to do this before deciding the option.
* */
**optarg = 0;
(*optarg)++;
if (**optarg == 0) {
/* in case of -x= ... or -xx= ...*/
return PARSEOPT_NOOPTARG;
}
} /* else *optarg = NULL (strchr fail) */
/* long option or -- terminator */
if (arg[1] == '-') {
if (!arg[2]) { /* -- terminator */
return PARSEOPT_DONE;
}
for (i = 0; opt[i].name_short || opt[i].name_long; i++) {
if (opt[i].name_long && strcmp(arg + 2, opt[i].name_long) == 0) {
has_optarg = (opt[i].has_arg) ? 1 : 0;
ret = opt[i].ret;
}
}
/* short option */
} else {
for (i = 0; opt[i].name_short || opt[i].name_long; i++) {
if (opt[i].name_short && strncmp(arg + 1, opt[i].name_short, 1) == 0) {
has_optarg = (opt[i].has_arg) ? 1 : 0;
ret = opt[i].ret;
}
}
}
if (ret == PARSEOPT_UNREC)
return PARSEOPT_UNREC;
/* since has_optarg is by default 0, if opt is unrecognized, it
* would remain 0 */
if (has_optarg && *optarg == NULL) {
if (argv[1] == NULL)
return PARSEOPT_NOOPTARG;
*optarg = argv[1];
*argvp = argv + 2;
return ret;
}
/* for opts without optarg or --opt=x or -o=x kinds */
*argvp = argv + 1;
return ret;
}