2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
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 * 4. 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 * @(#)glob.c 8.3 (Berkeley) 10/13/93
33 * $FreeBSD: src/lib/libc/gen/glob.c,v 1.27 2008/06/26 07:12:35 mtm Exp $
34 * $DragonFly: src/lib/libc/gen/glob.c,v 1.6 2005/12/07 02:28:15 corecode Exp $
38 * glob(3) -- a superset of the one defined in POSIX 1003.2.
40 * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
42 * Optional extra services, controlled by flags not defined by POSIX:
45 * Escaping convention: \ inhibits any special meaning the following
46 * character might have (except \ at end of string is retained).
48 * Set in gl_flags if pattern contained a globbing character.
50 * Same as GLOB_NOCHECK, but it will only append pattern if it did
51 * not contain any magic characters. [Used in csh style globbing]
53 * Use alternately specified directory access functions.
55 * expand ~user/foo to the /home/dir/of/user/foo
57 * expand {1,2}{a,b} to 1a 1b 2a 2b
59 * Number of matches in the current invocation of glob.
63 * Some notes on multibyte character support:
64 * 1. Patterns with illegal byte sequences match nothing - even if
65 * GLOB_NOCHECK is specified.
66 * 2. Illegal byte sequences in filenames are handled by treating them as
67 * single-byte characters with a value of the first byte of the sequence
69 * 3. State-dependent encodings are not currently supported.
72 #include <sys/param.h>
102 #define UNDERSCORE '_'
110 #define M_QUOTE 0x8000000000ULL
111 #define M_PROTECT 0x4000000000ULL
112 #define M_MASK 0xffffffffffULL
113 #define M_CHAR 0x00ffffffffULL
115 typedef uint_fast64_t Char;
120 #define M_PROTECT 0x40
129 #define CHAR(c) ((Char)((c)&M_CHAR))
130 #define META(c) ((Char)((c)|M_QUOTE))
131 #define M_ALL META('*')
132 #define M_END META(']')
133 #define M_NOT META('!')
134 #define M_ONE META('?')
135 #define M_RNG META('-')
136 #define M_SET META('[')
137 #define ismeta(c) (((c)&M_QUOTE) != 0)
140 #define GLOB_LIMIT_MALLOC 65536
141 #define GLOB_LIMIT_STAT 128
142 #define GLOB_LIMIT_READDIR 16384
144 #define GLOB_INDEX_MALLOC 0
145 #define GLOB_INDEX_STAT 1
146 #define GLOB_INDEX_READDIR 2
148 static int compare(const void *, const void *);
149 static int g_Ctoc(const Char *, char *, size_t);
150 static int g_lstat(const Char *, struct stat *, glob_t *);
151 static DIR *g_opendir(const Char *, glob_t *);
152 static const Char *g_strchr(const Char *, wchar_t);
154 static Char *g_strcat(Char *, const Char *);
156 static int g_stat(const Char *, struct stat *, glob_t *);
157 static int glob0(const Char *, glob_t *, size_t *);
158 static int glob1(const Char *, glob_t *, size_t *);
159 static int glob2(Char *, Char *, Char *, const Char *, glob_t *,
161 static int glob3(Char *, Char *, Char *, const Char *, const Char *,
163 static int globextend(const Char *, glob_t *, size_t *);
165 globtilde(const Char *, Char *, size_t, glob_t *);
166 static int globexp1(const Char *, glob_t *, size_t *);
167 static int globexp2(const Char *, const Char *, glob_t *, int *, size_t *);
168 static int match(const Char *, const Char *, const Char *);
170 static void qprintf(const char *, const Char *);
173 /* 0 = malloc(), 1 = stat(), 2 = readdir() */
174 static size_t limits[] = { 0, 0, 0 };
177 glob(const char *pattern, int flags, int (*errfunc)(const char *, int),
182 Char *bufnext, *bufend, patbuf[MAXPATHLEN], prot;
188 if (!(flags & GLOB_APPEND)) {
190 pglob->gl_pathv = NULL;
191 if (!(flags & GLOB_DOOFFS))
194 if (flags & GLOB_LIMIT) {
195 limit = pglob->gl_matchc;
200 pglob->gl_flags = flags & ~GLOB_MAGCHAR;
201 pglob->gl_errfunc = errfunc;
202 pglob->gl_matchc = 0;
205 bufend = bufnext + MAXPATHLEN - 1;
206 if (flags & GLOB_NOESCAPE) {
207 memset(&mbs, 0, sizeof(mbs));
208 while (bufend - bufnext >= MB_CUR_MAX) {
209 clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
210 if (clen == (size_t)-1 || clen == (size_t)-2)
211 return (GLOB_NOMATCH);
218 /* Protect the quoted characters. */
219 memset(&mbs, 0, sizeof(mbs));
220 while (bufend - bufnext >= MB_CUR_MAX) {
221 if (*patnext == QUOTE) {
222 if (*++patnext == EOS) {
223 *bufnext++ = QUOTE | M_PROTECT;
229 clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
230 if (clen == (size_t)-1 || clen == (size_t)-2)
231 return (GLOB_NOMATCH);
234 *bufnext++ = wc | prot;
240 if (flags & GLOB_BRACE)
241 return globexp1(patbuf, pglob, &limit);
243 return glob0(patbuf, pglob, &limit);
247 * Expand recursively a glob {} pattern. When there is no more expansion
248 * invoke the standard globbing routine to glob the rest of the magic
252 globexp1(const Char *pattern, glob_t *pglob, size_t *limit)
254 const Char *ptr = pattern;
257 /* Protect a single {}, for find(1), like csh */
258 if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
259 return glob0(pattern, pglob, limit);
261 while ((ptr = g_strchr(ptr, LBRACE)) != NULL)
262 if (!globexp2(ptr, pattern, pglob, &rv, limit))
265 return glob0(pattern, pglob, limit);
270 * Recursive brace globbing helper. Tries to expand a single brace.
271 * If it succeeds then it invokes globexp1 with the new pattern.
272 * If it fails then it tries to glob the rest of the pattern and returns.
275 globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv,
280 const Char *pe, *pm, *pm1, *pl;
281 Char patbuf[MAXPATHLEN];
283 /* copy part up to the brace */
284 for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
289 /* Find the balanced brace */
290 for (i = 0, pe = ++ptr; *pe; pe++)
291 if (*pe == LBRACKET) {
292 /* Ignore everything between [] */
293 for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
297 * We could not find a matching RBRACKET.
298 * Ignore and just look for RBRACE
303 else if (*pe == LBRACE)
305 else if (*pe == RBRACE) {
311 /* Non matching braces; just glob the pattern */
312 if (i != 0 || *pe == EOS) {
313 *rv = glob0(patbuf, pglob, limit);
317 for (i = 0, pl = pm = ptr; pm <= pe; pm++)
320 /* Ignore everything between [] */
321 for (pm1 = pm++; *pm != RBRACKET && *pm != EOS; pm++)
325 * We could not find a matching RBRACKET.
326 * Ignore and just look for RBRACE
343 if (i && *pm == COMMA)
346 /* Append the current string */
347 for (lm = ls; (pl < pm); *lm++ = *pl++)
350 * Append the rest of the pattern after the
353 for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
356 /* Expand the current pattern */
358 qprintf("globexp2:", patbuf);
360 *rv = globexp1(patbuf, pglob, limit);
362 /* move after the comma, to the next string */
377 * expand tilde from the passwd file.
380 globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
387 if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
391 * Copy up to the end of the string or /
393 eb = &patbuf[patbuf_len - 1];
394 for (p = pattern + 1, h = (char *) patbuf;
395 h < (char *)eb && *p && *p != SLASH; *h++ = *p++)
400 if (((char *) patbuf)[0] == EOS) {
402 * handle a plain ~ or ~/ by expanding $HOME first (iff
403 * we're not running setuid or setgid) and then trying
406 if (issetugid() != 0 ||
407 (h = getenv("HOME")) == NULL) {
408 if (((h = getlogin()) != NULL &&
409 (pwd = getpwnam(h)) != NULL) ||
410 (pwd = getpwuid(getuid())) != NULL)
420 if ((pwd = getpwnam((char*) patbuf)) == NULL)
426 /* Copy the home directory */
427 for (b = patbuf; b < eb && *h; *b++ = *h++)
430 /* Append the rest of the pattern */
431 while (b < eb && (*b++ = *p++) != EOS)
440 * The main glob() routine: compiles the pattern (optionally processing
441 * quotes), calls glob1() to do the real pattern matching, and finally
442 * sorts the list (unless unsorted operation is requested). Returns 0
443 * if things went well, nonzero if errors occurred.
446 glob0(const Char *pattern, glob_t *pglob, size_t *limit)
448 const Char *qpatnext;
451 Char *bufnext, patbuf[MAXPATHLEN];
453 qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
454 oldpathc = pglob->gl_pathc;
457 /* We don't need to check for buffer overflow any more. */
458 while ((c = *qpatnext++) != EOS) {
464 if (*qpatnext == EOS ||
465 g_strchr(qpatnext+1, RBRACKET) == NULL) {
466 *bufnext++ = LBRACKET;
476 *bufnext++ = CHAR(c);
477 if (*qpatnext == RANGE &&
478 (c = qpatnext[1]) != RBRACKET) {
480 *bufnext++ = CHAR(c);
483 } while ((c = *qpatnext++) != RBRACKET);
484 pglob->gl_flags |= GLOB_MAGCHAR;
488 pglob->gl_flags |= GLOB_MAGCHAR;
492 pglob->gl_flags |= GLOB_MAGCHAR;
493 /* collapse adjacent stars to one,
494 * to avoid exponential behavior
496 if (bufnext == patbuf || bufnext[-1] != M_ALL)
500 *bufnext++ = CHAR(c);
506 qprintf("glob0:", patbuf);
509 if ((err = glob1(patbuf, pglob, limit)) != 0)
513 * If there was no match we are going to append the pattern
514 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
515 * and the pattern did not contain any magic characters
516 * GLOB_NOMAGIC is there just for compatibility with csh.
518 if (pglob->gl_pathc == oldpathc) {
519 if (((pglob->gl_flags & GLOB_NOCHECK) ||
520 ((pglob->gl_flags & GLOB_NOMAGIC) &&
521 !(pglob->gl_flags & GLOB_MAGCHAR))))
522 return(globextend(pattern, pglob, limit));
524 return(GLOB_NOMATCH);
526 if (!(pglob->gl_flags & GLOB_NOSORT))
527 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
528 pglob->gl_pathc - oldpathc, sizeof(char *), compare);
533 compare(const void *p, const void *q)
535 return(strcmp(*(const char * const *)p, *(const char * const *)q));
539 glob1(const Char *pattern, glob_t *pglob, size_t *limit)
541 Char pathbuf[MAXPATHLEN];
543 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
546 return(glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1,
547 pattern, pglob, limit));
551 * The functions glob2 and glob3 are mutually recursive; there is one level
552 * of recursion for each segment in the pattern that contains one or more
556 glob2(Char *pathbuf, Char *pathend, Char *pathend_last, const Char *pattern,
557 glob_t *pglob, size_t *limit)
565 * Loop over pattern segments until end of pattern or until
566 * segment with meta character found.
568 for (anymeta = 0;;) {
569 if (*pattern == EOS) { /* End of pattern? */
571 if (g_lstat(pathbuf, &sb, pglob))
574 if ((pglob->gl_flags & GLOB_LIMIT) &&
575 limits[GLOB_INDEX_STAT]++ >= GLOB_LIMIT_STAT) {
582 if (((pglob->gl_flags & GLOB_MARK) &&
583 pathend[-1] != SEP) && (S_ISDIR(sb.st_mode)
584 || (S_ISLNK(sb.st_mode) &&
585 (g_stat(pathbuf, &sb, pglob) == 0) &&
586 S_ISDIR(sb.st_mode)))) {
587 if (pathend + 1 > pathend_last)
588 return (GLOB_ABORTED);
593 return(globextend(pathbuf, pglob, limit));
596 /* Find end of next segment, copy tentatively to pathend. */
599 while (*p != EOS && *p != SEP) {
602 if (q + 1 > pathend_last)
603 return (GLOB_ABORTED);
607 if (!anymeta) { /* No expansion, do next segment. */
610 while (*pattern == SEP) {
611 if (pathend + 1 > pathend_last)
612 return (GLOB_ABORTED);
613 *pathend++ = *pattern++;
615 } else /* Need expansion, recurse. */
616 return(glob3(pathbuf, pathend, pathend_last, pattern, p,
623 glob3(Char *pathbuf, Char *pathend, Char *pathend_last, const Char *pattern,
624 const Char *restpattern, glob_t *pglob, size_t *limit)
629 char buf[MAXPATHLEN];
630 struct dirent *(*readdirfunc)(DIR *);
632 if (pathend > pathend_last)
633 return (GLOB_ABORTED);
637 if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
638 /* TODO: don't call for ENOENT or ENOTDIR? */
639 if (pglob->gl_errfunc) {
640 if (g_Ctoc(pathbuf, buf, sizeof(buf)))
641 return (GLOB_ABORTED);
642 if (pglob->gl_errfunc(buf, errno) ||
643 pglob->gl_flags & GLOB_ERR)
644 return (GLOB_ABORTED);
651 /* pglob->gl_readdir takes a void *, fix this manually. */
652 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
653 readdirfunc = (struct dirent *(*)(DIR *))pglob->gl_readdir;
655 readdirfunc = readdir;
657 /* Search directory for matching names. */
658 while ((dp = (*readdirfunc)(dirp)) != NULL) {
665 if ((pglob->gl_flags & GLOB_LIMIT) &&
666 limits[GLOB_INDEX_READDIR]++ >= GLOB_LIMIT_READDIR) {
673 /* Initial DOT must be matched literally. */
674 if (dp->d_name[0] == DOT && *pattern != DOT &&
675 !(pglob->gl_flags & GLOB_PERIOD))
677 memset(&mbs, 0, sizeof(mbs));
680 while (dc < pathend_last) {
681 clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
682 if (clen == (size_t)-1 || clen == (size_t)-2) {
685 memset(&mbs, 0, sizeof(mbs));
687 if ((*dc++ = wc) == EOS)
691 if (!match(pathend, pattern, restpattern)) {
695 err = glob2(pathbuf, --dc, pathend_last, restpattern,
701 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
702 (*pglob->gl_closedir)(dirp);
710 * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
711 * add the new item, and update gl_pathc.
713 * This assumes the BSD realloc, which only copies the block when its size
714 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
717 * Return 0 if new item added, error code if memory couldn't be allocated.
719 * Invariant of the glob_t structure:
720 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
721 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
724 globextend(const Char *path, glob_t *pglob, size_t *limit)
727 size_t i, newsize, len;
731 if (*limit && pglob->gl_pathc > *limit) {
733 return (GLOB_NOSPACE);
736 newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
737 pathv = pglob->gl_pathv ?
738 realloc((char *)pglob->gl_pathv, newsize) :
741 if (pglob->gl_pathv) {
742 free(pglob->gl_pathv);
743 pglob->gl_pathv = NULL;
745 return(GLOB_NOSPACE);
748 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
749 /* first time around -- clear initial gl_offs items */
750 pathv += pglob->gl_offs;
751 for (i = pglob->gl_offs + 1; --i > 0; )
754 pglob->gl_pathv = pathv;
756 for (p = path; *p++;)
758 len = MB_CUR_MAX * (size_t)(p - path); /* XXX overallocation */
759 limits[GLOB_INDEX_MALLOC] += len;
760 if ((copy = malloc(len)) != NULL) {
761 if (g_Ctoc(path, copy, len)) {
763 return (GLOB_NOSPACE);
765 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
767 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
768 if ((pglob->gl_flags & GLOB_LIMIT) &&
769 (newsize + limits[GLOB_INDEX_MALLOC]) >= GLOB_LIMIT_MALLOC) {
774 return(copy == NULL ? GLOB_NOSPACE : 0);
778 * pattern matching function for filenames. Each occurrence of the *
779 * pattern causes a recursion level.
782 match(const Char * name, const Char *pat, const Char *patend)
784 int ok, negate_range;
787 while (pat < patend) {
789 switch (c & M_MASK) {
794 if (match(name, pat, patend))
796 while (*name++ != EOS);
804 if ((k = *name++) == EOS)
806 if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
808 while (((c = *pat++) & M_MASK) != M_END)
809 if ((*pat & M_MASK) == M_RNG) {
810 if (__collate_load_error ?
811 CHAR(c) <= CHAR(k) && CHAR(k) <= CHAR(pat[1]) :
812 __collate_range_cmp(CHAR(c), CHAR(k)) <= 0
813 && __collate_range_cmp(CHAR(k), CHAR(pat[1])) <= 0
819 if (ok == negate_range)
828 return(*name == EOS);
831 /* Free allocated data belonging to a glob_t structure. */
833 globfree(glob_t *pglob)
838 if (pglob->gl_pathv != NULL) {
839 pp = pglob->gl_pathv + pglob->gl_offs;
840 for (i = pglob->gl_pathc; i--; ++pp)
843 free(pglob->gl_pathv);
844 pglob->gl_pathv = NULL;
849 g_opendir(const Char *str, glob_t *pglob)
851 char buf[MAXPATHLEN];
855 else if (g_Ctoc(str, buf, sizeof(buf)))
858 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
859 return((*pglob->gl_opendir)(buf));
861 return(opendir(buf));
865 g_lstat(const Char *fn, struct stat *sb, glob_t *pglob)
867 char buf[MAXPATHLEN];
869 if (g_Ctoc(fn, buf, sizeof(buf))) {
870 errno = ENAMETOOLONG;
873 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
874 return((*pglob->gl_lstat)(buf, sb));
875 return(lstat(buf, sb));
879 g_stat(const Char *fn, struct stat *sb, glob_t *pglob)
881 char buf[MAXPATHLEN];
883 if (g_Ctoc(fn, buf, sizeof(buf))) {
884 errno = ENAMETOOLONG;
887 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
888 return((*pglob->gl_stat)(buf, sb));
889 return(stat(buf, sb));
893 g_strchr(const Char *str, wchar_t ch)
904 g_Ctoc(const Char *str, char *buf, size_t len)
909 memset(&mbs, 0, sizeof(mbs));
910 while (len >= MB_CUR_MAX) {
911 clen = wcrtomb(buf, *str, &mbs);
912 if (clen == (size_t)-1)
925 qprintf(const char *str, const Char *s)
929 printf("%s:\n", str);
931 printf("%c", CHAR(*p));
934 printf("%c", *p & M_PROTECT ? '"' : ' ');
937 printf("%c", ismeta(*p) ? '_' : ' ');