1#include <glob.h>
2#include <fnmatch.h>
3#include <sys/stat.h>
4#include <dirent.h>
5#include <limits.h>
6#include <string.h>
7#include <stdlib.h>
8#include <errno.h>
9#include <stddef.h>
10
11struct match
12{
13 struct match *next;
14 char name[1];
15};
16
17static int is_literal(const char *p, int useesc)
18{
19 int bracket = 0;
20 for (; *p; p++) {
21 switch (*p) {
22 case '\\':
23 if (!useesc) break;
24 case '?':
25 case '*':
26 return 0;
27 case '[':
28 bracket = 1;
29 break;
30 case ']':
31 if (bracket) return 0;
32 break;
33 }
34 }
35 return 1;
36}
37
38static int append(struct match **tail, const char *name, size_t len, int mark)
39{
40 struct match *new = malloc(sizeof(struct match) + len + 1);
41 if (!new) return -1;
42 (*tail)->next = new;
43 new->next = NULL;
44 strcpy(new->name, name);
45 if (mark) strcat(new->name, "/");
46 *tail = new;
47 return 0;
48}
49
50static int match_in_dir(const char *d, const char *p, int flags, int (*errfunc)(const char *path, int err), struct match **tail)
51{
52 DIR *dir;
53 struct dirent *de;
54 char pat[strlen(p)+1];
55 char *p2;
56 size_t l = strlen(d);
57 int literal;
58 int fnm_flags= ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
59 | ((!(flags & GLOB_PERIOD)) ? FNM_PERIOD : 0);
60 int error;
61
62 if ((p2 = strchr(p, '/'))) {
63 strcpy(pat, p);
64 pat[p2-p] = 0;
65 for (; *p2 == '/'; p2++);
66 p = pat;
67 }
68 literal = is_literal(p, !(flags & GLOB_NOESCAPE));
69 if (*d == '/' && !*(d+1)) l = 0;
70
71 /* rely on opendir failing for nondirectory objects */
72 dir = opendir(*d ? d : ".");
73 error = errno;
74 if (!dir) {
75 /* this is not an error -- we let opendir call stat for us */
76 if (error == ENOTDIR) return 0;
77 if (error == EACCES && !*p) {
78 struct stat st;
79 if (!stat(d, &st) && S_ISDIR(st.st_mode)) {
80 if (append(tail, d, l, l))
81 return GLOB_NOSPACE;
82 return 0;
83 }
84 }
85 if (errfunc(d, error) || (flags & GLOB_ERR))
86 return GLOB_ABORTED;
87 return 0;
88 }
89 if (!*p) {
90 error = append(tail, d, l, l) ? GLOB_NOSPACE : 0;
91 closedir(dir);
92 return error;
93 }
94 while ((de = readdir(dir))) {
95 char namebuf[l+de->d_reclen+2], *name = namebuf;
96 if (!literal && fnmatch(p, de->d_name, fnm_flags))
97 continue;
98 if (literal && strcmp(p, de->d_name))
99 continue;
100 if (p2 && de->d_type && !S_ISDIR(de->d_type<<12) && !S_ISLNK(de->d_type<<12))
101 continue;
102 /* With GLOB_PERIOD, don't allow matching . or .. unless
103 * fnmatch would match them with FNM_PERIOD rules in effect. */
104 if (p2 && (flags & GLOB_PERIOD) && de->d_name[0]=='.'
105 && (!de->d_name[1] || (de->d_name[1]=='.' && !de->d_name[2]))
106 && fnmatch(p, de->d_name, fnm_flags | FNM_PERIOD))
107 continue;
108 if (*d) {
109 memcpy(name, d, l);
110 name[l] = '/';
111 strcpy(name+l+1, de->d_name);
112 } else {
113 name = de->d_name;
114 }
115 if (p2) {
116 if ((error = match_in_dir(name, p2, flags, errfunc, tail))) {
117 closedir(dir);
118 return error;
119 }
120 } else {
121 int mark = 0;
122 if (flags & GLOB_MARK) {
123 if (de->d_type && !S_ISLNK(de->d_type<<12))
124 mark = S_ISDIR(de->d_type<<12);
125 else {
126 struct stat st;
127 stat(name, &st);
128 mark = S_ISDIR(st.st_mode);
129 }
130 }
131 if (append(tail, name, l+de->d_reclen+1, mark)) {
132 closedir(dir);
133 return GLOB_NOSPACE;
134 }
135 }
136 }
137 closedir(dir);
138 if (error && (errfunc(d, error) || (flags & GLOB_ERR)))
139 return GLOB_ABORTED;
140 return 0;
141}
142
143static int ignore_err(const char *path, int err)
144{
145 return 0;
146}
147
148static void freelist(struct match *head)
149{
150 struct match *match, *next;
151 for (match=head->next; match; match=next) {
152 next = match->next;
153 free(match);
154 }
155}
156
157static int sort(const void *a, const void *b)
158{
159 return strcmp(*(const char **)a, *(const char **)b);
160}
161
162int glob(const char *restrict pat, int flags, int (*errfunc)(const char *path, int err), glob_t *restrict g)
163{
164 const char *p=pat, *d;
165 struct match head = { .next = NULL }, *tail = &head;
166 size_t cnt, i;
167 size_t offs = (flags & GLOB_DOOFFS) ? g->gl_offs : 0;
168 int error = 0;
169
170 if (*p == '/') {
171 for (; *p == '/'; p++);
172 d = "/";
173 } else {
174 d = "";
175 }
176
177 if (!errfunc) errfunc = ignore_err;
178
179 if (!(flags & GLOB_APPEND)) {
180 g->gl_offs = offs;
181 g->gl_pathc = 0;
182 g->gl_pathv = NULL;
183 }
184
185 if (strnlen(p, PATH_MAX+1) > PATH_MAX) return GLOB_NOSPACE;
186
187 if (*pat) error = match_in_dir(d, p, flags, errfunc, &tail);
188 if (error == GLOB_NOSPACE) {
189 freelist(&head);
190 return error;
191 }
192
193 for (cnt=0, tail=head.next; tail; tail=tail->next, cnt++);
194 if (!cnt) {
195 if (flags & GLOB_NOCHECK) {
196 tail = &head;
197 if (append(&tail, pat, strlen(pat), 0))
198 return GLOB_NOSPACE;
199 cnt++;
200 } else
201 return GLOB_NOMATCH;
202 }
203
204 if (flags & GLOB_APPEND) {
205 char **pathv = realloc(g->gl_pathv, (offs + g->gl_pathc + cnt + 1) * sizeof(char *));
206 if (!pathv) {
207 freelist(&head);
208 return GLOB_NOSPACE;
209 }
210 g->gl_pathv = pathv;
211 offs += g->gl_pathc;
212 } else {
213 g->gl_pathv = malloc((offs + cnt + 1) * sizeof(char *));
214 if (!g->gl_pathv) {
215 freelist(&head);
216 return GLOB_NOSPACE;
217 }
218 for (i=0; i<offs; i++)
219 g->gl_pathv[i] = NULL;
220 }
221 for (i=0, tail=head.next; i<cnt; tail=tail->next, i++)
222 g->gl_pathv[offs + i] = tail->name;
223 g->gl_pathv[offs + i] = NULL;
224 g->gl_pathc += cnt;
225
226 if (!(flags & GLOB_NOSORT))
227 qsort(g->gl_pathv+offs, cnt, sizeof(char *), sort);
228
229 return error;
230}
231
232void globfree(glob_t *g)
233{
234 size_t i;
235 for (i=0; i<g->gl_pathc; i++)
236 free(g->gl_pathv[g->gl_offs + i] - offsetof(struct match, name));
237 free(g->gl_pathv);
238 g->gl_pathc = 0;
239 g->gl_pathv = NULL;
240}
241