version 1.28, 2011/01/21 23:30:31 |
version 1.36.4.1, 2017/05/02 03:19:16 |
Line 87 __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_STRING 524288 /* number of readdirs */ |
#define GLOB_LIMIT_STAT 128 /* number of stat system calls */ |
#define GLOB_LIMIT_STAT 128 /* number of stat system calls */ |
#define GLOB_LIMIT_READDIR 16384 /* total buffer size of path strings */ |
#define GLOB_LIMIT_READDIR 65536 /* total buffer size of path strings */ |
#define GLOB_LIMIT_PATH 1024 /* number of path elements */ |
#define GLOB_LIMIT_PATH 1024 /* number of path elements */ |
#define GLOB_LIMIT_BRACE 128 /* Number of brace calls */ |
#define GLOB_LIMIT_BRACE 128 /* Number of brace calls */ |
|
|
Line 132 struct glob_limit { |
|
Line 132 struct glob_limit { |
|
#define M_MASK 0xffff |
#define M_MASK 0xffff |
#define M_ASCII 0x00ff |
#define M_ASCII 0x00ff |
|
|
typedef u_short Char; |
typedef unsigned short Char; |
|
|
#else |
#else |
|
|
Line 180 static void qprintf(const char *, Char |
|
Line 180 static void qprintf(const char *, Char |
|
#endif |
#endif |
|
|
int |
int |
glob(const char *pattern, int flags, int (*errfunc)(const char *, int), |
glob(const char * __restrict pattern, int flags, int (*errfunc)(const char *, |
glob_t *pglob) |
int), glob_t * __restrict pglob) |
{ |
{ |
const u_char *patnext; |
const unsigned 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 }; |
struct glob_limit limit = { 0, 0, 0, 0 }; |
|
|
_DIAGASSERT(pattern != NULL); |
_DIAGASSERT(pattern != NULL); |
|
|
patnext = (const u_char *) pattern; |
patnext = (const unsigned char *) pattern; |
if (!(flags & GLOB_APPEND)) { |
if (!(flags & GLOB_APPEND)) { |
pglob->gl_pathc = 0; |
pglob->gl_pathc = 0; |
pglob->gl_pathv = NULL; |
pglob->gl_pathv = NULL; |
Line 612 glob2(Char *pathbuf, Char *pathend, Char |
|
Line 612 glob2(Char *pathbuf, Char *pathend, Char |
|
const Char *p; |
const Char *p; |
Char *q; |
Char *q; |
int anymeta; |
int anymeta; |
Char *pend; |
|
ptrdiff_t diff; |
|
|
|
_DIAGASSERT(pathbuf != NULL); |
_DIAGASSERT(pathbuf != NULL); |
_DIAGASSERT(pathend != NULL); |
_DIAGASSERT(pathend != NULL); |
Line 638 glob2(Char *pathbuf, Char *pathend, Char |
|
Line 636 glob2(Char *pathbuf, Char *pathend, Char |
|
errno = 0; |
errno = 0; |
*pathend++ = SEP; |
*pathend++ = SEP; |
*pathend = EOS; |
*pathend = EOS; |
printf("stat limit\n"); |
|
return GLOB_NOSPACE; |
return GLOB_NOSPACE; |
} |
} |
if (((pglob->gl_flags & GLOB_MARK) && |
if (((pglob->gl_flags & GLOB_MARK) && |
Line 666 printf("stat limit\n"); |
|
Line 663 printf("stat limit\n"); |
|
*q++ = *p++; |
*q++ = *p++; |
} |
} |
|
|
/* |
if (!anymeta) { |
* 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 790 glob3(Char *pathbuf, Char *pathend, Char |
|
Line 768 glob3(Char *pathbuf, Char *pathend, Char |
|
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; |
unsigned char *sc; |
Char *dc; |
Char *dc; |
|
|
if ((pglob->gl_flags & GLOB_LIMIT) && |
if ((pglob->gl_flags & GLOB_LIMIT) && |
Line 800 glob3(Char *pathbuf, Char *pathend, Char |
|
Line 778 glob3(Char *pathbuf, Char *pathend, Char |
|
errno = 0; |
errno = 0; |
*pathend++ = SEP; |
*pathend++ = SEP; |
*pathend = EOS; |
*pathend = EOS; |
return GLOB_NOSPACE; |
error = GLOB_NOSPACE; |
|
break; |
} |
} |
|
|
/* |
/* |
Line 822 glob3(Char *pathbuf, Char *pathend, Char |
|
Line 801 glob3(Char *pathbuf, Char *pathend, Char |
|
* The resulting string contains EOS, so we can |
* The resulting string contains EOS, so we can |
* use the pathlim character, if it is the nul |
* use the pathlim character, if it is the nul |
*/ |
*/ |
for (sc = (u_char *) dp->d_name, dc = pathend; |
for (sc = (unsigned char *) dp->d_name, dc = pathend; |
dc <= pathlim && (*dc++ = *sc++) != EOS;) |
dc <= pathlim && (*dc++ = *sc++) != EOS;) |
continue; |
continue; |
|
|
|
|
|
|
|
|
/* |
/* |
* pattern matching function for filenames. Each occurrence of the * |
* pattern matching function for filenames. |
* pattern causes a recursion level. |
|
*/ |
*/ |
static int |
static int |
match(const Char *name, const Char *pat, const Char *patend) |
match(const Char *name, const Char *pat, const Char *patend) |
{ |
{ |
int ok, negate_range; |
int ok, negate_range; |
Char c, k; |
Char c, k; |
|
const Char *patNext, *nameNext, *nameStart, *nameEnd; |
|
|
_DIAGASSERT(name != NULL); |
_DIAGASSERT(name != NULL); |
_DIAGASSERT(pat != NULL); |
_DIAGASSERT(pat != NULL); |
_DIAGASSERT(patend != NULL); |
_DIAGASSERT(patend != NULL); |
|
patNext = pat; |
while (pat < patend) { |
nameStart = nameNext = name; |
c = *pat++; |
nameEnd = NULL; |
|
|
|
while (pat < patend || *name) { |
|
c = *pat; |
|
if (*name == EOS) |
|
nameEnd = name; |
switch (c & M_MASK) { |
switch (c & M_MASK) { |
case M_ALL: |
case M_ALL: |
while (pat < patend && (*pat & M_MASK) == M_ALL) |
while (pat[1] == '*') pat++; |
pat++; /* eat consecutive '*' */ |
patNext = pat; |
if (pat == patend) |
nameNext = name + 1; |
return 1; |
pat++; |
for (; !match(name, pat, patend); name++) |
continue; |
if (*name == EOS) |
|
return 0; |
|
return 1; |
|
case M_ONE: |
case M_ONE: |
if (*name++ == EOS) |
if (*name == EOS) |
return 0; |
break; |
break; |
pat++; |
|
name++; |
|
continue; |
case M_SET: |
case M_SET: |
ok = 0; |
ok = 0; |
if ((k = *name++) == EOS) |
if ((k = *name) == EOS) |
return 0; |
break; |
|
pat++; |
|
name++; |
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 1000 match(const Char *name, const Char *pat, |
|
Line 985 match(const Char *name, const Char *pat, |
|
} else if (c == k) |
} else if (c == k) |
ok = 1; |
ok = 1; |
if (ok == negate_range) |
if (ok == negate_range) |
return 0; |
break; |
break; |
continue; |
default: |
default: |
if (*name++ != c) |
if (*name != c) |
return 0; |
break; |
break; |
pat++; |
|
name++; |
|
continue; |
} |
} |
|
if (nameNext != nameStart |
|
&& (nameEnd == NULL || nameNext <= nameEnd)) { |
|
pat = patNext; |
|
name = nameNext; |
|
continue; |
|
} |
|
return 0; |
} |
} |
return *name == EOS; |
return 1; |
} |
} |
|
|
/* Free allocated data belonging to a glob_t structure. */ |
/* Free allocated data belonging to a glob_t structure. */ |