]> git.saurik.com Git - apple/libc.git/blob - gen/FreeBSD/glob.c.patch
Libc-498.1.7.tar.gz
[apple/libc.git] / gen / FreeBSD / glob.c.patch
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 (
4 #include <sys/cdefs.h>
5 __FBSDID("$FreeBSD: src/lib/libc/gen/glob.c,v 1.22 2004/07/29 03:48:52 tjr Exp $");
6
7 +#include "xlocale_private.h"
8 +
9 /*
10 * glob(3) -- a superset of the one defined in POSIX 1003.2.
11 *
12 @@ -143,25 +145,33 @@ typedef char Char;
13 #define ismeta(c) (((c)&M_QUOTE) != 0)
14
15
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);
34 +
35 +
36 +static int g_lstat(Char *, struct stat *, glob_t *, locale_t);
37 +static DIR *g_opendir(Char *, glob_t *, locale_t);
38 #ifdef notdef
39 static Char *g_strcat(Char *, const Char *);
40 #endif
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 *);
47 -static const Char *
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);
59 #ifdef DEBUG
60 static void qprintf(const char *, Char *);
61 #endif
62 @@ -178,6 +188,8 @@ glob(pattern, flags, errfunc, pglob)
63 mbstate_t mbs;
64 wchar_t wc;
65 size_t clen;
66 + locale_t loc = __current_locale();
67 + int mb_cur_max = MB_CUR_MAX_L(loc);
68
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);
81 else if (clen == 0)
82 @@ -212,7 +224,7 @@ glob(pattern, flags, errfunc, pglob)
83 } else {
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)
92 prot = M_PROTECT;
93 } else
94 prot = 0;
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);
99 else if (clen == 0)
100 @@ -233,9 +245,9 @@ glob(pattern, flags, errfunc, pglob)
101 *bufnext = EOS;
102
103 if (flags & GLOB_BRACE)
104 - return globexp1(patbuf, pglob, &limit);
105 + return globexp1(patbuf, pglob, &limit, loc);
106 else
107 - return glob0(patbuf, pglob, &limit);
108 + return glob0(patbuf, pglob, &limit, loc);
109 }
110
111 /*
112 @@ -244,23 +256,24 @@ glob(pattern, flags, errfunc, pglob)
113 * characters
114 */
115 static int
116 -globexp1(pattern, pglob, limit)
117 +globexp1(pattern, pglob, limit, loc)
118 const Char *pattern;
119 glob_t *pglob;
120 int *limit;
121 + locale_t loc;
122 {
123 const Char* ptr = pattern;
124 int rv;
125
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);
130
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))
134 return rv;
135
136 - return glob0(pattern, pglob, limit);
137 + return glob0(pattern, pglob, limit, loc);
138 }
139
140
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.
143 */
144 static int
145 -globexp2(ptr, pattern, pglob, rv, limit)
146 +globexp2(ptr, pattern, pglob, rv, limit, loc)
147 const Char *ptr, *pattern;
148 glob_t *pglob;
149 int *rv, *limit;
150 + locale_t loc;
151 {
152 int i;
153 Char *lm, *ls;
154 @@ -310,7 +324,7 @@ globexp2(ptr, pattern, pglob, rv, limit)
155
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);
160 return 0;
161 }
162
163 @@ -357,7 +371,7 @@ globexp2(ptr, pattern, pglob, rv, limit)
164 #ifdef DEBUG
165 qprintf("globexp2:", patbuf);
166 #endif
167 - *rv = globexp1(patbuf, pglob, limit);
168 + *rv = globexp1(patbuf, pglob, limit, loc);
169
170 /* move after the comma, to the next string */
171 pl = pm + 1;
172 @@ -373,10 +387,11 @@ globexp2(ptr, pattern, pglob, rv, limit)
173
174
175
176 +#ifndef BUILDING_VARIANT
177 /*
178 * expand tilde from the passwd file.
179 */
180 -static const Char *
181 +__private_extern__ const Char *
182 globtilde(pattern, patbuf, patbuf_len, pglob)
183 const Char *pattern;
184 Char *patbuf;
185 @@ -438,6 +453,7 @@ globtilde(pattern, patbuf, patbuf_len, p
186
187 return patbuf;
188 }
189 +#endif /* BUILDING_VARIANT */
190
191
192 /*
193 @@ -447,13 +463,15 @@ globtilde(pattern, patbuf, patbuf_len, p
194 * if things went well, nonzero if errors occurred.
195 */
196 static int
197 -glob0(pattern, pglob, limit)
198 +glob0(pattern, pglob, limit, loc)
199 const Char *pattern;
200 glob_t *pglob;
201 int *limit;
202 + locale_t loc;
203 {
204 const Char *qpatnext;
205 - int c, err, oldpathc;
206 + Char c;
207 + int err, oldpathc;
208 Char *bufnext, patbuf[MAXPATHLEN];
209
210 qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
211 @@ -462,6 +480,10 @@ glob0(pattern, pglob, limit)
212
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);
217 + continue;
218 + } /* else */
219 switch (c) {
220 case LBRACKET:
221 c = *qpatnext;
222 @@ -512,7 +534,7 @@ glob0(pattern, pglob, limit)
223 qprintf("glob0:", patbuf);
224 #endif
225
226 - if ((err = glob1(patbuf, pglob, limit)) != 0)
227 + if ((err = glob1(patbuf, pglob, limit, loc)) != 0)
228 return(err);
229
230 /*
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));
237 else
238 return(GLOB_NOMATCH);
239 }
240 @@ -535,18 +557,21 @@ glob0(pattern, pglob, limit)
241 return(0);
242 }
243
244 -static int
245 +#ifndef BUILDING_VARIANT
246 +__private_extern__ int
247 compare(p, q)
248 const void *p, *q;
249 {
250 - return(strcmp(*(char **)p, *(char **)q));
251 + return(strcoll(*(char **)p, *(char **)q));
252 }
253 +#endif /* BUILDING_VARIANT */
254
255 static int
256 -glob1(pattern, pglob, limit)
257 +glob1(pattern, pglob, limit, loc)
258 Char *pattern;
259 glob_t *pglob;
260 int *limit;
261 + locale_t loc;
262 {
263 Char pathbuf[MAXPATHLEN];
264
265 @@ -554,7 +579,7 @@ glob1(pattern, pglob, limit)
266 if (*pattern == EOS)
267 return(0);
268 return(glob2(pathbuf, pathbuf, pathbuf + MAXPATHLEN - 1,
269 - pattern, pglob, limit));
270 + pattern, pglob, limit, loc));
271 }
272
273 /*
274 @@ -563,10 +588,11 @@ glob1(pattern, pglob, limit)
275 * meta characters.
276 */
277 static int
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;
281 glob_t *pglob;
282 int *limit;
283 + locale_t loc;
284 {
285 struct stat sb;
286 Char *p, *q;
287 @@ -579,13 +605,13 @@ glob2(pathbuf, pathend, pathend_last, pa
288 for (anymeta = 0;;) {
289 if (*pattern == EOS) { /* End of pattern? */
290 *pathend = EOS;
291 - if (g_lstat(pathbuf, &sb, pglob))
292 + if (g_lstat(pathbuf, &sb, pglob, loc))
293 return(0);
294
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
304 *pathend = EOS;
305 }
306 ++pglob->gl_matchc;
307 - return(globextend(pathbuf, pglob, limit));
308 + return(globextend(pathbuf, pglob, limit, loc));
309 }
310
311 /* Find end of next segment, copy tentatively to pathend. */
312 @@ -617,16 +643,17 @@ glob2(pathbuf, pathend, pathend_last, pa
313 }
314 } else /* Need expansion, recurse. */
315 return(glob3(pathbuf, pathend, pathend_last, pattern, p,
316 - pglob, limit));
317 + pglob, limit, loc));
318 }
319 /* NOTREACHED */
320 }
321
322 static int
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;
326 glob_t *pglob;
327 int *limit;
328 + locale_t loc;
329 {
330 struct dirent *dp;
331 DIR *dirp;
332 @@ -646,15 +673,16 @@ glob3(pathbuf, pathend, pathend_last, pa
333 *pathend = EOS;
334 errno = 0;
335
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);
347 }
348 + if (pglob->gl_flags & GLOB_ERR)
349 + return (GLOB_ABORTED);
350 return(0);
351 }
352
353 @@ -679,7 +707,7 @@ glob3(pathbuf, pathend, pathend_last, pa
354 dc = pathend;
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) {
360 wc = *sc;
361 clen = 1;
362 @@ -689,12 +717,12 @@ glob3(pathbuf, pathend, pathend_last, pa
363 break;
364 sc += clen;
365 }
366 - if (!match(pathend, pattern, restpattern)) {
367 + if (!match(pathend, pattern, restpattern, loc)) {
368 *pathend = EOS;
369 continue;
370 }
371 err = glob2(pathbuf, --dc, pathend_last, restpattern,
372 - pglob, limit);
373 + pglob, limit, loc);
374 if (err)
375 break;
376 }
377 @@ -707,6 +735,7 @@ glob3(pathbuf, pathend, pathend_last, pa
378 }
379
380
381 +#ifndef BUILDING_VARIANT
382 /*
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.
388 */
389 -static int
390 -globextend(path, pglob, limit)
391 +__private_extern__ int
392 +globextend(path, pglob, limit, loc)
393 const Char *path;
394 glob_t *pglob;
395 int *limit;
396 + locale_t loc;
397 {
398 char **pathv;
399 int i;
400 @@ -760,9 +790,9 @@ globextend(path, pglob, limit)
401
402 for (p = path; *p++;)
403 continue;
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)) {
409 free(copy);
410 return (GLOB_NOSPACE);
411 }
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.
415 */
416 -static int
417 -match(name, pat, patend)
418 +__private_extern__ int
419 +match(name, pat, patend, loc)
420 Char *name, *pat, *patend;
421 + locale_t loc;
422 {
423 int ok, negate_range;
424 Char c, k;
425 @@ -790,7 +821,7 @@ match(name, pat, patend)
426 if (pat == patend)
427 return(1);
428 do
429 - if (match(name, pat, patend))
430 + if (match(name, pat, patend, loc))
431 return(1);
432 while (*name++ != EOS);
433 return(0);
434 @@ -806,10 +837,10 @@ match(name, pat, patend)
435 ++pat;
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
445 )
446 ok = 1;
447 pat += 2;
448 @@ -844,18 +875,20 @@ globfree(pglob)
449 pglob->gl_pathv = NULL;
450 }
451 }
452 +#endif /* !BUILDING_VARIANT */
453
454 static DIR *
455 -g_opendir(str, pglob)
456 +g_opendir(str, pglob, loc)
457 Char *str;
458 glob_t *pglob;
459 + locale_t loc;
460 {
461 char buf[MAXPATHLEN];
462
463 if (!*str)
464 strcpy(buf, ".");
465 else {
466 - if (g_Ctoc(str, buf, sizeof(buf)))
467 + if (g_Ctoc(str, buf, sizeof(buf), loc))
468 return (NULL);
469 }
470
471 @@ -866,14 +899,15 @@ g_opendir(str, pglob)
472 }
473
474 static int
475 -g_lstat(fn, sb, pglob)
476 +g_lstat(fn, sb, pglob, loc)
477 Char *fn;
478 struct stat *sb;
479 glob_t *pglob;
480 + locale_t loc;
481 {
482 char buf[MAXPATHLEN];
483
484 - if (g_Ctoc(fn, buf, sizeof(buf))) {
485 + if (g_Ctoc(fn, buf, sizeof(buf), loc)) {
486 errno = ENAMETOOLONG;
487 return (-1);
488 }
489 @@ -883,14 +917,15 @@ g_lstat(fn, sb, pglob)
490 }
491
492 static int
493 -g_stat(fn, sb, pglob)
494 +g_stat(fn, sb, pglob, loc)
495 Char *fn;
496 struct stat *sb;
497 glob_t *pglob;
498 + locale_t loc;
499 {
500 char buf[MAXPATHLEN];
501
502 - if (g_Ctoc(fn, buf, sizeof(buf))) {
503 + if (g_Ctoc(fn, buf, sizeof(buf), loc)) {
504 errno = ENAMETOOLONG;
505 return (-1);
506 }
507 @@ -899,7 +934,8 @@ g_stat(fn, sb, pglob)
508 return(stat(buf, sb));
509 }
510
511 -static Char *
512 +#ifndef BUILDING_VARIANT
513 +__private_extern__ Char *
514 g_strchr(str, ch)
515 Char *str;
516 wchar_t ch;
517 @@ -911,18 +947,20 @@ g_strchr(str, ch)
518 return (NULL);
519 }
520
521 -static int
522 -g_Ctoc(str, buf, len)
523 +__private_extern__ int
524 +g_Ctoc(str, buf, len, loc)
525 const Char *str;
526 char *buf;
527 u_int len;
528 + locale_t loc;
529 {
530 mbstate_t mbs;
531 size_t clen;
532 + int mb_cur_max = MB_CUR_MAX_L(loc);
533
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)
540 return (1);
541 if (*str == L'\0')
542 @@ -954,3 +992,4 @@ qprintf(str, s)
543 (void)printf("\n");
544 }
545 #endif
546 +#endif /* !BUILDING_VARIANT */