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 (CHAR_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 wchar_t *twend
= (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. */
2368 while (runp
< twend
)
2370 if (runp
[0] == L
'-' && runp
[1] != '\0'
2371 && runp
+ 1 != twend
2373 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2375 /* Match against all characters in between the
2376 first and last character of the sequence. */
2379 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2380 if ((wint_t) wc
== c
)
2383 if (wc
<= runp
[1] && !not_in
)
2385 if (wc
<= runp
[1] && not_in
)
2387 /* The current character is not in the
2397 if ((wint_t) *runp
== c
&& !not_in
)
2399 if ((wint_t) *runp
== c
&& not_in
)
2409 if (runp
== twend
&& !not_in
)
2415 if (!(flags
& SUPPRESS
))
2419 if ((flags
& MALLOC
)
2420 && wstr
== (wchar_t *) *strptr
+ strsize
)
2422 /* Enlarge the buffer. */
2423 wstr
= (wchar_t *) realloc (*strptr
,
2425 * sizeof (wchar_t));
2428 /* Can't allocate that much. Last-ditch
2431 realloc (*strptr
, (strsize
+ 1)
2432 * sizeof (wchar_t));
2435 if (flags
& POSIX_MALLOC
)
2437 /* We lose. Oh well. Terminate the string
2438 and stop converting, so at least we don't
2440 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2447 *strptr
= (char *) wstr
;
2454 *strptr
= (char *) wstr
;
2461 while (--width
> 0 && inchar () != WEOF
);
2464 char buf
[MB_LEN_MAX
];
2468 if (__builtin_expect (inchar () == EOF
, 0))
2471 memset (&cstate
, '\0', sizeof (cstate
));
2475 if (wp
[c
] == not_in
)
2477 ungetc_not_eof (c
, s
);
2482 if (!(flags
& SUPPRESS
))
2486 /* Convert it into a wide character. */
2488 n
= __mbrtowc (wstr
, buf
, 1, &cstate
);
2490 if (n
== (size_t) -2)
2492 /* Possibly correct character, just not enough
2495 assert (cnt
< MB_CUR_MAX
);
2501 if ((flags
& MALLOC
)
2502 && wstr
== (wchar_t *) *strptr
+ strsize
)
2504 /* Enlarge the buffer. */
2505 wstr
= (wchar_t *) realloc (*strptr
,
2507 * sizeof (wchar_t)));
2510 /* Can't allocate that much. Last-ditch
2513 realloc (*strptr
, ((strsize
+ 1)
2514 * sizeof (wchar_t)));
2517 if (flags
& POSIX_MALLOC
)
2519 /* We lose. Oh well. Terminate the
2520 string and stop converting,
2521 so at least we don't skip any input. */
2522 ((wchar_t *) (*strptr
))[strsize
- 1] = L
'\0';
2529 *strptr
= (char *) wstr
;
2536 *strptr
= (char *) wstr
;
2546 while (inchar () != EOF
);
2548 if (__builtin_expect (cnt
!= 0, 0))
2549 /* We stopped in the middle of recognizing another
2550 character. That's a problem. */
2554 if (__builtin_expect (now
== read_in
, 0))
2555 /* We haven't succesfully read any character. */
2558 if (!(flags
& SUPPRESS
))
2562 if ((flags
& MALLOC
)
2563 && wstr
- (wchar_t *) *strptr
!= strsize
)
2565 wchar_t *cp
= (wchar_t *)
2566 realloc (*strptr
, ((wstr
- (wchar_t *) *strptr
)
2567 * sizeof(wchar_t)));
2569 *strptr
= (char *) cp
;
2578 size_t now
= read_in
;
2580 if (__builtin_expect (inchar () == EOF
, 0))
2583 #ifdef COMPILE_WSCANF
2585 memset (&state
, '\0', sizeof (state
));
2592 /* Test whether it's in the scanlist. */
2594 while (runp
< twend
)
2596 if (runp
[0] == L
'-' && runp
[1] != '\0'
2597 && runp
+ 1 != twend
2599 && (unsigned int) runp
[-1] <= (unsigned int) runp
[1])
2601 /* Match against all characters in between the
2602 first and last character of the sequence. */
2605 for (wc
= runp
[-1] + 1; wc
<= runp
[1]; ++wc
)
2606 if ((wint_t) wc
== c
)
2609 if (wc
<= runp
[1] && !not_in
)
2611 if (wc
<= runp
[1] && not_in
)
2613 /* The current character is not in the
2623 if ((wint_t) *runp
== c
&& !not_in
)
2625 if ((wint_t) *runp
== c
&& not_in
)
2635 if (runp
== twend
&& !not_in
)
2641 if (!(flags
& SUPPRESS
))
2643 if ((flags
& MALLOC
)
2644 && str
+ MB_CUR_MAX
>= *strptr
+ strsize
)
2646 /* Enlarge the buffer. */
2647 size_t strleng
= str
- *strptr
;
2650 newstr
= (char *) realloc (*strptr
, 2 * strsize
);
2653 /* Can't allocate that much. Last-ditch
2655 newstr
= (char *) realloc (*strptr
,
2656 strleng
+ MB_CUR_MAX
);
2659 if (flags
& POSIX_MALLOC
)
2661 /* We lose. Oh well. Terminate the string
2662 and stop converting, so at least we don't
2664 ((char *) (*strptr
))[strleng
] = '\0';
2672 str
= newstr
+ strleng
;
2673 strsize
= strleng
+ MB_CUR_MAX
;
2679 str
= newstr
+ strleng
;
2685 n
= __wcrtomb (!(flags
& SUPPRESS
) ? str
: NULL
, c
, &state
);
2686 if (__builtin_expect (n
== (size_t) -1, 0))
2689 assert (n
<= MB_CUR_MAX
);
2692 while (--width
> 0 && inchar () != WEOF
);
2697 if (wp
[c
] == not_in
)
2699 ungetc_not_eof (c
, s
);
2704 if (!(flags
& SUPPRESS
))
2707 if ((flags
& MALLOC
)
2708 && (char *) str
== *strptr
+ strsize
)
2710 /* Enlarge the buffer. */
2711 size_t newsize
= 2 * strsize
;
2714 str
= (char *) realloc (*strptr
, newsize
);
2717 /* Can't allocate that much. Last-ditch
2719 if (newsize
> strsize
+ 1)
2721 newsize
= strsize
+ 1;
2724 if (flags
& POSIX_MALLOC
)
2726 /* We lose. Oh well. Terminate the
2727 string and stop converting,
2728 so at least we don't skip any input. */
2729 ((char *) (*strptr
))[strsize
- 1] = '\0';
2736 *strptr
= (char *) str
;
2743 while (--width
> 0 && inchar () != EOF
);
2746 if (__builtin_expect (now
== read_in
, 0))
2747 /* We haven't succesfully read any character. */
2750 if (!(flags
& SUPPRESS
))
2752 #ifdef COMPILE_WSCANF
2753 /* We have to emit the code to get into the initial
2755 char buf
[MB_LEN_MAX
];
2756 size_t n
= __wcrtomb (buf
, L
'\0', &state
);
2757 if (n
> 0 && (flags
& MALLOC
)
2758 && str
+ n
>= *strptr
+ strsize
)
2760 /* Enlarge the buffer. */
2761 size_t strleng
= str
- *strptr
;
2764 newstr
= (char *) realloc (*strptr
, strleng
+ n
+ 1);
2767 if (flags
& POSIX_MALLOC
)
2769 /* We lose. Oh well. Terminate the string
2770 and stop converting, so at least we don't
2772 ((char *) (*strptr
))[strleng
] = '\0';
2780 str
= newstr
+ strleng
;
2781 strsize
= strleng
+ n
+ 1;
2785 str
= __mempcpy (str
, buf
, n
);
2789 if ((flags
& MALLOC
) && str
- *strptr
!= strsize
)
2791 char *cp
= (char *) realloc (*strptr
, str
- *strptr
);
2802 case L_('p'): /* Generic pointer. */
2804 /* A PTR must be the same size as a `long int'. */
2805 flags
&= ~(SHORT
|LONGDBL
);
2808 flags
|= READ_POINTER
;
2812 /* If this is an unknown format character punt. */
2817 /* The last thing we saw int the format string was a white space.
2818 Consume the last white spaces. */
2823 while (ISSPACE (c
));
2828 /* Unlock stream. */
2837 if (__builtin_expect (ptrs_to_free
!= NULL
, 0))
2839 struct ptrs_to_free
*p
= ptrs_to_free
;
2842 for (size_t cnt
= 0; cnt
< p
->count
; ++cnt
)
2844 free (*p
->ptrs
[cnt
]);
2845 *p
->ptrs
[cnt
] = NULL
;
2853 else if (__builtin_expect (strptr
!= NULL
, 0))
2861 #ifdef COMPILE_WSCANF
2863 __vfwscanf (FILE *s
, const wchar_t *format
, va_list argptr
)
2865 return _IO_vfwscanf (s
, format
, argptr
, NULL
);
2867 ldbl_weak_alias (__vfwscanf
, vfwscanf
)
2870 ___vfscanf (FILE *s
, const char *format
, va_list argptr
)
2872 return _IO_vfscanf_internal (s
, format
, argptr
, NULL
);
2874 ldbl_strong_alias (_IO_vfscanf_internal
, _IO_vfscanf
)
2875 ldbl_strong_alias (___vfscanf
, __vfscanf
)
2876 ldbl_hidden_def (___vfscanf
, __vfscanf
)
2877 ldbl_weak_alias (___vfscanf
, vfscanf
)