2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved
27 * Copyright (c) 1989, 1993
28 * The Regents of the University of California. All rights reserved.
30 * This code is derived from software contributed to Berkeley by
33 * The NEXTSTEP Software License Agreement specifies the terms
34 * and conditions for redistribution.
36 * @(#)glob.c 8.3 (Berkeley) 10/13/93
41 * glob(3) -- a superset of the one defined in POSIX 1003.2.
43 * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
45 * Optional extra services, controlled by flags not defined by POSIX:
48 * Escaping convention: \ inhibits any special meaning the following
49 * character might have (except \ at end of string is retained).
51 * Set in gl_flags if pattern contained a globbing character.
53 * Same as GLOB_NOCHECK, but it will only append pattern if it did
54 * not contain any magic characters. [Used in csh style globbing]
56 * Use alternately specified directory access functions.
58 * expand ~user/foo to the /home/dir/of/user/foo
60 * expand {1,2}{a,b} to 1a 1b 2a 2b
62 * Number of matches in the current invocation of glob.
65 #include <sys/param.h>
90 #define UNDERSCORE '_'
98 #define M_QUOTE 0x8000
99 #define M_PROTECT 0x4000
100 #define M_MASK 0xffff
101 #define M_ASCII 0x00ff
103 typedef u_short Char
;
108 #define M_PROTECT 0x40
117 #define CHAR(c) ((Char)((c)&M_ASCII))
118 #define META(c) ((Char)((c)|M_QUOTE))
119 #define M_ALL META('*')
120 #define M_END META(']')
121 #define M_NOT META('!')
122 #define M_ONE META('?')
123 #define M_RNG META('-')
124 #define M_SET META('[')
125 #define ismeta(c) (((c)&M_QUOTE) != 0)
128 static int compare
__P((const void *, const void *));
129 static void g_Ctoc
__P((const Char
*, char *));
130 static int g_lstat
__P((Char
*, struct stat
*, glob_t
*));
131 static DIR *g_opendir
__P((Char
*, glob_t
*));
132 static Char
*g_strchr
__P((Char
*, int));
134 static Char
*g_strcat
__P((Char
*, const Char
*));
136 static int g_stat
__P((Char
*, struct stat
*, glob_t
*));
137 static int glob0
__P((const Char
*, glob_t
*));
138 static int glob1
__P((Char
*, glob_t
*));
139 static int glob2
__P((Char
*, Char
*, Char
*, glob_t
*));
140 static int glob3
__P((Char
*, Char
*, Char
*, Char
*, glob_t
*));
141 static int globextend
__P((const Char
*, glob_t
*));
142 static const Char
* globtilde
__P((const Char
*, Char
*, glob_t
*));
143 static int globexp1
__P((const Char
*, glob_t
*));
144 static int globexp2
__P((const Char
*, const Char
*, glob_t
*, int *));
145 static int match
__P((Char
*, Char
*, Char
*));
147 static void qprintf
__P((const char *, Char
*));
151 glob(pattern
, flags
, errfunc
, pglob
)
153 int flags
, (*errfunc
) __P((const char *, int));
156 const u_char
*patnext
;
158 Char
*bufnext
, *bufend
, patbuf
[MAXPATHLEN
+1];
160 patnext
= (u_char
*) pattern
;
161 if (!(flags
& GLOB_APPEND
)) {
163 pglob
->gl_pathv
= NULL
;
164 if (!(flags
& GLOB_DOOFFS
))
167 pglob
->gl_flags
= flags
& ~GLOB_MAGCHAR
;
168 pglob
->gl_errfunc
= errfunc
;
169 pglob
->gl_matchc
= 0;
172 bufend
= bufnext
+ MAXPATHLEN
;
173 if (flags
& GLOB_QUOTE
) {
174 /* Protect the quoted characters. */
175 while (bufnext
< bufend
&& (c
= *patnext
++) != EOS
)
177 if ((c
= *patnext
++) == EOS
) {
181 *bufnext
++ = c
| M_PROTECT
;
187 while (bufnext
< bufend
&& (c
= *patnext
++) != EOS
)
191 if (flags
& GLOB_BRACE
)
192 return globexp1(patbuf
, pglob
);
194 return glob0(patbuf
, pglob
);
198 * Expand recursively a glob {} pattern. When there is no more expansion
199 * invoke the standard globbing routine to glob the rest of the magic
202 static int globexp1(pattern
, pglob
)
206 const Char
* ptr
= pattern
;
209 /* Protect a single {}, for find(1), like csh */
210 if (pattern
[0] == LBRACE
&& pattern
[1] == RBRACE
&& pattern
[2] == EOS
)
211 return glob0(pattern
, pglob
);
213 while ((ptr
= (const Char
*) g_strchr((Char
*) ptr
, LBRACE
)) != NULL
)
214 if (!globexp2(ptr
, pattern
, pglob
, &rv
))
217 return glob0(pattern
, pglob
);
222 * Recursive brace globbing helper. Tries to expand a single brace.
223 * If it succeeds then it invokes globexp1 with the new pattern.
224 * If it fails then it tries to glob the rest of the pattern and returns.
226 static int globexp2(ptr
, pattern
, pglob
, rv
)
227 const Char
*ptr
, *pattern
;
233 const Char
*pe
, *pm
, *pl
;
234 Char patbuf
[MAXPATHLEN
+ 1];
236 /* copy part up to the brace */
237 for (lm
= patbuf
, pm
= pattern
; pm
!= ptr
; *lm
++ = *pm
++)
241 /* Find the balanced brace */
242 for (i
= 0, pe
= ++ptr
; *pe
; pe
++)
243 if (*pe
== LBRACKET
) {
244 /* Ignore everything between [] */
245 for (pm
= pe
++; *pe
!= RBRACKET
&& *pe
!= EOS
; pe
++)
249 * We could not find a matching RBRACKET.
250 * Ignore and just look for RBRACE
255 else if (*pe
== LBRACE
)
257 else if (*pe
== RBRACE
) {
263 /* Non matching braces; just glob the pattern */
264 if (i
!= 0 || *pe
== EOS
) {
265 *rv
= glob0(patbuf
, pglob
);
269 for (i
= 0, pl
= pm
= ptr
; pm
<= pe
; pm
++)
272 /* Ignore everything between [] */
273 for (pl
= pm
++; *pm
!= RBRACKET
&& *pm
!= EOS
; pm
++)
277 * We could not find a matching RBRACKET.
278 * Ignore and just look for RBRACE
295 if (i
&& *pm
== COMMA
)
298 /* Append the current string */
299 for (lm
= ls
; (pl
< pm
); *lm
++ = *pl
++)
302 * Append the rest of the pattern after the
305 for (pl
= pe
+ 1; (*lm
++ = *pl
++) != EOS
;)
308 /* Expand the current pattern */
310 qprintf("globexp2:", patbuf
);
312 *rv
= globexp1(patbuf
, pglob
);
314 /* move after the comma, to the next string */
329 * expand tilde from the passwd file.
332 globtilde(pattern
, patbuf
, pglob
)
342 if (*pattern
!= TILDE
|| !(pglob
->gl_flags
& GLOB_TILDE
))
345 /* Copy up to the end of the string or / */
346 for (p
= pattern
+ 1, h
= (char *) patbuf
; *p
&& *p
!= SLASH
;
352 if (((char *) patbuf
)[0] == EOS
) {
354 * handle a plain ~ or ~/ by expanding $HOME
355 * first and then trying the password file
357 if ((h
= getenv("HOME")) == NULL
) {
358 if ((pwd
= getpwuid(getuid())) == NULL
)
368 if ((pwd
= getpwnam((char*) patbuf
)) == NULL
)
374 /* Copy the home directory */
375 for (b
= patbuf
; *h
; *b
++ = *h
++)
378 /* Append the rest of the pattern */
379 while ((*b
++ = *p
++) != EOS
)
387 * The main glob() routine: compiles the pattern (optionally processing
388 * quotes), calls glob1() to do the real pattern matching, and finally
389 * sorts the list (unless unsorted operation is requested). Returns 0
390 * if things went well, nonzero if errors occurred. It is not an error
391 * to find no matches.
394 glob0(pattern
, pglob
)
398 const Char
*qpatnext
;
399 int c
, err
, oldpathc
;
400 Char
*bufnext
, patbuf
[MAXPATHLEN
+1];
402 qpatnext
= globtilde(pattern
, patbuf
, pglob
);
403 oldpathc
= pglob
->gl_pathc
;
406 /* We don't need to check for buffer overflow any more. */
407 while ((c
= *qpatnext
++) != EOS
) {
413 if (*qpatnext
== EOS
||
414 g_strchr((Char
*) qpatnext
+1, RBRACKET
) == NULL
) {
415 *bufnext
++ = LBRACKET
;
425 *bufnext
++ = CHAR(c
);
426 if (*qpatnext
== RANGE
&&
427 (c
= qpatnext
[1]) != RBRACKET
) {
429 *bufnext
++ = CHAR(c
);
432 } while ((c
= *qpatnext
++) != RBRACKET
);
433 pglob
->gl_flags
|= GLOB_MAGCHAR
;
437 pglob
->gl_flags
|= GLOB_MAGCHAR
;
441 pglob
->gl_flags
|= GLOB_MAGCHAR
;
442 /* collapse adjacent stars to one,
443 * to avoid exponential behavior
445 if (bufnext
== patbuf
|| bufnext
[-1] != M_ALL
)
449 *bufnext
++ = CHAR(c
);
455 qprintf("glob0:", patbuf
);
458 if ((err
= glob1(patbuf
, pglob
)) != 0)
462 * If there was no match we are going to append the pattern
463 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
464 * and the pattern did not contain any magic characters
465 * GLOB_NOMAGIC is there just for compatibility with csh.
467 if (pglob
->gl_pathc
== oldpathc
&&
468 ((pglob
->gl_flags
& GLOB_NOCHECK
) ||
469 ((pglob
->gl_flags
& GLOB_NOMAGIC
) &&
470 !(pglob
->gl_flags
& GLOB_MAGCHAR
))))
471 return(globextend(pattern
, pglob
));
472 else if (!(pglob
->gl_flags
& GLOB_NOSORT
))
473 qsort(pglob
->gl_pathv
+ pglob
->gl_offs
+ oldpathc
,
474 pglob
->gl_pathc
- oldpathc
, sizeof(char *), compare
);
482 return(strcmp(*(char **)p
, *(char **)q
));
486 glob1(pattern
, pglob
)
490 Char pathbuf
[MAXPATHLEN
+1];
492 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
495 return(glob2(pathbuf
, pathbuf
, pattern
, pglob
));
499 * The functions glob2 and glob3 are mutually recursive; there is one level
500 * of recursion for each segment in the pattern that contains one or more
504 glob2(pathbuf
, pathend
, pattern
, pglob
)
505 Char
*pathbuf
, *pathend
, *pattern
;
513 * Loop over pattern segments until end of pattern or until
514 * segment with meta character found.
516 for (anymeta
= 0;;) {
517 if (*pattern
== EOS
) { /* End of pattern? */
519 if (g_lstat(pathbuf
, &sb
, pglob
))
522 if (((pglob
->gl_flags
& GLOB_MARK
) &&
523 pathend
[-1] != SEP
) && (S_ISDIR(sb
.st_mode
)
524 || (S_ISLNK(sb
.st_mode
) &&
525 (g_stat(pathbuf
, &sb
, pglob
) == 0) &&
526 S_ISDIR(sb
.st_mode
)))) {
531 return(globextend(pathbuf
, pglob
));
534 /* Find end of next segment, copy tentatively to pathend. */
537 while (*p
!= EOS
&& *p
!= SEP
) {
543 if (!anymeta
) { /* No expansion, do next segment. */
546 while (*pattern
== SEP
)
547 *pathend
++ = *pattern
++;
548 } else /* Need expansion, recurse. */
549 return(glob3(pathbuf
, pathend
, pattern
, p
, pglob
));
555 glob3(pathbuf
, pathend
, pattern
, restpattern
, pglob
)
556 Char
*pathbuf
, *pathend
, *pattern
, *restpattern
;
559 register struct dirent
*dp
;
562 char buf
[MAXPATHLEN
];
565 * The readdirfunc declaration can't be prototyped, because it is
566 * assigned, below, to two functions which are prototyped in glob.h
567 * and dirent.h as taking pointers to differently typed opaque
570 struct dirent
*(*readdirfunc
)();
575 if ((dirp
= g_opendir(pathbuf
, pglob
)) == NULL
) {
576 /* TODO: don't call for ENOENT or ENOTDIR? */
577 if (pglob
->gl_errfunc
) {
578 g_Ctoc(pathbuf
, buf
);
579 if (pglob
->gl_errfunc(buf
, errno
) ||
580 pglob
->gl_flags
& GLOB_ERR
)
588 /* Search directory for matching names. */
589 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
590 readdirfunc
= pglob
->gl_readdir
;
592 readdirfunc
= readdir
;
593 while ((dp
= (*readdirfunc
)(dirp
))) {
597 /* Initial DOT must be matched literally. */
598 if (dp
->d_name
[0] == DOT
&& *pattern
!= DOT
)
600 for (sc
= (u_char
*) dp
->d_name
, dc
= pathend
;
601 (*dc
++ = *sc
++) != EOS
;)
603 if (!match(pathend
, pattern
, restpattern
)) {
607 err
= glob2(pathbuf
, --dc
, restpattern
, pglob
);
612 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
613 (*pglob
->gl_closedir
)(dirp
);
621 * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
622 * add the new item, and update gl_pathc.
624 * This assumes the BSD realloc, which only copies the block when its size
625 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
628 * Return 0 if new item added, error code if memory couldn't be allocated.
630 * Invariant of the glob_t structure:
631 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
632 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
635 globextend(path
, pglob
)
639 register char **pathv
;
645 newsize
= sizeof(*pathv
) * (2 + pglob
->gl_pathc
+ pglob
->gl_offs
);
646 pathv
= pglob
->gl_pathv ?
647 realloc((char *)pglob
->gl_pathv
, newsize
) :
650 return(GLOB_NOSPACE
);
652 if (pglob
->gl_pathv
== NULL
&& pglob
->gl_offs
> 0) {
653 /* first time around -- clear initial gl_offs items */
654 pathv
+= pglob
->gl_offs
;
655 for (i
= pglob
->gl_offs
; --i
>= 0; )
658 pglob
->gl_pathv
= pathv
;
660 for (p
= path
; *p
++;)
662 if ((copy
= malloc(p
- path
)) != NULL
) {
664 pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
++] = copy
;
666 pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
] = NULL
;
667 return(copy
== NULL ? GLOB_NOSPACE
: 0);
672 * pattern matching function for filenames. Each occurrence of the *
673 * pattern causes a recursion level.
676 match(name
, pat
, patend
)
677 register Char
*name
, *pat
, *patend
;
679 int ok
, negate_range
;
682 while (pat
< patend
) {
684 switch (c
& M_MASK
) {
689 if (match(name
, pat
, patend
))
691 while (*name
++ != EOS
);
699 if ((k
= *name
++) == EOS
)
701 if ((negate_range
= ((*pat
& M_MASK
) == M_NOT
)) != EOS
)
703 while (((c
= *pat
++) & M_MASK
) != M_END
)
704 if ((*pat
& M_MASK
) == M_RNG
) {
705 if (c
<= k
&& k
<= pat
[1])
710 if (ok
== negate_range
)
719 return(*name
== EOS
);
722 /* Free allocated data belonging to a glob_t structure. */
730 if (pglob
->gl_pathv
!= NULL
) {
731 pp
= pglob
->gl_pathv
+ pglob
->gl_offs
;
732 for (i
= pglob
->gl_pathc
; i
--; ++pp
)
735 free(pglob
->gl_pathv
);
740 g_opendir(str
, pglob
)
744 char buf
[MAXPATHLEN
];
751 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
752 return((*pglob
->gl_opendir
)(buf
));
754 return(opendir(buf
));
758 g_lstat(fn
, sb
, pglob
)
763 char buf
[MAXPATHLEN
];
766 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
767 return((*pglob
->gl_lstat
)(buf
, sb
));
768 return(lstat(buf
, sb
));
772 g_stat(fn
, sb
, pglob
)
777 char buf
[MAXPATHLEN
];
780 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
781 return((*pglob
->gl_stat
)(buf
, sb
));
782 return(stat(buf
, sb
));
808 while((*dst
++ = *src
++) != EOS
)
817 register const Char
*str
;
822 for (dc
= buf
; (*dc
++ = *str
++) != EOS
;)
834 (void)printf("%s:\n", str
);
836 (void)printf("%c", CHAR(*p
));
839 (void)printf("%c", *p
& M_PROTECT ?
'"' : ' ');
842 (void)printf("%c", ismeta(*p
) ?
'_' : ' ');