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 { ssize_t __res = __printf_pad (s, (Padchar), width); \
55 if (__res == -1) return -1; \
59 # define vfprintf vfwprintf
60 # define CHAR_T wchar_t
61 # define UCHAR_T uwchar_t
63 # define L_(Str) L##Str
64 # define ISDIGIT(Ch) iswdigit (Ch)
67 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
68 # define PAD(Padchar) \
70 done += _IO_wpadn (s, (Padchar), width)
72 # define PUTC(C, F) wputc (C, F)
73 ssize_t __wprintf_pad __P ((FILE *, wchar_t pad, size_t n));
74 # define PAD(Padchar) \
76 { ssize_t __res = __wprintf_pad (s, (Padchar), width); \
77 if (__res == -1) return -1; \
82 /* Include the shared code for parsing the format string. */
83 #include "printf-parse.h"
87 /* This code is for use in libio. */
89 # define PUTC(C, F) _IO_putc_unlocked (C, F)
90 # define vfprintf _IO_vfprintf
91 # define FILE _IO_FILE
93 # define va_list _IO_va_list
95 # define BUFSIZ _IO_BUFSIZ
96 # define ARGCHECK(S, Format) \
99 /* Check file argument for consistence. */ \
100 CHECK_FILE (S, -1); \
101 if (S->_flags & _IO_NO_WRITES || Format == NULL) \
107 # define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
108 #else /* ! USE_IN_LIBIO */
109 /* This code is for use in the GNU C library. */
111 # define PUT(F, S, N) fwrite (S, 1, N, F)
112 # define ARGCHECK(S, Format) \
115 /* Check file argument for consistence. */ \
116 if (!__validfp(S) || !S->__mode.__write || Format == NULL) \
118 __set_errno (EINVAL); \
123 if (__flshfp (S, EOF) == EOF) \
128 # define UNBUFFERED_P(s) ((s)->__buffer == NULL)
130 /* XXX These declarations should go as soon as the stdio header files
131 have these prototypes. */
132 extern void __flockfile (FILE *);
133 extern void __funlockfile (FILE *);
134 #endif /* USE_IN_LIBIO */
137 #define outchar(Ch) \
140 register const int outc = (Ch); \
141 if (PUTC (outc, s) == EOF) \
148 #define outstring(String, Len) \
151 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
157 /* For handling long_double and longlong we use the same flag. */
159 # define is_longlong is_long_double
163 /* Global variables. */
164 static const char null[] = "(null)";
167 /* Helper function to provide temporary buffering for unbuffered streams. */
168 static int buffered_vfprintf __P ((FILE *stream, const CHAR_T *fmt, va_list));
170 /* Handle unknown format specifier. */
171 static int printf_unknown __P ((FILE *, const struct printf_info *,
172 const void *const *));
174 /* Group digits of number string. */
175 static char *group_number __P ((CHAR_T *, CHAR_T *, const CHAR_T *, wchar_t));
178 /* The function itself. */
180 vfprintf (FILE *s, const CHAR_T *format, va_list ap)
182 /* The character used as thousands separator. */
183 wchar_t thousands_sep;
185 /* The string describing the size of groups of digits. */
186 const char *grouping;
188 /* Place to accumulate the result. */
191 /* Current character in format string. */
194 /* End of leading constant string. */
195 const UCHAR_T *lead_str_end;
197 /* Points to next format specifier. */
198 const UCHAR_T *end_of_spec;
200 /* Buffer intermediate results. */
201 char work_buffer[1000];
202 #define workend (&work_buffer[sizeof (work_buffer) - 1])
204 /* State for restartable multibyte character handling functions. */
207 /* We have to save the original argument pointer. */
210 /* Count number of specifiers we already processed. */
213 /* For the %m format we may need the current `errno' value. */
214 int save_errno = errno;
217 /* This table maps a character into a number representing a
218 class. In each step there is a destination label for each
220 static const int jump_table[] =
222 /* ' ' */ 1, 0, 0, /* '#' */ 4,
223 0, /* '%' */ 14, 0, /* '\''*/ 6,
224 0, 0, /* '*' */ 7, /* '+' */ 2,
225 0, /* '-' */ 3, /* '.' */ 9, 0,
226 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
227 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
228 /* '8' */ 8, /* '9' */ 8, 0, 0,
230 0, 0, 0, /* 'C' */ 25,
231 0, /* 'E' */ 19, 0, /* 'G' */ 19,
233 /* 'L' */ 12, 0, 0, 0,
234 0, 0, 0, /* 'S' */ 21,
236 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
238 0, 0, 0, /* 'c' */ 20,
239 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
240 /* 'h' */ 10, /* 'i' */ 15, 0, 0,
241 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
242 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
243 0, /* 'u' */ 16, 0, 0,
247 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < ' ' || (Ch) > 'x')
248 #define CHAR_CLASS(Ch) (jump_table[(int) (Ch) - ' '])
249 #define JUMP(ChExpr, table) \
254 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
255 : table[CHAR_CLASS (spec)]; \
260 #define STEP0_3_TABLE \
261 /* Step 0: at the beginning. */ \
262 static const void *step0_jumps[26] = \
264 REF (form_unknown), \
265 REF (flag_space), /* for ' ' */ \
266 REF (flag_plus), /* for '+' */ \
267 REF (flag_minus), /* for '-' */ \
268 REF (flag_hash), /* for '<hash>' */ \
269 REF (flag_zero), /* for '0' */ \
270 REF (flag_quote), /* for '\'' */ \
271 REF (width_asterics), /* for '*' */ \
272 REF (width), /* for '1'...'9' */ \
273 REF (precision), /* for '.' */ \
274 REF (mod_half), /* for 'h' */ \
275 REF (mod_long), /* for 'l' */ \
276 REF (mod_longlong), /* for 'L', 'q' */ \
277 REF (mod_size_t), /* for 'Z' */ \
278 REF (form_percent), /* for '%' */ \
279 REF (form_integer), /* for 'd', 'i' */ \
280 REF (form_unsigned), /* for 'u' */ \
281 REF (form_octal), /* for 'o' */ \
282 REF (form_hexa), /* for 'X', 'x' */ \
283 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
284 REF (form_character), /* for 'c' */ \
285 REF (form_string), /* for 's', 'S' */ \
286 REF (form_pointer), /* for 'p' */ \
287 REF (form_number), /* for 'n' */ \
288 REF (form_strerror), /* for 'm' */ \
289 REF (form_wcharacter) /* for 'C' */ \
291 /* Step 1: after processing width. */ \
292 static const void *step1_jumps[26] = \
294 REF (form_unknown), \
295 REF (form_unknown), /* for ' ' */ \
296 REF (form_unknown), /* for '+' */ \
297 REF (form_unknown), /* for '-' */ \
298 REF (form_unknown), /* for '<hash>' */ \
299 REF (form_unknown), /* for '0' */ \
300 REF (form_unknown), /* for '\'' */ \
301 REF (form_unknown), /* for '*' */ \
302 REF (form_unknown), /* for '1'...'9' */ \
303 REF (precision), /* for '.' */ \
304 REF (mod_half), /* for 'h' */ \
305 REF (mod_long), /* for 'l' */ \
306 REF (mod_longlong), /* for 'L', 'q' */ \
307 REF (mod_size_t), /* for 'Z' */ \
308 REF (form_percent), /* for '%' */ \
309 REF (form_integer), /* for 'd', 'i' */ \
310 REF (form_unsigned), /* for 'u' */ \
311 REF (form_octal), /* for 'o' */ \
312 REF (form_hexa), /* for 'X', 'x' */ \
313 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
314 REF (form_character), /* for 'c' */ \
315 REF (form_string), /* for 's', 'S' */ \
316 REF (form_pointer), /* for 'p' */ \
317 REF (form_number), /* for 'n' */ \
318 REF (form_strerror), /* for 'm' */ \
319 REF (form_wcharacter) /* for 'C' */ \
321 /* Step 2: after processing precision. */ \
322 static const void *step2_jumps[26] = \
324 REF (form_unknown), \
325 REF (form_unknown), /* for ' ' */ \
326 REF (form_unknown), /* for '+' */ \
327 REF (form_unknown), /* for '-' */ \
328 REF (form_unknown), /* for '<hash>' */ \
329 REF (form_unknown), /* for '0' */ \
330 REF (form_unknown), /* for '\'' */ \
331 REF (form_unknown), /* for '*' */ \
332 REF (form_unknown), /* for '1'...'9' */ \
333 REF (form_unknown), /* for '.' */ \
334 REF (mod_half), /* for 'h' */ \
335 REF (mod_long), /* for 'l' */ \
336 REF (mod_longlong), /* for 'L', 'q' */ \
337 REF (mod_size_t), /* for 'Z' */ \
338 REF (form_percent), /* for '%' */ \
339 REF (form_integer), /* for 'd', 'i' */ \
340 REF (form_unsigned), /* for 'u' */ \
341 REF (form_octal), /* for 'o' */ \
342 REF (form_hexa), /* for 'X', 'x' */ \
343 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
344 REF (form_character), /* for 'c' */ \
345 REF (form_string), /* for 's', 'S' */ \
346 REF (form_pointer), /* for 'p' */ \
347 REF (form_number), /* for 'n' */ \
348 REF (form_strerror), /* for 'm' */ \
349 REF (form_wcharacter) /* for 'C' */ \
351 /* Step 3: after processing first 'l' modifier. */ \
352 static const void *step3_jumps[26] = \
354 REF (form_unknown), \
355 REF (form_unknown), /* for ' ' */ \
356 REF (form_unknown), /* for '+' */ \
357 REF (form_unknown), /* for '-' */ \
358 REF (form_unknown), /* for '<hash>' */ \
359 REF (form_unknown), /* for '0' */ \
360 REF (form_unknown), /* for '\'' */ \
361 REF (form_unknown), /* for '*' */ \
362 REF (form_unknown), /* for '1'...'9' */ \
363 REF (form_unknown), /* for '.' */ \
364 REF (form_unknown), /* for 'h' */ \
365 REF (mod_longlong), /* for 'l' */ \
366 REF (form_unknown), /* for 'L', 'q' */ \
367 REF (form_unknown), /* for 'Z' */ \
368 REF (form_percent), /* for '%' */ \
369 REF (form_integer), /* for 'd', 'i' */ \
370 REF (form_unsigned), /* for 'u' */ \
371 REF (form_octal), /* for 'o' */ \
372 REF (form_hexa), /* for 'X', 'x' */ \
373 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
374 REF (form_character), /* for 'c' */ \
375 REF (form_string), /* for 's', 'S' */ \
376 REF (form_pointer), /* for 'p' */ \
377 REF (form_number), /* for 'n' */ \
378 REF (form_strerror), /* for 'm' */ \
379 REF (form_wcharacter) /* for 'C' */ \
382 #define STEP4_TABLE \
383 /* Step 4: processing format specifier. */ \
384 static const void *step4_jumps[26] = \
386 REF (form_unknown), \
387 REF (form_unknown), /* for ' ' */ \
388 REF (form_unknown), /* for '+' */ \
389 REF (form_unknown), /* for '-' */ \
390 REF (form_unknown), /* for '<hash>' */ \
391 REF (form_unknown), /* for '0' */ \
392 REF (form_unknown), /* for '\'' */ \
393 REF (form_unknown), /* for '*' */ \
394 REF (form_unknown), /* for '1'...'9' */ \
395 REF (form_unknown), /* for '.' */ \
396 REF (form_unknown), /* for 'h' */ \
397 REF (form_unknown), /* for 'l' */ \
398 REF (form_unknown), /* for 'L', 'q' */ \
399 REF (form_unknown), /* for 'Z' */ \
400 REF (form_percent), /* for '%' */ \
401 REF (form_integer), /* for 'd', 'i' */ \
402 REF (form_unsigned), /* for 'u' */ \
403 REF (form_octal), /* for 'o' */ \
404 REF (form_hexa), /* for 'X', 'x' */ \
405 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
406 REF (form_character), /* for 'c' */ \
407 REF (form_string), /* for 's', 'S' */ \
408 REF (form_pointer), /* for 'p' */ \
409 REF (form_number), /* for 'n' */ \
410 REF (form_strerror), /* for 'm' */ \
411 REF (form_wcharacter) /* for 'C' */ \
415 #define process_arg(fspec) \
416 /* Start real work. We know about all flags and modifiers and \
417 now process the wanted format specifier. */ \
418 LABEL (form_percent): \
419 /* Write a literal "%". */ \
423 LABEL (form_integer): \
424 /* Signed decimal integer. */ \
429 long long int signed_number; \
432 signed_number = va_arg (ap, long long int); \
434 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
436 is_negative = signed_number < 0; \
437 number.longlong = is_negative ? (- signed_number) : signed_number; \
439 goto LABEL (longlong_number); \
443 long int signed_number; \
447 signed_number = va_arg (ap, long int); \
448 else /* `short int' will be promoted to `int'. */ \
449 signed_number = va_arg (ap, int); \
452 signed_number = args_value[fspec->data_arg].pa_long_int; \
454 signed_number = args_value[fspec->data_arg].pa_int; \
456 is_negative = signed_number < 0; \
457 number.word = is_negative ? (- signed_number) : signed_number; \
459 goto LABEL (number); \
463 LABEL (form_unsigned): \
464 /* Unsigned decimal integer. */ \
466 goto LABEL (unsigned_number); \
469 LABEL (form_octal): \
470 /* Unsigned octal integer. */ \
472 goto LABEL (unsigned_number); \
476 /* Unsigned hexadecimal integer. */ \
479 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
481 /* ANSI specifies the `+' and ` ' flags only for signed \
490 number.longlong = va_arg (ap, unsigned long long int); \
492 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
494 LABEL (longlong_number): \
496 /* Supply a default precision if none was given. */ \
499 /* We have to take care for the '0' flag. If a precision \
500 is given it must be ignored. */ \
503 /* If the precision is 0 and the number is 0 nothing has to \
504 be written for the number. */ \
505 if (prec == 0 && number.longlong == 0) \
509 /* Put the number in WORK. */ \
510 string = _itoa (number.longlong, workend + 1, base, \
513 if (group && grouping) \
514 string = group_number (string, workend, grouping, \
517 /* Simply further test for num != 0. */ \
518 number.word = number.longlong != 0; \
524 number.word = va_arg (ap, unsigned long int); \
525 else if (!is_short) \
526 number.word = va_arg (ap, unsigned int); \
528 number.word = (unsigned short int) va_arg (ap, unsigned int); \
531 number.word = args_value[fspec->data_arg].pa_u_long_int; \
532 else if (!is_short) \
533 number.word = args_value[fspec->data_arg].pa_u_int; \
535 number.word = (unsigned short int) \
536 args_value[fspec->data_arg].pa_u_short_int; \
540 /* Supply a default precision if none was given. */ \
543 /* We have to take care for the '0' flag. If a precision \
544 is given it must be ignored. */ \
547 /* If the precision is 0 and the number is 0 nothing has to \
548 be written for the number. */ \
549 if (prec == 0 && number.word == 0) \
553 /* Put the number in WORK. */ \
554 string = _itoa_word (number.word, workend + 1, base, \
557 if (group && grouping) \
558 string = group_number (string, workend, grouping, \
563 prec -= workend - string; \
566 /* Add zeros to the precision. */ \
569 else if (number.word != 0 && alt && base == 8) \
570 /* Add octal marker. */ \
575 width -= workend - string; \
577 if (number.word != 0 && alt && base == 16) \
578 /* Account for 0X hex marker. */ \
581 if (is_negative || showsign || space) \
586 while (width-- > 0) \
589 if (number.word != 0 && alt && base == 16) \
604 if (number.word != 0 && alt && base == 16) \
617 while (width-- > 0) \
621 outstring (string + 1, workend - string); \
627 if (number.word != 0 && alt && base == 16) \
640 width -= workend - string; \
641 outstring (string + 1, workend - string); \
647 LABEL (form_float): \
649 /* Floating-point number. This is handled by printf_fp.c. */ \
650 extern int __printf_fp __P ((FILE *, const struct printf_info *, \
651 const void **const)); \
657 struct printf_info info = { prec: prec, \
660 is_long_double: is_long_double, \
661 is_short: is_short, \
666 showsign: showsign, \
671 if (is_long_double) \
672 the_arg.pa_long_double = va_arg (ap, long double); \
674 the_arg.pa_double = va_arg (ap, double); \
675 ptr = (const void *) &the_arg; \
677 function_done = __printf_fp (s, &info, &ptr); \
681 ptr = (const void *) &args_value[fspec->data_arg]; \
683 function_done = __printf_fp (s, &fspec->info, &ptr); \
686 if (function_done < 0) \
687 /* Error in print handler. */ \
690 done += function_done; \
694 LABEL (form_character): \
697 goto LABEL (form_wcharacter); \
698 --width; /* Account for the character itself. */ \
702 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
704 outchar ((unsigned char) args_value[fspec->data_arg].pa_char); \
709 LABEL (form_wcharacter): \
711 /* Wide character. */ \
712 char buf[MB_CUR_MAX]; \
716 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wint_t) \
717 : args_value[fspec->data_arg].pa_wchar), \
722 outstring (buf, len); \
728 LABEL (form_string): \
732 /* The string argument could in fact be `char *' or `wchar_t *'. \
733 But this should not make a difference here. */ \
735 string = (char *) va_arg (ap, const char *); \
737 string = (char *) args_value[fspec->data_arg].pa_string; \
739 /* Entry point for printing other strings. */ \
740 LABEL (print_string): \
742 if (string == NULL) \
744 /* Write "(null)" if there's space. */ \
745 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
747 string = (char *) null; \
748 len = sizeof (null) - 1; \
752 string = (char *) ""; \
756 else if (!is_long && spec != L_('S')) \
759 /* Search for the end of the string, but don't search past \
760 the length specified by the precision. */ \
761 len = strnlen (string, prec); \
763 len = strlen (string); \
767 const wchar_t *s2 = (const wchar_t *) string; \
770 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
771 if (len == (size_t) -1) \
772 /* Illegal wide-character string. */ \
775 s2 = (const wchar_t *) string; \
776 string = alloca (len + 1); \
777 (void) __wcsrtombs (string, &s2, prec != -1 ? prec : UINT_MAX, \
781 if ((width -= len) < 0) \
783 outstring (string, len); \
789 outstring (string, len); \
795 LABEL (form_pointer): \
796 /* Generic pointer. */ \
800 ptr = va_arg (ap, void *); \
802 ptr = args_value[fspec->data_arg].pa_pointer; \
805 /* If the pointer is not NULL, write it as a %#x spec. */ \
807 number.word = (unsigned long int) ptr; \
812 goto LABEL (number); \
816 /* Write "(nil)" for a nil pointer. */ \
817 string = (char *) "(nil)"; \
818 /* Make sure the full string "(nil)" is printed. */ \
821 is_long = 0; /* This is no wide-char string. */ \
822 goto LABEL (print_string); \
827 LABEL (form_number): \
828 /* Answer the count of characters written. */ \
831 *(long long int *) va_arg (ap, void *) = done; \
833 *(long int *) va_arg (ap, void *) = done; \
834 else if (!is_short) \
835 *(int *) va_arg (ap, void *) = done; \
837 *(short int *) va_arg (ap, void *) = done; \
840 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
842 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
843 else if (!is_short) \
844 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
846 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
849 LABEL (form_strerror): \
850 /* Print description of error ERRNO. */ \
852 extern char *_strerror_internal __P ((int, char *buf, size_t)); \
855 _strerror_internal (save_errno, work_buffer, sizeof work_buffer); \
857 is_long = 0; /* This is no wide-char string. */ \
858 goto LABEL (print_string)
861 /* Sanity check of arguments. */
862 ARGCHECK (s, format);
864 if (UNBUFFERED_P (s))
865 /* Use a helper function which will allocate a local temporary buffer
866 for the stream and then call us again. */
867 return buffered_vfprintf (s, format, ap);
869 /* Initialize local variables. */
871 grouping = (const char *) -1;
875 /* Find the first format specifier. */
876 f = lead_str_end = find_spec (format, &mbstate);
880 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
883 __libc_cleanup_region_start ((void (*) (void *)) &__funlockfile, s);
887 /* Write the literal text before the first format. */
888 outstring ((const UCHAR_T *) format,
889 lead_str_end - (const UCHAR_T *) format);
891 /* If we only have to print a simple string, return now. */
895 /* Process whole format string. */
898 #define REF(Name) &&do_##Name
899 #define LABEL(Name) do_##Name
903 union printf_arg *args_value; /* This is not used here but ... */
904 int is_negative; /* Flag for negative number. */
907 unsigned long long int longlong;
908 unsigned long int word;
911 union printf_arg the_arg;
912 char *string; /* Pointer to argument string. */
913 int alt = 0; /* Alternate format. */
914 int space = 0; /* Use space prefix if no sign is needed. */
915 int left = 0; /* Left-justify output. */
916 int showsign = 0; /* Always begin with plus or minus sign. */
917 int group = 0; /* Print numbers according grouping rules. */
918 int is_long_double = 0; /* Argument is long double/ long long int. */
919 int is_short = 0; /* Argument is long int. */
920 int is_long = 0; /* Argument is short int. */
921 int width = 0; /* Width of output; 0 means none specified. */
922 int prec = -1; /* Precision of output; -1 means none specified. */
923 char pad = ' '; /* Padding character. */
926 /* Get current character in format string. */
927 JUMP (*++f, step0_jumps);
932 JUMP (*++f, step0_jumps);
937 JUMP (*++f, step0_jumps);
943 JUMP (*++f, step0_jumps);
948 JUMP (*++f, step0_jumps);
954 JUMP (*++f, step0_jumps);
960 /* XXX Completely wrong. Use wctob. */
961 if (grouping == (const char *) -1)
963 /* Figure out the thousands separator character. */
964 if (mbtowc (&thousands_sep,
965 _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
966 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
967 thousands_sep = (wchar_t)
968 *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
969 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
970 if (*grouping == '\0' || *grouping == CHAR_MAX
971 || thousands_sep == L'\0')
974 JUMP (*++f, step0_jumps);
976 /* Get width from argument. */
977 LABEL (width_asterics):
979 const UCHAR_T *tmp; /* Temporary value. */
982 if (ISDIGIT (*tmp) && read_int (&tmp) && *tmp == L_('$'))
983 /* The width comes from a positional parameter. */
986 width = va_arg (ap, int);
988 /* Negative width means left justified. */
996 JUMP (*f, step1_jumps);
998 /* Given width in format string. */
1000 width = read_int (&f);
1002 /* Oh, oh. The argument comes from a positional parameter. */
1004 JUMP (*f, step1_jumps);
1010 const UCHAR_T *tmp; /* Temporary value. */
1013 if (ISDIGIT (*tmp) && read_int (&tmp) > 0 && *tmp == L_('$'))
1014 /* The precision comes from a positional parameter. */
1017 prec = va_arg (ap, int);
1019 /* If the precision is negative the precision is omitted. */
1023 else if (ISDIGIT (*f))
1024 prec = read_int (&f);
1027 JUMP (*f, step2_jumps);
1029 /* Process 'h' modifier. No other modifier is allowed to
1033 JUMP (*++f, step4_jumps);
1035 /* Process 'l' modifier. There might another 'l' follow. */
1038 JUMP (*++f, step3_jumps);
1040 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1041 allowed to follow. */
1042 LABEL (mod_longlong):
1044 JUMP (*++f, step4_jumps);
1047 is_longlong = sizeof (size_t) > sizeof (unsigned long int);
1048 is_long = sizeof (size_t) > sizeof (unsigned int);
1049 JUMP (*++f, step4_jumps);
1052 /* Process current format. */
1055 process_arg (((struct printf_spec *) NULL));
1057 LABEL (form_unknown):
1058 if (spec == L_('\0'))
1060 /* The format string ended before the specifier is complete. */
1065 /* If we are in the fast loop force entering the complicated
1070 /* Look for next format specifier. */
1071 f = find_spec ((end_of_spec = ++f), &mbstate);
1073 /* Write the following constant string. */
1074 outstring (end_of_spec, f - end_of_spec);
1076 while (*f != L_('\0'));
1078 /* Unlock stream and return. */
1081 /* Here starts the more complex loop to handle positional parameters. */
1084 /* Array with information about the needed arguments. This has to
1085 be dynamically extendable. */
1087 size_t nspecs_max = 32; /* A more or less arbitrary start value. */
1088 struct printf_spec *specs
1089 = alloca (nspecs_max * sizeof (struct printf_spec));
1091 /* The number of arguments the format string requests. This will
1092 determine the size of the array needed to store the argument
1096 union printf_arg *args_value;
1098 /* Positional parameters refer to arguments directly. This could
1099 also determine the maximum number of arguments. Track the
1101 size_t max_ref_arg = 0;
1103 /* Just a counter. */
1107 if (grouping == (const char *) -1)
1109 /* XXX Use wctob. But this is incompatible for now. */
1110 /* Figure out the thousands separator character. */
1111 if (mbtowc (&thousands_sep,
1112 _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
1113 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
1114 thousands_sep = (wchar_t) *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1115 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1116 if (*grouping == '\0' || *grouping == CHAR_MAX
1117 || thousands_sep == L'\0')
1121 for (f = lead_str_end; *f != '\0'; f = specs[nspecs++].next_fmt)
1123 if (nspecs >= nspecs_max)
1125 /* Extend the array of format specifiers. */
1126 struct printf_spec *old = specs;
1129 specs = alloca (nspecs_max * sizeof (struct printf_spec));
1131 if (specs == &old[nspecs])
1132 /* Stack grows up, OLD was the last thing allocated;
1134 nspecs_max += nspecs_max / 2;
1137 /* Copy the old array's elements to the new space. */
1138 memcpy (specs, old, nspecs * sizeof (struct printf_spec));
1139 if (old == &specs[nspecs])
1140 /* Stack grows down, OLD was just below the new
1141 SPECS. We can use that space when the new space
1143 nspecs_max += nspecs_max / 2;
1147 /* Parse the format specifier. */
1148 nargs += parse_one_spec (f, nargs, &specs[nspecs], &max_ref_arg,
1152 /* Determine the number of arguments the format string consumes. */
1153 nargs = MAX (nargs, max_ref_arg);
1155 /* Allocate memory for the argument descriptions. */
1156 args_type = alloca (nargs * sizeof (int));
1157 memset (args_type, 0, nargs * sizeof (int));
1158 args_value = alloca (nargs * sizeof (union printf_arg));
1160 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1161 still zero after this loop, format is invalid. For now we
1162 simply use 0 as the value. */
1164 /* Fill in the types of all the arguments. */
1165 for (cnt = 0; cnt < nspecs; ++cnt)
1167 /* If the width is determined by an argument this is an int. */
1168 if (specs[cnt].width_arg != -1)
1169 args_type[specs[cnt].width_arg] = PA_INT;
1171 /* If the precision is determined by an argument this is an int. */
1172 if (specs[cnt].prec_arg != -1)
1173 args_type[specs[cnt].prec_arg] = PA_INT;
1175 switch (specs[cnt].ndata_args)
1177 case 0: /* No arguments. */
1179 case 1: /* One argument; we already have the type. */
1180 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1183 /* We have more than one argument for this format spec.
1184 We must call the arginfo function again to determine
1186 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1188 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg]);
1193 /* Now we know all the types and the order. Fill in the argument
1195 for (cnt = 0, ap = ap_save; cnt < nargs; ++cnt)
1196 switch (args_type[cnt])
1198 #define T(tag, mem, type) \
1200 args_value[cnt].mem = va_arg (ap, type); \
1203 T (PA_CHAR, pa_char, int); /* Promoted. */
1204 T (PA_WCHAR, pa_wchar, wint_t);
1205 T (PA_INT|PA_FLAG_SHORT, pa_short_int, int); /* Promoted. */
1206 T (PA_INT, pa_int, int);
1207 T (PA_INT|PA_FLAG_LONG, pa_long_int, long int);
1208 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1209 T (PA_FLOAT, pa_float, double); /* Promoted. */
1210 T (PA_DOUBLE, pa_double, double);
1211 T (PA_DOUBLE|PA_FLAG_LONG_DOUBLE, pa_long_double, long double);
1212 T (PA_STRING, pa_string, const char *);
1213 T (PA_WSTRING, pa_wstring, const wchar_t *);
1214 T (PA_POINTER, pa_pointer, void *);
1217 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1218 args_value[cnt].pa_pointer = va_arg (ap, void *);
1220 args_value[cnt].pa_long_double = 0.0;
1224 /* Now walk through all format specifiers and process them. */
1225 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1228 #define REF(Name) &&do2_##Name
1230 #define LABEL(Name) do2_##Name
1236 unsigned long long int longlong;
1237 unsigned long int word;
1240 union printf_arg the_arg;
1241 char *string; /* Pointer to argument string. */
1243 /* Fill variables from values in struct. */
1244 int alt = specs[nspecs_done].info.alt;
1245 int space = specs[nspecs_done].info.space;
1246 int left = specs[nspecs_done].info.left;
1247 int showsign = specs[nspecs_done].info.showsign;
1248 int group = specs[nspecs_done].info.group;
1249 int is_long_double = specs[nspecs_done].info.is_long_double;
1250 int is_short = specs[nspecs_done].info.is_short;
1251 int is_long = specs[nspecs_done].info.is_long;
1252 int width = specs[nspecs_done].info.width;
1253 int prec = specs[nspecs_done].info.prec;
1254 char pad = specs[nspecs_done].info.pad;
1255 CHAR_T spec = specs[nspecs_done].info.spec;
1257 /* Fill in last information. */
1258 if (specs[nspecs_done].width_arg != -1)
1260 /* Extract the field width from an argument. */
1261 specs[nspecs_done].info.width =
1262 args_value[specs[nspecs_done].width_arg].pa_int;
1264 if (specs[nspecs_done].info.width < 0)
1265 /* If the width value is negative left justification is
1266 selected and the value is taken as being positive. */
1268 specs[nspecs_done].info.width *= -1;
1269 left = specs[nspecs_done].info.left = 1;
1271 width = specs[nspecs_done].info.width;
1274 if (specs[nspecs_done].prec_arg != -1)
1276 /* Extract the precision from an argument. */
1277 specs[nspecs_done].info.prec =
1278 args_value[specs[nspecs_done].prec_arg].pa_int;
1280 if (specs[nspecs_done].info.prec < 0)
1281 /* If the precision is negative the precision is
1283 specs[nspecs_done].info.prec = -1;
1285 prec = specs[nspecs_done].info.prec;
1288 /* Process format specifiers. */
1291 JUMP (spec, step4_jumps);
1293 process_arg ((&specs[nspecs_done]));
1295 LABEL (form_unknown):
1297 extern printf_function **__printf_function_table;
1299 printf_function *function;
1304 (__printf_function_table == NULL ? NULL :
1305 __printf_function_table[specs[nspecs_done].info.spec]);
1307 if (function == NULL)
1308 function = &printf_unknown;
1310 ptr = alloca (specs[nspecs_done].ndata_args
1311 * sizeof (const void *));
1313 /* Fill in an array of pointers to the argument values. */
1314 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
1315 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1317 /* Call the function. */
1318 function_done = (*function) (s, &specs[nspecs_done].info, ptr);
1320 /* If an error occured we don't have information about #
1322 if (function_done < 0)
1328 done += function_done;
1333 /* Write the following constant string. */
1334 outstring (specs[nspecs_done].end_of_fmt,
1335 specs[nspecs_done].next_fmt
1336 - specs[nspecs_done].end_of_fmt);
1341 /* Unlock the stream. */
1342 __libc_cleanup_region_end (1);
1349 # ifdef strong_alias
1350 /* This is for glibc. */
1351 strong_alias (_IO_vfprintf, vfprintf);
1353 # if defined __ELF__ || defined __GNU_LIBRARY__
1354 # include <gnu-stabs.h>
1356 weak_alias (_IO_vfprintf, vfprintf);
1362 /* Handle an unknown format specifier. This prints out a canonicalized
1363 representation of the format spec itself. */
1365 printf_unknown (FILE *s, const struct printf_info *info,
1366 const void *const *args)
1370 char work_buffer[BUFSIZ];
1381 else if (info->space)
1385 if (info->pad == '0')
1388 if (info->width != 0)
1390 w = _itoa_word (info->width, workend + 1, 10, 0);
1391 while (++w <= workend)
1395 if (info->prec != -1)
1398 w = _itoa_word (info->prec, workend + 1, 10, 0);
1399 while (++w <= workend)
1403 if (info->spec != '\0')
1404 outchar (info->spec);
1409 /* Group the digits according to the grouping rules of the current locale.
1410 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1412 group_number (CHAR_T *w, CHAR_T *rear_ptr, const CHAR_T *grouping,
1413 wchar_t thousands_sep)
1418 /* We treat all negative values like CHAR_MAX. */
1420 if (*grouping == CHAR_MAX || *grouping < 0)
1421 /* No grouping should be done. */
1426 /* Copy existing string so that nothing gets overwritten. */
1427 src = (char *) alloca (rear_ptr - w);
1428 memcpy (src, w + 1, rear_ptr - w);
1429 s = &src[rear_ptr - w - 1];
1432 /* Process all characters in the string. */
1437 if (--len == 0 && s >= src)
1439 /* A new group begins. */
1440 *w-- = thousands_sep;
1443 if (*grouping == '\0')
1444 /* The previous grouping repeats ad infinitum. */
1446 else if (*grouping == CHAR_MAX || *grouping < 0)
1448 /* No further grouping to be done.
1449 Copy the rest of the number. */
1461 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
1464 struct _IO_FILE_plus _f;
1465 _IO_FILE *_put_stream;
1466 #ifdef _IO_MTSAFE_IO
1472 _IO_helper_overflow (_IO_FILE *s, int c)
1474 _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
1475 int used = s->_IO_write_ptr - s->_IO_write_base;
1478 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
1479 s->_IO_write_ptr -= written;
1484 static const struct _IO_jump_t _IO_helper_jumps =
1487 JUMP_INIT (finish, _IO_default_finish),
1488 JUMP_INIT (overflow, _IO_helper_overflow),
1489 JUMP_INIT (underflow, _IO_default_underflow),
1490 JUMP_INIT (uflow, _IO_default_uflow),
1491 JUMP_INIT (pbackfail, _IO_default_pbackfail),
1492 JUMP_INIT (xsputn, _IO_default_xsputn),
1493 JUMP_INIT (xsgetn, _IO_default_xsgetn),
1494 JUMP_INIT (seekoff, _IO_default_seekoff),
1495 JUMP_INIT (seekpos, _IO_default_seekpos),
1496 JUMP_INIT (setbuf, _IO_default_setbuf),
1497 JUMP_INIT (sync, _IO_default_sync),
1498 JUMP_INIT (doallocate, _IO_default_doallocate),
1499 JUMP_INIT (read, _IO_default_read),
1500 JUMP_INIT (write, _IO_default_write),
1501 JUMP_INIT (seek, _IO_default_seek),
1502 JUMP_INIT (close, _IO_default_close),
1503 JUMP_INIT (stat, _IO_default_stat)
1507 buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format,
1510 char buf[_IO_BUFSIZ];
1511 struct helper_file helper;
1512 register _IO_FILE *hp = (_IO_FILE *) &helper;
1513 int result, to_flush;
1515 /* Initialize helper. */
1516 helper._put_stream = s;
1517 hp->_IO_write_base = buf;
1518 hp->_IO_write_ptr = buf;
1519 hp->_IO_write_end = buf + sizeof buf;
1520 hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS;
1521 #ifdef _IO_MTSAFE_IO
1522 hp->_lock = &helper.lock;
1523 __libc_lock_init (*hp->_lock);
1525 _IO_JUMPS (hp) = (struct _IO_jump_t *) &_IO_helper_jumps;
1527 /* Now print to helper instead. */
1528 result = _IO_vfprintf (hp, format, args);
1530 /* Now flush anything from the helper to the S. */
1531 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
1533 if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
1540 #else /* !USE_IN_LIBIO */
1543 buffered_vfprintf (register FILE *s, const CHAR_T *format, va_list args)
1548 s->__bufp = s->__buffer = buf;
1549 s->__bufsize = sizeof buf;
1550 s->__put_limit = s->__buffer + s->__bufsize;
1551 s->__get_limit = s->__buffer;
1553 /* Now use buffer to print. */
1554 result = vfprintf (s, format, args);
1556 if (fflush (s) == EOF)
1558 s->__buffer = s->__bufp = s->__get_limit = s->__put_limit = NULL;
1564 /* Pads string with given number of a specified character.
1565 This code is taken from iopadn.c of the GNU I/O library. */
1567 static const CHAR_T blanks[PADSIZE] =
1568 { L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '),
1569 L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' ') };
1570 static const CHAR_T zeroes[PADSIZE] =
1571 { L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'),
1572 L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0') };
1575 #ifndef COMPILE_WPRINTF
1576 __printf_pad (FILE *s, char pad, size_t count)
1578 __wprintf_pad (FILE *s, wchar_t pad, size_t count)
1581 const CHAR_T *padptr;
1584 padptr = pad == L_(' ') ? blanks : zeroes;
1586 for (i = count; i >= PADSIZE; i -= PADSIZE)
1587 if (PUT (s, padptr, PADSIZE) != PADSIZE)
1590 if (PUT (s, padptr, i) != i)
1596 #endif /* USE_IN_LIBIO */