add GLOB_LIMIT from NetBSD

git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@9753 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
Assar Westerlund
2001-03-25 23:43:19 +00:00
parent 74e352cc31
commit 744da25480
2 changed files with 36 additions and 23 deletions

View File

@@ -145,16 +145,16 @@ static int compare (const void *, const void *);
static void g_Ctoc (const Char *, char *); static void g_Ctoc (const Char *, char *);
static int g_lstat (Char *, struct stat *, glob_t *); static int g_lstat (Char *, struct stat *, glob_t *);
static DIR *g_opendir (Char *, glob_t *); static DIR *g_opendir (Char *, glob_t *);
static Char *g_strchr (Char *, int); static Char *g_strchr (const Char *, int);
#ifdef notdef #ifdef notdef
static Char *g_strcat (Char *, const Char *); static Char *g_strcat (Char *, const Char *);
#endif #endif
static int g_stat (Char *, struct stat *, glob_t *); static int g_stat (Char *, struct stat *, glob_t *);
static int glob0 (const Char *, glob_t *); static int glob0 (const Char *, glob_t *);
static int glob1 (Char *, glob_t *); static int glob1 (Char *, glob_t *, size_t *);
static int glob2 (Char *, Char *, Char *, glob_t *); static int glob2 (Char *, Char *, Char *, glob_t *, size_t *);
static int glob3 (Char *, Char *, Char *, Char *, glob_t *); static int glob3 (Char *, Char *, Char *, Char *, glob_t *, size_t *);
static int globextend (const Char *, glob_t *); static int globextend (const Char *, glob_t *, size_t *);
static const Char * globtilde (const Char *, Char *, glob_t *); static const Char * globtilde (const Char *, Char *, glob_t *);
static int globexp1 (const Char *, glob_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 *);
@@ -173,7 +173,7 @@ glob(const char *pattern,
int c; int c;
Char *bufnext, *bufend, patbuf[MaxPathLen+1]; Char *bufnext, *bufend, patbuf[MaxPathLen+1];
patnext = (u_char *) pattern; patnext = (const u_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;
@@ -224,7 +224,7 @@ static int globexp1(const Char *pattern, glob_t *pglob)
if (pattern[0] == CHAR_LBRACE && pattern[1] == CHAR_RBRACE && pattern[2] == CHAR_EOS) if (pattern[0] == CHAR_LBRACE && pattern[1] == CHAR_RBRACE && pattern[2] == CHAR_EOS)
return glob0(pattern, pglob); return glob0(pattern, pglob);
while ((ptr = (const Char *) g_strchr((Char *) ptr, CHAR_LBRACE)) != NULL) while ((ptr = (const Char *) g_strchr(ptr, CHAR_LBRACE)) != NULL)
if (!globexp2(ptr, pattern, pglob, &rv)) if (!globexp2(ptr, pattern, pglob, &rv))
return rv; return rv;
@@ -405,6 +405,7 @@ glob0(const Char *pattern, glob_t *pglob)
const Char *qpatnext; const Char *qpatnext;
int c, err, oldpathc; int c, err, oldpathc;
Char *bufnext, patbuf[MaxPathLen+1]; Char *bufnext, patbuf[MaxPathLen+1];
size_t limit = 0;
qpatnext = globtilde(pattern, patbuf, pglob); qpatnext = globtilde(pattern, patbuf, pglob);
oldpathc = pglob->gl_pathc; oldpathc = pglob->gl_pathc;
@@ -418,7 +419,7 @@ glob0(const Char *pattern, glob_t *pglob)
if (c == CHAR_NOT) if (c == CHAR_NOT)
++qpatnext; ++qpatnext;
if (*qpatnext == CHAR_EOS || if (*qpatnext == CHAR_EOS ||
g_strchr((Char *) qpatnext+1, CHAR_RBRACKET) == NULL) { g_strchr(qpatnext+1, CHAR_RBRACKET) == NULL) {
*bufnext++ = CHAR_LBRACKET; *bufnext++ = CHAR_LBRACKET;
if (c == CHAR_NOT) if (c == CHAR_NOT)
--qpatnext; --qpatnext;
@@ -462,7 +463,7 @@ glob0(const Char *pattern, glob_t *pglob)
qprintf("glob0:", patbuf); qprintf("glob0:", patbuf);
#endif #endif
if ((err = glob1(patbuf, pglob)) != 0) if ((err = glob1(patbuf, pglob, &limit)) != 0)
return(err); return(err);
/* /*
@@ -475,7 +476,7 @@ glob0(const Char *pattern, glob_t *pglob)
((pglob->gl_flags & GLOB_NOCHECK) || ((pglob->gl_flags & GLOB_NOCHECK) ||
((pglob->gl_flags & GLOB_NOMAGIC) && ((pglob->gl_flags & GLOB_NOMAGIC) &&
!(pglob->gl_flags & GLOB_MAGCHAR)))) !(pglob->gl_flags & GLOB_MAGCHAR))))
return(globextend(pattern, pglob)); return(globextend(pattern, pglob, &limit));
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,
pglob->gl_pathc - oldpathc, sizeof(char *), compare); pglob->gl_pathc - oldpathc, sizeof(char *), compare);
@@ -489,14 +490,14 @@ compare(const void *p, const void *q)
} }
static int static int
glob1(Char *pattern, glob_t *pglob) glob1(Char *pattern, glob_t *pglob, size_t *limit)
{ {
Char pathbuf[MaxPathLen+1]; Char pathbuf[MaxPathLen+1];
/* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */ /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
if (*pattern == CHAR_EOS) if (*pattern == CHAR_EOS)
return(0); return(0);
return(glob2(pathbuf, pathbuf, pattern, pglob)); return(glob2(pathbuf, pathbuf, pattern, pglob, limit));
} }
/* /*
@@ -514,7 +515,8 @@ glob1(Char *pattern, glob_t *pglob)
#endif #endif
static int static int
glob2(Char *pathbuf, Char *pathend, Char *pattern, glob_t *pglob) glob2(Char *pathbuf, Char *pathend, Char *pattern, glob_t *pglob,
size_t *limit)
{ {
struct stat sb; struct stat sb;
Char *p, *q; Char *p, *q;
@@ -539,7 +541,7 @@ glob2(Char *pathbuf, Char *pathend, Char *pattern, glob_t *pglob)
*pathend = CHAR_EOS; *pathend = CHAR_EOS;
} }
++pglob->gl_matchc; ++pglob->gl_matchc;
return(globextend(pathbuf, pglob)); return(globextend(pathbuf, pglob, limit));
} }
/* Find end of next segment, copy tentatively to pathend. */ /* Find end of next segment, copy tentatively to pathend. */
@@ -557,14 +559,15 @@ glob2(Char *pathbuf, Char *pathend, Char *pattern, glob_t *pglob)
while (*pattern == CHAR_SEP) while (*pattern == CHAR_SEP)
*pathend++ = *pattern++; *pathend++ = *pattern++;
} else /* Need expansion, recurse. */ } else /* Need expansion, recurse. */
return(glob3(pathbuf, pathend, pattern, p, pglob)); return(glob3(pathbuf, pathend, pattern, p, pglob,
limit));
} }
/* CHAR_NOTREACHED */ /* NOTREACHED */
} }
static int static int
glob3(Char *pathbuf, Char *pathend, Char *pattern, Char *restpattern, glob3(Char *pathbuf, Char *pathend, Char *pattern, Char *restpattern,
glob_t *pglob) glob_t *pglob, size_t *limit)
{ {
struct dirent *dp; struct dirent *dp;
DIR *dirp; DIR *dirp;
@@ -614,7 +617,7 @@ glob3(Char *pathbuf, Char *pathend, Char *pattern, Char *restpattern,
*pathend = CHAR_EOS; *pathend = CHAR_EOS;
continue; continue;
} }
err = glob2(pathbuf, --dc, restpattern, pglob); err = glob2(pathbuf, --dc, restpattern, pglob, limit);
if (err) if (err)
break; break;
} }
@@ -642,11 +645,11 @@ glob3(Char *pathbuf, Char *pathend, Char *pattern, Char *restpattern,
* 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) globextend(const Char *path, glob_t *pglob, size_t *limit)
{ {
char **pathv; char **pathv;
int i; int i;
u_int newsize; size_t newsize, len;
char *copy; char *copy;
const Char *p; const Char *p;
@@ -667,11 +670,19 @@ globextend(const Char *path, glob_t *pglob)
for (p = path; *p++;) for (p = path; *p++;)
continue; continue;
if ((copy = malloc(p - path)) != NULL) { len = (size_t)(p - path);
*limit += len;
if ((copy = malloc(len)) != NULL) {
g_Ctoc(path, copy); g_Ctoc(path, copy);
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) {
errno = 0;
return(GLOB_NOSPACE);
}
return(copy == NULL ? GLOB_NOSPACE : 0); return(copy == NULL ? GLOB_NOSPACE : 0);
} }
@@ -739,6 +750,7 @@ globfree(glob_t *pglob)
if (*pp) if (*pp)
free(*pp); free(*pp);
free(pglob->gl_pathv); free(pglob->gl_pathv);
pglob->gl_pathv = NULL;
} }
} }
@@ -781,11 +793,11 @@ g_stat(Char *fn, struct stat *sb, glob_t *pglob)
} }
static Char * static Char *
g_strchr(Char *str, int ch) g_strchr(const Char *str, int ch)
{ {
do { do {
if (*str == ch) if (*str == ch)
return (str); return (Char *)str;
} while (*str++); } while (*str++);
return (NULL); return (NULL);
} }

View File

@@ -74,6 +74,7 @@ typedef struct {
#define GLOB_NOMAGIC 0x0200 /* GLOB_NOCHECK without magic chars (csh). */ #define GLOB_NOMAGIC 0x0200 /* GLOB_NOCHECK without magic chars (csh). */
#define GLOB_QUOTE 0x0400 /* Quote special chars with \. */ #define GLOB_QUOTE 0x0400 /* Quote special chars with \. */
#define GLOB_TILDE 0x0800 /* Expand tilde names from the passwd file. */ #define GLOB_TILDE 0x0800 /* Expand tilde names from the passwd file. */
#define GLOB_LIMIT 0x1000 /* Limit memory used by matches to ARG_MAX */
#define GLOB_NOSPACE (-1) /* Malloc call failed. */ #define GLOB_NOSPACE (-1) /* Malloc call failed. */
#define GLOB_ABEND (-2) /* Unignored error. */ #define GLOB_ABEND (-2) /* Unignored error. */