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