version 1.12, 2005/09/13 01:44:09 |
version 1.30, 2011/05/14 22:44:06 |
Line 59 __RCSID("$NetBSD$"); |
|
Line 59 __RCSID("$NetBSD$"); |
|
* expand ~user/foo to the /home/dir/of/user/foo |
* expand ~user/foo to the /home/dir/of/user/foo |
* GLOB_BRACE: |
* GLOB_BRACE: |
* expand {1,2}{a,b} to 1a 1b 2a 2b |
* expand {1,2}{a,b} to 1a 1b 2a 2b |
|
* GLOB_PERIOD: |
|
* allow metacharacters to match leading dots in filenames. |
|
* GLOB_NO_DOTDIRS: |
|
* . and .. are hidden from wildcards, even if GLOB_PERIOD is set. |
* gl_matchc: |
* gl_matchc: |
* Number of matches in the current invocation of glob. |
* Number of matches in the current invocation of glob. |
*/ |
*/ |
Line 74 __RCSID("$NetBSD$"); |
|
Line 78 __RCSID("$NetBSD$"); |
|
#include <glob.h> |
#include <glob.h> |
#include <pwd.h> |
#include <pwd.h> |
#include <stdio.h> |
#include <stdio.h> |
|
#include <stddef.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
#include <unistd.h> |
#include <unistd.h> |
Line 82 __RCSID("$NetBSD$"); |
|
Line 87 __RCSID("$NetBSD$"); |
|
#define NO_GETPW_R |
#define NO_GETPW_R |
#endif |
#endif |
|
|
|
#define GLOB_LIMIT_STRING 65536 /* number of readdirs */ |
|
#define GLOB_LIMIT_STAT 128 /* number of stat system calls */ |
|
#define GLOB_LIMIT_READDIR 16384 /* total buffer size of path strings */ |
|
#define GLOB_LIMIT_PATH 1024 /* number of path elements */ |
|
#define GLOB_LIMIT_BRACE 128 /* Number of brace calls */ |
|
|
|
struct glob_limit { |
|
size_t l_string; |
|
size_t l_stat; |
|
size_t l_readdir; |
|
size_t l_brace; |
|
}; |
|
|
/* |
/* |
* XXX: For NetBSD 1.4.x compatibility. (kill me l8r) |
* XXX: For NetBSD 1.4.x compatibility. (kill me l8r) |
*/ |
*/ |
Line 107 __RCSID("$NetBSD$"); |
|
Line 125 __RCSID("$NetBSD$"); |
|
#define SLASH '/' |
#define SLASH '/' |
#define COMMA ',' |
#define COMMA ',' |
|
|
#ifndef DEBUG |
#ifndef USE_8BIT_CHARS |
|
|
#define M_QUOTE 0x8000 |
#define M_QUOTE 0x8000 |
#define M_PROTECT 0x4000 |
#define M_PROTECT 0x4000 |
Line 118 typedef u_short Char; |
|
Line 136 typedef u_short Char; |
|
|
|
#else |
#else |
|
|
#define M_QUOTE 0x80 |
#define M_QUOTE (Char)0x80 |
#define M_PROTECT 0x40 |
#define M_PROTECT (Char)0x40 |
#define M_MASK 0xff |
#define M_MASK (Char)0xff |
#define M_ASCII 0x7f |
#define M_ASCII (Char)0x7f |
|
|
typedef char Char; |
typedef char Char; |
|
|
Line 139 typedef char Char; |
|
Line 157 typedef char Char; |
|
#define ismeta(c) (((c)&M_QUOTE) != 0) |
#define ismeta(c) (((c)&M_QUOTE) != 0) |
|
|
|
|
static int compare __P((const void *, const void *)); |
static int compare(const void *, const void *); |
static int g_Ctoc __P((const Char *, char *, size_t)); |
static int g_Ctoc(const Char *, char *, size_t); |
static int g_lstat __P((Char *, __gl_stat_t *, glob_t *)); |
static int g_lstat(Char *, __gl_stat_t *, glob_t *); |
static DIR *g_opendir __P((Char *, glob_t *)); |
static DIR *g_opendir(Char *, glob_t *); |
static Char *g_strchr __P((const Char *, int)); |
static Char *g_strchr(const Char *, int); |
static int g_stat __P((Char *, __gl_stat_t *, glob_t *)); |
static int g_stat(Char *, __gl_stat_t *, glob_t *); |
static int glob0 __P((const Char *, glob_t *)); |
static int glob0(const Char *, glob_t *, struct glob_limit *); |
static int glob1 __P((Char *, glob_t *, size_t *)); |
static int glob1(Char *, glob_t *, struct glob_limit *); |
static int glob2 __P((Char *, Char *, Char *, Char *, glob_t *, |
static int glob2(Char *, Char *, Char *, const Char *, glob_t *, |
size_t *)); |
struct glob_limit *); |
static int glob3 __P((Char *, Char *, Char *, Char *, Char *, glob_t *, |
static int glob3(Char *, Char *, Char *, const Char *, const Char *, |
size_t *)); |
const Char *, glob_t *, struct glob_limit *); |
static int globextend __P((const Char *, glob_t *, size_t *)); |
static int globextend(const Char *, glob_t *, struct glob_limit *); |
static const Char *globtilde __P((const Char *, Char *, size_t, glob_t *)); |
static const Char *globtilde(const Char *, Char *, size_t, glob_t *); |
static int globexp1 __P((const Char *, glob_t *)); |
static int globexp1(const Char *, glob_t *, struct glob_limit *); |
static int globexp2 __P((const Char *, const Char *, glob_t *, int *)); |
static int globexp2(const Char *, const Char *, glob_t *, int *, |
static int match __P((Char *, Char *, Char *)); |
struct glob_limit *); |
|
static int match(const Char *, const Char *, const Char *); |
#ifdef DEBUG |
#ifdef DEBUG |
static void qprintf __P((const char *, Char *)); |
static void qprintf(const char *, Char *); |
#endif |
#endif |
|
|
int |
int |
glob(pattern, flags, errfunc, pglob) |
glob(const char *pattern, int flags, int (*errfunc)(const char *, int), |
const char *pattern; |
glob_t *pglob) |
int flags, (*errfunc) __P((const char *, int)); |
|
glob_t *pglob; |
|
{ |
{ |
const u_char *patnext; |
const u_char *patnext; |
int c; |
int c; |
Char *bufnext, *bufend, patbuf[MAXPATHLEN+1]; |
Char *bufnext, *bufend, patbuf[MAXPATHLEN+1]; |
|
struct glob_limit limit = { 0, 0, 0, 0 }; |
|
|
_DIAGASSERT(pattern != NULL); |
_DIAGASSERT(pattern != NULL); |
|
|
Line 204 glob(pattern, flags, errfunc, pglob) |
|
Line 222 glob(pattern, flags, errfunc, pglob) |
|
*bufnext = EOS; |
*bufnext = EOS; |
|
|
if (flags & GLOB_BRACE) |
if (flags & GLOB_BRACE) |
return globexp1(patbuf, pglob); |
return globexp1(patbuf, pglob, &limit); |
else |
else |
return glob0(patbuf, pglob); |
return glob0(patbuf, pglob, &limit); |
} |
} |
|
|
/* |
/* |
Line 215 glob(pattern, flags, errfunc, pglob) |
|
Line 233 glob(pattern, flags, errfunc, pglob) |
|
* characters |
* characters |
*/ |
*/ |
static int |
static int |
globexp1(pattern, pglob) |
globexp1(const Char *pattern, glob_t *pglob, struct glob_limit *limit) |
const Char *pattern; |
|
glob_t *pglob; |
|
{ |
{ |
const Char* ptr = pattern; |
const Char* ptr = pattern; |
int rv; |
int rv; |
Line 225 globexp1(pattern, pglob) |
|
Line 241 globexp1(pattern, pglob) |
|
_DIAGASSERT(pattern != NULL); |
_DIAGASSERT(pattern != NULL); |
_DIAGASSERT(pglob != NULL); |
_DIAGASSERT(pglob != NULL); |
|
|
|
if ((pglob->gl_flags & GLOB_LIMIT) && |
|
limit->l_brace++ >= GLOB_LIMIT_BRACE) { |
|
errno = 0; |
|
return GLOB_NOSPACE; |
|
} |
|
|
/* Protect a single {}, for find(1), like csh */ |
/* Protect a single {}, for find(1), like csh */ |
if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS) |
if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS) |
return glob0(pattern, pglob); |
return glob0(pattern, pglob, limit); |
|
|
while ((ptr = (const Char *) g_strchr(ptr, LBRACE)) != NULL) |
while ((ptr = (const Char *) g_strchr(ptr, LBRACE)) != NULL) |
if (!globexp2(ptr, pattern, pglob, &rv)) |
if (!globexp2(ptr, pattern, pglob, &rv, limit)) |
return rv; |
return rv; |
|
|
return glob0(pattern, pglob); |
return glob0(pattern, pglob, limit); |
} |
} |
|
|
|
|
Line 243 globexp1(pattern, pglob) |
|
Line 265 globexp1(pattern, pglob) |
|
* If it fails then it tries to glob the rest of the pattern and returns. |
* If it fails then it tries to glob the rest of the pattern and returns. |
*/ |
*/ |
static int |
static int |
globexp2(ptr, pattern, pglob, rv) |
globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv, |
const Char *ptr, *pattern; |
struct glob_limit *limit) |
glob_t *pglob; |
|
int *rv; |
|
{ |
{ |
int i; |
int i; |
Char *lm, *ls; |
Char *lm, *ls; |
Line 291 globexp2(ptr, pattern, pglob, rv) |
|
Line 311 globexp2(ptr, pattern, pglob, rv) |
|
* we use `pattern', not `patbuf' here so that that |
* we use `pattern', not `patbuf' here so that that |
* unbalanced braces are passed to the match |
* unbalanced braces are passed to the match |
*/ |
*/ |
*rv = glob0(pattern, pglob); |
*rv = glob0(pattern, pglob, limit); |
return 0; |
return 0; |
} |
} |
|
|
Line 336 globexp2(ptr, pattern, pglob, rv) |
|
Line 356 globexp2(ptr, pattern, pglob, rv) |
|
|
|
/* Expand the current pattern */ |
/* Expand the current pattern */ |
#ifdef DEBUG |
#ifdef DEBUG |
qprintf("globexp2:", patbuf); |
qprintf("globexp2", patbuf); |
#endif |
#endif |
*rv = globexp1(patbuf, pglob); |
*rv = globexp1(patbuf, pglob, limit); |
|
|
/* move after the comma, to the next string */ |
/* move after the comma, to the next string */ |
pl = pm + 1; |
pl = pm + 1; |
Line 359 globexp2(ptr, pattern, pglob, rv) |
|
Line 379 globexp2(ptr, pattern, pglob, rv) |
|
* expand tilde from the passwd file. |
* expand tilde from the passwd file. |
*/ |
*/ |
static const Char * |
static const Char * |
globtilde(pattern, patbuf, patsize, pglob) |
globtilde(const Char *pattern, Char *patbuf, size_t patsize, glob_t *pglob) |
const Char *pattern; |
|
Char *patbuf; |
|
size_t patsize; |
|
glob_t *pglob; |
|
{ |
{ |
struct passwd *pwd; |
struct passwd *pwd; |
const char *h; |
const char *h; |
Line 455 globtilde(pattern, patbuf, patsize, pglo |
|
Line 471 globtilde(pattern, patbuf, patsize, pglo |
|
* to find no matches. |
* to find no matches. |
*/ |
*/ |
static int |
static int |
glob0(pattern, pglob) |
glob0(const Char *pattern, glob_t *pglob, struct glob_limit *limit) |
const Char *pattern; |
|
glob_t *pglob; |
|
{ |
{ |
const Char *qpatnext; |
const Char *qpatnext; |
int c, error, oldpathc; |
int c, error; |
|
__gl_size_t oldpathc; |
Char *bufnext, patbuf[MAXPATHLEN+1]; |
Char *bufnext, patbuf[MAXPATHLEN+1]; |
size_t limit = 0; |
|
|
|
_DIAGASSERT(pattern != NULL); |
_DIAGASSERT(pattern != NULL); |
_DIAGASSERT(pglob != NULL); |
_DIAGASSERT(pglob != NULL); |
Line 509 glob0(pattern, pglob) |
|
Line 523 glob0(pattern, pglob) |
|
break; |
break; |
case STAR: |
case STAR: |
pglob->gl_flags |= GLOB_MAGCHAR; |
pglob->gl_flags |= GLOB_MAGCHAR; |
/* collapse adjacent stars to one, |
/* collapse adjacent stars to one [or three if globstar] |
* to avoid exponential behavior |
* to avoid exponential behavior |
*/ |
*/ |
if (bufnext == patbuf || bufnext[-1] != M_ALL) |
if (bufnext == patbuf || bufnext[-1] != M_ALL || |
|
((pglob->gl_flags & GLOB_STAR) != 0 && |
|
(bufnext - 1 == patbuf || bufnext[-2] != M_ALL || |
|
bufnext - 2 == patbuf || bufnext[-3] != M_ALL))) |
*bufnext++ = M_ALL; |
*bufnext++ = M_ALL; |
break; |
break; |
default: |
default: |
Line 522 glob0(pattern, pglob) |
|
Line 539 glob0(pattern, pglob) |
|
} |
} |
*bufnext = EOS; |
*bufnext = EOS; |
#ifdef DEBUG |
#ifdef DEBUG |
qprintf("glob0:", patbuf); |
qprintf("glob0", patbuf); |
#endif |
#endif |
|
|
if ((error = glob1(patbuf, pglob, &limit)) != 0) |
if ((error = glob1(patbuf, pglob, limit)) != 0) |
return(error); |
return error; |
|
|
if (pglob->gl_pathc == oldpathc) { |
if (pglob->gl_pathc == oldpathc) { |
/* |
/* |
Line 539 glob0(pattern, pglob) |
|
Line 556 glob0(pattern, pglob) |
|
if ((pglob->gl_flags & GLOB_NOCHECK) || |
if ((pglob->gl_flags & GLOB_NOCHECK) || |
((pglob->gl_flags & (GLOB_NOMAGIC|GLOB_MAGCHAR)) |
((pglob->gl_flags & (GLOB_NOMAGIC|GLOB_MAGCHAR)) |
== GLOB_NOMAGIC)) { |
== GLOB_NOMAGIC)) { |
return globextend(pattern, pglob, &limit); |
return globextend(pattern, pglob, limit); |
} else { |
} else { |
return (GLOB_NOMATCH); |
return GLOB_NOMATCH; |
} |
} |
} else if (!(pglob->gl_flags & GLOB_NOSORT)) { |
} else if (!(pglob->gl_flags & GLOB_NOSORT)) { |
qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc, |
qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc, |
Line 549 glob0(pattern, pglob) |
|
Line 566 glob0(pattern, pglob) |
|
compare); |
compare); |
} |
} |
|
|
return(0); |
return 0; |
} |
} |
|
|
static int |
static int |
compare(p, q) |
compare(const void *p, const void *q) |
const void *p, *q; |
|
{ |
{ |
|
|
_DIAGASSERT(p != NULL); |
_DIAGASSERT(p != NULL); |
_DIAGASSERT(q != NULL); |
_DIAGASSERT(q != NULL); |
|
|
return(strcoll(*(const char * const *)p, *(const char * const *)q)); |
return strcoll(*(const char * const *)p, *(const char * const *)q); |
} |
} |
|
|
static int |
static int |
glob1(pattern, pglob, limit) |
glob1(Char *pattern, glob_t *pglob, struct glob_limit *limit) |
Char *pattern; |
|
glob_t *pglob; |
|
size_t *limit; |
|
{ |
{ |
Char pathbuf[MAXPATHLEN+1]; |
Char pathbuf[MAXPATHLEN+1]; |
|
|
Line 576 glob1(pattern, pglob, limit) |
|
Line 589 glob1(pattern, pglob, limit) |
|
|
|
/* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */ |
/* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */ |
if (*pattern == EOS) |
if (*pattern == EOS) |
return(0); |
return 0; |
/* |
/* |
* we save one character so that we can use ptr >= limit, |
* we save one character so that we can use ptr >= limit, |
* in the general case when we are appending non nul chars only. |
* in the general case when we are appending non nul chars only. |
*/ |
*/ |
return(glob2(pathbuf, pathbuf, pathbuf + sizeof(pathbuf) - 1, pattern, |
return glob2(pathbuf, pathbuf, |
pglob, limit)); |
pathbuf + (sizeof(pathbuf) / sizeof(*pathbuf)) - 1, pattern, |
|
pglob, limit); |
} |
} |
|
|
/* |
/* |
Line 591 glob1(pattern, pglob, limit) |
|
Line 605 glob1(pattern, pglob, limit) |
|
* meta characters. |
* meta characters. |
*/ |
*/ |
static int |
static int |
glob2(pathbuf, pathend, pathlim, pattern, pglob, limit) |
glob2(Char *pathbuf, Char *pathend, Char *pathlim, const Char *pattern, |
Char *pathbuf, *pathend, *pathlim, *pattern; |
glob_t *pglob, struct glob_limit *limit) |
glob_t *pglob; |
|
size_t *limit; |
|
{ |
{ |
__gl_stat_t sb; |
__gl_stat_t sb; |
Char *p, *q; |
const Char *p; |
|
Char *q; |
int anymeta; |
int anymeta; |
|
Char *pend; |
|
ptrdiff_t diff; |
|
|
_DIAGASSERT(pathbuf != NULL); |
_DIAGASSERT(pathbuf != NULL); |
_DIAGASSERT(pathend != NULL); |
_DIAGASSERT(pathend != NULL); |
_DIAGASSERT(pattern != NULL); |
_DIAGASSERT(pattern != NULL); |
_DIAGASSERT(pglob != NULL); |
_DIAGASSERT(pglob != NULL); |
|
|
|
#ifdef DEBUG |
|
qprintf("glob2", pathbuf); |
|
#endif |
/* |
/* |
* Loop over pattern segments until end of pattern or until |
* Loop over pattern segments until end of pattern or until |
* segment with meta character found. |
* segment with meta character found. |
Line 613 glob2(pathbuf, pathend, pathlim, pattern |
|
Line 631 glob2(pathbuf, pathend, pathlim, pattern |
|
if (*pattern == EOS) { /* End of pattern? */ |
if (*pattern == EOS) { /* End of pattern? */ |
*pathend = EOS; |
*pathend = EOS; |
if (g_lstat(pathbuf, &sb, pglob)) |
if (g_lstat(pathbuf, &sb, pglob)) |
return(0); |
return 0; |
|
|
|
if ((pglob->gl_flags & GLOB_LIMIT) && |
|
limit->l_stat++ >= GLOB_LIMIT_STAT) { |
|
errno = 0; |
|
*pathend++ = SEP; |
|
*pathend = EOS; |
|
return GLOB_NOSPACE; |
|
} |
if (((pglob->gl_flags & GLOB_MARK) && |
if (((pglob->gl_flags & GLOB_MARK) && |
pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) || |
pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) || |
(S_ISLNK(sb.st_mode) && |
(S_ISLNK(sb.st_mode) && |
(g_stat(pathbuf, &sb, pglob) == 0) && |
(g_stat(pathbuf, &sb, pglob) == 0) && |
S_ISDIR(sb.st_mode)))) { |
S_ISDIR(sb.st_mode)))) { |
if (pathend >= pathlim) |
if (pathend >= pathlim) |
return (GLOB_ABORTED); |
return GLOB_ABORTED; |
*pathend++ = SEP; |
*pathend++ = SEP; |
*pathend = EOS; |
*pathend = EOS; |
} |
} |
++pglob->gl_matchc; |
++pglob->gl_matchc; |
return(globextend(pathbuf, pglob, limit)); |
return globextend(pathbuf, pglob, limit); |
} |
} |
|
|
/* Find end of next segment, copy tentatively to pathend. */ |
/* Find end of next segment, copy tentatively to pathend. */ |
Line 640 glob2(pathbuf, pathend, pathlim, pattern |
|
Line 665 glob2(pathbuf, pathend, pathlim, pattern |
|
*q++ = *p++; |
*q++ = *p++; |
} |
} |
|
|
if (!anymeta) { /* No expansion, do next segment. */ |
/* |
|
* No expansion, or path ends in slash-dot shash-dot-dot, |
|
* do next segment. |
|
*/ |
|
if (pglob->gl_flags & GLOB_PERIOD) { |
|
for (pend = pathend; pend > pathbuf && pend[-1] == '/'; |
|
pend--) |
|
continue; |
|
diff = pend - pathbuf; |
|
} else { |
|
/* XXX: GCC */ |
|
diff = 0; |
|
pend = pathend; |
|
} |
|
|
|
if ((!anymeta) || |
|
((pglob->gl_flags & GLOB_PERIOD) && |
|
(diff >= 1 && pend[-1] == DOT) && |
|
(diff >= 2 && (pend[-2] == SLASH || pend[-2] == DOT)) && |
|
(diff < 3 || pend[-3] == SLASH))) { |
pathend = q; |
pathend = q; |
pattern = p; |
pattern = p; |
while (*pattern == SEP) { |
while (*pattern == SEP) { |
Line 649 glob2(pathbuf, pathend, pathlim, pattern |
|
Line 693 glob2(pathbuf, pathend, pathlim, pattern |
|
*pathend++ = *pattern++; |
*pathend++ = *pattern++; |
} |
} |
} else /* Need expansion, recurse. */ |
} else /* Need expansion, recurse. */ |
return(glob3(pathbuf, pathend, pathlim, pattern, p, |
return glob3(pathbuf, pathend, pathlim, pattern, p, |
pglob, limit)); |
pattern, pglob, limit); |
} |
} |
/* NOTREACHED */ |
/* NOTREACHED */ |
} |
} |
|
|
static int |
static int |
glob3(pathbuf, pathend, pathlim, pattern, restpattern, pglob, limit) |
glob3(Char *pathbuf, Char *pathend, Char *pathlim, const Char *pattern, |
Char *pathbuf, *pathend, *pathlim, *pattern, *restpattern; |
const Char *restpattern, const Char *pglobstar, glob_t *pglob, |
glob_t *pglob; |
struct glob_limit *limit) |
size_t *limit; |
|
{ |
{ |
struct dirent *dp; |
struct dirent *dp; |
DIR *dirp; |
DIR *dirp; |
|
__gl_stat_t sbuf; |
int error; |
int error; |
char buf[MAXPATHLEN]; |
char buf[MAXPATHLEN]; |
|
int globstar = 0; |
|
int chase_symlinks = 0; |
|
const Char *termstar = NULL; |
|
|
/* |
/* |
* The readdirfunc declaration can't be prototyped, because it is |
* The readdirfunc declaration can't be prototyped, because it is |
Line 672 glob3(pathbuf, pathend, pathlim, pattern |
|
Line 719 glob3(pathbuf, pathend, pathlim, pattern |
|
* and dirent.h as taking pointers to differently typed opaque |
* and dirent.h as taking pointers to differently typed opaque |
* structures. |
* structures. |
*/ |
*/ |
struct dirent *(*readdirfunc) __P((void *)); |
struct dirent *(*readdirfunc)(void *); |
|
|
_DIAGASSERT(pathbuf != NULL); |
_DIAGASSERT(pathbuf != NULL); |
_DIAGASSERT(pathend != NULL); |
_DIAGASSERT(pathend != NULL); |
Line 683 glob3(pathbuf, pathend, pathlim, pattern |
|
Line 730 glob3(pathbuf, pathend, pathlim, pattern |
|
*pathend = EOS; |
*pathend = EOS; |
errno = 0; |
errno = 0; |
|
|
|
while (pglobstar < restpattern) { |
|
if ((pglobstar[0] & M_MASK) == M_ALL && |
|
(pglobstar[1] & M_MASK) == M_ALL) { |
|
globstar = 1; |
|
chase_symlinks = (pglobstar[2] & M_MASK) == M_ALL; |
|
termstar = pglobstar + (2 + chase_symlinks); |
|
break; |
|
} |
|
pglobstar++; |
|
} |
|
|
|
if (globstar) { |
|
error = pglobstar == pattern && termstar == restpattern ? |
|
*restpattern == EOS ? |
|
glob2(pathbuf, pathend, pathlim, restpattern - 1, pglob, |
|
limit) : |
|
glob2(pathbuf, pathend, pathlim, restpattern + 1, pglob, |
|
limit) : |
|
glob3(pathbuf, pathend, pathlim, pattern, restpattern, |
|
termstar, pglob, limit); |
|
if (error) |
|
return error; |
|
*pathend = EOS; |
|
} |
|
|
|
if (*pathbuf && (g_lstat(pathbuf, &sbuf, pglob) || |
|
!S_ISDIR(sbuf.st_mode) |
|
#ifdef S_IFLINK |
|
&& ((globstar && !chase_symlinks) || !S_ISLNK(sbuf.st_mode)) |
|
#endif |
|
)) |
|
return 0; |
|
|
if ((dirp = g_opendir(pathbuf, pglob)) == NULL) { |
if ((dirp = g_opendir(pathbuf, pglob)) == NULL) { |
if (pglob->gl_errfunc) { |
if (pglob->gl_errfunc) { |
if (g_Ctoc(pathbuf, buf, sizeof(buf))) |
if (g_Ctoc(pathbuf, buf, sizeof(buf))) |
return (GLOB_ABORTED); |
return GLOB_ABORTED; |
if (pglob->gl_errfunc(buf, errno) || |
if (pglob->gl_errfunc(buf, errno) || |
pglob->gl_flags & GLOB_ERR) |
pglob->gl_flags & GLOB_ERR) |
return (GLOB_ABORTED); |
return GLOB_ABORTED; |
} |
} |
/* |
/* |
* Posix/XOpen: glob should return when it encounters a |
* Posix/XOpen: glob should return when it encounters a |
Line 698 glob3(pathbuf, pathend, pathlim, pattern |
|
Line 778 glob3(pathbuf, pathend, pathlim, pattern |
|
* I think that Posix had in mind EPERM... |
* I think that Posix had in mind EPERM... |
*/ |
*/ |
if (pglob->gl_flags & GLOB_ERR) |
if (pglob->gl_flags & GLOB_ERR) |
return (GLOB_ABORTED); |
return GLOB_ABORTED; |
|
|
return(0); |
return 0; |
} |
} |
|
|
error = 0; |
error = 0; |
Line 709 glob3(pathbuf, pathend, pathlim, pattern |
|
Line 789 glob3(pathbuf, pathend, pathlim, pattern |
|
if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
readdirfunc = pglob->gl_readdir; |
readdirfunc = pglob->gl_readdir; |
else |
else |
readdirfunc = (struct dirent *(*)__P((void *))) readdir; |
readdirfunc = (struct dirent *(*)(void *)) readdir; |
while ((dp = (*readdirfunc)(dirp)) != NULL) { |
while ((dp = (*readdirfunc)(dirp)) != NULL) { |
u_char *sc; |
u_char *sc; |
Char *dc; |
Char *dc; |
|
|
/* Initial DOT must be matched literally. */ |
if ((pglob->gl_flags & GLOB_LIMIT) && |
if (dp->d_name[0] == DOT && *pattern != DOT) |
limit->l_readdir++ >= GLOB_LIMIT_READDIR) { |
|
errno = 0; |
|
*pathend++ = SEP; |
|
*pathend = EOS; |
|
error = GLOB_NOSPACE; |
|
break; |
|
} |
|
|
|
/* |
|
* Initial DOT must be matched literally, unless we have |
|
* GLOB_PERIOD set. |
|
*/ |
|
if ((pglob->gl_flags & GLOB_PERIOD) == 0) |
|
if (dp->d_name[0] == DOT && *pattern != DOT) |
|
continue; |
|
/* |
|
* If GLOB_NO_DOTDIRS is set, . and .. vanish. |
|
*/ |
|
if ((pglob->gl_flags & GLOB_NO_DOTDIRS) && |
|
(dp->d_name[0] == DOT) && |
|
((dp->d_name[1] == EOS) || |
|
((dp->d_name[1] == DOT) && (dp->d_name[2] == EOS)))) |
continue; |
continue; |
/* |
/* |
* The resulting string contains EOS, so we can |
* The resulting string contains EOS, so we can |
Line 744 glob3(pathbuf, pathend, pathlim, pattern |
|
Line 845 glob3(pathbuf, pathend, pathlim, pattern |
|
} |
} |
} |
} |
|
|
if (!match(pathend, pattern, restpattern)) { |
if (globstar) { |
|
#ifdef S_IFLNK |
|
if (!chase_symlinks && |
|
(g_lstat(pathbuf, &sbuf, pglob) || |
|
S_ISLNK(sbuf.st_mode))) |
|
continue; |
|
#endif |
|
|
|
if (!match(pathend, pattern, termstar)) |
|
continue; |
|
|
|
if (--dc < pathlim - 2) |
|
*dc++ = SEP; |
|
*dc = EOS; |
|
error = glob2(pathbuf, dc, pathlim, pglobstar, |
|
pglob, limit); |
|
if (error) |
|
break; |
*pathend = EOS; |
*pathend = EOS; |
continue; |
} else { |
|
if (!match(pathend, pattern, restpattern)) { |
|
*pathend = EOS; |
|
continue; |
|
} |
|
error = glob2(pathbuf, --dc, pathlim, restpattern, |
|
pglob, limit); |
|
if (error) |
|
break; |
} |
} |
error = glob2(pathbuf, --dc, pathlim, restpattern, pglob, limit); |
|
if (error) |
|
break; |
|
} |
} |
|
|
if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
(*pglob->gl_closedir)(dirp); |
(*pglob->gl_closedir)(dirp); |
else |
else |
Line 762 glob3(pathbuf, pathend, pathlim, pattern |
|
Line 884 glob3(pathbuf, pathend, pathlim, pattern |
|
* Again Posix X/Open issue with regards to error handling. |
* Again Posix X/Open issue with regards to error handling. |
*/ |
*/ |
if ((error || errno) && (pglob->gl_flags & GLOB_ERR)) |
if ((error || errno) && (pglob->gl_flags & GLOB_ERR)) |
return (GLOB_ABORTED); |
return GLOB_ABORTED; |
|
|
return(error); |
return error; |
} |
} |
|
|
|
|
/* |
/* |
* Extend the gl_pathv member of a glob_t structure to accomodate a new item, |
* Extend the gl_pathv member of a glob_t structure to accommodate a new item, |
* add the new item, and update gl_pathc. |
* add the new item, and update gl_pathc. |
* |
* |
* This assumes the BSD realloc, which only copies the block when its size |
* This assumes the BSD realloc, which only copies the block when its size |
Line 783 glob3(pathbuf, pathend, pathlim, pattern |
|
Line 905 glob3(pathbuf, pathend, pathlim, pattern |
|
* gl_pathv points to (gl_offs + gl_pathc + 1) items. |
* gl_pathv points to (gl_offs + gl_pathc + 1) items. |
*/ |
*/ |
static int |
static int |
globextend(path, pglob, limit) |
globextend(const Char *path, glob_t *pglob, struct glob_limit *limit) |
const Char *path; |
|
glob_t *pglob; |
|
size_t *limit; |
|
{ |
{ |
char **pathv; |
char **pathv; |
int i; |
size_t i, newsize, len; |
size_t newsize, len; |
|
char *copy; |
char *copy; |
const Char *p; |
const Char *p; |
|
|
Line 798 globextend(path, pglob, limit) |
|
Line 916 globextend(path, pglob, limit) |
|
_DIAGASSERT(pglob != NULL); |
_DIAGASSERT(pglob != NULL); |
|
|
newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs); |
newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs); |
|
if ((pglob->gl_flags & GLOB_LIMIT) && |
|
newsize > GLOB_LIMIT_PATH * sizeof(*pathv)) |
|
goto nospace; |
pathv = pglob->gl_pathv ? realloc(pglob->gl_pathv, newsize) : |
pathv = pglob->gl_pathv ? realloc(pglob->gl_pathv, newsize) : |
malloc(newsize); |
malloc(newsize); |
if (pathv == NULL) |
if (pathv == NULL) |
return(GLOB_NOSPACE); |
return GLOB_NOSPACE; |
|
|
if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) { |
if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) { |
/* first time around -- clear initial gl_offs items */ |
/* first time around -- clear initial gl_offs items */ |
pathv += pglob->gl_offs; |
pathv += pglob->gl_offs; |
for (i = pglob->gl_offs; --i >= 0; ) |
for (i = pglob->gl_offs + 1; --i > 0; ) |
*--pathv = NULL; |
*--pathv = NULL; |
} |
} |
pglob->gl_pathv = pathv; |
pglob->gl_pathv = pathv; |
Line 814 globextend(path, pglob, limit) |
|
Line 935 globextend(path, pglob, limit) |
|
for (p = path; *p++;) |
for (p = path; *p++;) |
continue; |
continue; |
len = (size_t)(p - path); |
len = (size_t)(p - path); |
*limit += len; |
limit->l_string += len; |
if ((copy = malloc(len)) != NULL) { |
if ((copy = malloc(len)) != NULL) { |
if (g_Ctoc(path, copy, len)) { |
if (g_Ctoc(path, copy, len)) { |
free(copy); |
free(copy); |
return(GLOB_ABORTED); |
return GLOB_ABORTED; |
} |
} |
pathv[pglob->gl_offs + pglob->gl_pathc++] = copy; |
pathv[pglob->gl_offs + pglob->gl_pathc++] = copy; |
} |
} |
pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; |
pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; |
|
|
if ((pglob->gl_flags & GLOB_LIMIT) && (newsize + *limit) >= ARG_MAX) { |
if ((pglob->gl_flags & GLOB_LIMIT) && |
errno = 0; |
(newsize + limit->l_string) >= GLOB_LIMIT_STRING) |
return(GLOB_NOSPACE); |
goto nospace; |
} |
|
|
|
return(copy == NULL ? GLOB_NOSPACE : 0); |
return copy == NULL ? GLOB_NOSPACE : 0; |
|
nospace: |
|
errno = 0; |
|
return GLOB_NOSPACE; |
} |
} |
|
|
|
|
Line 838 globextend(path, pglob, limit) |
|
Line 961 globextend(path, pglob, limit) |
|
* pattern causes a recursion level. |
* pattern causes a recursion level. |
*/ |
*/ |
static int |
static int |
match(name, pat, patend) |
match(const Char *name, const Char *pat, const Char *patend) |
Char *name, *pat, *patend; |
|
{ |
{ |
int ok, negate_range; |
int ok, negate_range; |
Char c, k; |
Char c, k; |
Line 852 match(name, pat, patend) |
|
Line 974 match(name, pat, patend) |
|
c = *pat++; |
c = *pat++; |
switch (c & M_MASK) { |
switch (c & M_MASK) { |
case M_ALL: |
case M_ALL: |
|
while (pat < patend && (*pat & M_MASK) == M_ALL) |
|
pat++; /* eat consecutive '*' */ |
if (pat == patend) |
if (pat == patend) |
return(1); |
return 1; |
do |
for (; !match(name, pat, patend); name++) |
if (match(name, pat, patend)) |
if (*name == EOS) |
return(1); |
return 0; |
while (*name++ != EOS); |
return 1; |
return(0); |
|
case M_ONE: |
case M_ONE: |
if (*name++ == EOS) |
if (*name++ == EOS) |
return(0); |
return 0; |
break; |
break; |
case M_SET: |
case M_SET: |
ok = 0; |
ok = 0; |
if ((k = *name++) == EOS) |
if ((k = *name++) == EOS) |
return(0); |
return 0; |
if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS) |
if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS) |
++pat; |
++pat; |
while (((c = *pat++) & M_MASK) != M_END) |
while (((c = *pat++) & M_MASK) != M_END) |
Line 877 match(name, pat, patend) |
|
Line 1000 match(name, pat, patend) |
|
} else if (c == k) |
} else if (c == k) |
ok = 1; |
ok = 1; |
if (ok == negate_range) |
if (ok == negate_range) |
return(0); |
return 0; |
break; |
break; |
default: |
default: |
if (*name++ != c) |
if (*name++ != c) |
return(0); |
return 0; |
break; |
break; |
} |
} |
} |
} |
return(*name == EOS); |
return *name == EOS; |
} |
} |
|
|
/* Free allocated data belonging to a glob_t structure. */ |
/* Free allocated data belonging to a glob_t structure. */ |
void |
void |
globfree(pglob) |
globfree(glob_t *pglob) |
glob_t *pglob; |
|
{ |
{ |
int i; |
size_t i; |
char **pp; |
char **pp; |
|
|
_DIAGASSERT(pglob != NULL); |
_DIAGASSERT(pglob != NULL); |
|
Line 1031 globfree(pglob) |
|
} |
} |
} |
} |
|
|
|
#ifndef __LIBC12_SOURCE__ |
|
int |
|
glob_pattern_p(const char *pattern, int quote) |
|
{ |
|
int range = 0; |
|
|
|
for (; *pattern; pattern++) |
|
switch (*pattern) { |
|
case QUESTION: |
|
case STAR: |
|
return 1; |
|
|
|
case QUOTE: |
|
if (quote && pattern[1] != EOS) |
|
++pattern; |
|
break; |
|
|
|
case LBRACKET: |
|
range = 1; |
|
break; |
|
|
|
case RBRACKET: |
|
if (range) |
|
return 1; |
|
break; |
|
default: |
|
break; |
|
} |
|
|
|
return 0; |
|
} |
|
#endif |
|
|
static DIR * |
static DIR * |
g_opendir(str, pglob) |
g_opendir(Char *str, glob_t *pglob) |
Char *str; |
|
glob_t *pglob; |
|
{ |
{ |
char buf[MAXPATHLEN]; |
char buf[MAXPATHLEN]; |
|
|
Line 927 g_opendir(str, pglob) |
|
Line 1080 g_opendir(str, pglob) |
|
} |
} |
|
|
if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
return((*pglob->gl_opendir)(buf)); |
return (*pglob->gl_opendir)(buf); |
|
|
return(opendir(buf)); |
return opendir(buf); |
} |
} |
|
|
static int |
static int |
g_lstat(fn, sb, pglob) |
g_lstat(Char *fn, __gl_stat_t *sb, glob_t *pglob) |
Char *fn; |
|
__gl_stat_t *sb; |
|
glob_t *pglob; |
|
{ |
{ |
char buf[MAXPATHLEN]; |
char buf[MAXPATHLEN]; |
|
|
Line 947 g_lstat(fn, sb, pglob) |
|
Line 1097 g_lstat(fn, sb, pglob) |
|
if (g_Ctoc(fn, buf, sizeof(buf))) |
if (g_Ctoc(fn, buf, sizeof(buf))) |
return -1; |
return -1; |
if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
return((*pglob->gl_lstat)(buf, sb)); |
return (*pglob->gl_lstat)(buf, sb); |
return(lstat(buf, sb)); |
return lstat(buf, sb); |
} |
} |
|
|
static int |
static int |
g_stat(fn, sb, pglob) |
g_stat(Char *fn, __gl_stat_t *sb, glob_t *pglob) |
Char *fn; |
|
__gl_stat_t *sb; |
|
glob_t *pglob; |
|
{ |
{ |
char buf[MAXPATHLEN]; |
char buf[MAXPATHLEN]; |
|
|
Line 966 g_stat(fn, sb, pglob) |
|
Line 1113 g_stat(fn, sb, pglob) |
|
if (g_Ctoc(fn, buf, sizeof(buf))) |
if (g_Ctoc(fn, buf, sizeof(buf))) |
return -1; |
return -1; |
if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
return((*pglob->gl_stat)(buf, sb)); |
return (*pglob->gl_stat)(buf, sb); |
return(stat(buf, sb)); |
return stat(buf, sb); |
} |
} |
|
|
static Char * |
static Char * |
g_strchr(str, ch) |
g_strchr(const Char *str, int ch) |
const Char *str; |
|
int ch; |
|
{ |
{ |
|
|
_DIAGASSERT(str != NULL); |
_DIAGASSERT(str != NULL); |
|
|
do { |
do { |
if (*str == ch) |
if (*str == ch) |
/* LINTED this is libc's definition! */ |
return __UNCONST(str); |
return (Char *)str; |
|
} while (*str++); |
} while (*str++); |
return NULL; |
return NULL; |
} |
} |
|
|
static int |
static int |
g_Ctoc(str, buf, len) |
g_Ctoc(const Char *str, char *buf, size_t len) |
const Char *str; |
|
char *buf; |
|
size_t len; |
|
{ |
{ |
char *dc; |
char *dc; |
|
|
Line 1008 g_Ctoc(str, buf, len) |
|
Line 1149 g_Ctoc(str, buf, len) |
|
|
|
#ifdef DEBUG |
#ifdef DEBUG |
static void |
static void |
qprintf(str, s) |
qprintf(const char *str, Char *s) |
const char *str; |
|
Char *s; |
|
{ |
{ |
Char *p; |
Char *p; |
|
|