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