1 /* Copyright (C) 1991-2008, 2009 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 #include <bits/libc-lock.h>
29 #include <sys/param.h>
31 #include <locale/localeinfo.h>
34 /* This code is shared between the standard stdio implementation found
35 in GNU C library and the libio implementation originally found in
38 Beside this it is also shared between the normal and wide character
39 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
45 #define va_list _IO_va_list
47 #define BUFSIZ _IO_BUFSIZ
48 #define ARGCHECK(S, Format) \
51 /* Check file argument for consistence. */ \
53 if (S->_flags & _IO_NO_WRITES) \
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 (__builtin_expect ((unsigned int) INT_MAX - (unsigned int) done \
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) \
91 done_add (INTUSE(_IO_padn) (s, (Padchar), width))
92 # define PUTC(C, F) _IO_putc_unlocked (C, F)
93 # define ORIENT if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
96 # define vfprintf _IO_vfwprintf
97 # define CHAR_T wchar_t
98 /* This is a hack!!! There should be a type uwchar_t. */
99 # define UCHAR_T unsigned int /* uwchar_t */
100 # define INT_T wint_t
101 # define L_(Str) L##Str
102 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
103 # define STR_LEN(Str) __wcslen (Str)
107 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
108 # define PAD(Padchar) \
110 done_add (_IO_wpadn (s, (Padchar), width))
111 # define PUTC(C, F) _IO_putwc_unlocked (C, F)
112 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
115 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
116 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
121 #include "_i18n_number.h"
123 /* Include the shared code for parsing the format string. */
124 #include "printf-parse.h"
127 #define outchar(Ch) \
130 register const INT_T outc = (Ch); \
131 if (PUTC (outc, s) == EOF || done == INT_MAX) \
140 #define outstring(String, Len) \
143 assert ((size_t) done <= (size_t) INT_MAX); \
144 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len) \
145 || (size_t) INT_MAX - (size_t) done < (size_t) (Len)) \
154 /* For handling long_double and longlong we use the same flag. If
155 `long' and `long long' are effectively the same type define it to
157 #if LONG_MAX == LONG_LONG_MAX
158 # define is_longlong 0
160 # define is_longlong is_long_double
163 /* If `long' and `int' is effectively the same type we don't have to
164 handle `long separately. */
165 #if INT_MAX == LONG_MAX
166 # define is_long_num 0
168 # define is_long_num is_long
172 /* Global variables. */
173 static const CHAR_T null
[] = L_("(null)");
176 /* Helper function to provide temporary buffering for unbuffered streams. */
177 static int buffered_vfprintf (FILE *stream
, const CHAR_T
*fmt
, va_list)
178 __THROW
__attribute__ ((noinline
)) internal_function
;
180 /* Handle unknown format specifier. */
181 static int printf_unknown (FILE *, const struct printf_info
*,
182 const void *const *) __THROW
;
184 /* Group digits of number string. */
185 #ifdef COMPILE_WPRINTF
186 static CHAR_T
*group_number (CHAR_T
*, CHAR_T
*, const char *, wchar_t)
187 __THROW internal_function
;
189 static CHAR_T
*group_number (CHAR_T
*, CHAR_T
*, const char *, const char *)
190 __THROW internal_function
;
194 /* The function itself. */
196 vfprintf (FILE *s
, const CHAR_T
*format
, va_list ap
)
198 /* The character used as thousands separator. */
199 #ifdef COMPILE_WPRINTF
200 wchar_t thousands_sep
= L
'\0';
202 const char *thousands_sep
= NULL
;
205 /* The string describing the size of groups of digits. */
206 const char *grouping
;
208 /* Place to accumulate the result. */
211 /* Current character in format string. */
214 /* End of leading constant string. */
215 const UCHAR_T
*lead_str_end
;
217 /* Points to next format specifier. */
218 const UCHAR_T
*end_of_spec
;
220 /* Buffer intermediate results. */
221 CHAR_T work_buffer
[1000];
222 CHAR_T
*workstart
= NULL
;
225 /* We have to save the original argument pointer. */
228 /* Count number of specifiers we already processed. */
231 /* For the %m format we may need the current `errno' value. */
232 int save_errno
= errno
;
234 /* 1 if format is in read-only memory, -1 if it is in writable memory,
236 int readonly_format
= 0;
238 /* This table maps a character into a number representing a
239 class. In each step there is a destination label for each
241 static const uint8_t jump_table
[] =
243 /* ' ' */ 1, 0, 0, /* '#' */ 4,
244 0, /* '%' */ 14, 0, /* '\''*/ 6,
245 0, 0, /* '*' */ 7, /* '+' */ 2,
246 0, /* '-' */ 3, /* '.' */ 9, 0,
247 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
248 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
249 /* '8' */ 8, /* '9' */ 8, 0, 0,
251 0, /* 'A' */ 26, 0, /* 'C' */ 25,
252 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
253 0, /* 'I' */ 29, 0, 0,
254 /* 'L' */ 12, 0, 0, 0,
255 0, 0, 0, /* 'S' */ 21,
257 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
259 0, /* 'a' */ 26, 0, /* 'c' */ 20,
260 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
261 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
262 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
263 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
264 /* 't' */ 27, /* 'u' */ 16, 0, 0,
265 /* 'x' */ 18, 0, /* 'z' */ 13
268 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
269 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
271 /* 'int' is enough and it saves some space on 64 bit systems. */
272 # define JUMP_TABLE_TYPE const int
273 # define JUMP(ChExpr, table) \
277 void *__unbounded ptr; \
279 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
280 : table[CHAR_CLASS (spec)]; \
281 ptr = &&do_form_unknown + offset; \
286 # define JUMP_TABLE_TYPE const void *const
287 # define JUMP(ChExpr, table) \
290 const void *__unbounded ptr; \
292 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
293 : table[CHAR_CLASS (spec)]; \
299 #define STEP0_3_TABLE \
300 /* Step 0: at the beginning. */ \
301 static JUMP_TABLE_TYPE step0_jumps[30] = \
303 REF (form_unknown), \
304 REF (flag_space), /* for ' ' */ \
305 REF (flag_plus), /* for '+' */ \
306 REF (flag_minus), /* for '-' */ \
307 REF (flag_hash), /* for '<hash>' */ \
308 REF (flag_zero), /* for '0' */ \
309 REF (flag_quote), /* for '\'' */ \
310 REF (width_asterics), /* for '*' */ \
311 REF (width), /* for '1'...'9' */ \
312 REF (precision), /* for '.' */ \
313 REF (mod_half), /* for 'h' */ \
314 REF (mod_long), /* for 'l' */ \
315 REF (mod_longlong), /* for 'L', 'q' */ \
316 REF (mod_size_t), /* for 'z', 'Z' */ \
317 REF (form_percent), /* for '%' */ \
318 REF (form_integer), /* for 'd', 'i' */ \
319 REF (form_unsigned), /* for 'u' */ \
320 REF (form_octal), /* for 'o' */ \
321 REF (form_hexa), /* for 'X', 'x' */ \
322 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
323 REF (form_character), /* for 'c' */ \
324 REF (form_string), /* for 's', 'S' */ \
325 REF (form_pointer), /* for 'p' */ \
326 REF (form_number), /* for 'n' */ \
327 REF (form_strerror), /* for 'm' */ \
328 REF (form_wcharacter), /* for 'C' */ \
329 REF (form_floathex), /* for 'A', 'a' */ \
330 REF (mod_ptrdiff_t), /* for 't' */ \
331 REF (mod_intmax_t), /* for 'j' */ \
332 REF (flag_i18n), /* for 'I' */ \
334 /* Step 1: after processing width. */ \
335 static JUMP_TABLE_TYPE step1_jumps[30] = \
337 REF (form_unknown), \
338 REF (form_unknown), /* for ' ' */ \
339 REF (form_unknown), /* for '+' */ \
340 REF (form_unknown), /* for '-' */ \
341 REF (form_unknown), /* for '<hash>' */ \
342 REF (form_unknown), /* for '0' */ \
343 REF (form_unknown), /* for '\'' */ \
344 REF (form_unknown), /* for '*' */ \
345 REF (form_unknown), /* 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 (form_unknown) /* for 'I' */ \
368 /* Step 2: after processing precision. */ \
369 static JUMP_TABLE_TYPE step2_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 (form_unknown), /* 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 3a: after processing first 'h' modifier. */ \
403 static JUMP_TABLE_TYPE step3a_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_halfhalf), /* for 'h' */ \
416 REF (form_unknown), /* for 'l' */ \
417 REF (form_unknown), /* for 'L', 'q' */ \
418 REF (form_unknown), /* 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_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
425 REF (form_unknown), /* for 'c' */ \
426 REF (form_unknown), /* for 's', 'S' */ \
427 REF (form_unknown), /* for 'p' */ \
428 REF (form_number), /* for 'n' */ \
429 REF (form_unknown), /* for 'm' */ \
430 REF (form_unknown), /* for 'C' */ \
431 REF (form_unknown), /* for 'A', 'a' */ \
432 REF (form_unknown), /* for 't' */ \
433 REF (form_unknown), /* for 'j' */ \
434 REF (form_unknown) /* for 'I' */ \
436 /* Step 3b: after processing first 'l' modifier. */ \
437 static JUMP_TABLE_TYPE step3b_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 (form_unknown), /* for 'h' */ \
450 REF (mod_longlong), /* 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_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
459 REF (form_character), /* for 'c' */ \
460 REF (form_string), /* for 's', 'S' */ \
461 REF (form_pointer), /* for 'p' */ \
462 REF (form_number), /* for 'n' */ \
463 REF (form_strerror), /* for 'm' */ \
464 REF (form_wcharacter), /* for 'C' */ \
465 REF (form_floathex), /* for 'A', 'a' */ \
466 REF (form_unknown), /* for 't' */ \
467 REF (form_unknown), /* for 'j' */ \
468 REF (form_unknown) /* for 'I' */ \
471 #define STEP4_TABLE \
472 /* Step 4: processing format specifier. */ \
473 static JUMP_TABLE_TYPE step4_jumps[30] = \
475 REF (form_unknown), \
476 REF (form_unknown), /* for ' ' */ \
477 REF (form_unknown), /* for '+' */ \
478 REF (form_unknown), /* for '-' */ \
479 REF (form_unknown), /* for '<hash>' */ \
480 REF (form_unknown), /* for '0' */ \
481 REF (form_unknown), /* for '\'' */ \
482 REF (form_unknown), /* for '*' */ \
483 REF (form_unknown), /* for '1'...'9' */ \
484 REF (form_unknown), /* for '.' */ \
485 REF (form_unknown), /* for 'h' */ \
486 REF (form_unknown), /* for 'l' */ \
487 REF (form_unknown), /* for 'L', 'q' */ \
488 REF (form_unknown), /* for 'z', 'Z' */ \
489 REF (form_percent), /* for '%' */ \
490 REF (form_integer), /* for 'd', 'i' */ \
491 REF (form_unsigned), /* for 'u' */ \
492 REF (form_octal), /* for 'o' */ \
493 REF (form_hexa), /* for 'X', 'x' */ \
494 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
495 REF (form_character), /* for 'c' */ \
496 REF (form_string), /* for 's', 'S' */ \
497 REF (form_pointer), /* for 'p' */ \
498 REF (form_number), /* for 'n' */ \
499 REF (form_strerror), /* for 'm' */ \
500 REF (form_wcharacter), /* for 'C' */ \
501 REF (form_floathex), /* for 'A', 'a' */ \
502 REF (form_unknown), /* for 't' */ \
503 REF (form_unknown), /* for 'j' */ \
504 REF (form_unknown) /* for 'I' */ \
508 #define process_arg(fspec) \
509 /* Start real work. We know about all flags and modifiers and \
510 now process the wanted format specifier. */ \
511 LABEL (form_percent): \
512 /* Write a literal "%". */ \
516 LABEL (form_integer): \
517 /* Signed decimal integer. */ \
522 long long int signed_number; \
525 signed_number = va_arg (ap, long long int); \
527 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
529 is_negative = signed_number < 0; \
530 number.longlong = is_negative ? (- signed_number) : signed_number; \
532 goto LABEL (longlong_number); \
536 long int signed_number; \
541 signed_number = va_arg (ap, long int); \
543 signed_number = (signed char) va_arg (ap, unsigned int); \
544 else if (!is_short) \
545 signed_number = va_arg (ap, int); \
547 signed_number = (short int) va_arg (ap, unsigned int); \
551 signed_number = args_value[fspec->data_arg].pa_long_int; \
553 signed_number = (signed char) \
554 args_value[fspec->data_arg].pa_u_int; \
555 else if (!is_short) \
556 signed_number = args_value[fspec->data_arg].pa_int; \
558 signed_number = (short int) \
559 args_value[fspec->data_arg].pa_u_int; \
561 is_negative = signed_number < 0; \
562 number.word = is_negative ? (- signed_number) : signed_number; \
564 goto LABEL (number); \
568 LABEL (form_unsigned): \
569 /* Unsigned decimal integer. */ \
571 goto LABEL (unsigned_number); \
574 LABEL (form_octal): \
575 /* Unsigned octal integer. */ \
577 goto LABEL (unsigned_number); \
581 /* Unsigned hexadecimal integer. */ \
584 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
586 /* ISO specifies the `+' and ` ' flags only for signed \
595 number.longlong = va_arg (ap, unsigned long long int); \
597 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
599 LABEL (longlong_number): \
601 /* Supply a default precision if none was given. */ \
604 /* We have to take care for the '0' flag. If a precision \
605 is given it must be ignored. */ \
608 /* If the precision is 0 and the number is 0 nothing has to \
609 be written for the number, except for the 'o' format in \
611 if (prec == 0 && number.longlong == 0) \
614 if (base == 8 && alt) \
615 *--string = L_('0'); \
619 /* Put the number in WORK. */ \
620 string = _itoa (number.longlong, workend, base, \
622 if (group && grouping) \
623 string = group_number (string, workend, grouping, \
626 if (use_outdigits && base == 10) \
627 string = _i18n_number_rewrite (string, workend, workend); \
629 /* Simplify further test for num != 0. */ \
630 number.word = number.longlong != 0; \
637 number.word = va_arg (ap, unsigned long int); \
639 number.word = (unsigned char) va_arg (ap, unsigned int); \
640 else if (!is_short) \
641 number.word = va_arg (ap, unsigned int); \
643 number.word = (unsigned short int) va_arg (ap, unsigned int); \
647 number.word = args_value[fspec->data_arg].pa_u_long_int; \
649 number.word = (unsigned char) \
650 args_value[fspec->data_arg].pa_u_int; \
651 else if (!is_short) \
652 number.word = args_value[fspec->data_arg].pa_u_int; \
654 number.word = (unsigned short int) \
655 args_value[fspec->data_arg].pa_u_int; \
659 /* Supply a default precision if none was given. */ \
662 /* We have to take care for the '0' flag. If a precision \
663 is given it must be ignored. */ \
666 /* If the precision is 0 and the number is 0 nothing has to \
667 be written for the number, except for the 'o' format in \
669 if (prec == 0 && number.word == 0) \
672 if (base == 8 && alt) \
673 *--string = L_('0'); \
677 /* Put the number in WORK. */ \
678 string = _itoa_word (number.word, workend, base, \
680 if (group && grouping) \
681 string = group_number (string, workend, grouping, \
684 if (use_outdigits && base == 10) \
685 string = _i18n_number_rewrite (string, workend, workend); \
689 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
690 /* Add octal marker. */ \
691 *--string = L_('0'); \
693 prec = MAX (0, prec - (workend - string)); \
697 width -= workend - string + prec; \
699 if (number.word != 0 && alt && base == 16) \
700 /* Account for 0X hex marker. */ \
703 if (is_negative || showsign || space) \
706 if (pad == L_(' ')) \
719 if (number.word != 0 && alt && base == 16) \
728 outstring (string, workend - string); \
750 if (number.word != 0 && alt && base == 16) \
757 width -= workend - string + prec; \
767 outstring (string, workend - string); \
773 LABEL (form_float): \
775 /* Floating-point number. This is handled by printf_fp.c. */ \
782 is_long_double = 0; \
784 struct printf_info info = { .prec = prec, \
787 .is_long_double = is_long_double, \
788 .is_short = is_short, \
789 .is_long = is_long, \
793 .showsign = showsign, \
797 .i18n = use_outdigits, \
798 .wide = sizeof (CHAR_T) != 1 }; \
800 if (is_long_double) \
801 the_arg.pa_long_double = va_arg (ap, long double); \
803 the_arg.pa_double = va_arg (ap, double); \
804 ptr = (const void *) &the_arg; \
806 function_done = __printf_fp (s, &info, &ptr); \
810 ptr = (const void *) &args_value[fspec->data_arg]; \
813 fspec->data_arg_type = PA_DOUBLE; \
814 fspec->info.is_long_double = 0; \
817 function_done = __printf_fp (s, &fspec->info, &ptr); \
820 if (function_done < 0) \
822 /* Error in print handler. */ \
827 done_add (function_done); \
831 LABEL (form_floathex): \
833 /* Floating point number printed as hexadecimal number. */ \
840 is_long_double = 0; \
842 struct printf_info info = { .prec = prec, \
845 .is_long_double = is_long_double, \
846 .is_short = is_short, \
847 .is_long = is_long, \
851 .showsign = showsign, \
855 .wide = sizeof (CHAR_T) != 1 }; \
857 if (is_long_double) \
858 the_arg.pa_long_double = va_arg (ap, long double); \
860 the_arg.pa_double = va_arg (ap, double); \
861 ptr = (const void *) &the_arg; \
863 function_done = __printf_fphex (s, &info, &ptr); \
867 ptr = (const void *) &args_value[fspec->data_arg]; \
869 fspec->info.is_long_double = 0; \
871 function_done = __printf_fphex (s, &fspec->info, &ptr); \
874 if (function_done < 0) \
876 /* Error in print handler. */ \
881 done_add (function_done); \
885 LABEL (form_pointer): \
886 /* Generic pointer. */ \
890 ptr = va_arg (ap, void *); \
892 ptr = args_value[fspec->data_arg].pa_pointer; \
895 /* If the pointer is not NULL, write it as a %#x spec. */ \
897 number.word = (unsigned long int) ptr; \
902 goto LABEL (number); \
906 /* Write "(nil)" for a nil pointer. */ \
907 string = (CHAR_T *) L_("(nil)"); \
908 /* Make sure the full string "(nil)" is printed. */ \
911 is_long = 0; /* This is no wide-char string. */ \
912 goto LABEL (print_string); \
917 LABEL (form_number): \
918 if (s->_flags2 & _IO_FLAGS2_FORTIFY) \
920 if (! readonly_format) \
922 extern int __readonly_area (const void *, size_t) \
925 = __readonly_area (format, ((STR_LEN (format) + 1) \
926 * sizeof (CHAR_T))); \
928 if (readonly_format < 0) \
929 __libc_fatal ("*** %n in writable segment detected ***\n"); \
931 /* Answer the count of characters written. */ \
935 *(long long int *) va_arg (ap, void *) = done; \
936 else if (is_long_num) \
937 *(long int *) va_arg (ap, void *) = done; \
939 *(char *) va_arg (ap, void *) = done; \
940 else if (!is_short) \
941 *(int *) va_arg (ap, void *) = done; \
943 *(short int *) va_arg (ap, void *) = done; \
947 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
948 else if (is_long_num) \
949 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
951 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
952 else if (!is_short) \
953 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
955 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
958 LABEL (form_strerror): \
959 /* Print description of error ERRNO. */ \
961 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
962 sizeof work_buffer); \
963 is_long = 0; /* This is no wide-char string. */ \
964 goto LABEL (print_string)
966 #ifdef COMPILE_WPRINTF
967 # define process_string_arg(fspec) \
968 LABEL (form_character): \
971 goto LABEL (form_wcharacter); \
972 --width; /* Account for the character itself. */ \
976 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
978 outchar (__btowc ((unsigned char) \
979 args_value[fspec->data_arg].pa_int)); \
984 LABEL (form_wcharacter): \
986 /* Wide character. */ \
991 outchar (va_arg (ap, wchar_t)); \
993 outchar (args_value[fspec->data_arg].pa_wchar); \
999 LABEL (form_string): \
1002 int string_malloced; \
1004 /* The string argument could in fact be `char *' or `wchar_t *'. \
1005 But this should not make a difference here. */ \
1006 if (fspec == NULL) \
1007 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
1009 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
1011 /* Entry point for printing other strings. */ \
1012 LABEL (print_string): \
1014 string_malloced = 0; \
1015 if (string == NULL) \
1017 /* Write "(null)" if there's space. */ \
1019 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
1021 string = (CHAR_T *) null; \
1022 len = (sizeof (null) / sizeof (null[0])) - 1; \
1026 string = (CHAR_T *) L""; \
1030 else if (!is_long && spec != L_('S')) \
1032 /* This is complicated. We have to transform the multibyte \
1033 string into a wide character string. */ \
1034 const char *mbs = (const char *) string; \
1035 mbstate_t mbstate; \
1037 len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
1039 /* Allocate dynamically an array which definitely is long \
1040 enough for the wide character version. Each byte in the \
1041 multi-byte string can produce at most one wide character. */ \
1042 if (__libc_use_alloca (len * sizeof (wchar_t))) \
1043 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1044 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1051 string_malloced = 1; \
1053 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1054 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1055 if (len == (size_t) -1) \
1057 /* Illegal multibyte character. */ \
1065 /* Search for the end of the string, but don't search past \
1066 the length specified by the precision. */ \
1067 len = __wcsnlen (string, prec); \
1069 len = __wcslen (string); \
1072 if ((width -= len) < 0) \
1074 outstring (string, len); \
1080 outstring (string, len); \
1083 if (__builtin_expect (string_malloced, 0)) \
1088 # define process_string_arg(fspec) \
1089 LABEL (form_character): \
1092 goto LABEL (form_wcharacter); \
1093 --width; /* Account for the character itself. */ \
1096 if (fspec == NULL) \
1097 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1099 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1104 LABEL (form_wcharacter): \
1106 /* Wide character. */ \
1107 char buf[MB_CUR_MAX]; \
1108 mbstate_t mbstate; \
1111 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1112 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1113 : args_value[fspec->data_arg].pa_wchar), \
1115 if (len == (size_t) -1) \
1117 /* Something went wron gduring the conversion. Bail out. */ \
1124 outstring (buf, len); \
1130 LABEL (form_string): \
1133 int string_malloced; \
1135 /* The string argument could in fact be `char *' or `wchar_t *'. \
1136 But this should not make a difference here. */ \
1137 if (fspec == NULL) \
1138 string = (char *) va_arg (ap, const char *); \
1140 string = (char *) args_value[fspec->data_arg].pa_string; \
1142 /* Entry point for printing other strings. */ \
1143 LABEL (print_string): \
1145 string_malloced = 0; \
1146 if (string == NULL) \
1148 /* Write "(null)" if there's space. */ \
1149 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1151 string = (char *) null; \
1152 len = sizeof (null) - 1; \
1156 string = (char *) ""; \
1160 else if (!is_long && spec != L_('S')) \
1164 /* Search for the end of the string, but don't search past \
1165 the length (in bytes) specified by the precision. Also \
1166 don't use incomplete characters. */ \
1167 if (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MB_CUR_MAX) == 1) \
1168 len = __strnlen (string, prec); \
1171 /* In case we have a multibyte character set the \
1172 situation is more complicated. We must not copy \
1173 bytes at the end which form an incomplete character. */\
1174 size_t ignore_size = (unsigned) prec > 1024 ? 1024 : prec;\
1175 wchar_t ignore[ignore_size]; \
1176 const char *str2 = string; \
1177 const char *strend = string + prec; \
1178 if (strend < string) \
1179 strend = (const char *) UINTPTR_MAX; \
1182 memset (&ps, '\0', sizeof (ps)); \
1184 while (str2 != NULL && str2 < strend) \
1185 if (__mbsnrtowcs (ignore, &str2, strend - str2, \
1186 ignore_size, &ps) == (size_t) -1) \
1193 len = strlen (string); \
1195 len = str2 - string - (ps.__count & 7); \
1199 len = strlen (string); \
1203 const wchar_t *s2 = (const wchar_t *) string; \
1204 mbstate_t mbstate; \
1206 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1210 /* The string `s2' might not be NUL terminated. */ \
1211 if (__libc_use_alloca (prec)) \
1212 string = (char *) alloca (prec); \
1213 else if ((string = (char *) malloc (prec)) == NULL) \
1219 string_malloced = 1; \
1220 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1224 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1225 if (len != (size_t) -1) \
1227 assert (__mbsinit (&mbstate)); \
1228 s2 = (const wchar_t *) string; \
1229 if (__libc_use_alloca (len + 1)) \
1230 string = (char *) alloca (len + 1); \
1231 else if ((string = (char *) malloc (len + 1)) == NULL) \
1237 string_malloced = 1; \
1238 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1242 if (len == (size_t) -1) \
1244 /* Illegal wide-character string. */ \
1250 if ((width -= len) < 0) \
1252 outstring (string, len); \
1258 outstring (string, len); \
1261 if (__builtin_expect (string_malloced, 0)) \
1267 /* Orient the stream. */
1272 /* Sanity check of arguments. */
1273 ARGCHECK (s
, format
);
1276 /* Check for correct orientation. */
1277 if (_IO_vtable_offset (s
) == 0 &&
1278 _IO_fwide (s
, sizeof (CHAR_T
) == 1 ? -1 : 1)
1279 != (sizeof (CHAR_T
) == 1 ? -1 : 1))
1280 /* The stream is already oriented otherwise. */
1284 if (UNBUFFERED_P (s
))
1285 /* Use a helper function which will allocate a local temporary buffer
1286 for the stream and then call us again. */
1287 return buffered_vfprintf (s
, format
, ap
);
1289 /* Initialize local variables. */
1291 grouping
= (const char *) -1;
1293 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1294 since on some systems `va_list' is not an integral type. */
1295 __va_copy (ap_save
, ap
);
1301 #ifdef COMPILE_WPRINTF
1302 /* Find the first format specifier. */
1303 f
= lead_str_end
= __find_specwc ((const UCHAR_T
*) format
);
1305 /* Find the first format specifier. */
1306 f
= lead_str_end
= __find_specmb ((const UCHAR_T
*) format
);
1310 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile
, s
);
1313 /* Write the literal text before the first format. */
1314 outstring ((const UCHAR_T
*) format
,
1315 lead_str_end
- (const UCHAR_T
*) format
);
1317 /* If we only have to print a simple string, return now. */
1321 /* Process whole format string. */
1325 # define REF(Name) &&do_##Name - &&do_form_unknown
1327 # define REF(Name) &&do_##Name
1329 #define LABEL(Name) do_##Name
1333 union printf_arg
*args_value
; /* This is not used here but ... */
1334 int is_negative
; /* Flag for negative number. */
1337 unsigned long long int longlong
;
1338 unsigned long int word
;
1341 union printf_arg the_arg
;
1342 CHAR_T
*string
; /* Pointer to argument string. */
1343 int alt
= 0; /* Alternate format. */
1344 int space
= 0; /* Use space prefix if no sign is needed. */
1345 int left
= 0; /* Left-justify output. */
1346 int showsign
= 0; /* Always begin with plus or minus sign. */
1347 int group
= 0; /* Print numbers according grouping rules. */
1348 int is_long_double
= 0; /* Argument is long double/ long long int. */
1349 int is_short
= 0; /* Argument is short int. */
1350 int is_long
= 0; /* Argument is long int. */
1351 int is_char
= 0; /* Argument is promoted (unsigned) char. */
1352 int width
= 0; /* Width of output; 0 means none specified. */
1353 int prec
= -1; /* Precision of output; -1 means none specified. */
1354 /* This flag is set by the 'I' modifier and selects the use of the
1355 `outdigits' as determined by the current locale. */
1356 int use_outdigits
= 0;
1357 UCHAR_T pad
= L_(' ');/* Padding character. */
1361 workend
= &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
1363 /* Get current character in format string. */
1364 JUMP (*++f
, step0_jumps
);
1369 JUMP (*++f
, step0_jumps
);
1374 JUMP (*++f
, step0_jumps
);
1380 JUMP (*++f
, step0_jumps
);
1385 JUMP (*++f
, step0_jumps
);
1391 JUMP (*++f
, step0_jumps
);
1393 /* The '\'' flag. */
1397 if (grouping
== (const char *) -1)
1399 #ifdef COMPILE_WPRINTF
1400 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1401 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1403 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1406 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1407 if (*grouping
== '\0' || *grouping
== CHAR_MAX
1408 #ifdef COMPILE_WPRINTF
1409 || thousands_sep
== L
'\0'
1411 || *thousands_sep
== '\0'
1416 JUMP (*++f
, step0_jumps
);
1420 JUMP (*++f
, step0_jumps
);
1422 /* Get width from argument. */
1423 LABEL (width_asterics
):
1425 const UCHAR_T
*tmp
; /* Temporary value. */
1428 if (ISDIGIT (*tmp
) && read_int (&tmp
) && *tmp
== L_('$'))
1429 /* The width comes from a positional parameter. */
1432 width
= va_arg (ap
, int);
1434 /* Negative width means left justified. */
1442 if (width
+ 32 >= (int) (sizeof (work_buffer
)
1443 / sizeof (work_buffer
[0])))
1445 /* We have to use a special buffer. The "32" is just a safe
1446 bet for all the output which is not counted in the width. */
1447 if (__libc_use_alloca ((width
+ 32) * sizeof (CHAR_T
)))
1448 workend
= ((CHAR_T
*) alloca ((width
+ 32) * sizeof (CHAR_T
))
1452 workstart
= (CHAR_T
*) malloc ((width
+ 32) * sizeof (CHAR_T
));
1453 if (workstart
== NULL
)
1458 workend
= workstart
+ (width
+ 32);
1462 JUMP (*f
, step1_jumps
);
1464 /* Given width in format string. */
1466 width
= read_int (&f
);
1468 if (width
+ 32 >= (int) (sizeof (work_buffer
) / sizeof (work_buffer
[0])))
1470 /* We have to use a special buffer. The "32" is just a safe
1471 bet for all the output which is not counted in the width. */
1472 if (__libc_use_alloca ((width
+ 32) * sizeof (CHAR_T
)))
1473 workend
= ((CHAR_T
*) alloca ((width
+ 32) * sizeof (CHAR_T
))
1477 workstart
= (CHAR_T
*) malloc ((width
+ 32) * sizeof (CHAR_T
));
1478 if (workstart
== NULL
)
1483 workend
= workstart
+ (width
+ 32);
1487 /* Oh, oh. The argument comes from a positional parameter. */
1489 JUMP (*f
, step1_jumps
);
1495 const UCHAR_T
*tmp
; /* Temporary value. */
1498 if (ISDIGIT (*tmp
) && read_int (&tmp
) > 0 && *tmp
== L_('$'))
1499 /* The precision comes from a positional parameter. */
1502 prec
= va_arg (ap
, int);
1504 /* If the precision is negative the precision is omitted. */
1508 else if (ISDIGIT (*f
))
1509 prec
= read_int (&f
);
1513 && prec
+ 32 > (int)(sizeof (work_buffer
) / sizeof (work_buffer
[0])))
1515 if (__builtin_expect (prec
> ~((size_t) 0) / sizeof (CHAR_T
) - 31,
1521 size_t needed
= ((size_t) prec
+ 32) * sizeof (CHAR_T
);
1523 if (__libc_use_alloca (needed
))
1524 workend
= (((CHAR_T
*) alloca (needed
)) + ((size_t) prec
+ 32));
1527 workstart
= (CHAR_T
*) malloc (needed
);
1528 if (workstart
== NULL
)
1533 workend
= workstart
+ ((size_t) prec
+ 32);
1536 JUMP (*f
, step2_jumps
);
1538 /* Process 'h' modifier. There might another 'h' following. */
1541 JUMP (*++f
, step3a_jumps
);
1543 /* Process 'hh' modifier. */
1544 LABEL (mod_halfhalf
):
1547 JUMP (*++f
, step4_jumps
);
1549 /* Process 'l' modifier. There might another 'l' following. */
1552 JUMP (*++f
, step3b_jumps
);
1554 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1555 allowed to follow. */
1556 LABEL (mod_longlong
):
1559 JUMP (*++f
, step4_jumps
);
1562 is_long_double
= sizeof (size_t) > sizeof (unsigned long int);
1563 is_long
= sizeof (size_t) > sizeof (unsigned int);
1564 JUMP (*++f
, step4_jumps
);
1566 LABEL (mod_ptrdiff_t
):
1567 is_long_double
= sizeof (ptrdiff_t) > sizeof (unsigned long int);
1568 is_long
= sizeof (ptrdiff_t) > sizeof (unsigned int);
1569 JUMP (*++f
, step4_jumps
);
1571 LABEL (mod_intmax_t
):
1572 is_long_double
= sizeof (intmax_t) > sizeof (unsigned long int);
1573 is_long
= sizeof (intmax_t) > sizeof (unsigned int);
1574 JUMP (*++f
, step4_jumps
);
1576 /* Process current format. */
1579 process_arg (((struct printf_spec
*) NULL
));
1580 process_string_arg (((struct printf_spec
*) NULL
));
1582 LABEL (form_unknown
):
1583 if (spec
== L_('\0'))
1585 /* The format string ended before the specifier is complete. */
1590 /* If we are in the fast loop force entering the complicated
1595 /* The format is correctly handled. */
1598 if (__builtin_expect (workstart
!= NULL
, 0))
1602 /* Look for next format specifier. */
1603 #ifdef COMPILE_WPRINTF
1604 f
= __find_specwc ((end_of_spec
= ++f
));
1606 f
= __find_specmb ((end_of_spec
= ++f
));
1609 /* Write the following constant string. */
1610 outstring (end_of_spec
, f
- end_of_spec
);
1612 while (*f
!= L_('\0'));
1614 /* Unlock stream and return. */
1617 /* Here starts the more complex loop to handle positional parameters. */
1620 /* Array with information about the needed arguments. This has to
1621 be dynamically extensible. */
1623 size_t nspecs_max
= 32; /* A more or less arbitrary start value. */
1624 struct printf_spec
*specs
1625 = alloca (nspecs_max
* sizeof (struct printf_spec
));
1627 /* The number of arguments the format string requests. This will
1628 determine the size of the array needed to store the argument
1632 union printf_arg
*args_value
= NULL
;
1635 /* Positional parameters refer to arguments directly. This could
1636 also determine the maximum number of arguments. Track the
1638 size_t max_ref_arg
= 0;
1640 /* Just a counter. */
1646 if (grouping
== (const char *) -1)
1648 #ifdef COMPILE_WPRINTF
1649 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1650 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1652 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1655 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1656 if (*grouping
== '\0' || *grouping
== CHAR_MAX
)
1660 for (f
= lead_str_end
; *f
!= L_('\0'); f
= specs
[nspecs
++].next_fmt
)
1662 if (nspecs
>= nspecs_max
)
1664 /* Extend the array of format specifiers. */
1665 struct printf_spec
*old
= specs
;
1666 specs
= extend_alloca (specs
, nspecs_max
, 2 * nspecs_max
);
1668 /* Copy the old array's elements to the new space. */
1669 memmove (specs
, old
, nspecs
* sizeof (struct printf_spec
));
1672 /* Parse the format specifier. */
1673 #ifdef COMPILE_WPRINTF
1674 nargs
+= __parse_one_specwc (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1676 nargs
+= __parse_one_specmb (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1680 /* Determine the number of arguments the format string consumes. */
1681 nargs
= MAX (nargs
, max_ref_arg
);
1683 /* Allocate memory for the argument descriptions. */
1684 args_type
= alloca (nargs
* sizeof (int));
1685 memset (args_type
, s
->_flags2
& _IO_FLAGS2_FORTIFY
? '\xff' : '\0',
1686 nargs
* sizeof (int));
1687 args_value
= alloca (nargs
* sizeof (union printf_arg
));
1688 args_size
= alloca (nargs
* sizeof (int));
1690 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1691 still zero after this loop, format is invalid. For now we
1692 simply use 0 as the value. */
1694 /* Fill in the types of all the arguments. */
1695 for (cnt
= 0; cnt
< nspecs
; ++cnt
)
1697 /* If the width is determined by an argument this is an int. */
1698 if (specs
[cnt
].width_arg
!= -1)
1699 args_type
[specs
[cnt
].width_arg
] = PA_INT
;
1701 /* If the precision is determined by an argument this is an int. */
1702 if (specs
[cnt
].prec_arg
!= -1)
1703 args_type
[specs
[cnt
].prec_arg
] = PA_INT
;
1705 switch (specs
[cnt
].ndata_args
)
1707 case 0: /* No arguments. */
1709 case 1: /* One argument; we already have the
1711 args_type
[specs
[cnt
].data_arg
] = specs
[cnt
].data_arg_type
;
1712 args_size
[specs
[cnt
].data_arg
] = specs
[cnt
].size
;
1715 /* We have more than one argument for this format spec.
1716 We must call the arginfo function again to determine
1718 (void) (*__printf_arginfo_table
[specs
[cnt
].info
.spec
])
1720 specs
[cnt
].ndata_args
, &args_type
[specs
[cnt
].data_arg
],
1721 &args_size
[specs
[cnt
].data_arg
]);
1726 /* Now we know all the types and the order. Fill in the argument
1728 for (cnt
= 0; cnt
< nargs
; ++cnt
)
1729 switch (args_type
[cnt
])
1731 #define T(tag, mem, type) \
1733 args_value[cnt].mem = va_arg (ap_save, type); \
1736 T (PA_WCHAR
, pa_wchar
, wint_t);
1737 case PA_CHAR
: /* Promoted. */
1738 case PA_INT
|PA_FLAG_SHORT
: /* Promoted. */
1739 #if LONG_MAX == INT_MAX
1740 case PA_INT
|PA_FLAG_LONG
:
1742 T (PA_INT
, pa_int
, int);
1743 #if LONG_MAX == LONG_LONG_MAX
1744 case PA_INT
|PA_FLAG_LONG
:
1746 T (PA_INT
|PA_FLAG_LONG_LONG
, pa_long_long_int
, long long int);
1747 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1750 case PA_FLOAT
: /* Promoted. */
1751 T (PA_DOUBLE
, pa_double
, double);
1752 case PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
:
1755 args_value
[cnt
].pa_double
= va_arg (ap_save
, double);
1756 args_type
[cnt
] &= ~PA_FLAG_LONG_DOUBLE
;
1759 args_value
[cnt
].pa_long_double
= va_arg (ap_save
, long double);
1761 case PA_STRING
: /* All pointers are the same */
1762 case PA_WSTRING
: /* All pointers are the same */
1763 T (PA_POINTER
, pa_pointer
, void *);
1766 if ((args_type
[cnt
] & PA_FLAG_PTR
) != 0)
1767 args_value
[cnt
].pa_pointer
= va_arg (ap_save
, void *);
1768 else if (__builtin_expect (__printf_va_arg_table
!= NULL
, 0)
1769 && __printf_va_arg_table
[args_type
[cnt
] - PA_LAST
] != NULL
)
1771 args_value
[cnt
].pa_user
= alloca (args_size
[cnt
]);
1772 (*__printf_va_arg_table
[args_type
[cnt
] - PA_LAST
])
1773 (args_value
[cnt
].pa_user
, &ap_save
);
1776 args_value
[cnt
].pa_long_double
= 0.0;
1779 /* Error case. Not all parameters appear in N$ format
1780 strings. We have no way to determine their type. */
1781 assert (s
->_flags2
& _IO_FLAGS2_FORTIFY
);
1782 __libc_fatal ("*** invalid %N$ use detected ***\n");
1785 /* Now walk through all format specifiers and process them. */
1786 for (; (size_t) nspecs_done
< nspecs
; ++nspecs_done
)
1790 # define REF(Name) &&do2_##Name - &&do_form_unknown
1792 # define REF(Name) &&do2_##Name
1795 #define LABEL(Name) do2_##Name
1801 unsigned long long int longlong
;
1802 unsigned long int word
;
1805 union printf_arg the_arg
;
1806 CHAR_T
*string
; /* Pointer to argument string. */
1808 /* Fill variables from values in struct. */
1809 int alt
= specs
[nspecs_done
].info
.alt
;
1810 int space
= specs
[nspecs_done
].info
.space
;
1811 int left
= specs
[nspecs_done
].info
.left
;
1812 int showsign
= specs
[nspecs_done
].info
.showsign
;
1813 int group
= specs
[nspecs_done
].info
.group
;
1814 int is_long_double
= specs
[nspecs_done
].info
.is_long_double
;
1815 int is_short
= specs
[nspecs_done
].info
.is_short
;
1816 int is_char
= specs
[nspecs_done
].info
.is_char
;
1817 int is_long
= specs
[nspecs_done
].info
.is_long
;
1818 int width
= specs
[nspecs_done
].info
.width
;
1819 int prec
= specs
[nspecs_done
].info
.prec
;
1820 int use_outdigits
= specs
[nspecs_done
].info
.i18n
;
1821 char pad
= specs
[nspecs_done
].info
.pad
;
1822 CHAR_T spec
= specs
[nspecs_done
].info
.spec
;
1825 workend
= &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
1827 /* Fill in last information. */
1828 if (specs
[nspecs_done
].width_arg
!= -1)
1830 /* Extract the field width from an argument. */
1831 specs
[nspecs_done
].info
.width
=
1832 args_value
[specs
[nspecs_done
].width_arg
].pa_int
;
1834 if (specs
[nspecs_done
].info
.width
< 0)
1835 /* If the width value is negative left justification is
1836 selected and the value is taken as being positive. */
1838 specs
[nspecs_done
].info
.width
*= -1;
1839 left
= specs
[nspecs_done
].info
.left
= 1;
1841 width
= specs
[nspecs_done
].info
.width
;
1844 if (specs
[nspecs_done
].prec_arg
!= -1)
1846 /* Extract the precision from an argument. */
1847 specs
[nspecs_done
].info
.prec
=
1848 args_value
[specs
[nspecs_done
].prec_arg
].pa_int
;
1850 if (specs
[nspecs_done
].info
.prec
< 0)
1851 /* If the precision is negative the precision is
1853 specs
[nspecs_done
].info
.prec
= -1;
1855 prec
= specs
[nspecs_done
].info
.prec
;
1858 /* Maybe the buffer is too small. */
1859 if (MAX (prec
, width
) + 32 > (int) (sizeof (work_buffer
)
1862 if (__libc_use_alloca ((MAX (prec
, width
) + 32)
1864 workend
= ((CHAR_T
*) alloca ((MAX (prec
, width
) + 32)
1866 + (MAX (prec
, width
) + 32));
1869 workstart
= (CHAR_T
*) malloc ((MAX (prec
, width
) + 32)
1871 workend
= workstart
+ (MAX (prec
, width
) + 32);
1875 /* Process format specifiers. */
1878 extern printf_function
**__printf_function_table
;
1881 if (spec
<= UCHAR_MAX
1882 && __printf_function_table
!= NULL
1883 && __printf_function_table
[(size_t) spec
] != NULL
)
1885 const void **ptr
= alloca (specs
[nspecs_done
].ndata_args
1886 * sizeof (const void *));
1888 /* Fill in an array of pointers to the argument values. */
1889 for (unsigned int i
= 0; i
< specs
[nspecs_done
].ndata_args
;
1891 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
1893 /* Call the function. */
1894 function_done
= __printf_function_table
[(size_t) spec
]
1895 (s
, &specs
[nspecs_done
].info
, ptr
);
1897 if (function_done
!= -2)
1899 /* If an error occurred we don't have information
1900 about # of chars. */
1901 if (function_done
< 0)
1907 done_add (function_done
);
1912 JUMP (spec
, step4_jumps
);
1914 process_arg ((&specs
[nspecs_done
]));
1915 process_string_arg ((&specs
[nspecs_done
]));
1917 LABEL (form_unknown
):
1922 ptr
= alloca (specs
[nspecs_done
].ndata_args
1923 * sizeof (const void *));
1925 /* Fill in an array of pointers to the argument values. */
1926 for (i
= 0; i
< specs
[nspecs_done
].ndata_args
; ++i
)
1927 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
1929 /* Call the function. */
1930 function_done
= printf_unknown (s
, &specs
[nspecs_done
].info
,
1933 /* If an error occurred we don't have information about #
1935 if (function_done
< 0)
1941 done_add (function_done
);
1949 /* Write the following constant string. */
1950 outstring (specs
[nspecs_done
].end_of_fmt
,
1951 specs
[nspecs_done
].next_fmt
1952 - specs
[nspecs_done
].end_of_fmt
);
1957 if (__builtin_expect (workstart
!= NULL
, 0))
1959 /* Unlock the stream. */
1960 _IO_funlockfile (s
);
1961 _IO_cleanup_region_end (0);
1966 /* Handle an unknown format specifier. This prints out a canonicalized
1967 representation of the format spec itself. */
1969 printf_unknown (FILE *s
, const struct printf_info
*info
,
1970 const void *const *args
)
1974 CHAR_T work_buffer
[MAX (sizeof (info
->width
), sizeof (info
->prec
)) * 3];
1975 CHAR_T
*const workend
1976 = &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
1987 else if (info
->space
)
1991 if (info
->pad
== L_('0'))
1996 if (info
->width
!= 0)
1998 w
= _itoa_word (info
->width
, workend
, 10, 0);
2003 if (info
->prec
!= -1)
2006 w
= _itoa_word (info
->prec
, workend
, 10, 0);
2011 if (info
->spec
!= L_('\0'))
2012 outchar (info
->spec
);
2018 /* Group the digits according to the grouping rules of the current locale.
2019 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
2022 group_number (CHAR_T
*w
, CHAR_T
*rear_ptr
, const char *grouping
,
2023 #ifdef COMPILE_WPRINTF
2024 wchar_t thousands_sep
2026 const char *thousands_sep
2032 #ifndef COMPILE_WPRINTF
2033 int tlen
= strlen (thousands_sep
);
2036 /* We treat all negative values like CHAR_MAX. */
2038 if (*grouping
== CHAR_MAX
|| *grouping
<= 0)
2039 /* No grouping should be done. */
2044 /* Copy existing string so that nothing gets overwritten. */
2045 src
= (CHAR_T
*) alloca ((rear_ptr
- w
) * sizeof (CHAR_T
));
2046 s
= (CHAR_T
*) __mempcpy (src
, w
,
2047 (rear_ptr
- w
) * sizeof (CHAR_T
));
2050 /* Process all characters in the string. */
2055 if (--len
== 0 && s
> src
)
2057 /* A new group begins. */
2058 #ifdef COMPILE_WPRINTF
2059 *--w
= thousands_sep
;
2063 *--w
= thousands_sep
[--cnt
];
2067 if (*grouping
== CHAR_MAX
2073 /* No further grouping to be done.
2074 Copy the rest of the number. */
2080 else if (*grouping
!= '\0')
2081 /* The previous grouping repeats ad infinitum. */
2090 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2093 struct _IO_FILE_plus _f
;
2094 #ifdef COMPILE_WPRINTF
2095 struct _IO_wide_data _wide_data
;
2097 _IO_FILE
*_put_stream
;
2098 #ifdef _IO_MTSAFE_IO
2104 _IO_helper_overflow (_IO_FILE
*s
, int c
)
2106 _IO_FILE
*target
= ((struct helper_file
*) s
)->_put_stream
;
2107 #ifdef COMPILE_WPRINTF
2108 int used
= s
->_wide_data
->_IO_write_ptr
- s
->_wide_data
->_IO_write_base
;
2111 _IO_size_t written
= _IO_sputn (target
, s
->_wide_data
->_IO_write_base
,
2113 if (written
== 0 || written
== WEOF
)
2115 __wmemmove (s
->_wide_data
->_IO_write_base
,
2116 s
->_wide_data
->_IO_write_base
+ written
,
2118 s
->_wide_data
->_IO_write_ptr
-= written
;
2121 int used
= s
->_IO_write_ptr
- s
->_IO_write_base
;
2124 _IO_size_t written
= _IO_sputn (target
, s
->_IO_write_base
, used
);
2125 if (written
== 0 || written
== EOF
)
2127 memmove (s
->_IO_write_base
, s
->_IO_write_base
+ written
,
2129 s
->_IO_write_ptr
-= written
;
2135 #ifdef COMPILE_WPRINTF
2136 static const struct _IO_jump_t _IO_helper_jumps
=
2139 JUMP_INIT (finish
, INTUSE(_IO_wdefault_finish
)),
2140 JUMP_INIT (overflow
, _IO_helper_overflow
),
2141 JUMP_INIT (underflow
, _IO_default_underflow
),
2142 JUMP_INIT (uflow
, INTUSE(_IO_default_uflow
)),
2143 JUMP_INIT (pbackfail
, (_IO_pbackfail_t
) INTUSE(_IO_wdefault_pbackfail
)),
2144 JUMP_INIT (xsputn
, INTUSE(_IO_wdefault_xsputn
)),
2145 JUMP_INIT (xsgetn
, INTUSE(_IO_wdefault_xsgetn
)),
2146 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2147 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2148 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2149 JUMP_INIT (sync
, _IO_default_sync
),
2150 JUMP_INIT (doallocate
, INTUSE(_IO_wdefault_doallocate
)),
2151 JUMP_INIT (read
, _IO_default_read
),
2152 JUMP_INIT (write
, _IO_default_write
),
2153 JUMP_INIT (seek
, _IO_default_seek
),
2154 JUMP_INIT (close
, _IO_default_close
),
2155 JUMP_INIT (stat
, _IO_default_stat
)
2158 static const struct _IO_jump_t _IO_helper_jumps
=
2161 JUMP_INIT (finish
, INTUSE(_IO_default_finish
)),
2162 JUMP_INIT (overflow
, _IO_helper_overflow
),
2163 JUMP_INIT (underflow
, _IO_default_underflow
),
2164 JUMP_INIT (uflow
, INTUSE(_IO_default_uflow
)),
2165 JUMP_INIT (pbackfail
, INTUSE(_IO_default_pbackfail
)),
2166 JUMP_INIT (xsputn
, INTUSE(_IO_default_xsputn
)),
2167 JUMP_INIT (xsgetn
, INTUSE(_IO_default_xsgetn
)),
2168 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2169 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2170 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2171 JUMP_INIT (sync
, _IO_default_sync
),
2172 JUMP_INIT (doallocate
, INTUSE(_IO_default_doallocate
)),
2173 JUMP_INIT (read
, _IO_default_read
),
2174 JUMP_INIT (write
, _IO_default_write
),
2175 JUMP_INIT (seek
, _IO_default_seek
),
2176 JUMP_INIT (close
, _IO_default_close
),
2177 JUMP_INIT (stat
, _IO_default_stat
)
2183 buffered_vfprintf (register _IO_FILE
*s
, const CHAR_T
*format
,
2186 CHAR_T buf
[_IO_BUFSIZ
];
2187 struct helper_file helper
;
2188 register _IO_FILE
*hp
= (_IO_FILE
*) &helper
._f
;
2189 int result
, to_flush
;
2191 /* Orient the stream. */
2196 /* Initialize helper. */
2197 helper
._put_stream
= s
;
2198 #ifdef COMPILE_WPRINTF
2199 hp
->_wide_data
= &helper
._wide_data
;
2200 _IO_wsetp (hp
, buf
, buf
+ sizeof buf
/ sizeof (CHAR_T
));
2203 _IO_setp (hp
, buf
, buf
+ sizeof buf
);
2206 hp
->_IO_file_flags
= _IO_MAGIC
|_IO_NO_READS
|_IO_USER_LOCK
;
2207 #if _IO_JUMPS_OFFSET
2208 hp
->_vtable_offset
= 0;
2210 #ifdef _IO_MTSAFE_IO
2213 hp
->_flags2
= s
->_flags2
;
2214 _IO_JUMPS (&helper
._f
) = (struct _IO_jump_t
*) &_IO_helper_jumps
;
2216 /* Now print to helper instead. */
2217 #ifndef COMPILE_WPRINTF
2218 result
= INTUSE(_IO_vfprintf
) (hp
, format
, args
);
2220 result
= vfprintf (hp
, format
, args
);
2224 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile
, s
);
2227 /* Now flush anything from the helper to the S. */
2228 #ifdef COMPILE_WPRINTF
2229 if ((to_flush
= (hp
->_wide_data
->_IO_write_ptr
2230 - hp
->_wide_data
->_IO_write_base
)) > 0)
2232 if ((int) _IO_sputn (s
, hp
->_wide_data
->_IO_write_base
, to_flush
)
2237 if ((to_flush
= hp
->_IO_write_ptr
- hp
->_IO_write_base
) > 0)
2239 if ((int) _IO_sputn (s
, hp
->_IO_write_base
, to_flush
) != to_flush
)
2244 /* Unlock the stream. */
2245 _IO_funlockfile (s
);
2246 __libc_cleanup_region_end (0);
2252 #ifdef COMPILE_WPRINTF
2253 strong_alias (_IO_vfwprintf
, __vfwprintf
);
2254 ldbl_weak_alias (_IO_vfwprintf
, vfwprintf
);
2256 ldbl_strong_alias (_IO_vfprintf_internal
, vfprintf
);
2257 ldbl_hidden_def (_IO_vfprintf_internal
, vfprintf
)
2258 ldbl_strong_alias (_IO_vfprintf_internal
, _IO_vfprintf
);