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 */
64 #define mblen(_s,_n) mbrlen((_s),(_n),NULL)
75 #define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
78 #if !defined(S_ISLNK) && defined(S_IFLNK)
79 #define S_ISLNK(a) (((a) & S_IFMT) == S_IFLNK)
82 #if !defined(S_ISLNK) && !defined(lstat)
86 typedef unsigned short Char;
88 static int glob1 (Char *, glob_t *, int);
89 static int glob2 (struct strbuf *, const Char *, glob_t *, int);
90 static int glob3 (struct strbuf *, const Char *, const Char *,
91 const Char *, glob_t *, int);
92 static void globextend (const char *, glob_t *);
93 static int match (const char *, const Char *, const Char *,
95 static int compare (const void *, const void *);
96 static DIR *Opendir (const char *);
98 static int Lstat (const char *, struct stat *);
100 static int Stat (const char *, struct stat *sb);
101 static Char *Strchr (Char *, int);
103 static void qprintf (const Char *);
119 #define UNDERSCORE '_'
121 #define M_META 0x8000
122 #define M_PROTECT 0x4000
123 #define M_MASK 0xffff
124 #define M_ASCII 0x00ff
126 #define LCHAR(c) ((c)&M_ASCII)
127 #define META(c) ((c)|M_META)
128 #define M_ALL META('*')
129 #define M_END META(']')
130 #define M_NOT META('!')
131 #define M_ALTNOT META('^')
132 #define M_ONE META('?')
133 #define M_RNG META('-')
134 #define M_SET META('[')
135 #define ismeta(c) (((c)&M_META) != 0)
138 globcharcoll(__Char c1, __Char c2, int cs)
140 #if defined(NLS) && defined(LC_COLLATE) && defined(HAVE_STRCOLL)
141 # if defined(WIDE_STRINGS)
142 wchar_t s1[2], s2[2];
151 /* This should not be here, but I'll rather leave it in than engage in
152 a LC_COLLATE flamewar about a shell I don't use... */
153 if (iswlower(c1) && iswupper(c2))
155 if (iswupper(c1) && iswlower(c2))
161 s1[1] = s2[1] = '\0';
162 return wcscoll(s1, s2);
163 # else /* not WIDE_STRINGS */
169 * From kevin lyda <kevin@suberic.net>:
170 * strcoll does not guarantee case sorting, so we pre-process now:
173 c1 = islower(c1) ? c1 : tolower(c1);
174 c2 = islower(c2) ? c2 : tolower(c2);
176 if (islower(c1) && isupper(c2))
178 if (isupper(c1) && islower(c2))
183 s1[1] = s2[1] = '\0';
184 return strcoll(s1, s2);
192 * Need to dodge two kernel bugs:
193 * opendir("") != opendir(".")
194 * NAMEI_BUG: on plain files trailing slashes are ignored in some kernels.
195 * POSIX specifies that they should be ignored in directories.
199 Opendir(const char *str)
201 #if defined(hpux) || defined(__hpux)
206 return (opendir("."));
207 #if defined(hpux) || defined(__hpux)
209 * Opendir on some device files hangs, so avoid it
211 if (stat(str, &st) == -1 || !S_ISDIR(st.st_mode))
219 Lstat(const char *fn, struct stat *sb)
225 if (*fn != 0 && strend(fn)[-1] == '/' && !S_ISDIR(sb->st_mode))
227 # endif /* NAMEI_BUG */
235 Stat(const char *fn, struct stat *sb)
241 if (*fn != 0 && strend(fn)[-1] == '/' && !S_ISDIR(sb->st_mode))
243 #endif /* NAMEI_BUG */
248 Strchr(Char *str, int ch)
259 qprintf(const Char *s)
264 printf("%c", *p & 0xff);
267 printf("%c", *p & M_PROTECT ? '"' : ' ');
270 printf("%c", *p & M_META ? '_' : ' ');
276 compare(const void *p, const void *q)
278 #if defined(NLS) && defined(HAVE_STRCOLL)
279 return (strcoll(*(char *const *) p, *(char *const *) q));
281 return (strcmp(*(char *const *) p, *(char *const *) q));
282 #endif /* NLS && HAVE_STRCOLL */
286 * The main glob() routine: compiles the pattern (optionally processing
287 * quotes), calls glob1() to do the real pattern matching, and finally
288 * sorts the list (unless unsorted operation is requested). Returns 0
289 * if things went well, nonzero if errors occurred. It is not an error
290 * to find no matches.
293 glob(const char *pattern, int flags, int (*errfunc) (const char *, int),
297 Char *bufnext, m_not;
298 const unsigned char *patnext;
300 Char *qpatnext, *patbuf;
303 patnext = (const unsigned char *) pattern;
304 if (!(flags & GLOB_APPEND)) {
306 pglob->gl_pathv = NULL;
307 if (!(flags & GLOB_DOOFFS))
310 pglob->gl_flags = flags & ~GLOB_MAGCHAR;
311 pglob->gl_errfunc = errfunc;
312 oldpathc = pglob->gl_pathc;
313 pglob->gl_matchc = 0;
315 if (pglob->gl_flags & GLOB_ALTNOT) {
324 patbuf = xmalloc((strlen(pattern) + 1) * sizeof(*patbuf));
327 no_match = *patnext == not;
331 if (flags & GLOB_QUOTE) {
332 /* Protect the quoted characters */
333 while ((c = *patnext++) != EOS) {
337 len = mblen((const char *)(patnext - 1), MB_LEN_MAX);
339 TCSH_IGNORE(mblen(NULL, 0));
341 *bufnext++ = (Char) c;
343 *bufnext++ = (Char) (*patnext++ | M_PROTECT);
345 #endif /* WIDE_STRINGS */
347 if ((c = *patnext++) == EOS) {
351 *bufnext++ = (Char) (c | M_PROTECT);
354 *bufnext++ = (Char) c;
358 while ((c = *patnext++) != EOS)
359 *bufnext++ = (Char) c;
364 while ((c = *qpatnext++) != EOS) {
370 if (*qpatnext == EOS ||
371 Strchr(qpatnext + 1, RBRACKET) == NULL) {
372 *bufnext++ = LBRACKET;
377 pglob->gl_flags |= GLOB_MAGCHAR;
383 *bufnext++ = LCHAR(c);
384 if (*qpatnext == RANGE &&
385 (c = qpatnext[1]) != RBRACKET) {
387 *bufnext++ = LCHAR(c);
390 } while ((c = *qpatnext++) != RBRACKET);
394 pglob->gl_flags |= GLOB_MAGCHAR;
398 pglob->gl_flags |= GLOB_MAGCHAR;
399 /* collapse adjacent stars to one [or three if globstar],
400 * to avoid exponential behavior
402 if (bufnext == patbuf || bufnext[-1] != M_ALL ||
403 ((flags & GLOB_STAR) != 0 &&
404 (bufnext - 1 == patbuf || bufnext[-2] != M_ALL ||
405 bufnext - 2 == patbuf || bufnext[-3] != M_ALL)))
409 *bufnext++ = LCHAR(c);
418 if ((err = glob1(patbuf, pglob, no_match)) != 0) {
424 * If there was no match we are going to append the pattern
425 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
426 * and the pattern did not contain any magic characters
427 * GLOB_NOMAGIC is there just for compatibility with csh.
429 if (pglob->gl_pathc == oldpathc &&
430 ((flags & GLOB_NOCHECK) ||
431 ((flags & GLOB_NOMAGIC) && !(pglob->gl_flags & GLOB_MAGCHAR)))) {
432 if (!(flags & GLOB_QUOTE))
433 globextend(pattern, pglob);
438 /* copy pattern, interpreting quotes */
439 copy = xmalloc(strlen(pattern) + 1);
442 while (*src != EOS) {
443 /* Don't interpret quotes. The spec does not say we should do */
451 globextend(copy, pglob);
457 else if (!(flags & GLOB_NOSORT) && (pglob->gl_pathc != oldpathc))
458 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
459 pglob->gl_pathc - oldpathc, sizeof(char *), compare);
465 glob1(Char *pattern, glob_t *pglob, int no_match)
467 struct strbuf pathbuf = strbuf_INIT;
471 * a null pathname is invalid -- POSIX 1003.1 sect. 2.4.
475 err = glob2(&pathbuf, pattern, pglob, no_match);
481 * functions glob2 and glob3 are mutually recursive; there is one level
482 * of recursion for each segment in the pattern that contains one or
483 * more meta characters.
486 glob2(struct strbuf *pathbuf, const Char *pattern, glob_t *pglob, int no_match)
494 * loop over pattern segments until end of pattern or until segment with
495 * meta character found.
499 if (*pattern == EOS) { /* end of pattern? */
500 strbuf_terminate(pathbuf);
502 if (Lstat(pathbuf->s, &sbuf))
505 if (((pglob->gl_flags & GLOB_MARK) &&
506 pathbuf->s[pathbuf->len - 1] != SEP) &&
507 (S_ISDIR(sbuf.st_mode)
509 || (S_ISLNK(sbuf.st_mode) &&
510 (Stat(pathbuf->s, &sbuf) == 0) &&
511 S_ISDIR(sbuf.st_mode))
514 strbuf_append1(pathbuf, SEP);
515 strbuf_terminate(pathbuf);
518 globextend(pathbuf->s, pglob);
522 /* find end of next segment, tentatively copy to pathbuf */
524 orig_len = pathbuf->len;
525 while (*p != EOS && *p != SEP) {
528 strbuf_append1(pathbuf, *p++);
531 if (!anymeta) { /* no expansion, do next segment */
533 while (*pattern == SEP)
534 strbuf_append1(pathbuf, *pattern++);
536 else { /* need expansion, recurse */
537 pathbuf->len = orig_len;
538 return (glob3(pathbuf, pattern, p, pattern, pglob, no_match));
545 One_Char_mbtowc(__Char *pwc, const Char *s, size_t n)
548 char buf[MB_LEN_MAX], *p;
553 while (p < buf + n && (*p++ = LCHAR(*s++)) != 0)
555 return one_mbtowc(pwc, buf, n);
563 glob3(struct strbuf *pathbuf, const Char *pattern, const Char *restpattern,
564 const Char *pglobstar, glob_t *pglob, int no_match)
570 Char m_not = (pglob->gl_flags & GLOB_ALTNOT) ? M_ALTNOT : M_NOT;
573 int chase_symlinks = 0;
574 const Char *termstar = NULL;
576 strbuf_terminate(pathbuf);
577 orig_len = pathbuf->len;
580 while (pglobstar < restpattern) {
582 size_t width = One_Char_mbtowc(&wc, pglobstar, MB_LEN_MAX);
583 if ((pglobstar[0] & M_MASK) == M_ALL &&
584 (pglobstar[width] & M_MASK) == M_ALL) {
586 chase_symlinks = (pglobstar[2 * width] & M_MASK) == M_ALL;
587 termstar = pglobstar + (2 + chase_symlinks) * width;
594 err = pglobstar==pattern && termstar==restpattern ?
595 *restpattern == EOS ?
596 glob2(pathbuf, restpattern - 1, pglob, no_match) :
597 glob2(pathbuf, restpattern + 1, pglob, no_match) :
598 glob3(pathbuf, pattern, restpattern, termstar, pglob, no_match);
601 pathbuf->len = orig_len;
602 strbuf_terminate(pathbuf);
605 if (*pathbuf->s && (Lstat(pathbuf->s, &sbuf) || !S_ISDIR(sbuf.st_mode)
607 && ((globstar && !chase_symlinks) || !S_ISLNK(sbuf.st_mode))
612 if (!(dirp = Opendir(pathbuf->s))) {
613 /* todo: don't call for ENOENT or ENOTDIR? */
614 if ((pglob->gl_errfunc && (*pglob->gl_errfunc) (pathbuf->s, errno)) ||
615 (pglob->gl_flags & GLOB_ERR))
621 /* search directory for matching names */
622 while ((dp = readdir(dirp)) != NULL) {
623 /* initial DOT must be matched literally */
624 if (dp->d_name[0] == DOT && *pattern != DOT)
625 if (!(pglob->gl_flags & GLOB_DOT) || !dp->d_name[1] ||
626 (dp->d_name[1] == DOT && !dp->d_name[2]))
627 continue; /*unless globdot and not . or .. */
628 pathbuf->len = orig_len;
629 strbuf_append(pathbuf, dp->d_name);
630 strbuf_terminate(pathbuf);
634 if (!chase_symlinks &&
635 (Lstat(pathbuf->s, &sbuf) || S_ISLNK(sbuf.st_mode)))
638 if (match(pathbuf->s + orig_len, pattern, termstar,
639 (int)m_not) == no_match)
641 strbuf_append1(pathbuf, SEP);
642 strbuf_terminate(pathbuf);
643 if ((err = glob2(pathbuf, pglobstar, pglob, no_match)) != 0)
646 if (match(pathbuf->s + orig_len, pattern, restpattern,
647 (int) m_not) == no_match)
649 if ((err = glob2(pathbuf, restpattern, pglob, no_match)) != 0)
653 /* todo: check error from readdir? */
660 * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
661 * add the new item, and update gl_pathc.
663 * This assumes the BSD realloc, which only copies the block when its size
664 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
667 * Return 0 if new item added, error code if memory couldn't be allocated.
669 * Invariant of the glob_t structure:
670 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
671 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
674 globextend(const char *path, glob_t *pglob)
680 newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
681 pathv = xrealloc(pglob->gl_pathv, newsize);
683 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
684 /* first time around -- clear initial gl_offs items */
685 pathv += pglob->gl_offs;
686 for (i = pglob->gl_offs; --i >= 0;)
689 pglob->gl_pathv = pathv;
691 pathv[pglob->gl_offs + pglob->gl_pathc++] = strsave(path);
692 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
696 * pattern matching function for filenames. Each occurrence of the *
697 * pattern causes a recursion level.
700 match(const char *name, const Char *pat, const Char *patend, int m_not)
702 int ok, negate_range;
705 while (pat < patend) {
709 c = *pat; /* Only for M_MASK bits */
710 pat += One_Char_mbtowc(&wc, pat, MB_LEN_MAX);
711 lwk = one_mbtowc(&wk, name, MB_LEN_MAX);
712 switch (c & M_MASK) {
714 while (pat < patend && (*pat & M_MASK) == M_ALL) /* eat consecutive '*' */
715 pat += One_Char_mbtowc(&wc, pat, MB_LEN_MAX);
718 while (!match(name, pat, patend, m_not)) {
722 lwk = one_mbtowc(&wk, name, MB_LEN_MAX);
735 if ((negate_range = ((*pat & M_MASK) == m_not)) != 0)
737 while ((*pat & M_MASK) != M_END) {
738 pat += One_Char_mbtowc(&wc, pat, MB_LEN_MAX);
739 if ((*pat & M_MASK) == M_RNG) {
743 pat += One_Char_mbtowc(&wc2, pat, MB_LEN_MAX);
744 if (globcharcoll(wc, wk, 0) <= 0 &&
745 globcharcoll(wk, wc2, 0) <= 0)
750 pat += One_Char_mbtowc(&wc, pat, MB_LEN_MAX);
751 if (ok == negate_range)
755 if (*name == EOS || samecase(wk) != samecase(wc))
761 return (*name == EOS);
764 /* free allocated data belonging to a glob_t structure */
766 globfree(glob_t *pglob)
771 if (pglob->gl_pathv != NULL) {
772 pp = pglob->gl_pathv + pglob->gl_offs;
773 for (i = pglob->gl_pathc; i--; ++pp)
775 xfree(*pp), *pp = NULL;
776 xfree(pglob->gl_pathv), pglob->gl_pathv = NULL;