1 /* Copyright (C) 1991-2019 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>
35 /* This code is shared between the standard stdio implementation found
36 in GNU C library and the libio implementation originally found in
39 Beside this it is also shared between the normal and wide character
40 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
44 #ifdef COMPILE_WPRINTF
48 /* In some cases we need extra space for all the output which is not
49 counted in the width of the string. We assume 32 characters is
52 #define ARGCHECK(S, Format) \
55 /* Check file argument for consistence. */ \
57 if (S->_flags & _IO_NO_WRITES) \
59 S->_flags |= _IO_ERR_SEEN; \
60 __set_errno (EBADF); \
65 __set_errno (EINVAL); \
69 #define UNBUFFERED_P(S) ((S)->_flags & _IO_UNBUFFERED)
71 #if __HAVE_FLOAT128_UNLIKE_LDBL
72 # define PARSE_FLOAT_VA_ARG_EXTENDED(INFO) \
76 && (mode_flags & PRINTF_LDBL_USES_FLOAT128) != 0) \
78 INFO.is_binary128 = 1; \
79 the_arg.pa_float128 = va_arg (ap, _Float128); \
83 PARSE_FLOAT_VA_ARG (INFO); \
88 # define PARSE_FLOAT_VA_ARG_EXTENDED(INFO) \
89 PARSE_FLOAT_VA_ARG (INFO);
92 #define PARSE_FLOAT_VA_ARG(INFO) \
95 INFO.is_binary128 = 0; \
97 the_arg.pa_long_double = va_arg (ap, long double); \
99 the_arg.pa_double = va_arg (ap, double); \
103 #if __HAVE_FLOAT128_UNLIKE_LDBL
104 # define SETUP_FLOAT128_INFO(INFO) \
107 if ((mode_flags & PRINTF_LDBL_USES_FLOAT128) != 0) \
108 INFO.is_binary128 = is_long_double; \
110 INFO.is_binary128 = 0; \
114 # define SETUP_FLOAT128_INFO(INFO) \
117 INFO.is_binary128 = 0; \
122 #define done_add(val) \
124 unsigned int _val = val; \
125 assert ((unsigned int) done < (unsigned int) INT_MAX); \
126 if (__glibc_unlikely (INT_MAX - done < _val)) \
129 __set_errno (EOVERFLOW); \
135 #ifndef COMPILE_WPRINTF
136 # define vfprintf __vfprintf_internal
138 # define UCHAR_T unsigned char
140 typedef const char *THOUSANDS_SEP_T
;
142 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
143 # define STR_LEN(Str) strlen (Str)
145 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
146 # define PAD(Padchar) \
150 ssize_t written = _IO_padn (s, (Padchar), width); \
151 if (__glibc_unlikely (written != width)) \
156 done_add (written); \
159 # define PUTC(C, F) _IO_putc_unlocked (C, F)
160 # define ORIENT if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
163 # define vfprintf __vfwprintf_internal
164 # define CHAR_T wchar_t
165 /* This is a hack!!! There should be a type uwchar_t. */
166 # define UCHAR_T unsigned int /* uwchar_t */
167 # define INT_T wint_t
168 typedef wchar_t THOUSANDS_SEP_T
;
169 # define L_(Str) L##Str
170 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
171 # define STR_LEN(Str) __wcslen (Str)
175 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
176 # define PAD(Padchar) \
180 ssize_t written = _IO_wpadn (s, (Padchar), width); \
181 if (__glibc_unlikely (written != width)) \
186 done_add (written); \
189 # define PUTC(C, F) _IO_putwc_unlocked (C, F)
190 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
193 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
194 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
199 #include "_i18n_number.h"
201 /* Include the shared code for parsing the format string. */
202 #include "printf-parse.h"
205 #define outchar(Ch) \
208 const INT_T outc = (Ch); \
209 if (PUTC (outc, s) == EOF || done == INT_MAX) \
218 #define outstring(String, Len) \
221 assert ((size_t) done <= (size_t) INT_MAX); \
222 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
227 if (__glibc_unlikely (INT_MAX - done < (Len))) \
230 __set_errno (EOVERFLOW); \
237 /* For handling long_double and longlong we use the same flag. If
238 `long' and `long long' are effectively the same type define it to
240 #if LONG_MAX == LONG_LONG_MAX
241 # define is_longlong 0
243 # define is_longlong is_long_double
246 /* If `long' and `int' is effectively the same type we don't have to
247 handle `long separately. */
248 #if INT_MAX == LONG_MAX
249 # define is_long_num 0
251 # define is_long_num is_long
255 /* Global constants. */
256 static const CHAR_T null
[] = L_("(null)");
258 /* Size of the work_buffer variable (in characters, not bytes. */
259 enum { WORK_BUFFER_SIZE
= 1000 / sizeof (CHAR_T
) };
261 /* This table maps a character into a number representing a class. In
262 each step there is a destination label for each class. */
263 static const uint8_t jump_table
[] =
265 /* ' ' */ 1, 0, 0, /* '#' */ 4,
266 0, /* '%' */ 14, 0, /* '\''*/ 6,
267 0, 0, /* '*' */ 7, /* '+' */ 2,
268 0, /* '-' */ 3, /* '.' */ 9, 0,
269 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
270 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
271 /* '8' */ 8, /* '9' */ 8, 0, 0,
273 0, /* 'A' */ 26, 0, /* 'C' */ 25,
274 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
275 0, /* 'I' */ 29, 0, 0,
276 /* 'L' */ 12, 0, 0, 0,
277 0, 0, 0, /* 'S' */ 21,
279 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
281 0, /* 'a' */ 26, 0, /* 'c' */ 20,
282 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
283 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
284 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
285 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
286 /* 't' */ 27, /* 'u' */ 16, 0, 0,
287 /* 'x' */ 18, 0, /* 'z' */ 13
290 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
291 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
292 #define LABEL(Name) do_##Name
294 /* 'int' is enough and it saves some space on 64 bit systems. */
295 # define JUMP_TABLE_TYPE const int
296 # define JUMP_TABLE_BASE_LABEL do_form_unknown
297 # define REF(Name) &&do_##Name - &&JUMP_TABLE_BASE_LABEL
298 # define JUMP(ChExpr, table) \
304 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
305 : table[CHAR_CLASS (spec)]; \
306 ptr = &&JUMP_TABLE_BASE_LABEL + offset; \
311 # define JUMP_TABLE_TYPE const void *const
312 # define REF(Name) &&do_##Name
313 # define JUMP(ChExpr, table) \
318 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
319 : table[CHAR_CLASS (spec)]; \
325 #define STEP0_3_TABLE \
326 /* Step 0: at the beginning. */ \
327 static JUMP_TABLE_TYPE step0_jumps[30] = \
329 REF (form_unknown), \
330 REF (flag_space), /* for ' ' */ \
331 REF (flag_plus), /* for '+' */ \
332 REF (flag_minus), /* for '-' */ \
333 REF (flag_hash), /* for '<hash>' */ \
334 REF (flag_zero), /* for '0' */ \
335 REF (flag_quote), /* for '\'' */ \
336 REF (width_asterics), /* for '*' */ \
337 REF (width), /* for '1'...'9' */ \
338 REF (precision), /* for '.' */ \
339 REF (mod_half), /* for 'h' */ \
340 REF (mod_long), /* for 'l' */ \
341 REF (mod_longlong), /* for 'L', 'q' */ \
342 REF (mod_size_t), /* for 'z', 'Z' */ \
343 REF (form_percent), /* for '%' */ \
344 REF (form_integer), /* for 'd', 'i' */ \
345 REF (form_unsigned), /* for 'u' */ \
346 REF (form_octal), /* for 'o' */ \
347 REF (form_hexa), /* for 'X', 'x' */ \
348 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
349 REF (form_character), /* for 'c' */ \
350 REF (form_string), /* for 's', 'S' */ \
351 REF (form_pointer), /* for 'p' */ \
352 REF (form_number), /* for 'n' */ \
353 REF (form_strerror), /* for 'm' */ \
354 REF (form_wcharacter), /* for 'C' */ \
355 REF (form_floathex), /* for 'A', 'a' */ \
356 REF (mod_ptrdiff_t), /* for 't' */ \
357 REF (mod_intmax_t), /* for 'j' */ \
358 REF (flag_i18n), /* for 'I' */ \
360 /* Step 1: after processing width. */ \
361 static JUMP_TABLE_TYPE step1_jumps[30] = \
363 REF (form_unknown), \
364 REF (form_unknown), /* for ' ' */ \
365 REF (form_unknown), /* for '+' */ \
366 REF (form_unknown), /* for '-' */ \
367 REF (form_unknown), /* for '<hash>' */ \
368 REF (form_unknown), /* for '0' */ \
369 REF (form_unknown), /* for '\'' */ \
370 REF (form_unknown), /* for '*' */ \
371 REF (form_unknown), /* for '1'...'9' */ \
372 REF (precision), /* for '.' */ \
373 REF (mod_half), /* for 'h' */ \
374 REF (mod_long), /* for 'l' */ \
375 REF (mod_longlong), /* for 'L', 'q' */ \
376 REF (mod_size_t), /* for 'z', 'Z' */ \
377 REF (form_percent), /* for '%' */ \
378 REF (form_integer), /* for 'd', 'i' */ \
379 REF (form_unsigned), /* for 'u' */ \
380 REF (form_octal), /* for 'o' */ \
381 REF (form_hexa), /* for 'X', 'x' */ \
382 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
383 REF (form_character), /* for 'c' */ \
384 REF (form_string), /* for 's', 'S' */ \
385 REF (form_pointer), /* for 'p' */ \
386 REF (form_number), /* for 'n' */ \
387 REF (form_strerror), /* for 'm' */ \
388 REF (form_wcharacter), /* for 'C' */ \
389 REF (form_floathex), /* for 'A', 'a' */ \
390 REF (mod_ptrdiff_t), /* for 't' */ \
391 REF (mod_intmax_t), /* for 'j' */ \
392 REF (form_unknown) /* for 'I' */ \
394 /* Step 2: after processing precision. */ \
395 static JUMP_TABLE_TYPE step2_jumps[30] = \
397 REF (form_unknown), \
398 REF (form_unknown), /* for ' ' */ \
399 REF (form_unknown), /* for '+' */ \
400 REF (form_unknown), /* for '-' */ \
401 REF (form_unknown), /* for '<hash>' */ \
402 REF (form_unknown), /* for '0' */ \
403 REF (form_unknown), /* for '\'' */ \
404 REF (form_unknown), /* for '*' */ \
405 REF (form_unknown), /* for '1'...'9' */ \
406 REF (form_unknown), /* for '.' */ \
407 REF (mod_half), /* for 'h' */ \
408 REF (mod_long), /* for 'l' */ \
409 REF (mod_longlong), /* for 'L', 'q' */ \
410 REF (mod_size_t), /* for 'z', 'Z' */ \
411 REF (form_percent), /* for '%' */ \
412 REF (form_integer), /* for 'd', 'i' */ \
413 REF (form_unsigned), /* for 'u' */ \
414 REF (form_octal), /* for 'o' */ \
415 REF (form_hexa), /* for 'X', 'x' */ \
416 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
417 REF (form_character), /* for 'c' */ \
418 REF (form_string), /* for 's', 'S' */ \
419 REF (form_pointer), /* for 'p' */ \
420 REF (form_number), /* for 'n' */ \
421 REF (form_strerror), /* for 'm' */ \
422 REF (form_wcharacter), /* for 'C' */ \
423 REF (form_floathex), /* for 'A', 'a' */ \
424 REF (mod_ptrdiff_t), /* for 't' */ \
425 REF (mod_intmax_t), /* for 'j' */ \
426 REF (form_unknown) /* for 'I' */ \
428 /* Step 3a: after processing first 'h' modifier. */ \
429 static JUMP_TABLE_TYPE step3a_jumps[30] = \
431 REF (form_unknown), \
432 REF (form_unknown), /* for ' ' */ \
433 REF (form_unknown), /* for '+' */ \
434 REF (form_unknown), /* for '-' */ \
435 REF (form_unknown), /* for '<hash>' */ \
436 REF (form_unknown), /* for '0' */ \
437 REF (form_unknown), /* for '\'' */ \
438 REF (form_unknown), /* for '*' */ \
439 REF (form_unknown), /* for '1'...'9' */ \
440 REF (form_unknown), /* for '.' */ \
441 REF (mod_halfhalf), /* for 'h' */ \
442 REF (form_unknown), /* for 'l' */ \
443 REF (form_unknown), /* for 'L', 'q' */ \
444 REF (form_unknown), /* for 'z', 'Z' */ \
445 REF (form_percent), /* for '%' */ \
446 REF (form_integer), /* for 'd', 'i' */ \
447 REF (form_unsigned), /* for 'u' */ \
448 REF (form_octal), /* for 'o' */ \
449 REF (form_hexa), /* for 'X', 'x' */ \
450 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
451 REF (form_unknown), /* for 'c' */ \
452 REF (form_unknown), /* for 's', 'S' */ \
453 REF (form_unknown), /* for 'p' */ \
454 REF (form_number), /* for 'n' */ \
455 REF (form_unknown), /* for 'm' */ \
456 REF (form_unknown), /* for 'C' */ \
457 REF (form_unknown), /* for 'A', 'a' */ \
458 REF (form_unknown), /* for 't' */ \
459 REF (form_unknown), /* for 'j' */ \
460 REF (form_unknown) /* for 'I' */ \
462 /* Step 3b: after processing first 'l' modifier. */ \
463 static JUMP_TABLE_TYPE step3b_jumps[30] = \
465 REF (form_unknown), \
466 REF (form_unknown), /* for ' ' */ \
467 REF (form_unknown), /* for '+' */ \
468 REF (form_unknown), /* for '-' */ \
469 REF (form_unknown), /* for '<hash>' */ \
470 REF (form_unknown), /* for '0' */ \
471 REF (form_unknown), /* for '\'' */ \
472 REF (form_unknown), /* for '*' */ \
473 REF (form_unknown), /* for '1'...'9' */ \
474 REF (form_unknown), /* for '.' */ \
475 REF (form_unknown), /* for 'h' */ \
476 REF (mod_longlong), /* for 'l' */ \
477 REF (form_unknown), /* for 'L', 'q' */ \
478 REF (form_unknown), /* for 'z', 'Z' */ \
479 REF (form_percent), /* for '%' */ \
480 REF (form_integer), /* for 'd', 'i' */ \
481 REF (form_unsigned), /* for 'u' */ \
482 REF (form_octal), /* for 'o' */ \
483 REF (form_hexa), /* for 'X', 'x' */ \
484 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
485 REF (form_character), /* for 'c' */ \
486 REF (form_string), /* for 's', 'S' */ \
487 REF (form_pointer), /* for 'p' */ \
488 REF (form_number), /* for 'n' */ \
489 REF (form_strerror), /* for 'm' */ \
490 REF (form_wcharacter), /* for 'C' */ \
491 REF (form_floathex), /* for 'A', 'a' */ \
492 REF (form_unknown), /* for 't' */ \
493 REF (form_unknown), /* for 'j' */ \
494 REF (form_unknown) /* for 'I' */ \
497 #define STEP4_TABLE \
498 /* Step 4: processing format specifier. */ \
499 static JUMP_TABLE_TYPE step4_jumps[30] = \
501 REF (form_unknown), \
502 REF (form_unknown), /* for ' ' */ \
503 REF (form_unknown), /* for '+' */ \
504 REF (form_unknown), /* for '-' */ \
505 REF (form_unknown), /* for '<hash>' */ \
506 REF (form_unknown), /* for '0' */ \
507 REF (form_unknown), /* for '\'' */ \
508 REF (form_unknown), /* for '*' */ \
509 REF (form_unknown), /* for '1'...'9' */ \
510 REF (form_unknown), /* for '.' */ \
511 REF (form_unknown), /* for 'h' */ \
512 REF (form_unknown), /* for 'l' */ \
513 REF (form_unknown), /* for 'L', 'q' */ \
514 REF (form_unknown), /* for 'z', 'Z' */ \
515 REF (form_percent), /* for '%' */ \
516 REF (form_integer), /* for 'd', 'i' */ \
517 REF (form_unsigned), /* for 'u' */ \
518 REF (form_octal), /* for 'o' */ \
519 REF (form_hexa), /* for 'X', 'x' */ \
520 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
521 REF (form_character), /* for 'c' */ \
522 REF (form_string), /* for 's', 'S' */ \
523 REF (form_pointer), /* for 'p' */ \
524 REF (form_number), /* for 'n' */ \
525 REF (form_strerror), /* for 'm' */ \
526 REF (form_wcharacter), /* for 'C' */ \
527 REF (form_floathex), /* for 'A', 'a' */ \
528 REF (form_unknown), /* for 't' */ \
529 REF (form_unknown), /* for 'j' */ \
530 REF (form_unknown) /* for 'I' */ \
534 #define process_arg(fspec) \
535 /* Start real work. We know about all flags and modifiers and \
536 now process the wanted format specifier. */ \
537 LABEL (form_percent): \
538 /* Write a literal "%". */ \
542 LABEL (form_integer): \
543 /* Signed decimal integer. */ \
548 long long int signed_number; \
551 signed_number = va_arg (ap, long long int); \
553 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
555 is_negative = signed_number < 0; \
556 number.longlong = is_negative ? (- signed_number) : signed_number; \
558 goto LABEL (longlong_number); \
562 long int signed_number; \
567 signed_number = va_arg (ap, long int); \
569 signed_number = (signed char) va_arg (ap, unsigned int); \
570 else if (!is_short) \
571 signed_number = va_arg (ap, int); \
573 signed_number = (short int) va_arg (ap, unsigned int); \
577 signed_number = args_value[fspec->data_arg].pa_long_int; \
579 signed_number = (signed char) \
580 args_value[fspec->data_arg].pa_u_int; \
581 else if (!is_short) \
582 signed_number = args_value[fspec->data_arg].pa_int; \
584 signed_number = (short int) \
585 args_value[fspec->data_arg].pa_u_int; \
587 is_negative = signed_number < 0; \
588 number.word = is_negative ? (- signed_number) : signed_number; \
590 goto LABEL (number); \
594 LABEL (form_unsigned): \
595 /* Unsigned decimal integer. */ \
597 goto LABEL (unsigned_number); \
600 LABEL (form_octal): \
601 /* Unsigned octal integer. */ \
603 goto LABEL (unsigned_number); \
607 /* Unsigned hexadecimal integer. */ \
610 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
612 /* ISO specifies the `+' and ` ' flags only for signed \
621 number.longlong = va_arg (ap, unsigned long long int); \
623 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
625 LABEL (longlong_number): \
627 /* Supply a default precision if none was given. */ \
630 /* We have to take care for the '0' flag. If a precision \
631 is given it must be ignored. */ \
634 /* If the precision is 0 and the number is 0 nothing has to \
635 be written for the number, except for the 'o' format in \
637 if (prec == 0 && number.longlong == 0) \
640 if (base == 8 && alt) \
641 *--string = L_('0'); \
645 /* Put the number in WORK. */ \
646 string = _itoa (number.longlong, workend, base, \
648 if (group && grouping) \
649 string = group_number (work_buffer, string, workend, \
650 grouping, thousands_sep); \
651 if (use_outdigits && base == 10) \
652 string = _i18n_number_rewrite (string, workend, workend); \
654 /* Simplify further test for num != 0. */ \
655 number.word = number.longlong != 0; \
662 number.word = va_arg (ap, unsigned long int); \
664 number.word = (unsigned char) va_arg (ap, unsigned int); \
665 else if (!is_short) \
666 number.word = va_arg (ap, unsigned int); \
668 number.word = (unsigned short int) va_arg (ap, unsigned int); \
672 number.word = args_value[fspec->data_arg].pa_u_long_int; \
674 number.word = (unsigned char) \
675 args_value[fspec->data_arg].pa_u_int; \
676 else if (!is_short) \
677 number.word = args_value[fspec->data_arg].pa_u_int; \
679 number.word = (unsigned short int) \
680 args_value[fspec->data_arg].pa_u_int; \
684 /* Supply a default precision if none was given. */ \
687 /* We have to take care for the '0' flag. If a precision \
688 is given it must be ignored. */ \
691 /* If the precision is 0 and the number is 0 nothing has to \
692 be written for the number, except for the 'o' format in \
694 if (prec == 0 && number.word == 0) \
697 if (base == 8 && alt) \
698 *--string = L_('0'); \
702 /* Put the number in WORK. */ \
703 string = _itoa_word (number.word, workend, base, \
705 if (group && grouping) \
706 string = group_number (work_buffer, string, workend, \
707 grouping, thousands_sep); \
708 if (use_outdigits && base == 10) \
709 string = _i18n_number_rewrite (string, workend, workend); \
713 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
714 /* Add octal marker. */ \
715 *--string = L_('0'); \
717 prec = MAX (0, prec - (workend - string)); \
721 width -= workend - string + prec; \
723 if (number.word != 0 && alt && base == 16) \
724 /* Account for 0X hex marker. */ \
727 if (is_negative || showsign || space) \
730 if (pad == L_(' ')) \
743 if (number.word != 0 && alt && base == 16) \
752 outstring (string, workend - string); \
774 if (number.word != 0 && alt && base == 16) \
781 width -= workend - string + prec; \
791 outstring (string, workend - string); \
797 LABEL (form_float): \
799 /* Floating-point number. This is handled by printf_fp.c. */ \
805 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0)) \
806 is_long_double = 0; \
808 struct printf_info info = { .prec = prec, \
811 .is_long_double = is_long_double, \
812 .is_short = is_short, \
813 .is_long = is_long, \
817 .showsign = showsign, \
821 .i18n = use_outdigits, \
822 .wide = sizeof (CHAR_T) != 1, \
823 .is_binary128 = 0}; \
825 PARSE_FLOAT_VA_ARG_EXTENDED (info); \
826 ptr = (const void *) &the_arg; \
828 function_done = __printf_fp (s, &info, &ptr); \
832 ptr = (const void *) &args_value[fspec->data_arg]; \
833 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0)) \
835 fspec->data_arg_type = PA_DOUBLE; \
836 fspec->info.is_long_double = 0; \
838 SETUP_FLOAT128_INFO (fspec->info); \
840 function_done = __printf_fp (s, &fspec->info, &ptr); \
843 if (function_done < 0) \
845 /* Error in print handler; up to handler to set errno. */ \
850 done_add (function_done); \
854 LABEL (form_floathex): \
856 /* Floating point number printed as hexadecimal number. */ \
862 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0)) \
863 is_long_double = 0; \
865 struct printf_info info = { .prec = prec, \
868 .is_long_double = is_long_double, \
869 .is_short = is_short, \
870 .is_long = is_long, \
874 .showsign = showsign, \
878 .wide = sizeof (CHAR_T) != 1, \
879 .is_binary128 = 0}; \
881 PARSE_FLOAT_VA_ARG_EXTENDED (info); \
882 ptr = (const void *) &the_arg; \
884 function_done = __printf_fphex (s, &info, &ptr); \
888 ptr = (const void *) &args_value[fspec->data_arg]; \
889 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0)) \
890 fspec->info.is_long_double = 0; \
891 SETUP_FLOAT128_INFO (fspec->info); \
893 function_done = __printf_fphex (s, &fspec->info, &ptr); \
896 if (function_done < 0) \
898 /* Error in print handler; up to handler to set errno. */ \
903 done_add (function_done); \
907 LABEL (form_pointer): \
908 /* Generic pointer. */ \
912 ptr = va_arg (ap, void *); \
914 ptr = args_value[fspec->data_arg].pa_pointer; \
917 /* If the pointer is not NULL, write it as a %#x spec. */ \
919 number.word = (unsigned long int) ptr; \
924 goto LABEL (number); \
928 /* Write "(nil)" for a nil pointer. */ \
929 string = (CHAR_T *) L_("(nil)"); \
930 /* Make sure the full string "(nil)" is printed. */ \
933 /* This is a wide string iff compiling wprintf. */ \
934 is_long = sizeof (CHAR_T) > 1; \
935 goto LABEL (print_string); \
940 LABEL (form_number): \
941 if ((mode_flags & PRINTF_FORTIFY) != 0) \
943 if (! readonly_format) \
945 extern int __readonly_area (const void *, size_t) \
948 = __readonly_area (format, ((STR_LEN (format) + 1) \
949 * sizeof (CHAR_T))); \
951 if (readonly_format < 0) \
952 __libc_fatal ("*** %n in writable segment detected ***\n"); \
954 /* Answer the count of characters written. */ \
958 *(long long int *) va_arg (ap, void *) = done; \
959 else if (is_long_num) \
960 *(long int *) va_arg (ap, void *) = done; \
962 *(char *) va_arg (ap, void *) = done; \
963 else if (!is_short) \
964 *(int *) va_arg (ap, void *) = done; \
966 *(short int *) va_arg (ap, void *) = done; \
970 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
971 else if (is_long_num) \
972 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
974 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
975 else if (!is_short) \
976 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
978 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
981 LABEL (form_strerror): \
982 /* Print description of error ERRNO. */ \
984 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
985 WORK_BUFFER_SIZE * sizeof (CHAR_T)); \
986 is_long = 0; /* This is no wide-char string. */ \
987 goto LABEL (print_string)
989 #ifdef COMPILE_WPRINTF
990 # define process_string_arg(fspec) \
991 LABEL (form_character): \
994 goto LABEL (form_wcharacter); \
995 --width; /* Account for the character itself. */ \
999 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
1001 outchar (__btowc ((unsigned char) \
1002 args_value[fspec->data_arg].pa_int)); \
1007 LABEL (form_wcharacter): \
1009 /* Wide character. */ \
1013 if (fspec == NULL) \
1014 outchar (va_arg (ap, wchar_t)); \
1016 outchar (args_value[fspec->data_arg].pa_wchar); \
1022 LABEL (form_string): \
1025 int string_malloced; \
1027 /* The string argument could in fact be `char *' or `wchar_t *'. \
1028 But this should not make a difference here. */ \
1029 if (fspec == NULL) \
1030 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
1032 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
1034 /* Entry point for printing other strings. */ \
1035 LABEL (print_string): \
1037 string_malloced = 0; \
1038 if (string == NULL) \
1040 /* Write "(null)" if there's space. */ \
1041 if (prec == -1 || prec >= (int) array_length (null) - 1) \
1043 string = (CHAR_T *) null; \
1044 len = array_length (null) - 1; \
1048 string = (CHAR_T *) L""; \
1052 else if (!is_long && spec != L_('S')) \
1054 /* This is complicated. We have to transform the multibyte \
1055 string into a wide character string. */ \
1056 const char *mbs = (const char *) string; \
1057 mbstate_t mbstate; \
1059 len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
1061 /* Allocate dynamically an array which definitely is long \
1062 enough for the wide character version. Each byte in the \
1063 multi-byte string can produce at most one wide character. */ \
1064 if (__glibc_unlikely (len > SIZE_MAX / sizeof (wchar_t))) \
1066 __set_errno (EOVERFLOW); \
1070 else if (__libc_use_alloca (len * sizeof (wchar_t))) \
1071 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1072 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1079 string_malloced = 1; \
1081 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1082 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1083 if (len == (size_t) -1) \
1085 /* Illegal multibyte character. */ \
1093 /* Search for the end of the string, but don't search past \
1094 the length specified by the precision. */ \
1095 len = __wcsnlen (string, prec); \
1097 len = __wcslen (string); \
1100 if ((width -= len) < 0) \
1102 outstring (string, len); \
1108 outstring (string, len); \
1111 if (__glibc_unlikely (string_malloced)) \
1116 # define process_string_arg(fspec) \
1117 LABEL (form_character): \
1120 goto LABEL (form_wcharacter); \
1121 --width; /* Account for the character itself. */ \
1124 if (fspec == NULL) \
1125 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1127 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1132 LABEL (form_wcharacter): \
1134 /* Wide character. */ \
1135 char buf[MB_LEN_MAX]; \
1136 mbstate_t mbstate; \
1139 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1140 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1141 : args_value[fspec->data_arg].pa_wchar), \
1143 if (len == (size_t) -1) \
1145 /* Something went wrong during the conversion. Bail out. */ \
1152 outstring (buf, len); \
1158 LABEL (form_string): \
1161 int string_malloced; \
1163 /* The string argument could in fact be `char *' or `wchar_t *'. \
1164 But this should not make a difference here. */ \
1165 if (fspec == NULL) \
1166 string = (char *) va_arg (ap, const char *); \
1168 string = (char *) args_value[fspec->data_arg].pa_string; \
1170 /* Entry point for printing other strings. */ \
1171 LABEL (print_string): \
1173 string_malloced = 0; \
1174 if (string == NULL) \
1176 /* Write "(null)" if there's space. */ \
1177 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1179 string = (char *) null; \
1180 len = sizeof (null) - 1; \
1184 string = (char *) ""; \
1188 else if (!is_long && spec != L_('S')) \
1191 /* Search for the end of the string, but don't search past \
1192 the length (in bytes) specified by the precision. */ \
1193 len = __strnlen (string, prec); \
1195 len = strlen (string); \
1199 const wchar_t *s2 = (const wchar_t *) string; \
1200 mbstate_t mbstate; \
1202 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1206 /* The string `s2' might not be NUL terminated. */ \
1207 if (__libc_use_alloca (prec)) \
1208 string = (char *) alloca (prec); \
1209 else if ((string = (char *) malloc (prec)) == NULL) \
1215 string_malloced = 1; \
1216 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1220 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1221 if (len != (size_t) -1) \
1223 assert (__mbsinit (&mbstate)); \
1224 s2 = (const wchar_t *) string; \
1225 if (__libc_use_alloca (len + 1)) \
1226 string = (char *) alloca (len + 1); \
1227 else if ((string = (char *) malloc (len + 1)) == NULL) \
1233 string_malloced = 1; \
1234 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1238 if (len == (size_t) -1) \
1240 /* Illegal wide-character string. */ \
1246 if ((width -= len) < 0) \
1248 outstring (string, len); \
1254 outstring (string, len); \
1257 if (__glibc_unlikely (string_malloced)) \
1263 /* Helper function to provide temporary buffering for unbuffered streams. */
1264 static int buffered_vfprintf (FILE *stream
, const CHAR_T
*fmt
, va_list,
1266 __THROW
__attribute__ ((noinline
));
1268 /* Handle positional format specifiers. */
1269 static int printf_positional (FILE *s
,
1270 const CHAR_T
*format
, int readonly_format
,
1271 va_list ap
, va_list *ap_savep
, int done
,
1272 int nspecs_done
, const UCHAR_T
*lead_str_end
,
1273 CHAR_T
*work_buffer
, int save_errno
,
1274 const char *grouping
,
1275 THOUSANDS_SEP_T thousands_sep
,
1276 unsigned int mode_flags
);
1278 /* Handle unknown format specifier. */
1279 static int printf_unknown (FILE *, const struct printf_info
*,
1280 const void *const *) __THROW
;
1282 /* Group digits of number string. */
1283 static CHAR_T
*group_number (CHAR_T
*, CHAR_T
*, CHAR_T
*, const char *,
1286 /* The function itself. */
1288 vfprintf (FILE *s
, const CHAR_T
*format
, va_list ap
, unsigned int mode_flags
)
1290 /* The character used as thousands separator. */
1291 THOUSANDS_SEP_T thousands_sep
= 0;
1293 /* The string describing the size of groups of digits. */
1294 const char *grouping
;
1296 /* Place to accumulate the result. */
1299 /* Current character in format string. */
1302 /* End of leading constant string. */
1303 const UCHAR_T
*lead_str_end
;
1305 /* Points to next format specifier. */
1306 const UCHAR_T
*end_of_spec
;
1308 /* Buffer intermediate results. */
1309 CHAR_T work_buffer
[WORK_BUFFER_SIZE
];
1310 CHAR_T
*workstart
= NULL
;
1313 /* We have to save the original argument pointer. */
1316 /* Count number of specifiers we already processed. */
1319 /* For the %m format we may need the current `errno' value. */
1320 int save_errno
= errno
;
1322 /* 1 if format is in read-only memory, -1 if it is in writable memory,
1324 int readonly_format
= 0;
1326 /* Orient the stream. */
1331 /* Sanity check of arguments. */
1332 ARGCHECK (s
, format
);
1335 /* Check for correct orientation. */
1336 if (_IO_vtable_offset (s
) == 0
1337 && _IO_fwide (s
, sizeof (CHAR_T
) == 1 ? -1 : 1)
1338 != (sizeof (CHAR_T
) == 1 ? -1 : 1))
1339 /* The stream is already oriented otherwise. */
1343 if (UNBUFFERED_P (s
))
1344 /* Use a helper function which will allocate a local temporary buffer
1345 for the stream and then call us again. */
1346 return buffered_vfprintf (s
, format
, ap
, mode_flags
);
1348 /* Initialize local variables. */
1350 grouping
= (const char *) -1;
1352 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1353 since on some systems `va_list' is not an integral type. */
1354 __va_copy (ap_save
, ap
);
1360 #ifdef COMPILE_WPRINTF
1361 /* Find the first format specifier. */
1362 f
= lead_str_end
= __find_specwc ((const UCHAR_T
*) format
);
1364 /* Find the first format specifier. */
1365 f
= lead_str_end
= __find_specmb ((const UCHAR_T
*) format
);
1369 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile
, s
);
1372 /* Write the literal text before the first format. */
1373 outstring ((const UCHAR_T
*) format
,
1374 lead_str_end
- (const UCHAR_T
*) format
);
1376 /* If we only have to print a simple string, return now. */
1380 /* Use the slow path in case any printf handler is registered. */
1381 if (__glibc_unlikely (__printf_function_table
!= NULL
1382 || __printf_modifier_table
!= NULL
1383 || __printf_va_arg_table
!= NULL
))
1386 /* Process whole format string. */
1392 union printf_arg
*args_value
; /* This is not used here but ... */
1393 int is_negative
; /* Flag for negative number. */
1396 unsigned long long int longlong
;
1397 unsigned long int word
;
1400 union printf_arg the_arg
;
1401 CHAR_T
*string
; /* Pointer to argument string. */
1402 int alt
= 0; /* Alternate format. */
1403 int space
= 0; /* Use space prefix if no sign is needed. */
1404 int left
= 0; /* Left-justify output. */
1405 int showsign
= 0; /* Always begin with plus or minus sign. */
1406 int group
= 0; /* Print numbers according grouping rules. */
1407 int is_long_double
= 0; /* Argument is long double/ long long int. */
1408 int is_short
= 0; /* Argument is short int. */
1409 int is_long
= 0; /* Argument is long int. */
1410 int is_char
= 0; /* Argument is promoted (unsigned) char. */
1411 int width
= 0; /* Width of output; 0 means none specified. */
1412 int prec
= -1; /* Precision of output; -1 means none specified. */
1413 /* This flag is set by the 'I' modifier and selects the use of the
1414 `outdigits' as determined by the current locale. */
1415 int use_outdigits
= 0;
1416 UCHAR_T pad
= L_(' ');/* Padding character. */
1420 workend
= work_buffer
+ WORK_BUFFER_SIZE
;
1422 /* Get current character in format string. */
1423 JUMP (*++f
, step0_jumps
);
1428 JUMP (*++f
, step0_jumps
);
1433 JUMP (*++f
, step0_jumps
);
1439 JUMP (*++f
, step0_jumps
);
1444 JUMP (*++f
, step0_jumps
);
1450 JUMP (*++f
, step0_jumps
);
1452 /* The '\'' flag. */
1456 if (grouping
== (const char *) -1)
1458 #ifdef COMPILE_WPRINTF
1459 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1460 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1462 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1465 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1466 if (*grouping
== '\0' || *grouping
== CHAR_MAX
1467 #ifdef COMPILE_WPRINTF
1468 || thousands_sep
== L
'\0'
1470 || *thousands_sep
== '\0'
1475 JUMP (*++f
, step0_jumps
);
1479 JUMP (*++f
, step0_jumps
);
1481 /* Get width from argument. */
1482 LABEL (width_asterics
):
1484 const UCHAR_T
*tmp
; /* Temporary value. */
1489 int pos
= read_int (&tmp
);
1493 __set_errno (EOVERFLOW
);
1498 if (pos
&& *tmp
== L_('$'))
1499 /* The width comes from a positional parameter. */
1502 width
= va_arg (ap
, int);
1504 /* Negative width means left justified. */
1512 if (__glibc_unlikely (width
>= INT_MAX
/ sizeof (CHAR_T
) - EXTSIZ
))
1514 __set_errno (EOVERFLOW
);
1519 if (width
>= WORK_BUFFER_SIZE
- EXTSIZ
)
1521 /* We have to use a special buffer. */
1522 size_t needed
= ((size_t) width
+ EXTSIZ
) * sizeof (CHAR_T
);
1523 if (__libc_use_alloca (needed
))
1524 workend
= (CHAR_T
*) alloca (needed
) + width
+ EXTSIZ
;
1527 workstart
= (CHAR_T
*) malloc (needed
);
1528 if (workstart
== NULL
)
1533 workend
= workstart
+ width
+ EXTSIZ
;
1537 JUMP (*f
, step1_jumps
);
1539 /* Given width in format string. */
1541 width
= read_int (&f
);
1543 if (__glibc_unlikely (width
== -1
1544 || width
>= INT_MAX
/ sizeof (CHAR_T
) - EXTSIZ
))
1546 __set_errno (EOVERFLOW
);
1551 if (width
>= WORK_BUFFER_SIZE
- EXTSIZ
)
1553 /* We have to use a special buffer. */
1554 size_t needed
= ((size_t) width
+ EXTSIZ
) * sizeof (CHAR_T
);
1555 if (__libc_use_alloca (needed
))
1556 workend
= (CHAR_T
*) alloca (needed
) + width
+ EXTSIZ
;
1559 workstart
= (CHAR_T
*) malloc (needed
);
1560 if (workstart
== NULL
)
1565 workend
= workstart
+ width
+ EXTSIZ
;
1569 /* Oh, oh. The argument comes from a positional parameter. */
1571 JUMP (*f
, step1_jumps
);
1577 const UCHAR_T
*tmp
; /* Temporary value. */
1582 int pos
= read_int (&tmp
);
1586 __set_errno (EOVERFLOW
);
1591 if (pos
&& *tmp
== L_('$'))
1592 /* The precision comes from a positional parameter. */
1595 prec
= va_arg (ap
, int);
1597 /* If the precision is negative the precision is omitted. */
1601 else if (ISDIGIT (*f
))
1603 prec
= read_int (&f
);
1605 /* The precision was specified in this case as an extremely
1606 large positive value. */
1609 __set_errno (EOVERFLOW
);
1616 if (prec
> width
&& prec
> WORK_BUFFER_SIZE
- EXTSIZ
)
1618 /* Deallocate any previously allocated buffer because it is
1620 if (__glibc_unlikely (workstart
!= NULL
))
1623 if (__glibc_unlikely (prec
>= INT_MAX
/ sizeof (CHAR_T
) - EXTSIZ
))
1625 __set_errno (EOVERFLOW
);
1629 size_t needed
= ((size_t) prec
+ EXTSIZ
) * sizeof (CHAR_T
);
1631 if (__libc_use_alloca (needed
))
1632 workend
= (CHAR_T
*) alloca (needed
) + prec
+ EXTSIZ
;
1635 workstart
= (CHAR_T
*) malloc (needed
);
1636 if (workstart
== NULL
)
1641 workend
= workstart
+ prec
+ EXTSIZ
;
1644 JUMP (*f
, step2_jumps
);
1646 /* Process 'h' modifier. There might another 'h' following. */
1649 JUMP (*++f
, step3a_jumps
);
1651 /* Process 'hh' modifier. */
1652 LABEL (mod_halfhalf
):
1655 JUMP (*++f
, step4_jumps
);
1657 /* Process 'l' modifier. There might another 'l' following. */
1660 JUMP (*++f
, step3b_jumps
);
1662 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1663 allowed to follow. */
1664 LABEL (mod_longlong
):
1667 JUMP (*++f
, step4_jumps
);
1670 is_long_double
= sizeof (size_t) > sizeof (unsigned long int);
1671 is_long
= sizeof (size_t) > sizeof (unsigned int);
1672 JUMP (*++f
, step4_jumps
);
1674 LABEL (mod_ptrdiff_t
):
1675 is_long_double
= sizeof (ptrdiff_t) > sizeof (unsigned long int);
1676 is_long
= sizeof (ptrdiff_t) > sizeof (unsigned int);
1677 JUMP (*++f
, step4_jumps
);
1679 LABEL (mod_intmax_t
):
1680 is_long_double
= sizeof (intmax_t) > sizeof (unsigned long int);
1681 is_long
= sizeof (intmax_t) > sizeof (unsigned int);
1682 JUMP (*++f
, step4_jumps
);
1684 /* Process current format. */
1687 process_arg (((struct printf_spec
*) NULL
));
1688 process_string_arg (((struct printf_spec
*) NULL
));
1690 LABEL (form_unknown
):
1691 if (spec
== L_('\0'))
1693 /* The format string ended before the specifier is complete. */
1694 __set_errno (EINVAL
);
1699 /* If we are in the fast loop force entering the complicated
1704 /* The format is correctly handled. */
1707 if (__glibc_unlikely (workstart
!= NULL
))
1711 /* Look for next format specifier. */
1712 #ifdef COMPILE_WPRINTF
1713 f
= __find_specwc ((end_of_spec
= ++f
));
1715 f
= __find_specmb ((end_of_spec
= ++f
));
1718 /* Write the following constant string. */
1719 outstring (end_of_spec
, f
- end_of_spec
);
1721 while (*f
!= L_('\0'));
1723 /* Unlock stream and return. */
1726 /* Hand off processing for positional parameters. */
1728 if (__glibc_unlikely (workstart
!= NULL
))
1733 done
= printf_positional (s
, format
, readonly_format
, ap
, &ap_save
,
1734 done
, nspecs_done
, lead_str_end
, work_buffer
,
1735 save_errno
, grouping
, thousands_sep
, mode_flags
);
1738 if (__glibc_unlikely (workstart
!= NULL
))
1740 /* Unlock the stream. */
1741 _IO_funlockfile (s
);
1742 _IO_cleanup_region_end (0);
1748 printf_positional (FILE *s
, const CHAR_T
*format
, int readonly_format
,
1749 va_list ap
, va_list *ap_savep
, int done
, int nspecs_done
,
1750 const UCHAR_T
*lead_str_end
,
1751 CHAR_T
*work_buffer
, int save_errno
,
1752 const char *grouping
, THOUSANDS_SEP_T thousands_sep
,
1753 unsigned int mode_flags
)
1755 /* For positional argument handling. */
1756 struct scratch_buffer specsbuf
;
1757 scratch_buffer_init (&specsbuf
);
1758 struct printf_spec
*specs
= specsbuf
.data
;
1759 size_t specs_limit
= specsbuf
.length
/ sizeof (specs
[0]);
1761 /* Used as a backing store for args_value, args_size, args_type
1763 struct scratch_buffer argsbuf
;
1764 scratch_buffer_init (&argsbuf
);
1766 /* Array with information about the needed arguments. This has to
1767 be dynamically extensible. */
1770 /* The number of arguments the format string requests. This will
1771 determine the size of the array needed to store the argument
1775 /* Positional parameters refer to arguments directly. This could
1776 also determine the maximum number of arguments. Track the
1778 size_t max_ref_arg
= 0;
1780 /* Just a counter. */
1783 CHAR_T
*workstart
= NULL
;
1785 if (grouping
== (const char *) -1)
1787 #ifdef COMPILE_WPRINTF
1788 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1789 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1791 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1794 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1795 if (*grouping
== '\0' || *grouping
== CHAR_MAX
)
1799 for (const UCHAR_T
*f
= lead_str_end
; *f
!= L_('\0');
1800 f
= specs
[nspecs
++].next_fmt
)
1802 if (nspecs
== specs_limit
)
1804 if (!scratch_buffer_grow_preserve (&specsbuf
))
1809 specs
= specsbuf
.data
;
1810 specs_limit
= specsbuf
.length
/ sizeof (specs
[0]);
1813 /* Parse the format specifier. */
1814 #ifdef COMPILE_WPRINTF
1815 nargs
+= __parse_one_specwc (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1817 nargs
+= __parse_one_specmb (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1821 /* Determine the number of arguments the format string consumes. */
1822 nargs
= MAX (nargs
, max_ref_arg
);
1824 union printf_arg
*args_value
;
1828 /* Calculate total size needed to represent a single argument
1829 across all three argument-related arrays. */
1830 size_t bytes_per_arg
1831 = sizeof (*args_value
) + sizeof (*args_size
) + sizeof (*args_type
);
1832 if (!scratch_buffer_set_array_size (&argsbuf
, nargs
, bytes_per_arg
))
1837 args_value
= argsbuf
.data
;
1838 /* Set up the remaining two arrays to each point past the end of
1839 the prior array, since space for all three has been allocated
1841 args_size
= &args_value
[nargs
].pa_int
;
1842 args_type
= &args_size
[nargs
];
1843 memset (args_type
, (mode_flags
& PRINTF_FORTIFY
) != 0 ? '\xff' : '\0',
1844 nargs
* sizeof (*args_type
));
1847 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1848 still zero after this loop, format is invalid. For now we
1849 simply use 0 as the value. */
1851 /* Fill in the types of all the arguments. */
1852 for (cnt
= 0; cnt
< nspecs
; ++cnt
)
1854 /* If the width is determined by an argument this is an int. */
1855 if (specs
[cnt
].width_arg
!= -1)
1856 args_type
[specs
[cnt
].width_arg
] = PA_INT
;
1858 /* If the precision is determined by an argument this is an int. */
1859 if (specs
[cnt
].prec_arg
!= -1)
1860 args_type
[specs
[cnt
].prec_arg
] = PA_INT
;
1862 switch (specs
[cnt
].ndata_args
)
1864 case 0: /* No arguments. */
1866 case 1: /* One argument; we already have the
1868 args_type
[specs
[cnt
].data_arg
] = specs
[cnt
].data_arg_type
;
1869 args_size
[specs
[cnt
].data_arg
] = specs
[cnt
].size
;
1872 /* We have more than one argument for this format spec.
1873 We must call the arginfo function again to determine
1875 (void) (*__printf_arginfo_table
[specs
[cnt
].info
.spec
])
1877 specs
[cnt
].ndata_args
, &args_type
[specs
[cnt
].data_arg
],
1878 &args_size
[specs
[cnt
].data_arg
]);
1883 /* Now we know all the types and the order. Fill in the argument
1885 for (cnt
= 0; cnt
< nargs
; ++cnt
)
1886 switch (args_type
[cnt
])
1888 #define T(tag, mem, type) \
1890 args_value[cnt].mem = va_arg (*ap_savep, type); \
1893 T (PA_WCHAR
, pa_wchar
, wint_t);
1894 case PA_CHAR
: /* Promoted. */
1895 case PA_INT
|PA_FLAG_SHORT
: /* Promoted. */
1896 #if LONG_MAX == INT_MAX
1897 case PA_INT
|PA_FLAG_LONG
:
1899 T (PA_INT
, pa_int
, int);
1900 #if LONG_MAX == LONG_LONG_MAX
1901 case PA_INT
|PA_FLAG_LONG
:
1903 T (PA_INT
|PA_FLAG_LONG_LONG
, pa_long_long_int
, long long int);
1904 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1907 case PA_FLOAT
: /* Promoted. */
1908 T (PA_DOUBLE
, pa_double
, double);
1909 case PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
:
1910 if (__glibc_unlikely ((mode_flags
& PRINTF_LDBL_IS_DBL
) != 0))
1912 args_value
[cnt
].pa_double
= va_arg (*ap_savep
, double);
1913 args_type
[cnt
] &= ~PA_FLAG_LONG_DOUBLE
;
1915 #if __HAVE_FLOAT128_UNLIKE_LDBL
1916 else if ((mode_flags
& PRINTF_LDBL_USES_FLOAT128
) != 0)
1917 args_value
[cnt
].pa_float128
= va_arg (*ap_savep
, _Float128
);
1920 args_value
[cnt
].pa_long_double
= va_arg (*ap_savep
, long double);
1922 case PA_STRING
: /* All pointers are the same */
1923 case PA_WSTRING
: /* All pointers are the same */
1924 T (PA_POINTER
, pa_pointer
, void *);
1927 if ((args_type
[cnt
] & PA_FLAG_PTR
) != 0)
1928 args_value
[cnt
].pa_pointer
= va_arg (*ap_savep
, void *);
1929 else if (__glibc_unlikely (__printf_va_arg_table
!= NULL
)
1930 && __printf_va_arg_table
[args_type
[cnt
] - PA_LAST
] != NULL
)
1932 args_value
[cnt
].pa_user
= alloca (args_size
[cnt
]);
1933 (*__printf_va_arg_table
[args_type
[cnt
] - PA_LAST
])
1934 (args_value
[cnt
].pa_user
, ap_savep
);
1937 memset (&args_value
[cnt
], 0, sizeof (args_value
[cnt
]));
1940 /* Error case. Not all parameters appear in N$ format
1941 strings. We have no way to determine their type. */
1942 assert ((mode_flags
& PRINTF_FORTIFY
) != 0);
1943 __libc_fatal ("*** invalid %N$ use detected ***\n");
1946 /* Now walk through all format specifiers and process them. */
1947 for (; (size_t) nspecs_done
< nspecs
; ++nspecs_done
)
1954 unsigned long long int longlong
;
1955 unsigned long int word
;
1958 union printf_arg the_arg
;
1959 CHAR_T
*string
; /* Pointer to argument string. */
1961 /* Fill variables from values in struct. */
1962 int alt
= specs
[nspecs_done
].info
.alt
;
1963 int space
= specs
[nspecs_done
].info
.space
;
1964 int left
= specs
[nspecs_done
].info
.left
;
1965 int showsign
= specs
[nspecs_done
].info
.showsign
;
1966 int group
= specs
[nspecs_done
].info
.group
;
1967 int is_long_double
= specs
[nspecs_done
].info
.is_long_double
;
1968 int is_short
= specs
[nspecs_done
].info
.is_short
;
1969 int is_char
= specs
[nspecs_done
].info
.is_char
;
1970 int is_long
= specs
[nspecs_done
].info
.is_long
;
1971 int width
= specs
[nspecs_done
].info
.width
;
1972 int prec
= specs
[nspecs_done
].info
.prec
;
1973 int use_outdigits
= specs
[nspecs_done
].info
.i18n
;
1974 char pad
= specs
[nspecs_done
].info
.pad
;
1975 CHAR_T spec
= specs
[nspecs_done
].info
.spec
;
1978 CHAR_T
*workend
= work_buffer
+ WORK_BUFFER_SIZE
;
1980 /* Fill in last information. */
1981 if (specs
[nspecs_done
].width_arg
!= -1)
1983 /* Extract the field width from an argument. */
1984 specs
[nspecs_done
].info
.width
=
1985 args_value
[specs
[nspecs_done
].width_arg
].pa_int
;
1987 if (specs
[nspecs_done
].info
.width
< 0)
1988 /* If the width value is negative left justification is
1989 selected and the value is taken as being positive. */
1991 specs
[nspecs_done
].info
.width
*= -1;
1992 left
= specs
[nspecs_done
].info
.left
= 1;
1994 width
= specs
[nspecs_done
].info
.width
;
1997 if (specs
[nspecs_done
].prec_arg
!= -1)
1999 /* Extract the precision from an argument. */
2000 specs
[nspecs_done
].info
.prec
=
2001 args_value
[specs
[nspecs_done
].prec_arg
].pa_int
;
2003 if (specs
[nspecs_done
].info
.prec
< 0)
2004 /* If the precision is negative the precision is
2006 specs
[nspecs_done
].info
.prec
= -1;
2008 prec
= specs
[nspecs_done
].info
.prec
;
2011 /* Maybe the buffer is too small. */
2012 if (MAX (prec
, width
) + EXTSIZ
> WORK_BUFFER_SIZE
)
2014 if (__libc_use_alloca ((MAX (prec
, width
) + EXTSIZ
)
2016 workend
= ((CHAR_T
*) alloca ((MAX (prec
, width
) + EXTSIZ
)
2018 + (MAX (prec
, width
) + EXTSIZ
));
2021 workstart
= (CHAR_T
*) malloc ((MAX (prec
, width
) + EXTSIZ
)
2023 if (workstart
== NULL
)
2028 workend
= workstart
+ (MAX (prec
, width
) + EXTSIZ
);
2032 /* Process format specifiers. */
2035 extern printf_function
**__printf_function_table
;
2038 if (spec
<= UCHAR_MAX
2039 && __printf_function_table
!= NULL
2040 && __printf_function_table
[(size_t) spec
] != NULL
)
2042 const void **ptr
= alloca (specs
[nspecs_done
].ndata_args
2043 * sizeof (const void *));
2045 /* Fill in an array of pointers to the argument values. */
2046 for (unsigned int i
= 0; i
< specs
[nspecs_done
].ndata_args
;
2048 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
2050 /* Call the function. */
2051 function_done
= __printf_function_table
[(size_t) spec
]
2052 (s
, &specs
[nspecs_done
].info
, ptr
);
2054 if (function_done
!= -2)
2056 /* If an error occurred we don't have information
2057 about # of chars. */
2058 if (function_done
< 0)
2060 /* Function has set errno. */
2065 done_add (function_done
);
2070 JUMP (spec
, step4_jumps
);
2072 process_arg ((&specs
[nspecs_done
]));
2073 process_string_arg ((&specs
[nspecs_done
]));
2075 LABEL (form_unknown
):
2080 ptr
= alloca (specs
[nspecs_done
].ndata_args
2081 * sizeof (const void *));
2083 /* Fill in an array of pointers to the argument values. */
2084 for (i
= 0; i
< specs
[nspecs_done
].ndata_args
; ++i
)
2085 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
2087 /* Call the function. */
2088 function_done
= printf_unknown (s
, &specs
[nspecs_done
].info
,
2091 /* If an error occurred we don't have information about #
2093 if (function_done
< 0)
2095 /* Function has set errno. */
2100 done_add (function_done
);
2105 if (__glibc_unlikely (workstart
!= NULL
))
2109 /* Write the following constant string. */
2110 outstring (specs
[nspecs_done
].end_of_fmt
,
2111 specs
[nspecs_done
].next_fmt
2112 - specs
[nspecs_done
].end_of_fmt
);
2115 if (__glibc_unlikely (workstart
!= NULL
))
2117 scratch_buffer_free (&argsbuf
);
2118 scratch_buffer_free (&specsbuf
);
2122 /* Handle an unknown format specifier. This prints out a canonicalized
2123 representation of the format spec itself. */
2125 printf_unknown (FILE *s
, const struct printf_info
*info
,
2126 const void *const *args
)
2130 CHAR_T work_buffer
[MAX (sizeof (info
->width
), sizeof (info
->prec
)) * 3];
2131 CHAR_T
*const workend
2132 = &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
2143 else if (info
->space
)
2147 if (info
->pad
== L_('0'))
2152 if (info
->width
!= 0)
2154 w
= _itoa_word (info
->width
, workend
, 10, 0);
2159 if (info
->prec
!= -1)
2162 w
= _itoa_word (info
->prec
, workend
, 10, 0);
2167 if (info
->spec
!= L_('\0'))
2168 outchar (info
->spec
);
2174 /* Group the digits from W to REAR_PTR according to the grouping rules
2175 of the current locale. The interpretation of GROUPING is as in
2176 `struct lconv' from <locale.h>. The grouped number extends from
2177 the returned pointer until REAR_PTR. FRONT_PTR to W is used as a
2180 group_number (CHAR_T
*front_ptr
, CHAR_T
*w
, CHAR_T
*rear_ptr
,
2181 const char *grouping
, THOUSANDS_SEP_T thousands_sep
)
2183 /* Length of the current group. */
2185 #ifndef COMPILE_WPRINTF
2186 /* Length of the separator (in wide mode, the separator is always a
2187 single wide character). */
2188 int tlen
= strlen (thousands_sep
);
2191 /* We treat all negative values like CHAR_MAX. */
2193 if (*grouping
== CHAR_MAX
|| *grouping
<= 0)
2194 /* No grouping should be done. */
2199 /* Copy existing string so that nothing gets overwritten. */
2200 memmove (front_ptr
, w
, (rear_ptr
- w
) * sizeof (CHAR_T
));
2201 CHAR_T
*s
= front_ptr
+ (rear_ptr
- w
);
2205 /* Process all characters in the string. */
2206 while (s
> front_ptr
)
2210 if (--len
== 0 && s
> front_ptr
)
2212 /* A new group begins. */
2213 #ifdef COMPILE_WPRINTF
2215 *--w
= thousands_sep
;
2217 /* Not enough room for the separator. */
2223 *--w
= thousands_sep
[--cnt
];
2226 /* Not enough room for the separator. */
2230 if (*grouping
== CHAR_MAX
2237 /* No further grouping to be done. Copy the rest of the
2239 memmove (w
, s
, (front_ptr
-s
) * sizeof (CHAR_T
));
2242 else if (*grouping
!= '\0')
2245 /* The previous grouping repeats ad infinitum. */
2252 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2255 struct _IO_FILE_plus _f
;
2256 #ifdef COMPILE_WPRINTF
2257 struct _IO_wide_data _wide_data
;
2260 #ifdef _IO_MTSAFE_IO
2266 _IO_helper_overflow (FILE *s
, int c
)
2268 FILE *target
= ((struct helper_file
*) s
)->_put_stream
;
2269 #ifdef COMPILE_WPRINTF
2270 int used
= s
->_wide_data
->_IO_write_ptr
- s
->_wide_data
->_IO_write_base
;
2273 size_t written
= _IO_sputn (target
, s
->_wide_data
->_IO_write_base
, used
);
2274 if (written
== 0 || written
== WEOF
)
2276 __wmemmove (s
->_wide_data
->_IO_write_base
,
2277 s
->_wide_data
->_IO_write_base
+ written
,
2279 s
->_wide_data
->_IO_write_ptr
-= written
;
2282 int used
= s
->_IO_write_ptr
- s
->_IO_write_base
;
2285 size_t written
= _IO_sputn (target
, s
->_IO_write_base
, used
);
2286 if (written
== 0 || written
== EOF
)
2288 memmove (s
->_IO_write_base
, s
->_IO_write_base
+ written
,
2290 s
->_IO_write_ptr
-= written
;
2296 #ifdef COMPILE_WPRINTF
2297 static const struct _IO_jump_t _IO_helper_jumps libio_vtable
=
2300 JUMP_INIT (finish
, _IO_wdefault_finish
),
2301 JUMP_INIT (overflow
, _IO_helper_overflow
),
2302 JUMP_INIT (underflow
, _IO_default_underflow
),
2303 JUMP_INIT (uflow
, _IO_default_uflow
),
2304 JUMP_INIT (pbackfail
, (_IO_pbackfail_t
) _IO_wdefault_pbackfail
),
2305 JUMP_INIT (xsputn
, _IO_wdefault_xsputn
),
2306 JUMP_INIT (xsgetn
, _IO_wdefault_xsgetn
),
2307 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2308 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2309 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2310 JUMP_INIT (sync
, _IO_default_sync
),
2311 JUMP_INIT (doallocate
, _IO_wdefault_doallocate
),
2312 JUMP_INIT (read
, _IO_default_read
),
2313 JUMP_INIT (write
, _IO_default_write
),
2314 JUMP_INIT (seek
, _IO_default_seek
),
2315 JUMP_INIT (close
, _IO_default_close
),
2316 JUMP_INIT (stat
, _IO_default_stat
)
2319 static const struct _IO_jump_t _IO_helper_jumps libio_vtable
=
2322 JUMP_INIT (finish
, _IO_default_finish
),
2323 JUMP_INIT (overflow
, _IO_helper_overflow
),
2324 JUMP_INIT (underflow
, _IO_default_underflow
),
2325 JUMP_INIT (uflow
, _IO_default_uflow
),
2326 JUMP_INIT (pbackfail
, _IO_default_pbackfail
),
2327 JUMP_INIT (xsputn
, _IO_default_xsputn
),
2328 JUMP_INIT (xsgetn
, _IO_default_xsgetn
),
2329 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2330 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2331 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2332 JUMP_INIT (sync
, _IO_default_sync
),
2333 JUMP_INIT (doallocate
, _IO_default_doallocate
),
2334 JUMP_INIT (read
, _IO_default_read
),
2335 JUMP_INIT (write
, _IO_default_write
),
2336 JUMP_INIT (seek
, _IO_default_seek
),
2337 JUMP_INIT (close
, _IO_default_close
),
2338 JUMP_INIT (stat
, _IO_default_stat
)
2343 buffered_vfprintf (FILE *s
, const CHAR_T
*format
, va_list args
,
2344 unsigned int mode_flags
)
2347 struct helper_file helper
;
2348 FILE *hp
= (FILE *) &helper
._f
;
2349 int result
, to_flush
;
2351 /* Orient the stream. */
2356 /* Initialize helper. */
2357 helper
._put_stream
= s
;
2358 #ifdef COMPILE_WPRINTF
2359 hp
->_wide_data
= &helper
._wide_data
;
2360 _IO_wsetp (hp
, buf
, buf
+ sizeof buf
/ sizeof (CHAR_T
));
2363 _IO_setp (hp
, buf
, buf
+ sizeof buf
);
2366 hp
->_flags
= _IO_MAGIC
|_IO_NO_READS
|_IO_USER_LOCK
;
2367 #if _IO_JUMPS_OFFSET
2368 hp
->_vtable_offset
= 0;
2370 #ifdef _IO_MTSAFE_IO
2373 hp
->_flags2
= s
->_flags2
;
2374 _IO_JUMPS (&helper
._f
) = (struct _IO_jump_t
*) &_IO_helper_jumps
;
2376 /* Now print to helper instead. */
2377 result
= vfprintf (hp
, format
, args
, mode_flags
);
2380 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile
, s
);
2383 /* Now flush anything from the helper to the S. */
2384 #ifdef COMPILE_WPRINTF
2385 if ((to_flush
= (hp
->_wide_data
->_IO_write_ptr
2386 - hp
->_wide_data
->_IO_write_base
)) > 0)
2388 if ((int) _IO_sputn (s
, hp
->_wide_data
->_IO_write_base
, to_flush
)
2393 if ((to_flush
= hp
->_IO_write_ptr
- hp
->_IO_write_base
) > 0)
2395 if ((int) _IO_sputn (s
, hp
->_IO_write_base
, to_flush
) != to_flush
)
2400 /* Unlock the stream. */
2401 _IO_funlockfile (s
);
2402 __libc_cleanup_region_end (0);