-
Notifications
You must be signed in to change notification settings - Fork 4
/
liberty.c
478 lines (402 loc) · 8.86 KB
/
liberty.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#include "compat.h"
#include "liberty.h"
#include <errno.h>
#include <locale.h>
#include <stdio.h>
#include <ctype.h>
__noreturn void die_errno(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fputs("error: ", stderr);
vfprintf(stderr, fmt, ap);
fprintf(stderr, ": %s\n", strerror(errno));
va_end(ap);
exit(EXIT_FAILURE);
}
__noreturn void die(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fputs("error: ", stderr);
vfprintf(stderr, fmt, ap);
fputs("\n", stderr);
va_end(ap);
exit(EXIT_FAILURE);
}
void *xmalloc(size_t size)
{
void *p = malloc(size);
if (!p)
die("memory exhausted");
return p;
}
void *zmalloc(size_t size)
{
return memset(xmalloc(size), 0, size);
}
void *xcalloc(size_t count, size_t size)
{
void *p = calloc(count, size);
if (!p)
die("memory exhausted");
return p;
}
void *xrealloc(void *ptr, size_t size)
{
void *p = realloc(ptr, size);
if (!p)
die("memory exhausted");
return p;
}
/* alloc */
union align {
long l;
double d;
void (*f)(void);
};
struct bucket {
struct bucket *next;
char *cur;
char *limit;
};
union header {
struct bucket b;
union align a;
};
static struct bucket first[] = { { NULL }, { NULL } };
static struct bucket *area[] = { &first[0], &first[1] };
static struct bucket *freebuckets;
void *allocate(size_t n, enum allocate_area a)
{
struct bucket *p;
assert(a >= 0 && a < NELEMS(area));
p = area[a];
n = ROUNDUP(n, sizeof(union align));
while (n > p->limit - p->cur) {
if ((p->next = freebuckets) != NULL) {
freebuckets = freebuckets->next;
p = p->next;
} else {
size_t m = sizeof(union header) + n +
ROUNDUP(10 * 1024, sizeof(union align));
p->next = xmalloc(m);
p = p->next;
p->limit = (char *)p + m;
}
p->cur = (char *)((union header *)p + 1);
p->next = NULL;
area[a] = p;
}
p->cur += n;
return p->cur - n;
}
void *zallocate(size_t n, enum allocate_area a)
{
return memset(allocate(n, a), 0, n);
}
void deallocate(enum allocate_area a)
{
assert(a >= 0 && a < NELEMS(area));
area[a]->next = freebuckets;
freebuckets = first[a].next;
first[a].next = NULL;
area[a] = &first[a];
}
int file_exist(const char *path)
{
struct stat st;
return stat(path, &st) == 0;
}
size_t file_size(const char *path)
{
struct stat st;
if (stat(path, &st) == 0)
return st.st_size;
return 0;
}
/*
* return extension of a file (".c", ".s" etc.)
* (if no extension, return pointer to end-of-string)
*/
char *file_ext(const char *file)
{
char *base = file_basename(file);
char *dot = strrchr(base, '.');
return dot ? dot : strchr(base, 0);
}
/*
* change extension of a file to `ext`
* (if no extension, add `ext` to filename)
*/
char *ch_file_ext(const char *file, const char *ext)
{
char *path, *dot, *part;
path = xmalloc(strlen(file) + strlen(ext) + 2);
dot = file_ext(file);
if (dot[0]) {
part = strrchr(file, '.');
strncpy(path, file, part - file + 1);
strcpy(path + (part - file + 1), ext);
} else {
strcpy(path, file);
strcat(path, ".");
strcat(path, ext);
}
return path;
}
char *fullpath(const char *path)
{
if (path == NULL)
return NULL;
if (path[0] == '~' && path[1] == '/')
return joinpath(getenv("HOME"), path + 2);
else
return strdup(path);
}
char *joinpath(const char *dir, const char *name)
{
char *path;
size_t len;
if (dir == NULL || dir[0] == 0)
return name ? strdup(name) : NULL;
if (name == NULL || name[0] == 0)
return dir ? strdup(dir) : NULL;
if (name[0] == '/')
return strdup(name);
len = strlen(dir);
path = xmalloc(len + strlen(name) + 2);
strcpy(path, dir);
if (path[len - 1] != '/')
path[len++] = '/';
strcpy(path + len, name);
return path;
}
char *file_basename(const char *name)
{
char *p = strchr(name, 0);
while (p > name && !IS_DIRSEP(p[-1]))
p--;
return p;
}
char *file_dirname(const char *p)
{
return dirname(strdup(p));
}
char *mktempfile(const char *suffix)
{
char buf[32];
int fd;
if (suffix)
snprintf(buf, sizeof(buf), "/tmp/XXXXXX.%s", suffix);
else
snprintf(buf, sizeof(buf), "/tmp/XXXXXX");
fd = mkstemps(buf, strlen(buf) - strlen("/tmp/XXXXXX"));
if (fd < 0 || close(fd) < 0)
die_errno("can't make temporary filename");
return strdup(buf);
}
#define STR_HASH_INIT 5381
#define STR_HASH_STEP(h, c) (((h) << 5) + (h) + (c))
unsigned int strhash(const char *s)
{
unsigned int hash = STR_HASH_INIT;
int c;
while ((c = *s++))
hash = STR_HASH_STEP(hash, c);
return hash;
}
unsigned int strnhash(const char *s, size_t len)
{
unsigned int hash = STR_HASH_INIT;
for (const char *d = s + len; s < d; s++)
hash = STR_HASH_STEP(hash, *s);
return hash;
}
char *vstringf(const char *fmt, va_list ap)
{
char buf[BUFSIZ];
va_list ap2;
int total;
char *d;
va_copy(ap2, ap);
total = vsnprintf(buf, NELEMS(buf), fmt, ap);
if (total >= NELEMS(buf)) {
d = xmalloc(total + 8);
if (vsnprintf(d, total + 8, fmt, ap2) > total)
die("vstringf: how can this happend");
return d;
}
return strdup(buf);
}
char *stringf(const char *fmt, ...)
{
char *str;
va_list ap;
va_start(ap, fmt);
str = vstringf(fmt, ap);
va_end(ap);
return str;
}
int strstart(const char *prefix, char **pstr)
{
char *str;
size_t n;
if (!prefix || !pstr || !(str = *pstr))
return 0;
n = strlen(prefix);
if (strncmp(str, prefix, n) != 0)
return 0;
*pstr = str + n;
return 1;
}
/* for multi byte and wide char conversion. */
static void locale_init_once(void)
{
static int once;
if (!once) {
once = 1;
setlocale(LC_ALL, "");
}
}
int mb2wc(wchar_t *wc, const char *s, size_t n)
{
locale_init_once();
return mbtowc(wc, s, n);
}
size_t mbs2wcs(wchar_t *pwcs, const char *s, size_t n)
{
locale_init_once();
return mbstowcs(pwcs, s, n);
}
int wc2mb(char *s, wchar_t wc)
{
locale_init_once();
return wctomb(s, wc);
}
size_t wcs2mbs(char *s, const wchar_t *pwcs, size_t n)
{
locale_init_once();
return wcstombs(s, pwcs, n);
}
/* strbuf */
static void strbuf_grow(struct strbuf *sb, size_t new_size)
{
size_t size = sb->alloc;
if (size == 0)
size = 12; /* maybe a small string */
while (size < new_size)
size = size * 2;
sb->str = xrealloc(sb->str, size);
sb->alloc = size;
}
void strbuf_new(struct strbuf *sb)
{
memset(sb, 0, sizeof(struct strbuf));
}
void strbuf_free(struct strbuf *sb)
{
free(sb->str);
strbuf_new(sb);
}
void strbuf_reset(struct strbuf *sb)
{
sb->len = 0;
}
void strbuf_catc(struct strbuf *sb, int ch)
{
size_t size;
size = sb->len + 1;
if (size >= sb->alloc)
strbuf_grow(sb, size);
sb->str[sb->len] = ch;
sb->len++;
}
void strbuf_catn(struct strbuf *sb, const char *str, size_t len)
{
while (len--) {
int c = *str;
strbuf_catc(sb, c);
str++;
}
}
void strbuf_cats(struct strbuf *sb, const char *str)
{
while (1) {
int c = *str;
if (c == '\0')
break;
strbuf_catc(sb, c);
str++;
}
}
void strbuf_catf(struct strbuf *sb, const char *fmt, ...)
{
char *str;
va_list ap;
va_start(ap, fmt);
str = vstringf(fmt, ap);
va_end(ap);
strbuf_cats(sb, str);
free(str);
}
/* list */
static struct list *freelists;
void append(struct list **list, void *x)
{
struct list *new;
struct list *old = *list;
if ((new = freelists) != NULL)
freelists = freelists->link;
else
new = xmalloc(sizeof(*list));
if (old) {
new->link = old->link;
old->link = new;
} else {
new->link = new;
}
new->x = x;
*list = new;
}
size_t nlist(struct list *list)
{
size_t n = 0;
if (list) {
struct list *p = list;
do
n++;
while ((p = p->link) != list);
}
return n;
}
void *ltoa(struct list **list)
{
int i = 0;
void **array = xmalloc(sizeof(array[0]) * (nlist(*list) + 1));
if (*list) {
struct list *p = *list;
do {
p = p->link;
array[i++] = p->x;
} while (p != *list);
p = (*list)->link;
(*list)->link = freelists;
freelists = p;
}
*list = NULL;
array[i] = NULL;
return array;
}
size_t length(void *array)
{
size_t i;
void **a;
if (array == NULL)
return 0;
i = 0;
a = (void **)array;
while (a[i])
i++;
return i;
}