1 /* Copyright (C) 1991-2006, 2007 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 0x0001 /* l: long or double */
56 #define LONGDBL 0x0002 /* L: long long or long double */
57 #define SHORT 0x0004 /* h: short */
58 #define SUPPRESS 0x0008 /* *: suppress assignment */
59 #define POINTER 0x0010 /* weird %p pointer (`fake hex') */
60 #define NOSKIP 0x0020 /* do not skip blanks */
61 #define NUMBER_SIGNED 0x0040 /* signed integer */
62 #define GROUP 0x0080 /* ': group numbers */
63 #define MALLOC 0x0100 /* a: malloc strings */
64 #define CHAR 0x0200 /* hh: char */
65 #define I18N 0x0400 /* I: use locale's digits */
66 #define HEXA_FLOAT 0x0800 /* hexadecimal float */
67 #define READ_POINTER 0x1000 /* this is a pointer value */
70 #include <locale/localeinfo.h>
75 #define va_list _IO_va_list
78 # define ungetc(c, s) ((void) (c == WEOF \
80 INTUSE(_IO_sputbackwc) (s, c))))
81 # define ungetc_not_eof(c, s) ((void) (--read_in, \
82 INTUSE(_IO_sputbackwc) (s, c)))
83 # define inchar() (c == WEOF ? ((errno = inchar_errno), WEOF) \
84 : ((c = _IO_getwc_unlocked (s)), \
87 : (size_t) (inchar_errno = errno)), c))
89 # define MEMCPY(d, s, n) __wmemcpy (d, s, n)
90 # define ISSPACE(Ch) iswspace (Ch)
91 # define ISDIGIT(Ch) iswdigit (Ch)
92 # define ISXDIGIT(Ch) iswxdigit (Ch)
93 # define TOLOWER(Ch) towlower (Ch)
94 # define ORIENT if (_IO_fwide (s, 1) != 1) return WEOF
95 # define __strtoll_internal __wcstoll_internal
96 # define __strtoull_internal __wcstoull_internal
97 # define __strtol_internal __wcstol_internal
98 # define __strtoul_internal __wcstoul_internal
99 # define __strtold_internal __wcstold_internal
100 # define __strtod_internal __wcstod_internal
101 # define __strtof_internal __wcstof_internal
103 # define L_(Str) L##Str
104 # define CHAR_T wchar_t
105 # define UCHAR_T unsigned int
106 # define WINT_T wint_t
110 # define ungetc(c, s) ((void) ((int) c == EOF \
112 INTUSE(_IO_sputbackc) (s, (unsigned char) c))))
113 # define ungetc_not_eof(c, s) ((void) (--read_in, \
114 INTUSE(_IO_sputbackc) (s, (unsigned char) c)))
115 # define inchar() (c == EOF ? ((errno = inchar_errno), EOF) \
116 : ((c = _IO_getc_unlocked (s)), \
119 : (size_t) (inchar_errno = errno)), c))
120 # define MEMCPY(d, s, n) memcpy (d, s, n)
121 # define ISSPACE(Ch) __isspace_l (Ch, loc)
122 # define ISDIGIT(Ch) __isdigit_l (Ch, loc)
123 # define ISXDIGIT(Ch) __isxdigit_l (Ch, loc)
124 # define TOLOWER(Ch) __tolower_l ((unsigned char) (Ch), loc)
125 # define ORIENT if (_IO_vtable_offset (s) == 0 \
126 && _IO_fwide (s, -1) != -1) \
131 # define UCHAR_T unsigned char
135 #define encode_error() do { \
137 __set_errno (EILSEQ); \
140 #define conv_error() do { \
144 #define input_error() do { \
146 if (done == 0) done = EOF; \
149 #define ARGCHECK(s, format) \
152 /* Check file argument for consistence. */ \
153 CHECK_FILE (s, EOF); \
154 if (s->_flags & _IO_NO_READS) \
156 __set_errno (EBADF); \
159 else if (format == NULL) \
165 #define LOCK_STREAM(S) \
166 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, (S)); \
168 #define UNLOCK_STREAM(S) \
169 _IO_funlockfile (S); \
170 __libc_cleanup_region_end (0)
173 /* Read formatted input from S according to the format string
174 FORMAT, using the argument list in ARG.
175 Return the number of assignments made, or -1 for an input error. */
176 #ifdef COMPILE_WSCANF
178 _IO_vfwscanf (_IO_FILE
*s
, const wchar_t *format
, _IO_va_list argptr
,
182 _IO_vfscanf_internal (_IO_FILE
*s
, const char *format
, _IO_va_list argptr
,
187 register const CHAR_T
*f
= format
;
188 register UCHAR_T fc
; /* Current character of the format. */
189 register WINT_T done
= 0; /* Assignments done. */
190 register size_t read_in
= 0; /* Chars read in. */
191 register WINT_T c
= 0; /* Last char read. */
192 register int width
; /* Maximum field width. */
193 register int flags
; /* Modifiers for current format element. */
195 #ifndef COMPILE_WSCANF
196 __locale_t loc
= _NL_CURRENT_LOCALE
;
197 struct locale_data
*const curctype
= loc
->__locales
[LC_CTYPE
];
200 /* Errno of last failed inchar call. */
201 int inchar_errno
= 0;
202 /* Status for reading F-P nums. */
203 char got_dot
, got_e
, negative
;
204 /* If a [...] is a [^...]. */
206 #define exp_char not_in
207 /* Base for integral numbers. */
209 /* Decimal point character. */
210 #ifdef COMPILE_WSCANF
215 /* The thousands character of the current locale. */
216 #ifdef COMPILE_WSCANF
219 const char *thousands
;
221 /* State for the conversions. */
223 /* Integral holding variables. */
227 unsigned long long int uq
;
229 unsigned long int ul
;
231 /* Character-buffer pointer. */
233 wchar_t *wstr
= NULL
;
234 char **strptr
= NULL
;
236 /* We must not react on white spaces immediately because they can
237 possibly be matched even if in the input stream no character is
238 available anymore. */
241 CHAR_T
*tw
; /* Temporary pointer. */
242 CHAR_T
*wp
= NULL
; /* Workspace. */
243 size_t wpmax
= 0; /* Maximal size of workspace. */
244 size_t wpsize
; /* Currently used bytes in workspace. */
248 if (wpsize == wpmax) \
251 wpmax = (UCHAR_MAX + 1 > 2 * wpmax ? UCHAR_MAX + 1 : 2 * wpmax); \
252 wp = (CHAR_T *) alloca (wpmax * sizeof (wchar_t)); \
254 MEMCPY (wp, old, wpsize); \
256 wp[wpsize++] = (Ch); \
261 __va_copy (arg
, argptr
);
263 arg
= (va_list) argptr
;
270 ARGCHECK (s
, format
);
273 #ifndef COMPILE_WSCANF
274 struct locale_data
*const curnumeric
= loc
->__locales
[LC_NUMERIC
];
277 /* Figure out the decimal point character. */
278 #ifdef COMPILE_WSCANF
279 decimal
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
281 decimal
= curnumeric
->values
[_NL_ITEM_INDEX (DECIMAL_POINT
)].string
;
283 /* Figure out the thousands separator character. */
284 #ifdef COMPILE_WSCANF
285 thousands
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_THOUSANDS_SEP_WC
);
287 thousands
= curnumeric
->values
[_NL_ITEM_INDEX (THOUSANDS_SEP
)].string
;
288 if (*thousands
== '\0')
293 /* Lock the stream. */
297 #ifndef COMPILE_WSCANF
298 /* From now on we use `state' to convert the format string. */
299 memset (&state
, '\0', sizeof (state
));
302 /* Run through the format string. */
306 /* Extract the next argument, which is of type TYPE.
307 For a %N$... spec, this is the Nth argument from the beginning;
308 otherwise it is the next argument after the state now in ARG. */
310 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
311 ({ unsigned int pos = argpos; \
313 __va_copy (arg, argptr); \
315 (void) va_arg (arg, void *); \
316 va_arg (arg, type); \
320 /* XXX Possible optimization. */
321 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
322 ({ va_list arg = (va_list) argptr; \
323 arg = (va_list) ((char *) arg \
325 * __va_rounded_size (void *)); \
326 va_arg (arg, type); \
329 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
330 ({ unsigned int pos = argpos; \
331 va_list arg = (va_list) argptr; \
333 (void) va_arg (arg, void *); \
334 va_arg (arg, type); \
339 #ifndef COMPILE_WSCANF
340 if (!isascii ((unsigned char) *f
))
342 /* Non-ASCII, may be a multibyte. */
343 int len
= __mbrlen (f
, strlen (f
), &state
);
349 if (__builtin_expect (c
== EOF
, 0))
351 else if (c
!= (unsigned char) *f
++)
353 ungetc_not_eof (c
, s
);
366 /* Remember to skip spaces. */
373 /* Read a character. */
376 /* Characters other than format specs must just match. */
377 if (__builtin_expect (c
== EOF
, 0))
380 /* We saw white space char as the last character in the format
381 string. Now it's time to skip all leading white space. */
385 if (__builtin_expect (inchar () == EOF
, 0))
390 if (__builtin_expect (c
!= fc
, 0))
399 /* This is the start of the conversion string. */
402 /* Initialize state of modifiers. */
405 /* Prepare temporary buffer. */
408 /* Check for a positional parameter specification. */
409 if (ISDIGIT ((UCHAR_T
) *f
))
411 argpos
= (UCHAR_T
) *f
++ - L_('0');
412 while (ISDIGIT ((UCHAR_T
) *f
))
413 argpos
= argpos
* 10 + ((UCHAR_T
) *f
++ - L_('0'));
418 /* Oops; that was actually the field width. */
425 /* Check for the assignment-suppressing, the number grouping flag,
426 and the signal to use the locale's digit representation. */
427 while (*f
== L_('*') || *f
== L_('\'') || *f
== L_('I'))
434 #ifdef COMPILE_WSCANF
435 if (thousands
!= L
'\0')
437 if (thousands
!= NULL
)
446 /* Find the maximum field width. */
448 while (ISDIGIT ((UCHAR_T
) *f
))
451 width
+= (UCHAR_T
) *f
++ - L_('0');
457 /* Check for type modifiers. */
461 /* ints are short ints or chars. */
473 /* A double `l' is equivalent to an `L'. */
475 flags
|= LONGDBL
| LONG
;
478 /* ints are long ints. */
483 /* doubles are long doubles, and ints are long long ints. */
484 flags
|= LONGDBL
| LONG
;
487 /* The `a' is used as a flag only if followed by `s', `S' or
489 if (*f
!= L_('s') && *f
!= L_('S') && *f
!= L_('['))
494 /* String conversions (%s, %[) take a `char **'
495 arg and fill it in with a malloc'd pointer. */
499 if (need_longlong
&& sizeof (size_t) > sizeof (unsigned long int))
501 else if (sizeof (size_t) > sizeof (unsigned int))
505 if (need_longlong
&& sizeof (uintmax_t) > sizeof (unsigned long int))
507 else if (sizeof (uintmax_t) > sizeof (unsigned int))
511 if (need_longlong
&& sizeof (ptrdiff_t) > sizeof (long int))
513 else if (sizeof (ptrdiff_t) > sizeof (int))
517 /* Not a recognized modifier. Backup. */
522 /* End of the format string? */
523 if (__builtin_expect (*f
== L_('\0'), 0))
526 /* Find the conversion specifier. */
528 if (skip_space
|| (fc
!= L_('[') && fc
!= L_('c')
529 && fc
!= L_('C') && fc
!= L_('n')))
531 /* Eat whitespace. */
532 int save_errno
= errno
;
535 if (__builtin_expect (inchar () == EOF
&& errno
== EINTR
, 0))
545 case L_('%'): /* Must match a literal '%'. */
547 if (__builtin_expect (c
== EOF
, 0))
549 if (__builtin_expect (c
!= fc
, 0))
551 ungetc_not_eof (c
, s
);
556 case L_('n'): /* Answer number of assignments done. */
557 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
558 with the 'n' conversion specifier. */
559 if (!(flags
& SUPPRESS
))
561 /* Don't count the read-ahead. */
562 if (need_longlong
&& (flags
& LONGDBL
))
563 *ARG (long long int *) = read_in
;
564 else if (need_long
&& (flags
& LONG
))
565 *ARG (long int *) = read_in
;
566 else if (flags
& SHORT
)
567 *ARG (short int *) = read_in
;
568 else if (!(flags
& CHAR
))
569 *ARG (int *) = read_in
;
571 *ARG (char *) = read_in
;
573 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
574 /* We have a severe problem here. The ISO C standard
575 contradicts itself in explaining the effect of the %n
576 format in `scanf'. While in ISO C:1990 and the ISO C
577 Amendement 1:1995 the result is described as
579 Execution of a %n directive does not effect the
580 assignment count returned at the completion of
581 execution of the f(w)scanf function.
583 in ISO C Corrigendum 1:1994 the following was added:
586 Add the following fourth example:
589 int d1, d2, n1, n2, i;
590 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
591 the value 123 is assigned to d1 and the value3 to n1.
592 Because %n can never get an input failure the value
593 of 3 is also assigned to n2. The value of d2 is not
594 affected. The value 3 is assigned to i.
596 We go for now with the historically correct code from ISO C,
597 i.e., we don't count the %n assignments. When it ever
598 should proof to be wrong just remove the #ifdef above. */
604 case L_('c'): /* Match characters. */
605 if ((flags
& LONG
) == 0)
607 if (!(flags
& SUPPRESS
))
615 if (__builtin_expect (c
== EOF
, 0))
621 #ifdef COMPILE_WSCANF
622 /* We have to convert the wide character(s) into multibyte
623 characters and store the result. */
624 memset (&state
, '\0', sizeof (state
));
630 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
631 if (__builtin_expect (n
== (size_t) -1, 0))
632 /* No valid wide character. */
635 /* Increment the output pointer. Even if we don't
639 while (--width
> 0 && inchar () != EOF
);
641 if (!(flags
& SUPPRESS
))
645 while (--width
> 0 && inchar () != EOF
);
648 while (--width
> 0 && inchar () != EOF
);
651 if (!(flags
& SUPPRESS
))
658 if (!(flags
& SUPPRESS
))
660 wstr
= ARG (wchar_t *);
666 if (__builtin_expect (c
== EOF
, 0))
669 #ifdef COMPILE_WSCANF
670 /* Just store the incoming wide characters. */
671 if (!(flags
& SUPPRESS
))
675 while (--width
> 0 && inchar () != EOF
);
678 while (--width
> 0 && inchar () != EOF
);
681 /* We have to convert the multibyte input sequence to wide
686 memset (&cstate
, '\0', sizeof (cstate
));
690 /* This is what we present the mbrtowc function first. */
697 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
700 if (n
== (size_t) -2)
702 /* Possibly correct character, just not enough
704 if (__builtin_expect (inchar () == EOF
, 0))
711 if (__builtin_expect (n
!= 1, 0))
714 /* We have a match. */
718 /* Advance the result pointer. */
721 while (--width
> 0 && inchar () != EOF
);
725 if (!(flags
& SUPPRESS
))
730 case L_('s'): /* Read a string. */
733 #define STRING_ARG(Str, Type) \
734 do if (!(flags & SUPPRESS)) \
736 if (flags & MALLOC) \
738 /* The string is to be stored in a malloc'd buffer. */ \
739 strptr = ARG (char **); \
740 if (strptr == NULL) \
742 /* Allocate an initial buffer. */ \
744 *strptr = (char *) malloc (strsize * sizeof (Type)); \
745 Str = (Type *) *strptr; \
748 Str = ARG (Type *); \
752 STRING_ARG (str
, char);
755 if (__builtin_expect (c
== EOF
, 0))
758 #ifdef COMPILE_WSCANF
759 memset (&state
, '\0', sizeof (state
));
766 ungetc_not_eof (c
, s
);
770 #ifdef COMPILE_WSCANF
771 /* This is quite complicated. We have to convert the
772 wide characters into multibyte characters and then
777 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
778 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
780 /* We have to enlarge the buffer if the `a' flag
782 size_t strleng
= str
- *strptr
;
785 newstr
= (char *) realloc (*strptr
, strsize
* 2);
788 /* Can't allocate that much. Last-ditch
790 newstr
= (char *) realloc (*strptr
,
791 strleng
+ MB_CUR_MAX
);
794 /* We lose. Oh well. Terminate the
795 string and stop converting,
796 so at least we don't skip any input. */
797 ((char *) (*strptr
))[strleng
] = '\0';
804 str
= newstr
+ strleng
;
805 strsize
= strleng
+ MB_CUR_MAX
;
811 str
= newstr
+ strleng
;
816 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
,
818 if (__builtin_expect (n
== (size_t) -1, 0))
821 assert (n
<= MB_CUR_MAX
);
826 if (!(flags
& SUPPRESS
))
830 && (char *) str
== *strptr
+ strsize
)
832 /* Enlarge the buffer. */
833 str
= (char *) realloc (*strptr
, 2 * strsize
);
836 /* Can't allocate that much. Last-ditch
838 str
= (char *) realloc (*strptr
, strsize
+ 1);
841 /* We lose. Oh well. Terminate the
842 string and stop converting,
843 so at least we don't skip any input. */
844 ((char *) (*strptr
))[strsize
- 1] = '\0';
850 *strptr
= (char *) str
;
857 *strptr
= (char *) str
;
865 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
867 if (!(flags
& SUPPRESS
))
869 #ifdef COMPILE_WSCANF
870 /* We have to emit the code to get into the initial
872 char buf
[MB_LEN_MAX
];
873 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
874 if (n
> 0 && (flags
& MALLOC
)
875 && str
+ n
>= *strptr
+ strsize
)
877 /* Enlarge the buffer. */
878 size_t strleng
= str
- *strptr
;
881 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
884 /* We lose. Oh well. Terminate the string
885 and stop converting, so at least we don't
887 ((char *) (*strptr
))[strleng
] = '\0';
894 str
= newstr
+ strleng
;
895 strsize
= strleng
+ n
+ 1;
899 str
= __mempcpy (str
, buf
, n
);
903 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
905 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
918 #ifndef COMPILE_WSCANF
922 /* Wide character string. */
923 STRING_ARG (wstr
, wchar_t);
926 if (__builtin_expect (c
== EOF
, 0))
929 #ifndef COMPILE_WSCANF
930 memset (&cstate
, '\0', sizeof (cstate
));
937 ungetc_not_eof (c
, s
);
941 #ifdef COMPILE_WSCANF
943 if (!(flags
& SUPPRESS
))
947 && wstr
== (wchar_t *) *strptr
+ strsize
)
949 /* Enlarge the buffer. */
950 wstr
= (wchar_t *) realloc (*strptr
,
955 /* Can't allocate that much. Last-ditch
957 wstr
= (wchar_t *) realloc (*strptr
,
962 /* We lose. Oh well. Terminate the string
963 and stop converting, so at least we don't
965 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
971 *strptr
= (char *) wstr
;
978 *strptr
= (char *) wstr
;
994 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
997 if (n
== (size_t) -2)
999 /* Possibly correct character, just not enough
1001 if (__builtin_expect (inchar () == EOF
, 0))
1008 if (__builtin_expect (n
!= 1, 0))
1011 /* We have a match. */
1016 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
1017 && wstr
== (wchar_t *) *strptr
+ strsize
)
1019 /* Enlarge the buffer. */
1020 wstr
= (wchar_t *) realloc (*strptr
,
1022 * sizeof (wchar_t)));
1025 /* Can't allocate that much. Last-ditch effort. */
1026 wstr
= (wchar_t *) realloc (*strptr
,
1028 * sizeof (wchar_t)));
1031 /* We lose. Oh well. Terminate the
1032 string and stop converting, so at
1033 least we don't skip any input. */
1034 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1040 *strptr
= (char *) wstr
;
1047 *strptr
= (char *) wstr
;
1055 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
1057 if (!(flags
& SUPPRESS
))
1061 if ((flags
& MALLOC
) && wstr
- (wchar_t *) *strptr
!= strsize
)
1063 wchar_t *cp
= (wchar_t *) realloc (*strptr
,
1065 - (wchar_t *) *strptr
)
1066 * sizeof(wchar_t)));
1068 *strptr
= (char *) cp
;
1076 case L_('x'): /* Hexadecimal integer. */
1077 case L_('X'): /* Ditto. */
1081 case L_('o'): /* Octal integer. */
1085 case L_('u'): /* Unsigned decimal integer. */
1089 case L_('d'): /* Signed decimal integer. */
1091 flags
|= NUMBER_SIGNED
;
1094 case L_('i'): /* Generic number. */
1096 flags
|= NUMBER_SIGNED
;
1100 if (__builtin_expect (c
== EOF
, 0))
1103 /* Check for a sign. */
1104 if (c
== L_('-') || c
== L_('+'))
1112 /* Look for a leading indication of base. */
1113 if (width
!= 0 && c
== L_('0'))
1121 if (width
!= 0 && TOLOWER (c
) == L_('x'))
1139 if (base
== 10 && __builtin_expect ((flags
& I18N
) != 0, 0))
1144 #ifdef COMPILE_WSCANF
1145 const wchar_t *wcdigits
[10];
1146 const wchar_t *wcdigits_extended
[10];
1148 const char *mbdigits
[10];
1149 const char *mbdigits_extended
[10];
1151 /* "to_inpunct" is a map from ASCII digits to their
1152 equivalent in locale. This is defined for locales
1153 which use an extra digits set. */
1154 wctrans_t map
= __wctrans ("to_inpunct");
1158 #ifdef COMPILE_WSCANF
1159 to_level
= _NL_CURRENT_WORD (LC_CTYPE
,
1160 _NL_CTYPE_INDIGITS_WC_LEN
) - 1;
1162 to_level
= (uint32_t) curctype
->values
[_NL_ITEM_INDEX (_NL_CTYPE_INDIGITS_MB_LEN
)].word
- 1;
1165 /* Get the alternative digit forms if there are any. */
1166 if (__builtin_expect (map
!= NULL
, 0))
1168 /* Adding new level for extra digits set in locale file. */
1171 for (n
= 0; n
< 10; ++n
)
1173 #ifdef COMPILE_WSCANF
1174 wcdigits
[n
] = (const wchar_t *)
1175 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1177 wchar_t *wc_extended
= (wchar_t *)
1178 alloca ((to_level
+ 2) * sizeof (wchar_t));
1179 __wmemcpy (wc_extended
, wcdigits
[n
], to_level
);
1180 wc_extended
[to_level
] = __towctrans (L
'0' + n
, map
);
1181 wc_extended
[to_level
+ 1] = '\0';
1182 wcdigits_extended
[n
] = wc_extended
;
1185 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1187 /* Get the equivalent wide char in map. */
1188 wint_t extra_wcdigit
= __towctrans (L
'0' + n
, map
);
1190 /* Convert it to multibyte representation. */
1192 memset (&state
, '\0', sizeof (state
));
1194 char extra_mbdigit
[MB_LEN_MAX
];
1196 = __wcrtomb (extra_mbdigit
, extra_wcdigit
, &state
);
1198 if (mblen
== (size_t) -1)
1200 /* Ignore this new level. */
1205 /* Calculate the length of mbdigits[n]. */
1206 const char *last_char
= mbdigits
[n
];
1207 for (level
= 0; level
< to_level
; ++level
)
1208 last_char
= strchr (last_char
, '\0') + 1;
1210 size_t mbdigits_len
= last_char
- mbdigits
[n
];
1212 /* Allocate memory for extended multibyte digit. */
1214 mb_extended
= (char *) alloca (mbdigits_len
+ mblen
+ 1);
1216 /* And get the mbdigits + extra_digit string. */
1217 *(char *) __mempcpy (__mempcpy (mb_extended
, mbdigits
[n
],
1219 extra_mbdigit
, mblen
) = '\0';
1220 mbdigits_extended
[n
] = mb_extended
;
1225 /* Read the number into workspace. */
1226 while (c
!= EOF
&& width
!= 0)
1228 /* In this round we get the pointer to the digit strings
1229 and also perform the first round of comparisons. */
1230 for (n
= 0; n
< 10; ++n
)
1232 /* Get the string for the digits with value N. */
1233 #ifdef COMPILE_WSCANF
1234 if (__builtin_expect (map
!= NULL
, 0))
1235 wcdigits
[n
] = wcdigits_extended
[n
];
1237 wcdigits
[n
] = (const wchar_t *)
1238 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1239 wcdigits
[n
] += from_level
;
1241 if (c
== (wint_t) *wcdigits
[n
])
1243 to_level
= from_level
;
1247 /* Advance the pointer to the next string. */
1251 int avail
= width
> 0 ? width
: INT_MAX
;
1253 if (__builtin_expect (map
!= NULL
, 0))
1254 mbdigits
[n
] = mbdigits_extended
[n
];
1257 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1259 for (level
= 0; level
< from_level
; level
++)
1260 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1263 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1265 if (*++cmpp
== '\0')
1269 if (avail
== 0 || inchar () == EOF
)
1279 to_level
= from_level
;
1283 /* We are pushing all read characters back. */
1284 if (cmpp
> mbdigits
[n
])
1287 while (--cmpp
> mbdigits
[n
])
1288 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1289 c
= (unsigned char) *cmpp
;
1292 /* Advance the pointer to the next string. */
1293 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1299 /* Have not yet found the digit. */
1300 for (level
= from_level
+ 1; level
<= to_level
; ++level
)
1302 /* Search all ten digits of this level. */
1303 for (n
= 0; n
< 10; ++n
)
1305 #ifdef COMPILE_WSCANF
1306 if (c
== (wint_t) *wcdigits
[n
])
1309 /* Advance the pointer to the next string. */
1313 int avail
= width
> 0 ? width
: INT_MAX
;
1316 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1318 if (*++cmpp
== '\0')
1322 if (avail
== 0 || inchar () == EOF
)
1335 /* We are pushing all read characters back. */
1336 if (cmpp
> mbdigits
[n
])
1339 while (--cmpp
> mbdigits
[n
])
1340 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1341 c
= (unsigned char) *cmpp
;
1344 /* Advance the pointer to the next string. */
1345 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1361 else if (flags
& GROUP
)
1363 /* Try matching against the thousands separator. */
1364 #ifdef COMPILE_WSCANF
1368 const char *cmpp
= thousands
;
1369 int avail
= width
> 0 ? width
: INT_MAX
;
1371 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1374 if (*++cmpp
== '\0')
1378 if (avail
== 0 || inchar () == EOF
)
1386 /* We are pushing all read characters back. */
1387 if (cmpp
> thousands
)
1389 wpsize
-= cmpp
- thousands
;
1391 while (--cmpp
> thousands
)
1392 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1393 c
= (unsigned char) *cmpp
;
1401 /* The last thousands character will be added back by
1417 /* Read the number into workspace. */
1418 while (c
!= EOF
&& width
!= 0)
1425 else if (!ISDIGIT (c
) || (int) (c
- L_('0')) >= base
)
1427 if (base
== 10 && (flags
& GROUP
))
1429 /* Try matching against the thousands separator. */
1430 #ifdef COMPILE_WSCANF
1434 const char *cmpp
= thousands
;
1435 int avail
= width
> 0 ? width
: INT_MAX
;
1437 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1440 if (*++cmpp
== '\0')
1444 if (avail
== 0 || inchar () == EOF
)
1452 /* We are pushing all read characters back. */
1453 if (cmpp
> thousands
)
1455 wpsize
-= cmpp
- thousands
;
1457 while (--cmpp
> thousands
)
1458 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1459 c
= (unsigned char) *cmpp
;
1467 /* The last thousands character will be added back by
1483 || (wpsize
== 1 && (wp
[0] == L_('+') || wp
[0] == L_('-'))))
1485 /* There was no number. If we are supposed to read a pointer
1486 we must recognize "(nil)" as well. */
1487 if (__builtin_expect (wpsize
== 0
1488 && (flags
& READ_POINTER
)
1489 && (width
< 0 || width
>= 0)
1491 && TOLOWER (inchar ()) == L_('n')
1492 && TOLOWER (inchar ()) == L_('i')
1493 && TOLOWER (inchar ()) == L_('l')
1494 && inchar () == L_(')'), 1))
1495 /* We must produce the value of a NULL pointer. A single
1496 '0' digit is enough. */
1500 /* The last read character is not part of the number
1508 /* The just read character is not part of the number anymore. */
1511 /* Convert the number. */
1513 if (need_longlong
&& (flags
& LONGDBL
))
1515 if (flags
& NUMBER_SIGNED
)
1516 num
.q
= __strtoll_internal (wp
, &tw
, base
, flags
& GROUP
);
1518 num
.uq
= __strtoull_internal (wp
, &tw
, base
, flags
& GROUP
);
1522 if (flags
& NUMBER_SIGNED
)
1523 num
.l
= __strtol_internal (wp
, &tw
, base
, flags
& GROUP
);
1525 num
.ul
= __strtoul_internal (wp
, &tw
, base
, flags
& GROUP
);
1527 if (__builtin_expect (wp
== tw
, 0))
1530 if (!(flags
& SUPPRESS
))
1532 if (flags
& NUMBER_SIGNED
)
1534 if (need_longlong
&& (flags
& LONGDBL
))
1535 *ARG (LONGLONG
int *) = num
.q
;
1536 else if (need_long
&& (flags
& LONG
))
1537 *ARG (long int *) = num
.l
;
1538 else if (flags
& SHORT
)
1539 *ARG (short int *) = (short int) num
.l
;
1540 else if (!(flags
& CHAR
))
1541 *ARG (int *) = (int) num
.l
;
1543 *ARG (signed char *) = (signed char) num
.ul
;
1547 if (need_longlong
&& (flags
& LONGDBL
))
1548 *ARG (unsigned LONGLONG
int *) = num
.uq
;
1549 else if (need_long
&& (flags
& LONG
))
1550 *ARG (unsigned long int *) = num
.ul
;
1551 else if (flags
& SHORT
)
1552 *ARG (unsigned short int *)
1553 = (unsigned short int) num
.ul
;
1554 else if (!(flags
& CHAR
))
1555 *ARG (unsigned int *) = (unsigned int) num
.ul
;
1557 *ARG (unsigned char *) = (unsigned char) num
.ul
;
1563 case L_('e'): /* Floating-point numbers. */
1574 if (__builtin_expect (c
== EOF
, 0))
1577 got_dot
= got_e
= 0;
1579 /* Check for a sign. */
1580 if (c
== L_('-') || c
== L_('+'))
1582 negative
= c
== L_('-');
1583 if (__builtin_expect (width
== 0 || inchar () == EOF
, 0))
1584 /* EOF is only an input error before we read any chars. */
1592 /* Take care for the special arguments "nan" and "inf". */
1593 if (TOLOWER (c
) == L_('n'))
1597 if (__builtin_expect (width
== 0
1599 || TOLOWER (c
) != L_('a'), 0))
1604 if (__builtin_expect (width
== 0
1606 || TOLOWER (c
) != L_('n'), 0))
1614 else if (TOLOWER (c
) == L_('i'))
1616 /* Maybe "inf" or "infinity". */
1618 if (__builtin_expect (width
== 0
1620 || TOLOWER (c
) != L_('n'), 0))
1625 if (__builtin_expect (width
== 0
1627 || TOLOWER (c
) != L_('f'), 0))
1632 /* It is as least "inf". */
1633 if (width
!= 0 && inchar () != EOF
)
1635 if (TOLOWER (c
) == L_('i'))
1639 /* Now we have to read the rest as well. */
1641 if (__builtin_expect (width
== 0
1643 || TOLOWER (c
) != L_('n'), 0))
1648 if (__builtin_expect (width
== 0
1650 || TOLOWER (c
) != L_('i'), 0))
1655 if (__builtin_expect (width
== 0
1657 || TOLOWER (c
) != L_('t'), 0))
1662 if (__builtin_expect (width
== 0
1664 || TOLOWER (c
) != L_('y'), 0))
1678 if (width
!= 0 && c
== L_('0'))
1684 if (width
!= 0 && TOLOWER (c
) == L_('x'))
1686 /* It is a number in hexadecimal format. */
1689 flags
|= HEXA_FLOAT
;
1692 /* Grouping is not allowed. */
1704 else if (!got_e
&& (flags
& HEXA_FLOAT
) && ISXDIGIT (c
))
1706 else if (got_e
&& wp
[wpsize
- 1] == exp_char
1707 && (c
== L_('-') || c
== L_('+')))
1709 else if (wpsize
> 0 && !got_e
1710 && (CHAR_T
) TOLOWER (c
) == exp_char
)
1713 got_e
= got_dot
= 1;
1717 #ifdef COMPILE_WSCANF
1718 if (! got_dot
&& c
== decimal
)
1723 else if ((flags
& GROUP
) != 0 && ! got_dot
&& c
== thousands
)
1727 /* The last read character is not part of the number
1733 const char *cmpp
= decimal
;
1734 int avail
= width
> 0 ? width
: INT_MAX
;
1738 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1739 if (*++cmpp
== '\0')
1743 if (avail
== 0 || inchar () == EOF
)
1751 /* Add all the characters. */
1752 for (cmpp
= decimal
; *cmpp
!= '\0'; ++cmpp
)
1753 ADDW ((unsigned char) *cmpp
);
1760 /* Figure out whether it is a thousands separator.
1761 There is one problem: we possibly read more than
1762 one character. We cannot push them back but since
1763 we know that parts of the `decimal' string matched,
1764 we can compare against it. */
1765 const char *cmp2p
= thousands
;
1767 if ((flags
& GROUP
) != 0 && ! got_dot
)
1769 while (cmp2p
- thousands
< cmpp
- decimal
1770 && *cmp2p
== decimal
[cmp2p
- thousands
])
1772 if (cmp2p
- thousands
== cmpp
- decimal
)
1774 while ((unsigned char) *cmp2p
== c
&& avail
>= 0)
1775 if (*++cmp2p
== '\0')
1779 if (avail
== 0 || inchar () == EOF
)
1786 if (cmp2p
!= NULL
&& *cmp2p
== '\0')
1788 /* Add all the characters. */
1789 for (cmpp
= thousands
; *cmpp
!= '\0'; ++cmpp
)
1790 ADDW ((unsigned char) *cmpp
);
1796 /* The last read character is not part of the number
1805 if (width
== 0 || inchar () == EOF
)
1813 if (__builtin_expect ((flags
& I18N
) != 0, 0)
1814 /* Hexadecimal floats make no sense, fixing localized
1815 digits with ASCII letters. */
1816 && !(flags
& HEXA_FLOAT
)
1817 /* Minimum requirement. */
1818 && (wpsize
== 0 || got_dot
)
1819 && (map
= __wctrans ("to_inpunct")) != NULL
)
1821 /* Reget the first character. */
1824 /* Localized digits, decimal points, and thousands
1826 wint_t wcdigits
[12];
1828 /* First get decimal equivalent to check if we read it
1830 wcdigits
[11] = __towctrans (L
'.', map
);
1832 /* If we have not read any character or have just read
1833 locale decimal point which matches the decimal point
1834 for localized FP numbers, then we may have localized
1835 digits. Note, we test GOT_DOT above. */
1836 #ifdef COMPILE_WSCANF
1837 if (wpsize
== 0 || (wpsize
== 1 && wcdigits
[11] == decimal
))
1839 char mbdigits
[12][MB_LEN_MAX
+ 1];
1842 memset (&state
, '\0', sizeof (state
));
1844 bool match_so_far
= wpsize
== 0;
1845 size_t mblen
= __wcrtomb (mbdigits
[11], wcdigits
[11], &state
);
1846 if (mblen
!= (size_t) -1)
1848 mbdigits
[11][mblen
] = '\0';
1849 match_so_far
|= (wpsize
== strlen (decimal
)
1850 && strcmp (decimal
, mbdigits
[11]) == 0);
1854 size_t decimal_len
= strlen (decimal
);
1855 /* This should always be the case but the data comes
1857 if (decimal_len
<= MB_LEN_MAX
)
1859 match_so_far
|= wpsize
== decimal_len
;
1860 memcpy (mbdigits
[11], decimal
, decimal_len
+ 1);
1863 match_so_far
= false;
1869 bool have_locthousands
= (flags
& GROUP
) != 0;
1871 /* Now get the digits and the thousands-sep equivalents. */
1872 for (int n
= 0; n
< 11; ++n
)
1875 wcdigits
[n
] = __towctrans (L
'0' + n
, map
);
1878 wcdigits
[10] = __towctrans (L
',', map
);
1879 have_locthousands
&= wcdigits
[10] != L
'\0';
1882 #ifndef COMPILE_WSCANF
1883 memset (&state
, '\0', sizeof (state
));
1885 size_t mblen
= __wcrtomb (mbdigits
[n
], wcdigits
[n
],
1887 if (mblen
== (size_t) -1)
1891 if (have_locthousands
)
1893 size_t thousands_len
= strlen (thousands
);
1894 if (thousands_len
<= MB_LEN_MAX
)
1895 memcpy (mbdigits
[10], thousands
,
1898 have_locthousands
= false;
1902 /* Ignore checking against localized digits. */
1906 mbdigits
[n
][mblen
] = '\0';
1910 /* Start checking against localized digits, if
1911 convertion is done correctly. */
1914 if (got_e
&& wp
[wpsize
- 1] == exp_char
1915 && (c
== L_('-') || c
== L_('+')))
1917 else if (wpsize
> 0 && !got_e
1918 && (CHAR_T
) TOLOWER (c
) == exp_char
)
1921 got_e
= got_dot
= 1;
1925 /* Check against localized digits, decimal point,
1926 and thousands separator. */
1928 for (n
= 0; n
< 12; ++n
)
1930 #ifdef COMPILE_WSCANF
1931 if (c
== wcdigits
[n
])
1935 else if (n
== 11 && !got_dot
)
1940 else if (n
== 10 && have_locthousands
1944 /* The last read character is not part
1945 of the number anymore. */
1951 const char *cmpp
= mbdigits
[n
];
1952 int avail
= width
> 0 ? width
: INT_MAX
;
1954 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1955 if (*++cmpp
== '\0')
1959 if (avail
== 0 || inchar () == EOF
)
1970 else if (n
== 11 && !got_dot
)
1972 /* Add all the characters. */
1973 for (cmpp
= decimal
; *cmpp
!= '\0';
1975 ADDW ((unsigned char) *cmpp
);
1979 else if (n
== 10 && (flags
& GROUP
) != 0
1982 /* Add all the characters. */
1983 for (cmpp
= thousands
; *cmpp
!= '\0';
1985 ADDW ((unsigned char) *cmpp
);
1988 /* The last read character is not part
1989 of the number anymore. */
1995 /* We are pushing all read characters back. */
1996 if (cmpp
> mbdigits
[n
])
1999 while (--cmpp
> mbdigits
[n
])
2000 ungetc_not_eof ((unsigned char) *cmpp
, s
);
2001 c
= (unsigned char) *cmpp
;
2008 /* The last read character is not part
2009 of the number anymore. */
2015 if (width
== 0 || inchar () == EOF
)
2023 #ifndef COMPILE_WSCANF
2029 /* Have we read any character? If we try to read a number
2030 in hexadecimal notation and we have read only the `0x'
2031 prefix or no exponent this is an error. */
2032 if (__builtin_expect (wpsize
== 0
2033 || ((flags
& HEXA_FLOAT
)
2034 && (wpsize
== 2 || ! got_e
)), 0))
2038 /* Convert the number. */
2040 if ((flags
& LONGDBL
) && !__ldbl_is_dbl
)
2042 long double d
= __strtold_internal (wp
, &tw
, flags
& GROUP
);
2043 if (!(flags
& SUPPRESS
) && tw
!= wp
)
2044 *ARG (long double *) = negative
? -d
: d
;
2046 else if (flags
& (LONG
| LONGDBL
))
2048 double d
= __strtod_internal (wp
, &tw
, flags
& GROUP
);
2049 if (!(flags
& SUPPRESS
) && tw
!= wp
)
2050 *ARG (double *) = negative
? -d
: d
;
2054 float d
= __strtof_internal (wp
, &tw
, flags
& GROUP
);
2055 if (!(flags
& SUPPRESS
) && tw
!= wp
)
2056 *ARG (float *) = negative
? -d
: d
;
2059 if (__builtin_expect (tw
== wp
, 0))
2062 if (!(flags
& SUPPRESS
))
2066 case L_('['): /* Character class. */
2068 STRING_ARG (wstr
, wchar_t);
2070 STRING_ARG (str
, char);
2081 /* There is no width given so there is also no limit on the
2082 number of characters we read. Therefore we set width to
2083 a very high value to make the algorithm easier. */
2086 #ifdef COMPILE_WSCANF
2087 /* Find the beginning and the end of the scanlist. We are not
2088 creating a lookup table since it would have to be too large.
2089 Instead we search each time through the string. This is not
2090 a constant lookup time but who uses this feature deserves to
2092 tw
= (wchar_t *) f
; /* Marks the beginning. */
2097 while ((fc
= *f
++) != L
'\0' && fc
!= L
']');
2099 if (__builtin_expect (fc
== L
'\0', 0))
2101 wp
= (wchar_t *) f
- 1;
2103 /* Fill WP with byte flags indexed by character.
2104 We will use this flag map for matching input characters. */
2105 if (wpmax
< UCHAR_MAX
+ 1)
2107 wpmax
= UCHAR_MAX
+ 1;
2108 wp
= (char *) alloca (wpmax
);
2110 memset (wp
, '\0', UCHAR_MAX
+ 1);
2113 if (fc
== ']' || fc
== '-')
2115 /* If ] or - appears before any char in the set, it is not
2116 the terminator or separator, but the first char in the
2122 while ((fc
= *f
++) != '\0' && fc
!= ']')
2123 if (fc
== '-' && *f
!= '\0' && *f
!= ']'
2124 && (unsigned char) f
[-2] <= (unsigned char) *f
)
2126 /* Add all characters from the one before the '-'
2127 up to (but not including) the next format char. */
2128 for (fc
= (unsigned char) f
[-2]; fc
< (unsigned char) *f
; ++fc
)
2132 /* Add the character to the flag map. */
2135 if (__builtin_expect (fc
== '\0', 0))
2141 size_t now
= read_in
;
2142 #ifdef COMPILE_WSCANF
2143 if (__builtin_expect (inchar () == WEOF
, 0))
2150 /* Test whether it's in the scanlist. */
2154 if (runp
[0] == L
'-' && runp
[1] != '\0' && runp
+ 1 != wp
2156 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2158 /* Match against all characters in between the
2159 first and last character of the sequence. */
2162 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2163 if ((wint_t) wc
== c
)
2166 if (wc
<= runp
[1] && !not_in
)
2168 if (wc
<= runp
[1] && not_in
)
2170 /* The current character is not in the
2180 if ((wint_t) *runp
== c
&& !not_in
)
2182 if ((wint_t) *runp
== c
&& not_in
)
2192 if (runp
== wp
&& !not_in
)
2198 if (!(flags
& SUPPRESS
))
2202 if ((flags
& MALLOC
)
2203 && wstr
== (wchar_t *) *strptr
+ strsize
)
2205 /* Enlarge the buffer. */
2206 wstr
= (wchar_t *) realloc (*strptr
,
2208 * sizeof (wchar_t));
2211 /* Can't allocate that much. Last-ditch
2214 realloc (*strptr
, (strsize
+ 1)
2215 * sizeof (wchar_t));
2218 /* We lose. Oh well. Terminate the string
2219 and stop converting, so at least we don't
2221 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2227 *strptr
= (char *) wstr
;
2234 *strptr
= (char *) wstr
;
2241 while (--width
> 0 && inchar () != WEOF
);
2244 char buf
[MB_LEN_MAX
];
2248 if (__builtin_expect (inchar () == EOF
, 0))
2251 memset (&cstate
, '\0', sizeof (cstate
));
2255 if (wp
[c
] == not_in
)
2257 ungetc_not_eof (c
, s
);
2262 if (!(flags
& SUPPRESS
))
2266 /* Convert it into a wide character. */
2268 n
= __mbrtowc (wstr
, buf
, 1, &cstate
);
2270 if (n
== (size_t) -2)
2272 /* Possibly correct character, just not enough
2275 assert (cnt
< MB_CUR_MAX
);
2281 if ((flags
& MALLOC
)
2282 && wstr
== (wchar_t *) *strptr
+ strsize
)
2284 /* Enlarge the buffer. */
2285 wstr
= (wchar_t *) realloc (*strptr
,
2287 * sizeof (wchar_t)));
2290 /* Can't allocate that much. Last-ditch
2293 realloc (*strptr
, ((strsize
+ 1)
2294 * sizeof (wchar_t)));
2297 /* We lose. Oh well. Terminate the
2298 string and stop converting,
2299 so at least we don't skip any input. */
2300 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2306 *strptr
= (char *) wstr
;
2313 *strptr
= (char *) wstr
;
2323 while (inchar () != EOF
);
2325 if (__builtin_expect (cnt
!= 0, 0))
2326 /* We stopped in the middle of recognizing another
2327 character. That's a problem. */
2331 if (__builtin_expect (now
== read_in
, 0))
2332 /* We haven't succesfully read any character. */
2335 if (!(flags
& SUPPRESS
))
2339 if ((flags
& MALLOC
)
2340 && wstr
- (wchar_t *) *strptr
!= strsize
)
2342 wchar_t *cp
= (wchar_t *)
2343 realloc (*strptr
, ((wstr
- (wchar_t *) *strptr
)
2344 * sizeof(wchar_t)));
2346 *strptr
= (char *) cp
;
2354 size_t now
= read_in
;
2356 if (__builtin_expect (inchar () == EOF
, 0))
2359 #ifdef COMPILE_WSCANF
2361 memset (&state
, '\0', sizeof (state
));
2368 /* Test whether it's in the scanlist. */
2372 if (runp
[0] == L
'-' && runp
[1] != '\0' && runp
+ 1 != wp
2374 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2376 /* Match against all characters in between the
2377 first and last character of the sequence. */
2380 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2381 if ((wint_t) wc
== c
)
2384 if (wc
<= runp
[1] && !not_in
)
2386 if (wc
<= runp
[1] && not_in
)
2388 /* The current character is not in the
2398 if ((wint_t) *runp
== c
&& !not_in
)
2400 if ((wint_t) *runp
== c
&& not_in
)
2410 if (runp
== wp
&& !not_in
)
2416 if (!(flags
& SUPPRESS
))
2418 if ((flags
& MALLOC
)
2419 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
2421 /* Enlarge the buffer. */
2422 size_t strleng
= str
- *strptr
;
2425 newstr
= (char *) realloc (*strptr
, 2 * strsize
);
2428 /* Can't allocate that much. Last-ditch
2430 newstr
= (char *) realloc (*strptr
,
2431 strleng
+ MB_CUR_MAX
);
2434 /* We lose. Oh well. Terminate the string
2435 and stop converting, so at least we don't
2437 ((char *) (*strptr
))[strleng
] = '\0';
2444 str
= newstr
+ strleng
;
2445 strsize
= strleng
+ MB_CUR_MAX
;
2451 str
= newstr
+ strleng
;
2457 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
2458 if (__builtin_expect (n
== (size_t) -1, 0))
2461 assert (n
<= MB_CUR_MAX
);
2464 while (--width
> 0 && inchar () != WEOF
);
2469 if (wp
[c
] == not_in
)
2471 ungetc_not_eof (c
, s
);
2476 if (!(flags
& SUPPRESS
))
2479 if ((flags
& MALLOC
)
2480 && (char *) str
== *strptr
+ strsize
)
2482 /* Enlarge the buffer. */
2483 size_t newsize
= 2 * strsize
;
2486 str
= (char *) realloc (*strptr
, newsize
);
2489 /* Can't allocate that much. Last-ditch
2491 if (newsize
> strsize
+ 1)
2493 newsize
= strsize
+ 1;
2496 /* We lose. Oh well. Terminate the
2497 string and stop converting,
2498 so at least we don't skip any input. */
2499 ((char *) (*strptr
))[strsize
- 1] = '\0';
2505 *strptr
= (char *) str
;
2512 while (--width
> 0 && inchar () != EOF
);
2515 if (__builtin_expect (now
== read_in
, 0))
2516 /* We haven't succesfully read any character. */
2519 if (!(flags
& SUPPRESS
))
2521 #ifdef COMPILE_WSCANF
2522 /* We have to emit the code to get into the initial
2524 char buf
[MB_LEN_MAX
];
2525 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
2526 if (n
> 0 && (flags
& MALLOC
)
2527 && str
+ n
>= *strptr
+ strsize
)
2529 /* Enlarge the buffer. */
2530 size_t strleng
= str
- *strptr
;
2533 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
2536 /* We lose. Oh well. Terminate the string
2537 and stop converting, so at least we don't
2539 ((char *) (*strptr
))[strleng
] = '\0';
2546 str
= newstr
+ strleng
;
2547 strsize
= strleng
+ n
+ 1;
2551 str
= __mempcpy (str
, buf
, n
);
2555 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
2557 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
2567 case L_('p'): /* Generic pointer. */
2569 /* A PTR must be the same size as a `long int'. */
2570 flags
&= ~(SHORT
|LONGDBL
);
2573 flags
|= READ_POINTER
;
2577 /* If this is an unknown format character punt. */
2582 /* The last thing we saw int the format string was a white space.
2583 Consume the last white spaces. */
2588 while (ISSPACE (c
));
2593 /* Unlock stream. */
2602 #ifdef COMPILE_WSCANF
2604 __vfwscanf (FILE *s
, const wchar_t *format
, va_list argptr
)
2606 return _IO_vfwscanf (s
, format
, argptr
, NULL
);
2608 ldbl_weak_alias (__vfwscanf
, vfwscanf
)
2611 ___vfscanf (FILE *s
, const char *format
, va_list argptr
)
2613 return _IO_vfscanf_internal (s
, format
, argptr
, NULL
);
2615 ldbl_strong_alias (_IO_vfscanf_internal
, _IO_vfscanf
)
2616 ldbl_strong_alias (___vfscanf
, __vfscanf
)
2617 ldbl_hidden_def (___vfscanf
, __vfscanf
)
2618 ldbl_weak_alias (___vfscanf
, vfscanf
)