1 /* Copyright (C) 1991, 92, 93, 94, 95, 96 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. */
26 #include <libc-lock.h>
28 #include "../locale/localeinfo.h"
30 /* This code is shared between the standard stdio implementation found
31 in GNU C library and the libio implementation originally found in
34 Beside this it is also shared between the normal and wide character
35 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
37 #ifndef COMPILE_WPRINTF
39 # define UCHAR_T unsigned char
42 # define ISDIGIT(Ch) isdigit (Ch)
45 # define PUT(F, S, N) _IO_sputn (F, S, N)
46 # define PAD(Padchar) \
48 done += _IO_padn (s, Padchar, width)
50 # define PUTC(C, F) putc (C, F)
51 ssize_t __printf_pad
__P ((FILE *, char pad
, size_t n
));
52 # define PAD(Padchar) \
54 { if (__printf_pad (s, Padchar, width) == -1) \
55 return -1; else done += width; }
58 # define vfprintf vfwprintf
59 # define CHAR_T wchar_t
60 # define UCHAR_T uwchar_t
62 # define L_(Str) L##Str
63 # define ISDIGIT(Ch) iswdigit (Ch)
66 # define PUT(F, S, N) _IO_sputn (F, S, N)
67 # define PAD(Padchar) \
69 done += _IO_wpadn (s, Padchar, width)
71 # define PUTC(C, F) wputc (C, F)
72 ssize_t __wprintf_pad
__P ((FILE *, wchar_t pad
, size_t n
));
73 # define PAD(Padchar) \
75 { if (__wprintf_pad (s, Padchar, width) == -1) \
76 return -1; else done += width; }
80 /* Include the shared code for parsing the format string. */
81 #include "printf-parse.h"
85 /* This code is for use in libio. */
87 # define PUTC(C, F) _IO_putc_unlocked (C, F)
88 # define vfprintf _IO_vfprintf
89 # define FILE _IO_FILE
91 # define va_list _IO_va_list
93 # define BUFSIZ _IO_BUFSIZ
94 # define ARGCHECK(S, Format) \
97 /* Check file argument for consistence. */ \
99 if (S->_flags & _IO_NO_WRITES || Format == NULL) \
105 # define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
106 #else /* ! USE_IN_LIBIO */
107 /* This code is for use in the GNU C library. */
109 # define PUT(F, S, N) fwrite (S, 1, N, F)
110 # define ARGCHECK(S, Format) \
113 /* Check file argument for consistence. */ \
114 if (!__validfp(S) || !S->__mode.__write || Format == NULL) \
116 __set_errno (EINVAL); \
121 if (__flshfp (S, EOF) == EOF) \
126 # define UNBUFFERED_P(s) ((s)->__buffer == NULL)
128 /* XXX These declarations should go as soon as the stdio header files
129 have these prototypes. */
130 extern void __flockfile (FILE *);
131 extern void __funlockfile (FILE *);
132 #endif /* USE_IN_LIBIO */
135 #define outchar(Ch) \
138 register const int outc = (Ch); \
139 if (PUTC (outc, s) == EOF) \
146 #define outstring(String, Len) \
149 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
155 /* For handling long_double and longlong we use the same flag. */
157 # define is_longlong is_long_double
161 /* Global variables. */
162 static const char null
[] = "(null)";
165 /* Helper function to provide temporary buffering for unbuffered streams. */
166 static int buffered_vfprintf
__P ((FILE *stream
, const CHAR_T
*fmt
, va_list));
168 /* Handle unknown format specifier. */
169 static int printf_unknown
__P ((FILE *, const struct printf_info
*,
170 const void *const *));
172 /* Group digits of number string. */
173 static char *group_number
__P ((CHAR_T
*, CHAR_T
*, const CHAR_T
*, wchar_t));
176 /* The function itself. */
178 vfprintf (FILE *s
, const CHAR_T
*format
, va_list ap
)
180 /* The character used as thousands separator. */
181 wchar_t thousands_sep
;
183 /* The string describing the size of groups of digits. */
184 const char *grouping
;
186 /* Place to accumulate the result. */
189 /* Current character in format string. */
192 /* End of leading constant string. */
193 const UCHAR_T
*lead_str_end
;
195 /* Points to next format specifier. */
196 const UCHAR_T
*end_of_spec
;
198 /* Buffer intermediate results. */
199 char work_buffer
[1000];
200 #define workend (&work_buffer[sizeof (work_buffer) - 1])
202 /* State for restartable multibyte character handling functions. */
205 /* We have to save the original argument pointer. */
208 /* Count number of specifiers we already processed. */
212 /* This table maps a character into a number representing a
213 class. In each step there is a destination label for each
215 static const int jump_table
[] =
217 /* ' ' */ 1, 0, 0, /* '#' */ 4,
218 0, /* '%' */ 14, 0, /* '\''*/ 6,
219 0, 0, /* '*' */ 7, /* '+' */ 2,
220 0, /* '-' */ 3, /* '.' */ 9, 0,
221 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
222 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
223 /* '8' */ 8, /* '9' */ 8, 0, 0,
225 0, 0, 0, /* 'C' */ 25,
226 0, /* 'E' */ 19, 0, /* 'G' */ 19,
228 /* 'L' */ 12, 0, 0, 0,
229 0, 0, 0, /* 'S' */ 21,
231 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
233 0, 0, 0, /* 'c' */ 20,
234 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
235 /* 'h' */ 10, /* 'i' */ 15, 0, 0,
236 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
237 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
238 0, /* 'u' */ 16, 0, 0,
242 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < ' ' || (Ch) > 'x')
243 #define CHAR_CLASS(Ch) (jump_table[(int) (Ch) - ' '])
244 #define JUMP(ChExpr, table) \
249 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
250 : table[CHAR_CLASS (spec)]; \
255 #define STEP0_3_TABLE \
256 /* Step 0: at the beginning. */ \
257 static const void *step0_jumps[26] = \
259 REF (form_unknown), \
260 REF (flag_space), /* for ' ' */ \
261 REF (flag_plus), /* for '+' */ \
262 REF (flag_minus), /* for '-' */ \
263 REF (flag_hash), /* for '<hash>' */ \
264 REF (flag_zero), /* for '0' */ \
265 REF (flag_quote), /* for '\'' */ \
266 REF (width_asterics), /* for '*' */ \
267 REF (width), /* for '1'...'9' */ \
268 REF (precision), /* for '.' */ \
269 REF (mod_half), /* for 'h' */ \
270 REF (mod_long), /* for 'l' */ \
271 REF (mod_longlong), /* for 'L', 'q' */ \
272 REF (mod_size_t), /* for 'Z' */ \
273 REF (form_percent), /* for '%' */ \
274 REF (form_integer), /* for 'd', 'i' */ \
275 REF (form_unsigned), /* for 'u' */ \
276 REF (form_octal), /* for 'o' */ \
277 REF (form_hexa), /* for 'X', 'x' */ \
278 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
279 REF (form_character), /* for 'c' */ \
280 REF (form_string), /* for 's', 'S' */ \
281 REF (form_pointer), /* for 'p' */ \
282 REF (form_number), /* for 'n' */ \
283 REF (form_strerror), /* for 'm' */ \
284 REF (form_wcharacter) /* for 'C' */ \
286 /* Step 1: after processing width. */ \
287 static const void *step1_jumps[26] = \
289 REF (form_unknown), \
290 REF (form_unknown), /* for ' ' */ \
291 REF (form_unknown), /* for '+' */ \
292 REF (form_unknown), /* for '-' */ \
293 REF (form_unknown), /* for '<hash>' */ \
294 REF (form_unknown), /* for '0' */ \
295 REF (form_unknown), /* for '\'' */ \
296 REF (form_unknown), /* for '*' */ \
297 REF (form_unknown), /* for '1'...'9' */ \
298 REF (precision), /* for '.' */ \
299 REF (mod_half), /* for 'h' */ \
300 REF (mod_long), /* for 'l' */ \
301 REF (mod_longlong), /* for 'L', 'q' */ \
302 REF (mod_size_t), /* for 'Z' */ \
303 REF (form_percent), /* for '%' */ \
304 REF (form_integer), /* for 'd', 'i' */ \
305 REF (form_unsigned), /* for 'u' */ \
306 REF (form_octal), /* for 'o' */ \
307 REF (form_hexa), /* for 'X', 'x' */ \
308 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
309 REF (form_character), /* for 'c' */ \
310 REF (form_string), /* for 's', 'S' */ \
311 REF (form_pointer), /* for 'p' */ \
312 REF (form_number), /* for 'n' */ \
313 REF (form_strerror), /* for 'm' */ \
314 REF (form_wcharacter) /* for 'C' */ \
316 /* Step 2: after processing precision. */ \
317 static const void *step2_jumps[26] = \
319 REF (form_unknown), \
320 REF (form_unknown), /* for ' ' */ \
321 REF (form_unknown), /* for '+' */ \
322 REF (form_unknown), /* for '-' */ \
323 REF (form_unknown), /* for '<hash>' */ \
324 REF (form_unknown), /* for '0' */ \
325 REF (form_unknown), /* for '\'' */ \
326 REF (form_unknown), /* for '*' */ \
327 REF (form_unknown), /* for '1'...'9' */ \
328 REF (form_unknown), /* for '.' */ \
329 REF (mod_half), /* for 'h' */ \
330 REF (mod_long), /* for 'l' */ \
331 REF (mod_longlong), /* for 'L', 'q' */ \
332 REF (mod_size_t), /* for 'Z' */ \
333 REF (form_percent), /* for '%' */ \
334 REF (form_integer), /* for 'd', 'i' */ \
335 REF (form_unsigned), /* for 'u' */ \
336 REF (form_octal), /* for 'o' */ \
337 REF (form_hexa), /* for 'X', 'x' */ \
338 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
339 REF (form_character), /* for 'c' */ \
340 REF (form_string), /* for 's', 'S' */ \
341 REF (form_pointer), /* for 'p' */ \
342 REF (form_number), /* for 'n' */ \
343 REF (form_strerror), /* for 'm' */ \
344 REF (form_wcharacter) /* for 'C' */ \
346 /* Step 3: after processing first 'l' modifier. */ \
347 static const void *step3_jumps[26] = \
349 REF (form_unknown), \
350 REF (form_unknown), /* for ' ' */ \
351 REF (form_unknown), /* for '+' */ \
352 REF (form_unknown), /* for '-' */ \
353 REF (form_unknown), /* for '<hash>' */ \
354 REF (form_unknown), /* for '0' */ \
355 REF (form_unknown), /* for '\'' */ \
356 REF (form_unknown), /* for '*' */ \
357 REF (form_unknown), /* for '1'...'9' */ \
358 REF (form_unknown), /* for '.' */ \
359 REF (form_unknown), /* for 'h' */ \
360 REF (mod_longlong), /* for 'l' */ \
361 REF (form_unknown), /* for 'L', 'q' */ \
362 REF (form_unknown), /* for 'Z' */ \
363 REF (form_percent), /* for '%' */ \
364 REF (form_integer), /* for 'd', 'i' */ \
365 REF (form_unsigned), /* for 'u' */ \
366 REF (form_octal), /* for 'o' */ \
367 REF (form_hexa), /* for 'X', 'x' */ \
368 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
369 REF (form_character), /* for 'c' */ \
370 REF (form_string), /* for 's', 'S' */ \
371 REF (form_pointer), /* for 'p' */ \
372 REF (form_number), /* for 'n' */ \
373 REF (form_strerror), /* for 'm' */ \
374 REF (form_wcharacter) /* for 'C' */ \
377 #define STEP4_TABLE \
378 /* Step 4: processing format specifier. */ \
379 static const void *step4_jumps[26] = \
381 REF (form_unknown), \
382 REF (form_unknown), /* for ' ' */ \
383 REF (form_unknown), /* for '+' */ \
384 REF (form_unknown), /* for '-' */ \
385 REF (form_unknown), /* for '<hash>' */ \
386 REF (form_unknown), /* for '0' */ \
387 REF (form_unknown), /* for '\'' */ \
388 REF (form_unknown), /* for '*' */ \
389 REF (form_unknown), /* for '1'...'9' */ \
390 REF (form_unknown), /* for '.' */ \
391 REF (form_unknown), /* for 'h' */ \
392 REF (form_unknown), /* for 'l' */ \
393 REF (form_unknown), /* for 'L', 'q' */ \
394 REF (form_unknown), /* for 'Z' */ \
395 REF (form_percent), /* for '%' */ \
396 REF (form_integer), /* for 'd', 'i' */ \
397 REF (form_unsigned), /* for 'u' */ \
398 REF (form_octal), /* for 'o' */ \
399 REF (form_hexa), /* for 'X', 'x' */ \
400 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
401 REF (form_character), /* for 'c' */ \
402 REF (form_string), /* for 's', 'S' */ \
403 REF (form_pointer), /* for 'p' */ \
404 REF (form_number), /* for 'n' */ \
405 REF (form_strerror), /* for 'm' */ \
406 REF (form_wcharacter) /* for 'C' */ \
410 #define process_arg(fspec) \
411 /* Start real work. We know about all flags and modifiers and \
412 now process the wanted format specifier. */ \
413 LABEL (form_percent): \
414 /* Write a literal "%". */ \
418 LABEL (form_integer): \
419 /* Signed decimal integer. */ \
424 long long int signed_number; \
427 signed_number = va_arg (ap, long long int); \
429 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
431 is_negative = signed_number < 0; \
432 number.longlong = is_negative ? (- signed_number) : signed_number; \
434 goto LABEL (longlong_number); \
438 long int signed_number; \
442 signed_number = va_arg (ap, long int); \
443 else /* `short int' will be promoted to `int'. */ \
444 signed_number = va_arg (ap, int); \
447 signed_number = args_value[fspec->data_arg].pa_long_int; \
449 signed_number = args_value[fspec->data_arg].pa_int; \
451 is_negative = signed_number < 0; \
452 number.word = is_negative ? (- signed_number) : signed_number; \
454 goto LABEL (number); \
458 LABEL (form_unsigned): \
459 /* Unsigned decimal integer. */ \
461 goto LABEL (unsigned_number); \
464 LABEL (form_octal): \
465 /* Unsigned octal integer. */ \
467 goto LABEL (unsigned_number); \
471 /* Unsigned hexadecimal integer. */ \
474 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
476 /* ANSI specifies the `+' and ` ' flags only for signed \
485 number.longlong = va_arg (ap, unsigned long long int); \
487 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
489 LABEL (longlong_number): \
491 /* Supply a default precision if none was given. */ \
494 /* We have to take care for the '0' flag. If a precision \
495 is given it must be ignored. */ \
498 /* If the precision is 0 and the number is 0 nothing has to \
499 be written for the number. */ \
500 if (prec == 0 && number.longlong == 0) \
504 /* Put the number in WORK. */ \
505 string = _itoa (number.longlong, workend + 1, base, \
508 if (group && grouping) \
509 string = group_number (string, workend, grouping, \
512 /* Simply further test for num != 0. */ \
513 number.word = number.longlong != 0; \
519 number.word = va_arg (ap, unsigned long int); \
520 else if (!is_short) \
521 number.word = va_arg (ap, unsigned int); \
523 number.word = (unsigned short int) va_arg (ap, unsigned int); \
526 number.word = args_value[fspec->data_arg].pa_u_long_int; \
527 else if (!is_short) \
528 number.word = args_value[fspec->data_arg].pa_u_int; \
530 number.word = (unsigned short int) \
531 args_value[fspec->data_arg].pa_u_short_int; \
535 /* Supply a default precision if none was given. */ \
538 /* We have to take care for the '0' flag. If a precision \
539 is given it must be ignored. */ \
542 /* If the precision is 0 and the number is 0 nothing has to \
543 be written for the number. */ \
544 if (prec == 0 && number.word == 0) \
548 /* Put the number in WORK. */ \
549 string = _itoa_word (number.word, workend + 1, base, \
552 if (group && grouping) \
553 string = group_number (string, workend, grouping, \
558 prec -= workend - string; \
561 /* Add zeros to the precision. */ \
564 else if (number.word != 0 && alt && base == 8) \
565 /* Add octal marker. */ \
570 width -= workend - string; \
572 if (number.word != 0 && alt && base == 16) \
573 /* Account for 0X hex marker. */ \
576 if (is_negative || showsign || space) \
581 while (width-- > 0) \
584 if (number.word != 0 && alt && base == 16) \
599 if (number.word != 0 && alt && base == 16) \
612 while (width-- > 0) \
616 outstring (string + 1, workend - string); \
622 if (number.word != 0 && alt && base == 16) \
635 width -= workend - string; \
636 outstring (string + 1, workend - string); \
642 LABEL (form_float): \
644 /* Floating-point number. This is handled by printf_fp.c. */ \
645 extern int __printf_fp __P ((FILE *, const struct printf_info *, \
646 const void **const)); \
652 struct printf_info info = { prec: prec, \
655 is_long_double: is_long_double, \
656 is_short: is_short, \
661 showsign: showsign, \
666 if (is_long_double) \
667 the_arg.pa_long_double = va_arg (ap, long double); \
669 the_arg.pa_double = va_arg (ap, double); \
670 ptr = (const void *) &the_arg; \
672 function_done = __printf_fp (s, &info, &ptr); \
676 ptr = (const void *) &args_value[fspec->data_arg]; \
678 function_done = __printf_fp (s, &fspec->info, &ptr); \
681 if (function_done < 0) \
682 /* Error in print handler. */ \
685 done += function_done; \
689 LABEL (form_character): \
692 goto LABEL (form_wcharacter); \
693 --width; /* Account for the character itself. */ \
697 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
699 outchar ((unsigned char) args_value[fspec->data_arg].pa_char); \
704 LABEL (form_wcharacter): \
706 /* Wide character. */ \
707 char buf[MB_CUR_MAX]; \
711 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wint_t) \
712 : args_value[fspec->data_arg].pa_wchar), \
717 outstring (buf, len); \
723 LABEL (form_string): \
727 /* The string argument could in fact be `char *' or `wchar_t *'. \
728 But this should not make a difference here. */ \
730 string = (char *) va_arg (ap, const char *); \
732 string = (char *) args_value[fspec->data_arg].pa_string; \
734 /* Entry point for printing other strings. */ \
735 LABEL (print_string): \
737 if (string == NULL) \
739 /* Write "(null)" if there's space. */ \
740 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
742 string = (char *) null; \
743 len = sizeof (null) - 1; \
747 string = (char *) ""; \
751 else if (!is_long && spec != L_('S')) \
754 /* Search for the end of the string, but don't search past \
755 the length specified by the precision. */ \
756 len = strnlen (string, prec); \
758 len = strlen (string); \
762 const wchar_t *s2 = (const wchar_t *) string; \
765 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
766 if (len == (size_t) -1) \
767 /* Illegal wide-character string. */ \
770 s2 = (const wchar_t *) string; \
771 string = alloca (len + 1); \
772 (void) __wcsrtombs (string, &s2, prec != -1 ? prec : UINT_MAX, \
776 if ((width -= len) < 0) \
778 outstring (string, len); \
784 outstring (string, len); \
790 LABEL (form_pointer): \
791 /* Generic pointer. */ \
795 ptr = va_arg (ap, void *); \
797 ptr = args_value[fspec->data_arg].pa_pointer; \
800 /* If the pointer is not NULL, write it as a %#x spec. */ \
802 number.word = (unsigned long int) ptr; \
807 goto LABEL (number); \
811 /* Write "(nil)" for a nil pointer. */ \
812 string = (char *) "(nil)"; \
813 /* Make sure the full string "(nil)" is printed. */ \
816 is_long = 0; /* This is no wide-char string. */ \
817 goto LABEL (print_string); \
822 LABEL (form_number): \
823 /* Answer the count of characters written. */ \
826 *(long long int *) va_arg (ap, void *) = done; \
828 *(long int *) va_arg (ap, void *) = done; \
829 else if (!is_short) \
830 *(int *) va_arg (ap, void *) = done; \
832 *(short int *) va_arg (ap, void *) = done; \
835 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
837 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
838 else if (!is_short) \
839 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
841 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
844 LABEL (form_strerror): \
845 /* Print description of error ERRNO. */ \
847 extern char *_strerror_internal __P ((int, char *buf, size_t)); \
850 _strerror_internal (errno, work_buffer, sizeof work_buffer); \
852 is_long = 0; /* This is no wide-char string. */ \
853 goto LABEL (print_string)
856 /* Sanity check of arguments. */
857 ARGCHECK (s
, format
);
859 if (UNBUFFERED_P (s
))
860 /* Use a helper function which will allocate a local temporary buffer
861 for the stream and then call us again. */
862 return buffered_vfprintf (s
, format
, ap
);
864 /* Initialize local variables. */
866 grouping
= (const char *) -1;
870 /* Find the first format specifier. */
871 f
= lead_str_end
= find_spec (format
, &mbstate
);
875 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile
, s
);
878 __libc_cleanup_region_start ((void (*) (void *)) &__funlockfile
, s
);
882 /* Write the literal text before the first format. */
883 outstring ((const UCHAR_T
*) format
,
884 lead_str_end
- (const UCHAR_T
*) format
);
886 /* If we only have to print a simple string, return now. */
890 /* Process whole format string. */
893 #define REF(Name) &&do_##Name
894 #define LABEL(Name) do_##Name
898 union printf_arg
*args_value
; /* This is not used here but ... */
899 int is_negative
; /* Flag for negative number. */
902 unsigned long long int longlong
;
903 unsigned long int word
;
906 union printf_arg the_arg
;
907 char *string
; /* Pointer to argument string. */
908 int alt
= 0; /* Alternate format. */
909 int space
= 0; /* Use space prefix if no sign is needed. */
910 int left
= 0; /* Left-justify output. */
911 int showsign
= 0; /* Always begin with plus or minus sign. */
912 int group
= 0; /* Print numbers according grouping rules. */
913 int is_long_double
= 0; /* Argument is long double/ long long int. */
914 int is_short
= 0; /* Argument is long int. */
915 int is_long
= 0; /* Argument is short int. */
916 int width
= 0; /* Width of output; 0 means none specified. */
917 int prec
= -1; /* Precision of output; -1 means none specified. */
918 char pad
= ' '; /* Padding character. */
921 /* Get current character in format string. */
922 JUMP (*++f
, step0_jumps
);
927 JUMP (*++f
, step0_jumps
);
932 JUMP (*++f
, step0_jumps
);
938 JUMP (*++f
, step0_jumps
);
943 JUMP (*++f
, step0_jumps
);
949 JUMP (*++f
, step0_jumps
);
955 /* XXX Completely wrong. Use wctob. */
956 if (grouping
== (const char *) -1)
958 /* Figure out the thousands separator character. */
959 if (mbtowc (&thousands_sep
,
960 _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
),
961 strlen (_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
))) <= 0)
962 thousands_sep
= (wchar_t)
963 *_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
964 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
965 if (*grouping
== '\0' || *grouping
== CHAR_MAX
966 || thousands_sep
== L
'\0')
969 JUMP (*++f
, step0_jumps
);
971 /* Get width from argument. */
972 LABEL (width_asterics
):
974 const UCHAR_T
*tmp
; /* Temporary value. */
977 if (ISDIGIT (*tmp
) && read_int (&tmp
) && *tmp
== L_('$'))
978 /* The width comes from a positional parameter. */
981 width
= va_arg (ap
, int);
983 /* Negative width means left justified. */
991 JUMP (*f
, step1_jumps
);
993 /* Given width in format string. */
995 width
= read_int (&f
);
997 /* Oh, oh. The argument comes from a positional parameter. */
999 JUMP (*f
, step1_jumps
);
1005 const UCHAR_T
*tmp
; /* Temporary value. */
1008 if (ISDIGIT (*tmp
) && read_int (&tmp
) > 0 && *tmp
== L_('$'))
1009 /* The precision comes from a positional parameter. */
1012 prec
= va_arg (ap
, int);
1014 /* If the precision is negative the precision is omitted. */
1018 else if (ISDIGIT (*f
))
1019 prec
= read_int (&f
);
1022 JUMP (*f
, step2_jumps
);
1024 /* Process 'h' modifier. No other modifier is allowed to
1028 JUMP (*++f
, step4_jumps
);
1030 /* Process 'l' modifier. There might another 'l' follow. */
1033 JUMP (*++f
, step3_jumps
);
1035 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1036 allowed to follow. */
1037 LABEL (mod_longlong
):
1039 JUMP (*++f
, step4_jumps
);
1042 is_longlong
= sizeof (size_t) > sizeof (unsigned long int);
1043 is_long
= sizeof (size_t) > sizeof (unsigned int);
1044 JUMP (*++f
, step4_jumps
);
1047 /* Process current format. */
1050 process_arg (((struct printf_spec
*) NULL
));
1052 LABEL (form_unknown
):
1053 if (spec
== L_('\0'))
1055 /* The format string ended before the specifier is complete. */
1060 /* If we are in the fast loop force entering the complicated
1065 /* Look for next format specifier. */
1066 f
= find_spec ((end_of_spec
= ++f
), &mbstate
);
1068 /* Write the following constant string. */
1069 outstring (end_of_spec
, f
- end_of_spec
);
1071 while (*f
!= L_('\0'));
1073 /* Unlock stream and return. */
1076 /* Here starts the more complex loop to handle positional parameters. */
1079 /* Array with information about the needed arguments. This has to
1080 be dynamically extendable. */
1082 size_t nspecs_max
= 32; /* A more or less arbitrary start value. */
1083 struct printf_spec
*specs
1084 = alloca (nspecs_max
* sizeof (struct printf_spec
));
1086 /* The number of arguments the format string requests. This will
1087 determine the size of the array needed to store the argument
1091 union printf_arg
*args_value
;
1093 /* Positional parameters refer to arguments directly. This could
1094 also determine the maximum number of arguments. Track the
1096 size_t max_ref_arg
= 0;
1098 /* Just a counter. */
1102 if (grouping
== (const char *) -1)
1104 /* XXX Use wctob. But this is incompatible for now. */
1105 /* Figure out the thousands separator character. */
1106 if (mbtowc (&thousands_sep
,
1107 _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
),
1108 strlen (_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
))) <= 0)
1109 thousands_sep
= (wchar_t) *_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1110 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1111 if (*grouping
== '\0' || *grouping
== CHAR_MAX
1112 || thousands_sep
== L
'\0')
1116 for (f
= lead_str_end
; *f
!= '\0'; f
= specs
[nspecs
++].next_fmt
)
1118 if (nspecs
>= nspecs_max
)
1120 /* Extend the array of format specifiers. */
1121 struct printf_spec
*old
= specs
;
1124 specs
= alloca (nspecs_max
* sizeof (struct printf_spec
));
1126 if (specs
== &old
[nspecs
])
1127 /* Stack grows up, OLD was the last thing allocated;
1129 nspecs_max
+= nspecs_max
/ 2;
1132 /* Copy the old array's elements to the new space. */
1133 memcpy (specs
, old
, nspecs
* sizeof (struct printf_spec
));
1134 if (old
== &specs
[nspecs
])
1135 /* Stack grows down, OLD was just below the new
1136 SPECS. We can use that space when the new space
1138 nspecs_max
+= nspecs_max
/ 2;
1142 /* Parse the format specifier. */
1143 nargs
+= parse_one_spec (f
, nargs
, &specs
[nspecs
], &max_ref_arg
,
1147 /* Determine the number of arguments the format string consumes. */
1148 nargs
= MAX (nargs
, max_ref_arg
);
1150 /* Allocate memory for the argument descriptions. */
1151 args_type
= alloca (nargs
* sizeof (int));
1152 memset (args_type
, 0, nargs
* sizeof (int));
1153 args_value
= alloca (nargs
* sizeof (union printf_arg
));
1155 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1156 still zero after this loop, format is invalid. For now we
1157 simply use 0 as the value. */
1159 /* Fill in the types of all the arguments. */
1160 for (cnt
= 0; cnt
< nspecs
; ++cnt
)
1162 /* If the width is determined by an argument this is an int. */
1163 if (specs
[cnt
].width_arg
!= -1)
1164 args_type
[specs
[cnt
].width_arg
] = PA_INT
;
1166 /* If the precision is determined by an argument this is an int. */
1167 if (specs
[cnt
].prec_arg
!= -1)
1168 args_type
[specs
[cnt
].prec_arg
] = PA_INT
;
1170 switch (specs
[cnt
].ndata_args
)
1172 case 0: /* No arguments. */
1174 case 1: /* One argument; we already have the type. */
1175 args_type
[specs
[cnt
].data_arg
] = specs
[cnt
].data_arg_type
;
1178 /* We have more than one argument for this format spec.
1179 We must call the arginfo function again to determine
1181 (void) (*__printf_arginfo_table
[specs
[cnt
].info
.spec
])
1183 specs
[cnt
].ndata_args
, &args_type
[specs
[cnt
].data_arg
]);
1188 /* Now we know all the types and the order. Fill in the argument
1190 for (cnt
= 0, ap
= ap_save
; cnt
< nargs
; ++cnt
)
1191 switch (args_type
[cnt
])
1193 #define T(tag, mem, type) \
1195 args_value[cnt].mem = va_arg (ap, type); \
1198 T (PA_CHAR
, pa_char
, int); /* Promoted. */
1199 T (PA_WCHAR
, pa_wchar
, wint_t);
1200 T (PA_INT
|PA_FLAG_SHORT
, pa_short_int
, int); /* Promoted. */
1201 T (PA_INT
, pa_int
, int);
1202 T (PA_INT
|PA_FLAG_LONG
, pa_long_int
, long int);
1203 T (PA_INT
|PA_FLAG_LONG_LONG
, pa_long_long_int
, long long int);
1204 T (PA_FLOAT
, pa_float
, double); /* Promoted. */
1205 T (PA_DOUBLE
, pa_double
, double);
1206 T (PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
, pa_long_double
, long double);
1207 T (PA_STRING
, pa_string
, const char *);
1208 T (PA_WSTRING
, pa_wstring
, const wchar_t *);
1209 T (PA_POINTER
, pa_pointer
, void *);
1212 if ((args_type
[cnt
] & PA_FLAG_PTR
) != 0)
1213 args_value
[cnt
].pa_pointer
= va_arg (ap
, void *);
1215 args_value
[cnt
].pa_long_double
= 0.0;
1219 /* Now walk through all format specifiers and process them. */
1220 for (; (size_t) nspecs_done
< nspecs
; ++nspecs_done
)
1223 #define REF(Name) &&do2_##Name
1225 #define LABEL(Name) do2_##Name
1231 unsigned long long int longlong
;
1232 unsigned long int word
;
1235 union printf_arg the_arg
;
1236 char *string
; /* Pointer to argument string. */
1238 /* Fill variables from values in struct. */
1239 int alt
= specs
[nspecs_done
].info
.alt
;
1240 int space
= specs
[nspecs_done
].info
.space
;
1241 int left
= specs
[nspecs_done
].info
.left
;
1242 int showsign
= specs
[nspecs_done
].info
.showsign
;
1243 int group
= specs
[nspecs_done
].info
.group
;
1244 int is_long_double
= specs
[nspecs_done
].info
.is_long_double
;
1245 int is_short
= specs
[nspecs_done
].info
.is_short
;
1246 int is_long
= specs
[nspecs_done
].info
.is_long
;
1247 int width
= specs
[nspecs_done
].info
.width
;
1248 int prec
= specs
[nspecs_done
].info
.prec
;
1249 char pad
= specs
[nspecs_done
].info
.pad
;
1250 CHAR_T spec
= specs
[nspecs_done
].info
.spec
;
1252 /* Fill in last information. */
1253 if (specs
[nspecs_done
].width_arg
!= -1)
1255 /* Extract the field width from an argument. */
1256 specs
[nspecs_done
].info
.width
=
1257 args_value
[specs
[nspecs_done
].width_arg
].pa_int
;
1259 if (specs
[nspecs_done
].info
.width
< 0)
1260 /* If the width value is negative left justification is
1261 selected and the value is taken as being positive. */
1263 specs
[nspecs_done
].info
.width
*= -1;
1264 left
= specs
[nspecs_done
].info
.left
= 1;
1266 width
= specs
[nspecs_done
].info
.width
;
1269 if (specs
[nspecs_done
].prec_arg
!= -1)
1271 /* Extract the precision from an argument. */
1272 specs
[nspecs_done
].info
.prec
=
1273 args_value
[specs
[nspecs_done
].prec_arg
].pa_int
;
1275 if (specs
[nspecs_done
].info
.prec
< 0)
1276 /* If the precision is negative the precision is
1278 specs
[nspecs_done
].info
.prec
= -1;
1280 prec
= specs
[nspecs_done
].info
.prec
;
1283 /* Process format specifiers. */
1286 JUMP (spec
, step4_jumps
);
1288 process_arg ((&specs
[nspecs_done
]));
1290 LABEL (form_unknown
):
1292 extern printf_function
**__printf_function_table
;
1294 printf_function
*function
;
1299 (__printf_function_table
== NULL
? NULL
:
1300 __printf_function_table
[specs
[nspecs_done
].info
.spec
]);
1302 if (function
== NULL
)
1303 function
= &printf_unknown
;
1305 ptr
= alloca (specs
[nspecs_done
].ndata_args
1306 * sizeof (const void *));
1308 /* Fill in an array of pointers to the argument values. */
1309 for (i
= 0; i
< specs
[nspecs_done
].ndata_args
; ++i
)
1310 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
1312 /* Call the function. */
1313 function_done
= (*function
) (s
, &specs
[nspecs_done
].info
, ptr
);
1315 /* If an error occured we don't have information about #
1317 if (function_done
< 0)
1323 done
+= function_done
;
1328 /* Write the following constant string. */
1329 outstring (specs
[nspecs_done
].end_of_fmt
,
1330 specs
[nspecs_done
].next_fmt
1331 - specs
[nspecs_done
].end_of_fmt
);
1336 /* Unlock the stream. */
1337 __libc_cleanup_region_end (1);
1344 # ifdef strong_alias
1345 /* This is for glibc. */
1346 strong_alias (_IO_vfprintf
, vfprintf
);
1348 # if defined __ELF__ || defined __GNU_LIBRARY__
1349 # include <gnu-stabs.h>
1351 weak_alias (_IO_vfprintf
, vfprintf
);
1357 /* Handle an unknown format specifier. This prints out a canonicalized
1358 representation of the format spec itself. */
1360 printf_unknown (FILE *s
, const struct printf_info
*info
,
1361 const void *const *args
)
1365 char work_buffer
[BUFSIZ
];
1376 else if (info
->space
)
1380 if (info
->pad
== '0')
1383 if (info
->width
!= 0)
1385 w
= _itoa_word (info
->width
, workend
+ 1, 10, 0);
1386 while (++w
<= workend
)
1390 if (info
->prec
!= -1)
1393 w
= _itoa_word (info
->prec
, workend
+ 1, 10, 0);
1394 while (++w
<= workend
)
1398 if (info
->spec
!= '\0')
1399 outchar (info
->spec
);
1404 /* Group the digits according to the grouping rules of the current locale.
1405 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1407 group_number (CHAR_T
*w
, CHAR_T
*rear_ptr
, const CHAR_T
*grouping
,
1408 wchar_t thousands_sep
)
1413 /* We treat all negative values like CHAR_MAX. */
1415 if (*grouping
== CHAR_MAX
|| *grouping
< 0)
1416 /* No grouping should be done. */
1421 /* Copy existing string so that nothing gets overwritten. */
1422 src
= (char *) alloca (rear_ptr
- w
);
1423 memcpy (src
, w
+ 1, rear_ptr
- w
);
1424 s
= &src
[rear_ptr
- w
- 1];
1427 /* Process all characters in the string. */
1432 if (--len
== 0 && s
>= src
)
1434 /* A new group begins. */
1435 *w
-- = thousands_sep
;
1438 if (*grouping
== '\0')
1439 /* The previous grouping repeats ad infinitum. */
1441 else if (*grouping
== CHAR_MAX
|| *grouping
< 0)
1443 /* No further grouping to be done.
1444 Copy the rest of the number. */
1456 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
1459 struct _IO_FILE_plus _f
;
1460 _IO_FILE
*_put_stream
;
1461 #ifdef _IO_MTSAFE_IO
1467 _IO_helper_overflow (_IO_FILE
*s
, int c
)
1469 _IO_FILE
*target
= ((struct helper_file
*) s
)->_put_stream
;
1470 int used
= s
->_IO_write_ptr
- s
->_IO_write_base
;
1473 _IO_size_t written
= _IO_sputn (target
, s
->_IO_write_base
, used
);
1474 s
->_IO_write_ptr
-= written
;
1479 static const struct _IO_jump_t _IO_helper_jumps
=
1482 JUMP_INIT (finish
, _IO_default_finish
),
1483 JUMP_INIT (overflow
, _IO_helper_overflow
),
1484 JUMP_INIT (underflow
, _IO_default_underflow
),
1485 JUMP_INIT (uflow
, _IO_default_uflow
),
1486 JUMP_INIT (pbackfail
, _IO_default_pbackfail
),
1487 JUMP_INIT (xsputn
, _IO_default_xsputn
),
1488 JUMP_INIT (xsgetn
, _IO_default_xsgetn
),
1489 JUMP_INIT (seekoff
, _IO_default_seekoff
),
1490 JUMP_INIT (seekpos
, _IO_default_seekpos
),
1491 JUMP_INIT (setbuf
, _IO_default_setbuf
),
1492 JUMP_INIT (sync
, _IO_default_sync
),
1493 JUMP_INIT (doallocate
, _IO_default_doallocate
),
1494 JUMP_INIT (read
, _IO_default_read
),
1495 JUMP_INIT (write
, _IO_default_write
),
1496 JUMP_INIT (seek
, _IO_default_seek
),
1497 JUMP_INIT (close
, _IO_default_close
),
1498 JUMP_INIT (stat
, _IO_default_stat
)
1502 buffered_vfprintf (register _IO_FILE
*s
, const CHAR_T
*format
,
1505 char buf
[_IO_BUFSIZ
];
1506 struct helper_file helper
;
1507 register _IO_FILE
*hp
= (_IO_FILE
*) &helper
;
1508 int result
, to_flush
;
1510 /* Initialize helper. */
1511 helper
._put_stream
= s
;
1512 hp
->_IO_write_base
= buf
;
1513 hp
->_IO_write_ptr
= buf
;
1514 hp
->_IO_write_end
= buf
+ sizeof buf
;
1515 hp
->_IO_file_flags
= _IO_MAGIC
|_IO_NO_READS
;
1516 #ifdef _IO_MTSAFE_IO
1517 hp
->_lock
= &helper
.lock
;
1519 _IO_JUMPS (hp
) = (struct _IO_jump_t
*) &_IO_helper_jumps
;
1521 /* Now print to helper instead. */
1522 result
= _IO_vfprintf (hp
, format
, args
);
1524 /* Now flush anything from the helper to the S. */
1525 if ((to_flush
= hp
->_IO_write_ptr
- hp
->_IO_write_base
) > 0)
1527 if ((int) _IO_sputn (s
, hp
->_IO_write_base
, to_flush
) != to_flush
)
1534 #else /* !USE_IN_LIBIO */
1537 buffered_vfprintf (register FILE *s
, const CHAR_T
*format
, va_list args
)
1542 s
->__bufp
= s
->__buffer
= buf
;
1543 s
->__bufsize
= sizeof buf
;
1544 s
->__put_limit
= s
->__buffer
+ s
->__bufsize
;
1545 s
->__get_limit
= s
->__buffer
;
1547 /* Now use buffer to print. */
1548 result
= vfprintf (s
, format
, args
);
1550 if (fflush (s
) == EOF
)
1552 s
->__buffer
= s
->__bufp
= s
->__get_limit
= s
->__put_limit
= NULL
;
1558 /* Pads string with given number of a specified character.
1559 This code is taken from iopadn.c of the GNU I/O library. */
1561 static const CHAR_T blanks
[PADSIZE
] =
1562 { L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '),
1563 L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' ') };
1564 static const CHAR_T zeroes
[PADSIZE
] =
1565 { L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'),
1566 L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0') };
1569 #ifndef COMPILE_WPRINTF
1570 __printf_pad (FILE *s
, char pad
, size_t count
)
1572 __wprintf_pad (FILE *s
, wchar_t pad
, size_t count
)
1575 const CHAR_T
*padptr
;
1578 padptr
= pad
== L_(' ') ? blanks
: zeroes
;
1580 for (i
= count
; i
>= PADSIZE
; i
-= PADSIZE
)
1581 if (PUT (s
, padptr
, PADSIZE
) != PADSIZE
)
1584 if (PUT (s
, padptr
, i
) != i
)
1590 #endif /* USE_IN_LIBIO */