1 /* Copyright (C) 1991-1999, 2000, 2001 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 */
73 # define va_list _IO_va_list
75 # ifdef COMPILE_WSCANF
76 # define ungetc(c, s) ((void) (c == WEOF \
78 _IO_sputbackwc (s, c))))
79 # define ungetc_not_eof(c, s) ((void) (--read_in, \
80 _IO_sputbackwc (s, c)))
81 # define inchar() (c == WEOF ? WEOF \
82 : ((c = _IO_getwc_unlocked (s)), \
83 (void) (c != WEOF && ++read_in), c))
85 # define MEMCPY(d, s, n) __wmemcpy (d, s, n)
86 # define ISSPACE(Ch) iswspace (Ch)
87 # define ISDIGIT(Ch) iswdigit (Ch)
88 # define ISXDIGIT(Ch) iswxdigit (Ch)
89 # define TOLOWER(Ch) towlower (Ch)
90 # define ORIENT if (_IO_fwide (s, 1) != 1) return WEOF
91 # define __strtoll_internal __wcstoll_internal
92 # define __strtoull_internal __wcstoull_internal
93 # define __strtol_internal __wcstol_internal
94 # define __strtoul_internal __wcstoul_internal
95 # define __strtold_internal __wcstold_internal
96 # define __strtod_internal __wcstod_internal
97 # define __strtof_internal __wcstof_internal
99 # define L_(Str) L##Str
100 # define CHAR_T wchar_t
101 # define UCHAR_T unsigned int
102 # define WINT_T wint_t
106 # define ungetc(c, s) ((void) ((int) c == EOF \
108 _IO_sputbackc (s, (unsigned char) c))))
109 # define ungetc_not_eof(c, s) ((void) (--read_in, \
110 _IO_sputbackc (s, (unsigned char) c)))
111 # define inchar() (c == EOF ? EOF \
112 : ((c = _IO_getc_unlocked (s)), \
113 (void) (c != EOF && ++read_in), c))
114 # define MEMCPY(d, s, n) memcpy (d, s, n)
115 # define ISSPACE(Ch) isspace (Ch)
116 # define ISDIGIT(Ch) isdigit (Ch)
117 # define ISXDIGIT(Ch) isxdigit (Ch)
118 # define TOLOWER(Ch) tolower (Ch)
119 # define ORIENT if (s->_vtable_offset == 0 \
120 && _IO_fwide (s, -1) != -1) \
125 # define UCHAR_T unsigned char
129 # define encode_error() do { \
130 if (errp != NULL) *errp |= 4; \
131 _IO_funlockfile (s); \
132 __libc_cleanup_end (0); \
133 __set_errno (EILSEQ); \
136 # define conv_error() do { \
137 if (errp != NULL) *errp |= 2; \
138 _IO_funlockfile (s); \
139 __libc_cleanup_end (0); \
142 # define input_error() do { \
143 _IO_funlockfile (s); \
144 if (errp != NULL) *errp |= 1; \
145 __libc_cleanup_end (0); \
146 return done ?: EOF; \
148 # define memory_error() do { \
149 _IO_funlockfile (s); \
150 __set_errno (ENOMEM); \
151 __libc_cleanup_end (0); \
154 # define ARGCHECK(s, format) \
157 /* Check file argument for consistence. */ \
158 CHECK_FILE (s, EOF); \
159 if (s->_flags & _IO_NO_READS) \
161 __set_errno (EBADF); \
164 else if (format == NULL) \
170 # define LOCK_STREAM(S) \
171 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, (S)); \
173 # define UNLOCK_STREAM(S) \
174 _IO_funlockfile (S); \
175 __libc_cleanup_region_end (0)
177 # define ungetc(c, s) ((void) (c != EOF && --read_in), ungetc (c, s))
178 # define ungetc_not_eof(c, s) (--read_in, (ungetc) (c, s))
179 # define inchar() (c == EOF ? EOF \
180 : ((c = getc (s)), (void) (c != EOF && ++read_in), c))
181 # define MEMCPY(d, s, n) memcpy (d, s, n)
182 # define ISSPACE(Ch) isspace (Ch)
183 # define ISDIGIT(Ch) isdigit (Ch)
184 # define ISXDIGIT(Ch) isxdigit (Ch)
185 # define TOLOWER(Ch) tolower (Ch)
189 # define UCHAR_T unsigned char
192 # define encode_error() do { \
194 __set_errno (EILSEQ); \
197 # define conv_error() do { \
201 # define input_error() do { \
203 return done ?: EOF; \
205 # define memory_error() do { \
207 __set_errno (ENOMEM); \
210 # define ARGCHECK(s, format) \
213 /* Check file argument for consistence. */ \
214 if (!__validfp (s) || !s->__mode.__read) \
216 __set_errno (EBADF); \
219 else if (format == NULL) \
221 __set_errno (EINVAL); \
226 /* XXX For now !!! */
227 # define flockfile(S) /* nothing */
228 # define funlockfile(S) /* nothing */
229 # define LOCK_STREAM(S)
230 # define UNLOCK_STREAM(S)
232 # define LOCK_STREAM(S) \
233 __libc_cleanup_region_start (&__funlockfile, (S)); \
235 # define UNLOCK_STREAM(S) \
237 __libc_cleanup_region_end (0)
242 /* Read formatted input from S according to the format string
243 FORMAT, using the argument list in ARG.
244 Return the number of assignments made, or -1 for an input error. */
246 # ifdef COMPILE_WSCANF
248 _IO_vfwscanf (s
, format
, argptr
, errp
)
250 const wchar_t *format
;
255 _IO_vfscanf (s
, format
, argptr
, errp
)
263 __vfscanf (FILE *s
, const char *format
, va_list argptr
)
267 register const CHAR_T
*f
= format
;
268 register UCHAR_T fc
; /* Current character of the format. */
269 register WINT_T done
= 0; /* Assignments done. */
270 register size_t read_in
= 0; /* Chars read in. */
271 register WINT_T c
= 0; /* Last char read. */
272 register int width
; /* Maximum field width. */
273 register int flags
; /* Modifiers for current format element. */
275 /* Status for reading F-P nums. */
276 char got_dot
, got_e
, negative
;
277 /* If a [...] is a [^...]. */
279 #define exp_char not_in
280 /* Base for integral numbers. */
282 /* Signedness for integral numbers. */
284 #define is_hexa number_signed
285 /* Decimal point character. */
286 #ifdef COMPILE_WSCANF
291 /* The thousands character of the current locale. */
292 #ifdef COMPILE_WSCANF
295 const char *thousands
;
297 /* State for the conversions. */
299 /* Integral holding variables. */
303 unsigned long long int uq
;
305 unsigned long int ul
;
307 /* Character-buffer pointer. */
309 wchar_t *wstr
= NULL
;
310 char **strptr
= NULL
;
312 /* We must not react on white spaces immediately because they can
313 possibly be matched even if in the input stream no character is
314 available anymore. */
316 /* Nonzero if we are reading a pointer. */
319 CHAR_T
*tw
; /* Temporary pointer. */
320 CHAR_T
*wp
= NULL
; /* Workspace. */
321 size_t wpmax
= 0; /* Maximal size of workspace. */
322 size_t wpsize
; /* Currently used bytes in workspace. */
326 if (wpsize == wpmax) \
329 wpmax = (UCHAR_MAX + 1 > 2 * wpmax ? UCHAR_MAX + 1 : 2 * wpmax); \
330 wp = (CHAR_T *) alloca (wpmax * sizeof (wchar_t)); \
332 MEMCPY (wp, old, wpsize); \
334 wp[wpsize++] = (Ch); \
339 __va_copy (arg
, argptr
);
341 arg
= (va_list) argptr
;
348 ARGCHECK (s
, format
);
350 /* Figure out the decimal point character. */
351 #ifdef COMPILE_WSCANF
352 decimal
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
354 decimal
= _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
356 /* Figure out the thousands separator character. */
357 #ifdef COMPILE_WSCANF
358 thousands
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_THOUSANDS_SEP_WC
);
360 thousands
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
361 if (*thousands
== '\0')
365 /* Lock the stream. */
369 #ifndef COMPILE_WSCANF
370 /* From now on we use `state' to convert the format string. */
371 memset (&state
, '\0', sizeof (state
));
374 /* Run through the format string. */
378 /* Extract the next argument, which is of type TYPE.
379 For a %N$... spec, this is the Nth argument from the beginning;
380 otherwise it is the next argument after the state now in ARG. */
382 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
383 ({ unsigned int pos = argpos; \
385 __va_copy (arg, argptr); \
387 (void) va_arg (arg, void *); \
388 va_arg (arg, type); \
392 /* XXX Possible optimization. */
393 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
394 ({ va_list arg = (va_list) argptr; \
395 arg = (va_list) ((char *) arg \
397 * __va_rounded_size (void *)); \
398 va_arg (arg, type); \
401 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
402 ({ unsigned int pos = argpos; \
403 va_list arg = (va_list) argptr; \
405 (void) va_arg (arg, void *); \
406 va_arg (arg, type); \
411 #ifndef COMPILE_WSCANF
412 if (!isascii ((unsigned char) *f
))
414 /* Non-ASCII, may be a multibyte. */
415 int len
= __mbrlen (f
, strlen (f
), &state
);
423 else if (c
!= (unsigned char) *f
++)
425 ungetc_not_eof (c
, s
);
438 /* Remember to skip spaces. */
445 /* Read a character. */
448 /* Characters other than format specs must just match. */
452 /* We saw white space char as the last character in the format
453 string. Now it's time to skip all leading white space. */
457 if (inchar () == EOF
&& errno
== EINTR
)
471 /* This is the start of the conversion string. */
474 /* Not yet decided whether we read a pointer or not. */
477 /* Initialize state of modifiers. */
480 /* Prepare temporary buffer. */
483 /* Check for a positional parameter specification. */
484 if (ISDIGIT ((UCHAR_T
) *f
))
486 argpos
= (UCHAR_T
) *f
++ - L_('0');
487 while (ISDIGIT ((UCHAR_T
) *f
))
488 argpos
= argpos
* 10 + ((UCHAR_T
) *f
++ - L_('0'));
493 /* Oops; that was actually the field width. */
501 /* Check for the assignment-suppressing, the number grouping flag,
502 and the signal to use the locale's digit representation. */
503 while (*f
== L_('*') || *f
== L_('\'') || *f
== L_('I'))
517 /* We have seen width. */
518 if (ISDIGIT ((UCHAR_T
) *f
))
521 /* Find the maximum field width. */
523 while (ISDIGIT ((UCHAR_T
) *f
))
526 width
+= (UCHAR_T
) *f
++ - L_('0');
532 /* Check for type modifiers. */
536 /* ints are short ints or chars. */
548 /* A double `l' is equivalent to an `L'. */
550 flags
|= LONGDBL
| LONG
;
553 /* ints are long ints. */
558 /* doubles are long doubles, and ints are long long ints. */
559 flags
|= LONGDBL
| LONG
;
562 /* The `a' is used as a flag only if followed by `s', `S' or
564 if (*f
!= L_('s') && *f
!= L_('S') && *f
!= L_('['))
569 /* String conversions (%s, %[) take a `char **'
570 arg and fill it in with a malloc'd pointer. */
574 if (need_longlong
&& sizeof (size_t) > sizeof (unsigned long int))
576 else if (sizeof (size_t) > sizeof (unsigned int))
580 if (need_longlong
&& sizeof (uintmax_t) > sizeof (unsigned long int))
582 else if (sizeof (uintmax_t) > sizeof (unsigned int))
586 if (need_longlong
&& sizeof (ptrdiff_t) > sizeof (long int))
588 else if (sizeof (ptrdiff_t) > sizeof (int))
592 /* Not a recognized modifier. Backup. */
597 /* End of the format string? */
601 /* Find the conversion specifier. */
603 if (skip_space
|| (fc
!= L_('[') && fc
!= L_('c')
604 && fc
!= L_('C') && fc
!= L_('n')))
606 /* Eat whitespace. */
607 int save_errno
= errno
;
610 if (inchar () == EOF
&& errno
== EINTR
)
620 case L_('%'): /* Must match a literal '%'. */
626 ungetc_not_eof (c
, s
);
631 case L_('n'): /* Answer number of assignments done. */
632 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
633 with the 'n' conversion specifier. */
634 if (!(flags
& SUPPRESS
))
636 /* Don't count the read-ahead. */
637 if (need_longlong
&& (flags
& LONGDBL
))
638 *ARG (long long int *) = read_in
;
639 else if (need_long
&& (flags
& LONG
))
640 *ARG (long int *) = read_in
;
641 else if (flags
& SHORT
)
642 *ARG (short int *) = read_in
;
643 else if (!(flags
& CHAR
))
644 *ARG (int *) = read_in
;
646 *ARG (char *) = read_in
;
648 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
649 /* We have a severe problem here. The ISO C standard
650 contradicts itself in explaining the effect of the %n
651 format in `scanf'. While in ISO C:1990 and the ISO C
652 Amendement 1:1995 the result is described as
654 Execution of a %n directive does not effect the
655 assignment count returned at the completion of
656 execution of the f(w)scanf function.
658 in ISO C Corrigendum 1:1994 the following was added:
661 Add the following fourth example:
664 int d1, d2, n1, n2, i;
665 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
666 the value 123 is assigned to d1 and the value3 to n1.
667 Because %n can never get an input failure the value
668 of 3 is also assigned to n2. The value of d2 is not
669 affected. The value 3 is assigned to i.
671 We go for now with the historically correct code from ISO C,
672 i.e., we don't count the %n assignments. When it ever
673 should proof to be wrong just remove the #ifdef above. */
679 case L_('c'): /* Match characters. */
680 if ((flags
& LONG
) == 0)
682 if (!(flags
& SUPPRESS
))
696 #ifdef COMPILE_WSCANF
697 /* We have to convert the wide character(s) into multibyte
698 characters and store the result. */
699 memset (&state
, '\0', sizeof (state
));
705 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
706 if (n
== (size_t) -1)
707 /* No valid wide character. */
710 /* Increment the output pointer. Even if we don't
714 while (--width
> 0 && inchar () != EOF
);
716 if (!(flags
& SUPPRESS
))
720 while (--width
> 0 && inchar () != EOF
);
723 while (--width
> 0 && inchar () != EOF
);
726 if (!(flags
& SUPPRESS
))
733 if (!(flags
& SUPPRESS
))
735 wstr
= ARG (wchar_t *);
744 #ifdef COMPILE_WSCANF
745 /* Just store the incoming wide characters. */
746 if (!(flags
& SUPPRESS
))
750 while (--width
> 0 && inchar () != EOF
);
753 while (--width
> 0 && inchar () != EOF
);
756 /* We have to convert the multibyte input sequence to wide
761 memset (&cstate
, '\0', sizeof (cstate
));
765 /* This is what we present the mbrtowc function first. */
772 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
775 if (n
== (size_t) -2)
777 /* Possibly correct character, just not enough
779 if (inchar () == EOF
)
789 /* We have a match. */
793 /* Advance the result pointer. */
796 while (--width
> 0 && inchar () != EOF
);
800 if (!(flags
& SUPPRESS
))
805 case L_('s'): /* Read a string. */
808 #define STRING_ARG(Str, Type) \
809 do if (!(flags & SUPPRESS)) \
811 if (flags & MALLOC) \
813 /* The string is to be stored in a malloc'd buffer. */ \
814 strptr = ARG (char **); \
815 if (strptr == NULL) \
817 /* Allocate an initial buffer. */ \
819 *strptr = (char *) malloc (strsize * sizeof (Type)); \
820 Str = (Type *) *strptr; \
823 Str = ARG (Type *); \
827 STRING_ARG (str
, char);
833 #ifdef COMPILE_WSCANF
834 memset (&state
, '\0', sizeof (state
));
841 ungetc_not_eof (c
, s
);
845 #ifdef COMPILE_WSCANF
846 /* This is quite complicated. We have to convert the
847 wide characters into multibyte characters and then
852 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
853 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
855 /* We have to enlarge the buffer if the `a' flag
857 size_t strleng
= str
- *strptr
;
860 newstr
= (char *) realloc (*strptr
, strsize
* 2);
863 /* Can't allocate that much. Last-ditch
865 newstr
= (char *) realloc (*strptr
,
866 strleng
+ MB_CUR_MAX
);
869 /* We lose. Oh well. Terminate the
870 string and stop converting,
871 so at least we don't skip any input. */
872 ((char *) (*strptr
))[strleng
] = '\0';
879 str
= newstr
+ strleng
;
880 strsize
= strleng
+ MB_CUR_MAX
;
886 str
= newstr
+ strleng
;
891 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
,
893 if (n
== (size_t) -1)
896 assert (n
<= MB_CUR_MAX
);
901 if (!(flags
& SUPPRESS
))
905 && (char *) str
== *strptr
+ strsize
)
907 /* Enlarge the buffer. */
908 str
= (char *) realloc (*strptr
, 2 * strsize
);
911 /* Can't allocate that much. Last-ditch
913 str
= (char *) realloc (*strptr
, strsize
+ 1);
916 /* We lose. Oh well. Terminate the
917 string and stop converting,
918 so at least we don't skip any input. */
919 ((char *) (*strptr
))[strsize
- 1] = '\0';
925 *strptr
= (char *) str
;
932 *strptr
= (char *) str
;
940 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
942 if (!(flags
& SUPPRESS
))
944 #ifdef COMPILE_WSCANF
945 /* We have to emit the code to get into the initial
947 char buf
[MB_LEN_MAX
];
948 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
949 if (n
> 0 && (flags
& MALLOC
)
950 && str
+ n
>= *strptr
+ strsize
)
952 /* Enlarge the buffer. */
953 size_t strleng
= str
- *strptr
;
956 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
959 /* We lose. Oh well. Terminate the string
960 and stop converting, so at least we don't
962 ((char *) (*strptr
))[strleng
] = '\0';
969 str
= newstr
+ strleng
;
970 strsize
= strleng
+ n
+ 1;
974 str
= __mempcpy (str
, buf
, n
);
978 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
980 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
993 #ifndef COMPILE_WSCANF
997 /* Wide character string. */
998 STRING_ARG (wstr
, wchar_t);
1004 #ifndef COMPILE_WSCANF
1005 memset (&cstate
, '\0', sizeof (cstate
));
1012 ungetc_not_eof (c
, s
);
1016 #ifdef COMPILE_WSCANF
1018 if (!(flags
& SUPPRESS
))
1021 if ((flags
& MALLOC
)
1022 && wstr
== (wchar_t *) *strptr
+ strsize
)
1024 /* Enlarge the buffer. */
1025 wstr
= (wchar_t *) realloc (*strptr
,
1027 * sizeof (wchar_t));
1030 /* Can't allocate that much. Last-ditch
1032 wstr
= (wchar_t *) realloc (*strptr
,
1034 * sizeof (wchar_t));
1037 /* We lose. Oh well. Terminate the string
1038 and stop converting, so at least we don't
1040 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1046 *strptr
= (char *) wstr
;
1053 *strptr
= (char *) wstr
;
1069 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
1072 if (n
== (size_t) -2)
1074 /* Possibly correct character, just not enough
1076 if (inchar () == EOF
)
1086 /* We have a match. */
1091 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
1092 && wstr
== (wchar_t *) *strptr
+ strsize
)
1094 /* Enlarge the buffer. */
1095 wstr
= (wchar_t *) realloc (*strptr
,
1097 * sizeof (wchar_t)));
1100 /* Can't allocate that much. Last-ditch effort. */
1101 wstr
= (wchar_t *) realloc (*strptr
,
1103 * sizeof (wchar_t)));
1106 /* We lose. Oh well. Terminate the
1107 string and stop converting, so at
1108 least we don't skip any input. */
1109 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1115 *strptr
= (char *) wstr
;
1122 *strptr
= (char *) wstr
;
1130 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
1132 if (!(flags
& SUPPRESS
))
1136 if ((flags
& MALLOC
) && wstr
- (wchar_t *) *strptr
!= strsize
)
1138 wchar_t *cp
= (wchar_t *) realloc (*strptr
,
1140 - (wchar_t *) *strptr
)
1141 * sizeof(wchar_t)));
1143 *strptr
= (char *) cp
;
1151 case L_('x'): /* Hexadecimal integer. */
1152 case L_('X'): /* Ditto. */
1157 case L_('o'): /* Octal integer. */
1162 case L_('u'): /* Unsigned decimal integer. */
1167 case L_('d'): /* Signed decimal integer. */
1172 case L_('i'): /* Generic number. */
1181 /* Check for a sign. */
1182 if (c
== L_('-') || c
== L_('+'))
1190 /* Look for a leading indication of base. */
1191 if (width
!= 0 && c
== L_('0'))
1199 if (width
!= 0 && TOLOWER (c
) == L_('x'))
1217 if (base
== 10 && (flags
& I18N
) != 0)
1222 #ifdef COMPILE_WSCANF
1223 const wchar_t *wcdigits
[10];
1225 const char *mbdigits
[10];
1230 #ifdef COMPILE_WSCANF
1231 to_level
= _NL_CURRENT_WORD (LC_CTYPE
,
1232 _NL_CTYPE_INDIGITS_WC_LEN
) - 1;
1234 to_level
= _NL_CURRENT_WORD (LC_CTYPE
,
1235 _NL_CTYPE_INDIGITS_MB_LEN
) - 1;
1238 /* Read the number into workspace. */
1239 while (c
!= EOF
&& width
!= 0)
1241 /* In this round we get the pointer to the digit strings
1242 and also perform the first round of comparisons. */
1243 for (n
= 0; n
< 10; ++n
)
1245 /* Get the string for the digits with value N. */
1246 #ifdef COMPILE_WSCANF
1247 wcdigits
[n
] = (const wchar_t *)
1248 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1249 wcdigits
[n
] += from_level
;
1251 if (c
== *wcdigits
[n
])
1253 to_level
= from_level
;
1257 /* Advance the pointer to the next string. */
1261 int avail
= width
> 0 ? width
: INT_MAX
;
1263 mbdigits
[n
] = _NL_CURRENT (LC_CTYPE
,
1264 _NL_CTYPE_INDIGITS0_MB
+ n
);
1266 for (level
= 0; level
< from_level
; level
++)
1267 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1270 while ((unsigned char) *cmpp
== c
&& avail
> 0)
1272 if (*++cmpp
== '\0')
1276 if ((c
= inchar ()) == EOF
)
1286 to_level
= from_level
;
1290 /* We are pushing all read characters back. */
1291 if (cmpp
> mbdigits
[n
])
1294 while (--cmpp
> mbdigits
[n
])
1295 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1296 c
= (unsigned char) *cmpp
;
1299 /* Advance the pointer to the next string. */
1300 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1306 /* Have not yet found the digit. */
1307 for (level
= from_level
+ 1; level
<= to_level
; ++level
)
1309 /* Search all ten digits of this level. */
1310 for (n
= 0; n
< 10; ++n
)
1312 #ifdef COMPILE_WSCANF
1313 if (c
== *wcdigits
[n
])
1316 /* Advance the pointer to the next string. */
1320 int avail
= width
> 0 ? width
: INT_MAX
;
1323 while ((unsigned char) *cmpp
== c
&& avail
> 0)
1325 if (*++cmpp
== '\0')
1329 if ((c
= inchar ()) == EOF
)
1342 /* We are pushing all read characters back. */
1343 if (cmpp
> mbdigits
[n
])
1346 while (--cmpp
> mbdigits
[n
])
1347 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1348 c
= (unsigned char) *cmpp
;
1351 /* Advance the pointer to the next string. */
1352 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1368 else if ((flags
& GROUP
)
1369 #ifdef COMPILE_WSCANF
1370 && thousands
!= L
'\0'
1372 && thousands
!= NULL
1376 /* Try matching against the thousands separator. */
1377 #ifdef COMPILE_WSCANF
1381 const char *cmpp
= thousands
;
1382 int avail
= width
> 0 ? width
: INT_MAX
;
1384 while ((unsigned char) *cmpp
== c
&& avail
> 0)
1387 if (*++cmpp
== '\0')
1391 if ((c
= inchar ()) == EOF
)
1399 /* We are pushing all read characters back. */
1400 if (cmpp
> thousands
)
1402 wpsize
-= cmpp
- thousands
;
1404 while (--cmpp
> thousands
)
1405 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1406 c
= (unsigned char) *cmpp
;
1414 /* The last thousands character will be added back by
1430 /* Read the number into workspace. */
1431 while (c
!= EOF
&& width
!= 0)
1438 else if (!ISDIGIT (c
) || c
- L_('0') >= base
)
1440 if (base
== 10 && (flags
& GROUP
)
1441 #ifdef COMPILE_WSCANF
1442 && thousands
!= L
'\0'
1444 && thousands
!= NULL
1448 /* Try matching against the thousands separator. */
1449 #ifdef COMPILE_WSCANF
1453 const char *cmpp
= thousands
;
1454 int avail
= width
> 0 ? width
: INT_MAX
;
1456 while ((unsigned char) *cmpp
== c
&& avail
> 0)
1459 if (*++cmpp
== '\0')
1463 if ((c
= inchar ()) == EOF
)
1471 /* We are pushing all read characters back. */
1472 if (cmpp
> thousands
)
1474 wpsize
-= cmpp
- thousands
;
1476 while (--cmpp
> thousands
)
1477 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1478 c
= (unsigned char) *cmpp
;
1486 /* The last thousands character will be added back by
1502 || (wpsize
== 1 && (wp
[0] == L_('+') || wp
[0] == L_('-'))))
1504 /* There was no number. If we are supposed to read a pointer
1505 we must recognize "(nil)" as well. */
1506 if (wpsize
== 0 && read_pointer
&& (width
< 0 || width
>= 0)
1508 && TOLOWER (inchar ()) == L_('n')
1509 && TOLOWER (inchar ()) == L_('i')
1510 && TOLOWER (inchar ()) == L_('l')
1511 && inchar () == L_(')'))
1512 /* We must produce the value of a NULL pointer. A single
1513 '0' digit is enough. */
1517 /* The last read character is not part of the number
1525 /* The just read character is not part of the number anymore. */
1528 /* Convert the number. */
1530 if (need_longlong
&& (flags
& LONGDBL
))
1533 num
.q
= __strtoll_internal (wp
, &tw
, base
, flags
& GROUP
);
1535 num
.uq
= __strtoull_internal (wp
, &tw
, base
, flags
& GROUP
);
1540 num
.l
= __strtol_internal (wp
, &tw
, base
, flags
& GROUP
);
1542 num
.ul
= __strtoul_internal (wp
, &tw
, base
, flags
& GROUP
);
1547 if (!(flags
& SUPPRESS
))
1549 if (! number_signed
)
1551 if (need_longlong
&& (flags
& LONGDBL
))
1552 *ARG (unsigned LONGLONG
int *) = num
.uq
;
1553 else if (need_long
&& (flags
& LONG
))
1554 *ARG (unsigned long int *) = num
.ul
;
1555 else if (flags
& SHORT
)
1556 *ARG (unsigned short int *)
1557 = (unsigned short int) num
.ul
;
1558 else if (!(flags
& CHAR
))
1559 *ARG (unsigned int *) = (unsigned int) num
.ul
;
1561 *ARG (unsigned char *) = (unsigned char) num
.ul
;
1565 if (need_longlong
&& (flags
& LONGDBL
))
1566 *ARG (LONGLONG
int *) = num
.q
;
1567 else if (need_long
&& (flags
& LONG
))
1568 *ARG (long int *) = num
.l
;
1569 else if (flags
& SHORT
)
1570 *ARG (short int *) = (short int) num
.l
;
1571 else if (!(flags
& CHAR
))
1572 *ARG (int *) = (int) num
.l
;
1574 *ARG (signed char *) = (signed char) num
.ul
;
1580 case L_('e'): /* Floating-point numbers. */
1592 /* Check for a sign. */
1593 if (c
== L_('-') || c
== L_('+'))
1595 negative
= c
== L_('-');
1596 if (width
== 0 || inchar () == EOF
)
1597 /* EOF is only an input error before we read any chars. */
1599 if (! ISDIGIT (c
) && TOLOWER (c
) != L_('i'))
1601 #ifdef COMPILE_WSCANF
1604 /* This is no valid number. */
1609 /* Match against the decimal point. At this point
1610 we are taking advantage of the fact that we can
1611 push more than one character back. This is
1612 (almost) never necessary since the decimal point
1613 string hopefully never contains more than one
1615 const char *cmpp
= decimal
;
1616 int avail
= width
> 0 ? width
: INT_MAX
;
1618 while ((unsigned char) *cmpp
== c
&& avail
> 0)
1619 if (*++cmpp
== '\0')
1623 if (inchar () == EOF
)
1630 /* This is no valid number. */
1634 if (cmpp
== decimal
)
1636 c
= (unsigned char) *--cmpp
;
1651 /* Take care for the special arguments "nan" and "inf". */
1652 if (TOLOWER (c
) == L_('n'))
1656 if (width
== 0 || inchar () == EOF
|| TOLOWER (c
) != L_('a'))
1661 if (width
== 0 || inchar () == EOF
|| TOLOWER (c
) != L_('n'))
1669 else if (TOLOWER (c
) == L_('i'))
1671 /* Maybe "inf" or "infinity". */
1673 if (width
== 0 || inchar () == EOF
|| TOLOWER (c
) != L_('n'))
1678 if (width
== 0 || inchar () == EOF
|| TOLOWER (c
) != L_('f'))
1683 /* It is as least "inf". */
1684 if (width
!= 0 && inchar () != EOF
)
1686 if (TOLOWER (c
) == L_('i'))
1690 /* Now we have to read the rest as well. */
1692 if (width
== 0 || inchar () == EOF
1693 || TOLOWER (c
) != L_('n'))
1698 if (width
== 0 || inchar () == EOF
1699 || TOLOWER (c
) != L_('i'))
1704 if (width
== 0 || inchar () == EOF
1705 || TOLOWER (c
) != L_('t'))
1710 if (width
== 0 || inchar () == EOF
1711 || TOLOWER (c
) != L_('y'))
1726 if (width
!= 0 && c
== L_('0'))
1732 if (width
!= 0 && TOLOWER (c
) == L_('x'))
1734 /* It is a number in hexadecimal format. */
1740 /* Grouping is not allowed. */
1748 got_dot
= got_e
= 0;
1753 else if (!got_e
&& is_hexa
&& ISXDIGIT (c
))
1755 else if (got_e
&& wp
[wpsize
- 1] == exp_char
1756 && (c
== L_('-') || c
== L_('+')))
1758 else if (wpsize
> 0 && !got_e
&& TOLOWER (c
) == exp_char
)
1761 got_e
= got_dot
= 1;
1765 #ifdef COMPILE_WSCANF
1766 if (! got_dot
&& c
== decimal
)
1771 else if (thousands
!= L
'\0' && ! got_dot
&& c
== thousands
)
1775 /* The last read character is not part of the number
1781 const char *cmpp
= decimal
;
1782 int avail
= width
> 0 ? width
: INT_MAX
;
1786 while ((unsigned char) *cmpp
== c
&& avail
> 0)
1787 if (*++cmpp
== '\0')
1791 if (inchar () == EOF
)
1799 /* Add all the characters. */
1800 for (cmpp
= decimal
; *cmpp
!= '\0'; ++cmpp
)
1801 ADDW ((unsigned char) *cmpp
);
1808 /* Figure out whether it is a thousands separator.
1809 There is one problem: we possibly read more than
1810 one character. We cannot push them back but since
1811 we know that parts of the `decimal' string matched,
1812 we can compare against it. */
1813 const char *cmp2p
= thousands
;
1815 if (thousands
!= NULL
&& ! got_dot
)
1818 && *cmp2p
== decimal
[cmp2p
- thousands
])
1822 while ((unsigned char) *cmp2p
== c
&& avail
> 0)
1823 if (*++cmp2p
== '\0')
1827 if (inchar () == EOF
)
1834 if (cmp2p
!= NULL
&& *cmp2p
== '\0')
1836 /* Add all the characters. */
1837 for (cmpp
= thousands
; *cmpp
!= '\0'; ++cmpp
)
1838 ADDW ((unsigned char) *cmpp
);
1844 /* The last read character is not part of the number
1855 while (width
!= 0 && inchar () != EOF
);
1857 /* Have we read any character? If we try to read a number
1858 in hexadecimal notation and we have read only the `0x'
1859 prefix or no exponent this is an error. */
1860 if (wpsize
== 0 || (is_hexa
&& (wpsize
== 2 || ! got_e
)))
1864 /* Convert the number. */
1866 if (flags
& LONGDBL
)
1868 long double d
= __strtold_internal (wp
, &tw
, flags
& GROUP
);
1869 if (!(flags
& SUPPRESS
) && tw
!= wp
)
1870 *ARG (long double *) = negative
? -d
: d
;
1872 else if (flags
& LONG
)
1874 double d
= __strtod_internal (wp
, &tw
, flags
& GROUP
);
1875 if (!(flags
& SUPPRESS
) && tw
!= wp
)
1876 *ARG (double *) = negative
? -d
: d
;
1880 float d
= __strtof_internal (wp
, &tw
, flags
& GROUP
);
1881 if (!(flags
& SUPPRESS
) && tw
!= wp
)
1882 *ARG (float *) = negative
? -d
: d
;
1888 if (!(flags
& SUPPRESS
))
1892 case L_('['): /* Character class. */
1894 STRING_ARG (wstr
, wchar_t);
1896 STRING_ARG (str
, char);
1907 /* There is no width given so there is also no limit on the
1908 number of characters we read. Therefore we set width to
1909 a very high value to make the algorithm easier. */
1912 #ifdef COMPILE_WSCANF
1913 /* Find the beginning and the end of the scanlist. We are not
1914 creating a lookup table since it would have to be too large.
1915 Instead we search each time through the string. This is not
1916 a constant lookup time but who uses this feature deserves to
1918 tw
= (wchar_t *) f
; /* Marks the beginning. */
1923 while ((fc
= *f
++) != L
'\0' && fc
!= L
']');
1927 wp
= (wchar_t *) f
- 1;
1929 /* Fill WP with byte flags indexed by character.
1930 We will use this flag map for matching input characters. */
1931 if (wpmax
< UCHAR_MAX
+ 1)
1933 wpmax
= UCHAR_MAX
+ 1;
1934 wp
= (char *) alloca (wpmax
);
1936 memset (wp
, '\0', UCHAR_MAX
+ 1);
1939 if (fc
== ']' || fc
== '-')
1941 /* If ] or - appears before any char in the set, it is not
1942 the terminator or separator, but the first char in the
1948 while ((fc
= *f
++) != '\0' && fc
!= ']')
1949 if (fc
== '-' && *f
!= '\0' && *f
!= ']'
1950 && (unsigned char) f
[-2] <= (unsigned char) *f
)
1952 /* Add all characters from the one before the '-'
1953 up to (but not including) the next format char. */
1954 for (fc
= (unsigned char) f
[-2]; fc
< (unsigned char) *f
; ++fc
)
1958 /* Add the character to the flag map. */
1967 size_t now
= read_in
;
1968 #ifdef COMPILE_WSCANF
1969 if (inchar () == WEOF
)
1976 /* Test whether it's in the scanlist. */
1980 if (runp
[0] == L
'-' && runp
[1] != '\0' && runp
+ 1 != wp
1982 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
1984 /* Match against all characters in between the
1985 first and last character of the sequence. */
1988 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
1992 if (wc
<= runp
[1] && !not_in
)
1994 if (wc
<= runp
[1] && not_in
)
1996 /* The current character is not in the
2006 if (*runp
== c
&& !not_in
)
2008 if (*runp
== c
&& not_in
)
2018 if (runp
== wp
&& !not_in
)
2024 if (!(flags
& SUPPRESS
))
2028 if ((flags
& MALLOC
)
2029 && wstr
== (wchar_t *) *strptr
+ strsize
)
2031 /* Enlarge the buffer. */
2032 wstr
= (wchar_t *) realloc (*strptr
,
2034 * sizeof (wchar_t));
2037 /* Can't allocate that much. Last-ditch
2040 realloc (*strptr
, (strsize
+ 1)
2041 * sizeof (wchar_t));
2044 /* We lose. Oh well. Terminate the string
2045 and stop converting, so at least we don't
2047 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2053 *strptr
= (char *) wstr
;
2060 *strptr
= (char *) wstr
;
2067 while (--width
> 0 && inchar () != WEOF
);
2070 char buf
[MB_LEN_MAX
];
2074 if (inchar () == EOF
)
2077 memset (&cstate
, '\0', sizeof (cstate
));
2081 if (wp
[c
] == not_in
)
2083 ungetc_not_eof (c
, s
);
2088 if (!(flags
& SUPPRESS
))
2092 /* Convert it into a wide character. */
2094 n
= __mbrtowc (wstr
, buf
, 1, &cstate
);
2096 if (n
== (size_t) -2)
2098 /* Possibly correct character, just not enough
2101 assert (cnt
< MB_CUR_MAX
);
2106 if ((flags
& MALLOC
)
2107 && wstr
== (wchar_t *) *strptr
+ strsize
)
2109 /* Enlarge the buffer. */
2110 wstr
= (wchar_t *) realloc (*strptr
,
2112 * sizeof (wchar_t)));
2115 /* Can't allocate that much. Last-ditch
2118 realloc (*strptr
, ((strsize
+ 1)
2119 * sizeof (wchar_t)));
2122 /* We lose. Oh well. Terminate the
2123 string and stop converting,
2124 so at least we don't skip any input. */
2125 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2131 *strptr
= (char *) wstr
;
2138 *strptr
= (char *) wstr
;
2148 while (inchar () != EOF
);
2151 /* We stopped in the middle of recognizing another
2152 character. That's a problem. */
2157 /* We haven't succesfully read any character. */
2160 if (!(flags
& SUPPRESS
))
2164 if ((flags
& MALLOC
)
2165 && wstr
- (wchar_t *) *strptr
!= strsize
)
2167 wchar_t *cp
= (wchar_t *)
2168 realloc (*strptr
, ((wstr
- (wchar_t *) *strptr
)
2169 * sizeof(wchar_t)));
2171 *strptr
= (char *) cp
;
2179 size_t now
= read_in
;
2181 if (inchar () == EOF
)
2184 #ifdef COMPILE_WSCANF
2186 memset (&state
, '\0', sizeof (state
));
2193 /* Test whether it's in the scanlist. */
2197 if (runp
[0] == L
'-' && runp
[1] != '\0' && runp
+ 1 != wp
2199 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2201 /* Match against all characters in between the
2202 first and last character of the sequence. */
2205 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2209 if (wc
<= runp
[1] && !not_in
)
2211 if (wc
<= runp
[1] && not_in
)
2213 /* The current character is not in the
2223 if (*runp
== c
&& !not_in
)
2225 if (*runp
== c
&& not_in
)
2235 if (runp
== wp
&& !not_in
)
2241 if (!(flags
& SUPPRESS
))
2243 if ((flags
& MALLOC
)
2244 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
2246 /* Enlarge the buffer. */
2247 size_t strleng
= str
- *strptr
;
2250 newstr
= (char *) realloc (*strptr
, 2 * strsize
);
2253 /* Can't allocate that much. Last-ditch
2255 newstr
= (char *) realloc (*strptr
,
2256 strleng
+ MB_CUR_MAX
);
2259 /* We lose. Oh well. Terminate the string
2260 and stop converting, so at least we don't
2262 ((char *) (*strptr
))[strleng
] = '\0';
2269 str
= newstr
+ strleng
;
2270 strsize
= strleng
+ MB_CUR_MAX
;
2276 str
= newstr
+ strleng
;
2282 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
2283 if (n
== (size_t) -1)
2286 assert (n
<= MB_CUR_MAX
);
2289 while (--width
> 0 && inchar () != WEOF
);
2294 if (wp
[c
] == not_in
)
2296 ungetc_not_eof (c
, s
);
2301 if (!(flags
& SUPPRESS
))
2304 if ((flags
& MALLOC
)
2305 && (char *) str
== *strptr
+ strsize
)
2307 /* Enlarge the buffer. */
2308 str
= (char *) realloc (*strptr
, 2 * strsize
);
2311 /* Can't allocate that much. Last-ditch
2313 str
= (char *) realloc (*strptr
, strsize
+ 1);
2316 /* We lose. Oh well. Terminate the
2317 string and stop converting,
2318 so at least we don't skip any input. */
2319 ((char *) (*strptr
))[strsize
- 1] = '\0';
2325 *strptr
= (char *) str
;
2332 *strptr
= (char *) str
;
2339 while (--width
> 0 && inchar () != EOF
);
2343 /* We haven't succesfully read any character. */
2346 if (!(flags
& SUPPRESS
))
2348 #ifdef COMPILE_WSCANF
2349 /* We have to emit the code to get into the initial
2351 char buf
[MB_LEN_MAX
];
2352 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
2353 if (n
> 0 && (flags
& MALLOC
)
2354 && str
+ n
>= *strptr
+ strsize
)
2356 /* Enlarge the buffer. */
2357 size_t strleng
= str
- *strptr
;
2360 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
2363 /* We lose. Oh well. Terminate the string
2364 and stop converting, so at least we don't
2366 ((char *) (*strptr
))[strleng
] = '\0';
2373 str
= newstr
+ strleng
;
2374 strsize
= strleng
+ n
+ 1;
2378 str
= __mempcpy (str
, buf
, n
);
2382 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
2384 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
2394 case L_('p'): /* Generic pointer. */
2396 /* A PTR must be the same size as a `long int'. */
2397 flags
&= ~(SHORT
|LONGDBL
);
2405 /* If this is an unknown format character punt. */
2410 /* The last thing we saw int the format string was a white space.
2411 Consume the last white spaces. */
2416 while (ISSPACE (c
));
2420 /* Unlock stream. */
2427 # ifdef COMPILE_WSCANF
2429 __vfwscanf (FILE *s
, const wchar_t *format
, va_list argptr
)
2431 return _IO_vfwscanf (s
, format
, argptr
, NULL
);
2435 __vfscanf (FILE *s
, const char *format
, va_list argptr
)
2437 return _IO_vfscanf (s
, format
, argptr
, NULL
);
2442 #ifdef COMPILE_WSCANF
2443 weak_alias (__vfwscanf
, vfwscanf
)
2445 weak_alias (__vfscanf
, vfscanf
)