1 /* Copyright (C) 1991-2017 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>
78 #define va_list _IO_va_list
81 # define ungetc(c, s) ((void) (c == WEOF \
83 _IO_sputbackwc (s, c))))
84 # define ungetc_not_eof(c, s) ((void) (--read_in, \
85 _IO_sputbackwc (s, c)))
86 # define inchar() (c == WEOF ? ((errno = inchar_errno), WEOF) \
87 : ((c = _IO_getwc_unlocked (s)), \
90 : (size_t) (inchar_errno = errno)), c))
92 # define ISSPACE(Ch) iswspace (Ch)
93 # define ISDIGIT(Ch) iswdigit (Ch)
94 # define ISXDIGIT(Ch) iswxdigit (Ch)
95 # define TOLOWER(Ch) towlower (Ch)
96 # define ORIENT if (_IO_fwide (s, 1) != 1) return WEOF
97 # define __strtoll_internal __wcstoll_internal
98 # define __strtoull_internal __wcstoull_internal
99 # define __strtol_internal __wcstol_internal
100 # define __strtoul_internal __wcstoul_internal
101 # define __strtold_internal __wcstold_internal
102 # define __strtod_internal __wcstod_internal
103 # define __strtof_internal __wcstof_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 { \
140 __set_errno (EILSEQ); \
143 #define conv_error() do { \
147 #define input_error() do { \
149 if (done == 0) done = EOF; \
152 #define add_ptr_to_free(ptr) \
155 if (ptrs_to_free == NULL \
156 || ptrs_to_free->count == (sizeof (ptrs_to_free->ptrs) \
157 / sizeof (ptrs_to_free->ptrs[0]))) \
159 struct ptrs_to_free *new_ptrs = alloca (sizeof (*ptrs_to_free)); \
160 new_ptrs->count = 0; \
161 new_ptrs->next = ptrs_to_free; \
162 ptrs_to_free = new_ptrs; \
164 ptrs_to_free->ptrs[ptrs_to_free->count++] = (ptr); \
167 #define ARGCHECK(s, format) \
170 /* Check file argument for consistence. */ \
171 CHECK_FILE (s, EOF); \
172 if (s->_flags & _IO_NO_READS) \
174 __set_errno (EBADF); \
177 else if (format == NULL) \
183 #define LOCK_STREAM(S) \
184 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, (S)); \
186 #define UNLOCK_STREAM(S) \
187 _IO_funlockfile (S); \
188 __libc_cleanup_region_end (0)
193 struct ptrs_to_free
*next
;
200 struct scratch_buffer scratch
;
203 /* Returns a pointer to the first CHAR_T object in the buffer. Only
204 valid if char_buffer_add (BUFFER, CH) has been called and
205 char_buffer_error (BUFFER) is false. */
206 static inline CHAR_T
*
207 char_buffer_start (const struct char_buffer
*buffer
)
209 return (CHAR_T
*) buffer
->scratch
.data
;
212 /* Returns the number of CHAR_T objects in the buffer. Only valid if
213 char_buffer_error (BUFFER) is false. */
215 char_buffer_size (const struct char_buffer
*buffer
)
217 return buffer
->current
- char_buffer_start (buffer
);
220 /* Reinitializes BUFFER->current and BUFFER->end to cover the entire
223 char_buffer_rewind (struct char_buffer
*buffer
)
225 buffer
->current
= char_buffer_start (buffer
);
226 buffer
->end
= buffer
->current
+ buffer
->scratch
.length
/ sizeof (CHAR_T
);
229 /* Returns true if a previous call to char_buffer_add (BUFFER, CH)
232 char_buffer_error (const struct char_buffer
*buffer
)
234 return __glibc_unlikely (buffer
->current
== NULL
);
237 /* Slow path for char_buffer_add. */
239 char_buffer_add_slow (struct char_buffer
*buffer
, CHAR_T ch
)
241 if (char_buffer_error (buffer
))
243 size_t offset
= buffer
->end
- (CHAR_T
*) buffer
->scratch
.data
;
244 if (!scratch_buffer_grow_preserve (&buffer
->scratch
))
246 buffer
->current
= NULL
;
250 char_buffer_rewind (buffer
);
251 buffer
->current
+= offset
;
252 *buffer
->current
++ = ch
;
255 /* Adds CH to BUFFER. This function does not report any errors, check
256 for them with char_buffer_error. */
258 char_buffer_add (struct char_buffer
*buffer
, CHAR_T ch
)
259 __attribute__ ((always_inline
));
261 char_buffer_add (struct char_buffer
*buffer
, CHAR_T ch
)
263 if (__glibc_unlikely (buffer
->current
== buffer
->end
))
264 char_buffer_add_slow (buffer
, ch
);
266 *buffer
->current
++ = ch
;
269 /* Read formatted input from S according to the format string
270 FORMAT, using the argument list in ARG.
271 Return the number of assignments made, or -1 for an input error. */
272 #ifdef COMPILE_WSCANF
274 _IO_vfwscanf (_IO_FILE
*s
, const wchar_t *format
, _IO_va_list argptr
,
278 _IO_vfscanf_internal (_IO_FILE
*s
, const char *format
, _IO_va_list argptr
,
283 const CHAR_T
*f
= format
;
284 UCHAR_T fc
; /* Current character of the format. */
285 WINT_T done
= 0; /* Assignments done. */
286 size_t read_in
= 0; /* Chars read in. */
287 WINT_T c
= 0; /* Last char read. */
288 int width
; /* Maximum field width. */
289 int flags
; /* Modifiers for current format element. */
291 #ifndef COMPILE_WSCANF
292 locale_t loc
= _NL_CURRENT_LOCALE
;
293 struct __locale_data
*const curctype
= loc
->__locales
[LC_CTYPE
];
296 /* Errno of last failed inchar call. */
297 int inchar_errno
= 0;
298 /* Status for reading F-P nums. */
299 char got_digit
, got_dot
, got_e
, negative
;
300 /* If a [...] is a [^...]. */
302 #define exp_char not_in
303 /* Base for integral numbers. */
305 /* Decimal point character. */
306 #ifdef COMPILE_WSCANF
311 /* The thousands character of the current locale. */
312 #ifdef COMPILE_WSCANF
315 const char *thousands
;
317 struct ptrs_to_free
*ptrs_to_free
= NULL
;
318 /* State for the conversions. */
320 /* Integral holding variables. */
324 unsigned long long int uq
;
326 unsigned long int ul
;
328 /* Character-buffer pointer. */
330 wchar_t *wstr
= NULL
;
331 char **strptr
= NULL
;
333 /* We must not react on white spaces immediately because they can
334 possibly be matched even if in the input stream no character is
335 available anymore. */
338 CHAR_T
*tw
; /* Temporary pointer. */
339 struct char_buffer charbuf
;
340 scratch_buffer_init (&charbuf
.scratch
);
343 __va_copy (arg
, argptr
);
345 arg
= (va_list) argptr
;
352 ARGCHECK (s
, format
);
355 #ifndef COMPILE_WSCANF
356 struct __locale_data
*const curnumeric
= loc
->__locales
[LC_NUMERIC
];
359 /* Figure out the decimal point character. */
360 #ifdef COMPILE_WSCANF
361 decimal
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
363 decimal
= curnumeric
->values
[_NL_ITEM_INDEX (DECIMAL_POINT
)].string
;
365 /* Figure out the thousands separator character. */
366 #ifdef COMPILE_WSCANF
367 thousands
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_THOUSANDS_SEP_WC
);
369 thousands
= curnumeric
->values
[_NL_ITEM_INDEX (THOUSANDS_SEP
)].string
;
370 if (*thousands
== '\0')
375 /* Lock the stream. */
379 #ifndef COMPILE_WSCANF
380 /* From now on we use `state' to convert the format string. */
381 memset (&state
, '\0', sizeof (state
));
384 /* Run through the format string. */
388 /* Extract the next argument, which is of type TYPE.
389 For a %N$... spec, this is the Nth argument from the beginning;
390 otherwise it is the next argument after the state now in ARG. */
392 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
393 ({ unsigned int pos = argpos; \
395 __va_copy (arg, argptr); \
397 (void) va_arg (arg, void *); \
398 va_arg (arg, type); \
402 /* XXX Possible optimization. */
403 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
404 ({ va_list arg = (va_list) argptr; \
405 arg = (va_list) ((char *) arg \
407 * __va_rounded_size (void *)); \
408 va_arg (arg, type); \
411 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
412 ({ unsigned int pos = argpos; \
413 va_list arg = (va_list) argptr; \
415 (void) va_arg (arg, void *); \
416 va_arg (arg, type); \
421 #ifndef COMPILE_WSCANF
422 if (!isascii ((unsigned char) *f
))
424 /* Non-ASCII, may be a multibyte. */
425 int len
= __mbrlen (f
, strlen (f
), &state
);
431 if (__glibc_unlikely (c
== EOF
))
433 else if (c
!= (unsigned char) *f
++)
435 ungetc_not_eof (c
, s
);
448 /* Remember to skip spaces. */
455 /* Read a character. */
458 /* Characters other than format specs must just match. */
459 if (__glibc_unlikely (c
== EOF
))
462 /* We saw white space char as the last character in the format
463 string. Now it's time to skip all leading white space. */
467 if (__glibc_unlikely (inchar () == EOF
))
472 if (__glibc_unlikely (c
!= fc
))
481 /* This is the start of the conversion string. */
484 /* Initialize state of modifiers. */
487 /* Prepare temporary buffer. */
488 char_buffer_rewind (&charbuf
);
490 /* Check for a positional parameter specification. */
491 if (ISDIGIT ((UCHAR_T
) *f
))
493 argpos
= read_int ((const UCHAR_T
**) &f
);
498 /* Oops; that was actually the field width. */
505 /* Check for the assignment-suppressing, the number grouping flag,
506 and the signal to use the locale's digit representation. */
507 while (*f
== L_('*') || *f
== L_('\'') || *f
== L_('I'))
514 #ifdef COMPILE_WSCANF
515 if (thousands
!= L
'\0')
517 if (thousands
!= NULL
)
526 /* Find the maximum field width. */
528 if (ISDIGIT ((UCHAR_T
) *f
))
529 width
= read_int ((const UCHAR_T
**) &f
);
534 /* Check for type modifiers. */
538 /* ints are short ints or chars. */
550 /* A double `l' is equivalent to an `L'. */
552 flags
|= LONGDBL
| LONG
;
555 /* ints are long ints. */
560 /* doubles are long doubles, and ints are long long ints. */
561 flags
|= LONGDBL
| LONG
;
564 /* The `a' is used as a flag only if followed by `s', `S' or
566 if (*f
!= L_('s') && *f
!= L_('S') && *f
!= L_('['))
571 /* In __isoc99_*scanf %as, %aS and %a[ extension is not
573 if (s
->_flags2
& _IO_FLAGS2_SCANF_STD
)
578 /* String conversions (%s, %[) take a `char **'
579 arg and fill it in with a malloc'd pointer. */
583 flags
|= POSIX_MALLOC
;
591 if (need_longlong
&& sizeof (size_t) > sizeof (unsigned long int))
593 else if (sizeof (size_t) > sizeof (unsigned int))
597 if (need_longlong
&& sizeof (uintmax_t) > sizeof (unsigned long int))
599 else if (sizeof (uintmax_t) > sizeof (unsigned int))
603 if (need_longlong
&& sizeof (ptrdiff_t) > sizeof (long int))
605 else if (sizeof (ptrdiff_t) > sizeof (int))
609 /* Not a recognized modifier. Backup. */
614 /* End of the format string? */
615 if (__glibc_unlikely (*f
== L_('\0')))
618 /* Find the conversion specifier. */
620 if (skip_space
|| (fc
!= L_('[') && fc
!= L_('c')
621 && fc
!= L_('C') && fc
!= L_('n')))
623 /* Eat whitespace. */
624 int save_errno
= errno
;
627 /* We add the additional test for EOF here since otherwise
628 inchar will restore the old errno value which might be
629 EINTR but does not indicate an interrupt since nothing
630 was read at this time. */
631 if (__builtin_expect ((c
== EOF
|| inchar () == EOF
)
632 && errno
== EINTR
, 0))
635 __set_errno (save_errno
);
642 case L_('%'): /* Must match a literal '%'. */
644 if (__glibc_unlikely (c
== EOF
))
646 if (__glibc_unlikely (c
!= fc
))
648 ungetc_not_eof (c
, s
);
653 case L_('n'): /* Answer number of assignments done. */
654 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
655 with the 'n' conversion specifier. */
656 if (!(flags
& SUPPRESS
))
658 /* Don't count the read-ahead. */
659 if (need_longlong
&& (flags
& LONGDBL
))
660 *ARG (long long int *) = read_in
;
661 else if (need_long
&& (flags
& LONG
))
662 *ARG (long int *) = read_in
;
663 else if (flags
& SHORT
)
664 *ARG (short int *) = read_in
;
665 else if (!(flags
& CHAR
))
666 *ARG (int *) = read_in
;
668 *ARG (char *) = read_in
;
670 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
671 /* We have a severe problem here. The ISO C standard
672 contradicts itself in explaining the effect of the %n
673 format in `scanf'. While in ISO C:1990 and the ISO C
674 Amendement 1:1995 the result is described as
676 Execution of a %n directive does not effect the
677 assignment count returned at the completion of
678 execution of the f(w)scanf function.
680 in ISO C Corrigendum 1:1994 the following was added:
683 Add the following fourth example:
686 int d1, d2, n1, n2, i;
687 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
688 the value 123 is assigned to d1 and the value3 to n1.
689 Because %n can never get an input failure the value
690 of 3 is also assigned to n2. The value of d2 is not
691 affected. The value 3 is assigned to i.
693 We go for now with the historically correct code from ISO C,
694 i.e., we don't count the %n assignments. When it ever
695 should proof to be wrong just remove the #ifdef above. */
701 case L_('c'): /* Match characters. */
702 if ((flags
& LONG
) == 0)
707 #define STRING_ARG(Str, Type, Width) \
708 do if (!(flags & SUPPRESS)) \
710 if (flags & MALLOC) \
712 /* The string is to be stored in a malloc'd buffer. */ \
713 /* For %mS using char ** is actually wrong, but \
714 shouldn't make a difference on any arch glibc \
715 supports and would unnecessarily complicate \
717 strptr = ARG (char **); \
718 if (strptr == NULL) \
720 /* Allocate an initial buffer. */ \
722 *strptr = (char *) malloc (strsize * sizeof (Type)); \
723 Str = (Type *) *strptr; \
725 add_ptr_to_free (strptr); \
726 else if (flags & POSIX_MALLOC) \
733 Str = ARG (Type *); \
737 #ifdef COMPILE_WSCANF
738 STRING_ARG (str
, char, 100);
740 STRING_ARG (str
, char, (width
> 1024 ? 1024 : width
));
744 if (__glibc_unlikely (c
== EOF
))
747 #ifdef COMPILE_WSCANF
748 /* We have to convert the wide character(s) into multibyte
749 characters and store the result. */
750 memset (&state
, '\0', sizeof (state
));
756 if (!(flags
& SUPPRESS
) && (flags
& POSIX_MALLOC
)
757 && *strptr
+ strsize
- str
<= MB_LEN_MAX
)
759 /* We have to enlarge the buffer if the `m' flag
761 size_t strleng
= str
- *strptr
;
764 newstr
= (char *) realloc (*strptr
, strsize
* 2);
767 /* Can't allocate that much. Last-ditch effort. */
768 newstr
= (char *) realloc (*strptr
,
769 strleng
+ MB_LEN_MAX
);
772 /* c can't have `a' flag, only `m'. */
779 str
= newstr
+ strleng
;
780 strsize
= strleng
+ MB_LEN_MAX
;
786 str
= newstr
+ strleng
;
791 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
792 if (__glibc_unlikely (n
== (size_t) -1))
793 /* No valid wide character. */
796 /* Increment the output pointer. Even if we don't
800 while (--width
> 0 && inchar () != EOF
);
802 if (!(flags
& SUPPRESS
))
807 && (char *) str
== *strptr
+ strsize
)
809 /* Enlarge the buffer. */
812 + (strsize
>= width
? width
- 1 : strsize
);
814 str
= (char *) realloc (*strptr
, newsize
);
817 /* Can't allocate that much. Last-ditch
819 str
= (char *) realloc (*strptr
, strsize
+ 1);
822 /* c can't have `a' flag, only `m'. */
828 *strptr
= (char *) str
;
835 *strptr
= (char *) str
;
842 while (--width
> 0 && inchar () != EOF
);
845 while (--width
> 0 && inchar () != EOF
);
848 if (!(flags
& SUPPRESS
))
850 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
852 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
867 STRING_ARG (wstr
, wchar_t, (width
> 1024 ? 1024 : width
));
870 if (__glibc_unlikely (c
== EOF
))
873 #ifdef COMPILE_WSCANF
874 /* Just store the incoming wide characters. */
875 if (!(flags
& SUPPRESS
))
880 && wstr
== (wchar_t *) *strptr
+ strsize
)
883 = strsize
+ (strsize
> width
? width
- 1 : strsize
);
884 /* Enlarge the buffer. */
885 wstr
= (wchar_t *) realloc (*strptr
,
886 newsize
* sizeof (wchar_t));
889 /* Can't allocate that much. Last-ditch effort. */
890 wstr
= (wchar_t *) realloc (*strptr
,
895 /* C or lc can't have `a' flag, only `m'
902 *strptr
= (char *) wstr
;
909 *strptr
= (char *) wstr
;
916 while (--width
> 0 && inchar () != EOF
);
919 while (--width
> 0 && inchar () != EOF
);
922 /* We have to convert the multibyte input sequence to wide
927 memset (&cstate
, '\0', sizeof (cstate
));
931 /* This is what we present the mbrtowc function first. */
934 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
935 && wstr
== (wchar_t *) *strptr
+ strsize
)
938 = strsize
+ (strsize
> width
? width
- 1 : strsize
);
939 /* Enlarge the buffer. */
940 wstr
= (wchar_t *) realloc (*strptr
,
941 newsize
* sizeof (wchar_t));
944 /* Can't allocate that much. Last-ditch effort. */
945 wstr
= (wchar_t *) realloc (*strptr
,
947 * sizeof (wchar_t)));
950 /* C or lc can't have `a' flag, only `m' flag. */
956 *strptr
= (char *) wstr
;
963 *strptr
= (char *) wstr
;
973 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
976 if (n
== (size_t) -2)
978 /* Possibly correct character, just not enough
980 if (__glibc_unlikely (inchar () == EOF
))
987 if (__glibc_unlikely (n
!= 1))
990 /* We have a match. */
994 /* Advance the result pointer. */
997 while (--width
> 0 && inchar () != EOF
);
1001 if (!(flags
& SUPPRESS
))
1003 if ((flags
& MALLOC
) && wstr
- (wchar_t *) *strptr
!= strsize
)
1005 wchar_t *cp
= (wchar_t *) realloc (*strptr
,
1007 - (wchar_t *) *strptr
)
1008 * sizeof (wchar_t)));
1010 *strptr
= (char *) cp
;
1019 case L_('s'): /* Read a string. */
1020 if (!(flags
& LONG
))
1022 STRING_ARG (str
, char, 100);
1025 if (__glibc_unlikely (c
== EOF
))
1028 #ifdef COMPILE_WSCANF
1029 memset (&state
, '\0', sizeof (state
));
1036 ungetc_not_eof (c
, s
);
1040 #ifdef COMPILE_WSCANF
1041 /* This is quite complicated. We have to convert the
1042 wide characters into multibyte characters and then
1047 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
1048 && *strptr
+ strsize
- str
<= MB_LEN_MAX
)
1050 /* We have to enlarge the buffer if the `a' or `m'
1052 size_t strleng
= str
- *strptr
;
1055 newstr
= (char *) realloc (*strptr
, strsize
* 2);
1058 /* Can't allocate that much. Last-ditch
1060 newstr
= (char *) realloc (*strptr
,
1061 strleng
+ MB_LEN_MAX
);
1064 if (flags
& POSIX_MALLOC
)
1069 /* We lose. Oh well. Terminate the
1070 string and stop converting,
1071 so at least we don't skip any input. */
1072 ((char *) (*strptr
))[strleng
] = '\0';
1080 str
= newstr
+ strleng
;
1081 strsize
= strleng
+ MB_LEN_MAX
;
1087 str
= newstr
+ strleng
;
1092 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
,
1094 if (__glibc_unlikely (n
== (size_t) -1))
1097 assert (n
<= MB_LEN_MAX
);
1102 if (!(flags
& SUPPRESS
))
1105 if ((flags
& MALLOC
)
1106 && (char *) str
== *strptr
+ strsize
)
1108 /* Enlarge the buffer. */
1109 str
= (char *) realloc (*strptr
, 2 * strsize
);
1112 /* Can't allocate that much. Last-ditch
1114 str
= (char *) realloc (*strptr
, strsize
+ 1);
1117 if (flags
& POSIX_MALLOC
)
1122 /* We lose. Oh well. Terminate the
1123 string and stop converting,
1124 so at least we don't skip any input. */
1125 ((char *) (*strptr
))[strsize
- 1] = '\0';
1132 *strptr
= (char *) str
;
1139 *strptr
= (char *) str
;
1147 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
1149 if (!(flags
& SUPPRESS
))
1151 #ifdef COMPILE_WSCANF
1152 /* We have to emit the code to get into the initial
1154 char buf
[MB_LEN_MAX
];
1155 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
1156 if (n
> 0 && (flags
& MALLOC
)
1157 && str
+ n
>= *strptr
+ strsize
)
1159 /* Enlarge the buffer. */
1160 size_t strleng
= str
- *strptr
;
1163 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
1166 if (flags
& POSIX_MALLOC
)
1171 /* We lose. Oh well. Terminate the string
1172 and stop converting, so at least we don't
1174 ((char *) (*strptr
))[strleng
] = '\0';
1182 str
= newstr
+ strleng
;
1183 strsize
= strleng
+ n
+ 1;
1187 str
= __mempcpy (str
, buf
, n
);
1191 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
1193 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
1207 #ifndef COMPILE_WSCANF
1211 /* Wide character string. */
1212 STRING_ARG (wstr
, wchar_t, 100);
1215 if (__builtin_expect (c
== EOF
, 0))
1218 #ifndef COMPILE_WSCANF
1219 memset (&cstate
, '\0', sizeof (cstate
));
1226 ungetc_not_eof (c
, s
);
1230 #ifdef COMPILE_WSCANF
1232 if (!(flags
& SUPPRESS
))
1235 if ((flags
& MALLOC
)
1236 && wstr
== (wchar_t *) *strptr
+ strsize
)
1238 /* Enlarge the buffer. */
1239 wstr
= (wchar_t *) realloc (*strptr
,
1241 * sizeof (wchar_t));
1244 /* Can't allocate that much. Last-ditch
1246 wstr
= (wchar_t *) realloc (*strptr
,
1248 * sizeof (wchar_t));
1251 if (flags
& POSIX_MALLOC
)
1256 /* We lose. Oh well. Terminate the string
1257 and stop converting, so at least we don't
1259 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1266 *strptr
= (char *) wstr
;
1273 *strptr
= (char *) wstr
;
1289 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
1292 if (n
== (size_t) -2)
1294 /* Possibly correct character, just not enough
1296 if (__glibc_unlikely (inchar () == EOF
))
1303 if (__glibc_unlikely (n
!= 1))
1306 /* We have a match. */
1311 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
1312 && wstr
== (wchar_t *) *strptr
+ strsize
)
1314 /* Enlarge the buffer. */
1315 wstr
= (wchar_t *) realloc (*strptr
,
1317 * sizeof (wchar_t)));
1320 /* Can't allocate that much. Last-ditch effort. */
1321 wstr
= (wchar_t *) realloc (*strptr
,
1323 * sizeof (wchar_t)));
1326 if (flags
& POSIX_MALLOC
)
1331 /* We lose. Oh well. Terminate the
1332 string and stop converting, so at
1333 least we don't skip any input. */
1334 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1341 *strptr
= (char *) wstr
;
1348 *strptr
= (char *) wstr
;
1356 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
1358 if (!(flags
& SUPPRESS
))
1362 if ((flags
& MALLOC
) && wstr
- (wchar_t *) *strptr
!= strsize
)
1364 wchar_t *cp
= (wchar_t *) realloc (*strptr
,
1366 - (wchar_t *) *strptr
)
1367 * sizeof(wchar_t)));
1369 *strptr
= (char *) cp
;
1378 case L_('x'): /* Hexadecimal integer. */
1379 case L_('X'): /* Ditto. */
1383 case L_('o'): /* Octal integer. */
1387 case L_('u'): /* Unsigned decimal integer. */
1391 case L_('d'): /* Signed decimal integer. */
1393 flags
|= NUMBER_SIGNED
;
1396 case L_('i'): /* Generic number. */
1398 flags
|= NUMBER_SIGNED
;
1402 if (__glibc_unlikely (c
== EOF
))
1405 /* Check for a sign. */
1406 if (c
== L_('-') || c
== L_('+'))
1408 char_buffer_add (&charbuf
, c
);
1414 /* Look for a leading indication of base. */
1415 if (width
!= 0 && c
== L_('0'))
1420 char_buffer_add (&charbuf
, c
);
1423 if (width
!= 0 && TOLOWER (c
) == L_('x'))
1441 if (base
== 10 && __builtin_expect ((flags
& I18N
) != 0, 0))
1446 #ifdef COMPILE_WSCANF
1447 const wchar_t *wcdigits
[10];
1448 const wchar_t *wcdigits_extended
[10];
1450 const char *mbdigits
[10];
1451 const char *mbdigits_extended
[10];
1453 /* "to_inpunct" is a map from ASCII digits to their
1454 equivalent in locale. This is defined for locales
1455 which use an extra digits set. */
1456 wctrans_t map
= __wctrans ("to_inpunct");
1460 #ifdef COMPILE_WSCANF
1461 to_level
= _NL_CURRENT_WORD (LC_CTYPE
,
1462 _NL_CTYPE_INDIGITS_WC_LEN
) - 1;
1464 to_level
= (uint32_t) curctype
->values
[_NL_ITEM_INDEX (_NL_CTYPE_INDIGITS_MB_LEN
)].word
- 1;
1467 /* Get the alternative digit forms if there are any. */
1468 if (__glibc_unlikely (map
!= NULL
))
1470 /* Adding new level for extra digits set in locale file. */
1473 for (n
= 0; n
< 10; ++n
)
1475 #ifdef COMPILE_WSCANF
1476 wcdigits
[n
] = (const wchar_t *)
1477 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1479 wchar_t *wc_extended
= (wchar_t *)
1480 alloca ((to_level
+ 2) * sizeof (wchar_t));
1481 __wmemcpy (wc_extended
, wcdigits
[n
], to_level
);
1482 wc_extended
[to_level
] = __towctrans (L
'0' + n
, map
);
1483 wc_extended
[to_level
+ 1] = '\0';
1484 wcdigits_extended
[n
] = wc_extended
;
1487 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1489 /* Get the equivalent wide char in map. */
1490 wint_t extra_wcdigit
= __towctrans (L
'0' + n
, map
);
1492 /* Convert it to multibyte representation. */
1494 memset (&state
, '\0', sizeof (state
));
1496 char extra_mbdigit
[MB_LEN_MAX
];
1498 = __wcrtomb (extra_mbdigit
, extra_wcdigit
, &state
);
1500 if (mblen
== (size_t) -1)
1502 /* Ignore this new level. */
1507 /* Calculate the length of mbdigits[n]. */
1508 const char *last_char
= mbdigits
[n
];
1509 for (level
= 0; level
< to_level
; ++level
)
1510 last_char
= strchr (last_char
, '\0') + 1;
1512 size_t mbdigits_len
= last_char
- mbdigits
[n
];
1514 /* Allocate memory for extended multibyte digit. */
1516 mb_extended
= (char *) alloca (mbdigits_len
+ mblen
+ 1);
1518 /* And get the mbdigits + extra_digit string. */
1519 *(char *) __mempcpy (__mempcpy (mb_extended
, mbdigits
[n
],
1521 extra_mbdigit
, mblen
) = '\0';
1522 mbdigits_extended
[n
] = mb_extended
;
1527 /* Read the number into workspace. */
1528 while (c
!= EOF
&& width
!= 0)
1530 /* In this round we get the pointer to the digit strings
1531 and also perform the first round of comparisons. */
1532 for (n
= 0; n
< 10; ++n
)
1534 /* Get the string for the digits with value N. */
1535 #ifdef COMPILE_WSCANF
1537 /* wcdigits_extended[] is fully set in the loop
1538 above, but the test for "map != NULL" is done
1539 inside the loop here and outside the loop there. */
1540 DIAG_PUSH_NEEDS_COMMENT
;
1541 DIAG_IGNORE_NEEDS_COMMENT (4.7, "-Wmaybe-uninitialized");
1543 if (__glibc_unlikely (map
!= NULL
))
1544 wcdigits
[n
] = wcdigits_extended
[n
];
1546 wcdigits
[n
] = (const wchar_t *)
1547 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1548 wcdigits
[n
] += from_level
;
1550 DIAG_POP_NEEDS_COMMENT
;
1552 if (c
== (wint_t) *wcdigits
[n
])
1554 to_level
= from_level
;
1558 /* Advance the pointer to the next string. */
1562 int avail
= width
> 0 ? width
: INT_MAX
;
1564 if (__glibc_unlikely (map
!= NULL
))
1565 mbdigits
[n
] = mbdigits_extended
[n
];
1568 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1570 for (level
= 0; level
< from_level
; level
++)
1571 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1574 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1576 if (*++cmpp
== '\0')
1580 if (avail
== 0 || inchar () == EOF
)
1590 to_level
= from_level
;
1594 /* We are pushing all read characters back. */
1595 if (cmpp
> mbdigits
[n
])
1598 while (--cmpp
> mbdigits
[n
])
1599 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1600 c
= (unsigned char) *cmpp
;
1603 /* Advance the pointer to the next string. */
1604 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1610 /* Have not yet found the digit. */
1611 for (level
= from_level
+ 1; level
<= to_level
; ++level
)
1613 /* Search all ten digits of this level. */
1614 for (n
= 0; n
< 10; ++n
)
1616 #ifdef COMPILE_WSCANF
1617 if (c
== (wint_t) *wcdigits
[n
])
1620 /* Advance the pointer to the next string. */
1624 int avail
= width
> 0 ? width
: INT_MAX
;
1627 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1629 if (*++cmpp
== '\0')
1633 if (avail
== 0 || inchar () == EOF
)
1646 /* We are pushing all read characters back. */
1647 if (cmpp
> mbdigits
[n
])
1650 while (--cmpp
> mbdigits
[n
])
1651 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1652 c
= (unsigned char) *cmpp
;
1655 /* Advance the pointer to the next string. */
1656 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1672 else if (flags
& GROUP
)
1674 /* Try matching against the thousands separator. */
1675 #ifdef COMPILE_WSCANF
1679 const char *cmpp
= thousands
;
1680 int avail
= width
> 0 ? width
: INT_MAX
;
1682 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1684 char_buffer_add (&charbuf
, c
);
1685 if (*++cmpp
== '\0')
1689 if (avail
== 0 || inchar () == EOF
)
1695 if (char_buffer_error (&charbuf
))
1697 __set_errno (ENOMEM
);
1704 /* We are pushing all read characters back. */
1705 if (cmpp
> thousands
)
1707 charbuf
.current
-= cmpp
- thousands
;
1709 while (--cmpp
> thousands
)
1710 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1711 c
= (unsigned char) *cmpp
;
1719 /* The last thousands character will be added back by
1720 the char_buffer_add below. */
1727 char_buffer_add (&charbuf
, c
);
1735 /* Read the number into workspace. */
1736 while (c
!= EOF
&& width
!= 0)
1743 else if (!ISDIGIT (c
) || (int) (c
- L_('0')) >= base
)
1745 if (base
== 10 && (flags
& GROUP
))
1747 /* Try matching against the thousands separator. */
1748 #ifdef COMPILE_WSCANF
1752 const char *cmpp
= thousands
;
1753 int avail
= width
> 0 ? width
: INT_MAX
;
1755 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1757 char_buffer_add (&charbuf
, c
);
1758 if (*++cmpp
== '\0')
1762 if (avail
== 0 || inchar () == EOF
)
1768 if (char_buffer_error (&charbuf
))
1770 __set_errno (ENOMEM
);
1777 /* We are pushing all read characters back. */
1778 if (cmpp
> thousands
)
1780 charbuf
.current
-= cmpp
- thousands
;
1782 while (--cmpp
> thousands
)
1783 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1784 c
= (unsigned char) *cmpp
;
1792 /* The last thousands character will be added back by
1793 the char_buffer_add below. */
1800 char_buffer_add (&charbuf
, c
);
1807 if (char_buffer_error (&charbuf
))
1809 __set_errno (ENOMEM
);
1814 if (char_buffer_size (&charbuf
) == 0
1815 || (char_buffer_size (&charbuf
) == 1
1816 && (char_buffer_start (&charbuf
)[0] == L_('+')
1817 || char_buffer_start (&charbuf
)[0] == L_('-'))))
1819 /* There was no number. If we are supposed to read a pointer
1820 we must recognize "(nil)" as well. */
1821 if (__builtin_expect (char_buffer_size (&charbuf
) == 0
1822 && (flags
& READ_POINTER
)
1823 && (width
< 0 || width
>= 5)
1825 && TOLOWER (inchar ()) == L_('n')
1826 && TOLOWER (inchar ()) == L_('i')
1827 && TOLOWER (inchar ()) == L_('l')
1828 && inchar () == L_(')'), 1))
1829 /* We must produce the value of a NULL pointer. A single
1830 '0' digit is enough. */
1831 char_buffer_add (&charbuf
, L_('0'));
1834 /* The last read character is not part of the number
1842 /* The just read character is not part of the number anymore. */
1845 /* Convert the number. */
1846 char_buffer_add (&charbuf
, L_('\0'));
1847 if (char_buffer_error (&charbuf
))
1849 __set_errno (ENOMEM
);
1853 if (need_longlong
&& (flags
& LONGDBL
))
1855 if (flags
& NUMBER_SIGNED
)
1856 num
.q
= __strtoll_internal
1857 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1859 num
.uq
= __strtoull_internal
1860 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1864 if (flags
& NUMBER_SIGNED
)
1865 num
.l
= __strtol_internal
1866 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1868 num
.ul
= __strtoul_internal
1869 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1871 if (__glibc_unlikely (char_buffer_start (&charbuf
) == tw
))
1874 if (!(flags
& SUPPRESS
))
1876 if (flags
& NUMBER_SIGNED
)
1878 if (need_longlong
&& (flags
& LONGDBL
))
1879 *ARG (LONGLONG
int *) = num
.q
;
1880 else if (need_long
&& (flags
& LONG
))
1881 *ARG (long int *) = num
.l
;
1882 else if (flags
& SHORT
)
1883 *ARG (short int *) = (short int) num
.l
;
1884 else if (!(flags
& CHAR
))
1885 *ARG (int *) = (int) num
.l
;
1887 *ARG (signed char *) = (signed char) num
.ul
;
1891 if (need_longlong
&& (flags
& LONGDBL
))
1892 *ARG (unsigned LONGLONG
int *) = num
.uq
;
1893 else if (need_long
&& (flags
& LONG
))
1894 *ARG (unsigned long int *) = num
.ul
;
1895 else if (flags
& SHORT
)
1896 *ARG (unsigned short int *)
1897 = (unsigned short int) num
.ul
;
1898 else if (!(flags
& CHAR
))
1899 *ARG (unsigned int *) = (unsigned int) num
.ul
;
1901 *ARG (unsigned char *) = (unsigned char) num
.ul
;
1907 case L_('e'): /* Floating-point numbers. */
1918 if (__glibc_unlikely (c
== EOF
))
1921 got_digit
= got_dot
= got_e
= 0;
1923 /* Check for a sign. */
1924 if (c
== L_('-') || c
== L_('+'))
1926 negative
= c
== L_('-');
1927 if (__glibc_unlikely (width
== 0 || inchar () == EOF
))
1928 /* EOF is only an input error before we read any chars. */
1936 /* Take care for the special arguments "nan" and "inf". */
1937 if (TOLOWER (c
) == L_('n'))
1940 char_buffer_add (&charbuf
, c
);
1941 if (__builtin_expect (width
== 0
1943 || TOLOWER (c
) != L_('a'), 0))
1947 char_buffer_add (&charbuf
, c
);
1948 if (__builtin_expect (width
== 0
1950 || TOLOWER (c
) != L_('n'), 0))
1954 char_buffer_add (&charbuf
, c
);
1958 else if (TOLOWER (c
) == L_('i'))
1960 /* Maybe "inf" or "infinity". */
1961 char_buffer_add (&charbuf
, c
);
1962 if (__builtin_expect (width
== 0
1964 || TOLOWER (c
) != L_('n'), 0))
1968 char_buffer_add (&charbuf
, c
);
1969 if (__builtin_expect (width
== 0
1971 || TOLOWER (c
) != L_('f'), 0))
1975 char_buffer_add (&charbuf
, c
);
1976 /* It is as least "inf". */
1977 if (width
!= 0 && inchar () != EOF
)
1979 if (TOLOWER (c
) == L_('i'))
1983 /* Now we have to read the rest as well. */
1984 char_buffer_add (&charbuf
, c
);
1985 if (__builtin_expect (width
== 0
1987 || TOLOWER (c
) != L_('n'), 0))
1991 char_buffer_add (&charbuf
, c
);
1992 if (__builtin_expect (width
== 0
1994 || TOLOWER (c
) != L_('i'), 0))
1998 char_buffer_add (&charbuf
, c
);
1999 if (__builtin_expect (width
== 0
2001 || TOLOWER (c
) != L_('t'), 0))
2005 char_buffer_add (&charbuf
, c
);
2006 if (__builtin_expect (width
== 0
2008 || TOLOWER (c
) != L_('y'), 0))
2012 char_buffer_add (&charbuf
, c
);
2022 if (width
!= 0 && c
== L_('0'))
2024 char_buffer_add (&charbuf
, c
);
2028 if (width
!= 0 && TOLOWER (c
) == L_('x'))
2030 /* It is a number in hexadecimal format. */
2031 char_buffer_add (&charbuf
, c
);
2033 flags
|= HEXA_FLOAT
;
2036 /* Grouping is not allowed. */
2048 if (char_buffer_error (&charbuf
))
2050 __set_errno (ENOMEM
);
2056 char_buffer_add (&charbuf
, c
);
2059 else if (!got_e
&& (flags
& HEXA_FLOAT
) && ISXDIGIT (c
))
2061 char_buffer_add (&charbuf
, c
);
2064 else if (got_e
&& charbuf
.current
[-1] == exp_char
2065 && (c
== L_('-') || c
== L_('+')))
2066 char_buffer_add (&charbuf
, c
);
2067 else if (got_digit
&& !got_e
2068 && (CHAR_T
) TOLOWER (c
) == exp_char
)
2070 char_buffer_add (&charbuf
, exp_char
);
2071 got_e
= got_dot
= 1;
2075 #ifdef COMPILE_WSCANF
2076 if (! got_dot
&& c
== decimal
)
2078 char_buffer_add (&charbuf
, c
);
2081 else if ((flags
& GROUP
) != 0 && ! got_dot
&& c
== thousands
)
2082 char_buffer_add (&charbuf
, c
);
2085 /* The last read character is not part of the number
2091 const char *cmpp
= decimal
;
2092 int avail
= width
> 0 ? width
: INT_MAX
;
2096 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
2097 if (*++cmpp
== '\0')
2101 if (avail
== 0 || inchar () == EOF
)
2109 /* Add all the characters. */
2110 for (cmpp
= decimal
; *cmpp
!= '\0'; ++cmpp
)
2111 char_buffer_add (&charbuf
, (unsigned char) *cmpp
);
2118 /* Figure out whether it is a thousands separator.
2119 There is one problem: we possibly read more than
2120 one character. We cannot push them back but since
2121 we know that parts of the `decimal' string matched,
2122 we can compare against it. */
2123 const char *cmp2p
= thousands
;
2125 if ((flags
& GROUP
) != 0 && ! got_dot
)
2127 while (cmp2p
- thousands
< cmpp
- decimal
2128 && *cmp2p
== decimal
[cmp2p
- thousands
])
2130 if (cmp2p
- thousands
== cmpp
- decimal
)
2132 while ((unsigned char) *cmp2p
== c
&& avail
>= 0)
2133 if (*++cmp2p
== '\0')
2137 if (avail
== 0 || inchar () == EOF
)
2144 if (cmp2p
!= NULL
&& *cmp2p
== '\0')
2146 /* Add all the characters. */
2147 for (cmpp
= thousands
; *cmpp
!= '\0'; ++cmpp
)
2148 char_buffer_add (&charbuf
, (unsigned char) *cmpp
);
2154 /* The last read character is not part of the number
2163 if (width
== 0 || inchar () == EOF
)
2170 if (char_buffer_error (&charbuf
))
2172 __set_errno (ENOMEM
);
2178 if (__builtin_expect ((flags
& I18N
) != 0, 0)
2179 /* Hexadecimal floats make no sense, fixing localized
2180 digits with ASCII letters. */
2181 && !(flags
& HEXA_FLOAT
)
2182 /* Minimum requirement. */
2183 && (char_buffer_size (&charbuf
) == 0 || got_dot
)
2184 && (map
= __wctrans ("to_inpunct")) != NULL
)
2186 /* Reget the first character. */
2189 /* Localized digits, decimal points, and thousands
2191 wint_t wcdigits
[12];
2193 /* First get decimal equivalent to check if we read it
2195 wcdigits
[11] = __towctrans (L
'.', map
);
2197 /* If we have not read any character or have just read
2198 locale decimal point which matches the decimal point
2199 for localized FP numbers, then we may have localized
2200 digits. Note, we test GOT_DOT above. */
2201 #ifdef COMPILE_WSCANF
2202 if (char_buffer_size (&charbuf
) == 0
2203 || (char_buffer_size (&charbuf
) == 1
2204 && wcdigits
[11] == decimal
))
2206 char mbdigits
[12][MB_LEN_MAX
+ 1];
2209 memset (&state
, '\0', sizeof (state
));
2211 bool match_so_far
= char_buffer_size (&charbuf
) == 0;
2212 size_t mblen
= __wcrtomb (mbdigits
[11], wcdigits
[11], &state
);
2213 if (mblen
!= (size_t) -1)
2215 mbdigits
[11][mblen
] = '\0';
2217 (char_buffer_size (&charbuf
) == strlen (decimal
)
2218 && strcmp (decimal
, mbdigits
[11]) == 0);
2222 size_t decimal_len
= strlen (decimal
);
2223 /* This should always be the case but the data comes
2225 if (decimal_len
<= MB_LEN_MAX
)
2227 match_so_far
|= char_buffer_size (&charbuf
) == decimal_len
;
2228 memcpy (mbdigits
[11], decimal
, decimal_len
+ 1);
2231 match_so_far
= false;
2237 bool have_locthousands
= (flags
& GROUP
) != 0;
2239 /* Now get the digits and the thousands-sep equivalents. */
2240 for (int n
= 0; n
< 11; ++n
)
2243 wcdigits
[n
] = __towctrans (L
'0' + n
, map
);
2246 wcdigits
[10] = __towctrans (L
',', map
);
2247 have_locthousands
&= wcdigits
[10] != L
'\0';
2250 #ifndef COMPILE_WSCANF
2251 memset (&state
, '\0', sizeof (state
));
2253 size_t mblen
= __wcrtomb (mbdigits
[n
], wcdigits
[n
],
2255 if (mblen
== (size_t) -1)
2259 if (have_locthousands
)
2261 size_t thousands_len
= strlen (thousands
);
2262 if (thousands_len
<= MB_LEN_MAX
)
2263 memcpy (mbdigits
[10], thousands
,
2266 have_locthousands
= false;
2270 /* Ignore checking against localized digits. */
2274 mbdigits
[n
][mblen
] = '\0';
2278 /* Start checking against localized digits, if
2279 conversion is done correctly. */
2282 if (char_buffer_error (&charbuf
))
2284 __set_errno (ENOMEM
);
2288 if (got_e
&& charbuf
.current
[-1] == exp_char
2289 && (c
== L_('-') || c
== L_('+')))
2290 char_buffer_add (&charbuf
, c
);
2291 else if (char_buffer_size (&charbuf
) > 0 && !got_e
2292 && (CHAR_T
) TOLOWER (c
) == exp_char
)
2294 char_buffer_add (&charbuf
, exp_char
);
2295 got_e
= got_dot
= 1;
2299 /* Check against localized digits, decimal point,
2300 and thousands separator. */
2302 for (n
= 0; n
< 12; ++n
)
2304 #ifdef COMPILE_WSCANF
2305 if (c
== wcdigits
[n
])
2308 char_buffer_add (&charbuf
, L_('0') + n
);
2309 else if (n
== 11 && !got_dot
)
2311 char_buffer_add (&charbuf
, decimal
);
2314 else if (n
== 10 && have_locthousands
2316 char_buffer_add (&charbuf
, thousands
);
2318 /* The last read character is not part
2319 of the number anymore. */
2325 const char *cmpp
= mbdigits
[n
];
2326 int avail
= width
> 0 ? width
: INT_MAX
;
2328 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
2329 if (*++cmpp
== '\0')
2333 if (avail
== 0 || inchar () == EOF
)
2343 char_buffer_add (&charbuf
, L_('0') + n
);
2344 else if (n
== 11 && !got_dot
)
2346 /* Add all the characters. */
2347 for (cmpp
= decimal
; *cmpp
!= '\0';
2349 char_buffer_add (&charbuf
,
2350 (unsigned char) *cmpp
);
2354 else if (n
== 10 && (flags
& GROUP
) != 0
2357 /* Add all the characters. */
2358 for (cmpp
= thousands
; *cmpp
!= '\0';
2360 char_buffer_add (&charbuf
,
2361 (unsigned char) *cmpp
);
2364 /* The last read character is not part
2365 of the number anymore. */
2371 /* We are pushing all read characters back. */
2372 if (cmpp
> mbdigits
[n
])
2375 while (--cmpp
> mbdigits
[n
])
2376 ungetc_not_eof ((unsigned char) *cmpp
, s
);
2377 c
= (unsigned char) *cmpp
;
2384 /* The last read character is not part
2385 of the number anymore. */
2391 if (width
== 0 || inchar () == EOF
)
2399 #ifndef COMPILE_WSCANF
2405 if (char_buffer_error (&charbuf
))
2407 __set_errno (ENOMEM
);
2412 /* Have we read any character? If we try to read a number
2413 in hexadecimal notation and we have read only the `0x'
2414 prefix this is an error. */
2415 if (__glibc_unlikely (char_buffer_size (&charbuf
) == 0
2416 || ((flags
& HEXA_FLOAT
)
2417 && char_buffer_size (&charbuf
) == 2)))
2421 /* Convert the number. */
2422 char_buffer_add (&charbuf
, L_('\0'));
2423 if (char_buffer_error (&charbuf
))
2425 __set_errno (ENOMEM
);
2429 if ((flags
& LONGDBL
) && !__ldbl_is_dbl
)
2431 long double d
= __strtold_internal
2432 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2433 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2434 *ARG (long double *) = negative
? -d
: d
;
2436 else if (flags
& (LONG
| LONGDBL
))
2438 double d
= __strtod_internal
2439 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2440 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2441 *ARG (double *) = negative
? -d
: d
;
2445 float d
= __strtof_internal
2446 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2447 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2448 *ARG (float *) = negative
? -d
: d
;
2451 if (__glibc_unlikely (tw
== char_buffer_start (&charbuf
)))
2454 if (!(flags
& SUPPRESS
))
2458 case L_('['): /* Character class. */
2460 STRING_ARG (wstr
, wchar_t, 100);
2462 STRING_ARG (str
, char, 100);
2473 /* There is no width given so there is also no limit on the
2474 number of characters we read. Therefore we set width to
2475 a very high value to make the algorithm easier. */
2478 #ifdef COMPILE_WSCANF
2479 /* Find the beginning and the end of the scanlist. We are not
2480 creating a lookup table since it would have to be too large.
2481 Instead we search each time through the string. This is not
2482 a constant lookup time but who uses this feature deserves to
2484 tw
= (wchar_t *) f
; /* Marks the beginning. */
2489 while ((fc
= *f
++) != L
'\0' && fc
!= L
']');
2491 if (__glibc_unlikely (fc
== L
'\0'))
2493 wchar_t *twend
= (wchar_t *) f
- 1;
2495 /* Fill WP with byte flags indexed by character.
2496 We will use this flag map for matching input characters. */
2497 if (!scratch_buffer_set_array_size
2498 (&charbuf
.scratch
, UCHAR_MAX
+ 1, 1))
2503 memset (charbuf
.scratch
.data
, '\0', UCHAR_MAX
+ 1);
2506 if (fc
== ']' || fc
== '-')
2508 /* If ] or - appears before any char in the set, it is not
2509 the terminator or separator, but the first char in the
2511 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2515 while ((fc
= *f
++) != '\0' && fc
!= ']')
2516 if (fc
== '-' && *f
!= '\0' && *f
!= ']'
2517 && (unsigned char) f
[-2] <= (unsigned char) *f
)
2519 /* Add all characters from the one before the '-'
2520 up to (but not including) the next format char. */
2521 for (fc
= (unsigned char) f
[-2]; fc
< (unsigned char) *f
; ++fc
)
2522 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2525 /* Add the character to the flag map. */
2526 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2528 if (__glibc_unlikely (fc
== '\0'))
2534 size_t now
= read_in
;
2535 #ifdef COMPILE_WSCANF
2536 if (__glibc_unlikely (inchar () == WEOF
))
2543 /* Test whether it's in the scanlist. */
2545 while (runp
< twend
)
2547 if (runp
[0] == L
'-' && runp
[1] != '\0'
2548 && runp
+ 1 != twend
2550 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2552 /* Match against all characters in between the
2553 first and last character of the sequence. */
2556 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2557 if ((wint_t) wc
== c
)
2560 if (wc
<= runp
[1] && !not_in
)
2562 if (wc
<= runp
[1] && not_in
)
2564 /* The current character is not in the
2574 if ((wint_t) *runp
== c
&& !not_in
)
2576 if ((wint_t) *runp
== c
&& not_in
)
2586 if (runp
== twend
&& !not_in
)
2592 if (!(flags
& SUPPRESS
))
2596 if ((flags
& MALLOC
)
2597 && wstr
== (wchar_t *) *strptr
+ strsize
)
2599 /* Enlarge the buffer. */
2600 wstr
= (wchar_t *) realloc (*strptr
,
2602 * sizeof (wchar_t));
2605 /* Can't allocate that much. Last-ditch
2608 realloc (*strptr
, (strsize
+ 1)
2609 * sizeof (wchar_t));
2612 if (flags
& POSIX_MALLOC
)
2617 /* We lose. Oh well. Terminate the string
2618 and stop converting, so at least we don't
2620 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2627 *strptr
= (char *) wstr
;
2634 *strptr
= (char *) wstr
;
2641 while (--width
> 0 && inchar () != WEOF
);
2644 char buf
[MB_LEN_MAX
];
2648 if (__glibc_unlikely (inchar () == EOF
))
2651 memset (&cstate
, '\0', sizeof (cstate
));
2655 if (((char *) charbuf
.scratch
.data
)[c
] == not_in
)
2657 ungetc_not_eof (c
, s
);
2662 if (!(flags
& SUPPRESS
))
2666 /* Convert it into a wide character. */
2668 n
= __mbrtowc (wstr
, buf
, 1, &cstate
);
2670 if (n
== (size_t) -2)
2672 /* Possibly correct character, just not enough
2675 assert (cnt
< MB_LEN_MAX
);
2681 if ((flags
& MALLOC
)
2682 && wstr
== (wchar_t *) *strptr
+ strsize
)
2684 /* Enlarge the buffer. */
2685 wstr
= (wchar_t *) realloc (*strptr
,
2687 * sizeof (wchar_t)));
2690 /* Can't allocate that much. Last-ditch
2693 realloc (*strptr
, ((strsize
+ 1)
2694 * sizeof (wchar_t)));
2697 if (flags
& POSIX_MALLOC
)
2702 /* We lose. Oh well. Terminate the
2703 string and stop converting,
2704 so at least we don't skip any input. */
2705 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2712 *strptr
= (char *) wstr
;
2719 *strptr
= (char *) wstr
;
2729 while (inchar () != EOF
);
2731 if (__glibc_unlikely (cnt
!= 0))
2732 /* We stopped in the middle of recognizing another
2733 character. That's a problem. */
2737 if (__glibc_unlikely (now
== read_in
))
2738 /* We haven't succesfully read any character. */
2741 if (!(flags
& SUPPRESS
))
2745 if ((flags
& MALLOC
)
2746 && wstr
- (wchar_t *) *strptr
!= strsize
)
2748 wchar_t *cp
= (wchar_t *)
2749 realloc (*strptr
, ((wstr
- (wchar_t *) *strptr
)
2750 * sizeof(wchar_t)));
2752 *strptr
= (char *) cp
;
2761 size_t now
= read_in
;
2763 if (__glibc_unlikely (inchar () == EOF
))
2766 #ifdef COMPILE_WSCANF
2768 memset (&state
, '\0', sizeof (state
));
2775 /* Test whether it's in the scanlist. */
2777 while (runp
< twend
)
2779 if (runp
[0] == L
'-' && runp
[1] != '\0'
2780 && runp
+ 1 != twend
2782 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2784 /* Match against all characters in between the
2785 first and last character of the sequence. */
2788 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2789 if ((wint_t) wc
== c
)
2792 if (wc
<= runp
[1] && !not_in
)
2794 if (wc
<= runp
[1] && not_in
)
2796 /* The current character is not in the
2806 if ((wint_t) *runp
== c
&& !not_in
)
2808 if ((wint_t) *runp
== c
&& not_in
)
2818 if (runp
== twend
&& !not_in
)
2824 if (!(flags
& SUPPRESS
))
2826 if ((flags
& MALLOC
)
2827 && *strptr
+ strsize
- str
<= MB_LEN_MAX
)
2829 /* Enlarge the buffer. */
2830 size_t strleng
= str
- *strptr
;
2833 newstr
= (char *) realloc (*strptr
, 2 * strsize
);
2836 /* Can't allocate that much. Last-ditch
2838 newstr
= (char *) realloc (*strptr
,
2839 strleng
+ MB_LEN_MAX
);
2842 if (flags
& POSIX_MALLOC
)
2847 /* We lose. Oh well. Terminate the string
2848 and stop converting, so at least we don't
2850 ((char *) (*strptr
))[strleng
] = '\0';
2858 str
= newstr
+ strleng
;
2859 strsize
= strleng
+ MB_LEN_MAX
;
2865 str
= newstr
+ strleng
;
2871 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
2872 if (__glibc_unlikely (n
== (size_t) -1))
2875 assert (n
<= MB_LEN_MAX
);
2878 while (--width
> 0 && inchar () != WEOF
);
2883 if (((char *) charbuf
.scratch
.data
)[c
] == not_in
)
2885 ungetc_not_eof (c
, s
);
2890 if (!(flags
& SUPPRESS
))
2893 if ((flags
& MALLOC
)
2894 && (char *) str
== *strptr
+ strsize
)
2896 /* Enlarge the buffer. */
2897 size_t newsize
= 2 * strsize
;
2900 str
= (char *) realloc (*strptr
, newsize
);
2903 /* Can't allocate that much. Last-ditch
2905 if (newsize
> strsize
+ 1)
2907 newsize
= strsize
+ 1;
2910 if (flags
& POSIX_MALLOC
)
2915 /* We lose. Oh well. Terminate the
2916 string and stop converting,
2917 so at least we don't skip any input. */
2918 ((char *) (*strptr
))[strsize
- 1] = '\0';
2925 *strptr
= (char *) str
;
2932 while (--width
> 0 && inchar () != EOF
);
2935 if (__glibc_unlikely (now
== read_in
))
2936 /* We haven't succesfully read any character. */
2939 if (!(flags
& SUPPRESS
))
2941 #ifdef COMPILE_WSCANF
2942 /* We have to emit the code to get into the initial
2944 char buf
[MB_LEN_MAX
];
2945 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
2946 if (n
> 0 && (flags
& MALLOC
)
2947 && str
+ n
>= *strptr
+ strsize
)
2949 /* Enlarge the buffer. */
2950 size_t strleng
= str
- *strptr
;
2953 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
2956 if (flags
& POSIX_MALLOC
)
2961 /* We lose. Oh well. Terminate the string
2962 and stop converting, so at least we don't
2964 ((char *) (*strptr
))[strleng
] = '\0';
2972 str
= newstr
+ strleng
;
2973 strsize
= strleng
+ n
+ 1;
2977 str
= __mempcpy (str
, buf
, n
);
2981 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
2983 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
2994 case L_('p'): /* Generic pointer. */
2996 /* A PTR must be the same size as a `long int'. */
2997 flags
&= ~(SHORT
|LONGDBL
);
3000 flags
|= READ_POINTER
;
3004 /* If this is an unknown format character punt. */
3009 /* The last thing we saw int the format string was a white space.
3010 Consume the last white spaces. */
3015 while (ISSPACE (c
));
3020 /* Unlock stream. */
3023 scratch_buffer_free (&charbuf
.scratch
);
3027 if (__glibc_unlikely (done
== EOF
))
3029 if (__glibc_unlikely (ptrs_to_free
!= NULL
))
3031 struct ptrs_to_free
*p
= ptrs_to_free
;
3034 for (size_t cnt
= 0; cnt
< p
->count
; ++cnt
)
3036 free (*p
->ptrs
[cnt
]);
3037 *p
->ptrs
[cnt
] = NULL
;
3044 else if (__glibc_unlikely (strptr
!= NULL
))
3052 #ifdef COMPILE_WSCANF
3054 __vfwscanf (FILE *s
, const wchar_t *format
, va_list argptr
)
3056 return _IO_vfwscanf (s
, format
, argptr
, NULL
);
3058 ldbl_weak_alias (__vfwscanf
, vfwscanf
)
3061 ___vfscanf (FILE *s
, const char *format
, va_list argptr
)
3063 return _IO_vfscanf_internal (s
, format
, argptr
, NULL
);
3065 ldbl_strong_alias (_IO_vfscanf_internal
, _IO_vfscanf
)
3066 ldbl_hidden_def (_IO_vfscanf_internal
, _IO_vfscanf
)
3067 ldbl_strong_alias (___vfscanf
, __vfscanf
)
3068 ldbl_hidden_def (___vfscanf
, __vfscanf
)
3069 ldbl_weak_alias (___vfscanf
, vfscanf
)