1 /* Internal functions for the *scanf* implementation.
2 Copyright (C) 1991-2024 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. */
385 /* Extract the next argument, which is of type TYPE.
386 For a %N$... spec, this is the Nth argument from the beginning;
387 otherwise it is the next argument after the state now in ARG. */
389 # define ARG(type) (argpos == 0 ? va_arg (arg, type) \
390 : ({ unsigned int pos = argpos; \
392 __va_copy (arg, argptr); \
394 (void) va_arg (arg, void *); \
395 va_arg (arg, type); \
399 /* XXX Possible optimization. */
400 # define ARG(type) (argpos == 0 ? va_arg (arg, type) \
401 : ({ va_list arg = (va_list) argptr; \
402 arg = (va_list) ((char *) arg \
404 * __va_rounded_size (void *)); \
405 va_arg (arg, type); \
408 # define ARG(type) (argpos == 0 ? va_arg (arg, type) \
409 : ({ unsigned int pos = argpos; \
410 va_list arg = (va_list) argptr; \
412 (void) va_arg (arg, void *); \
413 va_arg (arg, type); \
418 #ifndef COMPILE_WSCANF
421 /* Non-ASCII, may be a multibyte. */
422 int len
= __mbrlen ((const char *) f
, strlen ((const char *) f
),
429 if (__glibc_unlikely (c
== EOF
))
433 ungetc_not_eof (c
, s
);
446 /* Remember to skip spaces. */
453 /* Read a character. */
456 /* Characters other than format specs must just match. */
457 if (__glibc_unlikely (c
== EOF
))
460 /* We saw white space char as the last character in the format
461 string. Now it's time to skip all leading white space. */
465 if (__glibc_unlikely (inchar () == EOF
))
470 if (__glibc_unlikely (c
!= fc
))
479 /* This is the start of the conversion string. */
482 /* Initialize state of modifiers. */
485 /* Prepare temporary buffer. */
486 char_buffer_rewind (&charbuf
);
488 /* Check for a positional parameter specification. */
491 argpos
= read_int (&f
);
496 /* Oops; that was actually the field width. */
503 /* Check for the assignment-suppressing, the number grouping flag,
504 and the signal to use the locale's digit representation. */
505 while (*f
== L_('*') || *f
== L_('\'') || *f
== L_('I'))
512 #ifdef COMPILE_WSCANF
513 if (thousands
!= L
'\0')
515 if (thousands
!= NULL
)
524 /* Find the maximum field width. */
527 width
= read_int (&f
);
532 /* Check for type modifiers. */
536 /* ints are short ints or chars. */
548 /* A double `l' is equivalent to an `L'. */
550 flags
|= LONGDBL
| LONG
;
553 /* ints are long ints. */
558 /* doubles are long doubles, and ints are long long ints. */
559 flags
|= LONGDBL
| LONG
;
562 /* The `a' is used as a flag only if followed by `s', `S' or
564 if (*f
!= L_('s') && *f
!= L_('S') && *f
!= L_('['))
569 /* In __isoc99_*scanf %as, %aS and %a[ extension is not
571 if (__glibc_likely ((mode_flags
& SCANF_ISOC99_A
) != 0))
576 /* String conversions (%s, %[) take a `char **'
577 arg and fill it in with a malloc'd pointer. */
581 flags
|= POSIX_MALLOC
;
589 if (need_longlong
&& sizeof (size_t) > sizeof (unsigned long int))
591 else if (sizeof (size_t) > sizeof (unsigned int))
595 if (need_longlong
&& sizeof (uintmax_t) > sizeof (unsigned long int))
597 else if (sizeof (uintmax_t) > sizeof (unsigned int))
601 if (need_longlong
&& sizeof (ptrdiff_t) > sizeof (long int))
603 else if (sizeof (ptrdiff_t) > sizeof (int))
616 bitwidth
= read_int (&f
);
621 bitwidth
= INT_FAST8_WIDTH
;
624 bitwidth
= INT_FAST16_WIDTH
;
627 bitwidth
= INT_FAST32_WIDTH
;
630 bitwidth
= INT_FAST64_WIDTH
;
644 flags
|= LONGDBL
| LONG
;
647 /* ISO C requires this error to be detected. */
648 __set_errno (EINVAL
);
654 /* Not a recognized modifier. Backup. */
659 /* End of the format string? */
660 if (__glibc_unlikely (*f
== L_('\0')))
663 /* Find the conversion specifier. */
665 if (skip_space
|| (fc
!= L_('[') && fc
!= L_('c')
666 && fc
!= L_('C') && fc
!= L_('n')))
668 /* Eat whitespace. */
669 int save_errno
= errno
;
672 /* We add the additional test for EOF here since otherwise
673 inchar will restore the old errno value which might be
674 EINTR but does not indicate an interrupt since nothing
675 was read at this time. */
676 if (__builtin_expect ((c
== EOF
|| inchar () == EOF
)
677 && errno
== EINTR
, 0))
680 __set_errno (save_errno
);
687 case L_('%'): /* Must match a literal '%'. */
689 if (__glibc_unlikely (c
== EOF
))
691 if (__glibc_unlikely (c
!= fc
))
693 ungetc_not_eof (c
, s
);
698 case L_('n'): /* Answer number of assignments done. */
699 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
700 with the 'n' conversion specifier. */
701 if (!(flags
& SUPPRESS
))
703 /* Don't count the read-ahead. */
704 if (need_longlong
&& (flags
& LONGDBL
))
705 *ARG (long long int *) = read_in
;
706 else if (need_long
&& (flags
& LONG
))
707 *ARG (long int *) = read_in
;
708 else if (flags
& SHORT
)
709 *ARG (short int *) = read_in
;
710 else if (!(flags
& CHAR
))
711 *ARG (int *) = read_in
;
713 *ARG (char *) = read_in
;
715 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
716 /* We have a severe problem here. The ISO C standard
717 contradicts itself in explaining the effect of the %n
718 format in `scanf'. While in ISO C:1990 and the ISO C
719 Amendment 1:1995 the result is described as
721 Execution of a %n directive does not effect the
722 assignment count returned at the completion of
723 execution of the f(w)scanf function.
725 in ISO C Corrigendum 1:1994 the following was added:
728 Add the following fourth example:
731 int d1, d2, n1, n2, i;
732 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
733 the value 123 is assigned to d1 and the value3 to n1.
734 Because %n can never get an input failure the value
735 of 3 is also assigned to n2. The value of d2 is not
736 affected. The value 3 is assigned to i.
738 We go for now with the historically correct code from ISO C,
739 i.e., we don't count the %n assignments. When it ever
740 should proof to be wrong just remove the #ifdef above. */
746 case L_('c'): /* Match characters. */
747 if ((flags
& LONG
) == 0)
752 #define STRING_ARG(Str, Type, Width) \
753 do if (!(flags & SUPPRESS)) \
755 if (flags & MALLOC) \
757 /* The string is to be stored in a malloc'd buffer. */ \
758 /* For %mS using char ** is actually wrong, but \
759 shouldn't make a difference on any arch glibc \
760 supports and would unnecessarily complicate \
762 strptr = ARG (char **); \
763 if (strptr == NULL) \
765 /* Allocate an initial buffer. */ \
767 *strptr = (char *) malloc (strsize * sizeof (Type)); \
768 Str = (Type *) *strptr; \
770 add_ptr_to_free (strptr); \
771 else if (flags & POSIX_MALLOC) \
778 Str = ARG (Type *); \
782 #ifdef COMPILE_WSCANF
783 STRING_ARG (str
, char, 100);
785 STRING_ARG (str
, char, (width
> 1024 ? 1024 : width
));
789 if (__glibc_unlikely (c
== EOF
))
792 #ifdef COMPILE_WSCANF
793 /* We have to convert the wide character(s) into multibyte
794 characters and store the result. */
795 memset (&state
, '\0', sizeof (state
));
801 if (!(flags
& SUPPRESS
) && (flags
& POSIX_MALLOC
)
802 && *strptr
+ strsize
- str
<= MB_LEN_MAX
)
804 /* We have to enlarge the buffer if the `m' flag
806 size_t strleng
= str
- *strptr
;
809 newstr
= (char *) realloc (*strptr
, strsize
* 2);
812 /* Can't allocate that much. Last-ditch effort. */
813 newstr
= (char *) realloc (*strptr
,
814 strleng
+ MB_LEN_MAX
);
817 /* c can't have `a' flag, only `m'. */
824 str
= newstr
+ strleng
;
825 strsize
= strleng
+ MB_LEN_MAX
;
831 str
= newstr
+ strleng
;
836 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
837 if (__glibc_unlikely (n
== (size_t) -1))
838 /* No valid wide character. */
841 /* Increment the output pointer. Even if we don't
845 while (--width
> 0 && inchar () != EOF
);
847 if (!(flags
& SUPPRESS
))
852 && (char *) str
== *strptr
+ strsize
)
854 /* Enlarge the buffer. */
857 + (strsize
>= width
? width
- 1 : strsize
);
859 str
= (char *) realloc (*strptr
, newsize
);
862 /* Can't allocate that much. Last-ditch
864 str
= (char *) realloc (*strptr
, strsize
+ 1);
867 /* c can't have `a' flag, only `m'. */
873 *strptr
= (char *) str
;
880 *strptr
= (char *) str
;
887 while (--width
> 0 && inchar () != EOF
);
890 while (--width
> 0 && inchar () != EOF
);
893 if (!(flags
& SUPPRESS
))
895 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
897 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
912 STRING_ARG (wstr
, wchar_t, (width
> 1024 ? 1024 : width
));
915 if (__glibc_unlikely (c
== EOF
))
918 #ifdef COMPILE_WSCANF
919 /* Just store the incoming wide characters. */
920 if (!(flags
& SUPPRESS
))
925 && wstr
== (wchar_t *) *strptr
+ strsize
)
928 = strsize
+ (strsize
> width
? width
- 1 : strsize
);
929 /* Enlarge the buffer. */
930 wstr
= (wchar_t *) realloc (*strptr
,
931 newsize
* sizeof (wchar_t));
934 /* Can't allocate that much. Last-ditch effort. */
935 wstr
= (wchar_t *) realloc (*strptr
,
940 /* C or lc can't have `a' flag, only `m'
947 *strptr
= (char *) wstr
;
954 *strptr
= (char *) wstr
;
961 while (--width
> 0 && inchar () != EOF
);
964 while (--width
> 0 && inchar () != EOF
);
967 /* We have to convert the multibyte input sequence to wide
972 memset (&cstate
, '\0', sizeof (cstate
));
976 /* This is what we present the mbrtowc function first. */
979 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
980 && wstr
== (wchar_t *) *strptr
+ strsize
)
983 = strsize
+ (strsize
> width
? width
- 1 : strsize
);
984 /* Enlarge the buffer. */
985 wstr
= (wchar_t *) realloc (*strptr
,
986 newsize
* sizeof (wchar_t));
989 /* Can't allocate that much. Last-ditch effort. */
990 wstr
= (wchar_t *) realloc (*strptr
,
992 * sizeof (wchar_t)));
995 /* C or lc can't have `a' flag, only `m' flag. */
1001 *strptr
= (char *) wstr
;
1008 *strptr
= (char *) wstr
;
1018 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
1021 if (n
== (size_t) -2)
1023 /* Possibly correct character, just not enough
1025 if (__glibc_unlikely (inchar () == EOF
))
1032 if (__glibc_unlikely (n
!= 1))
1035 /* We have a match. */
1039 /* Advance the result pointer. */
1042 while (--width
> 0 && inchar () != EOF
);
1046 if (!(flags
& SUPPRESS
))
1048 if ((flags
& MALLOC
) && wstr
- (wchar_t *) *strptr
!= strsize
)
1050 wchar_t *cp
= (wchar_t *) realloc (*strptr
,
1052 - (wchar_t *) *strptr
)
1053 * sizeof (wchar_t)));
1055 *strptr
= (char *) cp
;
1064 case L_('s'): /* Read a string. */
1065 if (!(flags
& LONG
))
1067 STRING_ARG (str
, char, 100);
1070 if (__glibc_unlikely (c
== EOF
))
1073 #ifdef COMPILE_WSCANF
1074 memset (&state
, '\0', sizeof (state
));
1081 ungetc_not_eof (c
, s
);
1085 #ifdef COMPILE_WSCANF
1086 /* This is quite complicated. We have to convert the
1087 wide characters into multibyte characters and then
1092 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
1093 && *strptr
+ strsize
- str
<= MB_LEN_MAX
)
1095 /* We have to enlarge the buffer if the `a' or `m'
1097 size_t strleng
= str
- *strptr
;
1100 newstr
= (char *) realloc (*strptr
, strsize
* 2);
1103 /* Can't allocate that much. Last-ditch
1105 newstr
= (char *) realloc (*strptr
,
1106 strleng
+ MB_LEN_MAX
);
1109 if (flags
& POSIX_MALLOC
)
1114 /* We lose. Oh well. Terminate the
1115 string and stop converting,
1116 so at least we don't skip any input. */
1117 ((char *) (*strptr
))[strleng
] = '\0';
1125 str
= newstr
+ strleng
;
1126 strsize
= strleng
+ MB_LEN_MAX
;
1132 str
= newstr
+ strleng
;
1137 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
,
1139 if (__glibc_unlikely (n
== (size_t) -1))
1142 assert (n
<= MB_LEN_MAX
);
1147 if (!(flags
& SUPPRESS
))
1150 if ((flags
& MALLOC
)
1151 && (char *) str
== *strptr
+ strsize
)
1153 /* Enlarge the buffer. */
1154 str
= (char *) realloc (*strptr
, 2 * strsize
);
1157 /* Can't allocate that much. Last-ditch
1159 str
= (char *) realloc (*strptr
, strsize
+ 1);
1162 if (flags
& POSIX_MALLOC
)
1167 /* We lose. Oh well. Terminate the
1168 string and stop converting,
1169 so at least we don't skip any input. */
1170 ((char *) (*strptr
))[strsize
- 1] = '\0';
1177 *strptr
= (char *) str
;
1184 *strptr
= (char *) str
;
1192 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
1194 if (!(flags
& SUPPRESS
))
1196 #ifdef COMPILE_WSCANF
1197 /* We have to emit the code to get into the initial
1199 char buf
[MB_LEN_MAX
];
1200 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
1201 if (n
> 0 && (flags
& MALLOC
)
1202 && str
+ n
>= *strptr
+ strsize
)
1204 /* Enlarge the buffer. */
1205 size_t strleng
= str
- *strptr
;
1208 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
1211 if (flags
& POSIX_MALLOC
)
1216 /* We lose. Oh well. Terminate the string
1217 and stop converting, so at least we don't
1219 ((char *) (*strptr
))[strleng
] = '\0';
1227 str
= newstr
+ strleng
;
1228 strsize
= strleng
+ n
+ 1;
1232 str
= __mempcpy (str
, buf
, n
);
1236 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
1238 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
1252 #ifndef COMPILE_WSCANF
1256 /* Wide character string. */
1257 STRING_ARG (wstr
, wchar_t, 100);
1260 if (__builtin_expect (c
== EOF
, 0))
1263 #ifndef COMPILE_WSCANF
1264 memset (&cstate
, '\0', sizeof (cstate
));
1271 ungetc_not_eof (c
, s
);
1275 #ifdef COMPILE_WSCANF
1277 if (!(flags
& SUPPRESS
))
1280 if ((flags
& MALLOC
)
1281 && wstr
== (wchar_t *) *strptr
+ strsize
)
1283 /* Enlarge the buffer. */
1284 wstr
= (wchar_t *) realloc (*strptr
,
1286 * sizeof (wchar_t));
1289 /* Can't allocate that much. Last-ditch
1291 wstr
= (wchar_t *) realloc (*strptr
,
1293 * sizeof (wchar_t));
1296 if (flags
& POSIX_MALLOC
)
1301 /* We lose. Oh well. Terminate the string
1302 and stop converting, so at least we don't
1304 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1311 *strptr
= (char *) wstr
;
1318 *strptr
= (char *) wstr
;
1334 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
1337 if (n
== (size_t) -2)
1339 /* Possibly correct character, just not enough
1341 if (__glibc_unlikely (inchar () == EOF
))
1348 if (__glibc_unlikely (n
!= 1))
1351 /* We have a match. */
1356 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
1357 && wstr
== (wchar_t *) *strptr
+ strsize
)
1359 /* Enlarge the buffer. */
1360 wstr
= (wchar_t *) realloc (*strptr
,
1362 * sizeof (wchar_t)));
1365 /* Can't allocate that much. Last-ditch effort. */
1366 wstr
= (wchar_t *) realloc (*strptr
,
1368 * sizeof (wchar_t)));
1371 if (flags
& POSIX_MALLOC
)
1376 /* We lose. Oh well. Terminate the
1377 string and stop converting, so at
1378 least we don't skip any input. */
1379 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1386 *strptr
= (char *) wstr
;
1393 *strptr
= (char *) wstr
;
1401 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
1403 if (!(flags
& SUPPRESS
))
1407 if ((flags
& MALLOC
) && wstr
- (wchar_t *) *strptr
!= strsize
)
1409 wchar_t *cp
= (wchar_t *) realloc (*strptr
,
1411 - (wchar_t *) *strptr
)
1412 * sizeof (wchar_t)));
1414 *strptr
= (char *) cp
;
1423 case L_('x'): /* Hexadecimal integer. */
1424 case L_('X'): /* Ditto. */
1428 case L_('o'): /* Octal integer. */
1432 case L_('b'): /* Binary integer. */
1436 case L_('u'): /* Unsigned decimal integer. */
1440 case L_('d'): /* Signed decimal integer. */
1442 flags
|= NUMBER_SIGNED
;
1445 case L_('i'): /* Generic number. */
1447 flags
|= NUMBER_SIGNED
;
1451 if (__glibc_unlikely (c
== EOF
))
1454 /* Check for a sign. */
1455 if (c
== L_('-') || c
== L_('+'))
1457 char_buffer_add (&charbuf
, c
);
1463 /* Look for a leading indication of base. */
1464 if (width
!= 0 && c
== L_('0'))
1469 char_buffer_add (&charbuf
, c
);
1472 if (width
!= 0 && TOLOWER (c
) == L_('x'))
1484 && TOLOWER (c
) == L_('b')
1486 || ((mode_flags
& SCANF_ISOC23_BIN_CST
) != 0
1501 if (base
== 10 && __builtin_expect ((flags
& I18N
) != 0, 0))
1506 enum { num_digits_len
= 10 };
1507 #ifdef COMPILE_WSCANF
1508 const wchar_t *wcdigits
[num_digits_len
];
1510 const char *mbdigits
[num_digits_len
];
1512 CHAR_T
*digits_extended
[num_digits_len
] = { NULL
};
1514 /* "to_inpunct" is a map from ASCII digits to their
1515 equivalent in locale. This is defined for locales
1516 which use an extra digits set. */
1517 wctrans_t map
= __wctrans ("to_inpunct");
1521 #ifdef COMPILE_WSCANF
1522 to_level
= _NL_CURRENT_WORD (LC_CTYPE
,
1523 _NL_CTYPE_INDIGITS_WC_LEN
) - 1;
1525 to_level
= (uint32_t) curctype
->values
[_NL_ITEM_INDEX (_NL_CTYPE_INDIGITS_MB_LEN
)].word
- 1;
1528 /* Get the alternative digit forms if there are any. */
1529 if (__glibc_unlikely (map
!= NULL
))
1531 /* Adding new level for extra digits set in locale file. */
1534 for (n
= 0; n
< num_digits_len
; ++n
)
1536 #ifdef COMPILE_WSCANF
1537 wcdigits
[n
] = (const wchar_t *)
1538 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1540 wchar_t *wc_extended
= (wchar_t *)
1541 malloc ((to_level
+ 2) * sizeof (wchar_t));
1542 if (wc_extended
== NULL
)
1545 goto digits_extended_fail
;
1547 __wmemcpy (wc_extended
, wcdigits
[n
], to_level
);
1548 wc_extended
[to_level
] = __towctrans (L
'0' + n
, map
);
1549 wc_extended
[to_level
+ 1] = '\0';
1550 digits_extended
[n
] = wc_extended
;
1553 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1555 /* Get the equivalent wide char in map. */
1556 wint_t extra_wcdigit
= __towctrans (L
'0' + n
, map
);
1558 /* Convert it to multibyte representation. */
1560 memset (&state
, '\0', sizeof (state
));
1562 char extra_mbdigit
[MB_LEN_MAX
];
1564 = __wcrtomb (extra_mbdigit
, extra_wcdigit
, &state
);
1566 if (mblen
== (size_t) -1)
1568 /* Ignore this new level. */
1573 /* Calculate the length of mbdigits[n]. */
1574 const char *last_char
= mbdigits
[n
];
1575 for (level
= 0; level
< to_level
; ++level
)
1576 last_char
= strchr (last_char
, '\0') + 1;
1578 size_t mbdigits_len
= last_char
- mbdigits
[n
];
1580 /* Allocate memory for extended multibyte digit. */
1581 char *mb_extended
= malloc (mbdigits_len
+ mblen
+ 1);
1582 if (mb_extended
== NULL
)
1585 goto digits_extended_fail
;
1588 /* And get the mbdigits + extra_digit string. */
1589 *(char *) __mempcpy (__mempcpy (mb_extended
, mbdigits
[n
],
1591 extra_mbdigit
, mblen
) = '\0';
1592 digits_extended
[n
] = mb_extended
;
1597 /* Read the number into workspace. */
1598 while (c
!= EOF
&& width
!= 0)
1600 /* In this round we get the pointer to the digit strings
1601 and also perform the first round of comparisons. */
1602 for (n
= 0; n
< num_digits_len
; ++n
)
1604 /* Get the string for the digits with value N. */
1605 #ifdef COMPILE_WSCANF
1607 /* wcdigits_extended[] is fully set in the loop
1608 above, but the test for "map != NULL" is done
1609 inside the loop here and outside the loop there. */
1610 DIAG_PUSH_NEEDS_COMMENT
;
1611 DIAG_IGNORE_NEEDS_COMMENT (4.7, "-Wmaybe-uninitialized");
1613 if (__glibc_unlikely (map
!= NULL
))
1614 wcdigits
[n
] = digits_extended
[n
];
1616 wcdigits
[n
] = (const wchar_t *)
1617 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1618 wcdigits
[n
] += from_level
;
1620 DIAG_POP_NEEDS_COMMENT
;
1622 if (c
== (wint_t) *wcdigits
[n
])
1624 to_level
= from_level
;
1628 /* Advance the pointer to the next string. */
1632 int avail
= width
> 0 ? width
: INT_MAX
;
1634 if (__glibc_unlikely (map
!= NULL
))
1635 mbdigits
[n
] = digits_extended
[n
];
1638 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1640 for (level
= 0; level
< from_level
; level
++)
1641 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1644 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1646 if (*++cmpp
== '\0')
1650 if (avail
== 0 || inchar () == EOF
)
1660 to_level
= from_level
;
1664 /* We are pushing all read characters back. */
1665 if (cmpp
> mbdigits
[n
])
1668 while (--cmpp
> mbdigits
[n
])
1669 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1670 c
= (unsigned char) *cmpp
;
1673 /* Advance the pointer to the next string. */
1674 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1678 if (n
== num_digits_len
)
1680 /* Have not yet found the digit. */
1681 for (level
= from_level
+ 1; level
<= to_level
; ++level
)
1683 /* Search all ten digits of this level. */
1684 for (n
= 0; n
< num_digits_len
; ++n
)
1686 #ifdef COMPILE_WSCANF
1687 if (c
== (wint_t) *wcdigits
[n
])
1690 /* Advance the pointer to the next string. */
1694 int avail
= width
> 0 ? width
: INT_MAX
;
1697 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1699 if (*++cmpp
== '\0')
1703 if (avail
== 0 || inchar () == EOF
)
1716 /* We are pushing all read characters back. */
1717 if (cmpp
> mbdigits
[n
])
1720 while (--cmpp
> mbdigits
[n
])
1721 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1722 c
= (unsigned char) *cmpp
;
1725 /* Advance the pointer to the next string. */
1726 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1740 if (n
< num_digits_len
)
1742 else if (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 digits_extended_fail
:
1805 for (n
= 0; n
< num_digits_len
; n
++)
1806 free (digits_extended
[n
]);
1812 /* Read the number into workspace. */
1813 while (c
!= EOF
&& width
!= 0)
1820 else if (!ISDIGIT (c
) || (int) (c
- L_('0')) >= base
)
1822 if (base
== 10 && (flags
& GROUP
))
1824 /* Try matching against the thousands separator. */
1825 #ifdef COMPILE_WSCANF
1829 const char *cmpp
= thousands
;
1830 int avail
= width
> 0 ? width
: INT_MAX
;
1832 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1834 char_buffer_add (&charbuf
, c
);
1835 if (*++cmpp
== '\0')
1839 if (avail
== 0 || inchar () == EOF
)
1845 if (char_buffer_error (&charbuf
))
1847 __set_errno (ENOMEM
);
1854 /* We are pushing all read characters back. */
1855 if (cmpp
> thousands
)
1857 charbuf
.current
-= cmpp
- thousands
;
1859 while (--cmpp
> thousands
)
1860 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1861 c
= (unsigned char) *cmpp
;
1869 /* The last thousands character will be added back by
1870 the char_buffer_add below. */
1877 char_buffer_add (&charbuf
, c
);
1884 if (char_buffer_error (&charbuf
))
1886 __set_errno (ENOMEM
);
1891 if (char_buffer_size (&charbuf
) == 0
1892 || (char_buffer_size (&charbuf
) == 1
1893 && (char_buffer_start (&charbuf
)[0] == L_('+')
1894 || char_buffer_start (&charbuf
)[0] == L_('-'))))
1896 /* There was no number. If we are supposed to read a pointer
1897 we must recognize "(nil)" as well. */
1898 if (__builtin_expect (char_buffer_size (&charbuf
) == 0
1899 && (flags
& READ_POINTER
)
1900 && (width
< 0 || width
>= 5)
1902 && TOLOWER (inchar ()) == L_('n')
1903 && TOLOWER (inchar ()) == L_('i')
1904 && TOLOWER (inchar ()) == L_('l')
1905 && inchar () == L_(')'), 1))
1906 /* We must produce the value of a NULL pointer. A single
1907 '0' digit is enough. */
1908 char_buffer_add (&charbuf
, L_('0'));
1911 /* The last read character is not part of the number
1919 /* The just read character is not part of the number anymore. */
1922 /* Convert the number. */
1923 char_buffer_add (&charbuf
, L_('\0'));
1924 if (char_buffer_error (&charbuf
))
1926 __set_errno (ENOMEM
);
1930 if (need_longlong
&& (flags
& LONGDBL
))
1932 if (flags
& NUMBER_SIGNED
)
1933 num
.q
= __strtoll_internal
1934 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1936 num
.uq
= __strtoull_internal
1937 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1941 if (flags
& NUMBER_SIGNED
)
1942 num
.l
= __strtol_internal
1943 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1945 num
.ul
= __strtoul_internal
1946 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1948 if (__glibc_unlikely (char_buffer_start (&charbuf
) == tw
))
1951 if (!(flags
& SUPPRESS
))
1953 if (flags
& NUMBER_SIGNED
)
1955 if (need_longlong
&& (flags
& LONGDBL
))
1956 *ARG (LONGLONG
int *) = num
.q
;
1957 else if (need_long
&& (flags
& LONG
))
1958 *ARG (long int *) = num
.l
;
1959 else if (flags
& SHORT
)
1960 *ARG (short int *) = (short int) num
.l
;
1961 else if (!(flags
& CHAR
))
1962 *ARG (int *) = (int) num
.l
;
1964 *ARG (signed char *) = (signed char) num
.ul
;
1968 if (need_longlong
&& (flags
& LONGDBL
))
1969 *ARG (unsigned LONGLONG
int *) = num
.uq
;
1970 else if (need_long
&& (flags
& LONG
))
1971 *ARG (unsigned long int *) = num
.ul
;
1972 else if (flags
& SHORT
)
1973 *ARG (unsigned short int *)
1974 = (unsigned short int) num
.ul
;
1975 else if (!(flags
& CHAR
))
1976 *ARG (unsigned int *) = (unsigned int) num
.ul
;
1978 *ARG (unsigned char *) = (unsigned char) num
.ul
;
1984 case L_('e'): /* Floating-point numbers. */
1995 if (__glibc_unlikely (c
== EOF
))
1998 got_digit
= got_dot
= got_e
= got_sign
= 0;
2000 /* Check for a sign. */
2001 if (c
== L_('-') || c
== L_('+'))
2004 char_buffer_add (&charbuf
, c
);
2005 if (__glibc_unlikely (width
== 0 || inchar () == EOF
))
2006 /* EOF is only an input error before we read any chars. */
2012 /* Take care for the special arguments "nan" and "inf". */
2013 if (TOLOWER (c
) == L_('n'))
2016 char_buffer_add (&charbuf
, c
);
2017 if (__builtin_expect (width
== 0
2019 || TOLOWER (c
) != L_('a'), 0))
2023 char_buffer_add (&charbuf
, c
);
2024 if (__builtin_expect (width
== 0
2026 || TOLOWER (c
) != L_('n'), 0))
2030 char_buffer_add (&charbuf
, c
);
2034 else if (TOLOWER (c
) == L_('i'))
2036 /* Maybe "inf" or "infinity". */
2037 char_buffer_add (&charbuf
, c
);
2038 if (__builtin_expect (width
== 0
2040 || TOLOWER (c
) != L_('n'), 0))
2044 char_buffer_add (&charbuf
, c
);
2045 if (__builtin_expect (width
== 0
2047 || TOLOWER (c
) != L_('f'), 0))
2051 char_buffer_add (&charbuf
, c
);
2052 /* It is as least "inf". */
2053 if (width
!= 0 && inchar () != EOF
)
2055 if (TOLOWER (c
) == L_('i'))
2059 /* Now we have to read the rest as well. */
2060 char_buffer_add (&charbuf
, c
);
2061 if (__builtin_expect (width
== 0
2063 || TOLOWER (c
) != L_('n'), 0))
2067 char_buffer_add (&charbuf
, c
);
2068 if (__builtin_expect (width
== 0
2070 || TOLOWER (c
) != L_('i'), 0))
2074 char_buffer_add (&charbuf
, c
);
2075 if (__builtin_expect (width
== 0
2077 || TOLOWER (c
) != L_('t'), 0))
2081 char_buffer_add (&charbuf
, c
);
2082 if (__builtin_expect (width
== 0
2084 || TOLOWER (c
) != L_('y'), 0))
2088 char_buffer_add (&charbuf
, c
);
2098 if (width
!= 0 && c
== L_('0'))
2100 char_buffer_add (&charbuf
, c
);
2104 if (width
!= 0 && TOLOWER (c
) == L_('x'))
2106 /* It is a number in hexadecimal format. */
2107 char_buffer_add (&charbuf
, c
);
2109 flags
|= HEXA_FLOAT
;
2112 /* Grouping is not allowed. */
2124 if (char_buffer_error (&charbuf
))
2126 __set_errno (ENOMEM
);
2132 char_buffer_add (&charbuf
, c
);
2135 else if (!got_e
&& (flags
& HEXA_FLOAT
) && ISXDIGIT (c
))
2137 char_buffer_add (&charbuf
, c
);
2140 else if (got_e
&& charbuf
.current
[-1] == exp_char
2141 && (c
== L_('-') || c
== L_('+')))
2142 char_buffer_add (&charbuf
, c
);
2143 else if (got_digit
&& !got_e
2144 && (CHAR_T
) TOLOWER (c
) == exp_char
)
2146 char_buffer_add (&charbuf
, exp_char
);
2147 got_e
= got_dot
= 1;
2151 #ifdef COMPILE_WSCANF
2152 if (! got_dot
&& c
== decimal
)
2154 char_buffer_add (&charbuf
, c
);
2157 else if ((flags
& GROUP
) != 0 && ! got_dot
&& c
== thousands
)
2158 char_buffer_add (&charbuf
, c
);
2161 /* The last read character is not part of the number
2167 const char *cmpp
= decimal
;
2168 int avail
= width
> 0 ? width
: INT_MAX
;
2172 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
2173 if (*++cmpp
== '\0')
2177 if (avail
== 0 || inchar () == EOF
)
2185 /* Add all the characters. */
2186 for (cmpp
= decimal
; *cmpp
!= '\0'; ++cmpp
)
2187 char_buffer_add (&charbuf
, (unsigned char) *cmpp
);
2194 /* Figure out whether it is a thousands separator.
2195 There is one problem: we possibly read more than
2196 one character. We cannot push them back but since
2197 we know that parts of the `decimal' string matched,
2198 we can compare against it. */
2199 const char *cmp2p
= thousands
;
2201 if ((flags
& GROUP
) != 0 && ! got_dot
)
2203 while (cmp2p
- thousands
< cmpp
- decimal
2204 && *cmp2p
== decimal
[cmp2p
- thousands
])
2206 if (cmp2p
- thousands
== cmpp
- decimal
)
2208 while ((unsigned char) *cmp2p
== c
&& avail
>= 0)
2209 if (*++cmp2p
== '\0')
2213 if (avail
== 0 || inchar () == EOF
)
2220 if (cmp2p
!= NULL
&& *cmp2p
== '\0')
2222 /* Add all the characters. */
2223 for (cmpp
= thousands
; *cmpp
!= '\0'; ++cmpp
)
2224 char_buffer_add (&charbuf
, (unsigned char) *cmpp
);
2230 /* The last read character is not part of the number
2239 if (width
== 0 || inchar () == EOF
)
2246 if (char_buffer_error (&charbuf
))
2248 __set_errno (ENOMEM
);
2254 if (__builtin_expect ((flags
& I18N
) != 0, 0)
2255 /* Hexadecimal floats make no sense, fixing localized
2256 digits with ASCII letters. */
2257 && !(flags
& HEXA_FLOAT
)
2258 /* Minimum requirement. */
2259 && (char_buffer_size (&charbuf
) == got_sign
|| got_dot
)
2260 && (map
= __wctrans ("to_inpunct")) != NULL
)
2262 /* Reget the first character. */
2265 /* Localized digits, decimal points, and thousands
2267 wint_t wcdigits
[12];
2269 /* First get decimal equivalent to check if we read it
2271 wcdigits
[11] = __towctrans (L
'.', map
);
2273 /* If we have not read any character or have just read
2274 locale decimal point which matches the decimal point
2275 for localized FP numbers, then we may have localized
2276 digits. Note, we test GOT_DOT above. */
2277 #ifdef COMPILE_WSCANF
2278 if (char_buffer_size (&charbuf
) == got_sign
2279 || (char_buffer_size (&charbuf
) == got_sign
+ 1
2280 && wcdigits
[11] == decimal
))
2282 char mbdigits
[12][MB_LEN_MAX
+ 1];
2285 memset (&state
, '\0', sizeof (state
));
2287 bool match_so_far
= char_buffer_size (&charbuf
) == got_sign
;
2288 size_t mblen
= __wcrtomb (mbdigits
[11], wcdigits
[11], &state
);
2289 if (mblen
!= (size_t) -1)
2291 mbdigits
[11][mblen
] = '\0';
2293 (char_buffer_size (&charbuf
) == strlen (decimal
) + got_sign
2294 && strcmp (decimal
, mbdigits
[11]) == 0);
2298 size_t decimal_len
= strlen (decimal
);
2299 /* This should always be the case but the data comes
2301 if (decimal_len
<= MB_LEN_MAX
)
2303 match_so_far
|= (char_buffer_size (&charbuf
)
2304 == decimal_len
+ got_sign
);
2305 memcpy (mbdigits
[11], decimal
, decimal_len
+ 1);
2308 match_so_far
= false;
2314 bool have_locthousands
= (flags
& GROUP
) != 0;
2316 /* Now get the digits and the thousands-sep equivalents. */
2317 for (int n
= 0; n
< 11; ++n
)
2320 wcdigits
[n
] = __towctrans (L
'0' + n
, map
);
2323 wcdigits
[10] = __towctrans (L
',', map
);
2324 have_locthousands
&= wcdigits
[10] != L
'\0';
2327 #ifndef COMPILE_WSCANF
2328 memset (&state
, '\0', sizeof (state
));
2330 size_t mblen
= __wcrtomb (mbdigits
[n
], wcdigits
[n
],
2332 if (mblen
== (size_t) -1)
2336 if (have_locthousands
)
2338 size_t thousands_len
= strlen (thousands
);
2339 if (thousands_len
<= MB_LEN_MAX
)
2340 memcpy (mbdigits
[10], thousands
,
2343 have_locthousands
= false;
2347 /* Ignore checking against localized digits. */
2351 mbdigits
[n
][mblen
] = '\0';
2355 /* Start checking against localized digits, if
2356 conversion is done correctly. */
2359 if (char_buffer_error (&charbuf
))
2361 __set_errno (ENOMEM
);
2365 if (got_e
&& charbuf
.current
[-1] == exp_char
2366 && (c
== L_('-') || c
== L_('+')))
2367 char_buffer_add (&charbuf
, c
);
2368 else if (char_buffer_size (&charbuf
) > got_sign
&& !got_e
2369 && (CHAR_T
) TOLOWER (c
) == exp_char
)
2371 char_buffer_add (&charbuf
, exp_char
);
2372 got_e
= got_dot
= 1;
2376 /* Check against localized digits, decimal point,
2377 and thousands separator. */
2379 for (n
= 0; n
< 12; ++n
)
2381 #ifdef COMPILE_WSCANF
2382 if (c
== wcdigits
[n
])
2385 char_buffer_add (&charbuf
, L_('0') + n
);
2386 else if (n
== 11 && !got_dot
)
2388 char_buffer_add (&charbuf
, decimal
);
2391 else if (n
== 10 && have_locthousands
2393 char_buffer_add (&charbuf
, thousands
);
2395 /* The last read character is not part
2396 of the number anymore. */
2402 const char *cmpp
= mbdigits
[n
];
2403 int avail
= width
> 0 ? width
: INT_MAX
;
2405 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
2406 if (*++cmpp
== '\0')
2410 if (avail
== 0 || inchar () == EOF
)
2420 char_buffer_add (&charbuf
, L_('0') + n
);
2421 else if (n
== 11 && !got_dot
)
2423 /* Add all the characters. */
2424 for (cmpp
= decimal
; *cmpp
!= '\0';
2426 char_buffer_add (&charbuf
,
2427 (unsigned char) *cmpp
);
2431 else if (n
== 10 && (flags
& GROUP
) != 0
2434 /* Add all the characters. */
2435 for (cmpp
= thousands
; *cmpp
!= '\0';
2437 char_buffer_add (&charbuf
,
2438 (unsigned char) *cmpp
);
2441 /* The last read character is not part
2442 of the number anymore. */
2448 /* We are pushing all read characters back. */
2449 if (cmpp
> mbdigits
[n
])
2452 while (--cmpp
> mbdigits
[n
])
2453 ungetc_not_eof ((unsigned char) *cmpp
, s
);
2454 c
= (unsigned char) *cmpp
;
2461 /* The last read character is not part
2462 of the number anymore. */
2468 if (width
== 0 || inchar () == EOF
)
2476 #ifndef COMPILE_WSCANF
2482 if (char_buffer_error (&charbuf
))
2484 __set_errno (ENOMEM
);
2489 /* Have we read any character? If we try to read a number
2490 in hexadecimal notation and we have read only the `0x'
2491 prefix this is an error. */
2492 if (__glibc_unlikely (char_buffer_size (&charbuf
) == got_sign
2493 || ((flags
& HEXA_FLOAT
)
2494 && (char_buffer_size (&charbuf
)
2499 /* Convert the number. */
2500 char_buffer_add (&charbuf
, L_('\0'));
2501 if (char_buffer_error (&charbuf
))
2503 __set_errno (ENOMEM
);
2507 #if __HAVE_FLOAT128_UNLIKE_LDBL
2508 if ((flags
& LONGDBL
) \
2509 && (mode_flags
& SCANF_LDBL_USES_FLOAT128
) != 0)
2511 _Float128 d
= __strtof128_internal
2512 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2513 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2514 *ARG (_Float128
*) = d
;
2518 if ((flags
& LONGDBL
) \
2519 && __glibc_likely ((mode_flags
& SCANF_LDBL_IS_DBL
) == 0))
2521 long double d
= __strtold_internal
2522 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2523 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2524 *ARG (long double *) = d
;
2526 else if (flags
& (LONG
| LONGDBL
))
2528 double d
= __strtod_internal
2529 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2530 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2531 *ARG (double *) = d
;
2535 float d
= __strtof_internal
2536 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2537 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2541 if (__glibc_unlikely (tw
== char_buffer_start (&charbuf
)))
2544 if (!(flags
& SUPPRESS
))
2548 case L_('['): /* Character class. */
2550 STRING_ARG (wstr
, wchar_t, 100);
2552 STRING_ARG (str
, char, 100);
2563 #ifdef COMPILE_WSCANF
2564 /* Find the beginning and the end of the scanlist. We are not
2565 creating a lookup table since it would have to be too large.
2566 Instead we search each time through the string. This is not
2567 a constant lookup time but who uses this feature deserves to
2569 tw
= (wchar_t *) f
; /* Marks the beginning. */
2574 while ((fc
= *f
++) != L
'\0' && fc
!= L
']');
2576 if (__glibc_unlikely (fc
== L
'\0'))
2578 wchar_t *twend
= (wchar_t *) f
- 1;
2580 /* Fill WP with byte flags indexed by character.
2581 We will use this flag map for matching input characters. */
2582 if (!scratch_buffer_set_array_size
2583 (&charbuf
.scratch
, UCHAR_MAX
+ 1, 1))
2588 memset (charbuf
.scratch
.data
, '\0', UCHAR_MAX
+ 1);
2591 if (fc
== ']' || fc
== '-')
2593 /* If ] or - appears before any char in the set, it is not
2594 the terminator or separator, but the first char in the
2596 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2600 while ((fc
= *f
++) != '\0' && fc
!= ']')
2601 if (fc
== '-' && *f
!= '\0' && *f
!= ']' && f
[-2] <= *f
)
2603 /* Add all characters from the one before the '-'
2604 up to (but not including) the next format char. */
2605 for (fc
= f
[-2]; fc
< *f
; ++fc
)
2606 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2609 /* Add the character to the flag map. */
2610 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2612 if (__glibc_unlikely (fc
== '\0'))
2618 size_t now
= read_in
;
2619 #ifdef COMPILE_WSCANF
2620 if (__glibc_unlikely (inchar () == WEOF
))
2627 /* Test whether it's in the scanlist. */
2629 while (runp
< twend
)
2631 if (runp
[0] == L
'-' && runp
[1] != '\0'
2632 && runp
+ 1 != twend
2634 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2636 /* Match against all characters in between the
2637 first and last character of the sequence. */
2640 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2641 if ((wint_t) wc
== c
)
2644 if (wc
<= runp
[1] && !not_in
)
2646 if (wc
<= runp
[1] && not_in
)
2648 /* The current character is not in the
2658 if ((wint_t) *runp
== c
&& !not_in
)
2660 if ((wint_t) *runp
== c
&& not_in
)
2670 if (runp
== twend
&& !not_in
)
2676 if (!(flags
& SUPPRESS
))
2680 if ((flags
& MALLOC
)
2681 && wstr
== (wchar_t *) *strptr
+ strsize
)
2683 /* Enlarge the buffer. */
2684 wstr
= (wchar_t *) realloc (*strptr
,
2686 * sizeof (wchar_t));
2689 /* Can't allocate that much. Last-ditch
2692 realloc (*strptr
, (strsize
+ 1)
2693 * sizeof (wchar_t));
2696 if (flags
& POSIX_MALLOC
)
2701 /* We lose. Oh well. Terminate the string
2702 and stop converting, so at least we don't
2704 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2711 *strptr
= (char *) wstr
;
2718 *strptr
= (char *) wstr
;
2725 while ((width
< 0 || --width
> 0) && inchar () != WEOF
);
2728 char buf
[MB_LEN_MAX
];
2732 if (__glibc_unlikely (inchar () == EOF
))
2735 memset (&cstate
, '\0', sizeof (cstate
));
2739 if (((char *) charbuf
.scratch
.data
)[c
] == not_in
)
2741 ungetc_not_eof (c
, s
);
2746 if (!(flags
& SUPPRESS
))
2750 /* Convert it into a wide character. */
2752 n
= __mbrtowc (wstr
, buf
, 1, &cstate
);
2754 if (n
== (size_t) -2)
2756 /* Possibly correct character, just not enough
2759 assert (cnt
< MB_LEN_MAX
);
2765 if ((flags
& MALLOC
)
2766 && wstr
== (wchar_t *) *strptr
+ strsize
)
2768 /* Enlarge the buffer. */
2769 wstr
= (wchar_t *) realloc (*strptr
,
2771 * sizeof (wchar_t)));
2774 /* Can't allocate that much. Last-ditch
2777 realloc (*strptr
, ((strsize
+ 1)
2778 * sizeof (wchar_t)));
2781 if (flags
& POSIX_MALLOC
)
2786 /* We lose. Oh well. Terminate the
2787 string and stop converting,
2788 so at least we don't skip any input. */
2789 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2796 *strptr
= (char *) wstr
;
2803 *strptr
= (char *) wstr
;
2810 if (width
>= 0 && --width
<= 0)
2813 while (inchar () != EOF
);
2815 if (__glibc_unlikely (cnt
!= 0))
2816 /* We stopped in the middle of recognizing another
2817 character. That's a problem. */
2821 if (__glibc_unlikely (now
== read_in
))
2822 /* We haven't successfully read any character. */
2825 if (!(flags
& SUPPRESS
))
2829 if ((flags
& MALLOC
)
2830 && wstr
- (wchar_t *) *strptr
!= strsize
)
2832 wchar_t *cp
= (wchar_t *)
2833 realloc (*strptr
, ((wstr
- (wchar_t *) *strptr
)
2834 * sizeof (wchar_t)));
2836 *strptr
= (char *) cp
;
2845 size_t now
= read_in
;
2847 if (__glibc_unlikely (inchar () == EOF
))
2850 #ifdef COMPILE_WSCANF
2852 memset (&state
, '\0', sizeof (state
));
2859 /* Test whether it's in the scanlist. */
2861 while (runp
< twend
)
2863 if (runp
[0] == L
'-' && runp
[1] != '\0'
2864 && runp
+ 1 != twend
2866 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2868 /* Match against all characters in between the
2869 first and last character of the sequence. */
2872 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2873 if ((wint_t) wc
== c
)
2876 if (wc
<= runp
[1] && !not_in
)
2878 if (wc
<= runp
[1] && not_in
)
2880 /* The current character is not in the
2890 if ((wint_t) *runp
== c
&& !not_in
)
2892 if ((wint_t) *runp
== c
&& not_in
)
2902 if (runp
== twend
&& !not_in
)
2908 if (!(flags
& SUPPRESS
))
2910 if ((flags
& MALLOC
)
2911 && *strptr
+ strsize
- str
<= MB_LEN_MAX
)
2913 /* Enlarge the buffer. */
2914 size_t strleng
= str
- *strptr
;
2917 newstr
= (char *) realloc (*strptr
, 2 * strsize
);
2920 /* Can't allocate that much. Last-ditch
2922 newstr
= (char *) realloc (*strptr
,
2923 strleng
+ MB_LEN_MAX
);
2926 if (flags
& POSIX_MALLOC
)
2931 /* We lose. Oh well. Terminate the string
2932 and stop converting, so at least we don't
2934 ((char *) (*strptr
))[strleng
] = '\0';
2942 str
= newstr
+ strleng
;
2943 strsize
= strleng
+ MB_LEN_MAX
;
2949 str
= newstr
+ strleng
;
2955 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
2956 if (__glibc_unlikely (n
== (size_t) -1))
2959 assert (n
<= MB_LEN_MAX
);
2962 while ((width
< 0 || --width
> 0) && inchar () != WEOF
);
2967 if (((char *) charbuf
.scratch
.data
)[c
] == not_in
)
2969 ungetc_not_eof (c
, s
);
2974 if (!(flags
& SUPPRESS
))
2977 if ((flags
& MALLOC
)
2978 && (char *) str
== *strptr
+ strsize
)
2980 /* Enlarge the buffer. */
2981 size_t newsize
= 2 * strsize
;
2984 str
= (char *) realloc (*strptr
, newsize
);
2987 /* Can't allocate that much. Last-ditch
2989 if (newsize
> strsize
+ 1)
2991 newsize
= strsize
+ 1;
2994 if (flags
& POSIX_MALLOC
)
2999 /* We lose. Oh well. Terminate the
3000 string and stop converting,
3001 so at least we don't skip any input. */
3002 ((char *) (*strptr
))[strsize
- 1] = '\0';
3009 *strptr
= (char *) str
;
3016 while ((width
< 0 || --width
> 0) && inchar () != EOF
);
3019 if (__glibc_unlikely (now
== read_in
))
3020 /* We haven't successfully read any character. */
3023 if (!(flags
& SUPPRESS
))
3025 #ifdef COMPILE_WSCANF
3026 /* We have to emit the code to get into the initial
3028 char buf
[MB_LEN_MAX
];
3029 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
3030 if (n
> 0 && (flags
& MALLOC
)
3031 && str
+ n
>= *strptr
+ strsize
)
3033 /* Enlarge the buffer. */
3034 size_t strleng
= str
- *strptr
;
3037 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
3040 if (flags
& POSIX_MALLOC
)
3045 /* We lose. Oh well. Terminate the string
3046 and stop converting, so at least we don't
3048 ((char *) (*strptr
))[strleng
] = '\0';
3056 str
= newstr
+ strleng
;
3057 strsize
= strleng
+ n
+ 1;
3061 str
= __mempcpy (str
, buf
, n
);
3065 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
3067 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
3078 case L_('p'): /* Generic pointer. */
3080 /* A PTR must be the same size as a `long int'. */
3081 flags
&= ~(SHORT
|LONGDBL
);
3084 flags
|= READ_POINTER
;
3088 /* If this is an unknown format character punt. */
3093 /* The last thing we saw int the format string was a white space.
3094 Consume the last white spaces. */
3099 while (ISSPACE (c
));
3104 /* Unlock stream. */
3107 scratch_buffer_free (&charbuf
.scratch
);
3109 if (__glibc_unlikely (done
== EOF
))
3111 if (__glibc_unlikely (ptrs_to_free
!= NULL
))
3113 struct ptrs_to_free
*p
= ptrs_to_free
;
3116 for (size_t cnt
= 0; cnt
< p
->count
; ++cnt
)
3118 free (*p
->ptrs
[cnt
]);
3119 *p
->ptrs
[cnt
] = NULL
;
3126 else if (__glibc_unlikely (strptr
!= NULL
))