1 /* Copyright (C) 1991-2018 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/>. */
18 #include <array_length.h>
28 #include <libc-lock.h>
29 #include <sys/param.h>
31 #include <locale/localeinfo.h>
33 #include <scratch_buffer.h>
35 /* This code is shared between the standard stdio implementation found
36 in GNU C library and the libio implementation originally found in
39 Beside this it is also shared between the normal and wide character
40 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
46 #define va_list _IO_va_list
48 #define BUFSIZ _IO_BUFSIZ
49 /* In some cases we need extra space for all the output which is not
50 counted in the width of the string. We assume 32 characters is
53 #define ARGCHECK(S, Format) \
56 /* Check file argument for consistence. */ \
58 if (S->_flags & _IO_NO_WRITES) \
60 S->_flags |= _IO_ERR_SEEN; \
61 __set_errno (EBADF); \
70 #define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
72 #define done_add(val) \
74 unsigned int _val = val; \
75 assert ((unsigned int) done < (unsigned int) INT_MAX); \
76 if (__glibc_unlikely (INT_MAX - done < _val)) \
79 __set_errno (EOVERFLOW); \
85 #ifndef COMPILE_WPRINTF
86 # define vfprintf _IO_vfprintf_internal
88 # define UCHAR_T unsigned char
90 typedef const char *THOUSANDS_SEP_T
;
92 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
93 # define STR_LEN(Str) strlen (Str)
95 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
96 # define PAD(Padchar) \
100 _IO_ssize_t written = _IO_padn (s, (Padchar), width); \
101 if (__glibc_unlikely (written != width)) \
106 done_add (written); \
109 # define PUTC(C, F) _IO_putc_unlocked (C, F)
110 # define ORIENT if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
113 # define vfprintf _IO_vfwprintf
114 # define CHAR_T wchar_t
115 /* This is a hack!!! There should be a type uwchar_t. */
116 # define UCHAR_T unsigned int /* uwchar_t */
117 # define INT_T wint_t
118 typedef wchar_t THOUSANDS_SEP_T
;
119 # define L_(Str) L##Str
120 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
121 # define STR_LEN(Str) __wcslen (Str)
125 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
126 # define PAD(Padchar) \
130 _IO_ssize_t written = _IO_wpadn (s, (Padchar), width); \
131 if (__glibc_unlikely (written != width)) \
136 done_add (written); \
139 # define PUTC(C, F) _IO_putwc_unlocked (C, F)
140 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
143 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
144 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
149 #include "_i18n_number.h"
151 /* Include the shared code for parsing the format string. */
152 #include "printf-parse.h"
155 #define outchar(Ch) \
158 const INT_T outc = (Ch); \
159 if (PUTC (outc, s) == EOF || done == INT_MAX) \
168 #define outstring(String, Len) \
171 assert ((size_t) done <= (size_t) INT_MAX); \
172 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
177 if (__glibc_unlikely (INT_MAX - done < (Len))) \
180 __set_errno (EOVERFLOW); \
187 /* For handling long_double and longlong we use the same flag. If
188 `long' and `long long' are effectively the same type define it to
190 #if LONG_MAX == LONG_LONG_MAX
191 # define is_longlong 0
193 # define is_longlong is_long_double
196 /* If `long' and `int' is effectively the same type we don't have to
197 handle `long separately. */
198 #if INT_MAX == LONG_MAX
199 # define is_long_num 0
201 # define is_long_num is_long
205 /* Global constants. */
206 static const CHAR_T null
[] = L_("(null)");
208 /* Size of the work_buffer variable (in characters, not bytes. */
209 enum { WORK_BUFFER_SIZE
= 1000 / sizeof (CHAR_T
) };
211 /* This table maps a character into a number representing a class. In
212 each step there is a destination label for each class. */
213 static const uint8_t jump_table
[] =
215 /* ' ' */ 1, 0, 0, /* '#' */ 4,
216 0, /* '%' */ 14, 0, /* '\''*/ 6,
217 0, 0, /* '*' */ 7, /* '+' */ 2,
218 0, /* '-' */ 3, /* '.' */ 9, 0,
219 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
220 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
221 /* '8' */ 8, /* '9' */ 8, 0, 0,
223 0, /* 'A' */ 26, 0, /* 'C' */ 25,
224 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
225 0, /* 'I' */ 29, 0, 0,
226 /* 'L' */ 12, 0, 0, 0,
227 0, 0, 0, /* 'S' */ 21,
229 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
231 0, /* 'a' */ 26, 0, /* 'c' */ 20,
232 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
233 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
234 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
235 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
236 /* 't' */ 27, /* 'u' */ 16, 0, 0,
237 /* 'x' */ 18, 0, /* 'z' */ 13
240 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
241 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
242 #define LABEL(Name) do_##Name
244 /* 'int' is enough and it saves some space on 64 bit systems. */
245 # define JUMP_TABLE_TYPE const int
246 # define JUMP_TABLE_BASE_LABEL do_form_unknown
247 # define REF(Name) &&do_##Name - &&JUMP_TABLE_BASE_LABEL
248 # define JUMP(ChExpr, table) \
254 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
255 : table[CHAR_CLASS (spec)]; \
256 ptr = &&JUMP_TABLE_BASE_LABEL + offset; \
261 # define JUMP_TABLE_TYPE const void *const
262 # define REF(Name) &&do_##Name
263 # define JUMP(ChExpr, table) \
268 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
269 : table[CHAR_CLASS (spec)]; \
275 #define STEP0_3_TABLE \
276 /* Step 0: at the beginning. */ \
277 static JUMP_TABLE_TYPE step0_jumps[30] = \
279 REF (form_unknown), \
280 REF (flag_space), /* for ' ' */ \
281 REF (flag_plus), /* for '+' */ \
282 REF (flag_minus), /* for '-' */ \
283 REF (flag_hash), /* for '<hash>' */ \
284 REF (flag_zero), /* for '0' */ \
285 REF (flag_quote), /* for '\'' */ \
286 REF (width_asterics), /* for '*' */ \
287 REF (width), /* for '1'...'9' */ \
288 REF (precision), /* for '.' */ \
289 REF (mod_half), /* for 'h' */ \
290 REF (mod_long), /* for 'l' */ \
291 REF (mod_longlong), /* for 'L', 'q' */ \
292 REF (mod_size_t), /* for 'z', 'Z' */ \
293 REF (form_percent), /* for '%' */ \
294 REF (form_integer), /* for 'd', 'i' */ \
295 REF (form_unsigned), /* for 'u' */ \
296 REF (form_octal), /* for 'o' */ \
297 REF (form_hexa), /* for 'X', 'x' */ \
298 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
299 REF (form_character), /* for 'c' */ \
300 REF (form_string), /* for 's', 'S' */ \
301 REF (form_pointer), /* for 'p' */ \
302 REF (form_number), /* for 'n' */ \
303 REF (form_strerror), /* for 'm' */ \
304 REF (form_wcharacter), /* for 'C' */ \
305 REF (form_floathex), /* for 'A', 'a' */ \
306 REF (mod_ptrdiff_t), /* for 't' */ \
307 REF (mod_intmax_t), /* for 'j' */ \
308 REF (flag_i18n), /* for 'I' */ \
310 /* Step 1: after processing width. */ \
311 static JUMP_TABLE_TYPE step1_jumps[30] = \
313 REF (form_unknown), \
314 REF (form_unknown), /* for ' ' */ \
315 REF (form_unknown), /* for '+' */ \
316 REF (form_unknown), /* for '-' */ \
317 REF (form_unknown), /* for '<hash>' */ \
318 REF (form_unknown), /* for '0' */ \
319 REF (form_unknown), /* for '\'' */ \
320 REF (form_unknown), /* for '*' */ \
321 REF (form_unknown), /* for '1'...'9' */ \
322 REF (precision), /* for '.' */ \
323 REF (mod_half), /* for 'h' */ \
324 REF (mod_long), /* for 'l' */ \
325 REF (mod_longlong), /* for 'L', 'q' */ \
326 REF (mod_size_t), /* for 'z', 'Z' */ \
327 REF (form_percent), /* for '%' */ \
328 REF (form_integer), /* for 'd', 'i' */ \
329 REF (form_unsigned), /* for 'u' */ \
330 REF (form_octal), /* for 'o' */ \
331 REF (form_hexa), /* for 'X', 'x' */ \
332 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
333 REF (form_character), /* for 'c' */ \
334 REF (form_string), /* for 's', 'S' */ \
335 REF (form_pointer), /* for 'p' */ \
336 REF (form_number), /* for 'n' */ \
337 REF (form_strerror), /* for 'm' */ \
338 REF (form_wcharacter), /* for 'C' */ \
339 REF (form_floathex), /* for 'A', 'a' */ \
340 REF (mod_ptrdiff_t), /* for 't' */ \
341 REF (mod_intmax_t), /* for 'j' */ \
342 REF (form_unknown) /* for 'I' */ \
344 /* Step 2: after processing precision. */ \
345 static JUMP_TABLE_TYPE step2_jumps[30] = \
347 REF (form_unknown), \
348 REF (form_unknown), /* for ' ' */ \
349 REF (form_unknown), /* for '+' */ \
350 REF (form_unknown), /* for '-' */ \
351 REF (form_unknown), /* for '<hash>' */ \
352 REF (form_unknown), /* for '0' */ \
353 REF (form_unknown), /* for '\'' */ \
354 REF (form_unknown), /* for '*' */ \
355 REF (form_unknown), /* for '1'...'9' */ \
356 REF (form_unknown), /* for '.' */ \
357 REF (mod_half), /* for 'h' */ \
358 REF (mod_long), /* for 'l' */ \
359 REF (mod_longlong), /* for 'L', 'q' */ \
360 REF (mod_size_t), /* for 'z', 'Z' */ \
361 REF (form_percent), /* for '%' */ \
362 REF (form_integer), /* for 'd', 'i' */ \
363 REF (form_unsigned), /* for 'u' */ \
364 REF (form_octal), /* for 'o' */ \
365 REF (form_hexa), /* for 'X', 'x' */ \
366 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
367 REF (form_character), /* for 'c' */ \
368 REF (form_string), /* for 's', 'S' */ \
369 REF (form_pointer), /* for 'p' */ \
370 REF (form_number), /* for 'n' */ \
371 REF (form_strerror), /* for 'm' */ \
372 REF (form_wcharacter), /* for 'C' */ \
373 REF (form_floathex), /* for 'A', 'a' */ \
374 REF (mod_ptrdiff_t), /* for 't' */ \
375 REF (mod_intmax_t), /* for 'j' */ \
376 REF (form_unknown) /* for 'I' */ \
378 /* Step 3a: after processing first 'h' modifier. */ \
379 static JUMP_TABLE_TYPE step3a_jumps[30] = \
381 REF (form_unknown), \
382 REF (form_unknown), /* for ' ' */ \
383 REF (form_unknown), /* for '+' */ \
384 REF (form_unknown), /* for '-' */ \
385 REF (form_unknown), /* for '<hash>' */ \
386 REF (form_unknown), /* for '0' */ \
387 REF (form_unknown), /* for '\'' */ \
388 REF (form_unknown), /* for '*' */ \
389 REF (form_unknown), /* for '1'...'9' */ \
390 REF (form_unknown), /* for '.' */ \
391 REF (mod_halfhalf), /* for 'h' */ \
392 REF (form_unknown), /* for 'l' */ \
393 REF (form_unknown), /* for 'L', 'q' */ \
394 REF (form_unknown), /* for 'z', 'Z' */ \
395 REF (form_percent), /* for '%' */ \
396 REF (form_integer), /* for 'd', 'i' */ \
397 REF (form_unsigned), /* for 'u' */ \
398 REF (form_octal), /* for 'o' */ \
399 REF (form_hexa), /* for 'X', 'x' */ \
400 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
401 REF (form_unknown), /* for 'c' */ \
402 REF (form_unknown), /* for 's', 'S' */ \
403 REF (form_unknown), /* for 'p' */ \
404 REF (form_number), /* for 'n' */ \
405 REF (form_unknown), /* for 'm' */ \
406 REF (form_unknown), /* for 'C' */ \
407 REF (form_unknown), /* for 'A', 'a' */ \
408 REF (form_unknown), /* for 't' */ \
409 REF (form_unknown), /* for 'j' */ \
410 REF (form_unknown) /* for 'I' */ \
412 /* Step 3b: after processing first 'l' modifier. */ \
413 static JUMP_TABLE_TYPE step3b_jumps[30] = \
415 REF (form_unknown), \
416 REF (form_unknown), /* for ' ' */ \
417 REF (form_unknown), /* for '+' */ \
418 REF (form_unknown), /* for '-' */ \
419 REF (form_unknown), /* for '<hash>' */ \
420 REF (form_unknown), /* for '0' */ \
421 REF (form_unknown), /* for '\'' */ \
422 REF (form_unknown), /* for '*' */ \
423 REF (form_unknown), /* for '1'...'9' */ \
424 REF (form_unknown), /* for '.' */ \
425 REF (form_unknown), /* for 'h' */ \
426 REF (mod_longlong), /* for 'l' */ \
427 REF (form_unknown), /* for 'L', 'q' */ \
428 REF (form_unknown), /* for 'z', 'Z' */ \
429 REF (form_percent), /* for '%' */ \
430 REF (form_integer), /* for 'd', 'i' */ \
431 REF (form_unsigned), /* for 'u' */ \
432 REF (form_octal), /* for 'o' */ \
433 REF (form_hexa), /* for 'X', 'x' */ \
434 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
435 REF (form_character), /* for 'c' */ \
436 REF (form_string), /* for 's', 'S' */ \
437 REF (form_pointer), /* for 'p' */ \
438 REF (form_number), /* for 'n' */ \
439 REF (form_strerror), /* for 'm' */ \
440 REF (form_wcharacter), /* for 'C' */ \
441 REF (form_floathex), /* for 'A', 'a' */ \
442 REF (form_unknown), /* for 't' */ \
443 REF (form_unknown), /* for 'j' */ \
444 REF (form_unknown) /* for 'I' */ \
447 #define STEP4_TABLE \
448 /* Step 4: processing format specifier. */ \
449 static JUMP_TABLE_TYPE step4_jumps[30] = \
451 REF (form_unknown), \
452 REF (form_unknown), /* for ' ' */ \
453 REF (form_unknown), /* for '+' */ \
454 REF (form_unknown), /* for '-' */ \
455 REF (form_unknown), /* for '<hash>' */ \
456 REF (form_unknown), /* for '0' */ \
457 REF (form_unknown), /* for '\'' */ \
458 REF (form_unknown), /* for '*' */ \
459 REF (form_unknown), /* for '1'...'9' */ \
460 REF (form_unknown), /* for '.' */ \
461 REF (form_unknown), /* for 'h' */ \
462 REF (form_unknown), /* for 'l' */ \
463 REF (form_unknown), /* for 'L', 'q' */ \
464 REF (form_unknown), /* for 'z', 'Z' */ \
465 REF (form_percent), /* for '%' */ \
466 REF (form_integer), /* for 'd', 'i' */ \
467 REF (form_unsigned), /* for 'u' */ \
468 REF (form_octal), /* for 'o' */ \
469 REF (form_hexa), /* for 'X', 'x' */ \
470 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
471 REF (form_character), /* for 'c' */ \
472 REF (form_string), /* for 's', 'S' */ \
473 REF (form_pointer), /* for 'p' */ \
474 REF (form_number), /* for 'n' */ \
475 REF (form_strerror), /* for 'm' */ \
476 REF (form_wcharacter), /* for 'C' */ \
477 REF (form_floathex), /* for 'A', 'a' */ \
478 REF (form_unknown), /* for 't' */ \
479 REF (form_unknown), /* for 'j' */ \
480 REF (form_unknown) /* for 'I' */ \
484 #define process_arg(fspec) \
485 /* Start real work. We know about all flags and modifiers and \
486 now process the wanted format specifier. */ \
487 LABEL (form_percent): \
488 /* Write a literal "%". */ \
492 LABEL (form_integer): \
493 /* Signed decimal integer. */ \
498 long long int signed_number; \
501 signed_number = va_arg (ap, long long int); \
503 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
505 is_negative = signed_number < 0; \
506 number.longlong = is_negative ? (- signed_number) : signed_number; \
508 goto LABEL (longlong_number); \
512 long int signed_number; \
517 signed_number = va_arg (ap, long int); \
519 signed_number = (signed char) va_arg (ap, unsigned int); \
520 else if (!is_short) \
521 signed_number = va_arg (ap, int); \
523 signed_number = (short int) va_arg (ap, unsigned int); \
527 signed_number = args_value[fspec->data_arg].pa_long_int; \
529 signed_number = (signed char) \
530 args_value[fspec->data_arg].pa_u_int; \
531 else if (!is_short) \
532 signed_number = args_value[fspec->data_arg].pa_int; \
534 signed_number = (short int) \
535 args_value[fspec->data_arg].pa_u_int; \
537 is_negative = signed_number < 0; \
538 number.word = is_negative ? (- signed_number) : signed_number; \
540 goto LABEL (number); \
544 LABEL (form_unsigned): \
545 /* Unsigned decimal integer. */ \
547 goto LABEL (unsigned_number); \
550 LABEL (form_octal): \
551 /* Unsigned octal integer. */ \
553 goto LABEL (unsigned_number); \
557 /* Unsigned hexadecimal integer. */ \
560 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
562 /* ISO specifies the `+' and ` ' flags only for signed \
571 number.longlong = va_arg (ap, unsigned long long int); \
573 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
575 LABEL (longlong_number): \
577 /* Supply a default precision if none was given. */ \
580 /* We have to take care for the '0' flag. If a precision \
581 is given it must be ignored. */ \
584 /* If the precision is 0 and the number is 0 nothing has to \
585 be written for the number, except for the 'o' format in \
587 if (prec == 0 && number.longlong == 0) \
590 if (base == 8 && alt) \
591 *--string = L_('0'); \
595 /* Put the number in WORK. */ \
596 string = _itoa (number.longlong, workend, base, \
598 if (group && grouping) \
599 string = group_number (work_buffer, string, workend, \
600 grouping, thousands_sep); \
601 if (use_outdigits && base == 10) \
602 string = _i18n_number_rewrite (string, workend, workend); \
604 /* Simplify further test for num != 0. */ \
605 number.word = number.longlong != 0; \
612 number.word = va_arg (ap, unsigned long int); \
614 number.word = (unsigned char) va_arg (ap, unsigned int); \
615 else if (!is_short) \
616 number.word = va_arg (ap, unsigned int); \
618 number.word = (unsigned short int) va_arg (ap, unsigned int); \
622 number.word = args_value[fspec->data_arg].pa_u_long_int; \
624 number.word = (unsigned char) \
625 args_value[fspec->data_arg].pa_u_int; \
626 else if (!is_short) \
627 number.word = args_value[fspec->data_arg].pa_u_int; \
629 number.word = (unsigned short int) \
630 args_value[fspec->data_arg].pa_u_int; \
634 /* Supply a default precision if none was given. */ \
637 /* We have to take care for the '0' flag. If a precision \
638 is given it must be ignored. */ \
641 /* If the precision is 0 and the number is 0 nothing has to \
642 be written for the number, except for the 'o' format in \
644 if (prec == 0 && number.word == 0) \
647 if (base == 8 && alt) \
648 *--string = L_('0'); \
652 /* Put the number in WORK. */ \
653 string = _itoa_word (number.word, workend, base, \
655 if (group && grouping) \
656 string = group_number (work_buffer, string, workend, \
657 grouping, thousands_sep); \
658 if (use_outdigits && base == 10) \
659 string = _i18n_number_rewrite (string, workend, workend); \
663 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
664 /* Add octal marker. */ \
665 *--string = L_('0'); \
667 prec = MAX (0, prec - (workend - string)); \
671 width -= workend - string + prec; \
673 if (number.word != 0 && alt && base == 16) \
674 /* Account for 0X hex marker. */ \
677 if (is_negative || showsign || space) \
680 if (pad == L_(' ')) \
693 if (number.word != 0 && alt && base == 16) \
702 outstring (string, workend - string); \
724 if (number.word != 0 && alt && base == 16) \
731 width -= workend - string + prec; \
741 outstring (string, workend - string); \
747 LABEL (form_float): \
749 /* Floating-point number. This is handled by printf_fp.c. */ \
756 is_long_double = 0; \
758 struct printf_info info = { .prec = prec, \
761 .is_long_double = is_long_double, \
762 .is_short = is_short, \
763 .is_long = is_long, \
767 .showsign = showsign, \
771 .i18n = use_outdigits, \
772 .wide = sizeof (CHAR_T) != 1, \
773 .is_binary128 = 0}; \
775 if (is_long_double) \
776 the_arg.pa_long_double = va_arg (ap, long double); \
778 the_arg.pa_double = va_arg (ap, double); \
779 ptr = (const void *) &the_arg; \
781 function_done = __printf_fp (s, &info, &ptr); \
785 ptr = (const void *) &args_value[fspec->data_arg]; \
788 fspec->data_arg_type = PA_DOUBLE; \
789 fspec->info.is_long_double = 0; \
791 /* Not supported by *printf functions. */ \
792 fspec->info.is_binary128 = 0; \
794 function_done = __printf_fp (s, &fspec->info, &ptr); \
797 if (function_done < 0) \
799 /* Error in print handler; up to handler to set errno. */ \
804 done_add (function_done); \
808 LABEL (form_floathex): \
810 /* Floating point number printed as hexadecimal number. */ \
817 is_long_double = 0; \
819 struct printf_info info = { .prec = prec, \
822 .is_long_double = is_long_double, \
823 .is_short = is_short, \
824 .is_long = is_long, \
828 .showsign = showsign, \
832 .wide = sizeof (CHAR_T) != 1, \
833 .is_binary128 = 0}; \
835 if (is_long_double) \
836 the_arg.pa_long_double = va_arg (ap, long double); \
838 the_arg.pa_double = va_arg (ap, double); \
839 ptr = (const void *) &the_arg; \
841 function_done = __printf_fphex (s, &info, &ptr); \
845 ptr = (const void *) &args_value[fspec->data_arg]; \
847 fspec->info.is_long_double = 0; \
848 /* Not supported by *printf functions. */ \
849 fspec->info.is_binary128 = 0; \
851 function_done = __printf_fphex (s, &fspec->info, &ptr); \
854 if (function_done < 0) \
856 /* Error in print handler; up to handler to set errno. */ \
861 done_add (function_done); \
865 LABEL (form_pointer): \
866 /* Generic pointer. */ \
870 ptr = va_arg (ap, void *); \
872 ptr = args_value[fspec->data_arg].pa_pointer; \
875 /* If the pointer is not NULL, write it as a %#x spec. */ \
877 number.word = (unsigned long int) ptr; \
882 goto LABEL (number); \
886 /* Write "(nil)" for a nil pointer. */ \
887 string = (CHAR_T *) L_("(nil)"); \
888 /* Make sure the full string "(nil)" is printed. */ \
891 /* This is a wide string iff compiling wprintf. */ \
892 is_long = sizeof (CHAR_T) > 1; \
893 goto LABEL (print_string); \
898 LABEL (form_number): \
899 if (s->_flags2 & _IO_FLAGS2_FORTIFY) \
901 if (! readonly_format) \
903 extern int __readonly_area (const void *, size_t) \
906 = __readonly_area (format, ((STR_LEN (format) + 1) \
907 * sizeof (CHAR_T))); \
909 if (readonly_format < 0) \
910 __libc_fatal ("*** %n in writable segment detected ***\n"); \
912 /* Answer the count of characters written. */ \
916 *(long long int *) va_arg (ap, void *) = done; \
917 else if (is_long_num) \
918 *(long int *) va_arg (ap, void *) = done; \
920 *(char *) va_arg (ap, void *) = done; \
921 else if (!is_short) \
922 *(int *) va_arg (ap, void *) = done; \
924 *(short int *) va_arg (ap, void *) = done; \
928 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
929 else if (is_long_num) \
930 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
932 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
933 else if (!is_short) \
934 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
936 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
939 LABEL (form_strerror): \
940 /* Print description of error ERRNO. */ \
942 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
943 WORK_BUFFER_SIZE * sizeof (CHAR_T)); \
944 is_long = 0; /* This is no wide-char string. */ \
945 goto LABEL (print_string)
947 #ifdef COMPILE_WPRINTF
948 # define process_string_arg(fspec) \
949 LABEL (form_character): \
952 goto LABEL (form_wcharacter); \
953 --width; /* Account for the character itself. */ \
957 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
959 outchar (__btowc ((unsigned char) \
960 args_value[fspec->data_arg].pa_int)); \
965 LABEL (form_wcharacter): \
967 /* Wide character. */ \
972 outchar (va_arg (ap, wchar_t)); \
974 outchar (args_value[fspec->data_arg].pa_wchar); \
980 LABEL (form_string): \
983 int string_malloced; \
985 /* The string argument could in fact be `char *' or `wchar_t *'. \
986 But this should not make a difference here. */ \
988 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
990 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
992 /* Entry point for printing other strings. */ \
993 LABEL (print_string): \
995 string_malloced = 0; \
996 if (string == NULL) \
998 /* Write "(null)" if there's space. */ \
999 if (prec == -1 || prec >= (int) array_length (null) - 1) \
1001 string = (CHAR_T *) null; \
1002 len = array_length (null) - 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
)