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