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