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