1 /* Copyright (C) 1991-2002, 2003, 2004, 2005, 2006, 2007, 2008
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
29 #include <bits/libc-lock.h>
30 #include <sys/param.h>
32 #include <locale/localeinfo.h>
35 /* This code is shared between the standard stdio implementation found
36 in GNU C library and the libio implementation originally found in
39 Beside this it is also shared between the normal and wide character
40 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
46 #define va_list _IO_va_list
48 #define BUFSIZ _IO_BUFSIZ
49 #define ARGCHECK(S, Format) \
52 /* Check file argument for consistence. */ \
54 if (S->_flags & _IO_NO_WRITES) \
56 __set_errno (EBADF); \
65 #define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
67 #define done_add(val) \
69 unsigned int _val = val; \
70 assert ((unsigned int) done < (unsigned int) INT_MAX); \
71 if (__builtin_expect ((unsigned int) INT_MAX - (unsigned int) done \
80 #ifndef COMPILE_WPRINTF
81 # define vfprintf _IO_vfprintf_internal
83 # define UCHAR_T unsigned char
86 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
87 # define STR_LEN(Str) strlen (Str)
89 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
90 # define PAD(Padchar) \
92 done_add (INTUSE(_IO_padn) (s, (Padchar), width))
93 # define PUTC(C, F) _IO_putc_unlocked (C, F)
94 # define ORIENT if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
97 # define vfprintf _IO_vfwprintf
98 # define CHAR_T wchar_t
99 /* This is a hack!!! There should be a type uwchar_t. */
100 # define UCHAR_T unsigned int /* uwchar_t */
101 # define INT_T wint_t
102 # define L_(Str) L##Str
103 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
104 # define STR_LEN(Str) __wcslen (Str)
108 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
109 # define PAD(Padchar) \
111 done_add (_IO_wpadn (s, (Padchar), width))
112 # define PUTC(C, F) _IO_putwc_unlocked (C, F)
113 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
116 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
117 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
122 #include "_i18n_number.h"
124 /* Include the shared code for parsing the format string. */
125 #include "printf-parse.h"
128 #define outchar(Ch) \
131 register const INT_T outc = (Ch); \
132 if (PUTC (outc, s) == EOF || done == INT_MAX) \
141 #define outstring(String, Len) \
144 assert ((size_t) done <= (size_t) INT_MAX); \
145 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len) \
146 || (size_t) INT_MAX - (size_t) done < (size_t) (Len)) \
155 /* For handling long_double and longlong we use the same flag. If
156 `long' and `long long' are effectively the same type define it to
158 #if LONG_MAX == LONG_LONG_MAX
159 # define is_longlong 0
161 # define is_longlong is_long_double
164 /* If `long' and `int' is effectively the same type we don't have to
165 handle `long separately. */
166 #if INT_MAX == LONG_MAX
167 # define is_long_num 0
169 # define is_long_num is_long
173 /* Global variables. */
174 static const CHAR_T null
[] = L_("(null)");
177 /* Helper function to provide temporary buffering for unbuffered streams. */
178 static int buffered_vfprintf (FILE *stream
, const CHAR_T
*fmt
, va_list)
179 __THROW
__attribute__ ((noinline
)) internal_function
;
181 /* Handle unknown format specifier. */
182 static int printf_unknown (FILE *, const struct printf_info
*,
183 const void *const *) __THROW
;
185 /* Group digits of number string. */
186 #ifdef COMPILE_WPRINTF
187 static CHAR_T
*group_number (CHAR_T
*, CHAR_T
*, const char *, wchar_t)
188 __THROW internal_function
;
190 static CHAR_T
*group_number (CHAR_T
*, CHAR_T
*, const char *, const char *)
191 __THROW internal_function
;
195 /* The function itself. */
197 vfprintf (FILE *s
, const CHAR_T
*format
, va_list ap
)
199 /* The character used as thousands separator. */
200 #ifdef COMPILE_WPRINTF
201 wchar_t thousands_sep
= L
'\0';
203 const char *thousands_sep
= NULL
;
206 /* The string describing the size of groups of digits. */
207 const char *grouping
;
209 /* Place to accumulate the result. */
212 /* Current character in format string. */
215 /* End of leading constant string. */
216 const UCHAR_T
*lead_str_end
;
218 /* Points to next format specifier. */
219 const UCHAR_T
*end_of_spec
;
221 /* Buffer intermediate results. */
222 CHAR_T work_buffer
[1000];
223 CHAR_T
*workstart
= NULL
;
226 /* We have to save the original argument pointer. */
229 /* Count number of specifiers we already processed. */
232 /* For the %m format we may need the current `errno' value. */
233 int save_errno
= errno
;
235 /* 1 if format is in read-only memory, -1 if it is in writable memory,
237 int readonly_format
= 0;
239 /* This table maps a character into a number representing a
240 class. In each step there is a destination label for each
242 static const int jump_table
[] =
244 /* ' ' */ 1, 0, 0, /* '#' */ 4,
245 0, /* '%' */ 14, 0, /* '\''*/ 6,
246 0, 0, /* '*' */ 7, /* '+' */ 2,
247 0, /* '-' */ 3, /* '.' */ 9, 0,
248 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
249 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
250 /* '8' */ 8, /* '9' */ 8, 0, 0,
252 0, /* 'A' */ 26, 0, /* 'C' */ 25,
253 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
254 0, /* 'I' */ 29, 0, 0,
255 /* 'L' */ 12, 0, 0, 0,
256 0, 0, 0, /* 'S' */ 21,
258 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
260 0, /* 'a' */ 26, 0, /* 'c' */ 20,
261 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
262 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
263 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
264 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
265 /* 't' */ 27, /* 'u' */ 16, 0, 0,
266 /* 'x' */ 18, 0, /* 'z' */ 13
269 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
270 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
272 /* 'int' is enough and it saves some space on 64 bit systems. */
273 # define JUMP_TABLE_TYPE const int
274 # define JUMP(ChExpr, table) \
278 void *__unbounded ptr; \
280 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
281 : table[CHAR_CLASS (spec)]; \
282 ptr = &&do_form_unknown + offset; \
287 # define JUMP_TABLE_TYPE const void *const
288 # define JUMP(ChExpr, table) \
291 const void *__unbounded ptr; \
293 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
294 : table[CHAR_CLASS (spec)]; \
300 #define STEP0_3_TABLE \
301 /* Step 0: at the beginning. */ \
302 static JUMP_TABLE_TYPE step0_jumps[30] = \
304 REF (form_unknown), \
305 REF (flag_space), /* for ' ' */ \
306 REF (flag_plus), /* for '+' */ \
307 REF (flag_minus), /* for '-' */ \
308 REF (flag_hash), /* for '<hash>' */ \
309 REF (flag_zero), /* for '0' */ \
310 REF (flag_quote), /* for '\'' */ \
311 REF (width_asterics), /* for '*' */ \
312 REF (width), /* for '1'...'9' */ \
313 REF (precision), /* for '.' */ \
314 REF (mod_half), /* for 'h' */ \
315 REF (mod_long), /* for 'l' */ \
316 REF (mod_longlong), /* for 'L', 'q' */ \
317 REF (mod_size_t), /* for 'z', 'Z' */ \
318 REF (form_percent), /* for '%' */ \
319 REF (form_integer), /* for 'd', 'i' */ \
320 REF (form_unsigned), /* for 'u' */ \
321 REF (form_octal), /* for 'o' */ \
322 REF (form_hexa), /* for 'X', 'x' */ \
323 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
324 REF (form_character), /* for 'c' */ \
325 REF (form_string), /* for 's', 'S' */ \
326 REF (form_pointer), /* for 'p' */ \
327 REF (form_number), /* for 'n' */ \
328 REF (form_strerror), /* for 'm' */ \
329 REF (form_wcharacter), /* for 'C' */ \
330 REF (form_floathex), /* for 'A', 'a' */ \
331 REF (mod_ptrdiff_t), /* for 't' */ \
332 REF (mod_intmax_t), /* for 'j' */ \
333 REF (flag_i18n), /* for 'I' */ \
335 /* Step 1: after processing width. */ \
336 static JUMP_TABLE_TYPE step1_jumps[30] = \
338 REF (form_unknown), \
339 REF (form_unknown), /* for ' ' */ \
340 REF (form_unknown), /* for '+' */ \
341 REF (form_unknown), /* for '-' */ \
342 REF (form_unknown), /* for '<hash>' */ \
343 REF (form_unknown), /* for '0' */ \
344 REF (form_unknown), /* for '\'' */ \
345 REF (form_unknown), /* for '*' */ \
346 REF (form_unknown), /* for '1'...'9' */ \
347 REF (precision), /* for '.' */ \
348 REF (mod_half), /* for 'h' */ \
349 REF (mod_long), /* for 'l' */ \
350 REF (mod_longlong), /* for 'L', 'q' */ \
351 REF (mod_size_t), /* for 'z', 'Z' */ \
352 REF (form_percent), /* for '%' */ \
353 REF (form_integer), /* for 'd', 'i' */ \
354 REF (form_unsigned), /* for 'u' */ \
355 REF (form_octal), /* for 'o' */ \
356 REF (form_hexa), /* for 'X', 'x' */ \
357 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
358 REF (form_character), /* for 'c' */ \
359 REF (form_string), /* for 's', 'S' */ \
360 REF (form_pointer), /* for 'p' */ \
361 REF (form_number), /* for 'n' */ \
362 REF (form_strerror), /* for 'm' */ \
363 REF (form_wcharacter), /* for 'C' */ \
364 REF (form_floathex), /* for 'A', 'a' */ \
365 REF (mod_ptrdiff_t), /* for 't' */ \
366 REF (mod_intmax_t), /* for 'j' */ \
367 REF (form_unknown) /* for 'I' */ \
369 /* Step 2: after processing precision. */ \
370 static JUMP_TABLE_TYPE step2_jumps[30] = \
372 REF (form_unknown), \
373 REF (form_unknown), /* for ' ' */ \
374 REF (form_unknown), /* for '+' */ \
375 REF (form_unknown), /* for '-' */ \
376 REF (form_unknown), /* for '<hash>' */ \
377 REF (form_unknown), /* for '0' */ \
378 REF (form_unknown), /* for '\'' */ \
379 REF (form_unknown), /* for '*' */ \
380 REF (form_unknown), /* for '1'...'9' */ \
381 REF (form_unknown), /* for '.' */ \
382 REF (mod_half), /* for 'h' */ \
383 REF (mod_long), /* for 'l' */ \
384 REF (mod_longlong), /* for 'L', 'q' */ \
385 REF (mod_size_t), /* for 'z', 'Z' */ \
386 REF (form_percent), /* for '%' */ \
387 REF (form_integer), /* for 'd', 'i' */ \
388 REF (form_unsigned), /* for 'u' */ \
389 REF (form_octal), /* for 'o' */ \
390 REF (form_hexa), /* for 'X', 'x' */ \
391 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
392 REF (form_character), /* for 'c' */ \
393 REF (form_string), /* for 's', 'S' */ \
394 REF (form_pointer), /* for 'p' */ \
395 REF (form_number), /* for 'n' */ \
396 REF (form_strerror), /* for 'm' */ \
397 REF (form_wcharacter), /* for 'C' */ \
398 REF (form_floathex), /* for 'A', 'a' */ \
399 REF (mod_ptrdiff_t), /* for 't' */ \
400 REF (mod_intmax_t), /* for 'j' */ \
401 REF (form_unknown) /* for 'I' */ \
403 /* Step 3a: after processing first 'h' modifier. */ \
404 static JUMP_TABLE_TYPE step3a_jumps[30] = \
406 REF (form_unknown), \
407 REF (form_unknown), /* for ' ' */ \
408 REF (form_unknown), /* for '+' */ \
409 REF (form_unknown), /* for '-' */ \
410 REF (form_unknown), /* for '<hash>' */ \
411 REF (form_unknown), /* for '0' */ \
412 REF (form_unknown), /* for '\'' */ \
413 REF (form_unknown), /* for '*' */ \
414 REF (form_unknown), /* for '1'...'9' */ \
415 REF (form_unknown), /* for '.' */ \
416 REF (mod_halfhalf), /* for 'h' */ \
417 REF (form_unknown), /* for 'l' */ \
418 REF (form_unknown), /* for 'L', 'q' */ \
419 REF (form_unknown), /* for 'z', 'Z' */ \
420 REF (form_percent), /* for '%' */ \
421 REF (form_integer), /* for 'd', 'i' */ \
422 REF (form_unsigned), /* for 'u' */ \
423 REF (form_octal), /* for 'o' */ \
424 REF (form_hexa), /* for 'X', 'x' */ \
425 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
426 REF (form_unknown), /* for 'c' */ \
427 REF (form_unknown), /* for 's', 'S' */ \
428 REF (form_unknown), /* for 'p' */ \
429 REF (form_number), /* for 'n' */ \
430 REF (form_unknown), /* for 'm' */ \
431 REF (form_unknown), /* for 'C' */ \
432 REF (form_unknown), /* for 'A', 'a' */ \
433 REF (form_unknown), /* for 't' */ \
434 REF (form_unknown), /* for 'j' */ \
435 REF (form_unknown) /* for 'I' */ \
437 /* Step 3b: after processing first 'l' modifier. */ \
438 static JUMP_TABLE_TYPE step3b_jumps[30] = \
440 REF (form_unknown), \
441 REF (form_unknown), /* for ' ' */ \
442 REF (form_unknown), /* for '+' */ \
443 REF (form_unknown), /* for '-' */ \
444 REF (form_unknown), /* for '<hash>' */ \
445 REF (form_unknown), /* for '0' */ \
446 REF (form_unknown), /* for '\'' */ \
447 REF (form_unknown), /* for '*' */ \
448 REF (form_unknown), /* for '1'...'9' */ \
449 REF (form_unknown), /* for '.' */ \
450 REF (form_unknown), /* for 'h' */ \
451 REF (mod_longlong), /* for 'l' */ \
452 REF (form_unknown), /* for 'L', 'q' */ \
453 REF (form_unknown), /* for 'z', 'Z' */ \
454 REF (form_percent), /* for '%' */ \
455 REF (form_integer), /* for 'd', 'i' */ \
456 REF (form_unsigned), /* for 'u' */ \
457 REF (form_octal), /* for 'o' */ \
458 REF (form_hexa), /* for 'X', 'x' */ \
459 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
460 REF (form_character), /* for 'c' */ \
461 REF (form_string), /* for 's', 'S' */ \
462 REF (form_pointer), /* for 'p' */ \
463 REF (form_number), /* for 'n' */ \
464 REF (form_strerror), /* for 'm' */ \
465 REF (form_wcharacter), /* for 'C' */ \
466 REF (form_floathex), /* for 'A', 'a' */ \
467 REF (form_unknown), /* for 't' */ \
468 REF (form_unknown), /* for 'j' */ \
469 REF (form_unknown) /* for 'I' */ \
472 #define STEP4_TABLE \
473 /* Step 4: processing format specifier. */ \
474 static JUMP_TABLE_TYPE step4_jumps[30] = \
476 REF (form_unknown), \
477 REF (form_unknown), /* for ' ' */ \
478 REF (form_unknown), /* for '+' */ \
479 REF (form_unknown), /* for '-' */ \
480 REF (form_unknown), /* for '<hash>' */ \
481 REF (form_unknown), /* for '0' */ \
482 REF (form_unknown), /* for '\'' */ \
483 REF (form_unknown), /* for '*' */ \
484 REF (form_unknown), /* for '1'...'9' */ \
485 REF (form_unknown), /* for '.' */ \
486 REF (form_unknown), /* for 'h' */ \
487 REF (form_unknown), /* for 'l' */ \
488 REF (form_unknown), /* for 'L', 'q' */ \
489 REF (form_unknown), /* for 'z', 'Z' */ \
490 REF (form_percent), /* for '%' */ \
491 REF (form_integer), /* for 'd', 'i' */ \
492 REF (form_unsigned), /* for 'u' */ \
493 REF (form_octal), /* for 'o' */ \
494 REF (form_hexa), /* for 'X', 'x' */ \
495 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
496 REF (form_character), /* for 'c' */ \
497 REF (form_string), /* for 's', 'S' */ \
498 REF (form_pointer), /* for 'p' */ \
499 REF (form_number), /* for 'n' */ \
500 REF (form_strerror), /* for 'm' */ \
501 REF (form_wcharacter), /* for 'C' */ \
502 REF (form_floathex), /* for 'A', 'a' */ \
503 REF (form_unknown), /* for 't' */ \
504 REF (form_unknown), /* for 'j' */ \
505 REF (form_unknown) /* for 'I' */ \
509 #define process_arg(fspec) \
510 /* Start real work. We know about all flags and modifiers and \
511 now process the wanted format specifier. */ \
512 LABEL (form_percent): \
513 /* Write a literal "%". */ \
517 LABEL (form_integer): \
518 /* Signed decimal integer. */ \
523 long long int signed_number; \
526 signed_number = va_arg (ap, long long int); \
528 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
530 is_negative = signed_number < 0; \
531 number.longlong = is_negative ? (- signed_number) : signed_number; \
533 goto LABEL (longlong_number); \
537 long int signed_number; \
542 signed_number = va_arg (ap, long int); \
544 signed_number = (signed char) va_arg (ap, unsigned int); \
545 else if (!is_short) \
546 signed_number = va_arg (ap, int); \
548 signed_number = (short int) va_arg (ap, unsigned int); \
552 signed_number = args_value[fspec->data_arg].pa_long_int; \
554 signed_number = (signed char) \
555 args_value[fspec->data_arg].pa_u_int; \
556 else if (!is_short) \
557 signed_number = args_value[fspec->data_arg].pa_int; \
559 signed_number = (short int) \
560 args_value[fspec->data_arg].pa_u_int; \
562 is_negative = signed_number < 0; \
563 number.word = is_negative ? (- signed_number) : signed_number; \
565 goto LABEL (number); \
569 LABEL (form_unsigned): \
570 /* Unsigned decimal integer. */ \
572 goto LABEL (unsigned_number); \
575 LABEL (form_octal): \
576 /* Unsigned octal integer. */ \
578 goto LABEL (unsigned_number); \
582 /* Unsigned hexadecimal integer. */ \
585 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
587 /* ISO specifies the `+' and ` ' flags only for signed \
596 number.longlong = va_arg (ap, unsigned long long int); \
598 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
600 LABEL (longlong_number): \
602 /* Supply a default precision if none was given. */ \
605 /* We have to take care for the '0' flag. If a precision \
606 is given it must be ignored. */ \
609 /* If the precision is 0 and the number is 0 nothing has to \
610 be written for the number, except for the 'o' format in \
612 if (prec == 0 && number.longlong == 0) \
615 if (base == 8 && alt) \
616 *--string = L_('0'); \
620 /* Put the number in WORK. */ \
621 string = _itoa (number.longlong, workend, base, \
623 if (group && grouping) \
624 string = group_number (string, workend, grouping, \
627 if (use_outdigits && base == 10) \
628 string = _i18n_number_rewrite (string, workend); \
630 /* Simplify further test for num != 0. */ \
631 number.word = number.longlong != 0; \
638 number.word = va_arg (ap, unsigned long int); \
640 number.word = (unsigned char) va_arg (ap, unsigned int); \
641 else if (!is_short) \
642 number.word = va_arg (ap, unsigned int); \
644 number.word = (unsigned short int) va_arg (ap, unsigned int); \
648 number.word = args_value[fspec->data_arg].pa_u_long_int; \
650 number.word = (unsigned char) \
651 args_value[fspec->data_arg].pa_u_int; \
652 else if (!is_short) \
653 number.word = args_value[fspec->data_arg].pa_u_int; \
655 number.word = (unsigned short int) \
656 args_value[fspec->data_arg].pa_u_int; \
660 /* Supply a default precision if none was given. */ \
663 /* We have to take care for the '0' flag. If a precision \
664 is given it must be ignored. */ \
667 /* If the precision is 0 and the number is 0 nothing has to \
668 be written for the number, except for the 'o' format in \
670 if (prec == 0 && number.word == 0) \
673 if (base == 8 && alt) \
674 *--string = L_('0'); \
678 /* Put the number in WORK. */ \
679 string = _itoa_word (number.word, workend, base, \
681 if (group && grouping) \
682 string = group_number (string, workend, grouping, \
685 if (use_outdigits && base == 10) \
686 string = _i18n_number_rewrite (string, workend); \
690 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
691 /* Add octal marker. */ \
692 *--string = L_('0'); \
694 prec = MAX (0, prec - (workend - string)); \
698 width -= workend - string + prec; \
700 if (number.word != 0 && alt && base == 16) \
701 /* Account for 0X hex marker. */ \
704 if (is_negative || showsign || space) \
707 if (pad == L_(' ')) \
720 if (number.word != 0 && alt && base == 16) \
729 outstring (string, workend - string); \
751 if (number.word != 0 && alt && base == 16) \
758 width -= workend - string + prec; \
768 outstring (string, workend - string); \
774 LABEL (form_float): \
776 /* Floating-point number. This is handled by printf_fp.c. */ \
783 is_long_double = 0; \
785 struct printf_info info = { .prec = prec, \
788 .is_long_double = is_long_double, \
789 .is_short = is_short, \
790 .is_long = is_long, \
794 .showsign = showsign, \
798 .i18n = use_outdigits, \
799 .wide = sizeof (CHAR_T) != 1 }; \
801 if (is_long_double) \
802 the_arg.pa_long_double = va_arg (ap, long double); \
804 the_arg.pa_double = va_arg (ap, double); \
805 ptr = (const void *) &the_arg; \
807 function_done = __printf_fp (s, &info, &ptr); \
811 ptr = (const void *) &args_value[fspec->data_arg]; \
814 fspec->data_arg_type = PA_DOUBLE; \
815 fspec->info.is_long_double = 0; \
818 function_done = __printf_fp (s, &fspec->info, &ptr); \
821 if (function_done < 0) \
823 /* Error in print handler. */ \
828 done_add (function_done); \
832 LABEL (form_floathex): \
834 /* Floating point number printed as hexadecimal number. */ \
841 is_long_double = 0; \
843 struct printf_info info = { .prec = prec, \
846 .is_long_double = is_long_double, \
847 .is_short = is_short, \
848 .is_long = is_long, \
852 .showsign = showsign, \
856 .wide = sizeof (CHAR_T) != 1 }; \
858 if (is_long_double) \
859 the_arg.pa_long_double = va_arg (ap, long double); \
861 the_arg.pa_double = va_arg (ap, double); \
862 ptr = (const void *) &the_arg; \
864 function_done = __printf_fphex (s, &info, &ptr); \
868 ptr = (const void *) &args_value[fspec->data_arg]; \
870 fspec->info.is_long_double = 0; \
872 function_done = __printf_fphex (s, &fspec->info, &ptr); \
875 if (function_done < 0) \
877 /* Error in print handler. */ \
882 done_add (function_done); \
886 LABEL (form_pointer): \
887 /* Generic pointer. */ \
891 ptr = va_arg (ap, void *); \
893 ptr = args_value[fspec->data_arg].pa_pointer; \
896 /* If the pointer is not NULL, write it as a %#x spec. */ \
898 number.word = (unsigned long int) ptr; \
903 goto LABEL (number); \
907 /* Write "(nil)" for a nil pointer. */ \
908 string = (CHAR_T *) L_("(nil)"); \
909 /* Make sure the full string "(nil)" is printed. */ \
912 is_long = 0; /* This is no wide-char string. */ \
913 goto LABEL (print_string); \
918 LABEL (form_number): \
919 if (s->_flags2 & _IO_FLAGS2_FORTIFY) \
921 if (! readonly_format) \
923 extern int __readonly_area (const void *, size_t) \
926 = __readonly_area (format, ((STR_LEN (format) + 1) \
927 * sizeof (CHAR_T))); \
929 if (readonly_format < 0) \
930 __libc_fatal ("*** %n in writable segment detected ***\n"); \
932 /* Answer the count of characters written. */ \
936 *(long long int *) va_arg (ap, void *) = done; \
937 else if (is_long_num) \
938 *(long int *) va_arg (ap, void *) = done; \
940 *(char *) va_arg (ap, void *) = done; \
941 else if (!is_short) \
942 *(int *) va_arg (ap, void *) = done; \
944 *(short int *) va_arg (ap, void *) = done; \
948 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
949 else if (is_long_num) \
950 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
952 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
953 else if (!is_short) \
954 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
956 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
959 LABEL (form_strerror): \
960 /* Print description of error ERRNO. */ \
962 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
963 sizeof work_buffer); \
964 is_long = 0; /* This is no wide-char string. */ \
965 goto LABEL (print_string)
967 #ifdef COMPILE_WPRINTF
968 # define process_string_arg(fspec) \
969 LABEL (form_character): \
972 goto LABEL (form_wcharacter); \
973 --width; /* Account for the character itself. */ \
977 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
979 outchar (__btowc ((unsigned char) \
980 args_value[fspec->data_arg].pa_int)); \
985 LABEL (form_wcharacter): \
987 /* Wide character. */ \
992 outchar (va_arg (ap, wchar_t)); \
994 outchar (args_value[fspec->data_arg].pa_wchar); \
1000 LABEL (form_string): \
1003 int string_malloced; \
1005 /* The string argument could in fact be `char *' or `wchar_t *'. \
1006 But this should not make a difference here. */ \
1007 if (fspec == NULL) \
1008 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
1010 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
1012 /* Entry point for printing other strings. */ \
1013 LABEL (print_string): \
1015 string_malloced = 0; \
1016 if (string == NULL) \
1018 /* Write "(null)" if there's space. */ \
1020 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
1022 string = (CHAR_T *) null; \
1023 len = (sizeof (null) / sizeof (null[0])) - 1; \
1027 string = (CHAR_T *) L""; \
1031 else if (!is_long && spec != L_('S')) \
1033 /* This is complicated. We have to transform the multibyte \
1034 string into a wide character string. */ \
1035 const char *mbs = (const char *) string; \
1036 mbstate_t mbstate; \
1038 len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
1040 /* Allocate dynamically an array which definitely is long \
1041 enough for the wide character version. Each byte in the \
1042 multi-byte string can produce at most one wide character. */ \
1043 if (__libc_use_alloca (len * sizeof (wchar_t))) \
1044 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1045 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1052 string_malloced = 1; \
1054 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1055 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1056 if (len == (size_t) -1) \
1058 /* Illegal multibyte character. */ \
1066 /* Search for the end of the string, but don't search past \
1067 the length specified by the precision. */ \
1068 len = __wcsnlen (string, prec); \
1070 len = __wcslen (string); \
1073 if ((width -= len) < 0) \
1075 outstring (string, len); \
1081 outstring (string, len); \
1084 if (__builtin_expect (string_malloced, 0)) \
1089 # define process_string_arg(fspec) \
1090 LABEL (form_character): \
1093 goto LABEL (form_wcharacter); \
1094 --width; /* Account for the character itself. */ \
1097 if (fspec == NULL) \
1098 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1100 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1105 LABEL (form_wcharacter): \
1107 /* Wide character. */ \
1108 char buf[MB_CUR_MAX]; \
1109 mbstate_t mbstate; \
1112 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1113 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1114 : args_value[fspec->data_arg].pa_wchar), \
1116 if (len == (size_t) -1) \
1118 /* Something went wron gduring the conversion. Bail out. */ \
1125 outstring (buf, len); \
1131 LABEL (form_string): \
1134 int string_malloced; \
1136 /* The string argument could in fact be `char *' or `wchar_t *'. \
1137 But this should not make a difference here. */ \
1138 if (fspec == NULL) \
1139 string = (char *) va_arg (ap, const char *); \
1141 string = (char *) args_value[fspec->data_arg].pa_string; \
1143 /* Entry point for printing other strings. */ \
1144 LABEL (print_string): \
1146 string_malloced = 0; \
1147 if (string == NULL) \
1149 /* Write "(null)" if there's space. */ \
1150 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1152 string = (char *) null; \
1153 len = sizeof (null) - 1; \
1157 string = (char *) ""; \
1161 else if (!is_long && spec != L_('S')) \
1165 /* Search for the end of the string, but don't search past \
1166 the length (in bytes) specified by the precision. Also \
1167 don't use incomplete characters. */ \
1168 if (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MB_CUR_MAX) == 1) \
1169 len = __strnlen (string, prec); \
1172 /* In case we have a multibyte character set the \
1173 situation is more complicated. We must not copy \
1174 bytes at the end which form an incomplete character. */\
1175 size_t ignore_size = (unsigned) prec > 1024 ? 1024 : prec;\
1176 wchar_t ignore[ignore_size]; \
1177 const char *str2 = string; \
1178 const char *strend = string + prec; \
1179 if (strend < string) \
1180 strend = (const char *) UINTPTR_MAX; \
1183 memset (&ps, '\0', sizeof (ps)); \
1185 while (str2 != NULL && str2 < strend) \
1186 if (__mbsnrtowcs (ignore, &str2, strend - str2, \
1187 ignore_size, &ps) == (size_t) -1) \
1194 len = strlen (string); \
1196 len = str2 - string - (ps.__count & 7); \
1200 len = strlen (string); \
1204 const wchar_t *s2 = (const wchar_t *) string; \
1205 mbstate_t mbstate; \
1207 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1211 /* The string `s2' might not be NUL terminated. */ \
1212 if (__libc_use_alloca (prec)) \
1213 string = (char *) alloca (prec); \
1214 else if ((string = (char *) malloc (prec)) == NULL) \
1220 string_malloced = 1; \
1221 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1225 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1226 if (len != (size_t) -1) \
1228 assert (__mbsinit (&mbstate)); \
1229 s2 = (const wchar_t *) string; \
1230 if (__libc_use_alloca (len + 1)) \
1231 string = (char *) alloca (len + 1); \
1232 else if ((string = (char *) malloc (len + 1)) == NULL) \
1238 string_malloced = 1; \
1239 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1243 if (len == (size_t) -1) \
1245 /* Illegal wide-character string. */ \
1251 if ((width -= len) < 0) \
1253 outstring (string, len); \
1259 outstring (string, len); \
1262 if (__builtin_expect (string_malloced, 0)) \
1268 /* Orient the stream. */
1273 /* Sanity check of arguments. */
1274 ARGCHECK (s
, format
);
1277 /* Check for correct orientation. */
1278 if (_IO_vtable_offset (s
) == 0 &&
1279 _IO_fwide (s
, sizeof (CHAR_T
) == 1 ? -1 : 1)
1280 != (sizeof (CHAR_T
) == 1 ? -1 : 1))
1281 /* The stream is already oriented otherwise. */
1285 if (UNBUFFERED_P (s
))
1286 /* Use a helper function which will allocate a local temporary buffer
1287 for the stream and then call us again. */
1288 return buffered_vfprintf (s
, format
, ap
);
1290 /* Initialize local variables. */
1292 grouping
= (const char *) -1;
1294 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1295 since on some systems `va_list' is not an integral type. */
1296 __va_copy (ap_save
, ap
);
1302 #ifdef COMPILE_WPRINTF
1303 /* Find the first format specifier. */
1304 f
= lead_str_end
= __find_specwc ((const UCHAR_T
*) format
);
1306 /* Find the first format specifier. */
1307 f
= lead_str_end
= __find_specmb ((const UCHAR_T
*) format
);
1311 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile
, s
);
1314 /* Write the literal text before the first format. */
1315 outstring ((const UCHAR_T
*) format
,
1316 lead_str_end
- (const UCHAR_T
*) format
);
1318 /* If we only have to print a simple string, return now. */
1322 /* Process whole format string. */
1326 # define REF(Name) &&do_##Name - &&do_form_unknown
1328 # define REF(Name) &&do_##Name
1330 #define LABEL(Name) do_##Name
1334 union printf_arg
*args_value
; /* This is not used here but ... */
1335 int is_negative
; /* Flag for negative number. */
1338 unsigned long long int longlong
;
1339 unsigned long int word
;
1342 union printf_arg the_arg
;
1343 CHAR_T
*string
; /* Pointer to argument string. */
1344 int alt
= 0; /* Alternate format. */
1345 int space
= 0; /* Use space prefix if no sign is needed. */
1346 int left
= 0; /* Left-justify output. */
1347 int showsign
= 0; /* Always begin with plus or minus sign. */
1348 int group
= 0; /* Print numbers according grouping rules. */
1349 int is_long_double
= 0; /* Argument is long double/ long long int. */
1350 int is_short
= 0; /* Argument is short int. */
1351 int is_long
= 0; /* Argument is long int. */
1352 int is_char
= 0; /* Argument is promoted (unsigned) char. */
1353 int width
= 0; /* Width of output; 0 means none specified. */
1354 int prec
= -1; /* Precision of output; -1 means none specified. */
1355 /* This flag is set by the 'I' modifier and selects the use of the
1356 `outdigits' as determined by the current locale. */
1357 int use_outdigits
= 0;
1358 UCHAR_T pad
= L_(' ');/* Padding character. */
1362 workend
= &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
1364 /* Get current character in format string. */
1365 JUMP (*++f
, step0_jumps
);
1370 JUMP (*++f
, step0_jumps
);
1375 JUMP (*++f
, step0_jumps
);
1381 JUMP (*++f
, step0_jumps
);
1386 JUMP (*++f
, step0_jumps
);
1392 JUMP (*++f
, step0_jumps
);
1394 /* The '\'' flag. */
1398 if (grouping
== (const char *) -1)
1400 #ifdef COMPILE_WPRINTF
1401 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1402 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1404 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1407 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1408 if (*grouping
== '\0' || *grouping
== CHAR_MAX
1409 #ifdef COMPILE_WPRINTF
1410 || thousands_sep
== L
'\0'
1412 || *thousands_sep
== '\0'
1417 JUMP (*++f
, step0_jumps
);
1421 JUMP (*++f
, step0_jumps
);
1423 /* Get width from argument. */
1424 LABEL (width_asterics
):
1426 const UCHAR_T
*tmp
; /* Temporary value. */
1429 if (ISDIGIT (*tmp
) && read_int (&tmp
) && *tmp
== L_('$'))
1430 /* The width comes from a positional parameter. */
1433 width
= va_arg (ap
, int);
1435 /* Negative width means left justified. */
1443 if (width
+ 32 >= (int) (sizeof (work_buffer
)
1444 / sizeof (work_buffer
[0])))
1446 /* We have to use a special buffer. The "32" is just a safe
1447 bet for all the output which is not counted in the width. */
1448 if (__libc_use_alloca ((width
+ 32) * sizeof (CHAR_T
)))
1449 workend
= ((CHAR_T
*) alloca ((width
+ 32) * sizeof (CHAR_T
))
1453 workstart
= (CHAR_T
*) malloc ((width
+ 32) * sizeof (CHAR_T
));
1454 if (workstart
== NULL
)
1459 workend
= workstart
+ (width
+ 32);
1463 JUMP (*f
, step1_jumps
);
1465 /* Given width in format string. */
1467 width
= read_int (&f
);
1469 if (width
+ 32 >= (int) (sizeof (work_buffer
) / sizeof (work_buffer
[0])))
1471 /* We have to use a special buffer. The "32" is just a safe
1472 bet for all the output which is not counted in the width. */
1473 if (__libc_use_alloca ((width
+ 32) * sizeof (CHAR_T
)))
1474 workend
= ((CHAR_T
*) alloca ((width
+ 32) * sizeof (CHAR_T
))
1478 workstart
= (CHAR_T
*) malloc ((width
+ 32) * sizeof (CHAR_T
));
1479 if (workstart
== NULL
)
1484 workend
= workstart
+ (width
+ 32);
1488 /* Oh, oh. The argument comes from a positional parameter. */
1490 JUMP (*f
, step1_jumps
);
1496 const UCHAR_T
*tmp
; /* Temporary value. */
1499 if (ISDIGIT (*tmp
) && read_int (&tmp
) > 0 && *tmp
== L_('$'))
1500 /* The precision comes from a positional parameter. */
1503 prec
= va_arg (ap
, int);
1505 /* If the precision is negative the precision is omitted. */
1509 else if (ISDIGIT (*f
))
1510 prec
= read_int (&f
);
1514 && prec
+ 32 > (int)(sizeof (work_buffer
) / sizeof (work_buffer
[0])))
1516 if (__builtin_expect (prec
> ~((size_t) 0) / sizeof (CHAR_T
) - 31,
1522 size_t needed
= ((size_t) prec
+ 32) * sizeof (CHAR_T
);
1524 if (__libc_use_alloca (needed
))
1525 workend
= (((CHAR_T
*) alloca (needed
)) + ((size_t) prec
+ 32));
1528 workstart
= (CHAR_T
*) malloc (needed
);
1529 if (workstart
== NULL
)
1534 workend
= workstart
+ ((size_t) prec
+ 32);
1537 JUMP (*f
, step2_jumps
);
1539 /* Process 'h' modifier. There might another 'h' following. */
1542 JUMP (*++f
, step3a_jumps
);
1544 /* Process 'hh' modifier. */
1545 LABEL (mod_halfhalf
):
1548 JUMP (*++f
, step4_jumps
);
1550 /* Process 'l' modifier. There might another 'l' following. */
1553 JUMP (*++f
, step3b_jumps
);
1555 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1556 allowed to follow. */
1557 LABEL (mod_longlong
):
1560 JUMP (*++f
, step4_jumps
);
1563 is_long_double
= sizeof (size_t) > sizeof (unsigned long int);
1564 is_long
= sizeof (size_t) > sizeof (unsigned int);
1565 JUMP (*++f
, step4_jumps
);
1567 LABEL (mod_ptrdiff_t
):
1568 is_long_double
= sizeof (ptrdiff_t) > sizeof (unsigned long int);
1569 is_long
= sizeof (ptrdiff_t) > sizeof (unsigned int);
1570 JUMP (*++f
, step4_jumps
);
1572 LABEL (mod_intmax_t
):
1573 is_long_double
= sizeof (intmax_t) > sizeof (unsigned long int);
1574 is_long
= sizeof (intmax_t) > sizeof (unsigned int);
1575 JUMP (*++f
, step4_jumps
);
1577 /* Process current format. */
1580 process_arg (((struct printf_spec
*) NULL
));
1581 process_string_arg (((struct printf_spec
*) NULL
));
1583 LABEL (form_unknown
):
1584 if (spec
== L_('\0'))
1586 /* The format string ended before the specifier is complete. */
1591 /* If we are in the fast loop force entering the complicated
1596 /* The format is correctly handled. */
1599 if (__builtin_expect (workstart
!= NULL
, 0))
1603 /* Look for next format specifier. */
1604 #ifdef COMPILE_WPRINTF
1605 f
= __find_specwc ((end_of_spec
= ++f
));
1607 f
= __find_specmb ((end_of_spec
= ++f
));
1610 /* Write the following constant string. */
1611 outstring (end_of_spec
, f
- end_of_spec
);
1613 while (*f
!= L_('\0'));
1615 /* Unlock stream and return. */
1618 /* Here starts the more complex loop to handle positional parameters. */
1621 /* Array with information about the needed arguments. This has to
1622 be dynamically extensible. */
1624 size_t nspecs_max
= 32; /* A more or less arbitrary start value. */
1625 struct printf_spec
*specs
1626 = alloca (nspecs_max
* sizeof (struct printf_spec
));
1628 /* The number of arguments the format string requests. This will
1629 determine the size of the array needed to store the argument
1633 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
;
1668 specs
= alloca (nspecs_max
* sizeof (struct printf_spec
));
1670 if (specs
== &old
[nspecs
])
1671 /* Stack grows up, OLD was the last thing allocated;
1673 nspecs_max
+= nspecs_max
/ 2;
1676 /* Copy the old array's elements to the new space. */
1677 memcpy (specs
, old
, nspecs
* sizeof (struct printf_spec
));
1678 if (old
== &specs
[nspecs
])
1679 /* Stack grows down, OLD was just below the new
1680 SPECS. We can use that space when the new space
1682 nspecs_max
+= nspecs_max
/ 2;
1686 /* Parse the format specifier. */
1687 #ifdef COMPILE_WPRINTF
1688 nargs
+= __parse_one_specwc (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1690 nargs
+= __parse_one_specmb (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1694 /* Determine the number of arguments the format string consumes. */
1695 nargs
= MAX (nargs
, max_ref_arg
);
1697 /* Allocate memory for the argument descriptions. */
1698 args_type
= alloca (nargs
* sizeof (int));
1699 memset (args_type
, s
->_flags2
& _IO_FLAGS2_FORTIFY
? '\xff' : '\0',
1700 nargs
* sizeof (int));
1701 args_value
= alloca (nargs
* sizeof (union printf_arg
));
1703 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1704 still zero after this loop, format is invalid. For now we
1705 simply use 0 as the value. */
1707 /* Fill in the types of all the arguments. */
1708 for (cnt
= 0; cnt
< nspecs
; ++cnt
)
1710 /* If the width is determined by an argument this is an int. */
1711 if (specs
[cnt
].width_arg
!= -1)
1712 args_type
[specs
[cnt
].width_arg
] = PA_INT
;
1714 /* If the precision is determined by an argument this is an int. */
1715 if (specs
[cnt
].prec_arg
!= -1)
1716 args_type
[specs
[cnt
].prec_arg
] = PA_INT
;
1718 switch (specs
[cnt
].ndata_args
)
1720 case 0: /* No arguments. */
1722 case 1: /* One argument; we already have the type. */
1723 args_type
[specs
[cnt
].data_arg
] = specs
[cnt
].data_arg_type
;
1726 /* We have more than one argument for this format spec.
1727 We must call the arginfo function again to determine
1729 (void) (*__printf_arginfo_table
[specs
[cnt
].info
.spec
])
1731 specs
[cnt
].ndata_args
, &args_type
[specs
[cnt
].data_arg
]);
1736 /* Now we know all the types and the order. Fill in the argument
1738 for (cnt
= 0; cnt
< nargs
; ++cnt
)
1739 switch (args_type
[cnt
])
1741 #define T(tag, mem, type) \
1743 args_value[cnt].mem = va_arg (ap_save, type); \
1746 T (PA_CHAR
, pa_int
, int); /* Promoted. */
1747 T (PA_WCHAR
, pa_wchar
, wint_t);
1748 T (PA_INT
|PA_FLAG_SHORT
, pa_int
, int); /* Promoted. */
1749 T (PA_INT
, pa_int
, int);
1750 T (PA_INT
|PA_FLAG_LONG
, pa_long_int
, long int);
1751 T (PA_INT
|PA_FLAG_LONG_LONG
, pa_long_long_int
, long long int);
1752 T (PA_FLOAT
, pa_double
, double); /* Promoted. */
1753 T (PA_DOUBLE
, pa_double
, double);
1754 case PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
:
1757 args_value
[cnt
].pa_double
= va_arg (ap_save
, double);
1758 args_type
[cnt
] &= ~PA_FLAG_LONG_DOUBLE
;
1761 args_value
[cnt
].pa_long_double
= va_arg (ap_save
, long double);
1763 T (PA_STRING
, pa_string
, const char *);
1764 T (PA_WSTRING
, pa_wstring
, const wchar_t *);
1765 T (PA_POINTER
, pa_pointer
, void *);
1768 if ((args_type
[cnt
] & PA_FLAG_PTR
) != 0)
1769 args_value
[cnt
].pa_pointer
= va_arg (ap_save
, void *);
1771 args_value
[cnt
].pa_long_double
= 0.0;
1774 /* Error case. Not all parameters appear in N$ format
1775 strings. We have no way to determine their type. */
1776 assert (s
->_flags2
& _IO_FLAGS2_FORTIFY
);
1777 __libc_fatal ("*** invalid %N$ use detected ***\n");
1780 /* Now walk through all format specifiers and process them. */
1781 for (; (size_t) nspecs_done
< nspecs
; ++nspecs_done
)
1785 # define REF(Name) &&do2_##Name - &&do_form_unknown
1787 # define REF(Name) &&do2_##Name
1790 #define LABEL(Name) do2_##Name
1796 unsigned long long int longlong
;
1797 unsigned long int word
;
1800 union printf_arg the_arg
;
1801 CHAR_T
*string
; /* Pointer to argument string. */
1803 /* Fill variables from values in struct. */
1804 int alt
= specs
[nspecs_done
].info
.alt
;
1805 int space
= specs
[nspecs_done
].info
.space
;
1806 int left
= specs
[nspecs_done
].info
.left
;
1807 int showsign
= specs
[nspecs_done
].info
.showsign
;
1808 int group
= specs
[nspecs_done
].info
.group
;
1809 int is_long_double
= specs
[nspecs_done
].info
.is_long_double
;
1810 int is_short
= specs
[nspecs_done
].info
.is_short
;
1811 int is_char
= specs
[nspecs_done
].info
.is_char
;
1812 int is_long
= specs
[nspecs_done
].info
.is_long
;
1813 int width
= specs
[nspecs_done
].info
.width
;
1814 int prec
= specs
[nspecs_done
].info
.prec
;
1815 int use_outdigits
= specs
[nspecs_done
].info
.i18n
;
1816 char pad
= specs
[nspecs_done
].info
.pad
;
1817 CHAR_T spec
= specs
[nspecs_done
].info
.spec
;
1820 workend
= &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
1822 /* Fill in last information. */
1823 if (specs
[nspecs_done
].width_arg
!= -1)
1825 /* Extract the field width from an argument. */
1826 specs
[nspecs_done
].info
.width
=
1827 args_value
[specs
[nspecs_done
].width_arg
].pa_int
;
1829 if (specs
[nspecs_done
].info
.width
< 0)
1830 /* If the width value is negative left justification is
1831 selected and the value is taken as being positive. */
1833 specs
[nspecs_done
].info
.width
*= -1;
1834 left
= specs
[nspecs_done
].info
.left
= 1;
1836 width
= specs
[nspecs_done
].info
.width
;
1839 if (specs
[nspecs_done
].prec_arg
!= -1)
1841 /* Extract the precision from an argument. */
1842 specs
[nspecs_done
].info
.prec
=
1843 args_value
[specs
[nspecs_done
].prec_arg
].pa_int
;
1845 if (specs
[nspecs_done
].info
.prec
< 0)
1846 /* If the precision is negative the precision is
1848 specs
[nspecs_done
].info
.prec
= -1;
1850 prec
= specs
[nspecs_done
].info
.prec
;
1853 /* Maybe the buffer is too small. */
1854 if (MAX (prec
, width
) + 32 > (int) (sizeof (work_buffer
)
1857 if (__libc_use_alloca ((MAX (prec
, width
) + 32)
1859 workend
= ((CHAR_T
*) alloca ((MAX (prec
, width
) + 32)
1861 + (MAX (prec
, width
) + 32));
1864 workstart
= (CHAR_T
*) malloc ((MAX (prec
, width
) + 32)
1866 workend
= workstart
+ (MAX (prec
, width
) + 32);
1870 /* Process format specifiers. */
1873 JUMP (spec
, step4_jumps
);
1875 process_arg ((&specs
[nspecs_done
]));
1876 process_string_arg ((&specs
[nspecs_done
]));
1878 LABEL (form_unknown
):
1880 extern printf_function
**__printf_function_table
;
1882 printf_function
*function
;
1887 (__printf_function_table
== NULL
? NULL
:
1888 __printf_function_table
[specs
[nspecs_done
].info
.spec
]);
1890 if (function
== NULL
)
1891 function
= &printf_unknown
;
1893 ptr
= alloca (specs
[nspecs_done
].ndata_args
1894 * sizeof (const void *));
1896 /* Fill in an array of pointers to the argument values. */
1897 for (i
= 0; i
< specs
[nspecs_done
].ndata_args
; ++i
)
1898 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
1900 /* Call the function. */
1901 function_done
= (*function
) (s
, &specs
[nspecs_done
].info
, ptr
);
1903 /* If an error occurred we don't have information about #
1905 if (function_done
< 0)
1911 done_add (function_done
);
1919 /* Write the following constant string. */
1920 outstring (specs
[nspecs_done
].end_of_fmt
,
1921 specs
[nspecs_done
].next_fmt
1922 - specs
[nspecs_done
].end_of_fmt
);
1927 if (__builtin_expect (workstart
!= NULL
, 0))
1929 /* Unlock the stream. */
1930 _IO_funlockfile (s
);
1931 _IO_cleanup_region_end (0);
1936 /* Handle an unknown format specifier. This prints out a canonicalized
1937 representation of the format spec itself. */
1939 printf_unknown (FILE *s
, const struct printf_info
*info
,
1940 const void *const *args
)
1944 CHAR_T work_buffer
[MAX (sizeof (info
->width
), sizeof (info
->prec
)) * 3];
1945 CHAR_T
*const workend
1946 = &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
1957 else if (info
->space
)
1961 if (info
->pad
== L_('0'))
1966 if (info
->width
!= 0)
1968 w
= _itoa_word (info
->width
, workend
, 10, 0);
1973 if (info
->prec
!= -1)
1976 w
= _itoa_word (info
->prec
, workend
, 10, 0);
1981 if (info
->spec
!= L_('\0'))
1982 outchar (info
->spec
);
1988 /* Group the digits according to the grouping rules of the current locale.
1989 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1992 group_number (CHAR_T
*w
, CHAR_T
*rear_ptr
, const char *grouping
,
1993 #ifdef COMPILE_WPRINTF
1994 wchar_t thousands_sep
1996 const char *thousands_sep
2002 #ifndef COMPILE_WPRINTF
2003 int tlen
= strlen (thousands_sep
);
2006 /* We treat all negative values like CHAR_MAX. */
2008 if (*grouping
== CHAR_MAX
|| *grouping
<= 0)
2009 /* No grouping should be done. */
2014 /* Copy existing string so that nothing gets overwritten. */
2015 src
= (CHAR_T
*) alloca ((rear_ptr
- w
) * sizeof (CHAR_T
));
2016 s
= (CHAR_T
*) __mempcpy (src
, w
,
2017 (rear_ptr
- w
) * sizeof (CHAR_T
));
2020 /* Process all characters in the string. */
2025 if (--len
== 0 && s
> src
)
2027 /* A new group begins. */
2028 #ifdef COMPILE_WPRINTF
2029 *--w
= thousands_sep
;
2033 *--w
= thousands_sep
[--cnt
];
2037 if (*grouping
== CHAR_MAX
2043 /* No further grouping to be done.
2044 Copy the rest of the number. */
2050 else if (*grouping
!= '\0')
2051 /* The previous grouping repeats ad infinitum. */
2060 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2063 struct _IO_FILE_plus _f
;
2064 #ifdef COMPILE_WPRINTF
2065 struct _IO_wide_data _wide_data
;
2067 _IO_FILE
*_put_stream
;
2068 #ifdef _IO_MTSAFE_IO
2074 _IO_helper_overflow (_IO_FILE
*s
, int c
)
2076 _IO_FILE
*target
= ((struct helper_file
*) s
)->_put_stream
;
2077 #ifdef COMPILE_WPRINTF
2078 int used
= s
->_wide_data
->_IO_write_ptr
- s
->_wide_data
->_IO_write_base
;
2081 _IO_size_t written
= _IO_sputn (target
, s
->_wide_data
->_IO_write_base
,
2085 s
->_wide_data
->_IO_write_ptr
-= written
;
2088 int used
= s
->_IO_write_ptr
- s
->_IO_write_base
;
2091 _IO_size_t written
= _IO_sputn (target
, s
->_IO_write_base
, used
);
2094 s
->_IO_write_ptr
-= written
;
2100 #ifdef COMPILE_WPRINTF
2101 static const struct _IO_jump_t _IO_helper_jumps
=
2104 JUMP_INIT (finish
, INTUSE(_IO_wdefault_finish
)),
2105 JUMP_INIT (overflow
, _IO_helper_overflow
),
2106 JUMP_INIT (underflow
, _IO_default_underflow
),
2107 JUMP_INIT (uflow
, INTUSE(_IO_default_uflow
)),
2108 JUMP_INIT (pbackfail
, (_IO_pbackfail_t
) INTUSE(_IO_wdefault_pbackfail
)),
2109 JUMP_INIT (xsputn
, INTUSE(_IO_wdefault_xsputn
)),
2110 JUMP_INIT (xsgetn
, INTUSE(_IO_wdefault_xsgetn
)),
2111 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2112 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2113 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2114 JUMP_INIT (sync
, _IO_default_sync
),
2115 JUMP_INIT (doallocate
, INTUSE(_IO_wdefault_doallocate
)),
2116 JUMP_INIT (read
, _IO_default_read
),
2117 JUMP_INIT (write
, _IO_default_write
),
2118 JUMP_INIT (seek
, _IO_default_seek
),
2119 JUMP_INIT (close
, _IO_default_close
),
2120 JUMP_INIT (stat
, _IO_default_stat
)
2123 static const struct _IO_jump_t _IO_helper_jumps
=
2126 JUMP_INIT (finish
, INTUSE(_IO_default_finish
)),
2127 JUMP_INIT (overflow
, _IO_helper_overflow
),
2128 JUMP_INIT (underflow
, _IO_default_underflow
),
2129 JUMP_INIT (uflow
, INTUSE(_IO_default_uflow
)),
2130 JUMP_INIT (pbackfail
, INTUSE(_IO_default_pbackfail
)),
2131 JUMP_INIT (xsputn
, INTUSE(_IO_default_xsputn
)),
2132 JUMP_INIT (xsgetn
, INTUSE(_IO_default_xsgetn
)),
2133 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2134 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2135 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2136 JUMP_INIT (sync
, _IO_default_sync
),
2137 JUMP_INIT (doallocate
, INTUSE(_IO_default_doallocate
)),
2138 JUMP_INIT (read
, _IO_default_read
),
2139 JUMP_INIT (write
, _IO_default_write
),
2140 JUMP_INIT (seek
, _IO_default_seek
),
2141 JUMP_INIT (close
, _IO_default_close
),
2142 JUMP_INIT (stat
, _IO_default_stat
)
2148 buffered_vfprintf (register _IO_FILE
*s
, const CHAR_T
*format
,
2151 CHAR_T buf
[_IO_BUFSIZ
];
2152 struct helper_file helper
;
2153 register _IO_FILE
*hp
= (_IO_FILE
*) &helper
._f
;
2154 int result
, to_flush
;
2156 /* Orient the stream. */
2161 /* Initialize helper. */
2162 helper
._put_stream
= s
;
2163 #ifdef COMPILE_WPRINTF
2164 hp
->_wide_data
= &helper
._wide_data
;
2165 _IO_wsetp (hp
, buf
, buf
+ sizeof buf
/ sizeof (CHAR_T
));
2168 _IO_setp (hp
, buf
, buf
+ sizeof buf
);
2171 hp
->_IO_file_flags
= _IO_MAGIC
|_IO_NO_READS
|_IO_USER_LOCK
;
2172 #if _IO_JUMPS_OFFSET
2173 hp
->_vtable_offset
= 0;
2175 #ifdef _IO_MTSAFE_IO
2178 hp
->_flags2
= s
->_flags2
;
2179 _IO_JUMPS (&helper
._f
) = (struct _IO_jump_t
*) &_IO_helper_jumps
;
2181 /* Now print to helper instead. */
2182 #ifndef COMPILE_WPRINTF
2183 result
= INTUSE(_IO_vfprintf
) (hp
, format
, args
);
2185 result
= vfprintf (hp
, format
, args
);
2189 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile
, s
);
2192 /* Now flush anything from the helper to the S. */
2193 #ifdef COMPILE_WPRINTF
2194 if ((to_flush
= (hp
->_wide_data
->_IO_write_ptr
2195 - hp
->_wide_data
->_IO_write_base
)) > 0)
2197 if ((int) _IO_sputn (s
, hp
->_wide_data
->_IO_write_base
, to_flush
)
2202 if ((to_flush
= hp
->_IO_write_ptr
- hp
->_IO_write_base
) > 0)
2204 if ((int) _IO_sputn (s
, hp
->_IO_write_base
, to_flush
) != to_flush
)
2209 /* Unlock the stream. */
2210 _IO_funlockfile (s
);
2211 __libc_cleanup_region_end (0);
2217 #ifdef COMPILE_WPRINTF
2218 strong_alias (_IO_vfwprintf
, __vfwprintf
);
2219 ldbl_weak_alias (_IO_vfwprintf
, vfwprintf
);
2221 ldbl_strong_alias (_IO_vfprintf_internal
, vfprintf
);
2222 ldbl_hidden_def (_IO_vfprintf_internal
, vfprintf
)
2223 ldbl_strong_alias (_IO_vfprintf_internal
, _IO_vfprintf
);