1 /* Copyright (C) 1991-2012 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 (INT_MAX - done < _val, 0)) \
73 __set_errno (EOVERFLOW); \
79 #ifndef COMPILE_WPRINTF
80 # define vfprintf _IO_vfprintf_internal
82 # define UCHAR_T unsigned char
85 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
86 # define STR_LEN(Str) strlen (Str)
88 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
89 # define PAD(Padchar) \
91 done_add (_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)) \
149 if (__builtin_expect (INT_MAX - done < (Len), 0)) \
152 __set_errno (EOVERFLOW); \
159 /* For handling long_double and longlong we use the same flag. If
160 `long' and `long long' are effectively the same type define it to
162 #if LONG_MAX == LONG_LONG_MAX
163 # define is_longlong 0
165 # define is_longlong is_long_double
168 /* If `long' and `int' is effectively the same type we don't have to
169 handle `long separately. */
170 #if INT_MAX == LONG_MAX
171 # define is_long_num 0
173 # define is_long_num is_long
177 /* Global variables. */
178 static const CHAR_T null[] = L_("(null)");
181 /* Helper function to provide temporary buffering for unbuffered streams. */
182 static int buffered_vfprintf (FILE *stream, const CHAR_T *fmt, va_list)
183 __THROW __attribute__ ((noinline)) internal_function;
185 /* Handle unknown format specifier. */
186 static int printf_unknown (FILE *, const struct printf_info *,
187 const void *const *) __THROW;
189 /* Group digits of number string. */
190 #ifdef COMPILE_WPRINTF
191 static CHAR_T *group_number (CHAR_T *, CHAR_T *, const char *, wchar_t)
192 __THROW internal_function;
194 static CHAR_T *group_number (CHAR_T *, CHAR_T *, const char *, const char *)
195 __THROW internal_function;
199 /* The function itself. */
201 vfprintf (FILE *s, const CHAR_T *format, va_list ap)
203 /* The character used as thousands separator. */
204 #ifdef COMPILE_WPRINTF
205 wchar_t thousands_sep = L'\0';
207 const char *thousands_sep = NULL;
210 /* The string describing the size of groups of digits. */
211 const char *grouping;
213 /* Place to accumulate the result. */
216 /* Current character in format string. */
219 /* End of leading constant string. */
220 const UCHAR_T *lead_str_end;
222 /* Points to next format specifier. */
223 const UCHAR_T *end_of_spec;
225 /* Buffer intermediate results. */
226 CHAR_T work_buffer[1000];
227 CHAR_T *workstart = NULL;
230 /* We have to save the original argument pointer. */
233 /* Count number of specifiers we already processed. */
236 /* For the %m format we may need the current `errno' value. */
237 int save_errno = errno;
239 /* 1 if format is in read-only memory, -1 if it is in writable memory,
241 int readonly_format = 0;
243 /* For the argument descriptions, which may be allocated on the heap. */
244 void *args_malloced = NULL;
246 /* This table maps a character into a number representing a
247 class. In each step there is a destination label for each
249 static const uint8_t jump_table[] =
251 /* ' ' */ 1, 0, 0, /* '#' */ 4,
252 0, /* '%' */ 14, 0, /* '\''*/ 6,
253 0, 0, /* '*' */ 7, /* '+' */ 2,
254 0, /* '-' */ 3, /* '.' */ 9, 0,
255 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
256 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
257 /* '8' */ 8, /* '9' */ 8, 0, 0,
259 0, /* 'A' */ 26, 0, /* 'C' */ 25,
260 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
261 0, /* 'I' */ 29, 0, 0,
262 /* 'L' */ 12, 0, 0, 0,
263 0, 0, 0, /* 'S' */ 21,
265 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
267 0, /* 'a' */ 26, 0, /* 'c' */ 20,
268 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
269 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
270 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
271 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
272 /* 't' */ 27, /* 'u' */ 16, 0, 0,
273 /* 'x' */ 18, 0, /* 'z' */ 13
276 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
277 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
279 /* 'int' is enough and it saves some space on 64 bit systems. */
280 # define JUMP_TABLE_TYPE const int
281 # define JUMP(ChExpr, table) \
285 void *__unbounded ptr; \
287 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
288 : table[CHAR_CLASS (spec)]; \
289 ptr = &&do_form_unknown + offset; \
294 # define JUMP_TABLE_TYPE const void *const
295 # define JUMP(ChExpr, table) \
298 const void *__unbounded ptr; \
300 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
301 : table[CHAR_CLASS (spec)]; \
307 #define STEP0_3_TABLE \
308 /* Step 0: at the beginning. */ \
309 static JUMP_TABLE_TYPE step0_jumps[30] = \
311 REF (form_unknown), \
312 REF (flag_space), /* for ' ' */ \
313 REF (flag_plus), /* for '+' */ \
314 REF (flag_minus), /* for '-' */ \
315 REF (flag_hash), /* for '<hash>' */ \
316 REF (flag_zero), /* for '0' */ \
317 REF (flag_quote), /* for '\'' */ \
318 REF (width_asterics), /* for '*' */ \
319 REF (width), /* for '1'...'9' */ \
320 REF (precision), /* for '.' */ \
321 REF (mod_half), /* for 'h' */ \
322 REF (mod_long), /* for 'l' */ \
323 REF (mod_longlong), /* for 'L', 'q' */ \
324 REF (mod_size_t), /* for 'z', 'Z' */ \
325 REF (form_percent), /* for '%' */ \
326 REF (form_integer), /* for 'd', 'i' */ \
327 REF (form_unsigned), /* for 'u' */ \
328 REF (form_octal), /* for 'o' */ \
329 REF (form_hexa), /* for 'X', 'x' */ \
330 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
331 REF (form_character), /* for 'c' */ \
332 REF (form_string), /* for 's', 'S' */ \
333 REF (form_pointer), /* for 'p' */ \
334 REF (form_number), /* for 'n' */ \
335 REF (form_strerror), /* for 'm' */ \
336 REF (form_wcharacter), /* for 'C' */ \
337 REF (form_floathex), /* for 'A', 'a' */ \
338 REF (mod_ptrdiff_t), /* for 't' */ \
339 REF (mod_intmax_t), /* for 'j' */ \
340 REF (flag_i18n), /* for 'I' */ \
342 /* Step 1: after processing width. */ \
343 static JUMP_TABLE_TYPE step1_jumps[30] = \
345 REF (form_unknown), \
346 REF (form_unknown), /* for ' ' */ \
347 REF (form_unknown), /* for '+' */ \
348 REF (form_unknown), /* for '-' */ \
349 REF (form_unknown), /* for '<hash>' */ \
350 REF (form_unknown), /* for '0' */ \
351 REF (form_unknown), /* for '\'' */ \
352 REF (form_unknown), /* for '*' */ \
353 REF (form_unknown), /* for '1'...'9' */ \
354 REF (precision), /* for '.' */ \
355 REF (mod_half), /* for 'h' */ \
356 REF (mod_long), /* for 'l' */ \
357 REF (mod_longlong), /* for 'L', 'q' */ \
358 REF (mod_size_t), /* for 'z', 'Z' */ \
359 REF (form_percent), /* for '%' */ \
360 REF (form_integer), /* for 'd', 'i' */ \
361 REF (form_unsigned), /* for 'u' */ \
362 REF (form_octal), /* for 'o' */ \
363 REF (form_hexa), /* for 'X', 'x' */ \
364 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
365 REF (form_character), /* for 'c' */ \
366 REF (form_string), /* for 's', 'S' */ \
367 REF (form_pointer), /* for 'p' */ \
368 REF (form_number), /* for 'n' */ \
369 REF (form_strerror), /* for 'm' */ \
370 REF (form_wcharacter), /* for 'C' */ \
371 REF (form_floathex), /* for 'A', 'a' */ \
372 REF (mod_ptrdiff_t), /* for 't' */ \
373 REF (mod_intmax_t), /* for 'j' */ \
374 REF (form_unknown) /* for 'I' */ \
376 /* Step 2: after processing precision. */ \
377 static JUMP_TABLE_TYPE step2_jumps[30] = \
379 REF (form_unknown), \
380 REF (form_unknown), /* for ' ' */ \
381 REF (form_unknown), /* for '+' */ \
382 REF (form_unknown), /* for '-' */ \
383 REF (form_unknown), /* for '<hash>' */ \
384 REF (form_unknown), /* for '0' */ \
385 REF (form_unknown), /* for '\'' */ \
386 REF (form_unknown), /* for '*' */ \
387 REF (form_unknown), /* for '1'...'9' */ \
388 REF (form_unknown), /* for '.' */ \
389 REF (mod_half), /* for 'h' */ \
390 REF (mod_long), /* for 'l' */ \
391 REF (mod_longlong), /* for 'L', 'q' */ \
392 REF (mod_size_t), /* for 'z', 'Z' */ \
393 REF (form_percent), /* for '%' */ \
394 REF (form_integer), /* for 'd', 'i' */ \
395 REF (form_unsigned), /* for 'u' */ \
396 REF (form_octal), /* for 'o' */ \
397 REF (form_hexa), /* for 'X', 'x' */ \
398 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
399 REF (form_character), /* for 'c' */ \
400 REF (form_string), /* for 's', 'S' */ \
401 REF (form_pointer), /* for 'p' */ \
402 REF (form_number), /* for 'n' */ \
403 REF (form_strerror), /* for 'm' */ \
404 REF (form_wcharacter), /* for 'C' */ \
405 REF (form_floathex), /* for 'A', 'a' */ \
406 REF (mod_ptrdiff_t), /* for 't' */ \
407 REF (mod_intmax_t), /* for 'j' */ \
408 REF (form_unknown) /* for 'I' */ \
410 /* Step 3a: after processing first 'h' modifier. */ \
411 static JUMP_TABLE_TYPE step3a_jumps[30] = \
413 REF (form_unknown), \
414 REF (form_unknown), /* for ' ' */ \
415 REF (form_unknown), /* for '+' */ \
416 REF (form_unknown), /* for '-' */ \
417 REF (form_unknown), /* for '<hash>' */ \
418 REF (form_unknown), /* for '0' */ \
419 REF (form_unknown), /* for '\'' */ \
420 REF (form_unknown), /* for '*' */ \
421 REF (form_unknown), /* for '1'...'9' */ \
422 REF (form_unknown), /* for '.' */ \
423 REF (mod_halfhalf), /* for 'h' */ \
424 REF (form_unknown), /* for 'l' */ \
425 REF (form_unknown), /* for 'L', 'q' */ \
426 REF (form_unknown), /* for 'z', 'Z' */ \
427 REF (form_percent), /* for '%' */ \
428 REF (form_integer), /* for 'd', 'i' */ \
429 REF (form_unsigned), /* for 'u' */ \
430 REF (form_octal), /* for 'o' */ \
431 REF (form_hexa), /* for 'X', 'x' */ \
432 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
433 REF (form_unknown), /* for 'c' */ \
434 REF (form_unknown), /* for 's', 'S' */ \
435 REF (form_unknown), /* for 'p' */ \
436 REF (form_number), /* for 'n' */ \
437 REF (form_unknown), /* for 'm' */ \
438 REF (form_unknown), /* for 'C' */ \
439 REF (form_unknown), /* for 'A', 'a' */ \
440 REF (form_unknown), /* for 't' */ \
441 REF (form_unknown), /* for 'j' */ \
442 REF (form_unknown) /* for 'I' */ \
444 /* Step 3b: after processing first 'l' modifier. */ \
445 static JUMP_TABLE_TYPE step3b_jumps[30] = \
447 REF (form_unknown), \
448 REF (form_unknown), /* for ' ' */ \
449 REF (form_unknown), /* for '+' */ \
450 REF (form_unknown), /* for '-' */ \
451 REF (form_unknown), /* for '<hash>' */ \
452 REF (form_unknown), /* for '0' */ \
453 REF (form_unknown), /* for '\'' */ \
454 REF (form_unknown), /* for '*' */ \
455 REF (form_unknown), /* for '1'...'9' */ \
456 REF (form_unknown), /* for '.' */ \
457 REF (form_unknown), /* for 'h' */ \
458 REF (mod_longlong), /* for 'l' */ \
459 REF (form_unknown), /* for 'L', 'q' */ \
460 REF (form_unknown), /* for 'z', 'Z' */ \
461 REF (form_percent), /* for '%' */ \
462 REF (form_integer), /* for 'd', 'i' */ \
463 REF (form_unsigned), /* for 'u' */ \
464 REF (form_octal), /* for 'o' */ \
465 REF (form_hexa), /* for 'X', 'x' */ \
466 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
467 REF (form_character), /* for 'c' */ \
468 REF (form_string), /* for 's', 'S' */ \
469 REF (form_pointer), /* for 'p' */ \
470 REF (form_number), /* for 'n' */ \
471 REF (form_strerror), /* for 'm' */ \
472 REF (form_wcharacter), /* for 'C' */ \
473 REF (form_floathex), /* for 'A', 'a' */ \
474 REF (form_unknown), /* for 't' */ \
475 REF (form_unknown), /* for 'j' */ \
476 REF (form_unknown) /* for 'I' */ \
479 #define STEP4_TABLE \
480 /* Step 4: processing format specifier. */ \
481 static JUMP_TABLE_TYPE step4_jumps[30] = \
483 REF (form_unknown), \
484 REF (form_unknown), /* for ' ' */ \
485 REF (form_unknown), /* for '+' */ \
486 REF (form_unknown), /* for '-' */ \
487 REF (form_unknown), /* for '<hash>' */ \
488 REF (form_unknown), /* for '0' */ \
489 REF (form_unknown), /* for '\'' */ \
490 REF (form_unknown), /* for '*' */ \
491 REF (form_unknown), /* for '1'...'9' */ \
492 REF (form_unknown), /* for '.' */ \
493 REF (form_unknown), /* for 'h' */ \
494 REF (form_unknown), /* for 'l' */ \
495 REF (form_unknown), /* for 'L', 'q' */ \
496 REF (form_unknown), /* for 'z', 'Z' */ \
497 REF (form_percent), /* for '%' */ \
498 REF (form_integer), /* for 'd', 'i' */ \
499 REF (form_unsigned), /* for 'u' */ \
500 REF (form_octal), /* for 'o' */ \
501 REF (form_hexa), /* for 'X', 'x' */ \
502 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
503 REF (form_character), /* for 'c' */ \
504 REF (form_string), /* for 's', 'S' */ \
505 REF (form_pointer), /* for 'p' */ \
506 REF (form_number), /* for 'n' */ \
507 REF (form_strerror), /* for 'm' */ \
508 REF (form_wcharacter), /* for 'C' */ \
509 REF (form_floathex), /* for 'A', 'a' */ \
510 REF (form_unknown), /* for 't' */ \
511 REF (form_unknown), /* for 'j' */ \
512 REF (form_unknown) /* for 'I' */ \
516 #define process_arg(fspec) \
517 /* Start real work. We know about all flags and modifiers and \
518 now process the wanted format specifier. */ \
519 LABEL (form_percent): \
520 /* Write a literal "%". */ \
524 LABEL (form_integer): \
525 /* Signed decimal integer. */ \
530 long long int signed_number; \
533 signed_number = va_arg (ap, long long int); \
535 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
537 is_negative = signed_number < 0; \
538 number.longlong = is_negative ? (- signed_number) : signed_number; \
540 goto LABEL (longlong_number); \
544 long int signed_number; \
549 signed_number = va_arg (ap, long int); \
551 signed_number = (signed char) va_arg (ap, unsigned int); \
552 else if (!is_short) \
553 signed_number = va_arg (ap, int); \
555 signed_number = (short int) va_arg (ap, unsigned int); \
559 signed_number = args_value[fspec->data_arg].pa_long_int; \
561 signed_number = (signed char) \
562 args_value[fspec->data_arg].pa_u_int; \
563 else if (!is_short) \
564 signed_number = args_value[fspec->data_arg].pa_int; \
566 signed_number = (short int) \
567 args_value[fspec->data_arg].pa_u_int; \
569 is_negative = signed_number < 0; \
570 number.word = is_negative ? (- signed_number) : signed_number; \
572 goto LABEL (number); \
576 LABEL (form_unsigned): \
577 /* Unsigned decimal integer. */ \
579 goto LABEL (unsigned_number); \
582 LABEL (form_octal): \
583 /* Unsigned octal integer. */ \
585 goto LABEL (unsigned_number); \
589 /* Unsigned hexadecimal integer. */ \
592 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
594 /* ISO specifies the `+' and ` ' flags only for signed \
603 number.longlong = va_arg (ap, unsigned long long int); \
605 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
607 LABEL (longlong_number): \
609 /* Supply a default precision if none was given. */ \
612 /* We have to take care for the '0' flag. If a precision \
613 is given it must be ignored. */ \
616 /* If the precision is 0 and the number is 0 nothing has to \
617 be written for the number, except for the 'o' format in \
619 if (prec == 0 && number.longlong == 0) \
622 if (base == 8 && alt) \
623 *--string = L_('0'); \
627 /* Put the number in WORK. */ \
628 string = _itoa (number.longlong, workend, base, \
630 if (group && grouping) \
631 string = group_number (string, workend, grouping, \
634 if (use_outdigits && base == 10) \
635 string = _i18n_number_rewrite (string, workend, workend); \
637 /* Simplify further test for num != 0. */ \
638 number.word = number.longlong != 0; \
645 number.word = va_arg (ap, unsigned long int); \
647 number.word = (unsigned char) va_arg (ap, unsigned int); \
648 else if (!is_short) \
649 number.word = va_arg (ap, unsigned int); \
651 number.word = (unsigned short int) va_arg (ap, unsigned int); \
655 number.word = args_value[fspec->data_arg].pa_u_long_int; \
657 number.word = (unsigned char) \
658 args_value[fspec->data_arg].pa_u_int; \
659 else if (!is_short) \
660 number.word = args_value[fspec->data_arg].pa_u_int; \
662 number.word = (unsigned short int) \
663 args_value[fspec->data_arg].pa_u_int; \
667 /* Supply a default precision if none was given. */ \
670 /* We have to take care for the '0' flag. If a precision \
671 is given it must be ignored. */ \
674 /* If the precision is 0 and the number is 0 nothing has to \
675 be written for the number, except for the 'o' format in \
677 if (prec == 0 && number.word == 0) \
680 if (base == 8 && alt) \
681 *--string = L_('0'); \
685 /* Put the number in WORK. */ \
686 string = _itoa_word (number.word, workend, base, \
688 if (group && grouping) \
689 string = group_number (string, workend, grouping, \
692 if (use_outdigits && base == 10) \
693 string = _i18n_number_rewrite (string, workend, workend); \
697 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
698 /* Add octal marker. */ \
699 *--string = L_('0'); \
701 prec = MAX (0, prec - (workend - string)); \
705 width -= workend - string + prec; \
707 if (number.word != 0 && alt && base == 16) \
708 /* Account for 0X hex marker. */ \
711 if (is_negative || showsign || space) \
714 if (pad == L_(' ')) \
727 if (number.word != 0 && alt && base == 16) \
736 outstring (string, workend - string); \
758 if (number.word != 0 && alt && base == 16) \
765 width -= workend - string + prec; \
775 outstring (string, workend - string); \
781 LABEL (form_float): \
783 /* Floating-point number. This is handled by printf_fp.c. */ \
790 is_long_double = 0; \
792 struct printf_info info = { .prec = prec, \
795 .is_long_double = is_long_double, \
796 .is_short = is_short, \
797 .is_long = is_long, \
801 .showsign = showsign, \
805 .i18n = use_outdigits, \
806 .wide = sizeof (CHAR_T) != 1 }; \
808 if (is_long_double) \
809 the_arg.pa_long_double = va_arg (ap, long double); \
811 the_arg.pa_double = va_arg (ap, double); \
812 ptr = (const void *) &the_arg; \
814 function_done = __printf_fp (s, &info, &ptr); \
818 ptr = (const void *) &args_value[fspec->data_arg]; \
821 fspec->data_arg_type = PA_DOUBLE; \
822 fspec->info.is_long_double = 0; \
825 function_done = __printf_fp (s, &fspec->info, &ptr); \
828 if (function_done < 0) \
830 /* Error in print handler; up to handler to set errno. */ \
835 done_add (function_done); \
839 LABEL (form_floathex): \
841 /* Floating point number printed as hexadecimal number. */ \
848 is_long_double = 0; \
850 struct printf_info info = { .prec = prec, \
853 .is_long_double = is_long_double, \
854 .is_short = is_short, \
855 .is_long = is_long, \
859 .showsign = showsign, \
863 .wide = sizeof (CHAR_T) != 1 }; \
865 if (is_long_double) \
866 the_arg.pa_long_double = va_arg (ap, long double); \
868 the_arg.pa_double = va_arg (ap, double); \
869 ptr = (const void *) &the_arg; \
871 function_done = __printf_fphex (s, &info, &ptr); \
875 ptr = (const void *) &args_value[fspec->data_arg]; \
877 fspec->info.is_long_double = 0; \
879 function_done = __printf_fphex (s, &fspec->info, &ptr); \
882 if (function_done < 0) \
884 /* Error in print handler; up to handler to set errno. */ \
889 done_add (function_done); \
893 LABEL (form_pointer): \
894 /* Generic pointer. */ \
898 ptr = va_arg (ap, void *); \
900 ptr = args_value[fspec->data_arg].pa_pointer; \
903 /* If the pointer is not NULL, write it as a %#x spec. */ \
905 number.word = (unsigned long int) ptr; \
910 goto LABEL (number); \
914 /* Write "(nil)" for a nil pointer. */ \
915 string = (CHAR_T *) L_("(nil)"); \
916 /* Make sure the full string "(nil)" is printed. */ \
919 is_long = 0; /* This is no wide-char string. */ \
920 goto LABEL (print_string); \
925 LABEL (form_number): \
926 if (s->_flags2 & _IO_FLAGS2_FORTIFY) \
928 if (! readonly_format) \
930 extern int __readonly_area (const void *, size_t) \
933 = __readonly_area (format, ((STR_LEN (format) + 1) \
934 * sizeof (CHAR_T))); \
936 if (readonly_format < 0) \
937 __libc_fatal ("*** %n in writable segment detected ***\n"); \
939 /* Answer the count of characters written. */ \
943 *(long long int *) va_arg (ap, void *) = done; \
944 else if (is_long_num) \
945 *(long int *) va_arg (ap, void *) = done; \
947 *(char *) va_arg (ap, void *) = done; \
948 else if (!is_short) \
949 *(int *) va_arg (ap, void *) = done; \
951 *(short int *) va_arg (ap, void *) = done; \
955 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
956 else if (is_long_num) \
957 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
959 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
960 else if (!is_short) \
961 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
963 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
966 LABEL (form_strerror): \
967 /* Print description of error ERRNO. */ \
969 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
970 sizeof work_buffer); \
971 is_long = 0; /* This is no wide-char string. */ \
972 goto LABEL (print_string)
974 #ifdef COMPILE_WPRINTF
975 # define process_string_arg(fspec) \
976 LABEL (form_character): \
979 goto LABEL (form_wcharacter); \
980 --width; /* Account for the character itself. */ \
984 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
986 outchar (__btowc ((unsigned char) \
987 args_value[fspec->data_arg].pa_int)); \
992 LABEL (form_wcharacter): \
994 /* Wide character. */ \
999 outchar (va_arg (ap, wchar_t)); \
1001 outchar (args_value[fspec->data_arg].pa_wchar); \
1007 LABEL (form_string): \
1010 int string_malloced; \
1012 /* The string argument could in fact be `char *' or `wchar_t *'. \
1013 But this should not make a difference here. */ \
1014 if (fspec == NULL) \
1015 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
1017 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
1019 /* Entry point for printing other strings. */ \
1020 LABEL (print_string): \
1022 string_malloced = 0; \
1023 if (string == NULL) \
1025 /* Write "(null)" if there's space. */ \
1027 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
1029 string = (CHAR_T *) null; \
1030 len = (sizeof (null) / sizeof (null[0])) - 1; \
1034 string = (CHAR_T *) L""; \
1038 else if (!is_long && spec != L_('S')) \
1040 /* This is complicated. We have to transform the multibyte \
1041 string into a wide character string. */ \
1042 const char *mbs = (const char *) string; \
1043 mbstate_t mbstate; \
1045 len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
1047 /* Allocate dynamically an array which definitely is long \
1048 enough for the wide character version. Each byte in the \
1049 multi-byte string can produce at most one wide character. */ \
1050 if (__libc_use_alloca (len * sizeof (wchar_t))) \
1051 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1052 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1059 string_malloced = 1; \
1061 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1062 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1063 if (len == (size_t) -1) \
1065 /* Illegal multibyte character. */ \
1073 /* Search for the end of the string, but don't search past \
1074 the length specified by the precision. */ \
1075 len = __wcsnlen (string, prec); \
1077 len = __wcslen (string); \
1080 if ((width -= len) < 0) \
1082 outstring (string, len); \
1088 outstring (string, len); \
1091 if (__builtin_expect (string_malloced, 0)) \
1096 # define process_string_arg(fspec) \
1097 LABEL (form_character): \
1100 goto LABEL (form_wcharacter); \
1101 --width; /* Account for the character itself. */ \
1104 if (fspec == NULL) \
1105 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1107 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1112 LABEL (form_wcharacter): \
1114 /* Wide character. */ \
1115 char buf[MB_CUR_MAX]; \
1116 mbstate_t mbstate; \
1119 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1120 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1121 : args_value[fspec->data_arg].pa_wchar), \
1123 if (len == (size_t) -1) \
1125 /* Something went wrong during the conversion. Bail out. */ \
1132 outstring (buf, len); \
1138 LABEL (form_string): \
1141 int string_malloced; \
1143 /* The string argument could in fact be `char *' or `wchar_t *'. \
1144 But this should not make a difference here. */ \
1145 if (fspec == NULL) \
1146 string = (char *) va_arg (ap, const char *); \
1148 string = (char *) args_value[fspec->data_arg].pa_string; \
1150 /* Entry point for printing other strings. */ \
1151 LABEL (print_string): \
1153 string_malloced = 0; \
1154 if (string == NULL) \
1156 /* Write "(null)" if there's space. */ \
1157 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1159 string = (char *) null; \
1160 len = sizeof (null) - 1; \
1164 string = (char *) ""; \
1168 else if (!is_long && spec != L_('S')) \
1172 /* Search for the end of the string, but don't search past \
1173 the length (in bytes) specified by the precision. Also \
1174 don't use incomplete characters. */ \
1175 if (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MB_CUR_MAX) == 1) \
1176 len = __strnlen (string, prec); \
1179 /* In case we have a multibyte character set the \
1180 situation is more complicated. We must not copy \
1181 bytes at the end which form an incomplete character. */\
1182 size_t ignore_size = (unsigned) prec > 1024 ? 1024 : prec;\
1183 wchar_t ignore[ignore_size]; \
1184 const char *str2 = string; \
1185 const char *strend = string + prec; \
1186 if (strend < string) \
1187 strend = (const char *) UINTPTR_MAX; \
1190 memset (&ps, '\0', sizeof (ps)); \
1192 while (str2 != NULL && str2 < strend) \
1193 if (__mbsnrtowcs (ignore, &str2, strend - str2, \
1194 ignore_size, &ps) == (size_t) -1) \
1196 /* Conversion function has set errno. */ \
1202 len = strlen (string); \
1204 len = str2 - string - (ps.__count & 7); \
1208 len = strlen (string); \
1212 const wchar_t *s2 = (const wchar_t *) string; \
1213 mbstate_t mbstate; \
1215 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1219 /* The string `s2' might not be NUL terminated. */ \
1220 if (__libc_use_alloca (prec)) \
1221 string = (char *) alloca (prec); \
1222 else if ((string = (char *) malloc (prec)) == NULL) \
1228 string_malloced = 1; \
1229 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1233 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1234 if (len != (size_t) -1) \
1236 assert (__mbsinit (&mbstate)); \
1237 s2 = (const wchar_t *) string; \
1238 if (__libc_use_alloca (len + 1)) \
1239 string = (char *) alloca (len + 1); \
1240 else if ((string = (char *) malloc (len + 1)) == NULL) \
1246 string_malloced = 1; \
1247 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1251 if (len == (size_t) -1) \
1253 /* Illegal wide-character string. */ \
1259 if ((width -= len) < 0) \
1261 outstring (string, len); \
1267 outstring (string, len); \
1270 if (__builtin_expect (string_malloced, 0)) \
1276 /* Orient the stream. */
1281 /* Sanity check of arguments. */
1282 ARGCHECK (s, format);
1285 /* Check for correct orientation. */
1286 if (_IO_vtable_offset (s) == 0 &&
1287 _IO_fwide (s, sizeof (CHAR_T) == 1 ? -1 : 1)
1288 != (sizeof (CHAR_T) == 1 ? -1 : 1))
1289 /* The stream is already oriented otherwise. */
1293 if (UNBUFFERED_P (s))
1294 /* Use a helper function which will allocate a local temporary buffer
1295 for the stream and then call us again. */
1296 return buffered_vfprintf (s, format, ap);
1298 /* Initialize local variables. */
1300 grouping = (const char *) -1;
1302 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1303 since on some systems `va_list' is not an integral type. */
1304 __va_copy (ap_save, ap);
1310 #ifdef COMPILE_WPRINTF
1311 /* Find the first format specifier. */
1312 f = lead_str_end = __find_specwc ((const UCHAR_T *) format);
1314 /* Find the first format specifier. */
1315 f = lead_str_end = __find_specmb ((const UCHAR_T *) format);
1319 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
1322 /* Write the literal text before the first format. */
1323 outstring ((const UCHAR_T *) format,
1324 lead_str_end - (const UCHAR_T *) format);
1326 /* If we only have to print a simple string, return now. */
1330 /* Use the slow path in case any printf handler is registered. */
1331 if (__builtin_expect (__printf_function_table != NULL
1332 || __printf_modifier_table != NULL
1333 || __printf_va_arg_table != NULL, 0))
1336 /* Process whole format string. */
1340 # define REF(Name) &&do_##Name - &&do_form_unknown
1342 # define REF(Name) &&do_##Name
1344 #define LABEL(Name) do_##Name
1348 union printf_arg *args_value; /* This is not used here but ... */
1349 int is_negative; /* Flag for negative number. */
1352 unsigned long long int longlong;
1353 unsigned long int word;
1356 union printf_arg the_arg;
1357 CHAR_T *string; /* Pointer to argument string. */
1358 int alt = 0; /* Alternate format. */
1359 int space = 0; /* Use space prefix if no sign is needed. */
1360 int left = 0; /* Left-justify output. */
1361 int showsign = 0; /* Always begin with plus or minus sign. */
1362 int group = 0; /* Print numbers according grouping rules. */
1363 int is_long_double = 0; /* Argument is long double/ long long int. */
1364 int is_short = 0; /* Argument is short int. */
1365 int is_long = 0; /* Argument is long int. */
1366 int is_char = 0; /* Argument is promoted (unsigned) char. */
1367 int width = 0; /* Width of output; 0 means none specified. */
1368 int prec = -1; /* Precision of output; -1 means none specified. */
1369 /* This flag is set by the 'I' modifier and selects the use of the
1370 `outdigits' as determined by the current locale. */
1371 int use_outdigits = 0;
1372 UCHAR_T pad = L_(' ');/* Padding character. */
1376 workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1378 /* Get current character in format string. */
1379 JUMP (*++f, step0_jumps);
1384 JUMP (*++f, step0_jumps);
1389 JUMP (*++f, step0_jumps);
1395 JUMP (*++f, step0_jumps);
1400 JUMP (*++f, step0_jumps);
1406 JUMP (*++f, step0_jumps);
1408 /* The '\'' flag. */
1412 if (grouping == (const char *) -1)
1414 #ifdef COMPILE_WPRINTF
1415 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1416 _NL_NUMERIC_THOUSANDS_SEP_WC);
1418 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1421 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1422 if (*grouping == '\0' || *grouping == CHAR_MAX
1423 #ifdef COMPILE_WPRINTF
1424 || thousands_sep == L'\0'
1426 || *thousands_sep == '\0'
1431 JUMP (*++f, step0_jumps);
1435 JUMP (*++f, step0_jumps);
1437 /* Get width from argument. */
1438 LABEL (width_asterics):
1440 const UCHAR_T *tmp; /* Temporary value. */
1445 int pos = read_int (&tmp);
1449 __set_errno (EOVERFLOW);
1454 if (pos && *tmp == L_('$'))
1455 /* The width comes from a positional parameter. */
1458 width = va_arg (ap, int);
1460 /* Negative width means left justified. */
1468 if (__builtin_expect (width >= INT_MAX / sizeof (CHAR_T) - 32, 0))
1470 __set_errno (EOVERFLOW);
1475 if (width >= sizeof (work_buffer) / sizeof (work_buffer[0]) - 32)
1477 /* We have to use a special buffer. The "32" is just a safe
1478 bet for all the output which is not counted in the width. */
1479 size_t needed = ((size_t) width + 32) * sizeof (CHAR_T);
1480 if (__libc_use_alloca (needed))
1481 workend = (CHAR_T *) alloca (needed) + width + 32;
1484 workstart = (CHAR_T *) malloc (needed);
1485 if (workstart == NULL)
1490 workend = workstart + width + 32;
1494 JUMP (*f, step1_jumps);
1496 /* Given width in format string. */
1498 width = read_int (&f);
1500 if (__builtin_expect (width == -1
1501 || width >= INT_MAX / sizeof (CHAR_T) - 32, 0))
1503 __set_errno (EOVERFLOW);
1508 if (width >= sizeof (work_buffer) / sizeof (work_buffer[0]) - 32)
1510 /* We have to use a special buffer. The "32" is just a safe
1511 bet for all the output which is not counted in the width. */
1512 size_t needed = ((size_t) width + 32) * sizeof (CHAR_T);
1513 if (__libc_use_alloca (needed))
1514 workend = (CHAR_T *) alloca (needed) + width + 32;
1517 workstart = (CHAR_T *) malloc (needed);
1518 if (workstart == NULL)
1523 workend = workstart + width + 32;
1527 /* Oh, oh. The argument comes from a positional parameter. */
1529 JUMP (*f, step1_jumps);
1535 const UCHAR_T *tmp; /* Temporary value. */
1540 int pos = read_int (&tmp);
1544 __set_errno (EOVERFLOW);
1549 if (pos && *tmp == L_('$'))
1550 /* The precision comes from a positional parameter. */
1553 prec = va_arg (ap, int);
1555 /* If the precision is negative the precision is omitted. */
1559 else if (ISDIGIT (*f))
1561 prec = read_int (&f);
1563 /* The precision was specified in this case as an extremely
1564 large positive value. */
1567 __set_errno (EOVERFLOW);
1575 && prec > sizeof (work_buffer) / sizeof (work_buffer[0]) - 32)
1577 if (__builtin_expect (prec >= INT_MAX / sizeof (CHAR_T) - 32, 0))
1579 __set_errno (EOVERFLOW);
1583 size_t needed = ((size_t) prec + 32) * sizeof (CHAR_T);
1585 if (__libc_use_alloca (needed))
1586 workend = (CHAR_T *) alloca (needed) + prec + 32;
1589 workstart = (CHAR_T *) malloc (needed);
1590 if (workstart == NULL)
1595 workend = workstart + prec + 32;
1598 JUMP (*f, step2_jumps);
1600 /* Process 'h' modifier. There might another 'h' following. */
1603 JUMP (*++f, step3a_jumps);
1605 /* Process 'hh' modifier. */
1606 LABEL (mod_halfhalf):
1609 JUMP (*++f, step4_jumps);
1611 /* Process 'l' modifier. There might another 'l' following. */
1614 JUMP (*++f, step3b_jumps);
1616 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1617 allowed to follow. */
1618 LABEL (mod_longlong):
1621 JUMP (*++f, step4_jumps);
1624 is_long_double = sizeof (size_t) > sizeof (unsigned long int);
1625 is_long = sizeof (size_t) > sizeof (unsigned int);
1626 JUMP (*++f, step4_jumps);
1628 LABEL (mod_ptrdiff_t):
1629 is_long_double = sizeof (ptrdiff_t) > sizeof (unsigned long int);
1630 is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
1631 JUMP (*++f, step4_jumps);
1633 LABEL (mod_intmax_t):
1634 is_long_double = sizeof (intmax_t) > sizeof (unsigned long int);
1635 is_long = sizeof (intmax_t) > sizeof (unsigned int);
1636 JUMP (*++f, step4_jumps);
1638 /* Process current format. */
1641 process_arg (((struct printf_spec *) NULL));
1642 process_string_arg (((struct printf_spec *) NULL));
1644 LABEL (form_unknown):
1645 if (spec == L_('\0'))
1647 /* The format string ended before the specifier is complete. */
1648 __set_errno (EINVAL);
1653 /* If we are in the fast loop force entering the complicated
1658 /* The format is correctly handled. */
1661 if (__builtin_expect (workstart != NULL, 0))
1665 /* Look for next format specifier. */
1666 #ifdef COMPILE_WPRINTF
1667 f = __find_specwc ((end_of_spec = ++f));
1669 f = __find_specmb ((end_of_spec = ++f));
1672 /* Write the following constant string. */
1673 outstring (end_of_spec, f - end_of_spec);
1675 while (*f != L_('\0'));
1677 /* Unlock stream and return. */
1680 /* Here starts the more complex loop to handle positional parameters. */
1683 /* Array with information about the needed arguments. This has to
1684 be dynamically extensible. */
1686 /* A more or less arbitrary start value. */
1687 size_t nspecs_size = 32 * sizeof (struct printf_spec);
1688 struct printf_spec *specs = alloca (nspecs_size);
1690 /* The number of arguments the format string requests. This will
1691 determine the size of the array needed to store the argument
1694 size_t bytes_per_arg;
1695 union printf_arg *args_value;
1699 /* Positional parameters refer to arguments directly. This could
1700 also determine the maximum number of arguments. Track the
1702 size_t max_ref_arg = 0;
1704 /* Just a counter. */
1710 if (grouping == (const char *) -1)
1712 #ifdef COMPILE_WPRINTF
1713 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1714 _NL_NUMERIC_THOUSANDS_SEP_WC);
1716 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1719 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1720 if (*grouping == '\0' || *grouping == CHAR_MAX)
1724 for (f = lead_str_end; *f != L_('\0'); f = specs[nspecs++].next_fmt)
1726 if (nspecs * sizeof (*specs) >= nspecs_size)
1728 /* Extend the array of format specifiers. */
1729 struct printf_spec *old = specs;
1730 specs = extend_alloca (specs, nspecs_size, 2 * nspecs_size);
1732 /* Copy the old array's elements to the new space. */
1733 memmove (specs, old, nspecs * sizeof (*specs));
1736 /* Parse the format specifier. */
1737 #ifdef COMPILE_WPRINTF
1738 nargs += __parse_one_specwc (f, nargs, &specs[nspecs], &max_ref_arg);
1740 nargs += __parse_one_specmb (f, nargs, &specs[nspecs], &max_ref_arg);
1744 /* Determine the number of arguments the format string consumes. */
1745 nargs = MAX (nargs, max_ref_arg);
1746 /* Calculate total size needed to represent a single argument across
1747 all three argument-related arrays. */
1748 bytes_per_arg = (sizeof (*args_value) + sizeof (*args_size)
1749 + sizeof (*args_type));
1751 /* Check for potential integer overflow. */
1752 if (__builtin_expect (nargs > INT_MAX / bytes_per_arg, 0))
1754 __set_errno (EOVERFLOW);
1759 /* Allocate memory for all three argument arrays. */
1760 if (__libc_use_alloca (nargs * bytes_per_arg))
1761 args_value = alloca (nargs * bytes_per_arg);
1764 args_value = args_malloced = malloc (nargs * bytes_per_arg);
1765 if (args_value == NULL)
1772 /* Set up the remaining two arrays to each point past the end of the
1773 prior array, since space for all three has been allocated now. */
1774 args_size = &args_value[nargs].pa_int;
1775 args_type = &args_size[nargs];
1776 memset (args_type, s->_flags2 & _IO_FLAGS2_FORTIFY ? '\xff' : '\0',
1777 nargs * sizeof (*args_type));
1779 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1780 still zero after this loop, format is invalid. For now we
1781 simply use 0 as the value. */
1783 /* Fill in the types of all the arguments. */
1784 for (cnt = 0; cnt < nspecs; ++cnt)
1786 /* If the width is determined by an argument this is an int. */
1787 if (specs[cnt].width_arg != -1)
1788 args_type[specs[cnt].width_arg] = PA_INT;
1790 /* If the precision is determined by an argument this is an int. */
1791 if (specs[cnt].prec_arg != -1)
1792 args_type[specs[cnt].prec_arg] = PA_INT;
1794 switch (specs[cnt].ndata_args)
1796 case 0: /* No arguments. */
1798 case 1: /* One argument; we already have the
1800 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1801 args_size[specs[cnt].data_arg] = specs[cnt].size;
1804 /* We have more than one argument for this format spec.
1805 We must call the arginfo function again to determine
1807 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1809 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg],
1810 &args_size[specs[cnt].data_arg]);
1815 /* Now we know all the types and the order. Fill in the argument
1817 for (cnt = 0; cnt < nargs; ++cnt)
1818 switch (args_type[cnt])
1820 #define T(tag, mem, type) \
1822 args_value[cnt].mem = va_arg (ap_save, type); \
1825 T (PA_WCHAR, pa_wchar, wint_t);
1826 case PA_CHAR: /* Promoted. */
1827 case PA_INT|PA_FLAG_SHORT: /* Promoted. */
1828 #if LONG_MAX == INT_MAX
1829 case PA_INT|PA_FLAG_LONG:
1831 T (PA_INT, pa_int, int);
1832 #if LONG_MAX == LONG_LONG_MAX
1833 case PA_INT|PA_FLAG_LONG:
1835 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1836 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1839 case PA_FLOAT: /* Promoted. */
1840 T (PA_DOUBLE, pa_double, double);
1841 case PA_DOUBLE|PA_FLAG_LONG_DOUBLE:
1844 args_value[cnt].pa_double = va_arg (ap_save, double);
1845 args_type[cnt] &= ~PA_FLAG_LONG_DOUBLE;
1848 args_value[cnt].pa_long_double = va_arg (ap_save, long double);
1850 case PA_STRING: /* All pointers are the same */
1851 case PA_WSTRING: /* All pointers are the same */
1852 T (PA_POINTER, pa_pointer, void *);
1855 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1856 args_value[cnt].pa_pointer = va_arg (ap_save, void *);
1857 else if (__builtin_expect (__printf_va_arg_table != NULL, 0)
1858 && __printf_va_arg_table[args_type[cnt] - PA_LAST] != NULL)
1860 args_value[cnt].pa_user = alloca (args_size[cnt]);
1861 (*__printf_va_arg_table[args_type[cnt] - PA_LAST])
1862 (args_value[cnt].pa_user, &ap_save);
1865 args_value[cnt].pa_long_double = 0.0;
1868 /* Error case. Not all parameters appear in N$ format
1869 strings. We have no way to determine their type. */
1870 assert (s->_flags2 & _IO_FLAGS2_FORTIFY);
1871 __libc_fatal ("*** invalid %N$ use detected ***\n");
1874 /* Now walk through all format specifiers and process them. */
1875 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1879 # define REF(Name) &&do2_##Name - &&do_form_unknown
1881 # define REF(Name) &&do2_##Name
1884 #define LABEL(Name) do2_##Name
1890 unsigned long long int longlong;
1891 unsigned long int word;
1894 union printf_arg the_arg;
1895 CHAR_T *string; /* Pointer to argument string. */
1897 /* Fill variables from values in struct. */
1898 int alt = specs[nspecs_done].info.alt;
1899 int space = specs[nspecs_done].info.space;
1900 int left = specs[nspecs_done].info.left;
1901 int showsign = specs[nspecs_done].info.showsign;
1902 int group = specs[nspecs_done].info.group;
1903 int is_long_double = specs[nspecs_done].info.is_long_double;
1904 int is_short = specs[nspecs_done].info.is_short;
1905 int is_char = specs[nspecs_done].info.is_char;
1906 int is_long = specs[nspecs_done].info.is_long;
1907 int width = specs[nspecs_done].info.width;
1908 int prec = specs[nspecs_done].info.prec;
1909 int use_outdigits = specs[nspecs_done].info.i18n;
1910 char pad = specs[nspecs_done].info.pad;
1911 CHAR_T spec = specs[nspecs_done].info.spec;
1914 workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1916 /* Fill in last information. */
1917 if (specs[nspecs_done].width_arg != -1)
1919 /* Extract the field width from an argument. */
1920 specs[nspecs_done].info.width =
1921 args_value[specs[nspecs_done].width_arg].pa_int;
1923 if (specs[nspecs_done].info.width < 0)
1924 /* If the width value is negative left justification is
1925 selected and the value is taken as being positive. */
1927 specs[nspecs_done].info.width *= -1;
1928 left = specs[nspecs_done].info.left = 1;
1930 width = specs[nspecs_done].info.width;
1933 if (specs[nspecs_done].prec_arg != -1)
1935 /* Extract the precision from an argument. */
1936 specs[nspecs_done].info.prec =
1937 args_value[specs[nspecs_done].prec_arg].pa_int;
1939 if (specs[nspecs_done].info.prec < 0)
1940 /* If the precision is negative the precision is
1942 specs[nspecs_done].info.prec = -1;
1944 prec = specs[nspecs_done].info.prec;
1947 /* Maybe the buffer is too small. */
1948 if (MAX (prec, width) + 32 > (int) (sizeof (work_buffer)
1951 if (__libc_use_alloca ((MAX (prec, width) + 32)
1953 workend = ((CHAR_T *) alloca ((MAX (prec, width) + 32)
1955 + (MAX (prec, width) + 32));
1958 workstart = (CHAR_T *) malloc ((MAX (prec, width) + 32)
1960 workend = workstart + (MAX (prec, width) + 32);
1964 /* Process format specifiers. */
1967 extern printf_function **__printf_function_table;
1970 if (spec <= UCHAR_MAX
1971 && __printf_function_table != NULL
1972 && __printf_function_table[(size_t) spec] != NULL)
1974 const void **ptr = alloca (specs[nspecs_done].ndata_args
1975 * sizeof (const void *));
1977 /* Fill in an array of pointers to the argument values. */
1978 for (unsigned int i = 0; i < specs[nspecs_done].ndata_args;
1980 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1982 /* Call the function. */
1983 function_done = __printf_function_table[(size_t) spec]
1984 (s, &specs[nspecs_done].info, ptr);
1986 if (function_done != -2)
1988 /* If an error occurred we don't have information
1989 about # of chars. */
1990 if (function_done < 0)
1992 /* Function has set errno. */
1997 done_add (function_done);
2002 JUMP (spec, step4_jumps);
2004 process_arg ((&specs[nspecs_done]));
2005 process_string_arg ((&specs[nspecs_done]));
2007 LABEL (form_unknown):
2012 ptr = alloca (specs[nspecs_done].ndata_args
2013 * sizeof (const void *));
2015 /* Fill in an array of pointers to the argument values. */
2016 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
2017 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
2019 /* Call the function. */
2020 function_done = printf_unknown (s, &specs[nspecs_done].info,
2023 /* If an error occurred we don't have information about #
2025 if (function_done < 0)
2027 /* Function has set errno. */
2032 done_add (function_done);
2040 /* Write the following constant string. */
2041 outstring (specs[nspecs_done].end_of_fmt,
2042 specs[nspecs_done].next_fmt
2043 - specs[nspecs_done].end_of_fmt);
2048 free (args_malloced);
2050 /* Unlock the stream. */
2051 _IO_funlockfile (s);
2052 _IO_cleanup_region_end (0);
2057 /* Handle an unknown format specifier. This prints out a canonicalized
2058 representation of the format spec itself. */
2060 printf_unknown (FILE *s, const struct printf_info *info,
2061 const void *const *args)
2065 CHAR_T work_buffer[MAX (sizeof (info->width), sizeof (info->prec)) * 3];
2066 CHAR_T *const workend
2067 = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
2078 else if (info->space)
2082 if (info->pad == L_('0'))
2087 if (info->width != 0)
2089 w = _itoa_word (info->width, workend, 10, 0);
2094 if (info->prec != -1)
2097 w = _itoa_word (info->prec, workend, 10, 0);
2102 if (info->spec != L_('\0'))
2103 outchar (info->spec);
2109 /* Group the digits according to the grouping rules of the current locale.
2110 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
2113 group_number (CHAR_T *w, CHAR_T *rear_ptr, const char *grouping,
2114 #ifdef COMPILE_WPRINTF
2115 wchar_t thousands_sep
2117 const char *thousands_sep
2123 #ifndef COMPILE_WPRINTF
2124 int tlen = strlen (thousands_sep);
2127 /* We treat all negative values like CHAR_MAX. */
2129 if (*grouping == CHAR_MAX || *grouping <= 0)
2130 /* No grouping should be done. */
2135 /* Copy existing string so that nothing gets overwritten. */
2136 src = (CHAR_T *) alloca ((rear_ptr - w) * sizeof (CHAR_T));
2137 s = (CHAR_T *) __mempcpy (src, w,
2138 (rear_ptr - w) * sizeof (CHAR_T));
2141 /* Process all characters in the string. */
2146 if (--len == 0 && s > src)
2148 /* A new group begins. */
2149 #ifdef COMPILE_WPRINTF
2150 *--w = thousands_sep;
2154 *--w = thousands_sep[--cnt];
2158 if (*grouping == CHAR_MAX
2164 /* No further grouping to be done.
2165 Copy the rest of the number. */
2171 else if (*grouping != '\0')
2172 /* The previous grouping repeats ad infinitum. */
2181 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2184 struct _IO_FILE_plus _f;
2185 #ifdef COMPILE_WPRINTF
2186 struct _IO_wide_data _wide_data;
2188 _IO_FILE *_put_stream;
2189 #ifdef _IO_MTSAFE_IO
2195 _IO_helper_overflow (_IO_FILE *s, int c)
2197 _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
2198 #ifdef COMPILE_WPRINTF
2199 int used = s->_wide_data->_IO_write_ptr - s->_wide_data->_IO_write_base;
2202 _IO_size_t written = _IO_sputn (target, s->_wide_data->_IO_write_base,
2204 if (written == 0 || written == WEOF)
2206 __wmemmove (s->_wide_data->_IO_write_base,
2207 s->_wide_data->_IO_write_base + written,
2209 s->_wide_data->_IO_write_ptr -= written;
2212 int used = s->_IO_write_ptr - s->_IO_write_base;
2215 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
2216 if (written == 0 || written == EOF)
2218 memmove (s->_IO_write_base, s->_IO_write_base + written,
2220 s->_IO_write_ptr -= written;
2226 #ifdef COMPILE_WPRINTF
2227 static const struct _IO_jump_t _IO_helper_jumps =
2230 JUMP_INIT (finish, _IO_wdefault_finish),
2231 JUMP_INIT (overflow, _IO_helper_overflow),
2232 JUMP_INIT (underflow, _IO_default_underflow),
2233 JUMP_INIT (uflow, _IO_default_uflow),
2234 JUMP_INIT (pbackfail, (_IO_pbackfail_t) _IO_wdefault_pbackfail),
2235 JUMP_INIT (xsputn, _IO_wdefault_xsputn),
2236 JUMP_INIT (xsgetn, _IO_wdefault_xsgetn),
2237 JUMP_INIT (seekoff, _IO_default_seekoff),
2238 JUMP_INIT (seekpos, _IO_default_seekpos),
2239 JUMP_INIT (setbuf, _IO_default_setbuf),
2240 JUMP_INIT (sync, _IO_default_sync),
2241 JUMP_INIT (doallocate, _IO_wdefault_doallocate),
2242 JUMP_INIT (read, _IO_default_read),
2243 JUMP_INIT (write, _IO_default_write),
2244 JUMP_INIT (seek, _IO_default_seek),
2245 JUMP_INIT (close, _IO_default_close),
2246 JUMP_INIT (stat, _IO_default_stat)
2249 static const struct _IO_jump_t _IO_helper_jumps =
2252 JUMP_INIT (finish, _IO_default_finish),
2253 JUMP_INIT (overflow, _IO_helper_overflow),
2254 JUMP_INIT (underflow, _IO_default_underflow),
2255 JUMP_INIT (uflow, _IO_default_uflow),
2256 JUMP_INIT (pbackfail, _IO_default_pbackfail),
2257 JUMP_INIT (xsputn, _IO_default_xsputn),
2258 JUMP_INIT (xsgetn, _IO_default_xsgetn),
2259 JUMP_INIT (seekoff, _IO_default_seekoff),
2260 JUMP_INIT (seekpos, _IO_default_seekpos),
2261 JUMP_INIT (setbuf, _IO_default_setbuf),
2262 JUMP_INIT (sync, _IO_default_sync),
2263 JUMP_INIT (doallocate, _IO_default_doallocate),
2264 JUMP_INIT (read, _IO_default_read),
2265 JUMP_INIT (write, _IO_default_write),
2266 JUMP_INIT (seek, _IO_default_seek),
2267 JUMP_INIT (close, _IO_default_close),
2268 JUMP_INIT (stat, _IO_default_stat)
2274 buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format,
2277 CHAR_T buf[_IO_BUFSIZ];
2278 struct helper_file helper;
2279 register _IO_FILE *hp = (_IO_FILE *) &helper._f;
2280 int result, to_flush;
2282 /* Orient the stream. */
2287 /* Initialize helper. */
2288 helper._put_stream = s;
2289 #ifdef COMPILE_WPRINTF
2290 hp->_wide_data = &helper._wide_data;
2291 _IO_wsetp (hp, buf, buf + sizeof buf / sizeof (CHAR_T));
2294 _IO_setp (hp, buf, buf + sizeof buf);
2297 hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS|_IO_USER_LOCK;
2298 #if _IO_JUMPS_OFFSET
2299 hp->_vtable_offset = 0;
2301 #ifdef _IO_MTSAFE_IO
2304 hp->_flags2 = s->_flags2;
2305 _IO_JUMPS (&helper._f) = (struct _IO_jump_t *) &_IO_helper_jumps;
2307 /* Now print to helper instead. */
2308 #ifndef COMPILE_WPRINTF
2309 result = _IO_vfprintf (hp, format, args);
2311 result = vfprintf (hp, format, args);
2315 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, s);
2318 /* Now flush anything from the helper to the S. */
2319 #ifdef COMPILE_WPRINTF
2320 if ((to_flush = (hp->_wide_data->_IO_write_ptr
2321 - hp->_wide_data->_IO_write_base)) > 0)
2323 if ((int) _IO_sputn (s, hp->_wide_data->_IO_write_base, to_flush)
2328 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
2330 if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
2335 /* Unlock the stream. */
2336 _IO_funlockfile (s);
2337 __libc_cleanup_region_end (0);
2343 #ifdef COMPILE_WPRINTF
2344 strong_alias (_IO_vfwprintf, __vfwprintf);
2345 ldbl_weak_alias (_IO_vfwprintf, vfwprintf);
2347 ldbl_strong_alias (_IO_vfprintf_internal, vfprintf);
2348 ldbl_hidden_def (_IO_vfprintf_internal, vfprintf)
2349 ldbl_strong_alias (_IO_vfprintf_internal, _IO_vfprintf);
2350 ldbl_hidden_def (_IO_vfprintf_internal, _IO_vfprintf)