.
[glibc.git] / stdio-common / vfscanf.c
blob66716022912f3b70d7d7ee37732b41095cdfcac6
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 0x001 /* l: long or double */
56 #define LONGDBL 0x002 /* L: long long or long double */
57 #define SHORT 0x004 /* h: short */
58 #define SUPPRESS 0x008 /* *: suppress assignment */
59 #define POINTER 0x010 /* weird %p pointer (`fake hex') */
60 #define NOSKIP 0x020 /* do not skip blanks */
61 #define WIDTH 0x040 /* width was given */
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 flags |= WIDTH;
427 argpos = 0;
428 goto got_width;
432 /* Check for the assignment-suppressing, the number grouping flag,
433 and the signal to use the locale's digit representation. */
434 while (*f == L_('*') || *f == L_('\'') || *f == L_('I'))
435 switch (*f++)
437 case L_('*'):
438 flags |= SUPPRESS;
439 break;
440 case L_('\''):
441 flags |= GROUP;
442 break;
443 case L_('I'):
444 flags |= I18N;
445 break;
448 /* We have seen width. */
449 if (ISDIGIT ((UCHAR_T) *f))
450 flags |= WIDTH;
452 /* Find the maximum field width. */
453 width = 0;
454 while (ISDIGIT ((UCHAR_T) *f))
456 width *= 10;
457 width += (UCHAR_T) *f++ - L_('0');
459 got_width:
460 if (width == 0)
461 width = -1;
463 /* Check for type modifiers. */
464 switch (*f++)
466 case L_('h'):
467 /* ints are short ints or chars. */
468 if (*f == L_('h'))
470 ++f;
471 flags |= CHAR;
473 else
474 flags |= SHORT;
475 break;
476 case L_('l'):
477 if (*f == L_('l'))
479 /* A double `l' is equivalent to an `L'. */
480 ++f;
481 flags |= LONGDBL | LONG;
483 else
484 /* ints are long ints. */
485 flags |= LONG;
486 break;
487 case L_('q'):
488 case L_('L'):
489 /* doubles are long doubles, and ints are long long ints. */
490 flags |= LONGDBL | LONG;
491 break;
492 case L_('a'):
493 /* The `a' is used as a flag only if followed by `s', `S' or
494 `['. */
495 if (*f != L_('s') && *f != L_('S') && *f != L_('['))
497 --f;
498 break;
500 /* String conversions (%s, %[) take a `char **'
501 arg and fill it in with a malloc'd pointer. */
502 flags |= MALLOC;
503 break;
504 case L_('z'):
505 if (need_longlong && sizeof (size_t) > sizeof (unsigned long int))
506 flags |= LONGDBL;
507 else if (sizeof (size_t) > sizeof (unsigned int))
508 flags |= LONG;
509 break;
510 case L_('j'):
511 if (need_longlong && sizeof (uintmax_t) > sizeof (unsigned long int))
512 flags |= LONGDBL;
513 else if (sizeof (uintmax_t) > sizeof (unsigned int))
514 flags |= LONG;
515 break;
516 case L_('t'):
517 if (need_longlong && sizeof (ptrdiff_t) > sizeof (long int))
518 flags |= LONGDBL;
519 else if (sizeof (ptrdiff_t) > sizeof (int))
520 flags |= LONG;
521 break;
522 default:
523 /* Not a recognized modifier. Backup. */
524 --f;
525 break;
528 /* End of the format string? */
529 if (__builtin_expect (*f == L_('\0'), 0))
530 conv_error ();
532 /* Find the conversion specifier. */
533 fc = *f++;
534 if (skip_space || (fc != L_('[') && fc != L_('c')
535 && fc != L_('C') && fc != L_('n')))
537 /* Eat whitespace. */
538 int save_errno = errno;
539 errno = 0;
541 if (__builtin_expect (inchar () == EOF && errno == EINTR, 0))
542 input_error ();
543 while (ISSPACE (c));
544 errno = save_errno;
545 ungetc (c, s);
546 skip_space = 0;
549 switch (fc)
551 case L_('%'): /* Must match a literal '%'. */
552 c = inchar ();
553 if (__builtin_expect (c == EOF, 0))
554 input_error ();
555 if (__builtin_expect (c != fc, 0))
557 ungetc_not_eof (c, s);
558 conv_error ();
560 break;
562 case L_('n'): /* Answer number of assignments done. */
563 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
564 with the 'n' conversion specifier. */
565 if (!(flags & SUPPRESS))
567 /* Don't count the read-ahead. */
568 if (need_longlong && (flags & LONGDBL))
569 *ARG (long long int *) = read_in;
570 else if (need_long && (flags & LONG))
571 *ARG (long int *) = read_in;
572 else if (flags & SHORT)
573 *ARG (short int *) = read_in;
574 else if (!(flags & CHAR))
575 *ARG (int *) = read_in;
576 else
577 *ARG (char *) = read_in;
579 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
580 /* We have a severe problem here. The ISO C standard
581 contradicts itself in explaining the effect of the %n
582 format in `scanf'. While in ISO C:1990 and the ISO C
583 Amendement 1:1995 the result is described as
585 Execution of a %n directive does not effect the
586 assignment count returned at the completion of
587 execution of the f(w)scanf function.
589 in ISO C Corrigendum 1:1994 the following was added:
591 Subclause 7.9.6.2
592 Add the following fourth example:
594 #include <stdio.h>
595 int d1, d2, n1, n2, i;
596 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
597 the value 123 is assigned to d1 and the value3 to n1.
598 Because %n can never get an input failure the value
599 of 3 is also assigned to n2. The value of d2 is not
600 affected. The value 3 is assigned to i.
602 We go for now with the historically correct code from ISO C,
603 i.e., we don't count the %n assignments. When it ever
604 should proof to be wrong just remove the #ifdef above. */
605 ++done;
606 #endif
608 break;
610 case L_('c'): /* Match characters. */
611 if ((flags & LONG) == 0)
613 if (!(flags & SUPPRESS))
615 str = ARG (char *);
616 if (str == NULL)
617 conv_error ();
620 c = inchar ();
621 if (__builtin_expect (c == EOF, 0))
622 input_error ();
624 if (width == -1)
625 width = 1;
627 #ifdef COMPILE_WSCANF
628 /* We have to convert the wide character(s) into multibyte
629 characters and store the result. */
630 memset (&state, '\0', sizeof (state));
634 size_t n;
636 n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c, &state);
637 if (__builtin_expect (n == (size_t) -1, 0))
638 /* No valid wide character. */
639 input_error ();
641 /* Increment the output pointer. Even if we don't
642 write anything. */
643 str += n;
645 while (--width > 0 && inchar () != EOF);
646 #else
647 if (!(flags & SUPPRESS))
650 *str++ = c;
651 while (--width > 0 && inchar () != EOF);
653 else
654 while (--width > 0 && inchar () != EOF);
655 #endif
657 if (!(flags & SUPPRESS))
658 ++done;
660 break;
662 /* FALLTHROUGH */
663 case L_('C'):
664 if (!(flags & SUPPRESS))
666 wstr = ARG (wchar_t *);
667 if (wstr == NULL)
668 conv_error ();
671 c = inchar ();
672 if (__builtin_expect (c == EOF, 0))
673 input_error ();
675 #ifdef COMPILE_WSCANF
676 /* Just store the incoming wide characters. */
677 if (!(flags & SUPPRESS))
680 *wstr++ = c;
681 while (--width > 0 && inchar () != EOF);
683 else
684 while (--width > 0 && inchar () != EOF);
685 #else
687 /* We have to convert the multibyte input sequence to wide
688 characters. */
689 char buf[1];
690 mbstate_t cstate;
692 memset (&cstate, '\0', sizeof (cstate));
696 /* This is what we present the mbrtowc function first. */
697 buf[0] = c;
699 while (1)
701 size_t n;
703 n = __mbrtowc (!(flags & SUPPRESS) ? wstr : NULL,
704 buf, 1, &cstate);
706 if (n == (size_t) -2)
708 /* Possibly correct character, just not enough
709 input. */
710 if (__builtin_expect (inchar () == EOF, 0))
711 encode_error ();
713 buf[0] = c;
714 continue;
717 if (__builtin_expect (n != 1, 0))
718 encode_error ();
720 /* We have a match. */
721 break;
724 /* Advance the result pointer. */
725 ++wstr;
727 while (--width > 0 && inchar () != EOF);
729 #endif
731 if (!(flags & SUPPRESS))
732 ++done;
734 break;
736 case L_('s'): /* Read a string. */
737 if (!(flags & LONG))
739 #define STRING_ARG(Str, Type) \
740 do if (!(flags & SUPPRESS)) \
742 if (flags & MALLOC) \
744 /* The string is to be stored in a malloc'd buffer. */ \
745 strptr = ARG (char **); \
746 if (strptr == NULL) \
747 conv_error (); \
748 /* Allocate an initial buffer. */ \
749 strsize = 100; \
750 *strptr = (char *) malloc (strsize * sizeof (Type)); \
751 Str = (Type *) *strptr; \
753 else \
754 Str = ARG (Type *); \
755 if (Str == NULL) \
756 conv_error (); \
757 } while (0)
758 STRING_ARG (str, char);
760 c = inchar ();
761 if (__builtin_expect (c == EOF, 0))
762 input_error ();
764 #ifdef COMPILE_WSCANF
765 memset (&state, '\0', sizeof (state));
766 #endif
770 if (ISSPACE (c))
772 ungetc_not_eof (c, s);
773 break;
776 #ifdef COMPILE_WSCANF
777 /* This is quite complicated. We have to convert the
778 wide characters into multibyte characters and then
779 store them. */
781 size_t n;
783 if (!(flags & SUPPRESS) && (flags & MALLOC)
784 && str + MB_CUR_MAX >= *strptr + strsize)
786 /* We have to enlarge the buffer if the `a' flag
787 was given. */
788 size_t strleng = str - *strptr;
789 char *newstr;
791 newstr = (char *) realloc (*strptr, strsize * 2);
792 if (newstr == NULL)
794 /* Can't allocate that much. Last-ditch
795 effort. */
796 newstr = (char *) realloc (*strptr,
797 strleng + MB_CUR_MAX);
798 if (newstr == NULL)
800 /* We lose. Oh well. Terminate the
801 string and stop converting,
802 so at least we don't skip any input. */
803 ((char *) (*strptr))[strleng] = '\0';
804 ++done;
805 conv_error ();
807 else
809 *strptr = newstr;
810 str = newstr + strleng;
811 strsize = strleng + MB_CUR_MAX;
814 else
816 *strptr = newstr;
817 str = newstr + strleng;
818 strsize *= 2;
822 n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c,
823 &state);
824 if (__builtin_expect (n == (size_t) -1, 0))
825 encode_error ();
827 assert (n <= MB_CUR_MAX);
828 str += n;
830 #else
831 /* This is easy. */
832 if (!(flags & SUPPRESS))
834 *str++ = c;
835 if ((flags & MALLOC)
836 && (char *) str == *strptr + strsize)
838 /* Enlarge the buffer. */
839 str = (char *) realloc (*strptr, 2 * strsize);
840 if (str == NULL)
842 /* Can't allocate that much. Last-ditch
843 effort. */
844 str = (char *) realloc (*strptr, strsize + 1);
845 if (str == NULL)
847 /* We lose. Oh well. Terminate the
848 string and stop converting,
849 so at least we don't skip any input. */
850 ((char *) (*strptr))[strsize - 1] = '\0';
851 ++done;
852 conv_error ();
854 else
856 *strptr = (char *) str;
857 str += strsize;
858 ++strsize;
861 else
863 *strptr = (char *) str;
864 str += strsize;
865 strsize *= 2;
869 #endif
871 while ((width <= 0 || --width > 0) && inchar () != EOF);
873 if (!(flags & SUPPRESS))
875 #ifdef COMPILE_WSCANF
876 /* We have to emit the code to get into the initial
877 state. */
878 char buf[MB_LEN_MAX];
879 size_t n = __wcrtomb (buf, L'\0', &state);
880 if (n > 0 && (flags & MALLOC)
881 && str + n >= *strptr + strsize)
883 /* Enlarge the buffer. */
884 size_t strleng = str - *strptr;
885 char *newstr;
887 newstr = (char *) realloc (*strptr, strleng + n + 1);
888 if (newstr == NULL)
890 /* We lose. Oh well. Terminate the string
891 and stop converting, so at least we don't
892 skip any input. */
893 ((char *) (*strptr))[strleng] = '\0';
894 ++done;
895 conv_error ();
897 else
899 *strptr = newstr;
900 str = newstr + strleng;
901 strsize = strleng + n + 1;
905 str = __mempcpy (str, buf, n);
906 #endif
907 *str++ = '\0';
909 if ((flags & MALLOC) && str - *strptr != strsize)
911 char *cp = (char *) realloc (*strptr, str - *strptr);
912 if (cp != NULL)
913 *strptr = cp;
916 ++done;
918 break;
920 /* FALLTHROUGH */
922 case L_('S'):
924 #ifndef COMPILE_WSCANF
925 mbstate_t cstate;
926 #endif
928 /* Wide character string. */
929 STRING_ARG (wstr, wchar_t);
931 c = inchar ();
932 if (__builtin_expect (c == EOF, 0))
933 input_error ();
935 #ifndef COMPILE_WSCANF
936 memset (&cstate, '\0', sizeof (cstate));
937 #endif
941 if (ISSPACE (c))
943 ungetc_not_eof (c, s);
944 break;
947 #ifdef COMPILE_WSCANF
948 /* This is easy. */
949 if (!(flags & SUPPRESS))
951 *wstr++ = c;
952 if ((flags & MALLOC)
953 && wstr == (wchar_t *) *strptr + strsize)
955 /* Enlarge the buffer. */
956 wstr = (wchar_t *) realloc (*strptr,
957 (2 * strsize)
958 * sizeof (wchar_t));
959 if (wstr == NULL)
961 /* Can't allocate that much. Last-ditch
962 effort. */
963 wstr = (wchar_t *) realloc (*strptr,
964 (strsize + 1)
965 * sizeof (wchar_t));
966 if (wstr == NULL)
968 /* We lose. Oh well. Terminate the string
969 and stop converting, so at least we don't
970 skip any input. */
971 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
972 ++done;
973 conv_error ();
975 else
977 *strptr = (char *) wstr;
978 wstr += strsize;
979 ++strsize;
982 else
984 *strptr = (char *) wstr;
985 wstr += strsize;
986 strsize *= 2;
990 #else
992 char buf[1];
994 buf[0] = c;
996 while (1)
998 size_t n;
1000 n = __mbrtowc (!(flags & SUPPRESS) ? wstr : NULL,
1001 buf, 1, &cstate);
1003 if (n == (size_t) -2)
1005 /* Possibly correct character, just not enough
1006 input. */
1007 if (__builtin_expect (inchar () == EOF, 0))
1008 encode_error ();
1010 buf[0] = c;
1011 continue;
1014 if (__builtin_expect (n != 1, 0))
1015 encode_error ();
1017 /* We have a match. */
1018 ++wstr;
1019 break;
1022 if (!(flags & SUPPRESS) && (flags & MALLOC)
1023 && wstr == (wchar_t *) *strptr + strsize)
1025 /* Enlarge the buffer. */
1026 wstr = (wchar_t *) realloc (*strptr,
1027 (2 * strsize
1028 * sizeof (wchar_t)));
1029 if (wstr == NULL)
1031 /* Can't allocate that much. Last-ditch effort. */
1032 wstr = (wchar_t *) realloc (*strptr,
1033 ((strsize + 1)
1034 * sizeof (wchar_t)));
1035 if (wstr == NULL)
1037 /* We lose. Oh well. Terminate the
1038 string and stop converting, so at
1039 least we don't skip any input. */
1040 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
1041 ++done;
1042 conv_error ();
1044 else
1046 *strptr = (char *) wstr;
1047 wstr += strsize;
1048 ++strsize;
1051 else
1053 *strptr = (char *) wstr;
1054 wstr += strsize;
1055 strsize *= 2;
1059 #endif
1061 while ((width <= 0 || --width > 0) && inchar () != EOF);
1063 if (!(flags & SUPPRESS))
1065 *wstr++ = L'\0';
1067 if ((flags & MALLOC) && wstr - (wchar_t *) *strptr != strsize)
1069 wchar_t *cp = (wchar_t *) realloc (*strptr,
1070 ((wstr
1071 - (wchar_t *) *strptr)
1072 * sizeof(wchar_t)));
1073 if (cp != NULL)
1074 *strptr = (char *) cp;
1077 ++done;
1080 break;
1082 case L_('x'): /* Hexadecimal integer. */
1083 case L_('X'): /* Ditto. */
1084 base = 16;
1085 number_signed = 0;
1086 goto number;
1088 case L_('o'): /* Octal integer. */
1089 base = 8;
1090 number_signed = 0;
1091 goto number;
1093 case L_('u'): /* Unsigned decimal integer. */
1094 base = 10;
1095 number_signed = 0;
1096 goto number;
1098 case L_('d'): /* Signed decimal integer. */
1099 base = 10;
1100 number_signed = 1;
1101 goto number;
1103 case L_('i'): /* Generic number. */
1104 base = 0;
1105 number_signed = 1;
1107 number:
1108 c = inchar ();
1109 if (__builtin_expect (c == EOF, 0))
1110 input_error ();
1112 /* Check for a sign. */
1113 if (c == L_('-') || c == L_('+'))
1115 ADDW (c);
1116 if (width > 0)
1117 --width;
1118 c = inchar ();
1121 /* Look for a leading indication of base. */
1122 if (width != 0 && c == L_('0'))
1124 if (width > 0)
1125 --width;
1127 ADDW (c);
1128 c = inchar ();
1130 if (width != 0 && TOLOWER (c) == L_('x'))
1132 if (base == 0)
1133 base = 16;
1134 if (base == 16)
1136 if (width > 0)
1137 --width;
1138 c = inchar ();
1141 else if (base == 0)
1142 base = 8;
1145 if (base == 0)
1146 base = 10;
1148 if (base == 10 && __builtin_expect ((flags & I18N) != 0, 0))
1150 int from_level;
1151 int to_level;
1152 int level;
1153 #ifdef COMPILE_WSCANF
1154 const wchar_t *wcdigits[10];
1155 const wchar_t *wcdigits_extended[10];
1156 #else
1157 const char *mbdigits[10];
1158 const char *mbdigits_extended[10];
1159 #endif
1160 /* "to_inpunct" is a map from ASCII digits to their
1161 equivalent in locale. This is defined for locales
1162 which use an extra digits set. */
1163 wctrans_t map = __wctrans ("to_inpunct");
1164 int n;
1166 from_level = 0;
1167 #ifdef COMPILE_WSCANF
1168 to_level = _NL_CURRENT_WORD (LC_CTYPE,
1169 _NL_CTYPE_INDIGITS_WC_LEN) - 1;
1170 #else
1171 to_level = (uint32_t) curctype->values[_NL_ITEM_INDEX (_NL_CTYPE_INDIGITS_MB_LEN)].word - 1;
1172 #endif
1174 /* Get the alternative digit forms if there are any. */
1175 if (__builtin_expect (map != NULL, 0))
1177 /* Adding new level for extra digits set in locale file. */
1178 ++to_level;
1180 for (n = 0; n < 10; ++n)
1182 #ifdef COMPILE_WSCANF
1183 wcdigits[n] = (const wchar_t *)
1184 _NL_CURRENT (LC_CTYPE, _NL_CTYPE_INDIGITS0_WC + n);
1186 wchar_t *wc_extended = (wchar_t *)
1187 alloca ((to_level + 2) * sizeof (wchar_t));
1188 __wmemcpy (wc_extended, wcdigits[n], to_level);
1189 wc_extended[to_level] = __towctrans (L'0' + n, map);
1190 wc_extended[to_level + 1] = '\0';
1191 wcdigits_extended[n] = wc_extended;
1192 #else
1193 mbdigits[n]
1194 = curctype->values[_NL_CTYPE_INDIGITS0_MB + n].string;
1196 /* Get the equivalent wide char in map. */
1197 wint_t extra_wcdigit = __towctrans (L'0' + n, map);
1199 /* Convert it to multibyte representation. */
1200 mbstate_t state;
1201 memset (&state, '\0', sizeof (state));
1203 char extra_mbdigit[MB_LEN_MAX];
1204 size_t mblen
1205 = __wcrtomb (extra_mbdigit, extra_wcdigit, &state);
1207 if (mblen == (size_t) -1)
1209 /* Ignore this new level. */
1210 map = NULL;
1211 break;
1214 /* Calculate the length of mbdigits[n]. */
1215 const char *last_char = mbdigits[n];
1216 for (level = 0; level < to_level; ++level)
1217 last_char = strchr (last_char, '\0') + 1;
1219 size_t mbdigits_len = last_char - mbdigits[n];
1221 /* Allocate memory for extended multibyte digit. */
1222 char *mb_extended;
1223 mb_extended = (char *) alloca (mbdigits_len + mblen + 1);
1225 /* And get the mbdigits + extra_digit string. */
1226 *(char *) __mempcpy (__mempcpy (mb_extended, mbdigits[n],
1227 mbdigits_len),
1228 extra_mbdigit, mblen) = '\0';
1229 mbdigits_extended[n] = mb_extended;
1230 #endif
1234 /* Read the number into workspace. */
1235 while (c != EOF && width != 0)
1237 /* In this round we get the pointer to the digit strings
1238 and also perform the first round of comparisons. */
1239 for (n = 0; n < 10; ++n)
1241 /* Get the string for the digits with value N. */
1242 #ifdef COMPILE_WSCANF
1243 if (__builtin_expect (map != NULL, 0))
1244 wcdigits[n] = wcdigits_extended[n];
1245 else
1246 wcdigits[n] = (const wchar_t *)
1247 _NL_CURRENT (LC_CTYPE, _NL_CTYPE_INDIGITS0_WC + n);
1248 wcdigits[n] += from_level;
1250 if (c == (wint_t) *wcdigits[n])
1252 to_level = from_level;
1253 break;
1256 /* Advance the pointer to the next string. */
1257 ++wcdigits[n];
1258 #else
1259 const char *cmpp;
1260 int avail = width > 0 ? width : INT_MAX;
1262 if (__builtin_expect (map != NULL, 0))
1263 mbdigits[n] = mbdigits_extended[n];
1264 else
1265 mbdigits[n]
1266 = curctype->values[_NL_CTYPE_INDIGITS0_MB + n].string;
1268 for (level = 0; level < from_level; level++)
1269 mbdigits[n] = strchr (mbdigits[n], '\0') + 1;
1271 cmpp = mbdigits[n];
1272 while ((unsigned char) *cmpp == c && avail > 0)
1274 if (*++cmpp == '\0')
1275 break;
1276 else
1278 if ((c = inchar ()) == EOF)
1279 break;
1280 --avail;
1284 if (*cmpp == '\0')
1286 if (width > 0)
1287 width = avail;
1288 to_level = from_level;
1289 break;
1292 /* We are pushing all read characters back. */
1293 if (cmpp > mbdigits[n])
1295 ungetc (c, s);
1296 while (--cmpp > mbdigits[n])
1297 ungetc_not_eof ((unsigned char) *cmpp, s);
1298 c = (unsigned char) *cmpp;
1301 /* Advance the pointer to the next string. */
1302 mbdigits[n] = strchr (mbdigits[n], '\0') + 1;
1303 #endif
1306 if (n == 10)
1308 /* Have not yet found the digit. */
1309 for (level = from_level + 1; level <= to_level; ++level)
1311 /* Search all ten digits of this level. */
1312 for (n = 0; n < 10; ++n)
1314 #ifdef COMPILE_WSCANF
1315 if (c == (wint_t) *wcdigits[n])
1316 break;
1318 /* Advance the pointer to the next string. */
1319 ++wcdigits[n];
1320 #else
1321 const char *cmpp;
1322 int avail = width > 0 ? width : INT_MAX;
1324 cmpp = mbdigits[n];
1325 while ((unsigned char) *cmpp == c && avail > 0)
1327 if (*++cmpp == '\0')
1328 break;
1329 else
1331 if ((c = inchar ()) == EOF)
1332 break;
1333 --avail;
1337 if (*cmpp == '\0')
1339 if (width > 0)
1340 width = avail;
1341 break;
1344 /* We are pushing all read characters back. */
1345 if (cmpp > mbdigits[n])
1347 ungetc (c, s);
1348 while (--cmpp > mbdigits[n])
1349 ungetc_not_eof ((unsigned char) *cmpp, s);
1350 c = (unsigned char) *cmpp;
1353 /* Advance the pointer to the next string. */
1354 mbdigits[n] = strchr (mbdigits[n], '\0') + 1;
1355 #endif
1358 if (n < 10)
1360 /* Found it. */
1361 from_level = level;
1362 to_level = level;
1363 break;
1368 if (n < 10)
1369 c = L_('0') + n;
1370 else if ((flags & GROUP)
1371 #ifdef COMPILE_WSCANF
1372 && thousands != L'\0'
1373 #else
1374 && thousands != NULL
1375 #endif
1378 /* Try matching against the thousands separator. */
1379 #ifdef COMPILE_WSCANF
1380 if (c != thousands)
1381 break;
1382 #else
1383 const char *cmpp = thousands;
1384 int avail = width > 0 ? width : INT_MAX;
1386 while ((unsigned char) *cmpp == c && avail > 0)
1388 ADDW (c);
1389 if (*++cmpp == '\0')
1390 break;
1391 else
1393 if ((c = inchar ()) == EOF)
1394 break;
1395 --avail;
1399 if (*cmpp != '\0')
1401 /* We are pushing all read characters back. */
1402 if (cmpp > thousands)
1404 wpsize -= cmpp - thousands;
1405 ungetc (c, s);
1406 while (--cmpp > thousands)
1407 ungetc_not_eof ((unsigned char) *cmpp, s);
1408 c = (unsigned char) *cmpp;
1410 break;
1413 if (width > 0)
1414 width = avail;
1416 /* The last thousands character will be added back by
1417 the ADDW below. */
1418 --wpsize;
1419 #endif
1421 else
1422 break;
1424 ADDW (c);
1425 if (width > 0)
1426 --width;
1428 c = inchar ();
1431 else
1432 /* Read the number into workspace. */
1433 while (c != EOF && width != 0)
1435 if (base == 16)
1437 if (!ISXDIGIT (c))
1438 break;
1440 else if (!ISDIGIT (c) || (int) (c - L_('0')) >= base)
1442 if (base == 10 && (flags & GROUP)
1443 #ifdef COMPILE_WSCANF
1444 && thousands != L'\0'
1445 #else
1446 && thousands != NULL
1447 #endif
1450 /* Try matching against the thousands separator. */
1451 #ifdef COMPILE_WSCANF
1452 if (c != thousands)
1453 break;
1454 #else
1455 const char *cmpp = thousands;
1456 int avail = width > 0 ? width : INT_MAX;
1458 while ((unsigned char) *cmpp == c && avail > 0)
1460 ADDW (c);
1461 if (*++cmpp == '\0')
1462 break;
1463 else
1465 if ((c = inchar ()) == EOF)
1466 break;
1467 --avail;
1471 if (*cmpp != '\0')
1473 /* We are pushing all read characters back. */
1474 if (cmpp > thousands)
1476 wpsize -= cmpp - thousands;
1477 ungetc (c, s);
1478 while (--cmpp > thousands)
1479 ungetc_not_eof ((unsigned char) *cmpp, s);
1480 c = (unsigned char) *cmpp;
1482 break;
1485 if (width > 0)
1486 width = avail;
1488 /* The last thousands character will be added back by
1489 the ADDW below. */
1490 --wpsize;
1491 #endif
1493 else
1494 break;
1496 ADDW (c);
1497 if (width > 0)
1498 --width;
1500 c = inchar ();
1503 if (wpsize == 0
1504 || (wpsize == 1 && (wp[0] == L_('+') || wp[0] == L_('-'))))
1506 /* There was no number. If we are supposed to read a pointer
1507 we must recognize "(nil)" as well. */
1508 if (__builtin_expect (wpsize == 0
1509 && read_pointer
1510 && (width < 0 || width >= 0)
1511 && c == '('
1512 && TOLOWER (inchar ()) == L_('n')
1513 && TOLOWER (inchar ()) == L_('i')
1514 && TOLOWER (inchar ()) == L_('l')
1515 && inchar () == L_(')'), 1))
1516 /* We must produce the value of a NULL pointer. A single
1517 '0' digit is enough. */
1518 ADDW (L_('0'));
1519 else
1521 /* The last read character is not part of the number
1522 anymore. */
1523 ungetc (c, s);
1525 conv_error ();
1528 else
1529 /* The just read character is not part of the number anymore. */
1530 ungetc (c, s);
1532 /* Convert the number. */
1533 ADDW (L_('\0'));
1534 if (need_longlong && (flags & LONGDBL))
1536 if (number_signed)
1537 num.q = __strtoll_internal (wp, &tw, base, flags & GROUP);
1538 else
1539 num.uq = __strtoull_internal (wp, &tw, base, flags & GROUP);
1541 else
1543 if (number_signed)
1544 num.l = __strtol_internal (wp, &tw, base, flags & GROUP);
1545 else
1546 num.ul = __strtoul_internal (wp, &tw, base, flags & GROUP);
1548 if (__builtin_expect (wp == tw, 0))
1549 conv_error ();
1551 if (!(flags & SUPPRESS))
1553 if (! number_signed)
1555 if (need_longlong && (flags & LONGDBL))
1556 *ARG (unsigned LONGLONG int *) = num.uq;
1557 else if (need_long && (flags & LONG))
1558 *ARG (unsigned long int *) = num.ul;
1559 else if (flags & SHORT)
1560 *ARG (unsigned short int *)
1561 = (unsigned short int) num.ul;
1562 else if (!(flags & CHAR))
1563 *ARG (unsigned int *) = (unsigned int) num.ul;
1564 else
1565 *ARG (unsigned char *) = (unsigned char) num.ul;
1567 else
1569 if (need_longlong && (flags & LONGDBL))
1570 *ARG (LONGLONG int *) = num.q;
1571 else if (need_long && (flags & LONG))
1572 *ARG (long int *) = num.l;
1573 else if (flags & SHORT)
1574 *ARG (short int *) = (short int) num.l;
1575 else if (!(flags & CHAR))
1576 *ARG (int *) = (int) num.l;
1577 else
1578 *ARG (signed char *) = (signed char) num.ul;
1580 ++done;
1582 break;
1584 case L_('e'): /* Floating-point numbers. */
1585 case L_('E'):
1586 case L_('f'):
1587 case L_('F'):
1588 case L_('g'):
1589 case L_('G'):
1590 case L_('a'):
1591 case L_('A'):
1592 c = inchar ();
1593 if (__builtin_expect (c == EOF, 0))
1594 input_error ();
1596 got_dot = got_e = 0;
1598 /* Check for a sign. */
1599 if (c == L_('-') || c == L_('+'))
1601 negative = c == L_('-');
1602 if (__builtin_expect (width == 0 || inchar () == EOF, 0))
1603 /* EOF is only an input error before we read any chars. */
1604 conv_error ();
1605 if (! ISDIGIT (c) && TOLOWER (c) != L_('i')
1606 && TOLOWER (c) != L_('n'))
1608 #ifdef COMPILE_WSCANF
1609 if (__builtin_expect (c != decimal, 0))
1611 /* This is no valid number. */
1612 ungetc (c, s);
1613 conv_error ();
1615 #else
1616 /* Match against the decimal point. At this point
1617 we are taking advantage of the fact that we can
1618 push more than one character back. This is
1619 (almost) never necessary since the decimal point
1620 string hopefully never contains more than one
1621 byte. */
1622 const char *cmpp = decimal;
1623 int avail = width > 0 ? width : INT_MAX;
1625 while ((unsigned char) *cmpp == c && avail-- > 0)
1626 if (*++cmpp == '\0')
1627 break;
1628 else
1630 if (inchar () == EOF)
1631 break;
1634 if (__builtin_expect (*cmpp != '\0', 0))
1636 /* This is no valid number. */
1637 while (1)
1639 ungetc (c, s);
1640 if (cmpp == decimal)
1641 break;
1642 c = (unsigned char) *--cmpp;
1645 conv_error ();
1647 else
1649 /* Add all the characters. */
1650 for (cmpp = decimal; *cmpp != '\0'; ++cmpp)
1651 ADDW ((unsigned char) *cmpp);
1652 if (width > 0)
1653 width = avail;
1654 got_dot = 1;
1656 c = inchar ();
1658 if (width > 0)
1659 width = avail;
1660 #endif
1662 if (width > 0)
1663 --width;
1665 else
1666 negative = 0;
1668 /* Take care for the special arguments "nan" and "inf". */
1669 if (TOLOWER (c) == L_('n'))
1671 /* Maybe "nan". */
1672 ADDW (c);
1673 if (__builtin_expect (width == 0
1674 || inchar () == EOF
1675 || TOLOWER (c) != L_('a'), 0))
1676 conv_error ();
1677 if (width > 0)
1678 --width;
1679 ADDW (c);
1680 if (__builtin_expect (width == 0
1681 || inchar () == EOF
1682 || TOLOWER (c) != L_('n'), 0))
1683 conv_error ();
1684 if (width > 0)
1685 --width;
1686 ADDW (c);
1687 /* It is "nan". */
1688 goto scan_float;
1690 else if (TOLOWER (c) == L_('i'))
1692 /* Maybe "inf" or "infinity". */
1693 ADDW (c);
1694 if (__builtin_expect (width == 0
1695 || inchar () == EOF
1696 || TOLOWER (c) != L_('n'), 0))
1697 conv_error ();
1698 if (width > 0)
1699 --width;
1700 ADDW (c);
1701 if (__builtin_expect (width == 0
1702 || inchar () == EOF
1703 || TOLOWER (c) != L_('f'), 0))
1704 conv_error ();
1705 if (width > 0)
1706 --width;
1707 ADDW (c);
1708 /* It is as least "inf". */
1709 if (width != 0 && inchar () != EOF)
1711 if (TOLOWER (c) == L_('i'))
1713 if (width > 0)
1714 --width;
1715 /* Now we have to read the rest as well. */
1716 ADDW (c);
1717 if (__builtin_expect (width == 0
1718 || inchar () == EOF
1719 || TOLOWER (c) != L_('n'), 0))
1720 conv_error ();
1721 if (width > 0)
1722 --width;
1723 ADDW (c);
1724 if (__builtin_expect (width == 0
1725 || inchar () == EOF
1726 || TOLOWER (c) != L_('i'), 0))
1727 conv_error ();
1728 if (width > 0)
1729 --width;
1730 ADDW (c);
1731 if (__builtin_expect (width == 0
1732 || inchar () == EOF
1733 || TOLOWER (c) != L_('t'), 0))
1734 conv_error ();
1735 if (width > 0)
1736 --width;
1737 ADDW (c);
1738 if (__builtin_expect (width == 0
1739 || inchar () == EOF
1740 || TOLOWER (c) != L_('y'), 0))
1741 conv_error ();
1742 if (width > 0)
1743 --width;
1744 ADDW (c);
1746 else
1747 /* Never mind. */
1748 ungetc (c, s);
1750 goto scan_float;
1753 is_hexa = 0;
1754 exp_char = L_('e');
1755 if (width != 0 && c == L_('0'))
1757 ADDW (c);
1758 c = inchar ();
1759 if (width > 0)
1760 --width;
1761 if (width != 0 && TOLOWER (c) == L_('x'))
1763 /* It is a number in hexadecimal format. */
1764 ADDW (c);
1766 is_hexa = 1;
1767 exp_char = L_('p');
1769 /* Grouping is not allowed. */
1770 flags &= ~GROUP;
1771 c = inchar ();
1772 if (width > 0)
1773 --width;
1779 if (ISDIGIT (c))
1780 ADDW (c);
1781 else if (!got_e && is_hexa && ISXDIGIT (c))
1782 ADDW (c);
1783 else if (got_e && wp[wpsize - 1] == exp_char
1784 && (c == L_('-') || c == L_('+')))
1785 ADDW (c);
1786 else if (wpsize > 0 && !got_e
1787 && (CHAR_T) TOLOWER (c) == exp_char)
1789 ADDW (exp_char);
1790 got_e = got_dot = 1;
1792 else
1794 #ifdef COMPILE_WSCANF
1795 if (! got_dot && c == decimal)
1797 ADDW (c);
1798 got_dot = 1;
1800 else if ((flags & GROUP) != 0 && thousands != L'\0'
1801 && ! got_dot && c == thousands)
1802 ADDW (c);
1803 else
1805 /* The last read character is not part of the number
1806 anymore. */
1807 ungetc (c, s);
1808 break;
1810 #else
1811 const char *cmpp = decimal;
1812 int avail = width > 0 ? width : INT_MAX;
1814 if (! got_dot)
1816 while ((unsigned char) *cmpp == c && avail > 0)
1817 if (*++cmpp == '\0')
1818 break;
1819 else
1821 if (inchar () == EOF)
1822 break;
1823 --avail;
1827 if (*cmpp == '\0')
1829 /* Add all the characters. */
1830 for (cmpp = decimal; *cmpp != '\0'; ++cmpp)
1831 ADDW ((unsigned char) *cmpp);
1832 if (width > 0)
1833 width = avail;
1834 got_dot = 1;
1836 else
1838 /* Figure out whether it is a thousands separator.
1839 There is one problem: we possibly read more than
1840 one character. We cannot push them back but since
1841 we know that parts of the `decimal' string matched,
1842 we can compare against it. */
1843 const char *cmp2p = thousands;
1845 if ((flags & GROUP) != 0 && thousands != NULL
1846 && ! got_dot)
1848 while (cmp2p - thousands < cmpp - decimal
1849 && *cmp2p == decimal[cmp2p - thousands])
1850 ++cmp2p;
1851 if (cmp2p - thousands == cmpp - decimal)
1853 while ((unsigned char) *cmp2p == c && avail > 0)
1854 if (*++cmp2p == '\0')
1855 break;
1856 else
1858 if (inchar () == EOF)
1859 break;
1860 --avail;
1865 if (cmp2p != NULL && *cmp2p == '\0')
1867 /* Add all the characters. */
1868 for (cmpp = thousands; *cmpp != '\0'; ++cmpp)
1869 ADDW ((unsigned char) *cmpp);
1870 if (width > 0)
1871 width = avail;
1873 else
1875 /* The last read character is not part of the number
1876 anymore. */
1877 ungetc (c, s);
1878 break;
1881 #endif
1883 if (width > 0)
1884 --width;
1886 while (width != 0 && inchar () != EOF);
1888 /* Have we read any character? If we try to read a number
1889 in hexadecimal notation and we have read only the `0x'
1890 prefix this is an error. */
1891 if (__builtin_expect (wpsize == 0 || (is_hexa && wpsize == 2), 0))
1892 conv_error ();
1894 scan_float:
1895 /* Convert the number. */
1896 ADDW (L_('\0'));
1897 if ((flags & LONGDBL) && !__ldbl_is_dbl)
1899 long double d = __strtold_internal (wp, &tw, flags & GROUP);
1900 if (!(flags & SUPPRESS) && tw != wp)
1901 *ARG (long double *) = negative ? -d : d;
1903 else if (flags & (LONG | LONGDBL))
1905 double d = __strtod_internal (wp, &tw, flags & GROUP);
1906 if (!(flags & SUPPRESS) && tw != wp)
1907 *ARG (double *) = negative ? -d : d;
1909 else
1911 float d = __strtof_internal (wp, &tw, flags & GROUP);
1912 if (!(flags & SUPPRESS) && tw != wp)
1913 *ARG (float *) = negative ? -d : d;
1916 if (__builtin_expect (tw == wp, 0))
1917 conv_error ();
1919 if (!(flags & SUPPRESS))
1920 ++done;
1921 break;
1923 case L_('['): /* Character class. */
1924 if (flags & LONG)
1925 STRING_ARG (wstr, wchar_t);
1926 else
1927 STRING_ARG (str, char);
1929 if (*f == L_('^'))
1931 ++f;
1932 not_in = 1;
1934 else
1935 not_in = 0;
1937 if (width < 0)
1938 /* There is no width given so there is also no limit on the
1939 number of characters we read. Therefore we set width to
1940 a very high value to make the algorithm easier. */
1941 width = INT_MAX;
1943 #ifdef COMPILE_WSCANF
1944 /* Find the beginning and the end of the scanlist. We are not
1945 creating a lookup table since it would have to be too large.
1946 Instead we search each time through the string. This is not
1947 a constant lookup time but who uses this feature deserves to
1948 be punished. */
1949 tw = (wchar_t *) f; /* Marks the beginning. */
1951 if (*f == L']')
1952 ++f;
1954 while ((fc = *f++) != L'\0' && fc != L']');
1956 if (__builtin_expect (fc == L'\0', 0))
1957 conv_error ();
1958 wp = (wchar_t *) f - 1;
1959 #else
1960 /* Fill WP with byte flags indexed by character.
1961 We will use this flag map for matching input characters. */
1962 if (wpmax < UCHAR_MAX + 1)
1964 wpmax = UCHAR_MAX + 1;
1965 wp = (char *) alloca (wpmax);
1967 memset (wp, '\0', UCHAR_MAX + 1);
1969 fc = *f;
1970 if (fc == ']' || fc == '-')
1972 /* If ] or - appears before any char in the set, it is not
1973 the terminator or separator, but the first char in the
1974 set. */
1975 wp[fc] = 1;
1976 ++f;
1979 while ((fc = *f++) != '\0' && fc != ']')
1980 if (fc == '-' && *f != '\0' && *f != ']'
1981 && (unsigned char) f[-2] <= (unsigned char) *f)
1983 /* Add all characters from the one before the '-'
1984 up to (but not including) the next format char. */
1985 for (fc = (unsigned char) f[-2]; fc < (unsigned char) *f; ++fc)
1986 wp[fc] = 1;
1988 else
1989 /* Add the character to the flag map. */
1990 wp[fc] = 1;
1992 if (__builtin_expect (fc == '\0', 0))
1993 conv_error();
1994 #endif
1996 if (flags & LONG)
1998 size_t now = read_in;
1999 #ifdef COMPILE_WSCANF
2000 if (__builtin_expect (inchar () == WEOF, 0))
2001 input_error ();
2005 wchar_t *runp;
2007 /* Test whether it's in the scanlist. */
2008 runp = tw;
2009 while (runp < wp)
2011 if (runp[0] == L'-' && runp[1] != '\0' && runp + 1 != wp
2012 && runp != tw
2013 && (unsigned int) runp[-1] <= (unsigned int) runp[1])
2015 /* Match against all characters in between the
2016 first and last character of the sequence. */
2017 wchar_t wc;
2019 for (wc = runp[-1] + 1; wc <= runp[1]; ++wc)
2020 if ((wint_t) wc == c)
2021 break;
2023 if (wc <= runp[1] && !not_in)
2024 break;
2025 if (wc <= runp[1] && not_in)
2027 /* The current character is not in the
2028 scanset. */
2029 ungetc (c, s);
2030 goto out;
2033 runp += 2;
2035 else
2037 if ((wint_t) *runp == c && !not_in)
2038 break;
2039 if ((wint_t) *runp == c && not_in)
2041 ungetc (c, s);
2042 goto out;
2045 ++runp;
2049 if (runp == wp && !not_in)
2051 ungetc (c, s);
2052 goto out;
2055 if (!(flags & SUPPRESS))
2057 *wstr++ = c;
2059 if ((flags & MALLOC)
2060 && wstr == (wchar_t *) *strptr + strsize)
2062 /* Enlarge the buffer. */
2063 wstr = (wchar_t *) realloc (*strptr,
2064 (2 * strsize)
2065 * sizeof (wchar_t));
2066 if (wstr == NULL)
2068 /* Can't allocate that much. Last-ditch
2069 effort. */
2070 wstr = (wchar_t *)
2071 realloc (*strptr, (strsize + 1)
2072 * sizeof (wchar_t));
2073 if (wstr == NULL)
2075 /* We lose. Oh well. Terminate the string
2076 and stop converting, so at least we don't
2077 skip any input. */
2078 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
2079 ++done;
2080 conv_error ();
2082 else
2084 *strptr = (char *) wstr;
2085 wstr += strsize;
2086 ++strsize;
2089 else
2091 *strptr = (char *) wstr;
2092 wstr += strsize;
2093 strsize *= 2;
2098 while (--width > 0 && inchar () != WEOF);
2099 out:
2100 #else
2101 char buf[MB_LEN_MAX];
2102 size_t cnt = 0;
2103 mbstate_t cstate;
2105 if (__builtin_expect (inchar () == EOF, 0))
2106 input_error ();
2108 memset (&cstate, '\0', sizeof (cstate));
2112 if (wp[c] == not_in)
2114 ungetc_not_eof (c, s);
2115 break;
2118 /* This is easy. */
2119 if (!(flags & SUPPRESS))
2121 size_t n;
2123 /* Convert it into a wide character. */
2124 buf[0] = c;
2125 n = __mbrtowc (wstr, buf, 1, &cstate);
2127 if (n == (size_t) -2)
2129 /* Possibly correct character, just not enough
2130 input. */
2131 ++cnt;
2132 assert (cnt < MB_CUR_MAX);
2133 continue;
2135 cnt = 0;
2137 ++wstr;
2138 if ((flags & MALLOC)
2139 && wstr == (wchar_t *) *strptr + strsize)
2141 /* Enlarge the buffer. */
2142 wstr = (wchar_t *) realloc (*strptr,
2143 (2 * strsize
2144 * sizeof (wchar_t)));
2145 if (wstr == NULL)
2147 /* Can't allocate that much. Last-ditch
2148 effort. */
2149 wstr = (wchar_t *)
2150 realloc (*strptr, ((strsize + 1)
2151 * sizeof (wchar_t)));
2152 if (wstr == NULL)
2154 /* We lose. Oh well. Terminate the
2155 string and stop converting,
2156 so at least we don't skip any input. */
2157 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
2158 ++done;
2159 conv_error ();
2161 else
2163 *strptr = (char *) wstr;
2164 wstr += strsize;
2165 ++strsize;
2168 else
2170 *strptr = (char *) wstr;
2171 wstr += strsize;
2172 strsize *= 2;
2177 if (--width <= 0)
2178 break;
2180 while (inchar () != EOF);
2182 if (__builtin_expect (cnt != 0, 0))
2183 /* We stopped in the middle of recognizing another
2184 character. That's a problem. */
2185 encode_error ();
2186 #endif
2188 if (__builtin_expect (now == read_in, 0))
2189 /* We haven't succesfully read any character. */
2190 conv_error ();
2192 if (!(flags & SUPPRESS))
2194 *wstr++ = L'\0';
2196 if ((flags & MALLOC)
2197 && wstr - (wchar_t *) *strptr != strsize)
2199 wchar_t *cp = (wchar_t *)
2200 realloc (*strptr, ((wstr - (wchar_t *) *strptr)
2201 * sizeof(wchar_t)));
2202 if (cp != NULL)
2203 *strptr = (char *) cp;
2206 ++done;
2209 else
2211 size_t now = read_in;
2213 if (__builtin_expect (inchar () == EOF, 0))
2214 input_error ();
2216 #ifdef COMPILE_WSCANF
2218 memset (&state, '\0', sizeof (state));
2222 wchar_t *runp;
2223 size_t n;
2225 /* Test whether it's in the scanlist. */
2226 runp = tw;
2227 while (runp < wp)
2229 if (runp[0] == L'-' && runp[1] != '\0' && runp + 1 != wp
2230 && runp != tw
2231 && (unsigned int) runp[-1] <= (unsigned int) runp[1])
2233 /* Match against all characters in between the
2234 first and last character of the sequence. */
2235 wchar_t wc;
2237 for (wc = runp[-1] + 1; wc <= runp[1]; ++wc)
2238 if ((wint_t) wc == c)
2239 break;
2241 if (wc <= runp[1] && !not_in)
2242 break;
2243 if (wc <= runp[1] && not_in)
2245 /* The current character is not in the
2246 scanset. */
2247 ungetc (c, s);
2248 goto out2;
2251 runp += 2;
2253 else
2255 if ((wint_t) *runp == c && !not_in)
2256 break;
2257 if ((wint_t) *runp == c && not_in)
2259 ungetc (c, s);
2260 goto out2;
2263 ++runp;
2267 if (runp == wp && !not_in)
2269 ungetc (c, s);
2270 goto out2;
2273 if (!(flags & SUPPRESS))
2275 if ((flags & MALLOC)
2276 && str + MB_CUR_MAX >= *strptr + strsize)
2278 /* Enlarge the buffer. */
2279 size_t strleng = str - *strptr;
2280 char *newstr;
2282 newstr = (char *) realloc (*strptr, 2 * strsize);
2283 if (newstr == NULL)
2285 /* Can't allocate that much. Last-ditch
2286 effort. */
2287 newstr = (char *) realloc (*strptr,
2288 strleng + MB_CUR_MAX);
2289 if (newstr == NULL)
2291 /* We lose. Oh well. Terminate the string
2292 and stop converting, so at least we don't
2293 skip any input. */
2294 ((char *) (*strptr))[strleng] = '\0';
2295 ++done;
2296 conv_error ();
2298 else
2300 *strptr = newstr;
2301 str = newstr + strleng;
2302 strsize = strleng + MB_CUR_MAX;
2305 else
2307 *strptr = newstr;
2308 str = newstr + strleng;
2309 strsize *= 2;
2314 n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c, &state);
2315 if (__builtin_expect (n == (size_t) -1, 0))
2316 encode_error ();
2318 assert (n <= MB_CUR_MAX);
2319 str += n;
2321 while (--width > 0 && inchar () != WEOF);
2322 out2:
2323 #else
2326 if (wp[c] == not_in)
2328 ungetc_not_eof (c, s);
2329 break;
2332 /* This is easy. */
2333 if (!(flags & SUPPRESS))
2335 *str++ = c;
2336 if ((flags & MALLOC)
2337 && (char *) str == *strptr + strsize)
2339 /* Enlarge the buffer. */
2340 size_t newsize = 2 * strsize;
2342 allocagain:
2343 str = (char *) realloc (*strptr, newsize);
2344 if (str == NULL)
2346 /* Can't allocate that much. Last-ditch
2347 effort. */
2348 if (newsize > strsize + 1)
2350 newsize = strsize + 1;
2351 goto allocagain;
2353 /* We lose. Oh well. Terminate the
2354 string and stop converting,
2355 so at least we don't skip any input. */
2356 ((char *) (*strptr))[strsize - 1] = '\0';
2357 ++done;
2358 conv_error ();
2360 else
2362 *strptr = (char *) str;
2363 str += strsize;
2364 strsize = newsize;
2369 while (--width > 0 && inchar () != EOF);
2370 #endif
2372 if (__builtin_expect (now == read_in, 0))
2373 /* We haven't succesfully read any character. */
2374 conv_error ();
2376 if (!(flags & SUPPRESS))
2378 #ifdef COMPILE_WSCANF
2379 /* We have to emit the code to get into the initial
2380 state. */
2381 char buf[MB_LEN_MAX];
2382 size_t n = __wcrtomb (buf, L'\0', &state);
2383 if (n > 0 && (flags & MALLOC)
2384 && str + n >= *strptr + strsize)
2386 /* Enlarge the buffer. */
2387 size_t strleng = str - *strptr;
2388 char *newstr;
2390 newstr = (char *) realloc (*strptr, strleng + n + 1);
2391 if (newstr == NULL)
2393 /* We lose. Oh well. Terminate the string
2394 and stop converting, so at least we don't
2395 skip any input. */
2396 ((char *) (*strptr))[strleng] = '\0';
2397 ++done;
2398 conv_error ();
2400 else
2402 *strptr = newstr;
2403 str = newstr + strleng;
2404 strsize = strleng + n + 1;
2408 str = __mempcpy (str, buf, n);
2409 #endif
2410 *str++ = '\0';
2412 if ((flags & MALLOC) && str - *strptr != strsize)
2414 char *cp = (char *) realloc (*strptr, str - *strptr);
2415 if (cp != NULL)
2416 *strptr = cp;
2419 ++done;
2422 break;
2424 case L_('p'): /* Generic pointer. */
2425 base = 16;
2426 /* A PTR must be the same size as a `long int'. */
2427 flags &= ~(SHORT|LONGDBL);
2428 if (need_long)
2429 flags |= LONG;
2430 number_signed = 0;
2431 read_pointer = 1;
2432 goto number;
2434 default:
2435 /* If this is an unknown format character punt. */
2436 conv_error ();
2440 /* The last thing we saw int the format string was a white space.
2441 Consume the last white spaces. */
2442 if (skip_space)
2445 c = inchar ();
2446 while (ISSPACE (c));
2447 ungetc (c, s);
2450 errout:
2451 /* Unlock stream. */
2452 UNLOCK_STREAM (s);
2454 if (errp != NULL)
2455 *errp |= errval;
2457 return done;
2460 #ifdef COMPILE_WSCANF
2462 __vfwscanf (FILE *s, const wchar_t *format, va_list argptr)
2464 return _IO_vfwscanf (s, format, argptr, NULL);
2466 ldbl_weak_alias (__vfwscanf, vfwscanf)
2467 #else
2469 ___vfscanf (FILE *s, const char *format, va_list argptr)
2471 return _IO_vfscanf_internal (s, format, argptr, NULL);
2473 ldbl_strong_alias (_IO_vfscanf_internal, _IO_vfscanf)
2474 ldbl_strong_alias (___vfscanf, __vfscanf)
2475 ldbl_hidden_def (___vfscanf, __vfscanf)
2476 ldbl_weak_alias (___vfscanf, vfscanf)
2477 #endif