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