1 /* Copyright (C) 1991,92,93,94,95,96,97,98,99 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 */
72 # define va_list _IO_va_list
74 # ifdef COMPILE_WPRINTF
75 # define ungetc(c, s) ((void) (c == WEOF \
77 _IO_sputbackwc (s, c))))
78 # define inchar() (c == WEOF ? WEOF \
79 : ((c = _IO_getwc_unlocked (s)), \
80 (void) (c != WEOF && ++read_in), c))
82 # define MEMCPY(d, s, n) wmemcpy (d, s, n)
83 # define ISSPACE(Ch) iswspace (Ch)
84 # define ISDIGIT(Ch) iswdigit (Ch)
85 # define ISXDIGIT(Ch) iswxdigit (Ch)
86 # define TOLOWER(Ch) towlower (Ch)
87 # define ORIENT if (s->_vtable_offset == 0 && _IO_fwide (s, 1) != 1)\
89 # define __strtoll_internal __wcstoll_internal
90 # define __strtoull_internal __wcstoull_internal
91 # define __strtol_internal __wcstol_internal
92 # define __strtoul_internal __wcstoul_internal
93 # define __strtold_internal __wcstold_internal
94 # define __strtod_internal __wcstod_internal
95 # define __strtof_internal __wcstof_internal
97 # define L_(Str) L##Str
98 # define CHAR_T wchar_t
99 # define UCHAR_T unsigned int
100 # define WINT_T wint_t
102 # define ungetc(c, s) ((void) ((int) c == EOF \
104 _IO_sputbackc (s, (unsigned char) c))))
105 # define inchar() (c == EOF ? EOF \
106 : ((c = _IO_getc_unlocked (s)), \
107 (void) (c != EOF && ++read_in), c))
108 # define MEMCPY(d, s, n) memcpy (d, s, n)
109 # define ISSPACE(Ch) isspace (Ch)
110 # define ISDIGIT(Ch) isdigit (Ch)
111 # define ISXDIGIT(Ch) isxdigit (Ch)
112 # define TOLOWER(Ch) tolower (Ch)
113 # define ORIENT if (_IO_fwide (s, -1) != -1) return EOF
117 # define UCHAR_T unsigned char
121 # define encode_error() do { \
122 if (errp != NULL) *errp |= 4; \
123 _IO_funlockfile (s); \
124 __libc_cleanup_end (0); \
125 __set_errno (EILSEQ); \
128 # define conv_error() do { \
129 if (errp != NULL) *errp |= 2; \
130 _IO_funlockfile (s); \
131 __libc_cleanup_end (0); \
134 # define input_error() do { \
135 _IO_funlockfile (s); \
136 if (errp != NULL) *errp |= 1; \
137 __libc_cleanup_end (0); \
138 return done ?: EOF; \
140 # define memory_error() do { \
141 _IO_funlockfile (s); \
142 __set_errno (ENOMEM); \
143 __libc_cleanup_end (0); \
146 # define ARGCHECK(s, format) \
149 /* Check file argument for consistence. */ \
150 CHECK_FILE (s, EOF); \
151 if (s->_flags & _IO_NO_READS) \
153 __set_errno (EBADF); \
156 else if (format == NULL) \
162 # define LOCK_STREAM(S) \
163 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, (S)); \
165 # define UNLOCK_STREAM(S) \
166 _IO_funlockfile (S); \
167 __libc_cleanup_region_end (0)
169 # define ungetc(c, s) ((void) (c != EOF && --read_in), ungetc (c, s))
170 # define inchar() (c == EOF ? EOF \
171 : ((c = getc (s)), (void) (c != EOF && ++read_in), c))
172 # define MEMCPY(d, s, n) memcpy (d, s, n)
173 # define ISSPACE(Ch) isspace (Ch)
174 # define ISDIGIT(Ch) isdigit (Ch)
175 # define ISXDIGIT(Ch) isxdigit (Ch)
176 # define TOLOWER(Ch) tolower (Ch)
180 # define UCHAR_T unsigned char
183 # define encode_error() do { \
185 __set_errno (EILSEQ); \
188 # define conv_error() do { \
192 # define input_error() do { \
194 return done ?: EOF; \
196 # define memory_error() do { \
198 __set_errno (ENOMEM); \
201 # define ARGCHECK(s, format) \
204 /* Check file argument for consistence. */ \
205 if (!__validfp (s) || !s->__mode.__read) \
207 __set_errno (EBADF); \
210 else if (format == NULL) \
212 __set_errno (EINVAL); \
217 /* XXX For now !!! */
218 # define flockfile(S) /* nothing */
219 # define funlockfile(S) /* nothing */
220 # define LOCK_STREAM(S)
221 # define UNLOCK_STREAM(S)
223 # define LOCK_STREAM(S) \
224 __libc_cleanup_region_start (&__funlockfile, (S)); \
226 # define UNLOCK_STREAM(S) \
228 __libc_cleanup_region_end (0)
233 /* Read formatted input from S according to the format string
234 FORMAT, using the argument list in ARG.
235 Return the number of assignments made, or -1 for an input error. */
237 # ifdef COMPILE_WPRINTF
239 _IO_vfwscanf (s
, format
, argptr
, errp
)
241 const wchar_t *format
;
246 _IO_vfscanf (s
, format
, argptr
, errp
)
254 __vfscanf (FILE *s
, const char *format
, va_list argptr
)
258 register const CHAR_T
*f
= format
;
259 register UCHAR_T fc
; /* Current character of the format. */
260 register size_t done
= 0; /* Assignments done. */
261 register size_t read_in
= 0; /* Chars read in. */
262 register WINT_T c
= 0; /* Last char read. */
263 register int width
; /* Maximum field width. */
264 register int flags
; /* Modifiers for current format element. */
266 /* Status for reading F-P nums. */
267 char got_dot
, got_e
, negative
;
268 /* If a [...] is a [^...]. */
270 #define exp_char not_in
271 /* Base for integral numbers. */
273 /* Signedness for integral numbers. */
275 #define is_hexa number_signed
276 /* Decimal point character. */
278 /* The thousands character of the current locale. */
280 /* State for the conversions. */
282 /* Integral holding variables. */
286 unsigned long long int uq
;
288 unsigned long int ul
;
290 /* Character-buffer pointer. */
292 wchar_t *wstr
= NULL
;
293 char **strptr
= NULL
;
295 /* We must not react on white spaces immediately because they can
296 possibly be matched even if in the input stream no character is
297 available anymore. */
299 /* Nonzero if we are reading a pointer. */
302 CHAR_T
*tw
; /* Temporary pointer. */
303 CHAR_T
*wp
= NULL
; /* Workspace. */
304 size_t wpmax
= 0; /* Maximal size of workspace. */
305 size_t wpsize
; /* Currently used bytes in workspace. */
309 if (wpsize == wpmax) \
312 wpmax = UCHAR_MAX > 2 * wpmax ? UCHAR_MAX : 2 * wpmax; \
313 wp = (CHAR_T *) alloca (wpmax * sizeof (wchar_t)); \
315 MEMCPY (wp, old, wpsize); \
317 wp[wpsize++] = (Ch); \
322 __va_copy (arg
, argptr
);
324 arg
= (va_list) argptr
;
331 ARGCHECK (s
, format
);
333 /* Figure out the decimal point character. */
334 memset (&state
, '\0', sizeof (state
));
335 if (__mbrtowc (&decimal
, _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
),
336 strlen (_NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
)), &state
)
338 decimal
= (wchar_t) *_NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
339 /* Figure out the thousands separator character. */
340 memset (&state
, '\0', sizeof (state
));
341 if (__mbrtowc (&thousands
, _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
),
342 strlen (_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
)),
344 thousands
= (wchar_t) *_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
346 /* Lock the stream. */
350 #ifndef COMPILE_WPRINTF
351 /* From now on we use `state' to convert the format string. */
352 memset (&state
, '\0', sizeof (state
));
355 /* Run through the format string. */
359 /* Extract the next argument, which is of type TYPE.
360 For a %N$... spec, this is the Nth argument from the beginning;
361 otherwise it is the next argument after the state now in ARG. */
363 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
364 ({ unsigned int pos = argpos; \
366 __va_copy (arg, argptr); \
368 (void) va_arg (arg, void *); \
369 va_arg (arg, type); \
373 /* XXX Possible optimization. */
374 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
375 ({ va_list arg = (va_list) argptr; \
376 arg = (va_list) ((char *) arg \
378 * __va_rounded_size (void *)); \
379 va_arg (arg, type); \
382 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
383 ({ unsigned int pos = argpos; \
384 va_list arg = (va_list) argptr; \
386 (void) va_arg (arg, void *); \
387 va_arg (arg, type); \
392 #ifndef COMPILE_WPRINTF
395 /* Non-ASCII, may be a multibyte. */
396 int len
= __mbrlen (f
, strlen (f
), &state
);
419 /* Remember to skip spaces. */
426 /* Read a character. */
429 /* Characters other than format specs must just match. */
433 /* We saw white space char as the last character in the format
434 string. Now it's time to skip all leading white space. */
438 if (inchar () == EOF
&& errno
== EINTR
)
452 /* This is the start of the conversion string. */
455 /* Not yet decided whether we read a pointer or not. */
458 /* Initialize state of modifiers. */
461 /* Prepare temporary buffer. */
464 /* Check for a positional parameter specification. */
467 argpos
= *f
++ - L_('0');
469 argpos
= argpos
* 10 + (*f
++ - L_('0'));
474 /* Oops; that was actually the field width. */
482 /* Check for the assignment-suppressing and the number grouping flag. */
483 while (*f
== L_('*') || *f
== L_('\''))
494 /* We have seen width. */
498 /* Find the maximum field width. */
503 width
+= *f
++ - L_('0');
509 /* Check for type modifiers. */
513 /* ints are short ints or chars. */
525 /* A double `l' is equivalent to an `L'. */
527 flags
|= LONGDBL
| LONG
;
530 /* ints are long ints. */
535 /* doubles are long doubles, and ints are long long ints. */
536 flags
|= LONGDBL
| LONG
;
539 /* The `a' is used as a flag only if followed by `s', `S' or
541 if (*f
!= L_('s') && *f
!= L_('S') && *f
!= L_('['))
546 /* String conversions (%s, %[) take a `char **'
547 arg and fill it in with a malloc'd pointer. */
551 if (need_longlong
&& sizeof (size_t) > sizeof (unsigned long int))
553 else if (sizeof (size_t) > sizeof (unsigned int))
557 if (need_longlong
&& sizeof (uintmax_t) > sizeof (unsigned long int))
559 else if (sizeof (uintmax_t) > sizeof (unsigned int))
563 if (need_longlong
&& sizeof (ptrdiff_t) > sizeof (long int))
565 else if (sizeof (ptrdiff_t) > sizeof (int))
569 /* Not a recognized modifier. Backup. */
574 /* End of the format string? */
578 /* Find the conversion specifier. */
580 if (skip_space
|| (fc
!= L_('[') && fc
!= L_('c')
581 && fc
!= L_('C') && fc
!= L_('n')))
583 /* Eat whitespace. */
584 int save_errno
= errno
;
587 if (inchar () == EOF
&& errno
== EINTR
)
597 case L_('%'): /* Must match a literal '%'. */
608 case L_('n'): /* Answer number of assignments done. */
609 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
610 with the 'n' conversion specifier. */
611 if (!(flags
& SUPPRESS
))
613 /* Don't count the read-ahead. */
614 if (need_longlong
&& (flags
& LONGDBL
))
615 *ARG (long long int *) = read_in
;
616 else if (need_long
&& (flags
& LONG
))
617 *ARG (long int *) = read_in
;
618 else if (flags
& SHORT
)
619 *ARG (short int *) = read_in
;
620 else if (!(flags
& CHAR
))
621 *ARG (int *) = read_in
;
623 *ARG (char *) = read_in
;
625 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
626 /* We have a severe problem here. The ISO C standard
627 contradicts itself in explaining the effect of the %n
628 format in `scanf'. While in ISO C:1990 and the ISO C
629 Amendement 1:1995 the result is described as
631 Execution of a %n directive does not effect the
632 assignment count returned at the completion of
633 execution of the f(w)scanf function.
635 in ISO C Corrigendum 1:1994 the following was added:
638 Add the following fourth example:
641 int d1, d2, n1, n2, i;
642 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
643 the value 123 is assigned to d1 and the value3 to n1.
644 Because %n can never get an input failure the value
645 of 3 is also assigned to n2. The value of d2 is not
646 affected. The value 3 is assigned to i.
648 We go for now with the historically correct code from ISO C,
649 i.e., we don't count the %n assignments. When it ever
650 should proof to be wrong just remove the #ifdef above. */
656 case L_('c'): /* Match characters. */
657 if ((flags
& LONG
) == 0)
659 if (!(flags
& SUPPRESS
))
673 #ifdef COMPILE_WPRINTF
674 /* We have to convert the wide character(s) into multibyte
675 characters and store the result. */
676 memset (&state
, '\0', sizeof (state
));
682 n
= wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
683 if (n
== (size_t) -1)
684 /* No valid wide character. */
687 /* Increment the output pointer. Even if we don't
691 while (--width
> 0 && inchar () != EOF
);
693 if (!(flags
& SUPPRESS
))
697 while (--width
> 0 && inchar () != EOF
);
700 while (--width
> 0 && inchar () != EOF
);
703 if (!(flags
& SUPPRESS
))
710 if (!(flags
& SUPPRESS
))
712 wstr
= ARG (wchar_t *);
721 #ifdef COMPILE_WPRINTF
722 /* Just store the incoming wide characters. */
723 if (!(flags
& SUPPRESS
))
727 while (--width
> 0 && inchar () != EOF
);
730 while (--width
> 0 && inchar () != EOF
);
733 /* We have to convert the multibyte input sequence to wide
735 char buf
[MB_LEN_MAX
];
738 memset (&cstate
, '\0', sizeof (cstate
));
744 /* This is what we present the mbrtowc function first. */
752 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
755 if (n
== (size_t) -2)
757 /* Possibly correct character, just not enough
759 assert (cnt
< MB_CUR_MAX
);
761 if (inchar () == EOF
)
771 /* We have a match. */
775 /* Advance the result pointer. */
778 while (--width
> 0 && inchar () != EOF
);
782 if (!(flags
& SUPPRESS
))
787 case L_('s'): /* Read a string. */
790 #define STRING_ARG(Str, Type) \
791 do if (!(flags & SUPPRESS)) \
793 if (flags & MALLOC) \
795 /* The string is to be stored in a malloc'd buffer. */ \
796 strptr = ARG (char **); \
797 if (strptr == NULL) \
799 /* Allocate an initial buffer. */ \
801 *strptr = (char *) malloc (strsize * sizeof (Type)); \
802 Str = (Type *) *strptr; \
805 Str = ARG (Type *); \
809 STRING_ARG (str
, char);
815 #ifdef COMPILE_WPRINTF
816 memset (&state
, '\0', sizeof (state
));
827 #ifdef COMPILE_WPRINTF
828 /* This is quite complicated. We have to convert the
829 wide characters into multibyte characters and then
834 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
835 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
837 /* We have to enlarge the buffer if the `a' flag
839 str
= (char *) realloc (*strptr
, strsize
* 2);
842 /* Can't allocate that much. Last-ditch
844 str
= (char *) realloc (*strptr
, strsize
+ 1);
847 /* We lose. Oh well. Terminate the
848 string and stop converting,
849 so at least we don't skip any input. */
850 ((char *) (*strptr
))[strsize
- 1] = '\0';
856 *strptr
= (char *) str
;
863 *strptr
= (char *) str
;
869 n
= wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
870 if (n
== (size_t) -1)
873 assert (n
<= MB_CUR_MAX
);
878 if (!(flags
& SUPPRESS
))
882 && (char *) str
== *strptr
+ strsize
)
884 /* Enlarge the buffer. */
885 str
= (char *) realloc (*strptr
, 2 * strsize
);
888 /* Can't allocate that much. Last-ditch
890 str
= (char *) realloc (*strptr
, strsize
+ 1);
893 /* We lose. Oh well. Terminate the
894 string and stop converting,
895 so at least we don't skip any input. */
896 ((char *) (*strptr
))[strsize
- 1] = '\0';
902 *strptr
= (char *) str
;
909 *strptr
= (char *) str
;
917 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
919 if (!(flags
& SUPPRESS
))
921 #ifdef COMPILE_WPRINTF
922 /* We have to emit the code to get into the intial
924 char buf
[MB_LEN_MAX
];
925 size_t n
= wcrtomb (buf
, L
'\0', &state
);
926 if (n
> 0 && (flags
& MALLOC
)
927 && str
+ n
>= *strptr
+ strsize
)
929 /* Enlarge the buffer. */
930 str
= (char *) realloc (*strptr
,
931 (str
+ n
+ 1) - *strptr
);
934 /* We lose. Oh well. Terminate the string
935 and stop converting, so at least we don't
937 ((char *) (*strptr
))[strsize
- 1] = '\0';
943 *strptr
= (char *) str
;
944 str
= ((char *) *strptr
) + strsize
;
945 strsize
= (str
+ n
+ 1) - *strptr
;
949 str
= __mempcpy (str
, buf
, n
);
953 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
955 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
968 #ifndef COMPILE_WPRINTF
972 /* Wide character string. */
973 STRING_ARG (wstr
, wchar_t);
979 #ifndef COMPILE_WPRINTF
980 memset (&cstate
, '\0', sizeof (cstate
));
991 #ifdef COMPILE_WPRINTF
993 if (!(flags
& SUPPRESS
))
997 && wstr
== (wchar_t *) *strptr
+ strsize
)
999 /* Enlarge the buffer. */
1000 wstr
= (wchar_t *) realloc (*strptr
,
1002 * sizeof (wchar_t));
1005 /* Can't allocate that much. Last-ditch
1007 wstr
= (wchar_t *) realloc (*strptr
,
1009 + sizeof (wchar_t)));
1012 /* We lose. Oh well. Terminate the string
1013 and stop converting, so at least we don't
1015 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1021 *strptr
= (char *) wstr
;
1028 *strptr
= (char *) wstr
;
1036 char buf
[MB_LEN_MAX
];
1046 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
1049 if (n
== (size_t) -2)
1051 /* Possibly correct character, just not enough
1053 assert (cnt
< MB_CUR_MAX
);
1055 if (inchar () == EOF
)
1065 /* We have a match. */
1069 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
1070 && wstr
== (wchar_t *) *strptr
+ strsize
)
1072 /* Enlarge the buffer. */
1073 wstr
= (wchar_t *) realloc (*strptr
,
1075 * sizeof (wchar_t)));
1078 /* Can't allocate that much. Last-ditch effort. */
1079 wstr
= (wchar_t *) realloc (*strptr
,
1081 * sizeof (wchar_t)));
1084 /* We lose. Oh well. Terminate the
1085 string and stop converting, so at
1086 least we don't skip any input. */
1087 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1093 *strptr
= (char *) wstr
;
1100 *strptr
= (char *) wstr
;
1108 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
1110 if (!(flags
& SUPPRESS
))
1114 if ((flags
& MALLOC
) && wstr
- (wchar_t *) *strptr
!= strsize
)
1116 wchar_t *cp
= (wchar_t *) realloc (*strptr
,
1118 - (wchar_t *) *strptr
)
1119 * sizeof(wchar_t)));
1121 *strptr
= (char *) cp
;
1129 case L_('x'): /* Hexadecimal integer. */
1130 case L_('X'): /* Ditto. */
1135 case L_('o'): /* Octal integer. */
1140 case L_('u'): /* Unsigned decimal integer. */
1145 case L_('d'): /* Signed decimal integer. */
1150 case L_('i'): /* Generic number. */
1159 /* Check for a sign. */
1160 if (c
== L_('-') || c
== L_('+'))
1168 /* Look for a leading indication of base. */
1169 if (width
!= 0 && c
== L_('0'))
1177 if (width
!= 0 && TOLOWER (c
) == L_('x'))
1195 /* Read the number into workspace. */
1196 while (c
!= EOF
&& width
!= 0)
1198 if (base
== 16 ? !ISXDIGIT (c
) :
1199 ((!ISDIGIT (c
) || c
- L_('0') >= base
) &&
1200 !((flags
& GROUP
) && base
== 10 && c
== thousands
)))
1210 (wpsize
== 1 && (wp
[0] == L_('+') || wp
[0] == L_('-'))))
1212 /* There was no number. If we are supposed to read a pointer
1213 we must recognize "(nil)" as well. */
1214 if (wpsize
== 0 && read_pointer
&& (width
< 0 || width
>= 0)
1216 && TOLOWER (inchar ()) == L_('n')
1217 && TOLOWER (inchar ()) == L_('i')
1218 && TOLOWER (inchar ()) == L_('l')
1219 && inchar () == L_(')'))
1220 /* We must produce the value of a NULL pointer. A single
1221 '0' digit is enough. */
1225 /* The last read character is not part of the number
1233 /* The just read character is not part of the number anymore. */
1236 /* Convert the number. */
1238 if (need_longlong
&& (flags
& LONGDBL
))
1241 num
.q
= __strtoll_internal (wp
, &tw
, base
, flags
& GROUP
);
1243 num
.uq
= __strtoull_internal (wp
, &tw
, base
, flags
& GROUP
);
1248 num
.l
= __strtol_internal (wp
, &tw
, base
, flags
& GROUP
);
1250 num
.ul
= __strtoul_internal (wp
, &tw
, base
, flags
& GROUP
);
1255 if (!(flags
& SUPPRESS
))
1257 if (! number_signed
)
1259 if (need_longlong
&& (flags
& LONGDBL
))
1260 *ARG (unsigned LONGLONG
int *) = num
.uq
;
1261 else if (need_long
&& (flags
& LONG
))
1262 *ARG (unsigned long int *) = num
.ul
;
1263 else if (flags
& SHORT
)
1264 *ARG (unsigned short int *)
1265 = (unsigned short int) num
.ul
;
1266 else if (!(flags
& CHAR
))
1267 *ARG (unsigned int *) = (unsigned int) num
.ul
;
1269 *ARG (unsigned char *) = (unsigned char) num
.ul
;
1273 if (need_longlong
&& (flags
& LONGDBL
))
1274 *ARG (LONGLONG
int *) = num
.q
;
1275 else if (need_long
&& (flags
& LONG
))
1276 *ARG (long int *) = num
.l
;
1277 else if (flags
& SHORT
)
1278 *ARG (short int *) = (short int) num
.l
;
1279 else if (!(flags
& CHAR
))
1280 *ARG (int *) = (int) num
.l
;
1282 *ARG (signed char *) = (signed char) num
.ul
;
1288 case L_('e'): /* Floating-point numbers. */
1299 /* Check for a sign. */
1300 if (c
== L_('-') || c
== L_('+'))
1302 negative
= c
== L_('-');
1303 if (width
== 0 || inchar () == EOF
)
1304 /* EOF is only an input error before we read any chars. */
1306 if (! ISDIGIT (c
) && c
!= decimal
)
1308 /* This is no valid number. */
1318 /* Take care for the special arguments "nan" and "inf". */
1319 if (TOLOWER (c
) == L_('n'))
1323 if (width
== 0 || inchar () == EOF
|| TOLOWER (c
) != L_('a'))
1328 if (width
== 0 || inchar () == EOF
|| TOLOWER (c
) != L_('n'))
1336 else if (TOLOWER (c
) == L_('i'))
1338 /* Maybe "inf" or "infinity". */
1340 if (width
== 0 || inchar () == EOF
|| TOLOWER (c
) != L_('n'))
1345 if (width
== 0 || inchar () == EOF
|| TOLOWER (c
) != L_('f'))
1350 /* It is as least "inf". */
1351 if (width
!= 0 && inchar () != EOF
)
1353 if (TOLOWER (c
) == L_('i'))
1357 /* Now we have to read the rest as well. */
1359 if (width
== 0 || inchar () == EOF
1360 || TOLOWER (c
) != L_('n'))
1365 if (width
== 0 || inchar () == EOF
1366 || TOLOWER (c
) != L_('i'))
1371 if (width
== 0 || inchar () == EOF
1372 || TOLOWER (c
) != L_('t'))
1377 if (width
== 0 || inchar () == EOF
1378 || TOLOWER (c
) != L_('y'))
1393 if (width
!= 0 && c
== L_('0'))
1399 if (width
!= 0 && TOLOWER (c
) == L_('x'))
1401 /* It is a number in hexadecimal format. */
1407 /* Grouping is not allowed. */
1415 got_dot
= got_e
= 0;
1420 else if (!got_e
&& is_hexa
&& ISXDIGIT (c
))
1422 else if (got_e
&& wp
[wpsize
- 1] == exp_char
1423 && (c
== L_('-') || c
== L_('+')))
1425 else if (wpsize
> 0 && !got_e
&& TOLOWER (c
) == exp_char
)
1428 got_e
= got_dot
= 1;
1430 else if (c
== decimal
&& !got_dot
)
1435 else if ((flags
& GROUP
) && c
== thousands
&& !got_dot
)
1439 /* The last read character is not part of the number
1447 while (width
!= 0 && inchar () != EOF
);
1449 /* Have we read any character? If we try to read a number
1450 in hexadecimal notation and we have read only the `0x'
1451 prefix or no exponent this is an error. */
1452 if (wpsize
== 0 || (is_hexa
&& (wpsize
== 2 || ! got_e
)))
1456 /* Convert the number. */
1458 if (flags
& LONGDBL
)
1460 long double d
= __strtold_internal (wp
, &tw
, flags
& GROUP
);
1461 if (!(flags
& SUPPRESS
) && tw
!= wp
)
1462 *ARG (long double *) = negative
? -d
: d
;
1464 else if (flags
& LONG
)
1466 double d
= __strtod_internal (wp
, &tw
, flags
& GROUP
);
1467 if (!(flags
& SUPPRESS
) && tw
!= wp
)
1468 *ARG (double *) = negative
? -d
: d
;
1472 float d
= __strtof_internal (wp
, &tw
, flags
& GROUP
);
1473 if (!(flags
& SUPPRESS
) && tw
!= wp
)
1474 *ARG (float *) = negative
? -d
: d
;
1480 if (!(flags
& SUPPRESS
))
1484 case L_('['): /* Character class. */
1486 STRING_ARG (wstr
, wchar_t);
1488 STRING_ARG (str
, char);
1499 /* There is no width given so there is also no limit on the
1500 number of characters we read. Therefore we set width to
1501 a very high value to make the algorithm easier. */
1504 #ifdef COMPILE_WPRINTF
1505 /* Find the beginning and the end of the scanlist. We are not
1506 creating a lookup table since it would have to be too large.
1507 Instead we search each time through the string. This is not
1508 a constant lookup time but who uses this feature deserves to
1510 tw
= (wchar_t *) f
; /* Marks the beginning. */
1512 if (*f
== ']' || *f
== '-')
1515 while ((fc
= *f
++) != L
'\0' && fc
!= L
']');
1519 wp
= (wchar_t *) f
- 1;
1521 /* Fill WP with byte flags indexed by character.
1522 We will use this flag map for matching input characters. */
1523 if (wpmax
< UCHAR_MAX
)
1526 wp
= (char *) alloca (wpmax
);
1528 memset (wp
, '\0', UCHAR_MAX
);
1531 if (fc
== ']' || fc
== '-')
1533 /* If ] or - appears before any char in the set, it is not
1534 the terminator or separator, but the first char in the
1541 while ((fc
= *f
++) != '\0' && fc
!= ']')
1542 if (fc
== '-' && *f
!= '\0' && *f
!= ']' && f
- 2 != tw
1543 && (unsigned char) f
[-2] <= (unsigned char) *f
)
1545 /* Add all characters from the one before the '-'
1546 up to (but not including) the next format char. */
1547 for (fc
= f
[-2]; fc
< *f
; ++fc
)
1551 /* Add the character to the flag map. */
1560 size_t now
= read_in
;
1561 #ifdef COMPILE_WPRINTF
1566 if (inchar () == WEOF
)
1569 /* Test whether it's in the scanlist. */
1573 if (runp
[0] == L
'-' && runp
[1] != '\0' && runp
[1] != ']'
1575 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
1577 /* Match against all characters in between the
1578 first and last character of the sequence. */
1581 for (wc
= runp
[-1] + 1; wc
< runp
[1]; ++wc
)
1585 if (wc
== runp
[1] && !not_in
)
1587 if (wc
== runp
[1] && not_in
)
1589 /* The current character is not in the
1597 if (*runp
== runp
[1] && !not_in
)
1599 if (*runp
!= runp
[1] && not_in
)
1609 if (!(flags
& SUPPRESS
))
1613 if ((flags
& MALLOC
)
1614 && wstr
== (wchar_t *) *strptr
+ strsize
)
1616 /* Enlarge the buffer. */
1617 wstr
= (wchar_t *) realloc (*strptr
,
1619 * sizeof (wchar_t));
1622 /* Can't allocate that much. Last-ditch
1625 realloc (*strptr
, (strsize
1626 + sizeof (wchar_t)));
1629 /* We lose. Oh well. Terminate the string
1630 and stop converting, so at least we don't
1632 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1638 *strptr
= (char *) wstr
;
1645 *strptr
= (char *) wstr
;
1652 while (--width
> 0);
1655 char buf
[MB_LEN_MAX
];
1659 memset (&cstate
, '\0', sizeof (cstate
));
1664 if (inchar () == EOF
)
1667 if (wp
[c
] == not_in
)
1674 if (!(flags
& SUPPRESS
))
1678 /* Convert it into a wide character. */
1679 n
= __mbrtowc (wstr
, buf
, cnt
, &cstate
);
1681 if (n
== (size_t) -2)
1683 /* Possibly correct character, just not enough
1685 assert (cnt
< MB_CUR_MAX
);
1693 if ((flags
& MALLOC
)
1694 && wstr
== (wchar_t *) *strptr
+ strsize
)
1696 /* Enlarge the buffer. */
1697 wstr
= (wchar_t *) realloc (*strptr
,
1699 * sizeof (wchar_t)));
1702 /* Can't allocate that much. Last-ditch
1705 realloc (*strptr
, ((strsize
+ 1)
1706 * sizeof (wchar_t)));
1709 /* We lose. Oh well. Terminate the
1710 string and stop converting,
1711 so at least we don't skip any input. */
1712 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1718 *strptr
= (char *) wstr
;
1725 *strptr
= (char *) wstr
;
1732 while (--width
> 0);
1735 /* We stopped in the middle of recognizing another
1736 character. That's a problem. */
1741 /* We haven't succesfully read any character. */
1744 if (!(flags
& SUPPRESS
))
1748 if ((flags
& MALLOC
)
1749 && wstr
- (wchar_t *) *strptr
!= strsize
)
1751 wchar_t *cp
= (wchar_t *)
1752 realloc (*strptr
, ((wstr
- (wchar_t *) *strptr
)
1753 * sizeof(wchar_t)));
1755 *strptr
= (char *) cp
;
1763 size_t now
= read_in
;
1764 #ifdef COMPILE_WPRINTF
1766 memset (&state
, '\0', sizeof (state
));
1773 if (inchar () == WEOF
)
1776 /* Test whether it's in the scanlist. */
1780 if (runp
[0] == L
'-' && runp
[1] != '\0' && runp
[1] != ']'
1782 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
1784 /* Match against all characters in between the
1785 first and last character of the sequence. */
1788 for (wc
= runp
[-1] + 1; wc
< runp
[1]; ++wc
)
1792 if (wc
== runp
[1] && !not_in
)
1794 if (wc
== runp
[1] && not_in
)
1796 /* The current character is not in the
1804 if (*runp
== runp
[1] && !not_in
)
1806 if (*runp
!= runp
[1] && not_in
)
1816 if (!(flags
& SUPPRESS
))
1818 if ((flags
& MALLOC
)
1819 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
1821 /* Enlarge the buffer. */
1822 str
= (char *) realloc (*strptr
, 2 * strsize
);
1825 /* Can't allocate that much. Last-ditch
1827 str
= (char *) realloc (*strptr
, strsize
+ 1);
1830 /* We lose. Oh well. Terminate the string
1831 and stop converting, so at least we don't
1833 (*strptr
)[strsize
- 1] = '\0';
1853 n
= wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
1854 if (n
== (size_t) -1)
1857 assert (n
<= MB_CUR_MAX
);
1860 while (--width
> 0);
1865 if (inchar () == EOF
)
1868 if (wp
[c
] == not_in
)
1875 if (!(flags
& SUPPRESS
))
1878 if ((flags
& MALLOC
)
1879 && (char *) str
== *strptr
+ strsize
)
1881 /* Enlarge the buffer. */
1882 str
= (char *) realloc (*strptr
, 2 * strsize
);
1885 /* Can't allocate that much. Last-ditch
1887 str
= (char *) realloc (*strptr
, strsize
+ 1);
1890 /* We lose. Oh well. Terminate the
1891 string and stop converting,
1892 so at least we don't skip any input. */
1893 ((char *) (*strptr
))[strsize
- 1] = '\0';
1899 *strptr
= (char *) str
;
1906 *strptr
= (char *) str
;
1913 while (--width
> 0);
1917 /* We haven't succesfully read any character. */
1920 if (!(flags
& SUPPRESS
))
1922 #ifdef COMPILE_WPRINTF
1923 /* We have to emit the code to get into the intial
1925 char buf
[MB_LEN_MAX
];
1926 size_t n
= wcrtomb (buf
, L
'\0', &state
);
1927 if (n
> 0 && (flags
& MALLOC
)
1928 && str
+ n
>= *strptr
+ strsize
)
1930 /* Enlarge the buffer. */
1931 str
= (char *) realloc (*strptr
,
1932 (str
+ n
+ 1) - *strptr
);
1935 /* We lose. Oh well. Terminate the string
1936 and stop converting, so at least we don't
1938 ((char *) (*strptr
))[strsize
- 1] = '\0';
1944 *strptr
= (char *) str
;
1945 str
= ((char *) *strptr
) + strsize
;
1946 strsize
= (str
+ n
+ 1) - *strptr
;
1950 str
= __mempcpy (str
, buf
, n
);
1954 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
1956 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
1966 case L_('p'): /* Generic pointer. */
1968 /* A PTR must be the same size as a `long int'. */
1969 flags
&= ~(SHORT
|LONGDBL
);
1977 /* If this is an unknown format character punt. */
1982 /* The last thing we saw int the format string was a white space.
1983 Consume the last white spaces. */
1988 while (ISSPACE (c
));
1992 /* Unlock stream. */
1999 # ifdef COMPILE_WPRINTF
2001 __vfwscanf (FILE *s
, const wchar_t *format
, va_list argptr
)
2003 return _IO_vfwscanf (s
, format
, argptr
, NULL
);
2007 __vfscanf (FILE *s
, const char *format
, va_list argptr
)
2009 return _IO_vfscanf (s
, format
, argptr
, NULL
);
2014 #ifdef COMPILE_WPRINTF
2015 weak_alias (__vfwscanf
, vfwscanf
)
2017 weak_alias (__vfscanf
, vfscanf
)