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