Update.
[glibc.git] / stdio-common / vfscanf.c
blob2e8cf9f5f5d469f4e1b2cbd984c52b18fafd7c82
1 /* Copyright (C) 1991,92,93,94,95,96,97,98,99 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 <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <wchar.h>
28 #include <wctype.h>
29 #include <bits/libc-lock.h>
30 #include <locale/localeinfo.h>
32 #ifdef __GNUC__
33 # define HAVE_LONGLONG
34 # define LONGLONG long long
35 #else
36 # define LONGLONG long
37 #endif
39 /* Those are flags in the conversion format. */
40 # define LONG 0x001 /* l: long or double */
41 # define LONGDBL 0x002 /* L: long long or long double */
42 # define SHORT 0x004 /* h: short */
43 # define SUPPRESS 0x008 /* *: suppress assignment */
44 # define POINTER 0x010 /* weird %p pointer (`fake hex') */
45 # define NOSKIP 0x020 /* do not skip blanks */
46 # define WIDTH 0x040 /* width was given */
47 # define GROUP 0x080 /* ': group numbers */
48 # define MALLOC 0x100 /* a: malloc strings */
49 # define CHAR 0x200 /* hh: 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 \
59 || (--read_in, \
60 _IO_sputbackc (s, (unsigned char) c))))
61 # define inchar() (c == EOF ? EOF \
62 : ((c = _IO_getc_unlocked (s)), \
63 (void) (c != EOF && ++read_in), c))
64 # define encode_error() do { \
65 if (errp != NULL) *errp |= 4; \
66 _IO_funlockfile (s); \
67 __libc_cleanup_end (0); \
68 __set_errno (EILSEQ); \
69 return done; \
70 } while (0)
71 # define conv_error() do { \
72 if (errp != NULL) *errp |= 2; \
73 _IO_funlockfile (s); \
74 __libc_cleanup_end (0); \
75 return done; \
76 } while (0)
77 # define input_error() do { \
78 _IO_funlockfile (s); \
79 if (errp != NULL) *errp |= 1; \
80 __libc_cleanup_end (0); \
81 return done ?: EOF; \
82 } while (0)
83 # define memory_error() do { \
84 _IO_funlockfile (s); \
85 __set_errno (ENOMEM); \
86 __libc_cleanup_end (0); \
87 return EOF; \
88 } while (0)
89 # define ARGCHECK(s, format) \
90 do \
91 { \
92 /* Check file argument for consistence. */ \
93 CHECK_FILE (s, EOF); \
94 if (s->_flags & _IO_NO_READS) \
95 { \
96 __set_errno (EBADF); \
97 return EOF; \
98 } \
99 else if (format == NULL) \
101 MAYBE_SET_EINVAL; \
102 return EOF; \
104 } while (0)
105 # define LOCK_STREAM(S) \
106 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, (S)); \
107 _IO_flockfile (S)
108 # define UNLOCK_STREAM(S) \
109 _IO_funlockfile (S); \
110 __libc_cleanup_region_end (0)
111 #else
112 # define ungetc(c, s) ((void) (c != EOF && --read_in), ungetc (c, s))
113 # define inchar() (c == EOF ? EOF \
114 : ((c = getc (s)), (void) (c != EOF && ++read_in), c))
115 # define encode_error() do { \
116 funlockfile (s); \
117 __set_errno (EILSEQ); \
118 return done; \
119 } while (0)
120 # define conv_error() do { \
121 funlockfile (s); \
122 return done; \
123 } while (0)
124 # define input_error() do { \
125 funlockfile (s); \
126 return done ?: EOF; \
127 } while (0)
128 # define memory_error() do { \
129 funlockfile (s); \
130 __set_errno (ENOMEM); \
131 return EOF; \
132 } while (0)
133 # define ARGCHECK(s, format) \
134 do \
136 /* Check file argument for consistence. */ \
137 if (!__validfp (s) || !s->__mode.__read) \
139 __set_errno (EBADF); \
140 return EOF; \
142 else if (format == NULL) \
144 __set_errno (EINVAL); \
145 return EOF; \
147 } while (0)
148 #if 1
149 /* XXX For now !!! */
150 # define flockfile(S) /* nothing */
151 # define funlockfile(S) /* nothing */
152 # define LOCK_STREAM(S)
153 # define UNLOCK_STREAM(S)
154 #else
155 # define LOCK_STREAM(S) \
156 __libc_cleanup_region_start (&__funlockfile, (S)); \
157 __flockfile (S)
158 # define UNLOCK_STREAM(S) \
159 __funlockfile (S); \
160 __libc_cleanup_region_end (0)
161 #endif
162 #endif
165 /* Read formatted input from S according to the format string
166 FORMAT, using the argument list in ARG.
167 Return the number of assignments made, or -1 for an input error. */
168 #ifdef USE_IN_LIBIO
170 _IO_vfscanf (s, format, argptr, errp)
171 _IO_FILE *s;
172 const char *format;
173 _IO_va_list argptr;
174 int *errp;
175 #else
177 __vfscanf (FILE *s, const char *format, va_list argptr)
178 #endif
180 va_list arg;
181 register const char *f = format;
182 register unsigned char fc; /* Current character of the format. */
183 register size_t done = 0; /* Assignments done. */
184 register size_t read_in = 0; /* Chars read in. */
185 register int c = 0; /* Last char read. */
186 register int width; /* Maximum field width. */
187 register int flags; /* Modifiers for current format element. */
189 /* Status for reading F-P nums. */
190 char got_dot, got_e, negative;
191 /* If a [...] is a [^...]. */
192 char not_in;
193 #define exp_char not_in
194 /* Base for integral numbers. */
195 int base;
196 /* Signedness for integral numbers. */
197 int number_signed;
198 #define is_hexa number_signed
199 /* Decimal point character. */
200 wchar_t decimal;
201 /* The thousands character of the current locale. */
202 wchar_t thousands;
203 /* State for the conversions. */
204 mbstate_t state;
205 /* Integral holding variables. */
206 union
208 long long int q;
209 unsigned long long int uq;
210 long int l;
211 unsigned long int ul;
212 } num;
213 /* Character-buffer pointer. */
214 char *str = NULL;
215 wchar_t *wstr = NULL;
216 char **strptr = NULL;
217 size_t strsize = 0;
218 /* We must not react on white spaces immediately because they can
219 possibly be matched even if in the input stream no character is
220 available anymore. */
221 int skip_space = 0;
222 /* Nonzero if we are reading a pointer. */
223 int read_pointer;
224 /* Workspace. */
225 char *tw; /* Temporary pointer. */
226 char *wp = NULL; /* Workspace. */
227 size_t wpmax = 0; /* Maximal size of workspace. */
228 size_t wpsize; /* Currently used bytes in workspace. */
229 #define ADDW(Ch) \
230 do \
232 if (wpsize == wpmax) \
234 char *old = wp; \
235 wpmax = UCHAR_MAX > 2 * wpmax ? UCHAR_MAX : 2 * wpmax; \
236 wp = (char *) alloca (wpmax); \
237 if (old != NULL) \
238 memcpy (wp, old, wpsize); \
240 wp[wpsize++] = (Ch); \
242 while (0)
244 #ifdef __va_copy
245 __va_copy (arg, argptr);
246 #else
247 arg = (va_list) argptr;
248 #endif
250 ARGCHECK (s, format);
252 /* Figure out the decimal point character. */
253 memset (&state, '\0', sizeof (state));
254 if (__mbrtowc (&decimal, _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT),
255 strlen (_NL_CURRENT (LC_NUMERIC, DECIMAL_POINT)), &state)
256 <= 0)
257 decimal = (wchar_t) *_NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
258 /* Figure out the thousands separator character. */
259 memset (&state, '\0', sizeof (state));
260 if (__mbrtowc (&thousands, _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
261 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP)),
262 &state) <= 0)
263 thousands = (wchar_t) *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
265 /* Lock the stream. */
266 LOCK_STREAM (s);
269 /* From now on we use `state' to convert the format string. */
270 memset (&state, '\0', sizeof (state));
272 /* Run through the format string. */
273 while (*f != '\0')
275 unsigned int argpos;
276 /* Extract the next argument, which is of type TYPE.
277 For a %N$... spec, this is the Nth argument from the beginning;
278 otherwise it is the next argument after the state now in ARG. */
279 #ifdef __va_copy
280 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
281 ({ unsigned int pos = argpos; \
282 va_list arg; \
283 __va_copy (arg, argptr); \
284 while (--pos > 0) \
285 (void) va_arg (arg, void *); \
286 va_arg (arg, type); \
288 #else
289 # if 0
290 /* XXX Possible optimization. */
291 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
292 ({ va_list arg = (va_list) argptr; \
293 arg = (va_list) ((char *) arg \
294 + (argpos - 1) \
295 * __va_rounded_size (void *)); \
296 va_arg (arg, type); \
298 # else
299 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
300 ({ unsigned int pos = argpos; \
301 va_list arg = (va_list) argptr; \
302 while (--pos > 0) \
303 (void) va_arg (arg, void *); \
304 va_arg (arg, type); \
306 # endif
307 #endif
309 if (!isascii (*f))
311 /* Non-ASCII, may be a multibyte. */
312 int len = __mbrlen (f, strlen (f), &state);
313 if (len > 0)
317 c = inchar ();
318 if (c == EOF)
319 input_error ();
320 else if (c != *f++)
322 ungetc (c, s);
323 conv_error ();
326 while (--len > 0);
327 continue;
331 fc = *f++;
332 if (fc != '%')
334 /* Remember to skip spaces. */
335 if (isspace (fc))
337 skip_space = 1;
338 continue;
341 /* Read a character. */
342 c = inchar ();
344 /* Characters other than format specs must just match. */
345 if (c == EOF)
346 input_error ();
348 /* We saw white space char as the last character in the format
349 string. Now it's time to skip all leading white space. */
350 if (skip_space)
352 while (isspace (c))
353 if (inchar () == EOF && errno == EINTR)
354 conv_error ();
355 skip_space = 0;
358 if (c != fc)
360 ungetc (c, s);
361 conv_error ();
364 continue;
367 /* This is the start of the conversion string. */
368 flags = 0;
370 /* Not yet decided whether we read a pointer or not. */
371 read_pointer = 0;
373 /* Initialize state of modifiers. */
374 argpos = 0;
376 /* Prepare temporary buffer. */
377 wpsize = 0;
379 /* Check for a positional parameter specification. */
380 if (isdigit (*f))
382 argpos = *f++ - '0';
383 while (isdigit (*f))
384 argpos = argpos * 10 + (*f++ - '0');
385 if (*f == '$')
386 ++f;
387 else
389 /* Oops; that was actually the field width. */
390 width = argpos;
391 flags |= WIDTH;
392 argpos = 0;
393 goto got_width;
397 /* Check for the assignment-suppressing and the number grouping flag. */
398 while (*f == '*' || *f == '\'')
399 switch (*f++)
401 case '*':
402 flags |= SUPPRESS;
403 break;
404 case '\'':
405 flags |= GROUP;
406 break;
409 /* We have seen width. */
410 if (isdigit (*f))
411 flags |= WIDTH;
413 /* Find the maximum field width. */
414 width = 0;
415 while (isdigit (*f))
417 width *= 10;
418 width += *f++ - '0';
420 got_width:
421 if (width == 0)
422 width = -1;
424 /* Check for type modifiers. */
425 switch (*f++)
427 case 'h':
428 /* ints are short ints or chars. */
429 if (*f == 'h')
431 ++f;
432 flags |= CHAR;
434 else
435 flags |= SHORT;
436 break;
437 case 'l':
438 if (*f == 'l')
440 /* A double `l' is equivalent to an `L'. */
441 ++f;
442 flags |= LONGDBL;
444 else
445 /* ints are long ints. */
446 flags |= LONG;
447 break;
448 case 'q':
449 case 'L':
450 /* doubles are long doubles, and ints are long long ints. */
451 flags |= LONGDBL;
452 break;
453 case 'a':
454 /* The `a' is used as a flag only if followed by `s', `S' or
455 `['. */
456 if (*f != 's' && *f != 'S' && *f != '[')
458 --f;
459 break;
461 /* String conversions (%s, %[) take a `char **'
462 arg and fill it in with a malloc'd pointer. */
463 flags |= MALLOC;
464 break;
465 case 'z':
466 if (sizeof (size_t) > sizeof (unsigned long int))
467 flags |= LONGDBL;
468 else if (sizeof (size_t) > sizeof (unsigned int))
469 flags |= LONG;
470 break;
471 case 'j':
472 if (sizeof (uintmax_t) > sizeof (unsigned long int))
473 flags |= LONGDBL;
474 else if (sizeof (uintmax_t) > sizeof (unsigned int))
475 flags |= LONG;
476 break;
477 case 't':
478 if (sizeof (ptrdiff_t) > sizeof (long int))
479 flags |= LONGDBL;
480 else if (sizeof (ptrdiff_t) > sizeof (int))
481 flags |= LONG;
482 break;
483 default:
484 /* Not a recognized modifier. Backup. */
485 --f;
486 break;
489 /* End of the format string? */
490 if (*f == '\0')
491 conv_error ();
493 /* Find the conversion specifier. */
494 fc = *f++;
495 if (skip_space || (fc != '[' && fc != 'c' && fc != 'C' && fc != 'n'))
497 /* Eat whitespace. */
498 int save_errno = errno;
499 errno = 0;
501 if (inchar () == EOF && errno == EINTR)
502 input_error ();
503 while (isspace (c));
504 errno = save_errno;
505 ungetc (c, s);
506 skip_space = 0;
509 switch (fc)
511 case '%': /* Must match a literal '%'. */
512 c = inchar ();
513 if (c == EOF)
514 input_error ();
515 if (c != fc)
517 ungetc (c, s);
518 conv_error ();
520 break;
522 case 'n': /* Answer number of assignments done. */
523 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
524 with the 'n' conversion specifier. */
525 if (!(flags & SUPPRESS))
527 /* Don't count the read-ahead. */
528 if (flags & LONGDBL)
529 *ARG (long long int *) = read_in;
530 else if (flags & LONG)
531 *ARG (long int *) = read_in;
532 else if (flags & SHORT)
533 *ARG (short int *) = read_in;
534 else
535 *ARG (int *) = read_in;
537 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
538 /* We have a severe problem here. The ISO C standard
539 contradicts itself in explaining the effect of the %n
540 format in `scanf'. While in ISO C:1990 and the ISO C
541 Amendement 1:1995 the result is described as
543 Execution of a %n directive does not effect the
544 assignment count returned at the completion of
545 execution of the f(w)scanf function.
547 in ISO C Corrigendum 1:1994 the following was added:
549 Subclause 7.9.6.2
550 Add the following fourth example:
552 #include <stdio.h>
553 int d1, d2, n1, n2, i;
554 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
555 the value 123 is assigned to d1 and the value3 to n1.
556 Because %n can never get an input failure the value
557 of 3 is also assigned to n2. The value of d2 is not
558 affected. The value 3 is assigned to i.
560 We go for now with the historically correct code from ISO C,
561 i.e., we don't count the %n assignments. When it ever
562 should proof to be wrong just remove the #ifdef above. */
563 ++done;
564 #endif
566 break;
568 case 'c': /* Match characters. */
569 if ((flags & LONG) == 0)
571 if (!(flags & SUPPRESS))
573 str = ARG (char *);
574 if (str == NULL)
575 conv_error ();
578 c = inchar ();
579 if (c == EOF)
580 input_error ();
582 if (width == -1)
583 width = 1;
585 if (!(flags & SUPPRESS))
588 *str++ = c;
589 while (--width > 0 && inchar () != EOF);
591 else
592 while (--width > 0 && inchar () != EOF);
594 if (!(flags & SUPPRESS))
595 ++done;
597 break;
599 /* FALLTHROUGH */
600 case 'C':
601 /* Get UTF-8 encoded wide character. Here we assume (as in
602 other parts of the libc) that we only have to handle
603 UTF-8. */
605 wint_t val;
606 size_t cnt = 0;
607 int first = 1;
609 if (!(flags & SUPPRESS))
611 wstr = ARG (wchar_t *);
612 if (str == NULL)
613 conv_error ();
618 #define NEXT_WIDE_CHAR(First) \
619 c = inchar (); \
620 if (c == EOF) \
622 /* EOF is only an error for the first character. */ \
623 if (First) \
624 input_error (); \
625 else \
626 break; \
628 val = c; \
629 if (val >= 0x80) \
631 if ((c & 0xc0) == 0x80 || (c & 0xfe) == 0xfe) \
632 encode_error (); \
633 if ((c & 0xe0) == 0xc0) \
635 /* We expect two bytes. */ \
636 cnt = 1; \
637 val &= 0x1f; \
639 else if ((c & 0xf0) == 0xe0) \
641 /* We expect three bytes. */ \
642 cnt = 2; \
643 val &= 0x0f; \
645 else if ((c & 0xf8) == 0xf0) \
647 /* We expect four bytes. */ \
648 cnt = 3; \
649 val &= 0x07; \
651 else if ((c & 0xfc) == 0xf8) \
653 /* We expect five bytes. */ \
654 cnt = 4; \
655 val &= 0x03; \
657 else \
659 /* We expect six bytes. */ \
660 cnt = 5; \
661 val &= 0x01; \
664 do \
666 c = inchar (); \
667 if (c == EOF \
668 || (c & 0xc0) == 0x80 || (c & 0xfe) == 0xfe) \
669 encode_error (); \
670 val <<= 6; \
671 val |= c & 0x3f; \
673 while (--cnt > 0); \
676 if (!(flags & SUPPRESS)) \
677 *wstr++ = val; \
678 First = 0
680 NEXT_WIDE_CHAR (first);
682 while (--width > 0);
684 if (!(flags & SUPPRESS))
685 ++done;
687 break;
689 case 's': /* Read a string. */
690 if (flags & LONG)
691 /* We have to process a wide character string. */
692 goto wide_char_string;
694 #define STRING_ARG(Str, Type) \
695 if (!(flags & SUPPRESS)) \
697 if (flags & MALLOC) \
699 /* The string is to be stored in a malloc'd buffer. */ \
700 strptr = ARG (char **); \
701 if (strptr == NULL) \
702 conv_error (); \
703 /* Allocate an initial buffer. */ \
704 strsize = 100; \
705 *strptr = malloc (strsize * sizeof (Type)); \
706 Str = (Type *) *strptr; \
708 else \
709 Str = ARG (Type *); \
710 if (Str == NULL) \
711 conv_error (); \
713 STRING_ARG (str, char);
715 c = inchar ();
716 if (c == EOF)
717 input_error ();
721 if (isspace (c))
723 ungetc (c, s);
724 break;
726 #define STRING_ADD_CHAR(Str, c, Type) \
727 if (!(flags & SUPPRESS)) \
729 *Str++ = c; \
730 if ((flags & MALLOC) && (char *) Str == *strptr + strsize) \
732 /* Enlarge the buffer. */ \
733 Str = realloc (*strptr, strsize * 2 * sizeof (Type)); \
734 if (Str == NULL) \
736 /* Can't allocate that much. Last-ditch effort. */\
737 Str = realloc (*strptr, \
738 (strsize + 1) * sizeof (Type)); \
739 if (Str == NULL) \
741 /* We lose. Oh well. \
742 Terminate the string and stop converting, \
743 so at least we don't skip any input. */ \
744 ((Type *) (*strptr))[strsize] = '\0'; \
745 ++done; \
746 conv_error (); \
748 else \
750 *strptr = (char *) Str; \
751 Str = ((Type *) *strptr) + strsize; \
752 ++strsize; \
755 else \
757 *strptr = (char *) Str; \
758 Str = ((Type *) *strptr) + strsize; \
759 strsize *= 2; \
763 STRING_ADD_CHAR (str, c, char);
764 } while ((width <= 0 || --width > 0) && inchar () != EOF);
766 if (!(flags & SUPPRESS))
768 *str = '\0';
769 ++done;
771 break;
773 case 'S':
774 /* Wide character string. */
775 wide_char_string:
777 wint_t val;
778 int first = 1;
779 STRING_ARG (wstr, wchar_t);
783 size_t cnt = 0;
784 NEXT_WIDE_CHAR (first);
786 if (__iswspace (val))
788 /* XXX We would have to push back the whole wide char
789 with possibly many bytes. But since scanf does
790 not make a difference for white space characters
791 we can simply push back a simple <SP> which is
792 guaranteed to be in the [:space:] class. */
793 ungetc (' ', s);
794 break;
797 STRING_ADD_CHAR (wstr, val, wchar_t);
798 first = 0;
800 while (width <= 0 || --width > 0);
802 if (!(flags & SUPPRESS))
804 *wstr = L'\0';
805 ++done;
808 break;
810 case 'x': /* Hexadecimal integer. */
811 case 'X': /* Ditto. */
812 base = 16;
813 number_signed = 0;
814 goto number;
816 case 'o': /* Octal integer. */
817 base = 8;
818 number_signed = 0;
819 goto number;
821 case 'u': /* Unsigned decimal integer. */
822 base = 10;
823 number_signed = 0;
824 goto number;
826 case 'd': /* Signed decimal integer. */
827 base = 10;
828 number_signed = 1;
829 goto number;
831 case 'i': /* Generic number. */
832 base = 0;
833 number_signed = 1;
835 number:
836 c = inchar ();
837 if (c == EOF)
838 input_error ();
840 /* Check for a sign. */
841 if (c == '-' || c == '+')
843 ADDW (c);
844 if (width > 0)
845 --width;
846 c = inchar ();
849 /* Look for a leading indication of base. */
850 if (width != 0 && c == '0')
852 if (width > 0)
853 --width;
855 ADDW (c);
856 c = inchar ();
858 if (width != 0 && _tolower (c) == 'x')
860 if (base == 0)
861 base = 16;
862 if (base == 16)
864 if (width > 0)
865 --width;
866 c = inchar ();
869 else if (base == 0)
870 base = 8;
873 if (base == 0)
874 base = 10;
876 /* Read the number into workspace. */
877 while (c != EOF && width != 0)
879 if (base == 16 ? !isxdigit (c) :
880 ((!isdigit (c) || c - '0' >= base) &&
881 !((flags & GROUP) && base == 10 && c == thousands)))
882 break;
883 ADDW (c);
884 if (width > 0)
885 --width;
887 c = inchar ();
890 if (wpsize == 0 ||
891 (wpsize == 1 && (wp[0] == '+' || wp[0] == '-')))
893 /* There was no number. If we are supposed to read a pointer
894 we must recognize "(nil)" as well. */
895 if (wpsize == 0 && read_pointer && (width < 0 || width >= 0)
896 && c == '('
897 && _tolower (inchar ()) == 'n'
898 && _tolower (inchar ()) == 'i'
899 && _tolower (inchar ()) == 'l'
900 && inchar () == ')')
901 /* We must produce the value of a NULL pointer. A single
902 '0' digit is enough. */
903 ADDW ('0');
904 else
906 /* The last read character is not part of the number
907 anymore. */
908 ungetc (c, s);
910 conv_error ();
913 else
914 /* The just read character is not part of the number anymore. */
915 ungetc (c, s);
917 /* Convert the number. */
918 ADDW ('\0');
919 if (flags & LONGDBL)
921 if (number_signed)
922 num.q = __strtoll_internal (wp, &tw, base, flags & GROUP);
923 else
924 num.uq = __strtoull_internal (wp, &tw, base, flags & GROUP);
926 else
928 if (number_signed)
929 num.l = __strtol_internal (wp, &tw, base, flags & GROUP);
930 else
931 num.ul = __strtoul_internal (wp, &tw, base, flags & GROUP);
933 if (wp == tw)
934 conv_error ();
936 if (!(flags & SUPPRESS))
938 if (! number_signed)
940 if (flags & LONGDBL)
941 *ARG (unsigned LONGLONG int *) = num.uq;
942 else if (flags & LONG)
943 *ARG (unsigned long int *) = num.ul;
944 else if (flags & SHORT)
945 *ARG (unsigned short int *)
946 = (unsigned short int) num.ul;
947 else if (flags & CHAR)
948 *ARG (unsigned char *) = (unsigned char) num.ul;
949 else
950 *ARG (unsigned int *) = (unsigned int) num.ul;
952 else
954 if (flags & LONGDBL)
955 *ARG (LONGLONG int *) = num.q;
956 else if (flags & LONG)
957 *ARG (long int *) = num.l;
958 else if (flags & SHORT)
959 *ARG (short int *) = (short int) num.l;
960 else if (flags & CHAR)
961 *ARG (signed char *) = (signed char) num.ul;
962 else
963 *ARG (int *) = (int) num.l;
965 ++done;
967 break;
969 case 'e': /* Floating-point numbers. */
970 case 'E':
971 case 'f':
972 case 'g':
973 case 'G':
974 case 'a':
975 case 'A':
976 c = inchar ();
977 if (c == EOF)
978 input_error ();
980 /* Check for a sign. */
981 if (c == '-' || c == '+')
983 negative = c == '-';
984 if (inchar () == EOF)
985 /* EOF is only an input error before we read any chars. */
986 conv_error ();
987 if (! isdigit (c) && c != decimal)
989 /* This is no valid number. */
990 ungetc (c, s);
991 input_error ();
993 if (width > 0)
994 --width;
996 else
997 negative = 0;
999 /* Take care for the special arguments "nan" and "inf". */
1000 if (_tolower (c) == 'n')
1002 /* Maybe "nan". */
1003 ADDW (c);
1004 if (inchar () == EOF || _tolower (c) != 'a')
1005 input_error ();
1006 ADDW (c);
1007 if (inchar () == EOF || _tolower (c) != 'n')
1008 input_error ();
1009 ADDW (c);
1010 /* It is "nan". */
1011 goto scan_float;
1013 else if (_tolower (c) == 'i')
1015 /* Maybe "inf" or "infinity". */
1016 ADDW (c);
1017 if (inchar () == EOF || _tolower (c) != 'n')
1018 input_error ();
1019 ADDW (c);
1020 if (inchar () == EOF || _tolower (c) != 'f')
1021 input_error ();
1022 ADDW (c);
1023 /* It is as least "inf". */
1024 if (inchar () != EOF)
1026 if (_tolower (c) == 'i')
1028 /* Now we have to read the rest as well. */
1029 ADDW (c);
1030 if (inchar () == EOF || _tolower (c) != 'n')
1031 input_error ();
1032 ADDW (c);
1033 if (inchar () == EOF || _tolower (c) != 'i')
1034 input_error ();
1035 ADDW (c);
1036 if (inchar () == EOF || _tolower (c) != 't')
1037 input_error ();
1038 ADDW (c);
1039 if (inchar () == EOF || _tolower (c) != 'y')
1040 input_error ();
1041 ADDW (c);
1043 else
1044 /* Never mind. */
1045 ungetc (c, s);
1047 goto scan_float;
1050 is_hexa = 0;
1051 exp_char = 'e';
1052 if (c == '0')
1054 ADDW (c);
1055 c = inchar ();
1056 if (_tolower (c) == 'x')
1058 /* It is a number in hexadecimal format. */
1059 ADDW (c);
1061 is_hexa = 1;
1062 exp_char = 'p';
1064 /* Grouping is not allowed. */
1065 flags &= ~GROUP;
1066 c = inchar ();
1070 got_dot = got_e = 0;
1073 if (isdigit (c))
1074 ADDW (c);
1075 else if (!got_e && is_hexa && isxdigit (c))
1076 ADDW (c);
1077 else if (got_e && wp[wpsize - 1] == exp_char
1078 && (c == '-' || c == '+'))
1079 ADDW (c);
1080 else if (wpsize > 0 && !got_e && _tolower (c) == exp_char)
1082 ADDW (exp_char);
1083 got_e = got_dot = 1;
1085 else if (c == decimal && !got_dot)
1087 ADDW (c);
1088 got_dot = 1;
1090 else if ((flags & GROUP) && c == thousands && !got_dot)
1091 ADDW (c);
1092 else
1094 /* The last read character is not part of the number
1095 anymore. */
1096 ungetc (c, s);
1097 break;
1099 if (width > 0)
1100 --width;
1102 while (width != 0 && inchar () != EOF);
1104 /* Have we read any character? If we try to read a number
1105 in hexadecimal notation and we have read only the `0x'
1106 prefix this is an error. */
1107 if (wpsize == 0 || (is_hexa && wpsize == 2))
1108 conv_error ();
1110 scan_float:
1111 /* Convert the number. */
1112 ADDW ('\0');
1113 if (flags & LONGDBL)
1115 long double d = __strtold_internal (wp, &tw, flags & GROUP);
1116 if (!(flags & SUPPRESS) && tw != wp)
1117 *ARG (long double *) = negative ? -d : d;
1119 else if (flags & LONG)
1121 double d = __strtod_internal (wp, &tw, flags & GROUP);
1122 if (!(flags & SUPPRESS) && tw != wp)
1123 *ARG (double *) = negative ? -d : d;
1125 else
1127 float d = __strtof_internal (wp, &tw, flags & GROUP);
1128 if (!(flags & SUPPRESS) && tw != wp)
1129 *ARG (float *) = negative ? -d : d;
1132 if (tw == wp)
1133 conv_error ();
1135 if (!(flags & SUPPRESS))
1136 ++done;
1137 break;
1139 case '[': /* Character class. */
1140 if (flags & LONG)
1142 STRING_ARG (wstr, wchar_t);
1143 c = '\0'; /* This is to keep gcc quiet. */
1145 else
1147 STRING_ARG (str, char);
1149 c = inchar ();
1150 if (c == EOF)
1151 input_error ();
1154 if (*f == '^')
1156 ++f;
1157 not_in = 1;
1159 else
1160 not_in = 0;
1162 /* Fill WP with byte flags indexed by character.
1163 We will use this flag map for matching input characters. */
1164 if (wpmax < UCHAR_MAX)
1166 wpmax = UCHAR_MAX;
1167 wp = (char *) alloca (wpmax);
1169 memset (wp, 0, UCHAR_MAX);
1171 fc = *f;
1172 if (fc == ']' || fc == '-')
1174 /* If ] or - appears before any char in the set, it is not
1175 the terminator or separator, but the first char in the
1176 set. */
1177 wp[fc] = 1;
1178 ++f;
1181 while ((fc = *f++) != '\0' && fc != ']')
1183 if (fc == '-' && *f != '\0' && *f != ']' &&
1184 (unsigned char) f[-2] <= (unsigned char) *f)
1186 /* Add all characters from the one before the '-'
1187 up to (but not including) the next format char. */
1188 for (fc = f[-2]; fc < *f; ++fc)
1189 wp[fc] = 1;
1191 else
1192 /* Add the character to the flag map. */
1193 wp[fc] = 1;
1195 if (fc == '\0')
1197 if (!(flags & LONG))
1198 ungetc (c, s);
1199 conv_error();
1202 if (flags & LONG)
1204 wint_t val;
1205 int first = 1;
1209 size_t cnt = 0;
1210 NEXT_WIDE_CHAR (first);
1211 if (val <= 255 && wp[val] == not_in)
1213 ungetc (val, s);
1214 break;
1216 STRING_ADD_CHAR (wstr, val, wchar_t);
1217 if (width > 0)
1218 --width;
1219 first = 0;
1221 while (width != 0);
1223 if (first)
1224 conv_error ();
1226 if (!(flags & SUPPRESS))
1228 *wstr = L'\0';
1229 ++done;
1232 else
1234 num.ul = read_in - 1; /* -1 because we already read one char. */
1237 if (wp[c] == not_in)
1239 ungetc (c, s);
1240 break;
1242 STRING_ADD_CHAR (str, c, char);
1243 if (width > 0)
1244 --width;
1246 while (width != 0 && inchar () != EOF);
1248 if (read_in == num.ul)
1249 conv_error ();
1251 if (!(flags & SUPPRESS))
1253 *str = '\0';
1254 ++done;
1257 break;
1259 case 'p': /* Generic pointer. */
1260 base = 16;
1261 /* A PTR must be the same size as a `long int'. */
1262 flags &= ~(SHORT|LONGDBL);
1263 flags |= LONG;
1264 number_signed = 0;
1265 read_pointer = 1;
1266 goto number;
1268 default:
1269 /* If this is an unknown format character punt. */
1270 conv_error ();
1274 /* The last thing we saw int the format string was a white space.
1275 Consume the last white spaces. */
1276 if (skip_space)
1279 c = inchar ();
1280 while (isspace (c));
1281 ungetc (c, s);
1284 /* Unlock stream. */
1285 UNLOCK_STREAM (s);
1287 return done;
1290 #ifdef USE_IN_LIBIO
1292 __vfscanf (FILE *s, const char *format, va_list argptr)
1294 return _IO_vfscanf (s, format, argptr, NULL);
1296 #endif
1298 weak_alias (__vfscanf, vfscanf)