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. */ \
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. */ \
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 wron gduring 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) \
1196 len = strlen (string); \
1198 len = str2 - string - (ps.__count & 7); \
1202 len = strlen (string); \
1206 const wchar_t *s2 = (const wchar_t *) string; \
1207 mbstate_t mbstate; \
1209 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1213 /* The string `s2' might not be NUL terminated. */ \
1214 if (__libc_use_alloca (prec)) \
1215 string = (char *) alloca (prec); \
1216 else if ((string = (char *) malloc (prec)) == NULL) \
1222 string_malloced = 1; \
1223 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1227 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1228 if (len != (size_t) -1) \
1230 assert (__mbsinit (&mbstate)); \
1231 s2 = (const wchar_t *) string; \
1232 if (__libc_use_alloca (len + 1)) \
1233 string = (char *) alloca (len + 1); \
1234 else if ((string = (char *) malloc (len + 1)) == NULL) \
1240 string_malloced = 1; \
1241 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1245 if (len == (size_t) -1) \
1247 /* Illegal wide-character string. */ \
1253 if ((width -= len) < 0) \
1255 outstring (string, len); \
1261 outstring (string, len); \
1264 if (__builtin_expect (string_malloced, 0)) \
1270 /* Orient the stream. */
1275 /* Sanity check of arguments. */
1276 ARGCHECK (s
, format
);
1279 /* Check for correct orientation. */
1280 if (_IO_vtable_offset (s
) == 0 &&
1281 _IO_fwide (s
, sizeof (CHAR_T
) == 1 ? -1 : 1)
1282 != (sizeof (CHAR_T
) == 1 ? -1 : 1))
1283 /* The stream is already oriented otherwise. */
1287 if (UNBUFFERED_P (s
))
1288 /* Use a helper function which will allocate a local temporary buffer
1289 for the stream and then call us again. */
1290 return buffered_vfprintf (s
, format
, ap
);
1292 /* Initialize local variables. */
1294 grouping
= (const char *) -1;
1296 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1297 since on some systems `va_list' is not an integral type. */
1298 __va_copy (ap_save
, ap
);
1304 #ifdef COMPILE_WPRINTF
1305 /* Find the first format specifier. */
1306 f
= lead_str_end
= __find_specwc ((const UCHAR_T
*) format
);
1308 /* Find the first format specifier. */
1309 f
= lead_str_end
= __find_specmb ((const UCHAR_T
*) format
);
1313 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile
, s
);
1316 /* Write the literal text before the first format. */
1317 outstring ((const UCHAR_T
*) format
,
1318 lead_str_end
- (const UCHAR_T
*) format
);
1320 /* If we only have to print a simple string, return now. */
1324 /* Use the slow path in case any printf handler is registered. */
1325 if (__builtin_expect (__printf_function_table
!= NULL
1326 || __printf_modifier_table
!= NULL
1327 || __printf_va_arg_table
!= NULL
, 0))
1330 /* Process whole format string. */
1334 # define REF(Name) &&do_##Name - &&do_form_unknown
1336 # define REF(Name) &&do_##Name
1338 #define LABEL(Name) do_##Name
1342 union printf_arg
*args_value
; /* This is not used here but ... */
1343 int is_negative
; /* Flag for negative number. */
1346 unsigned long long int longlong
;
1347 unsigned long int word
;
1350 union printf_arg the_arg
;
1351 CHAR_T
*string
; /* Pointer to argument string. */
1352 int alt
= 0; /* Alternate format. */
1353 int space
= 0; /* Use space prefix if no sign is needed. */
1354 int left
= 0; /* Left-justify output. */
1355 int showsign
= 0; /* Always begin with plus or minus sign. */
1356 int group
= 0; /* Print numbers according grouping rules. */
1357 int is_long_double
= 0; /* Argument is long double/ long long int. */
1358 int is_short
= 0; /* Argument is short int. */
1359 int is_long
= 0; /* Argument is long int. */
1360 int is_char
= 0; /* Argument is promoted (unsigned) char. */
1361 int width
= 0; /* Width of output; 0 means none specified. */
1362 int prec
= -1; /* Precision of output; -1 means none specified. */
1363 /* This flag is set by the 'I' modifier and selects the use of the
1364 `outdigits' as determined by the current locale. */
1365 int use_outdigits
= 0;
1366 UCHAR_T pad
= L_(' ');/* Padding character. */
1370 workend
= &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
1372 /* Get current character in format string. */
1373 JUMP (*++f
, step0_jumps
);
1378 JUMP (*++f
, step0_jumps
);
1383 JUMP (*++f
, step0_jumps
);
1389 JUMP (*++f
, step0_jumps
);
1394 JUMP (*++f
, step0_jumps
);
1400 JUMP (*++f
, step0_jumps
);
1402 /* The '\'' flag. */
1406 if (grouping
== (const char *) -1)
1408 #ifdef COMPILE_WPRINTF
1409 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1410 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1412 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1415 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1416 if (*grouping
== '\0' || *grouping
== CHAR_MAX
1417 #ifdef COMPILE_WPRINTF
1418 || thousands_sep
== L
'\0'
1420 || *thousands_sep
== '\0'
1425 JUMP (*++f
, step0_jumps
);
1429 JUMP (*++f
, step0_jumps
);
1431 /* Get width from argument. */
1432 LABEL (width_asterics
):
1434 const UCHAR_T
*tmp
; /* Temporary value. */
1437 if (ISDIGIT (*tmp
) && read_int (&tmp
) && *tmp
== L_('$'))
1438 /* The width comes from a positional parameter. */
1441 width
= va_arg (ap
, int);
1443 /* Negative width means left justified. */
1451 if (__builtin_expect (width
>= (size_t) -1 / sizeof (CHAR_T
) - 32, 0))
1453 __set_errno (ERANGE
);
1458 if (width
>= sizeof (work_buffer
) / sizeof (work_buffer
[0]) - 32)
1460 /* We have to use a special buffer. The "32" is just a safe
1461 bet for all the output which is not counted in the width. */
1462 size_t needed
= ((size_t) width
+ 32) * sizeof (CHAR_T
);
1463 if (__libc_use_alloca (needed
))
1464 workend
= (CHAR_T
*) alloca (needed
) + width
+ 32;
1467 workstart
= (CHAR_T
*) malloc (needed
);
1468 if (workstart
== NULL
)
1473 workend
= workstart
+ width
+ 32;
1477 JUMP (*f
, step1_jumps
);
1479 /* Given width in format string. */
1481 width
= read_int (&f
);
1483 if (__builtin_expect (width
>= (size_t) -1 / sizeof (CHAR_T
) - 32, 0))
1485 __set_errno (ERANGE
);
1490 if (width
>= sizeof (work_buffer
) / sizeof (work_buffer
[0]) - 32)
1492 /* We have to use a special buffer. The "32" is just a safe
1493 bet for all the output which is not counted in the width. */
1494 size_t needed
= ((size_t) width
+ 32) * sizeof (CHAR_T
);
1495 if (__libc_use_alloca (needed
))
1496 workend
= (CHAR_T
*) alloca (needed
) + width
+ 32;
1499 workstart
= (CHAR_T
*) malloc (needed
);
1500 if (workstart
== NULL
)
1505 workend
= workstart
+ width
+ 32;
1509 /* Oh, oh. The argument comes from a positional parameter. */
1511 JUMP (*f
, step1_jumps
);
1517 const UCHAR_T
*tmp
; /* Temporary value. */
1520 if (ISDIGIT (*tmp
) && read_int (&tmp
) > 0 && *tmp
== L_('$'))
1521 /* The precision comes from a positional parameter. */
1524 prec
= va_arg (ap
, int);
1526 /* If the precision is negative the precision is omitted. */
1530 else if (ISDIGIT (*f
))
1531 prec
= read_int (&f
);
1535 && prec
> sizeof (work_buffer
) / sizeof (work_buffer
[0]) - 32)
1537 if (__builtin_expect (prec
>= (size_t) -1 / sizeof (CHAR_T
) - 32, 0))
1539 __set_errno (ERANGE
);
1543 size_t needed
= ((size_t) prec
+ 32) * sizeof (CHAR_T
);
1545 if (__libc_use_alloca (needed
))
1546 workend
= (CHAR_T
*) alloca (needed
) + prec
+ 32;
1549 workstart
= (CHAR_T
*) malloc (needed
);
1550 if (workstart
== NULL
)
1555 workend
= workstart
+ prec
+ 32;
1558 JUMP (*f
, step2_jumps
);
1560 /* Process 'h' modifier. There might another 'h' following. */
1563 JUMP (*++f
, step3a_jumps
);
1565 /* Process 'hh' modifier. */
1566 LABEL (mod_halfhalf
):
1569 JUMP (*++f
, step4_jumps
);
1571 /* Process 'l' modifier. There might another 'l' following. */
1574 JUMP (*++f
, step3b_jumps
);
1576 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1577 allowed to follow. */
1578 LABEL (mod_longlong
):
1581 JUMP (*++f
, step4_jumps
);
1584 is_long_double
= sizeof (size_t) > sizeof (unsigned long int);
1585 is_long
= sizeof (size_t) > sizeof (unsigned int);
1586 JUMP (*++f
, step4_jumps
);
1588 LABEL (mod_ptrdiff_t
):
1589 is_long_double
= sizeof (ptrdiff_t) > sizeof (unsigned long int);
1590 is_long
= sizeof (ptrdiff_t) > sizeof (unsigned int);
1591 JUMP (*++f
, step4_jumps
);
1593 LABEL (mod_intmax_t
):
1594 is_long_double
= sizeof (intmax_t) > sizeof (unsigned long int);
1595 is_long
= sizeof (intmax_t) > sizeof (unsigned int);
1596 JUMP (*++f
, step4_jumps
);
1598 /* Process current format. */
1601 process_arg (((struct printf_spec
*) NULL
));
1602 process_string_arg (((struct printf_spec
*) NULL
));
1604 LABEL (form_unknown
):
1605 if (spec
== L_('\0'))
1607 /* The format string ended before the specifier is complete. */
1612 /* If we are in the fast loop force entering the complicated
1617 /* The format is correctly handled. */
1620 if (__builtin_expect (workstart
!= NULL
, 0))
1624 /* Look for next format specifier. */
1625 #ifdef COMPILE_WPRINTF
1626 f
= __find_specwc ((end_of_spec
= ++f
));
1628 f
= __find_specmb ((end_of_spec
= ++f
));
1631 /* Write the following constant string. */
1632 outstring (end_of_spec
, f
- end_of_spec
);
1634 while (*f
!= L_('\0'));
1636 /* Unlock stream and return. */
1639 /* Here starts the more complex loop to handle positional parameters. */
1642 /* Array with information about the needed arguments. This has to
1643 be dynamically extensible. */
1645 /* A more or less arbitrary start value. */
1646 size_t nspecs_size
= 32 * sizeof (struct printf_spec
);
1647 struct printf_spec
*specs
= alloca (nspecs_size
);
1649 /* The number of arguments the format string requests. This will
1650 determine the size of the array needed to store the argument
1653 size_t bytes_per_arg
;
1654 union printf_arg
*args_value
;
1658 /* Positional parameters refer to arguments directly. This could
1659 also determine the maximum number of arguments. Track the
1661 size_t max_ref_arg
= 0;
1663 /* Just a counter. */
1669 if (grouping
== (const char *) -1)
1671 #ifdef COMPILE_WPRINTF
1672 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1673 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1675 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1678 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1679 if (*grouping
== '\0' || *grouping
== CHAR_MAX
)
1683 for (f
= lead_str_end
; *f
!= L_('\0'); f
= specs
[nspecs
++].next_fmt
)
1685 if (nspecs
* sizeof (*specs
) >= nspecs_size
)
1687 /* Extend the array of format specifiers. */
1688 struct printf_spec
*old
= specs
;
1689 specs
= extend_alloca (specs
, nspecs_size
, 2 * nspecs_size
);
1691 /* Copy the old array's elements to the new space. */
1692 memmove (specs
, old
, nspecs
* sizeof (*specs
));
1695 /* Parse the format specifier. */
1696 #ifdef COMPILE_WPRINTF
1697 nargs
+= __parse_one_specwc (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1699 nargs
+= __parse_one_specmb (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1703 /* Determine the number of arguments the format string consumes. */
1704 nargs
= MAX (nargs
, max_ref_arg
);
1705 /* Calculate total size needed to represent a single argument across
1706 all three argument-related arrays. */
1707 bytes_per_arg
= sizeof (*args_value
) + sizeof (*args_size
)
1708 + sizeof (*args_type
);
1710 /* Check for potential integer overflow. */
1711 if (__builtin_expect (nargs
> SIZE_MAX
/ bytes_per_arg
, 0))
1713 __set_errno (ERANGE
);
1718 /* Allocate memory for all three argument arrays. */
1719 if (__libc_use_alloca (nargs
* bytes_per_arg
))
1720 args_value
= alloca (nargs
* bytes_per_arg
);
1723 args_value
= args_malloced
= malloc (nargs
* bytes_per_arg
);
1724 if (args_value
== NULL
)
1731 /* Set up the remaining two arrays to each point past the end of the
1732 prior array, since space for all three has been allocated now. */
1733 args_size
= &args_value
[nargs
].pa_int
;
1734 args_type
= &args_size
[nargs
];
1735 memset (args_type
, s
->_flags2
& _IO_FLAGS2_FORTIFY
? '\xff' : '\0',
1736 nargs
* sizeof (*args_type
));
1738 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1739 still zero after this loop, format is invalid. For now we
1740 simply use 0 as the value. */
1742 /* Fill in the types of all the arguments. */
1743 for (cnt
= 0; cnt
< nspecs
; ++cnt
)
1745 /* If the width is determined by an argument this is an int. */
1746 if (specs
[cnt
].width_arg
!= -1)
1747 args_type
[specs
[cnt
].width_arg
] = PA_INT
;
1749 /* If the precision is determined by an argument this is an int. */
1750 if (specs
[cnt
].prec_arg
!= -1)
1751 args_type
[specs
[cnt
].prec_arg
] = PA_INT
;
1753 switch (specs
[cnt
].ndata_args
)
1755 case 0: /* No arguments. */
1757 case 1: /* One argument; we already have the
1759 args_type
[specs
[cnt
].data_arg
] = specs
[cnt
].data_arg_type
;
1760 args_size
[specs
[cnt
].data_arg
] = specs
[cnt
].size
;
1763 /* We have more than one argument for this format spec.
1764 We must call the arginfo function again to determine
1766 (void) (*__printf_arginfo_table
[specs
[cnt
].info
.spec
])
1768 specs
[cnt
].ndata_args
, &args_type
[specs
[cnt
].data_arg
],
1769 &args_size
[specs
[cnt
].data_arg
]);
1774 /* Now we know all the types and the order. Fill in the argument
1776 for (cnt
= 0; cnt
< nargs
; ++cnt
)
1777 switch (args_type
[cnt
])
1779 #define T(tag, mem, type) \
1781 args_value[cnt].mem = va_arg (ap_save, type); \
1784 T (PA_WCHAR
, pa_wchar
, wint_t);
1785 case PA_CHAR
: /* Promoted. */
1786 case PA_INT
|PA_FLAG_SHORT
: /* Promoted. */
1787 #if LONG_MAX == INT_MAX
1788 case PA_INT
|PA_FLAG_LONG
:
1790 T (PA_INT
, pa_int
, int);
1791 #if LONG_MAX == LONG_LONG_MAX
1792 case PA_INT
|PA_FLAG_LONG
:
1794 T (PA_INT
|PA_FLAG_LONG_LONG
, pa_long_long_int
, long long int);
1795 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1798 case PA_FLOAT
: /* Promoted. */
1799 T (PA_DOUBLE
, pa_double
, double);
1800 case PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
:
1803 args_value
[cnt
].pa_double
= va_arg (ap_save
, double);
1804 args_type
[cnt
] &= ~PA_FLAG_LONG_DOUBLE
;
1807 args_value
[cnt
].pa_long_double
= va_arg (ap_save
, long double);
1809 case PA_STRING
: /* All pointers are the same */
1810 case PA_WSTRING
: /* All pointers are the same */
1811 T (PA_POINTER
, pa_pointer
, void *);
1814 if ((args_type
[cnt
] & PA_FLAG_PTR
) != 0)
1815 args_value
[cnt
].pa_pointer
= va_arg (ap_save
, void *);
1816 else if (__builtin_expect (__printf_va_arg_table
!= NULL
, 0)
1817 && __printf_va_arg_table
[args_type
[cnt
] - PA_LAST
] != NULL
)
1819 args_value
[cnt
].pa_user
= alloca (args_size
[cnt
]);
1820 (*__printf_va_arg_table
[args_type
[cnt
] - PA_LAST
])
1821 (args_value
[cnt
].pa_user
, &ap_save
);
1824 args_value
[cnt
].pa_long_double
= 0.0;
1827 /* Error case. Not all parameters appear in N$ format
1828 strings. We have no way to determine their type. */
1829 assert (s
->_flags2
& _IO_FLAGS2_FORTIFY
);
1830 __libc_fatal ("*** invalid %N$ use detected ***\n");
1833 /* Now walk through all format specifiers and process them. */
1834 for (; (size_t) nspecs_done
< nspecs
; ++nspecs_done
)
1838 # define REF(Name) &&do2_##Name - &&do_form_unknown
1840 # define REF(Name) &&do2_##Name
1843 #define LABEL(Name) do2_##Name
1849 unsigned long long int longlong
;
1850 unsigned long int word
;
1853 union printf_arg the_arg
;
1854 CHAR_T
*string
; /* Pointer to argument string. */
1856 /* Fill variables from values in struct. */
1857 int alt
= specs
[nspecs_done
].info
.alt
;
1858 int space
= specs
[nspecs_done
].info
.space
;
1859 int left
= specs
[nspecs_done
].info
.left
;
1860 int showsign
= specs
[nspecs_done
].info
.showsign
;
1861 int group
= specs
[nspecs_done
].info
.group
;
1862 int is_long_double
= specs
[nspecs_done
].info
.is_long_double
;
1863 int is_short
= specs
[nspecs_done
].info
.is_short
;
1864 int is_char
= specs
[nspecs_done
].info
.is_char
;
1865 int is_long
= specs
[nspecs_done
].info
.is_long
;
1866 int width
= specs
[nspecs_done
].info
.width
;
1867 int prec
= specs
[nspecs_done
].info
.prec
;
1868 int use_outdigits
= specs
[nspecs_done
].info
.i18n
;
1869 char pad
= specs
[nspecs_done
].info
.pad
;
1870 CHAR_T spec
= specs
[nspecs_done
].info
.spec
;
1873 workend
= &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
1875 /* Fill in last information. */
1876 if (specs
[nspecs_done
].width_arg
!= -1)
1878 /* Extract the field width from an argument. */
1879 specs
[nspecs_done
].info
.width
=
1880 args_value
[specs
[nspecs_done
].width_arg
].pa_int
;
1882 if (specs
[nspecs_done
].info
.width
< 0)
1883 /* If the width value is negative left justification is
1884 selected and the value is taken as being positive. */
1886 specs
[nspecs_done
].info
.width
*= -1;
1887 left
= specs
[nspecs_done
].info
.left
= 1;
1889 width
= specs
[nspecs_done
].info
.width
;
1892 if (specs
[nspecs_done
].prec_arg
!= -1)
1894 /* Extract the precision from an argument. */
1895 specs
[nspecs_done
].info
.prec
=
1896 args_value
[specs
[nspecs_done
].prec_arg
].pa_int
;
1898 if (specs
[nspecs_done
].info
.prec
< 0)
1899 /* If the precision is negative the precision is
1901 specs
[nspecs_done
].info
.prec
= -1;
1903 prec
= specs
[nspecs_done
].info
.prec
;
1906 /* Maybe the buffer is too small. */
1907 if (MAX (prec
, width
) + 32 > (int) (sizeof (work_buffer
)
1910 if (__libc_use_alloca ((MAX (prec
, width
) + 32)
1912 workend
= ((CHAR_T
*) alloca ((MAX (prec
, width
) + 32)
1914 + (MAX (prec
, width
) + 32));
1917 workstart
= (CHAR_T
*) malloc ((MAX (prec
, width
) + 32)
1919 workend
= workstart
+ (MAX (prec
, width
) + 32);
1923 /* Process format specifiers. */
1926 extern printf_function
**__printf_function_table
;
1929 if (spec
<= UCHAR_MAX
1930 && __printf_function_table
!= NULL
1931 && __printf_function_table
[(size_t) spec
] != NULL
)
1933 const void **ptr
= alloca (specs
[nspecs_done
].ndata_args
1934 * sizeof (const void *));
1936 /* Fill in an array of pointers to the argument values. */
1937 for (unsigned int i
= 0; i
< specs
[nspecs_done
].ndata_args
;
1939 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
1941 /* Call the function. */
1942 function_done
= __printf_function_table
[(size_t) spec
]
1943 (s
, &specs
[nspecs_done
].info
, ptr
);
1945 if (function_done
!= -2)
1947 /* If an error occurred we don't have information
1948 about # of chars. */
1949 if (function_done
< 0)
1955 done_add (function_done
);
1960 JUMP (spec
, step4_jumps
);
1962 process_arg ((&specs
[nspecs_done
]));
1963 process_string_arg ((&specs
[nspecs_done
]));
1965 LABEL (form_unknown
):
1970 ptr
= alloca (specs
[nspecs_done
].ndata_args
1971 * sizeof (const void *));
1973 /* Fill in an array of pointers to the argument values. */
1974 for (i
= 0; i
< specs
[nspecs_done
].ndata_args
; ++i
)
1975 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
1977 /* Call the function. */
1978 function_done
= printf_unknown (s
, &specs
[nspecs_done
].info
,
1981 /* If an error occurred we don't have information about #
1983 if (function_done
< 0)
1989 done_add (function_done
);
1997 /* Write the following constant string. */
1998 outstring (specs
[nspecs_done
].end_of_fmt
,
1999 specs
[nspecs_done
].next_fmt
2000 - specs
[nspecs_done
].end_of_fmt
);
2005 free (args_malloced
);
2007 /* Unlock the stream. */
2008 _IO_funlockfile (s
);
2009 _IO_cleanup_region_end (0);
2014 /* Handle an unknown format specifier. This prints out a canonicalized
2015 representation of the format spec itself. */
2017 printf_unknown (FILE *s
, const struct printf_info
*info
,
2018 const void *const *args
)
2022 CHAR_T work_buffer
[MAX (sizeof (info
->width
), sizeof (info
->prec
)) * 3];
2023 CHAR_T
*const workend
2024 = &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
2035 else if (info
->space
)
2039 if (info
->pad
== L_('0'))
2044 if (info
->width
!= 0)
2046 w
= _itoa_word (info
->width
, workend
, 10, 0);
2051 if (info
->prec
!= -1)
2054 w
= _itoa_word (info
->prec
, workend
, 10, 0);
2059 if (info
->spec
!= L_('\0'))
2060 outchar (info
->spec
);
2066 /* Group the digits according to the grouping rules of the current locale.
2067 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
2070 group_number (CHAR_T
*w
, CHAR_T
*rear_ptr
, const char *grouping
,
2071 #ifdef COMPILE_WPRINTF
2072 wchar_t thousands_sep
2074 const char *thousands_sep
2080 #ifndef COMPILE_WPRINTF
2081 int tlen
= strlen (thousands_sep
);
2084 /* We treat all negative values like CHAR_MAX. */
2086 if (*grouping
== CHAR_MAX
|| *grouping
<= 0)
2087 /* No grouping should be done. */
2092 /* Copy existing string so that nothing gets overwritten. */
2093 src
= (CHAR_T
*) alloca ((rear_ptr
- w
) * sizeof (CHAR_T
));
2094 s
= (CHAR_T
*) __mempcpy (src
, w
,
2095 (rear_ptr
- w
) * sizeof (CHAR_T
));
2098 /* Process all characters in the string. */
2103 if (--len
== 0 && s
> src
)
2105 /* A new group begins. */
2106 #ifdef COMPILE_WPRINTF
2107 *--w
= thousands_sep
;
2111 *--w
= thousands_sep
[--cnt
];
2115 if (*grouping
== CHAR_MAX
2121 /* No further grouping to be done.
2122 Copy the rest of the number. */
2128 else if (*grouping
!= '\0')
2129 /* The previous grouping repeats ad infinitum. */
2138 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2141 struct _IO_FILE_plus _f
;
2142 #ifdef COMPILE_WPRINTF
2143 struct _IO_wide_data _wide_data
;
2145 _IO_FILE
*_put_stream
;
2146 #ifdef _IO_MTSAFE_IO
2152 _IO_helper_overflow (_IO_FILE
*s
, int c
)
2154 _IO_FILE
*target
= ((struct helper_file
*) s
)->_put_stream
;
2155 #ifdef COMPILE_WPRINTF
2156 int used
= s
->_wide_data
->_IO_write_ptr
- s
->_wide_data
->_IO_write_base
;
2159 _IO_size_t written
= _IO_sputn (target
, s
->_wide_data
->_IO_write_base
,
2161 if (written
== 0 || written
== WEOF
)
2163 __wmemmove (s
->_wide_data
->_IO_write_base
,
2164 s
->_wide_data
->_IO_write_base
+ written
,
2166 s
->_wide_data
->_IO_write_ptr
-= written
;
2169 int used
= s
->_IO_write_ptr
- s
->_IO_write_base
;
2172 _IO_size_t written
= _IO_sputn (target
, s
->_IO_write_base
, used
);
2173 if (written
== 0 || written
== EOF
)
2175 memmove (s
->_IO_write_base
, s
->_IO_write_base
+ written
,
2177 s
->_IO_write_ptr
-= written
;
2183 #ifdef COMPILE_WPRINTF
2184 static const struct _IO_jump_t _IO_helper_jumps
=
2187 JUMP_INIT (finish
, INTUSE(_IO_wdefault_finish
)),
2188 JUMP_INIT (overflow
, _IO_helper_overflow
),
2189 JUMP_INIT (underflow
, _IO_default_underflow
),
2190 JUMP_INIT (uflow
, INTUSE(_IO_default_uflow
)),
2191 JUMP_INIT (pbackfail
, (_IO_pbackfail_t
) INTUSE(_IO_wdefault_pbackfail
)),
2192 JUMP_INIT (xsputn
, INTUSE(_IO_wdefault_xsputn
)),
2193 JUMP_INIT (xsgetn
, INTUSE(_IO_wdefault_xsgetn
)),
2194 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2195 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2196 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2197 JUMP_INIT (sync
, _IO_default_sync
),
2198 JUMP_INIT (doallocate
, INTUSE(_IO_wdefault_doallocate
)),
2199 JUMP_INIT (read
, _IO_default_read
),
2200 JUMP_INIT (write
, _IO_default_write
),
2201 JUMP_INIT (seek
, _IO_default_seek
),
2202 JUMP_INIT (close
, _IO_default_close
),
2203 JUMP_INIT (stat
, _IO_default_stat
)
2206 static const struct _IO_jump_t _IO_helper_jumps
=
2209 JUMP_INIT (finish
, INTUSE(_IO_default_finish
)),
2210 JUMP_INIT (overflow
, _IO_helper_overflow
),
2211 JUMP_INIT (underflow
, _IO_default_underflow
),
2212 JUMP_INIT (uflow
, INTUSE(_IO_default_uflow
)),
2213 JUMP_INIT (pbackfail
, INTUSE(_IO_default_pbackfail
)),
2214 JUMP_INIT (xsputn
, INTUSE(_IO_default_xsputn
)),
2215 JUMP_INIT (xsgetn
, INTUSE(_IO_default_xsgetn
)),
2216 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2217 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2218 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2219 JUMP_INIT (sync
, _IO_default_sync
),
2220 JUMP_INIT (doallocate
, INTUSE(_IO_default_doallocate
)),
2221 JUMP_INIT (read
, _IO_default_read
),
2222 JUMP_INIT (write
, _IO_default_write
),
2223 JUMP_INIT (seek
, _IO_default_seek
),
2224 JUMP_INIT (close
, _IO_default_close
),
2225 JUMP_INIT (stat
, _IO_default_stat
)
2231 buffered_vfprintf (register _IO_FILE
*s
, const CHAR_T
*format
,
2234 CHAR_T buf
[_IO_BUFSIZ
];
2235 struct helper_file helper
;
2236 register _IO_FILE
*hp
= (_IO_FILE
*) &helper
._f
;
2237 int result
, to_flush
;
2239 /* Orient the stream. */
2244 /* Initialize helper. */
2245 helper
._put_stream
= s
;
2246 #ifdef COMPILE_WPRINTF
2247 hp
->_wide_data
= &helper
._wide_data
;
2248 _IO_wsetp (hp
, buf
, buf
+ sizeof buf
/ sizeof (CHAR_T
));
2251 _IO_setp (hp
, buf
, buf
+ sizeof buf
);
2254 hp
->_IO_file_flags
= _IO_MAGIC
|_IO_NO_READS
|_IO_USER_LOCK
;
2255 #if _IO_JUMPS_OFFSET
2256 hp
->_vtable_offset
= 0;
2258 #ifdef _IO_MTSAFE_IO
2261 hp
->_flags2
= s
->_flags2
;
2262 _IO_JUMPS (&helper
._f
) = (struct _IO_jump_t
*) &_IO_helper_jumps
;
2264 /* Now print to helper instead. */
2265 #ifndef COMPILE_WPRINTF
2266 result
= INTUSE(_IO_vfprintf
) (hp
, format
, args
);
2268 result
= vfprintf (hp
, format
, args
);
2272 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile
, s
);
2275 /* Now flush anything from the helper to the S. */
2276 #ifdef COMPILE_WPRINTF
2277 if ((to_flush
= (hp
->_wide_data
->_IO_write_ptr
2278 - hp
->_wide_data
->_IO_write_base
)) > 0)
2280 if ((int) _IO_sputn (s
, hp
->_wide_data
->_IO_write_base
, to_flush
)
2285 if ((to_flush
= hp
->_IO_write_ptr
- hp
->_IO_write_base
) > 0)
2287 if ((int) _IO_sputn (s
, hp
->_IO_write_base
, to_flush
) != to_flush
)
2292 /* Unlock the stream. */
2293 _IO_funlockfile (s
);
2294 __libc_cleanup_region_end (0);
2300 #ifdef COMPILE_WPRINTF
2301 strong_alias (_IO_vfwprintf
, __vfwprintf
);
2302 ldbl_weak_alias (_IO_vfwprintf
, vfwprintf
);
2304 ldbl_strong_alias (_IO_vfprintf_internal
, vfprintf
);
2305 ldbl_hidden_def (_IO_vfprintf_internal
, vfprintf
)
2306 ldbl_strong_alias (_IO_vfprintf_internal
, _IO_vfprintf
);