| version 1.18.10.1, 2010/08/05 01:46:31 |
version 1.19, 2007/12/05 20:25:56 |
| 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. |
| * gl_matchc: |
* gl_matchc: |
| * Number of matches in the current invocation of glob. |
* Number of matches in the current invocation of glob. |
| */ |
*/ |
| Line 82 __RCSID("$NetBSD$"); |
|
| Line 84 __RCSID("$NetBSD$"); |
|
| #define NO_GETPW_R |
#define NO_GETPW_R |
| #endif |
#endif |
| |
|
| #define GLOB_LIMIT_MALLOC 65536 |
|
| #define GLOB_LIMIT_STAT 128 |
|
| #define GLOB_LIMIT_READDIR 16384 |
|
| |
|
| #define GLOB_INDEX_MALLOC 0 |
|
| #define GLOB_INDEX_STAT 1 |
|
| #define GLOB_INDEX_READDIR 2 |
|
| |
|
| /* |
/* |
| * XXX: For NetBSD 1.4.x compatibility. (kill me l8r) |
* XXX: For NetBSD 1.4.x compatibility. (kill me l8r) |
| */ |
*/ |
| Line 153 static int g_lstat(Char *, __gl_stat_t |
|
| Line 147 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 *); |
| static int glob1(Char *, glob_t *, size_t *); |
static int glob1(Char *, glob_t *, size_t *); |
| static int glob2(Char *, Char *, Char *, Char *, glob_t *, |
static int glob2(Char *, Char *, Char *, Char *, glob_t *, |
| size_t *); |
size_t *); |
| Line 161 static int glob3(Char *, Char *, Char * |
|
| Line 155 static int glob3(Char *, Char *, Char * |
|
| size_t *); |
size_t *); |
| static int globextend(const Char *, glob_t *, size_t *); |
static int globextend(const Char *, glob_t *, size_t *); |
| 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 *); |
| static int globexp2(const Char *, const Char *, glob_t *, int *, |
static int globexp2(const Char *, const Char *, glob_t *, int *); |
| size_t *); |
|
| static int match(Char *, Char *, Char *); |
static int match(Char *, Char *, Char *); |
| #ifdef DEBUG |
#ifdef DEBUG |
| static void qprintf(const char *, Char *); |
static void qprintf(const char *, Char *); |
| Line 176 glob(const char *pattern, int flags, int |
|
| Line 169 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() */ |
|
| size_t limit[] = { 0, 0, 0 }; |
|
| |
|
| _DIAGASSERT(pattern != NULL); |
_DIAGASSERT(pattern != NULL); |
| |
|
| Line 213 glob(const char *pattern, int flags, int |
|
| Line 204 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); |
| else |
else |
| return glob0(patbuf, pglob, limit); |
return glob0(patbuf, pglob); |
| } |
} |
| |
|
| /* |
/* |
| Line 224 glob(const char *pattern, int flags, int |
|
| Line 215 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) |
| { |
{ |
| const Char* ptr = pattern; |
const Char* ptr = pattern; |
| int rv; |
int rv; |
| Line 234 globexp1(const Char *pattern, glob_t *pg |
|
| Line 225 globexp1(const Char *pattern, glob_t *pg |
|
| |
|
| /* 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); |
| |
|
| while ((ptr = (const Char *) g_strchr(ptr, LBRACE)) != NULL) |
while ((ptr = (const Char *) g_strchr(ptr, LBRACE)) != NULL) |
| if (!globexp2(ptr, pattern, pglob, &rv, limit)) |
if (!globexp2(ptr, pattern, pglob, &rv)) |
| return rv; |
return rv; |
| |
|
| return glob0(pattern, pglob, limit); |
return glob0(pattern, pglob); |
| } |
} |
| |
|
| |
|
| Line 250 globexp1(const Char *pattern, glob_t *pg |
|
| Line 241 globexp1(const Char *pattern, glob_t *pg |
|
| * 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(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) |
|
| { |
{ |
| int i; |
int i; |
| Char *lm, *ls; |
Char *lm, *ls; |
| Line 296 globexp2(const Char *ptr, const Char *pa |
|
| Line 286 globexp2(const Char *ptr, const Char *pa |
|
| * 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, limit); |
*rv = glob0(pattern, pglob); |
| return 0; |
return 0; |
| } |
} |
| |
|
| Line 343 globexp2(const Char *ptr, const Char *pa |
|
| Line 333 globexp2(const Char *ptr, const Char *pa |
|
| #ifdef DEBUG |
#ifdef DEBUG |
| qprintf("globexp2:", patbuf); |
qprintf("globexp2:", patbuf); |
| #endif |
#endif |
| *rv = globexp1(patbuf, pglob, limit); |
*rv = globexp1(patbuf, pglob); |
| |
|
| /* move after the comma, to the next string */ |
/* move after the comma, to the next string */ |
| pl = pm + 1; |
pl = pm + 1; |
| Line 456 globtilde(const Char *pattern, Char *pat |
|
| Line 446 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) |
| { |
{ |
| const Char *qpatnext; |
const Char *qpatnext; |
| int c, error; |
int c, error; |
| __gl_size_t oldpathc; |
__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 524 glob0(const Char *pattern, glob_t *pglob |
|
| Line 515 glob0(const Char *pattern, glob_t *pglob |
|
| 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 538 glob0(const Char *pattern, glob_t *pglob |
|
| Line 529 glob0(const Char *pattern, glob_t *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; |
| } |
} |
| Line 609 glob2(Char *pathbuf, Char *pathend, Char |
|
| Line 600 glob2(Char *pathbuf, Char *pathend, Char |
|
| if (g_lstat(pathbuf, &sb, pglob)) |
if (g_lstat(pathbuf, &sb, pglob)) |
| return 0; |
return 0; |
| |
|
| if ((pglob->gl_flags & GLOB_LIMIT) && |
|
| limit[GLOB_INDEX_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) && |
| Line 713 glob3(Char *pathbuf, Char *pathend, Char |
|
| Line 697 glob3(Char *pathbuf, Char *pathend, Char |
|
| u_char *sc; |
u_char *sc; |
| Char *dc; |
Char *dc; |
| |
|
| /* Initial DOT must be matched literally. */ |
/* |
| if (dp->d_name[0] == DOT && *pattern != DOT) |
* Initial DOT must be matched literally, unless we have |
| continue; |
* GLOB_PERIOD set. |
| |
*/ |
| |
if ((pglob->gl_flags & GLOB_PERIOD) == 0) |
| |
if (dp->d_name[0] == DOT && *pattern != DOT) |
| |
continue; |
| /* |
/* |
| * 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 |
| Line 724 glob3(Char *pathbuf, Char *pathend, Char |
|
| Line 712 glob3(Char *pathbuf, Char *pathend, Char |
|
| dc <= pathlim && (*dc++ = *sc++) != EOS;) |
dc <= pathlim && (*dc++ = *sc++) != EOS;) |
| continue; |
continue; |
| |
|
| if ((pglob->gl_flags & GLOB_LIMIT) && |
|
| limit[GLOB_INDEX_READDIR]++ >= GLOB_LIMIT_READDIR) { |
|
| errno = 0; |
|
| *pathend++ = SEP; |
|
| *pathend = EOS; |
|
| return GLOB_NOSPACE; |
|
| } |
|
| |
|
| /* |
/* |
| * Have we filled the buffer without seeing EOS? |
* Have we filled the buffer without seeing EOS? |
| */ |
*/ |
| Line 755 glob3(Char *pathbuf, Char *pathend, Char |
|
| Line 735 glob3(Char *pathbuf, Char *pathend, Char |
|
| *pathend = EOS; |
*pathend = EOS; |
| continue; |
continue; |
| } |
} |
| error = glob2(pathbuf, --dc, pathlim, restpattern, pglob, |
error = glob2(pathbuf, --dc, pathlim, restpattern, pglob, limit); |
| limit); |
|
| if (error) |
if (error) |
| break; |
break; |
| } |
} |
| Line 818 globextend(const Char *path, glob_t *pgl |
|
| Line 797 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 += 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 828 globextend(const Char *path, glob_t *pgl |
|
| Line 807 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) >= ARG_MAX) { |
| (newsize + limit[GLOB_INDEX_MALLOC]) >= GLOB_LIMIT_MALLOC) { |
|
| errno = 0; |
errno = 0; |
| return GLOB_NOSPACE; |
return GLOB_NOSPACE; |
| } |
} |