1 /* Copyright (C) 1991-2007, 2010, 2011, 2012 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, see
16 <http://www.gnu.org/licenses/>. */
29 #include <bits/libc-lock.h>
30 #include <locale/localeinfo.h>
33 # define HAVE_LONGLONG
34 # define LONGLONG long long
36 # define LONGLONG long
39 /* Determine whether we have to handle `long long' at all. */
40 #if LONG_MAX == LONG_LONG_MAX
41 # define need_longlong 0
43 # define need_longlong 1
46 /* Determine whether we have to handle `long'. */
47 #if INT_MAX == LONG_MAX
53 /* Those are flags in the conversion format. */
54 #define LONG 0x0001 /* l: long or double */
55 #define LONGDBL 0x0002 /* L: long long or long double */
56 #define SHORT 0x0004 /* h: short */
57 #define SUPPRESS 0x0008 /* *: suppress assignment */
58 #define POINTER 0x0010 /* weird %p pointer (`fake hex') */
59 #define NOSKIP 0x0020 /* do not skip blanks */
60 #define NUMBER_SIGNED 0x0040 /* signed integer */
61 #define GROUP 0x0080 /* ': group numbers */
62 #define GNU_MALLOC 0x0100 /* a: malloc strings */
63 #define CHAR 0x0200 /* hh: char */
64 #define I18N 0x0400 /* I: use locale's digits */
65 #define HEXA_FLOAT 0x0800 /* hexadecimal float */
66 #define READ_POINTER 0x1000 /* this is a pointer value */
67 #define POSIX_MALLOC 0x2000 /* m: malloc strings */
68 #define MALLOC (GNU_MALLOC | POSIX_MALLOC)
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 add_ptr_to_free(ptr) \
152 if (ptrs_to_free == NULL \
153 || ptrs_to_free->count == (sizeof (ptrs_to_free->ptrs) \
154 / sizeof (ptrs_to_free->ptrs[0]))) \
156 struct ptrs_to_free *new_ptrs = alloca (sizeof (*ptrs_to_free)); \
157 new_ptrs->count = 0; \
158 new_ptrs->next = ptrs_to_free; \
159 ptrs_to_free = new_ptrs; \
161 ptrs_to_free->ptrs[ptrs_to_free->count++] = (ptr); \
164 #define ARGCHECK(s, format) \
167 /* Check file argument for consistence. */ \
168 CHECK_FILE (s, EOF); \
169 if (s->_flags & _IO_NO_READS) \
171 __set_errno (EBADF); \
174 else if (format == NULL) \
180 #define LOCK_STREAM(S) \
181 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, (S)); \
183 #define UNLOCK_STREAM(S) \
184 _IO_funlockfile (S); \
185 __libc_cleanup_region_end (0)
190 struct ptrs_to_free
*next
;
194 /* Read formatted input from S according to the format string
195 FORMAT, using the argument list in ARG.
196 Return the number of assignments made, or -1 for an input error. */
197 #ifdef COMPILE_WSCANF
199 _IO_vfwscanf (_IO_FILE
*s
, const wchar_t *format
, _IO_va_list argptr
,
203 _IO_vfscanf_internal (_IO_FILE
*s
, const char *format
, _IO_va_list argptr
,
208 register const CHAR_T
*f
= format
;
209 register UCHAR_T fc
; /* Current character of the format. */
210 register WINT_T done
= 0; /* Assignments done. */
211 register size_t read_in
= 0; /* Chars read in. */
212 register WINT_T c
= 0; /* Last char read. */
213 register int width
; /* Maximum field width. */
214 register int flags
; /* Modifiers for current format element. */
216 #ifndef COMPILE_WSCANF
217 __locale_t loc
= _NL_CURRENT_LOCALE
;
218 struct __locale_data
*const curctype
= loc
->__locales
[LC_CTYPE
];
221 /* Errno of last failed inchar call. */
222 int inchar_errno
= 0;
223 /* Status for reading F-P nums. */
224 char got_dot
, got_e
, negative
;
225 /* If a [...] is a [^...]. */
227 #define exp_char not_in
228 /* Base for integral numbers. */
230 /* Decimal point character. */
231 #ifdef COMPILE_WSCANF
236 /* The thousands character of the current locale. */
237 #ifdef COMPILE_WSCANF
240 const char *thousands
;
242 struct ptrs_to_free
*ptrs_to_free
= NULL
;
243 /* State for the conversions. */
245 /* Integral holding variables. */
249 unsigned long long int uq
;
251 unsigned long int ul
;
253 /* Character-buffer pointer. */
255 wchar_t *wstr
= NULL
;
256 char **strptr
= NULL
;
258 /* We must not react on white spaces immediately because they can
259 possibly be matched even if in the input stream no character is
260 available anymore. */
263 CHAR_T
*tw
; /* Temporary pointer. */
264 CHAR_T
*wp
= NULL
; /* Workspace. */
265 size_t wpmax
= 0; /* Maximal size of workspace. */
266 size_t wpsize
; /* Currently used bytes in workspace. */
267 bool use_malloc
= false;
271 if (__builtin_expect (wpsize == wpmax, 0)) \
274 size_t newsize = (UCHAR_MAX + 1 > 2 * wpmax \
275 ? UCHAR_MAX + 1 : 2 * wpmax); \
276 if (use_malloc || !__libc_use_alloca (newsize)) \
278 wp = realloc (use_malloc ? wp : NULL, newsize); \
287 MEMCPY (wp, old, wpsize); \
293 size_t s = wpmax * sizeof (CHAR_T); \
294 wp = (CHAR_T *) extend_alloca (wp, s, \
295 newsize * sizeof (CHAR_T)); \
296 wpmax = s / sizeof (CHAR_T); \
298 MEMCPY (wp, old, wpsize); \
301 wp[wpsize++] = (Ch); \
306 __va_copy (arg
, argptr
);
308 arg
= (va_list) argptr
;
315 ARGCHECK (s
, format
);
318 #ifndef COMPILE_WSCANF
319 struct __locale_data
*const curnumeric
= loc
->__locales
[LC_NUMERIC
];
322 /* Figure out the decimal point character. */
323 #ifdef COMPILE_WSCANF
324 decimal
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
326 decimal
= curnumeric
->values
[_NL_ITEM_INDEX (DECIMAL_POINT
)].string
;
328 /* Figure out the thousands separator character. */
329 #ifdef COMPILE_WSCANF
330 thousands
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_THOUSANDS_SEP_WC
);
332 thousands
= curnumeric
->values
[_NL_ITEM_INDEX (THOUSANDS_SEP
)].string
;
333 if (*thousands
== '\0')
338 /* Lock the stream. */
342 #ifndef COMPILE_WSCANF
343 /* From now on we use `state' to convert the format string. */
344 memset (&state
, '\0', sizeof (state
));
347 /* Run through the format string. */
351 /* Extract the next argument, which is of type TYPE.
352 For a %N$... spec, this is the Nth argument from the beginning;
353 otherwise it is the next argument after the state now in ARG. */
355 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
356 ({ unsigned int pos = argpos; \
358 __va_copy (arg, argptr); \
360 (void) va_arg (arg, void *); \
361 va_arg (arg, type); \
365 /* XXX Possible optimization. */
366 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
367 ({ va_list arg = (va_list) argptr; \
368 arg = (va_list) ((char *) arg \
370 * __va_rounded_size (void *)); \
371 va_arg (arg, type); \
374 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
375 ({ unsigned int pos = argpos; \
376 va_list arg = (va_list) argptr; \
378 (void) va_arg (arg, void *); \
379 va_arg (arg, type); \
384 #ifndef COMPILE_WSCANF
385 if (!isascii ((unsigned char) *f
))
387 /* Non-ASCII, may be a multibyte. */
388 int len
= __mbrlen (f
, strlen (f
), &state
);
394 if (__builtin_expect (c
== EOF
, 0))
396 else if (c
!= (unsigned char) *f
++)
398 ungetc_not_eof (c
, s
);
411 /* Remember to skip spaces. */
418 /* Read a character. */
421 /* Characters other than format specs must just match. */
422 if (__builtin_expect (c
== EOF
, 0))
425 /* We saw white space char as the last character in the format
426 string. Now it's time to skip all leading white space. */
430 if (__builtin_expect (inchar () == EOF
, 0))
435 if (__builtin_expect (c
!= fc
, 0))
444 /* This is the start of the conversion string. */
447 /* Initialize state of modifiers. */
450 /* Prepare temporary buffer. */
453 /* Check for a positional parameter specification. */
454 if (ISDIGIT ((UCHAR_T
) *f
))
456 argpos
= (UCHAR_T
) *f
++ - L_('0');
457 while (ISDIGIT ((UCHAR_T
) *f
))
458 argpos
= argpos
* 10 + ((UCHAR_T
) *f
++ - L_('0'));
463 /* Oops; that was actually the field width. */
470 /* Check for the assignment-suppressing, the number grouping flag,
471 and the signal to use the locale's digit representation. */
472 while (*f
== L_('*') || *f
== L_('\'') || *f
== L_('I'))
479 #ifdef COMPILE_WSCANF
480 if (thousands
!= L
'\0')
482 if (thousands
!= NULL
)
491 /* Find the maximum field width. */
493 while (ISDIGIT ((UCHAR_T
) *f
))
496 width
+= (UCHAR_T
) *f
++ - L_('0');
502 /* Check for type modifiers. */
506 /* ints are short ints or chars. */
518 /* A double `l' is equivalent to an `L'. */
520 flags
|= LONGDBL
| LONG
;
523 /* ints are long ints. */
528 /* doubles are long doubles, and ints are long long ints. */
529 flags
|= LONGDBL
| LONG
;
532 /* The `a' is used as a flag only if followed by `s', `S' or
534 if (*f
!= L_('s') && *f
!= L_('S') && *f
!= L_('['))
539 /* In __isoc99_*scanf %as, %aS and %a[ extension is not
541 if (s
->_flags2
& _IO_FLAGS2_SCANF_STD
)
546 /* String conversions (%s, %[) take a `char **'
547 arg and fill it in with a malloc'd pointer. */
551 flags
|= POSIX_MALLOC
;
559 if (need_longlong
&& sizeof (size_t) > sizeof (unsigned long int))
561 else if (sizeof (size_t) > sizeof (unsigned int))
565 if (need_longlong
&& sizeof (uintmax_t) > sizeof (unsigned long int))
567 else if (sizeof (uintmax_t) > sizeof (unsigned int))
571 if (need_longlong
&& sizeof (ptrdiff_t) > sizeof (long int))
573 else if (sizeof (ptrdiff_t) > sizeof (int))
577 /* Not a recognized modifier. Backup. */
582 /* End of the format string? */
583 if (__builtin_expect (*f
== L_('\0'), 0))
586 /* Find the conversion specifier. */
588 if (skip_space
|| (fc
!= L_('[') && fc
!= L_('c')
589 && fc
!= L_('C') && fc
!= L_('n')))
591 /* Eat whitespace. */
592 int save_errno
= errno
;
595 /* We add the additional test for EOF here since otherwise
596 inchar will restore the old errno value which might be
597 EINTR but does not indicate an interrupt since nothing
598 was read at this time. */
599 if (__builtin_expect ((c
== EOF
|| inchar () == EOF
)
600 && errno
== EINTR
, 0))
603 __set_errno (save_errno
);
610 case L_('%'): /* Must match a literal '%'. */
612 if (__builtin_expect (c
== EOF
, 0))
614 if (__builtin_expect (c
!= fc
, 0))
616 ungetc_not_eof (c
, s
);
621 case L_('n'): /* Answer number of assignments done. */
622 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
623 with the 'n' conversion specifier. */
624 if (!(flags
& SUPPRESS
))
626 /* Don't count the read-ahead. */
627 if (need_longlong
&& (flags
& LONGDBL
))
628 *ARG (long long int *) = read_in
;
629 else if (need_long
&& (flags
& LONG
))
630 *ARG (long int *) = read_in
;
631 else if (flags
& SHORT
)
632 *ARG (short int *) = read_in
;
633 else if (!(flags
& CHAR
))
634 *ARG (int *) = read_in
;
636 *ARG (char *) = read_in
;
638 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
639 /* We have a severe problem here. The ISO C standard
640 contradicts itself in explaining the effect of the %n
641 format in `scanf'. While in ISO C:1990 and the ISO C
642 Amendement 1:1995 the result is described as
644 Execution of a %n directive does not effect the
645 assignment count returned at the completion of
646 execution of the f(w)scanf function.
648 in ISO C Corrigendum 1:1994 the following was added:
651 Add the following fourth example:
654 int d1, d2, n1, n2, i;
655 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
656 the value 123 is assigned to d1 and the value3 to n1.
657 Because %n can never get an input failure the value
658 of 3 is also assigned to n2. The value of d2 is not
659 affected. The value 3 is assigned to i.
661 We go for now with the historically correct code from ISO C,
662 i.e., we don't count the %n assignments. When it ever
663 should proof to be wrong just remove the #ifdef above. */
669 case L_('c'): /* Match characters. */
670 if ((flags
& LONG
) == 0)
675 #define STRING_ARG(Str, Type, Width) \
676 do if (!(flags & SUPPRESS)) \
678 if (flags & MALLOC) \
680 /* The string is to be stored in a malloc'd buffer. */ \
681 /* For %mS using char ** is actually wrong, but \
682 shouldn't make a difference on any arch glibc \
683 supports and would unnecessarily complicate \
685 strptr = ARG (char **); \
686 if (strptr == NULL) \
688 /* Allocate an initial buffer. */ \
690 *strptr = (char *) malloc (strsize * sizeof (Type)); \
691 Str = (Type *) *strptr; \
693 add_ptr_to_free (strptr); \
694 else if (flags & POSIX_MALLOC) \
701 Str = ARG (Type *); \
705 #ifdef COMPILE_WSCANF
706 STRING_ARG (str
, char, 100);
708 STRING_ARG (str
, char, (width
> 1024 ? 1024 : width
));
712 if (__builtin_expect (c
== EOF
, 0))
715 #ifdef COMPILE_WSCANF
716 /* We have to convert the wide character(s) into multibyte
717 characters and store the result. */
718 memset (&state
, '\0', sizeof (state
));
724 if (!(flags
& SUPPRESS
) && (flags
& POSIX_MALLOC
)
725 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
727 /* We have to enlarge the buffer if the `m' flag
729 size_t strleng
= str
- *strptr
;
732 newstr
= (char *) realloc (*strptr
, strsize
* 2);
735 /* Can't allocate that much. Last-ditch effort. */
736 newstr
= (char *) realloc (*strptr
,
737 strleng
+ MB_CUR_MAX
);
740 /* c can't have `a' flag, only `m'. */
747 str
= newstr
+ strleng
;
748 strsize
= strleng
+ MB_CUR_MAX
;
754 str
= newstr
+ strleng
;
759 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
760 if (__builtin_expect (n
== (size_t) -1, 0))
761 /* No valid wide character. */
764 /* Increment the output pointer. Even if we don't
768 while (--width
> 0 && inchar () != EOF
);
770 if (!(flags
& SUPPRESS
))
775 && (char *) str
== *strptr
+ strsize
)
777 /* Enlarge the buffer. */
780 + (strsize
>= width
? width
- 1 : strsize
);
782 str
= (char *) realloc (*strptr
, newsize
);
785 /* Can't allocate that much. Last-ditch
787 str
= (char *) realloc (*strptr
, strsize
+ 1);
790 /* c can't have `a' flag, only `m'. */
796 *strptr
= (char *) str
;
803 *strptr
= (char *) str
;
810 while (--width
> 0 && inchar () != EOF
);
813 while (--width
> 0 && inchar () != EOF
);
816 if (!(flags
& SUPPRESS
))
818 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
820 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
835 STRING_ARG (wstr
, wchar_t, (width
> 1024 ? 1024 : width
));
838 if (__builtin_expect (c
== EOF
, 0))
841 #ifdef COMPILE_WSCANF
842 /* Just store the incoming wide characters. */
843 if (!(flags
& SUPPRESS
))
848 && wstr
== (wchar_t *) *strptr
+ strsize
)
851 = strsize
+ (strsize
> width
? width
- 1 : strsize
);
852 /* Enlarge the buffer. */
853 wstr
= (wchar_t *) realloc (*strptr
,
854 newsize
* sizeof (wchar_t));
857 /* Can't allocate that much. Last-ditch effort. */
858 wstr
= (wchar_t *) realloc (*strptr
,
863 /* C or lc can't have `a' flag, only `m'
870 *strptr
= (char *) wstr
;
877 *strptr
= (char *) wstr
;
884 while (--width
> 0 && inchar () != EOF
);
887 while (--width
> 0 && inchar () != EOF
);
890 /* We have to convert the multibyte input sequence to wide
895 memset (&cstate
, '\0', sizeof (cstate
));
899 /* This is what we present the mbrtowc function first. */
902 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
903 && wstr
== (wchar_t *) *strptr
+ strsize
)
906 = strsize
+ (strsize
> width
? width
- 1 : strsize
);
907 /* Enlarge the buffer. */
908 wstr
= (wchar_t *) realloc (*strptr
,
909 newsize
* sizeof (wchar_t));
912 /* Can't allocate that much. Last-ditch effort. */
913 wstr
= (wchar_t *) realloc (*strptr
,
915 * sizeof (wchar_t)));
918 /* C or lc can't have `a' flag, only `m' flag. */
924 *strptr
= (char *) wstr
;
931 *strptr
= (char *) wstr
;
941 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
944 if (n
== (size_t) -2)
946 /* Possibly correct character, just not enough
948 if (__builtin_expect (inchar () == EOF
, 0))
955 if (__builtin_expect (n
!= 1, 0))
958 /* We have a match. */
962 /* Advance the result pointer. */
965 while (--width
> 0 && inchar () != EOF
);
969 if (!(flags
& SUPPRESS
))
971 if ((flags
& MALLOC
) && wstr
- (wchar_t *) *strptr
!= strsize
)
973 wchar_t *cp
= (wchar_t *) realloc (*strptr
,
975 - (wchar_t *) *strptr
)
976 * sizeof (wchar_t)));
978 *strptr
= (char *) cp
;
987 case L_('s'): /* Read a string. */
990 STRING_ARG (str
, char, 100);
993 if (__builtin_expect (c
== EOF
, 0))
996 #ifdef COMPILE_WSCANF
997 memset (&state
, '\0', sizeof (state
));
1004 ungetc_not_eof (c
, s
);
1008 #ifdef COMPILE_WSCANF
1009 /* This is quite complicated. We have to convert the
1010 wide characters into multibyte characters and then
1015 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
1016 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
1018 /* We have to enlarge the buffer if the `a' or `m'
1020 size_t strleng
= str
- *strptr
;
1023 newstr
= (char *) realloc (*strptr
, strsize
* 2);
1026 /* Can't allocate that much. Last-ditch
1028 newstr
= (char *) realloc (*strptr
,
1029 strleng
+ MB_CUR_MAX
);
1032 if (flags
& POSIX_MALLOC
)
1037 /* We lose. Oh well. Terminate the
1038 string and stop converting,
1039 so at least we don't skip any input. */
1040 ((char *) (*strptr
))[strleng
] = '\0';
1048 str
= newstr
+ strleng
;
1049 strsize
= strleng
+ MB_CUR_MAX
;
1055 str
= newstr
+ strleng
;
1060 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
,
1062 if (__builtin_expect (n
== (size_t) -1, 0))
1065 assert (n
<= MB_CUR_MAX
);
1070 if (!(flags
& SUPPRESS
))
1073 if ((flags
& MALLOC
)
1074 && (char *) str
== *strptr
+ strsize
)
1076 /* Enlarge the buffer. */
1077 str
= (char *) realloc (*strptr
, 2 * strsize
);
1080 /* Can't allocate that much. Last-ditch
1082 str
= (char *) realloc (*strptr
, strsize
+ 1);
1085 if (flags
& POSIX_MALLOC
)
1090 /* We lose. Oh well. Terminate the
1091 string and stop converting,
1092 so at least we don't skip any input. */
1093 ((char *) (*strptr
))[strsize
- 1] = '\0';
1100 *strptr
= (char *) str
;
1107 *strptr
= (char *) str
;
1115 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
1117 if (!(flags
& SUPPRESS
))
1119 #ifdef COMPILE_WSCANF
1120 /* We have to emit the code to get into the initial
1122 char buf
[MB_LEN_MAX
];
1123 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
1124 if (n
> 0 && (flags
& MALLOC
)
1125 && str
+ n
>= *strptr
+ strsize
)
1127 /* Enlarge the buffer. */
1128 size_t strleng
= str
- *strptr
;
1131 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
1134 if (flags
& POSIX_MALLOC
)
1139 /* We lose. Oh well. Terminate the string
1140 and stop converting, so at least we don't
1142 ((char *) (*strptr
))[strleng
] = '\0';
1150 str
= newstr
+ strleng
;
1151 strsize
= strleng
+ n
+ 1;
1155 str
= __mempcpy (str
, buf
, n
);
1159 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
1161 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
1175 #ifndef COMPILE_WSCANF
1179 /* Wide character string. */
1180 STRING_ARG (wstr
, wchar_t, 100);
1183 if (__builtin_expect (c
== EOF
, 0))
1186 #ifndef COMPILE_WSCANF
1187 memset (&cstate
, '\0', sizeof (cstate
));
1194 ungetc_not_eof (c
, s
);
1198 #ifdef COMPILE_WSCANF
1200 if (!(flags
& SUPPRESS
))
1203 if ((flags
& MALLOC
)
1204 && wstr
== (wchar_t *) *strptr
+ strsize
)
1206 /* Enlarge the buffer. */
1207 wstr
= (wchar_t *) realloc (*strptr
,
1209 * sizeof (wchar_t));
1212 /* Can't allocate that much. Last-ditch
1214 wstr
= (wchar_t *) realloc (*strptr
,
1216 * sizeof (wchar_t));
1219 if (flags
& POSIX_MALLOC
)
1224 /* We lose. Oh well. Terminate the string
1225 and stop converting, so at least we don't
1227 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1234 *strptr
= (char *) wstr
;
1241 *strptr
= (char *) wstr
;
1257 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
1260 if (n
== (size_t) -2)
1262 /* Possibly correct character, just not enough
1264 if (__builtin_expect (inchar () == EOF
, 0))
1271 if (__builtin_expect (n
!= 1, 0))
1274 /* We have a match. */
1279 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
1280 && wstr
== (wchar_t *) *strptr
+ strsize
)
1282 /* Enlarge the buffer. */
1283 wstr
= (wchar_t *) realloc (*strptr
,
1285 * sizeof (wchar_t)));
1288 /* Can't allocate that much. Last-ditch effort. */
1289 wstr
= (wchar_t *) realloc (*strptr
,
1291 * sizeof (wchar_t)));
1294 if (flags
& POSIX_MALLOC
)
1299 /* We lose. Oh well. Terminate the
1300 string and stop converting, so at
1301 least we don't skip any input. */
1302 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1309 *strptr
= (char *) wstr
;
1316 *strptr
= (char *) wstr
;
1324 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
1326 if (!(flags
& SUPPRESS
))
1330 if ((flags
& MALLOC
) && wstr
- (wchar_t *) *strptr
!= strsize
)
1332 wchar_t *cp
= (wchar_t *) realloc (*strptr
,
1334 - (wchar_t *) *strptr
)
1335 * sizeof(wchar_t)));
1337 *strptr
= (char *) cp
;
1346 case L_('x'): /* Hexadecimal integer. */
1347 case L_('X'): /* Ditto. */
1351 case L_('o'): /* Octal integer. */
1355 case L_('u'): /* Unsigned decimal integer. */
1359 case L_('d'): /* Signed decimal integer. */
1361 flags
|= NUMBER_SIGNED
;
1364 case L_('i'): /* Generic number. */
1366 flags
|= NUMBER_SIGNED
;
1370 if (__builtin_expect (c
== EOF
, 0))
1373 /* Check for a sign. */
1374 if (c
== L_('-') || c
== L_('+'))
1382 /* Look for a leading indication of base. */
1383 if (width
!= 0 && c
== L_('0'))
1391 if (width
!= 0 && TOLOWER (c
) == L_('x'))
1409 if (base
== 10 && __builtin_expect ((flags
& I18N
) != 0, 0))
1414 #ifdef COMPILE_WSCANF
1415 const wchar_t *wcdigits
[10];
1416 const wchar_t *wcdigits_extended
[10];
1418 const char *mbdigits
[10];
1419 const char *mbdigits_extended
[10];
1421 /* "to_inpunct" is a map from ASCII digits to their
1422 equivalent in locale. This is defined for locales
1423 which use an extra digits set. */
1424 wctrans_t map
= __wctrans ("to_inpunct");
1428 #ifdef COMPILE_WSCANF
1429 to_level
= _NL_CURRENT_WORD (LC_CTYPE
,
1430 _NL_CTYPE_INDIGITS_WC_LEN
) - 1;
1432 to_level
= (uint32_t) curctype
->values
[_NL_ITEM_INDEX (_NL_CTYPE_INDIGITS_MB_LEN
)].word
- 1;
1435 /* Get the alternative digit forms if there are any. */
1436 if (__builtin_expect (map
!= NULL
, 0))
1438 /* Adding new level for extra digits set in locale file. */
1441 for (n
= 0; n
< 10; ++n
)
1443 #ifdef COMPILE_WSCANF
1444 wcdigits
[n
] = (const wchar_t *)
1445 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1447 wchar_t *wc_extended
= (wchar_t *)
1448 alloca ((to_level
+ 2) * sizeof (wchar_t));
1449 __wmemcpy (wc_extended
, wcdigits
[n
], to_level
);
1450 wc_extended
[to_level
] = __towctrans (L
'0' + n
, map
);
1451 wc_extended
[to_level
+ 1] = '\0';
1452 wcdigits_extended
[n
] = wc_extended
;
1455 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1457 /* Get the equivalent wide char in map. */
1458 wint_t extra_wcdigit
= __towctrans (L
'0' + n
, map
);
1460 /* Convert it to multibyte representation. */
1462 memset (&state
, '\0', sizeof (state
));
1464 char extra_mbdigit
[MB_LEN_MAX
];
1466 = __wcrtomb (extra_mbdigit
, extra_wcdigit
, &state
);
1468 if (mblen
== (size_t) -1)
1470 /* Ignore this new level. */
1475 /* Calculate the length of mbdigits[n]. */
1476 const char *last_char
= mbdigits
[n
];
1477 for (level
= 0; level
< to_level
; ++level
)
1478 last_char
= strchr (last_char
, '\0') + 1;
1480 size_t mbdigits_len
= last_char
- mbdigits
[n
];
1482 /* Allocate memory for extended multibyte digit. */
1484 mb_extended
= (char *) alloca (mbdigits_len
+ mblen
+ 1);
1486 /* And get the mbdigits + extra_digit string. */
1487 *(char *) __mempcpy (__mempcpy (mb_extended
, mbdigits
[n
],
1489 extra_mbdigit
, mblen
) = '\0';
1490 mbdigits_extended
[n
] = mb_extended
;
1495 /* Read the number into workspace. */
1496 while (c
!= EOF
&& width
!= 0)
1498 /* In this round we get the pointer to the digit strings
1499 and also perform the first round of comparisons. */
1500 for (n
= 0; n
< 10; ++n
)
1502 /* Get the string for the digits with value N. */
1503 #ifdef COMPILE_WSCANF
1504 if (__builtin_expect (map
!= NULL
, 0))
1505 wcdigits
[n
] = wcdigits_extended
[n
];
1507 wcdigits
[n
] = (const wchar_t *)
1508 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1509 wcdigits
[n
] += from_level
;
1511 if (c
== (wint_t) *wcdigits
[n
])
1513 to_level
= from_level
;
1517 /* Advance the pointer to the next string. */
1521 int avail
= width
> 0 ? width
: INT_MAX
;
1523 if (__builtin_expect (map
!= NULL
, 0))
1524 mbdigits
[n
] = mbdigits_extended
[n
];
1527 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1529 for (level
= 0; level
< from_level
; level
++)
1530 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1533 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1535 if (*++cmpp
== '\0')
1539 if (avail
== 0 || inchar () == EOF
)
1549 to_level
= from_level
;
1553 /* We are pushing all read characters back. */
1554 if (cmpp
> mbdigits
[n
])
1557 while (--cmpp
> mbdigits
[n
])
1558 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1559 c
= (unsigned char) *cmpp
;
1562 /* Advance the pointer to the next string. */
1563 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1569 /* Have not yet found the digit. */
1570 for (level
= from_level
+ 1; level
<= to_level
; ++level
)
1572 /* Search all ten digits of this level. */
1573 for (n
= 0; n
< 10; ++n
)
1575 #ifdef COMPILE_WSCANF
1576 if (c
== (wint_t) *wcdigits
[n
])
1579 /* Advance the pointer to the next string. */
1583 int avail
= width
> 0 ? width
: INT_MAX
;
1586 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1588 if (*++cmpp
== '\0')
1592 if (avail
== 0 || inchar () == EOF
)
1605 /* We are pushing all read characters back. */
1606 if (cmpp
> mbdigits
[n
])
1609 while (--cmpp
> mbdigits
[n
])
1610 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1611 c
= (unsigned char) *cmpp
;
1614 /* Advance the pointer to the next string. */
1615 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1631 else if (flags
& GROUP
)
1633 /* Try matching against the thousands separator. */
1634 #ifdef COMPILE_WSCANF
1638 const char *cmpp
= thousands
;
1639 int avail
= width
> 0 ? width
: INT_MAX
;
1641 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1644 if (*++cmpp
== '\0')
1648 if (avail
== 0 || inchar () == EOF
)
1656 /* We are pushing all read characters back. */
1657 if (cmpp
> thousands
)
1659 wpsize
-= cmpp
- thousands
;
1661 while (--cmpp
> thousands
)
1662 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1663 c
= (unsigned char) *cmpp
;
1671 /* The last thousands character will be added back by
1687 /* Read the number into workspace. */
1688 while (c
!= EOF
&& width
!= 0)
1695 else if (!ISDIGIT (c
) || (int) (c
- L_('0')) >= base
)
1697 if (base
== 10 && (flags
& GROUP
))
1699 /* Try matching against the thousands separator. */
1700 #ifdef COMPILE_WSCANF
1704 const char *cmpp
= thousands
;
1705 int avail
= width
> 0 ? width
: INT_MAX
;
1707 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1710 if (*++cmpp
== '\0')
1714 if (avail
== 0 || inchar () == EOF
)
1722 /* We are pushing all read characters back. */
1723 if (cmpp
> thousands
)
1725 wpsize
-= cmpp
- thousands
;
1727 while (--cmpp
> thousands
)
1728 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1729 c
= (unsigned char) *cmpp
;
1737 /* The last thousands character will be added back by
1753 || (wpsize
== 1 && (wp
[0] == L_('+') || wp
[0] == L_('-'))))
1755 /* There was no number. If we are supposed to read a pointer
1756 we must recognize "(nil)" as well. */
1757 if (__builtin_expect (wpsize
== 0
1758 && (flags
& READ_POINTER
)
1759 && (width
< 0 || width
>= 0)
1761 && TOLOWER (inchar ()) == L_('n')
1762 && TOLOWER (inchar ()) == L_('i')
1763 && TOLOWER (inchar ()) == L_('l')
1764 && inchar () == L_(')'), 1))
1765 /* We must produce the value of a NULL pointer. A single
1766 '0' digit is enough. */
1770 /* The last read character is not part of the number
1778 /* The just read character is not part of the number anymore. */
1781 /* Convert the number. */
1783 if (need_longlong
&& (flags
& LONGDBL
))
1785 if (flags
& NUMBER_SIGNED
)
1786 num
.q
= __strtoll_internal (wp
, &tw
, base
, flags
& GROUP
);
1788 num
.uq
= __strtoull_internal (wp
, &tw
, base
, flags
& GROUP
);
1792 if (flags
& NUMBER_SIGNED
)
1793 num
.l
= __strtol_internal (wp
, &tw
, base
, flags
& GROUP
);
1795 num
.ul
= __strtoul_internal (wp
, &tw
, base
, flags
& GROUP
);
1797 if (__builtin_expect (wp
== tw
, 0))
1800 if (!(flags
& SUPPRESS
))
1802 if (flags
& NUMBER_SIGNED
)
1804 if (need_longlong
&& (flags
& LONGDBL
))
1805 *ARG (LONGLONG
int *) = num
.q
;
1806 else if (need_long
&& (flags
& LONG
))
1807 *ARG (long int *) = num
.l
;
1808 else if (flags
& SHORT
)
1809 *ARG (short int *) = (short int) num
.l
;
1810 else if (!(flags
& CHAR
))
1811 *ARG (int *) = (int) num
.l
;
1813 *ARG (signed char *) = (signed char) num
.ul
;
1817 if (need_longlong
&& (flags
& LONGDBL
))
1818 *ARG (unsigned LONGLONG
int *) = num
.uq
;
1819 else if (need_long
&& (flags
& LONG
))
1820 *ARG (unsigned long int *) = num
.ul
;
1821 else if (flags
& SHORT
)
1822 *ARG (unsigned short int *)
1823 = (unsigned short int) num
.ul
;
1824 else if (!(flags
& CHAR
))
1825 *ARG (unsigned int *) = (unsigned int) num
.ul
;
1827 *ARG (unsigned char *) = (unsigned char) num
.ul
;
1833 case L_('e'): /* Floating-point numbers. */
1844 if (__builtin_expect (c
== EOF
, 0))
1847 got_dot
= got_e
= 0;
1849 /* Check for a sign. */
1850 if (c
== L_('-') || c
== L_('+'))
1852 negative
= c
== L_('-');
1853 if (__builtin_expect (width
== 0 || inchar () == EOF
, 0))
1854 /* EOF is only an input error before we read any chars. */
1862 /* Take care for the special arguments "nan" and "inf". */
1863 if (TOLOWER (c
) == L_('n'))
1867 if (__builtin_expect (width
== 0
1869 || TOLOWER (c
) != L_('a'), 0))
1874 if (__builtin_expect (width
== 0
1876 || TOLOWER (c
) != L_('n'), 0))
1884 else if (TOLOWER (c
) == L_('i'))
1886 /* Maybe "inf" or "infinity". */
1888 if (__builtin_expect (width
== 0
1890 || TOLOWER (c
) != L_('n'), 0))
1895 if (__builtin_expect (width
== 0
1897 || TOLOWER (c
) != L_('f'), 0))
1902 /* It is as least "inf". */
1903 if (width
!= 0 && inchar () != EOF
)
1905 if (TOLOWER (c
) == L_('i'))
1909 /* Now we have to read the rest as well. */
1911 if (__builtin_expect (width
== 0
1913 || TOLOWER (c
) != L_('n'), 0))
1918 if (__builtin_expect (width
== 0
1920 || TOLOWER (c
) != L_('i'), 0))
1925 if (__builtin_expect (width
== 0
1927 || TOLOWER (c
) != L_('t'), 0))
1932 if (__builtin_expect (width
== 0
1934 || TOLOWER (c
) != L_('y'), 0))
1948 if (width
!= 0 && c
== L_('0'))
1954 if (width
!= 0 && TOLOWER (c
) == L_('x'))
1956 /* It is a number in hexadecimal format. */
1959 flags
|= HEXA_FLOAT
;
1962 /* Grouping is not allowed. */
1974 else if (!got_e
&& (flags
& HEXA_FLOAT
) && ISXDIGIT (c
))
1976 else if (got_e
&& wp
[wpsize
- 1] == exp_char
1977 && (c
== L_('-') || c
== L_('+')))
1979 else if (wpsize
> 0 && !got_e
1980 && (CHAR_T
) TOLOWER (c
) == exp_char
)
1983 got_e
= got_dot
= 1;
1987 #ifdef COMPILE_WSCANF
1988 if (! got_dot
&& c
== decimal
)
1993 else if ((flags
& GROUP
) != 0 && ! got_dot
&& c
== thousands
)
1997 /* The last read character is not part of the number
2003 const char *cmpp
= decimal
;
2004 int avail
= width
> 0 ? width
: INT_MAX
;
2008 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
2009 if (*++cmpp
== '\0')
2013 if (avail
== 0 || inchar () == EOF
)
2021 /* Add all the characters. */
2022 for (cmpp
= decimal
; *cmpp
!= '\0'; ++cmpp
)
2023 ADDW ((unsigned char) *cmpp
);
2030 /* Figure out whether it is a thousands separator.
2031 There is one problem: we possibly read more than
2032 one character. We cannot push them back but since
2033 we know that parts of the `decimal' string matched,
2034 we can compare against it. */
2035 const char *cmp2p
= thousands
;
2037 if ((flags
& GROUP
) != 0 && ! got_dot
)
2039 while (cmp2p
- thousands
< cmpp
- decimal
2040 && *cmp2p
== decimal
[cmp2p
- thousands
])
2042 if (cmp2p
- thousands
== cmpp
- decimal
)
2044 while ((unsigned char) *cmp2p
== c
&& avail
>= 0)
2045 if (*++cmp2p
== '\0')
2049 if (avail
== 0 || inchar () == EOF
)
2056 if (cmp2p
!= NULL
&& *cmp2p
== '\0')
2058 /* Add all the characters. */
2059 for (cmpp
= thousands
; *cmpp
!= '\0'; ++cmpp
)
2060 ADDW ((unsigned char) *cmpp
);
2066 /* The last read character is not part of the number
2075 if (width
== 0 || inchar () == EOF
)
2083 if (__builtin_expect ((flags
& I18N
) != 0, 0)
2084 /* Hexadecimal floats make no sense, fixing localized
2085 digits with ASCII letters. */
2086 && !(flags
& HEXA_FLOAT
)
2087 /* Minimum requirement. */
2088 && (wpsize
== 0 || got_dot
)
2089 && (map
= __wctrans ("to_inpunct")) != NULL
)
2091 /* Reget the first character. */
2094 /* Localized digits, decimal points, and thousands
2096 wint_t wcdigits
[12];
2098 /* First get decimal equivalent to check if we read it
2100 wcdigits
[11] = __towctrans (L
'.', map
);
2102 /* If we have not read any character or have just read
2103 locale decimal point which matches the decimal point
2104 for localized FP numbers, then we may have localized
2105 digits. Note, we test GOT_DOT above. */
2106 #ifdef COMPILE_WSCANF
2107 if (wpsize
== 0 || (wpsize
== 1 && wcdigits
[11] == decimal
))
2109 char mbdigits
[12][MB_LEN_MAX
+ 1];
2112 memset (&state
, '\0', sizeof (state
));
2114 bool match_so_far
= wpsize
== 0;
2115 size_t mblen
= __wcrtomb (mbdigits
[11], wcdigits
[11], &state
);
2116 if (mblen
!= (size_t) -1)
2118 mbdigits
[11][mblen
] = '\0';
2119 match_so_far
|= (wpsize
== strlen (decimal
)
2120 && strcmp (decimal
, mbdigits
[11]) == 0);
2124 size_t decimal_len
= strlen (decimal
);
2125 /* This should always be the case but the data comes
2127 if (decimal_len
<= MB_LEN_MAX
)
2129 match_so_far
|= wpsize
== decimal_len
;
2130 memcpy (mbdigits
[11], decimal
, decimal_len
+ 1);
2133 match_so_far
= false;
2139 bool have_locthousands
= (flags
& GROUP
) != 0;
2141 /* Now get the digits and the thousands-sep equivalents. */
2142 for (int n
= 0; n
< 11; ++n
)
2145 wcdigits
[n
] = __towctrans (L
'0' + n
, map
);
2148 wcdigits
[10] = __towctrans (L
',', map
);
2149 have_locthousands
&= wcdigits
[10] != L
'\0';
2152 #ifndef COMPILE_WSCANF
2153 memset (&state
, '\0', sizeof (state
));
2155 size_t mblen
= __wcrtomb (mbdigits
[n
], wcdigits
[n
],
2157 if (mblen
== (size_t) -1)
2161 if (have_locthousands
)
2163 size_t thousands_len
= strlen (thousands
);
2164 if (thousands_len
<= MB_LEN_MAX
)
2165 memcpy (mbdigits
[10], thousands
,
2168 have_locthousands
= false;
2172 /* Ignore checking against localized digits. */
2176 mbdigits
[n
][mblen
] = '\0';
2180 /* Start checking against localized digits, if
2181 convertion is done correctly. */
2184 if (got_e
&& wp
[wpsize
- 1] == exp_char
2185 && (c
== L_('-') || c
== L_('+')))
2187 else if (wpsize
> 0 && !got_e
2188 && (CHAR_T
) TOLOWER (c
) == exp_char
)
2191 got_e
= got_dot
= 1;
2195 /* Check against localized digits, decimal point,
2196 and thousands separator. */
2198 for (n
= 0; n
< 12; ++n
)
2200 #ifdef COMPILE_WSCANF
2201 if (c
== wcdigits
[n
])
2205 else if (n
== 11 && !got_dot
)
2210 else if (n
== 10 && have_locthousands
2214 /* The last read character is not part
2215 of the number anymore. */
2221 const char *cmpp
= mbdigits
[n
];
2222 int avail
= width
> 0 ? width
: INT_MAX
;
2224 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
2225 if (*++cmpp
== '\0')
2229 if (avail
== 0 || inchar () == EOF
)
2240 else if (n
== 11 && !got_dot
)
2242 /* Add all the characters. */
2243 for (cmpp
= decimal
; *cmpp
!= '\0';
2245 ADDW ((unsigned char) *cmpp
);
2249 else if (n
== 10 && (flags
& GROUP
) != 0
2252 /* Add all the characters. */
2253 for (cmpp
= thousands
; *cmpp
!= '\0';
2255 ADDW ((unsigned char) *cmpp
);
2258 /* The last read character is not part
2259 of the number anymore. */
2265 /* We are pushing all read characters back. */
2266 if (cmpp
> mbdigits
[n
])
2269 while (--cmpp
> mbdigits
[n
])
2270 ungetc_not_eof ((unsigned char) *cmpp
, s
);
2271 c
= (unsigned char) *cmpp
;
2278 /* The last read character is not part
2279 of the number anymore. */
2285 if (width
== 0 || inchar () == EOF
)
2293 #ifndef COMPILE_WSCANF
2299 /* Have we read any character? If we try to read a number
2300 in hexadecimal notation and we have read only the `0x'
2301 prefix this is an error. */
2302 if (__builtin_expect (wpsize
== 0
2303 || ((flags
& HEXA_FLOAT
) && wpsize
== 2), 0))
2307 /* Convert the number. */
2309 if ((flags
& LONGDBL
) && !__ldbl_is_dbl
)
2311 long double d
= __strtold_internal (wp
, &tw
, flags
& GROUP
);
2312 if (!(flags
& SUPPRESS
) && tw
!= wp
)
2313 *ARG (long double *) = negative
? -d
: d
;
2315 else if (flags
& (LONG
| LONGDBL
))
2317 double d
= __strtod_internal (wp
, &tw
, flags
& GROUP
);
2318 if (!(flags
& SUPPRESS
) && tw
!= wp
)
2319 *ARG (double *) = negative
? -d
: d
;
2323 float d
= __strtof_internal (wp
, &tw
, flags
& GROUP
);
2324 if (!(flags
& SUPPRESS
) && tw
!= wp
)
2325 *ARG (float *) = negative
? -d
: d
;
2328 if (__builtin_expect (tw
== wp
, 0))
2331 if (!(flags
& SUPPRESS
))
2335 case L_('['): /* Character class. */
2337 STRING_ARG (wstr
, wchar_t, 100);
2339 STRING_ARG (str
, char, 100);
2350 /* There is no width given so there is also no limit on the
2351 number of characters we read. Therefore we set width to
2352 a very high value to make the algorithm easier. */
2355 #ifdef COMPILE_WSCANF
2356 /* Find the beginning and the end of the scanlist. We are not
2357 creating a lookup table since it would have to be too large.
2358 Instead we search each time through the string. This is not
2359 a constant lookup time but who uses this feature deserves to
2361 tw
= (wchar_t *) f
; /* Marks the beginning. */
2366 while ((fc
= *f
++) != L
'\0' && fc
!= L
']');
2368 if (__builtin_expect (fc
== L
'\0', 0))
2370 wchar_t *twend
= (wchar_t *) f
- 1;
2372 /* Fill WP with byte flags indexed by character.
2373 We will use this flag map for matching input characters. */
2374 if (wpmax
< UCHAR_MAX
+ 1)
2376 wpmax
= UCHAR_MAX
+ 1;
2377 wp
= (char *) alloca (wpmax
);
2379 memset (wp
, '\0', UCHAR_MAX
+ 1);
2382 if (fc
== ']' || fc
== '-')
2384 /* If ] or - appears before any char in the set, it is not
2385 the terminator or separator, but the first char in the
2391 while ((fc
= *f
++) != '\0' && fc
!= ']')
2392 if (fc
== '-' && *f
!= '\0' && *f
!= ']'
2393 && (unsigned char) f
[-2] <= (unsigned char) *f
)
2395 /* Add all characters from the one before the '-'
2396 up to (but not including) the next format char. */
2397 for (fc
= (unsigned char) f
[-2]; fc
< (unsigned char) *f
; ++fc
)
2401 /* Add the character to the flag map. */
2404 if (__builtin_expect (fc
== '\0', 0))
2410 size_t now
= read_in
;
2411 #ifdef COMPILE_WSCANF
2412 if (__builtin_expect (inchar () == WEOF
, 0))
2419 /* Test whether it's in the scanlist. */
2421 while (runp
< twend
)
2423 if (runp
[0] == L
'-' && runp
[1] != '\0'
2424 && runp
+ 1 != twend
2426 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2428 /* Match against all characters in between the
2429 first and last character of the sequence. */
2432 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2433 if ((wint_t) wc
== c
)
2436 if (wc
<= runp
[1] && !not_in
)
2438 if (wc
<= runp
[1] && not_in
)
2440 /* The current character is not in the
2450 if ((wint_t) *runp
== c
&& !not_in
)
2452 if ((wint_t) *runp
== c
&& not_in
)
2462 if (runp
== twend
&& !not_in
)
2468 if (!(flags
& SUPPRESS
))
2472 if ((flags
& MALLOC
)
2473 && wstr
== (wchar_t *) *strptr
+ strsize
)
2475 /* Enlarge the buffer. */
2476 wstr
= (wchar_t *) realloc (*strptr
,
2478 * sizeof (wchar_t));
2481 /* Can't allocate that much. Last-ditch
2484 realloc (*strptr
, (strsize
+ 1)
2485 * sizeof (wchar_t));
2488 if (flags
& POSIX_MALLOC
)
2493 /* We lose. Oh well. Terminate the string
2494 and stop converting, so at least we don't
2496 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2503 *strptr
= (char *) wstr
;
2510 *strptr
= (char *) wstr
;
2517 while (--width
> 0 && inchar () != WEOF
);
2520 char buf
[MB_LEN_MAX
];
2524 if (__builtin_expect (inchar () == EOF
, 0))
2527 memset (&cstate
, '\0', sizeof (cstate
));
2531 if (wp
[c
] == not_in
)
2533 ungetc_not_eof (c
, s
);
2538 if (!(flags
& SUPPRESS
))
2542 /* Convert it into a wide character. */
2544 n
= __mbrtowc (wstr
, buf
, 1, &cstate
);
2546 if (n
== (size_t) -2)
2548 /* Possibly correct character, just not enough
2551 assert (cnt
< MB_CUR_MAX
);
2557 if ((flags
& MALLOC
)
2558 && wstr
== (wchar_t *) *strptr
+ strsize
)
2560 /* Enlarge the buffer. */
2561 wstr
= (wchar_t *) realloc (*strptr
,
2563 * sizeof (wchar_t)));
2566 /* Can't allocate that much. Last-ditch
2569 realloc (*strptr
, ((strsize
+ 1)
2570 * sizeof (wchar_t)));
2573 if (flags
& POSIX_MALLOC
)
2578 /* We lose. Oh well. Terminate the
2579 string and stop converting,
2580 so at least we don't skip any input. */
2581 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2588 *strptr
= (char *) wstr
;
2595 *strptr
= (char *) wstr
;
2605 while (inchar () != EOF
);
2607 if (__builtin_expect (cnt
!= 0, 0))
2608 /* We stopped in the middle of recognizing another
2609 character. That's a problem. */
2613 if (__builtin_expect (now
== read_in
, 0))
2614 /* We haven't succesfully read any character. */
2617 if (!(flags
& SUPPRESS
))
2621 if ((flags
& MALLOC
)
2622 && wstr
- (wchar_t *) *strptr
!= strsize
)
2624 wchar_t *cp
= (wchar_t *)
2625 realloc (*strptr
, ((wstr
- (wchar_t *) *strptr
)
2626 * sizeof(wchar_t)));
2628 *strptr
= (char *) cp
;
2637 size_t now
= read_in
;
2639 if (__builtin_expect (inchar () == EOF
, 0))
2642 #ifdef COMPILE_WSCANF
2644 memset (&state
, '\0', sizeof (state
));
2651 /* Test whether it's in the scanlist. */
2653 while (runp
< twend
)
2655 if (runp
[0] == L
'-' && runp
[1] != '\0'
2656 && runp
+ 1 != twend
2658 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2660 /* Match against all characters in between the
2661 first and last character of the sequence. */
2664 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2665 if ((wint_t) wc
== c
)
2668 if (wc
<= runp
[1] && !not_in
)
2670 if (wc
<= runp
[1] && not_in
)
2672 /* The current character is not in the
2682 if ((wint_t) *runp
== c
&& !not_in
)
2684 if ((wint_t) *runp
== c
&& not_in
)
2694 if (runp
== twend
&& !not_in
)
2700 if (!(flags
& SUPPRESS
))
2702 if ((flags
& MALLOC
)
2703 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
2705 /* Enlarge the buffer. */
2706 size_t strleng
= str
- *strptr
;
2709 newstr
= (char *) realloc (*strptr
, 2 * strsize
);
2712 /* Can't allocate that much. Last-ditch
2714 newstr
= (char *) realloc (*strptr
,
2715 strleng
+ MB_CUR_MAX
);
2718 if (flags
& POSIX_MALLOC
)
2723 /* We lose. Oh well. Terminate the string
2724 and stop converting, so at least we don't
2726 ((char *) (*strptr
))[strleng
] = '\0';
2734 str
= newstr
+ strleng
;
2735 strsize
= strleng
+ MB_CUR_MAX
;
2741 str
= newstr
+ strleng
;
2747 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
2748 if (__builtin_expect (n
== (size_t) -1, 0))
2751 assert (n
<= MB_CUR_MAX
);
2754 while (--width
> 0 && inchar () != WEOF
);
2759 if (wp
[c
] == not_in
)
2761 ungetc_not_eof (c
, s
);
2766 if (!(flags
& SUPPRESS
))
2769 if ((flags
& MALLOC
)
2770 && (char *) str
== *strptr
+ strsize
)
2772 /* Enlarge the buffer. */
2773 size_t newsize
= 2 * strsize
;
2776 str
= (char *) realloc (*strptr
, newsize
);
2779 /* Can't allocate that much. Last-ditch
2781 if (newsize
> strsize
+ 1)
2783 newsize
= strsize
+ 1;
2786 if (flags
& POSIX_MALLOC
)
2791 /* We lose. Oh well. Terminate the
2792 string and stop converting,
2793 so at least we don't skip any input. */
2794 ((char *) (*strptr
))[strsize
- 1] = '\0';
2801 *strptr
= (char *) str
;
2808 while (--width
> 0 && inchar () != EOF
);
2811 if (__builtin_expect (now
== read_in
, 0))
2812 /* We haven't succesfully read any character. */
2815 if (!(flags
& SUPPRESS
))
2817 #ifdef COMPILE_WSCANF
2818 /* We have to emit the code to get into the initial
2820 char buf
[MB_LEN_MAX
];
2821 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
2822 if (n
> 0 && (flags
& MALLOC
)
2823 && str
+ n
>= *strptr
+ strsize
)
2825 /* Enlarge the buffer. */
2826 size_t strleng
= str
- *strptr
;
2829 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
2832 if (flags
& POSIX_MALLOC
)
2837 /* We lose. Oh well. Terminate the string
2838 and stop converting, so at least we don't
2840 ((char *) (*strptr
))[strleng
] = '\0';
2848 str
= newstr
+ strleng
;
2849 strsize
= strleng
+ n
+ 1;
2853 str
= __mempcpy (str
, buf
, n
);
2857 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
2859 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
2870 case L_('p'): /* Generic pointer. */
2872 /* A PTR must be the same size as a `long int'. */
2873 flags
&= ~(SHORT
|LONGDBL
);
2876 flags
|= READ_POINTER
;
2880 /* If this is an unknown format character punt. */
2885 /* The last thing we saw int the format string was a white space.
2886 Consume the last white spaces. */
2891 while (ISSPACE (c
));
2896 /* Unlock stream. */
2905 if (__builtin_expect (done
== EOF
, 0))
2907 if (__builtin_expect (ptrs_to_free
!= NULL
, 0))
2909 struct ptrs_to_free
*p
= ptrs_to_free
;
2912 for (size_t cnt
= 0; cnt
< p
->count
; ++cnt
)
2914 free (*p
->ptrs
[cnt
]);
2915 *p
->ptrs
[cnt
] = NULL
;
2922 else if (__builtin_expect (strptr
!= NULL
, 0))
2930 #ifdef COMPILE_WSCANF
2932 __vfwscanf (FILE *s
, const wchar_t *format
, va_list argptr
)
2934 return _IO_vfwscanf (s
, format
, argptr
, NULL
);
2936 ldbl_weak_alias (__vfwscanf
, vfwscanf
)
2939 ___vfscanf (FILE *s
, const char *format
, va_list argptr
)
2941 return _IO_vfscanf_internal (s
, format
, argptr
, NULL
);
2943 ldbl_strong_alias (_IO_vfscanf_internal
, _IO_vfscanf
)
2944 ldbl_strong_alias (___vfscanf
, __vfscanf
)
2945 ldbl_hidden_def (___vfscanf
, __vfscanf
)
2946 ldbl_weak_alias (___vfscanf
, vfscanf
)