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