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 va_list _IO_va_list
80 # define ungetc(c, s) ((void) (c == WEOF \
82 _IO_sputbackwc (s, c))))
83 # define ungetc_not_eof(c, s) ((void) (--read_in, \
84 _IO_sputbackwc (s, c)))
85 # define inchar() (c == WEOF ? ((errno = inchar_errno), WEOF) \
86 : ((c = _IO_getwc_unlocked (s)), \
89 : (size_t) (inchar_errno = errno)), c))
91 # define ISSPACE(Ch) iswspace (Ch)
92 # define ISDIGIT(Ch) iswdigit (Ch)
93 # define ISXDIGIT(Ch) iswxdigit (Ch)
94 # define TOLOWER(Ch) towlower (Ch)
95 # define ORIENT if (_IO_fwide (s, 1) != 1) return WEOF
96 # define __strtoll_internal __wcstoll_internal
97 # define __strtoull_internal __wcstoull_internal
98 # define __strtol_internal __wcstol_internal
99 # define __strtoul_internal __wcstoul_internal
100 # define __strtold_internal __wcstold_internal
101 # define __strtod_internal __wcstod_internal
102 # define __strtof_internal __wcstof_internal
104 # define L_(Str) L##Str
105 # define CHAR_T wchar_t
106 # define UCHAR_T unsigned int
107 # define WINT_T wint_t
111 # define ungetc(c, s) ((void) ((int) c == EOF \
113 _IO_sputbackc (s, (unsigned char) c))))
114 # define ungetc_not_eof(c, s) ((void) (--read_in, \
115 _IO_sputbackc (s, (unsigned char) c)))
116 # define inchar() (c == EOF ? ((errno = inchar_errno), EOF) \
117 : ((c = _IO_getc_unlocked (s)), \
120 : (size_t) (inchar_errno = errno)), c))
121 # define ISSPACE(Ch) __isspace_l (Ch, loc)
122 # define ISDIGIT(Ch) __isdigit_l (Ch, loc)
123 # define ISXDIGIT(Ch) __isxdigit_l (Ch, loc)
124 # define TOLOWER(Ch) __tolower_l ((unsigned char) (Ch), loc)
125 # define ORIENT if (_IO_vtable_offset (s) == 0 \
126 && _IO_fwide (s, -1) != -1) \
131 # define UCHAR_T unsigned char
135 #include "printf-parse.h" /* Use read_int. */
137 #define encode_error() do { \
139 __set_errno (EILSEQ); \
142 #define conv_error() do { \
146 #define input_error() do { \
148 if (done == 0) done = EOF; \
151 #define add_ptr_to_free(ptr) \
154 if (ptrs_to_free == NULL \
155 || ptrs_to_free->count == (sizeof (ptrs_to_free->ptrs) \
156 / sizeof (ptrs_to_free->ptrs[0]))) \
158 struct ptrs_to_free *new_ptrs = alloca (sizeof (*ptrs_to_free)); \
159 new_ptrs->count = 0; \
160 new_ptrs->next = ptrs_to_free; \
161 ptrs_to_free = new_ptrs; \
163 ptrs_to_free->ptrs[ptrs_to_free->count++] = (ptr); \
166 #define ARGCHECK(s, format) \
169 /* Check file argument for consistence. */ \
170 CHECK_FILE (s, EOF); \
171 if (s->_flags & _IO_NO_READS) \
173 __set_errno (EBADF); \
176 else if (format == NULL) \
182 #define LOCK_STREAM(S) \
183 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, (S)); \
185 #define UNLOCK_STREAM(S) \
186 _IO_funlockfile (S); \
187 __libc_cleanup_region_end (0)
192 struct ptrs_to_free
*next
;
199 struct scratch_buffer scratch
;
202 /* Returns a pointer to the first CHAR_T object in the buffer. Only
203 valid if char_buffer_add (BUFFER, CH) has been called and
204 char_buffer_error (BUFFER) is false. */
205 static inline CHAR_T
*
206 char_buffer_start (const struct char_buffer
*buffer
)
208 return (CHAR_T
*) buffer
->scratch
.data
;
211 /* Returns the number of CHAR_T objects in the buffer. Only valid if
212 char_buffer_error (BUFFER) is false. */
214 char_buffer_size (const struct char_buffer
*buffer
)
216 return buffer
->current
- char_buffer_start (buffer
);
219 /* Reinitializes BUFFER->current and BUFFER->end to cover the entire
222 char_buffer_rewind (struct char_buffer
*buffer
)
224 buffer
->current
= char_buffer_start (buffer
);
225 buffer
->end
= buffer
->current
+ buffer
->scratch
.length
/ sizeof (CHAR_T
);
228 /* Returns true if a previous call to char_buffer_add (BUFFER, CH)
231 char_buffer_error (const struct char_buffer
*buffer
)
233 return __glibc_unlikely (buffer
->current
== NULL
);
236 /* Slow path for char_buffer_add. */
238 char_buffer_add_slow (struct char_buffer
*buffer
, CHAR_T ch
)
240 if (char_buffer_error (buffer
))
242 size_t offset
= buffer
->end
- (CHAR_T
*) buffer
->scratch
.data
;
243 if (!scratch_buffer_grow_preserve (&buffer
->scratch
))
245 buffer
->current
= NULL
;
249 char_buffer_rewind (buffer
);
250 buffer
->current
+= offset
;
251 *buffer
->current
++ = ch
;
254 /* Adds CH to BUFFER. This function does not report any errors, check
255 for them with char_buffer_error. */
257 char_buffer_add (struct char_buffer
*buffer
, CHAR_T ch
)
258 __attribute__ ((always_inline
));
260 char_buffer_add (struct char_buffer
*buffer
, CHAR_T ch
)
262 if (__glibc_unlikely (buffer
->current
== buffer
->end
))
263 char_buffer_add_slow (buffer
, ch
);
265 *buffer
->current
++ = ch
;
268 /* Read formatted input from S according to the format string
269 FORMAT, using the argument list in ARG.
270 Return the number of assignments made, or -1 for an input error. */
271 #ifdef COMPILE_WSCANF
273 _IO_vfwscanf (_IO_FILE
*s
, const wchar_t *format
, _IO_va_list argptr
,
277 _IO_vfscanf_internal (_IO_FILE
*s
, const char *format
, _IO_va_list argptr
,
282 const CHAR_T
*f
= format
;
283 UCHAR_T fc
; /* Current character of the format. */
284 WINT_T done
= 0; /* Assignments done. */
285 size_t read_in
= 0; /* Chars read in. */
286 WINT_T c
= 0; /* Last char read. */
287 int width
; /* Maximum field width. */
288 int flags
; /* Modifiers for current format element. */
290 #ifndef COMPILE_WSCANF
291 locale_t loc
= _NL_CURRENT_LOCALE
;
292 struct __locale_data
*const curctype
= loc
->__locales
[LC_CTYPE
];
295 /* Errno of last failed inchar call. */
296 int inchar_errno
= 0;
297 /* Status for reading F-P nums. */
298 char got_digit
, got_dot
, got_e
, negative
;
299 /* If a [...] is a [^...]. */
301 #define exp_char not_in
302 /* Base for integral numbers. */
304 /* Decimal point character. */
305 #ifdef COMPILE_WSCANF
310 /* The thousands character of the current locale. */
311 #ifdef COMPILE_WSCANF
314 const char *thousands
;
316 struct ptrs_to_free
*ptrs_to_free
= NULL
;
317 /* State for the conversions. */
319 /* Integral holding variables. */
323 unsigned long long int uq
;
325 unsigned long int ul
;
327 /* Character-buffer pointer. */
329 wchar_t *wstr
= NULL
;
330 char **strptr
= NULL
;
332 /* We must not react on white spaces immediately because they can
333 possibly be matched even if in the input stream no character is
334 available anymore. */
337 CHAR_T
*tw
; /* Temporary pointer. */
338 struct char_buffer charbuf
;
339 scratch_buffer_init (&charbuf
.scratch
);
342 __va_copy (arg
, argptr
);
344 arg
= (va_list) argptr
;
351 ARGCHECK (s
, format
);
354 #ifndef COMPILE_WSCANF
355 struct __locale_data
*const curnumeric
= loc
->__locales
[LC_NUMERIC
];
358 /* Figure out the decimal point character. */
359 #ifdef COMPILE_WSCANF
360 decimal
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
362 decimal
= curnumeric
->values
[_NL_ITEM_INDEX (DECIMAL_POINT
)].string
;
364 /* Figure out the thousands separator character. */
365 #ifdef COMPILE_WSCANF
366 thousands
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_THOUSANDS_SEP_WC
);
368 thousands
= curnumeric
->values
[_NL_ITEM_INDEX (THOUSANDS_SEP
)].string
;
369 if (*thousands
== '\0')
374 /* Lock the stream. */
378 #ifndef COMPILE_WSCANF
379 /* From now on we use `state' to convert the format string. */
380 memset (&state
, '\0', sizeof (state
));
383 /* Run through the format string. */
387 /* Extract the next argument, which is of type TYPE.
388 For a %N$... spec, this is the Nth argument from the beginning;
389 otherwise it is the next argument after the state now in ARG. */
391 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
392 ({ unsigned int pos = argpos; \
394 __va_copy (arg, argptr); \
396 (void) va_arg (arg, void *); \
397 va_arg (arg, type); \
401 /* XXX Possible optimization. */
402 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
403 ({ va_list arg = (va_list) argptr; \
404 arg = (va_list) ((char *) arg \
406 * __va_rounded_size (void *)); \
407 va_arg (arg, type); \
410 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
411 ({ unsigned int pos = argpos; \
412 va_list arg = (va_list) argptr; \
414 (void) va_arg (arg, void *); \
415 va_arg (arg, type); \
420 #ifndef COMPILE_WSCANF
421 if (!isascii ((unsigned char) *f
))
423 /* Non-ASCII, may be a multibyte. */
424 int len
= __mbrlen (f
, strlen (f
), &state
);
430 if (__glibc_unlikely (c
== EOF
))
432 else if (c
!= (unsigned char) *f
++)
434 ungetc_not_eof (c
, s
);
447 /* Remember to skip spaces. */
454 /* Read a character. */
457 /* Characters other than format specs must just match. */
458 if (__glibc_unlikely (c
== EOF
))
461 /* We saw white space char as the last character in the format
462 string. Now it's time to skip all leading white space. */
466 if (__glibc_unlikely (inchar () == EOF
))
471 if (__glibc_unlikely (c
!= fc
))
480 /* This is the start of the conversion string. */
483 /* Initialize state of modifiers. */
486 /* Prepare temporary buffer. */
487 char_buffer_rewind (&charbuf
);
489 /* Check for a positional parameter specification. */
490 if (ISDIGIT ((UCHAR_T
) *f
))
492 argpos
= read_int ((const UCHAR_T
**) &f
);
497 /* Oops; that was actually the field width. */
504 /* Check for the assignment-suppressing, the number grouping flag,
505 and the signal to use the locale's digit representation. */
506 while (*f
== L_('*') || *f
== L_('\'') || *f
== L_('I'))
513 #ifdef COMPILE_WSCANF
514 if (thousands
!= L
'\0')
516 if (thousands
!= NULL
)
525 /* Find the maximum field width. */
527 if (ISDIGIT ((UCHAR_T
) *f
))
528 width
= read_int ((const UCHAR_T
**) &f
);
533 /* Check for type modifiers. */
537 /* ints are short ints or chars. */
549 /* A double `l' is equivalent to an `L'. */
551 flags
|= LONGDBL
| LONG
;
554 /* ints are long ints. */
559 /* doubles are long doubles, and ints are long long ints. */
560 flags
|= LONGDBL
| LONG
;
563 /* The `a' is used as a flag only if followed by `s', `S' or
565 if (*f
!= L_('s') && *f
!= L_('S') && *f
!= L_('['))
570 /* In __isoc99_*scanf %as, %aS and %a[ extension is not
572 if (s
->_flags2
& _IO_FLAGS2_SCANF_STD
)
577 /* String conversions (%s, %[) take a `char **'
578 arg and fill it in with a malloc'd pointer. */
582 flags
|= POSIX_MALLOC
;
590 if (need_longlong
&& sizeof (size_t) > sizeof (unsigned long int))
592 else if (sizeof (size_t) > sizeof (unsigned int))
596 if (need_longlong
&& sizeof (uintmax_t) > sizeof (unsigned long int))
598 else if (sizeof (uintmax_t) > sizeof (unsigned int))
602 if (need_longlong
&& sizeof (ptrdiff_t) > sizeof (long int))
604 else if (sizeof (ptrdiff_t) > sizeof (int))
608 /* Not a recognized modifier. Backup. */
613 /* End of the format string? */
614 if (__glibc_unlikely (*f
== L_('\0')))
617 /* Find the conversion specifier. */
619 if (skip_space
|| (fc
!= L_('[') && fc
!= L_('c')
620 && fc
!= L_('C') && fc
!= L_('n')))
622 /* Eat whitespace. */
623 int save_errno
= errno
;
626 /* We add the additional test for EOF here since otherwise
627 inchar will restore the old errno value which might be
628 EINTR but does not indicate an interrupt since nothing
629 was read at this time. */
630 if (__builtin_expect ((c
== EOF
|| inchar () == EOF
)
631 && errno
== EINTR
, 0))
634 __set_errno (save_errno
);
641 case L_('%'): /* Must match a literal '%'. */
643 if (__glibc_unlikely (c
== EOF
))
645 if (__glibc_unlikely (c
!= fc
))
647 ungetc_not_eof (c
, s
);
652 case L_('n'): /* Answer number of assignments done. */
653 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
654 with the 'n' conversion specifier. */
655 if (!(flags
& SUPPRESS
))
657 /* Don't count the read-ahead. */
658 if (need_longlong
&& (flags
& LONGDBL
))
659 *ARG (long long int *) = read_in
;
660 else if (need_long
&& (flags
& LONG
))
661 *ARG (long int *) = read_in
;
662 else if (flags
& SHORT
)
663 *ARG (short int *) = read_in
;
664 else if (!(flags
& CHAR
))
665 *ARG (int *) = read_in
;
667 *ARG (char *) = read_in
;
669 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
670 /* We have a severe problem here. The ISO C standard
671 contradicts itself in explaining the effect of the %n
672 format in `scanf'. While in ISO C:1990 and the ISO C
673 Amendement 1:1995 the result is described as
675 Execution of a %n directive does not effect the
676 assignment count returned at the completion of
677 execution of the f(w)scanf function.
679 in ISO C Corrigendum 1:1994 the following was added:
682 Add the following fourth example:
685 int d1, d2, n1, n2, i;
686 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
687 the value 123 is assigned to d1 and the value3 to n1.
688 Because %n can never get an input failure the value
689 of 3 is also assigned to n2. The value of d2 is not
690 affected. The value 3 is assigned to i.
692 We go for now with the historically correct code from ISO C,
693 i.e., we don't count the %n assignments. When it ever
694 should proof to be wrong just remove the #ifdef above. */
700 case L_('c'): /* Match characters. */
701 if ((flags
& LONG
) == 0)
706 #define STRING_ARG(Str, Type, Width) \
707 do if (!(flags & SUPPRESS)) \
709 if (flags & MALLOC) \
711 /* The string is to be stored in a malloc'd buffer. */ \
712 /* For %mS using char ** is actually wrong, but \
713 shouldn't make a difference on any arch glibc \
714 supports and would unnecessarily complicate \
716 strptr = ARG (char **); \
717 if (strptr == NULL) \
719 /* Allocate an initial buffer. */ \
721 *strptr = (char *) malloc (strsize * sizeof (Type)); \
722 Str = (Type *) *strptr; \
724 add_ptr_to_free (strptr); \
725 else if (flags & POSIX_MALLOC) \
732 Str = ARG (Type *); \
736 #ifdef COMPILE_WSCANF
737 STRING_ARG (str
, char, 100);
739 STRING_ARG (str
, char, (width
> 1024 ? 1024 : width
));
743 if (__glibc_unlikely (c
== EOF
))
746 #ifdef COMPILE_WSCANF
747 /* We have to convert the wide character(s) into multibyte
748 characters and store the result. */
749 memset (&state
, '\0', sizeof (state
));
755 if (!(flags
& SUPPRESS
) && (flags
& POSIX_MALLOC
)
756 && *strptr
+ strsize
- str
<= MB_LEN_MAX
)
758 /* We have to enlarge the buffer if the `m' flag
760 size_t strleng
= str
- *strptr
;
763 newstr
= (char *) realloc (*strptr
, strsize
* 2);
766 /* Can't allocate that much. Last-ditch effort. */
767 newstr
= (char *) realloc (*strptr
,
768 strleng
+ MB_LEN_MAX
);
771 /* c can't have `a' flag, only `m'. */
778 str
= newstr
+ strleng
;
779 strsize
= strleng
+ MB_LEN_MAX
;
785 str
= newstr
+ strleng
;
790 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
791 if (__glibc_unlikely (n
== (size_t) -1))
792 /* No valid wide character. */
795 /* Increment the output pointer. Even if we don't
799 while (--width
> 0 && inchar () != EOF
);
801 if (!(flags
& SUPPRESS
))
806 && (char *) str
== *strptr
+ strsize
)
808 /* Enlarge the buffer. */
811 + (strsize
>= width
? width
- 1 : strsize
);
813 str
= (char *) realloc (*strptr
, newsize
);
816 /* Can't allocate that much. Last-ditch
818 str
= (char *) realloc (*strptr
, strsize
+ 1);
821 /* c can't have `a' flag, only `m'. */
827 *strptr
= (char *) str
;
834 *strptr
= (char *) str
;
841 while (--width
> 0 && inchar () != EOF
);
844 while (--width
> 0 && inchar () != EOF
);
847 if (!(flags
& SUPPRESS
))
849 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
851 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
866 STRING_ARG (wstr
, wchar_t, (width
> 1024 ? 1024 : width
));
869 if (__glibc_unlikely (c
== EOF
))
872 #ifdef COMPILE_WSCANF
873 /* Just store the incoming wide characters. */
874 if (!(flags
& SUPPRESS
))
879 && wstr
== (wchar_t *) *strptr
+ strsize
)
882 = strsize
+ (strsize
> width
? width
- 1 : strsize
);
883 /* Enlarge the buffer. */
884 wstr
= (wchar_t *) realloc (*strptr
,
885 newsize
* sizeof (wchar_t));
888 /* Can't allocate that much. Last-ditch effort. */
889 wstr
= (wchar_t *) realloc (*strptr
,
894 /* C or lc can't have `a' flag, only `m'
901 *strptr
= (char *) wstr
;
908 *strptr
= (char *) wstr
;
915 while (--width
> 0 && inchar () != EOF
);
918 while (--width
> 0 && inchar () != EOF
);
921 /* We have to convert the multibyte input sequence to wide
926 memset (&cstate
, '\0', sizeof (cstate
));
930 /* This is what we present the mbrtowc function first. */
933 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
934 && wstr
== (wchar_t *) *strptr
+ strsize
)
937 = strsize
+ (strsize
> width
? width
- 1 : strsize
);
938 /* Enlarge the buffer. */
939 wstr
= (wchar_t *) realloc (*strptr
,
940 newsize
* sizeof (wchar_t));
943 /* Can't allocate that much. Last-ditch effort. */
944 wstr
= (wchar_t *) realloc (*strptr
,
946 * sizeof (wchar_t)));
949 /* C or lc can't have `a' flag, only `m' flag. */
955 *strptr
= (char *) wstr
;
962 *strptr
= (char *) wstr
;
972 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
975 if (n
== (size_t) -2)
977 /* Possibly correct character, just not enough
979 if (__glibc_unlikely (inchar () == EOF
))
986 if (__glibc_unlikely (n
!= 1))
989 /* We have a match. */
993 /* Advance the result pointer. */
996 while (--width
> 0 && inchar () != EOF
);
1000 if (!(flags
& SUPPRESS
))
1002 if ((flags
& MALLOC
) && wstr
- (wchar_t *) *strptr
!= strsize
)
1004 wchar_t *cp
= (wchar_t *) realloc (*strptr
,
1006 - (wchar_t *) *strptr
)
1007 * sizeof (wchar_t)));
1009 *strptr
= (char *) cp
;
1018 case L_('s'): /* Read a string. */
1019 if (!(flags
& LONG
))
1021 STRING_ARG (str
, char, 100);
1024 if (__glibc_unlikely (c
== EOF
))
1027 #ifdef COMPILE_WSCANF
1028 memset (&state
, '\0', sizeof (state
));
1035 ungetc_not_eof (c
, s
);
1039 #ifdef COMPILE_WSCANF
1040 /* This is quite complicated. We have to convert the
1041 wide characters into multibyte characters and then
1046 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
1047 && *strptr
+ strsize
- str
<= MB_LEN_MAX
)
1049 /* We have to enlarge the buffer if the `a' or `m'
1051 size_t strleng
= str
- *strptr
;
1054 newstr
= (char *) realloc (*strptr
, strsize
* 2);
1057 /* Can't allocate that much. Last-ditch
1059 newstr
= (char *) realloc (*strptr
,
1060 strleng
+ MB_LEN_MAX
);
1063 if (flags
& POSIX_MALLOC
)
1068 /* We lose. Oh well. Terminate the
1069 string and stop converting,
1070 so at least we don't skip any input. */
1071 ((char *) (*strptr
))[strleng
] = '\0';
1079 str
= newstr
+ strleng
;
1080 strsize
= strleng
+ MB_LEN_MAX
;
1086 str
= newstr
+ strleng
;
1091 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
,
1093 if (__glibc_unlikely (n
== (size_t) -1))
1096 assert (n
<= MB_LEN_MAX
);
1101 if (!(flags
& SUPPRESS
))
1104 if ((flags
& MALLOC
)
1105 && (char *) str
== *strptr
+ strsize
)
1107 /* Enlarge the buffer. */
1108 str
= (char *) realloc (*strptr
, 2 * strsize
);
1111 /* Can't allocate that much. Last-ditch
1113 str
= (char *) realloc (*strptr
, strsize
+ 1);
1116 if (flags
& POSIX_MALLOC
)
1121 /* We lose. Oh well. Terminate the
1122 string and stop converting,
1123 so at least we don't skip any input. */
1124 ((char *) (*strptr
))[strsize
- 1] = '\0';
1131 *strptr
= (char *) str
;
1138 *strptr
= (char *) str
;
1146 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
1148 if (!(flags
& SUPPRESS
))
1150 #ifdef COMPILE_WSCANF
1151 /* We have to emit the code to get into the initial
1153 char buf
[MB_LEN_MAX
];
1154 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
1155 if (n
> 0 && (flags
& MALLOC
)
1156 && str
+ n
>= *strptr
+ strsize
)
1158 /* Enlarge the buffer. */
1159 size_t strleng
= str
- *strptr
;
1162 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
1165 if (flags
& POSIX_MALLOC
)
1170 /* We lose. Oh well. Terminate the string
1171 and stop converting, so at least we don't
1173 ((char *) (*strptr
))[strleng
] = '\0';
1181 str
= newstr
+ strleng
;
1182 strsize
= strleng
+ n
+ 1;
1186 str
= __mempcpy (str
, buf
, n
);
1190 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
1192 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
1206 #ifndef COMPILE_WSCANF
1210 /* Wide character string. */
1211 STRING_ARG (wstr
, wchar_t, 100);
1214 if (__builtin_expect (c
== EOF
, 0))
1217 #ifndef COMPILE_WSCANF
1218 memset (&cstate
, '\0', sizeof (cstate
));
1225 ungetc_not_eof (c
, s
);
1229 #ifdef COMPILE_WSCANF
1231 if (!(flags
& SUPPRESS
))
1234 if ((flags
& MALLOC
)
1235 && wstr
== (wchar_t *) *strptr
+ strsize
)
1237 /* Enlarge the buffer. */
1238 wstr
= (wchar_t *) realloc (*strptr
,
1240 * sizeof (wchar_t));
1243 /* Can't allocate that much. Last-ditch
1245 wstr
= (wchar_t *) realloc (*strptr
,
1247 * sizeof (wchar_t));
1250 if (flags
& POSIX_MALLOC
)
1255 /* We lose. Oh well. Terminate the string
1256 and stop converting, so at least we don't
1258 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1265 *strptr
= (char *) wstr
;
1272 *strptr
= (char *) wstr
;
1288 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
1291 if (n
== (size_t) -2)
1293 /* Possibly correct character, just not enough
1295 if (__glibc_unlikely (inchar () == EOF
))
1302 if (__glibc_unlikely (n
!= 1))
1305 /* We have a match. */
1310 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
1311 && wstr
== (wchar_t *) *strptr
+ strsize
)
1313 /* Enlarge the buffer. */
1314 wstr
= (wchar_t *) realloc (*strptr
,
1316 * sizeof (wchar_t)));
1319 /* Can't allocate that much. Last-ditch effort. */
1320 wstr
= (wchar_t *) realloc (*strptr
,
1322 * sizeof (wchar_t)));
1325 if (flags
& POSIX_MALLOC
)
1330 /* We lose. Oh well. Terminate the
1331 string and stop converting, so at
1332 least we don't skip any input. */
1333 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1340 *strptr
= (char *) wstr
;
1347 *strptr
= (char *) wstr
;
1355 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
1357 if (!(flags
& SUPPRESS
))
1361 if ((flags
& MALLOC
) && wstr
- (wchar_t *) *strptr
!= strsize
)
1363 wchar_t *cp
= (wchar_t *) realloc (*strptr
,
1365 - (wchar_t *) *strptr
)
1366 * sizeof(wchar_t)));
1368 *strptr
= (char *) cp
;
1377 case L_('x'): /* Hexadecimal integer. */
1378 case L_('X'): /* Ditto. */
1382 case L_('o'): /* Octal integer. */
1386 case L_('u'): /* Unsigned decimal integer. */
1390 case L_('d'): /* Signed decimal integer. */
1392 flags
|= NUMBER_SIGNED
;
1395 case L_('i'): /* Generic number. */
1397 flags
|= NUMBER_SIGNED
;
1401 if (__glibc_unlikely (c
== EOF
))
1404 /* Check for a sign. */
1405 if (c
== L_('-') || c
== L_('+'))
1407 char_buffer_add (&charbuf
, c
);
1413 /* Look for a leading indication of base. */
1414 if (width
!= 0 && c
== L_('0'))
1419 char_buffer_add (&charbuf
, c
);
1422 if (width
!= 0 && TOLOWER (c
) == L_('x'))
1440 if (base
== 10 && __builtin_expect ((flags
& I18N
) != 0, 0))
1445 #ifdef COMPILE_WSCANF
1446 const wchar_t *wcdigits
[10];
1447 const wchar_t *wcdigits_extended
[10];
1449 const char *mbdigits
[10];
1450 const char *mbdigits_extended
[10];
1452 /* "to_inpunct" is a map from ASCII digits to their
1453 equivalent in locale. This is defined for locales
1454 which use an extra digits set. */
1455 wctrans_t map
= __wctrans ("to_inpunct");
1459 #ifdef COMPILE_WSCANF
1460 to_level
= _NL_CURRENT_WORD (LC_CTYPE
,
1461 _NL_CTYPE_INDIGITS_WC_LEN
) - 1;
1463 to_level
= (uint32_t) curctype
->values
[_NL_ITEM_INDEX (_NL_CTYPE_INDIGITS_MB_LEN
)].word
- 1;
1466 /* Get the alternative digit forms if there are any. */
1467 if (__glibc_unlikely (map
!= NULL
))
1469 /* Adding new level for extra digits set in locale file. */
1472 for (n
= 0; n
< 10; ++n
)
1474 #ifdef COMPILE_WSCANF
1475 wcdigits
[n
] = (const wchar_t *)
1476 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1478 wchar_t *wc_extended
= (wchar_t *)
1479 alloca ((to_level
+ 2) * sizeof (wchar_t));
1480 __wmemcpy (wc_extended
, wcdigits
[n
], to_level
);
1481 wc_extended
[to_level
] = __towctrans (L
'0' + n
, map
);
1482 wc_extended
[to_level
+ 1] = '\0';
1483 wcdigits_extended
[n
] = wc_extended
;
1486 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1488 /* Get the equivalent wide char in map. */
1489 wint_t extra_wcdigit
= __towctrans (L
'0' + n
, map
);
1491 /* Convert it to multibyte representation. */
1493 memset (&state
, '\0', sizeof (state
));
1495 char extra_mbdigit
[MB_LEN_MAX
];
1497 = __wcrtomb (extra_mbdigit
, extra_wcdigit
, &state
);
1499 if (mblen
== (size_t) -1)
1501 /* Ignore this new level. */
1506 /* Calculate the length of mbdigits[n]. */
1507 const char *last_char
= mbdigits
[n
];
1508 for (level
= 0; level
< to_level
; ++level
)
1509 last_char
= strchr (last_char
, '\0') + 1;
1511 size_t mbdigits_len
= last_char
- mbdigits
[n
];
1513 /* Allocate memory for extended multibyte digit. */
1515 mb_extended
= (char *) alloca (mbdigits_len
+ mblen
+ 1);
1517 /* And get the mbdigits + extra_digit string. */
1518 *(char *) __mempcpy (__mempcpy (mb_extended
, mbdigits
[n
],
1520 extra_mbdigit
, mblen
) = '\0';
1521 mbdigits_extended
[n
] = mb_extended
;
1526 /* Read the number into workspace. */
1527 while (c
!= EOF
&& width
!= 0)
1529 /* In this round we get the pointer to the digit strings
1530 and also perform the first round of comparisons. */
1531 for (n
= 0; n
< 10; ++n
)
1533 /* Get the string for the digits with value N. */
1534 #ifdef COMPILE_WSCANF
1536 /* wcdigits_extended[] is fully set in the loop
1537 above, but the test for "map != NULL" is done
1538 inside the loop here and outside the loop there. */
1539 DIAG_PUSH_NEEDS_COMMENT
;
1540 DIAG_IGNORE_NEEDS_COMMENT (4.7, "-Wmaybe-uninitialized");
1542 if (__glibc_unlikely (map
!= NULL
))
1543 wcdigits
[n
] = wcdigits_extended
[n
];
1545 wcdigits
[n
] = (const wchar_t *)
1546 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1547 wcdigits
[n
] += from_level
;
1549 DIAG_POP_NEEDS_COMMENT
;
1551 if (c
== (wint_t) *wcdigits
[n
])
1553 to_level
= from_level
;
1557 /* Advance the pointer to the next string. */
1561 int avail
= width
> 0 ? width
: INT_MAX
;
1563 if (__glibc_unlikely (map
!= NULL
))
1564 mbdigits
[n
] = mbdigits_extended
[n
];
1567 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1569 for (level
= 0; level
< from_level
; level
++)
1570 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1573 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1575 if (*++cmpp
== '\0')
1579 if (avail
== 0 || inchar () == EOF
)
1589 to_level
= from_level
;
1593 /* We are pushing all read characters back. */
1594 if (cmpp
> mbdigits
[n
])
1597 while (--cmpp
> mbdigits
[n
])
1598 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1599 c
= (unsigned char) *cmpp
;
1602 /* Advance the pointer to the next string. */
1603 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1609 /* Have not yet found the digit. */
1610 for (level
= from_level
+ 1; level
<= to_level
; ++level
)
1612 /* Search all ten digits of this level. */
1613 for (n
= 0; n
< 10; ++n
)
1615 #ifdef COMPILE_WSCANF
1616 if (c
== (wint_t) *wcdigits
[n
])
1619 /* Advance the pointer to the next string. */
1623 int avail
= width
> 0 ? width
: INT_MAX
;
1626 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1628 if (*++cmpp
== '\0')
1632 if (avail
== 0 || inchar () == EOF
)
1645 /* We are pushing all read characters back. */
1646 if (cmpp
> mbdigits
[n
])
1649 while (--cmpp
> mbdigits
[n
])
1650 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1651 c
= (unsigned char) *cmpp
;
1654 /* Advance the pointer to the next string. */
1655 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1671 else if (flags
& GROUP
)
1673 /* Try matching against the thousands separator. */
1674 #ifdef COMPILE_WSCANF
1678 const char *cmpp
= thousands
;
1679 int avail
= width
> 0 ? width
: INT_MAX
;
1681 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1683 char_buffer_add (&charbuf
, c
);
1684 if (*++cmpp
== '\0')
1688 if (avail
== 0 || inchar () == EOF
)
1694 if (char_buffer_error (&charbuf
))
1696 __set_errno (ENOMEM
);
1703 /* We are pushing all read characters back. */
1704 if (cmpp
> thousands
)
1706 charbuf
.current
-= cmpp
- thousands
;
1708 while (--cmpp
> thousands
)
1709 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1710 c
= (unsigned char) *cmpp
;
1718 /* The last thousands character will be added back by
1719 the char_buffer_add below. */
1726 char_buffer_add (&charbuf
, c
);
1734 /* Read the number into workspace. */
1735 while (c
!= EOF
&& width
!= 0)
1742 else if (!ISDIGIT (c
) || (int) (c
- L_('0')) >= base
)
1744 if (base
== 10 && (flags
& GROUP
))
1746 /* Try matching against the thousands separator. */
1747 #ifdef COMPILE_WSCANF
1751 const char *cmpp
= thousands
;
1752 int avail
= width
> 0 ? width
: INT_MAX
;
1754 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1756 char_buffer_add (&charbuf
, c
);
1757 if (*++cmpp
== '\0')
1761 if (avail
== 0 || inchar () == EOF
)
1767 if (char_buffer_error (&charbuf
))
1769 __set_errno (ENOMEM
);
1776 /* We are pushing all read characters back. */
1777 if (cmpp
> thousands
)
1779 charbuf
.current
-= cmpp
- thousands
;
1781 while (--cmpp
> thousands
)
1782 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1783 c
= (unsigned char) *cmpp
;
1791 /* The last thousands character will be added back by
1792 the char_buffer_add below. */
1799 char_buffer_add (&charbuf
, c
);
1806 if (char_buffer_error (&charbuf
))
1808 __set_errno (ENOMEM
);
1813 if (char_buffer_size (&charbuf
) == 0
1814 || (char_buffer_size (&charbuf
) == 1
1815 && (char_buffer_start (&charbuf
)[0] == L_('+')
1816 || char_buffer_start (&charbuf
)[0] == L_('-'))))
1818 /* There was no number. If we are supposed to read a pointer
1819 we must recognize "(nil)" as well. */
1820 if (__builtin_expect (char_buffer_size (&charbuf
) == 0
1821 && (flags
& READ_POINTER
)
1822 && (width
< 0 || width
>= 5)
1824 && TOLOWER (inchar ()) == L_('n')
1825 && TOLOWER (inchar ()) == L_('i')
1826 && TOLOWER (inchar ()) == L_('l')
1827 && inchar () == L_(')'), 1))
1828 /* We must produce the value of a NULL pointer. A single
1829 '0' digit is enough. */
1830 char_buffer_add (&charbuf
, L_('0'));
1833 /* The last read character is not part of the number
1841 /* The just read character is not part of the number anymore. */
1844 /* Convert the number. */
1845 char_buffer_add (&charbuf
, L_('\0'));
1846 if (char_buffer_error (&charbuf
))
1848 __set_errno (ENOMEM
);
1852 if (need_longlong
&& (flags
& LONGDBL
))
1854 if (flags
& NUMBER_SIGNED
)
1855 num
.q
= __strtoll_internal
1856 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1858 num
.uq
= __strtoull_internal
1859 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1863 if (flags
& NUMBER_SIGNED
)
1864 num
.l
= __strtol_internal
1865 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1867 num
.ul
= __strtoul_internal
1868 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1870 if (__glibc_unlikely (char_buffer_start (&charbuf
) == tw
))
1873 if (!(flags
& SUPPRESS
))
1875 if (flags
& NUMBER_SIGNED
)
1877 if (need_longlong
&& (flags
& LONGDBL
))
1878 *ARG (LONGLONG
int *) = num
.q
;
1879 else if (need_long
&& (flags
& LONG
))
1880 *ARG (long int *) = num
.l
;
1881 else if (flags
& SHORT
)
1882 *ARG (short int *) = (short int) num
.l
;
1883 else if (!(flags
& CHAR
))
1884 *ARG (int *) = (int) num
.l
;
1886 *ARG (signed char *) = (signed char) num
.ul
;
1890 if (need_longlong
&& (flags
& LONGDBL
))
1891 *ARG (unsigned LONGLONG
int *) = num
.uq
;
1892 else if (need_long
&& (flags
& LONG
))
1893 *ARG (unsigned long int *) = num
.ul
;
1894 else if (flags
& SHORT
)
1895 *ARG (unsigned short int *)
1896 = (unsigned short int) num
.ul
;
1897 else if (!(flags
& CHAR
))
1898 *ARG (unsigned int *) = (unsigned int) num
.ul
;
1900 *ARG (unsigned char *) = (unsigned char) num
.ul
;
1906 case L_('e'): /* Floating-point numbers. */
1917 if (__glibc_unlikely (c
== EOF
))
1920 got_digit
= got_dot
= got_e
= 0;
1922 /* Check for a sign. */
1923 if (c
== L_('-') || c
== L_('+'))
1925 negative
= c
== L_('-');
1926 if (__glibc_unlikely (width
== 0 || inchar () == EOF
))
1927 /* EOF is only an input error before we read any chars. */
1935 /* Take care for the special arguments "nan" and "inf". */
1936 if (TOLOWER (c
) == L_('n'))
1939 char_buffer_add (&charbuf
, c
);
1940 if (__builtin_expect (width
== 0
1942 || TOLOWER (c
) != L_('a'), 0))
1946 char_buffer_add (&charbuf
, c
);
1947 if (__builtin_expect (width
== 0
1949 || TOLOWER (c
) != L_('n'), 0))
1953 char_buffer_add (&charbuf
, c
);
1957 else if (TOLOWER (c
) == L_('i'))
1959 /* Maybe "inf" or "infinity". */
1960 char_buffer_add (&charbuf
, c
);
1961 if (__builtin_expect (width
== 0
1963 || TOLOWER (c
) != L_('n'), 0))
1967 char_buffer_add (&charbuf
, c
);
1968 if (__builtin_expect (width
== 0
1970 || TOLOWER (c
) != L_('f'), 0))
1974 char_buffer_add (&charbuf
, c
);
1975 /* It is as least "inf". */
1976 if (width
!= 0 && inchar () != EOF
)
1978 if (TOLOWER (c
) == L_('i'))
1982 /* Now we have to read the rest as well. */
1983 char_buffer_add (&charbuf
, c
);
1984 if (__builtin_expect (width
== 0
1986 || TOLOWER (c
) != L_('n'), 0))
1990 char_buffer_add (&charbuf
, c
);
1991 if (__builtin_expect (width
== 0
1993 || TOLOWER (c
) != L_('i'), 0))
1997 char_buffer_add (&charbuf
, c
);
1998 if (__builtin_expect (width
== 0
2000 || TOLOWER (c
) != L_('t'), 0))
2004 char_buffer_add (&charbuf
, c
);
2005 if (__builtin_expect (width
== 0
2007 || TOLOWER (c
) != L_('y'), 0))
2011 char_buffer_add (&charbuf
, c
);
2021 if (width
!= 0 && c
== L_('0'))
2023 char_buffer_add (&charbuf
, c
);
2027 if (width
!= 0 && TOLOWER (c
) == L_('x'))
2029 /* It is a number in hexadecimal format. */
2030 char_buffer_add (&charbuf
, c
);
2032 flags
|= HEXA_FLOAT
;
2035 /* Grouping is not allowed. */
2047 if (char_buffer_error (&charbuf
))
2049 __set_errno (ENOMEM
);
2055 char_buffer_add (&charbuf
, c
);
2058 else if (!got_e
&& (flags
& HEXA_FLOAT
) && ISXDIGIT (c
))
2060 char_buffer_add (&charbuf
, c
);
2063 else if (got_e
&& charbuf
.current
[-1] == exp_char
2064 && (c
== L_('-') || c
== L_('+')))
2065 char_buffer_add (&charbuf
, c
);
2066 else if (got_digit
&& !got_e
2067 && (CHAR_T
) TOLOWER (c
) == exp_char
)
2069 char_buffer_add (&charbuf
, exp_char
);
2070 got_e
= got_dot
= 1;
2074 #ifdef COMPILE_WSCANF
2075 if (! got_dot
&& c
== decimal
)
2077 char_buffer_add (&charbuf
, c
);
2080 else if ((flags
& GROUP
) != 0 && ! got_dot
&& c
== thousands
)
2081 char_buffer_add (&charbuf
, c
);
2084 /* The last read character is not part of the number
2090 const char *cmpp
= decimal
;
2091 int avail
= width
> 0 ? width
: INT_MAX
;
2095 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
2096 if (*++cmpp
== '\0')
2100 if (avail
== 0 || inchar () == EOF
)
2108 /* Add all the characters. */
2109 for (cmpp
= decimal
; *cmpp
!= '\0'; ++cmpp
)
2110 char_buffer_add (&charbuf
, (unsigned char) *cmpp
);
2117 /* Figure out whether it is a thousands separator.
2118 There is one problem: we possibly read more than
2119 one character. We cannot push them back but since
2120 we know that parts of the `decimal' string matched,
2121 we can compare against it. */
2122 const char *cmp2p
= thousands
;
2124 if ((flags
& GROUP
) != 0 && ! got_dot
)
2126 while (cmp2p
- thousands
< cmpp
- decimal
2127 && *cmp2p
== decimal
[cmp2p
- thousands
])
2129 if (cmp2p
- thousands
== cmpp
- decimal
)
2131 while ((unsigned char) *cmp2p
== c
&& avail
>= 0)
2132 if (*++cmp2p
== '\0')
2136 if (avail
== 0 || inchar () == EOF
)
2143 if (cmp2p
!= NULL
&& *cmp2p
== '\0')
2145 /* Add all the characters. */
2146 for (cmpp
= thousands
; *cmpp
!= '\0'; ++cmpp
)
2147 char_buffer_add (&charbuf
, (unsigned char) *cmpp
);
2153 /* The last read character is not part of the number
2162 if (width
== 0 || inchar () == EOF
)
2169 if (char_buffer_error (&charbuf
))
2171 __set_errno (ENOMEM
);
2177 if (__builtin_expect ((flags
& I18N
) != 0, 0)
2178 /* Hexadecimal floats make no sense, fixing localized
2179 digits with ASCII letters. */
2180 && !(flags
& HEXA_FLOAT
)
2181 /* Minimum requirement. */
2182 && (char_buffer_size (&charbuf
) == 0 || got_dot
)
2183 && (map
= __wctrans ("to_inpunct")) != NULL
)
2185 /* Reget the first character. */
2188 /* Localized digits, decimal points, and thousands
2190 wint_t wcdigits
[12];
2192 /* First get decimal equivalent to check if we read it
2194 wcdigits
[11] = __towctrans (L
'.', map
);
2196 /* If we have not read any character or have just read
2197 locale decimal point which matches the decimal point
2198 for localized FP numbers, then we may have localized
2199 digits. Note, we test GOT_DOT above. */
2200 #ifdef COMPILE_WSCANF
2201 if (char_buffer_size (&charbuf
) == 0
2202 || (char_buffer_size (&charbuf
) == 1
2203 && wcdigits
[11] == decimal
))
2205 char mbdigits
[12][MB_LEN_MAX
+ 1];
2208 memset (&state
, '\0', sizeof (state
));
2210 bool match_so_far
= char_buffer_size (&charbuf
) == 0;
2211 size_t mblen
= __wcrtomb (mbdigits
[11], wcdigits
[11], &state
);
2212 if (mblen
!= (size_t) -1)
2214 mbdigits
[11][mblen
] = '\0';
2216 (char_buffer_size (&charbuf
) == strlen (decimal
)
2217 && strcmp (decimal
, mbdigits
[11]) == 0);
2221 size_t decimal_len
= strlen (decimal
);
2222 /* This should always be the case but the data comes
2224 if (decimal_len
<= MB_LEN_MAX
)
2226 match_so_far
|= char_buffer_size (&charbuf
) == decimal_len
;
2227 memcpy (mbdigits
[11], decimal
, decimal_len
+ 1);
2230 match_so_far
= false;
2236 bool have_locthousands
= (flags
& GROUP
) != 0;
2238 /* Now get the digits and the thousands-sep equivalents. */
2239 for (int n
= 0; n
< 11; ++n
)
2242 wcdigits
[n
] = __towctrans (L
'0' + n
, map
);
2245 wcdigits
[10] = __towctrans (L
',', map
);
2246 have_locthousands
&= wcdigits
[10] != L
'\0';
2249 #ifndef COMPILE_WSCANF
2250 memset (&state
, '\0', sizeof (state
));
2252 size_t mblen
= __wcrtomb (mbdigits
[n
], wcdigits
[n
],
2254 if (mblen
== (size_t) -1)
2258 if (have_locthousands
)
2260 size_t thousands_len
= strlen (thousands
);
2261 if (thousands_len
<= MB_LEN_MAX
)
2262 memcpy (mbdigits
[10], thousands
,
2265 have_locthousands
= false;
2269 /* Ignore checking against localized digits. */
2273 mbdigits
[n
][mblen
] = '\0';
2277 /* Start checking against localized digits, if
2278 conversion is done correctly. */
2281 if (char_buffer_error (&charbuf
))
2283 __set_errno (ENOMEM
);
2287 if (got_e
&& charbuf
.current
[-1] == exp_char
2288 && (c
== L_('-') || c
== L_('+')))
2289 char_buffer_add (&charbuf
, c
);
2290 else if (char_buffer_size (&charbuf
) > 0 && !got_e
2291 && (CHAR_T
) TOLOWER (c
) == exp_char
)
2293 char_buffer_add (&charbuf
, exp_char
);
2294 got_e
= got_dot
= 1;
2298 /* Check against localized digits, decimal point,
2299 and thousands separator. */
2301 for (n
= 0; n
< 12; ++n
)
2303 #ifdef COMPILE_WSCANF
2304 if (c
== wcdigits
[n
])
2307 char_buffer_add (&charbuf
, L_('0') + n
);
2308 else if (n
== 11 && !got_dot
)
2310 char_buffer_add (&charbuf
, decimal
);
2313 else if (n
== 10 && have_locthousands
2315 char_buffer_add (&charbuf
, thousands
);
2317 /* The last read character is not part
2318 of the number anymore. */
2324 const char *cmpp
= mbdigits
[n
];
2325 int avail
= width
> 0 ? width
: INT_MAX
;
2327 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
2328 if (*++cmpp
== '\0')
2332 if (avail
== 0 || inchar () == EOF
)
2342 char_buffer_add (&charbuf
, L_('0') + n
);
2343 else if (n
== 11 && !got_dot
)
2345 /* Add all the characters. */
2346 for (cmpp
= decimal
; *cmpp
!= '\0';
2348 char_buffer_add (&charbuf
,
2349 (unsigned char) *cmpp
);
2353 else if (n
== 10 && (flags
& GROUP
) != 0
2356 /* Add all the characters. */
2357 for (cmpp
= thousands
; *cmpp
!= '\0';
2359 char_buffer_add (&charbuf
,
2360 (unsigned char) *cmpp
);
2363 /* The last read character is not part
2364 of the number anymore. */
2370 /* We are pushing all read characters back. */
2371 if (cmpp
> mbdigits
[n
])
2374 while (--cmpp
> mbdigits
[n
])
2375 ungetc_not_eof ((unsigned char) *cmpp
, s
);
2376 c
= (unsigned char) *cmpp
;
2383 /* The last read character is not part
2384 of the number anymore. */
2390 if (width
== 0 || inchar () == EOF
)
2398 #ifndef COMPILE_WSCANF
2404 if (char_buffer_error (&charbuf
))
2406 __set_errno (ENOMEM
);
2411 /* Have we read any character? If we try to read a number
2412 in hexadecimal notation and we have read only the `0x'
2413 prefix this is an error. */
2414 if (__glibc_unlikely (char_buffer_size (&charbuf
) == 0
2415 || ((flags
& HEXA_FLOAT
)
2416 && char_buffer_size (&charbuf
) == 2)))
2420 /* Convert the number. */
2421 char_buffer_add (&charbuf
, L_('\0'));
2422 if (char_buffer_error (&charbuf
))
2424 __set_errno (ENOMEM
);
2428 if ((flags
& LONGDBL
) && !__ldbl_is_dbl
)
2430 long double d
= __strtold_internal
2431 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2432 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2433 *ARG (long double *) = negative
? -d
: d
;
2435 else if (flags
& (LONG
| LONGDBL
))
2437 double d
= __strtod_internal
2438 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2439 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2440 *ARG (double *) = negative
? -d
: d
;
2444 float d
= __strtof_internal
2445 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2446 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2447 *ARG (float *) = negative
? -d
: d
;
2450 if (__glibc_unlikely (tw
== char_buffer_start (&charbuf
)))
2453 if (!(flags
& SUPPRESS
))
2457 case L_('['): /* Character class. */
2459 STRING_ARG (wstr
, wchar_t, 100);
2461 STRING_ARG (str
, char, 100);
2472 /* There is no width given so there is also no limit on the
2473 number of characters we read. Therefore we set width to
2474 a very high value to make the algorithm easier. */
2477 #ifdef COMPILE_WSCANF
2478 /* Find the beginning and the end of the scanlist. We are not
2479 creating a lookup table since it would have to be too large.
2480 Instead we search each time through the string. This is not
2481 a constant lookup time but who uses this feature deserves to
2483 tw
= (wchar_t *) f
; /* Marks the beginning. */
2488 while ((fc
= *f
++) != L
'\0' && fc
!= L
']');
2490 if (__glibc_unlikely (fc
== L
'\0'))
2492 wchar_t *twend
= (wchar_t *) f
- 1;
2494 /* Fill WP with byte flags indexed by character.
2495 We will use this flag map for matching input characters. */
2496 if (!scratch_buffer_set_array_size
2497 (&charbuf
.scratch
, UCHAR_MAX
+ 1, 1))
2502 memset (charbuf
.scratch
.data
, '\0', UCHAR_MAX
+ 1);
2505 if (fc
== ']' || fc
== '-')
2507 /* If ] or - appears before any char in the set, it is not
2508 the terminator or separator, but the first char in the
2510 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2514 while ((fc
= *f
++) != '\0' && fc
!= ']')
2515 if (fc
== '-' && *f
!= '\0' && *f
!= ']'
2516 && (unsigned char) f
[-2] <= (unsigned char) *f
)
2518 /* Add all characters from the one before the '-'
2519 up to (but not including) the next format char. */
2520 for (fc
= (unsigned char) f
[-2]; fc
< (unsigned char) *f
; ++fc
)
2521 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2524 /* Add the character to the flag map. */
2525 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2527 if (__glibc_unlikely (fc
== '\0'))
2533 size_t now
= read_in
;
2534 #ifdef COMPILE_WSCANF
2535 if (__glibc_unlikely (inchar () == WEOF
))
2542 /* Test whether it's in the scanlist. */
2544 while (runp
< twend
)
2546 if (runp
[0] == L
'-' && runp
[1] != '\0'
2547 && runp
+ 1 != twend
2549 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2551 /* Match against all characters in between the
2552 first and last character of the sequence. */
2555 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2556 if ((wint_t) wc
== c
)
2559 if (wc
<= runp
[1] && !not_in
)
2561 if (wc
<= runp
[1] && not_in
)
2563 /* The current character is not in the
2573 if ((wint_t) *runp
== c
&& !not_in
)
2575 if ((wint_t) *runp
== c
&& not_in
)
2585 if (runp
== twend
&& !not_in
)
2591 if (!(flags
& SUPPRESS
))
2595 if ((flags
& MALLOC
)
2596 && wstr
== (wchar_t *) *strptr
+ strsize
)
2598 /* Enlarge the buffer. */
2599 wstr
= (wchar_t *) realloc (*strptr
,
2601 * sizeof (wchar_t));
2604 /* Can't allocate that much. Last-ditch
2607 realloc (*strptr
, (strsize
+ 1)
2608 * sizeof (wchar_t));
2611 if (flags
& POSIX_MALLOC
)
2616 /* We lose. Oh well. Terminate the string
2617 and stop converting, so at least we don't
2619 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2626 *strptr
= (char *) wstr
;
2633 *strptr
= (char *) wstr
;
2640 while (--width
> 0 && inchar () != WEOF
);
2643 char buf
[MB_LEN_MAX
];
2647 if (__glibc_unlikely (inchar () == EOF
))
2650 memset (&cstate
, '\0', sizeof (cstate
));
2654 if (((char *) charbuf
.scratch
.data
)[c
] == not_in
)
2656 ungetc_not_eof (c
, s
);
2661 if (!(flags
& SUPPRESS
))
2665 /* Convert it into a wide character. */
2667 n
= __mbrtowc (wstr
, buf
, 1, &cstate
);
2669 if (n
== (size_t) -2)
2671 /* Possibly correct character, just not enough
2674 assert (cnt
< MB_LEN_MAX
);
2680 if ((flags
& MALLOC
)
2681 && wstr
== (wchar_t *) *strptr
+ strsize
)
2683 /* Enlarge the buffer. */
2684 wstr
= (wchar_t *) realloc (*strptr
,
2686 * sizeof (wchar_t)));
2689 /* Can't allocate that much. Last-ditch
2692 realloc (*strptr
, ((strsize
+ 1)
2693 * sizeof (wchar_t)));
2696 if (flags
& POSIX_MALLOC
)
2701 /* We lose. Oh well. Terminate the
2702 string and stop converting,
2703 so at least we don't skip any input. */
2704 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2711 *strptr
= (char *) wstr
;
2718 *strptr
= (char *) wstr
;
2728 while (inchar () != EOF
);
2730 if (__glibc_unlikely (cnt
!= 0))
2731 /* We stopped in the middle of recognizing another
2732 character. That's a problem. */
2736 if (__glibc_unlikely (now
== read_in
))
2737 /* We haven't succesfully read any character. */
2740 if (!(flags
& SUPPRESS
))
2744 if ((flags
& MALLOC
)
2745 && wstr
- (wchar_t *) *strptr
!= strsize
)
2747 wchar_t *cp
= (wchar_t *)
2748 realloc (*strptr
, ((wstr
- (wchar_t *) *strptr
)
2749 * sizeof(wchar_t)));
2751 *strptr
= (char *) cp
;
2760 size_t now
= read_in
;
2762 if (__glibc_unlikely (inchar () == EOF
))
2765 #ifdef COMPILE_WSCANF
2767 memset (&state
, '\0', sizeof (state
));
2774 /* Test whether it's in the scanlist. */
2776 while (runp
< twend
)
2778 if (runp
[0] == L
'-' && runp
[1] != '\0'
2779 && runp
+ 1 != twend
2781 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2783 /* Match against all characters in between the
2784 first and last character of the sequence. */
2787 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2788 if ((wint_t) wc
== c
)
2791 if (wc
<= runp
[1] && !not_in
)
2793 if (wc
<= runp
[1] && not_in
)
2795 /* The current character is not in the
2805 if ((wint_t) *runp
== c
&& !not_in
)
2807 if ((wint_t) *runp
== c
&& not_in
)
2817 if (runp
== twend
&& !not_in
)
2823 if (!(flags
& SUPPRESS
))
2825 if ((flags
& MALLOC
)
2826 && *strptr
+ strsize
- str
<= MB_LEN_MAX
)
2828 /* Enlarge the buffer. */
2829 size_t strleng
= str
- *strptr
;
2832 newstr
= (char *) realloc (*strptr
, 2 * strsize
);
2835 /* Can't allocate that much. Last-ditch
2837 newstr
= (char *) realloc (*strptr
,
2838 strleng
+ MB_LEN_MAX
);
2841 if (flags
& POSIX_MALLOC
)
2846 /* We lose. Oh well. Terminate the string
2847 and stop converting, so at least we don't
2849 ((char *) (*strptr
))[strleng
] = '\0';
2857 str
= newstr
+ strleng
;
2858 strsize
= strleng
+ MB_LEN_MAX
;
2864 str
= newstr
+ strleng
;
2870 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
2871 if (__glibc_unlikely (n
== (size_t) -1))
2874 assert (n
<= MB_LEN_MAX
);
2877 while (--width
> 0 && inchar () != WEOF
);
2882 if (((char *) charbuf
.scratch
.data
)[c
] == not_in
)
2884 ungetc_not_eof (c
, s
);
2889 if (!(flags
& SUPPRESS
))
2892 if ((flags
& MALLOC
)
2893 && (char *) str
== *strptr
+ strsize
)
2895 /* Enlarge the buffer. */
2896 size_t newsize
= 2 * strsize
;
2899 str
= (char *) realloc (*strptr
, newsize
);
2902 /* Can't allocate that much. Last-ditch
2904 if (newsize
> strsize
+ 1)
2906 newsize
= strsize
+ 1;
2909 if (flags
& POSIX_MALLOC
)
2914 /* We lose. Oh well. Terminate the
2915 string and stop converting,
2916 so at least we don't skip any input. */
2917 ((char *) (*strptr
))[strsize
- 1] = '\0';
2924 *strptr
= (char *) str
;
2931 while (--width
> 0 && inchar () != EOF
);
2934 if (__glibc_unlikely (now
== read_in
))
2935 /* We haven't succesfully read any character. */
2938 if (!(flags
& SUPPRESS
))
2940 #ifdef COMPILE_WSCANF
2941 /* We have to emit the code to get into the initial
2943 char buf
[MB_LEN_MAX
];
2944 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
2945 if (n
> 0 && (flags
& MALLOC
)
2946 && str
+ n
>= *strptr
+ strsize
)
2948 /* Enlarge the buffer. */
2949 size_t strleng
= str
- *strptr
;
2952 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
2955 if (flags
& POSIX_MALLOC
)
2960 /* We lose. Oh well. Terminate the string
2961 and stop converting, so at least we don't
2963 ((char *) (*strptr
))[strleng
] = '\0';
2971 str
= newstr
+ strleng
;
2972 strsize
= strleng
+ n
+ 1;
2976 str
= __mempcpy (str
, buf
, n
);
2980 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
2982 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
2993 case L_('p'): /* Generic pointer. */
2995 /* A PTR must be the same size as a `long int'. */
2996 flags
&= ~(SHORT
|LONGDBL
);
2999 flags
|= READ_POINTER
;
3003 /* If this is an unknown format character punt. */
3008 /* The last thing we saw int the format string was a white space.
3009 Consume the last white spaces. */
3014 while (ISSPACE (c
));
3019 /* Unlock stream. */
3022 scratch_buffer_free (&charbuf
.scratch
);
3026 if (__glibc_unlikely (done
== EOF
))
3028 if (__glibc_unlikely (ptrs_to_free
!= NULL
))
3030 struct ptrs_to_free
*p
= ptrs_to_free
;
3033 for (size_t cnt
= 0; cnt
< p
->count
; ++cnt
)
3035 free (*p
->ptrs
[cnt
]);
3036 *p
->ptrs
[cnt
] = NULL
;
3043 else if (__glibc_unlikely (strptr
!= NULL
))
3051 #ifdef COMPILE_WSCANF
3053 __vfwscanf (FILE *s
, const wchar_t *format
, va_list argptr
)
3055 return _IO_vfwscanf (s
, format
, argptr
, NULL
);
3057 ldbl_weak_alias (__vfwscanf
, vfwscanf
)
3060 ___vfscanf (FILE *s
, const char *format
, va_list argptr
)
3062 return _IO_vfscanf_internal (s
, format
, argptr
, NULL
);
3064 ldbl_strong_alias (_IO_vfscanf_internal
, _IO_vfscanf
)
3065 ldbl_hidden_def (_IO_vfscanf_internal
, _IO_vfscanf
)
3066 ldbl_strong_alias (___vfscanf
, __vfscanf
)
3067 ldbl_hidden_def (___vfscanf
, __vfscanf
)
3068 ldbl_weak_alias (___vfscanf
, vfscanf
)