x86: Use pad in pthread_unwind_buf to preserve shadow stack register
[glibc.git] / stdio-common / vfprintf.c
blobae412e4b8444aea22a691d9f62ecc07cf02a3389
1 /* Copyright (C) 1991-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #include <array_length.h>
19 #include <ctype.h>
20 #include <limits.h>
21 #include <printf.h>
22 #include <stdarg.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <wchar.h>
28 #include <libc-lock.h>
29 #include <sys/param.h>
30 #include <_itoa.h>
31 #include <locale/localeinfo.h>
32 #include <stdio.h>
33 #include <scratch_buffer.h>
35 /* This code is shared between the standard stdio implementation found
36 in GNU C library and the libio implementation originally found in
37 GNU libg++.
39 Beside this it is also shared between the normal and wide character
40 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
42 #include <libioP.h>
44 /* In some cases we need extra space for all the output which is not
45 counted in the width of the string. We assume 32 characters is
46 enough. */
47 #define EXTSIZ 32
48 #define ARGCHECK(S, Format) \
49 do \
50 { \
51 /* Check file argument for consistence. */ \
52 CHECK_FILE (S, -1); \
53 if (S->_flags & _IO_NO_WRITES) \
54 { \
55 S->_flags |= _IO_ERR_SEEN; \
56 __set_errno (EBADF); \
57 return -1; \
58 } \
59 if (Format == NULL) \
60 { \
61 __set_errno (EINVAL); \
62 return -1; \
63 } \
64 } while (0)
65 #define UNBUFFERED_P(S) ((S)->_flags & _IO_UNBUFFERED)
67 #define done_add(val) \
68 do { \
69 unsigned int _val = val; \
70 assert ((unsigned int) done < (unsigned int) INT_MAX); \
71 if (__glibc_unlikely (INT_MAX - done < _val)) \
72 { \
73 done = -1; \
74 __set_errno (EOVERFLOW); \
75 goto all_done; \
76 } \
77 done += _val; \
78 } while (0)
80 #ifndef COMPILE_WPRINTF
81 # define vfprintf _IO_vfprintf_internal
82 # define CHAR_T char
83 # define UCHAR_T unsigned char
84 # define INT_T int
85 typedef const char *THOUSANDS_SEP_T;
86 # define L_(Str) Str
87 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
88 # define STR_LEN(Str) strlen (Str)
90 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
91 # define PAD(Padchar) \
92 do { \
93 if (width > 0) \
94 { \
95 ssize_t written = _IO_padn (s, (Padchar), width); \
96 if (__glibc_unlikely (written != width)) \
97 { \
98 done = -1; \
99 goto all_done; \
101 done_add (written); \
103 } while (0)
104 # define PUTC(C, F) _IO_putc_unlocked (C, F)
105 # define ORIENT if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
106 return -1
107 #else
108 # define vfprintf _IO_vfwprintf
109 # define CHAR_T wchar_t
110 /* This is a hack!!! There should be a type uwchar_t. */
111 # define UCHAR_T unsigned int /* uwchar_t */
112 # define INT_T wint_t
113 typedef wchar_t THOUSANDS_SEP_T;
114 # define L_(Str) L##Str
115 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
116 # define STR_LEN(Str) __wcslen (Str)
118 # include <_itowa.h>
120 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
121 # define PAD(Padchar) \
122 do { \
123 if (width > 0) \
125 ssize_t written = _IO_wpadn (s, (Padchar), width); \
126 if (__glibc_unlikely (written != width)) \
128 done = -1; \
129 goto all_done; \
131 done_add (written); \
133 } while (0)
134 # define PUTC(C, F) _IO_putwc_unlocked (C, F)
135 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
137 # undef _itoa
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
144 #include "_i18n_number.h"
146 /* Include the shared code for parsing the format string. */
147 #include "printf-parse.h"
150 #define outchar(Ch) \
151 do \
153 const INT_T outc = (Ch); \
154 if (PUTC (outc, s) == EOF || done == INT_MAX) \
156 done = -1; \
157 goto all_done; \
159 ++done; \
161 while (0)
163 #define outstring(String, Len) \
164 do \
166 assert ((size_t) done <= (size_t) INT_MAX); \
167 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
169 done = -1; \
170 goto all_done; \
172 if (__glibc_unlikely (INT_MAX - done < (Len))) \
174 done = -1; \
175 __set_errno (EOVERFLOW); \
176 goto all_done; \
178 done += (Len); \
180 while (0)
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
188 # define is_longlong is_long_double
189 #endif
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
200 /* Global constants. */
201 static const CHAR_T null[] = L_("(null)");
203 /* Size of the work_buffer variable (in characters, not bytes. */
204 enum { WORK_BUFFER_SIZE = 1000 / sizeof (CHAR_T) };
206 /* This table maps a character into a number representing a class. In
207 each step there is a destination label for each class. */
208 static const uint8_t jump_table[] =
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,
218 0, /* 'A' */ 26, 0, /* 'C' */ 25,
219 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
220 0, /* 'I' */ 29, 0, 0,
221 /* 'L' */ 12, 0, 0, 0,
222 0, 0, 0, /* 'S' */ 21,
223 0, 0, 0, 0,
224 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
225 0, 0, 0, 0,
226 0, /* 'a' */ 26, 0, /* 'c' */ 20,
227 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
228 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
229 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
230 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
231 /* 't' */ 27, /* 'u' */ 16, 0, 0,
232 /* 'x' */ 18, 0, /* 'z' */ 13
235 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
236 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
237 #define LABEL(Name) do_##Name
238 #ifdef SHARED
239 /* 'int' is enough and it saves some space on 64 bit systems. */
240 # define JUMP_TABLE_TYPE const int
241 # define JUMP_TABLE_BASE_LABEL do_form_unknown
242 # define REF(Name) &&do_##Name - &&JUMP_TABLE_BASE_LABEL
243 # define JUMP(ChExpr, table) \
244 do \
246 int offset; \
247 void *ptr; \
248 spec = (ChExpr); \
249 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
250 : table[CHAR_CLASS (spec)]; \
251 ptr = &&JUMP_TABLE_BASE_LABEL + offset; \
252 goto *ptr; \
254 while (0)
255 #else
256 # define JUMP_TABLE_TYPE const void *const
257 # define REF(Name) &&do_##Name
258 # define JUMP(ChExpr, table) \
259 do \
261 const void *ptr; \
262 spec = (ChExpr); \
263 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
264 : table[CHAR_CLASS (spec)]; \
265 goto *ptr; \
267 while (0)
268 #endif
270 #define STEP0_3_TABLE \
271 /* Step 0: at the beginning. */ \
272 static JUMP_TABLE_TYPE step0_jumps[30] = \
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' */ \
287 REF (mod_size_t), /* for 'z', 'Z' */ \
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' */ \
293 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
294 REF (form_character), /* for 'c' */ \
295 REF (form_string), /* for 's', 'S' */ \
296 REF (form_pointer), /* for 'p' */ \
297 REF (form_number), /* for 'n' */ \
298 REF (form_strerror), /* for 'm' */ \
299 REF (form_wcharacter), /* for 'C' */ \
300 REF (form_floathex), /* for 'A', 'a' */ \
301 REF (mod_ptrdiff_t), /* for 't' */ \
302 REF (mod_intmax_t), /* for 'j' */ \
303 REF (flag_i18n), /* for 'I' */ \
304 }; \
305 /* Step 1: after processing width. */ \
306 static JUMP_TABLE_TYPE step1_jumps[30] = \
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' */ \
321 REF (mod_size_t), /* for 'z', 'Z' */ \
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' */ \
327 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
328 REF (form_character), /* for 'c' */ \
329 REF (form_string), /* for 's', 'S' */ \
330 REF (form_pointer), /* for 'p' */ \
331 REF (form_number), /* for 'n' */ \
332 REF (form_strerror), /* for 'm' */ \
333 REF (form_wcharacter), /* for 'C' */ \
334 REF (form_floathex), /* for 'A', 'a' */ \
335 REF (mod_ptrdiff_t), /* for 't' */ \
336 REF (mod_intmax_t), /* for 'j' */ \
337 REF (form_unknown) /* for 'I' */ \
338 }; \
339 /* Step 2: after processing precision. */ \
340 static JUMP_TABLE_TYPE step2_jumps[30] = \
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' */ \
355 REF (mod_size_t), /* for 'z', 'Z' */ \
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' */ \
361 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
362 REF (form_character), /* for 'c' */ \
363 REF (form_string), /* for 's', 'S' */ \
364 REF (form_pointer), /* for 'p' */ \
365 REF (form_number), /* for 'n' */ \
366 REF (form_strerror), /* for 'm' */ \
367 REF (form_wcharacter), /* for 'C' */ \
368 REF (form_floathex), /* for 'A', 'a' */ \
369 REF (mod_ptrdiff_t), /* for 't' */ \
370 REF (mod_intmax_t), /* for 'j' */ \
371 REF (form_unknown) /* for 'I' */ \
372 }; \
373 /* Step 3a: after processing first 'h' modifier. */ \
374 static JUMP_TABLE_TYPE step3a_jumps[30] = \
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' */ \
389 REF (form_unknown), /* for 'z', 'Z' */ \
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' */ \
395 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
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' */ \
402 REF (form_unknown), /* for 'A', 'a' */ \
403 REF (form_unknown), /* for 't' */ \
404 REF (form_unknown), /* for 'j' */ \
405 REF (form_unknown) /* for 'I' */ \
406 }; \
407 /* Step 3b: after processing first 'l' modifier. */ \
408 static JUMP_TABLE_TYPE step3b_jumps[30] = \
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' */ \
423 REF (form_unknown), /* for 'z', 'Z' */ \
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' */ \
429 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
430 REF (form_character), /* for 'c' */ \
431 REF (form_string), /* for 's', 'S' */ \
432 REF (form_pointer), /* for 'p' */ \
433 REF (form_number), /* for 'n' */ \
434 REF (form_strerror), /* for 'm' */ \
435 REF (form_wcharacter), /* for 'C' */ \
436 REF (form_floathex), /* for 'A', 'a' */ \
437 REF (form_unknown), /* for 't' */ \
438 REF (form_unknown), /* for 'j' */ \
439 REF (form_unknown) /* for 'I' */ \
442 #define STEP4_TABLE \
443 /* Step 4: processing format specifier. */ \
444 static JUMP_TABLE_TYPE step4_jumps[30] = \
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' */ \
459 REF (form_unknown), /* for 'z', 'Z' */ \
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' */ \
465 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
466 REF (form_character), /* for 'c' */ \
467 REF (form_string), /* for 's', 'S' */ \
468 REF (form_pointer), /* for 'p' */ \
469 REF (form_number), /* for 'n' */ \
470 REF (form_strerror), /* for 'm' */ \
471 REF (form_wcharacter), /* for 'C' */ \
472 REF (form_floathex), /* for 'A', 'a' */ \
473 REF (form_unknown), /* for 't' */ \
474 REF (form_unknown), /* for 'j' */ \
475 REF (form_unknown) /* for 'I' */ \
479 #define process_arg(fspec) \
480 /* Start real work. We know about all flags and modifiers and \
481 now process the wanted format specifier. */ \
482 LABEL (form_percent): \
483 /* Write a literal "%". */ \
484 outchar (L_('%')); \
485 break; \
487 LABEL (form_integer): \
488 /* Signed decimal integer. */ \
489 base = 10; \
491 if (is_longlong) \
493 long long int signed_number; \
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; \
500 is_negative = signed_number < 0; \
501 number.longlong = is_negative ? (- signed_number) : signed_number; \
503 goto LABEL (longlong_number); \
505 else \
507 long int signed_number; \
509 if (fspec == NULL) \
511 if (is_long_num) \
512 signed_number = va_arg (ap, long int); \
513 else if (is_char) \
514 signed_number = (signed char) va_arg (ap, unsigned int); \
515 else if (!is_short) \
516 signed_number = va_arg (ap, int); \
517 else \
518 signed_number = (short int) va_arg (ap, unsigned int); \
520 else \
521 if (is_long_num) \
522 signed_number = args_value[fspec->data_arg].pa_long_int; \
523 else if (is_char) \
524 signed_number = (signed char) \
525 args_value[fspec->data_arg].pa_u_int; \
526 else if (!is_short) \
527 signed_number = args_value[fspec->data_arg].pa_int; \
528 else \
529 signed_number = (short int) \
530 args_value[fspec->data_arg].pa_u_int; \
532 is_negative = signed_number < 0; \
533 number.word = is_negative ? (- signed_number) : signed_number; \
535 goto LABEL (number); \
537 /* NOTREACHED */ \
539 LABEL (form_unsigned): \
540 /* Unsigned decimal integer. */ \
541 base = 10; \
542 goto LABEL (unsigned_number); \
543 /* NOTREACHED */ \
545 LABEL (form_octal): \
546 /* Unsigned octal integer. */ \
547 base = 8; \
548 goto LABEL (unsigned_number); \
549 /* NOTREACHED */ \
551 LABEL (form_hexa): \
552 /* Unsigned hexadecimal integer. */ \
553 base = 16; \
555 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
557 /* ISO specifies the `+' and ` ' flags only for signed \
558 conversions. */ \
559 is_negative = 0; \
560 showsign = 0; \
561 space = 0; \
563 if (is_longlong) \
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; \
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. */ \
577 pad = L_(' '); \
579 /* If the precision is 0 and the number is 0 nothing has to \
580 be written for the number, except for the 'o' format in \
581 alternate form. */ \
582 if (prec == 0 && number.longlong == 0) \
584 string = workend; \
585 if (base == 8 && alt) \
586 *--string = L_('0'); \
588 else \
590 /* Put the number in WORK. */ \
591 string = _itoa (number.longlong, workend, base, \
592 spec == L_('X')); \
593 if (group && grouping) \
594 string = group_number (work_buffer, string, workend, \
595 grouping, thousands_sep); \
596 if (use_outdigits && base == 10) \
597 string = _i18n_number_rewrite (string, workend, workend); \
599 /* Simplify further test for num != 0. */ \
600 number.word = number.longlong != 0; \
602 else \
604 if (fspec == NULL) \
606 if (is_long_num) \
607 number.word = va_arg (ap, unsigned long int); \
608 else if (is_char) \
609 number.word = (unsigned char) va_arg (ap, unsigned int); \
610 else if (!is_short) \
611 number.word = va_arg (ap, unsigned int); \
612 else \
613 number.word = (unsigned short int) va_arg (ap, unsigned int); \
615 else \
616 if (is_long_num) \
617 number.word = args_value[fspec->data_arg].pa_u_long_int; \
618 else if (is_char) \
619 number.word = (unsigned char) \
620 args_value[fspec->data_arg].pa_u_int; \
621 else if (!is_short) \
622 number.word = args_value[fspec->data_arg].pa_u_int; \
623 else \
624 number.word = (unsigned short int) \
625 args_value[fspec->data_arg].pa_u_int; \
627 LABEL (number): \
628 if (prec < 0) \
629 /* Supply a default precision if none was given. */ \
630 prec = 1; \
631 else \
632 /* We have to take care for the '0' flag. If a precision \
633 is given it must be ignored. */ \
634 pad = L_(' '); \
636 /* If the precision is 0 and the number is 0 nothing has to \
637 be written for the number, except for the 'o' format in \
638 alternate form. */ \
639 if (prec == 0 && number.word == 0) \
641 string = workend; \
642 if (base == 8 && alt) \
643 *--string = L_('0'); \
645 else \
647 /* Put the number in WORK. */ \
648 string = _itoa_word (number.word, workend, base, \
649 spec == L_('X')); \
650 if (group && grouping) \
651 string = group_number (work_buffer, string, workend, \
652 grouping, thousands_sep); \
653 if (use_outdigits && base == 10) \
654 string = _i18n_number_rewrite (string, workend, workend); \
658 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
659 /* Add octal marker. */ \
660 *--string = L_('0'); \
662 prec = MAX (0, prec - (workend - string)); \
664 if (!left) \
666 width -= workend - string + prec; \
668 if (number.word != 0 && alt && base == 16) \
669 /* Account for 0X hex marker. */ \
670 width -= 2; \
672 if (is_negative || showsign || space) \
673 --width; \
675 if (pad == L_(' ')) \
677 PAD (L_(' ')); \
678 width = 0; \
681 if (is_negative) \
682 outchar (L_('-')); \
683 else if (showsign) \
684 outchar (L_('+')); \
685 else if (space) \
686 outchar (L_(' ')); \
688 if (number.word != 0 && alt && base == 16) \
690 outchar (L_('0')); \
691 outchar (spec); \
694 width += prec; \
695 PAD (L_('0')); \
697 outstring (string, workend - string); \
699 break; \
701 else \
703 if (is_negative) \
705 outchar (L_('-')); \
706 --width; \
708 else if (showsign) \
710 outchar (L_('+')); \
711 --width; \
713 else if (space) \
715 outchar (L_(' ')); \
716 --width; \
719 if (number.word != 0 && alt && base == 16) \
721 outchar (L_('0')); \
722 outchar (spec); \
723 width -= 2; \
726 width -= workend - string + prec; \
728 if (prec > 0) \
730 int temp = width; \
731 width = prec; \
732 PAD (L_('0')); \
733 width = temp; \
736 outstring (string, workend - string); \
738 PAD (L_(' ')); \
739 break; \
742 LABEL (form_float): \
744 /* Floating-point number. This is handled by printf_fp.c. */ \
745 const void *ptr; \
746 int function_done; \
748 if (fspec == NULL) \
750 if (__ldbl_is_dbl) \
751 is_long_double = 0; \
753 struct printf_info info = { .prec = prec, \
754 .width = width, \
755 .spec = spec, \
756 .is_long_double = is_long_double, \
757 .is_short = is_short, \
758 .is_long = is_long, \
759 .alt = alt, \
760 .space = space, \
761 .left = left, \
762 .showsign = showsign, \
763 .group = group, \
764 .pad = pad, \
765 .extra = 0, \
766 .i18n = use_outdigits, \
767 .wide = sizeof (CHAR_T) != 1, \
768 .is_binary128 = 0}; \
770 if (is_long_double) \
771 the_arg.pa_long_double = va_arg (ap, long double); \
772 else \
773 the_arg.pa_double = va_arg (ap, double); \
774 ptr = (const void *) &the_arg; \
776 function_done = __printf_fp (s, &info, &ptr); \
778 else \
780 ptr = (const void *) &args_value[fspec->data_arg]; \
781 if (__ldbl_is_dbl) \
783 fspec->data_arg_type = PA_DOUBLE; \
784 fspec->info.is_long_double = 0; \
786 /* Not supported by *printf functions. */ \
787 fspec->info.is_binary128 = 0; \
789 function_done = __printf_fp (s, &fspec->info, &ptr); \
792 if (function_done < 0) \
794 /* Error in print handler; up to handler to set errno. */ \
795 done = -1; \
796 goto all_done; \
799 done_add (function_done); \
801 break; \
803 LABEL (form_floathex): \
805 /* Floating point number printed as hexadecimal number. */ \
806 const void *ptr; \
807 int function_done; \
809 if (fspec == NULL) \
811 if (__ldbl_is_dbl) \
812 is_long_double = 0; \
814 struct printf_info info = { .prec = prec, \
815 .width = width, \
816 .spec = spec, \
817 .is_long_double = is_long_double, \
818 .is_short = is_short, \
819 .is_long = is_long, \
820 .alt = alt, \
821 .space = space, \
822 .left = left, \
823 .showsign = showsign, \
824 .group = group, \
825 .pad = pad, \
826 .extra = 0, \
827 .wide = sizeof (CHAR_T) != 1, \
828 .is_binary128 = 0}; \
830 if (is_long_double) \
831 the_arg.pa_long_double = va_arg (ap, long double); \
832 else \
833 the_arg.pa_double = va_arg (ap, double); \
834 ptr = (const void *) &the_arg; \
836 function_done = __printf_fphex (s, &info, &ptr); \
838 else \
840 ptr = (const void *) &args_value[fspec->data_arg]; \
841 if (__ldbl_is_dbl) \
842 fspec->info.is_long_double = 0; \
843 /* Not supported by *printf functions. */ \
844 fspec->info.is_binary128 = 0; \
846 function_done = __printf_fphex (s, &fspec->info, &ptr); \
849 if (function_done < 0) \
851 /* Error in print handler; up to handler to set errno. */ \
852 done = -1; \
853 goto all_done; \
856 done_add (function_done); \
858 break; \
860 LABEL (form_pointer): \
861 /* Generic pointer. */ \
863 const void *ptr; \
864 if (fspec == NULL) \
865 ptr = va_arg (ap, void *); \
866 else \
867 ptr = args_value[fspec->data_arg].pa_pointer; \
868 if (ptr != NULL) \
870 /* If the pointer is not NULL, write it as a %#x spec. */ \
871 base = 16; \
872 number.word = (unsigned long int) ptr; \
873 is_negative = 0; \
874 alt = 1; \
875 group = 0; \
876 spec = L_('x'); \
877 goto LABEL (number); \
879 else \
881 /* Write "(nil)" for a nil pointer. */ \
882 string = (CHAR_T *) L_("(nil)"); \
883 /* Make sure the full string "(nil)" is printed. */ \
884 if (prec < 5) \
885 prec = 5; \
886 /* This is a wide string iff compiling wprintf. */ \
887 is_long = sizeof (CHAR_T) > 1; \
888 goto LABEL (print_string); \
891 /* NOTREACHED */ \
893 LABEL (form_number): \
894 if (s->_flags2 & _IO_FLAGS2_FORTIFY) \
896 if (! readonly_format) \
898 extern int __readonly_area (const void *, size_t) \
899 attribute_hidden; \
900 readonly_format \
901 = __readonly_area (format, ((STR_LEN (format) + 1) \
902 * sizeof (CHAR_T))); \
904 if (readonly_format < 0) \
905 __libc_fatal ("*** %n in writable segment detected ***\n"); \
907 /* Answer the count of characters written. */ \
908 if (fspec == NULL) \
910 if (is_longlong) \
911 *(long long int *) va_arg (ap, void *) = done; \
912 else if (is_long_num) \
913 *(long int *) va_arg (ap, void *) = done; \
914 else if (is_char) \
915 *(char *) va_arg (ap, void *) = done; \
916 else if (!is_short) \
917 *(int *) va_arg (ap, void *) = done; \
918 else \
919 *(short int *) va_arg (ap, void *) = done; \
921 else \
922 if (is_longlong) \
923 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
924 else if (is_long_num) \
925 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
926 else if (is_char) \
927 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
928 else if (!is_short) \
929 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
930 else \
931 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
932 break; \
934 LABEL (form_strerror): \
935 /* Print description of error ERRNO. */ \
936 string = \
937 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
938 WORK_BUFFER_SIZE * sizeof (CHAR_T)); \
939 is_long = 0; /* This is no wide-char string. */ \
940 goto LABEL (print_string)
942 #ifdef COMPILE_WPRINTF
943 # define process_string_arg(fspec) \
944 LABEL (form_character): \
945 /* Character. */ \
946 if (is_long) \
947 goto LABEL (form_wcharacter); \
948 --width; /* Account for the character itself. */ \
949 if (!left) \
950 PAD (L' '); \
951 if (fspec == NULL) \
952 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
953 else \
954 outchar (__btowc ((unsigned char) \
955 args_value[fspec->data_arg].pa_int)); \
956 if (left) \
957 PAD (L' '); \
958 break; \
960 LABEL (form_wcharacter): \
962 /* Wide character. */ \
963 --width; \
964 if (!left) \
965 PAD (L' '); \
966 if (fspec == NULL) \
967 outchar (va_arg (ap, wchar_t)); \
968 else \
969 outchar (args_value[fspec->data_arg].pa_wchar); \
970 if (left) \
971 PAD (L' '); \
973 break; \
975 LABEL (form_string): \
977 size_t len; \
978 int string_malloced; \
980 /* The string argument could in fact be `char *' or `wchar_t *'. \
981 But this should not make a difference here. */ \
982 if (fspec == NULL) \
983 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
984 else \
985 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
987 /* Entry point for printing other strings. */ \
988 LABEL (print_string): \
990 string_malloced = 0; \
991 if (string == NULL) \
993 /* Write "(null)" if there's space. */ \
994 if (prec == -1 || prec >= (int) array_length (null) - 1) \
996 string = (CHAR_T *) null; \
997 len = array_length (null) - 1; \
999 else \
1001 string = (CHAR_T *) L""; \
1002 len = 0; \
1005 else if (!is_long && spec != L_('S')) \
1007 /* This is complicated. We have to transform the multibyte \
1008 string into a wide character string. */ \
1009 const char *mbs = (const char *) string; \
1010 mbstate_t mbstate; \
1012 len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
1014 /* Allocate dynamically an array which definitely is long \
1015 enough for the wide character version. Each byte in the \
1016 multi-byte string can produce at most one wide character. */ \
1017 if (__glibc_unlikely (len > SIZE_MAX / sizeof (wchar_t))) \
1019 __set_errno (EOVERFLOW); \
1020 done = -1; \
1021 goto all_done; \
1023 else if (__libc_use_alloca (len * sizeof (wchar_t))) \
1024 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1025 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1026 == NULL) \
1028 done = -1; \
1029 goto all_done; \
1031 else \
1032 string_malloced = 1; \
1034 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1035 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1036 if (len == (size_t) -1) \
1038 /* Illegal multibyte character. */ \
1039 done = -1; \
1040 goto all_done; \
1043 else \
1045 if (prec != -1) \
1046 /* Search for the end of the string, but don't search past \
1047 the length specified by the precision. */ \
1048 len = __wcsnlen (string, prec); \
1049 else \
1050 len = __wcslen (string); \
1053 if ((width -= len) < 0) \
1055 outstring (string, len); \
1056 break; \
1059 if (!left) \
1060 PAD (L' '); \
1061 outstring (string, len); \
1062 if (left) \
1063 PAD (L' '); \
1064 if (__glibc_unlikely (string_malloced)) \
1065 free (string); \
1067 break;
1068 #else
1069 # define process_string_arg(fspec) \
1070 LABEL (form_character): \
1071 /* Character. */ \
1072 if (is_long) \
1073 goto LABEL (form_wcharacter); \
1074 --width; /* Account for the character itself. */ \
1075 if (!left) \
1076 PAD (' '); \
1077 if (fspec == NULL) \
1078 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1079 else \
1080 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1081 if (left) \
1082 PAD (' '); \
1083 break; \
1085 LABEL (form_wcharacter): \
1087 /* Wide character. */ \
1088 char buf[MB_LEN_MAX]; \
1089 mbstate_t mbstate; \
1090 size_t len; \
1092 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1093 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1094 : args_value[fspec->data_arg].pa_wchar), \
1095 &mbstate); \
1096 if (len == (size_t) -1) \
1098 /* Something went wrong during the conversion. Bail out. */ \
1099 done = -1; \
1100 goto all_done; \
1102 width -= len; \
1103 if (!left) \
1104 PAD (' '); \
1105 outstring (buf, len); \
1106 if (left) \
1107 PAD (' '); \
1109 break; \
1111 LABEL (form_string): \
1113 size_t len; \
1114 int string_malloced; \
1116 /* The string argument could in fact be `char *' or `wchar_t *'. \
1117 But this should not make a difference here. */ \
1118 if (fspec == NULL) \
1119 string = (char *) va_arg (ap, const char *); \
1120 else \
1121 string = (char *) args_value[fspec->data_arg].pa_string; \
1123 /* Entry point for printing other strings. */ \
1124 LABEL (print_string): \
1126 string_malloced = 0; \
1127 if (string == NULL) \
1129 /* Write "(null)" if there's space. */ \
1130 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1132 string = (char *) null; \
1133 len = sizeof (null) - 1; \
1135 else \
1137 string = (char *) ""; \
1138 len = 0; \
1141 else if (!is_long && spec != L_('S')) \
1143 if (prec != -1) \
1144 /* Search for the end of the string, but don't search past \
1145 the length (in bytes) specified by the precision. */ \
1146 len = __strnlen (string, prec); \
1147 else \
1148 len = strlen (string); \
1150 else \
1152 const wchar_t *s2 = (const wchar_t *) string; \
1153 mbstate_t mbstate; \
1155 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1157 if (prec >= 0) \
1159 /* The string `s2' might not be NUL terminated. */ \
1160 if (__libc_use_alloca (prec)) \
1161 string = (char *) alloca (prec); \
1162 else if ((string = (char *) malloc (prec)) == NULL) \
1164 done = -1; \
1165 goto all_done; \
1167 else \
1168 string_malloced = 1; \
1169 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1171 else \
1173 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1174 if (len != (size_t) -1) \
1176 assert (__mbsinit (&mbstate)); \
1177 s2 = (const wchar_t *) string; \
1178 if (__libc_use_alloca (len + 1)) \
1179 string = (char *) alloca (len + 1); \
1180 else if ((string = (char *) malloc (len + 1)) == NULL) \
1182 done = -1; \
1183 goto all_done; \
1185 else \
1186 string_malloced = 1; \
1187 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1191 if (len == (size_t) -1) \
1193 /* Illegal wide-character string. */ \
1194 done = -1; \
1195 goto all_done; \
1199 if ((width -= len) < 0) \
1201 outstring (string, len); \
1202 break; \
1205 if (!left) \
1206 PAD (' '); \
1207 outstring (string, len); \
1208 if (left) \
1209 PAD (' '); \
1210 if (__glibc_unlikely (string_malloced)) \
1211 free (string); \
1213 break;
1214 #endif
1216 /* Helper function to provide temporary buffering for unbuffered streams. */
1217 static int buffered_vfprintf (FILE *stream, const CHAR_T *fmt, va_list)
1218 __THROW __attribute__ ((noinline));
1220 /* Handle positional format specifiers. */
1221 static int printf_positional (FILE *s,
1222 const CHAR_T *format, int readonly_format,
1223 va_list ap, va_list *ap_savep, int done,
1224 int nspecs_done, const UCHAR_T *lead_str_end,
1225 CHAR_T *work_buffer, int save_errno,
1226 const char *grouping, THOUSANDS_SEP_T);
1228 /* Handle unknown format specifier. */
1229 static int printf_unknown (FILE *, const struct printf_info *,
1230 const void *const *) __THROW;
1232 /* Group digits of number string. */
1233 static CHAR_T *group_number (CHAR_T *, CHAR_T *, CHAR_T *, const char *,
1234 THOUSANDS_SEP_T);
1236 /* The function itself. */
1238 vfprintf (FILE *s, const CHAR_T *format, va_list ap)
1240 /* The character used as thousands separator. */
1241 THOUSANDS_SEP_T thousands_sep = 0;
1243 /* The string describing the size of groups of digits. */
1244 const char *grouping;
1246 /* Place to accumulate the result. */
1247 int done;
1249 /* Current character in format string. */
1250 const UCHAR_T *f;
1252 /* End of leading constant string. */
1253 const UCHAR_T *lead_str_end;
1255 /* Points to next format specifier. */
1256 const UCHAR_T *end_of_spec;
1258 /* Buffer intermediate results. */
1259 CHAR_T work_buffer[WORK_BUFFER_SIZE];
1260 CHAR_T *workstart = NULL;
1261 CHAR_T *workend;
1263 /* We have to save the original argument pointer. */
1264 va_list ap_save;
1266 /* Count number of specifiers we already processed. */
1267 int nspecs_done;
1269 /* For the %m format we may need the current `errno' value. */
1270 int save_errno = errno;
1272 /* 1 if format is in read-only memory, -1 if it is in writable memory,
1273 0 if unknown. */
1274 int readonly_format = 0;
1276 /* Orient the stream. */
1277 #ifdef ORIENT
1278 ORIENT;
1279 #endif
1281 /* Sanity check of arguments. */
1282 ARGCHECK (s, format);
1284 #ifdef ORIENT
1285 /* Check for correct orientation. */
1286 if (_IO_vtable_offset (s) == 0 &&
1287 _IO_fwide (s, sizeof (CHAR_T) == 1 ? -1 : 1)
1288 != (sizeof (CHAR_T) == 1 ? -1 : 1))
1289 /* The stream is already oriented otherwise. */
1290 return EOF;
1291 #endif
1293 if (UNBUFFERED_P (s))
1294 /* Use a helper function which will allocate a local temporary buffer
1295 for the stream and then call us again. */
1296 return buffered_vfprintf (s, format, ap);
1298 /* Initialize local variables. */
1299 done = 0;
1300 grouping = (const char *) -1;
1301 #ifdef __va_copy
1302 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1303 since on some systems `va_list' is not an integral type. */
1304 __va_copy (ap_save, ap);
1305 #else
1306 ap_save = ap;
1307 #endif
1308 nspecs_done = 0;
1310 #ifdef COMPILE_WPRINTF
1311 /* Find the first format specifier. */
1312 f = lead_str_end = __find_specwc ((const UCHAR_T *) format);
1313 #else
1314 /* Find the first format specifier. */
1315 f = lead_str_end = __find_specmb ((const UCHAR_T *) format);
1316 #endif
1318 /* Lock stream. */
1319 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
1320 _IO_flockfile (s);
1322 /* Write the literal text before the first format. */
1323 outstring ((const UCHAR_T *) format,
1324 lead_str_end - (const UCHAR_T *) format);
1326 /* If we only have to print a simple string, return now. */
1327 if (*f == L_('\0'))
1328 goto all_done;
1330 /* Use the slow path in case any printf handler is registered. */
1331 if (__glibc_unlikely (__printf_function_table != NULL
1332 || __printf_modifier_table != NULL
1333 || __printf_va_arg_table != NULL))
1334 goto do_positional;
1336 /* Process whole format string. */
1339 STEP0_3_TABLE;
1340 STEP4_TABLE;
1342 union printf_arg *args_value; /* This is not used here but ... */
1343 int is_negative; /* Flag for negative number. */
1344 union
1346 unsigned long long int longlong;
1347 unsigned long int word;
1348 } number;
1349 int base;
1350 union printf_arg the_arg;
1351 CHAR_T *string; /* Pointer to argument string. */
1352 int alt = 0; /* Alternate format. */
1353 int space = 0; /* Use space prefix if no sign is needed. */
1354 int left = 0; /* Left-justify output. */
1355 int showsign = 0; /* Always begin with plus or minus sign. */
1356 int group = 0; /* Print numbers according grouping rules. */
1357 int is_long_double = 0; /* Argument is long double/ long long int. */
1358 int is_short = 0; /* Argument is short int. */
1359 int is_long = 0; /* Argument is long int. */
1360 int is_char = 0; /* Argument is promoted (unsigned) char. */
1361 int width = 0; /* Width of output; 0 means none specified. */
1362 int prec = -1; /* Precision of output; -1 means none specified. */
1363 /* This flag is set by the 'I' modifier and selects the use of the
1364 `outdigits' as determined by the current locale. */
1365 int use_outdigits = 0;
1366 UCHAR_T pad = L_(' ');/* Padding character. */
1367 CHAR_T spec;
1369 workstart = NULL;
1370 workend = work_buffer + WORK_BUFFER_SIZE;
1372 /* Get current character in format string. */
1373 JUMP (*++f, step0_jumps);
1375 /* ' ' flag. */
1376 LABEL (flag_space):
1377 space = 1;
1378 JUMP (*++f, step0_jumps);
1380 /* '+' flag. */
1381 LABEL (flag_plus):
1382 showsign = 1;
1383 JUMP (*++f, step0_jumps);
1385 /* The '-' flag. */
1386 LABEL (flag_minus):
1387 left = 1;
1388 pad = L_(' ');
1389 JUMP (*++f, step0_jumps);
1391 /* The '#' flag. */
1392 LABEL (flag_hash):
1393 alt = 1;
1394 JUMP (*++f, step0_jumps);
1396 /* The '0' flag. */
1397 LABEL (flag_zero):
1398 if (!left)
1399 pad = L_('0');
1400 JUMP (*++f, step0_jumps);
1402 /* The '\'' flag. */
1403 LABEL (flag_quote):
1404 group = 1;
1406 if (grouping == (const char *) -1)
1408 #ifdef COMPILE_WPRINTF
1409 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1410 _NL_NUMERIC_THOUSANDS_SEP_WC);
1411 #else
1412 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1413 #endif
1415 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1416 if (*grouping == '\0' || *grouping == CHAR_MAX
1417 #ifdef COMPILE_WPRINTF
1418 || thousands_sep == L'\0'
1419 #else
1420 || *thousands_sep == '\0'
1421 #endif
1423 grouping = NULL;
1425 JUMP (*++f, step0_jumps);
1427 LABEL (flag_i18n):
1428 use_outdigits = 1;
1429 JUMP (*++f, step0_jumps);
1431 /* Get width from argument. */
1432 LABEL (width_asterics):
1434 const UCHAR_T *tmp; /* Temporary value. */
1436 tmp = ++f;
1437 if (ISDIGIT (*tmp))
1439 int pos = read_int (&tmp);
1441 if (pos == -1)
1443 __set_errno (EOVERFLOW);
1444 done = -1;
1445 goto all_done;
1448 if (pos && *tmp == L_('$'))
1449 /* The width comes from a positional parameter. */
1450 goto do_positional;
1452 width = va_arg (ap, int);
1454 /* Negative width means left justified. */
1455 if (width < 0)
1457 width = -width;
1458 pad = L_(' ');
1459 left = 1;
1462 if (__glibc_unlikely (width >= INT_MAX / sizeof (CHAR_T) - EXTSIZ))
1464 __set_errno (EOVERFLOW);
1465 done = -1;
1466 goto all_done;
1469 if (width >= WORK_BUFFER_SIZE - EXTSIZ)
1471 /* We have to use a special buffer. */
1472 size_t needed = ((size_t) width + EXTSIZ) * sizeof (CHAR_T);
1473 if (__libc_use_alloca (needed))
1474 workend = (CHAR_T *) alloca (needed) + width + EXTSIZ;
1475 else
1477 workstart = (CHAR_T *) malloc (needed);
1478 if (workstart == NULL)
1480 done = -1;
1481 goto all_done;
1483 workend = workstart + width + EXTSIZ;
1487 JUMP (*f, step1_jumps);
1489 /* Given width in format string. */
1490 LABEL (width):
1491 width = read_int (&f);
1493 if (__glibc_unlikely (width == -1
1494 || width >= INT_MAX / sizeof (CHAR_T) - EXTSIZ))
1496 __set_errno (EOVERFLOW);
1497 done = -1;
1498 goto all_done;
1501 if (width >= WORK_BUFFER_SIZE - EXTSIZ)
1503 /* We have to use a special buffer. */
1504 size_t needed = ((size_t) width + EXTSIZ) * sizeof (CHAR_T);
1505 if (__libc_use_alloca (needed))
1506 workend = (CHAR_T *) alloca (needed) + width + EXTSIZ;
1507 else
1509 workstart = (CHAR_T *) malloc (needed);
1510 if (workstart == NULL)
1512 done = -1;
1513 goto all_done;
1515 workend = workstart + width + EXTSIZ;
1518 if (*f == L_('$'))
1519 /* Oh, oh. The argument comes from a positional parameter. */
1520 goto do_positional;
1521 JUMP (*f, step1_jumps);
1523 LABEL (precision):
1524 ++f;
1525 if (*f == L_('*'))
1527 const UCHAR_T *tmp; /* Temporary value. */
1529 tmp = ++f;
1530 if (ISDIGIT (*tmp))
1532 int pos = read_int (&tmp);
1534 if (pos == -1)
1536 __set_errno (EOVERFLOW);
1537 done = -1;
1538 goto all_done;
1541 if (pos && *tmp == L_('$'))
1542 /* The precision comes from a positional parameter. */
1543 goto do_positional;
1545 prec = va_arg (ap, int);
1547 /* If the precision is negative the precision is omitted. */
1548 if (prec < 0)
1549 prec = -1;
1551 else if (ISDIGIT (*f))
1553 prec = read_int (&f);
1555 /* The precision was specified in this case as an extremely
1556 large positive value. */
1557 if (prec == -1)
1559 __set_errno (EOVERFLOW);
1560 done = -1;
1561 goto all_done;
1564 else
1565 prec = 0;
1566 if (prec > width && prec > WORK_BUFFER_SIZE - EXTSIZ)
1568 /* Deallocate any previously allocated buffer because it is
1569 too small. */
1570 if (__glibc_unlikely (workstart != NULL))
1571 free (workstart);
1572 workstart = NULL;
1573 if (__glibc_unlikely (prec >= INT_MAX / sizeof (CHAR_T) - EXTSIZ))
1575 __set_errno (EOVERFLOW);
1576 done = -1;
1577 goto all_done;
1579 size_t needed = ((size_t) prec + EXTSIZ) * sizeof (CHAR_T);
1581 if (__libc_use_alloca (needed))
1582 workend = (CHAR_T *) alloca (needed) + prec + EXTSIZ;
1583 else
1585 workstart = (CHAR_T *) malloc (needed);
1586 if (workstart == NULL)
1588 done = -1;
1589 goto all_done;
1591 workend = workstart + prec + EXTSIZ;
1594 JUMP (*f, step2_jumps);
1596 /* Process 'h' modifier. There might another 'h' following. */
1597 LABEL (mod_half):
1598 is_short = 1;
1599 JUMP (*++f, step3a_jumps);
1601 /* Process 'hh' modifier. */
1602 LABEL (mod_halfhalf):
1603 is_short = 0;
1604 is_char = 1;
1605 JUMP (*++f, step4_jumps);
1607 /* Process 'l' modifier. There might another 'l' following. */
1608 LABEL (mod_long):
1609 is_long = 1;
1610 JUMP (*++f, step3b_jumps);
1612 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1613 allowed to follow. */
1614 LABEL (mod_longlong):
1615 is_long_double = 1;
1616 is_long = 1;
1617 JUMP (*++f, step4_jumps);
1619 LABEL (mod_size_t):
1620 is_long_double = sizeof (size_t) > sizeof (unsigned long int);
1621 is_long = sizeof (size_t) > sizeof (unsigned int);
1622 JUMP (*++f, step4_jumps);
1624 LABEL (mod_ptrdiff_t):
1625 is_long_double = sizeof (ptrdiff_t) > sizeof (unsigned long int);
1626 is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
1627 JUMP (*++f, step4_jumps);
1629 LABEL (mod_intmax_t):
1630 is_long_double = sizeof (intmax_t) > sizeof (unsigned long int);
1631 is_long = sizeof (intmax_t) > sizeof (unsigned int);
1632 JUMP (*++f, step4_jumps);
1634 /* Process current format. */
1635 while (1)
1637 process_arg (((struct printf_spec *) NULL));
1638 process_string_arg (((struct printf_spec *) NULL));
1640 LABEL (form_unknown):
1641 if (spec == L_('\0'))
1643 /* The format string ended before the specifier is complete. */
1644 __set_errno (EINVAL);
1645 done = -1;
1646 goto all_done;
1649 /* If we are in the fast loop force entering the complicated
1650 one. */
1651 goto do_positional;
1654 /* The format is correctly handled. */
1655 ++nspecs_done;
1657 if (__glibc_unlikely (workstart != NULL))
1658 free (workstart);
1659 workstart = NULL;
1661 /* Look for next format specifier. */
1662 #ifdef COMPILE_WPRINTF
1663 f = __find_specwc ((end_of_spec = ++f));
1664 #else
1665 f = __find_specmb ((end_of_spec = ++f));
1666 #endif
1668 /* Write the following constant string. */
1669 outstring (end_of_spec, f - end_of_spec);
1671 while (*f != L_('\0'));
1673 /* Unlock stream and return. */
1674 goto all_done;
1676 /* Hand off processing for positional parameters. */
1677 do_positional:
1678 if (__glibc_unlikely (workstart != NULL))
1680 free (workstart);
1681 workstart = NULL;
1683 done = printf_positional (s, format, readonly_format, ap, &ap_save,
1684 done, nspecs_done, lead_str_end, work_buffer,
1685 save_errno, grouping, thousands_sep);
1687 all_done:
1688 if (__glibc_unlikely (workstart != NULL))
1689 free (workstart);
1690 /* Unlock the stream. */
1691 _IO_funlockfile (s);
1692 _IO_cleanup_region_end (0);
1694 return done;
1697 static int
1698 printf_positional (FILE *s, const CHAR_T *format, int readonly_format,
1699 va_list ap, va_list *ap_savep, int done, int nspecs_done,
1700 const UCHAR_T *lead_str_end,
1701 CHAR_T *work_buffer, int save_errno,
1702 const char *grouping, THOUSANDS_SEP_T thousands_sep)
1704 /* For positional argument handling. */
1705 struct scratch_buffer specsbuf;
1706 scratch_buffer_init (&specsbuf);
1707 struct printf_spec *specs = specsbuf.data;
1708 size_t specs_limit = specsbuf.length / sizeof (specs[0]);
1710 /* Used as a backing store for args_value, args_size, args_type
1711 below. */
1712 struct scratch_buffer argsbuf;
1713 scratch_buffer_init (&argsbuf);
1715 /* Array with information about the needed arguments. This has to
1716 be dynamically extensible. */
1717 size_t nspecs = 0;
1719 /* The number of arguments the format string requests. This will
1720 determine the size of the array needed to store the argument
1721 attributes. */
1722 size_t nargs = 0;
1724 /* Positional parameters refer to arguments directly. This could
1725 also determine the maximum number of arguments. Track the
1726 maximum number. */
1727 size_t max_ref_arg = 0;
1729 /* Just a counter. */
1730 size_t cnt;
1732 CHAR_T *workstart = NULL;
1734 if (grouping == (const char *) -1)
1736 #ifdef COMPILE_WPRINTF
1737 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1738 _NL_NUMERIC_THOUSANDS_SEP_WC);
1739 #else
1740 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1741 #endif
1743 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1744 if (*grouping == '\0' || *grouping == CHAR_MAX)
1745 grouping = NULL;
1748 for (const UCHAR_T *f = lead_str_end; *f != L_('\0');
1749 f = specs[nspecs++].next_fmt)
1751 if (nspecs == specs_limit)
1753 if (!scratch_buffer_grow_preserve (&specsbuf))
1755 done = -1;
1756 goto all_done;
1758 specs = specsbuf.data;
1759 specs_limit = specsbuf.length / sizeof (specs[0]);
1762 /* Parse the format specifier. */
1763 #ifdef COMPILE_WPRINTF
1764 nargs += __parse_one_specwc (f, nargs, &specs[nspecs], &max_ref_arg);
1765 #else
1766 nargs += __parse_one_specmb (f, nargs, &specs[nspecs], &max_ref_arg);
1767 #endif
1770 /* Determine the number of arguments the format string consumes. */
1771 nargs = MAX (nargs, max_ref_arg);
1773 union printf_arg *args_value;
1774 int *args_size;
1775 int *args_type;
1777 /* Calculate total size needed to represent a single argument
1778 across all three argument-related arrays. */
1779 size_t bytes_per_arg
1780 = sizeof (*args_value) + sizeof (*args_size) + sizeof (*args_type);
1781 if (!scratch_buffer_set_array_size (&argsbuf, nargs, bytes_per_arg))
1783 done = -1;
1784 goto all_done;
1786 args_value = argsbuf.data;
1787 /* Set up the remaining two arrays to each point past the end of
1788 the prior array, since space for all three has been allocated
1789 now. */
1790 args_size = &args_value[nargs].pa_int;
1791 args_type = &args_size[nargs];
1792 memset (args_type, s->_flags2 & _IO_FLAGS2_FORTIFY ? '\xff' : '\0',
1793 nargs * sizeof (*args_type));
1796 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1797 still zero after this loop, format is invalid. For now we
1798 simply use 0 as the value. */
1800 /* Fill in the types of all the arguments. */
1801 for (cnt = 0; cnt < nspecs; ++cnt)
1803 /* If the width is determined by an argument this is an int. */
1804 if (specs[cnt].width_arg != -1)
1805 args_type[specs[cnt].width_arg] = PA_INT;
1807 /* If the precision is determined by an argument this is an int. */
1808 if (specs[cnt].prec_arg != -1)
1809 args_type[specs[cnt].prec_arg] = PA_INT;
1811 switch (specs[cnt].ndata_args)
1813 case 0: /* No arguments. */
1814 break;
1815 case 1: /* One argument; we already have the
1816 type and size. */
1817 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1818 args_size[specs[cnt].data_arg] = specs[cnt].size;
1819 break;
1820 default:
1821 /* We have more than one argument for this format spec.
1822 We must call the arginfo function again to determine
1823 all the types. */
1824 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1825 (&specs[cnt].info,
1826 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg],
1827 &args_size[specs[cnt].data_arg]);
1828 break;
1832 /* Now we know all the types and the order. Fill in the argument
1833 values. */
1834 for (cnt = 0; cnt < nargs; ++cnt)
1835 switch (args_type[cnt])
1837 #define T(tag, mem, type) \
1838 case tag: \
1839 args_value[cnt].mem = va_arg (*ap_savep, type); \
1840 break
1842 T (PA_WCHAR, pa_wchar, wint_t);
1843 case PA_CHAR: /* Promoted. */
1844 case PA_INT|PA_FLAG_SHORT: /* Promoted. */
1845 #if LONG_MAX == INT_MAX
1846 case PA_INT|PA_FLAG_LONG:
1847 #endif
1848 T (PA_INT, pa_int, int);
1849 #if LONG_MAX == LONG_LONG_MAX
1850 case PA_INT|PA_FLAG_LONG:
1851 #endif
1852 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1853 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1854 # error "he?"
1855 #endif
1856 case PA_FLOAT: /* Promoted. */
1857 T (PA_DOUBLE, pa_double, double);
1858 case PA_DOUBLE|PA_FLAG_LONG_DOUBLE:
1859 if (__ldbl_is_dbl)
1861 args_value[cnt].pa_double = va_arg (*ap_savep, double);
1862 args_type[cnt] &= ~PA_FLAG_LONG_DOUBLE;
1864 else
1865 args_value[cnt].pa_long_double = va_arg (*ap_savep, long double);
1866 break;
1867 case PA_STRING: /* All pointers are the same */
1868 case PA_WSTRING: /* All pointers are the same */
1869 T (PA_POINTER, pa_pointer, void *);
1870 #undef T
1871 default:
1872 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1873 args_value[cnt].pa_pointer = va_arg (*ap_savep, void *);
1874 else if (__glibc_unlikely (__printf_va_arg_table != NULL)
1875 && __printf_va_arg_table[args_type[cnt] - PA_LAST] != NULL)
1877 args_value[cnt].pa_user = alloca (args_size[cnt]);
1878 (*__printf_va_arg_table[args_type[cnt] - PA_LAST])
1879 (args_value[cnt].pa_user, ap_savep);
1881 else
1882 args_value[cnt].pa_long_double = 0.0;
1883 break;
1884 case -1:
1885 /* Error case. Not all parameters appear in N$ format
1886 strings. We have no way to determine their type. */
1887 assert (s->_flags2 & _IO_FLAGS2_FORTIFY);
1888 __libc_fatal ("*** invalid %N$ use detected ***\n");
1891 /* Now walk through all format specifiers and process them. */
1892 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1894 STEP4_TABLE;
1896 int is_negative;
1897 union
1899 unsigned long long int longlong;
1900 unsigned long int word;
1901 } number;
1902 int base;
1903 union printf_arg the_arg;
1904 CHAR_T *string; /* Pointer to argument string. */
1906 /* Fill variables from values in struct. */
1907 int alt = specs[nspecs_done].info.alt;
1908 int space = specs[nspecs_done].info.space;
1909 int left = specs[nspecs_done].info.left;
1910 int showsign = specs[nspecs_done].info.showsign;
1911 int group = specs[nspecs_done].info.group;
1912 int is_long_double = specs[nspecs_done].info.is_long_double;
1913 int is_short = specs[nspecs_done].info.is_short;
1914 int is_char = specs[nspecs_done].info.is_char;
1915 int is_long = specs[nspecs_done].info.is_long;
1916 int width = specs[nspecs_done].info.width;
1917 int prec = specs[nspecs_done].info.prec;
1918 int use_outdigits = specs[nspecs_done].info.i18n;
1919 char pad = specs[nspecs_done].info.pad;
1920 CHAR_T spec = specs[nspecs_done].info.spec;
1922 workstart = NULL;
1923 CHAR_T *workend = work_buffer + WORK_BUFFER_SIZE;
1925 /* Fill in last information. */
1926 if (specs[nspecs_done].width_arg != -1)
1928 /* Extract the field width from an argument. */
1929 specs[nspecs_done].info.width =
1930 args_value[specs[nspecs_done].width_arg].pa_int;
1932 if (specs[nspecs_done].info.width < 0)
1933 /* If the width value is negative left justification is
1934 selected and the value is taken as being positive. */
1936 specs[nspecs_done].info.width *= -1;
1937 left = specs[nspecs_done].info.left = 1;
1939 width = specs[nspecs_done].info.width;
1942 if (specs[nspecs_done].prec_arg != -1)
1944 /* Extract the precision from an argument. */
1945 specs[nspecs_done].info.prec =
1946 args_value[specs[nspecs_done].prec_arg].pa_int;
1948 if (specs[nspecs_done].info.prec < 0)
1949 /* If the precision is negative the precision is
1950 omitted. */
1951 specs[nspecs_done].info.prec = -1;
1953 prec = specs[nspecs_done].info.prec;
1956 /* Maybe the buffer is too small. */
1957 if (MAX (prec, width) + EXTSIZ > WORK_BUFFER_SIZE)
1959 if (__libc_use_alloca ((MAX (prec, width) + EXTSIZ)
1960 * sizeof (CHAR_T)))
1961 workend = ((CHAR_T *) alloca ((MAX (prec, width) + EXTSIZ)
1962 * sizeof (CHAR_T))
1963 + (MAX (prec, width) + EXTSIZ));
1964 else
1966 workstart = (CHAR_T *) malloc ((MAX (prec, width) + EXTSIZ)
1967 * sizeof (CHAR_T));
1968 if (workstart == NULL)
1970 done = -1;
1971 goto all_done;
1973 workend = workstart + (MAX (prec, width) + EXTSIZ);
1977 /* Process format specifiers. */
1978 while (1)
1980 extern printf_function **__printf_function_table;
1981 int function_done;
1983 if (spec <= UCHAR_MAX
1984 && __printf_function_table != NULL
1985 && __printf_function_table[(size_t) spec] != NULL)
1987 const void **ptr = alloca (specs[nspecs_done].ndata_args
1988 * sizeof (const void *));
1990 /* Fill in an array of pointers to the argument values. */
1991 for (unsigned int i = 0; i < specs[nspecs_done].ndata_args;
1992 ++i)
1993 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1995 /* Call the function. */
1996 function_done = __printf_function_table[(size_t) spec]
1997 (s, &specs[nspecs_done].info, ptr);
1999 if (function_done != -2)
2001 /* If an error occurred we don't have information
2002 about # of chars. */
2003 if (function_done < 0)
2005 /* Function has set errno. */
2006 done = -1;
2007 goto all_done;
2010 done_add (function_done);
2011 break;
2015 JUMP (spec, step4_jumps);
2017 process_arg ((&specs[nspecs_done]));
2018 process_string_arg ((&specs[nspecs_done]));
2020 LABEL (form_unknown):
2022 unsigned int i;
2023 const void **ptr;
2025 ptr = alloca (specs[nspecs_done].ndata_args
2026 * sizeof (const void *));
2028 /* Fill in an array of pointers to the argument values. */
2029 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
2030 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
2032 /* Call the function. */
2033 function_done = printf_unknown (s, &specs[nspecs_done].info,
2034 ptr);
2036 /* If an error occurred we don't have information about #
2037 of chars. */
2038 if (function_done < 0)
2040 /* Function has set errno. */
2041 done = -1;
2042 goto all_done;
2045 done_add (function_done);
2047 break;
2050 if (__glibc_unlikely (workstart != NULL))
2051 free (workstart);
2052 workstart = NULL;
2054 /* Write the following constant string. */
2055 outstring (specs[nspecs_done].end_of_fmt,
2056 specs[nspecs_done].next_fmt
2057 - specs[nspecs_done].end_of_fmt);
2059 all_done:
2060 if (__glibc_unlikely (workstart != NULL))
2061 free (workstart);
2062 scratch_buffer_free (&argsbuf);
2063 scratch_buffer_free (&specsbuf);
2064 return done;
2067 /* Handle an unknown format specifier. This prints out a canonicalized
2068 representation of the format spec itself. */
2069 static int
2070 printf_unknown (FILE *s, const struct printf_info *info,
2071 const void *const *args)
2074 int done = 0;
2075 CHAR_T work_buffer[MAX (sizeof (info->width), sizeof (info->prec)) * 3];
2076 CHAR_T *const workend
2077 = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
2078 CHAR_T *w;
2080 outchar (L_('%'));
2082 if (info->alt)
2083 outchar (L_('#'));
2084 if (info->group)
2085 outchar (L_('\''));
2086 if (info->showsign)
2087 outchar (L_('+'));
2088 else if (info->space)
2089 outchar (L_(' '));
2090 if (info->left)
2091 outchar (L_('-'));
2092 if (info->pad == L_('0'))
2093 outchar (L_('0'));
2094 if (info->i18n)
2095 outchar (L_('I'));
2097 if (info->width != 0)
2099 w = _itoa_word (info->width, workend, 10, 0);
2100 while (w < workend)
2101 outchar (*w++);
2104 if (info->prec != -1)
2106 outchar (L_('.'));
2107 w = _itoa_word (info->prec, workend, 10, 0);
2108 while (w < workend)
2109 outchar (*w++);
2112 if (info->spec != L_('\0'))
2113 outchar (info->spec);
2115 all_done:
2116 return done;
2119 /* Group the digits from W to REAR_PTR according to the grouping rules
2120 of the current locale. The interpretation of GROUPING is as in
2121 `struct lconv' from <locale.h>. The grouped number extends from
2122 the returned pointer until REAR_PTR. FRONT_PTR to W is used as a
2123 scratch area. */
2124 static CHAR_T *
2125 group_number (CHAR_T *front_ptr, CHAR_T *w, CHAR_T *rear_ptr,
2126 const char *grouping, THOUSANDS_SEP_T thousands_sep)
2128 /* Length of the current group. */
2129 int len;
2130 #ifndef COMPILE_WPRINTF
2131 /* Length of the separator (in wide mode, the separator is always a
2132 single wide character). */
2133 int tlen = strlen (thousands_sep);
2134 #endif
2136 /* We treat all negative values like CHAR_MAX. */
2138 if (*grouping == CHAR_MAX || *grouping <= 0)
2139 /* No grouping should be done. */
2140 return w;
2142 len = *grouping++;
2144 /* Copy existing string so that nothing gets overwritten. */
2145 memmove (front_ptr, w, (rear_ptr - w) * sizeof (CHAR_T));
2146 CHAR_T *s = front_ptr + (rear_ptr - w);
2148 w = rear_ptr;
2150 /* Process all characters in the string. */
2151 while (s > front_ptr)
2153 *--w = *--s;
2155 if (--len == 0 && s > front_ptr)
2157 /* A new group begins. */
2158 #ifdef COMPILE_WPRINTF
2159 if (w != s)
2160 *--w = thousands_sep;
2161 else
2162 /* Not enough room for the separator. */
2163 goto copy_rest;
2164 #else
2165 int cnt = tlen;
2166 if (tlen < w - s)
2168 *--w = thousands_sep[--cnt];
2169 while (cnt > 0);
2170 else
2171 /* Not enough room for the separator. */
2172 goto copy_rest;
2173 #endif
2175 if (*grouping == CHAR_MAX
2176 #if CHAR_MIN < 0
2177 || *grouping < 0
2178 #endif
2181 copy_rest:
2182 /* No further grouping to be done. Copy the rest of the
2183 number. */
2184 memmove (w, s, (front_ptr -s) * sizeof (CHAR_T));
2185 break;
2187 else if (*grouping != '\0')
2188 len = *grouping++;
2189 else
2190 /* The previous grouping repeats ad infinitum. */
2191 len = grouping[-1];
2194 return w;
2197 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2198 struct helper_file
2200 struct _IO_FILE_plus _f;
2201 #ifdef COMPILE_WPRINTF
2202 struct _IO_wide_data _wide_data;
2203 #endif
2204 FILE *_put_stream;
2205 #ifdef _IO_MTSAFE_IO
2206 _IO_lock_t lock;
2207 #endif
2210 static int
2211 _IO_helper_overflow (FILE *s, int c)
2213 FILE *target = ((struct helper_file*) s)->_put_stream;
2214 #ifdef COMPILE_WPRINTF
2215 int used = s->_wide_data->_IO_write_ptr - s->_wide_data->_IO_write_base;
2216 if (used)
2218 size_t written = _IO_sputn (target, s->_wide_data->_IO_write_base, used);
2219 if (written == 0 || written == WEOF)
2220 return WEOF;
2221 __wmemmove (s->_wide_data->_IO_write_base,
2222 s->_wide_data->_IO_write_base + written,
2223 used - written);
2224 s->_wide_data->_IO_write_ptr -= written;
2226 #else
2227 int used = s->_IO_write_ptr - s->_IO_write_base;
2228 if (used)
2230 size_t written = _IO_sputn (target, s->_IO_write_base, used);
2231 if (written == 0 || written == EOF)
2232 return EOF;
2233 memmove (s->_IO_write_base, s->_IO_write_base + written,
2234 used - written);
2235 s->_IO_write_ptr -= written;
2237 #endif
2238 return PUTC (c, s);
2241 #ifdef COMPILE_WPRINTF
2242 static const struct _IO_jump_t _IO_helper_jumps libio_vtable =
2244 JUMP_INIT_DUMMY,
2245 JUMP_INIT (finish, _IO_wdefault_finish),
2246 JUMP_INIT (overflow, _IO_helper_overflow),
2247 JUMP_INIT (underflow, _IO_default_underflow),
2248 JUMP_INIT (uflow, _IO_default_uflow),
2249 JUMP_INIT (pbackfail, (_IO_pbackfail_t) _IO_wdefault_pbackfail),
2250 JUMP_INIT (xsputn, _IO_wdefault_xsputn),
2251 JUMP_INIT (xsgetn, _IO_wdefault_xsgetn),
2252 JUMP_INIT (seekoff, _IO_default_seekoff),
2253 JUMP_INIT (seekpos, _IO_default_seekpos),
2254 JUMP_INIT (setbuf, _IO_default_setbuf),
2255 JUMP_INIT (sync, _IO_default_sync),
2256 JUMP_INIT (doallocate, _IO_wdefault_doallocate),
2257 JUMP_INIT (read, _IO_default_read),
2258 JUMP_INIT (write, _IO_default_write),
2259 JUMP_INIT (seek, _IO_default_seek),
2260 JUMP_INIT (close, _IO_default_close),
2261 JUMP_INIT (stat, _IO_default_stat)
2263 #else
2264 static const struct _IO_jump_t _IO_helper_jumps libio_vtable =
2266 JUMP_INIT_DUMMY,
2267 JUMP_INIT (finish, _IO_default_finish),
2268 JUMP_INIT (overflow, _IO_helper_overflow),
2269 JUMP_INIT (underflow, _IO_default_underflow),
2270 JUMP_INIT (uflow, _IO_default_uflow),
2271 JUMP_INIT (pbackfail, _IO_default_pbackfail),
2272 JUMP_INIT (xsputn, _IO_default_xsputn),
2273 JUMP_INIT (xsgetn, _IO_default_xsgetn),
2274 JUMP_INIT (seekoff, _IO_default_seekoff),
2275 JUMP_INIT (seekpos, _IO_default_seekpos),
2276 JUMP_INIT (setbuf, _IO_default_setbuf),
2277 JUMP_INIT (sync, _IO_default_sync),
2278 JUMP_INIT (doallocate, _IO_default_doallocate),
2279 JUMP_INIT (read, _IO_default_read),
2280 JUMP_INIT (write, _IO_default_write),
2281 JUMP_INIT (seek, _IO_default_seek),
2282 JUMP_INIT (close, _IO_default_close),
2283 JUMP_INIT (stat, _IO_default_stat)
2285 #endif
2287 static int
2288 buffered_vfprintf (FILE *s, const CHAR_T *format, va_list args)
2290 CHAR_T buf[BUFSIZ];
2291 struct helper_file helper;
2292 FILE *hp = (FILE *) &helper._f;
2293 int result, to_flush;
2295 /* Orient the stream. */
2296 #ifdef ORIENT
2297 ORIENT;
2298 #endif
2300 /* Initialize helper. */
2301 helper._put_stream = s;
2302 #ifdef COMPILE_WPRINTF
2303 hp->_wide_data = &helper._wide_data;
2304 _IO_wsetp (hp, buf, buf + sizeof buf / sizeof (CHAR_T));
2305 hp->_mode = 1;
2306 #else
2307 _IO_setp (hp, buf, buf + sizeof buf);
2308 hp->_mode = -1;
2309 #endif
2310 hp->_flags = _IO_MAGIC|_IO_NO_READS|_IO_USER_LOCK;
2311 #if _IO_JUMPS_OFFSET
2312 hp->_vtable_offset = 0;
2313 #endif
2314 #ifdef _IO_MTSAFE_IO
2315 hp->_lock = NULL;
2316 #endif
2317 hp->_flags2 = s->_flags2;
2318 _IO_JUMPS (&helper._f) = (struct _IO_jump_t *) &_IO_helper_jumps;
2320 /* Now print to helper instead. */
2321 #ifndef COMPILE_WPRINTF
2322 result = _IO_vfprintf (hp, format, args);
2323 #else
2324 result = vfprintf (hp, format, args);
2325 #endif
2327 /* Lock stream. */
2328 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, s);
2329 _IO_flockfile (s);
2331 /* Now flush anything from the helper to the S. */
2332 #ifdef COMPILE_WPRINTF
2333 if ((to_flush = (hp->_wide_data->_IO_write_ptr
2334 - hp->_wide_data->_IO_write_base)) > 0)
2336 if ((int) _IO_sputn (s, hp->_wide_data->_IO_write_base, to_flush)
2337 != to_flush)
2338 result = -1;
2340 #else
2341 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
2343 if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
2344 result = -1;
2346 #endif
2348 /* Unlock the stream. */
2349 _IO_funlockfile (s);
2350 __libc_cleanup_region_end (0);
2352 return result;
2355 #undef vfprintf
2356 #ifdef COMPILE_WPRINTF
2357 strong_alias (_IO_vfwprintf, __vfwprintf);
2358 ldbl_weak_alias (_IO_vfwprintf, vfwprintf);
2359 #else
2360 ldbl_strong_alias (_IO_vfprintf_internal, vfprintf);
2361 ldbl_hidden_def (_IO_vfprintf_internal, vfprintf)
2362 ldbl_strong_alias (_IO_vfprintf_internal, _IO_vfprintf);
2363 ldbl_hidden_def (_IO_vfprintf_internal, _IO_vfprintf)
2364 #endif