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 && --read_in), \
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 __set_errno (EILSEQ); \
69 # define conv_error() do { \
70 if (errp != NULL) *errp |= 2; \
71 _IO_funlockfile (s); \
74 # define input_error() do { \
75 _IO_funlockfile (s); \
76 if (errp != NULL) *errp |= 1; \
79 # define memory_error() do { \
80 _IO_funlockfile (s); \
81 __set_errno (ENOMEM); \
84 # define ARGCHECK(s, format) \
87 /* Check file argument for consistence. */ \
88 CHECK_FILE (s, EOF); \
89 if (s->_flags & _IO_NO_READS) \
91 __set_errno (EBADF); \
94 else if (format == NULL) \
100 # define LOCK_STREAM(S) \
101 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, (S)); \
103 # define UNLOCK_STREAM(S) \
104 _IO_funlockfile (S); \
105 __libc_cleanup_region_end (0)
107 # define ungetc(c, s) ((void) (c != EOF && --read_in), ungetc (c, s))
108 # define inchar() (c == EOF ? EOF \
109 : ((c = getc (s)), (void) (c != EOF && ++read_in), c))
110 # define encode_error() do { \
112 __set_errno (EILSEQ); \
115 # define conv_error() do { \
119 # define input_error() do { \
121 return done ?: EOF; \
123 # define memory_error() do { \
125 __set_errno (ENOMEM); \
128 # define ARGCHECK(s, format) \
131 /* Check file argument for consistence. */ \
132 if (!__validfp (s) || !s->__mode.__read) \
134 __set_errno (EBADF); \
137 else if (format == NULL) \
139 __set_errno (EINVAL); \
144 /* XXX For now !!! */
145 # define flockfile(S) /* nothing */
146 # define funlockfile(S) /* nothing */
147 # define LOCK_STREAM(S)
148 # define UNLOCK_STREAM(S)
150 # define LOCK_STREAM(S) \
151 __libc_cleanup_region_start (&__funlockfile, (S)); \
153 # define UNLOCK_STREAM(S) \
155 __libc_cleanup_region_end (0)
160 /* Read formatted input from S according to the format string
161 FORMAT, using the argument list in ARG.
162 Return the number of assignments made, or -1 for an input error. */
165 _IO_vfscanf (s
, format
, argptr
, errp
)
172 __vfscanf (FILE *s
, const char *format
, va_list argptr
)
176 register const char *f
= format
;
177 register unsigned char fc
; /* Current character of the format. */
178 register size_t done
= 0; /* Assignments done. */
179 register size_t read_in
= 0; /* Chars read in. */
180 register int c
= 0; /* Last char read. */
181 register int width
; /* Maximum field width. */
182 register int flags
; /* Modifiers for current format element. */
184 /* Status for reading F-P nums. */
185 char got_dot
, got_e
, negative
;
186 /* If a [...] is a [^...]. */
188 #define exp_char not_in
189 /* Base for integral numbers. */
191 /* Signedness for integral numbers. */
193 #define is_hexa number_signed
194 /* Decimal point character. */
196 /* The thousands character of the current locale. */
198 /* Integral holding variables. */
202 unsigned long long int uq
;
204 unsigned long int ul
;
206 /* Character-buffer pointer. */
208 wchar_t *wstr
= NULL
;
209 char **strptr
= NULL
;
211 /* We must not react on white spaces immediately because they can
212 possibly be matched even if in the input stream no character is
213 available anymore. */
216 char *tw
; /* Temporary pointer. */
217 char *wp
= NULL
; /* Workspace. */
218 size_t wpmax
= 0; /* Maximal size of workspace. */
219 size_t wpsize
; /* Currently used bytes in workspace. */
223 if (wpsize == wpmax) \
226 wpmax = UCHAR_MAX > 2 * wpmax ? UCHAR_MAX : 2 * wpmax; \
227 wp = (char *) alloca (wpmax); \
229 memcpy (wp, old, wpsize); \
231 wp[wpsize++] = (Ch); \
236 __va_copy (arg
, argptr
);
238 arg
= (va_list) argptr
;
241 ARGCHECK (s
, format
);
243 /* Figure out the decimal point character. */
244 if (mbtowc (&decimal
, _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
),
245 strlen (_NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
))) <= 0)
246 decimal
= (wchar_t) *_NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
247 /* Figure out the thousands separator character. */
248 if (mbtowc (&thousands
, _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
),
249 strlen (_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
))) <= 0)
250 thousands
= (wchar_t) *_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
252 /* Lock the stream. */
255 /* Run through the format string. */
259 /* Extract the next argument, which is of type TYPE.
260 For a %N$... spec, this is the Nth argument from the beginning;
261 otherwise it is the next argument after the state now in ARG. */
263 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
264 ({ unsigned int pos = argpos; \
266 __va_copy (arg, argptr); \
268 (void) va_arg (arg, void *); \
269 va_arg (arg, type); \
273 /* XXX Possible optimization. */
274 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
275 ({ va_list arg = (va_list) argptr; \
276 arg = (va_list) ((char *) arg \
278 * __va_rounded_size (void *)); \
279 va_arg (arg, type); \
282 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
283 ({ unsigned int pos = argpos; \
284 va_list arg = (va_list) argptr; \
286 (void) va_arg (arg, void *); \
287 va_arg (arg, type); \
294 /* Non-ASCII, may be a multibyte. */
295 int len
= mblen (f
, strlen (f
));
317 /* Remember to skip spaces. */
324 /* Read a character. */
327 /* Characters other than format specs must just match. */
331 /* We saw white space char as the last character in the format
332 string. Now it's time to skip all leading white space. */
336 if (inchar () == EOF
&& errno
== EINTR
)
350 /* This is the start of the conversion string. */
353 /* Initialize state of modifiers. */
356 /* Prepare temporary buffer. */
359 /* Check for a positional parameter specification. */
364 argpos
= argpos
* 10 + (*f
++ - '0');
369 /* Oops; that was actually the field width. */
377 /* Check for the assignment-suppressing and the number grouping flag. */
378 while (*f
== '*' || *f
== '\'')
389 /* We have seen width. */
393 /* Find the maximum field width. */
404 /* Check for type modifiers. */
405 while (*f
== 'h' || *f
== 'l' || *f
== 'L' || *f
== 'a' || *f
== 'q')
409 /* int's are short int's. */
410 if (flags
& (LONG
|LONGDBL
|CHAR
))
411 /* Signal illegal format element. */
422 if (flags
& (SHORT
|LONGDBL
|CHAR
))
424 else if (flags
& LONG
)
426 /* A double `l' is equivalent to an `L'. */
431 /* int's are long int's. */
436 /* double's are long double's, and int's are long long int's. */
438 /* Signal illegal format element. */
443 /* The `a' is used as a flag only if followed by `s', `S' or
445 if (*f
!= 's' && *f
!= 'S' && *f
!= '[')
451 /* Signal illegal format element. */
453 /* String conversions (%s, %[) take a `char **'
454 arg and fill it in with a malloc'd pointer. */
459 /* End of the format string? */
463 /* Find the conversion specifier. */
465 if (skip_space
|| (fc
!= '[' && fc
!= 'c' && fc
!= 'C' && fc
!= 'n'))
467 /* Eat whitespace. */
468 int save_errno
= errno
;
471 if (inchar () == EOF
&& errno
== EINTR
)
481 case '%': /* Must match a literal '%'. */
492 case 'n': /* Answer number of assignments done. */
493 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
494 with the 'n' conversion specifier. */
495 if (!(flags
& SUPPRESS
))
497 /* Don't count the read-ahead. */
499 *ARG (long long int *) = read_in
;
500 else if (flags
& LONG
)
501 *ARG (long int *) = read_in
;
502 else if (flags
& SHORT
)
503 *ARG (short int *) = read_in
;
505 *ARG (int *) = read_in
;
507 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
508 /* We have a severe problem here. The ISO C standard
509 contradicts itself in explaining the effect of the %n
510 format in `scanf'. While in ISO C:1990 and the ISO C
511 Amendement 1:1995 the result is described as
513 Execution of a %n directive does not effect the
514 assignment count returned at the completion of
515 execution of the f(w)scanf function.
517 in ISO C Corrigendum 1:1994 the following was added:
520 Add the following fourth example:
523 int d1, d2, n1, n2, i;
524 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
525 the value 123 is assigned to d1 and the value3 to n1.
526 Because %n can never get an input failure the value
527 of 3 is also assigned to n2. The value of d2 is not
528 affected. The value 3 is assigned to i.
530 We go for now with the historically correct code from ISO C,
531 i.e., we don't count the %n assignments. When it ever
532 should proof to be wrong just remove the #ifdef above. */
538 case 'c': /* Match characters. */
539 if ((flags
& LONG
) == 0)
541 if (!(flags
& SUPPRESS
))
555 if (!(flags
& SUPPRESS
))
559 while (--width
> 0 && inchar () != EOF
);
562 while (--width
> 0 && inchar () != EOF
);
564 if (!(flags
& SUPPRESS
))
571 /* Get UTF-8 encoded wide character. Here we assume (as in
572 other parts of the libc) that we only have to handle
579 if (!(flags
& SUPPRESS
))
581 wstr
= ARG (wchar_t *);
588 #define NEXT_WIDE_CHAR(First) \
591 /* EOF is only an error for the first character. */ \
599 if ((c & 0xc0) == 0x80 || (c & 0xfe) == 0xfe) \
601 if ((c & 0xe0) == 0xc0) \
603 /* We expect two bytes. */ \
607 else if ((c & 0xf0) == 0xe0) \
609 /* We expect three bytes. */ \
613 else if ((c & 0xf8) == 0xf0) \
615 /* We expect four bytes. */ \
619 else if ((c & 0xfc) == 0xf8) \
621 /* We expect five bytes. */ \
627 /* We expect six bytes. */ \
636 || (c & 0xc0) == 0x80 || (c & 0xfe) == 0xfe) \
644 if (!(flags & SUPPRESS)) \
648 NEXT_WIDE_CHAR (first
);
652 if (!(flags
& SUPPRESS
))
657 case 's': /* Read a string. */
659 /* We have to process a wide character string. */
660 goto wide_char_string
;
662 #define STRING_ARG(Str, Type) \
663 if (!(flags & SUPPRESS)) \
665 if (flags & MALLOC) \
667 /* The string is to be stored in a malloc'd buffer. */ \
668 strptr = ARG (char **); \
669 if (strptr == NULL) \
671 /* Allocate an initial buffer. */ \
673 *strptr = malloc (strsize * sizeof (Type)); \
674 Str = (Type *) *strptr; \
677 Str = ARG (Type *); \
681 STRING_ARG (str
, char);
694 #define STRING_ADD_CHAR(Str, c, Type) \
695 if (!(flags & SUPPRESS)) \
698 if ((flags & MALLOC) && (char *) Str == *strptr + strsize) \
700 /* Enlarge the buffer. */ \
701 Str = realloc (*strptr, strsize * 2 * sizeof (Type)); \
704 /* Can't allocate that much. Last-ditch effort. */\
705 Str = realloc (*strptr, \
706 (strsize + 1) * sizeof (Type)); \
709 /* We lose. Oh well. \
710 Terminate the string and stop converting, \
711 so at least we don't skip any input. */ \
712 ((Type *) (*strptr))[strsize] = '\0'; \
718 *strptr = (char *) Str; \
719 Str = ((Type *) *strptr) + strsize; \
725 *strptr = (char *) Str; \
726 Str = ((Type *) *strptr) + strsize; \
731 STRING_ADD_CHAR (str
, c
, char);
732 } while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
734 if (!(flags
& SUPPRESS
))
742 /* Wide character string. */
747 STRING_ARG (wstr
, wchar_t);
752 NEXT_WIDE_CHAR (first
);
756 /* XXX We would have to push back the whole wide char
757 with possibly many bytes. But since scanf does
758 not make a difference for white space characters
759 we can simply push back a simple <SP> which is
760 guaranteed to be in the [:space:] class. */
765 STRING_ADD_CHAR (wstr
, val
, wchar_t);
768 while (width
<= 0 || --width
> 0);
770 if (!(flags
& SUPPRESS
))
778 case 'x': /* Hexadecimal integer. */
779 case 'X': /* Ditto. */
784 case 'o': /* Octal integer. */
789 case 'u': /* Unsigned decimal integer. */
794 case 'd': /* Signed decimal integer. */
799 case 'i': /* Generic number. */
808 /* Check for a sign. */
809 if (c
== '-' || c
== '+')
817 /* Look for a leading indication of base. */
818 if (width
!= 0 && c
== '0')
826 if (width
!= 0 && tolower (c
) == 'x')
844 /* Read the number into workspace. */
845 while (c
!= EOF
&& width
!= 0)
847 if (base
== 16 ? !isxdigit (c
) :
848 ((!isdigit (c
) || c
- '0' >= base
) &&
849 !((flags
& GROUP
) && base
== 10 && c
== thousands
)))
858 /* The just read character is not part of the number anymore. */
862 (wpsize
== 1 && (wp
[0] == '+' || wp
[0] == '-')))
863 /* There was no number. */
866 /* Convert the number. */
871 num
.q
= __strtoll_internal (wp
, &tw
, base
, flags
& GROUP
);
873 num
.uq
= __strtoull_internal (wp
, &tw
, base
, flags
& GROUP
);
878 num
.l
= __strtol_internal (wp
, &tw
, base
, flags
& GROUP
);
880 num
.ul
= __strtoul_internal (wp
, &tw
, base
, flags
& GROUP
);
885 if (!(flags
& SUPPRESS
))
890 *ARG (unsigned LONGLONG
int *) = num
.uq
;
891 else if (flags
& LONG
)
892 *ARG (unsigned long int *) = num
.ul
;
893 else if (flags
& SHORT
)
894 *ARG (unsigned short int *)
895 = (unsigned short int) num
.ul
;
896 else if (flags
& CHAR
)
897 *ARG (unsigned char *) = (unsigned char) num
.ul
;
899 *ARG (unsigned int *) = (unsigned int) num
.ul
;
904 *ARG (LONGLONG
int *) = num
.q
;
905 else if (flags
& LONG
)
906 *ARG (long int *) = num
.l
;
907 else if (flags
& SHORT
)
908 *ARG (short int *) = (short int) num
.l
;
909 else if (flags
& CHAR
)
910 *ARG (signed char *) = (signed char) num
.ul
;
912 *ARG (int *) = (int) num
.l
;
918 case 'e': /* Floating-point numbers. */
929 /* Check for a sign. */
930 if (c
== '-' || c
== '+')
933 if (inchar () == EOF
)
934 /* EOF is only an input error before we read any chars. */
942 /* Take care for the special arguments "nan" and "inf". */
943 if (tolower (c
) == 'n')
947 if (inchar () == EOF
|| tolower (c
) != 'a')
950 if (inchar () == EOF
|| tolower (c
) != 'n')
956 else if (tolower (c
) == 'i')
958 /* Maybe "inf" or "infinity". */
960 if (inchar () == EOF
|| tolower (c
) != 'n')
963 if (inchar () == EOF
|| tolower (c
) != 'f')
966 /* It is as least "inf". */
967 if (inchar () != EOF
)
969 if (tolower (c
) == 'i')
971 /* No we have to read the rest as well. */
973 if (inchar () == EOF
|| tolower (c
) != 'n')
976 if (inchar () == EOF
|| tolower (c
) != 'i')
979 if (inchar () == EOF
|| tolower (c
) != 't')
982 if (inchar () == EOF
|| tolower (c
) != 'y')
999 if (tolower (c
) == 'x')
1001 /* It is a number in hexadecimal format. */
1007 /* Grouping is not allowed. */
1013 got_dot
= got_e
= 0;
1018 else if (!got_e
&& is_hexa
&& isxdigit (c
))
1020 else if (got_e
&& wp
[wpsize
- 1] == exp_char
1021 && (c
== '-' || c
== '+'))
1023 else if (wpsize
> 0 && !got_e
&& tolower (c
) == exp_char
)
1026 got_e
= got_dot
= 1;
1028 else if (c
== decimal
&& !got_dot
)
1033 else if ((flags
& GROUP
) && c
== thousands
&& !got_dot
)
1037 /* The last read character is not part of the number
1045 while (width
!= 0 && inchar () != EOF
);
1047 /* Have we read any character? If we try to read a number
1048 in hexadecimal notation and we have read only the `0x'
1049 prefix this is an error. */
1050 if (wpsize
== 0 || (is_hexa
&& wpsize
== 2))
1054 /* Convert the number. */
1056 if (flags
& LONGDBL
)
1058 long double d
= __strtold_internal (wp
, &tw
, flags
& GROUP
);
1059 if (!(flags
& SUPPRESS
) && tw
!= wp
)
1060 *ARG (long double *) = negative
? -d
: d
;
1062 else if (flags
& LONG
)
1064 double d
= __strtod_internal (wp
, &tw
, flags
& GROUP
);
1065 if (!(flags
& SUPPRESS
) && tw
!= wp
)
1066 *ARG (double *) = negative
? -d
: d
;
1070 float d
= __strtof_internal (wp
, &tw
, flags
& GROUP
);
1071 if (!(flags
& SUPPRESS
) && tw
!= wp
)
1072 *ARG (float *) = negative
? -d
: d
;
1078 if (!(flags
& SUPPRESS
))
1082 case '[': /* Character class. */
1085 STRING_ARG (wstr
, wchar_t);
1086 c
= '\0'; /* This is to keep gcc quiet. */
1090 STRING_ARG (str
, char);
1105 /* Fill WP with byte flags indexed by character.
1106 We will use this flag map for matching input characters. */
1107 if (wpmax
< UCHAR_MAX
)
1110 wp
= (char *) alloca (wpmax
);
1112 memset (wp
, 0, UCHAR_MAX
);
1115 if (fc
== ']' || fc
== '-')
1117 /* If ] or - appears before any char in the set, it is not
1118 the terminator or separator, but the first char in the
1124 while ((fc
= *f
++) != '\0' && fc
!= ']')
1126 if (fc
== '-' && *f
!= '\0' && *f
!= ']' &&
1127 (unsigned char) f
[-2] <= (unsigned char) *f
)
1129 /* Add all characters from the one before the '-'
1130 up to (but not including) the next format char. */
1131 for (fc
= f
[-2]; fc
< *f
; ++fc
)
1135 /* Add the character to the flag map. */
1140 if (!(flags
& LONG
))
1153 NEXT_WIDE_CHAR (first
);
1154 if (val
<= 255 && wp
[val
] == not_in
)
1159 STRING_ADD_CHAR (wstr
, val
, wchar_t);
1169 if (!(flags
& SUPPRESS
))
1177 num
.ul
= read_in
- 1; /* -1 because we already read one char. */
1180 if (wp
[c
] == not_in
)
1185 STRING_ADD_CHAR (str
, c
, char);
1189 while (width
!= 0 && inchar () != EOF
);
1191 if (read_in
== num
.ul
)
1194 if (!(flags
& SUPPRESS
))
1202 case 'p': /* Generic pointer. */
1204 /* A PTR must be the same size as a `long int'. */
1205 flags
&= ~(SHORT
|LONGDBL
);
1212 /* The last thing we saw int the format string was a white space.
1213 Consume the last white spaces. */
1218 while (isspace (c
));
1222 /* Unlock stream. */
1230 __vfscanf (FILE *s
, const char *format
, va_list argptr
)
1232 return _IO_vfscanf (s
, format
, argptr
, NULL
);
1236 weak_alias (__vfscanf
, vfscanf
)