- (djm) Another openbsd-compat/glob.c sync

This commit is contained in:
Damien Miller 2001-03-30 10:23:17 +10:00
parent 7de696e798
commit d8f72ca6d5
2 changed files with 60 additions and 30 deletions

View File

@ -1,3 +1,6 @@
20010330
- (djm) Another openbsd-compat/glob.c sync
20010329 20010329
- OpenBSD CVS Sync - OpenBSD CVS Sync
- stevesk@cvs.openbsd.org 2001/03/26 15:47:59 - stevesk@cvs.openbsd.org 2001/03/26 15:47:59
@ -4767,4 +4770,4 @@
- Wrote replacements for strlcpy and mkdtemp - Wrote replacements for strlcpy and mkdtemp
- Released 1.0pre1 - Released 1.0pre1
$Id: ChangeLog,v 1.1034 2001/03/29 00:45:12 mouring Exp $ $Id: ChangeLog,v 1.1035 2001/03/30 00:23:17 djm Exp $

View File

@ -56,7 +56,7 @@ get_arg_max()
#if 0 #if 0
static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93"; static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93";
#else #else
static char rcsid[] = "$OpenBSD: glob.c,v 1.13 2001/03/28 08:00:00 deraadt Exp $"; static char rcsid[] = "$OpenBSD: glob.c,v 1.14 2001/03/28 20:54:19 millert Exp $";
#endif #endif
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
@ -143,10 +143,11 @@ static DIR *g_opendir __P((Char *, glob_t *));
static Char *g_strchr __P((Char *, int)); static Char *g_strchr __P((Char *, int));
static int g_stat __P((Char *, struct stat *, glob_t *)); static int g_stat __P((Char *, struct stat *, glob_t *));
static int glob0 __P((const Char *, glob_t *)); static int glob0 __P((const Char *, glob_t *));
static int glob1 __P((Char *, glob_t *, size_t *)); static int glob1 __P((Char *, Char *, glob_t *, size_t *));
static int glob2 __P((Char *, Char *, Char *, glob_t *, size_t *)); static int glob2 __P((Char *, Char *, Char *, Char *, Char *, Char *,
static int glob3 __P((Char *, Char *, Char *, Char *, glob_t *, glob_t *, size_t *));
size_t *)); static int glob3 __P((Char *, Char *, Char *, Char *, Char *, Char *,
Char *, Char *, glob_t *, size_t *));
static int globextend __P((const Char *, glob_t *, size_t *)); static int globextend __P((const Char *, glob_t *, size_t *));
static const Char * static const Char *
globtilde __P((const Char *, Char *, size_t, glob_t *)); globtilde __P((const Char *, Char *, size_t, glob_t *));
@ -165,7 +166,7 @@ glob(pattern, flags, errfunc, pglob)
{ {
const u_char *patnext; const u_char *patnext;
int c; int c;
Char *bufnext, *bufend, patbuf[MAXPATHLEN+1]; Char *bufnext, *bufend, patbuf[MAXPATHLEN];
patnext = (u_char *) pattern; patnext = (u_char *) pattern;
if (!(flags & GLOB_APPEND)) { if (!(flags & GLOB_APPEND)) {
@ -179,7 +180,7 @@ glob(pattern, flags, errfunc, pglob)
pglob->gl_matchc = 0; pglob->gl_matchc = 0;
bufnext = patbuf; bufnext = patbuf;
bufend = bufnext + MAXPATHLEN; bufend = bufnext + MAXPATHLEN - 1;
if (flags & GLOB_NOESCAPE) if (flags & GLOB_NOESCAPE)
while (bufnext < bufend && (c = *patnext++) != EOS) while (bufnext < bufend && (c = *patnext++) != EOS)
*bufnext++ = c; *bufnext++ = c;
@ -209,7 +210,8 @@ glob(pattern, flags, errfunc, pglob)
* invoke the standard globbing routine to glob the rest of the magic * invoke the standard globbing routine to glob the rest of the magic
* characters * characters
*/ */
static int globexp1(pattern, pglob) static int
globexp1(pattern, pglob)
const Char *pattern; const Char *pattern;
glob_t *pglob; glob_t *pglob;
{ {
@ -233,7 +235,8 @@ static int globexp1(pattern, pglob)
* If it succeeds then it invokes globexp1 with the new pattern. * If it succeeds then it invokes globexp1 with the new pattern.
* 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 globexp2(ptr, pattern, pglob, rv) static int
globexp2(ptr, pattern, pglob, rv)
const Char *ptr, *pattern; const Char *ptr, *pattern;
glob_t *pglob; glob_t *pglob;
int *rv; int *rv;
@ -241,11 +244,12 @@ static int globexp2(ptr, pattern, pglob, rv)
int i; int i;
Char *lm, *ls; Char *lm, *ls;
const Char *pe, *pm, *pl; const Char *pe, *pm, *pl;
Char patbuf[MAXPATHLEN + 1]; Char patbuf[MAXPATHLEN];
/* copy part up to the brace */ /* copy part up to the brace */
for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++) for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
continue; continue;
*lm = EOS;
ls = lm; ls = lm;
/* Find the balanced brace */ /* Find the balanced brace */
@ -414,11 +418,10 @@ glob0(pattern, pglob)
{ {
const Char *qpatnext; const Char *qpatnext;
int c, err, oldpathc; int c, err, oldpathc;
Char *bufnext, patbuf[MAXPATHLEN+1]; Char *bufnext, patbuf[MAXPATHLEN];
size_t limit = 0; size_t limit = 0;
qpatnext = globtilde(pattern, patbuf, sizeof(patbuf) / sizeof(Char), qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
pglob);
oldpathc = pglob->gl_pathc; oldpathc = pglob->gl_pathc;
bufnext = patbuf; bufnext = patbuf;
@ -474,7 +477,7 @@ glob0(pattern, pglob)
qprintf("glob0:", patbuf); qprintf("glob0:", patbuf);
#endif #endif
if ((err = glob1(patbuf, pglob, &limit)) != 0) if ((err = glob1(patbuf, patbuf+MAXPATHLEN-1, pglob, &limit)) != 0)
return(err); return(err);
/* /*
@ -505,17 +508,19 @@ compare(p, q)
} }
static int static int
glob1(pattern, pglob, limitp) glob1(pattern, pattern_last, pglob, limitp)
Char *pattern; Char *pattern, *pattern_last;
glob_t *pglob; glob_t *pglob;
size_t *limitp; size_t *limitp;
{ {
Char pathbuf[MAXPATHLEN+1]; Char pathbuf[MAXPATHLEN];
/* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */ /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
if (*pattern == EOS) if (*pattern == EOS)
return(0); return(0);
return(glob2(pathbuf, pathbuf, pattern, pglob, limitp)); return(glob2(pathbuf, pathbuf+MAXPATHLEN-1,
pathbuf, pathbuf+MAXPATHLEN-1,
pattern, pattern_last, pglob, limitp));
} }
/* /*
@ -524,8 +529,10 @@ glob1(pattern, pglob, limitp)
* meta characters. * meta characters.
*/ */
static int static int
glob2(pathbuf, pathend, pattern, pglob, limitp) glob2(pathbuf, pathbuf_last, pathend, pathend_last, pattern,
Char *pathbuf, *pathend, *pattern; pattern_last, pglob, limitp)
Char *pathbuf, *pathbuf_last, *pathend, *pathend_last;
Char *pattern, *pattern_last;
glob_t *pglob; glob_t *pglob;
size_t *limitp; size_t *limitp;
{ {
@ -548,6 +555,8 @@ glob2(pathbuf, pathend, pattern, pglob, limitp)
(S_ISLNK(sb.st_mode) && (S_ISLNK(sb.st_mode) &&
(g_stat(pathbuf, &sb, pglob) == 0) && (g_stat(pathbuf, &sb, pglob) == 0) &&
S_ISDIR(sb.st_mode)))) { S_ISDIR(sb.st_mode)))) {
if (pathend+1 > pathend_last)
return (1);
*pathend++ = SEP; *pathend++ = SEP;
*pathend = EOS; *pathend = EOS;
} }
@ -561,25 +570,33 @@ glob2(pathbuf, pathend, pattern, pglob, limitp)
while (*p != EOS && *p != SEP) { while (*p != EOS && *p != SEP) {
if (ismeta(*p)) if (ismeta(*p))
anymeta = 1; anymeta = 1;
if (q+1 > pathend_last)
return (1);
*q++ = *p++; *q++ = *p++;
} }
if (!anymeta) { /* No expansion, do next segment. */ if (!anymeta) { /* No expansion, do next segment. */
pathend = q; pathend = q;
pattern = p; pattern = p;
while (*pattern == SEP) while (*pattern == SEP) {
if (pathend+1 > pathend_last)
return (1);
*pathend++ = *pattern++; *pathend++ = *pattern++;
}
} else } else
/* Need expansion, recurse. */ /* Need expansion, recurse. */
return(glob3(pathbuf, pathend, pattern, p, pglob, return(glob3(pathbuf, pathbuf_last, pathend,
limitp)); pathend_last, pattern, pattern_last,
p, pattern_last, pglob, limitp));
} }
/* NOTREACHED */ /* NOTREACHED */
} }
static int static int
glob3(pathbuf, pathend, pattern, restpattern, pglob, limitp) glob3(pathbuf, pathbuf_last, pathend, pathend_last, pattern, pattern_last,
Char *pathbuf, *pathend, *pattern, *restpattern; restpattern, restpattern_last, pglob, limitp)
Char *pathbuf, *pathbuf_last, *pathend, *pathend_last;
Char *pattern, *pattern_last, *restpattern, *restpattern_last;
glob_t *pglob; glob_t *pglob;
size_t *limitp; size_t *limitp;
{ {
@ -596,6 +613,8 @@ glob3(pathbuf, pathend, pattern, restpattern, pglob, limitp)
*/ */
struct dirent *(*readdirfunc)(); struct dirent *(*readdirfunc)();
if (pathend > pathend_last)
return (1);
*pathend = EOS; *pathend = EOS;
errno = 0; errno = 0;
@ -625,14 +644,22 @@ glob3(pathbuf, pathend, pattern, restpattern, pglob, limitp)
/* Initial DOT must be matched literally. */ /* Initial DOT must be matched literally. */
if (dp->d_name[0] == DOT && *pattern != DOT) if (dp->d_name[0] == DOT && *pattern != DOT)
continue; continue;
for (sc = (u_char *) dp->d_name, dc = pathend; dc = pathend;
(*dc++ = *sc++) != EOS;) sc = (u_char *) dp->d_name;
continue; while (dc < pathend_last && (*dc++ = *sc++) != EOS)
;
if (dc >= pathend_last) {
*dc = EOS;
err = 1;
break;
}
if (!match(pathend, pattern, restpattern)) { if (!match(pathend, pattern, restpattern)) {
*pathend = EOS; *pathend = EOS;
continue; continue;
} }
err = glob2(pathbuf, --dc, restpattern, pglob, limitp); err = glob2(pathbuf, pathbuf_last, --dc, pathend_last,
restpattern, restpattern_last, pglob, limitp);
if (err) if (err)
break; break;
} }