summaryrefslogtreecommitdiffstats
path: root/crypto/heimdal/lib/roken/glob.c
diff options
context:
space:
mode:
authorassar <assar@FreeBSD.org>2001-06-21 02:12:07 +0000
committerassar <assar@FreeBSD.org>2001-06-21 02:12:07 +0000
commit0c8fa354358381b3f1b92598e7f1b46f8cf744cc (patch)
treeed28ffb73cc0ae48a9892dab3f10b09bc36436d5 /crypto/heimdal/lib/roken/glob.c
parent06c859ecf534f468a52f24a3eb14409d73a4907c (diff)
downloadFreeBSD-src-0c8fa354358381b3f1b92598e7f1b46f8cf744cc.zip
FreeBSD-src-0c8fa354358381b3f1b92598e7f1b46f8cf744cc.tar.gz
import of heimdal 0.3f
Diffstat (limited to 'crypto/heimdal/lib/roken/glob.c')
-rw-r--r--crypto/heimdal/lib/roken/glob.c61
1 files changed, 38 insertions, 23 deletions
diff --git a/crypto/heimdal/lib/roken/glob.c b/crypto/heimdal/lib/roken/glob.c
index 66e8ec6..e4bc0dc 100644
--- a/crypto/heimdal/lib/roken/glob.c
+++ b/crypto/heimdal/lib/roken/glob.c
@@ -87,6 +87,9 @@
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
+#ifdef HAVE_LIMITS_H
+#include <limits.h>
+#endif
#include "glob.h"
#include "roken.h"
@@ -145,16 +148,16 @@ static int compare (const void *, const void *);
static void g_Ctoc (const Char *, char *);
static int g_lstat (Char *, struct stat *, glob_t *);
static DIR *g_opendir (Char *, glob_t *);
-static Char *g_strchr (Char *, int);
+static Char *g_strchr (const Char *, int);
#ifdef notdef
static Char *g_strcat (Char *, const Char *);
#endif
static int g_stat (Char *, struct stat *, glob_t *);
static int glob0 (const Char *, glob_t *);
-static int glob1 (Char *, glob_t *);
-static int glob2 (Char *, Char *, Char *, glob_t *);
-static int glob3 (Char *, Char *, Char *, Char *, glob_t *);
-static int globextend (const Char *, glob_t *);
+static int glob1 (Char *, glob_t *, size_t *);
+static int glob2 (Char *, Char *, Char *, glob_t *, size_t *);
+static int glob3 (Char *, Char *, Char *, Char *, glob_t *, size_t *);
+static int globextend (const Char *, glob_t *, size_t *);
static const Char * globtilde (const Char *, Char *, glob_t *);
static int globexp1 (const Char *, glob_t *);
static int globexp2 (const Char *, const Char *, glob_t *, int *);
@@ -173,7 +176,7 @@ glob(const char *pattern,
int c;
Char *bufnext, *bufend, patbuf[MaxPathLen+1];
- patnext = (u_char *) pattern;
+ patnext = (const u_char *) pattern;
if (!(flags & GLOB_APPEND)) {
pglob->gl_pathc = 0;
pglob->gl_pathv = NULL;
@@ -224,7 +227,7 @@ static int globexp1(const Char *pattern, glob_t *pglob)
if (pattern[0] == CHAR_LBRACE && pattern[1] == CHAR_RBRACE && pattern[2] == CHAR_EOS)
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))
return rv;
@@ -405,6 +408,7 @@ glob0(const Char *pattern, glob_t *pglob)
const Char *qpatnext;
int c, err, oldpathc;
Char *bufnext, patbuf[MaxPathLen+1];
+ size_t limit = 0;
qpatnext = globtilde(pattern, patbuf, pglob);
oldpathc = pglob->gl_pathc;
@@ -418,7 +422,7 @@ glob0(const Char *pattern, glob_t *pglob)
if (c == CHAR_NOT)
++qpatnext;
if (*qpatnext == CHAR_EOS ||
- g_strchr((Char *) qpatnext+1, CHAR_RBRACKET) == NULL) {
+ g_strchr(qpatnext+1, CHAR_RBRACKET) == NULL) {
*bufnext++ = CHAR_LBRACKET;
if (c == CHAR_NOT)
--qpatnext;
@@ -462,7 +466,7 @@ glob0(const Char *pattern, glob_t *pglob)
qprintf("glob0:", patbuf);
#endif
- if ((err = glob1(patbuf, pglob)) != 0)
+ if ((err = glob1(patbuf, pglob, &limit)) != 0)
return(err);
/*
@@ -475,7 +479,7 @@ glob0(const Char *pattern, glob_t *pglob)
((pglob->gl_flags & GLOB_NOCHECK) ||
((pglob->gl_flags & GLOB_NOMAGIC) &&
!(pglob->gl_flags & GLOB_MAGCHAR))))
- return(globextend(pattern, pglob));
+ return(globextend(pattern, pglob, &limit));
else if (!(pglob->gl_flags & GLOB_NOSORT))
qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
pglob->gl_pathc - oldpathc, sizeof(char *), compare);
@@ -489,14 +493,14 @@ compare(const void *p, const void *q)
}
static int
-glob1(Char *pattern, glob_t *pglob)
+glob1(Char *pattern, glob_t *pglob, size_t *limit)
{
Char pathbuf[MaxPathLen+1];
/* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
if (*pattern == CHAR_EOS)
return(0);
- return(glob2(pathbuf, pathbuf, pattern, pglob));
+ return(glob2(pathbuf, pathbuf, pattern, pglob, limit));
}
/*
@@ -514,7 +518,8 @@ glob1(Char *pattern, glob_t *pglob)
#endif
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;
Char *p, *q;
@@ -539,7 +544,7 @@ glob2(Char *pathbuf, Char *pathend, Char *pattern, glob_t *pglob)
*pathend = CHAR_EOS;
}
++pglob->gl_matchc;
- return(globextend(pathbuf, pglob));
+ return(globextend(pathbuf, pglob, limit));
}
/* Find end of next segment, copy tentatively to pathend. */
@@ -557,14 +562,15 @@ glob2(Char *pathbuf, Char *pathend, Char *pattern, glob_t *pglob)
while (*pattern == CHAR_SEP)
*pathend++ = *pattern++;
} else /* Need expansion, recurse. */
- return(glob3(pathbuf, pathend, pattern, p, pglob));
+ return(glob3(pathbuf, pathend, pattern, p, pglob,
+ limit));
}
- /* CHAR_NOTREACHED */
+ /* NOTREACHED */
}
static int
glob3(Char *pathbuf, Char *pathend, Char *pattern, Char *restpattern,
- glob_t *pglob)
+ glob_t *pglob, size_t *limit)
{
struct dirent *dp;
DIR *dirp;
@@ -614,7 +620,7 @@ glob3(Char *pathbuf, Char *pathend, Char *pattern, Char *restpattern,
*pathend = CHAR_EOS;
continue;
}
- err = glob2(pathbuf, --dc, restpattern, pglob);
+ err = glob2(pathbuf, --dc, restpattern, pglob, limit);
if (err)
break;
}
@@ -642,11 +648,11 @@ glob3(Char *pathbuf, Char *pathend, Char *pattern, Char *restpattern,
* gl_pathv points to (gl_offs + gl_pathc + 1) items.
*/
static int
-globextend(const Char *path, glob_t *pglob)
+globextend(const Char *path, glob_t *pglob, size_t *limit)
{
char **pathv;
int i;
- u_int newsize;
+ size_t newsize, len;
char *copy;
const Char *p;
@@ -667,11 +673,19 @@ globextend(const Char *path, glob_t *pglob)
for (p = path; *p++;)
continue;
- if ((copy = malloc(p - path)) != NULL) {
+ len = (size_t)(p - path);
+ *limit += len;
+ if ((copy = malloc(len)) != NULL) {
g_Ctoc(path, copy);
pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
}
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);
}
@@ -739,6 +753,7 @@ globfree(glob_t *pglob)
if (*pp)
free(*pp);
free(pglob->gl_pathv);
+ pglob->gl_pathv = NULL;
}
}
@@ -781,11 +796,11 @@ g_stat(Char *fn, struct stat *sb, glob_t *pglob)
}
static Char *
-g_strchr(Char *str, int ch)
+g_strchr(const Char *str, int ch)
{
do {
if (*str == ch)
- return (str);
+ return (Char *)str;
} while (*str++);
return (NULL);
}
OpenPOWER on IntegriCloud