1 /* Internal functions for the *scanf* implementation.
2 Copyright (C) 1991-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
31 #include <libc-diag.h>
32 #include <libc-lock.h>
33 #include <locale/localeinfo.h>
34 #include <scratch_buffer.h>
37 # define HAVE_LONGLONG
38 # define LONGLONG long long
40 # define LONGLONG long
43 /* Determine whether we have to handle `long long' at all. */
44 #if LONG_MAX == LONG_LONG_MAX
45 # define need_longlong 0
47 # define need_longlong 1
50 /* Determine whether we have to handle `long'. */
51 #if INT_MAX == LONG_MAX
57 /* Those are flags in the conversion format. */
58 #define LONG 0x0001 /* l: long or double */
59 #define LONGDBL 0x0002 /* L: long long or long double */
60 #define SHORT 0x0004 /* h: short */
61 #define SUPPRESS 0x0008 /* *: suppress assignment */
62 #define POINTER 0x0010 /* weird %p pointer (`fake hex') */
63 #define NOSKIP 0x0020 /* do not skip blanks */
64 #define NUMBER_SIGNED 0x0040 /* signed integer */
65 #define GROUP 0x0080 /* ': group numbers */
66 #define GNU_MALLOC 0x0100 /* a: malloc strings */
67 #define CHAR 0x0200 /* hh: char */
68 #define I18N 0x0400 /* I: use locale's digits */
69 #define HEXA_FLOAT 0x0800 /* hexadecimal float */
70 #define READ_POINTER 0x1000 /* this is a pointer value */
71 #define POSIX_MALLOC 0x2000 /* m: malloc strings */
72 #define MALLOC (GNU_MALLOC | POSIX_MALLOC)
74 #include <locale/localeinfo.h>
78 # define ungetc(c, s) ((void) (c == WEOF \
80 _IO_sputbackwc (s, c))))
81 # define ungetc_not_eof(c, s) ((void) (--read_in, \
82 _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 ISSPACE(Ch) iswspace (Ch)
90 # define ISDIGIT(Ch) iswdigit (Ch)
91 # define ISXDIGIT(Ch) iswxdigit (Ch)
92 # define TOLOWER(Ch) towlower (Ch)
93 # define ORIENT if (_IO_fwide (s, 1) != 1) return WEOF
94 # define __strtoll_internal __wcstoll_internal
95 # define __strtoull_internal __wcstoull_internal
96 # define __strtol_internal __wcstol_internal
97 # define __strtoul_internal __wcstoul_internal
98 # define __strtold_internal __wcstold_internal
99 # define __strtod_internal __wcstod_internal
100 # define __strtof_internal __wcstof_internal
101 # if __HAVE_FLOAT128_UNLIKE_LDBL
102 # define __strtof128_internal __wcstof128_internal
105 # define L_(Str) L##Str
106 # define CHAR_T wchar_t
107 # define UCHAR_T unsigned int
108 # define WINT_T wint_t
112 # define ungetc(c, s) ((void) ((int) c == EOF \
114 _IO_sputbackc (s, (unsigned char) c))))
115 # define ungetc_not_eof(c, s) ((void) (--read_in, \
116 _IO_sputbackc (s, (unsigned char) c)))
117 # define inchar() (c == EOF ? ((errno = inchar_errno), EOF) \
118 : ((c = _IO_getc_unlocked (s)), \
121 : (size_t) (inchar_errno = errno)), c))
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 #include "printf-parse.h" /* Use read_int. */
138 #define encode_error() do { \
139 __set_errno (EILSEQ); \
142 #define conv_error() do { \
145 #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) \
176 __set_errno (EINVAL); \
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
;
197 struct scratch_buffer scratch
;
200 /* Returns a pointer to the first CHAR_T object in the buffer. Only
201 valid if char_buffer_add (BUFFER, CH) has been called and
202 char_buffer_error (BUFFER) is false. */
203 static inline CHAR_T
*
204 char_buffer_start (const struct char_buffer
*buffer
)
206 return (CHAR_T
*) buffer
->scratch
.data
;
209 /* Returns the number of CHAR_T objects in the buffer. Only valid if
210 char_buffer_error (BUFFER) is false. */
212 char_buffer_size (const struct char_buffer
*buffer
)
214 return buffer
->current
- char_buffer_start (buffer
);
217 /* Reinitializes BUFFER->current and BUFFER->end to cover the entire
220 char_buffer_rewind (struct char_buffer
*buffer
)
222 buffer
->current
= char_buffer_start (buffer
);
223 buffer
->end
= buffer
->current
+ buffer
->scratch
.length
/ sizeof (CHAR_T
);
226 /* Returns true if a previous call to char_buffer_add (BUFFER, CH)
229 char_buffer_error (const struct char_buffer
*buffer
)
231 return __glibc_unlikely (buffer
->current
== NULL
);
234 /* Slow path for char_buffer_add. */
236 char_buffer_add_slow (struct char_buffer
*buffer
, CHAR_T ch
)
238 if (char_buffer_error (buffer
))
240 size_t offset
= buffer
->end
- (CHAR_T
*) buffer
->scratch
.data
;
241 if (!scratch_buffer_grow_preserve (&buffer
->scratch
))
243 buffer
->current
= NULL
;
247 char_buffer_rewind (buffer
);
248 buffer
->current
+= offset
;
249 *buffer
->current
++ = ch
;
252 /* Adds CH to BUFFER. This function does not report any errors, check
253 for them with char_buffer_error. */
255 char_buffer_add (struct char_buffer
*buffer
, CHAR_T ch
)
256 __attribute__ ((always_inline
));
258 char_buffer_add (struct char_buffer
*buffer
, CHAR_T ch
)
260 if (__glibc_unlikely (buffer
->current
== buffer
->end
))
261 char_buffer_add_slow (buffer
, ch
);
263 *buffer
->current
++ = ch
;
266 /* Read formatted input from S according to the format string
267 FORMAT, using the argument list in ARG.
268 Return the number of assignments made, or -1 for an input error. */
269 #ifdef COMPILE_WSCANF
271 __vfwscanf_internal (FILE *s
, const wchar_t *format
, va_list argptr
,
272 unsigned int mode_flags
)
275 __vfscanf_internal (FILE *s
, const char *format
, va_list argptr
,
276 unsigned int mode_flags
)
280 const UCHAR_T
*f
= (const UCHAR_T
*) format
;
281 UCHAR_T fc
; /* Current character of the format. */
282 WINT_T done
= 0; /* Assignments done. */
283 size_t read_in
= 0; /* Chars read in. */
284 WINT_T c
= 0; /* Last char read. */
285 int width
; /* Maximum field width. */
286 int flags
; /* Modifiers for current format element. */
287 #ifndef COMPILE_WSCANF
288 locale_t loc
= _NL_CURRENT_LOCALE
;
289 struct __locale_data
*const curctype
= loc
->__locales
[LC_CTYPE
];
292 /* Errno of last failed inchar call. */
293 int inchar_errno
= 0;
294 /* Status for reading F-P nums. */
295 char got_digit
, got_dot
, got_e
, got_sign
;
296 /* If a [...] is a [^...]. */
298 #define exp_char not_in
299 /* Base for integral numbers. */
301 /* Decimal point character. */
302 #ifdef COMPILE_WSCANF
307 /* The thousands character of the current locale. */
308 #ifdef COMPILE_WSCANF
311 const char *thousands
;
313 struct ptrs_to_free
*ptrs_to_free
= NULL
;
314 /* State for the conversions. */
316 /* Integral holding variables. */
320 unsigned long long int uq
;
322 unsigned long int ul
;
324 /* Character-buffer pointer. */
326 wchar_t *wstr
= NULL
;
327 char **strptr
= NULL
;
329 /* We must not react on white spaces immediately because they can
330 possibly be matched even if in the input stream no character is
331 available anymore. */
334 CHAR_T
*tw
; /* Temporary pointer. */
335 struct char_buffer charbuf
;
336 scratch_buffer_init (&charbuf
.scratch
);
339 __va_copy (arg
, argptr
);
341 arg
= (va_list) argptr
;
348 ARGCHECK (s
, format
);
351 #ifndef COMPILE_WSCANF
352 struct __locale_data
*const curnumeric
= loc
->__locales
[LC_NUMERIC
];
355 /* Figure out the decimal point character. */
356 #ifdef COMPILE_WSCANF
357 decimal
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
359 decimal
= curnumeric
->values
[_NL_ITEM_INDEX (DECIMAL_POINT
)].string
;
361 /* Figure out the thousands separator character. */
362 #ifdef COMPILE_WSCANF
363 thousands
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_THOUSANDS_SEP_WC
);
365 thousands
= curnumeric
->values
[_NL_ITEM_INDEX (THOUSANDS_SEP
)].string
;
366 if (*thousands
== '\0')
371 /* Lock the stream. */
375 #ifndef COMPILE_WSCANF
376 /* From now on we use `state' to convert the format string. */
377 memset (&state
, '\0', sizeof (state
));
380 /* Run through the format string. */
384 /* Extract the next argument, which is of type TYPE.
385 For a %N$... spec, this is the Nth argument from the beginning;
386 otherwise it is the next argument after the state now in ARG. */
388 # define ARG(type) (argpos == 0 ? va_arg (arg, type) \
389 : ({ unsigned int pos = argpos; \
391 __va_copy (arg, argptr); \
393 (void) va_arg (arg, void *); \
394 va_arg (arg, type); \
398 /* XXX Possible optimization. */
399 # define ARG(type) (argpos == 0 ? va_arg (arg, type) \
400 : ({ va_list arg = (va_list) argptr; \
401 arg = (va_list) ((char *) arg \
403 * __va_rounded_size (void *)); \
404 va_arg (arg, type); \
407 # define ARG(type) (argpos == 0 ? va_arg (arg, type) \
408 : ({ unsigned int pos = argpos; \
409 va_list arg = (va_list) argptr; \
411 (void) va_arg (arg, void *); \
412 va_arg (arg, type); \
417 #ifndef COMPILE_WSCANF
420 /* Non-ASCII, may be a multibyte. */
421 int len
= __mbrlen ((const char *) f
, strlen ((const char *) f
),
428 if (__glibc_unlikely (c
== EOF
))
432 ungetc_not_eof (c
, s
);
445 /* Remember to skip spaces. */
452 /* Read a character. */
455 /* Characters other than format specs must just match. */
456 if (__glibc_unlikely (c
== EOF
))
459 /* We saw white space char as the last character in the format
460 string. Now it's time to skip all leading white space. */
464 if (__glibc_unlikely (inchar () == EOF
))
469 if (__glibc_unlikely (c
!= fc
))
478 /* This is the start of the conversion string. */
481 /* Initialize state of modifiers. */
484 /* Prepare temporary buffer. */
485 char_buffer_rewind (&charbuf
);
487 /* Check for a positional parameter specification. */
490 argpos
= read_int (&f
);
495 /* Oops; that was actually the field width. */
502 /* Check for the assignment-suppressing, the number grouping flag,
503 and the signal to use the locale's digit representation. */
504 while (*f
== L_('*') || *f
== L_('\'') || *f
== L_('I'))
511 #ifdef COMPILE_WSCANF
512 if (thousands
!= L
'\0')
514 if (thousands
!= NULL
)
523 /* Find the maximum field width. */
526 width
= read_int (&f
);
531 /* Check for type modifiers. */
535 /* ints are short ints or chars. */
547 /* A double `l' is equivalent to an `L'. */
549 flags
|= LONGDBL
| LONG
;
552 /* ints are long ints. */
557 /* doubles are long doubles, and ints are long long ints. */
558 flags
|= LONGDBL
| LONG
;
561 /* The `a' is used as a flag only if followed by `s', `S' or
563 if (*f
!= L_('s') && *f
!= L_('S') && *f
!= L_('['))
568 /* In __isoc99_*scanf %as, %aS and %a[ extension is not
570 if (__glibc_likely ((mode_flags
& SCANF_ISOC99_A
) != 0))
575 /* String conversions (%s, %[) take a `char **'
576 arg and fill it in with a malloc'd pointer. */
580 flags
|= POSIX_MALLOC
;
588 if (need_longlong
&& sizeof (size_t) > sizeof (unsigned long int))
590 else if (sizeof (size_t) > sizeof (unsigned int))
594 if (need_longlong
&& sizeof (uintmax_t) > sizeof (unsigned long int))
596 else if (sizeof (uintmax_t) > sizeof (unsigned int))
600 if (need_longlong
&& sizeof (ptrdiff_t) > sizeof (long int))
602 else if (sizeof (ptrdiff_t) > sizeof (int))
606 /* Not a recognized modifier. Backup. */
611 /* End of the format string? */
612 if (__glibc_unlikely (*f
== L_('\0')))
615 /* Find the conversion specifier. */
617 if (skip_space
|| (fc
!= L_('[') && fc
!= L_('c')
618 && fc
!= L_('C') && fc
!= L_('n')))
620 /* Eat whitespace. */
621 int save_errno
= errno
;
624 /* We add the additional test for EOF here since otherwise
625 inchar will restore the old errno value which might be
626 EINTR but does not indicate an interrupt since nothing
627 was read at this time. */
628 if (__builtin_expect ((c
== EOF
|| inchar () == EOF
)
629 && errno
== EINTR
, 0))
632 __set_errno (save_errno
);
639 case L_('%'): /* Must match a literal '%'. */
641 if (__glibc_unlikely (c
== EOF
))
643 if (__glibc_unlikely (c
!= fc
))
645 ungetc_not_eof (c
, s
);
650 case L_('n'): /* Answer number of assignments done. */
651 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
652 with the 'n' conversion specifier. */
653 if (!(flags
& SUPPRESS
))
655 /* Don't count the read-ahead. */
656 if (need_longlong
&& (flags
& LONGDBL
))
657 *ARG (long long int *) = read_in
;
658 else if (need_long
&& (flags
& LONG
))
659 *ARG (long int *) = read_in
;
660 else if (flags
& SHORT
)
661 *ARG (short int *) = read_in
;
662 else if (!(flags
& CHAR
))
663 *ARG (int *) = read_in
;
665 *ARG (char *) = read_in
;
667 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
668 /* We have a severe problem here. The ISO C standard
669 contradicts itself in explaining the effect of the %n
670 format in `scanf'. While in ISO C:1990 and the ISO C
671 Amendment 1:1995 the result is described as
673 Execution of a %n directive does not effect the
674 assignment count returned at the completion of
675 execution of the f(w)scanf function.
677 in ISO C Corrigendum 1:1994 the following was added:
680 Add the following fourth example:
683 int d1, d2, n1, n2, i;
684 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
685 the value 123 is assigned to d1 and the value3 to n1.
686 Because %n can never get an input failure the value
687 of 3 is also assigned to n2. The value of d2 is not
688 affected. The value 3 is assigned to i.
690 We go for now with the historically correct code from ISO C,
691 i.e., we don't count the %n assignments. When it ever
692 should proof to be wrong just remove the #ifdef above. */
698 case L_('c'): /* Match characters. */
699 if ((flags
& LONG
) == 0)
704 #define STRING_ARG(Str, Type, Width) \
705 do if (!(flags & SUPPRESS)) \
707 if (flags & MALLOC) \
709 /* The string is to be stored in a malloc'd buffer. */ \
710 /* For %mS using char ** is actually wrong, but \
711 shouldn't make a difference on any arch glibc \
712 supports and would unnecessarily complicate \
714 strptr = ARG (char **); \
715 if (strptr == NULL) \
717 /* Allocate an initial buffer. */ \
719 *strptr = (char *) malloc (strsize * sizeof (Type)); \
720 Str = (Type *) *strptr; \
722 add_ptr_to_free (strptr); \
723 else if (flags & POSIX_MALLOC) \
730 Str = ARG (Type *); \
734 #ifdef COMPILE_WSCANF
735 STRING_ARG (str
, char, 100);
737 STRING_ARG (str
, char, (width
> 1024 ? 1024 : width
));
741 if (__glibc_unlikely (c
== EOF
))
744 #ifdef COMPILE_WSCANF
745 /* We have to convert the wide character(s) into multibyte
746 characters and store the result. */
747 memset (&state
, '\0', sizeof (state
));
753 if (!(flags
& SUPPRESS
) && (flags
& POSIX_MALLOC
)
754 && *strptr
+ strsize
- str
<= MB_LEN_MAX
)
756 /* We have to enlarge the buffer if the `m' flag
758 size_t strleng
= str
- *strptr
;
761 newstr
= (char *) realloc (*strptr
, strsize
* 2);
764 /* Can't allocate that much. Last-ditch effort. */
765 newstr
= (char *) realloc (*strptr
,
766 strleng
+ MB_LEN_MAX
);
769 /* c can't have `a' flag, only `m'. */
776 str
= newstr
+ strleng
;
777 strsize
= strleng
+ MB_LEN_MAX
;
783 str
= newstr
+ strleng
;
788 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
789 if (__glibc_unlikely (n
== (size_t) -1))
790 /* No valid wide character. */
793 /* Increment the output pointer. Even if we don't
797 while (--width
> 0 && inchar () != EOF
);
799 if (!(flags
& SUPPRESS
))
804 && (char *) str
== *strptr
+ strsize
)
806 /* Enlarge the buffer. */
809 + (strsize
>= width
? width
- 1 : strsize
);
811 str
= (char *) realloc (*strptr
, newsize
);
814 /* Can't allocate that much. Last-ditch
816 str
= (char *) realloc (*strptr
, strsize
+ 1);
819 /* c can't have `a' flag, only `m'. */
825 *strptr
= (char *) str
;
832 *strptr
= (char *) str
;
839 while (--width
> 0 && inchar () != EOF
);
842 while (--width
> 0 && inchar () != EOF
);
845 if (!(flags
& SUPPRESS
))
847 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
849 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
864 STRING_ARG (wstr
, wchar_t, (width
> 1024 ? 1024 : width
));
867 if (__glibc_unlikely (c
== EOF
))
870 #ifdef COMPILE_WSCANF
871 /* Just store the incoming wide characters. */
872 if (!(flags
& SUPPRESS
))
877 && wstr
== (wchar_t *) *strptr
+ strsize
)
880 = strsize
+ (strsize
> width
? width
- 1 : strsize
);
881 /* Enlarge the buffer. */
882 wstr
= (wchar_t *) realloc (*strptr
,
883 newsize
* sizeof (wchar_t));
886 /* Can't allocate that much. Last-ditch effort. */
887 wstr
= (wchar_t *) realloc (*strptr
,
892 /* C or lc can't have `a' flag, only `m'
899 *strptr
= (char *) wstr
;
906 *strptr
= (char *) wstr
;
913 while (--width
> 0 && inchar () != EOF
);
916 while (--width
> 0 && inchar () != EOF
);
919 /* We have to convert the multibyte input sequence to wide
924 memset (&cstate
, '\0', sizeof (cstate
));
928 /* This is what we present the mbrtowc function first. */
931 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
932 && wstr
== (wchar_t *) *strptr
+ strsize
)
935 = strsize
+ (strsize
> width
? width
- 1 : strsize
);
936 /* Enlarge the buffer. */
937 wstr
= (wchar_t *) realloc (*strptr
,
938 newsize
* sizeof (wchar_t));
941 /* Can't allocate that much. Last-ditch effort. */
942 wstr
= (wchar_t *) realloc (*strptr
,
944 * sizeof (wchar_t)));
947 /* C or lc can't have `a' flag, only `m' flag. */
953 *strptr
= (char *) wstr
;
960 *strptr
= (char *) wstr
;
970 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
973 if (n
== (size_t) -2)
975 /* Possibly correct character, just not enough
977 if (__glibc_unlikely (inchar () == EOF
))
984 if (__glibc_unlikely (n
!= 1))
987 /* We have a match. */
991 /* Advance the result pointer. */
994 while (--width
> 0 && inchar () != EOF
);
998 if (!(flags
& SUPPRESS
))
1000 if ((flags
& MALLOC
) && wstr
- (wchar_t *) *strptr
!= strsize
)
1002 wchar_t *cp
= (wchar_t *) realloc (*strptr
,
1004 - (wchar_t *) *strptr
)
1005 * sizeof (wchar_t)));
1007 *strptr
= (char *) cp
;
1016 case L_('s'): /* Read a string. */
1017 if (!(flags
& LONG
))
1019 STRING_ARG (str
, char, 100);
1022 if (__glibc_unlikely (c
== EOF
))
1025 #ifdef COMPILE_WSCANF
1026 memset (&state
, '\0', sizeof (state
));
1033 ungetc_not_eof (c
, s
);
1037 #ifdef COMPILE_WSCANF
1038 /* This is quite complicated. We have to convert the
1039 wide characters into multibyte characters and then
1044 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
1045 && *strptr
+ strsize
- str
<= MB_LEN_MAX
)
1047 /* We have to enlarge the buffer if the `a' or `m'
1049 size_t strleng
= str
- *strptr
;
1052 newstr
= (char *) realloc (*strptr
, strsize
* 2);
1055 /* Can't allocate that much. Last-ditch
1057 newstr
= (char *) realloc (*strptr
,
1058 strleng
+ MB_LEN_MAX
);
1061 if (flags
& POSIX_MALLOC
)
1066 /* We lose. Oh well. Terminate the
1067 string and stop converting,
1068 so at least we don't skip any input. */
1069 ((char *) (*strptr
))[strleng
] = '\0';
1077 str
= newstr
+ strleng
;
1078 strsize
= strleng
+ MB_LEN_MAX
;
1084 str
= newstr
+ strleng
;
1089 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
,
1091 if (__glibc_unlikely (n
== (size_t) -1))
1094 assert (n
<= MB_LEN_MAX
);
1099 if (!(flags
& SUPPRESS
))
1102 if ((flags
& MALLOC
)
1103 && (char *) str
== *strptr
+ strsize
)
1105 /* Enlarge the buffer. */
1106 str
= (char *) realloc (*strptr
, 2 * strsize
);
1109 /* Can't allocate that much. Last-ditch
1111 str
= (char *) realloc (*strptr
, strsize
+ 1);
1114 if (flags
& POSIX_MALLOC
)
1119 /* We lose. Oh well. Terminate the
1120 string and stop converting,
1121 so at least we don't skip any input. */
1122 ((char *) (*strptr
))[strsize
- 1] = '\0';
1129 *strptr
= (char *) str
;
1136 *strptr
= (char *) str
;
1144 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
1146 if (!(flags
& SUPPRESS
))
1148 #ifdef COMPILE_WSCANF
1149 /* We have to emit the code to get into the initial
1151 char buf
[MB_LEN_MAX
];
1152 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
1153 if (n
> 0 && (flags
& MALLOC
)
1154 && str
+ n
>= *strptr
+ strsize
)
1156 /* Enlarge the buffer. */
1157 size_t strleng
= str
- *strptr
;
1160 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
1163 if (flags
& POSIX_MALLOC
)
1168 /* We lose. Oh well. Terminate the string
1169 and stop converting, so at least we don't
1171 ((char *) (*strptr
))[strleng
] = '\0';
1179 str
= newstr
+ strleng
;
1180 strsize
= strleng
+ n
+ 1;
1184 str
= __mempcpy (str
, buf
, n
);
1188 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
1190 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
1204 #ifndef COMPILE_WSCANF
1208 /* Wide character string. */
1209 STRING_ARG (wstr
, wchar_t, 100);
1212 if (__builtin_expect (c
== EOF
, 0))
1215 #ifndef COMPILE_WSCANF
1216 memset (&cstate
, '\0', sizeof (cstate
));
1223 ungetc_not_eof (c
, s
);
1227 #ifdef COMPILE_WSCANF
1229 if (!(flags
& SUPPRESS
))
1232 if ((flags
& MALLOC
)
1233 && wstr
== (wchar_t *) *strptr
+ strsize
)
1235 /* Enlarge the buffer. */
1236 wstr
= (wchar_t *) realloc (*strptr
,
1238 * sizeof (wchar_t));
1241 /* Can't allocate that much. Last-ditch
1243 wstr
= (wchar_t *) realloc (*strptr
,
1245 * sizeof (wchar_t));
1248 if (flags
& POSIX_MALLOC
)
1253 /* We lose. Oh well. Terminate the string
1254 and stop converting, so at least we don't
1256 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1263 *strptr
= (char *) wstr
;
1270 *strptr
= (char *) wstr
;
1286 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
1289 if (n
== (size_t) -2)
1291 /* Possibly correct character, just not enough
1293 if (__glibc_unlikely (inchar () == EOF
))
1300 if (__glibc_unlikely (n
!= 1))
1303 /* We have a match. */
1308 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
1309 && wstr
== (wchar_t *) *strptr
+ strsize
)
1311 /* Enlarge the buffer. */
1312 wstr
= (wchar_t *) realloc (*strptr
,
1314 * sizeof (wchar_t)));
1317 /* Can't allocate that much. Last-ditch effort. */
1318 wstr
= (wchar_t *) realloc (*strptr
,
1320 * sizeof (wchar_t)));
1323 if (flags
& POSIX_MALLOC
)
1328 /* We lose. Oh well. Terminate the
1329 string and stop converting, so at
1330 least we don't skip any input. */
1331 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1338 *strptr
= (char *) wstr
;
1345 *strptr
= (char *) wstr
;
1353 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
1355 if (!(flags
& SUPPRESS
))
1359 if ((flags
& MALLOC
) && wstr
- (wchar_t *) *strptr
!= strsize
)
1361 wchar_t *cp
= (wchar_t *) realloc (*strptr
,
1363 - (wchar_t *) *strptr
)
1364 * sizeof (wchar_t)));
1366 *strptr
= (char *) cp
;
1375 case L_('x'): /* Hexadecimal integer. */
1376 case L_('X'): /* Ditto. */
1380 case L_('o'): /* Octal integer. */
1384 case L_('u'): /* Unsigned decimal integer. */
1388 case L_('d'): /* Signed decimal integer. */
1390 flags
|= NUMBER_SIGNED
;
1393 case L_('i'): /* Generic number. */
1395 flags
|= NUMBER_SIGNED
;
1399 if (__glibc_unlikely (c
== EOF
))
1402 /* Check for a sign. */
1403 if (c
== L_('-') || c
== L_('+'))
1405 char_buffer_add (&charbuf
, c
);
1411 /* Look for a leading indication of base. */
1412 if (width
!= 0 && c
== L_('0'))
1417 char_buffer_add (&charbuf
, c
);
1420 if (width
!= 0 && TOLOWER (c
) == L_('x'))
1431 else if ((mode_flags
& SCANF_ISOC23_BIN_CST
) != 0
1434 && TOLOWER (c
) == L_('b'))
1448 if (base
== 10 && __builtin_expect ((flags
& I18N
) != 0, 0))
1453 #ifdef COMPILE_WSCANF
1454 const wchar_t *wcdigits
[10];
1455 const wchar_t *wcdigits_extended
[10];
1457 const char *mbdigits
[10];
1458 const char *mbdigits_extended
[10];
1460 /* "to_inpunct" is a map from ASCII digits to their
1461 equivalent in locale. This is defined for locales
1462 which use an extra digits set. */
1463 wctrans_t map
= __wctrans ("to_inpunct");
1467 #ifdef COMPILE_WSCANF
1468 to_level
= _NL_CURRENT_WORD (LC_CTYPE
,
1469 _NL_CTYPE_INDIGITS_WC_LEN
) - 1;
1471 to_level
= (uint32_t) curctype
->values
[_NL_ITEM_INDEX (_NL_CTYPE_INDIGITS_MB_LEN
)].word
- 1;
1474 /* Get the alternative digit forms if there are any. */
1475 if (__glibc_unlikely (map
!= NULL
))
1477 /* Adding new level for extra digits set in locale file. */
1480 for (n
= 0; n
< 10; ++n
)
1482 #ifdef COMPILE_WSCANF
1483 wcdigits
[n
] = (const wchar_t *)
1484 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1486 wchar_t *wc_extended
= (wchar_t *)
1487 alloca ((to_level
+ 2) * sizeof (wchar_t));
1488 __wmemcpy (wc_extended
, wcdigits
[n
], to_level
);
1489 wc_extended
[to_level
] = __towctrans (L
'0' + n
, map
);
1490 wc_extended
[to_level
+ 1] = '\0';
1491 wcdigits_extended
[n
] = wc_extended
;
1494 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1496 /* Get the equivalent wide char in map. */
1497 wint_t extra_wcdigit
= __towctrans (L
'0' + n
, map
);
1499 /* Convert it to multibyte representation. */
1501 memset (&state
, '\0', sizeof (state
));
1503 char extra_mbdigit
[MB_LEN_MAX
];
1505 = __wcrtomb (extra_mbdigit
, extra_wcdigit
, &state
);
1507 if (mblen
== (size_t) -1)
1509 /* Ignore this new level. */
1514 /* Calculate the length of mbdigits[n]. */
1515 const char *last_char
= mbdigits
[n
];
1516 for (level
= 0; level
< to_level
; ++level
)
1517 last_char
= strchr (last_char
, '\0') + 1;
1519 size_t mbdigits_len
= last_char
- mbdigits
[n
];
1521 /* Allocate memory for extended multibyte digit. */
1523 mb_extended
= (char *) alloca (mbdigits_len
+ mblen
+ 1);
1525 /* And get the mbdigits + extra_digit string. */
1526 *(char *) __mempcpy (__mempcpy (mb_extended
, mbdigits
[n
],
1528 extra_mbdigit
, mblen
) = '\0';
1529 mbdigits_extended
[n
] = mb_extended
;
1534 /* Read the number into workspace. */
1535 while (c
!= EOF
&& width
!= 0)
1537 /* In this round we get the pointer to the digit strings
1538 and also perform the first round of comparisons. */
1539 for (n
= 0; n
< 10; ++n
)
1541 /* Get the string for the digits with value N. */
1542 #ifdef COMPILE_WSCANF
1544 /* wcdigits_extended[] is fully set in the loop
1545 above, but the test for "map != NULL" is done
1546 inside the loop here and outside the loop there. */
1547 DIAG_PUSH_NEEDS_COMMENT
;
1548 DIAG_IGNORE_NEEDS_COMMENT (4.7, "-Wmaybe-uninitialized");
1550 if (__glibc_unlikely (map
!= NULL
))
1551 wcdigits
[n
] = wcdigits_extended
[n
];
1553 wcdigits
[n
] = (const wchar_t *)
1554 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1555 wcdigits
[n
] += from_level
;
1557 DIAG_POP_NEEDS_COMMENT
;
1559 if (c
== (wint_t) *wcdigits
[n
])
1561 to_level
= from_level
;
1565 /* Advance the pointer to the next string. */
1569 int avail
= width
> 0 ? width
: INT_MAX
;
1571 if (__glibc_unlikely (map
!= NULL
))
1572 mbdigits
[n
] = mbdigits_extended
[n
];
1575 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1577 for (level
= 0; level
< from_level
; level
++)
1578 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1581 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1583 if (*++cmpp
== '\0')
1587 if (avail
== 0 || inchar () == EOF
)
1597 to_level
= from_level
;
1601 /* We are pushing all read characters back. */
1602 if (cmpp
> mbdigits
[n
])
1605 while (--cmpp
> mbdigits
[n
])
1606 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1607 c
= (unsigned char) *cmpp
;
1610 /* Advance the pointer to the next string. */
1611 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1617 /* Have not yet found the digit. */
1618 for (level
= from_level
+ 1; level
<= to_level
; ++level
)
1620 /* Search all ten digits of this level. */
1621 for (n
= 0; n
< 10; ++n
)
1623 #ifdef COMPILE_WSCANF
1624 if (c
== (wint_t) *wcdigits
[n
])
1627 /* Advance the pointer to the next string. */
1631 int avail
= width
> 0 ? width
: INT_MAX
;
1634 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1636 if (*++cmpp
== '\0')
1640 if (avail
== 0 || inchar () == EOF
)
1653 /* We are pushing all read characters back. */
1654 if (cmpp
> mbdigits
[n
])
1657 while (--cmpp
> mbdigits
[n
])
1658 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1659 c
= (unsigned char) *cmpp
;
1662 /* Advance the pointer to the next string. */
1663 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1679 else if (flags
& GROUP
)
1681 /* Try matching against the thousands separator. */
1682 #ifdef COMPILE_WSCANF
1686 const char *cmpp
= thousands
;
1687 int avail
= width
> 0 ? width
: INT_MAX
;
1689 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1691 char_buffer_add (&charbuf
, c
);
1692 if (*++cmpp
== '\0')
1696 if (avail
== 0 || inchar () == EOF
)
1702 if (char_buffer_error (&charbuf
))
1704 __set_errno (ENOMEM
);
1711 /* We are pushing all read characters back. */
1712 if (cmpp
> thousands
)
1714 charbuf
.current
-= cmpp
- thousands
;
1716 while (--cmpp
> thousands
)
1717 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1718 c
= (unsigned char) *cmpp
;
1726 /* The last thousands character will be added back by
1727 the char_buffer_add below. */
1734 char_buffer_add (&charbuf
, c
);
1742 /* Read the number into workspace. */
1743 while (c
!= EOF
&& width
!= 0)
1750 else if (!ISDIGIT (c
) || (int) (c
- L_('0')) >= base
)
1752 if (base
== 10 && (flags
& GROUP
))
1754 /* Try matching against the thousands separator. */
1755 #ifdef COMPILE_WSCANF
1759 const char *cmpp
= thousands
;
1760 int avail
= width
> 0 ? width
: INT_MAX
;
1762 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1764 char_buffer_add (&charbuf
, c
);
1765 if (*++cmpp
== '\0')
1769 if (avail
== 0 || inchar () == EOF
)
1775 if (char_buffer_error (&charbuf
))
1777 __set_errno (ENOMEM
);
1784 /* We are pushing all read characters back. */
1785 if (cmpp
> thousands
)
1787 charbuf
.current
-= cmpp
- thousands
;
1789 while (--cmpp
> thousands
)
1790 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1791 c
= (unsigned char) *cmpp
;
1799 /* The last thousands character will be added back by
1800 the char_buffer_add below. */
1807 char_buffer_add (&charbuf
, c
);
1814 if (char_buffer_error (&charbuf
))
1816 __set_errno (ENOMEM
);
1821 if (char_buffer_size (&charbuf
) == 0
1822 || (char_buffer_size (&charbuf
) == 1
1823 && (char_buffer_start (&charbuf
)[0] == L_('+')
1824 || char_buffer_start (&charbuf
)[0] == L_('-'))))
1826 /* There was no number. If we are supposed to read a pointer
1827 we must recognize "(nil)" as well. */
1828 if (__builtin_expect (char_buffer_size (&charbuf
) == 0
1829 && (flags
& READ_POINTER
)
1830 && (width
< 0 || width
>= 5)
1832 && TOLOWER (inchar ()) == L_('n')
1833 && TOLOWER (inchar ()) == L_('i')
1834 && TOLOWER (inchar ()) == L_('l')
1835 && inchar () == L_(')'), 1))
1836 /* We must produce the value of a NULL pointer. A single
1837 '0' digit is enough. */
1838 char_buffer_add (&charbuf
, L_('0'));
1841 /* The last read character is not part of the number
1849 /* The just read character is not part of the number anymore. */
1852 /* Convert the number. */
1853 char_buffer_add (&charbuf
, L_('\0'));
1854 if (char_buffer_error (&charbuf
))
1856 __set_errno (ENOMEM
);
1860 if (need_longlong
&& (flags
& LONGDBL
))
1862 if (flags
& NUMBER_SIGNED
)
1863 num
.q
= __strtoll_internal
1864 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1866 num
.uq
= __strtoull_internal
1867 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1871 if (flags
& NUMBER_SIGNED
)
1872 num
.l
= __strtol_internal
1873 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1875 num
.ul
= __strtoul_internal
1876 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1878 if (__glibc_unlikely (char_buffer_start (&charbuf
) == tw
))
1881 if (!(flags
& SUPPRESS
))
1883 if (flags
& NUMBER_SIGNED
)
1885 if (need_longlong
&& (flags
& LONGDBL
))
1886 *ARG (LONGLONG
int *) = num
.q
;
1887 else if (need_long
&& (flags
& LONG
))
1888 *ARG (long int *) = num
.l
;
1889 else if (flags
& SHORT
)
1890 *ARG (short int *) = (short int) num
.l
;
1891 else if (!(flags
& CHAR
))
1892 *ARG (int *) = (int) num
.l
;
1894 *ARG (signed char *) = (signed char) num
.ul
;
1898 if (need_longlong
&& (flags
& LONGDBL
))
1899 *ARG (unsigned LONGLONG
int *) = num
.uq
;
1900 else if (need_long
&& (flags
& LONG
))
1901 *ARG (unsigned long int *) = num
.ul
;
1902 else if (flags
& SHORT
)
1903 *ARG (unsigned short int *)
1904 = (unsigned short int) num
.ul
;
1905 else if (!(flags
& CHAR
))
1906 *ARG (unsigned int *) = (unsigned int) num
.ul
;
1908 *ARG (unsigned char *) = (unsigned char) num
.ul
;
1914 case L_('e'): /* Floating-point numbers. */
1925 if (__glibc_unlikely (c
== EOF
))
1928 got_digit
= got_dot
= got_e
= got_sign
= 0;
1930 /* Check for a sign. */
1931 if (c
== L_('-') || c
== L_('+'))
1934 char_buffer_add (&charbuf
, c
);
1935 if (__glibc_unlikely (width
== 0 || inchar () == EOF
))
1936 /* EOF is only an input error before we read any chars. */
1942 /* Take care for the special arguments "nan" and "inf". */
1943 if (TOLOWER (c
) == L_('n'))
1946 char_buffer_add (&charbuf
, c
);
1947 if (__builtin_expect (width
== 0
1949 || TOLOWER (c
) != L_('a'), 0))
1953 char_buffer_add (&charbuf
, c
);
1954 if (__builtin_expect (width
== 0
1956 || TOLOWER (c
) != L_('n'), 0))
1960 char_buffer_add (&charbuf
, c
);
1964 else if (TOLOWER (c
) == L_('i'))
1966 /* Maybe "inf" or "infinity". */
1967 char_buffer_add (&charbuf
, c
);
1968 if (__builtin_expect (width
== 0
1970 || TOLOWER (c
) != L_('n'), 0))
1974 char_buffer_add (&charbuf
, c
);
1975 if (__builtin_expect (width
== 0
1977 || TOLOWER (c
) != L_('f'), 0))
1981 char_buffer_add (&charbuf
, c
);
1982 /* It is as least "inf". */
1983 if (width
!= 0 && inchar () != EOF
)
1985 if (TOLOWER (c
) == L_('i'))
1989 /* Now we have to read the rest as well. */
1990 char_buffer_add (&charbuf
, c
);
1991 if (__builtin_expect (width
== 0
1993 || TOLOWER (c
) != L_('n'), 0))
1997 char_buffer_add (&charbuf
, c
);
1998 if (__builtin_expect (width
== 0
2000 || TOLOWER (c
) != L_('i'), 0))
2004 char_buffer_add (&charbuf
, c
);
2005 if (__builtin_expect (width
== 0
2007 || TOLOWER (c
) != L_('t'), 0))
2011 char_buffer_add (&charbuf
, c
);
2012 if (__builtin_expect (width
== 0
2014 || TOLOWER (c
) != L_('y'), 0))
2018 char_buffer_add (&charbuf
, c
);
2028 if (width
!= 0 && c
== L_('0'))
2030 char_buffer_add (&charbuf
, c
);
2034 if (width
!= 0 && TOLOWER (c
) == L_('x'))
2036 /* It is a number in hexadecimal format. */
2037 char_buffer_add (&charbuf
, c
);
2039 flags
|= HEXA_FLOAT
;
2042 /* Grouping is not allowed. */
2054 if (char_buffer_error (&charbuf
))
2056 __set_errno (ENOMEM
);
2062 char_buffer_add (&charbuf
, c
);
2065 else if (!got_e
&& (flags
& HEXA_FLOAT
) && ISXDIGIT (c
))
2067 char_buffer_add (&charbuf
, c
);
2070 else if (got_e
&& charbuf
.current
[-1] == exp_char
2071 && (c
== L_('-') || c
== L_('+')))
2072 char_buffer_add (&charbuf
, c
);
2073 else if (got_digit
&& !got_e
2074 && (CHAR_T
) TOLOWER (c
) == exp_char
)
2076 char_buffer_add (&charbuf
, exp_char
);
2077 got_e
= got_dot
= 1;
2081 #ifdef COMPILE_WSCANF
2082 if (! got_dot
&& c
== decimal
)
2084 char_buffer_add (&charbuf
, c
);
2087 else if ((flags
& GROUP
) != 0 && ! got_dot
&& c
== thousands
)
2088 char_buffer_add (&charbuf
, c
);
2091 /* The last read character is not part of the number
2097 const char *cmpp
= decimal
;
2098 int avail
= width
> 0 ? width
: INT_MAX
;
2102 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
2103 if (*++cmpp
== '\0')
2107 if (avail
== 0 || inchar () == EOF
)
2115 /* Add all the characters. */
2116 for (cmpp
= decimal
; *cmpp
!= '\0'; ++cmpp
)
2117 char_buffer_add (&charbuf
, (unsigned char) *cmpp
);
2124 /* Figure out whether it is a thousands separator.
2125 There is one problem: we possibly read more than
2126 one character. We cannot push them back but since
2127 we know that parts of the `decimal' string matched,
2128 we can compare against it. */
2129 const char *cmp2p
= thousands
;
2131 if ((flags
& GROUP
) != 0 && ! got_dot
)
2133 while (cmp2p
- thousands
< cmpp
- decimal
2134 && *cmp2p
== decimal
[cmp2p
- thousands
])
2136 if (cmp2p
- thousands
== cmpp
- decimal
)
2138 while ((unsigned char) *cmp2p
== c
&& avail
>= 0)
2139 if (*++cmp2p
== '\0')
2143 if (avail
== 0 || inchar () == EOF
)
2150 if (cmp2p
!= NULL
&& *cmp2p
== '\0')
2152 /* Add all the characters. */
2153 for (cmpp
= thousands
; *cmpp
!= '\0'; ++cmpp
)
2154 char_buffer_add (&charbuf
, (unsigned char) *cmpp
);
2160 /* The last read character is not part of the number
2169 if (width
== 0 || inchar () == EOF
)
2176 if (char_buffer_error (&charbuf
))
2178 __set_errno (ENOMEM
);
2184 if (__builtin_expect ((flags
& I18N
) != 0, 0)
2185 /* Hexadecimal floats make no sense, fixing localized
2186 digits with ASCII letters. */
2187 && !(flags
& HEXA_FLOAT
)
2188 /* Minimum requirement. */
2189 && (char_buffer_size (&charbuf
) == got_sign
|| got_dot
)
2190 && (map
= __wctrans ("to_inpunct")) != NULL
)
2192 /* Reget the first character. */
2195 /* Localized digits, decimal points, and thousands
2197 wint_t wcdigits
[12];
2199 /* First get decimal equivalent to check if we read it
2201 wcdigits
[11] = __towctrans (L
'.', map
);
2203 /* If we have not read any character or have just read
2204 locale decimal point which matches the decimal point
2205 for localized FP numbers, then we may have localized
2206 digits. Note, we test GOT_DOT above. */
2207 #ifdef COMPILE_WSCANF
2208 if (char_buffer_size (&charbuf
) == got_sign
2209 || (char_buffer_size (&charbuf
) == got_sign
+ 1
2210 && wcdigits
[11] == decimal
))
2212 char mbdigits
[12][MB_LEN_MAX
+ 1];
2215 memset (&state
, '\0', sizeof (state
));
2217 bool match_so_far
= char_buffer_size (&charbuf
) == got_sign
;
2218 size_t mblen
= __wcrtomb (mbdigits
[11], wcdigits
[11], &state
);
2219 if (mblen
!= (size_t) -1)
2221 mbdigits
[11][mblen
] = '\0';
2223 (char_buffer_size (&charbuf
) == strlen (decimal
) + got_sign
2224 && strcmp (decimal
, mbdigits
[11]) == 0);
2228 size_t decimal_len
= strlen (decimal
);
2229 /* This should always be the case but the data comes
2231 if (decimal_len
<= MB_LEN_MAX
)
2233 match_so_far
|= (char_buffer_size (&charbuf
)
2234 == decimal_len
+ got_sign
);
2235 memcpy (mbdigits
[11], decimal
, decimal_len
+ 1);
2238 match_so_far
= false;
2244 bool have_locthousands
= (flags
& GROUP
) != 0;
2246 /* Now get the digits and the thousands-sep equivalents. */
2247 for (int n
= 0; n
< 11; ++n
)
2250 wcdigits
[n
] = __towctrans (L
'0' + n
, map
);
2253 wcdigits
[10] = __towctrans (L
',', map
);
2254 have_locthousands
&= wcdigits
[10] != L
'\0';
2257 #ifndef COMPILE_WSCANF
2258 memset (&state
, '\0', sizeof (state
));
2260 size_t mblen
= __wcrtomb (mbdigits
[n
], wcdigits
[n
],
2262 if (mblen
== (size_t) -1)
2266 if (have_locthousands
)
2268 size_t thousands_len
= strlen (thousands
);
2269 if (thousands_len
<= MB_LEN_MAX
)
2270 memcpy (mbdigits
[10], thousands
,
2273 have_locthousands
= false;
2277 /* Ignore checking against localized digits. */
2281 mbdigits
[n
][mblen
] = '\0';
2285 /* Start checking against localized digits, if
2286 conversion is done correctly. */
2289 if (char_buffer_error (&charbuf
))
2291 __set_errno (ENOMEM
);
2295 if (got_e
&& charbuf
.current
[-1] == exp_char
2296 && (c
== L_('-') || c
== L_('+')))
2297 char_buffer_add (&charbuf
, c
);
2298 else if (char_buffer_size (&charbuf
) > got_sign
&& !got_e
2299 && (CHAR_T
) TOLOWER (c
) == exp_char
)
2301 char_buffer_add (&charbuf
, exp_char
);
2302 got_e
= got_dot
= 1;
2306 /* Check against localized digits, decimal point,
2307 and thousands separator. */
2309 for (n
= 0; n
< 12; ++n
)
2311 #ifdef COMPILE_WSCANF
2312 if (c
== wcdigits
[n
])
2315 char_buffer_add (&charbuf
, L_('0') + n
);
2316 else if (n
== 11 && !got_dot
)
2318 char_buffer_add (&charbuf
, decimal
);
2321 else if (n
== 10 && have_locthousands
2323 char_buffer_add (&charbuf
, thousands
);
2325 /* The last read character is not part
2326 of the number anymore. */
2332 const char *cmpp
= mbdigits
[n
];
2333 int avail
= width
> 0 ? width
: INT_MAX
;
2335 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
2336 if (*++cmpp
== '\0')
2340 if (avail
== 0 || inchar () == EOF
)
2350 char_buffer_add (&charbuf
, L_('0') + n
);
2351 else if (n
== 11 && !got_dot
)
2353 /* Add all the characters. */
2354 for (cmpp
= decimal
; *cmpp
!= '\0';
2356 char_buffer_add (&charbuf
,
2357 (unsigned char) *cmpp
);
2361 else if (n
== 10 && (flags
& GROUP
) != 0
2364 /* Add all the characters. */
2365 for (cmpp
= thousands
; *cmpp
!= '\0';
2367 char_buffer_add (&charbuf
,
2368 (unsigned char) *cmpp
);
2371 /* The last read character is not part
2372 of the number anymore. */
2378 /* We are pushing all read characters back. */
2379 if (cmpp
> mbdigits
[n
])
2382 while (--cmpp
> mbdigits
[n
])
2383 ungetc_not_eof ((unsigned char) *cmpp
, s
);
2384 c
= (unsigned char) *cmpp
;
2391 /* The last read character is not part
2392 of the number anymore. */
2398 if (width
== 0 || inchar () == EOF
)
2406 #ifndef COMPILE_WSCANF
2412 if (char_buffer_error (&charbuf
))
2414 __set_errno (ENOMEM
);
2419 /* Have we read any character? If we try to read a number
2420 in hexadecimal notation and we have read only the `0x'
2421 prefix this is an error. */
2422 if (__glibc_unlikely (char_buffer_size (&charbuf
) == got_sign
2423 || ((flags
& HEXA_FLOAT
)
2424 && (char_buffer_size (&charbuf
)
2429 /* Convert the number. */
2430 char_buffer_add (&charbuf
, L_('\0'));
2431 if (char_buffer_error (&charbuf
))
2433 __set_errno (ENOMEM
);
2437 #if __HAVE_FLOAT128_UNLIKE_LDBL
2438 if ((flags
& LONGDBL
) \
2439 && (mode_flags
& SCANF_LDBL_USES_FLOAT128
) != 0)
2441 _Float128 d
= __strtof128_internal
2442 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2443 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2444 *ARG (_Float128
*) = d
;
2448 if ((flags
& LONGDBL
) \
2449 && __glibc_likely ((mode_flags
& SCANF_LDBL_IS_DBL
) == 0))
2451 long double d
= __strtold_internal
2452 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2453 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2454 *ARG (long double *) = d
;
2456 else if (flags
& (LONG
| LONGDBL
))
2458 double d
= __strtod_internal
2459 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2460 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2461 *ARG (double *) = d
;
2465 float d
= __strtof_internal
2466 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2467 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2471 if (__glibc_unlikely (tw
== char_buffer_start (&charbuf
)))
2474 if (!(flags
& SUPPRESS
))
2478 case L_('['): /* Character class. */
2480 STRING_ARG (wstr
, wchar_t, 100);
2482 STRING_ARG (str
, char, 100);
2493 #ifdef COMPILE_WSCANF
2494 /* Find the beginning and the end of the scanlist. We are not
2495 creating a lookup table since it would have to be too large.
2496 Instead we search each time through the string. This is not
2497 a constant lookup time but who uses this feature deserves to
2499 tw
= (wchar_t *) f
; /* Marks the beginning. */
2504 while ((fc
= *f
++) != L
'\0' && fc
!= L
']');
2506 if (__glibc_unlikely (fc
== L
'\0'))
2508 wchar_t *twend
= (wchar_t *) f
- 1;
2510 /* Fill WP with byte flags indexed by character.
2511 We will use this flag map for matching input characters. */
2512 if (!scratch_buffer_set_array_size
2513 (&charbuf
.scratch
, UCHAR_MAX
+ 1, 1))
2518 memset (charbuf
.scratch
.data
, '\0', UCHAR_MAX
+ 1);
2521 if (fc
== ']' || fc
== '-')
2523 /* If ] or - appears before any char in the set, it is not
2524 the terminator or separator, but the first char in the
2526 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2530 while ((fc
= *f
++) != '\0' && fc
!= ']')
2531 if (fc
== '-' && *f
!= '\0' && *f
!= ']' && f
[-2] <= *f
)
2533 /* Add all characters from the one before the '-'
2534 up to (but not including) the next format char. */
2535 for (fc
= f
[-2]; fc
< *f
; ++fc
)
2536 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2539 /* Add the character to the flag map. */
2540 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2542 if (__glibc_unlikely (fc
== '\0'))
2548 size_t now
= read_in
;
2549 #ifdef COMPILE_WSCANF
2550 if (__glibc_unlikely (inchar () == WEOF
))
2557 /* Test whether it's in the scanlist. */
2559 while (runp
< twend
)
2561 if (runp
[0] == L
'-' && runp
[1] != '\0'
2562 && runp
+ 1 != twend
2564 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2566 /* Match against all characters in between the
2567 first and last character of the sequence. */
2570 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2571 if ((wint_t) wc
== c
)
2574 if (wc
<= runp
[1] && !not_in
)
2576 if (wc
<= runp
[1] && not_in
)
2578 /* The current character is not in the
2588 if ((wint_t) *runp
== c
&& !not_in
)
2590 if ((wint_t) *runp
== c
&& not_in
)
2600 if (runp
== twend
&& !not_in
)
2606 if (!(flags
& SUPPRESS
))
2610 if ((flags
& MALLOC
)
2611 && wstr
== (wchar_t *) *strptr
+ strsize
)
2613 /* Enlarge the buffer. */
2614 wstr
= (wchar_t *) realloc (*strptr
,
2616 * sizeof (wchar_t));
2619 /* Can't allocate that much. Last-ditch
2622 realloc (*strptr
, (strsize
+ 1)
2623 * sizeof (wchar_t));
2626 if (flags
& POSIX_MALLOC
)
2631 /* We lose. Oh well. Terminate the string
2632 and stop converting, so at least we don't
2634 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2641 *strptr
= (char *) wstr
;
2648 *strptr
= (char *) wstr
;
2655 while ((width
< 0 || --width
> 0) && inchar () != WEOF
);
2658 char buf
[MB_LEN_MAX
];
2662 if (__glibc_unlikely (inchar () == EOF
))
2665 memset (&cstate
, '\0', sizeof (cstate
));
2669 if (((char *) charbuf
.scratch
.data
)[c
] == not_in
)
2671 ungetc_not_eof (c
, s
);
2676 if (!(flags
& SUPPRESS
))
2680 /* Convert it into a wide character. */
2682 n
= __mbrtowc (wstr
, buf
, 1, &cstate
);
2684 if (n
== (size_t) -2)
2686 /* Possibly correct character, just not enough
2689 assert (cnt
< MB_LEN_MAX
);
2695 if ((flags
& MALLOC
)
2696 && wstr
== (wchar_t *) *strptr
+ strsize
)
2698 /* Enlarge the buffer. */
2699 wstr
= (wchar_t *) realloc (*strptr
,
2701 * sizeof (wchar_t)));
2704 /* Can't allocate that much. Last-ditch
2707 realloc (*strptr
, ((strsize
+ 1)
2708 * sizeof (wchar_t)));
2711 if (flags
& POSIX_MALLOC
)
2716 /* We lose. Oh well. Terminate the
2717 string and stop converting,
2718 so at least we don't skip any input. */
2719 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2726 *strptr
= (char *) wstr
;
2733 *strptr
= (char *) wstr
;
2740 if (width
>= 0 && --width
<= 0)
2743 while (inchar () != EOF
);
2745 if (__glibc_unlikely (cnt
!= 0))
2746 /* We stopped in the middle of recognizing another
2747 character. That's a problem. */
2751 if (__glibc_unlikely (now
== read_in
))
2752 /* We haven't successfully read any character. */
2755 if (!(flags
& SUPPRESS
))
2759 if ((flags
& MALLOC
)
2760 && wstr
- (wchar_t *) *strptr
!= strsize
)
2762 wchar_t *cp
= (wchar_t *)
2763 realloc (*strptr
, ((wstr
- (wchar_t *) *strptr
)
2764 * sizeof (wchar_t)));
2766 *strptr
= (char *) cp
;
2775 size_t now
= read_in
;
2777 if (__glibc_unlikely (inchar () == EOF
))
2780 #ifdef COMPILE_WSCANF
2782 memset (&state
, '\0', sizeof (state
));
2789 /* Test whether it's in the scanlist. */
2791 while (runp
< twend
)
2793 if (runp
[0] == L
'-' && runp
[1] != '\0'
2794 && runp
+ 1 != twend
2796 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2798 /* Match against all characters in between the
2799 first and last character of the sequence. */
2802 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2803 if ((wint_t) wc
== c
)
2806 if (wc
<= runp
[1] && !not_in
)
2808 if (wc
<= runp
[1] && not_in
)
2810 /* The current character is not in the
2820 if ((wint_t) *runp
== c
&& !not_in
)
2822 if ((wint_t) *runp
== c
&& not_in
)
2832 if (runp
== twend
&& !not_in
)
2838 if (!(flags
& SUPPRESS
))
2840 if ((flags
& MALLOC
)
2841 && *strptr
+ strsize
- str
<= MB_LEN_MAX
)
2843 /* Enlarge the buffer. */
2844 size_t strleng
= str
- *strptr
;
2847 newstr
= (char *) realloc (*strptr
, 2 * strsize
);
2850 /* Can't allocate that much. Last-ditch
2852 newstr
= (char *) realloc (*strptr
,
2853 strleng
+ MB_LEN_MAX
);
2856 if (flags
& POSIX_MALLOC
)
2861 /* We lose. Oh well. Terminate the string
2862 and stop converting, so at least we don't
2864 ((char *) (*strptr
))[strleng
] = '\0';
2872 str
= newstr
+ strleng
;
2873 strsize
= strleng
+ MB_LEN_MAX
;
2879 str
= newstr
+ strleng
;
2885 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
2886 if (__glibc_unlikely (n
== (size_t) -1))
2889 assert (n
<= MB_LEN_MAX
);
2892 while ((width
< 0 || --width
> 0) && inchar () != WEOF
);
2897 if (((char *) charbuf
.scratch
.data
)[c
] == not_in
)
2899 ungetc_not_eof (c
, s
);
2904 if (!(flags
& SUPPRESS
))
2907 if ((flags
& MALLOC
)
2908 && (char *) str
== *strptr
+ strsize
)
2910 /* Enlarge the buffer. */
2911 size_t newsize
= 2 * strsize
;
2914 str
= (char *) realloc (*strptr
, newsize
);
2917 /* Can't allocate that much. Last-ditch
2919 if (newsize
> strsize
+ 1)
2921 newsize
= strsize
+ 1;
2924 if (flags
& POSIX_MALLOC
)
2929 /* We lose. Oh well. Terminate the
2930 string and stop converting,
2931 so at least we don't skip any input. */
2932 ((char *) (*strptr
))[strsize
- 1] = '\0';
2939 *strptr
= (char *) str
;
2946 while ((width
< 0 || --width
> 0) && inchar () != EOF
);
2949 if (__glibc_unlikely (now
== read_in
))
2950 /* We haven't successfully read any character. */
2953 if (!(flags
& SUPPRESS
))
2955 #ifdef COMPILE_WSCANF
2956 /* We have to emit the code to get into the initial
2958 char buf
[MB_LEN_MAX
];
2959 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
2960 if (n
> 0 && (flags
& MALLOC
)
2961 && str
+ n
>= *strptr
+ strsize
)
2963 /* Enlarge the buffer. */
2964 size_t strleng
= str
- *strptr
;
2967 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
2970 if (flags
& POSIX_MALLOC
)
2975 /* We lose. Oh well. Terminate the string
2976 and stop converting, so at least we don't
2978 ((char *) (*strptr
))[strleng
] = '\0';
2986 str
= newstr
+ strleng
;
2987 strsize
= strleng
+ n
+ 1;
2991 str
= __mempcpy (str
, buf
, n
);
2995 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
2997 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
3008 case L_('p'): /* Generic pointer. */
3010 /* A PTR must be the same size as a `long int'. */
3011 flags
&= ~(SHORT
|LONGDBL
);
3014 flags
|= READ_POINTER
;
3018 /* If this is an unknown format character punt. */
3023 /* The last thing we saw int the format string was a white space.
3024 Consume the last white spaces. */
3029 while (ISSPACE (c
));
3034 /* Unlock stream. */
3037 scratch_buffer_free (&charbuf
.scratch
);
3039 if (__glibc_unlikely (done
== EOF
))
3041 if (__glibc_unlikely (ptrs_to_free
!= NULL
))
3043 struct ptrs_to_free
*p
= ptrs_to_free
;
3046 for (size_t cnt
= 0; cnt
< p
->count
; ++cnt
)
3048 free (*p
->ptrs
[cnt
]);
3049 *p
->ptrs
[cnt
] = NULL
;
3056 else if (__glibc_unlikely (strptr
!= NULL
))