* Updated glob.h and its implementation to FreeBSD current (1.10, 1.27),

adapted them to work on Haiku.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@28355 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2008-10-28 14:13:30 +00:00
parent f20ccdc461
commit 6e036f0479
2 changed files with 211 additions and 178 deletions
+16 -18
View File
@@ -32,22 +32,23 @@
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
*
* @(#)glob.h 8.1 (Berkeley) 6/2/93
*/ */
#ifndef _GLOB_H_ #ifndef _GLOB_H_
#define _GLOB_H_ #define _GLOB_H_
#include <sys/cdefs.h>
#include <sys/types.h>
struct stat; struct stat;
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. */
int (*gl_errfunc)(const char *, int);
/* Copy of errfunc parameter to glob. */ /* Copy of errfunc parameter to glob. */
int (*gl_errfunc)(const char *, int);
/* /*
* Alternate filesystem access methods for glob; replacement * Alternate filesystem access methods for glob; replacement
@@ -61,7 +62,6 @@ typedef struct {
int (*gl_stat)(const char *, struct stat *); int (*gl_stat)(const char *, struct stat *);
} glob_t; } glob_t;
#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. */
@@ -74,6 +74,7 @@ typedef struct {
#define GLOB_NOSPACE (-1) /* Malloc call failed. */ #define GLOB_NOSPACE (-1) /* Malloc call failed. */
#define GLOB_ABORTED (-2) /* Unignored error. */ #define GLOB_ABORTED (-2) /* Unignored error. */
#define GLOB_NOMATCH (-3) /* No match and GLOB_NOCHECK was not set. */ #define GLOB_NOMATCH (-3) /* No match and GLOB_NOCHECK was not set. */
#define GLOB_NOSYS (-4) /* Obsolete: source comptability only. */
#define GLOB_ALTDIRFUNC 0x0040 /* Use alternately specified directory funcs. */ #define GLOB_ALTDIRFUNC 0x0040 /* Use alternately specified directory funcs. */
#define GLOB_BRACE 0x0080 /* Expand braces ala csh. */ #define GLOB_BRACE 0x0080 /* Expand braces ala csh. */
@@ -83,16 +84,13 @@ typedef struct {
#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 number of returned paths */ #define GLOB_LIMIT 0x1000 /* limit number of returned paths */
/* source compatibility, these are the old names */
#define GLOB_MAXPATH GLOB_LIMIT
#define GLOB_ABEND GLOB_ABORTED
#ifdef __cplusplus __BEGIN_DECLS
extern "C" { int glob(const char *, int, int (*)(const char *, int), glob_t *);
#endif void globfree(glob_t *);
__END_DECLS
extern int glob(const char *pattern, int flags, int (*errfunc)(const char *, int), glob_t *); #endif /* !_GLOB_H_ */
extern void globfree(glob_t *);
#ifdef __cplusplus
}
#endif
#endif /* _GLOB_H_ */
+195 -160
View File
@@ -13,10 +13,6 @@
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors * 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software * may be used to endorse or promote products derived from this software
* without specific prior written permission. * without specific prior written permission.
@@ -34,6 +30,12 @@
* 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>
//__FBSDID("$FreeBSD: src/lib/libc/gen/glob.c,v 1.27 2008/06/26 07:12:35 mtm Exp $");
/* /*
* glob(3) -- a superset of the one defined in POSIX 1003.2. * glob(3) -- a superset of the one defined in POSIX 1003.2.
* *
@@ -59,6 +61,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 <sys/param.h> #include <sys/param.h>
#include <sys/stat.h> #include <sys/stat.h>
@@ -66,24 +78,29 @@
#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 <wchar.h>
//#include "collate.h" #ifndef __HAIKU__
# include "collate.h"
#endif
#define DOLLAR '$' #define DOLLAR '$'
#define DOT '.' #define DOT '.'
#define EOS '\0' #define EOS '\0'
#define LBRACKET '[' #define LBRACKET '['
#define NOT '!' #define NOT '!'
#define QUESTION '?' #define QUESTION '?'
#define QUOTE '\\' #define QUOTE '\\'
#define RANGE '-' #define RANGE '-'
#define RBRACKET ']' #define RBRACKET ']'
#define SEP '/' #define SEP '/'
#define STAR '*' #define STAR '*'
#define TILDE '~' #define TILDE '~'
#define UNDERSCORE '_' #define UNDERSCORE '_'
@@ -94,26 +111,26 @@
#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(']')
@@ -125,37 +142,39 @@ typedef char Char;
static int compare(const void *, const void *); static int compare(const void *, const void *);
static int g_Ctoc(const Char *, char *, u_int); static int g_Ctoc(const Char *, char *, size_t);
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 const Char *g_strchr(const Char *, wchar_t);
#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 *, int *); static int glob0(const Char *, glob_t *, size_t *);
static int glob1(Char *, glob_t *, int *); static int glob1(Char *, glob_t *, size_t *);
static int glob2(Char *, Char *, Char *, Char *, glob_t *, int *); static int glob2(Char *, Char *, Char *, Char *, glob_t *, size_t *);
static int glob3(Char *, Char *, Char *, Char *, Char *, glob_t *, int *); static int glob3(Char *, Char *, Char *, Char *, Char *, glob_t *, size_t *);
static int globextend(const Char *, glob_t *, int *); static int globextend(const Char *, glob_t *, size_t *);
static const Char * static const Char *
globtilde(const Char *, Char *, size_t, glob_t *); globtilde(const Char *, Char *, size_t, glob_t *);
static int globexp1(const Char *, glob_t *, int *); static int globexp1(const Char *, glob_t *, size_t *);
static int globexp2(const Char *, const Char *, glob_t *, int *, int *); static int globexp2(const Char *, const Char *, glob_t *, int *, size_t *);
static int match(Char *, Char *, Char *); static int match(Char *, Char *, Char *);
#ifdef DEBUG #ifdef DEBUG
static void qprintf(const char *, Char *); static void qprintf(const char *, Char *);
#endif #endif
int int
glob(const char *pattern, int flags, int (*errfunc)(const char *, int), glob_t *pglob) glob(const char *pattern, int flags, int (*errfunc)(const char *, int), glob_t *pglob)
{ {
const u_char *patnext; const char *patnext;
int c, limit; size_t limit;
Char *bufnext, *bufend, patbuf[MAXPATHLEN]; 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;
@@ -168,7 +187,6 @@ glob(const char *pattern, int flags, int (*errfunc)(const char *, int), glob_t *
limit = ARG_MAX; limit = ARG_MAX;
} else } else
limit = 0; 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;
@@ -176,38 +194,52 @@ glob(const char *pattern, int flags, int (*errfunc)(const char *, int), glob_t *
bufnext = patbuf; bufnext = patbuf;
bufend = bufnext + MAXPATHLEN - 1; bufend = bufnext + MAXPATHLEN - 1;
if (flags & GLOB_NOESCAPE) { if (flags & GLOB_NOESCAPE) {
while (bufnext < bufend && (c = *patnext++) != EOS) memset(&mbs, 0, sizeof(mbs));
*bufnext++ = c; 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 { } 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
else prot = 0;
*bufnext++ = c; 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;
}
} }
*bufnext = EOS; *bufnext = EOS;
if (flags & GLOB_BRACE) if (flags & GLOB_BRACE)
return globexp1(patbuf, pglob, &limit); return globexp1(patbuf, pglob, &limit);
else
return glob0(patbuf, pglob, &limit); return glob0(patbuf, pglob, &limit);
} }
/* /*
* Expand recursively a glob {} pattern. When there is no more expansion * Expand recursively a glob {} pattern. When there is no more expansion
* 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 static int
globexp1(const Char *pattern, glob_t *pglob, int *limit) globexp1(const Char *pattern, glob_t *pglob, size_t *limit)
{ {
const Char* ptr = pattern; const Char* ptr = pattern;
int rv; int rv;
@@ -216,7 +248,7 @@ globexp1(const Char *pattern, glob_t *pglob, int *limit)
if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS) if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
return glob0(pattern, pglob, limit); return glob0(pattern, pglob, limit);
while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL) while ((ptr = g_strchr(ptr, LBRACE)) != NULL)
if (!globexp2(ptr, pattern, pglob, &rv, limit)) if (!globexp2(ptr, pattern, pglob, &rv, limit))
return rv; return rv;
@@ -229,13 +261,12 @@ globexp1(const Char *pattern, glob_t *pglob, int *limit)
* 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 static int
globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv, int *limit) globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv, size_t *limit)
{ {
int i; int i;
Char *lm, *ls; Char *lm, *ls;
const Char *pe, *pm, *pl; const Char *pe, *pm, *pm1, *pl;
Char patbuf[MAXPATHLEN]; Char patbuf[MAXPATHLEN];
/* copy part up to the brace */ /* copy part up to the brace */
@@ -245,7 +276,7 @@ globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv, int *limi
ls = lm; ls = lm;
/* Find the balanced brace */ /* Find the balanced brace */
for (i = 0, pe = ++ptr; *pe; pe++) { for (i = 0, pe = ++ptr; *pe; pe++)
if (*pe == LBRACKET) { if (*pe == LBRACKET) {
/* Ignore everything between [] */ /* Ignore everything between [] */
for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++) for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
@@ -265,7 +296,6 @@ globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv, int *limi
break; break;
i--; i--;
} }
}
/* Non matching braces; just glob the pattern */ /* Non matching braces; just glob the pattern */
if (i != 0 || *pe == EOS) { if (i != 0 || *pe == EOS) {
@@ -273,18 +303,18 @@ globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv, int *limi
return 0; return 0;
} }
for (i = 0, pl = pm = ptr; pm <= pe; pm++) { for (i = 0, pl = pm = ptr; pm <= pe; pm++)
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;
@@ -326,17 +356,15 @@ globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv, int *limi
default: default:
break; break;
} }
}
*rv = 0; *rv = 0;
return 0; return 0;
} }
/* /*
* expand tilde from the passwd file. * expand tilde from the passwd file.
*/ */
static const Char * static const Char *
globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob) globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
{ {
@@ -365,9 +393,9 @@ globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
* the password file * the password file
*/ */
if ( if (
//#ifndef __NETBSD_SYSCALLS #ifndef __HAIKU__
// issetugid() != 0 || issetugid() != 0 ||
//#endif #endif
(h = getenv("HOME")) == NULL) { (h = getenv("HOME")) == NULL) {
if (((h = getlogin()) != NULL && if (((h = getlogin()) != NULL &&
(pwd = getpwnam(h)) != NULL) || (pwd = getpwnam(h)) != NULL) ||
@@ -406,12 +434,12 @@ globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
* 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. * if things went well, nonzero if errors occurred.
*/ */
static int static int
glob0(const Char *pattern, glob_t *pglob, int *limit) glob0(const Char *pattern, glob_t *pglob, size_t *limit)
{ {
const Char *qpatnext; const Char *qpatnext;
int c, err, oldpathc; int c, err;
size_t oldpathc;
Char *bufnext, patbuf[MAXPATHLEN]; Char *bufnext, patbuf[MAXPATHLEN];
qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob); qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
@@ -426,7 +454,7 @@ glob0(const Char *pattern, glob_t *pglob, int *limit)
if (c == NOT) if (c == NOT)
++qpatnext; ++qpatnext;
if (*qpatnext == EOS || if (*qpatnext == EOS ||
g_strchr((Char *) qpatnext+1, RBRACKET) == NULL) { g_strchr(qpatnext+1, RBRACKET) == NULL) {
*bufnext++ = LBRACKET; *bufnext++ = LBRACKET;
if (c == NOT) if (c == NOT)
--qpatnext; --qpatnext;
@@ -483,46 +511,42 @@ glob0(const Char *pattern, glob_t *pglob, int *limit)
if (((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 else
return GLOB_NOMATCH; return(GLOB_NOMATCH);
} }
if (!(pglob->gl_flags & GLOB_NOSORT)) 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(const void *p, const void *q) compare(const void *p, const void *q)
{ {
return strcmp(*(char **)p, *(char **)q); return(strcmp(*(char **)p, *(char **)q));
} }
static int static int
glob1(Char *pattern, glob_t *pglob, int *limit) glob1(Char *pattern, glob_t *pglob, size_t *limit)
{ {
Char pathbuf[MAXPATHLEN]; 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, pathbuf + MAXPATHLEN - 1,
return glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1, pattern, pglob, limit));
pattern, pglob, limit);
} }
/* /*
* The functions glob2 and glob3 are mutually recursive; there is one level * The functions glob2 and glob3 are mutually recursive; there is one level
* of recursion for each segment in the pattern that contains one or more * of recursion for each segment in the pattern that contains one or more
* meta characters. * meta characters.
*/ */
static int static int
glob2(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern, glob_t *pglob, int *limit) glob2(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern,
glob_t *pglob, size_t *limit)
{ {
struct stat sb; struct stat sb;
Char *p, *q; Char *p, *q;
@@ -544,12 +568,12 @@ glob2(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern, glob_t *p
(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) if (pathend + 1 > pathend_last)
return GLOB_ABORTED; return (GLOB_ABORTED);
*pathend++ = SEP; *pathend++ = SEP;
*pathend = EOS; *pathend = EOS;
} }
++pglob->gl_matchc; ++pglob->gl_matchc;
return globextend(pathbuf, pglob, limit); return(globextend(pathbuf, pglob, limit));
} }
/* Find end of next segment, copy tentatively to pathend. */ /* Find end of next segment, copy tentatively to pathend. */
@@ -559,7 +583,7 @@ glob2(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern, glob_t *p
if (ismeta(*p)) if (ismeta(*p))
anymeta = 1; anymeta = 1;
if (q + 1 > pathend_last) if (q + 1 > pathend_last)
return GLOB_ABORTED; return (GLOB_ABORTED);
*q++ = *p++; *q++ = *p++;
} }
@@ -568,19 +592,20 @@ glob2(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern, glob_t *p
pattern = p; pattern = p;
while (*pattern == SEP) { while (*pattern == SEP) {
if (pathend + 1 > pathend_last) if (pathend + 1 > pathend_last)
return GLOB_ABORTED; return (GLOB_ABORTED);
*pathend++ = *pattern++; *pathend++ = *pattern++;
} }
} else /* Need expansion, recurse. */ } else /* Need expansion, recurse. */
return glob3(pathbuf, pathend, pathend_last, pattern, p, pglob, limit); return(glob3(pathbuf, pathend, pathend_last, pattern, p,
pglob, limit));
} }
/* NOTREACHED */ /* NOTREACHED */
} }
static int static int
glob3(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern, Char *restpattern, glob3(Char *pathbuf, Char *pathend, Char *pathend_last,
glob_t *pglob, int *limit) Char *pattern, Char *restpattern,
glob_t *pglob, size_t *limit)
{ {
struct dirent *dp; struct dirent *dp;
DIR *dirp; DIR *dirp;
@@ -596,7 +621,7 @@ glob3(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern, Char *res
struct dirent *(*readdirfunc)(); struct dirent *(*readdirfunc)();
if (pathend > pathend_last) if (pathend > pathend_last)
return GLOB_ABORTED; return (GLOB_ABORTED);
*pathend = EOS; *pathend = EOS;
errno = 0; errno = 0;
@@ -604,12 +629,12 @@ glob3(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern, Char *res
/* TODO: don't call for ENOENT or ENOTDIR? */ /* TODO: don't call for ENOENT or ENOTDIR? */
if (pglob->gl_errfunc) { if (pglob->gl_errfunc) {
if (g_Ctoc(pathbuf, buf, sizeof(buf))) if (g_Ctoc(pathbuf, buf, sizeof(buf)))
return GLOB_ABORTED; 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_ABORTED; return (GLOB_ABORTED);
} }
return 0; return(0);
} }
err = 0; err = 0;
@@ -619,18 +644,30 @@ glob3(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern, Char *res
readdirfunc = pglob->gl_readdir; readdirfunc = pglob->gl_readdir;
else else
readdirfunc = readdir; readdirfunc = readdir;
while ((dp = (*readdirfunc)(dirp))) { while ((dp = (*readdirfunc)(dirp))) {
u_char *sc; char *sc;
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;
memset(&mbs, 0, sizeof(mbs));
dc = pathend; dc = pathend;
sc = (u_char *) dp->d_name; sc = dp->d_name;
while (dc < pathend_last && (*dc++ = *sc++) != EOS) 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;
@@ -645,8 +682,7 @@ glob3(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern, Char *res
(*pglob->gl_closedir)(dirp); (*pglob->gl_closedir)(dirp);
else else
closedir(dirp); closedir(dirp);
return(err);
return err;
} }
@@ -664,19 +700,17 @@ glob3(Char *pathbuf, Char *pathend, Char *pathend_last, Char *pattern, Char *res
* Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
* 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, int *limit) globextend(const Char *path, glob_t *pglob, size_t *limit)
{ {
char **pathv; char **pathv;
int i; size_t i, newsize, len;
u_int newsize, len;
char *copy; char *copy;
const Char *p; const Char *p;
if (*limit && pglob->gl_pathc > *limit) { if (*limit && pglob->gl_pathc > *limit) {
errno = 0; errno = 0;
return GLOB_NOSPACE; return (GLOB_NOSPACE);
} }
newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs); newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
@@ -688,37 +722,35 @@ globextend(const Char *path, glob_t *pglob, int *limit)
free(pglob->gl_pathv); free(pglob->gl_pathv);
pglob->gl_pathv = NULL; 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 */
if ((copy = malloc(len)) != NULL) { if ((copy = malloc(len)) != NULL) {
if (g_Ctoc(path, copy, len)) { if (g_Ctoc(path, copy, len)) {
free(copy); free(copy);
return GLOB_NOSPACE; 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;
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(Char *name, Char *pat, Char *patend) match(Char *name, Char *pat, Char *patend)
{ {
@@ -730,54 +762,56 @@ match(Char *name, Char *pat, Char *patend)
switch (c & M_MASK) { switch (c & M_MASK) {
case M_ALL: case M_ALL:
if (pat == patend) if (pat == patend)
return 1; return(1);
do do
if (match(name, pat, patend)) if (match(name, pat, patend))
return 1; return(1);
while (*name++ != EOS); while (*name++ != EOS);
return 0; return(0);
case M_ONE: case M_ONE:
if (*name++ == EOS) if (*name++ == EOS)
return 0; return(0);
break; break;
case M_SET: case M_SET:
ok = 0; ok = 0;
if ((k = *name++) == EOS) if ((k = *name++) == EOS)
return 0; return(0);
if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS) if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
++pat; ++pat;
while (((c = *pat++) & M_MASK) != M_END) while (((c = *pat++) & M_MASK) != M_END)
if ((*pat & M_MASK) == M_RNG) { if ((*pat & M_MASK) == M_RNG) {
// ToDo: collate stuff disabled
if ( if (
// __collate_load_error ? #ifndef __HAIKU__
__collate_load_error ?
#endif
CHAR(c) <= CHAR(k) && CHAR(k) <= CHAR(pat[1]) CHAR(c) <= CHAR(k) && CHAR(k) <= CHAR(pat[1])
// : __collate_range_cmp(CHAR(c), CHAR(k)) <= 0 #ifndef __HAIKU__
// && __collate_range_cmp(CHAR(k), CHAR(pat[1])) <= 0 :
__collate_range_cmp(CHAR(c), CHAR(k)) <= 0
&& __collate_range_cmp(CHAR(k), CHAR(pat[1])) <= 0
#endif
) )
ok = 1; ok = 1;
pat += 2; pat += 2;
} else if (c == k) } else if (c == k)
ok = 1; ok = 1;
if (ok == negate_range) if (ok == negate_range)
return 0; return(0);
break; break;
default: default:
if (*name++ != c) if (*name++ != c)
return 0; return(0);
break; break;
} }
} }
return *name == EOS; return(*name == EOS);
} }
/* Free allocated data belonging to a glob_t structure. */ /* Free allocated data belonging to a glob_t structure. */
void void
globfree(glob_t *pglob) globfree(glob_t *pglob)
{ {
int i; size_t i;
char **pp; char **pp;
if (pglob->gl_pathv != NULL) { if (pglob->gl_pathv != NULL) {
@@ -790,7 +824,6 @@ globfree(glob_t *pglob)
} }
} }
static DIR * static DIR *
g_opendir(Char *str, glob_t *pglob) g_opendir(Char *str, glob_t *pglob)
{ {
@@ -800,16 +833,15 @@ g_opendir(Char *str, glob_t *pglob)
strcpy(buf, "."); strcpy(buf, ".");
else { else {
if (g_Ctoc(str, buf, sizeof(buf))) if (g_Ctoc(str, buf, sizeof(buf)))
return NULL; return (NULL);
} }
if (pglob->gl_flags & GLOB_ALTDIRFUNC) if (pglob->gl_flags & GLOB_ALTDIRFUNC)
return (*pglob->gl_opendir)(buf); return((*pglob->gl_opendir)(buf));
return opendir(buf); return(opendir(buf));
} }
static int static int
g_lstat(Char *fn, struct stat *sb, glob_t *pglob) g_lstat(Char *fn, struct stat *sb, glob_t *pglob)
{ {
@@ -817,15 +849,13 @@ g_lstat(Char *fn, struct stat *sb, glob_t *pglob)
if (g_Ctoc(fn, buf, sizeof(buf))) { if (g_Ctoc(fn, buf, sizeof(buf))) {
errno = ENAMETOOLONG; errno = ENAMETOOLONG;
return -1; return (-1);
} }
if (pglob->gl_flags & GLOB_ALTDIRFUNC) if (pglob->gl_flags & GLOB_ALTDIRFUNC)
return (*pglob->gl_lstat)(buf, sb); return((*pglob->gl_lstat)(buf, sb));
return(lstat(buf, sb));
return lstat(buf, sb);
} }
static int static int
g_stat(Char *fn, struct stat *sb, glob_t *pglob) g_stat(Char *fn, struct stat *sb, glob_t *pglob)
{ {
@@ -833,54 +863,59 @@ g_stat(Char *fn, struct stat *sb, glob_t *pglob)
if (g_Ctoc(fn, buf, sizeof(buf))) { if (g_Ctoc(fn, buf, sizeof(buf))) {
errno = ENAMETOOLONG; errno = ENAMETOOLONG;
return -1; return (-1);
} }
if (pglob->gl_flags & GLOB_ALTDIRFUNC) if (pglob->gl_flags & GLOB_ALTDIRFUNC)
return (*pglob->gl_stat)(buf, sb); return((*pglob->gl_stat)(buf, sb));
return(stat(buf, sb));
return stat(buf, sb);
} }
static const Char *
static Char * g_strchr(const Char *str, wchar_t ch)
g_strchr(Char *str, int ch)
{ {
do { do {
if (*str == ch) if (*str == ch)
return str; return (str);
} while (*str++); } while (*str++);
return (NULL);
return NULL;
} }
static int static int
g_Ctoc(const Char *str, char *buf, u_int len) g_Ctoc(const Char *str, char *buf, size_t len)
{ {
mbstate_t mbs;
size_t clen;
while (len--) { memset(&mbs, 0, sizeof(mbs));
if ((*buf++ = *str++) == '\0') while (len >= MB_CUR_MAX) {
return 0; clen = wcrtomb(buf, *str, &mbs);
if (clen == (size_t)-1)
return (1);
if (*str == L'\0')
return (0);
str++;
buf += clen;
len -= clen;
} }
return 1; return (1);
} }
#ifdef DEBUG #ifdef DEBUG
static void static void
qprintf(const char *str, Char *s) qprintf(const char *str, Char *s)
{ {
Char *p; Char *p;
printf("%s:\n", str); (void)printf("%s:\n", str);
for (p = s; *p; p++) for (p = s; *p; p++)
printf("%c", CHAR(*p)); (void)printf("%c", CHAR(*p));
printf("\n"); (void)printf("\n");
for (p = s; *p; p++) for (p = s; *p; p++)
printf("%c", *p & M_PROTECT ? '"' : ' '); (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
printf("\n"); (void)printf("\n");
for (p = s; *p; p++) for (p = s; *p; p++)
printf("%c", ismeta(*p) ? '_' : ' '); (void)printf("%c", ismeta(*p) ? '_' : ' ');
printf("\n"); (void)printf("\n");
} }
#endif #endif