-
Notifications
You must be signed in to change notification settings - Fork 0
/
namecheck.c
206 lines (188 loc) · 4.05 KB
/
namecheck.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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "memory.h"
#include "namecheck.h"
enum pattern_type {/*{{{*/
PT_EXACT,
PT_STARTS_WITH,
PT_ENDS_WITH
};
/*}}}*/
struct pattern {/*{{{*/
struct pattern *next;
int pass; /* 1 for a positve pattern, 0 for a negative one */
enum pattern_type type;
char *pattern;
};
/*}}}*/
struct namecheck {/*{{{*/
struct pattern *head;
struct pattern *tail;
};
/*}}}*/
static void map_line(char *line, struct namecheck *nc)/*{{{*/
{
char *p, *q;
int len;
int negate = 0;
int wild_at_start = 0;
int wild_at_end = 0;
struct pattern *np;
p = line;
len = strlen(line);
if (line[len-1] == '\n') line[len-1] = 0;
/* Allow comments */
if (*p == '#') return;
if (*p == '!') {
negate = 1;
p++;
}
while (*p && isspace(*p)) p++;
if (*p == '*') {
wild_at_start = 1;
p++;
}
if (*p) {
q = p;
while (*q) {
if ((q[0] == '*') && !q[1]) {
wild_at_end = 1;
q[0] = 0;
break;
}
q++;
}
}
if (wild_at_start && wild_at_end) {
fprintf(stderr, "Cannot have pattern that is wild at both start and end : [%s]\n", line);
exit(1);
}
/* Now build the record. */
np = new(struct pattern);
if (wild_at_start) np->type = PT_ENDS_WITH;
else if (wild_at_end) np->type = PT_STARTS_WITH;
else np->type = PT_EXACT;
if (p) {
np->pattern = new_string(p);
} else {
np->pattern = NULL; /* total wildcard */
}
np->pass = negate ? 0 : 1;
np->next = NULL;
if (!nc->head) {
nc->head = nc->tail = np;
} else {
nc->tail->next = np;
nc->tail = np;
}
return;
}
/*}}}*/
/* Functions for comparing a filename against a list of patterns
* and returning a boolean. */
struct namecheck *make_namecheck(char *filename)/*{{{*/
{
struct namecheck *result;
FILE *in;
char line[1024];
in = fopen(filename, "r");
if (in) {
result = new(struct namecheck);
result->head = result->tail = NULL;
while (fgets(line, sizeof(line), in)) {
map_line(line, result);
}
fclose(in);
} else {
result = NULL;
}
return result;
}/*}}}*/
struct namecheck *make_namecheck_dir(const char *dir, const char *filename)/*{{{*/
{
int l1, l2;
char *temp;
struct namecheck *result;
l1 = strlen(dir);
l2 = strlen(filename);
temp = new_array(char, l1 + l2 + 2);
strcpy(temp, dir);
strcat(temp, "/");
strcat(temp, filename);
result = make_namecheck(temp);
free(temp);
return result;
}
/*}}}*/
void free_namecheck(struct namecheck *nc)/*{{{*/
{
struct pattern *p, *np;
for (p=nc->head; p; p=np) {
np = p->next;
free(p->pattern);
free(p);
}
free(nc);
}
/*}}}*/
static int ends_with(const char *name, const char *pattern) {/*{{{*/
int len, patlen;
len = strlen(name);
patlen = strlen(pattern);
if (len > patlen) {
if (!strcmp(name+len-patlen, pattern)) {
return 1;
} else {
return 0;
}
} else {
return 0;
}
}
/*}}}*/
static int starts_with(const char *name, const char *pattern)/*{{{*/
{
if (pattern) {
int patlen = strlen(pattern);
if (!strncmp(name, pattern, patlen)) {
return 1;
} else {
return 0;
}
} else {
/* Full wildcard, i.e. '*' or '!*' */
return 1;
}
}
/*}}}*/
enum nc_result lookup_namecheck(const struct namecheck *nc, const char *filename)/*{{{*/
{
/* It's always the FIRST match that prevails - this is quicker than searching
* for the last match! */
struct pattern *p;
for (p=nc->head; p; p=p->next) {
switch (p->type) {
case PT_EXACT:
if (!strcmp(filename, p->pattern)) {
if (p->pass) return NC_PASS;
else return NC_FAIL;
}
break;
case PT_STARTS_WITH:
if (starts_with(filename, p->pattern)) {
if (p->pass) return NC_PASS;
else return NC_FAIL;
}
break;
case PT_ENDS_WITH:
if (ends_with(filename, p->pattern)) {
if (p->pass) return NC_PASS;
else return NC_FAIL;
}
break;
}
}
return NC_UNMATCHED;
}
/*}}}*/