1 /* Copyright (C) 1991, 92, 93, 94, 95, 96, 97 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. */
20 #include <msvcrt/errno.h>
22 #include <msvcrt/ctype.h>
23 #include <msvcrt/stdio.h>
24 #include <msvcrt/stdlib.h>
25 #include <msvcrt/string.h>
26 #include <msvcrt/wchar.h>
27 #include <msvcrt/malloc.h>
28 #include <msvcrt/mbstring.h>
29 #include <msvcrt/internal/file.h>
30 #include <msvcrt/internal/stdio.h>
32 /* The internal entry points for `strtoX' take an extra flag argument
33 saying whether or not to parse locale-dependent number grouping. */
35 double __strtod_internal (const char *__nptr,char **__endptr, int __group);
36 float __strtof_internal (const char *__nptr, char **__endptr,int __group);
37 long double __strtold_internal (const char *__nptr,char **__endptr, int __group);
38 long int __strtol_internal (const char *__nptr, char **__endptr, int __base, int __group);
39 unsigned long int __strtoul_internal (const char *__nptr, char **__endptr, int __base, int __group);
45 #define LONGLONG long long
50 /* Those are flags in the conversion format. */
51 # define LONG 0x001 /* l: long or double */
52 # define LONGDBL 0x002 /* L: long long or long double */
53 # define SHORT 0x004 /* h: short */
54 # define SUPPRESS 0x008 /* *: suppress assignment */
55 # define POINTER 0x010 /* weird %p pointer (`fake hex') */
56 # define NOSKIP 0x020 /* do not skip blanks */
57 # define WIDTH 0x040 /* width was given */
58 # define GROUP 0x080 /* ': group numbers */
59 # define MALLOC 0x100 /* a: malloc strings */
61 # define TYPEMOD (LONG|LONGDBL|SHORT)
64 # define ungetc(c, s) ((void) (c != EOF && --read_in), ungetc (c, s))
65 # define inchar() ((c = getc (s)), (void) (c != EOF && ++read_in), c)
66 # define encode_error() do { \
68 __set_errno (EILSEQ); \
71 # define conv_error() do { \
75 # define input_error() do { \
77 return done ? 0 : EOF; \
79 # define memory_error() do { \
81 __set_errno (ENOMEM); \
84 # define ARGCHECK(s, format) \
87 /* Check file argument for consistence. */ \
88 if (!__validfp (s) || !s->__mode.__read) \
90 __set_errno (EBADF); \
93 else if (format == NULL) \
95 __set_errno (EINVAL); \
100 # define flockfile(S) /* nothing */
101 # define funlockfile(S) /* nothing */
103 char *wp = NULL; /* Workspace. */
104 size_t wpmax = 0; /* Maximal size of workspace. */
105 size_t wpsize = 0; /* Currently used bytes in workspace. */
113 wpmax = UCHAR_MAX > 2 * wpmax ? UCHAR_MAX : 2 * wpmax;
114 wp = (char *) malloc (wpmax);
117 memcpy (wp, old, wpsize);
125 int __vfscanf (FILE *s, const char *format, va_list argptr)
128 register const char *f = format;
129 register unsigned char fc; /* Current character of the format. */
130 register size_t done = 0; /* Assignments done. */
131 register size_t read_in = 0; /* Chars read in. */
132 register int c = 0; /* Last char read. */
133 register int width; /* Maximum field width. */
134 register int flags; /* Modifiers for current format element. */
136 /* Status for reading F-P nums. */
137 char got_dot, got_e, negative;
138 /* If a [...] is a [^...]. */
140 /* Base for integral numbers. */
142 /* Signedness for integral numbers. */
144 /* Decimal point character. */
145 wchar_t decimal = '.';
146 /* The thousands character of the current locale. */
147 wchar_t thousands = ',';
148 /* Integral holding variables. */
152 unsigned long long int uq;
154 unsigned long int ul;
156 /* Character-buffer pointer. */
158 wchar_t *wstr = NULL;
159 char **strptr = NULL;
161 /* We must not react on white spaces immediately because they can
162 possibly be matched even if in the input stream no character is
163 available anymore. */
166 char *tw; /* Temporary pointer. */
169 __va_copy (arg, argptr);
171 arg = (va_list) argptr;
176 /* Run through the format string. */
180 /* Extract the next argument, which is of type TYPE.
181 For a %N$... spec, this is the Nth argument from the beginning;
182 otherwise it is the next argument after the state now in ARG. */
183 #define ARG(type) va_arg(argptr,type)
187 /* Non-ASCII, may be a multibyte. */
188 // int len = mblen (f, strlen (f));
211 /* Remember to skip spaces. */
218 /* Read a character. */
221 /* Characters other than format specs must just match. */
225 /* We saw white space char as the last character in the format
226 string. Now it's time to skip all leading white space. */
230 if (inchar () == EOF && errno == EINTR)
244 /* This is the start of the conversion string. */
247 /* Initialize state of modifiers. */
250 /* Prepare temporary buffer. */
253 /* Check for a positional parameter specification. */
258 argpos = argpos * 10 + (*f++ - '0');
263 /* Oops; that was actually the field width. */
271 /* Check for the assignment-suppressing and the number grouping flag. */
272 while (*f == '*' || *f == '\'')
283 /* We have seen width. */
287 /* Find the maximum field width. */
298 /* Check for type modifiers. */
299 while (*f == 'h' || *f == 'l' || *f == 'L' || *f == 'a' || *f == 'q')
303 /* int's are short int's. */
305 /* Signal illegal format element. */
310 if (flags & (SHORT|LONGDBL))
312 else if (flags & LONG)
314 /* A double `l' is equivalent to an `L'. */
319 /* int's are long int's. */
324 /* double's are long double's, and int's are long long int's. */
326 /* Signal illegal format element. */
332 /* Signal illegal format element. */
334 /* String conversions (%s, %[) take a `char **'
335 arg and fill it in with a malloc'd pointer. */
340 /* End of the format string? */
344 /* We must take care for EINTR errors. */
345 if (c == EOF && errno == EINTR)
348 /* Find the conversion specifier. */
350 if (skip_space || (fc != '[' && fc != 'c' && fc != 'C' && fc != 'n'))
352 /* Eat whitespace. */
354 if (inchar () == EOF && errno == EINTR)
363 case '%': /* Must match a literal '%'. */
372 case 'n': /* Answer number of assignments done. */
373 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
374 with the 'n' conversion specifier. */
375 if (!(flags & SUPPRESS))
377 /* Don't count the read-ahead. */
379 *ARG (long int *) = read_in;
380 else if (flags & LONG)
381 *ARG (long int *) = read_in;
382 else if (flags & SHORT)
383 *ARG (short int *) = read_in;
385 *ARG (int *) = read_in;
387 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
388 /* We have a severe problem here. The ISO C standard
389 contradicts itself in explaining the effect of the %n
390 format in `scanf'. While in ISO C:1990 and the ISO C
391 Amendement 1:1995 the result is described as
393 Execution of a %n directive does not effect the
394 assignment count returned at the completion of
395 execution of the f(w)scanf function.
397 in ISO C Corrigendum 1:1994 the following was added:
400 Add the following fourth example:
403 int d1, d2, n1, n2, i;
404 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
405 the value 123 is assigned to d1 and the value3 to n1.
406 Because %n can never get an input failure the value
407 of 3 is also assigned to n2. The value of d2 is not
408 affected. The value 3 is assigned to i.
410 We go for now with the historically correct code fro ISO C,
411 i.e., we don't count the %n assignments. When it ever
412 should proof to be wrong just remove the #ifdef above. */
418 case 'c': /* Match characters. */
419 if ((flags & LONG) == 0)
421 if (!(flags & SUPPRESS))
435 if (!(flags & SUPPRESS))
439 while (--width > 0 && inchar () != EOF);
442 while (--width > 0 && inchar () != EOF);
445 /* I.e., EOF was read. */
448 if (!(flags & SUPPRESS))
455 /* Get UTF-8 encoded wide character. Here we assume (as in
456 other parts of the libc) that we only have to handle
463 if (!(flags & SUPPRESS))
465 wstr = ARG (wchar_t *);
472 #define NEXT_WIDE_CHAR(First) \
475 /* EOF is only an error for the first character. */ \
488 if ((c & 0xc0) == 0x80 || (c & 0xfe) == 0xfe) \
490 if ((c & 0xe0) == 0xc0) \
492 /* We expect two bytes. */ \
496 else if ((c & 0xf0) == 0xe0) \
498 /* We expect three bytes. */ \
502 else if ((c & 0xf8) == 0xf0) \
504 /* We expect four bytes. */ \
508 else if ((c & 0xfc) == 0xf8) \
510 /* We expect five bytes. */ \
516 /* We expect six bytes. */ \
525 || (c & 0xc0) == 0x80 || (c & 0xfe) == 0xfe) \
533 if (!(flags & SUPPRESS)) \
537 NEXT_WIDE_CHAR (first);
542 /* I.e., EOF was read. */
545 if (!(flags & SUPPRESS))
550 case 's': /* Read a string. */
552 /* We have to process a wide character string. */
553 goto wide_char_string;
555 #define STRING_ARG(Str, Type) \
556 if (!(flags & SUPPRESS)) \
558 if (flags & MALLOC) \
560 /* The string is to be stored in a malloc'd buffer. */ \
561 strptr = ARG (char **); \
562 if (strptr == NULL) \
564 /* Allocate an initial buffer. */ \
566 *strptr = malloc (strsize * sizeof (Type)); \
567 Str = (Type *) *strptr; \
570 Str = ARG (Type *); \
574 STRING_ARG (str, char);
587 #define STRING_ADD_CHAR(Str, c, Type) \
588 if (!(flags & SUPPRESS)) \
591 if ((flags & MALLOC) && (char *) Str == *strptr + strsize) \
593 /* Enlarge the buffer. */ \
594 Str = realloc (*strptr, strsize * 2 * sizeof (Type)); \
597 /* Can't allocate that much. Last-ditch effort. */\
598 Str = realloc (*strptr, \
599 (strsize + 1) * sizeof (Type)); \
602 /* We lose. Oh well. \
603 Terminate the string and stop converting, \
604 so at least we don't skip any input. */ \
605 ((Type *) (*strptr))[strsize] = '\0'; \
611 *strptr = (char *) Str; \
612 Str = ((Type *) *strptr) + strsize; \
618 *strptr = (char *) Str; \
619 Str = ((Type *) *strptr) + strsize; \
624 STRING_ADD_CHAR (str, c, char);
625 } while ((width <= 0 || --width > 0) && inchar () != EOF);
627 if (!(flags & SUPPRESS))
635 /* Wide character string. */
640 STRING_ARG (wstr, wchar_t);
645 NEXT_WIDE_CHAR (first);
649 /* XXX We would have to push back the whole wide char
650 with possibly many bytes. But since scanf does
651 not make a difference for white space characters
652 we can simply push back a simple <SP> which is
653 guaranteed to be in the [:space:] class. */
658 STRING_ADD_CHAR (wstr, val, wchar_t);
661 while (width <= 0 || --width > 0);
663 if (!(flags & SUPPRESS))
671 case 'x': /* Hexadecimal integer. */
672 case 'X': /* Ditto. */
677 case 'o': /* Octal integer. */
682 case 'u': /* Unsigned decimal integer. */
687 case 'd': /* Signed decimal integer. */
692 case 'i': /* Generic number. */
701 /* Check for a sign. */
702 if (c == '-' || c == '+')
710 /* Look for a leading indication of base. */
711 if (width != 0 && c == '0')
719 if (width != 0 && tolower (c) == 'x')
737 /* Read the number into workspace. */
738 while (c != EOF && width != 0)
740 if (base == 16 ? !isxdigit (c) :
741 ((!isdigit (c) || c - '0' >= base) &&
742 !((flags & GROUP) && base == 10 && c == thousands)))
751 /* The just read character is not part of the number anymore. */
755 (wpsize == 1 && (wp[0] == '+' || wp[0] == '-')))
756 /* There was no number. */
759 /* Convert the number. */
763 // if (number_signed)
764 // num.q = __strtoq_internal (wp, &tw, base, flags & GROUP);
766 // num.uq = __strtouq_internal (wp, &tw, base, flags & GROUP);
771 num.l = __strtol_internal (wp, &tw, base, flags & GROUP);
773 num.ul = __strtoul_internal (wp, &tw, base, flags & GROUP);
778 if (!(flags & SUPPRESS))
782 if (flags & LONGDBL) {
783 *ARG (unsigned LONGLONG int *) = num.uq;
785 else if (flags & LONG)
786 *ARG (unsigned long int *) = num.ul;
787 else if (flags & SHORT)
788 *ARG (unsigned short int *) = (unsigned short int) num.ul;
790 *ARG (unsigned int *) = (unsigned int) num.ul;
794 if (flags & LONGDBL) {
795 *ARG (LONGLONG int *) = num.q;
797 else if (flags & LONG)
798 *ARG (long int *) = num.l;
799 else if (flags & SHORT)
800 *ARG (short int *) = (short int) num.l;
802 *ARG (int *) = (int) num.l;
808 case 'e': /* Floating-point numbers. */
817 /* Check for a sign. */
818 if (c == '-' || c == '+')
821 if (inchar () == EOF)
822 /* EOF is only an input error before we read any chars. */
835 else if (got_e && wp[wpsize - 1] == 'e'
836 && (c == '-' || c == '+'))
838 else if (wpsize > 0 && !got_e && tolower (c) == 'e')
843 else if (c == decimal && !got_dot)
848 else if ((flags & GROUP) && c == thousands && !got_dot)
852 /* The last read character is not part of the number
860 while (width != 0 && inchar () != EOF);
865 /* Convert the number. */
869 long double d = __strtold_internal (wp, &tw, flags & GROUP);
870 if (!(flags & SUPPRESS) && tw != wp)
871 *ARG (long double *) = negative ? -d : d;
873 else if (flags & LONG)
875 double d = __strtod_internal (wp, &tw, flags & GROUP);
876 if (!(flags & SUPPRESS) && tw != wp)
877 *ARG (double *) = negative ? -d : d;
881 float d = __strtof_internal (wp, &tw, flags & GROUP);
882 if (!(flags & SUPPRESS) && tw != wp)
883 *ARG (float *) = negative ? -d : d;
889 if (!(flags & SUPPRESS))
893 case '[': /* Character class. */
896 STRING_ARG (wstr, wchar_t);
897 c = '\0'; /* This is to keep gcc quiet. */
901 STRING_ARG (str, char);
916 /* Fill WP with byte flags indexed by character.
917 We will use this flag map for matching input characters. */
918 if (wpmax < UCHAR_MAX)
921 wp = (char *) alloca (wpmax);
923 memset (wp, 0, UCHAR_MAX);
926 if (fc == ']' || fc == '-')
928 /* If ] or - appears before any char in the set, it is not
929 the terminator or separator, but the first char in the
935 while ((fc = *f++) != '\0' && fc != ']')
937 if (fc == '-' && *f != '\0' && *f != ']' &&
938 (unsigned char) f[-2] <= (unsigned char) *f)
940 /* Add all characters from the one before the '-'
941 up to (but not including) the next format char. */
942 for (fc = f[-2]; fc < *f; ++fc)
946 /* Add the character to the flag map. */
964 NEXT_WIDE_CHAR (first);
965 if (val > 255 || wp[val] == not_in)
967 /* XXX We have a problem here. We read a wide
968 character and this possibly took several
969 bytes. But we can only push back one single
970 character. To be sure we don't create wrong
971 input we push it back only in case it is
972 representable within one byte. */
977 STRING_ADD_CHAR (wstr, val, wchar_t);
987 if (!(flags & SUPPRESS))
995 num.ul = read_in - 1; /* -1 because we already read one char. */
1003 STRING_ADD_CHAR (str, c, char);
1007 while (width != 0 && inchar () != EOF);
1009 if (read_in == num.ul)
1012 if (!(flags & SUPPRESS))
1020 case 'p': /* Generic pointer. */
1022 /* A PTR must be the same size as a `long int'. */
1023 flags &= ~(SHORT|LONGDBL);
1030 /* The last thing we saw int the format string was a white space.
1031 Consume the last white spaces. */
1036 while (isspace (c));
1047 xfscanf(FILE *f, const char *fmt, ...)
1052 r = __vfscanf(f, fmt, a);
1058 double __strtod_internal (const char *__nptr,char **__endptr, int __group)
1060 return strtod(__nptr,__endptr);
1062 float __strtof_internal (const char *__nptr, char **__endptr,int __group)
1064 return (float)strtod(__nptr,__endptr);
1066 static double powten[] =
1068 1e1L, 1e2L, 1e4L, 1e8L, 1e16L, 1e32L, 1e64L, 1e128L, 1e256L,
1069 1e512L, 1e512L*1e512L, 1e2048L, 1e4096L
1072 long double __strtold_internal (const char *s,char **sret, int __group)
1075 long double r; /* result */
1076 int e, ne; /* exponent */
1077 int sign; /* +- 1.0 */
1087 while(*s && isspace(*s))
1098 while ((*s >= '0') && (*s <= '9'))
1109 while ((*s >= '0') && (*s <= '9'))
1125 if ((*s == 'e') || (*s == 'E'))
1135 while ((*s >= '0') && (*s <= '9'))
1150 /* possibly subnormal number, 10^e would overflow */
1168 d *= powten[l2powm1];
1183 long int __strtol_internal (const char *__nptr, char **__endptr, int __base, int __group)
1185 return strtol(__nptr,__endptr, __base);
1187 unsigned long int __strtoul_internal (const char *__nptr, char **__endptr, int __base, int __group)
1189 return strtoul(__nptr,__endptr, __base);