1 --- glob.c.orig 2008-03-15 10:50:43.000000000 -0700
2 +++ glob.c 2008-03-27 03:28:31.000000000 -0700
3 @@ -40,6 +40,8 @@ static char sccsid[] = "@(#)glob.c 8.3 (
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.
12 @@ -143,25 +145,33 @@ typedef char Char;
13 #define ismeta(c) (((c)&M_QUOTE) != 0)
16 -static int compare(const void *, const void *);
17 -static int g_Ctoc(const Char *, char *, u_int);
18 -static int g_lstat(Char *, struct stat *, glob_t *);
19 -static DIR *g_opendir(Char *, glob_t *);
20 -static Char *g_strchr(Char *, wchar_t);
21 +#define compare __gl_compare
22 +#define g_Ctoc __gl_g_Ctoc
23 +#define g_strchr __gl_g_strchr
24 +#define globextend __gl_globextend
25 +#define globtilde __gl_globtilde
26 +#define match __gl_match
27 +__private_extern__ int compare(const void *, const void *);
28 +__private_extern__ int g_Ctoc(const Char *, char *, u_int, locale_t);
29 +__private_extern__ Char *g_strchr(Char *, wchar_t);
30 +__private_extern__ int globextend(const Char *, glob_t *, int *, locale_t);
31 +__private_extern__ const Char *
32 + globtilde(const Char *, Char *, size_t, glob_t *);
33 +__private_extern__ int match(Char *, Char *, Char *, locale_t);
36 +static int g_lstat(Char *, struct stat *, glob_t *, locale_t);
37 +static DIR *g_opendir(Char *, glob_t *, locale_t);
39 static Char *g_strcat(Char *, const Char *);
41 -static int g_stat(Char *, struct stat *, glob_t *);
42 -static int glob0(const Char *, glob_t *, int *);
43 -static int glob1(Char *, glob_t *, int *);
44 -static int glob2(Char *, Char *, Char *, Char *, glob_t *, int *);
45 -static int glob3(Char *, Char *, Char *, Char *, Char *, glob_t *, int *);
46 -static int globextend(const Char *, glob_t *, int *);
48 - globtilde(const Char *, Char *, size_t, glob_t *);
49 -static int globexp1(const Char *, glob_t *, int *);
50 -static int globexp2(const Char *, const Char *, glob_t *, int *, int *);
51 -static int match(Char *, Char *, Char *);
52 +static int g_stat(Char *, struct stat *, glob_t *, locale_t);
53 +static int glob0(const Char *, glob_t *, int *, locale_t);
54 +static int glob1(Char *, glob_t *, int *, locale_t);
55 +static int glob2(Char *, Char *, Char *, Char *, glob_t *, int *, locale_t);
56 +static int glob3(Char *, Char *, Char *, Char *, Char *, glob_t *, int *, locale_t);
57 +static int globexp1(const Char *, glob_t *, int *, locale_t);
58 +static int globexp2(const Char *, const Char *, glob_t *, int *, int *, locale_t);
60 static void qprintf(const char *, Char *);
62 @@ -178,6 +188,8 @@ glob(pattern, flags, errfunc, pglob)
66 + locale_t loc = __current_locale();
67 + int mb_cur_max = MB_CUR_MAX_L(loc);
69 patnext = (u_char *) pattern;
70 if (!(flags & GLOB_APPEND)) {
71 @@ -200,8 +212,8 @@ glob(pattern, flags, errfunc, pglob)
72 bufend = bufnext + MAXPATHLEN - 1;
73 if (flags & GLOB_NOESCAPE) {
74 memset(&mbs, 0, sizeof(mbs));
75 - while (bufend - bufnext >= MB_CUR_MAX) {
76 - clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
77 + while (bufend - bufnext >= mb_cur_max) {
78 + clen = mbrtowc_l(&wc, (const char *)patnext, MB_LEN_MAX, &mbs, loc);
79 if (clen == (size_t)-1 || clen == (size_t)-2)
80 return (GLOB_NOMATCH);
82 @@ -212,7 +224,7 @@ glob(pattern, flags, errfunc, pglob)
84 /* Protect the quoted characters. */
85 memset(&mbs, 0, sizeof(mbs));
86 - while (bufend - bufnext >= MB_CUR_MAX) {
87 + while (bufend - bufnext >= mb_cur_max) {
88 if (*patnext == QUOTE) {
89 if (*++patnext == EOS) {
90 *bufnext++ = QUOTE | M_PROTECT;
91 @@ -221,7 +233,7 @@ glob(pattern, flags, errfunc, pglob)
95 - clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
96 + clen = mbrtowc_l(&wc, (const char *)patnext, MB_LEN_MAX, &mbs, loc);
97 if (clen == (size_t)-1 || clen == (size_t)-2)
98 return (GLOB_NOMATCH);
100 @@ -233,9 +245,9 @@ glob(pattern, flags, errfunc, pglob)
103 if (flags & GLOB_BRACE)
104 - return globexp1(patbuf, pglob, &limit);
105 + return globexp1(patbuf, pglob, &limit, loc);
107 - return glob0(patbuf, pglob, &limit);
108 + return glob0(patbuf, pglob, &limit, loc);
112 @@ -244,23 +256,24 @@ glob(pattern, flags, errfunc, pglob)
116 -globexp1(pattern, pglob, limit)
117 +globexp1(pattern, pglob, limit, loc)
123 const Char* ptr = pattern;
126 /* Protect a single {}, for find(1), like csh */
127 if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
128 - return glob0(pattern, pglob, limit);
129 + return glob0(pattern, pglob, limit, loc);
131 while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL)
132 - if (!globexp2(ptr, pattern, pglob, &rv, limit))
133 + if (!globexp2(ptr, pattern, pglob, &rv, limit, loc))
136 - return glob0(pattern, pglob, limit);
137 + return glob0(pattern, pglob, limit, loc);
141 @@ -270,10 +283,11 @@ globexp1(pattern, pglob, limit)
142 * If it fails then it tries to glob the rest of the pattern and returns.
145 -globexp2(ptr, pattern, pglob, rv, limit)
146 +globexp2(ptr, pattern, pglob, rv, limit, loc)
147 const Char *ptr, *pattern;
154 @@ -310,7 +324,7 @@ globexp2(ptr, pattern, pglob, rv, limit)
156 /* Non matching braces; just glob the pattern */
157 if (i != 0 || *pe == EOS) {
158 - *rv = glob0(patbuf, pglob, limit);
159 + *rv = glob0(patbuf, pglob, limit, loc);
163 @@ -357,7 +371,7 @@ globexp2(ptr, pattern, pglob, rv, limit)
165 qprintf("globexp2:", patbuf);
167 - *rv = globexp1(patbuf, pglob, limit);
168 + *rv = globexp1(patbuf, pglob, limit, loc);
170 /* move after the comma, to the next string */
172 @@ -373,10 +387,11 @@ globexp2(ptr, pattern, pglob, rv, limit)
176 +#ifndef BUILDING_VARIANT
178 * expand tilde from the passwd file.
181 +__private_extern__ const Char *
182 globtilde(pattern, patbuf, patbuf_len, pglob)
185 @@ -438,6 +453,7 @@ globtilde(pattern, patbuf, patbuf_len, p
189 +#endif /* BUILDING_VARIANT */
193 @@ -447,13 +463,15 @@ globtilde(pattern, patbuf, patbuf_len, p
194 * if things went well, nonzero if errors occurred.
197 -glob0(pattern, pglob, limit)
198 +glob0(pattern, pglob, limit, loc)
204 const Char *qpatnext;
205 - int c, err, oldpathc;
208 Char *bufnext, patbuf[MAXPATHLEN];
210 qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
211 @@ -462,6 +480,10 @@ glob0(pattern, pglob, limit)
213 /* We don't need to check for buffer overflow any more. */
214 while ((c = *qpatnext++) != EOS) {
215 + if (c & M_PROTECT) {
216 + *bufnext++ = CHAR(c);
222 @@ -512,7 +534,7 @@ glob0(pattern, pglob, limit)
223 qprintf("glob0:", patbuf);
226 - if ((err = glob1(patbuf, pglob, limit)) != 0)
227 + if ((err = glob1(patbuf, pglob, limit, loc)) != 0)
231 @@ -525,7 +547,7 @@ glob0(pattern, pglob, limit)
232 if (((pglob->gl_flags & GLOB_NOCHECK) ||
233 ((pglob->gl_flags & GLOB_NOMAGIC) &&
234 !(pglob->gl_flags & GLOB_MAGCHAR))))
235 - return(globextend(pattern, pglob, limit));
236 + return(globextend(pattern, pglob, limit, loc));
238 return(GLOB_NOMATCH);
240 @@ -535,18 +557,21 @@ glob0(pattern, pglob, limit)
245 +#ifndef BUILDING_VARIANT
246 +__private_extern__ int
250 - return(strcmp(*(char **)p, *(char **)q));
251 + return(strcoll(*(char **)p, *(char **)q));
253 +#endif /* BUILDING_VARIANT */
256 -glob1(pattern, pglob, limit)
257 +glob1(pattern, pglob, limit, loc)
263 Char pathbuf[MAXPATHLEN];
265 @@ -554,7 +579,7 @@ glob1(pattern, pglob, limit)
268 return(glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1,
269 - pattern, pglob, limit));
270 + pattern, pglob, limit, loc));
274 @@ -563,10 +588,11 @@ glob1(pattern, pglob, limit)
278 -glob2(pathbuf, pathend, pathend_last, pattern, pglob, limit)
279 +glob2(pathbuf, pathend, pathend_last, pattern, pglob, limit, loc)
280 Char *pathbuf, *pathend, *pathend_last, *pattern;
287 @@ -579,13 +605,13 @@ glob2(pathbuf, pathend, pathend_last, pa
288 for (anymeta = 0;;) {
289 if (*pattern == EOS) { /* End of pattern? */
291 - if (g_lstat(pathbuf, &sb, pglob))
292 + if (g_lstat(pathbuf, &sb, pglob, loc))
295 if (((pglob->gl_flags & GLOB_MARK) &&
296 pathend[-1] != SEP) && (S_ISDIR(sb.st_mode)
297 || (S_ISLNK(sb.st_mode) &&
298 - (g_stat(pathbuf, &sb, pglob) == 0) &&
299 + (g_stat(pathbuf, &sb, pglob, loc) == 0) &&
300 S_ISDIR(sb.st_mode)))) {
301 if (pathend + 1 > pathend_last)
302 return (GLOB_ABORTED);
303 @@ -593,7 +619,7 @@ glob2(pathbuf, pathend, pathend_last, pa
307 - return(globextend(pathbuf, pglob, limit));
308 + return(globextend(pathbuf, pglob, limit, loc));
311 /* Find end of next segment, copy tentatively to pathend. */
312 @@ -617,16 +643,17 @@ glob2(pathbuf, pathend, pathend_last, pa
314 } else /* Need expansion, recurse. */
315 return(glob3(pathbuf, pathend, pathend_last, pattern, p,
317 + pglob, limit, loc));
323 -glob3(pathbuf, pathend, pathend_last, pattern, restpattern, pglob, limit)
324 +glob3(pathbuf, pathend, pathend_last, pattern, restpattern, pglob, limit, loc)
325 Char *pathbuf, *pathend, *pathend_last, *pattern, *restpattern;
332 @@ -646,15 +673,16 @@ glob3(pathbuf, pathend, pathend_last, pa
336 - if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
337 + if ((dirp = g_opendir(pathbuf, pglob, loc)) == NULL) {
338 /* TODO: don't call for ENOENT or ENOTDIR? */
339 if (pglob->gl_errfunc) {
340 - if (g_Ctoc(pathbuf, buf, sizeof(buf)))
341 + if (g_Ctoc(pathbuf, buf, sizeof(buf), loc))
342 return (GLOB_ABORTED);
343 - if (pglob->gl_errfunc(buf, errno) ||
344 - pglob->gl_flags & GLOB_ERR)
345 + if (pglob->gl_errfunc(buf, errno))
346 return (GLOB_ABORTED);
348 + if (pglob->gl_flags & GLOB_ERR)
349 + return (GLOB_ABORTED);
353 @@ -679,7 +707,7 @@ glob3(pathbuf, pathend, pathend_last, pa
355 sc = (u_char *) dp->d_name;
356 while (dc < pathend_last) {
357 - clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
358 + clen = mbrtowc_l(&wc, (const char *)sc, MB_LEN_MAX, &mbs, loc);
359 if (clen == (size_t)-1 || clen == (size_t)-2) {
362 @@ -689,12 +717,12 @@ glob3(pathbuf, pathend, pathend_last, pa
366 - if (!match(pathend, pattern, restpattern)) {
367 + if (!match(pathend, pattern, restpattern, loc)) {
371 err = glob2(pathbuf, --dc, pathend_last, restpattern,
373 + pglob, limit, loc);
377 @@ -707,6 +735,7 @@ glob3(pathbuf, pathend, pathend_last, pa
381 +#ifndef BUILDING_VARIANT
383 * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
384 * add the new item, and update gl_pathc.
385 @@ -721,11 +750,12 @@ glob3(pathbuf, pathend, pathend_last, pa
386 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
387 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
390 -globextend(path, pglob, limit)
391 +__private_extern__ int
392 +globextend(path, pglob, limit, loc)
400 @@ -760,9 +790,9 @@ globextend(path, pglob, limit)
402 for (p = path; *p++;)
404 - len = MB_CUR_MAX * (size_t)(p - path); /* XXX overallocation */
405 + len = MB_CUR_MAX_L(loc) * (size_t)(p - path); /* XXX overallocation */
406 if ((copy = malloc(len)) != NULL) {
407 - if (g_Ctoc(path, copy, len)) {
408 + if (g_Ctoc(path, copy, len, loc)) {
410 return (GLOB_NOSPACE);
412 @@ -776,9 +806,10 @@ globextend(path, pglob, limit)
413 * pattern matching function for filenames. Each occurrence of the *
414 * pattern causes a recursion level.
417 -match(name, pat, patend)
418 +__private_extern__ int
419 +match(name, pat, patend, loc)
420 Char *name, *pat, *patend;
423 int ok, negate_range;
425 @@ -790,7 +821,7 @@ match(name, pat, patend)
429 - if (match(name, pat, patend))
430 + if (match(name, pat, patend, loc))
432 while (*name++ != EOS);
434 @@ -806,10 +837,10 @@ match(name, pat, patend)
436 while (((c = *pat++) & M_MASK) != M_END)
437 if ((*pat & M_MASK) == M_RNG) {
438 - if (__collate_load_error ?
439 + if (loc->__collate_load_error ?
440 CHAR(c) <= CHAR(k) && CHAR(k) <= CHAR(pat[1]) :
441 - __collate_range_cmp(CHAR(c), CHAR(k)) <= 0
442 - && __collate_range_cmp(CHAR(k), CHAR(pat[1])) <= 0
443 + __collate_range_cmp(CHAR(c), CHAR(k), loc) <= 0
444 + && __collate_range_cmp(CHAR(k), CHAR(pat[1]), loc) <= 0
448 @@ -844,18 +875,20 @@ globfree(pglob)
449 pglob->gl_pathv = NULL;
452 +#endif /* !BUILDING_VARIANT */
455 -g_opendir(str, pglob)
456 +g_opendir(str, pglob, loc)
461 char buf[MAXPATHLEN];
466 - if (g_Ctoc(str, buf, sizeof(buf)))
467 + if (g_Ctoc(str, buf, sizeof(buf), loc))
471 @@ -866,14 +899,15 @@ g_opendir(str, pglob)
475 -g_lstat(fn, sb, pglob)
476 +g_lstat(fn, sb, pglob, loc)
482 char buf[MAXPATHLEN];
484 - if (g_Ctoc(fn, buf, sizeof(buf))) {
485 + if (g_Ctoc(fn, buf, sizeof(buf), loc)) {
486 errno = ENAMETOOLONG;
489 @@ -883,14 +917,15 @@ g_lstat(fn, sb, pglob)
493 -g_stat(fn, sb, pglob)
494 +g_stat(fn, sb, pglob, loc)
500 char buf[MAXPATHLEN];
502 - if (g_Ctoc(fn, buf, sizeof(buf))) {
503 + if (g_Ctoc(fn, buf, sizeof(buf), loc)) {
504 errno = ENAMETOOLONG;
507 @@ -899,7 +934,8 @@ g_stat(fn, sb, pglob)
508 return(stat(buf, sb));
512 +#ifndef BUILDING_VARIANT
513 +__private_extern__ Char *
517 @@ -911,18 +947,20 @@ g_strchr(str, ch)
522 -g_Ctoc(str, buf, len)
523 +__private_extern__ int
524 +g_Ctoc(str, buf, len, loc)
532 + int mb_cur_max = MB_CUR_MAX_L(loc);
534 memset(&mbs, 0, sizeof(mbs));
535 - while (len >= MB_CUR_MAX) {
536 - clen = wcrtomb(buf, *str, &mbs);
537 + while (len >= mb_cur_max) {
538 + clen = wcrtomb_l(buf, *str, &mbs, loc);
539 if (clen == (size_t)-1)
542 @@ -954,3 +992,4 @@ qprintf(str, s)
546 +#endif /* !BUILDING_VARIANT */