]>
git.wh0rd.org - dump.git/blob - compat/lib/compatglob.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-1997
5 * Stelian Pop <pop@cybercable.fr>, 1999
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
45 * glob(3) -- a superset of the one defined in POSIX 1003.2.
47 * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
49 * Optional extra services, controlled by flags not defined by POSIX:
52 * Escaping convention: \ inhibits any special meaning the following
53 * character might have (except \ at end of string is retained).
55 * Set in gl_flags if pattern contained a globbing character.
57 * Same as GLOB_NOCHECK, but it will only append pattern if it did
58 * not contain any magic characters. [Used in csh style globbing]
60 * Use alternately specified directory access functions.
62 * expand ~user/foo to the /home/dir/of/user/foo
64 * expand {1,2}{a,b} to 1a 1b 2a 2b
66 * Number of matches in the current invocation of glob.
70 static const char rcsid
[] =
71 "$Id: compatglob.c,v 1.3 1999/10/13 09:57:19 stelian Exp $";
78 #include <sys/param.h>
84 #include <compatglob.h>
103 #define UNDERSCORE '_'
111 #define M_QUOTE 0x8000
112 #define M_PROTECT 0x4000
113 #define M_MASK 0xffff
114 #define M_ASCII 0x00ff
116 typedef u_short Char
;
121 #define M_PROTECT 0x40
130 #define CHAR(c) ((Char)((c)&M_ASCII))
131 #define META(c) ((Char)((c)|M_QUOTE))
132 #define M_ALL META('*')
133 #define M_END META(']')
134 #define M_NOT META('!')
135 #define M_ONE META('?')
136 #define M_RNG META('-')
137 #define M_SET META('[')
138 #define ismeta(c) (((c)&M_QUOTE) != 0)
141 static int compare
__P((const void *, const void *));
142 static void g_Ctoc
__P((const Char
*, char *));
143 static int g_lstat
__P((Char
*, struct stat
*, glob_t
*));
144 static DIR *g_opendir
__P((Char
*, glob_t
*));
145 static Char
*g_strchr
__P((Char
*, int));
147 static Char
*g_strcat
__P((Char
*, const Char
*));
149 static int g_stat
__P((Char
*, struct stat
*, glob_t
*));
150 static int glob0
__P((const Char
*, glob_t
*));
151 static int glob1
__P((Char
*, glob_t
*));
152 static int glob2
__P((Char
*, Char
*, Char
*, glob_t
*));
153 static int glob3
__P((Char
*, Char
*, Char
*, Char
*, glob_t
*));
154 static int globextend
__P((const Char
*, glob_t
*));
155 static const Char
* globtilde
__P((const Char
*, Char
*, size_t, glob_t
*));
156 static int globexp1
__P((const Char
*, glob_t
*));
157 static int globexp2
__P((const Char
*, const Char
*, glob_t
*, int *));
158 static int match
__P((Char
*, Char
*, Char
*));
160 static void qprintf
__P((const char *, Char
*));
164 glob(const char *pattern
, int flags
, int (*errfunc
) __P((const char *, int)), glob_t
*pglob
)
166 const u_char
*patnext
;
168 Char
*bufnext
, *bufend
, patbuf
[MAXPATHLEN
+1];
170 patnext
= (u_char
*) pattern
;
171 if (!(flags
& GLOB_APPEND
)) {
173 pglob
->gl_pathv
= NULL
;
174 if (!(flags
& GLOB_DOOFFS
))
177 pglob
->gl_flags
= flags
& ~GLOB_MAGCHAR
;
178 pglob
->gl_errfunc
= errfunc
;
179 pglob
->gl_matchc
= 0;
182 bufend
= bufnext
+ MAXPATHLEN
;
183 if (flags
& GLOB_QUOTE
) {
184 /* Protect the quoted characters. */
185 while (bufnext
< bufend
&& (c
= *patnext
++) != EOS
)
187 if ((c
= *patnext
++) == EOS
) {
191 *bufnext
++ = c
| M_PROTECT
;
197 while (bufnext
< bufend
&& (c
= *patnext
++) != EOS
)
201 if (flags
& GLOB_BRACE
)
202 return globexp1(patbuf
, pglob
);
204 return glob0(patbuf
, pglob
);
208 * Expand recursively a glob {} pattern. When there is no more expansion
209 * invoke the standard globbing routine to glob the rest of the magic
212 static int globexp1(const Char
*pattern
, glob_t
*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(const Char
*ptr
, const Char
*pattern
, glob_t
*pglob
, int *rv
)
238 const Char
*pe
, *pm
, *pl
;
239 Char patbuf
[MAXPATHLEN
+ 1];
241 /* copy part up to the brace */
242 for (lm
= patbuf
, pm
= pattern
; pm
!= ptr
; *lm
++ = *pm
++)
246 /* Find the balanced brace */
247 for (i
= 0, pe
= ++ptr
; *pe
; pe
++)
248 if (*pe
== LBRACKET
) {
249 /* Ignore everything between [] */
250 for (pm
= pe
++; *pe
!= RBRACKET
&& *pe
!= EOS
; pe
++)
254 * We could not find a matching RBRACKET.
255 * Ignore and just look for RBRACE
260 else if (*pe
== LBRACE
)
262 else if (*pe
== RBRACE
) {
268 /* Non matching braces; just glob the pattern */
269 if (i
!= 0 || *pe
== EOS
) {
270 *rv
= glob0(patbuf
, pglob
);
274 for (i
= 0, pl
= pm
= ptr
; pm
<= pe
; pm
++)
277 /* Ignore everything between [] */
278 for (pl
= pm
++; *pm
!= RBRACKET
&& *pm
!= EOS
; pm
++)
282 * We could not find a matching RBRACKET.
283 * Ignore and just look for RBRACE
300 if (i
&& *pm
== COMMA
)
303 /* Append the current string */
304 for (lm
= ls
; (pl
< pm
); *lm
++ = *pl
++)
307 * Append the rest of the pattern after the
310 for (pl
= pe
+ 1; (*lm
++ = *pl
++) != EOS
;)
313 /* Expand the current pattern */
315 qprintf("globexp2:", patbuf
);
317 *rv
= globexp1(patbuf
, pglob
);
319 /* move after the comma, to the next string */
334 * expand tilde from the passwd file.
337 globtilde(const Char
*pattern
, Char
*patbuf
, size_t patbuf_len
, glob_t
*pglob
)
344 if (*pattern
!= TILDE
|| !(pglob
->gl_flags
& GLOB_TILDE
))
348 * Copy up to the end of the string or /
350 eb
= &patbuf
[patbuf_len
- 1];
351 for (p
= pattern
+ 1, h
= (char *) patbuf
;
352 h
< (char *)eb
&& *p
&& *p
!= SLASH
; *h
++ = *p
++)
357 if (((char *) patbuf
)[0] == EOS
) {
359 * handle a plain ~ or ~/ by expanding $HOME first if
360 * we're not running setuid or setgid) and then trying
365 #ifndef __NETBSD_SYSCALLS
369 (h
= getenv("HOME")) == NULL
) {
370 if (((h
= getlogin()) != NULL
&&
371 (pwd
= getpwnam(h
)) != NULL
) ||
372 (pwd
= getpwuid(getuid())) != NULL
)
382 if ((pwd
= getpwnam((char*) patbuf
)) == NULL
)
388 /* Copy the home directory */
389 for (b
= patbuf
; b
< eb
&& *h
; *b
++ = *h
++)
392 /* Append the rest of the pattern */
393 while (b
< eb
&& (*b
++ = *p
++) != EOS
)
402 * The main glob() routine: compiles the pattern (optionally processing
403 * quotes), calls glob1() to do the real pattern matching, and finally
404 * sorts the list (unless unsorted operation is requested). Returns 0
405 * if things went well, nonzero if errors occurred. It is not an error
406 * to find no matches.
409 glob0(const Char
*pattern
, glob_t
*pglob
)
411 const Char
*qpatnext
;
412 int c
, err
, oldpathc
;
413 Char
*bufnext
, patbuf
[MAXPATHLEN
+1];
415 qpatnext
= globtilde(pattern
, patbuf
, sizeof(patbuf
) / sizeof(Char
),
417 oldpathc
= pglob
->gl_pathc
;
420 /* We don't need to check for buffer overflow any more. */
421 while ((c
= *qpatnext
++) != EOS
) {
427 if (*qpatnext
== EOS
||
428 g_strchr((Char
*) qpatnext
+1, RBRACKET
) == NULL
) {
429 *bufnext
++ = LBRACKET
;
439 *bufnext
++ = CHAR(c
);
440 if (*qpatnext
== RANGE
&&
441 (c
= qpatnext
[1]) != RBRACKET
) {
443 *bufnext
++ = CHAR(c
);
446 } while ((c
= *qpatnext
++) != RBRACKET
);
447 pglob
->gl_flags
|= GLOB_MAGCHAR
;
451 pglob
->gl_flags
|= GLOB_MAGCHAR
;
455 pglob
->gl_flags
|= GLOB_MAGCHAR
;
456 /* collapse adjacent stars to one,
457 * to avoid exponential behavior
459 if (bufnext
== patbuf
|| bufnext
[-1] != M_ALL
)
463 *bufnext
++ = CHAR(c
);
469 qprintf("glob0:", patbuf
);
472 if ((err
= glob1(patbuf
, pglob
)) != 0)
476 * If there was no match we are going to append the pattern
477 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
478 * and the pattern did not contain any magic characters
479 * GLOB_NOMAGIC is there just for compatibility with csh.
481 if (pglob
->gl_pathc
== oldpathc
&&
482 ((pglob
->gl_flags
& GLOB_NOCHECK
) ||
483 ((pglob
->gl_flags
& GLOB_NOMAGIC
) &&
484 !(pglob
->gl_flags
& GLOB_MAGCHAR
))))
485 return(globextend(pattern
, pglob
));
486 else if (!(pglob
->gl_flags
& GLOB_NOSORT
))
487 qsort(pglob
->gl_pathv
+ pglob
->gl_offs
+ oldpathc
,
488 pglob
->gl_pathc
- oldpathc
, sizeof(char *), compare
);
493 compare(const void *p
, const void *q
)
495 return(strcmp(*(char **)p
, *(char **)q
));
499 glob1(Char
*pattern
, glob_t
*pglob
)
501 Char pathbuf
[MAXPATHLEN
+1];
503 /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
506 return(glob2(pathbuf
, pathbuf
, pattern
, pglob
));
510 * The functions glob2 and glob3 are mutually recursive; there is one level
511 * of recursion for each segment in the pattern that contains one or more
515 glob2(Char
*pathbuf
, Char
*pathend
, Char
*pattern
, glob_t
*pglob
)
522 * Loop over pattern segments until end of pattern or until
523 * segment with meta character found.
525 for (anymeta
= 0;;) {
526 if (*pattern
== EOS
) { /* End of pattern? */
528 if (g_lstat(pathbuf
, &sb
, pglob
))
531 if (((pglob
->gl_flags
& GLOB_MARK
) &&
532 pathend
[-1] != SEP
) && (S_ISDIR(sb
.st_mode
)
533 || (S_ISLNK(sb
.st_mode
) &&
534 (g_stat(pathbuf
, &sb
, pglob
) == 0) &&
535 S_ISDIR(sb
.st_mode
)))) {
540 return(globextend(pathbuf
, pglob
));
543 /* Find end of next segment, copy tentatively to pathend. */
546 while (*p
!= EOS
&& *p
!= SEP
) {
552 if (!anymeta
) { /* No expansion, do next segment. */
555 while (*pattern
== SEP
)
556 *pathend
++ = *pattern
++;
557 } else /* Need expansion, recurse. */
558 return(glob3(pathbuf
, pathend
, pattern
, p
, pglob
));
564 glob3(Char
*pathbuf
, Char
*pathend
, Char
*pattern
, Char
*restpattern
, glob_t
*pglob
)
566 register struct dirent
*dp
;
569 char buf
[MAXPATHLEN
];
572 * The readdirfunc declaration can't be prototyped, because it is
573 * assigned, below, to two functions which are prototyped in glob.h
574 * and dirent.h as taking pointers to differently typed opaque
577 struct dirent
*(*readdirfunc
)();
582 if ((dirp
= g_opendir(pathbuf
, pglob
)) == NULL
) {
583 /* TODO: don't call for ENOENT or ENOTDIR? */
584 if (pglob
->gl_errfunc
) {
585 g_Ctoc(pathbuf
, buf
);
586 if (pglob
->gl_errfunc(buf
, errno
) ||
587 pglob
->gl_flags
& GLOB_ERR
)
595 /* Search directory for matching names. */
596 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
597 readdirfunc
= pglob
->gl_readdir
;
599 readdirfunc
= readdir
;
600 while ((dp
= (*readdirfunc
)(dirp
))) {
604 /* Initial DOT must be matched literally. */
605 if (dp
->d_name
[0] == DOT
&& *pattern
!= DOT
)
607 for (sc
= (u_char
*) dp
->d_name
, dc
= pathend
;
608 (*dc
++ = *sc
++) != EOS
;)
610 if (!match(pathend
, pattern
, restpattern
)) {
614 err
= glob2(pathbuf
, --dc
, restpattern
, pglob
);
619 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
620 (*pglob
->gl_closedir
)(dirp
);
628 * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
629 * add the new item, and update gl_pathc.
631 * This assumes the BSD realloc, which only copies the block when its size
632 * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
635 * Return 0 if new item added, error code if memory couldn't be allocated.
637 * Invariant of the glob_t structure:
638 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
639 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
642 globextend(const Char
*path
, glob_t
*pglob
)
644 register char **pathv
;
650 newsize
= sizeof(*pathv
) * (2 + pglob
->gl_pathc
+ pglob
->gl_offs
);
651 pathv
= pglob
->gl_pathv
?
652 realloc((char *)pglob
->gl_pathv
, newsize
) :
655 return(GLOB_NOSPACE
);
657 if (pglob
->gl_pathv
== NULL
&& pglob
->gl_offs
> 0) {
658 /* first time around -- clear initial gl_offs items */
659 pathv
+= pglob
->gl_offs
;
660 for (i
= pglob
->gl_offs
; --i
>= 0; )
663 pglob
->gl_pathv
= pathv
;
665 for (p
= path
; *p
++;)
667 if ((copy
= malloc(p
- path
)) != NULL
) {
669 pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
++] = copy
;
671 pathv
[pglob
->gl_offs
+ pglob
->gl_pathc
] = NULL
;
672 return(copy
== NULL
? GLOB_NOSPACE
: 0);
676 * pattern matching function for filenames. Each occurrence of the *
677 * pattern causes a recursion level.
680 match(Char
*name
, Char
*pat
, Char
*patend
)
682 int ok
, negate_range
;
685 while (pat
< patend
) {
687 switch (c
& M_MASK
) {
692 if (match(name
, pat
, patend
))
694 while (*name
++ != EOS
);
702 if ((k
= *name
++) == EOS
)
704 if ((negate_range
= ((*pat
& M_MASK
) == M_NOT
)) != EOS
)
706 while (((c
= *pat
++) & M_MASK
) != M_END
)
707 if ((*pat
& M_MASK
) == M_RNG
) {
708 if (c
<= k
&& k
<= pat
[1])
713 if (ok
== negate_range
)
722 return(*name
== EOS
);
725 /* Free allocated data belonging to a glob_t structure. */
727 globfree(glob_t
*pglob
)
732 if (pglob
->gl_pathv
!= NULL
) {
733 pp
= pglob
->gl_pathv
+ pglob
->gl_offs
;
734 for (i
= pglob
->gl_pathc
; i
--; ++pp
)
737 free(pglob
->gl_pathv
);
742 g_opendir(Char
*str
, glob_t
*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(Char
*fn
, struct stat
*sb
, glob_t
*pglob
)
760 char buf
[MAXPATHLEN
];
763 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
764 return((*pglob
->gl_lstat
)(buf
, sb
));
765 return(lstat(buf
, sb
));
769 g_stat(Char
*fn
, struct stat
*sb
, glob_t
*pglob
)
771 char buf
[MAXPATHLEN
];
774 if (pglob
->gl_flags
& GLOB_ALTDIRFUNC
)
775 return((*pglob
->gl_stat
)(buf
, sb
));
776 return(stat(buf
, sb
));
780 g_strchr(Char
*str
, int ch
)
791 g_strcat(Char
*dst
, const Char
*src
)
798 while((*dst
++ = *src
++) != EOS
)
806 g_Ctoc(const Char
*str
, char *buf
)
810 for (dc
= buf
; (*dc
++ = *str
++) != EOS
;)
816 qprintf(const char *str
, Char
*s
)
820 (void)printf("%s:\n", str
);
822 (void)printf("%c", CHAR(*p
));
825 (void)printf("%c", *p
& M_PROTECT
? '"' : ' ');
828 (void)printf("%c", ismeta(*p
) ? '_' : ' ');
833 #endif /* ! HAVE_GLOB */