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