Convert 703 function definitions to prototype style.
[glibc.git] / stdio-common / vfscanf.c
blob1382eb513d1ba0c5f34acfeef9b7524011806f01
1 /* Copyright (C) 1991-2015 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, see
16 <http://www.gnu.org/licenses/>. */
18 #include <assert.h>
19 #include <errno.h>
20 #include <limits.h>
21 #include <ctype.h>
22 #include <stdarg.h>
23 #include <stdbool.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 <libc-lock.h>
31 #include <locale/localeinfo.h>
32 #include <scratch_buffer.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 0x0001 /* l: long or double */
57 #define LONGDBL 0x0002 /* L: long long or long double */
58 #define SHORT 0x0004 /* h: short */
59 #define SUPPRESS 0x0008 /* *: suppress assignment */
60 #define POINTER 0x0010 /* weird %p pointer (`fake hex') */
61 #define NOSKIP 0x0020 /* do not skip blanks */
62 #define NUMBER_SIGNED 0x0040 /* signed integer */
63 #define GROUP 0x0080 /* ': group numbers */
64 #define GNU_MALLOC 0x0100 /* a: malloc strings */
65 #define CHAR 0x0200 /* hh: char */
66 #define I18N 0x0400 /* I: use locale's digits */
67 #define HEXA_FLOAT 0x0800 /* hexadecimal float */
68 #define READ_POINTER 0x1000 /* this is a pointer value */
69 #define POSIX_MALLOC 0x2000 /* m: malloc strings */
70 #define MALLOC (GNU_MALLOC | POSIX_MALLOC)
72 #include <locale/localeinfo.h>
73 #include <libioP.h>
74 #include <libio.h>
76 #undef va_list
77 #define va_list _IO_va_list
79 #ifdef COMPILE_WSCANF
80 # define ungetc(c, s) ((void) (c == WEOF \
81 || (--read_in, \
82 _IO_sputbackwc (s, c))))
83 # define ungetc_not_eof(c, s) ((void) (--read_in, \
84 _IO_sputbackwc (s, c)))
85 # define inchar() (c == WEOF ? ((errno = inchar_errno), WEOF) \
86 : ((c = _IO_getwc_unlocked (s)), \
87 (void) (c != WEOF \
88 ? ++read_in \
89 : (size_t) (inchar_errno = errno)), c))
91 # define ISSPACE(Ch) iswspace (Ch)
92 # define ISDIGIT(Ch) iswdigit (Ch)
93 # define ISXDIGIT(Ch) iswxdigit (Ch)
94 # define TOLOWER(Ch) towlower (Ch)
95 # define ORIENT if (_IO_fwide (s, 1) != 1) return WEOF
96 # define __strtoll_internal __wcstoll_internal
97 # define __strtoull_internal __wcstoull_internal
98 # define __strtol_internal __wcstol_internal
99 # define __strtoul_internal __wcstoul_internal
100 # define __strtold_internal __wcstold_internal
101 # define __strtod_internal __wcstod_internal
102 # define __strtof_internal __wcstof_internal
104 # define L_(Str) L##Str
105 # define CHAR_T wchar_t
106 # define UCHAR_T unsigned int
107 # define WINT_T wint_t
108 # undef EOF
109 # define EOF WEOF
110 #else
111 # define ungetc(c, s) ((void) ((int) c == EOF \
112 || (--read_in, \
113 _IO_sputbackc (s, (unsigned char) c))))
114 # define ungetc_not_eof(c, s) ((void) (--read_in, \
115 _IO_sputbackc (s, (unsigned char) c)))
116 # define inchar() (c == EOF ? ((errno = inchar_errno), EOF) \
117 : ((c = _IO_getc_unlocked (s)), \
118 (void) (c != EOF \
119 ? ++read_in \
120 : (size_t) (inchar_errno = errno)), c))
121 # define ISSPACE(Ch) __isspace_l (Ch, loc)
122 # define ISDIGIT(Ch) __isdigit_l (Ch, loc)
123 # define ISXDIGIT(Ch) __isxdigit_l (Ch, loc)
124 # define TOLOWER(Ch) __tolower_l ((unsigned char) (Ch), loc)
125 # define ORIENT if (_IO_vtable_offset (s) == 0 \
126 && _IO_fwide (s, -1) != -1) \
127 return EOF
129 # define L_(Str) Str
130 # define CHAR_T char
131 # define UCHAR_T unsigned char
132 # define WINT_T int
133 #endif
135 #define encode_error() do { \
136 errval = 4; \
137 __set_errno (EILSEQ); \
138 goto errout; \
139 } while (0)
140 #define conv_error() do { \
141 errval = 2; \
142 goto errout; \
143 } while (0)
144 #define input_error() do { \
145 errval = 1; \
146 if (done == 0) done = EOF; \
147 goto errout; \
148 } while (0)
149 #define add_ptr_to_free(ptr) \
150 do \
152 if (ptrs_to_free == NULL \
153 || ptrs_to_free->count == (sizeof (ptrs_to_free->ptrs) \
154 / sizeof (ptrs_to_free->ptrs[0]))) \
156 struct ptrs_to_free *new_ptrs = alloca (sizeof (*ptrs_to_free)); \
157 new_ptrs->count = 0; \
158 new_ptrs->next = ptrs_to_free; \
159 ptrs_to_free = new_ptrs; \
161 ptrs_to_free->ptrs[ptrs_to_free->count++] = (ptr); \
163 while (0)
164 #define ARGCHECK(s, format) \
165 do \
167 /* Check file argument for consistence. */ \
168 CHECK_FILE (s, EOF); \
169 if (s->_flags & _IO_NO_READS) \
171 __set_errno (EBADF); \
172 return EOF; \
174 else if (format == NULL) \
176 MAYBE_SET_EINVAL; \
177 return EOF; \
179 } while (0)
180 #define LOCK_STREAM(S) \
181 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, (S)); \
182 _IO_flockfile (S)
183 #define UNLOCK_STREAM(S) \
184 _IO_funlockfile (S); \
185 __libc_cleanup_region_end (0)
187 struct ptrs_to_free
189 size_t count;
190 struct ptrs_to_free *next;
191 char **ptrs[32];
194 struct char_buffer {
195 CHAR_T *current;
196 CHAR_T *end;
197 struct scratch_buffer scratch;
200 /* Returns a pointer to the first CHAR_T object in the buffer. Only
201 valid if char_buffer_add (BUFFER, CH) has been called and
202 char_buffer_error (BUFFER) is false. */
203 static inline CHAR_T *
204 char_buffer_start (const struct char_buffer *buffer)
206 return (CHAR_T *) buffer->scratch.data;
209 /* Returns the number of CHAR_T objects in the buffer. Only valid if
210 char_buffer_error (BUFFER) is false. */
211 static inline size_t
212 char_buffer_size (const struct char_buffer *buffer)
214 return buffer->current - char_buffer_start (buffer);
217 /* Reinitializes BUFFER->current and BUFFER->end to cover the entire
218 scratch buffer. */
219 static inline void
220 char_buffer_rewind (struct char_buffer *buffer)
222 buffer->current = char_buffer_start (buffer);
223 buffer->end = buffer->current + buffer->scratch.length / sizeof (CHAR_T);
226 /* Returns true if a previous call to char_buffer_add (BUFFER, CH)
227 failed. */
228 static inline bool
229 char_buffer_error (const struct char_buffer *buffer)
231 return __glibc_unlikely (buffer->current == NULL);
234 /* Slow path for char_buffer_add. */
235 static void
236 char_buffer_add_slow (struct char_buffer *buffer, CHAR_T ch)
238 if (char_buffer_error (buffer))
239 return;
240 size_t offset = buffer->end - (CHAR_T *) buffer->scratch.data;
241 if (!scratch_buffer_grow_preserve (&buffer->scratch))
243 buffer->current = NULL;
244 buffer->end = NULL;
245 return;
247 char_buffer_rewind (buffer);
248 buffer->current += offset;
249 *buffer->current++ = ch;
252 /* Adds CH to BUFFER. This function does not report any errors, check
253 for them with char_buffer_error. */
254 static inline void
255 char_buffer_add (struct char_buffer *buffer, CHAR_T ch)
256 __attribute__ ((always_inline));
257 static inline void
258 char_buffer_add (struct char_buffer *buffer, CHAR_T ch)
260 if (__glibc_unlikely (buffer->current == buffer->end))
261 char_buffer_add_slow (buffer, ch);
262 else
263 *buffer->current++ = ch;
266 /* Read formatted input from S according to the format string
267 FORMAT, using the argument list in ARG.
268 Return the number of assignments made, or -1 for an input error. */
269 #ifdef COMPILE_WSCANF
271 _IO_vfwscanf (_IO_FILE *s, const wchar_t *format, _IO_va_list argptr,
272 int *errp)
273 #else
275 _IO_vfscanf_internal (_IO_FILE *s, const char *format, _IO_va_list argptr,
276 int *errp)
277 #endif
279 va_list arg;
280 const CHAR_T *f = format;
281 UCHAR_T fc; /* Current character of the format. */
282 WINT_T done = 0; /* Assignments done. */
283 size_t read_in = 0; /* Chars read in. */
284 WINT_T c = 0; /* Last char read. */
285 int width; /* Maximum field width. */
286 int flags; /* Modifiers for current format element. */
287 int errval = 0;
288 #ifndef COMPILE_WSCANF
289 __locale_t loc = _NL_CURRENT_LOCALE;
290 struct __locale_data *const curctype = loc->__locales[LC_CTYPE];
291 #endif
293 /* Errno of last failed inchar call. */
294 int inchar_errno = 0;
295 /* Status for reading F-P nums. */
296 char got_digit, got_dot, got_e, negative;
297 /* If a [...] is a [^...]. */
298 CHAR_T not_in;
299 #define exp_char not_in
300 /* Base for integral numbers. */
301 int base;
302 /* Decimal point character. */
303 #ifdef COMPILE_WSCANF
304 wint_t decimal;
305 #else
306 const char *decimal;
307 #endif
308 /* The thousands character of the current locale. */
309 #ifdef COMPILE_WSCANF
310 wint_t thousands;
311 #else
312 const char *thousands;
313 #endif
314 struct ptrs_to_free *ptrs_to_free = NULL;
315 /* State for the conversions. */
316 mbstate_t state;
317 /* Integral holding variables. */
318 union
320 long long int q;
321 unsigned long long int uq;
322 long int l;
323 unsigned long int ul;
324 } num;
325 /* Character-buffer pointer. */
326 char *str = NULL;
327 wchar_t *wstr = NULL;
328 char **strptr = NULL;
329 ssize_t strsize = 0;
330 /* We must not react on white spaces immediately because they can
331 possibly be matched even if in the input stream no character is
332 available anymore. */
333 int skip_space = 0;
334 /* Workspace. */
335 CHAR_T *tw; /* Temporary pointer. */
336 struct char_buffer charbuf;
337 scratch_buffer_init (&charbuf.scratch);
339 #ifdef __va_copy
340 __va_copy (arg, argptr);
341 #else
342 arg = (va_list) argptr;
343 #endif
345 #ifdef ORIENT
346 ORIENT;
347 #endif
349 ARGCHECK (s, format);
352 #ifndef COMPILE_WSCANF
353 struct __locale_data *const curnumeric = loc->__locales[LC_NUMERIC];
354 #endif
356 /* Figure out the decimal point character. */
357 #ifdef COMPILE_WSCANF
358 decimal = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
359 #else
360 decimal = curnumeric->values[_NL_ITEM_INDEX (DECIMAL_POINT)].string;
361 #endif
362 /* Figure out the thousands separator character. */
363 #ifdef COMPILE_WSCANF
364 thousands = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_THOUSANDS_SEP_WC);
365 #else
366 thousands = curnumeric->values[_NL_ITEM_INDEX (THOUSANDS_SEP)].string;
367 if (*thousands == '\0')
368 thousands = NULL;
369 #endif
372 /* Lock the stream. */
373 LOCK_STREAM (s);
376 #ifndef COMPILE_WSCANF
377 /* From now on we use `state' to convert the format string. */
378 memset (&state, '\0', sizeof (state));
379 #endif
381 /* Run through the format string. */
382 while (*f != '\0')
384 unsigned int argpos;
385 /* Extract the next argument, which is of type TYPE.
386 For a %N$... spec, this is the Nth argument from the beginning;
387 otherwise it is the next argument after the state now in ARG. */
388 #ifdef __va_copy
389 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
390 ({ unsigned int pos = argpos; \
391 va_list arg; \
392 __va_copy (arg, argptr); \
393 while (--pos > 0) \
394 (void) va_arg (arg, void *); \
395 va_arg (arg, type); \
397 #else
398 # if 0
399 /* XXX Possible optimization. */
400 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
401 ({ va_list arg = (va_list) argptr; \
402 arg = (va_list) ((char *) arg \
403 + (argpos - 1) \
404 * __va_rounded_size (void *)); \
405 va_arg (arg, type); \
407 # else
408 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
409 ({ unsigned int pos = argpos; \
410 va_list arg = (va_list) argptr; \
411 while (--pos > 0) \
412 (void) va_arg (arg, void *); \
413 va_arg (arg, type); \
415 # endif
416 #endif
418 #ifndef COMPILE_WSCANF
419 if (!isascii ((unsigned char) *f))
421 /* Non-ASCII, may be a multibyte. */
422 int len = __mbrlen (f, strlen (f), &state);
423 if (len > 0)
427 c = inchar ();
428 if (__glibc_unlikely (c == EOF))
429 input_error ();
430 else if (c != (unsigned char) *f++)
432 ungetc_not_eof (c, s);
433 conv_error ();
436 while (--len > 0);
437 continue;
440 #endif
442 fc = *f++;
443 if (fc != '%')
445 /* Remember to skip spaces. */
446 if (ISSPACE (fc))
448 skip_space = 1;
449 continue;
452 /* Read a character. */
453 c = inchar ();
455 /* Characters other than format specs must just match. */
456 if (__glibc_unlikely (c == EOF))
457 input_error ();
459 /* We saw white space char as the last character in the format
460 string. Now it's time to skip all leading white space. */
461 if (skip_space)
463 while (ISSPACE (c))
464 if (__glibc_unlikely (inchar () == EOF))
465 input_error ();
466 skip_space = 0;
469 if (__glibc_unlikely (c != fc))
471 ungetc (c, s);
472 conv_error ();
475 continue;
478 /* This is the start of the conversion string. */
479 flags = 0;
481 /* Initialize state of modifiers. */
482 argpos = 0;
484 /* Prepare temporary buffer. */
485 char_buffer_rewind (&charbuf);
487 /* Check for a positional parameter specification. */
488 if (ISDIGIT ((UCHAR_T) *f))
490 argpos = (UCHAR_T) *f++ - L_('0');
491 while (ISDIGIT ((UCHAR_T) *f))
492 argpos = argpos * 10 + ((UCHAR_T) *f++ - L_('0'));
493 if (*f == L_('$'))
494 ++f;
495 else
497 /* Oops; that was actually the field width. */
498 width = argpos;
499 argpos = 0;
500 goto got_width;
504 /* Check for the assignment-suppressing, the number grouping flag,
505 and the signal to use the locale's digit representation. */
506 while (*f == L_('*') || *f == L_('\'') || *f == L_('I'))
507 switch (*f++)
509 case L_('*'):
510 flags |= SUPPRESS;
511 break;
512 case L_('\''):
513 #ifdef COMPILE_WSCANF
514 if (thousands != L'\0')
515 #else
516 if (thousands != NULL)
517 #endif
518 flags |= GROUP;
519 break;
520 case L_('I'):
521 flags |= I18N;
522 break;
525 /* Find the maximum field width. */
526 width = 0;
527 while (ISDIGIT ((UCHAR_T) *f))
529 width *= 10;
530 width += (UCHAR_T) *f++ - L_('0');
532 got_width:
533 if (width == 0)
534 width = -1;
536 /* Check for type modifiers. */
537 switch (*f++)
539 case L_('h'):
540 /* ints are short ints or chars. */
541 if (*f == L_('h'))
543 ++f;
544 flags |= CHAR;
546 else
547 flags |= SHORT;
548 break;
549 case L_('l'):
550 if (*f == L_('l'))
552 /* A double `l' is equivalent to an `L'. */
553 ++f;
554 flags |= LONGDBL | LONG;
556 else
557 /* ints are long ints. */
558 flags |= LONG;
559 break;
560 case L_('q'):
561 case L_('L'):
562 /* doubles are long doubles, and ints are long long ints. */
563 flags |= LONGDBL | LONG;
564 break;
565 case L_('a'):
566 /* The `a' is used as a flag only if followed by `s', `S' or
567 `['. */
568 if (*f != L_('s') && *f != L_('S') && *f != L_('['))
570 --f;
571 break;
573 /* In __isoc99_*scanf %as, %aS and %a[ extension is not
574 supported at all. */
575 if (s->_flags2 & _IO_FLAGS2_SCANF_STD)
577 --f;
578 break;
580 /* String conversions (%s, %[) take a `char **'
581 arg and fill it in with a malloc'd pointer. */
582 flags |= GNU_MALLOC;
583 break;
584 case L_('m'):
585 flags |= POSIX_MALLOC;
586 if (*f == L_('l'))
588 ++f;
589 flags |= LONG;
591 break;
592 case L_('z'):
593 if (need_longlong && sizeof (size_t) > sizeof (unsigned long int))
594 flags |= LONGDBL;
595 else if (sizeof (size_t) > sizeof (unsigned int))
596 flags |= LONG;
597 break;
598 case L_('j'):
599 if (need_longlong && sizeof (uintmax_t) > sizeof (unsigned long int))
600 flags |= LONGDBL;
601 else if (sizeof (uintmax_t) > sizeof (unsigned int))
602 flags |= LONG;
603 break;
604 case L_('t'):
605 if (need_longlong && sizeof (ptrdiff_t) > sizeof (long int))
606 flags |= LONGDBL;
607 else if (sizeof (ptrdiff_t) > sizeof (int))
608 flags |= LONG;
609 break;
610 default:
611 /* Not a recognized modifier. Backup. */
612 --f;
613 break;
616 /* End of the format string? */
617 if (__glibc_unlikely (*f == L_('\0')))
618 conv_error ();
620 /* Find the conversion specifier. */
621 fc = *f++;
622 if (skip_space || (fc != L_('[') && fc != L_('c')
623 && fc != L_('C') && fc != L_('n')))
625 /* Eat whitespace. */
626 int save_errno = errno;
627 __set_errno (0);
629 /* We add the additional test for EOF here since otherwise
630 inchar will restore the old errno value which might be
631 EINTR but does not indicate an interrupt since nothing
632 was read at this time. */
633 if (__builtin_expect ((c == EOF || inchar () == EOF)
634 && errno == EINTR, 0))
635 input_error ();
636 while (ISSPACE (c));
637 __set_errno (save_errno);
638 ungetc (c, s);
639 skip_space = 0;
642 switch (fc)
644 case L_('%'): /* Must match a literal '%'. */
645 c = inchar ();
646 if (__glibc_unlikely (c == EOF))
647 input_error ();
648 if (__glibc_unlikely (c != fc))
650 ungetc_not_eof (c, s);
651 conv_error ();
653 break;
655 case L_('n'): /* Answer number of assignments done. */
656 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
657 with the 'n' conversion specifier. */
658 if (!(flags & SUPPRESS))
660 /* Don't count the read-ahead. */
661 if (need_longlong && (flags & LONGDBL))
662 *ARG (long long int *) = read_in;
663 else if (need_long && (flags & LONG))
664 *ARG (long int *) = read_in;
665 else if (flags & SHORT)
666 *ARG (short int *) = read_in;
667 else if (!(flags & CHAR))
668 *ARG (int *) = read_in;
669 else
670 *ARG (char *) = read_in;
672 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
673 /* We have a severe problem here. The ISO C standard
674 contradicts itself in explaining the effect of the %n
675 format in `scanf'. While in ISO C:1990 and the ISO C
676 Amendement 1:1995 the result is described as
678 Execution of a %n directive does not effect the
679 assignment count returned at the completion of
680 execution of the f(w)scanf function.
682 in ISO C Corrigendum 1:1994 the following was added:
684 Subclause 7.9.6.2
685 Add the following fourth example:
687 #include <stdio.h>
688 int d1, d2, n1, n2, i;
689 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
690 the value 123 is assigned to d1 and the value3 to n1.
691 Because %n can never get an input failure the value
692 of 3 is also assigned to n2. The value of d2 is not
693 affected. The value 3 is assigned to i.
695 We go for now with the historically correct code from ISO C,
696 i.e., we don't count the %n assignments. When it ever
697 should proof to be wrong just remove the #ifdef above. */
698 ++done;
699 #endif
701 break;
703 case L_('c'): /* Match characters. */
704 if ((flags & LONG) == 0)
706 if (width == -1)
707 width = 1;
709 #define STRING_ARG(Str, Type, Width) \
710 do if (!(flags & SUPPRESS)) \
712 if (flags & MALLOC) \
714 /* The string is to be stored in a malloc'd buffer. */ \
715 /* For %mS using char ** is actually wrong, but \
716 shouldn't make a difference on any arch glibc \
717 supports and would unnecessarily complicate \
718 things. */ \
719 strptr = ARG (char **); \
720 if (strptr == NULL) \
721 conv_error (); \
722 /* Allocate an initial buffer. */ \
723 strsize = Width; \
724 *strptr = (char *) malloc (strsize * sizeof (Type)); \
725 Str = (Type *) *strptr; \
726 if (Str != NULL) \
727 add_ptr_to_free (strptr); \
728 else if (flags & POSIX_MALLOC) \
730 done = EOF; \
731 goto errout; \
734 else \
735 Str = ARG (Type *); \
736 if (Str == NULL) \
737 conv_error (); \
738 } while (0)
739 #ifdef COMPILE_WSCANF
740 STRING_ARG (str, char, 100);
741 #else
742 STRING_ARG (str, char, (width > 1024 ? 1024 : width));
743 #endif
745 c = inchar ();
746 if (__glibc_unlikely (c == EOF))
747 input_error ();
749 #ifdef COMPILE_WSCANF
750 /* We have to convert the wide character(s) into multibyte
751 characters and store the result. */
752 memset (&state, '\0', sizeof (state));
756 size_t n;
758 if (!(flags & SUPPRESS) && (flags & POSIX_MALLOC)
759 && str + MB_CUR_MAX >= *strptr + strsize)
761 /* We have to enlarge the buffer if the `m' flag
762 was given. */
763 size_t strleng = str - *strptr;
764 char *newstr;
766 newstr = (char *) realloc (*strptr, strsize * 2);
767 if (newstr == NULL)
769 /* Can't allocate that much. Last-ditch effort. */
770 newstr = (char *) realloc (*strptr,
771 strleng + MB_CUR_MAX);
772 if (newstr == NULL)
774 /* c can't have `a' flag, only `m'. */
775 done = EOF;
776 goto errout;
778 else
780 *strptr = newstr;
781 str = newstr + strleng;
782 strsize = strleng + MB_CUR_MAX;
785 else
787 *strptr = newstr;
788 str = newstr + strleng;
789 strsize *= 2;
793 n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c, &state);
794 if (__glibc_unlikely (n == (size_t) -1))
795 /* No valid wide character. */
796 input_error ();
798 /* Increment the output pointer. Even if we don't
799 write anything. */
800 str += n;
802 while (--width > 0 && inchar () != EOF);
803 #else
804 if (!(flags & SUPPRESS))
808 if ((flags & MALLOC)
809 && (char *) str == *strptr + strsize)
811 /* Enlarge the buffer. */
812 size_t newsize
813 = strsize
814 + (strsize >= width ? width - 1 : strsize);
816 str = (char *) realloc (*strptr, newsize);
817 if (str == NULL)
819 /* Can't allocate that much. Last-ditch
820 effort. */
821 str = (char *) realloc (*strptr, strsize + 1);
822 if (str == NULL)
824 /* c can't have `a' flag, only `m'. */
825 done = EOF;
826 goto errout;
828 else
830 *strptr = (char *) str;
831 str += strsize;
832 ++strsize;
835 else
837 *strptr = (char *) str;
838 str += strsize;
839 strsize = newsize;
842 *str++ = c;
844 while (--width > 0 && inchar () != EOF);
846 else
847 while (--width > 0 && inchar () != EOF);
848 #endif
850 if (!(flags & SUPPRESS))
852 if ((flags & MALLOC) && str - *strptr != strsize)
854 char *cp = (char *) realloc (*strptr, str - *strptr);
855 if (cp != NULL)
856 *strptr = cp;
858 strptr = NULL;
859 ++done;
862 break;
864 /* FALLTHROUGH */
865 case L_('C'):
866 if (width == -1)
867 width = 1;
869 STRING_ARG (wstr, wchar_t, (width > 1024 ? 1024 : width));
871 c = inchar ();
872 if (__glibc_unlikely (c == EOF))
873 input_error ();
875 #ifdef COMPILE_WSCANF
876 /* Just store the incoming wide characters. */
877 if (!(flags & SUPPRESS))
881 if ((flags & MALLOC)
882 && wstr == (wchar_t *) *strptr + strsize)
884 size_t newsize
885 = strsize + (strsize > width ? width - 1 : strsize);
886 /* Enlarge the buffer. */
887 wstr = (wchar_t *) realloc (*strptr,
888 newsize * sizeof (wchar_t));
889 if (wstr == NULL)
891 /* Can't allocate that much. Last-ditch effort. */
892 wstr = (wchar_t *) realloc (*strptr,
893 (strsize + 1)
894 * sizeof (wchar_t));
895 if (wstr == NULL)
897 /* C or lc can't have `a' flag, only `m'
898 flag. */
899 done = EOF;
900 goto errout;
902 else
904 *strptr = (char *) wstr;
905 wstr += strsize;
906 ++strsize;
909 else
911 *strptr = (char *) wstr;
912 wstr += strsize;
913 strsize = newsize;
916 *wstr++ = c;
918 while (--width > 0 && inchar () != EOF);
920 else
921 while (--width > 0 && inchar () != EOF);
922 #else
924 /* We have to convert the multibyte input sequence to wide
925 characters. */
926 char buf[1];
927 mbstate_t cstate;
929 memset (&cstate, '\0', sizeof (cstate));
933 /* This is what we present the mbrtowc function first. */
934 buf[0] = c;
936 if (!(flags & SUPPRESS) && (flags & MALLOC)
937 && wstr == (wchar_t *) *strptr + strsize)
939 size_t newsize
940 = strsize + (strsize > width ? width - 1 : strsize);
941 /* Enlarge the buffer. */
942 wstr = (wchar_t *) realloc (*strptr,
943 newsize * sizeof (wchar_t));
944 if (wstr == NULL)
946 /* Can't allocate that much. Last-ditch effort. */
947 wstr = (wchar_t *) realloc (*strptr,
948 ((strsize + 1)
949 * sizeof (wchar_t)));
950 if (wstr == NULL)
952 /* C or lc can't have `a' flag, only `m' flag. */
953 done = EOF;
954 goto errout;
956 else
958 *strptr = (char *) wstr;
959 wstr += strsize;
960 ++strsize;
963 else
965 *strptr = (char *) wstr;
966 wstr += strsize;
967 strsize = newsize;
971 while (1)
973 size_t n;
975 n = __mbrtowc (!(flags & SUPPRESS) ? wstr : NULL,
976 buf, 1, &cstate);
978 if (n == (size_t) -2)
980 /* Possibly correct character, just not enough
981 input. */
982 if (__glibc_unlikely (inchar () == EOF))
983 encode_error ();
985 buf[0] = c;
986 continue;
989 if (__glibc_unlikely (n != 1))
990 encode_error ();
992 /* We have a match. */
993 break;
996 /* Advance the result pointer. */
997 ++wstr;
999 while (--width > 0 && inchar () != EOF);
1001 #endif
1003 if (!(flags & SUPPRESS))
1005 if ((flags & MALLOC) && wstr - (wchar_t *) *strptr != strsize)
1007 wchar_t *cp = (wchar_t *) realloc (*strptr,
1008 ((wstr
1009 - (wchar_t *) *strptr)
1010 * sizeof (wchar_t)));
1011 if (cp != NULL)
1012 *strptr = (char *) cp;
1014 strptr = NULL;
1016 ++done;
1019 break;
1021 case L_('s'): /* Read a string. */
1022 if (!(flags & LONG))
1024 STRING_ARG (str, char, 100);
1026 c = inchar ();
1027 if (__glibc_unlikely (c == EOF))
1028 input_error ();
1030 #ifdef COMPILE_WSCANF
1031 memset (&state, '\0', sizeof (state));
1032 #endif
1036 if (ISSPACE (c))
1038 ungetc_not_eof (c, s);
1039 break;
1042 #ifdef COMPILE_WSCANF
1043 /* This is quite complicated. We have to convert the
1044 wide characters into multibyte characters and then
1045 store them. */
1047 size_t n;
1049 if (!(flags & SUPPRESS) && (flags & MALLOC)
1050 && str + MB_CUR_MAX >= *strptr + strsize)
1052 /* We have to enlarge the buffer if the `a' or `m'
1053 flag was given. */
1054 size_t strleng = str - *strptr;
1055 char *newstr;
1057 newstr = (char *) realloc (*strptr, strsize * 2);
1058 if (newstr == NULL)
1060 /* Can't allocate that much. Last-ditch
1061 effort. */
1062 newstr = (char *) realloc (*strptr,
1063 strleng + MB_CUR_MAX);
1064 if (newstr == NULL)
1066 if (flags & POSIX_MALLOC)
1068 done = EOF;
1069 goto errout;
1071 /* We lose. Oh well. Terminate the
1072 string and stop converting,
1073 so at least we don't skip any input. */
1074 ((char *) (*strptr))[strleng] = '\0';
1075 strptr = NULL;
1076 ++done;
1077 conv_error ();
1079 else
1081 *strptr = newstr;
1082 str = newstr + strleng;
1083 strsize = strleng + MB_CUR_MAX;
1086 else
1088 *strptr = newstr;
1089 str = newstr + strleng;
1090 strsize *= 2;
1094 n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c,
1095 &state);
1096 if (__glibc_unlikely (n == (size_t) -1))
1097 encode_error ();
1099 assert (n <= MB_CUR_MAX);
1100 str += n;
1102 #else
1103 /* This is easy. */
1104 if (!(flags & SUPPRESS))
1106 *str++ = c;
1107 if ((flags & MALLOC)
1108 && (char *) str == *strptr + strsize)
1110 /* Enlarge the buffer. */
1111 str = (char *) realloc (*strptr, 2 * strsize);
1112 if (str == NULL)
1114 /* Can't allocate that much. Last-ditch
1115 effort. */
1116 str = (char *) realloc (*strptr, strsize + 1);
1117 if (str == NULL)
1119 if (flags & POSIX_MALLOC)
1121 done = EOF;
1122 goto errout;
1124 /* We lose. Oh well. Terminate the
1125 string and stop converting,
1126 so at least we don't skip any input. */
1127 ((char *) (*strptr))[strsize - 1] = '\0';
1128 strptr = NULL;
1129 ++done;
1130 conv_error ();
1132 else
1134 *strptr = (char *) str;
1135 str += strsize;
1136 ++strsize;
1139 else
1141 *strptr = (char *) str;
1142 str += strsize;
1143 strsize *= 2;
1147 #endif
1149 while ((width <= 0 || --width > 0) && inchar () != EOF);
1151 if (!(flags & SUPPRESS))
1153 #ifdef COMPILE_WSCANF
1154 /* We have to emit the code to get into the initial
1155 state. */
1156 char buf[MB_LEN_MAX];
1157 size_t n = __wcrtomb (buf, L'\0', &state);
1158 if (n > 0 && (flags & MALLOC)
1159 && str + n >= *strptr + strsize)
1161 /* Enlarge the buffer. */
1162 size_t strleng = str - *strptr;
1163 char *newstr;
1165 newstr = (char *) realloc (*strptr, strleng + n + 1);
1166 if (newstr == NULL)
1168 if (flags & POSIX_MALLOC)
1170 done = EOF;
1171 goto errout;
1173 /* We lose. Oh well. Terminate the string
1174 and stop converting, so at least we don't
1175 skip any input. */
1176 ((char *) (*strptr))[strleng] = '\0';
1177 strptr = NULL;
1178 ++done;
1179 conv_error ();
1181 else
1183 *strptr = newstr;
1184 str = newstr + strleng;
1185 strsize = strleng + n + 1;
1189 str = __mempcpy (str, buf, n);
1190 #endif
1191 *str++ = '\0';
1193 if ((flags & MALLOC) && str - *strptr != strsize)
1195 char *cp = (char *) realloc (*strptr, str - *strptr);
1196 if (cp != NULL)
1197 *strptr = cp;
1199 strptr = NULL;
1201 ++done;
1203 break;
1205 /* FALLTHROUGH */
1207 case L_('S'):
1209 #ifndef COMPILE_WSCANF
1210 mbstate_t cstate;
1211 #endif
1213 /* Wide character string. */
1214 STRING_ARG (wstr, wchar_t, 100);
1216 c = inchar ();
1217 if (__builtin_expect (c == EOF, 0))
1218 input_error ();
1220 #ifndef COMPILE_WSCANF
1221 memset (&cstate, '\0', sizeof (cstate));
1222 #endif
1226 if (ISSPACE (c))
1228 ungetc_not_eof (c, s);
1229 break;
1232 #ifdef COMPILE_WSCANF
1233 /* This is easy. */
1234 if (!(flags & SUPPRESS))
1236 *wstr++ = c;
1237 if ((flags & MALLOC)
1238 && wstr == (wchar_t *) *strptr + strsize)
1240 /* Enlarge the buffer. */
1241 wstr = (wchar_t *) realloc (*strptr,
1242 (2 * strsize)
1243 * sizeof (wchar_t));
1244 if (wstr == NULL)
1246 /* Can't allocate that much. Last-ditch
1247 effort. */
1248 wstr = (wchar_t *) realloc (*strptr,
1249 (strsize + 1)
1250 * sizeof (wchar_t));
1251 if (wstr == NULL)
1253 if (flags & POSIX_MALLOC)
1255 done = EOF;
1256 goto errout;
1258 /* We lose. Oh well. Terminate the string
1259 and stop converting, so at least we don't
1260 skip any input. */
1261 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
1262 strptr = NULL;
1263 ++done;
1264 conv_error ();
1266 else
1268 *strptr = (char *) wstr;
1269 wstr += strsize;
1270 ++strsize;
1273 else
1275 *strptr = (char *) wstr;
1276 wstr += strsize;
1277 strsize *= 2;
1281 #else
1283 char buf[1];
1285 buf[0] = c;
1287 while (1)
1289 size_t n;
1291 n = __mbrtowc (!(flags & SUPPRESS) ? wstr : NULL,
1292 buf, 1, &cstate);
1294 if (n == (size_t) -2)
1296 /* Possibly correct character, just not enough
1297 input. */
1298 if (__glibc_unlikely (inchar () == EOF))
1299 encode_error ();
1301 buf[0] = c;
1302 continue;
1305 if (__glibc_unlikely (n != 1))
1306 encode_error ();
1308 /* We have a match. */
1309 ++wstr;
1310 break;
1313 if (!(flags & SUPPRESS) && (flags & MALLOC)
1314 && wstr == (wchar_t *) *strptr + strsize)
1316 /* Enlarge the buffer. */
1317 wstr = (wchar_t *) realloc (*strptr,
1318 (2 * strsize
1319 * sizeof (wchar_t)));
1320 if (wstr == NULL)
1322 /* Can't allocate that much. Last-ditch effort. */
1323 wstr = (wchar_t *) realloc (*strptr,
1324 ((strsize + 1)
1325 * sizeof (wchar_t)));
1326 if (wstr == NULL)
1328 if (flags & POSIX_MALLOC)
1330 done = EOF;
1331 goto errout;
1333 /* We lose. Oh well. Terminate the
1334 string and stop converting, so at
1335 least we don't skip any input. */
1336 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
1337 strptr = NULL;
1338 ++done;
1339 conv_error ();
1341 else
1343 *strptr = (char *) wstr;
1344 wstr += strsize;
1345 ++strsize;
1348 else
1350 *strptr = (char *) wstr;
1351 wstr += strsize;
1352 strsize *= 2;
1356 #endif
1358 while ((width <= 0 || --width > 0) && inchar () != EOF);
1360 if (!(flags & SUPPRESS))
1362 *wstr++ = L'\0';
1364 if ((flags & MALLOC) && wstr - (wchar_t *) *strptr != strsize)
1366 wchar_t *cp = (wchar_t *) realloc (*strptr,
1367 ((wstr
1368 - (wchar_t *) *strptr)
1369 * sizeof(wchar_t)));
1370 if (cp != NULL)
1371 *strptr = (char *) cp;
1373 strptr = NULL;
1375 ++done;
1378 break;
1380 case L_('x'): /* Hexadecimal integer. */
1381 case L_('X'): /* Ditto. */
1382 base = 16;
1383 goto number;
1385 case L_('o'): /* Octal integer. */
1386 base = 8;
1387 goto number;
1389 case L_('u'): /* Unsigned decimal integer. */
1390 base = 10;
1391 goto number;
1393 case L_('d'): /* Signed decimal integer. */
1394 base = 10;
1395 flags |= NUMBER_SIGNED;
1396 goto number;
1398 case L_('i'): /* Generic number. */
1399 base = 0;
1400 flags |= NUMBER_SIGNED;
1402 number:
1403 c = inchar ();
1404 if (__glibc_unlikely (c == EOF))
1405 input_error ();
1407 /* Check for a sign. */
1408 if (c == L_('-') || c == L_('+'))
1410 char_buffer_add (&charbuf, c);
1411 if (width > 0)
1412 --width;
1413 c = inchar ();
1416 /* Look for a leading indication of base. */
1417 if (width != 0 && c == L_('0'))
1419 if (width > 0)
1420 --width;
1422 char_buffer_add (&charbuf, c);
1423 c = inchar ();
1425 if (width != 0 && TOLOWER (c) == L_('x'))
1427 if (base == 0)
1428 base = 16;
1429 if (base == 16)
1431 if (width > 0)
1432 --width;
1433 c = inchar ();
1436 else if (base == 0)
1437 base = 8;
1440 if (base == 0)
1441 base = 10;
1443 if (base == 10 && __builtin_expect ((flags & I18N) != 0, 0))
1445 int from_level;
1446 int to_level;
1447 int level;
1448 #ifdef COMPILE_WSCANF
1449 const wchar_t *wcdigits[10];
1450 const wchar_t *wcdigits_extended[10];
1451 #else
1452 const char *mbdigits[10];
1453 const char *mbdigits_extended[10];
1454 #endif
1455 /* "to_inpunct" is a map from ASCII digits to their
1456 equivalent in locale. This is defined for locales
1457 which use an extra digits set. */
1458 wctrans_t map = __wctrans ("to_inpunct");
1459 int n;
1461 from_level = 0;
1462 #ifdef COMPILE_WSCANF
1463 to_level = _NL_CURRENT_WORD (LC_CTYPE,
1464 _NL_CTYPE_INDIGITS_WC_LEN) - 1;
1465 #else
1466 to_level = (uint32_t) curctype->values[_NL_ITEM_INDEX (_NL_CTYPE_INDIGITS_MB_LEN)].word - 1;
1467 #endif
1469 /* Get the alternative digit forms if there are any. */
1470 if (__glibc_unlikely (map != NULL))
1472 /* Adding new level for extra digits set in locale file. */
1473 ++to_level;
1475 for (n = 0; n < 10; ++n)
1477 #ifdef COMPILE_WSCANF
1478 wcdigits[n] = (const wchar_t *)
1479 _NL_CURRENT (LC_CTYPE, _NL_CTYPE_INDIGITS0_WC + n);
1481 wchar_t *wc_extended = (wchar_t *)
1482 alloca ((to_level + 2) * sizeof (wchar_t));
1483 __wmemcpy (wc_extended, wcdigits[n], to_level);
1484 wc_extended[to_level] = __towctrans (L'0' + n, map);
1485 wc_extended[to_level + 1] = '\0';
1486 wcdigits_extended[n] = wc_extended;
1487 #else
1488 mbdigits[n]
1489 = curctype->values[_NL_CTYPE_INDIGITS0_MB + n].string;
1491 /* Get the equivalent wide char in map. */
1492 wint_t extra_wcdigit = __towctrans (L'0' + n, map);
1494 /* Convert it to multibyte representation. */
1495 mbstate_t state;
1496 memset (&state, '\0', sizeof (state));
1498 char extra_mbdigit[MB_LEN_MAX];
1499 size_t mblen
1500 = __wcrtomb (extra_mbdigit, extra_wcdigit, &state);
1502 if (mblen == (size_t) -1)
1504 /* Ignore this new level. */
1505 map = NULL;
1506 break;
1509 /* Calculate the length of mbdigits[n]. */
1510 const char *last_char = mbdigits[n];
1511 for (level = 0; level < to_level; ++level)
1512 last_char = strchr (last_char, '\0') + 1;
1514 size_t mbdigits_len = last_char - mbdigits[n];
1516 /* Allocate memory for extended multibyte digit. */
1517 char *mb_extended;
1518 mb_extended = (char *) alloca (mbdigits_len + mblen + 1);
1520 /* And get the mbdigits + extra_digit string. */
1521 *(char *) __mempcpy (__mempcpy (mb_extended, mbdigits[n],
1522 mbdigits_len),
1523 extra_mbdigit, mblen) = '\0';
1524 mbdigits_extended[n] = mb_extended;
1525 #endif
1529 /* Read the number into workspace. */
1530 while (c != EOF && width != 0)
1532 /* In this round we get the pointer to the digit strings
1533 and also perform the first round of comparisons. */
1534 for (n = 0; n < 10; ++n)
1536 /* Get the string for the digits with value N. */
1537 #ifdef COMPILE_WSCANF
1538 if (__glibc_unlikely (map != NULL))
1539 wcdigits[n] = wcdigits_extended[n];
1540 else
1541 wcdigits[n] = (const wchar_t *)
1542 _NL_CURRENT (LC_CTYPE, _NL_CTYPE_INDIGITS0_WC + n);
1543 wcdigits[n] += from_level;
1545 if (c == (wint_t) *wcdigits[n])
1547 to_level = from_level;
1548 break;
1551 /* Advance the pointer to the next string. */
1552 ++wcdigits[n];
1553 #else
1554 const char *cmpp;
1555 int avail = width > 0 ? width : INT_MAX;
1557 if (__glibc_unlikely (map != NULL))
1558 mbdigits[n] = mbdigits_extended[n];
1559 else
1560 mbdigits[n]
1561 = curctype->values[_NL_CTYPE_INDIGITS0_MB + n].string;
1563 for (level = 0; level < from_level; level++)
1564 mbdigits[n] = strchr (mbdigits[n], '\0') + 1;
1566 cmpp = mbdigits[n];
1567 while ((unsigned char) *cmpp == c && avail >= 0)
1569 if (*++cmpp == '\0')
1570 break;
1571 else
1573 if (avail == 0 || inchar () == EOF)
1574 break;
1575 --avail;
1579 if (*cmpp == '\0')
1581 if (width > 0)
1582 width = avail;
1583 to_level = from_level;
1584 break;
1587 /* We are pushing all read characters back. */
1588 if (cmpp > mbdigits[n])
1590 ungetc (c, s);
1591 while (--cmpp > mbdigits[n])
1592 ungetc_not_eof ((unsigned char) *cmpp, s);
1593 c = (unsigned char) *cmpp;
1596 /* Advance the pointer to the next string. */
1597 mbdigits[n] = strchr (mbdigits[n], '\0') + 1;
1598 #endif
1601 if (n == 10)
1603 /* Have not yet found the digit. */
1604 for (level = from_level + 1; level <= to_level; ++level)
1606 /* Search all ten digits of this level. */
1607 for (n = 0; n < 10; ++n)
1609 #ifdef COMPILE_WSCANF
1610 if (c == (wint_t) *wcdigits[n])
1611 break;
1613 /* Advance the pointer to the next string. */
1614 ++wcdigits[n];
1615 #else
1616 const char *cmpp;
1617 int avail = width > 0 ? width : INT_MAX;
1619 cmpp = mbdigits[n];
1620 while ((unsigned char) *cmpp == c && avail >= 0)
1622 if (*++cmpp == '\0')
1623 break;
1624 else
1626 if (avail == 0 || inchar () == EOF)
1627 break;
1628 --avail;
1632 if (*cmpp == '\0')
1634 if (width > 0)
1635 width = avail;
1636 break;
1639 /* We are pushing all read characters back. */
1640 if (cmpp > mbdigits[n])
1642 ungetc (c, s);
1643 while (--cmpp > mbdigits[n])
1644 ungetc_not_eof ((unsigned char) *cmpp, s);
1645 c = (unsigned char) *cmpp;
1648 /* Advance the pointer to the next string. */
1649 mbdigits[n] = strchr (mbdigits[n], '\0') + 1;
1650 #endif
1653 if (n < 10)
1655 /* Found it. */
1656 from_level = level;
1657 to_level = level;
1658 break;
1663 if (n < 10)
1664 c = L_('0') + n;
1665 else if (flags & GROUP)
1667 /* Try matching against the thousands separator. */
1668 #ifdef COMPILE_WSCANF
1669 if (c != thousands)
1670 break;
1671 #else
1672 const char *cmpp = thousands;
1673 int avail = width > 0 ? width : INT_MAX;
1675 while ((unsigned char) *cmpp == c && avail >= 0)
1677 char_buffer_add (&charbuf, c);
1678 if (*++cmpp == '\0')
1679 break;
1680 else
1682 if (avail == 0 || inchar () == EOF)
1683 break;
1684 --avail;
1688 if (char_buffer_error (&charbuf))
1690 __set_errno (ENOMEM);
1691 done = EOF;
1692 goto errout;
1695 if (*cmpp != '\0')
1697 /* We are pushing all read characters back. */
1698 if (cmpp > thousands)
1700 charbuf.current -= cmpp - thousands;
1701 ungetc (c, s);
1702 while (--cmpp > thousands)
1703 ungetc_not_eof ((unsigned char) *cmpp, s);
1704 c = (unsigned char) *cmpp;
1706 break;
1709 if (width > 0)
1710 width = avail;
1712 /* The last thousands character will be added back by
1713 the char_buffer_add below. */
1714 --charbuf.current;
1715 #endif
1717 else
1718 break;
1720 char_buffer_add (&charbuf, c);
1721 if (width > 0)
1722 --width;
1724 c = inchar ();
1727 else
1728 /* Read the number into workspace. */
1729 while (c != EOF && width != 0)
1731 if (base == 16)
1733 if (!ISXDIGIT (c))
1734 break;
1736 else if (!ISDIGIT (c) || (int) (c - L_('0')) >= base)
1738 if (base == 10 && (flags & GROUP))
1740 /* Try matching against the thousands separator. */
1741 #ifdef COMPILE_WSCANF
1742 if (c != thousands)
1743 break;
1744 #else
1745 const char *cmpp = thousands;
1746 int avail = width > 0 ? width : INT_MAX;
1748 while ((unsigned char) *cmpp == c && avail >= 0)
1750 char_buffer_add (&charbuf, c);
1751 if (*++cmpp == '\0')
1752 break;
1753 else
1755 if (avail == 0 || inchar () == EOF)
1756 break;
1757 --avail;
1761 if (char_buffer_error (&charbuf))
1763 __set_errno (ENOMEM);
1764 done = EOF;
1765 goto errout;
1768 if (*cmpp != '\0')
1770 /* We are pushing all read characters back. */
1771 if (cmpp > thousands)
1773 charbuf.current -= cmpp - thousands;
1774 ungetc (c, s);
1775 while (--cmpp > thousands)
1776 ungetc_not_eof ((unsigned char) *cmpp, s);
1777 c = (unsigned char) *cmpp;
1779 break;
1782 if (width > 0)
1783 width = avail;
1785 /* The last thousands character will be added back by
1786 the char_buffer_add below. */
1787 --charbuf.current;
1788 #endif
1790 else
1791 break;
1793 char_buffer_add (&charbuf, c);
1794 if (width > 0)
1795 --width;
1797 c = inchar ();
1800 if (char_buffer_error (&charbuf))
1802 __set_errno (ENOMEM);
1803 done = EOF;
1804 goto errout;
1807 if (char_buffer_size (&charbuf) == 0
1808 || (char_buffer_size (&charbuf) == 1
1809 && (char_buffer_start (&charbuf)[0] == L_('+')
1810 || char_buffer_start (&charbuf)[0] == L_('-'))))
1812 /* There was no number. If we are supposed to read a pointer
1813 we must recognize "(nil)" as well. */
1814 if (__builtin_expect (char_buffer_size (&charbuf) == 0
1815 && (flags & READ_POINTER)
1816 && (width < 0 || width >= 5)
1817 && c == '('
1818 && TOLOWER (inchar ()) == L_('n')
1819 && TOLOWER (inchar ()) == L_('i')
1820 && TOLOWER (inchar ()) == L_('l')
1821 && inchar () == L_(')'), 1))
1822 /* We must produce the value of a NULL pointer. A single
1823 '0' digit is enough. */
1824 char_buffer_add (&charbuf, L_('0'));
1825 else
1827 /* The last read character is not part of the number
1828 anymore. */
1829 ungetc (c, s);
1831 conv_error ();
1834 else
1835 /* The just read character is not part of the number anymore. */
1836 ungetc (c, s);
1838 /* Convert the number. */
1839 char_buffer_add (&charbuf, L_('\0'));
1840 if (char_buffer_error (&charbuf))
1842 __set_errno (ENOMEM);
1843 done = EOF;
1844 goto errout;
1846 if (need_longlong && (flags & LONGDBL))
1848 if (flags & NUMBER_SIGNED)
1849 num.q = __strtoll_internal
1850 (char_buffer_start (&charbuf), &tw, base, flags & GROUP);
1851 else
1852 num.uq = __strtoull_internal
1853 (char_buffer_start (&charbuf), &tw, base, flags & GROUP);
1855 else
1857 if (flags & NUMBER_SIGNED)
1858 num.l = __strtol_internal
1859 (char_buffer_start (&charbuf), &tw, base, flags & GROUP);
1860 else
1861 num.ul = __strtoul_internal
1862 (char_buffer_start (&charbuf), &tw, base, flags & GROUP);
1864 if (__glibc_unlikely (char_buffer_start (&charbuf) == tw))
1865 conv_error ();
1867 if (!(flags & SUPPRESS))
1869 if (flags & NUMBER_SIGNED)
1871 if (need_longlong && (flags & LONGDBL))
1872 *ARG (LONGLONG int *) = num.q;
1873 else if (need_long && (flags & LONG))
1874 *ARG (long int *) = num.l;
1875 else if (flags & SHORT)
1876 *ARG (short int *) = (short int) num.l;
1877 else if (!(flags & CHAR))
1878 *ARG (int *) = (int) num.l;
1879 else
1880 *ARG (signed char *) = (signed char) num.ul;
1882 else
1884 if (need_longlong && (flags & LONGDBL))
1885 *ARG (unsigned LONGLONG int *) = num.uq;
1886 else if (need_long && (flags & LONG))
1887 *ARG (unsigned long int *) = num.ul;
1888 else if (flags & SHORT)
1889 *ARG (unsigned short int *)
1890 = (unsigned short int) num.ul;
1891 else if (!(flags & CHAR))
1892 *ARG (unsigned int *) = (unsigned int) num.ul;
1893 else
1894 *ARG (unsigned char *) = (unsigned char) num.ul;
1896 ++done;
1898 break;
1900 case L_('e'): /* Floating-point numbers. */
1901 case L_('E'):
1902 case L_('f'):
1903 case L_('F'):
1904 case L_('g'):
1905 case L_('G'):
1906 case L_('a'):
1907 case L_('A'):
1908 c = inchar ();
1909 if (width > 0)
1910 --width;
1911 if (__glibc_unlikely (c == EOF))
1912 input_error ();
1914 got_digit = got_dot = got_e = 0;
1916 /* Check for a sign. */
1917 if (c == L_('-') || c == L_('+'))
1919 negative = c == L_('-');
1920 if (__glibc_unlikely (width == 0 || inchar () == EOF))
1921 /* EOF is only an input error before we read any chars. */
1922 conv_error ();
1923 if (width > 0)
1924 --width;
1926 else
1927 negative = 0;
1929 /* Take care for the special arguments "nan" and "inf". */
1930 if (TOLOWER (c) == L_('n'))
1932 /* Maybe "nan". */
1933 char_buffer_add (&charbuf, c);
1934 if (__builtin_expect (width == 0
1935 || inchar () == EOF
1936 || TOLOWER (c) != L_('a'), 0))
1937 conv_error ();
1938 if (width > 0)
1939 --width;
1940 char_buffer_add (&charbuf, c);
1941 if (__builtin_expect (width == 0
1942 || inchar () == EOF
1943 || TOLOWER (c) != L_('n'), 0))
1944 conv_error ();
1945 if (width > 0)
1946 --width;
1947 char_buffer_add (&charbuf, c);
1948 /* It is "nan". */
1949 goto scan_float;
1951 else if (TOLOWER (c) == L_('i'))
1953 /* Maybe "inf" or "infinity". */
1954 char_buffer_add (&charbuf, c);
1955 if (__builtin_expect (width == 0
1956 || inchar () == EOF
1957 || TOLOWER (c) != L_('n'), 0))
1958 conv_error ();
1959 if (width > 0)
1960 --width;
1961 char_buffer_add (&charbuf, c);
1962 if (__builtin_expect (width == 0
1963 || inchar () == EOF
1964 || TOLOWER (c) != L_('f'), 0))
1965 conv_error ();
1966 if (width > 0)
1967 --width;
1968 char_buffer_add (&charbuf, c);
1969 /* It is as least "inf". */
1970 if (width != 0 && inchar () != EOF)
1972 if (TOLOWER (c) == L_('i'))
1974 if (width > 0)
1975 --width;
1976 /* Now we have to read the rest as well. */
1977 char_buffer_add (&charbuf, c);
1978 if (__builtin_expect (width == 0
1979 || inchar () == EOF
1980 || TOLOWER (c) != L_('n'), 0))
1981 conv_error ();
1982 if (width > 0)
1983 --width;
1984 char_buffer_add (&charbuf, c);
1985 if (__builtin_expect (width == 0
1986 || inchar () == EOF
1987 || TOLOWER (c) != L_('i'), 0))
1988 conv_error ();
1989 if (width > 0)
1990 --width;
1991 char_buffer_add (&charbuf, c);
1992 if (__builtin_expect (width == 0
1993 || inchar () == EOF
1994 || TOLOWER (c) != L_('t'), 0))
1995 conv_error ();
1996 if (width > 0)
1997 --width;
1998 char_buffer_add (&charbuf, c);
1999 if (__builtin_expect (width == 0
2000 || inchar () == EOF
2001 || TOLOWER (c) != L_('y'), 0))
2002 conv_error ();
2003 if (width > 0)
2004 --width;
2005 char_buffer_add (&charbuf, c);
2007 else
2008 /* Never mind. */
2009 ungetc (c, s);
2011 goto scan_float;
2014 exp_char = L_('e');
2015 if (width != 0 && c == L_('0'))
2017 char_buffer_add (&charbuf, c);
2018 c = inchar ();
2019 if (width > 0)
2020 --width;
2021 if (width != 0 && TOLOWER (c) == L_('x'))
2023 /* It is a number in hexadecimal format. */
2024 char_buffer_add (&charbuf, c);
2026 flags |= HEXA_FLOAT;
2027 exp_char = L_('p');
2029 /* Grouping is not allowed. */
2030 flags &= ~GROUP;
2031 c = inchar ();
2032 if (width > 0)
2033 --width;
2035 else
2036 got_digit = 1;
2039 while (1)
2041 if (char_buffer_error (&charbuf))
2043 __set_errno (ENOMEM);
2044 done = EOF;
2045 goto errout;
2047 if (ISDIGIT (c))
2049 char_buffer_add (&charbuf, c);
2050 got_digit = 1;
2052 else if (!got_e && (flags & HEXA_FLOAT) && ISXDIGIT (c))
2054 char_buffer_add (&charbuf, c);
2055 got_digit = 1;
2057 else if (got_e && charbuf.current[-1] == exp_char
2058 && (c == L_('-') || c == L_('+')))
2059 char_buffer_add (&charbuf, c);
2060 else if (got_digit && !got_e
2061 && (CHAR_T) TOLOWER (c) == exp_char)
2063 char_buffer_add (&charbuf, exp_char);
2064 got_e = got_dot = 1;
2066 else
2068 #ifdef COMPILE_WSCANF
2069 if (! got_dot && c == decimal)
2071 char_buffer_add (&charbuf, c);
2072 got_dot = 1;
2074 else if ((flags & GROUP) != 0 && ! got_dot && c == thousands)
2075 char_buffer_add (&charbuf, c);
2076 else
2078 /* The last read character is not part of the number
2079 anymore. */
2080 ungetc (c, s);
2081 break;
2083 #else
2084 const char *cmpp = decimal;
2085 int avail = width > 0 ? width : INT_MAX;
2087 if (! got_dot)
2089 while ((unsigned char) *cmpp == c && avail >= 0)
2090 if (*++cmpp == '\0')
2091 break;
2092 else
2094 if (avail == 0 || inchar () == EOF)
2095 break;
2096 --avail;
2100 if (*cmpp == '\0')
2102 /* Add all the characters. */
2103 for (cmpp = decimal; *cmpp != '\0'; ++cmpp)
2104 char_buffer_add (&charbuf, (unsigned char) *cmpp);
2105 if (width > 0)
2106 width = avail;
2107 got_dot = 1;
2109 else
2111 /* Figure out whether it is a thousands separator.
2112 There is one problem: we possibly read more than
2113 one character. We cannot push them back but since
2114 we know that parts of the `decimal' string matched,
2115 we can compare against it. */
2116 const char *cmp2p = thousands;
2118 if ((flags & GROUP) != 0 && ! got_dot)
2120 while (cmp2p - thousands < cmpp - decimal
2121 && *cmp2p == decimal[cmp2p - thousands])
2122 ++cmp2p;
2123 if (cmp2p - thousands == cmpp - decimal)
2125 while ((unsigned char) *cmp2p == c && avail >= 0)
2126 if (*++cmp2p == '\0')
2127 break;
2128 else
2130 if (avail == 0 || inchar () == EOF)
2131 break;
2132 --avail;
2137 if (cmp2p != NULL && *cmp2p == '\0')
2139 /* Add all the characters. */
2140 for (cmpp = thousands; *cmpp != '\0'; ++cmpp)
2141 char_buffer_add (&charbuf, (unsigned char) *cmpp);
2142 if (width > 0)
2143 width = avail;
2145 else
2147 /* The last read character is not part of the number
2148 anymore. */
2149 ungetc (c, s);
2150 break;
2153 #endif
2156 if (width == 0 || inchar () == EOF)
2157 break;
2159 if (width > 0)
2160 --width;
2163 if (char_buffer_error (&charbuf))
2165 __set_errno (ENOMEM);
2166 done = EOF;
2167 goto errout;
2170 wctrans_t map;
2171 if (__builtin_expect ((flags & I18N) != 0, 0)
2172 /* Hexadecimal floats make no sense, fixing localized
2173 digits with ASCII letters. */
2174 && !(flags & HEXA_FLOAT)
2175 /* Minimum requirement. */
2176 && (char_buffer_size (&charbuf) == 0 || got_dot)
2177 && (map = __wctrans ("to_inpunct")) != NULL)
2179 /* Reget the first character. */
2180 inchar ();
2182 /* Localized digits, decimal points, and thousands
2183 separator. */
2184 wint_t wcdigits[12];
2186 /* First get decimal equivalent to check if we read it
2187 or not. */
2188 wcdigits[11] = __towctrans (L'.', map);
2190 /* If we have not read any character or have just read
2191 locale decimal point which matches the decimal point
2192 for localized FP numbers, then we may have localized
2193 digits. Note, we test GOT_DOT above. */
2194 #ifdef COMPILE_WSCANF
2195 if (char_buffer_size (&charbuf) == 0
2196 || (char_buffer_size (&charbuf) == 1
2197 && wcdigits[11] == decimal))
2198 #else
2199 char mbdigits[12][MB_LEN_MAX + 1];
2201 mbstate_t state;
2202 memset (&state, '\0', sizeof (state));
2204 bool match_so_far = char_buffer_size (&charbuf) == 0;
2205 size_t mblen = __wcrtomb (mbdigits[11], wcdigits[11], &state);
2206 if (mblen != (size_t) -1)
2208 mbdigits[11][mblen] = '\0';
2209 match_so_far |=
2210 (char_buffer_size (&charbuf) == strlen (decimal)
2211 && strcmp (decimal, mbdigits[11]) == 0);
2213 else
2215 size_t decimal_len = strlen (decimal);
2216 /* This should always be the case but the data comes
2217 from a file. */
2218 if (decimal_len <= MB_LEN_MAX)
2220 match_so_far |= char_buffer_size (&charbuf) == decimal_len;
2221 memcpy (mbdigits[11], decimal, decimal_len + 1);
2223 else
2224 match_so_far = false;
2227 if (match_so_far)
2228 #endif
2230 bool have_locthousands = (flags & GROUP) != 0;
2232 /* Now get the digits and the thousands-sep equivalents. */
2233 for (int n = 0; n < 11; ++n)
2235 if (n < 10)
2236 wcdigits[n] = __towctrans (L'0' + n, map);
2237 else if (n == 10)
2239 wcdigits[10] = __towctrans (L',', map);
2240 have_locthousands &= wcdigits[10] != L'\0';
2243 #ifndef COMPILE_WSCANF
2244 memset (&state, '\0', sizeof (state));
2246 size_t mblen = __wcrtomb (mbdigits[n], wcdigits[n],
2247 &state);
2248 if (mblen == (size_t) -1)
2250 if (n == 10)
2252 if (have_locthousands)
2254 size_t thousands_len = strlen (thousands);
2255 if (thousands_len <= MB_LEN_MAX)
2256 memcpy (mbdigits[10], thousands,
2257 thousands_len + 1);
2258 else
2259 have_locthousands = false;
2262 else
2263 /* Ignore checking against localized digits. */
2264 goto no_i18nflt;
2266 else
2267 mbdigits[n][mblen] = '\0';
2268 #endif
2271 /* Start checking against localized digits, if
2272 conversion is done correctly. */
2273 while (1)
2275 if (char_buffer_error (&charbuf))
2277 __set_errno (ENOMEM);
2278 done = EOF;
2279 goto errout;
2281 if (got_e && charbuf.current[-1] == exp_char
2282 && (c == L_('-') || c == L_('+')))
2283 char_buffer_add (&charbuf, c);
2284 else if (char_buffer_size (&charbuf) > 0 && !got_e
2285 && (CHAR_T) TOLOWER (c) == exp_char)
2287 char_buffer_add (&charbuf, exp_char);
2288 got_e = got_dot = 1;
2290 else
2292 /* Check against localized digits, decimal point,
2293 and thousands separator. */
2294 int n;
2295 for (n = 0; n < 12; ++n)
2297 #ifdef COMPILE_WSCANF
2298 if (c == wcdigits[n])
2300 if (n < 10)
2301 char_buffer_add (&charbuf, L_('0') + n);
2302 else if (n == 11 && !got_dot)
2304 char_buffer_add (&charbuf, decimal);
2305 got_dot = 1;
2307 else if (n == 10 && have_locthousands
2308 && ! got_dot)
2309 char_buffer_add (&charbuf, thousands);
2310 else
2311 /* The last read character is not part
2312 of the number anymore. */
2313 n = 12;
2315 break;
2317 #else
2318 const char *cmpp = mbdigits[n];
2319 int avail = width > 0 ? width : INT_MAX;
2321 while ((unsigned char) *cmpp == c && avail >= 0)
2322 if (*++cmpp == '\0')
2323 break;
2324 else
2326 if (avail == 0 || inchar () == EOF)
2327 break;
2328 --avail;
2330 if (*cmpp == '\0')
2332 if (width > 0)
2333 width = avail;
2335 if (n < 10)
2336 char_buffer_add (&charbuf, L_('0') + n);
2337 else if (n == 11 && !got_dot)
2339 /* Add all the characters. */
2340 for (cmpp = decimal; *cmpp != '\0';
2341 ++cmpp)
2342 char_buffer_add (&charbuf,
2343 (unsigned char) *cmpp);
2345 got_dot = 1;
2347 else if (n == 10 && (flags & GROUP) != 0
2348 && ! got_dot)
2350 /* Add all the characters. */
2351 for (cmpp = thousands; *cmpp != '\0';
2352 ++cmpp)
2353 char_buffer_add (&charbuf,
2354 (unsigned char) *cmpp);
2356 else
2357 /* The last read character is not part
2358 of the number anymore. */
2359 n = 12;
2361 break;
2364 /* We are pushing all read characters back. */
2365 if (cmpp > mbdigits[n])
2367 ungetc (c, s);
2368 while (--cmpp > mbdigits[n])
2369 ungetc_not_eof ((unsigned char) *cmpp, s);
2370 c = (unsigned char) *cmpp;
2372 #endif
2375 if (n >= 12)
2377 /* The last read character is not part
2378 of the number anymore. */
2379 ungetc (c, s);
2380 break;
2384 if (width == 0 || inchar () == EOF)
2385 break;
2387 if (width > 0)
2388 --width;
2392 #ifndef COMPILE_WSCANF
2393 no_i18nflt:
2395 #endif
2398 if (char_buffer_error (&charbuf))
2400 __set_errno (ENOMEM);
2401 done = EOF;
2402 goto errout;
2405 /* Have we read any character? If we try to read a number
2406 in hexadecimal notation and we have read only the `0x'
2407 prefix this is an error. */
2408 if (__glibc_unlikely (char_buffer_size (&charbuf) == 0
2409 || ((flags & HEXA_FLOAT)
2410 && char_buffer_size (&charbuf) == 2)))
2411 conv_error ();
2413 scan_float:
2414 /* Convert the number. */
2415 char_buffer_add (&charbuf, L_('\0'));
2416 if (char_buffer_error (&charbuf))
2418 __set_errno (ENOMEM);
2419 done = EOF;
2420 goto errout;
2422 if ((flags & LONGDBL) && !__ldbl_is_dbl)
2424 long double d = __strtold_internal
2425 (char_buffer_start (&charbuf), &tw, flags & GROUP);
2426 if (!(flags & SUPPRESS) && tw != char_buffer_start (&charbuf))
2427 *ARG (long double *) = negative ? -d : d;
2429 else if (flags & (LONG | LONGDBL))
2431 double d = __strtod_internal
2432 (char_buffer_start (&charbuf), &tw, flags & GROUP);
2433 if (!(flags & SUPPRESS) && tw != char_buffer_start (&charbuf))
2434 *ARG (double *) = negative ? -d : d;
2436 else
2438 float d = __strtof_internal
2439 (char_buffer_start (&charbuf), &tw, flags & GROUP);
2440 if (!(flags & SUPPRESS) && tw != char_buffer_start (&charbuf))
2441 *ARG (float *) = negative ? -d : d;
2444 if (__glibc_unlikely (tw == char_buffer_start (&charbuf)))
2445 conv_error ();
2447 if (!(flags & SUPPRESS))
2448 ++done;
2449 break;
2451 case L_('['): /* Character class. */
2452 if (flags & LONG)
2453 STRING_ARG (wstr, wchar_t, 100);
2454 else
2455 STRING_ARG (str, char, 100);
2457 if (*f == L_('^'))
2459 ++f;
2460 not_in = 1;
2462 else
2463 not_in = 0;
2465 if (width < 0)
2466 /* There is no width given so there is also no limit on the
2467 number of characters we read. Therefore we set width to
2468 a very high value to make the algorithm easier. */
2469 width = INT_MAX;
2471 #ifdef COMPILE_WSCANF
2472 /* Find the beginning and the end of the scanlist. We are not
2473 creating a lookup table since it would have to be too large.
2474 Instead we search each time through the string. This is not
2475 a constant lookup time but who uses this feature deserves to
2476 be punished. */
2477 tw = (wchar_t *) f; /* Marks the beginning. */
2479 if (*f == L']')
2480 ++f;
2482 while ((fc = *f++) != L'\0' && fc != L']');
2484 if (__glibc_unlikely (fc == L'\0'))
2485 conv_error ();
2486 wchar_t *twend = (wchar_t *) f - 1;
2487 #else
2488 /* Fill WP with byte flags indexed by character.
2489 We will use this flag map for matching input characters. */
2490 if (!scratch_buffer_set_array_size
2491 (&charbuf.scratch, UCHAR_MAX + 1, 1))
2493 done = EOF;
2494 goto errout;
2496 memset (charbuf.scratch.data, '\0', UCHAR_MAX + 1);
2498 fc = *f;
2499 if (fc == ']' || fc == '-')
2501 /* If ] or - appears before any char in the set, it is not
2502 the terminator or separator, but the first char in the
2503 set. */
2504 ((char *)charbuf.scratch.data)[fc] = 1;
2505 ++f;
2508 while ((fc = *f++) != '\0' && fc != ']')
2509 if (fc == '-' && *f != '\0' && *f != ']'
2510 && (unsigned char) f[-2] <= (unsigned char) *f)
2512 /* Add all characters from the one before the '-'
2513 up to (but not including) the next format char. */
2514 for (fc = (unsigned char) f[-2]; fc < (unsigned char) *f; ++fc)
2515 ((char *)charbuf.scratch.data)[fc] = 1;
2517 else
2518 /* Add the character to the flag map. */
2519 ((char *)charbuf.scratch.data)[fc] = 1;
2521 if (__glibc_unlikely (fc == '\0'))
2522 conv_error();
2523 #endif
2525 if (flags & LONG)
2527 size_t now = read_in;
2528 #ifdef COMPILE_WSCANF
2529 if (__glibc_unlikely (inchar () == WEOF))
2530 input_error ();
2534 wchar_t *runp;
2536 /* Test whether it's in the scanlist. */
2537 runp = tw;
2538 while (runp < twend)
2540 if (runp[0] == L'-' && runp[1] != '\0'
2541 && runp + 1 != twend
2542 && runp != tw
2543 && (unsigned int) runp[-1] <= (unsigned int) runp[1])
2545 /* Match against all characters in between the
2546 first and last character of the sequence. */
2547 wchar_t wc;
2549 for (wc = runp[-1] + 1; wc <= runp[1]; ++wc)
2550 if ((wint_t) wc == c)
2551 break;
2553 if (wc <= runp[1] && !not_in)
2554 break;
2555 if (wc <= runp[1] && not_in)
2557 /* The current character is not in the
2558 scanset. */
2559 ungetc (c, s);
2560 goto out;
2563 runp += 2;
2565 else
2567 if ((wint_t) *runp == c && !not_in)
2568 break;
2569 if ((wint_t) *runp == c && not_in)
2571 ungetc (c, s);
2572 goto out;
2575 ++runp;
2579 if (runp == twend && !not_in)
2581 ungetc (c, s);
2582 goto out;
2585 if (!(flags & SUPPRESS))
2587 *wstr++ = c;
2589 if ((flags & MALLOC)
2590 && wstr == (wchar_t *) *strptr + strsize)
2592 /* Enlarge the buffer. */
2593 wstr = (wchar_t *) realloc (*strptr,
2594 (2 * strsize)
2595 * sizeof (wchar_t));
2596 if (wstr == NULL)
2598 /* Can't allocate that much. Last-ditch
2599 effort. */
2600 wstr = (wchar_t *)
2601 realloc (*strptr, (strsize + 1)
2602 * sizeof (wchar_t));
2603 if (wstr == NULL)
2605 if (flags & POSIX_MALLOC)
2607 done = EOF;
2608 goto errout;
2610 /* We lose. Oh well. Terminate the string
2611 and stop converting, so at least we don't
2612 skip any input. */
2613 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
2614 strptr = NULL;
2615 ++done;
2616 conv_error ();
2618 else
2620 *strptr = (char *) wstr;
2621 wstr += strsize;
2622 ++strsize;
2625 else
2627 *strptr = (char *) wstr;
2628 wstr += strsize;
2629 strsize *= 2;
2634 while (--width > 0 && inchar () != WEOF);
2635 out:
2636 #else
2637 char buf[MB_LEN_MAX];
2638 size_t cnt = 0;
2639 mbstate_t cstate;
2641 if (__glibc_unlikely (inchar () == EOF))
2642 input_error ();
2644 memset (&cstate, '\0', sizeof (cstate));
2648 if (((char *) charbuf.scratch.data)[c] == not_in)
2650 ungetc_not_eof (c, s);
2651 break;
2654 /* This is easy. */
2655 if (!(flags & SUPPRESS))
2657 size_t n;
2659 /* Convert it into a wide character. */
2660 buf[0] = c;
2661 n = __mbrtowc (wstr, buf, 1, &cstate);
2663 if (n == (size_t) -2)
2665 /* Possibly correct character, just not enough
2666 input. */
2667 ++cnt;
2668 assert (cnt < MB_CUR_MAX);
2669 continue;
2671 cnt = 0;
2673 ++wstr;
2674 if ((flags & MALLOC)
2675 && wstr == (wchar_t *) *strptr + strsize)
2677 /* Enlarge the buffer. */
2678 wstr = (wchar_t *) realloc (*strptr,
2679 (2 * strsize
2680 * sizeof (wchar_t)));
2681 if (wstr == NULL)
2683 /* Can't allocate that much. Last-ditch
2684 effort. */
2685 wstr = (wchar_t *)
2686 realloc (*strptr, ((strsize + 1)
2687 * sizeof (wchar_t)));
2688 if (wstr == NULL)
2690 if (flags & POSIX_MALLOC)
2692 done = EOF;
2693 goto errout;
2695 /* We lose. Oh well. Terminate the
2696 string and stop converting,
2697 so at least we don't skip any input. */
2698 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
2699 strptr = NULL;
2700 ++done;
2701 conv_error ();
2703 else
2705 *strptr = (char *) wstr;
2706 wstr += strsize;
2707 ++strsize;
2710 else
2712 *strptr = (char *) wstr;
2713 wstr += strsize;
2714 strsize *= 2;
2719 if (--width <= 0)
2720 break;
2722 while (inchar () != EOF);
2724 if (__glibc_unlikely (cnt != 0))
2725 /* We stopped in the middle of recognizing another
2726 character. That's a problem. */
2727 encode_error ();
2728 #endif
2730 if (__glibc_unlikely (now == read_in))
2731 /* We haven't succesfully read any character. */
2732 conv_error ();
2734 if (!(flags & SUPPRESS))
2736 *wstr++ = L'\0';
2738 if ((flags & MALLOC)
2739 && wstr - (wchar_t *) *strptr != strsize)
2741 wchar_t *cp = (wchar_t *)
2742 realloc (*strptr, ((wstr - (wchar_t *) *strptr)
2743 * sizeof(wchar_t)));
2744 if (cp != NULL)
2745 *strptr = (char *) cp;
2747 strptr = NULL;
2749 ++done;
2752 else
2754 size_t now = read_in;
2756 if (__glibc_unlikely (inchar () == EOF))
2757 input_error ();
2759 #ifdef COMPILE_WSCANF
2761 memset (&state, '\0', sizeof (state));
2765 wchar_t *runp;
2766 size_t n;
2768 /* Test whether it's in the scanlist. */
2769 runp = tw;
2770 while (runp < twend)
2772 if (runp[0] == L'-' && runp[1] != '\0'
2773 && runp + 1 != twend
2774 && runp != tw
2775 && (unsigned int) runp[-1] <= (unsigned int) runp[1])
2777 /* Match against all characters in between the
2778 first and last character of the sequence. */
2779 wchar_t wc;
2781 for (wc = runp[-1] + 1; wc <= runp[1]; ++wc)
2782 if ((wint_t) wc == c)
2783 break;
2785 if (wc <= runp[1] && !not_in)
2786 break;
2787 if (wc <= runp[1] && not_in)
2789 /* The current character is not in the
2790 scanset. */
2791 ungetc (c, s);
2792 goto out2;
2795 runp += 2;
2797 else
2799 if ((wint_t) *runp == c && !not_in)
2800 break;
2801 if ((wint_t) *runp == c && not_in)
2803 ungetc (c, s);
2804 goto out2;
2807 ++runp;
2811 if (runp == twend && !not_in)
2813 ungetc (c, s);
2814 goto out2;
2817 if (!(flags & SUPPRESS))
2819 if ((flags & MALLOC)
2820 && str + MB_CUR_MAX >= *strptr + strsize)
2822 /* Enlarge the buffer. */
2823 size_t strleng = str - *strptr;
2824 char *newstr;
2826 newstr = (char *) realloc (*strptr, 2 * strsize);
2827 if (newstr == NULL)
2829 /* Can't allocate that much. Last-ditch
2830 effort. */
2831 newstr = (char *) realloc (*strptr,
2832 strleng + MB_CUR_MAX);
2833 if (newstr == NULL)
2835 if (flags & POSIX_MALLOC)
2837 done = EOF;
2838 goto errout;
2840 /* We lose. Oh well. Terminate the string
2841 and stop converting, so at least we don't
2842 skip any input. */
2843 ((char *) (*strptr))[strleng] = '\0';
2844 strptr = NULL;
2845 ++done;
2846 conv_error ();
2848 else
2850 *strptr = newstr;
2851 str = newstr + strleng;
2852 strsize = strleng + MB_CUR_MAX;
2855 else
2857 *strptr = newstr;
2858 str = newstr + strleng;
2859 strsize *= 2;
2864 n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c, &state);
2865 if (__glibc_unlikely (n == (size_t) -1))
2866 encode_error ();
2868 assert (n <= MB_CUR_MAX);
2869 str += n;
2871 while (--width > 0 && inchar () != WEOF);
2872 out2:
2873 #else
2876 if (((char *) charbuf.scratch.data)[c] == not_in)
2878 ungetc_not_eof (c, s);
2879 break;
2882 /* This is easy. */
2883 if (!(flags & SUPPRESS))
2885 *str++ = c;
2886 if ((flags & MALLOC)
2887 && (char *) str == *strptr + strsize)
2889 /* Enlarge the buffer. */
2890 size_t newsize = 2 * strsize;
2892 allocagain:
2893 str = (char *) realloc (*strptr, newsize);
2894 if (str == NULL)
2896 /* Can't allocate that much. Last-ditch
2897 effort. */
2898 if (newsize > strsize + 1)
2900 newsize = strsize + 1;
2901 goto allocagain;
2903 if (flags & POSIX_MALLOC)
2905 done = EOF;
2906 goto errout;
2908 /* We lose. Oh well. Terminate the
2909 string and stop converting,
2910 so at least we don't skip any input. */
2911 ((char *) (*strptr))[strsize - 1] = '\0';
2912 strptr = NULL;
2913 ++done;
2914 conv_error ();
2916 else
2918 *strptr = (char *) str;
2919 str += strsize;
2920 strsize = newsize;
2925 while (--width > 0 && inchar () != EOF);
2926 #endif
2928 if (__glibc_unlikely (now == read_in))
2929 /* We haven't succesfully read any character. */
2930 conv_error ();
2932 if (!(flags & SUPPRESS))
2934 #ifdef COMPILE_WSCANF
2935 /* We have to emit the code to get into the initial
2936 state. */
2937 char buf[MB_LEN_MAX];
2938 size_t n = __wcrtomb (buf, L'\0', &state);
2939 if (n > 0 && (flags & MALLOC)
2940 && str + n >= *strptr + strsize)
2942 /* Enlarge the buffer. */
2943 size_t strleng = str - *strptr;
2944 char *newstr;
2946 newstr = (char *) realloc (*strptr, strleng + n + 1);
2947 if (newstr == NULL)
2949 if (flags & POSIX_MALLOC)
2951 done = EOF;
2952 goto errout;
2954 /* We lose. Oh well. Terminate the string
2955 and stop converting, so at least we don't
2956 skip any input. */
2957 ((char *) (*strptr))[strleng] = '\0';
2958 strptr = NULL;
2959 ++done;
2960 conv_error ();
2962 else
2964 *strptr = newstr;
2965 str = newstr + strleng;
2966 strsize = strleng + n + 1;
2970 str = __mempcpy (str, buf, n);
2971 #endif
2972 *str++ = '\0';
2974 if ((flags & MALLOC) && str - *strptr != strsize)
2976 char *cp = (char *) realloc (*strptr, str - *strptr);
2977 if (cp != NULL)
2978 *strptr = cp;
2980 strptr = NULL;
2982 ++done;
2985 break;
2987 case L_('p'): /* Generic pointer. */
2988 base = 16;
2989 /* A PTR must be the same size as a `long int'. */
2990 flags &= ~(SHORT|LONGDBL);
2991 if (need_long)
2992 flags |= LONG;
2993 flags |= READ_POINTER;
2994 goto number;
2996 default:
2997 /* If this is an unknown format character punt. */
2998 conv_error ();
3002 /* The last thing we saw int the format string was a white space.
3003 Consume the last white spaces. */
3004 if (skip_space)
3007 c = inchar ();
3008 while (ISSPACE (c));
3009 ungetc (c, s);
3012 errout:
3013 /* Unlock stream. */
3014 UNLOCK_STREAM (s);
3016 scratch_buffer_free (&charbuf.scratch);
3017 if (errp != NULL)
3018 *errp |= errval;
3020 if (__glibc_unlikely (done == EOF))
3022 if (__glibc_unlikely (ptrs_to_free != NULL))
3024 struct ptrs_to_free *p = ptrs_to_free;
3025 while (p != NULL)
3027 for (size_t cnt = 0; cnt < p->count; ++cnt)
3029 free (*p->ptrs[cnt]);
3030 *p->ptrs[cnt] = NULL;
3032 p = p->next;
3033 ptrs_to_free = p;
3037 else if (__glibc_unlikely (strptr != NULL))
3039 free (*strptr);
3040 *strptr = NULL;
3042 return done;
3045 #ifdef COMPILE_WSCANF
3047 __vfwscanf (FILE *s, const wchar_t *format, va_list argptr)
3049 return _IO_vfwscanf (s, format, argptr, NULL);
3051 ldbl_weak_alias (__vfwscanf, vfwscanf)
3052 #else
3054 ___vfscanf (FILE *s, const char *format, va_list argptr)
3056 return _IO_vfscanf_internal (s, format, argptr, NULL);
3058 ldbl_strong_alias (_IO_vfscanf_internal, _IO_vfscanf)
3059 ldbl_hidden_def (_IO_vfscanf_internal, _IO_vfscanf)
3060 ldbl_strong_alias (___vfscanf, __vfscanf)
3061 ldbl_hidden_def (___vfscanf, __vfscanf)
3062 ldbl_weak_alias (___vfscanf, vfscanf)
3063 #endif