1 /* Copyright (C) 1991-2002, 2003, 2004, 2005, 2006, 2007
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
103 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
104 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
109 #include "_i18n_number.h"
111 /* Include the shared code for parsing the format string. */
112 #include "printf-parse.h"
115 #define outchar(Ch) \
118 register const INT_T outc = (Ch); \
119 if (PUTC (outc, s) == EOF) \
129 #define outstring(String, Len) \
132 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
141 /* For handling long_double and longlong we use the same flag. If
142 `long' and `long long' are effectively the same type define it to
144 #if LONG_MAX == LONG_LONG_MAX
145 # define is_longlong 0
147 # define is_longlong is_long_double
150 /* If `long' and `int' is effectively the same type we don't have to
151 handle `long separately. */
152 #if INT_MAX == LONG_MAX
153 # define is_long_num 0
155 # define is_long_num is_long
159 /* Global variables. */
160 static const CHAR_T null
[] = L_("(null)");
163 /* Helper function to provide temporary buffering for unbuffered streams. */
164 static int buffered_vfprintf (FILE *stream
, const CHAR_T
*fmt
, va_list)
165 __THROW
__attribute__ ((noinline
)) internal_function
;
167 /* Handle unknown format specifier. */
168 static int printf_unknown (FILE *, const struct printf_info
*,
169 const void *const *) __THROW
;
171 /* Group digits of number string. */
172 #ifdef COMPILE_WPRINTF
173 static CHAR_T
*group_number (CHAR_T
*, CHAR_T
*, const char *, wchar_t)
174 __THROW internal_function
;
176 static CHAR_T
*group_number (CHAR_T
*, CHAR_T
*, const char *, const char *)
177 __THROW internal_function
;
181 /* The function itself. */
183 vfprintf (FILE *s
, const CHAR_T
*format
, va_list ap
)
185 /* The character used as thousands separator. */
186 #ifdef COMPILE_WPRINTF
187 wchar_t thousands_sep
= L
'\0';
189 const char *thousands_sep
= NULL
;
192 /* The string describing the size of groups of digits. */
193 const char *grouping
;
195 /* Place to accumulate the result. */
198 /* Current character in format string. */
201 /* End of leading constant string. */
202 const UCHAR_T
*lead_str_end
;
204 /* Points to next format specifier. */
205 const UCHAR_T
*end_of_spec
;
207 /* Buffer intermediate results. */
208 CHAR_T work_buffer
[1000];
209 CHAR_T
*workstart
= NULL
;
212 /* We have to save the original argument pointer. */
215 /* Count number of specifiers we already processed. */
218 /* For the %m format we may need the current `errno' value. */
219 int save_errno
= errno
;
221 /* 1 if format is in read-only memory, -1 if it is in writable memory,
223 int readonly_format
= 0;
225 /* This table maps a character into a number representing a
226 class. In each step there is a destination label for each
228 static const int jump_table
[] =
230 /* ' ' */ 1, 0, 0, /* '#' */ 4,
231 0, /* '%' */ 14, 0, /* '\''*/ 6,
232 0, 0, /* '*' */ 7, /* '+' */ 2,
233 0, /* '-' */ 3, /* '.' */ 9, 0,
234 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
235 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
236 /* '8' */ 8, /* '9' */ 8, 0, 0,
238 0, /* 'A' */ 26, 0, /* 'C' */ 25,
239 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
240 0, /* 'I' */ 29, 0, 0,
241 /* 'L' */ 12, 0, 0, 0,
242 0, 0, 0, /* 'S' */ 21,
244 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
246 0, /* 'a' */ 26, 0, /* 'c' */ 20,
247 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
248 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
249 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
250 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
251 /* 't' */ 27, /* 'u' */ 16, 0, 0,
252 /* 'x' */ 18, 0, /* 'z' */ 13
255 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
256 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
258 /* 'int' is enough and it saves some space on 64 bit systems. */
259 # define JUMP_TABLE_TYPE const int
260 # define JUMP(ChExpr, table) \
264 void *__unbounded ptr; \
266 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
267 : table[CHAR_CLASS (spec)]; \
268 ptr = &&do_form_unknown + offset; \
273 # define JUMP_TABLE_TYPE const void *const
274 # define JUMP(ChExpr, table) \
277 const void *__unbounded ptr; \
279 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
280 : table[CHAR_CLASS (spec)]; \
286 #define STEP0_3_TABLE \
287 /* Step 0: at the beginning. */ \
288 static JUMP_TABLE_TYPE step0_jumps[30] = \
290 REF (form_unknown), \
291 REF (flag_space), /* for ' ' */ \
292 REF (flag_plus), /* for '+' */ \
293 REF (flag_minus), /* for '-' */ \
294 REF (flag_hash), /* for '<hash>' */ \
295 REF (flag_zero), /* for '0' */ \
296 REF (flag_quote), /* for '\'' */ \
297 REF (width_asterics), /* for '*' */ \
298 REF (width), /* for '1'...'9' */ \
299 REF (precision), /* for '.' */ \
300 REF (mod_half), /* for 'h' */ \
301 REF (mod_long), /* for 'l' */ \
302 REF (mod_longlong), /* for 'L', 'q' */ \
303 REF (mod_size_t), /* for 'z', 'Z' */ \
304 REF (form_percent), /* for '%' */ \
305 REF (form_integer), /* for 'd', 'i' */ \
306 REF (form_unsigned), /* for 'u' */ \
307 REF (form_octal), /* for 'o' */ \
308 REF (form_hexa), /* for 'X', 'x' */ \
309 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
310 REF (form_character), /* for 'c' */ \
311 REF (form_string), /* for 's', 'S' */ \
312 REF (form_pointer), /* for 'p' */ \
313 REF (form_number), /* for 'n' */ \
314 REF (form_strerror), /* for 'm' */ \
315 REF (form_wcharacter), /* for 'C' */ \
316 REF (form_floathex), /* for 'A', 'a' */ \
317 REF (mod_ptrdiff_t), /* for 't' */ \
318 REF (mod_intmax_t), /* for 'j' */ \
319 REF (flag_i18n), /* for 'I' */ \
321 /* Step 1: after processing width. */ \
322 static JUMP_TABLE_TYPE step1_jumps[30] = \
324 REF (form_unknown), \
325 REF (form_unknown), /* for ' ' */ \
326 REF (form_unknown), /* for '+' */ \
327 REF (form_unknown), /* for '-' */ \
328 REF (form_unknown), /* for '<hash>' */ \
329 REF (form_unknown), /* for '0' */ \
330 REF (form_unknown), /* for '\'' */ \
331 REF (form_unknown), /* for '*' */ \
332 REF (form_unknown), /* for '1'...'9' */ \
333 REF (precision), /* for '.' */ \
334 REF (mod_half), /* for 'h' */ \
335 REF (mod_long), /* for 'l' */ \
336 REF (mod_longlong), /* for 'L', 'q' */ \
337 REF (mod_size_t), /* for 'z', 'Z' */ \
338 REF (form_percent), /* for '%' */ \
339 REF (form_integer), /* for 'd', 'i' */ \
340 REF (form_unsigned), /* for 'u' */ \
341 REF (form_octal), /* for 'o' */ \
342 REF (form_hexa), /* for 'X', 'x' */ \
343 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
344 REF (form_character), /* for 'c' */ \
345 REF (form_string), /* for 's', 'S' */ \
346 REF (form_pointer), /* for 'p' */ \
347 REF (form_number), /* for 'n' */ \
348 REF (form_strerror), /* for 'm' */ \
349 REF (form_wcharacter), /* for 'C' */ \
350 REF (form_floathex), /* for 'A', 'a' */ \
351 REF (mod_ptrdiff_t), /* for 't' */ \
352 REF (mod_intmax_t), /* for 'j' */ \
353 REF (form_unknown) /* for 'I' */ \
355 /* Step 2: after processing precision. */ \
356 static JUMP_TABLE_TYPE step2_jumps[30] = \
358 REF (form_unknown), \
359 REF (form_unknown), /* for ' ' */ \
360 REF (form_unknown), /* for '+' */ \
361 REF (form_unknown), /* for '-' */ \
362 REF (form_unknown), /* for '<hash>' */ \
363 REF (form_unknown), /* for '0' */ \
364 REF (form_unknown), /* for '\'' */ \
365 REF (form_unknown), /* for '*' */ \
366 REF (form_unknown), /* for '1'...'9' */ \
367 REF (form_unknown), /* for '.' */ \
368 REF (mod_half), /* for 'h' */ \
369 REF (mod_long), /* for 'l' */ \
370 REF (mod_longlong), /* for 'L', 'q' */ \
371 REF (mod_size_t), /* for 'z', 'Z' */ \
372 REF (form_percent), /* for '%' */ \
373 REF (form_integer), /* for 'd', 'i' */ \
374 REF (form_unsigned), /* for 'u' */ \
375 REF (form_octal), /* for 'o' */ \
376 REF (form_hexa), /* for 'X', 'x' */ \
377 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
378 REF (form_character), /* for 'c' */ \
379 REF (form_string), /* for 's', 'S' */ \
380 REF (form_pointer), /* for 'p' */ \
381 REF (form_number), /* for 'n' */ \
382 REF (form_strerror), /* for 'm' */ \
383 REF (form_wcharacter), /* for 'C' */ \
384 REF (form_floathex), /* for 'A', 'a' */ \
385 REF (mod_ptrdiff_t), /* for 't' */ \
386 REF (mod_intmax_t), /* for 'j' */ \
387 REF (form_unknown) /* for 'I' */ \
389 /* Step 3a: after processing first 'h' modifier. */ \
390 static JUMP_TABLE_TYPE step3a_jumps[30] = \
392 REF (form_unknown), \
393 REF (form_unknown), /* for ' ' */ \
394 REF (form_unknown), /* for '+' */ \
395 REF (form_unknown), /* for '-' */ \
396 REF (form_unknown), /* for '<hash>' */ \
397 REF (form_unknown), /* for '0' */ \
398 REF (form_unknown), /* for '\'' */ \
399 REF (form_unknown), /* for '*' */ \
400 REF (form_unknown), /* for '1'...'9' */ \
401 REF (form_unknown), /* for '.' */ \
402 REF (mod_halfhalf), /* for 'h' */ \
403 REF (form_unknown), /* for 'l' */ \
404 REF (form_unknown), /* for 'L', 'q' */ \
405 REF (form_unknown), /* for 'z', 'Z' */ \
406 REF (form_percent), /* for '%' */ \
407 REF (form_integer), /* for 'd', 'i' */ \
408 REF (form_unsigned), /* for 'u' */ \
409 REF (form_octal), /* for 'o' */ \
410 REF (form_hexa), /* for 'X', 'x' */ \
411 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
412 REF (form_unknown), /* for 'c' */ \
413 REF (form_unknown), /* for 's', 'S' */ \
414 REF (form_unknown), /* for 'p' */ \
415 REF (form_number), /* for 'n' */ \
416 REF (form_unknown), /* for 'm' */ \
417 REF (form_unknown), /* for 'C' */ \
418 REF (form_unknown), /* for 'A', 'a' */ \
419 REF (form_unknown), /* for 't' */ \
420 REF (form_unknown), /* for 'j' */ \
421 REF (form_unknown) /* for 'I' */ \
423 /* Step 3b: after processing first 'l' modifier. */ \
424 static JUMP_TABLE_TYPE step3b_jumps[30] = \
426 REF (form_unknown), \
427 REF (form_unknown), /* for ' ' */ \
428 REF (form_unknown), /* for '+' */ \
429 REF (form_unknown), /* for '-' */ \
430 REF (form_unknown), /* for '<hash>' */ \
431 REF (form_unknown), /* for '0' */ \
432 REF (form_unknown), /* for '\'' */ \
433 REF (form_unknown), /* for '*' */ \
434 REF (form_unknown), /* for '1'...'9' */ \
435 REF (form_unknown), /* for '.' */ \
436 REF (form_unknown), /* for 'h' */ \
437 REF (mod_longlong), /* for 'l' */ \
438 REF (form_unknown), /* for 'L', 'q' */ \
439 REF (form_unknown), /* for 'z', 'Z' */ \
440 REF (form_percent), /* for '%' */ \
441 REF (form_integer), /* for 'd', 'i' */ \
442 REF (form_unsigned), /* for 'u' */ \
443 REF (form_octal), /* for 'o' */ \
444 REF (form_hexa), /* for 'X', 'x' */ \
445 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
446 REF (form_character), /* for 'c' */ \
447 REF (form_string), /* for 's', 'S' */ \
448 REF (form_pointer), /* for 'p' */ \
449 REF (form_number), /* for 'n' */ \
450 REF (form_strerror), /* for 'm' */ \
451 REF (form_wcharacter), /* for 'C' */ \
452 REF (form_floathex), /* for 'A', 'a' */ \
453 REF (form_unknown), /* for 't' */ \
454 REF (form_unknown), /* for 'j' */ \
455 REF (form_unknown) /* for 'I' */ \
458 #define STEP4_TABLE \
459 /* Step 4: processing format specifier. */ \
460 static JUMP_TABLE_TYPE step4_jumps[30] = \
462 REF (form_unknown), \
463 REF (form_unknown), /* for ' ' */ \
464 REF (form_unknown), /* for '+' */ \
465 REF (form_unknown), /* for '-' */ \
466 REF (form_unknown), /* for '<hash>' */ \
467 REF (form_unknown), /* for '0' */ \
468 REF (form_unknown), /* for '\'' */ \
469 REF (form_unknown), /* for '*' */ \
470 REF (form_unknown), /* for '1'...'9' */ \
471 REF (form_unknown), /* for '.' */ \
472 REF (form_unknown), /* for 'h' */ \
473 REF (form_unknown), /* for 'l' */ \
474 REF (form_unknown), /* for 'L', 'q' */ \
475 REF (form_unknown), /* for 'z', 'Z' */ \
476 REF (form_percent), /* for '%' */ \
477 REF (form_integer), /* for 'd', 'i' */ \
478 REF (form_unsigned), /* for 'u' */ \
479 REF (form_octal), /* for 'o' */ \
480 REF (form_hexa), /* for 'X', 'x' */ \
481 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
482 REF (form_character), /* for 'c' */ \
483 REF (form_string), /* for 's', 'S' */ \
484 REF (form_pointer), /* for 'p' */ \
485 REF (form_number), /* for 'n' */ \
486 REF (form_strerror), /* for 'm' */ \
487 REF (form_wcharacter), /* for 'C' */ \
488 REF (form_floathex), /* for 'A', 'a' */ \
489 REF (form_unknown), /* for 't' */ \
490 REF (form_unknown), /* for 'j' */ \
491 REF (form_unknown) /* for 'I' */ \
495 #define process_arg(fspec) \
496 /* Start real work. We know about all flags and modifiers and \
497 now process the wanted format specifier. */ \
498 LABEL (form_percent): \
499 /* Write a literal "%". */ \
503 LABEL (form_integer): \
504 /* Signed decimal integer. */ \
509 long long int signed_number; \
512 signed_number = va_arg (ap, long long int); \
514 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
516 is_negative = signed_number < 0; \
517 number.longlong = is_negative ? (- signed_number) : signed_number; \
519 goto LABEL (longlong_number); \
523 long int signed_number; \
528 signed_number = va_arg (ap, long int); \
530 signed_number = (signed char) va_arg (ap, unsigned int); \
531 else if (!is_short) \
532 signed_number = va_arg (ap, int); \
534 signed_number = (short int) va_arg (ap, unsigned int); \
538 signed_number = args_value[fspec->data_arg].pa_long_int; \
540 signed_number = (signed char) \
541 args_value[fspec->data_arg].pa_u_int; \
542 else if (!is_short) \
543 signed_number = args_value[fspec->data_arg].pa_int; \
545 signed_number = (short int) \
546 args_value[fspec->data_arg].pa_u_int; \
548 is_negative = signed_number < 0; \
549 number.word = is_negative ? (- signed_number) : signed_number; \
551 goto LABEL (number); \
555 LABEL (form_unsigned): \
556 /* Unsigned decimal integer. */ \
558 goto LABEL (unsigned_number); \
561 LABEL (form_octal): \
562 /* Unsigned octal integer. */ \
564 goto LABEL (unsigned_number); \
568 /* Unsigned hexadecimal integer. */ \
571 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
573 /* ISO specifies the `+' and ` ' flags only for signed \
582 number.longlong = va_arg (ap, unsigned long long int); \
584 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
586 LABEL (longlong_number): \
588 /* Supply a default precision if none was given. */ \
591 /* We have to take care for the '0' flag. If a precision \
592 is given it must be ignored. */ \
595 /* If the precision is 0 and the number is 0 nothing has to \
596 be written for the number, except for the 'o' format in \
598 if (prec == 0 && number.longlong == 0) \
601 if (base == 8 && alt) \
602 *--string = L_('0'); \
606 /* Put the number in WORK. */ \
607 string = _itoa (number.longlong, workend, base, \
609 if (group && grouping) \
610 string = group_number (string, workend, grouping, \
613 if (use_outdigits && base == 10) \
614 string = _i18n_number_rewrite (string, workend); \
616 /* Simplify further test for num != 0. */ \
617 number.word = number.longlong != 0; \
624 number.word = va_arg (ap, unsigned long int); \
626 number.word = (unsigned char) va_arg (ap, unsigned int); \
627 else if (!is_short) \
628 number.word = va_arg (ap, unsigned int); \
630 number.word = (unsigned short int) va_arg (ap, unsigned int); \
634 number.word = args_value[fspec->data_arg].pa_u_long_int; \
636 number.word = (unsigned char) \
637 args_value[fspec->data_arg].pa_u_int; \
638 else if (!is_short) \
639 number.word = args_value[fspec->data_arg].pa_u_int; \
641 number.word = (unsigned short int) \
642 args_value[fspec->data_arg].pa_u_int; \
646 /* Supply a default precision if none was given. */ \
649 /* We have to take care for the '0' flag. If a precision \
650 is given it must be ignored. */ \
653 /* If the precision is 0 and the number is 0 nothing has to \
654 be written for the number, except for the 'o' format in \
656 if (prec == 0 && number.word == 0) \
659 if (base == 8 && alt) \
660 *--string = L_('0'); \
664 /* Put the number in WORK. */ \
665 string = _itoa_word (number.word, workend, base, \
667 if (group && grouping) \
668 string = group_number (string, workend, grouping, \
671 if (use_outdigits && base == 10) \
672 string = _i18n_number_rewrite (string, workend); \
676 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
677 /* Add octal marker. */ \
678 *--string = L_('0'); \
680 prec = MAX (0, prec - (workend - string)); \
684 width -= workend - string + prec; \
686 if (number.word != 0 && alt && base == 16) \
687 /* Account for 0X hex marker. */ \
690 if (is_negative || showsign || space) \
693 if (pad == L_(' ')) \
706 if (number.word != 0 && alt && base == 16) \
715 outstring (string, workend - string); \
737 if (number.word != 0 && alt && base == 16) \
744 width -= workend - string + prec; \
754 outstring (string, workend - string); \
760 LABEL (form_float): \
762 /* Floating-point number. This is handled by printf_fp.c. */ \
769 is_long_double = 0; \
771 struct printf_info info = { .prec = prec, \
774 .is_long_double = is_long_double, \
775 .is_short = is_short, \
776 .is_long = is_long, \
780 .showsign = showsign, \
784 .i18n = use_outdigits, \
785 .wide = sizeof (CHAR_T) != 1 }; \
787 if (is_long_double) \
788 the_arg.pa_long_double = va_arg (ap, long double); \
790 the_arg.pa_double = va_arg (ap, double); \
791 ptr = (const void *) &the_arg; \
793 function_done = __printf_fp (s, &info, &ptr); \
797 ptr = (const void *) &args_value[fspec->data_arg]; \
800 fspec->data_arg_type = PA_DOUBLE; \
801 fspec->info.is_long_double = 0; \
804 function_done = __printf_fp (s, &fspec->info, &ptr); \
807 if (function_done < 0) \
809 /* Error in print handler. */ \
814 done += function_done; \
818 LABEL (form_floathex): \
820 /* Floating point number printed as hexadecimal number. */ \
827 is_long_double = 0; \
829 struct printf_info info = { .prec = prec, \
832 .is_long_double = is_long_double, \
833 .is_short = is_short, \
834 .is_long = is_long, \
838 .showsign = showsign, \
842 .wide = sizeof (CHAR_T) != 1 }; \
844 if (is_long_double) \
845 the_arg.pa_long_double = va_arg (ap, long double); \
847 the_arg.pa_double = va_arg (ap, double); \
848 ptr = (const void *) &the_arg; \
850 function_done = __printf_fphex (s, &info, &ptr); \
854 ptr = (const void *) &args_value[fspec->data_arg]; \
856 fspec->info.is_long_double = 0; \
858 function_done = __printf_fphex (s, &fspec->info, &ptr); \
861 if (function_done < 0) \
863 /* Error in print handler. */ \
868 done += function_done; \
872 LABEL (form_pointer): \
873 /* Generic pointer. */ \
877 ptr = va_arg (ap, void *); \
879 ptr = args_value[fspec->data_arg].pa_pointer; \
882 /* If the pointer is not NULL, write it as a %#x spec. */ \
884 number.word = (unsigned long int) ptr; \
889 goto LABEL (number); \
893 /* Write "(nil)" for a nil pointer. */ \
894 string = (CHAR_T *) L_("(nil)"); \
895 /* Make sure the full string "(nil)" is printed. */ \
898 is_long = 0; /* This is no wide-char string. */ \
899 goto LABEL (print_string); \
904 LABEL (form_number): \
905 if (s->_flags2 & _IO_FLAGS2_FORTIFY) \
907 if (! readonly_format) \
909 extern int __readonly_area (const void *, size_t) \
912 = __readonly_area (format, ((STR_LEN (format) + 1) \
913 * sizeof (CHAR_T))); \
915 if (readonly_format < 0) \
916 __libc_fatal ("*** %n in writable segment detected ***\n"); \
918 /* Answer the count of characters written. */ \
922 *(long long int *) va_arg (ap, void *) = done; \
923 else if (is_long_num) \
924 *(long int *) va_arg (ap, void *) = done; \
926 *(char *) va_arg (ap, void *) = done; \
927 else if (!is_short) \
928 *(int *) va_arg (ap, void *) = done; \
930 *(short int *) va_arg (ap, void *) = done; \
934 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
935 else if (is_long_num) \
936 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
938 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
939 else if (!is_short) \
940 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
942 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
945 LABEL (form_strerror): \
946 /* Print description of error ERRNO. */ \
948 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
949 sizeof work_buffer); \
950 is_long = 0; /* This is no wide-char string. */ \
951 goto LABEL (print_string)
953 #ifdef COMPILE_WPRINTF
954 # define process_string_arg(fspec) \
955 LABEL (form_character): \
958 goto LABEL (form_wcharacter); \
959 --width; /* Account for the character itself. */ \
963 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
965 outchar (__btowc ((unsigned char) \
966 args_value[fspec->data_arg].pa_int)); \
971 LABEL (form_wcharacter): \
973 /* Wide character. */ \
978 outchar (va_arg (ap, wchar_t)); \
980 outchar (args_value[fspec->data_arg].pa_wchar); \
986 LABEL (form_string): \
989 int string_malloced; \
991 /* The string argument could in fact be `char *' or `wchar_t *'. \
992 But this should not make a difference here. */ \
994 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
996 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
998 /* Entry point for printing other strings. */ \
999 LABEL (print_string): \
1001 string_malloced = 0; \
1002 if (string == NULL) \
1004 /* Write "(null)" if there's space. */ \
1006 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
1008 string = (CHAR_T *) null; \
1009 len = (sizeof (null) / sizeof (null[0])) - 1; \
1013 string = (CHAR_T *) L""; \
1017 else if (!is_long && spec != L_('S')) \
1019 /* This is complicated. We have to transform the multibyte \
1020 string into a wide character string. */ \
1021 const char *mbs = (const char *) string; \
1022 mbstate_t mbstate; \
1024 len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
1026 /* Allocate dynamically an array which definitely is long \
1027 enough for the wide character version. Each byte in the \
1028 multi-byte string can produce at most one wide character. */ \
1029 if (__libc_use_alloca (len * sizeof (wchar_t))) \
1030 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1031 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1038 string_malloced = 1; \
1040 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1041 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1042 if (len == (size_t) -1) \
1044 /* Illegal multibyte character. */ \
1052 /* Search for the end of the string, but don't search past \
1053 the length specified by the precision. */ \
1054 len = __wcsnlen (string, prec); \
1056 len = __wcslen (string); \
1059 if ((width -= len) < 0) \
1061 outstring (string, len); \
1067 outstring (string, len); \
1070 if (__builtin_expect (string_malloced, 0)) \
1075 # define process_string_arg(fspec) \
1076 LABEL (form_character): \
1079 goto LABEL (form_wcharacter); \
1080 --width; /* Account for the character itself. */ \
1083 if (fspec == NULL) \
1084 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1086 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1091 LABEL (form_wcharacter): \
1093 /* Wide character. */ \
1094 char buf[MB_CUR_MAX]; \
1095 mbstate_t mbstate; \
1098 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1099 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1100 : args_value[fspec->data_arg].pa_wchar), \
1102 if (len == (size_t) -1) \
1104 /* Something went wron gduring the conversion. Bail out. */ \
1111 outstring (buf, len); \
1117 LABEL (form_string): \
1120 int string_malloced; \
1122 /* The string argument could in fact be `char *' or `wchar_t *'. \
1123 But this should not make a difference here. */ \
1124 if (fspec == NULL) \
1125 string = (char *) va_arg (ap, const char *); \
1127 string = (char *) args_value[fspec->data_arg].pa_string; \
1129 /* Entry point for printing other strings. */ \
1130 LABEL (print_string): \
1132 string_malloced = 0; \
1133 if (string == NULL) \
1135 /* Write "(null)" if there's space. */ \
1136 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1138 string = (char *) null; \
1139 len = sizeof (null) - 1; \
1143 string = (char *) ""; \
1147 else if (!is_long && spec != L_('S')) \
1151 /* Search for the end of the string, but don't search past \
1152 the length (in bytes) specified by the precision. Also \
1153 don't use incomplete characters. */ \
1154 if (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MB_CUR_MAX) == 1) \
1155 len = __strnlen (string, prec); \
1158 /* In case we have a multibyte character set the \
1159 situation is more complicated. We must not copy \
1160 bytes at the end which form an incomplete character. */\
1161 size_t ignore_size = (unsigned) prec > 1024 ? 1024 : prec;\
1162 wchar_t ignore[ignore_size]; \
1163 const char *str2 = string; \
1164 const char *strend = string + prec; \
1165 if (strend < string) \
1166 strend = (const char *) UINTPTR_MAX; \
1169 memset (&ps, '\0', sizeof (ps)); \
1171 while (str2 != NULL && str2 < strend) \
1172 if (__mbsnrtowcs (ignore, &str2, strend - str2, \
1173 ignore_size, &ps) == (size_t) -1) \
1180 len = strlen (string); \
1182 len = str2 - string - (ps.__count & 7); \
1186 len = strlen (string); \
1190 const wchar_t *s2 = (const wchar_t *) string; \
1191 mbstate_t mbstate; \
1193 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1197 /* The string `s2' might not be NUL terminated. */ \
1198 if (__libc_use_alloca (prec)) \
1199 string = (char *) alloca (prec); \
1200 else if ((string = (char *) malloc (prec)) == NULL) \
1206 string_malloced = 1; \
1207 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1211 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1212 if (len != (size_t) -1) \
1214 assert (__mbsinit (&mbstate)); \
1215 s2 = (const wchar_t *) string; \
1216 if (__libc_use_alloca (len + 1)) \
1217 string = (char *) alloca (len + 1); \
1218 else if ((string = (char *) malloc (len + 1)) == NULL) \
1224 string_malloced = 1; \
1225 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1229 if (len == (size_t) -1) \
1231 /* Illegal wide-character string. */ \
1237 if ((width -= len) < 0) \
1239 outstring (string, len); \
1245 outstring (string, len); \
1248 if (__builtin_expect (string_malloced, 0)) \
1254 /* Orient the stream. */
1259 /* Sanity check of arguments. */
1260 ARGCHECK (s
, format
);
1263 /* Check for correct orientation. */
1264 if (_IO_vtable_offset (s
) == 0 &&
1265 _IO_fwide (s
, sizeof (CHAR_T
) == 1 ? -1 : 1)
1266 != (sizeof (CHAR_T
) == 1 ? -1 : 1))
1267 /* The stream is already oriented otherwise. */
1271 if (UNBUFFERED_P (s
))
1272 /* Use a helper function which will allocate a local temporary buffer
1273 for the stream and then call us again. */
1274 return buffered_vfprintf (s
, format
, ap
);
1276 /* Initialize local variables. */
1278 grouping
= (const char *) -1;
1280 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1281 since on some systems `va_list' is not an integral type. */
1282 __va_copy (ap_save
, ap
);
1288 #ifdef COMPILE_WPRINTF
1289 /* Find the first format specifier. */
1290 f
= lead_str_end
= __find_specwc ((const UCHAR_T
*) format
);
1292 /* Find the first format specifier. */
1293 f
= lead_str_end
= __find_specmb ((const UCHAR_T
*) format
);
1297 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile
, s
);
1300 /* Write the literal text before the first format. */
1301 outstring ((const UCHAR_T
*) format
,
1302 lead_str_end
- (const UCHAR_T
*) format
);
1304 /* If we only have to print a simple string, return now. */
1308 /* Process whole format string. */
1312 # define REF(Name) &&do_##Name - &&do_form_unknown
1314 # define REF(Name) &&do_##Name
1316 #define LABEL(Name) do_##Name
1320 union printf_arg
*args_value
; /* This is not used here but ... */
1321 int is_negative
; /* Flag for negative number. */
1324 unsigned long long int longlong
;
1325 unsigned long int word
;
1328 union printf_arg the_arg
;
1329 CHAR_T
*string
; /* Pointer to argument string. */
1330 int alt
= 0; /* Alternate format. */
1331 int space
= 0; /* Use space prefix if no sign is needed. */
1332 int left
= 0; /* Left-justify output. */
1333 int showsign
= 0; /* Always begin with plus or minus sign. */
1334 int group
= 0; /* Print numbers according grouping rules. */
1335 int is_long_double
= 0; /* Argument is long double/ long long int. */
1336 int is_short
= 0; /* Argument is short int. */
1337 int is_long
= 0; /* Argument is long int. */
1338 int is_char
= 0; /* Argument is promoted (unsigned) char. */
1339 int width
= 0; /* Width of output; 0 means none specified. */
1340 int prec
= -1; /* Precision of output; -1 means none specified. */
1341 /* This flag is set by the 'I' modifier and selects the use of the
1342 `outdigits' as determined by the current locale. */
1343 int use_outdigits
= 0;
1344 UCHAR_T pad
= L_(' ');/* Padding character. */
1348 workend
= &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
1350 /* Get current character in format string. */
1351 JUMP (*++f
, step0_jumps
);
1356 JUMP (*++f
, step0_jumps
);
1361 JUMP (*++f
, step0_jumps
);
1367 JUMP (*++f
, step0_jumps
);
1372 JUMP (*++f
, step0_jumps
);
1378 JUMP (*++f
, step0_jumps
);
1380 /* The '\'' flag. */
1384 if (grouping
== (const char *) -1)
1386 #ifdef COMPILE_WPRINTF
1387 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1388 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1390 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1393 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1394 if (*grouping
== '\0' || *grouping
== CHAR_MAX
1395 #ifdef COMPILE_WPRINTF
1396 || thousands_sep
== L
'\0'
1398 || *thousands_sep
== '\0'
1403 JUMP (*++f
, step0_jumps
);
1407 JUMP (*++f
, step0_jumps
);
1409 /* Get width from argument. */
1410 LABEL (width_asterics
):
1412 const UCHAR_T
*tmp
; /* Temporary value. */
1415 if (ISDIGIT (*tmp
) && read_int (&tmp
) && *tmp
== L_('$'))
1416 /* The width comes from a positional parameter. */
1419 width
= va_arg (ap
, int);
1421 /* Negative width means left justified. */
1429 if (width
+ 32 >= (int) (sizeof (work_buffer
)
1430 / sizeof (work_buffer
[0])))
1432 /* We have to use a special buffer. The "32" is just a safe
1433 bet for all the output which is not counted in the width. */
1434 if (__libc_use_alloca ((width
+ 32) * sizeof (CHAR_T
)))
1435 workend
= ((CHAR_T
*) alloca ((width
+ 32) * sizeof (CHAR_T
))
1439 workstart
= (CHAR_T
*) malloc ((width
+ 32) * sizeof (CHAR_T
));
1440 if (workstart
== NULL
)
1445 workend
= workstart
+ (width
+ 32);
1449 JUMP (*f
, step1_jumps
);
1451 /* Given width in format string. */
1453 width
= read_int (&f
);
1455 if (width
+ 32 >= (int) (sizeof (work_buffer
) / sizeof (work_buffer
[0])))
1457 /* We have to use a special buffer. The "32" is just a safe
1458 bet for all the output which is not counted in the width. */
1459 if (__libc_use_alloca ((width
+ 32) * sizeof (CHAR_T
)))
1460 workend
= ((CHAR_T
*) alloca ((width
+ 32) * sizeof (CHAR_T
))
1464 workstart
= (CHAR_T
*) malloc ((width
+ 32) * sizeof (CHAR_T
));
1465 if (workstart
== NULL
)
1470 workend
= workstart
+ (width
+ 32);
1474 /* Oh, oh. The argument comes from a positional parameter. */
1476 JUMP (*f
, step1_jumps
);
1482 const UCHAR_T
*tmp
; /* Temporary value. */
1485 if (ISDIGIT (*tmp
) && read_int (&tmp
) > 0 && *tmp
== L_('$'))
1486 /* The precision comes from a positional parameter. */
1489 prec
= va_arg (ap
, int);
1491 /* If the precision is negative the precision is omitted. */
1495 else if (ISDIGIT (*f
))
1496 prec
= read_int (&f
);
1500 && prec
+ 32 > (int)(sizeof (work_buffer
) / sizeof (work_buffer
[0])))
1502 if (__libc_use_alloca ((prec
+ 32) * sizeof (CHAR_T
)))
1503 workend
= ((CHAR_T
*) alloca ((prec
+ 32) * sizeof (CHAR_T
)))
1507 workstart
= (CHAR_T
*) malloc ((prec
+ 32) * sizeof (CHAR_T
));
1508 if (workstart
== NULL
)
1513 workend
= workstart
+ (prec
+ 32);
1516 JUMP (*f
, step2_jumps
);
1518 /* Process 'h' modifier. There might another 'h' following. */
1521 JUMP (*++f
, step3a_jumps
);
1523 /* Process 'hh' modifier. */
1524 LABEL (mod_halfhalf
):
1527 JUMP (*++f
, step4_jumps
);
1529 /* Process 'l' modifier. There might another 'l' following. */
1532 JUMP (*++f
, step3b_jumps
);
1534 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1535 allowed to follow. */
1536 LABEL (mod_longlong
):
1539 JUMP (*++f
, step4_jumps
);
1542 is_long_double
= sizeof (size_t) > sizeof (unsigned long int);
1543 is_long
= sizeof (size_t) > sizeof (unsigned int);
1544 JUMP (*++f
, step4_jumps
);
1546 LABEL (mod_ptrdiff_t
):
1547 is_long_double
= sizeof (ptrdiff_t) > sizeof (unsigned long int);
1548 is_long
= sizeof (ptrdiff_t) > sizeof (unsigned int);
1549 JUMP (*++f
, step4_jumps
);
1551 LABEL (mod_intmax_t
):
1552 is_long_double
= sizeof (intmax_t) > sizeof (unsigned long int);
1553 is_long
= sizeof (intmax_t) > sizeof (unsigned int);
1554 JUMP (*++f
, step4_jumps
);
1556 /* Process current format. */
1559 process_arg (((struct printf_spec
*) NULL
));
1560 process_string_arg (((struct printf_spec
*) NULL
));
1562 LABEL (form_unknown
):
1563 if (spec
== L_('\0'))
1565 /* The format string ended before the specifier is complete. */
1570 /* If we are in the fast loop force entering the complicated
1575 /* The format is correctly handled. */
1578 if (__builtin_expect (workstart
!= NULL
, 0))
1582 /* Look for next format specifier. */
1583 #ifdef COMPILE_WPRINTF
1584 f
= __find_specwc ((end_of_spec
= ++f
));
1586 f
= __find_specmb ((end_of_spec
= ++f
));
1589 /* Write the following constant string. */
1590 outstring (end_of_spec
, f
- end_of_spec
);
1592 while (*f
!= L_('\0'));
1594 /* Unlock stream and return. */
1597 /* Here starts the more complex loop to handle positional parameters. */
1600 /* Array with information about the needed arguments. This has to
1601 be dynamically extensible. */
1603 size_t nspecs_max
= 32; /* A more or less arbitrary start value. */
1604 struct printf_spec
*specs
1605 = alloca (nspecs_max
* sizeof (struct printf_spec
));
1607 /* The number of arguments the format string requests. This will
1608 determine the size of the array needed to store the argument
1612 union printf_arg
*args_value
= NULL
;
1614 /* Positional parameters refer to arguments directly. This could
1615 also determine the maximum number of arguments. Track the
1617 size_t max_ref_arg
= 0;
1619 /* Just a counter. */
1625 if (grouping
== (const char *) -1)
1627 #ifdef COMPILE_WPRINTF
1628 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1629 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1631 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1634 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1635 if (*grouping
== '\0' || *grouping
== CHAR_MAX
)
1639 for (f
= lead_str_end
; *f
!= L_('\0'); f
= specs
[nspecs
++].next_fmt
)
1641 if (nspecs
>= nspecs_max
)
1643 /* Extend the array of format specifiers. */
1644 struct printf_spec
*old
= specs
;
1647 specs
= alloca (nspecs_max
* sizeof (struct printf_spec
));
1649 if (specs
== &old
[nspecs
])
1650 /* Stack grows up, OLD was the last thing allocated;
1652 nspecs_max
+= nspecs_max
/ 2;
1655 /* Copy the old array's elements to the new space. */
1656 memcpy (specs
, old
, nspecs
* sizeof (struct printf_spec
));
1657 if (old
== &specs
[nspecs
])
1658 /* Stack grows down, OLD was just below the new
1659 SPECS. We can use that space when the new space
1661 nspecs_max
+= nspecs_max
/ 2;
1665 /* Parse the format specifier. */
1666 #ifdef COMPILE_WPRINTF
1667 nargs
+= __parse_one_specwc (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1669 nargs
+= __parse_one_specmb (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1673 /* Determine the number of arguments the format string consumes. */
1674 nargs
= MAX (nargs
, max_ref_arg
);
1676 /* Allocate memory for the argument descriptions. */
1677 args_type
= alloca (nargs
* sizeof (int));
1678 memset (args_type
, s
->_flags2
& _IO_FLAGS2_FORTIFY
? '\xff' : '\0',
1679 nargs
* sizeof (int));
1680 args_value
= alloca (nargs
* sizeof (union printf_arg
));
1682 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1683 still zero after this loop, format is invalid. For now we
1684 simply use 0 as the value. */
1686 /* Fill in the types of all the arguments. */
1687 for (cnt
= 0; cnt
< nspecs
; ++cnt
)
1689 /* If the width is determined by an argument this is an int. */
1690 if (specs
[cnt
].width_arg
!= -1)
1691 args_type
[specs
[cnt
].width_arg
] = PA_INT
;
1693 /* If the precision is determined by an argument this is an int. */
1694 if (specs
[cnt
].prec_arg
!= -1)
1695 args_type
[specs
[cnt
].prec_arg
] = PA_INT
;
1697 switch (specs
[cnt
].ndata_args
)
1699 case 0: /* No arguments. */
1701 case 1: /* One argument; we already have the type. */
1702 args_type
[specs
[cnt
].data_arg
] = specs
[cnt
].data_arg_type
;
1705 /* We have more than one argument for this format spec.
1706 We must call the arginfo function again to determine
1708 (void) (*__printf_arginfo_table
[specs
[cnt
].info
.spec
])
1710 specs
[cnt
].ndata_args
, &args_type
[specs
[cnt
].data_arg
]);
1715 /* Now we know all the types and the order. Fill in the argument
1717 for (cnt
= 0; cnt
< nargs
; ++cnt
)
1718 switch (args_type
[cnt
])
1720 #define T(tag, mem, type) \
1722 args_value[cnt].mem = va_arg (ap_save, type); \
1725 T (PA_CHAR
, pa_int
, int); /* Promoted. */
1726 T (PA_WCHAR
, pa_wchar
, wint_t);
1727 T (PA_INT
|PA_FLAG_SHORT
, pa_int
, int); /* Promoted. */
1728 T (PA_INT
, pa_int
, int);
1729 T (PA_INT
|PA_FLAG_LONG
, pa_long_int
, long int);
1730 T (PA_INT
|PA_FLAG_LONG_LONG
, pa_long_long_int
, long long int);
1731 T (PA_FLOAT
, pa_double
, double); /* Promoted. */
1732 T (PA_DOUBLE
, pa_double
, double);
1733 case PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
:
1736 args_value
[cnt
].pa_double
= va_arg (ap_save
, double);
1737 args_type
[cnt
] &= ~PA_FLAG_LONG_DOUBLE
;
1740 args_value
[cnt
].pa_long_double
= va_arg (ap_save
, long double);
1742 T (PA_STRING
, pa_string
, const char *);
1743 T (PA_WSTRING
, pa_wstring
, const wchar_t *);
1744 T (PA_POINTER
, pa_pointer
, void *);
1747 if ((args_type
[cnt
] & PA_FLAG_PTR
) != 0)
1748 args_value
[cnt
].pa_pointer
= va_arg (ap_save
, void *);
1750 args_value
[cnt
].pa_long_double
= 0.0;
1753 /* Error case. Not all parameters appear in N$ format
1754 strings. We have no way to determine their type. */
1755 assert (s
->_flags2
& _IO_FLAGS2_FORTIFY
);
1756 __libc_fatal ("*** invalid %N$ use detected ***\n");
1759 /* Now walk through all format specifiers and process them. */
1760 for (; (size_t) nspecs_done
< nspecs
; ++nspecs_done
)
1764 # define REF(Name) &&do2_##Name - &&do_form_unknown
1766 # define REF(Name) &&do2_##Name
1769 #define LABEL(Name) do2_##Name
1775 unsigned long long int longlong
;
1776 unsigned long int word
;
1779 union printf_arg the_arg
;
1780 CHAR_T
*string
; /* Pointer to argument string. */
1782 /* Fill variables from values in struct. */
1783 int alt
= specs
[nspecs_done
].info
.alt
;
1784 int space
= specs
[nspecs_done
].info
.space
;
1785 int left
= specs
[nspecs_done
].info
.left
;
1786 int showsign
= specs
[nspecs_done
].info
.showsign
;
1787 int group
= specs
[nspecs_done
].info
.group
;
1788 int is_long_double
= specs
[nspecs_done
].info
.is_long_double
;
1789 int is_short
= specs
[nspecs_done
].info
.is_short
;
1790 int is_char
= specs
[nspecs_done
].info
.is_char
;
1791 int is_long
= specs
[nspecs_done
].info
.is_long
;
1792 int width
= specs
[nspecs_done
].info
.width
;
1793 int prec
= specs
[nspecs_done
].info
.prec
;
1794 int use_outdigits
= specs
[nspecs_done
].info
.i18n
;
1795 char pad
= specs
[nspecs_done
].info
.pad
;
1796 CHAR_T spec
= specs
[nspecs_done
].info
.spec
;
1799 workend
= &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
1801 /* Fill in last information. */
1802 if (specs
[nspecs_done
].width_arg
!= -1)
1804 /* Extract the field width from an argument. */
1805 specs
[nspecs_done
].info
.width
=
1806 args_value
[specs
[nspecs_done
].width_arg
].pa_int
;
1808 if (specs
[nspecs_done
].info
.width
< 0)
1809 /* If the width value is negative left justification is
1810 selected and the value is taken as being positive. */
1812 specs
[nspecs_done
].info
.width
*= -1;
1813 left
= specs
[nspecs_done
].info
.left
= 1;
1815 width
= specs
[nspecs_done
].info
.width
;
1818 if (specs
[nspecs_done
].prec_arg
!= -1)
1820 /* Extract the precision from an argument. */
1821 specs
[nspecs_done
].info
.prec
=
1822 args_value
[specs
[nspecs_done
].prec_arg
].pa_int
;
1824 if (specs
[nspecs_done
].info
.prec
< 0)
1825 /* If the precision is negative the precision is
1827 specs
[nspecs_done
].info
.prec
= -1;
1829 prec
= specs
[nspecs_done
].info
.prec
;
1832 /* Maybe the buffer is too small. */
1833 if (MAX (prec
, width
) + 32 > (int) (sizeof (work_buffer
)
1836 if (__libc_use_alloca ((MAX (prec
, width
) + 32)
1838 workend
= ((CHAR_T
*) alloca ((MAX (prec
, width
) + 32)
1840 + (MAX (prec
, width
) + 32));
1843 workstart
= (CHAR_T
*) malloc ((MAX (prec
, width
) + 32)
1845 workend
= workstart
+ (MAX (prec
, width
) + 32);
1849 /* Process format specifiers. */
1852 JUMP (spec
, step4_jumps
);
1854 process_arg ((&specs
[nspecs_done
]));
1855 process_string_arg ((&specs
[nspecs_done
]));
1857 LABEL (form_unknown
):
1859 extern printf_function
**__printf_function_table
;
1861 printf_function
*function
;
1866 (__printf_function_table
== NULL
? NULL
:
1867 __printf_function_table
[specs
[nspecs_done
].info
.spec
]);
1869 if (function
== NULL
)
1870 function
= &printf_unknown
;
1872 ptr
= alloca (specs
[nspecs_done
].ndata_args
1873 * sizeof (const void *));
1875 /* Fill in an array of pointers to the argument values. */
1876 for (i
= 0; i
< specs
[nspecs_done
].ndata_args
; ++i
)
1877 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
1879 /* Call the function. */
1880 function_done
= (*function
) (s
, &specs
[nspecs_done
].info
, ptr
);
1882 /* If an error occurred we don't have information about #
1884 if (function_done
< 0)
1890 done
+= function_done
;
1898 /* Write the following constant string. */
1899 outstring (specs
[nspecs_done
].end_of_fmt
,
1900 specs
[nspecs_done
].next_fmt
1901 - specs
[nspecs_done
].end_of_fmt
);
1906 if (__builtin_expect (workstart
!= NULL
, 0))
1908 /* Unlock the stream. */
1909 _IO_funlockfile (s
);
1910 _IO_cleanup_region_end (0);
1915 /* Handle an unknown format specifier. This prints out a canonicalized
1916 representation of the format spec itself. */
1918 printf_unknown (FILE *s
, const struct printf_info
*info
,
1919 const void *const *args
)
1923 CHAR_T work_buffer
[MAX (sizeof (info
->width
), sizeof (info
->prec
)) * 3];
1924 CHAR_T
*const workend
1925 = &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
1936 else if (info
->space
)
1940 if (info
->pad
== L_('0'))
1945 if (info
->width
!= 0)
1947 w
= _itoa_word (info
->width
, workend
, 10, 0);
1952 if (info
->prec
!= -1)
1955 w
= _itoa_word (info
->prec
, workend
, 10, 0);
1960 if (info
->spec
!= L_('\0'))
1961 outchar (info
->spec
);
1967 /* Group the digits according to the grouping rules of the current locale.
1968 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1971 group_number (CHAR_T
*w
, CHAR_T
*rear_ptr
, const char *grouping
,
1972 #ifdef COMPILE_WPRINTF
1973 wchar_t thousands_sep
1975 const char *thousands_sep
1981 #ifndef COMPILE_WPRINTF
1982 int tlen
= strlen (thousands_sep
);
1985 /* We treat all negative values like CHAR_MAX. */
1987 if (*grouping
== CHAR_MAX
|| *grouping
<= 0)
1988 /* No grouping should be done. */
1993 /* Copy existing string so that nothing gets overwritten. */
1994 src
= (CHAR_T
*) alloca ((rear_ptr
- w
) * sizeof (CHAR_T
));
1995 s
= (CHAR_T
*) __mempcpy (src
, w
,
1996 (rear_ptr
- w
) * sizeof (CHAR_T
));
1999 /* Process all characters in the string. */
2004 if (--len
== 0 && s
> src
)
2006 /* A new group begins. */
2007 #ifdef COMPILE_WPRINTF
2008 *--w
= thousands_sep
;
2012 *--w
= thousands_sep
[--cnt
];
2016 if (*grouping
== CHAR_MAX
2022 /* No further grouping to be done.
2023 Copy the rest of the number. */
2029 else if (*grouping
!= '\0')
2030 /* The previous grouping repeats ad infinitum. */
2039 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2042 struct _IO_FILE_plus _f
;
2043 #ifdef COMPILE_WPRINTF
2044 struct _IO_wide_data _wide_data
;
2046 _IO_FILE
*_put_stream
;
2047 #ifdef _IO_MTSAFE_IO
2053 _IO_helper_overflow (_IO_FILE
*s
, int c
)
2055 _IO_FILE
*target
= ((struct helper_file
*) s
)->_put_stream
;
2056 #ifdef COMPILE_WPRINTF
2057 int used
= s
->_wide_data
->_IO_write_ptr
- s
->_wide_data
->_IO_write_base
;
2060 _IO_size_t written
= _IO_sputn (target
, s
->_wide_data
->_IO_write_base
,
2062 s
->_wide_data
->_IO_write_ptr
-= written
;
2065 int used
= s
->_IO_write_ptr
- s
->_IO_write_base
;
2068 _IO_size_t written
= _IO_sputn (target
, s
->_IO_write_base
, used
);
2069 s
->_IO_write_ptr
-= written
;
2075 #ifdef COMPILE_WPRINTF
2076 static const struct _IO_jump_t _IO_helper_jumps
=
2079 JUMP_INIT (finish
, INTUSE(_IO_wdefault_finish
)),
2080 JUMP_INIT (overflow
, _IO_helper_overflow
),
2081 JUMP_INIT (underflow
, _IO_default_underflow
),
2082 JUMP_INIT (uflow
, INTUSE(_IO_default_uflow
)),
2083 JUMP_INIT (pbackfail
, (_IO_pbackfail_t
) INTUSE(_IO_wdefault_pbackfail
)),
2084 JUMP_INIT (xsputn
, INTUSE(_IO_wdefault_xsputn
)),
2085 JUMP_INIT (xsgetn
, INTUSE(_IO_wdefault_xsgetn
)),
2086 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2087 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2088 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2089 JUMP_INIT (sync
, _IO_default_sync
),
2090 JUMP_INIT (doallocate
, INTUSE(_IO_wdefault_doallocate
)),
2091 JUMP_INIT (read
, _IO_default_read
),
2092 JUMP_INIT (write
, _IO_default_write
),
2093 JUMP_INIT (seek
, _IO_default_seek
),
2094 JUMP_INIT (close
, _IO_default_close
),
2095 JUMP_INIT (stat
, _IO_default_stat
)
2098 static const struct _IO_jump_t _IO_helper_jumps
=
2101 JUMP_INIT (finish
, INTUSE(_IO_default_finish
)),
2102 JUMP_INIT (overflow
, _IO_helper_overflow
),
2103 JUMP_INIT (underflow
, _IO_default_underflow
),
2104 JUMP_INIT (uflow
, INTUSE(_IO_default_uflow
)),
2105 JUMP_INIT (pbackfail
, INTUSE(_IO_default_pbackfail
)),
2106 JUMP_INIT (xsputn
, INTUSE(_IO_default_xsputn
)),
2107 JUMP_INIT (xsgetn
, INTUSE(_IO_default_xsgetn
)),
2108 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2109 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2110 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2111 JUMP_INIT (sync
, _IO_default_sync
),
2112 JUMP_INIT (doallocate
, INTUSE(_IO_default_doallocate
)),
2113 JUMP_INIT (read
, _IO_default_read
),
2114 JUMP_INIT (write
, _IO_default_write
),
2115 JUMP_INIT (seek
, _IO_default_seek
),
2116 JUMP_INIT (close
, _IO_default_close
),
2117 JUMP_INIT (stat
, _IO_default_stat
)
2123 buffered_vfprintf (register _IO_FILE
*s
, const CHAR_T
*format
,
2126 CHAR_T buf
[_IO_BUFSIZ
];
2127 struct helper_file helper
;
2128 register _IO_FILE
*hp
= (_IO_FILE
*) &helper
._f
;
2129 int result
, to_flush
;
2131 /* Orient the stream. */
2136 /* Initialize helper. */
2137 helper
._put_stream
= s
;
2138 #ifdef COMPILE_WPRINTF
2139 hp
->_wide_data
= &helper
._wide_data
;
2140 _IO_wsetp (hp
, buf
, buf
+ sizeof buf
/ sizeof (CHAR_T
));
2143 _IO_setp (hp
, buf
, buf
+ sizeof buf
);
2146 hp
->_IO_file_flags
= _IO_MAGIC
|_IO_NO_READS
|_IO_USER_LOCK
;
2147 #if _IO_JUMPS_OFFSET
2148 hp
->_vtable_offset
= 0;
2150 #ifdef _IO_MTSAFE_IO
2153 hp
->_flags2
= s
->_flags2
;
2154 _IO_JUMPS (&helper
._f
) = (struct _IO_jump_t
*) &_IO_helper_jumps
;
2156 /* Now print to helper instead. */
2157 #ifndef COMPILE_WPRINTF
2158 result
= INTUSE(_IO_vfprintf
) (hp
, format
, args
);
2160 result
= vfprintf (hp
, format
, args
);
2164 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile
, s
);
2167 /* Now flush anything from the helper to the S. */
2168 #ifdef COMPILE_WPRINTF
2169 if ((to_flush
= (hp
->_wide_data
->_IO_write_ptr
2170 - hp
->_wide_data
->_IO_write_base
)) > 0)
2172 if ((int) _IO_sputn (s
, hp
->_wide_data
->_IO_write_base
, to_flush
)
2177 if ((to_flush
= hp
->_IO_write_ptr
- hp
->_IO_write_base
) > 0)
2179 if ((int) _IO_sputn (s
, hp
->_IO_write_base
, to_flush
) != to_flush
)
2184 /* Unlock the stream. */
2185 _IO_funlockfile (s
);
2186 __libc_cleanup_region_end (0);
2192 #ifdef COMPILE_WPRINTF
2193 strong_alias (_IO_vfwprintf
, __vfwprintf
);
2194 ldbl_weak_alias (_IO_vfwprintf
, vfwprintf
);
2196 ldbl_strong_alias (_IO_vfprintf_internal
, vfprintf
);
2197 ldbl_hidden_def (_IO_vfprintf_internal
, vfprintf
)
2198 ldbl_strong_alias (_IO_vfprintf_internal
, _IO_vfprintf
);