]>
git.saurik.com Git - apple/libc.git/blob - stdio/FreeBSD/vfwscanf.c
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid
[] = "@(#)vfscanf.c 8.1 (Berkeley) 6/4/93";
36 #endif /* LIBC_SCCS and not lint */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfwscanf.c,v 1.17 2009/01/19 06:19:51 das Exp $");
41 #include "xlocale_private.h"
43 #include "namespace.h"
54 #include "un-namespace.h"
56 #include "libc_private.h"
59 #ifndef NO_FLOATING_POINT
63 #define BUF 513 /* Maximum length of numeric string. */
66 * Flags used during conversion.
68 #define LONG 0x01 /* l: long or double */
69 #define LONGDBL 0x02 /* L: long double */
70 #define SHORT 0x04 /* h: short */
71 #define SUPPRESS 0x08 /* *: suppress assignment */
72 #define POINTER 0x10 /* p: void * (as hex) */
73 #define NOSKIP 0x20 /* [ or c: do not skip blanks */
74 #define LONGLONG 0x400 /* ll: long long (+ deprecated q: quad) */
75 #define INTMAXT 0x800 /* j: intmax_t */
76 #define PTRDIFFT 0x1000 /* t: ptrdiff_t */
77 #define SIZET 0x2000 /* z: size_t */
78 #define SHORTSHORT 0x4000 /* hh: char */
79 #define UNSIGNED 0x8000 /* %[oupxX] conversions */
82 * The following are used in integral conversions only:
83 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
85 #define SIGNOK 0x40 /* +/- is (still) legal */
86 #define NDIGITS 0x80 /* no digits detected */
87 #define PFXOK 0x100 /* 0x prefix is (still) legal */
88 #define NZDIGITS 0x200 /* no zero digits detected */
89 #define HAVESIGN 0x10000 /* sign detected */
94 #define CT_CHAR 0 /* %c conversion */
95 #define CT_CCL 1 /* %[...] conversion */
96 #define CT_STRING 2 /* %s conversion */
97 #define CT_INT 3 /* %[dioupxX] conversion */
98 #define CT_FLOAT 4 /* %[efgEFG] conversion */
100 #ifndef NO_FLOATING_POINT
101 static int parsefloat(FILE *, wchar_t **, size_t, locale_t loc
);
105 (cclcompl ? (wmemchr(ccls, (_c), ccle - ccls) == NULL) : \
106 (wmemchr(ccls, (_c), ccle - ccls) != NULL))
108 static const mbstate_t initial_mbs
;
114 vfwscanf(FILE * __restrict fp
, const wchar_t * __restrict fmt
, va_list ap
)
120 ret
= __vfwscanf(fp
, __current_locale(), fmt
, ap
);
126 vfwscanf_l(FILE * __restrict fp
, locale_t loc
, const wchar_t * __restrict fmt
,
131 NORMALIZE_LOCALE(loc
);
134 ret
= __vfwscanf(fp
, loc
, fmt
, ap
);
140 * Non-MT-safe version.
142 __private_extern__
int
143 __vfwscanf(FILE * __restrict fp
, locale_t loc
, const wchar_t * __restrict fmt
,
146 wint_t c
; /* character from format, or conversion */
147 size_t width
; /* field width, or 0 */
148 wchar_t *p
; /* points into all kinds of strings */
149 int n
; /* handy integer */
150 int flags
; /* flags as defined above */
151 wchar_t *p0
; /* saves original value of p when necessary */
152 int nassigned
; /* number of fields assigned */
153 int nread
; /* number of characters consumed from fp */
154 int base
; /* base argument to conversion function */
155 wchar_t buf
[BUF
]; /* buffer for numeric conversions */
156 const wchar_t *ccls
; /* character class start */
157 const wchar_t *ccle
; /* character class end */
158 int cclcompl
; /* ccl is complemented? */
159 wint_t wi
; /* handy wint_t */
160 char *mbp
; /* multibyte string pointer for %c %s %[ */
161 size_t nconv
; /* number of bytes in mb. conversion */
162 char mbbuf
[MB_LEN_MAX
]; /* temporary mb. character buffer */
163 int index
; /* for %index$ */
164 va_list ap_orig
; /* to reset ap to first argument */
166 int mb_cur_max
= MB_CUR_MAX_L(loc
);
168 /* `basefix' is used to avoid `if' tests in the integer scanner */
169 static const short basefix
[17] =
170 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
175 va_copy(ap_orig
, ap
);
180 if (iswspace_l(c
, loc
)) {
181 while ((c
= __fgetwc(fp
, loc
)) != WEOF
&&
185 __ungetwc(c
, fp
, loc
);
189 if ((wi
= __fgetwc(fp
, loc
)) == WEOF
)
196 * switch on the format. continue if done;
197 * break once format type is derived.
202 /* Consume leading white space */
204 if ((wi
= __fgetwc(fp
, loc
)) == WEOF
)
206 if (!iswspace_l(wi
, loc
))
212 __ungetwc(wi
, fp
, loc
);
220 if (index
< 1 || index
> NL_ARGMAX
|| fmt
[-3] != '%') {
225 va_copy(ap
, ap_orig
); /* reset to %1$ */
226 for (; index
> 1; index
--) {
244 flags
|= LONGLONG
; /* not quite */
263 case '0': case '1': case '2': case '3': case '4':
264 case '5': case '6': case '7': case '8': case '9':
265 width
= width
* 10 + c
- '0';
295 flags
|= PFXOK
; /* enable 0x prefixing */
301 #ifndef NO_FLOATING_POINT
302 case 'A': case 'E': case 'F': case 'G':
303 case 'a': case 'e': case 'f': case 'g':
324 while (*fmt
!= '\0' && *fmt
!= ']')
340 case 'p': /* pointer format is like hex */
341 flags
|= POINTER
| PFXOK
;
342 c
= CT_INT
; /* assumes sizeof(uintmax_t) */
343 flags
|= UNSIGNED
; /* >= sizeof(uintptr_t) */
349 if (flags
& SUPPRESS
) /* ??? */
351 void *ptr
= va_arg(ap
, void *);
354 else if (flags
& SHORTSHORT
)
355 *(char *)ptr
= nread
;
356 else if (flags
& SHORT
)
357 *(short *)ptr
= nread
;
358 else if (flags
& LONG
)
359 *(long *)ptr
= nread
;
360 else if (flags
& LONGLONG
)
361 *(long long *)ptr
= nread
;
362 else if (flags
& INTMAXT
)
363 *(intmax_t *)ptr
= nread
;
364 else if (flags
& SIZET
)
365 *(size_t *)ptr
= nread
;
366 else if (flags
& PTRDIFFT
)
367 *(ptrdiff_t *)ptr
= nread
;
376 * Disgusting backwards compatibility hack. XXX
378 case '\0': /* compat */
383 * Consume leading white space, except for formats
384 * that suppress this.
386 if ((flags
& NOSKIP
) == 0) {
387 while ((wi
= __fgetwc(fp
, loc
)) != WEOF
&& iswspace_l(wi
, loc
))
391 __ungetwc(wi
, fp
, loc
);
400 /* scan arbitrary characters (sets NOSKIP) */
404 if (!(flags
& SUPPRESS
))
405 p
= va_arg(ap
, wchar_t *);
407 while (width
-- != 0 &&
408 (wi
= __fgetwc(fp
, loc
)) != WEOF
) {
409 if (!(flags
& SUPPRESS
))
416 if (!(flags
& SUPPRESS
))
419 if (!(flags
& SUPPRESS
))
420 mbp
= va_arg(ap
, char *);
424 (wi
= __fgetwc(fp
, loc
)) != WEOF
) {
425 if (width
>= mb_cur_max
&&
426 !(flags
& SUPPRESS
)) {
427 nconv
= wcrtomb_l(mbp
, wi
, &mbs
, loc
);
428 if (nconv
== (size_t)-1)
431 nconv
= wcrtomb_l(mbbuf
, wi
,
433 if (nconv
== (size_t)-1)
436 __ungetwc(wi
, fp
, loc
);
439 if (!(flags
& SUPPRESS
))
443 if (!(flags
& SUPPRESS
))
451 if (!(flags
& SUPPRESS
))
457 /* scan a (nonempty) character class (sets NOSKIP) */
459 width
= (size_t)~0; /* `infinity' */
460 /* take only those things in the class */
461 if ((flags
& SUPPRESS
) && (flags
& LONG
)) {
463 while ((wi
= __fgetwc(fp
, loc
)) != WEOF
&&
464 width
-- != 0 && INCCL(wi
))
467 __ungetwc(wi
, fp
, loc
);
470 } else if (flags
& LONG
) {
471 p0
= p
= va_arg(ap
, wchar_t *);
472 while ((wi
= __fgetwc(fp
, loc
)) != WEOF
&&
473 width
-- != 0 && INCCL(wi
))
476 __ungetwc(wi
, fp
, loc
);
483 if (!(flags
& SUPPRESS
))
484 mbp
= va_arg(ap
, char *);
487 while ((wi
= __fgetwc(fp
, loc
)) != WEOF
&&
488 width
!= 0 && INCCL(wi
)) {
489 if (width
>= mb_cur_max
&&
490 !(flags
& SUPPRESS
)) {
491 nconv
= wcrtomb_l(mbp
, wi
, &mbs
, loc
);
492 if (nconv
== (size_t)-1)
495 nconv
= wcrtomb_l(mbbuf
, wi
,
497 if (nconv
== (size_t)-1)
501 if (!(flags
& SUPPRESS
))
505 if (!(flags
& SUPPRESS
))
511 __ungetwc(wi
, fp
, loc
);
514 if (!(flags
& SUPPRESS
)) {
523 /* like CCL, but zero-length string OK, & no NOSKIP */
526 if ((flags
& SUPPRESS
) && (flags
& LONG
)) {
527 while ((wi
= __fgetwc(fp
, loc
)) != WEOF
&&
529 !iswspace_l(wi
, loc
))
532 __ungetwc(wi
, fp
, loc
);
533 } else if (flags
& LONG
) {
534 p0
= p
= va_arg(ap
, wchar_t *);
535 while ((wi
= __fgetwc(fp
, loc
)) != WEOF
&&
537 !iswspace_l(wi
, loc
)) {
542 __ungetwc(wi
, fp
, loc
);
546 if (!(flags
& SUPPRESS
))
547 mbp
= va_arg(ap
, char *);
549 while ((wi
= __fgetwc(fp
, loc
)) != WEOF
&&
551 !iswspace_l(wi
, loc
)) {
552 if (width
>= mb_cur_max
&&
553 !(flags
& SUPPRESS
)) {
554 nconv
= wcrtomb_l(mbp
, wi
, &mbs
, loc
);
555 if (nconv
== (size_t)-1)
558 nconv
= wcrtomb_l(mbbuf
, wi
,
560 if (nconv
== (size_t)-1)
564 if (!(flags
& SUPPRESS
))
568 if (!(flags
& SUPPRESS
))
574 __ungetwc(wi
, fp
, loc
);
575 if (!(flags
& SUPPRESS
)) {
583 /* scan an integer as if by the conversion function */
584 if (width
== 0 || width
> sizeof(buf
) /
586 width
= sizeof(buf
) / sizeof(*buf
) - 1;
587 flags
|= SIGNOK
| NDIGITS
| NZDIGITS
;
588 for (p
= buf
; width
; width
--) {
589 c
= __fgetwc(fp
, loc
);
591 * Switch on the character; `goto ok'
592 * if we accept it as a part of number.
597 * The digit 0 is always legal, but is
598 * special. For %i conversions, if no
599 * digits (zero or nonzero) have been
600 * scanned (only signs), we will have
601 * base==0. In that case, we should set
602 * it to 8 and enable 0x prefixing.
603 * Also, if we have not scanned zero digits
604 * before this, do not turn off prefixing
605 * (someone else will turn it off if we
606 * have scanned any nonzero digits).
613 if (flags
& NZDIGITS
)
614 flags
&= ~(SIGNOK
|NZDIGITS
|NDIGITS
);
616 flags
&= ~(SIGNOK
|PFXOK
|NDIGITS
);
619 /* 1 through 7 always legal */
620 case '1': case '2': case '3':
621 case '4': case '5': case '6': case '7':
622 base
= basefix
[base
];
623 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
626 /* digits 8 and 9 ok iff decimal or hex */
628 base
= basefix
[base
];
630 break; /* not legal here */
631 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
634 /* letters ok iff hex */
635 case 'A': case 'B': case 'C':
636 case 'D': case 'E': case 'F':
637 case 'a': case 'b': case 'c':
638 case 'd': case 'e': case 'f':
639 /* no need to fix base here */
641 break; /* not legal here */
642 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
645 /* sign ok only as first character */
647 if (flags
& SIGNOK
) {
655 * x ok iff flag still set & 2nd char (or
656 * 3rd char if we have a sign).
659 if (flags
& PFXOK
&& p
==
660 buf
+ 1 + !!(flags
& HAVESIGN
)) {
661 base
= 16; /* if %i */
669 * If we got here, c is not a legal character
670 * for a number. Stop accumulating digits.
673 __ungetwc(c
, fp
, loc
);
677 * c is legal: store it and look at the next.
682 * If we had only a sign, it is no good; push
683 * back the sign. If the number ends in `x',
684 * it was [sign] '0' 'x', so push back the x
685 * and treat it as [sign] '0'.
687 if (flags
& NDIGITS
) {
689 __ungetwc(*--p
, fp
, loc
);
693 if (c
== 'x' || c
== 'X') {
695 __ungetwc(c
, fp
, loc
);
697 if ((flags
& SUPPRESS
) == 0) {
701 if ((flags
& UNSIGNED
) == 0)
702 res
= wcstoimax_l(buf
, NULL
, base
, loc
);
704 res
= wcstoumax_l(buf
, NULL
, base
, loc
);
706 *va_arg(ap
, void **) =
707 (void *)(uintptr_t)res
;
708 else if (flags
& SHORTSHORT
)
709 *va_arg(ap
, char *) = res
;
710 else if (flags
& SHORT
)
711 *va_arg(ap
, short *) = res
;
712 else if (flags
& LONG
)
713 *va_arg(ap
, long *) = res
;
714 else if (flags
& LONGLONG
)
715 *va_arg(ap
, long long *) = res
;
716 else if (flags
& INTMAXT
)
717 *va_arg(ap
, intmax_t *) = res
;
718 else if (flags
& PTRDIFFT
)
719 *va_arg(ap
, ptrdiff_t *) = res
;
720 else if (flags
& SIZET
)
721 *va_arg(ap
, size_t *) = res
;
723 *va_arg(ap
, int *) = res
;
729 #ifndef NO_FLOATING_POINT
733 /* scan a floating point number as if by strtod */
734 if ((width
= parsefloat(fp
, &pbuf
, width
, loc
)) == 0)
736 if ((flags
& SUPPRESS
) == 0) {
737 if (flags
& LONGDBL
) {
738 long double res
= wcstold_l(pbuf
, &p
, loc
);
739 *va_arg(ap
, long double *) = res
;
740 } else if (flags
& LONG
) {
741 double res
= wcstod_l(pbuf
, &p
, loc
);
742 *va_arg(ap
, double *) = res
;
744 float res
= wcstof_l(pbuf
, &p
, loc
);
745 *va_arg(ap
, float *) = res
;
752 #endif /* !NO_FLOATING_POINT */
756 return (nassigned
? nassigned
: EOF
);
761 #ifndef NO_FLOATING_POINT
762 extern char *__parsefloat_buf(size_t s
); /* see vfscanf-fbsd.c */
765 parsefloat(FILE *fp
, wchar_t **buf
, size_t width
, locale_t loc
)
772 S_START
, S_GOTSIGN
, S_INF
, S_NAN
, S_DONE
, S_MAYBEHEX
,
773 S_DIGITS
, S_FRAC
, S_EXP
, S_EXPDIGITS
777 _Bool gotmantdig
= 0, ishex
= 0;
784 nconv
= mbrtowc_l(&decpt
, localeconv()->decimal_point
, MB_CUR_MAX_L(loc
), &mbs
, loc
);
785 if (nconv
== (size_t)-1 || nconv
== (size_t)-2)
786 decpt
= '.'; /* failsafe */
788 s
= (width
== 0 ? BUF
: (width
+ 1));
789 if ((b
= (wchar_t *)__parsefloat_buf(s
* sizeof(wchar_t))) == NULL
) {
795 * We set commit = p whenever the string we have read so far
796 * constitutes a valid representation of a floating point
797 * number by itself. At some point, the parse will complete
798 * or fail, and we will ungetc() back to the last commit point.
799 * To ensure that the file offset gets updated properly, it is
800 * always necessary to read at least one character that doesn't
801 * match; thus, we can't short-circuit "infinity" or "nan(...)".
805 for (p
= b
; width
== 0 || p
< e
; ) {
806 if ((c
= __fgetwc(fp
, loc
)) == WEOF
)
812 if (c
== '-' || c
== '+')
837 (c
!= "nfinity"[infnanpos
] &&
838 c
!= "NFINITY"[infnanpos
]))
840 if (infnanpos
== 1 || infnanpos
== 6)
841 commit
= p
; /* inf or infinity */
847 if (c
!= 'A' && c
!= 'a')
851 if (c
!= 'N' && c
!= 'n')
864 } else if (!iswalnum_l(c
, loc
) && c
!= '_')
874 if (c
== 'X' || c
== 'x') {
877 } else { /* we saw a '0', but no 'x' */
882 if ((ishex
&& iswxdigit_l(c
, loc
)) || iswdigit_l(c
, loc
))
893 if (((c
== 'E' || c
== 'e') && !ishex
) ||
894 ((c
== 'P' || c
== 'p') && ishex
)) {
899 } else if ((ishex
&& iswxdigit_l(c
, loc
)) || iswdigit_l(c
, loc
)) {
907 if (c
== '-' || c
== '+')
912 if (iswdigit_l(c
, loc
))
918 LIBC_ABORT("unknown state %d", state
);
921 ssize_t diff
= (p
- b
);
922 ssize_t com
= (commit
- b
);
924 b
= (wchar_t *)__parsefloat_buf(s
* sizeof(wchar_t));
939 __ungetwc(c
, fp
, loc
);
941 __ungetwc(*p
, fp
, loc
);