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