2 * Copyright (c) 2008-2010 Todd C. Miller <Todd.Miller@courtesan.com>
3 * Copyright (c) 1989, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)glob.c 8.3 (Berkeley) 10/13/93
37 * glob(3) -- 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 * Set in gl_flags if pattern contained a globbing character.
46 * expand ~user/foo to the /home/dir/of/user/foo
48 * expand {1,2}{a,b} to 1a 1b 2a 2b
50 * Number of matches in the current invocation of glob.
55 #include <sys/param.h>
66 #endif /* STDC_HEADERS */
67 #if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
69 #endif /* HAVE_MALLOC_H && !STDC_HEADERS */
72 #endif /* HAVE_STRING_H */
75 #endif /* HAVE_STRINGS_H */
78 #endif /* HAVE_UNISTD_H */
83 # define dirent direct
84 # ifdef HAVE_SYS_NDIR_H
85 # include <sys/ndir.h>
87 # ifdef HAVE_SYS_DIR_H
99 #include "compat/glob.h"
100 #include "compat/charclass.h"
114 #define UNDERSCORE '_'
122 #define M_QUOTE 0x8000
123 #define M_PROTECT 0x4000
124 #define M_MASK 0xffff
125 #define M_ASCII 0x00ff
127 typedef unsigned short Char;
132 #define M_PROTECT 0x40
141 #define CHAR(c) ((Char)((c)&M_ASCII))
142 #define META(c) ((Char)((c)|M_QUOTE))
143 #define M_ALL META('*')
144 #define M_END META(']')
145 #define M_NOT META('!')
146 #define M_ONE META('?')
147 #define M_RNG META('-')
148 #define M_SET META('[')
149 #define M_CLASS META(':')
150 #define ismeta(c) (((c)&M_QUOTE) != 0)
153 static int compare(const void *, const void *);
154 static int g_Ctoc(const Char *, char *, unsigned int);
155 static int g_lstat(Char *, struct stat *, glob_t *);
156 static DIR *g_opendir(Char *, glob_t *);
157 static Char *g_strchr(const Char *, int);
158 static int g_strncmp(const Char *, const char *, size_t);
159 static int g_stat(Char *, struct stat *, glob_t *);
160 static int glob0(const Char *, glob_t *);
161 static int glob1(Char *, Char *, glob_t *);
162 static int glob2(Char *, Char *, Char *, Char *, Char *, Char *,
164 static int glob3(Char *, Char *, Char *, Char *, Char *, Char *,
165 Char *, Char *, glob_t *);
166 static int globextend(const Char *, glob_t *);
168 globtilde(const Char *, Char *, size_t, glob_t *);
169 static int globexp1(const Char *, glob_t *);
170 static int globexp2(const Char *, const Char *, glob_t *, int *);
171 static int match(Char *, Char *, Char *);
173 static void qprintf(const char *, Char *);
177 rpl_glob(const char *pattern, int flags, int (*errfunc)(const char *, int),
180 const unsigned char *patnext;
182 Char *bufnext, *bufend, patbuf[PATH_MAX];
184 patnext = (unsigned char *) pattern;
185 if (!(flags & GLOB_APPEND)) {
187 pglob->gl_pathv = NULL;
188 if (!(flags & GLOB_DOOFFS))
191 pglob->gl_flags = flags & ~GLOB_MAGCHAR;
192 pglob->gl_errfunc = errfunc;
193 pglob->gl_matchc = 0;
196 bufend = bufnext + PATH_MAX - 1;
197 if (flags & GLOB_NOESCAPE)
198 while (bufnext < bufend && (c = *patnext++) != EOS)
201 /* Protect the quoted characters. */
202 while (bufnext < bufend && (c = *patnext++) != EOS)
204 if ((c = *patnext++) == EOS) {
208 *bufnext++ = c | M_PROTECT;
214 if (flags & GLOB_BRACE)
215 return globexp1(patbuf, pglob);
217 return glob0(patbuf, pglob);
221 * Expand recursively a glob {} pattern. When there is no more expansion
222 * invoke the standard globbing routine to glob the rest of the magic
226 globexp1(const Char *pattern, glob_t *pglob)
228 const Char* ptr = pattern;
231 /* Protect a single {}, for find(1), like csh */
232 if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
233 return glob0(pattern, pglob);
235 while ((ptr = (const Char *) g_strchr(ptr, LBRACE)) != NULL)
236 if (!globexp2(ptr, pattern, pglob, &rv))
239 return glob0(pattern, pglob);
244 * Recursive brace globbing helper. Tries to expand a single brace.
245 * If it succeeds then it invokes globexp1 with the new pattern.
246 * If it fails then it tries to glob the rest of the pattern and returns.
249 globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv)
253 const Char *pe, *pm, *pl;
254 Char patbuf[PATH_MAX];
256 /* copy part up to the brace */
257 for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
262 /* Find the balanced brace */
263 for (i = 0, pe = ++ptr; *pe; pe++)
264 if (*pe == LBRACKET) {
265 /* Ignore everything between [] */
266 for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
270 * We could not find a matching RBRACKET.
271 * Ignore and just look for RBRACE
275 } else if (*pe == LBRACE)
277 else if (*pe == RBRACE) {
283 /* Non matching braces; just glob the pattern */
284 if (i != 0 || *pe == EOS) {
285 *rv = glob0(patbuf, pglob);
289 for (i = 0, pl = pm = ptr; pm <= pe; pm++) {
292 /* Ignore everything between [] */
293 for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++)
297 * We could not find a matching RBRACKET.
298 * Ignore and just look for RBRACE
315 if (i && *pm == COMMA)
318 /* Append the current string */
319 for (lm = ls; (pl < pm); *lm++ = *pl++)
323 * Append the rest of the pattern after the
326 for (pl = pe + 1; (*lm++ = *pl++) != EOS; )
329 /* Expand the current pattern */
331 qprintf("globexp2:", patbuf);
333 *rv = globexp1(patbuf, pglob);
335 /* move after the comma, to the next string */
351 * expand tilde from the passwd file.
354 globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
361 if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
364 /* Copy up to the end of the string or / */
365 eb = &patbuf[patbuf_len - 1];
366 for (p = pattern + 1, h = (char *) patbuf;
367 h < (char *)eb && *p && *p != SLASH; *h++ = *p++)
372 if (((char *) patbuf)[0] == EOS) {
374 * handle a plain ~ or ~/ by expanding $HOME
375 * first and then trying the password file
377 if ((h = getenv("HOME")) == NULL) {
378 if ((pwd = getpwuid(getuid())) == NULL)
387 if ((pwd = getpwnam((char*) patbuf)) == NULL)
393 /* Copy the home directory */
394 for (b = patbuf; b < eb && *h; *b++ = *h++)
397 /* Append the rest of the pattern */
398 while (b < eb && (*b++ = *p++) != EOS)
406 g_strncmp(const Char *s1, const char *s2, size_t n)
411 rv = *(Char *)s1 - *(const unsigned char *)s2++;
421 g_charclass(const Char **patternp, Char **bufnextp)
423 const Char *pattern = *patternp + 1;
424 Char *bufnext = *bufnextp;
429 if ((colon = g_strchr(pattern, ':')) == NULL || colon[1] != ']')
430 return 1; /* not a character class */
432 len = (size_t)(colon - pattern);
433 for (cc = cclasses; cc->name != NULL; cc++) {
434 if (!g_strncmp(pattern, cc->name, len) && cc->name[len] == '\0')
437 if (cc->name == NULL)
438 return -1; /* invalid character class */
439 *bufnext++ = M_CLASS;
440 *bufnext++ = (Char)(cc - &cclasses[0]);
442 *patternp += len + 3;
448 * The main glob() routine: compiles the pattern (optionally processing
449 * quotes), calls glob1() to do the real pattern matching, and finally
450 * sorts the list (unless unsorted operation is requested). Returns 0
451 * if things went well, nonzero if errors occurred. It is not an error
452 * to find no matches.
455 glob0(const Char *pattern, glob_t *pglob)
457 const Char *qpatnext;
458 int c, err, oldpathc;
459 Char *bufnext, patbuf[PATH_MAX];
461 qpatnext = globtilde(pattern, patbuf, PATH_MAX, pglob);
462 oldpathc = pglob->gl_pathc;
465 /* We don't need to check for buffer overflow any more. */
466 while ((c = *qpatnext++) != EOS) {
472 if (*qpatnext == EOS ||
473 g_strchr(qpatnext+1, RBRACKET) == NULL) {
474 *bufnext++ = LBRACKET;
484 if (c == LBRACKET && *qpatnext == ':') {
486 err = g_charclass(&qpatnext,
491 } while (c == LBRACKET && *qpatnext == ':');
493 !(pglob->gl_flags & GLOB_NOCHECK))
498 *bufnext++ = CHAR(c);
499 if (*qpatnext == RANGE &&
500 (c = qpatnext[1]) != RBRACKET) {
502 *bufnext++ = CHAR(c);
505 } while ((c = *qpatnext++) != RBRACKET);
506 pglob->gl_flags |= GLOB_MAGCHAR;
510 pglob->gl_flags |= GLOB_MAGCHAR;
514 pglob->gl_flags |= GLOB_MAGCHAR;
515 /* collapse adjacent stars to one,
516 * to avoid exponential behavior
518 if (bufnext == patbuf || bufnext[-1] != M_ALL)
522 *bufnext++ = CHAR(c);
528 qprintf("glob0:", patbuf);
531 if ((err = glob1(patbuf, patbuf + PATH_MAX - 1, pglob)) != 0)
535 * If there was no match we are going to append the pattern
536 * if GLOB_NOCHECK was specified.
538 if (pglob->gl_pathc == oldpathc) {
539 if (pglob->gl_flags & GLOB_NOCHECK)
540 return globextend(pattern, pglob);
544 if (!(pglob->gl_flags & GLOB_NOSORT))
545 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
546 pglob->gl_pathc - oldpathc, sizeof(char *), compare);
551 compare(const void *p, const void *q)
553 return strcmp(*(char **)p, *(char **)q);
557 glob1(Char *pattern, Char *pattern_last, glob_t *pglob)
559 Char pathbuf[PATH_MAX];
561 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
564 return glob2(pathbuf, pathbuf + PATH_MAX - 1,
565 pathbuf, pathbuf + PATH_MAX - 1,
566 pattern, pattern_last, pglob);
570 * The functions glob2 and glob3 are mutually recursive; there is one level
571 * of recursion for each segment in the pattern that contains one or more
575 glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
576 Char *pattern, Char *pattern_last, glob_t *pglob)
583 * Loop over pattern segments until end of pattern or until
584 * segment with meta character found.
586 for (anymeta = 0;;) {
587 if (*pattern == EOS) { /* End of pattern? */
589 if (g_lstat(pathbuf, &sb, pglob))
592 if (((pglob->gl_flags & GLOB_MARK) &&
593 pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) ||
594 (S_ISLNK(sb.st_mode) &&
595 (g_stat(pathbuf, &sb, pglob) == 0) &&
596 S_ISDIR(sb.st_mode)))) {
597 if (pathend+1 > pathend_last)
603 return globextend(pathbuf, pglob);
606 /* Find end of next segment, copy tentatively to pathend. */
609 while (*p != EOS && *p != SEP) {
612 if (q+1 > pathend_last)
617 if (!anymeta) { /* No expansion, do next segment. */
620 while (*pattern == SEP) {
621 if (pathend+1 > pathend_last)
623 *pathend++ = *pattern++;
626 /* Need expansion, recurse. */
627 return glob3(pathbuf, pathbuf_last, pathend,
628 pathend_last, pattern, pattern_last,
629 p, pattern_last, pglob);
635 glob3(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
636 Char *pattern, Char *pattern_last, Char *restpattern,
637 Char *restpattern_last, glob_t *pglob)
644 if (pathend > pathend_last)
649 if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
650 /* TODO: don't call for ENOENT or ENOTDIR? */
651 if (pglob->gl_errfunc) {
652 if (g_Ctoc(pathbuf, buf, sizeof(buf)))
654 if (pglob->gl_errfunc(buf, errno) ||
655 pglob->gl_flags & GLOB_ERR)
663 /* Search directory for matching names. */
664 while ((dp = readdir(dirp))) {
668 /* Initial DOT must be matched literally. */
669 if (dp->d_name[0] == DOT && *pattern != DOT)
672 sc = (unsigned char *) dp->d_name;
673 while (dc < pathend_last && (*dc++ = *sc++) != EOS)
675 if (dc >= pathend_last) {
681 if (!match(pathend, pattern, restpattern)) {
685 err = glob2(pathbuf, pathbuf_last, --dc, pathend_last,
686 restpattern, restpattern_last, pglob);
696 * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
697 * add the new item, and update gl_pathc.
699 * This assumes the BSD realloc, which only copies the block when its size
700 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
703 * Return 0 if new item added, error code if memory couldn't be allocated.
705 * Invariant of the glob_t structure:
706 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
707 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
710 globextend(const Char *path, glob_t *pglob)
714 unsigned int newsize, len;
718 newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
719 pathv = pglob->gl_pathv ?
720 (char **)realloc((char *)pglob->gl_pathv, newsize) :
721 (char **)malloc(newsize);
723 if (pglob->gl_pathv) {
724 free(pglob->gl_pathv);
725 pglob->gl_pathv = NULL;
730 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
731 /* first time around -- clear initial gl_offs items */
732 pathv += pglob->gl_offs;
733 for (i = pglob->gl_offs; --i >= 0; )
736 pglob->gl_pathv = pathv;
738 for (p = path; *p++;)
740 len = (size_t)(p - path);
741 if ((copy = malloc(len)) != NULL) {
742 if (g_Ctoc(path, copy, len)) {
746 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
748 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
750 return copy == NULL ? GLOB_NOSPACE : 0;
754 * pattern matching function for filenames. Each occurrence of the *
755 * pattern causes a recursion level.
758 match(Char *name, Char *pat, Char *patend)
760 int ok, negate_range;
763 while (pat < patend) {
765 switch (c & M_MASK) {
770 if (match(name, pat, patend))
772 } while (*name++ != EOS);
780 if ((k = *name++) == EOS)
782 if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
784 while (((c = *pat++) & M_MASK) != M_END) {
785 if ((c & M_MASK) == M_CLASS) {
786 int idx = *pat & M_MASK;
787 if (idx < NCCLASSES &&
788 cclasses[idx].isctype(k))
792 if ((*pat & M_MASK) == M_RNG) {
793 if (c <= k && k <= pat[1])
799 if (ok == negate_range)
811 /* Free allocated data belonging to a glob_t structure. */
813 rpl_globfree(glob_t *pglob)
818 if (pglob->gl_pathv != NULL) {
819 pp = pglob->gl_pathv + pglob->gl_offs;
820 for (i = pglob->gl_pathc; i--; ++pp)
823 free(pglob->gl_pathv);
824 pglob->gl_pathv = NULL;
829 g_opendir(Char *str, glob_t *pglob)
837 if (g_Ctoc(str, buf, sizeof(buf)))
844 g_lstat(Char *fn, struct stat *sb, glob_t *pglob)
848 if (g_Ctoc(fn, buf, sizeof(buf)))
850 return lstat(buf, sb);
854 g_stat(Char *fn, struct stat *sb, glob_t *pglob)
858 if (g_Ctoc(fn, buf, sizeof(buf)))
860 return stat(buf, sb);
864 g_strchr(const Char *str, int ch)
874 g_Ctoc(const Char *str, char *buf, unsigned int len)
878 if ((*buf++ = *str++) == EOS)
886 qprintf(const char *str, Char *s)
890 (void)printf("%s:\n", str);
892 (void)printf("%c", CHAR(*p));
895 (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
898 (void)printf("%c", ismeta(*p) ? '_' : ' ');