1 /* Copyright (C) 1991-2015 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 <bits/libc-lock.h>
28 #include <sys/param.h>
30 #include <locale/localeinfo.h>
33 /* This code is shared between the standard stdio implementation found
34 in GNU C library and the libio implementation originally found in
37 Beside this it is also shared between the normal and wide character
38 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
44 #define va_list _IO_va_list
46 #define BUFSIZ _IO_BUFSIZ
47 #define ARGCHECK(S, Format) \
50 /* Check file argument for consistence. */ \
52 if (S->_flags & _IO_NO_WRITES) \
54 S->_flags |= _IO_ERR_SEEN; \
55 __set_errno (EBADF); \
64 #define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
66 #define done_add(val) \
68 unsigned int _val = val; \
69 assert ((unsigned int) done < (unsigned int) INT_MAX); \
70 if (__glibc_unlikely (INT_MAX - done < _val)) \
73 __set_errno (EOVERFLOW); \
79 #ifndef COMPILE_WPRINTF
80 # define vfprintf _IO_vfprintf_internal
82 # define UCHAR_T unsigned char
85 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
86 # define STR_LEN(Str) strlen (Str)
88 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
89 # define PAD(Padchar) \
93 _IO_ssize_t written = _IO_padn (s, (Padchar), width); \
94 if (__glibc_unlikely (written != width)) \
102 # define PUTC(C, F) _IO_putc_unlocked (C, F)
103 # define ORIENT if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
106 # define vfprintf _IO_vfwprintf
107 # define CHAR_T wchar_t
108 /* This is a hack!!! There should be a type uwchar_t. */
109 # define UCHAR_T unsigned int /* uwchar_t */
110 # define INT_T wint_t
111 # define L_(Str) L##Str
112 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
113 # define STR_LEN(Str) __wcslen (Str)
117 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
118 # define PAD(Padchar) \
122 _IO_ssize_t written = _IO_wpadn (s, (Padchar), width); \
123 if (__glibc_unlikely (written != width)) \
128 done_add (written); \
131 # define PUTC(C, F) _IO_putwc_unlocked (C, F)
132 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
135 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
136 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
141 #include "_i18n_number.h"
143 /* Include the shared code for parsing the format string. */
144 #include "printf-parse.h"
147 #define outchar(Ch) \
150 const INT_T outc = (Ch); \
151 if (PUTC (outc, s) == EOF || done == INT_MAX) \
160 #define outstring(String, Len) \
163 assert ((size_t) done <= (size_t) INT_MAX); \
164 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
169 if (__glibc_unlikely (INT_MAX - done < (Len))) \
172 __set_errno (EOVERFLOW); \
179 /* For handling long_double and longlong we use the same flag. If
180 `long' and `long long' are effectively the same type define it to
182 #if LONG_MAX == LONG_LONG_MAX
183 # define is_longlong 0
185 # define is_longlong is_long_double
188 /* If `long' and `int' is effectively the same type we don't have to
189 handle `long separately. */
190 #if INT_MAX == LONG_MAX
191 # define is_long_num 0
193 # define is_long_num is_long
197 /* Global variables. */
198 static const CHAR_T null
[] = L_("(null)");
201 /* Helper function to provide temporary buffering for unbuffered streams. */
202 static int buffered_vfprintf (FILE *stream
, const CHAR_T
*fmt
, va_list)
203 __THROW
__attribute__ ((noinline
)) internal_function
;
205 /* Handle unknown format specifier. */
206 static int printf_unknown (FILE *, const struct printf_info
*,
207 const void *const *) __THROW
;
209 /* Group digits of number string. */
210 #ifdef COMPILE_WPRINTF
211 static CHAR_T
*group_number (CHAR_T
*, CHAR_T
*, const char *, wchar_t)
212 __THROW internal_function
;
214 static CHAR_T
*group_number (CHAR_T
*, CHAR_T
*, const char *, const char *)
215 __THROW internal_function
;
219 /* The function itself. */
221 vfprintf (FILE *s
, const CHAR_T
*format
, va_list ap
)
223 /* The character used as thousands separator. */
224 #ifdef COMPILE_WPRINTF
225 wchar_t thousands_sep
= L
'\0';
227 const char *thousands_sep
= NULL
;
230 /* The string describing the size of groups of digits. */
231 const char *grouping
;
233 /* Place to accumulate the result. */
236 /* Current character in format string. */
239 /* End of leading constant string. */
240 const UCHAR_T
*lead_str_end
;
242 /* Points to next format specifier. */
243 const UCHAR_T
*end_of_spec
;
245 /* Buffer intermediate results. */
246 CHAR_T work_buffer
[1000];
247 CHAR_T
*workstart
= NULL
;
250 /* We have to save the original argument pointer. */
253 /* Count number of specifiers we already processed. */
256 /* For the %m format we may need the current `errno' value. */
257 int save_errno
= errno
;
259 /* 1 if format is in read-only memory, -1 if it is in writable memory,
261 int readonly_format
= 0;
263 /* For the argument descriptions, which may be allocated on the heap. */
264 void *args_malloced
= NULL
;
266 /* For positional argument handling. */
267 struct printf_spec
*specs
;
269 /* Track if we malloced the SPECS array and thus must free it. */
270 bool specs_malloced
= false;
272 /* This table maps a character into a number representing a
273 class. In each step there is a destination label for each
275 static const uint8_t jump_table
[] =
277 /* ' ' */ 1, 0, 0, /* '#' */ 4,
278 0, /* '%' */ 14, 0, /* '\''*/ 6,
279 0, 0, /* '*' */ 7, /* '+' */ 2,
280 0, /* '-' */ 3, /* '.' */ 9, 0,
281 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
282 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
283 /* '8' */ 8, /* '9' */ 8, 0, 0,
285 0, /* 'A' */ 26, 0, /* 'C' */ 25,
286 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
287 0, /* 'I' */ 29, 0, 0,
288 /* 'L' */ 12, 0, 0, 0,
289 0, 0, 0, /* 'S' */ 21,
291 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
293 0, /* 'a' */ 26, 0, /* 'c' */ 20,
294 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
295 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
296 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
297 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
298 /* 't' */ 27, /* 'u' */ 16, 0, 0,
299 /* 'x' */ 18, 0, /* 'z' */ 13
302 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
303 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
305 /* 'int' is enough and it saves some space on 64 bit systems. */
306 # define JUMP_TABLE_TYPE const int
307 # define JUMP(ChExpr, table) \
313 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
314 : table[CHAR_CLASS (spec)]; \
315 ptr = &&do_form_unknown + offset; \
320 # define JUMP_TABLE_TYPE const void *const
321 # define JUMP(ChExpr, table) \
326 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
327 : table[CHAR_CLASS (spec)]; \
333 #define STEP0_3_TABLE \
334 /* Step 0: at the beginning. */ \
335 static JUMP_TABLE_TYPE step0_jumps[30] = \
337 REF (form_unknown), \
338 REF (flag_space), /* for ' ' */ \
339 REF (flag_plus), /* for '+' */ \
340 REF (flag_minus), /* for '-' */ \
341 REF (flag_hash), /* for '<hash>' */ \
342 REF (flag_zero), /* for '0' */ \
343 REF (flag_quote), /* for '\'' */ \
344 REF (width_asterics), /* for '*' */ \
345 REF (width), /* for '1'...'9' */ \
346 REF (precision), /* for '.' */ \
347 REF (mod_half), /* for 'h' */ \
348 REF (mod_long), /* for 'l' */ \
349 REF (mod_longlong), /* for 'L', 'q' */ \
350 REF (mod_size_t), /* for 'z', 'Z' */ \
351 REF (form_percent), /* for '%' */ \
352 REF (form_integer), /* for 'd', 'i' */ \
353 REF (form_unsigned), /* for 'u' */ \
354 REF (form_octal), /* for 'o' */ \
355 REF (form_hexa), /* for 'X', 'x' */ \
356 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
357 REF (form_character), /* for 'c' */ \
358 REF (form_string), /* for 's', 'S' */ \
359 REF (form_pointer), /* for 'p' */ \
360 REF (form_number), /* for 'n' */ \
361 REF (form_strerror), /* for 'm' */ \
362 REF (form_wcharacter), /* for 'C' */ \
363 REF (form_floathex), /* for 'A', 'a' */ \
364 REF (mod_ptrdiff_t), /* for 't' */ \
365 REF (mod_intmax_t), /* for 'j' */ \
366 REF (flag_i18n), /* for 'I' */ \
368 /* Step 1: after processing width. */ \
369 static JUMP_TABLE_TYPE step1_jumps[30] = \
371 REF (form_unknown), \
372 REF (form_unknown), /* for ' ' */ \
373 REF (form_unknown), /* for '+' */ \
374 REF (form_unknown), /* for '-' */ \
375 REF (form_unknown), /* for '<hash>' */ \
376 REF (form_unknown), /* for '0' */ \
377 REF (form_unknown), /* for '\'' */ \
378 REF (form_unknown), /* for '*' */ \
379 REF (form_unknown), /* for '1'...'9' */ \
380 REF (precision), /* for '.' */ \
381 REF (mod_half), /* for 'h' */ \
382 REF (mod_long), /* for 'l' */ \
383 REF (mod_longlong), /* for 'L', 'q' */ \
384 REF (mod_size_t), /* for 'z', 'Z' */ \
385 REF (form_percent), /* for '%' */ \
386 REF (form_integer), /* for 'd', 'i' */ \
387 REF (form_unsigned), /* for 'u' */ \
388 REF (form_octal), /* for 'o' */ \
389 REF (form_hexa), /* for 'X', 'x' */ \
390 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
391 REF (form_character), /* for 'c' */ \
392 REF (form_string), /* for 's', 'S' */ \
393 REF (form_pointer), /* for 'p' */ \
394 REF (form_number), /* for 'n' */ \
395 REF (form_strerror), /* for 'm' */ \
396 REF (form_wcharacter), /* for 'C' */ \
397 REF (form_floathex), /* for 'A', 'a' */ \
398 REF (mod_ptrdiff_t), /* for 't' */ \
399 REF (mod_intmax_t), /* for 'j' */ \
400 REF (form_unknown) /* for 'I' */ \
402 /* Step 2: after processing precision. */ \
403 static JUMP_TABLE_TYPE step2_jumps[30] = \
405 REF (form_unknown), \
406 REF (form_unknown), /* for ' ' */ \
407 REF (form_unknown), /* for '+' */ \
408 REF (form_unknown), /* for '-' */ \
409 REF (form_unknown), /* for '<hash>' */ \
410 REF (form_unknown), /* for '0' */ \
411 REF (form_unknown), /* for '\'' */ \
412 REF (form_unknown), /* for '*' */ \
413 REF (form_unknown), /* for '1'...'9' */ \
414 REF (form_unknown), /* for '.' */ \
415 REF (mod_half), /* for 'h' */ \
416 REF (mod_long), /* for 'l' */ \
417 REF (mod_longlong), /* for 'L', 'q' */ \
418 REF (mod_size_t), /* for 'z', 'Z' */ \
419 REF (form_percent), /* for '%' */ \
420 REF (form_integer), /* for 'd', 'i' */ \
421 REF (form_unsigned), /* for 'u' */ \
422 REF (form_octal), /* for 'o' */ \
423 REF (form_hexa), /* for 'X', 'x' */ \
424 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
425 REF (form_character), /* for 'c' */ \
426 REF (form_string), /* for 's', 'S' */ \
427 REF (form_pointer), /* for 'p' */ \
428 REF (form_number), /* for 'n' */ \
429 REF (form_strerror), /* for 'm' */ \
430 REF (form_wcharacter), /* for 'C' */ \
431 REF (form_floathex), /* for 'A', 'a' */ \
432 REF (mod_ptrdiff_t), /* for 't' */ \
433 REF (mod_intmax_t), /* for 'j' */ \
434 REF (form_unknown) /* for 'I' */ \
436 /* Step 3a: after processing first 'h' modifier. */ \
437 static JUMP_TABLE_TYPE step3a_jumps[30] = \
439 REF (form_unknown), \
440 REF (form_unknown), /* for ' ' */ \
441 REF (form_unknown), /* for '+' */ \
442 REF (form_unknown), /* for '-' */ \
443 REF (form_unknown), /* for '<hash>' */ \
444 REF (form_unknown), /* for '0' */ \
445 REF (form_unknown), /* for '\'' */ \
446 REF (form_unknown), /* for '*' */ \
447 REF (form_unknown), /* for '1'...'9' */ \
448 REF (form_unknown), /* for '.' */ \
449 REF (mod_halfhalf), /* for 'h' */ \
450 REF (form_unknown), /* for 'l' */ \
451 REF (form_unknown), /* for 'L', 'q' */ \
452 REF (form_unknown), /* for 'z', 'Z' */ \
453 REF (form_percent), /* for '%' */ \
454 REF (form_integer), /* for 'd', 'i' */ \
455 REF (form_unsigned), /* for 'u' */ \
456 REF (form_octal), /* for 'o' */ \
457 REF (form_hexa), /* for 'X', 'x' */ \
458 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
459 REF (form_unknown), /* for 'c' */ \
460 REF (form_unknown), /* for 's', 'S' */ \
461 REF (form_unknown), /* for 'p' */ \
462 REF (form_number), /* for 'n' */ \
463 REF (form_unknown), /* for 'm' */ \
464 REF (form_unknown), /* for 'C' */ \
465 REF (form_unknown), /* for 'A', 'a' */ \
466 REF (form_unknown), /* for 't' */ \
467 REF (form_unknown), /* for 'j' */ \
468 REF (form_unknown) /* for 'I' */ \
470 /* Step 3b: after processing first 'l' modifier. */ \
471 static JUMP_TABLE_TYPE step3b_jumps[30] = \
473 REF (form_unknown), \
474 REF (form_unknown), /* for ' ' */ \
475 REF (form_unknown), /* for '+' */ \
476 REF (form_unknown), /* for '-' */ \
477 REF (form_unknown), /* for '<hash>' */ \
478 REF (form_unknown), /* for '0' */ \
479 REF (form_unknown), /* for '\'' */ \
480 REF (form_unknown), /* for '*' */ \
481 REF (form_unknown), /* for '1'...'9' */ \
482 REF (form_unknown), /* for '.' */ \
483 REF (form_unknown), /* for 'h' */ \
484 REF (mod_longlong), /* for 'l' */ \
485 REF (form_unknown), /* for 'L', 'q' */ \
486 REF (form_unknown), /* for 'z', 'Z' */ \
487 REF (form_percent), /* for '%' */ \
488 REF (form_integer), /* for 'd', 'i' */ \
489 REF (form_unsigned), /* for 'u' */ \
490 REF (form_octal), /* for 'o' */ \
491 REF (form_hexa), /* for 'X', 'x' */ \
492 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
493 REF (form_character), /* for 'c' */ \
494 REF (form_string), /* for 's', 'S' */ \
495 REF (form_pointer), /* for 'p' */ \
496 REF (form_number), /* for 'n' */ \
497 REF (form_strerror), /* for 'm' */ \
498 REF (form_wcharacter), /* for 'C' */ \
499 REF (form_floathex), /* for 'A', 'a' */ \
500 REF (form_unknown), /* for 't' */ \
501 REF (form_unknown), /* for 'j' */ \
502 REF (form_unknown) /* for 'I' */ \
505 #define STEP4_TABLE \
506 /* Step 4: processing format specifier. */ \
507 static JUMP_TABLE_TYPE step4_jumps[30] = \
509 REF (form_unknown), \
510 REF (form_unknown), /* for ' ' */ \
511 REF (form_unknown), /* for '+' */ \
512 REF (form_unknown), /* for '-' */ \
513 REF (form_unknown), /* for '<hash>' */ \
514 REF (form_unknown), /* for '0' */ \
515 REF (form_unknown), /* for '\'' */ \
516 REF (form_unknown), /* for '*' */ \
517 REF (form_unknown), /* for '1'...'9' */ \
518 REF (form_unknown), /* for '.' */ \
519 REF (form_unknown), /* for 'h' */ \
520 REF (form_unknown), /* for 'l' */ \
521 REF (form_unknown), /* for 'L', 'q' */ \
522 REF (form_unknown), /* for 'z', 'Z' */ \
523 REF (form_percent), /* for '%' */ \
524 REF (form_integer), /* for 'd', 'i' */ \
525 REF (form_unsigned), /* for 'u' */ \
526 REF (form_octal), /* for 'o' */ \
527 REF (form_hexa), /* for 'X', 'x' */ \
528 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
529 REF (form_character), /* for 'c' */ \
530 REF (form_string), /* for 's', 'S' */ \
531 REF (form_pointer), /* for 'p' */ \
532 REF (form_number), /* for 'n' */ \
533 REF (form_strerror), /* for 'm' */ \
534 REF (form_wcharacter), /* for 'C' */ \
535 REF (form_floathex), /* for 'A', 'a' */ \
536 REF (form_unknown), /* for 't' */ \
537 REF (form_unknown), /* for 'j' */ \
538 REF (form_unknown) /* for 'I' */ \
542 #define process_arg(fspec) \
543 /* Start real work. We know about all flags and modifiers and \
544 now process the wanted format specifier. */ \
545 LABEL (form_percent): \
546 /* Write a literal "%". */ \
550 LABEL (form_integer): \
551 /* Signed decimal integer. */ \
556 long long int signed_number; \
559 signed_number = va_arg (ap, long long int); \
561 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
563 is_negative = signed_number < 0; \
564 number.longlong = is_negative ? (- signed_number) : signed_number; \
566 goto LABEL (longlong_number); \
570 long int signed_number; \
575 signed_number = va_arg (ap, long int); \
577 signed_number = (signed char) va_arg (ap, unsigned int); \
578 else if (!is_short) \
579 signed_number = va_arg (ap, int); \
581 signed_number = (short int) va_arg (ap, unsigned int); \
585 signed_number = args_value[fspec->data_arg].pa_long_int; \
587 signed_number = (signed char) \
588 args_value[fspec->data_arg].pa_u_int; \
589 else if (!is_short) \
590 signed_number = args_value[fspec->data_arg].pa_int; \
592 signed_number = (short int) \
593 args_value[fspec->data_arg].pa_u_int; \
595 is_negative = signed_number < 0; \
596 number.word = is_negative ? (- signed_number) : signed_number; \
598 goto LABEL (number); \
602 LABEL (form_unsigned): \
603 /* Unsigned decimal integer. */ \
605 goto LABEL (unsigned_number); \
608 LABEL (form_octal): \
609 /* Unsigned octal integer. */ \
611 goto LABEL (unsigned_number); \
615 /* Unsigned hexadecimal integer. */ \
618 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
620 /* ISO specifies the `+' and ` ' flags only for signed \
629 number.longlong = va_arg (ap, unsigned long long int); \
631 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
633 LABEL (longlong_number): \
635 /* Supply a default precision if none was given. */ \
638 /* We have to take care for the '0' flag. If a precision \
639 is given it must be ignored. */ \
642 /* If the precision is 0 and the number is 0 nothing has to \
643 be written for the number, except for the 'o' format in \
645 if (prec == 0 && number.longlong == 0) \
648 if (base == 8 && alt) \
649 *--string = L_('0'); \
653 /* Put the number in WORK. */ \
654 string = _itoa (number.longlong, workend, base, \
656 if (group && grouping) \
657 string = group_number (string, workend, grouping, \
660 if (use_outdigits && base == 10) \
661 string = _i18n_number_rewrite (string, workend, workend); \
663 /* Simplify further test for num != 0. */ \
664 number.word = number.longlong != 0; \
671 number.word = va_arg (ap, unsigned long int); \
673 number.word = (unsigned char) va_arg (ap, unsigned int); \
674 else if (!is_short) \
675 number.word = va_arg (ap, unsigned int); \
677 number.word = (unsigned short int) va_arg (ap, unsigned int); \
681 number.word = args_value[fspec->data_arg].pa_u_long_int; \
683 number.word = (unsigned char) \
684 args_value[fspec->data_arg].pa_u_int; \
685 else if (!is_short) \
686 number.word = args_value[fspec->data_arg].pa_u_int; \
688 number.word = (unsigned short int) \
689 args_value[fspec->data_arg].pa_u_int; \
693 /* Supply a default precision if none was given. */ \
696 /* We have to take care for the '0' flag. If a precision \
697 is given it must be ignored. */ \
700 /* If the precision is 0 and the number is 0 nothing has to \
701 be written for the number, except for the 'o' format in \
703 if (prec == 0 && number.word == 0) \
706 if (base == 8 && alt) \
707 *--string = L_('0'); \
711 /* Put the number in WORK. */ \
712 string = _itoa_word (number.word, workend, base, \
714 if (group && grouping) \
715 string = group_number (string, workend, grouping, \
718 if (use_outdigits && base == 10) \
719 string = _i18n_number_rewrite (string, workend, workend); \
723 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
724 /* Add octal marker. */ \
725 *--string = L_('0'); \
727 prec = MAX (0, prec - (workend - string)); \
731 width -= workend - string + prec; \
733 if (number.word != 0 && alt && base == 16) \
734 /* Account for 0X hex marker. */ \
737 if (is_negative || showsign || space) \
740 if (pad == L_(' ')) \
753 if (number.word != 0 && alt && base == 16) \
762 outstring (string, workend - string); \
784 if (number.word != 0 && alt && base == 16) \
791 width -= workend - string + prec; \
801 outstring (string, workend - string); \
807 LABEL (form_float): \
809 /* Floating-point number. This is handled by printf_fp.c. */ \
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 .i18n = use_outdigits, \
832 .wide = sizeof (CHAR_T) != 1 }; \
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_fp (s, &info, &ptr); \
844 ptr = (const void *) &args_value[fspec->data_arg]; \
847 fspec->data_arg_type = PA_DOUBLE; \
848 fspec->info.is_long_double = 0; \
851 function_done = __printf_fp (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_floathex): \
867 /* Floating point number printed as hexadecimal number. */ \
874 is_long_double = 0; \
876 struct printf_info info = { .prec = prec, \
879 .is_long_double = is_long_double, \
880 .is_short = is_short, \
881 .is_long = is_long, \
885 .showsign = showsign, \
889 .wide = sizeof (CHAR_T) != 1 }; \
891 if (is_long_double) \
892 the_arg.pa_long_double = va_arg (ap, long double); \
894 the_arg.pa_double = va_arg (ap, double); \
895 ptr = (const void *) &the_arg; \
897 function_done = __printf_fphex (s, &info, &ptr); \
901 ptr = (const void *) &args_value[fspec->data_arg]; \
903 fspec->info.is_long_double = 0; \
905 function_done = __printf_fphex (s, &fspec->info, &ptr); \
908 if (function_done < 0) \
910 /* Error in print handler; up to handler to set errno. */ \
915 done_add (function_done); \
919 LABEL (form_pointer): \
920 /* Generic pointer. */ \
924 ptr = va_arg (ap, void *); \
926 ptr = args_value[fspec->data_arg].pa_pointer; \
929 /* If the pointer is not NULL, write it as a %#x spec. */ \
931 number.word = (unsigned long int) ptr; \
936 goto LABEL (number); \
940 /* Write "(nil)" for a nil pointer. */ \
941 string = (CHAR_T *) L_("(nil)"); \
942 /* Make sure the full string "(nil)" is printed. */ \
945 /* This is a wide string iff compiling wprintf. */ \
946 is_long = sizeof (CHAR_T) > 1; \
947 goto LABEL (print_string); \
952 LABEL (form_number): \
953 if (s->_flags2 & _IO_FLAGS2_FORTIFY) \
955 if (! readonly_format) \
957 extern int __readonly_area (const void *, size_t) \
960 = __readonly_area (format, ((STR_LEN (format) + 1) \
961 * sizeof (CHAR_T))); \
963 if (readonly_format < 0) \
964 __libc_fatal ("*** %n in writable segment detected ***\n"); \
966 /* Answer the count of characters written. */ \
970 *(long long int *) va_arg (ap, void *) = done; \
971 else if (is_long_num) \
972 *(long int *) va_arg (ap, void *) = done; \
974 *(char *) va_arg (ap, void *) = done; \
975 else if (!is_short) \
976 *(int *) va_arg (ap, void *) = done; \
978 *(short int *) va_arg (ap, void *) = done; \
982 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
983 else if (is_long_num) \
984 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
986 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
987 else if (!is_short) \
988 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
990 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
993 LABEL (form_strerror): \
994 /* Print description of error ERRNO. */ \
996 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
997 sizeof work_buffer); \
998 is_long = 0; /* This is no wide-char string. */ \
999 goto LABEL (print_string)
1001 #ifdef COMPILE_WPRINTF
1002 # define process_string_arg(fspec) \
1003 LABEL (form_character): \
1006 goto LABEL (form_wcharacter); \
1007 --width; /* Account for the character itself. */ \
1010 if (fspec == NULL) \
1011 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
1013 outchar (__btowc ((unsigned char) \
1014 args_value[fspec->data_arg].pa_int)); \
1019 LABEL (form_wcharacter): \
1021 /* Wide character. */ \
1025 if (fspec == NULL) \
1026 outchar (va_arg (ap, wchar_t)); \
1028 outchar (args_value[fspec->data_arg].pa_wchar); \
1034 LABEL (form_string): \
1037 int string_malloced; \
1039 /* The string argument could in fact be `char *' or `wchar_t *'. \
1040 But this should not make a difference here. */ \
1041 if (fspec == NULL) \
1042 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
1044 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
1046 /* Entry point for printing other strings. */ \
1047 LABEL (print_string): \
1049 string_malloced = 0; \
1050 if (string == NULL) \
1052 /* Write "(null)" if there's space. */ \
1054 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
1056 string = (CHAR_T *) null; \
1057 len = (sizeof (null) / sizeof (null[0])) - 1; \
1061 string = (CHAR_T *) L""; \
1065 else if (!is_long && spec != L_('S')) \
1067 /* This is complicated. We have to transform the multibyte \
1068 string into a wide character string. */ \
1069 const char *mbs = (const char *) string; \
1070 mbstate_t mbstate; \
1072 len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
1074 /* Allocate dynamically an array which definitely is long \
1075 enough for the wide character version. Each byte in the \
1076 multi-byte string can produce at most one wide character. */ \
1077 if (__glibc_unlikely (len > SIZE_MAX / sizeof (wchar_t))) \
1079 __set_errno (EOVERFLOW); \
1083 else if (__libc_use_alloca (len * sizeof (wchar_t))) \
1084 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1085 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1092 string_malloced = 1; \
1094 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1095 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1096 if (len == (size_t) -1) \
1098 /* Illegal multibyte character. */ \
1106 /* Search for the end of the string, but don't search past \
1107 the length specified by the precision. */ \
1108 len = __wcsnlen (string, prec); \
1110 len = __wcslen (string); \
1113 if ((width -= len) < 0) \
1115 outstring (string, len); \
1121 outstring (string, len); \
1124 if (__glibc_unlikely (string_malloced)) \
1129 # define process_string_arg(fspec) \
1130 LABEL (form_character): \
1133 goto LABEL (form_wcharacter); \
1134 --width; /* Account for the character itself. */ \
1137 if (fspec == NULL) \
1138 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1140 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1145 LABEL (form_wcharacter): \
1147 /* Wide character. */ \
1148 char buf[MB_CUR_MAX]; \
1149 mbstate_t mbstate; \
1152 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1153 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1154 : args_value[fspec->data_arg].pa_wchar), \
1156 if (len == (size_t) -1) \
1158 /* Something went wrong during the conversion. Bail out. */ \
1165 outstring (buf, len); \
1171 LABEL (form_string): \
1174 int string_malloced; \
1176 /* The string argument could in fact be `char *' or `wchar_t *'. \
1177 But this should not make a difference here. */ \
1178 if (fspec == NULL) \
1179 string = (char *) va_arg (ap, const char *); \
1181 string = (char *) args_value[fspec->data_arg].pa_string; \
1183 /* Entry point for printing other strings. */ \
1184 LABEL (print_string): \
1186 string_malloced = 0; \
1187 if (string == NULL) \
1189 /* Write "(null)" if there's space. */ \
1190 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1192 string = (char *) null; \
1193 len = sizeof (null) - 1; \
1197 string = (char *) ""; \
1201 else if (!is_long && spec != L_('S')) \
1204 /* Search for the end of the string, but don't search past \
1205 the length (in bytes) specified by the precision. */ \
1206 len = __strnlen (string, prec); \
1208 len = strlen (string); \
1212 const wchar_t *s2 = (const wchar_t *) string; \
1213 mbstate_t mbstate; \
1215 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1219 /* The string `s2' might not be NUL terminated. */ \
1220 if (__libc_use_alloca (prec)) \
1221 string = (char *) alloca (prec); \
1222 else if ((string = (char *) malloc (prec)) == NULL) \
1228 string_malloced = 1; \
1229 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1233 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1234 if (len != (size_t) -1) \
1236 assert (__mbsinit (&mbstate)); \
1237 s2 = (const wchar_t *) string; \
1238 if (__libc_use_alloca (len + 1)) \
1239 string = (char *) alloca (len + 1); \
1240 else if ((string = (char *) malloc (len + 1)) == NULL) \
1246 string_malloced = 1; \
1247 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1251 if (len == (size_t) -1) \
1253 /* Illegal wide-character string. */ \
1259 if ((width -= len) < 0) \
1261 outstring (string, len); \
1267 outstring (string, len); \
1270 if (__glibc_unlikely (string_malloced)) \
1276 /* Orient the stream. */
1281 /* Sanity check of arguments. */
1282 ARGCHECK (s
, format
);
1285 /* Check for correct orientation. */
1286 if (_IO_vtable_offset (s
) == 0 &&
1287 _IO_fwide (s
, sizeof (CHAR_T
) == 1 ? -1 : 1)
1288 != (sizeof (CHAR_T
) == 1 ? -1 : 1))
1289 /* The stream is already oriented otherwise. */
1293 if (UNBUFFERED_P (s
))
1294 /* Use a helper function which will allocate a local temporary buffer
1295 for the stream and then call us again. */
1296 return buffered_vfprintf (s
, format
, ap
);
1298 /* Initialize local variables. */
1300 grouping
= (const char *) -1;
1302 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1303 since on some systems `va_list' is not an integral type. */
1304 __va_copy (ap_save
, ap
);
1310 #ifdef COMPILE_WPRINTF
1311 /* Find the first format specifier. */
1312 f
= lead_str_end
= __find_specwc ((const UCHAR_T
*) format
);
1314 /* Find the first format specifier. */
1315 f
= lead_str_end
= __find_specmb ((const UCHAR_T
*) format
);
1319 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile
, s
);
1322 /* Write the literal text before the first format. */
1323 outstring ((const UCHAR_T
*) format
,
1324 lead_str_end
- (const UCHAR_T
*) format
);
1326 /* If we only have to print a simple string, return now. */
1330 /* Use the slow path in case any printf handler is registered. */
1331 if (__glibc_unlikely (__printf_function_table
!= NULL
1332 || __printf_modifier_table
!= NULL
1333 || __printf_va_arg_table
!= NULL
))
1336 /* Process whole format string. */
1340 # define REF(Name) &&do_##Name - &&do_form_unknown
1342 # define REF(Name) &&do_##Name
1344 #define LABEL(Name) do_##Name
1348 union printf_arg
*args_value
; /* This is not used here but ... */
1349 int is_negative
; /* Flag for negative number. */
1352 unsigned long long int longlong
;
1353 unsigned long int word
;
1356 union printf_arg the_arg
;
1357 CHAR_T
*string
; /* Pointer to argument string. */
1358 int alt
= 0; /* Alternate format. */
1359 int space
= 0; /* Use space prefix if no sign is needed. */
1360 int left
= 0; /* Left-justify output. */
1361 int showsign
= 0; /* Always begin with plus or minus sign. */
1362 int group
= 0; /* Print numbers according grouping rules. */
1363 int is_long_double
= 0; /* Argument is long double/ long long int. */
1364 int is_short
= 0; /* Argument is short int. */
1365 int is_long
= 0; /* Argument is long int. */
1366 int is_char
= 0; /* Argument is promoted (unsigned) char. */
1367 int width
= 0; /* Width of output; 0 means none specified. */
1368 int prec
= -1; /* Precision of output; -1 means none specified. */
1369 /* This flag is set by the 'I' modifier and selects the use of the
1370 `outdigits' as determined by the current locale. */
1371 int use_outdigits
= 0;
1372 UCHAR_T pad
= L_(' ');/* Padding character. */
1376 workend
= &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
1378 /* Get current character in format string. */
1379 JUMP (*++f
, step0_jumps
);
1384 JUMP (*++f
, step0_jumps
);
1389 JUMP (*++f
, step0_jumps
);
1395 JUMP (*++f
, step0_jumps
);
1400 JUMP (*++f
, step0_jumps
);
1406 JUMP (*++f
, step0_jumps
);
1408 /* The '\'' flag. */
1412 if (grouping
== (const char *) -1)
1414 #ifdef COMPILE_WPRINTF
1415 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1416 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1418 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1421 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1422 if (*grouping
== '\0' || *grouping
== CHAR_MAX
1423 #ifdef COMPILE_WPRINTF
1424 || thousands_sep
== L
'\0'
1426 || *thousands_sep
== '\0'
1431 JUMP (*++f
, step0_jumps
);
1435 JUMP (*++f
, step0_jumps
);
1437 /* Get width from argument. */
1438 LABEL (width_asterics
):
1440 const UCHAR_T
*tmp
; /* Temporary value. */
1445 int pos
= read_int (&tmp
);
1449 __set_errno (EOVERFLOW
);
1454 if (pos
&& *tmp
== L_('$'))
1455 /* The width comes from a positional parameter. */
1458 width
= va_arg (ap
, int);
1460 /* Negative width means left justified. */
1468 if (__glibc_unlikely (width
>= INT_MAX
/ sizeof (CHAR_T
) - 32))
1470 __set_errno (EOVERFLOW
);
1475 if (width
>= sizeof (work_buffer
) / sizeof (work_buffer
[0]) - 32)
1477 /* We have to use a special buffer. The "32" is just a safe
1478 bet for all the output which is not counted in the width. */
1479 size_t needed
= ((size_t) width
+ 32) * sizeof (CHAR_T
);
1480 if (__libc_use_alloca (needed
))
1481 workend
= (CHAR_T
*) alloca (needed
) + width
+ 32;
1484 workstart
= (CHAR_T
*) malloc (needed
);
1485 if (workstart
== NULL
)
1490 workend
= workstart
+ width
+ 32;
1494 JUMP (*f
, step1_jumps
);
1496 /* Given width in format string. */
1498 width
= read_int (&f
);
1500 if (__glibc_unlikely (width
== -1
1501 || width
>= INT_MAX
/ sizeof (CHAR_T
) - 32))
1503 __set_errno (EOVERFLOW
);
1508 if (width
>= sizeof (work_buffer
) / sizeof (work_buffer
[0]) - 32)
1510 /* We have to use a special buffer. The "32" is just a safe
1511 bet for all the output which is not counted in the width. */
1512 size_t needed
= ((size_t) width
+ 32) * sizeof (CHAR_T
);
1513 if (__libc_use_alloca (needed
))
1514 workend
= (CHAR_T
*) alloca (needed
) + width
+ 32;
1517 workstart
= (CHAR_T
*) malloc (needed
);
1518 if (workstart
== NULL
)
1523 workend
= workstart
+ width
+ 32;
1527 /* Oh, oh. The argument comes from a positional parameter. */
1529 JUMP (*f
, step1_jumps
);
1535 const UCHAR_T
*tmp
; /* Temporary value. */
1540 int pos
= read_int (&tmp
);
1544 __set_errno (EOVERFLOW
);
1549 if (pos
&& *tmp
== L_('$'))
1550 /* The precision comes from a positional parameter. */
1553 prec
= va_arg (ap
, int);
1555 /* If the precision is negative the precision is omitted. */
1559 else if (ISDIGIT (*f
))
1561 prec
= read_int (&f
);
1563 /* The precision was specified in this case as an extremely
1564 large positive value. */
1567 __set_errno (EOVERFLOW
);
1575 && prec
> sizeof (work_buffer
) / sizeof (work_buffer
[0]) - 32)
1577 if (__glibc_unlikely (prec
>= INT_MAX
/ sizeof (CHAR_T
) - 32))
1579 __set_errno (EOVERFLOW
);
1583 size_t needed
= ((size_t) prec
+ 32) * sizeof (CHAR_T
);
1585 if (__libc_use_alloca (needed
))
1586 workend
= (CHAR_T
*) alloca (needed
) + prec
+ 32;
1589 workstart
= (CHAR_T
*) malloc (needed
);
1590 if (workstart
== NULL
)
1595 workend
= workstart
+ prec
+ 32;
1598 JUMP (*f
, step2_jumps
);
1600 /* Process 'h' modifier. There might another 'h' following. */
1603 JUMP (*++f
, step3a_jumps
);
1605 /* Process 'hh' modifier. */
1606 LABEL (mod_halfhalf
):
1609 JUMP (*++f
, step4_jumps
);
1611 /* Process 'l' modifier. There might another 'l' following. */
1614 JUMP (*++f
, step3b_jumps
);
1616 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1617 allowed to follow. */
1618 LABEL (mod_longlong
):
1621 JUMP (*++f
, step4_jumps
);
1624 is_long_double
= sizeof (size_t) > sizeof (unsigned long int);
1625 is_long
= sizeof (size_t) > sizeof (unsigned int);
1626 JUMP (*++f
, step4_jumps
);
1628 LABEL (mod_ptrdiff_t
):
1629 is_long_double
= sizeof (ptrdiff_t) > sizeof (unsigned long int);
1630 is_long
= sizeof (ptrdiff_t) > sizeof (unsigned int);
1631 JUMP (*++f
, step4_jumps
);
1633 LABEL (mod_intmax_t
):
1634 is_long_double
= sizeof (intmax_t) > sizeof (unsigned long int);
1635 is_long
= sizeof (intmax_t) > sizeof (unsigned int);
1636 JUMP (*++f
, step4_jumps
);
1638 /* Process current format. */
1641 process_arg (((struct printf_spec
*) NULL
));
1642 process_string_arg (((struct printf_spec
*) NULL
));
1644 LABEL (form_unknown
):
1645 if (spec
== L_('\0'))
1647 /* The format string ended before the specifier is complete. */
1648 __set_errno (EINVAL
);
1653 /* If we are in the fast loop force entering the complicated
1658 /* The format is correctly handled. */
1661 if (__glibc_unlikely (workstart
!= NULL
))
1665 /* Look for next format specifier. */
1666 #ifdef COMPILE_WPRINTF
1667 f
= __find_specwc ((end_of_spec
= ++f
));
1669 f
= __find_specmb ((end_of_spec
= ++f
));
1672 /* Write the following constant string. */
1673 outstring (end_of_spec
, f
- end_of_spec
);
1675 while (*f
!= L_('\0'));
1677 /* Unlock stream and return. */
1680 /* Here starts the more complex loop to handle positional parameters. */
1683 /* Array with information about the needed arguments. This has to
1684 be dynamically extensible. */
1686 /* A more or less arbitrary start value. */
1687 size_t nspecs_size
= 32 * sizeof (struct printf_spec
);
1689 specs
= alloca (nspecs_size
);
1690 /* The number of arguments the format string requests. This will
1691 determine the size of the array needed to store the argument
1694 size_t bytes_per_arg
;
1695 union printf_arg
*args_value
;
1699 /* Positional parameters refer to arguments directly. This could
1700 also determine the maximum number of arguments. Track the
1702 size_t max_ref_arg
= 0;
1704 /* Just a counter. */
1707 if (__glibc_unlikely (workstart
!= NULL
))
1711 if (grouping
== (const char *) -1)
1713 #ifdef COMPILE_WPRINTF
1714 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1715 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1717 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1720 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1721 if (*grouping
== '\0' || *grouping
== CHAR_MAX
)
1725 for (f
= lead_str_end
; *f
!= L_('\0'); f
= specs
[nspecs
++].next_fmt
)
1727 if (nspecs
* sizeof (*specs
) >= nspecs_size
)
1729 /* Extend the array of format specifiers. */
1730 if (nspecs_size
* 2 < nspecs_size
)
1732 __set_errno (ENOMEM
);
1736 struct printf_spec
*old
= specs
;
1737 if (__libc_use_alloca (2 * nspecs_size
))
1738 specs
= extend_alloca (specs
, nspecs_size
, 2 * nspecs_size
);
1742 specs
= malloc (nspecs_size
);
1745 __set_errno (ENOMEM
);
1752 /* Copy the old array's elements to the new space. */
1753 memmove (specs
, old
, nspecs
* sizeof (*specs
));
1755 /* If we had previously malloc'd space for SPECS, then
1756 release it after the copy is complete. */
1760 /* Now set SPECS_MALLOCED if needed. */
1761 if (!__libc_use_alloca (nspecs_size
))
1762 specs_malloced
= true;
1765 /* Parse the format specifier. */
1766 #ifdef COMPILE_WPRINTF
1767 nargs
+= __parse_one_specwc (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1769 nargs
+= __parse_one_specmb (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1773 /* Determine the number of arguments the format string consumes. */
1774 nargs
= MAX (nargs
, max_ref_arg
);
1775 /* Calculate total size needed to represent a single argument across
1776 all three argument-related arrays. */
1777 bytes_per_arg
= (sizeof (*args_value
) + sizeof (*args_size
)
1778 + sizeof (*args_type
));
1780 /* Check for potential integer overflow. */
1781 if (__glibc_unlikely (nargs
> INT_MAX
/ bytes_per_arg
))
1783 __set_errno (EOVERFLOW
);
1788 /* Allocate memory for all three argument arrays. */
1789 if (__libc_use_alloca (nargs
* bytes_per_arg
))
1790 args_value
= alloca (nargs
* bytes_per_arg
);
1793 args_value
= args_malloced
= malloc (nargs
* bytes_per_arg
);
1794 if (args_value
== NULL
)
1801 /* Set up the remaining two arrays to each point past the end of the
1802 prior array, since space for all three has been allocated now. */
1803 args_size
= &args_value
[nargs
].pa_int
;
1804 args_type
= &args_size
[nargs
];
1805 memset (args_type
, s
->_flags2
& _IO_FLAGS2_FORTIFY
? '\xff' : '\0',
1806 nargs
* sizeof (*args_type
));
1808 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1809 still zero after this loop, format is invalid. For now we
1810 simply use 0 as the value. */
1812 /* Fill in the types of all the arguments. */
1813 for (cnt
= 0; cnt
< nspecs
; ++cnt
)
1815 /* If the width is determined by an argument this is an int. */
1816 if (specs
[cnt
].width_arg
!= -1)
1817 args_type
[specs
[cnt
].width_arg
] = PA_INT
;
1819 /* If the precision is determined by an argument this is an int. */
1820 if (specs
[cnt
].prec_arg
!= -1)
1821 args_type
[specs
[cnt
].prec_arg
] = PA_INT
;
1823 switch (specs
[cnt
].ndata_args
)
1825 case 0: /* No arguments. */
1827 case 1: /* One argument; we already have the
1829 args_type
[specs
[cnt
].data_arg
] = specs
[cnt
].data_arg_type
;
1830 args_size
[specs
[cnt
].data_arg
] = specs
[cnt
].size
;
1833 /* We have more than one argument for this format spec.
1834 We must call the arginfo function again to determine
1836 (void) (*__printf_arginfo_table
[specs
[cnt
].info
.spec
])
1838 specs
[cnt
].ndata_args
, &args_type
[specs
[cnt
].data_arg
],
1839 &args_size
[specs
[cnt
].data_arg
]);
1844 /* Now we know all the types and the order. Fill in the argument
1846 for (cnt
= 0; cnt
< nargs
; ++cnt
)
1847 switch (args_type
[cnt
])
1849 #define T(tag, mem, type) \
1851 args_value[cnt].mem = va_arg (ap_save, type); \
1854 T (PA_WCHAR
, pa_wchar
, wint_t);
1855 case PA_CHAR
: /* Promoted. */
1856 case PA_INT
|PA_FLAG_SHORT
: /* Promoted. */
1857 #if LONG_MAX == INT_MAX
1858 case PA_INT
|PA_FLAG_LONG
:
1860 T (PA_INT
, pa_int
, int);
1861 #if LONG_MAX == LONG_LONG_MAX
1862 case PA_INT
|PA_FLAG_LONG
:
1864 T (PA_INT
|PA_FLAG_LONG_LONG
, pa_long_long_int
, long long int);
1865 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1868 case PA_FLOAT
: /* Promoted. */
1869 T (PA_DOUBLE
, pa_double
, double);
1870 case PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
:
1873 args_value
[cnt
].pa_double
= va_arg (ap_save
, double);
1874 args_type
[cnt
] &= ~PA_FLAG_LONG_DOUBLE
;
1877 args_value
[cnt
].pa_long_double
= va_arg (ap_save
, long double);
1879 case PA_STRING
: /* All pointers are the same */
1880 case PA_WSTRING
: /* All pointers are the same */
1881 T (PA_POINTER
, pa_pointer
, void *);
1884 if ((args_type
[cnt
] & PA_FLAG_PTR
) != 0)
1885 args_value
[cnt
].pa_pointer
= va_arg (ap_save
, void *);
1886 else if (__glibc_unlikely (__printf_va_arg_table
!= NULL
)
1887 && __printf_va_arg_table
[args_type
[cnt
] - PA_LAST
] != NULL
)
1889 args_value
[cnt
].pa_user
= alloca (args_size
[cnt
]);
1890 (*__printf_va_arg_table
[args_type
[cnt
] - PA_LAST
])
1891 (args_value
[cnt
].pa_user
, &ap_save
);
1894 args_value
[cnt
].pa_long_double
= 0.0;
1897 /* Error case. Not all parameters appear in N$ format
1898 strings. We have no way to determine their type. */
1899 assert (s
->_flags2
& _IO_FLAGS2_FORTIFY
);
1900 __libc_fatal ("*** invalid %N$ use detected ***\n");
1903 /* Now walk through all format specifiers and process them. */
1904 for (; (size_t) nspecs_done
< nspecs
; ++nspecs_done
)
1908 # define REF(Name) &&do2_##Name - &&do_form_unknown
1910 # define REF(Name) &&do2_##Name
1913 #define LABEL(Name) do2_##Name
1919 unsigned long long int longlong
;
1920 unsigned long int word
;
1923 union printf_arg the_arg
;
1924 CHAR_T
*string
; /* Pointer to argument string. */
1926 /* Fill variables from values in struct. */
1927 int alt
= specs
[nspecs_done
].info
.alt
;
1928 int space
= specs
[nspecs_done
].info
.space
;
1929 int left
= specs
[nspecs_done
].info
.left
;
1930 int showsign
= specs
[nspecs_done
].info
.showsign
;
1931 int group
= specs
[nspecs_done
].info
.group
;
1932 int is_long_double
= specs
[nspecs_done
].info
.is_long_double
;
1933 int is_short
= specs
[nspecs_done
].info
.is_short
;
1934 int is_char
= specs
[nspecs_done
].info
.is_char
;
1935 int is_long
= specs
[nspecs_done
].info
.is_long
;
1936 int width
= specs
[nspecs_done
].info
.width
;
1937 int prec
= specs
[nspecs_done
].info
.prec
;
1938 int use_outdigits
= specs
[nspecs_done
].info
.i18n
;
1939 char pad
= specs
[nspecs_done
].info
.pad
;
1940 CHAR_T spec
= specs
[nspecs_done
].info
.spec
;
1943 workend
= &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
1945 /* Fill in last information. */
1946 if (specs
[nspecs_done
].width_arg
!= -1)
1948 /* Extract the field width from an argument. */
1949 specs
[nspecs_done
].info
.width
=
1950 args_value
[specs
[nspecs_done
].width_arg
].pa_int
;
1952 if (specs
[nspecs_done
].info
.width
< 0)
1953 /* If the width value is negative left justification is
1954 selected and the value is taken as being positive. */
1956 specs
[nspecs_done
].info
.width
*= -1;
1957 left
= specs
[nspecs_done
].info
.left
= 1;
1959 width
= specs
[nspecs_done
].info
.width
;
1962 if (specs
[nspecs_done
].prec_arg
!= -1)
1964 /* Extract the precision from an argument. */
1965 specs
[nspecs_done
].info
.prec
=
1966 args_value
[specs
[nspecs_done
].prec_arg
].pa_int
;
1968 if (specs
[nspecs_done
].info
.prec
< 0)
1969 /* If the precision is negative the precision is
1971 specs
[nspecs_done
].info
.prec
= -1;
1973 prec
= specs
[nspecs_done
].info
.prec
;
1976 /* Maybe the buffer is too small. */
1977 if (MAX (prec
, width
) + 32 > (int) (sizeof (work_buffer
)
1980 if (__libc_use_alloca ((MAX (prec
, width
) + 32)
1982 workend
= ((CHAR_T
*) alloca ((MAX (prec
, width
) + 32)
1984 + (MAX (prec
, width
) + 32));
1987 workstart
= (CHAR_T
*) malloc ((MAX (prec
, width
) + 32)
1989 if (workstart
== NULL
)
1994 workend
= workstart
+ (MAX (prec
, width
) + 32);
1998 /* Process format specifiers. */
2001 extern printf_function
**__printf_function_table
;
2004 if (spec
<= UCHAR_MAX
2005 && __printf_function_table
!= NULL
2006 && __printf_function_table
[(size_t) spec
] != NULL
)
2008 const void **ptr
= alloca (specs
[nspecs_done
].ndata_args
2009 * sizeof (const void *));
2011 /* Fill in an array of pointers to the argument values. */
2012 for (unsigned int i
= 0; i
< specs
[nspecs_done
].ndata_args
;
2014 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
2016 /* Call the function. */
2017 function_done
= __printf_function_table
[(size_t) spec
]
2018 (s
, &specs
[nspecs_done
].info
, ptr
);
2020 if (function_done
!= -2)
2022 /* If an error occurred we don't have information
2023 about # of chars. */
2024 if (function_done
< 0)
2026 /* Function has set errno. */
2031 done_add (function_done
);
2036 JUMP (spec
, step4_jumps
);
2038 process_arg ((&specs
[nspecs_done
]));
2039 process_string_arg ((&specs
[nspecs_done
]));
2041 LABEL (form_unknown
):
2046 ptr
= alloca (specs
[nspecs_done
].ndata_args
2047 * sizeof (const void *));
2049 /* Fill in an array of pointers to the argument values. */
2050 for (i
= 0; i
< specs
[nspecs_done
].ndata_args
; ++i
)
2051 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
2053 /* Call the function. */
2054 function_done
= printf_unknown (s
, &specs
[nspecs_done
].info
,
2057 /* If an error occurred we don't have information about #
2059 if (function_done
< 0)
2061 /* Function has set errno. */
2066 done_add (function_done
);
2071 if (__glibc_unlikely (workstart
!= NULL
))
2075 /* Write the following constant string. */
2076 outstring (specs
[nspecs_done
].end_of_fmt
,
2077 specs
[nspecs_done
].next_fmt
2078 - specs
[nspecs_done
].end_of_fmt
);
2085 if (__glibc_unlikely (args_malloced
!= NULL
))
2086 free (args_malloced
);
2087 if (__glibc_unlikely (workstart
!= NULL
))
2089 /* Unlock the stream. */
2090 _IO_funlockfile (s
);
2091 _IO_cleanup_region_end (0);
2096 /* Handle an unknown format specifier. This prints out a canonicalized
2097 representation of the format spec itself. */
2099 printf_unknown (FILE *s
, const struct printf_info
*info
,
2100 const void *const *args
)
2104 CHAR_T work_buffer
[MAX (sizeof (info
->width
), sizeof (info
->prec
)) * 3];
2105 CHAR_T
*const workend
2106 = &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
2117 else if (info
->space
)
2121 if (info
->pad
== L_('0'))
2126 if (info
->width
!= 0)
2128 w
= _itoa_word (info
->width
, workend
, 10, 0);
2133 if (info
->prec
!= -1)
2136 w
= _itoa_word (info
->prec
, workend
, 10, 0);
2141 if (info
->spec
!= L_('\0'))
2142 outchar (info
->spec
);
2148 /* Group the digits according to the grouping rules of the current locale.
2149 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
2152 group_number (CHAR_T
*w
, CHAR_T
*rear_ptr
, const char *grouping
,
2153 #ifdef COMPILE_WPRINTF
2154 wchar_t thousands_sep
2156 const char *thousands_sep
2162 #ifndef COMPILE_WPRINTF
2163 int tlen
= strlen (thousands_sep
);
2166 /* We treat all negative values like CHAR_MAX. */
2168 if (*grouping
== CHAR_MAX
|| *grouping
<= 0)
2169 /* No grouping should be done. */
2174 /* Copy existing string so that nothing gets overwritten. */
2175 src
= (CHAR_T
*) alloca ((rear_ptr
- w
) * sizeof (CHAR_T
));
2176 s
= (CHAR_T
*) __mempcpy (src
, w
,
2177 (rear_ptr
- w
) * sizeof (CHAR_T
));
2180 /* Process all characters in the string. */
2185 if (--len
== 0 && s
> src
)
2187 /* A new group begins. */
2188 #ifdef COMPILE_WPRINTF
2189 *--w
= thousands_sep
;
2193 *--w
= thousands_sep
[--cnt
];
2197 if (*grouping
== CHAR_MAX
2203 /* No further grouping to be done.
2204 Copy the rest of the number. */
2210 else if (*grouping
!= '\0')
2211 /* The previous grouping repeats ad infinitum. */
2220 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2223 struct _IO_FILE_plus _f
;
2224 #ifdef COMPILE_WPRINTF
2225 struct _IO_wide_data _wide_data
;
2227 _IO_FILE
*_put_stream
;
2228 #ifdef _IO_MTSAFE_IO
2234 _IO_helper_overflow (_IO_FILE
*s
, int c
)
2236 _IO_FILE
*target
= ((struct helper_file
*) s
)->_put_stream
;
2237 #ifdef COMPILE_WPRINTF
2238 int used
= s
->_wide_data
->_IO_write_ptr
- s
->_wide_data
->_IO_write_base
;
2241 _IO_size_t written
= _IO_sputn (target
, s
->_wide_data
->_IO_write_base
,
2243 if (written
== 0 || written
== WEOF
)
2245 __wmemmove (s
->_wide_data
->_IO_write_base
,
2246 s
->_wide_data
->_IO_write_base
+ written
,
2248 s
->_wide_data
->_IO_write_ptr
-= written
;
2251 int used
= s
->_IO_write_ptr
- s
->_IO_write_base
;
2254 _IO_size_t written
= _IO_sputn (target
, s
->_IO_write_base
, used
);
2255 if (written
== 0 || written
== EOF
)
2257 memmove (s
->_IO_write_base
, s
->_IO_write_base
+ written
,
2259 s
->_IO_write_ptr
-= written
;
2265 #ifdef COMPILE_WPRINTF
2266 static const struct _IO_jump_t _IO_helper_jumps
=
2269 JUMP_INIT (finish
, _IO_wdefault_finish
),
2270 JUMP_INIT (overflow
, _IO_helper_overflow
),
2271 JUMP_INIT (underflow
, _IO_default_underflow
),
2272 JUMP_INIT (uflow
, _IO_default_uflow
),
2273 JUMP_INIT (pbackfail
, (_IO_pbackfail_t
) _IO_wdefault_pbackfail
),
2274 JUMP_INIT (xsputn
, _IO_wdefault_xsputn
),
2275 JUMP_INIT (xsgetn
, _IO_wdefault_xsgetn
),
2276 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2277 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2278 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2279 JUMP_INIT (sync
, _IO_default_sync
),
2280 JUMP_INIT (doallocate
, _IO_wdefault_doallocate
),
2281 JUMP_INIT (read
, _IO_default_read
),
2282 JUMP_INIT (write
, _IO_default_write
),
2283 JUMP_INIT (seek
, _IO_default_seek
),
2284 JUMP_INIT (close
, _IO_default_close
),
2285 JUMP_INIT (stat
, _IO_default_stat
)
2288 static const struct _IO_jump_t _IO_helper_jumps
=
2291 JUMP_INIT (finish
, _IO_default_finish
),
2292 JUMP_INIT (overflow
, _IO_helper_overflow
),
2293 JUMP_INIT (underflow
, _IO_default_underflow
),
2294 JUMP_INIT (uflow
, _IO_default_uflow
),
2295 JUMP_INIT (pbackfail
, _IO_default_pbackfail
),
2296 JUMP_INIT (xsputn
, _IO_default_xsputn
),
2297 JUMP_INIT (xsgetn
, _IO_default_xsgetn
),
2298 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2299 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2300 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2301 JUMP_INIT (sync
, _IO_default_sync
),
2302 JUMP_INIT (doallocate
, _IO_default_doallocate
),
2303 JUMP_INIT (read
, _IO_default_read
),
2304 JUMP_INIT (write
, _IO_default_write
),
2305 JUMP_INIT (seek
, _IO_default_seek
),
2306 JUMP_INIT (close
, _IO_default_close
),
2307 JUMP_INIT (stat
, _IO_default_stat
)
2313 buffered_vfprintf (_IO_FILE
*s
, const CHAR_T
*format
,
2316 CHAR_T buf
[_IO_BUFSIZ
];
2317 struct helper_file helper
;
2318 _IO_FILE
*hp
= (_IO_FILE
*) &helper
._f
;
2319 int result
, to_flush
;
2321 /* Orient the stream. */
2326 /* Initialize helper. */
2327 helper
._put_stream
= s
;
2328 #ifdef COMPILE_WPRINTF
2329 hp
->_wide_data
= &helper
._wide_data
;
2330 _IO_wsetp (hp
, buf
, buf
+ sizeof buf
/ sizeof (CHAR_T
));
2333 _IO_setp (hp
, buf
, buf
+ sizeof buf
);
2336 hp
->_IO_file_flags
= _IO_MAGIC
|_IO_NO_READS
|_IO_USER_LOCK
;
2337 #if _IO_JUMPS_OFFSET
2338 hp
->_vtable_offset
= 0;
2340 #ifdef _IO_MTSAFE_IO
2343 hp
->_flags2
= s
->_flags2
;
2344 _IO_JUMPS (&helper
._f
) = (struct _IO_jump_t
*) &_IO_helper_jumps
;
2346 /* Now print to helper instead. */
2347 #ifndef COMPILE_WPRINTF
2348 result
= _IO_vfprintf (hp
, format
, args
);
2350 result
= vfprintf (hp
, format
, args
);
2354 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile
, s
);
2357 /* Now flush anything from the helper to the S. */
2358 #ifdef COMPILE_WPRINTF
2359 if ((to_flush
= (hp
->_wide_data
->_IO_write_ptr
2360 - hp
->_wide_data
->_IO_write_base
)) > 0)
2362 if ((int) _IO_sputn (s
, hp
->_wide_data
->_IO_write_base
, to_flush
)
2367 if ((to_flush
= hp
->_IO_write_ptr
- hp
->_IO_write_base
) > 0)
2369 if ((int) _IO_sputn (s
, hp
->_IO_write_base
, to_flush
) != to_flush
)
2374 /* Unlock the stream. */
2375 _IO_funlockfile (s
);
2376 __libc_cleanup_region_end (0);
2382 #ifdef COMPILE_WPRINTF
2383 strong_alias (_IO_vfwprintf
, __vfwprintf
);
2384 ldbl_weak_alias (_IO_vfwprintf
, vfwprintf
);
2386 ldbl_strong_alias (_IO_vfprintf_internal
, vfprintf
);
2387 ldbl_hidden_def (_IO_vfprintf_internal
, vfprintf
)
2388 ldbl_strong_alias (_IO_vfprintf_internal
, _IO_vfprintf
);
2389 ldbl_hidden_def (_IO_vfprintf_internal
, _IO_vfprintf
)