1 /* Copyright (C) 1991-2002, 2003, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 #include <bits/libc-lock.h>
29 #include <sys/param.h>
31 #include <locale/localeinfo.h>
34 /* This code is shared between the standard stdio implementation found
35 in GNU C library and the libio implementation originally found in
38 Beside this it is also shared between the normal and wide character
39 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
45 #define va_list _IO_va_list
47 #define BUFSIZ _IO_BUFSIZ
48 #define ARGCHECK(S, Format) \
51 /* Check file argument for consistence. */ \
53 if (S->_flags & _IO_NO_WRITES) \
55 __set_errno (EBADF); \
64 #define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
66 #ifndef COMPILE_WPRINTF
67 # define vfprintf _IO_vfprintf
69 # define UCHAR_T unsigned char
72 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
73 # define STR_LEN(Str) strlen (Str)
75 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
76 # define PAD(Padchar) \
78 done += INTUSE(_IO_padn) (s, (Padchar), width)
79 # define PUTC(C, F) _IO_putc_unlocked (C, F)
80 # define ORIENT if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
83 # define vfprintf _IO_vfwprintf
84 # define CHAR_T wchar_t
85 /* This is a hack!!! There should be a type uwchar_t. */
86 # define UCHAR_T unsigned int /* uwchar_t */
88 # define L_(Str) L##Str
89 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
90 # define STR_LEN(Str) __wcslen (Str)
94 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
95 # define PAD(Padchar) \
97 done += _IO_wpadn (s, (Padchar), width)
98 # define PUTC(C, F) _IO_putwc_unlocked (C, F)
99 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
101 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
102 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
107 #include "_i18n_number.h"
109 /* Include the shared code for parsing the format string. */
110 #include "printf-parse.h"
113 #define outchar(Ch) \
116 register const INT_T outc = (Ch); \
117 if (PUTC (outc, s) == EOF) \
127 #define outstring(String, Len) \
130 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
139 /* For handling long_double and longlong we use the same flag. If
140 `long' and `long long' are effectively the same type define it to
142 #if LONG_MAX == LONG_LONG_MAX
143 # define is_longlong 0
145 # define is_longlong is_long_double
148 /* If `long' and `int' is effectively the same type we don't have to
149 handle `long separately. */
150 #if INT_MAX == LONG_MAX
151 # define is_long_num 0
153 # define is_long_num is_long
157 /* Global variables. */
158 static const CHAR_T null
[] = L_("(null)");
161 /* Helper function to provide temporary buffering for unbuffered streams. */
162 static int buffered_vfprintf (FILE *stream
, const CHAR_T
*fmt
, va_list)
163 __THROW
__attribute__ ((noinline
)) internal_function
;
165 /* Handle unknown format specifier. */
166 static int printf_unknown (FILE *, const struct printf_info
*,
167 const void *const *) __THROW
;
169 /* Group digits of number string. */
170 #ifdef COMPILE_WPRINTF
171 static CHAR_T
*group_number (CHAR_T
*, CHAR_T
*, const char *, wchar_t)
172 __THROW internal_function
;
174 static CHAR_T
*group_number (CHAR_T
*, CHAR_T
*, const char *, const char *)
175 __THROW internal_function
;
179 /* The function itself. */
181 vfprintf (FILE *s
, const CHAR_T
*format
, va_list ap
)
183 /* The character used as thousands separator. */
184 #ifdef COMPILE_WPRINTF
185 wchar_t thousands_sep
= L
'\0';
187 const char *thousands_sep
= NULL
;
190 /* The string describing the size of groups of digits. */
191 const char *grouping
;
193 /* Place to accumulate the result. */
196 /* Current character in format string. */
199 /* End of leading constant string. */
200 const UCHAR_T
*lead_str_end
;
202 /* Points to next format specifier. */
203 const UCHAR_T
*end_of_spec
;
205 /* Buffer intermediate results. */
206 CHAR_T work_buffer
[1000];
207 CHAR_T
*workstart
= NULL
;
210 /* State for restartable multibyte character handling functions. */
211 #ifndef COMPILE_WPRINTF
215 /* We have to save the original argument pointer. */
218 /* Count number of specifiers we already processed. */
221 /* For the %m format we may need the current `errno' value. */
222 int save_errno
= errno
;
224 /* 1 if format is in read-only memory, -1 if it is in writable memory,
226 int readonly_format
= 0;
228 /* This table maps a character into a number representing a
229 class. In each step there is a destination label for each
231 static const int jump_table
[] =
233 /* ' ' */ 1, 0, 0, /* '#' */ 4,
234 0, /* '%' */ 14, 0, /* '\''*/ 6,
235 0, 0, /* '*' */ 7, /* '+' */ 2,
236 0, /* '-' */ 3, /* '.' */ 9, 0,
237 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
238 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
239 /* '8' */ 8, /* '9' */ 8, 0, 0,
241 0, /* 'A' */ 26, 0, /* 'C' */ 25,
242 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
243 0, /* 'I' */ 29, 0, 0,
244 /* 'L' */ 12, 0, 0, 0,
245 0, 0, 0, /* 'S' */ 21,
247 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
249 0, /* 'a' */ 26, 0, /* 'c' */ 20,
250 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
251 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
252 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
253 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
254 /* 't' */ 27, /* 'u' */ 16, 0, 0,
255 /* 'x' */ 18, 0, /* 'z' */ 13
258 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
259 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
260 #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
261 /* 'int' is enough and it saves some space on 64 bit systems. */
262 # define JUMP_TABLE_TYPE const int
263 # define JUMP(ChExpr, table) \
267 void *__unbounded ptr; \
269 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
270 : table[CHAR_CLASS (spec)]; \
271 ptr = &&do_form_unknown + offset; \
276 # define JUMP_TABLE_TYPE const void *const
277 # define JUMP(ChExpr, table) \
280 const void *__unbounded ptr; \
282 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
283 : table[CHAR_CLASS (spec)]; \
289 #define STEP0_3_TABLE \
290 /* Step 0: at the beginning. */ \
291 static JUMP_TABLE_TYPE step0_jumps[30] = \
293 REF (form_unknown), \
294 REF (flag_space), /* for ' ' */ \
295 REF (flag_plus), /* for '+' */ \
296 REF (flag_minus), /* for '-' */ \
297 REF (flag_hash), /* for '<hash>' */ \
298 REF (flag_zero), /* for '0' */ \
299 REF (flag_quote), /* for '\'' */ \
300 REF (width_asterics), /* for '*' */ \
301 REF (width), /* for '1'...'9' */ \
302 REF (precision), /* for '.' */ \
303 REF (mod_half), /* for 'h' */ \
304 REF (mod_long), /* for 'l' */ \
305 REF (mod_longlong), /* for 'L', 'q' */ \
306 REF (mod_size_t), /* for 'z', 'Z' */ \
307 REF (form_percent), /* for '%' */ \
308 REF (form_integer), /* for 'd', 'i' */ \
309 REF (form_unsigned), /* for 'u' */ \
310 REF (form_octal), /* for 'o' */ \
311 REF (form_hexa), /* for 'X', 'x' */ \
312 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
313 REF (form_character), /* for 'c' */ \
314 REF (form_string), /* for 's', 'S' */ \
315 REF (form_pointer), /* for 'p' */ \
316 REF (form_number), /* for 'n' */ \
317 REF (form_strerror), /* for 'm' */ \
318 REF (form_wcharacter), /* for 'C' */ \
319 REF (form_floathex), /* for 'A', 'a' */ \
320 REF (mod_ptrdiff_t), /* for 't' */ \
321 REF (mod_intmax_t), /* for 'j' */ \
322 REF (flag_i18n), /* for 'I' */ \
324 /* Step 1: after processing width. */ \
325 static JUMP_TABLE_TYPE step1_jumps[30] = \
327 REF (form_unknown), \
328 REF (form_unknown), /* for ' ' */ \
329 REF (form_unknown), /* for '+' */ \
330 REF (form_unknown), /* for '-' */ \
331 REF (form_unknown), /* for '<hash>' */ \
332 REF (form_unknown), /* for '0' */ \
333 REF (form_unknown), /* for '\'' */ \
334 REF (form_unknown), /* for '*' */ \
335 REF (form_unknown), /* for '1'...'9' */ \
336 REF (precision), /* for '.' */ \
337 REF (mod_half), /* for 'h' */ \
338 REF (mod_long), /* for 'l' */ \
339 REF (mod_longlong), /* for 'L', 'q' */ \
340 REF (mod_size_t), /* for 'z', 'Z' */ \
341 REF (form_percent), /* for '%' */ \
342 REF (form_integer), /* for 'd', 'i' */ \
343 REF (form_unsigned), /* for 'u' */ \
344 REF (form_octal), /* for 'o' */ \
345 REF (form_hexa), /* for 'X', 'x' */ \
346 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
347 REF (form_character), /* for 'c' */ \
348 REF (form_string), /* for 's', 'S' */ \
349 REF (form_pointer), /* for 'p' */ \
350 REF (form_number), /* for 'n' */ \
351 REF (form_strerror), /* for 'm' */ \
352 REF (form_wcharacter), /* for 'C' */ \
353 REF (form_floathex), /* for 'A', 'a' */ \
354 REF (mod_ptrdiff_t), /* for 't' */ \
355 REF (mod_intmax_t), /* for 'j' */ \
356 REF (form_unknown) /* for 'I' */ \
358 /* Step 2: after processing precision. */ \
359 static JUMP_TABLE_TYPE step2_jumps[30] = \
361 REF (form_unknown), \
362 REF (form_unknown), /* for ' ' */ \
363 REF (form_unknown), /* for '+' */ \
364 REF (form_unknown), /* for '-' */ \
365 REF (form_unknown), /* for '<hash>' */ \
366 REF (form_unknown), /* for '0' */ \
367 REF (form_unknown), /* for '\'' */ \
368 REF (form_unknown), /* for '*' */ \
369 REF (form_unknown), /* for '1'...'9' */ \
370 REF (form_unknown), /* for '.' */ \
371 REF (mod_half), /* for 'h' */ \
372 REF (mod_long), /* for 'l' */ \
373 REF (mod_longlong), /* for 'L', 'q' */ \
374 REF (mod_size_t), /* for 'z', 'Z' */ \
375 REF (form_percent), /* for '%' */ \
376 REF (form_integer), /* for 'd', 'i' */ \
377 REF (form_unsigned), /* for 'u' */ \
378 REF (form_octal), /* for 'o' */ \
379 REF (form_hexa), /* for 'X', 'x' */ \
380 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
381 REF (form_character), /* for 'c' */ \
382 REF (form_string), /* for 's', 'S' */ \
383 REF (form_pointer), /* for 'p' */ \
384 REF (form_number), /* for 'n' */ \
385 REF (form_strerror), /* for 'm' */ \
386 REF (form_wcharacter), /* for 'C' */ \
387 REF (form_floathex), /* for 'A', 'a' */ \
388 REF (mod_ptrdiff_t), /* for 't' */ \
389 REF (mod_intmax_t), /* for 'j' */ \
390 REF (form_unknown) /* for 'I' */ \
392 /* Step 3a: after processing first 'h' modifier. */ \
393 static JUMP_TABLE_TYPE step3a_jumps[30] = \
395 REF (form_unknown), \
396 REF (form_unknown), /* for ' ' */ \
397 REF (form_unknown), /* for '+' */ \
398 REF (form_unknown), /* for '-' */ \
399 REF (form_unknown), /* for '<hash>' */ \
400 REF (form_unknown), /* for '0' */ \
401 REF (form_unknown), /* for '\'' */ \
402 REF (form_unknown), /* for '*' */ \
403 REF (form_unknown), /* for '1'...'9' */ \
404 REF (form_unknown), /* for '.' */ \
405 REF (mod_halfhalf), /* for 'h' */ \
406 REF (form_unknown), /* for 'l' */ \
407 REF (form_unknown), /* for 'L', 'q' */ \
408 REF (form_unknown), /* for 'z', 'Z' */ \
409 REF (form_percent), /* for '%' */ \
410 REF (form_integer), /* for 'd', 'i' */ \
411 REF (form_unsigned), /* for 'u' */ \
412 REF (form_octal), /* for 'o' */ \
413 REF (form_hexa), /* for 'X', 'x' */ \
414 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
415 REF (form_unknown), /* for 'c' */ \
416 REF (form_unknown), /* for 's', 'S' */ \
417 REF (form_unknown), /* for 'p' */ \
418 REF (form_number), /* for 'n' */ \
419 REF (form_unknown), /* for 'm' */ \
420 REF (form_unknown), /* for 'C' */ \
421 REF (form_unknown), /* for 'A', 'a' */ \
422 REF (form_unknown), /* for 't' */ \
423 REF (form_unknown), /* for 'j' */ \
424 REF (form_unknown) /* for 'I' */ \
426 /* Step 3b: after processing first 'l' modifier. */ \
427 static JUMP_TABLE_TYPE step3b_jumps[30] = \
429 REF (form_unknown), \
430 REF (form_unknown), /* for ' ' */ \
431 REF (form_unknown), /* for '+' */ \
432 REF (form_unknown), /* for '-' */ \
433 REF (form_unknown), /* for '<hash>' */ \
434 REF (form_unknown), /* for '0' */ \
435 REF (form_unknown), /* for '\'' */ \
436 REF (form_unknown), /* for '*' */ \
437 REF (form_unknown), /* for '1'...'9' */ \
438 REF (form_unknown), /* for '.' */ \
439 REF (form_unknown), /* for 'h' */ \
440 REF (mod_longlong), /* for 'l' */ \
441 REF (form_unknown), /* for 'L', 'q' */ \
442 REF (form_unknown), /* for 'z', 'Z' */ \
443 REF (form_percent), /* for '%' */ \
444 REF (form_integer), /* for 'd', 'i' */ \
445 REF (form_unsigned), /* for 'u' */ \
446 REF (form_octal), /* for 'o' */ \
447 REF (form_hexa), /* for 'X', 'x' */ \
448 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
449 REF (form_character), /* for 'c' */ \
450 REF (form_string), /* for 's', 'S' */ \
451 REF (form_pointer), /* for 'p' */ \
452 REF (form_number), /* for 'n' */ \
453 REF (form_strerror), /* for 'm' */ \
454 REF (form_wcharacter), /* for 'C' */ \
455 REF (form_floathex), /* for 'A', 'a' */ \
456 REF (form_unknown), /* for 't' */ \
457 REF (form_unknown), /* for 'j' */ \
458 REF (form_unknown) /* for 'I' */ \
461 #define STEP4_TABLE \
462 /* Step 4: processing format specifier. */ \
463 static JUMP_TABLE_TYPE step4_jumps[30] = \
465 REF (form_unknown), \
466 REF (form_unknown), /* for ' ' */ \
467 REF (form_unknown), /* for '+' */ \
468 REF (form_unknown), /* for '-' */ \
469 REF (form_unknown), /* for '<hash>' */ \
470 REF (form_unknown), /* for '0' */ \
471 REF (form_unknown), /* for '\'' */ \
472 REF (form_unknown), /* for '*' */ \
473 REF (form_unknown), /* for '1'...'9' */ \
474 REF (form_unknown), /* for '.' */ \
475 REF (form_unknown), /* for 'h' */ \
476 REF (form_unknown), /* for 'l' */ \
477 REF (form_unknown), /* for 'L', 'q' */ \
478 REF (form_unknown), /* for 'z', 'Z' */ \
479 REF (form_percent), /* for '%' */ \
480 REF (form_integer), /* for 'd', 'i' */ \
481 REF (form_unsigned), /* for 'u' */ \
482 REF (form_octal), /* for 'o' */ \
483 REF (form_hexa), /* for 'X', 'x' */ \
484 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
485 REF (form_character), /* for 'c' */ \
486 REF (form_string), /* for 's', 'S' */ \
487 REF (form_pointer), /* for 'p' */ \
488 REF (form_number), /* for 'n' */ \
489 REF (form_strerror), /* for 'm' */ \
490 REF (form_wcharacter), /* for 'C' */ \
491 REF (form_floathex), /* for 'A', 'a' */ \
492 REF (form_unknown), /* for 't' */ \
493 REF (form_unknown), /* for 'j' */ \
494 REF (form_unknown) /* for 'I' */ \
498 #define process_arg(fspec) \
499 /* Start real work. We know about all flags and modifiers and \
500 now process the wanted format specifier. */ \
501 LABEL (form_percent): \
502 /* Write a literal "%". */ \
506 LABEL (form_integer): \
507 /* Signed decimal integer. */ \
512 long long int signed_number; \
515 signed_number = va_arg (ap, long long int); \
517 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
519 is_negative = signed_number < 0; \
520 number.longlong = is_negative ? (- signed_number) : signed_number; \
522 goto LABEL (longlong_number); \
526 long int signed_number; \
531 signed_number = va_arg (ap, long int); \
532 else /* `char' and `short int' will be promoted to `int'. */ \
533 signed_number = va_arg (ap, int); \
537 signed_number = args_value[fspec->data_arg].pa_long_int; \
538 else /* `char' and `short int' will be promoted to `int'. */ \
539 signed_number = args_value[fspec->data_arg].pa_int; \
541 is_negative = signed_number < 0; \
542 number.word = is_negative ? (- signed_number) : signed_number; \
544 goto LABEL (number); \
548 LABEL (form_unsigned): \
549 /* Unsigned decimal integer. */ \
551 goto LABEL (unsigned_number); \
554 LABEL (form_octal): \
555 /* Unsigned octal integer. */ \
557 goto LABEL (unsigned_number); \
561 /* Unsigned hexadecimal integer. */ \
564 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
566 /* ISO specifies the `+' and ` ' flags only for signed \
575 number.longlong = va_arg (ap, unsigned long long int); \
577 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
579 LABEL (longlong_number): \
581 /* Supply a default precision if none was given. */ \
584 /* We have to take care for the '0' flag. If a precision \
585 is given it must be ignored. */ \
588 /* If the precision is 0 and the number is 0 nothing has to \
589 be written for the number, except for the 'o' format in \
591 if (prec == 0 && number.longlong == 0) \
594 if (base == 8 && alt) \
595 *--string = L_('0'); \
599 /* Put the number in WORK. */ \
600 string = _itoa (number.longlong, workend, base, \
602 if (group && grouping) \
603 string = group_number (string, workend, grouping, \
606 if (use_outdigits && base == 10) \
607 string = _i18n_number_rewrite (string, workend); \
609 /* Simplify further test for num != 0. */ \
610 number.word = number.longlong != 0; \
617 number.word = va_arg (ap, unsigned long int); \
619 number.word = (unsigned char) va_arg (ap, unsigned int); \
620 else if (!is_short) \
621 number.word = va_arg (ap, unsigned int); \
623 number.word = (unsigned short int) va_arg (ap, unsigned int); \
627 number.word = args_value[fspec->data_arg].pa_u_long_int; \
629 number.word = (unsigned char) \
630 args_value[fspec->data_arg].pa_u_int; \
631 else if (!is_short) \
632 number.word = args_value[fspec->data_arg].pa_u_int; \
634 number.word = (unsigned short int) \
635 args_value[fspec->data_arg].pa_u_int; \
639 /* Supply a default precision if none was given. */ \
642 /* We have to take care for the '0' flag. If a precision \
643 is given it must be ignored. */ \
646 /* If the precision is 0 and the number is 0 nothing has to \
647 be written for the number, except for the 'o' format in \
649 if (prec == 0 && number.word == 0) \
652 if (base == 8 && alt) \
653 *--string = L_('0'); \
657 /* Put the number in WORK. */ \
658 string = _itoa_word (number.word, workend, base, \
660 if (group && grouping) \
661 string = group_number (string, workend, grouping, \
664 if (use_outdigits && base == 10) \
665 string = _i18n_number_rewrite (string, workend); \
669 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
670 /* Add octal marker. */ \
671 *--string = L_('0'); \
673 prec = MAX (0, prec - (workend - string)); \
677 width -= workend - string + prec; \
679 if (number.word != 0 && alt && base == 16) \
680 /* Account for 0X hex marker. */ \
683 if (is_negative || showsign || space) \
686 if (pad == L_(' ')) \
699 if (number.word != 0 && alt && base == 16) \
708 outstring (string, workend - string); \
730 if (number.word != 0 && alt && base == 16) \
737 width -= workend - string + prec; \
747 outstring (string, workend - string); \
753 LABEL (form_float): \
755 /* Floating-point number. This is handled by printf_fp.c. */ \
761 struct printf_info info = { .prec = prec, \
764 .is_long_double = is_long_double, \
765 .is_short = is_short, \
766 .is_long = is_long, \
770 .showsign = showsign, \
774 .i18n = use_outdigits, \
775 .wide = sizeof (CHAR_T) != 1 }; \
777 if (is_long_double) \
778 the_arg.pa_long_double = va_arg (ap, long double); \
780 the_arg.pa_double = va_arg (ap, double); \
781 ptr = (const void *) &the_arg; \
783 function_done = __printf_fp (s, &info, &ptr); \
787 ptr = (const void *) &args_value[fspec->data_arg]; \
789 function_done = __printf_fp (s, &fspec->info, &ptr); \
792 if (function_done < 0) \
794 /* Error in print handler. */ \
799 done += function_done; \
803 LABEL (form_floathex): \
805 /* Floating point number printed as hexadecimal number. */ \
811 struct printf_info info = { .prec = prec, \
814 .is_long_double = is_long_double, \
815 .is_short = is_short, \
816 .is_long = is_long, \
820 .showsign = showsign, \
824 .wide = sizeof (CHAR_T) != 1 }; \
826 if (is_long_double) \
827 the_arg.pa_long_double = va_arg (ap, long double); \
829 the_arg.pa_double = va_arg (ap, double); \
830 ptr = (const void *) &the_arg; \
832 function_done = __printf_fphex (s, &info, &ptr); \
836 ptr = (const void *) &args_value[fspec->data_arg]; \
838 function_done = __printf_fphex (s, &fspec->info, &ptr); \
841 if (function_done < 0) \
843 /* Error in print handler. */ \
848 done += function_done; \
852 LABEL (form_pointer): \
853 /* Generic pointer. */ \
857 ptr = va_arg (ap, void *); \
859 ptr = args_value[fspec->data_arg].pa_pointer; \
862 /* If the pointer is not NULL, write it as a %#x spec. */ \
864 number.word = (unsigned long int) ptr; \
869 goto LABEL (number); \
873 /* Write "(nil)" for a nil pointer. */ \
874 string = (CHAR_T *) L_("(nil)"); \
875 /* Make sure the full string "(nil)" is printed. */ \
878 is_long = 0; /* This is no wide-char string. */ \
879 goto LABEL (print_string); \
884 LABEL (form_number): \
885 if (s->_flags2 & _IO_FLAGS2_FORTIFY) \
887 if (! readonly_format) \
889 extern int __readonly_area (const void *, size_t) \
892 = __readonly_area (format, ((STR_LEN (format) + 1) \
893 * sizeof (CHAR_T))); \
895 if (readonly_format < 0) \
896 __libc_fatal ("*** %n in writable segment detected ***\n"); \
898 /* Answer the count of characters written. */ \
902 *(long long int *) va_arg (ap, void *) = done; \
903 else if (is_long_num) \
904 *(long int *) va_arg (ap, void *) = done; \
906 *(char *) va_arg (ap, void *) = done; \
907 else if (!is_short) \
908 *(int *) va_arg (ap, void *) = done; \
910 *(short int *) va_arg (ap, void *) = done; \
914 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
915 else if (is_long_num) \
916 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
918 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
919 else if (!is_short) \
920 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
922 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
925 LABEL (form_strerror): \
926 /* Print description of error ERRNO. */ \
928 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
929 sizeof work_buffer); \
930 is_long = 0; /* This is no wide-char string. */ \
931 goto LABEL (print_string)
933 #ifdef COMPILE_WPRINTF
934 # define process_string_arg(fspec) \
935 LABEL (form_character): \
938 goto LABEL (form_wcharacter); \
939 --width; /* Account for the character itself. */ \
943 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
945 outchar (__btowc ((unsigned char) \
946 args_value[fspec->data_arg].pa_int)); \
951 LABEL (form_wcharacter): \
953 /* Wide character. */ \
958 outchar (va_arg (ap, wchar_t)); \
960 outchar (args_value[fspec->data_arg].pa_wchar); \
966 LABEL (form_string): \
969 int string_malloced; \
971 /* The string argument could in fact be `char *' or `wchar_t *'. \
972 But this should not make a difference here. */ \
974 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
976 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
978 /* Entry point for printing other strings. */ \
979 LABEL (print_string): \
981 string_malloced = 0; \
982 if (string == NULL) \
984 /* Write "(null)" if there's space. */ \
986 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
988 string = (CHAR_T *) null; \
989 len = (sizeof (null) / sizeof (null[0])) - 1; \
993 string = (CHAR_T *) L""; \
997 else if (!is_long && spec != L_('S')) \
999 /* This is complicated. We have to transform the multibyte \
1000 string into a wide character string. */ \
1001 const char *mbs = (const char *) string; \
1002 mbstate_t mbstate; \
1004 len = prec != -1 ? (size_t) prec : strlen (mbs); \
1006 /* Allocate dynamically an array which definitely is long \
1007 enough for the wide character version. */ \
1008 if (__libc_use_alloca (len * sizeof (wchar_t))) \
1009 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1010 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1017 string_malloced = 1; \
1019 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1020 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1021 if (len == (size_t) -1) \
1023 /* Illegal multibyte character. */ \
1031 /* Search for the end of the string, but don't search past \
1032 the length specified by the precision. */ \
1033 len = __wcsnlen (string, prec); \
1035 len = __wcslen (string); \
1038 if ((width -= len) < 0) \
1040 outstring (string, len); \
1046 outstring (string, len); \
1049 if (__builtin_expect (string_malloced, 0)) \
1054 # define process_string_arg(fspec) \
1055 LABEL (form_character): \
1058 goto LABEL (form_wcharacter); \
1059 --width; /* Account for the character itself. */ \
1062 if (fspec == NULL) \
1063 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1065 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1070 LABEL (form_wcharacter): \
1072 /* Wide character. */ \
1073 char buf[MB_CUR_MAX]; \
1074 mbstate_t mbstate; \
1077 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1078 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1079 : args_value[fspec->data_arg].pa_wchar), \
1081 if (len == (size_t) -1) \
1083 /* Something went wron gduring the conversion. Bail out. */ \
1090 outstring (buf, len); \
1096 LABEL (form_string): \
1099 int string_malloced; \
1101 /* The string argument could in fact be `char *' or `wchar_t *'. \
1102 But this should not make a difference here. */ \
1103 if (fspec == NULL) \
1104 string = (char *) va_arg (ap, const char *); \
1106 string = (char *) args_value[fspec->data_arg].pa_string; \
1108 /* Entry point for printing other strings. */ \
1109 LABEL (print_string): \
1111 string_malloced = 0; \
1112 if (string == NULL) \
1114 /* Write "(null)" if there's space. */ \
1115 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1117 string = (char *) null; \
1118 len = sizeof (null) - 1; \
1122 string = (char *) ""; \
1126 else if (!is_long && spec != L_('S')) \
1130 /* Search for the end of the string, but don't search past \
1131 the length (in bytes) specified by the precision. Also \
1132 don't use incomplete characters. */ \
1133 if (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MB_CUR_MAX) == 1) \
1134 len = __strnlen (string, prec); \
1137 /* In case we have a multibyte character set the \
1138 situation is more compilcated. We must not copy \
1139 bytes at the end which form an incomplete character. */\
1140 wchar_t ignore[prec]; \
1141 const char *str2 = string; \
1144 memset (&ps, '\0', sizeof (ps)); \
1145 if (__mbsnrtowcs (ignore, &str2, prec, prec, &ps) \
1152 len = strlen (string); \
1154 len = str2 - string - (ps.__count & 7); \
1158 len = strlen (string); \
1162 const wchar_t *s2 = (const wchar_t *) string; \
1163 mbstate_t mbstate; \
1165 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1169 /* The string `s2' might not be NUL terminated. */ \
1170 if (__libc_use_alloca (prec)) \
1171 string = (char *) alloca (prec); \
1172 else if ((string = (char *) malloc (prec)) == NULL) \
1178 string_malloced = 1; \
1179 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1183 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1184 if (len != (size_t) -1) \
1186 assert (__mbsinit (&mbstate)); \
1187 s2 = (const wchar_t *) string; \
1188 if (__libc_use_alloca (len + 1)) \
1189 string = (char *) alloca (len + 1); \
1190 else if ((string = (char *) malloc (len + 1)) == NULL) \
1196 string_malloced = 1; \
1197 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1201 if (len == (size_t) -1) \
1203 /* Illegal wide-character string. */ \
1209 if ((width -= len) < 0) \
1211 outstring (string, len); \
1217 outstring (string, len); \
1220 if (__builtin_expect (string_malloced, 0)) \
1226 /* Orient the stream. */
1231 /* Sanity check of arguments. */
1232 ARGCHECK (s
, format
);
1235 /* Check for correct orientation. */
1236 if (_IO_vtable_offset (s
) == 0 &&
1237 _IO_fwide (s
, sizeof (CHAR_T
) == 1 ? -1 : 1)
1238 != (sizeof (CHAR_T
) == 1 ? -1 : 1))
1239 /* The stream is already oriented otherwise. */
1243 if (UNBUFFERED_P (s
))
1244 /* Use a helper function which will allocate a local temporary buffer
1245 for the stream and then call us again. */
1246 return buffered_vfprintf (s
, format
, ap
);
1248 /* Initialize local variables. */
1250 grouping
= (const char *) -1;
1252 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1253 since on some systems `va_list' is not an integral type. */
1254 __va_copy (ap_save
, ap
);
1260 #ifdef COMPILE_WPRINTF
1261 /* Find the first format specifier. */
1262 f
= lead_str_end
= __find_specwc ((const UCHAR_T
*) format
);
1264 /* Put state for processing format string in initial state. */
1265 memset (&mbstate
, '\0', sizeof (mbstate_t));
1267 /* Find the first format specifier. */
1268 f
= lead_str_end
= __find_specmb (format
, &mbstate
);
1272 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile
, s
);
1275 /* Write the literal text before the first format. */
1276 outstring ((const UCHAR_T
*) format
,
1277 lead_str_end
- (const UCHAR_T
*) format
);
1279 /* If we only have to print a simple string, return now. */
1283 /* Process whole format string. */
1286 #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
1287 # define REF(Name) &&do_##Name - &&do_form_unknown
1289 # define REF(Name) &&do_##Name
1291 #define LABEL(Name) do_##Name
1295 union printf_arg
*args_value
; /* This is not used here but ... */
1296 int is_negative
; /* Flag for negative number. */
1299 unsigned long long int longlong
;
1300 unsigned long int word
;
1303 union printf_arg the_arg
;
1304 CHAR_T
*string
; /* Pointer to argument string. */
1305 int alt
= 0; /* Alternate format. */
1306 int space
= 0; /* Use space prefix if no sign is needed. */
1307 int left
= 0; /* Left-justify output. */
1308 int showsign
= 0; /* Always begin with plus or minus sign. */
1309 int group
= 0; /* Print numbers according grouping rules. */
1310 int is_long_double
= 0; /* Argument is long double/ long long int. */
1311 int is_short
= 0; /* Argument is short int. */
1312 int is_long
= 0; /* Argument is long int. */
1313 int is_char
= 0; /* Argument is promoted (unsigned) char. */
1314 int width
= 0; /* Width of output; 0 means none specified. */
1315 int prec
= -1; /* Precision of output; -1 means none specified. */
1316 /* This flag is set by the 'I' modifier and selects the use of the
1317 `outdigits' as determined by the current locale. */
1318 int use_outdigits
= 0;
1319 UCHAR_T pad
= L_(' ');/* Padding character. */
1323 workend
= &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
1325 /* Get current character in format string. */
1326 JUMP (*++f
, step0_jumps
);
1331 JUMP (*++f
, step0_jumps
);
1336 JUMP (*++f
, step0_jumps
);
1342 JUMP (*++f
, step0_jumps
);
1347 JUMP (*++f
, step0_jumps
);
1353 JUMP (*++f
, step0_jumps
);
1355 /* The '\'' flag. */
1359 if (grouping
== (const char *) -1)
1361 #ifdef COMPILE_WPRINTF
1362 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1363 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1365 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1368 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1369 if (*grouping
== '\0' || *grouping
== CHAR_MAX
1370 #ifdef COMPILE_WPRINTF
1371 || thousands_sep
== L
'\0'
1373 || *thousands_sep
== '\0'
1378 JUMP (*++f
, step0_jumps
);
1382 JUMP (*++f
, step0_jumps
);
1384 /* Get width from argument. */
1385 LABEL (width_asterics
):
1387 const UCHAR_T
*tmp
; /* Temporary value. */
1390 if (ISDIGIT (*tmp
) && read_int (&tmp
) && *tmp
== L_('$'))
1391 /* The width comes from a positional parameter. */
1394 width
= va_arg (ap
, int);
1396 /* Negative width means left justified. */
1404 if (width
+ 32 >= (int) (sizeof (work_buffer
)
1405 / sizeof (work_buffer
[0])))
1407 /* We have to use a special buffer. The "32" is just a safe
1408 bet for all the output which is not counted in the width. */
1409 if (__libc_use_alloca ((width
+ 32) * sizeof (CHAR_T
)))
1410 workend
= ((CHAR_T
*) alloca ((width
+ 32) * sizeof (CHAR_T
))
1414 workstart
= (CHAR_T
*) malloc ((width
+ 32) * sizeof (CHAR_T
));
1415 if (workstart
== NULL
)
1420 workend
= workstart
+ (width
+ 32);
1424 JUMP (*f
, step1_jumps
);
1426 /* Given width in format string. */
1428 width
= read_int (&f
);
1430 if (width
+ 32 >= (int) (sizeof (work_buffer
) / 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 /* Oh, oh. The argument comes from a positional parameter. */
1451 JUMP (*f
, step1_jumps
);
1457 const UCHAR_T
*tmp
; /* Temporary value. */
1460 if (ISDIGIT (*tmp
) && read_int (&tmp
) > 0 && *tmp
== L_('$'))
1461 /* The precision comes from a positional parameter. */
1464 prec
= va_arg (ap
, int);
1466 /* If the precision is negative the precision is omitted. */
1470 else if (ISDIGIT (*f
))
1471 prec
= read_int (&f
);
1475 && prec
+ 32 > (int)(sizeof (work_buffer
) / sizeof (work_buffer
[0])))
1477 if (__libc_use_alloca ((prec
+ 32) * sizeof (CHAR_T
)))
1478 workend
= ((CHAR_T
*) alloca ((prec
+ 32) * sizeof (CHAR_T
)))
1482 workstart
= (CHAR_T
*) malloc ((prec
+ 32) * sizeof (CHAR_T
));
1483 if (workstart
== NULL
)
1488 workend
= workstart
+ (prec
+ 32);
1491 JUMP (*f
, step2_jumps
);
1493 /* Process 'h' modifier. There might another 'h' following. */
1496 JUMP (*++f
, step3a_jumps
);
1498 /* Process 'hh' modifier. */
1499 LABEL (mod_halfhalf
):
1502 JUMP (*++f
, step4_jumps
);
1504 /* Process 'l' modifier. There might another 'l' following. */
1507 JUMP (*++f
, step3b_jumps
);
1509 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1510 allowed to follow. */
1511 LABEL (mod_longlong
):
1514 JUMP (*++f
, step4_jumps
);
1517 is_long_double
= sizeof (size_t) > sizeof (unsigned long int);
1518 is_long
= sizeof (size_t) > sizeof (unsigned int);
1519 JUMP (*++f
, step4_jumps
);
1521 LABEL (mod_ptrdiff_t
):
1522 is_long_double
= sizeof (ptrdiff_t) > sizeof (unsigned long int);
1523 is_long
= sizeof (ptrdiff_t) > sizeof (unsigned int);
1524 JUMP (*++f
, step4_jumps
);
1526 LABEL (mod_intmax_t
):
1527 is_long_double
= sizeof (intmax_t) > sizeof (unsigned long int);
1528 is_long
= sizeof (intmax_t) > sizeof (unsigned int);
1529 JUMP (*++f
, step4_jumps
);
1531 /* Process current format. */
1534 process_arg (((struct printf_spec
*) NULL
));
1535 process_string_arg (((struct printf_spec
*) NULL
));
1537 LABEL (form_unknown
):
1538 if (spec
== L_('\0'))
1540 /* The format string ended before the specifier is complete. */
1545 /* If we are in the fast loop force entering the complicated
1550 /* The format is correctly handled. */
1553 if (__builtin_expect (workstart
!= NULL
, 0))
1557 /* Look for next format specifier. */
1558 #ifdef COMPILE_WPRINTF
1559 f
= __find_specwc ((end_of_spec
= ++f
));
1561 f
= __find_specmb ((end_of_spec
= ++f
), &mbstate
);
1564 /* Write the following constant string. */
1565 outstring (end_of_spec
, f
- end_of_spec
);
1567 while (*f
!= L_('\0'));
1569 /* Unlock stream and return. */
1572 /* Here starts the more complex loop to handle positional parameters. */
1575 /* Array with information about the needed arguments. This has to
1576 be dynamically extensible. */
1578 size_t nspecs_max
= 32; /* A more or less arbitrary start value. */
1579 struct printf_spec
*specs
1580 = alloca (nspecs_max
* sizeof (struct printf_spec
));
1582 /* The number of arguments the format string requests. This will
1583 determine the size of the array needed to store the argument
1587 union printf_arg
*args_value
= NULL
;
1589 /* Positional parameters refer to arguments directly. This could
1590 also determine the maximum number of arguments. Track the
1592 size_t max_ref_arg
= 0;
1594 /* Just a counter. */
1598 if (grouping
== (const char *) -1)
1600 #ifdef COMPILE_WPRINTF
1601 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1602 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1604 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1607 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1608 if (*grouping
== '\0' || *grouping
== CHAR_MAX
)
1612 for (f
= lead_str_end
; *f
!= L_('\0'); f
= specs
[nspecs
++].next_fmt
)
1614 if (nspecs
>= nspecs_max
)
1616 /* Extend the array of format specifiers. */
1617 struct printf_spec
*old
= specs
;
1620 specs
= alloca (nspecs_max
* sizeof (struct printf_spec
));
1622 if (specs
== &old
[nspecs
])
1623 /* Stack grows up, OLD was the last thing allocated;
1625 nspecs_max
+= nspecs_max
/ 2;
1628 /* Copy the old array's elements to the new space. */
1629 memcpy (specs
, old
, nspecs
* sizeof (struct printf_spec
));
1630 if (old
== &specs
[nspecs
])
1631 /* Stack grows down, OLD was just below the new
1632 SPECS. We can use that space when the new space
1634 nspecs_max
+= nspecs_max
/ 2;
1638 /* Parse the format specifier. */
1639 #ifdef COMPILE_WPRINTF
1640 nargs
+= __parse_one_specwc (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1642 nargs
+= __parse_one_specmb (f
, nargs
, &specs
[nspecs
], &max_ref_arg
,
1647 /* Determine the number of arguments the format string consumes. */
1648 nargs
= MAX (nargs
, max_ref_arg
);
1650 /* Allocate memory for the argument descriptions. */
1651 args_type
= alloca (nargs
* sizeof (int));
1652 memset (args_type
, s
->_flags2
& _IO_FLAGS2_FORTIFY
? '\xff' : '\0',
1653 nargs
* sizeof (int));
1654 args_value
= alloca (nargs
* sizeof (union printf_arg
));
1656 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1657 still zero after this loop, format is invalid. For now we
1658 simply use 0 as the value. */
1660 /* Fill in the types of all the arguments. */
1661 for (cnt
= 0; cnt
< nspecs
; ++cnt
)
1663 /* If the width is determined by an argument this is an int. */
1664 if (specs
[cnt
].width_arg
!= -1)
1665 args_type
[specs
[cnt
].width_arg
] = PA_INT
;
1667 /* If the precision is determined by an argument this is an int. */
1668 if (specs
[cnt
].prec_arg
!= -1)
1669 args_type
[specs
[cnt
].prec_arg
] = PA_INT
;
1671 switch (specs
[cnt
].ndata_args
)
1673 case 0: /* No arguments. */
1675 case 1: /* One argument; we already have the type. */
1676 args_type
[specs
[cnt
].data_arg
] = specs
[cnt
].data_arg_type
;
1679 /* We have more than one argument for this format spec.
1680 We must call the arginfo function again to determine
1682 (void) (*__printf_arginfo_table
[specs
[cnt
].info
.spec
])
1684 specs
[cnt
].ndata_args
, &args_type
[specs
[cnt
].data_arg
]);
1689 /* Now we know all the types and the order. Fill in the argument
1691 for (cnt
= 0; cnt
< nargs
; ++cnt
)
1692 switch (args_type
[cnt
])
1694 #define T(tag, mem, type) \
1696 args_value[cnt].mem = va_arg (ap_save, type); \
1699 T (PA_CHAR
, pa_int
, int); /* Promoted. */
1700 T (PA_WCHAR
, pa_wchar
, wint_t);
1701 T (PA_INT
|PA_FLAG_SHORT
, pa_int
, int); /* Promoted. */
1702 T (PA_INT
, pa_int
, int);
1703 T (PA_INT
|PA_FLAG_LONG
, pa_long_int
, long int);
1704 T (PA_INT
|PA_FLAG_LONG_LONG
, pa_long_long_int
, long long int);
1705 T (PA_FLOAT
, pa_double
, double); /* Promoted. */
1706 T (PA_DOUBLE
, pa_double
, double);
1707 T (PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
, pa_long_double
, long double);
1708 T (PA_STRING
, pa_string
, const char *);
1709 T (PA_WSTRING
, pa_wstring
, const wchar_t *);
1710 T (PA_POINTER
, pa_pointer
, void *);
1713 if ((args_type
[cnt
] & PA_FLAG_PTR
) != 0)
1714 args_value
[cnt
].pa_pointer
= va_arg (ap_save
, void *);
1716 args_value
[cnt
].pa_long_double
= 0.0;
1719 /* Error case. Not all parameters appear in N$ format
1720 strings. We have no way to determine their type. */
1721 assert (s
->_flags2
& _IO_FLAGS2_FORTIFY
);
1722 __libc_fatal ("*** invalid %N$ use detected ***\n");
1725 /* Now walk through all format specifiers and process them. */
1726 for (; (size_t) nspecs_done
< nspecs
; ++nspecs_done
)
1729 #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
1730 # define REF(Name) &&do2_##Name - &&do_form_unknown
1732 # define REF(Name) &&do2_##Name
1735 #define LABEL(Name) do2_##Name
1741 unsigned long long int longlong
;
1742 unsigned long int word
;
1745 union printf_arg the_arg
;
1746 CHAR_T
*string
; /* Pointer to argument string. */
1748 /* Fill variables from values in struct. */
1749 int alt
= specs
[nspecs_done
].info
.alt
;
1750 int space
= specs
[nspecs_done
].info
.space
;
1751 int left
= specs
[nspecs_done
].info
.left
;
1752 int showsign
= specs
[nspecs_done
].info
.showsign
;
1753 int group
= specs
[nspecs_done
].info
.group
;
1754 int is_long_double
= specs
[nspecs_done
].info
.is_long_double
;
1755 int is_short
= specs
[nspecs_done
].info
.is_short
;
1756 int is_char
= specs
[nspecs_done
].info
.is_char
;
1757 int is_long
= specs
[nspecs_done
].info
.is_long
;
1758 int width
= specs
[nspecs_done
].info
.width
;
1759 int prec
= specs
[nspecs_done
].info
.prec
;
1760 int use_outdigits
= specs
[nspecs_done
].info
.i18n
;
1761 char pad
= specs
[nspecs_done
].info
.pad
;
1762 CHAR_T spec
= specs
[nspecs_done
].info
.spec
;
1763 CHAR_T
*workstart
= NULL
;
1765 /* Fill in last information. */
1766 if (specs
[nspecs_done
].width_arg
!= -1)
1768 /* Extract the field width from an argument. */
1769 specs
[nspecs_done
].info
.width
=
1770 args_value
[specs
[nspecs_done
].width_arg
].pa_int
;
1772 if (specs
[nspecs_done
].info
.width
< 0)
1773 /* If the width value is negative left justification is
1774 selected and the value is taken as being positive. */
1776 specs
[nspecs_done
].info
.width
*= -1;
1777 left
= specs
[nspecs_done
].info
.left
= 1;
1779 width
= specs
[nspecs_done
].info
.width
;
1782 if (specs
[nspecs_done
].prec_arg
!= -1)
1784 /* Extract the precision from an argument. */
1785 specs
[nspecs_done
].info
.prec
=
1786 args_value
[specs
[nspecs_done
].prec_arg
].pa_int
;
1788 if (specs
[nspecs_done
].info
.prec
< 0)
1789 /* If the precision is negative the precision is
1791 specs
[nspecs_done
].info
.prec
= -1;
1793 prec
= specs
[nspecs_done
].info
.prec
;
1796 /* Maybe the buffer is too small. */
1797 if (MAX (prec
, width
) + 32 > (int) (sizeof (work_buffer
)
1800 if (__libc_use_alloca ((MAX (prec
, width
) + 32)
1802 workend
= ((CHAR_T
*) alloca ((MAX (prec
, width
) + 32)
1804 + (MAX (prec
, width
) + 32));
1807 workstart
= (CHAR_T
*) malloc ((MAX (prec
, width
) + 32)
1809 workend
= workstart
+ (MAX (prec
, width
) + 32);
1813 /* Process format specifiers. */
1816 JUMP (spec
, step4_jumps
);
1818 process_arg ((&specs
[nspecs_done
]));
1819 process_string_arg ((&specs
[nspecs_done
]));
1821 LABEL (form_unknown
):
1823 extern printf_function
**__printf_function_table
;
1825 printf_function
*function
;
1830 (__printf_function_table
== NULL
? NULL
:
1831 __printf_function_table
[specs
[nspecs_done
].info
.spec
]);
1833 if (function
== NULL
)
1834 function
= &printf_unknown
;
1836 ptr
= alloca (specs
[nspecs_done
].ndata_args
1837 * sizeof (const void *));
1839 /* Fill in an array of pointers to the argument values. */
1840 for (i
= 0; i
< specs
[nspecs_done
].ndata_args
; ++i
)
1841 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
1843 /* Call the function. */
1844 function_done
= (*function
) (s
, &specs
[nspecs_done
].info
, ptr
);
1846 /* If an error occurred we don't have information about #
1848 if (function_done
< 0)
1854 done
+= function_done
;
1859 if (__builtin_expect (workstart
!= NULL
, 0))
1863 /* Write the following constant string. */
1864 outstring (specs
[nspecs_done
].end_of_fmt
,
1865 specs
[nspecs_done
].next_fmt
1866 - specs
[nspecs_done
].end_of_fmt
);
1871 if (__builtin_expect (workstart
!= NULL
, 0))
1873 /* Unlock the stream. */
1874 _IO_funlockfile (s
);
1875 _IO_cleanup_region_end (0);
1880 /* Handle an unknown format specifier. This prints out a canonicalized
1881 representation of the format spec itself. */
1883 printf_unknown (FILE *s
, const struct printf_info
*info
,
1884 const void *const *args
)
1888 CHAR_T work_buffer
[MAX (info
->width
, info
->spec
) + 32];
1889 CHAR_T
*const workend
1890 = &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
1901 else if (info
->space
)
1905 if (info
->pad
== L_('0'))
1910 if (info
->width
!= 0)
1912 w
= _itoa_word (info
->width
, workend
, 10, 0);
1917 if (info
->prec
!= -1)
1920 w
= _itoa_word (info
->prec
, workend
, 10, 0);
1925 if (info
->spec
!= L_('\0'))
1926 outchar (info
->spec
);
1932 /* Group the digits according to the grouping rules of the current locale.
1933 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1936 group_number (CHAR_T
*w
, CHAR_T
*rear_ptr
, const char *grouping
,
1937 #ifdef COMPILE_WPRINTF
1938 wchar_t thousands_sep
1940 const char *thousands_sep
1946 #ifndef COMPILE_WPRINTF
1947 int tlen
= strlen (thousands_sep
);
1950 /* We treat all negative values like CHAR_MAX. */
1952 if (*grouping
== CHAR_MAX
|| *grouping
<= 0)
1953 /* No grouping should be done. */
1958 /* Copy existing string so that nothing gets overwritten. */
1959 src
= (CHAR_T
*) alloca ((rear_ptr
- w
) * sizeof (CHAR_T
));
1960 s
= (CHAR_T
*) __mempcpy (src
, w
,
1961 (rear_ptr
- w
) * sizeof (CHAR_T
));
1964 /* Process all characters in the string. */
1969 if (--len
== 0 && s
> src
)
1971 /* A new group begins. */
1972 #ifdef COMPILE_WPRINTF
1973 *--w
= thousands_sep
;
1977 *--w
= thousands_sep
[--cnt
];
1981 if (*grouping
== CHAR_MAX
1987 /* No further grouping to be done.
1988 Copy the rest of the number. */
1994 else if (*grouping
!= '\0')
1995 /* The previous grouping repeats ad infinitum. */
2004 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2007 struct _IO_FILE_plus _f
;
2008 #ifdef COMPILE_WPRINTF
2009 struct _IO_wide_data _wide_data
;
2011 _IO_FILE
*_put_stream
;
2012 #ifdef _IO_MTSAFE_IO
2018 _IO_helper_overflow (_IO_FILE
*s
, int c
)
2020 _IO_FILE
*target
= ((struct helper_file
*) s
)->_put_stream
;
2021 #ifdef COMPILE_WPRINTF
2022 int used
= s
->_wide_data
->_IO_write_ptr
- s
->_wide_data
->_IO_write_base
;
2025 _IO_size_t written
= _IO_sputn (target
, s
->_wide_data
->_IO_write_base
,
2027 s
->_wide_data
->_IO_write_ptr
-= written
;
2030 int used
= s
->_IO_write_ptr
- s
->_IO_write_base
;
2033 _IO_size_t written
= _IO_sputn (target
, s
->_IO_write_base
, used
);
2034 s
->_IO_write_ptr
-= written
;
2040 #ifdef COMPILE_WPRINTF
2041 static const struct _IO_jump_t _IO_helper_jumps
=
2044 JUMP_INIT (finish
, INTUSE(_IO_wdefault_finish
)),
2045 JUMP_INIT (overflow
, _IO_helper_overflow
),
2046 JUMP_INIT (underflow
, _IO_default_underflow
),
2047 JUMP_INIT (uflow
, INTUSE(_IO_default_uflow
)),
2048 JUMP_INIT (pbackfail
, (_IO_pbackfail_t
) INTUSE(_IO_wdefault_pbackfail
)),
2049 JUMP_INIT (xsputn
, INTUSE(_IO_wdefault_xsputn
)),
2050 JUMP_INIT (xsgetn
, INTUSE(_IO_wdefault_xsgetn
)),
2051 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2052 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2053 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2054 JUMP_INIT (sync
, _IO_default_sync
),
2055 JUMP_INIT (doallocate
, INTUSE(_IO_wdefault_doallocate
)),
2056 JUMP_INIT (read
, _IO_default_read
),
2057 JUMP_INIT (write
, _IO_default_write
),
2058 JUMP_INIT (seek
, _IO_default_seek
),
2059 JUMP_INIT (close
, _IO_default_close
),
2060 JUMP_INIT (stat
, _IO_default_stat
)
2063 static const struct _IO_jump_t _IO_helper_jumps
=
2066 JUMP_INIT (finish
, INTUSE(_IO_default_finish
)),
2067 JUMP_INIT (overflow
, _IO_helper_overflow
),
2068 JUMP_INIT (underflow
, _IO_default_underflow
),
2069 JUMP_INIT (uflow
, INTUSE(_IO_default_uflow
)),
2070 JUMP_INIT (pbackfail
, INTUSE(_IO_default_pbackfail
)),
2071 JUMP_INIT (xsputn
, INTUSE(_IO_default_xsputn
)),
2072 JUMP_INIT (xsgetn
, INTUSE(_IO_default_xsgetn
)),
2073 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2074 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2075 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2076 JUMP_INIT (sync
, _IO_default_sync
),
2077 JUMP_INIT (doallocate
, INTUSE(_IO_default_doallocate
)),
2078 JUMP_INIT (read
, _IO_default_read
),
2079 JUMP_INIT (write
, _IO_default_write
),
2080 JUMP_INIT (seek
, _IO_default_seek
),
2081 JUMP_INIT (close
, _IO_default_close
),
2082 JUMP_INIT (stat
, _IO_default_stat
)
2088 buffered_vfprintf (register _IO_FILE
*s
, const CHAR_T
*format
,
2091 CHAR_T buf
[_IO_BUFSIZ
];
2092 struct helper_file helper
;
2093 register _IO_FILE
*hp
= (_IO_FILE
*) &helper
._f
;
2094 int result
, to_flush
;
2096 /* Orient the stream. */
2101 /* Initialize helper. */
2102 helper
._put_stream
= s
;
2103 #ifdef COMPILE_WPRINTF
2104 hp
->_wide_data
= &helper
._wide_data
;
2105 _IO_wsetp (hp
, buf
, buf
+ sizeof buf
/ sizeof (CHAR_T
));
2108 _IO_setp (hp
, buf
, buf
+ sizeof buf
);
2111 hp
->_IO_file_flags
= _IO_MAGIC
|_IO_NO_READS
|_IO_USER_LOCK
;
2112 #if _IO_JUMPS_OFFSET
2113 hp
->_vtable_offset
= 0;
2115 #ifdef _IO_MTSAFE_IO
2118 hp
->_flags2
= s
->_flags2
;
2119 _IO_JUMPS (&helper
._f
) = (struct _IO_jump_t
*) &_IO_helper_jumps
;
2121 /* Now print to helper instead. */
2122 #ifndef COMPILE_WPRINTF
2123 result
= INTUSE(_IO_vfprintf
) (hp
, format
, args
);
2125 result
= vfprintf (hp
, format
, args
);
2129 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile
, s
);
2132 /* Now flush anything from the helper to the S. */
2133 #ifdef COMPILE_WPRINTF
2134 if ((to_flush
= (hp
->_wide_data
->_IO_write_ptr
2135 - hp
->_wide_data
->_IO_write_base
)) > 0)
2137 if ((int) _IO_sputn (s
, hp
->_wide_data
->_IO_write_base
, to_flush
)
2142 if ((to_flush
= hp
->_IO_write_ptr
- hp
->_IO_write_base
) > 0)
2144 if ((int) _IO_sputn (s
, hp
->_IO_write_base
, to_flush
) != to_flush
)
2149 /* Unlock the stream. */
2150 _IO_funlockfile (s
);
2151 __libc_cleanup_region_end (0);
2158 /* This is for glibc. */
2159 # ifdef COMPILE_WPRINTF
2160 strong_alias (_IO_vfwprintf
, __vfwprintf
);
2161 weak_alias (_IO_vfwprintf
, vfwprintf
);
2163 strong_alias (_IO_vfprintf
, vfprintf
);
2164 libc_hidden_def (vfprintf
)
2165 INTDEF(_IO_vfprintf
)
2168 # if defined __ELF__ || defined __GNU_LIBRARY__
2169 # include <gnu-stabs.h>
2171 # ifdef COMPILE_WPRINTF
2172 weak_alias (_IO_vfwprintf
, vfwprintf
);
2174 weak_alias (_IO_vfprintf
, vfprintf
);