1 --- glob.c.orig 2009-05-12 11:21:55.000000000 -0700
2 +++ glob.c 2009-05-20 16:26:02.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,33 +145,40 @@ 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 *);
64 -glob(pattern, flags, errfunc, pglob)
66 +__glob(pattern, pglob)
68 - int flags, (*errfunc)(const char *, int);
71 const u_char *patnext;
72 @@ -178,30 +187,30 @@ glob(pattern, flags, errfunc, pglob)
76 + locale_t loc = __current_locale();
77 + int mb_cur_max = MB_CUR_MAX_L(loc);
79 patnext = (u_char *) pattern;
80 - if (!(flags & GLOB_APPEND)) {
81 + if (!(pglob->gl_flags & GLOB_APPEND)) {
83 pglob->gl_pathv = NULL;
84 - if (!(flags & GLOB_DOOFFS))
85 + if (!(pglob->gl_flags & GLOB_DOOFFS))
88 - if (flags & GLOB_LIMIT) {
89 + if (pglob->gl_flags & GLOB_LIMIT) {
90 limit = pglob->gl_matchc;
95 - pglob->gl_flags = flags & ~GLOB_MAGCHAR;
96 - pglob->gl_errfunc = errfunc;
100 bufend = bufnext + MAXPATHLEN - 1;
101 - if (flags & GLOB_NOESCAPE) {
102 + if (pglob->gl_flags & GLOB_NOESCAPE) {
103 memset(&mbs, 0, sizeof(mbs));
104 - while (bufend - bufnext >= MB_CUR_MAX) {
105 - clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
106 + while (bufend - bufnext >= mb_cur_max) {
107 + clen = mbrtowc_l(&wc, (const char *)patnext, MB_LEN_MAX, &mbs, loc);
108 if (clen == (size_t)-1 || clen == (size_t)-2)
109 return (GLOB_NOMATCH);
111 @@ -212,7 +221,7 @@ glob(pattern, flags, errfunc, pglob)
113 /* Protect the quoted characters. */
114 memset(&mbs, 0, sizeof(mbs));
115 - while (bufend - bufnext >= MB_CUR_MAX) {
116 + while (bufend - bufnext >= mb_cur_max) {
117 if (*patnext == QUOTE) {
118 if (*++patnext == EOS) {
119 *bufnext++ = QUOTE | M_PROTECT;
120 @@ -221,7 +230,7 @@ glob(pattern, flags, errfunc, pglob)
124 - clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);
125 + clen = mbrtowc_l(&wc, (const char *)patnext, MB_LEN_MAX, &mbs, loc);
126 if (clen == (size_t)-1 || clen == (size_t)-2)
127 return (GLOB_NOMATCH);
129 @@ -232,11 +241,40 @@ glob(pattern, flags, errfunc, pglob)
133 - if (flags & GLOB_BRACE)
134 - return globexp1(patbuf, pglob, &limit);
135 + if (pglob->gl_flags & GLOB_BRACE)
136 + return globexp1(patbuf, pglob, &limit, loc);
138 - return glob0(patbuf, pglob, &limit);
139 + return glob0(patbuf, pglob, &limit, loc);
143 +glob(pattern, flags, errfunc, pglob)
144 + const char *pattern;
145 + int flags, (*errfunc)(const char *, int);
149 + pglob->gl_flags = flags & ~(GLOB_MAGCHAR | _GLOB_ERR_BLOCK);
150 +#else /* !__BLOCKS__ */
151 + pglob->gl_flags = flags & ~GLOB_MAGCHAR;
152 +#endif /* __BLOCKS__ */
153 + pglob->gl_errfunc = errfunc;
154 + return __glob(pattern, pglob);
159 +glob_b(pattern, flags, errblk, pglob)
160 + const char *pattern;
161 + int flags, (^errblk)(const char *, int);
164 + pglob->gl_flags = flags & ~GLOB_MAGCHAR;
165 + pglob->gl_flags |= _GLOB_ERR_BLOCK;
166 + pglob->gl_errblk = errblk;
167 + return __glob(pattern, pglob);
169 +#endif /* __BLOCKS__ */
172 * Expand recursively a glob {} pattern. When there is no more expansion
173 @@ -244,23 +282,24 @@ glob(pattern, flags, errfunc, pglob)
177 -globexp1(pattern, pglob, limit)
178 +globexp1(pattern, pglob, limit, loc)
184 const Char* ptr = pattern;
187 /* Protect a single {}, for find(1), like csh */
188 if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
189 - return glob0(pattern, pglob, limit);
190 + return glob0(pattern, pglob, limit, loc);
192 while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL)
193 - if (!globexp2(ptr, pattern, pglob, &rv, limit))
194 + if (!globexp2(ptr, pattern, pglob, &rv, limit, loc))
197 - return glob0(pattern, pglob, limit);
198 + return glob0(pattern, pglob, limit, loc);
202 @@ -270,10 +309,11 @@ globexp1(pattern, pglob, limit)
203 * If it fails then it tries to glob the rest of the pattern and returns.
206 -globexp2(ptr, pattern, pglob, rv, limit)
207 +globexp2(ptr, pattern, pglob, rv, limit, loc)
208 const Char *ptr, *pattern;
215 @@ -310,7 +350,7 @@ globexp2(ptr, pattern, pglob, rv, limit)
217 /* Non matching braces; just glob the pattern */
218 if (i != 0 || *pe == EOS) {
219 - *rv = glob0(patbuf, pglob, limit);
220 + *rv = glob0(patbuf, pglob, limit, loc);
224 @@ -357,7 +397,7 @@ globexp2(ptr, pattern, pglob, rv, limit)
226 qprintf("globexp2:", patbuf);
228 - *rv = globexp1(patbuf, pglob, limit);
229 + *rv = globexp1(patbuf, pglob, limit, loc);
231 /* move after the comma, to the next string */
233 @@ -373,10 +413,11 @@ globexp2(ptr, pattern, pglob, rv, limit)
237 +#ifndef BUILDING_VARIANT
239 * expand tilde from the passwd file.
242 +__private_extern__ const Char *
243 globtilde(pattern, patbuf, patbuf_len, pglob)
246 @@ -438,6 +479,7 @@ globtilde(pattern, patbuf, patbuf_len, p
250 +#endif /* BUILDING_VARIANT */
254 @@ -447,13 +489,15 @@ globtilde(pattern, patbuf, patbuf_len, p
255 * if things went well, nonzero if errors occurred.
258 -glob0(pattern, pglob, limit)
259 +glob0(pattern, pglob, limit, loc)
265 const Char *qpatnext;
266 - int c, err, oldpathc;
269 Char *bufnext, patbuf[MAXPATHLEN];
271 qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
272 @@ -462,6 +506,10 @@ glob0(pattern, pglob, limit)
274 /* We don't need to check for buffer overflow any more. */
275 while ((c = *qpatnext++) != EOS) {
276 + if (c & M_PROTECT) {
277 + *bufnext++ = CHAR(c);
283 @@ -512,7 +560,7 @@ glob0(pattern, pglob, limit)
284 qprintf("glob0:", patbuf);
287 - if ((err = glob1(patbuf, pglob, limit)) != 0)
288 + if ((err = glob1(patbuf, pglob, limit, loc)) != 0)
292 @@ -525,7 +573,7 @@ glob0(pattern, pglob, limit)
293 if (((pglob->gl_flags & GLOB_NOCHECK) ||
294 ((pglob->gl_flags & GLOB_NOMAGIC) &&
295 !(pglob->gl_flags & GLOB_MAGCHAR))))
296 - return(globextend(pattern, pglob, limit));
297 + return(globextend(pattern, pglob, limit, loc));
299 return(GLOB_NOMATCH);
301 @@ -535,18 +583,21 @@ glob0(pattern, pglob, limit)
306 +#ifndef BUILDING_VARIANT
307 +__private_extern__ int
311 - return(strcmp(*(char **)p, *(char **)q));
312 + return(strcoll(*(char **)p, *(char **)q));
314 +#endif /* BUILDING_VARIANT */
317 -glob1(pattern, pglob, limit)
318 +glob1(pattern, pglob, limit, loc)
324 Char pathbuf[MAXPATHLEN];
326 @@ -554,7 +605,7 @@ glob1(pattern, pglob, limit)
329 return(glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1,
330 - pattern, pglob, limit));
331 + pattern, pglob, limit, loc));
335 @@ -563,10 +614,11 @@ glob1(pattern, pglob, limit)
339 -glob2(pathbuf, pathend, pathend_last, pattern, pglob, limit)
340 +glob2(pathbuf, pathend, pathend_last, pattern, pglob, limit, loc)
341 Char *pathbuf, *pathend, *pathend_last, *pattern;
348 @@ -579,13 +631,13 @@ glob2(pathbuf, pathend, pathend_last, pa
349 for (anymeta = 0;;) {
350 if (*pattern == EOS) { /* End of pattern? */
352 - if (g_lstat(pathbuf, &sb, pglob))
353 + if (g_lstat(pathbuf, &sb, pglob, loc))
356 if (((pglob->gl_flags & GLOB_MARK) &&
357 pathend[-1] != SEP) && (S_ISDIR(sb.st_mode)
358 || (S_ISLNK(sb.st_mode) &&
359 - (g_stat(pathbuf, &sb, pglob) == 0) &&
360 + (g_stat(pathbuf, &sb, pglob, loc) == 0) &&
361 S_ISDIR(sb.st_mode)))) {
362 if (pathend + 1 > pathend_last)
363 return (GLOB_ABORTED);
364 @@ -593,7 +645,7 @@ glob2(pathbuf, pathend, pathend_last, pa
368 - return(globextend(pathbuf, pglob, limit));
369 + return(globextend(pathbuf, pglob, limit, loc));
372 /* Find end of next segment, copy tentatively to pathend. */
373 @@ -617,16 +669,17 @@ glob2(pathbuf, pathend, pathend_last, pa
375 } else /* Need expansion, recurse. */
376 return(glob3(pathbuf, pathend, pathend_last, pattern, p,
378 + pglob, limit, loc));
384 -glob3(pathbuf, pathend, pathend_last, pattern, restpattern, pglob, limit)
385 +glob3(pathbuf, pathend, pathend_last, pattern, restpattern, pglob, limit, loc)
386 Char *pathbuf, *pathend, *pathend_last, *pattern, *restpattern;
393 @@ -646,15 +699,22 @@ glob3(pathbuf, pathend, pathend_last, pa
397 - if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
398 + if ((dirp = g_opendir(pathbuf, pglob, loc)) == NULL) {
399 /* TODO: don't call for ENOENT or ENOTDIR? */
400 if (pglob->gl_errfunc) {
401 - if (g_Ctoc(pathbuf, buf, sizeof(buf)))
402 + if (g_Ctoc(pathbuf, buf, sizeof(buf), loc))
403 return (GLOB_ABORTED);
404 - if (pglob->gl_errfunc(buf, errno) ||
405 - pglob->gl_flags & GLOB_ERR)
407 + if (pglob->gl_flags & _GLOB_ERR_BLOCK) {
408 + if (pglob->gl_errblk(buf, errno))
409 + return (GLOB_ABORTED);
411 +#endif /* __BLOCKS__ */
412 + if (pglob->gl_errfunc(buf, errno))
413 return (GLOB_ABORTED);
415 + if (pglob->gl_flags & GLOB_ERR)
416 + return (GLOB_ABORTED);
420 @@ -679,7 +739,7 @@ glob3(pathbuf, pathend, pathend_last, pa
422 sc = (u_char *) dp->d_name;
423 while (dc < pathend_last) {
424 - clen = mbrtowc(&wc, sc, MB_LEN_MAX, &mbs);
425 + clen = mbrtowc_l(&wc, (const char *)sc, MB_LEN_MAX, &mbs, loc);
426 if (clen == (size_t)-1 || clen == (size_t)-2) {
429 @@ -689,12 +749,12 @@ glob3(pathbuf, pathend, pathend_last, pa
433 - if (!match(pathend, pattern, restpattern)) {
434 + if (!match(pathend, pattern, restpattern, loc)) {
438 err = glob2(pathbuf, --dc, pathend_last, restpattern,
440 + pglob, limit, loc);
444 @@ -707,6 +767,7 @@ glob3(pathbuf, pathend, pathend_last, pa
448 +#ifndef BUILDING_VARIANT
450 * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
451 * add the new item, and update gl_pathc.
452 @@ -721,11 +782,12 @@ glob3(pathbuf, pathend, pathend_last, pa
453 * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
454 * gl_pathv points to (gl_offs + gl_pathc + 1) items.
457 -globextend(path, pglob, limit)
458 +__private_extern__ int
459 +globextend(path, pglob, limit, loc)
467 @@ -760,9 +822,9 @@ globextend(path, pglob, limit)
469 for (p = path; *p++;)
471 - len = MB_CUR_MAX * (size_t)(p - path); /* XXX overallocation */
472 + len = MB_CUR_MAX_L(loc) * (size_t)(p - path); /* XXX overallocation */
473 if ((copy = malloc(len)) != NULL) {
474 - if (g_Ctoc(path, copy, len)) {
475 + if (g_Ctoc(path, copy, len, loc)) {
477 return (GLOB_NOSPACE);
479 @@ -776,9 +838,10 @@ globextend(path, pglob, limit)
480 * pattern matching function for filenames. Each occurrence of the *
481 * pattern causes a recursion level.
484 -match(name, pat, patend)
485 +__private_extern__ int
486 +match(name, pat, patend, loc)
487 Char *name, *pat, *patend;
490 int ok, negate_range;
492 @@ -790,7 +853,7 @@ match(name, pat, patend)
496 - if (match(name, pat, patend))
497 + if (match(name, pat, patend, loc))
499 while (*name++ != EOS);
501 @@ -806,10 +869,10 @@ match(name, pat, patend)
503 while (((c = *pat++) & M_MASK) != M_END)
504 if ((*pat & M_MASK) == M_RNG) {
505 - if (__collate_load_error ?
506 + if (loc->__collate_load_error ?
507 CHAR(c) <= CHAR(k) && CHAR(k) <= CHAR(pat[1]) :
508 - __collate_range_cmp(CHAR(c), CHAR(k)) <= 0
509 - && __collate_range_cmp(CHAR(k), CHAR(pat[1])) <= 0
510 + __collate_range_cmp(CHAR(c), CHAR(k), loc) <= 0
511 + && __collate_range_cmp(CHAR(k), CHAR(pat[1]), loc) <= 0
515 @@ -844,18 +907,20 @@ globfree(pglob)
516 pglob->gl_pathv = NULL;
519 +#endif /* !BUILDING_VARIANT */
522 -g_opendir(str, pglob)
523 +g_opendir(str, pglob, loc)
528 char buf[MAXPATHLEN];
533 - if (g_Ctoc(str, buf, sizeof(buf)))
534 + if (g_Ctoc(str, buf, sizeof(buf), loc))
538 @@ -866,14 +931,15 @@ g_opendir(str, pglob)
542 -g_lstat(fn, sb, pglob)
543 +g_lstat(fn, sb, pglob, loc)
549 char buf[MAXPATHLEN];
551 - if (g_Ctoc(fn, buf, sizeof(buf))) {
552 + if (g_Ctoc(fn, buf, sizeof(buf), loc)) {
553 errno = ENAMETOOLONG;
556 @@ -883,14 +949,15 @@ g_lstat(fn, sb, pglob)
560 -g_stat(fn, sb, pglob)
561 +g_stat(fn, sb, pglob, loc)
567 char buf[MAXPATHLEN];
569 - if (g_Ctoc(fn, buf, sizeof(buf))) {
570 + if (g_Ctoc(fn, buf, sizeof(buf), loc)) {
571 errno = ENAMETOOLONG;
574 @@ -899,7 +966,8 @@ g_stat(fn, sb, pglob)
575 return(stat(buf, sb));
579 +#ifndef BUILDING_VARIANT
580 +__private_extern__ Char *
584 @@ -911,18 +979,20 @@ g_strchr(str, ch)
589 -g_Ctoc(str, buf, len)
590 +__private_extern__ int
591 +g_Ctoc(str, buf, len, loc)
599 + int mb_cur_max = MB_CUR_MAX_L(loc);
601 memset(&mbs, 0, sizeof(mbs));
602 - while (len >= MB_CUR_MAX) {
603 - clen = wcrtomb(buf, *str, &mbs);
604 + while (len >= mb_cur_max) {
605 + clen = wcrtomb_l(buf, *str, &mbs, loc);
606 if (clen == (size_t)-1)
609 @@ -954,3 +1024,4 @@ qprintf(str, s)
613 +#endif /* !BUILDING_VARIANT */