1 /* Copyright (C) 1991-2024 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>
29 #include <libc-lock.h>
30 #include <sys/param.h>
32 #include <locale/localeinfo.h>
33 #include <grouping_iterator.h>
35 #include <scratch_buffer.h>
37 #include <printf_buffer.h>
38 #include <printf_buffer_to_file.h>
40 /* This code is shared between the standard stdio implementation found
41 in GNU C library and the libio implementation originally found in
44 Beside this it is also shared between the normal and wide character
45 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
49 #ifdef COMPILE_WPRINTF
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 #ifndef COMPILE_WPRINTF
124 # include "printf_buffer-char.h"
125 # define vfprintf __vfprintf_internal
126 # define OTHER_CHAR_T wchar_t
127 # define UCHAR_T unsigned char
129 typedef const char *THOUSANDS_SEP_T
;
131 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
132 # define STR_LEN(Str) strlen (Str)
134 # define ORIENT if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
136 # define CONVERT_FROM_OTHER_STRING __wcsrtombs
138 # include "printf_buffer-wchar_t.h"
139 # define vfprintf __vfwprintf_internal
140 # define OTHER_CHAR_T char
141 /* This is a hack!!! There should be a type uwchar_t. */
142 # define UCHAR_T unsigned int /* uwchar_t */
143 # define INT_T wint_t
144 typedef wchar_t THOUSANDS_SEP_T
;
145 # define L_(Str) L##Str
146 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
147 # define STR_LEN(Str) __wcslen (Str)
151 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
152 # define CONVERT_FROM_OTHER_STRING __mbsrtowcs
155 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
156 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
161 /* Include the shared code for parsing the format string. */
162 #include "printf-parse.h"
165 /* Write the string SRC to S. If PREC is non-negative, write at most
166 PREC bytes. If LEFT is true, perform left justification. */
168 outstring_converted_wide_string (struct Xprintf_buffer
*target
,
169 const OTHER_CHAR_T
*src
, int prec
,
170 int width
, bool left
)
172 /* Use a small buffer to combine processing of multiple characters.
173 CONVERT_FROM_OTHER_STRING expects the buffer size in (wide)
174 characters, and buf_length counts that. */
175 enum { buf_length
= 256 / sizeof (CHAR_T
) };
176 CHAR_T buf
[buf_length
];
177 _Static_assert (sizeof (buf
) > MB_LEN_MAX
,
178 "buffer is large enough for a single multi-byte character");
180 /* Add the initial padding if needed. */
181 if (width
> 0 && !left
)
183 /* Make a first pass to find the output width, so that we can
184 add the required padding. */
185 mbstate_t mbstate
= { 0 };
186 const OTHER_CHAR_T
*src_copy
= src
;
187 size_t total_written
;
189 total_written
= CONVERT_FROM_OTHER_STRING
190 (NULL
, &src_copy
, 0, &mbstate
);
193 /* The source might not be null-terminated. Enforce the
194 limit manually, based on the output length. */
197 while (limit
> 0 && src_copy
!= NULL
)
199 size_t write_limit
= buf_length
;
200 if (write_limit
> limit
)
202 size_t written
= CONVERT_FROM_OTHER_STRING
203 (buf
, &src_copy
, write_limit
, &mbstate
);
204 if (written
== (size_t) -1)
206 Xprintf_buffer_mark_failed (target
);
211 total_written
+= written
;
216 /* Output initial padding. */
217 Xprintf_buffer_pad (target
, L_(' '), width
- total_written
);
218 if (Xprintf_buffer_has_failed (target
))
222 /* Convert the input string, piece by piece. */
223 size_t total_written
= 0;
225 mbstate_t mbstate
= { 0 };
226 /* If prec is negative, remaining is not decremented, otherwise,
227 it serves as the write limit. */
228 size_t remaining
= -1;
231 while (remaining
> 0 && src
!= NULL
)
233 size_t write_limit
= buf_length
;
234 if (remaining
< write_limit
)
235 write_limit
= remaining
;
236 size_t written
= CONVERT_FROM_OTHER_STRING
237 (buf
, &src
, write_limit
, &mbstate
);
238 if (written
== (size_t) -1)
240 Xprintf_buffer_mark_failed (target
);
245 Xprintf_buffer_write (target
, buf
, written
);
246 total_written
+= written
;
248 remaining
-= written
;
252 /* Add final padding. */
253 if (width
> 0 && left
)
254 Xprintf_buffer_pad (target
, L_(' '), width
- total_written
);
257 /* Calls __printf_fp or __printf_fphex based on the value of the
258 format specifier INFO->spec. */
260 __printf_fp_spec (struct Xprintf_buffer
*target
,
261 const struct printf_info
*info
, const void *const *args
)
263 if (info
->spec
== 'a' || info
->spec
== 'A')
264 Xprintf (fphex_l_buffer
) (target
, _NL_CURRENT_LOCALE
, info
, args
);
266 Xprintf (fp_l_buffer
) (target
, _NL_CURRENT_LOCALE
, info
, args
);
269 /* For handling long_double and longlong we use the same flag. If
270 `long' and `long long' are effectively the same type define it to
272 #if LONG_MAX == LONG_LONG_MAX
273 # define is_longlong 0
275 # define is_longlong is_long_double
278 /* If `long' and `int' is effectively the same type we don't have to
279 handle `long separately. */
280 #if INT_MAX == LONG_MAX
281 # define is_long_num 0
283 # define is_long_num is_long
287 /* Global constants. */
288 static const CHAR_T null
[] = L_("(null)");
290 /* Size of the work_buffer variable (in characters, not bytes. */
291 enum { WORK_BUFFER_SIZE
= 1000 / sizeof (CHAR_T
) };
293 /* This table maps a character into a number representing a class. In
294 each step there is a destination label for each class. */
295 static const uint8_t jump_table
[] =
297 /* ' ' */ 1, 0, 0, /* '#' */ 4,
298 0, /* '%' */ 14, 0, /* '\''*/ 6,
299 0, 0, /* '*' */ 7, /* '+' */ 2,
300 0, /* '-' */ 3, /* '.' */ 9, 0,
301 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
302 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
303 /* '8' */ 8, /* '9' */ 8, 0, 0,
305 0, /* 'A' */ 26, /* 'B' */ 30, /* 'C' */ 25,
306 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
307 0, /* 'I' */ 29, 0, 0,
308 /* 'L' */ 12, 0, 0, 0,
309 0, 0, 0, /* 'S' */ 21,
311 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
313 0, /* 'a' */ 26, /* 'b' */ 30, /* 'c' */ 20,
314 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
315 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
316 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
317 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
318 /* 't' */ 27, /* 'u' */ 16, 0, /* 'w' */ 31,
319 /* 'x' */ 18, 0, /* 'z' */ 13
322 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
323 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
324 #define LABEL(Name) do_##Name
326 /* 'int' is enough and it saves some space on 64 bit systems. */
327 # define JUMP_TABLE_TYPE const int
328 # define JUMP_TABLE_BASE_LABEL do_form_unknown
329 # define REF(Name) &&do_##Name - &&JUMP_TABLE_BASE_LABEL
330 # define JUMP(ChExpr, table) \
336 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
337 : table[CHAR_CLASS (spec)]; \
338 ptr = &&JUMP_TABLE_BASE_LABEL + offset; \
343 # define JUMP_TABLE_TYPE const void *const
344 # define REF(Name) &&do_##Name
345 # define JUMP(ChExpr, table) \
350 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
351 : table[CHAR_CLASS (spec)]; \
357 #define STEP0_3_TABLE \
358 /* Step 0: at the beginning. */ \
359 static JUMP_TABLE_TYPE step0_jumps[32] = \
361 REF (form_unknown), \
362 REF (flag_space), /* for ' ' */ \
363 REF (flag_plus), /* for '+' */ \
364 REF (flag_minus), /* for '-' */ \
365 REF (flag_hash), /* for '<hash>' */ \
366 REF (flag_zero), /* for '0' */ \
367 REF (flag_quote), /* for '\'' */ \
368 REF (width_asterics), /* for '*' */ \
369 REF (width), /* for '1'...'9' */ \
370 REF (precision), /* for '.' */ \
371 REF (mod_half), /* for 'h' */ \
372 REF (mod_long), /* for 'l' */ \
373 REF (mod_longlong), /* for 'L', 'q' */ \
374 REF (mod_size_t), /* for 'z', 'Z' */ \
375 REF (form_percent), /* for '%' */ \
376 REF (form_integer), /* for 'd', 'i' */ \
377 REF (form_unsigned), /* for 'u' */ \
378 REF (form_octal), /* for 'o' */ \
379 REF (form_hexa), /* for 'X', 'x' */ \
380 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
381 REF (form_character), /* for 'c' */ \
382 REF (form_string), /* for 's', 'S' */ \
383 REF (form_pointer), /* for 'p' */ \
384 REF (form_number), /* for 'n' */ \
385 REF (form_strerror), /* for 'm' */ \
386 REF (form_wcharacter), /* for 'C' */ \
387 REF (form_floathex), /* for 'A', 'a' */ \
388 REF (mod_ptrdiff_t), /* for 't' */ \
389 REF (mod_intmax_t), /* for 'j' */ \
390 REF (flag_i18n), /* for 'I' */ \
391 REF (form_binary), /* for 'B', 'b' */ \
392 REF (mod_bitwidth), /* for 'w' */ \
394 /* Step 1: after processing width. */ \
395 static JUMP_TABLE_TYPE step1_jumps[32] = \
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 (precision), /* 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' */ \
427 REF (form_binary), /* for 'B', 'b' */ \
428 REF (mod_bitwidth), /* for 'w' */ \
430 /* Step 2: after processing precision. */ \
431 static JUMP_TABLE_TYPE step2_jumps[32] = \
433 REF (form_unknown), \
434 REF (form_unknown), /* for ' ' */ \
435 REF (form_unknown), /* for '+' */ \
436 REF (form_unknown), /* for '-' */ \
437 REF (form_unknown), /* for '<hash>' */ \
438 REF (form_unknown), /* for '0' */ \
439 REF (form_unknown), /* for '\'' */ \
440 REF (form_unknown), /* for '*' */ \
441 REF (form_unknown), /* for '1'...'9' */ \
442 REF (form_unknown), /* for '.' */ \
443 REF (mod_half), /* for 'h' */ \
444 REF (mod_long), /* for 'l' */ \
445 REF (mod_longlong), /* for 'L', 'q' */ \
446 REF (mod_size_t), /* for 'z', 'Z' */ \
447 REF (form_percent), /* for '%' */ \
448 REF (form_integer), /* for 'd', 'i' */ \
449 REF (form_unsigned), /* for 'u' */ \
450 REF (form_octal), /* for 'o' */ \
451 REF (form_hexa), /* for 'X', 'x' */ \
452 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
453 REF (form_character), /* for 'c' */ \
454 REF (form_string), /* for 's', 'S' */ \
455 REF (form_pointer), /* for 'p' */ \
456 REF (form_number), /* for 'n' */ \
457 REF (form_strerror), /* for 'm' */ \
458 REF (form_wcharacter), /* for 'C' */ \
459 REF (form_floathex), /* for 'A', 'a' */ \
460 REF (mod_ptrdiff_t), /* for 't' */ \
461 REF (mod_intmax_t), /* for 'j' */ \
462 REF (form_unknown), /* for 'I' */ \
463 REF (form_binary), /* for 'B', 'b' */ \
464 REF (mod_bitwidth), /* for 'w' */ \
466 /* Step 3a: after processing first 'h' modifier. */ \
467 static JUMP_TABLE_TYPE step3a_jumps[32] = \
469 REF (form_unknown), \
470 REF (form_unknown), /* for ' ' */ \
471 REF (form_unknown), /* for '+' */ \
472 REF (form_unknown), /* for '-' */ \
473 REF (form_unknown), /* for '<hash>' */ \
474 REF (form_unknown), /* for '0' */ \
475 REF (form_unknown), /* for '\'' */ \
476 REF (form_unknown), /* for '*' */ \
477 REF (form_unknown), /* for '1'...'9' */ \
478 REF (form_unknown), /* for '.' */ \
479 REF (mod_halfhalf), /* for 'h' */ \
480 REF (form_unknown), /* for 'l' */ \
481 REF (form_unknown), /* for 'L', 'q' */ \
482 REF (form_unknown), /* for 'z', 'Z' */ \
483 REF (form_percent), /* for '%' */ \
484 REF (form_integer), /* for 'd', 'i' */ \
485 REF (form_unsigned), /* for 'u' */ \
486 REF (form_octal), /* for 'o' */ \
487 REF (form_hexa), /* for 'X', 'x' */ \
488 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
489 REF (form_unknown), /* for 'c' */ \
490 REF (form_unknown), /* for 's', 'S' */ \
491 REF (form_unknown), /* for 'p' */ \
492 REF (form_number), /* for 'n' */ \
493 REF (form_unknown), /* for 'm' */ \
494 REF (form_unknown), /* for 'C' */ \
495 REF (form_unknown), /* for 'A', 'a' */ \
496 REF (form_unknown), /* for 't' */ \
497 REF (form_unknown), /* for 'j' */ \
498 REF (form_unknown), /* for 'I' */ \
499 REF (form_binary), /* for 'B', 'b' */ \
500 REF (form_unknown), /* for 'w' */ \
502 /* Step 3b: after processing first 'l' modifier. */ \
503 static JUMP_TABLE_TYPE step3b_jumps[32] = \
505 REF (form_unknown), \
506 REF (form_unknown), /* for ' ' */ \
507 REF (form_unknown), /* for '+' */ \
508 REF (form_unknown), /* for '-' */ \
509 REF (form_unknown), /* for '<hash>' */ \
510 REF (form_unknown), /* for '0' */ \
511 REF (form_unknown), /* for '\'' */ \
512 REF (form_unknown), /* for '*' */ \
513 REF (form_unknown), /* for '1'...'9' */ \
514 REF (form_unknown), /* for '.' */ \
515 REF (form_unknown), /* for 'h' */ \
516 REF (mod_longlong), /* for 'l' */ \
517 REF (form_unknown), /* for 'L', 'q' */ \
518 REF (form_unknown), /* for 'z', 'Z' */ \
519 REF (form_percent), /* for '%' */ \
520 REF (form_integer), /* for 'd', 'i' */ \
521 REF (form_unsigned), /* for 'u' */ \
522 REF (form_octal), /* for 'o' */ \
523 REF (form_hexa), /* for 'X', 'x' */ \
524 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
525 REF (form_character), /* for 'c' */ \
526 REF (form_string), /* for 's', 'S' */ \
527 REF (form_pointer), /* for 'p' */ \
528 REF (form_number), /* for 'n' */ \
529 REF (form_strerror), /* for 'm' */ \
530 REF (form_wcharacter), /* for 'C' */ \
531 REF (form_floathex), /* for 'A', 'a' */ \
532 REF (form_unknown), /* for 't' */ \
533 REF (form_unknown), /* for 'j' */ \
534 REF (form_unknown), /* for 'I' */ \
535 REF (form_binary), /* for 'B', 'b' */ \
536 REF (form_unknown), /* for 'w' */ \
539 #define STEP4_TABLE \
540 /* Step 4: processing format specifier. */ \
541 static JUMP_TABLE_TYPE step4_jumps[32] = \
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 (form_unknown), /* 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_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
563 REF (form_character), /* for 'c' */ \
564 REF (form_string), /* for 's', 'S' */ \
565 REF (form_pointer), /* for 'p' */ \
566 REF (form_number), /* for 'n' */ \
567 REF (form_strerror), /* for 'm' */ \
568 REF (form_wcharacter), /* for 'C' */ \
569 REF (form_floathex), /* for 'A', 'a' */ \
570 REF (form_unknown), /* for 't' */ \
571 REF (form_unknown), /* for 'j' */ \
572 REF (form_unknown), /* for 'I' */ \
573 REF (form_binary), /* for 'B', 'b' */ \
574 REF (form_unknown), /* for 'w' */ \
577 /* Handle positional format specifiers. */
578 static void printf_positional (struct Xprintf_buffer
*buf
,
579 const CHAR_T
*format
, int readonly_format
,
580 va_list ap
, va_list *ap_savep
,
581 int nspecs_done
, const UCHAR_T
*lead_str_end
,
582 CHAR_T
*work_buffer
, int save_errno
,
583 const char *grouping
,
584 THOUSANDS_SEP_T thousands_sep
,
585 unsigned int mode_flags
);
587 /* Handle unknown format specifier. */
588 static void printf_unknown (struct Xprintf_buffer
*,
589 const struct printf_info
*) __THROW
;
591 static void group_number (struct Xprintf_buffer
*buf
,
592 struct grouping_iterator
*iter
,
593 CHAR_T
*from
, CHAR_T
*to
,
594 THOUSANDS_SEP_T thousands_sep
, bool i18n
);
596 /* The buffer-based function itself. */
598 Xprintf_buffer (struct Xprintf_buffer
*buf
, const CHAR_T
*format
,
599 va_list ap
, unsigned int mode_flags
)
601 /* The character used as thousands separator. */
602 THOUSANDS_SEP_T thousands_sep
= 0;
604 /* The string describing the size of groups of digits. */
605 const char *grouping
;
607 /* Current character in format string. */
610 /* End of leading constant string. */
611 const UCHAR_T
*lead_str_end
;
613 /* Points to next format specifier. */
614 const UCHAR_T
*end_of_spec
;
616 /* Buffer intermediate results. */
617 CHAR_T work_buffer
[WORK_BUFFER_SIZE
];
620 /* We have to save the original argument pointer. */
623 /* Count number of specifiers we already processed. */
626 /* For the %m format we may need the current `errno' value. */
627 int save_errno
= errno
;
629 /* 1 if format is in read-only memory, -1 if it is in writable memory,
631 int readonly_format
= 0;
633 /* Initialize local variables. */
634 grouping
= (const char *) -1;
636 /* This macro will be available soon in gcc's <stdarg.h>. We need it
637 since on some systems `va_list' is not an integral type. */
638 __va_copy (ap_save
, ap
);
644 #ifdef COMPILE_WPRINTF
645 /* Find the first format specifier. */
646 f
= lead_str_end
= __find_specwc ((const UCHAR_T
*) format
);
648 /* Find the first format specifier. */
649 f
= lead_str_end
= __find_specmb ((const UCHAR_T
*) format
);
652 /* Write the literal text before the first format. */
653 Xprintf_buffer_write (buf
, format
,
654 lead_str_end
- (const UCHAR_T
*) format
);
655 if (Xprintf_buffer_has_failed (buf
))
658 /* If we only have to print a simple string, return now. */
662 /* Use the slow path in case any printf handler is registered. */
663 if (__glibc_unlikely (__printf_function_table
!= NULL
664 || __printf_modifier_table
!= NULL
665 || __printf_va_arg_table
!= NULL
))
668 /* Process whole format string. */
674 int is_negative
; /* Flag for negative number. */
677 unsigned long long int longlong
;
678 unsigned long int word
;
681 union printf_arg the_arg
;
682 CHAR_T
*string
; /* Pointer to argument string. */
683 int alt
= 0; /* Alternate format. */
684 int space
= 0; /* Use space prefix if no sign is needed. */
685 int left
= 0; /* Left-justify output. */
686 int showsign
= 0; /* Always begin with plus or minus sign. */
687 int group
= 0; /* Print numbers according grouping rules. */
688 /* Argument is long double/long long int. Only used if
689 double/long double or long int/long long int are distinct. */
690 int is_long_double
__attribute__ ((unused
)) = 0;
691 int is_short
= 0; /* Argument is short int. */
692 int is_long
= 0; /* Argument is long int. */
693 int is_char
= 0; /* Argument is promoted (unsigned) char. */
694 int width
= 0; /* Width of output; 0 means none specified. */
695 int prec
= -1; /* Precision of output; -1 means none specified. */
696 /* This flag is set by the 'I' modifier and selects the use of the
697 `outdigits' as determined by the current locale. */
698 int use_outdigits
= 0;
699 UCHAR_T pad
= L_(' ');/* Padding character. */
702 workend
= work_buffer
+ WORK_BUFFER_SIZE
;
704 /* Get current character in format string. */
705 JUMP (*++f
, step0_jumps
);
710 JUMP (*++f
, step0_jumps
);
715 JUMP (*++f
, step0_jumps
);
721 JUMP (*++f
, step0_jumps
);
726 JUMP (*++f
, step0_jumps
);
732 JUMP (*++f
, step0_jumps
);
738 if (grouping
== (const char *) -1)
740 #ifdef COMPILE_WPRINTF
741 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
742 _NL_NUMERIC_THOUSANDS_SEP_WC
);
744 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
747 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
748 if (*grouping
== '\0' || *grouping
== CHAR_MAX
749 #ifdef COMPILE_WPRINTF
750 || thousands_sep
== L
'\0'
752 || *thousands_sep
== '\0'
757 JUMP (*++f
, step0_jumps
);
761 JUMP (*++f
, step0_jumps
);
763 /* Get width from argument. */
764 LABEL (width_asterics
):
766 const UCHAR_T
*tmp
; /* Temporary value. */
771 int pos
= read_int (&tmp
);
775 __set_errno (EOVERFLOW
);
776 Xprintf_buffer_mark_failed (buf
);
780 if (pos
&& *tmp
== L_('$'))
781 /* The width comes from a positional parameter. */
784 width
= va_arg (ap
, int);
786 /* Negative width means left justified. */
794 JUMP (*f
, step1_jumps
);
796 /* Given width in format string. */
798 width
= read_int (&f
);
800 if (__glibc_unlikely (width
== -1))
802 __set_errno (EOVERFLOW
);
803 Xprintf_buffer_mark_failed (buf
);
808 /* Oh, oh. The argument comes from a positional parameter. */
810 JUMP (*f
, step1_jumps
);
816 const UCHAR_T
*tmp
; /* Temporary value. */
821 int pos
= read_int (&tmp
);
825 __set_errno (EOVERFLOW
);
826 Xprintf_buffer_mark_failed (buf
);
830 if (pos
&& *tmp
== L_('$'))
831 /* The precision comes from a positional parameter. */
834 prec
= va_arg (ap
, int);
836 /* If the precision is negative the precision is omitted. */
840 else if (ISDIGIT (*f
))
842 prec
= read_int (&f
);
844 /* The precision was specified in this case as an extremely
845 large positive value. */
848 __set_errno (EOVERFLOW
);
849 Xprintf_buffer_mark_failed (buf
);
855 JUMP (*f
, step2_jumps
);
857 /* Process 'h' modifier. There might another 'h' following. */
860 JUMP (*++f
, step3a_jumps
);
862 /* Process 'hh' modifier. */
863 LABEL (mod_halfhalf
):
866 JUMP (*++f
, step4_jumps
);
868 /* Process 'l' modifier. There might another 'l' following. */
871 JUMP (*++f
, step3b_jumps
);
873 /* Process 'L', 'q', or 'll' modifier. No other modifier is
874 allowed to follow. */
875 LABEL (mod_longlong
):
878 JUMP (*++f
, step4_jumps
);
881 is_long_double
= sizeof (size_t) > sizeof (unsigned long int);
882 is_long
= sizeof (size_t) > sizeof (unsigned int);
883 JUMP (*++f
, step4_jumps
);
885 LABEL (mod_ptrdiff_t
):
886 is_long_double
= sizeof (ptrdiff_t) > sizeof (unsigned long int);
887 is_long
= sizeof (ptrdiff_t) > sizeof (unsigned int);
888 JUMP (*++f
, step4_jumps
);
890 LABEL (mod_intmax_t
):
891 is_long_double
= sizeof (intmax_t) > sizeof (unsigned long int);
892 is_long
= sizeof (intmax_t) > sizeof (unsigned int);
893 JUMP (*++f
, step4_jumps
);
895 /* Process 'wN' or 'wfN' modifier. */
896 LABEL (mod_bitwidth
):
898 bool is_fast
= false;
906 bitwidth
= read_int (&f
);
911 bitwidth
= INT_FAST8_WIDTH
;
914 bitwidth
= INT_FAST16_WIDTH
;
917 bitwidth
= INT_FAST32_WIDTH
;
920 bitwidth
= INT_FAST64_WIDTH
;
938 /* ISO C requires this error to be detected. */
939 __set_errno (EINVAL
);
940 Xprintf_buffer_mark_failed (buf
);
943 JUMP (*f
, step4_jumps
);
945 /* Process current format. */
948 #define process_arg_int() va_arg (ap, int)
949 #define process_arg_long_int() va_arg (ap, long int)
950 #define process_arg_long_long_int() va_arg (ap, long long int)
951 #define process_arg_pointer() va_arg (ap, void *)
952 #define process_arg_string() va_arg (ap, const char *)
953 #define process_arg_unsigned_int() va_arg (ap, unsigned int)
954 #define process_arg_unsigned_long_int() va_arg (ap, unsigned long int)
955 #define process_arg_unsigned_long_long_int() va_arg (ap, unsigned long long int)
956 #define process_arg_wchar_t() va_arg (ap, wchar_t)
957 #define process_arg_wstring() va_arg (ap, const wchar_t *)
958 #include "vfprintf-process-arg.c"
959 #undef process_arg_int
960 #undef process_arg_long_int
961 #undef process_arg_long_long_int
962 #undef process_arg_pointer
963 #undef process_arg_string
964 #undef process_arg_unsigned_int
965 #undef process_arg_unsigned_long_int
966 #undef process_arg_unsigned_long_long_int
967 #undef process_arg_wchar_t
968 #undef process_arg_wstring
971 LABEL (form_floathex
):
973 if (__glibc_unlikely ((mode_flags
& PRINTF_LDBL_IS_DBL
) != 0))
976 struct printf_info info
=
981 .is_long_double
= is_long_double
,
982 .is_short
= is_short
,
987 .showsign
= showsign
,
991 .i18n
= use_outdigits
,
992 .wide
= sizeof (CHAR_T
) != 1,
996 PARSE_FLOAT_VA_ARG_EXTENDED (info
);
997 const void *ptr
= &the_arg
;
999 __printf_fp_spec (buf
, &info
, &ptr
);
1003 LABEL (form_unknown
):
1004 if (spec
== L_('\0'))
1006 /* The format string ended before the specifier is complete. */
1007 __set_errno (EINVAL
);
1008 Xprintf_buffer_mark_failed (buf
);
1012 /* If we are in the fast loop force entering the complicated
1017 /* The format is correctly handled. */
1020 /* Look for next format specifier. */
1021 #ifdef COMPILE_WPRINTF
1022 f
= __find_specwc ((end_of_spec
= ++f
));
1024 f
= __find_specmb ((end_of_spec
= ++f
));
1027 /* Write the following constant string. */
1028 Xprintf_buffer_write (buf
, (const CHAR_T
*) end_of_spec
,
1031 while (*f
!= L_('\0') && !Xprintf_buffer_has_failed (buf
));
1034 /* printf_positional performs cleanup under its all_done label, so
1035 vfprintf-process-arg.c uses it for this function and
1036 printf_positional below. */
1039 /* Hand off processing for positional parameters. */
1041 printf_positional (buf
, format
, readonly_format
, ap
, &ap_save
,
1042 nspecs_done
, lead_str_end
, work_buffer
,
1043 save_errno
, grouping
, thousands_sep
, mode_flags
);
1047 printf_positional (struct Xprintf_buffer
* buf
, const CHAR_T
*format
,
1048 int readonly_format
,
1049 va_list ap
, va_list *ap_savep
, int nspecs_done
,
1050 const UCHAR_T
*lead_str_end
,
1051 CHAR_T
*work_buffer
, int save_errno
,
1052 const char *grouping
, THOUSANDS_SEP_T thousands_sep
,
1053 unsigned int mode_flags
)
1055 /* For positional argument handling. */
1056 struct scratch_buffer specsbuf
;
1057 scratch_buffer_init (&specsbuf
);
1058 struct printf_spec
*specs
= specsbuf
.data
;
1059 size_t specs_limit
= specsbuf
.length
/ sizeof (specs
[0]);
1061 /* Used as a backing store for args_value, args_size, args_type
1063 struct scratch_buffer argsbuf
;
1064 scratch_buffer_init (&argsbuf
);
1066 /* Array with information about the needed arguments. This has to
1067 be dynamically extensible. */
1070 /* The number of arguments the format string requests. This will
1071 determine the size of the array needed to store the argument
1075 /* Positional parameters refer to arguments directly. This could
1076 also determine the maximum number of arguments. Track the
1078 size_t max_ref_arg
= 0;
1080 /* Just a counter. */
1083 if (grouping
== (const char *) -1)
1085 #ifdef COMPILE_WPRINTF
1086 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1087 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1089 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1092 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1093 if (*grouping
== '\0' || *grouping
== CHAR_MAX
)
1097 for (const UCHAR_T
*f
= lead_str_end
; *f
!= L_('\0');
1098 f
= specs
[nspecs
++].next_fmt
)
1100 if (nspecs
== specs_limit
)
1102 if (!scratch_buffer_grow_preserve (&specsbuf
))
1104 Xprintf_buffer_mark_failed (buf
);
1107 specs
= specsbuf
.data
;
1108 specs_limit
= specsbuf
.length
/ sizeof (specs
[0]);
1111 /* Parse the format specifier. */
1113 #ifdef COMPILE_WPRINTF
1114 nargs
+= __parse_one_specwc (f
, nargs
, &specs
[nspecs
], &max_ref_arg
,
1117 nargs
+= __parse_one_specmb (f
, nargs
, &specs
[nspecs
], &max_ref_arg
,
1122 Xprintf_buffer_mark_failed (buf
);
1127 /* Determine the number of arguments the format string consumes. */
1128 nargs
= MAX (nargs
, max_ref_arg
);
1130 union printf_arg
*args_value
;
1134 size_t args_pa_user_offset
;
1136 /* Calculate total size needed to represent a single argument
1137 across all three argument-related arrays. */
1138 size_t bytes_per_arg
1139 = sizeof (*args_value
) + sizeof (*args_size
) + sizeof (*args_type
);
1140 if (!scratch_buffer_set_array_size (&argsbuf
, nargs
, bytes_per_arg
))
1142 Xprintf_buffer_mark_failed (buf
);
1145 args_value
= argsbuf
.data
;
1146 /* Set up the remaining two arrays to each point past the end of
1147 the prior array, since space for all three has been allocated
1149 args_size
= &args_value
[nargs
].pa_int
;
1150 args_type
= &args_size
[nargs
];
1151 args_pa_user
= &args_type
[nargs
];
1152 memset (args_type
, (mode_flags
& PRINTF_FORTIFY
) != 0 ? '\xff' : '\0',
1153 nargs
* sizeof (*args_type
));
1156 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1157 still zero after this loop, format is invalid. For now we
1158 simply use 0 as the value. */
1160 /* Fill in the types of all the arguments. */
1161 for (cnt
= 0; cnt
< nspecs
; ++cnt
)
1163 /* If the width is determined by an argument this is an int. */
1164 if (specs
[cnt
].width_arg
!= -1)
1165 args_type
[specs
[cnt
].width_arg
] = PA_INT
;
1167 /* If the precision is determined by an argument this is an int. */
1168 if (specs
[cnt
].prec_arg
!= -1)
1169 args_type
[specs
[cnt
].prec_arg
] = PA_INT
;
1171 switch (specs
[cnt
].ndata_args
)
1173 case 0: /* No arguments. */
1175 case 1: /* One argument; we already have the
1177 args_type
[specs
[cnt
].data_arg
] = specs
[cnt
].data_arg_type
;
1178 args_size
[specs
[cnt
].data_arg
] = specs
[cnt
].size
;
1181 /* We have more than one argument for this format spec.
1182 We must call the arginfo function again to determine
1184 (void) (*__printf_arginfo_table
[specs
[cnt
].info
.spec
])
1186 specs
[cnt
].ndata_args
, &args_type
[specs
[cnt
].data_arg
],
1187 &args_size
[specs
[cnt
].data_arg
]);
1192 /* Now we know all the types and the order. Fill in the argument
1194 for (cnt
= 0; cnt
< nargs
; ++cnt
)
1195 switch (args_type
[cnt
])
1197 #define T(tag, mem, type) \
1199 args_value[cnt].mem = va_arg (*ap_savep, type); \
1202 T (PA_WCHAR
, pa_wchar
, wint_t);
1203 case PA_CHAR
: /* Promoted. */
1204 case PA_INT
|PA_FLAG_SHORT
: /* Promoted. */
1205 #if LONG_MAX == INT_MAX
1206 case PA_INT
|PA_FLAG_LONG
:
1208 T (PA_INT
, pa_int
, int);
1209 #if LONG_MAX == LONG_LONG_MAX
1210 case PA_INT
|PA_FLAG_LONG
:
1212 T (PA_INT
|PA_FLAG_LONG_LONG
, pa_long_long_int
, long long int);
1213 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1216 case PA_FLOAT
: /* Promoted. */
1217 T (PA_DOUBLE
, pa_double
, double);
1218 case PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
:
1219 if (__glibc_unlikely ((mode_flags
& PRINTF_LDBL_IS_DBL
) != 0))
1221 args_value
[cnt
].pa_double
= va_arg (*ap_savep
, double);
1222 args_type
[cnt
] &= ~PA_FLAG_LONG_DOUBLE
;
1224 #if __HAVE_FLOAT128_UNLIKE_LDBL
1225 else if ((mode_flags
& PRINTF_LDBL_USES_FLOAT128
) != 0)
1226 args_value
[cnt
].pa_float128
= va_arg (*ap_savep
, _Float128
);
1229 args_value
[cnt
].pa_long_double
= va_arg (*ap_savep
, long double);
1231 case PA_STRING
: /* All pointers are the same */
1232 case PA_WSTRING
: /* All pointers are the same */
1233 T (PA_POINTER
, pa_pointer
, void *);
1236 if ((args_type
[cnt
] & PA_FLAG_PTR
) != 0)
1237 args_value
[cnt
].pa_pointer
= va_arg (*ap_savep
, void *);
1238 else if (__glibc_unlikely (__printf_va_arg_table
!= NULL
)
1239 && __printf_va_arg_table
[args_type
[cnt
] - PA_LAST
] != NULL
)
1241 while (args_pa_user
+ args_size
[cnt
] >
1242 argsbuf
.data
+ argsbuf
.length
)
1244 args_pa_user_offset
= args_pa_user
- (void *) &args_type
[nargs
];
1245 if (!scratch_buffer_grow_preserve (&argsbuf
))
1247 Xprintf_buffer_mark_failed (buf
);
1250 args_value
= argsbuf
.data
;
1251 /* Set up the remaining two arrays to each point past the end of
1252 the prior array, since space for all three has been allocated
1254 args_size
= &args_value
[nargs
].pa_int
;
1255 args_type
= &args_size
[nargs
];
1256 args_pa_user
= (void *) &args_type
[nargs
] + args_pa_user_offset
;
1258 args_value
[cnt
].pa_user
= args_pa_user
;
1259 args_pa_user
+= args_size
[cnt
];
1260 (*__printf_va_arg_table
[args_type
[cnt
] - PA_LAST
])
1261 (args_value
[cnt
].pa_user
, ap_savep
);
1264 memset (&args_value
[cnt
], 0, sizeof (args_value
[cnt
]));
1267 /* Error case. Not all parameters appear in N$ format
1268 strings. We have no way to determine their type. */
1269 assert ((mode_flags
& PRINTF_FORTIFY
) != 0);
1270 __libc_fatal ("*** invalid %N$ use detected ***\n");
1273 /* Now walk through all format specifiers and process them. */
1274 for (; (size_t) nspecs_done
< nspecs
&& !Xprintf_buffer_has_failed (buf
);
1282 unsigned long long int longlong
;
1283 unsigned long int word
;
1286 CHAR_T
*string
; /* Pointer to argument string. */
1288 /* Fill variables from values in struct. */
1289 int alt
= specs
[nspecs_done
].info
.alt
;
1290 int space
= specs
[nspecs_done
].info
.space
;
1291 int left
= specs
[nspecs_done
].info
.left
;
1292 int showsign
= specs
[nspecs_done
].info
.showsign
;
1293 int group
= specs
[nspecs_done
].info
.group
;
1294 int is_long_double
__attribute__ ((unused
))
1295 = specs
[nspecs_done
].info
.is_long_double
;
1296 int is_short
= specs
[nspecs_done
].info
.is_short
;
1297 int is_char
= specs
[nspecs_done
].info
.is_char
;
1298 int is_long
= specs
[nspecs_done
].info
.is_long
;
1299 int width
= specs
[nspecs_done
].info
.width
;
1300 int prec
= specs
[nspecs_done
].info
.prec
;
1301 int use_outdigits
= specs
[nspecs_done
].info
.i18n
;
1302 char pad
= specs
[nspecs_done
].info
.pad
;
1303 CHAR_T spec
= specs
[nspecs_done
].info
.spec
;
1305 CHAR_T
*workend
= work_buffer
+ WORK_BUFFER_SIZE
;
1307 /* Fill in last information. */
1308 if (specs
[nspecs_done
].width_arg
!= -1)
1310 /* Extract the field width from an argument. */
1311 specs
[nspecs_done
].info
.width
=
1312 args_value
[specs
[nspecs_done
].width_arg
].pa_int
;
1314 if (specs
[nspecs_done
].info
.width
< 0)
1315 /* If the width value is negative left justification is
1316 selected and the value is taken as being positive. */
1318 specs
[nspecs_done
].info
.width
*= -1;
1319 left
= specs
[nspecs_done
].info
.left
= 1;
1321 width
= specs
[nspecs_done
].info
.width
;
1324 if (specs
[nspecs_done
].prec_arg
!= -1)
1326 /* Extract the precision from an argument. */
1327 specs
[nspecs_done
].info
.prec
=
1328 args_value
[specs
[nspecs_done
].prec_arg
].pa_int
;
1330 if (specs
[nspecs_done
].info
.prec
< 0)
1331 /* If the precision is negative the precision is
1333 specs
[nspecs_done
].info
.prec
= -1;
1335 prec
= specs
[nspecs_done
].info
.prec
;
1338 /* Process format specifiers. */
1341 if (spec
<= UCHAR_MAX
1342 && __printf_function_table
!= NULL
1343 && __printf_function_table
[(size_t) spec
] != NULL
)
1346 = Xprintf (function_invoke
) (buf
,
1347 __printf_function_table
[(size_t) spec
],
1348 &args_value
[specs
[nspecs_done
]
1350 specs
[nspecs_done
].ndata_args
,
1351 &specs
[nspecs_done
].info
);
1352 if (function_done
!= -2)
1354 /* If an error occurred we don't have information
1355 about # of chars. */
1356 if (function_done
< 0)
1358 /* Function has set errno. */
1359 Xprintf_buffer_mark_failed (buf
);
1366 JUMP (spec
, step4_jumps
);
1368 #define process_arg_data args_value[specs[nspecs_done].data_arg]
1369 #define process_arg_int() process_arg_data.pa_int
1370 #define process_arg_long_int() process_arg_data.pa_long_int
1371 #define process_arg_long_long_int() process_arg_data.pa_long_long_int
1372 #define process_arg_pointer() process_arg_data.pa_pointer
1373 #define process_arg_string() process_arg_data.pa_string
1374 #define process_arg_unsigned_int() process_arg_data.pa_u_int
1375 #define process_arg_unsigned_long_int() process_arg_data.pa_u_long_int
1376 #define process_arg_unsigned_long_long_int() process_arg_data.pa_u_long_long_int
1377 #define process_arg_wchar_t() process_arg_data.pa_wchar
1378 #define process_arg_wstring() process_arg_data.pa_wstring
1379 #include "vfprintf-process-arg.c"
1380 #undef process_arg_data
1381 #undef process_arg_int
1382 #undef process_arg_long_int
1383 #undef process_arg_long_long_int
1384 #undef process_arg_pointer
1385 #undef process_arg_string
1386 #undef process_arg_unsigned_int
1387 #undef process_arg_unsigned_long_int
1388 #undef process_arg_unsigned_long_long_int
1389 #undef process_arg_wchar_t
1390 #undef process_arg_wstring
1393 LABEL (form_floathex
):
1396 = (const void *) &args_value
[specs
[nspecs_done
].data_arg
];
1397 if (__glibc_unlikely ((mode_flags
& PRINTF_LDBL_IS_DBL
) != 0))
1399 specs
[nspecs_done
].data_arg_type
= PA_DOUBLE
;
1400 specs
[nspecs_done
].info
.is_long_double
= 0;
1402 SETUP_FLOAT128_INFO (specs
[nspecs_done
].info
);
1404 __printf_fp_spec (buf
, &specs
[nspecs_done
].info
, &ptr
);
1408 LABEL (form_unknown
):
1410 printf_unknown (buf
, &specs
[nspecs_done
].info
);
1414 while (Xprintf_buffer_has_failed (buf
));
1416 /* Write the following constant string. */
1417 Xprintf_buffer_write (buf
,
1418 (const CHAR_T
*) specs
[nspecs_done
].end_of_fmt
,
1419 (specs
[nspecs_done
].next_fmt
1420 - specs
[nspecs_done
].end_of_fmt
));
1423 scratch_buffer_free (&argsbuf
);
1424 scratch_buffer_free (&specsbuf
);
1427 /* Handle an unknown format specifier. This prints out a canonicalized
1428 representation of the format spec itself. */
1430 printf_unknown (struct Xprintf_buffer
*buf
, const struct printf_info
*info
)
1432 CHAR_T work_buffer
[MAX (sizeof (info
->width
), sizeof (info
->prec
)) * 3];
1433 CHAR_T
*const workend
1434 = &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
1437 Xprintf_buffer_putc (buf
, L_('%'));
1440 Xprintf_buffer_putc (buf
, L_('#'));
1442 Xprintf_buffer_putc (buf
, L_('\''));
1444 Xprintf_buffer_putc (buf
, L_('+'));
1445 else if (info
->space
)
1446 Xprintf_buffer_putc (buf
, L_(' '));
1448 Xprintf_buffer_putc (buf
, L_('-'));
1449 if (info
->pad
== L_('0'))
1450 Xprintf_buffer_putc (buf
, L_('0'));
1452 Xprintf_buffer_putc (buf
, L_('I'));
1454 if (info
->width
!= 0)
1456 w
= _itoa_word (info
->width
, workend
, 10, 0);
1458 Xprintf_buffer_putc (buf
, *w
++);
1461 if (info
->prec
!= -1)
1463 Xprintf_buffer_putc (buf
, L_('.'));
1464 w
= _itoa_word (info
->prec
, workend
, 10, 0);
1466 Xprintf_buffer_putc (buf
, *w
++);
1469 if (info
->spec
!= L_('\0'))
1470 Xprintf_buffer_putc (buf
, info
->spec
);
1474 group_number (struct Xprintf_buffer
*buf
,
1475 struct grouping_iterator
*iter
,
1476 CHAR_T
*from
, CHAR_T
*to
, THOUSANDS_SEP_T thousands_sep
,
1480 for (CHAR_T
*cp
= from
; cp
!= to
; ++cp
)
1482 if (__grouping_iterator_next (iter
))
1484 #ifdef COMPILE_WPRINTF
1485 __wprintf_buffer_putc (buf
, thousands_sep
);
1487 __printf_buffer_puts (buf
, thousands_sep
);
1490 Xprintf_buffer_putc (buf
, *cp
);
1494 /* Apply digit translation and grouping. */
1495 for (CHAR_T
*cp
= from
; cp
!= to
; ++cp
)
1497 if (__grouping_iterator_next (iter
))
1499 #ifdef COMPILE_WPRINTF
1500 __wprintf_buffer_putc (buf
, thousands_sep
);
1502 __printf_buffer_puts (buf
, thousands_sep
);
1505 int digit
= *cp
- '0';
1506 #ifdef COMPILE_WPRINTF
1507 __wprintf_buffer_putc
1508 (buf
, _NL_CURRENT_WORD (LC_CTYPE
,
1509 _NL_CTYPE_OUTDIGIT0_WC
+ digit
));
1511 __printf_buffer_puts
1512 (buf
, _NL_CURRENT (LC_CTYPE
, _NL_CTYPE_OUTDIGIT0_MB
+ digit
));
1519 /* The FILE-based function. */
1521 vfprintf (FILE *s
, const CHAR_T
*format
, va_list ap
, unsigned int mode_flags
)
1523 /* Orient the stream. */
1528 /* Sanity check of arguments. */
1529 ARGCHECK (s
, format
);
1532 /* Check for correct orientation. */
1533 if (_IO_vtable_offset (s
) == 0
1534 && _IO_fwide (s
, sizeof (CHAR_T
) == 1 ? -1 : 1)
1535 != (sizeof (CHAR_T
) == 1 ? -1 : 1))
1536 /* The stream is already oriented otherwise. */
1540 if (!_IO_need_lock (s
))
1542 struct Xprintf (buffer_to_file
) wrap
;
1543 Xprintf (buffer_to_file_init
) (&wrap
, s
);
1544 Xprintf_buffer (&wrap
.base
, format
, ap
, mode_flags
);
1545 return Xprintf (buffer_to_file_done
) (&wrap
);
1551 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile
, s
);
1554 /* Set up the wrapping buffer. */
1555 struct Xprintf (buffer_to_file
) wrap
;
1556 Xprintf (buffer_to_file_init
) (&wrap
, s
);
1558 /* Perform the printing operation on the buffer. */
1559 Xprintf_buffer (&wrap
.base
, format
, ap
, mode_flags
);
1560 done
= Xprintf (buffer_to_file_done
) (&wrap
);
1562 /* Unlock the stream. */
1563 _IO_funlockfile (s
);
1564 _IO_cleanup_region_end (0);