]> git.saurik.com Git - apple/libc.git/blob - stdio/FreeBSD/vfwscanf.c.patch
Libc-583.tar.gz
[apple/libc.git] / stdio / FreeBSD / vfwscanf.c.patch
1 --- vfwscanf.c.orig 2009-02-15 03:11:22.000000000 -0800
2 +++ vfwscanf.c 2009-02-16 00:10:06.000000000 -0800
3 @@ -42,6 +42,8 @@ static char sccsid[] = "@(#)vfscanf.c 8.
4 #include <sys/cdefs.h>
5 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfwscanf.c,v 1.12 2004/05/02 20:13:29 obrien Exp $");
6
7 +#include "xlocale_private.h"
8 +
9 #include "namespace.h"
10 #include <ctype.h>
11 #include <inttypes.h>
12 @@ -98,7 +100,9 @@ __FBSDID("$FreeBSD: src/lib/libc/stdio/v
13 #define CT_INT 3 /* %[dioupxX] conversion */
14 #define CT_FLOAT 4 /* %[efgEFG] conversion */
15
16 -static int parsefloat(FILE *, wchar_t *, wchar_t *);
17 +#ifndef NO_FLOATING_POINT
18 +static int parsefloat(FILE *, wchar_t **, size_t, locale_t loc);
19 +#endif /* !NO_FLOATING_POINT */
20
21 extern int __scanfdebug;
22
23 @@ -116,7 +120,21 @@ vfwscanf(FILE * __restrict fp, const wch
24
25 FLOCKFILE(fp);
26 ORIENT(fp, 1);
27 - ret = __vfwscanf(fp, fmt, ap);
28 + ret = __vfwscanf(fp, __current_locale(), fmt, ap);
29 + FUNLOCKFILE(fp);
30 + return (ret);
31 +}
32 +
33 +int
34 +vfwscanf_l(FILE * __restrict fp, locale_t loc, const wchar_t * __restrict fmt,
35 + va_list ap)
36 +{
37 + int ret;
38 +
39 + NORMALIZE_LOCALE(loc);
40 + FLOCKFILE(fp);
41 + ORIENT(fp, 1);
42 + ret = __vfwscanf(fp, loc, fmt, ap);
43 FUNLOCKFILE(fp);
44 return (ret);
45 }
46 @@ -124,8 +142,9 @@ vfwscanf(FILE * __restrict fp, const wch
47 /*
48 * Non-MT-safe version.
49 */
50 -int
51 -__vfwscanf(FILE * __restrict fp, const wchar_t * __restrict fmt, va_list ap)
52 +__private_extern__ int
53 +__vfwscanf(FILE * __restrict fp, locale_t loc, const wchar_t * __restrict fmt,
54 + va_list ap)
55 {
56 wint_t c; /* character from format, or conversion */
57 size_t width; /* field width, or 0 */
58 @@ -134,7 +153,6 @@ __vfwscanf(FILE * __restrict fp, const w
59 int flags; /* flags as defined above */
60 wchar_t *p0; /* saves original value of p when necessary */
61 int nassigned; /* number of fields assigned */
62 - int nconversions; /* number of conversions */
63 int nread; /* number of characters consumed from fp */
64 int base; /* base argument to conversion function */
65 wchar_t buf[BUF]; /* buffer for numeric conversions */
66 @@ -145,31 +163,37 @@ __vfwscanf(FILE * __restrict fp, const w
67 char *mbp; /* multibyte string pointer for %c %s %[ */
68 size_t nconv; /* number of bytes in mb. conversion */
69 char mbbuf[MB_LEN_MAX]; /* temporary mb. character buffer */
70 + int index; /* for %index$ */
71 + va_list ap_orig; /* to reset ap to first argument */
72 static const mbstate_t initial;
73 mbstate_t mbs;
74 + int mb_cur_max = MB_CUR_MAX_L(loc);
75
76 /* `basefix' is used to avoid `if' tests in the integer scanner */
77 static short basefix[17] =
78 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
79
80 nassigned = 0;
81 - nconversions = 0;
82 nread = 0;
83 ccls = ccle = NULL;
84 + va_copy(ap_orig, ap);
85 for (;;) {
86 c = *fmt++;
87 if (c == 0)
88 return (nassigned);
89 - if (iswspace(c)) {
90 - while ((c = __fgetwc(fp)) != WEOF &&
91 - iswspace(c))
92 + if (iswspace_l(c, loc)) {
93 + while ((c = __fgetwc(fp, loc)) != WEOF &&
94 + iswspace_l(c, loc))
95 ;
96 if (c != WEOF)
97 - __ungetwc(c, fp);
98 + __ungetwc(c, fp, loc);
99 continue;
100 }
101 - if (c != '%')
102 + if (c != '%') {
103 + if ((wi = __fgetwc(fp, loc)) == WEOF)
104 + goto input_failure;
105 goto literal;
106 + }
107 width = 0;
108 flags = 0;
109 /*
110 @@ -179,16 +203,34 @@ __vfwscanf(FILE * __restrict fp, const w
111 again: c = *fmt++;
112 switch (c) {
113 case '%':
114 + /* Consume leading white space */
115 + for(;;) {
116 + if ((wi = __fgetwc(fp, loc)) == WEOF)
117 + goto input_failure;
118 + if (!iswspace_l(wi, loc))
119 + break;
120 + nread++;
121 + }
122 literal:
123 - if ((wi = __fgetwc(fp)) == WEOF)
124 - goto input_failure;
125 if (wi != c) {
126 - __ungetwc(wi, fp);
127 - goto input_failure;
128 + __ungetwc(wi, fp, loc);
129 + goto match_failure;
130 }
131 nread++;
132 continue;
133
134 + case '$':
135 + index = width;
136 + if (index < 1 || index > NL_ARGMAX || fmt[-3] != '%') {
137 + goto input_failure;
138 + }
139 + width = 0;
140 + va_end(ap);
141 + va_copy(ap, ap_orig); /* reset to %1$ */
142 + for (; index > 1; index--) {
143 + va_arg(ap, void*);
144 + }
145 + goto again;
146 case '*':
147 flags |= SUPPRESS;
148 goto again;
149 @@ -307,7 +349,6 @@ literal:
150 break;
151
152 case 'n':
153 - nconversions++;
154 if (flags & SUPPRESS) /* ??? */
155 continue;
156 if (flags & SHORTSHORT)
157 @@ -343,11 +384,11 @@ literal:
158 * that suppress this.
159 */
160 if ((flags & NOSKIP) == 0) {
161 - while ((wi = __fgetwc(fp)) != WEOF && iswspace(wi))
162 + while ((wi = __fgetwc(fp, loc)) != WEOF && iswspace_l(wi, loc))
163 nread++;
164 if (wi == WEOF)
165 goto input_failure;
166 - __ungetwc(wi, fp);
167 + __ungetwc(wi, fp, loc);
168 }
169
170 /*
171 @@ -364,7 +405,7 @@ literal:
172 p = va_arg(ap, wchar_t *);
173 n = 0;
174 while (width-- != 0 &&
175 - (wi = __fgetwc(fp)) != WEOF) {
176 + (wi = __fgetwc(fp, loc)) != WEOF) {
177 if (!(flags & SUPPRESS))
178 *p++ = (wchar_t)wi;
179 n++;
180 @@ -380,19 +421,19 @@ literal:
181 n = 0;
182 mbs = initial;
183 while (width != 0 &&
184 - (wi = __fgetwc(fp)) != WEOF) {
185 - if (width >= MB_CUR_MAX &&
186 + (wi = __fgetwc(fp, loc)) != WEOF) {
187 + if (width >= mb_cur_max &&
188 !(flags & SUPPRESS)) {
189 - nconv = wcrtomb(mbp, wi, &mbs);
190 + nconv = wcrtomb_l(mbp, wi, &mbs, loc);
191 if (nconv == (size_t)-1)
192 goto input_failure;
193 } else {
194 - nconv = wcrtomb(mbbuf, wi,
195 - &mbs);
196 + nconv = wcrtomb_l(mbbuf, wi,
197 + &mbs, loc);
198 if (nconv == (size_t)-1)
199 goto input_failure;
200 if (nconv > width) {
201 - __ungetwc(wi, fp);
202 + __ungetwc(wi, fp, loc);
203 break;
204 }
205 if (!(flags & SUPPRESS))
206 @@ -410,7 +451,6 @@ literal:
207 if (!(flags & SUPPRESS))
208 nassigned++;
209 }
210 - nconversions++;
211 break;
212
213 case CT_CCL:
214 @@ -420,20 +460,20 @@ literal:
215 /* take only those things in the class */
216 if ((flags & SUPPRESS) && (flags & LONG)) {
217 n = 0;
218 - while ((wi = __fgetwc(fp)) != WEOF &&
219 + while ((wi = __fgetwc(fp, loc)) != WEOF &&
220 width-- != 0 && INCCL(wi))
221 n++;
222 if (wi != WEOF)
223 - __ungetwc(wi, fp);
224 + __ungetwc(wi, fp, loc);
225 if (n == 0)
226 goto match_failure;
227 } else if (flags & LONG) {
228 p0 = p = va_arg(ap, wchar_t *);
229 - while ((wi = __fgetwc(fp)) != WEOF &&
230 + while ((wi = __fgetwc(fp, loc)) != WEOF &&
231 width-- != 0 && INCCL(wi))
232 *p++ = (wchar_t)wi;
233 if (wi != WEOF)
234 - __ungetwc(wi, fp);
235 + __ungetwc(wi, fp, loc);
236 n = p - p0;
237 if (n == 0)
238 goto match_failure;
239 @@ -444,16 +484,16 @@ literal:
240 mbp = va_arg(ap, char *);
241 n = 0;
242 mbs = initial;
243 - while ((wi = __fgetwc(fp)) != WEOF &&
244 + while ((wi = __fgetwc(fp, loc)) != WEOF &&
245 width != 0 && INCCL(wi)) {
246 - if (width >= MB_CUR_MAX &&
247 + if (width >= mb_cur_max &&
248 !(flags & SUPPRESS)) {
249 - nconv = wcrtomb(mbp, wi, &mbs);
250 + nconv = wcrtomb_l(mbp, wi, &mbs, loc);
251 if (nconv == (size_t)-1)
252 goto input_failure;
253 } else {
254 - nconv = wcrtomb(mbbuf, wi,
255 - &mbs);
256 + nconv = wcrtomb_l(mbbuf, wi,
257 + &mbs, loc);
258 if (nconv == (size_t)-1)
259 goto input_failure;
260 if (nconv > width)
261 @@ -468,14 +508,15 @@ literal:
262 n++;
263 }
264 if (wi != WEOF)
265 - __ungetwc(wi, fp);
266 + __ungetwc(wi, fp, loc);
267 + if (n == 0)
268 + goto match_failure;
269 if (!(flags & SUPPRESS)) {
270 *mbp = 0;
271 nassigned++;
272 }
273 }
274 nread += n;
275 - nconversions++;
276 break;
277
278 case CT_STRING:
279 @@ -483,39 +524,39 @@ literal:
280 if (width == 0)
281 width = (size_t)~0;
282 if ((flags & SUPPRESS) && (flags & LONG)) {
283 - while ((wi = __fgetwc(fp)) != WEOF &&
284 + while ((wi = __fgetwc(fp, loc)) != WEOF &&
285 width-- != 0 &&
286 - !iswspace(wi))
287 + !iswspace_l(wi, loc))
288 nread++;
289 if (wi != WEOF)
290 - __ungetwc(wi, fp);
291 + __ungetwc(wi, fp, loc);
292 } else if (flags & LONG) {
293 p0 = p = va_arg(ap, wchar_t *);
294 - while ((wi = __fgetwc(fp)) != WEOF &&
295 + while ((wi = __fgetwc(fp, loc)) != WEOF &&
296 width-- != 0 &&
297 - !iswspace(wi)) {
298 + !iswspace_l(wi, loc)) {
299 *p++ = (wchar_t)wi;
300 nread++;
301 }
302 if (wi != WEOF)
303 - __ungetwc(wi, fp);
304 + __ungetwc(wi, fp, loc);
305 *p = '\0';
306 nassigned++;
307 } else {
308 if (!(flags & SUPPRESS))
309 mbp = va_arg(ap, char *);
310 mbs = initial;
311 - while ((wi = __fgetwc(fp)) != WEOF &&
312 + while ((wi = __fgetwc(fp, loc)) != WEOF &&
313 width != 0 &&
314 - !iswspace(wi)) {
315 - if (width >= MB_CUR_MAX &&
316 + !iswspace_l(wi, loc)) {
317 + if (width >= mb_cur_max &&
318 !(flags & SUPPRESS)) {
319 - nconv = wcrtomb(mbp, wi, &mbs);
320 + nconv = wcrtomb_l(mbp, wi, &mbs, loc);
321 if (nconv == (size_t)-1)
322 goto input_failure;
323 } else {
324 - nconv = wcrtomb(mbbuf, wi,
325 - &mbs);
326 + nconv = wcrtomb_l(mbbuf, wi,
327 + &mbs, loc);
328 if (nconv == (size_t)-1)
329 goto input_failure;
330 if (nconv > width)
331 @@ -530,13 +571,12 @@ literal:
332 nread++;
333 }
334 if (wi != WEOF)
335 - __ungetwc(wi, fp);
336 + __ungetwc(wi, fp, loc);
337 if (!(flags & SUPPRESS)) {
338 *mbp = 0;
339 nassigned++;
340 }
341 }
342 - nconversions++;
343 continue;
344
345 case CT_INT:
346 @@ -546,7 +586,7 @@ literal:
347 width = sizeof(buf) / sizeof(*buf) - 1;
348 flags |= SIGNOK | NDIGITS | NZDIGITS;
349 for (p = buf; width; width--) {
350 - c = __fgetwc(fp);
351 + c = __fgetwc(fp, loc);
352 /*
353 * Switch on the character; `goto ok'
354 * if we accept it as a part of number.
355 @@ -630,7 +670,7 @@ literal:
356 * for a number. Stop accumulating digits.
357 */
358 if (c != WEOF)
359 - __ungetwc(c, fp);
360 + __ungetwc(c, fp, loc);
361 break;
362 ok:
363 /*
364 @@ -646,22 +686,22 @@ literal:
365 */
366 if (flags & NDIGITS) {
367 if (p > buf)
368 - __ungetwc(*--p, fp);
369 + __ungetwc(*--p, fp, loc);
370 goto match_failure;
371 }
372 c = p[-1];
373 if (c == 'x' || c == 'X') {
374 --p;
375 - __ungetwc(c, fp);
376 + __ungetwc(c, fp, loc);
377 }
378 if ((flags & SUPPRESS) == 0) {
379 uintmax_t res;
380
381 *p = 0;
382 if ((flags & UNSIGNED) == 0)
383 - res = wcstoimax(buf, NULL, base);
384 + res = wcstoimax_l(buf, NULL, base, loc);
385 else
386 - res = wcstoumax(buf, NULL, base);
387 + res = wcstoumax_l(buf, NULL, base, loc);
388 if (flags & POINTER)
389 *va_arg(ap, void **) =
390 (void *)(uintptr_t)res;
391 @@ -684,47 +724,47 @@ literal:
392 nassigned++;
393 }
394 nread += p - buf;
395 - nconversions++;
396 break;
397
398 #ifndef NO_FLOATING_POINT
399 case CT_FLOAT:
400 + {
401 + wchar_t *pbuf;
402 /* scan a floating point number as if by strtod */
403 - if (width == 0 || width > sizeof(buf) /
404 - sizeof(*buf) - 1)
405 - width = sizeof(buf) / sizeof(*buf) - 1;
406 - if ((width = parsefloat(fp, buf, buf + width)) == 0)
407 + if ((width = parsefloat(fp, &pbuf, width, loc)) == 0)
408 goto match_failure;
409 if ((flags & SUPPRESS) == 0) {
410 if (flags & LONGDBL) {
411 - long double res = wcstold(buf, &p);
412 + long double res = wcstold_l(pbuf, &p, loc);
413 *va_arg(ap, long double *) = res;
414 } else if (flags & LONG) {
415 - double res = wcstod(buf, &p);
416 + double res = wcstod_l(pbuf, &p, loc);
417 *va_arg(ap, double *) = res;
418 } else {
419 - float res = wcstof(buf, &p);
420 + float res = wcstof_l(pbuf, &p, loc);
421 *va_arg(ap, float *) = res;
422 }
423 - if (__scanfdebug && p - buf != width)
424 - abort();
425 + if (__scanfdebug && p - pbuf != width)
426 + LIBC_ABORT("p - pbuf %ld != width %ld", (long)(p - pbuf), width);
427 nassigned++;
428 }
429 nread += width;
430 - nconversions++;
431 break;
432 + }
433 #endif /* !NO_FLOATING_POINT */
434 }
435 }
436 input_failure:
437 - return (nconversions != 0 ? nassigned : EOF);
438 + return (nassigned ? nassigned : EOF);
439 match_failure:
440 return (nassigned);
441 }
442
443 #ifndef NO_FLOATING_POINT
444 +extern char *__parsefloat_buf(size_t s); /* see vfscanf-fbsd.c */
445 +
446 static int
447 -parsefloat(FILE *fp, wchar_t *buf, wchar_t *end)
448 +parsefloat(FILE *fp, wchar_t **buf, size_t width, locale_t loc)
449 {
450 wchar_t *commit, *p;
451 int infnanpos = 0;
452 @@ -733,9 +773,19 @@ parsefloat(FILE *fp, wchar_t *buf, wchar
453 S_DIGITS, S_FRAC, S_EXP, S_EXPDIGITS
454 } state = S_START;
455 wchar_t c;
456 - wchar_t decpt = (wchar_t)(unsigned char)*localeconv()->decimal_point;
457 + char *decimal_point;
458 + wchar_t decpt;
459 _Bool gotmantdig = 0, ishex = 0;
460 -
461 + wchar_t *b;
462 + wchar_t *e;
463 + size_t s;
464 +
465 + s = (width == 0 ? BUF : (width + 1));
466 + if ((b = (wchar_t *)__parsefloat_buf(s * sizeof(wchar_t))) == NULL) {
467 + *buf = NULL;
468 + return 0;
469 + }
470 + e = b + (s - 1);
471 /*
472 * We set commit = p whenever the string we have read so far
473 * constitutes a valid representation of a floating point
474 @@ -745,10 +795,12 @@ parsefloat(FILE *fp, wchar_t *buf, wchar
475 * always necessary to read at least one character that doesn't
476 * match; thus, we can't short-circuit "infinity" or "nan(...)".
477 */
478 - commit = buf - 1;
479 + commit = b - 1;
480 c = WEOF;
481 - for (p = buf; p < end; ) {
482 - if ((c = __fgetwc(fp)) == WEOF)
483 + decimal_point = localeconv_l(loc)->decimal_point;
484 + mbtowc_l(&decpt, decimal_point, strlen(decimal_point), loc);
485 + for (p = b; width == 0 || p < e; ) {
486 + if ((c = __fgetwc(fp, loc)) == WEOF)
487 break;
488 reswitch:
489 switch (state) {
490 @@ -808,7 +860,7 @@ reswitch:
491 if (c == ')') {
492 commit = p;
493 infnanpos = -2;
494 - } else if (!iswalnum(c) && c != '_')
495 + } else if (!iswalnum_l(c, loc) && c != '_')
496 goto parsedone;
497 break;
498 }
499 @@ -824,7 +876,7 @@ reswitch:
500 goto reswitch;
501 }
502 case S_DIGITS:
503 - if ((ishex && iswxdigit(c)) || iswdigit(c))
504 + if ((ishex && iswxdigit_l(c, loc)) || iswdigit_l(c, loc))
505 gotmantdig = 1;
506 else {
507 state = S_FRAC;
508 @@ -841,7 +893,7 @@ reswitch:
509 goto parsedone;
510 else
511 state = S_EXP;
512 - } else if ((ishex && iswxdigit(c)) || iswdigit(c)) {
513 + } else if ((ishex && iswxdigit_l(c, loc)) || iswdigit_l(c, loc)) {
514 commit = p;
515 gotmantdig = 1;
516 } else
517 @@ -854,13 +906,26 @@ reswitch:
518 else
519 goto reswitch;
520 case S_EXPDIGITS:
521 - if (iswdigit(c))
522 + if (iswdigit_l(c, loc))
523 commit = p;
524 else
525 goto parsedone;
526 break;
527 default:
528 - abort();
529 + LIBC_ABORT("unknown state %d", state);
530 + }
531 + if (p >= e) {
532 + ssize_t diff = (p - b);
533 + ssize_t com = (commit - b);
534 + s += BUF;
535 + b = (wchar_t *)__parsefloat_buf(s * sizeof(wchar_t));
536 + if (b == NULL) {
537 + *buf = NULL;
538 + return 0;
539 + }
540 + e = b + (s - 1);
541 + p = b + diff;
542 + commit = b + com;
543 }
544 *p++ = c;
545 c = WEOF;
546 @@ -868,10 +933,11 @@ reswitch:
547
548 parsedone:
549 if (c != WEOF)
550 - __ungetwc(c, fp);
551 + __ungetwc(c, fp, loc);
552 while (commit < --p)
553 - __ungetwc(*p, fp);
554 + __ungetwc(*p, fp, loc);
555 *++commit = '\0';
556 - return (commit - buf);
557 + *buf = b;
558 + return (commit - b);
559 }
560 #endif