1 /* Copyright (C) 1991-2012 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/>. */
27 #include <bits/libc-lock.h>
28 #include <sys/param.h>
30 #include <locale/localeinfo.h>
33 /* This code is shared between the standard stdio implementation found
34 in GNU C library and the libio implementation originally found in
37 Beside this it is also shared between the normal and wide character
38 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
44 #define va_list _IO_va_list
46 #define BUFSIZ _IO_BUFSIZ
47 #define ARGCHECK(S, Format) \
50 /* Check file argument for consistence. */ \
52 if (S->_flags & _IO_NO_WRITES) \
54 S->_flags |= _IO_ERR_SEEN; \
55 __set_errno (EBADF); \
64 #define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
66 #define done_add(val) \
68 unsigned int _val = val; \
69 assert ((unsigned int) done < (unsigned int) INT_MAX); \
70 if (__builtin_expect (INT_MAX - done < _val, 0)) \
73 __set_errno (EOVERFLOW); \
79 #ifndef COMPILE_WPRINTF
80 # define vfprintf _IO_vfprintf_internal
82 # define UCHAR_T unsigned char
85 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
86 # define STR_LEN(Str) strlen (Str)
88 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
89 # define PAD(Padchar) \
91 done_add (_IO_padn (s, (Padchar), width))
92 # define PUTC(C, F) _IO_putc_unlocked (C, F)
93 # define ORIENT if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
96 # define vfprintf _IO_vfwprintf
97 # define CHAR_T wchar_t
98 /* This is a hack!!! There should be a type uwchar_t. */
99 # define UCHAR_T unsigned int /* uwchar_t */
100 # define INT_T wint_t
101 # define L_(Str) L##Str
102 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
103 # define STR_LEN(Str) __wcslen (Str)
107 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
108 # define PAD(Padchar) \
110 done_add (_IO_wpadn (s, (Padchar), width))
111 # define PUTC(C, F) _IO_putwc_unlocked (C, F)
112 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
115 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
116 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
121 #include "_i18n_number.h"
123 /* Include the shared code for parsing the format string. */
124 #include "printf-parse.h"
127 #define outchar(Ch) \
130 register const INT_T outc = (Ch); \
131 if (PUTC (outc, s) == EOF || done == INT_MAX) \
140 #define outstring(String, Len) \
143 assert ((size_t) done <= (size_t) INT_MAX); \
144 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
149 if (__builtin_expect (INT_MAX - done < (Len), 0)) \
152 __set_errno (EOVERFLOW); \
159 /* For handling long_double and longlong we use the same flag. If
160 `long' and `long long' are effectively the same type define it to
162 #if LONG_MAX == LONG_LONG_MAX
163 # define is_longlong 0
165 # define is_longlong is_long_double
168 /* If `long' and `int' is effectively the same type we don't have to
169 handle `long separately. */
170 #if INT_MAX == LONG_MAX
171 # define is_long_num 0
173 # define is_long_num is_long
177 /* Global variables. */
178 static const CHAR_T null
[] = L_("(null)");
181 /* Helper function to provide temporary buffering for unbuffered streams. */
182 static int buffered_vfprintf (FILE *stream
, const CHAR_T
*fmt
, va_list)
183 __THROW
__attribute__ ((noinline
)) internal_function
;
185 /* Handle unknown format specifier. */
186 static int printf_unknown (FILE *, const struct printf_info
*,
187 const void *const *) __THROW
;
189 /* Group digits of number string. */
190 #ifdef COMPILE_WPRINTF
191 static CHAR_T
*group_number (CHAR_T
*, CHAR_T
*, const char *, wchar_t)
192 __THROW internal_function
;
194 static CHAR_T
*group_number (CHAR_T
*, CHAR_T
*, const char *, const char *)
195 __THROW internal_function
;
199 /* The function itself. */
201 vfprintf (FILE *s
, const CHAR_T
*format
, va_list ap
)
203 /* The character used as thousands separator. */
204 #ifdef COMPILE_WPRINTF
205 wchar_t thousands_sep
= L
'\0';
207 const char *thousands_sep
= NULL
;
210 /* The string describing the size of groups of digits. */
211 const char *grouping
;
213 /* Place to accumulate the result. */
216 /* Current character in format string. */
219 /* End of leading constant string. */
220 const UCHAR_T
*lead_str_end
;
222 /* Points to next format specifier. */
223 const UCHAR_T
*end_of_spec
;
225 /* Buffer intermediate results. */
226 CHAR_T work_buffer
[1000];
227 CHAR_T
*workstart
= NULL
;
230 /* We have to save the original argument pointer. */
233 /* Count number of specifiers we already processed. */
236 /* For the %m format we may need the current `errno' value. */
237 int save_errno
= errno
;
239 /* 1 if format is in read-only memory, -1 if it is in writable memory,
241 int readonly_format
= 0;
243 /* For the argument descriptions, which may be allocated on the heap. */
244 void *args_malloced
= NULL
;
246 /* This table maps a character into a number representing a
247 class. In each step there is a destination label for each
249 static const uint8_t jump_table
[] =
251 /* ' ' */ 1, 0, 0, /* '#' */ 4,
252 0, /* '%' */ 14, 0, /* '\''*/ 6,
253 0, 0, /* '*' */ 7, /* '+' */ 2,
254 0, /* '-' */ 3, /* '.' */ 9, 0,
255 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
256 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
257 /* '8' */ 8, /* '9' */ 8, 0, 0,
259 0, /* 'A' */ 26, 0, /* 'C' */ 25,
260 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
261 0, /* 'I' */ 29, 0, 0,
262 /* 'L' */ 12, 0, 0, 0,
263 0, 0, 0, /* 'S' */ 21,
265 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
267 0, /* 'a' */ 26, 0, /* 'c' */ 20,
268 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
269 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
270 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
271 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
272 /* 't' */ 27, /* 'u' */ 16, 0, 0,
273 /* 'x' */ 18, 0, /* 'z' */ 13
276 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
277 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
279 /* 'int' is enough and it saves some space on 64 bit systems. */
280 # define JUMP_TABLE_TYPE const int
281 # define JUMP(ChExpr, table) \
285 void *__unbounded ptr; \
287 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
288 : table[CHAR_CLASS (spec)]; \
289 ptr = &&do_form_unknown + offset; \
294 # define JUMP_TABLE_TYPE const void *const
295 # define JUMP(ChExpr, table) \
298 const void *__unbounded ptr; \
300 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
301 : table[CHAR_CLASS (spec)]; \
307 #define STEP0_3_TABLE \
308 /* Step 0: at the beginning. */ \
309 static JUMP_TABLE_TYPE step0_jumps[30] = \
311 REF (form_unknown), \
312 REF (flag_space), /* for ' ' */ \
313 REF (flag_plus), /* for '+' */ \
314 REF (flag_minus), /* for '-' */ \
315 REF (flag_hash), /* for '<hash>' */ \
316 REF (flag_zero), /* for '0' */ \
317 REF (flag_quote), /* for '\'' */ \
318 REF (width_asterics), /* for '*' */ \
319 REF (width), /* for '1'...'9' */ \
320 REF (precision), /* for '.' */ \
321 REF (mod_half), /* for 'h' */ \
322 REF (mod_long), /* for 'l' */ \
323 REF (mod_longlong), /* for 'L', 'q' */ \
324 REF (mod_size_t), /* for 'z', 'Z' */ \
325 REF (form_percent), /* for '%' */ \
326 REF (form_integer), /* for 'd', 'i' */ \
327 REF (form_unsigned), /* for 'u' */ \
328 REF (form_octal), /* for 'o' */ \
329 REF (form_hexa), /* for 'X', 'x' */ \
330 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
331 REF (form_character), /* for 'c' */ \
332 REF (form_string), /* for 's', 'S' */ \
333 REF (form_pointer), /* for 'p' */ \
334 REF (form_number), /* for 'n' */ \
335 REF (form_strerror), /* for 'm' */ \
336 REF (form_wcharacter), /* for 'C' */ \
337 REF (form_floathex), /* for 'A', 'a' */ \
338 REF (mod_ptrdiff_t), /* for 't' */ \
339 REF (mod_intmax_t), /* for 'j' */ \
340 REF (flag_i18n), /* for 'I' */ \
342 /* Step 1: after processing width. */ \
343 static JUMP_TABLE_TYPE step1_jumps[30] = \
345 REF (form_unknown), \
346 REF (form_unknown), /* for ' ' */ \
347 REF (form_unknown), /* for '+' */ \
348 REF (form_unknown), /* for '-' */ \
349 REF (form_unknown), /* for '<hash>' */ \
350 REF (form_unknown), /* for '0' */ \
351 REF (form_unknown), /* for '\'' */ \
352 REF (form_unknown), /* for '*' */ \
353 REF (form_unknown), /* for '1'...'9' */ \
354 REF (precision), /* for '.' */ \
355 REF (mod_half), /* for 'h' */ \
356 REF (mod_long), /* for 'l' */ \
357 REF (mod_longlong), /* for 'L', 'q' */ \
358 REF (mod_size_t), /* for 'z', 'Z' */ \
359 REF (form_percent), /* for '%' */ \
360 REF (form_integer), /* for 'd', 'i' */ \
361 REF (form_unsigned), /* for 'u' */ \
362 REF (form_octal), /* for 'o' */ \
363 REF (form_hexa), /* for 'X', 'x' */ \
364 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
365 REF (form_character), /* for 'c' */ \
366 REF (form_string), /* for 's', 'S' */ \
367 REF (form_pointer), /* for 'p' */ \
368 REF (form_number), /* for 'n' */ \
369 REF (form_strerror), /* for 'm' */ \
370 REF (form_wcharacter), /* for 'C' */ \
371 REF (form_floathex), /* for 'A', 'a' */ \
372 REF (mod_ptrdiff_t), /* for 't' */ \
373 REF (mod_intmax_t), /* for 'j' */ \
374 REF (form_unknown) /* for 'I' */ \
376 /* Step 2: after processing precision. */ \
377 static JUMP_TABLE_TYPE step2_jumps[30] = \
379 REF (form_unknown), \
380 REF (form_unknown), /* for ' ' */ \
381 REF (form_unknown), /* for '+' */ \
382 REF (form_unknown), /* for '-' */ \
383 REF (form_unknown), /* for '<hash>' */ \
384 REF (form_unknown), /* for '0' */ \
385 REF (form_unknown), /* for '\'' */ \
386 REF (form_unknown), /* for '*' */ \
387 REF (form_unknown), /* for '1'...'9' */ \
388 REF (form_unknown), /* for '.' */ \
389 REF (mod_half), /* for 'h' */ \
390 REF (mod_long), /* for 'l' */ \
391 REF (mod_longlong), /* for 'L', 'q' */ \
392 REF (mod_size_t), /* for 'z', 'Z' */ \
393 REF (form_percent), /* for '%' */ \
394 REF (form_integer), /* for 'd', 'i' */ \
395 REF (form_unsigned), /* for 'u' */ \
396 REF (form_octal), /* for 'o' */ \
397 REF (form_hexa), /* for 'X', 'x' */ \
398 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
399 REF (form_character), /* for 'c' */ \
400 REF (form_string), /* for 's', 'S' */ \
401 REF (form_pointer), /* for 'p' */ \
402 REF (form_number), /* for 'n' */ \
403 REF (form_strerror), /* for 'm' */ \
404 REF (form_wcharacter), /* for 'C' */ \
405 REF (form_floathex), /* for 'A', 'a' */ \
406 REF (mod_ptrdiff_t), /* for 't' */ \
407 REF (mod_intmax_t), /* for 'j' */ \
408 REF (form_unknown) /* for 'I' */ \
410 /* Step 3a: after processing first 'h' modifier. */ \
411 static JUMP_TABLE_TYPE step3a_jumps[30] = \
413 REF (form_unknown), \
414 REF (form_unknown), /* for ' ' */ \
415 REF (form_unknown), /* for '+' */ \
416 REF (form_unknown), /* for '-' */ \
417 REF (form_unknown), /* for '<hash>' */ \
418 REF (form_unknown), /* for '0' */ \
419 REF (form_unknown), /* for '\'' */ \
420 REF (form_unknown), /* for '*' */ \
421 REF (form_unknown), /* for '1'...'9' */ \
422 REF (form_unknown), /* for '.' */ \
423 REF (mod_halfhalf), /* for 'h' */ \
424 REF (form_unknown), /* for 'l' */ \
425 REF (form_unknown), /* for 'L', 'q' */ \
426 REF (form_unknown), /* for 'z', 'Z' */ \
427 REF (form_percent), /* for '%' */ \
428 REF (form_integer), /* for 'd', 'i' */ \
429 REF (form_unsigned), /* for 'u' */ \
430 REF (form_octal), /* for 'o' */ \
431 REF (form_hexa), /* for 'X', 'x' */ \
432 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
433 REF (form_unknown), /* for 'c' */ \
434 REF (form_unknown), /* for 's', 'S' */ \
435 REF (form_unknown), /* for 'p' */ \
436 REF (form_number), /* for 'n' */ \
437 REF (form_unknown), /* for 'm' */ \
438 REF (form_unknown), /* for 'C' */ \
439 REF (form_unknown), /* for 'A', 'a' */ \
440 REF (form_unknown), /* for 't' */ \
441 REF (form_unknown), /* for 'j' */ \
442 REF (form_unknown) /* for 'I' */ \
444 /* Step 3b: after processing first 'l' modifier. */ \
445 static JUMP_TABLE_TYPE step3b_jumps[30] = \
447 REF (form_unknown), \
448 REF (form_unknown), /* for ' ' */ \
449 REF (form_unknown), /* for '+' */ \
450 REF (form_unknown), /* for '-' */ \
451 REF (form_unknown), /* for '<hash>' */ \
452 REF (form_unknown), /* for '0' */ \
453 REF (form_unknown), /* for '\'' */ \
454 REF (form_unknown), /* for '*' */ \
455 REF (form_unknown), /* for '1'...'9' */ \
456 REF (form_unknown), /* for '.' */ \
457 REF (form_unknown), /* for 'h' */ \
458 REF (mod_longlong), /* for 'l' */ \
459 REF (form_unknown), /* for 'L', 'q' */ \
460 REF (form_unknown), /* for 'z', 'Z' */ \
461 REF (form_percent), /* for '%' */ \
462 REF (form_integer), /* for 'd', 'i' */ \
463 REF (form_unsigned), /* for 'u' */ \
464 REF (form_octal), /* for 'o' */ \
465 REF (form_hexa), /* for 'X', 'x' */ \
466 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
467 REF (form_character), /* for 'c' */ \
468 REF (form_string), /* for 's', 'S' */ \
469 REF (form_pointer), /* for 'p' */ \
470 REF (form_number), /* for 'n' */ \
471 REF (form_strerror), /* for 'm' */ \
472 REF (form_wcharacter), /* for 'C' */ \
473 REF (form_floathex), /* for 'A', 'a' */ \
474 REF (form_unknown), /* for 't' */ \
475 REF (form_unknown), /* for 'j' */ \
476 REF (form_unknown) /* for 'I' */ \
479 #define STEP4_TABLE \
480 /* Step 4: processing format specifier. */ \
481 static JUMP_TABLE_TYPE step4_jumps[30] = \
483 REF (form_unknown), \
484 REF (form_unknown), /* for ' ' */ \
485 REF (form_unknown), /* for '+' */ \
486 REF (form_unknown), /* for '-' */ \
487 REF (form_unknown), /* for '<hash>' */ \
488 REF (form_unknown), /* for '0' */ \
489 REF (form_unknown), /* for '\'' */ \
490 REF (form_unknown), /* for '*' */ \
491 REF (form_unknown), /* for '1'...'9' */ \
492 REF (form_unknown), /* for '.' */ \
493 REF (form_unknown), /* for 'h' */ \
494 REF (form_unknown), /* for 'l' */ \
495 REF (form_unknown), /* for 'L', 'q' */ \
496 REF (form_unknown), /* for 'z', 'Z' */ \
497 REF (form_percent), /* for '%' */ \
498 REF (form_integer), /* for 'd', 'i' */ \
499 REF (form_unsigned), /* for 'u' */ \
500 REF (form_octal), /* for 'o' */ \
501 REF (form_hexa), /* for 'X', 'x' */ \
502 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
503 REF (form_character), /* for 'c' */ \
504 REF (form_string), /* for 's', 'S' */ \
505 REF (form_pointer), /* for 'p' */ \
506 REF (form_number), /* for 'n' */ \
507 REF (form_strerror), /* for 'm' */ \
508 REF (form_wcharacter), /* for 'C' */ \
509 REF (form_floathex), /* for 'A', 'a' */ \
510 REF (form_unknown), /* for 't' */ \
511 REF (form_unknown), /* for 'j' */ \
512 REF (form_unknown) /* for 'I' */ \
516 #define process_arg(fspec) \
517 /* Start real work. We know about all flags and modifiers and \
518 now process the wanted format specifier. */ \
519 LABEL (form_percent): \
520 /* Write a literal "%". */ \
524 LABEL (form_integer): \
525 /* Signed decimal integer. */ \
530 long long int signed_number; \
533 signed_number = va_arg (ap, long long int); \
535 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
537 is_negative = signed_number < 0; \
538 number.longlong = is_negative ? (- signed_number) : signed_number; \
540 goto LABEL (longlong_number); \
544 long int signed_number; \
549 signed_number = va_arg (ap, long int); \
551 signed_number = (signed char) va_arg (ap, unsigned int); \
552 else if (!is_short) \
553 signed_number = va_arg (ap, int); \
555 signed_number = (short int) va_arg (ap, unsigned int); \
559 signed_number = args_value[fspec->data_arg].pa_long_int; \
561 signed_number = (signed char) \
562 args_value[fspec->data_arg].pa_u_int; \
563 else if (!is_short) \
564 signed_number = args_value[fspec->data_arg].pa_int; \
566 signed_number = (short int) \
567 args_value[fspec->data_arg].pa_u_int; \
569 is_negative = signed_number < 0; \
570 number.word = is_negative ? (- signed_number) : signed_number; \
572 goto LABEL (number); \
576 LABEL (form_unsigned): \
577 /* Unsigned decimal integer. */ \
579 goto LABEL (unsigned_number); \
582 LABEL (form_octal): \
583 /* Unsigned octal integer. */ \
585 goto LABEL (unsigned_number); \
589 /* Unsigned hexadecimal integer. */ \
592 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
594 /* ISO specifies the `+' and ` ' flags only for signed \
603 number.longlong = va_arg (ap, unsigned long long int); \
605 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
607 LABEL (longlong_number): \
609 /* Supply a default precision if none was given. */ \
612 /* We have to take care for the '0' flag. If a precision \
613 is given it must be ignored. */ \
616 /* If the precision is 0 and the number is 0 nothing has to \
617 be written for the number, except for the 'o' format in \
619 if (prec == 0 && number.longlong == 0) \
622 if (base == 8 && alt) \
623 *--string = L_('0'); \
627 /* Put the number in WORK. */ \
628 string = _itoa (number.longlong, workend, base, \
630 if (group && grouping) \
631 string = group_number (string, workend, grouping, \
634 if (use_outdigits && base == 10) \
635 string = _i18n_number_rewrite (string, workend, workend); \
637 /* Simplify further test for num != 0. */ \
638 number.word = number.longlong != 0; \
645 number.word = va_arg (ap, unsigned long int); \
647 number.word = (unsigned char) va_arg (ap, unsigned int); \
648 else if (!is_short) \
649 number.word = va_arg (ap, unsigned int); \
651 number.word = (unsigned short int) va_arg (ap, unsigned int); \
655 number.word = args_value[fspec->data_arg].pa_u_long_int; \
657 number.word = (unsigned char) \
658 args_value[fspec->data_arg].pa_u_int; \
659 else if (!is_short) \
660 number.word = args_value[fspec->data_arg].pa_u_int; \
662 number.word = (unsigned short int) \
663 args_value[fspec->data_arg].pa_u_int; \
667 /* Supply a default precision if none was given. */ \
670 /* We have to take care for the '0' flag. If a precision \
671 is given it must be ignored. */ \
674 /* If the precision is 0 and the number is 0 nothing has to \
675 be written for the number, except for the 'o' format in \
677 if (prec == 0 && number.word == 0) \
680 if (base == 8 && alt) \
681 *--string = L_('0'); \
685 /* Put the number in WORK. */ \
686 string = _itoa_word (number.word, workend, base, \
688 if (group && grouping) \
689 string = group_number (string, workend, grouping, \
692 if (use_outdigits && base == 10) \
693 string = _i18n_number_rewrite (string, workend, workend); \
697 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
698 /* Add octal marker. */ \
699 *--string = L_('0'); \
701 prec = MAX (0, prec - (workend - string)); \
705 width -= workend - string + prec; \
707 if (number.word != 0 && alt && base == 16) \
708 /* Account for 0X hex marker. */ \
711 if (is_negative || showsign || space) \
714 if (pad == L_(' ')) \
727 if (number.word != 0 && alt && base == 16) \
736 outstring (string, workend - string); \
758 if (number.word != 0 && alt && base == 16) \
765 width -= workend - string + prec; \
775 outstring (string, workend - string); \
781 LABEL (form_float): \
783 /* Floating-point number. This is handled by printf_fp.c. */ \
790 is_long_double = 0; \
792 struct printf_info info = { .prec = prec, \
795 .is_long_double = is_long_double, \
796 .is_short = is_short, \
797 .is_long = is_long, \
801 .showsign = showsign, \
805 .i18n = use_outdigits, \
806 .wide = sizeof (CHAR_T) != 1 }; \
808 if (is_long_double) \
809 the_arg.pa_long_double = va_arg (ap, long double); \
811 the_arg.pa_double = va_arg (ap, double); \
812 ptr = (const void *) &the_arg; \
814 function_done = __printf_fp (s, &info, &ptr); \
818 ptr = (const void *) &args_value[fspec->data_arg]; \
821 fspec->data_arg_type = PA_DOUBLE; \
822 fspec->info.is_long_double = 0; \
825 function_done = __printf_fp (s, &fspec->info, &ptr); \
828 if (function_done < 0) \
830 /* Error in print handler; up to handler to set errno. */ \
835 done_add (function_done); \
839 LABEL (form_floathex): \
841 /* Floating point number printed as hexadecimal number. */ \
848 is_long_double = 0; \
850 struct printf_info info = { .prec = prec, \
853 .is_long_double = is_long_double, \
854 .is_short = is_short, \
855 .is_long = is_long, \
859 .showsign = showsign, \
863 .wide = sizeof (CHAR_T) != 1 }; \
865 if (is_long_double) \
866 the_arg.pa_long_double = va_arg (ap, long double); \
868 the_arg.pa_double = va_arg (ap, double); \
869 ptr = (const void *) &the_arg; \
871 function_done = __printf_fphex (s, &info, &ptr); \
875 ptr = (const void *) &args_value[fspec->data_arg]; \
877 fspec->info.is_long_double = 0; \
879 function_done = __printf_fphex (s, &fspec->info, &ptr); \
882 if (function_done < 0) \
884 /* Error in print handler; up to handler to set errno. */ \
889 done_add (function_done); \
893 LABEL (form_pointer): \
894 /* Generic pointer. */ \
898 ptr = va_arg (ap, void *); \
900 ptr = args_value[fspec->data_arg].pa_pointer; \
903 /* If the pointer is not NULL, write it as a %#x spec. */ \
905 number.word = (unsigned long int) ptr; \
910 goto LABEL (number); \
914 /* Write "(nil)" for a nil pointer. */ \
915 string = (CHAR_T *) L_("(nil)"); \
916 /* Make sure the full string "(nil)" is printed. */ \
919 is_long = 0; /* This is no wide-char string. */ \
920 goto LABEL (print_string); \
925 LABEL (form_number): \
926 if (s->_flags2 & _IO_FLAGS2_FORTIFY) \
928 if (! readonly_format) \
930 extern int __readonly_area (const void *, size_t) \
933 = __readonly_area (format, ((STR_LEN (format) + 1) \
934 * sizeof (CHAR_T))); \
936 if (readonly_format < 0) \
937 __libc_fatal ("*** %n in writable segment detected ***\n"); \
939 /* Answer the count of characters written. */ \
943 *(long long int *) va_arg (ap, void *) = done; \
944 else if (is_long_num) \
945 *(long int *) va_arg (ap, void *) = done; \
947 *(char *) va_arg (ap, void *) = done; \
948 else if (!is_short) \
949 *(int *) va_arg (ap, void *) = done; \
951 *(short int *) va_arg (ap, void *) = done; \
955 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
956 else if (is_long_num) \
957 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
959 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
960 else if (!is_short) \
961 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
963 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
966 LABEL (form_strerror): \
967 /* Print description of error ERRNO. */ \
969 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
970 sizeof work_buffer); \
971 is_long = 0; /* This is no wide-char string. */ \
972 goto LABEL (print_string)
974 #ifdef COMPILE_WPRINTF
975 # define process_string_arg(fspec) \
976 LABEL (form_character): \
979 goto LABEL (form_wcharacter); \
980 --width; /* Account for the character itself. */ \
984 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
986 outchar (__btowc ((unsigned char) \
987 args_value[fspec->data_arg].pa_int)); \
992 LABEL (form_wcharacter): \
994 /* Wide character. */ \
999 outchar (va_arg (ap, wchar_t)); \
1001 outchar (args_value[fspec->data_arg].pa_wchar); \
1007 LABEL (form_string): \
1010 int string_malloced; \
1012 /* The string argument could in fact be `char *' or `wchar_t *'. \
1013 But this should not make a difference here. */ \
1014 if (fspec == NULL) \
1015 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
1017 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
1019 /* Entry point for printing other strings. */ \
1020 LABEL (print_string): \
1022 string_malloced = 0; \
1023 if (string == NULL) \
1025 /* Write "(null)" if there's space. */ \
1027 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
1029 string = (CHAR_T *) null; \
1030 len = (sizeof (null) / sizeof (null[0])) - 1; \
1034 string = (CHAR_T *) L""; \
1038 else if (!is_long && spec != L_('S')) \
1040 /* This is complicated. We have to transform the multibyte \
1041 string into a wide character string. */ \
1042 const char *mbs = (const char *) string; \
1043 mbstate_t mbstate; \
1045 len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
1047 /* Allocate dynamically an array which definitely is long \
1048 enough for the wide character version. Each byte in the \
1049 multi-byte string can produce at most one wide character. */ \
1050 if (__libc_use_alloca (len * sizeof (wchar_t))) \
1051 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1052 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1059 string_malloced = 1; \
1061 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1062 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1063 if (len == (size_t) -1) \
1065 /* Illegal multibyte character. */ \
1073 /* Search for the end of the string, but don't search past \
1074 the length specified by the precision. */ \
1075 len = __wcsnlen (string, prec); \
1077 len = __wcslen (string); \
1080 if ((width -= len) < 0) \
1082 outstring (string, len); \
1088 outstring (string, len); \
1091 if (__builtin_expect (string_malloced, 0)) \
1096 # define process_string_arg(fspec) \
1097 LABEL (form_character): \
1100 goto LABEL (form_wcharacter); \
1101 --width; /* Account for the character itself. */ \
1104 if (fspec == NULL) \
1105 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1107 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1112 LABEL (form_wcharacter): \
1114 /* Wide character. */ \
1115 char buf[MB_CUR_MAX]; \
1116 mbstate_t mbstate; \
1119 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1120 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1121 : args_value[fspec->data_arg].pa_wchar), \
1123 if (len == (size_t) -1) \
1125 /* Something went wrong during the conversion. Bail out. */ \
1132 outstring (buf, len); \
1138 LABEL (form_string): \
1141 int string_malloced; \
1143 /* The string argument could in fact be `char *' or `wchar_t *'. \
1144 But this should not make a difference here. */ \
1145 if (fspec == NULL) \
1146 string = (char *) va_arg (ap, const char *); \
1148 string = (char *) args_value[fspec->data_arg].pa_string; \
1150 /* Entry point for printing other strings. */ \
1151 LABEL (print_string): \
1153 string_malloced = 0; \
1154 if (string == NULL) \
1156 /* Write "(null)" if there's space. */ \
1157 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1159 string = (char *) null; \
1160 len = sizeof (null) - 1; \
1164 string = (char *) ""; \
1168 else if (!is_long && spec != L_('S')) \
1171 /* Search for the end of the string, but don't search past \
1172 the length (in bytes) specified by the precision. */ \
1173 len = __strnlen (string, prec); \
1175 len = strlen (string); \
1179 const wchar_t *s2 = (const wchar_t *) string; \
1180 mbstate_t mbstate; \
1182 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1186 /* The string `s2' might not be NUL terminated. */ \
1187 if (__libc_use_alloca (prec)) \
1188 string = (char *) alloca (prec); \
1189 else if ((string = (char *) malloc (prec)) == NULL) \
1195 string_malloced = 1; \
1196 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1200 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1201 if (len != (size_t) -1) \
1203 assert (__mbsinit (&mbstate)); \
1204 s2 = (const wchar_t *) string; \
1205 if (__libc_use_alloca (len + 1)) \
1206 string = (char *) alloca (len + 1); \
1207 else if ((string = (char *) malloc (len + 1)) == NULL) \
1213 string_malloced = 1; \
1214 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1218 if (len == (size_t) -1) \
1220 /* Illegal wide-character string. */ \
1226 if ((width -= len) < 0) \
1228 outstring (string, len); \
1234 outstring (string, len); \
1237 if (__builtin_expect (string_malloced, 0)) \
1243 /* Orient the stream. */
1248 /* Sanity check of arguments. */
1249 ARGCHECK (s
, format
);
1252 /* Check for correct orientation. */
1253 if (_IO_vtable_offset (s
) == 0 &&
1254 _IO_fwide (s
, sizeof (CHAR_T
) == 1 ? -1 : 1)
1255 != (sizeof (CHAR_T
) == 1 ? -1 : 1))
1256 /* The stream is already oriented otherwise. */
1260 if (UNBUFFERED_P (s
))
1261 /* Use a helper function which will allocate a local temporary buffer
1262 for the stream and then call us again. */
1263 return buffered_vfprintf (s
, format
, ap
);
1265 /* Initialize local variables. */
1267 grouping
= (const char *) -1;
1269 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1270 since on some systems `va_list' is not an integral type. */
1271 __va_copy (ap_save
, ap
);
1277 #ifdef COMPILE_WPRINTF
1278 /* Find the first format specifier. */
1279 f
= lead_str_end
= __find_specwc ((const UCHAR_T
*) format
);
1281 /* Find the first format specifier. */
1282 f
= lead_str_end
= __find_specmb ((const UCHAR_T
*) format
);
1286 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile
, s
);
1289 /* Write the literal text before the first format. */
1290 outstring ((const UCHAR_T
*) format
,
1291 lead_str_end
- (const UCHAR_T
*) format
);
1293 /* If we only have to print a simple string, return now. */
1297 /* Use the slow path in case any printf handler is registered. */
1298 if (__builtin_expect (__printf_function_table
!= NULL
1299 || __printf_modifier_table
!= NULL
1300 || __printf_va_arg_table
!= NULL
, 0))
1303 /* Process whole format string. */
1307 # define REF(Name) &&do_##Name - &&do_form_unknown
1309 # define REF(Name) &&do_##Name
1311 #define LABEL(Name) do_##Name
1315 union printf_arg
*args_value
; /* This is not used here but ... */
1316 int is_negative
; /* Flag for negative number. */
1319 unsigned long long int longlong
;
1320 unsigned long int word
;
1323 union printf_arg the_arg
;
1324 CHAR_T
*string
; /* Pointer to argument string. */
1325 int alt
= 0; /* Alternate format. */
1326 int space
= 0; /* Use space prefix if no sign is needed. */
1327 int left
= 0; /* Left-justify output. */
1328 int showsign
= 0; /* Always begin with plus or minus sign. */
1329 int group
= 0; /* Print numbers according grouping rules. */
1330 int is_long_double
= 0; /* Argument is long double/ long long int. */
1331 int is_short
= 0; /* Argument is short int. */
1332 int is_long
= 0; /* Argument is long int. */
1333 int is_char
= 0; /* Argument is promoted (unsigned) char. */
1334 int width
= 0; /* Width of output; 0 means none specified. */
1335 int prec
= -1; /* Precision of output; -1 means none specified. */
1336 /* This flag is set by the 'I' modifier and selects the use of the
1337 `outdigits' as determined by the current locale. */
1338 int use_outdigits
= 0;
1339 UCHAR_T pad
= L_(' ');/* Padding character. */
1343 workend
= &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
1345 /* Get current character in format string. */
1346 JUMP (*++f
, step0_jumps
);
1351 JUMP (*++f
, step0_jumps
);
1356 JUMP (*++f
, step0_jumps
);
1362 JUMP (*++f
, step0_jumps
);
1367 JUMP (*++f
, step0_jumps
);
1373 JUMP (*++f
, step0_jumps
);
1375 /* The '\'' flag. */
1379 if (grouping
== (const char *) -1)
1381 #ifdef COMPILE_WPRINTF
1382 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1383 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1385 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1388 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1389 if (*grouping
== '\0' || *grouping
== CHAR_MAX
1390 #ifdef COMPILE_WPRINTF
1391 || thousands_sep
== L
'\0'
1393 || *thousands_sep
== '\0'
1398 JUMP (*++f
, step0_jumps
);
1402 JUMP (*++f
, step0_jumps
);
1404 /* Get width from argument. */
1405 LABEL (width_asterics
):
1407 const UCHAR_T
*tmp
; /* Temporary value. */
1412 int pos
= read_int (&tmp
);
1416 __set_errno (EOVERFLOW
);
1421 if (pos
&& *tmp
== L_('$'))
1422 /* The width comes from a positional parameter. */
1425 width
= va_arg (ap
, int);
1427 /* Negative width means left justified. */
1435 if (__builtin_expect (width
>= INT_MAX
/ sizeof (CHAR_T
) - 32, 0))
1437 __set_errno (EOVERFLOW
);
1442 if (width
>= sizeof (work_buffer
) / sizeof (work_buffer
[0]) - 32)
1444 /* We have to use a special buffer. The "32" is just a safe
1445 bet for all the output which is not counted in the width. */
1446 size_t needed
= ((size_t) width
+ 32) * sizeof (CHAR_T
);
1447 if (__libc_use_alloca (needed
))
1448 workend
= (CHAR_T
*) alloca (needed
) + width
+ 32;
1451 workstart
= (CHAR_T
*) malloc (needed
);
1452 if (workstart
== NULL
)
1457 workend
= workstart
+ width
+ 32;
1461 JUMP (*f
, step1_jumps
);
1463 /* Given width in format string. */
1465 width
= read_int (&f
);
1467 if (__builtin_expect (width
== -1
1468 || width
>= INT_MAX
/ sizeof (CHAR_T
) - 32, 0))
1470 __set_errno (EOVERFLOW
);
1475 if (width
>= sizeof (work_buffer
) / sizeof (work_buffer
[0]) - 32)
1477 /* We have to use a special buffer. The "32" is just a safe
1478 bet for all the output which is not counted in the width. */
1479 size_t needed
= ((size_t) width
+ 32) * sizeof (CHAR_T
);
1480 if (__libc_use_alloca (needed
))
1481 workend
= (CHAR_T
*) alloca (needed
) + width
+ 32;
1484 workstart
= (CHAR_T
*) malloc (needed
);
1485 if (workstart
== NULL
)
1490 workend
= workstart
+ width
+ 32;
1494 /* Oh, oh. The argument comes from a positional parameter. */
1496 JUMP (*f
, step1_jumps
);
1502 const UCHAR_T
*tmp
; /* Temporary value. */
1507 int pos
= read_int (&tmp
);
1511 __set_errno (EOVERFLOW
);
1516 if (pos
&& *tmp
== L_('$'))
1517 /* The precision comes from a positional parameter. */
1520 prec
= va_arg (ap
, int);
1522 /* If the precision is negative the precision is omitted. */
1526 else if (ISDIGIT (*f
))
1528 prec
= read_int (&f
);
1530 /* The precision was specified in this case as an extremely
1531 large positive value. */
1534 __set_errno (EOVERFLOW
);
1542 && prec
> sizeof (work_buffer
) / sizeof (work_buffer
[0]) - 32)
1544 if (__builtin_expect (prec
>= INT_MAX
/ sizeof (CHAR_T
) - 32, 0))
1546 __set_errno (EOVERFLOW
);
1550 size_t needed
= ((size_t) prec
+ 32) * sizeof (CHAR_T
);
1552 if (__libc_use_alloca (needed
))
1553 workend
= (CHAR_T
*) alloca (needed
) + prec
+ 32;
1556 workstart
= (CHAR_T
*) malloc (needed
);
1557 if (workstart
== NULL
)
1562 workend
= workstart
+ prec
+ 32;
1565 JUMP (*f
, step2_jumps
);
1567 /* Process 'h' modifier. There might another 'h' following. */
1570 JUMP (*++f
, step3a_jumps
);
1572 /* Process 'hh' modifier. */
1573 LABEL (mod_halfhalf
):
1576 JUMP (*++f
, step4_jumps
);
1578 /* Process 'l' modifier. There might another 'l' following. */
1581 JUMP (*++f
, step3b_jumps
);
1583 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1584 allowed to follow. */
1585 LABEL (mod_longlong
):
1588 JUMP (*++f
, step4_jumps
);
1591 is_long_double
= sizeof (size_t) > sizeof (unsigned long int);
1592 is_long
= sizeof (size_t) > sizeof (unsigned int);
1593 JUMP (*++f
, step4_jumps
);
1595 LABEL (mod_ptrdiff_t
):
1596 is_long_double
= sizeof (ptrdiff_t) > sizeof (unsigned long int);
1597 is_long
= sizeof (ptrdiff_t) > sizeof (unsigned int);
1598 JUMP (*++f
, step4_jumps
);
1600 LABEL (mod_intmax_t
):
1601 is_long_double
= sizeof (intmax_t) > sizeof (unsigned long int);
1602 is_long
= sizeof (intmax_t) > sizeof (unsigned int);
1603 JUMP (*++f
, step4_jumps
);
1605 /* Process current format. */
1608 process_arg (((struct printf_spec
*) NULL
));
1609 process_string_arg (((struct printf_spec
*) NULL
));
1611 LABEL (form_unknown
):
1612 if (spec
== L_('\0'))
1614 /* The format string ended before the specifier is complete. */
1615 __set_errno (EINVAL
);
1620 /* If we are in the fast loop force entering the complicated
1625 /* The format is correctly handled. */
1628 if (__builtin_expect (workstart
!= NULL
, 0))
1632 /* Look for next format specifier. */
1633 #ifdef COMPILE_WPRINTF
1634 f
= __find_specwc ((end_of_spec
= ++f
));
1636 f
= __find_specmb ((end_of_spec
= ++f
));
1639 /* Write the following constant string. */
1640 outstring (end_of_spec
, f
- end_of_spec
);
1642 while (*f
!= L_('\0'));
1644 /* Unlock stream and return. */
1647 /* Here starts the more complex loop to handle positional parameters. */
1650 /* Array with information about the needed arguments. This has to
1651 be dynamically extensible. */
1653 /* A more or less arbitrary start value. */
1654 size_t nspecs_size
= 32 * sizeof (struct printf_spec
);
1655 struct printf_spec
*specs
= alloca (nspecs_size
);
1657 /* The number of arguments the format string requests. This will
1658 determine the size of the array needed to store the argument
1661 size_t bytes_per_arg
;
1662 union printf_arg
*args_value
;
1666 /* Positional parameters refer to arguments directly. This could
1667 also determine the maximum number of arguments. Track the
1669 size_t max_ref_arg
= 0;
1671 /* Just a counter. */
1677 if (grouping
== (const char *) -1)
1679 #ifdef COMPILE_WPRINTF
1680 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1681 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1683 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1686 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1687 if (*grouping
== '\0' || *grouping
== CHAR_MAX
)
1691 for (f
= lead_str_end
; *f
!= L_('\0'); f
= specs
[nspecs
++].next_fmt
)
1693 if (nspecs
* sizeof (*specs
) >= nspecs_size
)
1695 /* Extend the array of format specifiers. */
1696 struct printf_spec
*old
= specs
;
1697 specs
= extend_alloca (specs
, nspecs_size
, 2 * nspecs_size
);
1699 /* Copy the old array's elements to the new space. */
1700 memmove (specs
, old
, nspecs
* sizeof (*specs
));
1703 /* Parse the format specifier. */
1704 #ifdef COMPILE_WPRINTF
1705 nargs
+= __parse_one_specwc (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1707 nargs
+= __parse_one_specmb (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1711 /* Determine the number of arguments the format string consumes. */
1712 nargs
= MAX (nargs
, max_ref_arg
);
1713 /* Calculate total size needed to represent a single argument across
1714 all three argument-related arrays. */
1715 bytes_per_arg
= (sizeof (*args_value
) + sizeof (*args_size
)
1716 + sizeof (*args_type
));
1718 /* Check for potential integer overflow. */
1719 if (__builtin_expect (nargs
> INT_MAX
/ bytes_per_arg
, 0))
1721 __set_errno (EOVERFLOW
);
1726 /* Allocate memory for all three argument arrays. */
1727 if (__libc_use_alloca (nargs
* bytes_per_arg
))
1728 args_value
= alloca (nargs
* bytes_per_arg
);
1731 args_value
= args_malloced
= malloc (nargs
* bytes_per_arg
);
1732 if (args_value
== NULL
)
1739 /* Set up the remaining two arrays to each point past the end of the
1740 prior array, since space for all three has been allocated now. */
1741 args_size
= &args_value
[nargs
].pa_int
;
1742 args_type
= &args_size
[nargs
];
1743 memset (args_type
, s
->_flags2
& _IO_FLAGS2_FORTIFY
? '\xff' : '\0',
1744 nargs
* sizeof (*args_type
));
1746 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1747 still zero after this loop, format is invalid. For now we
1748 simply use 0 as the value. */
1750 /* Fill in the types of all the arguments. */
1751 for (cnt
= 0; cnt
< nspecs
; ++cnt
)
1753 /* If the width is determined by an argument this is an int. */
1754 if (specs
[cnt
].width_arg
!= -1)
1755 args_type
[specs
[cnt
].width_arg
] = PA_INT
;
1757 /* If the precision is determined by an argument this is an int. */
1758 if (specs
[cnt
].prec_arg
!= -1)
1759 args_type
[specs
[cnt
].prec_arg
] = PA_INT
;
1761 switch (specs
[cnt
].ndata_args
)
1763 case 0: /* No arguments. */
1765 case 1: /* One argument; we already have the
1767 args_type
[specs
[cnt
].data_arg
] = specs
[cnt
].data_arg_type
;
1768 args_size
[specs
[cnt
].data_arg
] = specs
[cnt
].size
;
1771 /* We have more than one argument for this format spec.
1772 We must call the arginfo function again to determine
1774 (void) (*__printf_arginfo_table
[specs
[cnt
].info
.spec
])
1776 specs
[cnt
].ndata_args
, &args_type
[specs
[cnt
].data_arg
],
1777 &args_size
[specs
[cnt
].data_arg
]);
1782 /* Now we know all the types and the order. Fill in the argument
1784 for (cnt
= 0; cnt
< nargs
; ++cnt
)
1785 switch (args_type
[cnt
])
1787 #define T(tag, mem, type) \
1789 args_value[cnt].mem = va_arg (ap_save, type); \
1792 T (PA_WCHAR
, pa_wchar
, wint_t);
1793 case PA_CHAR
: /* Promoted. */
1794 case PA_INT
|PA_FLAG_SHORT
: /* Promoted. */
1795 #if LONG_MAX == INT_MAX
1796 case PA_INT
|PA_FLAG_LONG
:
1798 T (PA_INT
, pa_int
, int);
1799 #if LONG_MAX == LONG_LONG_MAX
1800 case PA_INT
|PA_FLAG_LONG
:
1802 T (PA_INT
|PA_FLAG_LONG_LONG
, pa_long_long_int
, long long int);
1803 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1806 case PA_FLOAT
: /* Promoted. */
1807 T (PA_DOUBLE
, pa_double
, double);
1808 case PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
:
1811 args_value
[cnt
].pa_double
= va_arg (ap_save
, double);
1812 args_type
[cnt
] &= ~PA_FLAG_LONG_DOUBLE
;
1815 args_value
[cnt
].pa_long_double
= va_arg (ap_save
, long double);
1817 case PA_STRING
: /* All pointers are the same */
1818 case PA_WSTRING
: /* All pointers are the same */
1819 T (PA_POINTER
, pa_pointer
, void *);
1822 if ((args_type
[cnt
] & PA_FLAG_PTR
) != 0)
1823 args_value
[cnt
].pa_pointer
= va_arg (ap_save
, void *);
1824 else if (__builtin_expect (__printf_va_arg_table
!= NULL
, 0)
1825 && __printf_va_arg_table
[args_type
[cnt
] - PA_LAST
] != NULL
)
1827 args_value
[cnt
].pa_user
= alloca (args_size
[cnt
]);
1828 (*__printf_va_arg_table
[args_type
[cnt
] - PA_LAST
])
1829 (args_value
[cnt
].pa_user
, &ap_save
);
1832 args_value
[cnt
].pa_long_double
= 0.0;
1835 /* Error case. Not all parameters appear in N$ format
1836 strings. We have no way to determine their type. */
1837 assert (s
->_flags2
& _IO_FLAGS2_FORTIFY
);
1838 __libc_fatal ("*** invalid %N$ use detected ***\n");
1841 /* Now walk through all format specifiers and process them. */
1842 for (; (size_t) nspecs_done
< nspecs
; ++nspecs_done
)
1846 # define REF(Name) &&do2_##Name - &&do_form_unknown
1848 # define REF(Name) &&do2_##Name
1851 #define LABEL(Name) do2_##Name
1857 unsigned long long int longlong
;
1858 unsigned long int word
;
1861 union printf_arg the_arg
;
1862 CHAR_T
*string
; /* Pointer to argument string. */
1864 /* Fill variables from values in struct. */
1865 int alt
= specs
[nspecs_done
].info
.alt
;
1866 int space
= specs
[nspecs_done
].info
.space
;
1867 int left
= specs
[nspecs_done
].info
.left
;
1868 int showsign
= specs
[nspecs_done
].info
.showsign
;
1869 int group
= specs
[nspecs_done
].info
.group
;
1870 int is_long_double
= specs
[nspecs_done
].info
.is_long_double
;
1871 int is_short
= specs
[nspecs_done
].info
.is_short
;
1872 int is_char
= specs
[nspecs_done
].info
.is_char
;
1873 int is_long
= specs
[nspecs_done
].info
.is_long
;
1874 int width
= specs
[nspecs_done
].info
.width
;
1875 int prec
= specs
[nspecs_done
].info
.prec
;
1876 int use_outdigits
= specs
[nspecs_done
].info
.i18n
;
1877 char pad
= specs
[nspecs_done
].info
.pad
;
1878 CHAR_T spec
= specs
[nspecs_done
].info
.spec
;
1881 workend
= &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
1883 /* Fill in last information. */
1884 if (specs
[nspecs_done
].width_arg
!= -1)
1886 /* Extract the field width from an argument. */
1887 specs
[nspecs_done
].info
.width
=
1888 args_value
[specs
[nspecs_done
].width_arg
].pa_int
;
1890 if (specs
[nspecs_done
].info
.width
< 0)
1891 /* If the width value is negative left justification is
1892 selected and the value is taken as being positive. */
1894 specs
[nspecs_done
].info
.width
*= -1;
1895 left
= specs
[nspecs_done
].info
.left
= 1;
1897 width
= specs
[nspecs_done
].info
.width
;
1900 if (specs
[nspecs_done
].prec_arg
!= -1)
1902 /* Extract the precision from an argument. */
1903 specs
[nspecs_done
].info
.prec
=
1904 args_value
[specs
[nspecs_done
].prec_arg
].pa_int
;
1906 if (specs
[nspecs_done
].info
.prec
< 0)
1907 /* If the precision is negative the precision is
1909 specs
[nspecs_done
].info
.prec
= -1;
1911 prec
= specs
[nspecs_done
].info
.prec
;
1914 /* Maybe the buffer is too small. */
1915 if (MAX (prec
, width
) + 32 > (int) (sizeof (work_buffer
)
1918 if (__libc_use_alloca ((MAX (prec
, width
) + 32)
1920 workend
= ((CHAR_T
*) alloca ((MAX (prec
, width
) + 32)
1922 + (MAX (prec
, width
) + 32));
1925 workstart
= (CHAR_T
*) malloc ((MAX (prec
, width
) + 32)
1927 workend
= workstart
+ (MAX (prec
, width
) + 32);
1931 /* Process format specifiers. */
1934 extern printf_function
**__printf_function_table
;
1937 if (spec
<= UCHAR_MAX
1938 && __printf_function_table
!= NULL
1939 && __printf_function_table
[(size_t) spec
] != NULL
)
1941 const void **ptr
= alloca (specs
[nspecs_done
].ndata_args
1942 * sizeof (const void *));
1944 /* Fill in an array of pointers to the argument values. */
1945 for (unsigned int i
= 0; i
< specs
[nspecs_done
].ndata_args
;
1947 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
1949 /* Call the function. */
1950 function_done
= __printf_function_table
[(size_t) spec
]
1951 (s
, &specs
[nspecs_done
].info
, ptr
);
1953 if (function_done
!= -2)
1955 /* If an error occurred we don't have information
1956 about # of chars. */
1957 if (function_done
< 0)
1959 /* Function has set errno. */
1964 done_add (function_done
);
1969 JUMP (spec
, step4_jumps
);
1971 process_arg ((&specs
[nspecs_done
]));
1972 process_string_arg ((&specs
[nspecs_done
]));
1974 LABEL (form_unknown
):
1979 ptr
= alloca (specs
[nspecs_done
].ndata_args
1980 * sizeof (const void *));
1982 /* Fill in an array of pointers to the argument values. */
1983 for (i
= 0; i
< specs
[nspecs_done
].ndata_args
; ++i
)
1984 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
1986 /* Call the function. */
1987 function_done
= printf_unknown (s
, &specs
[nspecs_done
].info
,
1990 /* If an error occurred we don't have information about #
1992 if (function_done
< 0)
1994 /* Function has set errno. */
1999 done_add (function_done
);
2007 /* Write the following constant string. */
2008 outstring (specs
[nspecs_done
].end_of_fmt
,
2009 specs
[nspecs_done
].next_fmt
2010 - specs
[nspecs_done
].end_of_fmt
);
2015 free (args_malloced
);
2017 /* Unlock the stream. */
2018 _IO_funlockfile (s
);
2019 _IO_cleanup_region_end (0);
2024 /* Handle an unknown format specifier. This prints out a canonicalized
2025 representation of the format spec itself. */
2027 printf_unknown (FILE *s
, const struct printf_info
*info
,
2028 const void *const *args
)
2032 CHAR_T work_buffer
[MAX (sizeof (info
->width
), sizeof (info
->prec
)) * 3];
2033 CHAR_T
*const workend
2034 = &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
2045 else if (info
->space
)
2049 if (info
->pad
== L_('0'))
2054 if (info
->width
!= 0)
2056 w
= _itoa_word (info
->width
, workend
, 10, 0);
2061 if (info
->prec
!= -1)
2064 w
= _itoa_word (info
->prec
, workend
, 10, 0);
2069 if (info
->spec
!= L_('\0'))
2070 outchar (info
->spec
);
2076 /* Group the digits according to the grouping rules of the current locale.
2077 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
2080 group_number (CHAR_T
*w
, CHAR_T
*rear_ptr
, const char *grouping
,
2081 #ifdef COMPILE_WPRINTF
2082 wchar_t thousands_sep
2084 const char *thousands_sep
2090 #ifndef COMPILE_WPRINTF
2091 int tlen
= strlen (thousands_sep
);
2094 /* We treat all negative values like CHAR_MAX. */
2096 if (*grouping
== CHAR_MAX
|| *grouping
<= 0)
2097 /* No grouping should be done. */
2102 /* Copy existing string so that nothing gets overwritten. */
2103 src
= (CHAR_T
*) alloca ((rear_ptr
- w
) * sizeof (CHAR_T
));
2104 s
= (CHAR_T
*) __mempcpy (src
, w
,
2105 (rear_ptr
- w
) * sizeof (CHAR_T
));
2108 /* Process all characters in the string. */
2113 if (--len
== 0 && s
> src
)
2115 /* A new group begins. */
2116 #ifdef COMPILE_WPRINTF
2117 *--w
= thousands_sep
;
2121 *--w
= thousands_sep
[--cnt
];
2125 if (*grouping
== CHAR_MAX
2131 /* No further grouping to be done.
2132 Copy the rest of the number. */
2138 else if (*grouping
!= '\0')
2139 /* The previous grouping repeats ad infinitum. */
2148 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2151 struct _IO_FILE_plus _f
;
2152 #ifdef COMPILE_WPRINTF
2153 struct _IO_wide_data _wide_data
;
2155 _IO_FILE
*_put_stream
;
2156 #ifdef _IO_MTSAFE_IO
2162 _IO_helper_overflow (_IO_FILE
*s
, int c
)
2164 _IO_FILE
*target
= ((struct helper_file
*) s
)->_put_stream
;
2165 #ifdef COMPILE_WPRINTF
2166 int used
= s
->_wide_data
->_IO_write_ptr
- s
->_wide_data
->_IO_write_base
;
2169 _IO_size_t written
= _IO_sputn (target
, s
->_wide_data
->_IO_write_base
,
2171 if (written
== 0 || written
== WEOF
)
2173 __wmemmove (s
->_wide_data
->_IO_write_base
,
2174 s
->_wide_data
->_IO_write_base
+ written
,
2176 s
->_wide_data
->_IO_write_ptr
-= written
;
2179 int used
= s
->_IO_write_ptr
- s
->_IO_write_base
;
2182 _IO_size_t written
= _IO_sputn (target
, s
->_IO_write_base
, used
);
2183 if (written
== 0 || written
== EOF
)
2185 memmove (s
->_IO_write_base
, s
->_IO_write_base
+ written
,
2187 s
->_IO_write_ptr
-= written
;
2193 #ifdef COMPILE_WPRINTF
2194 static const struct _IO_jump_t _IO_helper_jumps
=
2197 JUMP_INIT (finish
, _IO_wdefault_finish
),
2198 JUMP_INIT (overflow
, _IO_helper_overflow
),
2199 JUMP_INIT (underflow
, _IO_default_underflow
),
2200 JUMP_INIT (uflow
, _IO_default_uflow
),
2201 JUMP_INIT (pbackfail
, (_IO_pbackfail_t
) _IO_wdefault_pbackfail
),
2202 JUMP_INIT (xsputn
, _IO_wdefault_xsputn
),
2203 JUMP_INIT (xsgetn
, _IO_wdefault_xsgetn
),
2204 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2205 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2206 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2207 JUMP_INIT (sync
, _IO_default_sync
),
2208 JUMP_INIT (doallocate
, _IO_wdefault_doallocate
),
2209 JUMP_INIT (read
, _IO_default_read
),
2210 JUMP_INIT (write
, _IO_default_write
),
2211 JUMP_INIT (seek
, _IO_default_seek
),
2212 JUMP_INIT (close
, _IO_default_close
),
2213 JUMP_INIT (stat
, _IO_default_stat
)
2216 static const struct _IO_jump_t _IO_helper_jumps
=
2219 JUMP_INIT (finish
, _IO_default_finish
),
2220 JUMP_INIT (overflow
, _IO_helper_overflow
),
2221 JUMP_INIT (underflow
, _IO_default_underflow
),
2222 JUMP_INIT (uflow
, _IO_default_uflow
),
2223 JUMP_INIT (pbackfail
, _IO_default_pbackfail
),
2224 JUMP_INIT (xsputn
, _IO_default_xsputn
),
2225 JUMP_INIT (xsgetn
, _IO_default_xsgetn
),
2226 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2227 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2228 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2229 JUMP_INIT (sync
, _IO_default_sync
),
2230 JUMP_INIT (doallocate
, _IO_default_doallocate
),
2231 JUMP_INIT (read
, _IO_default_read
),
2232 JUMP_INIT (write
, _IO_default_write
),
2233 JUMP_INIT (seek
, _IO_default_seek
),
2234 JUMP_INIT (close
, _IO_default_close
),
2235 JUMP_INIT (stat
, _IO_default_stat
)
2241 buffered_vfprintf (register _IO_FILE
*s
, const CHAR_T
*format
,
2244 CHAR_T buf
[_IO_BUFSIZ
];
2245 struct helper_file helper
;
2246 register _IO_FILE
*hp
= (_IO_FILE
*) &helper
._f
;
2247 int result
, to_flush
;
2249 /* Orient the stream. */
2254 /* Initialize helper. */
2255 helper
._put_stream
= s
;
2256 #ifdef COMPILE_WPRINTF
2257 hp
->_wide_data
= &helper
._wide_data
;
2258 _IO_wsetp (hp
, buf
, buf
+ sizeof buf
/ sizeof (CHAR_T
));
2261 _IO_setp (hp
, buf
, buf
+ sizeof buf
);
2264 hp
->_IO_file_flags
= _IO_MAGIC
|_IO_NO_READS
|_IO_USER_LOCK
;
2265 #if _IO_JUMPS_OFFSET
2266 hp
->_vtable_offset
= 0;
2268 #ifdef _IO_MTSAFE_IO
2271 hp
->_flags2
= s
->_flags2
;
2272 _IO_JUMPS (&helper
._f
) = (struct _IO_jump_t
*) &_IO_helper_jumps
;
2274 /* Now print to helper instead. */
2275 #ifndef COMPILE_WPRINTF
2276 result
= _IO_vfprintf (hp
, format
, args
);
2278 result
= vfprintf (hp
, format
, args
);
2282 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile
, s
);
2285 /* Now flush anything from the helper to the S. */
2286 #ifdef COMPILE_WPRINTF
2287 if ((to_flush
= (hp
->_wide_data
->_IO_write_ptr
2288 - hp
->_wide_data
->_IO_write_base
)) > 0)
2290 if ((int) _IO_sputn (s
, hp
->_wide_data
->_IO_write_base
, to_flush
)
2295 if ((to_flush
= hp
->_IO_write_ptr
- hp
->_IO_write_base
) > 0)
2297 if ((int) _IO_sputn (s
, hp
->_IO_write_base
, to_flush
) != to_flush
)
2302 /* Unlock the stream. */
2303 _IO_funlockfile (s
);
2304 __libc_cleanup_region_end (0);
2310 #ifdef COMPILE_WPRINTF
2311 strong_alias (_IO_vfwprintf
, __vfwprintf
);
2312 ldbl_weak_alias (_IO_vfwprintf
, vfwprintf
);
2314 ldbl_strong_alias (_IO_vfprintf_internal
, vfprintf
);
2315 ldbl_hidden_def (_IO_vfprintf_internal
, vfprintf
)
2316 ldbl_strong_alias (_IO_vfprintf_internal
, _IO_vfprintf
);
2317 ldbl_hidden_def (_IO_vfprintf_internal
, _IO_vfprintf
)