1 /* Copyright (C) 1991,92,93,94,95,96,97,98,99 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
27 #include <bits/libc-lock.h>
28 #include <sys/param.h>
30 #include <locale/localeinfo.h>
32 /* This code is shared between the standard stdio implementation found
33 in GNU C library and the libio implementation originally found in
36 Beside this it is also shared between the normal and wide character
37 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
41 /* This code is for use in libio. */
43 # define FILE _IO_FILE
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) isdigit (Ch)
74 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
75 # define PAD(Padchar) \
77 done += _IO_padn (s, (Padchar), width)
78 # define PUTC(C, F) _IO_putc_unlocked (C, F)
79 # define ORIENT if (s->_vtable_offset == 0 && _IO_fwide (s, -1) != -1)\
84 # define vfprintf _IO_vfwprintf
85 # define CHAR_T wchar_t
86 /* This is a hack!!! There should be a type uwchar_t. */
87 # define UCHAR_T unsigned int /* uwchar_t */
89 # define L_(Str) L##Str
90 # define ISDIGIT(Ch) iswdigit (Ch)
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)
104 #else /* ! USE_IN_LIBIO */
105 /* This code is for use in the GNU C library. */
107 # define ARGCHECK(S, Format) \
110 /* Check file argument for consistence. */ \
111 if (!__validfp (S) || !S->__mode.__write) \
113 __set_errno (EBADF); \
116 if (Format == NULL) \
118 __set_errno (EINVAL); \
123 if (__flshfp (S, EOF) == EOF) \
128 # define UNBUFFERED_P(s) ((s)->__buffer == NULL)
131 # define UCHAR_T unsigned char
134 # define ISDIGIT(Ch) isdigit (Ch)
136 # define PUT(F, S, N) fwrite (S, 1, N, F)
137 ssize_t __printf_pad
__P ((FILE *, char pad
, size_t n
));
138 # define PAD(Padchar) \
140 { ssize_t __res = __printf_pad (s, (Padchar), width); \
147 # define PUTC(C, F) putc (C, F)
149 /* XXX These declarations should go as soon as the stdio header files
150 have these prototypes. */
151 extern void __flockfile (FILE *);
152 extern void __funlockfile (FILE *);
153 #endif /* USE_IN_LIBIO */
155 /* Include the shared code for parsing the format string. */
156 #include "printf-parse.h"
159 #define outchar(Ch) \
162 register const INT_T outc = (Ch); \
163 if (PUTC (outc, s) == EOF) \
173 #define outstring(String, Len) \
176 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
185 /* For handling long_double and longlong we use the same flag. If
186 `long' and `long long' are effectively the same type define it to
188 #if LONG_MAX == LONG_LONG_MAX
189 # define is_longlong 0
191 # define is_longlong is_long_double
194 /* If `long' and `int' is effectively the same type we don't have to
195 handle `long separately. */
196 #if INT_MAX == LONG_MAX
197 # define is_long_num 0
199 # define is_long_num is_long
203 /* Global variables. */
204 static const CHAR_T null
[] = L_("(null)");
207 /* Helper function to provide temporary buffering for unbuffered streams. */
208 static int buffered_vfprintf
__P ((FILE *stream
, const CHAR_T
*fmt
, va_list))
211 /* Handle unknown format specifier. */
212 static int printf_unknown
__P ((FILE *, const struct printf_info
*,
213 const void *const *));
215 /* Group digits of number string. */
216 static CHAR_T
*group_number
__P ((CHAR_T
*, CHAR_T
*, const char *, wchar_t))
220 /* The function itself. */
222 vfprintf (FILE *s
, const CHAR_T
*format
, va_list ap
)
224 /* The character used as thousands separator. */
225 wchar_t thousands_sep
;
227 /* The string describing the size of groups of digits. */
228 const char *grouping
;
230 /* Place to accumulate the result. */
233 /* Current character in format string. */
236 /* End of leading constant string. */
237 const UCHAR_T
*lead_str_end
;
239 /* Points to next format specifier. */
240 const UCHAR_T
*end_of_spec
;
242 /* Buffer intermediate results. */
243 CHAR_T work_buffer
[1000];
246 /* State for restartable multibyte character handling functions. */
247 #ifndef COMPILE_WPRINTF
251 /* We have to save the original argument pointer. */
254 /* Count number of specifiers we already processed. */
257 /* For the %m format we may need the current `errno' value. */
258 int save_errno
= errno
;
261 /* This table maps a character into a number representing a
262 class. In each step there is a destination label for each
264 static const int jump_table
[] =
266 /* ' ' */ 1, 0, 0, /* '#' */ 4,
267 0, /* '%' */ 14, 0, /* '\''*/ 6,
268 0, 0, /* '*' */ 7, /* '+' */ 2,
269 0, /* '-' */ 3, /* '.' */ 9, 0,
270 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
271 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
272 /* '8' */ 8, /* '9' */ 8, 0, 0,
274 0, /* 'A' */ 26, 0, /* 'C' */ 25,
275 0, /* 'E' */ 19, 0, /* 'G' */ 19,
277 /* 'L' */ 12, 0, 0, 0,
278 0, 0, 0, /* 'S' */ 21,
280 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
282 0, /* 'a' */ 26, 0, /* 'c' */ 20,
283 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
284 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
285 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
286 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
287 /* 't' */ 27, /* 'u' */ 16, 0, 0,
288 /* 'x' */ 18, 0, /* 'z' */ 13
291 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
292 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
293 #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined PIC
294 /* 'int' is enough and it saves some space on 64 bit systems. */
295 # define JUMP_TABLE_TYPE const int
296 # define JUMP(ChExpr, table) \
302 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
303 : table[CHAR_CLASS (spec)]; \
304 ptr = &&do_form_unknown + offset; \
309 # define JUMP_TABLE_TYPE const void *const
310 # define JUMP(ChExpr, table) \
315 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
316 : table[CHAR_CLASS (spec)]; \
322 #define STEP0_3_TABLE \
323 /* Step 0: at the beginning. */ \
324 static JUMP_TABLE_TYPE step0_jumps[29] = \
326 REF (form_unknown), \
327 REF (flag_space), /* for ' ' */ \
328 REF (flag_plus), /* for '+' */ \
329 REF (flag_minus), /* for '-' */ \
330 REF (flag_hash), /* for '<hash>' */ \
331 REF (flag_zero), /* for '0' */ \
332 REF (flag_quote), /* for '\'' */ \
333 REF (width_asterics), /* for '*' */ \
334 REF (width), /* for '1'...'9' */ \
335 REF (precision), /* for '.' */ \
336 REF (mod_half), /* for 'h' */ \
337 REF (mod_long), /* for 'l' */ \
338 REF (mod_longlong), /* for 'L', 'q' */ \
339 REF (mod_size_t), /* for 'z', 'Z' */ \
340 REF (form_percent), /* for '%' */ \
341 REF (form_integer), /* for 'd', 'i' */ \
342 REF (form_unsigned), /* for 'u' */ \
343 REF (form_octal), /* for 'o' */ \
344 REF (form_hexa), /* for 'X', 'x' */ \
345 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
346 REF (form_character), /* for 'c' */ \
347 REF (form_string), /* for 's', 'S' */ \
348 REF (form_pointer), /* for 'p' */ \
349 REF (form_number), /* for 'n' */ \
350 REF (form_strerror), /* for 'm' */ \
351 REF (form_wcharacter), /* for 'C' */ \
352 REF (form_floathex), /* for 'A', 'a' */ \
353 REF (mod_ptrdiff_t), /* for 't' */ \
354 REF (mod_intmax_t), /* for 'j' */ \
356 /* Step 1: after processing width. */ \
357 static JUMP_TABLE_TYPE step1_jumps[29] = \
359 REF (form_unknown), \
360 REF (form_unknown), /* for ' ' */ \
361 REF (form_unknown), /* for '+' */ \
362 REF (form_unknown), /* for '-' */ \
363 REF (form_unknown), /* for '<hash>' */ \
364 REF (form_unknown), /* for '0' */ \
365 REF (form_unknown), /* for '\'' */ \
366 REF (form_unknown), /* for '*' */ \
367 REF (form_unknown), /* for '1'...'9' */ \
368 REF (precision), /* for '.' */ \
369 REF (mod_half), /* for 'h' */ \
370 REF (mod_long), /* for 'l' */ \
371 REF (mod_longlong), /* for 'L', 'q' */ \
372 REF (mod_size_t), /* for 'z', 'Z' */ \
373 REF (form_percent), /* for '%' */ \
374 REF (form_integer), /* for 'd', 'i' */ \
375 REF (form_unsigned), /* for 'u' */ \
376 REF (form_octal), /* for 'o' */ \
377 REF (form_hexa), /* for 'X', 'x' */ \
378 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
379 REF (form_character), /* for 'c' */ \
380 REF (form_string), /* for 's', 'S' */ \
381 REF (form_pointer), /* for 'p' */ \
382 REF (form_number), /* for 'n' */ \
383 REF (form_strerror), /* for 'm' */ \
384 REF (form_wcharacter), /* for 'C' */ \
385 REF (form_floathex), /* for 'A', 'a' */ \
386 REF (mod_ptrdiff_t), /* for 't' */ \
387 REF (mod_intmax_t) /* for 'j' */ \
389 /* Step 2: after processing precision. */ \
390 static JUMP_TABLE_TYPE step2_jumps[29] = \
392 REF (form_unknown), \
393 REF (form_unknown), /* for ' ' */ \
394 REF (form_unknown), /* for '+' */ \
395 REF (form_unknown), /* for '-' */ \
396 REF (form_unknown), /* for '<hash>' */ \
397 REF (form_unknown), /* for '0' */ \
398 REF (form_unknown), /* for '\'' */ \
399 REF (form_unknown), /* for '*' */ \
400 REF (form_unknown), /* for '1'...'9' */ \
401 REF (form_unknown), /* for '.' */ \
402 REF (mod_half), /* for 'h' */ \
403 REF (mod_long), /* for 'l' */ \
404 REF (mod_longlong), /* for 'L', 'q' */ \
405 REF (mod_size_t), /* for 'z', 'Z' */ \
406 REF (form_percent), /* for '%' */ \
407 REF (form_integer), /* for 'd', 'i' */ \
408 REF (form_unsigned), /* for 'u' */ \
409 REF (form_octal), /* for 'o' */ \
410 REF (form_hexa), /* for 'X', 'x' */ \
411 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
412 REF (form_character), /* for 'c' */ \
413 REF (form_string), /* for 's', 'S' */ \
414 REF (form_pointer), /* for 'p' */ \
415 REF (form_number), /* for 'n' */ \
416 REF (form_strerror), /* for 'm' */ \
417 REF (form_wcharacter), /* for 'C' */ \
418 REF (form_floathex), /* for 'A', 'a' */ \
419 REF (mod_ptrdiff_t), /* for 't' */ \
420 REF (mod_intmax_t) /* for 'j' */ \
422 /* Step 3a: after processing first 'h' modifier. */ \
423 static JUMP_TABLE_TYPE step3a_jumps[29] = \
425 REF (form_unknown), \
426 REF (form_unknown), /* for ' ' */ \
427 REF (form_unknown), /* for '+' */ \
428 REF (form_unknown), /* for '-' */ \
429 REF (form_unknown), /* for '<hash>' */ \
430 REF (form_unknown), /* for '0' */ \
431 REF (form_unknown), /* for '\'' */ \
432 REF (form_unknown), /* for '*' */ \
433 REF (form_unknown), /* for '1'...'9' */ \
434 REF (form_unknown), /* for '.' */ \
435 REF (mod_halfhalf), /* for 'h' */ \
436 REF (form_unknown), /* for 'l' */ \
437 REF (form_unknown), /* for 'L', 'q' */ \
438 REF (form_unknown), /* for 'z', 'Z' */ \
439 REF (form_percent), /* for '%' */ \
440 REF (form_integer), /* for 'd', 'i' */ \
441 REF (form_unsigned), /* for 'u' */ \
442 REF (form_octal), /* for 'o' */ \
443 REF (form_hexa), /* for 'X', 'x' */ \
444 REF (form_unknown), /* for 'E', 'e', 'f', 'G', 'g' */ \
445 REF (form_unknown), /* for 'c' */ \
446 REF (form_unknown), /* for 's', 'S' */ \
447 REF (form_unknown), /* for 'p' */ \
448 REF (form_number), /* for 'n' */ \
449 REF (form_unknown), /* for 'm' */ \
450 REF (form_unknown), /* for 'C' */ \
451 REF (form_unknown), /* for 'A', 'a' */ \
452 REF (form_unknown), /* for 't' */ \
453 REF (form_unknown) /* for 'j' */ \
455 /* Step 3b: after processing first 'l' modifier. */ \
456 static JUMP_TABLE_TYPE step3b_jumps[29] = \
458 REF (form_unknown), \
459 REF (form_unknown), /* for ' ' */ \
460 REF (form_unknown), /* for '+' */ \
461 REF (form_unknown), /* for '-' */ \
462 REF (form_unknown), /* for '<hash>' */ \
463 REF (form_unknown), /* for '0' */ \
464 REF (form_unknown), /* for '\'' */ \
465 REF (form_unknown), /* for '*' */ \
466 REF (form_unknown), /* for '1'...'9' */ \
467 REF (form_unknown), /* for '.' */ \
468 REF (form_unknown), /* for 'h' */ \
469 REF (mod_longlong), /* for 'l' */ \
470 REF (form_unknown), /* for 'L', 'q' */ \
471 REF (form_unknown), /* for 'z', 'Z' */ \
472 REF (form_percent), /* for '%' */ \
473 REF (form_integer), /* for 'd', 'i' */ \
474 REF (form_unsigned), /* for 'u' */ \
475 REF (form_octal), /* for 'o' */ \
476 REF (form_hexa), /* for 'X', 'x' */ \
477 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
478 REF (form_character), /* for 'c' */ \
479 REF (form_string), /* for 's', 'S' */ \
480 REF (form_pointer), /* for 'p' */ \
481 REF (form_number), /* for 'n' */ \
482 REF (form_strerror), /* for 'm' */ \
483 REF (form_wcharacter), /* for 'C' */ \
484 REF (form_floathex), /* for 'A', 'a' */ \
485 REF (form_unknown), /* for 't' */ \
486 REF (form_unknown) /* for 'j' */ \
489 #define STEP4_TABLE \
490 /* Step 4: processing format specifier. */ \
491 static JUMP_TABLE_TYPE step4_jumps[29] = \
493 REF (form_unknown), \
494 REF (form_unknown), /* for ' ' */ \
495 REF (form_unknown), /* for '+' */ \
496 REF (form_unknown), /* for '-' */ \
497 REF (form_unknown), /* for '<hash>' */ \
498 REF (form_unknown), /* for '0' */ \
499 REF (form_unknown), /* for '\'' */ \
500 REF (form_unknown), /* for '*' */ \
501 REF (form_unknown), /* for '1'...'9' */ \
502 REF (form_unknown), /* for '.' */ \
503 REF (form_unknown), /* for 'h' */ \
504 REF (form_unknown), /* for 'l' */ \
505 REF (form_unknown), /* for 'L', 'q' */ \
506 REF (form_unknown), /* for 'z', 'Z' */ \
507 REF (form_percent), /* for '%' */ \
508 REF (form_integer), /* for 'd', 'i' */ \
509 REF (form_unsigned), /* for 'u' */ \
510 REF (form_octal), /* for 'o' */ \
511 REF (form_hexa), /* for 'X', 'x' */ \
512 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
513 REF (form_character), /* for 'c' */ \
514 REF (form_string), /* for 's', 'S' */ \
515 REF (form_pointer), /* for 'p' */ \
516 REF (form_number), /* for 'n' */ \
517 REF (form_strerror), /* for 'm' */ \
518 REF (form_wcharacter), /* for 'C' */ \
519 REF (form_floathex), /* for 'A', 'a' */ \
520 REF (form_unknown), /* for 't' */ \
521 REF (form_unknown) /* for 'j' */ \
525 #define process_arg(fspec) \
526 /* Start real work. We know about all flags and modifiers and \
527 now process the wanted format specifier. */ \
528 LABEL (form_percent): \
529 /* Write a literal "%". */ \
533 LABEL (form_integer): \
534 /* Signed decimal integer. */ \
539 long long int signed_number; \
542 signed_number = va_arg (ap, long long int); \
544 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
546 is_negative = signed_number < 0; \
547 number.longlong = is_negative ? (- signed_number) : signed_number; \
549 goto LABEL (longlong_number); \
553 long int signed_number; \
558 signed_number = va_arg (ap, long int); \
559 else /* `char' and `short int' will be promoted to `int'. */ \
560 signed_number = va_arg (ap, int); \
564 signed_number = args_value[fspec->data_arg].pa_long_int; \
566 signed_number = args_value[fspec->data_arg].pa_int; \
568 is_negative = signed_number < 0; \
569 number.word = is_negative ? (- signed_number) : signed_number; \
571 goto LABEL (number); \
575 LABEL (form_unsigned): \
576 /* Unsigned decimal integer. */ \
578 goto LABEL (unsigned_number); \
581 LABEL (form_octal): \
582 /* Unsigned octal integer. */ \
584 goto LABEL (unsigned_number); \
588 /* Unsigned hexadecimal integer. */ \
591 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
593 /* ISO specifies the `+' and ` ' flags only for signed \
602 number.longlong = va_arg (ap, unsigned long long int); \
604 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
606 LABEL (longlong_number): \
608 /* Supply a default precision if none was given. */ \
611 /* We have to take care for the '0' flag. If a precision \
612 is given it must be ignored. */ \
615 /* If the precision is 0 and the number is 0 nothing has to \
616 be written for the number, except for the 'o' format in \
618 if (prec == 0 && number.longlong == 0) \
621 if (base == 8 && alt) \
622 *string-- = L_('0'); \
626 /* Put the number in WORK. */ \
627 string = _itoa (number.longlong, workend + 1, base, \
630 if (group && grouping) \
631 string = group_number (string, workend, grouping, \
634 /* Simplify further test for num != 0. */ \
635 number.word = number.longlong != 0; \
642 number.word = va_arg (ap, unsigned long int); \
643 else if (!is_short) \
644 number.word = va_arg (ap, unsigned int); \
646 number.word = (unsigned short int) va_arg (ap, unsigned int); \
650 number.word = args_value[fspec->data_arg].pa_u_long_int; \
652 number.word = (unsigned char) \
653 args_value[fspec->data_arg].pa_char; \
654 else if (!is_short) \
655 number.word = args_value[fspec->data_arg].pa_u_int; \
657 number.word = (unsigned short int) \
658 args_value[fspec->data_arg].pa_u_short_int; \
662 /* Supply a default precision if none was given. */ \
665 /* We have to take care for the '0' flag. If a precision \
666 is given it must be ignored. */ \
669 /* If the precision is 0 and the number is 0 nothing has to \
670 be written for the number, except for the 'o' format in \
672 if (prec == 0 && number.word == 0) \
675 if (base == 8 && alt) \
676 *string-- = L_('0'); \
680 /* Put the number in WORK. */ \
681 string = _itoa_word (number.word, workend + 1, base, \
684 if (group && grouping) \
685 string = group_number (string, workend, grouping, \
690 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
691 /* Add octal marker. */ \
692 *string-- = L_('0'); \
694 prec = MAX (0, prec - (workend - string)); \
698 width -= workend - string + prec; \
700 if (number.word != 0 && alt && base == 16) \
701 /* Account for 0X hex marker. */ \
704 if (is_negative || showsign || space) \
707 if (pad == L_(' ')) \
720 if (number.word != 0 && alt && base == 16) \
729 outstring (string + 1, workend - string); \
751 if (number.word != 0 && alt && base == 16) \
758 width -= workend - string + prec; \
768 outstring (string + 1, workend - string); \
774 LABEL (form_float): \
776 /* Floating-point number. This is handled by printf_fp.c. */ \
777 extern int __printf_fp __P ((FILE *, const struct printf_info *, \
778 const void **const)); \
784 struct printf_info info = { prec: prec, \
787 is_long_double: is_long_double, \
788 is_short: is_short, \
793 showsign: showsign, \
797 wide: sizeof (CHAR_T) != 1 }; \
799 if (is_long_double) \
800 the_arg.pa_long_double = va_arg (ap, long double); \
802 the_arg.pa_double = va_arg (ap, double); \
803 ptr = (const void *) &the_arg; \
805 function_done = __printf_fp (s, &info, &ptr); \
809 ptr = (const void *) &args_value[fspec->data_arg]; \
811 function_done = __printf_fp (s, &fspec->info, &ptr); \
814 if (function_done < 0) \
816 /* Error in print handler. */ \
821 done += function_done; \
825 LABEL (form_floathex): \
827 /* FLoating point number printed as hexadecimal number. */ \
828 extern int __printf_fphex __P ((FILE *, const struct printf_info *, \
829 const void **const)); \
835 struct printf_info info = { prec: prec, \
838 is_long_double: is_long_double, \
839 is_short: is_short, \
844 showsign: showsign, \
848 wide: sizeof (CHAR_T) != 1 }; \
850 if (is_long_double) \
851 the_arg.pa_long_double = va_arg (ap, long double); \
853 the_arg.pa_double = va_arg (ap, double); \
854 ptr = (const void *) &the_arg; \
856 function_done = __printf_fphex (s, &info, &ptr); \
860 ptr = (const void *) &args_value[fspec->data_arg]; \
862 function_done = __printf_fphex (s, &fspec->info, &ptr); \
865 if (function_done < 0) \
867 /* Error in print handler. */ \
872 done += function_done; \
876 LABEL (form_pointer): \
877 /* Generic pointer. */ \
881 ptr = va_arg (ap, void *); \
883 ptr = args_value[fspec->data_arg].pa_pointer; \
886 /* If the pointer is not NULL, write it as a %#x spec. */ \
888 number.word = (unsigned long int) ptr; \
893 goto LABEL (number); \
897 /* Write "(nil)" for a nil pointer. */ \
898 string = (CHAR_T *) L_("(nil)"); \
899 /* Make sure the full string "(nil)" is printed. */ \
902 is_long = 0; /* This is no wide-char string. */ \
903 goto LABEL (print_string); \
908 LABEL (form_number): \
909 /* Answer the count of characters written. */ \
913 *(long long int *) va_arg (ap, void *) = done; \
914 else if (is_long_num) \
915 *(long int *) va_arg (ap, void *) = done; \
916 else if (!is_short) \
917 *(int *) va_arg (ap, void *) = done; \
919 *(short int *) va_arg (ap, void *) = done; \
923 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
924 else if (is_long_num) \
925 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
926 else if (!is_short) \
927 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
929 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
932 LABEL (form_strerror): \
933 /* Print description of error ERRNO. */ \
935 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
936 sizeof work_buffer); \
937 is_long = 0; /* This is no wide-char string. */ \
938 goto LABEL (print_string)
940 #ifdef COMPILE_WPRINTF
941 # define process_string_arg(fspec) \
942 LABEL (form_character): \
945 goto LABEL (form_wcharacter); \
946 --width; /* Account for the character itself. */ \
950 outchar (btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
952 outchar (btowc ((unsigned char) args_value[fspec->data_arg].pa_char));\
957 LABEL (form_wcharacter): \
959 /* Wide character. */ \
964 outchar (va_arg (ap, wint_t)); \
966 outchar (args_value[fspec->data_arg].pa_wchar); \
972 LABEL (form_string): \
976 /* The string argument could in fact be `char *' or `wchar_t *'. \
977 But this should not make a difference here. */ \
979 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
981 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
983 /* Entry point for printing other strings. */ \
984 LABEL (print_string): \
986 if (string == NULL) \
988 /* Write "(null)" if there's space. */ \
990 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
992 string = (CHAR_T *) null; \
993 len = (sizeof (null) / sizeof (null[0])) - 1; \
997 string = (CHAR_T *) L""; \
1001 else if (!is_long && spec != L_('S')) \
1003 /* This is complicated. We have to transform the multibyte \
1004 string into a wide character string. */ \
1005 const char *mbs = (const char *) string; \
1006 mbstate_t mbstate; \
1008 len = prec == -1 ? strnlen (mbs, prec) : strlen (mbs); \
1010 /* Allocate dynamically an array which definitely is long \
1011 enough for the wide character version. */ \
1012 string = (CHAR_T *) alloca ((len + 1) * sizeof (wchar_t)); \
1014 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1015 len = __mbsrtowcs (string, &mbs, len + 1, &mbstate); \
1016 if (len == (size_t) -1) \
1018 /* Illegal multibyte character. */ \
1026 /* Search for the end of the string, but don't search past \
1027 the length specified by the precision. */ \
1028 len = __wcsnlen (string, prec); \
1030 len = __wcslen (string); \
1033 if ((width -= len) < 0) \
1035 outstring (string, len); \
1041 outstring (string, len); \
1047 # define process_string_arg(fspec) \
1048 LABEL (form_character): \
1051 goto LABEL (form_wcharacter); \
1052 --width; /* Account for the character itself. */ \
1055 if (fspec == NULL) \
1056 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1058 outchar ((unsigned char) args_value[fspec->data_arg].pa_char); \
1063 LABEL (form_wcharacter): \
1065 /* Wide character. */ \
1066 char buf[MB_CUR_MAX]; \
1067 mbstate_t mbstate; \
1070 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1071 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wint_t) \
1072 : args_value[fspec->data_arg].pa_wchar), \
1077 outstring (buf, len); \
1083 LABEL (form_string): \
1087 /* The string argument could in fact be `char *' or `wchar_t *'. \
1088 But this should not make a difference here. */ \
1089 if (fspec == NULL) \
1090 string = (char *) va_arg (ap, const char *); \
1092 string = (char *) args_value[fspec->data_arg].pa_string; \
1094 /* Entry point for printing other strings. */ \
1095 LABEL (print_string): \
1097 if (string == NULL) \
1099 /* Write "(null)" if there's space. */ \
1100 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1102 string = (char *) null; \
1103 len = sizeof (null) - 1; \
1107 string = (char *) ""; \
1111 else if (!is_long && spec != L_('S')) \
1114 /* Search for the end of the string, but don't search past \
1115 the length specified by the precision. */ \
1116 len = __strnlen (string, prec); \
1118 len = strlen (string); \
1122 const wchar_t *s2 = (const wchar_t *) string; \
1123 mbstate_t mbstate; \
1125 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1129 /* The string `s2' might not be NUL terminated. */ \
1130 string = (char *) alloca (prec); \
1131 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1135 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1136 if (len != (size_t) -1) \
1138 assert (__mbsinit (&mbstate)); \
1139 s2 = (const wchar_t *) string; \
1140 string = (char *) alloca (len + 1); \
1141 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1145 if (len == (size_t) -1) \
1147 /* Illegal wide-character string. */ \
1153 if ((width -= len) < 0) \
1155 outstring (string, len); \
1161 outstring (string, len); \
1168 /* Orient the stream. */
1173 /* Sanity check of arguments. */
1174 ARGCHECK (s
, format
);
1177 /* Check for correct orientation. */
1179 # ifdef USE_IN_LIBIO
1180 s
->_vtable_offset
== 0 &&
1182 _IO_fwide (s
, sizeof (CHAR_T
) == 1 ? -1 : 1)
1183 != (sizeof (CHAR_T
) == 1 ? -1 : 1))
1184 /* The stream is already oriented otherwise. */
1188 if (UNBUFFERED_P (s
))
1189 /* Use a helper function which will allocate a local temporary buffer
1190 for the stream and then call us again. */
1191 return buffered_vfprintf (s
, format
, ap
);
1193 /* Initialize local variables. */
1195 grouping
= (const char *) -1;
1197 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1198 since on some systems `va_list' is not an integral type. */
1199 __va_copy (ap_save
, ap
);
1205 #ifdef COMPILE_WPRINTF
1206 /* Find the first format specifier. */
1207 f
= lead_str_end
= find_spec ((const UCHAR_T
*) format
);
1209 /* Put state for processing format string in initial state. */
1210 memset (&mbstate
, '\0', sizeof (mbstate_t));
1212 /* Find the first format specifier. */
1213 f
= lead_str_end
= find_spec (format
, &mbstate
);
1218 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile
, s
);
1221 __libc_cleanup_region_start ((void (*) (void *)) &__funlockfile
, s
);
1225 /* Write the literal text before the first format. */
1226 outstring ((const UCHAR_T
*) format
,
1227 lead_str_end
- (const UCHAR_T
*) format
);
1229 /* If we only have to print a simple string, return now. */
1233 /* Process whole format string. */
1236 #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined PIC
1237 # define REF(Name) &&do_##Name - &&do_form_unknown
1239 # define REF(Name) &&do_##Name
1241 #define LABEL(Name) do_##Name
1245 union printf_arg
*args_value
; /* This is not used here but ... */
1246 int is_negative
; /* Flag for negative number. */
1249 unsigned long long int longlong
;
1250 unsigned long int word
;
1253 union printf_arg the_arg
;
1254 CHAR_T
*string
; /* Pointer to argument string. */
1255 int alt
= 0; /* Alternate format. */
1256 int space
= 0; /* Use space prefix if no sign is needed. */
1257 int left
= 0; /* Left-justify output. */
1258 int showsign
= 0; /* Always begin with plus or minus sign. */
1259 int group
= 0; /* Print numbers according grouping rules. */
1260 int is_long_double
= 0; /* Argument is long double/ long long int. */
1261 int is_short
= 0; /* Argument is short int. */
1262 int is_long
= 0; /* Argument is long int. */
1263 int is_char
= 0; /* Argument is promoted (unsigned) char. */
1264 int width
= 0; /* Width of output; 0 means none specified. */
1265 int prec
= -1; /* Precision of output; -1 means none specified. */
1266 UCHAR_T pad
= L_(' ');/* Padding character. */
1269 workend
= &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
) - 1];
1271 /* Get current character in format string. */
1272 JUMP (*++f
, step0_jumps
);
1277 JUMP (*++f
, step0_jumps
);
1282 JUMP (*++f
, step0_jumps
);
1288 JUMP (*++f
, step0_jumps
);
1293 JUMP (*++f
, step0_jumps
);
1299 JUMP (*++f
, step0_jumps
);
1301 /* The '\'' flag. */
1305 /* XXX Completely wrong. Use wctob. */
1306 if (grouping
== (const char *) -1)
1310 /* Figure out the thousands separator character. */
1311 memset (&mbstate
, '\0', sizeof (mbstate
));
1312 if (__mbrtowc (&thousands_sep
,
1313 _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
),
1314 strlen (_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
)),
1316 thousands_sep
= (wchar_t)
1317 *_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1318 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1319 if (*grouping
== '\0' || *grouping
== CHAR_MAX
1320 || thousands_sep
== L
'\0')
1323 JUMP (*++f
, step0_jumps
);
1325 /* Get width from argument. */
1326 LABEL (width_asterics
):
1328 const UCHAR_T
*tmp
; /* Temporary value. */
1331 if (ISDIGIT (*tmp
) && read_int (&tmp
) && *tmp
== L_('$'))
1332 /* The width comes from a positional parameter. */
1335 width
= va_arg (ap
, int);
1337 /* Negative width means left justified. */
1345 if (width
+ 32 >= sizeof (work_buffer
) / sizeof (work_buffer
[0]))
1346 /* We have to use a special buffer. The "32" is just a safe
1347 bet for all the output which is not counted in the width. */
1348 workend
= ((CHAR_T
*) alloca ((width
+ 32) * sizeof (CHAR_T
))
1351 JUMP (*f
, step1_jumps
);
1353 /* Given width in format string. */
1355 width
= read_int (&f
);
1357 if (width
+ 32 >= sizeof (work_buffer
) / sizeof (work_buffer
[0]))
1358 /* We have to use a special buffer. The "32" is just a safe
1359 bet for all the output which is not counted in the width. */
1360 workend
= ((CHAR_T
*) alloca ((width
+ 32) * sizeof (CHAR_T
))
1363 /* Oh, oh. The argument comes from a positional parameter. */
1365 JUMP (*f
, step1_jumps
);
1371 const UCHAR_T
*tmp
; /* Temporary value. */
1374 if (ISDIGIT (*tmp
) && read_int (&tmp
) > 0 && *tmp
== L_('$'))
1375 /* The precision comes from a positional parameter. */
1378 prec
= va_arg (ap
, int);
1380 /* If the precision is negative the precision is omitted. */
1384 else if (ISDIGIT (*f
))
1385 prec
= read_int (&f
);
1389 && prec
+ 32 > sizeof (work_buffer
) / sizeof (work_buffer
[0]))
1390 workend
= alloca (spec
+ 32) + (spec
+ 31);
1391 JUMP (*f
, step2_jumps
);
1393 /* Process 'h' modifier. There might another 'h' following. */
1396 JUMP (*++f
, step3a_jumps
);
1398 /* Process 'hh' modifier. */
1399 LABEL (mod_halfhalf
):
1402 JUMP (*++f
, step4_jumps
);
1404 /* Process 'l' modifier. There might another 'l' following. */
1407 JUMP (*++f
, step3b_jumps
);
1409 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1410 allowed to follow. */
1411 LABEL (mod_longlong
):
1413 JUMP (*++f
, step4_jumps
);
1416 is_long_double
= sizeof (size_t) > sizeof (unsigned long int);
1417 is_long
= sizeof (size_t) > sizeof (unsigned int);
1418 JUMP (*++f
, step4_jumps
);
1420 LABEL (mod_ptrdiff_t
):
1421 is_long_double
= sizeof (ptrdiff_t) > sizeof (unsigned long int);
1422 is_long
= sizeof (ptrdiff_t) > sizeof (unsigned int);
1423 JUMP (*++f
, step4_jumps
);
1425 LABEL (mod_intmax_t
):
1426 is_long_double
= sizeof (intmax_t) > sizeof (unsigned long int);
1427 is_long
= sizeof (intmax_t) > sizeof (unsigned int);
1428 JUMP (*++f
, step4_jumps
);
1430 /* Process current format. */
1433 process_arg (((struct printf_spec
*) NULL
));
1434 process_string_arg (((struct printf_spec
*) NULL
));
1436 LABEL (form_unknown
):
1437 if (spec
== L_('\0'))
1439 /* The format string ended before the specifier is complete. */
1444 /* If we are in the fast loop force entering the complicated
1449 /* The format is correctly handled. */
1452 /* Look for next format specifier. */
1453 #ifdef COMPILE_WPRINTF
1454 f
= find_spec ((end_of_spec
= ++f
));
1456 f
= find_spec ((end_of_spec
= ++f
), &mbstate
);
1459 /* Write the following constant string. */
1460 outstring (end_of_spec
, f
- end_of_spec
);
1462 while (*f
!= L_('\0'));
1464 /* Unlock stream and return. */
1467 /* Here starts the more complex loop to handle positional parameters. */
1470 /* Array with information about the needed arguments. This has to
1471 be dynamically extensible. */
1473 size_t nspecs_max
= 32; /* A more or less arbitrary start value. */
1474 struct printf_spec
*specs
1475 = alloca (nspecs_max
* sizeof (struct printf_spec
));
1477 /* The number of arguments the format string requests. This will
1478 determine the size of the array needed to store the argument
1482 union printf_arg
*args_value
= NULL
;
1484 /* Positional parameters refer to arguments directly. This could
1485 also determine the maximum number of arguments. Track the
1487 size_t max_ref_arg
= 0;
1489 /* Just a counter. */
1493 if (grouping
== (const char *) -1)
1497 /* Figure out the thousands separator character. */
1498 memset (&mbstate
, '\0', sizeof (mbstate
));
1499 if (__mbrtowc (&thousands_sep
,
1500 _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
),
1501 strlen (_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
)),
1503 thousands_sep
= (wchar_t) *_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1504 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1505 if (*grouping
== '\0' || *grouping
== CHAR_MAX
1506 || thousands_sep
== L
'\0')
1510 for (f
= lead_str_end
; *f
!= L_('\0'); f
= specs
[nspecs
++].next_fmt
)
1512 if (nspecs
>= nspecs_max
)
1514 /* Extend the array of format specifiers. */
1515 struct printf_spec
*old
= specs
;
1518 specs
= alloca (nspecs_max
* sizeof (struct printf_spec
));
1520 if (specs
== &old
[nspecs
])
1521 /* Stack grows up, OLD was the last thing allocated;
1523 nspecs_max
+= nspecs_max
/ 2;
1526 /* Copy the old array's elements to the new space. */
1527 memcpy (specs
, old
, nspecs
* sizeof (struct printf_spec
));
1528 if (old
== &specs
[nspecs
])
1529 /* Stack grows down, OLD was just below the new
1530 SPECS. We can use that space when the new space
1532 nspecs_max
+= nspecs_max
/ 2;
1536 /* Parse the format specifier. */
1537 #ifdef COMPILE_WPRINTF
1538 nargs
+= parse_one_spec (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1540 nargs
+= parse_one_spec (f
, nargs
, &specs
[nspecs
], &max_ref_arg
,
1545 /* Determine the number of arguments the format string consumes. */
1546 nargs
= MAX (nargs
, max_ref_arg
);
1548 /* Allocate memory for the argument descriptions. */
1549 args_type
= alloca (nargs
* sizeof (int));
1550 memset (args_type
, 0, nargs
* sizeof (int));
1551 args_value
= alloca (nargs
* sizeof (union printf_arg
));
1553 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1554 still zero after this loop, format is invalid. For now we
1555 simply use 0 as the value. */
1557 /* Fill in the types of all the arguments. */
1558 for (cnt
= 0; cnt
< nspecs
; ++cnt
)
1560 /* If the width is determined by an argument this is an int. */
1561 if (specs
[cnt
].width_arg
!= -1)
1562 args_type
[specs
[cnt
].width_arg
] = PA_INT
;
1564 /* If the precision is determined by an argument this is an int. */
1565 if (specs
[cnt
].prec_arg
!= -1)
1566 args_type
[specs
[cnt
].prec_arg
] = PA_INT
;
1568 switch (specs
[cnt
].ndata_args
)
1570 case 0: /* No arguments. */
1572 case 1: /* One argument; we already have the type. */
1573 args_type
[specs
[cnt
].data_arg
] = specs
[cnt
].data_arg_type
;
1576 /* We have more than one argument for this format spec.
1577 We must call the arginfo function again to determine
1579 (void) (*__printf_arginfo_table
[specs
[cnt
].info
.spec
])
1581 specs
[cnt
].ndata_args
, &args_type
[specs
[cnt
].data_arg
]);
1586 /* Now we know all the types and the order. Fill in the argument
1588 for (cnt
= 0; cnt
< nargs
; ++cnt
)
1589 switch (args_type
[cnt
])
1591 #define T(tag, mem, type) \
1593 args_value[cnt].mem = va_arg (ap_save, type); \
1596 T (PA_CHAR
, pa_char
, int); /* Promoted. */
1597 T (PA_WCHAR
, pa_wchar
, wint_t);
1598 T (PA_INT
|PA_FLAG_SHORT
, pa_short_int
, int); /* Promoted. */
1599 T (PA_INT
, pa_int
, int);
1600 T (PA_INT
|PA_FLAG_LONG
, pa_long_int
, long int);
1601 T (PA_INT
|PA_FLAG_LONG_LONG
, pa_long_long_int
, long long int);
1602 T (PA_FLOAT
, pa_float
, double); /* Promoted. */
1603 T (PA_DOUBLE
, pa_double
, double);
1604 T (PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
, pa_long_double
, long double);
1605 T (PA_STRING
, pa_string
, const char *);
1606 T (PA_WSTRING
, pa_wstring
, const wchar_t *);
1607 T (PA_POINTER
, pa_pointer
, void *);
1610 if ((args_type
[cnt
] & PA_FLAG_PTR
) != 0)
1611 args_value
[cnt
].pa_pointer
= va_arg (ap_save
, void *);
1613 args_value
[cnt
].pa_long_double
= 0.0;
1617 /* Now walk through all format specifiers and process them. */
1618 for (; (size_t) nspecs_done
< nspecs
; ++nspecs_done
)
1621 #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined PIC
1622 # define REF(Name) &&do2_##Name - &&do_form_unknown
1624 # define REF(Name) &&do2_##Name
1627 #define LABEL(Name) do2_##Name
1633 unsigned long long int longlong
;
1634 unsigned long int word
;
1637 union printf_arg the_arg
;
1638 CHAR_T
*string
; /* Pointer to argument string. */
1640 /* Fill variables from values in struct. */
1641 int alt
= specs
[nspecs_done
].info
.alt
;
1642 int space
= specs
[nspecs_done
].info
.space
;
1643 int left
= specs
[nspecs_done
].info
.left
;
1644 int showsign
= specs
[nspecs_done
].info
.showsign
;
1645 int group
= specs
[nspecs_done
].info
.group
;
1646 int is_long_double
= specs
[nspecs_done
].info
.is_long_double
;
1647 int is_short
= specs
[nspecs_done
].info
.is_short
;
1648 int is_char
= specs
[nspecs_done
].info
.is_char
;
1649 int is_long
= specs
[nspecs_done
].info
.is_long
;
1650 int width
= specs
[nspecs_done
].info
.width
;
1651 int prec
= specs
[nspecs_done
].info
.prec
;
1652 char pad
= specs
[nspecs_done
].info
.pad
;
1653 CHAR_T spec
= specs
[nspecs_done
].info
.spec
;
1655 /* Fill in last information. */
1656 if (specs
[nspecs_done
].width_arg
!= -1)
1658 /* Extract the field width from an argument. */
1659 specs
[nspecs_done
].info
.width
=
1660 args_value
[specs
[nspecs_done
].width_arg
].pa_int
;
1662 if (specs
[nspecs_done
].info
.width
< 0)
1663 /* If the width value is negative left justification is
1664 selected and the value is taken as being positive. */
1666 specs
[nspecs_done
].info
.width
*= -1;
1667 left
= specs
[nspecs_done
].info
.left
= 1;
1669 width
= specs
[nspecs_done
].info
.width
;
1672 if (specs
[nspecs_done
].prec_arg
!= -1)
1674 /* Extract the precision from an argument. */
1675 specs
[nspecs_done
].info
.prec
=
1676 args_value
[specs
[nspecs_done
].prec_arg
].pa_int
;
1678 if (specs
[nspecs_done
].info
.prec
< 0)
1679 /* If the precision is negative the precision is
1681 specs
[nspecs_done
].info
.prec
= -1;
1683 prec
= specs
[nspecs_done
].info
.prec
;
1686 /* Maybe the buffer is too small. */
1687 if (MAX (prec
, width
) + 32 > sizeof (work_buffer
) / sizeof (CHAR_T
))
1688 workend
= ((CHAR_T
*) alloca ((MAX (prec
, width
) + 32)
1690 + (MAX (prec
, width
) + 31));
1692 /* Process format specifiers. */
1695 JUMP (spec
, step4_jumps
);
1697 process_arg ((&specs
[nspecs_done
]));
1698 process_string_arg ((&specs
[nspecs_done
]));
1700 LABEL (form_unknown
):
1702 extern printf_function
**__printf_function_table
;
1704 printf_function
*function
;
1709 (__printf_function_table
== NULL
? NULL
:
1710 __printf_function_table
[specs
[nspecs_done
].info
.spec
]);
1712 if (function
== NULL
)
1713 function
= &printf_unknown
;
1715 ptr
= alloca (specs
[nspecs_done
].ndata_args
1716 * sizeof (const void *));
1718 /* Fill in an array of pointers to the argument values. */
1719 for (i
= 0; i
< specs
[nspecs_done
].ndata_args
; ++i
)
1720 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
1722 /* Call the function. */
1723 function_done
= (*function
) (s
, &specs
[nspecs_done
].info
, ptr
);
1725 /* If an error occurred we don't have information about #
1727 if (function_done
< 0)
1733 done
+= function_done
;
1738 /* Write the following constant string. */
1739 outstring (specs
[nspecs_done
].end_of_fmt
,
1740 specs
[nspecs_done
].next_fmt
1741 - specs
[nspecs_done
].end_of_fmt
);
1746 /* Unlock the stream. */
1748 _IO_funlockfile (s
);
1752 __libc_cleanup_region_end (0);
1757 /* Handle an unknown format specifier. This prints out a canonicalized
1758 representation of the format spec itself. */
1760 printf_unknown (FILE *s
, const struct printf_info
*info
,
1761 const void *const *args
)
1765 CHAR_T work_buffer
[MAX (info
->width
, info
->spec
) + 32];
1766 CHAR_T
*const workend
1767 = &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
) - 1];
1778 else if (info
->space
)
1782 if (info
->pad
== L_('0'))
1785 if (info
->width
!= 0)
1787 w
= _itoa_word (info
->width
, workend
+ 1, 10, 0);
1788 while (w
<= workend
)
1792 if (info
->prec
!= -1)
1795 w
= _itoa_word (info
->prec
, workend
+ 1, 10, 0);
1796 while (w
<= workend
)
1800 if (info
->spec
!= L_('\0'))
1801 outchar (info
->spec
);
1807 /* Group the digits according to the grouping rules of the current locale.
1808 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1811 group_number (CHAR_T
*w
, CHAR_T
*rear_ptr
, const char *grouping
,
1812 wchar_t thousands_sep
)
1817 /* We treat all negative values like CHAR_MAX. */
1819 if (*grouping
== CHAR_MAX
|| *grouping
<= 0)
1820 /* No grouping should be done. */
1825 /* Copy existing string so that nothing gets overwritten. */
1826 src
= (CHAR_T
*) alloca ((rear_ptr
- w
) * sizeof (CHAR_T
));
1827 s
= (CHAR_T
*) __mempcpy (src
, w
+ 1,
1828 (rear_ptr
- w
) * sizeof (CHAR_T
)) - 1;
1831 /* Process all characters in the string. */
1836 if (--len
== 0 && s
>= src
)
1838 /* A new group begins. */
1839 *w
-- = thousands_sep
;
1842 if (*grouping
== '\0')
1843 /* The previous grouping repeats ad infinitum. */
1845 else if (*grouping
== CHAR_MAX
1851 /* No further grouping to be done.
1852 Copy the rest of the number. */
1864 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
1867 struct _IO_FILE_plus _f
;
1868 _IO_FILE
*_put_stream
;
1869 #ifdef _IO_MTSAFE_IO
1875 _IO_helper_overflow (_IO_FILE
*s
, int c
)
1877 _IO_FILE
*target
= ((struct helper_file
*) s
)->_put_stream
;
1878 #ifdef COMPILE_WPRINTF
1879 int used
= s
->_wide_data
->_IO_write_ptr
- s
->_wide_data
->_IO_write_base
;
1882 _IO_size_t written
= _IO_sputn (target
, s
->_wide_data
->_IO_write_base
,
1884 s
->_wide_data
->_IO_write_ptr
-= written
;
1887 int used
= s
->_IO_write_ptr
- s
->_IO_write_base
;
1890 _IO_size_t written
= _IO_sputn (target
, s
->_IO_write_base
, used
);
1891 s
->_IO_write_ptr
-= written
;
1897 static const struct _IO_jump_t _IO_helper_jumps
=
1900 JUMP_INIT (finish
, _IO_default_finish
),
1901 JUMP_INIT (overflow
, _IO_helper_overflow
),
1902 JUMP_INIT (underflow
, _IO_default_underflow
),
1903 JUMP_INIT (uflow
, _IO_default_uflow
),
1904 JUMP_INIT (pbackfail
, _IO_default_pbackfail
),
1905 JUMP_INIT (xsputn
, _IO_default_xsputn
),
1906 JUMP_INIT (xsgetn
, _IO_default_xsgetn
),
1907 JUMP_INIT (seekoff
, _IO_default_seekoff
),
1908 JUMP_INIT (seekpos
, _IO_default_seekpos
),
1909 JUMP_INIT (setbuf
, _IO_default_setbuf
),
1910 JUMP_INIT (sync
, _IO_default_sync
),
1911 JUMP_INIT (doallocate
, _IO_default_doallocate
),
1912 JUMP_INIT (read
, _IO_default_read
),
1913 JUMP_INIT (write
, _IO_default_write
),
1914 JUMP_INIT (seek
, _IO_default_seek
),
1915 JUMP_INIT (close
, _IO_default_close
),
1916 JUMP_INIT (stat
, _IO_default_stat
)
1921 buffered_vfprintf (register _IO_FILE
*s
, const CHAR_T
*format
,
1924 CHAR_T buf
[_IO_BUFSIZ
];
1925 struct helper_file helper
;
1926 register _IO_FILE
*hp
= &helper
._f
.file
;
1927 int result
, to_flush
;
1929 /* Initialize helper. */
1930 helper
._put_stream
= s
;
1931 #ifdef COMPILE_WPRINTF
1932 _IO_wsetp (hp
, buf
, buf
+ sizeof buf
/ sizeof (CHAR_T
));
1935 _IO_setp (hp
, buf
, buf
+ sizeof buf
);
1938 hp
->_IO_file_flags
= _IO_MAGIC
|_IO_NO_READS
;
1939 #if _IO_JUMPS_OFFSET
1940 hp
->_vtable_offset
= 0;
1942 #ifdef _IO_MTSAFE_IO
1943 hp
->_lock
= &helper
.lock
;
1944 __libc_lock_init (*hp
->_lock
);
1946 _IO_JUMPS (hp
) = (struct _IO_jump_t
*) &_IO_helper_jumps
;
1948 /* Now print to helper instead. */
1949 result
= vfprintf (hp
, format
, args
);
1952 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile
, s
);
1955 /* Now flush anything from the helper to the S. */
1956 #ifdef COMPILE_WPRINTF
1957 if ((to_flush
= (hp
->_wide_data
->_IO_write_ptr
1958 - hp
->_wide_data
->_IO_write_base
)) > 0)
1960 if ((int) _IO_sputn (s
, hp
->_wide_data
->_IO_write_base
, to_flush
)
1965 if ((to_flush
= hp
->_IO_write_ptr
- hp
->_IO_write_base
) > 0)
1967 if ((int) _IO_sputn (s
, hp
->_IO_write_base
, to_flush
) != to_flush
)
1972 /* Unlock the stream. */
1973 _IO_funlockfile (s
);
1974 __libc_cleanup_region_end (0);
1979 #else /* !USE_IN_LIBIO */
1983 buffered_vfprintf (register FILE *s
, const CHAR_T
*format
, va_list args
)
1988 s
->__bufp
= s
->__buffer
= buf
;
1989 s
->__bufsize
= sizeof buf
;
1990 s
->__put_limit
= s
->__buffer
+ s
->__bufsize
;
1991 s
->__get_limit
= s
->__buffer
;
1993 /* Now use buffer to print. */
1994 result
= vfprintf (s
, format
, args
);
1996 if (fflush (s
) == EOF
)
1998 s
->__buffer
= s
->__bufp
= s
->__get_limit
= s
->__put_limit
= NULL
;
2004 /* Pads string with given number of a specified character.
2005 This code is taken from iopadn.c of the GNU I/O library. */
2007 static const CHAR_T blanks
[PADSIZE
] =
2008 { L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '),
2009 L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' ') };
2010 static const CHAR_T zeroes
[PADSIZE
] =
2011 { L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'),
2012 L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0') };
2015 #ifndef COMPILE_WPRINTF
2016 __printf_pad (FILE *s
, char pad
, size_t count
)
2018 __wprintf_pad (FILE *s
, wchar_t pad
, size_t count
)
2021 const CHAR_T
*padptr
;
2024 padptr
= pad
== L_(' ') ? blanks
: zeroes
;
2026 for (i
= count
; i
>= PADSIZE
; i
-= PADSIZE
)
2027 if (PUT (s
, padptr
, PADSIZE
) != PADSIZE
)
2030 if (PUT (s
, padptr
, i
) != i
)
2036 #endif /* USE_IN_LIBIO */
2040 # ifdef strong_alias
2041 /* This is for glibc. */
2042 # ifdef COMPILE_WPRINTF
2043 strong_alias (_IO_vfwprintf
, vfwprintf
);
2045 strong_alias (_IO_vfprintf
, vfprintf
);
2048 # if defined __ELF__ || defined __GNU_LIBRARY__
2049 # include <gnu-stabs.h>
2051 # ifdef COMPILE_WPRINTF
2052 weak_alias (_IO_vfwprintf
, vfwprintf
);
2054 weak_alias (_IO_vfprintf
, vfprintf
);