fix misleading placement of statement on same line as for loop in ldso
[musl] / src / regex / glob.c
1 #define _BSD_SOURCE
2 #include <glob.h>
3 #include <fnmatch.h>
4 #include <sys/stat.h>
5 #include <dirent.h>
6 #include <limits.h>
7 #include <string.h>
8 #include <stdlib.h>
9 #include <errno.h>
10 #include <stddef.h>
11
12 struct match
13 {
14         struct match *next;
15         char name[];
16 };
17
18 static int append(struct match **tail, const char *name, size_t len, int mark)
19 {
20         struct match *new = malloc(sizeof(struct match) + len + 2);
21         if (!new) return -1;
22         (*tail)->next = new;
23         new->next = NULL;
24         memcpy(new->name, name, len+1);
25         if (mark && len && name[len-1]!='/') {
26                 new->name[len] = '/';
27                 new->name[len+1] = 0;
28         }
29         *tail = new;
30         return 0;
31 }
32
33 static int do_glob(char *buf, size_t pos, int type, char *pat, int flags, int (*errfunc)(const char *path, int err), struct match **tail)
34 {
35         /* If GLOB_MARK is unused, we don't care about type. */
36         if (!type && !(flags & GLOB_MARK)) type = DT_REG;
37
38         /* Special-case the remaining pattern being all slashes, in
39          * which case we can use caller-passed type if it's a dir. */
40         if (*pat && type!=DT_DIR) type = 0;
41         while (pos+1 < PATH_MAX && *pat=='/') buf[pos++] = *pat++;
42
43         /* Consume maximal [escaped-]literal prefix of pattern, copying
44          * and un-escaping it to the running buffer as we go. */
45         ptrdiff_t i=0, j=0;
46         int in_bracket = 0, overflow = 0;
47         for (; pat[i]!='*' && pat[i]!='?' && (!in_bracket || pat[i]!=']'); i++) {
48                 if (!pat[i]) {
49                         if (overflow) return 0;
50                         pat += i;
51                         pos += j;
52                         i = j = 0;
53                         break;
54                 } else if (pat[i] == '[') {
55                         in_bracket = 1;
56                 } else if (pat[i] == '\\' && !(flags & GLOB_NOESCAPE)) {
57                         /* Backslashes inside a bracket are (at least by
58                          * our interpretation) non-special, so if next
59                          * char is ']' we have a complete expression. */
60                         if (in_bracket && pat[i+1]==']') break;
61                         /* Unpaired final backslash never matches. */
62                         if (!pat[i+1]) return 0;
63                         i++;
64                 }
65                 if (pat[i] == '/') {
66                         if (overflow) return 0;
67                         in_bracket = 0;
68                         pat += i+1;
69                         i = -1;
70                         pos += j+1;
71                         j = -1;
72                 }
73                 /* Only store a character if it fits in the buffer, but if
74                  * a potential bracket expression is open, the overflow
75                  * must be remembered and handled later only if the bracket
76                  * is unterminated (and thereby a literal), so as not to
77                  * disallow long bracket expressions with short matches. */
78                 if (pos+(j+1) < PATH_MAX) {
79                         buf[pos+j++] = pat[i];
80                 } else if (in_bracket) {
81                         overflow = 1;
82                 } else {
83                         return 0;
84                 }
85                 /* If we consume any new components, the caller-passed type
86                  * or dummy type from above is no longer valid. */
87                 type = 0;
88         }
89         buf[pos] = 0;
90         if (!*pat) {
91                 /* If we consumed any components above, or if GLOB_MARK is
92                  * requested and we don't yet know if the match is a dir,
93                  * we must call stat to confirm the file exists and/or
94                  * determine its type. */
95                 struct stat st;
96                 if ((flags & GLOB_MARK) && type==DT_LNK) type = 0;
97                 if (!type && stat(buf, &st)) {
98                         if (errno!=ENOENT && (errfunc(buf, errno) || (flags & GLOB_ERR)))
99                                 return GLOB_ABORTED;
100                         return 0;
101                 }
102                 if (!type && S_ISDIR(st.st_mode)) type = DT_DIR;
103                 if (append(tail, buf, pos, (flags & GLOB_MARK) && type==DT_DIR))
104                         return GLOB_NOSPACE;
105                 return 0;
106         }
107         char *p2 = strchr(pat, '/'), saved_sep = '/';
108         /* Check if the '/' was escaped and, if so, remove the escape char
109          * so that it will not be unpaired when passed to fnmatch. */
110         if (p2 && !(flags & GLOB_NOESCAPE)) {
111                 char *p;
112                 for (p=p2; p>pat && p[-1]=='\\'; p--);
113                 if ((p2-p)%2) {
114                         p2--;
115                         saved_sep = '\\';
116                 }
117         }
118         DIR *dir = opendir(pos ? buf : ".");
119         if (!dir) {
120                 if (errfunc(buf, errno) || (flags & GLOB_ERR))
121                         return GLOB_ABORTED;
122                 return 0;
123         }
124         int old_errno = errno;
125         struct dirent *de;
126         while (errno=0, de=readdir(dir)) {
127                 /* Quickly skip non-directories when there's pattern left. */
128                 if (p2 && de->d_type && de->d_type!=DT_DIR && de->d_type!=DT_LNK)
129                         continue;
130
131                 size_t l = strlen(de->d_name);
132                 if (l >= PATH_MAX-pos) continue;
133
134                 if (p2) *p2 = 0;
135
136                 int fnm_flags= ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
137                         | ((!(flags & GLOB_PERIOD)) ? FNM_PERIOD : 0);
138
139                 if (fnmatch(pat, de->d_name, fnm_flags))
140                         continue;
141
142                 /* With GLOB_PERIOD, don't allow matching . or .. unless
143                  * fnmatch would match them with FNM_PERIOD rules in effect. */
144                 if (p2 && (flags & GLOB_PERIOD) && de->d_name[0]=='.'
145                     && (!de->d_name[1] || de->d_name[1]=='.' && !de->d_name[2])
146                     && fnmatch(pat, de->d_name, fnm_flags | FNM_PERIOD))
147                         continue;
148
149                 memcpy(buf+pos, de->d_name, l+1);
150                 if (p2) *p2 = saved_sep;
151                 int r = do_glob(buf, pos+l, de->d_type, p2 ? p2 : "", flags, errfunc, tail);
152                 if (r) {
153                         closedir(dir);
154                         return r;
155                 }
156         }
157         int readerr = errno;
158         if (p2) *p2 = saved_sep;
159         closedir(dir);
160         if (readerr && (errfunc(buf, errno) || (flags & GLOB_ERR)))
161                 return GLOB_ABORTED;
162         errno = old_errno;
163         return 0;
164 }
165
166 static int ignore_err(const char *path, int err)
167 {
168         return 0;
169 }
170
171 static void freelist(struct match *head)
172 {
173         struct match *match, *next;
174         for (match=head->next; match; match=next) {
175                 next = match->next;
176                 free(match);
177         }
178 }
179
180 static int sort(const void *a, const void *b)
181 {
182         return strcmp(*(const char **)a, *(const char **)b);
183 }
184
185 int glob(const char *restrict pat, int flags, int (*errfunc)(const char *path, int err), glob_t *restrict g)
186 {
187         struct match head = { .next = NULL }, *tail = &head;
188         size_t cnt, i;
189         size_t offs = (flags & GLOB_DOOFFS) ? g->gl_offs : 0;
190         int error = 0;
191         char buf[PATH_MAX];
192         
193         if (!errfunc) errfunc = ignore_err;
194
195         if (!(flags & GLOB_APPEND)) {
196                 g->gl_offs = offs;
197                 g->gl_pathc = 0;
198                 g->gl_pathv = NULL;
199         }
200
201         if (*pat) {
202                 char *p = strdup(pat);
203                 if (!p) return GLOB_NOSPACE;
204                 buf[0] = 0;
205                 error = do_glob(buf, 0, 0, p, flags, errfunc, &tail);
206                 free(p);
207         }
208
209         if (error == GLOB_NOSPACE) {
210                 freelist(&head);
211                 return error;
212         }
213         
214         for (cnt=0, tail=head.next; tail; tail=tail->next, cnt++);
215         if (!cnt) {
216                 if (flags & GLOB_NOCHECK) {
217                         tail = &head;
218                         if (append(&tail, pat, strlen(pat), 0))
219                                 return GLOB_NOSPACE;
220                         cnt++;
221                 } else
222                         return GLOB_NOMATCH;
223         }
224
225         if (flags & GLOB_APPEND) {
226                 char **pathv = realloc(g->gl_pathv, (offs + g->gl_pathc + cnt + 1) * sizeof(char *));
227                 if (!pathv) {
228                         freelist(&head);
229                         return GLOB_NOSPACE;
230                 }
231                 g->gl_pathv = pathv;
232                 offs += g->gl_pathc;
233         } else {
234                 g->gl_pathv = malloc((offs + cnt + 1) * sizeof(char *));
235                 if (!g->gl_pathv) {
236                         freelist(&head);
237                         return GLOB_NOSPACE;
238                 }
239                 for (i=0; i<offs; i++)
240                         g->gl_pathv[i] = NULL;
241         }
242         for (i=0, tail=head.next; i<cnt; tail=tail->next, i++)
243                 g->gl_pathv[offs + i] = tail->name;
244         g->gl_pathv[offs + i] = NULL;
245         g->gl_pathc += cnt;
246
247         if (!(flags & GLOB_NOSORT))
248                 qsort(g->gl_pathv+offs, cnt, sizeof(char *), sort);
249         
250         return error;
251 }
252
253 void globfree(glob_t *g)
254 {
255         size_t i;
256         for (i=0; i<g->gl_pathc; i++)
257                 free(g->gl_pathv[g->gl_offs + i] - offsetof(struct match, name));
258         free(g->gl_pathv);
259         g->gl_pathc = 0;
260         g->gl_pathv = NULL;
261 }
262
263 weak_alias(glob, glob64);
264 weak_alias(globfree, globfree64);