Update.
[glibc.git] / stdio-common / vfscanf.c
blob8ed56b3c08ca3cded0eae4e9f8491460ae51513b
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. */
19 #include <errno.h>
20 #include <limits.h>
21 #include <ctype.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <wctype.h>
27 #include <bits/libc-lock.h>
28 #include <locale/localeinfo.h>
30 #ifdef __GNUC__
31 # define HAVE_LONGLONG
32 # define LONGLONG long long
33 #else
34 # define LONGLONG long
35 #endif
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)
52 #ifdef USE_IN_LIBIO
53 # include <libioP.h>
54 # include <libio.h>
56 # undef va_list
57 # define va_list _IO_va_list
58 # define ungetc(c, s) ((void) ((int) c != EOF && --read_in), \
59 _IO_ungetc (c, s))
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); \
67 return done; \
68 } while (0)
69 # define conv_error() do { \
70 if (errp != NULL) *errp |= 2; \
71 _IO_funlockfile (s); \
72 return done; \
73 } while (0)
74 # define input_error() do { \
75 _IO_funlockfile (s); \
76 if (errp != NULL) *errp |= 1; \
77 return done ?: EOF; \
78 } while (0)
79 # define memory_error() do { \
80 _IO_funlockfile (s); \
81 __set_errno (ENOMEM); \
82 return EOF; \
83 } while (0)
84 # define ARGCHECK(s, format) \
85 do \
86 { \
87 /* Check file argument for consistence. */ \
88 CHECK_FILE (s, EOF); \
89 if (s->_flags & _IO_NO_READS) \
90 { \
91 __set_errno (EBADF); \
92 return EOF; \
93 } \
94 else if (format == NULL) \
95 { \
96 MAYBE_SET_EINVAL; \
97 return EOF; \
98 } \
99 } while (0)
100 # define LOCK_STREAM(S) \
101 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, (S)); \
102 _IO_flockfile (S)
103 # define UNLOCK_STREAM __libc_cleanup_region_end (1)
104 #else
105 # define ungetc(c, s) ((void) (c != EOF && --read_in), ungetc (c, s))
106 # define inchar() (c == EOF ? EOF \
107 : ((c = getc (s)), (void) (c != EOF && ++read_in), c))
108 # define encode_error() do { \
109 funlockfile (s); \
110 __set_errno (EILSEQ); \
111 return done; \
112 } while (0)
113 # define conv_error() do { \
114 funlockfile (s); \
115 return done; \
116 } while (0)
117 # define input_error() do { \
118 funlockfile (s); \
119 return done ?: EOF; \
120 } while (0)
121 # define memory_error() do { \
122 funlockfile (s); \
123 __set_errno (ENOMEM); \
124 return EOF; \
125 } while (0)
126 # define ARGCHECK(s, format) \
127 do \
129 /* Check file argument for consistence. */ \
130 if (!__validfp (s) || !s->__mode.__read) \
132 __set_errno (EBADF); \
133 return EOF; \
135 else if (format == NULL) \
137 __set_errno (EINVAL); \
138 return EOF; \
140 } while (0)
141 #if 1
142 /* XXX For now !!! */
143 # define flockfile(S) /* nothing */
144 # define funlockfile(S) /* nothing */
145 # define LOCK_STREAM(S)
146 # define UNLOCK_STREAM
147 #else
148 # define LOCK_STREAM(S) \
149 __libc_cleanup_region_start (&__funlockfile, (S)); \
150 __flockfile (S)
151 # define UNLOCK_STREAM __libc_cleanup_region_end (1)
152 #endif
153 #endif
156 /* Read formatted input from S according to the format string
157 FORMAT, using the argument list in ARG.
158 Return the number of assignments made, or -1 for an input error. */
159 #ifdef USE_IN_LIBIO
161 _IO_vfscanf (s, format, argptr, errp)
162 _IO_FILE *s;
163 const char *format;
164 _IO_va_list argptr;
165 int *errp;
166 #else
168 __vfscanf (FILE *s, const char *format, va_list argptr)
169 #endif
171 va_list arg;
172 register const char *f = format;
173 register unsigned char fc; /* Current character of the format. */
174 register size_t done = 0; /* Assignments done. */
175 register size_t read_in = 0; /* Chars read in. */
176 register int c = 0; /* Last char read. */
177 register int width; /* Maximum field width. */
178 register int flags; /* Modifiers for current format element. */
180 /* Status for reading F-P nums. */
181 char got_dot, got_e, negative;
182 /* If a [...] is a [^...]. */
183 char not_in;
184 #define exp_char not_in
185 /* Base for integral numbers. */
186 int base;
187 /* Signedness for integral numbers. */
188 int number_signed;
189 #define is_hexa number_signed
190 /* Decimal point character. */
191 wchar_t decimal;
192 /* The thousands character of the current locale. */
193 wchar_t thousands;
194 /* Integral holding variables. */
195 union
197 long long int q;
198 unsigned long long int uq;
199 long int l;
200 unsigned long int ul;
201 } num;
202 /* Character-buffer pointer. */
203 char *str = NULL;
204 wchar_t *wstr = NULL;
205 char **strptr = NULL;
206 size_t strsize = 0;
207 /* We must not react on white spaces immediately because they can
208 possibly be matched even if in the input stream no character is
209 available anymore. */
210 int skip_space = 0;
211 /* Workspace. */
212 char *tw; /* Temporary pointer. */
213 char *wp = NULL; /* Workspace. */
214 size_t wpmax = 0; /* Maximal size of workspace. */
215 size_t wpsize; /* Currently used bytes in workspace. */
216 #define ADDW(Ch) \
217 do \
219 if (wpsize == wpmax) \
221 char *old = wp; \
222 wpmax = UCHAR_MAX > 2 * wpmax ? UCHAR_MAX : 2 * wpmax; \
223 wp = (char *) alloca (wpmax); \
224 if (old != NULL) \
225 memcpy (wp, old, wpsize); \
227 wp[wpsize++] = (Ch); \
229 while (0)
231 #ifdef __va_copy
232 __va_copy (arg, argptr);
233 #else
234 arg = (va_list) argptr;
235 #endif
237 ARGCHECK (s, format);
239 /* Figure out the decimal point character. */
240 if (mbtowc (&decimal, _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT),
241 strlen (_NL_CURRENT (LC_NUMERIC, DECIMAL_POINT))) <= 0)
242 decimal = (wchar_t) *_NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
243 /* Figure out the thousands separator character. */
244 if (mbtowc (&thousands, _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
245 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
246 thousands = (wchar_t) *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
248 /* Lock the stream. */
249 LOCK_STREAM (s);
251 /* Run through the format string. */
252 while (*f != '\0')
254 unsigned int argpos;
255 /* Extract the next argument, which is of type TYPE.
256 For a %N$... spec, this is the Nth argument from the beginning;
257 otherwise it is the next argument after the state now in ARG. */
258 #ifdef __va_copy
259 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
260 ({ unsigned int pos = argpos; \
261 va_list arg; \
262 __va_copy (arg, argptr); \
263 while (--pos > 0) \
264 (void) va_arg (arg, void *); \
265 va_arg (arg, type); \
267 #else
268 # if 0
269 /* XXX Possible optimization. */
270 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
271 ({ va_list arg = (va_list) argptr; \
272 arg = (va_list) ((char *) arg \
273 + (argpos - 1) \
274 * __va_rounded_size (void *)); \
275 va_arg (arg, type); \
277 # else
278 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
279 ({ unsigned int pos = argpos; \
280 va_list arg = (va_list) argptr; \
281 while (--pos > 0) \
282 (void) va_arg (arg, void *); \
283 va_arg (arg, type); \
285 # endif
286 #endif
288 if (!isascii (*f))
290 /* Non-ASCII, may be a multibyte. */
291 int len = mblen (f, strlen (f));
292 if (len > 0)
296 c = inchar ();
297 if (c == EOF)
298 input_error ();
299 else if (c != *f++)
301 ungetc (c, s);
302 conv_error ();
305 while (--len > 0);
306 continue;
310 fc = *f++;
311 if (fc != '%')
313 /* Remember to skip spaces. */
314 if (isspace (fc))
316 skip_space = 1;
317 continue;
320 /* Read a character. */
321 c = inchar ();
323 /* Characters other than format specs must just match. */
324 if (c == EOF)
325 input_error ();
327 /* We saw white space char as the last character in the format
328 string. Now it's time to skip all leading white space. */
329 if (skip_space)
331 while (isspace (c))
332 if (inchar () == EOF && errno == EINTR)
333 conv_error ();
334 skip_space = 0;
337 if (c != fc)
339 ungetc (c, s);
340 conv_error ();
343 continue;
346 /* This is the start of the conversion string. */
347 flags = 0;
349 /* Initialize state of modifiers. */
350 argpos = 0;
352 /* Prepare temporary buffer. */
353 wpsize = 0;
355 /* Check for a positional parameter specification. */
356 if (isdigit (*f))
358 argpos = *f++ - '0';
359 while (isdigit (*f))
360 argpos = argpos * 10 + (*f++ - '0');
361 if (*f == '$')
362 ++f;
363 else
365 /* Oops; that was actually the field width. */
366 width = argpos;
367 flags |= WIDTH;
368 argpos = 0;
369 goto got_width;
373 /* Check for the assignment-suppressing and the number grouping flag. */
374 while (*f == '*' || *f == '\'')
375 switch (*f++)
377 case '*':
378 flags |= SUPPRESS;
379 break;
380 case '\'':
381 flags |= GROUP;
382 break;
385 /* We have seen width. */
386 if (isdigit (*f))
387 flags |= WIDTH;
389 /* Find the maximum field width. */
390 width = 0;
391 while (isdigit (*f))
393 width *= 10;
394 width += *f++ - '0';
396 got_width:
397 if (width == 0)
398 width = -1;
400 /* Check for type modifiers. */
401 while (*f == 'h' || *f == 'l' || *f == 'L' || *f == 'a' || *f == 'q')
402 switch (*f++)
404 case 'h':
405 /* int's are short int's. */
406 if (flags & (LONG|LONGDBL|CHAR))
407 /* Signal illegal format element. */
408 conv_error ();
409 if (flags & SHORT)
411 flags &= ~SHORT;
412 flags |= CHAR;
414 else
415 flags |= SHORT;
416 break;
417 case 'l':
418 if (flags & (SHORT|LONGDBL|CHAR))
419 conv_error ();
420 else if (flags & LONG)
422 /* A double `l' is equivalent to an `L'. */
423 flags &= ~LONG;
424 flags |= LONGDBL;
426 else
427 /* int's are long int's. */
428 flags |= LONG;
429 break;
430 case 'q':
431 case 'L':
432 /* double's are long double's, and int's are long long int's. */
433 if (flags & TYPEMOD)
434 /* Signal illegal format element. */
435 conv_error ();
436 flags |= LONGDBL;
437 break;
438 case 'a':
439 /* The `a' is used as a flag only if followed by `s', `S' or
440 `['. */
441 if (*f != 's' && *f != 'S' && *f != '[')
443 --f;
444 break;
446 if (flags & TYPEMOD)
447 /* Signal illegal format element. */
448 conv_error ();
449 /* String conversions (%s, %[) take a `char **'
450 arg and fill it in with a malloc'd pointer. */
451 flags |= MALLOC;
452 break;
455 /* End of the format string? */
456 if (*f == '\0')
457 conv_error ();
459 /* Find the conversion specifier. */
460 fc = *f++;
461 if (skip_space || (fc != '[' && fc != 'c' && fc != 'C' && fc != 'n'))
463 /* Eat whitespace. */
464 int save_errno = errno;
465 errno = 0;
467 if (inchar () == EOF && errno == EINTR)
468 input_error ();
469 while (isspace (c));
470 errno = save_errno;
471 ungetc (c, s);
472 skip_space = 0;
475 switch (fc)
477 case '%': /* Must match a literal '%'. */
478 c = inchar ();
479 if (c == EOF)
480 input_error ();
481 if (c != fc)
483 ungetc (c, s);
484 conv_error ();
486 break;
488 case 'n': /* Answer number of assignments done. */
489 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
490 with the 'n' conversion specifier. */
491 if (!(flags & SUPPRESS))
493 /* Don't count the read-ahead. */
494 if (flags & LONGDBL)
495 *ARG (long long int *) = read_in;
496 else if (flags & LONG)
497 *ARG (long int *) = read_in;
498 else if (flags & SHORT)
499 *ARG (short int *) = read_in;
500 else
501 *ARG (int *) = read_in;
503 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
504 /* We have a severe problem here. The ISO C standard
505 contradicts itself in explaining the effect of the %n
506 format in `scanf'. While in ISO C:1990 and the ISO C
507 Amendement 1:1995 the result is described as
509 Execution of a %n directive does not effect the
510 assignment count returned at the completion of
511 execution of the f(w)scanf function.
513 in ISO C Corrigendum 1:1994 the following was added:
515 Subclause 7.9.6.2
516 Add the following fourth example:
518 #include <stdio.h>
519 int d1, d2, n1, n2, i;
520 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
521 the value 123 is assigned to d1 and the value3 to n1.
522 Because %n can never get an input failure the value
523 of 3 is also assigned to n2. The value of d2 is not
524 affected. The value 3 is assigned to i.
526 We go for now with the historically correct code from ISO C,
527 i.e., we don't count the %n assignments. When it ever
528 should proof to be wrong just remove the #ifdef above. */
529 ++done;
530 #endif
532 break;
534 case 'c': /* Match characters. */
535 if ((flags & LONG) == 0)
537 if (!(flags & SUPPRESS))
539 str = ARG (char *);
540 if (str == NULL)
541 conv_error ();
544 c = inchar ();
545 if (c == EOF)
546 input_error ();
548 if (width == -1)
549 width = 1;
551 if (!(flags & SUPPRESS))
554 *str++ = c;
555 while (--width > 0 && inchar () != EOF);
557 else
558 while (--width > 0 && inchar () != EOF);
560 if (!(flags & SUPPRESS))
561 ++done;
563 break;
565 /* FALLTHROUGH */
566 case 'C':
567 /* Get UTF-8 encoded wide character. Here we assume (as in
568 other parts of the libc) that we only have to handle
569 UTF-8. */
571 wint_t val;
572 size_t cnt = 0;
573 int first = 1;
575 if (!(flags & SUPPRESS))
577 wstr = ARG (wchar_t *);
578 if (str == NULL)
579 conv_error ();
584 #define NEXT_WIDE_CHAR(First) \
585 c = inchar (); \
586 if (c == EOF) \
587 /* EOF is only an error for the first character. */ \
588 if (First) \
589 input_error (); \
590 else \
591 break; \
592 val = c; \
593 if (val >= 0x80) \
595 if ((c & 0xc0) == 0x80 || (c & 0xfe) == 0xfe) \
596 encode_error (); \
597 if ((c & 0xe0) == 0xc0) \
599 /* We expect two bytes. */ \
600 cnt = 1; \
601 val &= 0x1f; \
603 else if ((c & 0xf0) == 0xe0) \
605 /* We expect three bytes. */ \
606 cnt = 2; \
607 val &= 0x0f; \
609 else if ((c & 0xf8) == 0xf0) \
611 /* We expect four bytes. */ \
612 cnt = 3; \
613 val &= 0x07; \
615 else if ((c & 0xfc) == 0xf8) \
617 /* We expect five bytes. */ \
618 cnt = 4; \
619 val &= 0x03; \
621 else \
623 /* We expect six bytes. */ \
624 cnt = 5; \
625 val &= 0x01; \
628 do \
630 c = inchar (); \
631 if (c == EOF \
632 || (c & 0xc0) == 0x80 || (c & 0xfe) == 0xfe) \
633 encode_error (); \
634 val <<= 6; \
635 val |= c & 0x3f; \
637 while (--cnt > 0); \
640 if (!(flags & SUPPRESS)) \
641 *wstr++ = val; \
642 First = 0
644 NEXT_WIDE_CHAR (first);
646 while (--width > 0);
648 if (!(flags & SUPPRESS))
649 ++done;
651 break;
653 case 's': /* Read a string. */
654 if (flags & LONG)
655 /* We have to process a wide character string. */
656 goto wide_char_string;
658 #define STRING_ARG(Str, Type) \
659 if (!(flags & SUPPRESS)) \
661 if (flags & MALLOC) \
663 /* The string is to be stored in a malloc'd buffer. */ \
664 strptr = ARG (char **); \
665 if (strptr == NULL) \
666 conv_error (); \
667 /* Allocate an initial buffer. */ \
668 strsize = 100; \
669 *strptr = malloc (strsize * sizeof (Type)); \
670 Str = (Type *) *strptr; \
672 else \
673 Str = ARG (Type *); \
674 if (Str == NULL) \
675 conv_error (); \
677 STRING_ARG (str, char);
679 c = inchar ();
680 if (c == EOF)
681 input_error ();
685 if (isspace (c))
687 ungetc (c, s);
688 break;
690 #define STRING_ADD_CHAR(Str, c, Type) \
691 if (!(flags & SUPPRESS)) \
693 *Str++ = c; \
694 if ((flags & MALLOC) && (char *) Str == *strptr + strsize) \
696 /* Enlarge the buffer. */ \
697 Str = realloc (*strptr, strsize * 2 * sizeof (Type)); \
698 if (Str == NULL) \
700 /* Can't allocate that much. Last-ditch effort. */\
701 Str = realloc (*strptr, \
702 (strsize + 1) * sizeof (Type)); \
703 if (Str == NULL) \
705 /* We lose. Oh well. \
706 Terminate the string and stop converting, \
707 so at least we don't skip any input. */ \
708 ((Type *) (*strptr))[strsize] = '\0'; \
709 ++done; \
710 conv_error (); \
712 else \
714 *strptr = (char *) Str; \
715 Str = ((Type *) *strptr) + strsize; \
716 ++strsize; \
719 else \
721 *strptr = (char *) Str; \
722 Str = ((Type *) *strptr) + strsize; \
723 strsize *= 2; \
727 STRING_ADD_CHAR (str, c, char);
728 } while ((width <= 0 || --width > 0) && inchar () != EOF);
730 if (!(flags & SUPPRESS))
732 *str = '\0';
733 ++done;
735 break;
737 case 'S':
738 /* Wide character string. */
739 wide_char_string:
741 wint_t val;
742 int first = 1;
743 STRING_ARG (wstr, wchar_t);
747 size_t cnt = 0;
748 NEXT_WIDE_CHAR (first);
750 if (iswspace (val))
752 /* XXX We would have to push back the whole wide char
753 with possibly many bytes. But since scanf does
754 not make a difference for white space characters
755 we can simply push back a simple <SP> which is
756 guaranteed to be in the [:space:] class. */
757 ungetc (' ', s);
758 break;
761 STRING_ADD_CHAR (wstr, val, wchar_t);
762 first = 0;
764 while (width <= 0 || --width > 0);
766 if (!(flags & SUPPRESS))
768 *wstr = L'\0';
769 ++done;
772 break;
774 case 'x': /* Hexadecimal integer. */
775 case 'X': /* Ditto. */
776 base = 16;
777 number_signed = 0;
778 goto number;
780 case 'o': /* Octal integer. */
781 base = 8;
782 number_signed = 0;
783 goto number;
785 case 'u': /* Unsigned decimal integer. */
786 base = 10;
787 number_signed = 0;
788 goto number;
790 case 'd': /* Signed decimal integer. */
791 base = 10;
792 number_signed = 1;
793 goto number;
795 case 'i': /* Generic number. */
796 base = 0;
797 number_signed = 1;
799 number:
800 c = inchar ();
801 if (c == EOF)
802 input_error ();
804 /* Check for a sign. */
805 if (c == '-' || c == '+')
807 ADDW (c);
808 if (width > 0)
809 --width;
810 c = inchar ();
813 /* Look for a leading indication of base. */
814 if (width != 0 && c == '0')
816 if (width > 0)
817 --width;
819 ADDW (c);
820 c = inchar ();
822 if (width != 0 && tolower (c) == 'x')
824 if (base == 0)
825 base = 16;
826 if (base == 16)
828 if (width > 0)
829 --width;
830 c = inchar ();
833 else if (base == 0)
834 base = 8;
837 if (base == 0)
838 base = 10;
840 /* Read the number into workspace. */
841 while (c != EOF && width != 0)
843 if (base == 16 ? !isxdigit (c) :
844 ((!isdigit (c) || c - '0' >= base) &&
845 !((flags & GROUP) && base == 10 && c == thousands)))
846 break;
847 ADDW (c);
848 if (width > 0)
849 --width;
851 c = inchar ();
854 /* The just read character is not part of the number anymore. */
855 ungetc (c, s);
857 if (wpsize == 0 ||
858 (wpsize == 1 && (wp[0] == '+' || wp[0] == '-')))
859 /* There was no number. */
860 conv_error ();
862 /* Convert the number. */
863 ADDW ('\0');
864 if (flags & LONGDBL)
866 if (number_signed)
867 num.q = __strtoll_internal (wp, &tw, base, flags & GROUP);
868 else
869 num.uq = __strtoull_internal (wp, &tw, base, flags & GROUP);
871 else
873 if (number_signed)
874 num.l = __strtol_internal (wp, &tw, base, flags & GROUP);
875 else
876 num.ul = __strtoul_internal (wp, &tw, base, flags & GROUP);
878 if (wp == tw)
879 conv_error ();
881 if (!(flags & SUPPRESS))
883 if (! number_signed)
885 if (flags & LONGDBL)
886 *ARG (unsigned LONGLONG int *) = num.uq;
887 else if (flags & LONG)
888 *ARG (unsigned long int *) = num.ul;
889 else if (flags & SHORT)
890 *ARG (unsigned short int *)
891 = (unsigned short int) num.ul;
892 else if (flags & CHAR)
893 *ARG (unsigned char *) = (unsigned char) num.ul;
894 else
895 *ARG (unsigned int *) = (unsigned int) num.ul;
897 else
899 if (flags & LONGDBL)
900 *ARG (LONGLONG int *) = num.q;
901 else if (flags & LONG)
902 *ARG (long int *) = num.l;
903 else if (flags & SHORT)
904 *ARG (short int *) = (short int) num.l;
905 else if (flags & CHAR)
906 *ARG (signed char *) = (signed char) num.ul;
907 else
908 *ARG (int *) = (int) num.l;
910 ++done;
912 break;
914 case 'e': /* Floating-point numbers. */
915 case 'E':
916 case 'f':
917 case 'g':
918 case 'G':
919 case 'a':
920 case 'A':
921 c = inchar ();
922 if (c == EOF)
923 input_error ();
925 /* Check for a sign. */
926 if (c == '-' || c == '+')
928 negative = c == '-';
929 if (inchar () == EOF)
930 /* EOF is only an input error before we read any chars. */
931 conv_error ();
932 if (width > 0)
933 --width;
935 else
936 negative = 0;
938 is_hexa = 0;
939 exp_char = 'e';
940 if (c == '0')
942 ADDW (c);
943 c = inchar ();
944 if (tolower (c) == 'x')
946 /* It is a number in hexadecimal format. */
947 ADDW (c);
949 is_hexa = 1;
950 exp_char = 'p';
952 /* Grouping is not allowed. */
953 flags &= ~GROUP;
954 c = inchar ();
958 got_dot = got_e = 0;
961 if (isdigit (c))
962 ADDW (c);
963 else if (!got_e && is_hexa && isxdigit (c))
964 ADDW (c);
965 else if (got_e && wp[wpsize - 1] == exp_char
966 && (c == '-' || c == '+'))
967 ADDW (c);
968 else if (wpsize > 0 && !got_e && tolower (c) == exp_char)
970 ADDW (exp_char);
971 got_e = got_dot = 1;
973 else if (c == decimal && !got_dot)
975 ADDW (c);
976 got_dot = 1;
978 else if ((flags & GROUP) && c == thousands && !got_dot)
979 ADDW (c);
980 else
982 /* The last read character is not part of the number
983 anymore. */
984 ungetc (c, s);
985 break;
987 if (width > 0)
988 --width;
990 while (width != 0 && inchar () != EOF);
992 /* Have we read any character? If we try to read a number
993 in hexadecimal notation and we have read only the `0x'
994 prefix this is an error. */
995 if (wpsize == 0 || (is_hexa && wpsize == 2))
996 conv_error ();
998 /* Convert the number. */
999 ADDW ('\0');
1000 if (flags & LONGDBL)
1002 long double d = __strtold_internal (wp, &tw, flags & GROUP);
1003 if (!(flags & SUPPRESS) && tw != wp)
1004 *ARG (long double *) = negative ? -d : d;
1006 else if (flags & LONG)
1008 double d = __strtod_internal (wp, &tw, flags & GROUP);
1009 if (!(flags & SUPPRESS) && tw != wp)
1010 *ARG (double *) = negative ? -d : d;
1012 else
1014 float d = __strtof_internal (wp, &tw, flags & GROUP);
1015 if (!(flags & SUPPRESS) && tw != wp)
1016 *ARG (float *) = negative ? -d : d;
1019 if (tw == wp)
1020 conv_error ();
1022 if (!(flags & SUPPRESS))
1023 ++done;
1024 break;
1026 case '[': /* Character class. */
1027 if (flags & LONG)
1029 STRING_ARG (wstr, wchar_t);
1030 c = '\0'; /* This is to keep gcc quiet. */
1032 else
1034 STRING_ARG (str, char);
1036 c = inchar ();
1037 if (c == EOF)
1038 input_error ();
1041 if (*f == '^')
1043 ++f;
1044 not_in = 1;
1046 else
1047 not_in = 0;
1049 /* Fill WP with byte flags indexed by character.
1050 We will use this flag map for matching input characters. */
1051 if (wpmax < UCHAR_MAX)
1053 wpmax = UCHAR_MAX;
1054 wp = (char *) alloca (wpmax);
1056 memset (wp, 0, UCHAR_MAX);
1058 fc = *f;
1059 if (fc == ']' || fc == '-')
1061 /* If ] or - appears before any char in the set, it is not
1062 the terminator or separator, but the first char in the
1063 set. */
1064 wp[fc] = 1;
1065 ++f;
1068 while ((fc = *f++) != '\0' && fc != ']')
1070 if (fc == '-' && *f != '\0' && *f != ']' &&
1071 (unsigned char) f[-2] <= (unsigned char) *f)
1073 /* Add all characters from the one before the '-'
1074 up to (but not including) the next format char. */
1075 for (fc = f[-2]; fc < *f; ++fc)
1076 wp[fc] = 1;
1078 else
1079 /* Add the character to the flag map. */
1080 wp[fc] = 1;
1082 if (fc == '\0')
1084 if (!(flags & LONG))
1085 ungetc (c, s);
1086 conv_error();
1089 if (flags & LONG)
1091 wint_t val;
1092 int first = 1;
1096 size_t cnt = 0;
1097 NEXT_WIDE_CHAR (first);
1098 if (val > 255 || wp[val] == not_in)
1100 /* XXX We have a problem here. We read a wide
1101 character and this possibly took several
1102 bytes. But we can only push back one single
1103 character. To be sure we don't create wrong
1104 input we push it back only in case it is
1105 representable within one byte. */
1106 if (val < 0x80)
1107 ungetc (val, s);
1108 break;
1110 STRING_ADD_CHAR (wstr, val, wchar_t);
1111 if (width > 0)
1112 --width;
1113 first = 0;
1115 while (width != 0);
1117 if (first)
1118 conv_error ();
1120 if (!(flags & SUPPRESS))
1122 *wstr = L'\0';
1123 ++done;
1126 else
1128 num.ul = read_in - 1; /* -1 because we already read one char. */
1131 if (wp[c] == not_in)
1133 ungetc (c, s);
1134 break;
1136 STRING_ADD_CHAR (str, c, char);
1137 if (width > 0)
1138 --width;
1140 while (width != 0 && inchar () != EOF);
1142 if (read_in == num.ul)
1143 conv_error ();
1145 if (!(flags & SUPPRESS))
1147 *str = '\0';
1148 ++done;
1151 break;
1153 case 'p': /* Generic pointer. */
1154 base = 16;
1155 /* A PTR must be the same size as a `long int'. */
1156 flags &= ~(SHORT|LONGDBL);
1157 flags |= LONG;
1158 number_signed = 0;
1159 goto number;
1163 /* The last thing we saw int the format string was a white space.
1164 Consume the last white spaces. */
1165 if (skip_space)
1168 c = inchar ();
1169 while (isspace (c));
1170 ungetc (c, s);
1173 /* Unlock stream. */
1174 UNLOCK_STREAM;
1176 return done;
1179 #ifdef USE_IN_LIBIO
1181 __vfscanf (FILE *s, const char *format, va_list argptr)
1183 return _IO_vfscanf (s, format, argptr, NULL);
1185 #endif
1187 weak_alias (__vfscanf, vfscanf)