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. */
19 #include "../locale/localeinfo.h"
27 #include <libc-lock.h>
31 #define LONGLONG long long
36 /* Those are flags in the conversion format. */
37 # define LONG 0x001 /* l: long or double */
38 # define LONGDBL 0x002 /* L: long long or long double */
39 # define SHORT 0x004 /* h: short */
40 # define SUPPRESS 0x008 /* *: suppress assignment */
41 # define POINTER 0x010 /* weird %p pointer (`fake hex') */
42 # define NOSKIP 0x020 /* do not skip blanks */
43 # define WIDTH 0x040 /* width was given */
44 # define GROUP 0x080 /* ': group numbers */
45 # define MALLOC 0x100 /* a: malloc strings */
47 # define TYPEMOD (LONG|LONGDBL|SHORT)
55 # define va_list _IO_va_list
56 # define ungetc(c, s) (--read_in, _IO_ungetc (c, s))
57 # define inchar() ((c = _IO_getc_unlocked (s)), (void) ++read_in, c)
58 # define conv_error() do { \
59 if (errp != NULL) *errp |= 2; \
60 _IO_funlockfile (s); \
63 # define input_error() do { \
64 _IO_funlockfile (s); \
65 if (errp != NULL) *errp |= 1; \
68 # define memory_error() do { \
69 _IO_funlockfile (s); \
70 __set_errno (ENOMEM); \
73 # define ARGCHECK(s, format) \
76 /* Check file argument for consistence. */ \
77 CHECK_FILE (s, EOF); \
78 if (s->_flags & _IO_NO_READS || format == NULL) \
84 # define LOCK_STREAM(S) \
85 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, (S)); \
87 # define UNLOCK_STREAM __libc_cleanup_region_end (1)
89 # define ungetc(c, s) (--read_in, ungetc (c, s))
90 # define inchar() ((c = getc (s)), (void) ++read_in, c)
91 # define conv_error() do { \
95 # define input_error() do { \
99 # define memory_error() do { \
101 __set_errno (ENOMEM); \
104 # define ARGCHECK(s, format) \
107 /* Check file argument for consistence. */ \
108 if (!__validfp (s) || !s->__mode.__read || format == NULL) \
110 __set_errno (EINVAL); \
115 /* XXX For now !!! */
116 # define flockfile(S) /* nothing */
117 # define funlockfile(S) /* nothing */
118 # define LOCK_STREAM(S)
119 # define UNLOCK_STREAM
121 # define LOCK_STREAM(S) \
122 __libc_cleanup_region_start (&__funlockfile, (S)); \
124 # define UNLOCK_STREAM __libc_cleanup_region_end (1)
129 /* Read formatted input from S according to the format string
130 FORMAT, using the argument list in ARG.
131 Return the number of assignments made, or -1 for an input error. */
134 _IO_vfscanf (s
, format
, argptr
, errp
)
141 __vfscanf (FILE *s
, const char *format
, va_list argptr
)
144 va_list arg
= (va_list) argptr
;
146 register const char *f
= format
;
147 register unsigned char fc
; /* Current character of the format. */
148 register size_t done
= 0; /* Assignments done. */
149 register size_t read_in
= 0; /* Chars read in. */
150 register int c
; /* Last char read. */
151 register int width
; /* Maximum field width. */
152 register int flags
; /* Modifiers for current format element. */
154 /* Status for reading F-P nums. */
155 char got_dot
, got_e
, negative
;
156 /* If a [...] is a [^...]. */
158 /* Base for integral numbers. */
160 /* Signedness for integral numbers. */
162 /* Decimal point character. */
164 /* The thousands character of the current locale. */
166 /* Integral holding variables. */
170 unsigned long long int uq
;
172 unsigned long int ul
;
174 /* Character-buffer pointer. */
176 char **strptr
= NULL
;
178 /* We must not react on white spaces immediately because they can
179 possibly be matched even if in the input stream no character is
180 available anymore. */
183 char *tw
; /* Temporary pointer. */
184 char *wp
= NULL
; /* Workspace. */
185 size_t wpmax
= 0; /* Maximal size of workspace. */
186 size_t wpsize
; /* Currently used bytes in workspace. */
190 if (wpsize == wpmax) \
193 wpmax = UCHAR_MAX > 2 * wpmax ? UCHAR_MAX : 2 * wpmax; \
194 wp = (char *) alloca (wpmax); \
196 memcpy (wp, old, wpsize); \
198 wp[wpsize++] = (Ch); \
202 ARGCHECK (s
, format
);
204 /* Figure out the decimal point character. */
205 if (mbtowc (&decimal
, _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
),
206 strlen (_NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
))) <= 0)
207 decimal
= (wchar_t) *_NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
208 /* Figure out the thousands separator character. */
209 if (mbtowc (&thousands
, _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
),
210 strlen (_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
))) <= 0)
211 thousands
= (wchar_t) *_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
213 /* Lock the stream. */
216 /* Run through the format string. */
220 /* Extract the next argument, which is of type TYPE.
221 For a %N$... spec, this is the Nth argument from the beginning;
222 otherwise it is the next argument after the state now in ARG. */
224 /* XXX Possible optimization. */
225 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
226 ({ va_list arg = (va_list) argptr; \
227 arg = (va_list) ((char *) arg \
229 * __va_rounded_size (void *)); \
230 va_arg (arg, type); \
233 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
234 ({ unsigned int pos = argpos; \
235 va_list arg = (va_list) argptr; \
237 (void) va_arg (arg, void *); \
238 va_arg (arg, type); \
244 /* Non-ASCII, may be a multibyte. */
245 int len
= mblen (f
, strlen (f
));
267 /* Remember to skip spaces. */
274 /* Read a character. */
277 /* Characters other than format specs must just match. */
281 /* We saw white space char as the last character in the format
282 string. Now it's time to skip all leading white space. */
299 /* This is the start of the conversion string. */
302 /* Initialize state of modifiers. */
305 /* Prepare temporary buffer. */
308 /* Check for a positional parameter specification. */
313 argpos
= argpos
* 10 + (*f
++ - '0');
318 /* Oops; that was actually the field width. */
326 /* Check for the assignment-suppressant and the number grouping flag. */
327 while (*f
== '*' || *f
== '\'')
338 /* We have seen width. */
342 /* Find the maximum field width. */
353 /* Check for type modifiers. */
354 while (*f
== 'h' || *f
== 'l' || *f
== 'L' || *f
== 'a' || *f
== 'q')
358 /* int's are short int's. */
360 /* Signal illegal format element. */
365 if (flags
& (SHORT
|LONGDBL
))
367 else if (flags
& LONG
)
369 /* A double `l' is equivalent to an `L'. */
374 /* int's are long int's. */
379 /* double's are long double's, and int's are long long int's. */
381 /* Signal illegal format element. */
387 /* Signal illegal format element. */
389 /* String conversions (%s, %[) take a `char **'
390 arg and fill it in with a malloc'd pointer. */
395 /* End of the format string? */
399 /* Find the conversion specifier. */
401 if (skip_space
|| (fc
!= '[' && fc
!= 'c' && fc
!= 'n'))
403 /* Eat whitespace. */
413 case '%': /* Must match a literal '%'. */
422 case 'n': /* Answer number of assignments done. */
423 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
424 with the 'n' conversion specifier. */
425 if (!(flags
& SUPPRESS
))
426 /* Don't count the read-ahead. */
428 *ARG (long long int *) = read_in
;
429 else if (flags
& LONG
)
430 *ARG (long int *) = read_in
;
431 else if (flags
& SHORT
)
432 *ARG (short int *) = read_in
;
434 *ARG (int *) = read_in
;
437 case 'c': /* Match characters. */
438 if (!(flags
& SUPPRESS
))
452 if (!(flags
& SUPPRESS
))
456 while (--width
> 0 && inchar () != EOF
);
459 while (--width
> 0 && inchar () != EOF
);
462 /* I.e., EOF was read. */
465 if (!(flags
& SUPPRESS
))
470 case 's': /* Read a string. */
472 if (!(flags & SUPPRESS)) \
474 if (flags & MALLOC) \
476 /* The string is to be stored in a malloc'd buffer. */ \
477 strptr = ARG (char **); \
478 if (strptr == NULL) \
480 /* Allocate an initial buffer. */ \
482 *strptr = str = malloc (strsize); \
485 str = ARG (char *); \
502 #define STRING_ADD_CHAR(c) \
503 if (!(flags & SUPPRESS)) \
506 if ((flags & MALLOC) && str == *strptr + strsize) \
508 /* Enlarge the buffer. */ \
509 str = realloc (*strptr, strsize * 2); \
512 /* Can't allocate that much. Last-ditch effort. */\
513 str = realloc (*strptr, strsize + 1); \
516 /* We lose. Oh well. \
517 Terminate the string and stop converting, \
518 so at least we don't skip any input. */ \
519 (*strptr)[strsize] = '\0'; \
539 } while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
541 if (!(flags
& SUPPRESS
))
548 case 'x': /* Hexadecimal integer. */
549 case 'X': /* Ditto. */
554 case 'o': /* Octal integer. */
559 case 'u': /* Unsigned decimal integer. */
564 case 'd': /* Signed decimal integer. */
569 case 'i': /* Generic number. */
578 /* Check for a sign. */
579 if (c
== '-' || c
== '+')
587 /* Look for a leading indication of base. */
588 if (width
!= 0 && c
== '0')
596 if (width
!= 0 && tolower (c
) == 'x')
614 /* Read the number into workspace. */
615 while (c
!= EOF
&& width
!= 0)
617 if (base
== 16 ? !isxdigit (c
) :
618 ((!isdigit (c
) || c
- '0' >= base
) &&
619 !((flags
& GROUP
) && base
== 10 && c
== thousands
)))
628 /* The just read character is not part of the number anymore. */
632 (wpsize
== 1 && (wp
[0] == '+' || wp
[0] == '-')))
633 /* There was no number. */
636 /* Convert the number. */
641 num
.q
= __strtoq_internal (wp
, &tw
, base
, flags
& GROUP
);
643 num
.uq
= __strtouq_internal (wp
, &tw
, base
, flags
& GROUP
);
648 num
.l
= __strtol_internal (wp
, &tw
, base
, flags
& GROUP
);
650 num
.ul
= __strtoul_internal (wp
, &tw
, base
, flags
& GROUP
);
655 if (!(flags
& SUPPRESS
))
660 *ARG (unsigned LONGLONG
int *) = num
.uq
;
661 else if (flags
& LONG
)
662 *ARG (unsigned long int *) = num
.ul
;
663 else if (flags
& SHORT
)
664 *ARG (unsigned short int *)
665 = (unsigned short int) num
.ul
;
667 *ARG (unsigned int *) = (unsigned int) num
.ul
;
672 *ARG (LONGLONG
int *) = num
.q
;
673 else if (flags
& LONG
)
674 *ARG (long int *) = num
.l
;
675 else if (flags
& SHORT
)
676 *ARG (short int *) = (short int) num
.l
;
678 *ARG (int *) = (int) num
.l
;
684 case 'e': /* Floating-point numbers. */
693 /* Check for a sign. */
694 if (c
== '-' || c
== '+')
697 if (inchar () == EOF
)
698 /* EOF is only an input error before we read any chars. */
711 else if (got_e
&& wp
[wpsize
- 1] == 'e'
712 && (c
== '-' || c
== '+'))
714 else if (wpsize
> 0 && !got_e
&& tolower (c
) == 'e')
719 else if (c
== decimal
&& !got_dot
)
724 else if ((flags
& GROUP
) && c
== thousands
&& !got_dot
)
731 while (inchar () != EOF
&& width
!= 0);
733 /* The last read character is not part of the number anymore. */
739 /* Convert the number. */
743 long double d
= __strtold_internal (wp
, &tw
, flags
& GROUP
);
744 if (!(flags
& SUPPRESS
) && tw
!= wp
)
745 *ARG (long double *) = negative
? -d
: d
;
747 else if (flags
& LONG
)
749 double d
= __strtod_internal (wp
, &tw
, flags
& GROUP
);
750 if (!(flags
& SUPPRESS
) && tw
!= wp
)
751 *ARG (double *) = negative
? -d
: d
;
755 float d
= __strtof_internal (wp
, &tw
, flags
& GROUP
);
756 if (!(flags
& SUPPRESS
) && tw
!= wp
)
757 *ARG (float *) = negative
? -d
: d
;
763 if (!(flags
& SUPPRESS
))
767 case '[': /* Character class. */
782 /* Fill WP with byte flags indexed by character.
783 We will use this flag map for matching input characters. */
784 if (wpmax
< UCHAR_MAX
)
787 wp
= (char *) alloca (wpmax
);
789 memset (wp
, 0, UCHAR_MAX
);
792 if (fc
== ']' || fc
== '-')
794 /* If ] or - appears before any char in the set, it is not
795 the terminator or separator, but the first char in the
801 while ((fc
= *f
++) != '\0' && fc
!= ']')
803 if (fc
== '-' && *f
!= '\0' && *f
!= ']' &&
804 (unsigned char) f
[-2] <= (unsigned char) *f
)
806 /* Add all characters from the one before the '-'
807 up to (but not including) the next format char. */
808 for (fc
= f
[-2]; fc
< *f
; ++fc
)
812 /* Add the character to the flag map. */
821 num
.ul
= read_in
- 1; /* -1 because we already read one char. */
833 while (width
!= 0 && inchar () != EOF
);
835 if (read_in
== num
.ul
)
838 if (!(flags
& SUPPRESS
))
845 case 'p': /* Generic pointer. */
847 /* A PTR must be the same size as a `long int'. */
848 flags
&= ~(SHORT
|LONGDBL
);
855 /* The last thing we saw int the format string was a white space.
856 Consume the last white spaces. */
873 __vfscanf (FILE *s
, const char *format
, va_list argptr
)
875 return _IO_vfscanf (s
, format
, argptr
, NULL
);
879 weak_alias (__vfscanf
, vfscanf
)