* elf/dl-close.c (free_mem): Free _dl_scope_free_list.
[glibc.git] / stdio-common / vfscanf.c
blobb1469b9a9e19fa56ac33992ba366fed6be9656e4
1 /* Copyright (C) 1991-2006, 2007 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 Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <assert.h>
20 #include <errno.h>
21 #include <limits.h>
22 #include <ctype.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <wchar.h>
29 #include <wctype.h>
30 #include <bits/libc-lock.h>
31 #include <locale/localeinfo.h>
33 #ifdef __GNUC__
34 # define HAVE_LONGLONG
35 # define LONGLONG long long
36 #else
37 # define LONGLONG long
38 #endif
40 /* Determine whether we have to handle `long long' at all. */
41 #if LONG_MAX == LONG_LONG_MAX
42 # define need_longlong 0
43 #else
44 # define need_longlong 1
45 #endif
47 /* Determine whether we have to handle `long'. */
48 #if INT_MAX == LONG_MAX
49 # define need_long 0
50 #else
51 # define need_long 1
52 #endif
54 /* Those are flags in the conversion format. */
55 #define LONG 0x0001 /* l: long or double */
56 #define LONGDBL 0x0002 /* L: long long or long double */
57 #define SHORT 0x0004 /* h: short */
58 #define SUPPRESS 0x0008 /* *: suppress assignment */
59 #define POINTER 0x0010 /* weird %p pointer (`fake hex') */
60 #define NOSKIP 0x0020 /* do not skip blanks */
61 #define NUMBER_SIGNED 0x0040 /* signed integer */
62 #define GROUP 0x0080 /* ': group numbers */
63 #define MALLOC 0x0100 /* a: malloc strings */
64 #define CHAR 0x0200 /* hh: char */
65 #define I18N 0x0400 /* I: use locale's digits */
66 #define HEXA_FLOAT 0x0800 /* hexadecimal float */
67 #define READ_POINTER 0x1000 /* this is a pointer value */
70 #include <locale/localeinfo.h>
71 #include <libioP.h>
72 #include <libio.h>
74 #undef va_list
75 #define va_list _IO_va_list
77 #ifdef COMPILE_WSCANF
78 # define ungetc(c, s) ((void) (c == WEOF \
79 || (--read_in, \
80 INTUSE(_IO_sputbackwc) (s, c))))
81 # define ungetc_not_eof(c, s) ((void) (--read_in, \
82 INTUSE(_IO_sputbackwc) (s, c)))
83 # define inchar() (c == WEOF ? ((errno = inchar_errno), WEOF) \
84 : ((c = _IO_getwc_unlocked (s)), \
85 (void) (c != WEOF \
86 ? ++read_in \
87 : (size_t) (inchar_errno = errno)), c))
89 # define MEMCPY(d, s, n) __wmemcpy (d, s, n)
90 # define ISSPACE(Ch) iswspace (Ch)
91 # define ISDIGIT(Ch) iswdigit (Ch)
92 # define ISXDIGIT(Ch) iswxdigit (Ch)
93 # define TOLOWER(Ch) towlower (Ch)
94 # define ORIENT if (_IO_fwide (s, 1) != 1) return WEOF
95 # define __strtoll_internal __wcstoll_internal
96 # define __strtoull_internal __wcstoull_internal
97 # define __strtol_internal __wcstol_internal
98 # define __strtoul_internal __wcstoul_internal
99 # define __strtold_internal __wcstold_internal
100 # define __strtod_internal __wcstod_internal
101 # define __strtof_internal __wcstof_internal
103 # define L_(Str) L##Str
104 # define CHAR_T wchar_t
105 # define UCHAR_T unsigned int
106 # define WINT_T wint_t
107 # undef EOF
108 # define EOF WEOF
109 #else
110 # define ungetc(c, s) ((void) ((int) c == EOF \
111 || (--read_in, \
112 INTUSE(_IO_sputbackc) (s, (unsigned char) c))))
113 # define ungetc_not_eof(c, s) ((void) (--read_in, \
114 INTUSE(_IO_sputbackc) (s, (unsigned char) c)))
115 # define inchar() (c == EOF ? ((errno = inchar_errno), EOF) \
116 : ((c = _IO_getc_unlocked (s)), \
117 (void) (c != EOF \
118 ? ++read_in \
119 : (size_t) (inchar_errno = errno)), c))
120 # define MEMCPY(d, s, n) memcpy (d, s, n)
121 # define ISSPACE(Ch) __isspace_l (Ch, loc)
122 # define ISDIGIT(Ch) __isdigit_l (Ch, loc)
123 # define ISXDIGIT(Ch) __isxdigit_l (Ch, loc)
124 # define TOLOWER(Ch) __tolower_l ((unsigned char) (Ch), loc)
125 # define ORIENT if (_IO_vtable_offset (s) == 0 \
126 && _IO_fwide (s, -1) != -1) \
127 return EOF
129 # define L_(Str) Str
130 # define CHAR_T char
131 # define UCHAR_T unsigned char
132 # define WINT_T int
133 #endif
135 #define encode_error() do { \
136 errval = 4; \
137 __set_errno (EILSEQ); \
138 goto errout; \
139 } while (0)
140 #define conv_error() do { \
141 errval = 2; \
142 goto errout; \
143 } while (0)
144 #define input_error() do { \
145 errval = 1; \
146 if (done == 0) done = EOF; \
147 goto errout; \
148 } while (0)
149 #define ARGCHECK(s, format) \
150 do \
152 /* Check file argument for consistence. */ \
153 CHECK_FILE (s, EOF); \
154 if (s->_flags & _IO_NO_READS) \
156 __set_errno (EBADF); \
157 return EOF; \
159 else if (format == NULL) \
161 MAYBE_SET_EINVAL; \
162 return EOF; \
164 } while (0)
165 #define LOCK_STREAM(S) \
166 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, (S)); \
167 _IO_flockfile (S)
168 #define UNLOCK_STREAM(S) \
169 _IO_funlockfile (S); \
170 __libc_cleanup_region_end (0)
173 /* Read formatted input from S according to the format string
174 FORMAT, using the argument list in ARG.
175 Return the number of assignments made, or -1 for an input error. */
176 #ifdef COMPILE_WSCANF
178 _IO_vfwscanf (_IO_FILE *s, const wchar_t *format, _IO_va_list argptr,
179 int *errp)
180 #else
182 _IO_vfscanf_internal (_IO_FILE *s, const char *format, _IO_va_list argptr,
183 int *errp)
184 #endif
186 va_list arg;
187 register const CHAR_T *f = format;
188 register UCHAR_T fc; /* Current character of the format. */
189 register WINT_T done = 0; /* Assignments done. */
190 register size_t read_in = 0; /* Chars read in. */
191 register WINT_T c = 0; /* Last char read. */
192 register int width; /* Maximum field width. */
193 register int flags; /* Modifiers for current format element. */
194 int errval = 0;
195 #ifndef COMPILE_WSCANF
196 __locale_t loc = _NL_CURRENT_LOCALE;
197 struct locale_data *const curctype = loc->__locales[LC_CTYPE];
198 #endif
200 /* Errno of last failed inchar call. */
201 int inchar_errno = 0;
202 /* Status for reading F-P nums. */
203 char got_dot, got_e, negative;
204 /* If a [...] is a [^...]. */
205 CHAR_T not_in;
206 #define exp_char not_in
207 /* Base for integral numbers. */
208 int base;
209 /* Decimal point character. */
210 #ifdef COMPILE_WSCANF
211 wint_t decimal;
212 #else
213 const char *decimal;
214 #endif
215 /* The thousands character of the current locale. */
216 #ifdef COMPILE_WSCANF
217 wint_t thousands;
218 #else
219 const char *thousands;
220 #endif
221 /* State for the conversions. */
222 mbstate_t state;
223 /* Integral holding variables. */
224 union
226 long long int q;
227 unsigned long long int uq;
228 long int l;
229 unsigned long int ul;
230 } num;
231 /* Character-buffer pointer. */
232 char *str = NULL;
233 wchar_t *wstr = NULL;
234 char **strptr = NULL;
235 ssize_t strsize = 0;
236 /* We must not react on white spaces immediately because they can
237 possibly be matched even if in the input stream no character is
238 available anymore. */
239 int skip_space = 0;
240 /* Workspace. */
241 CHAR_T *tw; /* Temporary pointer. */
242 CHAR_T *wp = NULL; /* Workspace. */
243 size_t wpmax = 0; /* Maximal size of workspace. */
244 size_t wpsize; /* Currently used bytes in workspace. */
245 #define ADDW(Ch) \
246 do \
248 if (wpsize == wpmax) \
250 CHAR_T *old = wp; \
251 wpmax = (UCHAR_MAX + 1 > 2 * wpmax ? UCHAR_MAX + 1 : 2 * wpmax); \
252 wp = (CHAR_T *) alloca (wpmax * sizeof (wchar_t)); \
253 if (old != NULL) \
254 MEMCPY (wp, old, wpsize); \
256 wp[wpsize++] = (Ch); \
258 while (0)
260 #ifdef __va_copy
261 __va_copy (arg, argptr);
262 #else
263 arg = (va_list) argptr;
264 #endif
266 #ifdef ORIENT
267 ORIENT;
268 #endif
270 ARGCHECK (s, format);
273 #ifndef COMPILE_WSCANF
274 struct locale_data *const curnumeric = loc->__locales[LC_NUMERIC];
275 #endif
277 /* Figure out the decimal point character. */
278 #ifdef COMPILE_WSCANF
279 decimal = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
280 #else
281 decimal = curnumeric->values[_NL_ITEM_INDEX (DECIMAL_POINT)].string;
282 #endif
283 /* Figure out the thousands separator character. */
284 #ifdef COMPILE_WSCANF
285 thousands = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_THOUSANDS_SEP_WC);
286 #else
287 thousands = curnumeric->values[_NL_ITEM_INDEX (THOUSANDS_SEP)].string;
288 if (*thousands == '\0')
289 thousands = NULL;
290 #endif
293 /* Lock the stream. */
294 LOCK_STREAM (s);
297 #ifndef COMPILE_WSCANF
298 /* From now on we use `state' to convert the format string. */
299 memset (&state, '\0', sizeof (state));
300 #endif
302 /* Run through the format string. */
303 while (*f != '\0')
305 unsigned int argpos;
306 /* Extract the next argument, which is of type TYPE.
307 For a %N$... spec, this is the Nth argument from the beginning;
308 otherwise it is the next argument after the state now in ARG. */
309 #ifdef __va_copy
310 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
311 ({ unsigned int pos = argpos; \
312 va_list arg; \
313 __va_copy (arg, argptr); \
314 while (--pos > 0) \
315 (void) va_arg (arg, void *); \
316 va_arg (arg, type); \
318 #else
319 # if 0
320 /* XXX Possible optimization. */
321 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
322 ({ va_list arg = (va_list) argptr; \
323 arg = (va_list) ((char *) arg \
324 + (argpos - 1) \
325 * __va_rounded_size (void *)); \
326 va_arg (arg, type); \
328 # else
329 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
330 ({ unsigned int pos = argpos; \
331 va_list arg = (va_list) argptr; \
332 while (--pos > 0) \
333 (void) va_arg (arg, void *); \
334 va_arg (arg, type); \
336 # endif
337 #endif
339 #ifndef COMPILE_WSCANF
340 if (!isascii ((unsigned char) *f))
342 /* Non-ASCII, may be a multibyte. */
343 int len = __mbrlen (f, strlen (f), &state);
344 if (len > 0)
348 c = inchar ();
349 if (__builtin_expect (c == EOF, 0))
350 input_error ();
351 else if (c != (unsigned char) *f++)
353 ungetc_not_eof (c, s);
354 conv_error ();
357 while (--len > 0);
358 continue;
361 #endif
363 fc = *f++;
364 if (fc != '%')
366 /* Remember to skip spaces. */
367 if (ISSPACE (fc))
369 skip_space = 1;
370 continue;
373 /* Read a character. */
374 c = inchar ();
376 /* Characters other than format specs must just match. */
377 if (__builtin_expect (c == EOF, 0))
378 input_error ();
380 /* We saw white space char as the last character in the format
381 string. Now it's time to skip all leading white space. */
382 if (skip_space)
384 while (ISSPACE (c))
385 if (__builtin_expect (inchar () == EOF, 0))
386 input_error ();
387 skip_space = 0;
390 if (__builtin_expect (c != fc, 0))
392 ungetc (c, s);
393 conv_error ();
396 continue;
399 /* This is the start of the conversion string. */
400 flags = 0;
402 /* Initialize state of modifiers. */
403 argpos = 0;
405 /* Prepare temporary buffer. */
406 wpsize = 0;
408 /* Check for a positional parameter specification. */
409 if (ISDIGIT ((UCHAR_T) *f))
411 argpos = (UCHAR_T) *f++ - L_('0');
412 while (ISDIGIT ((UCHAR_T) *f))
413 argpos = argpos * 10 + ((UCHAR_T) *f++ - L_('0'));
414 if (*f == L_('$'))
415 ++f;
416 else
418 /* Oops; that was actually the field width. */
419 width = argpos;
420 argpos = 0;
421 goto got_width;
425 /* Check for the assignment-suppressing, the number grouping flag,
426 and the signal to use the locale's digit representation. */
427 while (*f == L_('*') || *f == L_('\'') || *f == L_('I'))
428 switch (*f++)
430 case L_('*'):
431 flags |= SUPPRESS;
432 break;
433 case L_('\''):
434 #ifdef COMPILE_WSCANF
435 if (thousands != L'\0')
436 #else
437 if (thousands != NULL)
438 #endif
439 flags |= GROUP;
440 break;
441 case L_('I'):
442 flags |= I18N;
443 break;
446 /* Find the maximum field width. */
447 width = 0;
448 while (ISDIGIT ((UCHAR_T) *f))
450 width *= 10;
451 width += (UCHAR_T) *f++ - L_('0');
453 got_width:
454 if (width == 0)
455 width = -1;
457 /* Check for type modifiers. */
458 switch (*f++)
460 case L_('h'):
461 /* ints are short ints or chars. */
462 if (*f == L_('h'))
464 ++f;
465 flags |= CHAR;
467 else
468 flags |= SHORT;
469 break;
470 case L_('l'):
471 if (*f == L_('l'))
473 /* A double `l' is equivalent to an `L'. */
474 ++f;
475 flags |= LONGDBL | LONG;
477 else
478 /* ints are long ints. */
479 flags |= LONG;
480 break;
481 case L_('q'):
482 case L_('L'):
483 /* doubles are long doubles, and ints are long long ints. */
484 flags |= LONGDBL | LONG;
485 break;
486 case L_('a'):
487 /* The `a' is used as a flag only if followed by `s', `S' or
488 `['. */
489 if (*f != L_('s') && *f != L_('S') && *f != L_('['))
491 --f;
492 break;
494 /* String conversions (%s, %[) take a `char **'
495 arg and fill it in with a malloc'd pointer. */
496 flags |= MALLOC;
497 break;
498 case L_('z'):
499 if (need_longlong && sizeof (size_t) > sizeof (unsigned long int))
500 flags |= LONGDBL;
501 else if (sizeof (size_t) > sizeof (unsigned int))
502 flags |= LONG;
503 break;
504 case L_('j'):
505 if (need_longlong && sizeof (uintmax_t) > sizeof (unsigned long int))
506 flags |= LONGDBL;
507 else if (sizeof (uintmax_t) > sizeof (unsigned int))
508 flags |= LONG;
509 break;
510 case L_('t'):
511 if (need_longlong && sizeof (ptrdiff_t) > sizeof (long int))
512 flags |= LONGDBL;
513 else if (sizeof (ptrdiff_t) > sizeof (int))
514 flags |= LONG;
515 break;
516 default:
517 /* Not a recognized modifier. Backup. */
518 --f;
519 break;
522 /* End of the format string? */
523 if (__builtin_expect (*f == L_('\0'), 0))
524 conv_error ();
526 /* Find the conversion specifier. */
527 fc = *f++;
528 if (skip_space || (fc != L_('[') && fc != L_('c')
529 && fc != L_('C') && fc != L_('n')))
531 /* Eat whitespace. */
532 int save_errno = errno;
533 errno = 0;
535 if (__builtin_expect (inchar () == EOF && errno == EINTR, 0))
536 input_error ();
537 while (ISSPACE (c));
538 errno = save_errno;
539 ungetc (c, s);
540 skip_space = 0;
543 switch (fc)
545 case L_('%'): /* Must match a literal '%'. */
546 c = inchar ();
547 if (__builtin_expect (c == EOF, 0))
548 input_error ();
549 if (__builtin_expect (c != fc, 0))
551 ungetc_not_eof (c, s);
552 conv_error ();
554 break;
556 case L_('n'): /* Answer number of assignments done. */
557 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
558 with the 'n' conversion specifier. */
559 if (!(flags & SUPPRESS))
561 /* Don't count the read-ahead. */
562 if (need_longlong && (flags & LONGDBL))
563 *ARG (long long int *) = read_in;
564 else if (need_long && (flags & LONG))
565 *ARG (long int *) = read_in;
566 else if (flags & SHORT)
567 *ARG (short int *) = read_in;
568 else if (!(flags & CHAR))
569 *ARG (int *) = read_in;
570 else
571 *ARG (char *) = read_in;
573 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
574 /* We have a severe problem here. The ISO C standard
575 contradicts itself in explaining the effect of the %n
576 format in `scanf'. While in ISO C:1990 and the ISO C
577 Amendement 1:1995 the result is described as
579 Execution of a %n directive does not effect the
580 assignment count returned at the completion of
581 execution of the f(w)scanf function.
583 in ISO C Corrigendum 1:1994 the following was added:
585 Subclause 7.9.6.2
586 Add the following fourth example:
588 #include <stdio.h>
589 int d1, d2, n1, n2, i;
590 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
591 the value 123 is assigned to d1 and the value3 to n1.
592 Because %n can never get an input failure the value
593 of 3 is also assigned to n2. The value of d2 is not
594 affected. The value 3 is assigned to i.
596 We go for now with the historically correct code from ISO C,
597 i.e., we don't count the %n assignments. When it ever
598 should proof to be wrong just remove the #ifdef above. */
599 ++done;
600 #endif
602 break;
604 case L_('c'): /* Match characters. */
605 if ((flags & LONG) == 0)
607 if (!(flags & SUPPRESS))
609 str = ARG (char *);
610 if (str == NULL)
611 conv_error ();
614 c = inchar ();
615 if (__builtin_expect (c == EOF, 0))
616 input_error ();
618 if (width == -1)
619 width = 1;
621 #ifdef COMPILE_WSCANF
622 /* We have to convert the wide character(s) into multibyte
623 characters and store the result. */
624 memset (&state, '\0', sizeof (state));
628 size_t n;
630 n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c, &state);
631 if (__builtin_expect (n == (size_t) -1, 0))
632 /* No valid wide character. */
633 input_error ();
635 /* Increment the output pointer. Even if we don't
636 write anything. */
637 str += n;
639 while (--width > 0 && inchar () != EOF);
640 #else
641 if (!(flags & SUPPRESS))
644 *str++ = c;
645 while (--width > 0 && inchar () != EOF);
647 else
648 while (--width > 0 && inchar () != EOF);
649 #endif
651 if (!(flags & SUPPRESS))
652 ++done;
654 break;
656 /* FALLTHROUGH */
657 case L_('C'):
658 if (!(flags & SUPPRESS))
660 wstr = ARG (wchar_t *);
661 if (wstr == NULL)
662 conv_error ();
665 c = inchar ();
666 if (__builtin_expect (c == EOF, 0))
667 input_error ();
669 #ifdef COMPILE_WSCANF
670 /* Just store the incoming wide characters. */
671 if (!(flags & SUPPRESS))
674 *wstr++ = c;
675 while (--width > 0 && inchar () != EOF);
677 else
678 while (--width > 0 && inchar () != EOF);
679 #else
681 /* We have to convert the multibyte input sequence to wide
682 characters. */
683 char buf[1];
684 mbstate_t cstate;
686 memset (&cstate, '\0', sizeof (cstate));
690 /* This is what we present the mbrtowc function first. */
691 buf[0] = c;
693 while (1)
695 size_t n;
697 n = __mbrtowc (!(flags & SUPPRESS) ? wstr : NULL,
698 buf, 1, &cstate);
700 if (n == (size_t) -2)
702 /* Possibly correct character, just not enough
703 input. */
704 if (__builtin_expect (inchar () == EOF, 0))
705 encode_error ();
707 buf[0] = c;
708 continue;
711 if (__builtin_expect (n != 1, 0))
712 encode_error ();
714 /* We have a match. */
715 break;
718 /* Advance the result pointer. */
719 ++wstr;
721 while (--width > 0 && inchar () != EOF);
723 #endif
725 if (!(flags & SUPPRESS))
726 ++done;
728 break;
730 case L_('s'): /* Read a string. */
731 if (!(flags & LONG))
733 #define STRING_ARG(Str, Type) \
734 do if (!(flags & SUPPRESS)) \
736 if (flags & MALLOC) \
738 /* The string is to be stored in a malloc'd buffer. */ \
739 strptr = ARG (char **); \
740 if (strptr == NULL) \
741 conv_error (); \
742 /* Allocate an initial buffer. */ \
743 strsize = 100; \
744 *strptr = (char *) malloc (strsize * sizeof (Type)); \
745 Str = (Type *) *strptr; \
747 else \
748 Str = ARG (Type *); \
749 if (Str == NULL) \
750 conv_error (); \
751 } while (0)
752 STRING_ARG (str, char);
754 c = inchar ();
755 if (__builtin_expect (c == EOF, 0))
756 input_error ();
758 #ifdef COMPILE_WSCANF
759 memset (&state, '\0', sizeof (state));
760 #endif
764 if (ISSPACE (c))
766 ungetc_not_eof (c, s);
767 break;
770 #ifdef COMPILE_WSCANF
771 /* This is quite complicated. We have to convert the
772 wide characters into multibyte characters and then
773 store them. */
775 size_t n;
777 if (!(flags & SUPPRESS) && (flags & MALLOC)
778 && str + MB_CUR_MAX >= *strptr + strsize)
780 /* We have to enlarge the buffer if the `a' flag
781 was given. */
782 size_t strleng = str - *strptr;
783 char *newstr;
785 newstr = (char *) realloc (*strptr, strsize * 2);
786 if (newstr == NULL)
788 /* Can't allocate that much. Last-ditch
789 effort. */
790 newstr = (char *) realloc (*strptr,
791 strleng + MB_CUR_MAX);
792 if (newstr == NULL)
794 /* We lose. Oh well. Terminate the
795 string and stop converting,
796 so at least we don't skip any input. */
797 ((char *) (*strptr))[strleng] = '\0';
798 ++done;
799 conv_error ();
801 else
803 *strptr = newstr;
804 str = newstr + strleng;
805 strsize = strleng + MB_CUR_MAX;
808 else
810 *strptr = newstr;
811 str = newstr + strleng;
812 strsize *= 2;
816 n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c,
817 &state);
818 if (__builtin_expect (n == (size_t) -1, 0))
819 encode_error ();
821 assert (n <= MB_CUR_MAX);
822 str += n;
824 #else
825 /* This is easy. */
826 if (!(flags & SUPPRESS))
828 *str++ = c;
829 if ((flags & MALLOC)
830 && (char *) str == *strptr + strsize)
832 /* Enlarge the buffer. */
833 str = (char *) realloc (*strptr, 2 * strsize);
834 if (str == NULL)
836 /* Can't allocate that much. Last-ditch
837 effort. */
838 str = (char *) realloc (*strptr, strsize + 1);
839 if (str == NULL)
841 /* We lose. Oh well. Terminate the
842 string and stop converting,
843 so at least we don't skip any input. */
844 ((char *) (*strptr))[strsize - 1] = '\0';
845 ++done;
846 conv_error ();
848 else
850 *strptr = (char *) str;
851 str += strsize;
852 ++strsize;
855 else
857 *strptr = (char *) str;
858 str += strsize;
859 strsize *= 2;
863 #endif
865 while ((width <= 0 || --width > 0) && inchar () != EOF);
867 if (!(flags & SUPPRESS))
869 #ifdef COMPILE_WSCANF
870 /* We have to emit the code to get into the initial
871 state. */
872 char buf[MB_LEN_MAX];
873 size_t n = __wcrtomb (buf, L'\0', &state);
874 if (n > 0 && (flags & MALLOC)
875 && str + n >= *strptr + strsize)
877 /* Enlarge the buffer. */
878 size_t strleng = str - *strptr;
879 char *newstr;
881 newstr = (char *) realloc (*strptr, strleng + n + 1);
882 if (newstr == NULL)
884 /* We lose. Oh well. Terminate the string
885 and stop converting, so at least we don't
886 skip any input. */
887 ((char *) (*strptr))[strleng] = '\0';
888 ++done;
889 conv_error ();
891 else
893 *strptr = newstr;
894 str = newstr + strleng;
895 strsize = strleng + n + 1;
899 str = __mempcpy (str, buf, n);
900 #endif
901 *str++ = '\0';
903 if ((flags & MALLOC) && str - *strptr != strsize)
905 char *cp = (char *) realloc (*strptr, str - *strptr);
906 if (cp != NULL)
907 *strptr = cp;
910 ++done;
912 break;
914 /* FALLTHROUGH */
916 case L_('S'):
918 #ifndef COMPILE_WSCANF
919 mbstate_t cstate;
920 #endif
922 /* Wide character string. */
923 STRING_ARG (wstr, wchar_t);
925 c = inchar ();
926 if (__builtin_expect (c == EOF, 0))
927 input_error ();
929 #ifndef COMPILE_WSCANF
930 memset (&cstate, '\0', sizeof (cstate));
931 #endif
935 if (ISSPACE (c))
937 ungetc_not_eof (c, s);
938 break;
941 #ifdef COMPILE_WSCANF
942 /* This is easy. */
943 if (!(flags & SUPPRESS))
945 *wstr++ = c;
946 if ((flags & MALLOC)
947 && wstr == (wchar_t *) *strptr + strsize)
949 /* Enlarge the buffer. */
950 wstr = (wchar_t *) realloc (*strptr,
951 (2 * strsize)
952 * sizeof (wchar_t));
953 if (wstr == NULL)
955 /* Can't allocate that much. Last-ditch
956 effort. */
957 wstr = (wchar_t *) realloc (*strptr,
958 (strsize + 1)
959 * sizeof (wchar_t));
960 if (wstr == NULL)
962 /* We lose. Oh well. Terminate the string
963 and stop converting, so at least we don't
964 skip any input. */
965 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
966 ++done;
967 conv_error ();
969 else
971 *strptr = (char *) wstr;
972 wstr += strsize;
973 ++strsize;
976 else
978 *strptr = (char *) wstr;
979 wstr += strsize;
980 strsize *= 2;
984 #else
986 char buf[1];
988 buf[0] = c;
990 while (1)
992 size_t n;
994 n = __mbrtowc (!(flags & SUPPRESS) ? wstr : NULL,
995 buf, 1, &cstate);
997 if (n == (size_t) -2)
999 /* Possibly correct character, just not enough
1000 input. */
1001 if (__builtin_expect (inchar () == EOF, 0))
1002 encode_error ();
1004 buf[0] = c;
1005 continue;
1008 if (__builtin_expect (n != 1, 0))
1009 encode_error ();
1011 /* We have a match. */
1012 ++wstr;
1013 break;
1016 if (!(flags & SUPPRESS) && (flags & MALLOC)
1017 && wstr == (wchar_t *) *strptr + strsize)
1019 /* Enlarge the buffer. */
1020 wstr = (wchar_t *) realloc (*strptr,
1021 (2 * strsize
1022 * sizeof (wchar_t)));
1023 if (wstr == NULL)
1025 /* Can't allocate that much. Last-ditch effort. */
1026 wstr = (wchar_t *) realloc (*strptr,
1027 ((strsize + 1)
1028 * sizeof (wchar_t)));
1029 if (wstr == NULL)
1031 /* We lose. Oh well. Terminate the
1032 string and stop converting, so at
1033 least we don't skip any input. */
1034 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
1035 ++done;
1036 conv_error ();
1038 else
1040 *strptr = (char *) wstr;
1041 wstr += strsize;
1042 ++strsize;
1045 else
1047 *strptr = (char *) wstr;
1048 wstr += strsize;
1049 strsize *= 2;
1053 #endif
1055 while ((width <= 0 || --width > 0) && inchar () != EOF);
1057 if (!(flags & SUPPRESS))
1059 *wstr++ = L'\0';
1061 if ((flags & MALLOC) && wstr - (wchar_t *) *strptr != strsize)
1063 wchar_t *cp = (wchar_t *) realloc (*strptr,
1064 ((wstr
1065 - (wchar_t *) *strptr)
1066 * sizeof(wchar_t)));
1067 if (cp != NULL)
1068 *strptr = (char *) cp;
1071 ++done;
1074 break;
1076 case L_('x'): /* Hexadecimal integer. */
1077 case L_('X'): /* Ditto. */
1078 base = 16;
1079 goto number;
1081 case L_('o'): /* Octal integer. */
1082 base = 8;
1083 goto number;
1085 case L_('u'): /* Unsigned decimal integer. */
1086 base = 10;
1087 goto number;
1089 case L_('d'): /* Signed decimal integer. */
1090 base = 10;
1091 flags |= NUMBER_SIGNED;
1092 goto number;
1094 case L_('i'): /* Generic number. */
1095 base = 0;
1096 flags |= NUMBER_SIGNED;
1098 number:
1099 c = inchar ();
1100 if (__builtin_expect (c == EOF, 0))
1101 input_error ();
1103 /* Check for a sign. */
1104 if (c == L_('-') || c == L_('+'))
1106 ADDW (c);
1107 if (width > 0)
1108 --width;
1109 c = inchar ();
1112 /* Look for a leading indication of base. */
1113 if (width != 0 && c == L_('0'))
1115 if (width > 0)
1116 --width;
1118 ADDW (c);
1119 c = inchar ();
1121 if (width != 0 && TOLOWER (c) == L_('x'))
1123 if (base == 0)
1124 base = 16;
1125 if (base == 16)
1127 if (width > 0)
1128 --width;
1129 c = inchar ();
1132 else if (base == 0)
1133 base = 8;
1136 if (base == 0)
1137 base = 10;
1139 if (base == 10 && __builtin_expect ((flags & I18N) != 0, 0))
1141 int from_level;
1142 int to_level;
1143 int level;
1144 #ifdef COMPILE_WSCANF
1145 const wchar_t *wcdigits[10];
1146 const wchar_t *wcdigits_extended[10];
1147 #else
1148 const char *mbdigits[10];
1149 const char *mbdigits_extended[10];
1150 #endif
1151 /* "to_inpunct" is a map from ASCII digits to their
1152 equivalent in locale. This is defined for locales
1153 which use an extra digits set. */
1154 wctrans_t map = __wctrans ("to_inpunct");
1155 int n;
1157 from_level = 0;
1158 #ifdef COMPILE_WSCANF
1159 to_level = _NL_CURRENT_WORD (LC_CTYPE,
1160 _NL_CTYPE_INDIGITS_WC_LEN) - 1;
1161 #else
1162 to_level = (uint32_t) curctype->values[_NL_ITEM_INDEX (_NL_CTYPE_INDIGITS_MB_LEN)].word - 1;
1163 #endif
1165 /* Get the alternative digit forms if there are any. */
1166 if (__builtin_expect (map != NULL, 0))
1168 /* Adding new level for extra digits set in locale file. */
1169 ++to_level;
1171 for (n = 0; n < 10; ++n)
1173 #ifdef COMPILE_WSCANF
1174 wcdigits[n] = (const wchar_t *)
1175 _NL_CURRENT (LC_CTYPE, _NL_CTYPE_INDIGITS0_WC + n);
1177 wchar_t *wc_extended = (wchar_t *)
1178 alloca ((to_level + 2) * sizeof (wchar_t));
1179 __wmemcpy (wc_extended, wcdigits[n], to_level);
1180 wc_extended[to_level] = __towctrans (L'0' + n, map);
1181 wc_extended[to_level + 1] = '\0';
1182 wcdigits_extended[n] = wc_extended;
1183 #else
1184 mbdigits[n]
1185 = curctype->values[_NL_CTYPE_INDIGITS0_MB + n].string;
1187 /* Get the equivalent wide char in map. */
1188 wint_t extra_wcdigit = __towctrans (L'0' + n, map);
1190 /* Convert it to multibyte representation. */
1191 mbstate_t state;
1192 memset (&state, '\0', sizeof (state));
1194 char extra_mbdigit[MB_LEN_MAX];
1195 size_t mblen
1196 = __wcrtomb (extra_mbdigit, extra_wcdigit, &state);
1198 if (mblen == (size_t) -1)
1200 /* Ignore this new level. */
1201 map = NULL;
1202 break;
1205 /* Calculate the length of mbdigits[n]. */
1206 const char *last_char = mbdigits[n];
1207 for (level = 0; level < to_level; ++level)
1208 last_char = strchr (last_char, '\0') + 1;
1210 size_t mbdigits_len = last_char - mbdigits[n];
1212 /* Allocate memory for extended multibyte digit. */
1213 char *mb_extended;
1214 mb_extended = (char *) alloca (mbdigits_len + mblen + 1);
1216 /* And get the mbdigits + extra_digit string. */
1217 *(char *) __mempcpy (__mempcpy (mb_extended, mbdigits[n],
1218 mbdigits_len),
1219 extra_mbdigit, mblen) = '\0';
1220 mbdigits_extended[n] = mb_extended;
1221 #endif
1225 /* Read the number into workspace. */
1226 while (c != EOF && width != 0)
1228 /* In this round we get the pointer to the digit strings
1229 and also perform the first round of comparisons. */
1230 for (n = 0; n < 10; ++n)
1232 /* Get the string for the digits with value N. */
1233 #ifdef COMPILE_WSCANF
1234 if (__builtin_expect (map != NULL, 0))
1235 wcdigits[n] = wcdigits_extended[n];
1236 else
1237 wcdigits[n] = (const wchar_t *)
1238 _NL_CURRENT (LC_CTYPE, _NL_CTYPE_INDIGITS0_WC + n);
1239 wcdigits[n] += from_level;
1241 if (c == (wint_t) *wcdigits[n])
1243 to_level = from_level;
1244 break;
1247 /* Advance the pointer to the next string. */
1248 ++wcdigits[n];
1249 #else
1250 const char *cmpp;
1251 int avail = width > 0 ? width : INT_MAX;
1253 if (__builtin_expect (map != NULL, 0))
1254 mbdigits[n] = mbdigits_extended[n];
1255 else
1256 mbdigits[n]
1257 = curctype->values[_NL_CTYPE_INDIGITS0_MB + n].string;
1259 for (level = 0; level < from_level; level++)
1260 mbdigits[n] = strchr (mbdigits[n], '\0') + 1;
1262 cmpp = mbdigits[n];
1263 while ((unsigned char) *cmpp == c && avail >= 0)
1265 if (*++cmpp == '\0')
1266 break;
1267 else
1269 if (avail == 0 || inchar () == EOF)
1270 break;
1271 --avail;
1275 if (*cmpp == '\0')
1277 if (width > 0)
1278 width = avail;
1279 to_level = from_level;
1280 break;
1283 /* We are pushing all read characters back. */
1284 if (cmpp > mbdigits[n])
1286 ungetc (c, s);
1287 while (--cmpp > mbdigits[n])
1288 ungetc_not_eof ((unsigned char) *cmpp, s);
1289 c = (unsigned char) *cmpp;
1292 /* Advance the pointer to the next string. */
1293 mbdigits[n] = strchr (mbdigits[n], '\0') + 1;
1294 #endif
1297 if (n == 10)
1299 /* Have not yet found the digit. */
1300 for (level = from_level + 1; level <= to_level; ++level)
1302 /* Search all ten digits of this level. */
1303 for (n = 0; n < 10; ++n)
1305 #ifdef COMPILE_WSCANF
1306 if (c == (wint_t) *wcdigits[n])
1307 break;
1309 /* Advance the pointer to the next string. */
1310 ++wcdigits[n];
1311 #else
1312 const char *cmpp;
1313 int avail = width > 0 ? width : INT_MAX;
1315 cmpp = mbdigits[n];
1316 while ((unsigned char) *cmpp == c && avail >= 0)
1318 if (*++cmpp == '\0')
1319 break;
1320 else
1322 if (avail == 0 || inchar () == EOF)
1323 break;
1324 --avail;
1328 if (*cmpp == '\0')
1330 if (width > 0)
1331 width = avail;
1332 break;
1335 /* We are pushing all read characters back. */
1336 if (cmpp > mbdigits[n])
1338 ungetc (c, s);
1339 while (--cmpp > mbdigits[n])
1340 ungetc_not_eof ((unsigned char) *cmpp, s);
1341 c = (unsigned char) *cmpp;
1344 /* Advance the pointer to the next string. */
1345 mbdigits[n] = strchr (mbdigits[n], '\0') + 1;
1346 #endif
1349 if (n < 10)
1351 /* Found it. */
1352 from_level = level;
1353 to_level = level;
1354 break;
1359 if (n < 10)
1360 c = L_('0') + n;
1361 else if (flags & GROUP)
1363 /* Try matching against the thousands separator. */
1364 #ifdef COMPILE_WSCANF
1365 if (c != thousands)
1366 break;
1367 #else
1368 const char *cmpp = thousands;
1369 int avail = width > 0 ? width : INT_MAX;
1371 while ((unsigned char) *cmpp == c && avail >= 0)
1373 ADDW (c);
1374 if (*++cmpp == '\0')
1375 break;
1376 else
1378 if (avail == 0 || inchar () == EOF)
1379 break;
1380 --avail;
1384 if (*cmpp != '\0')
1386 /* We are pushing all read characters back. */
1387 if (cmpp > thousands)
1389 wpsize -= cmpp - thousands;
1390 ungetc (c, s);
1391 while (--cmpp > thousands)
1392 ungetc_not_eof ((unsigned char) *cmpp, s);
1393 c = (unsigned char) *cmpp;
1395 break;
1398 if (width > 0)
1399 width = avail;
1401 /* The last thousands character will be added back by
1402 the ADDW below. */
1403 --wpsize;
1404 #endif
1406 else
1407 break;
1409 ADDW (c);
1410 if (width > 0)
1411 --width;
1413 c = inchar ();
1416 else
1417 /* Read the number into workspace. */
1418 while (c != EOF && width != 0)
1420 if (base == 16)
1422 if (!ISXDIGIT (c))
1423 break;
1425 else if (!ISDIGIT (c) || (int) (c - L_('0')) >= base)
1427 if (base == 10 && (flags & GROUP))
1429 /* Try matching against the thousands separator. */
1430 #ifdef COMPILE_WSCANF
1431 if (c != thousands)
1432 break;
1433 #else
1434 const char *cmpp = thousands;
1435 int avail = width > 0 ? width : INT_MAX;
1437 while ((unsigned char) *cmpp == c && avail >= 0)
1439 ADDW (c);
1440 if (*++cmpp == '\0')
1441 break;
1442 else
1444 if (avail == 0 || inchar () == EOF)
1445 break;
1446 --avail;
1450 if (*cmpp != '\0')
1452 /* We are pushing all read characters back. */
1453 if (cmpp > thousands)
1455 wpsize -= cmpp - thousands;
1456 ungetc (c, s);
1457 while (--cmpp > thousands)
1458 ungetc_not_eof ((unsigned char) *cmpp, s);
1459 c = (unsigned char) *cmpp;
1461 break;
1464 if (width > 0)
1465 width = avail;
1467 /* The last thousands character will be added back by
1468 the ADDW below. */
1469 --wpsize;
1470 #endif
1472 else
1473 break;
1475 ADDW (c);
1476 if (width > 0)
1477 --width;
1479 c = inchar ();
1482 if (wpsize == 0
1483 || (wpsize == 1 && (wp[0] == L_('+') || wp[0] == L_('-'))))
1485 /* There was no number. If we are supposed to read a pointer
1486 we must recognize "(nil)" as well. */
1487 if (__builtin_expect (wpsize == 0
1488 && (flags & READ_POINTER)
1489 && (width < 0 || width >= 0)
1490 && c == '('
1491 && TOLOWER (inchar ()) == L_('n')
1492 && TOLOWER (inchar ()) == L_('i')
1493 && TOLOWER (inchar ()) == L_('l')
1494 && inchar () == L_(')'), 1))
1495 /* We must produce the value of a NULL pointer. A single
1496 '0' digit is enough. */
1497 ADDW (L_('0'));
1498 else
1500 /* The last read character is not part of the number
1501 anymore. */
1502 ungetc (c, s);
1504 conv_error ();
1507 else
1508 /* The just read character is not part of the number anymore. */
1509 ungetc (c, s);
1511 /* Convert the number. */
1512 ADDW (L_('\0'));
1513 if (need_longlong && (flags & LONGDBL))
1515 if (flags & NUMBER_SIGNED)
1516 num.q = __strtoll_internal (wp, &tw, base, flags & GROUP);
1517 else
1518 num.uq = __strtoull_internal (wp, &tw, base, flags & GROUP);
1520 else
1522 if (flags & NUMBER_SIGNED)
1523 num.l = __strtol_internal (wp, &tw, base, flags & GROUP);
1524 else
1525 num.ul = __strtoul_internal (wp, &tw, base, flags & GROUP);
1527 if (__builtin_expect (wp == tw, 0))
1528 conv_error ();
1530 if (!(flags & SUPPRESS))
1532 if (flags & NUMBER_SIGNED)
1534 if (need_longlong && (flags & LONGDBL))
1535 *ARG (LONGLONG int *) = num.q;
1536 else if (need_long && (flags & LONG))
1537 *ARG (long int *) = num.l;
1538 else if (flags & SHORT)
1539 *ARG (short int *) = (short int) num.l;
1540 else if (!(flags & CHAR))
1541 *ARG (int *) = (int) num.l;
1542 else
1543 *ARG (signed char *) = (signed char) num.ul;
1545 else
1547 if (need_longlong && (flags & LONGDBL))
1548 *ARG (unsigned LONGLONG int *) = num.uq;
1549 else if (need_long && (flags & LONG))
1550 *ARG (unsigned long int *) = num.ul;
1551 else if (flags & SHORT)
1552 *ARG (unsigned short int *)
1553 = (unsigned short int) num.ul;
1554 else if (!(flags & CHAR))
1555 *ARG (unsigned int *) = (unsigned int) num.ul;
1556 else
1557 *ARG (unsigned char *) = (unsigned char) num.ul;
1559 ++done;
1561 break;
1563 case L_('e'): /* Floating-point numbers. */
1564 case L_('E'):
1565 case L_('f'):
1566 case L_('F'):
1567 case L_('g'):
1568 case L_('G'):
1569 case L_('a'):
1570 case L_('A'):
1571 c = inchar ();
1572 if (width > 0)
1573 --width;
1574 if (__builtin_expect (c == EOF, 0))
1575 input_error ();
1577 got_dot = got_e = 0;
1579 /* Check for a sign. */
1580 if (c == L_('-') || c == L_('+'))
1582 negative = c == L_('-');
1583 if (__builtin_expect (width == 0 || inchar () == EOF, 0))
1584 /* EOF is only an input error before we read any chars. */
1585 conv_error ();
1586 if (width > 0)
1587 --width;
1589 else
1590 negative = 0;
1592 /* Take care for the special arguments "nan" and "inf". */
1593 if (TOLOWER (c) == L_('n'))
1595 /* Maybe "nan". */
1596 ADDW (c);
1597 if (__builtin_expect (width == 0
1598 || inchar () == EOF
1599 || TOLOWER (c) != L_('a'), 0))
1600 conv_error ();
1601 if (width > 0)
1602 --width;
1603 ADDW (c);
1604 if (__builtin_expect (width == 0
1605 || inchar () == EOF
1606 || TOLOWER (c) != L_('n'), 0))
1607 conv_error ();
1608 if (width > 0)
1609 --width;
1610 ADDW (c);
1611 /* It is "nan". */
1612 goto scan_float;
1614 else if (TOLOWER (c) == L_('i'))
1616 /* Maybe "inf" or "infinity". */
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 if (__builtin_expect (width == 0
1626 || inchar () == EOF
1627 || TOLOWER (c) != L_('f'), 0))
1628 conv_error ();
1629 if (width > 0)
1630 --width;
1631 ADDW (c);
1632 /* It is as least "inf". */
1633 if (width != 0 && inchar () != EOF)
1635 if (TOLOWER (c) == L_('i'))
1637 if (width > 0)
1638 --width;
1639 /* Now we have to read the rest as well. */
1640 ADDW (c);
1641 if (__builtin_expect (width == 0
1642 || inchar () == EOF
1643 || TOLOWER (c) != L_('n'), 0))
1644 conv_error ();
1645 if (width > 0)
1646 --width;
1647 ADDW (c);
1648 if (__builtin_expect (width == 0
1649 || inchar () == EOF
1650 || TOLOWER (c) != L_('i'), 0))
1651 conv_error ();
1652 if (width > 0)
1653 --width;
1654 ADDW (c);
1655 if (__builtin_expect (width == 0
1656 || inchar () == EOF
1657 || TOLOWER (c) != L_('t'), 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_('y'), 0))
1665 conv_error ();
1666 if (width > 0)
1667 --width;
1668 ADDW (c);
1670 else
1671 /* Never mind. */
1672 ungetc (c, s);
1674 goto scan_float;
1677 exp_char = L_('e');
1678 if (width != 0 && c == L_('0'))
1680 ADDW (c);
1681 c = inchar ();
1682 if (width > 0)
1683 --width;
1684 if (width != 0 && TOLOWER (c) == L_('x'))
1686 /* It is a number in hexadecimal format. */
1687 ADDW (c);
1689 flags |= HEXA_FLOAT;
1690 exp_char = L_('p');
1692 /* Grouping is not allowed. */
1693 flags &= ~GROUP;
1694 c = inchar ();
1695 if (width > 0)
1696 --width;
1700 while (1)
1702 if (ISDIGIT (c))
1703 ADDW (c);
1704 else if (!got_e && (flags & HEXA_FLOAT) && ISXDIGIT (c))
1705 ADDW (c);
1706 else if (got_e && wp[wpsize - 1] == exp_char
1707 && (c == L_('-') || c == L_('+')))
1708 ADDW (c);
1709 else if (wpsize > 0 && !got_e
1710 && (CHAR_T) TOLOWER (c) == exp_char)
1712 ADDW (exp_char);
1713 got_e = got_dot = 1;
1715 else
1717 #ifdef COMPILE_WSCANF
1718 if (! got_dot && c == decimal)
1720 ADDW (c);
1721 got_dot = 1;
1723 else if ((flags & GROUP) != 0 && ! got_dot && c == thousands)
1724 ADDW (c);
1725 else
1727 /* The last read character is not part of the number
1728 anymore. */
1729 ungetc (c, s);
1730 break;
1732 #else
1733 const char *cmpp = decimal;
1734 int avail = width > 0 ? width : INT_MAX;
1736 if (! got_dot)
1738 while ((unsigned char) *cmpp == c && avail >= 0)
1739 if (*++cmpp == '\0')
1740 break;
1741 else
1743 if (avail == 0 || inchar () == EOF)
1744 break;
1745 --avail;
1749 if (*cmpp == '\0')
1751 /* Add all the characters. */
1752 for (cmpp = decimal; *cmpp != '\0'; ++cmpp)
1753 ADDW ((unsigned char) *cmpp);
1754 if (width > 0)
1755 width = avail;
1756 got_dot = 1;
1758 else
1760 /* Figure out whether it is a thousands separator.
1761 There is one problem: we possibly read more than
1762 one character. We cannot push them back but since
1763 we know that parts of the `decimal' string matched,
1764 we can compare against it. */
1765 const char *cmp2p = thousands;
1767 if ((flags & GROUP) != 0 && ! got_dot)
1769 while (cmp2p - thousands < cmpp - decimal
1770 && *cmp2p == decimal[cmp2p - thousands])
1771 ++cmp2p;
1772 if (cmp2p - thousands == cmpp - decimal)
1774 while ((unsigned char) *cmp2p == c && avail >= 0)
1775 if (*++cmp2p == '\0')
1776 break;
1777 else
1779 if (avail == 0 || inchar () == EOF)
1780 break;
1781 --avail;
1786 if (cmp2p != NULL && *cmp2p == '\0')
1788 /* Add all the characters. */
1789 for (cmpp = thousands; *cmpp != '\0'; ++cmpp)
1790 ADDW ((unsigned char) *cmpp);
1791 if (width > 0)
1792 width = avail;
1794 else
1796 /* The last read character is not part of the number
1797 anymore. */
1798 ungetc (c, s);
1799 break;
1802 #endif
1805 if (width == 0 || inchar () == EOF)
1806 break;
1808 if (width > 0)
1809 --width;
1812 wctrans_t map;
1813 if (__builtin_expect ((flags & I18N) != 0, 0)
1814 /* Hexadecimal floats make no sense, fixing localized
1815 digits with ASCII letters. */
1816 && !(flags & HEXA_FLOAT)
1817 /* Minimum requirement. */
1818 && (wpsize == 0 || got_dot)
1819 && (map = __wctrans ("to_inpunct")) != NULL)
1821 /* Reget the first character. */
1822 inchar ();
1824 /* Localized digits, decimal points, and thousands
1825 separator. */
1826 wint_t wcdigits[12];
1828 /* First get decimal equivalent to check if we read it
1829 or not. */
1830 wcdigits[11] = __towctrans (L'.', map);
1832 /* If we have not read any character or have just read
1833 locale decimal point which matches the decimal point
1834 for localized FP numbers, then we may have localized
1835 digits. Note, we test GOT_DOT above. */
1836 #ifdef COMPILE_WSCANF
1837 if (wpsize == 0 || (wpsize == 1 && wcdigits[11] == decimal))
1838 #else
1839 char mbdigits[12][MB_LEN_MAX + 1];
1841 mbstate_t state;
1842 memset (&state, '\0', sizeof (state));
1844 bool match_so_far = wpsize == 0;
1845 size_t mblen = __wcrtomb (mbdigits[11], wcdigits[11], &state);
1846 if (mblen != (size_t) -1)
1848 mbdigits[11][mblen] = '\0';
1849 match_so_far |= (wpsize == strlen (decimal)
1850 && strcmp (decimal, mbdigits[11]) == 0);
1852 else
1854 size_t decimal_len = strlen (decimal);
1855 /* This should always be the case but the data comes
1856 from a file. */
1857 if (decimal_len <= MB_LEN_MAX)
1859 match_so_far |= wpsize == decimal_len;
1860 memcpy (mbdigits[11], decimal, decimal_len + 1);
1862 else
1863 match_so_far = false;
1866 if (match_so_far)
1867 #endif
1869 bool have_locthousands = (flags & GROUP) != 0;
1871 /* Now get the digits and the thousands-sep equivalents. */
1872 for (int n = 0; n < 11; ++n)
1874 if (n < 10)
1875 wcdigits[n] = __towctrans (L'0' + n, map);
1876 else if (n == 10)
1878 wcdigits[10] = __towctrans (L',', map);
1879 have_locthousands &= wcdigits[10] != L'\0';
1882 #ifndef COMPILE_WSCANF
1883 memset (&state, '\0', sizeof (state));
1885 size_t mblen = __wcrtomb (mbdigits[n], wcdigits[n],
1886 &state);
1887 if (mblen == (size_t) -1)
1889 if (n == 10)
1891 if (have_locthousands)
1893 size_t thousands_len = strlen (thousands);
1894 if (thousands_len <= MB_LEN_MAX)
1895 memcpy (mbdigits[10], thousands,
1896 thousands_len + 1);
1897 else
1898 have_locthousands = false;
1901 else
1902 /* Ignore checking against localized digits. */
1903 goto no_i18nflt;
1905 else
1906 mbdigits[n][mblen] = '\0';
1907 #endif
1910 /* Start checking against localized digits, if
1911 convertion is done correctly. */
1912 while (1)
1914 if (got_e && wp[wpsize - 1] == exp_char
1915 && (c == L_('-') || c == L_('+')))
1916 ADDW (c);
1917 else if (wpsize > 0 && !got_e
1918 && (CHAR_T) TOLOWER (c) == exp_char)
1920 ADDW (exp_char);
1921 got_e = got_dot = 1;
1923 else
1925 /* Check against localized digits, decimal point,
1926 and thousands separator. */
1927 int n;
1928 for (n = 0; n < 12; ++n)
1930 #ifdef COMPILE_WSCANF
1931 if (c == wcdigits[n])
1933 if (n < 10)
1934 ADDW (L_('0') + n);
1935 else if (n == 11 && !got_dot)
1937 ADDW (decimal);
1938 got_dot = 1;
1940 else if (n == 10 && have_locthousands
1941 && ! got_dot)
1942 ADDW (thousands);
1943 else
1944 /* The last read character is not part
1945 of the number anymore. */
1946 n = 12;
1948 break;
1950 #else
1951 const char *cmpp = mbdigits[n];
1952 int avail = width > 0 ? width : INT_MAX;
1954 while ((unsigned char) *cmpp == c && avail >= 0)
1955 if (*++cmpp == '\0')
1956 break;
1957 else
1959 if (avail == 0 || inchar () == EOF)
1960 break;
1961 --avail;
1963 if (*cmpp == '\0')
1965 if (width > 0)
1966 width = avail;
1968 if (n < 10)
1969 ADDW (L_('0') + n);
1970 else if (n == 11 && !got_dot)
1972 /* Add all the characters. */
1973 for (cmpp = decimal; *cmpp != '\0';
1974 ++cmpp)
1975 ADDW ((unsigned char) *cmpp);
1977 got_dot = 1;
1979 else if (n == 10 && (flags & GROUP) != 0
1980 && ! got_dot)
1982 /* Add all the characters. */
1983 for (cmpp = thousands; *cmpp != '\0';
1984 ++cmpp)
1985 ADDW ((unsigned char) *cmpp);
1987 else
1988 /* The last read character is not part
1989 of the number anymore. */
1990 n = 12;
1992 break;
1995 /* We are pushing all read characters back. */
1996 if (cmpp > mbdigits[n])
1998 ungetc (c, s);
1999 while (--cmpp > mbdigits[n])
2000 ungetc_not_eof ((unsigned char) *cmpp, s);
2001 c = (unsigned char) *cmpp;
2003 #endif
2006 if (n >= 12)
2008 /* The last read character is not part
2009 of the number anymore. */
2010 ungetc (c, s);
2011 break;
2015 if (width == 0 || inchar () == EOF)
2016 break;
2018 if (width > 0)
2019 --width;
2023 #ifndef COMPILE_WSCANF
2024 no_i18nflt:
2026 #endif
2029 /* Have we read any character? If we try to read a number
2030 in hexadecimal notation and we have read only the `0x'
2031 prefix this is an error. */
2032 if (__builtin_expect (wpsize == 0
2033 || ((flags & HEXA_FLOAT) && wpsize == 2), 0))
2034 conv_error ();
2036 scan_float:
2037 /* Convert the number. */
2038 ADDW (L_('\0'));
2039 if ((flags & LONGDBL) && !__ldbl_is_dbl)
2041 long double d = __strtold_internal (wp, &tw, flags & GROUP);
2042 if (!(flags & SUPPRESS) && tw != wp)
2043 *ARG (long double *) = negative ? -d : d;
2045 else if (flags & (LONG | LONGDBL))
2047 double d = __strtod_internal (wp, &tw, flags & GROUP);
2048 if (!(flags & SUPPRESS) && tw != wp)
2049 *ARG (double *) = negative ? -d : d;
2051 else
2053 float d = __strtof_internal (wp, &tw, flags & GROUP);
2054 if (!(flags & SUPPRESS) && tw != wp)
2055 *ARG (float *) = negative ? -d : d;
2058 if (__builtin_expect (tw == wp, 0))
2059 conv_error ();
2061 if (!(flags & SUPPRESS))
2062 ++done;
2063 break;
2065 case L_('['): /* Character class. */
2066 if (flags & LONG)
2067 STRING_ARG (wstr, wchar_t);
2068 else
2069 STRING_ARG (str, char);
2071 if (*f == L_('^'))
2073 ++f;
2074 not_in = 1;
2076 else
2077 not_in = 0;
2079 if (width < 0)
2080 /* There is no width given so there is also no limit on the
2081 number of characters we read. Therefore we set width to
2082 a very high value to make the algorithm easier. */
2083 width = INT_MAX;
2085 #ifdef COMPILE_WSCANF
2086 /* Find the beginning and the end of the scanlist. We are not
2087 creating a lookup table since it would have to be too large.
2088 Instead we search each time through the string. This is not
2089 a constant lookup time but who uses this feature deserves to
2090 be punished. */
2091 tw = (wchar_t *) f; /* Marks the beginning. */
2093 if (*f == L']')
2094 ++f;
2096 while ((fc = *f++) != L'\0' && fc != L']');
2098 if (__builtin_expect (fc == L'\0', 0))
2099 conv_error ();
2100 wp = (wchar_t *) f - 1;
2101 #else
2102 /* Fill WP with byte flags indexed by character.
2103 We will use this flag map for matching input characters. */
2104 if (wpmax < UCHAR_MAX + 1)
2106 wpmax = UCHAR_MAX + 1;
2107 wp = (char *) alloca (wpmax);
2109 memset (wp, '\0', UCHAR_MAX + 1);
2111 fc = *f;
2112 if (fc == ']' || fc == '-')
2114 /* If ] or - appears before any char in the set, it is not
2115 the terminator or separator, but the first char in the
2116 set. */
2117 wp[fc] = 1;
2118 ++f;
2121 while ((fc = *f++) != '\0' && fc != ']')
2122 if (fc == '-' && *f != '\0' && *f != ']'
2123 && (unsigned char) f[-2] <= (unsigned char) *f)
2125 /* Add all characters from the one before the '-'
2126 up to (but not including) the next format char. */
2127 for (fc = (unsigned char) f[-2]; fc < (unsigned char) *f; ++fc)
2128 wp[fc] = 1;
2130 else
2131 /* Add the character to the flag map. */
2132 wp[fc] = 1;
2134 if (__builtin_expect (fc == '\0', 0))
2135 conv_error();
2136 #endif
2138 if (flags & LONG)
2140 size_t now = read_in;
2141 #ifdef COMPILE_WSCANF
2142 if (__builtin_expect (inchar () == WEOF, 0))
2143 input_error ();
2147 wchar_t *runp;
2149 /* Test whether it's in the scanlist. */
2150 runp = tw;
2151 while (runp < wp)
2153 if (runp[0] == L'-' && runp[1] != '\0' && runp + 1 != wp
2154 && runp != tw
2155 && (unsigned int) runp[-1] <= (unsigned int) runp[1])
2157 /* Match against all characters in between the
2158 first and last character of the sequence. */
2159 wchar_t wc;
2161 for (wc = runp[-1] + 1; wc <= runp[1]; ++wc)
2162 if ((wint_t) wc == c)
2163 break;
2165 if (wc <= runp[1] && !not_in)
2166 break;
2167 if (wc <= runp[1] && not_in)
2169 /* The current character is not in the
2170 scanset. */
2171 ungetc (c, s);
2172 goto out;
2175 runp += 2;
2177 else
2179 if ((wint_t) *runp == c && !not_in)
2180 break;
2181 if ((wint_t) *runp == c && not_in)
2183 ungetc (c, s);
2184 goto out;
2187 ++runp;
2191 if (runp == wp && !not_in)
2193 ungetc (c, s);
2194 goto out;
2197 if (!(flags & SUPPRESS))
2199 *wstr++ = c;
2201 if ((flags & MALLOC)
2202 && wstr == (wchar_t *) *strptr + strsize)
2204 /* Enlarge the buffer. */
2205 wstr = (wchar_t *) realloc (*strptr,
2206 (2 * strsize)
2207 * sizeof (wchar_t));
2208 if (wstr == NULL)
2210 /* Can't allocate that much. Last-ditch
2211 effort. */
2212 wstr = (wchar_t *)
2213 realloc (*strptr, (strsize + 1)
2214 * sizeof (wchar_t));
2215 if (wstr == NULL)
2217 /* We lose. Oh well. Terminate the string
2218 and stop converting, so at least we don't
2219 skip any input. */
2220 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
2221 ++done;
2222 conv_error ();
2224 else
2226 *strptr = (char *) wstr;
2227 wstr += strsize;
2228 ++strsize;
2231 else
2233 *strptr = (char *) wstr;
2234 wstr += strsize;
2235 strsize *= 2;
2240 while (--width > 0 && inchar () != WEOF);
2241 out:
2242 #else
2243 char buf[MB_LEN_MAX];
2244 size_t cnt = 0;
2245 mbstate_t cstate;
2247 if (__builtin_expect (inchar () == EOF, 0))
2248 input_error ();
2250 memset (&cstate, '\0', sizeof (cstate));
2254 if (wp[c] == not_in)
2256 ungetc_not_eof (c, s);
2257 break;
2260 /* This is easy. */
2261 if (!(flags & SUPPRESS))
2263 size_t n;
2265 /* Convert it into a wide character. */
2266 buf[0] = c;
2267 n = __mbrtowc (wstr, buf, 1, &cstate);
2269 if (n == (size_t) -2)
2271 /* Possibly correct character, just not enough
2272 input. */
2273 ++cnt;
2274 assert (cnt < MB_CUR_MAX);
2275 continue;
2277 cnt = 0;
2279 ++wstr;
2280 if ((flags & MALLOC)
2281 && wstr == (wchar_t *) *strptr + strsize)
2283 /* Enlarge the buffer. */
2284 wstr = (wchar_t *) realloc (*strptr,
2285 (2 * strsize
2286 * sizeof (wchar_t)));
2287 if (wstr == NULL)
2289 /* Can't allocate that much. Last-ditch
2290 effort. */
2291 wstr = (wchar_t *)
2292 realloc (*strptr, ((strsize + 1)
2293 * sizeof (wchar_t)));
2294 if (wstr == NULL)
2296 /* We lose. Oh well. Terminate the
2297 string and stop converting,
2298 so at least we don't skip any input. */
2299 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
2300 ++done;
2301 conv_error ();
2303 else
2305 *strptr = (char *) wstr;
2306 wstr += strsize;
2307 ++strsize;
2310 else
2312 *strptr = (char *) wstr;
2313 wstr += strsize;
2314 strsize *= 2;
2319 if (--width <= 0)
2320 break;
2322 while (inchar () != EOF);
2324 if (__builtin_expect (cnt != 0, 0))
2325 /* We stopped in the middle of recognizing another
2326 character. That's a problem. */
2327 encode_error ();
2328 #endif
2330 if (__builtin_expect (now == read_in, 0))
2331 /* We haven't succesfully read any character. */
2332 conv_error ();
2334 if (!(flags & SUPPRESS))
2336 *wstr++ = L'\0';
2338 if ((flags & MALLOC)
2339 && wstr - (wchar_t *) *strptr != strsize)
2341 wchar_t *cp = (wchar_t *)
2342 realloc (*strptr, ((wstr - (wchar_t *) *strptr)
2343 * sizeof(wchar_t)));
2344 if (cp != NULL)
2345 *strptr = (char *) cp;
2348 ++done;
2351 else
2353 size_t now = read_in;
2355 if (__builtin_expect (inchar () == EOF, 0))
2356 input_error ();
2358 #ifdef COMPILE_WSCANF
2360 memset (&state, '\0', sizeof (state));
2364 wchar_t *runp;
2365 size_t n;
2367 /* Test whether it's in the scanlist. */
2368 runp = tw;
2369 while (runp < wp)
2371 if (runp[0] == L'-' && runp[1] != '\0' && runp + 1 != wp
2372 && runp != tw
2373 && (unsigned int) runp[-1] <= (unsigned int) runp[1])
2375 /* Match against all characters in between the
2376 first and last character of the sequence. */
2377 wchar_t wc;
2379 for (wc = runp[-1] + 1; wc <= runp[1]; ++wc)
2380 if ((wint_t) wc == c)
2381 break;
2383 if (wc <= runp[1] && !not_in)
2384 break;
2385 if (wc <= runp[1] && not_in)
2387 /* The current character is not in the
2388 scanset. */
2389 ungetc (c, s);
2390 goto out2;
2393 runp += 2;
2395 else
2397 if ((wint_t) *runp == c && !not_in)
2398 break;
2399 if ((wint_t) *runp == c && not_in)
2401 ungetc (c, s);
2402 goto out2;
2405 ++runp;
2409 if (runp == wp && !not_in)
2411 ungetc (c, s);
2412 goto out2;
2415 if (!(flags & SUPPRESS))
2417 if ((flags & MALLOC)
2418 && str + MB_CUR_MAX >= *strptr + strsize)
2420 /* Enlarge the buffer. */
2421 size_t strleng = str - *strptr;
2422 char *newstr;
2424 newstr = (char *) realloc (*strptr, 2 * strsize);
2425 if (newstr == NULL)
2427 /* Can't allocate that much. Last-ditch
2428 effort. */
2429 newstr = (char *) realloc (*strptr,
2430 strleng + MB_CUR_MAX);
2431 if (newstr == NULL)
2433 /* We lose. Oh well. Terminate the string
2434 and stop converting, so at least we don't
2435 skip any input. */
2436 ((char *) (*strptr))[strleng] = '\0';
2437 ++done;
2438 conv_error ();
2440 else
2442 *strptr = newstr;
2443 str = newstr + strleng;
2444 strsize = strleng + MB_CUR_MAX;
2447 else
2449 *strptr = newstr;
2450 str = newstr + strleng;
2451 strsize *= 2;
2456 n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c, &state);
2457 if (__builtin_expect (n == (size_t) -1, 0))
2458 encode_error ();
2460 assert (n <= MB_CUR_MAX);
2461 str += n;
2463 while (--width > 0 && inchar () != WEOF);
2464 out2:
2465 #else
2468 if (wp[c] == not_in)
2470 ungetc_not_eof (c, s);
2471 break;
2474 /* This is easy. */
2475 if (!(flags & SUPPRESS))
2477 *str++ = c;
2478 if ((flags & MALLOC)
2479 && (char *) str == *strptr + strsize)
2481 /* Enlarge the buffer. */
2482 size_t newsize = 2 * strsize;
2484 allocagain:
2485 str = (char *) realloc (*strptr, newsize);
2486 if (str == NULL)
2488 /* Can't allocate that much. Last-ditch
2489 effort. */
2490 if (newsize > strsize + 1)
2492 newsize = strsize + 1;
2493 goto allocagain;
2495 /* We lose. Oh well. Terminate the
2496 string and stop converting,
2497 so at least we don't skip any input. */
2498 ((char *) (*strptr))[strsize - 1] = '\0';
2499 ++done;
2500 conv_error ();
2502 else
2504 *strptr = (char *) str;
2505 str += strsize;
2506 strsize = newsize;
2511 while (--width > 0 && inchar () != EOF);
2512 #endif
2514 if (__builtin_expect (now == read_in, 0))
2515 /* We haven't succesfully read any character. */
2516 conv_error ();
2518 if (!(flags & SUPPRESS))
2520 #ifdef COMPILE_WSCANF
2521 /* We have to emit the code to get into the initial
2522 state. */
2523 char buf[MB_LEN_MAX];
2524 size_t n = __wcrtomb (buf, L'\0', &state);
2525 if (n > 0 && (flags & MALLOC)
2526 && str + n >= *strptr + strsize)
2528 /* Enlarge the buffer. */
2529 size_t strleng = str - *strptr;
2530 char *newstr;
2532 newstr = (char *) realloc (*strptr, strleng + n + 1);
2533 if (newstr == NULL)
2535 /* We lose. Oh well. Terminate the string
2536 and stop converting, so at least we don't
2537 skip any input. */
2538 ((char *) (*strptr))[strleng] = '\0';
2539 ++done;
2540 conv_error ();
2542 else
2544 *strptr = newstr;
2545 str = newstr + strleng;
2546 strsize = strleng + n + 1;
2550 str = __mempcpy (str, buf, n);
2551 #endif
2552 *str++ = '\0';
2554 if ((flags & MALLOC) && str - *strptr != strsize)
2556 char *cp = (char *) realloc (*strptr, str - *strptr);
2557 if (cp != NULL)
2558 *strptr = cp;
2561 ++done;
2564 break;
2566 case L_('p'): /* Generic pointer. */
2567 base = 16;
2568 /* A PTR must be the same size as a `long int'. */
2569 flags &= ~(SHORT|LONGDBL);
2570 if (need_long)
2571 flags |= LONG;
2572 flags |= READ_POINTER;
2573 goto number;
2575 default:
2576 /* If this is an unknown format character punt. */
2577 conv_error ();
2581 /* The last thing we saw int the format string was a white space.
2582 Consume the last white spaces. */
2583 if (skip_space)
2586 c = inchar ();
2587 while (ISSPACE (c));
2588 ungetc (c, s);
2591 errout:
2592 /* Unlock stream. */
2593 UNLOCK_STREAM (s);
2595 if (errp != NULL)
2596 *errp |= errval;
2598 return done;
2601 #ifdef COMPILE_WSCANF
2603 __vfwscanf (FILE *s, const wchar_t *format, va_list argptr)
2605 return _IO_vfwscanf (s, format, argptr, NULL);
2607 ldbl_weak_alias (__vfwscanf, vfwscanf)
2608 #else
2610 ___vfscanf (FILE *s, const char *format, va_list argptr)
2612 return _IO_vfscanf_internal (s, format, argptr, NULL);
2614 ldbl_strong_alias (_IO_vfscanf_internal, _IO_vfscanf)
2615 ldbl_strong_alias (___vfscanf, __vfscanf)
2616 ldbl_hidden_def (___vfscanf, __vfscanf)
2617 ldbl_weak_alias (___vfscanf, vfscanf)
2618 #endif