* glob.c: Remove.

* glob.cc: New file.  Latest glob version from FreeBSD plus Cygwin
	specific changes (__stat64/__stat32, ignore_case_with_glob,
	drop collate functions).
	(glob3): Return GLOB_ABORTED in case directory is unreadable and
	GLOB_ERR is set, as demanded by SUSv3.
	* glob.h: Import latest version from FreeBSD.
This commit is contained in:
Corinna Vinschen 2006-10-17 17:34:56 +00:00
parent c4e12ec359
commit 733c9ed374
3 changed files with 328 additions and 273 deletions

View File

@ -1,3 +1,13 @@
2006-10-17 Corinna Vinschen <corinna@vinschen.de>
* glob.c: Remove.
* glob.cc: New file. Latest glob version from FreeBSD plus Cygwin
specific changes (__stat64/__stat32, ignore_case_with_glob,
drop collate functions).
(glob3): Return GLOB_ABORTED in case directory is unreadable and
GLOB_ERR is set, as demanded by SUSv3.
* glob.h: Import latest version from FreeBSD.
2006-10-17 Corinna Vinschen <corinna@vinschen.de> 2006-10-17 Corinna Vinschen <corinna@vinschen.de>
* winsup.h (WINVER): Define instead of _WIN32_WINNT. Set to 0x0502. * winsup.h (WINVER): Define instead of _WIN32_WINNT. Set to 0x0502.

View File

@ -1,5 +1,3 @@
/* $NetBSD: __glob13.c,v 1.1.2.1 1997/10/22 06:41:27 thorpej Exp $ */
/* /*
* Copyright (c) 1989, 1993 * Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved. * The Regents of the University of California. All rights reserved.
@ -36,7 +34,13 @@
* SUCH DAMAGE. * SUCH DAMAGE.
*/ */
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93";
#endif /* LIBC_SCCS and not lint */
#include <sys/cdefs.h> #include <sys/cdefs.h>
#ifdef __CYGWIN
__FBSDID("$FreeBSD: /repoman/r/ncvs/src/lib/libc/gen/glob.c,v 1.25 2006/06/05 18:22:13 delphij Exp $");
#endif
/* /*
* glob(3) -- a superset of the one defined in POSIX 1003.2. * glob(3) -- a superset of the one defined in POSIX 1003.2.
@ -63,6 +67,16 @@
* Number of matches in the current invocation of glob. * Number of matches in the current invocation of glob.
*/ */
/*
* Some notes on multibyte character support:
* 1. Patterns with illegal byte sequences match nothing - even if
* GLOB_NOCHECK is specified.
* 2. Illegal byte sequences in filenames are handled by treating them as
* single-byte characters with a value of the first byte of the sequence
* cast to wchar_t.
* 3. State-dependent encodings are not currently supported.
*/
#include "winsup.h" #include "winsup.h"
#include <sys/param.h> #include <sys/param.h>
@ -72,14 +86,24 @@
#include <dirent.h> #include <dirent.h>
#include <errno.h> #include <errno.h>
#include <glob.h> #include <glob.h>
#include <limits.h>
#include <pwd.h> #include <pwd.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <windows.h> #include <wchar.h>
//#include "collate.h"
#include "cygerrno.h"
#include "security.h"
#include "path.h"
#include "fhandler.h"
#include "dtable.h"
#include "cygheap.h"
#include "perprocess.h" #include "perprocess.h"
#include "cygwin/version.h" #include "cygwin/version.h"
@ -87,22 +111,10 @@
#define ARG_MAX 32000 /* See CreateProcess */ #define ARG_MAX 32000 /* See CreateProcess */
#endif #endif
#ifdef __weak_alias #undef MAXPATHLEN
#ifdef __LIBC12_SOURCE__ #define MAXPATHLEN 16384
__weak_alias(glob,_glob);
__weak_alias(globfree,_globfree);
#else
#error "XXX THESE ARE NOT RIGHT!"
__weak_alias(__glob13,___glob13);
__weak_alias(__globfree13,___globfree13);
#endif /* __LIBC12_SOURCE__ */
#endif /* __weak_alias */
#ifdef __LIBC12_SOURCE__ extern BOOL ignore_case_with_glob;
#define STAT stat12
#else
#define STAT __stat64
#endif
#define DOLLAR '$' #define DOLLAR '$'
#define DOT '.' #define DOT '.'
@ -124,26 +136,26 @@ __weak_alias(__globfree13,___globfree13);
#ifndef DEBUG #ifndef DEBUG
#define M_QUOTE 0x8000 #define M_QUOTE 0x8000000000ULL
#define M_PROTECT 0x4000 #define M_PROTECT 0x4000000000ULL
#define M_MASK 0xffff #define M_MASK 0xffffffffffULL
#define M_ASCII 0x00ff #define M_CHAR 0x00ffffffffULL
typedef u_short Char; typedef uint_fast64_t Char;
#else #else
#define M_QUOTE 0x80 #define M_QUOTE 0x80
#define M_PROTECT 0x40 #define M_PROTECT 0x40
#define M_MASK 0xff #define M_MASK 0xff
#define M_ASCII 0x7f #define M_CHAR 0x7f
typedef char Char; typedef char Char;
#endif #endif
#define CHAR(c) ((Char)((c)&M_ASCII)) #define CHAR(c) ((Char)((c)&M_CHAR))
#define META(c) ((Char)((c)|M_QUOTE)) #define META(c) ((Char)((c)|M_QUOTE))
#define M_ALL META('*') #define M_ALL META('*')
#define M_END META(']') #define M_END META(']')
@ -154,78 +166,96 @@ typedef char Char;
#define ismeta(c) (((c)&M_QUOTE) != 0) #define ismeta(c) (((c)&M_QUOTE) != 0)
static int compare __P((const void *, const void *)); static int compare(const void *, const void *);
static void g_Ctoc __P((const Char *, char *)); static int g_Ctoc(const Char *, char *, size_t);
static int g_lstat __P((Char *, struct STAT *, glob_t *)); static int g_lstat(Char *, struct __stat64 *, glob_t *);
static DIR *g_opendir __P((Char *, glob_t *)); static DIR *g_opendir(Char *, glob_t *);
static Char *g_strchr __P((Char *, int)); static Char *g_strchr(Char *, wchar_t);
#ifdef notdef #ifdef notdef
static Char *g_strcat __P((Char *, const Char *)); static Char *g_strcat(Char *, const Char *);
#endif #endif
static int g_stat __P((Char *, struct STAT *, glob_t *)); static int g_stat(Char *, struct __stat64 *, glob_t *);
static int glob0 __P((const Char *, glob_t *)); static int glob0(const Char *, glob_t *, size_t *);
static int glob1 __P((Char *, glob_t *, size_t *)); static int glob1(Char *, glob_t *, size_t *);
static int glob2 __P((Char *, Char *, Char *, glob_t *, size_t *)); static int glob2(Char *, Char *, Char *, Char *, glob_t *, size_t *);
static int glob3 __P((Char *, Char *, Char *, Char *, glob_t *, size_t *)); static int glob3(Char *, Char *, Char *, Char *, Char *, glob_t *, size_t *);
static int globextend __P((const Char *, glob_t *, size_t *)); static int globextend(const Char *, glob_t *, size_t *);
static const Char * globtilde __P((const Char *, Char *, glob_t *)); static const Char *
static int globexp1 __P((const Char *, glob_t *)); globtilde(const Char *, Char *, size_t, glob_t *);
static int globexp2 __P((const Char *, const Char *, glob_t *, int *)); static int globexp1(const Char *, glob_t *, size_t *);
static int match __P((Char *, Char *, Char *)); static int globexp2(const Char *, const Char *, glob_t *, int *, size_t *);
static int match(Char *, Char *, Char *);
#ifdef DEBUG #ifdef DEBUG
static void qprintf __P((const char *, Char *)); static void qprintf(const char *, Char *);
#endif #endif
#undef MAXPATHLEN
#define MAXPATHLEN 16384
extern BOOL ignore_case_with_glob;
int int
glob(pattern, flags, errfunc, pglob) glob(const char *pattern, int flags, int (*errfunc)(const char *, int), glob_t *pglob)
const char *pattern;
int flags, (*errfunc) __P((const char *, int));
glob_t *pglob;
{ {
const u_char *patnext; const char *patnext;
int c; size_t limit;
Char *bufnext, *bufend, patbuf[MAXPATHLEN+1]; Char *bufnext, *bufend, patbuf[MAXPATHLEN], prot;
mbstate_t mbs;
wchar_t wc;
size_t clen;
patnext = (u_char *) pattern; patnext = 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;
if (!(flags & GLOB_DOOFFS)) if (!(flags & GLOB_DOOFFS))
pglob->gl_offs = 0; pglob->gl_offs = 0;
} }
if (flags & GLOB_LIMIT) {
limit = pglob->gl_matchc;
if (limit == 0)
limit = ARG_MAX;
} else
limit = 0;
pglob->gl_flags = flags & ~GLOB_MAGCHAR; pglob->gl_flags = flags & ~GLOB_MAGCHAR;
pglob->gl_errfunc = errfunc; pglob->gl_errfunc = errfunc;
pglob->gl_matchc = 0; pglob->gl_matchc = 0;
bufnext = patbuf; bufnext = patbuf;
bufend = bufnext + MAXPATHLEN; bufend = bufnext + MAXPATHLEN - 1;
if (flags & GLOB_QUOTE) { if (flags & GLOB_NOESCAPE) {
memset(&mbs, 0, sizeof(mbs));
while (bufend - bufnext >= MB_CUR_MAX) {
clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
if (clen == (size_t)-1 || clen == (size_t)-2)
return (GLOB_NOMATCH);
else if (clen == 0)
break;
*bufnext++ = wc;
patnext += clen;
}
} else {
/* Protect the quoted characters. */ /* Protect the quoted characters. */
while (bufnext < bufend && (c = *patnext++) != EOS) memset(&mbs, 0, sizeof(mbs));
if (c == QUOTE) { while (bufend - bufnext >= MB_CUR_MAX) {
if ((c = *patnext++) == EOS) { if (*patnext == QUOTE) {
c = QUOTE; if (*++patnext == EOS) {
--patnext; *bufnext++ = QUOTE | M_PROTECT;
continue;
} }
*bufnext++ = c | M_PROTECT; prot = M_PROTECT;
} else
prot = 0;
clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
if (clen == (size_t)-1 || clen == (size_t)-2)
return (GLOB_NOMATCH);
else if (clen == 0)
break;
*bufnext++ = wc | prot;
patnext += clen;
} }
else
*bufnext++ = c;
} }
else
while (bufnext < bufend && (c = *patnext++) != EOS)
*bufnext++ = c;
*bufnext = EOS; *bufnext = EOS;
if (flags & GLOB_BRACE) if (flags & GLOB_BRACE)
return globexp1(patbuf, pglob); return globexp1(patbuf, pglob, &limit);
else else
return glob0(patbuf, pglob); return glob0(patbuf, pglob, &limit);
} }
/* /*
@ -233,22 +263,21 @@ 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
const Char *pattern; globexp1(const Char *pattern, glob_t *pglob, size_t *limit)
glob_t *pglob;
{ {
const Char* ptr = pattern; const Char* ptr = pattern;
int rv; int rv;
/* 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); return glob0(pattern, pglob, limit);
while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL) while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL)
if (!globexp2(ptr, pattern, pglob, &rv)) if (!globexp2(ptr, pattern, pglob, &rv, limit))
return rv; return rv;
return glob0(pattern, pglob); return glob0(pattern, pglob, limit);
} }
@ -257,19 +286,18 @@ 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
const Char *ptr, *pattern; globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv, size_t *limit)
glob_t *pglob;
int *rv;
{ {
int i; int i;
Char *lm, *ls; Char *lm, *ls;
const Char *pe, *pm, *pl; const Char *pe, *pm, *pm1, *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 */
@ -296,7 +324,7 @@ static int globexp2(ptr, pattern, pglob, rv)
/* Non matching braces; just glob the pattern */ /* Non matching braces; just glob the pattern */
if (i != 0 || *pe == EOS) { if (i != 0 || *pe == EOS) {
*rv = glob0(patbuf, pglob); *rv = glob0(patbuf, pglob, limit);
return 0; return 0;
} }
@ -304,14 +332,14 @@ static int globexp2(ptr, pattern, pglob, rv)
switch (*pm) { switch (*pm) {
case LBRACKET: case LBRACKET:
/* Ignore everything between [] */ /* Ignore everything between [] */
for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++) for (pm1 = pm++; *pm != RBRACKET && *pm != EOS; pm++)
continue; continue;
if (*pm == EOS) { if (*pm == EOS) {
/* /*
* We could not find a matching RBRACKET. * We could not find a matching RBRACKET.
* Ignore and just look for RBRACE * Ignore and just look for RBRACE
*/ */
pm = pl; pm = pm1;
} }
break; break;
@ -343,7 +371,7 @@ static int globexp2(ptr, pattern, pglob, rv)
#ifdef DEBUG #ifdef DEBUG
qprintf("globexp2:", patbuf); qprintf("globexp2:", patbuf);
#endif #endif
*rv = globexp1(patbuf, pglob); *rv = globexp1(patbuf, pglob, limit);
/* move after the comma, to the next string */ /* move after the comma, to the next string */
pl = pm + 1; pl = pm + 1;
@ -363,36 +391,40 @@ static int globexp2(ptr, pattern, pglob, rv)
* expand tilde from the passwd file. * expand tilde from the passwd file.
*/ */
static const Char * static const Char *
globtilde(pattern, patbuf, pglob) globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
const Char *pattern;
Char *patbuf;
glob_t *pglob;
{ {
struct passwd *pwd; struct passwd *pwd;
char *h; char *h;
const Char *p; const Char *p;
Char *b; Char *b, *eb;
if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE)) if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
return pattern; return pattern;
/* Copy up to the end of the string or / */ /*
for (p = pattern + 1, h = (char *) patbuf; *p && *p != SLASH; * Copy up to the end of the string or /
*h++ = *p++) */
eb = &patbuf[patbuf_len - 1];
for (p = pattern + 1, h = (char *) patbuf;
h < (char *)eb && *p && *p != SLASH; *h++ = *p++)
continue; continue;
*h = EOS; *h = EOS;
if (((char *) patbuf)[0] == EOS) { if (((char *) patbuf)[0] == EOS) {
/* /*
* handle a plain ~ or ~/ by expanding $HOME * handle a plain ~ or ~/ by expanding $HOME first (iff
* first and then trying the password file * we're not running setuid or setgid) and then trying
* the password file
*/ */
if ((h = getenv("HOME")) == NULL) { if (cygheap->user.issetuid() != 0 ||
if ((pwd = getpwuid32(getuid32())) == NULL) (h = getenv("HOME")) == NULL) {
return pattern; if (((h = getlogin()) != NULL &&
else (pwd = getpwnam(h)) != NULL) ||
(pwd = getpwuid32(getuid32())) != NULL)
h = pwd->pw_dir; h = pwd->pw_dir;
else
return pattern;
} }
} }
else { else {
@ -406,12 +438,13 @@ globtilde(pattern, patbuf, pglob)
} }
/* Copy the home directory */ /* Copy the home directory */
for (b = patbuf; *h; *b++ = *h++) for (b = patbuf; b < eb && *h; *b++ = *h++)
continue; continue;
/* Append the rest of the pattern */ /* Append the rest of the pattern */
while ((*b++ = *p++) != EOS) while (b < eb && (*b++ = *p++) != EOS)
continue; continue;
*b = EOS;
return patbuf; return patbuf;
} }
@ -421,20 +454,17 @@ globtilde(pattern, patbuf, pglob)
* The main glob() routine: compiles the pattern (optionally processing * The main glob() routine: compiles the pattern (optionally processing
* quotes), calls glob1() to do the real pattern matching, and finally * quotes), calls glob1() to do the real pattern matching, and finally
* sorts the list (unless unsorted operation is requested). Returns 0 * sorts the list (unless unsorted operation is requested). Returns 0
* if things went well, nonzero if errors occurred. It is not an error * if things went well, nonzero if errors occurred.
* to find no matches.
*/ */
static int static int
glob0(pattern, pglob) glob0(const Char *pattern, glob_t *pglob, size_t *limit)
const Char *pattern;
glob_t *pglob;
{ {
const Char *qpatnext; const Char *qpatnext;
int c, err, oldpathc; int c, err;
Char *bufnext, patbuf[MAXPATHLEN+1]; size_t oldpathc;
size_t limit = 0; Char *bufnext, patbuf[MAXPATHLEN];
qpatnext = globtilde(pattern, patbuf, pglob); qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
oldpathc = pglob->gl_pathc; oldpathc = pglob->gl_pathc;
bufnext = patbuf; bufnext = patbuf;
@ -490,7 +520,7 @@ glob0(pattern, pglob)
qprintf("glob0:", patbuf); qprintf("glob0:", patbuf);
#endif #endif
if ((err = glob1(patbuf, pglob, &limit)) != 0) if ((err = glob1(patbuf, pglob, limit)) != 0)
return(err); return(err);
/* /*
@ -499,36 +529,36 @@ glob0(pattern, pglob)
* and the pattern did not contain any magic characters * and the pattern did not contain any magic characters
* GLOB_NOMAGIC is there just for compatibility with csh. * GLOB_NOMAGIC is there just for compatibility with csh.
*/ */
if (pglob->gl_pathc == oldpathc && if (pglob->gl_pathc == oldpathc) {
((pglob->gl_flags & GLOB_NOCHECK) || if (((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, &limit)); return(globextend(pattern, pglob, limit));
else if (!(pglob->gl_flags & GLOB_NOSORT)) else
return(GLOB_NOMATCH);
}
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);
return(0); return(0);
} }
static int static int
compare(p, q) compare(const void *p, const void *q)
const void *p, *q;
{ {
return(strcmp(*(char **)p, *(char **)q)); return(strcmp(*(char **)p, *(char **)q));
} }
static int static int
glob1(pattern, pglob, limit) glob1(Char *pattern, glob_t *pglob, size_t *limit)
Char *pattern;
glob_t *pglob;
size_t *limit;
{ {
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, limit)); return(glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1,
pattern, pglob, limit));
} }
/* /*
@ -537,12 +567,10 @@ glob1(pattern, pglob, limit)
* meta characters. * meta characters.
*/ */
static int static int
glob2(pathbuf, pathend, pattern, pglob, limit) glob2(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern,
Char *pathbuf, *pathend, *pattern; glob_t *pglob, size_t *limit)
glob_t *pglob;
size_t *limit;
{ {
struct STAT sb; struct __stat64 sb;
Char *p, *q; Char *p, *q;
int anymeta; int anymeta;
@ -561,6 +589,8 @@ glob2(pathbuf, pathend, pattern, pglob, limit)
|| (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 (GLOB_ABORTED);
*pathend++ = SEP; *pathend++ = SEP;
*pathend = EOS; *pathend = EOS;
} }
@ -574,28 +604,32 @@ glob2(pathbuf, pathend, pattern, pglob, limit)
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 (GLOB_ABORTED);
*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 (GLOB_ABORTED);
*pathend++ = *pattern++; *pathend++ = *pattern++;
}
} else /* Need expansion, recurse. */ } else /* Need expansion, recurse. */
return(glob3(pathbuf, pathend, pattern, p, pglob, return(glob3(pathbuf, pathend, pathend_last, pattern, p,
limit)); pglob, limit));
} }
/* NOTREACHED */ /* NOTREACHED */
} }
static int static int
glob3(pathbuf, pathend, pattern, restpattern, pglob, limit) glob3(Char *pathbuf, Char *pathend, Char *pathend_last,
Char *pathbuf, *pathend, *pattern, *restpattern; Char *pattern, Char *restpattern,
glob_t *pglob; glob_t *pglob, size_t *limit)
size_t *limit;
{ {
register struct dirent *dp; struct dirent *dp;
DIR *dirp; DIR *dirp;
int err; int err;
char buf[MAXPATHLEN]; char buf[MAXPATHLEN];
@ -606,20 +640,23 @@ glob3(pathbuf, pathend, pattern, restpattern, pglob, limit)
* and dirent.h as taking pointers to differently typed opaque * and dirent.h as taking pointers to differently typed opaque
* structures. * structures.
*/ */
struct dirent *(*readdirfunc) __P((void *)); struct dirent *(*readdirfunc)(void *);
if (pathend > pathend_last)
return (GLOB_ABORTED);
*pathend = EOS; *pathend = EOS;
errno = 0; errno = 0;
if ((dirp = g_opendir(pathbuf, pglob)) == NULL) { if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
/* TODO: don't call for ENOENT or ENOTDIR? */ /* TODO: don't call for ENOENT or ENOTDIR? */
if (pglob->gl_errfunc) { if (pglob->gl_errfunc) {
g_Ctoc(pathbuf, buf); if (g_Ctoc(pathbuf, buf, sizeof(buf)))
return (GLOB_ABORTED);
if (pglob->gl_errfunc(buf, errno) || if (pglob->gl_errfunc(buf, errno) ||
pglob->gl_flags & GLOB_ERR) pglob->gl_flags & GLOB_ERR)
return (GLOB_ABEND); return (GLOB_ABORTED);
} }
return(0); return((pglob->gl_flags & GLOB_ERR) ? GLOB_ABORTED : 0);
} }
err = 0; err = 0;
@ -628,22 +665,37 @@ glob3(pathbuf, pathend, pattern, restpattern, pglob, limit)
if (pglob->gl_flags & GLOB_ALTDIRFUNC) if (pglob->gl_flags & GLOB_ALTDIRFUNC)
readdirfunc = pglob->gl_readdir; readdirfunc = pglob->gl_readdir;
else else
readdirfunc = (struct dirent *(*)__P((void *))) readdir; readdirfunc = (dirent*(*)(void*)) readdir;
while ((dp = (*readdirfunc)(dirp))) { while ((dp = (*readdirfunc)(dirp))) {
register u_char *sc; char *sc;
register Char *dc; Char *dc;
wchar_t wc;
size_t clen;
mbstate_t mbs;
/* 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; memset(&mbs, 0, sizeof(mbs));
(*dc++ = *sc++) != EOS;) dc = pathend;
continue; sc = dp->d_name;
while (dc < pathend_last) {
clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
if (clen == (size_t)-1 || clen == (size_t)-2) {
wc = *sc;
clen = 1;
memset(&mbs, 0, sizeof(mbs));
}
if ((*dc++ = wc) == EOS)
break;
sc += clen;
}
if (!match(pathend, pattern, restpattern)) { if (!match(pathend, pattern, restpattern)) {
*pathend = EOS; *pathend = EOS;
continue; continue;
} }
err = glob2(pathbuf, --dc, restpattern, pglob, limit); err = glob2(pathbuf, --dc, pathend_last, restpattern,
pglob, limit);
if (err) if (err)
break; break;
} }
@ -657,7 +709,7 @@ glob3(pathbuf, pathend, pattern, restpattern, pglob, limit)
/* /*
* Extend the gl_pathv member of a glob_t structure to accommodate a new item, * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
* add the new item, and update gl_pathc. * add the new item, and update gl_pathc.
* *
* This assumes the BSD realloc, which only copies the block when its size * This assumes the BSD realloc, which only copies the block when its size
@ -671,58 +723,58 @@ glob3(pathbuf, pathend, pattern, restpattern, pglob, limit)
* 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(path, pglob, limit) globextend(const Char *path, glob_t *pglob, size_t *limit)
const Char *path;
glob_t *pglob;
size_t *limit;
{ {
register char **pathv; char **pathv;
register int i; size_t i, newsize, len;
size_t newsize, len;
char *copy; char *copy;
const Char *p; const Char *p;
if (*limit && pglob->gl_pathc > *limit) {
errno = 0;
return (GLOB_NOSPACE);
}
newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs); newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
pathv = pglob->gl_pathv ? pathv = pglob->gl_pathv ?
realloc((char *)pglob->gl_pathv, newsize) : (char **) realloc((char *)pglob->gl_pathv, newsize) :
malloc(newsize); (char **) malloc(newsize);
if (pathv == NULL) if (pathv == NULL) {
if (pglob->gl_pathv) {
free(pglob->gl_pathv);
pglob->gl_pathv = NULL;
}
return(GLOB_NOSPACE); return(GLOB_NOSPACE);
}
if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) { if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
/* first time around -- clear initial gl_offs items */ /* first time around -- clear initial gl_offs items */
pathv += pglob->gl_offs; pathv += pglob->gl_offs;
for (i = pglob->gl_offs; --i >= 0;) for (i = pglob->gl_offs + 1; --i > 0; )
*--pathv = NULL; *--pathv = NULL;
} }
pglob->gl_pathv = pathv; pglob->gl_pathv = pathv;
for (p = path; *p++;) for (p = path; *p++;)
continue; continue;
len = (size_t)(p - path); len = MB_CUR_MAX * (size_t)(p - path); /* XXX overallocation */
*limit += len; if ((copy = (char *) malloc(len)) != NULL) {
if ((copy = malloc(len)) != NULL) { if (g_Ctoc(path, copy, len)) {
g_Ctoc(path, copy); free(copy);
return (GLOB_NOSPACE);
}
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);
} }
/* /*
* pattern matching function for filenames. Each occurrence of the * * pattern matching function for filenames. Each occurrence of the *
* pattern causes a recursion level. * pattern causes a recursion level.
*/ */
static int static int
match(name, pat, patend) match(Char *name, Char *pat, Char *patend)
register Char *name, *pat, *patend;
{ {
int ok, negate_range; int ok, negate_range;
Char c, k; Char c, k;
@ -791,11 +843,10 @@ match(name, pat, patend)
/* Free allocated data belonging to a glob_t structure. */ /* Free allocated data belonging to a glob_t structure. */
void void
globfree(pglob) globfree(glob_t *pglob)
glob_t *pglob;
{ {
register int i; size_t i;
register char **pp; char **pp;
if (pglob->gl_pathv != NULL) { if (pglob->gl_pathv != NULL) {
pp = pglob->gl_pathv + pglob->gl_offs; pp = pglob->gl_pathv + pglob->gl_offs;
@ -803,29 +854,30 @@ globfree(pglob)
if (*pp) if (*pp)
free(*pp); free(*pp);
free(pglob->gl_pathv); free(pglob->gl_pathv);
pglob->gl_pathv = NULL;
} }
} }
static DIR * static DIR *
g_opendir(str, pglob) g_opendir(Char *str, glob_t *pglob)
register Char *str;
glob_t *pglob;
{ {
char buf[MAXPATHLEN]; char buf[MAXPATHLEN];
if (!*str) if (!*str)
strcpy(buf, "."); strcpy(buf, ".");
else else {
g_Ctoc(str, buf); if (g_Ctoc(str, buf, sizeof(buf)))
return (NULL);
}
if (pglob->gl_flags & GLOB_ALTDIRFUNC) if (pglob->gl_flags & GLOB_ALTDIRFUNC)
return((*pglob->gl_opendir)(buf)); return (DIR *) ((*pglob->gl_opendir)((const char *) buf));
return(opendir(buf)); return(opendir(buf));
} }
static void static void
stat32_to_STAT (struct __stat32 *src, struct STAT *dst) stat32_to_stat64 (struct __stat32 *src, struct __stat64 *dst)
{ {
dst->st_dev = src->st_dev; dst->st_dev = src->st_dev;
dst->st_ino = src->st_ino; dst->st_ino = src->st_ino;
@ -843,54 +895,55 @@ stat32_to_STAT (struct __stat32 *src, struct STAT *dst)
} }
static int static int
g_lstat(fn, sb, pglob) g_lstat(Char *fn, struct __stat64 *sb, glob_t *pglob)
register Char *fn;
struct STAT *sb;
glob_t *pglob;
{ {
char buf[MAXPATHLEN]; char buf[MAXPATHLEN];
g_Ctoc(fn, buf); if (g_Ctoc(fn, buf, sizeof(buf))) {
errno = ENAMETOOLONG;
return (-1);
}
if (pglob->gl_flags & GLOB_ALTDIRFUNC) { if (pglob->gl_flags & GLOB_ALTDIRFUNC) {
struct __stat32 lsb; struct __stat32 lsb;
int ret; int ret;
if (CYGWIN_VERSION_CHECK_FOR_USING_BIG_TYPES) if (CYGWIN_VERSION_CHECK_FOR_USING_BIG_TYPES)
ret = (*pglob->gl_lstat)(buf, &sb); ret = (*pglob->gl_lstat)(buf, sb);
else if (!(ret = (*pglob->gl_lstat)(buf, &lsb))) else if (!(ret = (*pglob->gl_lstat)(buf,
stat32_to_STAT (&lsb, sb); (struct __stat64 *)&lsb)))
stat32_to_stat64 (&lsb, sb);
return ret; return ret;
} }
return(lstat64(buf, sb)); return(lstat64(buf, sb));
} }
static int static int
g_stat(fn, sb, pglob) g_stat(Char *fn, struct __stat64 *sb, glob_t *pglob)
register Char *fn;
struct STAT *sb;
glob_t *pglob;
{ {
char buf[MAXPATHLEN]; char buf[MAXPATHLEN];
g_Ctoc(fn, buf); if (g_Ctoc(fn, buf, sizeof(buf))) {
errno = ENAMETOOLONG;
return (-1);
}
if (pglob->gl_flags & GLOB_ALTDIRFUNC) { if (pglob->gl_flags & GLOB_ALTDIRFUNC) {
struct __stat32 lsb; struct __stat32 lsb;
int ret; int ret;
if (CYGWIN_VERSION_CHECK_FOR_USING_BIG_TYPES) if (CYGWIN_VERSION_CHECK_FOR_USING_BIG_TYPES)
ret = (*pglob->gl_stat)(buf, &sb); ret = (*pglob->gl_stat)(buf, sb);
if (!(ret = (*pglob->gl_stat)(buf, &lsb))) else if (!(ret = (*pglob->gl_stat)(buf,
stat32_to_STAT (&lsb, sb); (struct __stat64 *)&lsb)))
stat32_to_stat64 (&lsb, sb);
return ret; return ret;
} }
return(stat64(buf, sb)); return(stat64(buf, sb));
} }
static Char * static Char *
g_strchr(str, ch) g_strchr(Char *str, wchar_t ch)
Char *str;
int ch;
{ {
do { do {
if (*str == ch) if (*str == ch)
return (str); return (str);
@ -898,42 +951,31 @@ g_strchr(str, ch)
return (NULL); return (NULL);
} }
#ifdef notdef static int
static Char * g_Ctoc(const Char *str, char *buf, size_t len)
g_strcat(dst, src)
Char *dst;
const Char* src;
{ {
Char *sdst = dst; mbstate_t mbs;
size_t clen;
while (*dst++) memset(&mbs, 0, sizeof(mbs));
continue; while (len >= (size_t) MB_CUR_MAX) {
--dst; clen = wcrtomb(buf, *str, &mbs);
while((*dst++ = *src++) != EOS) if (clen == (size_t)-1)
continue; return (1);
if (*str == L'\0')
return (sdst); return (0);
str++;
buf += clen;
len -= clen;
} }
#endif return (1);
static void
g_Ctoc(str, buf)
register const Char *str;
char *buf;
{
register char *dc;
for (dc = buf; (*dc++ = *str++) != EOS;)
continue;
} }
#ifdef DEBUG #ifdef DEBUG
static void static void
qprintf(str, s) qprintf(const char *str, Char *s)
const char *str;
register Char *s;
{ {
register Char *p; Char *p;
(void)printf("%s:\n", str); (void)printf("%s:\n", str);
for (p = s; *p; p++) for (p = s; *p; p++)

View File

@ -1,5 +1,3 @@
/* $NetBSD: glob.h,v 1.6.2.2 1997/11/04 23:38:33 thorpej Exp $ */
/* /*
* Copyright (c) 1989, 1993 * Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved. * The Regents of the University of California. All rights reserved.
@ -36,6 +34,7 @@
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* @(#)glob.h 8.1 (Berkeley) 6/2/93 * @(#)glob.h 8.1 (Berkeley) 6/2/93
* $FreeBSD: /repoman/r/ncvs/src/include/glob.h,v 1.10 2006/05/22 05:57:39 ache Exp $
*/ */
#ifndef _GLOB_H_ #ifndef _GLOB_H_
@ -46,9 +45,9 @@
#include <sys/stat.h> #include <sys/stat.h>
typedef struct { typedef struct {
int gl_pathc; /* Count of total paths so far. */ size_t gl_pathc; /* Count of total paths so far. */
int gl_matchc; /* Count of paths matching pattern. */ size_t gl_matchc; /* Count of paths matching pattern. */
int gl_offs; /* Reserved at beginning of gl_pathv. */ size_t gl_offs; /* Reserved at beginning of gl_pathv. */
int gl_flags; /* Copy of flags parameter to glob. */ int gl_flags; /* Copy of flags parameter to glob. */
char **gl_pathv; /* List of paths matching pattern. */ char **gl_pathv; /* List of paths matching pattern. */
/* Copy of errfunc parameter to glob. */ /* Copy of errfunc parameter to glob. */
@ -59,29 +58,33 @@ typedef struct {
* versions of closedir(3), readdir(3), opendir(3), stat(2) * versions of closedir(3), readdir(3), opendir(3), stat(2)
* and lstat(2). * and lstat(2).
*/ */
void (*gl_closedir) __P((void *)); void (*gl_closedir)(void *);
struct dirent *(*gl_readdir) __P((void *)); struct dirent *(*gl_readdir)(void *);
void *(*gl_opendir) __P((const char *)); void *(*gl_opendir)(const char *);
#ifdef __LIBC12_SOURCE__
int (*gl_lstat) __P((const char *, struct stat12 *));
int (*gl_stat) __P((const char *, struct stat12 *));
#else
#if defined (__INSIDE_CYGWIN__) #if defined (__INSIDE_CYGWIN__)
int (*gl_lstat) (); int (*gl_lstat) __P((const char *, struct __stat64 *));
int (*gl_stat) (); int (*gl_stat) __P((const char *, struct __stat64 *));
#else #else
int (*gl_lstat) __P((const char *, struct stat *)); int (*gl_lstat) __P((const char *, struct stat *));
int (*gl_stat) __P((const char *, struct stat *)); int (*gl_stat) __P((const char *, struct stat *));
#endif #endif
#endif
} glob_t; } glob_t;
/* Believed to have been introduced in 1003.2-1992 */
#define GLOB_APPEND 0x0001 /* Append to output from previous call. */ #define GLOB_APPEND 0x0001 /* Append to output from previous call. */
#define GLOB_DOOFFS 0x0002 /* Use gl_offs. */ #define GLOB_DOOFFS 0x0002 /* Use gl_offs. */
#define GLOB_ERR 0x0004 /* Return on error. */ #define GLOB_ERR 0x0004 /* Return on error. */
#define GLOB_MARK 0x0008 /* Append / to matching directories. */ #define GLOB_MARK 0x0008 /* Append / to matching directories. */
#define GLOB_NOCHECK 0x0010 /* Return pattern itself if nothing matches. */ #define GLOB_NOCHECK 0x0010 /* Return pattern itself if nothing matches. */
#define GLOB_NOSORT 0x0020 /* Don't sort. */ #define GLOB_NOSORT 0x0020 /* Don't sort. */
#define GLOB_NOESCAPE 0x2000 /* Disable backslash escaping. */
/* Error values returned by glob(3) */
#define GLOB_NOSPACE (-1) /* Malloc call failed. */
#define GLOB_ABORTED (-2) /* Unignored error. */
#define GLOB_NOMATCH (-3) /* No match and GLOB_NOCHECK was not set. */
#define GLOB_NOSYS (-4) /* Obsolete: source comptability only. */
#ifndef _POSIX_SOURCE #ifndef _POSIX_SOURCE
#define GLOB_ALTDIRFUNC 0x0040 /* Use alternately specified directory funcs. */ #define GLOB_ALTDIRFUNC 0x0040 /* Use alternately specified directory funcs. */
@ -90,11 +93,12 @@ 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_LIMIT 0x1000 /* limit number of returned paths */
#endif
#define GLOB_NOSPACE (-1) /* Malloc call failed. */ /* source compatibility, these are the old names */
#define GLOB_ABEND (-2) /* Unignored error. */ #define GLOB_MAXPATH GLOB_LIMIT
#define GLOB_ABEND GLOB_ABORTED
#endif /* __BSD_VISIBLE */
__BEGIN_DECLS __BEGIN_DECLS
@ -104,10 +108,9 @@ __BEGIN_DECLS
#else #else
# define DLLEXPORT __declspec(dllimport) # define DLLEXPORT __declspec(dllimport)
#endif #endif
int DLLEXPORT glob(const char *, int, int (*)(const char *, int), glob_t *); int DLLEXPORT glob(const char *, int, int (*)(const char *, int), glob_t *);
void DLLEXPORT globfree(glob_t *); void DLLEXPORT globfree(glob_t *);
#undef DLLEXPORT
__END_DECLS __END_DECLS
#endif /* !_GLOB_H_ */ #endif /* !_GLOB_H_ */