1 /* Copyright (C) 1991-2016 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-lock.h>
31 #include <locale/localeinfo.h>
32 #include <scratch_buffer.h>
35 # define HAVE_LONGLONG
36 # define LONGLONG long long
38 # define LONGLONG long
41 /* Determine whether we have to handle `long long' at all. */
42 #if LONG_MAX == LONG_LONG_MAX
43 # define need_longlong 0
45 # define need_longlong 1
48 /* Determine whether we have to handle `long'. */
49 #if INT_MAX == LONG_MAX
55 /* Those are flags in the conversion format. */
56 #define LONG 0x0001 /* l: long or double */
57 #define LONGDBL 0x0002 /* L: long long or long double */
58 #define SHORT 0x0004 /* h: short */
59 #define SUPPRESS 0x0008 /* *: suppress assignment */
60 #define POINTER 0x0010 /* weird %p pointer (`fake hex') */
61 #define NOSKIP 0x0020 /* do not skip blanks */
62 #define NUMBER_SIGNED 0x0040 /* signed integer */
63 #define GROUP 0x0080 /* ': group numbers */
64 #define GNU_MALLOC 0x0100 /* a: malloc strings */
65 #define CHAR 0x0200 /* hh: char */
66 #define I18N 0x0400 /* I: use locale's digits */
67 #define HEXA_FLOAT 0x0800 /* hexadecimal float */
68 #define READ_POINTER 0x1000 /* this is a pointer value */
69 #define POSIX_MALLOC 0x2000 /* m: malloc strings */
70 #define MALLOC (GNU_MALLOC | POSIX_MALLOC)
72 #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 #define encode_error() do { \
137 __set_errno (EILSEQ); \
140 #define conv_error() do { \
144 #define input_error() do { \
146 if (done == 0) done = EOF; \
149 #define add_ptr_to_free(ptr) \
152 if (ptrs_to_free == NULL \
153 || ptrs_to_free->count == (sizeof (ptrs_to_free->ptrs) \
154 / sizeof (ptrs_to_free->ptrs[0]))) \
156 struct ptrs_to_free *new_ptrs = alloca (sizeof (*ptrs_to_free)); \
157 new_ptrs->count = 0; \
158 new_ptrs->next = ptrs_to_free; \
159 ptrs_to_free = new_ptrs; \
161 ptrs_to_free->ptrs[ptrs_to_free->count++] = (ptr); \
164 #define ARGCHECK(s, format) \
167 /* Check file argument for consistence. */ \
168 CHECK_FILE (s, EOF); \
169 if (s->_flags & _IO_NO_READS) \
171 __set_errno (EBADF); \
174 else if (format == NULL) \
180 #define LOCK_STREAM(S) \
181 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, (S)); \
183 #define UNLOCK_STREAM(S) \
184 _IO_funlockfile (S); \
185 __libc_cleanup_region_end (0)
190 struct ptrs_to_free
*next
;
197 struct scratch_buffer scratch
;
200 /* Returns a pointer to the first CHAR_T object in the buffer. Only
201 valid if char_buffer_add (BUFFER, CH) has been called and
202 char_buffer_error (BUFFER) is false. */
203 static inline CHAR_T
*
204 char_buffer_start (const struct char_buffer
*buffer
)
206 return (CHAR_T
*) buffer
->scratch
.data
;
209 /* Returns the number of CHAR_T objects in the buffer. Only valid if
210 char_buffer_error (BUFFER) is false. */
212 char_buffer_size (const struct char_buffer
*buffer
)
214 return buffer
->current
- char_buffer_start (buffer
);
217 /* Reinitializes BUFFER->current and BUFFER->end to cover the entire
220 char_buffer_rewind (struct char_buffer
*buffer
)
222 buffer
->current
= char_buffer_start (buffer
);
223 buffer
->end
= buffer
->current
+ buffer
->scratch
.length
/ sizeof (CHAR_T
);
226 /* Returns true if a previous call to char_buffer_add (BUFFER, CH)
229 char_buffer_error (const struct char_buffer
*buffer
)
231 return __glibc_unlikely (buffer
->current
== NULL
);
234 /* Slow path for char_buffer_add. */
236 char_buffer_add_slow (struct char_buffer
*buffer
, CHAR_T ch
)
238 if (char_buffer_error (buffer
))
240 size_t offset
= buffer
->end
- (CHAR_T
*) buffer
->scratch
.data
;
241 if (!scratch_buffer_grow_preserve (&buffer
->scratch
))
243 buffer
->current
= NULL
;
247 char_buffer_rewind (buffer
);
248 buffer
->current
+= offset
;
249 *buffer
->current
++ = ch
;
252 /* Adds CH to BUFFER. This function does not report any errors, check
253 for them with char_buffer_error. */
255 char_buffer_add (struct char_buffer
*buffer
, CHAR_T ch
)
256 __attribute__ ((always_inline
));
258 char_buffer_add (struct char_buffer
*buffer
, CHAR_T ch
)
260 if (__glibc_unlikely (buffer
->current
== buffer
->end
))
261 char_buffer_add_slow (buffer
, ch
);
263 *buffer
->current
++ = ch
;
266 /* Read formatted input from S according to the format string
267 FORMAT, using the argument list in ARG.
268 Return the number of assignments made, or -1 for an input error. */
269 #ifdef COMPILE_WSCANF
271 _IO_vfwscanf (_IO_FILE
*s
, const wchar_t *format
, _IO_va_list argptr
,
275 _IO_vfscanf_internal (_IO_FILE
*s
, const char *format
, _IO_va_list argptr
,
280 const CHAR_T
*f
= format
;
281 UCHAR_T fc
; /* Current character of the format. */
282 WINT_T done
= 0; /* Assignments done. */
283 size_t read_in
= 0; /* Chars read in. */
284 WINT_T c
= 0; /* Last char read. */
285 int width
; /* Maximum field width. */
286 int flags
; /* Modifiers for current format element. */
288 #ifndef COMPILE_WSCANF
289 __locale_t loc
= _NL_CURRENT_LOCALE
;
290 struct __locale_data
*const curctype
= loc
->__locales
[LC_CTYPE
];
293 /* Errno of last failed inchar call. */
294 int inchar_errno
= 0;
295 /* Status for reading F-P nums. */
296 char got_digit
, got_dot
, got_e
, negative
;
297 /* If a [...] is a [^...]. */
299 #define exp_char not_in
300 /* Base for integral numbers. */
302 /* Decimal point character. */
303 #ifdef COMPILE_WSCANF
308 /* The thousands character of the current locale. */
309 #ifdef COMPILE_WSCANF
312 const char *thousands
;
314 struct ptrs_to_free
*ptrs_to_free
= NULL
;
315 /* State for the conversions. */
317 /* Integral holding variables. */
321 unsigned long long int uq
;
323 unsigned long int ul
;
325 /* Character-buffer pointer. */
327 wchar_t *wstr
= NULL
;
328 char **strptr
= NULL
;
330 /* We must not react on white spaces immediately because they can
331 possibly be matched even if in the input stream no character is
332 available anymore. */
335 CHAR_T
*tw
; /* Temporary pointer. */
336 struct char_buffer charbuf
;
337 scratch_buffer_init (&charbuf
.scratch
);
340 __va_copy (arg
, argptr
);
342 arg
= (va_list) argptr
;
349 ARGCHECK (s
, format
);
352 #ifndef COMPILE_WSCANF
353 struct __locale_data
*const curnumeric
= loc
->__locales
[LC_NUMERIC
];
356 /* Figure out the decimal point character. */
357 #ifdef COMPILE_WSCANF
358 decimal
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
360 decimal
= curnumeric
->values
[_NL_ITEM_INDEX (DECIMAL_POINT
)].string
;
362 /* Figure out the thousands separator character. */
363 #ifdef COMPILE_WSCANF
364 thousands
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_THOUSANDS_SEP_WC
);
366 thousands
= curnumeric
->values
[_NL_ITEM_INDEX (THOUSANDS_SEP
)].string
;
367 if (*thousands
== '\0')
372 /* Lock the stream. */
376 #ifndef COMPILE_WSCANF
377 /* From now on we use `state' to convert the format string. */
378 memset (&state
, '\0', sizeof (state
));
381 /* Run through the format string. */
385 /* Extract the next argument, which is of type TYPE.
386 For a %N$... spec, this is the Nth argument from the beginning;
387 otherwise it is the next argument after the state now in ARG. */
389 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
390 ({ unsigned int pos = argpos; \
392 __va_copy (arg, argptr); \
394 (void) va_arg (arg, void *); \
395 va_arg (arg, type); \
399 /* XXX Possible optimization. */
400 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
401 ({ va_list arg = (va_list) argptr; \
402 arg = (va_list) ((char *) arg \
404 * __va_rounded_size (void *)); \
405 va_arg (arg, type); \
408 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
409 ({ unsigned int pos = argpos; \
410 va_list arg = (va_list) argptr; \
412 (void) va_arg (arg, void *); \
413 va_arg (arg, type); \
418 #ifndef COMPILE_WSCANF
419 if (!isascii ((unsigned char) *f
))
421 /* Non-ASCII, may be a multibyte. */
422 int len
= __mbrlen (f
, strlen (f
), &state
);
428 if (__glibc_unlikely (c
== EOF
))
430 else if (c
!= (unsigned char) *f
++)
432 ungetc_not_eof (c
, s
);
445 /* Remember to skip spaces. */
452 /* Read a character. */
455 /* Characters other than format specs must just match. */
456 if (__glibc_unlikely (c
== EOF
))
459 /* We saw white space char as the last character in the format
460 string. Now it's time to skip all leading white space. */
464 if (__glibc_unlikely (inchar () == EOF
))
469 if (__glibc_unlikely (c
!= fc
))
478 /* This is the start of the conversion string. */
481 /* Initialize state of modifiers. */
484 /* Prepare temporary buffer. */
485 char_buffer_rewind (&charbuf
);
487 /* Check for a positional parameter specification. */
488 if (ISDIGIT ((UCHAR_T
) *f
))
490 argpos
= (UCHAR_T
) *f
++ - L_('0');
491 while (ISDIGIT ((UCHAR_T
) *f
))
492 argpos
= argpos
* 10 + ((UCHAR_T
) *f
++ - L_('0'));
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 while (ISDIGIT ((UCHAR_T
) *f
))
530 width
+= (UCHAR_T
) *f
++ - L_('0');
536 /* Check for type modifiers. */
540 /* ints are short ints or chars. */
552 /* A double `l' is equivalent to an `L'. */
554 flags
|= LONGDBL
| LONG
;
557 /* ints are long ints. */
562 /* doubles are long doubles, and ints are long long ints. */
563 flags
|= LONGDBL
| LONG
;
566 /* The `a' is used as a flag only if followed by `s', `S' or
568 if (*f
!= L_('s') && *f
!= L_('S') && *f
!= L_('['))
573 /* In __isoc99_*scanf %as, %aS and %a[ extension is not
575 if (s
->_flags2
& _IO_FLAGS2_SCANF_STD
)
580 /* String conversions (%s, %[) take a `char **'
581 arg and fill it in with a malloc'd pointer. */
585 flags
|= POSIX_MALLOC
;
593 if (need_longlong
&& sizeof (size_t) > sizeof (unsigned long int))
595 else if (sizeof (size_t) > sizeof (unsigned int))
599 if (need_longlong
&& sizeof (uintmax_t) > sizeof (unsigned long int))
601 else if (sizeof (uintmax_t) > sizeof (unsigned int))
605 if (need_longlong
&& sizeof (ptrdiff_t) > sizeof (long int))
607 else if (sizeof (ptrdiff_t) > sizeof (int))
611 /* Not a recognized modifier. Backup. */
616 /* End of the format string? */
617 if (__glibc_unlikely (*f
== L_('\0')))
620 /* Find the conversion specifier. */
622 if (skip_space
|| (fc
!= L_('[') && fc
!= L_('c')
623 && fc
!= L_('C') && fc
!= L_('n')))
625 /* Eat whitespace. */
626 int save_errno
= errno
;
629 /* We add the additional test for EOF here since otherwise
630 inchar will restore the old errno value which might be
631 EINTR but does not indicate an interrupt since nothing
632 was read at this time. */
633 if (__builtin_expect ((c
== EOF
|| inchar () == EOF
)
634 && errno
== EINTR
, 0))
637 __set_errno (save_errno
);
644 case L_('%'): /* Must match a literal '%'. */
646 if (__glibc_unlikely (c
== EOF
))
648 if (__glibc_unlikely (c
!= fc
))
650 ungetc_not_eof (c
, s
);
655 case L_('n'): /* Answer number of assignments done. */
656 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
657 with the 'n' conversion specifier. */
658 if (!(flags
& SUPPRESS
))
660 /* Don't count the read-ahead. */
661 if (need_longlong
&& (flags
& LONGDBL
))
662 *ARG (long long int *) = read_in
;
663 else if (need_long
&& (flags
& LONG
))
664 *ARG (long int *) = read_in
;
665 else if (flags
& SHORT
)
666 *ARG (short int *) = read_in
;
667 else if (!(flags
& CHAR
))
668 *ARG (int *) = read_in
;
670 *ARG (char *) = read_in
;
672 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
673 /* We have a severe problem here. The ISO C standard
674 contradicts itself in explaining the effect of the %n
675 format in `scanf'. While in ISO C:1990 and the ISO C
676 Amendement 1:1995 the result is described as
678 Execution of a %n directive does not effect the
679 assignment count returned at the completion of
680 execution of the f(w)scanf function.
682 in ISO C Corrigendum 1:1994 the following was added:
685 Add the following fourth example:
688 int d1, d2, n1, n2, i;
689 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
690 the value 123 is assigned to d1 and the value3 to n1.
691 Because %n can never get an input failure the value
692 of 3 is also assigned to n2. The value of d2 is not
693 affected. The value 3 is assigned to i.
695 We go for now with the historically correct code from ISO C,
696 i.e., we don't count the %n assignments. When it ever
697 should proof to be wrong just remove the #ifdef above. */
703 case L_('c'): /* Match characters. */
704 if ((flags
& LONG
) == 0)
709 #define STRING_ARG(Str, Type, Width) \
710 do if (!(flags & SUPPRESS)) \
712 if (flags & MALLOC) \
714 /* The string is to be stored in a malloc'd buffer. */ \
715 /* For %mS using char ** is actually wrong, but \
716 shouldn't make a difference on any arch glibc \
717 supports and would unnecessarily complicate \
719 strptr = ARG (char **); \
720 if (strptr == NULL) \
722 /* Allocate an initial buffer. */ \
724 *strptr = (char *) malloc (strsize * sizeof (Type)); \
725 Str = (Type *) *strptr; \
727 add_ptr_to_free (strptr); \
728 else if (flags & POSIX_MALLOC) \
735 Str = ARG (Type *); \
739 #ifdef COMPILE_WSCANF
740 STRING_ARG (str
, char, 100);
742 STRING_ARG (str
, char, (width
> 1024 ? 1024 : width
));
746 if (__glibc_unlikely (c
== EOF
))
749 #ifdef COMPILE_WSCANF
750 /* We have to convert the wide character(s) into multibyte
751 characters and store the result. */
752 memset (&state
, '\0', sizeof (state
));
758 if (!(flags
& SUPPRESS
) && (flags
& POSIX_MALLOC
)
759 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
761 /* We have to enlarge the buffer if the `m' flag
763 size_t strleng
= str
- *strptr
;
766 newstr
= (char *) realloc (*strptr
, strsize
* 2);
769 /* Can't allocate that much. Last-ditch effort. */
770 newstr
= (char *) realloc (*strptr
,
771 strleng
+ MB_CUR_MAX
);
774 /* c can't have `a' flag, only `m'. */
781 str
= newstr
+ strleng
;
782 strsize
= strleng
+ MB_CUR_MAX
;
788 str
= newstr
+ strleng
;
793 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
794 if (__glibc_unlikely (n
== (size_t) -1))
795 /* No valid wide character. */
798 /* Increment the output pointer. Even if we don't
802 while (--width
> 0 && inchar () != EOF
);
804 if (!(flags
& SUPPRESS
))
809 && (char *) str
== *strptr
+ strsize
)
811 /* Enlarge the buffer. */
814 + (strsize
>= width
? width
- 1 : strsize
);
816 str
= (char *) realloc (*strptr
, newsize
);
819 /* Can't allocate that much. Last-ditch
821 str
= (char *) realloc (*strptr
, strsize
+ 1);
824 /* c can't have `a' flag, only `m'. */
830 *strptr
= (char *) str
;
837 *strptr
= (char *) str
;
844 while (--width
> 0 && inchar () != EOF
);
847 while (--width
> 0 && inchar () != EOF
);
850 if (!(flags
& SUPPRESS
))
852 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
854 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
869 STRING_ARG (wstr
, wchar_t, (width
> 1024 ? 1024 : width
));
872 if (__glibc_unlikely (c
== EOF
))
875 #ifdef COMPILE_WSCANF
876 /* Just store the incoming wide characters. */
877 if (!(flags
& SUPPRESS
))
882 && wstr
== (wchar_t *) *strptr
+ strsize
)
885 = strsize
+ (strsize
> width
? width
- 1 : strsize
);
886 /* Enlarge the buffer. */
887 wstr
= (wchar_t *) realloc (*strptr
,
888 newsize
* sizeof (wchar_t));
891 /* Can't allocate that much. Last-ditch effort. */
892 wstr
= (wchar_t *) realloc (*strptr
,
897 /* C or lc can't have `a' flag, only `m'
904 *strptr
= (char *) wstr
;
911 *strptr
= (char *) wstr
;
918 while (--width
> 0 && inchar () != EOF
);
921 while (--width
> 0 && inchar () != EOF
);
924 /* We have to convert the multibyte input sequence to wide
929 memset (&cstate
, '\0', sizeof (cstate
));
933 /* This is what we present the mbrtowc function first. */
936 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
937 && wstr
== (wchar_t *) *strptr
+ strsize
)
940 = strsize
+ (strsize
> width
? width
- 1 : strsize
);
941 /* Enlarge the buffer. */
942 wstr
= (wchar_t *) realloc (*strptr
,
943 newsize
* sizeof (wchar_t));
946 /* Can't allocate that much. Last-ditch effort. */
947 wstr
= (wchar_t *) realloc (*strptr
,
949 * sizeof (wchar_t)));
952 /* C or lc can't have `a' flag, only `m' flag. */
958 *strptr
= (char *) wstr
;
965 *strptr
= (char *) wstr
;
975 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
978 if (n
== (size_t) -2)
980 /* Possibly correct character, just not enough
982 if (__glibc_unlikely (inchar () == EOF
))
989 if (__glibc_unlikely (n
!= 1))
992 /* We have a match. */
996 /* Advance the result pointer. */
999 while (--width
> 0 && inchar () != EOF
);
1003 if (!(flags
& SUPPRESS
))
1005 if ((flags
& MALLOC
) && wstr
- (wchar_t *) *strptr
!= strsize
)
1007 wchar_t *cp
= (wchar_t *) realloc (*strptr
,
1009 - (wchar_t *) *strptr
)
1010 * sizeof (wchar_t)));
1012 *strptr
= (char *) cp
;
1021 case L_('s'): /* Read a string. */
1022 if (!(flags
& LONG
))
1024 STRING_ARG (str
, char, 100);
1027 if (__glibc_unlikely (c
== EOF
))
1030 #ifdef COMPILE_WSCANF
1031 memset (&state
, '\0', sizeof (state
));
1038 ungetc_not_eof (c
, s
);
1042 #ifdef COMPILE_WSCANF
1043 /* This is quite complicated. We have to convert the
1044 wide characters into multibyte characters and then
1049 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
1050 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
1052 /* We have to enlarge the buffer if the `a' or `m'
1054 size_t strleng
= str
- *strptr
;
1057 newstr
= (char *) realloc (*strptr
, strsize
* 2);
1060 /* Can't allocate that much. Last-ditch
1062 newstr
= (char *) realloc (*strptr
,
1063 strleng
+ MB_CUR_MAX
);
1066 if (flags
& POSIX_MALLOC
)
1071 /* We lose. Oh well. Terminate the
1072 string and stop converting,
1073 so at least we don't skip any input. */
1074 ((char *) (*strptr
))[strleng
] = '\0';
1082 str
= newstr
+ strleng
;
1083 strsize
= strleng
+ MB_CUR_MAX
;
1089 str
= newstr
+ strleng
;
1094 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
,
1096 if (__glibc_unlikely (n
== (size_t) -1))
1099 assert (n
<= MB_CUR_MAX
);
1104 if (!(flags
& SUPPRESS
))
1107 if ((flags
& MALLOC
)
1108 && (char *) str
== *strptr
+ strsize
)
1110 /* Enlarge the buffer. */
1111 str
= (char *) realloc (*strptr
, 2 * strsize
);
1114 /* Can't allocate that much. Last-ditch
1116 str
= (char *) realloc (*strptr
, strsize
+ 1);
1119 if (flags
& POSIX_MALLOC
)
1124 /* We lose. Oh well. Terminate the
1125 string and stop converting,
1126 so at least we don't skip any input. */
1127 ((char *) (*strptr
))[strsize
- 1] = '\0';
1134 *strptr
= (char *) str
;
1141 *strptr
= (char *) str
;
1149 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
1151 if (!(flags
& SUPPRESS
))
1153 #ifdef COMPILE_WSCANF
1154 /* We have to emit the code to get into the initial
1156 char buf
[MB_LEN_MAX
];
1157 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
1158 if (n
> 0 && (flags
& MALLOC
)
1159 && str
+ n
>= *strptr
+ strsize
)
1161 /* Enlarge the buffer. */
1162 size_t strleng
= str
- *strptr
;
1165 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
1168 if (flags
& POSIX_MALLOC
)
1173 /* We lose. Oh well. Terminate the string
1174 and stop converting, so at least we don't
1176 ((char *) (*strptr
))[strleng
] = '\0';
1184 str
= newstr
+ strleng
;
1185 strsize
= strleng
+ n
+ 1;
1189 str
= __mempcpy (str
, buf
, n
);
1193 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
1195 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
1209 #ifndef COMPILE_WSCANF
1213 /* Wide character string. */
1214 STRING_ARG (wstr
, wchar_t, 100);
1217 if (__builtin_expect (c
== EOF
, 0))
1220 #ifndef COMPILE_WSCANF
1221 memset (&cstate
, '\0', sizeof (cstate
));
1228 ungetc_not_eof (c
, s
);
1232 #ifdef COMPILE_WSCANF
1234 if (!(flags
& SUPPRESS
))
1237 if ((flags
& MALLOC
)
1238 && wstr
== (wchar_t *) *strptr
+ strsize
)
1240 /* Enlarge the buffer. */
1241 wstr
= (wchar_t *) realloc (*strptr
,
1243 * sizeof (wchar_t));
1246 /* Can't allocate that much. Last-ditch
1248 wstr
= (wchar_t *) realloc (*strptr
,
1250 * sizeof (wchar_t));
1253 if (flags
& POSIX_MALLOC
)
1258 /* We lose. Oh well. Terminate the string
1259 and stop converting, so at least we don't
1261 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1268 *strptr
= (char *) wstr
;
1275 *strptr
= (char *) wstr
;
1291 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
1294 if (n
== (size_t) -2)
1296 /* Possibly correct character, just not enough
1298 if (__glibc_unlikely (inchar () == EOF
))
1305 if (__glibc_unlikely (n
!= 1))
1308 /* We have a match. */
1313 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
1314 && wstr
== (wchar_t *) *strptr
+ strsize
)
1316 /* Enlarge the buffer. */
1317 wstr
= (wchar_t *) realloc (*strptr
,
1319 * sizeof (wchar_t)));
1322 /* Can't allocate that much. Last-ditch effort. */
1323 wstr
= (wchar_t *) realloc (*strptr
,
1325 * sizeof (wchar_t)));
1328 if (flags
& POSIX_MALLOC
)
1333 /* We lose. Oh well. Terminate the
1334 string and stop converting, so at
1335 least we don't skip any input. */
1336 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1343 *strptr
= (char *) wstr
;
1350 *strptr
= (char *) wstr
;
1358 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
1360 if (!(flags
& SUPPRESS
))
1364 if ((flags
& MALLOC
) && wstr
- (wchar_t *) *strptr
!= strsize
)
1366 wchar_t *cp
= (wchar_t *) realloc (*strptr
,
1368 - (wchar_t *) *strptr
)
1369 * sizeof(wchar_t)));
1371 *strptr
= (char *) cp
;
1380 case L_('x'): /* Hexadecimal integer. */
1381 case L_('X'): /* Ditto. */
1385 case L_('o'): /* Octal integer. */
1389 case L_('u'): /* Unsigned decimal integer. */
1393 case L_('d'): /* Signed decimal integer. */
1395 flags
|= NUMBER_SIGNED
;
1398 case L_('i'): /* Generic number. */
1400 flags
|= NUMBER_SIGNED
;
1404 if (__glibc_unlikely (c
== EOF
))
1407 /* Check for a sign. */
1408 if (c
== L_('-') || c
== L_('+'))
1410 char_buffer_add (&charbuf
, c
);
1416 /* Look for a leading indication of base. */
1417 if (width
!= 0 && c
== L_('0'))
1422 char_buffer_add (&charbuf
, c
);
1425 if (width
!= 0 && TOLOWER (c
) == L_('x'))
1443 if (base
== 10 && __builtin_expect ((flags
& I18N
) != 0, 0))
1448 #ifdef COMPILE_WSCANF
1449 const wchar_t *wcdigits
[10];
1450 const wchar_t *wcdigits_extended
[10];
1452 const char *mbdigits
[10];
1453 const char *mbdigits_extended
[10];
1455 /* "to_inpunct" is a map from ASCII digits to their
1456 equivalent in locale. This is defined for locales
1457 which use an extra digits set. */
1458 wctrans_t map
= __wctrans ("to_inpunct");
1462 #ifdef COMPILE_WSCANF
1463 to_level
= _NL_CURRENT_WORD (LC_CTYPE
,
1464 _NL_CTYPE_INDIGITS_WC_LEN
) - 1;
1466 to_level
= (uint32_t) curctype
->values
[_NL_ITEM_INDEX (_NL_CTYPE_INDIGITS_MB_LEN
)].word
- 1;
1469 /* Get the alternative digit forms if there are any. */
1470 if (__glibc_unlikely (map
!= NULL
))
1472 /* Adding new level for extra digits set in locale file. */
1475 for (n
= 0; n
< 10; ++n
)
1477 #ifdef COMPILE_WSCANF
1478 wcdigits
[n
] = (const wchar_t *)
1479 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1481 wchar_t *wc_extended
= (wchar_t *)
1482 alloca ((to_level
+ 2) * sizeof (wchar_t));
1483 __wmemcpy (wc_extended
, wcdigits
[n
], to_level
);
1484 wc_extended
[to_level
] = __towctrans (L
'0' + n
, map
);
1485 wc_extended
[to_level
+ 1] = '\0';
1486 wcdigits_extended
[n
] = wc_extended
;
1489 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1491 /* Get the equivalent wide char in map. */
1492 wint_t extra_wcdigit
= __towctrans (L
'0' + n
, map
);
1494 /* Convert it to multibyte representation. */
1496 memset (&state
, '\0', sizeof (state
));
1498 char extra_mbdigit
[MB_LEN_MAX
];
1500 = __wcrtomb (extra_mbdigit
, extra_wcdigit
, &state
);
1502 if (mblen
== (size_t) -1)
1504 /* Ignore this new level. */
1509 /* Calculate the length of mbdigits[n]. */
1510 const char *last_char
= mbdigits
[n
];
1511 for (level
= 0; level
< to_level
; ++level
)
1512 last_char
= strchr (last_char
, '\0') + 1;
1514 size_t mbdigits_len
= last_char
- mbdigits
[n
];
1516 /* Allocate memory for extended multibyte digit. */
1518 mb_extended
= (char *) alloca (mbdigits_len
+ mblen
+ 1);
1520 /* And get the mbdigits + extra_digit string. */
1521 *(char *) __mempcpy (__mempcpy (mb_extended
, mbdigits
[n
],
1523 extra_mbdigit
, mblen
) = '\0';
1524 mbdigits_extended
[n
] = mb_extended
;
1529 /* Read the number into workspace. */
1530 while (c
!= EOF
&& width
!= 0)
1532 /* In this round we get the pointer to the digit strings
1533 and also perform the first round of comparisons. */
1534 for (n
= 0; n
< 10; ++n
)
1536 /* Get the string for the digits with value N. */
1537 #ifdef COMPILE_WSCANF
1538 if (__glibc_unlikely (map
!= NULL
))
1539 wcdigits
[n
] = wcdigits_extended
[n
];
1541 wcdigits
[n
] = (const wchar_t *)
1542 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1543 wcdigits
[n
] += from_level
;
1545 if (c
== (wint_t) *wcdigits
[n
])
1547 to_level
= from_level
;
1551 /* Advance the pointer to the next string. */
1555 int avail
= width
> 0 ? width
: INT_MAX
;
1557 if (__glibc_unlikely (map
!= NULL
))
1558 mbdigits
[n
] = mbdigits_extended
[n
];
1561 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1563 for (level
= 0; level
< from_level
; level
++)
1564 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1567 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1569 if (*++cmpp
== '\0')
1573 if (avail
== 0 || inchar () == EOF
)
1583 to_level
= from_level
;
1587 /* We are pushing all read characters back. */
1588 if (cmpp
> mbdigits
[n
])
1591 while (--cmpp
> mbdigits
[n
])
1592 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1593 c
= (unsigned char) *cmpp
;
1596 /* Advance the pointer to the next string. */
1597 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1603 /* Have not yet found the digit. */
1604 for (level
= from_level
+ 1; level
<= to_level
; ++level
)
1606 /* Search all ten digits of this level. */
1607 for (n
= 0; n
< 10; ++n
)
1609 #ifdef COMPILE_WSCANF
1610 if (c
== (wint_t) *wcdigits
[n
])
1613 /* Advance the pointer to the next string. */
1617 int avail
= width
> 0 ? width
: INT_MAX
;
1620 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1622 if (*++cmpp
== '\0')
1626 if (avail
== 0 || inchar () == EOF
)
1639 /* We are pushing all read characters back. */
1640 if (cmpp
> mbdigits
[n
])
1643 while (--cmpp
> mbdigits
[n
])
1644 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1645 c
= (unsigned char) *cmpp
;
1648 /* Advance the pointer to the next string. */
1649 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1665 else if (flags
& GROUP
)
1667 /* Try matching against the thousands separator. */
1668 #ifdef COMPILE_WSCANF
1672 const char *cmpp
= thousands
;
1673 int avail
= width
> 0 ? width
: INT_MAX
;
1675 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1677 char_buffer_add (&charbuf
, c
);
1678 if (*++cmpp
== '\0')
1682 if (avail
== 0 || inchar () == EOF
)
1688 if (char_buffer_error (&charbuf
))
1690 __set_errno (ENOMEM
);
1697 /* We are pushing all read characters back. */
1698 if (cmpp
> thousands
)
1700 charbuf
.current
-= cmpp
- thousands
;
1702 while (--cmpp
> thousands
)
1703 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1704 c
= (unsigned char) *cmpp
;
1712 /* The last thousands character will be added back by
1713 the char_buffer_add below. */
1720 char_buffer_add (&charbuf
, c
);
1728 /* Read the number into workspace. */
1729 while (c
!= EOF
&& width
!= 0)
1736 else if (!ISDIGIT (c
) || (int) (c
- L_('0')) >= base
)
1738 if (base
== 10 && (flags
& GROUP
))
1740 /* Try matching against the thousands separator. */
1741 #ifdef COMPILE_WSCANF
1745 const char *cmpp
= thousands
;
1746 int avail
= width
> 0 ? width
: INT_MAX
;
1748 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1750 char_buffer_add (&charbuf
, c
);
1751 if (*++cmpp
== '\0')
1755 if (avail
== 0 || inchar () == EOF
)
1761 if (char_buffer_error (&charbuf
))
1763 __set_errno (ENOMEM
);
1770 /* We are pushing all read characters back. */
1771 if (cmpp
> thousands
)
1773 charbuf
.current
-= cmpp
- thousands
;
1775 while (--cmpp
> thousands
)
1776 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1777 c
= (unsigned char) *cmpp
;
1785 /* The last thousands character will be added back by
1786 the char_buffer_add below. */
1793 char_buffer_add (&charbuf
, c
);
1800 if (char_buffer_error (&charbuf
))
1802 __set_errno (ENOMEM
);
1807 if (char_buffer_size (&charbuf
) == 0
1808 || (char_buffer_size (&charbuf
) == 1
1809 && (char_buffer_start (&charbuf
)[0] == L_('+')
1810 || char_buffer_start (&charbuf
)[0] == L_('-'))))
1812 /* There was no number. If we are supposed to read a pointer
1813 we must recognize "(nil)" as well. */
1814 if (__builtin_expect (char_buffer_size (&charbuf
) == 0
1815 && (flags
& READ_POINTER
)
1816 && (width
< 0 || width
>= 5)
1818 && TOLOWER (inchar ()) == L_('n')
1819 && TOLOWER (inchar ()) == L_('i')
1820 && TOLOWER (inchar ()) == L_('l')
1821 && inchar () == L_(')'), 1))
1822 /* We must produce the value of a NULL pointer. A single
1823 '0' digit is enough. */
1824 char_buffer_add (&charbuf
, L_('0'));
1827 /* The last read character is not part of the number
1835 /* The just read character is not part of the number anymore. */
1838 /* Convert the number. */
1839 char_buffer_add (&charbuf
, L_('\0'));
1840 if (char_buffer_error (&charbuf
))
1842 __set_errno (ENOMEM
);
1846 if (need_longlong
&& (flags
& LONGDBL
))
1848 if (flags
& NUMBER_SIGNED
)
1849 num
.q
= __strtoll_internal
1850 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1852 num
.uq
= __strtoull_internal
1853 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1857 if (flags
& NUMBER_SIGNED
)
1858 num
.l
= __strtol_internal
1859 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1861 num
.ul
= __strtoul_internal
1862 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1864 if (__glibc_unlikely (char_buffer_start (&charbuf
) == tw
))
1867 if (!(flags
& SUPPRESS
))
1869 if (flags
& NUMBER_SIGNED
)
1871 if (need_longlong
&& (flags
& LONGDBL
))
1872 *ARG (LONGLONG
int *) = num
.q
;
1873 else if (need_long
&& (flags
& LONG
))
1874 *ARG (long int *) = num
.l
;
1875 else if (flags
& SHORT
)
1876 *ARG (short int *) = (short int) num
.l
;
1877 else if (!(flags
& CHAR
))
1878 *ARG (int *) = (int) num
.l
;
1880 *ARG (signed char *) = (signed char) num
.ul
;
1884 if (need_longlong
&& (flags
& LONGDBL
))
1885 *ARG (unsigned LONGLONG
int *) = num
.uq
;
1886 else if (need_long
&& (flags
& LONG
))
1887 *ARG (unsigned long int *) = num
.ul
;
1888 else if (flags
& SHORT
)
1889 *ARG (unsigned short int *)
1890 = (unsigned short int) num
.ul
;
1891 else if (!(flags
& CHAR
))
1892 *ARG (unsigned int *) = (unsigned int) num
.ul
;
1894 *ARG (unsigned char *) = (unsigned char) num
.ul
;
1900 case L_('e'): /* Floating-point numbers. */
1911 if (__glibc_unlikely (c
== EOF
))
1914 got_digit
= got_dot
= got_e
= 0;
1916 /* Check for a sign. */
1917 if (c
== L_('-') || c
== L_('+'))
1919 negative
= c
== L_('-');
1920 if (__glibc_unlikely (width
== 0 || inchar () == EOF
))
1921 /* EOF is only an input error before we read any chars. */
1929 /* Take care for the special arguments "nan" and "inf". */
1930 if (TOLOWER (c
) == L_('n'))
1933 char_buffer_add (&charbuf
, c
);
1934 if (__builtin_expect (width
== 0
1936 || TOLOWER (c
) != L_('a'), 0))
1940 char_buffer_add (&charbuf
, c
);
1941 if (__builtin_expect (width
== 0
1943 || TOLOWER (c
) != L_('n'), 0))
1947 char_buffer_add (&charbuf
, c
);
1951 else if (TOLOWER (c
) == L_('i'))
1953 /* Maybe "inf" or "infinity". */
1954 char_buffer_add (&charbuf
, c
);
1955 if (__builtin_expect (width
== 0
1957 || TOLOWER (c
) != L_('n'), 0))
1961 char_buffer_add (&charbuf
, c
);
1962 if (__builtin_expect (width
== 0
1964 || TOLOWER (c
) != L_('f'), 0))
1968 char_buffer_add (&charbuf
, c
);
1969 /* It is as least "inf". */
1970 if (width
!= 0 && inchar () != EOF
)
1972 if (TOLOWER (c
) == L_('i'))
1976 /* Now we have to read the rest as well. */
1977 char_buffer_add (&charbuf
, c
);
1978 if (__builtin_expect (width
== 0
1980 || TOLOWER (c
) != L_('n'), 0))
1984 char_buffer_add (&charbuf
, c
);
1985 if (__builtin_expect (width
== 0
1987 || TOLOWER (c
) != L_('i'), 0))
1991 char_buffer_add (&charbuf
, c
);
1992 if (__builtin_expect (width
== 0
1994 || TOLOWER (c
) != L_('t'), 0))
1998 char_buffer_add (&charbuf
, c
);
1999 if (__builtin_expect (width
== 0
2001 || TOLOWER (c
) != L_('y'), 0))
2005 char_buffer_add (&charbuf
, c
);
2015 if (width
!= 0 && c
== L_('0'))
2017 char_buffer_add (&charbuf
, c
);
2021 if (width
!= 0 && TOLOWER (c
) == L_('x'))
2023 /* It is a number in hexadecimal format. */
2024 char_buffer_add (&charbuf
, c
);
2026 flags
|= HEXA_FLOAT
;
2029 /* Grouping is not allowed. */
2041 if (char_buffer_error (&charbuf
))
2043 __set_errno (ENOMEM
);
2049 char_buffer_add (&charbuf
, c
);
2052 else if (!got_e
&& (flags
& HEXA_FLOAT
) && ISXDIGIT (c
))
2054 char_buffer_add (&charbuf
, c
);
2057 else if (got_e
&& charbuf
.current
[-1] == exp_char
2058 && (c
== L_('-') || c
== L_('+')))
2059 char_buffer_add (&charbuf
, c
);
2060 else if (got_digit
&& !got_e
2061 && (CHAR_T
) TOLOWER (c
) == exp_char
)
2063 char_buffer_add (&charbuf
, exp_char
);
2064 got_e
= got_dot
= 1;
2068 #ifdef COMPILE_WSCANF
2069 if (! got_dot
&& c
== decimal
)
2071 char_buffer_add (&charbuf
, c
);
2074 else if ((flags
& GROUP
) != 0 && ! got_dot
&& c
== thousands
)
2075 char_buffer_add (&charbuf
, c
);
2078 /* The last read character is not part of the number
2084 const char *cmpp
= decimal
;
2085 int avail
= width
> 0 ? width
: INT_MAX
;
2089 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
2090 if (*++cmpp
== '\0')
2094 if (avail
== 0 || inchar () == EOF
)
2102 /* Add all the characters. */
2103 for (cmpp
= decimal
; *cmpp
!= '\0'; ++cmpp
)
2104 char_buffer_add (&charbuf
, (unsigned char) *cmpp
);
2111 /* Figure out whether it is a thousands separator.
2112 There is one problem: we possibly read more than
2113 one character. We cannot push them back but since
2114 we know that parts of the `decimal' string matched,
2115 we can compare against it. */
2116 const char *cmp2p
= thousands
;
2118 if ((flags
& GROUP
) != 0 && ! got_dot
)
2120 while (cmp2p
- thousands
< cmpp
- decimal
2121 && *cmp2p
== decimal
[cmp2p
- thousands
])
2123 if (cmp2p
- thousands
== cmpp
- decimal
)
2125 while ((unsigned char) *cmp2p
== c
&& avail
>= 0)
2126 if (*++cmp2p
== '\0')
2130 if (avail
== 0 || inchar () == EOF
)
2137 if (cmp2p
!= NULL
&& *cmp2p
== '\0')
2139 /* Add all the characters. */
2140 for (cmpp
= thousands
; *cmpp
!= '\0'; ++cmpp
)
2141 char_buffer_add (&charbuf
, (unsigned char) *cmpp
);
2147 /* The last read character is not part of the number
2156 if (width
== 0 || inchar () == EOF
)
2163 if (char_buffer_error (&charbuf
))
2165 __set_errno (ENOMEM
);
2171 if (__builtin_expect ((flags
& I18N
) != 0, 0)
2172 /* Hexadecimal floats make no sense, fixing localized
2173 digits with ASCII letters. */
2174 && !(flags
& HEXA_FLOAT
)
2175 /* Minimum requirement. */
2176 && (char_buffer_size (&charbuf
) == 0 || got_dot
)
2177 && (map
= __wctrans ("to_inpunct")) != NULL
)
2179 /* Reget the first character. */
2182 /* Localized digits, decimal points, and thousands
2184 wint_t wcdigits
[12];
2186 /* First get decimal equivalent to check if we read it
2188 wcdigits
[11] = __towctrans (L
'.', map
);
2190 /* If we have not read any character or have just read
2191 locale decimal point which matches the decimal point
2192 for localized FP numbers, then we may have localized
2193 digits. Note, we test GOT_DOT above. */
2194 #ifdef COMPILE_WSCANF
2195 if (char_buffer_size (&charbuf
) == 0
2196 || (char_buffer_size (&charbuf
) == 1
2197 && wcdigits
[11] == decimal
))
2199 char mbdigits
[12][MB_LEN_MAX
+ 1];
2202 memset (&state
, '\0', sizeof (state
));
2204 bool match_so_far
= char_buffer_size (&charbuf
) == 0;
2205 size_t mblen
= __wcrtomb (mbdigits
[11], wcdigits
[11], &state
);
2206 if (mblen
!= (size_t) -1)
2208 mbdigits
[11][mblen
] = '\0';
2210 (char_buffer_size (&charbuf
) == strlen (decimal
)
2211 && strcmp (decimal
, mbdigits
[11]) == 0);
2215 size_t decimal_len
= strlen (decimal
);
2216 /* This should always be the case but the data comes
2218 if (decimal_len
<= MB_LEN_MAX
)
2220 match_so_far
|= char_buffer_size (&charbuf
) == decimal_len
;
2221 memcpy (mbdigits
[11], decimal
, decimal_len
+ 1);
2224 match_so_far
= false;
2230 bool have_locthousands
= (flags
& GROUP
) != 0;
2232 /* Now get the digits and the thousands-sep equivalents. */
2233 for (int n
= 0; n
< 11; ++n
)
2236 wcdigits
[n
] = __towctrans (L
'0' + n
, map
);
2239 wcdigits
[10] = __towctrans (L
',', map
);
2240 have_locthousands
&= wcdigits
[10] != L
'\0';
2243 #ifndef COMPILE_WSCANF
2244 memset (&state
, '\0', sizeof (state
));
2246 size_t mblen
= __wcrtomb (mbdigits
[n
], wcdigits
[n
],
2248 if (mblen
== (size_t) -1)
2252 if (have_locthousands
)
2254 size_t thousands_len
= strlen (thousands
);
2255 if (thousands_len
<= MB_LEN_MAX
)
2256 memcpy (mbdigits
[10], thousands
,
2259 have_locthousands
= false;
2263 /* Ignore checking against localized digits. */
2267 mbdigits
[n
][mblen
] = '\0';
2271 /* Start checking against localized digits, if
2272 conversion is done correctly. */
2275 if (char_buffer_error (&charbuf
))
2277 __set_errno (ENOMEM
);
2281 if (got_e
&& charbuf
.current
[-1] == exp_char
2282 && (c
== L_('-') || c
== L_('+')))
2283 char_buffer_add (&charbuf
, c
);
2284 else if (char_buffer_size (&charbuf
) > 0 && !got_e
2285 && (CHAR_T
) TOLOWER (c
) == exp_char
)
2287 char_buffer_add (&charbuf
, exp_char
);
2288 got_e
= got_dot
= 1;
2292 /* Check against localized digits, decimal point,
2293 and thousands separator. */
2295 for (n
= 0; n
< 12; ++n
)
2297 #ifdef COMPILE_WSCANF
2298 if (c
== wcdigits
[n
])
2301 char_buffer_add (&charbuf
, L_('0') + n
);
2302 else if (n
== 11 && !got_dot
)
2304 char_buffer_add (&charbuf
, decimal
);
2307 else if (n
== 10 && have_locthousands
2309 char_buffer_add (&charbuf
, thousands
);
2311 /* The last read character is not part
2312 of the number anymore. */
2318 const char *cmpp
= mbdigits
[n
];
2319 int avail
= width
> 0 ? width
: INT_MAX
;
2321 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
2322 if (*++cmpp
== '\0')
2326 if (avail
== 0 || inchar () == EOF
)
2336 char_buffer_add (&charbuf
, L_('0') + n
);
2337 else if (n
== 11 && !got_dot
)
2339 /* Add all the characters. */
2340 for (cmpp
= decimal
; *cmpp
!= '\0';
2342 char_buffer_add (&charbuf
,
2343 (unsigned char) *cmpp
);
2347 else if (n
== 10 && (flags
& GROUP
) != 0
2350 /* Add all the characters. */
2351 for (cmpp
= thousands
; *cmpp
!= '\0';
2353 char_buffer_add (&charbuf
,
2354 (unsigned char) *cmpp
);
2357 /* The last read character is not part
2358 of the number anymore. */
2364 /* We are pushing all read characters back. */
2365 if (cmpp
> mbdigits
[n
])
2368 while (--cmpp
> mbdigits
[n
])
2369 ungetc_not_eof ((unsigned char) *cmpp
, s
);
2370 c
= (unsigned char) *cmpp
;
2377 /* The last read character is not part
2378 of the number anymore. */
2384 if (width
== 0 || inchar () == EOF
)
2392 #ifndef COMPILE_WSCANF
2398 if (char_buffer_error (&charbuf
))
2400 __set_errno (ENOMEM
);
2405 /* Have we read any character? If we try to read a number
2406 in hexadecimal notation and we have read only the `0x'
2407 prefix this is an error. */
2408 if (__glibc_unlikely (char_buffer_size (&charbuf
) == 0
2409 || ((flags
& HEXA_FLOAT
)
2410 && char_buffer_size (&charbuf
) == 2)))
2414 /* Convert the number. */
2415 char_buffer_add (&charbuf
, L_('\0'));
2416 if (char_buffer_error (&charbuf
))
2418 __set_errno (ENOMEM
);
2422 if ((flags
& LONGDBL
) && !__ldbl_is_dbl
)
2424 long double d
= __strtold_internal
2425 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2426 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2427 *ARG (long double *) = negative
? -d
: d
;
2429 else if (flags
& (LONG
| LONGDBL
))
2431 double d
= __strtod_internal
2432 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2433 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2434 *ARG (double *) = negative
? -d
: d
;
2438 float d
= __strtof_internal
2439 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2440 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2441 *ARG (float *) = negative
? -d
: d
;
2444 if (__glibc_unlikely (tw
== char_buffer_start (&charbuf
)))
2447 if (!(flags
& SUPPRESS
))
2451 case L_('['): /* Character class. */
2453 STRING_ARG (wstr
, wchar_t, 100);
2455 STRING_ARG (str
, char, 100);
2466 /* There is no width given so there is also no limit on the
2467 number of characters we read. Therefore we set width to
2468 a very high value to make the algorithm easier. */
2471 #ifdef COMPILE_WSCANF
2472 /* Find the beginning and the end of the scanlist. We are not
2473 creating a lookup table since it would have to be too large.
2474 Instead we search each time through the string. This is not
2475 a constant lookup time but who uses this feature deserves to
2477 tw
= (wchar_t *) f
; /* Marks the beginning. */
2482 while ((fc
= *f
++) != L
'\0' && fc
!= L
']');
2484 if (__glibc_unlikely (fc
== L
'\0'))
2486 wchar_t *twend
= (wchar_t *) f
- 1;
2488 /* Fill WP with byte flags indexed by character.
2489 We will use this flag map for matching input characters. */
2490 if (!scratch_buffer_set_array_size
2491 (&charbuf
.scratch
, UCHAR_MAX
+ 1, 1))
2496 memset (charbuf
.scratch
.data
, '\0', UCHAR_MAX
+ 1);
2499 if (fc
== ']' || fc
== '-')
2501 /* If ] or - appears before any char in the set, it is not
2502 the terminator or separator, but the first char in the
2504 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2508 while ((fc
= *f
++) != '\0' && fc
!= ']')
2509 if (fc
== '-' && *f
!= '\0' && *f
!= ']'
2510 && (unsigned char) f
[-2] <= (unsigned char) *f
)
2512 /* Add all characters from the one before the '-'
2513 up to (but not including) the next format char. */
2514 for (fc
= (unsigned char) f
[-2]; fc
< (unsigned char) *f
; ++fc
)
2515 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2518 /* Add the character to the flag map. */
2519 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2521 if (__glibc_unlikely (fc
== '\0'))
2527 size_t now
= read_in
;
2528 #ifdef COMPILE_WSCANF
2529 if (__glibc_unlikely (inchar () == WEOF
))
2536 /* Test whether it's in the scanlist. */
2538 while (runp
< twend
)
2540 if (runp
[0] == L
'-' && runp
[1] != '\0'
2541 && runp
+ 1 != twend
2543 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2545 /* Match against all characters in between the
2546 first and last character of the sequence. */
2549 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2550 if ((wint_t) wc
== c
)
2553 if (wc
<= runp
[1] && !not_in
)
2555 if (wc
<= runp
[1] && not_in
)
2557 /* The current character is not in the
2567 if ((wint_t) *runp
== c
&& !not_in
)
2569 if ((wint_t) *runp
== c
&& not_in
)
2579 if (runp
== twend
&& !not_in
)
2585 if (!(flags
& SUPPRESS
))
2589 if ((flags
& MALLOC
)
2590 && wstr
== (wchar_t *) *strptr
+ strsize
)
2592 /* Enlarge the buffer. */
2593 wstr
= (wchar_t *) realloc (*strptr
,
2595 * sizeof (wchar_t));
2598 /* Can't allocate that much. Last-ditch
2601 realloc (*strptr
, (strsize
+ 1)
2602 * sizeof (wchar_t));
2605 if (flags
& POSIX_MALLOC
)
2610 /* We lose. Oh well. Terminate the string
2611 and stop converting, so at least we don't
2613 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2620 *strptr
= (char *) wstr
;
2627 *strptr
= (char *) wstr
;
2634 while (--width
> 0 && inchar () != WEOF
);
2637 char buf
[MB_LEN_MAX
];
2641 if (__glibc_unlikely (inchar () == EOF
))
2644 memset (&cstate
, '\0', sizeof (cstate
));
2648 if (((char *) charbuf
.scratch
.data
)[c
] == not_in
)
2650 ungetc_not_eof (c
, s
);
2655 if (!(flags
& SUPPRESS
))
2659 /* Convert it into a wide character. */
2661 n
= __mbrtowc (wstr
, buf
, 1, &cstate
);
2663 if (n
== (size_t) -2)
2665 /* Possibly correct character, just not enough
2668 assert (cnt
< MB_CUR_MAX
);
2674 if ((flags
& MALLOC
)
2675 && wstr
== (wchar_t *) *strptr
+ strsize
)
2677 /* Enlarge the buffer. */
2678 wstr
= (wchar_t *) realloc (*strptr
,
2680 * sizeof (wchar_t)));
2683 /* Can't allocate that much. Last-ditch
2686 realloc (*strptr
, ((strsize
+ 1)
2687 * sizeof (wchar_t)));
2690 if (flags
& POSIX_MALLOC
)
2695 /* We lose. Oh well. Terminate the
2696 string and stop converting,
2697 so at least we don't skip any input. */
2698 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2705 *strptr
= (char *) wstr
;
2712 *strptr
= (char *) wstr
;
2722 while (inchar () != EOF
);
2724 if (__glibc_unlikely (cnt
!= 0))
2725 /* We stopped in the middle of recognizing another
2726 character. That's a problem. */
2730 if (__glibc_unlikely (now
== read_in
))
2731 /* We haven't succesfully read any character. */
2734 if (!(flags
& SUPPRESS
))
2738 if ((flags
& MALLOC
)
2739 && wstr
- (wchar_t *) *strptr
!= strsize
)
2741 wchar_t *cp
= (wchar_t *)
2742 realloc (*strptr
, ((wstr
- (wchar_t *) *strptr
)
2743 * sizeof(wchar_t)));
2745 *strptr
= (char *) cp
;
2754 size_t now
= read_in
;
2756 if (__glibc_unlikely (inchar () == EOF
))
2759 #ifdef COMPILE_WSCANF
2761 memset (&state
, '\0', sizeof (state
));
2768 /* Test whether it's in the scanlist. */
2770 while (runp
< twend
)
2772 if (runp
[0] == L
'-' && runp
[1] != '\0'
2773 && runp
+ 1 != twend
2775 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2777 /* Match against all characters in between the
2778 first and last character of the sequence. */
2781 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2782 if ((wint_t) wc
== c
)
2785 if (wc
<= runp
[1] && !not_in
)
2787 if (wc
<= runp
[1] && not_in
)
2789 /* The current character is not in the
2799 if ((wint_t) *runp
== c
&& !not_in
)
2801 if ((wint_t) *runp
== c
&& not_in
)
2811 if (runp
== twend
&& !not_in
)
2817 if (!(flags
& SUPPRESS
))
2819 if ((flags
& MALLOC
)
2820 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
2822 /* Enlarge the buffer. */
2823 size_t strleng
= str
- *strptr
;
2826 newstr
= (char *) realloc (*strptr
, 2 * strsize
);
2829 /* Can't allocate that much. Last-ditch
2831 newstr
= (char *) realloc (*strptr
,
2832 strleng
+ MB_CUR_MAX
);
2835 if (flags
& POSIX_MALLOC
)
2840 /* We lose. Oh well. Terminate the string
2841 and stop converting, so at least we don't
2843 ((char *) (*strptr
))[strleng
] = '\0';
2851 str
= newstr
+ strleng
;
2852 strsize
= strleng
+ MB_CUR_MAX
;
2858 str
= newstr
+ strleng
;
2864 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
2865 if (__glibc_unlikely (n
== (size_t) -1))
2868 assert (n
<= MB_CUR_MAX
);
2871 while (--width
> 0 && inchar () != WEOF
);
2876 if (((char *) charbuf
.scratch
.data
)[c
] == not_in
)
2878 ungetc_not_eof (c
, s
);
2883 if (!(flags
& SUPPRESS
))
2886 if ((flags
& MALLOC
)
2887 && (char *) str
== *strptr
+ strsize
)
2889 /* Enlarge the buffer. */
2890 size_t newsize
= 2 * strsize
;
2893 str
= (char *) realloc (*strptr
, newsize
);
2896 /* Can't allocate that much. Last-ditch
2898 if (newsize
> strsize
+ 1)
2900 newsize
= strsize
+ 1;
2903 if (flags
& POSIX_MALLOC
)
2908 /* We lose. Oh well. Terminate the
2909 string and stop converting,
2910 so at least we don't skip any input. */
2911 ((char *) (*strptr
))[strsize
- 1] = '\0';
2918 *strptr
= (char *) str
;
2925 while (--width
> 0 && inchar () != EOF
);
2928 if (__glibc_unlikely (now
== read_in
))
2929 /* We haven't succesfully read any character. */
2932 if (!(flags
& SUPPRESS
))
2934 #ifdef COMPILE_WSCANF
2935 /* We have to emit the code to get into the initial
2937 char buf
[MB_LEN_MAX
];
2938 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
2939 if (n
> 0 && (flags
& MALLOC
)
2940 && str
+ n
>= *strptr
+ strsize
)
2942 /* Enlarge the buffer. */
2943 size_t strleng
= str
- *strptr
;
2946 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
2949 if (flags
& POSIX_MALLOC
)
2954 /* We lose. Oh well. Terminate the string
2955 and stop converting, so at least we don't
2957 ((char *) (*strptr
))[strleng
] = '\0';
2965 str
= newstr
+ strleng
;
2966 strsize
= strleng
+ n
+ 1;
2970 str
= __mempcpy (str
, buf
, n
);
2974 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
2976 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
2987 case L_('p'): /* Generic pointer. */
2989 /* A PTR must be the same size as a `long int'. */
2990 flags
&= ~(SHORT
|LONGDBL
);
2993 flags
|= READ_POINTER
;
2997 /* If this is an unknown format character punt. */
3002 /* The last thing we saw int the format string was a white space.
3003 Consume the last white spaces. */
3008 while (ISSPACE (c
));
3013 /* Unlock stream. */
3016 scratch_buffer_free (&charbuf
.scratch
);
3020 if (__glibc_unlikely (done
== EOF
))
3022 if (__glibc_unlikely (ptrs_to_free
!= NULL
))
3024 struct ptrs_to_free
*p
= ptrs_to_free
;
3027 for (size_t cnt
= 0; cnt
< p
->count
; ++cnt
)
3029 free (*p
->ptrs
[cnt
]);
3030 *p
->ptrs
[cnt
] = NULL
;
3037 else if (__glibc_unlikely (strptr
!= NULL
))
3045 #ifdef COMPILE_WSCANF
3047 __vfwscanf (FILE *s
, const wchar_t *format
, va_list argptr
)
3049 return _IO_vfwscanf (s
, format
, argptr
, NULL
);
3051 ldbl_weak_alias (__vfwscanf
, vfwscanf
)
3054 ___vfscanf (FILE *s
, const char *format
, va_list argptr
)
3056 return _IO_vfscanf_internal (s
, format
, argptr
, NULL
);
3058 ldbl_strong_alias (_IO_vfscanf_internal
, _IO_vfscanf
)
3059 ldbl_hidden_def (_IO_vfscanf_internal
, _IO_vfscanf
)
3060 ldbl_strong_alias (___vfscanf
, __vfscanf
)
3061 ldbl_hidden_def (___vfscanf
, __vfscanf
)
3062 ldbl_weak_alias (___vfscanf
, vfscanf
)