]>
git.wh0rd.org - dump.git/blob - compat/lib/glob.c
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994, 1995, 1996
9 * Copyright (c) 1989, 1993
10 * The Regents of the University of California. All rights reserved.
12 * This code is derived from software contributed to Berkeley by
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgement:
25 * This product includes software developed by the University of
26 * California, Berkeley and its contributors.
27 * 4. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 #if defined(LIBC_SCCS) && !defined(lint)
45 static char sccsid
[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93";
46 #endif /* LIBC_SCCS and not lint */
49 * glob(3) -- a superset of the one defined in POSIX 1003.2.
51 * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
53 * Optional extra services, controlled by flags not defined by POSIX:
56 * Escaping convention: \ inhibits any special meaning the following
57 * character might have (except \ at end of string is retained).
59 * Set in gl_flags if pattern contained a globbing character.
61 * Same as GLOB_NOCHECK, but it will only append pattern if it did
62 * not contain any magic characters. [Used in csh style globbing]
64 * Use alternately specified directory access functions.
66 * expand ~user/foo to the /home/dir/of/user/foo
68 * expand {1,2}{a,b} to 1a 1b 2a 2b
70 * Number of matches in the current invocation of glob.
73 #include <sys/param.h>
98 #define UNDERSCORE '_'
106 #define M_QUOTE 0x8000
107 #define M_PROTECT 0x4000
108 #define M_MASK 0xffff
109 #define M_ASCII 0x00ff
111 typedef u_short Char
;
116 #define M_PROTECT 0x40
125 #define CHAR(c) ((Char)((c)&M_ASCII))
126 #define META(c) ((Char)((c)|M_QUOTE))
127 #define M_ALL META('*')
128 #define M_END META(']')
129 #define M_NOT META('!')
130 #define M_ONE META('?')
131 #define M_RNG META('-')
132 #define M_SET META('[')
133 #define ismeta(c) (((c)&M_QUOTE) != 0)
136 static int compare
__P((const void *, const void *));
137 static void g_Ctoc
__P((const Char
*, char *));
138 static int g_lstat
__P((Char
*, struct stat
*, glob_t
*));
139 static DIR *g_opendir
__P((Char
*, glob_t
*));
140 static Char
*g_strchr
__P((Char
*, int));
142 static Char
*g_strcat
__P((Char
*, const Char
*));
144 static int g_stat
__P((Char
*, struct stat
*, glob_t
*));
145 static int glob0
__P((const Char
*, glob_t
*));
146 static int glob1
__P((Char
*, glob_t
*));
147 static int glob2
__P((Char
*, Char
*, Char
*, glob_t
*));
148 static int glob3
__P((Char
*, Char
*, Char
*, Char
*, glob_t
*));
149 static int globextend
__P((const Char
*, glob_t
*));
150 static const Char
* globtilde
__P((const Char
*, Char
*, glob_t
*));
151 static int globexp1
__P((const Char
*, glob_t
*));
152 static int globexp2
__P((const Char
*, const Char
*, glob_t
*, int *));
153 static int match
__P((Char
*, Char
*, Char
*));
155 static void qprintf
__P((const char *, Char
*));
159 glob(pattern
, flags
, errfunc
, pglob
)
161 int flags
, (*errfunc
) __P((const char *, int));
164 const u_char
*patnext
;
166 Char
*bufnext
, *bufend
, patbuf
[MAXPATHLEN
+1];
168 patnext
= (u_char
*) pattern
;
169 if (!(flags
& GLOB_APPEND
)) {
171 pglob
->gl_pathv
= NULL
;
172 if (!(flags
& GLOB_DOOFFS
))
175 pglob
->gl_flags
= flags
& ~GLOB_MAGCHAR
;
176 pglob
->gl_errfunc
= errfunc
;
177 pglob
->gl_matchc
= 0;
180 bufend
= bufnext
+ MAXPATHLEN
;
181 if (flags
& GLOB_QUOTE
) {
182 /* Protect the quoted characters. */
183 while (bufnext
< bufend
&& (c
= *patnext
++) != EOS
)
185 if ((c
= *patnext
++) == EOS
) {
189 *bufnext
++ = c
| M_PROTECT
;
195 while (bufnext
< bufend
&& (c
= *patnext
++) != EOS
)
199 if (flags
& GLOB_BRACE
)
200 return globexp1(patbuf
, pglob
);
202 return glob0(patbuf
, pglob
);
206 * Expand recursively a glob {} pattern. When there is no more expansion
207 * invoke the standard globbing routine to glob the rest of the magic
210 static int globexp1(pattern
, pglob
)
214 const Char
* ptr
= pattern
;
217 /* Protect a single {}, for find(1), like csh */
218 if (pattern
[0] == LBRACE
&& pattern
[1] == RBRACE
&& pattern
[2] == EOS
)
219 return glob0(pattern
, pglob
);
221 while ((ptr
= (const Char
*) g_strchr((Char
*) ptr
, LBRACE
)) != NULL
)
222 if (!globexp2(ptr
, pattern
, pglob
, &rv
))
225 return glob0(pattern
, pglob
);
230 * Recursive brace globbing helper. Tries to expand a single brace.
231 * If it succeeds then it invokes globexp1 with the new pattern.
232 * If it fails then it tries to glob the rest of the pattern and returns.
234 static int globexp2(ptr
, pattern
, pglob
, rv
)
235 const Char
*ptr
, *pattern
;
241 const Char
*pe
, *pm
, *pl
;
242 Char patbuf
[MAXPATHLEN
+ 1];
244 /* copy part up to the brace */
245 for (lm
= patbuf
, pm
= pattern
; pm
!= ptr
; *lm
++ = *pm
++)
249 /* Find the balanced brace */
250 for (i
= 0, pe
= ++ptr
; *pe
; pe
++)
251 if (*pe
== LBRACKET
) {
252 /* Ignore everything between [] */
253 for (pm
= pe
++; *pe
!= RBRACKET
&& *pe
!= EOS
; pe
++)
257 * We could not find a matching RBRACKET.
258 * Ignore and just look for RBRACE
263 else if (*pe
== LBRACE
)
265 else if (*pe
== RBRACE
) {
271 /* Non matching braces; just glob the pattern */
272 if (i
!= 0 || *pe
== EOS
) {
273 *rv
= glob0(patbuf
, pglob
);
277 for (i
= 0, pl
= pm
= ptr
; pm
<= pe
; pm
++)
280 /* Ignore everything between [] */
281 for (pl
= pm
++; *pm
!= RBRACKET
&& *pm
!= EOS
; pm
++)
285 * We could not find a matching RBRACKET.
286 * Ignore and just look for RBRACE
303 if (i
&& *pm
== COMMA
)
306 /* Append the current string */
307 for (lm
= ls
; (pl
< pm
); *lm
++ = *pl
++)
310 * Append the rest of the pattern after the
313 for (pl
= pe
+ 1; (*lm
++ = *pl
++) != EOS
;)
316 /* Expand the current pattern */
318 qprintf("globexp2:", patbuf
);
320 *rv
= globexp1(patbuf
, pglob
);
322 /* move after the comma, to the next string */
337 * expand tilde from the passwd file.
340 globtilde(pattern
, patbuf
, pglob
)
350 if (*pattern
!= TILDE
|| !(pglob
->gl_flags
& GLOB_TILDE
))
353 /* Copy up to the end of the string or / */
354 for (p
= pattern
+ 1, h
= (char *) patbuf
; *p
&& *p
!= SLASH
;
360 if (((char *) patbuf
)[0] == EOS
) {
362 * handle a plain ~ or ~/ by expanding $HOME
363 * first and then trying the password file
365 if ((h
= getenv("HOME")) == NULL
) {
366 if ((pwd
= getpwuid(getuid())) == NULL
)
376 if ((pwd
= getpwnam((char*) patbuf
)) == NULL
)
382 /* Copy the home directory */
383 for (b
= patbuf
; *h
; *b
++ = *h
++)
386 /* Append the rest of the pattern */
387 while ((*b
++ = *p
++) != EOS
)
395 * The main glob() routine: compiles the pattern (optionally processing
396 * quotes), calls glob1() to do the real pattern matching, and finally
397 * sorts the list (unless unsorted operation is requested). Returns 0
398 * if things went well, nonzero if errors occurred. It is not an error
399 * to find no matches.
402 glob0(pattern
, pglob
)
406 const Char
*qpatnext
;
407 int c
, err
, oldpathc
;
408 Char
*bufnext
, patbuf
[MAXPATHLEN
+1];
410 qpatnext
= globtilde(pattern
, patbuf
, pglob
);
411 oldpathc
= pglob
->gl_pathc
;
414 /* We don't need to check for buffer overflow any more. */
415 while ((c
= *qpatnext
++) != EOS
) {
421 if (*qpatnext
== EOS
||
422 g_strchr((Char
*) qpatnext
+1, RBRACKET
) == NULL
) {
423 *bufnext
++ = LBRACKET
;
433 *bufnext
++ = CHAR(c
);
434 if (*qpatnext
== RANGE
&&
435 (c
= qpatnext
[1]) != RBRACKET
) {
437 *bufnext
++ = CHAR(c
);
440 } while ((c
= *qpatnext
++) != RBRACKET
);
441 pglob
->gl_flags
|= GLOB_MAGCHAR
;
445 pglob
->gl_flags
|= GLOB_MAGCHAR
;
449 pglob
->gl_flags
|= GLOB_MAGCHAR
;
450 /* collapse adjacent stars to one,
451 * to avoid exponential behavior
453 if (bufnext
== patbuf
|| bufnext
[-1] != M_ALL
)
457 *bufnext
++ = CHAR(c
);
463 qprintf("glob0:", patbuf
);
466 if ((err
= glob1(patbuf
, pglob
)) != 0)
470 * If there was no match we are going to append the pattern
471 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
472 * and the pattern did not contain any magic characters
473 * GLOB_NOMAGIC is there just for compatibility with csh.
475 if (pglob
->gl_pathc
== oldpathc
&&
476 ((pglob
->gl_flags
& GLOB_NOCHECK
) ||
477 ((pglob
->gl_flags
& GLOB_NOMAGIC
) &&
478 !(pglob
->gl_flags
& GLOB_MAGCHAR
))))
479 return(globextend(pattern
, pglob
));
480 else if (!(pglob
->gl_flags
& GLOB_NOSORT
))
481 qsort(pglob
->gl_pathv
+ pglob
->gl_offs
+ oldpathc
,
482 pglob
->gl_pathc
- oldpathc
, sizeof(char *), compare
);
490 return(strcmp(*(char **)p
, *(char **)q
));
494 glob1(pattern
, pglob
)
498 Char pathbuf
[MAXPATHLEN
+1];
500 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
503 return(glob2(pathbuf
, pathbuf
, pattern
, pglob
));
507 * The functions glob2 and glob3 are mutually recursive; there is one level
508 * of recursion for each segment in the pattern that contains one or more
512 glob2(pathbuf
, pathend
, pattern
, pglob
)
513 Char
*pathbuf
, *pathend
, *pattern
;
521 * Loop over pattern segments until end of pattern or until
522 * segment with meta character found.
524 for (anymeta
= 0;;) {
525 if (*pattern
== EOS
) { /* End of pattern? */
527 if (g_lstat(pathbuf
, &sb
, pglob
))
530 if (((pglob
->gl_flags
& GLOB_MARK
) &&
531 pathend
[-1] != SEP
) && (S_ISDIR(sb
.st_mode
)
532 || (S_ISLNK(sb
.st_mode
) &&
533 (g_stat(pathbuf
, &sb
, pglob
) == 0) &&
534 S_ISDIR(sb
.st_mode
)))) {
539 return(globextend(pathbuf
, pglob
));
542 /* Find end of next segment, copy tentatively to pathend. */
545 while (*p
!= EOS
&& *p
!= SEP
) {
551 if (!anymeta
) { /* No expansion, do next segment. */
554 while (*pattern
== SEP
)
555 *pathend
++ = *pattern
++;
556 } else /* Need expansion, recurse. */
557 return(glob3(pathbuf
, pathend
, pattern
, p
, pglob
));
563 glob3(pathbuf
, pathend
, pattern
, restpattern
, pglob
)
564 Char
*pathbuf
, *pathend
, *pattern
, *restpattern
;
567 register struct dirent
*dp
;
570 char buf
[MAXPATHLEN
];
573 * The readdirfunc declaration can't be prototyped, because it is
574 * assigned, below, to two functions which are prototyped in glob.h
575 * and dirent.h as taking pointers to differently typed opaque
578 struct dirent
*(*readdirfunc
)();
583 if ((dirp
= g_opendir(pathbuf
, pglob
)) == NULL
) {
584 /* TODO: don't call for ENOENT or ENOTDIR? */
585 if (pglob
->gl_errfunc
) {
586 g_Ctoc(pathbuf
, buf
);
587 if (pglob
->gl_errfunc(buf
, errno
) ||
588 pglob
->gl_flags
& GLOB_ERR
)
596 /* Search directory for matching names. */
597 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
598 readdirfunc
= pglob
->gl_readdir
;
600 readdirfunc
= readdir
;
601 while ((dp
= (*readdirfunc
)(dirp
))) {
605 /* Initial DOT must be matched literally. */
606 if (dp
->d_name
[0] == DOT
&& *pattern
!= DOT
)
608 for (sc
= (u_char
*) dp
->d_name
, dc
= pathend
;
609 (*dc
++ = *sc
++) != EOS
;)
611 if (!match(pathend
, pattern
, restpattern
)) {
615 err
= glob2(pathbuf
, --dc
, restpattern
, pglob
);
620 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
621 (*pglob
->gl_closedir
)(dirp
);
629 * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
630 * add the new item, and update gl_pathc.
632 * This assumes the BSD realloc, which only copies the block when its size
633 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
636 * Return 0 if new item added, error code if memory couldn't be allocated.
638 * Invariant of the glob_t structure:
639 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
640 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
643 globextend(path
, pglob
)
647 register char **pathv
;
653 newsize
= sizeof(*pathv
) * (2 + pglob
->gl_pathc
+ pglob
->gl_offs
);
654 pathv
= pglob
->gl_pathv
?
655 realloc((char *)pglob
->gl_pathv
, newsize
) :
658 return(GLOB_NOSPACE
);
660 if (pglob
->gl_pathv
== NULL
&& pglob
->gl_offs
> 0) {
661 /* first time around -- clear initial gl_offs items */
662 pathv
+= pglob
->gl_offs
;
663 for (i
= pglob
->gl_offs
; --i
>= 0; )
666 pglob
->gl_pathv
= pathv
;
668 for (p
= path
; *p
++;)
670 if ((copy
= malloc(p
- path
)) != NULL
) {
672 pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
++] = copy
;
674 pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
] = NULL
;
675 return(copy
== NULL
? GLOB_NOSPACE
: 0);
680 * pattern matching function for filenames. Each occurrence of the *
681 * pattern causes a recursion level.
684 match(name
, pat
, patend
)
685 register Char
*name
, *pat
, *patend
;
687 int ok
, negate_range
;
690 while (pat
< patend
) {
692 switch (c
& M_MASK
) {
697 if (match(name
, pat
, patend
))
699 while (*name
++ != EOS
);
707 if ((k
= *name
++) == EOS
)
709 if ((negate_range
= ((*pat
& M_MASK
) == M_NOT
)) != EOS
)
711 while (((c
= *pat
++) & M_MASK
) != M_END
)
712 if ((*pat
& M_MASK
) == M_RNG
) {
713 if (c
<= k
&& k
<= pat
[1])
718 if (ok
== negate_range
)
727 return(*name
== EOS
);
730 /* Free allocated data belonging to a glob_t structure. */
738 if (pglob
->gl_pathv
!= NULL
) {
739 pp
= pglob
->gl_pathv
+ pglob
->gl_offs
;
740 for (i
= pglob
->gl_pathc
; i
--; ++pp
)
743 free(pglob
->gl_pathv
);
748 g_opendir(str
, pglob
)
752 char buf
[MAXPATHLEN
];
759 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
760 return((*pglob
->gl_opendir
)(buf
));
762 return(opendir(buf
));
766 g_lstat(fn
, sb
, pglob
)
771 char buf
[MAXPATHLEN
];
774 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
775 return((*pglob
->gl_lstat
)(buf
, sb
));
776 return(lstat(buf
, sb
));
780 g_stat(fn
, sb
, pglob
)
785 char buf
[MAXPATHLEN
];
788 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
789 return((*pglob
->gl_stat
)(buf
, sb
));
790 return(stat(buf
, sb
));
816 while((*dst
++ = *src
++) != EOS
)
825 register const Char
*str
;
830 for (dc
= buf
; (*dc
++ = *str
++) != EOS
;)
842 (void)printf("%s:\n", str
);
844 (void)printf("%c", CHAR(*p
));
847 (void)printf("%c", *p
& M_PROTECT
? '"' : ' ');
850 (void)printf("%c", ismeta(*p
) ? '_' : ' ');