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_('b'): /* Binary integer. */
1388 case L_('u'): /* Unsigned decimal integer. */
1392 case L_('d'): /* Signed decimal integer. */
1394 flags
|= NUMBER_SIGNED
;
1397 case L_('i'): /* Generic number. */
1399 flags
|= NUMBER_SIGNED
;
1403 if (__glibc_unlikely (c
== EOF
))
1406 /* Check for a sign. */
1407 if (c
== L_('-') || c
== L_('+'))
1409 char_buffer_add (&charbuf
, c
);
1415 /* Look for a leading indication of base. */
1416 if (width
!= 0 && c
== L_('0'))
1421 char_buffer_add (&charbuf
, c
);
1424 if (width
!= 0 && TOLOWER (c
) == L_('x'))
1436 && TOLOWER (c
) == L_('b')
1438 || ((mode_flags
& SCANF_ISOC23_BIN_CST
) != 0
1453 if (base
== 10 && __builtin_expect ((flags
& I18N
) != 0, 0))
1458 enum { num_digits_len
= 10 };
1459 #ifdef COMPILE_WSCANF
1460 const wchar_t *wcdigits
[num_digits_len
];
1462 const char *mbdigits
[num_digits_len
];
1464 CHAR_T
*digits_extended
[num_digits_len
] = { NULL
};
1466 /* "to_inpunct" is a map from ASCII digits to their
1467 equivalent in locale. This is defined for locales
1468 which use an extra digits set. */
1469 wctrans_t map
= __wctrans ("to_inpunct");
1473 #ifdef COMPILE_WSCANF
1474 to_level
= _NL_CURRENT_WORD (LC_CTYPE
,
1475 _NL_CTYPE_INDIGITS_WC_LEN
) - 1;
1477 to_level
= (uint32_t) curctype
->values
[_NL_ITEM_INDEX (_NL_CTYPE_INDIGITS_MB_LEN
)].word
- 1;
1480 /* Get the alternative digit forms if there are any. */
1481 if (__glibc_unlikely (map
!= NULL
))
1483 /* Adding new level for extra digits set in locale file. */
1486 for (n
= 0; n
< num_digits_len
; ++n
)
1488 #ifdef COMPILE_WSCANF
1489 wcdigits
[n
] = (const wchar_t *)
1490 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1492 wchar_t *wc_extended
= (wchar_t *)
1493 malloc ((to_level
+ 2) * sizeof (wchar_t));
1494 if (wc_extended
== NULL
)
1497 goto digits_extended_fail
;
1499 __wmemcpy (wc_extended
, wcdigits
[n
], to_level
);
1500 wc_extended
[to_level
] = __towctrans (L
'0' + n
, map
);
1501 wc_extended
[to_level
+ 1] = '\0';
1502 digits_extended
[n
] = wc_extended
;
1505 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1507 /* Get the equivalent wide char in map. */
1508 wint_t extra_wcdigit
= __towctrans (L
'0' + n
, map
);
1510 /* Convert it to multibyte representation. */
1512 memset (&state
, '\0', sizeof (state
));
1514 char extra_mbdigit
[MB_LEN_MAX
];
1516 = __wcrtomb (extra_mbdigit
, extra_wcdigit
, &state
);
1518 if (mblen
== (size_t) -1)
1520 /* Ignore this new level. */
1525 /* Calculate the length of mbdigits[n]. */
1526 const char *last_char
= mbdigits
[n
];
1527 for (level
= 0; level
< to_level
; ++level
)
1528 last_char
= strchr (last_char
, '\0') + 1;
1530 size_t mbdigits_len
= last_char
- mbdigits
[n
];
1532 /* Allocate memory for extended multibyte digit. */
1533 char *mb_extended
= malloc (mbdigits_len
+ mblen
+ 1);
1534 if (mb_extended
== NULL
)
1537 goto digits_extended_fail
;
1540 /* And get the mbdigits + extra_digit string. */
1541 *(char *) __mempcpy (__mempcpy (mb_extended
, mbdigits
[n
],
1543 extra_mbdigit
, mblen
) = '\0';
1544 digits_extended
[n
] = mb_extended
;
1549 /* Read the number into workspace. */
1550 while (c
!= EOF
&& width
!= 0)
1552 /* In this round we get the pointer to the digit strings
1553 and also perform the first round of comparisons. */
1554 for (n
= 0; n
< num_digits_len
; ++n
)
1556 /* Get the string for the digits with value N. */
1557 #ifdef COMPILE_WSCANF
1559 /* wcdigits_extended[] is fully set in the loop
1560 above, but the test for "map != NULL" is done
1561 inside the loop here and outside the loop there. */
1562 DIAG_PUSH_NEEDS_COMMENT
;
1563 DIAG_IGNORE_NEEDS_COMMENT (4.7, "-Wmaybe-uninitialized");
1565 if (__glibc_unlikely (map
!= NULL
))
1566 wcdigits
[n
] = digits_extended
[n
];
1568 wcdigits
[n
] = (const wchar_t *)
1569 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1570 wcdigits
[n
] += from_level
;
1572 DIAG_POP_NEEDS_COMMENT
;
1574 if (c
== (wint_t) *wcdigits
[n
])
1576 to_level
= from_level
;
1580 /* Advance the pointer to the next string. */
1584 int avail
= width
> 0 ? width
: INT_MAX
;
1586 if (__glibc_unlikely (map
!= NULL
))
1587 mbdigits
[n
] = digits_extended
[n
];
1590 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1592 for (level
= 0; level
< from_level
; level
++)
1593 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1596 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1598 if (*++cmpp
== '\0')
1602 if (avail
== 0 || inchar () == EOF
)
1612 to_level
= from_level
;
1616 /* We are pushing all read characters back. */
1617 if (cmpp
> mbdigits
[n
])
1620 while (--cmpp
> mbdigits
[n
])
1621 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1622 c
= (unsigned char) *cmpp
;
1625 /* Advance the pointer to the next string. */
1626 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1630 if (n
== num_digits_len
)
1632 /* Have not yet found the digit. */
1633 for (level
= from_level
+ 1; level
<= to_level
; ++level
)
1635 /* Search all ten digits of this level. */
1636 for (n
= 0; n
< num_digits_len
; ++n
)
1638 #ifdef COMPILE_WSCANF
1639 if (c
== (wint_t) *wcdigits
[n
])
1642 /* Advance the pointer to the next string. */
1646 int avail
= width
> 0 ? width
: INT_MAX
;
1649 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1651 if (*++cmpp
== '\0')
1655 if (avail
== 0 || inchar () == EOF
)
1668 /* We are pushing all read characters back. */
1669 if (cmpp
> mbdigits
[n
])
1672 while (--cmpp
> mbdigits
[n
])
1673 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1674 c
= (unsigned char) *cmpp
;
1677 /* Advance the pointer to the next string. */
1678 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1692 if (n
< num_digits_len
)
1694 else if (flags
& GROUP
)
1696 /* Try matching against the thousands separator. */
1697 #ifdef COMPILE_WSCANF
1701 const char *cmpp
= thousands
;
1702 int avail
= width
> 0 ? width
: INT_MAX
;
1704 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1706 char_buffer_add (&charbuf
, c
);
1707 if (*++cmpp
== '\0')
1711 if (avail
== 0 || inchar () == EOF
)
1717 if (char_buffer_error (&charbuf
))
1719 __set_errno (ENOMEM
);
1726 /* We are pushing all read characters back. */
1727 if (cmpp
> thousands
)
1729 charbuf
.current
-= cmpp
- thousands
;
1731 while (--cmpp
> thousands
)
1732 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1733 c
= (unsigned char) *cmpp
;
1741 /* The last thousands character will be added back by
1742 the char_buffer_add below. */
1749 char_buffer_add (&charbuf
, c
);
1756 digits_extended_fail
:
1757 for (n
= 0; n
< num_digits_len
; n
++)
1758 free (digits_extended
[n
]);
1764 /* Read the number into workspace. */
1765 while (c
!= EOF
&& width
!= 0)
1772 else if (!ISDIGIT (c
) || (int) (c
- L_('0')) >= base
)
1774 if (base
== 10 && (flags
& GROUP
))
1776 /* Try matching against the thousands separator. */
1777 #ifdef COMPILE_WSCANF
1781 const char *cmpp
= thousands
;
1782 int avail
= width
> 0 ? width
: INT_MAX
;
1784 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1786 char_buffer_add (&charbuf
, c
);
1787 if (*++cmpp
== '\0')
1791 if (avail
== 0 || inchar () == EOF
)
1797 if (char_buffer_error (&charbuf
))
1799 __set_errno (ENOMEM
);
1806 /* We are pushing all read characters back. */
1807 if (cmpp
> thousands
)
1809 charbuf
.current
-= cmpp
- thousands
;
1811 while (--cmpp
> thousands
)
1812 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1813 c
= (unsigned char) *cmpp
;
1821 /* The last thousands character will be added back by
1822 the char_buffer_add below. */
1829 char_buffer_add (&charbuf
, c
);
1836 if (char_buffer_error (&charbuf
))
1838 __set_errno (ENOMEM
);
1843 if (char_buffer_size (&charbuf
) == 0
1844 || (char_buffer_size (&charbuf
) == 1
1845 && (char_buffer_start (&charbuf
)[0] == L_('+')
1846 || char_buffer_start (&charbuf
)[0] == L_('-'))))
1848 /* There was no number. If we are supposed to read a pointer
1849 we must recognize "(nil)" as well. */
1850 if (__builtin_expect (char_buffer_size (&charbuf
) == 0
1851 && (flags
& READ_POINTER
)
1852 && (width
< 0 || width
>= 5)
1854 && TOLOWER (inchar ()) == L_('n')
1855 && TOLOWER (inchar ()) == L_('i')
1856 && TOLOWER (inchar ()) == L_('l')
1857 && inchar () == L_(')'), 1))
1858 /* We must produce the value of a NULL pointer. A single
1859 '0' digit is enough. */
1860 char_buffer_add (&charbuf
, L_('0'));
1863 /* The last read character is not part of the number
1871 /* The just read character is not part of the number anymore. */
1874 /* Convert the number. */
1875 char_buffer_add (&charbuf
, L_('\0'));
1876 if (char_buffer_error (&charbuf
))
1878 __set_errno (ENOMEM
);
1882 if (need_longlong
&& (flags
& LONGDBL
))
1884 if (flags
& NUMBER_SIGNED
)
1885 num
.q
= __strtoll_internal
1886 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1888 num
.uq
= __strtoull_internal
1889 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1893 if (flags
& NUMBER_SIGNED
)
1894 num
.l
= __strtol_internal
1895 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1897 num
.ul
= __strtoul_internal
1898 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1900 if (__glibc_unlikely (char_buffer_start (&charbuf
) == tw
))
1903 if (!(flags
& SUPPRESS
))
1905 if (flags
& NUMBER_SIGNED
)
1907 if (need_longlong
&& (flags
& LONGDBL
))
1908 *ARG (LONGLONG
int *) = num
.q
;
1909 else if (need_long
&& (flags
& LONG
))
1910 *ARG (long int *) = num
.l
;
1911 else if (flags
& SHORT
)
1912 *ARG (short int *) = (short int) num
.l
;
1913 else if (!(flags
& CHAR
))
1914 *ARG (int *) = (int) num
.l
;
1916 *ARG (signed char *) = (signed char) num
.ul
;
1920 if (need_longlong
&& (flags
& LONGDBL
))
1921 *ARG (unsigned LONGLONG
int *) = num
.uq
;
1922 else if (need_long
&& (flags
& LONG
))
1923 *ARG (unsigned long int *) = num
.ul
;
1924 else if (flags
& SHORT
)
1925 *ARG (unsigned short int *)
1926 = (unsigned short int) num
.ul
;
1927 else if (!(flags
& CHAR
))
1928 *ARG (unsigned int *) = (unsigned int) num
.ul
;
1930 *ARG (unsigned char *) = (unsigned char) num
.ul
;
1936 case L_('e'): /* Floating-point numbers. */
1947 if (__glibc_unlikely (c
== EOF
))
1950 got_digit
= got_dot
= got_e
= got_sign
= 0;
1952 /* Check for a sign. */
1953 if (c
== L_('-') || c
== L_('+'))
1956 char_buffer_add (&charbuf
, c
);
1957 if (__glibc_unlikely (width
== 0 || inchar () == EOF
))
1958 /* EOF is only an input error before we read any chars. */
1964 /* Take care for the special arguments "nan" and "inf". */
1965 if (TOLOWER (c
) == L_('n'))
1968 char_buffer_add (&charbuf
, c
);
1969 if (__builtin_expect (width
== 0
1971 || TOLOWER (c
) != L_('a'), 0))
1975 char_buffer_add (&charbuf
, c
);
1976 if (__builtin_expect (width
== 0
1978 || TOLOWER (c
) != L_('n'), 0))
1982 char_buffer_add (&charbuf
, c
);
1986 else if (TOLOWER (c
) == L_('i'))
1988 /* Maybe "inf" or "infinity". */
1989 char_buffer_add (&charbuf
, c
);
1990 if (__builtin_expect (width
== 0
1992 || TOLOWER (c
) != L_('n'), 0))
1996 char_buffer_add (&charbuf
, c
);
1997 if (__builtin_expect (width
== 0
1999 || TOLOWER (c
) != L_('f'), 0))
2003 char_buffer_add (&charbuf
, c
);
2004 /* It is as least "inf". */
2005 if (width
!= 0 && inchar () != EOF
)
2007 if (TOLOWER (c
) == L_('i'))
2011 /* Now we have to read the rest as well. */
2012 char_buffer_add (&charbuf
, c
);
2013 if (__builtin_expect (width
== 0
2015 || TOLOWER (c
) != L_('n'), 0))
2019 char_buffer_add (&charbuf
, c
);
2020 if (__builtin_expect (width
== 0
2022 || TOLOWER (c
) != L_('i'), 0))
2026 char_buffer_add (&charbuf
, c
);
2027 if (__builtin_expect (width
== 0
2029 || TOLOWER (c
) != L_('t'), 0))
2033 char_buffer_add (&charbuf
, c
);
2034 if (__builtin_expect (width
== 0
2036 || TOLOWER (c
) != L_('y'), 0))
2040 char_buffer_add (&charbuf
, c
);
2050 if (width
!= 0 && c
== L_('0'))
2052 char_buffer_add (&charbuf
, c
);
2056 if (width
!= 0 && TOLOWER (c
) == L_('x'))
2058 /* It is a number in hexadecimal format. */
2059 char_buffer_add (&charbuf
, c
);
2061 flags
|= HEXA_FLOAT
;
2064 /* Grouping is not allowed. */
2076 if (char_buffer_error (&charbuf
))
2078 __set_errno (ENOMEM
);
2084 char_buffer_add (&charbuf
, c
);
2087 else if (!got_e
&& (flags
& HEXA_FLOAT
) && ISXDIGIT (c
))
2089 char_buffer_add (&charbuf
, c
);
2092 else if (got_e
&& charbuf
.current
[-1] == exp_char
2093 && (c
== L_('-') || c
== L_('+')))
2094 char_buffer_add (&charbuf
, c
);
2095 else if (got_digit
&& !got_e
2096 && (CHAR_T
) TOLOWER (c
) == exp_char
)
2098 char_buffer_add (&charbuf
, exp_char
);
2099 got_e
= got_dot
= 1;
2103 #ifdef COMPILE_WSCANF
2104 if (! got_dot
&& c
== decimal
)
2106 char_buffer_add (&charbuf
, c
);
2109 else if ((flags
& GROUP
) != 0 && ! got_dot
&& c
== thousands
)
2110 char_buffer_add (&charbuf
, c
);
2113 /* The last read character is not part of the number
2119 const char *cmpp
= decimal
;
2120 int avail
= width
> 0 ? width
: INT_MAX
;
2124 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
2125 if (*++cmpp
== '\0')
2129 if (avail
== 0 || inchar () == EOF
)
2137 /* Add all the characters. */
2138 for (cmpp
= decimal
; *cmpp
!= '\0'; ++cmpp
)
2139 char_buffer_add (&charbuf
, (unsigned char) *cmpp
);
2146 /* Figure out whether it is a thousands separator.
2147 There is one problem: we possibly read more than
2148 one character. We cannot push them back but since
2149 we know that parts of the `decimal' string matched,
2150 we can compare against it. */
2151 const char *cmp2p
= thousands
;
2153 if ((flags
& GROUP
) != 0 && ! got_dot
)
2155 while (cmp2p
- thousands
< cmpp
- decimal
2156 && *cmp2p
== decimal
[cmp2p
- thousands
])
2158 if (cmp2p
- thousands
== cmpp
- decimal
)
2160 while ((unsigned char) *cmp2p
== c
&& avail
>= 0)
2161 if (*++cmp2p
== '\0')
2165 if (avail
== 0 || inchar () == EOF
)
2172 if (cmp2p
!= NULL
&& *cmp2p
== '\0')
2174 /* Add all the characters. */
2175 for (cmpp
= thousands
; *cmpp
!= '\0'; ++cmpp
)
2176 char_buffer_add (&charbuf
, (unsigned char) *cmpp
);
2182 /* The last read character is not part of the number
2191 if (width
== 0 || inchar () == EOF
)
2198 if (char_buffer_error (&charbuf
))
2200 __set_errno (ENOMEM
);
2206 if (__builtin_expect ((flags
& I18N
) != 0, 0)
2207 /* Hexadecimal floats make no sense, fixing localized
2208 digits with ASCII letters. */
2209 && !(flags
& HEXA_FLOAT
)
2210 /* Minimum requirement. */
2211 && (char_buffer_size (&charbuf
) == got_sign
|| got_dot
)
2212 && (map
= __wctrans ("to_inpunct")) != NULL
)
2214 /* Reget the first character. */
2217 /* Localized digits, decimal points, and thousands
2219 wint_t wcdigits
[12];
2221 /* First get decimal equivalent to check if we read it
2223 wcdigits
[11] = __towctrans (L
'.', map
);
2225 /* If we have not read any character or have just read
2226 locale decimal point which matches the decimal point
2227 for localized FP numbers, then we may have localized
2228 digits. Note, we test GOT_DOT above. */
2229 #ifdef COMPILE_WSCANF
2230 if (char_buffer_size (&charbuf
) == got_sign
2231 || (char_buffer_size (&charbuf
) == got_sign
+ 1
2232 && wcdigits
[11] == decimal
))
2234 char mbdigits
[12][MB_LEN_MAX
+ 1];
2237 memset (&state
, '\0', sizeof (state
));
2239 bool match_so_far
= char_buffer_size (&charbuf
) == got_sign
;
2240 size_t mblen
= __wcrtomb (mbdigits
[11], wcdigits
[11], &state
);
2241 if (mblen
!= (size_t) -1)
2243 mbdigits
[11][mblen
] = '\0';
2245 (char_buffer_size (&charbuf
) == strlen (decimal
) + got_sign
2246 && strcmp (decimal
, mbdigits
[11]) == 0);
2250 size_t decimal_len
= strlen (decimal
);
2251 /* This should always be the case but the data comes
2253 if (decimal_len
<= MB_LEN_MAX
)
2255 match_so_far
|= (char_buffer_size (&charbuf
)
2256 == decimal_len
+ got_sign
);
2257 memcpy (mbdigits
[11], decimal
, decimal_len
+ 1);
2260 match_so_far
= false;
2266 bool have_locthousands
= (flags
& GROUP
) != 0;
2268 /* Now get the digits and the thousands-sep equivalents. */
2269 for (int n
= 0; n
< 11; ++n
)
2272 wcdigits
[n
] = __towctrans (L
'0' + n
, map
);
2275 wcdigits
[10] = __towctrans (L
',', map
);
2276 have_locthousands
&= wcdigits
[10] != L
'\0';
2279 #ifndef COMPILE_WSCANF
2280 memset (&state
, '\0', sizeof (state
));
2282 size_t mblen
= __wcrtomb (mbdigits
[n
], wcdigits
[n
],
2284 if (mblen
== (size_t) -1)
2288 if (have_locthousands
)
2290 size_t thousands_len
= strlen (thousands
);
2291 if (thousands_len
<= MB_LEN_MAX
)
2292 memcpy (mbdigits
[10], thousands
,
2295 have_locthousands
= false;
2299 /* Ignore checking against localized digits. */
2303 mbdigits
[n
][mblen
] = '\0';
2307 /* Start checking against localized digits, if
2308 conversion is done correctly. */
2311 if (char_buffer_error (&charbuf
))
2313 __set_errno (ENOMEM
);
2317 if (got_e
&& charbuf
.current
[-1] == exp_char
2318 && (c
== L_('-') || c
== L_('+')))
2319 char_buffer_add (&charbuf
, c
);
2320 else if (char_buffer_size (&charbuf
) > got_sign
&& !got_e
2321 && (CHAR_T
) TOLOWER (c
) == exp_char
)
2323 char_buffer_add (&charbuf
, exp_char
);
2324 got_e
= got_dot
= 1;
2328 /* Check against localized digits, decimal point,
2329 and thousands separator. */
2331 for (n
= 0; n
< 12; ++n
)
2333 #ifdef COMPILE_WSCANF
2334 if (c
== wcdigits
[n
])
2337 char_buffer_add (&charbuf
, L_('0') + n
);
2338 else if (n
== 11 && !got_dot
)
2340 char_buffer_add (&charbuf
, decimal
);
2343 else if (n
== 10 && have_locthousands
2345 char_buffer_add (&charbuf
, thousands
);
2347 /* The last read character is not part
2348 of the number anymore. */
2354 const char *cmpp
= mbdigits
[n
];
2355 int avail
= width
> 0 ? width
: INT_MAX
;
2357 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
2358 if (*++cmpp
== '\0')
2362 if (avail
== 0 || inchar () == EOF
)
2372 char_buffer_add (&charbuf
, L_('0') + n
);
2373 else if (n
== 11 && !got_dot
)
2375 /* Add all the characters. */
2376 for (cmpp
= decimal
; *cmpp
!= '\0';
2378 char_buffer_add (&charbuf
,
2379 (unsigned char) *cmpp
);
2383 else if (n
== 10 && (flags
& GROUP
) != 0
2386 /* Add all the characters. */
2387 for (cmpp
= thousands
; *cmpp
!= '\0';
2389 char_buffer_add (&charbuf
,
2390 (unsigned char) *cmpp
);
2393 /* The last read character is not part
2394 of the number anymore. */
2400 /* We are pushing all read characters back. */
2401 if (cmpp
> mbdigits
[n
])
2404 while (--cmpp
> mbdigits
[n
])
2405 ungetc_not_eof ((unsigned char) *cmpp
, s
);
2406 c
= (unsigned char) *cmpp
;
2413 /* The last read character is not part
2414 of the number anymore. */
2420 if (width
== 0 || inchar () == EOF
)
2428 #ifndef COMPILE_WSCANF
2434 if (char_buffer_error (&charbuf
))
2436 __set_errno (ENOMEM
);
2441 /* Have we read any character? If we try to read a number
2442 in hexadecimal notation and we have read only the `0x'
2443 prefix this is an error. */
2444 if (__glibc_unlikely (char_buffer_size (&charbuf
) == got_sign
2445 || ((flags
& HEXA_FLOAT
)
2446 && (char_buffer_size (&charbuf
)
2451 /* Convert the number. */
2452 char_buffer_add (&charbuf
, L_('\0'));
2453 if (char_buffer_error (&charbuf
))
2455 __set_errno (ENOMEM
);
2459 #if __HAVE_FLOAT128_UNLIKE_LDBL
2460 if ((flags
& LONGDBL
) \
2461 && (mode_flags
& SCANF_LDBL_USES_FLOAT128
) != 0)
2463 _Float128 d
= __strtof128_internal
2464 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2465 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2466 *ARG (_Float128
*) = d
;
2470 if ((flags
& LONGDBL
) \
2471 && __glibc_likely ((mode_flags
& SCANF_LDBL_IS_DBL
) == 0))
2473 long double d
= __strtold_internal
2474 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2475 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2476 *ARG (long double *) = d
;
2478 else if (flags
& (LONG
| LONGDBL
))
2480 double d
= __strtod_internal
2481 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2482 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2483 *ARG (double *) = d
;
2487 float d
= __strtof_internal
2488 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2489 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2493 if (__glibc_unlikely (tw
== char_buffer_start (&charbuf
)))
2496 if (!(flags
& SUPPRESS
))
2500 case L_('['): /* Character class. */
2502 STRING_ARG (wstr
, wchar_t, 100);
2504 STRING_ARG (str
, char, 100);
2515 #ifdef COMPILE_WSCANF
2516 /* Find the beginning and the end of the scanlist. We are not
2517 creating a lookup table since it would have to be too large.
2518 Instead we search each time through the string. This is not
2519 a constant lookup time but who uses this feature deserves to
2521 tw
= (wchar_t *) f
; /* Marks the beginning. */
2526 while ((fc
= *f
++) != L
'\0' && fc
!= L
']');
2528 if (__glibc_unlikely (fc
== L
'\0'))
2530 wchar_t *twend
= (wchar_t *) f
- 1;
2532 /* Fill WP with byte flags indexed by character.
2533 We will use this flag map for matching input characters. */
2534 if (!scratch_buffer_set_array_size
2535 (&charbuf
.scratch
, UCHAR_MAX
+ 1, 1))
2540 memset (charbuf
.scratch
.data
, '\0', UCHAR_MAX
+ 1);
2543 if (fc
== ']' || fc
== '-')
2545 /* If ] or - appears before any char in the set, it is not
2546 the terminator or separator, but the first char in the
2548 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2552 while ((fc
= *f
++) != '\0' && fc
!= ']')
2553 if (fc
== '-' && *f
!= '\0' && *f
!= ']' && f
[-2] <= *f
)
2555 /* Add all characters from the one before the '-'
2556 up to (but not including) the next format char. */
2557 for (fc
= f
[-2]; fc
< *f
; ++fc
)
2558 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2561 /* Add the character to the flag map. */
2562 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2564 if (__glibc_unlikely (fc
== '\0'))
2570 size_t now
= read_in
;
2571 #ifdef COMPILE_WSCANF
2572 if (__glibc_unlikely (inchar () == WEOF
))
2579 /* Test whether it's in the scanlist. */
2581 while (runp
< twend
)
2583 if (runp
[0] == L
'-' && runp
[1] != '\0'
2584 && runp
+ 1 != twend
2586 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2588 /* Match against all characters in between the
2589 first and last character of the sequence. */
2592 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2593 if ((wint_t) wc
== c
)
2596 if (wc
<= runp
[1] && !not_in
)
2598 if (wc
<= runp
[1] && not_in
)
2600 /* The current character is not in the
2610 if ((wint_t) *runp
== c
&& !not_in
)
2612 if ((wint_t) *runp
== c
&& not_in
)
2622 if (runp
== twend
&& !not_in
)
2628 if (!(flags
& SUPPRESS
))
2632 if ((flags
& MALLOC
)
2633 && wstr
== (wchar_t *) *strptr
+ strsize
)
2635 /* Enlarge the buffer. */
2636 wstr
= (wchar_t *) realloc (*strptr
,
2638 * sizeof (wchar_t));
2641 /* Can't allocate that much. Last-ditch
2644 realloc (*strptr
, (strsize
+ 1)
2645 * sizeof (wchar_t));
2648 if (flags
& POSIX_MALLOC
)
2653 /* We lose. Oh well. Terminate the string
2654 and stop converting, so at least we don't
2656 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2663 *strptr
= (char *) wstr
;
2670 *strptr
= (char *) wstr
;
2677 while ((width
< 0 || --width
> 0) && inchar () != WEOF
);
2680 char buf
[MB_LEN_MAX
];
2684 if (__glibc_unlikely (inchar () == EOF
))
2687 memset (&cstate
, '\0', sizeof (cstate
));
2691 if (((char *) charbuf
.scratch
.data
)[c
] == not_in
)
2693 ungetc_not_eof (c
, s
);
2698 if (!(flags
& SUPPRESS
))
2702 /* Convert it into a wide character. */
2704 n
= __mbrtowc (wstr
, buf
, 1, &cstate
);
2706 if (n
== (size_t) -2)
2708 /* Possibly correct character, just not enough
2711 assert (cnt
< MB_LEN_MAX
);
2717 if ((flags
& MALLOC
)
2718 && wstr
== (wchar_t *) *strptr
+ strsize
)
2720 /* Enlarge the buffer. */
2721 wstr
= (wchar_t *) realloc (*strptr
,
2723 * sizeof (wchar_t)));
2726 /* Can't allocate that much. Last-ditch
2729 realloc (*strptr
, ((strsize
+ 1)
2730 * sizeof (wchar_t)));
2733 if (flags
& POSIX_MALLOC
)
2738 /* We lose. Oh well. Terminate the
2739 string and stop converting,
2740 so at least we don't skip any input. */
2741 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2748 *strptr
= (char *) wstr
;
2755 *strptr
= (char *) wstr
;
2762 if (width
>= 0 && --width
<= 0)
2765 while (inchar () != EOF
);
2767 if (__glibc_unlikely (cnt
!= 0))
2768 /* We stopped in the middle of recognizing another
2769 character. That's a problem. */
2773 if (__glibc_unlikely (now
== read_in
))
2774 /* We haven't successfully read any character. */
2777 if (!(flags
& SUPPRESS
))
2781 if ((flags
& MALLOC
)
2782 && wstr
- (wchar_t *) *strptr
!= strsize
)
2784 wchar_t *cp
= (wchar_t *)
2785 realloc (*strptr
, ((wstr
- (wchar_t *) *strptr
)
2786 * sizeof (wchar_t)));
2788 *strptr
= (char *) cp
;
2797 size_t now
= read_in
;
2799 if (__glibc_unlikely (inchar () == EOF
))
2802 #ifdef COMPILE_WSCANF
2804 memset (&state
, '\0', sizeof (state
));
2811 /* Test whether it's in the scanlist. */
2813 while (runp
< twend
)
2815 if (runp
[0] == L
'-' && runp
[1] != '\0'
2816 && runp
+ 1 != twend
2818 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2820 /* Match against all characters in between the
2821 first and last character of the sequence. */
2824 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2825 if ((wint_t) wc
== c
)
2828 if (wc
<= runp
[1] && !not_in
)
2830 if (wc
<= runp
[1] && not_in
)
2832 /* The current character is not in the
2842 if ((wint_t) *runp
== c
&& !not_in
)
2844 if ((wint_t) *runp
== c
&& not_in
)
2854 if (runp
== twend
&& !not_in
)
2860 if (!(flags
& SUPPRESS
))
2862 if ((flags
& MALLOC
)
2863 && *strptr
+ strsize
- str
<= MB_LEN_MAX
)
2865 /* Enlarge the buffer. */
2866 size_t strleng
= str
- *strptr
;
2869 newstr
= (char *) realloc (*strptr
, 2 * strsize
);
2872 /* Can't allocate that much. Last-ditch
2874 newstr
= (char *) realloc (*strptr
,
2875 strleng
+ MB_LEN_MAX
);
2878 if (flags
& POSIX_MALLOC
)
2883 /* We lose. Oh well. Terminate the string
2884 and stop converting, so at least we don't
2886 ((char *) (*strptr
))[strleng
] = '\0';
2894 str
= newstr
+ strleng
;
2895 strsize
= strleng
+ MB_LEN_MAX
;
2901 str
= newstr
+ strleng
;
2907 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
2908 if (__glibc_unlikely (n
== (size_t) -1))
2911 assert (n
<= MB_LEN_MAX
);
2914 while ((width
< 0 || --width
> 0) && inchar () != WEOF
);
2919 if (((char *) charbuf
.scratch
.data
)[c
] == not_in
)
2921 ungetc_not_eof (c
, s
);
2926 if (!(flags
& SUPPRESS
))
2929 if ((flags
& MALLOC
)
2930 && (char *) str
== *strptr
+ strsize
)
2932 /* Enlarge the buffer. */
2933 size_t newsize
= 2 * strsize
;
2936 str
= (char *) realloc (*strptr
, newsize
);
2939 /* Can't allocate that much. Last-ditch
2941 if (newsize
> strsize
+ 1)
2943 newsize
= strsize
+ 1;
2946 if (flags
& POSIX_MALLOC
)
2951 /* We lose. Oh well. Terminate the
2952 string and stop converting,
2953 so at least we don't skip any input. */
2954 ((char *) (*strptr
))[strsize
- 1] = '\0';
2961 *strptr
= (char *) str
;
2968 while ((width
< 0 || --width
> 0) && inchar () != EOF
);
2971 if (__glibc_unlikely (now
== read_in
))
2972 /* We haven't successfully read any character. */
2975 if (!(flags
& SUPPRESS
))
2977 #ifdef COMPILE_WSCANF
2978 /* We have to emit the code to get into the initial
2980 char buf
[MB_LEN_MAX
];
2981 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
2982 if (n
> 0 && (flags
& MALLOC
)
2983 && str
+ n
>= *strptr
+ strsize
)
2985 /* Enlarge the buffer. */
2986 size_t strleng
= str
- *strptr
;
2989 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
2992 if (flags
& POSIX_MALLOC
)
2997 /* We lose. Oh well. Terminate the string
2998 and stop converting, so at least we don't
3000 ((char *) (*strptr
))[strleng
] = '\0';
3008 str
= newstr
+ strleng
;
3009 strsize
= strleng
+ n
+ 1;
3013 str
= __mempcpy (str
, buf
, n
);
3017 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
3019 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
3030 case L_('p'): /* Generic pointer. */
3032 /* A PTR must be the same size as a `long int'. */
3033 flags
&= ~(SHORT
|LONGDBL
);
3036 flags
|= READ_POINTER
;
3040 /* If this is an unknown format character punt. */
3045 /* The last thing we saw int the format string was a white space.
3046 Consume the last white spaces. */
3051 while (ISSPACE (c
));
3056 /* Unlock stream. */
3059 scratch_buffer_free (&charbuf
.scratch
);
3061 if (__glibc_unlikely (done
== EOF
))
3063 if (__glibc_unlikely (ptrs_to_free
!= NULL
))
3065 struct ptrs_to_free
*p
= ptrs_to_free
;
3068 for (size_t cnt
= 0; cnt
< p
->count
; ++cnt
)
3070 free (*p
->ptrs
[cnt
]);
3071 *p
->ptrs
[cnt
] = NULL
;
3078 else if (__glibc_unlikely (strptr
!= NULL
))