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 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 (s->_vtable_offset == 0 && _IO_fwide (s, 1) != 1)\
92 # define __strtoll_internal __wcstoll_internal
93 # define __strtoull_internal __wcstoull_internal
94 # define __strtol_internal __wcstol_internal
95 # define __strtoul_internal __wcstoul_internal
96 # define __strtold_internal __wcstold_internal
97 # define __strtod_internal __wcstod_internal
98 # define __strtof_internal __wcstof_internal
100 # define L_(Str) L##Str
101 # define CHAR_T wchar_t
102 # define UCHAR_T unsigned int
103 # define WINT_T wint_t
107 # define ungetc(c, s) ((void) ((int) c == EOF \
109 _IO_sputbackc (s, (unsigned char) c))))
110 # define ungetc_not_eof(c, s) ((void) (--read_in, \
111 _IO_sputbackc (s, (unsigned char) c)))
112 # define inchar() (c == EOF ? EOF \
113 : ((c = _IO_getc_unlocked (s)), \
114 (void) (c != EOF && ++read_in), c))
115 # define MEMCPY(d, s, n) memcpy (d, s, n)
116 # define ISSPACE(Ch) isspace (Ch)
117 # define ISDIGIT(Ch) isdigit (Ch)
118 # define ISXDIGIT(Ch) isxdigit (Ch)
119 # define TOLOWER(Ch) tolower (Ch)
120 # define ORIENT if (_IO_fwide (s, -1) != -1) return EOF
124 # define UCHAR_T unsigned char
128 # define encode_error() do { \
129 if (errp != NULL) *errp |= 4; \
130 _IO_funlockfile (s); \
131 __libc_cleanup_end (0); \
132 __set_errno (EILSEQ); \
135 # define conv_error() do { \
136 if (errp != NULL) *errp |= 2; \
137 _IO_funlockfile (s); \
138 __libc_cleanup_end (0); \
141 # define input_error() do { \
142 _IO_funlockfile (s); \
143 if (errp != NULL) *errp |= 1; \
144 __libc_cleanup_end (0); \
145 return done ?: EOF; \
147 # define memory_error() do { \
148 _IO_funlockfile (s); \
149 __set_errno (ENOMEM); \
150 __libc_cleanup_end (0); \
153 # define ARGCHECK(s, format) \
156 /* Check file argument for consistence. */ \
157 CHECK_FILE (s, EOF); \
158 if (s->_flags & _IO_NO_READS) \
160 __set_errno (EBADF); \
163 else if (format == NULL) \
169 # define LOCK_STREAM(S) \
170 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, (S)); \
172 # define UNLOCK_STREAM(S) \
173 _IO_funlockfile (S); \
174 __libc_cleanup_region_end (0)
176 # define ungetc(c, s) ((void) (c != EOF && --read_in), ungetc (c, s))
177 # define ungetc_not_eof(c, s) (--read_in, (ungetc) (c, s))
178 # define inchar() (c == EOF ? EOF \
179 : ((c = getc (s)), (void) (c != EOF && ++read_in), c))
180 # define MEMCPY(d, s, n) memcpy (d, s, n)
181 # define ISSPACE(Ch) isspace (Ch)
182 # define ISDIGIT(Ch) isdigit (Ch)
183 # define ISXDIGIT(Ch) isxdigit (Ch)
184 # define TOLOWER(Ch) tolower (Ch)
188 # define UCHAR_T unsigned char
191 # define encode_error() do { \
193 __set_errno (EILSEQ); \
196 # define conv_error() do { \
200 # define input_error() do { \
202 return done ?: EOF; \
204 # define memory_error() do { \
206 __set_errno (ENOMEM); \
209 # define ARGCHECK(s, format) \
212 /* Check file argument for consistence. */ \
213 if (!__validfp (s) || !s->__mode.__read) \
215 __set_errno (EBADF); \
218 else if (format == NULL) \
220 __set_errno (EINVAL); \
225 /* XXX For now !!! */
226 # define flockfile(S) /* nothing */
227 # define funlockfile(S) /* nothing */
228 # define LOCK_STREAM(S)
229 # define UNLOCK_STREAM(S)
231 # define LOCK_STREAM(S) \
232 __libc_cleanup_region_start (&__funlockfile, (S)); \
234 # define UNLOCK_STREAM(S) \
236 __libc_cleanup_region_end (0)
241 /* Read formatted input from S according to the format string
242 FORMAT, using the argument list in ARG.
243 Return the number of assignments made, or -1 for an input error. */
245 # ifdef COMPILE_WSCANF
247 _IO_vfwscanf (s, format, argptr, errp)
249 const wchar_t *format;
254 _IO_vfscanf (s, format, argptr, errp)
262 __vfscanf (FILE *s, const char *format, va_list argptr)
266 register const CHAR_T *f = format;
267 register UCHAR_T fc; /* Current character of the format. */
268 register size_t done = 0; /* Assignments done. */
269 register size_t read_in = 0; /* Chars read in. */
270 register WINT_T c = 0; /* Last char read. */
271 register int width; /* Maximum field width. */
272 register int flags; /* Modifiers for current format element. */
274 /* Status for reading F-P nums. */
275 char got_dot, got_e, negative;
276 /* If a [...] is a [^...]. */
278 #define exp_char not_in
279 /* Base for integral numbers. */
281 /* Signedness for integral numbers. */
283 #define is_hexa number_signed
284 /* Decimal point character. */
285 #ifdef COMPILE_WSCANF
290 /* The thousands character of the current locale. */
291 #ifdef COMPILE_WSCANF
294 const char *thousands;
296 /* State for the conversions. */
298 /* Integral holding variables. */
302 unsigned long long int uq;
304 unsigned long int ul;
306 /* Character-buffer pointer. */
308 wchar_t *wstr = NULL;
309 char **strptr = NULL;
311 /* We must not react on white spaces immediately because they can
312 possibly be matched even if in the input stream no character is
313 available anymore. */
315 /* Nonzero if we are reading a pointer. */
318 CHAR_T *tw; /* Temporary pointer. */
319 CHAR_T *wp = NULL; /* Workspace. */
320 size_t wpmax = 0; /* Maximal size of workspace. */
321 size_t wpsize; /* Currently used bytes in workspace. */
325 if (wpsize == wpmax) \
328 wpmax = (UCHAR_MAX + 1 > 2 * wpmax ? UCHAR_MAX + 1 : 2 * wpmax); \
329 wp = (CHAR_T *) alloca (wpmax * sizeof (wchar_t)); \
331 MEMCPY (wp, old, wpsize); \
333 wp[wpsize++] = (Ch); \
338 __va_copy (arg, argptr);
340 arg = (va_list) argptr;
347 ARGCHECK (s, format);
349 /* Figure out the decimal point character. */
350 #ifdef COMPILE_WSCANF
351 decimal = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
353 decimal = _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
355 /* Figure out the thousands separator character. */
356 #ifdef COMPILE_WSCANF
357 thousands = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_THOUSANDS_SEP_WC);
359 thousands = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
360 if (*thousands == '\0')
364 /* Lock the stream. */
368 #ifndef COMPILE_WSCANF
369 /* From now on we use `state' to convert the format string. */
370 memset (&state, '\0', sizeof (state));
373 /* Run through the format string. */
377 /* Extract the next argument, which is of type TYPE.
378 For a %N$... spec, this is the Nth argument from the beginning;
379 otherwise it is the next argument after the state now in ARG. */
381 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
382 ({ unsigned int pos = argpos; \
384 __va_copy (arg, argptr); \
386 (void) va_arg (arg, void *); \
387 va_arg (arg, type); \
391 /* XXX Possible optimization. */
392 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
393 ({ va_list arg = (va_list) argptr; \
394 arg = (va_list) ((char *) arg \
396 * __va_rounded_size (void *)); \
397 va_arg (arg, type); \
400 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
401 ({ unsigned int pos = argpos; \
402 va_list arg = (va_list) argptr; \
404 (void) va_arg (arg, void *); \
405 va_arg (arg, type); \
410 #ifndef COMPILE_WSCANF
411 if (!isascii ((unsigned char) *f))
413 /* Non-ASCII, may be a multibyte. */
414 int len = __mbrlen (f, strlen (f), &state);
422 else if (c != (unsigned char) *f++)
424 ungetc_not_eof (c, s);
437 /* Remember to skip spaces. */
444 /* Read a character. */
447 /* Characters other than format specs must just match. */
451 /* We saw white space char as the last character in the format
452 string. Now it's time to skip all leading white space. */
456 if (inchar () == EOF && errno == EINTR)
470 /* This is the start of the conversion string. */
473 /* Not yet decided whether we read a pointer or not. */
476 /* Initialize state of modifiers. */
479 /* Prepare temporary buffer. */
482 /* Check for a positional parameter specification. */
483 if (ISDIGIT ((UCHAR_T) *f))
485 argpos = (UCHAR_T) *f++ - L_('0');
486 while (ISDIGIT ((UCHAR_T) *f))
487 argpos = argpos * 10 + ((UCHAR_T) *f++ - L_('0'));
492 /* Oops; that was actually the field width. */
500 /* Check for the assignment-suppressing, the number grouping flag,
501 and the signal to use the locale's digit representation. */
502 while (*f == L_('*') || *f == L_('\'') || *f == L_('I'))
516 /* We have seen width. */
517 if (ISDIGIT ((UCHAR_T) *f))
520 /* Find the maximum field width. */
522 while (ISDIGIT ((UCHAR_T) *f))
525 width += (UCHAR_T) *f++ - L_('0');
531 /* Check for type modifiers. */
535 /* ints are short ints or chars. */
547 /* A double `l' is equivalent to an `L'. */
549 flags |= LONGDBL | LONG;
552 /* ints are long ints. */
557 /* doubles are long doubles, and ints are long long ints. */
558 flags |= LONGDBL | LONG;
561 /* The `a' is used as a flag only if followed by `s', `S' or
563 if (*f != L_('s') && *f != L_('S') && *f != L_('['))
568 /* String conversions (%s, %[) take a `char **'
569 arg and fill it in with a malloc'd pointer. */
573 if (need_longlong && sizeof (size_t) > sizeof (unsigned long int))
575 else if (sizeof (size_t) > sizeof (unsigned int))
579 if (need_longlong && sizeof (uintmax_t) > sizeof (unsigned long int))
581 else if (sizeof (uintmax_t) > sizeof (unsigned int))
585 if (need_longlong && sizeof (ptrdiff_t) > sizeof (long int))
587 else if (sizeof (ptrdiff_t) > sizeof (int))
591 /* Not a recognized modifier. Backup. */
596 /* End of the format string? */
600 /* Find the conversion specifier. */
602 if (skip_space || (fc != L_('[') && fc != L_('c')
603 && fc != L_('C') && fc != L_('n')))
605 /* Eat whitespace. */
606 int save_errno = errno;
609 if (inchar () == EOF && errno == EINTR)
619 case L_('%'): /* Must match a literal '%'. */
625 ungetc_not_eof (c, s);
630 case L_('n'): /* Answer number of assignments done. */
631 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
632 with the 'n' conversion specifier. */
633 if (!(flags & SUPPRESS))
635 /* Don't count the read-ahead. */
636 if (need_longlong && (flags & LONGDBL))
637 *ARG (long long int *) = read_in;
638 else if (need_long && (flags & LONG))
639 *ARG (long int *) = read_in;
640 else if (flags & SHORT)
641 *ARG (short int *) = read_in;
642 else if (!(flags & CHAR))
643 *ARG (int *) = read_in;
645 *ARG (char *) = read_in;
647 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
648 /* We have a severe problem here. The ISO C standard
649 contradicts itself in explaining the effect of the %n
650 format in `scanf'. While in ISO C:1990 and the ISO C
651 Amendement 1:1995 the result is described as
653 Execution of a %n directive does not effect the
654 assignment count returned at the completion of
655 execution of the f(w)scanf function.
657 in ISO C Corrigendum 1:1994 the following was added:
660 Add the following fourth example:
663 int d1, d2, n1, n2, i;
664 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
665 the value 123 is assigned to d1 and the value3 to n1.
666 Because %n can never get an input failure the value
667 of 3 is also assigned to n2. The value of d2 is not
668 affected. The value 3 is assigned to i.
670 We go for now with the historically correct code from ISO C,
671 i.e., we don't count the %n assignments. When it ever
672 should proof to be wrong just remove the #ifdef above. */
678 case L_('c'): /* Match characters. */
679 if ((flags & LONG) == 0)
681 if (!(flags & SUPPRESS))
695 #ifdef COMPILE_WSCANF
696 /* We have to convert the wide character(s) into multibyte
697 characters and store the result. */
698 memset (&state, '\0', sizeof (state));
704 n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c, &state);
705 if (n == (size_t) -1)
706 /* No valid wide character. */
709 /* Increment the output pointer. Even if we don't
713 while (--width > 0 && inchar () != EOF);
715 if (!(flags & SUPPRESS))
719 while (--width > 0 && inchar () != EOF);
722 while (--width > 0 && inchar () != EOF);
725 if (!(flags & SUPPRESS))
732 if (!(flags & SUPPRESS))
734 wstr = ARG (wchar_t *);
743 #ifdef COMPILE_WSCANF
744 /* Just store the incoming wide characters. */
745 if (!(flags & SUPPRESS))
749 while (--width > 0 && inchar () != EOF);
752 while (--width > 0 && inchar () != EOF);
755 /* We have to convert the multibyte input sequence to wide
757 char buf[MB_LEN_MAX];
760 memset (&cstate, '\0', sizeof (cstate));
766 /* This is what we present the mbrtowc function first. */
774 n = __mbrtowc (!(flags & SUPPRESS) ? wstr : NULL,
777 if (n == (size_t) -2)
779 /* Possibly correct character, just not enough
781 assert (cnt < MB_CUR_MAX);
783 if (inchar () == EOF)
793 /* We have a match. */
797 /* Advance the result pointer. */
800 while (--width > 0 && inchar () != EOF);
804 if (!(flags & SUPPRESS))
809 case L_('s'): /* Read a string. */
812 #define STRING_ARG(Str, Type) \
813 do if (!(flags & SUPPRESS)) \
815 if (flags & MALLOC) \
817 /* The string is to be stored in a malloc'd buffer. */ \
818 strptr = ARG (char **); \
819 if (strptr == NULL) \
821 /* Allocate an initial buffer. */ \
823 *strptr = (char *) malloc (strsize * sizeof (Type)); \
824 Str = (Type *) *strptr; \
827 Str = ARG (Type *); \
831 STRING_ARG (str, char);
837 #ifdef COMPILE_WSCANF
838 memset (&state, '\0', sizeof (state));
845 ungetc_not_eof (c, s);
849 #ifdef COMPILE_WSCANF
850 /* This is quite complicated. We have to convert the
851 wide characters into multibyte characters and then
856 if (!(flags & SUPPRESS) && (flags & MALLOC)
857 && str + MB_CUR_MAX >= *strptr + strsize)
859 /* We have to enlarge the buffer if the `a' flag
861 str = (char *) realloc (*strptr, strsize * 2);
864 /* Can't allocate that much. Last-ditch
866 str = (char *) realloc (*strptr, strsize + 1);
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))[strsize - 1] = '\0';
878 *strptr = (char *) str;
885 *strptr = (char *) str;
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 str = (char *) realloc (*strptr,
954 (str + n + 1) - *strptr);
957 /* We lose. Oh well. Terminate the string
958 and stop converting, so at least we don't
960 ((char *) (*strptr))[strsize - 1] = '\0';
966 *strptr = (char *) str;
967 str = ((char *) *strptr) + strsize;
968 strsize = (str + n + 1) - *strptr;
972 str = __mempcpy (str, buf, n);
976 if ((flags & MALLOC) && str - *strptr != strsize)
978 char *cp = (char *) realloc (*strptr, str - *strptr);
991 #ifndef COMPILE_WSCANF
995 /* Wide character string. */
996 STRING_ARG (wstr, wchar_t);
1002 #ifndef COMPILE_WSCANF
1003 memset (&cstate, '\0', sizeof (cstate));
1010 ungetc_not_eof (c, s);
1014 #ifdef COMPILE_WSCANF
1016 if (!(flags & SUPPRESS))
1019 if ((flags & MALLOC)
1020 && wstr == (wchar_t *) *strptr + strsize)
1022 /* Enlarge the buffer. */
1023 wstr = (wchar_t *) realloc (*strptr,
1025 * sizeof (wchar_t));
1028 /* Can't allocate that much. Last-ditch
1030 wstr = (wchar_t *) realloc (*strptr,
1032 + sizeof (wchar_t)));
1035 /* We lose. Oh well. Terminate the string
1036 and stop converting, so at least we don't
1038 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
1044 *strptr = (char *) wstr;
1051 *strptr = (char *) wstr;
1059 char buf[MB_LEN_MAX];
1069 n = __mbrtowc (!(flags & SUPPRESS) ? wstr : NULL,
1072 if (n == (size_t) -2)
1074 /* Possibly correct character, just not enough
1076 assert (cnt < MB_CUR_MAX);
1078 if (inchar () == EOF)
1088 /* We have a match. */
1092 if (!(flags & SUPPRESS) && (flags & MALLOC)
1093 && wstr == (wchar_t *) *strptr + strsize)
1095 /* Enlarge the buffer. */
1096 wstr = (wchar_t *) realloc (*strptr,
1098 * sizeof (wchar_t)));
1101 /* Can't allocate that much. Last-ditch effort. */
1102 wstr = (wchar_t *) realloc (*strptr,
1104 * sizeof (wchar_t)));
1107 /* We lose. Oh well. Terminate the
1108 string and stop converting, so at
1109 least we don't skip any input. */
1110 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
1116 *strptr = (char *) wstr;
1123 *strptr = (char *) wstr;
1131 while ((width <= 0 || --width > 0) && inchar () != EOF);
1133 if (!(flags & SUPPRESS))
1137 if ((flags & MALLOC) && wstr - (wchar_t *) *strptr != strsize)
1139 wchar_t *cp = (wchar_t *) realloc (*strptr,
1141 - (wchar_t *) *strptr)
1142 * sizeof(wchar_t)));
1144 *strptr = (char *) cp;
1152 case L_('x'): /* Hexadecimal integer. */
1153 case L_('X'): /* Ditto. */
1158 case L_('o'): /* Octal integer. */
1163 case L_('u'): /* Unsigned decimal integer. */
1168 case L_('d'): /* Signed decimal integer. */
1173 case L_('i'): /* Generic number. */
1182 /* Check for a sign. */
1183 if (c == L_('-') || c == L_('+'))
1191 /* Look for a leading indication of base. */
1192 if (width != 0 && c == L_('0'))
1200 if (width != 0 && TOLOWER (c) == L_('x'))
1218 if (base == 10 && (flags & I18N) != 0)
1223 #ifdef COMPILE_WSCANF
1224 const wchar_t *wcdigits[10];
1226 const char *mbdigits[10];
1231 #ifdef COMPILE_WSCANF
1232 to_level = _NL_CURRENT_WORD (LC_CTYPE,
1233 _NL_CTYPE_INDIGITS_WC_LEN) - 1;
1235 to_level = _NL_CURRENT_WORD (LC_CTYPE,
1236 _NL_CTYPE_INDIGITS_MB_LEN) - 1;
1239 /* Read the number into workspace. */
1240 while (c != EOF && width != 0)
1242 /* In this round we get the pointer to the digit strings
1243 and also perform the first round of comparisons. */
1244 for (n = 0; n < 10; ++n)
1246 /* Get the string for the digits with value N. */
1247 #ifdef COMPILE_WSCANF
1248 wcdigits[n] = (const wchar_t *)
1249 _NL_CURRENT (LC_CTYPE, _NL_CTYPE_INDIGITS0_WC + n);
1250 wcdigits[n] += from_level;
1252 if (c == *wcdigits[n])
1254 to_level = from_level;
1258 /* Advance the pointer to the next string. */
1262 int avail = width > 0 ? width : INT_MAX;
1264 mbdigits[n] = _NL_CURRENT (LC_CTYPE,
1265 _NL_CTYPE_INDIGITS0_MB + n);
1267 for (level = 0; level < from_level; level++)
1268 mbdigits[n] = strchr (mbdigits[n], '\0') + 1;
1271 while ((unsigned char) *cmpp == c && avail > 0)
1273 if (*++cmpp == '\0')
1277 if ((c = inchar ()) == EOF)
1287 to_level = from_level;
1291 /* We are pushing all read characters back. */
1292 if (cmpp > mbdigits[n])
1295 while (--cmpp > mbdigits[n])
1296 ungetc_not_eof ((unsigned char) *cmpp, s);
1297 c = (unsigned char) *cmpp;
1300 /* Advance the pointer to the next string. */
1301 mbdigits[n] = strchr (mbdigits[n], '\0') + 1;
1307 /* Have not yet found the digit. */
1308 for (level = from_level + 1; level <= to_level; ++level)
1310 /* Search all ten digits of this level. */
1311 for (n = 0; n < 10; ++n)
1313 #ifdef COMPILE_WSCANF
1314 if (c == *wcdigits[n])
1317 /* Advance the pointer to the next string. */
1321 int avail = width > 0 ? width : INT_MAX;
1324 while ((unsigned char) *cmpp == c && avail > 0)
1326 if (*++cmpp == '\0')
1330 if ((c = inchar ()) == EOF)
1343 /* We are pushing all read characters back. */
1344 if (cmpp > mbdigits[n])
1347 while (--cmpp > mbdigits[n])
1348 ungetc_not_eof ((unsigned char) *cmpp, s);
1349 c = (unsigned char) *cmpp;
1352 /* Advance the pointer to the next string. */
1353 mbdigits[n] = strchr (mbdigits[n], '\0') + 1;
1369 else if ((flags & GROUP)
1370 #ifdef COMPILE_WSCANF
1371 && thousands != L'\0'
1373 && thousands != NULL
1377 /* Try matching against the thousands separator. */
1378 #ifdef COMPILE_WSCANF
1382 const char *cmpp = thousands;
1383 int avail = width > 0 ? width : INT_MAX;
1385 while ((unsigned char) *cmpp == c && avail > 0)
1388 if (*++cmpp == '\0')
1392 if ((c = inchar ()) == EOF)
1400 /* We are pushing all read characters back. */
1401 if (cmpp > thousands)
1403 wpsize -= cmpp - thousands;
1405 while (--cmpp > thousands)
1406 ungetc_not_eof ((unsigned char) *cmpp, s);
1407 c = (unsigned char) *cmpp;
1415 /* The last thousands character will be added back by
1431 /* Read the number into workspace. */
1432 while (c != EOF && width != 0)
1439 else if (!ISDIGIT (c) || c - L_('0') >= base)
1441 if (base == 10 && (flags & GROUP)
1442 #ifdef COMPILE_WSCANF
1443 && thousands != L'\0'
1445 && thousands != NULL
1449 /* Try matching against the thousands separator. */
1450 #ifdef COMPILE_WSCANF
1454 const char *cmpp = thousands;
1455 int avail = width > 0 ? width : INT_MAX;
1457 while ((unsigned char) *cmpp == c && avail > 0)
1460 if (*++cmpp == '\0')
1464 if ((c = inchar ()) == EOF)
1472 /* We are pushing all read characters back. */
1473 if (cmpp > thousands)
1475 wpsize -= cmpp - thousands;
1477 while (--cmpp > thousands)
1478 ungetc_not_eof ((unsigned char) *cmpp, s);
1479 c = (unsigned char) *cmpp;
1487 /* The last thousands character will be added back by
1503 || (wpsize == 1 && (wp[0] == L_('+') || wp[0] == L_('-'))))
1505 /* There was no number. If we are supposed to read a pointer
1506 we must recognize "(nil)" as well. */
1507 if (wpsize == 0 && read_pointer && (width < 0 || width >= 0)
1509 && TOLOWER (inchar ()) == L_('n')
1510 && TOLOWER (inchar ()) == L_('i')
1511 && TOLOWER (inchar ()) == L_('l')
1512 && inchar () == L_(')'))
1513 /* We must produce the value of a NULL pointer. A single
1514 '0' digit is enough. */
1518 /* The last read character is not part of the number
1526 /* The just read character is not part of the number anymore. */
1529 /* Convert the number. */
1531 if (need_longlong && (flags & LONGDBL))
1534 num.q = __strtoll_internal (wp, &tw, base, flags & GROUP);
1536 num.uq = __strtoull_internal (wp, &tw, base, flags & GROUP);
1541 num.l = __strtol_internal (wp, &tw, base, flags & GROUP);
1543 num.ul = __strtoul_internal (wp, &tw, base, flags & GROUP);
1548 if (!(flags & SUPPRESS))
1550 if (! number_signed)
1552 if (need_longlong && (flags & LONGDBL))
1553 *ARG (unsigned LONGLONG int *) = num.uq;
1554 else if (need_long && (flags & LONG))
1555 *ARG (unsigned long int *) = num.ul;
1556 else if (flags & SHORT)
1557 *ARG (unsigned short int *)
1558 = (unsigned short int) num.ul;
1559 else if (!(flags & CHAR))
1560 *ARG (unsigned int *) = (unsigned int) num.ul;
1562 *ARG (unsigned char *) = (unsigned char) num.ul;
1566 if (need_longlong && (flags & LONGDBL))
1567 *ARG (LONGLONG int *) = num.q;
1568 else if (need_long && (flags & LONG))
1569 *ARG (long int *) = num.l;
1570 else if (flags & SHORT)
1571 *ARG (short int *) = (short int) num.l;
1572 else if (!(flags & CHAR))
1573 *ARG (int *) = (int) num.l;
1575 *ARG (signed char *) = (signed char) num.ul;
1581 case L_('e'): /* Floating-point numbers. */
1593 /* Check for a sign. */
1594 if (c == L_('-') || c == L_('+'))
1596 negative = c == L_('-');
1597 if (width == 0 || inchar () == EOF)
1598 /* EOF is only an input error before we read any chars. */
1602 #ifdef COMPILE_WSCANF
1605 /* This is no valid number. */
1610 /* Match against the decimal point. At this point
1611 we are taking advantage of the fact that we can
1612 push more than one character back. This is
1613 (almost) never necessary since the decimal point
1614 string hopefully never contains more than one
1616 const char *cmpp = decimal;
1617 int avail = width > 0 ? width : INT_MAX;
1619 while ((unsigned char) *cmpp == c && avail > 0)
1620 if (*++cmpp == '\0')
1624 if (inchar () == EOF)
1631 /* This is no valid number. */
1635 if (cmpp == decimal)
1637 c = (unsigned char) *--cmpp;
1652 /* Take care for the special arguments "nan" and "inf". */
1653 if (TOLOWER (c) == L_('n'))
1657 if (width == 0 || inchar () == EOF || TOLOWER (c) != L_('a'))
1662 if (width == 0 || inchar () == EOF || TOLOWER (c) != L_('n'))
1670 else if (TOLOWER (c) == L_('i'))
1672 /* Maybe "inf" or "infinity". */
1674 if (width == 0 || inchar () == EOF || TOLOWER (c) != L_('n'))
1679 if (width == 0 || inchar () == EOF || TOLOWER (c) != L_('f'))
1684 /* It is as least "inf". */
1685 if (width != 0 && inchar () != EOF)
1687 if (TOLOWER (c) == L_('i'))
1691 /* Now we have to read the rest as well. */
1693 if (width == 0 || inchar () == EOF
1694 || TOLOWER (c) != L_('n'))
1699 if (width == 0 || inchar () == EOF
1700 || TOLOWER (c) != L_('i'))
1705 if (width == 0 || inchar () == EOF
1706 || TOLOWER (c) != L_('t'))
1711 if (width == 0 || inchar () == EOF
1712 || TOLOWER (c) != L_('y'))
1727 if (width != 0 && c == L_('0'))
1733 if (width != 0 && TOLOWER (c) == L_('x'))
1735 /* It is a number in hexadecimal format. */
1741 /* Grouping is not allowed. */
1749 got_dot = got_e = 0;
1754 else if (!got_e && is_hexa && ISXDIGIT (c))
1756 else if (got_e && wp[wpsize - 1] == exp_char
1757 && (c == L_('-') || c == L_('+')))
1759 else if (wpsize > 0 && !got_e && TOLOWER (c) == exp_char)
1762 got_e = got_dot = 1;
1766 #ifdef COMPILE_WSCANF
1767 if (! got_dot && c == decimal)
1772 else if (thousands != L'\0' && ! got_dot && c == thousands)
1776 /* The last read character is not part of the number
1782 const char *cmpp = decimal;
1783 int avail = width > 0 ? width : INT_MAX;
1787 while ((unsigned char) *cmpp == c && avail > 0)
1788 if (*++cmpp == '\0')
1792 if (inchar () == EOF)
1800 /* Add all the characters. */
1801 for (cmpp = decimal; *cmpp != '\0'; ++cmpp)
1802 ADDW ((unsigned char) *cmpp);
1809 /* Figure out whether it is a thousands separator.
1810 There is one problem: we possibly read more than
1811 one character. We cannot push them back but since
1812 we know that parts of the `decimal' string matched,
1813 we can compare against it. */
1814 const char *cmp2p = thousands;
1816 if (thousands != NULL && ! got_dot)
1819 && *cmp2p == decimal[cmp2p - thousands])
1823 while ((unsigned char) *cmp2p == c && avail > 0)
1824 if (*++cmp2p == '\0')
1828 if (inchar () == EOF)
1835 if (cmp2p != NULL && *cmp2p == '\0')
1837 /* Add all the characters. */
1838 for (cmpp = thousands; *cmpp != '\0'; ++cmpp)
1839 ADDW ((unsigned char) *cmpp);
1845 /* The last read character is not part of the number
1856 while (width != 0 && inchar () != EOF);
1858 /* Have we read any character? If we try to read a number
1859 in hexadecimal notation and we have read only the `0x'
1860 prefix or no exponent this is an error. */
1861 if (wpsize == 0 || (is_hexa && (wpsize == 2 || ! got_e)))
1865 /* Convert the number. */
1867 if (flags & LONGDBL)
1869 long double d = __strtold_internal (wp, &tw, flags & GROUP);
1870 if (!(flags & SUPPRESS) && tw != wp)
1871 *ARG (long double *) = negative ? -d : d;
1873 else if (flags & LONG)
1875 double d = __strtod_internal (wp, &tw, flags & GROUP);
1876 if (!(flags & SUPPRESS) && tw != wp)
1877 *ARG (double *) = negative ? -d : d;
1881 float d = __strtof_internal (wp, &tw, flags & GROUP);
1882 if (!(flags & SUPPRESS) && tw != wp)
1883 *ARG (float *) = negative ? -d : d;
1889 if (!(flags & SUPPRESS))
1893 case L_('['): /* Character class. */
1895 STRING_ARG (wstr, wchar_t);
1897 STRING_ARG (str, char);
1908 /* There is no width given so there is also no limit on the
1909 number of characters we read. Therefore we set width to
1910 a very high value to make the algorithm easier. */
1913 #ifdef COMPILE_WSCANF
1914 /* Find the beginning and the end of the scanlist. We are not
1915 creating a lookup table since it would have to be too large.
1916 Instead we search each time through the string. This is not
1917 a constant lookup time but who uses this feature deserves to
1919 tw = (wchar_t *) f; /* Marks the beginning. */
1921 if (*f == ']' || *f == '-')
1924 while ((fc = *f++) != L'\0' && fc != L']');
1928 wp = (wchar_t *) f - 1;
1930 /* Fill WP with byte flags indexed by character.
1931 We will use this flag map for matching input characters. */
1932 if (wpmax < UCHAR_MAX + 1)
1934 wpmax = UCHAR_MAX + 1;
1935 wp = (char *) alloca (wpmax);
1937 memset (wp, '\0', UCHAR_MAX + 1);
1940 if (fc == ']' || fc == '-')
1942 /* If ] or - appears before any char in the set, it is not
1943 the terminator or separator, but the first char in the
1949 while ((fc = *f++) != '\0' && fc != ']')
1950 if (fc == '-' && *f != '\0' && *f != ']'
1951 && (unsigned char) f[-2] <= (unsigned char) *f)
1953 /* Add all characters from the one before the '-'
1954 up to (but not including) the next format char. */
1955 for (fc = (unsigned char) f[-2]; fc < (unsigned char) *f; ++fc)
1959 /* Add the character to the flag map. */
1968 size_t now = read_in;
1969 #ifdef COMPILE_WSCANF
1974 if (inchar () == WEOF)
1977 /* Test whether it's in the scanlist. */
1981 if (runp[0] == L'-' && runp[1] != '\0' && runp[1] != ']'
1983 && (unsigned int) runp[-1] <= (unsigned int) runp[1])
1985 /* Match against all characters in between the
1986 first and last character of the sequence. */
1989 for (wc = runp[-1] + 1; wc < runp[1]; ++wc)
1993 if (wc == runp[1] && !not_in)
1995 if (wc == runp[1] && not_in)
1997 /* The current character is not in the
2005 if (*runp == runp[1] && !not_in)
2007 if (*runp != runp[1] && not_in)
2017 if (!(flags & SUPPRESS))
2021 if ((flags & MALLOC)
2022 && wstr == (wchar_t *) *strptr + strsize)
2024 /* Enlarge the buffer. */
2025 wstr = (wchar_t *) realloc (*strptr,
2027 * sizeof (wchar_t));
2030 /* Can't allocate that much. Last-ditch
2033 realloc (*strptr, (strsize
2034 + sizeof (wchar_t)));
2037 /* We lose. Oh well. Terminate the string
2038 and stop converting, so at least we don't
2040 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
2046 *strptr = (char *) wstr;
2053 *strptr = (char *) wstr;
2060 while (--width > 0);
2063 char buf[MB_LEN_MAX];
2067 memset (&cstate, '\0', sizeof (cstate));
2072 if (inchar () == EOF)
2075 if (wp[c] == not_in)
2077 ungetc_not_eof (c, s);
2082 if (!(flags & SUPPRESS))
2086 /* Convert it into a wide character. */
2087 n = __mbrtowc (wstr, buf, cnt, &cstate);
2089 if (n == (size_t) -2)
2091 /* Possibly correct character, just not enough
2093 assert (cnt < MB_CUR_MAX);
2101 if ((flags & MALLOC)
2102 && wstr == (wchar_t *) *strptr + strsize)
2104 /* Enlarge the buffer. */
2105 wstr = (wchar_t *) realloc (*strptr,
2107 * sizeof (wchar_t)));
2110 /* Can't allocate that much. Last-ditch
2113 realloc (*strptr, ((strsize + 1)
2114 * sizeof (wchar_t)));
2117 /* We lose. Oh well. Terminate the
2118 string and stop converting,
2119 so at least we don't skip any input. */
2120 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
2126 *strptr = (char *) wstr;
2133 *strptr = (char *) wstr;
2140 while (--width > 0);
2143 /* We stopped in the middle of recognizing another
2144 character. That's a problem. */
2149 /* We haven't succesfully read any character. */
2152 if (!(flags & SUPPRESS))
2156 if ((flags & MALLOC)
2157 && wstr - (wchar_t *) *strptr != strsize)
2159 wchar_t *cp = (wchar_t *)
2160 realloc (*strptr, ((wstr - (wchar_t *) *strptr)
2161 * sizeof(wchar_t)));
2163 *strptr = (char *) cp;
2171 size_t now = read_in;
2172 #ifdef COMPILE_WSCANF
2174 memset (&state, '\0', sizeof (state));
2181 if (inchar () == WEOF)
2184 /* Test whether it's in the scanlist. */
2188 if (runp[0] == L'-' && runp[1] != '\0' && runp[1] != ']'
2190 && (unsigned int) runp[-1] <= (unsigned int) runp[1])
2192 /* Match against all characters in between the
2193 first and last character of the sequence. */
2196 for (wc = runp[-1] + 1; wc < runp[1]; ++wc)
2200 if (wc == runp[1] && !not_in)
2202 if (wc == runp[1] && not_in)
2204 /* The current character is not in the
2212 if (*runp == runp[1] && !not_in)
2214 if (*runp != runp[1] && not_in)
2224 if (!(flags & SUPPRESS))
2226 if ((flags & MALLOC)
2227 && str + MB_CUR_MAX >= *strptr + strsize)
2229 /* Enlarge the buffer. */
2230 str = (char *) realloc (*strptr, 2 * strsize);
2233 /* Can't allocate that much. Last-ditch
2235 str = (char *) realloc (*strptr, strsize + 1);
2238 /* We lose. Oh well. Terminate the string
2239 and stop converting, so at least we don't
2241 (*strptr)[strsize - 1] = '\0';
2261 n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c, &state);
2262 if (n == (size_t) -1)
2265 assert (n <= MB_CUR_MAX);
2268 while (--width > 0);
2273 if (inchar () == EOF)
2276 if (wp[c] == not_in)
2278 ungetc_not_eof (c, s);
2283 if (!(flags & SUPPRESS))
2286 if ((flags & MALLOC)
2287 && (char *) str == *strptr + strsize)
2289 /* Enlarge the buffer. */
2290 str = (char *) realloc (*strptr, 2 * strsize);
2293 /* Can't allocate that much. Last-ditch
2295 str = (char *) realloc (*strptr, strsize + 1);
2298 /* We lose. Oh well. Terminate the
2299 string and stop converting,
2300 so at least we don't skip any input. */
2301 ((char *) (*strptr))[strsize - 1] = '\0';
2307 *strptr = (char *) str;
2314 *strptr = (char *) str;
2321 while (--width > 0);
2325 /* We haven't succesfully read any character. */
2328 if (!(flags & SUPPRESS))
2330 #ifdef COMPILE_WSCANF
2331 /* We have to emit the code to get into the initial
2333 char buf[MB_LEN_MAX];
2334 size_t n = __wcrtomb (buf, L'\0', &state);
2335 if (n > 0 && (flags & MALLOC)
2336 && str + n >= *strptr + strsize)
2338 /* Enlarge the buffer. */
2339 str = (char *) realloc (*strptr,
2340 (str + n + 1) - *strptr);
2343 /* We lose. Oh well. Terminate the string
2344 and stop converting, so at least we don't
2346 ((char *) (*strptr))[strsize - 1] = '\0';
2352 *strptr = (char *) str;
2353 str = ((char *) *strptr) + strsize;
2354 strsize = (str + n + 1) - *strptr;
2358 str = __mempcpy (str, buf, n);
2362 if ((flags & MALLOC) && str - *strptr != strsize)
2364 char *cp = (char *) realloc (*strptr, str - *strptr);
2374 case L_('p'): /* Generic pointer. */
2376 /* A PTR must be the same size as a `long int'. */
2377 flags &= ~(SHORT|LONGDBL);
2385 /* If this is an unknown format character punt. */
2390 /* The last thing we saw int the format string was a white space.
2391 Consume the last white spaces. */
2396 while (ISSPACE (c));
2400 /* Unlock stream. */
2407 # ifdef COMPILE_WSCANF
2409 __vfwscanf (FILE *s, const wchar_t *format, va_list argptr)
2411 return _IO_vfwscanf (s, format, argptr, NULL);
2415 __vfscanf (FILE *s, const char *format, va_list argptr)
2417 return _IO_vfscanf (s, format, argptr, NULL);
2422 #ifdef COMPILE_WSCANF
2423 weak_alias (__vfwscanf, vfwscanf)
2425 weak_alias (__vfscanf, vfscanf)