1 /* Internal functions for the *scanf* implementation.
2 Copyright (C) 1991-2022 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 Amendement 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'))
1438 if (base
== 10 && __builtin_expect ((flags
& I18N
) != 0, 0))
1443 #ifdef COMPILE_WSCANF
1444 const wchar_t *wcdigits
[10];
1445 const wchar_t *wcdigits_extended
[10];
1447 const char *mbdigits
[10];
1448 const char *mbdigits_extended
[10];
1450 /* "to_inpunct" is a map from ASCII digits to their
1451 equivalent in locale. This is defined for locales
1452 which use an extra digits set. */
1453 wctrans_t map
= __wctrans ("to_inpunct");
1457 #ifdef COMPILE_WSCANF
1458 to_level
= _NL_CURRENT_WORD (LC_CTYPE
,
1459 _NL_CTYPE_INDIGITS_WC_LEN
) - 1;
1461 to_level
= (uint32_t) curctype
->values
[_NL_ITEM_INDEX (_NL_CTYPE_INDIGITS_MB_LEN
)].word
- 1;
1464 /* Get the alternative digit forms if there are any. */
1465 if (__glibc_unlikely (map
!= NULL
))
1467 /* Adding new level for extra digits set in locale file. */
1470 for (n
= 0; n
< 10; ++n
)
1472 #ifdef COMPILE_WSCANF
1473 wcdigits
[n
] = (const wchar_t *)
1474 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1476 wchar_t *wc_extended
= (wchar_t *)
1477 alloca ((to_level
+ 2) * sizeof (wchar_t));
1478 __wmemcpy (wc_extended
, wcdigits
[n
], to_level
);
1479 wc_extended
[to_level
] = __towctrans (L
'0' + n
, map
);
1480 wc_extended
[to_level
+ 1] = '\0';
1481 wcdigits_extended
[n
] = wc_extended
;
1484 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1486 /* Get the equivalent wide char in map. */
1487 wint_t extra_wcdigit
= __towctrans (L
'0' + n
, map
);
1489 /* Convert it to multibyte representation. */
1491 memset (&state
, '\0', sizeof (state
));
1493 char extra_mbdigit
[MB_LEN_MAX
];
1495 = __wcrtomb (extra_mbdigit
, extra_wcdigit
, &state
);
1497 if (mblen
== (size_t) -1)
1499 /* Ignore this new level. */
1504 /* Calculate the length of mbdigits[n]. */
1505 const char *last_char
= mbdigits
[n
];
1506 for (level
= 0; level
< to_level
; ++level
)
1507 last_char
= strchr (last_char
, '\0') + 1;
1509 size_t mbdigits_len
= last_char
- mbdigits
[n
];
1511 /* Allocate memory for extended multibyte digit. */
1513 mb_extended
= (char *) alloca (mbdigits_len
+ mblen
+ 1);
1515 /* And get the mbdigits + extra_digit string. */
1516 *(char *) __mempcpy (__mempcpy (mb_extended
, mbdigits
[n
],
1518 extra_mbdigit
, mblen
) = '\0';
1519 mbdigits_extended
[n
] = mb_extended
;
1524 /* Read the number into workspace. */
1525 while (c
!= EOF
&& width
!= 0)
1527 /* In this round we get the pointer to the digit strings
1528 and also perform the first round of comparisons. */
1529 for (n
= 0; n
< 10; ++n
)
1531 /* Get the string for the digits with value N. */
1532 #ifdef COMPILE_WSCANF
1534 /* wcdigits_extended[] is fully set in the loop
1535 above, but the test for "map != NULL" is done
1536 inside the loop here and outside the loop there. */
1537 DIAG_PUSH_NEEDS_COMMENT
;
1538 DIAG_IGNORE_NEEDS_COMMENT (4.7, "-Wmaybe-uninitialized");
1540 if (__glibc_unlikely (map
!= NULL
))
1541 wcdigits
[n
] = wcdigits_extended
[n
];
1543 wcdigits
[n
] = (const wchar_t *)
1544 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1545 wcdigits
[n
] += from_level
;
1547 DIAG_POP_NEEDS_COMMENT
;
1549 if (c
== (wint_t) *wcdigits
[n
])
1551 to_level
= from_level
;
1555 /* Advance the pointer to the next string. */
1559 int avail
= width
> 0 ? width
: INT_MAX
;
1561 if (__glibc_unlikely (map
!= NULL
))
1562 mbdigits
[n
] = mbdigits_extended
[n
];
1565 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1567 for (level
= 0; level
< from_level
; level
++)
1568 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1571 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1573 if (*++cmpp
== '\0')
1577 if (avail
== 0 || inchar () == EOF
)
1587 to_level
= from_level
;
1591 /* We are pushing all read characters back. */
1592 if (cmpp
> mbdigits
[n
])
1595 while (--cmpp
> mbdigits
[n
])
1596 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1597 c
= (unsigned char) *cmpp
;
1600 /* Advance the pointer to the next string. */
1601 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1607 /* Have not yet found the digit. */
1608 for (level
= from_level
+ 1; level
<= to_level
; ++level
)
1610 /* Search all ten digits of this level. */
1611 for (n
= 0; n
< 10; ++n
)
1613 #ifdef COMPILE_WSCANF
1614 if (c
== (wint_t) *wcdigits
[n
])
1617 /* Advance the pointer to the next string. */
1621 int avail
= width
> 0 ? width
: INT_MAX
;
1624 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1626 if (*++cmpp
== '\0')
1630 if (avail
== 0 || inchar () == EOF
)
1643 /* We are pushing all read characters back. */
1644 if (cmpp
> mbdigits
[n
])
1647 while (--cmpp
> mbdigits
[n
])
1648 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1649 c
= (unsigned char) *cmpp
;
1652 /* Advance the pointer to the next string. */
1653 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1669 else if (flags
& GROUP
)
1671 /* Try matching against the thousands separator. */
1672 #ifdef COMPILE_WSCANF
1676 const char *cmpp
= thousands
;
1677 int avail
= width
> 0 ? width
: INT_MAX
;
1679 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1681 char_buffer_add (&charbuf
, c
);
1682 if (*++cmpp
== '\0')
1686 if (avail
== 0 || inchar () == EOF
)
1692 if (char_buffer_error (&charbuf
))
1694 __set_errno (ENOMEM
);
1701 /* We are pushing all read characters back. */
1702 if (cmpp
> thousands
)
1704 charbuf
.current
-= cmpp
- thousands
;
1706 while (--cmpp
> thousands
)
1707 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1708 c
= (unsigned char) *cmpp
;
1716 /* The last thousands character will be added back by
1717 the char_buffer_add below. */
1724 char_buffer_add (&charbuf
, c
);
1732 /* Read the number into workspace. */
1733 while (c
!= EOF
&& width
!= 0)
1740 else if (!ISDIGIT (c
) || (int) (c
- L_('0')) >= base
)
1742 if (base
== 10 && (flags
& GROUP
))
1744 /* Try matching against the thousands separator. */
1745 #ifdef COMPILE_WSCANF
1749 const char *cmpp
= thousands
;
1750 int avail
= width
> 0 ? width
: INT_MAX
;
1752 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1754 char_buffer_add (&charbuf
, c
);
1755 if (*++cmpp
== '\0')
1759 if (avail
== 0 || inchar () == EOF
)
1765 if (char_buffer_error (&charbuf
))
1767 __set_errno (ENOMEM
);
1774 /* We are pushing all read characters back. */
1775 if (cmpp
> thousands
)
1777 charbuf
.current
-= cmpp
- thousands
;
1779 while (--cmpp
> thousands
)
1780 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1781 c
= (unsigned char) *cmpp
;
1789 /* The last thousands character will be added back by
1790 the char_buffer_add below. */
1797 char_buffer_add (&charbuf
, c
);
1804 if (char_buffer_error (&charbuf
))
1806 __set_errno (ENOMEM
);
1811 if (char_buffer_size (&charbuf
) == 0
1812 || (char_buffer_size (&charbuf
) == 1
1813 && (char_buffer_start (&charbuf
)[0] == L_('+')
1814 || char_buffer_start (&charbuf
)[0] == L_('-'))))
1816 /* There was no number. If we are supposed to read a pointer
1817 we must recognize "(nil)" as well. */
1818 if (__builtin_expect (char_buffer_size (&charbuf
) == 0
1819 && (flags
& READ_POINTER
)
1820 && (width
< 0 || width
>= 5)
1822 && TOLOWER (inchar ()) == L_('n')
1823 && TOLOWER (inchar ()) == L_('i')
1824 && TOLOWER (inchar ()) == L_('l')
1825 && inchar () == L_(')'), 1))
1826 /* We must produce the value of a NULL pointer. A single
1827 '0' digit is enough. */
1828 char_buffer_add (&charbuf
, L_('0'));
1831 /* The last read character is not part of the number
1839 /* The just read character is not part of the number anymore. */
1842 /* Convert the number. */
1843 char_buffer_add (&charbuf
, L_('\0'));
1844 if (char_buffer_error (&charbuf
))
1846 __set_errno (ENOMEM
);
1850 if (need_longlong
&& (flags
& LONGDBL
))
1852 if (flags
& NUMBER_SIGNED
)
1853 num
.q
= __strtoll_internal
1854 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1856 num
.uq
= __strtoull_internal
1857 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1861 if (flags
& NUMBER_SIGNED
)
1862 num
.l
= __strtol_internal
1863 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1865 num
.ul
= __strtoul_internal
1866 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1868 if (__glibc_unlikely (char_buffer_start (&charbuf
) == tw
))
1871 if (!(flags
& SUPPRESS
))
1873 if (flags
& NUMBER_SIGNED
)
1875 if (need_longlong
&& (flags
& LONGDBL
))
1876 *ARG (LONGLONG
int *) = num
.q
;
1877 else if (need_long
&& (flags
& LONG
))
1878 *ARG (long int *) = num
.l
;
1879 else if (flags
& SHORT
)
1880 *ARG (short int *) = (short int) num
.l
;
1881 else if (!(flags
& CHAR
))
1882 *ARG (int *) = (int) num
.l
;
1884 *ARG (signed char *) = (signed char) num
.ul
;
1888 if (need_longlong
&& (flags
& LONGDBL
))
1889 *ARG (unsigned LONGLONG
int *) = num
.uq
;
1890 else if (need_long
&& (flags
& LONG
))
1891 *ARG (unsigned long int *) = num
.ul
;
1892 else if (flags
& SHORT
)
1893 *ARG (unsigned short int *)
1894 = (unsigned short int) num
.ul
;
1895 else if (!(flags
& CHAR
))
1896 *ARG (unsigned int *) = (unsigned int) num
.ul
;
1898 *ARG (unsigned char *) = (unsigned char) num
.ul
;
1904 case L_('e'): /* Floating-point numbers. */
1915 if (__glibc_unlikely (c
== EOF
))
1918 got_digit
= got_dot
= got_e
= got_sign
= 0;
1920 /* Check for a sign. */
1921 if (c
== L_('-') || c
== L_('+'))
1924 char_buffer_add (&charbuf
, c
);
1925 if (__glibc_unlikely (width
== 0 || inchar () == EOF
))
1926 /* EOF is only an input error before we read any chars. */
1932 /* Take care for the special arguments "nan" and "inf". */
1933 if (TOLOWER (c
) == L_('n'))
1936 char_buffer_add (&charbuf
, c
);
1937 if (__builtin_expect (width
== 0
1939 || TOLOWER (c
) != L_('a'), 0))
1943 char_buffer_add (&charbuf
, c
);
1944 if (__builtin_expect (width
== 0
1946 || TOLOWER (c
) != L_('n'), 0))
1950 char_buffer_add (&charbuf
, c
);
1954 else if (TOLOWER (c
) == L_('i'))
1956 /* Maybe "inf" or "infinity". */
1957 char_buffer_add (&charbuf
, c
);
1958 if (__builtin_expect (width
== 0
1960 || TOLOWER (c
) != L_('n'), 0))
1964 char_buffer_add (&charbuf
, c
);
1965 if (__builtin_expect (width
== 0
1967 || TOLOWER (c
) != L_('f'), 0))
1971 char_buffer_add (&charbuf
, c
);
1972 /* It is as least "inf". */
1973 if (width
!= 0 && inchar () != EOF
)
1975 if (TOLOWER (c
) == L_('i'))
1979 /* Now we have to read the rest as well. */
1980 char_buffer_add (&charbuf
, c
);
1981 if (__builtin_expect (width
== 0
1983 || TOLOWER (c
) != L_('n'), 0))
1987 char_buffer_add (&charbuf
, c
);
1988 if (__builtin_expect (width
== 0
1990 || TOLOWER (c
) != L_('i'), 0))
1994 char_buffer_add (&charbuf
, c
);
1995 if (__builtin_expect (width
== 0
1997 || TOLOWER (c
) != L_('t'), 0))
2001 char_buffer_add (&charbuf
, c
);
2002 if (__builtin_expect (width
== 0
2004 || TOLOWER (c
) != L_('y'), 0))
2008 char_buffer_add (&charbuf
, c
);
2018 if (width
!= 0 && c
== L_('0'))
2020 char_buffer_add (&charbuf
, c
);
2024 if (width
!= 0 && TOLOWER (c
) == L_('x'))
2026 /* It is a number in hexadecimal format. */
2027 char_buffer_add (&charbuf
, c
);
2029 flags
|= HEXA_FLOAT
;
2032 /* Grouping is not allowed. */
2044 if (char_buffer_error (&charbuf
))
2046 __set_errno (ENOMEM
);
2052 char_buffer_add (&charbuf
, c
);
2055 else if (!got_e
&& (flags
& HEXA_FLOAT
) && ISXDIGIT (c
))
2057 char_buffer_add (&charbuf
, c
);
2060 else if (got_e
&& charbuf
.current
[-1] == exp_char
2061 && (c
== L_('-') || c
== L_('+')))
2062 char_buffer_add (&charbuf
, c
);
2063 else if (got_digit
&& !got_e
2064 && (CHAR_T
) TOLOWER (c
) == exp_char
)
2066 char_buffer_add (&charbuf
, exp_char
);
2067 got_e
= got_dot
= 1;
2071 #ifdef COMPILE_WSCANF
2072 if (! got_dot
&& c
== decimal
)
2074 char_buffer_add (&charbuf
, c
);
2077 else if ((flags
& GROUP
) != 0 && ! got_dot
&& c
== thousands
)
2078 char_buffer_add (&charbuf
, c
);
2081 /* The last read character is not part of the number
2087 const char *cmpp
= decimal
;
2088 int avail
= width
> 0 ? width
: INT_MAX
;
2092 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
2093 if (*++cmpp
== '\0')
2097 if (avail
== 0 || inchar () == EOF
)
2105 /* Add all the characters. */
2106 for (cmpp
= decimal
; *cmpp
!= '\0'; ++cmpp
)
2107 char_buffer_add (&charbuf
, (unsigned char) *cmpp
);
2114 /* Figure out whether it is a thousands separator.
2115 There is one problem: we possibly read more than
2116 one character. We cannot push them back but since
2117 we know that parts of the `decimal' string matched,
2118 we can compare against it. */
2119 const char *cmp2p
= thousands
;
2121 if ((flags
& GROUP
) != 0 && ! got_dot
)
2123 while (cmp2p
- thousands
< cmpp
- decimal
2124 && *cmp2p
== decimal
[cmp2p
- thousands
])
2126 if (cmp2p
- thousands
== cmpp
- decimal
)
2128 while ((unsigned char) *cmp2p
== c
&& avail
>= 0)
2129 if (*++cmp2p
== '\0')
2133 if (avail
== 0 || inchar () == EOF
)
2140 if (cmp2p
!= NULL
&& *cmp2p
== '\0')
2142 /* Add all the characters. */
2143 for (cmpp
= thousands
; *cmpp
!= '\0'; ++cmpp
)
2144 char_buffer_add (&charbuf
, (unsigned char) *cmpp
);
2150 /* The last read character is not part of the number
2159 if (width
== 0 || inchar () == EOF
)
2166 if (char_buffer_error (&charbuf
))
2168 __set_errno (ENOMEM
);
2174 if (__builtin_expect ((flags
& I18N
) != 0, 0)
2175 /* Hexadecimal floats make no sense, fixing localized
2176 digits with ASCII letters. */
2177 && !(flags
& HEXA_FLOAT
)
2178 /* Minimum requirement. */
2179 && (char_buffer_size (&charbuf
) == got_sign
|| got_dot
)
2180 && (map
= __wctrans ("to_inpunct")) != NULL
)
2182 /* Reget the first character. */
2185 /* Localized digits, decimal points, and thousands
2187 wint_t wcdigits
[12];
2189 /* First get decimal equivalent to check if we read it
2191 wcdigits
[11] = __towctrans (L
'.', map
);
2193 /* If we have not read any character or have just read
2194 locale decimal point which matches the decimal point
2195 for localized FP numbers, then we may have localized
2196 digits. Note, we test GOT_DOT above. */
2197 #ifdef COMPILE_WSCANF
2198 if (char_buffer_size (&charbuf
) == got_sign
2199 || (char_buffer_size (&charbuf
) == got_sign
+ 1
2200 && wcdigits
[11] == decimal
))
2202 char mbdigits
[12][MB_LEN_MAX
+ 1];
2205 memset (&state
, '\0', sizeof (state
));
2207 bool match_so_far
= char_buffer_size (&charbuf
) == got_sign
;
2208 size_t mblen
= __wcrtomb (mbdigits
[11], wcdigits
[11], &state
);
2209 if (mblen
!= (size_t) -1)
2211 mbdigits
[11][mblen
] = '\0';
2213 (char_buffer_size (&charbuf
) == strlen (decimal
) + got_sign
2214 && strcmp (decimal
, mbdigits
[11]) == 0);
2218 size_t decimal_len
= strlen (decimal
);
2219 /* This should always be the case but the data comes
2221 if (decimal_len
<= MB_LEN_MAX
)
2223 match_so_far
|= (char_buffer_size (&charbuf
)
2224 == decimal_len
+ got_sign
);
2225 memcpy (mbdigits
[11], decimal
, decimal_len
+ 1);
2228 match_so_far
= false;
2234 bool have_locthousands
= (flags
& GROUP
) != 0;
2236 /* Now get the digits and the thousands-sep equivalents. */
2237 for (int n
= 0; n
< 11; ++n
)
2240 wcdigits
[n
] = __towctrans (L
'0' + n
, map
);
2243 wcdigits
[10] = __towctrans (L
',', map
);
2244 have_locthousands
&= wcdigits
[10] != L
'\0';
2247 #ifndef COMPILE_WSCANF
2248 memset (&state
, '\0', sizeof (state
));
2250 size_t mblen
= __wcrtomb (mbdigits
[n
], wcdigits
[n
],
2252 if (mblen
== (size_t) -1)
2256 if (have_locthousands
)
2258 size_t thousands_len
= strlen (thousands
);
2259 if (thousands_len
<= MB_LEN_MAX
)
2260 memcpy (mbdigits
[10], thousands
,
2263 have_locthousands
= false;
2267 /* Ignore checking against localized digits. */
2271 mbdigits
[n
][mblen
] = '\0';
2275 /* Start checking against localized digits, if
2276 conversion is done correctly. */
2279 if (char_buffer_error (&charbuf
))
2281 __set_errno (ENOMEM
);
2285 if (got_e
&& charbuf
.current
[-1] == exp_char
2286 && (c
== L_('-') || c
== L_('+')))
2287 char_buffer_add (&charbuf
, c
);
2288 else if (char_buffer_size (&charbuf
) > got_sign
&& !got_e
2289 && (CHAR_T
) TOLOWER (c
) == exp_char
)
2291 char_buffer_add (&charbuf
, exp_char
);
2292 got_e
= got_dot
= 1;
2296 /* Check against localized digits, decimal point,
2297 and thousands separator. */
2299 for (n
= 0; n
< 12; ++n
)
2301 #ifdef COMPILE_WSCANF
2302 if (c
== wcdigits
[n
])
2305 char_buffer_add (&charbuf
, L_('0') + n
);
2306 else if (n
== 11 && !got_dot
)
2308 char_buffer_add (&charbuf
, decimal
);
2311 else if (n
== 10 && have_locthousands
2313 char_buffer_add (&charbuf
, thousands
);
2315 /* The last read character is not part
2316 of the number anymore. */
2322 const char *cmpp
= mbdigits
[n
];
2323 int avail
= width
> 0 ? width
: INT_MAX
;
2325 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
2326 if (*++cmpp
== '\0')
2330 if (avail
== 0 || inchar () == EOF
)
2340 char_buffer_add (&charbuf
, L_('0') + n
);
2341 else if (n
== 11 && !got_dot
)
2343 /* Add all the characters. */
2344 for (cmpp
= decimal
; *cmpp
!= '\0';
2346 char_buffer_add (&charbuf
,
2347 (unsigned char) *cmpp
);
2351 else if (n
== 10 && (flags
& GROUP
) != 0
2354 /* Add all the characters. */
2355 for (cmpp
= thousands
; *cmpp
!= '\0';
2357 char_buffer_add (&charbuf
,
2358 (unsigned char) *cmpp
);
2361 /* The last read character is not part
2362 of the number anymore. */
2368 /* We are pushing all read characters back. */
2369 if (cmpp
> mbdigits
[n
])
2372 while (--cmpp
> mbdigits
[n
])
2373 ungetc_not_eof ((unsigned char) *cmpp
, s
);
2374 c
= (unsigned char) *cmpp
;
2381 /* The last read character is not part
2382 of the number anymore. */
2388 if (width
== 0 || inchar () == EOF
)
2396 #ifndef COMPILE_WSCANF
2402 if (char_buffer_error (&charbuf
))
2404 __set_errno (ENOMEM
);
2409 /* Have we read any character? If we try to read a number
2410 in hexadecimal notation and we have read only the `0x'
2411 prefix this is an error. */
2412 if (__glibc_unlikely (char_buffer_size (&charbuf
) == got_sign
2413 || ((flags
& HEXA_FLOAT
)
2414 && (char_buffer_size (&charbuf
)
2419 /* Convert the number. */
2420 char_buffer_add (&charbuf
, L_('\0'));
2421 if (char_buffer_error (&charbuf
))
2423 __set_errno (ENOMEM
);
2427 #if __HAVE_FLOAT128_UNLIKE_LDBL
2428 if ((flags
& LONGDBL
) \
2429 && (mode_flags
& SCANF_LDBL_USES_FLOAT128
) != 0)
2431 _Float128 d
= __strtof128_internal
2432 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2433 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2434 *ARG (_Float128
*) = d
;
2438 if ((flags
& LONGDBL
) \
2439 && __glibc_likely ((mode_flags
& SCANF_LDBL_IS_DBL
) == 0))
2441 long double d
= __strtold_internal
2442 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2443 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2444 *ARG (long double *) = d
;
2446 else if (flags
& (LONG
| LONGDBL
))
2448 double d
= __strtod_internal
2449 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2450 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2451 *ARG (double *) = d
;
2455 float d
= __strtof_internal
2456 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2457 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2461 if (__glibc_unlikely (tw
== char_buffer_start (&charbuf
)))
2464 if (!(flags
& SUPPRESS
))
2468 case L_('['): /* Character class. */
2470 STRING_ARG (wstr
, wchar_t, 100);
2472 STRING_ARG (str
, char, 100);
2483 #ifdef COMPILE_WSCANF
2484 /* Find the beginning and the end of the scanlist. We are not
2485 creating a lookup table since it would have to be too large.
2486 Instead we search each time through the string. This is not
2487 a constant lookup time but who uses this feature deserves to
2489 tw
= (wchar_t *) f
; /* Marks the beginning. */
2494 while ((fc
= *f
++) != L
'\0' && fc
!= L
']');
2496 if (__glibc_unlikely (fc
== L
'\0'))
2498 wchar_t *twend
= (wchar_t *) f
- 1;
2500 /* Fill WP with byte flags indexed by character.
2501 We will use this flag map for matching input characters. */
2502 if (!scratch_buffer_set_array_size
2503 (&charbuf
.scratch
, UCHAR_MAX
+ 1, 1))
2508 memset (charbuf
.scratch
.data
, '\0', UCHAR_MAX
+ 1);
2511 if (fc
== ']' || fc
== '-')
2513 /* If ] or - appears before any char in the set, it is not
2514 the terminator or separator, but the first char in the
2516 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2520 while ((fc
= *f
++) != '\0' && fc
!= ']')
2521 if (fc
== '-' && *f
!= '\0' && *f
!= ']' && f
[-2] <= *f
)
2523 /* Add all characters from the one before the '-'
2524 up to (but not including) the next format char. */
2525 for (fc
= f
[-2]; fc
< *f
; ++fc
)
2526 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2529 /* Add the character to the flag map. */
2530 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2532 if (__glibc_unlikely (fc
== '\0'))
2538 size_t now
= read_in
;
2539 #ifdef COMPILE_WSCANF
2540 if (__glibc_unlikely (inchar () == WEOF
))
2547 /* Test whether it's in the scanlist. */
2549 while (runp
< twend
)
2551 if (runp
[0] == L
'-' && runp
[1] != '\0'
2552 && runp
+ 1 != twend
2554 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2556 /* Match against all characters in between the
2557 first and last character of the sequence. */
2560 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2561 if ((wint_t) wc
== c
)
2564 if (wc
<= runp
[1] && !not_in
)
2566 if (wc
<= runp
[1] && not_in
)
2568 /* The current character is not in the
2578 if ((wint_t) *runp
== c
&& !not_in
)
2580 if ((wint_t) *runp
== c
&& not_in
)
2590 if (runp
== twend
&& !not_in
)
2596 if (!(flags
& SUPPRESS
))
2600 if ((flags
& MALLOC
)
2601 && wstr
== (wchar_t *) *strptr
+ strsize
)
2603 /* Enlarge the buffer. */
2604 wstr
= (wchar_t *) realloc (*strptr
,
2606 * sizeof (wchar_t));
2609 /* Can't allocate that much. Last-ditch
2612 realloc (*strptr
, (strsize
+ 1)
2613 * sizeof (wchar_t));
2616 if (flags
& POSIX_MALLOC
)
2621 /* We lose. Oh well. Terminate the string
2622 and stop converting, so at least we don't
2624 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2631 *strptr
= (char *) wstr
;
2638 *strptr
= (char *) wstr
;
2645 while ((width
< 0 || --width
> 0) && inchar () != WEOF
);
2648 char buf
[MB_LEN_MAX
];
2652 if (__glibc_unlikely (inchar () == EOF
))
2655 memset (&cstate
, '\0', sizeof (cstate
));
2659 if (((char *) charbuf
.scratch
.data
)[c
] == not_in
)
2661 ungetc_not_eof (c
, s
);
2666 if (!(flags
& SUPPRESS
))
2670 /* Convert it into a wide character. */
2672 n
= __mbrtowc (wstr
, buf
, 1, &cstate
);
2674 if (n
== (size_t) -2)
2676 /* Possibly correct character, just not enough
2679 assert (cnt
< MB_LEN_MAX
);
2685 if ((flags
& MALLOC
)
2686 && wstr
== (wchar_t *) *strptr
+ strsize
)
2688 /* Enlarge the buffer. */
2689 wstr
= (wchar_t *) realloc (*strptr
,
2691 * sizeof (wchar_t)));
2694 /* Can't allocate that much. Last-ditch
2697 realloc (*strptr
, ((strsize
+ 1)
2698 * sizeof (wchar_t)));
2701 if (flags
& POSIX_MALLOC
)
2706 /* We lose. Oh well. Terminate the
2707 string and stop converting,
2708 so at least we don't skip any input. */
2709 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2716 *strptr
= (char *) wstr
;
2723 *strptr
= (char *) wstr
;
2730 if (width
>= 0 && --width
<= 0)
2733 while (inchar () != EOF
);
2735 if (__glibc_unlikely (cnt
!= 0))
2736 /* We stopped in the middle of recognizing another
2737 character. That's a problem. */
2741 if (__glibc_unlikely (now
== read_in
))
2742 /* We haven't succesfully read any character. */
2745 if (!(flags
& SUPPRESS
))
2749 if ((flags
& MALLOC
)
2750 && wstr
- (wchar_t *) *strptr
!= strsize
)
2752 wchar_t *cp
= (wchar_t *)
2753 realloc (*strptr
, ((wstr
- (wchar_t *) *strptr
)
2754 * sizeof (wchar_t)));
2756 *strptr
= (char *) cp
;
2765 size_t now
= read_in
;
2767 if (__glibc_unlikely (inchar () == EOF
))
2770 #ifdef COMPILE_WSCANF
2772 memset (&state
, '\0', sizeof (state
));
2779 /* Test whether it's in the scanlist. */
2781 while (runp
< twend
)
2783 if (runp
[0] == L
'-' && runp
[1] != '\0'
2784 && runp
+ 1 != twend
2786 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2788 /* Match against all characters in between the
2789 first and last character of the sequence. */
2792 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2793 if ((wint_t) wc
== c
)
2796 if (wc
<= runp
[1] && !not_in
)
2798 if (wc
<= runp
[1] && not_in
)
2800 /* The current character is not in the
2810 if ((wint_t) *runp
== c
&& !not_in
)
2812 if ((wint_t) *runp
== c
&& not_in
)
2822 if (runp
== twend
&& !not_in
)
2828 if (!(flags
& SUPPRESS
))
2830 if ((flags
& MALLOC
)
2831 && *strptr
+ strsize
- str
<= MB_LEN_MAX
)
2833 /* Enlarge the buffer. */
2834 size_t strleng
= str
- *strptr
;
2837 newstr
= (char *) realloc (*strptr
, 2 * strsize
);
2840 /* Can't allocate that much. Last-ditch
2842 newstr
= (char *) realloc (*strptr
,
2843 strleng
+ MB_LEN_MAX
);
2846 if (flags
& POSIX_MALLOC
)
2851 /* We lose. Oh well. Terminate the string
2852 and stop converting, so at least we don't
2854 ((char *) (*strptr
))[strleng
] = '\0';
2862 str
= newstr
+ strleng
;
2863 strsize
= strleng
+ MB_LEN_MAX
;
2869 str
= newstr
+ strleng
;
2875 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
2876 if (__glibc_unlikely (n
== (size_t) -1))
2879 assert (n
<= MB_LEN_MAX
);
2882 while ((width
< 0 || --width
> 0) && inchar () != WEOF
);
2887 if (((char *) charbuf
.scratch
.data
)[c
] == not_in
)
2889 ungetc_not_eof (c
, s
);
2894 if (!(flags
& SUPPRESS
))
2897 if ((flags
& MALLOC
)
2898 && (char *) str
== *strptr
+ strsize
)
2900 /* Enlarge the buffer. */
2901 size_t newsize
= 2 * strsize
;
2904 str
= (char *) realloc (*strptr
, newsize
);
2907 /* Can't allocate that much. Last-ditch
2909 if (newsize
> strsize
+ 1)
2911 newsize
= strsize
+ 1;
2914 if (flags
& POSIX_MALLOC
)
2919 /* We lose. Oh well. Terminate the
2920 string and stop converting,
2921 so at least we don't skip any input. */
2922 ((char *) (*strptr
))[strsize
- 1] = '\0';
2929 *strptr
= (char *) str
;
2936 while ((width
< 0 || --width
> 0) && inchar () != EOF
);
2939 if (__glibc_unlikely (now
== read_in
))
2940 /* We haven't succesfully read any character. */
2943 if (!(flags
& SUPPRESS
))
2945 #ifdef COMPILE_WSCANF
2946 /* We have to emit the code to get into the initial
2948 char buf
[MB_LEN_MAX
];
2949 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
2950 if (n
> 0 && (flags
& MALLOC
)
2951 && str
+ n
>= *strptr
+ strsize
)
2953 /* Enlarge the buffer. */
2954 size_t strleng
= str
- *strptr
;
2957 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
2960 if (flags
& POSIX_MALLOC
)
2965 /* We lose. Oh well. Terminate the string
2966 and stop converting, so at least we don't
2968 ((char *) (*strptr
))[strleng
] = '\0';
2976 str
= newstr
+ strleng
;
2977 strsize
= strleng
+ n
+ 1;
2981 str
= __mempcpy (str
, buf
, n
);
2985 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
2987 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
2998 case L_('p'): /* Generic pointer. */
3000 /* A PTR must be the same size as a `long int'. */
3001 flags
&= ~(SHORT
|LONGDBL
);
3004 flags
|= READ_POINTER
;
3008 /* If this is an unknown format character punt. */
3013 /* The last thing we saw int the format string was a white space.
3014 Consume the last white spaces. */
3019 while (ISSPACE (c
));
3024 /* Unlock stream. */
3027 scratch_buffer_free (&charbuf
.scratch
);
3029 if (__glibc_unlikely (done
== EOF
))
3031 if (__glibc_unlikely (ptrs_to_free
!= NULL
))
3033 struct ptrs_to_free
*p
= ptrs_to_free
;
3036 for (size_t cnt
= 0; cnt
< p
->count
; ++cnt
)
3038 free (*p
->ptrs
[cnt
]);
3039 *p
->ptrs
[cnt
] = NULL
;
3046 else if (__glibc_unlikely (strptr
!= NULL
))