1 /* Copyright (C) 1991-2002, 2003, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
30 #include <bits/libc-lock.h>
31 #include <locale/localeinfo.h>
34 # define HAVE_LONGLONG
35 # define LONGLONG long long
37 # define LONGLONG long
40 /* Determine whether we have to handle `long long' at all. */
41 #if LONG_MAX == LONG_LONG_MAX
42 # define need_longlong 0
44 # define need_longlong 1
47 /* Determine whether we have to handle `long'. */
48 #if INT_MAX == LONG_MAX
54 /* Those are flags in the conversion format. */
55 #define LONG 0x001 /* l: long or double */
56 #define LONGDBL 0x002 /* L: long long or long double */
57 #define SHORT 0x004 /* h: short */
58 #define SUPPRESS 0x008 /* *: suppress assignment */
59 #define POINTER 0x010 /* weird %p pointer (`fake hex') */
60 #define NOSKIP 0x020 /* do not skip blanks */
61 #define WIDTH 0x040 /* width was given */
62 #define GROUP 0x080 /* ': group numbers */
63 #define MALLOC 0x100 /* a: malloc strings */
64 #define CHAR 0x200 /* hh: char */
65 #define I18N 0x400 /* I: use locale's digits */
68 #include <locale/localeinfo.h>
73 #define va_list _IO_va_list
76 # define ungetc(c, s) ((void) (c == WEOF \
78 INTUSE(_IO_sputbackwc) (s, c))))
79 # define ungetc_not_eof(c, s) ((void) (--read_in, \
80 INTUSE(_IO_sputbackwc) (s, c)))
81 # define inchar() (c == WEOF ? ((errno = inchar_errno), WEOF) \
82 : ((c = _IO_getwc_unlocked (s)), \
85 : (size_t) (inchar_errno = errno)), c))
87 # define MEMCPY(d, s, n) __wmemcpy (d, s, n)
88 # define ISSPACE(Ch) iswspace (Ch)
89 # define ISDIGIT(Ch) iswdigit (Ch)
90 # define ISXDIGIT(Ch) iswxdigit (Ch)
91 # define TOLOWER(Ch) towlower (Ch)
92 # define ORIENT if (_IO_fwide (s, 1) != 1) return WEOF
93 # define __strtoll_internal __wcstoll_internal
94 # define __strtoull_internal __wcstoull_internal
95 # define __strtol_internal __wcstol_internal
96 # define __strtoul_internal __wcstoul_internal
97 # define __strtold_internal __wcstold_internal
98 # define __strtod_internal __wcstod_internal
99 # define __strtof_internal __wcstof_internal
101 # define L_(Str) L##Str
102 # define CHAR_T wchar_t
103 # define UCHAR_T unsigned int
104 # define WINT_T wint_t
108 # define ungetc(c, s) ((void) ((int) c == EOF \
110 INTUSE(_IO_sputbackc) (s, (unsigned char) c))))
111 # define ungetc_not_eof(c, s) ((void) (--read_in, \
112 INTUSE(_IO_sputbackc) (s, (unsigned char) c)))
113 # define inchar() (c == EOF ? ((errno = inchar_errno), EOF) \
114 : ((c = _IO_getc_unlocked (s)), \
117 : (size_t) (inchar_errno = errno)), c))
118 # define MEMCPY(d, s, n) memcpy (d, s, n)
119 # define ISSPACE(Ch) __isspace_l (Ch, loc)
120 # define ISDIGIT(Ch) __isdigit_l (Ch, loc)
121 # define ISXDIGIT(Ch) __isxdigit_l (Ch, loc)
122 # define TOLOWER(Ch) __tolower_l ((unsigned char) (Ch), loc)
123 # define ORIENT if (_IO_vtable_offset (s) == 0 \
124 && _IO_fwide (s, -1) != -1) \
129 # define UCHAR_T unsigned char
133 #define encode_error() do { \
135 __set_errno (EILSEQ); \
138 #define conv_error() do { \
142 #define input_error() do { \
144 if (done == 0) done = EOF; \
147 #define memory_error() do { \
148 __set_errno (ENOMEM); \
152 #define ARGCHECK(s, format) \
155 /* Check file argument for consistence. */ \
156 CHECK_FILE (s, EOF); \
157 if (s->_flags & _IO_NO_READS) \
159 __set_errno (EBADF); \
162 else if (format == NULL) \
168 #define LOCK_STREAM(S) \
169 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, (S)); \
171 #define UNLOCK_STREAM(S) \
172 _IO_funlockfile (S); \
173 __libc_cleanup_region_end (0)
176 /* Read formatted input from S according to the format string
177 FORMAT, using the argument list in ARG.
178 Return the number of assignments made, or -1 for an input error. */
179 #ifdef COMPILE_WSCANF
181 _IO_vfwscanf (s
, format
, argptr
, errp
)
183 const wchar_t *format
;
188 _IO_vfscanf (s
, format
, argptr
, errp
)
196 register const CHAR_T
*f
= format
;
197 register UCHAR_T fc
; /* Current character of the format. */
198 register WINT_T done
= 0; /* Assignments done. */
199 register size_t read_in
= 0; /* Chars read in. */
200 register WINT_T c
= 0; /* Last char read. */
201 register int width
; /* Maximum field width. */
202 register int flags
; /* Modifiers for current format element. */
204 #ifndef COMPILE_WSCANF
205 __locale_t loc
= _NL_CURRENT_LOCALE
;
206 struct locale_data
*const curctype
= loc
->__locales
[LC_CTYPE
];
209 /* Errno of last failed inchar call. */
210 int inchar_errno
= 0;
211 /* Status for reading F-P nums. */
212 char got_dot
, got_e
, negative
;
213 /* If a [...] is a [^...]. */
215 #define exp_char not_in
216 /* Base for integral numbers. */
218 /* Signedness for integral numbers. */
220 #define is_hexa number_signed
221 /* Decimal point character. */
222 #ifdef COMPILE_WSCANF
227 /* The thousands character of the current locale. */
228 #ifdef COMPILE_WSCANF
231 const char *thousands
;
233 /* State for the conversions. */
235 /* Integral holding variables. */
239 unsigned long long int uq
;
241 unsigned long int ul
;
243 /* Character-buffer pointer. */
245 wchar_t *wstr
= NULL
;
246 char **strptr
= NULL
;
248 /* We must not react on white spaces immediately because they can
249 possibly be matched even if in the input stream no character is
250 available anymore. */
252 /* Nonzero if we are reading a pointer. */
255 CHAR_T
*tw
; /* Temporary pointer. */
256 CHAR_T
*wp
= NULL
; /* Workspace. */
257 size_t wpmax
= 0; /* Maximal size of workspace. */
258 size_t wpsize
; /* Currently used bytes in workspace. */
262 if (wpsize == wpmax) \
265 wpmax = (UCHAR_MAX + 1 > 2 * wpmax ? UCHAR_MAX + 1 : 2 * wpmax); \
266 wp = (CHAR_T *) alloca (wpmax * sizeof (wchar_t)); \
268 MEMCPY (wp, old, wpsize); \
270 wp[wpsize++] = (Ch); \
275 __va_copy (arg
, argptr
);
277 arg
= (va_list) argptr
;
284 ARGCHECK (s
, format
);
287 #ifndef COMPILE_WSCANF
288 struct locale_data
*const curnumeric
= loc
->__locales
[LC_NUMERIC
];
291 /* Figure out the decimal point character. */
292 #ifdef COMPILE_WSCANF
293 decimal
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
295 decimal
= curnumeric
->values
[_NL_ITEM_INDEX (DECIMAL_POINT
)].string
;
297 /* Figure out the thousands separator character. */
298 #ifdef COMPILE_WSCANF
299 thousands
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_THOUSANDS_SEP_WC
);
301 thousands
= curnumeric
->values
[_NL_ITEM_INDEX (THOUSANDS_SEP
)].string
;
302 if (*thousands
== '\0')
307 /* Lock the stream. */
311 #ifndef COMPILE_WSCANF
312 /* From now on we use `state' to convert the format string. */
313 memset (&state
, '\0', sizeof (state
));
316 /* Run through the format string. */
320 /* Extract the next argument, which is of type TYPE.
321 For a %N$... spec, this is the Nth argument from the beginning;
322 otherwise it is the next argument after the state now in ARG. */
324 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
325 ({ unsigned int pos = argpos; \
327 __va_copy (arg, argptr); \
329 (void) va_arg (arg, void *); \
330 va_arg (arg, type); \
334 /* XXX Possible optimization. */
335 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
336 ({ va_list arg = (va_list) argptr; \
337 arg = (va_list) ((char *) arg \
339 * __va_rounded_size (void *)); \
340 va_arg (arg, type); \
343 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
344 ({ unsigned int pos = argpos; \
345 va_list arg = (va_list) argptr; \
347 (void) va_arg (arg, void *); \
348 va_arg (arg, type); \
353 #ifndef COMPILE_WSCANF
354 if (!isascii ((unsigned char) *f
))
356 /* Non-ASCII, may be a multibyte. */
357 int len
= __mbrlen (f
, strlen (f
), &state
);
365 else if (c
!= (unsigned char) *f
++)
367 ungetc_not_eof (c
, s
);
380 /* Remember to skip spaces. */
387 /* Read a character. */
390 /* Characters other than format specs must just match. */
394 /* We saw white space char as the last character in the format
395 string. Now it's time to skip all leading white space. */
399 if (inchar () == EOF
)
413 /* This is the start of the conversion string. */
416 /* Not yet decided whether we read a pointer or not. */
419 /* Initialize state of modifiers. */
422 /* Prepare temporary buffer. */
425 /* Check for a positional parameter specification. */
426 if (ISDIGIT ((UCHAR_T
) *f
))
428 argpos
= (UCHAR_T
) *f
++ - L_('0');
429 while (ISDIGIT ((UCHAR_T
) *f
))
430 argpos
= argpos
* 10 + ((UCHAR_T
) *f
++ - L_('0'));
435 /* Oops; that was actually the field width. */
443 /* Check for the assignment-suppressing, the number grouping flag,
444 and the signal to use the locale's digit representation. */
445 while (*f
== L_('*') || *f
== L_('\'') || *f
== L_('I'))
459 /* We have seen width. */
460 if (ISDIGIT ((UCHAR_T
) *f
))
463 /* Find the maximum field width. */
465 while (ISDIGIT ((UCHAR_T
) *f
))
468 width
+= (UCHAR_T
) *f
++ - L_('0');
474 /* Check for type modifiers. */
478 /* ints are short ints or chars. */
490 /* A double `l' is equivalent to an `L'. */
492 flags
|= LONGDBL
| LONG
;
495 /* ints are long ints. */
500 /* doubles are long doubles, and ints are long long ints. */
501 flags
|= LONGDBL
| LONG
;
504 /* The `a' is used as a flag only if followed by `s', `S' or
506 if (*f
!= L_('s') && *f
!= L_('S') && *f
!= L_('['))
511 /* String conversions (%s, %[) take a `char **'
512 arg and fill it in with a malloc'd pointer. */
516 if (need_longlong
&& sizeof (size_t) > sizeof (unsigned long int))
518 else if (sizeof (size_t) > sizeof (unsigned int))
522 if (need_longlong
&& sizeof (uintmax_t) > sizeof (unsigned long int))
524 else if (sizeof (uintmax_t) > sizeof (unsigned int))
528 if (need_longlong
&& sizeof (ptrdiff_t) > sizeof (long int))
530 else if (sizeof (ptrdiff_t) > sizeof (int))
534 /* Not a recognized modifier. Backup. */
539 /* End of the format string? */
543 /* Find the conversion specifier. */
545 if (skip_space
|| (fc
!= L_('[') && fc
!= L_('c')
546 && fc
!= L_('C') && fc
!= L_('n')))
548 /* Eat whitespace. */
549 int save_errno
= errno
;
552 if (inchar () == EOF
&& errno
== EINTR
)
562 case L_('%'): /* Must match a literal '%'. */
568 ungetc_not_eof (c
, s
);
573 case L_('n'): /* Answer number of assignments done. */
574 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
575 with the 'n' conversion specifier. */
576 if (!(flags
& SUPPRESS
))
578 /* Don't count the read-ahead. */
579 if (need_longlong
&& (flags
& LONGDBL
))
580 *ARG (long long int *) = read_in
;
581 else if (need_long
&& (flags
& LONG
))
582 *ARG (long int *) = read_in
;
583 else if (flags
& SHORT
)
584 *ARG (short int *) = read_in
;
585 else if (!(flags
& CHAR
))
586 *ARG (int *) = read_in
;
588 *ARG (char *) = read_in
;
590 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
591 /* We have a severe problem here. The ISO C standard
592 contradicts itself in explaining the effect of the %n
593 format in `scanf'. While in ISO C:1990 and the ISO C
594 Amendement 1:1995 the result is described as
596 Execution of a %n directive does not effect the
597 assignment count returned at the completion of
598 execution of the f(w)scanf function.
600 in ISO C Corrigendum 1:1994 the following was added:
603 Add the following fourth example:
606 int d1, d2, n1, n2, i;
607 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
608 the value 123 is assigned to d1 and the value3 to n1.
609 Because %n can never get an input failure the value
610 of 3 is also assigned to n2. The value of d2 is not
611 affected. The value 3 is assigned to i.
613 We go for now with the historically correct code from ISO C,
614 i.e., we don't count the %n assignments. When it ever
615 should proof to be wrong just remove the #ifdef above. */
621 case L_('c'): /* Match characters. */
622 if ((flags
& LONG
) == 0)
624 if (!(flags
& SUPPRESS
))
638 #ifdef COMPILE_WSCANF
639 /* We have to convert the wide character(s) into multibyte
640 characters and store the result. */
641 memset (&state
, '\0', sizeof (state
));
647 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
648 if (n
== (size_t) -1)
649 /* No valid wide character. */
652 /* Increment the output pointer. Even if we don't
656 while (--width
> 0 && inchar () != EOF
);
658 if (!(flags
& SUPPRESS
))
662 while (--width
> 0 && inchar () != EOF
);
665 while (--width
> 0 && inchar () != EOF
);
668 if (!(flags
& SUPPRESS
))
675 if (!(flags
& SUPPRESS
))
677 wstr
= ARG (wchar_t *);
686 #ifdef COMPILE_WSCANF
687 /* Just store the incoming wide characters. */
688 if (!(flags
& SUPPRESS
))
692 while (--width
> 0 && inchar () != EOF
);
695 while (--width
> 0 && inchar () != EOF
);
698 /* We have to convert the multibyte input sequence to wide
703 memset (&cstate
, '\0', sizeof (cstate
));
707 /* This is what we present the mbrtowc function first. */
714 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
717 if (n
== (size_t) -2)
719 /* Possibly correct character, just not enough
721 if (inchar () == EOF
)
731 /* We have a match. */
735 /* Advance the result pointer. */
738 while (--width
> 0 && inchar () != EOF
);
742 if (!(flags
& SUPPRESS
))
747 case L_('s'): /* Read a string. */
750 #define STRING_ARG(Str, Type) \
751 do if (!(flags & SUPPRESS)) \
753 if (flags & MALLOC) \
755 /* The string is to be stored in a malloc'd buffer. */ \
756 strptr = ARG (char **); \
757 if (strptr == NULL) \
759 /* Allocate an initial buffer. */ \
761 *strptr = (char *) malloc (strsize * sizeof (Type)); \
762 Str = (Type *) *strptr; \
765 Str = ARG (Type *); \
769 STRING_ARG (str
, char);
775 #ifdef COMPILE_WSCANF
776 memset (&state
, '\0', sizeof (state
));
783 ungetc_not_eof (c
, s
);
787 #ifdef COMPILE_WSCANF
788 /* This is quite complicated. We have to convert the
789 wide characters into multibyte characters and then
794 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
795 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
797 /* We have to enlarge the buffer if the `a' flag
799 size_t strleng
= str
- *strptr
;
802 newstr
= (char *) realloc (*strptr
, strsize
* 2);
805 /* Can't allocate that much. Last-ditch
807 newstr
= (char *) realloc (*strptr
,
808 strleng
+ MB_CUR_MAX
);
811 /* We lose. Oh well. Terminate the
812 string and stop converting,
813 so at least we don't skip any input. */
814 ((char *) (*strptr
))[strleng
] = '\0';
821 str
= newstr
+ strleng
;
822 strsize
= strleng
+ MB_CUR_MAX
;
828 str
= newstr
+ strleng
;
833 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
,
835 if (n
== (size_t) -1)
838 assert (n
<= MB_CUR_MAX
);
843 if (!(flags
& SUPPRESS
))
847 && (char *) str
== *strptr
+ strsize
)
849 /* Enlarge the buffer. */
850 str
= (char *) realloc (*strptr
, 2 * strsize
);
853 /* Can't allocate that much. Last-ditch
855 str
= (char *) realloc (*strptr
, strsize
+ 1);
858 /* We lose. Oh well. Terminate the
859 string and stop converting,
860 so at least we don't skip any input. */
861 ((char *) (*strptr
))[strsize
- 1] = '\0';
867 *strptr
= (char *) str
;
874 *strptr
= (char *) str
;
882 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
884 if (!(flags
& SUPPRESS
))
886 #ifdef COMPILE_WSCANF
887 /* We have to emit the code to get into the initial
889 char buf
[MB_LEN_MAX
];
890 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
891 if (n
> 0 && (flags
& MALLOC
)
892 && str
+ n
>= *strptr
+ strsize
)
894 /* Enlarge the buffer. */
895 size_t strleng
= str
- *strptr
;
898 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
901 /* We lose. Oh well. Terminate the string
902 and stop converting, so at least we don't
904 ((char *) (*strptr
))[strleng
] = '\0';
911 str
= newstr
+ strleng
;
912 strsize
= strleng
+ n
+ 1;
916 str
= __mempcpy (str
, buf
, n
);
920 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
922 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
935 #ifndef COMPILE_WSCANF
939 /* Wide character string. */
940 STRING_ARG (wstr
, wchar_t);
946 #ifndef COMPILE_WSCANF
947 memset (&cstate
, '\0', sizeof (cstate
));
954 ungetc_not_eof (c
, s
);
958 #ifdef COMPILE_WSCANF
960 if (!(flags
& SUPPRESS
))
964 && wstr
== (wchar_t *) *strptr
+ strsize
)
966 /* Enlarge the buffer. */
967 wstr
= (wchar_t *) realloc (*strptr
,
972 /* Can't allocate that much. Last-ditch
974 wstr
= (wchar_t *) realloc (*strptr
,
979 /* We lose. Oh well. Terminate the string
980 and stop converting, so at least we don't
982 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
988 *strptr
= (char *) wstr
;
995 *strptr
= (char *) wstr
;
1011 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
1014 if (n
== (size_t) -2)
1016 /* Possibly correct character, just not enough
1018 if (inchar () == EOF
)
1028 /* We have a match. */
1033 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
1034 && wstr
== (wchar_t *) *strptr
+ strsize
)
1036 /* Enlarge the buffer. */
1037 wstr
= (wchar_t *) realloc (*strptr
,
1039 * sizeof (wchar_t)));
1042 /* Can't allocate that much. Last-ditch effort. */
1043 wstr
= (wchar_t *) realloc (*strptr
,
1045 * sizeof (wchar_t)));
1048 /* We lose. Oh well. Terminate the
1049 string and stop converting, so at
1050 least we don't skip any input. */
1051 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1057 *strptr
= (char *) wstr
;
1064 *strptr
= (char *) wstr
;
1072 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
1074 if (!(flags
& SUPPRESS
))
1078 if ((flags
& MALLOC
) && wstr
- (wchar_t *) *strptr
!= strsize
)
1080 wchar_t *cp
= (wchar_t *) realloc (*strptr
,
1082 - (wchar_t *) *strptr
)
1083 * sizeof(wchar_t)));
1085 *strptr
= (char *) cp
;
1093 case L_('x'): /* Hexadecimal integer. */
1094 case L_('X'): /* Ditto. */
1099 case L_('o'): /* Octal integer. */
1104 case L_('u'): /* Unsigned decimal integer. */
1109 case L_('d'): /* Signed decimal integer. */
1114 case L_('i'): /* Generic number. */
1123 /* Check for a sign. */
1124 if (c
== L_('-') || c
== L_('+'))
1132 /* Look for a leading indication of base. */
1133 if (width
!= 0 && c
== L_('0'))
1141 if (width
!= 0 && TOLOWER (c
) == L_('x'))
1159 if (base
== 10 && (flags
& I18N
) != 0)
1164 #ifdef COMPILE_WSCANF
1165 const wchar_t *wcdigits
[10];
1166 const wchar_t *wcdigits_extended
[10];
1168 const char *mbdigits
[10];
1169 const char *mbdigits_extended
[10];
1171 /* "to_inpunct" is a map from ASCII digits to their
1172 equivalent in locale. This is defined for locales
1173 which use an extra digits set. */
1174 wctrans_t map
= __wctrans ("to_inpunct");
1178 #ifdef COMPILE_WSCANF
1179 to_level
= _NL_CURRENT_WORD (LC_CTYPE
,
1180 _NL_CTYPE_INDIGITS_WC_LEN
) - 1;
1182 to_level
= (uint32_t) curctype
->values
[_NL_ITEM_INDEX (_NL_CTYPE_INDIGITS_MB_LEN
)].word
- 1;
1185 /* Get the alternative digit forms if there are any. */
1186 if (__builtin_expect (map
!= NULL
, 0))
1188 /* Adding new level for extra digits set in locale file. */
1191 for (n
= 0; n
< 10; ++n
)
1193 #ifdef COMPILE_WSCANF
1194 wcdigits
[n
] = (const wchar_t *)
1195 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1197 wchar_t *wc_extended
= (wchar_t *)
1198 alloca ((to_level
+ 2) * sizeof (wchar_t));
1199 __wmemcpy (wc_extended
, wcdigits
[n
], to_level
);
1200 wc_extended
[to_level
] = __towctrans (L
'0' + n
, map
);
1201 wc_extended
[to_level
+ 1] = '\0';
1202 wcdigits_extended
[n
] = wc_extended
;
1205 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1207 /* Get the equivalent wide char in map. */
1208 wint_t extra_wcdigit
= __towctrans (L
'0' + n
, map
);
1210 /* Convert it to multibyte representation. */
1212 memset (&state
, '\0', sizeof (state
));
1214 char extra_mbdigit
[MB_LEN_MAX
];
1216 = __wcrtomb (extra_mbdigit
, extra_wcdigit
, &state
);
1218 if (mblen
== (size_t) -1)
1220 /* Ignore this new level. */
1225 /* Calculate the length of mbdigits[n]. */
1226 const char *last_char
= mbdigits
[n
];
1227 for (level
= 0; level
< to_level
; ++level
)
1228 last_char
= strchr (last_char
, '\0') + 1;
1230 size_t mbdigits_len
= last_char
- mbdigits
[n
];
1232 /* Allocate memory for extended multibyte digit. */
1234 mb_extended
= (char *) alloca (mbdigits_len
+ mblen
+ 1);
1236 /* And get the mbdigits + extra_digit string. */
1237 *(char *) __mempcpy (__mempcpy (mb_extended
, mbdigits
[n
],
1239 extra_mbdigit
, mblen
) = '\0';
1240 mbdigits_extended
[n
] = mb_extended
;
1245 /* Read the number into workspace. */
1246 while (c
!= EOF
&& width
!= 0)
1248 /* In this round we get the pointer to the digit strings
1249 and also perform the first round of comparisons. */
1250 for (n
= 0; n
< 10; ++n
)
1252 /* Get the string for the digits with value N. */
1253 #ifdef COMPILE_WSCANF
1254 if (__builtin_expect (map
!= NULL
, 0))
1255 wcdigits
[n
] = wcdigits_extended
[n
];
1257 wcdigits
[n
] = (const wchar_t *)
1258 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1259 wcdigits
[n
] += from_level
;
1261 if (c
== (wint_t) *wcdigits
[n
])
1263 to_level
= from_level
;
1267 /* Advance the pointer to the next string. */
1271 int avail
= width
> 0 ? width
: INT_MAX
;
1273 if (__builtin_expect (map
!= NULL
, 0))
1274 mbdigits
[n
] = mbdigits_extended
[n
];
1277 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1279 for (level
= 0; level
< from_level
; level
++)
1280 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1283 while ((unsigned char) *cmpp
== c
&& avail
> 0)
1285 if (*++cmpp
== '\0')
1289 if ((c
= inchar ()) == EOF
)
1299 to_level
= from_level
;
1303 /* We are pushing all read characters back. */
1304 if (cmpp
> mbdigits
[n
])
1307 while (--cmpp
> mbdigits
[n
])
1308 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1309 c
= (unsigned char) *cmpp
;
1312 /* Advance the pointer to the next string. */
1313 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1319 /* Have not yet found the digit. */
1320 for (level
= from_level
+ 1; level
<= to_level
; ++level
)
1322 /* Search all ten digits of this level. */
1323 for (n
= 0; n
< 10; ++n
)
1325 #ifdef COMPILE_WSCANF
1326 if (c
== (wint_t) *wcdigits
[n
])
1329 /* Advance the pointer to the next string. */
1333 int avail
= width
> 0 ? width
: INT_MAX
;
1336 while ((unsigned char) *cmpp
== c
&& avail
> 0)
1338 if (*++cmpp
== '\0')
1342 if ((c
= inchar ()) == EOF
)
1355 /* We are pushing all read characters back. */
1356 if (cmpp
> mbdigits
[n
])
1359 while (--cmpp
> mbdigits
[n
])
1360 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1361 c
= (unsigned char) *cmpp
;
1364 /* Advance the pointer to the next string. */
1365 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1381 else if ((flags
& GROUP
)
1382 #ifdef COMPILE_WSCANF
1383 && thousands
!= L
'\0'
1385 && thousands
!= NULL
1389 /* Try matching against the thousands separator. */
1390 #ifdef COMPILE_WSCANF
1394 const char *cmpp
= thousands
;
1395 int avail
= width
> 0 ? width
: INT_MAX
;
1397 while ((unsigned char) *cmpp
== c
&& avail
> 0)
1400 if (*++cmpp
== '\0')
1404 if ((c
= inchar ()) == EOF
)
1412 /* We are pushing all read characters back. */
1413 if (cmpp
> thousands
)
1415 wpsize
-= cmpp
- thousands
;
1417 while (--cmpp
> thousands
)
1418 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1419 c
= (unsigned char) *cmpp
;
1427 /* The last thousands character will be added back by
1443 /* Read the number into workspace. */
1444 while (c
!= EOF
&& width
!= 0)
1451 else if (!ISDIGIT (c
) || (int) (c
- L_('0')) >= base
)
1453 if (base
== 10 && (flags
& GROUP
)
1454 #ifdef COMPILE_WSCANF
1455 && thousands
!= L
'\0'
1457 && thousands
!= NULL
1461 /* Try matching against the thousands separator. */
1462 #ifdef COMPILE_WSCANF
1466 const char *cmpp
= thousands
;
1467 int avail
= width
> 0 ? width
: INT_MAX
;
1469 while ((unsigned char) *cmpp
== c
&& avail
> 0)
1472 if (*++cmpp
== '\0')
1476 if ((c
= inchar ()) == EOF
)
1484 /* We are pushing all read characters back. */
1485 if (cmpp
> thousands
)
1487 wpsize
-= cmpp
- thousands
;
1489 while (--cmpp
> thousands
)
1490 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1491 c
= (unsigned char) *cmpp
;
1499 /* The last thousands character will be added back by
1515 || (wpsize
== 1 && (wp
[0] == L_('+') || wp
[0] == L_('-'))))
1517 /* There was no number. If we are supposed to read a pointer
1518 we must recognize "(nil)" as well. */
1519 if (wpsize
== 0 && read_pointer
&& (width
< 0 || width
>= 0)
1521 && TOLOWER (inchar ()) == L_('n')
1522 && TOLOWER (inchar ()) == L_('i')
1523 && TOLOWER (inchar ()) == L_('l')
1524 && inchar () == L_(')'))
1525 /* We must produce the value of a NULL pointer. A single
1526 '0' digit is enough. */
1530 /* The last read character is not part of the number
1538 /* The just read character is not part of the number anymore. */
1541 /* Convert the number. */
1543 if (need_longlong
&& (flags
& LONGDBL
))
1546 num
.q
= __strtoll_internal (wp
, &tw
, base
, flags
& GROUP
);
1548 num
.uq
= __strtoull_internal (wp
, &tw
, base
, flags
& GROUP
);
1553 num
.l
= __strtol_internal (wp
, &tw
, base
, flags
& GROUP
);
1555 num
.ul
= __strtoul_internal (wp
, &tw
, base
, flags
& GROUP
);
1560 if (!(flags
& SUPPRESS
))
1562 if (! number_signed
)
1564 if (need_longlong
&& (flags
& LONGDBL
))
1565 *ARG (unsigned LONGLONG
int *) = num
.uq
;
1566 else if (need_long
&& (flags
& LONG
))
1567 *ARG (unsigned long int *) = num
.ul
;
1568 else if (flags
& SHORT
)
1569 *ARG (unsigned short int *)
1570 = (unsigned short int) num
.ul
;
1571 else if (!(flags
& CHAR
))
1572 *ARG (unsigned int *) = (unsigned int) num
.ul
;
1574 *ARG (unsigned char *) = (unsigned char) num
.ul
;
1578 if (need_longlong
&& (flags
& LONGDBL
))
1579 *ARG (LONGLONG
int *) = num
.q
;
1580 else if (need_long
&& (flags
& LONG
))
1581 *ARG (long int *) = num
.l
;
1582 else if (flags
& SHORT
)
1583 *ARG (short int *) = (short int) num
.l
;
1584 else if (!(flags
& CHAR
))
1585 *ARG (int *) = (int) num
.l
;
1587 *ARG (signed char *) = (signed char) num
.ul
;
1593 case L_('e'): /* Floating-point numbers. */
1605 /* Check for a sign. */
1606 if (c
== L_('-') || c
== L_('+'))
1608 negative
= c
== L_('-');
1609 if (width
== 0 || inchar () == EOF
)
1610 /* EOF is only an input error before we read any chars. */
1612 if (! ISDIGIT (c
) && TOLOWER (c
) != L_('i')
1613 && TOLOWER (c
) != L_('n'))
1615 #ifdef COMPILE_WSCANF
1618 /* This is no valid number. */
1623 /* Match against the decimal point. At this point
1624 we are taking advantage of the fact that we can
1625 push more than one character back. This is
1626 (almost) never necessary since the decimal point
1627 string hopefully never contains more than one
1629 const char *cmpp
= decimal
;
1630 int avail
= width
> 0 ? width
: INT_MAX
;
1632 while ((unsigned char) *cmpp
== c
&& avail
> 0)
1633 if (*++cmpp
== '\0')
1637 if (inchar () == EOF
)
1644 /* This is no valid number. */
1648 if (cmpp
== decimal
)
1650 c
= (unsigned char) *--cmpp
;
1665 /* Take care for the special arguments "nan" and "inf". */
1666 if (TOLOWER (c
) == L_('n'))
1670 if (width
== 0 || inchar () == EOF
|| TOLOWER (c
) != L_('a'))
1675 if (width
== 0 || inchar () == EOF
|| TOLOWER (c
) != L_('n'))
1683 else if (TOLOWER (c
) == L_('i'))
1685 /* Maybe "inf" or "infinity". */
1687 if (width
== 0 || inchar () == EOF
|| TOLOWER (c
) != L_('n'))
1692 if (width
== 0 || inchar () == EOF
|| TOLOWER (c
) != L_('f'))
1697 /* It is as least "inf". */
1698 if (width
!= 0 && inchar () != EOF
)
1700 if (TOLOWER (c
) == L_('i'))
1704 /* Now we have to read the rest as well. */
1706 if (width
== 0 || inchar () == EOF
1707 || TOLOWER (c
) != L_('n'))
1712 if (width
== 0 || inchar () == EOF
1713 || TOLOWER (c
) != L_('i'))
1718 if (width
== 0 || inchar () == EOF
1719 || TOLOWER (c
) != L_('t'))
1724 if (width
== 0 || inchar () == EOF
1725 || TOLOWER (c
) != L_('y'))
1740 if (width
!= 0 && c
== L_('0'))
1746 if (width
!= 0 && TOLOWER (c
) == L_('x'))
1748 /* It is a number in hexadecimal format. */
1754 /* Grouping is not allowed. */
1762 got_dot
= got_e
= 0;
1767 else if (!got_e
&& is_hexa
&& ISXDIGIT (c
))
1769 else if (got_e
&& wp
[wpsize
- 1] == exp_char
1770 && (c
== L_('-') || c
== L_('+')))
1772 else if (wpsize
> 0 && !got_e
1773 && (CHAR_T
) TOLOWER (c
) == exp_char
)
1776 got_e
= got_dot
= 1;
1780 #ifdef COMPILE_WSCANF
1781 if (! got_dot
&& c
== decimal
)
1786 else if ((flags
& GROUP
) != 0 && thousands
!= L
'\0'
1787 && ! got_dot
&& c
== thousands
)
1791 /* The last read character is not part of the number
1797 const char *cmpp
= decimal
;
1798 int avail
= width
> 0 ? width
: INT_MAX
;
1802 while ((unsigned char) *cmpp
== c
&& avail
> 0)
1803 if (*++cmpp
== '\0')
1807 if (inchar () == EOF
)
1815 /* Add all the characters. */
1816 for (cmpp
= decimal
; *cmpp
!= '\0'; ++cmpp
)
1817 ADDW ((unsigned char) *cmpp
);
1824 /* Figure out whether it is a thousands separator.
1825 There is one problem: we possibly read more than
1826 one character. We cannot push them back but since
1827 we know that parts of the `decimal' string matched,
1828 we can compare against it. */
1829 const char *cmp2p
= thousands
;
1831 if ((flags
& GROUP
) != 0 && thousands
!= NULL
1834 while (cmp2p
- thousands
< cmpp
- decimal
1835 && *cmp2p
== decimal
[cmp2p
- thousands
])
1837 if (cmp2p
- thousands
== cmpp
- decimal
)
1839 while ((unsigned char) *cmp2p
== c
&& avail
> 0)
1840 if (*++cmp2p
== '\0')
1844 if (inchar () == EOF
)
1851 if (cmp2p
!= NULL
&& *cmp2p
== '\0')
1853 /* Add all the characters. */
1854 for (cmpp
= thousands
; *cmpp
!= '\0'; ++cmpp
)
1855 ADDW ((unsigned char) *cmpp
);
1861 /* The last read character is not part of the number
1872 while (width
!= 0 && inchar () != EOF
);
1874 /* Have we read any character? If we try to read a number
1875 in hexadecimal notation and we have read only the `0x'
1876 prefix or no exponent this is an error. */
1877 if (wpsize
== 0 || (is_hexa
&& (wpsize
== 2 || ! got_e
)))
1881 /* Convert the number. */
1883 if (flags
& LONGDBL
)
1885 long double d
= __strtold_internal (wp
, &tw
, flags
& GROUP
);
1886 if (!(flags
& SUPPRESS
) && tw
!= wp
)
1887 *ARG (long double *) = negative
? -d
: d
;
1889 else if (flags
& LONG
)
1891 double d
= __strtod_internal (wp
, &tw
, flags
& GROUP
);
1892 if (!(flags
& SUPPRESS
) && tw
!= wp
)
1893 *ARG (double *) = negative
? -d
: d
;
1897 float d
= __strtof_internal (wp
, &tw
, flags
& GROUP
);
1898 if (!(flags
& SUPPRESS
) && tw
!= wp
)
1899 *ARG (float *) = negative
? -d
: d
;
1905 if (!(flags
& SUPPRESS
))
1909 case L_('['): /* Character class. */
1911 STRING_ARG (wstr
, wchar_t);
1913 STRING_ARG (str
, char);
1924 /* There is no width given so there is also no limit on the
1925 number of characters we read. Therefore we set width to
1926 a very high value to make the algorithm easier. */
1929 #ifdef COMPILE_WSCANF
1930 /* Find the beginning and the end of the scanlist. We are not
1931 creating a lookup table since it would have to be too large.
1932 Instead we search each time through the string. This is not
1933 a constant lookup time but who uses this feature deserves to
1935 tw
= (wchar_t *) f
; /* Marks the beginning. */
1940 while ((fc
= *f
++) != L
'\0' && fc
!= L
']');
1944 wp
= (wchar_t *) f
- 1;
1946 /* Fill WP with byte flags indexed by character.
1947 We will use this flag map for matching input characters. */
1948 if (wpmax
< UCHAR_MAX
+ 1)
1950 wpmax
= UCHAR_MAX
+ 1;
1951 wp
= (char *) alloca (wpmax
);
1953 memset (wp
, '\0', UCHAR_MAX
+ 1);
1956 if (fc
== ']' || fc
== '-')
1958 /* If ] or - appears before any char in the set, it is not
1959 the terminator or separator, but the first char in the
1965 while ((fc
= *f
++) != '\0' && fc
!= ']')
1966 if (fc
== '-' && *f
!= '\0' && *f
!= ']'
1967 && (unsigned char) f
[-2] <= (unsigned char) *f
)
1969 /* Add all characters from the one before the '-'
1970 up to (but not including) the next format char. */
1971 for (fc
= (unsigned char) f
[-2]; fc
< (unsigned char) *f
; ++fc
)
1975 /* Add the character to the flag map. */
1984 size_t now
= read_in
;
1985 #ifdef COMPILE_WSCANF
1986 if (inchar () == WEOF
)
1993 /* Test whether it's in the scanlist. */
1997 if (runp
[0] == L
'-' && runp
[1] != '\0' && runp
+ 1 != wp
1999 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2001 /* Match against all characters in between the
2002 first and last character of the sequence. */
2005 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2006 if ((wint_t) wc
== c
)
2009 if (wc
<= runp
[1] && !not_in
)
2011 if (wc
<= runp
[1] && not_in
)
2013 /* The current character is not in the
2023 if ((wint_t) *runp
== c
&& !not_in
)
2025 if ((wint_t) *runp
== c
&& not_in
)
2035 if (runp
== wp
&& !not_in
)
2041 if (!(flags
& SUPPRESS
))
2045 if ((flags
& MALLOC
)
2046 && wstr
== (wchar_t *) *strptr
+ strsize
)
2048 /* Enlarge the buffer. */
2049 wstr
= (wchar_t *) realloc (*strptr
,
2051 * sizeof (wchar_t));
2054 /* Can't allocate that much. Last-ditch
2057 realloc (*strptr
, (strsize
+ 1)
2058 * sizeof (wchar_t));
2061 /* We lose. Oh well. Terminate the string
2062 and stop converting, so at least we don't
2064 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2070 *strptr
= (char *) wstr
;
2077 *strptr
= (char *) wstr
;
2084 while (--width
> 0 && inchar () != WEOF
);
2087 char buf
[MB_LEN_MAX
];
2091 if (inchar () == EOF
)
2094 memset (&cstate
, '\0', sizeof (cstate
));
2098 if (wp
[c
] == not_in
)
2100 ungetc_not_eof (c
, s
);
2105 if (!(flags
& SUPPRESS
))
2109 /* Convert it into a wide character. */
2111 n
= __mbrtowc (wstr
, buf
, 1, &cstate
);
2113 if (n
== (size_t) -2)
2115 /* Possibly correct character, just not enough
2118 assert (cnt
< MB_CUR_MAX
);
2124 if ((flags
& MALLOC
)
2125 && wstr
== (wchar_t *) *strptr
+ strsize
)
2127 /* Enlarge the buffer. */
2128 wstr
= (wchar_t *) realloc (*strptr
,
2130 * sizeof (wchar_t)));
2133 /* Can't allocate that much. Last-ditch
2136 realloc (*strptr
, ((strsize
+ 1)
2137 * sizeof (wchar_t)));
2140 /* We lose. Oh well. Terminate the
2141 string and stop converting,
2142 so at least we don't skip any input. */
2143 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2149 *strptr
= (char *) wstr
;
2156 *strptr
= (char *) wstr
;
2166 while (inchar () != EOF
);
2169 /* We stopped in the middle of recognizing another
2170 character. That's a problem. */
2175 /* We haven't succesfully read any character. */
2178 if (!(flags
& SUPPRESS
))
2182 if ((flags
& MALLOC
)
2183 && wstr
- (wchar_t *) *strptr
!= strsize
)
2185 wchar_t *cp
= (wchar_t *)
2186 realloc (*strptr
, ((wstr
- (wchar_t *) *strptr
)
2187 * sizeof(wchar_t)));
2189 *strptr
= (char *) cp
;
2197 size_t now
= read_in
;
2199 if (inchar () == EOF
)
2202 #ifdef COMPILE_WSCANF
2204 memset (&state
, '\0', sizeof (state
));
2211 /* Test whether it's in the scanlist. */
2215 if (runp
[0] == L
'-' && runp
[1] != '\0' && runp
+ 1 != wp
2217 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2219 /* Match against all characters in between the
2220 first and last character of the sequence. */
2223 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2224 if ((wint_t) wc
== c
)
2227 if (wc
<= runp
[1] && !not_in
)
2229 if (wc
<= runp
[1] && not_in
)
2231 /* The current character is not in the
2241 if ((wint_t) *runp
== c
&& !not_in
)
2243 if ((wint_t) *runp
== c
&& not_in
)
2253 if (runp
== wp
&& !not_in
)
2259 if (!(flags
& SUPPRESS
))
2261 if ((flags
& MALLOC
)
2262 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
2264 /* Enlarge the buffer. */
2265 size_t strleng
= str
- *strptr
;
2268 newstr
= (char *) realloc (*strptr
, 2 * strsize
);
2271 /* Can't allocate that much. Last-ditch
2273 newstr
= (char *) realloc (*strptr
,
2274 strleng
+ MB_CUR_MAX
);
2277 /* We lose. Oh well. Terminate the string
2278 and stop converting, so at least we don't
2280 ((char *) (*strptr
))[strleng
] = '\0';
2287 str
= newstr
+ strleng
;
2288 strsize
= strleng
+ MB_CUR_MAX
;
2294 str
= newstr
+ strleng
;
2300 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
2301 if (n
== (size_t) -1)
2304 assert (n
<= MB_CUR_MAX
);
2307 while (--width
> 0 && inchar () != WEOF
);
2312 if (wp
[c
] == not_in
)
2314 ungetc_not_eof (c
, s
);
2319 if (!(flags
& SUPPRESS
))
2322 if ((flags
& MALLOC
)
2323 && (char *) str
== *strptr
+ strsize
)
2325 /* Enlarge the buffer. */
2326 size_t newsize
= 2 * strsize
;
2329 str
= (char *) realloc (*strptr
, newsize
);
2332 /* Can't allocate that much. Last-ditch
2334 if (newsize
> strsize
+ 1)
2336 newsize
= strsize
+ 1;
2339 /* We lose. Oh well. Terminate the
2340 string and stop converting,
2341 so at least we don't skip any input. */
2342 ((char *) (*strptr
))[strsize
- 1] = '\0';
2348 *strptr
= (char *) str
;
2355 while (--width
> 0 && inchar () != EOF
);
2359 /* We haven't succesfully read any character. */
2362 if (!(flags
& SUPPRESS
))
2364 #ifdef COMPILE_WSCANF
2365 /* We have to emit the code to get into the initial
2367 char buf
[MB_LEN_MAX
];
2368 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
2369 if (n
> 0 && (flags
& MALLOC
)
2370 && str
+ n
>= *strptr
+ strsize
)
2372 /* Enlarge the buffer. */
2373 size_t strleng
= str
- *strptr
;
2376 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
2379 /* We lose. Oh well. Terminate the string
2380 and stop converting, so at least we don't
2382 ((char *) (*strptr
))[strleng
] = '\0';
2389 str
= newstr
+ strleng
;
2390 strsize
= strleng
+ n
+ 1;
2394 str
= __mempcpy (str
, buf
, n
);
2398 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
2400 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
2410 case L_('p'): /* Generic pointer. */
2412 /* A PTR must be the same size as a `long int'. */
2413 flags
&= ~(SHORT
|LONGDBL
);
2421 /* If this is an unknown format character punt. */
2426 /* The last thing we saw int the format string was a white space.
2427 Consume the last white spaces. */
2432 while (ISSPACE (c
));
2437 /* Unlock stream. */
2446 #ifdef COMPILE_WSCANF
2448 __vfwscanf (FILE *s
, const wchar_t *format
, va_list argptr
)
2450 return _IO_vfwscanf (s
, format
, argptr
, NULL
);
2454 __vfscanf (FILE *s
, const char *format
, va_list argptr
)
2456 return INTUSE(_IO_vfscanf
) (s
, format
, argptr
, NULL
);
2458 libc_hidden_def (__vfscanf
)
2461 #ifdef COMPILE_WSCANF
2462 weak_alias (__vfwscanf
, vfwscanf
)
2464 weak_alias (__vfscanf
, vfscanf
)