1 /* Copyright (C) 1991-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
27 #include <libc-lock.h>
28 #include <sys/param.h>
30 #include <locale/localeinfo.h>
32 #include <scratch_buffer.h>
34 /* This code is shared between the standard stdio implementation found
35 in GNU C library and the libio implementation originally found in
38 Beside this it is also shared between the normal and wide character
39 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
45 #define va_list _IO_va_list
47 #define BUFSIZ _IO_BUFSIZ
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); \
69 #define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
71 #define done_add(val) \
73 unsigned int _val = val; \
74 assert ((unsigned int) done < (unsigned int) INT_MAX); \
75 if (__glibc_unlikely (INT_MAX - done < _val)) \
78 __set_errno (EOVERFLOW); \
84 #ifndef COMPILE_WPRINTF
85 # define vfprintf _IO_vfprintf_internal
87 # define UCHAR_T unsigned char
89 typedef const char *THOUSANDS_SEP_T
;
91 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
92 # define STR_LEN(Str) strlen (Str)
94 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
95 # define PAD(Padchar) \
99 _IO_ssize_t written = _IO_padn (s, (Padchar), width); \
100 if (__glibc_unlikely (written != width)) \
105 done_add (written); \
108 # define PUTC(C, F) _IO_putc_unlocked (C, F)
109 # define ORIENT if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
112 # define vfprintf _IO_vfwprintf
113 # define CHAR_T wchar_t
114 /* This is a hack!!! There should be a type uwchar_t. */
115 # define UCHAR_T unsigned int /* uwchar_t */
116 # define INT_T wint_t
117 typedef wchar_t THOUSANDS_SEP_T
;
118 # define L_(Str) L##Str
119 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
120 # define STR_LEN(Str) __wcslen (Str)
124 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
125 # define PAD(Padchar) \
129 _IO_ssize_t written = _IO_wpadn (s, (Padchar), width); \
130 if (__glibc_unlikely (written != width)) \
135 done_add (written); \
138 # define PUTC(C, F) _IO_putwc_unlocked (C, F)
139 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
142 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
143 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
148 #include "_i18n_number.h"
150 /* Include the shared code for parsing the format string. */
151 #include "printf-parse.h"
154 #define outchar(Ch) \
157 const INT_T outc = (Ch); \
158 if (PUTC (outc, s) == EOF || done == INT_MAX) \
167 #define outstring(String, Len) \
170 assert ((size_t) done <= (size_t) INT_MAX); \
171 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
176 if (__glibc_unlikely (INT_MAX - done < (Len))) \
179 __set_errno (EOVERFLOW); \
186 /* For handling long_double and longlong we use the same flag. If
187 `long' and `long long' are effectively the same type define it to
189 #if LONG_MAX == LONG_LONG_MAX
190 # define is_longlong 0
192 # define is_longlong is_long_double
195 /* If `long' and `int' is effectively the same type we don't have to
196 handle `long separately. */
197 #if INT_MAX == LONG_MAX
198 # define is_long_num 0
200 # define is_long_num is_long
204 /* Global constants. */
205 static const CHAR_T null
[] = L_("(null)");
207 /* Size of the work_buffer variable (in characters, not bytes. */
208 enum { WORK_BUFFER_SIZE
= 1000 / sizeof (CHAR_T
) };
210 /* This table maps a character into a number representing a class. In
211 each step there is a destination label for each class. */
212 static const uint8_t jump_table
[] =
214 /* ' ' */ 1, 0, 0, /* '#' */ 4,
215 0, /* '%' */ 14, 0, /* '\''*/ 6,
216 0, 0, /* '*' */ 7, /* '+' */ 2,
217 0, /* '-' */ 3, /* '.' */ 9, 0,
218 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
219 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
220 /* '8' */ 8, /* '9' */ 8, 0, 0,
222 0, /* 'A' */ 26, 0, /* 'C' */ 25,
223 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
224 0, /* 'I' */ 29, 0, 0,
225 /* 'L' */ 12, 0, 0, 0,
226 0, 0, 0, /* 'S' */ 21,
228 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
230 0, /* 'a' */ 26, 0, /* 'c' */ 20,
231 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
232 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
233 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
234 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
235 /* 't' */ 27, /* 'u' */ 16, 0, 0,
236 /* 'x' */ 18, 0, /* 'z' */ 13
239 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
240 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
241 #define LABEL(Name) do_##Name
243 /* 'int' is enough and it saves some space on 64 bit systems. */
244 # define JUMP_TABLE_TYPE const int
245 # define JUMP_TABLE_BASE_LABEL do_form_unknown
246 # define REF(Name) &&do_##Name - &&JUMP_TABLE_BASE_LABEL
247 # define JUMP(ChExpr, table) \
253 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
254 : table[CHAR_CLASS (spec)]; \
255 ptr = &&JUMP_TABLE_BASE_LABEL + offset; \
260 # define JUMP_TABLE_TYPE const void *const
261 # define REF(Name) &&do_##Name
262 # define JUMP(ChExpr, table) \
267 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
268 : table[CHAR_CLASS (spec)]; \
274 #define STEP0_3_TABLE \
275 /* Step 0: at the beginning. */ \
276 static JUMP_TABLE_TYPE step0_jumps[30] = \
278 REF (form_unknown), \
279 REF (flag_space), /* for ' ' */ \
280 REF (flag_plus), /* for '+' */ \
281 REF (flag_minus), /* for '-' */ \
282 REF (flag_hash), /* for '<hash>' */ \
283 REF (flag_zero), /* for '0' */ \
284 REF (flag_quote), /* for '\'' */ \
285 REF (width_asterics), /* for '*' */ \
286 REF (width), /* for '1'...'9' */ \
287 REF (precision), /* for '.' */ \
288 REF (mod_half), /* for 'h' */ \
289 REF (mod_long), /* for 'l' */ \
290 REF (mod_longlong), /* for 'L', 'q' */ \
291 REF (mod_size_t), /* for 'z', 'Z' */ \
292 REF (form_percent), /* for '%' */ \
293 REF (form_integer), /* for 'd', 'i' */ \
294 REF (form_unsigned), /* for 'u' */ \
295 REF (form_octal), /* for 'o' */ \
296 REF (form_hexa), /* for 'X', 'x' */ \
297 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
298 REF (form_character), /* for 'c' */ \
299 REF (form_string), /* for 's', 'S' */ \
300 REF (form_pointer), /* for 'p' */ \
301 REF (form_number), /* for 'n' */ \
302 REF (form_strerror), /* for 'm' */ \
303 REF (form_wcharacter), /* for 'C' */ \
304 REF (form_floathex), /* for 'A', 'a' */ \
305 REF (mod_ptrdiff_t), /* for 't' */ \
306 REF (mod_intmax_t), /* for 'j' */ \
307 REF (flag_i18n), /* for 'I' */ \
309 /* Step 1: after processing width. */ \
310 static JUMP_TABLE_TYPE step1_jumps[30] = \
312 REF (form_unknown), \
313 REF (form_unknown), /* for ' ' */ \
314 REF (form_unknown), /* for '+' */ \
315 REF (form_unknown), /* for '-' */ \
316 REF (form_unknown), /* for '<hash>' */ \
317 REF (form_unknown), /* for '0' */ \
318 REF (form_unknown), /* for '\'' */ \
319 REF (form_unknown), /* for '*' */ \
320 REF (form_unknown), /* for '1'...'9' */ \
321 REF (precision), /* for '.' */ \
322 REF (mod_half), /* for 'h' */ \
323 REF (mod_long), /* for 'l' */ \
324 REF (mod_longlong), /* for 'L', 'q' */ \
325 REF (mod_size_t), /* for 'z', 'Z' */ \
326 REF (form_percent), /* for '%' */ \
327 REF (form_integer), /* for 'd', 'i' */ \
328 REF (form_unsigned), /* for 'u' */ \
329 REF (form_octal), /* for 'o' */ \
330 REF (form_hexa), /* for 'X', 'x' */ \
331 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
332 REF (form_character), /* for 'c' */ \
333 REF (form_string), /* for 's', 'S' */ \
334 REF (form_pointer), /* for 'p' */ \
335 REF (form_number), /* for 'n' */ \
336 REF (form_strerror), /* for 'm' */ \
337 REF (form_wcharacter), /* for 'C' */ \
338 REF (form_floathex), /* for 'A', 'a' */ \
339 REF (mod_ptrdiff_t), /* for 't' */ \
340 REF (mod_intmax_t), /* for 'j' */ \
341 REF (form_unknown) /* for 'I' */ \
343 /* Step 2: after processing precision. */ \
344 static JUMP_TABLE_TYPE step2_jumps[30] = \
346 REF (form_unknown), \
347 REF (form_unknown), /* for ' ' */ \
348 REF (form_unknown), /* for '+' */ \
349 REF (form_unknown), /* for '-' */ \
350 REF (form_unknown), /* for '<hash>' */ \
351 REF (form_unknown), /* for '0' */ \
352 REF (form_unknown), /* for '\'' */ \
353 REF (form_unknown), /* for '*' */ \
354 REF (form_unknown), /* for '1'...'9' */ \
355 REF (form_unknown), /* for '.' */ \
356 REF (mod_half), /* for 'h' */ \
357 REF (mod_long), /* for 'l' */ \
358 REF (mod_longlong), /* for 'L', 'q' */ \
359 REF (mod_size_t), /* for 'z', 'Z' */ \
360 REF (form_percent), /* for '%' */ \
361 REF (form_integer), /* for 'd', 'i' */ \
362 REF (form_unsigned), /* for 'u' */ \
363 REF (form_octal), /* for 'o' */ \
364 REF (form_hexa), /* for 'X', 'x' */ \
365 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
366 REF (form_character), /* for 'c' */ \
367 REF (form_string), /* for 's', 'S' */ \
368 REF (form_pointer), /* for 'p' */ \
369 REF (form_number), /* for 'n' */ \
370 REF (form_strerror), /* for 'm' */ \
371 REF (form_wcharacter), /* for 'C' */ \
372 REF (form_floathex), /* for 'A', 'a' */ \
373 REF (mod_ptrdiff_t), /* for 't' */ \
374 REF (mod_intmax_t), /* for 'j' */ \
375 REF (form_unknown) /* for 'I' */ \
377 /* Step 3a: after processing first 'h' modifier. */ \
378 static JUMP_TABLE_TYPE step3a_jumps[30] = \
380 REF (form_unknown), \
381 REF (form_unknown), /* for ' ' */ \
382 REF (form_unknown), /* for '+' */ \
383 REF (form_unknown), /* for '-' */ \
384 REF (form_unknown), /* for '<hash>' */ \
385 REF (form_unknown), /* for '0' */ \
386 REF (form_unknown), /* for '\'' */ \
387 REF (form_unknown), /* for '*' */ \
388 REF (form_unknown), /* for '1'...'9' */ \
389 REF (form_unknown), /* for '.' */ \
390 REF (mod_halfhalf), /* for 'h' */ \
391 REF (form_unknown), /* for 'l' */ \
392 REF (form_unknown), /* for 'L', 'q' */ \
393 REF (form_unknown), /* for 'z', 'Z' */ \
394 REF (form_percent), /* for '%' */ \
395 REF (form_integer), /* for 'd', 'i' */ \
396 REF (form_unsigned), /* for 'u' */ \
397 REF (form_octal), /* for 'o' */ \
398 REF (form_hexa), /* for 'X', 'x' */ \
399 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
400 REF (form_unknown), /* for 'c' */ \
401 REF (form_unknown), /* for 's', 'S' */ \
402 REF (form_unknown), /* for 'p' */ \
403 REF (form_number), /* for 'n' */ \
404 REF (form_unknown), /* for 'm' */ \
405 REF (form_unknown), /* for 'C' */ \
406 REF (form_unknown), /* for 'A', 'a' */ \
407 REF (form_unknown), /* for 't' */ \
408 REF (form_unknown), /* for 'j' */ \
409 REF (form_unknown) /* for 'I' */ \
411 /* Step 3b: after processing first 'l' modifier. */ \
412 static JUMP_TABLE_TYPE step3b_jumps[30] = \
414 REF (form_unknown), \
415 REF (form_unknown), /* for ' ' */ \
416 REF (form_unknown), /* for '+' */ \
417 REF (form_unknown), /* for '-' */ \
418 REF (form_unknown), /* for '<hash>' */ \
419 REF (form_unknown), /* for '0' */ \
420 REF (form_unknown), /* for '\'' */ \
421 REF (form_unknown), /* for '*' */ \
422 REF (form_unknown), /* for '1'...'9' */ \
423 REF (form_unknown), /* for '.' */ \
424 REF (form_unknown), /* for 'h' */ \
425 REF (mod_longlong), /* for 'l' */ \
426 REF (form_unknown), /* for 'L', 'q' */ \
427 REF (form_unknown), /* for 'z', 'Z' */ \
428 REF (form_percent), /* for '%' */ \
429 REF (form_integer), /* for 'd', 'i' */ \
430 REF (form_unsigned), /* for 'u' */ \
431 REF (form_octal), /* for 'o' */ \
432 REF (form_hexa), /* for 'X', 'x' */ \
433 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
434 REF (form_character), /* for 'c' */ \
435 REF (form_string), /* for 's', 'S' */ \
436 REF (form_pointer), /* for 'p' */ \
437 REF (form_number), /* for 'n' */ \
438 REF (form_strerror), /* for 'm' */ \
439 REF (form_wcharacter), /* for 'C' */ \
440 REF (form_floathex), /* for 'A', 'a' */ \
441 REF (form_unknown), /* for 't' */ \
442 REF (form_unknown), /* for 'j' */ \
443 REF (form_unknown) /* for 'I' */ \
446 #define STEP4_TABLE \
447 /* Step 4: processing format specifier. */ \
448 static JUMP_TABLE_TYPE step4_jumps[30] = \
450 REF (form_unknown), \
451 REF (form_unknown), /* for ' ' */ \
452 REF (form_unknown), /* for '+' */ \
453 REF (form_unknown), /* for '-' */ \
454 REF (form_unknown), /* for '<hash>' */ \
455 REF (form_unknown), /* for '0' */ \
456 REF (form_unknown), /* for '\'' */ \
457 REF (form_unknown), /* for '*' */ \
458 REF (form_unknown), /* for '1'...'9' */ \
459 REF (form_unknown), /* for '.' */ \
460 REF (form_unknown), /* for 'h' */ \
461 REF (form_unknown), /* for 'l' */ \
462 REF (form_unknown), /* for 'L', 'q' */ \
463 REF (form_unknown), /* for 'z', 'Z' */ \
464 REF (form_percent), /* for '%' */ \
465 REF (form_integer), /* for 'd', 'i' */ \
466 REF (form_unsigned), /* for 'u' */ \
467 REF (form_octal), /* for 'o' */ \
468 REF (form_hexa), /* for 'X', 'x' */ \
469 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
470 REF (form_character), /* for 'c' */ \
471 REF (form_string), /* for 's', 'S' */ \
472 REF (form_pointer), /* for 'p' */ \
473 REF (form_number), /* for 'n' */ \
474 REF (form_strerror), /* for 'm' */ \
475 REF (form_wcharacter), /* for 'C' */ \
476 REF (form_floathex), /* for 'A', 'a' */ \
477 REF (form_unknown), /* for 't' */ \
478 REF (form_unknown), /* for 'j' */ \
479 REF (form_unknown) /* for 'I' */ \
483 #define process_arg(fspec) \
484 /* Start real work. We know about all flags and modifiers and \
485 now process the wanted format specifier. */ \
486 LABEL (form_percent): \
487 /* Write a literal "%". */ \
491 LABEL (form_integer): \
492 /* Signed decimal integer. */ \
497 long long int signed_number; \
500 signed_number = va_arg (ap, long long int); \
502 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
504 is_negative = signed_number < 0; \
505 number.longlong = is_negative ? (- signed_number) : signed_number; \
507 goto LABEL (longlong_number); \
511 long int signed_number; \
516 signed_number = va_arg (ap, long int); \
518 signed_number = (signed char) va_arg (ap, unsigned int); \
519 else if (!is_short) \
520 signed_number = va_arg (ap, int); \
522 signed_number = (short int) va_arg (ap, unsigned int); \
526 signed_number = args_value[fspec->data_arg].pa_long_int; \
528 signed_number = (signed char) \
529 args_value[fspec->data_arg].pa_u_int; \
530 else if (!is_short) \
531 signed_number = args_value[fspec->data_arg].pa_int; \
533 signed_number = (short int) \
534 args_value[fspec->data_arg].pa_u_int; \
536 is_negative = signed_number < 0; \
537 number.word = is_negative ? (- signed_number) : signed_number; \
539 goto LABEL (number); \
543 LABEL (form_unsigned): \
544 /* Unsigned decimal integer. */ \
546 goto LABEL (unsigned_number); \
549 LABEL (form_octal): \
550 /* Unsigned octal integer. */ \
552 goto LABEL (unsigned_number); \
556 /* Unsigned hexadecimal integer. */ \
559 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
561 /* ISO specifies the `+' and ` ' flags only for signed \
570 number.longlong = va_arg (ap, unsigned long long int); \
572 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
574 LABEL (longlong_number): \
576 /* Supply a default precision if none was given. */ \
579 /* We have to take care for the '0' flag. If a precision \
580 is given it must be ignored. */ \
583 /* If the precision is 0 and the number is 0 nothing has to \
584 be written for the number, except for the 'o' format in \
586 if (prec == 0 && number.longlong == 0) \
589 if (base == 8 && alt) \
590 *--string = L_('0'); \
594 /* Put the number in WORK. */ \
595 string = _itoa (number.longlong, workend, base, \
597 if (group && grouping) \
598 string = group_number (work_buffer, string, workend, \
599 grouping, thousands_sep); \
600 if (use_outdigits && base == 10) \
601 string = _i18n_number_rewrite (string, workend, workend); \
603 /* Simplify further test for num != 0. */ \
604 number.word = number.longlong != 0; \
611 number.word = va_arg (ap, unsigned long int); \
613 number.word = (unsigned char) va_arg (ap, unsigned int); \
614 else if (!is_short) \
615 number.word = va_arg (ap, unsigned int); \
617 number.word = (unsigned short int) va_arg (ap, unsigned int); \
621 number.word = args_value[fspec->data_arg].pa_u_long_int; \
623 number.word = (unsigned char) \
624 args_value[fspec->data_arg].pa_u_int; \
625 else if (!is_short) \
626 number.word = args_value[fspec->data_arg].pa_u_int; \
628 number.word = (unsigned short int) \
629 args_value[fspec->data_arg].pa_u_int; \
633 /* Supply a default precision if none was given. */ \
636 /* We have to take care for the '0' flag. If a precision \
637 is given it must be ignored. */ \
640 /* If the precision is 0 and the number is 0 nothing has to \
641 be written for the number, except for the 'o' format in \
643 if (prec == 0 && number.word == 0) \
646 if (base == 8 && alt) \
647 *--string = L_('0'); \
651 /* Put the number in WORK. */ \
652 string = _itoa_word (number.word, workend, base, \
654 if (group && grouping) \
655 string = group_number (work_buffer, string, workend, \
656 grouping, thousands_sep); \
657 if (use_outdigits && base == 10) \
658 string = _i18n_number_rewrite (string, workend, workend); \
662 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
663 /* Add octal marker. */ \
664 *--string = L_('0'); \
666 prec = MAX (0, prec - (workend - string)); \
670 width -= workend - string + prec; \
672 if (number.word != 0 && alt && base == 16) \
673 /* Account for 0X hex marker. */ \
676 if (is_negative || showsign || space) \
679 if (pad == L_(' ')) \
692 if (number.word != 0 && alt && base == 16) \
701 outstring (string, workend - string); \
723 if (number.word != 0 && alt && base == 16) \
730 width -= workend - string + prec; \
740 outstring (string, workend - string); \
746 LABEL (form_float): \
748 /* Floating-point number. This is handled by printf_fp.c. */ \
755 is_long_double = 0; \
757 struct printf_info info = { .prec = prec, \
760 .is_long_double = is_long_double, \
761 .is_short = is_short, \
762 .is_long = is_long, \
766 .showsign = showsign, \
770 .i18n = use_outdigits, \
771 .wide = sizeof (CHAR_T) != 1, \
772 .is_binary128 = 0}; \
774 if (is_long_double) \
775 the_arg.pa_long_double = va_arg (ap, long double); \
777 the_arg.pa_double = va_arg (ap, double); \
778 ptr = (const void *) &the_arg; \
780 function_done = __printf_fp (s, &info, &ptr); \
784 ptr = (const void *) &args_value[fspec->data_arg]; \
787 fspec->data_arg_type = PA_DOUBLE; \
788 fspec->info.is_long_double = 0; \
790 /* Not supported by *printf functions. */ \
791 fspec->info.is_binary128 = 0; \
793 function_done = __printf_fp (s, &fspec->info, &ptr); \
796 if (function_done < 0) \
798 /* Error in print handler; up to handler to set errno. */ \
803 done_add (function_done); \
807 LABEL (form_floathex): \
809 /* Floating point number printed as hexadecimal number. */ \
816 is_long_double = 0; \
818 struct printf_info info = { .prec = prec, \
821 .is_long_double = is_long_double, \
822 .is_short = is_short, \
823 .is_long = is_long, \
827 .showsign = showsign, \
831 .wide = sizeof (CHAR_T) != 1, \
832 .is_binary128 = 0}; \
834 if (is_long_double) \
835 the_arg.pa_long_double = va_arg (ap, long double); \
837 the_arg.pa_double = va_arg (ap, double); \
838 ptr = (const void *) &the_arg; \
840 function_done = __printf_fphex (s, &info, &ptr); \
844 ptr = (const void *) &args_value[fspec->data_arg]; \
846 fspec->info.is_long_double = 0; \
847 /* Not supported by *printf functions. */ \
848 fspec->info.is_binary128 = 0; \
850 function_done = __printf_fphex (s, &fspec->info, &ptr); \
853 if (function_done < 0) \
855 /* Error in print handler; up to handler to set errno. */ \
860 done_add (function_done); \
864 LABEL (form_pointer): \
865 /* Generic pointer. */ \
869 ptr = va_arg (ap, void *); \
871 ptr = args_value[fspec->data_arg].pa_pointer; \
874 /* If the pointer is not NULL, write it as a %#x spec. */ \
876 number.word = (unsigned long int) ptr; \
881 goto LABEL (number); \
885 /* Write "(nil)" for a nil pointer. */ \
886 string = (CHAR_T *) L_("(nil)"); \
887 /* Make sure the full string "(nil)" is printed. */ \
890 /* This is a wide string iff compiling wprintf. */ \
891 is_long = sizeof (CHAR_T) > 1; \
892 goto LABEL (print_string); \
897 LABEL (form_number): \
898 if (s->_flags2 & _IO_FLAGS2_FORTIFY) \
900 if (! readonly_format) \
902 extern int __readonly_area (const void *, size_t) \
905 = __readonly_area (format, ((STR_LEN (format) + 1) \
906 * sizeof (CHAR_T))); \
908 if (readonly_format < 0) \
909 __libc_fatal ("*** %n in writable segment detected ***\n"); \
911 /* Answer the count of characters written. */ \
915 *(long long int *) va_arg (ap, void *) = done; \
916 else if (is_long_num) \
917 *(long int *) va_arg (ap, void *) = done; \
919 *(char *) va_arg (ap, void *) = done; \
920 else if (!is_short) \
921 *(int *) va_arg (ap, void *) = done; \
923 *(short int *) va_arg (ap, void *) = done; \
927 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
928 else if (is_long_num) \
929 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
931 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
932 else if (!is_short) \
933 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
935 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
938 LABEL (form_strerror): \
939 /* Print description of error ERRNO. */ \
941 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
942 WORK_BUFFER_SIZE * sizeof (CHAR_T)); \
943 is_long = 0; /* This is no wide-char string. */ \
944 goto LABEL (print_string)
946 #ifdef COMPILE_WPRINTF
947 # define process_string_arg(fspec) \
948 LABEL (form_character): \
951 goto LABEL (form_wcharacter); \
952 --width; /* Account for the character itself. */ \
956 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
958 outchar (__btowc ((unsigned char) \
959 args_value[fspec->data_arg].pa_int)); \
964 LABEL (form_wcharacter): \
966 /* Wide character. */ \
971 outchar (va_arg (ap, wchar_t)); \
973 outchar (args_value[fspec->data_arg].pa_wchar); \
979 LABEL (form_string): \
982 int string_malloced; \
984 /* The string argument could in fact be `char *' or `wchar_t *'. \
985 But this should not make a difference here. */ \
987 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
989 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
991 /* Entry point for printing other strings. */ \
992 LABEL (print_string): \
994 string_malloced = 0; \
995 if (string == NULL) \
997 /* Write "(null)" if there's space. */ \
999 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
1001 string = (CHAR_T *) null; \
1002 len = (sizeof (null) / sizeof (null[0])) - 1; \
1006 string = (CHAR_T *) L""; \
1010 else if (!is_long && spec != L_('S')) \
1012 /* This is complicated. We have to transform the multibyte \
1013 string into a wide character string. */ \
1014 const char *mbs = (const char *) string; \
1015 mbstate_t mbstate; \
1017 len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
1019 /* Allocate dynamically an array which definitely is long \
1020 enough for the wide character version. Each byte in the \
1021 multi-byte string can produce at most one wide character. */ \
1022 if (__glibc_unlikely (len > SIZE_MAX / sizeof (wchar_t))) \
1024 __set_errno (EOVERFLOW); \
1028 else if (__libc_use_alloca (len * sizeof (wchar_t))) \
1029 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1030 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1037 string_malloced = 1; \
1039 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1040 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1041 if (len == (size_t) -1) \
1043 /* Illegal multibyte character. */ \
1051 /* Search for the end of the string, but don't search past \
1052 the length specified by the precision. */ \
1053 len = __wcsnlen (string, prec); \
1055 len = __wcslen (string); \
1058 if ((width -= len) < 0) \
1060 outstring (string, len); \
1066 outstring (string, len); \
1069 if (__glibc_unlikely (string_malloced)) \
1074 # define process_string_arg(fspec) \
1075 LABEL (form_character): \
1078 goto LABEL (form_wcharacter); \
1079 --width; /* Account for the character itself. */ \
1082 if (fspec == NULL) \
1083 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1085 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1090 LABEL (form_wcharacter): \
1092 /* Wide character. */ \
1093 char buf[MB_LEN_MAX]; \
1094 mbstate_t mbstate; \
1097 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1098 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1099 : args_value[fspec->data_arg].pa_wchar), \
1101 if (len == (size_t) -1) \
1103 /* Something went wrong during the conversion. Bail out. */ \
1110 outstring (buf, len); \
1116 LABEL (form_string): \
1119 int string_malloced; \
1121 /* The string argument could in fact be `char *' or `wchar_t *'. \
1122 But this should not make a difference here. */ \
1123 if (fspec == NULL) \
1124 string = (char *) va_arg (ap, const char *); \
1126 string = (char *) args_value[fspec->data_arg].pa_string; \
1128 /* Entry point for printing other strings. */ \
1129 LABEL (print_string): \
1131 string_malloced = 0; \
1132 if (string == NULL) \
1134 /* Write "(null)" if there's space. */ \
1135 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1137 string = (char *) null; \
1138 len = sizeof (null) - 1; \
1142 string = (char *) ""; \
1146 else if (!is_long && spec != L_('S')) \
1149 /* Search for the end of the string, but don't search past \
1150 the length (in bytes) specified by the precision. */ \
1151 len = __strnlen (string, prec); \
1153 len = strlen (string); \
1157 const wchar_t *s2 = (const wchar_t *) string; \
1158 mbstate_t mbstate; \
1160 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1164 /* The string `s2' might not be NUL terminated. */ \
1165 if (__libc_use_alloca (prec)) \
1166 string = (char *) alloca (prec); \
1167 else if ((string = (char *) malloc (prec)) == NULL) \
1173 string_malloced = 1; \
1174 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1178 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1179 if (len != (size_t) -1) \
1181 assert (__mbsinit (&mbstate)); \
1182 s2 = (const wchar_t *) string; \
1183 if (__libc_use_alloca (len + 1)) \
1184 string = (char *) alloca (len + 1); \
1185 else if ((string = (char *) malloc (len + 1)) == NULL) \
1191 string_malloced = 1; \
1192 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1196 if (len == (size_t) -1) \
1198 /* Illegal wide-character string. */ \
1204 if ((width -= len) < 0) \
1206 outstring (string, len); \
1212 outstring (string, len); \
1215 if (__glibc_unlikely (string_malloced)) \
1221 /* Helper function to provide temporary buffering for unbuffered streams. */
1222 static int buffered_vfprintf (FILE *stream
, const CHAR_T
*fmt
, va_list)
1223 __THROW
__attribute__ ((noinline
));
1225 /* Handle positional format specifiers. */
1226 static int printf_positional (_IO_FILE
*s
,
1227 const CHAR_T
*format
, int readonly_format
,
1228 va_list ap
, va_list *ap_savep
, int done
,
1229 int nspecs_done
, const UCHAR_T
*lead_str_end
,
1230 CHAR_T
*work_buffer
, int save_errno
,
1231 const char *grouping
, THOUSANDS_SEP_T
);
1233 /* Handle unknown format specifier. */
1234 static int printf_unknown (FILE *, const struct printf_info
*,
1235 const void *const *) __THROW
;
1237 /* Group digits of number string. */
1238 static CHAR_T
*group_number (CHAR_T
*, CHAR_T
*, CHAR_T
*, const char *,
1241 /* The function itself. */
1243 vfprintf (FILE *s
, const CHAR_T
*format
, va_list ap
)
1245 /* The character used as thousands separator. */
1246 THOUSANDS_SEP_T thousands_sep
= 0;
1248 /* The string describing the size of groups of digits. */
1249 const char *grouping
;
1251 /* Place to accumulate the result. */
1254 /* Current character in format string. */
1257 /* End of leading constant string. */
1258 const UCHAR_T
*lead_str_end
;
1260 /* Points to next format specifier. */
1261 const UCHAR_T
*end_of_spec
;
1263 /* Buffer intermediate results. */
1264 CHAR_T work_buffer
[WORK_BUFFER_SIZE
];
1265 CHAR_T
*workstart
= NULL
;
1268 /* We have to save the original argument pointer. */
1271 /* Count number of specifiers we already processed. */
1274 /* For the %m format we may need the current `errno' value. */
1275 int save_errno
= errno
;
1277 /* 1 if format is in read-only memory, -1 if it is in writable memory,
1279 int readonly_format
= 0;
1281 /* Orient the stream. */
1286 /* Sanity check of arguments. */
1287 ARGCHECK (s
, format
);
1290 /* Check for correct orientation. */
1291 if (_IO_vtable_offset (s
) == 0 &&
1292 _IO_fwide (s
, sizeof (CHAR_T
) == 1 ? -1 : 1)
1293 != (sizeof (CHAR_T
) == 1 ? -1 : 1))
1294 /* The stream is already oriented otherwise. */
1298 if (UNBUFFERED_P (s
))
1299 /* Use a helper function which will allocate a local temporary buffer
1300 for the stream and then call us again. */
1301 return buffered_vfprintf (s
, format
, ap
);
1303 /* Initialize local variables. */
1305 grouping
= (const char *) -1;
1307 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1308 since on some systems `va_list' is not an integral type. */
1309 __va_copy (ap_save
, ap
);
1315 #ifdef COMPILE_WPRINTF
1316 /* Find the first format specifier. */
1317 f
= lead_str_end
= __find_specwc ((const UCHAR_T
*) format
);
1319 /* Find the first format specifier. */
1320 f
= lead_str_end
= __find_specmb ((const UCHAR_T
*) format
);
1324 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile
, s
);
1327 /* Write the literal text before the first format. */
1328 outstring ((const UCHAR_T
*) format
,
1329 lead_str_end
- (const UCHAR_T
*) format
);
1331 /* If we only have to print a simple string, return now. */
1335 /* Use the slow path in case any printf handler is registered. */
1336 if (__glibc_unlikely (__printf_function_table
!= NULL
1337 || __printf_modifier_table
!= NULL
1338 || __printf_va_arg_table
!= NULL
))
1341 /* Process whole format string. */
1347 union printf_arg
*args_value
; /* This is not used here but ... */
1348 int is_negative
; /* Flag for negative number. */
1351 unsigned long long int longlong
;
1352 unsigned long int word
;
1355 union printf_arg the_arg
;
1356 CHAR_T
*string
; /* Pointer to argument string. */
1357 int alt
= 0; /* Alternate format. */
1358 int space
= 0; /* Use space prefix if no sign is needed. */
1359 int left
= 0; /* Left-justify output. */
1360 int showsign
= 0; /* Always begin with plus or minus sign. */
1361 int group
= 0; /* Print numbers according grouping rules. */
1362 int is_long_double
= 0; /* Argument is long double/ long long int. */
1363 int is_short
= 0; /* Argument is short int. */
1364 int is_long
= 0; /* Argument is long int. */
1365 int is_char
= 0; /* Argument is promoted (unsigned) char. */
1366 int width
= 0; /* Width of output; 0 means none specified. */
1367 int prec
= -1; /* Precision of output; -1 means none specified. */
1368 /* This flag is set by the 'I' modifier and selects the use of the
1369 `outdigits' as determined by the current locale. */
1370 int use_outdigits
= 0;
1371 UCHAR_T pad
= L_(' ');/* Padding character. */
1375 workend
= work_buffer
+ WORK_BUFFER_SIZE
;
1377 /* Get current character in format string. */
1378 JUMP (*++f
, step0_jumps
);
1383 JUMP (*++f
, step0_jumps
);
1388 JUMP (*++f
, step0_jumps
);
1394 JUMP (*++f
, step0_jumps
);
1399 JUMP (*++f
, step0_jumps
);
1405 JUMP (*++f
, step0_jumps
);
1407 /* The '\'' flag. */
1411 if (grouping
== (const char *) -1)
1413 #ifdef COMPILE_WPRINTF
1414 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1415 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1417 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1420 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1421 if (*grouping
== '\0' || *grouping
== CHAR_MAX
1422 #ifdef COMPILE_WPRINTF
1423 || thousands_sep
== L
'\0'
1425 || *thousands_sep
== '\0'
1430 JUMP (*++f
, step0_jumps
);
1434 JUMP (*++f
, step0_jumps
);
1436 /* Get width from argument. */
1437 LABEL (width_asterics
):
1439 const UCHAR_T
*tmp
; /* Temporary value. */
1444 int pos
= read_int (&tmp
);
1448 __set_errno (EOVERFLOW
);
1453 if (pos
&& *tmp
== L_('$'))
1454 /* The width comes from a positional parameter. */
1457 width
= va_arg (ap
, int);
1459 /* Negative width means left justified. */
1467 if (__glibc_unlikely (width
>= INT_MAX
/ sizeof (CHAR_T
) - EXTSIZ
))
1469 __set_errno (EOVERFLOW
);
1474 if (width
>= WORK_BUFFER_SIZE
- EXTSIZ
)
1476 /* We have to use a special buffer. */
1477 size_t needed
= ((size_t) width
+ EXTSIZ
) * sizeof (CHAR_T
);
1478 if (__libc_use_alloca (needed
))
1479 workend
= (CHAR_T
*) alloca (needed
) + width
+ EXTSIZ
;
1482 workstart
= (CHAR_T
*) malloc (needed
);
1483 if (workstart
== NULL
)
1488 workend
= workstart
+ width
+ EXTSIZ
;
1492 JUMP (*f
, step1_jumps
);
1494 /* Given width in format string. */
1496 width
= read_int (&f
);
1498 if (__glibc_unlikely (width
== -1
1499 || width
>= INT_MAX
/ sizeof (CHAR_T
) - EXTSIZ
))
1501 __set_errno (EOVERFLOW
);
1506 if (width
>= WORK_BUFFER_SIZE
- EXTSIZ
)
1508 /* We have to use a special buffer. */
1509 size_t needed
= ((size_t) width
+ EXTSIZ
) * sizeof (CHAR_T
);
1510 if (__libc_use_alloca (needed
))
1511 workend
= (CHAR_T
*) alloca (needed
) + width
+ EXTSIZ
;
1514 workstart
= (CHAR_T
*) malloc (needed
);
1515 if (workstart
== NULL
)
1520 workend
= workstart
+ width
+ EXTSIZ
;
1524 /* Oh, oh. The argument comes from a positional parameter. */
1526 JUMP (*f
, step1_jumps
);
1532 const UCHAR_T
*tmp
; /* Temporary value. */
1537 int pos
= read_int (&tmp
);
1541 __set_errno (EOVERFLOW
);
1546 if (pos
&& *tmp
== L_('$'))
1547 /* The precision comes from a positional parameter. */
1550 prec
= va_arg (ap
, int);
1552 /* If the precision is negative the precision is omitted. */
1556 else if (ISDIGIT (*f
))
1558 prec
= read_int (&f
);
1560 /* The precision was specified in this case as an extremely
1561 large positive value. */
1564 __set_errno (EOVERFLOW
);
1571 if (prec
> width
&& prec
> WORK_BUFFER_SIZE
- EXTSIZ
)
1573 /* Deallocate any previously allocated buffer because it is
1575 if (__glibc_unlikely (workstart
!= NULL
))
1578 if (__glibc_unlikely (prec
>= INT_MAX
/ sizeof (CHAR_T
) - EXTSIZ
))
1580 __set_errno (EOVERFLOW
);
1584 size_t needed
= ((size_t) prec
+ EXTSIZ
) * sizeof (CHAR_T
);
1586 if (__libc_use_alloca (needed
))
1587 workend
= (CHAR_T
*) alloca (needed
) + prec
+ EXTSIZ
;
1590 workstart
= (CHAR_T
*) malloc (needed
);
1591 if (workstart
== NULL
)
1596 workend
= workstart
+ prec
+ EXTSIZ
;
1599 JUMP (*f
, step2_jumps
);
1601 /* Process 'h' modifier. There might another 'h' following. */
1604 JUMP (*++f
, step3a_jumps
);
1606 /* Process 'hh' modifier. */
1607 LABEL (mod_halfhalf
):
1610 JUMP (*++f
, step4_jumps
);
1612 /* Process 'l' modifier. There might another 'l' following. */
1615 JUMP (*++f
, step3b_jumps
);
1617 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1618 allowed to follow. */
1619 LABEL (mod_longlong
):
1622 JUMP (*++f
, step4_jumps
);
1625 is_long_double
= sizeof (size_t) > sizeof (unsigned long int);
1626 is_long
= sizeof (size_t) > sizeof (unsigned int);
1627 JUMP (*++f
, step4_jumps
);
1629 LABEL (mod_ptrdiff_t
):
1630 is_long_double
= sizeof (ptrdiff_t) > sizeof (unsigned long int);
1631 is_long
= sizeof (ptrdiff_t) > sizeof (unsigned int);
1632 JUMP (*++f
, step4_jumps
);
1634 LABEL (mod_intmax_t
):
1635 is_long_double
= sizeof (intmax_t) > sizeof (unsigned long int);
1636 is_long
= sizeof (intmax_t) > sizeof (unsigned int);
1637 JUMP (*++f
, step4_jumps
);
1639 /* Process current format. */
1642 process_arg (((struct printf_spec
*) NULL
));
1643 process_string_arg (((struct printf_spec
*) NULL
));
1645 LABEL (form_unknown
):
1646 if (spec
== L_('\0'))
1648 /* The format string ended before the specifier is complete. */
1649 __set_errno (EINVAL
);
1654 /* If we are in the fast loop force entering the complicated
1659 /* The format is correctly handled. */
1662 if (__glibc_unlikely (workstart
!= NULL
))
1666 /* Look for next format specifier. */
1667 #ifdef COMPILE_WPRINTF
1668 f
= __find_specwc ((end_of_spec
= ++f
));
1670 f
= __find_specmb ((end_of_spec
= ++f
));
1673 /* Write the following constant string. */
1674 outstring (end_of_spec
, f
- end_of_spec
);
1676 while (*f
!= L_('\0'));
1678 /* Unlock stream and return. */
1681 /* Hand off processing for positional parameters. */
1683 if (__glibc_unlikely (workstart
!= NULL
))
1688 done
= printf_positional (s
, format
, readonly_format
, ap
, &ap_save
,
1689 done
, nspecs_done
, lead_str_end
, work_buffer
,
1690 save_errno
, grouping
, thousands_sep
);
1693 if (__glibc_unlikely (workstart
!= NULL
))
1695 /* Unlock the stream. */
1696 _IO_funlockfile (s
);
1697 _IO_cleanup_region_end (0);
1703 printf_positional (_IO_FILE
*s
, const CHAR_T
*format
, int readonly_format
,
1704 va_list ap
, va_list *ap_savep
, int done
, int nspecs_done
,
1705 const UCHAR_T
*lead_str_end
,
1706 CHAR_T
*work_buffer
, int save_errno
,
1707 const char *grouping
, THOUSANDS_SEP_T thousands_sep
)
1709 /* For positional argument handling. */
1710 struct scratch_buffer specsbuf
;
1711 scratch_buffer_init (&specsbuf
);
1712 struct printf_spec
*specs
= specsbuf
.data
;
1713 size_t specs_limit
= specsbuf
.length
/ sizeof (specs
[0]);
1715 /* Used as a backing store for args_value, args_size, args_type
1717 struct scratch_buffer argsbuf
;
1718 scratch_buffer_init (&argsbuf
);
1720 /* Array with information about the needed arguments. This has to
1721 be dynamically extensible. */
1724 /* The number of arguments the format string requests. This will
1725 determine the size of the array needed to store the argument
1729 /* Positional parameters refer to arguments directly. This could
1730 also determine the maximum number of arguments. Track the
1732 size_t max_ref_arg
= 0;
1734 /* Just a counter. */
1737 CHAR_T
*workstart
= NULL
;
1739 if (grouping
== (const char *) -1)
1741 #ifdef COMPILE_WPRINTF
1742 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1743 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1745 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1748 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1749 if (*grouping
== '\0' || *grouping
== CHAR_MAX
)
1753 for (const UCHAR_T
*f
= lead_str_end
; *f
!= L_('\0');
1754 f
= specs
[nspecs
++].next_fmt
)
1756 if (nspecs
== specs_limit
)
1758 if (!scratch_buffer_grow_preserve (&specsbuf
))
1763 specs
= specsbuf
.data
;
1764 specs_limit
= specsbuf
.length
/ sizeof (specs
[0]);
1767 /* Parse the format specifier. */
1768 #ifdef COMPILE_WPRINTF
1769 nargs
+= __parse_one_specwc (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1771 nargs
+= __parse_one_specmb (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1775 /* Determine the number of arguments the format string consumes. */
1776 nargs
= MAX (nargs
, max_ref_arg
);
1778 union printf_arg
*args_value
;
1782 /* Calculate total size needed to represent a single argument
1783 across all three argument-related arrays. */
1784 size_t bytes_per_arg
1785 = sizeof (*args_value
) + sizeof (*args_size
) + sizeof (*args_type
);
1786 if (!scratch_buffer_set_array_size (&argsbuf
, nargs
, bytes_per_arg
))
1791 args_value
= argsbuf
.data
;
1792 /* Set up the remaining two arrays to each point past the end of
1793 the prior array, since space for all three has been allocated
1795 args_size
= &args_value
[nargs
].pa_int
;
1796 args_type
= &args_size
[nargs
];
1797 memset (args_type
, s
->_flags2
& _IO_FLAGS2_FORTIFY
? '\xff' : '\0',
1798 nargs
* sizeof (*args_type
));
1801 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1802 still zero after this loop, format is invalid. For now we
1803 simply use 0 as the value. */
1805 /* Fill in the types of all the arguments. */
1806 for (cnt
= 0; cnt
< nspecs
; ++cnt
)
1808 /* If the width is determined by an argument this is an int. */
1809 if (specs
[cnt
].width_arg
!= -1)
1810 args_type
[specs
[cnt
].width_arg
] = PA_INT
;
1812 /* If the precision is determined by an argument this is an int. */
1813 if (specs
[cnt
].prec_arg
!= -1)
1814 args_type
[specs
[cnt
].prec_arg
] = PA_INT
;
1816 switch (specs
[cnt
].ndata_args
)
1818 case 0: /* No arguments. */
1820 case 1: /* One argument; we already have the
1822 args_type
[specs
[cnt
].data_arg
] = specs
[cnt
].data_arg_type
;
1823 args_size
[specs
[cnt
].data_arg
] = specs
[cnt
].size
;
1826 /* We have more than one argument for this format spec.
1827 We must call the arginfo function again to determine
1829 (void) (*__printf_arginfo_table
[specs
[cnt
].info
.spec
])
1831 specs
[cnt
].ndata_args
, &args_type
[specs
[cnt
].data_arg
],
1832 &args_size
[specs
[cnt
].data_arg
]);
1837 /* Now we know all the types and the order. Fill in the argument
1839 for (cnt
= 0; cnt
< nargs
; ++cnt
)
1840 switch (args_type
[cnt
])
1842 #define T(tag, mem, type) \
1844 args_value[cnt].mem = va_arg (*ap_savep, type); \
1847 T (PA_WCHAR
, pa_wchar
, wint_t);
1848 case PA_CHAR
: /* Promoted. */
1849 case PA_INT
|PA_FLAG_SHORT
: /* Promoted. */
1850 #if LONG_MAX == INT_MAX
1851 case PA_INT
|PA_FLAG_LONG
:
1853 T (PA_INT
, pa_int
, int);
1854 #if LONG_MAX == LONG_LONG_MAX
1855 case PA_INT
|PA_FLAG_LONG
:
1857 T (PA_INT
|PA_FLAG_LONG_LONG
, pa_long_long_int
, long long int);
1858 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1861 case PA_FLOAT
: /* Promoted. */
1862 T (PA_DOUBLE
, pa_double
, double);
1863 case PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
:
1866 args_value
[cnt
].pa_double
= va_arg (*ap_savep
, double);
1867 args_type
[cnt
] &= ~PA_FLAG_LONG_DOUBLE
;
1870 args_value
[cnt
].pa_long_double
= va_arg (*ap_savep
, long double);
1872 case PA_STRING
: /* All pointers are the same */
1873 case PA_WSTRING
: /* All pointers are the same */
1874 T (PA_POINTER
, pa_pointer
, void *);
1877 if ((args_type
[cnt
] & PA_FLAG_PTR
) != 0)
1878 args_value
[cnt
].pa_pointer
= va_arg (*ap_savep
, void *);
1879 else if (__glibc_unlikely (__printf_va_arg_table
!= NULL
)
1880 && __printf_va_arg_table
[args_type
[cnt
] - PA_LAST
] != NULL
)
1882 args_value
[cnt
].pa_user
= alloca (args_size
[cnt
]);
1883 (*__printf_va_arg_table
[args_type
[cnt
] - PA_LAST
])
1884 (args_value
[cnt
].pa_user
, ap_savep
);
1887 args_value
[cnt
].pa_long_double
= 0.0;
1890 /* Error case. Not all parameters appear in N$ format
1891 strings. We have no way to determine their type. */
1892 assert (s
->_flags2
& _IO_FLAGS2_FORTIFY
);
1893 __libc_fatal ("*** invalid %N$ use detected ***\n");
1896 /* Now walk through all format specifiers and process them. */
1897 for (; (size_t) nspecs_done
< nspecs
; ++nspecs_done
)
1904 unsigned long long int longlong
;
1905 unsigned long int word
;
1908 union printf_arg the_arg
;
1909 CHAR_T
*string
; /* Pointer to argument string. */
1911 /* Fill variables from values in struct. */
1912 int alt
= specs
[nspecs_done
].info
.alt
;
1913 int space
= specs
[nspecs_done
].info
.space
;
1914 int left
= specs
[nspecs_done
].info
.left
;
1915 int showsign
= specs
[nspecs_done
].info
.showsign
;
1916 int group
= specs
[nspecs_done
].info
.group
;
1917 int is_long_double
= specs
[nspecs_done
].info
.is_long_double
;
1918 int is_short
= specs
[nspecs_done
].info
.is_short
;
1919 int is_char
= specs
[nspecs_done
].info
.is_char
;
1920 int is_long
= specs
[nspecs_done
].info
.is_long
;
1921 int width
= specs
[nspecs_done
].info
.width
;
1922 int prec
= specs
[nspecs_done
].info
.prec
;
1923 int use_outdigits
= specs
[nspecs_done
].info
.i18n
;
1924 char pad
= specs
[nspecs_done
].info
.pad
;
1925 CHAR_T spec
= specs
[nspecs_done
].info
.spec
;
1928 CHAR_T
*workend
= work_buffer
+ WORK_BUFFER_SIZE
;
1930 /* Fill in last information. */
1931 if (specs
[nspecs_done
].width_arg
!= -1)
1933 /* Extract the field width from an argument. */
1934 specs
[nspecs_done
].info
.width
=
1935 args_value
[specs
[nspecs_done
].width_arg
].pa_int
;
1937 if (specs
[nspecs_done
].info
.width
< 0)
1938 /* If the width value is negative left justification is
1939 selected and the value is taken as being positive. */
1941 specs
[nspecs_done
].info
.width
*= -1;
1942 left
= specs
[nspecs_done
].info
.left
= 1;
1944 width
= specs
[nspecs_done
].info
.width
;
1947 if (specs
[nspecs_done
].prec_arg
!= -1)
1949 /* Extract the precision from an argument. */
1950 specs
[nspecs_done
].info
.prec
=
1951 args_value
[specs
[nspecs_done
].prec_arg
].pa_int
;
1953 if (specs
[nspecs_done
].info
.prec
< 0)
1954 /* If the precision is negative the precision is
1956 specs
[nspecs_done
].info
.prec
= -1;
1958 prec
= specs
[nspecs_done
].info
.prec
;
1961 /* Maybe the buffer is too small. */
1962 if (MAX (prec
, width
) + EXTSIZ
> WORK_BUFFER_SIZE
)
1964 if (__libc_use_alloca ((MAX (prec
, width
) + EXTSIZ
)
1966 workend
= ((CHAR_T
*) alloca ((MAX (prec
, width
) + EXTSIZ
)
1968 + (MAX (prec
, width
) + EXTSIZ
));
1971 workstart
= (CHAR_T
*) malloc ((MAX (prec
, width
) + EXTSIZ
)
1973 if (workstart
== NULL
)
1978 workend
= workstart
+ (MAX (prec
, width
) + EXTSIZ
);
1982 /* Process format specifiers. */
1985 extern printf_function
**__printf_function_table
;
1988 if (spec
<= UCHAR_MAX
1989 && __printf_function_table
!= NULL
1990 && __printf_function_table
[(size_t) spec
] != NULL
)
1992 const void **ptr
= alloca (specs
[nspecs_done
].ndata_args
1993 * sizeof (const void *));
1995 /* Fill in an array of pointers to the argument values. */
1996 for (unsigned int i
= 0; i
< specs
[nspecs_done
].ndata_args
;
1998 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
2000 /* Call the function. */
2001 function_done
= __printf_function_table
[(size_t) spec
]
2002 (s
, &specs
[nspecs_done
].info
, ptr
);
2004 if (function_done
!= -2)
2006 /* If an error occurred we don't have information
2007 about # of chars. */
2008 if (function_done
< 0)
2010 /* Function has set errno. */
2015 done_add (function_done
);
2020 JUMP (spec
, step4_jumps
);
2022 process_arg ((&specs
[nspecs_done
]));
2023 process_string_arg ((&specs
[nspecs_done
]));
2025 LABEL (form_unknown
):
2030 ptr
= alloca (specs
[nspecs_done
].ndata_args
2031 * sizeof (const void *));
2033 /* Fill in an array of pointers to the argument values. */
2034 for (i
= 0; i
< specs
[nspecs_done
].ndata_args
; ++i
)
2035 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
2037 /* Call the function. */
2038 function_done
= printf_unknown (s
, &specs
[nspecs_done
].info
,
2041 /* If an error occurred we don't have information about #
2043 if (function_done
< 0)
2045 /* Function has set errno. */
2050 done_add (function_done
);
2055 if (__glibc_unlikely (workstart
!= NULL
))
2059 /* Write the following constant string. */
2060 outstring (specs
[nspecs_done
].end_of_fmt
,
2061 specs
[nspecs_done
].next_fmt
2062 - specs
[nspecs_done
].end_of_fmt
);
2065 if (__glibc_unlikely (workstart
!= NULL
))
2067 scratch_buffer_free (&argsbuf
);
2068 scratch_buffer_free (&specsbuf
);
2072 /* Handle an unknown format specifier. This prints out a canonicalized
2073 representation of the format spec itself. */
2075 printf_unknown (FILE *s
, const struct printf_info
*info
,
2076 const void *const *args
)
2080 CHAR_T work_buffer
[MAX (sizeof (info
->width
), sizeof (info
->prec
)) * 3];
2081 CHAR_T
*const workend
2082 = &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
2093 else if (info
->space
)
2097 if (info
->pad
== L_('0'))
2102 if (info
->width
!= 0)
2104 w
= _itoa_word (info
->width
, workend
, 10, 0);
2109 if (info
->prec
!= -1)
2112 w
= _itoa_word (info
->prec
, workend
, 10, 0);
2117 if (info
->spec
!= L_('\0'))
2118 outchar (info
->spec
);
2124 /* Group the digits from W to REAR_PTR according to the grouping rules
2125 of the current locale. The interpretation of GROUPING is as in
2126 `struct lconv' from <locale.h>. The grouped number extends from
2127 the returned pointer until REAR_PTR. FRONT_PTR to W is used as a
2130 group_number (CHAR_T
*front_ptr
, CHAR_T
*w
, CHAR_T
*rear_ptr
,
2131 const char *grouping
, THOUSANDS_SEP_T thousands_sep
)
2133 /* Length of the current group. */
2135 #ifndef COMPILE_WPRINTF
2136 /* Length of the separator (in wide mode, the separator is always a
2137 single wide character). */
2138 int tlen
= strlen (thousands_sep
);
2141 /* We treat all negative values like CHAR_MAX. */
2143 if (*grouping
== CHAR_MAX
|| *grouping
<= 0)
2144 /* No grouping should be done. */
2149 /* Copy existing string so that nothing gets overwritten. */
2150 memmove (front_ptr
, w
, (rear_ptr
- w
) * sizeof (CHAR_T
));
2151 CHAR_T
*s
= front_ptr
+ (rear_ptr
- w
);
2155 /* Process all characters in the string. */
2156 while (s
> front_ptr
)
2160 if (--len
== 0 && s
> front_ptr
)
2162 /* A new group begins. */
2163 #ifdef COMPILE_WPRINTF
2165 *--w
= thousands_sep
;
2167 /* Not enough room for the separator. */
2173 *--w
= thousands_sep
[--cnt
];
2176 /* Not enough room for the separator. */
2180 if (*grouping
== CHAR_MAX
2187 /* No further grouping to be done. Copy the rest of the
2189 memmove (w
, s
, (front_ptr
-s
) * sizeof (CHAR_T
));
2192 else if (*grouping
!= '\0')
2195 /* The previous grouping repeats ad infinitum. */
2202 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2205 struct _IO_FILE_plus _f
;
2206 #ifdef COMPILE_WPRINTF
2207 struct _IO_wide_data _wide_data
;
2209 _IO_FILE
*_put_stream
;
2210 #ifdef _IO_MTSAFE_IO
2216 _IO_helper_overflow (_IO_FILE
*s
, int c
)
2218 _IO_FILE
*target
= ((struct helper_file
*) s
)->_put_stream
;
2219 #ifdef COMPILE_WPRINTF
2220 int used
= s
->_wide_data
->_IO_write_ptr
- s
->_wide_data
->_IO_write_base
;
2223 _IO_size_t written
= _IO_sputn (target
, s
->_wide_data
->_IO_write_base
,
2225 if (written
== 0 || written
== WEOF
)
2227 __wmemmove (s
->_wide_data
->_IO_write_base
,
2228 s
->_wide_data
->_IO_write_base
+ written
,
2230 s
->_wide_data
->_IO_write_ptr
-= written
;
2233 int used
= s
->_IO_write_ptr
- s
->_IO_write_base
;
2236 _IO_size_t written
= _IO_sputn (target
, s
->_IO_write_base
, used
);
2237 if (written
== 0 || written
== EOF
)
2239 memmove (s
->_IO_write_base
, s
->_IO_write_base
+ written
,
2241 s
->_IO_write_ptr
-= written
;
2247 #ifdef COMPILE_WPRINTF
2248 static const struct _IO_jump_t _IO_helper_jumps libio_vtable
=
2251 JUMP_INIT (finish
, _IO_wdefault_finish
),
2252 JUMP_INIT (overflow
, _IO_helper_overflow
),
2253 JUMP_INIT (underflow
, _IO_default_underflow
),
2254 JUMP_INIT (uflow
, _IO_default_uflow
),
2255 JUMP_INIT (pbackfail
, (_IO_pbackfail_t
) _IO_wdefault_pbackfail
),
2256 JUMP_INIT (xsputn
, _IO_wdefault_xsputn
),
2257 JUMP_INIT (xsgetn
, _IO_wdefault_xsgetn
),
2258 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2259 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2260 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2261 JUMP_INIT (sync
, _IO_default_sync
),
2262 JUMP_INIT (doallocate
, _IO_wdefault_doallocate
),
2263 JUMP_INIT (read
, _IO_default_read
),
2264 JUMP_INIT (write
, _IO_default_write
),
2265 JUMP_INIT (seek
, _IO_default_seek
),
2266 JUMP_INIT (close
, _IO_default_close
),
2267 JUMP_INIT (stat
, _IO_default_stat
)
2270 static const struct _IO_jump_t _IO_helper_jumps libio_vtable
=
2273 JUMP_INIT (finish
, _IO_default_finish
),
2274 JUMP_INIT (overflow
, _IO_helper_overflow
),
2275 JUMP_INIT (underflow
, _IO_default_underflow
),
2276 JUMP_INIT (uflow
, _IO_default_uflow
),
2277 JUMP_INIT (pbackfail
, _IO_default_pbackfail
),
2278 JUMP_INIT (xsputn
, _IO_default_xsputn
),
2279 JUMP_INIT (xsgetn
, _IO_default_xsgetn
),
2280 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2281 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2282 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2283 JUMP_INIT (sync
, _IO_default_sync
),
2284 JUMP_INIT (doallocate
, _IO_default_doallocate
),
2285 JUMP_INIT (read
, _IO_default_read
),
2286 JUMP_INIT (write
, _IO_default_write
),
2287 JUMP_INIT (seek
, _IO_default_seek
),
2288 JUMP_INIT (close
, _IO_default_close
),
2289 JUMP_INIT (stat
, _IO_default_stat
)
2294 buffered_vfprintf (_IO_FILE
*s
, const CHAR_T
*format
,
2297 CHAR_T buf
[_IO_BUFSIZ
];
2298 struct helper_file helper
;
2299 _IO_FILE
*hp
= (_IO_FILE
*) &helper
._f
;
2300 int result
, to_flush
;
2302 /* Orient the stream. */
2307 /* Initialize helper. */
2308 helper
._put_stream
= s
;
2309 #ifdef COMPILE_WPRINTF
2310 hp
->_wide_data
= &helper
._wide_data
;
2311 _IO_wsetp (hp
, buf
, buf
+ sizeof buf
/ sizeof (CHAR_T
));
2314 _IO_setp (hp
, buf
, buf
+ sizeof buf
);
2317 hp
->_IO_file_flags
= _IO_MAGIC
|_IO_NO_READS
|_IO_USER_LOCK
;
2318 #if _IO_JUMPS_OFFSET
2319 hp
->_vtable_offset
= 0;
2321 #ifdef _IO_MTSAFE_IO
2324 hp
->_flags2
= s
->_flags2
;
2325 _IO_JUMPS (&helper
._f
) = (struct _IO_jump_t
*) &_IO_helper_jumps
;
2327 /* Now print to helper instead. */
2328 #ifndef COMPILE_WPRINTF
2329 result
= _IO_vfprintf (hp
, format
, args
);
2331 result
= vfprintf (hp
, format
, args
);
2335 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile
, s
);
2338 /* Now flush anything from the helper to the S. */
2339 #ifdef COMPILE_WPRINTF
2340 if ((to_flush
= (hp
->_wide_data
->_IO_write_ptr
2341 - hp
->_wide_data
->_IO_write_base
)) > 0)
2343 if ((int) _IO_sputn (s
, hp
->_wide_data
->_IO_write_base
, to_flush
)
2348 if ((to_flush
= hp
->_IO_write_ptr
- hp
->_IO_write_base
) > 0)
2350 if ((int) _IO_sputn (s
, hp
->_IO_write_base
, to_flush
) != to_flush
)
2355 /* Unlock the stream. */
2356 _IO_funlockfile (s
);
2357 __libc_cleanup_region_end (0);
2363 #ifdef COMPILE_WPRINTF
2364 strong_alias (_IO_vfwprintf
, __vfwprintf
);
2365 ldbl_weak_alias (_IO_vfwprintf
, vfwprintf
);
2367 ldbl_strong_alias (_IO_vfprintf_internal
, vfprintf
);
2368 ldbl_hidden_def (_IO_vfprintf_internal
, vfprintf
)
2369 ldbl_strong_alias (_IO_vfprintf_internal
, _IO_vfprintf
);
2370 ldbl_hidden_def (_IO_vfprintf_internal
, _IO_vfprintf
)