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. */
27 #include <bits/libc-lock.h>
28 #include <locale/localeinfo.h>
31 # define HAVE_LONGLONG
32 # define LONGLONG long long
34 # define LONGLONG long
37 /* Those are flags in the conversion format. */
38 # define LONG 0x001 /* l: long or double */
39 # define LONGDBL 0x002 /* L: long long or long double */
40 # define SHORT 0x004 /* h: short */
41 # define SUPPRESS 0x008 /* *: suppress assignment */
42 # define POINTER 0x010 /* weird %p pointer (`fake hex') */
43 # define NOSKIP 0x020 /* do not skip blanks */
44 # define WIDTH 0x040 /* width was given */
45 # define GROUP 0x080 /* ': group numbers */
46 # define MALLOC 0x100 /* a: malloc strings */
47 # define CHAR 0x200 /* hh: char */
49 # define TYPEMOD (LONG|LONGDBL|SHORT|CHAR)
57 # define va_list _IO_va_list
58 # define ungetc(c, s) ((void) ((int) c == EOF \
60 _IO_sputbackc (s, (unsigned char) c))))
61 # define inchar() (c == EOF ? EOF \
62 : ((c = _IO_getc_unlocked (s)), \
63 (void) (c != EOF && ++read_in), c))
64 # define encode_error() do { \
65 if (errp != NULL) *errp |= 4; \
66 _IO_funlockfile (s); \
67 __libc_cleanup_end (0); \
68 __set_errno (EILSEQ); \
71 # define conv_error() do { \
72 if (errp != NULL) *errp |= 2; \
73 _IO_funlockfile (s); \
74 __libc_cleanup_end (0); \
77 # define input_error() do { \
78 _IO_funlockfile (s); \
79 if (errp != NULL) *errp |= 1; \
80 __libc_cleanup_end (0); \
83 # define memory_error() do { \
84 _IO_funlockfile (s); \
85 __set_errno (ENOMEM); \
86 __libc_cleanup_end (0); \
89 # define ARGCHECK(s, format) \
92 /* Check file argument for consistence. */ \
93 CHECK_FILE (s, EOF); \
94 if (s->_flags & _IO_NO_READS) \
96 __set_errno (EBADF); \
99 else if (format == NULL) \
105 # define LOCK_STREAM(S) \
106 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, (S)); \
108 # define UNLOCK_STREAM(S) \
109 _IO_funlockfile (S); \
110 __libc_cleanup_region_end (0)
112 # define ungetc(c, s) ((void) (c != EOF && --read_in), ungetc (c, s))
113 # define inchar() (c == EOF ? EOF \
114 : ((c = getc (s)), (void) (c != EOF && ++read_in), c))
115 # define encode_error() do { \
117 __set_errno (EILSEQ); \
120 # define conv_error() do { \
124 # define input_error() do { \
126 return done ?: EOF; \
128 # define memory_error() do { \
130 __set_errno (ENOMEM); \
133 # define ARGCHECK(s, format) \
136 /* Check file argument for consistence. */ \
137 if (!__validfp (s) || !s->__mode.__read) \
139 __set_errno (EBADF); \
142 else if (format == NULL) \
144 __set_errno (EINVAL); \
149 /* XXX For now !!! */
150 # define flockfile(S) /* nothing */
151 # define funlockfile(S) /* nothing */
152 # define LOCK_STREAM(S)
153 # define UNLOCK_STREAM(S)
155 # define LOCK_STREAM(S) \
156 __libc_cleanup_region_start (&__funlockfile, (S)); \
158 # define UNLOCK_STREAM(S) \
160 __libc_cleanup_region_end (0)
165 /* Read formatted input from S according to the format string
166 FORMAT, using the argument list in ARG.
167 Return the number of assignments made, or -1 for an input error. */
170 _IO_vfscanf (s
, format
, argptr
, errp
)
177 __vfscanf (FILE *s
, const char *format
, va_list argptr
)
181 register const char *f
= format
;
182 register unsigned char fc
; /* Current character of the format. */
183 register size_t done
= 0; /* Assignments done. */
184 register size_t read_in
= 0; /* Chars read in. */
185 register int c
= 0; /* Last char read. */
186 register int width
; /* Maximum field width. */
187 register int flags
; /* Modifiers for current format element. */
189 /* Status for reading F-P nums. */
190 char got_dot
, got_e
, negative
;
191 /* If a [...] is a [^...]. */
193 #define exp_char not_in
194 /* Base for integral numbers. */
196 /* Signedness for integral numbers. */
198 #define is_hexa number_signed
199 /* Decimal point character. */
201 /* The thousands character of the current locale. */
203 /* Integral holding variables. */
207 unsigned long long int uq
;
209 unsigned long int ul
;
211 /* Character-buffer pointer. */
213 wchar_t *wstr
= NULL
;
214 char **strptr
= NULL
;
216 /* We must not react on white spaces immediately because they can
217 possibly be matched even if in the input stream no character is
218 available anymore. */
221 char *tw
; /* Temporary pointer. */
222 char *wp
= NULL
; /* Workspace. */
223 size_t wpmax
= 0; /* Maximal size of workspace. */
224 size_t wpsize
; /* Currently used bytes in workspace. */
228 if (wpsize == wpmax) \
231 wpmax = UCHAR_MAX > 2 * wpmax ? UCHAR_MAX : 2 * wpmax; \
232 wp = (char *) alloca (wpmax); \
234 memcpy (wp, old, wpsize); \
236 wp[wpsize++] = (Ch); \
241 __va_copy (arg
, argptr
);
243 arg
= (va_list) argptr
;
246 ARGCHECK (s
, format
);
248 /* Figure out the decimal point character. */
249 if (mbtowc (&decimal
, _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
),
250 strlen (_NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
))) <= 0)
251 decimal
= (wchar_t) *_NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
252 /* Figure out the thousands separator character. */
253 if (mbtowc (&thousands
, _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
),
254 strlen (_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
))) <= 0)
255 thousands
= (wchar_t) *_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
257 /* Lock the stream. */
260 /* Run through the format string. */
264 /* Extract the next argument, which is of type TYPE.
265 For a %N$... spec, this is the Nth argument from the beginning;
266 otherwise it is the next argument after the state now in ARG. */
268 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
269 ({ unsigned int pos = argpos; \
271 __va_copy (arg, argptr); \
273 (void) va_arg (arg, void *); \
274 va_arg (arg, type); \
278 /* XXX Possible optimization. */
279 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
280 ({ va_list arg = (va_list) argptr; \
281 arg = (va_list) ((char *) arg \
283 * __va_rounded_size (void *)); \
284 va_arg (arg, type); \
287 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
288 ({ unsigned int pos = argpos; \
289 va_list arg = (va_list) argptr; \
291 (void) va_arg (arg, void *); \
292 va_arg (arg, type); \
299 /* Non-ASCII, may be a multibyte. */
300 int len
= mblen (f
, strlen (f
));
322 /* Remember to skip spaces. */
329 /* Read a character. */
332 /* Characters other than format specs must just match. */
336 /* We saw white space char as the last character in the format
337 string. Now it's time to skip all leading white space. */
341 if (inchar () == EOF
&& errno
== EINTR
)
355 /* This is the start of the conversion string. */
358 /* Initialize state of modifiers. */
361 /* Prepare temporary buffer. */
364 /* Check for a positional parameter specification. */
369 argpos
= argpos
* 10 + (*f
++ - '0');
374 /* Oops; that was actually the field width. */
382 /* Check for the assignment-suppressing and the number grouping flag. */
383 while (*f
== '*' || *f
== '\'')
394 /* We have seen width. */
398 /* Find the maximum field width. */
409 /* Check for type modifiers. */
410 while (*f
== 'h' || *f
== 'l' || *f
== 'L' || *f
== 'a' || *f
== 'q')
414 /* int's are short int's. */
415 if (flags
& (LONG
|LONGDBL
|CHAR
))
416 /* Signal illegal format element. */
427 if (flags
& (SHORT
|LONGDBL
|CHAR
))
429 else if (flags
& LONG
)
431 /* A double `l' is equivalent to an `L'. */
436 /* int's are long int's. */
441 /* double's are long double's, and int's are long long int's. */
443 /* Signal illegal format element. */
448 /* The `a' is used as a flag only if followed by `s', `S' or
450 if (*f
!= 's' && *f
!= 'S' && *f
!= '[')
456 /* Signal illegal format element. */
458 /* String conversions (%s, %[) take a `char **'
459 arg and fill it in with a malloc'd pointer. */
464 /* End of the format string? */
468 /* Find the conversion specifier. */
470 if (skip_space
|| (fc
!= '[' && fc
!= 'c' && fc
!= 'C' && fc
!= 'n'))
472 /* Eat whitespace. */
473 int save_errno
= errno
;
476 if (inchar () == EOF
&& errno
== EINTR
)
486 case '%': /* Must match a literal '%'. */
497 case 'n': /* Answer number of assignments done. */
498 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
499 with the 'n' conversion specifier. */
500 if (!(flags
& SUPPRESS
))
502 /* Don't count the read-ahead. */
504 *ARG (long long int *) = read_in
;
505 else if (flags
& LONG
)
506 *ARG (long int *) = read_in
;
507 else if (flags
& SHORT
)
508 *ARG (short int *) = read_in
;
510 *ARG (int *) = read_in
;
512 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
513 /* We have a severe problem here. The ISO C standard
514 contradicts itself in explaining the effect of the %n
515 format in `scanf'. While in ISO C:1990 and the ISO C
516 Amendement 1:1995 the result is described as
518 Execution of a %n directive does not effect the
519 assignment count returned at the completion of
520 execution of the f(w)scanf function.
522 in ISO C Corrigendum 1:1994 the following was added:
525 Add the following fourth example:
528 int d1, d2, n1, n2, i;
529 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
530 the value 123 is assigned to d1 and the value3 to n1.
531 Because %n can never get an input failure the value
532 of 3 is also assigned to n2. The value of d2 is not
533 affected. The value 3 is assigned to i.
535 We go for now with the historically correct code from ISO C,
536 i.e., we don't count the %n assignments. When it ever
537 should proof to be wrong just remove the #ifdef above. */
543 case 'c': /* Match characters. */
544 if ((flags
& LONG
) == 0)
546 if (!(flags
& SUPPRESS
))
560 if (!(flags
& SUPPRESS
))
564 while (--width
> 0 && inchar () != EOF
);
567 while (--width
> 0 && inchar () != EOF
);
569 if (!(flags
& SUPPRESS
))
576 /* Get UTF-8 encoded wide character. Here we assume (as in
577 other parts of the libc) that we only have to handle
584 if (!(flags
& SUPPRESS
))
586 wstr
= ARG (wchar_t *);
593 #define NEXT_WIDE_CHAR(First) \
596 /* EOF is only an error for the first character. */ \
604 if ((c & 0xc0) == 0x80 || (c & 0xfe) == 0xfe) \
606 if ((c & 0xe0) == 0xc0) \
608 /* We expect two bytes. */ \
612 else if ((c & 0xf0) == 0xe0) \
614 /* We expect three bytes. */ \
618 else if ((c & 0xf8) == 0xf0) \
620 /* We expect four bytes. */ \
624 else if ((c & 0xfc) == 0xf8) \
626 /* We expect five bytes. */ \
632 /* We expect six bytes. */ \
641 || (c & 0xc0) == 0x80 || (c & 0xfe) == 0xfe) \
649 if (!(flags & SUPPRESS)) \
653 NEXT_WIDE_CHAR (first
);
657 if (!(flags
& SUPPRESS
))
662 case 's': /* Read a string. */
664 /* We have to process a wide character string. */
665 goto wide_char_string
;
667 #define STRING_ARG(Str, Type) \
668 if (!(flags & SUPPRESS)) \
670 if (flags & MALLOC) \
672 /* The string is to be stored in a malloc'd buffer. */ \
673 strptr = ARG (char **); \
674 if (strptr == NULL) \
676 /* Allocate an initial buffer. */ \
678 *strptr = malloc (strsize * sizeof (Type)); \
679 Str = (Type *) *strptr; \
682 Str = ARG (Type *); \
686 STRING_ARG (str
, char);
699 #define STRING_ADD_CHAR(Str, c, Type) \
700 if (!(flags & SUPPRESS)) \
703 if ((flags & MALLOC) && (char *) Str == *strptr + strsize) \
705 /* Enlarge the buffer. */ \
706 Str = realloc (*strptr, strsize * 2 * sizeof (Type)); \
709 /* Can't allocate that much. Last-ditch effort. */\
710 Str = realloc (*strptr, \
711 (strsize + 1) * sizeof (Type)); \
714 /* We lose. Oh well. \
715 Terminate the string and stop converting, \
716 so at least we don't skip any input. */ \
717 ((Type *) (*strptr))[strsize] = '\0'; \
723 *strptr = (char *) Str; \
724 Str = ((Type *) *strptr) + strsize; \
730 *strptr = (char *) Str; \
731 Str = ((Type *) *strptr) + strsize; \
736 STRING_ADD_CHAR (str
, c
, char);
737 } while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
739 if (!(flags
& SUPPRESS
))
747 /* Wide character string. */
752 STRING_ARG (wstr
, wchar_t);
757 NEXT_WIDE_CHAR (first
);
761 /* XXX We would have to push back the whole wide char
762 with possibly many bytes. But since scanf does
763 not make a difference for white space characters
764 we can simply push back a simple <SP> which is
765 guaranteed to be in the [:space:] class. */
770 STRING_ADD_CHAR (wstr
, val
, wchar_t);
773 while (width
<= 0 || --width
> 0);
775 if (!(flags
& SUPPRESS
))
783 case 'x': /* Hexadecimal integer. */
784 case 'X': /* Ditto. */
789 case 'o': /* Octal integer. */
794 case 'u': /* Unsigned decimal integer. */
799 case 'd': /* Signed decimal integer. */
804 case 'i': /* Generic number. */
813 /* Check for a sign. */
814 if (c
== '-' || c
== '+')
822 /* Look for a leading indication of base. */
823 if (width
!= 0 && c
== '0')
831 if (width
!= 0 && tolower (c
) == 'x')
849 /* Read the number into workspace. */
850 while (c
!= EOF
&& width
!= 0)
852 if (base
== 16 ? !isxdigit (c
) :
853 ((!isdigit (c
) || c
- '0' >= base
) &&
854 !((flags
& GROUP
) && base
== 10 && c
== thousands
)))
863 /* The just read character is not part of the number anymore. */
867 (wpsize
== 1 && (wp
[0] == '+' || wp
[0] == '-')))
868 /* There was no number. */
871 /* Convert the number. */
876 num
.q
= __strtoll_internal (wp
, &tw
, base
, flags
& GROUP
);
878 num
.uq
= __strtoull_internal (wp
, &tw
, base
, flags
& GROUP
);
883 num
.l
= __strtol_internal (wp
, &tw
, base
, flags
& GROUP
);
885 num
.ul
= __strtoul_internal (wp
, &tw
, base
, flags
& GROUP
);
890 if (!(flags
& SUPPRESS
))
895 *ARG (unsigned LONGLONG
int *) = num
.uq
;
896 else if (flags
& LONG
)
897 *ARG (unsigned long int *) = num
.ul
;
898 else if (flags
& SHORT
)
899 *ARG (unsigned short int *)
900 = (unsigned short int) num
.ul
;
901 else if (flags
& CHAR
)
902 *ARG (unsigned char *) = (unsigned char) num
.ul
;
904 *ARG (unsigned int *) = (unsigned int) num
.ul
;
909 *ARG (LONGLONG
int *) = num
.q
;
910 else if (flags
& LONG
)
911 *ARG (long int *) = num
.l
;
912 else if (flags
& SHORT
)
913 *ARG (short int *) = (short int) num
.l
;
914 else if (flags
& CHAR
)
915 *ARG (signed char *) = (signed char) num
.ul
;
917 *ARG (int *) = (int) num
.l
;
923 case 'e': /* Floating-point numbers. */
934 /* Check for a sign. */
935 if (c
== '-' || c
== '+')
938 if (inchar () == EOF
)
939 /* EOF is only an input error before we read any chars. */
947 /* Take care for the special arguments "nan" and "inf". */
948 if (tolower (c
) == 'n')
952 if (inchar () == EOF
|| tolower (c
) != 'a')
955 if (inchar () == EOF
|| tolower (c
) != 'n')
961 else if (tolower (c
) == 'i')
963 /* Maybe "inf" or "infinity". */
965 if (inchar () == EOF
|| tolower (c
) != 'n')
968 if (inchar () == EOF
|| tolower (c
) != 'f')
971 /* It is as least "inf". */
972 if (inchar () != EOF
)
974 if (tolower (c
) == 'i')
976 /* No we have to read the rest as well. */
978 if (inchar () == EOF
|| tolower (c
) != 'n')
981 if (inchar () == EOF
|| tolower (c
) != 'i')
984 if (inchar () == EOF
|| tolower (c
) != 't')
987 if (inchar () == EOF
|| tolower (c
) != 'y')
1004 if (tolower (c
) == 'x')
1006 /* It is a number in hexadecimal format. */
1012 /* Grouping is not allowed. */
1018 got_dot
= got_e
= 0;
1023 else if (!got_e
&& is_hexa
&& isxdigit (c
))
1025 else if (got_e
&& wp
[wpsize
- 1] == exp_char
1026 && (c
== '-' || c
== '+'))
1028 else if (wpsize
> 0 && !got_e
&& tolower (c
) == exp_char
)
1031 got_e
= got_dot
= 1;
1033 else if (c
== decimal
&& !got_dot
)
1038 else if ((flags
& GROUP
) && c
== thousands
&& !got_dot
)
1042 /* The last read character is not part of the number
1050 while (width
!= 0 && inchar () != EOF
);
1052 /* Have we read any character? If we try to read a number
1053 in hexadecimal notation and we have read only the `0x'
1054 prefix this is an error. */
1055 if (wpsize
== 0 || (is_hexa
&& wpsize
== 2))
1059 /* Convert the number. */
1061 if (flags
& LONGDBL
)
1063 long double d
= __strtold_internal (wp
, &tw
, flags
& GROUP
);
1064 if (!(flags
& SUPPRESS
) && tw
!= wp
)
1065 *ARG (long double *) = negative
? -d
: d
;
1067 else if (flags
& LONG
)
1069 double d
= __strtod_internal (wp
, &tw
, flags
& GROUP
);
1070 if (!(flags
& SUPPRESS
) && tw
!= wp
)
1071 *ARG (double *) = negative
? -d
: d
;
1075 float d
= __strtof_internal (wp
, &tw
, flags
& GROUP
);
1076 if (!(flags
& SUPPRESS
) && tw
!= wp
)
1077 *ARG (float *) = negative
? -d
: d
;
1083 if (!(flags
& SUPPRESS
))
1087 case '[': /* Character class. */
1090 STRING_ARG (wstr
, wchar_t);
1091 c
= '\0'; /* This is to keep gcc quiet. */
1095 STRING_ARG (str
, char);
1110 /* Fill WP with byte flags indexed by character.
1111 We will use this flag map for matching input characters. */
1112 if (wpmax
< UCHAR_MAX
)
1115 wp
= (char *) alloca (wpmax
);
1117 memset (wp
, 0, UCHAR_MAX
);
1120 if (fc
== ']' || fc
== '-')
1122 /* If ] or - appears before any char in the set, it is not
1123 the terminator or separator, but the first char in the
1129 while ((fc
= *f
++) != '\0' && fc
!= ']')
1131 if (fc
== '-' && *f
!= '\0' && *f
!= ']' &&
1132 (unsigned char) f
[-2] <= (unsigned char) *f
)
1134 /* Add all characters from the one before the '-'
1135 up to (but not including) the next format char. */
1136 for (fc
= f
[-2]; fc
< *f
; ++fc
)
1140 /* Add the character to the flag map. */
1145 if (!(flags
& LONG
))
1158 NEXT_WIDE_CHAR (first
);
1159 if (val
<= 255 && wp
[val
] == not_in
)
1164 STRING_ADD_CHAR (wstr
, val
, wchar_t);
1174 if (!(flags
& SUPPRESS
))
1182 num
.ul
= read_in
- 1; /* -1 because we already read one char. */
1185 if (wp
[c
] == not_in
)
1190 STRING_ADD_CHAR (str
, c
, char);
1194 while (width
!= 0 && inchar () != EOF
);
1196 if (read_in
== num
.ul
)
1199 if (!(flags
& SUPPRESS
))
1207 case 'p': /* Generic pointer. */
1209 /* A PTR must be the same size as a `long int'. */
1210 flags
&= ~(SHORT
|LONGDBL
);
1217 /* The last thing we saw int the format string was a white space.
1218 Consume the last white spaces. */
1223 while (isspace (c
));
1227 /* Unlock stream. */
1235 __vfscanf (FILE *s
, const char *format
, va_list argptr
)
1237 return _IO_vfscanf (s
, format
, argptr
, NULL
);
1241 weak_alias (__vfscanf
, vfscanf
)