1 /* Copyright (C) 1991-2015 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/>. */
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 GNU_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 */
68 #define POSIX_MALLOC 0x2000 /* m: malloc strings */
69 #define MALLOC (GNU_MALLOC | POSIX_MALLOC)
71 #include <locale/localeinfo.h>
76 #define va_list _IO_va_list
79 # define ungetc(c, s) ((void) (c == WEOF \
81 _IO_sputbackwc (s, c))))
82 # define ungetc_not_eof(c, s) ((void) (--read_in, \
83 _IO_sputbackwc (s, c)))
84 # define inchar() (c == WEOF ? ((errno = inchar_errno), WEOF) \
85 : ((c = _IO_getwc_unlocked (s)), \
88 : (size_t) (inchar_errno = errno)), c))
90 # define MEMCPY(d, s, n) __wmemcpy (d, s, n)
91 # define ISSPACE(Ch) iswspace (Ch)
92 # define ISDIGIT(Ch) iswdigit (Ch)
93 # define ISXDIGIT(Ch) iswxdigit (Ch)
94 # define TOLOWER(Ch) towlower (Ch)
95 # define ORIENT if (_IO_fwide (s, 1) != 1) return WEOF
96 # define __strtoll_internal __wcstoll_internal
97 # define __strtoull_internal __wcstoull_internal
98 # define __strtol_internal __wcstol_internal
99 # define __strtoul_internal __wcstoul_internal
100 # define __strtold_internal __wcstold_internal
101 # define __strtod_internal __wcstod_internal
102 # define __strtof_internal __wcstof_internal
104 # define L_(Str) L##Str
105 # define CHAR_T wchar_t
106 # define UCHAR_T unsigned int
107 # define WINT_T wint_t
111 # define ungetc(c, s) ((void) ((int) c == EOF \
113 _IO_sputbackc (s, (unsigned char) c))))
114 # define ungetc_not_eof(c, s) ((void) (--read_in, \
115 _IO_sputbackc (s, (unsigned char) c)))
116 # define inchar() (c == EOF ? ((errno = inchar_errno), EOF) \
117 : ((c = _IO_getc_unlocked (s)), \
120 : (size_t) (inchar_errno = errno)), c))
121 # define MEMCPY(d, s, n) memcpy (d, s, n)
122 # define ISSPACE(Ch) __isspace_l (Ch, loc)
123 # define ISDIGIT(Ch) __isdigit_l (Ch, loc)
124 # define ISXDIGIT(Ch) __isxdigit_l (Ch, loc)
125 # define TOLOWER(Ch) __tolower_l ((unsigned char) (Ch), loc)
126 # define ORIENT if (_IO_vtable_offset (s) == 0 \
127 && _IO_fwide (s, -1) != -1) \
132 # define UCHAR_T unsigned char
136 #define encode_error() do { \
138 __set_errno (EILSEQ); \
141 #define conv_error() do { \
145 #define input_error() do { \
147 if (done == 0) done = EOF; \
150 #define add_ptr_to_free(ptr) \
153 if (ptrs_to_free == NULL \
154 || ptrs_to_free->count == (sizeof (ptrs_to_free->ptrs) \
155 / sizeof (ptrs_to_free->ptrs[0]))) \
157 struct ptrs_to_free *new_ptrs = alloca (sizeof (*ptrs_to_free)); \
158 new_ptrs->count = 0; \
159 new_ptrs->next = ptrs_to_free; \
160 ptrs_to_free = new_ptrs; \
162 ptrs_to_free->ptrs[ptrs_to_free->count++] = (ptr); \
165 #define ARGCHECK(s, format) \
168 /* Check file argument for consistence. */ \
169 CHECK_FILE (s, EOF); \
170 if (s->_flags & _IO_NO_READS) \
172 __set_errno (EBADF); \
175 else if (format == NULL) \
181 #define LOCK_STREAM(S) \
182 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, (S)); \
184 #define UNLOCK_STREAM(S) \
185 _IO_funlockfile (S); \
186 __libc_cleanup_region_end (0)
191 struct ptrs_to_free *next;
195 /* Read formatted input from S according to the format string
196 FORMAT, using the argument list in ARG.
197 Return the number of assignments made, or -1 for an input error. */
198 #ifdef COMPILE_WSCANF
200 _IO_vfwscanf (_IO_FILE *s, const wchar_t *format, _IO_va_list argptr,
204 _IO_vfscanf_internal (_IO_FILE *s, const char *format, _IO_va_list argptr,
209 const CHAR_T *f = format;
210 UCHAR_T fc; /* Current character of the format. */
211 WINT_T done = 0; /* Assignments done. */
212 size_t read_in = 0; /* Chars read in. */
213 WINT_T c = 0; /* Last char read. */
214 int width; /* Maximum field width. */
215 int flags; /* Modifiers for current format element. */
217 #ifndef COMPILE_WSCANF
218 __locale_t loc = _NL_CURRENT_LOCALE;
219 struct __locale_data *const curctype = loc->__locales[LC_CTYPE];
222 /* Errno of last failed inchar call. */
223 int inchar_errno = 0;
224 /* Status for reading F-P nums. */
225 char got_digit, got_dot, got_e, negative;
226 /* If a [...] is a [^...]. */
228 #define exp_char not_in
229 /* Base for integral numbers. */
231 /* Decimal point character. */
232 #ifdef COMPILE_WSCANF
237 /* The thousands character of the current locale. */
238 #ifdef COMPILE_WSCANF
241 const char *thousands;
243 struct ptrs_to_free *ptrs_to_free = NULL;
244 /* State for the conversions. */
246 /* Integral holding variables. */
250 unsigned long long int uq;
252 unsigned long int ul;
254 /* Character-buffer pointer. */
256 wchar_t *wstr = NULL;
257 char **strptr = NULL;
259 /* We must not react on white spaces immediately because they can
260 possibly be matched even if in the input stream no character is
261 available anymore. */
264 CHAR_T *tw; /* Temporary pointer. */
265 CHAR_T *wp = NULL; /* Workspace. */
266 size_t wpmax = 0; /* Maximal size of workspace. */
267 size_t wpsize; /* Currently used bytes in workspace. */
268 bool use_malloc = false;
272 if (__glibc_unlikely (wpsize == wpmax)) \
275 size_t newsize = (UCHAR_MAX + 1 > 2 * wpmax \
276 ? UCHAR_MAX + 1 : 2 * wpmax); \
277 if (use_malloc || !__libc_use_alloca (newsize)) \
279 wp = realloc (use_malloc ? wp : NULL, newsize); \
288 MEMCPY (wp, old, wpsize); \
294 size_t s = wpmax * sizeof (CHAR_T); \
295 wp = (CHAR_T *) extend_alloca (wp, s, \
296 newsize * sizeof (CHAR_T)); \
297 wpmax = s / sizeof (CHAR_T); \
299 MEMCPY (wp, old, wpsize); \
302 wp[wpsize++] = (Ch); \
307 __va_copy (arg, argptr);
309 arg = (va_list) argptr;
316 ARGCHECK (s, format);
319 #ifndef COMPILE_WSCANF
320 struct __locale_data *const curnumeric = loc->__locales[LC_NUMERIC];
323 /* Figure out the decimal point character. */
324 #ifdef COMPILE_WSCANF
325 decimal = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
327 decimal = curnumeric->values[_NL_ITEM_INDEX (DECIMAL_POINT)].string;
329 /* Figure out the thousands separator character. */
330 #ifdef COMPILE_WSCANF
331 thousands = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_THOUSANDS_SEP_WC);
333 thousands = curnumeric->values[_NL_ITEM_INDEX (THOUSANDS_SEP)].string;
334 if (*thousands == '\0')
339 /* Lock the stream. */
343 #ifndef COMPILE_WSCANF
344 /* From now on we use `state' to convert the format string. */
345 memset (&state, '\0', sizeof (state));
348 /* Run through the format string. */
352 /* Extract the next argument, which is of type TYPE.
353 For a %N$... spec, this is the Nth argument from the beginning;
354 otherwise it is the next argument after the state now in ARG. */
356 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
357 ({ unsigned int pos = argpos; \
359 __va_copy (arg, argptr); \
361 (void) va_arg (arg, void *); \
362 va_arg (arg, type); \
366 /* XXX Possible optimization. */
367 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
368 ({ va_list arg = (va_list) argptr; \
369 arg = (va_list) ((char *) arg \
371 * __va_rounded_size (void *)); \
372 va_arg (arg, type); \
375 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
376 ({ unsigned int pos = argpos; \
377 va_list arg = (va_list) argptr; \
379 (void) va_arg (arg, void *); \
380 va_arg (arg, type); \
385 #ifndef COMPILE_WSCANF
386 if (!isascii ((unsigned char) *f))
388 /* Non-ASCII, may be a multibyte. */
389 int len = __mbrlen (f, strlen (f), &state);
395 if (__glibc_unlikely (c == EOF))
397 else if (c != (unsigned char) *f++)
399 ungetc_not_eof (c, s);
412 /* Remember to skip spaces. */
419 /* Read a character. */
422 /* Characters other than format specs must just match. */
423 if (__glibc_unlikely (c == EOF))
426 /* We saw white space char as the last character in the format
427 string. Now it's time to skip all leading white space. */
431 if (__glibc_unlikely (inchar () == EOF))
436 if (__glibc_unlikely (c != fc))
445 /* This is the start of the conversion string. */
448 /* Initialize state of modifiers. */
451 /* Prepare temporary buffer. */
454 /* Check for a positional parameter specification. */
455 if (ISDIGIT ((UCHAR_T) *f))
457 argpos = (UCHAR_T) *f++ - L_('0');
458 while (ISDIGIT ((UCHAR_T) *f))
459 argpos = argpos * 10 + ((UCHAR_T) *f++ - L_('0'));
464 /* Oops; that was actually the field width. */
471 /* Check for the assignment-suppressing, the number grouping flag,
472 and the signal to use the locale's digit representation. */
473 while (*f == L_('*') || *f == L_('\'') || *f == L_('I'))
480 #ifdef COMPILE_WSCANF
481 if (thousands != L'\0')
483 if (thousands != NULL)
492 /* Find the maximum field width. */
494 while (ISDIGIT ((UCHAR_T) *f))
497 width += (UCHAR_T) *f++ - L_('0');
503 /* Check for type modifiers. */
507 /* ints are short ints or chars. */
519 /* A double `l' is equivalent to an `L'. */
521 flags |= LONGDBL | LONG;
524 /* ints are long ints. */
529 /* doubles are long doubles, and ints are long long ints. */
530 flags |= LONGDBL | LONG;
533 /* The `a' is used as a flag only if followed by `s', `S' or
535 if (*f != L_('s') && *f != L_('S') && *f != L_('['))
540 /* In __isoc99_*scanf %as, %aS and %a[ extension is not
542 if (s->_flags2 & _IO_FLAGS2_SCANF_STD)
547 /* String conversions (%s, %[) take a `char **'
548 arg and fill it in with a malloc'd pointer. */
552 flags |= POSIX_MALLOC;
560 if (need_longlong && sizeof (size_t) > sizeof (unsigned long int))
562 else if (sizeof (size_t) > sizeof (unsigned int))
566 if (need_longlong && sizeof (uintmax_t) > sizeof (unsigned long int))
568 else if (sizeof (uintmax_t) > sizeof (unsigned int))
572 if (need_longlong && sizeof (ptrdiff_t) > sizeof (long int))
574 else if (sizeof (ptrdiff_t) > sizeof (int))
578 /* Not a recognized modifier. Backup. */
583 /* End of the format string? */
584 if (__glibc_unlikely (*f == L_('\0')))
587 /* Find the conversion specifier. */
589 if (skip_space || (fc != L_('[') && fc != L_('c')
590 && fc != L_('C') && fc != L_('n')))
592 /* Eat whitespace. */
593 int save_errno = errno;
596 /* We add the additional test for EOF here since otherwise
597 inchar will restore the old errno value which might be
598 EINTR but does not indicate an interrupt since nothing
599 was read at this time. */
600 if (__builtin_expect ((c == EOF || inchar () == EOF)
601 && errno == EINTR, 0))
604 __set_errno (save_errno);
611 case L_('%'): /* Must match a literal '%'. */
613 if (__glibc_unlikely (c == EOF))
615 if (__glibc_unlikely (c != fc))
617 ungetc_not_eof (c, s);
622 case L_('n'): /* Answer number of assignments done. */
623 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
624 with the 'n' conversion specifier. */
625 if (!(flags & SUPPRESS))
627 /* Don't count the read-ahead. */
628 if (need_longlong && (flags & LONGDBL))
629 *ARG (long long int *) = read_in;
630 else if (need_long && (flags & LONG))
631 *ARG (long int *) = read_in;
632 else if (flags & SHORT)
633 *ARG (short int *) = read_in;
634 else if (!(flags & CHAR))
635 *ARG (int *) = read_in;
637 *ARG (char *) = read_in;
639 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
640 /* We have a severe problem here. The ISO C standard
641 contradicts itself in explaining the effect of the %n
642 format in `scanf'. While in ISO C:1990 and the ISO C
643 Amendement 1:1995 the result is described as
645 Execution of a %n directive does not effect the
646 assignment count returned at the completion of
647 execution of the f(w)scanf function.
649 in ISO C Corrigendum 1:1994 the following was added:
652 Add the following fourth example:
655 int d1, d2, n1, n2, i;
656 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
657 the value 123 is assigned to d1 and the value3 to n1.
658 Because %n can never get an input failure the value
659 of 3 is also assigned to n2. The value of d2 is not
660 affected. The value 3 is assigned to i.
662 We go for now with the historically correct code from ISO C,
663 i.e., we don't count the %n assignments. When it ever
664 should proof to be wrong just remove the #ifdef above. */
670 case L_('c'): /* Match characters. */
671 if ((flags & LONG) == 0)
676 #define STRING_ARG(Str, Type, Width) \
677 do if (!(flags & SUPPRESS)) \
679 if (flags & MALLOC) \
681 /* The string is to be stored in a malloc'd buffer. */ \
682 /* For %mS using char ** is actually wrong, but \
683 shouldn't make a difference on any arch glibc \
684 supports and would unnecessarily complicate \
686 strptr = ARG (char **); \
687 if (strptr == NULL) \
689 /* Allocate an initial buffer. */ \
691 *strptr = (char *) malloc (strsize * sizeof (Type)); \
692 Str = (Type *) *strptr; \
694 add_ptr_to_free (strptr); \
695 else if (flags & POSIX_MALLOC) \
702 Str = ARG (Type *); \
706 #ifdef COMPILE_WSCANF
707 STRING_ARG (str, char, 100);
709 STRING_ARG (str, char, (width > 1024 ? 1024 : width));
713 if (__glibc_unlikely (c == EOF))
716 #ifdef COMPILE_WSCANF
717 /* We have to convert the wide character(s) into multibyte
718 characters and store the result. */
719 memset (&state, '\0', sizeof (state));
725 if (!(flags & SUPPRESS) && (flags & POSIX_MALLOC)
726 && str + MB_CUR_MAX >= *strptr + strsize)
728 /* We have to enlarge the buffer if the `m' flag
730 size_t strleng = str - *strptr;
733 newstr = (char *) realloc (*strptr, strsize * 2);
736 /* Can't allocate that much. Last-ditch effort. */
737 newstr = (char *) realloc (*strptr,
738 strleng + MB_CUR_MAX);
741 /* c can't have `a' flag, only `m'. */
748 str = newstr + strleng;
749 strsize = strleng + MB_CUR_MAX;
755 str = newstr + strleng;
760 n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c, &state);
761 if (__glibc_unlikely (n == (size_t) -1))
762 /* No valid wide character. */
765 /* Increment the output pointer. Even if we don't
769 while (--width > 0 && inchar () != EOF);
771 if (!(flags & SUPPRESS))
776 && (char *) str == *strptr + strsize)
778 /* Enlarge the buffer. */
781 + (strsize >= width ? width - 1 : strsize);
783 str = (char *) realloc (*strptr, newsize);
786 /* Can't allocate that much. Last-ditch
788 str = (char *) realloc (*strptr, strsize + 1);
791 /* c can't have `a' flag, only `m'. */
797 *strptr = (char *) str;
804 *strptr = (char *) str;
811 while (--width > 0 && inchar () != EOF);
814 while (--width > 0 && inchar () != EOF);
817 if (!(flags & SUPPRESS))
819 if ((flags & MALLOC) && str - *strptr != strsize)
821 char *cp = (char *) realloc (*strptr, str - *strptr);
836 STRING_ARG (wstr, wchar_t, (width > 1024 ? 1024 : width));
839 if (__glibc_unlikely (c == EOF))
842 #ifdef COMPILE_WSCANF
843 /* Just store the incoming wide characters. */
844 if (!(flags & SUPPRESS))
849 && wstr == (wchar_t *) *strptr + strsize)
852 = strsize + (strsize > width ? width - 1 : strsize);
853 /* Enlarge the buffer. */
854 wstr = (wchar_t *) realloc (*strptr,
855 newsize * sizeof (wchar_t));
858 /* Can't allocate that much. Last-ditch effort. */
859 wstr = (wchar_t *) realloc (*strptr,
864 /* C or lc can't have `a' flag, only `m'
871 *strptr = (char *) wstr;
878 *strptr = (char *) wstr;
885 while (--width > 0 && inchar () != EOF);
888 while (--width > 0 && inchar () != EOF);
891 /* We have to convert the multibyte input sequence to wide
896 memset (&cstate, '\0', sizeof (cstate));
900 /* This is what we present the mbrtowc function first. */
903 if (!(flags & SUPPRESS) && (flags & MALLOC)
904 && wstr == (wchar_t *) *strptr + strsize)
907 = strsize + (strsize > width ? width - 1 : strsize);
908 /* Enlarge the buffer. */
909 wstr = (wchar_t *) realloc (*strptr,
910 newsize * sizeof (wchar_t));
913 /* Can't allocate that much. Last-ditch effort. */
914 wstr = (wchar_t *) realloc (*strptr,
916 * sizeof (wchar_t)));
919 /* C or lc can't have `a' flag, only `m' flag. */
925 *strptr = (char *) wstr;
932 *strptr = (char *) wstr;
942 n = __mbrtowc (!(flags & SUPPRESS) ? wstr : NULL,
945 if (n == (size_t) -2)
947 /* Possibly correct character, just not enough
949 if (__glibc_unlikely (inchar () == EOF))
956 if (__glibc_unlikely (n != 1))
959 /* We have a match. */
963 /* Advance the result pointer. */
966 while (--width > 0 && inchar () != EOF);
970 if (!(flags & SUPPRESS))
972 if ((flags & MALLOC) && wstr - (wchar_t *) *strptr != strsize)
974 wchar_t *cp = (wchar_t *) realloc (*strptr,
976 - (wchar_t *) *strptr)
977 * sizeof (wchar_t)));
979 *strptr = (char *) cp;
988 case L_('s'): /* Read a string. */
991 STRING_ARG (str, char, 100);
994 if (__glibc_unlikely (c == EOF))
997 #ifdef COMPILE_WSCANF
998 memset (&state, '\0', sizeof (state));
1005 ungetc_not_eof (c, s);
1009 #ifdef COMPILE_WSCANF
1010 /* This is quite complicated. We have to convert the
1011 wide characters into multibyte characters and then
1016 if (!(flags & SUPPRESS) && (flags & MALLOC)
1017 && str + MB_CUR_MAX >= *strptr + strsize)
1019 /* We have to enlarge the buffer if the `a' or `m'
1021 size_t strleng = str - *strptr;
1024 newstr = (char *) realloc (*strptr, strsize * 2);
1027 /* Can't allocate that much. Last-ditch
1029 newstr = (char *) realloc (*strptr,
1030 strleng + MB_CUR_MAX);
1033 if (flags & POSIX_MALLOC)
1038 /* We lose. Oh well. Terminate the
1039 string and stop converting,
1040 so at least we don't skip any input. */
1041 ((char *) (*strptr))[strleng] = '\0';
1049 str = newstr + strleng;
1050 strsize = strleng + MB_CUR_MAX;
1056 str = newstr + strleng;
1061 n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c,
1063 if (__glibc_unlikely (n == (size_t) -1))
1066 assert (n <= MB_CUR_MAX);
1071 if (!(flags & SUPPRESS))
1074 if ((flags & MALLOC)
1075 && (char *) str == *strptr + strsize)
1077 /* Enlarge the buffer. */
1078 str = (char *) realloc (*strptr, 2 * strsize);
1081 /* Can't allocate that much. Last-ditch
1083 str = (char *) realloc (*strptr, strsize + 1);
1086 if (flags & POSIX_MALLOC)
1091 /* We lose. Oh well. Terminate the
1092 string and stop converting,
1093 so at least we don't skip any input. */
1094 ((char *) (*strptr))[strsize - 1] = '\0';
1101 *strptr = (char *) str;
1108 *strptr = (char *) str;
1116 while ((width <= 0 || --width > 0) && inchar () != EOF);
1118 if (!(flags & SUPPRESS))
1120 #ifdef COMPILE_WSCANF
1121 /* We have to emit the code to get into the initial
1123 char buf[MB_LEN_MAX];
1124 size_t n = __wcrtomb (buf, L'\0', &state);
1125 if (n > 0 && (flags & MALLOC)
1126 && str + n >= *strptr + strsize)
1128 /* Enlarge the buffer. */
1129 size_t strleng = str - *strptr;
1132 newstr = (char *) realloc (*strptr, strleng + n + 1);
1135 if (flags & POSIX_MALLOC)
1140 /* We lose. Oh well. Terminate the string
1141 and stop converting, so at least we don't
1143 ((char *) (*strptr))[strleng] = '\0';
1151 str = newstr + strleng;
1152 strsize = strleng + n + 1;
1156 str = __mempcpy (str, buf, n);
1160 if ((flags & MALLOC) && str - *strptr != strsize)
1162 char *cp = (char *) realloc (*strptr, str - *strptr);
1176 #ifndef COMPILE_WSCANF
1180 /* Wide character string. */
1181 STRING_ARG (wstr, wchar_t, 100);
1184 if (__builtin_expect (c == EOF, 0))
1187 #ifndef COMPILE_WSCANF
1188 memset (&cstate, '\0', sizeof (cstate));
1195 ungetc_not_eof (c, s);
1199 #ifdef COMPILE_WSCANF
1201 if (!(flags & SUPPRESS))
1204 if ((flags & MALLOC)
1205 && wstr == (wchar_t *) *strptr + strsize)
1207 /* Enlarge the buffer. */
1208 wstr = (wchar_t *) realloc (*strptr,
1210 * sizeof (wchar_t));
1213 /* Can't allocate that much. Last-ditch
1215 wstr = (wchar_t *) realloc (*strptr,
1217 * sizeof (wchar_t));
1220 if (flags & POSIX_MALLOC)
1225 /* We lose. Oh well. Terminate the string
1226 and stop converting, so at least we don't
1228 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
1235 *strptr = (char *) wstr;
1242 *strptr = (char *) wstr;
1258 n = __mbrtowc (!(flags & SUPPRESS) ? wstr : NULL,
1261 if (n == (size_t) -2)
1263 /* Possibly correct character, just not enough
1265 if (__glibc_unlikely (inchar () == EOF))
1272 if (__glibc_unlikely (n != 1))
1275 /* We have a match. */
1280 if (!(flags & SUPPRESS) && (flags & MALLOC)
1281 && wstr == (wchar_t *) *strptr + strsize)
1283 /* Enlarge the buffer. */
1284 wstr = (wchar_t *) realloc (*strptr,
1286 * sizeof (wchar_t)));
1289 /* Can't allocate that much. Last-ditch effort. */
1290 wstr = (wchar_t *) realloc (*strptr,
1292 * sizeof (wchar_t)));
1295 if (flags & POSIX_MALLOC)
1300 /* We lose. Oh well. Terminate the
1301 string and stop converting, so at
1302 least we don't skip any input. */
1303 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
1310 *strptr = (char *) wstr;
1317 *strptr = (char *) wstr;
1325 while ((width <= 0 || --width > 0) && inchar () != EOF);
1327 if (!(flags & SUPPRESS))
1331 if ((flags & MALLOC) && wstr - (wchar_t *) *strptr != strsize)
1333 wchar_t *cp = (wchar_t *) realloc (*strptr,
1335 - (wchar_t *) *strptr)
1336 * sizeof(wchar_t)));
1338 *strptr = (char *) cp;
1347 case L_('x'): /* Hexadecimal integer. */
1348 case L_('X'): /* Ditto. */
1352 case L_('o'): /* Octal integer. */
1356 case L_('u'): /* Unsigned decimal integer. */
1360 case L_('d'): /* Signed decimal integer. */
1362 flags |= NUMBER_SIGNED;
1365 case L_('i'): /* Generic number. */
1367 flags |= NUMBER_SIGNED;
1371 if (__glibc_unlikely (c == EOF))
1374 /* Check for a sign. */
1375 if (c == L_('-') || c == L_('+'))
1383 /* Look for a leading indication of base. */
1384 if (width != 0 && c == L_('0'))
1392 if (width != 0 && TOLOWER (c) == L_('x'))
1410 if (base == 10 && __builtin_expect ((flags & I18N) != 0, 0))
1415 #ifdef COMPILE_WSCANF
1416 const wchar_t *wcdigits[10];
1417 const wchar_t *wcdigits_extended[10];
1419 const char *mbdigits[10];
1420 const char *mbdigits_extended[10];
1422 /* "to_inpunct" is a map from ASCII digits to their
1423 equivalent in locale. This is defined for locales
1424 which use an extra digits set. */
1425 wctrans_t map = __wctrans ("to_inpunct");
1429 #ifdef COMPILE_WSCANF
1430 to_level = _NL_CURRENT_WORD (LC_CTYPE,
1431 _NL_CTYPE_INDIGITS_WC_LEN) - 1;
1433 to_level = (uint32_t) curctype->values[_NL_ITEM_INDEX (_NL_CTYPE_INDIGITS_MB_LEN)].word - 1;
1436 /* Get the alternative digit forms if there are any. */
1437 if (__glibc_unlikely (map != NULL))
1439 /* Adding new level for extra digits set in locale file. */
1442 for (n = 0; n < 10; ++n)
1444 #ifdef COMPILE_WSCANF
1445 wcdigits[n] = (const wchar_t *)
1446 _NL_CURRENT (LC_CTYPE, _NL_CTYPE_INDIGITS0_WC + n);
1448 wchar_t *wc_extended = (wchar_t *)
1449 alloca ((to_level + 2) * sizeof (wchar_t));
1450 __wmemcpy (wc_extended, wcdigits[n], to_level);
1451 wc_extended[to_level] = __towctrans (L'0' + n, map);
1452 wc_extended[to_level + 1] = '\0';
1453 wcdigits_extended[n] = wc_extended;
1456 = curctype->values[_NL_CTYPE_INDIGITS0_MB + n].string;
1458 /* Get the equivalent wide char in map. */
1459 wint_t extra_wcdigit = __towctrans (L'0' + n, map);
1461 /* Convert it to multibyte representation. */
1463 memset (&state, '\0', sizeof (state));
1465 char extra_mbdigit[MB_LEN_MAX];
1467 = __wcrtomb (extra_mbdigit, extra_wcdigit, &state);
1469 if (mblen == (size_t) -1)
1471 /* Ignore this new level. */
1476 /* Calculate the length of mbdigits[n]. */
1477 const char *last_char = mbdigits[n];
1478 for (level = 0; level < to_level; ++level)
1479 last_char = strchr (last_char, '\0') + 1;
1481 size_t mbdigits_len = last_char - mbdigits[n];
1483 /* Allocate memory for extended multibyte digit. */
1485 mb_extended = (char *) alloca (mbdigits_len + mblen + 1);
1487 /* And get the mbdigits + extra_digit string. */
1488 *(char *) __mempcpy (__mempcpy (mb_extended, mbdigits[n],
1490 extra_mbdigit, mblen) = '\0';
1491 mbdigits_extended[n] = mb_extended;
1496 /* Read the number into workspace. */
1497 while (c != EOF && width != 0)
1499 /* In this round we get the pointer to the digit strings
1500 and also perform the first round of comparisons. */
1501 for (n = 0; n < 10; ++n)
1503 /* Get the string for the digits with value N. */
1504 #ifdef COMPILE_WSCANF
1505 if (__glibc_unlikely (map != NULL))
1506 wcdigits[n] = wcdigits_extended[n];
1508 wcdigits[n] = (const wchar_t *)
1509 _NL_CURRENT (LC_CTYPE, _NL_CTYPE_INDIGITS0_WC + n);
1510 wcdigits[n] += from_level;
1512 if (c == (wint_t) *wcdigits[n])
1514 to_level = from_level;
1518 /* Advance the pointer to the next string. */
1522 int avail = width > 0 ? width : INT_MAX;
1524 if (__glibc_unlikely (map != NULL))
1525 mbdigits[n] = mbdigits_extended[n];
1528 = curctype->values[_NL_CTYPE_INDIGITS0_MB + n].string;
1530 for (level = 0; level < from_level; level++)
1531 mbdigits[n] = strchr (mbdigits[n], '\0') + 1;
1534 while ((unsigned char) *cmpp == c && avail >= 0)
1536 if (*++cmpp == '\0')
1540 if (avail == 0 || inchar () == EOF)
1550 to_level = from_level;
1554 /* We are pushing all read characters back. */
1555 if (cmpp > mbdigits[n])
1558 while (--cmpp > mbdigits[n])
1559 ungetc_not_eof ((unsigned char) *cmpp, s);
1560 c = (unsigned char) *cmpp;
1563 /* Advance the pointer to the next string. */
1564 mbdigits[n] = strchr (mbdigits[n], '\0') + 1;
1570 /* Have not yet found the digit. */
1571 for (level = from_level + 1; level <= to_level; ++level)
1573 /* Search all ten digits of this level. */
1574 for (n = 0; n < 10; ++n)
1576 #ifdef COMPILE_WSCANF
1577 if (c == (wint_t) *wcdigits[n])
1580 /* Advance the pointer to the next string. */
1584 int avail = width > 0 ? width : INT_MAX;
1587 while ((unsigned char) *cmpp == c && avail >= 0)
1589 if (*++cmpp == '\0')
1593 if (avail == 0 || inchar () == EOF)
1606 /* We are pushing all read characters back. */
1607 if (cmpp > mbdigits[n])
1610 while (--cmpp > mbdigits[n])
1611 ungetc_not_eof ((unsigned char) *cmpp, s);
1612 c = (unsigned char) *cmpp;
1615 /* Advance the pointer to the next string. */
1616 mbdigits[n] = strchr (mbdigits[n], '\0') + 1;
1632 else if (flags & GROUP)
1634 /* Try matching against the thousands separator. */
1635 #ifdef COMPILE_WSCANF
1639 const char *cmpp = thousands;
1640 int avail = width > 0 ? width : INT_MAX;
1642 while ((unsigned char) *cmpp == c && avail >= 0)
1645 if (*++cmpp == '\0')
1649 if (avail == 0 || inchar () == EOF)
1657 /* We are pushing all read characters back. */
1658 if (cmpp > thousands)
1660 wpsize -= cmpp - thousands;
1662 while (--cmpp > thousands)
1663 ungetc_not_eof ((unsigned char) *cmpp, s);
1664 c = (unsigned char) *cmpp;
1672 /* The last thousands character will be added back by
1688 /* Read the number into workspace. */
1689 while (c != EOF && width != 0)
1696 else if (!ISDIGIT (c) || (int) (c - L_('0')) >= base)
1698 if (base == 10 && (flags & GROUP))
1700 /* Try matching against the thousands separator. */
1701 #ifdef COMPILE_WSCANF
1705 const char *cmpp = thousands;
1706 int avail = width > 0 ? width : INT_MAX;
1708 while ((unsigned char) *cmpp == c && avail >= 0)
1711 if (*++cmpp == '\0')
1715 if (avail == 0 || inchar () == EOF)
1723 /* We are pushing all read characters back. */
1724 if (cmpp > thousands)
1726 wpsize -= cmpp - thousands;
1728 while (--cmpp > thousands)
1729 ungetc_not_eof ((unsigned char) *cmpp, s);
1730 c = (unsigned char) *cmpp;
1738 /* The last thousands character will be added back by
1754 || (wpsize == 1 && (wp[0] == L_('+') || wp[0] == L_('-'))))
1756 /* There was no number. If we are supposed to read a pointer
1757 we must recognize "(nil)" as well. */
1758 if (__builtin_expect (wpsize == 0
1759 && (flags & READ_POINTER)
1760 && (width < 0 || width >= 5)
1762 && TOLOWER (inchar ()) == L_('n')
1763 && TOLOWER (inchar ()) == L_('i')
1764 && TOLOWER (inchar ()) == L_('l')
1765 && inchar () == L_(')'), 1))
1766 /* We must produce the value of a NULL pointer. A single
1767 '0' digit is enough. */
1771 /* The last read character is not part of the number
1779 /* The just read character is not part of the number anymore. */
1782 /* Convert the number. */
1784 if (need_longlong && (flags & LONGDBL))
1786 if (flags & NUMBER_SIGNED)
1787 num.q = __strtoll_internal (wp, &tw, base, flags & GROUP);
1789 num.uq = __strtoull_internal (wp, &tw, base, flags & GROUP);
1793 if (flags & NUMBER_SIGNED)
1794 num.l = __strtol_internal (wp, &tw, base, flags & GROUP);
1796 num.ul = __strtoul_internal (wp, &tw, base, flags & GROUP);
1798 if (__glibc_unlikely (wp == tw))
1801 if (!(flags & SUPPRESS))
1803 if (flags & NUMBER_SIGNED)
1805 if (need_longlong && (flags & LONGDBL))
1806 *ARG (LONGLONG int *) = num.q;
1807 else if (need_long && (flags & LONG))
1808 *ARG (long int *) = num.l;
1809 else if (flags & SHORT)
1810 *ARG (short int *) = (short int) num.l;
1811 else if (!(flags & CHAR))
1812 *ARG (int *) = (int) num.l;
1814 *ARG (signed char *) = (signed char) num.ul;
1818 if (need_longlong && (flags & LONGDBL))
1819 *ARG (unsigned LONGLONG int *) = num.uq;
1820 else if (need_long && (flags & LONG))
1821 *ARG (unsigned long int *) = num.ul;
1822 else if (flags & SHORT)
1823 *ARG (unsigned short int *)
1824 = (unsigned short int) num.ul;
1825 else if (!(flags & CHAR))
1826 *ARG (unsigned int *) = (unsigned int) num.ul;
1828 *ARG (unsigned char *) = (unsigned char) num.ul;
1834 case L_('e'): /* Floating-point numbers. */
1845 if (__glibc_unlikely (c == EOF))
1848 got_digit = got_dot = got_e = 0;
1850 /* Check for a sign. */
1851 if (c == L_('-') || c == L_('+'))
1853 negative = c == L_('-');
1854 if (__glibc_unlikely (width == 0 || inchar () == EOF))
1855 /* EOF is only an input error before we read any chars. */
1863 /* Take care for the special arguments "nan" and "inf". */
1864 if (TOLOWER (c) == L_('n'))
1868 if (__builtin_expect (width == 0
1870 || TOLOWER (c) != L_('a'), 0))
1875 if (__builtin_expect (width == 0
1877 || TOLOWER (c) != L_('n'), 0))
1885 else if (TOLOWER (c) == L_('i'))
1887 /* Maybe "inf" or "infinity". */
1889 if (__builtin_expect (width == 0
1891 || TOLOWER (c) != L_('n'), 0))
1896 if (__builtin_expect (width == 0
1898 || TOLOWER (c) != L_('f'), 0))
1903 /* It is as least "inf". */
1904 if (width != 0 && inchar () != EOF)
1906 if (TOLOWER (c) == L_('i'))
1910 /* Now we have to read the rest as well. */
1912 if (__builtin_expect (width == 0
1914 || TOLOWER (c) != L_('n'), 0))
1919 if (__builtin_expect (width == 0
1921 || TOLOWER (c) != L_('i'), 0))
1926 if (__builtin_expect (width == 0
1928 || TOLOWER (c) != L_('t'), 0))
1933 if (__builtin_expect (width == 0
1935 || TOLOWER (c) != L_('y'), 0))
1949 if (width != 0 && c == L_('0'))
1955 if (width != 0 && TOLOWER (c) == L_('x'))
1957 /* It is a number in hexadecimal format. */
1960 flags |= HEXA_FLOAT;
1963 /* Grouping is not allowed. */
1980 else if (!got_e && (flags & HEXA_FLOAT) && ISXDIGIT (c))
1985 else if (got_e && wp[wpsize - 1] == exp_char
1986 && (c == L_('-') || c == L_('+')))
1988 else if (got_digit && !got_e
1989 && (CHAR_T) TOLOWER (c) == exp_char)
1992 got_e = got_dot = 1;
1996 #ifdef COMPILE_WSCANF
1997 if (! got_dot && c == decimal)
2002 else if ((flags & GROUP) != 0 && ! got_dot && c == thousands)
2006 /* The last read character is not part of the number
2012 const char *cmpp = decimal;
2013 int avail = width > 0 ? width : INT_MAX;
2017 while ((unsigned char) *cmpp == c && avail >= 0)
2018 if (*++cmpp == '\0')
2022 if (avail == 0 || inchar () == EOF)
2030 /* Add all the characters. */
2031 for (cmpp = decimal; *cmpp != '\0'; ++cmpp)
2032 ADDW ((unsigned char) *cmpp);
2039 /* Figure out whether it is a thousands separator.
2040 There is one problem: we possibly read more than
2041 one character. We cannot push them back but since
2042 we know that parts of the `decimal' string matched,
2043 we can compare against it. */
2044 const char *cmp2p = thousands;
2046 if ((flags & GROUP) != 0 && ! got_dot)
2048 while (cmp2p - thousands < cmpp - decimal
2049 && *cmp2p == decimal[cmp2p - thousands])
2051 if (cmp2p - thousands == cmpp - decimal)
2053 while ((unsigned char) *cmp2p == c && avail >= 0)
2054 if (*++cmp2p == '\0')
2058 if (avail == 0 || inchar () == EOF)
2065 if (cmp2p != NULL && *cmp2p == '\0')
2067 /* Add all the characters. */
2068 for (cmpp = thousands; *cmpp != '\0'; ++cmpp)
2069 ADDW ((unsigned char) *cmpp);
2075 /* The last read character is not part of the number
2084 if (width == 0 || inchar () == EOF)
2092 if (__builtin_expect ((flags & I18N) != 0, 0)
2093 /* Hexadecimal floats make no sense, fixing localized
2094 digits with ASCII letters. */
2095 && !(flags & HEXA_FLOAT)
2096 /* Minimum requirement. */
2097 && (wpsize == 0 || got_dot)
2098 && (map = __wctrans ("to_inpunct")) != NULL)
2100 /* Reget the first character. */
2103 /* Localized digits, decimal points, and thousands
2105 wint_t wcdigits[12];
2107 /* First get decimal equivalent to check if we read it
2109 wcdigits[11] = __towctrans (L'.', map);
2111 /* If we have not read any character or have just read
2112 locale decimal point which matches the decimal point
2113 for localized FP numbers, then we may have localized
2114 digits. Note, we test GOT_DOT above. */
2115 #ifdef COMPILE_WSCANF
2116 if (wpsize == 0 || (wpsize == 1 && wcdigits[11] == decimal))
2118 char mbdigits[12][MB_LEN_MAX + 1];
2121 memset (&state, '\0', sizeof (state));
2123 bool match_so_far = wpsize == 0;
2124 size_t mblen = __wcrtomb (mbdigits[11], wcdigits[11], &state);
2125 if (mblen != (size_t) -1)
2127 mbdigits[11][mblen] = '\0';
2128 match_so_far |= (wpsize == strlen (decimal)
2129 && strcmp (decimal, mbdigits[11]) == 0);
2133 size_t decimal_len = strlen (decimal);
2134 /* This should always be the case but the data comes
2136 if (decimal_len <= MB_LEN_MAX)
2138 match_so_far |= wpsize == decimal_len;
2139 memcpy (mbdigits[11], decimal, decimal_len + 1);
2142 match_so_far = false;
2148 bool have_locthousands = (flags & GROUP) != 0;
2150 /* Now get the digits and the thousands-sep equivalents. */
2151 for (int n = 0; n < 11; ++n)
2154 wcdigits[n] = __towctrans (L'0' + n, map);
2157 wcdigits[10] = __towctrans (L',', map);
2158 have_locthousands &= wcdigits[10] != L'\0';
2161 #ifndef COMPILE_WSCANF
2162 memset (&state, '\0', sizeof (state));
2164 size_t mblen = __wcrtomb (mbdigits[n], wcdigits[n],
2166 if (mblen == (size_t) -1)
2170 if (have_locthousands)
2172 size_t thousands_len = strlen (thousands);
2173 if (thousands_len <= MB_LEN_MAX)
2174 memcpy (mbdigits[10], thousands,
2177 have_locthousands = false;
2181 /* Ignore checking against localized digits. */
2185 mbdigits[n][mblen] = '\0';
2189 /* Start checking against localized digits, if
2190 conversion is done correctly. */
2193 if (got_e && wp[wpsize - 1] == exp_char
2194 && (c == L_('-') || c == L_('+')))
2196 else if (wpsize > 0 && !got_e
2197 && (CHAR_T) TOLOWER (c) == exp_char)
2200 got_e = got_dot = 1;
2204 /* Check against localized digits, decimal point,
2205 and thousands separator. */
2207 for (n = 0; n < 12; ++n)
2209 #ifdef COMPILE_WSCANF
2210 if (c == wcdigits[n])
2214 else if (n == 11 && !got_dot)
2219 else if (n == 10 && have_locthousands
2223 /* The last read character is not part
2224 of the number anymore. */
2230 const char *cmpp = mbdigits[n];
2231 int avail = width > 0 ? width : INT_MAX;
2233 while ((unsigned char) *cmpp == c && avail >= 0)
2234 if (*++cmpp == '\0')
2238 if (avail == 0 || inchar () == EOF)
2249 else if (n == 11 && !got_dot)
2251 /* Add all the characters. */
2252 for (cmpp = decimal; *cmpp != '\0';
2254 ADDW ((unsigned char) *cmpp);
2258 else if (n == 10 && (flags & GROUP) != 0
2261 /* Add all the characters. */
2262 for (cmpp = thousands; *cmpp != '\0';
2264 ADDW ((unsigned char) *cmpp);
2267 /* The last read character is not part
2268 of the number anymore. */
2274 /* We are pushing all read characters back. */
2275 if (cmpp > mbdigits[n])
2278 while (--cmpp > mbdigits[n])
2279 ungetc_not_eof ((unsigned char) *cmpp, s);
2280 c = (unsigned char) *cmpp;
2287 /* The last read character is not part
2288 of the number anymore. */
2294 if (width == 0 || inchar () == EOF)
2302 #ifndef COMPILE_WSCANF
2308 /* Have we read any character? If we try to read a number
2309 in hexadecimal notation and we have read only the `0x'
2310 prefix this is an error. */
2311 if (__builtin_expect (wpsize == 0
2312 || ((flags & HEXA_FLOAT) && wpsize == 2), 0))
2316 /* Convert the number. */
2318 if ((flags & LONGDBL) && !__ldbl_is_dbl)
2320 long double d = __strtold_internal (wp, &tw, flags & GROUP);
2321 if (!(flags & SUPPRESS) && tw != wp)
2322 *ARG (long double *) = negative ? -d : d;
2324 else if (flags & (LONG | LONGDBL))
2326 double d = __strtod_internal (wp, &tw, flags & GROUP);
2327 if (!(flags & SUPPRESS) && tw != wp)
2328 *ARG (double *) = negative ? -d : d;
2332 float d = __strtof_internal (wp, &tw, flags & GROUP);
2333 if (!(flags & SUPPRESS) && tw != wp)
2334 *ARG (float *) = negative ? -d : d;
2337 if (__glibc_unlikely (tw == wp))
2340 if (!(flags & SUPPRESS))
2344 case L_('['): /* Character class. */
2346 STRING_ARG (wstr, wchar_t, 100);
2348 STRING_ARG (str, char, 100);
2359 /* There is no width given so there is also no limit on the
2360 number of characters we read. Therefore we set width to
2361 a very high value to make the algorithm easier. */
2364 #ifdef COMPILE_WSCANF
2365 /* Find the beginning and the end of the scanlist. We are not
2366 creating a lookup table since it would have to be too large.
2367 Instead we search each time through the string. This is not
2368 a constant lookup time but who uses this feature deserves to
2370 tw = (wchar_t *) f; /* Marks the beginning. */
2375 while ((fc = *f++) != L'\0' && fc != L']');
2377 if (__glibc_unlikely (fc == L'\0'))
2379 wchar_t *twend = (wchar_t *) f - 1;
2381 /* Fill WP with byte flags indexed by character.
2382 We will use this flag map for matching input characters. */
2383 if (wpmax < UCHAR_MAX + 1)
2385 wpmax = UCHAR_MAX + 1;
2386 wp = (char *) alloca (wpmax);
2388 memset (wp, '\0', UCHAR_MAX + 1);
2391 if (fc == ']' || fc == '-')
2393 /* If ] or - appears before any char in the set, it is not
2394 the terminator or separator, but the first char in the
2400 while ((fc = *f++) != '\0' && fc != ']')
2401 if (fc == '-' && *f != '\0' && *f != ']'
2402 && (unsigned char) f[-2] <= (unsigned char) *f)
2404 /* Add all characters from the one before the '-'
2405 up to (but not including) the next format char. */
2406 for (fc = (unsigned char) f[-2]; fc < (unsigned char) *f; ++fc)
2410 /* Add the character to the flag map. */
2413 if (__glibc_unlikely (fc == '\0'))
2419 size_t now = read_in;
2420 #ifdef COMPILE_WSCANF
2421 if (__glibc_unlikely (inchar () == WEOF))
2428 /* Test whether it's in the scanlist. */
2430 while (runp < twend)
2432 if (runp[0] == L'-' && runp[1] != '\0'
2433 && runp + 1 != twend
2435 && (unsigned int) runp[-1] <= (unsigned int) runp[1])
2437 /* Match against all characters in between the
2438 first and last character of the sequence. */
2441 for (wc = runp[-1] + 1; wc <= runp[1]; ++wc)
2442 if ((wint_t) wc == c)
2445 if (wc <= runp[1] && !not_in)
2447 if (wc <= runp[1] && not_in)
2449 /* The current character is not in the
2459 if ((wint_t) *runp == c && !not_in)
2461 if ((wint_t) *runp == c && not_in)
2471 if (runp == twend && !not_in)
2477 if (!(flags & SUPPRESS))
2481 if ((flags & MALLOC)
2482 && wstr == (wchar_t *) *strptr + strsize)
2484 /* Enlarge the buffer. */
2485 wstr = (wchar_t *) realloc (*strptr,
2487 * sizeof (wchar_t));
2490 /* Can't allocate that much. Last-ditch
2493 realloc (*strptr, (strsize + 1)
2494 * sizeof (wchar_t));
2497 if (flags & POSIX_MALLOC)
2502 /* We lose. Oh well. Terminate the string
2503 and stop converting, so at least we don't
2505 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
2512 *strptr = (char *) wstr;
2519 *strptr = (char *) wstr;
2526 while (--width > 0 && inchar () != WEOF);
2529 char buf[MB_LEN_MAX];
2533 if (__glibc_unlikely (inchar () == EOF))
2536 memset (&cstate, '\0', sizeof (cstate));
2540 if (wp[c] == not_in)
2542 ungetc_not_eof (c, s);
2547 if (!(flags & SUPPRESS))
2551 /* Convert it into a wide character. */
2553 n = __mbrtowc (wstr, buf, 1, &cstate);
2555 if (n == (size_t) -2)
2557 /* Possibly correct character, just not enough
2560 assert (cnt < MB_CUR_MAX);
2566 if ((flags & MALLOC)
2567 && wstr == (wchar_t *) *strptr + strsize)
2569 /* Enlarge the buffer. */
2570 wstr = (wchar_t *) realloc (*strptr,
2572 * sizeof (wchar_t)));
2575 /* Can't allocate that much. Last-ditch
2578 realloc (*strptr, ((strsize + 1)
2579 * sizeof (wchar_t)));
2582 if (flags & POSIX_MALLOC)
2587 /* We lose. Oh well. Terminate the
2588 string and stop converting,
2589 so at least we don't skip any input. */
2590 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
2597 *strptr = (char *) wstr;
2604 *strptr = (char *) wstr;
2614 while (inchar () != EOF);
2616 if (__glibc_unlikely (cnt != 0))
2617 /* We stopped in the middle of recognizing another
2618 character. That's a problem. */
2622 if (__glibc_unlikely (now == read_in))
2623 /* We haven't succesfully read any character. */
2626 if (!(flags & SUPPRESS))
2630 if ((flags & MALLOC)
2631 && wstr - (wchar_t *) *strptr != strsize)
2633 wchar_t *cp = (wchar_t *)
2634 realloc (*strptr, ((wstr - (wchar_t *) *strptr)
2635 * sizeof(wchar_t)));
2637 *strptr = (char *) cp;
2646 size_t now = read_in;
2648 if (__glibc_unlikely (inchar () == EOF))
2651 #ifdef COMPILE_WSCANF
2653 memset (&state, '\0', sizeof (state));
2660 /* Test whether it's in the scanlist. */
2662 while (runp < twend)
2664 if (runp[0] == L'-' && runp[1] != '\0'
2665 && runp + 1 != twend
2667 && (unsigned int) runp[-1] <= (unsigned int) runp[1])
2669 /* Match against all characters in between the
2670 first and last character of the sequence. */
2673 for (wc = runp[-1] + 1; wc <= runp[1]; ++wc)
2674 if ((wint_t) wc == c)
2677 if (wc <= runp[1] && !not_in)
2679 if (wc <= runp[1] && not_in)
2681 /* The current character is not in the
2691 if ((wint_t) *runp == c && !not_in)
2693 if ((wint_t) *runp == c && not_in)
2703 if (runp == twend && !not_in)
2709 if (!(flags & SUPPRESS))
2711 if ((flags & MALLOC)
2712 && str + MB_CUR_MAX >= *strptr + strsize)
2714 /* Enlarge the buffer. */
2715 size_t strleng = str - *strptr;
2718 newstr = (char *) realloc (*strptr, 2 * strsize);
2721 /* Can't allocate that much. Last-ditch
2723 newstr = (char *) realloc (*strptr,
2724 strleng + MB_CUR_MAX);
2727 if (flags & POSIX_MALLOC)
2732 /* We lose. Oh well. Terminate the string
2733 and stop converting, so at least we don't
2735 ((char *) (*strptr))[strleng] = '\0';
2743 str = newstr + strleng;
2744 strsize = strleng + MB_CUR_MAX;
2750 str = newstr + strleng;
2756 n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c, &state);
2757 if (__glibc_unlikely (n == (size_t) -1))
2760 assert (n <= MB_CUR_MAX);
2763 while (--width > 0 && inchar () != WEOF);
2768 if (wp[c] == not_in)
2770 ungetc_not_eof (c, s);
2775 if (!(flags & SUPPRESS))
2778 if ((flags & MALLOC)
2779 && (char *) str == *strptr + strsize)
2781 /* Enlarge the buffer. */
2782 size_t newsize = 2 * strsize;
2785 str = (char *) realloc (*strptr, newsize);
2788 /* Can't allocate that much. Last-ditch
2790 if (newsize > strsize + 1)
2792 newsize = strsize + 1;
2795 if (flags & POSIX_MALLOC)
2800 /* We lose. Oh well. Terminate the
2801 string and stop converting,
2802 so at least we don't skip any input. */
2803 ((char *) (*strptr))[strsize - 1] = '\0';
2810 *strptr = (char *) str;
2817 while (--width > 0 && inchar () != EOF);
2820 if (__glibc_unlikely (now == read_in))
2821 /* We haven't succesfully read any character. */
2824 if (!(flags & SUPPRESS))
2826 #ifdef COMPILE_WSCANF
2827 /* We have to emit the code to get into the initial
2829 char buf[MB_LEN_MAX];
2830 size_t n = __wcrtomb (buf, L'\0', &state);
2831 if (n > 0 && (flags & MALLOC)
2832 && str + n >= *strptr + strsize)
2834 /* Enlarge the buffer. */
2835 size_t strleng = str - *strptr;
2838 newstr = (char *) realloc (*strptr, strleng + n + 1);
2841 if (flags & POSIX_MALLOC)
2846 /* We lose. Oh well. Terminate the string
2847 and stop converting, so at least we don't
2849 ((char *) (*strptr))[strleng] = '\0';
2857 str = newstr + strleng;
2858 strsize = strleng + n + 1;
2862 str = __mempcpy (str, buf, n);
2866 if ((flags & MALLOC) && str - *strptr != strsize)
2868 char *cp = (char *) realloc (*strptr, str - *strptr);
2879 case L_('p'): /* Generic pointer. */
2881 /* A PTR must be the same size as a `long int'. */
2882 flags &= ~(SHORT|LONGDBL);
2885 flags |= READ_POINTER;
2889 /* If this is an unknown format character punt. */
2894 /* The last thing we saw int the format string was a white space.
2895 Consume the last white spaces. */
2900 while (ISSPACE (c));
2905 /* Unlock stream. */
2914 if (__glibc_unlikely (done == EOF))
2916 if (__glibc_unlikely (ptrs_to_free != NULL))
2918 struct ptrs_to_free *p = ptrs_to_free;
2921 for (size_t cnt = 0; cnt < p->count; ++cnt)
2923 free (*p->ptrs[cnt]);
2924 *p->ptrs[cnt] = NULL;
2931 else if (__glibc_unlikely (strptr != NULL))
2939 #ifdef COMPILE_WSCANF
2941 __vfwscanf (FILE *s, const wchar_t *format, va_list argptr)
2943 return _IO_vfwscanf (s, format, argptr, NULL);
2945 ldbl_weak_alias (__vfwscanf, vfwscanf)
2948 ___vfscanf (FILE *s, const char *format, va_list argptr)
2950 return _IO_vfscanf_internal (s, format, argptr, NULL);
2952 ldbl_strong_alias (_IO_vfscanf_internal, _IO_vfscanf)
2953 ldbl_hidden_def (_IO_vfscanf_internal, _IO_vfscanf)
2954 ldbl_strong_alias (___vfscanf, __vfscanf)
2955 ldbl_hidden_def (___vfscanf, __vfscanf)
2956 ldbl_weak_alias (___vfscanf, vfscanf)