1 /* Copyright (C) 1991-2002, 2003, 2004, 2005, 2006
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
29 #include <bits/libc-lock.h>
30 #include <sys/param.h>
32 #include <locale/localeinfo.h>
35 /* This code is shared between the standard stdio implementation found
36 in GNU C library and the libio implementation originally found in
39 Beside this it is also shared between the normal and wide character
40 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
46 #define va_list _IO_va_list
48 #define BUFSIZ _IO_BUFSIZ
49 #define ARGCHECK(S, Format) \
52 /* Check file argument for consistence. */ \
54 if (S->_flags & _IO_NO_WRITES) \
56 __set_errno (EBADF); \
65 #define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
67 #ifndef COMPILE_WPRINTF
68 # define vfprintf _IO_vfprintf_internal
70 # define UCHAR_T unsigned char
73 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
74 # define STR_LEN(Str) strlen (Str)
76 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
77 # define PAD(Padchar) \
79 done += INTUSE(_IO_padn) (s, (Padchar), width)
80 # define PUTC(C, F) _IO_putc_unlocked (C, F)
81 # define ORIENT if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
84 # define vfprintf _IO_vfwprintf
85 # define CHAR_T wchar_t
86 /* This is a hack!!! There should be a type uwchar_t. */
87 # define UCHAR_T unsigned int /* uwchar_t */
89 # define L_(Str) L##Str
90 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
91 # define STR_LEN(Str) __wcslen (Str)
95 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
96 # define PAD(Padchar) \
98 done += _IO_wpadn (s, (Padchar), width)
99 # define PUTC(C, F) _IO_putwc_unlocked (C, F)
100 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
102 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
103 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
108 #include "_i18n_number.h"
110 /* Include the shared code for parsing the format string. */
111 #include "printf-parse.h"
114 #define outchar(Ch) \
117 register const INT_T outc = (Ch); \
118 if (PUTC (outc, s) == EOF) \
128 #define outstring(String, Len) \
131 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
140 /* For handling long_double and longlong we use the same flag. If
141 `long' and `long long' are effectively the same type define it to
143 #if LONG_MAX == LONG_LONG_MAX
144 # define is_longlong 0
146 # define is_longlong is_long_double
149 /* If `long' and `int' is effectively the same type we don't have to
150 handle `long separately. */
151 #if INT_MAX == LONG_MAX
152 # define is_long_num 0
154 # define is_long_num is_long
158 /* Global variables. */
159 static const CHAR_T null
[] = L_("(null)");
162 /* Helper function to provide temporary buffering for unbuffered streams. */
163 static int buffered_vfprintf (FILE *stream
, const CHAR_T
*fmt
, va_list)
164 __THROW
__attribute__ ((noinline
)) internal_function
;
166 /* Handle unknown format specifier. */
167 static int printf_unknown (FILE *, const struct printf_info
*,
168 const void *const *) __THROW
;
170 /* Group digits of number string. */
171 #ifdef COMPILE_WPRINTF
172 static CHAR_T
*group_number (CHAR_T
*, CHAR_T
*, const char *, wchar_t)
173 __THROW internal_function
;
175 static CHAR_T
*group_number (CHAR_T
*, CHAR_T
*, const char *, const char *)
176 __THROW internal_function
;
180 /* The function itself. */
182 vfprintf (FILE *s
, const CHAR_T
*format
, va_list ap
)
184 /* The character used as thousands separator. */
185 #ifdef COMPILE_WPRINTF
186 wchar_t thousands_sep
= L
'\0';
188 const char *thousands_sep
= NULL
;
191 /* The string describing the size of groups of digits. */
192 const char *grouping
;
194 /* Place to accumulate the result. */
197 /* Current character in format string. */
200 /* End of leading constant string. */
201 const UCHAR_T
*lead_str_end
;
203 /* Points to next format specifier. */
204 const UCHAR_T
*end_of_spec
;
206 /* Buffer intermediate results. */
207 CHAR_T work_buffer
[1000];
208 CHAR_T
*workstart
= NULL
;
211 /* State for restartable multibyte character handling functions. */
212 #ifndef COMPILE_WPRINTF
216 /* We have to save the original argument pointer. */
219 /* Count number of specifiers we already processed. */
222 /* For the %m format we may need the current `errno' value. */
223 int save_errno
= errno
;
225 /* 1 if format is in read-only memory, -1 if it is in writable memory,
227 int readonly_format
= 0;
229 /* This table maps a character into a number representing a
230 class. In each step there is a destination label for each
232 static const int jump_table
[] =
234 /* ' ' */ 1, 0, 0, /* '#' */ 4,
235 0, /* '%' */ 14, 0, /* '\''*/ 6,
236 0, 0, /* '*' */ 7, /* '+' */ 2,
237 0, /* '-' */ 3, /* '.' */ 9, 0,
238 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
239 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
240 /* '8' */ 8, /* '9' */ 8, 0, 0,
242 0, /* 'A' */ 26, 0, /* 'C' */ 25,
243 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
244 0, /* 'I' */ 29, 0, 0,
245 /* 'L' */ 12, 0, 0, 0,
246 0, 0, 0, /* 'S' */ 21,
248 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
250 0, /* 'a' */ 26, 0, /* 'c' */ 20,
251 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
252 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
253 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
254 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
255 /* 't' */ 27, /* 'u' */ 16, 0, 0,
256 /* 'x' */ 18, 0, /* 'z' */ 13
259 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
260 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
262 /* 'int' is enough and it saves some space on 64 bit systems. */
263 # define JUMP_TABLE_TYPE const int
264 # define JUMP(ChExpr, table) \
268 void *__unbounded ptr; \
270 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
271 : table[CHAR_CLASS (spec)]; \
272 ptr = &&do_form_unknown + offset; \
277 # define JUMP_TABLE_TYPE const void *const
278 # define JUMP(ChExpr, table) \
281 const void *__unbounded ptr; \
283 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
284 : table[CHAR_CLASS (spec)]; \
290 #define STEP0_3_TABLE \
291 /* Step 0: at the beginning. */ \
292 static JUMP_TABLE_TYPE step0_jumps[30] = \
294 REF (form_unknown), \
295 REF (flag_space), /* for ' ' */ \
296 REF (flag_plus), /* for '+' */ \
297 REF (flag_minus), /* for '-' */ \
298 REF (flag_hash), /* for '<hash>' */ \
299 REF (flag_zero), /* for '0' */ \
300 REF (flag_quote), /* for '\'' */ \
301 REF (width_asterics), /* for '*' */ \
302 REF (width), /* for '1'...'9' */ \
303 REF (precision), /* for '.' */ \
304 REF (mod_half), /* for 'h' */ \
305 REF (mod_long), /* for 'l' */ \
306 REF (mod_longlong), /* for 'L', 'q' */ \
307 REF (mod_size_t), /* for 'z', 'Z' */ \
308 REF (form_percent), /* for '%' */ \
309 REF (form_integer), /* for 'd', 'i' */ \
310 REF (form_unsigned), /* for 'u' */ \
311 REF (form_octal), /* for 'o' */ \
312 REF (form_hexa), /* for 'X', 'x' */ \
313 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
314 REF (form_character), /* for 'c' */ \
315 REF (form_string), /* for 's', 'S' */ \
316 REF (form_pointer), /* for 'p' */ \
317 REF (form_number), /* for 'n' */ \
318 REF (form_strerror), /* for 'm' */ \
319 REF (form_wcharacter), /* for 'C' */ \
320 REF (form_floathex), /* for 'A', 'a' */ \
321 REF (mod_ptrdiff_t), /* for 't' */ \
322 REF (mod_intmax_t), /* for 'j' */ \
323 REF (flag_i18n), /* for 'I' */ \
325 /* Step 1: after processing width. */ \
326 static JUMP_TABLE_TYPE step1_jumps[30] = \
328 REF (form_unknown), \
329 REF (form_unknown), /* for ' ' */ \
330 REF (form_unknown), /* for '+' */ \
331 REF (form_unknown), /* for '-' */ \
332 REF (form_unknown), /* for '<hash>' */ \
333 REF (form_unknown), /* for '0' */ \
334 REF (form_unknown), /* for '\'' */ \
335 REF (form_unknown), /* for '*' */ \
336 REF (form_unknown), /* for '1'...'9' */ \
337 REF (precision), /* for '.' */ \
338 REF (mod_half), /* for 'h' */ \
339 REF (mod_long), /* for 'l' */ \
340 REF (mod_longlong), /* for 'L', 'q' */ \
341 REF (mod_size_t), /* for 'z', 'Z' */ \
342 REF (form_percent), /* for '%' */ \
343 REF (form_integer), /* for 'd', 'i' */ \
344 REF (form_unsigned), /* for 'u' */ \
345 REF (form_octal), /* for 'o' */ \
346 REF (form_hexa), /* for 'X', 'x' */ \
347 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
348 REF (form_character), /* for 'c' */ \
349 REF (form_string), /* for 's', 'S' */ \
350 REF (form_pointer), /* for 'p' */ \
351 REF (form_number), /* for 'n' */ \
352 REF (form_strerror), /* for 'm' */ \
353 REF (form_wcharacter), /* for 'C' */ \
354 REF (form_floathex), /* for 'A', 'a' */ \
355 REF (mod_ptrdiff_t), /* for 't' */ \
356 REF (mod_intmax_t), /* for 'j' */ \
357 REF (form_unknown) /* for 'I' */ \
359 /* Step 2: after processing precision. */ \
360 static JUMP_TABLE_TYPE step2_jumps[30] = \
362 REF (form_unknown), \
363 REF (form_unknown), /* for ' ' */ \
364 REF (form_unknown), /* for '+' */ \
365 REF (form_unknown), /* for '-' */ \
366 REF (form_unknown), /* for '<hash>' */ \
367 REF (form_unknown), /* for '0' */ \
368 REF (form_unknown), /* for '\'' */ \
369 REF (form_unknown), /* for '*' */ \
370 REF (form_unknown), /* for '1'...'9' */ \
371 REF (form_unknown), /* for '.' */ \
372 REF (mod_half), /* for 'h' */ \
373 REF (mod_long), /* for 'l' */ \
374 REF (mod_longlong), /* for 'L', 'q' */ \
375 REF (mod_size_t), /* for 'z', 'Z' */ \
376 REF (form_percent), /* for '%' */ \
377 REF (form_integer), /* for 'd', 'i' */ \
378 REF (form_unsigned), /* for 'u' */ \
379 REF (form_octal), /* for 'o' */ \
380 REF (form_hexa), /* for 'X', 'x' */ \
381 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
382 REF (form_character), /* for 'c' */ \
383 REF (form_string), /* for 's', 'S' */ \
384 REF (form_pointer), /* for 'p' */ \
385 REF (form_number), /* for 'n' */ \
386 REF (form_strerror), /* for 'm' */ \
387 REF (form_wcharacter), /* for 'C' */ \
388 REF (form_floathex), /* for 'A', 'a' */ \
389 REF (mod_ptrdiff_t), /* for 't' */ \
390 REF (mod_intmax_t), /* for 'j' */ \
391 REF (form_unknown) /* for 'I' */ \
393 /* Step 3a: after processing first 'h' modifier. */ \
394 static JUMP_TABLE_TYPE step3a_jumps[30] = \
396 REF (form_unknown), \
397 REF (form_unknown), /* for ' ' */ \
398 REF (form_unknown), /* for '+' */ \
399 REF (form_unknown), /* for '-' */ \
400 REF (form_unknown), /* for '<hash>' */ \
401 REF (form_unknown), /* for '0' */ \
402 REF (form_unknown), /* for '\'' */ \
403 REF (form_unknown), /* for '*' */ \
404 REF (form_unknown), /* for '1'...'9' */ \
405 REF (form_unknown), /* for '.' */ \
406 REF (mod_halfhalf), /* for 'h' */ \
407 REF (form_unknown), /* for 'l' */ \
408 REF (form_unknown), /* for 'L', 'q' */ \
409 REF (form_unknown), /* for 'z', 'Z' */ \
410 REF (form_percent), /* for '%' */ \
411 REF (form_integer), /* for 'd', 'i' */ \
412 REF (form_unsigned), /* for 'u' */ \
413 REF (form_octal), /* for 'o' */ \
414 REF (form_hexa), /* for 'X', 'x' */ \
415 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
416 REF (form_unknown), /* for 'c' */ \
417 REF (form_unknown), /* for 's', 'S' */ \
418 REF (form_unknown), /* for 'p' */ \
419 REF (form_number), /* for 'n' */ \
420 REF (form_unknown), /* for 'm' */ \
421 REF (form_unknown), /* for 'C' */ \
422 REF (form_unknown), /* for 'A', 'a' */ \
423 REF (form_unknown), /* for 't' */ \
424 REF (form_unknown), /* for 'j' */ \
425 REF (form_unknown) /* for 'I' */ \
427 /* Step 3b: after processing first 'l' modifier. */ \
428 static JUMP_TABLE_TYPE step3b_jumps[30] = \
430 REF (form_unknown), \
431 REF (form_unknown), /* for ' ' */ \
432 REF (form_unknown), /* for '+' */ \
433 REF (form_unknown), /* for '-' */ \
434 REF (form_unknown), /* for '<hash>' */ \
435 REF (form_unknown), /* for '0' */ \
436 REF (form_unknown), /* for '\'' */ \
437 REF (form_unknown), /* for '*' */ \
438 REF (form_unknown), /* for '1'...'9' */ \
439 REF (form_unknown), /* for '.' */ \
440 REF (form_unknown), /* for 'h' */ \
441 REF (mod_longlong), /* for 'l' */ \
442 REF (form_unknown), /* for 'L', 'q' */ \
443 REF (form_unknown), /* for 'z', 'Z' */ \
444 REF (form_percent), /* for '%' */ \
445 REF (form_integer), /* for 'd', 'i' */ \
446 REF (form_unsigned), /* for 'u' */ \
447 REF (form_octal), /* for 'o' */ \
448 REF (form_hexa), /* for 'X', 'x' */ \
449 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
450 REF (form_character), /* for 'c' */ \
451 REF (form_string), /* for 's', 'S' */ \
452 REF (form_pointer), /* for 'p' */ \
453 REF (form_number), /* for 'n' */ \
454 REF (form_strerror), /* for 'm' */ \
455 REF (form_wcharacter), /* for 'C' */ \
456 REF (form_floathex), /* for 'A', 'a' */ \
457 REF (form_unknown), /* for 't' */ \
458 REF (form_unknown), /* for 'j' */ \
459 REF (form_unknown) /* for 'I' */ \
462 #define STEP4_TABLE \
463 /* Step 4: processing format specifier. */ \
464 static JUMP_TABLE_TYPE step4_jumps[30] = \
466 REF (form_unknown), \
467 REF (form_unknown), /* for ' ' */ \
468 REF (form_unknown), /* for '+' */ \
469 REF (form_unknown), /* for '-' */ \
470 REF (form_unknown), /* for '<hash>' */ \
471 REF (form_unknown), /* for '0' */ \
472 REF (form_unknown), /* for '\'' */ \
473 REF (form_unknown), /* for '*' */ \
474 REF (form_unknown), /* for '1'...'9' */ \
475 REF (form_unknown), /* for '.' */ \
476 REF (form_unknown), /* for 'h' */ \
477 REF (form_unknown), /* for 'l' */ \
478 REF (form_unknown), /* for 'L', 'q' */ \
479 REF (form_unknown), /* for 'z', 'Z' */ \
480 REF (form_percent), /* for '%' */ \
481 REF (form_integer), /* for 'd', 'i' */ \
482 REF (form_unsigned), /* for 'u' */ \
483 REF (form_octal), /* for 'o' */ \
484 REF (form_hexa), /* for 'X', 'x' */ \
485 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
486 REF (form_character), /* for 'c' */ \
487 REF (form_string), /* for 's', 'S' */ \
488 REF (form_pointer), /* for 'p' */ \
489 REF (form_number), /* for 'n' */ \
490 REF (form_strerror), /* for 'm' */ \
491 REF (form_wcharacter), /* for 'C' */ \
492 REF (form_floathex), /* for 'A', 'a' */ \
493 REF (form_unknown), /* for 't' */ \
494 REF (form_unknown), /* for 'j' */ \
495 REF (form_unknown) /* for 'I' */ \
499 #define process_arg(fspec) \
500 /* Start real work. We know about all flags and modifiers and \
501 now process the wanted format specifier. */ \
502 LABEL (form_percent): \
503 /* Write a literal "%". */ \
507 LABEL (form_integer): \
508 /* Signed decimal integer. */ \
513 long long int signed_number; \
516 signed_number = va_arg (ap, long long int); \
518 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
520 is_negative = signed_number < 0; \
521 number.longlong = is_negative ? (- signed_number) : signed_number; \
523 goto LABEL (longlong_number); \
527 long int signed_number; \
532 signed_number = va_arg (ap, long int); \
534 signed_number = (signed char) va_arg (ap, unsigned int); \
535 else if (!is_short) \
536 signed_number = va_arg (ap, int); \
538 signed_number = (short int) va_arg (ap, unsigned int); \
542 signed_number = args_value[fspec->data_arg].pa_long_int; \
544 signed_number = (signed char) \
545 args_value[fspec->data_arg].pa_u_int; \
546 else if (!is_short) \
547 signed_number = args_value[fspec->data_arg].pa_int; \
549 signed_number = (short int) \
550 args_value[fspec->data_arg].pa_u_int; \
552 is_negative = signed_number < 0; \
553 number.word = is_negative ? (- signed_number) : signed_number; \
555 goto LABEL (number); \
559 LABEL (form_unsigned): \
560 /* Unsigned decimal integer. */ \
562 goto LABEL (unsigned_number); \
565 LABEL (form_octal): \
566 /* Unsigned octal integer. */ \
568 goto LABEL (unsigned_number); \
572 /* Unsigned hexadecimal integer. */ \
575 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
577 /* ISO specifies the `+' and ` ' flags only for signed \
586 number.longlong = va_arg (ap, unsigned long long int); \
588 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
590 LABEL (longlong_number): \
592 /* Supply a default precision if none was given. */ \
595 /* We have to take care for the '0' flag. If a precision \
596 is given it must be ignored. */ \
599 /* If the precision is 0 and the number is 0 nothing has to \
600 be written for the number, except for the 'o' format in \
602 if (prec == 0 && number.longlong == 0) \
605 if (base == 8 && alt) \
606 *--string = L_('0'); \
610 /* Put the number in WORK. */ \
611 string = _itoa (number.longlong, workend, base, \
613 if (group && grouping) \
614 string = group_number (string, workend, grouping, \
617 if (use_outdigits && base == 10) \
618 string = _i18n_number_rewrite (string, workend); \
620 /* Simplify further test for num != 0. */ \
621 number.word = number.longlong != 0; \
628 number.word = va_arg (ap, unsigned long int); \
630 number.word = (unsigned char) va_arg (ap, unsigned int); \
631 else if (!is_short) \
632 number.word = va_arg (ap, unsigned int); \
634 number.word = (unsigned short int) va_arg (ap, unsigned int); \
638 number.word = args_value[fspec->data_arg].pa_u_long_int; \
640 number.word = (unsigned char) \
641 args_value[fspec->data_arg].pa_u_int; \
642 else if (!is_short) \
643 number.word = args_value[fspec->data_arg].pa_u_int; \
645 number.word = (unsigned short int) \
646 args_value[fspec->data_arg].pa_u_int; \
650 /* Supply a default precision if none was given. */ \
653 /* We have to take care for the '0' flag. If a precision \
654 is given it must be ignored. */ \
657 /* If the precision is 0 and the number is 0 nothing has to \
658 be written for the number, except for the 'o' format in \
660 if (prec == 0 && number.word == 0) \
663 if (base == 8 && alt) \
664 *--string = L_('0'); \
668 /* Put the number in WORK. */ \
669 string = _itoa_word (number.word, workend, base, \
671 if (group && grouping) \
672 string = group_number (string, workend, grouping, \
675 if (use_outdigits && base == 10) \
676 string = _i18n_number_rewrite (string, workend); \
680 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
681 /* Add octal marker. */ \
682 *--string = L_('0'); \
684 prec = MAX (0, prec - (workend - string)); \
688 width -= workend - string + prec; \
690 if (number.word != 0 && alt && base == 16) \
691 /* Account for 0X hex marker. */ \
694 if (is_negative || showsign || space) \
697 if (pad == L_(' ')) \
710 if (number.word != 0 && alt && base == 16) \
719 outstring (string, workend - string); \
741 if (number.word != 0 && alt && base == 16) \
748 width -= workend - string + prec; \
758 outstring (string, workend - string); \
764 LABEL (form_float): \
766 /* Floating-point number. This is handled by printf_fp.c. */ \
773 is_long_double = 0; \
775 struct printf_info info = { .prec = prec, \
778 .is_long_double = is_long_double, \
779 .is_short = is_short, \
780 .is_long = is_long, \
784 .showsign = showsign, \
788 .i18n = use_outdigits, \
789 .wide = sizeof (CHAR_T) != 1 }; \
791 if (is_long_double) \
792 the_arg.pa_long_double = va_arg (ap, long double); \
794 the_arg.pa_double = va_arg (ap, double); \
795 ptr = (const void *) &the_arg; \
797 function_done = __printf_fp (s, &info, &ptr); \
801 ptr = (const void *) &args_value[fspec->data_arg]; \
804 fspec->data_arg_type = PA_DOUBLE; \
805 fspec->info.is_long_double = 0; \
808 function_done = __printf_fp (s, &fspec->info, &ptr); \
811 if (function_done < 0) \
813 /* Error in print handler. */ \
818 done += function_done; \
822 LABEL (form_floathex): \
824 /* Floating point number printed as hexadecimal number. */ \
831 is_long_double = 0; \
833 struct printf_info info = { .prec = prec, \
836 .is_long_double = is_long_double, \
837 .is_short = is_short, \
838 .is_long = is_long, \
842 .showsign = showsign, \
846 .wide = sizeof (CHAR_T) != 1 }; \
848 if (is_long_double) \
849 the_arg.pa_long_double = va_arg (ap, long double); \
851 the_arg.pa_double = va_arg (ap, double); \
852 ptr = (const void *) &the_arg; \
854 function_done = __printf_fphex (s, &info, &ptr); \
858 ptr = (const void *) &args_value[fspec->data_arg]; \
860 fspec->info.is_long_double = 0; \
862 function_done = __printf_fphex (s, &fspec->info, &ptr); \
865 if (function_done < 0) \
867 /* Error in print handler. */ \
872 done += function_done; \
876 LABEL (form_pointer): \
877 /* Generic pointer. */ \
881 ptr = va_arg (ap, void *); \
883 ptr = args_value[fspec->data_arg].pa_pointer; \
886 /* If the pointer is not NULL, write it as a %#x spec. */ \
888 number.word = (unsigned long int) ptr; \
893 goto LABEL (number); \
897 /* Write "(nil)" for a nil pointer. */ \
898 string = (CHAR_T *) L_("(nil)"); \
899 /* Make sure the full string "(nil)" is printed. */ \
902 is_long = 0; /* This is no wide-char string. */ \
903 goto LABEL (print_string); \
908 LABEL (form_number): \
909 if (s->_flags2 & _IO_FLAGS2_FORTIFY) \
911 if (! readonly_format) \
913 extern int __readonly_area (const void *, size_t) \
916 = __readonly_area (format, ((STR_LEN (format) + 1) \
917 * sizeof (CHAR_T))); \
919 if (readonly_format < 0) \
920 __libc_fatal ("*** %n in writable segment detected ***\n"); \
922 /* Answer the count of characters written. */ \
926 *(long long int *) va_arg (ap, void *) = done; \
927 else if (is_long_num) \
928 *(long int *) va_arg (ap, void *) = done; \
930 *(char *) va_arg (ap, void *) = done; \
931 else if (!is_short) \
932 *(int *) va_arg (ap, void *) = done; \
934 *(short int *) va_arg (ap, void *) = done; \
938 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
939 else if (is_long_num) \
940 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
942 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
943 else if (!is_short) \
944 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
946 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
949 LABEL (form_strerror): \
950 /* Print description of error ERRNO. */ \
952 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
953 sizeof work_buffer); \
954 is_long = 0; /* This is no wide-char string. */ \
955 goto LABEL (print_string)
957 #ifdef COMPILE_WPRINTF
958 # define process_string_arg(fspec) \
959 LABEL (form_character): \
962 goto LABEL (form_wcharacter); \
963 --width; /* Account for the character itself. */ \
967 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
969 outchar (__btowc ((unsigned char) \
970 args_value[fspec->data_arg].pa_int)); \
975 LABEL (form_wcharacter): \
977 /* Wide character. */ \
982 outchar (va_arg (ap, wchar_t)); \
984 outchar (args_value[fspec->data_arg].pa_wchar); \
990 LABEL (form_string): \
993 int string_malloced; \
995 /* The string argument could in fact be `char *' or `wchar_t *'. \
996 But this should not make a difference here. */ \
998 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
1000 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
1002 /* Entry point for printing other strings. */ \
1003 LABEL (print_string): \
1005 string_malloced = 0; \
1006 if (string == NULL) \
1008 /* Write "(null)" if there's space. */ \
1010 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
1012 string = (CHAR_T *) null; \
1013 len = (sizeof (null) / sizeof (null[0])) - 1; \
1017 string = (CHAR_T *) L""; \
1021 else if (!is_long && spec != L_('S')) \
1023 /* This is complicated. We have to transform the multibyte \
1024 string into a wide character string. */ \
1025 const char *mbs = (const char *) string; \
1026 mbstate_t mbstate; \
1028 len = prec != -1 ? (size_t) prec : strlen (mbs); \
1030 /* Allocate dynamically an array which definitely is long \
1031 enough for the wide character version. */ \
1032 if (__libc_use_alloca (len * sizeof (wchar_t))) \
1033 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1034 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1041 string_malloced = 1; \
1043 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1044 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1045 if (len == (size_t) -1) \
1047 /* Illegal multibyte character. */ \
1055 /* Search for the end of the string, but don't search past \
1056 the length specified by the precision. */ \
1057 len = __wcsnlen (string, prec); \
1059 len = __wcslen (string); \
1062 if ((width -= len) < 0) \
1064 outstring (string, len); \
1070 outstring (string, len); \
1073 if (__builtin_expect (string_malloced, 0)) \
1078 # define process_string_arg(fspec) \
1079 LABEL (form_character): \
1082 goto LABEL (form_wcharacter); \
1083 --width; /* Account for the character itself. */ \
1086 if (fspec == NULL) \
1087 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1089 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1094 LABEL (form_wcharacter): \
1096 /* Wide character. */ \
1097 char buf[MB_CUR_MAX]; \
1098 mbstate_t mbstate; \
1101 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1102 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1103 : args_value[fspec->data_arg].pa_wchar), \
1105 if (len == (size_t) -1) \
1107 /* Something went wron gduring the conversion. Bail out. */ \
1114 outstring (buf, len); \
1120 LABEL (form_string): \
1123 int string_malloced; \
1125 /* The string argument could in fact be `char *' or `wchar_t *'. \
1126 But this should not make a difference here. */ \
1127 if (fspec == NULL) \
1128 string = (char *) va_arg (ap, const char *); \
1130 string = (char *) args_value[fspec->data_arg].pa_string; \
1132 /* Entry point for printing other strings. */ \
1133 LABEL (print_string): \
1135 string_malloced = 0; \
1136 if (string == NULL) \
1138 /* Write "(null)" if there's space. */ \
1139 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1141 string = (char *) null; \
1142 len = sizeof (null) - 1; \
1146 string = (char *) ""; \
1150 else if (!is_long && spec != L_('S')) \
1154 /* Search for the end of the string, but don't search past \
1155 the length (in bytes) specified by the precision. Also \
1156 don't use incomplete characters. */ \
1157 if (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MB_CUR_MAX) == 1) \
1158 len = __strnlen (string, prec); \
1161 /* In case we have a multibyte character set the \
1162 situation is more compilcated. We must not copy \
1163 bytes at the end which form an incomplete character. */\
1164 wchar_t ignore[prec]; \
1165 const char *str2 = string; \
1168 memset (&ps, '\0', sizeof (ps)); \
1169 if (__mbsnrtowcs (ignore, &str2, prec, prec, &ps) \
1176 len = strlen (string); \
1178 len = str2 - string - (ps.__count & 7); \
1182 len = strlen (string); \
1186 const wchar_t *s2 = (const wchar_t *) string; \
1187 mbstate_t mbstate; \
1189 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1193 /* The string `s2' might not be NUL terminated. */ \
1194 if (__libc_use_alloca (prec)) \
1195 string = (char *) alloca (prec); \
1196 else if ((string = (char *) malloc (prec)) == NULL) \
1202 string_malloced = 1; \
1203 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1207 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1208 if (len != (size_t) -1) \
1210 assert (__mbsinit (&mbstate)); \
1211 s2 = (const wchar_t *) string; \
1212 if (__libc_use_alloca (len + 1)) \
1213 string = (char *) alloca (len + 1); \
1214 else if ((string = (char *) malloc (len + 1)) == NULL) \
1220 string_malloced = 1; \
1221 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1225 if (len == (size_t) -1) \
1227 /* Illegal wide-character string. */ \
1233 if ((width -= len) < 0) \
1235 outstring (string, len); \
1241 outstring (string, len); \
1244 if (__builtin_expect (string_malloced, 0)) \
1250 /* Orient the stream. */
1255 /* Sanity check of arguments. */
1256 ARGCHECK (s
, format
);
1259 /* Check for correct orientation. */
1260 if (_IO_vtable_offset (s
) == 0 &&
1261 _IO_fwide (s
, sizeof (CHAR_T
) == 1 ? -1 : 1)
1262 != (sizeof (CHAR_T
) == 1 ? -1 : 1))
1263 /* The stream is already oriented otherwise. */
1267 if (UNBUFFERED_P (s
))
1268 /* Use a helper function which will allocate a local temporary buffer
1269 for the stream and then call us again. */
1270 return buffered_vfprintf (s
, format
, ap
);
1272 /* Initialize local variables. */
1274 grouping
= (const char *) -1;
1276 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1277 since on some systems `va_list' is not an integral type. */
1278 __va_copy (ap_save
, ap
);
1284 #ifdef COMPILE_WPRINTF
1285 /* Find the first format specifier. */
1286 f
= lead_str_end
= __find_specwc ((const UCHAR_T
*) format
);
1288 /* Put state for processing format string in initial state. */
1289 memset (&mbstate
, '\0', sizeof (mbstate_t));
1291 /* Find the first format specifier. */
1292 f
= lead_str_end
= __find_specmb (format
, &mbstate
);
1296 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile
, s
);
1299 /* Write the literal text before the first format. */
1300 outstring ((const UCHAR_T
*) format
,
1301 lead_str_end
- (const UCHAR_T
*) format
);
1303 /* If we only have to print a simple string, return now. */
1307 /* Process whole format string. */
1311 # define REF(Name) &&do_##Name - &&do_form_unknown
1313 # define REF(Name) &&do_##Name
1315 #define LABEL(Name) do_##Name
1319 union printf_arg
*args_value
; /* This is not used here but ... */
1320 int is_negative
; /* Flag for negative number. */
1323 unsigned long long int longlong
;
1324 unsigned long int word
;
1327 union printf_arg the_arg
;
1328 CHAR_T
*string
; /* Pointer to argument string. */
1329 int alt
= 0; /* Alternate format. */
1330 int space
= 0; /* Use space prefix if no sign is needed. */
1331 int left
= 0; /* Left-justify output. */
1332 int showsign
= 0; /* Always begin with plus or minus sign. */
1333 int group
= 0; /* Print numbers according grouping rules. */
1334 int is_long_double
= 0; /* Argument is long double/ long long int. */
1335 int is_short
= 0; /* Argument is short int. */
1336 int is_long
= 0; /* Argument is long int. */
1337 int is_char
= 0; /* Argument is promoted (unsigned) char. */
1338 int width
= 0; /* Width of output; 0 means none specified. */
1339 int prec
= -1; /* Precision of output; -1 means none specified. */
1340 /* This flag is set by the 'I' modifier and selects the use of the
1341 `outdigits' as determined by the current locale. */
1342 int use_outdigits
= 0;
1343 UCHAR_T pad
= L_(' ');/* Padding character. */
1347 workend
= &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
1349 /* Get current character in format string. */
1350 JUMP (*++f
, step0_jumps
);
1355 JUMP (*++f
, step0_jumps
);
1360 JUMP (*++f
, step0_jumps
);
1366 JUMP (*++f
, step0_jumps
);
1371 JUMP (*++f
, step0_jumps
);
1377 JUMP (*++f
, step0_jumps
);
1379 /* The '\'' flag. */
1383 if (grouping
== (const char *) -1)
1385 #ifdef COMPILE_WPRINTF
1386 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1387 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1389 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1392 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1393 if (*grouping
== '\0' || *grouping
== CHAR_MAX
1394 #ifdef COMPILE_WPRINTF
1395 || thousands_sep
== L
'\0'
1397 || *thousands_sep
== '\0'
1402 JUMP (*++f
, step0_jumps
);
1406 JUMP (*++f
, step0_jumps
);
1408 /* Get width from argument. */
1409 LABEL (width_asterics
):
1411 const UCHAR_T
*tmp
; /* Temporary value. */
1414 if (ISDIGIT (*tmp
) && read_int (&tmp
) && *tmp
== L_('$'))
1415 /* The width comes from a positional parameter. */
1418 width
= va_arg (ap
, int);
1420 /* Negative width means left justified. */
1428 if (width
+ 32 >= (int) (sizeof (work_buffer
)
1429 / sizeof (work_buffer
[0])))
1431 /* We have to use a special buffer. The "32" is just a safe
1432 bet for all the output which is not counted in the width. */
1433 if (__libc_use_alloca ((width
+ 32) * sizeof (CHAR_T
)))
1434 workend
= ((CHAR_T
*) alloca ((width
+ 32) * sizeof (CHAR_T
))
1438 workstart
= (CHAR_T
*) malloc ((width
+ 32) * sizeof (CHAR_T
));
1439 if (workstart
== NULL
)
1444 workend
= workstart
+ (width
+ 32);
1448 JUMP (*f
, step1_jumps
);
1450 /* Given width in format string. */
1452 width
= read_int (&f
);
1454 if (width
+ 32 >= (int) (sizeof (work_buffer
) / sizeof (work_buffer
[0])))
1456 /* We have to use a special buffer. The "32" is just a safe
1457 bet for all the output which is not counted in the width. */
1458 if (__libc_use_alloca ((width
+ 32) * sizeof (CHAR_T
)))
1459 workend
= ((CHAR_T
*) alloca ((width
+ 32) * sizeof (CHAR_T
))
1463 workstart
= (CHAR_T
*) malloc ((width
+ 32) * sizeof (CHAR_T
));
1464 if (workstart
== NULL
)
1469 workend
= workstart
+ (width
+ 32);
1473 /* Oh, oh. The argument comes from a positional parameter. */
1475 JUMP (*f
, step1_jumps
);
1481 const UCHAR_T
*tmp
; /* Temporary value. */
1484 if (ISDIGIT (*tmp
) && read_int (&tmp
) > 0 && *tmp
== L_('$'))
1485 /* The precision comes from a positional parameter. */
1488 prec
= va_arg (ap
, int);
1490 /* If the precision is negative the precision is omitted. */
1494 else if (ISDIGIT (*f
))
1495 prec
= read_int (&f
);
1499 && prec
+ 32 > (int)(sizeof (work_buffer
) / sizeof (work_buffer
[0])))
1501 if (__libc_use_alloca ((prec
+ 32) * sizeof (CHAR_T
)))
1502 workend
= ((CHAR_T
*) alloca ((prec
+ 32) * sizeof (CHAR_T
)))
1506 workstart
= (CHAR_T
*) malloc ((prec
+ 32) * sizeof (CHAR_T
));
1507 if (workstart
== NULL
)
1512 workend
= workstart
+ (prec
+ 32);
1515 JUMP (*f
, step2_jumps
);
1517 /* Process 'h' modifier. There might another 'h' following. */
1520 JUMP (*++f
, step3a_jumps
);
1522 /* Process 'hh' modifier. */
1523 LABEL (mod_halfhalf
):
1526 JUMP (*++f
, step4_jumps
);
1528 /* Process 'l' modifier. There might another 'l' following. */
1531 JUMP (*++f
, step3b_jumps
);
1533 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1534 allowed to follow. */
1535 LABEL (mod_longlong
):
1538 JUMP (*++f
, step4_jumps
);
1541 is_long_double
= sizeof (size_t) > sizeof (unsigned long int);
1542 is_long
= sizeof (size_t) > sizeof (unsigned int);
1543 JUMP (*++f
, step4_jumps
);
1545 LABEL (mod_ptrdiff_t
):
1546 is_long_double
= sizeof (ptrdiff_t) > sizeof (unsigned long int);
1547 is_long
= sizeof (ptrdiff_t) > sizeof (unsigned int);
1548 JUMP (*++f
, step4_jumps
);
1550 LABEL (mod_intmax_t
):
1551 is_long_double
= sizeof (intmax_t) > sizeof (unsigned long int);
1552 is_long
= sizeof (intmax_t) > sizeof (unsigned int);
1553 JUMP (*++f
, step4_jumps
);
1555 /* Process current format. */
1558 process_arg (((struct printf_spec
*) NULL
));
1559 process_string_arg (((struct printf_spec
*) NULL
));
1561 LABEL (form_unknown
):
1562 if (spec
== L_('\0'))
1564 /* The format string ended before the specifier is complete. */
1569 /* If we are in the fast loop force entering the complicated
1574 /* The format is correctly handled. */
1577 if (__builtin_expect (workstart
!= NULL
, 0))
1581 /* Look for next format specifier. */
1582 #ifdef COMPILE_WPRINTF
1583 f
= __find_specwc ((end_of_spec
= ++f
));
1585 f
= __find_specmb ((end_of_spec
= ++f
), &mbstate
);
1588 /* Write the following constant string. */
1589 outstring (end_of_spec
, f
- end_of_spec
);
1591 while (*f
!= L_('\0'));
1593 /* Unlock stream and return. */
1596 /* Here starts the more complex loop to handle positional parameters. */
1599 /* Array with information about the needed arguments. This has to
1600 be dynamically extensible. */
1602 size_t nspecs_max
= 32; /* A more or less arbitrary start value. */
1603 struct printf_spec
*specs
1604 = alloca (nspecs_max
* sizeof (struct printf_spec
));
1606 /* The number of arguments the format string requests. This will
1607 determine the size of the array needed to store the argument
1611 union printf_arg
*args_value
= NULL
;
1613 /* Positional parameters refer to arguments directly. This could
1614 also determine the maximum number of arguments. Track the
1616 size_t max_ref_arg
= 0;
1618 /* Just a counter. */
1622 if (grouping
== (const char *) -1)
1624 #ifdef COMPILE_WPRINTF
1625 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1626 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1628 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1631 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1632 if (*grouping
== '\0' || *grouping
== CHAR_MAX
)
1636 for (f
= lead_str_end
; *f
!= L_('\0'); f
= specs
[nspecs
++].next_fmt
)
1638 if (nspecs
>= nspecs_max
)
1640 /* Extend the array of format specifiers. */
1641 struct printf_spec
*old
= specs
;
1644 specs
= alloca (nspecs_max
* sizeof (struct printf_spec
));
1646 if (specs
== &old
[nspecs
])
1647 /* Stack grows up, OLD was the last thing allocated;
1649 nspecs_max
+= nspecs_max
/ 2;
1652 /* Copy the old array's elements to the new space. */
1653 memcpy (specs
, old
, nspecs
* sizeof (struct printf_spec
));
1654 if (old
== &specs
[nspecs
])
1655 /* Stack grows down, OLD was just below the new
1656 SPECS. We can use that space when the new space
1658 nspecs_max
+= nspecs_max
/ 2;
1662 /* Parse the format specifier. */
1663 #ifdef COMPILE_WPRINTF
1664 nargs
+= __parse_one_specwc (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1666 nargs
+= __parse_one_specmb (f
, nargs
, &specs
[nspecs
], &max_ref_arg
,
1671 /* Determine the number of arguments the format string consumes. */
1672 nargs
= MAX (nargs
, max_ref_arg
);
1674 /* Allocate memory for the argument descriptions. */
1675 args_type
= alloca (nargs
* sizeof (int));
1676 memset (args_type
, s
->_flags2
& _IO_FLAGS2_FORTIFY
? '\xff' : '\0',
1677 nargs
* sizeof (int));
1678 args_value
= alloca (nargs
* sizeof (union printf_arg
));
1680 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1681 still zero after this loop, format is invalid. For now we
1682 simply use 0 as the value. */
1684 /* Fill in the types of all the arguments. */
1685 for (cnt
= 0; cnt
< nspecs
; ++cnt
)
1687 /* If the width is determined by an argument this is an int. */
1688 if (specs
[cnt
].width_arg
!= -1)
1689 args_type
[specs
[cnt
].width_arg
] = PA_INT
;
1691 /* If the precision is determined by an argument this is an int. */
1692 if (specs
[cnt
].prec_arg
!= -1)
1693 args_type
[specs
[cnt
].prec_arg
] = PA_INT
;
1695 switch (specs
[cnt
].ndata_args
)
1697 case 0: /* No arguments. */
1699 case 1: /* One argument; we already have the type. */
1700 args_type
[specs
[cnt
].data_arg
] = specs
[cnt
].data_arg_type
;
1703 /* We have more than one argument for this format spec.
1704 We must call the arginfo function again to determine
1706 (void) (*__printf_arginfo_table
[specs
[cnt
].info
.spec
])
1708 specs
[cnt
].ndata_args
, &args_type
[specs
[cnt
].data_arg
]);
1713 /* Now we know all the types and the order. Fill in the argument
1715 for (cnt
= 0; cnt
< nargs
; ++cnt
)
1716 switch (args_type
[cnt
])
1718 #define T(tag, mem, type) \
1720 args_value[cnt].mem = va_arg (ap_save, type); \
1723 T (PA_CHAR
, pa_int
, int); /* Promoted. */
1724 T (PA_WCHAR
, pa_wchar
, wint_t);
1725 T (PA_INT
|PA_FLAG_SHORT
, pa_int
, int); /* Promoted. */
1726 T (PA_INT
, pa_int
, int);
1727 T (PA_INT
|PA_FLAG_LONG
, pa_long_int
, long int);
1728 T (PA_INT
|PA_FLAG_LONG_LONG
, pa_long_long_int
, long long int);
1729 T (PA_FLOAT
, pa_double
, double); /* Promoted. */
1730 T (PA_DOUBLE
, pa_double
, double);
1731 case PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
:
1734 args_value
[cnt
].pa_double
= va_arg (ap_save
, double);
1735 args_type
[cnt
] &= ~PA_FLAG_LONG_DOUBLE
;
1738 args_value
[cnt
].pa_long_double
= va_arg (ap_save
, long double);
1740 T (PA_STRING
, pa_string
, const char *);
1741 T (PA_WSTRING
, pa_wstring
, const wchar_t *);
1742 T (PA_POINTER
, pa_pointer
, void *);
1745 if ((args_type
[cnt
] & PA_FLAG_PTR
) != 0)
1746 args_value
[cnt
].pa_pointer
= va_arg (ap_save
, void *);
1748 args_value
[cnt
].pa_long_double
= 0.0;
1751 /* Error case. Not all parameters appear in N$ format
1752 strings. We have no way to determine their type. */
1753 assert (s
->_flags2
& _IO_FLAGS2_FORTIFY
);
1754 __libc_fatal ("*** invalid %N$ use detected ***\n");
1757 /* Now walk through all format specifiers and process them. */
1758 for (; (size_t) nspecs_done
< nspecs
; ++nspecs_done
)
1762 # define REF(Name) &&do2_##Name - &&do_form_unknown
1764 # define REF(Name) &&do2_##Name
1767 #define LABEL(Name) do2_##Name
1773 unsigned long long int longlong
;
1774 unsigned long int word
;
1777 union printf_arg the_arg
;
1778 CHAR_T
*string
; /* Pointer to argument string. */
1780 /* Fill variables from values in struct. */
1781 int alt
= specs
[nspecs_done
].info
.alt
;
1782 int space
= specs
[nspecs_done
].info
.space
;
1783 int left
= specs
[nspecs_done
].info
.left
;
1784 int showsign
= specs
[nspecs_done
].info
.showsign
;
1785 int group
= specs
[nspecs_done
].info
.group
;
1786 int is_long_double
= specs
[nspecs_done
].info
.is_long_double
;
1787 int is_short
= specs
[nspecs_done
].info
.is_short
;
1788 int is_char
= specs
[nspecs_done
].info
.is_char
;
1789 int is_long
= specs
[nspecs_done
].info
.is_long
;
1790 int width
= specs
[nspecs_done
].info
.width
;
1791 int prec
= specs
[nspecs_done
].info
.prec
;
1792 int use_outdigits
= specs
[nspecs_done
].info
.i18n
;
1793 char pad
= specs
[nspecs_done
].info
.pad
;
1794 CHAR_T spec
= specs
[nspecs_done
].info
.spec
;
1795 CHAR_T
*workstart
= NULL
;
1797 /* Fill in last information. */
1798 if (specs
[nspecs_done
].width_arg
!= -1)
1800 /* Extract the field width from an argument. */
1801 specs
[nspecs_done
].info
.width
=
1802 args_value
[specs
[nspecs_done
].width_arg
].pa_int
;
1804 if (specs
[nspecs_done
].info
.width
< 0)
1805 /* If the width value is negative left justification is
1806 selected and the value is taken as being positive. */
1808 specs
[nspecs_done
].info
.width
*= -1;
1809 left
= specs
[nspecs_done
].info
.left
= 1;
1811 width
= specs
[nspecs_done
].info
.width
;
1814 if (specs
[nspecs_done
].prec_arg
!= -1)
1816 /* Extract the precision from an argument. */
1817 specs
[nspecs_done
].info
.prec
=
1818 args_value
[specs
[nspecs_done
].prec_arg
].pa_int
;
1820 if (specs
[nspecs_done
].info
.prec
< 0)
1821 /* If the precision is negative the precision is
1823 specs
[nspecs_done
].info
.prec
= -1;
1825 prec
= specs
[nspecs_done
].info
.prec
;
1828 /* Maybe the buffer is too small. */
1829 if (MAX (prec
, width
) + 32 > (int) (sizeof (work_buffer
)
1832 if (__libc_use_alloca ((MAX (prec
, width
) + 32)
1834 workend
= ((CHAR_T
*) alloca ((MAX (prec
, width
) + 32)
1836 + (MAX (prec
, width
) + 32));
1839 workstart
= (CHAR_T
*) malloc ((MAX (prec
, width
) + 32)
1841 workend
= workstart
+ (MAX (prec
, width
) + 32);
1845 /* Process format specifiers. */
1848 JUMP (spec
, step4_jumps
);
1850 process_arg ((&specs
[nspecs_done
]));
1851 process_string_arg ((&specs
[nspecs_done
]));
1853 LABEL (form_unknown
):
1855 extern printf_function
**__printf_function_table
;
1857 printf_function
*function
;
1862 (__printf_function_table
== NULL
? NULL
:
1863 __printf_function_table
[specs
[nspecs_done
].info
.spec
]);
1865 if (function
== NULL
)
1866 function
= &printf_unknown
;
1868 ptr
= alloca (specs
[nspecs_done
].ndata_args
1869 * sizeof (const void *));
1871 /* Fill in an array of pointers to the argument values. */
1872 for (i
= 0; i
< specs
[nspecs_done
].ndata_args
; ++i
)
1873 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
1875 /* Call the function. */
1876 function_done
= (*function
) (s
, &specs
[nspecs_done
].info
, ptr
);
1878 /* If an error occurred we don't have information about #
1880 if (function_done
< 0)
1886 done
+= function_done
;
1891 if (__builtin_expect (workstart
!= NULL
, 0))
1895 /* Write the following constant string. */
1896 outstring (specs
[nspecs_done
].end_of_fmt
,
1897 specs
[nspecs_done
].next_fmt
1898 - specs
[nspecs_done
].end_of_fmt
);
1903 if (__builtin_expect (workstart
!= NULL
, 0))
1905 /* Unlock the stream. */
1906 _IO_funlockfile (s
);
1907 _IO_cleanup_region_end (0);
1912 /* Handle an unknown format specifier. This prints out a canonicalized
1913 representation of the format spec itself. */
1915 printf_unknown (FILE *s
, const struct printf_info
*info
,
1916 const void *const *args
)
1920 CHAR_T work_buffer
[MAX (info
->width
, info
->spec
) + 32];
1921 CHAR_T
*const workend
1922 = &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
1933 else if (info
->space
)
1937 if (info
->pad
== L_('0'))
1942 if (info
->width
!= 0)
1944 w
= _itoa_word (info
->width
, workend
, 10, 0);
1949 if (info
->prec
!= -1)
1952 w
= _itoa_word (info
->prec
, workend
, 10, 0);
1957 if (info
->spec
!= L_('\0'))
1958 outchar (info
->spec
);
1964 /* Group the digits according to the grouping rules of the current locale.
1965 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1968 group_number (CHAR_T
*w
, CHAR_T
*rear_ptr
, const char *grouping
,
1969 #ifdef COMPILE_WPRINTF
1970 wchar_t thousands_sep
1972 const char *thousands_sep
1978 #ifndef COMPILE_WPRINTF
1979 int tlen
= strlen (thousands_sep
);
1982 /* We treat all negative values like CHAR_MAX. */
1984 if (*grouping
== CHAR_MAX
|| *grouping
<= 0)
1985 /* No grouping should be done. */
1990 /* Copy existing string so that nothing gets overwritten. */
1991 src
= (CHAR_T
*) alloca ((rear_ptr
- w
) * sizeof (CHAR_T
));
1992 s
= (CHAR_T
*) __mempcpy (src
, w
,
1993 (rear_ptr
- w
) * sizeof (CHAR_T
));
1996 /* Process all characters in the string. */
2001 if (--len
== 0 && s
> src
)
2003 /* A new group begins. */
2004 #ifdef COMPILE_WPRINTF
2005 *--w
= thousands_sep
;
2009 *--w
= thousands_sep
[--cnt
];
2013 if (*grouping
== CHAR_MAX
2019 /* No further grouping to be done.
2020 Copy the rest of the number. */
2026 else if (*grouping
!= '\0')
2027 /* The previous grouping repeats ad infinitum. */
2036 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2039 struct _IO_FILE_plus _f
;
2040 #ifdef COMPILE_WPRINTF
2041 struct _IO_wide_data _wide_data
;
2043 _IO_FILE
*_put_stream
;
2044 #ifdef _IO_MTSAFE_IO
2050 _IO_helper_overflow (_IO_FILE
*s
, int c
)
2052 _IO_FILE
*target
= ((struct helper_file
*) s
)->_put_stream
;
2053 #ifdef COMPILE_WPRINTF
2054 int used
= s
->_wide_data
->_IO_write_ptr
- s
->_wide_data
->_IO_write_base
;
2057 _IO_size_t written
= _IO_sputn (target
, s
->_wide_data
->_IO_write_base
,
2059 s
->_wide_data
->_IO_write_ptr
-= written
;
2062 int used
= s
->_IO_write_ptr
- s
->_IO_write_base
;
2065 _IO_size_t written
= _IO_sputn (target
, s
->_IO_write_base
, used
);
2066 s
->_IO_write_ptr
-= written
;
2072 #ifdef COMPILE_WPRINTF
2073 static const struct _IO_jump_t _IO_helper_jumps
=
2076 JUMP_INIT (finish
, INTUSE(_IO_wdefault_finish
)),
2077 JUMP_INIT (overflow
, _IO_helper_overflow
),
2078 JUMP_INIT (underflow
, _IO_default_underflow
),
2079 JUMP_INIT (uflow
, INTUSE(_IO_default_uflow
)),
2080 JUMP_INIT (pbackfail
, (_IO_pbackfail_t
) INTUSE(_IO_wdefault_pbackfail
)),
2081 JUMP_INIT (xsputn
, INTUSE(_IO_wdefault_xsputn
)),
2082 JUMP_INIT (xsgetn
, INTUSE(_IO_wdefault_xsgetn
)),
2083 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2084 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2085 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2086 JUMP_INIT (sync
, _IO_default_sync
),
2087 JUMP_INIT (doallocate
, INTUSE(_IO_wdefault_doallocate
)),
2088 JUMP_INIT (read
, _IO_default_read
),
2089 JUMP_INIT (write
, _IO_default_write
),
2090 JUMP_INIT (seek
, _IO_default_seek
),
2091 JUMP_INIT (close
, _IO_default_close
),
2092 JUMP_INIT (stat
, _IO_default_stat
)
2095 static const struct _IO_jump_t _IO_helper_jumps
=
2098 JUMP_INIT (finish
, INTUSE(_IO_default_finish
)),
2099 JUMP_INIT (overflow
, _IO_helper_overflow
),
2100 JUMP_INIT (underflow
, _IO_default_underflow
),
2101 JUMP_INIT (uflow
, INTUSE(_IO_default_uflow
)),
2102 JUMP_INIT (pbackfail
, INTUSE(_IO_default_pbackfail
)),
2103 JUMP_INIT (xsputn
, INTUSE(_IO_default_xsputn
)),
2104 JUMP_INIT (xsgetn
, INTUSE(_IO_default_xsgetn
)),
2105 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2106 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2107 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2108 JUMP_INIT (sync
, _IO_default_sync
),
2109 JUMP_INIT (doallocate
, INTUSE(_IO_default_doallocate
)),
2110 JUMP_INIT (read
, _IO_default_read
),
2111 JUMP_INIT (write
, _IO_default_write
),
2112 JUMP_INIT (seek
, _IO_default_seek
),
2113 JUMP_INIT (close
, _IO_default_close
),
2114 JUMP_INIT (stat
, _IO_default_stat
)
2120 buffered_vfprintf (register _IO_FILE
*s
, const CHAR_T
*format
,
2123 CHAR_T buf
[_IO_BUFSIZ
];
2124 struct helper_file helper
;
2125 register _IO_FILE
*hp
= (_IO_FILE
*) &helper
._f
;
2126 int result
, to_flush
;
2128 /* Orient the stream. */
2133 /* Initialize helper. */
2134 helper
._put_stream
= s
;
2135 #ifdef COMPILE_WPRINTF
2136 hp
->_wide_data
= &helper
._wide_data
;
2137 _IO_wsetp (hp
, buf
, buf
+ sizeof buf
/ sizeof (CHAR_T
));
2140 _IO_setp (hp
, buf
, buf
+ sizeof buf
);
2143 hp
->_IO_file_flags
= _IO_MAGIC
|_IO_NO_READS
|_IO_USER_LOCK
;
2144 #if _IO_JUMPS_OFFSET
2145 hp
->_vtable_offset
= 0;
2147 #ifdef _IO_MTSAFE_IO
2150 hp
->_flags2
= s
->_flags2
;
2151 _IO_JUMPS (&helper
._f
) = (struct _IO_jump_t
*) &_IO_helper_jumps
;
2153 /* Now print to helper instead. */
2154 #ifndef COMPILE_WPRINTF
2155 result
= INTUSE(_IO_vfprintf
) (hp
, format
, args
);
2157 result
= vfprintf (hp
, format
, args
);
2161 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile
, s
);
2164 /* Now flush anything from the helper to the S. */
2165 #ifdef COMPILE_WPRINTF
2166 if ((to_flush
= (hp
->_wide_data
->_IO_write_ptr
2167 - hp
->_wide_data
->_IO_write_base
)) > 0)
2169 if ((int) _IO_sputn (s
, hp
->_wide_data
->_IO_write_base
, to_flush
)
2174 if ((to_flush
= hp
->_IO_write_ptr
- hp
->_IO_write_base
) > 0)
2176 if ((int) _IO_sputn (s
, hp
->_IO_write_base
, to_flush
) != to_flush
)
2181 /* Unlock the stream. */
2182 _IO_funlockfile (s
);
2183 __libc_cleanup_region_end (0);
2189 #ifdef COMPILE_WPRINTF
2190 strong_alias (_IO_vfwprintf
, __vfwprintf
);
2191 ldbl_weak_alias (_IO_vfwprintf
, vfwprintf
);
2193 ldbl_strong_alias (_IO_vfprintf_internal
, vfprintf
);
2194 ldbl_hidden_def (_IO_vfprintf_internal
, vfprintf
)
2195 ldbl_strong_alias (_IO_vfprintf_internal
, _IO_vfprintf
);