forked from andbof/yastg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ptrarray.c
110 lines (92 loc) · 2.08 KB
/
ptrarray.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/file.h>
#include <sys/mman.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "ptrarray.h"
struct ptrarray* ptrarray_create()
{
struct ptrarray *a = malloc(sizeof(*a));
memset(a, 0, sizeof(*a));
return a;
}
void ptrarray_free(struct ptrarray *a)
{
assert(a);
for (size_t i = 0; i < a->used; i++)
free(a->array[i]);
free(a);
}
#define PTRARRAY_MIN_ALLOC 16
struct ptrarray* ptrarray_add(struct ptrarray *a, void *ptr)
{
struct ptrarray *n;
assert(a->used <= a->alloc);
if (a->used == a->alloc) {
if (a->alloc == 0)
a->alloc = PTRARRAY_MIN_ALLOC / 2;
n = realloc(a, (a->alloc * 2)*sizeof(void*) + sizeof(*a));
if (n == NULL) /* FIXME, this should probably be detected by the caller */
return a;
a = n;
a->alloc *= 2;
}
a->array[a->used++] = ptr;
return a;
}
struct ptrarray* file_to_ptrarray(const char * const fn, struct ptrarray *a)
{
FILE *f;
off_t size;
char *begin, *ptr, *end;
if ((f = fopen(fn, "r")) == NULL)
goto load_return;
if (flock(fileno(f), LOCK_SH) != 0)
goto load_close;
if (fseek(f, 0, SEEK_END) != 0)
goto load_close;
if ((size = ftell(f)) < 0)
goto load_close;
if (fseek(f, 0, SEEK_SET) != 0)
goto load_close;
if ((begin = mmap(NULL, size, PROT_READ,
MAP_SHARED | MAP_POPULATE, fileno(f), 0)) == NULL)
goto load_close;
end = begin + size;
ptr = begin;
unsigned int len;
char *name, *name_end;
while (ptr < end) {
while (ptr <= end && isspace(*ptr))
ptr++;
name_end = ptr;
while (name_end <= end && *name_end != '\n' && *name_end != '#')
name_end++;
len = name_end - ptr;
if (len > 0) {
name = malloc(len + 1);
if (name == NULL)
goto load_munmap;
memcpy(name, ptr, len);
while (isspace(name[len - 1]))
len--;
name[len] = '\0';
a = ptrarray_add(a, name);
}
/* Skip any comment following */
while (*name_end != '\n' && name_end <= end)
name_end++;
ptr = name_end + 1;
}
load_munmap:
munmap(ptr, size);
load_close:
fclose(f);
load_return:
return a;
}