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