Fix tst-sscanf and tst-swscanf on 64-bit.
[jlayton/glibc.git] / stdio-common / vfprintf.c
1 /* Copyright (C) 1991-2014 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
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.
8
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.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <http://www.gnu.org/licenses/>.  */
17
18 #include <ctype.h>
19 #include <limits.h>
20 #include <printf.h>
21 #include <stdarg.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <wchar.h>
27 #include <bits/libc-lock.h>
28 #include <sys/param.h>
29 #include <_itoa.h>
30 #include <locale/localeinfo.h>
31 #include <stdio.h>
32
33 /* This code is shared between the standard stdio implementation found
34    in GNU C library and the libio implementation originally found in
35    GNU libg++.
36
37    Beside this it is also shared between the normal and wide character
38    implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995.  */
39
40
41 #include <libioP.h>
42 #define FILE            _IO_FILE
43 #undef va_list
44 #define va_list _IO_va_list
45 #undef BUFSIZ
46 #define BUFSIZ          _IO_BUFSIZ
47 #define ARGCHECK(S, Format) \
48   do                                                                          \
49     {                                                                         \
50       /* Check file argument for consistence.  */                             \
51       CHECK_FILE (S, -1);                                                     \
52       if (S->_flags & _IO_NO_WRITES)                                          \
53         {                                                                     \
54           S->_flags |= _IO_ERR_SEEN;                                          \
55           __set_errno (EBADF);                                                \
56           return -1;                                                          \
57         }                                                                     \
58       if (Format == NULL)                                                     \
59         {                                                                     \
60           MAYBE_SET_EINVAL;                                                   \
61           return -1;                                                          \
62         }                                                                     \
63     } while (0)
64 #define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
65
66 #define done_add(val) \
67   do {                                                                        \
68     unsigned int _val = val;                                                  \
69     assert ((unsigned int) done < (unsigned int) INT_MAX);                    \
70     if (__glibc_unlikely (INT_MAX - done < _val))                             \
71       {                                                                       \
72         done = -1;                                                            \
73          __set_errno (EOVERFLOW);                                             \
74         goto all_done;                                                        \
75       }                                                                       \
76     done += _val;                                                             \
77   } while (0)
78
79 #ifndef COMPILE_WPRINTF
80 # define vfprintf       _IO_vfprintf_internal
81 # define CHAR_T         char
82 # define UCHAR_T        unsigned char
83 # define INT_T          int
84 # define L_(Str)        Str
85 # define ISDIGIT(Ch)    ((unsigned int) ((Ch) - '0') < 10)
86 # define STR_LEN(Str)   strlen (Str)
87
88 # define PUT(F, S, N)   _IO_sputn ((F), (S), (N))
89 # define PAD(Padchar) \
90   do {                                                                        \
91     if (width > 0)                                                            \
92       {                                                                       \
93         _IO_ssize_t written = _IO_padn (s, (Padchar), width);                 \
94         if (__glibc_unlikely (written != width))                              \
95           {                                                                   \
96             done = -1;                                                        \
97             goto all_done;                                                    \
98           }                                                                   \
99         done_add (written);                                                   \
100       }                                                                       \
101   } while (0)
102 # define PUTC(C, F)     _IO_putc_unlocked (C, F)
103 # define ORIENT         if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
104                           return -1
105 #else
106 # define vfprintf       _IO_vfwprintf
107 # define CHAR_T         wchar_t
108 /* This is a hack!!!  There should be a type uwchar_t.  */
109 # define UCHAR_T        unsigned int /* uwchar_t */
110 # define INT_T          wint_t
111 # define L_(Str)        L##Str
112 # define ISDIGIT(Ch)    ((unsigned int) ((Ch) - L'0') < 10)
113 # define STR_LEN(Str)   __wcslen (Str)
114
115 # include <_itowa.h>
116
117 # define PUT(F, S, N)   _IO_sputn ((F), (S), (N))
118 # define PAD(Padchar) \
119   do {                                                                        \
120     if (width > 0)                                                            \
121       {                                                                       \
122         _IO_ssize_t written = _IO_wpadn (s, (Padchar), width);                \
123         if (__glibc_unlikely (written != width))                              \
124           {                                                                   \
125             done = -1;                                                        \
126             goto all_done;                                                    \
127           }                                                                   \
128         done_add (written);                                                   \
129       }                                                                       \
130   } while (0)
131 # define PUTC(C, F)     _IO_putwc_unlocked (C, F)
132 # define ORIENT         if (_IO_fwide (s, 1) != 1) return -1
133
134 # undef _itoa
135 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
136 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
137 # undef EOF
138 # define EOF WEOF
139 #endif
140
141 #include "_i18n_number.h"
142
143 /* Include the shared code for parsing the format string.  */
144 #include "printf-parse.h"
145
146
147 #define outchar(Ch)                                                           \
148   do                                                                          \
149     {                                                                         \
150       const INT_T outc = (Ch);                                                \
151       if (PUTC (outc, s) == EOF || done == INT_MAX)                           \
152         {                                                                     \
153           done = -1;                                                          \
154           goto all_done;                                                      \
155         }                                                                     \
156       ++done;                                                                 \
157     }                                                                         \
158   while (0)
159
160 #define outstring(String, Len)                                                \
161   do                                                                          \
162     {                                                                         \
163       assert ((size_t) done <= (size_t) INT_MAX);                             \
164       if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len))                \
165         {                                                                     \
166           done = -1;                                                          \
167           goto all_done;                                                      \
168         }                                                                     \
169       if (__glibc_unlikely (INT_MAX - done < (Len)))                          \
170       {                                                                       \
171         done = -1;                                                            \
172          __set_errno (EOVERFLOW);                                             \
173         goto all_done;                                                        \
174       }                                                                       \
175       done += (Len);                                                          \
176     }                                                                         \
177   while (0)
178
179 /* For handling long_double and longlong we use the same flag.  If
180    `long' and `long long' are effectively the same type define it to
181    zero.  */
182 #if LONG_MAX == LONG_LONG_MAX
183 # define is_longlong 0
184 #else
185 # define is_longlong is_long_double
186 #endif
187
188 /* If `long' and `int' is effectively the same type we don't have to
189    handle `long separately.  */
190 #if INT_MAX == LONG_MAX
191 # define is_long_num    0
192 #else
193 # define is_long_num    is_long
194 #endif
195
196
197 /* Global variables.  */
198 static const CHAR_T null[] = L_("(null)");
199
200
201 /* Helper function to provide temporary buffering for unbuffered streams.  */
202 static int buffered_vfprintf (FILE *stream, const CHAR_T *fmt, va_list)
203      __THROW __attribute__ ((noinline)) internal_function;
204
205 /* Handle unknown format specifier.  */
206 static int printf_unknown (FILE *, const struct printf_info *,
207                            const void *const *) __THROW;
208
209 /* Group digits of number string.  */
210 #ifdef COMPILE_WPRINTF
211 static CHAR_T *group_number (CHAR_T *, CHAR_T *, const char *, wchar_t)
212      __THROW internal_function;
213 #else
214 static CHAR_T *group_number (CHAR_T *, CHAR_T *, const char *, const char *)
215      __THROW internal_function;
216 #endif
217
218
219 /* The function itself.  */
220 int
221 vfprintf (FILE *s, const CHAR_T *format, va_list ap)
222 {
223   /* The character used as thousands separator.  */
224 #ifdef COMPILE_WPRINTF
225   wchar_t thousands_sep = L'\0';
226 #else
227   const char *thousands_sep = NULL;
228 #endif
229
230   /* The string describing the size of groups of digits.  */
231   const char *grouping;
232
233   /* Place to accumulate the result.  */
234   int done;
235
236   /* Current character in format string.  */
237   const UCHAR_T *f;
238
239   /* End of leading constant string.  */
240   const UCHAR_T *lead_str_end;
241
242   /* Points to next format specifier.  */
243   const UCHAR_T *end_of_spec;
244
245   /* Buffer intermediate results.  */
246   CHAR_T work_buffer[1000];
247   CHAR_T *workstart = NULL;
248   CHAR_T *workend;
249
250   /* We have to save the original argument pointer.  */
251   va_list ap_save;
252
253   /* Count number of specifiers we already processed.  */
254   int nspecs_done;
255
256   /* For the %m format we may need the current `errno' value.  */
257   int save_errno = errno;
258
259   /* 1 if format is in read-only memory, -1 if it is in writable memory,
260      0 if unknown.  */
261   int readonly_format = 0;
262
263   /* For the argument descriptions, which may be allocated on the heap.  */
264   void *args_malloced = NULL;
265
266   /* This table maps a character into a number representing a
267      class.  In each step there is a destination label for each
268      class.  */
269   static const uint8_t jump_table[] =
270   {
271     /* ' ' */  1,            0,            0, /* '#' */  4,
272                0, /* '%' */ 14,            0, /* '\''*/  6,
273                0,            0, /* '*' */  7, /* '+' */  2,
274                0, /* '-' */  3, /* '.' */  9,            0,
275     /* '0' */  5, /* '1' */  8, /* '2' */  8, /* '3' */  8,
276     /* '4' */  8, /* '5' */  8, /* '6' */  8, /* '7' */  8,
277     /* '8' */  8, /* '9' */  8,            0,            0,
278                0,            0,            0,            0,
279                0, /* 'A' */ 26,            0, /* 'C' */ 25,
280                0, /* 'E' */ 19, /* F */   19, /* 'G' */ 19,
281                0, /* 'I' */ 29,            0,            0,
282     /* 'L' */ 12,            0,            0,            0,
283                0,            0,            0, /* 'S' */ 21,
284                0,            0,            0,            0,
285     /* 'X' */ 18,            0, /* 'Z' */ 13,            0,
286                0,            0,            0,            0,
287                0, /* 'a' */ 26,            0, /* 'c' */ 20,
288     /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
289     /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28,            0,
290     /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
291     /* 'p' */ 22, /* 'q' */ 12,            0, /* 's' */ 21,
292     /* 't' */ 27, /* 'u' */ 16,            0,            0,
293     /* 'x' */ 18,            0, /* 'z' */ 13
294   };
295
296 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
297 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
298 #ifdef SHARED
299   /* 'int' is enough and it saves some space on 64 bit systems.  */
300 # define JUMP_TABLE_TYPE const int
301 # define JUMP(ChExpr, table)                                                  \
302       do                                                                      \
303         {                                                                     \
304           int offset;                                                         \
305           void *ptr;                                                          \
306           spec = (ChExpr);                                                    \
307           offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown)              \
308             : table[CHAR_CLASS (spec)];                                       \
309           ptr = &&do_form_unknown + offset;                                   \
310           goto *ptr;                                                          \
311         }                                                                     \
312       while (0)
313 #else
314 # define JUMP_TABLE_TYPE const void *const
315 # define JUMP(ChExpr, table)                                                  \
316       do                                                                      \
317         {                                                                     \
318           const void *ptr;                                                    \
319           spec = (ChExpr);                                                    \
320           ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown)                 \
321             : table[CHAR_CLASS (spec)];                                       \
322           goto *ptr;                                                          \
323         }                                                                     \
324       while (0)
325 #endif
326
327 #define STEP0_3_TABLE                                                         \
328     /* Step 0: at the beginning.  */                                          \
329     static JUMP_TABLE_TYPE step0_jumps[30] =                                  \
330     {                                                                         \
331       REF (form_unknown),                                                     \
332       REF (flag_space),         /* for ' ' */                                 \
333       REF (flag_plus),          /* for '+' */                                 \
334       REF (flag_minus),         /* for '-' */                                 \
335       REF (flag_hash),          /* for '<hash>' */                            \
336       REF (flag_zero),          /* for '0' */                                 \
337       REF (flag_quote),         /* for '\'' */                                \
338       REF (width_asterics),     /* for '*' */                                 \
339       REF (width),              /* for '1'...'9' */                           \
340       REF (precision),          /* for '.' */                                 \
341       REF (mod_half),           /* for 'h' */                                 \
342       REF (mod_long),           /* for 'l' */                                 \
343       REF (mod_longlong),       /* for 'L', 'q' */                            \
344       REF (mod_size_t),         /* for 'z', 'Z' */                            \
345       REF (form_percent),       /* for '%' */                                 \
346       REF (form_integer),       /* for 'd', 'i' */                            \
347       REF (form_unsigned),      /* for 'u' */                                 \
348       REF (form_octal),         /* for 'o' */                                 \
349       REF (form_hexa),          /* for 'X', 'x' */                            \
350       REF (form_float),         /* for 'E', 'e', 'F', 'f', 'G', 'g' */        \
351       REF (form_character),     /* for 'c' */                                 \
352       REF (form_string),        /* for 's', 'S' */                            \
353       REF (form_pointer),       /* for 'p' */                                 \
354       REF (form_number),        /* for 'n' */                                 \
355       REF (form_strerror),      /* for 'm' */                                 \
356       REF (form_wcharacter),    /* for 'C' */                                 \
357       REF (form_floathex),      /* for 'A', 'a' */                            \
358       REF (mod_ptrdiff_t),      /* for 't' */                                 \
359       REF (mod_intmax_t),       /* for 'j' */                                 \
360       REF (flag_i18n),          /* for 'I' */                                 \
361     };                                                                        \
362     /* Step 1: after processing width.  */                                    \
363     static JUMP_TABLE_TYPE step1_jumps[30] =                                  \
364     {                                                                         \
365       REF (form_unknown),                                                     \
366       REF (form_unknown),       /* for ' ' */                                 \
367       REF (form_unknown),       /* for '+' */                                 \
368       REF (form_unknown),       /* for '-' */                                 \
369       REF (form_unknown),       /* for '<hash>' */                            \
370       REF (form_unknown),       /* for '0' */                                 \
371       REF (form_unknown),       /* for '\'' */                                \
372       REF (form_unknown),       /* for '*' */                                 \
373       REF (form_unknown),       /* for '1'...'9' */                           \
374       REF (precision),          /* for '.' */                                 \
375       REF (mod_half),           /* for 'h' */                                 \
376       REF (mod_long),           /* for 'l' */                                 \
377       REF (mod_longlong),       /* for 'L', 'q' */                            \
378       REF (mod_size_t),         /* for 'z', 'Z' */                            \
379       REF (form_percent),       /* for '%' */                                 \
380       REF (form_integer),       /* for 'd', 'i' */                            \
381       REF (form_unsigned),      /* for 'u' */                                 \
382       REF (form_octal),         /* for 'o' */                                 \
383       REF (form_hexa),          /* for 'X', 'x' */                            \
384       REF (form_float),         /* for 'E', 'e', 'F', 'f', 'G', 'g' */        \
385       REF (form_character),     /* for 'c' */                                 \
386       REF (form_string),        /* for 's', 'S' */                            \
387       REF (form_pointer),       /* for 'p' */                                 \
388       REF (form_number),        /* for 'n' */                                 \
389       REF (form_strerror),      /* for 'm' */                                 \
390       REF (form_wcharacter),    /* for 'C' */                                 \
391       REF (form_floathex),      /* for 'A', 'a' */                            \
392       REF (mod_ptrdiff_t),      /* for 't' */                                 \
393       REF (mod_intmax_t),       /* for 'j' */                                 \
394       REF (form_unknown)        /* for 'I' */                                 \
395     };                                                                        \
396     /* Step 2: after processing precision.  */                                \
397     static JUMP_TABLE_TYPE step2_jumps[30] =                                  \
398     {                                                                         \
399       REF (form_unknown),                                                     \
400       REF (form_unknown),       /* for ' ' */                                 \
401       REF (form_unknown),       /* for '+' */                                 \
402       REF (form_unknown),       /* for '-' */                                 \
403       REF (form_unknown),       /* for '<hash>' */                            \
404       REF (form_unknown),       /* for '0' */                                 \
405       REF (form_unknown),       /* for '\'' */                                \
406       REF (form_unknown),       /* for '*' */                                 \
407       REF (form_unknown),       /* for '1'...'9' */                           \
408       REF (form_unknown),       /* for '.' */                                 \
409       REF (mod_half),           /* for 'h' */                                 \
410       REF (mod_long),           /* for 'l' */                                 \
411       REF (mod_longlong),       /* for 'L', 'q' */                            \
412       REF (mod_size_t),         /* for 'z', 'Z' */                            \
413       REF (form_percent),       /* for '%' */                                 \
414       REF (form_integer),       /* for 'd', 'i' */                            \
415       REF (form_unsigned),      /* for 'u' */                                 \
416       REF (form_octal),         /* for 'o' */                                 \
417       REF (form_hexa),          /* for 'X', 'x' */                            \
418       REF (form_float),         /* for 'E', 'e', 'F', 'f', 'G', 'g' */        \
419       REF (form_character),     /* for 'c' */                                 \
420       REF (form_string),        /* for 's', 'S' */                            \
421       REF (form_pointer),       /* for 'p' */                                 \
422       REF (form_number),        /* for 'n' */                                 \
423       REF (form_strerror),      /* for 'm' */                                 \
424       REF (form_wcharacter),    /* for 'C' */                                 \
425       REF (form_floathex),      /* for 'A', 'a' */                            \
426       REF (mod_ptrdiff_t),      /* for 't' */                                 \
427       REF (mod_intmax_t),       /* for 'j' */                                 \
428       REF (form_unknown)        /* for 'I' */                                 \
429     };                                                                        \
430     /* Step 3a: after processing first 'h' modifier.  */                      \
431     static JUMP_TABLE_TYPE step3a_jumps[30] =                                 \
432     {                                                                         \
433       REF (form_unknown),                                                     \
434       REF (form_unknown),       /* for ' ' */                                 \
435       REF (form_unknown),       /* for '+' */                                 \
436       REF (form_unknown),       /* for '-' */                                 \
437       REF (form_unknown),       /* for '<hash>' */                            \
438       REF (form_unknown),       /* for '0' */                                 \
439       REF (form_unknown),       /* for '\'' */                                \
440       REF (form_unknown),       /* for '*' */                                 \
441       REF (form_unknown),       /* for '1'...'9' */                           \
442       REF (form_unknown),       /* for '.' */                                 \
443       REF (mod_halfhalf),       /* for 'h' */                                 \
444       REF (form_unknown),       /* for 'l' */                                 \
445       REF (form_unknown),       /* for 'L', 'q' */                            \
446       REF (form_unknown),       /* for 'z', 'Z' */                            \
447       REF (form_percent),       /* for '%' */                                 \
448       REF (form_integer),       /* for 'd', 'i' */                            \
449       REF (form_unsigned),      /* for 'u' */                                 \
450       REF (form_octal),         /* for 'o' */                                 \
451       REF (form_hexa),          /* for 'X', 'x' */                            \
452       REF (form_unknown),       /* for 'E', 'e', 'F', 'f', 'G', 'g' */        \
453       REF (form_unknown),       /* for 'c' */                                 \
454       REF (form_unknown),       /* for 's', 'S' */                            \
455       REF (form_unknown),       /* for 'p' */                                 \
456       REF (form_number),        /* for 'n' */                                 \
457       REF (form_unknown),       /* for 'm' */                                 \
458       REF (form_unknown),       /* for 'C' */                                 \
459       REF (form_unknown),       /* for 'A', 'a' */                            \
460       REF (form_unknown),       /* for 't' */                                 \
461       REF (form_unknown),       /* for 'j' */                                 \
462       REF (form_unknown)        /* for 'I' */                                 \
463     };                                                                        \
464     /* Step 3b: after processing first 'l' modifier.  */                      \
465     static JUMP_TABLE_TYPE step3b_jumps[30] =                                 \
466     {                                                                         \
467       REF (form_unknown),                                                     \
468       REF (form_unknown),       /* for ' ' */                                 \
469       REF (form_unknown),       /* for '+' */                                 \
470       REF (form_unknown),       /* for '-' */                                 \
471       REF (form_unknown),       /* for '<hash>' */                            \
472       REF (form_unknown),       /* for '0' */                                 \
473       REF (form_unknown),       /* for '\'' */                                \
474       REF (form_unknown),       /* for '*' */                                 \
475       REF (form_unknown),       /* for '1'...'9' */                           \
476       REF (form_unknown),       /* for '.' */                                 \
477       REF (form_unknown),       /* for 'h' */                                 \
478       REF (mod_longlong),       /* for 'l' */                                 \
479       REF (form_unknown),       /* for 'L', 'q' */                            \
480       REF (form_unknown),       /* for 'z', 'Z' */                            \
481       REF (form_percent),       /* for '%' */                                 \
482       REF (form_integer),       /* for 'd', 'i' */                            \
483       REF (form_unsigned),      /* for 'u' */                                 \
484       REF (form_octal),         /* for 'o' */                                 \
485       REF (form_hexa),          /* for 'X', 'x' */                            \
486       REF (form_float),         /* for 'E', 'e', 'F', 'f', 'G', 'g' */        \
487       REF (form_character),     /* for 'c' */                                 \
488       REF (form_string),        /* for 's', 'S' */                            \
489       REF (form_pointer),       /* for 'p' */                                 \
490       REF (form_number),        /* for 'n' */                                 \
491       REF (form_strerror),      /* for 'm' */                                 \
492       REF (form_wcharacter),    /* for 'C' */                                 \
493       REF (form_floathex),      /* for 'A', 'a' */                            \
494       REF (form_unknown),       /* for 't' */                                 \
495       REF (form_unknown),       /* for 'j' */                                 \
496       REF (form_unknown)        /* for 'I' */                                 \
497     }
498
499 #define STEP4_TABLE                                                           \
500     /* Step 4: processing format specifier.  */                               \
501     static JUMP_TABLE_TYPE step4_jumps[30] =                                  \
502     {                                                                         \
503       REF (form_unknown),                                                     \
504       REF (form_unknown),       /* for ' ' */                                 \
505       REF (form_unknown),       /* for '+' */                                 \
506       REF (form_unknown),       /* for '-' */                                 \
507       REF (form_unknown),       /* for '<hash>' */                            \
508       REF (form_unknown),       /* for '0' */                                 \
509       REF (form_unknown),       /* for '\'' */                                \
510       REF (form_unknown),       /* for '*' */                                 \
511       REF (form_unknown),       /* for '1'...'9' */                           \
512       REF (form_unknown),       /* for '.' */                                 \
513       REF (form_unknown),       /* for 'h' */                                 \
514       REF (form_unknown),       /* for 'l' */                                 \
515       REF (form_unknown),       /* for 'L', 'q' */                            \
516       REF (form_unknown),       /* for 'z', 'Z' */                            \
517       REF (form_percent),       /* for '%' */                                 \
518       REF (form_integer),       /* for 'd', 'i' */                            \
519       REF (form_unsigned),      /* for 'u' */                                 \
520       REF (form_octal),         /* for 'o' */                                 \
521       REF (form_hexa),          /* for 'X', 'x' */                            \
522       REF (form_float),         /* for 'E', 'e', 'F', 'f', 'G', 'g' */        \
523       REF (form_character),     /* for 'c' */                                 \
524       REF (form_string),        /* for 's', 'S' */                            \
525       REF (form_pointer),       /* for 'p' */                                 \
526       REF (form_number),        /* for 'n' */                                 \
527       REF (form_strerror),      /* for 'm' */                                 \
528       REF (form_wcharacter),    /* for 'C' */                                 \
529       REF (form_floathex),      /* for 'A', 'a' */                            \
530       REF (form_unknown),       /* for 't' */                                 \
531       REF (form_unknown),       /* for 'j' */                                 \
532       REF (form_unknown)        /* for 'I' */                                 \
533     }
534
535
536 #define process_arg(fspec)                                                    \
537       /* Start real work.  We know about all flags and modifiers and          \
538          now process the wanted format specifier.  */                         \
539     LABEL (form_percent):                                                     \
540       /* Write a literal "%".  */                                             \
541       outchar (L_('%'));                                                      \
542       break;                                                                  \
543                                                                               \
544     LABEL (form_integer):                                                     \
545       /* Signed decimal integer.  */                                          \
546       base = 10;                                                              \
547                                                                               \
548       if (is_longlong)                                                        \
549         {                                                                     \
550           long long int signed_number;                                        \
551                                                                               \
552           if (fspec == NULL)                                                  \
553             signed_number = va_arg (ap, long long int);                       \
554           else                                                                \
555             signed_number = args_value[fspec->data_arg].pa_long_long_int;     \
556                                                                               \
557           is_negative = signed_number < 0;                                    \
558           number.longlong = is_negative ? (- signed_number) : signed_number;  \
559                                                                               \
560           goto LABEL (longlong_number);                                       \
561         }                                                                     \
562       else                                                                    \
563         {                                                                     \
564           long int signed_number;                                             \
565                                                                               \
566           if (fspec == NULL)                                                  \
567             {                                                                 \
568               if (is_long_num)                                                \
569                 signed_number = va_arg (ap, long int);                        \
570               else if (is_char)                                               \
571                 signed_number = (signed char) va_arg (ap, unsigned int);      \
572               else if (!is_short)                                             \
573                 signed_number = va_arg (ap, int);                             \
574               else                                                            \
575                 signed_number = (short int) va_arg (ap, unsigned int);        \
576             }                                                                 \
577           else                                                                \
578             if (is_long_num)                                                  \
579               signed_number = args_value[fspec->data_arg].pa_long_int;        \
580             else if (is_char)                                                 \
581               signed_number = (signed char)                                   \
582                 args_value[fspec->data_arg].pa_u_int;                         \
583             else if (!is_short)                                               \
584               signed_number = args_value[fspec->data_arg].pa_int;             \
585             else                                                              \
586               signed_number = (short int)                                     \
587                 args_value[fspec->data_arg].pa_u_int;                         \
588                                                                               \
589           is_negative = signed_number < 0;                                    \
590           number.word = is_negative ? (- signed_number) : signed_number;      \
591                                                                               \
592           goto LABEL (number);                                                \
593         }                                                                     \
594       /* NOTREACHED */                                                        \
595                                                                               \
596     LABEL (form_unsigned):                                                    \
597       /* Unsigned decimal integer.  */                                        \
598       base = 10;                                                              \
599       goto LABEL (unsigned_number);                                           \
600       /* NOTREACHED */                                                        \
601                                                                               \
602     LABEL (form_octal):                                                       \
603       /* Unsigned octal integer.  */                                          \
604       base = 8;                                                               \
605       goto LABEL (unsigned_number);                                           \
606       /* NOTREACHED */                                                        \
607                                                                               \
608     LABEL (form_hexa):                                                        \
609       /* Unsigned hexadecimal integer.  */                                    \
610       base = 16;                                                              \
611                                                                               \
612     LABEL (unsigned_number):      /* Unsigned number of base BASE.  */        \
613                                                                               \
614       /* ISO specifies the `+' and ` ' flags only for signed                  \
615          conversions.  */                                                     \
616       is_negative = 0;                                                        \
617       showsign = 0;                                                           \
618       space = 0;                                                              \
619                                                                               \
620       if (is_longlong)                                                        \
621         {                                                                     \
622           if (fspec == NULL)                                                  \
623             number.longlong = va_arg (ap, unsigned long long int);            \
624           else                                                                \
625             number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
626                                                                               \
627         LABEL (longlong_number):                                              \
628           if (prec < 0)                                                       \
629             /* Supply a default precision if none was given.  */              \
630             prec = 1;                                                         \
631           else                                                                \
632             /* We have to take care for the '0' flag.  If a precision         \
633                is given it must be ignored.  */                               \
634             pad = L_(' ');                                                    \
635                                                                               \
636           /* If the precision is 0 and the number is 0 nothing has to         \
637              be written for the number, except for the 'o' format in          \
638              alternate form.  */                                              \
639           if (prec == 0 && number.longlong == 0)                              \
640             {                                                                 \
641               string = workend;                                               \
642               if (base == 8 && alt)                                           \
643                 *--string = L_('0');                                          \
644             }                                                                 \
645           else                                                                \
646             {                                                                 \
647               /* Put the number in WORK.  */                                  \
648               string = _itoa (number.longlong, workend, base,                 \
649                               spec == L_('X'));                               \
650               if (group && grouping)                                          \
651                 string = group_number (string, workend, grouping,             \
652                                        thousands_sep);                        \
653                                                                               \
654               if (use_outdigits && base == 10)                                \
655                 string = _i18n_number_rewrite (string, workend, workend);     \
656             }                                                                 \
657           /* Simplify further test for num != 0.  */                          \
658           number.word = number.longlong != 0;                                 \
659         }                                                                     \
660       else                                                                    \
661         {                                                                     \
662           if (fspec == NULL)                                                  \
663             {                                                                 \
664               if (is_long_num)                                                \
665                 number.word = va_arg (ap, unsigned long int);                 \
666               else if (is_char)                                               \
667                 number.word = (unsigned char) va_arg (ap, unsigned int);      \
668               else if (!is_short)                                             \
669                 number.word = va_arg (ap, unsigned int);                      \
670               else                                                            \
671                 number.word = (unsigned short int) va_arg (ap, unsigned int); \
672             }                                                                 \
673           else                                                                \
674             if (is_long_num)                                                  \
675               number.word = args_value[fspec->data_arg].pa_u_long_int;        \
676             else if (is_char)                                                 \
677               number.word = (unsigned char)                                   \
678                 args_value[fspec->data_arg].pa_u_int;                         \
679             else if (!is_short)                                               \
680               number.word = args_value[fspec->data_arg].pa_u_int;             \
681             else                                                              \
682               number.word = (unsigned short int)                              \
683                 args_value[fspec->data_arg].pa_u_int;                         \
684                                                                               \
685         LABEL (number):                                                       \
686           if (prec < 0)                                                       \
687             /* Supply a default precision if none was given.  */              \
688             prec = 1;                                                         \
689           else                                                                \
690             /* We have to take care for the '0' flag.  If a precision         \
691                is given it must be ignored.  */                               \
692             pad = L_(' ');                                                    \
693                                                                               \
694           /* If the precision is 0 and the number is 0 nothing has to         \
695              be written for the number, except for the 'o' format in          \
696              alternate form.  */                                              \
697           if (prec == 0 && number.word == 0)                                  \
698             {                                                                 \
699               string = workend;                                               \
700               if (base == 8 && alt)                                           \
701                 *--string = L_('0');                                          \
702             }                                                                 \
703           else                                                                \
704             {                                                                 \
705               /* Put the number in WORK.  */                                  \
706               string = _itoa_word (number.word, workend, base,                \
707                                    spec == L_('X'));                          \
708               if (group && grouping)                                          \
709                 string = group_number (string, workend, grouping,             \
710                                        thousands_sep);                        \
711                                                                               \
712               if (use_outdigits && base == 10)                                \
713                 string = _i18n_number_rewrite (string, workend, workend);     \
714             }                                                                 \
715         }                                                                     \
716                                                                               \
717       if (prec <= workend - string && number.word != 0 && alt && base == 8)   \
718         /* Add octal marker.  */                                              \
719         *--string = L_('0');                                                  \
720                                                                               \
721       prec = MAX (0, prec - (workend - string));                              \
722                                                                               \
723       if (!left)                                                              \
724         {                                                                     \
725           width -= workend - string + prec;                                   \
726                                                                               \
727           if (number.word != 0 && alt && base == 16)                          \
728             /* Account for 0X hex marker.  */                                 \
729             width -= 2;                                                       \
730                                                                               \
731           if (is_negative || showsign || space)                               \
732             --width;                                                          \
733                                                                               \
734           if (pad == L_(' '))                                                 \
735             {                                                                 \
736               PAD (L_(' '));                                                  \
737               width = 0;                                                      \
738             }                                                                 \
739                                                                               \
740           if (is_negative)                                                    \
741             outchar (L_('-'));                                                \
742           else if (showsign)                                                  \
743             outchar (L_('+'));                                                \
744           else if (space)                                                     \
745             outchar (L_(' '));                                                \
746                                                                               \
747           if (number.word != 0 && alt && base == 16)                          \
748             {                                                                 \
749               outchar (L_('0'));                                              \
750               outchar (spec);                                                 \
751             }                                                                 \
752                                                                               \
753           width += prec;                                                      \
754           PAD (L_('0'));                                                      \
755                                                                               \
756           outstring (string, workend - string);                               \
757                                                                               \
758           break;                                                              \
759         }                                                                     \
760       else                                                                    \
761         {                                                                     \
762           if (is_negative)                                                    \
763             {                                                                 \
764               outchar (L_('-'));                                              \
765               --width;                                                        \
766             }                                                                 \
767           else if (showsign)                                                  \
768             {                                                                 \
769               outchar (L_('+'));                                              \
770               --width;                                                        \
771             }                                                                 \
772           else if (space)                                                     \
773             {                                                                 \
774               outchar (L_(' '));                                              \
775               --width;                                                        \
776             }                                                                 \
777                                                                               \
778           if (number.word != 0 && alt && base == 16)                          \
779             {                                                                 \
780               outchar (L_('0'));                                              \
781               outchar (spec);                                                 \
782               width -= 2;                                                     \
783             }                                                                 \
784                                                                               \
785           width -= workend - string + prec;                                   \
786                                                                               \
787           if (prec > 0)                                                       \
788             {                                                                 \
789               int temp = width;                                               \
790               width = prec;                                                   \
791               PAD (L_('0'));                                                  \
792               width = temp;                                                   \
793             }                                                                 \
794                                                                               \
795           outstring (string, workend - string);                               \
796                                                                               \
797           PAD (L_(' '));                                                      \
798           break;                                                              \
799         }                                                                     \
800                                                                               \
801     LABEL (form_float):                                                       \
802       {                                                                       \
803         /* Floating-point number.  This is handled by printf_fp.c.  */        \
804         const void *ptr;                                                      \
805         int function_done;                                                    \
806                                                                               \
807         if (fspec == NULL)                                                    \
808           {                                                                   \
809             if (__ldbl_is_dbl)                                                \
810               is_long_double = 0;                                             \
811                                                                               \
812             struct printf_info info = { .prec = prec,                         \
813                                         .width = width,                       \
814                                         .spec = spec,                         \
815                                         .is_long_double = is_long_double,     \
816                                         .is_short = is_short,                 \
817                                         .is_long = is_long,                   \
818                                         .alt = alt,                           \
819                                         .space = space,                       \
820                                         .left = left,                         \
821                                         .showsign = showsign,                 \
822                                         .group = group,                       \
823                                         .pad = pad,                           \
824                                         .extra = 0,                           \
825                                         .i18n = use_outdigits,                \
826                                         .wide = sizeof (CHAR_T) != 1 };       \
827                                                                               \
828             if (is_long_double)                                               \
829               the_arg.pa_long_double = va_arg (ap, long double);              \
830             else                                                              \
831               the_arg.pa_double = va_arg (ap, double);                        \
832             ptr = (const void *) &the_arg;                                    \
833                                                                               \
834             function_done = __printf_fp (s, &info, &ptr);                     \
835           }                                                                   \
836         else                                                                  \
837           {                                                                   \
838             ptr = (const void *) &args_value[fspec->data_arg];                \
839             if (__ldbl_is_dbl)                                                \
840               {                                                               \
841                 fspec->data_arg_type = PA_DOUBLE;                             \
842                 fspec->info.is_long_double = 0;                               \
843               }                                                               \
844                                                                               \
845             function_done = __printf_fp (s, &fspec->info, &ptr);              \
846           }                                                                   \
847                                                                               \
848         if (function_done < 0)                                                \
849           {                                                                   \
850             /* Error in print handler; up to handler to set errno.  */        \
851             done = -1;                                                        \
852             goto all_done;                                                    \
853           }                                                                   \
854                                                                               \
855         done_add (function_done);                                             \
856       }                                                                       \
857       break;                                                                  \
858                                                                               \
859     LABEL (form_floathex):                                                    \
860       {                                                                       \
861         /* Floating point number printed as hexadecimal number.  */           \
862         const void *ptr;                                                      \
863         int function_done;                                                    \
864                                                                               \
865         if (fspec == NULL)                                                    \
866           {                                                                   \
867             if (__ldbl_is_dbl)                                                \
868               is_long_double = 0;                                             \
869                                                                               \
870             struct printf_info info = { .prec = prec,                         \
871                                         .width = width,                       \
872                                         .spec = spec,                         \
873                                         .is_long_double = is_long_double,     \
874                                         .is_short = is_short,                 \
875                                         .is_long = is_long,                   \
876                                         .alt = alt,                           \
877                                         .space = space,                       \
878                                         .left = left,                         \
879                                         .showsign = showsign,                 \
880                                         .group = group,                       \
881                                         .pad = pad,                           \
882                                         .extra = 0,                           \
883                                         .wide = sizeof (CHAR_T) != 1 };       \
884                                                                               \
885             if (is_long_double)                                               \
886               the_arg.pa_long_double = va_arg (ap, long double);              \
887             else                                                              \
888               the_arg.pa_double = va_arg (ap, double);                        \
889             ptr = (const void *) &the_arg;                                    \
890                                                                               \
891             function_done = __printf_fphex (s, &info, &ptr);                  \
892           }                                                                   \
893         else                                                                  \
894           {                                                                   \
895             ptr = (const void *) &args_value[fspec->data_arg];                \
896             if (__ldbl_is_dbl)                                                \
897               fspec->info.is_long_double = 0;                                 \
898                                                                               \
899             function_done = __printf_fphex (s, &fspec->info, &ptr);           \
900           }                                                                   \
901                                                                               \
902         if (function_done < 0)                                                \
903           {                                                                   \
904             /* Error in print handler; up to handler to set errno.  */        \
905             done = -1;                                                        \
906             goto all_done;                                                    \
907           }                                                                   \
908                                                                               \
909         done_add (function_done);                                             \
910       }                                                                       \
911       break;                                                                  \
912                                                                               \
913     LABEL (form_pointer):                                                     \
914       /* Generic pointer.  */                                                 \
915       {                                                                       \
916         const void *ptr;                                                      \
917         if (fspec == NULL)                                                    \
918           ptr = va_arg (ap, void *);                                          \
919         else                                                                  \
920           ptr = args_value[fspec->data_arg].pa_pointer;                       \
921         if (ptr != NULL)                                                      \
922           {                                                                   \
923             /* If the pointer is not NULL, write it as a %#x spec.  */        \
924             base = 16;                                                        \
925             number.word = (unsigned long int) ptr;                            \
926             is_negative = 0;                                                  \
927             alt = 1;                                                          \
928             group = 0;                                                        \
929             spec = L_('x');                                                   \
930             goto LABEL (number);                                              \
931           }                                                                   \
932         else                                                                  \
933           {                                                                   \
934             /* Write "(nil)" for a nil pointer.  */                           \
935             string = (CHAR_T *) L_("(nil)");                                  \
936             /* Make sure the full string "(nil)" is printed.  */              \
937             if (prec < 5)                                                     \
938               prec = 5;                                                       \
939             is_long = 0;        /* This is no wide-char string.  */           \
940             goto LABEL (print_string);                                        \
941           }                                                                   \
942       }                                                                       \
943       /* NOTREACHED */                                                        \
944                                                                               \
945     LABEL (form_number):                                                      \
946       if (s->_flags2 & _IO_FLAGS2_FORTIFY)                                    \
947         {                                                                     \
948           if (! readonly_format)                                              \
949             {                                                                 \
950               extern int __readonly_area (const void *, size_t)               \
951                 attribute_hidden;                                             \
952               readonly_format                                                 \
953                 = __readonly_area (format, ((STR_LEN (format) + 1)            \
954                                             * sizeof (CHAR_T)));              \
955             }                                                                 \
956           if (readonly_format < 0)                                            \
957             __libc_fatal ("*** %n in writable segment detected ***\n");       \
958         }                                                                     \
959       /* Answer the count of characters written.  */                          \
960       if (fspec == NULL)                                                      \
961         {                                                                     \
962           if (is_longlong)                                                    \
963             *(long long int *) va_arg (ap, void *) = done;                    \
964           else if (is_long_num)                                               \
965             *(long int *) va_arg (ap, void *) = done;                         \
966           else if (is_char)                                                   \
967             *(char *) va_arg (ap, void *) = done;                             \
968           else if (!is_short)                                                 \
969             *(int *) va_arg (ap, void *) = done;                              \
970           else                                                                \
971             *(short int *) va_arg (ap, void *) = done;                        \
972         }                                                                     \
973       else                                                                    \
974         if (is_longlong)                                                      \
975           *(long long int *) args_value[fspec->data_arg].pa_pointer = done;   \
976         else if (is_long_num)                                                 \
977           *(long int *) args_value[fspec->data_arg].pa_pointer = done;        \
978         else if (is_char)                                                     \
979           *(char *) args_value[fspec->data_arg].pa_pointer = done;            \
980         else if (!is_short)                                                   \
981           *(int *) args_value[fspec->data_arg].pa_pointer = done;             \
982         else                                                                  \
983           *(short int *) args_value[fspec->data_arg].pa_pointer = done;       \
984       break;                                                                  \
985                                                                               \
986     LABEL (form_strerror):                                                    \
987       /* Print description of error ERRNO.  */                                \
988       string =                                                                \
989         (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer,            \
990                                  sizeof work_buffer);                         \
991       is_long = 0;              /* This is no wide-char string.  */           \
992       goto LABEL (print_string)
993
994 #ifdef COMPILE_WPRINTF
995 # define process_string_arg(fspec) \
996     LABEL (form_character):                                                   \
997       /* Character.  */                                                       \
998       if (is_long)                                                            \
999         goto LABEL (form_wcharacter);                                         \
1000       --width;  /* Account for the character itself.  */                      \
1001       if (!left)                                                              \
1002         PAD (L' ');                                                           \
1003       if (fspec == NULL)                                                      \
1004         outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
1005       else                                                                    \
1006         outchar (__btowc ((unsigned char)                                     \
1007                           args_value[fspec->data_arg].pa_int));               \
1008       if (left)                                                               \
1009         PAD (L' ');                                                           \
1010       break;                                                                  \
1011                                                                               \
1012     LABEL (form_wcharacter):                                                  \
1013       {                                                                       \
1014         /* Wide character.  */                                                \
1015         --width;                                                              \
1016         if (!left)                                                            \
1017           PAD (L' ');                                                         \
1018         if (fspec == NULL)                                                    \
1019           outchar (va_arg (ap, wchar_t));                                     \
1020         else                                                                  \
1021           outchar (args_value[fspec->data_arg].pa_wchar);                     \
1022         if (left)                                                             \
1023           PAD (L' ');                                                         \
1024       }                                                                       \
1025       break;                                                                  \
1026                                                                               \
1027     LABEL (form_string):                                                      \
1028       {                                                                       \
1029         size_t len;                                                           \
1030         int string_malloced;                                                  \
1031                                                                               \
1032         /* The string argument could in fact be `char *' or `wchar_t *'.      \
1033            But this should not make a difference here.  */                    \
1034         if (fspec == NULL)                                                    \
1035           string = (CHAR_T *) va_arg (ap, const wchar_t *);                   \
1036         else                                                                  \
1037           string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring;         \
1038                                                                               \
1039         /* Entry point for printing other strings.  */                        \
1040       LABEL (print_string):                                                   \
1041                                                                               \
1042         string_malloced = 0;                                                  \
1043         if (string == NULL)                                                   \
1044           {                                                                   \
1045             /* Write "(null)" if there's space.  */                           \
1046             if (prec == -1                                                    \
1047                 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1)      \
1048               {                                                               \
1049                 string = (CHAR_T *) null;                                     \
1050                 len = (sizeof (null) / sizeof (null[0])) - 1;                 \
1051               }                                                               \
1052             else                                                              \
1053               {                                                               \
1054                 string = (CHAR_T *) L"";                                      \
1055                 len = 0;                                                      \
1056               }                                                               \
1057           }                                                                   \
1058         else if (!is_long && spec != L_('S'))                                 \
1059           {                                                                   \
1060             /* This is complicated.  We have to transform the multibyte       \
1061                string into a wide character string.  */                       \
1062             const char *mbs = (const char *) string;                          \
1063             mbstate_t mbstate;                                                \
1064                                                                               \
1065             len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
1066                                                                               \
1067             /* Allocate dynamically an array which definitely is long         \
1068                enough for the wide character version.  Each byte in the       \
1069                multi-byte string can produce at most one wide character.  */  \
1070             if (__glibc_unlikely (len > SIZE_MAX / sizeof (wchar_t)))         \
1071               {                                                               \
1072                 __set_errno (EOVERFLOW);                                      \
1073                 done = -1;                                                    \
1074                 goto all_done;                                                \
1075               }                                                               \
1076             else if (__libc_use_alloca (len * sizeof (wchar_t)))              \
1077               string = (CHAR_T *) alloca (len * sizeof (wchar_t));            \
1078             else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t)))    \
1079                      == NULL)                                                 \
1080               {                                                               \
1081                 done = -1;                                                    \
1082                 goto all_done;                                                \
1083               }                                                               \
1084             else                                                              \
1085               string_malloced = 1;                                            \
1086                                                                               \
1087             memset (&mbstate, '\0', sizeof (mbstate_t));                      \
1088             len = __mbsrtowcs (string, &mbs, len, &mbstate);                  \
1089             if (len == (size_t) -1)                                           \
1090               {                                                               \
1091                 /* Illegal multibyte character.  */                           \
1092                 done = -1;                                                    \
1093                 goto all_done;                                                \
1094               }                                                               \
1095           }                                                                   \
1096         else                                                                  \
1097           {                                                                   \
1098             if (prec != -1)                                                   \
1099               /* Search for the end of the string, but don't search past      \
1100                  the length specified by the precision.  */                   \
1101               len = __wcsnlen (string, prec);                                 \
1102             else                                                              \
1103               len = __wcslen (string);                                        \
1104           }                                                                   \
1105                                                                               \
1106         if ((width -= len) < 0)                                               \
1107           {                                                                   \
1108             outstring (string, len);                                          \
1109             break;                                                            \
1110           }                                                                   \
1111                                                                               \
1112         if (!left)                                                            \
1113           PAD (L' ');                                                         \
1114         outstring (string, len);                                              \
1115         if (left)                                                             \
1116           PAD (L' ');                                                         \
1117         if (__glibc_unlikely (string_malloced))                               \
1118           free (string);                                                      \
1119       }                                                                       \
1120       break;
1121 #else
1122 # define process_string_arg(fspec) \
1123     LABEL (form_character):                                                   \
1124       /* Character.  */                                                       \
1125       if (is_long)                                                            \
1126         goto LABEL (form_wcharacter);                                         \
1127       --width;  /* Account for the character itself.  */                      \
1128       if (!left)                                                              \
1129         PAD (' ');                                                            \
1130       if (fspec == NULL)                                                      \
1131         outchar ((unsigned char) va_arg (ap, int)); /* Promoted.  */          \
1132       else                                                                    \
1133         outchar ((unsigned char) args_value[fspec->data_arg].pa_int);         \
1134       if (left)                                                               \
1135         PAD (' ');                                                            \
1136       break;                                                                  \
1137                                                                               \
1138     LABEL (form_wcharacter):                                                  \
1139       {                                                                       \
1140         /* Wide character.  */                                                \
1141         char buf[MB_CUR_MAX];                                                 \
1142         mbstate_t mbstate;                                                    \
1143         size_t len;                                                           \
1144                                                                               \
1145         memset (&mbstate, '\0', sizeof (mbstate_t));                          \
1146         len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t)           \
1147                                : args_value[fspec->data_arg].pa_wchar),       \
1148                          &mbstate);                                           \
1149         if (len == (size_t) -1)                                               \
1150           {                                                                   \
1151             /* Something went wrong during the conversion.  Bail out.  */     \
1152             done = -1;                                                        \
1153             goto all_done;                                                    \
1154           }                                                                   \
1155         width -= len;                                                         \
1156         if (!left)                                                            \
1157           PAD (' ');                                                          \
1158         outstring (buf, len);                                                 \
1159         if (left)                                                             \
1160           PAD (' ');                                                          \
1161       }                                                                       \
1162       break;                                                                  \
1163                                                                               \
1164     LABEL (form_string):                                                      \
1165       {                                                                       \
1166         size_t len;                                                           \
1167         int string_malloced;                                                  \
1168                                                                               \
1169         /* The string argument could in fact be `char *' or `wchar_t *'.      \
1170            But this should not make a difference here.  */                    \
1171         if (fspec == NULL)                                                    \
1172           string = (char *) va_arg (ap, const char *);                        \
1173         else                                                                  \
1174           string = (char *) args_value[fspec->data_arg].pa_string;            \
1175                                                                               \
1176         /* Entry point for printing other strings.  */                        \
1177       LABEL (print_string):                                                   \
1178                                                                               \
1179         string_malloced = 0;                                                  \
1180         if (string == NULL)                                                   \
1181           {                                                                   \
1182             /* Write "(null)" if there's space.  */                           \
1183             if (prec == -1 || prec >= (int) sizeof (null) - 1)                \
1184               {                                                               \
1185                 string = (char *) null;                                       \
1186                 len = sizeof (null) - 1;                                      \
1187               }                                                               \
1188             else                                                              \
1189               {                                                               \
1190                 string = (char *) "";                                         \
1191                 len = 0;                                                      \
1192               }                                                               \
1193           }                                                                   \
1194         else if (!is_long && spec != L_('S'))                                 \
1195           {                                                                   \
1196             if (prec != -1)                                                   \
1197               /* Search for the end of the string, but don't search past      \
1198                  the length (in bytes) specified by the precision.  */        \
1199               len = __strnlen (string, prec);                                 \
1200             else                                                              \
1201               len = strlen (string);                                          \
1202           }                                                                   \
1203         else                                                                  \
1204           {                                                                   \
1205             const wchar_t *s2 = (const wchar_t *) string;                     \
1206             mbstate_t mbstate;                                                \
1207                                                                               \
1208             memset (&mbstate, '\0', sizeof (mbstate_t));                      \
1209                                                                               \
1210             if (prec >= 0)                                                    \
1211               {                                                               \
1212                 /* The string `s2' might not be NUL terminated.  */           \
1213                 if (__libc_use_alloca (prec))                                 \
1214                   string = (char *) alloca (prec);                            \
1215                 else if ((string = (char *) malloc (prec)) == NULL)           \
1216                   {                                                           \
1217                     done = -1;                                                \
1218                     goto all_done;                                            \
1219                   }                                                           \
1220                 else                                                          \
1221                   string_malloced = 1;                                        \
1222                 len = __wcsrtombs (string, &s2, prec, &mbstate);              \
1223               }                                                               \
1224             else                                                              \
1225               {                                                               \
1226                 len = __wcsrtombs (NULL, &s2, 0, &mbstate);                   \
1227                 if (len != (size_t) -1)                                       \
1228                   {                                                           \
1229                     assert (__mbsinit (&mbstate));                            \
1230                     s2 = (const wchar_t *) string;                            \
1231                     if (__libc_use_alloca (len + 1))                          \
1232                       string = (char *) alloca (len + 1);                     \
1233                     else if ((string = (char *) malloc (len + 1)) == NULL)    \
1234                       {                                                       \
1235                         done = -1;                                            \
1236                         goto all_done;                                        \
1237                       }                                                       \
1238                     else                                                      \
1239                       string_malloced = 1;                                    \
1240                     (void) __wcsrtombs (string, &s2, len + 1, &mbstate);      \
1241                   }                                                           \
1242               }                                                               \
1243                                                                               \
1244             if (len == (size_t) -1)                                           \
1245               {                                                               \
1246                 /* Illegal wide-character string.  */                         \
1247                 done = -1;                                                    \
1248                 goto all_done;                                                \
1249               }                                                               \
1250           }                                                                   \
1251                                                                               \
1252         if ((width -= len) < 0)                                               \
1253           {                                                                   \
1254             outstring (string, len);                                          \
1255             break;                                                            \
1256           }                                                                   \
1257                                                                               \
1258         if (!left)                                                            \
1259           PAD (' ');                                                          \
1260         outstring (string, len);                                              \
1261         if (left)                                                             \
1262           PAD (' ');                                                          \
1263         if (__glibc_unlikely (string_malloced))                               \
1264           free (string);                                                      \
1265       }                                                                       \
1266       break;
1267 #endif
1268
1269   /* Orient the stream.  */
1270 #ifdef ORIENT
1271   ORIENT;
1272 #endif
1273
1274   /* Sanity check of arguments.  */
1275   ARGCHECK (s, format);
1276
1277 #ifdef ORIENT
1278   /* Check for correct orientation.  */
1279   if (_IO_vtable_offset (s) == 0 &&
1280       _IO_fwide (s, sizeof (CHAR_T) == 1 ? -1 : 1)
1281       != (sizeof (CHAR_T) == 1 ? -1 : 1))
1282     /* The stream is already oriented otherwise.  */
1283     return EOF;
1284 #endif
1285
1286   if (UNBUFFERED_P (s))
1287     /* Use a helper function which will allocate a local temporary buffer
1288        for the stream and then call us again.  */
1289     return buffered_vfprintf (s, format, ap);
1290
1291   /* Initialize local variables.  */
1292   done = 0;
1293   grouping = (const char *) -1;
1294 #ifdef __va_copy
1295   /* This macro will be available soon in gcc's <stdarg.h>.  We need it
1296      since on some systems `va_list' is not an integral type.  */
1297   __va_copy (ap_save, ap);
1298 #else
1299   ap_save = ap;
1300 #endif
1301   nspecs_done = 0;
1302
1303 #ifdef COMPILE_WPRINTF
1304   /* Find the first format specifier.  */
1305   f = lead_str_end = __find_specwc ((const UCHAR_T *) format);
1306 #else
1307   /* Find the first format specifier.  */
1308   f = lead_str_end = __find_specmb ((const UCHAR_T *) format);
1309 #endif
1310
1311   /* Lock stream.  */
1312   _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
1313   _IO_flockfile (s);
1314
1315   /* Write the literal text before the first format.  */
1316   outstring ((const UCHAR_T *) format,
1317              lead_str_end - (const UCHAR_T *) format);
1318
1319   /* If we only have to print a simple string, return now.  */
1320   if (*f == L_('\0'))
1321     goto all_done;
1322
1323   /* Use the slow path in case any printf handler is registered.  */
1324   if (__glibc_unlikely (__printf_function_table != NULL
1325                         || __printf_modifier_table != NULL
1326                         || __printf_va_arg_table != NULL))
1327     goto do_positional;
1328
1329   /* Process whole format string.  */
1330   do
1331     {
1332 #ifdef SHARED
1333 # define REF(Name) &&do_##Name - &&do_form_unknown
1334 #else
1335 # define REF(Name) &&do_##Name
1336 #endif
1337 #define LABEL(Name) do_##Name
1338       STEP0_3_TABLE;
1339       STEP4_TABLE;
1340
1341       union printf_arg *args_value;     /* This is not used here but ... */
1342       int is_negative;  /* Flag for negative number.  */
1343       union
1344       {
1345         unsigned long long int longlong;
1346         unsigned long int word;
1347       } number;
1348       int base;
1349       union printf_arg the_arg;
1350       CHAR_T *string;   /* Pointer to argument string.  */
1351       int alt = 0;      /* Alternate format.  */
1352       int space = 0;    /* Use space prefix if no sign is needed.  */
1353       int left = 0;     /* Left-justify output.  */
1354       int showsign = 0; /* Always begin with plus or minus sign.  */
1355       int group = 0;    /* Print numbers according grouping rules.  */
1356       int is_long_double = 0; /* Argument is long double/ long long int.  */
1357       int is_short = 0; /* Argument is short int.  */
1358       int is_long = 0;  /* Argument is long int.  */
1359       int is_char = 0;  /* Argument is promoted (unsigned) char.  */
1360       int width = 0;    /* Width of output; 0 means none specified.  */
1361       int prec = -1;    /* Precision of output; -1 means none specified.  */
1362       /* This flag is set by the 'I' modifier and selects the use of the
1363          `outdigits' as determined by the current locale.  */
1364       int use_outdigits = 0;
1365       UCHAR_T pad = L_(' ');/* Padding character.  */
1366       CHAR_T spec;
1367
1368       workstart = NULL;
1369       workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1370
1371       /* Get current character in format string.  */
1372       JUMP (*++f, step0_jumps);
1373
1374       /* ' ' flag.  */
1375     LABEL (flag_space):
1376       space = 1;
1377       JUMP (*++f, step0_jumps);
1378
1379       /* '+' flag.  */
1380     LABEL (flag_plus):
1381       showsign = 1;
1382       JUMP (*++f, step0_jumps);
1383
1384       /* The '-' flag.  */
1385     LABEL (flag_minus):
1386       left = 1;
1387       pad = L_(' ');
1388       JUMP (*++f, step0_jumps);
1389
1390       /* The '#' flag.  */
1391     LABEL (flag_hash):
1392       alt = 1;
1393       JUMP (*++f, step0_jumps);
1394
1395       /* The '0' flag.  */
1396     LABEL (flag_zero):
1397       if (!left)
1398         pad = L_('0');
1399       JUMP (*++f, step0_jumps);
1400
1401       /* The '\'' flag.  */
1402     LABEL (flag_quote):
1403       group = 1;
1404
1405       if (grouping == (const char *) -1)
1406         {
1407 #ifdef COMPILE_WPRINTF
1408           thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1409                                             _NL_NUMERIC_THOUSANDS_SEP_WC);
1410 #else
1411           thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1412 #endif
1413
1414           grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1415           if (*grouping == '\0' || *grouping == CHAR_MAX
1416 #ifdef COMPILE_WPRINTF
1417               || thousands_sep == L'\0'
1418 #else
1419               || *thousands_sep == '\0'
1420 #endif
1421               )
1422             grouping = NULL;
1423         }
1424       JUMP (*++f, step0_jumps);
1425
1426     LABEL (flag_i18n):
1427       use_outdigits = 1;
1428       JUMP (*++f, step0_jumps);
1429
1430       /* Get width from argument.  */
1431     LABEL (width_asterics):
1432       {
1433         const UCHAR_T *tmp;     /* Temporary value.  */
1434
1435         tmp = ++f;
1436         if (ISDIGIT (*tmp))
1437           {
1438             int pos = read_int (&tmp);
1439
1440             if (pos == -1)
1441               {
1442                 __set_errno (EOVERFLOW);
1443                 done = -1;
1444                 goto all_done;
1445               }
1446
1447             if (pos && *tmp == L_('$'))
1448               /* The width comes from a positional parameter.  */
1449               goto do_positional;
1450           }
1451         width = va_arg (ap, int);
1452
1453         /* Negative width means left justified.  */
1454         if (width < 0)
1455           {
1456             width = -width;
1457             pad = L_(' ');
1458             left = 1;
1459           }
1460
1461         if (__glibc_unlikely (width >= INT_MAX / sizeof (CHAR_T) - 32))
1462           {
1463             __set_errno (EOVERFLOW);
1464             done = -1;
1465             goto all_done;
1466           }
1467
1468         if (width >= sizeof (work_buffer) / sizeof (work_buffer[0]) - 32)
1469           {
1470             /* We have to use a special buffer.  The "32" is just a safe
1471                bet for all the output which is not counted in the width.  */
1472             size_t needed = ((size_t) width + 32) * sizeof (CHAR_T);
1473             if (__libc_use_alloca (needed))
1474               workend = (CHAR_T *) alloca (needed) + width + 32;
1475             else
1476               {
1477                 workstart = (CHAR_T *) malloc (needed);
1478                 if (workstart == NULL)
1479                   {
1480                     done = -1;
1481                     goto all_done;
1482                   }
1483                 workend = workstart + width + 32;
1484               }
1485           }
1486       }
1487       JUMP (*f, step1_jumps);
1488
1489       /* Given width in format string.  */
1490     LABEL (width):
1491       width = read_int (&f);
1492
1493       if (__glibc_unlikely (width == -1
1494                             || width >= INT_MAX / sizeof (CHAR_T) - 32))
1495         {
1496           __set_errno (EOVERFLOW);
1497           done = -1;
1498           goto all_done;
1499         }
1500
1501       if (width >= sizeof (work_buffer) / sizeof (work_buffer[0]) - 32)
1502         {
1503           /* We have to use a special buffer.  The "32" is just a safe
1504              bet for all the output which is not counted in the width.  */
1505           size_t needed = ((size_t) width + 32) * sizeof (CHAR_T);
1506           if (__libc_use_alloca (needed))
1507             workend = (CHAR_T *) alloca (needed) + width + 32;
1508           else
1509             {
1510               workstart = (CHAR_T *) malloc (needed);
1511               if (workstart == NULL)
1512                 {
1513                   done = -1;
1514                   goto all_done;
1515                 }
1516               workend = workstart + width + 32;
1517             }
1518         }
1519       if (*f == L_('$'))
1520         /* Oh, oh.  The argument comes from a positional parameter.  */
1521         goto do_positional;
1522       JUMP (*f, step1_jumps);
1523
1524     LABEL (precision):
1525       ++f;
1526       if (*f == L_('*'))
1527         {
1528           const UCHAR_T *tmp;   /* Temporary value.  */
1529
1530           tmp = ++f;
1531           if (ISDIGIT (*tmp))
1532             {
1533               int pos = read_int (&tmp);
1534
1535               if (pos == -1)
1536                 {
1537                   __set_errno (EOVERFLOW);
1538                   done = -1;
1539                   goto all_done;
1540                 }
1541
1542               if (pos && *tmp == L_('$'))
1543                 /* The precision comes from a positional parameter.  */
1544                 goto do_positional;
1545             }
1546           prec = va_arg (ap, int);
1547
1548           /* If the precision is negative the precision is omitted.  */
1549           if (prec < 0)
1550             prec = -1;
1551         }
1552       else if (ISDIGIT (*f))
1553         {
1554           prec = read_int (&f);
1555
1556           /* The precision was specified in this case as an extremely
1557              large positive value.  */
1558           if (prec == -1)
1559             {
1560               __set_errno (EOVERFLOW);
1561               done = -1;
1562               goto all_done;
1563             }
1564         }
1565       else
1566         prec = 0;
1567       if (prec > width
1568           && prec > sizeof (work_buffer) / sizeof (work_buffer[0]) - 32)
1569         {
1570           if (__glibc_unlikely (prec >= INT_MAX / sizeof (CHAR_T) - 32))
1571             {
1572               __set_errno (EOVERFLOW);
1573               done = -1;
1574               goto all_done;
1575             }
1576           size_t needed = ((size_t) prec + 32) * sizeof (CHAR_T);
1577
1578           if (__libc_use_alloca (needed))
1579             workend = (CHAR_T *) alloca (needed) + prec + 32;
1580           else
1581             {
1582               workstart = (CHAR_T *) malloc (needed);
1583               if (workstart == NULL)
1584                 {
1585                   done = -1;
1586                   goto all_done;
1587                 }
1588               workend = workstart + prec + 32;
1589             }
1590         }
1591       JUMP (*f, step2_jumps);
1592
1593       /* Process 'h' modifier.  There might another 'h' following.  */
1594     LABEL (mod_half):
1595       is_short = 1;
1596       JUMP (*++f, step3a_jumps);
1597
1598       /* Process 'hh' modifier.  */
1599     LABEL (mod_halfhalf):
1600       is_short = 0;
1601       is_char = 1;
1602       JUMP (*++f, step4_jumps);
1603
1604       /* Process 'l' modifier.  There might another 'l' following.  */
1605     LABEL (mod_long):
1606       is_long = 1;
1607       JUMP (*++f, step3b_jumps);
1608
1609       /* Process 'L', 'q', or 'll' modifier.  No other modifier is
1610          allowed to follow.  */
1611     LABEL (mod_longlong):
1612       is_long_double = 1;
1613       is_long = 1;
1614       JUMP (*++f, step4_jumps);
1615
1616     LABEL (mod_size_t):
1617       is_long_double = sizeof (size_t) > sizeof (unsigned long int);
1618       is_long = sizeof (size_t) > sizeof (unsigned int);
1619       JUMP (*++f, step4_jumps);
1620
1621     LABEL (mod_ptrdiff_t):
1622       is_long_double = sizeof (ptrdiff_t) > sizeof (unsigned long int);
1623       is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
1624       JUMP (*++f, step4_jumps);
1625
1626     LABEL (mod_intmax_t):
1627       is_long_double = sizeof (intmax_t) > sizeof (unsigned long int);
1628       is_long = sizeof (intmax_t) > sizeof (unsigned int);
1629       JUMP (*++f, step4_jumps);
1630
1631       /* Process current format.  */
1632       while (1)
1633         {
1634           process_arg (((struct printf_spec *) NULL));
1635           process_string_arg (((struct printf_spec *) NULL));
1636
1637         LABEL (form_unknown):
1638           if (spec == L_('\0'))
1639             {
1640               /* The format string ended before the specifier is complete.  */
1641               __set_errno (EINVAL);
1642               done = -1;
1643               goto all_done;
1644             }
1645
1646           /* If we are in the fast loop force entering the complicated
1647              one.  */
1648           goto do_positional;
1649         }
1650
1651       /* The format is correctly handled.  */
1652       ++nspecs_done;
1653
1654       if (__glibc_unlikely (workstart != NULL))
1655         free (workstart);
1656       workstart = NULL;
1657
1658       /* Look for next format specifier.  */
1659 #ifdef COMPILE_WPRINTF
1660       f = __find_specwc ((end_of_spec = ++f));
1661 #else
1662       f = __find_specmb ((end_of_spec = ++f));
1663 #endif
1664
1665       /* Write the following constant string.  */
1666       outstring (end_of_spec, f - end_of_spec);
1667     }
1668   while (*f != L_('\0'));
1669
1670   /* Unlock stream and return.  */
1671   goto all_done;
1672
1673   /* Here starts the more complex loop to handle positional parameters.  */
1674 do_positional:
1675   {
1676     /* Array with information about the needed arguments.  This has to
1677        be dynamically extensible.  */
1678     size_t nspecs = 0;
1679     /* A more or less arbitrary start value.  */
1680     size_t nspecs_size = 32 * sizeof (struct printf_spec);
1681     struct printf_spec *specs = alloca (nspecs_size);
1682
1683     /* The number of arguments the format string requests.  This will
1684        determine the size of the array needed to store the argument
1685        attributes.  */
1686     size_t nargs = 0;
1687     size_t bytes_per_arg;
1688     union printf_arg *args_value;
1689     int *args_size;
1690     int *args_type;
1691
1692     /* Positional parameters refer to arguments directly.  This could
1693        also determine the maximum number of arguments.  Track the
1694        maximum number.  */
1695     size_t max_ref_arg = 0;
1696
1697     /* Just a counter.  */
1698     size_t cnt;
1699
1700     if (__glibc_unlikely (workstart != NULL))
1701       free (workstart);
1702     workstart = NULL;
1703
1704     if (grouping == (const char *) -1)
1705       {
1706 #ifdef COMPILE_WPRINTF
1707         thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1708                                           _NL_NUMERIC_THOUSANDS_SEP_WC);
1709 #else
1710         thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1711 #endif
1712
1713         grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1714         if (*grouping == '\0' || *grouping == CHAR_MAX)
1715           grouping = NULL;
1716       }
1717
1718     for (f = lead_str_end; *f != L_('\0'); f = specs[nspecs++].next_fmt)
1719       {
1720         if (nspecs * sizeof (*specs) >= nspecs_size)
1721           {
1722             /* Extend the array of format specifiers.  */
1723             struct printf_spec *old = specs;
1724             specs = extend_alloca (specs, nspecs_size, 2 * nspecs_size);
1725
1726             /* Copy the old array's elements to the new space.  */
1727             memmove (specs, old, nspecs * sizeof (*specs));
1728           }
1729
1730         /* Parse the format specifier.  */
1731 #ifdef COMPILE_WPRINTF
1732         nargs += __parse_one_specwc (f, nargs, &specs[nspecs], &max_ref_arg);
1733 #else
1734         nargs += __parse_one_specmb (f, nargs, &specs[nspecs], &max_ref_arg);
1735 #endif
1736       }
1737
1738     /* Determine the number of arguments the format string consumes.  */
1739     nargs = MAX (nargs, max_ref_arg);
1740     /* Calculate total size needed to represent a single argument across
1741        all three argument-related arrays.  */
1742     bytes_per_arg = (sizeof (*args_value) + sizeof (*args_size)
1743                      + sizeof (*args_type));
1744
1745     /* Check for potential integer overflow.  */
1746     if (__glibc_unlikely (nargs > INT_MAX / bytes_per_arg))
1747       {
1748          __set_errno (EOVERFLOW);
1749          done = -1;
1750          goto all_done;
1751       }
1752
1753     /* Allocate memory for all three argument arrays.  */
1754     if (__libc_use_alloca (nargs * bytes_per_arg))
1755         args_value = alloca (nargs * bytes_per_arg);
1756     else
1757       {
1758         args_value = args_malloced = malloc (nargs * bytes_per_arg);
1759         if (args_value == NULL)
1760           {
1761             done = -1;
1762             goto all_done;
1763           }
1764       }
1765
1766     /* Set up the remaining two arrays to each point past the end of the
1767        prior array, since space for all three has been allocated now.  */
1768     args_size = &args_value[nargs].pa_int;
1769     args_type = &args_size[nargs];
1770     memset (args_type, s->_flags2 & _IO_FLAGS2_FORTIFY ? '\xff' : '\0',
1771             nargs * sizeof (*args_type));
1772
1773     /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1774        still zero after this loop, format is invalid.  For now we
1775        simply use 0 as the value.  */
1776
1777     /* Fill in the types of all the arguments.  */
1778     for (cnt = 0; cnt < nspecs; ++cnt)
1779       {
1780         /* If the width is determined by an argument this is an int.  */
1781         if (specs[cnt].width_arg != -1)
1782           args_type[specs[cnt].width_arg] = PA_INT;
1783
1784         /* If the precision is determined by an argument this is an int.  */
1785         if (specs[cnt].prec_arg != -1)
1786           args_type[specs[cnt].prec_arg] = PA_INT;
1787
1788         switch (specs[cnt].ndata_args)
1789           {
1790           case 0:               /* No arguments.  */
1791             break;
1792           case 1:               /* One argument; we already have the
1793                                    type and size.  */
1794             args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1795             args_size[specs[cnt].data_arg] = specs[cnt].size;
1796             break;
1797           default:
1798             /* We have more than one argument for this format spec.
1799                We must call the arginfo function again to determine
1800                all the types.  */
1801             (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1802               (&specs[cnt].info,
1803                specs[cnt].ndata_args, &args_type[specs[cnt].data_arg],
1804                &args_size[specs[cnt].data_arg]);
1805             break;
1806           }
1807       }
1808
1809     /* Now we know all the types and the order.  Fill in the argument
1810        values.  */
1811     for (cnt = 0; cnt < nargs; ++cnt)
1812       switch (args_type[cnt])
1813         {
1814 #define T(tag, mem, type)                                                     \
1815         case tag:                                                             \
1816           args_value[cnt].mem = va_arg (ap_save, type);                       \
1817           break
1818
1819         T (PA_WCHAR, pa_wchar, wint_t);
1820         case PA_CHAR:                           /* Promoted.  */
1821         case PA_INT|PA_FLAG_SHORT:              /* Promoted.  */
1822 #if LONG_MAX == INT_MAX
1823         case PA_INT|PA_FLAG_LONG:
1824 #endif
1825         T (PA_INT, pa_int, int);
1826 #if LONG_MAX == LONG_LONG_MAX
1827         case PA_INT|PA_FLAG_LONG:
1828 #endif
1829         T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1830 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1831 # error "he?"
1832 #endif
1833         case PA_FLOAT:                          /* Promoted.  */
1834         T (PA_DOUBLE, pa_double, double);
1835         case PA_DOUBLE|PA_FLAG_LONG_DOUBLE:
1836           if (__ldbl_is_dbl)
1837             {
1838               args_value[cnt].pa_double = va_arg (ap_save, double);
1839               args_type[cnt] &= ~PA_FLAG_LONG_DOUBLE;
1840             }
1841           else
1842             args_value[cnt].pa_long_double = va_arg (ap_save, long double);
1843           break;
1844         case PA_STRING:                         /* All pointers are the same */
1845         case PA_WSTRING:                        /* All pointers are the same */
1846         T (PA_POINTER, pa_pointer, void *);
1847 #undef T
1848         default:
1849           if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1850             args_value[cnt].pa_pointer = va_arg (ap_save, void *);
1851           else if (__glibc_unlikely (__printf_va_arg_table != NULL)
1852                    && __printf_va_arg_table[args_type[cnt] - PA_LAST] != NULL)
1853             {
1854               args_value[cnt].pa_user = alloca (args_size[cnt]);
1855               (*__printf_va_arg_table[args_type[cnt] - PA_LAST])
1856                 (args_value[cnt].pa_user, &ap_save);
1857             }
1858           else
1859             args_value[cnt].pa_long_double = 0.0;
1860           break;
1861         case -1:
1862           /* Error case.  Not all parameters appear in N$ format
1863              strings.  We have no way to determine their type.  */
1864           assert (s->_flags2 & _IO_FLAGS2_FORTIFY);
1865           __libc_fatal ("*** invalid %N$ use detected ***\n");
1866         }
1867
1868     /* Now walk through all format specifiers and process them.  */
1869     for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1870       {
1871 #undef REF
1872 #ifdef SHARED
1873 # define REF(Name) &&do2_##Name - &&do_form_unknown
1874 #else
1875 # define REF(Name) &&do2_##Name
1876 #endif
1877 #undef LABEL
1878 #define LABEL(Name) do2_##Name
1879         STEP4_TABLE;
1880
1881         int is_negative;
1882         union
1883         {
1884           unsigned long long int longlong;
1885           unsigned long int word;
1886         } number;
1887         int base;
1888         union printf_arg the_arg;
1889         CHAR_T *string;         /* Pointer to argument string.  */
1890
1891         /* Fill variables from values in struct.  */
1892         int alt = specs[nspecs_done].info.alt;
1893         int space = specs[nspecs_done].info.space;
1894         int left = specs[nspecs_done].info.left;
1895         int showsign = specs[nspecs_done].info.showsign;
1896         int group = specs[nspecs_done].info.group;
1897         int is_long_double = specs[nspecs_done].info.is_long_double;
1898         int is_short = specs[nspecs_done].info.is_short;
1899         int is_char = specs[nspecs_done].info.is_char;
1900         int is_long = specs[nspecs_done].info.is_long;
1901         int width = specs[nspecs_done].info.width;
1902         int prec = specs[nspecs_done].info.prec;
1903         int use_outdigits = specs[nspecs_done].info.i18n;
1904         char pad = specs[nspecs_done].info.pad;
1905         CHAR_T spec = specs[nspecs_done].info.spec;
1906
1907         workstart = NULL;
1908         workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1909
1910         /* Fill in last information.  */
1911         if (specs[nspecs_done].width_arg != -1)
1912           {
1913             /* Extract the field width from an argument.  */
1914             specs[nspecs_done].info.width =
1915               args_value[specs[nspecs_done].width_arg].pa_int;
1916
1917             if (specs[nspecs_done].info.width < 0)
1918               /* If the width value is negative left justification is
1919                  selected and the value is taken as being positive.  */
1920               {
1921                 specs[nspecs_done].info.width *= -1;
1922                 left = specs[nspecs_done].info.left = 1;
1923               }
1924             width = specs[nspecs_done].info.width;
1925           }
1926
1927         if (specs[nspecs_done].prec_arg != -1)
1928           {
1929             /* Extract the precision from an argument.  */
1930             specs[nspecs_done].info.prec =
1931               args_value[specs[nspecs_done].prec_arg].pa_int;
1932
1933             if (specs[nspecs_done].info.prec < 0)
1934               /* If the precision is negative the precision is
1935                  omitted.  */
1936               specs[nspecs_done].info.prec = -1;
1937
1938             prec = specs[nspecs_done].info.prec;
1939           }
1940
1941         /* Maybe the buffer is too small.  */
1942         if (MAX (prec, width) + 32 > (int) (sizeof (work_buffer)
1943                                             / sizeof (CHAR_T)))
1944           {
1945             if (__libc_use_alloca ((MAX (prec, width) + 32)
1946                                    * sizeof (CHAR_T)))
1947               workend = ((CHAR_T *) alloca ((MAX (prec, width) + 32)
1948                                             * sizeof (CHAR_T))
1949                          + (MAX (prec, width) + 32));
1950             else
1951               {
1952                 workstart = (CHAR_T *) malloc ((MAX (prec, width) + 32)
1953                                                * sizeof (CHAR_T));
1954                 if (workstart == NULL)
1955                   {
1956                     done = -1;
1957                     goto all_done;
1958                   }
1959                 workend = workstart + (MAX (prec, width) + 32);
1960               }
1961           }
1962
1963         /* Process format specifiers.  */
1964         while (1)
1965           {
1966             extern printf_function **__printf_function_table;
1967             int function_done;
1968
1969             if (spec <= UCHAR_MAX
1970                 && __printf_function_table != NULL
1971                 && __printf_function_table[(size_t) spec] != NULL)
1972               {
1973                 const void **ptr = alloca (specs[nspecs_done].ndata_args
1974                                            * sizeof (const void *));
1975
1976                 /* Fill in an array of pointers to the argument values.  */
1977                 for (unsigned int i = 0; i < specs[nspecs_done].ndata_args;
1978                      ++i)
1979                   ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1980
1981                 /* Call the function.  */
1982                 function_done = __printf_function_table[(size_t) spec]
1983                   (s, &specs[nspecs_done].info, ptr);
1984
1985                 if (function_done != -2)
1986                   {
1987                     /* If an error occurred we don't have information
1988                        about # of chars.  */
1989                     if (function_done < 0)
1990                       {
1991                         /* Function has set errno.  */
1992                         done = -1;
1993                         goto all_done;
1994                       }
1995
1996                     done_add (function_done);
1997                     break;
1998                   }
1999               }
2000
2001             JUMP (spec, step4_jumps);
2002
2003             process_arg ((&specs[nspecs_done]));
2004             process_string_arg ((&specs[nspecs_done]));
2005
2006           LABEL (form_unknown):
2007             {
2008               unsigned int i;
2009               const void **ptr;
2010
2011               ptr = alloca (specs[nspecs_done].ndata_args
2012                             * sizeof (const void *));
2013
2014               /* Fill in an array of pointers to the argument values.  */
2015               for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
2016                 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
2017
2018               /* Call the function.  */
2019               function_done = printf_unknown (s, &specs[nspecs_done].info,
2020                                               ptr);
2021
2022               /* If an error occurred we don't have information about #
2023                  of chars.  */
2024               if (function_done < 0)
2025                 {
2026                   /* Function has set errno.  */
2027                   done = -1;
2028                   goto all_done;
2029                 }
2030
2031               done_add (function_done);
2032             }
2033             break;
2034           }
2035
2036         if (__glibc_unlikely (workstart != NULL))
2037           free (workstart);
2038         workstart = NULL;
2039
2040         /* Write the following constant string.  */
2041         outstring (specs[nspecs_done].end_of_fmt,
2042                    specs[nspecs_done].next_fmt
2043                    - specs[nspecs_done].end_of_fmt);
2044       }
2045   }
2046
2047 all_done:
2048   if (__glibc_unlikely (args_malloced != NULL))
2049     free (args_malloced);
2050   if (__glibc_unlikely (workstart != NULL))
2051     free (workstart);
2052   /* Unlock the stream.  */
2053   _IO_funlockfile (s);
2054   _IO_cleanup_region_end (0);
2055
2056   return done;
2057 }
2058 \f
2059 /* Handle an unknown format specifier.  This prints out a canonicalized
2060    representation of the format spec itself.  */
2061 static int
2062 printf_unknown (FILE *s, const struct printf_info *info,
2063                 const void *const *args)
2064
2065 {
2066   int done = 0;
2067   CHAR_T work_buffer[MAX (sizeof (info->width), sizeof (info->prec)) * 3];
2068   CHAR_T *const workend
2069     = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
2070   CHAR_T *w;
2071
2072   outchar (L_('%'));
2073
2074   if (info->alt)
2075     outchar (L_('#'));
2076   if (info->group)
2077     outchar (L_('\''));
2078   if (info->showsign)
2079     outchar (L_('+'));
2080   else if (info->space)
2081     outchar (L_(' '));
2082   if (info->left)
2083     outchar (L_('-'));
2084   if (info->pad == L_('0'))
2085     outchar (L_('0'));
2086   if (info->i18n)
2087     outchar (L_('I'));
2088
2089   if (info->width != 0)
2090     {
2091       w = _itoa_word (info->width, workend, 10, 0);
2092       while (w < workend)
2093         outchar (*w++);
2094     }
2095
2096   if (info->prec != -1)
2097     {
2098       outchar (L_('.'));
2099       w = _itoa_word (info->prec, workend, 10, 0);
2100       while (w < workend)
2101         outchar (*w++);
2102     }
2103
2104   if (info->spec != L_('\0'))
2105     outchar (info->spec);
2106
2107  all_done:
2108   return done;
2109 }
2110 \f
2111 /* Group the digits according to the grouping rules of the current locale.
2112    The interpretation of GROUPING is as in `struct lconv' from <locale.h>.  */
2113 static CHAR_T *
2114 internal_function
2115 group_number (CHAR_T *w, CHAR_T *rear_ptr, const char *grouping,
2116 #ifdef COMPILE_WPRINTF
2117               wchar_t thousands_sep
2118 #else
2119               const char *thousands_sep
2120 #endif
2121               )
2122 {
2123   int len;
2124   CHAR_T *src, *s;
2125 #ifndef COMPILE_WPRINTF
2126   int tlen = strlen (thousands_sep);
2127 #endif
2128
2129   /* We treat all negative values like CHAR_MAX.  */
2130
2131   if (*grouping == CHAR_MAX || *grouping <= 0)
2132     /* No grouping should be done.  */
2133     return w;
2134
2135   len = *grouping++;
2136
2137   /* Copy existing string so that nothing gets overwritten.  */
2138   src = (CHAR_T *) alloca ((rear_ptr - w) * sizeof (CHAR_T));
2139   s = (CHAR_T *) __mempcpy (src, w,
2140                             (rear_ptr - w) * sizeof (CHAR_T));
2141   w = rear_ptr;
2142
2143   /* Process all characters in the string.  */
2144   while (s > src)
2145     {
2146       *--w = *--s;
2147
2148       if (--len == 0 && s > src)
2149         {
2150           /* A new group begins.  */
2151 #ifdef COMPILE_WPRINTF
2152           *--w = thousands_sep;
2153 #else
2154           int cnt = tlen;
2155           do
2156             *--w = thousands_sep[--cnt];
2157           while (cnt > 0);
2158 #endif
2159
2160           if (*grouping == CHAR_MAX
2161 #if CHAR_MIN < 0
2162                    || *grouping < 0
2163 #endif
2164                    )
2165             {
2166               /* No further grouping to be done.
2167                  Copy the rest of the number.  */
2168               do
2169                 *--w = *--s;
2170               while (s > src);
2171               break;
2172             }
2173           else if (*grouping != '\0')
2174             /* The previous grouping repeats ad infinitum.  */
2175             len = *grouping++;
2176           else
2177             len = grouping[-1];
2178         }
2179     }
2180   return w;
2181 }
2182 \f
2183 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer.  */
2184 struct helper_file
2185   {
2186     struct _IO_FILE_plus _f;
2187 #ifdef COMPILE_WPRINTF
2188     struct _IO_wide_data _wide_data;
2189 #endif
2190     _IO_FILE *_put_stream;
2191 #ifdef _IO_MTSAFE_IO
2192     _IO_lock_t lock;
2193 #endif
2194   };
2195
2196 static int
2197 _IO_helper_overflow (_IO_FILE *s, int c)
2198 {
2199   _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
2200 #ifdef COMPILE_WPRINTF
2201   int used = s->_wide_data->_IO_write_ptr - s->_wide_data->_IO_write_base;
2202   if (used)
2203     {
2204       _IO_size_t written = _IO_sputn (target, s->_wide_data->_IO_write_base,
2205                                       used);
2206       if (written == 0 || written == WEOF)
2207         return WEOF;
2208       __wmemmove (s->_wide_data->_IO_write_base,
2209                   s->_wide_data->_IO_write_base + written,
2210                   used - written);
2211       s->_wide_data->_IO_write_ptr -= written;
2212     }
2213 #else
2214   int used = s->_IO_write_ptr - s->_IO_write_base;
2215   if (used)
2216     {
2217       _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
2218       if (written == 0 || written == EOF)
2219         return EOF;
2220       memmove (s->_IO_write_base, s->_IO_write_base + written,
2221                used - written);
2222       s->_IO_write_ptr -= written;
2223     }
2224 #endif
2225   return PUTC (c, s);
2226 }
2227
2228 #ifdef COMPILE_WPRINTF
2229 static const struct _IO_jump_t _IO_helper_jumps =
2230 {
2231   JUMP_INIT_DUMMY,
2232   JUMP_INIT (finish, _IO_wdefault_finish),
2233   JUMP_INIT (overflow, _IO_helper_overflow),
2234   JUMP_INIT (underflow, _IO_default_underflow),
2235   JUMP_INIT (uflow, _IO_default_uflow),
2236   JUMP_INIT (pbackfail, (_IO_pbackfail_t) _IO_wdefault_pbackfail),
2237   JUMP_INIT (xsputn, _IO_wdefault_xsputn),
2238   JUMP_INIT (xsgetn, _IO_wdefault_xsgetn),
2239   JUMP_INIT (seekoff, _IO_default_seekoff),
2240   JUMP_INIT (seekpos, _IO_default_seekpos),
2241   JUMP_INIT (setbuf, _IO_default_setbuf),
2242   JUMP_INIT (sync, _IO_default_sync),
2243   JUMP_INIT (doallocate, _IO_wdefault_doallocate),
2244   JUMP_INIT (read, _IO_default_read),
2245   JUMP_INIT (write, _IO_default_write),
2246   JUMP_INIT (seek, _IO_default_seek),
2247   JUMP_INIT (close, _IO_default_close),
2248   JUMP_INIT (stat, _IO_default_stat)
2249 };
2250 #else
2251 static const struct _IO_jump_t _IO_helper_jumps =
2252 {
2253   JUMP_INIT_DUMMY,
2254   JUMP_INIT (finish, _IO_default_finish),
2255   JUMP_INIT (overflow, _IO_helper_overflow),
2256   JUMP_INIT (underflow, _IO_default_underflow),
2257   JUMP_INIT (uflow, _IO_default_uflow),
2258   JUMP_INIT (pbackfail, _IO_default_pbackfail),
2259   JUMP_INIT (xsputn, _IO_default_xsputn),
2260   JUMP_INIT (xsgetn, _IO_default_xsgetn),
2261   JUMP_INIT (seekoff, _IO_default_seekoff),
2262   JUMP_INIT (seekpos, _IO_default_seekpos),
2263   JUMP_INIT (setbuf, _IO_default_setbuf),
2264   JUMP_INIT (sync, _IO_default_sync),
2265   JUMP_INIT (doallocate, _IO_default_doallocate),
2266   JUMP_INIT (read, _IO_default_read),
2267   JUMP_INIT (write, _IO_default_write),
2268   JUMP_INIT (seek, _IO_default_seek),
2269   JUMP_INIT (close, _IO_default_close),
2270   JUMP_INIT (stat, _IO_default_stat)
2271 };
2272 #endif
2273
2274 static int
2275 internal_function
2276 buffered_vfprintf (_IO_FILE *s, const CHAR_T *format,
2277                    _IO_va_list args)
2278 {
2279   CHAR_T buf[_IO_BUFSIZ];
2280   struct helper_file helper;
2281   _IO_FILE *hp = (_IO_FILE *) &helper._f;
2282   int result, to_flush;
2283
2284   /* Orient the stream.  */
2285 #ifdef ORIENT
2286   ORIENT;
2287 #endif
2288
2289   /* Initialize helper.  */
2290   helper._put_stream = s;
2291 #ifdef COMPILE_WPRINTF
2292   hp->_wide_data = &helper._wide_data;
2293   _IO_wsetp (hp, buf, buf + sizeof buf / sizeof (CHAR_T));
2294   hp->_mode = 1;
2295 #else
2296   _IO_setp (hp, buf, buf + sizeof buf);
2297   hp->_mode = -1;
2298 #endif
2299   hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS|_IO_USER_LOCK;
2300 #if _IO_JUMPS_OFFSET
2301   hp->_vtable_offset = 0;
2302 #endif
2303 #ifdef _IO_MTSAFE_IO
2304   hp->_lock = NULL;
2305 #endif
2306   hp->_flags2 = s->_flags2;
2307   _IO_JUMPS (&helper._f) = (struct _IO_jump_t *) &_IO_helper_jumps;
2308
2309   /* Now print to helper instead.  */
2310 #ifndef COMPILE_WPRINTF
2311   result = _IO_vfprintf (hp, format, args);
2312 #else
2313   result = vfprintf (hp, format, args);
2314 #endif
2315
2316   /* Lock stream.  */
2317   __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, s);
2318   _IO_flockfile (s);
2319
2320   /* Now flush anything from the helper to the S. */
2321 #ifdef COMPILE_WPRINTF
2322   if ((to_flush = (hp->_wide_data->_IO_write_ptr
2323                    - hp->_wide_data->_IO_write_base)) > 0)
2324     {
2325       if ((int) _IO_sputn (s, hp->_wide_data->_IO_write_base, to_flush)
2326           != to_flush)
2327         result = -1;
2328     }
2329 #else
2330   if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
2331     {
2332       if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
2333         result = -1;
2334     }
2335 #endif
2336
2337   /* Unlock the stream.  */
2338   _IO_funlockfile (s);
2339   __libc_cleanup_region_end (0);
2340
2341   return result;
2342 }
2343
2344 #undef vfprintf
2345 #ifdef COMPILE_WPRINTF
2346 strong_alias (_IO_vfwprintf, __vfwprintf);
2347 ldbl_weak_alias (_IO_vfwprintf, vfwprintf);
2348 #else
2349 ldbl_strong_alias (_IO_vfprintf_internal, vfprintf);
2350 ldbl_hidden_def (_IO_vfprintf_internal, vfprintf)
2351 ldbl_strong_alias (_IO_vfprintf_internal, _IO_vfprintf);
2352 ldbl_hidden_def (_IO_vfprintf_internal, _IO_vfprintf)
2353 #endif