Update.
[glibc.git] / stdio-common / vfscanf.c
blob3ca82cc4c7c36b60a12a89d72840644e73644211
1 /* Copyright (C) 1991-1999, 2000 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #include <assert.h>
20 #include <errno.h>
21 #include <limits.h>
22 #include <ctype.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <wchar.h>
29 #include <wctype.h>
30 #include <bits/libc-lock.h>
31 #include <locale/localeinfo.h>
33 #ifdef __GNUC__
34 # define HAVE_LONGLONG
35 # define LONGLONG long long
36 #else
37 # define LONGLONG long
38 #endif
40 /* Determine whether we have to handle `long long' at all. */
41 #if LONG_MAX == LONG_LONG_MAX
42 # define need_longlong 0
43 #else
44 # define need_longlong 1
45 #endif
47 /* Determine whether we have to handle `long'. */
48 #if INT_MAX == LONG_MAX
49 # define need_long 0
50 #else
51 # define need_long 1
52 #endif
54 /* Those are flags in the conversion format. */
55 #define LONG 0x001 /* l: long or double */
56 #define LONGDBL 0x002 /* L: long long or long double */
57 #define SHORT 0x004 /* h: short */
58 #define SUPPRESS 0x008 /* *: suppress assignment */
59 #define POINTER 0x010 /* weird %p pointer (`fake hex') */
60 #define NOSKIP 0x020 /* do not skip blanks */
61 #define WIDTH 0x040 /* width was given */
62 #define GROUP 0x080 /* ': group numbers */
63 #define MALLOC 0x100 /* a: malloc strings */
64 #define CHAR 0x200 /* hh: char */
65 #define I18N 0x400 /* I: use locale's digits */
68 #ifdef USE_IN_LIBIO
69 # include <libioP.h>
70 # include <libio.h>
72 # undef va_list
73 # define va_list _IO_va_list
75 # ifdef COMPILE_WPRINTF
76 # define ungetc(c, s) ((void) (c == WEOF \
77 || (--read_in, \
78 _IO_sputbackwc (s, c))))
79 # define inchar() (c == WEOF ? WEOF \
80 : ((c = _IO_getwc_unlocked (s)), \
81 (void) (c != WEOF && ++read_in), c))
83 # define MEMCPY(d, s, n) wmemcpy (d, s, n)
84 # define ISSPACE(Ch) iswspace (Ch)
85 # define ISDIGIT(Ch) iswdigit (Ch)
86 # define ISXDIGIT(Ch) iswxdigit (Ch)
87 # define TOLOWER(Ch) towlower (Ch)
88 # define ORIENT if (s->_vtable_offset == 0 && _IO_fwide (s, 1) != 1)\
89 return EOF
90 # define __strtoll_internal __wcstoll_internal
91 # define __strtoull_internal __wcstoull_internal
92 # define __strtol_internal __wcstol_internal
93 # define __strtoul_internal __wcstoul_internal
94 # define __strtold_internal __wcstold_internal
95 # define __strtod_internal __wcstod_internal
96 # define __strtof_internal __wcstof_internal
98 # define L_(Str) L##Str
99 # define CHAR_T wchar_t
100 # define UCHAR_T unsigned int
101 # define WINT_T wint_t
102 # else
103 # define ungetc(c, s) ((void) ((int) c == EOF \
104 || (--read_in, \
105 _IO_sputbackc (s, (unsigned char) c))))
106 # define inchar() (c == EOF ? EOF \
107 : ((c = _IO_getc_unlocked (s)), \
108 (void) (c != EOF && ++read_in), c))
109 # define MEMCPY(d, s, n) memcpy (d, s, n)
110 # define ISSPACE(Ch) isspace (Ch)
111 # define ISDIGIT(Ch) isdigit (Ch)
112 # define ISXDIGIT(Ch) isxdigit (Ch)
113 # define TOLOWER(Ch) tolower (Ch)
114 # define ORIENT if (_IO_fwide (s, -1) != -1) return EOF
116 # define L_(Str) Str
117 # define CHAR_T char
118 # define UCHAR_T unsigned char
119 # define WINT_T int
120 # endif
122 # define encode_error() do { \
123 if (errp != NULL) *errp |= 4; \
124 _IO_funlockfile (s); \
125 __libc_cleanup_end (0); \
126 __set_errno (EILSEQ); \
127 return done; \
128 } while (0)
129 # define conv_error() do { \
130 if (errp != NULL) *errp |= 2; \
131 _IO_funlockfile (s); \
132 __libc_cleanup_end (0); \
133 return done; \
134 } while (0)
135 # define input_error() do { \
136 _IO_funlockfile (s); \
137 if (errp != NULL) *errp |= 1; \
138 __libc_cleanup_end (0); \
139 return done ?: EOF; \
140 } while (0)
141 # define memory_error() do { \
142 _IO_funlockfile (s); \
143 __set_errno (ENOMEM); \
144 __libc_cleanup_end (0); \
145 return EOF; \
146 } while (0)
147 # define ARGCHECK(s, format) \
148 do \
150 /* Check file argument for consistence. */ \
151 CHECK_FILE (s, EOF); \
152 if (s->_flags & _IO_NO_READS) \
154 __set_errno (EBADF); \
155 return EOF; \
157 else if (format == NULL) \
159 MAYBE_SET_EINVAL; \
160 return EOF; \
162 } while (0)
163 # define LOCK_STREAM(S) \
164 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, (S)); \
165 _IO_flockfile (S)
166 # define UNLOCK_STREAM(S) \
167 _IO_funlockfile (S); \
168 __libc_cleanup_region_end (0)
169 #else
170 # define ungetc(c, s) ((void) (c != EOF && --read_in), ungetc (c, s))
171 # define inchar() (c == EOF ? EOF \
172 : ((c = getc (s)), (void) (c != EOF && ++read_in), c))
173 # define MEMCPY(d, s, n) memcpy (d, s, n)
174 # define ISSPACE(Ch) isspace (Ch)
175 # define ISDIGIT(Ch) isdigit (Ch)
176 # define ISXDIGIT(Ch) isxdigit (Ch)
177 # define TOLOWER(Ch) tolower (Ch)
179 # define L_(Str) Str
180 # define CHAR_T char
181 # define UCHAR_T unsigned char
182 # define WINT_T int
184 # define encode_error() do { \
185 funlockfile (s); \
186 __set_errno (EILSEQ); \
187 return done; \
188 } while (0)
189 # define conv_error() do { \
190 funlockfile (s); \
191 return done; \
192 } while (0)
193 # define input_error() do { \
194 funlockfile (s); \
195 return done ?: EOF; \
196 } while (0)
197 # define memory_error() do { \
198 funlockfile (s); \
199 __set_errno (ENOMEM); \
200 return EOF; \
201 } while (0)
202 # define ARGCHECK(s, format) \
203 do \
205 /* Check file argument for consistence. */ \
206 if (!__validfp (s) || !s->__mode.__read) \
208 __set_errno (EBADF); \
209 return EOF; \
211 else if (format == NULL) \
213 __set_errno (EINVAL); \
214 return EOF; \
216 } while (0)
217 #if 1
218 /* XXX For now !!! */
219 # define flockfile(S) /* nothing */
220 # define funlockfile(S) /* nothing */
221 # define LOCK_STREAM(S)
222 # define UNLOCK_STREAM(S)
223 #else
224 # define LOCK_STREAM(S) \
225 __libc_cleanup_region_start (&__funlockfile, (S)); \
226 __flockfile (S)
227 # define UNLOCK_STREAM(S) \
228 __funlockfile (S); \
229 __libc_cleanup_region_end (0)
230 #endif
231 #endif
234 /* Read formatted input from S according to the format string
235 FORMAT, using the argument list in ARG.
236 Return the number of assignments made, or -1 for an input error. */
237 #ifdef USE_IN_LIBIO
238 # ifdef COMPILE_WPRINTF
240 _IO_vfwscanf (s, format, argptr, errp)
241 _IO_FILE *s;
242 const wchar_t *format;
243 _IO_va_list argptr;
244 int *errp;
245 # else
247 _IO_vfscanf (s, format, argptr, errp)
248 _IO_FILE *s;
249 const char *format;
250 _IO_va_list argptr;
251 int *errp;
252 # endif
253 #else
255 __vfscanf (FILE *s, const char *format, va_list argptr)
256 #endif
258 va_list arg;
259 register const CHAR_T *f = format;
260 register UCHAR_T fc; /* Current character of the format. */
261 register size_t done = 0; /* Assignments done. */
262 register size_t read_in = 0; /* Chars read in. */
263 register WINT_T c = 0; /* Last char read. */
264 register int width; /* Maximum field width. */
265 register int flags; /* Modifiers for current format element. */
267 /* Status for reading F-P nums. */
268 char got_dot, got_e, negative;
269 /* If a [...] is a [^...]. */
270 CHAR_T not_in;
271 #define exp_char not_in
272 /* Base for integral numbers. */
273 int base;
274 /* Signedness for integral numbers. */
275 int number_signed;
276 #define is_hexa number_signed
277 /* Decimal point character. */
278 wchar_t decimal;
279 /* The thousands character of the current locale. */
280 wchar_t thousands;
281 /* State for the conversions. */
282 mbstate_t state;
283 /* Integral holding variables. */
284 union
286 long long int q;
287 unsigned long long int uq;
288 long int l;
289 unsigned long int ul;
290 } num;
291 /* Character-buffer pointer. */
292 char *str = NULL;
293 wchar_t *wstr = NULL;
294 char **strptr = NULL;
295 size_t strsize = 0;
296 /* We must not react on white spaces immediately because they can
297 possibly be matched even if in the input stream no character is
298 available anymore. */
299 int skip_space = 0;
300 /* Nonzero if we are reading a pointer. */
301 int read_pointer;
302 /* Workspace. */
303 CHAR_T *tw; /* Temporary pointer. */
304 CHAR_T *wp = NULL; /* Workspace. */
305 size_t wpmax = 0; /* Maximal size of workspace. */
306 size_t wpsize; /* Currently used bytes in workspace. */
307 #define ADDW(Ch) \
308 do \
310 if (wpsize == wpmax) \
312 CHAR_T *old = wp; \
313 wpmax = UCHAR_MAX > 2 * wpmax ? UCHAR_MAX : 2 * wpmax; \
314 wp = (CHAR_T *) alloca (wpmax * sizeof (wchar_t)); \
315 if (old != NULL) \
316 MEMCPY (wp, old, wpsize); \
318 wp[wpsize++] = (Ch); \
320 while (0)
322 #ifdef __va_copy
323 __va_copy (arg, argptr);
324 #else
325 arg = (va_list) argptr;
326 #endif
328 #ifdef ORIENT
329 ORIENT;
330 #endif
332 ARGCHECK (s, format);
334 /* Figure out the decimal point character. */
335 memset (&state, '\0', sizeof (state));
336 if (__mbrtowc (&decimal, _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT),
337 strlen (_NL_CURRENT (LC_NUMERIC, DECIMAL_POINT)), &state)
338 <= 0)
339 decimal = (wchar_t) *_NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
340 /* Figure out the thousands separator character. */
341 memset (&state, '\0', sizeof (state));
342 if (__mbrtowc (&thousands, _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
343 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP)),
344 &state) <= 0)
345 thousands = (wchar_t) *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
347 /* Lock the stream. */
348 LOCK_STREAM (s);
351 #ifndef COMPILE_WPRINTF
352 /* From now on we use `state' to convert the format string. */
353 memset (&state, '\0', sizeof (state));
354 #endif
356 /* Run through the format string. */
357 while (*f != '\0')
359 unsigned int argpos;
360 /* Extract the next argument, which is of type TYPE.
361 For a %N$... spec, this is the Nth argument from the beginning;
362 otherwise it is the next argument after the state now in ARG. */
363 #ifdef __va_copy
364 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
365 ({ unsigned int pos = argpos; \
366 va_list arg; \
367 __va_copy (arg, argptr); \
368 while (--pos > 0) \
369 (void) va_arg (arg, void *); \
370 va_arg (arg, type); \
372 #else
373 # if 0
374 /* XXX Possible optimization. */
375 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
376 ({ va_list arg = (va_list) argptr; \
377 arg = (va_list) ((char *) arg \
378 + (argpos - 1) \
379 * __va_rounded_size (void *)); \
380 va_arg (arg, type); \
382 # else
383 # define ARG(type) (argpos == 0 ? va_arg (arg, type) : \
384 ({ unsigned int pos = argpos; \
385 va_list arg = (va_list) argptr; \
386 while (--pos > 0) \
387 (void) va_arg (arg, void *); \
388 va_arg (arg, type); \
390 # endif
391 #endif
393 #ifndef COMPILE_WPRINTF
394 if (!isascii (*f))
396 /* Non-ASCII, may be a multibyte. */
397 int len = __mbrlen (f, strlen (f), &state);
398 if (len > 0)
402 c = inchar ();
403 if (c == EOF)
404 input_error ();
405 else if (c != *f++)
407 ungetc (c, s);
408 conv_error ();
411 while (--len > 0);
412 continue;
415 #endif
417 fc = *f++;
418 if (fc != '%')
420 /* Remember to skip spaces. */
421 if (ISSPACE (fc))
423 skip_space = 1;
424 continue;
427 /* Read a character. */
428 c = inchar ();
430 /* Characters other than format specs must just match. */
431 if (c == EOF)
432 input_error ();
434 /* We saw white space char as the last character in the format
435 string. Now it's time to skip all leading white space. */
436 if (skip_space)
438 while (ISSPACE (c))
439 if (inchar () == EOF && errno == EINTR)
440 conv_error ();
441 skip_space = 0;
444 if (c != fc)
446 ungetc (c, s);
447 conv_error ();
450 continue;
453 /* This is the start of the conversion string. */
454 flags = 0;
456 /* Not yet decided whether we read a pointer or not. */
457 read_pointer = 0;
459 /* Initialize state of modifiers. */
460 argpos = 0;
462 /* Prepare temporary buffer. */
463 wpsize = 0;
465 /* Check for a positional parameter specification. */
466 if (ISDIGIT (*f))
468 argpos = *f++ - L_('0');
469 while (ISDIGIT (*f))
470 argpos = argpos * 10 + (*f++ - L_('0'));
471 if (*f == L_('$'))
472 ++f;
473 else
475 /* Oops; that was actually the field width. */
476 width = argpos;
477 flags |= WIDTH;
478 argpos = 0;
479 goto got_width;
483 /* Check for the assignment-suppressing, the number grouping flag,
484 and the signal to use the locale's digit representation. */
485 while (*f == L_('*') || *f == L_('\'') || *f == L_('I'))
486 switch (*f++)
488 case L_('*'):
489 flags |= SUPPRESS;
490 break;
491 case L_('\''):
492 flags |= GROUP;
493 break;
494 case L_('I'):
495 flags |= I18N;
496 break;
499 /* We have seen width. */
500 if (ISDIGIT (*f))
501 flags |= WIDTH;
503 /* Find the maximum field width. */
504 width = 0;
505 while (ISDIGIT (*f))
507 width *= 10;
508 width += *f++ - L_('0');
510 got_width:
511 if (width == 0)
512 width = -1;
514 /* Check for type modifiers. */
515 switch (*f++)
517 case L_('h'):
518 /* ints are short ints or chars. */
519 if (*f == L_('h'))
521 ++f;
522 flags |= CHAR;
524 else
525 flags |= SHORT;
526 break;
527 case L_('l'):
528 if (*f == L_('l'))
530 /* A double `l' is equivalent to an `L'. */
531 ++f;
532 flags |= LONGDBL | LONG;
534 else
535 /* ints are long ints. */
536 flags |= LONG;
537 break;
538 case L_('q'):
539 case L_('L'):
540 /* doubles are long doubles, and ints are long long ints. */
541 flags |= LONGDBL | LONG;
542 break;
543 case L_('a'):
544 /* The `a' is used as a flag only if followed by `s', `S' or
545 `['. */
546 if (*f != L_('s') && *f != L_('S') && *f != L_('['))
548 --f;
549 break;
551 /* String conversions (%s, %[) take a `char **'
552 arg and fill it in with a malloc'd pointer. */
553 flags |= MALLOC;
554 break;
555 case L_('z'):
556 if (need_longlong && sizeof (size_t) > sizeof (unsigned long int))
557 flags |= LONGDBL;
558 else if (sizeof (size_t) > sizeof (unsigned int))
559 flags |= LONG;
560 break;
561 case L_('j'):
562 if (need_longlong && sizeof (uintmax_t) > sizeof (unsigned long int))
563 flags |= LONGDBL;
564 else if (sizeof (uintmax_t) > sizeof (unsigned int))
565 flags |= LONG;
566 break;
567 case L_('t'):
568 if (need_longlong && sizeof (ptrdiff_t) > sizeof (long int))
569 flags |= LONGDBL;
570 else if (sizeof (ptrdiff_t) > sizeof (int))
571 flags |= LONG;
572 break;
573 default:
574 /* Not a recognized modifier. Backup. */
575 --f;
576 break;
579 /* End of the format string? */
580 if (*f == L_('\0'))
581 conv_error ();
583 /* Find the conversion specifier. */
584 fc = *f++;
585 if (skip_space || (fc != L_('[') && fc != L_('c')
586 && fc != L_('C') && fc != L_('n')))
588 /* Eat whitespace. */
589 int save_errno = errno;
590 errno = 0;
592 if (inchar () == EOF && errno == EINTR)
593 input_error ();
594 while (ISSPACE (c));
595 errno = save_errno;
596 ungetc (c, s);
597 skip_space = 0;
600 switch (fc)
602 case L_('%'): /* Must match a literal '%'. */
603 c = inchar ();
604 if (c == EOF)
605 input_error ();
606 if (c != fc)
608 ungetc (c, s);
609 conv_error ();
611 break;
613 case L_('n'): /* Answer number of assignments done. */
614 /* Corrigendum 1 to ISO C 1990 describes the allowed flags
615 with the 'n' conversion specifier. */
616 if (!(flags & SUPPRESS))
618 /* Don't count the read-ahead. */
619 if (need_longlong && (flags & LONGDBL))
620 *ARG (long long int *) = read_in;
621 else if (need_long && (flags & LONG))
622 *ARG (long int *) = read_in;
623 else if (flags & SHORT)
624 *ARG (short int *) = read_in;
625 else if (!(flags & CHAR))
626 *ARG (int *) = read_in;
627 else
628 *ARG (char *) = read_in;
630 #ifdef NO_BUG_IN_ISO_C_CORRIGENDUM_1
631 /* We have a severe problem here. The ISO C standard
632 contradicts itself in explaining the effect of the %n
633 format in `scanf'. While in ISO C:1990 and the ISO C
634 Amendement 1:1995 the result is described as
636 Execution of a %n directive does not effect the
637 assignment count returned at the completion of
638 execution of the f(w)scanf function.
640 in ISO C Corrigendum 1:1994 the following was added:
642 Subclause 7.9.6.2
643 Add the following fourth example:
645 #include <stdio.h>
646 int d1, d2, n1, n2, i;
647 i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);
648 the value 123 is assigned to d1 and the value3 to n1.
649 Because %n can never get an input failure the value
650 of 3 is also assigned to n2. The value of d2 is not
651 affected. The value 3 is assigned to i.
653 We go for now with the historically correct code from ISO C,
654 i.e., we don't count the %n assignments. When it ever
655 should proof to be wrong just remove the #ifdef above. */
656 ++done;
657 #endif
659 break;
661 case L_('c'): /* Match characters. */
662 if ((flags & LONG) == 0)
664 if (!(flags & SUPPRESS))
666 str = ARG (char *);
667 if (str == NULL)
668 conv_error ();
671 c = inchar ();
672 if (c == EOF)
673 input_error ();
675 if (width == -1)
676 width = 1;
678 #ifdef COMPILE_WPRINTF
679 /* We have to convert the wide character(s) into multibyte
680 characters and store the result. */
681 memset (&state, '\0', sizeof (state));
685 size_t n;
687 n = wcrtomb (!(flags & SUPPRESS) ? str : NULL, c, &state);
688 if (n == (size_t) -1)
689 /* No valid wide character. */
690 input_error ();
692 /* Increment the output pointer. Even if we don't
693 write anything. */
694 str += n;
696 while (--width > 0 && inchar () != EOF);
697 #else
698 if (!(flags & SUPPRESS))
701 *str++ = c;
702 while (--width > 0 && inchar () != EOF);
704 else
705 while (--width > 0 && inchar () != EOF);
706 #endif
708 if (!(flags & SUPPRESS))
709 ++done;
711 break;
713 /* FALLTHROUGH */
714 case L_('C'):
715 if (!(flags & SUPPRESS))
717 wstr = ARG (wchar_t *);
718 if (str == NULL)
719 conv_error ();
722 c = inchar ();
723 if (c == EOF)
724 input_error ();
726 #ifdef COMPILE_WPRINTF
727 /* Just store the incoming wide characters. */
728 if (!(flags & SUPPRESS))
731 *wstr++ = c;
732 while (--width > 0 && inchar () != EOF);
734 else
735 while (--width > 0 && inchar () != EOF);
736 #else
738 /* We have to convert the multibyte input sequence to wide
739 characters. */
740 char buf[MB_LEN_MAX];
741 mbstate_t cstate;
743 memset (&cstate, '\0', sizeof (cstate));
747 size_t cnt;
749 /* This is what we present the mbrtowc function first. */
750 buf[0] = c;
751 cnt = 1;
753 while (1)
755 size_t n;
757 n = __mbrtowc (!(flags & SUPPRESS) ? wstr : NULL,
758 buf, cnt, &cstate);
760 if (n == (size_t) -2)
762 /* Possibly correct character, just not enough
763 input. */
764 assert (cnt < MB_CUR_MAX);
766 if (inchar () == EOF)
767 encode_error ();
769 buf[cnt++] = c;
770 continue;
773 if (n != cnt)
774 encode_error ();
776 /* We have a match. */
777 break;
780 /* Advance the result pointer. */
781 ++wstr;
783 while (--width > 0 && inchar () != EOF);
785 #endif
787 if (!(flags & SUPPRESS))
788 ++done;
790 break;
792 case L_('s'): /* Read a string. */
793 if (!(flags & LONG))
795 #define STRING_ARG(Str, Type) \
796 do if (!(flags & SUPPRESS)) \
798 if (flags & MALLOC) \
800 /* The string is to be stored in a malloc'd buffer. */ \
801 strptr = ARG (char **); \
802 if (strptr == NULL) \
803 conv_error (); \
804 /* Allocate an initial buffer. */ \
805 strsize = 100; \
806 *strptr = (char *) malloc (strsize * sizeof (Type)); \
807 Str = (Type *) *strptr; \
809 else \
810 Str = ARG (Type *); \
811 if (Str == NULL) \
812 conv_error (); \
813 } while (0)
814 STRING_ARG (str, char);
816 c = inchar ();
817 if (c == EOF)
818 input_error ();
820 #ifdef COMPILE_WPRINTF
821 memset (&state, '\0', sizeof (state));
822 #endif
826 if (ISSPACE (c))
828 ungetc (c, s);
829 break;
832 #ifdef COMPILE_WPRINTF
833 /* This is quite complicated. We have to convert the
834 wide characters into multibyte characters and then
835 store them. */
837 size_t n;
839 if (!(flags & SUPPRESS) && (flags & MALLOC)
840 && str + MB_CUR_MAX >= *strptr + strsize)
842 /* We have to enlarge the buffer if the `a' flag
843 was given. */
844 str = (char *) realloc (*strptr, strsize * 2);
845 if (str == NULL)
847 /* Can't allocate that much. Last-ditch
848 effort. */
849 str = (char *) realloc (*strptr, strsize + 1);
850 if (str == NULL)
852 /* We lose. Oh well. Terminate the
853 string and stop converting,
854 so at least we don't skip any input. */
855 ((char *) (*strptr))[strsize - 1] = '\0';
856 ++done;
857 conv_error ();
859 else
861 *strptr = (char *) str;
862 str += strsize;
863 ++strsize;
866 else
868 *strptr = (char *) str;
869 str += strsize;
870 strsize *= 2;
874 n = wcrtomb (!(flags & SUPPRESS) ? str : NULL, c, &state);
875 if (n == (size_t) -1)
876 encode_error ();
878 assert (n <= MB_CUR_MAX);
879 str += n;
881 #else
882 /* This is easy. */
883 if (!(flags & SUPPRESS))
885 *str++ = c;
886 if ((flags & MALLOC)
887 && (char *) str == *strptr + strsize)
889 /* Enlarge the buffer. */
890 str = (char *) realloc (*strptr, 2 * strsize);
891 if (str == NULL)
893 /* Can't allocate that much. Last-ditch
894 effort. */
895 str = (char *) realloc (*strptr, strsize + 1);
896 if (str == NULL)
898 /* We lose. Oh well. Terminate the
899 string and stop converting,
900 so at least we don't skip any input. */
901 ((char *) (*strptr))[strsize - 1] = '\0';
902 ++done;
903 conv_error ();
905 else
907 *strptr = (char *) str;
908 str += strsize;
909 ++strsize;
912 else
914 *strptr = (char *) str;
915 str += strsize;
916 strsize *= 2;
920 #endif
922 while ((width <= 0 || --width > 0) && inchar () != EOF);
924 if (!(flags & SUPPRESS))
926 #ifdef COMPILE_WPRINTF
927 /* We have to emit the code to get into the intial
928 state. */
929 char buf[MB_LEN_MAX];
930 size_t n = wcrtomb (buf, L'\0', &state);
931 if (n > 0 && (flags & MALLOC)
932 && str + n >= *strptr + strsize)
934 /* Enlarge the buffer. */
935 str = (char *) realloc (*strptr,
936 (str + n + 1) - *strptr);
937 if (str == NULL)
939 /* We lose. Oh well. Terminate the string
940 and stop converting, so at least we don't
941 skip any input. */
942 ((char *) (*strptr))[strsize - 1] = '\0';
943 ++done;
944 conv_error ();
946 else
948 *strptr = (char *) str;
949 str = ((char *) *strptr) + strsize;
950 strsize = (str + n + 1) - *strptr;
954 str = __mempcpy (str, buf, n);
955 #endif
956 *str = '\0';
958 if ((flags & MALLOC) && str - *strptr != strsize)
960 char *cp = (char *) realloc (*strptr, str - *strptr);
961 if (cp != NULL)
962 *strptr = cp;
965 ++done;
967 break;
969 /* FALLTHROUGH */
971 case L_('S'):
973 #ifndef COMPILE_WPRINTF
974 mbstate_t cstate;
975 #endif
977 /* Wide character string. */
978 STRING_ARG (wstr, wchar_t);
980 c = inchar ();
981 if (c == EOF)
982 input_error ();
984 #ifndef COMPILE_WPRINTF
985 memset (&cstate, '\0', sizeof (cstate));
986 #endif
990 if (ISSPACE (c))
992 ungetc (c, s);
993 break;
996 #ifdef COMPILE_WPRINTF
997 /* This is easy. */
998 if (!(flags & SUPPRESS))
1000 *wstr++ = c;
1001 if ((flags & MALLOC)
1002 && wstr == (wchar_t *) *strptr + strsize)
1004 /* Enlarge the buffer. */
1005 wstr = (wchar_t *) realloc (*strptr,
1006 (2 * strsize)
1007 * sizeof (wchar_t));
1008 if (wstr == NULL)
1010 /* Can't allocate that much. Last-ditch
1011 effort. */
1012 wstr = (wchar_t *) realloc (*strptr,
1013 (strsize
1014 + sizeof (wchar_t)));
1015 if (wstr == NULL)
1017 /* We lose. Oh well. Terminate the string
1018 and stop converting, so at least we don't
1019 skip any input. */
1020 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
1021 ++done;
1022 conv_error ();
1024 else
1026 *strptr = (char *) wstr;
1027 wstr += strsize;
1028 ++strsize;
1031 else
1033 *strptr = (char *) wstr;
1034 wstr += strsize;
1035 strsize *= 2;
1039 #else
1041 char buf[MB_LEN_MAX];
1042 size_t cnt;
1044 buf[0] = c;
1045 cnt = 1;
1047 while (1)
1049 size_t n;
1051 n = __mbrtowc (!(flags & SUPPRESS) ? wstr : NULL,
1052 buf, cnt, &cstate);
1054 if (n == (size_t) -2)
1056 /* Possibly correct character, just not enough
1057 input. */
1058 assert (cnt < MB_CUR_MAX);
1060 if (inchar () == EOF)
1061 encode_error ();
1063 buf[cnt++] = c;
1064 continue;
1067 if (n != cnt)
1068 encode_error ();
1070 /* We have a match. */
1071 break;
1074 if (!(flags & SUPPRESS) && (flags & MALLOC)
1075 && wstr == (wchar_t *) *strptr + strsize)
1077 /* Enlarge the buffer. */
1078 wstr = (wchar_t *) realloc (*strptr,
1079 (2 * strsize
1080 * sizeof (wchar_t)));
1081 if (wstr == NULL)
1083 /* Can't allocate that much. Last-ditch effort. */
1084 wstr = (wchar_t *) realloc (*strptr,
1085 ((strsize + 1)
1086 * sizeof (wchar_t)));
1087 if (wstr == NULL)
1089 /* We lose. Oh well. Terminate the
1090 string and stop converting, so at
1091 least we don't skip any input. */
1092 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
1093 ++done;
1094 conv_error ();
1096 else
1098 *strptr = (char *) wstr;
1099 wstr += strsize;
1100 ++strsize;
1103 else
1105 *strptr = (char *) wstr;
1106 wstr += strsize;
1107 strsize *= 2;
1111 #endif
1113 while ((width <= 0 || --width > 0) && inchar () != EOF);
1115 if (!(flags & SUPPRESS))
1117 *wstr++ = L'\0';
1119 if ((flags & MALLOC) && wstr - (wchar_t *) *strptr != strsize)
1121 wchar_t *cp = (wchar_t *) realloc (*strptr,
1122 ((wstr
1123 - (wchar_t *) *strptr)
1124 * sizeof(wchar_t)));
1125 if (cp != NULL)
1126 *strptr = (char *) cp;
1129 ++done;
1132 break;
1134 case L_('x'): /* Hexadecimal integer. */
1135 case L_('X'): /* Ditto. */
1136 base = 16;
1137 number_signed = 0;
1138 goto number;
1140 case L_('o'): /* Octal integer. */
1141 base = 8;
1142 number_signed = 0;
1143 goto number;
1145 case L_('u'): /* Unsigned decimal integer. */
1146 base = 10;
1147 number_signed = 0;
1148 goto number;
1150 case L_('d'): /* Signed decimal integer. */
1151 base = 10;
1152 number_signed = 1;
1153 goto number;
1155 case L_('i'): /* Generic number. */
1156 base = 0;
1157 number_signed = 1;
1159 number:
1160 c = inchar ();
1161 if (c == EOF)
1162 input_error ();
1164 /* Check for a sign. */
1165 if (c == L_('-') || c == L_('+'))
1167 ADDW (c);
1168 if (width > 0)
1169 --width;
1170 c = inchar ();
1173 /* Look for a leading indication of base. */
1174 if (width != 0 && c == L_('0'))
1176 if (width > 0)
1177 --width;
1179 ADDW (c);
1180 c = inchar ();
1182 if (width != 0 && TOLOWER (c) == L_('x'))
1184 if (base == 0)
1185 base = 16;
1186 if (base == 16)
1188 if (width > 0)
1189 --width;
1190 c = inchar ();
1193 else if (base == 0)
1194 base = 8;
1197 if (base == 0)
1198 base = 10;
1200 if (base == 10 && (flags & I18N) != 0)
1202 int from_level;
1203 int to_level;
1204 #ifdef COMPILE_WPRINTF
1205 const wchar_t *wcdigits[10];
1206 #else
1207 const char *mbdigits[10];
1208 #endif
1209 int n;
1211 from_level = 0;
1212 #ifdef COMPILE_WPRINTF
1213 to_level = _NL_CURRENT_WORD (LC_CTYPE,
1214 _NL_CTYPE_INDIGITS_WC_LEN) - 1;
1215 #else
1216 to_level = _NL_CURRENT_WORD (LC_CTYPE,
1217 _NL_CTYPE_INDIGITS_MB_LEN) - 1;
1218 #endif
1220 /* In this round we get the pointer to the digit strings
1221 and also perform the first round of comparisons. */
1222 for (n = 0; n < 10; ++n)
1224 /* Get the string for the digits with value N. */
1225 #ifdef COMPILE_WPRINTF
1226 wcdigits[n] = (const wchar_t *)
1227 _NL_CURRENT (LC_CTYPE, _NL_CTYPE_INDIGITS0_WC + n);
1228 if (c == *wcdigits[n])
1229 break;
1231 /* Advance the pointer to the next string. */
1232 ++wcdigits[n];
1233 #else
1234 size_t dlen;
1235 size_t dcnt;
1237 mbdigits[n] = _NL_CURRENT (LC_CTYPE,
1238 _NL_CTYPE_INDIGITS0_MB + n);
1239 dlen = strlen (mbdigits[n]);
1241 dcnt = 0;
1244 if (c != mbdigits[n][dcnt])
1245 break;
1246 c = inchar ();
1248 while (--dcnt > 0);
1250 if (dcnt == 0)
1251 /* We found it. */
1252 break;
1254 /* Advance the pointer to the next string. */
1255 mbdigits[n] += dlen + 1;
1256 #endif
1259 if (n == 10)
1261 /*Have not yet found the digit. */
1262 while (++from_level <= to_level)
1264 /* Search all ten digits of this level. */
1265 for (n = 0; n < 10; ++n)
1267 #ifdef COMPILE_WPRINTF
1268 if (c == *wcdigits[n])
1269 break;
1271 /* Advance the pointer to the next string. */
1272 ++wcdigits[n];
1273 #else
1274 size_t dlen = strlen (mbdigits[n]);
1275 size_t dcnt;
1277 dcnt = 0;
1280 if (c != mbdigits[n][dcnt])
1281 break;
1282 c = inchar ();
1284 while (--dcnt > 0);
1286 if (dcnt == 0)
1287 /* We found it. */
1288 break;
1290 /* Advance the pointer to the next string. */
1291 mbdigits[n] += dlen + 1;
1292 #endif
1295 if (n < 10)
1296 /* Found it. */
1297 break;
1299 /* Next level. */
1300 ++from_level;
1304 if (n == 10)
1306 /* Haven't found anything. Push the last character back
1307 and return an error. */
1308 ungetc (c, s);
1309 input_error ();
1312 ADDW (L_('0') + n);
1314 else
1315 /* Read the number into workspace. */
1316 while (c != EOF && width != 0)
1318 if (base == 16 ? !ISXDIGIT (c) :
1319 ((!ISDIGIT (c) || c - L_('0') >= base) &&
1320 !((flags & GROUP) && base == 10 && c == thousands)))
1321 break;
1322 ADDW (c);
1323 if (width > 0)
1324 --width;
1326 c = inchar ();
1329 if (wpsize == 0
1330 || (wpsize == 1 && (wp[0] == L_('+') || wp[0] == L_('-'))))
1332 /* There was no number. If we are supposed to read a pointer
1333 we must recognize "(nil)" as well. */
1334 if (wpsize == 0 && read_pointer && (width < 0 || width >= 0)
1335 && c == '('
1336 && TOLOWER (inchar ()) == L_('n')
1337 && TOLOWER (inchar ()) == L_('i')
1338 && TOLOWER (inchar ()) == L_('l')
1339 && inchar () == L_(')'))
1340 /* We must produce the value of a NULL pointer. A single
1341 '0' digit is enough. */
1342 ADDW (L_('0'));
1343 else
1345 /* The last read character is not part of the number
1346 anymore. */
1347 ungetc (c, s);
1349 conv_error ();
1352 else
1353 /* The just read character is not part of the number anymore. */
1354 ungetc (c, s);
1356 /* Convert the number. */
1357 ADDW (L_('\0'));
1358 if (need_longlong && (flags & LONGDBL))
1360 if (number_signed)
1361 num.q = __strtoll_internal (wp, &tw, base, flags & GROUP);
1362 else
1363 num.uq = __strtoull_internal (wp, &tw, base, flags & GROUP);
1365 else
1367 if (number_signed)
1368 num.l = __strtol_internal (wp, &tw, base, flags & GROUP);
1369 else
1370 num.ul = __strtoul_internal (wp, &tw, base, flags & GROUP);
1372 if (wp == tw)
1373 conv_error ();
1375 if (!(flags & SUPPRESS))
1377 if (! number_signed)
1379 if (need_longlong && (flags & LONGDBL))
1380 *ARG (unsigned LONGLONG int *) = num.uq;
1381 else if (need_long && (flags & LONG))
1382 *ARG (unsigned long int *) = num.ul;
1383 else if (flags & SHORT)
1384 *ARG (unsigned short int *)
1385 = (unsigned short int) num.ul;
1386 else if (!(flags & CHAR))
1387 *ARG (unsigned int *) = (unsigned int) num.ul;
1388 else
1389 *ARG (unsigned char *) = (unsigned char) num.ul;
1391 else
1393 if (need_longlong && (flags & LONGDBL))
1394 *ARG (LONGLONG int *) = num.q;
1395 else if (need_long && (flags & LONG))
1396 *ARG (long int *) = num.l;
1397 else if (flags & SHORT)
1398 *ARG (short int *) = (short int) num.l;
1399 else if (!(flags & CHAR))
1400 *ARG (int *) = (int) num.l;
1401 else
1402 *ARG (signed char *) = (signed char) num.ul;
1404 ++done;
1406 break;
1408 case L_('e'): /* Floating-point numbers. */
1409 case L_('E'):
1410 case L_('f'):
1411 case L_('g'):
1412 case L_('G'):
1413 case L_('a'):
1414 case L_('A'):
1415 c = inchar ();
1416 if (c == EOF)
1417 input_error ();
1419 /* Check for a sign. */
1420 if (c == L_('-') || c == L_('+'))
1422 negative = c == L_('-');
1423 if (width == 0 || inchar () == EOF)
1424 /* EOF is only an input error before we read any chars. */
1425 conv_error ();
1426 if (! ISDIGIT (c) && c != decimal)
1428 /* This is no valid number. */
1429 ungetc (c, s);
1430 input_error ();
1432 if (width > 0)
1433 --width;
1435 else
1436 negative = 0;
1438 /* Take care for the special arguments "nan" and "inf". */
1439 if (TOLOWER (c) == L_('n'))
1441 /* Maybe "nan". */
1442 ADDW (c);
1443 if (width == 0 || inchar () == EOF || TOLOWER (c) != L_('a'))
1444 input_error ();
1445 if (width > 0)
1446 --width;
1447 ADDW (c);
1448 if (width == 0 || inchar () == EOF || TOLOWER (c) != L_('n'))
1449 input_error ();
1450 if (width > 0)
1451 --width;
1452 ADDW (c);
1453 /* It is "nan". */
1454 goto scan_float;
1456 else if (TOLOWER (c) == L_('i'))
1458 /* Maybe "inf" or "infinity". */
1459 ADDW (c);
1460 if (width == 0 || inchar () == EOF || TOLOWER (c) != L_('n'))
1461 input_error ();
1462 if (width > 0)
1463 --width;
1464 ADDW (c);
1465 if (width == 0 || inchar () == EOF || TOLOWER (c) != L_('f'))
1466 input_error ();
1467 if (width > 0)
1468 --width;
1469 ADDW (c);
1470 /* It is as least "inf". */
1471 if (width != 0 && inchar () != EOF)
1473 if (TOLOWER (c) == L_('i'))
1475 if (width > 0)
1476 --width;
1477 /* Now we have to read the rest as well. */
1478 ADDW (c);
1479 if (width == 0 || inchar () == EOF
1480 || TOLOWER (c) != L_('n'))
1481 input_error ();
1482 if (width > 0)
1483 --width;
1484 ADDW (c);
1485 if (width == 0 || inchar () == EOF
1486 || TOLOWER (c) != L_('i'))
1487 input_error ();
1488 if (width > 0)
1489 --width;
1490 ADDW (c);
1491 if (width == 0 || inchar () == EOF
1492 || TOLOWER (c) != L_('t'))
1493 input_error ();
1494 if (width > 0)
1495 --width;
1496 ADDW (c);
1497 if (width == 0 || inchar () == EOF
1498 || TOLOWER (c) != L_('y'))
1499 input_error ();
1500 if (width > 0)
1501 --width;
1502 ADDW (c);
1504 else
1505 /* Never mind. */
1506 ungetc (c, s);
1508 goto scan_float;
1511 is_hexa = 0;
1512 exp_char = L_('e');
1513 if (width != 0 && c == L_('0'))
1515 ADDW (c);
1516 c = inchar ();
1517 if (width > 0)
1518 --width;
1519 if (width != 0 && TOLOWER (c) == L_('x'))
1521 /* It is a number in hexadecimal format. */
1522 ADDW (c);
1524 is_hexa = 1;
1525 exp_char = L_('p');
1527 /* Grouping is not allowed. */
1528 flags &= ~GROUP;
1529 c = inchar ();
1530 if (width > 0)
1531 --width;
1535 got_dot = got_e = 0;
1538 if (ISDIGIT (c))
1539 ADDW (c);
1540 else if (!got_e && is_hexa && ISXDIGIT (c))
1541 ADDW (c);
1542 else if (got_e && wp[wpsize - 1] == exp_char
1543 && (c == L_('-') || c == L_('+')))
1544 ADDW (c);
1545 else if (wpsize > 0 && !got_e && TOLOWER (c) == exp_char)
1547 ADDW (exp_char);
1548 got_e = got_dot = 1;
1550 else if (c == decimal && !got_dot)
1552 ADDW (c);
1553 got_dot = 1;
1555 else if ((flags & GROUP) && c == thousands && !got_dot)
1556 ADDW (c);
1557 else
1559 /* The last read character is not part of the number
1560 anymore. */
1561 ungetc (c, s);
1562 break;
1564 if (width > 0)
1565 --width;
1567 while (width != 0 && inchar () != EOF);
1569 /* Have we read any character? If we try to read a number
1570 in hexadecimal notation and we have read only the `0x'
1571 prefix or no exponent this is an error. */
1572 if (wpsize == 0 || (is_hexa && (wpsize == 2 || ! got_e)))
1573 conv_error ();
1575 scan_float:
1576 /* Convert the number. */
1577 ADDW (L_('\0'));
1578 if (flags & LONGDBL)
1580 long double d = __strtold_internal (wp, &tw, flags & GROUP);
1581 if (!(flags & SUPPRESS) && tw != wp)
1582 *ARG (long double *) = negative ? -d : d;
1584 else if (flags & LONG)
1586 double d = __strtod_internal (wp, &tw, flags & GROUP);
1587 if (!(flags & SUPPRESS) && tw != wp)
1588 *ARG (double *) = negative ? -d : d;
1590 else
1592 float d = __strtof_internal (wp, &tw, flags & GROUP);
1593 if (!(flags & SUPPRESS) && tw != wp)
1594 *ARG (float *) = negative ? -d : d;
1597 if (tw == wp)
1598 conv_error ();
1600 if (!(flags & SUPPRESS))
1601 ++done;
1602 break;
1604 case L_('['): /* Character class. */
1605 if (flags & LONG)
1606 STRING_ARG (wstr, wchar_t);
1607 else
1608 STRING_ARG (str, char);
1610 if (*f == L_('^'))
1612 ++f;
1613 not_in = 1;
1615 else
1616 not_in = 0;
1618 if (width < 0)
1619 /* There is no width given so there is also no limit on the
1620 number of characters we read. Therefore we set width to
1621 a very high value to make the algorithm easier. */
1622 width = INT_MAX;
1624 #ifdef COMPILE_WPRINTF
1625 /* Find the beginning and the end of the scanlist. We are not
1626 creating a lookup table since it would have to be too large.
1627 Instead we search each time through the string. This is not
1628 a constant lookup time but who uses this feature deserves to
1629 be punished. */
1630 tw = (wchar_t *) f; /* Marks the beginning. */
1632 if (*f == ']' || *f == '-')
1633 ++f;
1635 while ((fc = *f++) != L'\0' && fc != L']');
1637 if (fc == L'\0')
1638 conv_error ();
1639 wp = (wchar_t *) f - 1;
1640 #else
1641 /* Fill WP with byte flags indexed by character.
1642 We will use this flag map for matching input characters. */
1643 if (wpmax < UCHAR_MAX)
1645 wpmax = UCHAR_MAX;
1646 wp = (char *) alloca (wpmax);
1648 memset (wp, '\0', UCHAR_MAX);
1650 fc = *f;
1651 if (fc == ']' || fc == '-')
1653 /* If ] or - appears before any char in the set, it is not
1654 the terminator or separator, but the first char in the
1655 set. */
1656 wp[fc] = 1;
1657 ++f;
1660 while ((fc = *f++) != '\0' && fc != ']')
1661 if (fc == '-' && *f != '\0' && *f != ']'
1662 && (unsigned char) f[-2] <= (unsigned char) *f)
1664 /* Add all characters from the one before the '-'
1665 up to (but not including) the next format char. */
1666 for (fc = f[-2]; fc < *f; ++fc)
1667 wp[fc] = 1;
1669 else
1670 /* Add the character to the flag map. */
1671 wp[fc] = 1;
1673 if (fc == '\0')
1674 conv_error();
1675 #endif
1677 if (flags & LONG)
1679 size_t now = read_in;
1680 #ifdef COMPILE_WPRINTF
1683 wchar_t *runp;
1685 if (inchar () == WEOF)
1686 break;
1688 /* Test whether it's in the scanlist. */
1689 runp = tw;
1690 while (runp < wp)
1692 if (runp[0] == L'-' && runp[1] != '\0' && runp[1] != ']'
1693 && runp != tw
1694 && (unsigned int) runp[-1] <= (unsigned int) runp[1])
1696 /* Match against all characters in between the
1697 first and last character of the sequence. */
1698 wchar_t wc;
1700 for (wc = runp[-1] + 1; wc < runp[1]; ++wc)
1701 if (wc == c)
1702 break;
1704 if (wc == runp[1] && !not_in)
1705 break;
1706 if (wc == runp[1] && not_in)
1708 /* The current character is not in the
1709 scanset. */
1710 ungetwc (c, s);
1711 goto out;
1714 else
1716 if (*runp == runp[1] && !not_in)
1717 break;
1718 if (*runp != runp[1] && not_in)
1720 ungetwc (c ,s);
1721 goto out;
1725 ++runp;
1728 if (!(flags & SUPPRESS))
1730 *wstr++ = c;
1732 if ((flags & MALLOC)
1733 && wstr == (wchar_t *) *strptr + strsize)
1735 /* Enlarge the buffer. */
1736 wstr = (wchar_t *) realloc (*strptr,
1737 (2 * strsize)
1738 * sizeof (wchar_t));
1739 if (wstr == NULL)
1741 /* Can't allocate that much. Last-ditch
1742 effort. */
1743 wstr = (wchar_t *)
1744 realloc (*strptr, (strsize
1745 + sizeof (wchar_t)));
1746 if (wstr == NULL)
1748 /* We lose. Oh well. Terminate the string
1749 and stop converting, so at least we don't
1750 skip any input. */
1751 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
1752 ++done;
1753 conv_error ();
1755 else
1757 *strptr = (char *) wstr;
1758 wstr += strsize;
1759 ++strsize;
1762 else
1764 *strptr = (char *) wstr;
1765 wstr += strsize;
1766 strsize *= 2;
1771 while (--width > 0);
1772 out:
1773 #else
1774 char buf[MB_LEN_MAX];
1775 size_t cnt = 0;
1776 mbstate_t cstate;
1778 memset (&cstate, '\0', sizeof (cstate));
1782 again:
1783 if (inchar () == EOF)
1784 break;
1786 if (wp[c] == not_in)
1788 ungetc (c, s);
1789 break;
1792 /* This is easy. */
1793 if (!(flags & SUPPRESS))
1795 size_t n;
1797 /* Convert it into a wide character. */
1798 n = __mbrtowc (wstr, buf, cnt, &cstate);
1800 if (n == (size_t) -2)
1802 /* Possibly correct character, just not enough
1803 input. */
1804 assert (cnt < MB_CUR_MAX);
1805 goto again;
1808 if (n != cnt)
1809 encode_error ();
1811 ++wstr;
1812 if ((flags & MALLOC)
1813 && wstr == (wchar_t *) *strptr + strsize)
1815 /* Enlarge the buffer. */
1816 wstr = (wchar_t *) realloc (*strptr,
1817 (2 * strsize
1818 * sizeof (wchar_t)));
1819 if (wstr == NULL)
1821 /* Can't allocate that much. Last-ditch
1822 effort. */
1823 wstr = (wchar_t *)
1824 realloc (*strptr, ((strsize + 1)
1825 * sizeof (wchar_t)));
1826 if (wstr == NULL)
1828 /* We lose. Oh well. Terminate the
1829 string and stop converting,
1830 so at least we don't skip any input. */
1831 ((wchar_t *) (*strptr))[strsize - 1] = L'\0';
1832 ++done;
1833 conv_error ();
1835 else
1837 *strptr = (char *) wstr;
1838 wstr += strsize;
1839 ++strsize;
1842 else
1844 *strptr = (char *) wstr;
1845 wstr += strsize;
1846 strsize *= 2;
1851 while (--width > 0);
1853 if (cnt != 0)
1854 /* We stopped in the middle of recognizing another
1855 character. That's a problem. */
1856 encode_error ();
1857 #endif
1859 if (now == read_in)
1860 /* We haven't succesfully read any character. */
1861 conv_error ();
1863 if (!(flags & SUPPRESS))
1865 *wstr++ = L'\0';
1867 if ((flags & MALLOC)
1868 && wstr - (wchar_t *) *strptr != strsize)
1870 wchar_t *cp = (wchar_t *)
1871 realloc (*strptr, ((wstr - (wchar_t *) *strptr)
1872 * sizeof(wchar_t)));
1873 if (cp != NULL)
1874 *strptr = (char *) cp;
1877 ++done;
1880 else
1882 size_t now = read_in;
1883 #ifdef COMPILE_WPRINTF
1885 memset (&state, '\0', sizeof (state));
1889 wchar_t *runp;
1890 size_t n;
1892 if (inchar () == WEOF)
1893 break;
1895 /* Test whether it's in the scanlist. */
1896 runp = tw;
1897 while (runp < wp)
1899 if (runp[0] == L'-' && runp[1] != '\0' && runp[1] != ']'
1900 && runp != tw
1901 && (unsigned int) runp[-1] <= (unsigned int) runp[1])
1903 /* Match against all characters in between the
1904 first and last character of the sequence. */
1905 wchar_t wc;
1907 for (wc = runp[-1] + 1; wc < runp[1]; ++wc)
1908 if (wc == c)
1909 break;
1911 if (wc == runp[1] && !not_in)
1912 break;
1913 if (wc == runp[1] && not_in)
1915 /* The current character is not in the
1916 scanset. */
1917 ungetwc (c, s);
1918 goto out2;
1921 else
1923 if (*runp == runp[1] && !not_in)
1924 break;
1925 if (*runp != runp[1] && not_in)
1927 ungetwc (c ,s);
1928 goto out2;
1932 ++runp;
1935 if (!(flags & SUPPRESS))
1937 if ((flags & MALLOC)
1938 && str + MB_CUR_MAX >= *strptr + strsize)
1940 /* Enlarge the buffer. */
1941 str = (char *) realloc (*strptr, 2 * strsize);
1942 if (str == NULL)
1944 /* Can't allocate that much. Last-ditch
1945 effort. */
1946 str = (char *) realloc (*strptr, strsize + 1);
1947 if (str == NULL)
1949 /* We lose. Oh well. Terminate the string
1950 and stop converting, so at least we don't
1951 skip any input. */
1952 (*strptr)[strsize - 1] = '\0';
1953 ++done;
1954 conv_error ();
1956 else
1958 *strptr = str;
1959 str += strsize;
1960 ++strsize;
1963 else
1965 *strptr = str;
1966 str += strsize;
1967 strsize *= 2;
1972 n = wcrtomb (!(flags & SUPPRESS) ? str : NULL, c, &state);
1973 if (n == (size_t) -1)
1974 encode_error ();
1976 assert (n <= MB_CUR_MAX);
1977 str += n;
1979 while (--width > 0);
1980 out2:
1981 #else
1984 if (inchar () == EOF)
1985 break;
1987 if (wp[c] == not_in)
1989 ungetc (c, s);
1990 break;
1993 /* This is easy. */
1994 if (!(flags & SUPPRESS))
1996 *str++ = c;
1997 if ((flags & MALLOC)
1998 && (char *) str == *strptr + strsize)
2000 /* Enlarge the buffer. */
2001 str = (char *) realloc (*strptr, 2 * strsize);
2002 if (str == NULL)
2004 /* Can't allocate that much. Last-ditch
2005 effort. */
2006 str = (char *) realloc (*strptr, strsize + 1);
2007 if (str == NULL)
2009 /* We lose. Oh well. Terminate the
2010 string and stop converting,
2011 so at least we don't skip any input. */
2012 ((char *) (*strptr))[strsize - 1] = '\0';
2013 ++done;
2014 conv_error ();
2016 else
2018 *strptr = (char *) str;
2019 str += strsize;
2020 ++strsize;
2023 else
2025 *strptr = (char *) str;
2026 str += strsize;
2027 strsize *= 2;
2032 while (--width > 0);
2033 #endif
2035 if (now == read_in)
2036 /* We haven't succesfully read any character. */
2037 conv_error ();
2039 if (!(flags & SUPPRESS))
2041 #ifdef COMPILE_WPRINTF
2042 /* We have to emit the code to get into the intial
2043 state. */
2044 char buf[MB_LEN_MAX];
2045 size_t n = wcrtomb (buf, L'\0', &state);
2046 if (n > 0 && (flags & MALLOC)
2047 && str + n >= *strptr + strsize)
2049 /* Enlarge the buffer. */
2050 str = (char *) realloc (*strptr,
2051 (str + n + 1) - *strptr);
2052 if (str == NULL)
2054 /* We lose. Oh well. Terminate the string
2055 and stop converting, so at least we don't
2056 skip any input. */
2057 ((char *) (*strptr))[strsize - 1] = '\0';
2058 ++done;
2059 conv_error ();
2061 else
2063 *strptr = (char *) str;
2064 str = ((char *) *strptr) + strsize;
2065 strsize = (str + n + 1) - *strptr;
2069 str = __mempcpy (str, buf, n);
2070 #endif
2071 *str = '\0';
2073 if ((flags & MALLOC) && str - *strptr != strsize)
2075 char *cp = (char *) realloc (*strptr, str - *strptr);
2076 if (cp != NULL)
2077 *strptr = cp;
2080 ++done;
2083 break;
2085 case L_('p'): /* Generic pointer. */
2086 base = 16;
2087 /* A PTR must be the same size as a `long int'. */
2088 flags &= ~(SHORT|LONGDBL);
2089 if (need_long)
2090 flags |= LONG;
2091 number_signed = 0;
2092 read_pointer = 1;
2093 goto number;
2095 default:
2096 /* If this is an unknown format character punt. */
2097 conv_error ();
2101 /* The last thing we saw int the format string was a white space.
2102 Consume the last white spaces. */
2103 if (skip_space)
2106 c = inchar ();
2107 while (ISSPACE (c));
2108 ungetc (c, s);
2111 /* Unlock stream. */
2112 UNLOCK_STREAM (s);
2114 return done;
2117 #ifdef USE_IN_LIBIO
2118 # ifdef COMPILE_WPRINTF
2120 __vfwscanf (FILE *s, const wchar_t *format, va_list argptr)
2122 return _IO_vfwscanf (s, format, argptr, NULL);
2124 # else
2126 __vfscanf (FILE *s, const char *format, va_list argptr)
2128 return _IO_vfscanf (s, format, argptr, NULL);
2130 # endif
2131 #endif
2133 #ifdef COMPILE_WPRINTF
2134 weak_alias (__vfwscanf, vfwscanf)
2135 #else
2136 weak_alias (__vfscanf, vfscanf)
2137 #endif