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/stdarg.h> // robd
21 #include <msvcrt/crttypes.h> // robd
23 #include <msvcrt/errno.h>
25 #include <msvcrt/ctype.h>
26 #include <msvcrt/stdio.h>
27 #include <msvcrt/stdlib.h>
28 #include <msvcrt/string.h>
29 #include <msvcrt/wchar.h>
30 #include <msvcrt/malloc.h>
31 #include <msvcrt/mbstring.h>
32 #include <msvcrt/internal/file.h>
33 #include <msvcrt/internal/stdio.h>
35 /* The internal entry points for `strtoX' take an extra flag argument
36 saying whether or not to parse locale-dependent number grouping. */
38 double __strtod_internal (const char *__nptr,char **__endptr, int __group);
39 float __strtof_internal (const char *__nptr, char **__endptr,int __group);
40 long double __strtold_internal (const char *__nptr,char **__endptr, int __group);
41 long int __strtol_internal (const char *__nptr, char **__endptr, int __base, int __group);
42 unsigned long int __strtoul_internal (const char *__nptr, char **__endptr, int __base, int __group);
45 #include <msvcrt/crttypes.h> // robd
47 //#define HAVE_LONGLONG
48 //#define LONGLONG LONGLONG
50 //#define LONGLONG long
53 /* Those are flags in the conversion format. */
54 # define LONG 0x001 /* l: long or double */
55 # define LONGDBL 0x002 /* L: LONGLONG or long double */
56 # define SHORT 0x004 /* h: short */
57 # define SUPPRESS 0x008 /* *: suppress assignment */
58 # define POINTER 0x010 /* weird %p pointer (`fake hex') */
59 # define NOSKIP 0x020 /* do not skip blanks */
60 # define WIDTH 0x040 /* width was given */
61 # define GROUP 0x080 /* ': group numbers */
62 # define MALLOC 0x100 /* a: malloc strings */
64 # define TYPEMOD (LONG|LONGDBL|SHORT)
67 # define UNGETC(c, s) ((void) (((wint_t)c) != ((wint_t)EOF) && --read_in), ungetc (c, s))
68 # define inchar() ((c = getc (s)), (void) (c != EOF && ++read_in), c)
69 # define encode_error() do { \
71 __set_errno (EILSEQ); \
74 # define conv_error() do { \
78 # define input_error() do { \
80 return done ? 0 : EOF; \
82 # define memory_error() do { \
84 __set_errno (ENOMEM); \
87 # define ARGCHECK(s, format) \
90 /* Check file argument for consistence. */ \
91 if (!__validfp (s) || !s->__mode.__read) \
93 __set_errno (EBADF); \
96 else if (format == NULL) \
98 __set_errno (EINVAL); \
103 # define flockfile(S) /* nothing */
104 # define funlockfile(S) /* nothing */
108 if (wpsize == wpmax) \
111 wpmax = UCHAR_MAX > 2 * wpmax ? UCHAR_MAX : 2 * wpmax; \
112 wp = (char *) malloc (wpmax); \
115 memcpy (wp, old, wpsize); \
119 wp[wpsize++] = (Ch); \
123 int __vfscanf (FILE *s, const char *format, va_list argptr)
126 register const char *f = format;
127 register unsigned char fc; /* Current character of the format. */
128 register size_t done = 0; /* Assignments done. */
129 register size_t read_in = 0; /* Chars read in. */
130 register int c = 0; /* Last char read. */
131 register int width; /* Maximum field width. */
132 register int flags; /* Modifiers for current format element. */
134 /* Status for reading F-P nums. */
135 char got_dot, got_e, negative;
136 /* If a [...] is a [^...]. */
138 /* Base for integral numbers. */
140 /* Signedness for integral numbers. */
142 /* Decimal point character. */
143 wchar_t decimal = '.';
144 /* The thousands character of the current locale. */
145 wchar_t thousands = ',';
146 /* Integral holding variables. */
152 unsigned long int ul;
154 /* Character-buffer pointer. */
156 wchar_t *wstr = NULL;
157 char **strptr = NULL;
159 /* We must not react on white spaces immediately because they can
160 possibly be matched even if in the input stream no character is
161 available anymore. */
164 char *wp = NULL; /* Workspace. */
165 size_t wpmax = 0; /* Maximal size of workspace. */
166 size_t wpsize = 0; /* Currently used bytes in workspace. */
167 char *tw; /* Temporary pointer. */
170 __va_copy (arg, argptr);
172 arg = (va_list) argptr;
177 /* Run through the format string. */
181 /* Extract the next argument, which is of type TYPE.
182 For a %N$... spec, this is the Nth argument from the beginning;
183 otherwise it is the next argument after the state now in ARG. */
184 #define ARG(type) va_arg(argptr,type)
188 /* Non-ASCII, may be a multibyte. */
189 // int len = mblen (f, strlen (f));
212 /* Remember to skip spaces. */
219 /* Read a character. */
222 /* Characters other than format specs must just match. */
226 /* We saw white space char as the last character in the format
227 string. Now it's time to skip all leading white space. */
231 if (inchar () == EOF && *_errno() == EINTR)
245 /* This is the start of the conversion string. */
248 /* Initialize state of modifiers. */
251 /* Prepare temporary buffer. */
254 /* Check for a positional parameter specification. */
259 argpos = argpos * 10 + (*f++ - '0');
264 /* Oops; that was actually the field width. */
272 /* Check for the assignment-suppressing and the number grouping flag. */
273 while (*f == '*' || *f == '\'')
284 /* We have seen width. */
288 /* Find the maximum field width. */
299 /* Check for type modifiers. */
300 while (*f == 'h' || *f == 'l' || *f == 'L' || *f == 'a' || *f == 'q')
304 /* int's are short int's. */
306 /* Signal illegal format element. */
311 if (flags & (SHORT|LONGDBL))
313 else if (flags & LONG)
315 /* A double `l' is equivalent to an `L'. */
320 /* int's are long int's. */
325 /* double's are long double's, and int's are LONGLONG int's. */
327 /* Signal illegal format element. */
333 /* Signal illegal format element. */
335 /* String conversions (%s, %[) take a `char **'
336 arg and fill it in with a malloc'd pointer. */
341 /* End of the format string? */
345 /* We must take care for EINTR errors. */
346 if (c == EOF && *_errno() == EINTR)
349 /* Find the conversion specifier. */
351 if (skip_space || (fc != '[' && fc != 'c' && fc != 'C' && fc != 'n'))
353 /* Eat whitespace. */
355 if (inchar () == EOF && *_errno() == EINTR)
364 case '%': /* Must match a literal '%'. */
373 case 'n': /* Answer number of assignments done. */
374 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
375 with the 'n' conversion specifier. */
376 if (!(flags & SUPPRESS))
378 /* Don't count the read-ahead. */
380 *ARG (long int *) = read_in;
381 else if (flags & LONG)
382 *ARG (long int *) = read_in;
383 else if (flags & SHORT)
384 *ARG (short int *) = read_in;
386 *ARG (int *) = read_in;
388 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
389 /* We have a severe problem here. The ISO C standard
390 contradicts itself in explaining the effect of the %n
391 format in `scanf'. While in ISO C:1990 and the ISO C
392 Amendement 1:1995 the result is described as
394 Execution of a %n directive does not effect the
395 assignment count returned at the completion of
396 execution of the f(w)scanf function.
398 in ISO C Corrigendum 1:1994 the following was added:
401 Add the following fourth example:
404 int d1, d2, n1, n2, i;
405 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
406 the value 123 is assigned to d1 and the value3 to n1.
407 Because %n can never get an input failure the value
408 of 3 is also assigned to n2. The value of d2 is not
409 affected. The value 3 is assigned to i.
411 We go for now with the historically correct code fro ISO C,
412 i.e., we don't count the %n assignments. When it ever
413 should proof to be wrong just remove the #ifdef above. */
419 case 'c': /* Match characters. */
420 if ((flags & LONG) == 0)
422 if (!(flags & SUPPRESS))
436 if (!(flags & SUPPRESS))
440 while (--width > 0 && inchar () != EOF);
443 while (--width > 0 && inchar () != EOF);
446 /* I.e., EOF was read. */
449 if (!(flags & SUPPRESS))
456 /* Get UTF-8 encoded wide character. Here we assume (as in
457 other parts of the libc) that we only have to handle
464 if (!(flags & SUPPRESS))
466 wstr = ARG (wchar_t *);
473 #define NEXT_WIDE_CHAR(First) \
476 /* EOF is only an error for the first character. */ \
489 if ((c & 0xc0) == 0x80 || (c & 0xfe) == 0xfe) \
491 if ((c & 0xe0) == 0xc0) \
493 /* We expect two bytes. */ \
497 else if ((c & 0xf0) == 0xe0) \
499 /* We expect three bytes. */ \
503 else if ((c & 0xf8) == 0xf0) \
505 /* We expect four bytes. */ \
509 else if ((c & 0xfc) == 0xf8) \
511 /* We expect five bytes. */ \
517 /* We expect six bytes. */ \
526 || (c & 0xc0) == 0x80 || (c & 0xfe) == 0xfe) \
534 if (!(flags & SUPPRESS)) \
538 NEXT_WIDE_CHAR (first);
543 /* I.e., EOF was read. */
546 if (!(flags & SUPPRESS))
551 case 's': /* Read a string. */
553 /* We have to process a wide character string. */
554 goto wide_char_string;
556 #define STRING_ARG(Str, Type) \
557 if (!(flags & SUPPRESS)) \
559 if (flags & MALLOC) \
561 /* The string is to be stored in a malloc'd buffer. */ \
562 strptr = ARG (char **); \
563 if (strptr == NULL) \
565 /* Allocate an initial buffer. */ \
567 *strptr = malloc (strsize * sizeof (Type)); \
568 Str = (Type *) *strptr; \
571 Str = ARG (Type *); \
575 STRING_ARG (str, char);
588 #define STRING_ADD_CHAR(Str, c, Type) \
589 if (!(flags & SUPPRESS)) \
592 if ((flags & MALLOC) && (char *) Str == *strptr + strsize) \
594 /* Enlarge the buffer. */ \
595 Str = realloc (*strptr, strsize * 2 * sizeof (Type)); \
598 /* Can't allocate that much. Last-ditch effort. */\
599 Str = realloc (*strptr, \
600 (strsize + 1) * sizeof (Type)); \
603 /* We lose. Oh well. \
604 Terminate the string and stop converting, \
605 so at least we don't skip any input. */ \
606 ((Type *) (*strptr))[strsize] = '\0'; \
612 *strptr = (char *) Str; \
613 Str = ((Type *) *strptr) + strsize; \
619 *strptr = (char *) Str; \
620 Str = ((Type *) *strptr) + strsize; \
625 STRING_ADD_CHAR (str, c, char);
626 } while ((width <= 0 || --width > 0) && inchar () != EOF);
628 if (!(flags & SUPPRESS))
636 /* Wide character string. */
641 STRING_ARG (wstr, wchar_t);
646 NEXT_WIDE_CHAR (first);
650 /* XXX We would have to push back the whole wide char
651 with possibly many bytes. But since scanf does
652 not make a difference for white space characters
653 we can simply push back a simple <SP> which is
654 guaranteed to be in the [:space:] class. */
659 STRING_ADD_CHAR (wstr, val, wchar_t);
662 while (width <= 0 || --width > 0);
664 if (!(flags & SUPPRESS))
672 case 'x': /* Hexadecimal integer. */
673 case 'X': /* Ditto. */
678 case 'o': /* Octal integer. */
683 case 'u': /* Unsigned decimal integer. */
688 case 'd': /* Signed decimal integer. */
693 case 'i': /* Generic number. */
702 /* Check for a sign. */
703 if (c == '-' || c == '+')
711 /* Look for a leading indication of base. */
712 if (width != 0 && c == '0')
720 if (width != 0 && tolower (c) == 'x')
738 /* Read the number into workspace. */
739 while (c != EOF && width != 0)
741 if (base == 16 ? !isxdigit (c) :
742 ((!isdigit (c) || c - '0' >= base) &&
743 !((flags & GROUP) && base == 10 && c == thousands)))
752 /* The just read character is not part of the number anymore. */
756 (wpsize == 1 && (wp[0] == '+' || wp[0] == '-')))
757 /* There was no number. */
760 /* Convert the number. */
764 // if (number_signed)
765 // num.q = __strtoq_internal (wp, &tw, base, flags & GROUP);
767 // num.uq = __strtouq_internal (wp, &tw, base, flags & GROUP);
772 num.l = __strtol_internal (wp, &tw, base, flags & GROUP);
774 num.ul = __strtoul_internal (wp, &tw, base, flags & GROUP);
779 if (!(flags & SUPPRESS))
783 if (flags & LONGDBL) {
784 *ARG (ULONGLONG*) = num.uq;
786 else if (flags & LONG)
787 *ARG (unsigned long int*) = num.ul;
788 else if (flags & SHORT)
789 *ARG (unsigned short int*) = (unsigned short int) num.ul;
791 *ARG (unsigned int*) = (unsigned int) num.ul;
795 if (flags & LONGDBL) {
796 *ARG (LONGLONG*) = num.q;
798 else if (flags & LONG)
799 *ARG (long int *) = num.l;
800 else if (flags & SHORT)
801 *ARG (short int *) = (short int) num.l;
803 *ARG (int *) = (int) num.l;
809 case 'e': /* Floating-point numbers. */
818 /* Check for a sign. */
819 if (c == '-' || c == '+')
822 if (inchar () == EOF)
823 /* EOF is only an input error before we read any chars. */
836 else if (got_e && wp[wpsize - 1] == 'e'
837 && (c == '-' || c == '+'))
839 else if (wpsize > 0 && !got_e && tolower (c) == 'e')
844 else if (c == decimal && !got_dot)
849 else if ((flags & GROUP) && c == thousands && !got_dot)
853 /* The last read character is not part of the number
861 while (width != 0 && inchar () != EOF);
866 /* Convert the number. */
870 long double d = __strtold_internal (wp, &tw, flags & GROUP);
871 if (!(flags & SUPPRESS) && tw != wp)
872 *ARG (long double *) = negative ? -d : d;
874 else if (flags & LONG)
876 double d = __strtod_internal (wp, &tw, flags & GROUP);
877 if (!(flags & SUPPRESS) && tw != wp)
878 *ARG (double *) = negative ? -d : d;
882 float d = __strtof_internal (wp, &tw, flags & GROUP);
883 if (!(flags & SUPPRESS) && tw != wp)
884 *ARG (float *) = negative ? -d : d;
890 if (!(flags & SUPPRESS))
894 case '[': /* Character class. */
897 STRING_ARG (wstr, wchar_t);
898 c = '\0'; /* This is to keep gcc quiet. */
902 STRING_ARG (str, char);
917 /* Fill WP with byte flags indexed by character.
918 We will use this flag map for matching input characters. */
919 if (wpmax < UCHAR_MAX)
922 wp = (char *) alloca (wpmax);
924 memset (wp, 0, UCHAR_MAX);
927 if (fc == ']' || fc == '-')
929 /* If ] or - appears before any char in the set, it is not
930 the terminator or separator, but the first char in the
936 while ((fc = *f++) != '\0' && fc != ']')
938 if (fc == '-' && *f != '\0' && *f != ']' &&
939 (unsigned char) f[-2] <= (unsigned char) *f)
941 /* Add all characters from the one before the '-'
942 up to (but not including) the next format char. */
943 for (fc = f[-2]; fc < *f; ++fc)
947 /* Add the character to the flag map. */
965 NEXT_WIDE_CHAR (first);
966 if (val > 255 || wp[val] == not_in)
968 /* XXX We have a problem here. We read a wide
969 character and this possibly took several
970 bytes. But we can only push back one single
971 character. To be sure we don't create wrong
972 input we push it back only in case it is
973 representable within one byte. */
978 STRING_ADD_CHAR (wstr, val, wchar_t);
988 if (!(flags & SUPPRESS))
996 num.ul = read_in - 1; /* -1 because we already read one char. */
1004 STRING_ADD_CHAR (str, c, char);
1008 while (width != 0 && inchar () != EOF);
1010 if (read_in == num.ul)
1013 if (!(flags & SUPPRESS))
1021 case 'p': /* Generic pointer. */
1023 /* A PTR must be the same size as a `long int'. */
1024 flags &= ~(SHORT|LONGDBL);
1031 /* The last thing we saw int the format string was a white space.
1032 Consume the last white spaces. */
1037 while (isspace (c));
1048 xfscanf(FILE *f, const char *fmt, ...)
1053 r = __vfscanf(f, fmt, a);
1059 double __strtod_internal (const char *__nptr,char **__endptr, int __group)
1061 return strtod(__nptr,__endptr);
1063 float __strtof_internal (const char *__nptr, char **__endptr,int __group)
1065 return (float)strtod(__nptr,__endptr);
1067 static double powten[] =
1069 1e1L, 1e2L, 1e4L, 1e8L, 1e16L, 1e32L, 1e64L, 1e128L, 1e256L,
1071 1e512L, 1e512L*1e512L, 1e2048L, 1e4096L
1073 1e256L, 1e256L, 1e256L, 1e256L
1077 long double __strtold_internal (const char *s,char **sret, int __group)
1080 long double r; /* result */
1081 int e, ne; /* exponent */
1082 int sign; /* +- 1.0 */
1092 while(*s && isspace(*s))
1103 while ((*s >= '0') && (*s <= '9'))
1114 while ((*s >= '0') && (*s <= '9'))
1130 if ((*s == 'e') || (*s == 'E'))
1140 while ((*s >= '0') && (*s <= '9'))
1155 /* possibly subnormal number, 10^e would overflow */
1173 d *= powten[l2powm1];
1188 long int __strtol_internal (const char *__nptr, char **__endptr, int __base, int __group)
1190 return strtol(__nptr,__endptr, __base);
1192 unsigned long int __strtoul_internal (const char *__nptr, char **__endptr, int __base, int __group)
1194 return strtoul(__nptr,__endptr, __base);