]>
Commit | Line | Data |
---|---|---|
a65ea33b | 1 | /* $OpenBSD: glob.c,v 1.25 2005/08/08 08:05:34 espie Exp $ */ |
84ceda19 | 2 | /* |
3 | * Copyright (c) 1989, 1993 | |
4 | * The Regents of the University of California. All rights reserved. | |
5 | * | |
6 | * This code is derived from software contributed to Berkeley by | |
7 | * Guido van Rossum. | |
8 | * | |
9 | * Redistribution and use in source and binary forms, with or without | |
10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | |
12 | * 1. Redistributions of source code must retain the above copyright | |
13 | * notice, this list of conditions and the following disclaimer. | |
14 | * 2. Redistributions in binary form must reproduce the above copyright | |
15 | * notice, this list of conditions and the following disclaimer in the | |
16 | * documentation and/or other materials provided with the distribution. | |
3e67f7df | 17 | * 3. Neither the name of the University nor the names of its contributors |
84ceda19 | 18 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
f6d4fb87 | 34 | /* OPENBSD ORIGINAL: lib/libc/gen/glob.c */ |
35 | ||
84ceda19 | 36 | #include "includes.h" |
b5b88c19 | 37 | #include <sys/types.h> |
38 | #include <sys/stat.h> | |
39 | #include <dirent.h> | |
84ceda19 | 40 | #include <ctype.h> |
22bbb3e6 | 41 | #include <pwd.h> |
84ceda19 | 42 | |
d60382e0 | 43 | static long |
44 | get_arg_max(void) | |
9887f269 | 45 | { |
46 | #ifdef ARG_MAX | |
47 | return(ARG_MAX); | |
48 | #elif defined(HAVE_SYSCONF) && defined(_SC_ARG_MAX) | |
49 | return(sysconf(_SC_ARG_MAX)); | |
50 | #else | |
51 | return(256); /* XXX: arbitrary */ | |
52 | #endif | |
53 | } | |
54 | ||
40849fdb | 55 | #if !defined(HAVE_GLOB) || !defined(GLOB_HAS_ALTDIRFUNC) || \ |
56 | !defined(GLOB_HAS_GL_MATCHC) | |
84ceda19 | 57 | |
84ceda19 | 58 | /* |
59 | * glob(3) -- a superset of the one defined in POSIX 1003.2. | |
60 | * | |
61 | * The [!...] convention to negate a range is supported (SysV, Posix, ksh). | |
62 | * | |
63 | * Optional extra services, controlled by flags not defined by POSIX: | |
64 | * | |
65 | * GLOB_QUOTE: | |
66 | * Escaping convention: \ inhibits any special meaning the following | |
67 | * character might have (except \ at end of string is retained). | |
68 | * GLOB_MAGCHAR: | |
69 | * Set in gl_flags if pattern contained a globbing character. | |
70 | * GLOB_NOMAGIC: | |
71 | * Same as GLOB_NOCHECK, but it will only append pattern if it did | |
72 | * not contain any magic characters. [Used in csh style globbing] | |
73 | * GLOB_ALTDIRFUNC: | |
74 | * Use alternately specified directory access functions. | |
75 | * GLOB_TILDE: | |
76 | * expand ~user/foo to the /home/dir/of/user/foo | |
77 | * GLOB_BRACE: | |
78 | * expand {1,2}{a,b} to 1a 1b 2a 2b | |
79 | * gl_matchc: | |
80 | * Number of matches in the current invocation of glob. | |
81 | */ | |
82 | ||
83 | ||
84 | #define DOLLAR '$' | |
85 | #define DOT '.' | |
86 | #define EOS '\0' | |
87 | #define LBRACKET '[' | |
88 | #define NOT '!' | |
89 | #define QUESTION '?' | |
90 | #define QUOTE '\\' | |
91 | #define RANGE '-' | |
92 | #define RBRACKET ']' | |
93 | #define SEP '/' | |
94 | #define STAR '*' | |
375c1dee | 95 | #undef TILDE /* Some platforms may already define it */ |
84ceda19 | 96 | #define TILDE '~' |
97 | #define UNDERSCORE '_' | |
98 | #define LBRACE '{' | |
99 | #define RBRACE '}' | |
100 | #define SLASH '/' | |
101 | #define COMMA ',' | |
102 | ||
103 | #ifndef DEBUG | |
104 | ||
105 | #define M_QUOTE 0x8000 | |
106 | #define M_PROTECT 0x4000 | |
107 | #define M_MASK 0xffff | |
108 | #define M_ASCII 0x00ff | |
109 | ||
110 | typedef u_short Char; | |
111 | ||
112 | #else | |
113 | ||
114 | #define M_QUOTE 0x80 | |
115 | #define M_PROTECT 0x40 | |
116 | #define M_MASK 0xff | |
117 | #define M_ASCII 0x7f | |
118 | ||
119 | typedef char Char; | |
120 | ||
121 | #endif | |
122 | ||
123 | ||
124 | #define CHAR(c) ((Char)((c)&M_ASCII)) | |
125 | #define META(c) ((Char)((c)|M_QUOTE)) | |
126 | #define M_ALL META('*') | |
127 | #define M_END META(']') | |
128 | #define M_NOT META('!') | |
129 | #define M_ONE META('?') | |
130 | #define M_RNG META('-') | |
131 | #define M_SET META('[') | |
132 | #define ismeta(c) (((c)&M_QUOTE) != 0) | |
133 | ||
134 | ||
ac8802eb | 135 | static int compare(const void *, const void *); |
136 | static int g_Ctoc(const Char *, char *, u_int); | |
137 | static int g_lstat(Char *, struct stat *, glob_t *); | |
138 | static DIR *g_opendir(Char *, glob_t *); | |
139 | static Char *g_strchr(Char *, int); | |
140 | static int g_stat(Char *, struct stat *, glob_t *); | |
141 | static int glob0(const Char *, glob_t *); | |
142 | static int glob1(Char *, Char *, glob_t *, size_t *); | |
143 | static int glob2(Char *, Char *, Char *, Char *, Char *, Char *, | |
144 | glob_t *, size_t *); | |
145 | static int glob3(Char *, Char *, Char *, Char *, Char *, Char *, | |
146 | Char *, Char *, glob_t *, size_t *); | |
147 | static int globextend(const Char *, glob_t *, size_t *); | |
8d539493 | 148 | static const Char * |
ac8802eb | 149 | globtilde(const Char *, Char *, size_t, glob_t *); |
150 | static int globexp1(const Char *, glob_t *); | |
151 | static int globexp2(const Char *, const Char *, glob_t *, int *); | |
152 | static int match(Char *, Char *, Char *); | |
84ceda19 | 153 | #ifdef DEBUG |
ac8802eb | 154 | static void qprintf(const char *, Char *); |
84ceda19 | 155 | #endif |
156 | ||
157 | int | |
a65ea33b | 158 | glob(const char *pattern, int flags, int (*errfunc)(const char *, int), |
159 | glob_t *pglob) | |
84ceda19 | 160 | { |
161 | const u_char *patnext; | |
162 | int c; | |
b7a2a476 | 163 | Char *bufnext, *bufend, patbuf[MAXPATHLEN]; |
84ceda19 | 164 | |
165 | patnext = (u_char *) pattern; | |
166 | if (!(flags & GLOB_APPEND)) { | |
167 | pglob->gl_pathc = 0; | |
168 | pglob->gl_pathv = NULL; | |
169 | if (!(flags & GLOB_DOOFFS)) | |
170 | pglob->gl_offs = 0; | |
171 | } | |
172 | pglob->gl_flags = flags & ~GLOB_MAGCHAR; | |
173 | pglob->gl_errfunc = errfunc; | |
174 | pglob->gl_matchc = 0; | |
175 | ||
176 | bufnext = patbuf; | |
b7a2a476 | 177 | bufend = bufnext + MAXPATHLEN - 1; |
84ceda19 | 178 | if (flags & GLOB_NOESCAPE) |
8d0cc79b | 179 | while (bufnext < bufend && (c = *patnext++) != EOS) |
180 | *bufnext++ = c; | |
84ceda19 | 181 | else { |
182 | /* Protect the quoted characters. */ | |
183 | while (bufnext < bufend && (c = *patnext++) != EOS) | |
184 | if (c == QUOTE) { | |
185 | if ((c = *patnext++) == EOS) { | |
186 | c = QUOTE; | |
187 | --patnext; | |
188 | } | |
189 | *bufnext++ = c | M_PROTECT; | |
e400a640 | 190 | } else |
84ceda19 | 191 | *bufnext++ = c; |
192 | } | |
193 | *bufnext = EOS; | |
194 | ||
195 | if (flags & GLOB_BRACE) | |
196 | return globexp1(patbuf, pglob); | |
197 | else | |
198 | return glob0(patbuf, pglob); | |
199 | } | |
200 | ||
201 | /* | |
202 | * Expand recursively a glob {} pattern. When there is no more expansion | |
203 | * invoke the standard globbing routine to glob the rest of the magic | |
204 | * characters | |
205 | */ | |
b7a2a476 | 206 | static int |
a65ea33b | 207 | globexp1(const Char *pattern, glob_t *pglob) |
84ceda19 | 208 | { |
209 | const Char* ptr = pattern; | |
210 | int rv; | |
211 | ||
212 | /* Protect a single {}, for find(1), like csh */ | |
213 | if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS) | |
214 | return glob0(pattern, pglob); | |
215 | ||
216 | while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL) | |
217 | if (!globexp2(ptr, pattern, pglob, &rv)) | |
218 | return rv; | |
219 | ||
220 | return glob0(pattern, pglob); | |
221 | } | |
222 | ||
223 | ||
224 | /* | |
225 | * Recursive brace globbing helper. Tries to expand a single brace. | |
226 | * If it succeeds then it invokes globexp1 with the new pattern. | |
227 | * If it fails then it tries to glob the rest of the pattern and returns. | |
228 | */ | |
b7a2a476 | 229 | static int |
a65ea33b | 230 | globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv) |
84ceda19 | 231 | { |
232 | int i; | |
233 | Char *lm, *ls; | |
234 | const Char *pe, *pm, *pl; | |
b7a2a476 | 235 | Char patbuf[MAXPATHLEN]; |
84ceda19 | 236 | |
237 | /* copy part up to the brace */ | |
238 | for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++) | |
e400a640 | 239 | ; |
b7a2a476 | 240 | *lm = EOS; |
84ceda19 | 241 | ls = lm; |
242 | ||
243 | /* Find the balanced brace */ | |
244 | for (i = 0, pe = ++ptr; *pe; pe++) | |
245 | if (*pe == LBRACKET) { | |
246 | /* Ignore everything between [] */ | |
247 | for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++) | |
e400a640 | 248 | ; |
84ceda19 | 249 | if (*pe == EOS) { |
250 | /* | |
251 | * We could not find a matching RBRACKET. | |
252 | * Ignore and just look for RBRACE | |
253 | */ | |
254 | pe = pm; | |
255 | } | |
e400a640 | 256 | } else if (*pe == LBRACE) |
84ceda19 | 257 | i++; |
258 | else if (*pe == RBRACE) { | |
259 | if (i == 0) | |
260 | break; | |
261 | i--; | |
262 | } | |
263 | ||
264 | /* Non matching braces; just glob the pattern */ | |
265 | if (i != 0 || *pe == EOS) { | |
266 | *rv = glob0(patbuf, pglob); | |
267 | return 0; | |
268 | } | |
269 | ||
8d0cc79b | 270 | for (i = 0, pl = pm = ptr; pm <= pe; pm++) { |
84ceda19 | 271 | switch (*pm) { |
272 | case LBRACKET: | |
273 | /* Ignore everything between [] */ | |
274 | for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++) | |
e400a640 | 275 | ; |
84ceda19 | 276 | if (*pm == EOS) { |
277 | /* | |
278 | * We could not find a matching RBRACKET. | |
279 | * Ignore and just look for RBRACE | |
280 | */ | |
281 | pm = pl; | |
282 | } | |
283 | break; | |
284 | ||
285 | case LBRACE: | |
286 | i++; | |
287 | break; | |
288 | ||
289 | case RBRACE: | |
290 | if (i) { | |
8d0cc79b | 291 | i--; |
292 | break; | |
84ceda19 | 293 | } |
294 | /* FALLTHROUGH */ | |
295 | case COMMA: | |
296 | if (i && *pm == COMMA) | |
297 | break; | |
298 | else { | |
299 | /* Append the current string */ | |
300 | for (lm = ls; (pl < pm); *lm++ = *pl++) | |
e400a640 | 301 | ; |
302 | ||
84ceda19 | 303 | /* |
304 | * Append the rest of the pattern after the | |
305 | * closing brace | |
306 | */ | |
e400a640 | 307 | for (pl = pe + 1; (*lm++ = *pl++) != EOS; ) |
308 | ; | |
84ceda19 | 309 | |
310 | /* Expand the current pattern */ | |
311 | #ifdef DEBUG | |
312 | qprintf("globexp2:", patbuf); | |
313 | #endif | |
314 | *rv = globexp1(patbuf, pglob); | |
315 | ||
316 | /* move after the comma, to the next string */ | |
317 | pl = pm + 1; | |
318 | } | |
319 | break; | |
320 | ||
321 | default: | |
322 | break; | |
323 | } | |
8d0cc79b | 324 | } |
84ceda19 | 325 | *rv = 0; |
326 | return 0; | |
327 | } | |
328 | ||
329 | ||
330 | ||
331 | /* | |
332 | * expand tilde from the passwd file. | |
333 | */ | |
334 | static const Char * | |
a65ea33b | 335 | globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob) |
84ceda19 | 336 | { |
337 | struct passwd *pwd; | |
338 | char *h; | |
339 | const Char *p; | |
340 | Char *b, *eb; | |
341 | ||
342 | if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE)) | |
343 | return pattern; | |
344 | ||
345 | /* Copy up to the end of the string or / */ | |
346 | eb = &patbuf[patbuf_len - 1]; | |
347 | for (p = pattern + 1, h = (char *) patbuf; | |
348 | h < (char *)eb && *p && *p != SLASH; *h++ = *p++) | |
e400a640 | 349 | ; |
84ceda19 | 350 | |
351 | *h = EOS; | |
352 | ||
e400a640 | 353 | #if 0 |
354 | if (h == (char *)eb) | |
355 | return what; | |
356 | #endif | |
357 | ||
84ceda19 | 358 | if (((char *) patbuf)[0] == EOS) { |
359 | /* | |
360 | * handle a plain ~ or ~/ by expanding $HOME | |
361 | * first and then trying the password file | |
362 | */ | |
363 | #if 0 | |
364 | if (issetugid() != 0 || (h = getenv("HOME")) == NULL) { | |
365 | #endif | |
366 | if ((getuid() != geteuid()) || (h = getenv("HOME")) == NULL) { | |
367 | if ((pwd = getpwuid(getuid())) == NULL) | |
368 | return pattern; | |
369 | else | |
370 | h = pwd->pw_dir; | |
371 | } | |
e400a640 | 372 | } else { |
84ceda19 | 373 | /* |
374 | * Expand a ~user | |
375 | */ | |
376 | if ((pwd = getpwnam((char*) patbuf)) == NULL) | |
377 | return pattern; | |
378 | else | |
379 | h = pwd->pw_dir; | |
380 | } | |
381 | ||
382 | /* Copy the home directory */ | |
383 | for (b = patbuf; b < eb && *h; *b++ = *h++) | |
e400a640 | 384 | ; |
84ceda19 | 385 | |
386 | /* Append the rest of the pattern */ | |
387 | while (b < eb && (*b++ = *p++) != EOS) | |
e400a640 | 388 | ; |
84ceda19 | 389 | *b = EOS; |
390 | ||
391 | return patbuf; | |
392 | } | |
393 | ||
394 | ||
395 | /* | |
396 | * The main glob() routine: compiles the pattern (optionally processing | |
397 | * quotes), calls glob1() to do the real pattern matching, and finally | |
398 | * sorts the list (unless unsorted operation is requested). Returns 0 | |
399 | * if things went well, nonzero if errors occurred. It is not an error | |
400 | * to find no matches. | |
401 | */ | |
402 | static int | |
a65ea33b | 403 | glob0(const Char *pattern, glob_t *pglob) |
84ceda19 | 404 | { |
405 | const Char *qpatnext; | |
406 | int c, err, oldpathc; | |
b7a2a476 | 407 | Char *bufnext, patbuf[MAXPATHLEN]; |
8d539493 | 408 | size_t limit = 0; |
84ceda19 | 409 | |
b7a2a476 | 410 | qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob); |
84ceda19 | 411 | oldpathc = pglob->gl_pathc; |
412 | bufnext = patbuf; | |
413 | ||
414 | /* We don't need to check for buffer overflow any more. */ | |
415 | while ((c = *qpatnext++) != EOS) { | |
416 | switch (c) { | |
417 | case LBRACKET: | |
418 | c = *qpatnext; | |
419 | if (c == NOT) | |
420 | ++qpatnext; | |
421 | if (*qpatnext == EOS || | |
422 | g_strchr((Char *) qpatnext+1, RBRACKET) == NULL) { | |
423 | *bufnext++ = LBRACKET; | |
424 | if (c == NOT) | |
425 | --qpatnext; | |
426 | break; | |
427 | } | |
428 | *bufnext++ = M_SET; | |
429 | if (c == NOT) | |
430 | *bufnext++ = M_NOT; | |
431 | c = *qpatnext++; | |
432 | do { | |
433 | *bufnext++ = CHAR(c); | |
434 | if (*qpatnext == RANGE && | |
435 | (c = qpatnext[1]) != RBRACKET) { | |
436 | *bufnext++ = M_RNG; | |
437 | *bufnext++ = CHAR(c); | |
438 | qpatnext += 2; | |
439 | } | |
440 | } while ((c = *qpatnext++) != RBRACKET); | |
441 | pglob->gl_flags |= GLOB_MAGCHAR; | |
442 | *bufnext++ = M_END; | |
443 | break; | |
444 | case QUESTION: | |
445 | pglob->gl_flags |= GLOB_MAGCHAR; | |
446 | *bufnext++ = M_ONE; | |
447 | break; | |
448 | case STAR: | |
449 | pglob->gl_flags |= GLOB_MAGCHAR; | |
450 | /* collapse adjacent stars to one, | |
451 | * to avoid exponential behavior | |
452 | */ | |
453 | if (bufnext == patbuf || bufnext[-1] != M_ALL) | |
8d0cc79b | 454 | *bufnext++ = M_ALL; |
84ceda19 | 455 | break; |
456 | default: | |
457 | *bufnext++ = CHAR(c); | |
458 | break; | |
459 | } | |
460 | } | |
461 | *bufnext = EOS; | |
462 | #ifdef DEBUG | |
463 | qprintf("glob0:", patbuf); | |
464 | #endif | |
465 | ||
b7a2a476 | 466 | if ((err = glob1(patbuf, patbuf+MAXPATHLEN-1, pglob, &limit)) != 0) |
84ceda19 | 467 | return(err); |
468 | ||
469 | /* | |
470 | * If there was no match we are going to append the pattern | |
471 | * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified | |
472 | * and the pattern did not contain any magic characters | |
473 | * GLOB_NOMAGIC is there just for compatibility with csh. | |
474 | */ | |
475 | if (pglob->gl_pathc == oldpathc) { | |
476 | if ((pglob->gl_flags & GLOB_NOCHECK) || | |
477 | ((pglob->gl_flags & GLOB_NOMAGIC) && | |
478 | !(pglob->gl_flags & GLOB_MAGCHAR))) | |
8d539493 | 479 | return(globextend(pattern, pglob, &limit)); |
84ceda19 | 480 | else |
481 | return(GLOB_NOMATCH); | |
482 | } | |
483 | if (!(pglob->gl_flags & GLOB_NOSORT)) | |
484 | qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc, | |
485 | pglob->gl_pathc - oldpathc, sizeof(char *), compare); | |
486 | return(0); | |
487 | } | |
488 | ||
489 | static int | |
a65ea33b | 490 | compare(const void *p, const void *q) |
84ceda19 | 491 | { |
492 | return(strcmp(*(char **)p, *(char **)q)); | |
493 | } | |
494 | ||
495 | static int | |
a65ea33b | 496 | glob1(Char *pattern, Char *pattern_last, glob_t *pglob, size_t *limitp) |
84ceda19 | 497 | { |
b7a2a476 | 498 | Char pathbuf[MAXPATHLEN]; |
84ceda19 | 499 | |
500 | /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */ | |
501 | if (*pattern == EOS) | |
502 | return(0); | |
b7a2a476 | 503 | return(glob2(pathbuf, pathbuf+MAXPATHLEN-1, |
504 | pathbuf, pathbuf+MAXPATHLEN-1, | |
505 | pattern, pattern_last, pglob, limitp)); | |
84ceda19 | 506 | } |
507 | ||
508 | /* | |
509 | * The functions glob2 and glob3 are mutually recursive; there is one level | |
510 | * of recursion for each segment in the pattern that contains one or more | |
511 | * meta characters. | |
512 | */ | |
513 | static int | |
a65ea33b | 514 | glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last, |
515 | Char *pattern, Char *pattern_last, glob_t *pglob, size_t *limitp) | |
84ceda19 | 516 | { |
517 | struct stat sb; | |
518 | Char *p, *q; | |
519 | int anymeta; | |
520 | ||
521 | /* | |
522 | * Loop over pattern segments until end of pattern or until | |
523 | * segment with meta character found. | |
524 | */ | |
525 | for (anymeta = 0;;) { | |
526 | if (*pattern == EOS) { /* End of pattern? */ | |
527 | *pathend = EOS; | |
528 | if (g_lstat(pathbuf, &sb, pglob)) | |
529 | return(0); | |
530 | ||
531 | if (((pglob->gl_flags & GLOB_MARK) && | |
8d0cc79b | 532 | pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) || |
533 | (S_ISLNK(sb.st_mode) && | |
84ceda19 | 534 | (g_stat(pathbuf, &sb, pglob) == 0) && |
535 | S_ISDIR(sb.st_mode)))) { | |
b7a2a476 | 536 | if (pathend+1 > pathend_last) |
537 | return (1); | |
84ceda19 | 538 | *pathend++ = SEP; |
539 | *pathend = EOS; | |
540 | } | |
541 | ++pglob->gl_matchc; | |
8d539493 | 542 | return(globextend(pathbuf, pglob, limitp)); |
84ceda19 | 543 | } |
544 | ||
545 | /* Find end of next segment, copy tentatively to pathend. */ | |
546 | q = pathend; | |
547 | p = pattern; | |
548 | while (*p != EOS && *p != SEP) { | |
549 | if (ismeta(*p)) | |
550 | anymeta = 1; | |
b7a2a476 | 551 | if (q+1 > pathend_last) |
552 | return (1); | |
84ceda19 | 553 | *q++ = *p++; |
554 | } | |
555 | ||
556 | if (!anymeta) { /* No expansion, do next segment. */ | |
557 | pathend = q; | |
558 | pattern = p; | |
b7a2a476 | 559 | while (*pattern == SEP) { |
560 | if (pathend+1 > pathend_last) | |
561 | return (1); | |
84ceda19 | 562 | *pathend++ = *pattern++; |
b7a2a476 | 563 | } |
8d0cc79b | 564 | } else |
565 | /* Need expansion, recurse. */ | |
b7a2a476 | 566 | return(glob3(pathbuf, pathbuf_last, pathend, |
567 | pathend_last, pattern, pattern_last, | |
568 | p, pattern_last, pglob, limitp)); | |
84ceda19 | 569 | } |
570 | /* NOTREACHED */ | |
571 | } | |
572 | ||
573 | static int | |
a65ea33b | 574 | glob3(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last, |
575 | Char *pattern, Char *pattern_last, Char *restpattern, | |
576 | Char *restpattern_last, glob_t *pglob, size_t *limitp) | |
84ceda19 | 577 | { |
a65ea33b | 578 | struct dirent *dp; |
84ceda19 | 579 | DIR *dirp; |
580 | int err; | |
581 | char buf[MAXPATHLEN]; | |
582 | ||
583 | /* | |
584 | * The readdirfunc declaration can't be prototyped, because it is | |
585 | * assigned, below, to two functions which are prototyped in glob.h | |
586 | * and dirent.h as taking pointers to differently typed opaque | |
587 | * structures. | |
588 | */ | |
510a42ce | 589 | struct dirent *(*readdirfunc)(void *); |
84ceda19 | 590 | |
b7a2a476 | 591 | if (pathend > pathend_last) |
592 | return (1); | |
84ceda19 | 593 | *pathend = EOS; |
594 | errno = 0; | |
595 | ||
596 | if ((dirp = g_opendir(pathbuf, pglob)) == NULL) { | |
597 | /* TODO: don't call for ENOENT or ENOTDIR? */ | |
598 | if (pglob->gl_errfunc) { | |
e400a640 | 599 | if (g_Ctoc(pathbuf, buf, sizeof(buf))) |
8d0cc79b | 600 | return(GLOB_ABORTED); |
84ceda19 | 601 | if (pglob->gl_errfunc(buf, errno) || |
602 | pglob->gl_flags & GLOB_ERR) | |
8d0cc79b | 603 | return(GLOB_ABORTED); |
84ceda19 | 604 | } |
605 | return(0); | |
606 | } | |
607 | ||
608 | err = 0; | |
609 | ||
610 | /* Search directory for matching names. */ | |
611 | if (pglob->gl_flags & GLOB_ALTDIRFUNC) | |
612 | readdirfunc = pglob->gl_readdir; | |
613 | else | |
510a42ce | 614 | readdirfunc = (struct dirent *(*)(void *))readdir; |
84ceda19 | 615 | while ((dp = (*readdirfunc)(dirp))) { |
a65ea33b | 616 | u_char *sc; |
617 | Char *dc; | |
84ceda19 | 618 | |
619 | /* Initial DOT must be matched literally. */ | |
620 | if (dp->d_name[0] == DOT && *pattern != DOT) | |
621 | continue; | |
b7a2a476 | 622 | dc = pathend; |
623 | sc = (u_char *) dp->d_name; | |
624 | while (dc < pathend_last && (*dc++ = *sc++) != EOS) | |
625 | ; | |
626 | if (dc >= pathend_last) { | |
627 | *dc = EOS; | |
628 | err = 1; | |
629 | break; | |
630 | } | |
631 | ||
84ceda19 | 632 | if (!match(pathend, pattern, restpattern)) { |
633 | *pathend = EOS; | |
634 | continue; | |
635 | } | |
b7a2a476 | 636 | err = glob2(pathbuf, pathbuf_last, --dc, pathend_last, |
637 | restpattern, restpattern_last, pglob, limitp); | |
84ceda19 | 638 | if (err) |
639 | break; | |
640 | } | |
641 | ||
642 | if (pglob->gl_flags & GLOB_ALTDIRFUNC) | |
643 | (*pglob->gl_closedir)(dirp); | |
644 | else | |
645 | closedir(dirp); | |
646 | return(err); | |
647 | } | |
648 | ||
649 | ||
650 | /* | |
ac8802eb | 651 | * Extend the gl_pathv member of a glob_t structure to accommodate a new item, |
84ceda19 | 652 | * add the new item, and update gl_pathc. |
653 | * | |
654 | * This assumes the BSD realloc, which only copies the block when its size | |
655 | * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic | |
656 | * behavior. | |
657 | * | |
658 | * Return 0 if new item added, error code if memory couldn't be allocated. | |
659 | * | |
660 | * Invariant of the glob_t structure: | |
661 | * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and | |
662 | * gl_pathv points to (gl_offs + gl_pathc + 1) items. | |
663 | */ | |
664 | static int | |
a65ea33b | 665 | globextend(const Char *path, glob_t *pglob, size_t *limitp) |
84ceda19 | 666 | { |
a65ea33b | 667 | char **pathv; |
668 | int i; | |
8d539493 | 669 | u_int newsize, len; |
84ceda19 | 670 | char *copy; |
671 | const Char *p; | |
672 | ||
673 | newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs); | |
8d539493 | 674 | pathv = pglob->gl_pathv ? realloc((char *)pglob->gl_pathv, newsize) : |
675 | malloc(newsize); | |
84ceda19 | 676 | if (pathv == NULL) { |
01022caf | 677 | if (pglob->gl_pathv) { |
84ceda19 | 678 | free(pglob->gl_pathv); |
01022caf | 679 | pglob->gl_pathv = NULL; |
680 | } | |
84ceda19 | 681 | return(GLOB_NOSPACE); |
682 | } | |
683 | ||
684 | if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) { | |
685 | /* first time around -- clear initial gl_offs items */ | |
686 | pathv += pglob->gl_offs; | |
687 | for (i = pglob->gl_offs; --i >= 0; ) | |
688 | *--pathv = NULL; | |
689 | } | |
690 | pglob->gl_pathv = pathv; | |
691 | ||
692 | for (p = path; *p++;) | |
e400a640 | 693 | ; |
8d539493 | 694 | len = (size_t)(p - path); |
695 | *limitp += len; | |
696 | if ((copy = malloc(len)) != NULL) { | |
e400a640 | 697 | if (g_Ctoc(path, copy, len)) { |
8d0cc79b | 698 | free(copy); |
699 | return(GLOB_NOSPACE); | |
700 | } | |
84ceda19 | 701 | pathv[pglob->gl_offs + pglob->gl_pathc++] = copy; |
702 | } | |
703 | pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; | |
8d539493 | 704 | |
705 | if ((pglob->gl_flags & GLOB_LIMIT) && | |
9887f269 | 706 | newsize + *limitp >= (u_int) get_arg_max()) { |
8d539493 | 707 | errno = 0; |
708 | return(GLOB_NOSPACE); | |
709 | } | |
710 | ||
84ceda19 | 711 | return(copy == NULL ? GLOB_NOSPACE : 0); |
712 | } | |
713 | ||
714 | ||
715 | /* | |
716 | * pattern matching function for filenames. Each occurrence of the * | |
717 | * pattern causes a recursion level. | |
718 | */ | |
719 | static int | |
a65ea33b | 720 | match(Char *name, Char *pat, Char *patend) |
84ceda19 | 721 | { |
722 | int ok, negate_range; | |
723 | Char c, k; | |
724 | ||
725 | while (pat < patend) { | |
726 | c = *pat++; | |
727 | switch (c & M_MASK) { | |
728 | case M_ALL: | |
729 | if (pat == patend) | |
730 | return(1); | |
a65ea33b | 731 | do { |
84ceda19 | 732 | if (match(name, pat, patend)) |
733 | return(1); | |
a65ea33b | 734 | } while (*name++ != EOS); |
84ceda19 | 735 | return(0); |
736 | case M_ONE: | |
737 | if (*name++ == EOS) | |
738 | return(0); | |
739 | break; | |
740 | case M_SET: | |
741 | ok = 0; | |
742 | if ((k = *name++) == EOS) | |
743 | return(0); | |
744 | if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS) | |
745 | ++pat; | |
746 | while (((c = *pat++) & M_MASK) != M_END) | |
747 | if ((*pat & M_MASK) == M_RNG) { | |
748 | if (c <= k && k <= pat[1]) | |
749 | ok = 1; | |
750 | pat += 2; | |
751 | } else if (c == k) | |
752 | ok = 1; | |
753 | if (ok == negate_range) | |
754 | return(0); | |
755 | break; | |
756 | default: | |
757 | if (*name++ != c) | |
758 | return(0); | |
759 | break; | |
760 | } | |
761 | } | |
762 | return(*name == EOS); | |
763 | } | |
764 | ||
765 | /* Free allocated data belonging to a glob_t structure. */ | |
766 | void | |
a65ea33b | 767 | globfree(glob_t *pglob) |
84ceda19 | 768 | { |
a65ea33b | 769 | int i; |
770 | char **pp; | |
84ceda19 | 771 | |
772 | if (pglob->gl_pathv != NULL) { | |
773 | pp = pglob->gl_pathv + pglob->gl_offs; | |
774 | for (i = pglob->gl_pathc; i--; ++pp) | |
775 | if (*pp) | |
776 | free(*pp); | |
777 | free(pglob->gl_pathv); | |
01022caf | 778 | pglob->gl_pathv = NULL; |
84ceda19 | 779 | } |
780 | } | |
781 | ||
782 | static DIR * | |
a65ea33b | 783 | g_opendir(Char *str, glob_t *pglob) |
84ceda19 | 784 | { |
785 | char buf[MAXPATHLEN]; | |
786 | ||
787 | if (!*str) | |
ac8802eb | 788 | strlcpy(buf, ".", sizeof buf); |
8d0cc79b | 789 | else { |
e400a640 | 790 | if (g_Ctoc(str, buf, sizeof(buf))) |
8d0cc79b | 791 | return(NULL); |
792 | } | |
84ceda19 | 793 | |
794 | if (pglob->gl_flags & GLOB_ALTDIRFUNC) | |
795 | return((*pglob->gl_opendir)(buf)); | |
796 | ||
797 | return(opendir(buf)); | |
798 | } | |
799 | ||
800 | static int | |
a65ea33b | 801 | g_lstat(Char *fn, struct stat *sb, glob_t *pglob) |
84ceda19 | 802 | { |
803 | char buf[MAXPATHLEN]; | |
804 | ||
e400a640 | 805 | if (g_Ctoc(fn, buf, sizeof(buf))) |
8d0cc79b | 806 | return(-1); |
84ceda19 | 807 | if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
808 | return((*pglob->gl_lstat)(buf, sb)); | |
809 | return(lstat(buf, sb)); | |
810 | } | |
811 | ||
812 | static int | |
a65ea33b | 813 | g_stat(Char *fn, struct stat *sb, glob_t *pglob) |
84ceda19 | 814 | { |
815 | char buf[MAXPATHLEN]; | |
816 | ||
e400a640 | 817 | if (g_Ctoc(fn, buf, sizeof(buf))) |
8d0cc79b | 818 | return(-1); |
84ceda19 | 819 | if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
820 | return((*pglob->gl_stat)(buf, sb)); | |
821 | return(stat(buf, sb)); | |
822 | } | |
823 | ||
824 | static Char * | |
a65ea33b | 825 | g_strchr(Char *str, int ch) |
84ceda19 | 826 | { |
827 | do { | |
828 | if (*str == ch) | |
829 | return (str); | |
830 | } while (*str++); | |
831 | return (NULL); | |
832 | } | |
833 | ||
8d0cc79b | 834 | static int |
a65ea33b | 835 | g_Ctoc(const Char *str, char *buf, u_int len) |
84ceda19 | 836 | { |
84ceda19 | 837 | |
e400a640 | 838 | while (len--) { |
839 | if ((*buf++ = *str++) == EOS) | |
840 | return (0); | |
841 | } | |
842 | return (1); | |
84ceda19 | 843 | } |
844 | ||
845 | #ifdef DEBUG | |
846 | static void | |
a65ea33b | 847 | qprintf(const char *str, Char *s) |
84ceda19 | 848 | { |
a65ea33b | 849 | Char *p; |
84ceda19 | 850 | |
851 | (void)printf("%s:\n", str); | |
852 | for (p = s; *p; p++) | |
853 | (void)printf("%c", CHAR(*p)); | |
854 | (void)printf("\n"); | |
855 | for (p = s; *p; p++) | |
856 | (void)printf("%c", *p & M_PROTECT ? '"' : ' '); | |
857 | (void)printf("\n"); | |
858 | for (p = s; *p; p++) | |
859 | (void)printf("%c", ismeta(*p) ? '_' : ' '); | |
860 | (void)printf("\n"); | |
861 | } | |
862 | #endif | |
863 | ||
40849fdb | 864 | #endif /* !defined(HAVE_GLOB) || !defined(GLOB_HAS_ALTDIRFUNC) || |
865 | !defined(GLOB_HAS_GL_MATCHC) */ | |
84ceda19 | 866 |