1 /* Copyright (C) 1991-2008, 2009, 2010, 2011 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
27 #include <bits/libc-lock.h>
28 #include <sys/param.h>
30 #include <locale/localeinfo.h>
33 /* This code is shared between the standard stdio implementation found
34 in GNU C library and the libio implementation originally found in
37 Beside this it is also shared between the normal and wide character
38 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
44 #define va_list _IO_va_list
46 #define BUFSIZ _IO_BUFSIZ
47 #define ARGCHECK(S, Format) \
50 /* Check file argument for consistence. */ \
52 if (S->_flags & _IO_NO_WRITES) \
54 S->_flags |= _IO_ERR_SEEN; \
55 __set_errno (EBADF); \
64 #define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
66 #define done_add(val) \
68 unsigned int _val = val; \
69 assert ((unsigned int) done < (unsigned int) INT_MAX); \
70 if (__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 /* For the argument descriptions, which may be allocated on the heap. */
239 void *args_malloced
= NULL
;
241 /* This table maps a character into a number representing a
242 class. In each step there is a destination label for each
244 static const uint8_t jump_table
[] =
246 /* ' ' */ 1, 0, 0, /* '#' */ 4,
247 0, /* '%' */ 14, 0, /* '\''*/ 6,
248 0, 0, /* '*' */ 7, /* '+' */ 2,
249 0, /* '-' */ 3, /* '.' */ 9, 0,
250 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
251 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
252 /* '8' */ 8, /* '9' */ 8, 0, 0,
254 0, /* 'A' */ 26, 0, /* 'C' */ 25,
255 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
256 0, /* 'I' */ 29, 0, 0,
257 /* 'L' */ 12, 0, 0, 0,
258 0, 0, 0, /* 'S' */ 21,
260 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
262 0, /* 'a' */ 26, 0, /* 'c' */ 20,
263 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
264 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
265 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
266 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
267 /* 't' */ 27, /* 'u' */ 16, 0, 0,
268 /* 'x' */ 18, 0, /* 'z' */ 13
271 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
272 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
274 /* 'int' is enough and it saves some space on 64 bit systems. */
275 # define JUMP_TABLE_TYPE const int
276 # define JUMP(ChExpr, table) \
280 void *__unbounded ptr; \
282 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
283 : table[CHAR_CLASS (spec)]; \
284 ptr = &&do_form_unknown + offset; \
289 # define JUMP_TABLE_TYPE const void *const
290 # define JUMP(ChExpr, table) \
293 const void *__unbounded ptr; \
295 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
296 : table[CHAR_CLASS (spec)]; \
302 #define STEP0_3_TABLE \
303 /* Step 0: at the beginning. */ \
304 static JUMP_TABLE_TYPE step0_jumps[30] = \
306 REF (form_unknown), \
307 REF (flag_space), /* for ' ' */ \
308 REF (flag_plus), /* for '+' */ \
309 REF (flag_minus), /* for '-' */ \
310 REF (flag_hash), /* for '<hash>' */ \
311 REF (flag_zero), /* for '0' */ \
312 REF (flag_quote), /* for '\'' */ \
313 REF (width_asterics), /* for '*' */ \
314 REF (width), /* for '1'...'9' */ \
315 REF (precision), /* for '.' */ \
316 REF (mod_half), /* for 'h' */ \
317 REF (mod_long), /* for 'l' */ \
318 REF (mod_longlong), /* for 'L', 'q' */ \
319 REF (mod_size_t), /* for 'z', 'Z' */ \
320 REF (form_percent), /* for '%' */ \
321 REF (form_integer), /* for 'd', 'i' */ \
322 REF (form_unsigned), /* for 'u' */ \
323 REF (form_octal), /* for 'o' */ \
324 REF (form_hexa), /* for 'X', 'x' */ \
325 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
326 REF (form_character), /* for 'c' */ \
327 REF (form_string), /* for 's', 'S' */ \
328 REF (form_pointer), /* for 'p' */ \
329 REF (form_number), /* for 'n' */ \
330 REF (form_strerror), /* for 'm' */ \
331 REF (form_wcharacter), /* for 'C' */ \
332 REF (form_floathex), /* for 'A', 'a' */ \
333 REF (mod_ptrdiff_t), /* for 't' */ \
334 REF (mod_intmax_t), /* for 'j' */ \
335 REF (flag_i18n), /* for 'I' */ \
337 /* Step 1: after processing width. */ \
338 static JUMP_TABLE_TYPE step1_jumps[30] = \
340 REF (form_unknown), \
341 REF (form_unknown), /* for ' ' */ \
342 REF (form_unknown), /* for '+' */ \
343 REF (form_unknown), /* for '-' */ \
344 REF (form_unknown), /* for '<hash>' */ \
345 REF (form_unknown), /* for '0' */ \
346 REF (form_unknown), /* for '\'' */ \
347 REF (form_unknown), /* for '*' */ \
348 REF (form_unknown), /* for '1'...'9' */ \
349 REF (precision), /* for '.' */ \
350 REF (mod_half), /* for 'h' */ \
351 REF (mod_long), /* for 'l' */ \
352 REF (mod_longlong), /* for 'L', 'q' */ \
353 REF (mod_size_t), /* for 'z', 'Z' */ \
354 REF (form_percent), /* for '%' */ \
355 REF (form_integer), /* for 'd', 'i' */ \
356 REF (form_unsigned), /* for 'u' */ \
357 REF (form_octal), /* for 'o' */ \
358 REF (form_hexa), /* for 'X', 'x' */ \
359 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
360 REF (form_character), /* for 'c' */ \
361 REF (form_string), /* for 's', 'S' */ \
362 REF (form_pointer), /* for 'p' */ \
363 REF (form_number), /* for 'n' */ \
364 REF (form_strerror), /* for 'm' */ \
365 REF (form_wcharacter), /* for 'C' */ \
366 REF (form_floathex), /* for 'A', 'a' */ \
367 REF (mod_ptrdiff_t), /* for 't' */ \
368 REF (mod_intmax_t), /* for 'j' */ \
369 REF (form_unknown) /* for 'I' */ \
371 /* Step 2: after processing precision. */ \
372 static JUMP_TABLE_TYPE step2_jumps[30] = \
374 REF (form_unknown), \
375 REF (form_unknown), /* for ' ' */ \
376 REF (form_unknown), /* for '+' */ \
377 REF (form_unknown), /* for '-' */ \
378 REF (form_unknown), /* for '<hash>' */ \
379 REF (form_unknown), /* for '0' */ \
380 REF (form_unknown), /* for '\'' */ \
381 REF (form_unknown), /* for '*' */ \
382 REF (form_unknown), /* for '1'...'9' */ \
383 REF (form_unknown), /* for '.' */ \
384 REF (mod_half), /* for 'h' */ \
385 REF (mod_long), /* for 'l' */ \
386 REF (mod_longlong), /* for 'L', 'q' */ \
387 REF (mod_size_t), /* for 'z', 'Z' */ \
388 REF (form_percent), /* for '%' */ \
389 REF (form_integer), /* for 'd', 'i' */ \
390 REF (form_unsigned), /* for 'u' */ \
391 REF (form_octal), /* for 'o' */ \
392 REF (form_hexa), /* for 'X', 'x' */ \
393 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
394 REF (form_character), /* for 'c' */ \
395 REF (form_string), /* for 's', 'S' */ \
396 REF (form_pointer), /* for 'p' */ \
397 REF (form_number), /* for 'n' */ \
398 REF (form_strerror), /* for 'm' */ \
399 REF (form_wcharacter), /* for 'C' */ \
400 REF (form_floathex), /* for 'A', 'a' */ \
401 REF (mod_ptrdiff_t), /* for 't' */ \
402 REF (mod_intmax_t), /* for 'j' */ \
403 REF (form_unknown) /* for 'I' */ \
405 /* Step 3a: after processing first 'h' modifier. */ \
406 static JUMP_TABLE_TYPE step3a_jumps[30] = \
408 REF (form_unknown), \
409 REF (form_unknown), /* for ' ' */ \
410 REF (form_unknown), /* for '+' */ \
411 REF (form_unknown), /* for '-' */ \
412 REF (form_unknown), /* for '<hash>' */ \
413 REF (form_unknown), /* for '0' */ \
414 REF (form_unknown), /* for '\'' */ \
415 REF (form_unknown), /* for '*' */ \
416 REF (form_unknown), /* for '1'...'9' */ \
417 REF (form_unknown), /* for '.' */ \
418 REF (mod_halfhalf), /* for 'h' */ \
419 REF (form_unknown), /* for 'l' */ \
420 REF (form_unknown), /* for 'L', 'q' */ \
421 REF (form_unknown), /* for 'z', 'Z' */ \
422 REF (form_percent), /* for '%' */ \
423 REF (form_integer), /* for 'd', 'i' */ \
424 REF (form_unsigned), /* for 'u' */ \
425 REF (form_octal), /* for 'o' */ \
426 REF (form_hexa), /* for 'X', 'x' */ \
427 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
428 REF (form_unknown), /* for 'c' */ \
429 REF (form_unknown), /* for 's', 'S' */ \
430 REF (form_unknown), /* for 'p' */ \
431 REF (form_number), /* for 'n' */ \
432 REF (form_unknown), /* for 'm' */ \
433 REF (form_unknown), /* for 'C' */ \
434 REF (form_unknown), /* for 'A', 'a' */ \
435 REF (form_unknown), /* for 't' */ \
436 REF (form_unknown), /* for 'j' */ \
437 REF (form_unknown) /* for 'I' */ \
439 /* Step 3b: after processing first 'l' modifier. */ \
440 static JUMP_TABLE_TYPE step3b_jumps[30] = \
442 REF (form_unknown), \
443 REF (form_unknown), /* for ' ' */ \
444 REF (form_unknown), /* for '+' */ \
445 REF (form_unknown), /* for '-' */ \
446 REF (form_unknown), /* for '<hash>' */ \
447 REF (form_unknown), /* for '0' */ \
448 REF (form_unknown), /* for '\'' */ \
449 REF (form_unknown), /* for '*' */ \
450 REF (form_unknown), /* for '1'...'9' */ \
451 REF (form_unknown), /* for '.' */ \
452 REF (form_unknown), /* for 'h' */ \
453 REF (mod_longlong), /* for 'l' */ \
454 REF (form_unknown), /* for 'L', 'q' */ \
455 REF (form_unknown), /* for 'z', 'Z' */ \
456 REF (form_percent), /* for '%' */ \
457 REF (form_integer), /* for 'd', 'i' */ \
458 REF (form_unsigned), /* for 'u' */ \
459 REF (form_octal), /* for 'o' */ \
460 REF (form_hexa), /* for 'X', 'x' */ \
461 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
462 REF (form_character), /* for 'c' */ \
463 REF (form_string), /* for 's', 'S' */ \
464 REF (form_pointer), /* for 'p' */ \
465 REF (form_number), /* for 'n' */ \
466 REF (form_strerror), /* for 'm' */ \
467 REF (form_wcharacter), /* for 'C' */ \
468 REF (form_floathex), /* for 'A', 'a' */ \
469 REF (form_unknown), /* for 't' */ \
470 REF (form_unknown), /* for 'j' */ \
471 REF (form_unknown) /* for 'I' */ \
474 #define STEP4_TABLE \
475 /* Step 4: processing format specifier. */ \
476 static JUMP_TABLE_TYPE step4_jumps[30] = \
478 REF (form_unknown), \
479 REF (form_unknown), /* for ' ' */ \
480 REF (form_unknown), /* for '+' */ \
481 REF (form_unknown), /* for '-' */ \
482 REF (form_unknown), /* for '<hash>' */ \
483 REF (form_unknown), /* for '0' */ \
484 REF (form_unknown), /* for '\'' */ \
485 REF (form_unknown), /* for '*' */ \
486 REF (form_unknown), /* for '1'...'9' */ \
487 REF (form_unknown), /* for '.' */ \
488 REF (form_unknown), /* for 'h' */ \
489 REF (form_unknown), /* for 'l' */ \
490 REF (form_unknown), /* for 'L', 'q' */ \
491 REF (form_unknown), /* for 'z', 'Z' */ \
492 REF (form_percent), /* for '%' */ \
493 REF (form_integer), /* for 'd', 'i' */ \
494 REF (form_unsigned), /* for 'u' */ \
495 REF (form_octal), /* for 'o' */ \
496 REF (form_hexa), /* for 'X', 'x' */ \
497 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
498 REF (form_character), /* for 'c' */ \
499 REF (form_string), /* for 's', 'S' */ \
500 REF (form_pointer), /* for 'p' */ \
501 REF (form_number), /* for 'n' */ \
502 REF (form_strerror), /* for 'm' */ \
503 REF (form_wcharacter), /* for 'C' */ \
504 REF (form_floathex), /* for 'A', 'a' */ \
505 REF (form_unknown), /* for 't' */ \
506 REF (form_unknown), /* for 'j' */ \
507 REF (form_unknown) /* for 'I' */ \
511 #define process_arg(fspec) \
512 /* Start real work. We know about all flags and modifiers and \
513 now process the wanted format specifier. */ \
514 LABEL (form_percent): \
515 /* Write a literal "%". */ \
519 LABEL (form_integer): \
520 /* Signed decimal integer. */ \
525 long long int signed_number; \
528 signed_number = va_arg (ap, long long int); \
530 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
532 is_negative = signed_number < 0; \
533 number.longlong = is_negative ? (- signed_number) : signed_number; \
535 goto LABEL (longlong_number); \
539 long int signed_number; \
544 signed_number = va_arg (ap, long int); \
546 signed_number = (signed char) va_arg (ap, unsigned int); \
547 else if (!is_short) \
548 signed_number = va_arg (ap, int); \
550 signed_number = (short int) va_arg (ap, unsigned int); \
554 signed_number = args_value[fspec->data_arg].pa_long_int; \
556 signed_number = (signed char) \
557 args_value[fspec->data_arg].pa_u_int; \
558 else if (!is_short) \
559 signed_number = args_value[fspec->data_arg].pa_int; \
561 signed_number = (short int) \
562 args_value[fspec->data_arg].pa_u_int; \
564 is_negative = signed_number < 0; \
565 number.word = is_negative ? (- signed_number) : signed_number; \
567 goto LABEL (number); \
571 LABEL (form_unsigned): \
572 /* Unsigned decimal integer. */ \
574 goto LABEL (unsigned_number); \
577 LABEL (form_octal): \
578 /* Unsigned octal integer. */ \
580 goto LABEL (unsigned_number); \
584 /* Unsigned hexadecimal integer. */ \
587 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
589 /* ISO specifies the `+' and ` ' flags only for signed \
598 number.longlong = va_arg (ap, unsigned long long int); \
600 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
602 LABEL (longlong_number): \
604 /* Supply a default precision if none was given. */ \
607 /* We have to take care for the '0' flag. If a precision \
608 is given it must be ignored. */ \
611 /* If the precision is 0 and the number is 0 nothing has to \
612 be written for the number, except for the 'o' format in \
614 if (prec == 0 && number.longlong == 0) \
617 if (base == 8 && alt) \
618 *--string = L_('0'); \
622 /* Put the number in WORK. */ \
623 string = _itoa (number.longlong, workend, base, \
625 if (group && grouping) \
626 string = group_number (string, workend, grouping, \
629 if (use_outdigits && base == 10) \
630 string = _i18n_number_rewrite (string, workend, workend); \
632 /* Simplify further test for num != 0. */ \
633 number.word = number.longlong != 0; \
640 number.word = va_arg (ap, unsigned long int); \
642 number.word = (unsigned char) va_arg (ap, unsigned int); \
643 else if (!is_short) \
644 number.word = va_arg (ap, unsigned int); \
646 number.word = (unsigned short int) va_arg (ap, unsigned int); \
650 number.word = args_value[fspec->data_arg].pa_u_long_int; \
652 number.word = (unsigned char) \
653 args_value[fspec->data_arg].pa_u_int; \
654 else if (!is_short) \
655 number.word = args_value[fspec->data_arg].pa_u_int; \
657 number.word = (unsigned short int) \
658 args_value[fspec->data_arg].pa_u_int; \
662 /* Supply a default precision if none was given. */ \
665 /* We have to take care for the '0' flag. If a precision \
666 is given it must be ignored. */ \
669 /* If the precision is 0 and the number is 0 nothing has to \
670 be written for the number, except for the 'o' format in \
672 if (prec == 0 && number.word == 0) \
675 if (base == 8 && alt) \
676 *--string = L_('0'); \
680 /* Put the number in WORK. */ \
681 string = _itoa_word (number.word, workend, base, \
683 if (group && grouping) \
684 string = group_number (string, workend, grouping, \
687 if (use_outdigits && base == 10) \
688 string = _i18n_number_rewrite (string, workend, workend); \
692 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
693 /* Add octal marker. */ \
694 *--string = L_('0'); \
696 prec = MAX (0, prec - (workend - string)); \
700 width -= workend - string + prec; \
702 if (number.word != 0 && alt && base == 16) \
703 /* Account for 0X hex marker. */ \
706 if (is_negative || showsign || space) \
709 if (pad == L_(' ')) \
722 if (number.word != 0 && alt && base == 16) \
731 outstring (string, workend - string); \
753 if (number.word != 0 && alt && base == 16) \
760 width -= workend - string + prec; \
770 outstring (string, workend - string); \
776 LABEL (form_float): \
778 /* Floating-point number. This is handled by printf_fp.c. */ \
785 is_long_double = 0; \
787 struct printf_info info = { .prec = prec, \
790 .is_long_double = is_long_double, \
791 .is_short = is_short, \
792 .is_long = is_long, \
796 .showsign = showsign, \
800 .i18n = use_outdigits, \
801 .wide = sizeof (CHAR_T) != 1 }; \
803 if (is_long_double) \
804 the_arg.pa_long_double = va_arg (ap, long double); \
806 the_arg.pa_double = va_arg (ap, double); \
807 ptr = (const void *) &the_arg; \
809 function_done = __printf_fp (s, &info, &ptr); \
813 ptr = (const void *) &args_value[fspec->data_arg]; \
816 fspec->data_arg_type = PA_DOUBLE; \
817 fspec->info.is_long_double = 0; \
820 function_done = __printf_fp (s, &fspec->info, &ptr); \
823 if (function_done < 0) \
825 /* Error in print handler; up to handler to set errno. */ \
830 done_add (function_done); \
834 LABEL (form_floathex): \
836 /* Floating point number printed as hexadecimal number. */ \
843 is_long_double = 0; \
845 struct printf_info info = { .prec = prec, \
848 .is_long_double = is_long_double, \
849 .is_short = is_short, \
850 .is_long = is_long, \
854 .showsign = showsign, \
858 .wide = sizeof (CHAR_T) != 1 }; \
860 if (is_long_double) \
861 the_arg.pa_long_double = va_arg (ap, long double); \
863 the_arg.pa_double = va_arg (ap, double); \
864 ptr = (const void *) &the_arg; \
866 function_done = __printf_fphex (s, &info, &ptr); \
870 ptr = (const void *) &args_value[fspec->data_arg]; \
872 fspec->info.is_long_double = 0; \
874 function_done = __printf_fphex (s, &fspec->info, &ptr); \
877 if (function_done < 0) \
879 /* Error in print handler; up to handler to set errno. */ \
884 done_add (function_done); \
888 LABEL (form_pointer): \
889 /* Generic pointer. */ \
893 ptr = va_arg (ap, void *); \
895 ptr = args_value[fspec->data_arg].pa_pointer; \
898 /* If the pointer is not NULL, write it as a %#x spec. */ \
900 number.word = (unsigned long int) ptr; \
905 goto LABEL (number); \
909 /* Write "(nil)" for a nil pointer. */ \
910 string = (CHAR_T *) L_("(nil)"); \
911 /* Make sure the full string "(nil)" is printed. */ \
914 is_long = 0; /* This is no wide-char string. */ \
915 goto LABEL (print_string); \
920 LABEL (form_number): \
921 if (s->_flags2 & _IO_FLAGS2_FORTIFY) \
923 if (! readonly_format) \
925 extern int __readonly_area (const void *, size_t) \
928 = __readonly_area (format, ((STR_LEN (format) + 1) \
929 * sizeof (CHAR_T))); \
931 if (readonly_format < 0) \
932 __libc_fatal ("*** %n in writable segment detected ***\n"); \
934 /* Answer the count of characters written. */ \
938 *(long long int *) va_arg (ap, void *) = done; \
939 else if (is_long_num) \
940 *(long int *) va_arg (ap, void *) = done; \
942 *(char *) va_arg (ap, void *) = done; \
943 else if (!is_short) \
944 *(int *) va_arg (ap, void *) = done; \
946 *(short int *) va_arg (ap, void *) = done; \
950 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
951 else if (is_long_num) \
952 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
954 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
955 else if (!is_short) \
956 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
958 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
961 LABEL (form_strerror): \
962 /* Print description of error ERRNO. */ \
964 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
965 sizeof work_buffer); \
966 is_long = 0; /* This is no wide-char string. */ \
967 goto LABEL (print_string)
969 #ifdef COMPILE_WPRINTF
970 # define process_string_arg(fspec) \
971 LABEL (form_character): \
974 goto LABEL (form_wcharacter); \
975 --width; /* Account for the character itself. */ \
979 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
981 outchar (__btowc ((unsigned char) \
982 args_value[fspec->data_arg].pa_int)); \
987 LABEL (form_wcharacter): \
989 /* Wide character. */ \
994 outchar (va_arg (ap, wchar_t)); \
996 outchar (args_value[fspec->data_arg].pa_wchar); \
1002 LABEL (form_string): \
1005 int string_malloced; \
1007 /* The string argument could in fact be `char *' or `wchar_t *'. \
1008 But this should not make a difference here. */ \
1009 if (fspec == NULL) \
1010 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
1012 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
1014 /* Entry point for printing other strings. */ \
1015 LABEL (print_string): \
1017 string_malloced = 0; \
1018 if (string == NULL) \
1020 /* Write "(null)" if there's space. */ \
1022 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
1024 string = (CHAR_T *) null; \
1025 len = (sizeof (null) / sizeof (null[0])) - 1; \
1029 string = (CHAR_T *) L""; \
1033 else if (!is_long && spec != L_('S')) \
1035 /* This is complicated. We have to transform the multibyte \
1036 string into a wide character string. */ \
1037 const char *mbs = (const char *) string; \
1038 mbstate_t mbstate; \
1040 len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
1042 /* Allocate dynamically an array which definitely is long \
1043 enough for the wide character version. Each byte in the \
1044 multi-byte string can produce at most one wide character. */ \
1045 if (__libc_use_alloca (len * sizeof (wchar_t))) \
1046 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1047 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1054 string_malloced = 1; \
1056 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1057 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1058 if (len == (size_t) -1) \
1060 /* Illegal multibyte character. */ \
1068 /* Search for the end of the string, but don't search past \
1069 the length specified by the precision. */ \
1070 len = __wcsnlen (string, prec); \
1072 len = __wcslen (string); \
1075 if ((width -= len) < 0) \
1077 outstring (string, len); \
1083 outstring (string, len); \
1086 if (__builtin_expect (string_malloced, 0)) \
1091 # define process_string_arg(fspec) \
1092 LABEL (form_character): \
1095 goto LABEL (form_wcharacter); \
1096 --width; /* Account for the character itself. */ \
1099 if (fspec == NULL) \
1100 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1102 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1107 LABEL (form_wcharacter): \
1109 /* Wide character. */ \
1110 char buf[MB_CUR_MAX]; \
1111 mbstate_t mbstate; \
1114 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1115 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1116 : args_value[fspec->data_arg].pa_wchar), \
1118 if (len == (size_t) -1) \
1120 /* Something went wrong during the conversion. Bail out. */ \
1127 outstring (buf, len); \
1133 LABEL (form_string): \
1136 int string_malloced; \
1138 /* The string argument could in fact be `char *' or `wchar_t *'. \
1139 But this should not make a difference here. */ \
1140 if (fspec == NULL) \
1141 string = (char *) va_arg (ap, const char *); \
1143 string = (char *) args_value[fspec->data_arg].pa_string; \
1145 /* Entry point for printing other strings. */ \
1146 LABEL (print_string): \
1148 string_malloced = 0; \
1149 if (string == NULL) \
1151 /* Write "(null)" if there's space. */ \
1152 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1154 string = (char *) null; \
1155 len = sizeof (null) - 1; \
1159 string = (char *) ""; \
1163 else if (!is_long && spec != L_('S')) \
1167 /* Search for the end of the string, but don't search past \
1168 the length (in bytes) specified by the precision. Also \
1169 don't use incomplete characters. */ \
1170 if (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MB_CUR_MAX) == 1) \
1171 len = __strnlen (string, prec); \
1174 /* In case we have a multibyte character set the \
1175 situation is more complicated. We must not copy \
1176 bytes at the end which form an incomplete character. */\
1177 size_t ignore_size = (unsigned) prec > 1024 ? 1024 : prec;\
1178 wchar_t ignore[ignore_size]; \
1179 const char *str2 = string; \
1180 const char *strend = string + prec; \
1181 if (strend < string) \
1182 strend = (const char *) UINTPTR_MAX; \
1185 memset (&ps, '\0', sizeof (ps)); \
1187 while (str2 != NULL && str2 < strend) \
1188 if (__mbsnrtowcs (ignore, &str2, strend - str2, \
1189 ignore_size, &ps) == (size_t) -1) \
1191 /* Conversion function has set errno. */ \
1197 len = strlen (string); \
1199 len = str2 - string - (ps.__count & 7); \
1203 len = strlen (string); \
1207 const wchar_t *s2 = (const wchar_t *) string; \
1208 mbstate_t mbstate; \
1210 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1214 /* The string `s2' might not be NUL terminated. */ \
1215 if (__libc_use_alloca (prec)) \
1216 string = (char *) alloca (prec); \
1217 else if ((string = (char *) malloc (prec)) == NULL) \
1223 string_malloced = 1; \
1224 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1228 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1229 if (len != (size_t) -1) \
1231 assert (__mbsinit (&mbstate)); \
1232 s2 = (const wchar_t *) string; \
1233 if (__libc_use_alloca (len + 1)) \
1234 string = (char *) alloca (len + 1); \
1235 else if ((string = (char *) malloc (len + 1)) == NULL) \
1241 string_malloced = 1; \
1242 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1246 if (len == (size_t) -1) \
1248 /* Illegal wide-character string. */ \
1254 if ((width -= len) < 0) \
1256 outstring (string, len); \
1262 outstring (string, len); \
1265 if (__builtin_expect (string_malloced, 0)) \
1271 /* Orient the stream. */
1276 /* Sanity check of arguments. */
1277 ARGCHECK (s
, format
);
1280 /* Check for correct orientation. */
1281 if (_IO_vtable_offset (s
) == 0 &&
1282 _IO_fwide (s
, sizeof (CHAR_T
) == 1 ? -1 : 1)
1283 != (sizeof (CHAR_T
) == 1 ? -1 : 1))
1284 /* The stream is already oriented otherwise. */
1288 if (UNBUFFERED_P (s
))
1289 /* Use a helper function which will allocate a local temporary buffer
1290 for the stream and then call us again. */
1291 return buffered_vfprintf (s
, format
, ap
);
1293 /* Initialize local variables. */
1295 grouping
= (const char *) -1;
1297 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1298 since on some systems `va_list' is not an integral type. */
1299 __va_copy (ap_save
, ap
);
1305 #ifdef COMPILE_WPRINTF
1306 /* Find the first format specifier. */
1307 f
= lead_str_end
= __find_specwc ((const UCHAR_T
*) format
);
1309 /* Find the first format specifier. */
1310 f
= lead_str_end
= __find_specmb ((const UCHAR_T
*) format
);
1314 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile
, s
);
1317 /* Write the literal text before the first format. */
1318 outstring ((const UCHAR_T
*) format
,
1319 lead_str_end
- (const UCHAR_T
*) format
);
1321 /* If we only have to print a simple string, return now. */
1325 /* Use the slow path in case any printf handler is registered. */
1326 if (__builtin_expect (__printf_function_table
!= NULL
1327 || __printf_modifier_table
!= NULL
1328 || __printf_va_arg_table
!= NULL
, 0))
1331 /* Process whole format string. */
1335 # define REF(Name) &&do_##Name - &&do_form_unknown
1337 # define REF(Name) &&do_##Name
1339 #define LABEL(Name) do_##Name
1343 union printf_arg
*args_value
; /* This is not used here but ... */
1344 int is_negative
; /* Flag for negative number. */
1347 unsigned long long int longlong
;
1348 unsigned long int word
;
1351 union printf_arg the_arg
;
1352 CHAR_T
*string
; /* Pointer to argument string. */
1353 int alt
= 0; /* Alternate format. */
1354 int space
= 0; /* Use space prefix if no sign is needed. */
1355 int left
= 0; /* Left-justify output. */
1356 int showsign
= 0; /* Always begin with plus or minus sign. */
1357 int group
= 0; /* Print numbers according grouping rules. */
1358 int is_long_double
= 0; /* Argument is long double/ long long int. */
1359 int is_short
= 0; /* Argument is short int. */
1360 int is_long
= 0; /* Argument is long int. */
1361 int is_char
= 0; /* Argument is promoted (unsigned) char. */
1362 int width
= 0; /* Width of output; 0 means none specified. */
1363 int prec
= -1; /* Precision of output; -1 means none specified. */
1364 /* This flag is set by the 'I' modifier and selects the use of the
1365 `outdigits' as determined by the current locale. */
1366 int use_outdigits
= 0;
1367 UCHAR_T pad
= L_(' ');/* Padding character. */
1371 workend
= &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
1373 /* Get current character in format string. */
1374 JUMP (*++f
, step0_jumps
);
1379 JUMP (*++f
, step0_jumps
);
1384 JUMP (*++f
, step0_jumps
);
1390 JUMP (*++f
, step0_jumps
);
1395 JUMP (*++f
, step0_jumps
);
1401 JUMP (*++f
, step0_jumps
);
1403 /* The '\'' flag. */
1407 if (grouping
== (const char *) -1)
1409 #ifdef COMPILE_WPRINTF
1410 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1411 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1413 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1416 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1417 if (*grouping
== '\0' || *grouping
== CHAR_MAX
1418 #ifdef COMPILE_WPRINTF
1419 || thousands_sep
== L
'\0'
1421 || *thousands_sep
== '\0'
1426 JUMP (*++f
, step0_jumps
);
1430 JUMP (*++f
, step0_jumps
);
1432 /* Get width from argument. */
1433 LABEL (width_asterics
):
1435 const UCHAR_T
*tmp
; /* Temporary value. */
1438 if (ISDIGIT (*tmp
) && read_int (&tmp
) && *tmp
== L_('$'))
1439 /* The width comes from a positional parameter. */
1442 width
= va_arg (ap
, int);
1444 /* Negative width means left justified. */
1452 if (__builtin_expect (width
>= (size_t) -1 / sizeof (CHAR_T
) - 32, 0))
1454 __set_errno (ERANGE
);
1459 if (width
>= sizeof (work_buffer
) / sizeof (work_buffer
[0]) - 32)
1461 /* We have to use a special buffer. The "32" is just a safe
1462 bet for all the output which is not counted in the width. */
1463 size_t needed
= ((size_t) width
+ 32) * sizeof (CHAR_T
);
1464 if (__libc_use_alloca (needed
))
1465 workend
= (CHAR_T
*) alloca (needed
) + width
+ 32;
1468 workstart
= (CHAR_T
*) malloc (needed
);
1469 if (workstart
== NULL
)
1474 workend
= workstart
+ width
+ 32;
1478 JUMP (*f
, step1_jumps
);
1480 /* Given width in format string. */
1482 width
= read_int (&f
);
1484 if (__builtin_expect (width
>= (size_t) -1 / sizeof (CHAR_T
) - 32, 0))
1486 __set_errno (ERANGE
);
1491 if (width
>= sizeof (work_buffer
) / sizeof (work_buffer
[0]) - 32)
1493 /* We have to use a special buffer. The "32" is just a safe
1494 bet for all the output which is not counted in the width. */
1495 size_t needed
= ((size_t) width
+ 32) * sizeof (CHAR_T
);
1496 if (__libc_use_alloca (needed
))
1497 workend
= (CHAR_T
*) alloca (needed
) + width
+ 32;
1500 workstart
= (CHAR_T
*) malloc (needed
);
1501 if (workstart
== NULL
)
1506 workend
= workstart
+ width
+ 32;
1510 /* Oh, oh. The argument comes from a positional parameter. */
1512 JUMP (*f
, step1_jumps
);
1518 const UCHAR_T
*tmp
; /* Temporary value. */
1521 if (ISDIGIT (*tmp
) && read_int (&tmp
) > 0 && *tmp
== L_('$'))
1522 /* The precision comes from a positional parameter. */
1525 prec
= va_arg (ap
, int);
1527 /* If the precision is negative the precision is omitted. */
1531 else if (ISDIGIT (*f
))
1532 prec
= read_int (&f
);
1536 && prec
> sizeof (work_buffer
) / sizeof (work_buffer
[0]) - 32)
1538 if (__builtin_expect (prec
>= (size_t) -1 / sizeof (CHAR_T
) - 32, 0))
1540 __set_errno (ERANGE
);
1544 size_t needed
= ((size_t) prec
+ 32) * sizeof (CHAR_T
);
1546 if (__libc_use_alloca (needed
))
1547 workend
= (CHAR_T
*) alloca (needed
) + prec
+ 32;
1550 workstart
= (CHAR_T
*) malloc (needed
);
1551 if (workstart
== NULL
)
1556 workend
= workstart
+ prec
+ 32;
1559 JUMP (*f
, step2_jumps
);
1561 /* Process 'h' modifier. There might another 'h' following. */
1564 JUMP (*++f
, step3a_jumps
);
1566 /* Process 'hh' modifier. */
1567 LABEL (mod_halfhalf
):
1570 JUMP (*++f
, step4_jumps
);
1572 /* Process 'l' modifier. There might another 'l' following. */
1575 JUMP (*++f
, step3b_jumps
);
1577 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1578 allowed to follow. */
1579 LABEL (mod_longlong
):
1582 JUMP (*++f
, step4_jumps
);
1585 is_long_double
= sizeof (size_t) > sizeof (unsigned long int);
1586 is_long
= sizeof (size_t) > sizeof (unsigned int);
1587 JUMP (*++f
, step4_jumps
);
1589 LABEL (mod_ptrdiff_t
):
1590 is_long_double
= sizeof (ptrdiff_t) > sizeof (unsigned long int);
1591 is_long
= sizeof (ptrdiff_t) > sizeof (unsigned int);
1592 JUMP (*++f
, step4_jumps
);
1594 LABEL (mod_intmax_t
):
1595 is_long_double
= sizeof (intmax_t) > sizeof (unsigned long int);
1596 is_long
= sizeof (intmax_t) > sizeof (unsigned int);
1597 JUMP (*++f
, step4_jumps
);
1599 /* Process current format. */
1602 process_arg (((struct printf_spec
*) NULL
));
1603 process_string_arg (((struct printf_spec
*) NULL
));
1605 LABEL (form_unknown
):
1606 if (spec
== L_('\0'))
1608 /* The format string ended before the specifier is complete. */
1609 __set_errno (EINVAL
);
1614 /* If we are in the fast loop force entering the complicated
1619 /* The format is correctly handled. */
1622 if (__builtin_expect (workstart
!= NULL
, 0))
1626 /* Look for next format specifier. */
1627 #ifdef COMPILE_WPRINTF
1628 f
= __find_specwc ((end_of_spec
= ++f
));
1630 f
= __find_specmb ((end_of_spec
= ++f
));
1633 /* Write the following constant string. */
1634 outstring (end_of_spec
, f
- end_of_spec
);
1636 while (*f
!= L_('\0'));
1638 /* Unlock stream and return. */
1641 /* Here starts the more complex loop to handle positional parameters. */
1644 /* Array with information about the needed arguments. This has to
1645 be dynamically extensible. */
1647 /* A more or less arbitrary start value. */
1648 size_t nspecs_size
= 32 * sizeof (struct printf_spec
);
1649 struct printf_spec
*specs
= alloca (nspecs_size
);
1651 /* The number of arguments the format string requests. This will
1652 determine the size of the array needed to store the argument
1655 size_t bytes_per_arg
;
1656 union printf_arg
*args_value
;
1660 /* Positional parameters refer to arguments directly. This could
1661 also determine the maximum number of arguments. Track the
1663 size_t max_ref_arg
= 0;
1665 /* Just a counter. */
1671 if (grouping
== (const char *) -1)
1673 #ifdef COMPILE_WPRINTF
1674 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1675 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1677 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1680 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1681 if (*grouping
== '\0' || *grouping
== CHAR_MAX
)
1685 for (f
= lead_str_end
; *f
!= L_('\0'); f
= specs
[nspecs
++].next_fmt
)
1687 if (nspecs
* sizeof (*specs
) >= nspecs_size
)
1689 /* Extend the array of format specifiers. */
1690 struct printf_spec
*old
= specs
;
1691 specs
= extend_alloca (specs
, nspecs_size
, 2 * nspecs_size
);
1693 /* Copy the old array's elements to the new space. */
1694 memmove (specs
, old
, nspecs
* sizeof (*specs
));
1697 /* Parse the format specifier. */
1698 #ifdef COMPILE_WPRINTF
1699 nargs
+= __parse_one_specwc (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1701 nargs
+= __parse_one_specmb (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1705 /* Determine the number of arguments the format string consumes. */
1706 nargs
= MAX (nargs
, max_ref_arg
);
1707 /* Calculate total size needed to represent a single argument across
1708 all three argument-related arrays. */
1709 bytes_per_arg
= sizeof (*args_value
) + sizeof (*args_size
)
1710 + sizeof (*args_type
);
1712 /* Check for potential integer overflow. */
1713 if (__builtin_expect (nargs
> SIZE_MAX
/ bytes_per_arg
, 0))
1715 __set_errno (ERANGE
);
1720 /* Allocate memory for all three argument arrays. */
1721 if (__libc_use_alloca (nargs
* bytes_per_arg
))
1722 args_value
= alloca (nargs
* bytes_per_arg
);
1725 args_value
= args_malloced
= malloc (nargs
* bytes_per_arg
);
1726 if (args_value
== NULL
)
1733 /* Set up the remaining two arrays to each point past the end of the
1734 prior array, since space for all three has been allocated now. */
1735 args_size
= &args_value
[nargs
].pa_int
;
1736 args_type
= &args_size
[nargs
];
1737 memset (args_type
, s
->_flags2
& _IO_FLAGS2_FORTIFY
? '\xff' : '\0',
1738 nargs
* sizeof (*args_type
));
1740 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1741 still zero after this loop, format is invalid. For now we
1742 simply use 0 as the value. */
1744 /* Fill in the types of all the arguments. */
1745 for (cnt
= 0; cnt
< nspecs
; ++cnt
)
1747 /* If the width is determined by an argument this is an int. */
1748 if (specs
[cnt
].width_arg
!= -1)
1749 args_type
[specs
[cnt
].width_arg
] = PA_INT
;
1751 /* If the precision is determined by an argument this is an int. */
1752 if (specs
[cnt
].prec_arg
!= -1)
1753 args_type
[specs
[cnt
].prec_arg
] = PA_INT
;
1755 switch (specs
[cnt
].ndata_args
)
1757 case 0: /* No arguments. */
1759 case 1: /* One argument; we already have the
1761 args_type
[specs
[cnt
].data_arg
] = specs
[cnt
].data_arg_type
;
1762 args_size
[specs
[cnt
].data_arg
] = specs
[cnt
].size
;
1765 /* We have more than one argument for this format spec.
1766 We must call the arginfo function again to determine
1768 (void) (*__printf_arginfo_table
[specs
[cnt
].info
.spec
])
1770 specs
[cnt
].ndata_args
, &args_type
[specs
[cnt
].data_arg
],
1771 &args_size
[specs
[cnt
].data_arg
]);
1776 /* Now we know all the types and the order. Fill in the argument
1778 for (cnt
= 0; cnt
< nargs
; ++cnt
)
1779 switch (args_type
[cnt
])
1781 #define T(tag, mem, type) \
1783 args_value[cnt].mem = va_arg (ap_save, type); \
1786 T (PA_WCHAR
, pa_wchar
, wint_t);
1787 case PA_CHAR
: /* Promoted. */
1788 case PA_INT
|PA_FLAG_SHORT
: /* Promoted. */
1789 #if LONG_MAX == INT_MAX
1790 case PA_INT
|PA_FLAG_LONG
:
1792 T (PA_INT
, pa_int
, int);
1793 #if LONG_MAX == LONG_LONG_MAX
1794 case PA_INT
|PA_FLAG_LONG
:
1796 T (PA_INT
|PA_FLAG_LONG_LONG
, pa_long_long_int
, long long int);
1797 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1800 case PA_FLOAT
: /* Promoted. */
1801 T (PA_DOUBLE
, pa_double
, double);
1802 case PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
:
1805 args_value
[cnt
].pa_double
= va_arg (ap_save
, double);
1806 args_type
[cnt
] &= ~PA_FLAG_LONG_DOUBLE
;
1809 args_value
[cnt
].pa_long_double
= va_arg (ap_save
, long double);
1811 case PA_STRING
: /* All pointers are the same */
1812 case PA_WSTRING
: /* All pointers are the same */
1813 T (PA_POINTER
, pa_pointer
, void *);
1816 if ((args_type
[cnt
] & PA_FLAG_PTR
) != 0)
1817 args_value
[cnt
].pa_pointer
= va_arg (ap_save
, void *);
1818 else if (__builtin_expect (__printf_va_arg_table
!= NULL
, 0)
1819 && __printf_va_arg_table
[args_type
[cnt
] - PA_LAST
] != NULL
)
1821 args_value
[cnt
].pa_user
= alloca (args_size
[cnt
]);
1822 (*__printf_va_arg_table
[args_type
[cnt
] - PA_LAST
])
1823 (args_value
[cnt
].pa_user
, &ap_save
);
1826 args_value
[cnt
].pa_long_double
= 0.0;
1829 /* Error case. Not all parameters appear in N$ format
1830 strings. We have no way to determine their type. */
1831 assert (s
->_flags2
& _IO_FLAGS2_FORTIFY
);
1832 __libc_fatal ("*** invalid %N$ use detected ***\n");
1835 /* Now walk through all format specifiers and process them. */
1836 for (; (size_t) nspecs_done
< nspecs
; ++nspecs_done
)
1840 # define REF(Name) &&do2_##Name - &&do_form_unknown
1842 # define REF(Name) &&do2_##Name
1845 #define LABEL(Name) do2_##Name
1851 unsigned long long int longlong
;
1852 unsigned long int word
;
1855 union printf_arg the_arg
;
1856 CHAR_T
*string
; /* Pointer to argument string. */
1858 /* Fill variables from values in struct. */
1859 int alt
= specs
[nspecs_done
].info
.alt
;
1860 int space
= specs
[nspecs_done
].info
.space
;
1861 int left
= specs
[nspecs_done
].info
.left
;
1862 int showsign
= specs
[nspecs_done
].info
.showsign
;
1863 int group
= specs
[nspecs_done
].info
.group
;
1864 int is_long_double
= specs
[nspecs_done
].info
.is_long_double
;
1865 int is_short
= specs
[nspecs_done
].info
.is_short
;
1866 int is_char
= specs
[nspecs_done
].info
.is_char
;
1867 int is_long
= specs
[nspecs_done
].info
.is_long
;
1868 int width
= specs
[nspecs_done
].info
.width
;
1869 int prec
= specs
[nspecs_done
].info
.prec
;
1870 int use_outdigits
= specs
[nspecs_done
].info
.i18n
;
1871 char pad
= specs
[nspecs_done
].info
.pad
;
1872 CHAR_T spec
= specs
[nspecs_done
].info
.spec
;
1875 workend
= &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
1877 /* Fill in last information. */
1878 if (specs
[nspecs_done
].width_arg
!= -1)
1880 /* Extract the field width from an argument. */
1881 specs
[nspecs_done
].info
.width
=
1882 args_value
[specs
[nspecs_done
].width_arg
].pa_int
;
1884 if (specs
[nspecs_done
].info
.width
< 0)
1885 /* If the width value is negative left justification is
1886 selected and the value is taken as being positive. */
1888 specs
[nspecs_done
].info
.width
*= -1;
1889 left
= specs
[nspecs_done
].info
.left
= 1;
1891 width
= specs
[nspecs_done
].info
.width
;
1894 if (specs
[nspecs_done
].prec_arg
!= -1)
1896 /* Extract the precision from an argument. */
1897 specs
[nspecs_done
].info
.prec
=
1898 args_value
[specs
[nspecs_done
].prec_arg
].pa_int
;
1900 if (specs
[nspecs_done
].info
.prec
< 0)
1901 /* If the precision is negative the precision is
1903 specs
[nspecs_done
].info
.prec
= -1;
1905 prec
= specs
[nspecs_done
].info
.prec
;
1908 /* Maybe the buffer is too small. */
1909 if (MAX (prec
, width
) + 32 > (int) (sizeof (work_buffer
)
1912 if (__libc_use_alloca ((MAX (prec
, width
) + 32)
1914 workend
= ((CHAR_T
*) alloca ((MAX (prec
, width
) + 32)
1916 + (MAX (prec
, width
) + 32));
1919 workstart
= (CHAR_T
*) malloc ((MAX (prec
, width
) + 32)
1921 workend
= workstart
+ (MAX (prec
, width
) + 32);
1925 /* Process format specifiers. */
1928 extern printf_function
**__printf_function_table
;
1931 if (spec
<= UCHAR_MAX
1932 && __printf_function_table
!= NULL
1933 && __printf_function_table
[(size_t) spec
] != NULL
)
1935 const void **ptr
= alloca (specs
[nspecs_done
].ndata_args
1936 * sizeof (const void *));
1938 /* Fill in an array of pointers to the argument values. */
1939 for (unsigned int i
= 0; i
< specs
[nspecs_done
].ndata_args
;
1941 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
1943 /* Call the function. */
1944 function_done
= __printf_function_table
[(size_t) spec
]
1945 (s
, &specs
[nspecs_done
].info
, ptr
);
1947 if (function_done
!= -2)
1949 /* If an error occurred we don't have information
1950 about # of chars. */
1951 if (function_done
< 0)
1953 /* Function has set errno. */
1958 done_add (function_done
);
1963 JUMP (spec
, step4_jumps
);
1965 process_arg ((&specs
[nspecs_done
]));
1966 process_string_arg ((&specs
[nspecs_done
]));
1968 LABEL (form_unknown
):
1973 ptr
= alloca (specs
[nspecs_done
].ndata_args
1974 * sizeof (const void *));
1976 /* Fill in an array of pointers to the argument values. */
1977 for (i
= 0; i
< specs
[nspecs_done
].ndata_args
; ++i
)
1978 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
1980 /* Call the function. */
1981 function_done
= printf_unknown (s
, &specs
[nspecs_done
].info
,
1984 /* If an error occurred we don't have information about #
1986 if (function_done
< 0)
1988 /* Function has set errno. */
1993 done_add (function_done
);
2001 /* Write the following constant string. */
2002 outstring (specs
[nspecs_done
].end_of_fmt
,
2003 specs
[nspecs_done
].next_fmt
2004 - specs
[nspecs_done
].end_of_fmt
);
2009 free (args_malloced
);
2011 /* Unlock the stream. */
2012 _IO_funlockfile (s
);
2013 _IO_cleanup_region_end (0);
2018 /* Handle an unknown format specifier. This prints out a canonicalized
2019 representation of the format spec itself. */
2021 printf_unknown (FILE *s
, const struct printf_info
*info
,
2022 const void *const *args
)
2026 CHAR_T work_buffer
[MAX (sizeof (info
->width
), sizeof (info
->prec
)) * 3];
2027 CHAR_T
*const workend
2028 = &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
2039 else if (info
->space
)
2043 if (info
->pad
== L_('0'))
2048 if (info
->width
!= 0)
2050 w
= _itoa_word (info
->width
, workend
, 10, 0);
2055 if (info
->prec
!= -1)
2058 w
= _itoa_word (info
->prec
, workend
, 10, 0);
2063 if (info
->spec
!= L_('\0'))
2064 outchar (info
->spec
);
2070 /* Group the digits according to the grouping rules of the current locale.
2071 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
2074 group_number (CHAR_T
*w
, CHAR_T
*rear_ptr
, const char *grouping
,
2075 #ifdef COMPILE_WPRINTF
2076 wchar_t thousands_sep
2078 const char *thousands_sep
2084 #ifndef COMPILE_WPRINTF
2085 int tlen
= strlen (thousands_sep
);
2088 /* We treat all negative values like CHAR_MAX. */
2090 if (*grouping
== CHAR_MAX
|| *grouping
<= 0)
2091 /* No grouping should be done. */
2096 /* Copy existing string so that nothing gets overwritten. */
2097 src
= (CHAR_T
*) alloca ((rear_ptr
- w
) * sizeof (CHAR_T
));
2098 s
= (CHAR_T
*) __mempcpy (src
, w
,
2099 (rear_ptr
- w
) * sizeof (CHAR_T
));
2102 /* Process all characters in the string. */
2107 if (--len
== 0 && s
> src
)
2109 /* A new group begins. */
2110 #ifdef COMPILE_WPRINTF
2111 *--w
= thousands_sep
;
2115 *--w
= thousands_sep
[--cnt
];
2119 if (*grouping
== CHAR_MAX
2125 /* No further grouping to be done.
2126 Copy the rest of the number. */
2132 else if (*grouping
!= '\0')
2133 /* The previous grouping repeats ad infinitum. */
2142 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2145 struct _IO_FILE_plus _f
;
2146 #ifdef COMPILE_WPRINTF
2147 struct _IO_wide_data _wide_data
;
2149 _IO_FILE
*_put_stream
;
2150 #ifdef _IO_MTSAFE_IO
2156 _IO_helper_overflow (_IO_FILE
*s
, int c
)
2158 _IO_FILE
*target
= ((struct helper_file
*) s
)->_put_stream
;
2159 #ifdef COMPILE_WPRINTF
2160 int used
= s
->_wide_data
->_IO_write_ptr
- s
->_wide_data
->_IO_write_base
;
2163 _IO_size_t written
= _IO_sputn (target
, s
->_wide_data
->_IO_write_base
,
2165 if (written
== 0 || written
== WEOF
)
2167 __wmemmove (s
->_wide_data
->_IO_write_base
,
2168 s
->_wide_data
->_IO_write_base
+ written
,
2170 s
->_wide_data
->_IO_write_ptr
-= written
;
2173 int used
= s
->_IO_write_ptr
- s
->_IO_write_base
;
2176 _IO_size_t written
= _IO_sputn (target
, s
->_IO_write_base
, used
);
2177 if (written
== 0 || written
== EOF
)
2179 memmove (s
->_IO_write_base
, s
->_IO_write_base
+ written
,
2181 s
->_IO_write_ptr
-= written
;
2187 #ifdef COMPILE_WPRINTF
2188 static const struct _IO_jump_t _IO_helper_jumps
=
2191 JUMP_INIT (finish
, INTUSE(_IO_wdefault_finish
)),
2192 JUMP_INIT (overflow
, _IO_helper_overflow
),
2193 JUMP_INIT (underflow
, _IO_default_underflow
),
2194 JUMP_INIT (uflow
, INTUSE(_IO_default_uflow
)),
2195 JUMP_INIT (pbackfail
, (_IO_pbackfail_t
) INTUSE(_IO_wdefault_pbackfail
)),
2196 JUMP_INIT (xsputn
, INTUSE(_IO_wdefault_xsputn
)),
2197 JUMP_INIT (xsgetn
, INTUSE(_IO_wdefault_xsgetn
)),
2198 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2199 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2200 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2201 JUMP_INIT (sync
, _IO_default_sync
),
2202 JUMP_INIT (doallocate
, INTUSE(_IO_wdefault_doallocate
)),
2203 JUMP_INIT (read
, _IO_default_read
),
2204 JUMP_INIT (write
, _IO_default_write
),
2205 JUMP_INIT (seek
, _IO_default_seek
),
2206 JUMP_INIT (close
, _IO_default_close
),
2207 JUMP_INIT (stat
, _IO_default_stat
)
2210 static const struct _IO_jump_t _IO_helper_jumps
=
2213 JUMP_INIT (finish
, INTUSE(_IO_default_finish
)),
2214 JUMP_INIT (overflow
, _IO_helper_overflow
),
2215 JUMP_INIT (underflow
, _IO_default_underflow
),
2216 JUMP_INIT (uflow
, INTUSE(_IO_default_uflow
)),
2217 JUMP_INIT (pbackfail
, INTUSE(_IO_default_pbackfail
)),
2218 JUMP_INIT (xsputn
, INTUSE(_IO_default_xsputn
)),
2219 JUMP_INIT (xsgetn
, INTUSE(_IO_default_xsgetn
)),
2220 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2221 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2222 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2223 JUMP_INIT (sync
, _IO_default_sync
),
2224 JUMP_INIT (doallocate
, INTUSE(_IO_default_doallocate
)),
2225 JUMP_INIT (read
, _IO_default_read
),
2226 JUMP_INIT (write
, _IO_default_write
),
2227 JUMP_INIT (seek
, _IO_default_seek
),
2228 JUMP_INIT (close
, _IO_default_close
),
2229 JUMP_INIT (stat
, _IO_default_stat
)
2235 buffered_vfprintf (register _IO_FILE
*s
, const CHAR_T
*format
,
2238 CHAR_T buf
[_IO_BUFSIZ
];
2239 struct helper_file helper
;
2240 register _IO_FILE
*hp
= (_IO_FILE
*) &helper
._f
;
2241 int result
, to_flush
;
2243 /* Orient the stream. */
2248 /* Initialize helper. */
2249 helper
._put_stream
= s
;
2250 #ifdef COMPILE_WPRINTF
2251 hp
->_wide_data
= &helper
._wide_data
;
2252 _IO_wsetp (hp
, buf
, buf
+ sizeof buf
/ sizeof (CHAR_T
));
2255 _IO_setp (hp
, buf
, buf
+ sizeof buf
);
2258 hp
->_IO_file_flags
= _IO_MAGIC
|_IO_NO_READS
|_IO_USER_LOCK
;
2259 #if _IO_JUMPS_OFFSET
2260 hp
->_vtable_offset
= 0;
2262 #ifdef _IO_MTSAFE_IO
2265 hp
->_flags2
= s
->_flags2
;
2266 _IO_JUMPS (&helper
._f
) = (struct _IO_jump_t
*) &_IO_helper_jumps
;
2268 /* Now print to helper instead. */
2269 #ifndef COMPILE_WPRINTF
2270 result
= INTUSE(_IO_vfprintf
) (hp
, format
, args
);
2272 result
= vfprintf (hp
, format
, args
);
2276 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile
, s
);
2279 /* Now flush anything from the helper to the S. */
2280 #ifdef COMPILE_WPRINTF
2281 if ((to_flush
= (hp
->_wide_data
->_IO_write_ptr
2282 - hp
->_wide_data
->_IO_write_base
)) > 0)
2284 if ((int) _IO_sputn (s
, hp
->_wide_data
->_IO_write_base
, to_flush
)
2289 if ((to_flush
= hp
->_IO_write_ptr
- hp
->_IO_write_base
) > 0)
2291 if ((int) _IO_sputn (s
, hp
->_IO_write_base
, to_flush
) != to_flush
)
2296 /* Unlock the stream. */
2297 _IO_funlockfile (s
);
2298 __libc_cleanup_region_end (0);
2304 #ifdef COMPILE_WPRINTF
2305 strong_alias (_IO_vfwprintf
, __vfwprintf
);
2306 ldbl_weak_alias (_IO_vfwprintf
, vfwprintf
);
2308 ldbl_strong_alias (_IO_vfprintf_internal
, vfprintf
);
2309 ldbl_hidden_def (_IO_vfprintf_internal
, vfprintf
)
2310 ldbl_strong_alias (_IO_vfprintf_internal
, _IO_vfprintf
);