1 /* Copyright (C) 1991-1999, 2000 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
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 inchar() (c == WEOF ? WEOF \
80 : ((c = _IO_getwc_unlocked (s)), \
81 (void) (c != WEOF && ++read_in), c))
83 # define MEMCPY(d, s, n) wmemcpy (d, s, n)
84 # define ISSPACE(Ch) iswspace (Ch)
85 # define ISDIGIT(Ch) iswdigit (Ch)
86 # define ISXDIGIT(Ch) iswxdigit (Ch)
87 # define TOLOWER(Ch) towlower (Ch)
88 # define ORIENT if (s->_vtable_offset == 0 && _IO_fwide (s, 1) != 1)\
90 # define __strtoll_internal __wcstoll_internal
91 # define __strtoull_internal __wcstoull_internal
92 # define __strtol_internal __wcstol_internal
93 # define __strtoul_internal __wcstoul_internal
94 # define __strtold_internal __wcstold_internal
95 # define __strtod_internal __wcstod_internal
96 # define __strtof_internal __wcstof_internal
98 # define L_(Str) L##Str
99 # define CHAR_T wchar_t
100 # define UCHAR_T unsigned int
101 # define WINT_T wint_t
105 # define ungetc(c, s) ((void) ((int) c == EOF \
107 _IO_sputbackc (s, (unsigned char) c))))
108 # define inchar() (c == EOF ? EOF \
109 : ((c = _IO_getc_unlocked (s)), \
110 (void) (c != EOF && ++read_in), c))
111 # define MEMCPY(d, s, n) memcpy (d, s, n)
112 # define ISSPACE(Ch) isspace (Ch)
113 # define ISDIGIT(Ch) isdigit (Ch)
114 # define ISXDIGIT(Ch) isxdigit (Ch)
115 # define TOLOWER(Ch) tolower (Ch)
116 # define ORIENT if (_IO_fwide (s, -1) != -1) return EOF
120 # define UCHAR_T unsigned char
124 # define encode_error() do { \
125 if (errp != NULL) *errp |= 4; \
126 _IO_funlockfile (s); \
127 __libc_cleanup_end (0); \
128 __set_errno (EILSEQ); \
131 # define conv_error() do { \
132 if (errp != NULL) *errp |= 2; \
133 _IO_funlockfile (s); \
134 __libc_cleanup_end (0); \
137 # define input_error() do { \
138 _IO_funlockfile (s); \
139 if (errp != NULL) *errp |= 1; \
140 __libc_cleanup_end (0); \
141 return done ?: EOF; \
143 # define memory_error() do { \
144 _IO_funlockfile (s); \
145 __set_errno (ENOMEM); \
146 __libc_cleanup_end (0); \
149 # define ARGCHECK(s, format) \
152 /* Check file argument for consistence. */ \
153 CHECK_FILE (s, EOF); \
154 if (s->_flags & _IO_NO_READS) \
156 __set_errno (EBADF); \
159 else if (format == NULL) \
165 # define LOCK_STREAM(S) \
166 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, (S)); \
168 # define UNLOCK_STREAM(S) \
169 _IO_funlockfile (S); \
170 __libc_cleanup_region_end (0)
172 # define ungetc(c, s) ((void) (c != EOF && --read_in), ungetc (c, s))
173 # define inchar() (c == EOF ? EOF \
174 : ((c = getc (s)), (void) (c != EOF && ++read_in), c))
175 # define MEMCPY(d, s, n) memcpy (d, s, n)
176 # define ISSPACE(Ch) isspace (Ch)
177 # define ISDIGIT(Ch) isdigit (Ch)
178 # define ISXDIGIT(Ch) isxdigit (Ch)
179 # define TOLOWER(Ch) tolower (Ch)
183 # define UCHAR_T unsigned char
186 # define encode_error() do { \
188 __set_errno (EILSEQ); \
191 # define conv_error() do { \
195 # define input_error() do { \
197 return done ?: EOF; \
199 # define memory_error() do { \
201 __set_errno (ENOMEM); \
204 # define ARGCHECK(s, format) \
207 /* Check file argument for consistence. */ \
208 if (!__validfp (s) || !s->__mode.__read) \
210 __set_errno (EBADF); \
213 else if (format == NULL) \
215 __set_errno (EINVAL); \
220 /* XXX For now !!! */
221 # define flockfile(S) /* nothing */
222 # define funlockfile(S) /* nothing */
223 # define LOCK_STREAM(S)
224 # define UNLOCK_STREAM(S)
226 # define LOCK_STREAM(S) \
227 __libc_cleanup_region_start (&__funlockfile, (S)); \
229 # define UNLOCK_STREAM(S) \
231 __libc_cleanup_region_end (0)
236 /* Read formatted input from S according to the format string
237 FORMAT, using the argument list in ARG.
238 Return the number of assignments made, or -1 for an input error. */
240 # ifdef COMPILE_WSCANF
242 _IO_vfwscanf (s
, format
, argptr
, errp
)
244 const wchar_t *format
;
249 _IO_vfscanf (s
, format
, argptr
, errp
)
257 __vfscanf (FILE *s
, const char *format
, va_list argptr
)
261 register const CHAR_T
*f
= format
;
262 register UCHAR_T fc
; /* Current character of the format. */
263 register size_t done
= 0; /* Assignments done. */
264 register size_t read_in
= 0; /* Chars read in. */
265 register WINT_T c
= 0; /* Last char read. */
266 register int width
; /* Maximum field width. */
267 register int flags
; /* Modifiers for current format element. */
269 /* Status for reading F-P nums. */
270 char got_dot
, got_e
, negative
;
271 /* If a [...] is a [^...]. */
273 #define exp_char not_in
274 /* Base for integral numbers. */
276 /* Signedness for integral numbers. */
278 #define is_hexa number_signed
279 /* Decimal point character. */
280 #ifdef COMPILE_WSCANF
285 /* The thousands character of the current locale. */
286 #ifdef COMPILE_WSCANF
289 const char *thousands
;
291 /* State for the conversions. */
293 /* Integral holding variables. */
297 unsigned long long int uq
;
299 unsigned long int ul
;
301 /* Character-buffer pointer. */
303 wchar_t *wstr
= NULL
;
304 char **strptr
= NULL
;
306 /* We must not react on white spaces immediately because they can
307 possibly be matched even if in the input stream no character is
308 available anymore. */
310 /* Nonzero if we are reading a pointer. */
313 CHAR_T
*tw
; /* Temporary pointer. */
314 CHAR_T
*wp
= NULL
; /* Workspace. */
315 size_t wpmax
= 0; /* Maximal size of workspace. */
316 size_t wpsize
; /* Currently used bytes in workspace. */
320 if (wpsize == wpmax) \
323 wpmax = UCHAR_MAX > 2 * wpmax ? UCHAR_MAX : 2 * wpmax; \
324 wp = (CHAR_T *) alloca (wpmax * sizeof (wchar_t)); \
326 MEMCPY (wp, old, wpsize); \
328 wp[wpsize++] = (Ch); \
333 __va_copy (arg
, argptr
);
335 arg
= (va_list) argptr
;
342 ARGCHECK (s
, format
);
344 /* Figure out the decimal point character. */
345 #ifdef COMPILE_WSCANF
346 decimal
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
348 decimal
= _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
350 /* Figure out the thousands separator character. */
351 #ifdef COMPILE_WSCANF
352 thousands
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_THOUSANDS_SEP_WC
);
354 thousands
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
355 if (*thousands
== '\0')
359 /* Lock the stream. */
363 #ifndef COMPILE_WSCANF
364 /* From now on we use `state' to convert the format string. */
365 memset (&state
, '\0', sizeof (state
));
368 /* Run through the format string. */
372 /* Extract the next argument, which is of type TYPE.
373 For a %N$... spec, this is the Nth argument from the beginning;
374 otherwise it is the next argument after the state now in ARG. */
376 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
377 ({ unsigned int pos = argpos; \
379 __va_copy (arg, argptr); \
381 (void) va_arg (arg, void *); \
382 va_arg (arg, type); \
386 /* XXX Possible optimization. */
387 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
388 ({ va_list arg = (va_list) argptr; \
389 arg = (va_list) ((char *) arg \
391 * __va_rounded_size (void *)); \
392 va_arg (arg, type); \
395 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
396 ({ unsigned int pos = argpos; \
397 va_list arg = (va_list) argptr; \
399 (void) va_arg (arg, void *); \
400 va_arg (arg, type); \
405 #ifndef COMPILE_WSCANF
408 /* Non-ASCII, may be a multibyte. */
409 int len
= __mbrlen (f
, strlen (f
), &state
);
432 /* Remember to skip spaces. */
439 /* Read a character. */
442 /* Characters other than format specs must just match. */
446 /* We saw white space char as the last character in the format
447 string. Now it's time to skip all leading white space. */
451 if (inchar () == EOF
&& errno
== EINTR
)
465 /* This is the start of the conversion string. */
468 /* Not yet decided whether we read a pointer or not. */
471 /* Initialize state of modifiers. */
474 /* Prepare temporary buffer. */
477 /* Check for a positional parameter specification. */
480 argpos
= *f
++ - L_('0');
482 argpos
= argpos
* 10 + (*f
++ - L_('0'));
487 /* Oops; that was actually the field width. */
495 /* Check for the assignment-suppressing, the number grouping flag,
496 and the signal to use the locale's digit representation. */
497 while (*f
== L_('*') || *f
== L_('\'') || *f
== L_('I'))
511 /* We have seen width. */
515 /* Find the maximum field width. */
520 width
+= *f
++ - L_('0');
526 /* Check for type modifiers. */
530 /* ints are short ints or chars. */
542 /* A double `l' is equivalent to an `L'. */
544 flags
|= LONGDBL
| LONG
;
547 /* ints are long ints. */
552 /* doubles are long doubles, and ints are long long ints. */
553 flags
|= LONGDBL
| LONG
;
556 /* The `a' is used as a flag only if followed by `s', `S' or
558 if (*f
!= L_('s') && *f
!= L_('S') && *f
!= L_('['))
563 /* String conversions (%s, %[) take a `char **'
564 arg and fill it in with a malloc'd pointer. */
568 if (need_longlong
&& sizeof (size_t) > sizeof (unsigned long int))
570 else if (sizeof (size_t) > sizeof (unsigned int))
574 if (need_longlong
&& sizeof (uintmax_t) > sizeof (unsigned long int))
576 else if (sizeof (uintmax_t) > sizeof (unsigned int))
580 if (need_longlong
&& sizeof (ptrdiff_t) > sizeof (long int))
582 else if (sizeof (ptrdiff_t) > sizeof (int))
586 /* Not a recognized modifier. Backup. */
591 /* End of the format string? */
595 /* Find the conversion specifier. */
597 if (skip_space
|| (fc
!= L_('[') && fc
!= L_('c')
598 && fc
!= L_('C') && fc
!= L_('n')))
600 /* Eat whitespace. */
601 int save_errno
= errno
;
604 if (inchar () == EOF
&& errno
== EINTR
)
614 case L_('%'): /* Must match a literal '%'. */
625 case L_('n'): /* Answer number of assignments done. */
626 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
627 with the 'n' conversion specifier. */
628 if (!(flags
& SUPPRESS
))
630 /* Don't count the read-ahead. */
631 if (need_longlong
&& (flags
& LONGDBL
))
632 *ARG (long long int *) = read_in
;
633 else if (need_long
&& (flags
& LONG
))
634 *ARG (long int *) = read_in
;
635 else if (flags
& SHORT
)
636 *ARG (short int *) = read_in
;
637 else if (!(flags
& CHAR
))
638 *ARG (int *) = read_in
;
640 *ARG (char *) = read_in
;
642 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
643 /* We have a severe problem here. The ISO C standard
644 contradicts itself in explaining the effect of the %n
645 format in `scanf'. While in ISO C:1990 and the ISO C
646 Amendement 1:1995 the result is described as
648 Execution of a %n directive does not effect the
649 assignment count returned at the completion of
650 execution of the f(w)scanf function.
652 in ISO C Corrigendum 1:1994 the following was added:
655 Add the following fourth example:
658 int d1, d2, n1, n2, i;
659 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
660 the value 123 is assigned to d1 and the value3 to n1.
661 Because %n can never get an input failure the value
662 of 3 is also assigned to n2. The value of d2 is not
663 affected. The value 3 is assigned to i.
665 We go for now with the historically correct code from ISO C,
666 i.e., we don't count the %n assignments. When it ever
667 should proof to be wrong just remove the #ifdef above. */
673 case L_('c'): /* Match characters. */
674 if ((flags
& LONG
) == 0)
676 if (!(flags
& SUPPRESS
))
690 #ifdef COMPILE_WSCANF
691 /* We have to convert the wide character(s) into multibyte
692 characters and store the result. */
693 memset (&state
, '\0', sizeof (state
));
699 n
= wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
700 if (n
== (size_t) -1)
701 /* No valid wide character. */
704 /* Increment the output pointer. Even if we don't
708 while (--width
> 0 && inchar () != EOF
);
710 if (!(flags
& SUPPRESS
))
714 while (--width
> 0 && inchar () != EOF
);
717 while (--width
> 0 && inchar () != EOF
);
720 if (!(flags
& SUPPRESS
))
727 if (!(flags
& SUPPRESS
))
729 wstr
= ARG (wchar_t *);
738 #ifdef COMPILE_WSCANF
739 /* Just store the incoming wide characters. */
740 if (!(flags
& SUPPRESS
))
744 while (--width
> 0 && inchar () != EOF
);
747 while (--width
> 0 && inchar () != EOF
);
750 /* We have to convert the multibyte input sequence to wide
752 char buf
[MB_LEN_MAX
];
755 memset (&cstate
, '\0', sizeof (cstate
));
761 /* This is what we present the mbrtowc function first. */
769 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
772 if (n
== (size_t) -2)
774 /* Possibly correct character, just not enough
776 assert (cnt
< MB_CUR_MAX
);
778 if (inchar () == EOF
)
788 /* We have a match. */
792 /* Advance the result pointer. */
795 while (--width
> 0 && inchar () != EOF
);
799 if (!(flags
& SUPPRESS
))
804 case L_('s'): /* Read a string. */
807 #define STRING_ARG(Str, Type) \
808 do if (!(flags & SUPPRESS)) \
810 if (flags & MALLOC) \
812 /* The string is to be stored in a malloc'd buffer. */ \
813 strptr = ARG (char **); \
814 if (strptr == NULL) \
816 /* Allocate an initial buffer. */ \
818 *strptr = (char *) malloc (strsize * sizeof (Type)); \
819 Str = (Type *) *strptr; \
822 Str = ARG (Type *); \
826 STRING_ARG (str
, char);
832 #ifdef COMPILE_WSCANF
833 memset (&state
, '\0', sizeof (state
));
844 #ifdef COMPILE_WSCANF
845 /* This is quite complicated. We have to convert the
846 wide characters into multibyte characters and then
851 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
852 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
854 /* We have to enlarge the buffer if the `a' flag
856 str
= (char *) realloc (*strptr
, strsize
* 2);
859 /* Can't allocate that much. Last-ditch
861 str
= (char *) realloc (*strptr
, strsize
+ 1);
864 /* We lose. Oh well. Terminate the
865 string and stop converting,
866 so at least we don't skip any input. */
867 ((char *) (*strptr
))[strsize
- 1] = '\0';
873 *strptr
= (char *) str
;
880 *strptr
= (char *) str
;
886 n
= wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
887 if (n
== (size_t) -1)
890 assert (n
<= MB_CUR_MAX
);
895 if (!(flags
& SUPPRESS
))
899 && (char *) str
== *strptr
+ strsize
)
901 /* Enlarge the buffer. */
902 str
= (char *) realloc (*strptr
, 2 * strsize
);
905 /* Can't allocate that much. Last-ditch
907 str
= (char *) realloc (*strptr
, strsize
+ 1);
910 /* We lose. Oh well. Terminate the
911 string and stop converting,
912 so at least we don't skip any input. */
913 ((char *) (*strptr
))[strsize
- 1] = '\0';
919 *strptr
= (char *) str
;
926 *strptr
= (char *) str
;
934 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
936 if (!(flags
& SUPPRESS
))
938 #ifdef COMPILE_WSCANF
939 /* We have to emit the code to get into the intial
941 char buf
[MB_LEN_MAX
];
942 size_t n
= wcrtomb (buf
, L
'\0', &state
);
943 if (n
> 0 && (flags
& MALLOC
)
944 && str
+ n
>= *strptr
+ strsize
)
946 /* Enlarge the buffer. */
947 str
= (char *) realloc (*strptr
,
948 (str
+ n
+ 1) - *strptr
);
951 /* We lose. Oh well. Terminate the string
952 and stop converting, so at least we don't
954 ((char *) (*strptr
))[strsize
- 1] = '\0';
960 *strptr
= (char *) str
;
961 str
= ((char *) *strptr
) + strsize
;
962 strsize
= (str
+ n
+ 1) - *strptr
;
966 str
= __mempcpy (str
, buf
, n
);
970 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
972 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
985 #ifndef COMPILE_WSCANF
989 /* Wide character string. */
990 STRING_ARG (wstr
, wchar_t);
996 #ifndef COMPILE_WSCANF
997 memset (&cstate
, '\0', sizeof (cstate
));
1008 #ifdef COMPILE_WSCANF
1010 if (!(flags
& SUPPRESS
))
1013 if ((flags
& MALLOC
)
1014 && wstr
== (wchar_t *) *strptr
+ strsize
)
1016 /* Enlarge the buffer. */
1017 wstr
= (wchar_t *) realloc (*strptr
,
1019 * sizeof (wchar_t));
1022 /* Can't allocate that much. Last-ditch
1024 wstr
= (wchar_t *) realloc (*strptr
,
1026 + sizeof (wchar_t)));
1029 /* We lose. Oh well. Terminate the string
1030 and stop converting, so at least we don't
1032 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1038 *strptr
= (char *) wstr
;
1045 *strptr
= (char *) wstr
;
1053 char buf
[MB_LEN_MAX
];
1063 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
1066 if (n
== (size_t) -2)
1068 /* Possibly correct character, just not enough
1070 assert (cnt
< MB_CUR_MAX
);
1072 if (inchar () == EOF
)
1082 /* We have a match. */
1086 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
1087 && wstr
== (wchar_t *) *strptr
+ strsize
)
1089 /* Enlarge the buffer. */
1090 wstr
= (wchar_t *) realloc (*strptr
,
1092 * sizeof (wchar_t)));
1095 /* Can't allocate that much. Last-ditch effort. */
1096 wstr
= (wchar_t *) realloc (*strptr
,
1098 * sizeof (wchar_t)));
1101 /* We lose. Oh well. Terminate the
1102 string and stop converting, so at
1103 least we don't skip any input. */
1104 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1110 *strptr
= (char *) wstr
;
1117 *strptr
= (char *) wstr
;
1125 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
1127 if (!(flags
& SUPPRESS
))
1131 if ((flags
& MALLOC
) && wstr
- (wchar_t *) *strptr
!= strsize
)
1133 wchar_t *cp
= (wchar_t *) realloc (*strptr
,
1135 - (wchar_t *) *strptr
)
1136 * sizeof(wchar_t)));
1138 *strptr
= (char *) cp
;
1146 case L_('x'): /* Hexadecimal integer. */
1147 case L_('X'): /* Ditto. */
1152 case L_('o'): /* Octal integer. */
1157 case L_('u'): /* Unsigned decimal integer. */
1162 case L_('d'): /* Signed decimal integer. */
1167 case L_('i'): /* Generic number. */
1176 /* Check for a sign. */
1177 if (c
== L_('-') || c
== L_('+'))
1185 /* Look for a leading indication of base. */
1186 if (width
!= 0 && c
== L_('0'))
1194 if (width
!= 0 && TOLOWER (c
) == L_('x'))
1212 if (base
== 10 && (flags
& I18N
) != 0)
1217 #ifdef COMPILE_WSCANF
1218 const wchar_t *wcdigits
[10];
1220 const char *mbdigits
[10];
1225 #ifdef COMPILE_WSCANF
1226 to_level
= _NL_CURRENT_WORD (LC_CTYPE
,
1227 _NL_CTYPE_INDIGITS_WC_LEN
) - 1;
1229 to_level
= _NL_CURRENT_WORD (LC_CTYPE
,
1230 _NL_CTYPE_INDIGITS_MB_LEN
) - 1;
1233 /* Read the number into workspace. */
1234 while (c
!= EOF
&& width
!= 0)
1236 /* In this round we get the pointer to the digit strings
1237 and also perform the first round of comparisons. */
1238 for (n
= 0; n
< 10; ++n
)
1240 /* Get the string for the digits with value N. */
1241 #ifdef COMPILE_WSCANF
1242 wcdigits
[n
] = (const wchar_t *)
1243 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1244 wcdigits
[n
] += from_level
;
1246 if (c
== *wcdigits
[n
])
1248 to_level
= from_level
;
1252 /* Advance the pointer to the next string. */
1256 int avail
= width
> 0 ? width
: INT_MAX
;
1258 mbdigits
[n
] = _NL_CURRENT (LC_CTYPE
,
1259 _NL_CTYPE_INDIGITS0_MB
+ n
);
1261 for (level
= 0; level
< from_level
; level
++)
1262 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1265 while (*cmpp
== c
&& avail
> 0)
1267 if (*++cmpp
== '\0')
1271 if ((c
= inchar ()) == EOF
)
1281 to_level
= from_level
;
1285 /* We are pushing all read characters back. */
1286 if (cmpp
> mbdigits
[n
])
1289 while (--cmpp
> mbdigits
[n
])
1294 /* Advance the pointer to the next string. */
1295 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1301 /* Have not yet found the digit. */
1302 for (level
= from_level
+ 1; level
<= to_level
; ++level
)
1304 /* Search all ten digits of this level. */
1305 for (n
= 0; n
< 10; ++n
)
1307 #ifdef COMPILE_WSCANF
1308 if (c
== *wcdigits
[n
])
1311 /* Advance the pointer to the next string. */
1315 int avail
= width
> 0 ? width
: INT_MAX
;
1318 while (*cmpp
== c
&& avail
> 0)
1320 if (*++cmpp
== '\0')
1324 if ((c
= inchar ()) == EOF
)
1337 /* We are pushing all read characters back. */
1338 if (cmpp
> mbdigits
[n
])
1341 while (--cmpp
> mbdigits
[n
])
1346 /* Advance the pointer to the next string. */
1347 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1363 else if ((flags
& GROUP
)
1364 #ifdef COMPILE_WSCANF
1365 && thousands
!= L
'\0'
1367 && thousands
!= NULL
1371 /* Try matching against the thousands separator. */
1372 #ifdef COMPILE_WSCANF
1376 const char *cmpp
= thousands
;
1377 int avail
= width
> 0 ? width
: INT_MAX
;
1379 while (*cmpp
== c
&& avail
> 0)
1382 if (*++cmpp
== '\0')
1386 if ((c
= inchar ()) == EOF
)
1394 /* We are pushing all read characters back. */
1395 if (cmpp
> thousands
)
1397 wpsize
-= cmpp
- thousands
;
1399 while (--cmpp
> thousands
)
1409 /* The last thousands character will be added back by
1425 /* Read the number into workspace. */
1426 while (c
!= EOF
&& width
!= 0)
1433 else if (!ISDIGIT (c
) || c
- L_('0') >= base
)
1435 if (base
== 10 && (flags
& GROUP
)
1436 #ifdef COMPILE_WSCANF
1437 && thousands
!= L
'\0'
1439 && thousands
!= NULL
1443 /* Try matching against the thousands separator. */
1444 #ifdef COMPILE_WSCANF
1448 const char *cmpp
= thousands
;
1449 int avail
= width
> 0 ? width
: INT_MAX
;
1451 while (*cmpp
== c
&& avail
> 0)
1454 if (*++cmpp
== '\0')
1458 if ((c
= inchar ()) == EOF
)
1466 /* We are pushing all read characters back. */
1467 if (cmpp
> thousands
)
1469 wpsize
-= cmpp
- thousands
;
1471 while (--cmpp
> thousands
)
1481 /* The last thousands character will be added back by
1497 || (wpsize
== 1 && (wp
[0] == L_('+') || wp
[0] == L_('-'))))
1499 /* There was no number. If we are supposed to read a pointer
1500 we must recognize "(nil)" as well. */
1501 if (wpsize
== 0 && read_pointer
&& (width
< 0 || width
>= 0)
1503 && TOLOWER (inchar ()) == L_('n')
1504 && TOLOWER (inchar ()) == L_('i')
1505 && TOLOWER (inchar ()) == L_('l')
1506 && inchar () == L_(')'))
1507 /* We must produce the value of a NULL pointer. A single
1508 '0' digit is enough. */
1512 /* The last read character is not part of the number
1520 /* The just read character is not part of the number anymore. */
1523 /* Convert the number. */
1525 if (need_longlong
&& (flags
& LONGDBL
))
1528 num
.q
= __strtoll_internal (wp
, &tw
, base
, flags
& GROUP
);
1530 num
.uq
= __strtoull_internal (wp
, &tw
, base
, flags
& GROUP
);
1535 num
.l
= __strtol_internal (wp
, &tw
, base
, flags
& GROUP
);
1537 num
.ul
= __strtoul_internal (wp
, &tw
, base
, flags
& GROUP
);
1542 if (!(flags
& SUPPRESS
))
1544 if (! number_signed
)
1546 if (need_longlong
&& (flags
& LONGDBL
))
1547 *ARG (unsigned LONGLONG
int *) = num
.uq
;
1548 else if (need_long
&& (flags
& LONG
))
1549 *ARG (unsigned long int *) = num
.ul
;
1550 else if (flags
& SHORT
)
1551 *ARG (unsigned short int *)
1552 = (unsigned short int) num
.ul
;
1553 else if (!(flags
& CHAR
))
1554 *ARG (unsigned int *) = (unsigned int) num
.ul
;
1556 *ARG (unsigned char *) = (unsigned char) num
.ul
;
1560 if (need_longlong
&& (flags
& LONGDBL
))
1561 *ARG (LONGLONG
int *) = num
.q
;
1562 else if (need_long
&& (flags
& LONG
))
1563 *ARG (long int *) = num
.l
;
1564 else if (flags
& SHORT
)
1565 *ARG (short int *) = (short int) num
.l
;
1566 else if (!(flags
& CHAR
))
1567 *ARG (int *) = (int) num
.l
;
1569 *ARG (signed char *) = (signed char) num
.ul
;
1575 case L_('e'): /* Floating-point numbers. */
1586 /* Check for a sign. */
1587 if (c
== L_('-') || c
== L_('+'))
1589 negative
= c
== L_('-');
1590 if (width
== 0 || inchar () == EOF
)
1591 /* EOF is only an input error before we read any chars. */
1595 #ifdef COMPILE_WSCANF
1598 /* This is no valid number. */
1603 /* Match against the decimal point. At this point
1604 we are taking advantage of the fact that we can
1605 push more than one character back. This is
1606 (almost) never necessary since the decimal point
1607 string hopefully never contains more than one
1609 const char *cmpp
= decimal
;
1610 int avail
= width
> 0 ? width
: INT_MAX
;
1612 while (*cmpp
== c
&& avail
> 0)
1613 if (*++cmpp
== '\0')
1617 if (inchar () == EOF
)
1624 /* This is no valid number. */
1628 if (cmpp
== decimal
)
1645 /* Take care for the special arguments "nan" and "inf". */
1646 if (TOLOWER (c
) == L_('n'))
1650 if (width
== 0 || inchar () == EOF
|| TOLOWER (c
) != L_('a'))
1655 if (width
== 0 || inchar () == EOF
|| TOLOWER (c
) != L_('n'))
1663 else if (TOLOWER (c
) == L_('i'))
1665 /* Maybe "inf" or "infinity". */
1667 if (width
== 0 || inchar () == EOF
|| TOLOWER (c
) != L_('n'))
1672 if (width
== 0 || inchar () == EOF
|| TOLOWER (c
) != L_('f'))
1677 /* It is as least "inf". */
1678 if (width
!= 0 && inchar () != EOF
)
1680 if (TOLOWER (c
) == L_('i'))
1684 /* Now we have to read the rest as well. */
1686 if (width
== 0 || inchar () == EOF
1687 || TOLOWER (c
) != L_('n'))
1692 if (width
== 0 || inchar () == EOF
1693 || TOLOWER (c
) != L_('i'))
1698 if (width
== 0 || inchar () == EOF
1699 || TOLOWER (c
) != L_('t'))
1704 if (width
== 0 || inchar () == EOF
1705 || TOLOWER (c
) != L_('y'))
1720 if (width
!= 0 && c
== L_('0'))
1726 if (width
!= 0 && TOLOWER (c
) == L_('x'))
1728 /* It is a number in hexadecimal format. */
1734 /* Grouping is not allowed. */
1742 got_dot
= got_e
= 0;
1747 else if (!got_e
&& is_hexa
&& ISXDIGIT (c
))
1749 else if (got_e
&& wp
[wpsize
- 1] == exp_char
1750 && (c
== L_('-') || c
== L_('+')))
1752 else if (wpsize
> 0 && !got_e
&& TOLOWER (c
) == exp_char
)
1755 got_e
= got_dot
= 1;
1759 #ifdef COMPILE_WSCANF
1760 if (! got_dot
&& c
== decimal
)
1765 else if (thousands
!= L
'\0' && ! got_dot
&& c
== thousands
)
1769 /* The last read character is not part of the number
1775 const char *cmpp
= decimal
;
1776 int avail
= width
> 0 ? width
: INT_MAX
;
1780 while (*cmpp
== c
&& avail
> 0)
1781 if (*++cmpp
== '\0')
1785 if (inchar () == EOF
)
1793 /* Add all the characters. */
1794 for (cmpp
= decimal
; *cmpp
!= '\0'; ++cmpp
)
1802 /* Figure out whether it is a thousands separator.
1803 There is one problem: we possibly read more than
1804 one character. We cannot push them back but since
1805 we know that parts of the `decimal' string matched,
1806 we can compare against it. */
1807 const char *cmp2p
= thousands
;
1809 if (thousands
!= NULL
&& ! got_dot
)
1812 && *cmp2p
== decimal
[cmp2p
- thousands
])
1816 while (*cmp2p
== c
&& avail
> 0)
1817 if (*++cmp2p
== '\0')
1821 if (inchar () == EOF
)
1828 if (cmp2p
!= NULL
&& *cmp2p
== '\0')
1830 /* Add all the characters. */
1831 for (cmpp
= thousands
; *cmpp
!= '\0'; ++cmpp
)
1838 /* The last read character is not part of the number
1849 while (width
!= 0 && inchar () != EOF
);
1851 /* Have we read any character? If we try to read a number
1852 in hexadecimal notation and we have read only the `0x'
1853 prefix or no exponent this is an error. */
1854 if (wpsize
== 0 || (is_hexa
&& (wpsize
== 2 || ! got_e
)))
1858 /* Convert the number. */
1860 if (flags
& LONGDBL
)
1862 long double d
= __strtold_internal (wp
, &tw
, flags
& GROUP
);
1863 if (!(flags
& SUPPRESS
) && tw
!= wp
)
1864 *ARG (long double *) = negative
? -d
: d
;
1866 else if (flags
& LONG
)
1868 double d
= __strtod_internal (wp
, &tw
, flags
& GROUP
);
1869 if (!(flags
& SUPPRESS
) && tw
!= wp
)
1870 *ARG (double *) = negative
? -d
: d
;
1874 float d
= __strtof_internal (wp
, &tw
, flags
& GROUP
);
1875 if (!(flags
& SUPPRESS
) && tw
!= wp
)
1876 *ARG (float *) = negative
? -d
: d
;
1882 if (!(flags
& SUPPRESS
))
1886 case L_('['): /* Character class. */
1888 STRING_ARG (wstr
, wchar_t);
1890 STRING_ARG (str
, char);
1901 /* There is no width given so there is also no limit on the
1902 number of characters we read. Therefore we set width to
1903 a very high value to make the algorithm easier. */
1906 #ifdef COMPILE_WSCANF
1907 /* Find the beginning and the end of the scanlist. We are not
1908 creating a lookup table since it would have to be too large.
1909 Instead we search each time through the string. This is not
1910 a constant lookup time but who uses this feature deserves to
1912 tw
= (wchar_t *) f
; /* Marks the beginning. */
1914 if (*f
== ']' || *f
== '-')
1917 while ((fc
= *f
++) != L
'\0' && fc
!= L
']');
1921 wp
= (wchar_t *) f
- 1;
1923 /* Fill WP with byte flags indexed by character.
1924 We will use this flag map for matching input characters. */
1925 if (wpmax
< UCHAR_MAX
)
1928 wp
= (char *) alloca (wpmax
);
1930 memset (wp
, '\0', UCHAR_MAX
);
1933 if (fc
== ']' || fc
== '-')
1935 /* If ] or - appears before any char in the set, it is not
1936 the terminator or separator, but the first char in the
1942 while ((fc
= *f
++) != '\0' && fc
!= ']')
1943 if (fc
== '-' && *f
!= '\0' && *f
!= ']'
1944 && (unsigned char) f
[-2] <= (unsigned char) *f
)
1946 /* Add all characters from the one before the '-'
1947 up to (but not including) the next format char. */
1948 for (fc
= f
[-2]; fc
< *f
; ++fc
)
1952 /* Add the character to the flag map. */
1961 size_t now
= read_in
;
1962 #ifdef COMPILE_WSCANF
1967 if (inchar () == WEOF
)
1970 /* Test whether it's in the scanlist. */
1974 if (runp
[0] == L
'-' && runp
[1] != '\0' && runp
[1] != ']'
1976 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
1978 /* Match against all characters in between the
1979 first and last character of the sequence. */
1982 for (wc
= runp
[-1] + 1; wc
< runp
[1]; ++wc
)
1986 if (wc
== runp
[1] && !not_in
)
1988 if (wc
== runp
[1] && not_in
)
1990 /* The current character is not in the
1998 if (*runp
== runp
[1] && !not_in
)
2000 if (*runp
!= runp
[1] && not_in
)
2010 if (!(flags
& SUPPRESS
))
2014 if ((flags
& MALLOC
)
2015 && wstr
== (wchar_t *) *strptr
+ strsize
)
2017 /* Enlarge the buffer. */
2018 wstr
= (wchar_t *) realloc (*strptr
,
2020 * sizeof (wchar_t));
2023 /* Can't allocate that much. Last-ditch
2026 realloc (*strptr
, (strsize
2027 + sizeof (wchar_t)));
2030 /* We lose. Oh well. Terminate the string
2031 and stop converting, so at least we don't
2033 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2039 *strptr
= (char *) wstr
;
2046 *strptr
= (char *) wstr
;
2053 while (--width
> 0);
2056 char buf
[MB_LEN_MAX
];
2060 memset (&cstate
, '\0', sizeof (cstate
));
2065 if (inchar () == EOF
)
2068 if (wp
[c
] == not_in
)
2075 if (!(flags
& SUPPRESS
))
2079 /* Convert it into a wide character. */
2080 n
= __mbrtowc (wstr
, buf
, cnt
, &cstate
);
2082 if (n
== (size_t) -2)
2084 /* Possibly correct character, just not enough
2086 assert (cnt
< MB_CUR_MAX
);
2094 if ((flags
& MALLOC
)
2095 && wstr
== (wchar_t *) *strptr
+ strsize
)
2097 /* Enlarge the buffer. */
2098 wstr
= (wchar_t *) realloc (*strptr
,
2100 * sizeof (wchar_t)));
2103 /* Can't allocate that much. Last-ditch
2106 realloc (*strptr
, ((strsize
+ 1)
2107 * sizeof (wchar_t)));
2110 /* We lose. Oh well. Terminate the
2111 string and stop converting,
2112 so at least we don't skip any input. */
2113 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2119 *strptr
= (char *) wstr
;
2126 *strptr
= (char *) wstr
;
2133 while (--width
> 0);
2136 /* We stopped in the middle of recognizing another
2137 character. That's a problem. */
2142 /* We haven't succesfully read any character. */
2145 if (!(flags
& SUPPRESS
))
2149 if ((flags
& MALLOC
)
2150 && wstr
- (wchar_t *) *strptr
!= strsize
)
2152 wchar_t *cp
= (wchar_t *)
2153 realloc (*strptr
, ((wstr
- (wchar_t *) *strptr
)
2154 * sizeof(wchar_t)));
2156 *strptr
= (char *) cp
;
2164 size_t now
= read_in
;
2165 #ifdef COMPILE_WSCANF
2167 memset (&state
, '\0', sizeof (state
));
2174 if (inchar () == WEOF
)
2177 /* Test whether it's in the scanlist. */
2181 if (runp
[0] == L
'-' && runp
[1] != '\0' && runp
[1] != ']'
2183 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2185 /* Match against all characters in between the
2186 first and last character of the sequence. */
2189 for (wc
= runp
[-1] + 1; wc
< runp
[1]; ++wc
)
2193 if (wc
== runp
[1] && !not_in
)
2195 if (wc
== runp
[1] && not_in
)
2197 /* The current character is not in the
2205 if (*runp
== runp
[1] && !not_in
)
2207 if (*runp
!= runp
[1] && not_in
)
2217 if (!(flags
& SUPPRESS
))
2219 if ((flags
& MALLOC
)
2220 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
2222 /* Enlarge the buffer. */
2223 str
= (char *) realloc (*strptr
, 2 * strsize
);
2226 /* Can't allocate that much. Last-ditch
2228 str
= (char *) realloc (*strptr
, strsize
+ 1);
2231 /* We lose. Oh well. Terminate the string
2232 and stop converting, so at least we don't
2234 (*strptr
)[strsize
- 1] = '\0';
2254 n
= wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
2255 if (n
== (size_t) -1)
2258 assert (n
<= MB_CUR_MAX
);
2261 while (--width
> 0);
2266 if (inchar () == EOF
)
2269 if (wp
[c
] == not_in
)
2276 if (!(flags
& SUPPRESS
))
2279 if ((flags
& MALLOC
)
2280 && (char *) str
== *strptr
+ strsize
)
2282 /* Enlarge the buffer. */
2283 str
= (char *) realloc (*strptr
, 2 * strsize
);
2286 /* Can't allocate that much. Last-ditch
2288 str
= (char *) realloc (*strptr
, strsize
+ 1);
2291 /* We lose. Oh well. Terminate the
2292 string and stop converting,
2293 so at least we don't skip any input. */
2294 ((char *) (*strptr
))[strsize
- 1] = '\0';
2300 *strptr
= (char *) str
;
2307 *strptr
= (char *) str
;
2314 while (--width
> 0);
2318 /* We haven't succesfully read any character. */
2321 if (!(flags
& SUPPRESS
))
2323 #ifdef COMPILE_WSCANF
2324 /* We have to emit the code to get into the intial
2326 char buf
[MB_LEN_MAX
];
2327 size_t n
= wcrtomb (buf
, L
'\0', &state
);
2328 if (n
> 0 && (flags
& MALLOC
)
2329 && str
+ n
>= *strptr
+ strsize
)
2331 /* Enlarge the buffer. */
2332 str
= (char *) realloc (*strptr
,
2333 (str
+ n
+ 1) - *strptr
);
2336 /* We lose. Oh well. Terminate the string
2337 and stop converting, so at least we don't
2339 ((char *) (*strptr
))[strsize
- 1] = '\0';
2345 *strptr
= (char *) str
;
2346 str
= ((char *) *strptr
) + strsize
;
2347 strsize
= (str
+ n
+ 1) - *strptr
;
2351 str
= __mempcpy (str
, buf
, n
);
2355 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
2357 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
2367 case L_('p'): /* Generic pointer. */
2369 /* A PTR must be the same size as a `long int'. */
2370 flags
&= ~(SHORT
|LONGDBL
);
2378 /* If this is an unknown format character punt. */
2383 /* The last thing we saw int the format string was a white space.
2384 Consume the last white spaces. */
2389 while (ISSPACE (c
));
2393 /* Unlock stream. */
2400 # ifdef COMPILE_WSCANF
2402 __vfwscanf (FILE *s
, const wchar_t *format
, va_list argptr
)
2404 return _IO_vfwscanf (s
, format
, argptr
, NULL
);
2408 __vfscanf (FILE *s
, const char *format
, va_list argptr
)
2410 return _IO_vfscanf (s
, format
, argptr
, NULL
);
2415 #ifdef COMPILE_WSCANF
2416 weak_alias (__vfwscanf
, vfwscanf
)
2418 weak_alias (__vfscanf
, vfscanf
)