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)
74 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
75 # define PAD(Padchar) \
77 done += INTUSE(_IO_padn) (s, (Padchar), width)
78 # define PUTC(C, F) _IO_putc_unlocked (C, F)
79 # define ORIENT if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
82 # define vfprintf _IO_vfwprintf
83 # define CHAR_T wchar_t
84 /* This is a hack!!! There should be a type uwchar_t. */
85 # define UCHAR_T unsigned int /* uwchar_t */
87 # define L_(Str) L##Str
88 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
92 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
93 # define PAD(Padchar) \
95 done += _IO_wpadn (s, (Padchar), width)
96 # define PUTC(C, F) _IO_putwc_unlocked (C, F)
97 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
99 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
100 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
105 #include "_i18n_number.h"
107 /* Include the shared code for parsing the format string. */
108 #include "printf-parse.h"
111 #define outchar(Ch) \
114 register const INT_T outc = (Ch); \
115 if (PUTC (outc, s) == EOF) \
125 #define outstring(String, Len) \
128 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
137 /* For handling long_double and longlong we use the same flag. If
138 `long' and `long long' are effectively the same type define it to
140 #if LONG_MAX == LONG_LONG_MAX
141 # define is_longlong 0
143 # define is_longlong is_long_double
146 /* If `long' and `int' is effectively the same type we don't have to
147 handle `long separately. */
148 #if INT_MAX == LONG_MAX
149 # define is_long_num 0
151 # define is_long_num is_long
155 /* Global variables. */
156 static const CHAR_T null
[] = L_("(null)");
159 /* Helper function to provide temporary buffering for unbuffered streams. */
160 static int buffered_vfprintf (FILE *stream
, const CHAR_T
*fmt
, va_list)
161 __THROW
__attribute__ ((noinline
)) internal_function
;
163 /* Handle unknown format specifier. */
164 static int printf_unknown (FILE *, const struct printf_info
*,
165 const void *const *) __THROW
;
167 /* Group digits of number string. */
168 #ifdef COMPILE_WPRINTF
169 static CHAR_T
*group_number (CHAR_T
*, CHAR_T
*, const char *, wchar_t)
170 __THROW internal_function
;
172 static CHAR_T
*group_number (CHAR_T
*, CHAR_T
*, const char *, const char *)
173 __THROW internal_function
;
177 /* The function itself. */
179 vfprintf (FILE *s
, const CHAR_T
*format
, va_list ap
)
181 /* The character used as thousands separator. */
182 #ifdef COMPILE_WPRINTF
183 wchar_t thousands_sep
= L
'\0';
185 const char *thousands_sep
= NULL
;
188 /* The string describing the size of groups of digits. */
189 const char *grouping
;
191 /* Place to accumulate the result. */
194 /* Current character in format string. */
197 /* End of leading constant string. */
198 const UCHAR_T
*lead_str_end
;
200 /* Points to next format specifier. */
201 const UCHAR_T
*end_of_spec
;
203 /* Buffer intermediate results. */
204 CHAR_T work_buffer
[1000];
205 CHAR_T
*workstart
= NULL
;
208 /* State for restartable multibyte character handling functions. */
209 #ifndef COMPILE_WPRINTF
213 /* We have to save the original argument pointer. */
216 /* Count number of specifiers we already processed. */
219 /* For the %m format we may need the current `errno' value. */
220 int save_errno
= errno
;
223 /* This table maps a character into a number representing a
224 class. In each step there is a destination label for each
226 static const int jump_table
[] =
228 /* ' ' */ 1, 0, 0, /* '#' */ 4,
229 0, /* '%' */ 14, 0, /* '\''*/ 6,
230 0, 0, /* '*' */ 7, /* '+' */ 2,
231 0, /* '-' */ 3, /* '.' */ 9, 0,
232 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
233 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
234 /* '8' */ 8, /* '9' */ 8, 0, 0,
236 0, /* 'A' */ 26, 0, /* 'C' */ 25,
237 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
238 0, /* 'I' */ 29, 0, 0,
239 /* 'L' */ 12, 0, 0, 0,
240 0, 0, 0, /* 'S' */ 21,
242 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
244 0, /* 'a' */ 26, 0, /* 'c' */ 20,
245 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
246 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
247 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
248 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
249 /* 't' */ 27, /* 'u' */ 16, 0, 0,
250 /* 'x' */ 18, 0, /* 'z' */ 13
253 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
254 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
255 #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
256 /* 'int' is enough and it saves some space on 64 bit systems. */
257 # define JUMP_TABLE_TYPE const int
258 # define JUMP(ChExpr, table) \
262 void *__unbounded ptr; \
264 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
265 : table[CHAR_CLASS (spec)]; \
266 ptr = &&do_form_unknown + offset; \
271 # define JUMP_TABLE_TYPE const void *const
272 # define JUMP(ChExpr, table) \
275 const void *__unbounded ptr; \
277 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
278 : table[CHAR_CLASS (spec)]; \
284 #define STEP0_3_TABLE \
285 /* Step 0: at the beginning. */ \
286 static JUMP_TABLE_TYPE step0_jumps[30] = \
288 REF (form_unknown), \
289 REF (flag_space), /* for ' ' */ \
290 REF (flag_plus), /* for '+' */ \
291 REF (flag_minus), /* for '-' */ \
292 REF (flag_hash), /* for '<hash>' */ \
293 REF (flag_zero), /* for '0' */ \
294 REF (flag_quote), /* for '\'' */ \
295 REF (width_asterics), /* for '*' */ \
296 REF (width), /* for '1'...'9' */ \
297 REF (precision), /* for '.' */ \
298 REF (mod_half), /* for 'h' */ \
299 REF (mod_long), /* for 'l' */ \
300 REF (mod_longlong), /* for 'L', 'q' */ \
301 REF (mod_size_t), /* for 'z', 'Z' */ \
302 REF (form_percent), /* for '%' */ \
303 REF (form_integer), /* for 'd', 'i' */ \
304 REF (form_unsigned), /* for 'u' */ \
305 REF (form_octal), /* for 'o' */ \
306 REF (form_hexa), /* for 'X', 'x' */ \
307 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
308 REF (form_character), /* for 'c' */ \
309 REF (form_string), /* for 's', 'S' */ \
310 REF (form_pointer), /* for 'p' */ \
311 REF (form_number), /* for 'n' */ \
312 REF (form_strerror), /* for 'm' */ \
313 REF (form_wcharacter), /* for 'C' */ \
314 REF (form_floathex), /* for 'A', 'a' */ \
315 REF (mod_ptrdiff_t), /* for 't' */ \
316 REF (mod_intmax_t), /* for 'j' */ \
317 REF (flag_i18n), /* for 'I' */ \
319 /* Step 1: after processing width. */ \
320 static JUMP_TABLE_TYPE step1_jumps[30] = \
322 REF (form_unknown), \
323 REF (form_unknown), /* for ' ' */ \
324 REF (form_unknown), /* for '+' */ \
325 REF (form_unknown), /* for '-' */ \
326 REF (form_unknown), /* for '<hash>' */ \
327 REF (form_unknown), /* for '0' */ \
328 REF (form_unknown), /* for '\'' */ \
329 REF (form_unknown), /* for '*' */ \
330 REF (form_unknown), /* for '1'...'9' */ \
331 REF (precision), /* for '.' */ \
332 REF (mod_half), /* for 'h' */ \
333 REF (mod_long), /* for 'l' */ \
334 REF (mod_longlong), /* for 'L', 'q' */ \
335 REF (mod_size_t), /* for 'z', 'Z' */ \
336 REF (form_percent), /* for '%' */ \
337 REF (form_integer), /* for 'd', 'i' */ \
338 REF (form_unsigned), /* for 'u' */ \
339 REF (form_octal), /* for 'o' */ \
340 REF (form_hexa), /* for 'X', 'x' */ \
341 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
342 REF (form_character), /* for 'c' */ \
343 REF (form_string), /* for 's', 'S' */ \
344 REF (form_pointer), /* for 'p' */ \
345 REF (form_number), /* for 'n' */ \
346 REF (form_strerror), /* for 'm' */ \
347 REF (form_wcharacter), /* for 'C' */ \
348 REF (form_floathex), /* for 'A', 'a' */ \
349 REF (mod_ptrdiff_t), /* for 't' */ \
350 REF (mod_intmax_t), /* for 'j' */ \
351 REF (form_unknown) /* for 'I' */ \
353 /* Step 2: after processing precision. */ \
354 static JUMP_TABLE_TYPE step2_jumps[30] = \
356 REF (form_unknown), \
357 REF (form_unknown), /* for ' ' */ \
358 REF (form_unknown), /* for '+' */ \
359 REF (form_unknown), /* for '-' */ \
360 REF (form_unknown), /* for '<hash>' */ \
361 REF (form_unknown), /* for '0' */ \
362 REF (form_unknown), /* for '\'' */ \
363 REF (form_unknown), /* for '*' */ \
364 REF (form_unknown), /* for '1'...'9' */ \
365 REF (form_unknown), /* for '.' */ \
366 REF (mod_half), /* for 'h' */ \
367 REF (mod_long), /* for 'l' */ \
368 REF (mod_longlong), /* for 'L', 'q' */ \
369 REF (mod_size_t), /* for 'z', 'Z' */ \
370 REF (form_percent), /* for '%' */ \
371 REF (form_integer), /* for 'd', 'i' */ \
372 REF (form_unsigned), /* for 'u' */ \
373 REF (form_octal), /* for 'o' */ \
374 REF (form_hexa), /* for 'X', 'x' */ \
375 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
376 REF (form_character), /* for 'c' */ \
377 REF (form_string), /* for 's', 'S' */ \
378 REF (form_pointer), /* for 'p' */ \
379 REF (form_number), /* for 'n' */ \
380 REF (form_strerror), /* for 'm' */ \
381 REF (form_wcharacter), /* for 'C' */ \
382 REF (form_floathex), /* for 'A', 'a' */ \
383 REF (mod_ptrdiff_t), /* for 't' */ \
384 REF (mod_intmax_t), /* for 'j' */ \
385 REF (form_unknown) /* for 'I' */ \
387 /* Step 3a: after processing first 'h' modifier. */ \
388 static JUMP_TABLE_TYPE step3a_jumps[30] = \
390 REF (form_unknown), \
391 REF (form_unknown), /* for ' ' */ \
392 REF (form_unknown), /* for '+' */ \
393 REF (form_unknown), /* for '-' */ \
394 REF (form_unknown), /* for '<hash>' */ \
395 REF (form_unknown), /* for '0' */ \
396 REF (form_unknown), /* for '\'' */ \
397 REF (form_unknown), /* for '*' */ \
398 REF (form_unknown), /* for '1'...'9' */ \
399 REF (form_unknown), /* for '.' */ \
400 REF (mod_halfhalf), /* for 'h' */ \
401 REF (form_unknown), /* for 'l' */ \
402 REF (form_unknown), /* for 'L', 'q' */ \
403 REF (form_unknown), /* for 'z', 'Z' */ \
404 REF (form_percent), /* for '%' */ \
405 REF (form_integer), /* for 'd', 'i' */ \
406 REF (form_unsigned), /* for 'u' */ \
407 REF (form_octal), /* for 'o' */ \
408 REF (form_hexa), /* for 'X', 'x' */ \
409 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
410 REF (form_unknown), /* for 'c' */ \
411 REF (form_unknown), /* for 's', 'S' */ \
412 REF (form_unknown), /* for 'p' */ \
413 REF (form_number), /* for 'n' */ \
414 REF (form_unknown), /* for 'm' */ \
415 REF (form_unknown), /* for 'C' */ \
416 REF (form_unknown), /* for 'A', 'a' */ \
417 REF (form_unknown), /* for 't' */ \
418 REF (form_unknown), /* for 'j' */ \
419 REF (form_unknown) /* for 'I' */ \
421 /* Step 3b: after processing first 'l' modifier. */ \
422 static JUMP_TABLE_TYPE step3b_jumps[30] = \
424 REF (form_unknown), \
425 REF (form_unknown), /* for ' ' */ \
426 REF (form_unknown), /* for '+' */ \
427 REF (form_unknown), /* for '-' */ \
428 REF (form_unknown), /* for '<hash>' */ \
429 REF (form_unknown), /* for '0' */ \
430 REF (form_unknown), /* for '\'' */ \
431 REF (form_unknown), /* for '*' */ \
432 REF (form_unknown), /* for '1'...'9' */ \
433 REF (form_unknown), /* for '.' */ \
434 REF (form_unknown), /* for 'h' */ \
435 REF (mod_longlong), /* for 'l' */ \
436 REF (form_unknown), /* for 'L', 'q' */ \
437 REF (form_unknown), /* for 'z', 'Z' */ \
438 REF (form_percent), /* for '%' */ \
439 REF (form_integer), /* for 'd', 'i' */ \
440 REF (form_unsigned), /* for 'u' */ \
441 REF (form_octal), /* for 'o' */ \
442 REF (form_hexa), /* for 'X', 'x' */ \
443 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
444 REF (form_character), /* for 'c' */ \
445 REF (form_string), /* for 's', 'S' */ \
446 REF (form_pointer), /* for 'p' */ \
447 REF (form_number), /* for 'n' */ \
448 REF (form_strerror), /* for 'm' */ \
449 REF (form_wcharacter), /* for 'C' */ \
450 REF (form_floathex), /* for 'A', 'a' */ \
451 REF (form_unknown), /* for 't' */ \
452 REF (form_unknown), /* for 'j' */ \
453 REF (form_unknown) /* for 'I' */ \
456 #define STEP4_TABLE \
457 /* Step 4: processing format specifier. */ \
458 static JUMP_TABLE_TYPE step4_jumps[30] = \
460 REF (form_unknown), \
461 REF (form_unknown), /* for ' ' */ \
462 REF (form_unknown), /* for '+' */ \
463 REF (form_unknown), /* for '-' */ \
464 REF (form_unknown), /* for '<hash>' */ \
465 REF (form_unknown), /* for '0' */ \
466 REF (form_unknown), /* for '\'' */ \
467 REF (form_unknown), /* for '*' */ \
468 REF (form_unknown), /* for '1'...'9' */ \
469 REF (form_unknown), /* for '.' */ \
470 REF (form_unknown), /* for 'h' */ \
471 REF (form_unknown), /* for 'l' */ \
472 REF (form_unknown), /* for 'L', 'q' */ \
473 REF (form_unknown), /* for 'z', 'Z' */ \
474 REF (form_percent), /* for '%' */ \
475 REF (form_integer), /* for 'd', 'i' */ \
476 REF (form_unsigned), /* for 'u' */ \
477 REF (form_octal), /* for 'o' */ \
478 REF (form_hexa), /* for 'X', 'x' */ \
479 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
480 REF (form_character), /* for 'c' */ \
481 REF (form_string), /* for 's', 'S' */ \
482 REF (form_pointer), /* for 'p' */ \
483 REF (form_number), /* for 'n' */ \
484 REF (form_strerror), /* for 'm' */ \
485 REF (form_wcharacter), /* for 'C' */ \
486 REF (form_floathex), /* for 'A', 'a' */ \
487 REF (form_unknown), /* for 't' */ \
488 REF (form_unknown), /* for 'j' */ \
489 REF (form_unknown) /* for 'I' */ \
493 #define process_arg(fspec) \
494 /* Start real work. We know about all flags and modifiers and \
495 now process the wanted format specifier. */ \
496 LABEL (form_percent): \
497 /* Write a literal "%". */ \
501 LABEL (form_integer): \
502 /* Signed decimal integer. */ \
507 long long int signed_number; \
510 signed_number = va_arg (ap, long long int); \
512 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
514 is_negative = signed_number < 0; \
515 number.longlong = is_negative ? (- signed_number) : signed_number; \
517 goto LABEL (longlong_number); \
521 long int signed_number; \
526 signed_number = va_arg (ap, long int); \
527 else /* `char' and `short int' will be promoted to `int'. */ \
528 signed_number = va_arg (ap, int); \
532 signed_number = args_value[fspec->data_arg].pa_long_int; \
533 else /* `char' and `short int' will be promoted to `int'. */ \
534 signed_number = args_value[fspec->data_arg].pa_int; \
536 is_negative = signed_number < 0; \
537 number.word = is_negative ? (- signed_number) : signed_number; \
539 goto LABEL (number); \
543 LABEL (form_unsigned): \
544 /* Unsigned decimal integer. */ \
546 goto LABEL (unsigned_number); \
549 LABEL (form_octal): \
550 /* Unsigned octal integer. */ \
552 goto LABEL (unsigned_number); \
556 /* Unsigned hexadecimal integer. */ \
559 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
561 /* ISO specifies the `+' and ` ' flags only for signed \
570 number.longlong = va_arg (ap, unsigned long long int); \
572 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
574 LABEL (longlong_number): \
576 /* Supply a default precision if none was given. */ \
579 /* We have to take care for the '0' flag. If a precision \
580 is given it must be ignored. */ \
583 /* If the precision is 0 and the number is 0 nothing has to \
584 be written for the number, except for the 'o' format in \
586 if (prec == 0 && number.longlong == 0) \
589 if (base == 8 && alt) \
590 *--string = L_('0'); \
594 /* Put the number in WORK. */ \
595 string = _itoa (number.longlong, workend, base, \
597 if (group && grouping) \
598 string = group_number (string, workend, grouping, \
601 if (use_outdigits && base == 10) \
602 string = _i18n_number_rewrite (string, workend); \
604 /* Simplify further test for num != 0. */ \
605 number.word = number.longlong != 0; \
612 number.word = va_arg (ap, unsigned long int); \
614 number.word = (unsigned char) va_arg (ap, unsigned int); \
615 else if (!is_short) \
616 number.word = va_arg (ap, unsigned int); \
618 number.word = (unsigned short int) va_arg (ap, unsigned int); \
622 number.word = args_value[fspec->data_arg].pa_u_long_int; \
624 number.word = (unsigned char) \
625 args_value[fspec->data_arg].pa_u_int; \
626 else if (!is_short) \
627 number.word = args_value[fspec->data_arg].pa_u_int; \
629 number.word = (unsigned short int) \
630 args_value[fspec->data_arg].pa_u_int; \
634 /* Supply a default precision if none was given. */ \
637 /* We have to take care for the '0' flag. If a precision \
638 is given it must be ignored. */ \
641 /* If the precision is 0 and the number is 0 nothing has to \
642 be written for the number, except for the 'o' format in \
644 if (prec == 0 && number.word == 0) \
647 if (base == 8 && alt) \
648 *--string = L_('0'); \
652 /* Put the number in WORK. */ \
653 string = _itoa_word (number.word, workend, base, \
655 if (group && grouping) \
656 string = group_number (string, workend, grouping, \
659 if (use_outdigits && base == 10) \
660 string = _i18n_number_rewrite (string, workend); \
664 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
665 /* Add octal marker. */ \
666 *--string = L_('0'); \
668 prec = MAX (0, prec - (workend - string)); \
672 width -= workend - string + prec; \
674 if (number.word != 0 && alt && base == 16) \
675 /* Account for 0X hex marker. */ \
678 if (is_negative || showsign || space) \
681 if (pad == L_(' ')) \
694 if (number.word != 0 && alt && base == 16) \
703 outstring (string, workend - string); \
725 if (number.word != 0 && alt && base == 16) \
732 width -= workend - string + prec; \
742 outstring (string, workend - string); \
748 LABEL (form_float): \
750 /* Floating-point number. This is handled by printf_fp.c. */ \
756 struct printf_info info = { .prec = prec, \
759 .is_long_double = is_long_double, \
760 .is_short = is_short, \
761 .is_long = is_long, \
765 .showsign = showsign, \
769 .i18n = use_outdigits, \
770 .wide = sizeof (CHAR_T) != 1 }; \
772 if (is_long_double) \
773 the_arg.pa_long_double = va_arg (ap, long double); \
775 the_arg.pa_double = va_arg (ap, double); \
776 ptr = (const void *) &the_arg; \
778 function_done = __printf_fp (s, &info, &ptr); \
782 ptr = (const void *) &args_value[fspec->data_arg]; \
784 function_done = __printf_fp (s, &fspec->info, &ptr); \
787 if (function_done < 0) \
789 /* Error in print handler. */ \
794 done += function_done; \
798 LABEL (form_floathex): \
800 /* Floating point number printed as hexadecimal number. */ \
806 struct printf_info info = { .prec = prec, \
809 .is_long_double = is_long_double, \
810 .is_short = is_short, \
811 .is_long = is_long, \
815 .showsign = showsign, \
819 .wide = sizeof (CHAR_T) != 1 }; \
821 if (is_long_double) \
822 the_arg.pa_long_double = va_arg (ap, long double); \
824 the_arg.pa_double = va_arg (ap, double); \
825 ptr = (const void *) &the_arg; \
827 function_done = __printf_fphex (s, &info, &ptr); \
831 ptr = (const void *) &args_value[fspec->data_arg]; \
833 function_done = __printf_fphex (s, &fspec->info, &ptr); \
836 if (function_done < 0) \
838 /* Error in print handler. */ \
843 done += function_done; \
847 LABEL (form_pointer): \
848 /* Generic pointer. */ \
852 ptr = va_arg (ap, void *); \
854 ptr = args_value[fspec->data_arg].pa_pointer; \
857 /* If the pointer is not NULL, write it as a %#x spec. */ \
859 number.word = (unsigned long int) ptr; \
864 goto LABEL (number); \
868 /* Write "(nil)" for a nil pointer. */ \
869 string = (CHAR_T *) L_("(nil)"); \
870 /* Make sure the full string "(nil)" is printed. */ \
873 is_long = 0; /* This is no wide-char string. */ \
874 goto LABEL (print_string); \
879 LABEL (form_number): \
880 /* Answer the count of characters written. */ \
884 *(long long int *) va_arg (ap, void *) = done; \
885 else if (is_long_num) \
886 *(long int *) va_arg (ap, void *) = done; \
888 *(char *) va_arg (ap, void *) = done; \
889 else if (!is_short) \
890 *(int *) va_arg (ap, void *) = done; \
892 *(short int *) va_arg (ap, void *) = done; \
896 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
897 else if (is_long_num) \
898 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
900 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
901 else if (!is_short) \
902 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
904 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
907 LABEL (form_strerror): \
908 /* Print description of error ERRNO. */ \
910 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
911 sizeof work_buffer); \
912 is_long = 0; /* This is no wide-char string. */ \
913 goto LABEL (print_string)
915 #ifdef COMPILE_WPRINTF
916 # define process_string_arg(fspec) \
917 LABEL (form_character): \
920 goto LABEL (form_wcharacter); \
921 --width; /* Account for the character itself. */ \
925 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
927 outchar (__btowc ((unsigned char) \
928 args_value[fspec->data_arg].pa_int)); \
933 LABEL (form_wcharacter): \
935 /* Wide character. */ \
940 outchar (va_arg (ap, wchar_t)); \
942 outchar (args_value[fspec->data_arg].pa_wchar); \
948 LABEL (form_string): \
951 int string_malloced; \
953 /* The string argument could in fact be `char *' or `wchar_t *'. \
954 But this should not make a difference here. */ \
956 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
958 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
960 /* Entry point for printing other strings. */ \
961 LABEL (print_string): \
963 string_malloced = 0; \
964 if (string == NULL) \
966 /* Write "(null)" if there's space. */ \
968 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
970 string = (CHAR_T *) null; \
971 len = (sizeof (null) / sizeof (null[0])) - 1; \
975 string = (CHAR_T *) L""; \
979 else if (!is_long && spec != L_('S')) \
981 /* This is complicated. We have to transform the multibyte \
982 string into a wide character string. */ \
983 const char *mbs = (const char *) string; \
986 len = prec != -1 ? (size_t) prec : strlen (mbs); \
988 /* Allocate dynamically an array which definitely is long \
989 enough for the wide character version. */ \
990 if (__libc_use_alloca (len * sizeof (wchar_t))) \
991 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
992 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
999 string_malloced = 1; \
1001 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1002 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1003 if (len == (size_t) -1) \
1005 /* Illegal multibyte character. */ \
1013 /* Search for the end of the string, but don't search past \
1014 the length specified by the precision. */ \
1015 len = __wcsnlen (string, prec); \
1017 len = __wcslen (string); \
1020 if ((width -= len) < 0) \
1022 outstring (string, len); \
1028 outstring (string, len); \
1031 if (__builtin_expect (string_malloced, 0)) \
1036 # define process_string_arg(fspec) \
1037 LABEL (form_character): \
1040 goto LABEL (form_wcharacter); \
1041 --width; /* Account for the character itself. */ \
1044 if (fspec == NULL) \
1045 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1047 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1052 LABEL (form_wcharacter): \
1054 /* Wide character. */ \
1055 char buf[MB_CUR_MAX]; \
1056 mbstate_t mbstate; \
1059 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1060 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1061 : args_value[fspec->data_arg].pa_wchar), \
1063 if (len == (size_t) -1) \
1065 /* Something went wron gduring the conversion. Bail out. */ \
1072 outstring (buf, len); \
1078 LABEL (form_string): \
1081 int string_malloced; \
1083 /* The string argument could in fact be `char *' or `wchar_t *'. \
1084 But this should not make a difference here. */ \
1085 if (fspec == NULL) \
1086 string = (char *) va_arg (ap, const char *); \
1088 string = (char *) args_value[fspec->data_arg].pa_string; \
1090 /* Entry point for printing other strings. */ \
1091 LABEL (print_string): \
1093 string_malloced = 0; \
1094 if (string == NULL) \
1096 /* Write "(null)" if there's space. */ \
1097 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1099 string = (char *) null; \
1100 len = sizeof (null) - 1; \
1104 string = (char *) ""; \
1108 else if (!is_long && spec != L_('S')) \
1112 /* Search for the end of the string, but don't search past \
1113 the length (in bytes) specified by the precision. Also \
1114 don't use incomplete characters. */ \
1115 if (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MB_CUR_MAX) == 1) \
1116 len = __strnlen (string, prec); \
1119 /* In case we have a multibyte character set the \
1120 situation is more compilcated. We must not copy \
1121 bytes at the end which form an incomplete character. */\
1122 wchar_t ignore[prec]; \
1123 const char *str2 = string; \
1126 memset (&ps, '\0', sizeof (ps)); \
1127 if (__mbsnrtowcs (ignore, &str2, prec, prec, &ps) \
1134 len = strlen (string); \
1136 len = str2 - string - (ps.__count & 7); \
1140 len = strlen (string); \
1144 const wchar_t *s2 = (const wchar_t *) string; \
1145 mbstate_t mbstate; \
1147 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1151 /* The string `s2' might not be NUL terminated. */ \
1152 if (__libc_use_alloca (prec)) \
1153 string = (char *) alloca (prec); \
1154 else if ((string = (char *) malloc (prec)) == NULL) \
1160 string_malloced = 1; \
1161 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1165 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1166 if (len != (size_t) -1) \
1168 assert (__mbsinit (&mbstate)); \
1169 s2 = (const wchar_t *) string; \
1170 if (__libc_use_alloca (len + 1)) \
1171 string = (char *) alloca (len + 1); \
1172 else if ((string = (char *) malloc (len + 1)) == NULL) \
1178 string_malloced = 1; \
1179 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1183 if (len == (size_t) -1) \
1185 /* Illegal wide-character string. */ \
1191 if ((width -= len) < 0) \
1193 outstring (string, len); \
1199 outstring (string, len); \
1202 if (__builtin_expect (string_malloced, 0)) \
1208 /* Orient the stream. */
1213 /* Sanity check of arguments. */
1214 ARGCHECK (s
, format
);
1217 /* Check for correct orientation. */
1218 if (_IO_vtable_offset (s
) == 0 &&
1219 _IO_fwide (s
, sizeof (CHAR_T
) == 1 ? -1 : 1)
1220 != (sizeof (CHAR_T
) == 1 ? -1 : 1))
1221 /* The stream is already oriented otherwise. */
1225 if (UNBUFFERED_P (s
))
1226 /* Use a helper function which will allocate a local temporary buffer
1227 for the stream and then call us again. */
1228 return buffered_vfprintf (s
, format
, ap
);
1230 /* Initialize local variables. */
1232 grouping
= (const char *) -1;
1234 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1235 since on some systems `va_list' is not an integral type. */
1236 __va_copy (ap_save
, ap
);
1242 #ifdef COMPILE_WPRINTF
1243 /* Find the first format specifier. */
1244 f
= lead_str_end
= __find_specwc ((const UCHAR_T
*) format
);
1246 /* Put state for processing format string in initial state. */
1247 memset (&mbstate
, '\0', sizeof (mbstate_t));
1249 /* Find the first format specifier. */
1250 f
= lead_str_end
= __find_specmb (format
, &mbstate
);
1254 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile
, s
);
1257 /* Write the literal text before the first format. */
1258 outstring ((const UCHAR_T
*) format
,
1259 lead_str_end
- (const UCHAR_T
*) format
);
1261 /* If we only have to print a simple string, return now. */
1265 /* Process whole format string. */
1268 #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
1269 # define REF(Name) &&do_##Name - &&do_form_unknown
1271 # define REF(Name) &&do_##Name
1273 #define LABEL(Name) do_##Name
1277 union printf_arg
*args_value
; /* This is not used here but ... */
1278 int is_negative
; /* Flag for negative number. */
1281 unsigned long long int longlong
;
1282 unsigned long int word
;
1285 union printf_arg the_arg
;
1286 CHAR_T
*string
; /* Pointer to argument string. */
1287 int alt
= 0; /* Alternate format. */
1288 int space
= 0; /* Use space prefix if no sign is needed. */
1289 int left
= 0; /* Left-justify output. */
1290 int showsign
= 0; /* Always begin with plus or minus sign. */
1291 int group
= 0; /* Print numbers according grouping rules. */
1292 int is_long_double
= 0; /* Argument is long double/ long long int. */
1293 int is_short
= 0; /* Argument is short int. */
1294 int is_long
= 0; /* Argument is long int. */
1295 int is_char
= 0; /* Argument is promoted (unsigned) char. */
1296 int width
= 0; /* Width of output; 0 means none specified. */
1297 int prec
= -1; /* Precision of output; -1 means none specified. */
1298 /* This flag is set by the 'I' modifier and selects the use of the
1299 `outdigits' as determined by the current locale. */
1300 int use_outdigits
= 0;
1301 UCHAR_T pad
= L_(' ');/* Padding character. */
1305 workend
= &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
1307 /* Get current character in format string. */
1308 JUMP (*++f
, step0_jumps
);
1313 JUMP (*++f
, step0_jumps
);
1318 JUMP (*++f
, step0_jumps
);
1324 JUMP (*++f
, step0_jumps
);
1329 JUMP (*++f
, step0_jumps
);
1335 JUMP (*++f
, step0_jumps
);
1337 /* The '\'' flag. */
1341 if (grouping
== (const char *) -1)
1343 #ifdef COMPILE_WPRINTF
1344 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1345 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1347 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1350 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1351 if (*grouping
== '\0' || *grouping
== CHAR_MAX
1352 #ifdef COMPILE_WPRINTF
1353 || thousands_sep
== L
'\0'
1355 || *thousands_sep
== '\0'
1360 JUMP (*++f
, step0_jumps
);
1364 JUMP (*++f
, step0_jumps
);
1366 /* Get width from argument. */
1367 LABEL (width_asterics
):
1369 const UCHAR_T
*tmp
; /* Temporary value. */
1372 if (ISDIGIT (*tmp
) && read_int (&tmp
) && *tmp
== L_('$'))
1373 /* The width comes from a positional parameter. */
1376 width
= va_arg (ap
, int);
1378 /* Negative width means left justified. */
1386 if (width
+ 32 >= (int) (sizeof (work_buffer
)
1387 / sizeof (work_buffer
[0])))
1389 /* We have to use a special buffer. The "32" is just a safe
1390 bet for all the output which is not counted in the width. */
1391 if (__libc_use_alloca ((width
+ 32) * sizeof (CHAR_T
)))
1392 workend
= ((CHAR_T
*) alloca ((width
+ 32) * sizeof (CHAR_T
))
1396 workstart
= (CHAR_T
*) malloc ((width
+ 32) * sizeof (CHAR_T
));
1397 if (workstart
== NULL
)
1402 workend
= workstart
+ (width
+ 32);
1406 JUMP (*f
, step1_jumps
);
1408 /* Given width in format string. */
1410 width
= read_int (&f
);
1412 if (width
+ 32 >= (int) (sizeof (work_buffer
) / sizeof (work_buffer
[0])))
1414 /* We have to use a special buffer. The "32" is just a safe
1415 bet for all the output which is not counted in the width. */
1416 if (__libc_use_alloca ((width
+ 32) * sizeof (CHAR_T
)))
1417 workend
= ((CHAR_T
*) alloca ((width
+ 32) * sizeof (CHAR_T
))
1421 workstart
= (CHAR_T
*) malloc ((width
+ 32) * sizeof (CHAR_T
));
1422 if (workstart
== NULL
)
1427 workend
= workstart
+ (width
+ 32);
1431 /* Oh, oh. The argument comes from a positional parameter. */
1433 JUMP (*f
, step1_jumps
);
1439 const UCHAR_T
*tmp
; /* Temporary value. */
1442 if (ISDIGIT (*tmp
) && read_int (&tmp
) > 0 && *tmp
== L_('$'))
1443 /* The precision comes from a positional parameter. */
1446 prec
= va_arg (ap
, int);
1448 /* If the precision is negative the precision is omitted. */
1452 else if (ISDIGIT (*f
))
1453 prec
= read_int (&f
);
1457 && prec
+ 32 > (int)(sizeof (work_buffer
) / sizeof (work_buffer
[0])))
1459 if (__libc_use_alloca ((prec
+ 32) * sizeof (CHAR_T
)))
1460 workend
= ((CHAR_T
*) alloca ((prec
+ 32) * sizeof (CHAR_T
)))
1464 workstart
= (CHAR_T
*) malloc ((prec
+ 32) * sizeof (CHAR_T
));
1465 if (workstart
== NULL
)
1470 workend
= workstart
+ (prec
+ 32);
1473 JUMP (*f
, step2_jumps
);
1475 /* Process 'h' modifier. There might another 'h' following. */
1478 JUMP (*++f
, step3a_jumps
);
1480 /* Process 'hh' modifier. */
1481 LABEL (mod_halfhalf
):
1484 JUMP (*++f
, step4_jumps
);
1486 /* Process 'l' modifier. There might another 'l' following. */
1489 JUMP (*++f
, step3b_jumps
);
1491 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1492 allowed to follow. */
1493 LABEL (mod_longlong
):
1496 JUMP (*++f
, step4_jumps
);
1499 is_long_double
= sizeof (size_t) > sizeof (unsigned long int);
1500 is_long
= sizeof (size_t) > sizeof (unsigned int);
1501 JUMP (*++f
, step4_jumps
);
1503 LABEL (mod_ptrdiff_t
):
1504 is_long_double
= sizeof (ptrdiff_t) > sizeof (unsigned long int);
1505 is_long
= sizeof (ptrdiff_t) > sizeof (unsigned int);
1506 JUMP (*++f
, step4_jumps
);
1508 LABEL (mod_intmax_t
):
1509 is_long_double
= sizeof (intmax_t) > sizeof (unsigned long int);
1510 is_long
= sizeof (intmax_t) > sizeof (unsigned int);
1511 JUMP (*++f
, step4_jumps
);
1513 /* Process current format. */
1516 process_arg (((struct printf_spec
*) NULL
));
1517 process_string_arg (((struct printf_spec
*) NULL
));
1519 LABEL (form_unknown
):
1520 if (spec
== L_('\0'))
1522 /* The format string ended before the specifier is complete. */
1527 /* If we are in the fast loop force entering the complicated
1532 /* The format is correctly handled. */
1535 if (__builtin_expect (workstart
!= NULL
, 0))
1539 /* Look for next format specifier. */
1540 #ifdef COMPILE_WPRINTF
1541 f
= __find_specwc ((end_of_spec
= ++f
));
1543 f
= __find_specmb ((end_of_spec
= ++f
), &mbstate
);
1546 /* Write the following constant string. */
1547 outstring (end_of_spec
, f
- end_of_spec
);
1549 while (*f
!= L_('\0'));
1551 /* Unlock stream and return. */
1554 /* Here starts the more complex loop to handle positional parameters. */
1557 /* Array with information about the needed arguments. This has to
1558 be dynamically extensible. */
1560 size_t nspecs_max
= 32; /* A more or less arbitrary start value. */
1561 struct printf_spec
*specs
1562 = alloca (nspecs_max
* sizeof (struct printf_spec
));
1564 /* The number of arguments the format string requests. This will
1565 determine the size of the array needed to store the argument
1569 union printf_arg
*args_value
= NULL
;
1571 /* Positional parameters refer to arguments directly. This could
1572 also determine the maximum number of arguments. Track the
1574 size_t max_ref_arg
= 0;
1576 /* Just a counter. */
1580 if (grouping
== (const char *) -1)
1582 #ifdef COMPILE_WPRINTF
1583 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1584 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1586 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1589 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1590 if (*grouping
== '\0' || *grouping
== CHAR_MAX
)
1594 for (f
= lead_str_end
; *f
!= L_('\0'); f
= specs
[nspecs
++].next_fmt
)
1596 if (nspecs
>= nspecs_max
)
1598 /* Extend the array of format specifiers. */
1599 struct printf_spec
*old
= specs
;
1602 specs
= alloca (nspecs_max
* sizeof (struct printf_spec
));
1604 if (specs
== &old
[nspecs
])
1605 /* Stack grows up, OLD was the last thing allocated;
1607 nspecs_max
+= nspecs_max
/ 2;
1610 /* Copy the old array's elements to the new space. */
1611 memcpy (specs
, old
, nspecs
* sizeof (struct printf_spec
));
1612 if (old
== &specs
[nspecs
])
1613 /* Stack grows down, OLD was just below the new
1614 SPECS. We can use that space when the new space
1616 nspecs_max
+= nspecs_max
/ 2;
1620 /* Parse the format specifier. */
1621 #ifdef COMPILE_WPRINTF
1622 nargs
+= __parse_one_specwc (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1624 nargs
+= __parse_one_specmb (f
, nargs
, &specs
[nspecs
], &max_ref_arg
,
1629 /* Determine the number of arguments the format string consumes. */
1630 nargs
= MAX (nargs
, max_ref_arg
);
1632 /* Allocate memory for the argument descriptions. */
1633 args_type
= alloca (nargs
* sizeof (int));
1634 memset (args_type
, 0, nargs
* sizeof (int));
1635 args_value
= alloca (nargs
* sizeof (union printf_arg
));
1637 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1638 still zero after this loop, format is invalid. For now we
1639 simply use 0 as the value. */
1641 /* Fill in the types of all the arguments. */
1642 for (cnt
= 0; cnt
< nspecs
; ++cnt
)
1644 /* If the width is determined by an argument this is an int. */
1645 if (specs
[cnt
].width_arg
!= -1)
1646 args_type
[specs
[cnt
].width_arg
] = PA_INT
;
1648 /* If the precision is determined by an argument this is an int. */
1649 if (specs
[cnt
].prec_arg
!= -1)
1650 args_type
[specs
[cnt
].prec_arg
] = PA_INT
;
1652 switch (specs
[cnt
].ndata_args
)
1654 case 0: /* No arguments. */
1656 case 1: /* One argument; we already have the type. */
1657 args_type
[specs
[cnt
].data_arg
] = specs
[cnt
].data_arg_type
;
1660 /* We have more than one argument for this format spec.
1661 We must call the arginfo function again to determine
1663 (void) (*__printf_arginfo_table
[specs
[cnt
].info
.spec
])
1665 specs
[cnt
].ndata_args
, &args_type
[specs
[cnt
].data_arg
]);
1670 /* Now we know all the types and the order. Fill in the argument
1672 for (cnt
= 0; cnt
< nargs
; ++cnt
)
1673 switch (args_type
[cnt
])
1675 #define T(tag, mem, type) \
1677 args_value[cnt].mem = va_arg (ap_save, type); \
1680 T (PA_CHAR
, pa_int
, int); /* Promoted. */
1681 T (PA_WCHAR
, pa_wchar
, wint_t);
1682 T (PA_INT
|PA_FLAG_SHORT
, pa_int
, int); /* Promoted. */
1683 T (PA_INT
, pa_int
, int);
1684 T (PA_INT
|PA_FLAG_LONG
, pa_long_int
, long int);
1685 T (PA_INT
|PA_FLAG_LONG_LONG
, pa_long_long_int
, long long int);
1686 T (PA_FLOAT
, pa_double
, double); /* Promoted. */
1687 T (PA_DOUBLE
, pa_double
, double);
1688 T (PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
, pa_long_double
, long double);
1689 T (PA_STRING
, pa_string
, const char *);
1690 T (PA_WSTRING
, pa_wstring
, const wchar_t *);
1691 T (PA_POINTER
, pa_pointer
, void *);
1694 if ((args_type
[cnt
] & PA_FLAG_PTR
) != 0)
1695 args_value
[cnt
].pa_pointer
= va_arg (ap_save
, void *);
1697 args_value
[cnt
].pa_long_double
= 0.0;
1701 /* Now walk through all format specifiers and process them. */
1702 for (; (size_t) nspecs_done
< nspecs
; ++nspecs_done
)
1705 #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
1706 # define REF(Name) &&do2_##Name - &&do_form_unknown
1708 # define REF(Name) &&do2_##Name
1711 #define LABEL(Name) do2_##Name
1717 unsigned long long int longlong
;
1718 unsigned long int word
;
1721 union printf_arg the_arg
;
1722 CHAR_T
*string
; /* Pointer to argument string. */
1724 /* Fill variables from values in struct. */
1725 int alt
= specs
[nspecs_done
].info
.alt
;
1726 int space
= specs
[nspecs_done
].info
.space
;
1727 int left
= specs
[nspecs_done
].info
.left
;
1728 int showsign
= specs
[nspecs_done
].info
.showsign
;
1729 int group
= specs
[nspecs_done
].info
.group
;
1730 int is_long_double
= specs
[nspecs_done
].info
.is_long_double
;
1731 int is_short
= specs
[nspecs_done
].info
.is_short
;
1732 int is_char
= specs
[nspecs_done
].info
.is_char
;
1733 int is_long
= specs
[nspecs_done
].info
.is_long
;
1734 int width
= specs
[nspecs_done
].info
.width
;
1735 int prec
= specs
[nspecs_done
].info
.prec
;
1736 int use_outdigits
= specs
[nspecs_done
].info
.i18n
;
1737 char pad
= specs
[nspecs_done
].info
.pad
;
1738 CHAR_T spec
= specs
[nspecs_done
].info
.spec
;
1739 CHAR_T
*workstart
= NULL
;
1741 /* Fill in last information. */
1742 if (specs
[nspecs_done
].width_arg
!= -1)
1744 /* Extract the field width from an argument. */
1745 specs
[nspecs_done
].info
.width
=
1746 args_value
[specs
[nspecs_done
].width_arg
].pa_int
;
1748 if (specs
[nspecs_done
].info
.width
< 0)
1749 /* If the width value is negative left justification is
1750 selected and the value is taken as being positive. */
1752 specs
[nspecs_done
].info
.width
*= -1;
1753 left
= specs
[nspecs_done
].info
.left
= 1;
1755 width
= specs
[nspecs_done
].info
.width
;
1758 if (specs
[nspecs_done
].prec_arg
!= -1)
1760 /* Extract the precision from an argument. */
1761 specs
[nspecs_done
].info
.prec
=
1762 args_value
[specs
[nspecs_done
].prec_arg
].pa_int
;
1764 if (specs
[nspecs_done
].info
.prec
< 0)
1765 /* If the precision is negative the precision is
1767 specs
[nspecs_done
].info
.prec
= -1;
1769 prec
= specs
[nspecs_done
].info
.prec
;
1772 /* Maybe the buffer is too small. */
1773 if (MAX (prec
, width
) + 32 > (int) (sizeof (work_buffer
)
1776 if (__libc_use_alloca ((MAX (prec
, width
) + 32)
1778 workend
= ((CHAR_T
*) alloca ((MAX (prec
, width
) + 32)
1780 + (MAX (prec
, width
) + 32));
1783 workstart
= (CHAR_T
*) malloc ((MAX (prec
, width
) + 32)
1785 workend
= workstart
+ (MAX (prec
, width
) + 32);
1789 /* Process format specifiers. */
1792 JUMP (spec
, step4_jumps
);
1794 process_arg ((&specs
[nspecs_done
]));
1795 process_string_arg ((&specs
[nspecs_done
]));
1797 LABEL (form_unknown
):
1799 extern printf_function
**__printf_function_table
;
1801 printf_function
*function
;
1806 (__printf_function_table
== NULL
? NULL
:
1807 __printf_function_table
[specs
[nspecs_done
].info
.spec
]);
1809 if (function
== NULL
)
1810 function
= &printf_unknown
;
1812 ptr
= alloca (specs
[nspecs_done
].ndata_args
1813 * sizeof (const void *));
1815 /* Fill in an array of pointers to the argument values. */
1816 for (i
= 0; i
< specs
[nspecs_done
].ndata_args
; ++i
)
1817 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
1819 /* Call the function. */
1820 function_done
= (*function
) (s
, &specs
[nspecs_done
].info
, ptr
);
1822 /* If an error occurred we don't have information about #
1824 if (function_done
< 0)
1830 done
+= function_done
;
1835 if (__builtin_expect (workstart
!= NULL
, 0))
1839 /* Write the following constant string. */
1840 outstring (specs
[nspecs_done
].end_of_fmt
,
1841 specs
[nspecs_done
].next_fmt
1842 - specs
[nspecs_done
].end_of_fmt
);
1847 if (__builtin_expect (workstart
!= NULL
, 0))
1849 /* Unlock the stream. */
1850 _IO_funlockfile (s
);
1851 _IO_cleanup_region_end (0);
1856 /* Handle an unknown format specifier. This prints out a canonicalized
1857 representation of the format spec itself. */
1859 printf_unknown (FILE *s
, const struct printf_info
*info
,
1860 const void *const *args
)
1864 CHAR_T work_buffer
[MAX (info
->width
, info
->spec
) + 32];
1865 CHAR_T
*const workend
1866 = &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
1877 else if (info
->space
)
1881 if (info
->pad
== L_('0'))
1886 if (info
->width
!= 0)
1888 w
= _itoa_word (info
->width
, workend
, 10, 0);
1893 if (info
->prec
!= -1)
1896 w
= _itoa_word (info
->prec
, workend
, 10, 0);
1901 if (info
->spec
!= L_('\0'))
1902 outchar (info
->spec
);
1908 /* Group the digits according to the grouping rules of the current locale.
1909 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1912 group_number (CHAR_T
*w
, CHAR_T
*rear_ptr
, const char *grouping
,
1913 #ifdef COMPILE_WPRINTF
1914 wchar_t thousands_sep
1916 const char *thousands_sep
1922 #ifndef COMPILE_WPRINTF
1923 int tlen
= strlen (thousands_sep
);
1926 /* We treat all negative values like CHAR_MAX. */
1928 if (*grouping
== CHAR_MAX
|| *grouping
<= 0)
1929 /* No grouping should be done. */
1934 /* Copy existing string so that nothing gets overwritten. */
1935 src
= (CHAR_T
*) alloca ((rear_ptr
- w
) * sizeof (CHAR_T
));
1936 s
= (CHAR_T
*) __mempcpy (src
, w
,
1937 (rear_ptr
- w
) * sizeof (CHAR_T
));
1940 /* Process all characters in the string. */
1945 if (--len
== 0 && s
> src
)
1947 /* A new group begins. */
1948 #ifdef COMPILE_WPRINTF
1949 *--w
= thousands_sep
;
1953 *--w
= thousands_sep
[--cnt
];
1957 if (*grouping
== CHAR_MAX
1963 /* No further grouping to be done.
1964 Copy the rest of the number. */
1970 else if (*grouping
!= '\0')
1971 /* The previous grouping repeats ad infinitum. */
1980 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
1983 struct _IO_FILE_plus _f
;
1984 #ifdef COMPILE_WPRINTF
1985 struct _IO_wide_data _wide_data
;
1987 _IO_FILE
*_put_stream
;
1988 #ifdef _IO_MTSAFE_IO
1994 _IO_helper_overflow (_IO_FILE
*s
, int c
)
1996 _IO_FILE
*target
= ((struct helper_file
*) s
)->_put_stream
;
1997 #ifdef COMPILE_WPRINTF
1998 int used
= s
->_wide_data
->_IO_write_ptr
- s
->_wide_data
->_IO_write_base
;
2001 _IO_size_t written
= _IO_sputn (target
, s
->_wide_data
->_IO_write_base
,
2003 s
->_wide_data
->_IO_write_ptr
-= written
;
2006 int used
= s
->_IO_write_ptr
- s
->_IO_write_base
;
2009 _IO_size_t written
= _IO_sputn (target
, s
->_IO_write_base
, used
);
2010 s
->_IO_write_ptr
-= written
;
2016 #ifdef COMPILE_WPRINTF
2017 static const struct _IO_jump_t _IO_helper_jumps
=
2020 JUMP_INIT (finish
, INTUSE(_IO_wdefault_finish
)),
2021 JUMP_INIT (overflow
, _IO_helper_overflow
),
2022 JUMP_INIT (underflow
, _IO_default_underflow
),
2023 JUMP_INIT (uflow
, INTUSE(_IO_default_uflow
)),
2024 JUMP_INIT (pbackfail
, (_IO_pbackfail_t
) INTUSE(_IO_wdefault_pbackfail
)),
2025 JUMP_INIT (xsputn
, INTUSE(_IO_wdefault_xsputn
)),
2026 JUMP_INIT (xsgetn
, INTUSE(_IO_wdefault_xsgetn
)),
2027 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2028 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2029 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2030 JUMP_INIT (sync
, _IO_default_sync
),
2031 JUMP_INIT (doallocate
, INTUSE(_IO_wdefault_doallocate
)),
2032 JUMP_INIT (read
, _IO_default_read
),
2033 JUMP_INIT (write
, _IO_default_write
),
2034 JUMP_INIT (seek
, _IO_default_seek
),
2035 JUMP_INIT (close
, _IO_default_close
),
2036 JUMP_INIT (stat
, _IO_default_stat
)
2039 static const struct _IO_jump_t _IO_helper_jumps
=
2042 JUMP_INIT (finish
, INTUSE(_IO_default_finish
)),
2043 JUMP_INIT (overflow
, _IO_helper_overflow
),
2044 JUMP_INIT (underflow
, _IO_default_underflow
),
2045 JUMP_INIT (uflow
, INTUSE(_IO_default_uflow
)),
2046 JUMP_INIT (pbackfail
, INTUSE(_IO_default_pbackfail
)),
2047 JUMP_INIT (xsputn
, INTUSE(_IO_default_xsputn
)),
2048 JUMP_INIT (xsgetn
, INTUSE(_IO_default_xsgetn
)),
2049 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2050 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2051 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2052 JUMP_INIT (sync
, _IO_default_sync
),
2053 JUMP_INIT (doallocate
, INTUSE(_IO_default_doallocate
)),
2054 JUMP_INIT (read
, _IO_default_read
),
2055 JUMP_INIT (write
, _IO_default_write
),
2056 JUMP_INIT (seek
, _IO_default_seek
),
2057 JUMP_INIT (close
, _IO_default_close
),
2058 JUMP_INIT (stat
, _IO_default_stat
)
2064 buffered_vfprintf (register _IO_FILE
*s
, const CHAR_T
*format
,
2067 CHAR_T buf
[_IO_BUFSIZ
];
2068 struct helper_file helper
;
2069 register _IO_FILE
*hp
= (_IO_FILE
*) &helper
._f
;
2070 int result
, to_flush
;
2072 /* Orient the stream. */
2077 /* Initialize helper. */
2078 helper
._put_stream
= s
;
2079 #ifdef COMPILE_WPRINTF
2080 hp
->_wide_data
= &helper
._wide_data
;
2081 _IO_wsetp (hp
, buf
, buf
+ sizeof buf
/ sizeof (CHAR_T
));
2084 _IO_setp (hp
, buf
, buf
+ sizeof buf
);
2087 hp
->_IO_file_flags
= _IO_MAGIC
|_IO_NO_READS
|_IO_USER_LOCK
;
2088 #if _IO_JUMPS_OFFSET
2089 hp
->_vtable_offset
= 0;
2091 #ifdef _IO_MTSAFE_IO
2094 _IO_JUMPS (&helper
._f
) = (struct _IO_jump_t
*) &_IO_helper_jumps
;
2096 /* Now print to helper instead. */
2097 #ifndef COMPILE_WPRINTF
2098 result
= INTUSE(_IO_vfprintf
) (hp
, format
, args
);
2100 result
= vfprintf (hp
, format
, args
);
2104 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile
, s
);
2107 /* Now flush anything from the helper to the S. */
2108 #ifdef COMPILE_WPRINTF
2109 if ((to_flush
= (hp
->_wide_data
->_IO_write_ptr
2110 - hp
->_wide_data
->_IO_write_base
)) > 0)
2112 if ((int) _IO_sputn (s
, hp
->_wide_data
->_IO_write_base
, to_flush
)
2117 if ((to_flush
= hp
->_IO_write_ptr
- hp
->_IO_write_base
) > 0)
2119 if ((int) _IO_sputn (s
, hp
->_IO_write_base
, to_flush
) != to_flush
)
2124 /* Unlock the stream. */
2125 _IO_funlockfile (s
);
2126 __libc_cleanup_region_end (0);
2133 /* This is for glibc. */
2134 # ifdef COMPILE_WPRINTF
2135 strong_alias (_IO_vfwprintf
, __vfwprintf
);
2136 weak_alias (_IO_vfwprintf
, vfwprintf
);
2138 strong_alias (_IO_vfprintf
, vfprintf
);
2139 libc_hidden_def (vfprintf
)
2140 INTDEF(_IO_vfprintf
)
2143 # if defined __ELF__ || defined __GNU_LIBRARY__
2144 # include <gnu-stabs.h>
2146 # ifdef COMPILE_WPRINTF
2147 weak_alias (_IO_vfwprintf
, vfwprintf
);
2149 weak_alias (_IO_vfprintf
, vfprintf
);