|
version 1.27, 2010/09/06 14:40:25
|
version 1.28, 2011/01/21 23:30:31
|
|
Line 87 __RCSID("$NetBSD$");
|
Line 87 __RCSID("$NetBSD$");
|
| #define NO_GETPW_R |
#define NO_GETPW_R |
| #endif |
#endif |
| |
|
| #define GLOB_LIMIT_MALLOC 65536 |
#define GLOB_LIMIT_STRING 65536 /* number of readdirs */ |
| #define GLOB_LIMIT_STAT 128 |
#define GLOB_LIMIT_STAT 128 /* number of stat system calls */ |
| #define GLOB_LIMIT_READDIR 16384 |
#define GLOB_LIMIT_READDIR 16384 /* total buffer size of path strings */ |
| |
#define GLOB_LIMIT_PATH 1024 /* number of path elements */ |
| #define GLOB_INDEX_MALLOC 0 |
#define GLOB_LIMIT_BRACE 128 /* Number of brace calls */ |
| #define GLOB_INDEX_STAT 1 |
|
| #define GLOB_INDEX_READDIR 2 |
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 158 static int g_lstat(Char *, __gl_stat_t
|
Line 163 static int g_lstat(Char *, __gl_stat_t
|
| static DIR *g_opendir(Char *, glob_t *); |
static DIR *g_opendir(Char *, glob_t *); |
| static Char *g_strchr(const Char *, int); |
static Char *g_strchr(const Char *, int); |
| static int g_stat(Char *, __gl_stat_t *, glob_t *); |
static int g_stat(Char *, __gl_stat_t *, glob_t *); |
| static int glob0(const Char *, glob_t *, size_t *); |
static int glob0(const Char *, glob_t *, struct glob_limit *); |
| static int glob1(Char *, glob_t *, size_t *); |
static int glob1(Char *, glob_t *, struct glob_limit *); |
| static int glob2(Char *, Char *, Char *, const Char *, glob_t *, |
static int glob2(Char *, Char *, Char *, const Char *, glob_t *, |
| size_t *); |
struct glob_limit *); |
| static int glob3(Char *, Char *, Char *, const Char *, const Char *, |
static int glob3(Char *, Char *, Char *, const Char *, const Char *, |
| const Char *, glob_t *, size_t *); |
const Char *, glob_t *, struct glob_limit *); |
| static int globextend(const Char *, glob_t *, size_t *); |
static int globextend(const Char *, glob_t *, struct glob_limit *); |
| static const Char *globtilde(const Char *, Char *, size_t, glob_t *); |
static const Char *globtilde(const Char *, Char *, size_t, glob_t *); |
| static int globexp1(const Char *, glob_t *, size_t *); |
static int globexp1(const Char *, glob_t *, struct glob_limit *); |
| static int globexp2(const Char *, const Char *, glob_t *, int *, |
static int globexp2(const Char *, const Char *, glob_t *, int *, |
| size_t *); |
struct glob_limit *); |
| static int match(const Char *, const Char *, const Char *); |
static int match(const Char *, const Char *, const Char *); |
| #ifdef DEBUG |
#ifdef DEBUG |
| static void qprintf(const char *, Char *); |
static void qprintf(const char *, Char *); |
|
Line 181 glob(const char *pattern, int flags, int
|
Line 186 glob(const char *pattern, int flags, int
|
| const u_char *patnext; |
const u_char *patnext; |
| int c; |
int c; |
| Char *bufnext, *bufend, patbuf[MAXPATHLEN+1]; |
Char *bufnext, *bufend, patbuf[MAXPATHLEN+1]; |
| /* 0 = malloc(), 1 = stat(), 2 = readdir() */ |
struct glob_limit limit = { 0, 0, 0, 0 }; |
| size_t limit[] = { 0, 0, 0 }; |
|
| |
|
| _DIAGASSERT(pattern != NULL); |
_DIAGASSERT(pattern != NULL); |
| |
|
|
Line 218 glob(const char *pattern, int flags, int
|
Line 222 glob(const char *pattern, int flags, int
|
| *bufnext = EOS; |
*bufnext = EOS; |
| |
|
| if (flags & GLOB_BRACE) |
if (flags & GLOB_BRACE) |
| return globexp1(patbuf, pglob, limit); |
return globexp1(patbuf, pglob, &limit); |
| else |
else |
| return glob0(patbuf, pglob, limit); |
return glob0(patbuf, pglob, &limit); |
| } |
} |
| |
|
| /* |
/* |
|
Line 229 glob(const char *pattern, int flags, int
|
Line 233 glob(const char *pattern, int flags, int
|
| * characters |
* characters |
| */ |
*/ |
| static int |
static int |
| globexp1(const Char *pattern, glob_t *pglob, size_t *limit) |
globexp1(const Char *pattern, glob_t *pglob, struct glob_limit *limit) |
| { |
{ |
| const Char* ptr = pattern; |
const Char* ptr = pattern; |
| int rv; |
int rv; |
|
Line 237 globexp1(const Char *pattern, glob_t *pg
|
Line 241 globexp1(const Char *pattern, glob_t *pg
|
| _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, limit); |
return glob0(pattern, pglob, limit); |
|
Line 256 globexp1(const Char *pattern, glob_t *pg
|
Line 266 globexp1(const Char *pattern, glob_t *pg
|
| */ |
*/ |
| static int |
static int |
| globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv, |
globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv, |
| size_t *limit) |
struct glob_limit *limit) |
| { |
{ |
| int i; |
int i; |
| Char *lm, *ls; |
Char *lm, *ls; |
|
Line 461 globtilde(const Char *pattern, Char *pat
|
Line 471 globtilde(const Char *pattern, Char *pat
|
| * to find no matches. |
* to find no matches. |
| */ |
*/ |
| static int |
static int |
| glob0(const Char *pattern, glob_t *pglob, size_t *limit) |
glob0(const Char *pattern, glob_t *pglob, struct glob_limit *limit) |
| { |
{ |
| const Char *qpatnext; |
const Char *qpatnext; |
| int c, error; |
int c, error; |
|
Line 570 compare(const void *p, const void *q)
|
Line 580 compare(const void *p, const void *q)
|
| } |
} |
| |
|
| static int |
static int |
| glob1(Char *pattern, glob_t *pglob, size_t *limit) |
glob1(Char *pattern, glob_t *pglob, struct glob_limit *limit) |
| { |
{ |
| Char pathbuf[MAXPATHLEN+1]; |
Char pathbuf[MAXPATHLEN+1]; |
| |
|
|
Line 596 glob1(Char *pattern, glob_t *pglob, size
|
Line 606 glob1(Char *pattern, glob_t *pglob, size
|
| */ |
*/ |
| static int |
static int |
| glob2(Char *pathbuf, Char *pathend, Char *pathlim, const Char *pattern, |
glob2(Char *pathbuf, Char *pathend, Char *pathlim, const Char *pattern, |
| glob_t *pglob, size_t *limit) |
glob_t *pglob, struct glob_limit *limit) |
| { |
{ |
| __gl_stat_t sb; |
__gl_stat_t sb; |
| const Char *p; |
const Char *p; |
|
Line 624 glob2(Char *pathbuf, Char *pathend, Char
|
Line 634 glob2(Char *pathbuf, Char *pathend, Char
|
| return 0; |
return 0; |
| |
|
| if ((pglob->gl_flags & GLOB_LIMIT) && |
if ((pglob->gl_flags & GLOB_LIMIT) && |
| limit[GLOB_INDEX_STAT]++ >= GLOB_LIMIT_STAT) { |
limit->l_stat++ >= GLOB_LIMIT_STAT) { |
| 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 692 glob2(Char *pathbuf, Char *pathend, Char
|
Line 703 glob2(Char *pathbuf, Char *pathend, Char
|
| static int |
static int |
| glob3(Char *pathbuf, Char *pathend, Char *pathlim, const Char *pattern, |
glob3(Char *pathbuf, Char *pathend, Char *pathlim, const Char *pattern, |
| const Char *restpattern, const Char *pglobstar, glob_t *pglob, |
const Char *restpattern, const Char *pglobstar, glob_t *pglob, |
| size_t *limit) |
struct glob_limit *limit) |
| { |
{ |
| struct dirent *dp; |
struct dirent *dp; |
| DIR *dirp; |
DIR *dirp; |
|
Line 785 glob3(Char *pathbuf, Char *pathend, Char
|
Line 796 glob3(Char *pathbuf, Char *pathend, Char
|
| Char *dc; |
Char *dc; |
| |
|
| if ((pglob->gl_flags & GLOB_LIMIT) && |
if ((pglob->gl_flags & GLOB_LIMIT) && |
| limit[GLOB_INDEX_READDIR]++ >= GLOB_LIMIT_READDIR) { |
limit->l_readdir++ >= GLOB_LIMIT_READDIR) { |
| errno = 0; |
errno = 0; |
| *pathend++ = SEP; |
*pathend++ = SEP; |
| *pathend = EOS; |
*pathend = EOS; |
|
Line 894 glob3(Char *pathbuf, Char *pathend, Char
|
Line 905 glob3(Char *pathbuf, Char *pathend, Char
|
| * 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(const Char *path, glob_t *pglob, size_t *limit) |
globextend(const Char *path, glob_t *pglob, struct glob_limit *limit) |
| { |
{ |
| char **pathv; |
char **pathv; |
| size_t i, newsize, len; |
size_t i, newsize, len; |
|
Line 905 globextend(const Char *path, glob_t *pgl
|
Line 916 globextend(const Char *path, glob_t *pgl
|
| _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) |
|
Line 921 globextend(const Char *path, glob_t *pgl
|
Line 935 globextend(const Char *path, glob_t *pgl
|
| for (p = path; *p++;) |
for (p = path; *p++;) |
| continue; |
continue; |
| len = (size_t)(p - path); |
len = (size_t)(p - path); |
| limit[GLOB_INDEX_MALLOC] += 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); |
|
Line 932 globextend(const Char *path, glob_t *pgl
|
Line 946 globextend(const Char *path, glob_t *pgl
|
| pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; |
pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; |
| |
|
| if ((pglob->gl_flags & GLOB_LIMIT) && |
if ((pglob->gl_flags & GLOB_LIMIT) && |
| (newsize + limit[GLOB_INDEX_MALLOC]) >= GLOB_LIMIT_MALLOC) { |
(newsize + limit->l_string) >= GLOB_LIMIT_STRING) |
| errno = 0; |
goto nospace; |
| return GLOB_NOSPACE; |
|
| } |
|
| |
|
| return copy == NULL ? GLOB_NOSPACE : 0; |
return copy == NULL ? GLOB_NOSPACE : 0; |
| |
nospace: |
| |
errno = 0; |
| |
return GLOB_NOSPACE; |
| } |
} |
| |
|
| |
|