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 <stelian@popies.net>, 1999-2000
6
* Stelian Pop <stelian@popies.net> - Alc�ve <www.alcove.com>, 2000-2002
10
* Copyright (c) 1989, 1993
11
* The Regents of the University of California. All rights reserved.
13
* This code is derived from software contributed to Berkeley by
16
* Redistribution and use in source and binary forms, with or without
17
* modification, are permitted provided that the following conditions
19
* 1. Redistributions of source code must retain the above copyright
20
* notice, this list of conditions and the following disclaimer.
21
* 2. Redistributions in binary form must reproduce the above copyright
22
* notice, this list of conditions and the following disclaimer in the
23
* documentation and/or other materials provided with the distribution.
24
* 3. All advertising materials mentioning features or use of this software
25
* must display the following acknowledgement:
26
* This product includes software developed by the University of
27
* California, Berkeley and its contributors.
28
* 4. Neither the name of the University nor the names of its contributors
29
* may be used to endorse or promote products derived from this software
30
* without specific prior written permission.
32
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46
* glob(3) -- a superset of the one defined in POSIX 1003.2.
48
* The [!...] convention to negate a range is supported (SysV, Posix, ksh).
50
* Optional extra services, controlled by flags not defined by POSIX:
53
* Escaping convention: \ inhibits any special meaning the following
54
* character might have (except \ at end of string is retained).
56
* Set in gl_flags if pattern contained a globbing character.
58
* Same as GLOB_NOCHECK, but it will only append pattern if it did
59
* not contain any magic characters. [Used in csh style globbing]
61
* Use alternately specified directory access functions.
63
* expand ~user/foo to the /home/dir/of/user/foo
65
* expand {1,2}{a,b} to 1a 1b 2a 2b
67
* Number of matches in the current invocation of glob.
71
static const char rcsid[] =
72
"$Id: compatglob.c,v 1.8 2002/01/25 15:08:59 stelian Exp $";
79
#include <sys/param.h>
85
#include <compatglob.h>
104
#define UNDERSCORE '_'
112
#define M_QUOTE 0x8000
113
#define M_PROTECT 0x4000
114
#define M_MASK 0xffff
115
#define M_ASCII 0x00ff
117
typedef u_short Char;
122
#define M_PROTECT 0x40
131
#define CHAR(c) ((Char)((c)&M_ASCII))
132
#define META(c) ((Char)((c)|M_QUOTE))
133
#define M_ALL META('*')
134
#define M_END META(']')
135
#define M_NOT META('!')
136
#define M_ONE META('?')
137
#define M_RNG META('-')
138
#define M_SET META('[')
139
#define ismeta(c) (((c)&M_QUOTE) != 0)
142
static int compare __P((const void *, const void *));
143
static void g_Ctoc __P((const Char *, char *));
144
static int g_lstat __P((Char *, struct stat *, glob_t *));
145
static DIR *g_opendir __P((Char *, glob_t *));
146
static Char *g_strchr __P((Char *, int));
148
static Char *g_strcat __P((Char *, const Char *));
150
static int g_stat __P((Char *, struct stat *, glob_t *));
151
static int glob0 __P((const Char *, glob_t *));
152
static int glob1 __P((Char *, glob_t *));
153
static int glob2 __P((Char *, Char *, Char *, glob_t *));
154
static int glob3 __P((Char *, Char *, Char *, Char *, glob_t *));
155
static int globextend __P((const Char *, glob_t *));
156
static const Char * globtilde __P((const Char *, Char *, size_t, glob_t *));
157
static int globexp1 __P((const Char *, glob_t *));
158
static int globexp2 __P((const Char *, const Char *, glob_t *, int *));
159
static int match __P((Char *, Char *, Char *));
161
static void qprintf __P((const char *, Char *));
165
glob(const char *pattern, int flags, int (*errfunc) __P((const char *, int)), glob_t *pglob)
167
const u_char *patnext;
169
Char *bufnext, *bufend, patbuf[MAXPATHLEN+1];
171
patnext = (u_char *) pattern;
172
if (!(flags & GLOB_APPEND)) {
174
pglob->gl_pathv = NULL;
175
if (!(flags & GLOB_DOOFFS))
178
pglob->gl_flags = flags & ~GLOB_MAGCHAR;
179
pglob->gl_errfunc = errfunc;
180
pglob->gl_matchc = 0;
183
bufend = bufnext + MAXPATHLEN;
184
if (flags & GLOB_QUOTE) {
185
/* Protect the quoted characters. */
186
while (bufnext < bufend && (c = *patnext++) != EOS)
188
if ((c = *patnext++) == EOS) {
192
*bufnext++ = c | M_PROTECT;
198
while (bufnext < bufend && (c = *patnext++) != EOS)
202
if (flags & GLOB_BRACE)
203
return globexp1(patbuf, pglob);
205
return glob0(patbuf, pglob);
209
* Expand recursively a glob {} pattern. When there is no more expansion
210
* invoke the standard globbing routine to glob the rest of the magic
213
static int globexp1(const Char *pattern, glob_t *pglob)
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);
222
while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL)
223
if (!globexp2(ptr, pattern, pglob, &rv))
226
return glob0(pattern, pglob);
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.
235
static int globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv)
239
const Char *pe, *pm, *pl;
240
Char patbuf[MAXPATHLEN + 1];
242
/* copy part up to the brace */
243
for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
247
/* Find the balanced brace */
248
for (i = 0, pe = ++ptr; *pe; pe++)
249
if (*pe == LBRACKET) {
250
/* Ignore everything between [] */
251
for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
255
* We could not find a matching RBRACKET.
256
* Ignore and just look for RBRACE
261
else if (*pe == LBRACE)
263
else if (*pe == RBRACE) {
269
/* Non matching braces; just glob the pattern */
270
if (i != 0 || *pe == EOS) {
271
*rv = glob0(patbuf, pglob);
275
for (i = 0, pl = pm = ptr; pm <= pe; pm++)
278
/* Ignore everything between [] */
279
for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++)
283
* We could not find a matching RBRACKET.
284
* Ignore and just look for RBRACE
301
if (i && *pm == COMMA)
304
/* Append the current string */
305
for (lm = ls; (pl < pm); *lm++ = *pl++)
308
* Append the rest of the pattern after the
311
for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
314
/* Expand the current pattern */
316
qprintf("globexp2:", patbuf);
318
*rv = globexp1(patbuf, pglob);
320
/* move after the comma, to the next string */
335
* expand tilde from the passwd file.
338
globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
345
if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
349
* Copy up to the end of the string or /
351
eb = &patbuf[patbuf_len - 1];
352
for (p = pattern + 1, h = (char *) patbuf;
353
h < (char *)eb && *p && *p != SLASH; *h++ = *p++)
358
if (((char *) patbuf)[0] == EOS) {
360
* handle a plain ~ or ~/ by expanding $HOME first if
361
* we're not running setuid or setgid) and then trying
366
#ifndef __NETBSD_SYSCALLS
370
(h = getenv("HOME")) == NULL) {
371
if (((h = getlogin()) != NULL &&
372
(pwd = getpwnam(h)) != NULL) ||
373
(pwd = getpwuid(getuid())) != NULL)
383
if ((pwd = getpwnam((char*) patbuf)) == NULL)
389
/* Copy the home directory */
390
for (b = patbuf; b < eb && *h; *b++ = *h++)
393
/* Append the rest of the pattern */
394
while (b < eb && (*b++ = *p++) != EOS)
403
* The main glob() routine: compiles the pattern (optionally processing
404
* quotes), calls glob1() to do the real pattern matching, and finally
405
* sorts the list (unless unsorted operation is requested). Returns 0
406
* if things went well, nonzero if errors occurred. It is not an error
407
* to find no matches.
410
glob0(const Char *pattern, glob_t *pglob)
412
const Char *qpatnext;
413
int c, err, oldpathc;
414
Char *bufnext, patbuf[MAXPATHLEN+1];
416
qpatnext = globtilde(pattern, patbuf, sizeof(patbuf) / sizeof(Char),
418
oldpathc = pglob->gl_pathc;
421
/* We don't need to check for buffer overflow any more. */
422
while ((c = *qpatnext++) != EOS) {
428
if (*qpatnext == EOS ||
429
g_strchr((Char *) qpatnext+1, RBRACKET) == NULL) {
430
*bufnext++ = LBRACKET;
440
*bufnext++ = CHAR(c);
441
if (*qpatnext == RANGE &&
442
(c = qpatnext[1]) != RBRACKET) {
444
*bufnext++ = CHAR(c);
447
} while ((c = *qpatnext++) != RBRACKET);
448
pglob->gl_flags |= GLOB_MAGCHAR;
452
pglob->gl_flags |= GLOB_MAGCHAR;
456
pglob->gl_flags |= GLOB_MAGCHAR;
457
/* collapse adjacent stars to one,
458
* to avoid exponential behavior
460
if (bufnext == patbuf || bufnext[-1] != M_ALL)
464
*bufnext++ = CHAR(c);
470
qprintf("glob0:", patbuf);
473
if ((err = glob1(patbuf, pglob)) != 0)
477
* If there was no match we are going to append the pattern
478
* if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
479
* and the pattern did not contain any magic characters
480
* GLOB_NOMAGIC is there just for compatibility with csh.
482
if (pglob->gl_pathc == oldpathc &&
483
((pglob->gl_flags & GLOB_NOCHECK) ||
484
((pglob->gl_flags & GLOB_NOMAGIC) &&
485
!(pglob->gl_flags & GLOB_MAGCHAR))))
486
return(globextend(pattern, pglob));
487
else if (!(pglob->gl_flags & GLOB_NOSORT))
488
qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
489
pglob->gl_pathc - oldpathc, sizeof(char *), compare);
494
compare(const void *p, const void *q)
496
return(strcmp(*(char **)p, *(char **)q));
500
glob1(Char *pattern, glob_t *pglob)
502
Char pathbuf[MAXPATHLEN+1];
504
/* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
507
return(glob2(pathbuf, pathbuf, pattern, pglob));
511
* The functions glob2 and glob3 are mutually recursive; there is one level
512
* of recursion for each segment in the pattern that contains one or more
516
glob2(Char *pathbuf, Char *pathend, Char *pattern, glob_t *pglob)
523
* Loop over pattern segments until end of pattern or until
524
* segment with meta character found.
526
for (anymeta = 0;;) {
527
if (*pattern == EOS) { /* End of pattern? */
529
if (g_lstat(pathbuf, &sb, pglob))
532
if (((pglob->gl_flags & GLOB_MARK) &&
533
pathend[-1] != SEP) && (S_ISDIR(sb.st_mode)
534
|| (S_ISLNK(sb.st_mode) &&
535
(g_stat(pathbuf, &sb, pglob) == 0) &&
536
S_ISDIR(sb.st_mode)))) {
541
return(globextend(pathbuf, pglob));
544
/* Find end of next segment, copy tentatively to pathend. */
547
while (*p != EOS && *p != SEP) {
553
if (!anymeta) { /* No expansion, do next segment. */
556
while (*pattern == SEP)
557
*pathend++ = *pattern++;
558
} else /* Need expansion, recurse. */
559
return(glob3(pathbuf, pathend, pattern, p, pglob));
565
glob3(Char *pathbuf, Char *pathend, Char *pattern, Char *restpattern, glob_t *pglob)
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(const Char *path, glob_t *pglob)
651
newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
652
pathv = pglob->gl_pathv ?
653
realloc((char *)pglob->gl_pathv, newsize) :
656
return(GLOB_NOSPACE);
658
if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
659
/* first time around -- clear initial gl_offs items */
660
pathv += pglob->gl_offs;
661
for (i = pglob->gl_offs; --i >= 0; )
664
pglob->gl_pathv = pathv;
666
for (p = path; *p++;)
668
if ((copy = malloc(p - path)) != NULL) {
670
pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
672
pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
673
return(copy == NULL ? GLOB_NOSPACE : 0);
677
* pattern matching function for filenames. Each occurrence of the *
678
* pattern causes a recursion level.
681
match(Char *name, Char *pat, Char *patend)
683
int ok, negate_range;
686
while (pat < patend) {
688
switch (c & M_MASK) {
693
if (match(name, pat, patend))
695
while (*name++ != EOS);
703
if ((k = *name++) == EOS)
705
if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
707
while (((c = *pat++) & M_MASK) != M_END)
708
if ((*pat & M_MASK) == M_RNG) {
709
if (c <= k && k <= pat[1])
714
if (ok == negate_range)
723
return(*name == EOS);
726
/* Free allocated data belonging to a glob_t structure. */
728
globfree(glob_t *pglob)
733
if (pglob->gl_pathv != NULL) {
734
pp = pglob->gl_pathv + pglob->gl_offs;
735
for (i = pglob->gl_pathc; i--; ++pp)
738
free(pglob->gl_pathv);
743
g_opendir(Char *str, glob_t *pglob)
745
char buf[MAXPATHLEN];
752
if (pglob->gl_flags & GLOB_ALTDIRFUNC)
753
return((*pglob->gl_opendir)(buf));
755
return(opendir(buf));
759
g_lstat(Char *fn, struct stat *sb, glob_t *pglob)
761
char buf[MAXPATHLEN];
764
if (pglob->gl_flags & GLOB_ALTDIRFUNC)
765
return((*pglob->gl_lstat)(buf, sb));
766
return(lstat(buf, sb));
770
g_stat(Char *fn, struct stat *sb, glob_t *pglob)
772
char buf[MAXPATHLEN];
775
if (pglob->gl_flags & GLOB_ALTDIRFUNC)
776
return((*pglob->gl_stat)(buf, sb));
777
return(stat(buf, sb));
781
g_strchr(Char *str, int ch)
792
g_strcat(Char *dst, const Char *src)
799
while((*dst++ = *src++) != EOS)
807
g_Ctoc(const Char *str, char *buf)
811
for (dc = buf; (*dc++ = *str++) != EOS;)
817
qprintf(const char *str, Char *s)
821
(void)printf("%s:\n", str);
823
(void)printf("%c", CHAR(*p));
826
(void)printf("%c", *p & M_PROTECT ? '"' : ' ');
829
(void)printf("%c", ismeta(*p) ? '_' : ' ');
834
#endif /* ! HAVE_GLOB */