1 /* Copyright (C) 1991-2020 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 <https://www.gnu.org/licenses/>. */
18 #include <array_length.h>
28 #include <libc-lock.h>
29 #include <sys/param.h>
31 #include <locale/localeinfo.h>
33 #include <scratch_buffer.h>
36 /* This code is shared between the standard stdio implementation found
37 in GNU C library and the libio implementation originally found in
40 Beside this it is also shared between the normal and wide character
41 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
45 #ifdef COMPILE_WPRINTF
49 /* In some cases we need extra space for all the output which is not
50 counted in the width of the string. We assume 32 characters is
53 #define ARGCHECK(S, Format) \
56 /* Check file argument for consistence. */ \
58 if (S->_flags & _IO_NO_WRITES) \
60 S->_flags |= _IO_ERR_SEEN; \
61 __set_errno (EBADF); \
66 __set_errno (EINVAL); \
70 #define UNBUFFERED_P(S) ((S)->_flags & _IO_UNBUFFERED)
72 #if __HAVE_FLOAT128_UNLIKE_LDBL
73 # define PARSE_FLOAT_VA_ARG_EXTENDED(INFO) \
77 && (mode_flags & PRINTF_LDBL_USES_FLOAT128) != 0) \
79 INFO.is_binary128 = 1; \
80 the_arg.pa_float128 = va_arg (ap, _Float128); \
84 PARSE_FLOAT_VA_ARG (INFO); \
89 # define PARSE_FLOAT_VA_ARG_EXTENDED(INFO) \
90 PARSE_FLOAT_VA_ARG (INFO);
93 #define PARSE_FLOAT_VA_ARG(INFO) \
96 INFO.is_binary128 = 0; \
98 the_arg.pa_long_double = va_arg (ap, long double); \
100 the_arg.pa_double = va_arg (ap, double); \
104 #if __HAVE_FLOAT128_UNLIKE_LDBL
105 # define SETUP_FLOAT128_INFO(INFO) \
108 if ((mode_flags & PRINTF_LDBL_USES_FLOAT128) != 0) \
109 INFO.is_binary128 = is_long_double; \
111 INFO.is_binary128 = 0; \
115 # define SETUP_FLOAT128_INFO(INFO) \
118 INFO.is_binary128 = 0; \
123 /* Add LENGTH to DONE. Return the new value of DONE, or -1 on
124 overflow (and set errno accordingly). */
126 done_add_func (size_t length
, int done
)
131 if (INT_ADD_WRAPV (done
, length
, &ret
))
133 __set_errno (EOVERFLOW
);
139 #define done_add(val) \
142 /* Ensure that VAL has a type similar to int. */ \
143 _Static_assert (sizeof (val) == sizeof (int), "value int size"); \
144 _Static_assert ((__typeof__ (val)) -1 < 0, "value signed"); \
145 done = done_add_func ((val), done); \
151 #ifndef COMPILE_WPRINTF
152 # define vfprintf __vfprintf_internal
154 # define OTHER_CHAR_T wchar_t
155 # define UCHAR_T unsigned char
157 typedef const char *THOUSANDS_SEP_T
;
159 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
160 # define STR_LEN(Str) strlen (Str)
162 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
163 # define PUTC(C, F) _IO_putc_unlocked (C, F)
164 # define ORIENT if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
166 # define CONVERT_FROM_OTHER_STRING __wcsrtombs
168 # define vfprintf __vfwprintf_internal
169 # define CHAR_T wchar_t
170 # define OTHER_CHAR_T char
171 /* This is a hack!!! There should be a type uwchar_t. */
172 # define UCHAR_T unsigned int /* uwchar_t */
173 # define INT_T wint_t
174 typedef wchar_t THOUSANDS_SEP_T
;
175 # define L_(Str) L##Str
176 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
177 # define STR_LEN(Str) __wcslen (Str)
181 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
182 # define PUTC(C, F) _IO_putwc_unlocked (C, F)
183 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
184 # define CONVERT_FROM_OTHER_STRING __mbsrtowcs
187 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
188 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
194 pad_func (FILE *s
, CHAR_T padchar
, int width
, int done
)
199 #ifndef COMPILE_WPRINTF
200 written
= _IO_padn (s
, padchar
, width
);
202 written
= _IO_wpadn (s
, padchar
, width
);
204 if (__glibc_unlikely (written
!= width
))
206 return done_add_func (width
, done
);
211 #define PAD(Padchar) \
214 done = pad_func (s, (Padchar), width, done); \
220 #include "_i18n_number.h"
222 /* Include the shared code for parsing the format string. */
223 #include "printf-parse.h"
226 #define outchar(Ch) \
229 const INT_T outc = (Ch); \
230 if (PUTC (outc, s) == EOF || done == INT_MAX) \
240 outstring_func (FILE *s
, const UCHAR_T
*string
, size_t length
, int done
)
242 assert ((size_t) done
<= (size_t) INT_MAX
);
243 if ((size_t) PUT (s
, string
, length
) != (size_t) (length
))
245 return done_add_func (length
, done
);
248 #define outstring(String, Len) \
251 const void *string_ = (String); \
252 done = outstring_func (s, string_, (Len), done); \
258 /* Write the string SRC to S. If PREC is non-negative, write at most
259 PREC bytes. If LEFT is true, perform left justification. */
261 outstring_converted_wide_string (FILE *s
, const OTHER_CHAR_T
*src
, int prec
,
262 int width
, bool left
, int done
)
264 /* Use a small buffer to combine processing of multiple characters.
265 CONVERT_FROM_OTHER_STRING expects the buffer size in (wide)
266 characters, and buf_length counts that. */
267 enum { buf_length
= 256 / sizeof (CHAR_T
) };
268 CHAR_T buf
[buf_length
];
269 _Static_assert (sizeof (buf
) > MB_LEN_MAX
,
270 "buffer is large enough for a single multi-byte character");
272 /* Add the initial padding if needed. */
273 if (width
> 0 && !left
)
275 /* Make a first pass to find the output width, so that we can
276 add the required padding. */
277 mbstate_t mbstate
= { 0 };
278 const OTHER_CHAR_T
*src_copy
= src
;
279 size_t total_written
;
281 total_written
= CONVERT_FROM_OTHER_STRING
282 (NULL
, &src_copy
, 0, &mbstate
);
285 /* The source might not be null-terminated. Enforce the
286 limit manually, based on the output length. */
289 while (limit
> 0 && src_copy
!= NULL
)
291 size_t write_limit
= buf_length
;
292 if (write_limit
> limit
)
294 size_t written
= CONVERT_FROM_OTHER_STRING
295 (buf
, &src_copy
, write_limit
, &mbstate
);
296 if (written
== (size_t) -1)
300 total_written
+= written
;
305 /* Output initial padding. */
306 if (total_written
< width
)
308 done
= pad_func (s
, L_(' '), width
- total_written
, done
);
314 /* Convert the input string, piece by piece. */
315 size_t total_written
= 0;
317 mbstate_t mbstate
= { 0 };
318 /* If prec is negative, remaining is not decremented, otherwise,
319 it serves as the write limit. */
320 size_t remaining
= -1;
323 while (remaining
> 0 && src
!= NULL
)
325 size_t write_limit
= buf_length
;
326 if (remaining
< write_limit
)
327 write_limit
= remaining
;
328 size_t written
= CONVERT_FROM_OTHER_STRING
329 (buf
, &src
, write_limit
, &mbstate
);
330 if (written
== (size_t) -1)
334 done
= outstring_func (s
, (const UCHAR_T
*) buf
, written
, done
);
337 total_written
+= written
;
339 remaining
-= written
;
343 /* Add final padding. */
344 if (width
> 0 && left
&& total_written
< width
)
345 return pad_func (s
, L_(' '), width
- total_written
, done
);
349 /* For handling long_double and longlong we use the same flag. If
350 `long' and `long long' are effectively the same type define it to
352 #if LONG_MAX == LONG_LONG_MAX
353 # define is_longlong 0
355 # define is_longlong is_long_double
358 /* If `long' and `int' is effectively the same type we don't have to
359 handle `long separately. */
360 #if INT_MAX == LONG_MAX
361 # define is_long_num 0
363 # define is_long_num is_long
367 /* Global constants. */
368 static const CHAR_T null
[] = L_("(null)");
370 /* Size of the work_buffer variable (in characters, not bytes. */
371 enum { WORK_BUFFER_SIZE
= 1000 / sizeof (CHAR_T
) };
373 /* This table maps a character into a number representing a class. In
374 each step there is a destination label for each class. */
375 static const uint8_t jump_table
[] =
377 /* ' ' */ 1, 0, 0, /* '#' */ 4,
378 0, /* '%' */ 14, 0, /* '\''*/ 6,
379 0, 0, /* '*' */ 7, /* '+' */ 2,
380 0, /* '-' */ 3, /* '.' */ 9, 0,
381 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
382 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
383 /* '8' */ 8, /* '9' */ 8, 0, 0,
385 0, /* 'A' */ 26, 0, /* 'C' */ 25,
386 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
387 0, /* 'I' */ 29, 0, 0,
388 /* 'L' */ 12, 0, 0, 0,
389 0, 0, 0, /* 'S' */ 21,
391 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
393 0, /* 'a' */ 26, 0, /* 'c' */ 20,
394 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
395 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
396 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
397 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
398 /* 't' */ 27, /* 'u' */ 16, 0, 0,
399 /* 'x' */ 18, 0, /* 'z' */ 13
402 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
403 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
404 #define LABEL(Name) do_##Name
406 /* 'int' is enough and it saves some space on 64 bit systems. */
407 # define JUMP_TABLE_TYPE const int
408 # define JUMP_TABLE_BASE_LABEL do_form_unknown
409 # define REF(Name) &&do_##Name - &&JUMP_TABLE_BASE_LABEL
410 # define JUMP(ChExpr, table) \
416 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
417 : table[CHAR_CLASS (spec)]; \
418 ptr = &&JUMP_TABLE_BASE_LABEL + offset; \
423 # define JUMP_TABLE_TYPE const void *const
424 # define REF(Name) &&do_##Name
425 # define JUMP(ChExpr, table) \
430 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
431 : table[CHAR_CLASS (spec)]; \
437 #define STEP0_3_TABLE \
438 /* Step 0: at the beginning. */ \
439 static JUMP_TABLE_TYPE step0_jumps[30] = \
441 REF (form_unknown), \
442 REF (flag_space), /* for ' ' */ \
443 REF (flag_plus), /* for '+' */ \
444 REF (flag_minus), /* for '-' */ \
445 REF (flag_hash), /* for '<hash>' */ \
446 REF (flag_zero), /* for '0' */ \
447 REF (flag_quote), /* for '\'' */ \
448 REF (width_asterics), /* for '*' */ \
449 REF (width), /* for '1'...'9' */ \
450 REF (precision), /* for '.' */ \
451 REF (mod_half), /* for 'h' */ \
452 REF (mod_long), /* for 'l' */ \
453 REF (mod_longlong), /* for 'L', 'q' */ \
454 REF (mod_size_t), /* for 'z', 'Z' */ \
455 REF (form_percent), /* for '%' */ \
456 REF (form_integer), /* for 'd', 'i' */ \
457 REF (form_unsigned), /* for 'u' */ \
458 REF (form_octal), /* for 'o' */ \
459 REF (form_hexa), /* for 'X', 'x' */ \
460 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
461 REF (form_character), /* for 'c' */ \
462 REF (form_string), /* for 's', 'S' */ \
463 REF (form_pointer), /* for 'p' */ \
464 REF (form_number), /* for 'n' */ \
465 REF (form_strerror), /* for 'm' */ \
466 REF (form_wcharacter), /* for 'C' */ \
467 REF (form_floathex), /* for 'A', 'a' */ \
468 REF (mod_ptrdiff_t), /* for 't' */ \
469 REF (mod_intmax_t), /* for 'j' */ \
470 REF (flag_i18n), /* for 'I' */ \
472 /* Step 1: after processing width. */ \
473 static JUMP_TABLE_TYPE step1_jumps[30] = \
475 REF (form_unknown), \
476 REF (form_unknown), /* for ' ' */ \
477 REF (form_unknown), /* for '+' */ \
478 REF (form_unknown), /* for '-' */ \
479 REF (form_unknown), /* for '<hash>' */ \
480 REF (form_unknown), /* for '0' */ \
481 REF (form_unknown), /* for '\'' */ \
482 REF (form_unknown), /* for '*' */ \
483 REF (form_unknown), /* for '1'...'9' */ \
484 REF (precision), /* for '.' */ \
485 REF (mod_half), /* for 'h' */ \
486 REF (mod_long), /* for 'l' */ \
487 REF (mod_longlong), /* for 'L', 'q' */ \
488 REF (mod_size_t), /* for 'z', 'Z' */ \
489 REF (form_percent), /* for '%' */ \
490 REF (form_integer), /* for 'd', 'i' */ \
491 REF (form_unsigned), /* for 'u' */ \
492 REF (form_octal), /* for 'o' */ \
493 REF (form_hexa), /* for 'X', 'x' */ \
494 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
495 REF (form_character), /* for 'c' */ \
496 REF (form_string), /* for 's', 'S' */ \
497 REF (form_pointer), /* for 'p' */ \
498 REF (form_number), /* for 'n' */ \
499 REF (form_strerror), /* for 'm' */ \
500 REF (form_wcharacter), /* for 'C' */ \
501 REF (form_floathex), /* for 'A', 'a' */ \
502 REF (mod_ptrdiff_t), /* for 't' */ \
503 REF (mod_intmax_t), /* for 'j' */ \
504 REF (form_unknown) /* for 'I' */ \
506 /* Step 2: after processing precision. */ \
507 static JUMP_TABLE_TYPE step2_jumps[30] = \
509 REF (form_unknown), \
510 REF (form_unknown), /* for ' ' */ \
511 REF (form_unknown), /* for '+' */ \
512 REF (form_unknown), /* for '-' */ \
513 REF (form_unknown), /* for '<hash>' */ \
514 REF (form_unknown), /* for '0' */ \
515 REF (form_unknown), /* for '\'' */ \
516 REF (form_unknown), /* for '*' */ \
517 REF (form_unknown), /* for '1'...'9' */ \
518 REF (form_unknown), /* for '.' */ \
519 REF (mod_half), /* for 'h' */ \
520 REF (mod_long), /* for 'l' */ \
521 REF (mod_longlong), /* for 'L', 'q' */ \
522 REF (mod_size_t), /* for 'z', 'Z' */ \
523 REF (form_percent), /* for '%' */ \
524 REF (form_integer), /* for 'd', 'i' */ \
525 REF (form_unsigned), /* for 'u' */ \
526 REF (form_octal), /* for 'o' */ \
527 REF (form_hexa), /* for 'X', 'x' */ \
528 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
529 REF (form_character), /* for 'c' */ \
530 REF (form_string), /* for 's', 'S' */ \
531 REF (form_pointer), /* for 'p' */ \
532 REF (form_number), /* for 'n' */ \
533 REF (form_strerror), /* for 'm' */ \
534 REF (form_wcharacter), /* for 'C' */ \
535 REF (form_floathex), /* for 'A', 'a' */ \
536 REF (mod_ptrdiff_t), /* for 't' */ \
537 REF (mod_intmax_t), /* for 'j' */ \
538 REF (form_unknown) /* for 'I' */ \
540 /* Step 3a: after processing first 'h' modifier. */ \
541 static JUMP_TABLE_TYPE step3a_jumps[30] = \
543 REF (form_unknown), \
544 REF (form_unknown), /* for ' ' */ \
545 REF (form_unknown), /* for '+' */ \
546 REF (form_unknown), /* for '-' */ \
547 REF (form_unknown), /* for '<hash>' */ \
548 REF (form_unknown), /* for '0' */ \
549 REF (form_unknown), /* for '\'' */ \
550 REF (form_unknown), /* for '*' */ \
551 REF (form_unknown), /* for '1'...'9' */ \
552 REF (form_unknown), /* for '.' */ \
553 REF (mod_halfhalf), /* for 'h' */ \
554 REF (form_unknown), /* for 'l' */ \
555 REF (form_unknown), /* for 'L', 'q' */ \
556 REF (form_unknown), /* for 'z', 'Z' */ \
557 REF (form_percent), /* for '%' */ \
558 REF (form_integer), /* for 'd', 'i' */ \
559 REF (form_unsigned), /* for 'u' */ \
560 REF (form_octal), /* for 'o' */ \
561 REF (form_hexa), /* for 'X', 'x' */ \
562 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
563 REF (form_unknown), /* for 'c' */ \
564 REF (form_unknown), /* for 's', 'S' */ \
565 REF (form_unknown), /* for 'p' */ \
566 REF (form_number), /* for 'n' */ \
567 REF (form_unknown), /* for 'm' */ \
568 REF (form_unknown), /* for 'C' */ \
569 REF (form_unknown), /* for 'A', 'a' */ \
570 REF (form_unknown), /* for 't' */ \
571 REF (form_unknown), /* for 'j' */ \
572 REF (form_unknown) /* for 'I' */ \
574 /* Step 3b: after processing first 'l' modifier. */ \
575 static JUMP_TABLE_TYPE step3b_jumps[30] = \
577 REF (form_unknown), \
578 REF (form_unknown), /* for ' ' */ \
579 REF (form_unknown), /* for '+' */ \
580 REF (form_unknown), /* for '-' */ \
581 REF (form_unknown), /* for '<hash>' */ \
582 REF (form_unknown), /* for '0' */ \
583 REF (form_unknown), /* for '\'' */ \
584 REF (form_unknown), /* for '*' */ \
585 REF (form_unknown), /* for '1'...'9' */ \
586 REF (form_unknown), /* for '.' */ \
587 REF (form_unknown), /* for 'h' */ \
588 REF (mod_longlong), /* for 'l' */ \
589 REF (form_unknown), /* for 'L', 'q' */ \
590 REF (form_unknown), /* for 'z', 'Z' */ \
591 REF (form_percent), /* for '%' */ \
592 REF (form_integer), /* for 'd', 'i' */ \
593 REF (form_unsigned), /* for 'u' */ \
594 REF (form_octal), /* for 'o' */ \
595 REF (form_hexa), /* for 'X', 'x' */ \
596 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
597 REF (form_character), /* for 'c' */ \
598 REF (form_string), /* for 's', 'S' */ \
599 REF (form_pointer), /* for 'p' */ \
600 REF (form_number), /* for 'n' */ \
601 REF (form_strerror), /* for 'm' */ \
602 REF (form_wcharacter), /* for 'C' */ \
603 REF (form_floathex), /* for 'A', 'a' */ \
604 REF (form_unknown), /* for 't' */ \
605 REF (form_unknown), /* for 'j' */ \
606 REF (form_unknown) /* for 'I' */ \
609 #define STEP4_TABLE \
610 /* Step 4: processing format specifier. */ \
611 static JUMP_TABLE_TYPE step4_jumps[30] = \
613 REF (form_unknown), \
614 REF (form_unknown), /* for ' ' */ \
615 REF (form_unknown), /* for '+' */ \
616 REF (form_unknown), /* for '-' */ \
617 REF (form_unknown), /* for '<hash>' */ \
618 REF (form_unknown), /* for '0' */ \
619 REF (form_unknown), /* for '\'' */ \
620 REF (form_unknown), /* for '*' */ \
621 REF (form_unknown), /* for '1'...'9' */ \
622 REF (form_unknown), /* for '.' */ \
623 REF (form_unknown), /* for 'h' */ \
624 REF (form_unknown), /* for 'l' */ \
625 REF (form_unknown), /* for 'L', 'q' */ \
626 REF (form_unknown), /* for 'z', 'Z' */ \
627 REF (form_percent), /* for '%' */ \
628 REF (form_integer), /* for 'd', 'i' */ \
629 REF (form_unsigned), /* for 'u' */ \
630 REF (form_octal), /* for 'o' */ \
631 REF (form_hexa), /* for 'X', 'x' */ \
632 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
633 REF (form_character), /* for 'c' */ \
634 REF (form_string), /* for 's', 'S' */ \
635 REF (form_pointer), /* for 'p' */ \
636 REF (form_number), /* for 'n' */ \
637 REF (form_strerror), /* for 'm' */ \
638 REF (form_wcharacter), /* for 'C' */ \
639 REF (form_floathex), /* for 'A', 'a' */ \
640 REF (form_unknown), /* for 't' */ \
641 REF (form_unknown), /* for 'j' */ \
642 REF (form_unknown) /* for 'I' */ \
646 #define process_arg(fspec) \
647 /* Start real work. We know about all flags and modifiers and \
648 now process the wanted format specifier. */ \
649 LABEL (form_percent): \
650 /* Write a literal "%". */ \
654 LABEL (form_integer): \
655 /* Signed decimal integer. */ \
660 long long int signed_number; \
663 signed_number = va_arg (ap, long long int); \
665 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
667 is_negative = signed_number < 0; \
668 number.longlong = is_negative ? (- signed_number) : signed_number; \
670 goto LABEL (longlong_number); \
674 long int signed_number; \
679 signed_number = va_arg (ap, long int); \
681 signed_number = (signed char) va_arg (ap, unsigned int); \
682 else if (!is_short) \
683 signed_number = va_arg (ap, int); \
685 signed_number = (short int) va_arg (ap, unsigned int); \
689 signed_number = args_value[fspec->data_arg].pa_long_int; \
691 signed_number = (signed char) \
692 args_value[fspec->data_arg].pa_u_int; \
693 else if (!is_short) \
694 signed_number = args_value[fspec->data_arg].pa_int; \
696 signed_number = (short int) \
697 args_value[fspec->data_arg].pa_u_int; \
699 is_negative = signed_number < 0; \
700 number.word = is_negative ? (- signed_number) : signed_number; \
702 goto LABEL (number); \
706 LABEL (form_unsigned): \
707 /* Unsigned decimal integer. */ \
709 goto LABEL (unsigned_number); \
712 LABEL (form_octal): \
713 /* Unsigned octal integer. */ \
715 goto LABEL (unsigned_number); \
719 /* Unsigned hexadecimal integer. */ \
722 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
724 /* ISO specifies the `+' and ` ' flags only for signed \
733 number.longlong = va_arg (ap, unsigned long long int); \
735 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
737 LABEL (longlong_number): \
739 /* Supply a default precision if none was given. */ \
742 /* We have to take care for the '0' flag. If a precision \
743 is given it must be ignored. */ \
746 /* If the precision is 0 and the number is 0 nothing has to \
747 be written for the number, except for the 'o' format in \
749 if (prec == 0 && number.longlong == 0) \
752 if (base == 8 && alt) \
753 *--string = L_('0'); \
757 /* Put the number in WORK. */ \
758 string = _itoa (number.longlong, workend, base, \
760 if (group && grouping) \
761 string = group_number (work_buffer, string, workend, \
762 grouping, thousands_sep); \
763 if (use_outdigits && base == 10) \
764 string = _i18n_number_rewrite (string, workend, workend); \
766 /* Simplify further test for num != 0. */ \
767 number.word = number.longlong != 0; \
774 number.word = va_arg (ap, unsigned long int); \
776 number.word = (unsigned char) va_arg (ap, unsigned int); \
777 else if (!is_short) \
778 number.word = va_arg (ap, unsigned int); \
780 number.word = (unsigned short int) va_arg (ap, unsigned int); \
784 number.word = args_value[fspec->data_arg].pa_u_long_int; \
786 number.word = (unsigned char) \
787 args_value[fspec->data_arg].pa_u_int; \
788 else if (!is_short) \
789 number.word = args_value[fspec->data_arg].pa_u_int; \
791 number.word = (unsigned short int) \
792 args_value[fspec->data_arg].pa_u_int; \
796 /* Supply a default precision if none was given. */ \
799 /* We have to take care for the '0' flag. If a precision \
800 is given it must be ignored. */ \
803 /* If the precision is 0 and the number is 0 nothing has to \
804 be written for the number, except for the 'o' format in \
806 if (prec == 0 && number.word == 0) \
809 if (base == 8 && alt) \
810 *--string = L_('0'); \
814 /* Put the number in WORK. */ \
815 string = _itoa_word (number.word, workend, base, \
817 if (group && grouping) \
818 string = group_number (work_buffer, string, workend, \
819 grouping, thousands_sep); \
820 if (use_outdigits && base == 10) \
821 string = _i18n_number_rewrite (string, workend, workend); \
825 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
826 /* Add octal marker. */ \
827 *--string = L_('0'); \
829 prec = MAX (0, prec - (workend - string)); \
833 width -= workend - string + prec; \
835 if (number.word != 0 && alt && base == 16) \
836 /* Account for 0X hex marker. */ \
839 if (is_negative || showsign || space) \
842 if (pad == L_(' ')) \
855 if (number.word != 0 && alt && base == 16) \
864 outstring (string, workend - string); \
886 if (number.word != 0 && alt && base == 16) \
893 width -= workend - string + prec; \
903 outstring (string, workend - string); \
909 LABEL (form_float): \
911 /* Floating-point number. This is handled by printf_fp.c. */ \
917 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0)) \
918 is_long_double = 0; \
920 struct printf_info info = { .prec = prec, \
923 .is_long_double = is_long_double, \
924 .is_short = is_short, \
925 .is_long = is_long, \
929 .showsign = showsign, \
933 .i18n = use_outdigits, \
934 .wide = sizeof (CHAR_T) != 1, \
935 .is_binary128 = 0}; \
937 PARSE_FLOAT_VA_ARG_EXTENDED (info); \
938 ptr = (const void *) &the_arg; \
940 function_done = __printf_fp (s, &info, &ptr); \
944 ptr = (const void *) &args_value[fspec->data_arg]; \
945 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0)) \
947 fspec->data_arg_type = PA_DOUBLE; \
948 fspec->info.is_long_double = 0; \
950 SETUP_FLOAT128_INFO (fspec->info); \
952 function_done = __printf_fp (s, &fspec->info, &ptr); \
955 if (function_done < 0) \
957 /* Error in print handler; up to handler to set errno. */ \
962 done_add (function_done); \
966 LABEL (form_floathex): \
968 /* Floating point number printed as hexadecimal number. */ \
974 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0)) \
975 is_long_double = 0; \
977 struct printf_info info = { .prec = prec, \
980 .is_long_double = is_long_double, \
981 .is_short = is_short, \
982 .is_long = is_long, \
986 .showsign = showsign, \
990 .wide = sizeof (CHAR_T) != 1, \
991 .is_binary128 = 0}; \
993 PARSE_FLOAT_VA_ARG_EXTENDED (info); \
994 ptr = (const void *) &the_arg; \
996 function_done = __printf_fphex (s, &info, &ptr); \
1000 ptr = (const void *) &args_value[fspec->data_arg]; \
1001 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0)) \
1002 fspec->info.is_long_double = 0; \
1003 SETUP_FLOAT128_INFO (fspec->info); \
1005 function_done = __printf_fphex (s, &fspec->info, &ptr); \
1008 if (function_done < 0) \
1010 /* Error in print handler; up to handler to set errno. */ \
1015 done_add (function_done); \
1019 LABEL (form_pointer): \
1020 /* Generic pointer. */ \
1023 if (fspec == NULL) \
1024 ptr = va_arg (ap, void *); \
1026 ptr = args_value[fspec->data_arg].pa_pointer; \
1029 /* If the pointer is not NULL, write it as a %#x spec. */ \
1031 number.word = (unsigned long int) ptr; \
1036 goto LABEL (number); \
1040 /* Write "(nil)" for a nil pointer. */ \
1041 string = (CHAR_T *) L_("(nil)"); \
1042 /* Make sure the full string "(nil)" is printed. */ \
1045 /* This is a wide string iff compiling wprintf. */ \
1046 is_long = sizeof (CHAR_T) > 1; \
1047 goto LABEL (print_string); \
1052 LABEL (form_number): \
1053 if ((mode_flags & PRINTF_FORTIFY) != 0) \
1055 if (! readonly_format) \
1057 extern int __readonly_area (const void *, size_t) \
1060 = __readonly_area (format, ((STR_LEN (format) + 1) \
1061 * sizeof (CHAR_T))); \
1063 if (readonly_format < 0) \
1064 __libc_fatal ("*** %n in writable segment detected ***\n"); \
1066 /* Answer the count of characters written. */ \
1067 if (fspec == NULL) \
1070 *(long long int *) va_arg (ap, void *) = done; \
1071 else if (is_long_num) \
1072 *(long int *) va_arg (ap, void *) = done; \
1074 *(char *) va_arg (ap, void *) = done; \
1075 else if (!is_short) \
1076 *(int *) va_arg (ap, void *) = done; \
1078 *(short int *) va_arg (ap, void *) = done; \
1082 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
1083 else if (is_long_num) \
1084 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
1086 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
1087 else if (!is_short) \
1088 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
1090 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
1093 LABEL (form_strerror): \
1094 /* Print description of error ERRNO. */ \
1096 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
1097 WORK_BUFFER_SIZE * sizeof (CHAR_T)); \
1098 is_long = 0; /* This is no wide-char string. */ \
1099 goto LABEL (print_string)
1101 #ifdef COMPILE_WPRINTF
1102 # define process_string_arg(fspec) \
1103 LABEL (form_character): \
1106 goto LABEL (form_wcharacter); \
1107 --width; /* Account for the character itself. */ \
1110 if (fspec == NULL) \
1111 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
1113 outchar (__btowc ((unsigned char) \
1114 args_value[fspec->data_arg].pa_int)); \
1119 LABEL (form_wcharacter): \
1121 /* Wide character. */ \
1125 if (fspec == NULL) \
1126 outchar (va_arg (ap, wchar_t)); \
1128 outchar (args_value[fspec->data_arg].pa_wchar); \
1134 LABEL (form_string): \
1138 /* The string argument could in fact be `char *' or `wchar_t *'. \
1139 But this should not make a difference here. */ \
1140 if (fspec == NULL) \
1141 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
1143 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
1145 /* Entry point for printing other strings. */ \
1146 LABEL (print_string): \
1148 if (string == NULL) \
1150 /* Write "(null)" if there's space. */ \
1151 if (prec == -1 || prec >= (int) array_length (null) - 1) \
1153 string = (CHAR_T *) null; \
1154 len = array_length (null) - 1; \
1158 string = (CHAR_T *) L""; \
1162 else if (!is_long && spec != L_('S')) \
1164 done = outstring_converted_wide_string \
1165 (s, (const char *) string, prec, width, left, done); \
1168 /* The padding has already been written. */ \
1174 /* Search for the end of the string, but don't search past \
1175 the length specified by the precision. */ \
1176 len = __wcsnlen (string, prec); \
1178 len = __wcslen (string); \
1181 if ((width -= len) < 0) \
1183 outstring (string, len); \
1189 outstring (string, len); \
1195 # define process_string_arg(fspec) \
1196 LABEL (form_character): \
1199 goto LABEL (form_wcharacter); \
1200 --width; /* Account for the character itself. */ \
1203 if (fspec == NULL) \
1204 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1206 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1211 LABEL (form_wcharacter): \
1213 /* Wide character. */ \
1214 char buf[MB_LEN_MAX]; \
1215 mbstate_t mbstate; \
1218 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1219 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1220 : args_value[fspec->data_arg].pa_wchar), \
1222 if (len == (size_t) -1) \
1224 /* Something went wrong during the conversion. Bail out. */ \
1231 outstring (buf, len); \
1237 LABEL (form_string): \
1241 /* The string argument could in fact be `char *' or `wchar_t *'. \
1242 But this should not make a difference here. */ \
1243 if (fspec == NULL) \
1244 string = (char *) va_arg (ap, const char *); \
1246 string = (char *) args_value[fspec->data_arg].pa_string; \
1248 /* Entry point for printing other strings. */ \
1249 LABEL (print_string): \
1251 if (string == NULL) \
1253 /* Write "(null)" if there's space. */ \
1254 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1256 string = (char *) null; \
1257 len = sizeof (null) - 1; \
1261 string = (char *) ""; \
1265 else if (!is_long && spec != L_('S')) \
1268 /* Search for the end of the string, but don't search past \
1269 the length (in bytes) specified by the precision. */ \
1270 len = __strnlen (string, prec); \
1272 len = strlen (string); \
1276 done = outstring_converted_wide_string \
1277 (s, (const wchar_t *) string, prec, width, left, done); \
1280 /* The padding has already been written. */ \
1284 if ((width -= len) < 0) \
1286 outstring (string, len); \
1292 outstring (string, len); \
1299 /* Helper function to provide temporary buffering for unbuffered streams. */
1300 static int buffered_vfprintf (FILE *stream
, const CHAR_T
*fmt
, va_list,
1302 __THROW
__attribute__ ((noinline
));
1304 /* Handle positional format specifiers. */
1305 static int printf_positional (FILE *s
,
1306 const CHAR_T
*format
, int readonly_format
,
1307 va_list ap
, va_list *ap_savep
, int done
,
1308 int nspecs_done
, const UCHAR_T
*lead_str_end
,
1309 CHAR_T
*work_buffer
, int save_errno
,
1310 const char *grouping
,
1311 THOUSANDS_SEP_T thousands_sep
,
1312 unsigned int mode_flags
);
1314 /* Handle unknown format specifier. */
1315 static int printf_unknown (FILE *, const struct printf_info
*,
1316 const void *const *) __THROW
;
1318 /* Group digits of number string. */
1319 static CHAR_T
*group_number (CHAR_T
*, CHAR_T
*, CHAR_T
*, const char *,
1322 /* The function itself. */
1324 vfprintf (FILE *s
, const CHAR_T
*format
, va_list ap
, unsigned int mode_flags
)
1326 /* The character used as thousands separator. */
1327 THOUSANDS_SEP_T thousands_sep
= 0;
1329 /* The string describing the size of groups of digits. */
1330 const char *grouping
;
1332 /* Place to accumulate the result. */
1335 /* Current character in format string. */
1338 /* End of leading constant string. */
1339 const UCHAR_T
*lead_str_end
;
1341 /* Points to next format specifier. */
1342 const UCHAR_T
*end_of_spec
;
1344 /* Buffer intermediate results. */
1345 CHAR_T work_buffer
[WORK_BUFFER_SIZE
];
1346 CHAR_T
*workstart
= NULL
;
1349 /* We have to save the original argument pointer. */
1352 /* Count number of specifiers we already processed. */
1355 /* For the %m format we may need the current `errno' value. */
1356 int save_errno
= errno
;
1358 /* 1 if format is in read-only memory, -1 if it is in writable memory,
1360 int readonly_format
= 0;
1362 /* Orient the stream. */
1367 /* Sanity check of arguments. */
1368 ARGCHECK (s
, format
);
1371 /* Check for correct orientation. */
1372 if (_IO_vtable_offset (s
) == 0
1373 && _IO_fwide (s
, sizeof (CHAR_T
) == 1 ? -1 : 1)
1374 != (sizeof (CHAR_T
) == 1 ? -1 : 1))
1375 /* The stream is already oriented otherwise. */
1379 if (UNBUFFERED_P (s
))
1380 /* Use a helper function which will allocate a local temporary buffer
1381 for the stream and then call us again. */
1382 return buffered_vfprintf (s
, format
, ap
, mode_flags
);
1384 /* Initialize local variables. */
1386 grouping
= (const char *) -1;
1388 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1389 since on some systems `va_list' is not an integral type. */
1390 __va_copy (ap_save
, ap
);
1396 #ifdef COMPILE_WPRINTF
1397 /* Find the first format specifier. */
1398 f
= lead_str_end
= __find_specwc ((const UCHAR_T
*) format
);
1400 /* Find the first format specifier. */
1401 f
= lead_str_end
= __find_specmb ((const UCHAR_T
*) format
);
1405 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile
, s
);
1408 /* Write the literal text before the first format. */
1409 outstring ((const UCHAR_T
*) format
,
1410 lead_str_end
- (const UCHAR_T
*) format
);
1412 /* If we only have to print a simple string, return now. */
1416 /* Use the slow path in case any printf handler is registered. */
1417 if (__glibc_unlikely (__printf_function_table
!= NULL
1418 || __printf_modifier_table
!= NULL
1419 || __printf_va_arg_table
!= NULL
))
1422 /* Process whole format string. */
1428 union printf_arg
*args_value
; /* This is not used here but ... */
1429 int is_negative
; /* Flag for negative number. */
1432 unsigned long long int longlong
;
1433 unsigned long int word
;
1436 union printf_arg the_arg
;
1437 CHAR_T
*string
; /* Pointer to argument string. */
1438 int alt
= 0; /* Alternate format. */
1439 int space
= 0; /* Use space prefix if no sign is needed. */
1440 int left
= 0; /* Left-justify output. */
1441 int showsign
= 0; /* Always begin with plus or minus sign. */
1442 int group
= 0; /* Print numbers according grouping rules. */
1443 int is_long_double
= 0; /* Argument is long double/ long long int. */
1444 int is_short
= 0; /* Argument is short int. */
1445 int is_long
= 0; /* Argument is long int. */
1446 int is_char
= 0; /* Argument is promoted (unsigned) char. */
1447 int width
= 0; /* Width of output; 0 means none specified. */
1448 int prec
= -1; /* Precision of output; -1 means none specified. */
1449 /* This flag is set by the 'I' modifier and selects the use of the
1450 `outdigits' as determined by the current locale. */
1451 int use_outdigits
= 0;
1452 UCHAR_T pad
= L_(' ');/* Padding character. */
1456 workend
= work_buffer
+ WORK_BUFFER_SIZE
;
1458 /* Get current character in format string. */
1459 JUMP (*++f
, step0_jumps
);
1464 JUMP (*++f
, step0_jumps
);
1469 JUMP (*++f
, step0_jumps
);
1475 JUMP (*++f
, step0_jumps
);
1480 JUMP (*++f
, step0_jumps
);
1486 JUMP (*++f
, step0_jumps
);
1488 /* The '\'' flag. */
1492 if (grouping
== (const char *) -1)
1494 #ifdef COMPILE_WPRINTF
1495 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1496 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1498 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1501 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1502 if (*grouping
== '\0' || *grouping
== CHAR_MAX
1503 #ifdef COMPILE_WPRINTF
1504 || thousands_sep
== L
'\0'
1506 || *thousands_sep
== '\0'
1511 JUMP (*++f
, step0_jumps
);
1515 JUMP (*++f
, step0_jumps
);
1517 /* Get width from argument. */
1518 LABEL (width_asterics
):
1520 const UCHAR_T
*tmp
; /* Temporary value. */
1525 int pos
= read_int (&tmp
);
1529 __set_errno (EOVERFLOW
);
1534 if (pos
&& *tmp
== L_('$'))
1535 /* The width comes from a positional parameter. */
1538 width
= va_arg (ap
, int);
1540 /* Negative width means left justified. */
1548 if (__glibc_unlikely (width
>= INT_MAX
/ sizeof (CHAR_T
) - EXTSIZ
))
1550 __set_errno (EOVERFLOW
);
1555 if (width
>= WORK_BUFFER_SIZE
- EXTSIZ
)
1557 /* We have to use a special buffer. */
1558 size_t needed
= ((size_t) width
+ EXTSIZ
) * sizeof (CHAR_T
);
1559 if (__libc_use_alloca (needed
))
1560 workend
= (CHAR_T
*) alloca (needed
) + width
+ EXTSIZ
;
1563 workstart
= (CHAR_T
*) malloc (needed
);
1564 if (workstart
== NULL
)
1569 workend
= workstart
+ width
+ EXTSIZ
;
1573 JUMP (*f
, step1_jumps
);
1575 /* Given width in format string. */
1577 width
= read_int (&f
);
1579 if (__glibc_unlikely (width
== -1
1580 || width
>= INT_MAX
/ sizeof (CHAR_T
) - EXTSIZ
))
1582 __set_errno (EOVERFLOW
);
1587 if (width
>= WORK_BUFFER_SIZE
- EXTSIZ
)
1589 /* We have to use a special buffer. */
1590 size_t needed
= ((size_t) width
+ EXTSIZ
) * sizeof (CHAR_T
);
1591 if (__libc_use_alloca (needed
))
1592 workend
= (CHAR_T
*) alloca (needed
) + width
+ EXTSIZ
;
1595 workstart
= (CHAR_T
*) malloc (needed
);
1596 if (workstart
== NULL
)
1601 workend
= workstart
+ width
+ EXTSIZ
;
1605 /* Oh, oh. The argument comes from a positional parameter. */
1607 JUMP (*f
, step1_jumps
);
1613 const UCHAR_T
*tmp
; /* Temporary value. */
1618 int pos
= read_int (&tmp
);
1622 __set_errno (EOVERFLOW
);
1627 if (pos
&& *tmp
== L_('$'))
1628 /* The precision comes from a positional parameter. */
1631 prec
= va_arg (ap
, int);
1633 /* If the precision is negative the precision is omitted. */
1637 else if (ISDIGIT (*f
))
1639 prec
= read_int (&f
);
1641 /* The precision was specified in this case as an extremely
1642 large positive value. */
1645 __set_errno (EOVERFLOW
);
1652 if (prec
> width
&& prec
> WORK_BUFFER_SIZE
- EXTSIZ
)
1654 /* Deallocate any previously allocated buffer because it is
1656 if (__glibc_unlikely (workstart
!= NULL
))
1659 if (__glibc_unlikely (prec
>= INT_MAX
/ sizeof (CHAR_T
) - EXTSIZ
))
1661 __set_errno (EOVERFLOW
);
1665 size_t needed
= ((size_t) prec
+ EXTSIZ
) * sizeof (CHAR_T
);
1667 if (__libc_use_alloca (needed
))
1668 workend
= (CHAR_T
*) alloca (needed
) + prec
+ EXTSIZ
;
1671 workstart
= (CHAR_T
*) malloc (needed
);
1672 if (workstart
== NULL
)
1677 workend
= workstart
+ prec
+ EXTSIZ
;
1680 JUMP (*f
, step2_jumps
);
1682 /* Process 'h' modifier. There might another 'h' following. */
1685 JUMP (*++f
, step3a_jumps
);
1687 /* Process 'hh' modifier. */
1688 LABEL (mod_halfhalf
):
1691 JUMP (*++f
, step4_jumps
);
1693 /* Process 'l' modifier. There might another 'l' following. */
1696 JUMP (*++f
, step3b_jumps
);
1698 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1699 allowed to follow. */
1700 LABEL (mod_longlong
):
1703 JUMP (*++f
, step4_jumps
);
1706 is_long_double
= sizeof (size_t) > sizeof (unsigned long int);
1707 is_long
= sizeof (size_t) > sizeof (unsigned int);
1708 JUMP (*++f
, step4_jumps
);
1710 LABEL (mod_ptrdiff_t
):
1711 is_long_double
= sizeof (ptrdiff_t) > sizeof (unsigned long int);
1712 is_long
= sizeof (ptrdiff_t) > sizeof (unsigned int);
1713 JUMP (*++f
, step4_jumps
);
1715 LABEL (mod_intmax_t
):
1716 is_long_double
= sizeof (intmax_t) > sizeof (unsigned long int);
1717 is_long
= sizeof (intmax_t) > sizeof (unsigned int);
1718 JUMP (*++f
, step4_jumps
);
1720 /* Process current format. */
1723 process_arg (((struct printf_spec
*) NULL
));
1724 process_string_arg (((struct printf_spec
*) NULL
));
1726 LABEL (form_unknown
):
1727 if (spec
== L_('\0'))
1729 /* The format string ended before the specifier is complete. */
1730 __set_errno (EINVAL
);
1735 /* If we are in the fast loop force entering the complicated
1740 /* The format is correctly handled. */
1743 if (__glibc_unlikely (workstart
!= NULL
))
1747 /* Look for next format specifier. */
1748 #ifdef COMPILE_WPRINTF
1749 f
= __find_specwc ((end_of_spec
= ++f
));
1751 f
= __find_specmb ((end_of_spec
= ++f
));
1754 /* Write the following constant string. */
1755 outstring (end_of_spec
, f
- end_of_spec
);
1757 while (*f
!= L_('\0'));
1759 /* Unlock stream and return. */
1762 /* Hand off processing for positional parameters. */
1764 if (__glibc_unlikely (workstart
!= NULL
))
1769 done
= printf_positional (s
, format
, readonly_format
, ap
, &ap_save
,
1770 done
, nspecs_done
, lead_str_end
, work_buffer
,
1771 save_errno
, grouping
, thousands_sep
, mode_flags
);
1774 if (__glibc_unlikely (workstart
!= NULL
))
1776 /* Unlock the stream. */
1777 _IO_funlockfile (s
);
1778 _IO_cleanup_region_end (0);
1784 printf_positional (FILE *s
, const CHAR_T
*format
, int readonly_format
,
1785 va_list ap
, va_list *ap_savep
, int done
, int nspecs_done
,
1786 const UCHAR_T
*lead_str_end
,
1787 CHAR_T
*work_buffer
, int save_errno
,
1788 const char *grouping
, THOUSANDS_SEP_T thousands_sep
,
1789 unsigned int mode_flags
)
1791 /* For positional argument handling. */
1792 struct scratch_buffer specsbuf
;
1793 scratch_buffer_init (&specsbuf
);
1794 struct printf_spec
*specs
= specsbuf
.data
;
1795 size_t specs_limit
= specsbuf
.length
/ sizeof (specs
[0]);
1797 /* Used as a backing store for args_value, args_size, args_type
1799 struct scratch_buffer argsbuf
;
1800 scratch_buffer_init (&argsbuf
);
1802 /* Array with information about the needed arguments. This has to
1803 be dynamically extensible. */
1806 /* The number of arguments the format string requests. This will
1807 determine the size of the array needed to store the argument
1811 /* Positional parameters refer to arguments directly. This could
1812 also determine the maximum number of arguments. Track the
1814 size_t max_ref_arg
= 0;
1816 /* Just a counter. */
1819 CHAR_T
*workstart
= NULL
;
1821 if (grouping
== (const char *) -1)
1823 #ifdef COMPILE_WPRINTF
1824 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1825 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1827 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1830 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1831 if (*grouping
== '\0' || *grouping
== CHAR_MAX
)
1835 for (const UCHAR_T
*f
= lead_str_end
; *f
!= L_('\0');
1836 f
= specs
[nspecs
++].next_fmt
)
1838 if (nspecs
== specs_limit
)
1840 if (!scratch_buffer_grow_preserve (&specsbuf
))
1845 specs
= specsbuf
.data
;
1846 specs_limit
= specsbuf
.length
/ sizeof (specs
[0]);
1849 /* Parse the format specifier. */
1850 #ifdef COMPILE_WPRINTF
1851 nargs
+= __parse_one_specwc (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1853 nargs
+= __parse_one_specmb (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1857 /* Determine the number of arguments the format string consumes. */
1858 nargs
= MAX (nargs
, max_ref_arg
);
1860 union printf_arg
*args_value
;
1864 /* Calculate total size needed to represent a single argument
1865 across all three argument-related arrays. */
1866 size_t bytes_per_arg
1867 = sizeof (*args_value
) + sizeof (*args_size
) + sizeof (*args_type
);
1868 if (!scratch_buffer_set_array_size (&argsbuf
, nargs
, bytes_per_arg
))
1873 args_value
= argsbuf
.data
;
1874 /* Set up the remaining two arrays to each point past the end of
1875 the prior array, since space for all three has been allocated
1877 args_size
= &args_value
[nargs
].pa_int
;
1878 args_type
= &args_size
[nargs
];
1879 memset (args_type
, (mode_flags
& PRINTF_FORTIFY
) != 0 ? '\xff' : '\0',
1880 nargs
* sizeof (*args_type
));
1883 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1884 still zero after this loop, format is invalid. For now we
1885 simply use 0 as the value. */
1887 /* Fill in the types of all the arguments. */
1888 for (cnt
= 0; cnt
< nspecs
; ++cnt
)
1890 /* If the width is determined by an argument this is an int. */
1891 if (specs
[cnt
].width_arg
!= -1)
1892 args_type
[specs
[cnt
].width_arg
] = PA_INT
;
1894 /* If the precision is determined by an argument this is an int. */
1895 if (specs
[cnt
].prec_arg
!= -1)
1896 args_type
[specs
[cnt
].prec_arg
] = PA_INT
;
1898 switch (specs
[cnt
].ndata_args
)
1900 case 0: /* No arguments. */
1902 case 1: /* One argument; we already have the
1904 args_type
[specs
[cnt
].data_arg
] = specs
[cnt
].data_arg_type
;
1905 args_size
[specs
[cnt
].data_arg
] = specs
[cnt
].size
;
1908 /* We have more than one argument for this format spec.
1909 We must call the arginfo function again to determine
1911 (void) (*__printf_arginfo_table
[specs
[cnt
].info
.spec
])
1913 specs
[cnt
].ndata_args
, &args_type
[specs
[cnt
].data_arg
],
1914 &args_size
[specs
[cnt
].data_arg
]);
1919 /* Now we know all the types and the order. Fill in the argument
1921 for (cnt
= 0; cnt
< nargs
; ++cnt
)
1922 switch (args_type
[cnt
])
1924 #define T(tag, mem, type) \
1926 args_value[cnt].mem = va_arg (*ap_savep, type); \
1929 T (PA_WCHAR
, pa_wchar
, wint_t);
1930 case PA_CHAR
: /* Promoted. */
1931 case PA_INT
|PA_FLAG_SHORT
: /* Promoted. */
1932 #if LONG_MAX == INT_MAX
1933 case PA_INT
|PA_FLAG_LONG
:
1935 T (PA_INT
, pa_int
, int);
1936 #if LONG_MAX == LONG_LONG_MAX
1937 case PA_INT
|PA_FLAG_LONG
:
1939 T (PA_INT
|PA_FLAG_LONG_LONG
, pa_long_long_int
, long long int);
1940 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1943 case PA_FLOAT
: /* Promoted. */
1944 T (PA_DOUBLE
, pa_double
, double);
1945 case PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
:
1946 if (__glibc_unlikely ((mode_flags
& PRINTF_LDBL_IS_DBL
) != 0))
1948 args_value
[cnt
].pa_double
= va_arg (*ap_savep
, double);
1949 args_type
[cnt
] &= ~PA_FLAG_LONG_DOUBLE
;
1951 #if __HAVE_FLOAT128_UNLIKE_LDBL
1952 else if ((mode_flags
& PRINTF_LDBL_USES_FLOAT128
) != 0)
1953 args_value
[cnt
].pa_float128
= va_arg (*ap_savep
, _Float128
);
1956 args_value
[cnt
].pa_long_double
= va_arg (*ap_savep
, long double);
1958 case PA_STRING
: /* All pointers are the same */
1959 case PA_WSTRING
: /* All pointers are the same */
1960 T (PA_POINTER
, pa_pointer
, void *);
1963 if ((args_type
[cnt
] & PA_FLAG_PTR
) != 0)
1964 args_value
[cnt
].pa_pointer
= va_arg (*ap_savep
, void *);
1965 else if (__glibc_unlikely (__printf_va_arg_table
!= NULL
)
1966 && __printf_va_arg_table
[args_type
[cnt
] - PA_LAST
] != NULL
)
1968 args_value
[cnt
].pa_user
= alloca (args_size
[cnt
]);
1969 (*__printf_va_arg_table
[args_type
[cnt
] - PA_LAST
])
1970 (args_value
[cnt
].pa_user
, ap_savep
);
1973 memset (&args_value
[cnt
], 0, sizeof (args_value
[cnt
]));
1976 /* Error case. Not all parameters appear in N$ format
1977 strings. We have no way to determine their type. */
1978 assert ((mode_flags
& PRINTF_FORTIFY
) != 0);
1979 __libc_fatal ("*** invalid %N$ use detected ***\n");
1982 /* Now walk through all format specifiers and process them. */
1983 for (; (size_t) nspecs_done
< nspecs
; ++nspecs_done
)
1990 unsigned long long int longlong
;
1991 unsigned long int word
;
1994 union printf_arg the_arg
;
1995 CHAR_T
*string
; /* Pointer to argument string. */
1997 /* Fill variables from values in struct. */
1998 int alt
= specs
[nspecs_done
].info
.alt
;
1999 int space
= specs
[nspecs_done
].info
.space
;
2000 int left
= specs
[nspecs_done
].info
.left
;
2001 int showsign
= specs
[nspecs_done
].info
.showsign
;
2002 int group
= specs
[nspecs_done
].info
.group
;
2003 int is_long_double
= specs
[nspecs_done
].info
.is_long_double
;
2004 int is_short
= specs
[nspecs_done
].info
.is_short
;
2005 int is_char
= specs
[nspecs_done
].info
.is_char
;
2006 int is_long
= specs
[nspecs_done
].info
.is_long
;
2007 int width
= specs
[nspecs_done
].info
.width
;
2008 int prec
= specs
[nspecs_done
].info
.prec
;
2009 int use_outdigits
= specs
[nspecs_done
].info
.i18n
;
2010 char pad
= specs
[nspecs_done
].info
.pad
;
2011 CHAR_T spec
= specs
[nspecs_done
].info
.spec
;
2014 CHAR_T
*workend
= work_buffer
+ WORK_BUFFER_SIZE
;
2016 /* Fill in last information. */
2017 if (specs
[nspecs_done
].width_arg
!= -1)
2019 /* Extract the field width from an argument. */
2020 specs
[nspecs_done
].info
.width
=
2021 args_value
[specs
[nspecs_done
].width_arg
].pa_int
;
2023 if (specs
[nspecs_done
].info
.width
< 0)
2024 /* If the width value is negative left justification is
2025 selected and the value is taken as being positive. */
2027 specs
[nspecs_done
].info
.width
*= -1;
2028 left
= specs
[nspecs_done
].info
.left
= 1;
2030 width
= specs
[nspecs_done
].info
.width
;
2033 if (specs
[nspecs_done
].prec_arg
!= -1)
2035 /* Extract the precision from an argument. */
2036 specs
[nspecs_done
].info
.prec
=
2037 args_value
[specs
[nspecs_done
].prec_arg
].pa_int
;
2039 if (specs
[nspecs_done
].info
.prec
< 0)
2040 /* If the precision is negative the precision is
2042 specs
[nspecs_done
].info
.prec
= -1;
2044 prec
= specs
[nspecs_done
].info
.prec
;
2047 /* Maybe the buffer is too small. */
2048 if (MAX (prec
, width
) + EXTSIZ
> WORK_BUFFER_SIZE
)
2050 if (__libc_use_alloca ((MAX (prec
, width
) + EXTSIZ
)
2052 workend
= ((CHAR_T
*) alloca ((MAX (prec
, width
) + EXTSIZ
)
2054 + (MAX (prec
, width
) + EXTSIZ
));
2057 workstart
= (CHAR_T
*) malloc ((MAX (prec
, width
) + EXTSIZ
)
2059 if (workstart
== NULL
)
2064 workend
= workstart
+ (MAX (prec
, width
) + EXTSIZ
);
2068 /* Process format specifiers. */
2071 extern printf_function
**__printf_function_table
;
2074 if (spec
<= UCHAR_MAX
2075 && __printf_function_table
!= NULL
2076 && __printf_function_table
[(size_t) spec
] != NULL
)
2078 const void **ptr
= alloca (specs
[nspecs_done
].ndata_args
2079 * sizeof (const void *));
2081 /* Fill in an array of pointers to the argument values. */
2082 for (unsigned int i
= 0; i
< specs
[nspecs_done
].ndata_args
;
2084 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
2086 /* Call the function. */
2087 function_done
= __printf_function_table
[(size_t) spec
]
2088 (s
, &specs
[nspecs_done
].info
, ptr
);
2090 if (function_done
!= -2)
2092 /* If an error occurred we don't have information
2093 about # of chars. */
2094 if (function_done
< 0)
2096 /* Function has set errno. */
2101 done_add (function_done
);
2106 JUMP (spec
, step4_jumps
);
2108 process_arg ((&specs
[nspecs_done
]));
2109 process_string_arg ((&specs
[nspecs_done
]));
2111 LABEL (form_unknown
):
2116 ptr
= alloca (specs
[nspecs_done
].ndata_args
2117 * sizeof (const void *));
2119 /* Fill in an array of pointers to the argument values. */
2120 for (i
= 0; i
< specs
[nspecs_done
].ndata_args
; ++i
)
2121 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
2123 /* Call the function. */
2124 function_done
= printf_unknown (s
, &specs
[nspecs_done
].info
,
2127 /* If an error occurred we don't have information about #
2129 if (function_done
< 0)
2131 /* Function has set errno. */
2136 done_add (function_done
);
2141 if (__glibc_unlikely (workstart
!= NULL
))
2145 /* Write the following constant string. */
2146 outstring (specs
[nspecs_done
].end_of_fmt
,
2147 specs
[nspecs_done
].next_fmt
2148 - specs
[nspecs_done
].end_of_fmt
);
2151 if (__glibc_unlikely (workstart
!= NULL
))
2153 scratch_buffer_free (&argsbuf
);
2154 scratch_buffer_free (&specsbuf
);
2158 /* Handle an unknown format specifier. This prints out a canonicalized
2159 representation of the format spec itself. */
2161 printf_unknown (FILE *s
, const struct printf_info
*info
,
2162 const void *const *args
)
2166 CHAR_T work_buffer
[MAX (sizeof (info
->width
), sizeof (info
->prec
)) * 3];
2167 CHAR_T
*const workend
2168 = &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
2179 else if (info
->space
)
2183 if (info
->pad
== L_('0'))
2188 if (info
->width
!= 0)
2190 w
= _itoa_word (info
->width
, workend
, 10, 0);
2195 if (info
->prec
!= -1)
2198 w
= _itoa_word (info
->prec
, workend
, 10, 0);
2203 if (info
->spec
!= L_('\0'))
2204 outchar (info
->spec
);
2210 /* Group the digits from W to REAR_PTR according to the grouping rules
2211 of the current locale. The interpretation of GROUPING is as in
2212 `struct lconv' from <locale.h>. The grouped number extends from
2213 the returned pointer until REAR_PTR. FRONT_PTR to W is used as a
2216 group_number (CHAR_T
*front_ptr
, CHAR_T
*w
, CHAR_T
*rear_ptr
,
2217 const char *grouping
, THOUSANDS_SEP_T thousands_sep
)
2219 /* Length of the current group. */
2221 #ifndef COMPILE_WPRINTF
2222 /* Length of the separator (in wide mode, the separator is always a
2223 single wide character). */
2224 int tlen
= strlen (thousands_sep
);
2227 /* We treat all negative values like CHAR_MAX. */
2229 if (*grouping
== CHAR_MAX
|| *grouping
<= 0)
2230 /* No grouping should be done. */
2235 /* Copy existing string so that nothing gets overwritten. */
2236 memmove (front_ptr
, w
, (rear_ptr
- w
) * sizeof (CHAR_T
));
2237 CHAR_T
*s
= front_ptr
+ (rear_ptr
- w
);
2241 /* Process all characters in the string. */
2242 while (s
> front_ptr
)
2246 if (--len
== 0 && s
> front_ptr
)
2248 /* A new group begins. */
2249 #ifdef COMPILE_WPRINTF
2251 *--w
= thousands_sep
;
2253 /* Not enough room for the separator. */
2259 *--w
= thousands_sep
[--cnt
];
2262 /* Not enough room for the separator. */
2266 if (*grouping
== CHAR_MAX
2273 /* No further grouping to be done. Copy the rest of the
2275 memmove (w
, s
, (front_ptr
-s
) * sizeof (CHAR_T
));
2278 else if (*grouping
!= '\0')
2281 /* The previous grouping repeats ad infinitum. */
2288 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2291 struct _IO_FILE_plus _f
;
2292 #ifdef COMPILE_WPRINTF
2293 struct _IO_wide_data _wide_data
;
2296 #ifdef _IO_MTSAFE_IO
2302 _IO_helper_overflow (FILE *s
, int c
)
2304 FILE *target
= ((struct helper_file
*) s
)->_put_stream
;
2305 #ifdef COMPILE_WPRINTF
2306 int used
= s
->_wide_data
->_IO_write_ptr
- s
->_wide_data
->_IO_write_base
;
2309 size_t written
= _IO_sputn (target
, s
->_wide_data
->_IO_write_base
, used
);
2310 if (written
== 0 || written
== WEOF
)
2312 __wmemmove (s
->_wide_data
->_IO_write_base
,
2313 s
->_wide_data
->_IO_write_base
+ written
,
2315 s
->_wide_data
->_IO_write_ptr
-= written
;
2318 int used
= s
->_IO_write_ptr
- s
->_IO_write_base
;
2321 size_t written
= _IO_sputn (target
, s
->_IO_write_base
, used
);
2322 if (written
== 0 || written
== EOF
)
2324 memmove (s
->_IO_write_base
, s
->_IO_write_base
+ written
,
2326 s
->_IO_write_ptr
-= written
;
2332 #ifdef COMPILE_WPRINTF
2333 static const struct _IO_jump_t _IO_helper_jumps libio_vtable
=
2336 JUMP_INIT (finish
, _IO_wdefault_finish
),
2337 JUMP_INIT (overflow
, _IO_helper_overflow
),
2338 JUMP_INIT (underflow
, _IO_default_underflow
),
2339 JUMP_INIT (uflow
, _IO_default_uflow
),
2340 JUMP_INIT (pbackfail
, (_IO_pbackfail_t
) _IO_wdefault_pbackfail
),
2341 JUMP_INIT (xsputn
, _IO_wdefault_xsputn
),
2342 JUMP_INIT (xsgetn
, _IO_wdefault_xsgetn
),
2343 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2344 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2345 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2346 JUMP_INIT (sync
, _IO_default_sync
),
2347 JUMP_INIT (doallocate
, _IO_wdefault_doallocate
),
2348 JUMP_INIT (read
, _IO_default_read
),
2349 JUMP_INIT (write
, _IO_default_write
),
2350 JUMP_INIT (seek
, _IO_default_seek
),
2351 JUMP_INIT (close
, _IO_default_close
),
2352 JUMP_INIT (stat
, _IO_default_stat
)
2355 static const struct _IO_jump_t _IO_helper_jumps libio_vtable
=
2358 JUMP_INIT (finish
, _IO_default_finish
),
2359 JUMP_INIT (overflow
, _IO_helper_overflow
),
2360 JUMP_INIT (underflow
, _IO_default_underflow
),
2361 JUMP_INIT (uflow
, _IO_default_uflow
),
2362 JUMP_INIT (pbackfail
, _IO_default_pbackfail
),
2363 JUMP_INIT (xsputn
, _IO_default_xsputn
),
2364 JUMP_INIT (xsgetn
, _IO_default_xsgetn
),
2365 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2366 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2367 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2368 JUMP_INIT (sync
, _IO_default_sync
),
2369 JUMP_INIT (doallocate
, _IO_default_doallocate
),
2370 JUMP_INIT (read
, _IO_default_read
),
2371 JUMP_INIT (write
, _IO_default_write
),
2372 JUMP_INIT (seek
, _IO_default_seek
),
2373 JUMP_INIT (close
, _IO_default_close
),
2374 JUMP_INIT (stat
, _IO_default_stat
)
2379 buffered_vfprintf (FILE *s
, const CHAR_T
*format
, va_list args
,
2380 unsigned int mode_flags
)
2383 struct helper_file helper
;
2384 FILE *hp
= (FILE *) &helper
._f
;
2385 int result
, to_flush
;
2387 /* Orient the stream. */
2392 /* Initialize helper. */
2393 helper
._put_stream
= s
;
2394 #ifdef COMPILE_WPRINTF
2395 hp
->_wide_data
= &helper
._wide_data
;
2396 _IO_wsetp (hp
, buf
, buf
+ sizeof buf
/ sizeof (CHAR_T
));
2399 _IO_setp (hp
, buf
, buf
+ sizeof buf
);
2402 hp
->_flags
= _IO_MAGIC
|_IO_NO_READS
|_IO_USER_LOCK
;
2403 #if _IO_JUMPS_OFFSET
2404 hp
->_vtable_offset
= 0;
2406 #ifdef _IO_MTSAFE_IO
2409 hp
->_flags2
= s
->_flags2
;
2410 _IO_JUMPS (&helper
._f
) = (struct _IO_jump_t
*) &_IO_helper_jumps
;
2412 /* Now print to helper instead. */
2413 result
= vfprintf (hp
, format
, args
, mode_flags
);
2416 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile
, s
);
2419 /* Now flush anything from the helper to the S. */
2420 #ifdef COMPILE_WPRINTF
2421 if ((to_flush
= (hp
->_wide_data
->_IO_write_ptr
2422 - hp
->_wide_data
->_IO_write_base
)) > 0)
2424 if ((int) _IO_sputn (s
, hp
->_wide_data
->_IO_write_base
, to_flush
)
2429 if ((to_flush
= hp
->_IO_write_ptr
- hp
->_IO_write_base
) > 0)
2431 if ((int) _IO_sputn (s
, hp
->_IO_write_base
, to_flush
) != to_flush
)
2436 /* Unlock the stream. */
2437 _IO_funlockfile (s
);
2438 __libc_cleanup_region_end (0);