-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxs_glob.h
56 lines (37 loc) · 1.16 KB
/
xs_glob.h
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
/* copyright (c) 2022 grunfink - MIT license */
#ifndef _XS_GLOB_H
#define _XS_GLOB_H
d_char *xs_glob_n(const char *spec, int basename, int reverse, int max);
#define xs_glob(spec, basename, reverse) xs_glob_n(spec, basename, reverse, XS_ALL)
#ifdef XS_IMPLEMENTATION
#include <glob.h>
d_char *xs_glob_n(const char *spec, int basename, int reverse, int max)
/* does a globbing and returns the found files */
{
glob_t globbuf;
d_char *list = xs_list_new();
if (glob(spec, 0, NULL, &globbuf) == 0) {
int n;
if (max > globbuf.gl_pathc)
max = globbuf.gl_pathc;
for (n = 0; n < max; n++) {
char *p;
if (reverse)
p = globbuf.gl_pathv[globbuf.gl_pathc - n - 1];
else
p = globbuf.gl_pathv[n];
if (p != NULL) {
if (basename) {
if ((p = strrchr(p, '/')) == NULL)
continue;
p++;
}
list = xs_list_append(list, p);
}
}
}
globfree(&globbuf);
return list;
}
#endif /* XS_IMPLEMENTATION */
#endif /* _XS_GLOB_H */