]>
git.saurik.com Git - apple/libinfo.git/blob - util.subproj/glob.c
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
40 * glob(3) -- a superset of the one defined in POSIX 1003.2.
42 * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
44 * Optional extra services, controlled by flags not defined by POSIX:
47 * Escaping convention: \ inhibits any special meaning the following
48 * character might have (except \ at end of string is retained).
50 * Set in gl_flags if pattern contained a globbing character.
52 * Same as GLOB_NOCHECK, but it will only append pattern if it did
53 * not contain any magic characters. [Used in csh style globbing]
55 * Use alternately specified directory access functions.
57 * expand ~user/foo to the /home/dir/of/user/foo
59 * expand {1,2}{a,b} to 1a 1b 2a 2b
61 * Number of matches in the current invocation of glob.
64 #include <sys/param.h>
89 #define UNDERSCORE '_'
97 #define M_QUOTE 0x8000
98 #define M_PROTECT 0x4000
100 #define M_ASCII 0x00ff
102 typedef u_short Char
;
107 #define M_PROTECT 0x40
116 #define CHAR(c) ((Char)((c)&M_ASCII))
117 #define META(c) ((Char)((c)|M_QUOTE))
118 #define M_ALL META('*')
119 #define M_END META(']')
120 #define M_NOT META('!')
121 #define M_ONE META('?')
122 #define M_RNG META('-')
123 #define M_SET META('[')
124 #define ismeta(c) (((c)&M_QUOTE) != 0)
127 static int compare
__P((const void *, const void *));
128 static int g_Ctoc
__P((const Char
*, char *, u_int
));
129 static int g_lstat
__P((Char
*, struct stat
*, glob_t
*));
130 static DIR *g_opendir
__P((Char
*, glob_t
*));
131 static Char
*g_strchr
__P((Char
*, int));
133 static Char
*g_strcat
__P((Char
*, const Char
*));
135 static int g_stat
__P((Char
*, struct stat
*, glob_t
*));
136 static int glob0
__P((const Char
*, glob_t
*, int *));
137 static int glob1
__P((Char
*, glob_t
*, int *));
138 static int glob2
__P((Char
*, Char
*, Char
*, Char
*, glob_t
*, int *));
139 static int glob3
__P((Char
*, Char
*, Char
*, Char
*, Char
*, glob_t
*, int *));
140 static int globextend
__P((const Char
*, glob_t
*, int *));
142 globtilde
__P((const Char
*, Char
*, size_t, glob_t
*));
143 static int globexp1
__P((const Char
*, glob_t
*, int *));
144 static int globexp2
__P((const Char
*, const Char
*, glob_t
*, int *, 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
];
160 patnext
= (u_char
*) pattern
;
161 if (!(flags
& GLOB_APPEND
)) {
163 pglob
->gl_pathv
= NULL
;
164 if (!(flags
& GLOB_DOOFFS
))
167 #if !defined(GLOB_MAXPATH)
168 #define GLOB_MAXPATH 0x1000
170 if (flags
& GLOB_MAXPATH
)
171 limit
= pglob
->gl_matchc
;
174 pglob
->gl_flags
= flags
& ~GLOB_MAGCHAR
;
175 pglob
->gl_errfunc
= errfunc
;
176 pglob
->gl_matchc
= 0;
179 bufend
= bufnext
+ MAXPATHLEN
- 1;
180 if (flags
& GLOB_QUOTE
) {
181 /* Protect the quoted characters. */
182 while (bufnext
< bufend
&& (c
= *patnext
++) != EOS
)
184 if ((c
= *patnext
++) == EOS
) {
188 *bufnext
++ = c
| M_PROTECT
;
194 while (bufnext
< bufend
&& (c
= *patnext
++) != EOS
)
198 if (flags
& GLOB_BRACE
)
199 return globexp1(patbuf
, pglob
, &limit
);
201 return glob0(patbuf
, pglob
, &limit
);
205 * Expand recursively a glob {} pattern. When there is no more expansion
206 * invoke the standard globbing routine to glob the rest of the magic
210 globexp1(pattern
, pglob
, limit
)
215 const Char
* ptr
= pattern
;
218 /* Protect a single {}, for find(1), like csh */
219 if (pattern
[0] == LBRACE
&& pattern
[1] == RBRACE
&& pattern
[2] == EOS
)
220 return glob0(pattern
, pglob
, limit
);
222 while ((ptr
= (const Char
*) g_strchr((Char
*) ptr
, LBRACE
)) != NULL
)
223 if (!globexp2(ptr
, pattern
, pglob
, &rv
, limit
))
226 return glob0(pattern
, pglob
, limit
);
231 * Recursive brace globbing helper. Tries to expand a single brace.
232 * If it succeeds then it invokes globexp1 with the new pattern.
233 * If it fails then it tries to glob the rest of the pattern and returns.
236 globexp2(ptr
, pattern
, pglob
, rv
, limit
)
237 const Char
*ptr
, *pattern
;
243 const Char
*pe
, *pm
, *pl
;
244 Char patbuf
[MAXPATHLEN
];
246 /* copy part up to the brace */
247 for (lm
= patbuf
, pm
= pattern
; pm
!= ptr
; *lm
++ = *pm
++)
252 /* Find the balanced brace */
253 for (i
= 0, pe
= ++ptr
; *pe
; pe
++)
254 if (*pe
== LBRACKET
) {
255 /* Ignore everything between [] */
256 for (pm
= pe
++; *pe
!= RBRACKET
&& *pe
!= EOS
; pe
++)
260 * We could not find a matching RBRACKET.
261 * Ignore and just look for RBRACE
266 else if (*pe
== LBRACE
)
268 else if (*pe
== RBRACE
) {
274 /* Non matching braces; just glob the pattern */
275 if (i
!= 0 || *pe
== EOS
) {
276 *rv
= glob0(patbuf
, pglob
, limit
);
280 for (i
= 0, pl
= pm
= ptr
; pm
<= pe
; pm
++)
283 /* Ignore everything between [] */
284 for (pl
= pm
++; *pm
!= RBRACKET
&& *pm
!= EOS
; pm
++)
288 * We could not find a matching RBRACKET.
289 * Ignore and just look for RBRACE
306 if (i
&& *pm
== COMMA
)
309 /* Append the current string */
310 for (lm
= ls
; (pl
< pm
); *lm
++ = *pl
++)
313 * Append the rest of the pattern after the
316 for (pl
= pe
+ 1; (*lm
++ = *pl
++) != EOS
;)
319 /* Expand the current pattern */
321 qprintf("globexp2:", patbuf
);
323 *rv
= globexp1(patbuf
, pglob
, limit
);
325 /* move after the comma, to the next string */
340 * expand tilde from the passwd file.
343 globtilde(pattern
, patbuf
, patbuf_len
, pglob
)
354 if (*pattern
!= TILDE
|| !(pglob
->gl_flags
& GLOB_TILDE
))
358 * Copy up to the end of the string or /
360 eb
= &patbuf
[patbuf_len
- 1];
361 for (p
= pattern
+ 1, h
= (char *) patbuf
;
362 h
< (char *)eb
&& *p
&& *p
!= SLASH
; *h
++ = *p
++)
367 if (((char *) patbuf
)[0] == EOS
) {
369 * handle a plain ~ or ~/ by expanding $HOME first (iff
370 * we're not running setuid or setgid) and then trying
375 #ifndef __NETBSD_SYSCALLS
379 (h
= getenv("HOME")) == NULL
) {
380 if (((h
= getlogin()) != NULL
&&
381 (pwd
= getpwnam(h
)) != NULL
) ||
382 (pwd
= getpwuid(getuid())) != NULL
)
392 if ((pwd
= getpwnam((char*) patbuf
)) == NULL
)
398 /* Copy the home directory */
399 for (b
= patbuf
; b
< eb
&& *h
; *b
++ = *h
++)
402 /* Append the rest of the pattern */
403 while (b
< eb
&& (*b
++ = *p
++) != EOS
)
412 * The main glob() routine: compiles the pattern (optionally processing
413 * quotes), calls glob1() to do the real pattern matching, and finally
414 * sorts the list (unless unsorted operation is requested). Returns 0
415 * if things went well, nonzero if errors occurred. It is not an error
416 * to find no matches.
419 glob0(pattern
, pglob
, limit
)
424 const Char
*qpatnext
;
425 int c
, err
, oldpathc
;
426 Char
*bufnext
, patbuf
[MAXPATHLEN
];
428 qpatnext
= globtilde(pattern
, patbuf
, MAXPATHLEN
, pglob
);
429 oldpathc
= pglob
->gl_pathc
;
432 /* We don't need to check for buffer overflow any more. */
433 while ((c
= *qpatnext
++) != EOS
) {
439 if (*qpatnext
== EOS
||
440 g_strchr((Char
*) qpatnext
+1, RBRACKET
) == NULL
) {
441 *bufnext
++ = LBRACKET
;
451 *bufnext
++ = CHAR(c
);
452 if (*qpatnext
== RANGE
&&
453 (c
= qpatnext
[1]) != RBRACKET
) {
455 *bufnext
++ = CHAR(c
);
458 } while ((c
= *qpatnext
++) != RBRACKET
);
459 pglob
->gl_flags
|= GLOB_MAGCHAR
;
463 pglob
->gl_flags
|= GLOB_MAGCHAR
;
467 pglob
->gl_flags
|= GLOB_MAGCHAR
;
468 /* collapse adjacent stars to one,
469 * to avoid exponential behavior
471 if (bufnext
== patbuf
|| bufnext
[-1] != M_ALL
)
475 *bufnext
++ = CHAR(c
);
481 qprintf("glob0:", patbuf
);
484 if ((err
= glob1(patbuf
, pglob
, limit
)) != 0)
488 * If there was no match we are going to append the pattern
489 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
490 * and the pattern did not contain any magic characters
491 * GLOB_NOMAGIC is there just for compatibility with csh.
493 if (pglob
->gl_pathc
== oldpathc
&&
494 ((pglob
->gl_flags
& GLOB_NOCHECK
) ||
495 ((pglob
->gl_flags
& GLOB_NOMAGIC
) &&
496 !(pglob
->gl_flags
& GLOB_MAGCHAR
))))
497 return(globextend(pattern
, pglob
, limit
));
498 else if (!(pglob
->gl_flags
& GLOB_NOSORT
))
499 qsort(pglob
->gl_pathv
+ pglob
->gl_offs
+ oldpathc
,
500 pglob
->gl_pathc
- oldpathc
, sizeof(char *), compare
);
508 return(strcmp(*(char **)p
, *(char **)q
));
512 glob1(pattern
, pglob
, limit
)
517 Char pathbuf
[MAXPATHLEN
];
519 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
522 return(glob2(pathbuf
, pathbuf
, pathbuf
+ MAXPATHLEN
- 1,
523 pattern
, pglob
, limit
));
527 * The functions glob2 and glob3 are mutually recursive; there is one level
528 * of recursion for each segment in the pattern that contains one or more
532 glob2(pathbuf
, pathend
, pathend_last
, pattern
, pglob
, limit
)
533 Char
*pathbuf
, *pathend
, *pathend_last
, *pattern
;
542 * Loop over pattern segments until end of pattern or until
543 * segment with meta character found.
545 for (anymeta
= 0;;) {
546 if (*pattern
== EOS
) { /* End of pattern? */
548 if (g_lstat(pathbuf
, &sb
, pglob
))
551 if (((pglob
->gl_flags
& GLOB_MARK
) &&
552 pathend
[-1] != SEP
) && (S_ISDIR(sb
.st_mode
)
553 || (S_ISLNK(sb
.st_mode
) &&
554 (g_stat(pathbuf
, &sb
, pglob
) == 0) &&
555 S_ISDIR(sb
.st_mode
)))) {
556 if (pathend
+ 1 > pathend_last
)
562 return(globextend(pathbuf
, pglob
, limit
));
565 /* Find end of next segment, copy tentatively to pathend. */
568 while (*p
!= EOS
&& *p
!= SEP
) {
571 if (q
+ 1 > pathend_last
)
576 if (!anymeta
) { /* No expansion, do next segment. */
579 while (*pattern
== SEP
) {
580 if (pathend
+ 1 > pathend_last
)
582 *pathend
++ = *pattern
++;
584 } else /* Need expansion, recurse. */
585 return(glob3(pathbuf
, pathend
, pathend_last
, pattern
, p
,
592 glob3(pathbuf
, pathend
, pathend_last
, pattern
, restpattern
, pglob
, limit
)
593 Char
*pathbuf
, *pathend
, *pathend_last
, *pattern
, *restpattern
;
597 register struct dirent
*dp
;
600 char buf
[MAXPATHLEN
];
603 * The readdirfunc declaration can't be prototyped, because it is
604 * assigned, below, to two functions which are prototyped in glob.h
605 * and dirent.h as taking pointers to differently typed opaque
608 struct dirent
*(*readdirfunc
)();
610 if (pathend
> pathend_last
)
615 if ((dirp
= g_opendir(pathbuf
, pglob
)) == NULL
) {
616 /* TODO: don't call for ENOENT or ENOTDIR? */
617 if (pglob
->gl_errfunc
) {
618 if (g_Ctoc(pathbuf
, buf
, sizeof(buf
)))
620 if (pglob
->gl_errfunc(buf
, errno
) ||
621 pglob
->gl_flags
& GLOB_ERR
)
629 /* Search directory for matching names. */
630 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
631 readdirfunc
= pglob
->gl_readdir
;
633 readdirfunc
= readdir
;
634 while ((dp
= (*readdirfunc
)(dirp
))) {
638 /* Initial DOT must be matched literally. */
639 if (dp
->d_name
[0] == DOT
&& *pattern
!= DOT
)
642 sc
= (u_char
*) dp
->d_name
;
643 while (dc
< pathend_last
&& (*dc
++ = *sc
++) != EOS
)
645 if (!match(pathend
, pattern
, restpattern
)) {
649 err
= glob2(pathbuf
, --dc
, pathend_last
, restpattern
,
655 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
656 (*pglob
->gl_closedir
)(dirp
);
664 * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
665 * add the new item, and update gl_pathc.
667 * This assumes the BSD realloc, which only copies the block when its size
668 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
671 * Return 0 if new item added, error code if memory couldn't be allocated.
673 * Invariant of the glob_t structure:
674 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
675 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
678 globextend(path
, pglob
, limit
)
683 register char **pathv
;
689 if (*limit
&& pglob
->gl_pathc
> *limit
)
690 #if !defined(GLOB_LIMIT)
691 #define GLOB_LIMIT (-3)
695 newsize
= sizeof(*pathv
) * (2 + pglob
->gl_pathc
+ pglob
->gl_offs
);
696 pathv
= pglob
->gl_pathv
?
697 realloc((char *)pglob
->gl_pathv
, newsize
) :
700 if (pglob
->gl_pathv
) {
701 free(pglob
->gl_pathv
);
702 pglob
->gl_pathv
= NULL
;
704 return(GLOB_NOSPACE
);
707 if (pglob
->gl_pathv
== NULL
&& pglob
->gl_offs
> 0) {
708 /* first time around -- clear initial gl_offs items */
709 pathv
+= pglob
->gl_offs
;
710 for (i
= pglob
->gl_offs
; --i
>= 0; )
713 pglob
->gl_pathv
= pathv
;
715 for (p
= path
; *p
++;)
717 len
= (size_t)(p
- path
);
718 if ((copy
= malloc(len
)) != NULL
) {
719 if (g_Ctoc(path
, copy
, len
)) {
721 return (GLOB_NOSPACE
);
723 pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
++] = copy
;
725 pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
] = NULL
;
726 return(copy
== NULL
? GLOB_NOSPACE
: 0);
730 * pattern matching function for filenames. Each occurrence of the *
731 * pattern causes a recursion level.
734 match(name
, pat
, patend
)
735 register Char
*name
, *pat
, *patend
;
737 int ok
, negate_range
;
740 while (pat
< patend
) {
742 switch (c
& M_MASK
) {
747 if (match(name
, pat
, patend
))
749 while (*name
++ != EOS
);
757 if ((k
= *name
++) == EOS
)
759 if ((negate_range
= ((*pat
& M_MASK
) == M_NOT
)) != EOS
)
761 while (((c
= *pat
++) & M_MASK
) != M_END
)
762 if ((*pat
& M_MASK
) == M_RNG
) {
763 if (c
<= k
&& k
<= pat
[1])
768 if (ok
== negate_range
)
777 return(*name
== EOS
);
780 /* Free allocated data belonging to a glob_t structure. */
788 if (pglob
->gl_pathv
!= NULL
) {
789 pp
= pglob
->gl_pathv
+ pglob
->gl_offs
;
790 for (i
= pglob
->gl_pathc
; i
--; ++pp
)
793 free(pglob
->gl_pathv
);
794 pglob
->gl_pathv
= NULL
;
799 g_opendir(str
, pglob
)
803 char buf
[MAXPATHLEN
];
808 if (g_Ctoc(str
, buf
, sizeof(buf
)))
812 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
813 return((*pglob
->gl_opendir
)(buf
));
815 return(opendir(buf
));
819 g_lstat(fn
, sb
, pglob
)
824 char buf
[MAXPATHLEN
];
826 if (g_Ctoc(fn
, buf
, sizeof(buf
))) {
827 errno
= ENAMETOOLONG
;
830 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
831 return((*pglob
->gl_lstat
)(buf
, sb
));
832 return(lstat(buf
, sb
));
836 g_stat(fn
, sb
, pglob
)
841 char buf
[MAXPATHLEN
];
843 if (g_Ctoc(fn
, buf
, sizeof(buf
))) {
844 errno
= ENAMETOOLONG
;
847 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
848 return((*pglob
->gl_stat
)(buf
, sb
));
849 return(stat(buf
, sb
));
865 g_Ctoc(str
, buf
, len
)
872 if ((*buf
++ = *str
++) == '\0')
886 (void)printf("%s:\n", str
);
888 (void)printf("%c", CHAR(*p
));
891 (void)printf("%c", *p
& M_PROTECT
? '"' : ' ');
894 (void)printf("%c", ismeta(*p
) ? '_' : ' ');