]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Stdio/vfwscanf.c
Add Socket Libraries.
[mirror_edk2.git] / StdLib / LibC / Stdio / vfwscanf.c
1 /*-
2 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
3 This program and the accompanying materials are licensed and made available under
4 the terms and conditions of the BSD License that accompanies this distribution.
5 The full text of the license may be found at
6 http://opensource.org/licenses/bsd-license.
7
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
10
11 * Copyright (c) 1990, 1993
12 * The Regents of the University of California. All rights reserved.
13 *
14 * This code is derived from software contributed to Berkeley by
15 * Chris Torek.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement:
27 * This product includes software developed by the University of
28 * California, Berkeley and its contributors.
29 * 4. Neither the name of the University nor the names of its contributors
30 * may be used to endorse or promote products derived from this software
31 * without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * SUCH DAMAGE.
44
45 FreeBSD: src/lib/libc/stdio/vfwscanf.c,v 1.12 2004/05/02 20:13:29 obrien Exp
46 NetBSD: vfwscanf.c,v 1.2 2005/06/12 05:48:41 lukem Exp
47 */
48 #include <LibConfig.h>
49 #include <sys/EfiCdefs.h>
50
51 #include "namespace.h"
52 #include <ctype.h>
53 #include <inttypes.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <stddef.h>
57 #include <stdarg.h>
58 #include <string.h>
59 #include <limits.h>
60 #include <wchar.h>
61 #include <wctype.h>
62
63 #include "reentrant.h"
64 #include "local.h"
65
66 #ifndef NO_FLOATING_POINT
67 #include <locale.h>
68 #endif
69
70 #if defined(_MSC_VER) /* Handle Microsoft VC++ compiler specifics. */
71 #pragma warning ( disable : 4244 ) // Allow wint_t to wchar_t conversions
72 #pragma warning ( disable : 4305 ) // Allow truncation from UINT64 to void*
73 #pragma warning ( disable : 4701 ) // Disable false warning for local variable p near line 375
74 #endif
75
76
77 #define BUF 513 /* Maximum length of numeric string. */
78
79 /*
80 * Flags used during conversion.
81 */
82 #define LONG 0x01 /* l: long or double */
83 #define LONGDBL 0x02 /* L: long double */
84 #define SHORT 0x04 /* h: short */
85 #define SUPPRESS 0x08 /* *: suppress assignment */
86 #define POINTER 0x10 /* p: void * (as hex) */
87 #define NOSKIP 0x20 /* [ or c: do not skip blanks */
88 #define LONGLONG 0x400 /* ll: quad_t (+ deprecated q: quad) */
89 #define INTMAXT 0x800 /* j: intmax_t */
90 #define PTRDIFFT 0x1000 /* t: ptrdiff_t */
91 #define SIZET 0x2000 /* z: size_t */
92 #define SHORTSHORT 0x4000 /* hh: char */
93 #define UNSIGNED 0x8000 /* %[oupxX] conversions */
94
95 /*
96 * The following are used in integral conversions only:
97 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
98 */
99 #define SIGNOK 0x40 /* +/- is (still) legal */
100 #define NDIGITS 0x80 /* no digits detected */
101 #define PFXOK 0x100 /* 0x prefix is (still) legal */
102 #define NZDIGITS 0x200 /* no zero digits detected */
103 #define HAVESIGN 0x10000 /* sign detected */
104
105 /*
106 * Conversion types.
107 */
108 #define CT_CHAR 0 /* %c conversion */
109 #define CT_CCL 1 /* %[...] conversion */
110 #define CT_STRING 2 /* %s conversion */
111 #define CT_INT 3 /* %[dioupxX] conversion */
112 #define CT_FLOAT 4 /* %[efgEFG] conversion */
113
114 static int parsefloat(FILE *, wchar_t *, wchar_t *);
115
116 #define INCCL(_c) \
117 (cclcompl ? (wmemchr(ccls, (_c), (size_t)(ccle - ccls)) == NULL) : \
118 (wmemchr(ccls, (_c), (size_t)(ccle - ccls)) != NULL))
119
120 /*
121 * MT-safe version.
122 */
123 int
124 vfwscanf(FILE * __restrict fp, const wchar_t * __restrict fmt, va_list ap)
125 {
126 int ret;
127
128 FLOCKFILE(fp);
129 _SET_ORIENTATION(fp, 1);
130 ret = __vfwscanf_unlocked(fp, fmt, ap);
131 FUNLOCKFILE(fp);
132 return (ret);
133 }
134
135 /*
136 * Non-MT-safe version.
137 */
138 int
139 __vfwscanf_unlocked(FILE * __restrict fp, const wchar_t * __restrict fmt, va_list ap)
140 {
141 wint_t c; /* character from format, or conversion */
142 size_t width; /* field width, or 0 */
143 wchar_t *p; /* points into all kinds of strings */
144 int n; /* handy integer */
145 int flags; /* flags as defined above */
146 wchar_t *p0; /* saves original value of p when necessary */
147 int nassigned; /* number of fields assigned */
148 int nconversions; /* number of conversions */
149 int nread; /* number of characters consumed from fp */
150 int base; /* base argument to conversion function */
151 wchar_t buf[BUF]; /* buffer for numeric conversions */
152 const wchar_t *ccls; /* character class start */
153 const wchar_t *ccle; /* character class end */
154 int cclcompl; /* ccl is complemented? */
155 wint_t wi; /* handy wint_t */
156 char *mbp; /* multibyte string pointer for %c %s %[ */
157 size_t nconv; /* number of bytes in mb. conversion */
158 char mbbuf[MB_LEN_MAX]; /* temporary mb. character buffer */
159 static const mbstate_t initial = { 0 };
160 mbstate_t mbs;
161
162 /* `basefix' is used to avoid `if' tests in the integer scanner */
163 static short basefix[17] =
164 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
165
166 nassigned = 0;
167 nconversions = 0;
168 nread = 0;
169 ccls = ccle = NULL;
170 base = 0;
171 cclcompl = 0;
172 mbp = NULL;
173 for (;;) {
174 c = *fmt++;
175 if (c == 0)
176 return (nassigned);
177 if (iswspace(c)) {
178 while ((c = __fgetwc_unlock(fp)) != WEOF &&
179 iswspace(c))
180 ;
181 if (c != WEOF)
182 ungetwc(c, fp);
183 continue;
184 }
185 if (c != '%')
186 goto literal;
187 width = 0;
188 flags = 0;
189 /*
190 * switch on the format. continue if done;
191 * break once format type is derived.
192 */
193 again: c = *fmt++;
194 switch (c) {
195 case '%':
196 literal:
197 if ((wi = __fgetwc_unlock(fp)) == WEOF)
198 goto input_failure;
199 if (wi != c) {
200 ungetwc(wi, fp);
201 goto match_failure;
202 }
203 nread++;
204 continue;
205
206 case '*':
207 flags |= SUPPRESS;
208 goto again;
209 case 'j':
210 flags |= INTMAXT;
211 goto again;
212 case 'l':
213 if (flags & LONG) {
214 flags &= ~LONG;
215 flags |= LONGLONG;
216 } else
217 flags |= LONG;
218 goto again;
219 case 'q':
220 flags |= LONGLONG; /* not quite */
221 goto again;
222 case 't':
223 flags |= PTRDIFFT;
224 goto again;
225 case 'z':
226 flags |= SIZET;
227 goto again;
228 case 'L':
229 flags |= LONGDBL;
230 goto again;
231 case 'h':
232 if (flags & SHORT) {
233 flags &= ~SHORT;
234 flags |= SHORTSHORT;
235 } else
236 flags |= SHORT;
237 goto again;
238
239 case '0': case '1': case '2': case '3': case '4':
240 case '5': case '6': case '7': case '8': case '9':
241 width = width * 10 + c - '0';
242 goto again;
243
244 /*
245 * Conversions.
246 */
247 case 'd':
248 c = CT_INT;
249 base = 10;
250 break;
251
252 case 'i':
253 c = CT_INT;
254 base = 0;
255 break;
256
257 case 'o':
258 c = CT_INT;
259 flags |= UNSIGNED;
260 base = 8;
261 break;
262
263 case 'u':
264 c = CT_INT;
265 flags |= UNSIGNED;
266 base = 10;
267 break;
268
269 case 'X':
270 case 'x':
271 flags |= PFXOK; /* enable 0x prefixing */
272 c = CT_INT;
273 flags |= UNSIGNED;
274 base = 16;
275 break;
276
277 #ifndef NO_FLOATING_POINT
278 //case 'A':
279 case 'E': case 'F': case 'G':
280 //case 'a':
281 case 'e': case 'f': case 'g':
282 c = CT_FLOAT;
283 break;
284 #endif
285
286 case 'S':
287 flags |= LONG;
288 /* FALLTHROUGH */
289 case 's':
290 c = CT_STRING;
291 break;
292
293 case '[':
294 ccls = fmt;
295 if (*fmt == '^') {
296 cclcompl = 1;
297 fmt++;
298 } else
299 cclcompl = 0;
300 if (*fmt == ']')
301 fmt++;
302 while (*fmt != '\0' && *fmt != ']')
303 fmt++;
304 ccle = fmt;
305 fmt++;
306 flags |= NOSKIP;
307 c = CT_CCL;
308 break;
309
310 case 'C':
311 flags |= LONG;
312 /* FALLTHROUGH */
313 case 'c':
314 flags |= NOSKIP;
315 c = CT_CHAR;
316 break;
317
318 case 'p': /* pointer format is like hex */
319 flags |= POINTER | PFXOK;
320 c = CT_INT; /* assumes sizeof(uintmax_t) */
321 flags |= UNSIGNED; /* >= sizeof(uintptr_t) */
322 base = 16;
323 break;
324
325 case 'n':
326 nconversions++;
327 if (flags & SUPPRESS) /* ??? */
328 continue;
329 if (flags & SHORTSHORT)
330 *va_arg(ap, char *) = (char)nread;
331 else if (flags & SHORT)
332 *va_arg(ap, short *) = (short)nread;
333 else if (flags & LONG)
334 *va_arg(ap, long *) = (long)nread;
335 else if (flags & LONGLONG)
336 *va_arg(ap, INT64 *) = (INT64)nread; // was quad_t
337 else if (flags & INTMAXT)
338 *va_arg(ap, intmax_t *) = (intmax_t)nread;
339 else if (flags & SIZET)
340 *va_arg(ap, size_t *) = (size_t)nread;
341 else if (flags & PTRDIFFT)
342 *va_arg(ap, ptrdiff_t *) = (ptrdiff_t)nread;
343 else
344 *va_arg(ap, int *) = nread;
345 continue;
346
347 default:
348 goto match_failure;
349
350 /*
351 * Disgusting backwards compatibility hack. XXX
352 */
353 case '\0': /* compat */
354 return (EOF);
355 }
356
357 /*
358 * Consume leading white space, except for formats
359 * that suppress this.
360 */
361 if ((flags & NOSKIP) == 0) {
362 while ((wi = __fgetwc_unlock(fp)) != WEOF && iswspace(wi))
363 nread++;
364 if (wi == WEOF)
365 goto input_failure;
366 ungetwc(wi, fp);
367 }
368
369 /*
370 * Do the conversion.
371 */
372 switch (c) {
373
374 case CT_CHAR:
375 /* scan arbitrary characters (sets NOSKIP) */
376 if (width == 0)
377 width = 1;
378 if (flags & LONG) {
379 if (!(flags & SUPPRESS))
380 p = va_arg(ap, wchar_t *);
381 n = 0;
382 while (width-- != 0 &&
383 (wi = __fgetwc_unlock(fp)) != WEOF) {
384 if (!(flags & SUPPRESS))
385 *p++ = (wchar_t)wi;
386 n++;
387 }
388 if (n == 0)
389 goto input_failure;
390 nread += n;
391 if (!(flags & SUPPRESS))
392 nassigned++;
393 } else {
394 if (!(flags & SUPPRESS))
395 mbp = va_arg(ap, char *);
396 n = 0;
397 mbs = initial;
398 while (width != 0 &&
399 (wi = __fgetwc_unlock(fp)) != WEOF) {
400 if (width >= MB_CUR_MAX &&
401 !(flags & SUPPRESS)) {
402 nconv = wcrtomb(mbp, (wchar_t)wi, &mbs);
403 if (nconv == (size_t)-1)
404 goto input_failure;
405 } else {
406 nconv = wcrtomb(mbbuf, (wchar_t)wi,
407 &mbs);
408 if (nconv == (size_t)-1)
409 goto input_failure;
410 if (nconv > width) {
411 ungetwc(wi, fp);
412 break;
413 }
414 if (!(flags & SUPPRESS))
415 memcpy(mbp, mbbuf,
416 nconv);
417 }
418 if (!(flags & SUPPRESS))
419 mbp += nconv;
420 width -= nconv;
421 n++;
422 }
423 if (n == 0)
424 goto input_failure;
425 nread += n;
426 if (!(flags & SUPPRESS))
427 nassigned++;
428 }
429 nconversions++;
430 break;
431
432 case CT_CCL:
433 /* scan a (nonempty) character class (sets NOSKIP) */
434 if (width == 0)
435 width = (size_t)~0; /* `infinity' */
436 /* take only those things in the class */
437 if ((flags & SUPPRESS) && (flags & LONG)) {
438 n = 0;
439 while ((wi = __fgetwc_unlock(fp)) != WEOF &&
440 width-- != 0 && INCCL(wi))
441 n++;
442 if (wi != WEOF)
443 ungetwc(wi, fp);
444 if (n == 0)
445 goto match_failure;
446 } else if (flags & LONG) {
447 p0 = p = va_arg(ap, wchar_t *);
448 while ((wi = __fgetwc_unlock(fp)) != WEOF &&
449 width-- != 0 && INCCL(wi))
450 *p++ = (wchar_t)wi;
451 if (wi != WEOF)
452 ungetwc(wi, fp);
453 n = p - p0;
454 if (n == 0)
455 goto match_failure;
456 *p = 0;
457 nassigned++;
458 } else {
459 if (!(flags & SUPPRESS))
460 mbp = va_arg(ap, char *);
461 n = 0;
462 mbs = initial;
463 while ((wi = __fgetwc_unlock(fp)) != WEOF &&
464 width != 0 && INCCL(wi)) {
465 if (width >= MB_CUR_MAX &&
466 !(flags & SUPPRESS)) {
467 nconv = wcrtomb(mbp, wi, &mbs);
468 if (nconv == (size_t)-1)
469 goto input_failure;
470 } else {
471 nconv = wcrtomb(mbbuf, wi,
472 &mbs);
473 if (nconv == (size_t)-1)
474 goto input_failure;
475 if (nconv > width)
476 break;
477 if (!(flags & SUPPRESS))
478 memcpy(mbp, mbbuf,
479 nconv);
480 }
481 if (!(flags & SUPPRESS))
482 mbp += nconv;
483 width -= nconv;
484 n++;
485 }
486 if (wi != WEOF)
487 ungetwc(wi, fp);
488 if (!(flags & SUPPRESS)) {
489 *mbp = 0;
490 nassigned++;
491 }
492 }
493 nread += n;
494 nconversions++;
495 break;
496
497 case CT_STRING:
498 /* like CCL, but zero-length string OK, & no NOSKIP */
499 if (width == 0)
500 width = (size_t)~0;
501 if ((flags & SUPPRESS) && (flags & LONG)) {
502 while ((wi = __fgetwc_unlock(fp)) != WEOF &&
503 width-- != 0 &&
504 !iswspace(wi))
505 nread++;
506 if (wi != WEOF)
507 ungetwc(wi, fp);
508 } else if (flags & LONG) {
509 p0 = p = va_arg(ap, wchar_t *);
510 while ((wi = __fgetwc_unlock(fp)) != WEOF &&
511 width-- != 0 &&
512 !iswspace(wi)) {
513 *p++ = (wchar_t)wi;
514 nread++;
515 }
516 if (wi != WEOF)
517 ungetwc(wi, fp);
518 *p = '\0';
519 nassigned++;
520 } else {
521 if (!(flags & SUPPRESS))
522 mbp = va_arg(ap, char *);
523 mbs = initial;
524 while ((wi = __fgetwc_unlock(fp)) != WEOF &&
525 width != 0 &&
526 !iswspace(wi)) {
527 if (width >= MB_CUR_MAX &&
528 !(flags & SUPPRESS)) {
529 nconv = wcrtomb(mbp, wi, &mbs);
530 if (nconv == (size_t)-1)
531 goto input_failure;
532 } else {
533 nconv = wcrtomb(mbbuf, wi,
534 &mbs);
535 if (nconv == (size_t)-1)
536 goto input_failure;
537 if (nconv > width)
538 break;
539 if (!(flags & SUPPRESS))
540 memcpy(mbp, mbbuf,
541 nconv);
542 }
543 if (!(flags & SUPPRESS))
544 mbp += nconv;
545 width -= nconv;
546 nread++;
547 }
548 if (wi != WEOF)
549 ungetwc(wi, fp);
550 if (!(flags & SUPPRESS)) {
551 *mbp = 0;
552 nassigned++;
553 }
554 }
555 nconversions++;
556 continue;
557
558 case CT_INT:
559 /* scan an integer as if by the conversion function */
560 if (width == 0 || width > sizeof(buf) /
561 sizeof(*buf) - 1)
562 width = sizeof(buf) / sizeof(*buf) - 1;
563 flags |= SIGNOK | NDIGITS | NZDIGITS;
564 for (p = buf; width; width--) {
565 c = __fgetwc_unlock(fp);
566 /*
567 * Switch on the character; `goto ok'
568 * if we accept it as a part of number.
569 */
570 switch (c) {
571
572 /*
573 * The digit 0 is always legal, but is
574 * special. For %i conversions, if no
575 * digits (zero or nonzero) have been
576 * scanned (only signs), we will have
577 * base==0. In that case, we should set
578 * it to 8 and enable 0x prefixing.
579 * Also, if we have not scanned zero digits
580 * before this, do not turn off prefixing
581 * (someone else will turn it off if we
582 * have scanned any nonzero digits).
583 */
584 case '0':
585 if (base == 0) {
586 base = 8;
587 flags |= PFXOK;
588 }
589 if (flags & NZDIGITS)
590 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
591 else
592 flags &= ~(SIGNOK|PFXOK|NDIGITS);
593 goto ok;
594
595 /* 1 through 7 always legal */
596 case '1': case '2': case '3':
597 case '4': case '5': case '6': case '7':
598 base = basefix[base];
599 flags &= ~(SIGNOK | PFXOK | NDIGITS);
600 goto ok;
601
602 /* digits 8 and 9 ok iff decimal or hex */
603 case '8': case '9':
604 base = basefix[base];
605 if (base <= 8)
606 break; /* not legal here */
607 flags &= ~(SIGNOK | PFXOK | NDIGITS);
608 goto ok;
609
610 /* letters ok iff hex */
611 case 'A': case 'B': case 'C':
612 case 'D': case 'E': case 'F':
613 case 'a': case 'b': case 'c':
614 case 'd': case 'e': case 'f':
615 /* no need to fix base here */
616 if (base <= 10)
617 break; /* not legal here */
618 flags &= ~(SIGNOK | PFXOK | NDIGITS);
619 goto ok;
620
621 /* sign ok only as first character */
622 case '+': case '-':
623 if (flags & SIGNOK) {
624 flags &= ~SIGNOK;
625 flags |= HAVESIGN;
626 goto ok;
627 }
628 break;
629
630 /*
631 * x ok iff flag still set & 2nd char (or
632 * 3rd char if we have a sign).
633 */
634 case 'x': case 'X':
635 if (flags & PFXOK && p ==
636 buf + 1 + !!(flags & HAVESIGN)) {
637 base = 16; /* if %i */
638 flags &= ~PFXOK;
639 goto ok;
640 }
641 break;
642 }
643
644 /*
645 * If we got here, c is not a legal character
646 * for a number. Stop accumulating digits.
647 */
648 if (c != WEOF)
649 ungetwc(c, fp);
650 break;
651 ok:
652 /*
653 * c is legal: store it and look at the next.
654 */
655 *p++ = (wchar_t)c;
656 }
657 /*
658 * If we had only a sign, it is no good; push
659 * back the sign. If the number ends in `x',
660 * it was [sign] '0' 'x', so push back the x
661 * and treat it as [sign] '0'.
662 */
663 if (flags & NDIGITS) {
664 if (p > buf)
665 ungetwc(*--p, fp);
666 goto match_failure;
667 }
668 c = p[-1];
669 if (c == 'x' || c == 'X') {
670 --p;
671 ungetwc(c, fp);
672 }
673 if ((flags & SUPPRESS) == 0) {
674 uintmax_t res;
675
676 *p = 0;
677 if ((flags & UNSIGNED) == 0)
678 res = wcstoimax(buf, NULL, base);
679 else
680 res = wcstoumax(buf, NULL, base);
681
682 if (flags & POINTER) {
683 *va_arg(ap, void **) = (void *)res;
684 }
685 else if (flags & SHORTSHORT) {
686 *va_arg(ap, char *) = (char)res;
687 }
688 else if (flags & SHORT) {
689 *va_arg(ap, short *) = (short)res;
690 }
691 else if (flags & LONG) {
692 *va_arg(ap, long *) = (long)res;
693 }
694 else if (flags & LONGLONG) {
695 *va_arg(ap, INT64 *) = res; // was quad_t
696 }
697 else if (flags & INTMAXT) {
698 *va_arg(ap, intmax_t *) = res;
699 }
700 else if (flags & PTRDIFFT) {
701 *va_arg(ap, ptrdiff_t *) = (ptrdiff_t)res;
702 }
703 else if (flags & SIZET) {
704 *va_arg(ap, size_t *) = (size_t)res;
705 }
706 else {
707 *va_arg(ap, int *) = (int)res;
708 }
709 nassigned++;
710 }
711 nread += p - buf;
712 nconversions++;
713 break;
714
715 #ifndef NO_FLOATING_POINT
716 case CT_FLOAT:
717 /* scan a floating point number as if by strtod */
718 if (width == 0 || width > sizeof(buf) /
719 sizeof(*buf) - 1)
720 width = sizeof(buf) / sizeof(*buf) - 1;
721 if ((width = parsefloat(fp, buf, buf + width)) == 0)
722 goto match_failure;
723 if ((flags & SUPPRESS) == 0) {
724 #ifdef REAL_LONG_DOUBLE_SUPPORT
725 if (flags & LONGDBL) {
726 long double res = wcstold(buf, &p);
727 *va_arg(ap, long double *) = res;
728 } else
729 #endif
730 if (flags & (LONG | LONGDBL)) {
731 double res = wcstod(buf, &p);
732 *va_arg(ap, double *) = res;
733 }
734 else {
735 float res = wcstof(buf, &p);
736 *va_arg(ap, float *) = res;
737 }
738 #ifdef DEBUG
739 if (p - buf != width)
740 abort();
741 #endif
742 nassigned++;
743 }
744 nread += (int)width;
745 nconversions++;
746 break;
747 #endif /* !NO_FLOATING_POINT */
748 }
749 }
750 input_failure:
751 return (nconversions != 0 ? nassigned : EOF);
752 match_failure:
753 return (nassigned);
754 }
755
756 #ifndef NO_FLOATING_POINT
757 static int
758 parsefloat(FILE *fp, wchar_t *buf, wchar_t *end)
759 {
760 wchar_t *commit, *p;
761 int infnanpos = 0;
762 enum {
763 S_START, S_GOTSIGN, S_INF, S_NAN, S_MAYBEHEX,
764 S_DIGITS, S_FRAC, S_EXP, S_EXPDIGITS
765 } state = S_START;
766 wchar_t c;
767 wchar_t decpt = (wchar_t)(unsigned char)*localeconv()->decimal_point;
768 int gotmantdig = 0, ishex = 0;
769
770 /*
771 * We set commit = p whenever the string we have read so far
772 * constitutes a valid representation of a floating point
773 * number by itself. At some point, the parse will complete
774 * or fail, and we will ungetc() back to the last commit point.
775 * To ensure that the file offset gets updated properly, it is
776 * always necessary to read at least one character that doesn't
777 * match; thus, we can't short-circuit "infinity" or "nan(...)".
778 */
779 commit = buf - 1;
780 c = (wchar_t)WEOF;
781 for (p = buf; p < end; ) {
782 if ((wint_t)(c = __fgetwc_unlock(fp)) == WEOF)
783 break;
784 reswitch:
785 switch (state) {
786 case S_START:
787 state = S_GOTSIGN;
788 if (c == '-' || c == '+')
789 break;
790 else
791 goto reswitch;
792 case S_GOTSIGN:
793 switch (c) {
794 case '0':
795 state = S_MAYBEHEX;
796 commit = p;
797 break;
798 case 'I':
799 case 'i':
800 state = S_INF;
801 break;
802 case 'N':
803 case 'n':
804 state = S_NAN;
805 break;
806 default:
807 state = S_DIGITS;
808 goto reswitch;
809 }
810 break;
811 case S_INF:
812 if (infnanpos > 6 ||
813 (c != "nfinity"[infnanpos] &&
814 c != "NFINITY"[infnanpos]))
815 goto parsedone;
816 if (infnanpos == 1 || infnanpos == 6)
817 commit = p; /* inf or infinity */
818 infnanpos++;
819 break;
820 case S_NAN:
821 switch (infnanpos) {
822 case -1: /* XXX kludge to deal with nan(...) */
823 goto parsedone;
824 case 0:
825 if (c != 'A' && c != 'a')
826 goto parsedone;
827 break;
828 case 1:
829 if (c != 'N' && c != 'n')
830 goto parsedone;
831 else
832 commit = p;
833 break;
834 case 2:
835 if (c != '(')
836 goto parsedone;
837 break;
838 default:
839 if (c == ')') {
840 commit = p;
841 infnanpos = -2;
842 } else if (!iswalnum(c) && c != '_')
843 goto parsedone;
844 break;
845 }
846 infnanpos++;
847 break;
848 case S_MAYBEHEX:
849 state = S_DIGITS;
850 if (c == 'X' || c == 'x') {
851 ishex = 1;
852 break;
853 } else { /* we saw a '0', but no 'x' */
854 gotmantdig = 1;
855 goto reswitch;
856 }
857 case S_DIGITS:
858 if ((ishex && iswxdigit(c)) || iswdigit(c))
859 gotmantdig = 1;
860 else {
861 state = S_FRAC;
862 if (c != decpt)
863 goto reswitch;
864 }
865 if (gotmantdig)
866 commit = p;
867 break;
868 case S_FRAC:
869 if (((c == 'E' || c == 'e') && !ishex) ||
870 ((c == 'P' || c == 'p') && ishex)) {
871 if (!gotmantdig)
872 goto parsedone;
873 else
874 state = S_EXP;
875 } else if ((ishex && iswxdigit(c)) || iswdigit(c)) {
876 commit = p;
877 gotmantdig = 1;
878 } else
879 goto parsedone;
880 break;
881 case S_EXP:
882 state = S_EXPDIGITS;
883 if (c == '-' || c == '+')
884 break;
885 else
886 goto reswitch;
887 case S_EXPDIGITS:
888 if (iswdigit(c))
889 commit = p;
890 else
891 goto parsedone;
892 break;
893 default:
894 abort();
895 }
896 *p++ = c;
897 c = (wchar_t)WEOF;
898 }
899
900 parsedone:
901 if ((wint_t)c != WEOF)
902 ungetwc(c, fp);
903 while (commit < --p)
904 ungetwc(*p, fp);
905 *++commit = '\0';
906 return (commit - buf);
907 }
908 #endif