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 * Copyright (c) 2011 The FreeBSD Foundation
10 * Portions of this software were developed by David Chisnall
11 * under sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93";
40 #endif /* LIBC_SCCS and not lint */
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
45 * glob(3) -- a superset of the one defined in POSIX 1003.2.
47 * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
49 * Optional extra services, controlled by flags not defined by POSIX:
52 * Escaping convention: \ inhibits any special meaning the following
53 * character might have (except \ at end of string is retained).
55 * Set in gl_flags if pattern contained a globbing character.
57 * Same as GLOB_NOCHECK, but it will only append pattern if it did
58 * not contain any magic characters. [Used in csh style globbing]
60 * Use alternately specified directory access functions.
62 * expand ~user/foo to the /home/dir/of/user/foo
64 * expand {1,2}{a,b} to 1a 1b 2a 2b
66 * Number of matches in the current invocation of glob.
70 * Some notes on multibyte character support:
71 * 1. Patterns with illegal byte sequences match nothing - even if
72 * GLOB_NOCHECK is specified.
73 * 2. Illegal byte sequences in filenames are handled by treating them as
74 * single-byte characters with a value of the first byte of the sequence
76 * 3. State-dependent encodings are not currently supported.
79 #include <sys/param.h>
98 * glob(3) expansion limits. Stop the expansion if any of these limits
99 * is reached. This caps the runtime in the face of DoS attacks. See
102 #define GLOB_LIMIT_BRACE 128 /* number of brace calls */
103 #define GLOB_LIMIT_PATH 65536 /* number of path elements */
104 #define GLOB_LIMIT_READDIR 16384 /* number of readdirs */
105 #define GLOB_LIMIT_STAT 1024 /* number of stat system calls */
106 #define GLOB_LIMIT_STRING ARG_MAX /* maximum total size for paths */
111 size_t l_readdir_cnt;
128 #define UNDERSCORE '_'
136 #define M_QUOTE 0x8000000000ULL
137 #define M_PROTECT 0x4000000000ULL
138 #define M_MASK 0xffffffffffULL
139 #define M_CHAR 0x00ffffffffULL
141 typedef uint_fast64_t Char;
146 #define M_PROTECT 0x40
155 #define CHAR(c) ((Char)((c)&M_CHAR))
156 #define META(c) ((Char)((c)|M_QUOTE))
157 #define M_ALL META('*')
158 #define M_END META(']')
159 #define M_NOT META('!')
160 #define M_ONE META('?')
161 #define M_RNG META('-')
162 #define M_SET META('[')
163 #define ismeta(c) (((c)&M_QUOTE) != 0)
166 static int compare(const void *, const void *);
167 static int g_Ctoc(const Char *, char *, size_t);
168 static int g_lstat(Char *, struct stat *, glob_t *);
169 static DIR *g_opendir(Char *, glob_t *);
170 static const Char *g_strchr(const Char *, wchar_t);
172 static Char *g_strcat(Char *, const Char *);
174 static int g_stat(Char *, struct stat *, glob_t *);
175 static int glob0(const Char *, glob_t *, struct glob_limit *);
176 static int glob1(Char *, glob_t *, struct glob_limit *);
177 static int glob2(Char *, Char *, Char *, Char *, glob_t *,
178 struct glob_limit *);
179 static int glob3(Char *, Char *, Char *, Char *, Char *, glob_t *,
180 struct glob_limit *);
181 static int globextend(const Char *, glob_t *, struct glob_limit *);
183 globtilde(const Char *, Char *, size_t, glob_t *);
184 static int globexp1(const Char *, glob_t *, struct glob_limit *);
185 static int globexp2(const Char *, const Char *, glob_t *, int *,
186 struct glob_limit *);
187 static int match(Char *, Char *, Char *);
189 static void qprintf(const char *, Char *);
193 glob(const char * __restrict pattern, int flags,
194 int (*errfunc)(const char *, int), glob_t * __restrict pglob)
196 struct glob_limit limit = { 0, 0, 0, 0, 0 };
198 Char *bufnext, *bufend, patbuf[MAXPATHLEN], prot;
204 if (!(flags & GLOB_APPEND)) {
206 pglob->gl_pathv = NULL;
207 if (!(flags & GLOB_DOOFFS))
210 if (flags & GLOB_LIMIT) {
211 limit.l_path_lim = pglob->gl_matchc;
212 if (limit.l_path_lim == 0)
213 limit.l_path_lim = GLOB_LIMIT_PATH;
215 pglob->gl_flags = flags & ~GLOB_MAGCHAR;
216 pglob->gl_errfunc = errfunc;
217 pglob->gl_matchc = 0;
220 bufend = bufnext + MAXPATHLEN - 1;
221 if (flags & GLOB_NOESCAPE) {
222 memset(&mbs, 0, sizeof(mbs));
223 while (bufend - bufnext >= MB_CUR_MAX) {
224 clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
225 if (clen == (size_t)-1 || clen == (size_t)-2)
226 return (GLOB_NOMATCH);
233 /* Protect the quoted characters. */
234 memset(&mbs, 0, sizeof(mbs));
235 while (bufend - bufnext >= MB_CUR_MAX) {
236 if (*patnext == QUOTE) {
237 if (*++patnext == EOS) {
238 *bufnext++ = QUOTE | M_PROTECT;
244 clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
245 if (clen == (size_t)-1 || clen == (size_t)-2)
246 return (GLOB_NOMATCH);
249 *bufnext++ = wc | prot;
255 if (flags & GLOB_BRACE)
256 return (globexp1(patbuf, pglob, &limit));
258 return (glob0(patbuf, pglob, &limit));
262 * Expand recursively a glob {} pattern. When there is no more expansion
263 * invoke the standard globbing routine to glob the rest of the magic
267 globexp1(const Char *pattern, glob_t *pglob, struct glob_limit *limit)
269 const Char* ptr = pattern;
272 if ((pglob->gl_flags & GLOB_LIMIT) &&
273 limit->l_brace_cnt++ >= GLOB_LIMIT_BRACE) {
275 return (GLOB_NOSPACE);
278 /* Protect a single {}, for find(1), like csh */
279 if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
280 return glob0(pattern, pglob, limit);
282 while ((ptr = g_strchr(ptr, LBRACE)) != NULL)
283 if (!globexp2(ptr, pattern, pglob, &rv, limit))
286 return glob0(pattern, pglob, limit);
291 * Recursive brace globbing helper. Tries to expand a single brace.
292 * If it succeeds then it invokes globexp1 with the new pattern.
293 * If it fails then it tries to glob the rest of the pattern and returns.
296 globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv,
297 struct glob_limit *limit)
301 const Char *pe, *pm, *pm1, *pl;
302 Char patbuf[MAXPATHLEN];
304 /* copy part up to the brace */
305 for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
310 /* Find the balanced brace */
311 for (i = 0, pe = ++ptr; *pe; pe++)
312 if (*pe == LBRACKET) {
313 /* Ignore everything between [] */
314 for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
318 * We could not find a matching RBRACKET.
319 * Ignore and just look for RBRACE
324 else if (*pe == LBRACE)
326 else if (*pe == RBRACE) {
332 /* Non matching braces; just glob the pattern */
333 if (i != 0 || *pe == EOS) {
334 *rv = glob0(patbuf, pglob, limit);
338 for (i = 0, pl = pm = ptr; pm <= pe; pm++)
341 /* Ignore everything between [] */
342 for (pm1 = pm++; *pm != RBRACKET && *pm != EOS; pm++)
346 * We could not find a matching RBRACKET.
347 * Ignore and just look for RBRACE
364 if (i && *pm == COMMA)
367 /* Append the current string */
368 for (lm = ls; (pl < pm); *lm++ = *pl++)
371 * Append the rest of the pattern after the
374 for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
377 /* Expand the current pattern */
379 qprintf("globexp2:", patbuf);
381 *rv = globexp1(patbuf, pglob, limit);
383 /* move after the comma, to the next string */
398 * expand tilde from the passwd file.
401 globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
408 if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
412 * Copy up to the end of the string or /
414 eb = &patbuf[patbuf_len - 1];
415 for (p = pattern + 1, h = (char *) patbuf;
416 h < (char *)eb && *p && *p != SLASH; *h++ = *p++)
421 if (((char *) patbuf)[0] == EOS) {
423 * handle a plain ~ or ~/ by expanding $HOME first (iff
424 * we're not running setuid or setgid) and then trying
427 if (issetugid() != 0 ||
428 (h = getenv("HOME")) == NULL) {
429 if (((h = getlogin()) != NULL &&
430 (pwd = getpwnam(h)) != NULL) ||
431 (pwd = getpwuid(getuid())) != NULL)
441 if ((pwd = getpwnam((char*) patbuf)) == NULL)
447 /* Copy the home directory */
448 for (b = patbuf; b < eb && *h; *b++ = *h++)
451 /* Append the rest of the pattern */
452 while (b < eb && (*b++ = *p++) != EOS)
461 * The main glob() routine: compiles the pattern (optionally processing
462 * quotes), calls glob1() to do the real pattern matching, and finally
463 * sorts the list (unless unsorted operation is requested). Returns 0
464 * if things went well, nonzero if errors occurred.
467 glob0(const Char *pattern, glob_t *pglob, struct glob_limit *limit)
469 const Char *qpatnext;
472 Char *bufnext, c, patbuf[MAXPATHLEN];
474 qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
475 oldpathc = pglob->gl_pathc;
478 /* We don't need to check for buffer overflow any more. */
479 while ((c = *qpatnext++) != EOS) {
485 if (*qpatnext == EOS ||
486 g_strchr(qpatnext+1, RBRACKET) == NULL) {
487 *bufnext++ = LBRACKET;
497 *bufnext++ = CHAR(c);
498 if (*qpatnext == RANGE &&
499 (c = qpatnext[1]) != RBRACKET) {
501 *bufnext++ = CHAR(c);
504 } while ((c = *qpatnext++) != RBRACKET);
505 pglob->gl_flags |= GLOB_MAGCHAR;
509 pglob->gl_flags |= GLOB_MAGCHAR;
513 pglob->gl_flags |= GLOB_MAGCHAR;
514 /* collapse adjacent stars to one,
515 * to avoid exponential behavior
517 if (bufnext == patbuf || bufnext[-1] != M_ALL)
521 *bufnext++ = CHAR(c);
527 qprintf("glob0:", patbuf);
530 if ((err = glob1(patbuf, pglob, limit)) != 0)
534 * If there was no match we are going to append the pattern
535 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
536 * and the pattern did not contain any magic characters
537 * GLOB_NOMAGIC is there just for compatibility with csh.
539 if (pglob->gl_pathc == oldpathc) {
540 if (((pglob->gl_flags & GLOB_NOCHECK) ||
541 ((pglob->gl_flags & GLOB_NOMAGIC) &&
542 !(pglob->gl_flags & GLOB_MAGCHAR))))
543 return (globextend(pattern, pglob, limit));
545 return (GLOB_NOMATCH);
547 if (!(pglob->gl_flags & GLOB_NOSORT))
548 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
549 pglob->gl_pathc - oldpathc, sizeof(char *), compare);
554 compare(const void *p, const void *q)
556 return (strcmp(*(char **)p, *(char **)q));
560 glob1(Char *pattern, glob_t *pglob, struct glob_limit *limit)
562 Char pathbuf[MAXPATHLEN];
564 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
567 return (glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1,
568 pattern, pglob, limit));
572 * The functions glob2 and glob3 are mutually recursive; there is one level
573 * of recursion for each segment in the pattern that contains one or more
577 glob2(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern,
578 glob_t *pglob, struct glob_limit *limit)
585 * Loop over pattern segments until end of pattern or until
586 * segment with meta character found.
588 for (anymeta = 0;;) {
589 if (*pattern == EOS) { /* End of pattern? */
591 if (g_lstat(pathbuf, &sb, pglob))
594 if ((pglob->gl_flags & GLOB_LIMIT) &&
595 limit->l_stat_cnt++ >= GLOB_LIMIT_STAT) {
597 if (pathend + 1 > pathend_last)
598 return (GLOB_ABORTED);
601 return (GLOB_NOSPACE);
603 if (((pglob->gl_flags & GLOB_MARK) &&
604 pathend[-1] != SEP) && (S_ISDIR(sb.st_mode)
605 || (S_ISLNK(sb.st_mode) &&
606 (g_stat(pathbuf, &sb, pglob) == 0) &&
607 S_ISDIR(sb.st_mode)))) {
608 if (pathend + 1 > pathend_last)
609 return (GLOB_ABORTED);
614 return (globextend(pathbuf, pglob, limit));
617 /* Find end of next segment, copy tentatively to pathend. */
620 while (*p != EOS && *p != SEP) {
623 if (q + 1 > pathend_last)
624 return (GLOB_ABORTED);
628 if (!anymeta) { /* No expansion, do next segment. */
631 while (*pattern == SEP) {
632 if (pathend + 1 > pathend_last)
633 return (GLOB_ABORTED);
634 *pathend++ = *pattern++;
636 } else /* Need expansion, recurse. */
637 return (glob3(pathbuf, pathend, pathend_last, pattern,
644 glob3(Char *pathbuf, Char *pathend, Char *pathend_last,
645 Char *pattern, Char *restpattern,
646 glob_t *pglob, struct glob_limit *limit)
651 char buf[MAXPATHLEN];
654 * The readdirfunc declaration can't be prototyped, because it is
655 * assigned, below, to two functions which are prototyped in glob.h
656 * and dirent.h as taking pointers to differently typed opaque
659 struct dirent *(*readdirfunc)();
661 if (pathend > pathend_last)
662 return (GLOB_ABORTED);
666 if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
667 /* TODO: don't call for ENOENT or ENOTDIR? */
668 if (pglob->gl_errfunc) {
669 if (g_Ctoc(pathbuf, buf, sizeof(buf)))
670 return (GLOB_ABORTED);
671 if (pglob->gl_errfunc(buf, errno) ||
672 pglob->gl_flags & GLOB_ERR)
673 return (GLOB_ABORTED);
680 /* Search directory for matching names. */
681 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
682 readdirfunc = pglob->gl_readdir;
684 readdirfunc = readdir;
685 while ((dp = (*readdirfunc)(dirp))) {
692 if ((pglob->gl_flags & GLOB_LIMIT) &&
693 limit->l_readdir_cnt++ >= GLOB_LIMIT_READDIR) {
695 if (pathend + 1 > pathend_last)
705 /* Initial DOT must be matched literally. */
706 if (dp->d_name[0] == DOT && *pattern != DOT)
708 memset(&mbs, 0, sizeof(mbs));
711 while (dc < pathend_last) {
712 clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
713 if (clen == (size_t)-1 || clen == (size_t)-2) {
716 memset(&mbs, 0, sizeof(mbs));
718 if ((*dc++ = wc) == EOS)
722 if (!match(pathend, pattern, restpattern)) {
726 err = glob2(pathbuf, --dc, pathend_last, restpattern,
732 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
733 (*pglob->gl_closedir)(dirp);
741 * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
742 * add the new item, and update gl_pathc.
744 * This assumes the BSD realloc, which only copies the block when its size
745 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
748 * Return 0 if new item added, error code if memory couldn't be allocated.
750 * Invariant of the glob_t structure:
751 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
752 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
755 globextend(const Char *path, glob_t *pglob, struct glob_limit *limit)
758 size_t i, newsize, len;
762 if ((pglob->gl_flags & GLOB_LIMIT) &&
763 pglob->gl_matchc > limit->l_path_lim) {
765 return (GLOB_NOSPACE);
768 newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
769 /* realloc(NULL, newsize) is equivalent to malloc(newsize). */
770 pathv = realloc((void *)pglob->gl_pathv, newsize);
772 return (GLOB_NOSPACE);
774 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
775 /* first time around -- clear initial gl_offs items */
776 pathv += pglob->gl_offs;
777 for (i = pglob->gl_offs + 1; --i > 0; )
780 pglob->gl_pathv = pathv;
782 for (p = path; *p++;)
784 len = MB_CUR_MAX * (size_t)(p - path); /* XXX overallocation */
785 limit->l_string_cnt += len;
786 if ((pglob->gl_flags & GLOB_LIMIT) &&
787 limit->l_string_cnt >= GLOB_LIMIT_STRING) {
789 return (GLOB_NOSPACE);
791 if ((copy = malloc(len)) != NULL) {
792 if (g_Ctoc(path, copy, len)) {
794 return (GLOB_NOSPACE);
796 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
798 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
799 return (copy == NULL ? GLOB_NOSPACE : 0);
803 * pattern matching function for filenames. Each occurrence of the *
804 * pattern causes a recursion level.
807 match(Char *name, Char *pat, Char *patend)
809 int ok, negate_range;
811 struct xlocale_collate *table =
812 (struct xlocale_collate*)__get_locale()->components[XLC_COLLATE];
814 while (pat < patend) {
816 switch (c & M_MASK) {
821 if (match(name, pat, patend))
823 while (*name++ != EOS);
831 if ((k = *name++) == EOS)
833 if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
835 while (((c = *pat++) & M_MASK) != M_END)
836 if ((*pat & M_MASK) == M_RNG) {
837 if (table->__collate_load_error ?
838 CHAR(c) <= CHAR(k) && CHAR(k) <= CHAR(pat[1]) :
839 __collate_range_cmp(table, CHAR(c), CHAR(k)) <= 0
840 && __collate_range_cmp(table, CHAR(k), CHAR(pat[1])) <= 0
846 if (ok == negate_range)
855 return (*name == EOS);
858 /* Free allocated data belonging to a glob_t structure. */
860 globfree(glob_t *pglob)
865 if (pglob->gl_pathv != NULL) {
866 pp = pglob->gl_pathv + pglob->gl_offs;
867 for (i = pglob->gl_pathc; i--; ++pp)
870 free(pglob->gl_pathv);
871 pglob->gl_pathv = NULL;
876 g_opendir(Char *str, glob_t *pglob)
878 char buf[MAXPATHLEN];
883 if (g_Ctoc(str, buf, sizeof(buf)))
887 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
888 return ((*pglob->gl_opendir)(buf));
890 return (opendir(buf));
894 g_lstat(Char *fn, struct stat *sb, glob_t *pglob)
896 char buf[MAXPATHLEN];
898 if (g_Ctoc(fn, buf, sizeof(buf))) {
899 errno = ENAMETOOLONG;
902 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
903 return((*pglob->gl_lstat)(buf, sb));
904 return (lstat(buf, sb));
908 g_stat(Char *fn, struct stat *sb, glob_t *pglob)
910 char buf[MAXPATHLEN];
912 if (g_Ctoc(fn, buf, sizeof(buf))) {
913 errno = ENAMETOOLONG;
916 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
917 return ((*pglob->gl_stat)(buf, sb));
918 return (stat(buf, sb));
922 g_strchr(const Char *str, wchar_t ch)
933 g_Ctoc(const Char *str, char *buf, size_t len)
938 memset(&mbs, 0, sizeof(mbs));
939 while (len >= MB_CUR_MAX) {
940 clen = wcrtomb(buf, *str, &mbs);
941 if (clen == (size_t)-1)
954 qprintf(const char *str, Char *s)
958 (void)printf("%s:\n", str);
960 (void)printf("%c", CHAR(*p));
963 (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
966 (void)printf("%c", ismeta(*p) ? '_' : ' ');