..
[glibc.git] / stdio-common / vfscanf.c
blob1149686d75ad8b1e6f1aed23c07005180169b035
1 /* Copyright (C) 1991-2002, 2003, 2004, 2005, 2006, 2007
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <ctype.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <wchar.h>
30 #include <wctype.h>
31 #include <bits/libc-lock.h>
32 #include <locale/localeinfo.h>
34 #ifdef __GNUC__
35 # define HAVE_LONGLONG
36 # define LONGLONG long long
37 #else
38 # define LONGLONG long
39 #endif
41 /* Determine whether we have to handle `long long' at all. */
42 #if LONG_MAX == LONG_LONG_MAX
43 # define need_longlong 0
44 #else
45 # define need_longlong 1
46 #endif
48 /* Determine whether we have to handle `long'. */
49 #if INT_MAX == LONG_MAX
50 # define need_long 0
51 #else
52 # define need_long 1
53 #endif
55 /* Those are flags in the conversion format. */
56 #define LONG 0x001 /* l: long or double */
57 #define LONGDBL 0x002 /* L: long long or long double */
58 #define SHORT 0x004 /* h: short */
59 #define SUPPRESS 0x008 /* *: suppress assignment */
60 #define POINTER 0x010 /* weird %p pointer (`fake hex') */
61 #define NOSKIP 0x020 /* do not skip blanks */
62 #define GROUP 0x080 /* ': group numbers */
63 #define MALLOC 0x100 /* a: malloc strings */
64 #define CHAR 0x200 /* hh: char */
65 #define I18N 0x400 /* I: use locale's digits */
68 #include <locale/localeinfo.h>
69 #include <libioP.h>
70 #include <libio.h>
72 #undef va_list
73 #define va_list _IO_va_list
75 #ifdef COMPILE_WSCANF
76 # define ungetc(c, s) ((void) (c == WEOF \
77 || (--read_in, \
78 INTUSE(_IO_sputbackwc) (s, c))))
79 # define ungetc_not_eof(c, s) ((void) (--read_in, \
80 INTUSE(_IO_sputbackwc) (s, c)))
81 # define inchar() (c == WEOF ? ((errno = inchar_errno), WEOF) \
82 : ((c = _IO_getwc_unlocked (s)), \
83 (void) (c != WEOF \
84 ? ++read_in \
85 : (size_t) (inchar_errno = errno)), c))
87 # define MEMCPY(d, s, n) __wmemcpy (d, s, n)
88 # define ISSPACE(Ch) iswspace (Ch)
89 # define ISDIGIT(Ch) iswdigit (Ch)
90 # define ISXDIGIT(Ch) iswxdigit (Ch)
91 # define TOLOWER(Ch) towlower (Ch)
92 # define ORIENT if (_IO_fwide (s, 1) != 1) return WEOF
93 # define __strtoll_internal __wcstoll_internal
94 # define __strtoull_internal __wcstoull_internal
95 # define __strtol_internal __wcstol_internal
96 # define __strtoul_internal __wcstoul_internal
97 # define __strtold_internal __wcstold_internal
98 # define __strtod_internal __wcstod_internal
99 # define __strtof_internal __wcstof_internal
101 # define L_(Str) L##Str
102 # define CHAR_T wchar_t
103 # define UCHAR_T unsigned int
104 # define WINT_T wint_t
105 # undef EOF
106 # define EOF WEOF
107 #else
108 # define ungetc(c, s) ((void) ((int) c == EOF \
109 || (--read_in, \
110 INTUSE(_IO_sputbackc) (s, (unsigned char) c))))
111 # define ungetc_not_eof(c, s) ((void) (--read_in, \
112 INTUSE(_IO_sputbackc) (s, (unsigned char) c)))
113 # define inchar() (c == EOF ? ((errno = inchar_errno), EOF) \
114 : ((c = _IO_getc_unlocked (s)), \
115 (void) (c != EOF \
116 ? ++read_in \
117 : (size_t) (inchar_errno = errno)), c))
118 # define MEMCPY(d, s, n) memcpy (d, s, n)
119 # define ISSPACE(Ch) __isspace_l (Ch, loc)
120 # define ISDIGIT(Ch) __isdigit_l (Ch, loc)
121 # define ISXDIGIT(Ch) __isxdigit_l (Ch, loc)
122 # define TOLOWER(Ch) __tolower_l ((unsigned char) (Ch), loc)
123 # define ORIENT if (_IO_vtable_offset (s) == 0 \
124 && _IO_fwide (s, -1) != -1) \
125 return EOF
127 # define L_(Str) Str
128 # define CHAR_T char
129 # define UCHAR_T unsigned char
130 # define WINT_T int
131 #endif
133 #define encode_error() do { \
134 errval = 4; \
135 __set_errno (EILSEQ); \
136 goto errout; \
137 } while (0)
138 #define conv_error() do { \
139 errval = 2; \
140 goto errout; \
141 } while (0)
142 #define input_error() do { \
143 errval = 1; \
144 if (done == 0) done = EOF; \
145 goto errout; \
146 } while (0)
147 #define ARGCHECK(s, format) \
148 do \
150 /* Check file argument for consistence. */ \
151 CHECK_FILE (s, EOF); \
152 if (s->_flags & _IO_NO_READS) \
154 __set_errno (EBADF); \
155 return EOF; \
157 else if (format == NULL) \
159 MAYBE_SET_EINVAL; \
160 return EOF; \
162 } while (0)
163 #define LOCK_STREAM(S) \
164 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, (S)); \
165 _IO_flockfile (S)
166 #define UNLOCK_STREAM(S) \
167 _IO_funlockfile (S); \
168 __libc_cleanup_region_end (0)
171 /* Read formatted input from S according to the format string
172 FORMAT, using the argument list in ARG.
173 Return the number of assignments made, or -1 for an input error. */
174 #ifdef COMPILE_WSCANF
176 _IO_vfwscanf (_IO_FILE *s, const wchar_t *format, _IO_va_list argptr,
177 int *errp)
178 #else
180 _IO_vfscanf_internal (_IO_FILE *s, const char *format, _IO_va_list argptr,
181 int *errp)
182 #endif
184 va_list arg;
185 register const CHAR_T *f = format;
186 register UCHAR_T fc; /* Current character of the format. */
187 register WINT_T done = 0; /* Assignments done. */
188 register size_t read_in = 0; /* Chars read in. */
189 register WINT_T c = 0; /* Last char read. */
190 register int width; /* Maximum field width. */
191 register int flags; /* Modifiers for current format element. */
192 int errval = 0;
193 #ifndef COMPILE_WSCANF
194 __locale_t loc = _NL_CURRENT_LOCALE;
195 struct locale_data *const curctype = loc->__locales[LC_CTYPE];
196 #endif
198 /* Errno of last failed inchar call. */
199 int inchar_errno = 0;
200 /* Status for reading F-P nums. */
201 char got_dot, got_e, negative;
202 /* If a [...] is a [^...]. */
203 CHAR_T not_in;
204 #define exp_char not_in
205 /* Base for integral numbers. */
206 int base;
207 /* Signedness for integral numbers. */
208 int number_signed;
209 #define is_hexa number_signed
210 /* Decimal point character. */
211 #ifdef COMPILE_WSCANF
212 wint_t decimal;
213 #else
214 const char *decimal;
215 #endif
216 /* The thousands character of the current locale. */
217 #ifdef COMPILE_WSCANF
218 wint_t thousands;
219 #else
220 const char *thousands;
221 #endif
222 /* State for the conversions. */
223 mbstate_t state;
224 /* Integral holding variables. */
225 union
227 long long int q;
228 unsigned long long int uq;
229 long int l;
230 unsigned long int ul;
231 } num;
232 /* Character-buffer pointer. */
233 char *str = NULL;
234 wchar_t *wstr = NULL;
235 char **strptr = NULL;
236 ssize_t strsize = 0;
237 /* We must not react on white spaces immediately because they can
238 possibly be matched even if in the input stream no character is
239 available anymore. */
240 int skip_space = 0;
241 /* Nonzero if we are reading a pointer. */
242 int read_pointer;
243 /* Workspace. */
244 CHAR_T *tw; /* Temporary pointer. */
245 CHAR_T *wp = NULL; /* Workspace. */
246 size_t wpmax = 0; /* Maximal size of workspace. */
247 size_t wpsize; /* Currently used bytes in workspace. */
248 #define ADDW(Ch) \
249 do \
251 if (wpsize == wpmax) \
253 CHAR_T *old = wp; \
254 wpmax = (UCHAR_MAX + 1 > 2 * wpmax ? UCHAR_MAX + 1 : 2 * wpmax); \
255 wp = (CHAR_T *) alloca (wpmax * sizeof (wchar_t)); \
256 if (old != NULL) \
257 MEMCPY (wp, old, wpsize); \
259 wp[wpsize++] = (Ch); \
261 while (0)
263 #ifdef __va_copy
264 __va_copy (arg, argptr);
265 #else
266 arg = (va_list) argptr;
267 #endif
269 #ifdef ORIENT
270 ORIENT;
271 #endif
273 ARGCHECK (s, format);
276 #ifndef COMPILE_WSCANF
277 struct locale_data *const curnumeric = loc->__locales[LC_NUMERIC];
278 #endif
280 /* Figure out the decimal point character. */
281 #ifdef COMPILE_WSCANF
282 decimal = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
283 #else
284 decimal = curnumeric->values[_NL_ITEM_INDEX (DECIMAL_POINT)].string;
285 #endif
286 /* Figure out the thousands separator character. */
287 #ifdef COMPILE_WSCANF
288 thousands = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_THOUSANDS_SEP_WC);
289 #else
290 thousands = curnumeric->values[_NL_ITEM_INDEX (THOUSANDS_SEP)].string;
291 if (*thousands == '\0')
292 thousands = NULL;
293 #endif
296 /* Lock the stream. */
297 LOCK_STREAM (s);
300 #ifndef COMPILE_WSCANF
301 /* From now on we use `state' to convert the format string. */
302 memset (&state, '\0', sizeof (state));
303 #endif
305 /* Run through the format string. */
306 while (*f != '\0')
308 unsigned int argpos;
309 /* Extract the next argument, which is of type TYPE.
310 For a %N$... spec, this is the Nth argument from the beginning;
311 otherwise it is the next argument after the state now in ARG. */
312 #ifdef __va_copy
313 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
314 ({ unsigned int pos = argpos; \
315 va_list arg; \
316 __va_copy (arg, argptr); \
317 while (--pos > 0) \
318 (void) va_arg (arg, void *); \
319 va_arg (arg, type); \
321 #else
322 # if 0
323 /* XXX Possible optimization. */
324 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
325 ({ va_list arg = (va_list) argptr; \
326 arg = (va_list) ((char *) arg \
327 + (argpos - 1) \
328 * __va_rounded_size (void *)); \
329 va_arg (arg, type); \
331 # else
332 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
333 ({ unsigned int pos = argpos; \
334 va_list arg = (va_list) argptr; \
335 while (--pos > 0) \
336 (void) va_arg (arg, void *); \
337 va_arg (arg, type); \
339 # endif
340 #endif
342 #ifndef COMPILE_WSCANF
343 if (!isascii ((unsigned char) *f))
345 /* Non-ASCII, may be a multibyte. */
346 int len = __mbrlen (f, strlen (f), &state);
347 if (len > 0)
351 c = inchar ();
352 if (__builtin_expect (c == EOF, 0))
353 input_error ();
354 else if (c != (unsigned char) *f++)
356 ungetc_not_eof (c, s);
357 conv_error ();
360 while (--len > 0);
361 continue;
364 #endif
366 fc = *f++;
367 if (fc != '%')
369 /* Remember to skip spaces. */
370 if (ISSPACE (fc))
372 skip_space = 1;
373 continue;
376 /* Read a character. */
377 c = inchar ();
379 /* Characters other than format specs must just match. */
380 if (__builtin_expect (c == EOF, 0))
381 input_error ();
383 /* We saw white space char as the last character in the format
384 string. Now it's time to skip all leading white space. */
385 if (skip_space)
387 while (ISSPACE (c))
388 if (__builtin_expect (inchar () == EOF, 0))
389 input_error ();
390 skip_space = 0;
393 if (__builtin_expect (c != fc, 0))
395 ungetc (c, s);
396 conv_error ();
399 continue;
402 /* This is the start of the conversion string. */
403 flags = 0;
405 /* Not yet decided whether we read a pointer or not. */
406 read_pointer = 0;
408 /* Initialize state of modifiers. */
409 argpos = 0;
411 /* Prepare temporary buffer. */
412 wpsize = 0;
414 /* Check for a positional parameter specification. */
415 if (ISDIGIT ((UCHAR_T) *f))
417 argpos = (UCHAR_T) *f++ - L_('0');
418 while (ISDIGIT ((UCHAR_T) *f))
419 argpos = argpos * 10 + ((UCHAR_T) *f++ - L_('0'));
420 if (*f == L_('$'))
421 ++f;
422 else
424 /* Oops; that was actually the field width. */
425 width = argpos;
426 argpos = 0;
427 goto got_width;
431 /* Check for the assignment-suppressing, the number grouping flag,
432 and the signal to use the locale's digit representation. */
433 while (*f == L_('*') || *f == L_('\'') || *f == L_('I'))
434 switch (*f++)
436 case L_('*'):
437 flags |= SUPPRESS;
438 break;
439 case L_('\''):
440 flags |= GROUP;
441 break;
442 case L_('I'):
443 flags |= I18N;
444 break;
447 /* Find the maximum field width. */
448 width = 0;
449 while (ISDIGIT ((UCHAR_T) *f))
451 width *= 10;
452 width += (UCHAR_T) *f++ - L_('0');
454 got_width:
455 if (width == 0)
456 width = -1;
458 /* Check for type modifiers. */
459 switch (*f++)
461 case L_('h'):
462 /* ints are short ints or chars. */
463 if (*f == L_('h'))
465 ++f;
466 flags |= CHAR;
468 else
469 flags |= SHORT;
470 break;
471 case L_('l'):
472 if (*f == L_('l'))
474 /* A double `l' is equivalent to an `L'. */
475 ++f;
476 flags |= LONGDBL | LONG;
478 else
479 /* ints are long ints. */
480 flags |= LONG;
481 break;
482 case L_('q'):
483 case L_('L'):
484 /* doubles are long doubles, and ints are long long ints. */
485 flags |= LONGDBL | LONG;
486 break;
487 case L_('a'):
488 /* The `a' is used as a flag only if followed by `s', `S' or
489 `['. */
490 if (*f != L_('s') && *f != L_('S') && *f != L_('['))
492 --f;
493 break;
495 /* String conversions (%s, %[) take a `char **'
496 arg and fill it in with a malloc'd pointer. */
497 flags |= MALLOC;
498 break;
499 case L_('z'):
500 if (need_longlong && sizeof (size_t) > sizeof (unsigned long int))
501 flags |= LONGDBL;
502 else if (sizeof (size_t) > sizeof (unsigned int))
503 flags |= LONG;
504 break;
505 case L_('j'):
506 if (need_longlong && sizeof (uintmax_t) > sizeof (unsigned long int))
507 flags |= LONGDBL;
508 else if (sizeof (uintmax_t) > sizeof (unsigned int))
509 flags |= LONG;
510 break;
511 case L_('t'):
512 if (need_longlong && sizeof (ptrdiff_t) > sizeof (long int))
513 flags |= LONGDBL;
514 else if (sizeof (ptrdiff_t) > sizeof (int))
515 flags |= LONG;
516 break;
517 default:
518 /* Not a recognized modifier. Backup. */
519 --f;
520 break;
523 /* End of the format string? */
524 if (__builtin_expect (*f == L_('\0'), 0))
525 conv_error ();
527 /* Find the conversion specifier. */
528 fc = *f++;
529 if (skip_space || (fc != L_('[') && fc != L_('c')
530 && fc != L_('C') && fc != L_('n')))
532 /* Eat whitespace. */
533 int save_errno = errno;
534 errno = 0;
536 if (__builtin_expect (inchar () == EOF && errno == EINTR, 0))
537 input_error ();
538 while (ISSPACE (c));
539 errno = save_errno;
540 ungetc (c, s);
541 skip_space = 0;
544 switch (fc)
546 case L_('%'): /* Must match a literal '%'. */
547 c = inchar ();
548 if (__builtin_expect (c == EOF, 0))
549 input_error ();
550 if (__builtin_expect (c != fc, 0))
552 ungetc_not_eof (c, s);
553 conv_error ();
555 break;
557 case L_('n'): /* Answer number of assignments done. */
558 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
559 with the 'n' conversion specifier. */
560 if (!(flags & SUPPRESS))
562 /* Don't count the read-ahead. */
563 if (need_longlong && (flags & LONGDBL))
564 *ARG (long long int *) = read_in;
565 else if (need_long && (flags & LONG))
566 *ARG (long int *) = read_in;
567 else if (flags & SHORT)
568 *ARG (short int *) = read_in;
569 else if (!(flags & CHAR))
570 *ARG (int *) = read_in;
571 else
572 *ARG (char *) = read_in;
574 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
575 /* We have a severe problem here. The ISO C standard
576 contradicts itself in explaining the effect of the %n
577 format in `scanf'. While in ISO C:1990 and the ISO C
578 Amendement 1:1995 the result is described as
580 Execution of a %n directive does not effect the
581 assignment count returned at the completion of
582 execution of the f(w)scanf function.
584 in ISO C Corrigendum 1:1994 the following was added:
586 Subclause 7.9.6.2
587 Add the following fourth example:
589 #include <stdio.h>
590 int d1, d2, n1, n2, i;
591 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
592 the value 123 is assigned to d1 and the value3 to n1.
593 Because %n can never get an input failure the value
594 of 3 is also assigned to n2. The value of d2 is not
595 affected. The value 3 is assigned to i.
597 We go for now with the historically correct code from ISO C,
598 i.e., we don't count the %n assignments. When it ever
599 should proof to be wrong just remove the #ifdef above. */
600 ++done;
601 #endif
603 break;
605 case L_('c'): /* Match characters. */
606 if ((flags & LONG) == 0)
608 if (!(flags & SUPPRESS))
610 str = ARG (char *);
611 if (str == NULL)
612 conv_error ();
615 c = inchar ();
616 if (__builtin_expect (c == EOF, 0))
617 input_error ();
619 if (width == -1)
620 width = 1;
622 #ifdef COMPILE_WSCANF
623 /* We have to convert the wide character(s) into multibyte
624 characters and store the result. */
625 memset (&state, '\0', sizeof (state));
629 size_t n;
631 n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c, &state);
632 if (__builtin_expect (n == (size_t) -1, 0))
633 /* No valid wide character. */
634 input_error ();
636 /* Increment the output pointer. Even if we don't
637 write anything. */
638 str += n;
640 while (--width > 0 && inchar () != EOF);
641 #else
642 if (!(flags & SUPPRESS))
645 *str++ = c;
646 while (--width > 0 && inchar () != EOF);
648 else
649 while (--width > 0 && inchar () != EOF);
650 #endif
652 if (!(flags & SUPPRESS))
653 ++done;
655 break;
657 /* FALLTHROUGH */
658 case L_('C'):
659 if (!(flags & SUPPRESS))
661 wstr = ARG (wchar_t *);
662 if (wstr == NULL)
663 conv_error ();
666 c = inchar ();
667 if (__builtin_expect (c == EOF, 0))
668 input_error ();
670 #ifdef COMPILE_WSCANF
671 /* Just store the incoming wide characters. */
672 if (!(flags & SUPPRESS))
675 *wstr++ = c;
676 while (--width > 0 && inchar () != EOF);
678 else
679 while (--width > 0 && inchar () != EOF);
680 #else
682 /* We have to convert the multibyte input sequence to wide
683 characters. */
684 char buf[1];
685 mbstate_t cstate;
687 memset (&cstate, '\0', sizeof (cstate));
691 /* This is what we present the mbrtowc function first. */
692 buf[0] = c;
694 while (1)
696 size_t n;
698 n = __mbrtowc (!(flags & SUPPRESS) ? wstr : NULL,
699 buf, 1, &cstate);
701 if (n == (size_t) -2)
703 /* Possibly correct character, just not enough
704 input. */
705 if (__builtin_expect (inchar () == EOF, 0))
706 encode_error ();
708 buf[0] = c;
709 continue;
712 if (__builtin_expect (n != 1, 0))
713 encode_error ();
715 /* We have a match. */
716 break;
719 /* Advance the result pointer. */
720 ++wstr;
722 while (--width > 0 && inchar () != EOF);
724 #endif
726 if (!(flags & SUPPRESS))
727 ++done;
729 break;
731 case L_('s'): /* Read a string. */
732 if (!(flags & LONG))
734 #define STRING_ARG(Str, Type) \
735 do if (!(flags & SUPPRESS)) \
737 if (flags & MALLOC) \
739 /* The string is to be stored in a malloc'd buffer. */ \
740 strptr = ARG (char **); \
741 if (strptr == NULL) \
742 conv_error (); \
743 /* Allocate an initial buffer. */ \
744 strsize = 100; \
745 *strptr = (char *) malloc (strsize * sizeof (Type)); \
746 Str = (Type *) *strptr; \
748 else \
749 Str = ARG (Type *); \
750 if (Str == NULL) \
751 conv_error (); \
752 } while (0)
753 STRING_ARG (str, char);
755 c = inchar ();
756 if (__builtin_expect (c == EOF, 0))
757 input_error ();
759 #ifdef COMPILE_WSCANF
760 memset (&state, '\0', sizeof (state));
761 #endif
765 if (ISSPACE (c))
767 ungetc_not_eof (c, s);
768 break;
771 #ifdef COMPILE_WSCANF
772 /* This is quite complicated. We have to convert the
773 wide characters into multibyte characters and then
774 store them. */
776 size_t n;
778 if (!(flags & SUPPRESS) && (flags & MALLOC)
779 && str + MB_CUR_MAX >= *strptr + strsize)
781 /* We have to enlarge the buffer if the `a' flag
782 was given. */
783 size_t strleng = str - *strptr;
784 char *newstr;
786 newstr = (char *) realloc (*strptr, strsize * 2);
787 if (newstr == NULL)
789 /* Can't allocate that much. Last-ditch
790 effort. */
791 newstr = (char *) realloc (*strptr,
792 strleng + MB_CUR_MAX);
793 if (newstr == NULL)
795 /* We lose. Oh well. Terminate the
796 string and stop converting,
797 so at least we don't skip any input. */
798 ((char *) (*strptr))[strleng] = '\0';
799 ++done;
800 conv_error ();
802 else
804 *strptr = newstr;
805 str = newstr + strleng;
806 strsize = strleng + MB_CUR_MAX;
809 else
811 *strptr = newstr;
812 str = newstr + strleng;
813 strsize *= 2;
817 n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c,
818 &state);
819 if (__builtin_expect (n == (size_t) -1, 0))
820 encode_error ();
822 assert (n <= MB_CUR_MAX);
823 str += n;
825 #else
826 /* This is easy. */
827 if (!(flags & SUPPRESS))
829 *str++ = c;
830 if ((flags & MALLOC)
831 && (char *) str == *strptr + strsize)
833 /* Enlarge the buffer. */
834 str = (char *) realloc (*strptr, 2 * strsize);
835 if (str == NULL)
837 /* Can't allocate that much. Last-ditch
838 effort. */
839 str = (char *) realloc (*strptr, strsize + 1);
840 if (str == NULL)
842 /* We lose. Oh well. Terminate the
843 string and stop converting,
844 so at least we don't skip any input. */
845 ((char *) (*strptr))[strsize - 1] = '\0';
846 ++done;
847 conv_error ();
849 else
851 *strptr = (char *) str;
852 str += strsize;
853 ++strsize;
856 else
858 *strptr = (char *) str;
859 str += strsize;
860 strsize *= 2;
864 #endif
866 while ((width <= 0 || --width > 0) && inchar () != EOF);
868 if (!(flags & SUPPRESS))
870 #ifdef COMPILE_WSCANF
871 /* We have to emit the code to get into the initial
872 state. */
873 char buf[MB_LEN_MAX];
874 size_t n = __wcrtomb (buf, L'\0', &state);
875 if (n > 0 && (flags & MALLOC)
876 && str + n >= *strptr + strsize)
878 /* Enlarge the buffer. */
879 size_t strleng = str - *strptr;
880 char *newstr;
882 newstr = (char *) realloc (*strptr, strleng + n + 1);
883 if (newstr == NULL)
885 /* We lose. Oh well. Terminate the string
886 and stop converting, so at least we don't
887 skip any input. */
888 ((char *) (*strptr))[strleng] = '\0';
889 ++done;
890 conv_error ();
892 else
894 *strptr = newstr;
895 str = newstr + strleng;
896 strsize = strleng + n + 1;
900 str = __mempcpy (str, buf, n);
901 #endif
902 *str++ = '\0';
904 if ((flags & MALLOC) && str - *strptr != strsize)
906 char *cp = (char *) realloc (*strptr, str - *strptr);
907 if (cp != NULL)
908 *strptr = cp;
911 ++done;
913 break;
915 /* FALLTHROUGH */
917 case L_('S'):
919 #ifndef COMPILE_WSCANF
920 mbstate_t cstate;
921 #endif
923 /* Wide character string. */
924 STRING_ARG (wstr, wchar_t);
926 c = inchar ();
927 if (__builtin_expect (c == EOF, 0))
928 input_error ();
930 #ifndef COMPILE_WSCANF
931 memset (&cstate, '\0', sizeof (cstate));
932 #endif
936 if (ISSPACE (c))
938 ungetc_not_eof (c, s);
939 break;
942 #ifdef COMPILE_WSCANF
943 /* This is easy. */
944 if (!(flags & SUPPRESS))
946 *wstr++ = c;
947 if ((flags & MALLOC)
948 && wstr == (wchar_t *) *strptr + strsize)
950 /* Enlarge the buffer. */
951 wstr = (wchar_t *) realloc (*strptr,
952 (2 * strsize)
953 * sizeof (wchar_t));
954 if (wstr == NULL)
956 /* Can't allocate that much. Last-ditch
957 effort. */
958 wstr = (wchar_t *) realloc (*strptr,
959 (strsize + 1)
960 * sizeof (wchar_t));
961 if (wstr == NULL)
963 /* We lose. Oh well. Terminate the string
964 and stop converting, so at least we don't
965 skip any input. */
966 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
967 ++done;
968 conv_error ();
970 else
972 *strptr = (char *) wstr;
973 wstr += strsize;
974 ++strsize;
977 else
979 *strptr = (char *) wstr;
980 wstr += strsize;
981 strsize *= 2;
985 #else
987 char buf[1];
989 buf[0] = c;
991 while (1)
993 size_t n;
995 n = __mbrtowc (!(flags & SUPPRESS) ? wstr : NULL,
996 buf, 1, &cstate);
998 if (n == (size_t) -2)
1000 /* Possibly correct character, just not enough
1001 input. */
1002 if (__builtin_expect (inchar () == EOF, 0))
1003 encode_error ();
1005 buf[0] = c;
1006 continue;
1009 if (__builtin_expect (n != 1, 0))
1010 encode_error ();
1012 /* We have a match. */
1013 ++wstr;
1014 break;
1017 if (!(flags & SUPPRESS) && (flags & MALLOC)
1018 && wstr == (wchar_t *) *strptr + strsize)
1020 /* Enlarge the buffer. */
1021 wstr = (wchar_t *) realloc (*strptr,
1022 (2 * strsize
1023 * sizeof (wchar_t)));
1024 if (wstr == NULL)
1026 /* Can't allocate that much. Last-ditch effort. */
1027 wstr = (wchar_t *) realloc (*strptr,
1028 ((strsize + 1)
1029 * sizeof (wchar_t)));
1030 if (wstr == NULL)
1032 /* We lose. Oh well. Terminate the
1033 string and stop converting, so at
1034 least we don't skip any input. */
1035 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
1036 ++done;
1037 conv_error ();
1039 else
1041 *strptr = (char *) wstr;
1042 wstr += strsize;
1043 ++strsize;
1046 else
1048 *strptr = (char *) wstr;
1049 wstr += strsize;
1050 strsize *= 2;
1054 #endif
1056 while ((width <= 0 || --width > 0) && inchar () != EOF);
1058 if (!(flags & SUPPRESS))
1060 *wstr++ = L'\0';
1062 if ((flags & MALLOC) && wstr - (wchar_t *) *strptr != strsize)
1064 wchar_t *cp = (wchar_t *) realloc (*strptr,
1065 ((wstr
1066 - (wchar_t *) *strptr)
1067 * sizeof(wchar_t)));
1068 if (cp != NULL)
1069 *strptr = (char *) cp;
1072 ++done;
1075 break;
1077 case L_('x'): /* Hexadecimal integer. */
1078 case L_('X'): /* Ditto. */
1079 base = 16;
1080 number_signed = 0;
1081 goto number;
1083 case L_('o'): /* Octal integer. */
1084 base = 8;
1085 number_signed = 0;
1086 goto number;
1088 case L_('u'): /* Unsigned decimal integer. */
1089 base = 10;
1090 number_signed = 0;
1091 goto number;
1093 case L_('d'): /* Signed decimal integer. */
1094 base = 10;
1095 number_signed = 1;
1096 goto number;
1098 case L_('i'): /* Generic number. */
1099 base = 0;
1100 number_signed = 1;
1102 number:
1103 c = inchar ();
1104 if (__builtin_expect (c == EOF, 0))
1105 input_error ();
1107 /* Check for a sign. */
1108 if (c == L_('-') || c == L_('+'))
1110 ADDW (c);
1111 if (width > 0)
1112 --width;
1113 c = inchar ();
1116 /* Look for a leading indication of base. */
1117 if (width != 0 && c == L_('0'))
1119 if (width > 0)
1120 --width;
1122 ADDW (c);
1123 c = inchar ();
1125 if (width != 0 && TOLOWER (c) == L_('x'))
1127 if (base == 0)
1128 base = 16;
1129 if (base == 16)
1131 if (width > 0)
1132 --width;
1133 c = inchar ();
1136 else if (base == 0)
1137 base = 8;
1140 if (base == 0)
1141 base = 10;
1143 if (base == 10 && __builtin_expect ((flags & I18N) != 0, 0))
1145 int from_level;
1146 int to_level;
1147 int level;
1148 #ifdef COMPILE_WSCANF
1149 const wchar_t *wcdigits[10];
1150 const wchar_t *wcdigits_extended[10];
1151 #else
1152 const char *mbdigits[10];
1153 const char *mbdigits_extended[10];
1154 #endif
1155 /* "to_inpunct" is a map from ASCII digits to their
1156 equivalent in locale. This is defined for locales
1157 which use an extra digits set. */
1158 wctrans_t map = __wctrans ("to_inpunct");
1159 int n;
1161 from_level = 0;
1162 #ifdef COMPILE_WSCANF
1163 to_level = _NL_CURRENT_WORD (LC_CTYPE,
1164 _NL_CTYPE_INDIGITS_WC_LEN) - 1;
1165 #else
1166 to_level = (uint32_t) curctype->values[_NL_ITEM_INDEX (_NL_CTYPE_INDIGITS_MB_LEN)].word - 1;
1167 #endif
1169 /* Get the alternative digit forms if there are any. */
1170 if (__builtin_expect (map != NULL, 0))
1172 /* Adding new level for extra digits set in locale file. */
1173 ++to_level;
1175 for (n = 0; n < 10; ++n)
1177 #ifdef COMPILE_WSCANF
1178 wcdigits[n] = (const wchar_t *)
1179 _NL_CURRENT (LC_CTYPE, _NL_CTYPE_INDIGITS0_WC + n);
1181 wchar_t *wc_extended = (wchar_t *)
1182 alloca ((to_level + 2) * sizeof (wchar_t));
1183 __wmemcpy (wc_extended, wcdigits[n], to_level);
1184 wc_extended[to_level] = __towctrans (L'0' + n, map);
1185 wc_extended[to_level + 1] = '\0';
1186 wcdigits_extended[n] = wc_extended;
1187 #else
1188 mbdigits[n]
1189 = curctype->values[_NL_CTYPE_INDIGITS0_MB + n].string;
1191 /* Get the equivalent wide char in map. */
1192 wint_t extra_wcdigit = __towctrans (L'0' + n, map);
1194 /* Convert it to multibyte representation. */
1195 mbstate_t state;
1196 memset (&state, '\0', sizeof (state));
1198 char extra_mbdigit[MB_LEN_MAX];
1199 size_t mblen
1200 = __wcrtomb (extra_mbdigit, extra_wcdigit, &state);
1202 if (mblen == (size_t) -1)
1204 /* Ignore this new level. */
1205 map = NULL;
1206 break;
1209 /* Calculate the length of mbdigits[n]. */
1210 const char *last_char = mbdigits[n];
1211 for (level = 0; level < to_level; ++level)
1212 last_char = strchr (last_char, '\0') + 1;
1214 size_t mbdigits_len = last_char - mbdigits[n];
1216 /* Allocate memory for extended multibyte digit. */
1217 char *mb_extended;
1218 mb_extended = (char *) alloca (mbdigits_len + mblen + 1);
1220 /* And get the mbdigits + extra_digit string. */
1221 *(char *) __mempcpy (__mempcpy (mb_extended, mbdigits[n],
1222 mbdigits_len),
1223 extra_mbdigit, mblen) = '\0';
1224 mbdigits_extended[n] = mb_extended;
1225 #endif
1229 /* Read the number into workspace. */
1230 while (c != EOF && width != 0)
1232 /* In this round we get the pointer to the digit strings
1233 and also perform the first round of comparisons. */
1234 for (n = 0; n < 10; ++n)
1236 /* Get the string for the digits with value N. */
1237 #ifdef COMPILE_WSCANF
1238 if (__builtin_expect (map != NULL, 0))
1239 wcdigits[n] = wcdigits_extended[n];
1240 else
1241 wcdigits[n] = (const wchar_t *)
1242 _NL_CURRENT (LC_CTYPE, _NL_CTYPE_INDIGITS0_WC + n);
1243 wcdigits[n] += from_level;
1245 if (c == (wint_t) *wcdigits[n])
1247 to_level = from_level;
1248 break;
1251 /* Advance the pointer to the next string. */
1252 ++wcdigits[n];
1253 #else
1254 const char *cmpp;
1255 int avail = width > 0 ? width : INT_MAX;
1257 if (__builtin_expect (map != NULL, 0))
1258 mbdigits[n] = mbdigits_extended[n];
1259 else
1260 mbdigits[n]
1261 = curctype->values[_NL_CTYPE_INDIGITS0_MB + n].string;
1263 for (level = 0; level < from_level; level++)
1264 mbdigits[n] = strchr (mbdigits[n], '\0') + 1;
1266 cmpp = mbdigits[n];
1267 while ((unsigned char) *cmpp == c && avail > 0)
1269 if (*++cmpp == '\0')
1270 break;
1271 else
1273 if ((c = inchar ()) == EOF)
1274 break;
1275 --avail;
1279 if (*cmpp == '\0')
1281 if (width > 0)
1282 width = avail;
1283 to_level = from_level;
1284 break;
1287 /* We are pushing all read characters back. */
1288 if (cmpp > mbdigits[n])
1290 ungetc (c, s);
1291 while (--cmpp > mbdigits[n])
1292 ungetc_not_eof ((unsigned char) *cmpp, s);
1293 c = (unsigned char) *cmpp;
1296 /* Advance the pointer to the next string. */
1297 mbdigits[n] = strchr (mbdigits[n], '\0') + 1;
1298 #endif
1301 if (n == 10)
1303 /* Have not yet found the digit. */
1304 for (level = from_level + 1; level <= to_level; ++level)
1306 /* Search all ten digits of this level. */
1307 for (n = 0; n < 10; ++n)
1309 #ifdef COMPILE_WSCANF
1310 if (c == (wint_t) *wcdigits[n])
1311 break;
1313 /* Advance the pointer to the next string. */
1314 ++wcdigits[n];
1315 #else
1316 const char *cmpp;
1317 int avail = width > 0 ? width : INT_MAX;
1319 cmpp = mbdigits[n];
1320 while ((unsigned char) *cmpp == c && avail > 0)
1322 if (*++cmpp == '\0')
1323 break;
1324 else
1326 if ((c = inchar ()) == EOF)
1327 break;
1328 --avail;
1332 if (*cmpp == '\0')
1334 if (width > 0)
1335 width = avail;
1336 break;
1339 /* We are pushing all read characters back. */
1340 if (cmpp > mbdigits[n])
1342 ungetc (c, s);
1343 while (--cmpp > mbdigits[n])
1344 ungetc_not_eof ((unsigned char) *cmpp, s);
1345 c = (unsigned char) *cmpp;
1348 /* Advance the pointer to the next string. */
1349 mbdigits[n] = strchr (mbdigits[n], '\0') + 1;
1350 #endif
1353 if (n < 10)
1355 /* Found it. */
1356 from_level = level;
1357 to_level = level;
1358 break;
1363 if (n < 10)
1364 c = L_('0') + n;
1365 else if ((flags & GROUP)
1366 #ifdef COMPILE_WSCANF
1367 && thousands != L'\0'
1368 #else
1369 && thousands != NULL
1370 #endif
1373 /* Try matching against the thousands separator. */
1374 #ifdef COMPILE_WSCANF
1375 if (c != thousands)
1376 break;
1377 #else
1378 const char *cmpp = thousands;
1379 int avail = width > 0 ? width : INT_MAX;
1381 while ((unsigned char) *cmpp == c && avail > 0)
1383 ADDW (c);
1384 if (*++cmpp == '\0')
1385 break;
1386 else
1388 if ((c = inchar ()) == EOF)
1389 break;
1390 --avail;
1394 if (*cmpp != '\0')
1396 /* We are pushing all read characters back. */
1397 if (cmpp > thousands)
1399 wpsize -= cmpp - thousands;
1400 ungetc (c, s);
1401 while (--cmpp > thousands)
1402 ungetc_not_eof ((unsigned char) *cmpp, s);
1403 c = (unsigned char) *cmpp;
1405 break;
1408 if (width > 0)
1409 width = avail;
1411 /* The last thousands character will be added back by
1412 the ADDW below. */
1413 --wpsize;
1414 #endif
1416 else
1417 break;
1419 ADDW (c);
1420 if (width > 0)
1421 --width;
1423 c = inchar ();
1426 else
1427 /* Read the number into workspace. */
1428 while (c != EOF && width != 0)
1430 if (base == 16)
1432 if (!ISXDIGIT (c))
1433 break;
1435 else if (!ISDIGIT (c) || (int) (c - L_('0')) >= base)
1437 if (base == 10 && (flags & GROUP)
1438 #ifdef COMPILE_WSCANF
1439 && thousands != L'\0'
1440 #else
1441 && thousands != NULL
1442 #endif
1445 /* Try matching against the thousands separator. */
1446 #ifdef COMPILE_WSCANF
1447 if (c != thousands)
1448 break;
1449 #else
1450 const char *cmpp = thousands;
1451 int avail = width > 0 ? width : INT_MAX;
1453 while ((unsigned char) *cmpp == c && avail > 0)
1455 ADDW (c);
1456 if (*++cmpp == '\0')
1457 break;
1458 else
1460 if ((c = inchar ()) == EOF)
1461 break;
1462 --avail;
1466 if (*cmpp != '\0')
1468 /* We are pushing all read characters back. */
1469 if (cmpp > thousands)
1471 wpsize -= cmpp - thousands;
1472 ungetc (c, s);
1473 while (--cmpp > thousands)
1474 ungetc_not_eof ((unsigned char) *cmpp, s);
1475 c = (unsigned char) *cmpp;
1477 break;
1480 if (width > 0)
1481 width = avail;
1483 /* The last thousands character will be added back by
1484 the ADDW below. */
1485 --wpsize;
1486 #endif
1488 else
1489 break;
1491 ADDW (c);
1492 if (width > 0)
1493 --width;
1495 c = inchar ();
1498 if (wpsize == 0
1499 || (wpsize == 1 && (wp[0] == L_('+') || wp[0] == L_('-'))))
1501 /* There was no number. If we are supposed to read a pointer
1502 we must recognize "(nil)" as well. */
1503 if (__builtin_expect (wpsize == 0
1504 && read_pointer
1505 && (width < 0 || width >= 0)
1506 && c == '('
1507 && TOLOWER (inchar ()) == L_('n')
1508 && TOLOWER (inchar ()) == L_('i')
1509 && TOLOWER (inchar ()) == L_('l')
1510 && inchar () == L_(')'), 1))
1511 /* We must produce the value of a NULL pointer. A single
1512 '0' digit is enough. */
1513 ADDW (L_('0'));
1514 else
1516 /* The last read character is not part of the number
1517 anymore. */
1518 ungetc (c, s);
1520 conv_error ();
1523 else
1524 /* The just read character is not part of the number anymore. */
1525 ungetc (c, s);
1527 /* Convert the number. */
1528 ADDW (L_('\0'));
1529 if (need_longlong && (flags & LONGDBL))
1531 if (number_signed)
1532 num.q = __strtoll_internal (wp, &tw, base, flags & GROUP);
1533 else
1534 num.uq = __strtoull_internal (wp, &tw, base, flags & GROUP);
1536 else
1538 if (number_signed)
1539 num.l = __strtol_internal (wp, &tw, base, flags & GROUP);
1540 else
1541 num.ul = __strtoul_internal (wp, &tw, base, flags & GROUP);
1543 if (__builtin_expect (wp == tw, 0))
1544 conv_error ();
1546 if (!(flags & SUPPRESS))
1548 if (! number_signed)
1550 if (need_longlong && (flags & LONGDBL))
1551 *ARG (unsigned LONGLONG int *) = num.uq;
1552 else if (need_long && (flags & LONG))
1553 *ARG (unsigned long int *) = num.ul;
1554 else if (flags & SHORT)
1555 *ARG (unsigned short int *)
1556 = (unsigned short int) num.ul;
1557 else if (!(flags & CHAR))
1558 *ARG (unsigned int *) = (unsigned int) num.ul;
1559 else
1560 *ARG (unsigned char *) = (unsigned char) num.ul;
1562 else
1564 if (need_longlong && (flags & LONGDBL))
1565 *ARG (LONGLONG int *) = num.q;
1566 else if (need_long && (flags & LONG))
1567 *ARG (long int *) = num.l;
1568 else if (flags & SHORT)
1569 *ARG (short int *) = (short int) num.l;
1570 else if (!(flags & CHAR))
1571 *ARG (int *) = (int) num.l;
1572 else
1573 *ARG (signed char *) = (signed char) num.ul;
1575 ++done;
1577 break;
1579 case L_('e'): /* Floating-point numbers. */
1580 case L_('E'):
1581 case L_('f'):
1582 case L_('F'):
1583 case L_('g'):
1584 case L_('G'):
1585 case L_('a'):
1586 case L_('A'):
1587 c = inchar ();
1588 if (__builtin_expect (c == EOF, 0))
1589 input_error ();
1591 got_dot = got_e = 0;
1593 /* Check for a sign. */
1594 if (c == L_('-') || c == L_('+'))
1596 negative = c == L_('-');
1597 if (__builtin_expect (width == 0 || inchar () == EOF, 0))
1598 /* EOF is only an input error before we read any chars. */
1599 conv_error ();
1600 if (width > 0)
1601 --width;
1603 else
1604 negative = 0;
1606 /* Take care for the special arguments "nan" and "inf". */
1607 if (TOLOWER (c) == L_('n'))
1609 /* Maybe "nan". */
1610 ADDW (c);
1611 if (__builtin_expect (width == 0
1612 || inchar () == EOF
1613 || TOLOWER (c) != L_('a'), 0))
1614 conv_error ();
1615 if (width > 0)
1616 --width;
1617 ADDW (c);
1618 if (__builtin_expect (width == 0
1619 || inchar () == EOF
1620 || TOLOWER (c) != L_('n'), 0))
1621 conv_error ();
1622 if (width > 0)
1623 --width;
1624 ADDW (c);
1625 /* It is "nan". */
1626 goto scan_float;
1628 else if (TOLOWER (c) == L_('i'))
1630 /* Maybe "inf" or "infinity". */
1631 ADDW (c);
1632 if (__builtin_expect (width == 0
1633 || inchar () == EOF
1634 || TOLOWER (c) != L_('n'), 0))
1635 conv_error ();
1636 if (width > 0)
1637 --width;
1638 ADDW (c);
1639 if (__builtin_expect (width == 0
1640 || inchar () == EOF
1641 || TOLOWER (c) != L_('f'), 0))
1642 conv_error ();
1643 if (width > 0)
1644 --width;
1645 ADDW (c);
1646 /* It is as least "inf". */
1647 if (width != 0 && inchar () != EOF)
1649 if (TOLOWER (c) == L_('i'))
1651 if (width > 0)
1652 --width;
1653 /* Now we have to read the rest as well. */
1654 ADDW (c);
1655 if (__builtin_expect (width == 0
1656 || inchar () == EOF
1657 || TOLOWER (c) != L_('n'), 0))
1658 conv_error ();
1659 if (width > 0)
1660 --width;
1661 ADDW (c);
1662 if (__builtin_expect (width == 0
1663 || inchar () == EOF
1664 || TOLOWER (c) != L_('i'), 0))
1665 conv_error ();
1666 if (width > 0)
1667 --width;
1668 ADDW (c);
1669 if (__builtin_expect (width == 0
1670 || inchar () == EOF
1671 || TOLOWER (c) != L_('t'), 0))
1672 conv_error ();
1673 if (width > 0)
1674 --width;
1675 ADDW (c);
1676 if (__builtin_expect (width == 0
1677 || inchar () == EOF
1678 || TOLOWER (c) != L_('y'), 0))
1679 conv_error ();
1680 if (width > 0)
1681 --width;
1682 ADDW (c);
1684 else
1685 /* Never mind. */
1686 ungetc (c, s);
1688 goto scan_float;
1691 is_hexa = 0;
1692 exp_char = L_('e');
1693 if (width != 0 && c == L_('0'))
1695 ADDW (c);
1696 c = inchar ();
1697 if (width > 0)
1698 --width;
1699 if (width != 0 && TOLOWER (c) == L_('x'))
1701 /* It is a number in hexadecimal format. */
1702 ADDW (c);
1704 is_hexa = 1;
1705 exp_char = L_('p');
1707 /* Grouping is not allowed. */
1708 flags &= ~GROUP;
1709 c = inchar ();
1710 if (width > 0)
1711 --width;
1717 if (ISDIGIT (c))
1718 ADDW (c);
1719 else if (!got_e && is_hexa && ISXDIGIT (c))
1720 ADDW (c);
1721 else if (got_e && wp[wpsize - 1] == exp_char
1722 && (c == L_('-') || c == L_('+')))
1723 ADDW (c);
1724 else if (wpsize > 0 && !got_e
1725 && (CHAR_T) TOLOWER (c) == exp_char)
1727 ADDW (exp_char);
1728 got_e = got_dot = 1;
1730 else
1732 #ifdef COMPILE_WSCANF
1733 if (! got_dot && c == decimal)
1735 ADDW (c);
1736 got_dot = 1;
1738 else if ((flags & GROUP) != 0 && thousands != L'\0'
1739 && ! got_dot && c == thousands)
1740 ADDW (c);
1741 else
1743 /* The last read character is not part of the number
1744 anymore. */
1745 ungetc (c, s);
1746 break;
1748 #else
1749 const char *cmpp = decimal;
1750 int avail = width > 0 ? width : INT_MAX;
1752 if (! got_dot)
1754 while ((unsigned char) *cmpp == c && avail > 0)
1755 if (*++cmpp == '\0')
1756 break;
1757 else
1759 if (inchar () == EOF)
1760 break;
1761 --avail;
1765 if (*cmpp == '\0')
1767 /* Add all the characters. */
1768 for (cmpp = decimal; *cmpp != '\0'; ++cmpp)
1769 ADDW ((unsigned char) *cmpp);
1770 if (width > 0)
1771 width = avail;
1772 got_dot = 1;
1774 else
1776 /* Figure out whether it is a thousands separator.
1777 There is one problem: we possibly read more than
1778 one character. We cannot push them back but since
1779 we know that parts of the `decimal' string matched,
1780 we can compare against it. */
1781 const char *cmp2p = thousands;
1783 if ((flags & GROUP) != 0 && thousands != NULL
1784 && ! got_dot)
1786 while (cmp2p - thousands < cmpp - decimal
1787 && *cmp2p == decimal[cmp2p - thousands])
1788 ++cmp2p;
1789 if (cmp2p - thousands == cmpp - decimal)
1791 while ((unsigned char) *cmp2p == c && avail > 0)
1792 if (*++cmp2p == '\0')
1793 break;
1794 else
1796 if (inchar () == EOF)
1797 break;
1798 --avail;
1803 if (cmp2p != NULL && *cmp2p == '\0')
1805 /* Add all the characters. */
1806 for (cmpp = thousands; *cmpp != '\0'; ++cmpp)
1807 ADDW ((unsigned char) *cmpp);
1808 if (width > 0)
1809 width = avail;
1811 else
1813 /* The last read character is not part of the number
1814 anymore. */
1815 ungetc (c, s);
1816 break;
1819 #endif
1821 if (width > 0)
1822 --width;
1824 while (width != 0 && inchar () != EOF);
1826 /* Have we read any character? If we try to read a number
1827 in hexadecimal notation and we have read only the `0x'
1828 prefix or no exponent this is an error. */
1829 if (__builtin_expect (wpsize == 0
1830 || (is_hexa && (wpsize == 2 || ! got_e)), 0))
1831 conv_error ();
1833 scan_float:
1834 /* Convert the number. */
1835 ADDW (L_('\0'));
1836 if ((flags & LONGDBL) && !__ldbl_is_dbl)
1838 long double d = __strtold_internal (wp, &tw, flags & GROUP);
1839 if (!(flags & SUPPRESS) && tw != wp)
1840 *ARG (long double *) = negative ? -d : d;
1842 else if (flags & (LONG | LONGDBL))
1844 double d = __strtod_internal (wp, &tw, flags & GROUP);
1845 if (!(flags & SUPPRESS) && tw != wp)
1846 *ARG (double *) = negative ? -d : d;
1848 else
1850 float d = __strtof_internal (wp, &tw, flags & GROUP);
1851 if (!(flags & SUPPRESS) && tw != wp)
1852 *ARG (float *) = negative ? -d : d;
1855 if (__builtin_expect (tw == wp, 0))
1856 conv_error ();
1858 if (!(flags & SUPPRESS))
1859 ++done;
1860 break;
1862 case L_('['): /* Character class. */
1863 if (flags & LONG)
1864 STRING_ARG (wstr, wchar_t);
1865 else
1866 STRING_ARG (str, char);
1868 if (*f == L_('^'))
1870 ++f;
1871 not_in = 1;
1873 else
1874 not_in = 0;
1876 if (width < 0)
1877 /* There is no width given so there is also no limit on the
1878 number of characters we read. Therefore we set width to
1879 a very high value to make the algorithm easier. */
1880 width = INT_MAX;
1882 #ifdef COMPILE_WSCANF
1883 /* Find the beginning and the end of the scanlist. We are not
1884 creating a lookup table since it would have to be too large.
1885 Instead we search each time through the string. This is not
1886 a constant lookup time but who uses this feature deserves to
1887 be punished. */
1888 tw = (wchar_t *) f; /* Marks the beginning. */
1890 if (*f == L']')
1891 ++f;
1893 while ((fc = *f++) != L'\0' && fc != L']');
1895 if (__builtin_expect (fc == L'\0', 0))
1896 conv_error ();
1897 wp = (wchar_t *) f - 1;
1898 #else
1899 /* Fill WP with byte flags indexed by character.
1900 We will use this flag map for matching input characters. */
1901 if (wpmax < UCHAR_MAX + 1)
1903 wpmax = UCHAR_MAX + 1;
1904 wp = (char *) alloca (wpmax);
1906 memset (wp, '\0', UCHAR_MAX + 1);
1908 fc = *f;
1909 if (fc == ']' || fc == '-')
1911 /* If ] or - appears before any char in the set, it is not
1912 the terminator or separator, but the first char in the
1913 set. */
1914 wp[fc] = 1;
1915 ++f;
1918 while ((fc = *f++) != '\0' && fc != ']')
1919 if (fc == '-' && *f != '\0' && *f != ']'
1920 && (unsigned char) f[-2] <= (unsigned char) *f)
1922 /* Add all characters from the one before the '-'
1923 up to (but not including) the next format char. */
1924 for (fc = (unsigned char) f[-2]; fc < (unsigned char) *f; ++fc)
1925 wp[fc] = 1;
1927 else
1928 /* Add the character to the flag map. */
1929 wp[fc] = 1;
1931 if (__builtin_expect (fc == '\0', 0))
1932 conv_error();
1933 #endif
1935 if (flags & LONG)
1937 size_t now = read_in;
1938 #ifdef COMPILE_WSCANF
1939 if (__builtin_expect (inchar () == WEOF, 0))
1940 input_error ();
1944 wchar_t *runp;
1946 /* Test whether it's in the scanlist. */
1947 runp = tw;
1948 while (runp < wp)
1950 if (runp[0] == L'-' && runp[1] != '\0' && runp + 1 != wp
1951 && runp != tw
1952 && (unsigned int) runp[-1] <= (unsigned int) runp[1])
1954 /* Match against all characters in between the
1955 first and last character of the sequence. */
1956 wchar_t wc;
1958 for (wc = runp[-1] + 1; wc <= runp[1]; ++wc)
1959 if ((wint_t) wc == c)
1960 break;
1962 if (wc <= runp[1] && !not_in)
1963 break;
1964 if (wc <= runp[1] && not_in)
1966 /* The current character is not in the
1967 scanset. */
1968 ungetc (c, s);
1969 goto out;
1972 runp += 2;
1974 else
1976 if ((wint_t) *runp == c && !not_in)
1977 break;
1978 if ((wint_t) *runp == c && not_in)
1980 ungetc (c, s);
1981 goto out;
1984 ++runp;
1988 if (runp == wp && !not_in)
1990 ungetc (c, s);
1991 goto out;
1994 if (!(flags & SUPPRESS))
1996 *wstr++ = c;
1998 if ((flags & MALLOC)
1999 && wstr == (wchar_t *) *strptr + strsize)
2001 /* Enlarge the buffer. */
2002 wstr = (wchar_t *) realloc (*strptr,
2003 (2 * strsize)
2004 * sizeof (wchar_t));
2005 if (wstr == NULL)
2007 /* Can't allocate that much. Last-ditch
2008 effort. */
2009 wstr = (wchar_t *)
2010 realloc (*strptr, (strsize + 1)
2011 * sizeof (wchar_t));
2012 if (wstr == NULL)
2014 /* We lose. Oh well. Terminate the string
2015 and stop converting, so at least we don't
2016 skip any input. */
2017 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
2018 ++done;
2019 conv_error ();
2021 else
2023 *strptr = (char *) wstr;
2024 wstr += strsize;
2025 ++strsize;
2028 else
2030 *strptr = (char *) wstr;
2031 wstr += strsize;
2032 strsize *= 2;
2037 while (--width > 0 && inchar () != WEOF);
2038 out:
2039 #else
2040 char buf[MB_LEN_MAX];
2041 size_t cnt = 0;
2042 mbstate_t cstate;
2044 if (__builtin_expect (inchar () == EOF, 0))
2045 input_error ();
2047 memset (&cstate, '\0', sizeof (cstate));
2051 if (wp[c] == not_in)
2053 ungetc_not_eof (c, s);
2054 break;
2057 /* This is easy. */
2058 if (!(flags & SUPPRESS))
2060 size_t n;
2062 /* Convert it into a wide character. */
2063 buf[0] = c;
2064 n = __mbrtowc (wstr, buf, 1, &cstate);
2066 if (n == (size_t) -2)
2068 /* Possibly correct character, just not enough
2069 input. */
2070 ++cnt;
2071 assert (cnt < MB_CUR_MAX);
2072 continue;
2074 cnt = 0;
2076 ++wstr;
2077 if ((flags & MALLOC)
2078 && wstr == (wchar_t *) *strptr + strsize)
2080 /* Enlarge the buffer. */
2081 wstr = (wchar_t *) realloc (*strptr,
2082 (2 * strsize
2083 * sizeof (wchar_t)));
2084 if (wstr == NULL)
2086 /* Can't allocate that much. Last-ditch
2087 effort. */
2088 wstr = (wchar_t *)
2089 realloc (*strptr, ((strsize + 1)
2090 * sizeof (wchar_t)));
2091 if (wstr == NULL)
2093 /* We lose. Oh well. Terminate the
2094 string and stop converting,
2095 so at least we don't skip any input. */
2096 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
2097 ++done;
2098 conv_error ();
2100 else
2102 *strptr = (char *) wstr;
2103 wstr += strsize;
2104 ++strsize;
2107 else
2109 *strptr = (char *) wstr;
2110 wstr += strsize;
2111 strsize *= 2;
2116 if (--width <= 0)
2117 break;
2119 while (inchar () != EOF);
2121 if (__builtin_expect (cnt != 0, 0))
2122 /* We stopped in the middle of recognizing another
2123 character. That's a problem. */
2124 encode_error ();
2125 #endif
2127 if (__builtin_expect (now == read_in, 0))
2128 /* We haven't succesfully read any character. */
2129 conv_error ();
2131 if (!(flags & SUPPRESS))
2133 *wstr++ = L'\0';
2135 if ((flags & MALLOC)
2136 && wstr - (wchar_t *) *strptr != strsize)
2138 wchar_t *cp = (wchar_t *)
2139 realloc (*strptr, ((wstr - (wchar_t *) *strptr)
2140 * sizeof(wchar_t)));
2141 if (cp != NULL)
2142 *strptr = (char *) cp;
2145 ++done;
2148 else
2150 size_t now = read_in;
2152 if (__builtin_expect (inchar () == EOF, 0))
2153 input_error ();
2155 #ifdef COMPILE_WSCANF
2157 memset (&state, '\0', sizeof (state));
2161 wchar_t *runp;
2162 size_t n;
2164 /* Test whether it's in the scanlist. */
2165 runp = tw;
2166 while (runp < wp)
2168 if (runp[0] == L'-' && runp[1] != '\0' && runp + 1 != wp
2169 && runp != tw
2170 && (unsigned int) runp[-1] <= (unsigned int) runp[1])
2172 /* Match against all characters in between the
2173 first and last character of the sequence. */
2174 wchar_t wc;
2176 for (wc = runp[-1] + 1; wc <= runp[1]; ++wc)
2177 if ((wint_t) wc == c)
2178 break;
2180 if (wc <= runp[1] && !not_in)
2181 break;
2182 if (wc <= runp[1] && not_in)
2184 /* The current character is not in the
2185 scanset. */
2186 ungetc (c, s);
2187 goto out2;
2190 runp += 2;
2192 else
2194 if ((wint_t) *runp == c && !not_in)
2195 break;
2196 if ((wint_t) *runp == c && not_in)
2198 ungetc (c, s);
2199 goto out2;
2202 ++runp;
2206 if (runp == wp && !not_in)
2208 ungetc (c, s);
2209 goto out2;
2212 if (!(flags & SUPPRESS))
2214 if ((flags & MALLOC)
2215 && str + MB_CUR_MAX >= *strptr + strsize)
2217 /* Enlarge the buffer. */
2218 size_t strleng = str - *strptr;
2219 char *newstr;
2221 newstr = (char *) realloc (*strptr, 2 * strsize);
2222 if (newstr == NULL)
2224 /* Can't allocate that much. Last-ditch
2225 effort. */
2226 newstr = (char *) realloc (*strptr,
2227 strleng + MB_CUR_MAX);
2228 if (newstr == NULL)
2230 /* We lose. Oh well. Terminate the string
2231 and stop converting, so at least we don't
2232 skip any input. */
2233 ((char *) (*strptr))[strleng] = '\0';
2234 ++done;
2235 conv_error ();
2237 else
2239 *strptr = newstr;
2240 str = newstr + strleng;
2241 strsize = strleng + MB_CUR_MAX;
2244 else
2246 *strptr = newstr;
2247 str = newstr + strleng;
2248 strsize *= 2;
2253 n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c, &state);
2254 if (__builtin_expect (n == (size_t) -1, 0))
2255 encode_error ();
2257 assert (n <= MB_CUR_MAX);
2258 str += n;
2260 while (--width > 0 && inchar () != WEOF);
2261 out2:
2262 #else
2265 if (wp[c] == not_in)
2267 ungetc_not_eof (c, s);
2268 break;
2271 /* This is easy. */
2272 if (!(flags & SUPPRESS))
2274 *str++ = c;
2275 if ((flags & MALLOC)
2276 && (char *) str == *strptr + strsize)
2278 /* Enlarge the buffer. */
2279 size_t newsize = 2 * strsize;
2281 allocagain:
2282 str = (char *) realloc (*strptr, newsize);
2283 if (str == NULL)
2285 /* Can't allocate that much. Last-ditch
2286 effort. */
2287 if (newsize > strsize + 1)
2289 newsize = strsize + 1;
2290 goto allocagain;
2292 /* We lose. Oh well. Terminate the
2293 string and stop converting,
2294 so at least we don't skip any input. */
2295 ((char *) (*strptr))[strsize - 1] = '\0';
2296 ++done;
2297 conv_error ();
2299 else
2301 *strptr = (char *) str;
2302 str += strsize;
2303 strsize = newsize;
2308 while (--width > 0 && inchar () != EOF);
2309 #endif
2311 if (__builtin_expect (now == read_in, 0))
2312 /* We haven't succesfully read any character. */
2313 conv_error ();
2315 if (!(flags & SUPPRESS))
2317 #ifdef COMPILE_WSCANF
2318 /* We have to emit the code to get into the initial
2319 state. */
2320 char buf[MB_LEN_MAX];
2321 size_t n = __wcrtomb (buf, L'\0', &state);
2322 if (n > 0 && (flags & MALLOC)
2323 && str + n >= *strptr + strsize)
2325 /* Enlarge the buffer. */
2326 size_t strleng = str - *strptr;
2327 char *newstr;
2329 newstr = (char *) realloc (*strptr, strleng + n + 1);
2330 if (newstr == NULL)
2332 /* We lose. Oh well. Terminate the string
2333 and stop converting, so at least we don't
2334 skip any input. */
2335 ((char *) (*strptr))[strleng] = '\0';
2336 ++done;
2337 conv_error ();
2339 else
2341 *strptr = newstr;
2342 str = newstr + strleng;
2343 strsize = strleng + n + 1;
2347 str = __mempcpy (str, buf, n);
2348 #endif
2349 *str++ = '\0';
2351 if ((flags & MALLOC) && str - *strptr != strsize)
2353 char *cp = (char *) realloc (*strptr, str - *strptr);
2354 if (cp != NULL)
2355 *strptr = cp;
2358 ++done;
2361 break;
2363 case L_('p'): /* Generic pointer. */
2364 base = 16;
2365 /* A PTR must be the same size as a `long int'. */
2366 flags &= ~(SHORT|LONGDBL);
2367 if (need_long)
2368 flags |= LONG;
2369 number_signed = 0;
2370 read_pointer = 1;
2371 goto number;
2373 default:
2374 /* If this is an unknown format character punt. */
2375 conv_error ();
2379 /* The last thing we saw int the format string was a white space.
2380 Consume the last white spaces. */
2381 if (skip_space)
2384 c = inchar ();
2385 while (ISSPACE (c));
2386 ungetc (c, s);
2389 errout:
2390 /* Unlock stream. */
2391 UNLOCK_STREAM (s);
2393 if (errp != NULL)
2394 *errp |= errval;
2396 return done;
2399 #ifdef COMPILE_WSCANF
2401 __vfwscanf (FILE *s, const wchar_t *format, va_list argptr)
2403 return _IO_vfwscanf (s, format, argptr, NULL);
2405 ldbl_weak_alias (__vfwscanf, vfwscanf)
2406 #else
2408 ___vfscanf (FILE *s, const char *format, va_list argptr)
2410 return _IO_vfscanf_internal (s, format, argptr, NULL);
2412 ldbl_strong_alias (_IO_vfscanf_internal, _IO_vfscanf)
2413 ldbl_strong_alias (___vfscanf, __vfscanf)
2414 ldbl_hidden_def (___vfscanf, __vfscanf)
2415 ldbl_weak_alias (___vfscanf, vfscanf)
2416 #endif