]> git.saurik.com Git - apple/libc.git/blob - stdio/FreeBSD/vfscanf.c
4f1c3c799fab4f9605060fe16cf9828d9739fcd8
[apple/libc.git] / stdio / FreeBSD / vfscanf.c
1 /*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char sccsid[] = "@(#)vfscanf.c 8.1 (Berkeley) 6/4/93";
35 #endif /* LIBC_SCCS and not lint */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfscanf.c,v 1.43 2009/01/19 06:19:51 das Exp $");
38
39 #include "xlocale_private.h"
40
41 #include "namespace.h"
42 #include <ctype.h>
43 #include <inttypes.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <stddef.h>
47 #include <stdarg.h>
48 #include <string.h>
49 #include <wchar.h>
50 #include <wctype.h>
51 #include <pthread.h>
52 #include "un-namespace.h"
53
54 #include "collate.h"
55 #include "libc_private.h"
56 #include "local.h"
57
58 #ifndef NO_FLOATING_POINT
59 #include <locale.h>
60 #endif
61
62 #define BUF 513 /* Maximum length of numeric string. */
63
64 /*
65 * Flags used during conversion.
66 */
67 #define LONG 0x01 /* l: long or double */
68 #define LONGDBL 0x02 /* L: long double */
69 #define SHORT 0x04 /* h: short */
70 #define SUPPRESS 0x08 /* *: suppress assignment */
71 #define POINTER 0x10 /* p: void * (as hex) */
72 #define NOSKIP 0x20 /* [ or c: do not skip blanks */
73 #define LONGLONG 0x400 /* ll: long long (+ deprecated q: quad) */
74 #define INTMAXT 0x800 /* j: intmax_t */
75 #define PTRDIFFT 0x1000 /* t: ptrdiff_t */
76 #define SIZET 0x2000 /* z: size_t */
77 #define SHORTSHORT 0x4000 /* hh: char */
78 #define UNSIGNED 0x8000 /* %[oupxX] conversions */
79
80 /*
81 * The following are used in integral conversions only:
82 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
83 */
84 #define SIGNOK 0x40 /* +/- is (still) legal */
85 #define NDIGITS 0x80 /* no digits detected */
86 #define PFXOK 0x100 /* 0x prefix is (still) legal */
87 #define NZDIGITS 0x200 /* no zero digits detected */
88 #define HAVESIGN 0x10000 /* sign detected */
89
90 /*
91 * Conversion types.
92 */
93 #define CT_CHAR 0 /* %c conversion */
94 #define CT_CCL 1 /* %[...] conversion */
95 #define CT_STRING 2 /* %s conversion */
96 #define CT_INT 3 /* %[dioupxX] conversion */
97 #define CT_FLOAT 4 /* %[efgEFG] conversion */
98
99 static const u_char *__sccl(char *, const u_char *, locale_t);
100 #ifndef NO_FLOATING_POINT
101 static int parsefloat(FILE *, char **, size_t, locale_t);
102 #endif
103
104 __weak_reference(__vfscanf, vfscanf);
105
106 /*
107 * __vfscanf - MT-safe version
108 */
109 int
110 __vfscanf(FILE * __restrict fp, char const * __restrict fmt0, va_list ap)
111 {
112 int ret;
113
114 FLOCKFILE(fp);
115 ret = __svfscanf_l(fp, __current_locale(), fmt0, ap);
116 FUNLOCKFILE(fp);
117 return (ret);
118 }
119
120 int
121 vfscanf_l(FILE * __restrict fp, locale_t loc, char const * __restrict fmt0, va_list ap)
122 {
123 int ret;
124
125 NORMALIZE_LOCALE(loc);
126 FLOCKFILE(fp);
127 ret = __svfscanf_l(fp, loc, fmt0, ap);
128 FUNLOCKFILE(fp);
129 return (ret);
130 }
131
132 /*
133 * __svfscanf - non-MT-safe version of __vfscanf
134 */
135 __private_extern__ int
136 __svfscanf_l(FILE * __restrict fp, locale_t loc, const char * __restrict fmt0, va_list ap)
137 {
138 const u_char *fmt = (const u_char *)fmt0;
139 int c; /* character from format, or conversion */
140 size_t width; /* field width, or 0 */
141 char *p; /* points into all kinds of strings */
142 int n; /* handy integer */
143 int flags; /* flags as defined above */
144 char *p0; /* saves original value of p when necessary */
145 int nassigned; /* number of fields assigned */
146 int nread; /* number of characters consumed from fp */
147 int base; /* base argument to conversion function */
148 char ccltab[256]; /* character class table for %[...] */
149 char buf[BUF]; /* buffer for numeric and mb conversions */
150 wchar_t *wcp; /* handy wide character pointer */
151 size_t nconv; /* length of multibyte sequence converted */
152 int index; /* %index$, zero if unset */
153 va_list ap_orig; /* to reset ap to first argument */
154 static const mbstate_t initial;
155 mbstate_t mbs;
156 int mb_cur_max;
157
158 /* `basefix' is used to avoid `if' tests in the integer scanner */
159 static const short basefix[17] =
160 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
161
162 NORMALIZE_LOCALE(loc);
163 mb_cur_max = MB_CUR_MAX_L(loc);
164 ORIENT(fp, -1);
165
166 nassigned = 0;
167 nread = 0;
168 va_copy(ap_orig, ap);
169 for (;;) {
170 c = *fmt++;
171 if (c == 0)
172 return (nassigned);
173 if (isspace_l(c, loc)) {
174 while ((fp->_r > 0 || __srefill(fp) == 0) && isspace_l(*fp->_p, loc))
175 nread++, fp->_r--, fp->_p++;
176 continue;
177 }
178 if (c != '%') {
179 if (fp->_r <= 0 && __srefill(fp))
180 goto input_failure;
181 goto literal;
182 }
183 width = 0;
184 flags = 0;
185 /*
186 * switch on the format. continue if done;
187 * break once format type is derived.
188 */
189 again: c = *fmt++;
190 switch (c) {
191 case '%':
192 /* Consume leading white space */
193 for(;;) {
194 if (fp->_r <= 0 && __srefill(fp))
195 goto input_failure;
196 if (!isspace_l(*fp->_p, loc))
197 break;
198 nread++;
199 fp->_r--;
200 fp->_p++;
201 }
202 literal:
203 if (*fp->_p != c)
204 goto match_failure;
205 fp->_r--, fp->_p++;
206 nread++;
207 continue;
208
209 case '$':
210 index = width;
211 if (index < 1 || index > NL_ARGMAX || fmt[-3] != '%') {
212 goto input_failure;
213 }
214 width = 0;
215 va_end(ap);
216 va_copy(ap, ap_orig); /* reset to %1$ */
217 for (; index > 1; index--) {
218 va_arg(ap, void*);
219 }
220 goto again;
221 case '*':
222 flags |= SUPPRESS;
223 goto again;
224 case 'j':
225 flags |= INTMAXT;
226 goto again;
227 case 'l':
228 if (flags & LONG) {
229 flags &= ~LONG;
230 flags |= LONGLONG;
231 } else
232 flags |= LONG;
233 goto again;
234 case 'q':
235 flags |= LONGLONG; /* not quite */
236 goto again;
237 case 't':
238 flags |= PTRDIFFT;
239 goto again;
240 case 'z':
241 flags |= SIZET;
242 goto again;
243 case 'L':
244 flags |= LONGDBL;
245 goto again;
246 case 'h':
247 if (flags & SHORT) {
248 flags &= ~SHORT;
249 flags |= SHORTSHORT;
250 } else
251 flags |= SHORT;
252 goto again;
253
254 case '0': case '1': case '2': case '3': case '4':
255 case '5': case '6': case '7': case '8': case '9':
256 width = width * 10 + c - '0';
257 goto again;
258
259 /*
260 * Conversions.
261 */
262 case 'd':
263 c = CT_INT;
264 base = 10;
265 break;
266
267 case 'i':
268 c = CT_INT;
269 base = 0;
270 break;
271
272 case 'o':
273 c = CT_INT;
274 flags |= UNSIGNED;
275 base = 8;
276 break;
277
278 case 'u':
279 c = CT_INT;
280 flags |= UNSIGNED;
281 base = 10;
282 break;
283
284 case 'X':
285 case 'x':
286 flags |= PFXOK; /* enable 0x prefixing */
287 c = CT_INT;
288 flags |= UNSIGNED;
289 base = 16;
290 break;
291
292 #ifndef NO_FLOATING_POINT
293 case 'A': case 'E': case 'F': case 'G':
294 case 'a': case 'e': case 'f': case 'g':
295 c = CT_FLOAT;
296 break;
297 #endif
298
299 case 'S':
300 flags |= LONG;
301 /* FALLTHROUGH */
302 case 's':
303 c = CT_STRING;
304 break;
305
306 case '[':
307 fmt = __sccl(ccltab, fmt, loc);
308 flags |= NOSKIP;
309 c = CT_CCL;
310 break;
311
312 case 'C':
313 flags |= LONG;
314 /* FALLTHROUGH */
315 case 'c':
316 flags |= NOSKIP;
317 c = CT_CHAR;
318 break;
319
320 case 'p': /* pointer format is like hex */
321 flags |= POINTER | PFXOK;
322 c = CT_INT; /* assumes sizeof(uintmax_t) */
323 flags |= UNSIGNED; /* >= sizeof(uintptr_t) */
324 base = 16;
325 break;
326
327 case 'n':
328 {
329 void *ptr = va_arg(ap, void *);
330 if ((ptr == NULL) || (flags & SUPPRESS)) /* ??? */
331 continue;
332 else if (flags & SHORTSHORT)
333 *(char *)ptr = nread;
334 else if (flags & SHORT)
335 *(short *)ptr = nread;
336 else if (flags & LONG)
337 *(long *)ptr = nread;
338 else if (flags & LONGLONG)
339 *(long long *)ptr = nread;
340 else if (flags & INTMAXT)
341 *(intmax_t *)ptr = nread;
342 else if (flags & SIZET)
343 *(size_t *)ptr = nread;
344 else if (flags & PTRDIFFT)
345 *(ptrdiff_t *)ptr = nread;
346 else
347 *(int *)ptr = nread;
348 continue;
349 }
350 default:
351 goto match_failure;
352
353 /*
354 * Disgusting backwards compatibility hack. XXX
355 */
356 case '\0': /* compat */
357 return (EOF);
358 }
359
360 /*
361 * We have a conversion that requires input.
362 */
363 if (fp->_r <= 0 && __srefill(fp))
364 goto input_failure;
365
366 /*
367 * Consume leading white space, except for formats
368 * that suppress this.
369 */
370 if ((flags & NOSKIP) == 0) {
371 while (isspace_l(*fp->_p, loc)) {
372 nread++;
373 if (--fp->_r > 0)
374 fp->_p++;
375 else if (__srefill(fp))
376 goto input_failure;
377 }
378 /*
379 * Note that there is at least one character in
380 * the buffer, so conversions that do not set NOSKIP
381 * ca no longer result in an input failure.
382 */
383 }
384
385 /*
386 * Do the conversion.
387 */
388 switch (c) {
389
390 case CT_CHAR:
391 /* scan arbitrary characters (sets NOSKIP) */
392 if (width == 0)
393 width = 1;
394 if (flags & LONG) {
395 if ((flags & SUPPRESS) == 0)
396 wcp = va_arg(ap, wchar_t *);
397 else
398 wcp = NULL;
399 n = 0;
400 while (width != 0) {
401 if (n == mb_cur_max) {
402 fp->_flags |= __SERR;
403 goto input_failure;
404 }
405 buf[n++] = *fp->_p;
406 fp->_p++;
407 fp->_r--;
408 mbs = initial;
409 nconv = mbrtowc_l(wcp, buf, n, &mbs, loc);
410 if (nconv == (size_t)-1) {
411 fp->_flags |= __SERR;
412 goto input_failure;
413 }
414 if (nconv == 0 && !(flags & SUPPRESS))
415 *wcp = L'\0';
416 if (nconv != (size_t)-2) {
417 nread += n;
418 width--;
419 if (!(flags & SUPPRESS))
420 wcp++;
421 n = 0;
422 }
423 if (fp->_r <= 0 && __srefill(fp)) {
424 if (n != 0) {
425 fp->_flags |= __SERR;
426 goto input_failure;
427 }
428 break;
429 }
430 }
431 if (!(flags & SUPPRESS))
432 nassigned++;
433 } else if (flags & SUPPRESS) {
434 size_t sum = 0;
435 for (;;) {
436 if ((n = fp->_r) < width) {
437 sum += n;
438 width -= n;
439 fp->_p += n;
440 if (__srefill(fp)) {
441 if (sum == 0)
442 goto input_failure;
443 break;
444 }
445 } else {
446 sum += width;
447 fp->_r -= width;
448 fp->_p += width;
449 break;
450 }
451 }
452 nread += sum;
453 } else {
454 size_t r = __fread((void *)va_arg(ap, char *), 1,
455 width, fp);
456
457 if (r == 0)
458 goto input_failure;
459 nread += r;
460 nassigned++;
461 }
462 break;
463
464 case CT_CCL:
465 /* scan a (nonempty) character class (sets NOSKIP) */
466 if (width == 0)
467 width = (size_t)~0; /* `infinity' */
468 /* take only those things in the class */
469 if (flags & LONG) {
470 wchar_t twc;
471 int nchars;
472
473 if ((flags & SUPPRESS) == 0)
474 wcp = va_arg(ap, wchar_t *);
475 else
476 wcp = &twc;
477 n = 0;
478 nchars = 0;
479 while (width != 0) {
480 if (n == mb_cur_max) {
481 fp->_flags |= __SERR;
482 goto input_failure;
483 }
484 buf[n++] = *fp->_p;
485 fp->_p++;
486 fp->_r--;
487 mbs = initial;
488 nconv = mbrtowc_l(wcp, buf, n, &mbs, loc);
489 if (nconv == (size_t)-1) {
490 fp->_flags |= __SERR;
491 goto input_failure;
492 }
493 if (nconv == 0)
494 *wcp = L'\0';
495 if (nconv != (size_t)-2) {
496 if (wctob_l(*wcp, loc) != EOF &&
497 !ccltab[wctob_l(*wcp, loc)]) {
498 while (n != 0) {
499 n--;
500 __ungetc(buf[n],
501 fp);
502 }
503 break;
504 }
505 nread += n;
506 width--;
507 if (!(flags & SUPPRESS))
508 wcp++;
509 nchars++;
510 n = 0;
511 }
512 if (fp->_r <= 0 && __srefill(fp)) {
513 if (n != 0) {
514 fp->_flags |= __SERR;
515 goto input_failure;
516 }
517 break;
518 }
519 }
520 if (n != 0) {
521 fp->_flags |= __SERR;
522 goto input_failure;
523 }
524 n = nchars;
525 if (n == 0)
526 goto match_failure;
527 if (!(flags & SUPPRESS)) {
528 *wcp = L'\0';
529 nassigned++;
530 }
531 } else if (flags & SUPPRESS) {
532 n = 0;
533 while (ccltab[*fp->_p]) {
534 n++, fp->_r--, fp->_p++;
535 if (--width == 0)
536 break;
537 if (fp->_r <= 0 && __srefill(fp)) {
538 if (n == 0)
539 goto input_failure;
540 break;
541 }
542 }
543 if (n == 0)
544 goto match_failure;
545 } else {
546 p0 = p = va_arg(ap, char *);
547 while (ccltab[*fp->_p]) {
548 fp->_r--;
549 *p++ = *fp->_p++;
550 if (--width == 0)
551 break;
552 if (fp->_r <= 0 && __srefill(fp)) {
553 if (p == p0)
554 goto input_failure;
555 break;
556 }
557 }
558 n = p - p0;
559 if (n == 0)
560 goto match_failure;
561 *p = 0;
562 nassigned++;
563 }
564 nread += n;
565 break;
566
567 case CT_STRING:
568 /* like CCL, but zero-length string OK, & no NOSKIP */
569 if (width == 0)
570 width = (size_t)~0;
571 if (flags & LONG) {
572 wchar_t twc;
573
574 if ((flags & SUPPRESS) == 0)
575 wcp = va_arg(ap, wchar_t *);
576 else
577 wcp = &twc;
578 n = 0;
579 while (width != 0) {
580 if (n == mb_cur_max) {
581 fp->_flags |= __SERR;
582 goto input_failure;
583 }
584 buf[n++] = *fp->_p;
585 fp->_p++;
586 fp->_r--;
587 mbs = initial;
588 nconv = mbrtowc_l(wcp, buf, n, &mbs, loc);
589 if (nconv == (size_t)-1) {
590 fp->_flags |= __SERR;
591 goto input_failure;
592 }
593 if (nconv == 0)
594 *wcp = L'\0';
595 if (nconv != (size_t)-2) {
596 if (iswspace_l(*wcp, loc)) {
597 while (n != 0) {
598 n--;
599 __ungetc(buf[n],
600 fp);
601 }
602 break;
603 }
604 nread += n;
605 width--;
606 if (!(flags & SUPPRESS))
607 wcp++;
608 n = 0;
609 }
610 if (fp->_r <= 0 && __srefill(fp)) {
611 if (n != 0) {
612 fp->_flags |= __SERR;
613 goto input_failure;
614 }
615 break;
616 }
617 }
618 if (!(flags & SUPPRESS)) {
619 *wcp = L'\0';
620 nassigned++;
621 }
622 } else if (flags & SUPPRESS) {
623 n = 0;
624 while (!isspace_l(*fp->_p, loc)) {
625 n++, fp->_r--, fp->_p++;
626 if (--width == 0)
627 break;
628 if (fp->_r <= 0 && __srefill(fp))
629 break;
630 }
631 nread += n;
632 } else {
633 p0 = p = va_arg(ap, char *);
634 while (!isspace_l(*fp->_p, loc)) {
635 fp->_r--;
636 *p++ = *fp->_p++;
637 if (--width == 0)
638 break;
639 if (fp->_r <= 0 && __srefill(fp))
640 break;
641 }
642 *p = 0;
643 nread += p - p0;
644 nassigned++;
645 }
646 continue;
647
648 case CT_INT:
649 /* scan an integer as if by the conversion function */
650 #ifdef hardway
651 if (width == 0 || width > sizeof(buf) - 1)
652 width = sizeof(buf) - 1;
653 #else
654 /* size_t is unsigned, hence this optimisation */
655 if (--width > sizeof(buf) - 2)
656 width = sizeof(buf) - 2;
657 width++;
658 #endif
659 flags |= SIGNOK | NDIGITS | NZDIGITS;
660 for (p = buf; width; width--) {
661 c = *fp->_p;
662 /*
663 * Switch on the character; `goto ok'
664 * if we accept it as a part of number.
665 */
666 switch (c) {
667
668 /*
669 * The digit 0 is always legal, but is
670 * special. For %i conversions, if no
671 * digits (zero or nonzero) have been
672 * scanned (only signs), we will have
673 * base==0. In that case, we should set
674 * it to 8 and enable 0x prefixing.
675 * Also, if we have not scanned zero digits
676 * before this, do not turn off prefixing
677 * (someone else will turn it off if we
678 * have scanned any nonzero digits).
679 */
680 case '0':
681 if (base == 0) {
682 base = 8;
683 flags |= PFXOK;
684 }
685 if (flags & NZDIGITS)
686 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
687 else
688 flags &= ~(SIGNOK|PFXOK|NDIGITS);
689 goto ok;
690
691 /* 1 through 7 always legal */
692 case '1': case '2': case '3':
693 case '4': case '5': case '6': case '7':
694 base = basefix[base];
695 flags &= ~(SIGNOK | PFXOK | NDIGITS);
696 goto ok;
697
698 /* digits 8 and 9 ok iff decimal or hex */
699 case '8': case '9':
700 base = basefix[base];
701 if (base <= 8)
702 break; /* not legal here */
703 flags &= ~(SIGNOK | PFXOK | NDIGITS);
704 goto ok;
705
706 /* letters ok iff hex */
707 case 'A': case 'B': case 'C':
708 case 'D': case 'E': case 'F':
709 case 'a': case 'b': case 'c':
710 case 'd': case 'e': case 'f':
711 /* no need to fix base here */
712 if (base <= 10)
713 break; /* not legal here */
714 flags &= ~(SIGNOK | PFXOK | NDIGITS);
715 goto ok;
716
717 /* sign ok only as first character */
718 case '+': case '-':
719 if (flags & SIGNOK) {
720 flags &= ~SIGNOK;
721 flags |= HAVESIGN;
722 goto ok;
723 }
724 break;
725
726 /*
727 * x ok iff flag still set & 2nd char (or
728 * 3rd char if we have a sign).
729 */
730 case 'x': case 'X':
731 if (flags & PFXOK && p ==
732 buf + 1 + !!(flags & HAVESIGN)) {
733 base = 16; /* if %i */
734 flags &= ~PFXOK;
735 goto ok;
736 }
737 break;
738 }
739
740 /*
741 * If we got here, c is not a legal character
742 * for a number. Stop accumulating digits.
743 */
744 break;
745 ok:
746 /*
747 * c is legal: store it and look at the next.
748 */
749 *p++ = c;
750 if (--fp->_r > 0)
751 fp->_p++;
752 else if (__srefill(fp))
753 break; /* EOF */
754 }
755 /*
756 * If we had only a sign, it is no good; push
757 * back the sign. If the number ends in `x',
758 * it was [sign] '0' 'x', so push back the x
759 * and treat it as [sign] '0'.
760 */
761 if (flags & NDIGITS) {
762 if (p > buf)
763 (void) __ungetc(*(u_char *)--p, fp);
764 goto match_failure;
765 }
766 c = ((u_char *)p)[-1];
767 if (c == 'x' || c == 'X') {
768 --p;
769 (void) __ungetc(c, fp);
770 }
771 if ((flags & SUPPRESS) == 0) {
772 uintmax_t res;
773
774 *p = 0;
775 if ((flags & UNSIGNED) == 0)
776 res = strtoimax_l(buf, (char **)NULL, base, loc);
777 else
778 res = strtoumax_l(buf, (char **)NULL, base, loc);
779 if (flags & POINTER)
780 *va_arg(ap, void **) =
781 (void *)(uintptr_t)res;
782 else if (flags & SHORTSHORT)
783 *va_arg(ap, char *) = res;
784 else if (flags & SHORT)
785 *va_arg(ap, short *) = res;
786 else if (flags & LONG)
787 *va_arg(ap, long *) = res;
788 else if (flags & LONGLONG)
789 *va_arg(ap, long long *) = res;
790 else if (flags & INTMAXT)
791 *va_arg(ap, intmax_t *) = res;
792 else if (flags & PTRDIFFT)
793 *va_arg(ap, ptrdiff_t *) = res;
794 else if (flags & SIZET)
795 *va_arg(ap, size_t *) = res;
796 else
797 *va_arg(ap, int *) = res;
798 nassigned++;
799 }
800 nread += p - buf;
801 break;
802
803 #ifndef NO_FLOATING_POINT
804 case CT_FLOAT:
805 {
806 char *pbuf;
807 /* scan a floating point number as if by strtod */
808 if ((width = parsefloat(fp, &pbuf, width, loc)) == 0)
809 goto match_failure;
810 if ((flags & SUPPRESS) == 0) {
811 if (flags & LONGDBL) {
812 long double res = strtold_l(pbuf, &p, loc);
813 *va_arg(ap, long double *) = res;
814 } else if (flags & LONG) {
815 double res = strtod_l(pbuf, &p, loc);
816 *va_arg(ap, double *) = res;
817 } else {
818 float res = strtof_l(pbuf, &p, loc);
819 *va_arg(ap, float *) = res;
820 }
821 nassigned++;
822 }
823 nread += width;
824 break;
825 }
826 #endif /* !NO_FLOATING_POINT */
827 }
828 }
829 input_failure:
830 return (nassigned ? nassigned : EOF);
831 match_failure:
832 return (nassigned);
833 }
834
835 int
836 __svfscanf(FILE * __restrict fp, const char * __restrict fmt0, va_list ap)
837 {
838 return __svfscanf_l(fp, __current_locale(), fmt0, ap);
839 }
840
841 /*
842 * Fill in the given table from the scanset at the given format
843 * (just after `['). Return a pointer to the character past the
844 * closing `]'. The table has a 1 wherever characters should be
845 * considered part of the scanset.
846 */
847 static const u_char *
848 __sccl(tab, fmt, loc)
849 char *tab;
850 const u_char *fmt;
851 locale_t loc;
852 {
853 int c, n, v, i;
854
855 /* first `clear' the whole table */
856 c = *fmt++; /* first char hat => negated scanset */
857 if (c == '^') {
858 v = 1; /* default => accept */
859 c = *fmt++; /* get new first char */
860 } else
861 v = 0; /* default => reject */
862
863 /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
864 (void) memset(tab, v, 256);
865
866 if (c == 0)
867 return (fmt - 1);/* format ended before closing ] */
868
869 /*
870 * Now set the entries corresponding to the actual scanset
871 * to the opposite of the above.
872 *
873 * The first character may be ']' (or '-') without being special;
874 * the last character may be '-'.
875 */
876 v = 1 - v;
877 for (;;) {
878 tab[c] = v; /* take character c */
879 doswitch:
880 n = *fmt++; /* and examine the next */
881 switch (n) {
882
883 case 0: /* format ended too soon */
884 return (fmt - 1);
885
886 case '-':
887 {
888 /*
889 * A scanset of the form
890 * [01+-]
891 * is defined as `the digit 0, the digit 1,
892 * the character +, the character -', but
893 * the effect of a scanset such as
894 * [a-zA-Z0-9]
895 * is implementation defined. The V7 Unix
896 * scanf treats `a-z' as `the letters a through
897 * z', but treats `a-a' as `the letter a, the
898 * character -, and the letter a'.
899 *
900 * For compatibility, the `-' is not considerd
901 * to define a range if the character following
902 * it is either a close bracket (required by ANSI)
903 * or is not numerically greater than the character
904 * we just stored in the table (c).
905 */
906 n = *fmt;
907 if (n == ']'
908 || (loc->__collate_load_error ? n < c :
909 __collate_range_cmp (n, c, loc) < 0
910 )
911 ) {
912 c = '-';
913 break; /* resume the for(;;) */
914 }
915 fmt++;
916 /* fill in the range */
917 if (loc->__collate_load_error) {
918 do {
919 tab[++c] = v;
920 } while (c < n);
921 } else {
922 for (i = 0; i < 256; i ++)
923 if ( __collate_range_cmp (c, i, loc) < 0
924 && __collate_range_cmp (i, n, loc) <= 0
925 )
926 tab[i] = v;
927 }
928 #if 1 /* XXX another disgusting compatibility hack */
929 c = n;
930 /*
931 * Alas, the V7 Unix scanf also treats formats
932 * such as [a-c-e] as `the letters a through e'.
933 * This too is permitted by the standard....
934 */
935 goto doswitch;
936 #else
937 c = *fmt++;
938 if (c == 0)
939 return (fmt - 1);
940 if (c == ']')
941 return (fmt);
942 #endif
943 break;
944 }
945 case ']': /* end of scanset */
946 return (fmt);
947
948 default: /* just another character */
949 c = n;
950 break;
951 }
952 }
953 /* NOTREACHED */
954 }
955
956 #ifndef NO_FLOATING_POINT
957 /*
958 * Maintain a per-thread parsefloat buffer, shared by __svfscanf_l and
959 * __vfwscanf.
960 */
961 #ifdef BUILDING_VARIANT
962 extern char *__parsefloat_buf(size_t s);
963 #else /* !BUILDING_VARIANT */
964 __private_extern__ char *
965 __parsefloat_buf(size_t s)
966 {
967 char *b;
968 static pthread_key_t parsefloat_tsd_key = (pthread_key_t)-1;
969 static pthread_mutex_t parsefloat_tsd_lock = PTHREAD_MUTEX_INITIALIZER;
970 static size_t bsiz = 0;
971
972 if (parsefloat_tsd_key == (pthread_key_t)-1) {
973 pthread_mutex_lock(&parsefloat_tsd_lock);
974 if (parsefloat_tsd_key == (pthread_key_t)-1) {
975 parsefloat_tsd_key = __LIBC_PTHREAD_KEY_PARSEFLOAT;
976 pthread_key_init_np(parsefloat_tsd_key, free);
977 }
978 pthread_mutex_unlock(&parsefloat_tsd_lock);
979 }
980 if ((b = (char *)pthread_getspecific(parsefloat_tsd_key)) == NULL) {
981 bsiz = s > BUF ? s : BUF;
982 b = (char *)malloc(bsiz);
983 if (b == NULL) {
984 bsiz = 0;
985 return NULL;
986 }
987 pthread_setspecific(parsefloat_tsd_key, b);
988 return b;
989 }
990 if (s > bsiz) {
991 b = (char *)reallocf(b, s);
992 pthread_setspecific(parsefloat_tsd_key, b);
993 if (b == NULL) {
994 bsiz = 0;
995 return NULL;
996 }
997 bsiz = s;
998 }
999 return b;
1000 }
1001 #endif /* BUILDING_VARIANT */
1002
1003 static int
1004 parsefloat(FILE *fp, char **buf, size_t width, locale_t loc)
1005 {
1006 char *commit, *p;
1007 int infnanpos = 0, decptpos = 0;
1008 enum {
1009 S_START, S_GOTSIGN, S_INF, S_NAN, S_DONE, S_MAYBEHEX,
1010 S_DIGITS, S_DECPT, S_FRAC, S_EXP, S_EXPDIGITS
1011 } state = S_START;
1012 unsigned char c;
1013 const char *decpt = localeconv_l(loc)->decimal_point;
1014 _Bool gotmantdig = 0, ishex = 0;
1015 char *b;
1016 char *e;
1017 size_t s;
1018
1019 s = (width == 0 ? BUF : (width + 1));
1020 if ((b = __parsefloat_buf(s)) == NULL) {
1021 *buf = NULL;
1022 return 0;
1023 }
1024 e = b + (s - 1);
1025 /*
1026 * We set commit = p whenever the string we have read so far
1027 * constitutes a valid representation of a floating point
1028 * number by itself. At some point, the parse will complete
1029 * or fail, and we will ungetc() back to the last commit point.
1030 * To ensure that the file offset gets updated properly, it is
1031 * always necessary to read at least one character that doesn't
1032 * match; thus, we can't short-circuit "infinity" or "nan(...)".
1033 */
1034 commit = b - 1;
1035 for (p = b; width == 0 || p < e; ) {
1036 c = *fp->_p;
1037 reswitch:
1038 switch (state) {
1039 case S_START:
1040 state = S_GOTSIGN;
1041 if (c == '-' || c == '+')
1042 break;
1043 else
1044 goto reswitch;
1045 case S_GOTSIGN:
1046 switch (c) {
1047 case '0':
1048 state = S_MAYBEHEX;
1049 commit = p;
1050 break;
1051 case 'I':
1052 case 'i':
1053 state = S_INF;
1054 break;
1055 case 'N':
1056 case 'n':
1057 state = S_NAN;
1058 break;
1059 default:
1060 state = S_DIGITS;
1061 goto reswitch;
1062 }
1063 break;
1064 case S_INF:
1065 if (infnanpos > 6 ||
1066 (c != "nfinity"[infnanpos] &&
1067 c != "NFINITY"[infnanpos]))
1068 goto parsedone;
1069 if (infnanpos == 1 || infnanpos == 6)
1070 commit = p; /* inf or infinity */
1071 infnanpos++;
1072 break;
1073 case S_NAN:
1074 switch (infnanpos) {
1075 case 0:
1076 if (c != 'A' && c != 'a')
1077 goto parsedone;
1078 break;
1079 case 1:
1080 if (c != 'N' && c != 'n')
1081 goto parsedone;
1082 else
1083 commit = p;
1084 break;
1085 case 2:
1086 if (c != '(')
1087 goto parsedone;
1088 break;
1089 default:
1090 if (c == ')') {
1091 commit = p;
1092 state = S_DONE;
1093 } else if (!isalnum_l(c, loc) && c != '_')
1094 goto parsedone;
1095 break;
1096 }
1097 infnanpos++;
1098 break;
1099 case S_DONE:
1100 goto parsedone;
1101 case S_MAYBEHEX:
1102 state = S_DIGITS;
1103 if (c == 'X' || c == 'x') {
1104 ishex = 1;
1105 break;
1106 } else { /* we saw a '0', but no 'x' */
1107 gotmantdig = 1;
1108 goto reswitch;
1109 }
1110 case S_DIGITS:
1111 if ((ishex && isxdigit_l(c, loc)) || isdigit_l(c, loc)) {
1112 gotmantdig = 1;
1113 commit = p;
1114 break;
1115 } else {
1116 state = S_DECPT;
1117 goto reswitch;
1118 }
1119 case S_DECPT:
1120 if (c == decpt[decptpos]) {
1121 if (decpt[++decptpos] == '\0') {
1122 /* We read the complete decpt seq. */
1123 state = S_FRAC;
1124 if (gotmantdig)
1125 commit = p;
1126 }
1127 break;
1128 } else if (!decptpos) {
1129 /* We didn't read any decpt characters. */
1130 state = S_FRAC;
1131 goto reswitch;
1132 } else {
1133 /*
1134 * We read part of a multibyte decimal point,
1135 * but the rest is invalid, so bail.
1136 */
1137 goto parsedone;
1138 }
1139 case S_FRAC:
1140 if (((c == 'E' || c == 'e') && !ishex) ||
1141 ((c == 'P' || c == 'p') && ishex)) {
1142 if (!gotmantdig)
1143 goto parsedone;
1144 else
1145 state = S_EXP;
1146 } else if ((ishex && isxdigit_l(c, loc)) || isdigit_l(c, loc)) {
1147 commit = p;
1148 gotmantdig = 1;
1149 } else
1150 goto parsedone;
1151 break;
1152 case S_EXP:
1153 state = S_EXPDIGITS;
1154 if (c == '-' || c == '+')
1155 break;
1156 else
1157 goto reswitch;
1158 case S_EXPDIGITS:
1159 if (isdigit_l(c, loc))
1160 commit = p;
1161 else
1162 goto parsedone;
1163 break;
1164 default:
1165 LIBC_ABORT("unknown state %d", state);
1166 }
1167 if (p >= e) {
1168 ssize_t diff = (p - b);
1169 ssize_t com = (commit - b);
1170 s += BUF;
1171 b = __parsefloat_buf(s);
1172 if (b == NULL) {
1173 *buf = NULL;
1174 return 0;
1175 }
1176 e = b + (s - 1);
1177 p = b + diff;
1178 commit = b + com;
1179 }
1180 *p++ = c;
1181 if (--fp->_r > 0)
1182 fp->_p++;
1183 else if (__srefill(fp))
1184 break; /* EOF */
1185 }
1186
1187 parsedone:
1188 while (commit < --p)
1189 __ungetc(*(u_char *)p, fp);
1190 *++commit = '\0';
1191 *buf = b;
1192 return (commit - b);
1193 }
1194 #endif