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