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