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 #include <sys/types.h>
56 #include <sys/param.h>
63 #pragma warning(disable:4244)
64 #endif /* WINNT_NATIVE */
79 #define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
82 #if !defined(S_ISLNK) && defined(S_IFLNK)
83 #define S_ISLNK(a) (((a) & S_IFMT) == S_IFLNK)
86 #if !defined(S_ISLNK) && !defined(lstat)
90 typedef unsigned short Char
;
92 static int glob1
__P((Char
*, glob_t
*, int));
93 static int glob2
__P((Char
*, Char
*, Char
*, glob_t
*, int));
94 static int glob3
__P((Char
*, Char
*, Char
*, Char
*,
96 static int globextend
__P((Char
*, glob_t
*));
97 static int match
__P((Char
*, Char
*, Char
*, int));
99 static int compare
__P((const ptr_t
, const ptr_t
));
101 static DIR *Opendir
__P((Char
*));
103 static int Lstat
__P((Char
*, struct stat
*));
105 static int Stat
__P((Char
*, struct stat
*sb
));
106 static Char
*Strchr
__P((Char
*, int));
108 static void qprintf
__P((Char
*));
124 #define UNDERSCORE '_'
126 #define M_META 0x8000
127 #define M_PROTECT 0x4000
128 #define M_MASK 0xffff
129 #define M_ASCII 0x00ff
131 #define CHAR(c) ((c)&M_ASCII)
132 #define META(c) ((c)|M_META)
133 #define M_ALL META('*')
134 #define M_END META(']')
135 #define M_NOT META('!')
136 #define M_ALTNOT META('^')
137 #define M_ONE META('?')
138 #define M_RNG META('-')
139 #define M_SET META('[')
140 #define ismeta(c) (((c)&M_META) != 0)
143 #define GLOBBUFLEN MAXPATHLEN
145 #define GLOBBUFLEN BUFSIZE
152 #if defined(NLS) && defined(LC_COLLATE) && !defined(NOSTRCOLL)
158 * From kevin lyda <kevin@suberic.net>:
159 * strcoll does not guarantee case sorting, so we pre-process now:
161 if (islower(c1
) && isupper(c2
))
165 s1
[1] = s2
[1] = '\0';
166 return strcoll(s1
, s2
);
173 * Need to dodge two kernel bugs:
174 * opendir("") != opendir(".")
175 * NAMEI_BUG: on plain files trailing slashes are ignored in some kernels.
176 * POSIX specifies that they should be ignored in directories.
183 char buf
[GLOBBUFLEN
];
184 register char *dc
= buf
;
185 #if defined(hpux) || defined(__hpux)
190 return (opendir("."));
191 while ((*dc
++ = *str
++) != '\0')
193 #if defined(hpux) || defined(__hpux)
195 * Opendir on some device files hangs, so avoid it
197 if (stat(buf
, &st
) == -1 || !S_ISDIR(st
.st_mode
))
200 return (opendir(buf
));
209 char buf
[GLOBBUFLEN
];
210 register char *dc
= buf
;
212 while ((*dc
++ = *fn
++) != '\0')
221 return (*--dc
== '/' && !S_ISDIR(sb
->st_mode
) ? -1 : st
);
224 return (lstat(buf
, sb
));
225 # endif /* NAMEI_BUG */
236 char buf
[GLOBBUFLEN
];
237 register char *dc
= buf
;
239 while ((*dc
++ = *fn
++) != '\0')
248 return (*--dc
== '/' && !S_ISDIR(sb
->st_mode
) ? -1 : st
);
251 return (stat(buf
, sb
));
252 #endif /* NAMEI_BUG */
275 printf("%c", *p
& 0xff);
278 printf("%c", *p
& M_PROTECT
? '"' : ' ');
281 printf("%c", *p
& M_META
? '_' : ' ');
290 #if defined(NLS) && !defined(NOSTRCOLL)
291 errno
= 0; /* strcoll sets errno, another brain-damage */
293 return (strcoll(*(char **) p
, *(char **) q
));
295 return (strcmp(*(char **) p
, *(char **) q
));
296 #endif /* NLS && !NOSTRCOLL */
300 * The main glob() routine: compiles the pattern (optionally processing
301 * quotes), calls glob1() to do the real pattern matching, and finally
302 * sorts the list (unless unsorted operation is requested). Returns 0
303 * if things went well, nonzero if errors occurred. It is not an error
304 * to find no matches.
307 glob(pattern
, flags
, errfunc
, pglob
)
310 int (*errfunc
) __P((const char *, int));
314 Char
*bufnext
, *bufend
, *compilebuf
, m_not
;
315 const unsigned char *compilepat
, *patnext
;
317 Char patbuf
[GLOBBUFLEN
+ 1], *qpatnext
;
320 patnext
= (unsigned char *) pattern
;
321 if (!(flags
& GLOB_APPEND
)) {
323 pglob
->gl_pathv
= NULL
;
324 if (!(flags
& GLOB_DOOFFS
))
327 pglob
->gl_flags
= flags
& ~GLOB_MAGCHAR
;
328 pglob
->gl_errfunc
= errfunc
;
329 oldpathc
= pglob
->gl_pathc
;
330 pglob
->gl_matchc
= 0;
332 if (pglob
->gl_flags
& GLOB_ALTNOT
) {
342 bufend
= bufnext
+ GLOBBUFLEN
;
343 compilebuf
= bufnext
;
344 compilepat
= patnext
;
346 no_match
= *patnext
== not;
350 if (flags
& GLOB_QUOTE
) {
351 /* Protect the quoted characters */
352 while (bufnext
< bufend
&& (c
= *patnext
++) != EOS
)
354 if (Ismbyte1(c
) && *patnext
!= EOS
)
356 *bufnext
++ = (Char
) c
;
357 *bufnext
++ = (Char
) *patnext
++;
360 #endif /* DSPMBYTE */
362 if ((c
= *patnext
++) == EOS
) {
366 *bufnext
++ = (Char
) (c
| M_PROTECT
);
369 *bufnext
++ = (Char
) c
;
372 while (bufnext
< bufend
&& (c
= *patnext
++) != EOS
)
373 *bufnext
++ = (Char
) c
;
378 /* we don't need to check for buffer overflow any more */
379 while ((c
= *qpatnext
++) != EOS
) {
381 if (Ismbyte1(c
) && *qpatnext
!= EOS
)
383 *bufnext
++ = CHAR(c
);
384 *bufnext
++ = CHAR(*qpatnext
++);
387 #endif /* DSPMBYTE */
393 if (*qpatnext
== EOS
||
394 Strchr(qpatnext
+ 1, RBRACKET
) == NULL
) {
395 *bufnext
++ = LBRACKET
;
400 pglob
->gl_flags
|= GLOB_MAGCHAR
;
406 *bufnext
++ = CHAR(c
);
407 if (*qpatnext
== RANGE
&&
408 (c
= qpatnext
[1]) != RBRACKET
) {
410 *bufnext
++ = CHAR(c
);
413 } while ((c
= *qpatnext
++) != RBRACKET
);
417 pglob
->gl_flags
|= GLOB_MAGCHAR
;
421 pglob
->gl_flags
|= GLOB_MAGCHAR
;
422 /* collapse adjacent stars to one, to avoid
423 * exponential behavior
425 if (bufnext
== patbuf
|| bufnext
[-1] != M_ALL
)
429 *bufnext
++ = CHAR(c
);
438 if ((err
= glob1(patbuf
, pglob
, no_match
)) != 0)
442 * If there was no match we are going to append the pattern
443 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
444 * and the pattern did not contain any magic characters
445 * GLOB_NOMAGIC is there just for compatibility with csh.
447 if (pglob
->gl_pathc
== oldpathc
&&
448 ((flags
& GLOB_NOCHECK
) ||
449 ((flags
& GLOB_NOMAGIC
) && !(pglob
->gl_flags
& GLOB_MAGCHAR
)))) {
450 if (!(flags
& GLOB_QUOTE
)) {
451 Char
*dp
= compilebuf
;
452 const unsigned char *sp
= compilepat
;
454 while ((*dp
++ = *sp
++) != '\0')
459 * copy pattern, interpreting quotes; this is slightly different
460 * than the interpretation of quotes above -- which should prevail?
462 while (*compilepat
!= EOS
) {
463 if (*compilepat
== QUOTE
) {
464 if (*++compilepat
== EOS
)
467 *compilebuf
++ = (unsigned char) *compilepat
++;
471 return (globextend(patbuf
, pglob
));
473 else if (!(flags
& GLOB_NOSORT
) && (pglob
->gl_pathc
!= oldpathc
))
474 qsort((char *) (pglob
->gl_pathv
+ pglob
->gl_offs
+ oldpathc
),
475 pglob
->gl_pathc
- oldpathc
, sizeof(char *),
476 (int (*) __P((const void *, const void *))) compare
);
481 glob1(pattern
, pglob
, no_match
)
486 Char pathbuf
[GLOBBUFLEN
+ 1];
489 * a null pathname is invalid -- POSIX 1003.1 sect. 2.4.
493 return (glob2(pathbuf
, pathbuf
, pattern
, pglob
, no_match
));
497 * functions glob2 and glob3 are mutually recursive; there is one level
498 * of recursion for each segment in the pattern that contains one or
499 * more meta characters.
502 glob2(pathbuf
, pathend
, pattern
, pglob
, no_match
)
503 Char
*pathbuf
, *pathend
, *pattern
;
512 * loop over pattern segments until end of pattern or until segment with
513 * meta character found.
517 if (*pattern
== EOS
) { /* end of pattern? */
520 if (Lstat(pathbuf
, &sbuf
))
523 if (((pglob
->gl_flags
& GLOB_MARK
) &&
524 pathend
[-1] != SEP
) &&
525 (S_ISDIR(sbuf
.st_mode
)
527 || (S_ISLNK(sbuf
.st_mode
) &&
528 (Stat(pathbuf
, &sbuf
) == 0) &&
529 S_ISDIR(sbuf
.st_mode
))
536 return (globextend(pathbuf
, pglob
));
539 /* find end of next segment, copy tentatively to pathend */
542 while (*p
!= EOS
&& *p
!= SEP
) {
548 if (!anymeta
) { /* no expansion, do next segment */
551 while (*pattern
== SEP
)
552 *pathend
++ = *pattern
++;
554 else /* need expansion, recurse */
555 return (glob3(pathbuf
, pathend
, pattern
, p
, pglob
, no_match
));
562 glob3(pathbuf
, pathend
, pattern
, restpattern
, pglob
, no_match
)
563 Char
*pathbuf
, *pathend
, *pattern
, *restpattern
;
570 Char m_not
= (pglob
->gl_flags
& GLOB_ALTNOT
) ? M_ALTNOT
: M_NOT
;
571 char cpathbuf
[GLOBBUFLEN
], *ptr
;;
576 if (!(dirp
= Opendir(pathbuf
))) {
577 /* todo: don't call for ENOENT or ENOTDIR? */
578 for (ptr
= cpathbuf
; (*ptr
++ = (char) *pathbuf
++) != EOS
;)
580 if ((pglob
->gl_errfunc
&& (*pglob
->gl_errfunc
) (cpathbuf
, errno
)) ||
581 (pglob
->gl_flags
& GLOB_ERR
))
589 /* search directory for matching names */
590 while ((dp
= readdir(dirp
)) != NULL
) {
591 register unsigned char *sc
;
594 /* initial DOT must be matched literally */
595 if (dp
->d_name
[0] == DOT
&& *pattern
!= DOT
)
597 for (sc
= (unsigned char *) dp
->d_name
, dc
= pathend
;
598 (*dc
++ = *sc
++) != '\0';)
600 if (match(pathend
, pattern
, restpattern
, (int) m_not
) == no_match
) {
604 err
= glob2(pathbuf
, --dc
, restpattern
, pglob
, no_match
);
608 /* todo: check error from readdir? */
609 (void) closedir(dirp
);
615 * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
616 * add the new item, and update gl_pathc.
618 * This assumes the BSD realloc, which only copies the block when its size
619 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
622 * Return 0 if new item added, error code if memory couldn't be allocated.
624 * Invariant of the glob_t structure:
625 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
626 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
629 globextend(path
, pglob
)
633 register char **pathv
;
635 unsigned int newsize
;
639 newsize
= sizeof(*pathv
) * (2 + pglob
->gl_pathc
+ pglob
->gl_offs
);
640 pathv
= (char **) (pglob
->gl_pathv
?
641 xrealloc((ptr_t
) pglob
->gl_pathv
, (size_t) newsize
) :
642 xmalloc((size_t) newsize
));
644 return (GLOB_NOSPACE
);
646 if (pglob
->gl_pathv
== NULL
&& pglob
->gl_offs
> 0) {
647 /* first time around -- clear initial gl_offs items */
648 pathv
+= pglob
->gl_offs
;
649 for (i
= pglob
->gl_offs
; --i
>= 0;)
652 pglob
->gl_pathv
= pathv
;
654 for (p
= path
; *p
++;)
656 if ((copy
= (char *) xmalloc((size_t) (p
- path
))) != NULL
) {
657 register char *dc
= copy
;
658 register Char
*sc
= path
;
660 while ((*dc
++ = *sc
++) != '\0')
662 pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
++] = copy
;
664 pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
] = NULL
;
665 return ((copy
== NULL
) ? GLOB_NOSPACE
: 0);
670 * pattern matching function for filenames. Each occurrence of the *
671 * pattern causes a recursion level.
674 match(name
, pat
, patend
, m_not
)
675 register Char
*name
, *pat
, *patend
;
678 int ok
, negate_range
;
681 while (pat
< patend
) {
683 switch (c
& M_MASK
) {
688 if (match(name
, pat
, patend
, m_not
))
690 while (*name
++ != EOS
);
698 if ((k
= *name
++) == EOS
)
700 if ((negate_range
= ((*pat
& M_MASK
) == m_not
)) != 0)
702 while (((c
= *pat
++) & M_MASK
) != M_END
) {
703 if ((*pat
& M_MASK
) == M_RNG
) {
704 if (globcharcoll(CHAR(c
), CHAR(k
)) <= 0 &&
705 globcharcoll(CHAR(k
), CHAR(pat
[1])) <= 0)
712 if (ok
== negate_range
)
717 if (samecase(k
) != samecase(c
))
722 return (*name
== EOS
);
725 /* free allocated data belonging to a glob_t structure */
733 if (pglob
->gl_pathv
!= NULL
) {
734 pp
= pglob
->gl_pathv
+ pglob
->gl_offs
;
735 for (i
= pglob
->gl_pathc
; i
--; ++pp
)
737 xfree((ptr_t
) *pp
), *pp
= NULL
;
738 xfree((ptr_t
) pglob
->gl_pathv
), pglob
->gl_pathv
= NULL
;