* posix/tst-rfc3484.c: Include <ifaddrs.h> early.
[glibc.git] / stdio-common / vfscanf.c
blob4dd776843150efc505a3ccf2e75a51d113e01715
1 /* Copyright (C) 1991-2002, 2003, 2004, 2005, 2006
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <ctype.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <wchar.h>
30 #include <wctype.h>
31 #include <bits/libc-lock.h>
32 #include <locale/localeinfo.h>
34 #ifdef __GNUC__
35 # define HAVE_LONGLONG
36 # define LONGLONG long long
37 #else
38 # define LONGLONG long
39 #endif
41 /* Determine whether we have to handle `long long' at all. */
42 #if LONG_MAX == LONG_LONG_MAX
43 # define need_longlong 0
44 #else
45 # define need_longlong 1
46 #endif
48 /* Determine whether we have to handle `long'. */
49 #if INT_MAX == LONG_MAX
50 # define need_long 0
51 #else
52 # define need_long 1
53 #endif
55 /* Those are flags in the conversion format. */
56 #define LONG 0x001 /* l: long or double */
57 #define LONGDBL 0x002 /* L: long long or long double */
58 #define SHORT 0x004 /* h: short */
59 #define SUPPRESS 0x008 /* *: suppress assignment */
60 #define POINTER 0x010 /* weird %p pointer (`fake hex') */
61 #define NOSKIP 0x020 /* do not skip blanks */
62 #define WIDTH 0x040 /* width was given */
63 #define GROUP 0x080 /* ': group numbers */
64 #define MALLOC 0x100 /* a: malloc strings */
65 #define CHAR 0x200 /* hh: char */
66 #define I18N 0x400 /* I: use locale's digits */
69 #include <locale/localeinfo.h>
70 #include <libioP.h>
71 #include <libio.h>
73 #undef va_list
74 #define va_list _IO_va_list
76 #ifdef COMPILE_WSCANF
77 # define ungetc(c, s) ((void) (c == WEOF \
78 || (--read_in, \
79 INTUSE(_IO_sputbackwc) (s, c))))
80 # define ungetc_not_eof(c, s) ((void) (--read_in, \
81 INTUSE(_IO_sputbackwc) (s, c)))
82 # define inchar() (c == WEOF ? ((errno = inchar_errno), WEOF) \
83 : ((c = _IO_getwc_unlocked (s)), \
84 (void) (c != WEOF \
85 ? ++read_in \
86 : (size_t) (inchar_errno = errno)), c))
88 # define MEMCPY(d, s, n) __wmemcpy (d, s, n)
89 # define ISSPACE(Ch) iswspace (Ch)
90 # define ISDIGIT(Ch) iswdigit (Ch)
91 # define ISXDIGIT(Ch) iswxdigit (Ch)
92 # define TOLOWER(Ch) towlower (Ch)
93 # define ORIENT if (_IO_fwide (s, 1) != 1) return WEOF
94 # define __strtoll_internal __wcstoll_internal
95 # define __strtoull_internal __wcstoull_internal
96 # define __strtol_internal __wcstol_internal
97 # define __strtoul_internal __wcstoul_internal
98 # define __strtold_internal __wcstold_internal
99 # define __strtod_internal __wcstod_internal
100 # define __strtof_internal __wcstof_internal
102 # define L_(Str) L##Str
103 # define CHAR_T wchar_t
104 # define UCHAR_T unsigned int
105 # define WINT_T wint_t
106 # undef EOF
107 # define EOF WEOF
108 #else
109 # define ungetc(c, s) ((void) ((int) c == EOF \
110 || (--read_in, \
111 INTUSE(_IO_sputbackc) (s, (unsigned char) c))))
112 # define ungetc_not_eof(c, s) ((void) (--read_in, \
113 INTUSE(_IO_sputbackc) (s, (unsigned char) c)))
114 # define inchar() (c == EOF ? ((errno = inchar_errno), EOF) \
115 : ((c = _IO_getc_unlocked (s)), \
116 (void) (c != EOF \
117 ? ++read_in \
118 : (size_t) (inchar_errno = errno)), c))
119 # define MEMCPY(d, s, n) memcpy (d, s, n)
120 # define ISSPACE(Ch) __isspace_l (Ch, loc)
121 # define ISDIGIT(Ch) __isdigit_l (Ch, loc)
122 # define ISXDIGIT(Ch) __isxdigit_l (Ch, loc)
123 # define TOLOWER(Ch) __tolower_l ((unsigned char) (Ch), loc)
124 # define ORIENT if (_IO_vtable_offset (s) == 0 \
125 && _IO_fwide (s, -1) != -1) \
126 return EOF
128 # define L_(Str) Str
129 # define CHAR_T char
130 # define UCHAR_T unsigned char
131 # define WINT_T int
132 #endif
134 #define encode_error() do { \
135 errval = 4; \
136 __set_errno (EILSEQ); \
137 goto errout; \
138 } while (0)
139 #define conv_error() do { \
140 errval = 2; \
141 goto errout; \
142 } while (0)
143 #define input_error() do { \
144 errval = 1; \
145 if (done == 0) done = EOF; \
146 goto errout; \
147 } while (0)
148 #define memory_error() do { \
149 __set_errno (ENOMEM); \
150 done = EOF; \
151 goto errout; \
152 } while (0)
153 #define ARGCHECK(s, format) \
154 do \
156 /* Check file argument for consistence. */ \
157 CHECK_FILE (s, EOF); \
158 if (s->_flags & _IO_NO_READS) \
160 __set_errno (EBADF); \
161 return EOF; \
163 else if (format == NULL) \
165 MAYBE_SET_EINVAL; \
166 return EOF; \
168 } while (0)
169 #define LOCK_STREAM(S) \
170 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, (S)); \
171 _IO_flockfile (S)
172 #define UNLOCK_STREAM(S) \
173 _IO_funlockfile (S); \
174 __libc_cleanup_region_end (0)
177 /* Read formatted input from S according to the format string
178 FORMAT, using the argument list in ARG.
179 Return the number of assignments made, or -1 for an input error. */
180 #ifdef COMPILE_WSCANF
182 _IO_vfwscanf (_IO_FILE *s, const wchar_t *format, _IO_va_list argptr,
183 int *errp)
184 #else
186 _IO_vfscanf_internal (_IO_FILE *s, const char *format, _IO_va_list argptr,
187 int *errp)
188 #endif
190 va_list arg;
191 register const CHAR_T *f = format;
192 register UCHAR_T fc; /* Current character of the format. */
193 register WINT_T done = 0; /* Assignments done. */
194 register size_t read_in = 0; /* Chars read in. */
195 register WINT_T c = 0; /* Last char read. */
196 register int width; /* Maximum field width. */
197 register int flags; /* Modifiers for current format element. */
198 int errval = 0;
199 #ifndef COMPILE_WSCANF
200 __locale_t loc = _NL_CURRENT_LOCALE;
201 struct locale_data *const curctype = loc->__locales[LC_CTYPE];
202 #endif
204 /* Errno of last failed inchar call. */
205 int inchar_errno = 0;
206 /* Status for reading F-P nums. */
207 char got_dot, got_e, negative;
208 /* If a [...] is a [^...]. */
209 CHAR_T not_in;
210 #define exp_char not_in
211 /* Base for integral numbers. */
212 int base;
213 /* Signedness for integral numbers. */
214 int number_signed;
215 #define is_hexa number_signed
216 /* Decimal point character. */
217 #ifdef COMPILE_WSCANF
218 wint_t decimal;
219 #else
220 const char *decimal;
221 #endif
222 /* The thousands character of the current locale. */
223 #ifdef COMPILE_WSCANF
224 wint_t thousands;
225 #else
226 const char *thousands;
227 #endif
228 /* State for the conversions. */
229 mbstate_t state;
230 /* Integral holding variables. */
231 union
233 long long int q;
234 unsigned long long int uq;
235 long int l;
236 unsigned long int ul;
237 } num;
238 /* Character-buffer pointer. */
239 char *str = NULL;
240 wchar_t *wstr = NULL;
241 char **strptr = NULL;
242 ssize_t strsize = 0;
243 /* We must not react on white spaces immediately because they can
244 possibly be matched even if in the input stream no character is
245 available anymore. */
246 int skip_space = 0;
247 /* Nonzero if we are reading a pointer. */
248 int read_pointer;
249 /* Workspace. */
250 CHAR_T *tw; /* Temporary pointer. */
251 CHAR_T *wp = NULL; /* Workspace. */
252 size_t wpmax = 0; /* Maximal size of workspace. */
253 size_t wpsize; /* Currently used bytes in workspace. */
254 #define ADDW(Ch) \
255 do \
257 if (wpsize == wpmax) \
259 CHAR_T *old = wp; \
260 wpmax = (UCHAR_MAX + 1 > 2 * wpmax ? UCHAR_MAX + 1 : 2 * wpmax); \
261 wp = (CHAR_T *) alloca (wpmax * sizeof (wchar_t)); \
262 if (old != NULL) \
263 MEMCPY (wp, old, wpsize); \
265 wp[wpsize++] = (Ch); \
267 while (0)
269 #ifdef __va_copy
270 __va_copy (arg, argptr);
271 #else
272 arg = (va_list) argptr;
273 #endif
275 #ifdef ORIENT
276 ORIENT;
277 #endif
279 ARGCHECK (s, format);
282 #ifndef COMPILE_WSCANF
283 struct locale_data *const curnumeric = loc->__locales[LC_NUMERIC];
284 #endif
286 /* Figure out the decimal point character. */
287 #ifdef COMPILE_WSCANF
288 decimal = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
289 #else
290 decimal = curnumeric->values[_NL_ITEM_INDEX (DECIMAL_POINT)].string;
291 #endif
292 /* Figure out the thousands separator character. */
293 #ifdef COMPILE_WSCANF
294 thousands = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_THOUSANDS_SEP_WC);
295 #else
296 thousands = curnumeric->values[_NL_ITEM_INDEX (THOUSANDS_SEP)].string;
297 if (*thousands == '\0')
298 thousands = NULL;
299 #endif
302 /* Lock the stream. */
303 LOCK_STREAM (s);
306 #ifndef COMPILE_WSCANF
307 /* From now on we use `state' to convert the format string. */
308 memset (&state, '\0', sizeof (state));
309 #endif
311 /* Run through the format string. */
312 while (*f != '\0')
314 unsigned int argpos;
315 /* Extract the next argument, which is of type TYPE.
316 For a %N$... spec, this is the Nth argument from the beginning;
317 otherwise it is the next argument after the state now in ARG. */
318 #ifdef __va_copy
319 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
320 ({ unsigned int pos = argpos; \
321 va_list arg; \
322 __va_copy (arg, argptr); \
323 while (--pos > 0) \
324 (void) va_arg (arg, void *); \
325 va_arg (arg, type); \
327 #else
328 # if 0
329 /* XXX Possible optimization. */
330 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
331 ({ va_list arg = (va_list) argptr; \
332 arg = (va_list) ((char *) arg \
333 + (argpos - 1) \
334 * __va_rounded_size (void *)); \
335 va_arg (arg, type); \
337 # else
338 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
339 ({ unsigned int pos = argpos; \
340 va_list arg = (va_list) argptr; \
341 while (--pos > 0) \
342 (void) va_arg (arg, void *); \
343 va_arg (arg, type); \
345 # endif
346 #endif
348 #ifndef COMPILE_WSCANF
349 if (!isascii ((unsigned char) *f))
351 /* Non-ASCII, may be a multibyte. */
352 int len = __mbrlen (f, strlen (f), &state);
353 if (len > 0)
357 c = inchar ();
358 if (c == EOF)
359 input_error ();
360 else if (c != (unsigned char) *f++)
362 ungetc_not_eof (c, s);
363 conv_error ();
366 while (--len > 0);
367 continue;
370 #endif
372 fc = *f++;
373 if (fc != '%')
375 /* Remember to skip spaces. */
376 if (ISSPACE (fc))
378 skip_space = 1;
379 continue;
382 /* Read a character. */
383 c = inchar ();
385 /* Characters other than format specs must just match. */
386 if (c == EOF)
387 input_error ();
389 /* We saw white space char as the last character in the format
390 string. Now it's time to skip all leading white space. */
391 if (skip_space)
393 while (ISSPACE (c))
394 if (inchar () == EOF)
395 input_error ();
396 skip_space = 0;
399 if (c != fc)
401 ungetc (c, s);
402 conv_error ();
405 continue;
408 /* This is the start of the conversion string. */
409 flags = 0;
411 /* Not yet decided whether we read a pointer or not. */
412 read_pointer = 0;
414 /* Initialize state of modifiers. */
415 argpos = 0;
417 /* Prepare temporary buffer. */
418 wpsize = 0;
420 /* Check for a positional parameter specification. */
421 if (ISDIGIT ((UCHAR_T) *f))
423 argpos = (UCHAR_T) *f++ - L_('0');
424 while (ISDIGIT ((UCHAR_T) *f))
425 argpos = argpos * 10 + ((UCHAR_T) *f++ - L_('0'));
426 if (*f == L_('$'))
427 ++f;
428 else
430 /* Oops; that was actually the field width. */
431 width = argpos;
432 flags |= WIDTH;
433 argpos = 0;
434 goto got_width;
438 /* Check for the assignment-suppressing, the number grouping flag,
439 and the signal to use the locale's digit representation. */
440 while (*f == L_('*') || *f == L_('\'') || *f == L_('I'))
441 switch (*f++)
443 case L_('*'):
444 flags |= SUPPRESS;
445 break;
446 case L_('\''):
447 flags |= GROUP;
448 break;
449 case L_('I'):
450 flags |= I18N;
451 break;
454 /* We have seen width. */
455 if (ISDIGIT ((UCHAR_T) *f))
456 flags |= WIDTH;
458 /* Find the maximum field width. */
459 width = 0;
460 while (ISDIGIT ((UCHAR_T) *f))
462 width *= 10;
463 width += (UCHAR_T) *f++ - L_('0');
465 got_width:
466 if (width == 0)
467 width = -1;
469 /* Check for type modifiers. */
470 switch (*f++)
472 case L_('h'):
473 /* ints are short ints or chars. */
474 if (*f == L_('h'))
476 ++f;
477 flags |= CHAR;
479 else
480 flags |= SHORT;
481 break;
482 case L_('l'):
483 if (*f == L_('l'))
485 /* A double `l' is equivalent to an `L'. */
486 ++f;
487 flags |= LONGDBL | LONG;
489 else
490 /* ints are long ints. */
491 flags |= LONG;
492 break;
493 case L_('q'):
494 case L_('L'):
495 /* doubles are long doubles, and ints are long long ints. */
496 flags |= LONGDBL | LONG;
497 break;
498 case L_('a'):
499 /* The `a' is used as a flag only if followed by `s', `S' or
500 `['. */
501 if (*f != L_('s') && *f != L_('S') && *f != L_('['))
503 --f;
504 break;
506 /* String conversions (%s, %[) take a `char **'
507 arg and fill it in with a malloc'd pointer. */
508 flags |= MALLOC;
509 break;
510 case L_('z'):
511 if (need_longlong && sizeof (size_t) > sizeof (unsigned long int))
512 flags |= LONGDBL;
513 else if (sizeof (size_t) > sizeof (unsigned int))
514 flags |= LONG;
515 break;
516 case L_('j'):
517 if (need_longlong && sizeof (uintmax_t) > sizeof (unsigned long int))
518 flags |= LONGDBL;
519 else if (sizeof (uintmax_t) > sizeof (unsigned int))
520 flags |= LONG;
521 break;
522 case L_('t'):
523 if (need_longlong && sizeof (ptrdiff_t) > sizeof (long int))
524 flags |= LONGDBL;
525 else if (sizeof (ptrdiff_t) > sizeof (int))
526 flags |= LONG;
527 break;
528 default:
529 /* Not a recognized modifier. Backup. */
530 --f;
531 break;
534 /* End of the format string? */
535 if (*f == L_('\0'))
536 conv_error ();
538 /* Find the conversion specifier. */
539 fc = *f++;
540 if (skip_space || (fc != L_('[') && fc != L_('c')
541 && fc != L_('C') && fc != L_('n')))
543 /* Eat whitespace. */
544 int save_errno = errno;
545 errno = 0;
547 if (inchar () == EOF && errno == EINTR)
548 input_error ();
549 while (ISSPACE (c));
550 errno = save_errno;
551 ungetc (c, s);
552 skip_space = 0;
555 switch (fc)
557 case L_('%'): /* Must match a literal '%'. */
558 c = inchar ();
559 if (c == EOF)
560 input_error ();
561 if (c != fc)
563 ungetc_not_eof (c, s);
564 conv_error ();
566 break;
568 case L_('n'): /* Answer number of assignments done. */
569 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
570 with the 'n' conversion specifier. */
571 if (!(flags & SUPPRESS))
573 /* Don't count the read-ahead. */
574 if (need_longlong && (flags & LONGDBL))
575 *ARG (long long int *) = read_in;
576 else if (need_long && (flags & LONG))
577 *ARG (long int *) = read_in;
578 else if (flags & SHORT)
579 *ARG (short int *) = read_in;
580 else if (!(flags & CHAR))
581 *ARG (int *) = read_in;
582 else
583 *ARG (char *) = read_in;
585 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
586 /* We have a severe problem here. The ISO C standard
587 contradicts itself in explaining the effect of the %n
588 format in `scanf'. While in ISO C:1990 and the ISO C
589 Amendement 1:1995 the result is described as
591 Execution of a %n directive does not effect the
592 assignment count returned at the completion of
593 execution of the f(w)scanf function.
595 in ISO C Corrigendum 1:1994 the following was added:
597 Subclause 7.9.6.2
598 Add the following fourth example:
600 #include <stdio.h>
601 int d1, d2, n1, n2, i;
602 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
603 the value 123 is assigned to d1 and the value3 to n1.
604 Because %n can never get an input failure the value
605 of 3 is also assigned to n2. The value of d2 is not
606 affected. The value 3 is assigned to i.
608 We go for now with the historically correct code from ISO C,
609 i.e., we don't count the %n assignments. When it ever
610 should proof to be wrong just remove the #ifdef above. */
611 ++done;
612 #endif
614 break;
616 case L_('c'): /* Match characters. */
617 if ((flags & LONG) == 0)
619 if (!(flags & SUPPRESS))
621 str = ARG (char *);
622 if (str == NULL)
623 conv_error ();
626 c = inchar ();
627 if (c == EOF)
628 input_error ();
630 if (width == -1)
631 width = 1;
633 #ifdef COMPILE_WSCANF
634 /* We have to convert the wide character(s) into multibyte
635 characters and store the result. */
636 memset (&state, '\0', sizeof (state));
640 size_t n;
642 n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c, &state);
643 if (n == (size_t) -1)
644 /* No valid wide character. */
645 input_error ();
647 /* Increment the output pointer. Even if we don't
648 write anything. */
649 str += n;
651 while (--width > 0 && inchar () != EOF);
652 #else
653 if (!(flags & SUPPRESS))
656 *str++ = c;
657 while (--width > 0 && inchar () != EOF);
659 else
660 while (--width > 0 && inchar () != EOF);
661 #endif
663 if (!(flags & SUPPRESS))
664 ++done;
666 break;
668 /* FALLTHROUGH */
669 case L_('C'):
670 if (!(flags & SUPPRESS))
672 wstr = ARG (wchar_t *);
673 if (wstr == NULL)
674 conv_error ();
677 c = inchar ();
678 if (c == EOF)
679 input_error ();
681 #ifdef COMPILE_WSCANF
682 /* Just store the incoming wide characters. */
683 if (!(flags & SUPPRESS))
686 *wstr++ = c;
687 while (--width > 0 && inchar () != EOF);
689 else
690 while (--width > 0 && inchar () != EOF);
691 #else
693 /* We have to convert the multibyte input sequence to wide
694 characters. */
695 char buf[1];
696 mbstate_t cstate;
698 memset (&cstate, '\0', sizeof (cstate));
702 /* This is what we present the mbrtowc function first. */
703 buf[0] = c;
705 while (1)
707 size_t n;
709 n = __mbrtowc (!(flags & SUPPRESS) ? wstr : NULL,
710 buf, 1, &cstate);
712 if (n == (size_t) -2)
714 /* Possibly correct character, just not enough
715 input. */
716 if (inchar () == EOF)
717 encode_error ();
719 buf[0] = c;
720 continue;
723 if (n != 1)
724 encode_error ();
726 /* We have a match. */
727 break;
730 /* Advance the result pointer. */
731 ++wstr;
733 while (--width > 0 && inchar () != EOF);
735 #endif
737 if (!(flags & SUPPRESS))
738 ++done;
740 break;
742 case L_('s'): /* Read a string. */
743 if (!(flags & LONG))
745 #define STRING_ARG(Str, Type) \
746 do if (!(flags & SUPPRESS)) \
748 if (flags & MALLOC) \
750 /* The string is to be stored in a malloc'd buffer. */ \
751 strptr = ARG (char **); \
752 if (strptr == NULL) \
753 conv_error (); \
754 /* Allocate an initial buffer. */ \
755 strsize = 100; \
756 *strptr = (char *) malloc (strsize * sizeof (Type)); \
757 Str = (Type *) *strptr; \
759 else \
760 Str = ARG (Type *); \
761 if (Str == NULL) \
762 conv_error (); \
763 } while (0)
764 STRING_ARG (str, char);
766 c = inchar ();
767 if (c == EOF)
768 input_error ();
770 #ifdef COMPILE_WSCANF
771 memset (&state, '\0', sizeof (state));
772 #endif
776 if (ISSPACE (c))
778 ungetc_not_eof (c, s);
779 break;
782 #ifdef COMPILE_WSCANF
783 /* This is quite complicated. We have to convert the
784 wide characters into multibyte characters and then
785 store them. */
787 size_t n;
789 if (!(flags & SUPPRESS) && (flags & MALLOC)
790 && str + MB_CUR_MAX >= *strptr + strsize)
792 /* We have to enlarge the buffer if the `a' flag
793 was given. */
794 size_t strleng = str - *strptr;
795 char *newstr;
797 newstr = (char *) realloc (*strptr, strsize * 2);
798 if (newstr == NULL)
800 /* Can't allocate that much. Last-ditch
801 effort. */
802 newstr = (char *) realloc (*strptr,
803 strleng + MB_CUR_MAX);
804 if (newstr == NULL)
806 /* We lose. Oh well. Terminate the
807 string and stop converting,
808 so at least we don't skip any input. */
809 ((char *) (*strptr))[strleng] = '\0';
810 ++done;
811 conv_error ();
813 else
815 *strptr = newstr;
816 str = newstr + strleng;
817 strsize = strleng + MB_CUR_MAX;
820 else
822 *strptr = newstr;
823 str = newstr + strleng;
824 strsize *= 2;
828 n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c,
829 &state);
830 if (n == (size_t) -1)
831 encode_error ();
833 assert (n <= MB_CUR_MAX);
834 str += n;
836 #else
837 /* This is easy. */
838 if (!(flags & SUPPRESS))
840 *str++ = c;
841 if ((flags & MALLOC)
842 && (char *) str == *strptr + strsize)
844 /* Enlarge the buffer. */
845 str = (char *) realloc (*strptr, 2 * strsize);
846 if (str == NULL)
848 /* Can't allocate that much. Last-ditch
849 effort. */
850 str = (char *) realloc (*strptr, strsize + 1);
851 if (str == NULL)
853 /* We lose. Oh well. Terminate the
854 string and stop converting,
855 so at least we don't skip any input. */
856 ((char *) (*strptr))[strsize - 1] = '\0';
857 ++done;
858 conv_error ();
860 else
862 *strptr = (char *) str;
863 str += strsize;
864 ++strsize;
867 else
869 *strptr = (char *) str;
870 str += strsize;
871 strsize *= 2;
875 #endif
877 while ((width <= 0 || --width > 0) && inchar () != EOF);
879 if (!(flags & SUPPRESS))
881 #ifdef COMPILE_WSCANF
882 /* We have to emit the code to get into the initial
883 state. */
884 char buf[MB_LEN_MAX];
885 size_t n = __wcrtomb (buf, L'\0', &state);
886 if (n > 0 && (flags & MALLOC)
887 && str + n >= *strptr + strsize)
889 /* Enlarge the buffer. */
890 size_t strleng = str - *strptr;
891 char *newstr;
893 newstr = (char *) realloc (*strptr, strleng + n + 1);
894 if (newstr == NULL)
896 /* We lose. Oh well. Terminate the string
897 and stop converting, so at least we don't
898 skip any input. */
899 ((char *) (*strptr))[strleng] = '\0';
900 ++done;
901 conv_error ();
903 else
905 *strptr = newstr;
906 str = newstr + strleng;
907 strsize = strleng + n + 1;
911 str = __mempcpy (str, buf, n);
912 #endif
913 *str++ = '\0';
915 if ((flags & MALLOC) && str - *strptr != strsize)
917 char *cp = (char *) realloc (*strptr, str - *strptr);
918 if (cp != NULL)
919 *strptr = cp;
922 ++done;
924 break;
926 /* FALLTHROUGH */
928 case L_('S'):
930 #ifndef COMPILE_WSCANF
931 mbstate_t cstate;
932 #endif
934 /* Wide character string. */
935 STRING_ARG (wstr, wchar_t);
937 c = inchar ();
938 if (c == EOF)
939 input_error ();
941 #ifndef COMPILE_WSCANF
942 memset (&cstate, '\0', sizeof (cstate));
943 #endif
947 if (ISSPACE (c))
949 ungetc_not_eof (c, s);
950 break;
953 #ifdef COMPILE_WSCANF
954 /* This is easy. */
955 if (!(flags & SUPPRESS))
957 *wstr++ = c;
958 if ((flags & MALLOC)
959 && wstr == (wchar_t *) *strptr + strsize)
961 /* Enlarge the buffer. */
962 wstr = (wchar_t *) realloc (*strptr,
963 (2 * strsize)
964 * sizeof (wchar_t));
965 if (wstr == NULL)
967 /* Can't allocate that much. Last-ditch
968 effort. */
969 wstr = (wchar_t *) realloc (*strptr,
970 (strsize + 1)
971 * sizeof (wchar_t));
972 if (wstr == NULL)
974 /* We lose. Oh well. Terminate the string
975 and stop converting, so at least we don't
976 skip any input. */
977 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
978 ++done;
979 conv_error ();
981 else
983 *strptr = (char *) wstr;
984 wstr += strsize;
985 ++strsize;
988 else
990 *strptr = (char *) wstr;
991 wstr += strsize;
992 strsize *= 2;
996 #else
998 char buf[1];
1000 buf[0] = c;
1002 while (1)
1004 size_t n;
1006 n = __mbrtowc (!(flags & SUPPRESS) ? wstr : NULL,
1007 buf, 1, &cstate);
1009 if (n == (size_t) -2)
1011 /* Possibly correct character, just not enough
1012 input. */
1013 if (inchar () == EOF)
1014 encode_error ();
1016 buf[0] = c;
1017 continue;
1020 if (n != 1)
1021 encode_error ();
1023 /* We have a match. */
1024 ++wstr;
1025 break;
1028 if (!(flags & SUPPRESS) && (flags & MALLOC)
1029 && wstr == (wchar_t *) *strptr + strsize)
1031 /* Enlarge the buffer. */
1032 wstr = (wchar_t *) realloc (*strptr,
1033 (2 * strsize
1034 * sizeof (wchar_t)));
1035 if (wstr == NULL)
1037 /* Can't allocate that much. Last-ditch effort. */
1038 wstr = (wchar_t *) realloc (*strptr,
1039 ((strsize + 1)
1040 * sizeof (wchar_t)));
1041 if (wstr == NULL)
1043 /* We lose. Oh well. Terminate the
1044 string and stop converting, so at
1045 least we don't skip any input. */
1046 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
1047 ++done;
1048 conv_error ();
1050 else
1052 *strptr = (char *) wstr;
1053 wstr += strsize;
1054 ++strsize;
1057 else
1059 *strptr = (char *) wstr;
1060 wstr += strsize;
1061 strsize *= 2;
1065 #endif
1067 while ((width <= 0 || --width > 0) && inchar () != EOF);
1069 if (!(flags & SUPPRESS))
1071 *wstr++ = L'\0';
1073 if ((flags & MALLOC) && wstr - (wchar_t *) *strptr != strsize)
1075 wchar_t *cp = (wchar_t *) realloc (*strptr,
1076 ((wstr
1077 - (wchar_t *) *strptr)
1078 * sizeof(wchar_t)));
1079 if (cp != NULL)
1080 *strptr = (char *) cp;
1083 ++done;
1086 break;
1088 case L_('x'): /* Hexadecimal integer. */
1089 case L_('X'): /* Ditto. */
1090 base = 16;
1091 number_signed = 0;
1092 goto number;
1094 case L_('o'): /* Octal integer. */
1095 base = 8;
1096 number_signed = 0;
1097 goto number;
1099 case L_('u'): /* Unsigned decimal integer. */
1100 base = 10;
1101 number_signed = 0;
1102 goto number;
1104 case L_('d'): /* Signed decimal integer. */
1105 base = 10;
1106 number_signed = 1;
1107 goto number;
1109 case L_('i'): /* Generic number. */
1110 base = 0;
1111 number_signed = 1;
1113 number:
1114 c = inchar ();
1115 if (c == EOF)
1116 input_error ();
1118 /* Check for a sign. */
1119 if (c == L_('-') || c == L_('+'))
1121 ADDW (c);
1122 if (width > 0)
1123 --width;
1124 c = inchar ();
1127 /* Look for a leading indication of base. */
1128 if (width != 0 && c == L_('0'))
1130 if (width > 0)
1131 --width;
1133 ADDW (c);
1134 c = inchar ();
1136 if (width != 0 && TOLOWER (c) == L_('x'))
1138 if (base == 0)
1139 base = 16;
1140 if (base == 16)
1142 if (width > 0)
1143 --width;
1144 c = inchar ();
1147 else if (base == 0)
1148 base = 8;
1151 if (base == 0)
1152 base = 10;
1154 if (base == 10 && (flags & I18N) != 0)
1156 int from_level;
1157 int to_level;
1158 int level;
1159 #ifdef COMPILE_WSCANF
1160 const wchar_t *wcdigits[10];
1161 const wchar_t *wcdigits_extended[10];
1162 #else
1163 const char *mbdigits[10];
1164 const char *mbdigits_extended[10];
1165 #endif
1166 /* "to_inpunct" is a map from ASCII digits to their
1167 equivalent in locale. This is defined for locales
1168 which use an extra digits set. */
1169 wctrans_t map = __wctrans ("to_inpunct");
1170 int n;
1172 from_level = 0;
1173 #ifdef COMPILE_WSCANF
1174 to_level = _NL_CURRENT_WORD (LC_CTYPE,
1175 _NL_CTYPE_INDIGITS_WC_LEN) - 1;
1176 #else
1177 to_level = (uint32_t) curctype->values[_NL_ITEM_INDEX (_NL_CTYPE_INDIGITS_MB_LEN)].word - 1;
1178 #endif
1180 /* Get the alternative digit forms if there are any. */
1181 if (__builtin_expect (map != NULL, 0))
1183 /* Adding new level for extra digits set in locale file. */
1184 ++to_level;
1186 for (n = 0; n < 10; ++n)
1188 #ifdef COMPILE_WSCANF
1189 wcdigits[n] = (const wchar_t *)
1190 _NL_CURRENT (LC_CTYPE, _NL_CTYPE_INDIGITS0_WC + n);
1192 wchar_t *wc_extended = (wchar_t *)
1193 alloca ((to_level + 2) * sizeof (wchar_t));
1194 __wmemcpy (wc_extended, wcdigits[n], to_level);
1195 wc_extended[to_level] = __towctrans (L'0' + n, map);
1196 wc_extended[to_level + 1] = '\0';
1197 wcdigits_extended[n] = wc_extended;
1198 #else
1199 mbdigits[n]
1200 = curctype->values[_NL_CTYPE_INDIGITS0_MB + n].string;
1202 /* Get the equivalent wide char in map. */
1203 wint_t extra_wcdigit = __towctrans (L'0' + n, map);
1205 /* Convert it to multibyte representation. */
1206 mbstate_t state;
1207 memset (&state, '\0', sizeof (state));
1209 char extra_mbdigit[MB_LEN_MAX];
1210 size_t mblen
1211 = __wcrtomb (extra_mbdigit, extra_wcdigit, &state);
1213 if (mblen == (size_t) -1)
1215 /* Ignore this new level. */
1216 map = NULL;
1217 break;
1220 /* Calculate the length of mbdigits[n]. */
1221 const char *last_char = mbdigits[n];
1222 for (level = 0; level < to_level; ++level)
1223 last_char = strchr (last_char, '\0') + 1;
1225 size_t mbdigits_len = last_char - mbdigits[n];
1227 /* Allocate memory for extended multibyte digit. */
1228 char *mb_extended;
1229 mb_extended = (char *) alloca (mbdigits_len + mblen + 1);
1231 /* And get the mbdigits + extra_digit string. */
1232 *(char *) __mempcpy (__mempcpy (mb_extended, mbdigits[n],
1233 mbdigits_len),
1234 extra_mbdigit, mblen) = '\0';
1235 mbdigits_extended[n] = mb_extended;
1236 #endif
1240 /* Read the number into workspace. */
1241 while (c != EOF && width != 0)
1243 /* In this round we get the pointer to the digit strings
1244 and also perform the first round of comparisons. */
1245 for (n = 0; n < 10; ++n)
1247 /* Get the string for the digits with value N. */
1248 #ifdef COMPILE_WSCANF
1249 if (__builtin_expect (map != NULL, 0))
1250 wcdigits[n] = wcdigits_extended[n];
1251 else
1252 wcdigits[n] = (const wchar_t *)
1253 _NL_CURRENT (LC_CTYPE, _NL_CTYPE_INDIGITS0_WC + n);
1254 wcdigits[n] += from_level;
1256 if (c == (wint_t) *wcdigits[n])
1258 to_level = from_level;
1259 break;
1262 /* Advance the pointer to the next string. */
1263 ++wcdigits[n];
1264 #else
1265 const char *cmpp;
1266 int avail = width > 0 ? width : INT_MAX;
1268 if (__builtin_expect (map != NULL, 0))
1269 mbdigits[n] = mbdigits_extended[n];
1270 else
1271 mbdigits[n]
1272 = curctype->values[_NL_CTYPE_INDIGITS0_MB + n].string;
1274 for (level = 0; level < from_level; level++)
1275 mbdigits[n] = strchr (mbdigits[n], '\0') + 1;
1277 cmpp = mbdigits[n];
1278 while ((unsigned char) *cmpp == c && avail > 0)
1280 if (*++cmpp == '\0')
1281 break;
1282 else
1284 if ((c = inchar ()) == EOF)
1285 break;
1286 --avail;
1290 if (*cmpp == '\0')
1292 if (width > 0)
1293 width = avail;
1294 to_level = from_level;
1295 break;
1298 /* We are pushing all read characters back. */
1299 if (cmpp > mbdigits[n])
1301 ungetc (c, s);
1302 while (--cmpp > mbdigits[n])
1303 ungetc_not_eof ((unsigned char) *cmpp, s);
1304 c = (unsigned char) *cmpp;
1307 /* Advance the pointer to the next string. */
1308 mbdigits[n] = strchr (mbdigits[n], '\0') + 1;
1309 #endif
1312 if (n == 10)
1314 /* Have not yet found the digit. */
1315 for (level = from_level + 1; level <= to_level; ++level)
1317 /* Search all ten digits of this level. */
1318 for (n = 0; n < 10; ++n)
1320 #ifdef COMPILE_WSCANF
1321 if (c == (wint_t) *wcdigits[n])
1322 break;
1324 /* Advance the pointer to the next string. */
1325 ++wcdigits[n];
1326 #else
1327 const char *cmpp;
1328 int avail = width > 0 ? width : INT_MAX;
1330 cmpp = mbdigits[n];
1331 while ((unsigned char) *cmpp == c && avail > 0)
1333 if (*++cmpp == '\0')
1334 break;
1335 else
1337 if ((c = inchar ()) == EOF)
1338 break;
1339 --avail;
1343 if (*cmpp == '\0')
1345 if (width > 0)
1346 width = avail;
1347 break;
1350 /* We are pushing all read characters back. */
1351 if (cmpp > mbdigits[n])
1353 ungetc (c, s);
1354 while (--cmpp > mbdigits[n])
1355 ungetc_not_eof ((unsigned char) *cmpp, s);
1356 c = (unsigned char) *cmpp;
1359 /* Advance the pointer to the next string. */
1360 mbdigits[n] = strchr (mbdigits[n], '\0') + 1;
1361 #endif
1364 if (n < 10)
1366 /* Found it. */
1367 from_level = level;
1368 to_level = level;
1369 break;
1374 if (n < 10)
1375 c = L_('0') + n;
1376 else if ((flags & GROUP)
1377 #ifdef COMPILE_WSCANF
1378 && thousands != L'\0'
1379 #else
1380 && thousands != NULL
1381 #endif
1384 /* Try matching against the thousands separator. */
1385 #ifdef COMPILE_WSCANF
1386 if (c != thousands)
1387 break;
1388 #else
1389 const char *cmpp = thousands;
1390 int avail = width > 0 ? width : INT_MAX;
1392 while ((unsigned char) *cmpp == c && avail > 0)
1394 ADDW (c);
1395 if (*++cmpp == '\0')
1396 break;
1397 else
1399 if ((c = inchar ()) == EOF)
1400 break;
1401 --avail;
1405 if (*cmpp != '\0')
1407 /* We are pushing all read characters back. */
1408 if (cmpp > thousands)
1410 wpsize -= cmpp - thousands;
1411 ungetc (c, s);
1412 while (--cmpp > thousands)
1413 ungetc_not_eof ((unsigned char) *cmpp, s);
1414 c = (unsigned char) *cmpp;
1416 break;
1419 if (width > 0)
1420 width = avail;
1422 /* The last thousands character will be added back by
1423 the ADDW below. */
1424 --wpsize;
1425 #endif
1427 else
1428 break;
1430 ADDW (c);
1431 if (width > 0)
1432 --width;
1434 c = inchar ();
1437 else
1438 /* Read the number into workspace. */
1439 while (c != EOF && width != 0)
1441 if (base == 16)
1443 if (!ISXDIGIT (c))
1444 break;
1446 else if (!ISDIGIT (c) || (int) (c - L_('0')) >= base)
1448 if (base == 10 && (flags & GROUP)
1449 #ifdef COMPILE_WSCANF
1450 && thousands != L'\0'
1451 #else
1452 && thousands != NULL
1453 #endif
1456 /* Try matching against the thousands separator. */
1457 #ifdef COMPILE_WSCANF
1458 if (c != thousands)
1459 break;
1460 #else
1461 const char *cmpp = thousands;
1462 int avail = width > 0 ? width : INT_MAX;
1464 while ((unsigned char) *cmpp == c && avail > 0)
1466 ADDW (c);
1467 if (*++cmpp == '\0')
1468 break;
1469 else
1471 if ((c = inchar ()) == EOF)
1472 break;
1473 --avail;
1477 if (*cmpp != '\0')
1479 /* We are pushing all read characters back. */
1480 if (cmpp > thousands)
1482 wpsize -= cmpp - thousands;
1483 ungetc (c, s);
1484 while (--cmpp > thousands)
1485 ungetc_not_eof ((unsigned char) *cmpp, s);
1486 c = (unsigned char) *cmpp;
1488 break;
1491 if (width > 0)
1492 width = avail;
1494 /* The last thousands character will be added back by
1495 the ADDW below. */
1496 --wpsize;
1497 #endif
1499 else
1500 break;
1502 ADDW (c);
1503 if (width > 0)
1504 --width;
1506 c = inchar ();
1509 if (wpsize == 0
1510 || (wpsize == 1 && (wp[0] == L_('+') || wp[0] == L_('-'))))
1512 /* There was no number. If we are supposed to read a pointer
1513 we must recognize "(nil)" as well. */
1514 if (wpsize == 0 && read_pointer && (width < 0 || width >= 0)
1515 && c == '('
1516 && TOLOWER (inchar ()) == L_('n')
1517 && TOLOWER (inchar ()) == L_('i')
1518 && TOLOWER (inchar ()) == L_('l')
1519 && inchar () == L_(')'))
1520 /* We must produce the value of a NULL pointer. A single
1521 '0' digit is enough. */
1522 ADDW (L_('0'));
1523 else
1525 /* The last read character is not part of the number
1526 anymore. */
1527 ungetc (c, s);
1529 conv_error ();
1532 else
1533 /* The just read character is not part of the number anymore. */
1534 ungetc (c, s);
1536 /* Convert the number. */
1537 ADDW (L_('\0'));
1538 if (need_longlong && (flags & LONGDBL))
1540 if (number_signed)
1541 num.q = __strtoll_internal (wp, &tw, base, flags & GROUP);
1542 else
1543 num.uq = __strtoull_internal (wp, &tw, base, flags & GROUP);
1545 else
1547 if (number_signed)
1548 num.l = __strtol_internal (wp, &tw, base, flags & GROUP);
1549 else
1550 num.ul = __strtoul_internal (wp, &tw, base, flags & GROUP);
1552 if (wp == tw)
1553 conv_error ();
1555 if (!(flags & SUPPRESS))
1557 if (! number_signed)
1559 if (need_longlong && (flags & LONGDBL))
1560 *ARG (unsigned LONGLONG int *) = num.uq;
1561 else if (need_long && (flags & LONG))
1562 *ARG (unsigned long int *) = num.ul;
1563 else if (flags & SHORT)
1564 *ARG (unsigned short int *)
1565 = (unsigned short int) num.ul;
1566 else if (!(flags & CHAR))
1567 *ARG (unsigned int *) = (unsigned int) num.ul;
1568 else
1569 *ARG (unsigned char *) = (unsigned char) num.ul;
1571 else
1573 if (need_longlong && (flags & LONGDBL))
1574 *ARG (LONGLONG int *) = num.q;
1575 else if (need_long && (flags & LONG))
1576 *ARG (long int *) = num.l;
1577 else if (flags & SHORT)
1578 *ARG (short int *) = (short int) num.l;
1579 else if (!(flags & CHAR))
1580 *ARG (int *) = (int) num.l;
1581 else
1582 *ARG (signed char *) = (signed char) num.ul;
1584 ++done;
1586 break;
1588 case L_('e'): /* Floating-point numbers. */
1589 case L_('E'):
1590 case L_('f'):
1591 case L_('F'):
1592 case L_('g'):
1593 case L_('G'):
1594 case L_('a'):
1595 case L_('A'):
1596 c = inchar ();
1597 if (c == EOF)
1598 input_error ();
1600 got_dot = got_e = 0;
1602 /* Check for a sign. */
1603 if (c == L_('-') || c == L_('+'))
1605 negative = c == L_('-');
1606 if (width == 0 || inchar () == EOF)
1607 /* EOF is only an input error before we read any chars. */
1608 conv_error ();
1609 if (! ISDIGIT (c) && TOLOWER (c) != L_('i')
1610 && TOLOWER (c) != L_('n'))
1612 #ifdef COMPILE_WSCANF
1613 if (c != decimal)
1615 /* This is no valid number. */
1616 ungetc (c, s);
1617 conv_error ();
1619 #else
1620 /* Match against the decimal point. At this point
1621 we are taking advantage of the fact that we can
1622 push more than one character back. This is
1623 (almost) never necessary since the decimal point
1624 string hopefully never contains more than one
1625 byte. */
1626 const char *cmpp = decimal;
1627 int avail = width > 0 ? width : INT_MAX;
1629 while ((unsigned char) *cmpp == c && avail-- > 0)
1630 if (*++cmpp == '\0')
1631 break;
1632 else
1634 if (inchar () == EOF)
1635 break;
1638 if (*cmpp != '\0')
1640 /* This is no valid number. */
1641 while (1)
1643 ungetc (c, s);
1644 if (cmpp == decimal)
1645 break;
1646 c = (unsigned char) *--cmpp;
1649 conv_error ();
1651 else
1653 /* Add all the characters. */
1654 for (cmpp = decimal; *cmpp != '\0'; ++cmpp)
1655 ADDW ((unsigned char) *cmpp);
1656 if (width > 0)
1657 width = avail;
1658 got_dot = 1;
1660 c = inchar ();
1662 if (width > 0)
1663 width = avail;
1664 #endif
1666 if (width > 0)
1667 --width;
1669 else
1670 negative = 0;
1672 /* Take care for the special arguments "nan" and "inf". */
1673 if (TOLOWER (c) == L_('n'))
1675 /* Maybe "nan". */
1676 ADDW (c);
1677 if (width == 0 || inchar () == EOF || TOLOWER (c) != L_('a'))
1678 conv_error ();
1679 if (width > 0)
1680 --width;
1681 ADDW (c);
1682 if (width == 0 || inchar () == EOF || TOLOWER (c) != L_('n'))
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 (width == 0 || inchar () == EOF || TOLOWER (c) != L_('n'))
1695 conv_error ();
1696 if (width > 0)
1697 --width;
1698 ADDW (c);
1699 if (width == 0 || inchar () == EOF || TOLOWER (c) != L_('f'))
1700 conv_error ();
1701 if (width > 0)
1702 --width;
1703 ADDW (c);
1704 /* It is as least "inf". */
1705 if (width != 0 && inchar () != EOF)
1707 if (TOLOWER (c) == L_('i'))
1709 if (width > 0)
1710 --width;
1711 /* Now we have to read the rest as well. */
1712 ADDW (c);
1713 if (width == 0 || inchar () == EOF
1714 || TOLOWER (c) != L_('n'))
1715 conv_error ();
1716 if (width > 0)
1717 --width;
1718 ADDW (c);
1719 if (width == 0 || inchar () == EOF
1720 || TOLOWER (c) != L_('i'))
1721 conv_error ();
1722 if (width > 0)
1723 --width;
1724 ADDW (c);
1725 if (width == 0 || inchar () == EOF
1726 || TOLOWER (c) != L_('t'))
1727 conv_error ();
1728 if (width > 0)
1729 --width;
1730 ADDW (c);
1731 if (width == 0 || inchar () == EOF
1732 || TOLOWER (c) != L_('y'))
1733 conv_error ();
1734 if (width > 0)
1735 --width;
1736 ADDW (c);
1738 else
1739 /* Never mind. */
1740 ungetc (c, s);
1742 goto scan_float;
1745 is_hexa = 0;
1746 exp_char = L_('e');
1747 if (width != 0 && c == L_('0'))
1749 ADDW (c);
1750 c = inchar ();
1751 if (width > 0)
1752 --width;
1753 if (width != 0 && TOLOWER (c) == L_('x'))
1755 /* It is a number in hexadecimal format. */
1756 ADDW (c);
1758 is_hexa = 1;
1759 exp_char = L_('p');
1761 /* Grouping is not allowed. */
1762 flags &= ~GROUP;
1763 c = inchar ();
1764 if (width > 0)
1765 --width;
1771 if (ISDIGIT (c))
1772 ADDW (c);
1773 else if (!got_e && is_hexa && ISXDIGIT (c))
1774 ADDW (c);
1775 else if (got_e && wp[wpsize - 1] == exp_char
1776 && (c == L_('-') || c == L_('+')))
1777 ADDW (c);
1778 else if (wpsize > 0 && !got_e
1779 && (CHAR_T) TOLOWER (c) == exp_char)
1781 ADDW (exp_char);
1782 got_e = got_dot = 1;
1784 else
1786 #ifdef COMPILE_WSCANF
1787 if (! got_dot && c == decimal)
1789 ADDW (c);
1790 got_dot = 1;
1792 else if ((flags & GROUP) != 0 && thousands != L'\0'
1793 && ! got_dot && c == thousands)
1794 ADDW (c);
1795 else
1797 /* The last read character is not part of the number
1798 anymore. */
1799 ungetc (c, s);
1800 break;
1802 #else
1803 const char *cmpp = decimal;
1804 int avail = width > 0 ? width : INT_MAX;
1806 if (! got_dot)
1808 while ((unsigned char) *cmpp == c && avail > 0)
1809 if (*++cmpp == '\0')
1810 break;
1811 else
1813 if (inchar () == EOF)
1814 break;
1815 --avail;
1819 if (*cmpp == '\0')
1821 /* Add all the characters. */
1822 for (cmpp = decimal; *cmpp != '\0'; ++cmpp)
1823 ADDW ((unsigned char) *cmpp);
1824 if (width > 0)
1825 width = avail;
1826 got_dot = 1;
1828 else
1830 /* Figure out whether it is a thousands separator.
1831 There is one problem: we possibly read more than
1832 one character. We cannot push them back but since
1833 we know that parts of the `decimal' string matched,
1834 we can compare against it. */
1835 const char *cmp2p = thousands;
1837 if ((flags & GROUP) != 0 && thousands != NULL
1838 && ! got_dot)
1840 while (cmp2p - thousands < cmpp - decimal
1841 && *cmp2p == decimal[cmp2p - thousands])
1842 ++cmp2p;
1843 if (cmp2p - thousands == cmpp - decimal)
1845 while ((unsigned char) *cmp2p == c && avail > 0)
1846 if (*++cmp2p == '\0')
1847 break;
1848 else
1850 if (inchar () == EOF)
1851 break;
1852 --avail;
1857 if (cmp2p != NULL && *cmp2p == '\0')
1859 /* Add all the characters. */
1860 for (cmpp = thousands; *cmpp != '\0'; ++cmpp)
1861 ADDW ((unsigned char) *cmpp);
1862 if (width > 0)
1863 width = avail;
1865 else
1867 /* The last read character is not part of the number
1868 anymore. */
1869 ungetc (c, s);
1870 break;
1873 #endif
1875 if (width > 0)
1876 --width;
1878 while (width != 0 && inchar () != EOF);
1880 /* Have we read any character? If we try to read a number
1881 in hexadecimal notation and we have read only the `0x'
1882 prefix or no exponent this is an error. */
1883 if (wpsize == 0 || (is_hexa && (wpsize == 2 || ! got_e)))
1884 conv_error ();
1886 scan_float:
1887 /* Convert the number. */
1888 ADDW (L_('\0'));
1889 if ((flags & LONGDBL) && !__ldbl_is_dbl)
1891 long double d = __strtold_internal (wp, &tw, flags & GROUP);
1892 if (!(flags & SUPPRESS) && tw != wp)
1893 *ARG (long double *) = negative ? -d : d;
1895 else if (flags & (LONG | LONGDBL))
1897 double d = __strtod_internal (wp, &tw, flags & GROUP);
1898 if (!(flags & SUPPRESS) && tw != wp)
1899 *ARG (double *) = negative ? -d : d;
1901 else
1903 float d = __strtof_internal (wp, &tw, flags & GROUP);
1904 if (!(flags & SUPPRESS) && tw != wp)
1905 *ARG (float *) = negative ? -d : d;
1908 if (tw == wp)
1909 conv_error ();
1911 if (!(flags & SUPPRESS))
1912 ++done;
1913 break;
1915 case L_('['): /* Character class. */
1916 if (flags & LONG)
1917 STRING_ARG (wstr, wchar_t);
1918 else
1919 STRING_ARG (str, char);
1921 if (*f == L_('^'))
1923 ++f;
1924 not_in = 1;
1926 else
1927 not_in = 0;
1929 if (width < 0)
1930 /* There is no width given so there is also no limit on the
1931 number of characters we read. Therefore we set width to
1932 a very high value to make the algorithm easier. */
1933 width = INT_MAX;
1935 #ifdef COMPILE_WSCANF
1936 /* Find the beginning and the end of the scanlist. We are not
1937 creating a lookup table since it would have to be too large.
1938 Instead we search each time through the string. This is not
1939 a constant lookup time but who uses this feature deserves to
1940 be punished. */
1941 tw = (wchar_t *) f; /* Marks the beginning. */
1943 if (*f == L']')
1944 ++f;
1946 while ((fc = *f++) != L'\0' && fc != L']');
1948 if (fc == L'\0')
1949 conv_error ();
1950 wp = (wchar_t *) f - 1;
1951 #else
1952 /* Fill WP with byte flags indexed by character.
1953 We will use this flag map for matching input characters. */
1954 if (wpmax < UCHAR_MAX + 1)
1956 wpmax = UCHAR_MAX + 1;
1957 wp = (char *) alloca (wpmax);
1959 memset (wp, '\0', UCHAR_MAX + 1);
1961 fc = *f;
1962 if (fc == ']' || fc == '-')
1964 /* If ] or - appears before any char in the set, it is not
1965 the terminator or separator, but the first char in the
1966 set. */
1967 wp[fc] = 1;
1968 ++f;
1971 while ((fc = *f++) != '\0' && fc != ']')
1972 if (fc == '-' && *f != '\0' && *f != ']'
1973 && (unsigned char) f[-2] <= (unsigned char) *f)
1975 /* Add all characters from the one before the '-'
1976 up to (but not including) the next format char. */
1977 for (fc = (unsigned char) f[-2]; fc < (unsigned char) *f; ++fc)
1978 wp[fc] = 1;
1980 else
1981 /* Add the character to the flag map. */
1982 wp[fc] = 1;
1984 if (fc == '\0')
1985 conv_error();
1986 #endif
1988 if (flags & LONG)
1990 size_t now = read_in;
1991 #ifdef COMPILE_WSCANF
1992 if (inchar () == WEOF)
1993 input_error ();
1997 wchar_t *runp;
1999 /* Test whether it's in the scanlist. */
2000 runp = tw;
2001 while (runp < wp)
2003 if (runp[0] == L'-' && runp[1] != '\0' && runp + 1 != wp
2004 && runp != tw
2005 && (unsigned int) runp[-1] <= (unsigned int) runp[1])
2007 /* Match against all characters in between the
2008 first and last character of the sequence. */
2009 wchar_t wc;
2011 for (wc = runp[-1] + 1; wc <= runp[1]; ++wc)
2012 if ((wint_t) wc == c)
2013 break;
2015 if (wc <= runp[1] && !not_in)
2016 break;
2017 if (wc <= runp[1] && not_in)
2019 /* The current character is not in the
2020 scanset. */
2021 ungetc (c, s);
2022 goto out;
2025 runp += 2;
2027 else
2029 if ((wint_t) *runp == c && !not_in)
2030 break;
2031 if ((wint_t) *runp == c && not_in)
2033 ungetc (c, s);
2034 goto out;
2037 ++runp;
2041 if (runp == wp && !not_in)
2043 ungetc (c, s);
2044 goto out;
2047 if (!(flags & SUPPRESS))
2049 *wstr++ = c;
2051 if ((flags & MALLOC)
2052 && wstr == (wchar_t *) *strptr + strsize)
2054 /* Enlarge the buffer. */
2055 wstr = (wchar_t *) realloc (*strptr,
2056 (2 * strsize)
2057 * sizeof (wchar_t));
2058 if (wstr == NULL)
2060 /* Can't allocate that much. Last-ditch
2061 effort. */
2062 wstr = (wchar_t *)
2063 realloc (*strptr, (strsize + 1)
2064 * sizeof (wchar_t));
2065 if (wstr == NULL)
2067 /* We lose. Oh well. Terminate the string
2068 and stop converting, so at least we don't
2069 skip any input. */
2070 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
2071 ++done;
2072 conv_error ();
2074 else
2076 *strptr = (char *) wstr;
2077 wstr += strsize;
2078 ++strsize;
2081 else
2083 *strptr = (char *) wstr;
2084 wstr += strsize;
2085 strsize *= 2;
2090 while (--width > 0 && inchar () != WEOF);
2091 out:
2092 #else
2093 char buf[MB_LEN_MAX];
2094 size_t cnt = 0;
2095 mbstate_t cstate;
2097 if (inchar () == EOF)
2098 input_error ();
2100 memset (&cstate, '\0', sizeof (cstate));
2104 if (wp[c] == not_in)
2106 ungetc_not_eof (c, s);
2107 break;
2110 /* This is easy. */
2111 if (!(flags & SUPPRESS))
2113 size_t n;
2115 /* Convert it into a wide character. */
2116 buf[0] = c;
2117 n = __mbrtowc (wstr, buf, 1, &cstate);
2119 if (n == (size_t) -2)
2121 /* Possibly correct character, just not enough
2122 input. */
2123 ++cnt;
2124 assert (cnt < MB_CUR_MAX);
2125 continue;
2127 cnt = 0;
2129 ++wstr;
2130 if ((flags & MALLOC)
2131 && wstr == (wchar_t *) *strptr + strsize)
2133 /* Enlarge the buffer. */
2134 wstr = (wchar_t *) realloc (*strptr,
2135 (2 * strsize
2136 * sizeof (wchar_t)));
2137 if (wstr == NULL)
2139 /* Can't allocate that much. Last-ditch
2140 effort. */
2141 wstr = (wchar_t *)
2142 realloc (*strptr, ((strsize + 1)
2143 * sizeof (wchar_t)));
2144 if (wstr == NULL)
2146 /* We lose. Oh well. Terminate the
2147 string and stop converting,
2148 so at least we don't skip any input. */
2149 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
2150 ++done;
2151 conv_error ();
2153 else
2155 *strptr = (char *) wstr;
2156 wstr += strsize;
2157 ++strsize;
2160 else
2162 *strptr = (char *) wstr;
2163 wstr += strsize;
2164 strsize *= 2;
2169 if (--width <= 0)
2170 break;
2172 while (inchar () != EOF);
2174 if (cnt != 0)
2175 /* We stopped in the middle of recognizing another
2176 character. That's a problem. */
2177 encode_error ();
2178 #endif
2180 if (now == read_in)
2181 /* We haven't succesfully read any character. */
2182 conv_error ();
2184 if (!(flags & SUPPRESS))
2186 *wstr++ = L'\0';
2188 if ((flags & MALLOC)
2189 && wstr - (wchar_t *) *strptr != strsize)
2191 wchar_t *cp = (wchar_t *)
2192 realloc (*strptr, ((wstr - (wchar_t *) *strptr)
2193 * sizeof(wchar_t)));
2194 if (cp != NULL)
2195 *strptr = (char *) cp;
2198 ++done;
2201 else
2203 size_t now = read_in;
2205 if (inchar () == EOF)
2206 input_error ();
2208 #ifdef COMPILE_WSCANF
2210 memset (&state, '\0', sizeof (state));
2214 wchar_t *runp;
2215 size_t n;
2217 /* Test whether it's in the scanlist. */
2218 runp = tw;
2219 while (runp < wp)
2221 if (runp[0] == L'-' && runp[1] != '\0' && runp + 1 != wp
2222 && runp != tw
2223 && (unsigned int) runp[-1] <= (unsigned int) runp[1])
2225 /* Match against all characters in between the
2226 first and last character of the sequence. */
2227 wchar_t wc;
2229 for (wc = runp[-1] + 1; wc <= runp[1]; ++wc)
2230 if ((wint_t) wc == c)
2231 break;
2233 if (wc <= runp[1] && !not_in)
2234 break;
2235 if (wc <= runp[1] && not_in)
2237 /* The current character is not in the
2238 scanset. */
2239 ungetc (c, s);
2240 goto out2;
2243 runp += 2;
2245 else
2247 if ((wint_t) *runp == c && !not_in)
2248 break;
2249 if ((wint_t) *runp == c && not_in)
2251 ungetc (c, s);
2252 goto out2;
2255 ++runp;
2259 if (runp == wp && !not_in)
2261 ungetc (c, s);
2262 goto out2;
2265 if (!(flags & SUPPRESS))
2267 if ((flags & MALLOC)
2268 && str + MB_CUR_MAX >= *strptr + strsize)
2270 /* Enlarge the buffer. */
2271 size_t strleng = str - *strptr;
2272 char *newstr;
2274 newstr = (char *) realloc (*strptr, 2 * strsize);
2275 if (newstr == NULL)
2277 /* Can't allocate that much. Last-ditch
2278 effort. */
2279 newstr = (char *) realloc (*strptr,
2280 strleng + MB_CUR_MAX);
2281 if (newstr == NULL)
2283 /* We lose. Oh well. Terminate the string
2284 and stop converting, so at least we don't
2285 skip any input. */
2286 ((char *) (*strptr))[strleng] = '\0';
2287 ++done;
2288 conv_error ();
2290 else
2292 *strptr = newstr;
2293 str = newstr + strleng;
2294 strsize = strleng + MB_CUR_MAX;
2297 else
2299 *strptr = newstr;
2300 str = newstr + strleng;
2301 strsize *= 2;
2306 n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c, &state);
2307 if (n == (size_t) -1)
2308 encode_error ();
2310 assert (n <= MB_CUR_MAX);
2311 str += n;
2313 while (--width > 0 && inchar () != WEOF);
2314 out2:
2315 #else
2318 if (wp[c] == not_in)
2320 ungetc_not_eof (c, s);
2321 break;
2324 /* This is easy. */
2325 if (!(flags & SUPPRESS))
2327 *str++ = c;
2328 if ((flags & MALLOC)
2329 && (char *) str == *strptr + strsize)
2331 /* Enlarge the buffer. */
2332 size_t newsize = 2 * strsize;
2334 allocagain:
2335 str = (char *) realloc (*strptr, newsize);
2336 if (str == NULL)
2338 /* Can't allocate that much. Last-ditch
2339 effort. */
2340 if (newsize > strsize + 1)
2342 newsize = strsize + 1;
2343 goto allocagain;
2345 /* We lose. Oh well. Terminate the
2346 string and stop converting,
2347 so at least we don't skip any input. */
2348 ((char *) (*strptr))[strsize - 1] = '\0';
2349 ++done;
2350 conv_error ();
2352 else
2354 *strptr = (char *) str;
2355 str += strsize;
2356 strsize = newsize;
2361 while (--width > 0 && inchar () != EOF);
2362 #endif
2364 if (now == read_in)
2365 /* We haven't succesfully read any character. */
2366 conv_error ();
2368 if (!(flags & SUPPRESS))
2370 #ifdef COMPILE_WSCANF
2371 /* We have to emit the code to get into the initial
2372 state. */
2373 char buf[MB_LEN_MAX];
2374 size_t n = __wcrtomb (buf, L'\0', &state);
2375 if (n > 0 && (flags & MALLOC)
2376 && str + n >= *strptr + strsize)
2378 /* Enlarge the buffer. */
2379 size_t strleng = str - *strptr;
2380 char *newstr;
2382 newstr = (char *) realloc (*strptr, strleng + n + 1);
2383 if (newstr == NULL)
2385 /* We lose. Oh well. Terminate the string
2386 and stop converting, so at least we don't
2387 skip any input. */
2388 ((char *) (*strptr))[strleng] = '\0';
2389 ++done;
2390 conv_error ();
2392 else
2394 *strptr = newstr;
2395 str = newstr + strleng;
2396 strsize = strleng + n + 1;
2400 str = __mempcpy (str, buf, n);
2401 #endif
2402 *str++ = '\0';
2404 if ((flags & MALLOC) && str - *strptr != strsize)
2406 char *cp = (char *) realloc (*strptr, str - *strptr);
2407 if (cp != NULL)
2408 *strptr = cp;
2411 ++done;
2414 break;
2416 case L_('p'): /* Generic pointer. */
2417 base = 16;
2418 /* A PTR must be the same size as a `long int'. */
2419 flags &= ~(SHORT|LONGDBL);
2420 if (need_long)
2421 flags |= LONG;
2422 number_signed = 0;
2423 read_pointer = 1;
2424 goto number;
2426 default:
2427 /* If this is an unknown format character punt. */
2428 conv_error ();
2432 /* The last thing we saw int the format string was a white space.
2433 Consume the last white spaces. */
2434 if (skip_space)
2437 c = inchar ();
2438 while (ISSPACE (c));
2439 ungetc (c, s);
2442 errout:
2443 /* Unlock stream. */
2444 UNLOCK_STREAM (s);
2446 if (errp != NULL)
2447 *errp |= errval;
2449 return done;
2452 #ifdef COMPILE_WSCANF
2454 __vfwscanf (FILE *s, const wchar_t *format, va_list argptr)
2456 return _IO_vfwscanf (s, format, argptr, NULL);
2458 ldbl_weak_alias (__vfwscanf, vfwscanf)
2459 #else
2461 ___vfscanf (FILE *s, const char *format, va_list argptr)
2463 return _IO_vfscanf_internal (s, format, argptr, NULL);
2465 ldbl_strong_alias (_IO_vfscanf_internal, _IO_vfscanf)
2466 ldbl_strong_alias (___vfscanf, __vfscanf)
2467 ldbl_hidden_def (___vfscanf, __vfscanf)
2468 ldbl_weak_alias (___vfscanf, vfscanf)
2469 #endif