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