Update.
[glibc.git] / stdio-common / vfscanf.c
blobc1ff2690b772fb6878ef846566172324c5759c93
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(S) \
104 _IO_funlockfile (S); \
105 __libc_cleanup_region_end (0)
106 #else
107 # define ungetc(c, s) ((void) (c != EOF && --read_in), ungetc (c, s))
108 # define inchar() (c == EOF ? EOF \
109 : ((c = getc (s)), (void) (c != EOF && ++read_in), c))
110 # define encode_error() do { \
111 funlockfile (s); \
112 __set_errno (EILSEQ); \
113 return done; \
114 } while (0)
115 # define conv_error() do { \
116 funlockfile (s); \
117 return done; \
118 } while (0)
119 # define input_error() do { \
120 funlockfile (s); \
121 return done ?: EOF; \
122 } while (0)
123 # define memory_error() do { \
124 funlockfile (s); \
125 __set_errno (ENOMEM); \
126 return EOF; \
127 } while (0)
128 # define ARGCHECK(s, format) \
129 do \
131 /* Check file argument for consistence. */ \
132 if (!__validfp (s) || !s->__mode.__read) \
134 __set_errno (EBADF); \
135 return EOF; \
137 else if (format == NULL) \
139 __set_errno (EINVAL); \
140 return EOF; \
142 } while (0)
143 #if 1
144 /* XXX For now !!! */
145 # define flockfile(S) /* nothing */
146 # define funlockfile(S) /* nothing */
147 # define LOCK_STREAM(S)
148 # define UNLOCK_STREAM(S)
149 #else
150 # define LOCK_STREAM(S) \
151 __libc_cleanup_region_start (&__funlockfile, (S)); \
152 __flockfile (S)
153 # define UNLOCK_STREAM(S) \
154 __funlockfile (S); \
155 __libc_cleanup_region_end (0)
156 #endif
157 #endif
160 /* Read formatted input from S according to the format string
161 FORMAT, using the argument list in ARG.
162 Return the number of assignments made, or -1 for an input error. */
163 #ifdef USE_IN_LIBIO
165 _IO_vfscanf (s, format, argptr, errp)
166 _IO_FILE *s;
167 const char *format;
168 _IO_va_list argptr;
169 int *errp;
170 #else
172 __vfscanf (FILE *s, const char *format, va_list argptr)
173 #endif
175 va_list arg;
176 register const char *f = format;
177 register unsigned char fc; /* Current character of the format. */
178 register size_t done = 0; /* Assignments done. */
179 register size_t read_in = 0; /* Chars read in. */
180 register int c = 0; /* Last char read. */
181 register int width; /* Maximum field width. */
182 register int flags; /* Modifiers for current format element. */
184 /* Status for reading F-P nums. */
185 char got_dot, got_e, negative;
186 /* If a [...] is a [^...]. */
187 char not_in;
188 #define exp_char not_in
189 /* Base for integral numbers. */
190 int base;
191 /* Signedness for integral numbers. */
192 int number_signed;
193 #define is_hexa number_signed
194 /* Decimal point character. */
195 wchar_t decimal;
196 /* The thousands character of the current locale. */
197 wchar_t thousands;
198 /* Integral holding variables. */
199 union
201 long long int q;
202 unsigned long long int uq;
203 long int l;
204 unsigned long int ul;
205 } num;
206 /* Character-buffer pointer. */
207 char *str = NULL;
208 wchar_t *wstr = NULL;
209 char **strptr = NULL;
210 size_t strsize = 0;
211 /* We must not react on white spaces immediately because they can
212 possibly be matched even if in the input stream no character is
213 available anymore. */
214 int skip_space = 0;
215 /* Workspace. */
216 char *tw; /* Temporary pointer. */
217 char *wp = NULL; /* Workspace. */
218 size_t wpmax = 0; /* Maximal size of workspace. */
219 size_t wpsize; /* Currently used bytes in workspace. */
220 #define ADDW(Ch) \
221 do \
223 if (wpsize == wpmax) \
225 char *old = wp; \
226 wpmax = UCHAR_MAX > 2 * wpmax ? UCHAR_MAX : 2 * wpmax; \
227 wp = (char *) alloca (wpmax); \
228 if (old != NULL) \
229 memcpy (wp, old, wpsize); \
231 wp[wpsize++] = (Ch); \
233 while (0)
235 #ifdef __va_copy
236 __va_copy (arg, argptr);
237 #else
238 arg = (va_list) argptr;
239 #endif
241 ARGCHECK (s, format);
243 /* Figure out the decimal point character. */
244 if (mbtowc (&decimal, _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT),
245 strlen (_NL_CURRENT (LC_NUMERIC, DECIMAL_POINT))) <= 0)
246 decimal = (wchar_t) *_NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
247 /* Figure out the thousands separator character. */
248 if (mbtowc (&thousands, _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
249 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
250 thousands = (wchar_t) *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
252 /* Lock the stream. */
253 LOCK_STREAM (s);
255 /* Run through the format string. */
256 while (*f != '\0')
258 unsigned int argpos;
259 /* Extract the next argument, which is of type TYPE.
260 For a %N$... spec, this is the Nth argument from the beginning;
261 otherwise it is the next argument after the state now in ARG. */
262 #ifdef __va_copy
263 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
264 ({ unsigned int pos = argpos; \
265 va_list arg; \
266 __va_copy (arg, argptr); \
267 while (--pos > 0) \
268 (void) va_arg (arg, void *); \
269 va_arg (arg, type); \
271 #else
272 # if 0
273 /* XXX Possible optimization. */
274 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
275 ({ va_list arg = (va_list) argptr; \
276 arg = (va_list) ((char *) arg \
277 + (argpos - 1) \
278 * __va_rounded_size (void *)); \
279 va_arg (arg, type); \
281 # else
282 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
283 ({ unsigned int pos = argpos; \
284 va_list arg = (va_list) argptr; \
285 while (--pos > 0) \
286 (void) va_arg (arg, void *); \
287 va_arg (arg, type); \
289 # endif
290 #endif
292 if (!isascii (*f))
294 /* Non-ASCII, may be a multibyte. */
295 int len = mblen (f, strlen (f));
296 if (len > 0)
300 c = inchar ();
301 if (c == EOF)
302 input_error ();
303 else if (c != *f++)
305 ungetc (c, s);
306 conv_error ();
309 while (--len > 0);
310 continue;
314 fc = *f++;
315 if (fc != '%')
317 /* Remember to skip spaces. */
318 if (isspace (fc))
320 skip_space = 1;
321 continue;
324 /* Read a character. */
325 c = inchar ();
327 /* Characters other than format specs must just match. */
328 if (c == EOF)
329 input_error ();
331 /* We saw white space char as the last character in the format
332 string. Now it's time to skip all leading white space. */
333 if (skip_space)
335 while (isspace (c))
336 if (inchar () == EOF && errno == EINTR)
337 conv_error ();
338 skip_space = 0;
341 if (c != fc)
343 ungetc (c, s);
344 conv_error ();
347 continue;
350 /* This is the start of the conversion string. */
351 flags = 0;
353 /* Initialize state of modifiers. */
354 argpos = 0;
356 /* Prepare temporary buffer. */
357 wpsize = 0;
359 /* Check for a positional parameter specification. */
360 if (isdigit (*f))
362 argpos = *f++ - '0';
363 while (isdigit (*f))
364 argpos = argpos * 10 + (*f++ - '0');
365 if (*f == '$')
366 ++f;
367 else
369 /* Oops; that was actually the field width. */
370 width = argpos;
371 flags |= WIDTH;
372 argpos = 0;
373 goto got_width;
377 /* Check for the assignment-suppressing and the number grouping flag. */
378 while (*f == '*' || *f == '\'')
379 switch (*f++)
381 case '*':
382 flags |= SUPPRESS;
383 break;
384 case '\'':
385 flags |= GROUP;
386 break;
389 /* We have seen width. */
390 if (isdigit (*f))
391 flags |= WIDTH;
393 /* Find the maximum field width. */
394 width = 0;
395 while (isdigit (*f))
397 width *= 10;
398 width += *f++ - '0';
400 got_width:
401 if (width == 0)
402 width = -1;
404 /* Check for type modifiers. */
405 while (*f == 'h' || *f == 'l' || *f == 'L' || *f == 'a' || *f == 'q')
406 switch (*f++)
408 case 'h':
409 /* int's are short int's. */
410 if (flags & (LONG|LONGDBL|CHAR))
411 /* Signal illegal format element. */
412 conv_error ();
413 if (flags & SHORT)
415 flags &= ~SHORT;
416 flags |= CHAR;
418 else
419 flags |= SHORT;
420 break;
421 case 'l':
422 if (flags & (SHORT|LONGDBL|CHAR))
423 conv_error ();
424 else if (flags & LONG)
426 /* A double `l' is equivalent to an `L'. */
427 flags &= ~LONG;
428 flags |= LONGDBL;
430 else
431 /* int's are long int's. */
432 flags |= LONG;
433 break;
434 case 'q':
435 case 'L':
436 /* double's are long double's, and int's are long long int's. */
437 if (flags & TYPEMOD)
438 /* Signal illegal format element. */
439 conv_error ();
440 flags |= LONGDBL;
441 break;
442 case 'a':
443 /* The `a' is used as a flag only if followed by `s', `S' or
444 `['. */
445 if (*f != 's' && *f != 'S' && *f != '[')
447 --f;
448 break;
450 if (flags & TYPEMOD)
451 /* Signal illegal format element. */
452 conv_error ();
453 /* String conversions (%s, %[) take a `char **'
454 arg and fill it in with a malloc'd pointer. */
455 flags |= MALLOC;
456 break;
459 /* End of the format string? */
460 if (*f == '\0')
461 conv_error ();
463 /* Find the conversion specifier. */
464 fc = *f++;
465 if (skip_space || (fc != '[' && fc != 'c' && fc != 'C' && fc != 'n'))
467 /* Eat whitespace. */
468 int save_errno = errno;
469 errno = 0;
471 if (inchar () == EOF && errno == EINTR)
472 input_error ();
473 while (isspace (c));
474 errno = save_errno;
475 ungetc (c, s);
476 skip_space = 0;
479 switch (fc)
481 case '%': /* Must match a literal '%'. */
482 c = inchar ();
483 if (c == EOF)
484 input_error ();
485 if (c != fc)
487 ungetc (c, s);
488 conv_error ();
490 break;
492 case 'n': /* Answer number of assignments done. */
493 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
494 with the 'n' conversion specifier. */
495 if (!(flags & SUPPRESS))
497 /* Don't count the read-ahead. */
498 if (flags & LONGDBL)
499 *ARG (long long int *) = read_in;
500 else if (flags & LONG)
501 *ARG (long int *) = read_in;
502 else if (flags & SHORT)
503 *ARG (short int *) = read_in;
504 else
505 *ARG (int *) = read_in;
507 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
508 /* We have a severe problem here. The ISO C standard
509 contradicts itself in explaining the effect of the %n
510 format in `scanf'. While in ISO C:1990 and the ISO C
511 Amendement 1:1995 the result is described as
513 Execution of a %n directive does not effect the
514 assignment count returned at the completion of
515 execution of the f(w)scanf function.
517 in ISO C Corrigendum 1:1994 the following was added:
519 Subclause 7.9.6.2
520 Add the following fourth example:
522 #include <stdio.h>
523 int d1, d2, n1, n2, i;
524 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
525 the value 123 is assigned to d1 and the value3 to n1.
526 Because %n can never get an input failure the value
527 of 3 is also assigned to n2. The value of d2 is not
528 affected. The value 3 is assigned to i.
530 We go for now with the historically correct code from ISO C,
531 i.e., we don't count the %n assignments. When it ever
532 should proof to be wrong just remove the #ifdef above. */
533 ++done;
534 #endif
536 break;
538 case 'c': /* Match characters. */
539 if ((flags & LONG) == 0)
541 if (!(flags & SUPPRESS))
543 str = ARG (char *);
544 if (str == NULL)
545 conv_error ();
548 c = inchar ();
549 if (c == EOF)
550 input_error ();
552 if (width == -1)
553 width = 1;
555 if (!(flags & SUPPRESS))
558 *str++ = c;
559 while (--width > 0 && inchar () != EOF);
561 else
562 while (--width > 0 && inchar () != EOF);
564 if (!(flags & SUPPRESS))
565 ++done;
567 break;
569 /* FALLTHROUGH */
570 case 'C':
571 /* Get UTF-8 encoded wide character. Here we assume (as in
572 other parts of the libc) that we only have to handle
573 UTF-8. */
575 wint_t val;
576 size_t cnt = 0;
577 int first = 1;
579 if (!(flags & SUPPRESS))
581 wstr = ARG (wchar_t *);
582 if (str == NULL)
583 conv_error ();
588 #define NEXT_WIDE_CHAR(First) \
589 c = inchar (); \
590 if (c == EOF) \
591 /* EOF is only an error for the first character. */ \
592 if (First) \
593 input_error (); \
594 else \
595 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 (!(flags & SUPPRESS))
653 ++done;
655 break;
657 case 's': /* Read a string. */
658 if (flags & LONG)
659 /* We have to process a wide character string. */
660 goto wide_char_string;
662 #define STRING_ARG(Str, Type) \
663 if (!(flags & SUPPRESS)) \
665 if (flags & MALLOC) \
667 /* The string is to be stored in a malloc'd buffer. */ \
668 strptr = ARG (char **); \
669 if (strptr == NULL) \
670 conv_error (); \
671 /* Allocate an initial buffer. */ \
672 strsize = 100; \
673 *strptr = malloc (strsize * sizeof (Type)); \
674 Str = (Type *) *strptr; \
676 else \
677 Str = ARG (Type *); \
678 if (Str == NULL) \
679 conv_error (); \
681 STRING_ARG (str, char);
683 c = inchar ();
684 if (c == EOF)
685 input_error ();
689 if (isspace (c))
691 ungetc (c, s);
692 break;
694 #define STRING_ADD_CHAR(Str, c, Type) \
695 if (!(flags & SUPPRESS)) \
697 *Str++ = c; \
698 if ((flags & MALLOC) && (char *) Str == *strptr + strsize) \
700 /* Enlarge the buffer. */ \
701 Str = realloc (*strptr, strsize * 2 * sizeof (Type)); \
702 if (Str == NULL) \
704 /* Can't allocate that much. Last-ditch effort. */\
705 Str = realloc (*strptr, \
706 (strsize + 1) * sizeof (Type)); \
707 if (Str == NULL) \
709 /* We lose. Oh well. \
710 Terminate the string and stop converting, \
711 so at least we don't skip any input. */ \
712 ((Type *) (*strptr))[strsize] = '\0'; \
713 ++done; \
714 conv_error (); \
716 else \
718 *strptr = (char *) Str; \
719 Str = ((Type *) *strptr) + strsize; \
720 ++strsize; \
723 else \
725 *strptr = (char *) Str; \
726 Str = ((Type *) *strptr) + strsize; \
727 strsize *= 2; \
731 STRING_ADD_CHAR (str, c, char);
732 } while ((width <= 0 || --width > 0) && inchar () != EOF);
734 if (!(flags & SUPPRESS))
736 *str = '\0';
737 ++done;
739 break;
741 case 'S':
742 /* Wide character string. */
743 wide_char_string:
745 wint_t val;
746 int first = 1;
747 STRING_ARG (wstr, wchar_t);
751 size_t cnt = 0;
752 NEXT_WIDE_CHAR (first);
754 if (iswspace (val))
756 /* XXX We would have to push back the whole wide char
757 with possibly many bytes. But since scanf does
758 not make a difference for white space characters
759 we can simply push back a simple <SP> which is
760 guaranteed to be in the [:space:] class. */
761 ungetc (' ', s);
762 break;
765 STRING_ADD_CHAR (wstr, val, wchar_t);
766 first = 0;
768 while (width <= 0 || --width > 0);
770 if (!(flags & SUPPRESS))
772 *wstr = L'\0';
773 ++done;
776 break;
778 case 'x': /* Hexadecimal integer. */
779 case 'X': /* Ditto. */
780 base = 16;
781 number_signed = 0;
782 goto number;
784 case 'o': /* Octal integer. */
785 base = 8;
786 number_signed = 0;
787 goto number;
789 case 'u': /* Unsigned decimal integer. */
790 base = 10;
791 number_signed = 0;
792 goto number;
794 case 'd': /* Signed decimal integer. */
795 base = 10;
796 number_signed = 1;
797 goto number;
799 case 'i': /* Generic number. */
800 base = 0;
801 number_signed = 1;
803 number:
804 c = inchar ();
805 if (c == EOF)
806 input_error ();
808 /* Check for a sign. */
809 if (c == '-' || c == '+')
811 ADDW (c);
812 if (width > 0)
813 --width;
814 c = inchar ();
817 /* Look for a leading indication of base. */
818 if (width != 0 && c == '0')
820 if (width > 0)
821 --width;
823 ADDW (c);
824 c = inchar ();
826 if (width != 0 && tolower (c) == 'x')
828 if (base == 0)
829 base = 16;
830 if (base == 16)
832 if (width > 0)
833 --width;
834 c = inchar ();
837 else if (base == 0)
838 base = 8;
841 if (base == 0)
842 base = 10;
844 /* Read the number into workspace. */
845 while (c != EOF && width != 0)
847 if (base == 16 ? !isxdigit (c) :
848 ((!isdigit (c) || c - '0' >= base) &&
849 !((flags & GROUP) && base == 10 && c == thousands)))
850 break;
851 ADDW (c);
852 if (width > 0)
853 --width;
855 c = inchar ();
858 /* The just read character is not part of the number anymore. */
859 ungetc (c, s);
861 if (wpsize == 0 ||
862 (wpsize == 1 && (wp[0] == '+' || wp[0] == '-')))
863 /* There was no number. */
864 conv_error ();
866 /* Convert the number. */
867 ADDW ('\0');
868 if (flags & LONGDBL)
870 if (number_signed)
871 num.q = __strtoll_internal (wp, &tw, base, flags & GROUP);
872 else
873 num.uq = __strtoull_internal (wp, &tw, base, flags & GROUP);
875 else
877 if (number_signed)
878 num.l = __strtol_internal (wp, &tw, base, flags & GROUP);
879 else
880 num.ul = __strtoul_internal (wp, &tw, base, flags & GROUP);
882 if (wp == tw)
883 conv_error ();
885 if (!(flags & SUPPRESS))
887 if (! number_signed)
889 if (flags & LONGDBL)
890 *ARG (unsigned LONGLONG int *) = num.uq;
891 else if (flags & LONG)
892 *ARG (unsigned long int *) = num.ul;
893 else if (flags & SHORT)
894 *ARG (unsigned short int *)
895 = (unsigned short int) num.ul;
896 else if (flags & CHAR)
897 *ARG (unsigned char *) = (unsigned char) num.ul;
898 else
899 *ARG (unsigned int *) = (unsigned int) num.ul;
901 else
903 if (flags & LONGDBL)
904 *ARG (LONGLONG int *) = num.q;
905 else if (flags & LONG)
906 *ARG (long int *) = num.l;
907 else if (flags & SHORT)
908 *ARG (short int *) = (short int) num.l;
909 else if (flags & CHAR)
910 *ARG (signed char *) = (signed char) num.ul;
911 else
912 *ARG (int *) = (int) num.l;
914 ++done;
916 break;
918 case 'e': /* Floating-point numbers. */
919 case 'E':
920 case 'f':
921 case 'g':
922 case 'G':
923 case 'a':
924 case 'A':
925 c = inchar ();
926 if (c == EOF)
927 input_error ();
929 /* Check for a sign. */
930 if (c == '-' || c == '+')
932 negative = c == '-';
933 if (inchar () == EOF)
934 /* EOF is only an input error before we read any chars. */
935 conv_error ();
936 if (width > 0)
937 --width;
939 else
940 negative = 0;
942 /* Take care for the special arguments "nan" and "inf". */
943 if (tolower (c) == 'n')
945 /* Maybe "nan". */
946 ADDW (c);
947 if (inchar () == EOF || tolower (c) != 'a')
948 input_error ();
949 ADDW (c);
950 if (inchar () == EOF || tolower (c) != 'n')
951 input_error ();
952 ADDW (c);
953 /* It is "nan". */
954 goto scan_float;
956 else if (tolower (c) == 'i')
958 /* Maybe "inf" or "infinity". */
959 ADDW (c);
960 if (inchar () == EOF || tolower (c) != 'n')
961 input_error ();
962 ADDW (c);
963 if (inchar () == EOF || tolower (c) != 'f')
964 input_error ();
965 ADDW (c);
966 /* It is as least "inf". */
967 if (inchar () != EOF)
969 if (tolower (c) == 'i')
971 /* No we have to read the rest as well. */
972 ADDW (c);
973 if (inchar () == EOF || tolower (c) != 'n')
974 input_error ();
975 ADDW (c);
976 if (inchar () == EOF || tolower (c) != 'i')
977 input_error ();
978 ADDW (c);
979 if (inchar () == EOF || tolower (c) != 't')
980 input_error ();
981 ADDW (c);
982 if (inchar () == EOF || tolower (c) != 'y')
983 input_error ();
984 ADDW (c);
986 else
987 /* Never mind. */
988 ungetc (c, s);
990 goto scan_float;
993 is_hexa = 0;
994 exp_char = 'e';
995 if (c == '0')
997 ADDW (c);
998 c = inchar ();
999 if (tolower (c) == 'x')
1001 /* It is a number in hexadecimal format. */
1002 ADDW (c);
1004 is_hexa = 1;
1005 exp_char = 'p';
1007 /* Grouping is not allowed. */
1008 flags &= ~GROUP;
1009 c = inchar ();
1013 got_dot = got_e = 0;
1016 if (isdigit (c))
1017 ADDW (c);
1018 else if (!got_e && is_hexa && isxdigit (c))
1019 ADDW (c);
1020 else if (got_e && wp[wpsize - 1] == exp_char
1021 && (c == '-' || c == '+'))
1022 ADDW (c);
1023 else if (wpsize > 0 && !got_e && tolower (c) == exp_char)
1025 ADDW (exp_char);
1026 got_e = got_dot = 1;
1028 else if (c == decimal && !got_dot)
1030 ADDW (c);
1031 got_dot = 1;
1033 else if ((flags & GROUP) && c == thousands && !got_dot)
1034 ADDW (c);
1035 else
1037 /* The last read character is not part of the number
1038 anymore. */
1039 ungetc (c, s);
1040 break;
1042 if (width > 0)
1043 --width;
1045 while (width != 0 && inchar () != EOF);
1047 /* Have we read any character? If we try to read a number
1048 in hexadecimal notation and we have read only the `0x'
1049 prefix this is an error. */
1050 if (wpsize == 0 || (is_hexa && wpsize == 2))
1051 conv_error ();
1053 scan_float:
1054 /* Convert the number. */
1055 ADDW ('\0');
1056 if (flags & LONGDBL)
1058 long double d = __strtold_internal (wp, &tw, flags & GROUP);
1059 if (!(flags & SUPPRESS) && tw != wp)
1060 *ARG (long double *) = negative ? -d : d;
1062 else if (flags & LONG)
1064 double d = __strtod_internal (wp, &tw, flags & GROUP);
1065 if (!(flags & SUPPRESS) && tw != wp)
1066 *ARG (double *) = negative ? -d : d;
1068 else
1070 float d = __strtof_internal (wp, &tw, flags & GROUP);
1071 if (!(flags & SUPPRESS) && tw != wp)
1072 *ARG (float *) = negative ? -d : d;
1075 if (tw == wp)
1076 conv_error ();
1078 if (!(flags & SUPPRESS))
1079 ++done;
1080 break;
1082 case '[': /* Character class. */
1083 if (flags & LONG)
1085 STRING_ARG (wstr, wchar_t);
1086 c = '\0'; /* This is to keep gcc quiet. */
1088 else
1090 STRING_ARG (str, char);
1092 c = inchar ();
1093 if (c == EOF)
1094 input_error ();
1097 if (*f == '^')
1099 ++f;
1100 not_in = 1;
1102 else
1103 not_in = 0;
1105 /* Fill WP with byte flags indexed by character.
1106 We will use this flag map for matching input characters. */
1107 if (wpmax < UCHAR_MAX)
1109 wpmax = UCHAR_MAX;
1110 wp = (char *) alloca (wpmax);
1112 memset (wp, 0, UCHAR_MAX);
1114 fc = *f;
1115 if (fc == ']' || fc == '-')
1117 /* If ] or - appears before any char in the set, it is not
1118 the terminator or separator, but the first char in the
1119 set. */
1120 wp[fc] = 1;
1121 ++f;
1124 while ((fc = *f++) != '\0' && fc != ']')
1126 if (fc == '-' && *f != '\0' && *f != ']' &&
1127 (unsigned char) f[-2] <= (unsigned char) *f)
1129 /* Add all characters from the one before the '-'
1130 up to (but not including) the next format char. */
1131 for (fc = f[-2]; fc < *f; ++fc)
1132 wp[fc] = 1;
1134 else
1135 /* Add the character to the flag map. */
1136 wp[fc] = 1;
1138 if (fc == '\0')
1140 if (!(flags & LONG))
1141 ungetc (c, s);
1142 conv_error();
1145 if (flags & LONG)
1147 wint_t val;
1148 int first = 1;
1152 size_t cnt = 0;
1153 NEXT_WIDE_CHAR (first);
1154 if (val <= 255 && wp[val] == not_in)
1156 ungetc (val, s);
1157 break;
1159 STRING_ADD_CHAR (wstr, val, wchar_t);
1160 if (width > 0)
1161 --width;
1162 first = 0;
1164 while (width != 0);
1166 if (first)
1167 conv_error ();
1169 if (!(flags & SUPPRESS))
1171 *wstr = L'\0';
1172 ++done;
1175 else
1177 num.ul = read_in - 1; /* -1 because we already read one char. */
1180 if (wp[c] == not_in)
1182 ungetc (c, s);
1183 break;
1185 STRING_ADD_CHAR (str, c, char);
1186 if (width > 0)
1187 --width;
1189 while (width != 0 && inchar () != EOF);
1191 if (read_in == num.ul)
1192 conv_error ();
1194 if (!(flags & SUPPRESS))
1196 *str = '\0';
1197 ++done;
1200 break;
1202 case 'p': /* Generic pointer. */
1203 base = 16;
1204 /* A PTR must be the same size as a `long int'. */
1205 flags &= ~(SHORT|LONGDBL);
1206 flags |= LONG;
1207 number_signed = 0;
1208 goto number;
1212 /* The last thing we saw int the format string was a white space.
1213 Consume the last white spaces. */
1214 if (skip_space)
1217 c = inchar ();
1218 while (isspace (c));
1219 ungetc (c, s);
1222 /* Unlock stream. */
1223 UNLOCK_STREAM (s);
1225 return done;
1228 #ifdef USE_IN_LIBIO
1230 __vfscanf (FILE *s, const char *format, va_list argptr)
1232 return _IO_vfscanf (s, format, argptr, NULL);
1234 #endif
1236 weak_alias (__vfscanf, vfscanf)