1 /* Copyright (C) 1991,92,93,94,95,96,97,98 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. */
28 #include <bits/libc-lock.h>
29 #include <locale/localeinfo.h>
32 # define HAVE_LONGLONG
33 # define LONGLONG long long
35 # define LONGLONG long
38 /* Those are flags in the conversion format. */
39 # define LONG 0x001 /* l: long or double */
40 # define LONGDBL 0x002 /* L: long long or long double */
41 # define SHORT 0x004 /* h: short */
42 # define SUPPRESS 0x008 /* *: suppress assignment */
43 # define POINTER 0x010 /* weird %p pointer (`fake hex') */
44 # define NOSKIP 0x020 /* do not skip blanks */
45 # define WIDTH 0x040 /* width was given */
46 # define GROUP 0x080 /* ': group numbers */
47 # define MALLOC 0x100 /* a: malloc strings */
48 # define CHAR 0x200 /* hh: char */
56 # define va_list _IO_va_list
57 # define ungetc(c, s) ((void) ((int) c == EOF \
59 _IO_sputbackc (s, (unsigned char) c))))
60 # define inchar() (c == EOF ? EOF \
61 : ((c = _IO_getc_unlocked (s)), \
62 (void) (c != EOF && ++read_in), c))
63 # define encode_error() do { \
64 if (errp != NULL) *errp |= 4; \
65 _IO_funlockfile (s); \
66 __libc_cleanup_end (0); \
67 __set_errno (EILSEQ); \
70 # define conv_error() do { \
71 if (errp != NULL) *errp |= 2; \
72 _IO_funlockfile (s); \
73 __libc_cleanup_end (0); \
76 # define input_error() do { \
77 _IO_funlockfile (s); \
78 if (errp != NULL) *errp |= 1; \
79 __libc_cleanup_end (0); \
82 # define memory_error() do { \
83 _IO_funlockfile (s); \
84 __set_errno (ENOMEM); \
85 __libc_cleanup_end (0); \
88 # define ARGCHECK(s, format) \
91 /* Check file argument for consistence. */ \
92 CHECK_FILE (s, EOF); \
93 if (s->_flags & _IO_NO_READS) \
95 __set_errno (EBADF); \
98 else if (format == NULL) \
104 # define LOCK_STREAM(S) \
105 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, (S)); \
107 # define UNLOCK_STREAM(S) \
108 _IO_funlockfile (S); \
109 __libc_cleanup_region_end (0)
111 # define ungetc(c, s) ((void) (c != EOF && --read_in), ungetc (c, s))
112 # define inchar() (c == EOF ? EOF \
113 : ((c = getc (s)), (void) (c != EOF && ++read_in), c))
114 # define encode_error() do { \
116 __set_errno (EILSEQ); \
119 # define conv_error() do { \
123 # define input_error() do { \
125 return done ?: EOF; \
127 # define memory_error() do { \
129 __set_errno (ENOMEM); \
132 # define ARGCHECK(s, format) \
135 /* Check file argument for consistence. */ \
136 if (!__validfp (s) || !s->__mode.__read) \
138 __set_errno (EBADF); \
141 else if (format == NULL) \
143 __set_errno (EINVAL); \
148 /* XXX For now !!! */
149 # define flockfile(S) /* nothing */
150 # define funlockfile(S) /* nothing */
151 # define LOCK_STREAM(S)
152 # define UNLOCK_STREAM(S)
154 # define LOCK_STREAM(S) \
155 __libc_cleanup_region_start (&__funlockfile, (S)); \
157 # define UNLOCK_STREAM(S) \
159 __libc_cleanup_region_end (0)
164 /* Read formatted input from S according to the format string
165 FORMAT, using the argument list in ARG.
166 Return the number of assignments made, or -1 for an input error. */
169 _IO_vfscanf (s
, format
, argptr
, errp
)
176 __vfscanf (FILE *s
, const char *format
, va_list argptr
)
180 register const char *f
= format
;
181 register unsigned char fc
; /* Current character of the format. */
182 register size_t done
= 0; /* Assignments done. */
183 register size_t read_in
= 0; /* Chars read in. */
184 register int c
= 0; /* Last char read. */
185 register int width
; /* Maximum field width. */
186 register int flags
; /* Modifiers for current format element. */
188 /* Status for reading F-P nums. */
189 char got_dot
, got_e
, negative
;
190 /* If a [...] is a [^...]. */
192 #define exp_char not_in
193 /* Base for integral numbers. */
195 /* Signedness for integral numbers. */
197 #define is_hexa number_signed
198 /* Decimal point character. */
200 /* The thousands character of the current locale. */
202 /* Integral holding variables. */
206 unsigned long long int uq
;
208 unsigned long int ul
;
210 /* Character-buffer pointer. */
212 wchar_t *wstr
= NULL
;
213 char **strptr
= NULL
;
215 /* We must not react on white spaces immediately because they can
216 possibly be matched even if in the input stream no character is
217 available anymore. */
219 /* Nonzero if we are reading a pointer. */
222 char *tw
; /* Temporary pointer. */
223 char *wp
= NULL
; /* Workspace. */
224 size_t wpmax
= 0; /* Maximal size of workspace. */
225 size_t wpsize
; /* Currently used bytes in workspace. */
229 if (wpsize == wpmax) \
232 wpmax = UCHAR_MAX > 2 * wpmax ? UCHAR_MAX : 2 * wpmax; \
233 wp = (char *) alloca (wpmax); \
235 memcpy (wp, old, wpsize); \
237 wp[wpsize++] = (Ch); \
242 __va_copy (arg
, argptr
);
244 arg
= (va_list) argptr
;
247 ARGCHECK (s
, format
);
249 /* Figure out the decimal point character. */
250 if (mbtowc (&decimal
, _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
),
251 strlen (_NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
))) <= 0)
252 decimal
= (wchar_t) *_NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
253 /* Figure out the thousands separator character. */
254 if (mbtowc (&thousands
, _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
),
255 strlen (_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
))) <= 0)
256 thousands
= (wchar_t) *_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
258 /* Lock the stream. */
261 /* Run through the format string. */
265 /* Extract the next argument, which is of type TYPE.
266 For a %N$... spec, this is the Nth argument from the beginning;
267 otherwise it is the next argument after the state now in ARG. */
269 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
270 ({ unsigned int pos = argpos; \
272 __va_copy (arg, argptr); \
274 (void) va_arg (arg, void *); \
275 va_arg (arg, type); \
279 /* XXX Possible optimization. */
280 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
281 ({ va_list arg = (va_list) argptr; \
282 arg = (va_list) ((char *) arg \
284 * __va_rounded_size (void *)); \
285 va_arg (arg, type); \
288 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
289 ({ unsigned int pos = argpos; \
290 va_list arg = (va_list) argptr; \
292 (void) va_arg (arg, void *); \
293 va_arg (arg, type); \
300 /* Non-ASCII, may be a multibyte. */
301 int len
= mblen (f
, strlen (f
));
323 /* Remember to skip spaces. */
330 /* Read a character. */
333 /* Characters other than format specs must just match. */
337 /* We saw white space char as the last character in the format
338 string. Now it's time to skip all leading white space. */
342 if (inchar () == EOF
&& errno
== EINTR
)
356 /* This is the start of the conversion string. */
359 /* Not yet decided whether we read a pointer or not. */
362 /* Initialize state of modifiers. */
365 /* Prepare temporary buffer. */
368 /* Check for a positional parameter specification. */
373 argpos
= argpos
* 10 + (*f
++ - '0');
378 /* Oops; that was actually the field width. */
386 /* Check for the assignment-suppressing and the number grouping flag. */
387 while (*f
== '*' || *f
== '\'')
398 /* We have seen width. */
402 /* Find the maximum field width. */
413 /* Check for type modifiers. */
417 /* ints are short ints or chars. */
429 /* A double `l' is equivalent to an `L'. */
434 /* ints are long ints. */
439 /* doubles are long doubles, and ints are long long ints. */
443 /* The `a' is used as a flag only if followed by `s', `S' or
445 if (*f
!= 's' && *f
!= 'S' && *f
!= '[')
450 /* String conversions (%s, %[) take a `char **'
451 arg and fill it in with a malloc'd pointer. */
455 if (sizeof (size_t) > sizeof (unsigned long int))
457 else if (sizeof (size_t) > sizeof (unsigned int))
461 if (sizeof (uintmax_t) > sizeof (unsigned long int))
463 else if (sizeof (uintmax_t) > sizeof (unsigned int))
467 if (sizeof (ptrdiff_t) > sizeof (long int))
469 else if (sizeof (ptrdiff_t) > sizeof (int))
473 /* Not a recognized modifier. Backup. */
478 /* End of the format string? */
482 /* Find the conversion specifier. */
484 if (skip_space
|| (fc
!= '[' && fc
!= 'c' && fc
!= 'C' && fc
!= 'n'))
486 /* Eat whitespace. */
487 int save_errno
= errno
;
490 if (inchar () == EOF
&& errno
== EINTR
)
500 case '%': /* Must match a literal '%'. */
511 case 'n': /* Answer number of assignments done. */
512 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
513 with the 'n' conversion specifier. */
514 if (!(flags
& SUPPRESS
))
516 /* Don't count the read-ahead. */
518 *ARG (long long int *) = read_in
;
519 else if (flags
& LONG
)
520 *ARG (long int *) = read_in
;
521 else if (flags
& SHORT
)
522 *ARG (short int *) = read_in
;
524 *ARG (int *) = read_in
;
526 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
527 /* We have a severe problem here. The ISO C standard
528 contradicts itself in explaining the effect of the %n
529 format in `scanf'. While in ISO C:1990 and the ISO C
530 Amendement 1:1995 the result is described as
532 Execution of a %n directive does not effect the
533 assignment count returned at the completion of
534 execution of the f(w)scanf function.
536 in ISO C Corrigendum 1:1994 the following was added:
539 Add the following fourth example:
542 int d1, d2, n1, n2, i;
543 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
544 the value 123 is assigned to d1 and the value3 to n1.
545 Because %n can never get an input failure the value
546 of 3 is also assigned to n2. The value of d2 is not
547 affected. The value 3 is assigned to i.
549 We go for now with the historically correct code from ISO C,
550 i.e., we don't count the %n assignments. When it ever
551 should proof to be wrong just remove the #ifdef above. */
557 case 'c': /* Match characters. */
558 if ((flags
& LONG
) == 0)
560 if (!(flags
& SUPPRESS
))
574 if (!(flags
& SUPPRESS
))
578 while (--width
> 0 && inchar () != EOF
);
581 while (--width
> 0 && inchar () != EOF
);
583 if (!(flags
& SUPPRESS
))
590 /* Get UTF-8 encoded wide character. Here we assume (as in
591 other parts of the libc) that we only have to handle
598 if (!(flags
& SUPPRESS
))
600 wstr
= ARG (wchar_t *);
607 #define NEXT_WIDE_CHAR(First) \
611 /* EOF is only an error for the first character. */ \
620 if ((c & 0xc0) == 0x80 || (c & 0xfe) == 0xfe) \
622 if ((c & 0xe0) == 0xc0) \
624 /* We expect two bytes. */ \
628 else if ((c & 0xf0) == 0xe0) \
630 /* We expect three bytes. */ \
634 else if ((c & 0xf8) == 0xf0) \
636 /* We expect four bytes. */ \
640 else if ((c & 0xfc) == 0xf8) \
642 /* We expect five bytes. */ \
648 /* We expect six bytes. */ \
657 || (c & 0xc0) == 0x80 || (c & 0xfe) == 0xfe) \
665 if (!(flags & SUPPRESS)) \
669 NEXT_WIDE_CHAR (first
);
673 if (!(flags
& SUPPRESS
))
678 case 's': /* Read a string. */
680 /* We have to process a wide character string. */
681 goto wide_char_string
;
683 #define STRING_ARG(Str, Type) \
684 if (!(flags & SUPPRESS)) \
686 if (flags & MALLOC) \
688 /* The string is to be stored in a malloc'd buffer. */ \
689 strptr = ARG (char **); \
690 if (strptr == NULL) \
692 /* Allocate an initial buffer. */ \
694 *strptr = malloc (strsize * sizeof (Type)); \
695 Str = (Type *) *strptr; \
698 Str = ARG (Type *); \
702 STRING_ARG (str
, char);
715 #define STRING_ADD_CHAR(Str, c, Type) \
716 if (!(flags & SUPPRESS)) \
719 if ((flags & MALLOC) && (char *) Str == *strptr + strsize) \
721 /* Enlarge the buffer. */ \
722 Str = realloc (*strptr, strsize * 2 * sizeof (Type)); \
725 /* Can't allocate that much. Last-ditch effort. */\
726 Str = realloc (*strptr, \
727 (strsize + 1) * sizeof (Type)); \
730 /* We lose. Oh well. \
731 Terminate the string and stop converting, \
732 so at least we don't skip any input. */ \
733 ((Type *) (*strptr))[strsize] = '\0'; \
739 *strptr = (char *) Str; \
740 Str = ((Type *) *strptr) + strsize; \
746 *strptr = (char *) Str; \
747 Str = ((Type *) *strptr) + strsize; \
752 STRING_ADD_CHAR (str
, c
, char);
753 } while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
755 if (!(flags
& SUPPRESS
))
763 /* Wide character string. */
768 STRING_ARG (wstr
, wchar_t);
773 NEXT_WIDE_CHAR (first
);
777 /* XXX We would have to push back the whole wide char
778 with possibly many bytes. But since scanf does
779 not make a difference for white space characters
780 we can simply push back a simple <SP> which is
781 guaranteed to be in the [:space:] class. */
786 STRING_ADD_CHAR (wstr
, val
, wchar_t);
789 while (width
<= 0 || --width
> 0);
791 if (!(flags
& SUPPRESS
))
799 case 'x': /* Hexadecimal integer. */
800 case 'X': /* Ditto. */
805 case 'o': /* Octal integer. */
810 case 'u': /* Unsigned decimal integer. */
815 case 'd': /* Signed decimal integer. */
820 case 'i': /* Generic number. */
829 /* Check for a sign. */
830 if (c
== '-' || c
== '+')
838 /* Look for a leading indication of base. */
839 if (width
!= 0 && c
== '0')
847 if (width
!= 0 && tolower (c
) == 'x')
865 /* Read the number into workspace. */
866 while (c
!= EOF
&& width
!= 0)
868 if (base
== 16 ? !isxdigit (c
) :
869 ((!isdigit (c
) || c
- '0' >= base
) &&
870 !((flags
& GROUP
) && base
== 10 && c
== thousands
)))
880 (wpsize
== 1 && (wp
[0] == '+' || wp
[0] == '-')))
882 /* There was no number. If we are supposed to read a pointer
883 we must recognize "(nil)" as well. */
884 if (wpsize
== 0 && read_pointer
&& (width
< 0 || width
>= 0)
886 && tolower (inchar ()) == 'n'
887 && tolower (inchar ()) == 'i'
888 && tolower (inchar ()) == 'l'
890 /* We must produce the value of a NULL pointer. A single
891 '0' digit is enough. */
895 /* The last read character is not part of the number
903 /* The just read character is not part of the number anymore. */
906 /* Convert the number. */
911 num
.q
= __strtoll_internal (wp
, &tw
, base
, flags
& GROUP
);
913 num
.uq
= __strtoull_internal (wp
, &tw
, base
, flags
& GROUP
);
918 num
.l
= __strtol_internal (wp
, &tw
, base
, flags
& GROUP
);
920 num
.ul
= __strtoul_internal (wp
, &tw
, base
, flags
& GROUP
);
925 if (!(flags
& SUPPRESS
))
930 *ARG (unsigned LONGLONG
int *) = num
.uq
;
931 else if (flags
& LONG
)
932 *ARG (unsigned long int *) = num
.ul
;
933 else if (flags
& SHORT
)
934 *ARG (unsigned short int *)
935 = (unsigned short int) num
.ul
;
936 else if (flags
& CHAR
)
937 *ARG (unsigned char *) = (unsigned char) num
.ul
;
939 *ARG (unsigned int *) = (unsigned int) num
.ul
;
944 *ARG (LONGLONG
int *) = num
.q
;
945 else if (flags
& LONG
)
946 *ARG (long int *) = num
.l
;
947 else if (flags
& SHORT
)
948 *ARG (short int *) = (short int) num
.l
;
949 else if (flags
& CHAR
)
950 *ARG (signed char *) = (signed char) num
.ul
;
952 *ARG (int *) = (int) num
.l
;
958 case 'e': /* Floating-point numbers. */
969 /* Check for a sign. */
970 if (c
== '-' || c
== '+')
973 if (inchar () == EOF
)
974 /* EOF is only an input error before we read any chars. */
982 /* Take care for the special arguments "nan" and "inf". */
983 if (tolower (c
) == 'n')
987 if (inchar () == EOF
|| tolower (c
) != 'a')
990 if (inchar () == EOF
|| tolower (c
) != 'n')
996 else if (tolower (c
) == 'i')
998 /* Maybe "inf" or "infinity". */
1000 if (inchar () == EOF
|| tolower (c
) != 'n')
1003 if (inchar () == EOF
|| tolower (c
) != 'f')
1006 /* It is as least "inf". */
1007 if (inchar () != EOF
)
1009 if (tolower (c
) == 'i')
1011 /* No we have to read the rest as well. */
1013 if (inchar () == EOF
|| tolower (c
) != 'n')
1016 if (inchar () == EOF
|| tolower (c
) != 'i')
1019 if (inchar () == EOF
|| tolower (c
) != 't')
1022 if (inchar () == EOF
|| tolower (c
) != 'y')
1039 if (tolower (c
) == 'x')
1041 /* It is a number in hexadecimal format. */
1047 /* Grouping is not allowed. */
1053 got_dot
= got_e
= 0;
1058 else if (!got_e
&& is_hexa
&& isxdigit (c
))
1060 else if (got_e
&& wp
[wpsize
- 1] == exp_char
1061 && (c
== '-' || c
== '+'))
1063 else if (wpsize
> 0 && !got_e
&& tolower (c
) == exp_char
)
1066 got_e
= got_dot
= 1;
1068 else if (c
== decimal
&& !got_dot
)
1073 else if ((flags
& GROUP
) && c
== thousands
&& !got_dot
)
1077 /* The last read character is not part of the number
1085 while (width
!= 0 && inchar () != EOF
);
1087 /* Have we read any character? If we try to read a number
1088 in hexadecimal notation and we have read only the `0x'
1089 prefix this is an error. */
1090 if (wpsize
== 0 || (is_hexa
&& wpsize
== 2))
1094 /* Convert the number. */
1096 if (flags
& LONGDBL
)
1098 long double d
= __strtold_internal (wp
, &tw
, flags
& GROUP
);
1099 if (!(flags
& SUPPRESS
) && tw
!= wp
)
1100 *ARG (long double *) = negative
? -d
: d
;
1102 else if (flags
& LONG
)
1104 double d
= __strtod_internal (wp
, &tw
, flags
& GROUP
);
1105 if (!(flags
& SUPPRESS
) && tw
!= wp
)
1106 *ARG (double *) = negative
? -d
: d
;
1110 float d
= __strtof_internal (wp
, &tw
, flags
& GROUP
);
1111 if (!(flags
& SUPPRESS
) && tw
!= wp
)
1112 *ARG (float *) = negative
? -d
: d
;
1118 if (!(flags
& SUPPRESS
))
1122 case '[': /* Character class. */
1125 STRING_ARG (wstr
, wchar_t);
1126 c
= '\0'; /* This is to keep gcc quiet. */
1130 STRING_ARG (str
, char);
1145 /* Fill WP with byte flags indexed by character.
1146 We will use this flag map for matching input characters. */
1147 if (wpmax
< UCHAR_MAX
)
1150 wp
= (char *) alloca (wpmax
);
1152 memset (wp
, 0, UCHAR_MAX
);
1155 if (fc
== ']' || fc
== '-')
1157 /* If ] or - appears before any char in the set, it is not
1158 the terminator or separator, but the first char in the
1164 while ((fc
= *f
++) != '\0' && fc
!= ']')
1166 if (fc
== '-' && *f
!= '\0' && *f
!= ']' &&
1167 (unsigned char) f
[-2] <= (unsigned char) *f
)
1169 /* Add all characters from the one before the '-'
1170 up to (but not including) the next format char. */
1171 for (fc
= f
[-2]; fc
< *f
; ++fc
)
1175 /* Add the character to the flag map. */
1180 if (!(flags
& LONG
))
1193 NEXT_WIDE_CHAR (first
);
1194 if (val
<= 255 && wp
[val
] == not_in
)
1199 STRING_ADD_CHAR (wstr
, val
, wchar_t);
1209 if (!(flags
& SUPPRESS
))
1217 num
.ul
= read_in
- 1; /* -1 because we already read one char. */
1220 if (wp
[c
] == not_in
)
1225 STRING_ADD_CHAR (str
, c
, char);
1229 while (width
!= 0 && inchar () != EOF
);
1231 if (read_in
== num
.ul
)
1234 if (!(flags
& SUPPRESS
))
1242 case 'p': /* Generic pointer. */
1244 /* A PTR must be the same size as a `long int'. */
1245 flags
&= ~(SHORT
|LONGDBL
);
1252 /* If this is an unknown format character punt. */
1257 /* The last thing we saw int the format string was a white space.
1258 Consume the last white spaces. */
1263 while (isspace (c
));
1267 /* Unlock stream. */
1275 __vfscanf (FILE *s
, const char *format
, va_list argptr
)
1277 return _IO_vfscanf (s
, format
, argptr
, NULL
);
1281 weak_alias (__vfscanf
, vfscanf
)