1 /* Copyright (C) 1991-2002, 2003, 2004 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
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 0x001 /* l: long or double */
56 #define LONGDBL 0x002 /* L: long long or long double */
57 #define SHORT 0x004 /* h: short */
58 #define SUPPRESS 0x008 /* *: suppress assignment */
59 #define POINTER 0x010 /* weird %p pointer (`fake hex') */
60 #define NOSKIP 0x020 /* do not skip blanks */
61 #define WIDTH 0x040 /* width was given */
62 #define GROUP 0x080 /* ': group numbers */
63 #define MALLOC 0x100 /* a: malloc strings */
64 #define CHAR 0x200 /* hh: char */
65 #define I18N 0x400 /* I: use locale's digits */
68 #include <locale/localeinfo.h>
73 #define va_list _IO_va_list
76 # define ungetc(c, s) ((void) (c == WEOF \
78 INTUSE(_IO_sputbackwc) (s, c))))
79 # define ungetc_not_eof(c, s) ((void) (--read_in, \
80 INTUSE(_IO_sputbackwc) (s, c)))
81 # define inchar() (c == WEOF ? ((errno = inchar_errno), WEOF) \
82 : ((c = _IO_getwc_unlocked (s)), \
85 : (size_t) (inchar_errno = errno)), c))
87 # define MEMCPY(d, s, n) __wmemcpy (d, s, n)
88 # define ISSPACE(Ch) iswspace (Ch)
89 # define ISDIGIT(Ch) iswdigit (Ch)
90 # define ISXDIGIT(Ch) iswxdigit (Ch)
91 # define TOLOWER(Ch) towlower (Ch)
92 # define ORIENT if (_IO_fwide (s, 1) != 1) return WEOF
93 # define __strtoll_internal __wcstoll_internal
94 # define __strtoull_internal __wcstoull_internal
95 # define __strtol_internal __wcstol_internal
96 # define __strtoul_internal __wcstoul_internal
97 # define __strtold_internal __wcstold_internal
98 # define __strtod_internal __wcstod_internal
99 # define __strtof_internal __wcstof_internal
101 # define L_(Str) L##Str
102 # define CHAR_T wchar_t
103 # define UCHAR_T unsigned int
104 # define WINT_T wint_t
108 # define ungetc(c, s) ((void) ((int) c == EOF \
110 INTUSE(_IO_sputbackc) (s, (unsigned char) c))))
111 # define ungetc_not_eof(c, s) ((void) (--read_in, \
112 INTUSE(_IO_sputbackc) (s, (unsigned char) c)))
113 # define inchar() (c == EOF ? ((errno = inchar_errno), EOF) \
114 : ((c = _IO_getc_unlocked (s)), \
117 : (size_t) (inchar_errno = errno)), c))
118 # define MEMCPY(d, s, n) memcpy (d, s, n)
119 # define ISSPACE(Ch) __isspace_l (Ch, loc)
120 # define ISDIGIT(Ch) __isdigit_l (Ch, loc)
121 # define ISXDIGIT(Ch) __isxdigit_l (Ch, loc)
122 # define TOLOWER(Ch) __tolower_l ((unsigned char) (Ch), loc)
123 # define ORIENT if (_IO_vtable_offset (s) == 0 \
124 && _IO_fwide (s, -1) != -1) \
129 # define UCHAR_T unsigned char
133 #define encode_error() do { \
135 __set_errno (EILSEQ); \
138 #define conv_error() do { \
142 #define input_error() do { \
144 if (done == 0) done = EOF; \
147 #define memory_error() do { \
148 __set_errno (ENOMEM); \
152 #define ARGCHECK(s, format) \
155 /* Check file argument for consistence. */ \
156 CHECK_FILE (s, EOF); \
157 if (s->_flags & _IO_NO_READS) \
159 __set_errno (EBADF); \
162 else if (format == NULL) \
168 #define LOCK_STREAM(S) \
169 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, (S)); \
171 #define UNLOCK_STREAM(S) \
172 _IO_funlockfile (S); \
173 __libc_cleanup_region_end (0)
176 /* Read formatted input from S according to the format string
177 FORMAT, using the argument list in ARG.
178 Return the number of assignments made, or -1 for an input error. */
179 #ifdef COMPILE_WSCANF
181 _IO_vfwscanf (s
, format
, argptr
, errp
)
183 const wchar_t *format
;
188 _IO_vfscanf (s
, format
, argptr
, errp
)
196 register const CHAR_T
*f
= format
;
197 register UCHAR_T fc
; /* Current character of the format. */
198 register WINT_T done
= 0; /* Assignments done. */
199 register size_t read_in
= 0; /* Chars read in. */
200 register WINT_T c
= 0; /* Last char read. */
201 register int width
; /* Maximum field width. */
202 register int flags
; /* Modifiers for current format element. */
204 #ifndef COMPILE_WSCANF
205 __locale_t loc
= _NL_CURRENT_LOCALE
;
206 struct locale_data
*const curctype
= loc
->__locales
[LC_CTYPE
];
209 /* Errno of last failed inchar call. */
210 int inchar_errno
= 0;
211 /* Status for reading F-P nums. */
212 char got_dot
, got_e
, negative
;
213 /* If a [...] is a [^...]. */
215 #define exp_char not_in
216 /* Base for integral numbers. */
218 /* Signedness for integral numbers. */
220 #define is_hexa number_signed
221 /* Decimal point character. */
222 #ifdef COMPILE_WSCANF
227 /* The thousands character of the current locale. */
228 #ifdef COMPILE_WSCANF
231 const char *thousands
;
233 /* State for the conversions. */
235 /* Integral holding variables. */
239 unsigned long long int uq
;
241 unsigned long int ul
;
243 /* Character-buffer pointer. */
245 wchar_t *wstr
= NULL
;
246 char **strptr
= NULL
;
248 /* We must not react on white spaces immediately because they can
249 possibly be matched even if in the input stream no character is
250 available anymore. */
252 /* Nonzero if we are reading a pointer. */
255 CHAR_T
*tw
; /* Temporary pointer. */
256 CHAR_T
*wp
= NULL
; /* Workspace. */
257 size_t wpmax
= 0; /* Maximal size of workspace. */
258 size_t wpsize
; /* Currently used bytes in workspace. */
262 if (wpsize == wpmax) \
265 wpmax = (UCHAR_MAX + 1 > 2 * wpmax ? UCHAR_MAX + 1 : 2 * wpmax); \
266 wp = (CHAR_T *) alloca (wpmax * sizeof (wchar_t)); \
268 MEMCPY (wp, old, wpsize); \
270 wp[wpsize++] = (Ch); \
275 __va_copy (arg
, argptr
);
277 arg
= (va_list) argptr
;
284 ARGCHECK (s
, format
);
287 #ifndef COMPILE_WSCANF
288 struct locale_data
*const curnumeric
= loc
->__locales
[LC_NUMERIC
];
291 /* Figure out the decimal point character. */
292 #ifdef COMPILE_WSCANF
293 decimal
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
295 decimal
= curnumeric
->values
[_NL_ITEM_INDEX (DECIMAL_POINT
)].string
;
297 /* Figure out the thousands separator character. */
298 #ifdef COMPILE_WSCANF
299 thousands
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_THOUSANDS_SEP_WC
);
301 thousands
= curnumeric
->values
[_NL_ITEM_INDEX (THOUSANDS_SEP
)].string
;
302 if (*thousands
== '\0')
307 /* Lock the stream. */
311 #ifndef COMPILE_WSCANF
312 /* From now on we use `state' to convert the format string. */
313 memset (&state
, '\0', sizeof (state
));
316 /* Run through the format string. */
320 /* Extract the next argument, which is of type TYPE.
321 For a %N$... spec, this is the Nth argument from the beginning;
322 otherwise it is the next argument after the state now in ARG. */
324 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
325 ({ unsigned int pos = argpos; \
327 __va_copy (arg, argptr); \
329 (void) va_arg (arg, void *); \
330 va_arg (arg, type); \
334 /* XXX Possible optimization. */
335 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
336 ({ va_list arg = (va_list) argptr; \
337 arg = (va_list) ((char *) arg \
339 * __va_rounded_size (void *)); \
340 va_arg (arg, type); \
343 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
344 ({ unsigned int pos = argpos; \
345 va_list arg = (va_list) argptr; \
347 (void) va_arg (arg, void *); \
348 va_arg (arg, type); \
353 #ifndef COMPILE_WSCANF
354 if (!isascii ((unsigned char) *f
))
356 /* Non-ASCII, may be a multibyte. */
357 int len
= __mbrlen (f
, strlen (f
), &state
);
365 else if (c
!= (unsigned char) *f
++)
367 ungetc_not_eof (c
, s
);
380 /* Remember to skip spaces. */
387 /* Read a character. */
390 /* Characters other than format specs must just match. */
394 /* We saw white space char as the last character in the format
395 string. Now it's time to skip all leading white space. */
399 if (inchar () == EOF
)
413 /* This is the start of the conversion string. */
416 /* Not yet decided whether we read a pointer or not. */
419 /* Initialize state of modifiers. */
422 /* Prepare temporary buffer. */
425 /* Check for a positional parameter specification. */
426 if (ISDIGIT ((UCHAR_T
) *f
))
428 argpos
= (UCHAR_T
) *f
++ - L_('0');
429 while (ISDIGIT ((UCHAR_T
) *f
))
430 argpos
= argpos
* 10 + ((UCHAR_T
) *f
++ - L_('0'));
435 /* Oops; that was actually the field width. */
443 /* Check for the assignment-suppressing, the number grouping flag,
444 and the signal to use the locale's digit representation. */
445 while (*f
== L_('*') || *f
== L_('\'') || *f
== L_('I'))
459 /* We have seen width. */
460 if (ISDIGIT ((UCHAR_T
) *f
))
463 /* Find the maximum field width. */
465 while (ISDIGIT ((UCHAR_T
) *f
))
468 width
+= (UCHAR_T
) *f
++ - L_('0');
474 /* Check for type modifiers. */
478 /* ints are short ints or chars. */
490 /* A double `l' is equivalent to an `L'. */
492 flags
|= LONGDBL
| LONG
;
495 /* ints are long ints. */
500 /* doubles are long doubles, and ints are long long ints. */
501 flags
|= LONGDBL
| LONG
;
504 /* The `a' is used as a flag only if followed by `s', `S' or
506 if (*f
!= L_('s') && *f
!= L_('S') && *f
!= L_('['))
511 /* String conversions (%s, %[) take a `char **'
512 arg and fill it in with a malloc'd pointer. */
516 if (need_longlong
&& sizeof (size_t) > sizeof (unsigned long int))
518 else if (sizeof (size_t) > sizeof (unsigned int))
522 if (need_longlong
&& sizeof (uintmax_t) > sizeof (unsigned long int))
524 else if (sizeof (uintmax_t) > sizeof (unsigned int))
528 if (need_longlong
&& sizeof (ptrdiff_t) > sizeof (long int))
530 else if (sizeof (ptrdiff_t) > sizeof (int))
534 /* Not a recognized modifier. Backup. */
539 /* End of the format string? */
543 /* Find the conversion specifier. */
545 if (skip_space
|| (fc
!= L_('[') && fc
!= L_('c')
546 && fc
!= L_('C') && fc
!= L_('n')
549 /* Eat whitespace. */
550 int save_errno
= errno
;
553 if (inchar () == EOF
&& errno
== EINTR
)
563 case L_('%'): /* Must match a literal '%'. */
569 ungetc_not_eof (c
, s
);
574 case L_('n'): /* Answer number of assignments done. */
575 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
576 with the 'n' conversion specifier. */
577 if (!(flags
& SUPPRESS
))
579 /* Don't count the read-ahead. */
580 if (need_longlong
&& (flags
& LONGDBL
))
581 *ARG (long long int *) = read_in
;
582 else if (need_long
&& (flags
& LONG
))
583 *ARG (long int *) = read_in
;
584 else if (flags
& SHORT
)
585 *ARG (short int *) = read_in
;
586 else if (!(flags
& CHAR
))
587 *ARG (int *) = read_in
;
589 *ARG (char *) = read_in
;
591 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
592 /* We have a severe problem here. The ISO C standard
593 contradicts itself in explaining the effect of the %n
594 format in `scanf'. While in ISO C:1990 and the ISO C
595 Amendement 1:1995 the result is described as
597 Execution of a %n directive does not effect the
598 assignment count returned at the completion of
599 execution of the f(w)scanf function.
601 in ISO C Corrigendum 1:1994 the following was added:
604 Add the following fourth example:
607 int d1, d2, n1, n2, i;
608 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
609 the value 123 is assigned to d1 and the value3 to n1.
610 Because %n can never get an input failure the value
611 of 3 is also assigned to n2. The value of d2 is not
612 affected. The value 3 is assigned to i.
614 We go for now with the historically correct code from ISO C,
615 i.e., we don't count the %n assignments. When it ever
616 should proof to be wrong just remove the #ifdef above. */
622 case L_('c'): /* Match characters. */
623 if ((flags
& LONG
) == 0)
625 if (!(flags
& SUPPRESS
))
639 #ifdef COMPILE_WSCANF
640 /* We have to convert the wide character(s) into multibyte
641 characters and store the result. */
642 memset (&state
, '\0', sizeof (state
));
648 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
649 if (n
== (size_t) -1)
650 /* No valid wide character. */
653 /* Increment the output pointer. Even if we don't
657 while (--width
> 0 && inchar () != EOF
);
659 if (!(flags
& SUPPRESS
))
663 while (--width
> 0 && inchar () != EOF
);
666 while (--width
> 0 && inchar () != EOF
);
669 if (!(flags
& SUPPRESS
))
676 if (!(flags
& SUPPRESS
))
678 wstr
= ARG (wchar_t *);
687 #ifdef COMPILE_WSCANF
688 /* Just store the incoming wide characters. */
689 if (!(flags
& SUPPRESS
))
693 while (--width
> 0 && inchar () != EOF
);
696 while (--width
> 0 && inchar () != EOF
);
699 /* We have to convert the multibyte input sequence to wide
704 memset (&cstate
, '\0', sizeof (cstate
));
708 /* This is what we present the mbrtowc function first. */
715 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
718 if (n
== (size_t) -2)
720 /* Possibly correct character, just not enough
722 if (inchar () == EOF
)
732 /* We have a match. */
736 /* Advance the result pointer. */
739 while (--width
> 0 && inchar () != EOF
);
743 if (!(flags
& SUPPRESS
))
748 case L_('s'): /* Read a string. */
751 #define STRING_ARG(Str, Type) \
752 do if (!(flags & SUPPRESS)) \
754 if (flags & MALLOC) \
756 /* The string is to be stored in a malloc'd buffer. */ \
757 strptr = ARG (char **); \
758 if (strptr == NULL) \
760 /* Allocate an initial buffer. */ \
762 *strptr = (char *) malloc (strsize * sizeof (Type)); \
763 Str = (Type *) *strptr; \
766 Str = ARG (Type *); \
770 STRING_ARG (str
, char);
776 #ifdef COMPILE_WSCANF
777 memset (&state
, '\0', sizeof (state
));
784 ungetc_not_eof (c
, s
);
788 #ifdef COMPILE_WSCANF
789 /* This is quite complicated. We have to convert the
790 wide characters into multibyte characters and then
795 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
796 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
798 /* We have to enlarge the buffer if the `a' flag
800 size_t strleng
= str
- *strptr
;
803 newstr
= (char *) realloc (*strptr
, strsize
* 2);
806 /* Can't allocate that much. Last-ditch
808 newstr
= (char *) realloc (*strptr
,
809 strleng
+ MB_CUR_MAX
);
812 /* We lose. Oh well. Terminate the
813 string and stop converting,
814 so at least we don't skip any input. */
815 ((char *) (*strptr
))[strleng
] = '\0';
822 str
= newstr
+ strleng
;
823 strsize
= strleng
+ MB_CUR_MAX
;
829 str
= newstr
+ strleng
;
834 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
,
836 if (n
== (size_t) -1)
839 assert (n
<= MB_CUR_MAX
);
844 if (!(flags
& SUPPRESS
))
848 && (char *) str
== *strptr
+ strsize
)
850 /* Enlarge the buffer. */
851 str
= (char *) realloc (*strptr
, 2 * strsize
);
854 /* Can't allocate that much. Last-ditch
856 str
= (char *) realloc (*strptr
, strsize
+ 1);
859 /* We lose. Oh well. Terminate the
860 string and stop converting,
861 so at least we don't skip any input. */
862 ((char *) (*strptr
))[strsize
- 1] = '\0';
868 *strptr
= (char *) str
;
875 *strptr
= (char *) str
;
883 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
885 if (!(flags
& SUPPRESS
))
887 #ifdef COMPILE_WSCANF
888 /* We have to emit the code to get into the initial
890 char buf
[MB_LEN_MAX
];
891 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
892 if (n
> 0 && (flags
& MALLOC
)
893 && str
+ n
>= *strptr
+ strsize
)
895 /* Enlarge the buffer. */
896 size_t strleng
= str
- *strptr
;
899 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
902 /* We lose. Oh well. Terminate the string
903 and stop converting, so at least we don't
905 ((char *) (*strptr
))[strleng
] = '\0';
912 str
= newstr
+ strleng
;
913 strsize
= strleng
+ n
+ 1;
917 str
= __mempcpy (str
, buf
, n
);
921 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
923 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
936 #ifndef COMPILE_WSCANF
940 /* Wide character string. */
941 STRING_ARG (wstr
, wchar_t);
947 #ifndef COMPILE_WSCANF
948 memset (&cstate
, '\0', sizeof (cstate
));
955 ungetc_not_eof (c
, s
);
959 #ifdef COMPILE_WSCANF
961 if (!(flags
& SUPPRESS
))
965 && wstr
== (wchar_t *) *strptr
+ strsize
)
967 /* Enlarge the buffer. */
968 wstr
= (wchar_t *) realloc (*strptr
,
973 /* Can't allocate that much. Last-ditch
975 wstr
= (wchar_t *) realloc (*strptr
,
980 /* We lose. Oh well. Terminate the string
981 and stop converting, so at least we don't
983 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
989 *strptr
= (char *) wstr
;
996 *strptr
= (char *) wstr
;
1012 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
1015 if (n
== (size_t) -2)
1017 /* Possibly correct character, just not enough
1019 if (inchar () == EOF
)
1029 /* We have a match. */
1034 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
1035 && wstr
== (wchar_t *) *strptr
+ strsize
)
1037 /* Enlarge the buffer. */
1038 wstr
= (wchar_t *) realloc (*strptr
,
1040 * sizeof (wchar_t)));
1043 /* Can't allocate that much. Last-ditch effort. */
1044 wstr
= (wchar_t *) realloc (*strptr
,
1046 * sizeof (wchar_t)));
1049 /* We lose. Oh well. Terminate the
1050 string and stop converting, so at
1051 least we don't skip any input. */
1052 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1058 *strptr
= (char *) wstr
;
1065 *strptr
= (char *) wstr
;
1073 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
1075 if (!(flags
& SUPPRESS
))
1079 if ((flags
& MALLOC
) && wstr
- (wchar_t *) *strptr
!= strsize
)
1081 wchar_t *cp
= (wchar_t *) realloc (*strptr
,
1083 - (wchar_t *) *strptr
)
1084 * sizeof(wchar_t)));
1086 *strptr
= (char *) cp
;
1094 case L_('x'): /* Hexadecimal integer. */
1095 case L_('X'): /* Ditto. */
1100 case L_('o'): /* Octal integer. */
1105 case L_('u'): /* Unsigned decimal integer. */
1110 case L_('d'): /* Signed decimal integer. */
1115 case L_('i'): /* Generic number. */
1124 /* Check for a sign. */
1125 if (c
== L_('-') || c
== L_('+'))
1133 /* Look for a leading indication of base. */
1134 if (width
!= 0 && c
== L_('0'))
1142 if (width
!= 0 && TOLOWER (c
) == L_('x'))
1160 if (base
== 10 && (flags
& I18N
) != 0)
1165 #ifdef COMPILE_WSCANF
1166 const wchar_t *wcdigits
[10];
1168 const char *mbdigits
[10];
1173 #ifdef COMPILE_WSCANF
1174 to_level
= _NL_CURRENT_WORD (LC_CTYPE
,
1175 _NL_CTYPE_INDIGITS_WC_LEN
) - 1;
1177 to_level
= (uint32_t) curctype
->values
[_NL_ITEM_INDEX (_NL_CTYPE_INDIGITS_MB_LEN
)].word
- 1;
1180 /* Read the number into workspace. */
1181 while (c
!= EOF
&& width
!= 0)
1183 /* In this round we get the pointer to the digit strings
1184 and also perform the first round of comparisons. */
1185 for (n
= 0; n
< 10; ++n
)
1187 /* Get the string for the digits with value N. */
1188 #ifdef COMPILE_WSCANF
1189 wcdigits
[n
] = (const wchar_t *)
1190 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1191 wcdigits
[n
] += from_level
;
1193 if (c
== (wint_t) *wcdigits
[n
])
1195 to_level
= from_level
;
1199 /* Advance the pointer to the next string. */
1203 int avail
= width
> 0 ? width
: INT_MAX
;
1206 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1208 for (level
= 0; level
< from_level
; level
++)
1209 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1212 while ((unsigned char) *cmpp
== c
&& avail
> 0)
1214 if (*++cmpp
== '\0')
1218 if ((c
= inchar ()) == EOF
)
1228 to_level
= from_level
;
1232 /* We are pushing all read characters back. */
1233 if (cmpp
> mbdigits
[n
])
1236 while (--cmpp
> mbdigits
[n
])
1237 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1238 c
= (unsigned char) *cmpp
;
1241 /* Advance the pointer to the next string. */
1242 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1248 /* Have not yet found the digit. */
1249 for (level
= from_level
+ 1; level
<= to_level
; ++level
)
1251 /* Search all ten digits of this level. */
1252 for (n
= 0; n
< 10; ++n
)
1254 #ifdef COMPILE_WSCANF
1255 if (c
== (wint_t) *wcdigits
[n
])
1258 /* Advance the pointer to the next string. */
1262 int avail
= width
> 0 ? width
: INT_MAX
;
1265 while ((unsigned char) *cmpp
== c
&& avail
> 0)
1267 if (*++cmpp
== '\0')
1271 if ((c
= inchar ()) == EOF
)
1284 /* We are pushing all read characters back. */
1285 if (cmpp
> mbdigits
[n
])
1288 while (--cmpp
> mbdigits
[n
])
1289 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1290 c
= (unsigned char) *cmpp
;
1293 /* Advance the pointer to the next string. */
1294 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1310 else if ((flags
& GROUP
)
1311 #ifdef COMPILE_WSCANF
1312 && thousands
!= L
'\0'
1314 && thousands
!= NULL
1318 /* Try matching against the thousands separator. */
1319 #ifdef COMPILE_WSCANF
1323 const char *cmpp
= thousands
;
1324 int avail
= width
> 0 ? width
: INT_MAX
;
1326 while ((unsigned char) *cmpp
== c
&& avail
> 0)
1329 if (*++cmpp
== '\0')
1333 if ((c
= inchar ()) == EOF
)
1341 /* We are pushing all read characters back. */
1342 if (cmpp
> thousands
)
1344 wpsize
-= cmpp
- thousands
;
1346 while (--cmpp
> thousands
)
1347 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1348 c
= (unsigned char) *cmpp
;
1356 /* The last thousands character will be added back by
1372 /* Read the number into workspace. */
1373 while (c
!= EOF
&& width
!= 0)
1380 else if (!ISDIGIT (c
) || (int) (c
- L_('0')) >= base
)
1382 if (base
== 10 && (flags
& GROUP
)
1383 #ifdef COMPILE_WSCANF
1384 && thousands
!= L
'\0'
1386 && thousands
!= NULL
1390 /* Try matching against the thousands separator. */
1391 #ifdef COMPILE_WSCANF
1395 const char *cmpp
= thousands
;
1396 int avail
= width
> 0 ? width
: INT_MAX
;
1398 while ((unsigned char) *cmpp
== c
&& avail
> 0)
1401 if (*++cmpp
== '\0')
1405 if ((c
= inchar ()) == EOF
)
1413 /* We are pushing all read characters back. */
1414 if (cmpp
> thousands
)
1416 wpsize
-= cmpp
- thousands
;
1418 while (--cmpp
> thousands
)
1419 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1420 c
= (unsigned char) *cmpp
;
1428 /* The last thousands character will be added back by
1444 || (wpsize
== 1 && (wp
[0] == L_('+') || wp
[0] == L_('-'))))
1446 /* There was no number. If we are supposed to read a pointer
1447 we must recognize "(nil)" as well. */
1448 if (wpsize
== 0 && read_pointer
&& (width
< 0 || width
>= 0)
1450 && TOLOWER (inchar ()) == L_('n')
1451 && TOLOWER (inchar ()) == L_('i')
1452 && TOLOWER (inchar ()) == L_('l')
1453 && inchar () == L_(')'))
1454 /* We must produce the value of a NULL pointer. A single
1455 '0' digit is enough. */
1459 /* The last read character is not part of the number
1467 /* The just read character is not part of the number anymore. */
1470 /* Convert the number. */
1472 if (need_longlong
&& (flags
& LONGDBL
))
1475 num
.q
= __strtoll_internal (wp
, &tw
, base
, flags
& GROUP
);
1477 num
.uq
= __strtoull_internal (wp
, &tw
, base
, flags
& GROUP
);
1482 num
.l
= __strtol_internal (wp
, &tw
, base
, flags
& GROUP
);
1484 num
.ul
= __strtoul_internal (wp
, &tw
, base
, flags
& GROUP
);
1489 if (!(flags
& SUPPRESS
))
1491 if (! number_signed
)
1493 if (need_longlong
&& (flags
& LONGDBL
))
1494 *ARG (unsigned LONGLONG
int *) = num
.uq
;
1495 else if (need_long
&& (flags
& LONG
))
1496 *ARG (unsigned long int *) = num
.ul
;
1497 else if (flags
& SHORT
)
1498 *ARG (unsigned short int *)
1499 = (unsigned short int) num
.ul
;
1500 else if (!(flags
& CHAR
))
1501 *ARG (unsigned int *) = (unsigned int) num
.ul
;
1503 *ARG (unsigned char *) = (unsigned char) num
.ul
;
1507 if (need_longlong
&& (flags
& LONGDBL
))
1508 *ARG (LONGLONG
int *) = num
.q
;
1509 else if (need_long
&& (flags
& LONG
))
1510 *ARG (long int *) = num
.l
;
1511 else if (flags
& SHORT
)
1512 *ARG (short int *) = (short int) num
.l
;
1513 else if (!(flags
& CHAR
))
1514 *ARG (int *) = (int) num
.l
;
1516 *ARG (signed char *) = (signed char) num
.ul
;
1522 case L_('e'): /* Floating-point numbers. */
1534 /* Check for a sign. */
1535 if (c
== L_('-') || c
== L_('+'))
1537 negative
= c
== L_('-');
1538 if (width
== 0 || inchar () == EOF
)
1539 /* EOF is only an input error before we read any chars. */
1541 if (! ISDIGIT (c
) && TOLOWER (c
) != L_('i')
1542 && TOLOWER (c
) != L_('n'))
1544 #ifdef COMPILE_WSCANF
1547 /* This is no valid number. */
1552 /* Match against the decimal point. At this point
1553 we are taking advantage of the fact that we can
1554 push more than one character back. This is
1555 (almost) never necessary since the decimal point
1556 string hopefully never contains more than one
1558 const char *cmpp
= decimal
;
1559 int avail
= width
> 0 ? width
: INT_MAX
;
1561 while ((unsigned char) *cmpp
== c
&& avail
> 0)
1562 if (*++cmpp
== '\0')
1566 if (inchar () == EOF
)
1573 /* This is no valid number. */
1577 if (cmpp
== decimal
)
1579 c
= (unsigned char) *--cmpp
;
1594 /* Take care for the special arguments "nan" and "inf". */
1595 if (TOLOWER (c
) == L_('n'))
1599 if (width
== 0 || inchar () == EOF
|| TOLOWER (c
) != L_('a'))
1604 if (width
== 0 || inchar () == EOF
|| TOLOWER (c
) != L_('n'))
1612 else if (TOLOWER (c
) == L_('i'))
1614 /* Maybe "inf" or "infinity". */
1616 if (width
== 0 || inchar () == EOF
|| TOLOWER (c
) != L_('n'))
1621 if (width
== 0 || inchar () == EOF
|| TOLOWER (c
) != L_('f'))
1626 /* It is as least "inf". */
1627 if (width
!= 0 && inchar () != EOF
)
1629 if (TOLOWER (c
) == L_('i'))
1633 /* Now we have to read the rest as well. */
1635 if (width
== 0 || inchar () == EOF
1636 || TOLOWER (c
) != L_('n'))
1641 if (width
== 0 || inchar () == EOF
1642 || TOLOWER (c
) != L_('i'))
1647 if (width
== 0 || inchar () == EOF
1648 || TOLOWER (c
) != L_('t'))
1653 if (width
== 0 || inchar () == EOF
1654 || TOLOWER (c
) != L_('y'))
1669 if (width
!= 0 && c
== L_('0'))
1675 if (width
!= 0 && TOLOWER (c
) == L_('x'))
1677 /* It is a number in hexadecimal format. */
1683 /* Grouping is not allowed. */
1691 got_dot
= got_e
= 0;
1696 else if (!got_e
&& is_hexa
&& ISXDIGIT (c
))
1698 else if (got_e
&& wp
[wpsize
- 1] == exp_char
1699 && (c
== L_('-') || c
== L_('+')))
1701 else if (wpsize
> 0 && !got_e
1702 && (CHAR_T
) TOLOWER (c
) == exp_char
)
1705 got_e
= got_dot
= 1;
1709 #ifdef COMPILE_WSCANF
1710 if (! got_dot
&& c
== decimal
)
1715 else if ((flags
& GROUP
) != 0 && thousands
!= L
'\0'
1716 && ! got_dot
&& c
== thousands
)
1720 /* The last read character is not part of the number
1726 const char *cmpp
= decimal
;
1727 int avail
= width
> 0 ? width
: INT_MAX
;
1731 while ((unsigned char) *cmpp
== c
&& avail
> 0)
1732 if (*++cmpp
== '\0')
1736 if (inchar () == EOF
)
1744 /* Add all the characters. */
1745 for (cmpp
= decimal
; *cmpp
!= '\0'; ++cmpp
)
1746 ADDW ((unsigned char) *cmpp
);
1753 /* Figure out whether it is a thousands separator.
1754 There is one problem: we possibly read more than
1755 one character. We cannot push them back but since
1756 we know that parts of the `decimal' string matched,
1757 we can compare against it. */
1758 const char *cmp2p
= thousands
;
1760 if ((flags
& GROUP
) != 0 && thousands
!= NULL
1763 while (cmp2p
- thousands
< cmpp
- decimal
1764 && *cmp2p
== decimal
[cmp2p
- thousands
])
1766 if (cmp2p
- thousands
== cmpp
- decimal
)
1768 while ((unsigned char) *cmp2p
== c
&& avail
> 0)
1769 if (*++cmp2p
== '\0')
1773 if (inchar () == EOF
)
1780 if (cmp2p
!= NULL
&& *cmp2p
== '\0')
1782 /* Add all the characters. */
1783 for (cmpp
= thousands
; *cmpp
!= '\0'; ++cmpp
)
1784 ADDW ((unsigned char) *cmpp
);
1790 /* The last read character is not part of the number
1801 while (width
!= 0 && inchar () != EOF
);
1803 /* Have we read any character? If we try to read a number
1804 in hexadecimal notation and we have read only the `0x'
1805 prefix or no exponent this is an error. */
1806 if (wpsize
== 0 || (is_hexa
&& (wpsize
== 2 || ! got_e
)))
1810 /* Convert the number. */
1812 if (flags
& LONGDBL
)
1814 long double d
= __strtold_internal (wp
, &tw
, flags
& GROUP
);
1815 if (!(flags
& SUPPRESS
) && tw
!= wp
)
1816 *ARG (long double *) = negative
? -d
: d
;
1818 else if (flags
& LONG
)
1820 double d
= __strtod_internal (wp
, &tw
, flags
& GROUP
);
1821 if (!(flags
& SUPPRESS
) && tw
!= wp
)
1822 *ARG (double *) = negative
? -d
: d
;
1826 float d
= __strtof_internal (wp
, &tw
, flags
& GROUP
);
1827 if (!(flags
& SUPPRESS
) && tw
!= wp
)
1828 *ARG (float *) = negative
? -d
: d
;
1834 if (!(flags
& SUPPRESS
))
1838 case L_('['): /* Character class. */
1840 STRING_ARG (wstr
, wchar_t);
1842 STRING_ARG (str
, char);
1853 /* There is no width given so there is also no limit on the
1854 number of characters we read. Therefore we set width to
1855 a very high value to make the algorithm easier. */
1858 #ifdef COMPILE_WSCANF
1859 /* Find the beginning and the end of the scanlist. We are not
1860 creating a lookup table since it would have to be too large.
1861 Instead we search each time through the string. This is not
1862 a constant lookup time but who uses this feature deserves to
1864 tw
= (wchar_t *) f
; /* Marks the beginning. */
1869 while ((fc
= *f
++) != L
'\0' && fc
!= L
']');
1873 wp
= (wchar_t *) f
- 1;
1875 /* Fill WP with byte flags indexed by character.
1876 We will use this flag map for matching input characters. */
1877 if (wpmax
< UCHAR_MAX
+ 1)
1879 wpmax
= UCHAR_MAX
+ 1;
1880 wp
= (char *) alloca (wpmax
);
1882 memset (wp
, '\0', UCHAR_MAX
+ 1);
1885 if (fc
== ']' || fc
== '-')
1887 /* If ] or - appears before any char in the set, it is not
1888 the terminator or separator, but the first char in the
1894 while ((fc
= *f
++) != '\0' && fc
!= ']')
1895 if (fc
== '-' && *f
!= '\0' && *f
!= ']'
1896 && (unsigned char) f
[-2] <= (unsigned char) *f
)
1898 /* Add all characters from the one before the '-'
1899 up to (but not including) the next format char. */
1900 for (fc
= (unsigned char) f
[-2]; fc
< (unsigned char) *f
; ++fc
)
1904 /* Add the character to the flag map. */
1913 size_t now
= read_in
;
1914 #ifdef COMPILE_WSCANF
1915 if (inchar () == WEOF
)
1922 /* Test whether it's in the scanlist. */
1926 if (runp
[0] == L
'-' && runp
[1] != '\0' && runp
+ 1 != wp
1928 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
1930 /* Match against all characters in between the
1931 first and last character of the sequence. */
1934 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
1935 if ((wint_t) wc
== c
)
1938 if (wc
<= runp
[1] && !not_in
)
1940 if (wc
<= runp
[1] && not_in
)
1942 /* The current character is not in the
1952 if ((wint_t) *runp
== c
&& !not_in
)
1954 if ((wint_t) *runp
== c
&& not_in
)
1964 if (runp
== wp
&& !not_in
)
1970 if (!(flags
& SUPPRESS
))
1974 if ((flags
& MALLOC
)
1975 && wstr
== (wchar_t *) *strptr
+ strsize
)
1977 /* Enlarge the buffer. */
1978 wstr
= (wchar_t *) realloc (*strptr
,
1980 * sizeof (wchar_t));
1983 /* Can't allocate that much. Last-ditch
1986 realloc (*strptr
, (strsize
+ 1)
1987 * sizeof (wchar_t));
1990 /* We lose. Oh well. Terminate the string
1991 and stop converting, so at least we don't
1993 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1999 *strptr
= (char *) wstr
;
2006 *strptr
= (char *) wstr
;
2013 while (--width
> 0 && inchar () != WEOF
);
2016 char buf
[MB_LEN_MAX
];
2020 if (inchar () == EOF
)
2023 memset (&cstate
, '\0', sizeof (cstate
));
2027 if (wp
[c
] == not_in
)
2029 ungetc_not_eof (c
, s
);
2034 if (!(flags
& SUPPRESS
))
2038 /* Convert it into a wide character. */
2040 n
= __mbrtowc (wstr
, buf
, 1, &cstate
);
2042 if (n
== (size_t) -2)
2044 /* Possibly correct character, just not enough
2047 assert (cnt
< MB_CUR_MAX
);
2053 if ((flags
& MALLOC
)
2054 && wstr
== (wchar_t *) *strptr
+ strsize
)
2056 /* Enlarge the buffer. */
2057 wstr
= (wchar_t *) realloc (*strptr
,
2059 * sizeof (wchar_t)));
2062 /* Can't allocate that much. Last-ditch
2065 realloc (*strptr
, ((strsize
+ 1)
2066 * sizeof (wchar_t)));
2069 /* We lose. Oh well. Terminate the
2070 string and stop converting,
2071 so at least we don't skip any input. */
2072 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2078 *strptr
= (char *) wstr
;
2085 *strptr
= (char *) wstr
;
2095 while (inchar () != EOF
);
2098 /* We stopped in the middle of recognizing another
2099 character. That's a problem. */
2104 /* We haven't succesfully read any character. */
2107 if (!(flags
& SUPPRESS
))
2111 if ((flags
& MALLOC
)
2112 && wstr
- (wchar_t *) *strptr
!= strsize
)
2114 wchar_t *cp
= (wchar_t *)
2115 realloc (*strptr
, ((wstr
- (wchar_t *) *strptr
)
2116 * sizeof(wchar_t)));
2118 *strptr
= (char *) cp
;
2126 size_t now
= read_in
;
2128 if (inchar () == EOF
)
2131 #ifdef COMPILE_WSCANF
2133 memset (&state
, '\0', sizeof (state
));
2140 /* Test whether it's in the scanlist. */
2144 if (runp
[0] == L
'-' && runp
[1] != '\0' && runp
+ 1 != wp
2146 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2148 /* Match against all characters in between the
2149 first and last character of the sequence. */
2152 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2153 if ((wint_t) wc
== c
)
2156 if (wc
<= runp
[1] && !not_in
)
2158 if (wc
<= runp
[1] && not_in
)
2160 /* The current character is not in the
2170 if ((wint_t) *runp
== c
&& !not_in
)
2172 if ((wint_t) *runp
== c
&& not_in
)
2182 if (runp
== wp
&& !not_in
)
2188 if (!(flags
& SUPPRESS
))
2190 if ((flags
& MALLOC
)
2191 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
2193 /* Enlarge the buffer. */
2194 size_t strleng
= str
- *strptr
;
2197 newstr
= (char *) realloc (*strptr
, 2 * strsize
);
2200 /* Can't allocate that much. Last-ditch
2202 newstr
= (char *) realloc (*strptr
,
2203 strleng
+ MB_CUR_MAX
);
2206 /* We lose. Oh well. Terminate the string
2207 and stop converting, so at least we don't
2209 ((char *) (*strptr
))[strleng
] = '\0';
2216 str
= newstr
+ strleng
;
2217 strsize
= strleng
+ MB_CUR_MAX
;
2223 str
= newstr
+ strleng
;
2229 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
2230 if (n
== (size_t) -1)
2233 assert (n
<= MB_CUR_MAX
);
2236 while (--width
> 0 && inchar () != WEOF
);
2241 if (wp
[c
] == not_in
)
2243 ungetc_not_eof (c
, s
);
2248 if (!(flags
& SUPPRESS
))
2251 if ((flags
& MALLOC
)
2252 && (char *) str
== *strptr
+ strsize
)
2254 /* Enlarge the buffer. */
2255 size_t newsize
= 2 * strsize
;
2258 str
= (char *) realloc (*strptr
, newsize
);
2261 /* Can't allocate that much. Last-ditch
2263 if (newsize
> strsize
+ 1)
2265 newsize
= strsize
+ 1;
2268 /* We lose. Oh well. Terminate the
2269 string and stop converting,
2270 so at least we don't skip any input. */
2271 ((char *) (*strptr
))[strsize
- 1] = '\0';
2277 *strptr
= (char *) str
;
2284 while (--width
> 0 && inchar () != EOF
);
2288 /* We haven't succesfully read any character. */
2291 if (!(flags
& SUPPRESS
))
2293 #ifdef COMPILE_WSCANF
2294 /* We have to emit the code to get into the initial
2296 char buf
[MB_LEN_MAX
];
2297 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
2298 if (n
> 0 && (flags
& MALLOC
)
2299 && str
+ n
>= *strptr
+ strsize
)
2301 /* Enlarge the buffer. */
2302 size_t strleng
= str
- *strptr
;
2305 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
2308 /* We lose. Oh well. Terminate the string
2309 and stop converting, so at least we don't
2311 ((char *) (*strptr
))[strleng
] = '\0';
2318 str
= newstr
+ strleng
;
2319 strsize
= strleng
+ n
+ 1;
2323 str
= __mempcpy (str
, buf
, n
);
2327 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
2329 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
2339 case L_('p'): /* Generic pointer. */
2341 /* A PTR must be the same size as a `long int'. */
2342 flags
&= ~(SHORT
|LONGDBL
);
2350 /* If this is an unknown format character punt. */
2355 /* The last thing we saw int the format string was a white space.
2356 Consume the last white spaces. */
2361 while (ISSPACE (c
));
2366 /* Unlock stream. */
2375 #ifdef COMPILE_WSCANF
2377 __vfwscanf (FILE *s
, const wchar_t *format
, va_list argptr
)
2379 return _IO_vfwscanf (s
, format
, argptr
, NULL
);
2383 __vfscanf (FILE *s
, const char *format
, va_list argptr
)
2385 return INTUSE(_IO_vfscanf
) (s
, format
, argptr
, NULL
);
2387 libc_hidden_def (__vfscanf
)
2390 #ifdef COMPILE_WSCANF
2391 weak_alias (__vfwscanf
, vfwscanf
)
2393 weak_alias (__vfscanf
, vfscanf
)