1 /* Copyright (C) 1991-2006, 2007 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
30 #include <bits/libc-lock.h>
31 #include <locale/localeinfo.h>
34 # define HAVE_LONGLONG
35 # define LONGLONG long long
37 # define LONGLONG long
40 /* Determine whether we have to handle `long long' at all. */
41 #if LONG_MAX == LONG_LONG_MAX
42 # define need_longlong 0
44 # define need_longlong 1
47 /* Determine whether we have to handle `long'. */
48 #if INT_MAX == LONG_MAX
54 /* Those are flags in the conversion format. */
55 #define LONG 0x0001 /* l: long or double */
56 #define LONGDBL 0x0002 /* L: long long or long double */
57 #define SHORT 0x0004 /* h: short */
58 #define SUPPRESS 0x0008 /* *: suppress assignment */
59 #define POINTER 0x0010 /* weird %p pointer (`fake hex') */
60 #define NOSKIP 0x0020 /* do not skip blanks */
61 #define NUMBER_SIGNED 0x0040 /* signed integer */
62 #define GROUP 0x0080 /* ': group numbers */
63 #define GNU_MALLOC 0x0100 /* a: malloc strings */
64 #define CHAR 0x0200 /* hh: char */
65 #define I18N 0x0400 /* I: use locale's digits */
66 #define HEXA_FLOAT 0x0800 /* hexadecimal float */
67 #define READ_POINTER 0x1000 /* this is a pointer value */
68 #define POSIX_MALLOC 0x2000 /* m: malloc strings */
69 #define MALLOC (GNU_MALLOC | POSIX_MALLOC)
71 #include <locale/localeinfo.h>
76 #define va_list _IO_va_list
79 # define ungetc(c, s) ((void) (c == WEOF \
81 INTUSE(_IO_sputbackwc) (s, c))))
82 # define ungetc_not_eof(c, s) ((void) (--read_in, \
83 INTUSE(_IO_sputbackwc) (s, c)))
84 # define inchar() (c == WEOF ? ((errno = inchar_errno), WEOF) \
85 : ((c = _IO_getwc_unlocked (s)), \
88 : (size_t) (inchar_errno = errno)), c))
90 # define MEMCPY(d, s, n) __wmemcpy (d, s, n)
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
111 # define ungetc(c, s) ((void) ((int) c == EOF \
113 INTUSE(_IO_sputbackc) (s, (unsigned char) c))))
114 # define ungetc_not_eof(c, s) ((void) (--read_in, \
115 INTUSE(_IO_sputbackc) (s, (unsigned char) c)))
116 # define inchar() (c == EOF ? ((errno = inchar_errno), EOF) \
117 : ((c = _IO_getc_unlocked (s)), \
120 : (size_t) (inchar_errno = errno)), c))
121 # define MEMCPY(d, s, n) memcpy (d, s, n)
122 # define ISSPACE(Ch) __isspace_l (Ch, loc)
123 # define ISDIGIT(Ch) __isdigit_l (Ch, loc)
124 # define ISXDIGIT(Ch) __isxdigit_l (Ch, loc)
125 # define TOLOWER(Ch) __tolower_l ((unsigned char) (Ch), loc)
126 # define ORIENT if (_IO_vtable_offset (s) == 0 \
127 && _IO_fwide (s, -1) != -1) \
132 # define UCHAR_T unsigned char
136 #define encode_error() do { \
138 __set_errno (EILSEQ); \
141 #define conv_error() do { \
145 #define input_error() do { \
147 if (done == 0) done = EOF; \
150 #define add_ptr_to_free(ptr) \
153 if (ptrs_to_free == NULL \
154 || ptrs_to_free->count == (sizeof (ptrs_to_free->ptrs) \
155 / sizeof (ptrs_to_free->ptrs[0]))) \
157 struct ptrs_to_free *new_ptrs = alloca (sizeof (*ptrs_to_free)); \
158 new_ptrs->count = 0; \
159 new_ptrs->next = ptrs_to_free; \
160 ptrs_to_free = new_ptrs; \
162 ptrs_to_free->ptrs[ptrs_to_free->count++] = (ptr); \
165 #define ARGCHECK(s, format) \
168 /* Check file argument for consistence. */ \
169 CHECK_FILE (s, EOF); \
170 if (s->_flags & _IO_NO_READS) \
172 __set_errno (EBADF); \
175 else if (format == NULL) \
181 #define LOCK_STREAM(S) \
182 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, (S)); \
184 #define UNLOCK_STREAM(S) \
185 _IO_funlockfile (S); \
186 __libc_cleanup_region_end (0)
191 struct ptrs_to_free
*next
;
195 /* Read formatted input from S according to the format string
196 FORMAT, using the argument list in ARG.
197 Return the number of assignments made, or -1 for an input error. */
198 #ifdef COMPILE_WSCANF
200 _IO_vfwscanf (_IO_FILE
*s
, const wchar_t *format
, _IO_va_list argptr
,
204 _IO_vfscanf_internal (_IO_FILE
*s
, const char *format
, _IO_va_list argptr
,
209 register const CHAR_T
*f
= format
;
210 register UCHAR_T fc
; /* Current character of the format. */
211 register WINT_T done
= 0; /* Assignments done. */
212 register size_t read_in
= 0; /* Chars read in. */
213 register WINT_T c
= 0; /* Last char read. */
214 register int width
; /* Maximum field width. */
215 register int flags
; /* Modifiers for current format element. */
217 #ifndef COMPILE_WSCANF
218 __locale_t loc
= _NL_CURRENT_LOCALE
;
219 struct locale_data
*const curctype
= loc
->__locales
[LC_CTYPE
];
222 /* Errno of last failed inchar call. */
223 int inchar_errno
= 0;
224 /* Status for reading F-P nums. */
225 char got_dot
, got_e
, negative
;
226 /* If a [...] is a [^...]. */
228 #define exp_char not_in
229 /* Base for integral numbers. */
231 /* Decimal point character. */
232 #ifdef COMPILE_WSCANF
237 /* The thousands character of the current locale. */
238 #ifdef COMPILE_WSCANF
241 const char *thousands
;
243 struct ptrs_to_free
*ptrs_to_free
= NULL
;
244 /* State for the conversions. */
246 /* Integral holding variables. */
250 unsigned long long int uq
;
252 unsigned long int ul
;
254 /* Character-buffer pointer. */
256 wchar_t *wstr
= NULL
;
257 char **strptr
= NULL
;
259 /* We must not react on white spaces immediately because they can
260 possibly be matched even if in the input stream no character is
261 available anymore. */
264 CHAR_T
*tw
; /* Temporary pointer. */
265 CHAR_T
*wp
= NULL
; /* Workspace. */
266 size_t wpmax
= 0; /* Maximal size of workspace. */
267 size_t wpsize
; /* Currently used bytes in workspace. */
271 if (wpsize == wpmax) \
274 wpmax = (UCHAR_MAX + 1 > 2 * wpmax ? UCHAR_MAX + 1 : 2 * wpmax); \
275 wp = (CHAR_T *) alloca (wpmax * sizeof (wchar_t)); \
277 MEMCPY (wp, old, wpsize); \
279 wp[wpsize++] = (Ch); \
284 __va_copy (arg
, argptr
);
286 arg
= (va_list) argptr
;
293 ARGCHECK (s
, format
);
296 #ifndef COMPILE_WSCANF
297 struct locale_data
*const curnumeric
= loc
->__locales
[LC_NUMERIC
];
300 /* Figure out the decimal point character. */
301 #ifdef COMPILE_WSCANF
302 decimal
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
304 decimal
= curnumeric
->values
[_NL_ITEM_INDEX (DECIMAL_POINT
)].string
;
306 /* Figure out the thousands separator character. */
307 #ifdef COMPILE_WSCANF
308 thousands
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_THOUSANDS_SEP_WC
);
310 thousands
= curnumeric
->values
[_NL_ITEM_INDEX (THOUSANDS_SEP
)].string
;
311 if (*thousands
== '\0')
316 /* Lock the stream. */
320 #ifndef COMPILE_WSCANF
321 /* From now on we use `state' to convert the format string. */
322 memset (&state
, '\0', sizeof (state
));
325 /* Run through the format string. */
329 /* Extract the next argument, which is of type TYPE.
330 For a %N$... spec, this is the Nth argument from the beginning;
331 otherwise it is the next argument after the state now in ARG. */
333 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
334 ({ unsigned int pos = argpos; \
336 __va_copy (arg, argptr); \
338 (void) va_arg (arg, void *); \
339 va_arg (arg, type); \
343 /* XXX Possible optimization. */
344 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
345 ({ va_list arg = (va_list) argptr; \
346 arg = (va_list) ((char *) arg \
348 * __va_rounded_size (void *)); \
349 va_arg (arg, type); \
352 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
353 ({ unsigned int pos = argpos; \
354 va_list arg = (va_list) argptr; \
356 (void) va_arg (arg, void *); \
357 va_arg (arg, type); \
362 #ifndef COMPILE_WSCANF
363 if (!isascii ((unsigned char) *f
))
365 /* Non-ASCII, may be a multibyte. */
366 int len
= __mbrlen (f
, strlen (f
), &state
);
372 if (__builtin_expect (c
== EOF
, 0))
374 else if (c
!= (unsigned char) *f
++)
376 ungetc_not_eof (c
, s
);
389 /* Remember to skip spaces. */
396 /* Read a character. */
399 /* Characters other than format specs must just match. */
400 if (__builtin_expect (c
== EOF
, 0))
403 /* We saw white space char as the last character in the format
404 string. Now it's time to skip all leading white space. */
408 if (__builtin_expect (inchar () == EOF
, 0))
413 if (__builtin_expect (c
!= fc
, 0))
422 /* This is the start of the conversion string. */
425 /* Initialize state of modifiers. */
428 /* Prepare temporary buffer. */
431 /* Check for a positional parameter specification. */
432 if (ISDIGIT ((UCHAR_T
) *f
))
434 argpos
= (UCHAR_T
) *f
++ - L_('0');
435 while (ISDIGIT ((UCHAR_T
) *f
))
436 argpos
= argpos
* 10 + ((UCHAR_T
) *f
++ - L_('0'));
441 /* Oops; that was actually the field width. */
448 /* Check for the assignment-suppressing, the number grouping flag,
449 and the signal to use the locale's digit representation. */
450 while (*f
== L_('*') || *f
== L_('\'') || *f
== L_('I'))
457 #ifdef COMPILE_WSCANF
458 if (thousands
!= L
'\0')
460 if (thousands
!= NULL
)
469 /* Find the maximum field width. */
471 while (ISDIGIT ((UCHAR_T
) *f
))
474 width
+= (UCHAR_T
) *f
++ - L_('0');
480 /* Check for type modifiers. */
484 /* ints are short ints or chars. */
496 /* A double `l' is equivalent to an `L'. */
498 flags
|= LONGDBL
| LONG
;
501 /* ints are long ints. */
506 /* doubles are long doubles, and ints are long long ints. */
507 flags
|= LONGDBL
| LONG
;
510 /* The `a' is used as a flag only if followed by `s', `S' or
512 if (*f
!= L_('s') && *f
!= L_('S') && *f
!= L_('['))
517 /* In __isoc99_*scanf %as, %aS and %a[ extension is not
519 if (s
->_flags2
& _IO_FLAGS2_SCANF_STD
)
524 /* String conversions (%s, %[) take a `char **'
525 arg and fill it in with a malloc'd pointer. */
529 flags
|= POSIX_MALLOC
;
537 if (need_longlong
&& sizeof (size_t) > sizeof (unsigned long int))
539 else if (sizeof (size_t) > sizeof (unsigned int))
543 if (need_longlong
&& sizeof (uintmax_t) > sizeof (unsigned long int))
545 else if (sizeof (uintmax_t) > sizeof (unsigned int))
549 if (need_longlong
&& sizeof (ptrdiff_t) > sizeof (long int))
551 else if (sizeof (ptrdiff_t) > sizeof (int))
555 /* Not a recognized modifier. Backup. */
560 /* End of the format string? */
561 if (__builtin_expect (*f
== L_('\0'), 0))
564 /* Find the conversion specifier. */
566 if (skip_space
|| (fc
!= L_('[') && fc
!= L_('c')
567 && fc
!= L_('C') && fc
!= L_('n')))
569 /* Eat whitespace. */
570 int save_errno
= errno
;
573 /* We add the additional test for EOF here since otherwise
574 inchar will restore the old errno value which might be
575 EINTR but does not indicate an interrupt since nothing
576 was read at this time. */
577 if (__builtin_expect ((c
== EOF
|| inchar () == EOF
)
578 && errno
== EINTR
, 0))
581 __set_errno (save_errno
);
588 case L_('%'): /* Must match a literal '%'. */
590 if (__builtin_expect (c
== EOF
, 0))
592 if (__builtin_expect (c
!= fc
, 0))
594 ungetc_not_eof (c
, s
);
599 case L_('n'): /* Answer number of assignments done. */
600 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
601 with the 'n' conversion specifier. */
602 if (!(flags
& SUPPRESS
))
604 /* Don't count the read-ahead. */
605 if (need_longlong
&& (flags
& LONGDBL
))
606 *ARG (long long int *) = read_in
;
607 else if (need_long
&& (flags
& LONG
))
608 *ARG (long int *) = read_in
;
609 else if (flags
& SHORT
)
610 *ARG (short int *) = read_in
;
611 else if (!(flags
& CHAR
))
612 *ARG (int *) = read_in
;
614 *ARG (char *) = read_in
;
616 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
617 /* We have a severe problem here. The ISO C standard
618 contradicts itself in explaining the effect of the %n
619 format in `scanf'. While in ISO C:1990 and the ISO C
620 Amendement 1:1995 the result is described as
622 Execution of a %n directive does not effect the
623 assignment count returned at the completion of
624 execution of the f(w)scanf function.
626 in ISO C Corrigendum 1:1994 the following was added:
629 Add the following fourth example:
632 int d1, d2, n1, n2, i;
633 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
634 the value 123 is assigned to d1 and the value3 to n1.
635 Because %n can never get an input failure the value
636 of 3 is also assigned to n2. The value of d2 is not
637 affected. The value 3 is assigned to i.
639 We go for now with the historically correct code from ISO C,
640 i.e., we don't count the %n assignments. When it ever
641 should proof to be wrong just remove the #ifdef above. */
647 case L_('c'): /* Match characters. */
648 if ((flags
& LONG
) == 0)
653 #define STRING_ARG(Str, Type, Width) \
654 do if (!(flags & SUPPRESS)) \
656 if (flags & MALLOC) \
658 /* The string is to be stored in a malloc'd buffer. */ \
659 /* For %mS using char ** is actually wrong, but \
660 shouldn't make a difference on any arch glibc \
661 supports and would unnecessarily complicate \
663 strptr = ARG (char **); \
664 if (strptr == NULL) \
666 /* Allocate an initial buffer. */ \
668 *strptr = (char *) malloc (strsize * sizeof (Type)); \
669 Str = (Type *) *strptr; \
671 add_ptr_to_free (strptr); \
672 else if (flags & POSIX_MALLOC) \
676 Str = ARG (Type *); \
680 #ifdef COMPILE_WSCANF
681 STRING_ARG (str
, char, 100);
683 STRING_ARG (str
, char, (width
> 1024 ? 1024 : width
));
687 if (__builtin_expect (c
== EOF
, 0))
690 #ifdef COMPILE_WSCANF
691 /* We have to convert the wide character(s) into multibyte
692 characters and store the result. */
693 memset (&state
, '\0', sizeof (state
));
699 if (!(flags
& SUPPRESS
) && (flags
& POSIX_MALLOC
)
700 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
702 /* We have to enlarge the buffer if the `m' flag
704 size_t strleng
= str
- *strptr
;
707 newstr
= (char *) realloc (*strptr
, strsize
* 2);
710 /* Can't allocate that much. Last-ditch effort. */
711 newstr
= (char *) realloc (*strptr
,
712 strleng
+ MB_CUR_MAX
);
714 /* c can't have `a' flag, only `m'. */
719 str
= newstr
+ strleng
;
720 strsize
= strleng
+ MB_CUR_MAX
;
726 str
= newstr
+ strleng
;
731 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
732 if (__builtin_expect (n
== (size_t) -1, 0))
733 /* No valid wide character. */
736 /* Increment the output pointer. Even if we don't
740 while (--width
> 0 && inchar () != EOF
);
742 if (!(flags
& SUPPRESS
))
747 && (char *) str
== *strptr
+ strsize
)
749 /* Enlarge the buffer. */
752 + (strsize
>= width
? width
- 1 : strsize
);
754 str
= (char *) realloc (*strptr
, newsize
);
757 /* Can't allocate that much. Last-ditch
759 str
= (char *) realloc (*strptr
, strsize
+ 1);
761 /* c can't have `a' flag, only `m'. */
765 *strptr
= (char *) str
;
772 *strptr
= (char *) str
;
779 while (--width
> 0 && inchar () != EOF
);
782 while (--width
> 0 && inchar () != EOF
);
785 if (!(flags
& SUPPRESS
))
787 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
789 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
804 STRING_ARG (wstr
, wchar_t, (width
> 1024 ? 1024 : width
));
807 if (__builtin_expect (c
== EOF
, 0))
810 #ifdef COMPILE_WSCANF
811 /* Just store the incoming wide characters. */
812 if (!(flags
& SUPPRESS
))
817 && wstr
== (wchar_t *) *strptr
+ strsize
)
820 = strsize
+ (strsize
> width
? width
- 1 : strsize
);
821 /* Enlarge the buffer. */
822 wstr
= (wchar_t *) realloc (*strptr
,
823 newsize
* sizeof (wchar_t));
826 /* Can't allocate that much. Last-ditch effort. */
827 wstr
= (wchar_t *) realloc (*strptr
,
831 /* C or lc can't have `a' flag, only `m' flag. */
835 *strptr
= (char *) wstr
;
842 *strptr
= (char *) wstr
;
849 while (--width
> 0 && inchar () != EOF
);
852 while (--width
> 0 && inchar () != EOF
);
855 /* We have to convert the multibyte input sequence to wide
860 memset (&cstate
, '\0', sizeof (cstate
));
864 /* This is what we present the mbrtowc function first. */
867 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
868 && wstr
== (wchar_t *) *strptr
+ strsize
)
871 = strsize
+ (strsize
> width
? width
- 1 : strsize
);
872 /* Enlarge the buffer. */
873 wstr
= (wchar_t *) realloc (*strptr
,
874 newsize
* sizeof (wchar_t));
877 /* Can't allocate that much. Last-ditch effort. */
878 wstr
= (wchar_t *) realloc (*strptr
,
880 * sizeof (wchar_t)));
882 /* C or lc can't have `a' flag, only `m' flag. */
886 *strptr
= (char *) wstr
;
893 *strptr
= (char *) wstr
;
903 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
906 if (n
== (size_t) -2)
908 /* Possibly correct character, just not enough
910 if (__builtin_expect (inchar () == EOF
, 0))
917 if (__builtin_expect (n
!= 1, 0))
920 /* We have a match. */
924 /* Advance the result pointer. */
927 while (--width
> 0 && inchar () != EOF
);
931 if (!(flags
& SUPPRESS
))
933 if ((flags
& MALLOC
) && wstr
- (wchar_t *) *strptr
!= strsize
)
935 wchar_t *cp
= (wchar_t *) realloc (*strptr
,
937 - (wchar_t *) *strptr
)
938 * sizeof (wchar_t)));
940 *strptr
= (char *) cp
;
949 case L_('s'): /* Read a string. */
952 STRING_ARG (str
, char, 100);
955 if (__builtin_expect (c
== EOF
, 0))
958 #ifdef COMPILE_WSCANF
959 memset (&state
, '\0', sizeof (state
));
966 ungetc_not_eof (c
, s
);
970 #ifdef COMPILE_WSCANF
971 /* This is quite complicated. We have to convert the
972 wide characters into multibyte characters and then
977 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
978 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
980 /* We have to enlarge the buffer if the `a' or `m'
982 size_t strleng
= str
- *strptr
;
985 newstr
= (char *) realloc (*strptr
, strsize
* 2);
988 /* Can't allocate that much. Last-ditch
990 newstr
= (char *) realloc (*strptr
,
991 strleng
+ MB_CUR_MAX
);
994 if (flags
& POSIX_MALLOC
)
996 /* We lose. Oh well. Terminate the
997 string and stop converting,
998 so at least we don't skip any input. */
999 ((char *) (*strptr
))[strleng
] = '\0';
1007 str
= newstr
+ strleng
;
1008 strsize
= strleng
+ MB_CUR_MAX
;
1014 str
= newstr
+ strleng
;
1019 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
,
1021 if (__builtin_expect (n
== (size_t) -1, 0))
1024 assert (n
<= MB_CUR_MAX
);
1029 if (!(flags
& SUPPRESS
))
1032 if ((flags
& MALLOC
)
1033 && (char *) str
== *strptr
+ strsize
)
1035 /* Enlarge the buffer. */
1036 str
= (char *) realloc (*strptr
, 2 * strsize
);
1039 /* Can't allocate that much. Last-ditch
1041 str
= (char *) realloc (*strptr
, strsize
+ 1);
1044 if (flags
& POSIX_MALLOC
)
1046 /* We lose. Oh well. Terminate the
1047 string and stop converting,
1048 so at least we don't skip any input. */
1049 ((char *) (*strptr
))[strsize
- 1] = '\0';
1056 *strptr
= (char *) str
;
1063 *strptr
= (char *) str
;
1071 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
1073 if (!(flags
& SUPPRESS
))
1075 #ifdef COMPILE_WSCANF
1076 /* We have to emit the code to get into the initial
1078 char buf
[MB_LEN_MAX
];
1079 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
1080 if (n
> 0 && (flags
& MALLOC
)
1081 && str
+ n
>= *strptr
+ strsize
)
1083 /* Enlarge the buffer. */
1084 size_t strleng
= str
- *strptr
;
1087 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
1090 if (flags
& POSIX_MALLOC
)
1092 /* We lose. Oh well. Terminate the string
1093 and stop converting, so at least we don't
1095 ((char *) (*strptr
))[strleng
] = '\0';
1103 str
= newstr
+ strleng
;
1104 strsize
= strleng
+ n
+ 1;
1108 str
= __mempcpy (str
, buf
, n
);
1112 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
1114 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
1128 #ifndef COMPILE_WSCANF
1132 /* Wide character string. */
1133 STRING_ARG (wstr
, wchar_t, 100);
1136 if (__builtin_expect (c
== EOF
, 0))
1139 #ifndef COMPILE_WSCANF
1140 memset (&cstate
, '\0', sizeof (cstate
));
1147 ungetc_not_eof (c
, s
);
1151 #ifdef COMPILE_WSCANF
1153 if (!(flags
& SUPPRESS
))
1156 if ((flags
& MALLOC
)
1157 && wstr
== (wchar_t *) *strptr
+ strsize
)
1159 /* Enlarge the buffer. */
1160 wstr
= (wchar_t *) realloc (*strptr
,
1162 * sizeof (wchar_t));
1165 /* Can't allocate that much. Last-ditch
1167 wstr
= (wchar_t *) realloc (*strptr
,
1169 * sizeof (wchar_t));
1172 if (flags
& POSIX_MALLOC
)
1174 /* We lose. Oh well. Terminate the string
1175 and stop converting, so at least we don't
1177 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1184 *strptr
= (char *) wstr
;
1191 *strptr
= (char *) wstr
;
1207 n
= __mbrtowc (!(flags
& SUPPRESS
) ? wstr
: NULL
,
1210 if (n
== (size_t) -2)
1212 /* Possibly correct character, just not enough
1214 if (__builtin_expect (inchar () == EOF
, 0))
1221 if (__builtin_expect (n
!= 1, 0))
1224 /* We have a match. */
1229 if (!(flags
& SUPPRESS
) && (flags
& MALLOC
)
1230 && wstr
== (wchar_t *) *strptr
+ strsize
)
1232 /* Enlarge the buffer. */
1233 wstr
= (wchar_t *) realloc (*strptr
,
1235 * sizeof (wchar_t)));
1238 /* Can't allocate that much. Last-ditch effort. */
1239 wstr
= (wchar_t *) realloc (*strptr
,
1241 * sizeof (wchar_t)));
1244 if (flags
& POSIX_MALLOC
)
1246 /* We lose. Oh well. Terminate the
1247 string and stop converting, so at
1248 least we don't skip any input. */
1249 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
1256 *strptr
= (char *) wstr
;
1263 *strptr
= (char *) wstr
;
1271 while ((width
<= 0 || --width
> 0) && inchar () != EOF
);
1273 if (!(flags
& SUPPRESS
))
1277 if ((flags
& MALLOC
) && wstr
- (wchar_t *) *strptr
!= strsize
)
1279 wchar_t *cp
= (wchar_t *) realloc (*strptr
,
1281 - (wchar_t *) *strptr
)
1282 * sizeof(wchar_t)));
1284 *strptr
= (char *) cp
;
1293 case L_('x'): /* Hexadecimal integer. */
1294 case L_('X'): /* Ditto. */
1298 case L_('o'): /* Octal integer. */
1302 case L_('u'): /* Unsigned decimal integer. */
1306 case L_('d'): /* Signed decimal integer. */
1308 flags
|= NUMBER_SIGNED
;
1311 case L_('i'): /* Generic number. */
1313 flags
|= NUMBER_SIGNED
;
1317 if (__builtin_expect (c
== EOF
, 0))
1320 /* Check for a sign. */
1321 if (c
== L_('-') || c
== L_('+'))
1329 /* Look for a leading indication of base. */
1330 if (width
!= 0 && c
== L_('0'))
1338 if (width
!= 0 && TOLOWER (c
) == L_('x'))
1356 if (base
== 10 && __builtin_expect ((flags
& I18N
) != 0, 0))
1361 #ifdef COMPILE_WSCANF
1362 const wchar_t *wcdigits
[10];
1363 const wchar_t *wcdigits_extended
[10];
1365 const char *mbdigits
[10];
1366 const char *mbdigits_extended
[10];
1368 /* "to_inpunct" is a map from ASCII digits to their
1369 equivalent in locale. This is defined for locales
1370 which use an extra digits set. */
1371 wctrans_t map
= __wctrans ("to_inpunct");
1375 #ifdef COMPILE_WSCANF
1376 to_level
= _NL_CURRENT_WORD (LC_CTYPE
,
1377 _NL_CTYPE_INDIGITS_WC_LEN
) - 1;
1379 to_level
= (uint32_t) curctype
->values
[_NL_ITEM_INDEX (_NL_CTYPE_INDIGITS_MB_LEN
)].word
- 1;
1382 /* Get the alternative digit forms if there are any. */
1383 if (__builtin_expect (map
!= NULL
, 0))
1385 /* Adding new level for extra digits set in locale file. */
1388 for (n
= 0; n
< 10; ++n
)
1390 #ifdef COMPILE_WSCANF
1391 wcdigits
[n
] = (const wchar_t *)
1392 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1394 wchar_t *wc_extended
= (wchar_t *)
1395 alloca ((to_level
+ 2) * sizeof (wchar_t));
1396 __wmemcpy (wc_extended
, wcdigits
[n
], to_level
);
1397 wc_extended
[to_level
] = __towctrans (L
'0' + n
, map
);
1398 wc_extended
[to_level
+ 1] = '\0';
1399 wcdigits_extended
[n
] = wc_extended
;
1402 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1404 /* Get the equivalent wide char in map. */
1405 wint_t extra_wcdigit
= __towctrans (L
'0' + n
, map
);
1407 /* Convert it to multibyte representation. */
1409 memset (&state
, '\0', sizeof (state
));
1411 char extra_mbdigit
[MB_LEN_MAX
];
1413 = __wcrtomb (extra_mbdigit
, extra_wcdigit
, &state
);
1415 if (mblen
== (size_t) -1)
1417 /* Ignore this new level. */
1422 /* Calculate the length of mbdigits[n]. */
1423 const char *last_char
= mbdigits
[n
];
1424 for (level
= 0; level
< to_level
; ++level
)
1425 last_char
= strchr (last_char
, '\0') + 1;
1427 size_t mbdigits_len
= last_char
- mbdigits
[n
];
1429 /* Allocate memory for extended multibyte digit. */
1431 mb_extended
= (char *) alloca (mbdigits_len
+ mblen
+ 1);
1433 /* And get the mbdigits + extra_digit string. */
1434 *(char *) __mempcpy (__mempcpy (mb_extended
, mbdigits
[n
],
1436 extra_mbdigit
, mblen
) = '\0';
1437 mbdigits_extended
[n
] = mb_extended
;
1442 /* Read the number into workspace. */
1443 while (c
!= EOF
&& width
!= 0)
1445 /* In this round we get the pointer to the digit strings
1446 and also perform the first round of comparisons. */
1447 for (n
= 0; n
< 10; ++n
)
1449 /* Get the string for the digits with value N. */
1450 #ifdef COMPILE_WSCANF
1451 if (__builtin_expect (map
!= NULL
, 0))
1452 wcdigits
[n
] = wcdigits_extended
[n
];
1454 wcdigits
[n
] = (const wchar_t *)
1455 _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_INDIGITS0_WC
+ n
);
1456 wcdigits
[n
] += from_level
;
1458 if (c
== (wint_t) *wcdigits
[n
])
1460 to_level
= from_level
;
1464 /* Advance the pointer to the next string. */
1468 int avail
= width
> 0 ? width
: INT_MAX
;
1470 if (__builtin_expect (map
!= NULL
, 0))
1471 mbdigits
[n
] = mbdigits_extended
[n
];
1474 = curctype
->values
[_NL_CTYPE_INDIGITS0_MB
+ n
].string
;
1476 for (level
= 0; level
< from_level
; level
++)
1477 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1480 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1482 if (*++cmpp
== '\0')
1486 if (avail
== 0 || inchar () == EOF
)
1496 to_level
= from_level
;
1500 /* We are pushing all read characters back. */
1501 if (cmpp
> mbdigits
[n
])
1504 while (--cmpp
> mbdigits
[n
])
1505 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1506 c
= (unsigned char) *cmpp
;
1509 /* Advance the pointer to the next string. */
1510 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1516 /* Have not yet found the digit. */
1517 for (level
= from_level
+ 1; level
<= to_level
; ++level
)
1519 /* Search all ten digits of this level. */
1520 for (n
= 0; n
< 10; ++n
)
1522 #ifdef COMPILE_WSCANF
1523 if (c
== (wint_t) *wcdigits
[n
])
1526 /* Advance the pointer to the next string. */
1530 int avail
= width
> 0 ? width
: INT_MAX
;
1533 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1535 if (*++cmpp
== '\0')
1539 if (avail
== 0 || inchar () == EOF
)
1552 /* We are pushing all read characters back. */
1553 if (cmpp
> mbdigits
[n
])
1556 while (--cmpp
> mbdigits
[n
])
1557 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1558 c
= (unsigned char) *cmpp
;
1561 /* Advance the pointer to the next string. */
1562 mbdigits
[n
] = strchr (mbdigits
[n
], '\0') + 1;
1578 else if (flags
& GROUP
)
1580 /* Try matching against the thousands separator. */
1581 #ifdef COMPILE_WSCANF
1585 const char *cmpp
= thousands
;
1586 int avail
= width
> 0 ? width
: INT_MAX
;
1588 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1591 if (*++cmpp
== '\0')
1595 if (avail
== 0 || inchar () == EOF
)
1603 /* We are pushing all read characters back. */
1604 if (cmpp
> thousands
)
1606 wpsize
-= cmpp
- thousands
;
1608 while (--cmpp
> thousands
)
1609 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1610 c
= (unsigned char) *cmpp
;
1618 /* The last thousands character will be added back by
1634 /* Read the number into workspace. */
1635 while (c
!= EOF
&& width
!= 0)
1642 else if (!ISDIGIT (c
) || (int) (c
- L_('0')) >= base
)
1644 if (base
== 10 && (flags
& GROUP
))
1646 /* Try matching against the thousands separator. */
1647 #ifdef COMPILE_WSCANF
1651 const char *cmpp
= thousands
;
1652 int avail
= width
> 0 ? width
: INT_MAX
;
1654 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1657 if (*++cmpp
== '\0')
1661 if (avail
== 0 || inchar () == EOF
)
1669 /* We are pushing all read characters back. */
1670 if (cmpp
> thousands
)
1672 wpsize
-= cmpp
- thousands
;
1674 while (--cmpp
> thousands
)
1675 ungetc_not_eof ((unsigned char) *cmpp
, s
);
1676 c
= (unsigned char) *cmpp
;
1684 /* The last thousands character will be added back by
1700 || (wpsize
== 1 && (wp
[0] == L_('+') || wp
[0] == L_('-'))))
1702 /* There was no number. If we are supposed to read a pointer
1703 we must recognize "(nil)" as well. */
1704 if (__builtin_expect (wpsize
== 0
1705 && (flags
& READ_POINTER
)
1706 && (width
< 0 || width
>= 0)
1708 && TOLOWER (inchar ()) == L_('n')
1709 && TOLOWER (inchar ()) == L_('i')
1710 && TOLOWER (inchar ()) == L_('l')
1711 && inchar () == L_(')'), 1))
1712 /* We must produce the value of a NULL pointer. A single
1713 '0' digit is enough. */
1717 /* The last read character is not part of the number
1725 /* The just read character is not part of the number anymore. */
1728 /* Convert the number. */
1730 if (need_longlong
&& (flags
& LONGDBL
))
1732 if (flags
& NUMBER_SIGNED
)
1733 num
.q
= __strtoll_internal (wp
, &tw
, base
, flags
& GROUP
);
1735 num
.uq
= __strtoull_internal (wp
, &tw
, base
, flags
& GROUP
);
1739 if (flags
& NUMBER_SIGNED
)
1740 num
.l
= __strtol_internal (wp
, &tw
, base
, flags
& GROUP
);
1742 num
.ul
= __strtoul_internal (wp
, &tw
, base
, flags
& GROUP
);
1744 if (__builtin_expect (wp
== tw
, 0))
1747 if (!(flags
& SUPPRESS
))
1749 if (flags
& NUMBER_SIGNED
)
1751 if (need_longlong
&& (flags
& LONGDBL
))
1752 *ARG (LONGLONG
int *) = num
.q
;
1753 else if (need_long
&& (flags
& LONG
))
1754 *ARG (long int *) = num
.l
;
1755 else if (flags
& SHORT
)
1756 *ARG (short int *) = (short int) num
.l
;
1757 else if (!(flags
& CHAR
))
1758 *ARG (int *) = (int) num
.l
;
1760 *ARG (signed char *) = (signed char) num
.ul
;
1764 if (need_longlong
&& (flags
& LONGDBL
))
1765 *ARG (unsigned LONGLONG
int *) = num
.uq
;
1766 else if (need_long
&& (flags
& LONG
))
1767 *ARG (unsigned long int *) = num
.ul
;
1768 else if (flags
& SHORT
)
1769 *ARG (unsigned short int *)
1770 = (unsigned short int) num
.ul
;
1771 else if (!(flags
& CHAR
))
1772 *ARG (unsigned int *) = (unsigned int) num
.ul
;
1774 *ARG (unsigned char *) = (unsigned char) num
.ul
;
1780 case L_('e'): /* Floating-point numbers. */
1791 if (__builtin_expect (c
== EOF
, 0))
1794 got_dot
= got_e
= 0;
1796 /* Check for a sign. */
1797 if (c
== L_('-') || c
== L_('+'))
1799 negative
= c
== L_('-');
1800 if (__builtin_expect (width
== 0 || inchar () == EOF
, 0))
1801 /* EOF is only an input error before we read any chars. */
1809 /* Take care for the special arguments "nan" and "inf". */
1810 if (TOLOWER (c
) == L_('n'))
1814 if (__builtin_expect (width
== 0
1816 || TOLOWER (c
) != L_('a'), 0))
1821 if (__builtin_expect (width
== 0
1823 || TOLOWER (c
) != L_('n'), 0))
1831 else if (TOLOWER (c
) == L_('i'))
1833 /* Maybe "inf" or "infinity". */
1835 if (__builtin_expect (width
== 0
1837 || TOLOWER (c
) != L_('n'), 0))
1842 if (__builtin_expect (width
== 0
1844 || TOLOWER (c
) != L_('f'), 0))
1849 /* It is as least "inf". */
1850 if (width
!= 0 && inchar () != EOF
)
1852 if (TOLOWER (c
) == L_('i'))
1856 /* Now we have to read the rest as well. */
1858 if (__builtin_expect (width
== 0
1860 || TOLOWER (c
) != L_('n'), 0))
1865 if (__builtin_expect (width
== 0
1867 || TOLOWER (c
) != L_('i'), 0))
1872 if (__builtin_expect (width
== 0
1874 || TOLOWER (c
) != L_('t'), 0))
1879 if (__builtin_expect (width
== 0
1881 || TOLOWER (c
) != L_('y'), 0))
1895 if (width
!= 0 && c
== L_('0'))
1901 if (width
!= 0 && TOLOWER (c
) == L_('x'))
1903 /* It is a number in hexadecimal format. */
1906 flags
|= HEXA_FLOAT
;
1909 /* Grouping is not allowed. */
1921 else if (!got_e
&& (flags
& HEXA_FLOAT
) && ISXDIGIT (c
))
1923 else if (got_e
&& wp
[wpsize
- 1] == exp_char
1924 && (c
== L_('-') || c
== L_('+')))
1926 else if (wpsize
> 0 && !got_e
1927 && (CHAR_T
) TOLOWER (c
) == exp_char
)
1930 got_e
= got_dot
= 1;
1934 #ifdef COMPILE_WSCANF
1935 if (! got_dot
&& c
== decimal
)
1940 else if ((flags
& GROUP
) != 0 && ! got_dot
&& c
== thousands
)
1944 /* The last read character is not part of the number
1950 const char *cmpp
= decimal
;
1951 int avail
= width
> 0 ? width
: INT_MAX
;
1955 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
1956 if (*++cmpp
== '\0')
1960 if (avail
== 0 || inchar () == EOF
)
1968 /* Add all the characters. */
1969 for (cmpp
= decimal
; *cmpp
!= '\0'; ++cmpp
)
1970 ADDW ((unsigned char) *cmpp
);
1977 /* Figure out whether it is a thousands separator.
1978 There is one problem: we possibly read more than
1979 one character. We cannot push them back but since
1980 we know that parts of the `decimal' string matched,
1981 we can compare against it. */
1982 const char *cmp2p
= thousands
;
1984 if ((flags
& GROUP
) != 0 && ! got_dot
)
1986 while (cmp2p
- thousands
< cmpp
- decimal
1987 && *cmp2p
== decimal
[cmp2p
- thousands
])
1989 if (cmp2p
- thousands
== cmpp
- decimal
)
1991 while ((unsigned char) *cmp2p
== c
&& avail
>= 0)
1992 if (*++cmp2p
== '\0')
1996 if (avail
== 0 || inchar () == EOF
)
2003 if (cmp2p
!= NULL
&& *cmp2p
== '\0')
2005 /* Add all the characters. */
2006 for (cmpp
= thousands
; *cmpp
!= '\0'; ++cmpp
)
2007 ADDW ((unsigned char) *cmpp
);
2013 /* The last read character is not part of the number
2022 if (width
== 0 || inchar () == EOF
)
2030 if (__builtin_expect ((flags
& I18N
) != 0, 0)
2031 /* Hexadecimal floats make no sense, fixing localized
2032 digits with ASCII letters. */
2033 && !(flags
& HEXA_FLOAT
)
2034 /* Minimum requirement. */
2035 && (wpsize
== 0 || got_dot
)
2036 && (map
= __wctrans ("to_inpunct")) != NULL
)
2038 /* Reget the first character. */
2041 /* Localized digits, decimal points, and thousands
2043 wint_t wcdigits
[12];
2045 /* First get decimal equivalent to check if we read it
2047 wcdigits
[11] = __towctrans (L
'.', map
);
2049 /* If we have not read any character or have just read
2050 locale decimal point which matches the decimal point
2051 for localized FP numbers, then we may have localized
2052 digits. Note, we test GOT_DOT above. */
2053 #ifdef COMPILE_WSCANF
2054 if (wpsize
== 0 || (wpsize
== 1 && wcdigits
[11] == decimal
))
2056 char mbdigits
[12][MB_LEN_MAX
+ 1];
2059 memset (&state
, '\0', sizeof (state
));
2061 bool match_so_far
= wpsize
== 0;
2062 size_t mblen
= __wcrtomb (mbdigits
[11], wcdigits
[11], &state
);
2063 if (mblen
!= (size_t) -1)
2065 mbdigits
[11][mblen
] = '\0';
2066 match_so_far
|= (wpsize
== strlen (decimal
)
2067 && strcmp (decimal
, mbdigits
[11]) == 0);
2071 size_t decimal_len
= strlen (decimal
);
2072 /* This should always be the case but the data comes
2074 if (decimal_len
<= MB_LEN_MAX
)
2076 match_so_far
|= wpsize
== decimal_len
;
2077 memcpy (mbdigits
[11], decimal
, decimal_len
+ 1);
2080 match_so_far
= false;
2086 bool have_locthousands
= (flags
& GROUP
) != 0;
2088 /* Now get the digits and the thousands-sep equivalents. */
2089 for (int n
= 0; n
< 11; ++n
)
2092 wcdigits
[n
] = __towctrans (L
'0' + n
, map
);
2095 wcdigits
[10] = __towctrans (L
',', map
);
2096 have_locthousands
&= wcdigits
[10] != L
'\0';
2099 #ifndef COMPILE_WSCANF
2100 memset (&state
, '\0', sizeof (state
));
2102 size_t mblen
= __wcrtomb (mbdigits
[n
], wcdigits
[n
],
2104 if (mblen
== (size_t) -1)
2108 if (have_locthousands
)
2110 size_t thousands_len
= strlen (thousands
);
2111 if (thousands_len
<= MB_LEN_MAX
)
2112 memcpy (mbdigits
[10], thousands
,
2115 have_locthousands
= false;
2119 /* Ignore checking against localized digits. */
2123 mbdigits
[n
][mblen
] = '\0';
2127 /* Start checking against localized digits, if
2128 convertion is done correctly. */
2131 if (got_e
&& wp
[wpsize
- 1] == exp_char
2132 && (c
== L_('-') || c
== L_('+')))
2134 else if (wpsize
> 0 && !got_e
2135 && (CHAR_T
) TOLOWER (c
) == exp_char
)
2138 got_e
= got_dot
= 1;
2142 /* Check against localized digits, decimal point,
2143 and thousands separator. */
2145 for (n
= 0; n
< 12; ++n
)
2147 #ifdef COMPILE_WSCANF
2148 if (c
== wcdigits
[n
])
2152 else if (n
== 11 && !got_dot
)
2157 else if (n
== 10 && have_locthousands
2161 /* The last read character is not part
2162 of the number anymore. */
2168 const char *cmpp
= mbdigits
[n
];
2169 int avail
= width
> 0 ? width
: INT_MAX
;
2171 while ((unsigned char) *cmpp
== c
&& avail
>= 0)
2172 if (*++cmpp
== '\0')
2176 if (avail
== 0 || inchar () == EOF
)
2187 else if (n
== 11 && !got_dot
)
2189 /* Add all the characters. */
2190 for (cmpp
= decimal
; *cmpp
!= '\0';
2192 ADDW ((unsigned char) *cmpp
);
2196 else if (n
== 10 && (flags
& GROUP
) != 0
2199 /* Add all the characters. */
2200 for (cmpp
= thousands
; *cmpp
!= '\0';
2202 ADDW ((unsigned char) *cmpp
);
2205 /* The last read character is not part
2206 of the number anymore. */
2212 /* We are pushing all read characters back. */
2213 if (cmpp
> mbdigits
[n
])
2216 while (--cmpp
> mbdigits
[n
])
2217 ungetc_not_eof ((unsigned char) *cmpp
, s
);
2218 c
= (unsigned char) *cmpp
;
2225 /* The last read character is not part
2226 of the number anymore. */
2232 if (width
== 0 || inchar () == EOF
)
2240 #ifndef COMPILE_WSCANF
2246 /* Have we read any character? If we try to read a number
2247 in hexadecimal notation and we have read only the `0x'
2248 prefix this is an error. */
2249 if (__builtin_expect (wpsize
== 0
2250 || ((flags
& HEXA_FLOAT
) && wpsize
== 2), 0))
2254 /* Convert the number. */
2256 if ((flags
& LONGDBL
) && !__ldbl_is_dbl
)
2258 long double d
= __strtold_internal (wp
, &tw
, flags
& GROUP
);
2259 if (!(flags
& SUPPRESS
) && tw
!= wp
)
2260 *ARG (long double *) = negative
? -d
: d
;
2262 else if (flags
& (LONG
| LONGDBL
))
2264 double d
= __strtod_internal (wp
, &tw
, flags
& GROUP
);
2265 if (!(flags
& SUPPRESS
) && tw
!= wp
)
2266 *ARG (double *) = negative
? -d
: d
;
2270 float d
= __strtof_internal (wp
, &tw
, flags
& GROUP
);
2271 if (!(flags
& SUPPRESS
) && tw
!= wp
)
2272 *ARG (float *) = negative
? -d
: d
;
2275 if (__builtin_expect (tw
== wp
, 0))
2278 if (!(flags
& SUPPRESS
))
2282 case L_('['): /* Character class. */
2284 STRING_ARG (wstr
, wchar_t, 100);
2286 STRING_ARG (str
, char, 100);
2297 /* There is no width given so there is also no limit on the
2298 number of characters we read. Therefore we set width to
2299 a very high value to make the algorithm easier. */
2302 #ifdef COMPILE_WSCANF
2303 /* Find the beginning and the end of the scanlist. We are not
2304 creating a lookup table since it would have to be too large.
2305 Instead we search each time through the string. This is not
2306 a constant lookup time but who uses this feature deserves to
2308 tw
= (wchar_t *) f
; /* Marks the beginning. */
2313 while ((fc
= *f
++) != L
'\0' && fc
!= L
']');
2315 if (__builtin_expect (fc
== L
'\0', 0))
2317 wp
= (wchar_t *) f
- 1;
2319 /* Fill WP with byte flags indexed by character.
2320 We will use this flag map for matching input characters. */
2321 if (wpmax
< UCHAR_MAX
+ 1)
2323 wpmax
= UCHAR_MAX
+ 1;
2324 wp
= (char *) alloca (wpmax
);
2326 memset (wp
, '\0', UCHAR_MAX
+ 1);
2329 if (fc
== ']' || fc
== '-')
2331 /* If ] or - appears before any char in the set, it is not
2332 the terminator or separator, but the first char in the
2338 while ((fc
= *f
++) != '\0' && fc
!= ']')
2339 if (fc
== '-' && *f
!= '\0' && *f
!= ']'
2340 && (unsigned char) f
[-2] <= (unsigned char) *f
)
2342 /* Add all characters from the one before the '-'
2343 up to (but not including) the next format char. */
2344 for (fc
= (unsigned char) f
[-2]; fc
< (unsigned char) *f
; ++fc
)
2348 /* Add the character to the flag map. */
2351 if (__builtin_expect (fc
== '\0', 0))
2357 size_t now
= read_in
;
2358 #ifdef COMPILE_WSCANF
2359 if (__builtin_expect (inchar () == WEOF
, 0))
2366 /* Test whether it's in the scanlist. */
2370 if (runp
[0] == L
'-' && runp
[1] != '\0' && runp
+ 1 != wp
2372 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2374 /* Match against all characters in between the
2375 first and last character of the sequence. */
2378 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2379 if ((wint_t) wc
== c
)
2382 if (wc
<= runp
[1] && !not_in
)
2384 if (wc
<= runp
[1] && not_in
)
2386 /* The current character is not in the
2396 if ((wint_t) *runp
== c
&& !not_in
)
2398 if ((wint_t) *runp
== c
&& not_in
)
2408 if (runp
== wp
&& !not_in
)
2414 if (!(flags
& SUPPRESS
))
2418 if ((flags
& MALLOC
)
2419 && wstr
== (wchar_t *) *strptr
+ strsize
)
2421 /* Enlarge the buffer. */
2422 wstr
= (wchar_t *) realloc (*strptr
,
2424 * sizeof (wchar_t));
2427 /* Can't allocate that much. Last-ditch
2430 realloc (*strptr
, (strsize
+ 1)
2431 * sizeof (wchar_t));
2434 if (flags
& POSIX_MALLOC
)
2436 /* We lose. Oh well. Terminate the string
2437 and stop converting, so at least we don't
2439 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2446 *strptr
= (char *) wstr
;
2453 *strptr
= (char *) wstr
;
2460 while (--width
> 0 && inchar () != WEOF
);
2463 char buf
[MB_LEN_MAX
];
2467 if (__builtin_expect (inchar () == EOF
, 0))
2470 memset (&cstate
, '\0', sizeof (cstate
));
2474 if (wp
[c
] == not_in
)
2476 ungetc_not_eof (c
, s
);
2481 if (!(flags
& SUPPRESS
))
2485 /* Convert it into a wide character. */
2487 n
= __mbrtowc (wstr
, buf
, 1, &cstate
);
2489 if (n
== (size_t) -2)
2491 /* Possibly correct character, just not enough
2494 assert (cnt
< MB_CUR_MAX
);
2500 if ((flags
& MALLOC
)
2501 && wstr
== (wchar_t *) *strptr
+ strsize
)
2503 /* Enlarge the buffer. */
2504 wstr
= (wchar_t *) realloc (*strptr
,
2506 * sizeof (wchar_t)));
2509 /* Can't allocate that much. Last-ditch
2512 realloc (*strptr
, ((strsize
+ 1)
2513 * sizeof (wchar_t)));
2516 if (flags
& POSIX_MALLOC
)
2518 /* We lose. Oh well. Terminate the
2519 string and stop converting,
2520 so at least we don't skip any input. */
2521 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2528 *strptr
= (char *) wstr
;
2535 *strptr
= (char *) wstr
;
2545 while (inchar () != EOF
);
2547 if (__builtin_expect (cnt
!= 0, 0))
2548 /* We stopped in the middle of recognizing another
2549 character. That's a problem. */
2553 if (__builtin_expect (now
== read_in
, 0))
2554 /* We haven't succesfully read any character. */
2557 if (!(flags
& SUPPRESS
))
2561 if ((flags
& MALLOC
)
2562 && wstr
- (wchar_t *) *strptr
!= strsize
)
2564 wchar_t *cp
= (wchar_t *)
2565 realloc (*strptr
, ((wstr
- (wchar_t *) *strptr
)
2566 * sizeof(wchar_t)));
2568 *strptr
= (char *) cp
;
2577 size_t now
= read_in
;
2579 if (__builtin_expect (inchar () == EOF
, 0))
2582 #ifdef COMPILE_WSCANF
2584 memset (&state
, '\0', sizeof (state
));
2591 /* Test whether it's in the scanlist. */
2595 if (runp
[0] == L
'-' && runp
[1] != '\0' && runp
+ 1 != wp
2597 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2599 /* Match against all characters in between the
2600 first and last character of the sequence. */
2603 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2604 if ((wint_t) wc
== c
)
2607 if (wc
<= runp
[1] && !not_in
)
2609 if (wc
<= runp
[1] && not_in
)
2611 /* The current character is not in the
2621 if ((wint_t) *runp
== c
&& !not_in
)
2623 if ((wint_t) *runp
== c
&& not_in
)
2633 if (runp
== wp
&& !not_in
)
2639 if (!(flags
& SUPPRESS
))
2641 if ((flags
& MALLOC
)
2642 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
2644 /* Enlarge the buffer. */
2645 size_t strleng
= str
- *strptr
;
2648 newstr
= (char *) realloc (*strptr
, 2 * strsize
);
2651 /* Can't allocate that much. Last-ditch
2653 newstr
= (char *) realloc (*strptr
,
2654 strleng
+ MB_CUR_MAX
);
2657 if (flags
& POSIX_MALLOC
)
2659 /* We lose. Oh well. Terminate the string
2660 and stop converting, so at least we don't
2662 ((char *) (*strptr
))[strleng
] = '\0';
2670 str
= newstr
+ strleng
;
2671 strsize
= strleng
+ MB_CUR_MAX
;
2677 str
= newstr
+ strleng
;
2683 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
2684 if (__builtin_expect (n
== (size_t) -1, 0))
2687 assert (n
<= MB_CUR_MAX
);
2690 while (--width
> 0 && inchar () != WEOF
);
2695 if (wp
[c
] == not_in
)
2697 ungetc_not_eof (c
, s
);
2702 if (!(flags
& SUPPRESS
))
2705 if ((flags
& MALLOC
)
2706 && (char *) str
== *strptr
+ strsize
)
2708 /* Enlarge the buffer. */
2709 size_t newsize
= 2 * strsize
;
2712 str
= (char *) realloc (*strptr
, newsize
);
2715 /* Can't allocate that much. Last-ditch
2717 if (newsize
> strsize
+ 1)
2719 newsize
= strsize
+ 1;
2722 if (flags
& POSIX_MALLOC
)
2724 /* We lose. Oh well. Terminate the
2725 string and stop converting,
2726 so at least we don't skip any input. */
2727 ((char *) (*strptr
))[strsize
- 1] = '\0';
2734 *strptr
= (char *) str
;
2741 while (--width
> 0 && inchar () != EOF
);
2744 if (__builtin_expect (now
== read_in
, 0))
2745 /* We haven't succesfully read any character. */
2748 if (!(flags
& SUPPRESS
))
2750 #ifdef COMPILE_WSCANF
2751 /* We have to emit the code to get into the initial
2753 char buf
[MB_LEN_MAX
];
2754 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
2755 if (n
> 0 && (flags
& MALLOC
)
2756 && str
+ n
>= *strptr
+ strsize
)
2758 /* Enlarge the buffer. */
2759 size_t strleng
= str
- *strptr
;
2762 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
2765 if (flags
& POSIX_MALLOC
)
2767 /* We lose. Oh well. Terminate the string
2768 and stop converting, so at least we don't
2770 ((char *) (*strptr
))[strleng
] = '\0';
2778 str
= newstr
+ strleng
;
2779 strsize
= strleng
+ n
+ 1;
2783 str
= __mempcpy (str
, buf
, n
);
2787 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
2789 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
2800 case L_('p'): /* Generic pointer. */
2802 /* A PTR must be the same size as a `long int'. */
2803 flags
&= ~(SHORT
|LONGDBL
);
2806 flags
|= READ_POINTER
;
2810 /* If this is an unknown format character punt. */
2815 /* The last thing we saw int the format string was a white space.
2816 Consume the last white spaces. */
2821 while (ISSPACE (c
));
2826 /* Unlock stream. */
2835 if (__builtin_expect (ptrs_to_free
!= NULL
, 0))
2837 struct ptrs_to_free
*p
= ptrs_to_free
;
2840 for (size_t cnt
= 0; cnt
< p
->count
; ++cnt
)
2842 free (*p
->ptrs
[cnt
]);
2843 *p
->ptrs
[cnt
] = NULL
;
2846 free (ptrs_to_free
);
2852 else if (__builtin_expect (strptr
!= NULL
, 0))
2860 #ifdef COMPILE_WSCANF
2862 __vfwscanf (FILE *s
, const wchar_t *format
, va_list argptr
)
2864 return _IO_vfwscanf (s
, format
, argptr
, NULL
);
2866 ldbl_weak_alias (__vfwscanf
, vfwscanf
)
2869 ___vfscanf (FILE *s
, const char *format
, va_list argptr
)
2871 return _IO_vfscanf_internal (s
, format
, argptr
, NULL
);
2873 ldbl_strong_alias (_IO_vfscanf_internal
, _IO_vfscanf
)
2874 ldbl_strong_alias (___vfscanf
, __vfscanf
)
2875 ldbl_hidden_def (___vfscanf
, __vfscanf
)
2876 ldbl_weak_alias (___vfscanf
, vfscanf
)