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