1 /* Copyright (C) 1991-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
30 #include <libc-diag.h>
31 #include <libc-lock.h>
32 #include <locale/localeinfo.h>
33 #include <scratch_buffer.h>
36 # define HAVE_LONGLONG
37 # define LONGLONG long long
39 # define LONGLONG long
42 /* Determine whether we have to handle `long long' at all. */
43 #if LONG_MAX == LONG_LONG_MAX
44 # define need_longlong 0
46 # define need_longlong 1
49 /* Determine whether we have to handle `long'. */
50 #if INT_MAX == LONG_MAX
56 /* Those are flags in the conversion format. */
57 #define LONG 0x0001 /* l: long or double */
58 #define LONGDBL 0x0002 /* L: long long or long double */
59 #define SHORT 0x0004 /* h: short */
60 #define SUPPRESS 0x0008 /* *: suppress assignment */
61 #define POINTER 0x0010 /* weird %p pointer (`fake hex') */
62 #define NOSKIP 0x0020 /* do not skip blanks */
63 #define NUMBER_SIGNED 0x0040 /* signed integer */
64 #define GROUP 0x0080 /* ': group numbers */
65 #define GNU_MALLOC 0x0100 /* a: malloc strings */
66 #define CHAR 0x0200 /* hh: char */
67 #define I18N 0x0400 /* I: use locale's digits */
68 #define HEXA_FLOAT 0x0800 /* hexadecimal float */
69 #define READ_POINTER 0x1000 /* this is a pointer value */
70 #define POSIX_MALLOC 0x2000 /* m: malloc strings */
71 #define MALLOC (GNU_MALLOC | POSIX_MALLOC)
73 #include <locale/localeinfo.h>
77 # define ungetc(c, s) ((void) (c == WEOF \
79 _IO_sputbackwc (s, c))))
80 # define ungetc_not_eof(c, s) ((void) (--read_in, \
81 _IO_sputbackwc (s, c)))
82 # define inchar() (c == WEOF ? ((errno = inchar_errno), WEOF) \
83 : ((c = _IO_getwc_unlocked (s)), \
86 : (size_t) (inchar_errno = errno)), c))
88 # define ISSPACE(Ch) iswspace (Ch)
89 # define ISDIGIT(Ch) iswdigit (Ch)
90 # define ISXDIGIT(Ch) iswxdigit (Ch)
91 # define TOLOWER(Ch) towlower (Ch)
92 # define ORIENT if (_IO_fwide (s, 1) != 1) return WEOF
93 # define __strtoll_internal __wcstoll_internal
94 # define __strtoull_internal __wcstoull_internal
95 # define __strtol_internal __wcstol_internal
96 # define __strtoul_internal __wcstoul_internal
97 # define __strtold_internal __wcstold_internal
98 # define __strtod_internal __wcstod_internal
99 # define __strtof_internal __wcstof_internal
101 # define L_(Str) L##Str
102 # define CHAR_T wchar_t
103 # define UCHAR_T unsigned int
104 # define WINT_T wint_t
108 # define ungetc(c, s) ((void) ((int) c == EOF \
110 _IO_sputbackc (s, (unsigned char) c))))
111 # define ungetc_not_eof(c, s) ((void) (--read_in, \
112 _IO_sputbackc (s, (unsigned char) c)))
113 # define inchar() (c == EOF ? ((errno = inchar_errno), EOF) \
114 : ((c = _IO_getc_unlocked (s)), \
117 : (size_t) (inchar_errno = errno)), c))
118 # define ISSPACE(Ch) __isspace_l (Ch, loc)
119 # define ISDIGIT(Ch) __isdigit_l (Ch, loc)
120 # define ISXDIGIT(Ch) __isxdigit_l (Ch, loc)
121 # define TOLOWER(Ch) __tolower_l ((unsigned char) (Ch), loc)
122 # define ORIENT if (_IO_vtable_offset (s) == 0 \
123 && _IO_fwide (s, -1) != -1) \
128 # define UCHAR_T unsigned char
132 #include "printf-parse.h" /* Use read_int. */
134 #define encode_error() do { \
136 __set_errno (EILSEQ); \
139 #define conv_error() do { \
143 #define input_error() do { \
145 if (done == 0) done = EOF; \
148 #define add_ptr_to_free(ptr) \
151 if (ptrs_to_free == NULL \
152 || ptrs_to_free->count == (sizeof (ptrs_to_free->ptrs) \
153 / sizeof (ptrs_to_free->ptrs[0]))) \
155 struct ptrs_to_free *new_ptrs = alloca (sizeof (*ptrs_to_free)); \
156 new_ptrs->count = 0; \
157 new_ptrs->next = ptrs_to_free; \
158 ptrs_to_free = new_ptrs; \
160 ptrs_to_free->ptrs[ptrs_to_free->count++] = (ptr); \
163 #define ARGCHECK(s, format) \
166 /* Check file argument for consistence. */ \
167 CHECK_FILE (s, EOF); \
168 if (s->_flags & _IO_NO_READS) \
170 __set_errno (EBADF); \
173 else if (format == NULL) \
175 __set_errno (EINVAL); \
179 #define LOCK_STREAM(S) \
180 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, (S)); \
182 #define UNLOCK_STREAM(S) \
183 _IO_funlockfile (S); \
184 __libc_cleanup_region_end (0)
189 struct ptrs_to_free
*next
;
196 struct scratch_buffer scratch
;
199 /* Returns a pointer to the first CHAR_T object in the buffer. Only
200 valid if char_buffer_add (BUFFER, CH) has been called and
201 char_buffer_error (BUFFER) is false. */
202 static inline CHAR_T
*
203 char_buffer_start (const struct char_buffer
*buffer
)
205 return (CHAR_T
*) buffer
->scratch
.data
;
208 /* Returns the number of CHAR_T objects in the buffer. Only valid if
209 char_buffer_error (BUFFER) is false. */
211 char_buffer_size (const struct char_buffer
*buffer
)
213 return buffer
->current
- char_buffer_start (buffer
);
216 /* Reinitializes BUFFER->current and BUFFER->end to cover the entire
219 char_buffer_rewind (struct char_buffer
*buffer
)
221 buffer
->current
= char_buffer_start (buffer
);
222 buffer
->end
= buffer
->current
+ buffer
->scratch
.length
/ sizeof (CHAR_T
);
225 /* Returns true if a previous call to char_buffer_add (BUFFER, CH)
228 char_buffer_error (const struct char_buffer
*buffer
)
230 return __glibc_unlikely (buffer
->current
== NULL
);
233 /* Slow path for char_buffer_add. */
235 char_buffer_add_slow (struct char_buffer
*buffer
, CHAR_T ch
)
237 if (char_buffer_error (buffer
))
239 size_t offset
= buffer
->end
- (CHAR_T
*) buffer
->scratch
.data
;
240 if (!scratch_buffer_grow_preserve (&buffer
->scratch
))
242 buffer
->current
= NULL
;
246 char_buffer_rewind (buffer
);
247 buffer
->current
+= offset
;
248 *buffer
->current
++ = ch
;
251 /* Adds CH to BUFFER. This function does not report any errors, check
252 for them with char_buffer_error. */
254 char_buffer_add (struct char_buffer
*buffer
, CHAR_T ch
)
255 __attribute__ ((always_inline
));
257 char_buffer_add (struct char_buffer
*buffer
, CHAR_T ch
)
259 if (__glibc_unlikely (buffer
->current
== buffer
->end
))
260 char_buffer_add_slow (buffer
, ch
);
262 *buffer
->current
++ = ch
;
265 /* Read formatted input from S according to the format string
266 FORMAT, using the argument list in ARG.
267 Return the number of assignments made, or -1 for an input error. */
268 #ifdef COMPILE_WSCANF
270 _IO_vfwscanf (FILE *s
, const wchar_t *format
, va_list argptr
,
274 _IO_vfscanf_internal (FILE *s
, const char *format
, va_list argptr
,
279 const CHAR_T
*f
= format
;
280 UCHAR_T fc
; /* Current character of the format. */
281 WINT_T done
= 0; /* Assignments done. */
282 size_t read_in
= 0; /* Chars read in. */
283 WINT_T c
= 0; /* Last char read. */
284 int width
; /* Maximum field width. */
285 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
418 if (!isascii ((unsigned char) *f
))
420 /* Non-ASCII, may be a multibyte. */
421 int len
= __mbrlen (f
, strlen (f
), &state
);
427 if (__glibc_unlikely (c
== EOF
))
429 else if (c
!= (unsigned char) *f
++)
431 ungetc_not_eof (c
, s
);
444 /* Remember to skip spaces. */
451 /* Read a character. */
454 /* Characters other than format specs must just match. */
455 if (__glibc_unlikely (c
== EOF
))
458 /* We saw white space char as the last character in the format
459 string. Now it's time to skip all leading white space. */
463 if (__glibc_unlikely (inchar () == EOF
))
468 if (__glibc_unlikely (c
!= fc
))
477 /* This is the start of the conversion string. */
480 /* Initialize state of modifiers. */
483 /* Prepare temporary buffer. */
484 char_buffer_rewind (&charbuf
);
486 /* Check for a positional parameter specification. */
487 if (ISDIGIT ((UCHAR_T
) *f
))
489 argpos
= read_int ((const UCHAR_T
**) &f
);
494 /* Oops; that was actually the field width. */
501 /* Check for the assignment-suppressing, the number grouping flag,
502 and the signal to use the locale's digit representation. */
503 while (*f
== L_('*') || *f
== L_('\'') || *f
== L_('I'))
510 #ifdef COMPILE_WSCANF
511 if (thousands
!= L
'\0')
513 if (thousands
!= NULL
)
522 /* Find the maximum field width. */
524 if (ISDIGIT ((UCHAR_T
) *f
))
525 width
= read_int ((const UCHAR_T
**) &f
);
530 /* Check for type modifiers. */
534 /* ints are short ints or chars. */
546 /* A double `l' is equivalent to an `L'. */
548 flags
|= LONGDBL
| LONG
;
551 /* ints are long ints. */
556 /* doubles are long doubles, and ints are long long ints. */
557 flags
|= LONGDBL
| LONG
;
560 /* The `a' is used as a flag only if followed by `s', `S' or
562 if (*f
!= L_('s') && *f
!= L_('S') && *f
!= L_('['))
567 /* In __isoc99_*scanf %as, %aS and %a[ extension is not
569 if (s
->_flags2
& _IO_FLAGS2_SCANF_STD
)
574 /* String conversions (%s, %[) take a `char **'
575 arg and fill it in with a malloc'd pointer. */
579 flags
|= POSIX_MALLOC
;
587 if (need_longlong
&& sizeof (size_t) > sizeof (unsigned long int))
589 else if (sizeof (size_t) > sizeof (unsigned int))
593 if (need_longlong
&& sizeof (uintmax_t) > sizeof (unsigned long int))
595 else if (sizeof (uintmax_t) > sizeof (unsigned int))
599 if (need_longlong
&& sizeof (ptrdiff_t) > sizeof (long int))
601 else if (sizeof (ptrdiff_t) > sizeof (int))
605 /* Not a recognized modifier. Backup. */
610 /* End of the format string? */
611 if (__glibc_unlikely (*f
== L_('\0')))
614 /* Find the conversion specifier. */
616 if (skip_space
|| (fc
!= L_('[') && fc
!= L_('c')
617 && fc
!= L_('C') && fc
!= L_('n')))
619 /* Eat whitespace. */
620 int save_errno
= errno
;
623 /* We add the additional test for EOF here since otherwise
624 inchar will restore the old errno value which might be
625 EINTR but does not indicate an interrupt since nothing
626 was read at this time. */
627 if (__builtin_expect ((c
== EOF
|| inchar () == EOF
)
628 && errno
== EINTR
, 0))
631 __set_errno (save_errno
);
638 case L_('%'): /* Must match a literal '%'. */
640 if (__glibc_unlikely (c
== EOF
))
642 if (__glibc_unlikely (c
!= fc
))
644 ungetc_not_eof (c
, s
);
649 case L_('n'): /* Answer number of assignments done. */
650 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
651 with the 'n' conversion specifier. */
652 if (!(flags
& SUPPRESS
))
654 /* Don't count the read-ahead. */
655 if (need_longlong
&& (flags
& LONGDBL
))
656 *ARG (long long int *) = read_in
;
657 else if (need_long
&& (flags
& LONG
))
658 *ARG (long int *) = read_in
;
659 else if (flags
& SHORT
)
660 *ARG (short int *) = read_in
;
661 else if (!(flags
& CHAR
))
662 *ARG (int *) = read_in
;
664 *ARG (char *) = read_in
;
666 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
667 /* We have a severe problem here. The ISO C standard
668 contradicts itself in explaining the effect of the %n
669 format in `scanf'. While in ISO C:1990 and the ISO C
670 Amendement 1:1995 the result is described as
672 Execution of a %n directive does not effect the
673 assignment count returned at the completion of
674 execution of the f(w)scanf function.
676 in ISO C Corrigendum 1:1994 the following was added:
679 Add the following fourth example:
682 int d1, d2, n1, n2, i;
683 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
684 the value 123 is assigned to d1 and the value3 to n1.
685 Because %n can never get an input failure the value
686 of 3 is also assigned to n2. The value of d2 is not
687 affected. The value 3 is assigned to i.
689 We go for now with the historically correct code from ISO C,
690 i.e., we don't count the %n assignments. When it ever
691 should proof to be wrong just remove the #ifdef above. */
697 case L_('c'): /* Match characters. */
698 if ((flags
& LONG
) == 0)
703 #define STRING_ARG(Str, Type, Width) \
704 do if (!(flags & SUPPRESS)) \
706 if (flags & MALLOC) \
708 /* The string is to be stored in a malloc'd buffer. */ \
709 /* For %mS using char ** is actually wrong, but \
710 shouldn't make a difference on any arch glibc \
711 supports and would unnecessarily complicate \
713 strptr = ARG (char **); \
714 if (strptr == NULL) \
716 /* Allocate an initial buffer. */ \
718 *strptr = (char *) malloc (strsize * sizeof (Type)); \
719 Str = (Type *) *strptr; \
721 add_ptr_to_free (strptr); \
722 else if (flags & POSIX_MALLOC) \
729 Str = ARG (Type *); \
733 #ifdef COMPILE_WSCANF
734 STRING_ARG (str
, char, 100);
736 STRING_ARG (str
, char, (width
> 1024 ? 1024 : width
));
740 if (__glibc_unlikely (c
== EOF
))
743 #ifdef COMPILE_WSCANF
744 /* We have to convert the wide character(s) into multibyte
745 characters and store the result. */
746 memset (&state
, '\0', sizeof (state
));
752 if (!(flags
& SUPPRESS
) && (flags
& POSIX_MALLOC
)
753 && *strptr
+ strsize
- str
<= MB_LEN_MAX
)
755 /* We have to enlarge the buffer if the `m' flag
757 size_t strleng
= str
- *strptr
;
760 newstr
= (char *) realloc (*strptr
, strsize
* 2);
763 /* Can't allocate that much. Last-ditch effort. */
764 newstr
= (char *) realloc (*strptr
,
765 strleng
+ MB_LEN_MAX
);
768 /* c can't have `a' flag, only `m'. */
775 str
= newstr
+ strleng
;
776 strsize
= strleng
+ MB_LEN_MAX
;
782 str
= newstr
+ strleng
;
787 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
788 if (__glibc_unlikely (n
== (size_t) -1))
789 /* No valid wide character. */
792 /* Increment the output pointer. Even if we don't
796 while (--width
> 0 && inchar () != EOF
);
798 if (!(flags
& SUPPRESS
))
803 && (char *) str
== *strptr
+ strsize
)
805 /* Enlarge the buffer. */
808 + (strsize
>= width
? width
- 1 : strsize
);
810 str
= (char *) realloc (*strptr
, newsize
);
813 /* Can't allocate that much. Last-ditch
815 str
= (char *) realloc (*strptr
, strsize
+ 1);
818 /* c can't have `a' flag, only `m'. */
824 *strptr
= (char *) str
;
831 *strptr
= (char *) str
;
838 while (--width
> 0 && inchar () != EOF
);
841 while (--width
> 0 && inchar () != EOF
);
844 if (!(flags
& SUPPRESS
))
846 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
848 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
863 STRING_ARG (wstr
, wchar_t, (width
> 1024 ? 1024 : width
));
866 if (__glibc_unlikely (c
== EOF
))
869 #ifdef COMPILE_WSCANF
870 /* Just store the incoming wide characters. */
871 if (!(flags
& SUPPRESS
))
876 && wstr
== (wchar_t *) *strptr
+ strsize
)
879 = strsize
+ (strsize
> width
? width
- 1 : strsize
);
880 /* Enlarge the buffer. */
881 wstr
= (wchar_t *) realloc (*strptr
,
882 newsize
* sizeof (wchar_t));
885 /* Can't allocate that much. Last-ditch effort. */
886 wstr
= (wchar_t *) realloc (*strptr
,
891 /* C or lc can't have `a' flag, only `m'
898 *strptr
= (char *) wstr
;
905 *strptr
= (char *) wstr
;
912 while (--width
> 0 && inchar () != EOF
);
915 while (--width
> 0 && inchar () != EOF
);
918 /* We have to convert the multibyte input sequence to wide
923 memset (&cstate
, '\0', sizeof (cstate
));
927 /* This is what we present the mbrtowc function first. */
930 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
931 && wstr
== (wchar_t *) *strptr
+ strsize
)
934 = strsize
+ (strsize
> width
? width
- 1 : strsize
);
935 /* Enlarge the buffer. */
936 wstr
= (wchar_t *) realloc (*strptr
,
937 newsize
* sizeof (wchar_t));
940 /* Can't allocate that much. Last-ditch effort. */
941 wstr
= (wchar_t *) realloc (*strptr
,
943 * sizeof (wchar_t)));
946 /* C or lc can't have `a' flag, only `m' flag. */
952 *strptr
= (char *) wstr
;
959 *strptr
= (char *) wstr
;
969 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
972 if (n
== (size_t) -2)
974 /* Possibly correct character, just not enough
976 if (__glibc_unlikely (inchar () == EOF
))
983 if (__glibc_unlikely (n
!= 1))
986 /* We have a match. */
990 /* Advance the result pointer. */
993 while (--width
> 0 && inchar () != EOF
);
997 if (!(flags
& SUPPRESS
))
999 if ((flags
& MALLOC
) && wstr
- (wchar_t *) *strptr
!= strsize
)
1001 wchar_t *cp
= (wchar_t *) realloc (*strptr
,
1003 - (wchar_t *) *strptr
)
1004 * sizeof (wchar_t)));
1006 *strptr
= (char *) cp
;
1015 case L_('s'): /* Read a string. */
1016 if (!(flags
& LONG
))
1018 STRING_ARG (str
, char, 100);
1021 if (__glibc_unlikely (c
== EOF
))
1024 #ifdef COMPILE_WSCANF
1025 memset (&state
, '\0', sizeof (state
));
1032 ungetc_not_eof (c
, s
);
1036 #ifdef COMPILE_WSCANF
1037 /* This is quite complicated. We have to convert the
1038 wide characters into multibyte characters and then
1043 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
1044 && *strptr
+ strsize
- str
<= MB_LEN_MAX
)
1046 /* We have to enlarge the buffer if the `a' or `m'
1048 size_t strleng
= str
- *strptr
;
1051 newstr
= (char *) realloc (*strptr
, strsize
* 2);
1054 /* Can't allocate that much. Last-ditch
1056 newstr
= (char *) realloc (*strptr
,
1057 strleng
+ MB_LEN_MAX
);
1060 if (flags
& POSIX_MALLOC
)
1065 /* We lose. Oh well. Terminate the
1066 string and stop converting,
1067 so at least we don't skip any input. */
1068 ((char *) (*strptr
))[strleng
] = '\0';
1076 str
= newstr
+ strleng
;
1077 strsize
= strleng
+ MB_LEN_MAX
;
1083 str
= newstr
+ strleng
;
1088 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
,
1090 if (__glibc_unlikely (n
== (size_t) -1))
1093 assert (n
<= MB_LEN_MAX
);
1098 if (!(flags
& SUPPRESS
))
1101 if ((flags
& MALLOC
)
1102 && (char *) str
== *strptr
+ strsize
)
1104 /* Enlarge the buffer. */
1105 str
= (char *) realloc (*strptr
, 2 * strsize
);
1108 /* Can't allocate that much. Last-ditch
1110 str
= (char *) realloc (*strptr
, strsize
+ 1);
1113 if (flags
& POSIX_MALLOC
)
1118 /* We lose. Oh well. Terminate the
1119 string and stop converting,
1120 so at least we don't skip any input. */
1121 ((char *) (*strptr
))[strsize
- 1] = '\0';
1128 *strptr
= (char *) str
;
1135 *strptr
= (char *) str
;
1143 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
1145 if (!(flags
& SUPPRESS
))
1147 #ifdef COMPILE_WSCANF
1148 /* We have to emit the code to get into the initial
1150 char buf
[MB_LEN_MAX
];
1151 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
1152 if (n
> 0 && (flags
& MALLOC
)
1153 && str
+ n
>= *strptr
+ strsize
)
1155 /* Enlarge the buffer. */
1156 size_t strleng
= str
- *strptr
;
1159 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
1162 if (flags
& POSIX_MALLOC
)
1167 /* We lose. Oh well. Terminate the string
1168 and stop converting, so at least we don't
1170 ((char *) (*strptr
))[strleng
] = '\0';
1178 str
= newstr
+ strleng
;
1179 strsize
= strleng
+ n
+ 1;
1183 str
= __mempcpy (str
, buf
, n
);
1187 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
1189 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
1203 #ifndef COMPILE_WSCANF
1207 /* Wide character string. */
1208 STRING_ARG (wstr
, wchar_t, 100);
1211 if (__builtin_expect (c
== EOF
, 0))
1214 #ifndef COMPILE_WSCANF
1215 memset (&cstate
, '\0', sizeof (cstate
));
1222 ungetc_not_eof (c
, s
);
1226 #ifdef COMPILE_WSCANF
1228 if (!(flags
& SUPPRESS
))
1231 if ((flags
& MALLOC
)
1232 && wstr
== (wchar_t *) *strptr
+ strsize
)
1234 /* Enlarge the buffer. */
1235 wstr
= (wchar_t *) realloc (*strptr
,
1237 * sizeof (wchar_t));
1240 /* Can't allocate that much. Last-ditch
1242 wstr
= (wchar_t *) realloc (*strptr
,
1244 * sizeof (wchar_t));
1247 if (flags
& POSIX_MALLOC
)
1252 /* We lose. Oh well. Terminate the string
1253 and stop converting, so at least we don't
1255 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1262 *strptr
= (char *) wstr
;
1269 *strptr
= (char *) wstr
;
1285 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
1288 if (n
== (size_t) -2)
1290 /* Possibly correct character, just not enough
1292 if (__glibc_unlikely (inchar () == EOF
))
1299 if (__glibc_unlikely (n
!= 1))
1302 /* We have a match. */
1307 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
1308 && wstr
== (wchar_t *) *strptr
+ strsize
)
1310 /* Enlarge the buffer. */
1311 wstr
= (wchar_t *) realloc (*strptr
,
1313 * sizeof (wchar_t)));
1316 /* Can't allocate that much. Last-ditch effort. */
1317 wstr
= (wchar_t *) realloc (*strptr
,
1319 * sizeof (wchar_t)));
1322 if (flags
& POSIX_MALLOC
)
1327 /* We lose. Oh well. Terminate the
1328 string and stop converting, so at
1329 least we don't skip any input. */
1330 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1337 *strptr
= (char *) wstr
;
1344 *strptr
= (char *) wstr
;
1352 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
1354 if (!(flags
& SUPPRESS
))
1358 if ((flags
& MALLOC
) && wstr
- (wchar_t *) *strptr
!= strsize
)
1360 wchar_t *cp
= (wchar_t *) realloc (*strptr
,
1362 - (wchar_t *) *strptr
)
1363 * sizeof(wchar_t)));
1365 *strptr
= (char *) cp
;
1374 case L_('x'): /* Hexadecimal integer. */
1375 case L_('X'): /* Ditto. */
1379 case L_('o'): /* Octal integer. */
1383 case L_('u'): /* Unsigned decimal integer. */
1387 case L_('d'): /* Signed decimal integer. */
1389 flags
|= NUMBER_SIGNED
;
1392 case L_('i'): /* Generic number. */
1394 flags
|= NUMBER_SIGNED
;
1398 if (__glibc_unlikely (c
== EOF
))
1401 /* Check for a sign. */
1402 if (c
== L_('-') || c
== L_('+'))
1404 char_buffer_add (&charbuf
, c
);
1410 /* Look for a leading indication of base. */
1411 if (width
!= 0 && c
== L_('0'))
1416 char_buffer_add (&charbuf
, c
);
1419 if (width
!= 0 && TOLOWER (c
) == L_('x'))
1437 if (base
== 10 && __builtin_expect ((flags
& I18N
) != 0, 0))
1442 #ifdef COMPILE_WSCANF
1443 const wchar_t *wcdigits
[10];
1444 const wchar_t *wcdigits_extended
[10];
1446 const char *mbdigits
[10];
1447 const char *mbdigits_extended
[10];
1449 /* "to_inpunct" is a map from ASCII digits to their
1450 equivalent in locale. This is defined for locales
1451 which use an extra digits set. */
1452 wctrans_t map
= __wctrans ("to_inpunct");
1456 #ifdef COMPILE_WSCANF
1457 to_level
= _NL_CURRENT_WORD (LC_CTYPE
,
1458 _NL_CTYPE_INDIGITS_WC_LEN
) - 1;
1460 to_level
= (uint32_t) curctype
->values
[_NL_ITEM_INDEX (_NL_CTYPE_INDIGITS_MB_LEN
)].word
- 1;
1463 /* Get the alternative digit forms if there are any. */
1464 if (__glibc_unlikely (map
!= NULL
))
1466 /* Adding new level for extra digits set in locale file. */
1469 for (n
= 0; n
< 10; ++n
)
1471 #ifdef COMPILE_WSCANF
1472 wcdigits
[n
] = (const wchar_t *)
1473 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1475 wchar_t *wc_extended
= (wchar_t *)
1476 alloca ((to_level
+ 2) * sizeof (wchar_t));
1477 __wmemcpy (wc_extended
, wcdigits
[n
], to_level
);
1478 wc_extended
[to_level
] = __towctrans (L
'0' + n
, map
);
1479 wc_extended
[to_level
+ 1] = '\0';
1480 wcdigits_extended
[n
] = wc_extended
;
1483 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1485 /* Get the equivalent wide char in map. */
1486 wint_t extra_wcdigit
= __towctrans (L
'0' + n
, map
);
1488 /* Convert it to multibyte representation. */
1490 memset (&state
, '\0', sizeof (state
));
1492 char extra_mbdigit
[MB_LEN_MAX
];
1494 = __wcrtomb (extra_mbdigit
, extra_wcdigit
, &state
);
1496 if (mblen
== (size_t) -1)
1498 /* Ignore this new level. */
1503 /* Calculate the length of mbdigits[n]. */
1504 const char *last_char
= mbdigits
[n
];
1505 for (level
= 0; level
< to_level
; ++level
)
1506 last_char
= strchr (last_char
, '\0') + 1;
1508 size_t mbdigits_len
= last_char
- mbdigits
[n
];
1510 /* Allocate memory for extended multibyte digit. */
1512 mb_extended
= (char *) alloca (mbdigits_len
+ mblen
+ 1);
1514 /* And get the mbdigits + extra_digit string. */
1515 *(char *) __mempcpy (__mempcpy (mb_extended
, mbdigits
[n
],
1517 extra_mbdigit
, mblen
) = '\0';
1518 mbdigits_extended
[n
] = mb_extended
;
1523 /* Read the number into workspace. */
1524 while (c
!= EOF
&& width
!= 0)
1526 /* In this round we get the pointer to the digit strings
1527 and also perform the first round of comparisons. */
1528 for (n
= 0; n
< 10; ++n
)
1530 /* Get the string for the digits with value N. */
1531 #ifdef COMPILE_WSCANF
1533 /* wcdigits_extended[] is fully set in the loop
1534 above, but the test for "map != NULL" is done
1535 inside the loop here and outside the loop there. */
1536 DIAG_PUSH_NEEDS_COMMENT
;
1537 DIAG_IGNORE_NEEDS_COMMENT (4.7, "-Wmaybe-uninitialized");
1539 if (__glibc_unlikely (map
!= NULL
))
1540 wcdigits
[n
] = wcdigits_extended
[n
];
1542 wcdigits
[n
] = (const wchar_t *)
1543 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1544 wcdigits
[n
] += from_level
;
1546 DIAG_POP_NEEDS_COMMENT
;
1548 if (c
== (wint_t) *wcdigits
[n
])
1550 to_level
= from_level
;
1554 /* Advance the pointer to the next string. */
1558 int avail
= width
> 0 ? width
: INT_MAX
;
1560 if (__glibc_unlikely (map
!= NULL
))
1561 mbdigits
[n
] = mbdigits_extended
[n
];
1564 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1566 for (level
= 0; level
< from_level
; level
++)
1567 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1570 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1572 if (*++cmpp
== '\0')
1576 if (avail
== 0 || inchar () == EOF
)
1586 to_level
= from_level
;
1590 /* We are pushing all read characters back. */
1591 if (cmpp
> mbdigits
[n
])
1594 while (--cmpp
> mbdigits
[n
])
1595 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1596 c
= (unsigned char) *cmpp
;
1599 /* Advance the pointer to the next string. */
1600 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1606 /* Have not yet found the digit. */
1607 for (level
= from_level
+ 1; level
<= to_level
; ++level
)
1609 /* Search all ten digits of this level. */
1610 for (n
= 0; n
< 10; ++n
)
1612 #ifdef COMPILE_WSCANF
1613 if (c
== (wint_t) *wcdigits
[n
])
1616 /* Advance the pointer to the next string. */
1620 int avail
= width
> 0 ? width
: INT_MAX
;
1623 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1625 if (*++cmpp
== '\0')
1629 if (avail
== 0 || inchar () == EOF
)
1642 /* We are pushing all read characters back. */
1643 if (cmpp
> mbdigits
[n
])
1646 while (--cmpp
> mbdigits
[n
])
1647 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1648 c
= (unsigned char) *cmpp
;
1651 /* Advance the pointer to the next string. */
1652 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1668 else if (flags
& GROUP
)
1670 /* Try matching against the thousands separator. */
1671 #ifdef COMPILE_WSCANF
1675 const char *cmpp
= thousands
;
1676 int avail
= width
> 0 ? width
: INT_MAX
;
1678 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1680 char_buffer_add (&charbuf
, c
);
1681 if (*++cmpp
== '\0')
1685 if (avail
== 0 || inchar () == EOF
)
1691 if (char_buffer_error (&charbuf
))
1693 __set_errno (ENOMEM
);
1700 /* We are pushing all read characters back. */
1701 if (cmpp
> thousands
)
1703 charbuf
.current
-= cmpp
- thousands
;
1705 while (--cmpp
> thousands
)
1706 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1707 c
= (unsigned char) *cmpp
;
1715 /* The last thousands character will be added back by
1716 the char_buffer_add below. */
1723 char_buffer_add (&charbuf
, c
);
1731 /* Read the number into workspace. */
1732 while (c
!= EOF
&& width
!= 0)
1739 else if (!ISDIGIT (c
) || (int) (c
- L_('0')) >= base
)
1741 if (base
== 10 && (flags
& GROUP
))
1743 /* Try matching against the thousands separator. */
1744 #ifdef COMPILE_WSCANF
1748 const char *cmpp
= thousands
;
1749 int avail
= width
> 0 ? width
: INT_MAX
;
1751 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1753 char_buffer_add (&charbuf
, c
);
1754 if (*++cmpp
== '\0')
1758 if (avail
== 0 || inchar () == EOF
)
1764 if (char_buffer_error (&charbuf
))
1766 __set_errno (ENOMEM
);
1773 /* We are pushing all read characters back. */
1774 if (cmpp
> thousands
)
1776 charbuf
.current
-= cmpp
- thousands
;
1778 while (--cmpp
> thousands
)
1779 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1780 c
= (unsigned char) *cmpp
;
1788 /* The last thousands character will be added back by
1789 the char_buffer_add below. */
1796 char_buffer_add (&charbuf
, c
);
1803 if (char_buffer_error (&charbuf
))
1805 __set_errno (ENOMEM
);
1810 if (char_buffer_size (&charbuf
) == 0
1811 || (char_buffer_size (&charbuf
) == 1
1812 && (char_buffer_start (&charbuf
)[0] == L_('+')
1813 || char_buffer_start (&charbuf
)[0] == L_('-'))))
1815 /* There was no number. If we are supposed to read a pointer
1816 we must recognize "(nil)" as well. */
1817 if (__builtin_expect (char_buffer_size (&charbuf
) == 0
1818 && (flags
& READ_POINTER
)
1819 && (width
< 0 || width
>= 5)
1821 && TOLOWER (inchar ()) == L_('n')
1822 && TOLOWER (inchar ()) == L_('i')
1823 && TOLOWER (inchar ()) == L_('l')
1824 && inchar () == L_(')'), 1))
1825 /* We must produce the value of a NULL pointer. A single
1826 '0' digit is enough. */
1827 char_buffer_add (&charbuf
, L_('0'));
1830 /* The last read character is not part of the number
1838 /* The just read character is not part of the number anymore. */
1841 /* Convert the number. */
1842 char_buffer_add (&charbuf
, L_('\0'));
1843 if (char_buffer_error (&charbuf
))
1845 __set_errno (ENOMEM
);
1849 if (need_longlong
&& (flags
& LONGDBL
))
1851 if (flags
& NUMBER_SIGNED
)
1852 num
.q
= __strtoll_internal
1853 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1855 num
.uq
= __strtoull_internal
1856 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1860 if (flags
& NUMBER_SIGNED
)
1861 num
.l
= __strtol_internal
1862 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1864 num
.ul
= __strtoul_internal
1865 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1867 if (__glibc_unlikely (char_buffer_start (&charbuf
) == tw
))
1870 if (!(flags
& SUPPRESS
))
1872 if (flags
& NUMBER_SIGNED
)
1874 if (need_longlong
&& (flags
& LONGDBL
))
1875 *ARG (LONGLONG
int *) = num
.q
;
1876 else if (need_long
&& (flags
& LONG
))
1877 *ARG (long int *) = num
.l
;
1878 else if (flags
& SHORT
)
1879 *ARG (short int *) = (short int) num
.l
;
1880 else if (!(flags
& CHAR
))
1881 *ARG (int *) = (int) num
.l
;
1883 *ARG (signed char *) = (signed char) num
.ul
;
1887 if (need_longlong
&& (flags
& LONGDBL
))
1888 *ARG (unsigned LONGLONG
int *) = num
.uq
;
1889 else if (need_long
&& (flags
& LONG
))
1890 *ARG (unsigned long int *) = num
.ul
;
1891 else if (flags
& SHORT
)
1892 *ARG (unsigned short int *)
1893 = (unsigned short int) num
.ul
;
1894 else if (!(flags
& CHAR
))
1895 *ARG (unsigned int *) = (unsigned int) num
.ul
;
1897 *ARG (unsigned char *) = (unsigned char) num
.ul
;
1903 case L_('e'): /* Floating-point numbers. */
1914 if (__glibc_unlikely (c
== EOF
))
1917 got_digit
= got_dot
= got_e
= got_sign
= 0;
1919 /* Check for a sign. */
1920 if (c
== L_('-') || c
== L_('+'))
1923 char_buffer_add (&charbuf
, c
);
1924 if (__glibc_unlikely (width
== 0 || inchar () == EOF
))
1925 /* EOF is only an input error before we read any chars. */
1931 /* Take care for the special arguments "nan" and "inf". */
1932 if (TOLOWER (c
) == L_('n'))
1935 char_buffer_add (&charbuf
, c
);
1936 if (__builtin_expect (width
== 0
1938 || TOLOWER (c
) != L_('a'), 0))
1942 char_buffer_add (&charbuf
, c
);
1943 if (__builtin_expect (width
== 0
1945 || TOLOWER (c
) != L_('n'), 0))
1949 char_buffer_add (&charbuf
, c
);
1953 else if (TOLOWER (c
) == L_('i'))
1955 /* Maybe "inf" or "infinity". */
1956 char_buffer_add (&charbuf
, c
);
1957 if (__builtin_expect (width
== 0
1959 || TOLOWER (c
) != L_('n'), 0))
1963 char_buffer_add (&charbuf
, c
);
1964 if (__builtin_expect (width
== 0
1966 || TOLOWER (c
) != L_('f'), 0))
1970 char_buffer_add (&charbuf
, c
);
1971 /* It is as least "inf". */
1972 if (width
!= 0 && inchar () != EOF
)
1974 if (TOLOWER (c
) == L_('i'))
1978 /* Now we have to read the rest as well. */
1979 char_buffer_add (&charbuf
, c
);
1980 if (__builtin_expect (width
== 0
1982 || TOLOWER (c
) != L_('n'), 0))
1986 char_buffer_add (&charbuf
, c
);
1987 if (__builtin_expect (width
== 0
1989 || TOLOWER (c
) != L_('i'), 0))
1993 char_buffer_add (&charbuf
, c
);
1994 if (__builtin_expect (width
== 0
1996 || TOLOWER (c
) != L_('t'), 0))
2000 char_buffer_add (&charbuf
, c
);
2001 if (__builtin_expect (width
== 0
2003 || TOLOWER (c
) != L_('y'), 0))
2007 char_buffer_add (&charbuf
, c
);
2017 if (width
!= 0 && c
== L_('0'))
2019 char_buffer_add (&charbuf
, c
);
2023 if (width
!= 0 && TOLOWER (c
) == L_('x'))
2025 /* It is a number in hexadecimal format. */
2026 char_buffer_add (&charbuf
, c
);
2028 flags
|= HEXA_FLOAT
;
2031 /* Grouping is not allowed. */
2043 if (char_buffer_error (&charbuf
))
2045 __set_errno (ENOMEM
);
2051 char_buffer_add (&charbuf
, c
);
2054 else if (!got_e
&& (flags
& HEXA_FLOAT
) && ISXDIGIT (c
))
2056 char_buffer_add (&charbuf
, c
);
2059 else if (got_e
&& charbuf
.current
[-1] == exp_char
2060 && (c
== L_('-') || c
== L_('+')))
2061 char_buffer_add (&charbuf
, c
);
2062 else if (got_digit
&& !got_e
2063 && (CHAR_T
) TOLOWER (c
) == exp_char
)
2065 char_buffer_add (&charbuf
, exp_char
);
2066 got_e
= got_dot
= 1;
2070 #ifdef COMPILE_WSCANF
2071 if (! got_dot
&& c
== decimal
)
2073 char_buffer_add (&charbuf
, c
);
2076 else if ((flags
& GROUP
) != 0 && ! got_dot
&& c
== thousands
)
2077 char_buffer_add (&charbuf
, c
);
2080 /* The last read character is not part of the number
2086 const char *cmpp
= decimal
;
2087 int avail
= width
> 0 ? width
: INT_MAX
;
2091 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
2092 if (*++cmpp
== '\0')
2096 if (avail
== 0 || inchar () == EOF
)
2104 /* Add all the characters. */
2105 for (cmpp
= decimal
; *cmpp
!= '\0'; ++cmpp
)
2106 char_buffer_add (&charbuf
, (unsigned char) *cmpp
);
2113 /* Figure out whether it is a thousands separator.
2114 There is one problem: we possibly read more than
2115 one character. We cannot push them back but since
2116 we know that parts of the `decimal' string matched,
2117 we can compare against it. */
2118 const char *cmp2p
= thousands
;
2120 if ((flags
& GROUP
) != 0 && ! got_dot
)
2122 while (cmp2p
- thousands
< cmpp
- decimal
2123 && *cmp2p
== decimal
[cmp2p
- thousands
])
2125 if (cmp2p
- thousands
== cmpp
- decimal
)
2127 while ((unsigned char) *cmp2p
== c
&& avail
>= 0)
2128 if (*++cmp2p
== '\0')
2132 if (avail
== 0 || inchar () == EOF
)
2139 if (cmp2p
!= NULL
&& *cmp2p
== '\0')
2141 /* Add all the characters. */
2142 for (cmpp
= thousands
; *cmpp
!= '\0'; ++cmpp
)
2143 char_buffer_add (&charbuf
, (unsigned char) *cmpp
);
2149 /* The last read character is not part of the number
2158 if (width
== 0 || inchar () == EOF
)
2165 if (char_buffer_error (&charbuf
))
2167 __set_errno (ENOMEM
);
2173 if (__builtin_expect ((flags
& I18N
) != 0, 0)
2174 /* Hexadecimal floats make no sense, fixing localized
2175 digits with ASCII letters. */
2176 && !(flags
& HEXA_FLOAT
)
2177 /* Minimum requirement. */
2178 && (char_buffer_size (&charbuf
) == got_sign
|| got_dot
)
2179 && (map
= __wctrans ("to_inpunct")) != NULL
)
2181 /* Reget the first character. */
2184 /* Localized digits, decimal points, and thousands
2186 wint_t wcdigits
[12];
2188 /* First get decimal equivalent to check if we read it
2190 wcdigits
[11] = __towctrans (L
'.', map
);
2192 /* If we have not read any character or have just read
2193 locale decimal point which matches the decimal point
2194 for localized FP numbers, then we may have localized
2195 digits. Note, we test GOT_DOT above. */
2196 #ifdef COMPILE_WSCANF
2197 if (char_buffer_size (&charbuf
) == got_sign
2198 || (char_buffer_size (&charbuf
) == got_sign
+ 1
2199 && wcdigits
[11] == decimal
))
2201 char mbdigits
[12][MB_LEN_MAX
+ 1];
2204 memset (&state
, '\0', sizeof (state
));
2206 bool match_so_far
= char_buffer_size (&charbuf
) == got_sign
;
2207 size_t mblen
= __wcrtomb (mbdigits
[11], wcdigits
[11], &state
);
2208 if (mblen
!= (size_t) -1)
2210 mbdigits
[11][mblen
] = '\0';
2212 (char_buffer_size (&charbuf
) == strlen (decimal
) + got_sign
2213 && strcmp (decimal
, mbdigits
[11]) == 0);
2217 size_t decimal_len
= strlen (decimal
);
2218 /* This should always be the case but the data comes
2220 if (decimal_len
<= MB_LEN_MAX
)
2222 match_so_far
|= (char_buffer_size (&charbuf
)
2223 == decimal_len
+ got_sign
);
2224 memcpy (mbdigits
[11], decimal
, decimal_len
+ 1);
2227 match_so_far
= false;
2233 bool have_locthousands
= (flags
& GROUP
) != 0;
2235 /* Now get the digits and the thousands-sep equivalents. */
2236 for (int n
= 0; n
< 11; ++n
)
2239 wcdigits
[n
] = __towctrans (L
'0' + n
, map
);
2242 wcdigits
[10] = __towctrans (L
',', map
);
2243 have_locthousands
&= wcdigits
[10] != L
'\0';
2246 #ifndef COMPILE_WSCANF
2247 memset (&state
, '\0', sizeof (state
));
2249 size_t mblen
= __wcrtomb (mbdigits
[n
], wcdigits
[n
],
2251 if (mblen
== (size_t) -1)
2255 if (have_locthousands
)
2257 size_t thousands_len
= strlen (thousands
);
2258 if (thousands_len
<= MB_LEN_MAX
)
2259 memcpy (mbdigits
[10], thousands
,
2262 have_locthousands
= false;
2266 /* Ignore checking against localized digits. */
2270 mbdigits
[n
][mblen
] = '\0';
2274 /* Start checking against localized digits, if
2275 conversion is done correctly. */
2278 if (char_buffer_error (&charbuf
))
2280 __set_errno (ENOMEM
);
2284 if (got_e
&& charbuf
.current
[-1] == exp_char
2285 && (c
== L_('-') || c
== L_('+')))
2286 char_buffer_add (&charbuf
, c
);
2287 else if (char_buffer_size (&charbuf
) > got_sign
&& !got_e
2288 && (CHAR_T
) TOLOWER (c
) == exp_char
)
2290 char_buffer_add (&charbuf
, exp_char
);
2291 got_e
= got_dot
= 1;
2295 /* Check against localized digits, decimal point,
2296 and thousands separator. */
2298 for (n
= 0; n
< 12; ++n
)
2300 #ifdef COMPILE_WSCANF
2301 if (c
== wcdigits
[n
])
2304 char_buffer_add (&charbuf
, L_('0') + n
);
2305 else if (n
== 11 && !got_dot
)
2307 char_buffer_add (&charbuf
, decimal
);
2310 else if (n
== 10 && have_locthousands
2312 char_buffer_add (&charbuf
, thousands
);
2314 /* The last read character is not part
2315 of the number anymore. */
2321 const char *cmpp
= mbdigits
[n
];
2322 int avail
= width
> 0 ? width
: INT_MAX
;
2324 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
2325 if (*++cmpp
== '\0')
2329 if (avail
== 0 || inchar () == EOF
)
2339 char_buffer_add (&charbuf
, L_('0') + n
);
2340 else if (n
== 11 && !got_dot
)
2342 /* Add all the characters. */
2343 for (cmpp
= decimal
; *cmpp
!= '\0';
2345 char_buffer_add (&charbuf
,
2346 (unsigned char) *cmpp
);
2350 else if (n
== 10 && (flags
& GROUP
) != 0
2353 /* Add all the characters. */
2354 for (cmpp
= thousands
; *cmpp
!= '\0';
2356 char_buffer_add (&charbuf
,
2357 (unsigned char) *cmpp
);
2360 /* The last read character is not part
2361 of the number anymore. */
2367 /* We are pushing all read characters back. */
2368 if (cmpp
> mbdigits
[n
])
2371 while (--cmpp
> mbdigits
[n
])
2372 ungetc_not_eof ((unsigned char) *cmpp
, s
);
2373 c
= (unsigned char) *cmpp
;
2380 /* The last read character is not part
2381 of the number anymore. */
2387 if (width
== 0 || inchar () == EOF
)
2395 #ifndef COMPILE_WSCANF
2401 if (char_buffer_error (&charbuf
))
2403 __set_errno (ENOMEM
);
2408 /* Have we read any character? If we try to read a number
2409 in hexadecimal notation and we have read only the `0x'
2410 prefix this is an error. */
2411 if (__glibc_unlikely (char_buffer_size (&charbuf
) == got_sign
2412 || ((flags
& HEXA_FLOAT
)
2413 && (char_buffer_size (&charbuf
)
2418 /* Convert the number. */
2419 char_buffer_add (&charbuf
, L_('\0'));
2420 if (char_buffer_error (&charbuf
))
2422 __set_errno (ENOMEM
);
2426 if ((flags
& LONGDBL
) && !__ldbl_is_dbl
)
2428 long double d
= __strtold_internal
2429 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2430 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2431 *ARG (long double *) = d
;
2433 else if (flags
& (LONG
| LONGDBL
))
2435 double d
= __strtod_internal
2436 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2437 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2438 *ARG (double *) = d
;
2442 float d
= __strtof_internal
2443 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2444 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2448 if (__glibc_unlikely (tw
== char_buffer_start (&charbuf
)))
2451 if (!(flags
& SUPPRESS
))
2455 case L_('['): /* Character class. */
2457 STRING_ARG (wstr
, wchar_t, 100);
2459 STRING_ARG (str
, char, 100);
2470 /* There is no width given so there is also no limit on the
2471 number of characters we read. Therefore we set width to
2472 a very high value to make the algorithm easier. */
2475 #ifdef COMPILE_WSCANF
2476 /* Find the beginning and the end of the scanlist. We are not
2477 creating a lookup table since it would have to be too large.
2478 Instead we search each time through the string. This is not
2479 a constant lookup time but who uses this feature deserves to
2481 tw
= (wchar_t *) f
; /* Marks the beginning. */
2486 while ((fc
= *f
++) != L
'\0' && fc
!= L
']');
2488 if (__glibc_unlikely (fc
== L
'\0'))
2490 wchar_t *twend
= (wchar_t *) f
- 1;
2492 /* Fill WP with byte flags indexed by character.
2493 We will use this flag map for matching input characters. */
2494 if (!scratch_buffer_set_array_size
2495 (&charbuf
.scratch
, UCHAR_MAX
+ 1, 1))
2500 memset (charbuf
.scratch
.data
, '\0', UCHAR_MAX
+ 1);
2503 if (fc
== ']' || fc
== '-')
2505 /* If ] or - appears before any char in the set, it is not
2506 the terminator or separator, but the first char in the
2508 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2512 while ((fc
= *f
++) != '\0' && fc
!= ']')
2513 if (fc
== '-' && *f
!= '\0' && *f
!= ']'
2514 && (unsigned char) f
[-2] <= (unsigned char) *f
)
2516 /* Add all characters from the one before the '-'
2517 up to (but not including) the next format char. */
2518 for (fc
= (unsigned char) f
[-2]; fc
< (unsigned char) *f
; ++fc
)
2519 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2522 /* Add the character to the flag map. */
2523 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2525 if (__glibc_unlikely (fc
== '\0'))
2531 size_t now
= read_in
;
2532 #ifdef COMPILE_WSCANF
2533 if (__glibc_unlikely (inchar () == WEOF
))
2540 /* Test whether it's in the scanlist. */
2542 while (runp
< twend
)
2544 if (runp
[0] == L
'-' && runp
[1] != '\0'
2545 && runp
+ 1 != twend
2547 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2549 /* Match against all characters in between the
2550 first and last character of the sequence. */
2553 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2554 if ((wint_t) wc
== c
)
2557 if (wc
<= runp
[1] && !not_in
)
2559 if (wc
<= runp
[1] && not_in
)
2561 /* The current character is not in the
2571 if ((wint_t) *runp
== c
&& !not_in
)
2573 if ((wint_t) *runp
== c
&& not_in
)
2583 if (runp
== twend
&& !not_in
)
2589 if (!(flags
& SUPPRESS
))
2593 if ((flags
& MALLOC
)
2594 && wstr
== (wchar_t *) *strptr
+ strsize
)
2596 /* Enlarge the buffer. */
2597 wstr
= (wchar_t *) realloc (*strptr
,
2599 * sizeof (wchar_t));
2602 /* Can't allocate that much. Last-ditch
2605 realloc (*strptr
, (strsize
+ 1)
2606 * sizeof (wchar_t));
2609 if (flags
& POSIX_MALLOC
)
2614 /* We lose. Oh well. Terminate the string
2615 and stop converting, so at least we don't
2617 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2624 *strptr
= (char *) wstr
;
2631 *strptr
= (char *) wstr
;
2638 while (--width
> 0 && inchar () != WEOF
);
2641 char buf
[MB_LEN_MAX
];
2645 if (__glibc_unlikely (inchar () == EOF
))
2648 memset (&cstate
, '\0', sizeof (cstate
));
2652 if (((char *) charbuf
.scratch
.data
)[c
] == not_in
)
2654 ungetc_not_eof (c
, s
);
2659 if (!(flags
& SUPPRESS
))
2663 /* Convert it into a wide character. */
2665 n
= __mbrtowc (wstr
, buf
, 1, &cstate
);
2667 if (n
== (size_t) -2)
2669 /* Possibly correct character, just not enough
2672 assert (cnt
< MB_LEN_MAX
);
2678 if ((flags
& MALLOC
)
2679 && wstr
== (wchar_t *) *strptr
+ strsize
)
2681 /* Enlarge the buffer. */
2682 wstr
= (wchar_t *) realloc (*strptr
,
2684 * sizeof (wchar_t)));
2687 /* Can't allocate that much. Last-ditch
2690 realloc (*strptr
, ((strsize
+ 1)
2691 * sizeof (wchar_t)));
2694 if (flags
& POSIX_MALLOC
)
2699 /* We lose. Oh well. Terminate the
2700 string and stop converting,
2701 so at least we don't skip any input. */
2702 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2709 *strptr
= (char *) wstr
;
2716 *strptr
= (char *) wstr
;
2726 while (inchar () != EOF
);
2728 if (__glibc_unlikely (cnt
!= 0))
2729 /* We stopped in the middle of recognizing another
2730 character. That's a problem. */
2734 if (__glibc_unlikely (now
== read_in
))
2735 /* We haven't succesfully read any character. */
2738 if (!(flags
& SUPPRESS
))
2742 if ((flags
& MALLOC
)
2743 && wstr
- (wchar_t *) *strptr
!= strsize
)
2745 wchar_t *cp
= (wchar_t *)
2746 realloc (*strptr
, ((wstr
- (wchar_t *) *strptr
)
2747 * sizeof(wchar_t)));
2749 *strptr
= (char *) cp
;
2758 size_t now
= read_in
;
2760 if (__glibc_unlikely (inchar () == EOF
))
2763 #ifdef COMPILE_WSCANF
2765 memset (&state
, '\0', sizeof (state
));
2772 /* Test whether it's in the scanlist. */
2774 while (runp
< twend
)
2776 if (runp
[0] == L
'-' && runp
[1] != '\0'
2777 && runp
+ 1 != twend
2779 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2781 /* Match against all characters in between the
2782 first and last character of the sequence. */
2785 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2786 if ((wint_t) wc
== c
)
2789 if (wc
<= runp
[1] && !not_in
)
2791 if (wc
<= runp
[1] && not_in
)
2793 /* The current character is not in the
2803 if ((wint_t) *runp
== c
&& !not_in
)
2805 if ((wint_t) *runp
== c
&& not_in
)
2815 if (runp
== twend
&& !not_in
)
2821 if (!(flags
& SUPPRESS
))
2823 if ((flags
& MALLOC
)
2824 && *strptr
+ strsize
- str
<= MB_LEN_MAX
)
2826 /* Enlarge the buffer. */
2827 size_t strleng
= str
- *strptr
;
2830 newstr
= (char *) realloc (*strptr
, 2 * strsize
);
2833 /* Can't allocate that much. Last-ditch
2835 newstr
= (char *) realloc (*strptr
,
2836 strleng
+ MB_LEN_MAX
);
2839 if (flags
& POSIX_MALLOC
)
2844 /* We lose. Oh well. Terminate the string
2845 and stop converting, so at least we don't
2847 ((char *) (*strptr
))[strleng
] = '\0';
2855 str
= newstr
+ strleng
;
2856 strsize
= strleng
+ MB_LEN_MAX
;
2862 str
= newstr
+ strleng
;
2868 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
2869 if (__glibc_unlikely (n
== (size_t) -1))
2872 assert (n
<= MB_LEN_MAX
);
2875 while (--width
> 0 && inchar () != WEOF
);
2880 if (((char *) charbuf
.scratch
.data
)[c
] == not_in
)
2882 ungetc_not_eof (c
, s
);
2887 if (!(flags
& SUPPRESS
))
2890 if ((flags
& MALLOC
)
2891 && (char *) str
== *strptr
+ strsize
)
2893 /* Enlarge the buffer. */
2894 size_t newsize
= 2 * strsize
;
2897 str
= (char *) realloc (*strptr
, newsize
);
2900 /* Can't allocate that much. Last-ditch
2902 if (newsize
> strsize
+ 1)
2904 newsize
= strsize
+ 1;
2907 if (flags
& POSIX_MALLOC
)
2912 /* We lose. Oh well. Terminate the
2913 string and stop converting,
2914 so at least we don't skip any input. */
2915 ((char *) (*strptr
))[strsize
- 1] = '\0';
2922 *strptr
= (char *) str
;
2929 while (--width
> 0 && inchar () != EOF
);
2932 if (__glibc_unlikely (now
== read_in
))
2933 /* We haven't succesfully read any character. */
2936 if (!(flags
& SUPPRESS
))
2938 #ifdef COMPILE_WSCANF
2939 /* We have to emit the code to get into the initial
2941 char buf
[MB_LEN_MAX
];
2942 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
2943 if (n
> 0 && (flags
& MALLOC
)
2944 && str
+ n
>= *strptr
+ strsize
)
2946 /* Enlarge the buffer. */
2947 size_t strleng
= str
- *strptr
;
2950 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
2953 if (flags
& POSIX_MALLOC
)
2958 /* We lose. Oh well. Terminate the string
2959 and stop converting, so at least we don't
2961 ((char *) (*strptr
))[strleng
] = '\0';
2969 str
= newstr
+ strleng
;
2970 strsize
= strleng
+ n
+ 1;
2974 str
= __mempcpy (str
, buf
, n
);
2978 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
2980 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
2991 case L_('p'): /* Generic pointer. */
2993 /* A PTR must be the same size as a `long int'. */
2994 flags
&= ~(SHORT
|LONGDBL
);
2997 flags
|= READ_POINTER
;
3001 /* If this is an unknown format character punt. */
3006 /* The last thing we saw int the format string was a white space.
3007 Consume the last white spaces. */
3012 while (ISSPACE (c
));
3017 /* Unlock stream. */
3020 scratch_buffer_free (&charbuf
.scratch
);
3024 if (__glibc_unlikely (done
== EOF
))
3026 if (__glibc_unlikely (ptrs_to_free
!= NULL
))
3028 struct ptrs_to_free
*p
= ptrs_to_free
;
3031 for (size_t cnt
= 0; cnt
< p
->count
; ++cnt
)
3033 free (*p
->ptrs
[cnt
]);
3034 *p
->ptrs
[cnt
] = NULL
;
3041 else if (__glibc_unlikely (strptr
!= NULL
))
3049 #ifdef COMPILE_WSCANF
3051 __vfwscanf (FILE *s
, const wchar_t *format
, va_list argptr
)
3053 return _IO_vfwscanf (s
, format
, argptr
, NULL
);
3055 ldbl_weak_alias (__vfwscanf
, vfwscanf
)
3058 ___vfscanf (FILE *s
, const char *format
, va_list argptr
)
3060 return _IO_vfscanf_internal (s
, format
, argptr
, NULL
);
3062 ldbl_strong_alias (_IO_vfscanf_internal
, _IO_vfscanf
)
3063 ldbl_hidden_def (_IO_vfscanf_internal
, _IO_vfscanf
)
3064 ldbl_strong_alias (___vfscanf
, __vfscanf
)
3065 ldbl_hidden_def (___vfscanf
, __vfscanf
)
3066 ldbl_weak_alias (___vfscanf
, vfscanf
)