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.
5 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfwscanf.c,v 1.12 2004/05/02 20:13:29 obrien Exp $");
7 +#include "xlocale_private.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 */
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 */
21 extern int __scanfdebug;
23 @@ -116,7 +120,21 @@ vfwscanf(FILE * __restrict fp, const wch
27 - ret = __vfwscanf(fp, fmt, ap);
28 + ret = __vfwscanf(fp, __current_locale(), fmt, ap);
34 +vfwscanf_l(FILE * __restrict fp, locale_t loc, const wchar_t * __restrict fmt,
39 + NORMALIZE_LOCALE(loc);
42 + ret = __vfwscanf(fp, loc, fmt, ap);
46 @@ -124,8 +142,9 @@ vfwscanf(FILE * __restrict fp, const wch
48 * Non-MT-safe version.
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,
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;
74 + int mb_cur_max = MB_CUR_MAX_L(loc);
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 };
84 + va_copy(ap_orig, ap);
90 - while ((c = __fgetwc(fp)) != WEOF &&
92 + if (iswspace_l(c, loc)) {
93 + while ((c = __fgetwc(fp, loc)) != WEOF &&
98 + __ungetwc(c, fp, loc);
103 + if ((wi = __fgetwc(fp, loc)) == WEOF)
104 + goto input_failure;
110 @@ -179,16 +203,34 @@ __vfwscanf(FILE * __restrict fp, const w
114 + /* Consume leading white space */
116 + if ((wi = __fgetwc(fp, loc)) == WEOF)
117 + goto input_failure;
118 + if (!iswspace_l(wi, loc))
123 - if ((wi = __fgetwc(fp)) == WEOF)
124 - goto input_failure;
127 - goto input_failure;
128 + __ungetwc(wi, fp, loc);
129 + goto match_failure;
136 + if (index < 1 || index > NL_ARGMAX || fmt[-3] != '%') {
137 + goto input_failure;
141 + va_copy(ap, ap_orig); /* reset to %1$ */
142 + for (; index > 1; index--) {
149 @@ -307,7 +349,6 @@ literal:
154 if (flags & SUPPRESS) /* ??? */
156 if (flags & SHORTSHORT)
157 @@ -343,11 +384,11 @@ literal:
158 * that suppress this.
160 if ((flags & NOSKIP) == 0) {
161 - while ((wi = __fgetwc(fp)) != WEOF && iswspace(wi))
162 + while ((wi = __fgetwc(fp, loc)) != WEOF && iswspace_l(wi, loc))
167 + __ungetwc(wi, fp, loc);
171 @@ -364,7 +405,7 @@ literal:
172 p = va_arg(ap, wchar_t *);
174 while (width-- != 0 &&
175 - (wi = __fgetwc(fp)) != WEOF) {
176 + (wi = __fgetwc(fp, loc)) != WEOF) {
177 if (!(flags & SUPPRESS))
180 @@ -380,19 +421,19 @@ literal:
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)
194 - nconv = wcrtomb(mbbuf, wi,
196 + nconv = wcrtomb_l(mbbuf, wi,
198 if (nconv == (size_t)-1)
202 + __ungetwc(wi, fp, loc);
205 if (!(flags & SUPPRESS))
206 @@ -410,7 +451,6 @@ literal:
207 if (!(flags & SUPPRESS))
214 @@ -420,20 +460,20 @@ literal:
215 /* take only those things in the class */
216 if ((flags & SUPPRESS) && (flags & LONG)) {
218 - while ((wi = __fgetwc(fp)) != WEOF &&
219 + while ((wi = __fgetwc(fp, loc)) != WEOF &&
220 width-- != 0 && INCCL(wi))
224 + __ungetwc(wi, fp, loc);
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))
235 + __ungetwc(wi, fp, loc);
239 @@ -444,16 +484,16 @@ literal:
240 mbp = va_arg(ap, char *);
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)
254 - nconv = wcrtomb(mbbuf, wi,
256 + nconv = wcrtomb_l(mbbuf, wi,
258 if (nconv == (size_t)-1)
261 @@ -468,14 +508,15 @@ literal:
266 + __ungetwc(wi, fp, loc);
268 + goto match_failure;
269 if (!(flags & SUPPRESS)) {
279 @@ -483,39 +524,39 @@ literal:
282 if ((flags & SUPPRESS) && (flags & LONG)) {
283 - while ((wi = __fgetwc(fp)) != WEOF &&
284 + while ((wi = __fgetwc(fp, loc)) != WEOF &&
287 + !iswspace_l(wi, loc))
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 &&
298 + !iswspace_l(wi, loc)) {
304 + __ungetwc(wi, fp, loc);
308 if (!(flags & SUPPRESS))
309 mbp = va_arg(ap, char *);
311 - while ((wi = __fgetwc(fp)) != WEOF &&
312 + while ((wi = __fgetwc(fp, loc)) != WEOF &&
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)
324 - nconv = wcrtomb(mbbuf, wi,
326 + nconv = wcrtomb_l(mbbuf, wi,
328 if (nconv == (size_t)-1)
331 @@ -530,13 +571,12 @@ literal:
336 + __ungetwc(wi, fp, loc);
337 if (!(flags & SUPPRESS)) {
346 @@ -546,7 +586,7 @@ literal:
347 width = sizeof(buf) / sizeof(*buf) - 1;
348 flags |= SIGNOK | NDIGITS | NZDIGITS;
349 for (p = buf; width; width--) {
351 + c = __fgetwc(fp, loc);
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.
360 + __ungetwc(c, fp, loc);
364 @@ -646,22 +686,22 @@ literal:
366 if (flags & NDIGITS) {
368 - __ungetwc(*--p, fp);
369 + __ungetwc(*--p, fp, loc);
373 if (c == 'x' || c == 'X') {
376 + __ungetwc(c, fp, loc);
378 if ((flags & SUPPRESS) == 0) {
382 if ((flags & UNSIGNED) == 0)
383 - res = wcstoimax(buf, NULL, base);
384 + res = wcstoimax_l(buf, NULL, base, loc);
386 - res = wcstoumax(buf, NULL, base);
387 + res = wcstoumax_l(buf, NULL, base, loc);
389 *va_arg(ap, void **) =
390 (void *)(uintptr_t)res;
391 @@ -684,47 +724,47 @@ literal:
398 #ifndef NO_FLOATING_POINT
402 /* scan a floating point number as if by strtod */
403 - if (width == 0 || width > sizeof(buf) /
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)
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;
419 - float res = wcstof(buf, &p);
420 + float res = wcstof_l(pbuf, &p, loc);
421 *va_arg(ap, float *) = res;
423 - if (__scanfdebug && p - buf != width)
425 + if (__scanfdebug && p - pbuf != width)
426 + LIBC_ABORT("p - pbuf %ld != width %ld", (long)(p - pbuf), width);
433 #endif /* !NO_FLOATING_POINT */
437 - return (nconversions != 0 ? nassigned : EOF);
438 + return (nassigned ? nassigned : EOF);
443 #ifndef NO_FLOATING_POINT
444 +extern char *__parsefloat_buf(size_t s); /* see vfscanf-fbsd.c */
447 -parsefloat(FILE *fp, wchar_t *buf, wchar_t *end)
448 +parsefloat(FILE *fp, wchar_t **buf, size_t width, locale_t loc)
452 @@ -733,9 +773,19 @@ parsefloat(FILE *fp, wchar_t *buf, wchar
453 S_DIGITS, S_FRAC, S_EXP, S_EXPDIGITS
456 - wchar_t decpt = (wchar_t)(unsigned char)*localeconv()->decimal_point;
457 + char *decimal_point;
459 _Bool gotmantdig = 0, ishex = 0;
465 + s = (width == 0 ? BUF : (width + 1));
466 + if ((b = (wchar_t *)__parsefloat_buf(s * sizeof(wchar_t))) == NULL) {
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(...)".
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)
490 @@ -808,7 +860,7 @@ reswitch:
494 - } else if (!iswalnum(c) && c != '_')
495 + } else if (!iswalnum_l(c, loc) && c != '_')
499 @@ -824,7 +876,7 @@ reswitch:
503 - if ((ishex && iswxdigit(c)) || iswdigit(c))
504 + if ((ishex && iswxdigit_l(c, loc)) || iswdigit_l(c, loc))
508 @@ -841,7 +893,7 @@ reswitch:
512 - } else if ((ishex && iswxdigit(c)) || iswdigit(c)) {
513 + } else if ((ishex && iswxdigit_l(c, loc)) || iswdigit_l(c, loc)) {
517 @@ -854,13 +906,26 @@ reswitch:
522 + if (iswdigit_l(c, loc))
529 + LIBC_ABORT("unknown state %d", state);
532 + ssize_t diff = (p - b);
533 + ssize_t com = (commit - b);
535 + b = (wchar_t *)__parsefloat_buf(s * sizeof(wchar_t));
546 @@ -868,10 +933,11 @@ reswitch:
551 + __ungetwc(c, fp, loc);
554 + __ungetwc(*p, fp, loc);
556 - return (commit - buf);
558 + return (commit - b);