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-internal.h>
31 #include <libc-lock.h>
32 #include <locale/localeinfo.h>
33 #include <scratch_buffer.h>
36 # define HAVE_LONGLONG
37 # define LONGLONG long long
39 # define LONGLONG long
42 /* Determine whether we have to handle `long long' at all. */
43 #if LONG_MAX == LONG_LONG_MAX
44 # define need_longlong 0
46 # define need_longlong 1
49 /* Determine whether we have to handle `long'. */
50 #if INT_MAX == LONG_MAX
56 /* Those are flags in the conversion format. */
57 #define LONG 0x0001 /* l: long or double */
58 #define LONGDBL 0x0002 /* L: long long or long double */
59 #define SHORT 0x0004 /* h: short */
60 #define SUPPRESS 0x0008 /* *: suppress assignment */
61 #define POINTER 0x0010 /* weird %p pointer (`fake hex') */
62 #define NOSKIP 0x0020 /* do not skip blanks */
63 #define NUMBER_SIGNED 0x0040 /* signed integer */
64 #define GROUP 0x0080 /* ': group numbers */
65 #define GNU_MALLOC 0x0100 /* a: malloc strings */
66 #define CHAR 0x0200 /* hh: char */
67 #define I18N 0x0400 /* I: use locale's digits */
68 #define HEXA_FLOAT 0x0800 /* hexadecimal float */
69 #define READ_POINTER 0x1000 /* this is a pointer value */
70 #define POSIX_MALLOC 0x2000 /* m: malloc strings */
71 #define MALLOC (GNU_MALLOC | POSIX_MALLOC)
73 #include <locale/localeinfo.h>
78 #define va_list _IO_va_list
81 # define ungetc(c, s) ((void) (c == WEOF \
83 _IO_sputbackwc (s, c))))
84 # define ungetc_not_eof(c, s) ((void) (--read_in, \
85 _IO_sputbackwc (s, c)))
86 # define inchar() (c == WEOF ? ((errno = inchar_errno), WEOF) \
87 : ((c = _IO_getwc_unlocked (s)), \
90 : (size_t) (inchar_errno = errno)), c))
92 # define ISSPACE(Ch) iswspace (Ch)
93 # define ISDIGIT(Ch) iswdigit (Ch)
94 # define ISXDIGIT(Ch) iswxdigit (Ch)
95 # define TOLOWER(Ch) towlower (Ch)
96 # define ORIENT if (_IO_fwide (s, 1) != 1) return WEOF
97 # define __strtoll_internal __wcstoll_internal
98 # define __strtoull_internal __wcstoull_internal
99 # define __strtol_internal __wcstol_internal
100 # define __strtoul_internal __wcstoul_internal
101 # define __strtold_internal __wcstold_internal
102 # define __strtod_internal __wcstod_internal
103 # define __strtof_internal __wcstof_internal
105 # define L_(Str) L##Str
106 # define CHAR_T wchar_t
107 # define UCHAR_T unsigned int
108 # define WINT_T wint_t
112 # define ungetc(c, s) ((void) ((int) c == EOF \
114 _IO_sputbackc (s, (unsigned char) c))))
115 # define ungetc_not_eof(c, s) ((void) (--read_in, \
116 _IO_sputbackc (s, (unsigned char) c)))
117 # define inchar() (c == EOF ? ((errno = inchar_errno), EOF) \
118 : ((c = _IO_getc_unlocked (s)), \
121 : (size_t) (inchar_errno = errno)), c))
122 # define ISSPACE(Ch) __isspace_l (Ch, loc)
123 # define ISDIGIT(Ch) __isdigit_l (Ch, loc)
124 # define ISXDIGIT(Ch) __isxdigit_l (Ch, loc)
125 # define TOLOWER(Ch) __tolower_l ((unsigned char) (Ch), loc)
126 # define ORIENT if (_IO_vtable_offset (s) == 0 \
127 && _IO_fwide (s, -1) != -1) \
132 # define UCHAR_T unsigned char
136 #define encode_error() do { \
138 __set_errno (EILSEQ); \
141 #define conv_error() do { \
145 #define input_error() do { \
147 if (done == 0) done = EOF; \
150 #define add_ptr_to_free(ptr) \
153 if (ptrs_to_free == NULL \
154 || ptrs_to_free->count == (sizeof (ptrs_to_free->ptrs) \
155 / sizeof (ptrs_to_free->ptrs[0]))) \
157 struct ptrs_to_free *new_ptrs = alloca (sizeof (*ptrs_to_free)); \
158 new_ptrs->count = 0; \
159 new_ptrs->next = ptrs_to_free; \
160 ptrs_to_free = new_ptrs; \
162 ptrs_to_free->ptrs[ptrs_to_free->count++] = (ptr); \
165 #define ARGCHECK(s, format) \
168 /* Check file argument for consistence. */ \
169 CHECK_FILE (s, EOF); \
170 if (s->_flags & _IO_NO_READS) \
172 __set_errno (EBADF); \
175 else if (format == NULL) \
181 #define LOCK_STREAM(S) \
182 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, (S)); \
184 #define UNLOCK_STREAM(S) \
185 _IO_funlockfile (S); \
186 __libc_cleanup_region_end (0)
191 struct ptrs_to_free
*next
;
198 struct scratch_buffer scratch
;
201 /* Returns a pointer to the first CHAR_T object in the buffer. Only
202 valid if char_buffer_add (BUFFER, CH) has been called and
203 char_buffer_error (BUFFER) is false. */
204 static inline CHAR_T
*
205 char_buffer_start (const struct char_buffer
*buffer
)
207 return (CHAR_T
*) buffer
->scratch
.data
;
210 /* Returns the number of CHAR_T objects in the buffer. Only valid if
211 char_buffer_error (BUFFER) is false. */
213 char_buffer_size (const struct char_buffer
*buffer
)
215 return buffer
->current
- char_buffer_start (buffer
);
218 /* Reinitializes BUFFER->current and BUFFER->end to cover the entire
221 char_buffer_rewind (struct char_buffer
*buffer
)
223 buffer
->current
= char_buffer_start (buffer
);
224 buffer
->end
= buffer
->current
+ buffer
->scratch
.length
/ sizeof (CHAR_T
);
227 /* Returns true if a previous call to char_buffer_add (BUFFER, CH)
230 char_buffer_error (const struct char_buffer
*buffer
)
232 return __glibc_unlikely (buffer
->current
== NULL
);
235 /* Slow path for char_buffer_add. */
237 char_buffer_add_slow (struct char_buffer
*buffer
, CHAR_T ch
)
239 if (char_buffer_error (buffer
))
241 size_t offset
= buffer
->end
- (CHAR_T
*) buffer
->scratch
.data
;
242 if (!scratch_buffer_grow_preserve (&buffer
->scratch
))
244 buffer
->current
= NULL
;
248 char_buffer_rewind (buffer
);
249 buffer
->current
+= offset
;
250 *buffer
->current
++ = ch
;
253 /* Adds CH to BUFFER. This function does not report any errors, check
254 for them with char_buffer_error. */
256 char_buffer_add (struct char_buffer
*buffer
, CHAR_T ch
)
257 __attribute__ ((always_inline
));
259 char_buffer_add (struct char_buffer
*buffer
, CHAR_T ch
)
261 if (__glibc_unlikely (buffer
->current
== buffer
->end
))
262 char_buffer_add_slow (buffer
, ch
);
264 *buffer
->current
++ = ch
;
267 /* Read formatted input from S according to the format string
268 FORMAT, using the argument list in ARG.
269 Return the number of assignments made, or -1 for an input error. */
270 #ifdef COMPILE_WSCANF
272 _IO_vfwscanf (_IO_FILE
*s
, const wchar_t *format
, _IO_va_list argptr
,
276 _IO_vfscanf_internal (_IO_FILE
*s
, const char *format
, _IO_va_list argptr
,
281 const CHAR_T
*f
= format
;
282 UCHAR_T fc
; /* Current character of the format. */
283 WINT_T done
= 0; /* Assignments done. */
284 size_t read_in
= 0; /* Chars read in. */
285 WINT_T c
= 0; /* Last char read. */
286 int width
; /* Maximum field width. */
287 int flags
; /* Modifiers for current format element. */
289 #ifndef COMPILE_WSCANF
290 __locale_t loc
= _NL_CURRENT_LOCALE
;
291 struct __locale_data
*const curctype
= loc
->__locales
[LC_CTYPE
];
294 /* Errno of last failed inchar call. */
295 int inchar_errno
= 0;
296 /* Status for reading F-P nums. */
297 char got_digit
, got_dot
, got_e
, negative
;
298 /* If a [...] is a [^...]. */
300 #define exp_char not_in
301 /* Base for integral numbers. */
303 /* Decimal point character. */
304 #ifdef COMPILE_WSCANF
309 /* The thousands character of the current locale. */
310 #ifdef COMPILE_WSCANF
313 const char *thousands
;
315 struct ptrs_to_free
*ptrs_to_free
= NULL
;
316 /* State for the conversions. */
318 /* Integral holding variables. */
322 unsigned long long int uq
;
324 unsigned long int ul
;
326 /* Character-buffer pointer. */
328 wchar_t *wstr
= NULL
;
329 char **strptr
= NULL
;
331 /* We must not react on white spaces immediately because they can
332 possibly be matched even if in the input stream no character is
333 available anymore. */
336 CHAR_T
*tw
; /* Temporary pointer. */
337 struct char_buffer charbuf
;
338 scratch_buffer_init (&charbuf
.scratch
);
341 __va_copy (arg
, argptr
);
343 arg
= (va_list) argptr
;
350 ARGCHECK (s
, format
);
353 #ifndef COMPILE_WSCANF
354 struct __locale_data
*const curnumeric
= loc
->__locales
[LC_NUMERIC
];
357 /* Figure out the decimal point character. */
358 #ifdef COMPILE_WSCANF
359 decimal
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
361 decimal
= curnumeric
->values
[_NL_ITEM_INDEX (DECIMAL_POINT
)].string
;
363 /* Figure out the thousands separator character. */
364 #ifdef COMPILE_WSCANF
365 thousands
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_THOUSANDS_SEP_WC
);
367 thousands
= curnumeric
->values
[_NL_ITEM_INDEX (THOUSANDS_SEP
)].string
;
368 if (*thousands
== '\0')
373 /* Lock the stream. */
377 #ifndef COMPILE_WSCANF
378 /* From now on we use `state' to convert the format string. */
379 memset (&state
, '\0', sizeof (state
));
382 /* Run through the format string. */
386 /* Extract the next argument, which is of type TYPE.
387 For a %N$... spec, this is the Nth argument from the beginning;
388 otherwise it is the next argument after the state now in ARG. */
390 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
391 ({ unsigned int pos = argpos; \
393 __va_copy (arg, argptr); \
395 (void) va_arg (arg, void *); \
396 va_arg (arg, type); \
400 /* XXX Possible optimization. */
401 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
402 ({ va_list arg = (va_list) argptr; \
403 arg = (va_list) ((char *) arg \
405 * __va_rounded_size (void *)); \
406 va_arg (arg, type); \
409 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
410 ({ unsigned int pos = argpos; \
411 va_list arg = (va_list) argptr; \
413 (void) va_arg (arg, void *); \
414 va_arg (arg, type); \
419 #ifndef COMPILE_WSCANF
420 if (!isascii ((unsigned char) *f
))
422 /* Non-ASCII, may be a multibyte. */
423 int len
= __mbrlen (f
, strlen (f
), &state
);
429 if (__glibc_unlikely (c
== EOF
))
431 else if (c
!= (unsigned char) *f
++)
433 ungetc_not_eof (c
, s
);
446 /* Remember to skip spaces. */
453 /* Read a character. */
456 /* Characters other than format specs must just match. */
457 if (__glibc_unlikely (c
== EOF
))
460 /* We saw white space char as the last character in the format
461 string. Now it's time to skip all leading white space. */
465 if (__glibc_unlikely (inchar () == EOF
))
470 if (__glibc_unlikely (c
!= fc
))
479 /* This is the start of the conversion string. */
482 /* Initialize state of modifiers. */
485 /* Prepare temporary buffer. */
486 char_buffer_rewind (&charbuf
);
488 /* Check for a positional parameter specification. */
489 if (ISDIGIT ((UCHAR_T
) *f
))
491 argpos
= (UCHAR_T
) *f
++ - L_('0');
492 while (ISDIGIT ((UCHAR_T
) *f
))
493 argpos
= argpos
* 10 + ((UCHAR_T
) *f
++ - L_('0'));
498 /* Oops; that was actually the field width. */
505 /* Check for the assignment-suppressing, the number grouping flag,
506 and the signal to use the locale's digit representation. */
507 while (*f
== L_('*') || *f
== L_('\'') || *f
== L_('I'))
514 #ifdef COMPILE_WSCANF
515 if (thousands
!= L
'\0')
517 if (thousands
!= NULL
)
526 /* Find the maximum field width. */
528 while (ISDIGIT ((UCHAR_T
) *f
))
531 width
+= (UCHAR_T
) *f
++ - L_('0');
537 /* Check for type modifiers. */
541 /* ints are short ints or chars. */
553 /* A double `l' is equivalent to an `L'. */
555 flags
|= LONGDBL
| LONG
;
558 /* ints are long ints. */
563 /* doubles are long doubles, and ints are long long ints. */
564 flags
|= LONGDBL
| LONG
;
567 /* The `a' is used as a flag only if followed by `s', `S' or
569 if (*f
!= L_('s') && *f
!= L_('S') && *f
!= L_('['))
574 /* In __isoc99_*scanf %as, %aS and %a[ extension is not
576 if (s
->_flags2
& _IO_FLAGS2_SCANF_STD
)
581 /* String conversions (%s, %[) take a `char **'
582 arg and fill it in with a malloc'd pointer. */
586 flags
|= POSIX_MALLOC
;
594 if (need_longlong
&& sizeof (size_t) > sizeof (unsigned long int))
596 else if (sizeof (size_t) > sizeof (unsigned int))
600 if (need_longlong
&& sizeof (uintmax_t) > sizeof (unsigned long int))
602 else if (sizeof (uintmax_t) > sizeof (unsigned int))
606 if (need_longlong
&& sizeof (ptrdiff_t) > sizeof (long int))
608 else if (sizeof (ptrdiff_t) > sizeof (int))
612 /* Not a recognized modifier. Backup. */
617 /* End of the format string? */
618 if (__glibc_unlikely (*f
== L_('\0')))
621 /* Find the conversion specifier. */
623 if (skip_space
|| (fc
!= L_('[') && fc
!= L_('c')
624 && fc
!= L_('C') && fc
!= L_('n')))
626 /* Eat whitespace. */
627 int save_errno
= errno
;
630 /* We add the additional test for EOF here since otherwise
631 inchar will restore the old errno value which might be
632 EINTR but does not indicate an interrupt since nothing
633 was read at this time. */
634 if (__builtin_expect ((c
== EOF
|| inchar () == EOF
)
635 && errno
== EINTR
, 0))
638 __set_errno (save_errno
);
645 case L_('%'): /* Must match a literal '%'. */
647 if (__glibc_unlikely (c
== EOF
))
649 if (__glibc_unlikely (c
!= fc
))
651 ungetc_not_eof (c
, s
);
656 case L_('n'): /* Answer number of assignments done. */
657 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
658 with the 'n' conversion specifier. */
659 if (!(flags
& SUPPRESS
))
661 /* Don't count the read-ahead. */
662 if (need_longlong
&& (flags
& LONGDBL
))
663 *ARG (long long int *) = read_in
;
664 else if (need_long
&& (flags
& LONG
))
665 *ARG (long int *) = read_in
;
666 else if (flags
& SHORT
)
667 *ARG (short int *) = read_in
;
668 else if (!(flags
& CHAR
))
669 *ARG (int *) = read_in
;
671 *ARG (char *) = read_in
;
673 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
674 /* We have a severe problem here. The ISO C standard
675 contradicts itself in explaining the effect of the %n
676 format in `scanf'. While in ISO C:1990 and the ISO C
677 Amendement 1:1995 the result is described as
679 Execution of a %n directive does not effect the
680 assignment count returned at the completion of
681 execution of the f(w)scanf function.
683 in ISO C Corrigendum 1:1994 the following was added:
686 Add the following fourth example:
689 int d1, d2, n1, n2, i;
690 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
691 the value 123 is assigned to d1 and the value3 to n1.
692 Because %n can never get an input failure the value
693 of 3 is also assigned to n2. The value of d2 is not
694 affected. The value 3 is assigned to i.
696 We go for now with the historically correct code from ISO C,
697 i.e., we don't count the %n assignments. When it ever
698 should proof to be wrong just remove the #ifdef above. */
704 case L_('c'): /* Match characters. */
705 if ((flags
& LONG
) == 0)
710 #define STRING_ARG(Str, Type, Width) \
711 do if (!(flags & SUPPRESS)) \
713 if (flags & MALLOC) \
715 /* The string is to be stored in a malloc'd buffer. */ \
716 /* For %mS using char ** is actually wrong, but \
717 shouldn't make a difference on any arch glibc \
718 supports and would unnecessarily complicate \
720 strptr = ARG (char **); \
721 if (strptr == NULL) \
723 /* Allocate an initial buffer. */ \
725 *strptr = (char *) malloc (strsize * sizeof (Type)); \
726 Str = (Type *) *strptr; \
728 add_ptr_to_free (strptr); \
729 else if (flags & POSIX_MALLOC) \
736 Str = ARG (Type *); \
740 #ifdef COMPILE_WSCANF
741 STRING_ARG (str
, char, 100);
743 STRING_ARG (str
, char, (width
> 1024 ? 1024 : width
));
747 if (__glibc_unlikely (c
== EOF
))
750 #ifdef COMPILE_WSCANF
751 /* We have to convert the wide character(s) into multibyte
752 characters and store the result. */
753 memset (&state
, '\0', sizeof (state
));
759 if (!(flags
& SUPPRESS
) && (flags
& POSIX_MALLOC
)
760 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
762 /* We have to enlarge the buffer if the `m' flag
764 size_t strleng
= str
- *strptr
;
767 newstr
= (char *) realloc (*strptr
, strsize
* 2);
770 /* Can't allocate that much. Last-ditch effort. */
771 newstr
= (char *) realloc (*strptr
,
772 strleng
+ MB_CUR_MAX
);
775 /* c can't have `a' flag, only `m'. */
782 str
= newstr
+ strleng
;
783 strsize
= strleng
+ MB_CUR_MAX
;
789 str
= newstr
+ strleng
;
794 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
795 if (__glibc_unlikely (n
== (size_t) -1))
796 /* No valid wide character. */
799 /* Increment the output pointer. Even if we don't
803 while (--width
> 0 && inchar () != EOF
);
805 if (!(flags
& SUPPRESS
))
810 && (char *) str
== *strptr
+ strsize
)
812 /* Enlarge the buffer. */
815 + (strsize
>= width
? width
- 1 : strsize
);
817 str
= (char *) realloc (*strptr
, newsize
);
820 /* Can't allocate that much. Last-ditch
822 str
= (char *) realloc (*strptr
, strsize
+ 1);
825 /* c can't have `a' flag, only `m'. */
831 *strptr
= (char *) str
;
838 *strptr
= (char *) str
;
845 while (--width
> 0 && inchar () != EOF
);
848 while (--width
> 0 && inchar () != EOF
);
851 if (!(flags
& SUPPRESS
))
853 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
855 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
870 STRING_ARG (wstr
, wchar_t, (width
> 1024 ? 1024 : width
));
873 if (__glibc_unlikely (c
== EOF
))
876 #ifdef COMPILE_WSCANF
877 /* Just store the incoming wide characters. */
878 if (!(flags
& SUPPRESS
))
883 && wstr
== (wchar_t *) *strptr
+ strsize
)
886 = strsize
+ (strsize
> width
? width
- 1 : strsize
);
887 /* Enlarge the buffer. */
888 wstr
= (wchar_t *) realloc (*strptr
,
889 newsize
* sizeof (wchar_t));
892 /* Can't allocate that much. Last-ditch effort. */
893 wstr
= (wchar_t *) realloc (*strptr
,
898 /* C or lc can't have `a' flag, only `m'
905 *strptr
= (char *) wstr
;
912 *strptr
= (char *) wstr
;
919 while (--width
> 0 && inchar () != EOF
);
922 while (--width
> 0 && inchar () != EOF
);
925 /* We have to convert the multibyte input sequence to wide
930 memset (&cstate
, '\0', sizeof (cstate
));
934 /* This is what we present the mbrtowc function first. */
937 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
938 && wstr
== (wchar_t *) *strptr
+ strsize
)
941 = strsize
+ (strsize
> width
? width
- 1 : strsize
);
942 /* Enlarge the buffer. */
943 wstr
= (wchar_t *) realloc (*strptr
,
944 newsize
* sizeof (wchar_t));
947 /* Can't allocate that much. Last-ditch effort. */
948 wstr
= (wchar_t *) realloc (*strptr
,
950 * sizeof (wchar_t)));
953 /* C or lc can't have `a' flag, only `m' flag. */
959 *strptr
= (char *) wstr
;
966 *strptr
= (char *) wstr
;
976 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
979 if (n
== (size_t) -2)
981 /* Possibly correct character, just not enough
983 if (__glibc_unlikely (inchar () == EOF
))
990 if (__glibc_unlikely (n
!= 1))
993 /* We have a match. */
997 /* Advance the result pointer. */
1000 while (--width
> 0 && inchar () != EOF
);
1004 if (!(flags
& SUPPRESS
))
1006 if ((flags
& MALLOC
) && wstr
- (wchar_t *) *strptr
!= strsize
)
1008 wchar_t *cp
= (wchar_t *) realloc (*strptr
,
1010 - (wchar_t *) *strptr
)
1011 * sizeof (wchar_t)));
1013 *strptr
= (char *) cp
;
1022 case L_('s'): /* Read a string. */
1023 if (!(flags
& LONG
))
1025 STRING_ARG (str
, char, 100);
1028 if (__glibc_unlikely (c
== EOF
))
1031 #ifdef COMPILE_WSCANF
1032 memset (&state
, '\0', sizeof (state
));
1039 ungetc_not_eof (c
, s
);
1043 #ifdef COMPILE_WSCANF
1044 /* This is quite complicated. We have to convert the
1045 wide characters into multibyte characters and then
1050 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
1051 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
1053 /* We have to enlarge the buffer if the `a' or `m'
1055 size_t strleng
= str
- *strptr
;
1058 newstr
= (char *) realloc (*strptr
, strsize
* 2);
1061 /* Can't allocate that much. Last-ditch
1063 newstr
= (char *) realloc (*strptr
,
1064 strleng
+ MB_CUR_MAX
);
1067 if (flags
& POSIX_MALLOC
)
1072 /* We lose. Oh well. Terminate the
1073 string and stop converting,
1074 so at least we don't skip any input. */
1075 ((char *) (*strptr
))[strleng
] = '\0';
1083 str
= newstr
+ strleng
;
1084 strsize
= strleng
+ MB_CUR_MAX
;
1090 str
= newstr
+ strleng
;
1095 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
,
1097 if (__glibc_unlikely (n
== (size_t) -1))
1100 assert (n
<= MB_CUR_MAX
);
1105 if (!(flags
& SUPPRESS
))
1108 if ((flags
& MALLOC
)
1109 && (char *) str
== *strptr
+ strsize
)
1111 /* Enlarge the buffer. */
1112 str
= (char *) realloc (*strptr
, 2 * strsize
);
1115 /* Can't allocate that much. Last-ditch
1117 str
= (char *) realloc (*strptr
, strsize
+ 1);
1120 if (flags
& POSIX_MALLOC
)
1125 /* We lose. Oh well. Terminate the
1126 string and stop converting,
1127 so at least we don't skip any input. */
1128 ((char *) (*strptr
))[strsize
- 1] = '\0';
1135 *strptr
= (char *) str
;
1142 *strptr
= (char *) str
;
1150 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
1152 if (!(flags
& SUPPRESS
))
1154 #ifdef COMPILE_WSCANF
1155 /* We have to emit the code to get into the initial
1157 char buf
[MB_LEN_MAX
];
1158 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
1159 if (n
> 0 && (flags
& MALLOC
)
1160 && str
+ n
>= *strptr
+ strsize
)
1162 /* Enlarge the buffer. */
1163 size_t strleng
= str
- *strptr
;
1166 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
1169 if (flags
& POSIX_MALLOC
)
1174 /* We lose. Oh well. Terminate the string
1175 and stop converting, so at least we don't
1177 ((char *) (*strptr
))[strleng
] = '\0';
1185 str
= newstr
+ strleng
;
1186 strsize
= strleng
+ n
+ 1;
1190 str
= __mempcpy (str
, buf
, n
);
1194 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
1196 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
1210 #ifndef COMPILE_WSCANF
1214 /* Wide character string. */
1215 STRING_ARG (wstr
, wchar_t, 100);
1218 if (__builtin_expect (c
== EOF
, 0))
1221 #ifndef COMPILE_WSCANF
1222 memset (&cstate
, '\0', sizeof (cstate
));
1229 ungetc_not_eof (c
, s
);
1233 #ifdef COMPILE_WSCANF
1235 if (!(flags
& SUPPRESS
))
1238 if ((flags
& MALLOC
)
1239 && wstr
== (wchar_t *) *strptr
+ strsize
)
1241 /* Enlarge the buffer. */
1242 wstr
= (wchar_t *) realloc (*strptr
,
1244 * sizeof (wchar_t));
1247 /* Can't allocate that much. Last-ditch
1249 wstr
= (wchar_t *) realloc (*strptr
,
1251 * sizeof (wchar_t));
1254 if (flags
& POSIX_MALLOC
)
1259 /* We lose. Oh well. Terminate the string
1260 and stop converting, so at least we don't
1262 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1269 *strptr
= (char *) wstr
;
1276 *strptr
= (char *) wstr
;
1292 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
1295 if (n
== (size_t) -2)
1297 /* Possibly correct character, just not enough
1299 if (__glibc_unlikely (inchar () == EOF
))
1306 if (__glibc_unlikely (n
!= 1))
1309 /* We have a match. */
1314 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
1315 && wstr
== (wchar_t *) *strptr
+ strsize
)
1317 /* Enlarge the buffer. */
1318 wstr
= (wchar_t *) realloc (*strptr
,
1320 * sizeof (wchar_t)));
1323 /* Can't allocate that much. Last-ditch effort. */
1324 wstr
= (wchar_t *) realloc (*strptr
,
1326 * sizeof (wchar_t)));
1329 if (flags
& POSIX_MALLOC
)
1334 /* We lose. Oh well. Terminate the
1335 string and stop converting, so at
1336 least we don't skip any input. */
1337 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1344 *strptr
= (char *) wstr
;
1351 *strptr
= (char *) wstr
;
1359 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
1361 if (!(flags
& SUPPRESS
))
1365 if ((flags
& MALLOC
) && wstr
- (wchar_t *) *strptr
!= strsize
)
1367 wchar_t *cp
= (wchar_t *) realloc (*strptr
,
1369 - (wchar_t *) *strptr
)
1370 * sizeof(wchar_t)));
1372 *strptr
= (char *) cp
;
1381 case L_('x'): /* Hexadecimal integer. */
1382 case L_('X'): /* Ditto. */
1386 case L_('o'): /* Octal integer. */
1390 case L_('u'): /* Unsigned decimal integer. */
1394 case L_('d'): /* Signed decimal integer. */
1396 flags
|= NUMBER_SIGNED
;
1399 case L_('i'): /* Generic number. */
1401 flags
|= NUMBER_SIGNED
;
1405 if (__glibc_unlikely (c
== EOF
))
1408 /* Check for a sign. */
1409 if (c
== L_('-') || c
== L_('+'))
1411 char_buffer_add (&charbuf
, c
);
1417 /* Look for a leading indication of base. */
1418 if (width
!= 0 && c
== L_('0'))
1423 char_buffer_add (&charbuf
, c
);
1426 if (width
!= 0 && TOLOWER (c
) == L_('x'))
1444 if (base
== 10 && __builtin_expect ((flags
& I18N
) != 0, 0))
1449 #ifdef COMPILE_WSCANF
1450 const wchar_t *wcdigits
[10];
1451 const wchar_t *wcdigits_extended
[10];
1453 const char *mbdigits
[10];
1454 const char *mbdigits_extended
[10];
1456 /* "to_inpunct" is a map from ASCII digits to their
1457 equivalent in locale. This is defined for locales
1458 which use an extra digits set. */
1459 wctrans_t map
= __wctrans ("to_inpunct");
1463 #ifdef COMPILE_WSCANF
1464 to_level
= _NL_CURRENT_WORD (LC_CTYPE
,
1465 _NL_CTYPE_INDIGITS_WC_LEN
) - 1;
1467 to_level
= (uint32_t) curctype
->values
[_NL_ITEM_INDEX (_NL_CTYPE_INDIGITS_MB_LEN
)].word
- 1;
1470 /* Get the alternative digit forms if there are any. */
1471 if (__glibc_unlikely (map
!= NULL
))
1473 /* Adding new level for extra digits set in locale file. */
1476 for (n
= 0; n
< 10; ++n
)
1478 #ifdef COMPILE_WSCANF
1479 wcdigits
[n
] = (const wchar_t *)
1480 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1482 wchar_t *wc_extended
= (wchar_t *)
1483 alloca ((to_level
+ 2) * sizeof (wchar_t));
1484 __wmemcpy (wc_extended
, wcdigits
[n
], to_level
);
1485 wc_extended
[to_level
] = __towctrans (L
'0' + n
, map
);
1486 wc_extended
[to_level
+ 1] = '\0';
1487 wcdigits_extended
[n
] = wc_extended
;
1490 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1492 /* Get the equivalent wide char in map. */
1493 wint_t extra_wcdigit
= __towctrans (L
'0' + n
, map
);
1495 /* Convert it to multibyte representation. */
1497 memset (&state
, '\0', sizeof (state
));
1499 char extra_mbdigit
[MB_LEN_MAX
];
1501 = __wcrtomb (extra_mbdigit
, extra_wcdigit
, &state
);
1503 if (mblen
== (size_t) -1)
1505 /* Ignore this new level. */
1510 /* Calculate the length of mbdigits[n]. */
1511 const char *last_char
= mbdigits
[n
];
1512 for (level
= 0; level
< to_level
; ++level
)
1513 last_char
= strchr (last_char
, '\0') + 1;
1515 size_t mbdigits_len
= last_char
- mbdigits
[n
];
1517 /* Allocate memory for extended multibyte digit. */
1519 mb_extended
= (char *) alloca (mbdigits_len
+ mblen
+ 1);
1521 /* And get the mbdigits + extra_digit string. */
1522 *(char *) __mempcpy (__mempcpy (mb_extended
, mbdigits
[n
],
1524 extra_mbdigit
, mblen
) = '\0';
1525 mbdigits_extended
[n
] = mb_extended
;
1530 /* Read the number into workspace. */
1531 while (c
!= EOF
&& width
!= 0)
1533 /* In this round we get the pointer to the digit strings
1534 and also perform the first round of comparisons. */
1535 for (n
= 0; n
< 10; ++n
)
1537 /* Get the string for the digits with value N. */
1538 #ifdef COMPILE_WSCANF
1540 /* wcdigits_extended[] is fully set in the loop
1541 above, but the test for "map != NULL" is done
1542 inside the loop here and outside the loop there. */
1543 DIAG_PUSH_NEEDS_COMMENT
;
1544 DIAG_IGNORE_NEEDS_COMMENT (4.7, "-Wmaybe-uninitialized");
1546 if (__glibc_unlikely (map
!= NULL
))
1547 wcdigits
[n
] = wcdigits_extended
[n
];
1549 wcdigits
[n
] = (const wchar_t *)
1550 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1551 wcdigits
[n
] += from_level
;
1553 DIAG_POP_NEEDS_COMMENT
;
1555 if (c
== (wint_t) *wcdigits
[n
])
1557 to_level
= from_level
;
1561 /* Advance the pointer to the next string. */
1565 int avail
= width
> 0 ? width
: INT_MAX
;
1567 if (__glibc_unlikely (map
!= NULL
))
1568 mbdigits
[n
] = mbdigits_extended
[n
];
1571 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1573 for (level
= 0; level
< from_level
; level
++)
1574 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1577 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1579 if (*++cmpp
== '\0')
1583 if (avail
== 0 || inchar () == EOF
)
1593 to_level
= from_level
;
1597 /* We are pushing all read characters back. */
1598 if (cmpp
> mbdigits
[n
])
1601 while (--cmpp
> mbdigits
[n
])
1602 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1603 c
= (unsigned char) *cmpp
;
1606 /* Advance the pointer to the next string. */
1607 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1613 /* Have not yet found the digit. */
1614 for (level
= from_level
+ 1; level
<= to_level
; ++level
)
1616 /* Search all ten digits of this level. */
1617 for (n
= 0; n
< 10; ++n
)
1619 #ifdef COMPILE_WSCANF
1620 if (c
== (wint_t) *wcdigits
[n
])
1623 /* Advance the pointer to the next string. */
1627 int avail
= width
> 0 ? width
: INT_MAX
;
1630 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1632 if (*++cmpp
== '\0')
1636 if (avail
== 0 || inchar () == EOF
)
1649 /* We are pushing all read characters back. */
1650 if (cmpp
> mbdigits
[n
])
1653 while (--cmpp
> mbdigits
[n
])
1654 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1655 c
= (unsigned char) *cmpp
;
1658 /* Advance the pointer to the next string. */
1659 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1675 else if (flags
& GROUP
)
1677 /* Try matching against the thousands separator. */
1678 #ifdef COMPILE_WSCANF
1682 const char *cmpp
= thousands
;
1683 int avail
= width
> 0 ? width
: INT_MAX
;
1685 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1687 char_buffer_add (&charbuf
, c
);
1688 if (*++cmpp
== '\0')
1692 if (avail
== 0 || inchar () == EOF
)
1698 if (char_buffer_error (&charbuf
))
1700 __set_errno (ENOMEM
);
1707 /* We are pushing all read characters back. */
1708 if (cmpp
> thousands
)
1710 charbuf
.current
-= cmpp
- thousands
;
1712 while (--cmpp
> thousands
)
1713 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1714 c
= (unsigned char) *cmpp
;
1722 /* The last thousands character will be added back by
1723 the char_buffer_add below. */
1730 char_buffer_add (&charbuf
, c
);
1738 /* Read the number into workspace. */
1739 while (c
!= EOF
&& width
!= 0)
1746 else if (!ISDIGIT (c
) || (int) (c
- L_('0')) >= base
)
1748 if (base
== 10 && (flags
& GROUP
))
1750 /* Try matching against the thousands separator. */
1751 #ifdef COMPILE_WSCANF
1755 const char *cmpp
= thousands
;
1756 int avail
= width
> 0 ? width
: INT_MAX
;
1758 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1760 char_buffer_add (&charbuf
, c
);
1761 if (*++cmpp
== '\0')
1765 if (avail
== 0 || inchar () == EOF
)
1771 if (char_buffer_error (&charbuf
))
1773 __set_errno (ENOMEM
);
1780 /* We are pushing all read characters back. */
1781 if (cmpp
> thousands
)
1783 charbuf
.current
-= cmpp
- thousands
;
1785 while (--cmpp
> thousands
)
1786 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1787 c
= (unsigned char) *cmpp
;
1795 /* The last thousands character will be added back by
1796 the char_buffer_add below. */
1803 char_buffer_add (&charbuf
, c
);
1810 if (char_buffer_error (&charbuf
))
1812 __set_errno (ENOMEM
);
1817 if (char_buffer_size (&charbuf
) == 0
1818 || (char_buffer_size (&charbuf
) == 1
1819 && (char_buffer_start (&charbuf
)[0] == L_('+')
1820 || char_buffer_start (&charbuf
)[0] == L_('-'))))
1822 /* There was no number. If we are supposed to read a pointer
1823 we must recognize "(nil)" as well. */
1824 if (__builtin_expect (char_buffer_size (&charbuf
) == 0
1825 && (flags
& READ_POINTER
)
1826 && (width
< 0 || width
>= 5)
1828 && TOLOWER (inchar ()) == L_('n')
1829 && TOLOWER (inchar ()) == L_('i')
1830 && TOLOWER (inchar ()) == L_('l')
1831 && inchar () == L_(')'), 1))
1832 /* We must produce the value of a NULL pointer. A single
1833 '0' digit is enough. */
1834 char_buffer_add (&charbuf
, L_('0'));
1837 /* The last read character is not part of the number
1845 /* The just read character is not part of the number anymore. */
1848 /* Convert the number. */
1849 char_buffer_add (&charbuf
, L_('\0'));
1850 if (char_buffer_error (&charbuf
))
1852 __set_errno (ENOMEM
);
1856 if (need_longlong
&& (flags
& LONGDBL
))
1858 if (flags
& NUMBER_SIGNED
)
1859 num
.q
= __strtoll_internal
1860 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1862 num
.uq
= __strtoull_internal
1863 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1867 if (flags
& NUMBER_SIGNED
)
1868 num
.l
= __strtol_internal
1869 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1871 num
.ul
= __strtoul_internal
1872 (char_buffer_start (&charbuf
), &tw
, base
, flags
& GROUP
);
1874 if (__glibc_unlikely (char_buffer_start (&charbuf
) == tw
))
1877 if (!(flags
& SUPPRESS
))
1879 if (flags
& NUMBER_SIGNED
)
1881 if (need_longlong
&& (flags
& LONGDBL
))
1882 *ARG (LONGLONG
int *) = num
.q
;
1883 else if (need_long
&& (flags
& LONG
))
1884 *ARG (long int *) = num
.l
;
1885 else if (flags
& SHORT
)
1886 *ARG (short int *) = (short int) num
.l
;
1887 else if (!(flags
& CHAR
))
1888 *ARG (int *) = (int) num
.l
;
1890 *ARG (signed char *) = (signed char) num
.ul
;
1894 if (need_longlong
&& (flags
& LONGDBL
))
1895 *ARG (unsigned LONGLONG
int *) = num
.uq
;
1896 else if (need_long
&& (flags
& LONG
))
1897 *ARG (unsigned long int *) = num
.ul
;
1898 else if (flags
& SHORT
)
1899 *ARG (unsigned short int *)
1900 = (unsigned short int) num
.ul
;
1901 else if (!(flags
& CHAR
))
1902 *ARG (unsigned int *) = (unsigned int) num
.ul
;
1904 *ARG (unsigned char *) = (unsigned char) num
.ul
;
1910 case L_('e'): /* Floating-point numbers. */
1921 if (__glibc_unlikely (c
== EOF
))
1924 got_digit
= got_dot
= got_e
= 0;
1926 /* Check for a sign. */
1927 if (c
== L_('-') || c
== L_('+'))
1929 negative
= c
== L_('-');
1930 if (__glibc_unlikely (width
== 0 || inchar () == EOF
))
1931 /* EOF is only an input error before we read any chars. */
1939 /* Take care for the special arguments "nan" and "inf". */
1940 if (TOLOWER (c
) == L_('n'))
1943 char_buffer_add (&charbuf
, c
);
1944 if (__builtin_expect (width
== 0
1946 || TOLOWER (c
) != L_('a'), 0))
1950 char_buffer_add (&charbuf
, c
);
1951 if (__builtin_expect (width
== 0
1953 || TOLOWER (c
) != L_('n'), 0))
1957 char_buffer_add (&charbuf
, c
);
1961 else if (TOLOWER (c
) == L_('i'))
1963 /* Maybe "inf" or "infinity". */
1964 char_buffer_add (&charbuf
, c
);
1965 if (__builtin_expect (width
== 0
1967 || TOLOWER (c
) != L_('n'), 0))
1971 char_buffer_add (&charbuf
, c
);
1972 if (__builtin_expect (width
== 0
1974 || TOLOWER (c
) != L_('f'), 0))
1978 char_buffer_add (&charbuf
, c
);
1979 /* It is as least "inf". */
1980 if (width
!= 0 && inchar () != EOF
)
1982 if (TOLOWER (c
) == L_('i'))
1986 /* Now we have to read the rest as well. */
1987 char_buffer_add (&charbuf
, c
);
1988 if (__builtin_expect (width
== 0
1990 || TOLOWER (c
) != L_('n'), 0))
1994 char_buffer_add (&charbuf
, c
);
1995 if (__builtin_expect (width
== 0
1997 || TOLOWER (c
) != L_('i'), 0))
2001 char_buffer_add (&charbuf
, c
);
2002 if (__builtin_expect (width
== 0
2004 || TOLOWER (c
) != L_('t'), 0))
2008 char_buffer_add (&charbuf
, c
);
2009 if (__builtin_expect (width
== 0
2011 || TOLOWER (c
) != L_('y'), 0))
2015 char_buffer_add (&charbuf
, c
);
2025 if (width
!= 0 && c
== L_('0'))
2027 char_buffer_add (&charbuf
, c
);
2031 if (width
!= 0 && TOLOWER (c
) == L_('x'))
2033 /* It is a number in hexadecimal format. */
2034 char_buffer_add (&charbuf
, c
);
2036 flags
|= HEXA_FLOAT
;
2039 /* Grouping is not allowed. */
2051 if (char_buffer_error (&charbuf
))
2053 __set_errno (ENOMEM
);
2059 char_buffer_add (&charbuf
, c
);
2062 else if (!got_e
&& (flags
& HEXA_FLOAT
) && ISXDIGIT (c
))
2064 char_buffer_add (&charbuf
, c
);
2067 else if (got_e
&& charbuf
.current
[-1] == exp_char
2068 && (c
== L_('-') || c
== L_('+')))
2069 char_buffer_add (&charbuf
, c
);
2070 else if (got_digit
&& !got_e
2071 && (CHAR_T
) TOLOWER (c
) == exp_char
)
2073 char_buffer_add (&charbuf
, exp_char
);
2074 got_e
= got_dot
= 1;
2078 #ifdef COMPILE_WSCANF
2079 if (! got_dot
&& c
== decimal
)
2081 char_buffer_add (&charbuf
, c
);
2084 else if ((flags
& GROUP
) != 0 && ! got_dot
&& c
== thousands
)
2085 char_buffer_add (&charbuf
, c
);
2088 /* The last read character is not part of the number
2094 const char *cmpp
= decimal
;
2095 int avail
= width
> 0 ? width
: INT_MAX
;
2099 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
2100 if (*++cmpp
== '\0')
2104 if (avail
== 0 || inchar () == EOF
)
2112 /* Add all the characters. */
2113 for (cmpp
= decimal
; *cmpp
!= '\0'; ++cmpp
)
2114 char_buffer_add (&charbuf
, (unsigned char) *cmpp
);
2121 /* Figure out whether it is a thousands separator.
2122 There is one problem: we possibly read more than
2123 one character. We cannot push them back but since
2124 we know that parts of the `decimal' string matched,
2125 we can compare against it. */
2126 const char *cmp2p
= thousands
;
2128 if ((flags
& GROUP
) != 0 && ! got_dot
)
2130 while (cmp2p
- thousands
< cmpp
- decimal
2131 && *cmp2p
== decimal
[cmp2p
- thousands
])
2133 if (cmp2p
- thousands
== cmpp
- decimal
)
2135 while ((unsigned char) *cmp2p
== c
&& avail
>= 0)
2136 if (*++cmp2p
== '\0')
2140 if (avail
== 0 || inchar () == EOF
)
2147 if (cmp2p
!= NULL
&& *cmp2p
== '\0')
2149 /* Add all the characters. */
2150 for (cmpp
= thousands
; *cmpp
!= '\0'; ++cmpp
)
2151 char_buffer_add (&charbuf
, (unsigned char) *cmpp
);
2157 /* The last read character is not part of the number
2166 if (width
== 0 || inchar () == EOF
)
2173 if (char_buffer_error (&charbuf
))
2175 __set_errno (ENOMEM
);
2181 if (__builtin_expect ((flags
& I18N
) != 0, 0)
2182 /* Hexadecimal floats make no sense, fixing localized
2183 digits with ASCII letters. */
2184 && !(flags
& HEXA_FLOAT
)
2185 /* Minimum requirement. */
2186 && (char_buffer_size (&charbuf
) == 0 || got_dot
)
2187 && (map
= __wctrans ("to_inpunct")) != NULL
)
2189 /* Reget the first character. */
2192 /* Localized digits, decimal points, and thousands
2194 wint_t wcdigits
[12];
2196 /* First get decimal equivalent to check if we read it
2198 wcdigits
[11] = __towctrans (L
'.', map
);
2200 /* If we have not read any character or have just read
2201 locale decimal point which matches the decimal point
2202 for localized FP numbers, then we may have localized
2203 digits. Note, we test GOT_DOT above. */
2204 #ifdef COMPILE_WSCANF
2205 if (char_buffer_size (&charbuf
) == 0
2206 || (char_buffer_size (&charbuf
) == 1
2207 && wcdigits
[11] == decimal
))
2209 char mbdigits
[12][MB_LEN_MAX
+ 1];
2212 memset (&state
, '\0', sizeof (state
));
2214 bool match_so_far
= char_buffer_size (&charbuf
) == 0;
2215 size_t mblen
= __wcrtomb (mbdigits
[11], wcdigits
[11], &state
);
2216 if (mblen
!= (size_t) -1)
2218 mbdigits
[11][mblen
] = '\0';
2220 (char_buffer_size (&charbuf
) == strlen (decimal
)
2221 && strcmp (decimal
, mbdigits
[11]) == 0);
2225 size_t decimal_len
= strlen (decimal
);
2226 /* This should always be the case but the data comes
2228 if (decimal_len
<= MB_LEN_MAX
)
2230 match_so_far
|= char_buffer_size (&charbuf
) == decimal_len
;
2231 memcpy (mbdigits
[11], decimal
, decimal_len
+ 1);
2234 match_so_far
= false;
2240 bool have_locthousands
= (flags
& GROUP
) != 0;
2242 /* Now get the digits and the thousands-sep equivalents. */
2243 for (int n
= 0; n
< 11; ++n
)
2246 wcdigits
[n
] = __towctrans (L
'0' + n
, map
);
2249 wcdigits
[10] = __towctrans (L
',', map
);
2250 have_locthousands
&= wcdigits
[10] != L
'\0';
2253 #ifndef COMPILE_WSCANF
2254 memset (&state
, '\0', sizeof (state
));
2256 size_t mblen
= __wcrtomb (mbdigits
[n
], wcdigits
[n
],
2258 if (mblen
== (size_t) -1)
2262 if (have_locthousands
)
2264 size_t thousands_len
= strlen (thousands
);
2265 if (thousands_len
<= MB_LEN_MAX
)
2266 memcpy (mbdigits
[10], thousands
,
2269 have_locthousands
= false;
2273 /* Ignore checking against localized digits. */
2277 mbdigits
[n
][mblen
] = '\0';
2281 /* Start checking against localized digits, if
2282 conversion is done correctly. */
2285 if (char_buffer_error (&charbuf
))
2287 __set_errno (ENOMEM
);
2291 if (got_e
&& charbuf
.current
[-1] == exp_char
2292 && (c
== L_('-') || c
== L_('+')))
2293 char_buffer_add (&charbuf
, c
);
2294 else if (char_buffer_size (&charbuf
) > 0 && !got_e
2295 && (CHAR_T
) TOLOWER (c
) == exp_char
)
2297 char_buffer_add (&charbuf
, exp_char
);
2298 got_e
= got_dot
= 1;
2302 /* Check against localized digits, decimal point,
2303 and thousands separator. */
2305 for (n
= 0; n
< 12; ++n
)
2307 #ifdef COMPILE_WSCANF
2308 if (c
== wcdigits
[n
])
2311 char_buffer_add (&charbuf
, L_('0') + n
);
2312 else if (n
== 11 && !got_dot
)
2314 char_buffer_add (&charbuf
, decimal
);
2317 else if (n
== 10 && have_locthousands
2319 char_buffer_add (&charbuf
, thousands
);
2321 /* The last read character is not part
2322 of the number anymore. */
2328 const char *cmpp
= mbdigits
[n
];
2329 int avail
= width
> 0 ? width
: INT_MAX
;
2331 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
2332 if (*++cmpp
== '\0')
2336 if (avail
== 0 || inchar () == EOF
)
2346 char_buffer_add (&charbuf
, L_('0') + n
);
2347 else if (n
== 11 && !got_dot
)
2349 /* Add all the characters. */
2350 for (cmpp
= decimal
; *cmpp
!= '\0';
2352 char_buffer_add (&charbuf
,
2353 (unsigned char) *cmpp
);
2357 else if (n
== 10 && (flags
& GROUP
) != 0
2360 /* Add all the characters. */
2361 for (cmpp
= thousands
; *cmpp
!= '\0';
2363 char_buffer_add (&charbuf
,
2364 (unsigned char) *cmpp
);
2367 /* The last read character is not part
2368 of the number anymore. */
2374 /* We are pushing all read characters back. */
2375 if (cmpp
> mbdigits
[n
])
2378 while (--cmpp
> mbdigits
[n
])
2379 ungetc_not_eof ((unsigned char) *cmpp
, s
);
2380 c
= (unsigned char) *cmpp
;
2387 /* The last read character is not part
2388 of the number anymore. */
2394 if (width
== 0 || inchar () == EOF
)
2402 #ifndef COMPILE_WSCANF
2408 if (char_buffer_error (&charbuf
))
2410 __set_errno (ENOMEM
);
2415 /* Have we read any character? If we try to read a number
2416 in hexadecimal notation and we have read only the `0x'
2417 prefix this is an error. */
2418 if (__glibc_unlikely (char_buffer_size (&charbuf
) == 0
2419 || ((flags
& HEXA_FLOAT
)
2420 && char_buffer_size (&charbuf
) == 2)))
2424 /* Convert the number. */
2425 char_buffer_add (&charbuf
, L_('\0'));
2426 if (char_buffer_error (&charbuf
))
2428 __set_errno (ENOMEM
);
2432 if ((flags
& LONGDBL
) && !__ldbl_is_dbl
)
2434 long double d
= __strtold_internal
2435 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2436 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2437 *ARG (long double *) = negative
? -d
: d
;
2439 else if (flags
& (LONG
| LONGDBL
))
2441 double d
= __strtod_internal
2442 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2443 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2444 *ARG (double *) = negative
? -d
: d
;
2448 float d
= __strtof_internal
2449 (char_buffer_start (&charbuf
), &tw
, flags
& GROUP
);
2450 if (!(flags
& SUPPRESS
) && tw
!= char_buffer_start (&charbuf
))
2451 *ARG (float *) = negative
? -d
: d
;
2454 if (__glibc_unlikely (tw
== char_buffer_start (&charbuf
)))
2457 if (!(flags
& SUPPRESS
))
2461 case L_('['): /* Character class. */
2463 STRING_ARG (wstr
, wchar_t, 100);
2465 STRING_ARG (str
, char, 100);
2476 /* There is no width given so there is also no limit on the
2477 number of characters we read. Therefore we set width to
2478 a very high value to make the algorithm easier. */
2481 #ifdef COMPILE_WSCANF
2482 /* Find the beginning and the end of the scanlist. We are not
2483 creating a lookup table since it would have to be too large.
2484 Instead we search each time through the string. This is not
2485 a constant lookup time but who uses this feature deserves to
2487 tw
= (wchar_t *) f
; /* Marks the beginning. */
2492 while ((fc
= *f
++) != L
'\0' && fc
!= L
']');
2494 if (__glibc_unlikely (fc
== L
'\0'))
2496 wchar_t *twend
= (wchar_t *) f
- 1;
2498 /* Fill WP with byte flags indexed by character.
2499 We will use this flag map for matching input characters. */
2500 if (!scratch_buffer_set_array_size
2501 (&charbuf
.scratch
, UCHAR_MAX
+ 1, 1))
2506 memset (charbuf
.scratch
.data
, '\0', UCHAR_MAX
+ 1);
2509 if (fc
== ']' || fc
== '-')
2511 /* If ] or - appears before any char in the set, it is not
2512 the terminator or separator, but the first char in the
2514 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2518 while ((fc
= *f
++) != '\0' && fc
!= ']')
2519 if (fc
== '-' && *f
!= '\0' && *f
!= ']'
2520 && (unsigned char) f
[-2] <= (unsigned char) *f
)
2522 /* Add all characters from the one before the '-'
2523 up to (but not including) the next format char. */
2524 for (fc
= (unsigned char) f
[-2]; fc
< (unsigned char) *f
; ++fc
)
2525 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2528 /* Add the character to the flag map. */
2529 ((char *)charbuf
.scratch
.data
)[fc
] = 1;
2531 if (__glibc_unlikely (fc
== '\0'))
2537 size_t now
= read_in
;
2538 #ifdef COMPILE_WSCANF
2539 if (__glibc_unlikely (inchar () == WEOF
))
2546 /* Test whether it's in the scanlist. */
2548 while (runp
< twend
)
2550 if (runp
[0] == L
'-' && runp
[1] != '\0'
2551 && runp
+ 1 != twend
2553 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2555 /* Match against all characters in between the
2556 first and last character of the sequence. */
2559 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2560 if ((wint_t) wc
== c
)
2563 if (wc
<= runp
[1] && !not_in
)
2565 if (wc
<= runp
[1] && not_in
)
2567 /* The current character is not in the
2577 if ((wint_t) *runp
== c
&& !not_in
)
2579 if ((wint_t) *runp
== c
&& not_in
)
2589 if (runp
== twend
&& !not_in
)
2595 if (!(flags
& SUPPRESS
))
2599 if ((flags
& MALLOC
)
2600 && wstr
== (wchar_t *) *strptr
+ strsize
)
2602 /* Enlarge the buffer. */
2603 wstr
= (wchar_t *) realloc (*strptr
,
2605 * sizeof (wchar_t));
2608 /* Can't allocate that much. Last-ditch
2611 realloc (*strptr
, (strsize
+ 1)
2612 * sizeof (wchar_t));
2615 if (flags
& POSIX_MALLOC
)
2620 /* We lose. Oh well. Terminate the string
2621 and stop converting, so at least we don't
2623 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2630 *strptr
= (char *) wstr
;
2637 *strptr
= (char *) wstr
;
2644 while (--width
> 0 && inchar () != WEOF
);
2647 char buf
[MB_LEN_MAX
];
2651 if (__glibc_unlikely (inchar () == EOF
))
2654 memset (&cstate
, '\0', sizeof (cstate
));
2658 if (((char *) charbuf
.scratch
.data
)[c
] == not_in
)
2660 ungetc_not_eof (c
, s
);
2665 if (!(flags
& SUPPRESS
))
2669 /* Convert it into a wide character. */
2671 n
= __mbrtowc (wstr
, buf
, 1, &cstate
);
2673 if (n
== (size_t) -2)
2675 /* Possibly correct character, just not enough
2678 assert (cnt
< MB_CUR_MAX
);
2684 if ((flags
& MALLOC
)
2685 && wstr
== (wchar_t *) *strptr
+ strsize
)
2687 /* Enlarge the buffer. */
2688 wstr
= (wchar_t *) realloc (*strptr
,
2690 * sizeof (wchar_t)));
2693 /* Can't allocate that much. Last-ditch
2696 realloc (*strptr
, ((strsize
+ 1)
2697 * sizeof (wchar_t)));
2700 if (flags
& POSIX_MALLOC
)
2705 /* We lose. Oh well. Terminate the
2706 string and stop converting,
2707 so at least we don't skip any input. */
2708 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2715 *strptr
= (char *) wstr
;
2722 *strptr
= (char *) wstr
;
2732 while (inchar () != EOF
);
2734 if (__glibc_unlikely (cnt
!= 0))
2735 /* We stopped in the middle of recognizing another
2736 character. That's a problem. */
2740 if (__glibc_unlikely (now
== read_in
))
2741 /* We haven't succesfully read any character. */
2744 if (!(flags
& SUPPRESS
))
2748 if ((flags
& MALLOC
)
2749 && wstr
- (wchar_t *) *strptr
!= strsize
)
2751 wchar_t *cp
= (wchar_t *)
2752 realloc (*strptr
, ((wstr
- (wchar_t *) *strptr
)
2753 * sizeof(wchar_t)));
2755 *strptr
= (char *) cp
;
2764 size_t now
= read_in
;
2766 if (__glibc_unlikely (inchar () == EOF
))
2769 #ifdef COMPILE_WSCANF
2771 memset (&state
, '\0', sizeof (state
));
2778 /* Test whether it's in the scanlist. */
2780 while (runp
< twend
)
2782 if (runp
[0] == L
'-' && runp
[1] != '\0'
2783 && runp
+ 1 != twend
2785 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2787 /* Match against all characters in between the
2788 first and last character of the sequence. */
2791 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2792 if ((wint_t) wc
== c
)
2795 if (wc
<= runp
[1] && !not_in
)
2797 if (wc
<= runp
[1] && not_in
)
2799 /* The current character is not in the
2809 if ((wint_t) *runp
== c
&& !not_in
)
2811 if ((wint_t) *runp
== c
&& not_in
)
2821 if (runp
== twend
&& !not_in
)
2827 if (!(flags
& SUPPRESS
))
2829 if ((flags
& MALLOC
)
2830 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
2832 /* Enlarge the buffer. */
2833 size_t strleng
= str
- *strptr
;
2836 newstr
= (char *) realloc (*strptr
, 2 * strsize
);
2839 /* Can't allocate that much. Last-ditch
2841 newstr
= (char *) realloc (*strptr
,
2842 strleng
+ MB_CUR_MAX
);
2845 if (flags
& POSIX_MALLOC
)
2850 /* We lose. Oh well. Terminate the string
2851 and stop converting, so at least we don't
2853 ((char *) (*strptr
))[strleng
] = '\0';
2861 str
= newstr
+ strleng
;
2862 strsize
= strleng
+ MB_CUR_MAX
;
2868 str
= newstr
+ strleng
;
2874 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
2875 if (__glibc_unlikely (n
== (size_t) -1))
2878 assert (n
<= MB_CUR_MAX
);
2881 while (--width
> 0 && inchar () != WEOF
);
2886 if (((char *) charbuf
.scratch
.data
)[c
] == not_in
)
2888 ungetc_not_eof (c
, s
);
2893 if (!(flags
& SUPPRESS
))
2896 if ((flags
& MALLOC
)
2897 && (char *) str
== *strptr
+ strsize
)
2899 /* Enlarge the buffer. */
2900 size_t newsize
= 2 * strsize
;
2903 str
= (char *) realloc (*strptr
, newsize
);
2906 /* Can't allocate that much. Last-ditch
2908 if (newsize
> strsize
+ 1)
2910 newsize
= strsize
+ 1;
2913 if (flags
& POSIX_MALLOC
)
2918 /* We lose. Oh well. Terminate the
2919 string and stop converting,
2920 so at least we don't skip any input. */
2921 ((char *) (*strptr
))[strsize
- 1] = '\0';
2928 *strptr
= (char *) str
;
2935 while (--width
> 0 && inchar () != EOF
);
2938 if (__glibc_unlikely (now
== read_in
))
2939 /* We haven't succesfully read any character. */
2942 if (!(flags
& SUPPRESS
))
2944 #ifdef COMPILE_WSCANF
2945 /* We have to emit the code to get into the initial
2947 char buf
[MB_LEN_MAX
];
2948 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
2949 if (n
> 0 && (flags
& MALLOC
)
2950 && str
+ n
>= *strptr
+ strsize
)
2952 /* Enlarge the buffer. */
2953 size_t strleng
= str
- *strptr
;
2956 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
2959 if (flags
& POSIX_MALLOC
)
2964 /* We lose. Oh well. Terminate the string
2965 and stop converting, so at least we don't
2967 ((char *) (*strptr
))[strleng
] = '\0';
2975 str
= newstr
+ strleng
;
2976 strsize
= strleng
+ n
+ 1;
2980 str
= __mempcpy (str
, buf
, n
);
2984 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
2986 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
2997 case L_('p'): /* Generic pointer. */
2999 /* A PTR must be the same size as a `long int'. */
3000 flags
&= ~(SHORT
|LONGDBL
);
3003 flags
|= READ_POINTER
;
3007 /* If this is an unknown format character punt. */
3012 /* The last thing we saw int the format string was a white space.
3013 Consume the last white spaces. */
3018 while (ISSPACE (c
));
3023 /* Unlock stream. */
3026 scratch_buffer_free (&charbuf
.scratch
);
3030 if (__glibc_unlikely (done
== EOF
))
3032 if (__glibc_unlikely (ptrs_to_free
!= NULL
))
3034 struct ptrs_to_free
*p
= ptrs_to_free
;
3037 for (size_t cnt
= 0; cnt
< p
->count
; ++cnt
)
3039 free (*p
->ptrs
[cnt
]);
3040 *p
->ptrs
[cnt
] = NULL
;
3047 else if (__glibc_unlikely (strptr
!= NULL
))
3055 #ifdef COMPILE_WSCANF
3057 __vfwscanf (FILE *s
, const wchar_t *format
, va_list argptr
)
3059 return _IO_vfwscanf (s
, format
, argptr
, NULL
);
3061 ldbl_weak_alias (__vfwscanf
, vfwscanf
)
3064 ___vfscanf (FILE *s
, const char *format
, va_list argptr
)
3066 return _IO_vfscanf_internal (s
, format
, argptr
, NULL
);
3068 ldbl_strong_alias (_IO_vfscanf_internal
, _IO_vfscanf
)
3069 ldbl_hidden_def (_IO_vfscanf_internal
, _IO_vfscanf
)
3070 ldbl_strong_alias (___vfscanf
, __vfscanf
)
3071 ldbl_hidden_def (___vfscanf
, __vfscanf
)
3072 ldbl_weak_alias (___vfscanf
, vfscanf
)