2 * Copyright (c) 1989 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
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
32 #if defined(LIBC_SCCS) && !defined(lint)
33 static char sccsid[] = "@(#)glob.c 5.12 (Berkeley) 6/24/91";
34 #endif /* LIBC_SCCS and not lint */
36 * Glob: the interface is a superset of the one defined in POSIX 1003.2,
39 * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
41 * Optional extra services, controlled by flags not defined by POSIX:
44 * Escaping convention: \ inhibits any special meaning the following
45 * character might have (except \ at end of string is retained).
47 * Set in gl_flags if pattern contained a globbing character.
49 * Use ^ instead of ! for "not".
51 * Number of matches in the current invocation of glob.
55 #pragma warning(disable:4244)
56 #endif /* WINNT_NATIVE */
71 #define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
74 #if !defined(S_ISLNK) && defined(S_IFLNK)
75 #define S_ISLNK(a) (((a) & S_IFMT) == S_IFLNK)
78 #if !defined(S_ISLNK) && !defined(lstat)
82 typedef unsigned short Char;
84 static int glob1 __P((Char *, glob_t *, int));
85 static int glob2 __P((Char *, Char *, Char *, glob_t *, int));
86 static int glob3 __P((Char *, Char *, Char *, Char *,
88 static int globextend __P((Char *, glob_t *));
89 static int match __P((Char *, Char *, Char *, int));
91 static int compare __P((const ptr_t, const ptr_t));
93 static DIR *Opendir __P((Char *));
95 static int Lstat __P((Char *, struct stat *));
97 static int Stat __P((Char *, struct stat *sb));
98 static Char *Strchr __P((Char *, int));
100 static void qprintf __P((Char *));
116 #define UNDERSCORE '_'
118 #define M_META 0x8000
119 #define M_PROTECT 0x4000
120 #define M_MASK 0xffff
121 #define M_ASCII 0x00ff
123 #define CHAR(c) ((c)&M_ASCII)
124 #define META(c) ((c)|M_META)
125 #define M_ALL META('*')
126 #define M_END META(']')
127 #define M_NOT META('!')
128 #define M_ALTNOT META('^')
129 #define M_ONE META('?')
130 #define M_RNG META('-')
131 #define M_SET META('[')
132 #define ismeta(c) (((c)&M_META) != 0)
135 #define GLOBBUFLEN MAXPATHLEN
137 #define GLOBBUFLEN BUFSIZE
141 globcharcoll(c1, c2, cs)
145 #if defined(NLS) && defined(LC_COLLATE) && !defined(NOSTRCOLL)
146 # if defined(SHORT_STRINGS)
147 wchar_t s1[2], s2[2];
155 /* This should not be here, but I'll rather leave it in than engage in
156 a LC_COLLATE flamewar about a shell I don't use... */
157 if (iswlower(c1) && iswupper(c2))
159 if (iswupper(c1) && iswlower(c2))
164 s1[1] = s2[1] = '\0';
165 return wcscoll(s1, s2);
166 # else /* not SHORT_STRINGS */
172 * From kevin lyda <kevin@suberic.net>:
173 * strcoll does not guarantee case sorting, so we pre-process now:
176 c1 = islower(c1) ? c1 : tolower(c1);
177 c2 = islower(c2) ? c2 : tolower(c2);
179 if (islower(c1) && isupper(c2))
181 if (isupper(c1) && islower(c2))
186 s1[1] = s2[1] = '\0';
187 return strcoll(s1, s2);
195 * Need to dodge two kernel bugs:
196 * opendir("") != opendir(".")
197 * NAMEI_BUG: on plain files trailing slashes are ignored in some kernels.
198 * POSIX specifies that they should be ignored in directories.
205 char buf[GLOBBUFLEN];
207 #if defined(hpux) || defined(__hpux)
212 return (opendir("."));
213 while ((*dc++ = *str++) != '\0')
215 #if defined(hpux) || defined(__hpux)
217 * Opendir on some device files hangs, so avoid it
219 if (stat(buf, &st) == -1 || !S_ISDIR(st.st_mode))
222 return (opendir(buf));
231 char buf[GLOBBUFLEN];
234 while ((*dc++ = *fn++) != '\0')
243 return (*--dc == '/' && !S_ISDIR(sb->st_mode) ? -1 : st);
246 return (lstat(buf, sb));
247 # endif /* NAMEI_BUG */
258 char buf[GLOBBUFLEN];
261 while ((*dc++ = *fn++) != '\0')
270 return (*--dc == '/' && !S_ISDIR(sb->st_mode) ? -1 : st);
273 return (stat(buf, sb));
274 #endif /* NAMEI_BUG */
297 printf("%c", *p & 0xff);
300 printf("%c", *p & M_PROTECT ? '"' : ' ');
303 printf("%c", *p & M_META ? '_' : ' ');
312 #if defined(NLS) && !defined(NOSTRCOLL)
313 errno = 0; /* strcoll sets errno, another brain-damage */
315 return (strcoll(*(char **) p, *(char **) q));
317 return (strcmp(*(char **) p, *(char **) q));
318 #endif /* NLS && !NOSTRCOLL */
322 * The main glob() routine: compiles the pattern (optionally processing
323 * quotes), calls glob1() to do the real pattern matching, and finally
324 * sorts the list (unless unsorted operation is requested). Returns 0
325 * if things went well, nonzero if errors occurred. It is not an error
326 * to find no matches.
329 glob(pattern, flags, errfunc, pglob)
332 int (*errfunc) __P((const char *, int));
336 Char *bufnext, *bufend, *compilebuf, m_not;
337 const unsigned char *compilepat, *patnext;
341 Char patbuf[GLOBBUFLEN + MB_LEN_MAX + 1];
343 Char patbuf[GLOBBUFLEN + 1];
347 patnext = (const unsigned char *) pattern;
348 if (!(flags & GLOB_APPEND)) {
350 pglob->gl_pathv = NULL;
351 if (!(flags & GLOB_DOOFFS))
354 pglob->gl_flags = flags & ~GLOB_MAGCHAR;
355 pglob->gl_errfunc = errfunc;
356 oldpathc = pglob->gl_pathc;
357 pglob->gl_matchc = 0;
359 if (pglob->gl_flags & GLOB_ALTNOT) {
369 bufend = bufnext + GLOBBUFLEN;
370 compilebuf = bufnext;
371 compilepat = patnext;
373 no_match = *patnext == not;
377 if (flags & GLOB_QUOTE) {
378 /* Protect the quoted characters */
379 while (bufnext < bufend && (c = *patnext++) != EOS) {
383 len = mblen((const char *)(patnext - 1), MB_LEN_MAX);
387 *bufnext++ = (Char) c;
389 *bufnext++ = (Char) (*patnext++ | M_PROTECT);
391 #endif /* WIDE_STRINGS */
393 if ((c = *patnext++) == EOS) {
397 *bufnext++ = (Char) (c | M_PROTECT);
400 *bufnext++ = (Char) c;
404 while (bufnext < bufend && (c = *patnext++) != EOS)
405 *bufnext++ = (Char) c;
410 /* we don't need to check for buffer overflow any more */
411 while ((c = *qpatnext++) != EOS) {
417 if (*qpatnext == EOS ||
418 Strchr(qpatnext + 1, RBRACKET) == NULL) {
419 *bufnext++ = LBRACKET;
424 pglob->gl_flags |= GLOB_MAGCHAR;
430 *bufnext++ = CHAR(c);
431 if (*qpatnext == RANGE &&
432 (c = qpatnext[1]) != RBRACKET) {
434 *bufnext++ = CHAR(c);
437 } while ((c = *qpatnext++) != RBRACKET);
441 pglob->gl_flags |= GLOB_MAGCHAR;
445 pglob->gl_flags |= GLOB_MAGCHAR;
446 /* collapse adjacent stars to one, to avoid
447 * exponential behavior
449 if (bufnext == patbuf || bufnext[-1] != M_ALL)
453 *bufnext++ = CHAR(c);
462 if ((err = glob1(patbuf, pglob, no_match)) != 0)
466 * If there was no match we are going to append the pattern
467 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
468 * and the pattern did not contain any magic characters
469 * GLOB_NOMAGIC is there just for compatibility with csh.
471 if (pglob->gl_pathc == oldpathc &&
472 ((flags & GLOB_NOCHECK) ||
473 ((flags & GLOB_NOMAGIC) && !(pglob->gl_flags & GLOB_MAGCHAR)))) {
474 if (!(flags & GLOB_QUOTE)) {
475 Char *dp = compilebuf;
476 const unsigned char *sp = compilepat;
478 while ((*dp++ = *sp++) != '\0')
483 * copy pattern, interpreting quotes; this is slightly different
484 * than the interpretation of quotes above -- which should prevail?
486 while (*compilepat != EOS) {
487 if (*compilepat == QUOTE) {
488 if (*++compilepat == EOS)
491 *compilebuf++ = (unsigned char) *compilepat++;
495 return (globextend(patbuf, pglob));
497 else if (!(flags & GLOB_NOSORT) && (pglob->gl_pathc != oldpathc))
498 qsort((char *) (pglob->gl_pathv + pglob->gl_offs + oldpathc),
499 pglob->gl_pathc - oldpathc, sizeof(char *),
500 (int (*) __P((const void *, const void *))) compare);
505 glob1(pattern, pglob, no_match)
510 Char pathbuf[GLOBBUFLEN + 1];
513 * a null pathname is invalid -- POSIX 1003.1 sect. 2.4.
517 return (glob2(pathbuf, pathbuf, pattern, pglob, no_match));
521 * functions glob2 and glob3 are mutually recursive; there is one level
522 * of recursion for each segment in the pattern that contains one or
523 * more meta characters.
526 glob2(pathbuf, pathend, pattern, pglob, no_match)
527 Char *pathbuf, *pathend, *pattern;
536 * loop over pattern segments until end of pattern or until segment with
537 * meta character found.
541 if (*pattern == EOS) { /* end of pattern? */
544 if (Lstat(pathbuf, &sbuf))
547 if (((pglob->gl_flags & GLOB_MARK) &&
548 pathend[-1] != SEP) &&
549 (S_ISDIR(sbuf.st_mode)
551 || (S_ISLNK(sbuf.st_mode) &&
552 (Stat(pathbuf, &sbuf) == 0) &&
553 S_ISDIR(sbuf.st_mode))
560 return (globextend(pathbuf, pglob));
563 /* find end of next segment, copy tentatively to pathend */
566 while (*p != EOS && *p != SEP) {
572 if (!anymeta) { /* no expansion, do next segment */
575 while (*pattern == SEP)
576 *pathend++ = *pattern++;
578 else /* need expansion, recurse */
579 return (glob3(pathbuf, pathend, pattern, p, pglob, no_match));
586 glob3(pathbuf, pathend, pattern, restpattern, pglob, no_match)
587 Char *pathbuf, *pathend, *pattern, *restpattern;
594 Char m_not = (pglob->gl_flags & GLOB_ALTNOT) ? M_ALTNOT : M_NOT;
595 char cpathbuf[GLOBBUFLEN], *ptr;;
600 if (!(dirp = Opendir(pathbuf))) {
601 /* todo: don't call for ENOENT or ENOTDIR? */
602 for (ptr = cpathbuf; (*ptr++ = (char) *pathbuf++) != EOS;)
604 if ((pglob->gl_errfunc && (*pglob->gl_errfunc) (cpathbuf, errno)) ||
605 (pglob->gl_flags & GLOB_ERR))
613 /* search directory for matching names */
614 while ((dp = readdir(dirp)) != NULL) {
618 /* initial DOT must be matched literally */
619 if (dp->d_name[0] == DOT && *pattern != DOT)
621 for (sc = (unsigned char *) dp->d_name, dc = pathend;
622 (*dc++ = *sc++) != '\0';)
624 if (match(pathend, pattern, restpattern, (int) m_not) == no_match) {
628 err = glob2(pathbuf, --dc, restpattern, pglob, no_match);
632 /* todo: check error from readdir? */
633 (void) closedir(dirp);
639 * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
640 * add the new item, and update gl_pathc.
642 * This assumes the BSD realloc, which only copies the block when its size
643 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
646 * Return 0 if new item added, error code if memory couldn't be allocated.
648 * Invariant of the glob_t structure:
649 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
650 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
653 globextend(path, pglob)
659 unsigned int newsize;
663 newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
664 pathv = (char **) (pglob->gl_pathv ?
665 xrealloc((ptr_t) pglob->gl_pathv, (size_t) newsize) :
666 xmalloc((size_t) newsize));
668 return (GLOB_NOSPACE);
670 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
671 /* first time around -- clear initial gl_offs items */
672 pathv += pglob->gl_offs;
673 for (i = pglob->gl_offs; --i >= 0;)
676 pglob->gl_pathv = pathv;
678 for (p = path; *p++;)
680 if ((copy = (char *) xmalloc((size_t) (p - path))) != NULL) {
684 while ((*dc++ = *sc++) != '\0')
686 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
688 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
689 return ((copy == NULL) ? GLOB_NOSPACE : 0);
694 One_mbtowc(NLSChar *pwc, const Char *s, size_t n)
697 char buf[MB_LEN_MAX], *p;
702 while (p < buf + n && (*p++ = CHAR(*s++)) != 0)
704 return one_mbtowc(pwc, buf, n);
706 return NLSFrom(s, n, pwc);
711 * pattern matching function for filenames. Each occurrence of the *
712 * pattern causes a recursion level.
715 match(name, pat, patend, m_not)
716 Char *name, *pat, *patend;
719 int ok, negate_range;
722 while (pat < patend) {
727 c = *pat; /* Only for M_MASK bits */
728 pat += One_mbtowc(&wc, pat, MB_LEN_MAX);
729 lwk = One_mbtowc(&wk, name, MB_LEN_MAX);
730 switch (c & M_MASK) {
735 if (match(name, pat, patend, m_not))
740 lwk = One_mbtowc(&wk, name, MB_LEN_MAX);
753 if ((negate_range = ((*pat & M_MASK) == m_not)) != 0)
755 while ((*pat & M_MASK) != M_END) {
756 pat += One_mbtowc(&wc, pat, MB_LEN_MAX);
757 if ((*pat & M_MASK) == M_RNG) {
761 pat += One_mbtowc(&wc2, pat, MB_LEN_MAX);
762 if (globcharcoll(wc, wk, 0) <= 0 &&
763 globcharcoll(wk, wc2, 0) <= 0)
768 pat += One_mbtowc(&wc, pat, MB_LEN_MAX);
769 if (ok == negate_range)
774 if (samecase(wk) != samecase(wc))
779 return (*name == EOS);
782 /* free allocated data belonging to a glob_t structure */
790 if (pglob->gl_pathv != NULL) {
791 pp = pglob->gl_pathv + pglob->gl_offs;
792 for (i = pglob->gl_pathc; i--; ++pp)
794 xfree((ptr_t) *pp), *pp = NULL;
795 xfree((ptr_t) pglob->gl_pathv), pglob->gl_pathv = NULL;