Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. =================================================================== RCS file: /ftp/cvs/cvsroot/src/lib/libc/gen/glob.c,v retrieving revision 1.18.2.1 retrieving revision 1.19 diff -u -p -r1.18.2.1 -r1.19 --- src/lib/libc/gen/glob.c 2010/08/05 01:43:20 1.18.2.1 +++ src/lib/libc/gen/glob.c 2007/12/05 20:25:56 1.19 @@ -1,4 +1,4 @@ -/* $NetBSD: glob.c,v 1.18.2.1 2010/08/05 01:43:20 riz Exp $ */ +/* $NetBSD: glob.c,v 1.19 2007/12/05 20:25:56 christos Exp $ */ /* * Copyright (c) 1989, 1993 @@ -37,7 +37,7 @@ #if 0 static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93"; #else -__RCSID("$NetBSD: glob.c,v 1.18.2.1 2010/08/05 01:43:20 riz Exp $"); +__RCSID("$NetBSD: glob.c,v 1.19 2007/12/05 20:25:56 christos Exp $"); #endif #endif /* LIBC_SCCS and not lint */ @@ -59,6 +59,8 @@ __RCSID("$NetBSD: glob.c,v 1.18.2.1 2010 * expand ~user/foo to the /home/dir/of/user/foo * GLOB_BRACE: * expand {1,2}{a,b} to 1a 1b 2a 2b + * GLOB_PERIOD: + * allow metacharacters to match leading dots in filenames. * gl_matchc: * Number of matches in the current invocation of glob. */ @@ -82,14 +84,6 @@ __RCSID("$NetBSD: glob.c,v 1.18.2.1 2010 #define NO_GETPW_R #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) */ @@ -153,7 +147,7 @@ static int g_lstat(Char *, __gl_stat_t static DIR *g_opendir(Char *, glob_t *); static Char *g_strchr(const Char *, int); 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 glob2(Char *, Char *, Char *, Char *, glob_t *, size_t *); @@ -161,9 +155,8 @@ static int glob3(Char *, Char *, Char * size_t *); static int globextend(const Char *, glob_t *, size_t *); static const Char *globtilde(const Char *, Char *, size_t, glob_t *); -static int globexp1(const Char *, glob_t *, size_t *); -static int globexp2(const Char *, const Char *, glob_t *, int *, - size_t *); +static int globexp1(const Char *, glob_t *); +static int globexp2(const Char *, const Char *, glob_t *, int *); static int match(Char *, Char *, Char *); #ifdef DEBUG static void qprintf(const char *, Char *); @@ -176,8 +169,6 @@ glob(const char *pattern, int flags, int const u_char *patnext; int c; Char *bufnext, *bufend, patbuf[MAXPATHLEN+1]; - /* 0 = malloc(), 1 = stat(), 2 = readdir() */ - size_t limit[] = { 0, 0, 0 }; _DIAGASSERT(pattern != NULL); @@ -213,9 +204,9 @@ glob(const char *pattern, int flags, int *bufnext = EOS; if (flags & GLOB_BRACE) - return globexp1(patbuf, pglob, limit); + return globexp1(patbuf, pglob); else - return glob0(patbuf, pglob, limit); + return glob0(patbuf, pglob); } /* @@ -224,7 +215,7 @@ glob(const char *pattern, int flags, int * characters */ static int -globexp1(const Char *pattern, glob_t *pglob, size_t *limit) +globexp1(const Char *pattern, glob_t *pglob) { const Char* ptr = pattern; int rv; @@ -234,13 +225,13 @@ globexp1(const Char *pattern, glob_t *pg /* Protect a single {}, for find(1), like csh */ 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) - if (!globexp2(ptr, pattern, pglob, &rv, limit)) + if (!globexp2(ptr, pattern, pglob, &rv)) return rv; - return glob0(pattern, pglob, limit); + return glob0(pattern, pglob); } @@ -250,8 +241,7 @@ globexp1(const Char *pattern, glob_t *pg * If it fails then it tries to glob the rest of the pattern and returns. */ static int -globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv, - size_t *limit) +globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv) { int i; Char *lm, *ls; @@ -296,7 +286,7 @@ globexp2(const Char *ptr, const Char *pa * we use `pattern', not `patbuf' here so that that * unbalanced braces are passed to the match */ - *rv = glob0(pattern, pglob, limit); + *rv = glob0(pattern, pglob); return 0; } @@ -343,7 +333,7 @@ globexp2(const Char *ptr, const Char *pa #ifdef DEBUG qprintf("globexp2:", patbuf); #endif - *rv = globexp1(patbuf, pglob, limit); + *rv = globexp1(patbuf, pglob); /* move after the comma, to the next string */ pl = pm + 1; @@ -456,12 +446,13 @@ globtilde(const Char *pattern, Char *pat * to find no matches. */ static int -glob0(const Char *pattern, glob_t *pglob, size_t *limit) +glob0(const Char *pattern, glob_t *pglob) { const Char *qpatnext; int c, error; __gl_size_t oldpathc; Char *bufnext, patbuf[MAXPATHLEN+1]; + size_t limit = 0; _DIAGASSERT(pattern != NULL); _DIAGASSERT(pglob != NULL); @@ -524,7 +515,7 @@ glob0(const Char *pattern, glob_t *pglob qprintf("glob0:", patbuf); #endif - if ((error = glob1(patbuf, pglob, limit)) != 0) + if ((error = glob1(patbuf, pglob, &limit)) != 0) return error; if (pglob->gl_pathc == oldpathc) { @@ -538,7 +529,7 @@ glob0(const Char *pattern, glob_t *pglob if ((pglob->gl_flags & GLOB_NOCHECK) || ((pglob->gl_flags & (GLOB_NOMAGIC|GLOB_MAGCHAR)) == GLOB_NOMAGIC)) { - return globextend(pattern, pglob, limit); + return globextend(pattern, pglob, &limit); } else { return GLOB_NOMATCH; } @@ -609,13 +600,6 @@ glob2(Char *pathbuf, Char *pathend, Char if (g_lstat(pathbuf, &sb, pglob)) 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) && pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) || (S_ISLNK(sb.st_mode) && @@ -713,9 +697,13 @@ glob3(Char *pathbuf, Char *pathend, Char u_char *sc; Char *dc; - /* Initial DOT must be matched literally. */ - if (dp->d_name[0] == DOT && *pattern != DOT) - continue; + /* + * 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; /* * The resulting string contains EOS, so we can * use the pathlim character, if it is the nul @@ -724,14 +712,6 @@ glob3(Char *pathbuf, Char *pathend, Char dc <= pathlim && (*dc++ = *sc++) != EOS;) 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? */ @@ -755,8 +735,7 @@ glob3(Char *pathbuf, Char *pathend, Char *pathend = EOS; continue; } - error = glob2(pathbuf, --dc, pathlim, restpattern, pglob, - limit); + error = glob2(pathbuf, --dc, pathlim, restpattern, pglob, limit); if (error) break; } @@ -818,7 +797,7 @@ globextend(const Char *path, glob_t *pgl for (p = path; *p++;) continue; len = (size_t)(p - path); - limit[GLOB_INDEX_MALLOC] += len; + *limit += len; if ((copy = malloc(len)) != NULL) { if (g_Ctoc(path, copy, len)) { free(copy); @@ -828,8 +807,7 @@ globextend(const Char *path, glob_t *pgl } pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; - if ((pglob->gl_flags & GLOB_LIMIT) && - (newsize + limit[GLOB_INDEX_MALLOC]) >= GLOB_LIMIT_MALLOC) { + if ((pglob->gl_flags & GLOB_LIMIT) && (newsize + *limit) >= ARG_MAX) { errno = 0; return GLOB_NOSPACE; }