forked from cloudflare/doom-wasm
-
Notifications
You must be signed in to change notification settings - Fork 3
/
i_glob.c
373 lines (322 loc) · 8.33 KB
/
i_glob.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
//
// Copyright(C) 2018 Simon Howard
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//
// File globbing API. This allows the contents of the filesystem
// to be interrogated.
//
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "i_glob.h"
#include "m_misc.h"
#include "config.h"
#if defined(_MSC_VER)
// For Visual C++, we need to include the win_opendir module.
#include <win_opendir.h>
#include <sys/stat.h>
#define S_ISDIR(m) (((m)& S_IFMT) == S_IFDIR)
#elif defined(HAVE_DIRENT_H)
#include <dirent.h>
#include <sys/stat.h>
#elif defined(__WATCOMC__)
// Watcom has the same API in a different header.
#include <direct.h>
#else
#define NO_DIRENT_IMPLEMENTATION
#endif
#ifndef NO_DIRENT_IMPLEMENTATION
// Only the fields d_name and (as an XSI extension) d_ino are specified
// in POSIX.1. Other than Linux, the d_type field is available mainly
// only on BSD systems. The remaining fields are available on many, but
// not all systems.
static boolean IsDirectory(char *dir, struct dirent *de)
{
#if defined(_DIRENT_HAVE_D_TYPE)
if (de->d_type != DT_UNKNOWN && de->d_type != DT_LNK)
{
return de->d_type == DT_DIR;
}
else
#endif
{
char *filename;
struct stat sb;
int result;
filename = M_StringJoin(dir, DIR_SEPARATOR_S, de->d_name, NULL);
result = stat(filename, &sb);
free(filename);
if (result != 0)
{
return false;
}
return S_ISDIR(sb.st_mode);
}
}
struct glob_s
{
char **globs;
int num_globs;
int flags;
DIR *dir;
char *directory;
char *last_filename;
// These fields are only used when the GLOB_FLAG_SORTED flag is set:
char **filenames;
int filenames_len;
int next_index;
};
static void FreeStringList(char **globs, int num_globs)
{
int i;
for (i = 0; i < num_globs; ++i)
{
free(globs[i]);
}
free(globs);
}
glob_t *I_StartMultiGlob(const char *directory, int flags,
const char *glob, ...)
{
char **globs;
int num_globs;
glob_t *result;
va_list args;
globs = malloc(sizeof(char *));
if (globs == NULL)
{
return NULL;
}
globs[0] = M_StringDuplicate(glob);
num_globs = 1;
va_start(args, glob);
for (;;)
{
const char *arg = va_arg(args, const char *);
char **new_globs;
if (arg == NULL)
{
break;
}
new_globs = realloc(globs, sizeof(char *) * (num_globs + 1));
if (new_globs == NULL)
{
FreeStringList(globs, num_globs);
}
globs = new_globs;
globs[num_globs] = M_StringDuplicate(arg);
++num_globs;
}
va_end(args);
result = malloc(sizeof(glob_t));
if (result == NULL)
{
FreeStringList(globs, num_globs);
return NULL;
}
result->dir = opendir(directory);
if (result->dir == NULL)
{
FreeStringList(globs, num_globs);
free(result);
return NULL;
}
result->directory = M_StringDuplicate(directory);
result->globs = globs;
result->num_globs = num_globs;
result->flags = flags;
result->last_filename = NULL;
result->filenames = NULL;
result->filenames_len = 0;
result->next_index = -1;
return result;
}
glob_t *I_StartGlob(const char *directory, const char *glob, int flags)
{
return I_StartMultiGlob(directory, flags, glob, NULL);
}
void I_EndGlob(glob_t *glob)
{
if (glob == NULL)
{
return;
}
FreeStringList(glob->globs, glob->num_globs);
FreeStringList(glob->filenames, glob->filenames_len);
free(glob->directory);
free(glob->last_filename);
(void) closedir(glob->dir);
free(glob);
}
static boolean MatchesGlob(const char *name, const char *glob, int flags)
{
int n, g;
while (*glob != '\0')
{
n = *name;
g = *glob;
if ((flags & GLOB_FLAG_NOCASE) != 0)
{
n = tolower(n);
g = tolower(g);
}
if (g == '*')
{
// To handle *-matching we skip past the * and recurse
// to check each subsequent character in turn. If none
// match then the whole match is a failure.
while (*name != '\0')
{
if (MatchesGlob(name, glob + 1, flags))
{
return true;
}
++name;
}
return glob[1] == '\0';
}
else if (g != '?' && n != g)
{
// For normal characters the name must match the glob,
// but for ? we don't care what the character is.
return false;
}
++name;
++glob;
}
// Match successful when glob and name end at the same time.
return *name == '\0';
}
static boolean MatchesAnyGlob(const char *name, glob_t *glob)
{
int i;
for (i = 0; i < glob->num_globs; ++i)
{
if (MatchesGlob(name, glob->globs[i], glob->flags))
{
return true;
}
}
return false;
}
static char *NextGlob(glob_t *glob)
{
struct dirent *de;
do
{
de = readdir(glob->dir);
if (de == NULL)
{
return NULL;
}
} while (IsDirectory(glob->directory, de)
|| !MatchesAnyGlob(de->d_name, glob));
// Return the fully-qualified path, not just the bare filename.
return M_StringJoin(glob->directory, DIR_SEPARATOR_S, de->d_name, NULL);
}
static void ReadAllFilenames(glob_t *glob)
{
char *name;
glob->filenames = NULL;
glob->filenames_len = 0;
glob->next_index = 0;
for (;;)
{
name = NextGlob(glob);
if (name == NULL)
{
break;
}
glob->filenames = realloc(glob->filenames,
(glob->filenames_len + 1) * sizeof(char *));
glob->filenames[glob->filenames_len] = name;
++glob->filenames_len;
}
}
static void SortFilenames(char **filenames, int len, int flags)
{
char *pivot, *tmp;
int i, left_len, cmp;
if (len <= 1)
{
return;
}
pivot = filenames[len - 1];
left_len = 0;
for (i = 0; i < len-1; ++i)
{
if ((flags & GLOB_FLAG_NOCASE) != 0)
{
cmp = strcasecmp(filenames[i], pivot);
}
else
{
cmp = strcmp(filenames[i], pivot);
}
if (cmp < 0)
{
tmp = filenames[i];
filenames[i] = filenames[left_len];
filenames[left_len] = tmp;
++left_len;
}
}
filenames[len - 1] = filenames[left_len];
filenames[left_len] = pivot;
SortFilenames(filenames, left_len, flags);
SortFilenames(&filenames[left_len + 1], len - left_len - 1, flags);
}
const char *I_NextGlob(glob_t *glob)
{
const char *result;
if (glob == NULL)
{
return NULL;
}
// In unsorted mode we just return the filenames as we read
// them back from the system API.
if ((glob->flags & GLOB_FLAG_SORTED) == 0)
{
free(glob->last_filename);
glob->last_filename = NextGlob(glob);
return glob->last_filename;
}
// In sorted mode we read the whole list of filenames into memory,
// sort them and return them one at a time.
if (glob->next_index < 0)
{
ReadAllFilenames(glob);
SortFilenames(glob->filenames, glob->filenames_len, glob->flags);
}
if (glob->next_index >= glob->filenames_len)
{
return NULL;
}
result = glob->filenames[glob->next_index];
++glob->next_index;
return result;
}
#else /* #ifdef NO_DIRENT_IMPLEMENTATION */
#warning No native implementation of file globbing.
glob_t *I_StartGlob(const char *directory, const char *glob, int flags)
{
return NULL;
}
void I_EndGlob(glob_t *glob)
{
}
const char *I_NextGlob(glob_t *glob)
{
return "";
}
#endif /* #ifdef NO_DIRENT_IMPLEMENTATION */