1 /* Copyright (C) 1991, 92, 93, 94, 95, 96 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
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
20 #include "../locale/localeinfo.h"
32 #define LONGLONG long 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 */
53 # define va_list _IO_va_list
54 # define ungetc(c, s) _IO_ungetc (c, s)
55 # define inchar() ((c = _IO_getc (s)), (void) ++read_in, c)
56 # define conv_error() return ((void) (errp != NULL && (*errp |= 2)), \
57 (void) (c == EOF || _IO_ungetc (c, s)), done)
59 # define input_error() return ((void) (errp != NULL && (*errp |= 1)), \
60 done == 0 ? EOF : done)
61 # define memory_error() return ((void) (errno = ENOMEM), EOF)
62 # define ARGCHECK(s, format) \
65 /* Check file argument for consistence. */ \
67 if (s->_flags & _IO_NO_READS || format == NULL) \
74 # define inchar() ((c = getc (s)), (void) ++read_in, c)
75 # define conv_error() return ((void) ungetc (c, s), done)
76 # define input_error() return (done == 0 ? EOF : done)
77 # define memory_error() return ((void) (errno = ENOMEM), EOF)
78 # define ARGCHECK(s, format) \
81 /* Check file argument for consistence. */ \
82 if (!__validfp (s) || !s->__mode.__read || format == NULL) \
91 /* Read formatted input from S according to the format string
92 FORMAT, using the argument list in ARG.
93 Return the number of assignments made, or -1 for an input error. */
96 _IO_vfscanf (s
, format
, argptr
, errp
)
103 __vfscanf (FILE *s
, const char *format
, va_list argptr
)
106 va_list arg
= (va_list) argptr
;
108 register const char *f
= format
;
109 register unsigned char fc
; /* Current character of the format. */
110 register size_t done
= 0; /* Assignments done. */
111 register size_t read_in
= 0; /* Chars read in. */
112 register int c
; /* Last char read. */
113 register int width
; /* Maximum field width. */
114 register int flags
; /* Modifiers for current format element. */
116 /* Status for reading F-P nums. */
118 /* If a [...] is a [^...]. */
120 /* Base for integral numbers. */
122 /* Signedness for integral numbers. */
124 /* Decimal point character. */
126 /* The thousands character of the current locale. */
128 /* Integral holding variables. */
132 unsigned long long int uq
;
134 unsigned long int ul
;
136 /* Character-buffer pointer. */
137 register char *str
, **strptr
;
139 /* We must not react on white spaces immediately because they can
140 possibly be matched even if in the input stream no character is
141 available anymore. */
144 char *tw
; /* Temporary pointer. */
145 char *wp
= NULL
; /* Workspace. */
146 size_t wpmax
= 0; /* Maximal size of workspace. */
147 size_t wpsize
; /* Currently used bytes in workspace. */
151 if (wpsize == wpmax) \
154 wpmax = UCHAR_MAX > 2 * wpmax ? UCHAR_MAX : 2 * wpmax; \
155 wp = (char *) alloca (wpmax); \
157 memcpy (wp, old, wpsize); \
159 wp[wpsize++] = (Ch); \
163 ARGCHECK (s
, format
);
165 /* Figure out the decimal point character. */
166 if (mbtowc (&decimal
, _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
),
167 strlen (_NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
))) <= 0)
168 decimal
= (wchar_t) *_NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
169 /* Figure out the thousands separator character. */
170 if (mbtowc (&thousands
, _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
),
171 strlen (_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
))) <= 0)
172 thousands
= (wchar_t) *_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
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. */
184 /* XXX Possible optimization. */
185 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
186 ({ va_list arg = (va_list) argptr; \
187 arg = (va_list) ((char *) arg \
189 * __va_rounded_size (void *)); \
190 va_arg (arg, type); \
193 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
194 ({ unsigned int pos = argpos; \
195 va_list arg = (va_list) argptr; \
197 (void) va_arg (arg, void *); \
198 va_arg (arg, type); \
204 /* Non-ASCII, may be a multibyte. */
205 int len
= mblen (f
, strlen (f
));
222 /* Remember to skip spaces. */
229 /* Characters other than format specs must just match. */
233 /* We saw white space char as the last character in the format
234 string. Now it's time to skip all leading white space. */
250 /* This is the start of the conversion string. */
253 /* Initialize state of modifiers. */
256 /* Prepare temporary buffer. */
259 /* Check for a positional parameter specification. */
264 argpos
= argpos
* 10 + (*f
++ - '0');
269 /* Oops; that was actually the field width. */
277 /* Check for the assignment-suppressant and the number grouping flag. */
278 while (*f
== '*' || *f
== '\'')
289 /* We have seen width. */
293 /* Find the maximum field width. */
304 /* Check for type modifiers. */
305 while (*f
== 'h' || *f
== 'l' || *f
== 'L' || *f
== 'a' || *f
== 'q')
309 /* int's are short int's. */
310 if (flags
& (LONG
|LONGDBL
))
311 /* Signal illegal format element. */
316 if (flags
& (SHORT
|LONGDBL
))
318 else if (flags
& LONG
)
320 /* A double `l' is equivalent to an `L'. */
325 /* int's are long int's. */
330 /* double's are long double's, and int's are long long int's. */
331 if (flags
& (LONG
|SHORT
))
332 /* Signal illegal format element. */
337 /* String conversions (%s, %[) take a `char **'
338 arg and fill it in with a malloc'd pointer. */
343 /* End of the format string? */
347 /* Find the conversion specifier. */
349 if (skip_space
|| (fc
!= '[' && fc
!= 'c' && fc
!= 'n'))
351 /* Eat whitespace. */
359 case '%': /* Must match a literal '%'. */
365 case 'n': /* Answer number of assignments done. */
366 if (!(flags
& SUPPRESS
))
367 *ARG (int *) = read_in
- 1; /* Don't count the read-ahead. */
370 case 'c': /* Match characters. */
371 if (!(flags
& SUPPRESS
))
384 if (!(flags
& SUPPRESS
))
388 while (inchar () != EOF
&& --width
> 0);
391 while (inchar () != EOF
&& --width
> 0);
393 if (!(flags
& SUPPRESS
))
398 case 's': /* Read a string. */
400 if (!(flags & SUPPRESS)) \
402 if (flags & MALLOC) \
404 /* The string is to be stored in a malloc'd buffer. */ \
405 strptr = ARG (char **); \
406 if (strptr == NULL) \
408 /* Allocate an initial buffer. */ \
410 *strptr = str = malloc (strsize); \
413 str = ARG (char *); \
426 #define STRING_ADD_CHAR(c) \
427 if (!(flags & SUPPRESS)) \
430 if ((flags & MALLOC) && str == *strptr + strsize) \
432 /* Enlarge the buffer. */ \
433 str = realloc (*strptr, strsize * 2); \
436 /* Can't allocate that much. Last-ditch effort. */\
437 str = realloc (*strptr, strsize + 1); \
440 /* We lose. Oh well. \
441 Terminate the string and stop converting, \
442 so at least we don't skip any input. */ \
443 (*strptr)[strsize] = '\0'; \
463 } while (inchar () != EOF
&& (width
<= 0 || --width
> 0));
465 if (!(flags
& SUPPRESS
))
472 case 'x': /* Hexadecimal integer. */
473 case 'X': /* Ditto. */
478 case 'o': /* Octal integer. */
483 case 'u': /* Unsigned decimal integer. */
488 case 'd': /* Signed decimal integer. */
493 case 'i': /* Generic number. */
501 /* Check for a sign. */
502 if (c
== '-' || c
== '+')
510 /* Look for a leading indication of base. */
511 if (width
!= 0 && c
== '0')
519 if (width
!= 0 && tolower (c
) == 'x')
537 /* Read the number into workspace. */
538 while (c
!= EOF
&& width
!= 0)
540 if (base
== 16 ? !isxdigit (c
) :
541 ((!isdigit (c
) || c
- '0' >= base
) &&
542 !((flags
& GROUP
) && base
== 10 && c
== thousands
)))
552 (wpsize
== 1 && (wp
[0] == '+' || wp
[0] == '-')))
553 /* There was no number. */
556 /* Convert the number. */
561 num
.q
= __strtoq_internal (wp
, &tw
, base
, flags
& GROUP
);
563 num
.uq
= __strtouq_internal (wp
, &tw
, base
, flags
& GROUP
);
568 num
.l
= __strtol_internal (wp
, &tw
, base
, flags
& GROUP
);
570 num
.ul
= __strtoul_internal (wp
, &tw
, base
, flags
& GROUP
);
575 if (!(flags
& SUPPRESS
))
580 *ARG (unsigned LONGLONG
int *) = num
.uq
;
581 else if (flags
& LONG
)
582 *ARG (unsigned long int *) = num
.ul
;
583 else if (flags
& SHORT
)
584 *ARG (unsigned short int *)
585 = (unsigned short int) num
.ul
;
587 *ARG (unsigned int *) = (unsigned int) num
.ul
;
592 *ARG (LONGLONG
int *) = num
.q
;
593 else if (flags
& LONG
)
594 *ARG (long int *) = num
.l
;
595 else if (flags
& SHORT
)
596 *ARG (short int *) = (short int) num
.l
;
598 *ARG (int *) = (int) num
.l
;
604 case 'e': /* Floating-point numbers. */
612 /* Check for a sign. */
613 if (c
== '-' || c
== '+')
616 if (inchar () == EOF
)
617 /* EOF is only an input error before we read any chars. */
628 else if (got_e
&& wp
[wpsize
- 1] == 'e'
629 && (c
== '-' || c
== '+'))
631 else if (!got_e
&& tolower (c
) == 'e')
636 else if (c
== decimal
&& !got_dot
)
641 else if ((flags
& GROUP
) && c
== thousands
&& !got_dot
)
647 } while (inchar () != EOF
&& width
!= 0);
651 if (wp
[wpsize
- 1] == '-' || wp
[wpsize
- 1] == '+'
652 || wp
[wpsize
- 1] == 'e')
655 /* Convert the number. */
659 long double d
= __strtold_internal (wp
, &tw
, flags
& GROUP
);
660 if (!(flags
& SUPPRESS
) && tw
!= wp
)
661 *ARG (long double *) = d
;
663 else if (flags
& LONG
)
665 double d
= __strtod_internal (wp
, &tw
, flags
& GROUP
);
666 if (!(flags
& SUPPRESS
) && tw
!= wp
)
671 float d
= __strtof_internal (wp
, &tw
, flags
& GROUP
);
672 if (!(flags
& SUPPRESS
) && tw
!= wp
)
679 if (!(flags
& SUPPRESS
))
683 case '[': /* Character class. */
697 /* Fill WP with byte flags indexed by character.
698 We will use this flag map for matching input characters. */
699 if (wpmax
< UCHAR_MAX
)
702 wp
= (char *) alloca (wpmax
);
704 memset (wp
, 0, UCHAR_MAX
);
707 if (fc
== ']' || fc
== '-')
709 /* If ] or - appears before any char in the set, it is not
710 the terminator or separator, but the first char in the
716 while ((fc
= *f
++) != '\0' && fc
!= ']')
718 if (fc
== '-' && *f
!= '\0' && *f
!= ']' &&
719 (unsigned char) f
[-2] <= (unsigned char) *f
)
721 /* Add all characters from the one before the '-'
722 up to (but not including) the next format char. */
723 for (fc
= f
[-2]; fc
< *f
; ++fc
)
727 /* Add the character to the flag map. */
741 } while (inchar () != EOF
&& width
!= 0);
742 if (read_in
== num
.ul
)
745 if (!(flags
& SUPPRESS
))
752 case 'p': /* Generic pointer. */
754 /* A PTR must be the same size as a `long int'. */
755 flags
&= ~(SHORT
|LONGDBL
);
762 /* The last thing we saw int the format string was a white space.
763 Consume the last white spaces. */
768 return ((void) (c
== EOF
|| ungetc (c
, s
)), done
);
773 __vfscanf (FILE *s
, const char *format
, va_list argptr
)
775 return _IO_vfscanf (s
, format
, argptr
, NULL
);
779 weak_alias (__vfscanf
, vfscanf
)