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