Change to generate file handling __need_FOPEN_MAX.
[glibc/history.git] / stdio-common / vfscanf.c
blob9f8eba9c4cbcd35ca22276b01655c636d2d6ebaf
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. */
19 #include "../locale/localeinfo.h"
20 #include <errno.h>
21 #include <limits.h>
22 #include <ctype.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <wctype.h>
28 #include <bits/libc-lock.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 = _IO_getc_unlocked (s)), \
61 (void) (c != EOF && ++read_in), c)
62 # define encode_error() do { \
63 if (errp != NULL) *errp |= 4; \
64 _IO_funlockfile (s); \
65 __set_errno (EILSEQ); \
66 return done; \
67 } while (0)
68 # define conv_error() do { \
69 if (errp != NULL) *errp |= 2; \
70 _IO_funlockfile (s); \
71 return done; \
72 } while (0)
73 # define input_error() do { \
74 _IO_funlockfile (s); \
75 if (errp != NULL) *errp |= 1; \
76 return done ?: EOF; \
77 } while (0)
78 # define memory_error() do { \
79 _IO_funlockfile (s); \
80 __set_errno (ENOMEM); \
81 return EOF; \
82 } while (0)
83 # define ARGCHECK(s, format) \
84 do \
85 { \
86 /* Check file argument for consistence. */ \
87 CHECK_FILE (s, EOF); \
88 if (s->_flags & _IO_NO_READS) \
89 { \
90 __set_errno (EBADF); \
91 return EOF; \
92 } \
93 else if (format == NULL) \
94 { \
95 MAYBE_SET_EINVAL; \
96 return EOF; \
97 } \
98 } while (0)
99 # define LOCK_STREAM(S) \
100 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, (S)); \
101 _IO_flockfile (S)
102 # define UNLOCK_STREAM __libc_cleanup_region_end (1)
103 #else
104 # define ungetc(c, s) ((void) (c != EOF && --read_in), ungetc (c, s))
105 # define inchar() ((c = getc (s)), (void) (c != EOF && ++read_in), c)
106 # define encode_error() do { \
107 funlockfile (s); \
108 __set_errno (EILSEQ); \
109 return done; \
110 } while (0)
111 # define conv_error() do { \
112 funlockfile (s); \
113 return done; \
114 } while (0)
115 # define input_error() do { \
116 funlockfile (s); \
117 return done ?: EOF; \
118 } while (0)
119 # define memory_error() do { \
120 funlockfile (s); \
121 __set_errno (ENOMEM); \
122 return EOF; \
123 } while (0)
124 # define ARGCHECK(s, format) \
125 do \
127 /* Check file argument for consistence. */ \
128 if (!__validfp (s) || !s->__mode.__read) \
130 __set_errno (EBADF); \
131 return EOF; \
133 else if (format == NULL) \
135 __set_errno (EINVAL); \
136 return EOF; \
138 } while (0)
139 #if 1
140 /* XXX For now !!! */
141 # define flockfile(S) /* nothing */
142 # define funlockfile(S) /* nothing */
143 # define LOCK_STREAM(S)
144 # define UNLOCK_STREAM
145 #else
146 # define LOCK_STREAM(S) \
147 __libc_cleanup_region_start (&__funlockfile, (S)); \
148 __flockfile (S)
149 # define UNLOCK_STREAM __libc_cleanup_region_end (1)
150 #endif
151 #endif
154 /* Read formatted input from S according to the format string
155 FORMAT, using the argument list in ARG.
156 Return the number of assignments made, or -1 for an input error. */
157 #ifdef USE_IN_LIBIO
159 _IO_vfscanf (s, format, argptr, errp)
160 _IO_FILE *s;
161 const char *format;
162 _IO_va_list argptr;
163 int *errp;
164 #else
166 __vfscanf (FILE *s, const char *format, va_list argptr)
167 #endif
169 va_list arg;
170 register const char *f = format;
171 register unsigned char fc; /* Current character of the format. */
172 register size_t done = 0; /* Assignments done. */
173 register size_t read_in = 0; /* Chars read in. */
174 register int c = 0; /* Last char read. */
175 register int width; /* Maximum field width. */
176 register int flags; /* Modifiers for current format element. */
178 /* Status for reading F-P nums. */
179 char got_dot, got_e, negative;
180 /* If a [...] is a [^...]. */
181 char not_in;
182 #define exp_char not_in
183 /* Base for integral numbers. */
184 int base;
185 /* Signedness for integral numbers. */
186 int number_signed;
187 #define is_hexa number_signed
188 /* Decimal point character. */
189 wchar_t decimal;
190 /* The thousands character of the current locale. */
191 wchar_t thousands;
192 /* Integral holding variables. */
193 union
195 long long int q;
196 unsigned long long int uq;
197 long int l;
198 unsigned long int ul;
199 } num;
200 /* Character-buffer pointer. */
201 char *str = NULL;
202 wchar_t *wstr = NULL;
203 char **strptr = NULL;
204 size_t strsize = 0;
205 /* We must not react on white spaces immediately because they can
206 possibly be matched even if in the input stream no character is
207 available anymore. */
208 int skip_space = 0;
209 /* Workspace. */
210 char *tw; /* Temporary pointer. */
211 char *wp = NULL; /* Workspace. */
212 size_t wpmax = 0; /* Maximal size of workspace. */
213 size_t wpsize; /* Currently used bytes in workspace. */
214 #define ADDW(Ch) \
215 do \
217 if (wpsize == wpmax) \
219 char *old = wp; \
220 wpmax = UCHAR_MAX > 2 * wpmax ? UCHAR_MAX : 2 * wpmax; \
221 wp = (char *) alloca (wpmax); \
222 if (old != NULL) \
223 memcpy (wp, old, wpsize); \
225 wp[wpsize++] = (Ch); \
227 while (0)
229 #ifdef __va_copy
230 __va_copy (arg, argptr);
231 #else
232 arg = (va_list) argptr;
233 #endif
235 ARGCHECK (s, format);
237 /* Figure out the decimal point character. */
238 if (mbtowc (&decimal, _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT),
239 strlen (_NL_CURRENT (LC_NUMERIC, DECIMAL_POINT))) <= 0)
240 decimal = (wchar_t) *_NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
241 /* Figure out the thousands separator character. */
242 if (mbtowc (&thousands, _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
243 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
244 thousands = (wchar_t) *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
246 /* Lock the stream. */
247 LOCK_STREAM (s);
249 /* Run through the format string. */
250 while (*f != '\0')
252 unsigned int argpos;
253 /* Extract the next argument, which is of type TYPE.
254 For a %N$... spec, this is the Nth argument from the beginning;
255 otherwise it is the next argument after the state now in ARG. */
256 #ifdef __va_copy
257 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
258 ({ unsigned int pos = argpos; \
259 va_list arg; \
260 __va_copy (arg, argptr); \
261 while (--pos > 0) \
262 (void) va_arg (arg, void *); \
263 va_arg (arg, type); \
265 #else
266 # if 0
267 /* XXX Possible optimization. */
268 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
269 ({ va_list arg = (va_list) argptr; \
270 arg = (va_list) ((char *) arg \
271 + (argpos - 1) \
272 * __va_rounded_size (void *)); \
273 va_arg (arg, type); \
275 # else
276 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
277 ({ unsigned int pos = argpos; \
278 va_list arg = (va_list) argptr; \
279 while (--pos > 0) \
280 (void) va_arg (arg, void *); \
281 va_arg (arg, type); \
283 # endif
284 #endif
286 if (!isascii (*f))
288 /* Non-ASCII, may be a multibyte. */
289 int len = mblen (f, strlen (f));
290 if (len > 0)
294 c = inchar ();
295 if (c == EOF)
296 input_error ();
297 else if (c != *f++)
299 ungetc (c, s);
300 conv_error ();
303 while (--len > 0);
304 continue;
308 fc = *f++;
309 if (fc != '%')
311 /* Remember to skip spaces. */
312 if (isspace (fc))
314 skip_space = 1;
315 continue;
318 /* Read a character. */
319 c = inchar ();
321 /* Characters other than format specs must just match. */
322 if (c == EOF)
323 input_error ();
325 /* We saw white space char as the last character in the format
326 string. Now it's time to skip all leading white space. */
327 if (skip_space)
329 while (isspace (c))
330 if (inchar () == EOF && errno == EINTR)
331 conv_error ();
332 skip_space = 0;
335 if (c != fc)
337 ungetc (c, s);
338 conv_error ();
341 continue;
344 /* This is the start of the conversion string. */
345 flags = 0;
347 /* Initialize state of modifiers. */
348 argpos = 0;
350 /* Prepare temporary buffer. */
351 wpsize = 0;
353 /* Check for a positional parameter specification. */
354 if (isdigit (*f))
356 argpos = *f++ - '0';
357 while (isdigit (*f))
358 argpos = argpos * 10 + (*f++ - '0');
359 if (*f == '$')
360 ++f;
361 else
363 /* Oops; that was actually the field width. */
364 width = argpos;
365 flags |= WIDTH;
366 argpos = 0;
367 goto got_width;
371 /* Check for the assignment-suppressing and the number grouping flag. */
372 while (*f == '*' || *f == '\'')
373 switch (*f++)
375 case '*':
376 flags |= SUPPRESS;
377 break;
378 case '\'':
379 flags |= GROUP;
380 break;
383 /* We have seen width. */
384 if (isdigit (*f))
385 flags |= WIDTH;
387 /* Find the maximum field width. */
388 width = 0;
389 while (isdigit (*f))
391 width *= 10;
392 width += *f++ - '0';
394 got_width:
395 if (width == 0)
396 width = -1;
398 /* Check for type modifiers. */
399 while (*f == 'h' || *f == 'l' || *f == 'L' || *f == 'a' || *f == 'q')
400 switch (*f++)
402 case 'h':
403 /* int's are short int's. */
404 if (flags & (LONG|LONGDBL|CHAR))
405 /* Signal illegal format element. */
406 conv_error ();
407 if (flags & SHORT)
409 flags &= ~SHORT;
410 flags |= CHAR;
412 else
413 flags |= SHORT;
414 break;
415 case 'l':
416 if (flags & (SHORT|LONGDBL|CHAR))
417 conv_error ();
418 else if (flags & LONG)
420 /* A double `l' is equivalent to an `L'. */
421 flags &= ~LONG;
422 flags |= LONGDBL;
424 else
425 /* int's are long int's. */
426 flags |= LONG;
427 break;
428 case 'q':
429 case 'L':
430 /* double's are long double's, and int's are long long int's. */
431 if (flags & TYPEMOD)
432 /* Signal illegal format element. */
433 conv_error ();
434 flags |= LONGDBL;
435 break;
436 case 'a':
437 /* The `a' is used as a flag only if followed by `s', `S' or
438 `['. */
439 if (*f != 's' && *f != 'S' && *f != '[')
441 --f;
442 break;
444 if (flags & TYPEMOD)
445 /* Signal illegal format element. */
446 conv_error ();
447 /* String conversions (%s, %[) take a `char **'
448 arg and fill it in with a malloc'd pointer. */
449 flags |= MALLOC;
450 break;
453 /* End of the format string? */
454 if (*f == '\0')
455 conv_error ();
457 /* We must take care for EINTR errors. */
458 if (c == EOF && errno == EINTR)
459 input_error ();
461 /* Find the conversion specifier. */
462 fc = *f++;
463 if (skip_space || (fc != '[' && fc != 'c' && fc != 'C' && fc != 'n'))
465 /* Eat whitespace. */
467 if (inchar () == EOF && errno == EINTR)
468 input_error ();
469 while (isspace (c));
470 ungetc (c, s);
471 skip_space = 0;
474 switch (fc)
476 case '%': /* Must match a literal '%'. */
477 c = inchar ();
478 if (c != fc)
480 ungetc (c, s);
481 conv_error ();
483 break;
485 case 'n': /* Answer number of assignments done. */
486 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
487 with the 'n' conversion specifier. */
488 if (!(flags & SUPPRESS))
490 /* Don't count the read-ahead. */
491 if (flags & LONGDBL)
492 *ARG (long long int *) = read_in;
493 else if (flags & LONG)
494 *ARG (long int *) = read_in;
495 else if (flags & SHORT)
496 *ARG (short int *) = read_in;
497 else
498 *ARG (int *) = read_in;
500 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
501 /* We have a severe problem here. The ISO C standard
502 contradicts itself in explaining the effect of the %n
503 format in `scanf'. While in ISO C:1990 and the ISO C
504 Amendement 1:1995 the result is described as
506 Execution of a %n directive does not effect the
507 assignment count returned at the completion of
508 execution of the f(w)scanf function.
510 in ISO C Corrigendum 1:1994 the following was added:
512 Subclause 7.9.6.2
513 Add the following fourth example:
515 #include <stdio.h>
516 int d1, d2, n1, n2, i;
517 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
518 the value 123 is assigned to d1 and the value3 to n1.
519 Because %n can never get an input failure the value
520 of 3 is also assigned to n2. The value of d2 is not
521 affected. The value 3 is assigned to i.
523 We go for now with the historically correct code fro ISO C,
524 i.e., we don't count the %n assignments. When it ever
525 should proof to be wrong just remove the #ifdef above. */
526 ++done;
527 #endif
529 break;
531 case 'c': /* Match characters. */
532 if ((flags & LONG) == 0)
534 if (!(flags & SUPPRESS))
536 str = ARG (char *);
537 if (str == NULL)
538 conv_error ();
541 c = inchar ();
542 if (c == EOF)
543 input_error ();
545 if (width == -1)
546 width = 1;
548 if (!(flags & SUPPRESS))
551 *str++ = c;
552 while (--width > 0 && inchar () != EOF);
554 else
555 while (--width > 0 && inchar () != EOF);
557 if (width > 0)
558 /* I.e., EOF was read. */
559 --read_in;
561 if (!(flags & SUPPRESS))
562 ++done;
564 break;
566 /* FALLTHROUGH */
567 case 'C':
568 /* Get UTF-8 encoded wide character. Here we assume (as in
569 other parts of the libc) that we only have to handle
570 UTF-8. */
572 wint_t val;
573 size_t cnt = 0;
574 int first = 1;
576 if (!(flags & SUPPRESS))
578 wstr = ARG (wchar_t *);
579 if (str == NULL)
580 conv_error ();
585 #define NEXT_WIDE_CHAR(First) \
586 c = inchar (); \
587 if (c == EOF) \
588 /* EOF is only an error for the first character. */ \
589 if (First) \
590 input_error (); \
591 else \
593 --read_in; \
594 break; \
596 val = c; \
597 if (val >= 0x80) \
599 if ((c & 0xc0) == 0x80 || (c & 0xfe) == 0xfe) \
600 encode_error (); \
601 if ((c & 0xe0) == 0xc0) \
603 /* We expect two bytes. */ \
604 cnt = 1; \
605 val &= 0x1f; \
607 else if ((c & 0xf0) == 0xe0) \
609 /* We expect three bytes. */ \
610 cnt = 2; \
611 val &= 0x0f; \
613 else if ((c & 0xf8) == 0xf0) \
615 /* We expect four bytes. */ \
616 cnt = 3; \
617 val &= 0x07; \
619 else if ((c & 0xfc) == 0xf8) \
621 /* We expect five bytes. */ \
622 cnt = 4; \
623 val &= 0x03; \
625 else \
627 /* We expect six bytes. */ \
628 cnt = 5; \
629 val &= 0x01; \
632 do \
634 c = inchar (); \
635 if (c == EOF \
636 || (c & 0xc0) == 0x80 || (c & 0xfe) == 0xfe) \
637 encode_error (); \
638 val <<= 6; \
639 val |= c & 0x3f; \
641 while (--cnt > 0); \
644 if (!(flags & SUPPRESS)) \
645 *wstr++ = val; \
646 first = 0
648 NEXT_WIDE_CHAR (first);
650 while (--width > 0);
652 if (width > 0)
653 /* I.e., EOF was read. */
654 --read_in;
656 if (!(flags & SUPPRESS))
657 ++done;
659 break;
661 case 's': /* Read a string. */
662 if (flags & LONG)
663 /* We have to process a wide character string. */
664 goto wide_char_string;
666 #define STRING_ARG(Str, Type) \
667 if (!(flags & SUPPRESS)) \
669 if (flags & MALLOC) \
671 /* The string is to be stored in a malloc'd buffer. */ \
672 strptr = ARG (char **); \
673 if (strptr == NULL) \
674 conv_error (); \
675 /* Allocate an initial buffer. */ \
676 strsize = 100; \
677 *strptr = malloc (strsize * sizeof (Type)); \
678 Str = (Type *) *strptr; \
680 else \
681 Str = ARG (Type *); \
682 if (Str == NULL) \
683 conv_error (); \
685 STRING_ARG (str, char);
687 c = inchar ();
688 if (c == EOF)
689 input_error ();
693 if (isspace (c))
695 ungetc (c, s);
696 break;
698 #define STRING_ADD_CHAR(Str, c, Type) \
699 if (!(flags & SUPPRESS)) \
701 *Str++ = c; \
702 if ((flags & MALLOC) && (char *) Str == *strptr + strsize) \
704 /* Enlarge the buffer. */ \
705 Str = realloc (*strptr, strsize * 2 * sizeof (Type)); \
706 if (Str == NULL) \
708 /* Can't allocate that much. Last-ditch effort. */\
709 Str = realloc (*strptr, \
710 (strsize + 1) * sizeof (Type)); \
711 if (Str == NULL) \
713 /* We lose. Oh well. \
714 Terminate the string and stop converting, \
715 so at least we don't skip any input. */ \
716 ((Type *) (*strptr))[strsize] = '\0'; \
717 ++done; \
718 conv_error (); \
720 else \
722 *strptr = (char *) Str; \
723 Str = ((Type *) *strptr) + strsize; \
724 ++strsize; \
727 else \
729 *strptr = (char *) Str; \
730 Str = ((Type *) *strptr) + strsize; \
731 strsize *= 2; \
735 STRING_ADD_CHAR (str, c, char);
736 } while ((width <= 0 || --width > 0) && inchar () != EOF);
738 if (!(flags & SUPPRESS))
740 *str = '\0';
741 ++done;
743 break;
745 case 'S':
746 /* Wide character string. */
747 wide_char_string:
749 wint_t val;
750 int first = 1;
751 STRING_ARG (wstr, wchar_t);
755 size_t cnt = 0;
756 NEXT_WIDE_CHAR (first);
758 if (iswspace (val))
760 /* XXX We would have to push back the whole wide char
761 with possibly many bytes. But since scanf does
762 not make a difference for white space characters
763 we can simply push back a simple <SP> which is
764 guaranteed to be in the [:space:] class. */
765 ungetc (' ', s);
766 break;
769 STRING_ADD_CHAR (wstr, val, wchar_t);
770 first = 0;
772 while (width <= 0 || --width > 0);
774 if (!(flags & SUPPRESS))
776 *wstr = L'\0';
777 ++done;
780 break;
782 case 'x': /* Hexadecimal integer. */
783 case 'X': /* Ditto. */
784 base = 16;
785 number_signed = 0;
786 goto number;
788 case 'o': /* Octal integer. */
789 base = 8;
790 number_signed = 0;
791 goto number;
793 case 'u': /* Unsigned decimal integer. */
794 base = 10;
795 number_signed = 0;
796 goto number;
798 case 'd': /* Signed decimal integer. */
799 base = 10;
800 number_signed = 1;
801 goto number;
803 case 'i': /* Generic number. */
804 base = 0;
805 number_signed = 1;
807 number:
808 c = inchar ();
809 if (c == EOF)
810 input_error ();
812 /* Check for a sign. */
813 if (c == '-' || c == '+')
815 ADDW (c);
816 if (width > 0)
817 --width;
818 c = inchar ();
821 /* Look for a leading indication of base. */
822 if (width != 0 && c == '0')
824 if (width > 0)
825 --width;
827 ADDW (c);
828 c = inchar ();
830 if (width != 0 && tolower (c) == 'x')
832 if (base == 0)
833 base = 16;
834 if (base == 16)
836 if (width > 0)
837 --width;
838 c = inchar ();
841 else if (base == 0)
842 base = 8;
845 if (base == 0)
846 base = 10;
848 /* Read the number into workspace. */
849 while (c != EOF && width != 0)
851 if (base == 16 ? !isxdigit (c) :
852 ((!isdigit (c) || c - '0' >= base) &&
853 !((flags & GROUP) && base == 10 && c == thousands)))
854 break;
855 ADDW (c);
856 if (width > 0)
857 --width;
859 c = inchar ();
862 /* The just read character is not part of the number anymore. */
863 ungetc (c, s);
865 if (wpsize == 0 ||
866 (wpsize == 1 && (wp[0] == '+' || wp[0] == '-')))
867 /* There was no number. */
868 conv_error ();
870 /* Convert the number. */
871 ADDW ('\0');
872 if (flags & LONGDBL)
874 if (number_signed)
875 num.q = __strtoll_internal (wp, &tw, base, flags & GROUP);
876 else
877 num.uq = __strtoull_internal (wp, &tw, base, flags & GROUP);
879 else
881 if (number_signed)
882 num.l = __strtol_internal (wp, &tw, base, flags & GROUP);
883 else
884 num.ul = __strtoul_internal (wp, &tw, base, flags & GROUP);
886 if (wp == tw)
887 conv_error ();
889 if (!(flags & SUPPRESS))
891 if (! number_signed)
893 if (flags & LONGDBL)
894 *ARG (unsigned LONGLONG int *) = num.uq;
895 else if (flags & LONG)
896 *ARG (unsigned long int *) = num.ul;
897 else if (flags & SHORT)
898 *ARG (unsigned short int *)
899 = (unsigned short int) num.ul;
900 else if (flags & CHAR)
901 *ARG (unsigned char *) = (unsigned char) num.ul;
902 else
903 *ARG (unsigned int *) = (unsigned int) num.ul;
905 else
907 if (flags & LONGDBL)
908 *ARG (LONGLONG int *) = num.q;
909 else if (flags & LONG)
910 *ARG (long int *) = num.l;
911 else if (flags & SHORT)
912 *ARG (short int *) = (short int) num.l;
913 else if (flags & CHAR)
914 *ARG (signed char *) = (signed char) num.ul;
915 else
916 *ARG (int *) = (int) num.l;
918 ++done;
920 break;
922 case 'e': /* Floating-point numbers. */
923 case 'E':
924 case 'f':
925 case 'g':
926 case 'G':
927 case 'a':
928 case 'A':
929 c = inchar ();
930 if (c == EOF)
931 input_error ();
933 /* Check for a sign. */
934 if (c == '-' || c == '+')
936 negative = c == '-';
937 if (inchar () == EOF)
938 /* EOF is only an input error before we read any chars. */
939 conv_error ();
940 if (width > 0)
941 --width;
943 else
944 negative = 0;
946 is_hexa = 0;
947 exp_char = 'e';
948 if (c == '0')
950 ADDW (c);
951 c = inchar ();
952 if (tolower (c) == 'x')
954 /* It is a number in hexadecimal format. */
955 ADDW (c);
957 is_hexa = 1;
958 exp_char = 'p';
960 /* Grouping is not allowed. */
961 flags &= ~GROUP;
962 c = inchar ();
966 got_dot = got_e = 0;
969 if (isdigit (c))
970 ADDW (c);
971 else if (!got_e && is_hexa && isxdigit (c))
972 ADDW (c);
973 else if (got_e && wp[wpsize - 1] == exp_char
974 && (c == '-' || c == '+'))
975 ADDW (c);
976 else if (wpsize > 0 && !got_e && tolower (c) == exp_char)
978 ADDW (exp_char);
979 got_e = got_dot = 1;
981 else if (c == decimal && !got_dot)
983 ADDW (c);
984 got_dot = 1;
986 else if ((flags & GROUP) && c == thousands && !got_dot)
987 ADDW (c);
988 else
990 /* The last read character is not part of the number
991 anymore. */
992 ungetc (c, s);
993 break;
995 if (width > 0)
996 --width;
998 while (width != 0 && inchar () != EOF);
1000 /* Have we read any character? If we try to read a number
1001 in hexadecimal notation and we have read only the `0x'
1002 prefix this is an error. */
1003 if (wpsize == 0 || (is_hexa && wpsize == 2))
1004 conv_error ();
1006 /* Convert the number. */
1007 ADDW ('\0');
1008 if (flags & LONGDBL)
1010 long double d = __strtold_internal (wp, &tw, flags & GROUP);
1011 if (!(flags & SUPPRESS) && tw != wp)
1012 *ARG (long double *) = negative ? -d : d;
1014 else if (flags & LONG)
1016 double d = __strtod_internal (wp, &tw, flags & GROUP);
1017 if (!(flags & SUPPRESS) && tw != wp)
1018 *ARG (double *) = negative ? -d : d;
1020 else
1022 float d = __strtof_internal (wp, &tw, flags & GROUP);
1023 if (!(flags & SUPPRESS) && tw != wp)
1024 *ARG (float *) = negative ? -d : d;
1027 if (tw == wp)
1028 conv_error ();
1030 if (!(flags & SUPPRESS))
1031 ++done;
1032 break;
1034 case '[': /* Character class. */
1035 if (flags & LONG)
1037 STRING_ARG (wstr, wchar_t);
1038 c = '\0'; /* This is to keep gcc quiet. */
1040 else
1042 STRING_ARG (str, char);
1044 c = inchar ();
1045 if (c == EOF)
1046 input_error ();
1049 if (*f == '^')
1051 ++f;
1052 not_in = 1;
1054 else
1055 not_in = 0;
1057 /* Fill WP with byte flags indexed by character.
1058 We will use this flag map for matching input characters. */
1059 if (wpmax < UCHAR_MAX)
1061 wpmax = UCHAR_MAX;
1062 wp = (char *) alloca (wpmax);
1064 memset (wp, 0, UCHAR_MAX);
1066 fc = *f;
1067 if (fc == ']' || fc == '-')
1069 /* If ] or - appears before any char in the set, it is not
1070 the terminator or separator, but the first char in the
1071 set. */
1072 wp[fc] = 1;
1073 ++f;
1076 while ((fc = *f++) != '\0' && fc != ']')
1078 if (fc == '-' && *f != '\0' && *f != ']' &&
1079 (unsigned char) f[-2] <= (unsigned char) *f)
1081 /* Add all characters from the one before the '-'
1082 up to (but not including) the next format char. */
1083 for (fc = f[-2]; fc < *f; ++fc)
1084 wp[fc] = 1;
1086 else
1087 /* Add the character to the flag map. */
1088 wp[fc] = 1;
1090 if (fc == '\0')
1092 if (!(flags & LONG))
1093 ungetc (c, s);
1094 conv_error();
1097 if (flags & LONG)
1099 wint_t val;
1100 int first = 1;
1104 size_t cnt = 0;
1105 NEXT_WIDE_CHAR (first);
1106 if (val > 255 || wp[val] == not_in)
1108 /* XXX We have a problem here. We read a wide
1109 character and this possibly took several
1110 bytes. But we can only push back one single
1111 character. To be sure we don't create wrong
1112 input we push it back only in case it is
1113 representable within one byte. */
1114 if (val < 0x80)
1115 ungetc (val, s);
1116 break;
1118 STRING_ADD_CHAR (wstr, val, wchar_t);
1119 if (width > 0)
1120 --width;
1121 first = 0;
1123 while (width != 0);
1125 if (first)
1126 conv_error ();
1128 if (!(flags & SUPPRESS))
1130 *wstr = L'\0';
1131 ++done;
1134 else
1136 num.ul = read_in - 1; /* -1 because we already read one char. */
1139 if (wp[c] == not_in)
1141 ungetc (c, s);
1142 break;
1144 STRING_ADD_CHAR (str, c, char);
1145 if (width > 0)
1146 --width;
1148 while (width != 0 && inchar () != EOF);
1150 if (read_in == num.ul)
1151 conv_error ();
1153 if (!(flags & SUPPRESS))
1155 *str = '\0';
1156 ++done;
1159 break;
1161 case 'p': /* Generic pointer. */
1162 base = 16;
1163 /* A PTR must be the same size as a `long int'. */
1164 flags &= ~(SHORT|LONGDBL);
1165 flags |= LONG;
1166 number_signed = 0;
1167 goto number;
1171 /* The last thing we saw int the format string was a white space.
1172 Consume the last white spaces. */
1173 if (skip_space)
1176 c = inchar ();
1177 while (isspace (c));
1178 ungetc (c, s);
1181 /* Unlock stream. */
1182 UNLOCK_STREAM;
1184 return done;
1187 #ifdef USE_IN_LIBIO
1189 __vfscanf (FILE *s, const char *format, va_list argptr)
1191 return _IO_vfscanf (s, format, argptr, NULL);
1193 #endif
1195 weak_alias (__vfscanf, vfscanf)