1 /* Copyright (C) 1991-2014 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 <bits/libc-lock.h>
31 #include <locale/localeinfo.h>
34 # define HAVE_LONGLONG
35 # define LONGLONG long long
37 # define LONGLONG long
40 /* Determine whether we have to handle `long long' at all. */
41 #if LONG_MAX == LONG_LONG_MAX
42 # define need_longlong 0
44 # define need_longlong 1
47 /* Determine whether we have to handle `long'. */
48 #if INT_MAX == LONG_MAX
54 /* Those are flags in the conversion format. */
55 #define LONG 0x0001 /* l: long or double */
56 #define LONGDBL 0x0002 /* L: long long or long double */
57 #define SHORT 0x0004 /* h: short */
58 #define SUPPRESS 0x0008 /* *: suppress assignment */
59 #define POINTER 0x0010 /* weird %p pointer (`fake hex') */
60 #define NOSKIP 0x0020 /* do not skip blanks */
61 #define NUMBER_SIGNED 0x0040 /* signed integer */
62 #define GROUP 0x0080 /* ': group numbers */
63 #define GNU_MALLOC 0x0100 /* a: malloc strings */
64 #define CHAR 0x0200 /* hh: char */
65 #define I18N 0x0400 /* I: use locale's digits */
66 #define HEXA_FLOAT 0x0800 /* hexadecimal float */
67 #define READ_POINTER 0x1000 /* this is a pointer value */
68 #define POSIX_MALLOC 0x2000 /* m: malloc strings */
69 #define MALLOC (GNU_MALLOC | POSIX_MALLOC)
71 #include <locale/localeinfo.h>
76 #define va_list _IO_va_list
79 # define ungetc(c, s) ((void) (c == WEOF \
81 _IO_sputbackwc (s, c))))
82 # define ungetc_not_eof(c, s) ((void) (--read_in, \
83 _IO_sputbackwc (s, c)))
84 # define inchar() (c == WEOF ? ((errno = inchar_errno), WEOF) \
85 : ((c = _IO_getwc_unlocked (stream)), \
88 : (size_t) (inchar_errno = errno)), c))
90 # define MEMCPY(d, s, n) __wmemcpy (d, s, n)
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 (stream, 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 (stream)), \
120 : (size_t) (inchar_errno = errno)), c))
121 # define MEMCPY(d, s, n) memcpy (d, s, n)
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 (stream) == 0 \
127 && _IO_fwide (stream, -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
;
196 # define __va_copy(dst, src) (dst) = (va_list) (src)
199 /* Read formatted input from S according to the format string
200 FORMAT, using the argument list in ARG.
201 Return the number of assignments made, or -1 for an input error. */
202 #ifdef COMPILE_WSCANF
204 _IO_vfwscanf (_IO_FILE
*stream
, const wchar_t *format
, _IO_va_list argptr
,
208 _IO_vfscanf_internal (_IO_FILE
*stream
, const char *format
, _IO_va_list argptr
,
213 const CHAR_T
*f
= format
;
214 UCHAR_T fc
; /* Current character of the format. */
215 WINT_T done
= 0; /* Assignments done. */
216 size_t read_in
= 0; /* Chars read in. */
217 WINT_T c
= 0; /* Last char read. */
218 int width
; /* Maximum field width. */
219 int flags
; /* Modifiers for current format element. */
221 #ifndef COMPILE_WSCANF
222 __locale_t loc
= _NL_CURRENT_LOCALE
;
223 struct __locale_data
*const curctype
= loc
->__locales
[LC_CTYPE
];
226 /* Errno of last failed inchar call. */
227 int inchar_errno
= 0;
228 /* Status for reading F-P nums. */
229 char got_digit
, got_dot
, got_e
, negative
;
230 /* If a [...] is a [^...]. */
232 #define exp_char not_in
233 /* Base for integral numbers. */
235 /* Decimal point character. */
236 #ifdef COMPILE_WSCANF
241 /* The thousands character of the current locale. */
242 #ifdef COMPILE_WSCANF
245 const char *thousands
;
247 struct ptrs_to_free
*ptrs_to_free
= NULL
;
248 /* State for the conversions. */
250 /* Integral holding variables. */
254 unsigned long long int uq
;
256 unsigned long int ul
;
258 /* Character-buffer pointer. */
260 wchar_t *wstr
= NULL
;
261 char **strptr
= NULL
;
263 /* We must not react on white spaces immediately because they can
264 possibly be matched even if in the input stream no character is
265 available anymore. */
268 CHAR_T
*tw
; /* Temporary pointer. */
269 CHAR_T
*wp
= NULL
; /* Workspace. */
270 size_t wpmax
= 0; /* Maximal size of workspace. */
271 size_t wpsize
; /* Currently used bytes in workspace. */
272 bool use_malloc
= false;
276 if (__glibc_unlikely (wpsize == wpmax)) \
279 size_t newsize = (UCHAR_MAX + 1 > 2 * wpmax \
280 ? UCHAR_MAX + 1 : 2 * wpmax); \
281 if (use_malloc || !__libc_use_alloca (newsize)) \
283 wp = realloc (use_malloc ? wp : NULL, newsize); \
292 MEMCPY (wp, old, wpsize); \
298 size_t s = wpmax * sizeof (CHAR_T); \
299 wp = (CHAR_T *) extend_alloca (wp, s, \
300 newsize * sizeof (CHAR_T)); \
301 wpmax = s / sizeof (CHAR_T); \
303 MEMCPY (wp, old, wpsize); \
306 wp[wpsize++] = (Ch); \
310 __va_copy (arg
, argptr
);
316 ARGCHECK (stream
, format
);
319 #ifndef COMPILE_WSCANF
320 struct __locale_data
*const curnumeric
= loc
->__locales
[LC_NUMERIC
];
323 /* Figure out the decimal point character. */
324 #ifdef COMPILE_WSCANF
325 decimal
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
327 decimal
= curnumeric
->values
[_NL_ITEM_INDEX (DECIMAL_POINT
)].string
;
329 /* Figure out the thousands separator character. */
330 #ifdef COMPILE_WSCANF
331 thousands
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_THOUSANDS_SEP_WC
);
333 thousands
= curnumeric
->values
[_NL_ITEM_INDEX (THOUSANDS_SEP
)].string
;
334 if (*thousands
== '\0')
339 /* Lock the stream. */
340 LOCK_STREAM (stream
);
343 #ifndef COMPILE_WSCANF
344 /* From now on we use `state' to convert the format string. */
345 memset (&state
, '\0', sizeof (state
));
348 /* Run through the format string. */
352 /* Extract the next argument, which is of type TYPE.
353 For a %N$... spec, this is the Nth argument from the beginning;
354 otherwise it is the next argument after the state now in ARG. */
355 #define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
356 ({ unsigned int pos = argpos; \
358 __va_copy (argcopy, argptr); \
360 (void) va_arg (argcopy, void *); \
361 va_arg (argcopy, type); \
364 #ifndef COMPILE_WSCANF
365 if (!isascii ((unsigned char) *f
))
367 /* Non-ASCII, may be a multibyte. */
368 int len
= __mbrlen (f
, strlen (f
), &state
);
374 if (__glibc_unlikely (c
== EOF
))
376 else if (c
!= (unsigned char) *f
++)
378 ungetc_not_eof (c
, stream
);
391 /* Remember to skip spaces. */
398 /* Read a character. */
401 /* Characters other than format specs must just match. */
402 if (__glibc_unlikely (c
== EOF
))
405 /* We saw white space char as the last character in the format
406 string. Now it's time to skip all leading white space. */
410 if (__glibc_unlikely (inchar () == EOF
))
415 if (__glibc_unlikely (c
!= fc
))
424 /* This is the start of the conversion string. */
427 /* Initialize state of modifiers. */
430 /* Prepare temporary buffer. */
433 /* Check for a positional parameter specification. */
434 if (ISDIGIT ((UCHAR_T
) *f
))
436 argpos
= (UCHAR_T
) *f
++ - L_('0');
437 while (ISDIGIT ((UCHAR_T
) *f
))
438 argpos
= argpos
* 10 + ((UCHAR_T
) *f
++ - L_('0'));
443 /* Oops; that was actually the field width. */
450 /* Check for the assignment-suppressing, the number grouping flag,
451 and the signal to use the locale's digit representation. */
452 while (*f
== L_('*') || *f
== L_('\'') || *f
== L_('I'))
459 #ifdef COMPILE_WSCANF
460 if (thousands
!= L
'\0')
462 if (thousands
!= NULL
)
471 /* Find the maximum field width. */
473 while (ISDIGIT ((UCHAR_T
) *f
))
476 width
+= (UCHAR_T
) *f
++ - L_('0');
482 /* Check for type modifiers. */
486 /* ints are short ints or chars. */
498 /* A double `l' is equivalent to an `L'. */
500 flags
|= LONGDBL
| LONG
;
503 /* ints are long ints. */
508 /* doubles are long doubles, and ints are long long ints. */
509 flags
|= LONGDBL
| LONG
;
512 /* The `a' is used as a flag only if followed by `s', `S' or
514 if (*f
!= L_('s') && *f
!= L_('S') && *f
!= L_('['))
519 /* In __isoc99_*scanf %as, %aS and %a[ extension is not
521 if (stream
->_flags2
& _IO_FLAGS2_SCANF_STD
)
526 /* String conversions (%s, %[) take a `char **'
527 arg and fill it in with a malloc'd pointer. */
531 flags
|= POSIX_MALLOC
;
539 if (need_longlong
&& sizeof (size_t) > sizeof (unsigned long int))
541 else if (sizeof (size_t) > sizeof (unsigned int))
545 if (need_longlong
&& sizeof (uintmax_t) > sizeof (unsigned long int))
547 else if (sizeof (uintmax_t) > sizeof (unsigned int))
551 if (need_longlong
&& sizeof (ptrdiff_t) > sizeof (long int))
553 else if (sizeof (ptrdiff_t) > sizeof (int))
557 /* Not a recognized modifier. Backup. */
562 /* End of the format string? */
563 if (__glibc_unlikely (*f
== L_('\0')))
566 /* Find the conversion specifier. */
568 if (skip_space
|| (fc
!= L_('[') && fc
!= L_('c')
569 && fc
!= L_('C') && fc
!= L_('n')))
571 /* Eat whitespace. */
572 int save_errno
= errno
;
575 /* We add the additional test for EOF here since otherwise
576 inchar will restore the old errno value which might be
577 EINTR but does not indicate an interrupt since nothing
578 was read at this time. */
579 if (__builtin_expect ((c
== EOF
|| inchar () == EOF
)
580 && errno
== EINTR
, 0))
583 __set_errno (save_errno
);
590 case L_('%'): /* Must match a literal '%'. */
592 if (__glibc_unlikely (c
== EOF
))
594 if (__glibc_unlikely (c
!= fc
))
596 ungetc_not_eof (c
, stream
);
601 case L_('n'): /* Answer number of assignments done. */
602 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
603 with the 'n' conversion specifier. */
604 if (!(flags
& SUPPRESS
))
606 /* Don't count the read-ahead. */
607 if (need_longlong
&& (flags
& LONGDBL
))
608 *ARG (long long int *) = read_in
;
609 else if (need_long
&& (flags
& LONG
))
610 *ARG (long int *) = read_in
;
611 else if (flags
& SHORT
)
612 *ARG (short int *) = read_in
;
613 else if (!(flags
& CHAR
))
614 *ARG (int *) = read_in
;
616 *ARG (char *) = read_in
;
618 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
619 /* We have a severe problem here. The ISO C standard
620 contradicts itself in explaining the effect of the %n
621 format in `scanf'. While in ISO C:1990 and the ISO C
622 Amendement 1:1995 the result is described as
624 Execution of a %n directive does not effect the
625 assignment count returned at the completion of
626 execution of the f(w)scanf function.
628 in ISO C Corrigendum 1:1994 the following was added:
631 Add the following fourth example:
634 int d1, d2, n1, n2, i;
635 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
636 the value 123 is assigned to d1 and the value3 to n1.
637 Because %n can never get an input failure the value
638 of 3 is also assigned to n2. The value of d2 is not
639 affected. The value 3 is assigned to i.
641 We go for now with the historically correct code from ISO C,
642 i.e., we don't count the %n assignments. When it ever
643 should proof to be wrong just remove the #ifdef above. */
649 case L_('c'): /* Match characters. */
650 if ((flags
& LONG
) == 0)
655 #define STRING_ARG(Str, Type, Width) \
656 do if (!(flags & SUPPRESS)) \
658 if (flags & MALLOC) \
660 /* The string is to be stored in a malloc'd buffer. */ \
661 /* For %mS using char ** is actually wrong, but \
662 shouldn't make a difference on any arch glibc \
663 supports and would unnecessarily complicate \
665 strptr = ARG (char **); \
666 if (strptr == NULL) \
668 /* Allocate an initial buffer. */ \
670 *strptr = (char *) malloc (strsize * sizeof (Type)); \
671 Str = (Type *) *strptr; \
673 add_ptr_to_free (strptr); \
674 else if (flags & POSIX_MALLOC) \
681 Str = ARG (Type *); \
685 #ifdef COMPILE_WSCANF
686 STRING_ARG (str
, char, 100);
688 STRING_ARG (str
, char, (width
> 1024 ? 1024 : width
));
692 if (__glibc_unlikely (c
== EOF
))
695 #ifdef COMPILE_WSCANF
696 /* We have to convert the wide character(s) into multibyte
697 characters and store the result. */
698 memset (&state
, '\0', sizeof (state
));
704 if (!(flags
& SUPPRESS
) && (flags
& POSIX_MALLOC
)
705 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
707 /* We have to enlarge the buffer if the `m' flag
709 size_t strleng
= str
- *strptr
;
712 newstr
= (char *) realloc (*strptr
, strsize
* 2);
715 /* Can't allocate that much. Last-ditch effort. */
716 newstr
= (char *) realloc (*strptr
,
717 strleng
+ MB_CUR_MAX
);
720 /* c can't have `a' flag, only `m'. */
727 str
= newstr
+ strleng
;
728 strsize
= strleng
+ MB_CUR_MAX
;
734 str
= newstr
+ strleng
;
739 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
740 if (__glibc_unlikely (n
== (size_t) -1))
741 /* No valid wide character. */
744 /* Increment the output pointer. Even if we don't
748 while (--width
> 0 && inchar () != EOF
);
750 if (!(flags
& SUPPRESS
))
755 && (char *) str
== *strptr
+ strsize
)
757 /* Enlarge the buffer. */
760 + (strsize
>= width
? width
- 1 : strsize
);
762 str
= (char *) realloc (*strptr
, newsize
);
765 /* Can't allocate that much. Last-ditch
767 str
= (char *) realloc (*strptr
, strsize
+ 1);
770 /* c can't have `a' flag, only `m'. */
776 *strptr
= (char *) str
;
783 *strptr
= (char *) str
;
790 while (--width
> 0 && inchar () != EOF
);
793 while (--width
> 0 && inchar () != EOF
);
796 if (!(flags
& SUPPRESS
))
798 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
800 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
815 STRING_ARG (wstr
, wchar_t, (width
> 1024 ? 1024 : width
));
818 if (__glibc_unlikely (c
== EOF
))
821 #ifdef COMPILE_WSCANF
822 /* Just store the incoming wide characters. */
823 if (!(flags
& SUPPRESS
))
828 && wstr
== (wchar_t *) *strptr
+ strsize
)
831 = strsize
+ (strsize
> width
? width
- 1 : strsize
);
832 /* Enlarge the buffer. */
833 wstr
= (wchar_t *) realloc (*strptr
,
834 newsize
* sizeof (wchar_t));
837 /* Can't allocate that much. Last-ditch effort. */
838 wstr
= (wchar_t *) realloc (*strptr
,
843 /* C or lc can't have `a' flag, only `m'
850 *strptr
= (char *) wstr
;
857 *strptr
= (char *) wstr
;
864 while (--width
> 0 && inchar () != EOF
);
867 while (--width
> 0 && inchar () != EOF
);
870 /* We have to convert the multibyte input sequence to wide
875 memset (&cstate
, '\0', sizeof (cstate
));
879 /* This is what we present the mbrtowc function first. */
882 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
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
,
895 * sizeof (wchar_t)));
898 /* C or lc can't have `a' flag, only `m' flag. */
904 *strptr
= (char *) wstr
;
911 *strptr
= (char *) wstr
;
921 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
924 if (n
== (size_t) -2)
926 /* Possibly correct character, just not enough
928 if (__glibc_unlikely (inchar () == EOF
))
935 if (__glibc_unlikely (n
!= 1))
938 /* We have a match. */
942 /* Advance the result pointer. */
945 while (--width
> 0 && inchar () != EOF
);
949 if (!(flags
& SUPPRESS
))
951 if ((flags
& MALLOC
) && wstr
- (wchar_t *) *strptr
!= strsize
)
953 wchar_t *cp
= (wchar_t *) realloc (*strptr
,
955 - (wchar_t *) *strptr
)
956 * sizeof (wchar_t)));
958 *strptr
= (char *) cp
;
967 case L_('s'): /* Read a string. */
970 STRING_ARG (str
, char, 100);
973 if (__glibc_unlikely (c
== EOF
))
976 #ifdef COMPILE_WSCANF
977 memset (&state
, '\0', sizeof (state
));
984 ungetc_not_eof (c
, stream
);
988 #ifdef COMPILE_WSCANF
989 /* This is quite complicated. We have to convert the
990 wide characters into multibyte characters and then
995 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
996 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
998 /* We have to enlarge the buffer if the `a' or `m'
1000 size_t strleng
= str
- *strptr
;
1003 newstr
= (char *) realloc (*strptr
, strsize
* 2);
1006 /* Can't allocate that much. Last-ditch
1008 newstr
= (char *) realloc (*strptr
,
1009 strleng
+ MB_CUR_MAX
);
1012 if (flags
& POSIX_MALLOC
)
1017 /* We lose. Oh well. Terminate the
1018 string and stop converting,
1019 so at least we don't skip any input. */
1020 ((char *) (*strptr
))[strleng
] = '\0';
1028 str
= newstr
+ strleng
;
1029 strsize
= strleng
+ MB_CUR_MAX
;
1035 str
= newstr
+ strleng
;
1040 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
,
1042 if (__glibc_unlikely (n
== (size_t) -1))
1045 assert (n
<= MB_CUR_MAX
);
1050 if (!(flags
& SUPPRESS
))
1053 if ((flags
& MALLOC
)
1054 && (char *) str
== *strptr
+ strsize
)
1056 /* Enlarge the buffer. */
1057 str
= (char *) realloc (*strptr
, 2 * strsize
);
1060 /* Can't allocate that much. Last-ditch
1062 str
= (char *) realloc (*strptr
, strsize
+ 1);
1065 if (flags
& POSIX_MALLOC
)
1070 /* We lose. Oh well. Terminate the
1071 string and stop converting,
1072 so at least we don't skip any input. */
1073 ((char *) (*strptr
))[strsize
- 1] = '\0';
1080 *strptr
= (char *) str
;
1087 *strptr
= (char *) str
;
1095 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
1097 if (!(flags
& SUPPRESS
))
1099 #ifdef COMPILE_WSCANF
1100 /* We have to emit the code to get into the initial
1102 char buf
[MB_LEN_MAX
];
1103 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
1104 if (n
> 0 && (flags
& MALLOC
)
1105 && str
+ n
>= *strptr
+ strsize
)
1107 /* Enlarge the buffer. */
1108 size_t strleng
= str
- *strptr
;
1111 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
1114 if (flags
& POSIX_MALLOC
)
1119 /* We lose. Oh well. Terminate the string
1120 and stop converting, so at least we don't
1122 ((char *) (*strptr
))[strleng
] = '\0';
1130 str
= newstr
+ strleng
;
1131 strsize
= strleng
+ n
+ 1;
1135 str
= __mempcpy (str
, buf
, n
);
1139 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
1141 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
1155 #ifndef COMPILE_WSCANF
1159 /* Wide character string. */
1160 STRING_ARG (wstr
, wchar_t, 100);
1163 if (__builtin_expect (c
== EOF
, 0))
1166 #ifndef COMPILE_WSCANF
1167 memset (&cstate
, '\0', sizeof (cstate
));
1174 ungetc_not_eof (c
, stream
);
1178 #ifdef COMPILE_WSCANF
1180 if (!(flags
& SUPPRESS
))
1183 if ((flags
& MALLOC
)
1184 && wstr
== (wchar_t *) *strptr
+ strsize
)
1186 /* Enlarge the buffer. */
1187 wstr
= (wchar_t *) realloc (*strptr
,
1189 * sizeof (wchar_t));
1192 /* Can't allocate that much. Last-ditch
1194 wstr
= (wchar_t *) realloc (*strptr
,
1196 * sizeof (wchar_t));
1199 if (flags
& POSIX_MALLOC
)
1204 /* We lose. Oh well. Terminate the string
1205 and stop converting, so at least we don't
1207 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1214 *strptr
= (char *) wstr
;
1221 *strptr
= (char *) wstr
;
1237 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
1240 if (n
== (size_t) -2)
1242 /* Possibly correct character, just not enough
1244 if (__glibc_unlikely (inchar () == EOF
))
1251 if (__glibc_unlikely (n
!= 1))
1254 /* We have a match. */
1259 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
1260 && wstr
== (wchar_t *) *strptr
+ strsize
)
1262 /* Enlarge the buffer. */
1263 wstr
= (wchar_t *) realloc (*strptr
,
1265 * sizeof (wchar_t)));
1268 /* Can't allocate that much. Last-ditch effort. */
1269 wstr
= (wchar_t *) realloc (*strptr
,
1271 * sizeof (wchar_t)));
1274 if (flags
& POSIX_MALLOC
)
1279 /* We lose. Oh well. Terminate the
1280 string and stop converting, so at
1281 least we don't skip any input. */
1282 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1289 *strptr
= (char *) wstr
;
1296 *strptr
= (char *) wstr
;
1304 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
1306 if (!(flags
& SUPPRESS
))
1310 if ((flags
& MALLOC
) && wstr
- (wchar_t *) *strptr
!= strsize
)
1312 wchar_t *cp
= (wchar_t *) realloc (*strptr
,
1314 - (wchar_t *) *strptr
)
1315 * sizeof(wchar_t)));
1317 *strptr
= (char *) cp
;
1326 case L_('x'): /* Hexadecimal integer. */
1327 case L_('X'): /* Ditto. */
1331 case L_('o'): /* Octal integer. */
1335 case L_('u'): /* Unsigned decimal integer. */
1339 case L_('d'): /* Signed decimal integer. */
1341 flags
|= NUMBER_SIGNED
;
1344 case L_('i'): /* Generic number. */
1346 flags
|= NUMBER_SIGNED
;
1350 if (__glibc_unlikely (c
== EOF
))
1353 /* Check for a sign. */
1354 if (c
== L_('-') || c
== L_('+'))
1362 /* Look for a leading indication of base. */
1363 if (width
!= 0 && c
== L_('0'))
1371 if (width
!= 0 && TOLOWER (c
) == L_('x'))
1389 if (base
== 10 && __builtin_expect ((flags
& I18N
) != 0, 0))
1394 #ifdef COMPILE_WSCANF
1395 const wchar_t *wcdigits
[10];
1396 const wchar_t *wcdigits_extended
[10];
1398 const char *mbdigits
[10];
1399 const char *mbdigits_extended
[10];
1401 /* "to_inpunct" is a map from ASCII digits to their
1402 equivalent in locale. This is defined for locales
1403 which use an extra digits set. */
1404 wctrans_t map
= __wctrans ("to_inpunct");
1408 #ifdef COMPILE_WSCANF
1409 to_level
= _NL_CURRENT_WORD (LC_CTYPE
,
1410 _NL_CTYPE_INDIGITS_WC_LEN
) - 1;
1412 to_level
= (uint32_t) curctype
->values
[_NL_ITEM_INDEX (_NL_CTYPE_INDIGITS_MB_LEN
)].word
- 1;
1415 /* Get the alternative digit forms if there are any. */
1416 if (__glibc_unlikely (map
!= NULL
))
1418 /* Adding new level for extra digits set in locale file. */
1421 for (n
= 0; n
< 10; ++n
)
1423 #ifdef COMPILE_WSCANF
1424 wcdigits
[n
] = (const wchar_t *)
1425 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1427 wchar_t *wc_extended
= (wchar_t *)
1428 alloca ((to_level
+ 2) * sizeof (wchar_t));
1429 __wmemcpy (wc_extended
, wcdigits
[n
], to_level
);
1430 wc_extended
[to_level
] = __towctrans (L
'0' + n
, map
);
1431 wc_extended
[to_level
+ 1] = '\0';
1432 wcdigits_extended
[n
] = wc_extended
;
1435 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1437 /* Get the equivalent wide char in map. */
1438 wint_t extra_wcdigit
= __towctrans (L
'0' + n
, map
);
1440 /* Convert it to multibyte representation. */
1442 memset (&dstate
, '\0', sizeof (dstate
));
1444 char extra_mbdigit
[MB_LEN_MAX
];
1446 = __wcrtomb (extra_mbdigit
, extra_wcdigit
, &dstate
);
1448 if (mblen
== (size_t) -1)
1450 /* Ignore this new level. */
1455 /* Calculate the length of mbdigits[n]. */
1456 const char *last_char
= mbdigits
[n
];
1457 for (level
= 0; level
< to_level
; ++level
)
1458 last_char
= strchr (last_char
, '\0') + 1;
1460 size_t mbdigits_len
= last_char
- mbdigits
[n
];
1462 /* Allocate memory for extended multibyte digit. */
1464 mb_extended
= (char *) alloca (mbdigits_len
+ mblen
+ 1);
1466 /* And get the mbdigits + extra_digit string. */
1467 *(char *) __mempcpy (__mempcpy (mb_extended
, mbdigits
[n
],
1469 extra_mbdigit
, mblen
) = '\0';
1470 mbdigits_extended
[n
] = mb_extended
;
1475 /* Read the number into workspace. */
1476 while (c
!= EOF
&& width
!= 0)
1478 /* In this round we get the pointer to the digit strings
1479 and also perform the first round of comparisons. */
1480 for (n
= 0; n
< 10; ++n
)
1482 /* Get the string for the digits with value N. */
1483 #ifdef COMPILE_WSCANF
1484 if (__glibc_unlikely (map
!= NULL
))
1485 wcdigits
[n
] = wcdigits_extended
[n
];
1487 wcdigits
[n
] = (const wchar_t *)
1488 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1489 wcdigits
[n
] += from_level
;
1491 if (c
== (wint_t) *wcdigits
[n
])
1493 to_level
= from_level
;
1497 /* Advance the pointer to the next string. */
1501 int avail
= width
> 0 ? width
: INT_MAX
;
1503 if (__glibc_unlikely (map
!= NULL
))
1504 mbdigits
[n
] = mbdigits_extended
[n
];
1507 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1509 for (level
= 0; level
< from_level
; level
++)
1510 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1513 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1515 if (*++cmpp
== '\0')
1519 if (avail
== 0 || inchar () == EOF
)
1529 to_level
= from_level
;
1533 /* We are pushing all read characters back. */
1534 if (cmpp
> mbdigits
[n
])
1537 while (--cmpp
> mbdigits
[n
])
1538 ungetc_not_eof ((unsigned char) *cmpp
, stream
);
1539 c
= (unsigned char) *cmpp
;
1542 /* Advance the pointer to the next string. */
1543 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1549 /* Have not yet found the digit. */
1550 for (level
= from_level
+ 1; level
<= to_level
; ++level
)
1552 /* Search all ten digits of this level. */
1553 for (n
= 0; n
< 10; ++n
)
1555 #ifdef COMPILE_WSCANF
1556 if (c
== (wint_t) *wcdigits
[n
])
1559 /* Advance the pointer to the next string. */
1563 int avail
= width
> 0 ? width
: INT_MAX
;
1566 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1568 if (*++cmpp
== '\0')
1572 if (avail
== 0 || inchar () == EOF
)
1585 /* We are pushing all read characters back. */
1586 if (cmpp
> mbdigits
[n
])
1589 while (--cmpp
> mbdigits
[n
])
1590 ungetc_not_eof ((unsigned char) *cmpp
,
1592 c
= (unsigned char) *cmpp
;
1595 /* Advance the pointer to the next string. */
1596 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1612 else if (flags
& GROUP
)
1614 /* Try matching against the thousands separator. */
1615 #ifdef COMPILE_WSCANF
1619 const char *cmpp
= thousands
;
1620 int avail
= width
> 0 ? width
: INT_MAX
;
1622 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1625 if (*++cmpp
== '\0')
1629 if (avail
== 0 || inchar () == EOF
)
1637 /* We are pushing all read characters back. */
1638 if (cmpp
> thousands
)
1640 wpsize
-= cmpp
- thousands
;
1642 while (--cmpp
> thousands
)
1643 ungetc_not_eof ((unsigned char) *cmpp
, stream
);
1644 c
= (unsigned char) *cmpp
;
1652 /* The last thousands character will be added back by
1668 /* Read the number into workspace. */
1669 while (c
!= EOF
&& width
!= 0)
1676 else if (!ISDIGIT (c
) || (int) (c
- L_('0')) >= base
)
1678 if (base
== 10 && (flags
& GROUP
))
1680 /* Try matching against the thousands separator. */
1681 #ifdef COMPILE_WSCANF
1685 const char *cmpp
= thousands
;
1686 int avail
= width
> 0 ? width
: INT_MAX
;
1688 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1691 if (*++cmpp
== '\0')
1695 if (avail
== 0 || inchar () == EOF
)
1703 /* We are pushing all read characters back. */
1704 if (cmpp
> thousands
)
1706 wpsize
-= cmpp
- thousands
;
1708 while (--cmpp
> thousands
)
1709 ungetc_not_eof ((unsigned char) *cmpp
,
1711 c
= (unsigned char) *cmpp
;
1719 /* The last thousands character will be added back by
1735 || (wpsize
== 1 && (wp
[0] == L_('+') || wp
[0] == L_('-'))))
1737 /* There was no number. If we are supposed to read a pointer
1738 we must recognize "(nil)" as well. */
1739 if (__builtin_expect (wpsize
== 0
1740 && (flags
& READ_POINTER
)
1741 && (width
< 0 || width
>= 5)
1743 && TOLOWER (inchar ()) == L_('n')
1744 && TOLOWER (inchar ()) == L_('i')
1745 && TOLOWER (inchar ()) == L_('l')
1746 && inchar () == L_(')'), 1))
1747 /* We must produce the value of a NULL pointer. A single
1748 '0' digit is enough. */
1752 /* The last read character is not part of the number
1760 /* The just read character is not part of the number anymore. */
1763 /* Convert the number. */
1765 if (need_longlong
&& (flags
& LONGDBL
))
1767 if (flags
& NUMBER_SIGNED
)
1768 num
.q
= __strtoll_internal (wp
, &tw
, base
, flags
& GROUP
);
1770 num
.uq
= __strtoull_internal (wp
, &tw
, base
, flags
& GROUP
);
1774 if (flags
& NUMBER_SIGNED
)
1775 num
.l
= __strtol_internal (wp
, &tw
, base
, flags
& GROUP
);
1777 num
.ul
= __strtoul_internal (wp
, &tw
, base
, flags
& GROUP
);
1779 if (__glibc_unlikely (wp
== tw
))
1782 if (!(flags
& SUPPRESS
))
1784 if (flags
& NUMBER_SIGNED
)
1786 if (need_longlong
&& (flags
& LONGDBL
))
1787 *ARG (LONGLONG
int *) = num
.q
;
1788 else if (need_long
&& (flags
& LONG
))
1789 *ARG (long int *) = num
.l
;
1790 else if (flags
& SHORT
)
1791 *ARG (short int *) = (short int) num
.l
;
1792 else if (!(flags
& CHAR
))
1793 *ARG (int *) = (int) num
.l
;
1795 *ARG (signed char *) = (signed char) num
.ul
;
1799 if (need_longlong
&& (flags
& LONGDBL
))
1800 *ARG (unsigned LONGLONG
int *) = num
.uq
;
1801 else if (need_long
&& (flags
& LONG
))
1802 *ARG (unsigned long int *) = num
.ul
;
1803 else if (flags
& SHORT
)
1804 *ARG (unsigned short int *)
1805 = (unsigned short int) num
.ul
;
1806 else if (!(flags
& CHAR
))
1807 *ARG (unsigned int *) = (unsigned int) num
.ul
;
1809 *ARG (unsigned char *) = (unsigned char) num
.ul
;
1815 case L_('e'): /* Floating-point numbers. */
1826 if (__glibc_unlikely (c
== EOF
))
1829 got_digit
= got_dot
= got_e
= 0;
1831 /* Check for a sign. */
1832 if (c
== L_('-') || c
== L_('+'))
1834 negative
= c
== L_('-');
1835 if (__glibc_unlikely (width
== 0 || inchar () == EOF
))
1836 /* EOF is only an input error before we read any chars. */
1844 /* Take care for the special arguments "nan" and "inf". */
1845 if (TOLOWER (c
) == L_('n'))
1849 if (__builtin_expect (width
== 0
1851 || TOLOWER (c
) != L_('a'), 0))
1856 if (__builtin_expect (width
== 0
1858 || TOLOWER (c
) != L_('n'), 0))
1866 else if (TOLOWER (c
) == L_('i'))
1868 /* Maybe "inf" or "infinity". */
1870 if (__builtin_expect (width
== 0
1872 || TOLOWER (c
) != L_('n'), 0))
1877 if (__builtin_expect (width
== 0
1879 || TOLOWER (c
) != L_('f'), 0))
1884 /* It is as least "inf". */
1885 if (width
!= 0 && inchar () != EOF
)
1887 if (TOLOWER (c
) == L_('i'))
1891 /* Now we have to read the rest as well. */
1893 if (__builtin_expect (width
== 0
1895 || TOLOWER (c
) != L_('n'), 0))
1900 if (__builtin_expect (width
== 0
1902 || TOLOWER (c
) != L_('i'), 0))
1907 if (__builtin_expect (width
== 0
1909 || TOLOWER (c
) != L_('t'), 0))
1914 if (__builtin_expect (width
== 0
1916 || TOLOWER (c
) != L_('y'), 0))
1930 if (width
!= 0 && c
== L_('0'))
1936 if (width
!= 0 && TOLOWER (c
) == L_('x'))
1938 /* It is a number in hexadecimal format. */
1941 flags
|= HEXA_FLOAT
;
1944 /* Grouping is not allowed. */
1961 else if (!got_e
&& (flags
& HEXA_FLOAT
) && ISXDIGIT (c
))
1966 else if (got_e
&& wp
[wpsize
- 1] == exp_char
1967 && (c
== L_('-') || c
== L_('+')))
1969 else if (got_digit
&& !got_e
1970 && (CHAR_T
) TOLOWER (c
) == exp_char
)
1973 got_e
= got_dot
= 1;
1977 #ifdef COMPILE_WSCANF
1978 if (! got_dot
&& c
== decimal
)
1983 else if ((flags
& GROUP
) != 0 && ! got_dot
&& c
== thousands
)
1987 /* The last read character is not part of the number
1993 const char *cmpp
= decimal
;
1994 int avail
= width
> 0 ? width
: INT_MAX
;
1998 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1999 if (*++cmpp
== '\0')
2003 if (avail
== 0 || inchar () == EOF
)
2011 /* Add all the characters. */
2012 for (cmpp
= decimal
; *cmpp
!= '\0'; ++cmpp
)
2013 ADDW ((unsigned char) *cmpp
);
2020 /* Figure out whether it is a thousands separator.
2021 There is one problem: we possibly read more than
2022 one character. We cannot push them back but since
2023 we know that parts of the `decimal' string matched,
2024 we can compare against it. */
2025 const char *cmp2p
= thousands
;
2027 if ((flags
& GROUP
) != 0 && ! got_dot
)
2029 while (cmp2p
- thousands
< cmpp
- decimal
2030 && *cmp2p
== decimal
[cmp2p
- thousands
])
2032 if (cmp2p
- thousands
== cmpp
- decimal
)
2034 while ((unsigned char) *cmp2p
== c
&& avail
>= 0)
2035 if (*++cmp2p
== '\0')
2039 if (avail
== 0 || inchar () == EOF
)
2046 if (cmp2p
!= NULL
&& *cmp2p
== '\0')
2048 /* Add all the characters. */
2049 for (cmpp
= thousands
; *cmpp
!= '\0'; ++cmpp
)
2050 ADDW ((unsigned char) *cmpp
);
2056 /* The last read character is not part of the number
2065 if (width
== 0 || inchar () == EOF
)
2073 if (__builtin_expect ((flags
& I18N
) != 0, 0)
2074 /* Hexadecimal floats make no sense, fixing localized
2075 digits with ASCII letters. */
2076 && !(flags
& HEXA_FLOAT
)
2077 /* Minimum requirement. */
2078 && (wpsize
== 0 || got_dot
)
2079 && (map
= __wctrans ("to_inpunct")) != NULL
)
2081 /* Reget the first character. */
2084 /* Localized digits, decimal points, and thousands
2086 wint_t wcdigits
[12];
2088 /* First get decimal equivalent to check if we read it
2090 wcdigits
[11] = __towctrans (L
'.', map
);
2092 /* If we have not read any character or have just read
2093 locale decimal point which matches the decimal point
2094 for localized FP numbers, then we may have localized
2095 digits. Note, we test GOT_DOT above. */
2096 #ifdef COMPILE_WSCANF
2097 if (wpsize
== 0 || (wpsize
== 1 && wcdigits
[11] == decimal
))
2099 char mbdigits
[12][MB_LEN_MAX
+ 1];
2102 memset (&dstate
, '\0', sizeof (dstate
));
2104 bool match_so_far
= wpsize
== 0;
2105 size_t mblen
= __wcrtomb (mbdigits
[11], wcdigits
[11], &dstate
);
2106 if (mblen
!= (size_t) -1)
2108 mbdigits
[11][mblen
] = '\0';
2109 match_so_far
|= (wpsize
== strlen (decimal
)
2110 && strcmp (decimal
, mbdigits
[11]) == 0);
2114 size_t decimal_len
= strlen (decimal
);
2115 /* This should always be the case but the data comes
2117 if (decimal_len
<= MB_LEN_MAX
)
2119 match_so_far
|= wpsize
== decimal_len
;
2120 memcpy (mbdigits
[11], decimal
, decimal_len
+ 1);
2123 match_so_far
= false;
2129 bool have_locthousands
= (flags
& GROUP
) != 0;
2131 /* Now get the digits and the thousands-sep equivalents. */
2132 for (int n
= 0; n
< 11; ++n
)
2135 wcdigits
[n
] = __towctrans (L
'0' + n
, map
);
2138 wcdigits
[10] = __towctrans (L
',', map
);
2139 have_locthousands
&= wcdigits
[10] != L
'\0';
2142 #ifndef COMPILE_WSCANF
2143 memset (&state
, '\0', sizeof (state
));
2145 mblen
= __wcrtomb (mbdigits
[n
], wcdigits
[n
], &state
);
2146 if (mblen
== (size_t) -1)
2150 if (have_locthousands
)
2152 size_t thousands_len
= strlen (thousands
);
2153 if (thousands_len
<= MB_LEN_MAX
)
2154 memcpy (mbdigits
[10], thousands
,
2157 have_locthousands
= false;
2161 /* Ignore checking against localized digits. */
2165 mbdigits
[n
][mblen
] = '\0';
2169 /* Start checking against localized digits, if
2170 conversion is done correctly. */
2173 if (got_e
&& wp
[wpsize
- 1] == exp_char
2174 && (c
== L_('-') || c
== L_('+')))
2176 else if (wpsize
> 0 && !got_e
2177 && (CHAR_T
) TOLOWER (c
) == exp_char
)
2180 got_e
= got_dot
= 1;
2184 /* Check against localized digits, decimal point,
2185 and thousands separator. */
2187 for (n
= 0; n
< 12; ++n
)
2189 #ifdef COMPILE_WSCANF
2190 if (c
== wcdigits
[n
])
2194 else if (n
== 11 && !got_dot
)
2199 else if (n
== 10 && have_locthousands
2203 /* The last read character is not part
2204 of the number anymore. */
2210 const char *cmpp
= mbdigits
[n
];
2211 int avail
= width
> 0 ? width
: INT_MAX
;
2213 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
2214 if (*++cmpp
== '\0')
2218 if (avail
== 0 || inchar () == EOF
)
2229 else if (n
== 11 && !got_dot
)
2231 /* Add all the characters. */
2232 for (cmpp
= decimal
; *cmpp
!= '\0';
2234 ADDW ((unsigned char) *cmpp
);
2238 else if (n
== 10 && (flags
& GROUP
) != 0
2241 /* Add all the characters. */
2242 for (cmpp
= thousands
; *cmpp
!= '\0';
2244 ADDW ((unsigned char) *cmpp
);
2247 /* The last read character is not part
2248 of the number anymore. */
2254 /* We are pushing all read characters back. */
2255 if (cmpp
> mbdigits
[n
])
2258 while (--cmpp
> mbdigits
[n
])
2259 ungetc_not_eof ((unsigned char) *cmpp
,
2261 c
= (unsigned char) *cmpp
;
2268 /* The last read character is not part
2269 of the number anymore. */
2275 if (width
== 0 || inchar () == EOF
)
2283 #ifndef COMPILE_WSCANF
2289 /* Have we read any character? If we try to read a number
2290 in hexadecimal notation and we have read only the `0x'
2291 prefix this is an error. */
2292 if (__builtin_expect (wpsize
== 0
2293 || ((flags
& HEXA_FLOAT
) && wpsize
== 2), 0))
2297 /* Convert the number. */
2299 if ((flags
& LONGDBL
) && !__ldbl_is_dbl
)
2301 long double d
= __strtold_internal (wp
, &tw
, flags
& GROUP
);
2302 if (!(flags
& SUPPRESS
) && tw
!= wp
)
2303 *ARG (long double *) = negative
? -d
: d
;
2305 else if (flags
& (LONG
| LONGDBL
))
2307 double d
= __strtod_internal (wp
, &tw
, flags
& GROUP
);
2308 if (!(flags
& SUPPRESS
) && tw
!= wp
)
2309 *ARG (double *) = negative
? -d
: d
;
2313 float d
= __strtof_internal (wp
, &tw
, flags
& GROUP
);
2314 if (!(flags
& SUPPRESS
) && tw
!= wp
)
2315 *ARG (float *) = negative
? -d
: d
;
2318 if (__glibc_unlikely (tw
== wp
))
2321 if (!(flags
& SUPPRESS
))
2325 case L_('['): /* Character class. */
2327 STRING_ARG (wstr
, wchar_t, 100);
2329 STRING_ARG (str
, char, 100);
2340 /* There is no width given so there is also no limit on the
2341 number of characters we read. Therefore we set width to
2342 a very high value to make the algorithm easier. */
2345 #ifdef COMPILE_WSCANF
2346 /* Find the beginning and the end of the scanlist. We are not
2347 creating a lookup table since it would have to be too large.
2348 Instead we search each time through the string. This is not
2349 a constant lookup time but who uses this feature deserves to
2351 tw
= (wchar_t *) f
; /* Marks the beginning. */
2356 while ((fc
= *f
++) != L
'\0' && fc
!= L
']');
2358 if (__glibc_unlikely (fc
== L
'\0'))
2360 wchar_t *twend
= (wchar_t *) f
- 1;
2362 /* Fill WP with byte flags indexed by character.
2363 We will use this flag map for matching input characters. */
2364 if (wpmax
< UCHAR_MAX
+ 1)
2366 wpmax
= UCHAR_MAX
+ 1;
2367 wp
= (char *) alloca (wpmax
);
2369 memset (wp
, '\0', UCHAR_MAX
+ 1);
2372 if (fc
== ']' || fc
== '-')
2374 /* If ] or - appears before any char in the set, it is not
2375 the terminator or separator, but the first char in the
2381 while ((fc
= *f
++) != '\0' && fc
!= ']')
2382 if (fc
== '-' && *f
!= '\0' && *f
!= ']'
2383 && (unsigned char) f
[-2] <= (unsigned char) *f
)
2385 /* Add all characters from the one before the '-'
2386 up to (but not including) the next format char. */
2387 for (fc
= (unsigned char) f
[-2]; fc
< (unsigned char) *f
; ++fc
)
2391 /* Add the character to the flag map. */
2394 if (__glibc_unlikely (fc
== '\0'))
2400 size_t now
= read_in
;
2401 #ifdef COMPILE_WSCANF
2402 if (__glibc_unlikely (inchar () == WEOF
))
2409 /* Test whether it's in the scanlist. */
2411 while (runp
< twend
)
2413 if (runp
[0] == L
'-' && runp
[1] != '\0'
2414 && runp
+ 1 != twend
2416 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2418 /* Match against all characters in between the
2419 first and last character of the sequence. */
2422 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2423 if ((wint_t) wc
== c
)
2426 if (wc
<= runp
[1] && !not_in
)
2428 if (wc
<= runp
[1] && not_in
)
2430 /* The current character is not in the
2440 if ((wint_t) *runp
== c
&& !not_in
)
2442 if ((wint_t) *runp
== c
&& not_in
)
2452 if (runp
== twend
&& !not_in
)
2458 if (!(flags
& SUPPRESS
))
2462 if ((flags
& MALLOC
)
2463 && wstr
== (wchar_t *) *strptr
+ strsize
)
2465 /* Enlarge the buffer. */
2466 wstr
= (wchar_t *) realloc (*strptr
,
2468 * sizeof (wchar_t));
2471 /* Can't allocate that much. Last-ditch
2474 realloc (*strptr
, (strsize
+ 1)
2475 * sizeof (wchar_t));
2478 if (flags
& POSIX_MALLOC
)
2483 /* We lose. Oh well. Terminate the string
2484 and stop converting, so at least we don't
2486 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2493 *strptr
= (char *) wstr
;
2500 *strptr
= (char *) wstr
;
2507 while (--width
> 0 && inchar () != WEOF
);
2510 char buf
[MB_LEN_MAX
];
2514 if (__glibc_unlikely (inchar () == EOF
))
2517 memset (&cstate
, '\0', sizeof (cstate
));
2521 if (wp
[c
] == not_in
)
2523 ungetc_not_eof (c
, stream
);
2528 if (!(flags
& SUPPRESS
))
2532 /* Convert it into a wide character. */
2534 n
= __mbrtowc (wstr
, buf
, 1, &cstate
);
2536 if (n
== (size_t) -2)
2538 /* Possibly correct character, just not enough
2541 assert (cnt
< MB_CUR_MAX
);
2547 if ((flags
& MALLOC
)
2548 && wstr
== (wchar_t *) *strptr
+ strsize
)
2550 /* Enlarge the buffer. */
2551 wstr
= (wchar_t *) realloc (*strptr
,
2553 * sizeof (wchar_t)));
2556 /* Can't allocate that much. Last-ditch
2559 realloc (*strptr
, ((strsize
+ 1)
2560 * sizeof (wchar_t)));
2563 if (flags
& POSIX_MALLOC
)
2568 /* We lose. Oh well. Terminate the
2569 string and stop converting,
2570 so at least we don't skip any input. */
2571 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2578 *strptr
= (char *) wstr
;
2585 *strptr
= (char *) wstr
;
2595 while (inchar () != EOF
);
2597 if (__glibc_unlikely (cnt
!= 0))
2598 /* We stopped in the middle of recognizing another
2599 character. That's a problem. */
2603 if (__glibc_unlikely (now
== read_in
))
2604 /* We haven't succesfully read any character. */
2607 if (!(flags
& SUPPRESS
))
2611 if ((flags
& MALLOC
)
2612 && wstr
- (wchar_t *) *strptr
!= strsize
)
2614 wchar_t *cp
= (wchar_t *)
2615 realloc (*strptr
, ((wstr
- (wchar_t *) *strptr
)
2616 * sizeof(wchar_t)));
2618 *strptr
= (char *) cp
;
2627 size_t now
= read_in
;
2629 if (__glibc_unlikely (inchar () == EOF
))
2632 #ifdef COMPILE_WSCANF
2634 memset (&state
, '\0', sizeof (state
));
2641 /* Test whether it's in the scanlist. */
2643 while (runp
< twend
)
2645 if (runp
[0] == L
'-' && runp
[1] != '\0'
2646 && runp
+ 1 != twend
2648 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2650 /* Match against all characters in between the
2651 first and last character of the sequence. */
2654 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2655 if ((wint_t) wc
== c
)
2658 if (wc
<= runp
[1] && !not_in
)
2660 if (wc
<= runp
[1] && not_in
)
2662 /* The current character is not in the
2672 if ((wint_t) *runp
== c
&& !not_in
)
2674 if ((wint_t) *runp
== c
&& not_in
)
2684 if (runp
== twend
&& !not_in
)
2690 if (!(flags
& SUPPRESS
))
2692 if ((flags
& MALLOC
)
2693 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
2695 /* Enlarge the buffer. */
2696 size_t strleng
= str
- *strptr
;
2699 newstr
= (char *) realloc (*strptr
, 2 * strsize
);
2702 /* Can't allocate that much. Last-ditch
2704 newstr
= (char *) realloc (*strptr
,
2705 strleng
+ MB_CUR_MAX
);
2708 if (flags
& POSIX_MALLOC
)
2713 /* We lose. Oh well. Terminate the string
2714 and stop converting, so at least we don't
2716 ((char *) (*strptr
))[strleng
] = '\0';
2724 str
= newstr
+ strleng
;
2725 strsize
= strleng
+ MB_CUR_MAX
;
2731 str
= newstr
+ strleng
;
2737 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
2738 if (__glibc_unlikely (n
== (size_t) -1))
2741 assert (n
<= MB_CUR_MAX
);
2744 while (--width
> 0 && inchar () != WEOF
);
2749 if (wp
[c
] == not_in
)
2751 ungetc_not_eof (c
, stream
);
2756 if (!(flags
& SUPPRESS
))
2759 if ((flags
& MALLOC
)
2760 && (char *) str
== *strptr
+ strsize
)
2762 /* Enlarge the buffer. */
2763 size_t newsize
= 2 * strsize
;
2766 str
= (char *) realloc (*strptr
, newsize
);
2769 /* Can't allocate that much. Last-ditch
2771 if (newsize
> strsize
+ 1)
2773 newsize
= strsize
+ 1;
2776 if (flags
& POSIX_MALLOC
)
2781 /* We lose. Oh well. Terminate the
2782 string and stop converting,
2783 so at least we don't skip any input. */
2784 ((char *) (*strptr
))[strsize
- 1] = '\0';
2791 *strptr
= (char *) str
;
2798 while (--width
> 0 && inchar () != EOF
);
2801 if (__glibc_unlikely (now
== read_in
))
2802 /* We haven't succesfully read any character. */
2805 if (!(flags
& SUPPRESS
))
2807 #ifdef COMPILE_WSCANF
2808 /* We have to emit the code to get into the initial
2810 char buf
[MB_LEN_MAX
];
2811 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
2812 if (n
> 0 && (flags
& MALLOC
)
2813 && str
+ n
>= *strptr
+ strsize
)
2815 /* Enlarge the buffer. */
2816 size_t strleng
= str
- *strptr
;
2819 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
2822 if (flags
& POSIX_MALLOC
)
2827 /* We lose. Oh well. Terminate the string
2828 and stop converting, so at least we don't
2830 ((char *) (*strptr
))[strleng
] = '\0';
2838 str
= newstr
+ strleng
;
2839 strsize
= strleng
+ n
+ 1;
2843 str
= __mempcpy (str
, buf
, n
);
2847 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
2849 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
2860 case L_('p'): /* Generic pointer. */
2862 /* A PTR must be the same size as a `long int'. */
2863 flags
&= ~(SHORT
|LONGDBL
);
2866 flags
|= READ_POINTER
;
2870 /* If this is an unknown format character punt. */
2875 /* The last thing we saw int the format string was a white space.
2876 Consume the last white spaces. */
2881 while (ISSPACE (c
));
2886 /* Unlock stream. */
2887 UNLOCK_STREAM (stream
);
2895 if (__glibc_unlikely (done
== EOF
))
2897 if (__glibc_unlikely (ptrs_to_free
!= NULL
))
2899 struct ptrs_to_free
*p
= ptrs_to_free
;
2902 for (size_t cnt
= 0; cnt
< p
->count
; ++cnt
)
2904 free (*p
->ptrs
[cnt
]);
2905 *p
->ptrs
[cnt
] = NULL
;
2912 else if (__glibc_unlikely (strptr
!= NULL
))
2920 #ifdef COMPILE_WSCANF
2922 __vfwscanf (FILE *s
, const wchar_t *format
, va_list argptr
)
2924 return _IO_vfwscanf (s
, format
, argptr
, NULL
);
2926 ldbl_weak_alias (__vfwscanf
, vfwscanf
)
2929 ___vfscanf (FILE *s
, const char *format
, va_list argptr
)
2931 return _IO_vfscanf_internal (s
, format
, argptr
, NULL
);
2933 ldbl_strong_alias (_IO_vfscanf_internal
, _IO_vfscanf
)
2934 ldbl_hidden_def (_IO_vfscanf_internal
, _IO_vfscanf
)
2935 ldbl_strong_alias (___vfscanf
, __vfscanf
)
2936 ldbl_hidden_def (___vfscanf
, __vfscanf
)
2937 ldbl_weak_alias (___vfscanf
, vfscanf
)