1 --- glob.c.orig 2004-11-25 11:38:01.000000000 -0800
2 +++ glob.c 2005-02-24 16:02:34.000000000 -0800
5 __FBSDID("$FreeBSD: src/lib/libc/gen/glob.c,v 1.22 2004/07/29 03:48:52 tjr Exp $");
7 +#include "xlocale_private.h"
10 * glob(3) -- a superset of the one defined in POSIX 1003.2.
15 static int compare(const void *, const void *);
16 -static int g_Ctoc(const Char *, char *, u_int);
17 -static int g_lstat(Char *, struct stat *, glob_t *);
18 -static DIR *g_opendir(Char *, glob_t *);
19 +static int g_Ctoc(const Char *, char *, u_int, locale_t);
20 +static int g_lstat(Char *, struct stat *, glob_t *, locale_t);
21 +static DIR *g_opendir(Char *, glob_t *, locale_t);
22 static Char *g_strchr(Char *, wchar_t);
24 static Char *g_strcat(Char *, const Char *);
26 -static int g_stat(Char *, struct stat *, glob_t *);
27 -static int glob0(const Char *, glob_t *, int *);
28 -static int glob1(Char *, glob_t *, int *);
29 -static int glob2(Char *, Char *, Char *, Char *, glob_t *, int *);
30 -static int glob3(Char *, Char *, Char *, Char *, Char *, glob_t *, int *);
31 -static int globextend(const Char *, glob_t *, int *);
32 +static int g_stat(Char *, struct stat *, glob_t *, locale_t);
33 +static int glob0(const Char *, glob_t *, int *, locale_t);
34 +static int glob1(Char *, glob_t *, int *, locale_t);
35 +static int glob2(Char *, Char *, Char *, Char *, glob_t *, int *, locale_t);
36 +static int glob3(Char *, Char *, Char *, Char *, Char *, glob_t *, int *, locale_t);
37 +static int globextend(const Char *, glob_t *, int *, locale_t);
39 globtilde(const Char *, Char *, size_t, glob_t *);
40 -static int globexp1(const Char *, glob_t *, int *);
41 -static int globexp2(const Char *, const Char *, glob_t *, int *, int *);
42 -static int match(Char *, Char *, Char *);
43 +static int globexp1(const Char *, glob_t *, int *, locale_t);
44 +static int globexp2(const Char *, const Char *, glob_t *, int *, int *, locale_t);
45 +static int match(Char *, Char *, Char *, locale_t);
47 static void qprintf(const char *, Char *);
53 + locale_t loc = __current_locale();
54 + int mb_cur_max = MB_CUR_MAX_L(loc);
56 patnext = (u_char *) pattern;
57 if (!(flags & GLOB_APPEND)) {
59 bufend = bufnext + MAXPATHLEN - 1;
60 if (flags & GLOB_NOESCAPE) {
61 memset(&mbs, 0, sizeof(mbs));
62 - while (bufend - bufnext >= MB_CUR_MAX) {
63 - clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
64 + while (bufend - bufnext >= mb_cur_max) {
65 + clen = mbrtowc_l(&wc, (const char *)patnext, MB_LEN_MAX, &mbs, loc);
66 if (clen == (size_t)-1 || clen == (size_t)-2)
67 return (GLOB_NOMATCH);
71 /* Protect the quoted characters. */
72 memset(&mbs, 0, sizeof(mbs));
73 - while (bufend - bufnext >= MB_CUR_MAX) {
74 + while (bufend - bufnext >= mb_cur_max) {
75 if (*patnext == QUOTE) {
76 if (*++patnext == EOS) {
77 *bufnext++ = QUOTE | M_PROTECT;
82 - clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
83 + clen = mbrtowc_l(&wc, (const char *)patnext, MB_LEN_MAX, &mbs, loc);
84 if (clen == (size_t)-1 || clen == (size_t)-2)
85 return (GLOB_NOMATCH);
90 if (flags & GLOB_BRACE)
91 - return globexp1(patbuf, pglob, &limit);
92 + return globexp1(patbuf, pglob, &limit, loc);
94 - return glob0(patbuf, pglob, &limit);
95 + return glob0(patbuf, pglob, &limit, loc);
103 -globexp1(pattern, pglob, limit)
104 +globexp1(pattern, pglob, limit, loc)
110 const Char* ptr = pattern;
113 /* Protect a single {}, for find(1), like csh */
114 if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
115 - return glob0(pattern, pglob, limit);
116 + return glob0(pattern, pglob, limit, loc);
118 while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL)
119 - if (!globexp2(ptr, pattern, pglob, &rv, limit))
120 + if (!globexp2(ptr, pattern, pglob, &rv, limit, loc))
123 - return glob0(pattern, pglob, limit);
124 + return glob0(pattern, pglob, limit, loc);
128 @@ -270,10 +275,11 @@
129 * If it fails then it tries to glob the rest of the pattern and returns.
132 -globexp2(ptr, pattern, pglob, rv, limit)
133 +globexp2(ptr, pattern, pglob, rv, limit, loc)
134 const Char *ptr, *pattern;
143 /* Non matching braces; just glob the pattern */
144 if (i != 0 || *pe == EOS) {
145 - *rv = glob0(patbuf, pglob, limit);
146 + *rv = glob0(patbuf, pglob, limit, loc);
152 qprintf("globexp2:", patbuf);
154 - *rv = globexp1(patbuf, pglob, limit);
155 + *rv = globexp1(patbuf, pglob, limit, loc);
157 /* move after the comma, to the next string */
159 @@ -447,10 +453,11 @@
160 * if things went well, nonzero if errors occurred.
163 -glob0(pattern, pglob, limit)
164 +glob0(pattern, pglob, limit, loc)
170 const Char *qpatnext;
171 int c, err, oldpathc;
173 qprintf("glob0:", patbuf);
176 - if ((err = glob1(patbuf, pglob, limit)) != 0)
177 + if ((err = glob1(patbuf, pglob, limit, loc)) != 0)
182 if (((pglob->gl_flags & GLOB_NOCHECK) ||
183 ((pglob->gl_flags & GLOB_NOMAGIC) &&
184 !(pglob->gl_flags & GLOB_MAGCHAR))))
185 - return(globextend(pattern, pglob, limit));
186 + return(globextend(pattern, pglob, limit, loc));
188 return(GLOB_NOMATCH);
190 @@ -543,10 +550,11 @@
194 -glob1(pattern, pglob, limit)
195 +glob1(pattern, pglob, limit, loc)
201 Char pathbuf[MAXPATHLEN];
206 return(glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1,
207 - pattern, pglob, limit));
208 + pattern, pglob, limit, loc));
212 @@ -563,10 +571,11 @@
216 -glob2(pathbuf, pathend, pathend_last, pattern, pglob, limit)
217 +glob2(pathbuf, pathend, pathend_last, pattern, pglob, limit, loc)
218 Char *pathbuf, *pathend, *pathend_last, *pattern;
225 @@ -579,13 +588,13 @@
226 for (anymeta = 0;;) {
227 if (*pattern == EOS) { /* End of pattern? */
229 - if (g_lstat(pathbuf, &sb, pglob))
230 + if (g_lstat(pathbuf, &sb, pglob, loc))
233 if (((pglob->gl_flags & GLOB_MARK) &&
234 pathend[-1] != SEP) && (S_ISDIR(sb.st_mode)
235 || (S_ISLNK(sb.st_mode) &&
236 - (g_stat(pathbuf, &sb, pglob) == 0) &&
237 + (g_stat(pathbuf, &sb, pglob, loc) == 0) &&
238 S_ISDIR(sb.st_mode)))) {
239 if (pathend + 1 > pathend_last)
240 return (GLOB_ABORTED);
245 - return(globextend(pathbuf, pglob, limit));
246 + return(globextend(pathbuf, pglob, limit, loc));
249 /* Find end of next segment, copy tentatively to pathend. */
250 @@ -617,16 +626,17 @@
252 } else /* Need expansion, recurse. */
253 return(glob3(pathbuf, pathend, pathend_last, pattern, p,
255 + pglob, limit, loc));
261 -glob3(pathbuf, pathend, pathend_last, pattern, restpattern, pglob, limit)
262 +glob3(pathbuf, pathend, pathend_last, pattern, restpattern, pglob, limit, loc)
263 Char *pathbuf, *pathend, *pathend_last, *pattern, *restpattern;
270 @@ -646,10 +656,10 @@
274 - if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
275 + if ((dirp = g_opendir(pathbuf, pglob, loc)) == NULL) {
276 /* TODO: don't call for ENOENT or ENOTDIR? */
277 if (pglob->gl_errfunc) {
278 - if (g_Ctoc(pathbuf, buf, sizeof(buf)))
279 + if (g_Ctoc(pathbuf, buf, sizeof(buf), loc))
280 return (GLOB_ABORTED);
281 if (pglob->gl_errfunc(buf, errno) ||
282 pglob->gl_flags & GLOB_ERR)
285 sc = (u_char *) dp->d_name;
286 while (dc < pathend_last) {
287 - clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
288 + clen = mbrtowc_l(&wc, (const char *)sc, MB_LEN_MAX, &mbs, loc);
289 if (clen == (size_t)-1 || clen == (size_t)-2) {
292 @@ -689,12 +699,12 @@
296 - if (!match(pathend, pattern, restpattern)) {
297 + if (!match(pathend, pattern, restpattern, loc)) {
301 err = glob2(pathbuf, --dc, pathend_last, restpattern,
303 + pglob, limit, loc);
307 @@ -722,10 +732,11 @@
308 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
311 -globextend(path, pglob, limit)
312 +globextend(path, pglob, limit, loc)
322 for (p = path; *p++;)
324 - len = MB_CUR_MAX * (size_t)(p - path); /* XXX overallocation */
325 + len = MB_CUR_MAX_L(loc) * (size_t)(p - path); /* XXX overallocation */
326 if ((copy = malloc(len)) != NULL) {
327 - if (g_Ctoc(path, copy, len)) {
328 + if (g_Ctoc(path, copy, len, loc)) {
330 return (GLOB_NOSPACE);
333 * pattern causes a recursion level.
336 -match(name, pat, patend)
337 +match(name, pat, patend, loc)
338 Char *name, *pat, *patend;
341 int ok, negate_range;
347 - if (match(name, pat, patend))
348 + if (match(name, pat, patend, loc))
350 while (*name++ != EOS);
352 @@ -806,10 +818,10 @@
354 while (((c = *pat++) & M_MASK) != M_END)
355 if ((*pat & M_MASK) == M_RNG) {
356 - if (__collate_load_error ?
357 + if (loc->__collate_load_error ?
358 CHAR(c) <= CHAR(k) && CHAR(k) <= CHAR(pat[1]) :
359 - __collate_range_cmp(CHAR(c), CHAR(k)) <= 0
360 - && __collate_range_cmp(CHAR(k), CHAR(pat[1])) <= 0
361 + __collate_range_cmp(CHAR(c), CHAR(k), loc) <= 0
362 + && __collate_range_cmp(CHAR(k), CHAR(pat[1]), loc) <= 0
366 @@ -846,16 +858,17 @@
370 -g_opendir(str, pglob)
371 +g_opendir(str, pglob, loc)
376 char buf[MAXPATHLEN];
381 - if (g_Ctoc(str, buf, sizeof(buf)))
382 + if (g_Ctoc(str, buf, sizeof(buf), loc))
386 @@ -866,14 +879,15 @@
390 -g_lstat(fn, sb, pglob)
391 +g_lstat(fn, sb, pglob, loc)
397 char buf[MAXPATHLEN];
399 - if (g_Ctoc(fn, buf, sizeof(buf))) {
400 + if (g_Ctoc(fn, buf, sizeof(buf), loc)) {
401 errno = ENAMETOOLONG;
404 @@ -883,14 +897,15 @@
408 -g_stat(fn, sb, pglob)
409 +g_stat(fn, sb, pglob, loc)
415 char buf[MAXPATHLEN];
417 - if (g_Ctoc(fn, buf, sizeof(buf))) {
418 + if (g_Ctoc(fn, buf, sizeof(buf), loc)) {
419 errno = ENAMETOOLONG;
422 @@ -912,17 +927,19 @@
426 -g_Ctoc(str, buf, len)
427 +g_Ctoc(str, buf, len, loc)
435 + int mb_cur_max = MB_CUR_MAX_L(loc);
437 memset(&mbs, 0, sizeof(mbs));
438 - while (len >= MB_CUR_MAX) {
439 - clen = wcrtomb(buf, *str, &mbs);
440 + while (len >= mb_cur_max) {
441 + clen = wcrtomb_l(buf, *str, &mbs, loc);
442 if (clen == (size_t)-1)