Commit | Line | Data |
---|---|---|
72b16652 GS |
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 GS |
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 GS |
33 | #if defined(LIBC_SCCS) && !defined(lint) |
34 | static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93"; | |
bac331f5 JH |
35 | /* most changes between the version above and the one below have been ported: |
36 | static char sscsid[]= "$OpenBSD: glob.c,v 1.8.10.1 2001/04/10 jason Exp $"; | |
37 | */ | |
72b16652 GS |
38 | #endif /* LIBC_SCCS and not lint */ |
39 | ||
40 | /* | |
41 | * glob(3) -- a superset of the one defined in POSIX 1003.2. | |
42 | * | |
43 | * The [!...] convention to negate a range is supported (SysV, Posix, ksh). | |
44 | * | |
45 | * Optional extra services, controlled by flags not defined by POSIX: | |
46 | * | |
47 | * GLOB_QUOTE: | |
48 | * Escaping convention: \ inhibits any special meaning the following | |
49 | * character might have (except \ at end of string is retained). | |
50 | * GLOB_MAGCHAR: | |
51 | * Set in gl_flags if pattern contained a globbing character. | |
52 | * GLOB_NOMAGIC: | |
53 | * Same as GLOB_NOCHECK, but it will only append pattern if it did | |
54 | * not contain any magic characters. [Used in csh style globbing] | |
55 | * GLOB_ALTDIRFUNC: | |
56 | * Use alternately specified directory access functions. | |
57 | * GLOB_TILDE: | |
58 | * expand ~user/foo to the /home/dir/of/user/foo | |
59 | * GLOB_BRACE: | |
60 | * expand {1,2}{a,b} to 1a 1b 2a 2b | |
61 | * gl_matchc: | |
62 | * Number of matches in the current invocation of glob. | |
2d5e9e5d JH |
63 | * GLOB_ALPHASORT: |
64 | * sort alphabetically like csh (case doesn't matter) instead of in ASCII | |
65 | * order | |
72b16652 GS |
66 | */ |
67 | ||
68 | #include <EXTERN.h> | |
69 | #include <perl.h> | |
4f49e16e GS |
70 | #include <XSUB.h> |
71 | ||
72b16652 GS |
72 | #include "bsd_glob.h" |
73 | #ifdef I_PWD | |
74 | # include <pwd.h> | |
75 | #else | |
76 | #ifdef HAS_PASSWD | |
77 | struct passwd *getpwnam(char *); | |
78 | struct passwd *getpwuid(Uid_t); | |
79 | #endif | |
80 | #endif | |
81 | ||
82 | #ifndef MAXPATHLEN | |
83 | # ifdef PATH_MAX | |
84 | # define MAXPATHLEN PATH_MAX | |
7369a524 CN |
85 | # ifdef MACOS_TRADITIONAL |
86 | # define MAXPATHLEN 255 | |
87 | # else | |
88 | # define MAXPATHLEN 1024 | |
89 | # endif | |
72b16652 GS |
90 | # endif |
91 | #endif | |
92 | ||
b8ef571c JH |
93 | #ifdef I_LIMITS |
94 | #include <limits.h> | |
95 | #endif | |
96 | ||
97 | #ifndef ARG_MAX | |
6eb630b7 CN |
98 | # ifdef MACOS_TRADITIONAL |
99 | # define ARG_MAX 65536 /* Mac OS is actually unlimited */ | |
b8ef571c | 100 | # else |
6eb630b7 CN |
101 | # ifdef _SC_ARG_MAX |
102 | # define ARG_MAX (sysconf(_SC_ARG_MAX)) | |
b8ef571c | 103 | # else |
6eb630b7 CN |
104 | # ifdef _POSIX_ARG_MAX |
105 | # define ARG_MAX _POSIX_ARG_MAX | |
b8ef571c | 106 | # else |
6eb630b7 CN |
107 | # ifdef WIN32 |
108 | # define ARG_MAX 14500 /* from VC's limits.h */ | |
109 | # else | |
110 | # define ARG_MAX 4096 /* from POSIX, be conservative */ | |
111 | # endif | |
b8ef571c JH |
112 | # endif |
113 | # endif | |
114 | # endif | |
115 | #endif | |
116 | ||
3e5d0dec GS |
117 | #define BG_DOLLAR '$' |
118 | #define BG_DOT '.' | |
119 | #define BG_EOS '\0' | |
120 | #define BG_LBRACKET '[' | |
121 | #define BG_NOT '!' | |
122 | #define BG_QUESTION '?' | |
123 | #define BG_QUOTE '\\' | |
124 | #define BG_RANGE '-' | |
125 | #define BG_RBRACKET ']' | |
7369a524 CN |
126 | #ifdef MACOS_TRADITIONAL |
127 | # define BG_SEP ':' | |
128 | #else | |
129 | # define BG_SEP '/' | |
130 | #endif | |
220398a0 PM |
131 | #ifdef DOSISH |
132 | #define BG_SEP2 '\\' | |
133 | #endif | |
3e5d0dec GS |
134 | #define BG_STAR '*' |
135 | #define BG_TILDE '~' | |
136 | #define BG_UNDERSCORE '_' | |
137 | #define BG_LBRACE '{' | |
138 | #define BG_RBRACE '}' | |
139 | #define BG_SLASH '/' | |
140 | #define BG_COMMA ',' | |
72b16652 GS |
141 | |
142 | #ifndef GLOB_DEBUG | |
143 | ||
144 | #define M_QUOTE 0x8000 | |
145 | #define M_PROTECT 0x4000 | |
146 | #define M_MASK 0xffff | |
147 | #define M_ASCII 0x00ff | |
148 | ||
149 | typedef U16 Char; | |
150 | ||
151 | #else | |
152 | ||
153 | #define M_QUOTE 0x80 | |
154 | #define M_PROTECT 0x40 | |
155 | #define M_MASK 0xff | |
156 | #define M_ASCII 0x7f | |
157 | ||
158 | typedef U8 Char; | |
159 | ||
160 | #endif /* !GLOB_DEBUG */ | |
161 | ||
162 | ||
163 | #define CHAR(c) ((Char)((c)&M_ASCII)) | |
164 | #define META(c) ((Char)((c)|M_QUOTE)) | |
165 | #define M_ALL META('*') | |
166 | #define M_END META(']') | |
167 | #define M_NOT META('!') | |
168 | #define M_ONE META('?') | |
169 | #define M_RNG META('-') | |
170 | #define M_SET META('[') | |
171 | #define ismeta(c) (((c)&M_QUOTE) != 0) | |
172 | ||
173 | ||
174 | static int compare(const void *, const void *); | |
220398a0 | 175 | static int ci_compare(const void *, const void *); |
b8ef571c | 176 | static int g_Ctoc(const Char *, char *, STRLEN); |
72b16652 GS |
177 | static int g_lstat(Char *, Stat_t *, glob_t *); |
178 | static DIR *g_opendir(Char *, glob_t *); | |
179 | static Char *g_strchr(Char *, int); | |
72b16652 GS |
180 | static int g_stat(Char *, Stat_t *, glob_t *); |
181 | static int glob0(const Char *, glob_t *); | |
b8ef571c JH |
182 | static int glob1(Char *, Char *, glob_t *, size_t *); |
183 | static int glob2(Char *, Char *, Char *, Char *, Char *, Char *, | |
184 | glob_t *, size_t *); | |
185 | static int glob3(Char *, Char *, Char *, Char *, Char *, Char *, | |
186 | Char *, Char *, glob_t *, size_t *); | |
187 | static int globextend(const Char *, glob_t *, size_t *); | |
188 | static const Char * | |
189 | globtilde(const Char *, Char *, size_t, glob_t *); | |
72b16652 GS |
190 | static int globexp1(const Char *, glob_t *); |
191 | static int globexp2(const Char *, const Char *, glob_t *, int *); | |
220398a0 | 192 | static int match(Char *, Char *, Char *, int); |
72b16652 GS |
193 | #ifdef GLOB_DEBUG |
194 | static void qprintf(const char *, Char *); | |
195 | #endif /* GLOB_DEBUG */ | |
196 | ||
4f49e16e GS |
197 | #ifdef PERL_IMPLICIT_CONTEXT |
198 | static Direntry_t * my_readdir(DIR*); | |
199 | ||
200 | static Direntry_t * | |
201 | my_readdir(DIR *d) | |
202 | { | |
203 | return PerlDir_read(d); | |
204 | } | |
205 | #else | |
206 | #define my_readdir readdir | |
207 | #endif | |
208 | ||
72b16652 GS |
209 | int |
210 | bsd_glob(const char *pattern, int flags, | |
211 | int (*errfunc)(const char *, int), glob_t *pglob) | |
212 | { | |
213 | const U8 *patnext; | |
214 | int c; | |
b8ef571c | 215 | Char *bufnext, *bufend, patbuf[MAXPATHLEN]; |
72b16652 GS |
216 | |
217 | patnext = (U8 *) pattern; | |
218 | if (!(flags & GLOB_APPEND)) { | |
219 | pglob->gl_pathc = 0; | |
220 | pglob->gl_pathv = NULL; | |
221 | if (!(flags & GLOB_DOOFFS)) | |
222 | pglob->gl_offs = 0; | |
223 | } | |
224 | pglob->gl_flags = flags & ~GLOB_MAGCHAR; | |
225 | pglob->gl_errfunc = errfunc; | |
226 | pglob->gl_matchc = 0; | |
227 | ||
228 | bufnext = patbuf; | |
b8ef571c | 229 | bufend = bufnext + MAXPATHLEN - 1; |
220398a0 PM |
230 | #ifdef DOSISH |
231 | /* Nasty hack to treat patterns like "C:*" correctly. In this | |
232 | * case, the * should match any file in the current directory | |
233 | * on the C: drive. However, the glob code does not treat the | |
234 | * colon specially, so it looks for files beginning "C:" in | |
235 | * the current directory. To fix this, change the pattern to | |
236 | * add an explicit "./" at the start (just after the drive | |
237 | * letter and colon - ie change to "C:./*"). | |
238 | */ | |
239 | if (isalpha(pattern[0]) && pattern[1] == ':' && | |
240 | pattern[2] != BG_SEP && pattern[2] != BG_SEP2 && | |
241 | bufend - bufnext > 4) { | |
242 | *bufnext++ = pattern[0]; | |
243 | *bufnext++ = ':'; | |
244 | *bufnext++ = '.'; | |
245 | *bufnext++ = BG_SEP; | |
246 | patnext += 2; | |
247 | } | |
248 | #endif | |
72b16652 GS |
249 | if (flags & GLOB_QUOTE) { |
250 | /* Protect the quoted characters. */ | |
3e5d0dec GS |
251 | while (bufnext < bufend && (c = *patnext++) != BG_EOS) |
252 | if (c == BG_QUOTE) { | |
220398a0 PM |
253 | #ifdef DOSISH |
254 | /* To avoid backslashitis on Win32, | |
255 | * we only treat \ as a quoting character | |
256 | * if it precedes one of the | |
257 | * metacharacters []-{}~\ | |
258 | */ | |
259 | if ((c = *patnext++) != '[' && c != ']' && | |
260 | c != '-' && c != '{' && c != '}' && | |
261 | c != '~' && c != '\\') { | |
262 | #else | |
3e5d0dec | 263 | if ((c = *patnext++) == BG_EOS) { |
220398a0 | 264 | #endif |
3e5d0dec | 265 | c = BG_QUOTE; |
72b16652 GS |
266 | --patnext; |
267 | } | |
268 | *bufnext++ = c | M_PROTECT; | |
b8ef571c | 269 | } else |
72b16652 | 270 | *bufnext++ = c; |
b8ef571c JH |
271 | } else |
272 | while (bufnext < bufend && (c = *patnext++) != BG_EOS) | |
273 | *bufnext++ = c; | |
3e5d0dec | 274 | *bufnext = BG_EOS; |
72b16652 GS |
275 | |
276 | if (flags & GLOB_BRACE) | |
277 | return globexp1(patbuf, pglob); | |
278 | else | |
279 | return glob0(patbuf, pglob); | |
280 | } | |
281 | ||
282 | /* | |
283 | * Expand recursively a glob {} pattern. When there is no more expansion | |
284 | * invoke the standard globbing routine to glob the rest of the magic | |
285 | * characters | |
286 | */ | |
b8ef571c JH |
287 | static int |
288 | globexp1(const Char *pattern, glob_t *pglob) | |
72b16652 GS |
289 | { |
290 | const Char* ptr = pattern; | |
291 | int rv; | |
292 | ||
293 | /* Protect a single {}, for find(1), like csh */ | |
3e5d0dec | 294 | if (pattern[0] == BG_LBRACE && pattern[1] == BG_RBRACE && pattern[2] == BG_EOS) |
72b16652 GS |
295 | return glob0(pattern, pglob); |
296 | ||
3e5d0dec | 297 | while ((ptr = (const Char *) g_strchr((Char *) ptr, BG_LBRACE)) != NULL) |
72b16652 GS |
298 | if (!globexp2(ptr, pattern, pglob, &rv)) |
299 | return rv; | |
300 | ||
301 | return glob0(pattern, pglob); | |
302 | } | |
303 | ||
304 | ||
305 | /* | |
306 | * Recursive brace globbing helper. Tries to expand a single brace. | |
307 | * If it succeeds then it invokes globexp1 with the new pattern. | |
308 | * If it fails then it tries to glob the rest of the pattern and returns. | |
309 | */ | |
b8ef571c JH |
310 | static int |
311 | globexp2(const Char *ptr, const Char *pattern, | |
312 | glob_t *pglob, int *rv) | |
72b16652 GS |
313 | { |
314 | int i; | |
315 | Char *lm, *ls; | |
316 | const Char *pe, *pm, *pl; | |
b8ef571c | 317 | Char patbuf[MAXPATHLEN]; |
72b16652 GS |
318 | |
319 | /* copy part up to the brace */ | |
320 | for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++) | |
b8ef571c JH |
321 | ; |
322 | *lm = BG_EOS; | |
72b16652 GS |
323 | ls = lm; |
324 | ||
325 | /* Find the balanced brace */ | |
326 | for (i = 0, pe = ++ptr; *pe; pe++) | |
3e5d0dec | 327 | if (*pe == BG_LBRACKET) { |
72b16652 | 328 | /* Ignore everything between [] */ |
3e5d0dec | 329 | for (pm = pe++; *pe != BG_RBRACKET && *pe != BG_EOS; pe++) |
b8ef571c | 330 | ; |
3e5d0dec | 331 | if (*pe == BG_EOS) { |
72b16652 | 332 | /* |
3e5d0dec GS |
333 | * We could not find a matching BG_RBRACKET. |
334 | * Ignore and just look for BG_RBRACE | |
72b16652 GS |
335 | */ |
336 | pe = pm; | |
337 | } | |
b8ef571c | 338 | } else if (*pe == BG_LBRACE) |
72b16652 | 339 | i++; |
3e5d0dec | 340 | else if (*pe == BG_RBRACE) { |
72b16652 GS |
341 | if (i == 0) |
342 | break; | |
343 | i--; | |
344 | } | |
345 | ||
346 | /* Non matching braces; just glob the pattern */ | |
3e5d0dec | 347 | if (i != 0 || *pe == BG_EOS) { |
72b16652 GS |
348 | *rv = glob0(patbuf, pglob); |
349 | return 0; | |
350 | } | |
351 | ||
b8ef571c | 352 | for (i = 0, pl = pm = ptr; pm <= pe; pm++) { |
72b16652 | 353 | switch (*pm) { |
3e5d0dec | 354 | case BG_LBRACKET: |
72b16652 | 355 | /* Ignore everything between [] */ |
3e5d0dec | 356 | for (pl = pm++; *pm != BG_RBRACKET && *pm != BG_EOS; pm++) |
b8ef571c | 357 | ; |
3e5d0dec | 358 | if (*pm == BG_EOS) { |
72b16652 | 359 | /* |
3e5d0dec GS |
360 | * We could not find a matching BG_RBRACKET. |
361 | * Ignore and just look for BG_RBRACE | |
72b16652 GS |
362 | */ |
363 | pm = pl; | |
364 | } | |
365 | break; | |
366 | ||
3e5d0dec | 367 | case BG_LBRACE: |
72b16652 GS |
368 | i++; |
369 | break; | |
370 | ||
3e5d0dec | 371 | case BG_RBRACE: |
72b16652 | 372 | if (i) { |
b8ef571c JH |
373 | i--; |
374 | break; | |
72b16652 GS |
375 | } |
376 | /* FALLTHROUGH */ | |
3e5d0dec GS |
377 | case BG_COMMA: |
378 | if (i && *pm == BG_COMMA) | |
72b16652 GS |
379 | break; |
380 | else { | |
381 | /* Append the current string */ | |
382 | for (lm = ls; (pl < pm); *lm++ = *pl++) | |
b8ef571c JH |
383 | ; |
384 | ||
72b16652 GS |
385 | /* |
386 | * Append the rest of the pattern after the | |
387 | * closing brace | |
388 | */ | |
b8ef571c JH |
389 | for (pl = pe + 1; (*lm++ = *pl++) != BG_EOS; ) |
390 | ; | |
72b16652 GS |
391 | |
392 | /* Expand the current pattern */ | |
393 | #ifdef GLOB_DEBUG | |
394 | qprintf("globexp2:", patbuf); | |
395 | #endif /* GLOB_DEBUG */ | |
396 | *rv = globexp1(patbuf, pglob); | |
397 | ||
398 | /* move after the comma, to the next string */ | |
399 | pl = pm + 1; | |
400 | } | |
401 | break; | |
402 | ||
403 | default: | |
404 | break; | |
405 | } | |
b8ef571c | 406 | } |
72b16652 GS |
407 | *rv = 0; |
408 | return 0; | |
409 | } | |
410 | ||
411 | ||
412 | ||
413 | /* | |
414 | * expand tilde from the passwd file. | |
415 | */ | |
416 | static const Char * | |
bac331f5 | 417 | globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob) |
72b16652 GS |
418 | { |
419 | struct passwd *pwd; | |
420 | char *h; | |
421 | const Char *p; | |
bac331f5 | 422 | Char *b, *eb; |
72b16652 | 423 | |
3e5d0dec | 424 | if (*pattern != BG_TILDE || !(pglob->gl_flags & GLOB_TILDE)) |
72b16652 GS |
425 | return pattern; |
426 | ||
427 | /* Copy up to the end of the string or / */ | |
bac331f5 JH |
428 | eb = &patbuf[patbuf_len - 1]; |
429 | for (p = pattern + 1, h = (char *) patbuf; | |
430 | h < (char*)eb && *p && *p != BG_SLASH; *h++ = *p++) | |
b8ef571c | 431 | ; |
72b16652 | 432 | |
3e5d0dec | 433 | *h = BG_EOS; |
72b16652 | 434 | |
b8ef571c JH |
435 | #if 0 |
436 | if (h == (char *)eb) | |
437 | return what; | |
438 | #endif | |
439 | ||
3e5d0dec | 440 | if (((char *) patbuf)[0] == BG_EOS) { |
72b16652 GS |
441 | /* |
442 | * handle a plain ~ or ~/ by expanding $HOME | |
443 | * first and then trying the password file | |
444 | */ | |
445 | if ((h = getenv("HOME")) == NULL) { | |
446 | #ifdef HAS_PASSWD | |
447 | if ((pwd = getpwuid(getuid())) == NULL) | |
448 | return pattern; | |
449 | else | |
450 | h = pwd->pw_dir; | |
451 | #else | |
452 | return pattern; | |
453 | #endif | |
454 | } | |
b8ef571c | 455 | } else { |
72b16652 GS |
456 | /* |
457 | * Expand a ~user | |
458 | */ | |
459 | #ifdef HAS_PASSWD | |
460 | if ((pwd = getpwnam((char*) patbuf)) == NULL) | |
461 | return pattern; | |
462 | else | |
463 | h = pwd->pw_dir; | |
464 | #else | |
465 | return pattern; | |
466 | #endif | |
467 | } | |
468 | ||
469 | /* Copy the home directory */ | |
bac331f5 | 470 | for (b = patbuf; b < eb && *h; *b++ = *h++) |
b8ef571c | 471 | ; |
72b16652 GS |
472 | |
473 | /* Append the rest of the pattern */ | |
bac331f5 | 474 | while (b < eb && (*b++ = *p++) != BG_EOS) |
b8ef571c | 475 | ; |
bac331f5 | 476 | *b = BG_EOS; |
72b16652 GS |
477 | |
478 | return patbuf; | |
479 | } | |
480 | ||
481 | ||
482 | /* | |
483 | * The main glob() routine: compiles the pattern (optionally processing | |
484 | * quotes), calls glob1() to do the real pattern matching, and finally | |
485 | * sorts the list (unless unsorted operation is requested). Returns 0 | |
486 | * if things went well, nonzero if errors occurred. It is not an error | |
487 | * to find no matches. | |
488 | */ | |
489 | static int | |
490 | glob0(const Char *pattern, glob_t *pglob) | |
491 | { | |
492 | const Char *qpat, *qpatnext; | |
493 | int c, err, oldflags, oldpathc; | |
b8ef571c JH |
494 | Char *bufnext, patbuf[MAXPATHLEN]; |
495 | size_t limit = 0; | |
72b16652 | 496 | |
7369a524 CN |
497 | #ifdef MACOS_TRADITIONAL |
498 | if ( (*pattern == BG_TILDE) && (pglob->gl_flags & GLOB_TILDE) ) { | |
6eb630b7 | 499 | return(globextend(pattern, pglob, &limit)); |
7369a524 CN |
500 | } |
501 | #endif | |
502 | ||
b8ef571c | 503 | qpat = globtilde(pattern, patbuf, MAXPATHLEN, pglob); |
72b16652 GS |
504 | qpatnext = qpat; |
505 | oldflags = pglob->gl_flags; | |
506 | oldpathc = pglob->gl_pathc; | |
507 | bufnext = patbuf; | |
508 | ||
509 | /* We don't need to check for buffer overflow any more. */ | |
3e5d0dec | 510 | while ((c = *qpatnext++) != BG_EOS) { |
72b16652 | 511 | switch (c) { |
3e5d0dec | 512 | case BG_LBRACKET: |
72b16652 | 513 | c = *qpatnext; |
3e5d0dec | 514 | if (c == BG_NOT) |
72b16652 | 515 | ++qpatnext; |
3e5d0dec GS |
516 | if (*qpatnext == BG_EOS || |
517 | g_strchr((Char *) qpatnext+1, BG_RBRACKET) == NULL) { | |
518 | *bufnext++ = BG_LBRACKET; | |
519 | if (c == BG_NOT) | |
72b16652 GS |
520 | --qpatnext; |
521 | break; | |
522 | } | |
523 | *bufnext++ = M_SET; | |
3e5d0dec | 524 | if (c == BG_NOT) |
72b16652 GS |
525 | *bufnext++ = M_NOT; |
526 | c = *qpatnext++; | |
527 | do { | |
528 | *bufnext++ = CHAR(c); | |
3e5d0dec GS |
529 | if (*qpatnext == BG_RANGE && |
530 | (c = qpatnext[1]) != BG_RBRACKET) { | |
72b16652 GS |
531 | *bufnext++ = M_RNG; |
532 | *bufnext++ = CHAR(c); | |
533 | qpatnext += 2; | |
534 | } | |
3e5d0dec | 535 | } while ((c = *qpatnext++) != BG_RBRACKET); |
72b16652 GS |
536 | pglob->gl_flags |= GLOB_MAGCHAR; |
537 | *bufnext++ = M_END; | |
538 | break; | |
3e5d0dec | 539 | case BG_QUESTION: |
72b16652 GS |
540 | pglob->gl_flags |= GLOB_MAGCHAR; |
541 | *bufnext++ = M_ONE; | |
542 | break; | |
3e5d0dec | 543 | case BG_STAR: |
72b16652 GS |
544 | pglob->gl_flags |= GLOB_MAGCHAR; |
545 | /* collapse adjacent stars to one, | |
546 | * to avoid exponential behavior | |
547 | */ | |
548 | if (bufnext == patbuf || bufnext[-1] != M_ALL) | |
b8ef571c | 549 | *bufnext++ = M_ALL; |
72b16652 GS |
550 | break; |
551 | default: | |
552 | *bufnext++ = CHAR(c); | |
553 | break; | |
554 | } | |
555 | } | |
3e5d0dec | 556 | *bufnext = BG_EOS; |
72b16652 GS |
557 | #ifdef GLOB_DEBUG |
558 | qprintf("glob0:", patbuf); | |
559 | #endif /* GLOB_DEBUG */ | |
560 | ||
b8ef571c | 561 | if ((err = glob1(patbuf, patbuf+MAXPATHLEN-1, pglob, &limit)) != 0) { |
72b16652 GS |
562 | pglob->gl_flags = oldflags; |
563 | return(err); | |
564 | } | |
565 | ||
566 | /* | |
567 | * If there was no match we are going to append the pattern | |
568 | * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified | |
569 | * and the pattern did not contain any magic characters | |
570 | * GLOB_NOMAGIC is there just for compatibility with csh. | |
571 | */ | |
572 | if (pglob->gl_pathc == oldpathc && | |
573 | ((pglob->gl_flags & GLOB_NOCHECK) || | |
574 | ((pglob->gl_flags & GLOB_NOMAGIC) && | |
575 | !(pglob->gl_flags & GLOB_MAGCHAR)))) | |
576 | { | |
577 | #ifdef GLOB_DEBUG | |
578 | printf("calling globextend from glob0\n"); | |
579 | #endif /* GLOB_DEBUG */ | |
580 | pglob->gl_flags = oldflags; | |
b8ef571c | 581 | return(globextend(qpat, pglob, &limit)); |
72b16652 GS |
582 | } |
583 | else if (!(pglob->gl_flags & GLOB_NOSORT)) | |
584 | qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc, | |
220398a0 | 585 | pglob->gl_pathc - oldpathc, sizeof(char *), |
2d5e9e5d JH |
586 | (pglob->gl_flags & (GLOB_ALPHASORT|GLOB_NOCASE)) |
587 | ? ci_compare : compare); | |
72b16652 GS |
588 | pglob->gl_flags = oldflags; |
589 | return(0); | |
590 | } | |
591 | ||
592 | static int | |
220398a0 PM |
593 | ci_compare(const void *p, const void *q) |
594 | { | |
b8ef571c JH |
595 | const char *pp = *(const char **)p; |
596 | const char *qq = *(const char **)q; | |
597 | int ci; | |
598 | while (*pp && *qq) { | |
599 | if (tolower(*pp) != tolower(*qq)) | |
600 | break; | |
601 | ++pp; | |
602 | ++qq; | |
603 | } | |
604 | ci = tolower(*pp) - tolower(*qq); | |
605 | if (ci == 0) | |
606 | return compare(p, q); | |
607 | return ci; | |
220398a0 PM |
608 | } |
609 | ||
610 | static int | |
72b16652 GS |
611 | compare(const void *p, const void *q) |
612 | { | |
613 | return(strcmp(*(char **)p, *(char **)q)); | |
614 | } | |
615 | ||
616 | static int | |
b8ef571c | 617 | glob1(Char *pattern, Char *pattern_last, glob_t *pglob, size_t *limitp) |
72b16652 | 618 | { |
b8ef571c | 619 | Char pathbuf[MAXPATHLEN]; |
72b16652 GS |
620 | |
621 | /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */ | |
3e5d0dec | 622 | if (*pattern == BG_EOS) |
72b16652 | 623 | return(0); |
b8ef571c JH |
624 | return(glob2(pathbuf, pathbuf+MAXPATHLEN-1, |
625 | pathbuf, pathbuf+MAXPATHLEN-1, | |
626 | pattern, pattern_last, pglob, limitp)); | |
72b16652 GS |
627 | } |
628 | ||
629 | /* | |
630 | * The functions glob2 and glob3 are mutually recursive; there is one level | |
631 | * of recursion for each segment in the pattern that contains one or more | |
632 | * meta characters. | |
633 | */ | |
634 | static int | |
b8ef571c JH |
635 | glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last, |
636 | Char *pattern, Char *pattern_last, glob_t *pglob, size_t *limitp) | |
72b16652 GS |
637 | { |
638 | Stat_t sb; | |
639 | Char *p, *q; | |
640 | int anymeta; | |
641 | ||
642 | /* | |
643 | * Loop over pattern segments until end of pattern or until | |
644 | * segment with meta character found. | |
645 | */ | |
646 | for (anymeta = 0;;) { | |
3e5d0dec GS |
647 | if (*pattern == BG_EOS) { /* End of pattern? */ |
648 | *pathend = BG_EOS; | |
72b16652 GS |
649 | if (g_lstat(pathbuf, &sb, pglob)) |
650 | return(0); | |
72b16652 GS |
651 | |
652 | if (((pglob->gl_flags & GLOB_MARK) && | |
220398a0 PM |
653 | pathend[-1] != BG_SEP |
654 | #ifdef DOSISH | |
655 | && pathend[-1] != BG_SEP2 | |
656 | #endif | |
b8ef571c JH |
657 | ) && (S_ISDIR(sb.st_mode) || |
658 | (S_ISLNK(sb.st_mode) && | |
72b16652 GS |
659 | (g_stat(pathbuf, &sb, pglob) == 0) && |
660 | S_ISDIR(sb.st_mode)))) { | |
b8ef571c JH |
661 | if (pathend+1 > pathend_last) |
662 | return (1); | |
3e5d0dec GS |
663 | *pathend++ = BG_SEP; |
664 | *pathend = BG_EOS; | |
72b16652 GS |
665 | } |
666 | ++pglob->gl_matchc; | |
667 | #ifdef GLOB_DEBUG | |
668 | printf("calling globextend from glob2\n"); | |
669 | #endif /* GLOB_DEBUG */ | |
b8ef571c | 670 | return(globextend(pathbuf, pglob, limitp)); |
72b16652 GS |
671 | } |
672 | ||
673 | /* Find end of next segment, copy tentatively to pathend. */ | |
674 | q = pathend; | |
675 | p = pattern; | |
220398a0 PM |
676 | while (*p != BG_EOS && *p != BG_SEP |
677 | #ifdef DOSISH | |
678 | && *p != BG_SEP2 | |
679 | #endif | |
680 | ) { | |
72b16652 GS |
681 | if (ismeta(*p)) |
682 | anymeta = 1; | |
b8ef571c JH |
683 | if (q+1 > pathend_last) |
684 | return (1); | |
72b16652 GS |
685 | *q++ = *p++; |
686 | } | |
687 | ||
688 | if (!anymeta) { /* No expansion, do next segment. */ | |
689 | pathend = q; | |
690 | pattern = p; | |
220398a0 PM |
691 | while (*pattern == BG_SEP |
692 | #ifdef DOSISH | |
693 | || *pattern == BG_SEP2 | |
694 | #endif | |
b8ef571c JH |
695 | ) { |
696 | if (pathend+1 > pathend_last) | |
697 | return (1); | |
72b16652 | 698 | *pathend++ = *pattern++; |
b8ef571c JH |
699 | } |
700 | } else | |
701 | /* Need expansion, recurse. */ | |
702 | return(glob3(pathbuf, pathbuf_last, pathend, | |
703 | pathend_last, pattern, pattern_last, | |
704 | p, pattern_last, pglob, limitp)); | |
72b16652 GS |
705 | } |
706 | /* NOTREACHED */ | |
707 | } | |
708 | ||
709 | static int | |
b8ef571c JH |
710 | glob3(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last, |
711 | Char *pattern, Char *pattern_last, | |
712 | Char *restpattern, Char *restpattern_last, glob_t *pglob, size_t *limitp) | |
72b16652 GS |
713 | { |
714 | register Direntry_t *dp; | |
715 | DIR *dirp; | |
716 | int err; | |
220398a0 | 717 | int nocase; |
72b16652 GS |
718 | char buf[MAXPATHLEN]; |
719 | ||
720 | /* | |
721 | * The readdirfunc declaration can't be prototyped, because it is | |
722 | * assigned, below, to two functions which are prototyped in glob.h | |
723 | * and dirent.h as taking pointers to differently typed opaque | |
724 | * structures. | |
725 | */ | |
cb359b41 | 726 | Direntry_t *(*readdirfunc)(DIR*); |
72b16652 | 727 | |
b8ef571c JH |
728 | if (pathend > pathend_last) |
729 | return (1); | |
3e5d0dec | 730 | *pathend = BG_EOS; |
72b16652 GS |
731 | errno = 0; |
732 | ||
f0963acb GS |
733 | #ifdef VMS |
734 | { | |
bac331f5 JH |
735 | Char *q = pathend; |
736 | if (q - pathbuf > 5) { | |
737 | q -= 5; | |
738 | if (q[0] == '.' && | |
739 | tolower(q[1]) == 'd' && tolower(q[2]) == 'i' && | |
740 | tolower(q[3]) == 'r' && q[4] == '/') | |
741 | { | |
742 | q[0] = '/'; | |
743 | q[1] = BG_EOS; | |
744 | pathend = q+1; | |
745 | } | |
746 | } | |
f0963acb GS |
747 | } |
748 | #endif | |
72b16652 GS |
749 | if ((dirp = g_opendir(pathbuf, pglob)) == NULL) { |
750 | /* TODO: don't call for ENOENT or ENOTDIR? */ | |
751 | if (pglob->gl_errfunc) { | |
b8ef571c JH |
752 | if (g_Ctoc(pathbuf, buf, sizeof(buf))) |
753 | return (GLOB_ABEND); | |
72b16652 GS |
754 | if (pglob->gl_errfunc(buf, errno) || |
755 | (pglob->gl_flags & GLOB_ERR)) | |
756 | return (GLOB_ABEND); | |
757 | } | |
758 | return(0); | |
759 | } | |
760 | ||
761 | err = 0; | |
220398a0 | 762 | nocase = ((pglob->gl_flags & GLOB_NOCASE) != 0); |
72b16652 GS |
763 | |
764 | /* Search directory for matching names. */ | |
765 | if (pglob->gl_flags & GLOB_ALTDIRFUNC) | |
b8ef571c | 766 | readdirfunc = (Direntry_t *(*)(DIR *))pglob->gl_readdir; |
72b16652 | 767 | else |
4f49e16e | 768 | readdirfunc = my_readdir; |
72b16652 GS |
769 | while ((dp = (*readdirfunc)(dirp))) { |
770 | register U8 *sc; | |
771 | register Char *dc; | |
772 | ||
3e5d0dec GS |
773 | /* Initial BG_DOT must be matched literally. */ |
774 | if (dp->d_name[0] == BG_DOT && *pattern != BG_DOT) | |
72b16652 | 775 | continue; |
b8ef571c JH |
776 | dc = pathend; |
777 | sc = (U8 *) dp->d_name; | |
778 | while (dc < pathend_last && (*dc++ = *sc++) != BG_EOS) | |
779 | ; | |
780 | if (dc >= pathend_last) { | |
781 | *dc = BG_EOS; | |
782 | err = 1; | |
783 | break; | |
784 | } | |
785 | ||
220398a0 | 786 | if (!match(pathend, pattern, restpattern, nocase)) { |
3e5d0dec | 787 | *pathend = BG_EOS; |
72b16652 GS |
788 | continue; |
789 | } | |
b8ef571c JH |
790 | err = glob2(pathbuf, pathbuf_last, --dc, pathend_last, |
791 | restpattern, restpattern_last, pglob, limitp); | |
72b16652 GS |
792 | if (err) |
793 | break; | |
794 | } | |
795 | ||
796 | if (pglob->gl_flags & GLOB_ALTDIRFUNC) | |
797 | (*pglob->gl_closedir)(dirp); | |
798 | else | |
4f49e16e | 799 | PerlDir_close(dirp); |
72b16652 GS |
800 | return(err); |
801 | } | |
802 | ||
803 | ||
804 | /* | |
805 | * Extend the gl_pathv member of a glob_t structure to accomodate a new item, | |
806 | * add the new item, and update gl_pathc. | |
807 | * | |
808 | * This assumes the BSD realloc, which only copies the block when its size | |
809 | * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic | |
810 | * behavior. | |
811 | * | |
812 | * Return 0 if new item added, error code if memory couldn't be allocated. | |
813 | * | |
814 | * Invariant of the glob_t structure: | |
815 | * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and | |
816 | * gl_pathv points to (gl_offs + gl_pathc + 1) items. | |
817 | */ | |
818 | static int | |
b8ef571c | 819 | globextend(const Char *path, glob_t *pglob, size_t *limitp) |
72b16652 GS |
820 | { |
821 | register char **pathv; | |
822 | register int i; | |
b8ef571c | 823 | STRLEN newsize, len; |
72b16652 GS |
824 | char *copy; |
825 | const Char *p; | |
826 | ||
827 | #ifdef GLOB_DEBUG | |
828 | printf("Adding "); | |
829 | for (p = path; *p; p++) | |
830 | (void)printf("%c", CHAR(*p)); | |
831 | printf("\n"); | |
3e5d0dec | 832 | #endif /* GLOB_DEBUG */ |
72b16652 | 833 | |
b8ef571c | 834 | newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs); |
4f49e16e | 835 | if (pglob->gl_pathv) |
b8ef571c | 836 | pathv = Renew(pglob->gl_pathv,newsize,char*); |
4f49e16e | 837 | else |
b8ef571c JH |
838 | New(0,pathv,newsize,char*); |
839 | if (pathv == NULL) { | |
840 | if (pglob->gl_pathv) { | |
841 | Safefree(pglob->gl_pathv); | |
842 | pglob->gl_pathv = NULL; | |
843 | } | |
72b16652 | 844 | return(GLOB_NOSPACE); |
b8ef571c | 845 | } |
72b16652 GS |
846 | |
847 | if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) { | |
848 | /* first time around -- clear initial gl_offs items */ | |
849 | pathv += pglob->gl_offs; | |
850 | for (i = pglob->gl_offs; --i >= 0; ) | |
851 | *--pathv = NULL; | |
852 | } | |
853 | pglob->gl_pathv = pathv; | |
854 | ||
855 | for (p = path; *p++;) | |
b8ef571c JH |
856 | ; |
857 | len = (STRLEN)(p - path); | |
bac331f5 | 858 | *limitp += len; |
4f49e16e GS |
859 | New(0, copy, p-path, char); |
860 | if (copy != NULL) { | |
b8ef571c JH |
861 | if (g_Ctoc(path, copy, len)) { |
862 | Safefree(copy); | |
863 | return(GLOB_NOSPACE); | |
864 | } | |
72b16652 GS |
865 | pathv[pglob->gl_offs + pglob->gl_pathc++] = copy; |
866 | } | |
867 | pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; | |
b8ef571c JH |
868 | |
869 | if ((pglob->gl_flags & GLOB_LIMIT) && | |
870 | newsize + *limitp >= ARG_MAX) { | |
871 | errno = 0; | |
872 | return(GLOB_NOSPACE); | |
873 | } | |
bac331f5 | 874 | |
72b16652 GS |
875 | return(copy == NULL ? GLOB_NOSPACE : 0); |
876 | } | |
877 | ||
878 | ||
879 | /* | |
880 | * pattern matching function for filenames. Each occurrence of the * | |
881 | * pattern causes a recursion level. | |
882 | */ | |
883 | static int | |
220398a0 | 884 | match(register Char *name, register Char *pat, register Char *patend, int nocase) |
72b16652 GS |
885 | { |
886 | int ok, negate_range; | |
887 | Char c, k; | |
888 | ||
889 | while (pat < patend) { | |
890 | c = *pat++; | |
891 | switch (c & M_MASK) { | |
892 | case M_ALL: | |
893 | if (pat == patend) | |
894 | return(1); | |
895 | do | |
220398a0 | 896 | if (match(name, pat, patend, nocase)) |
72b16652 | 897 | return(1); |
b8ef571c JH |
898 | while (*name++ != BG_EOS) |
899 | ; | |
72b16652 GS |
900 | return(0); |
901 | case M_ONE: | |
3e5d0dec | 902 | if (*name++ == BG_EOS) |
72b16652 GS |
903 | return(0); |
904 | break; | |
905 | case M_SET: | |
906 | ok = 0; | |
3e5d0dec | 907 | if ((k = *name++) == BG_EOS) |
72b16652 | 908 | return(0); |
3e5d0dec | 909 | if ((negate_range = ((*pat & M_MASK) == M_NOT)) != BG_EOS) |
72b16652 GS |
910 | ++pat; |
911 | while (((c = *pat++) & M_MASK) != M_END) | |
912 | if ((*pat & M_MASK) == M_RNG) { | |
220398a0 PM |
913 | if (nocase) { |
914 | if (tolower(c) <= tolower(k) && tolower(k) <= tolower(pat[1])) | |
915 | ok = 1; | |
916 | } else { | |
917 | if (c <= k && k <= pat[1]) | |
918 | ok = 1; | |
919 | } | |
72b16652 | 920 | pat += 2; |
220398a0 | 921 | } else if (nocase ? (tolower(c) == tolower(k)) : (c == k)) |
72b16652 GS |
922 | ok = 1; |
923 | if (ok == negate_range) | |
924 | return(0); | |
925 | break; | |
926 | default: | |
220398a0 PM |
927 | k = *name++; |
928 | if (nocase ? (tolower(k) != tolower(c)) : (k != c)) | |
72b16652 GS |
929 | return(0); |
930 | break; | |
931 | } | |
932 | } | |
3e5d0dec | 933 | return(*name == BG_EOS); |
72b16652 GS |
934 | } |
935 | ||
936 | /* Free allocated data belonging to a glob_t structure. */ | |
937 | void | |
938 | bsd_globfree(glob_t *pglob) | |
939 | { | |
940 | register int i; | |
941 | register char **pp; | |
942 | ||
943 | if (pglob->gl_pathv != NULL) { | |
944 | pp = pglob->gl_pathv + pglob->gl_offs; | |
945 | for (i = pglob->gl_pathc; i--; ++pp) | |
946 | if (*pp) | |
4f49e16e GS |
947 | Safefree(*pp); |
948 | Safefree(pglob->gl_pathv); | |
b8ef571c | 949 | pglob->gl_pathv = NULL; |
72b16652 GS |
950 | } |
951 | } | |
952 | ||
953 | static DIR * | |
954 | g_opendir(register Char *str, glob_t *pglob) | |
955 | { | |
956 | char buf[MAXPATHLEN]; | |
957 | ||
7369a524 CN |
958 | if (!*str) { |
959 | #ifdef MACOS_TRADITIONAL | |
960 | strcpy(buf, ":"); | |
961 | #else | |
72b16652 | 962 | strcpy(buf, "."); |
7369a524 CN |
963 | #endif |
964 | } else { | |
b8ef571c JH |
965 | if (g_Ctoc(str, buf, sizeof(buf))) |
966 | return(NULL); | |
7369a524 | 967 | } |
72b16652 GS |
968 | |
969 | if (pglob->gl_flags & GLOB_ALTDIRFUNC) | |
970 | return((*pglob->gl_opendir)(buf)); | |
bac331f5 JH |
971 | |
972 | return(PerlDir_open(buf)); | |
72b16652 GS |
973 | } |
974 | ||
72b16652 GS |
975 | static int |
976 | g_lstat(register Char *fn, Stat_t *sb, glob_t *pglob) | |
977 | { | |
978 | char buf[MAXPATHLEN]; | |
979 | ||
b8ef571c JH |
980 | if (g_Ctoc(fn, buf, sizeof(buf))) |
981 | return(-1); | |
72b16652 GS |
982 | if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
983 | return((*pglob->gl_lstat)(buf, sb)); | |
4f49e16e GS |
984 | #ifdef HAS_LSTAT |
985 | return(PerlLIO_lstat(buf, sb)); | |
986 | #else | |
987 | return(PerlLIO_stat(buf, sb)); | |
72b16652 | 988 | #endif /* HAS_LSTAT */ |
4f49e16e | 989 | } |
72b16652 GS |
990 | |
991 | static int | |
992 | g_stat(register Char *fn, Stat_t *sb, glob_t *pglob) | |
993 | { | |
994 | char buf[MAXPATHLEN]; | |
995 | ||
b8ef571c JH |
996 | if (g_Ctoc(fn, buf, sizeof(buf))) |
997 | return(-1); | |
72b16652 GS |
998 | if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
999 | return((*pglob->gl_stat)(buf, sb)); | |
4f49e16e | 1000 | return(PerlLIO_stat(buf, sb)); |
72b16652 GS |
1001 | } |
1002 | ||
1003 | static Char * | |
1004 | g_strchr(Char *str, int ch) | |
1005 | { | |
1006 | do { | |
1007 | if (*str == ch) | |
1008 | return (str); | |
1009 | } while (*str++); | |
1010 | return (NULL); | |
1011 | } | |
1012 | ||
b8ef571c JH |
1013 | static int |
1014 | g_Ctoc(register const Char *str, char *buf, STRLEN len) | |
72b16652 | 1015 | { |
b8ef571c JH |
1016 | while (len--) { |
1017 | if ((*buf++ = *str++) == BG_EOS) | |
1018 | return (0); | |
1019 | } | |
1020 | return (1); | |
72b16652 GS |
1021 | } |
1022 | ||
1023 | #ifdef GLOB_DEBUG | |
1024 | static void | |
1025 | qprintf(const char *str, register Char *s) | |
1026 | { | |
1027 | register Char *p; | |
1028 | ||
1029 | (void)printf("%s:\n", str); | |
1030 | for (p = s; *p; p++) | |
1031 | (void)printf("%c", CHAR(*p)); | |
1032 | (void)printf("\n"); | |
1033 | for (p = s; *p; p++) | |
1034 | (void)printf("%c", *p & M_PROTECT ? '"' : ' '); | |
1035 | (void)printf("\n"); | |
1036 | for (p = s; *p; p++) | |
1037 | (void)printf("%c", ismeta(*p) ? '_' : ' '); | |
1038 | (void)printf("\n"); | |
1039 | } | |
1040 | #endif /* GLOB_DEBUG */ |