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
16 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
27 #include "../locale/localeinfo.h"
29 /* This code is shared between the standard stdio implementation found
30 in GNU C library and the libio implementation originally found in
33 Beside this it is also shared between the normal and wide character
34 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
36 #ifndef COMPILE_WPRINTF
38 # define UCHAR_T unsigned char
41 # define ISDIGIT(Ch) isdigit (Ch)
44 # define PUT(F, S, N) _IO_sputn (F, S, N)
45 # define PAD(Padchar) \
47 done += _IO_padn (s, Padchar, width)
49 # define PUTC(C, F) putc (C, F)
50 ssize_t __printf_pad __P ((FILE *, char pad, size_t n));
51 # define PAD(Padchar) \
53 { if (__printf_pad (s, Padchar, width) == -1) \
54 return -1; else done += width; }
57 # define vfprintf vfwprintf
58 # define CHAR_T wchar_t
59 # define UCHAR_T uwchar_t
61 # define L_(Str) L##Str
62 # define ISDIGIT(Ch) iswdigit (Ch)
65 # define PUT(F, S, N) _IO_sputn (F, S, N)
66 # define PAD(Padchar) \
68 done += _IO_wpadn (s, Padchar, width)
70 # define PUTC(C, F) wputc (C, F)
71 ssize_t __wprintf_pad __P ((FILE *, wchar_t pad, size_t n));
72 # define PAD(Padchar) \
74 { if (__wprintf_pad (s, Padchar, width) == -1) \
75 return -1; else done += width; }
79 /* Include the shared code for parsing the format string. */
80 #include "printf-parse.h"
84 /* This code is for use in libio. */
86 # define PUTC(C, F) _IO_putc_unlocked (C, F)
87 # define vfprintf _IO_vfprintf
88 # define size_t _IO_size_t
89 # define FILE _IO_FILE
90 # define va_list _IO_va_list
92 # define BUFSIZ _IO_BUFSIZ
93 # define ARGCHECK(S, Format) \
96 /* Check file argument for consistence. */ \
98 if (S->_flags & _IO_NO_WRITES || Format == NULL) \
104 # define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
105 #else /* ! USE_IN_LIBIO */
106 /* This code is for use in the GNU C library. */
108 # define PUT(F, S, N) fwrite (S, 1, N, F)
109 # define ARGCHECK(S, Format) \
112 /* Check file argument for consistence. */ \
113 if (!__validfp(S) || !S->__mode.__write || Format == NULL) \
120 if (__flshfp (S, EOF) == EOF) \
125 # define UNBUFFERED_P(s) ((s)->__buffer == NULL)
126 # define flockfile(S) /* nothing */
127 # define funlockfile(S) /* nothing */
128 #endif /* USE_IN_LIBIO */
131 #define outchar(Ch) \
134 register const int outc = (Ch); \
135 if (PUTC (outc, s) == EOF) \
142 #define outstring(String, Len) \
145 if (PUT (s, String, Len) != Len) \
151 /* For handling long_double and longlong we use the same flag. */
153 # define is_longlong is_long_double
157 /* Global variables. */
158 static const char null[] = "(null)";
161 /* Helper function to provide temporary buffering for unbuffered streams. */
162 static int buffered_vfprintf __P ((FILE *stream, const CHAR_T *fmt, va_list));
164 /* Handle unknown format specifier. */
165 static int printf_unknown __P ((FILE *, const struct printf_info *,
166 const void *const *));
168 /* Group digits of number string. */
169 static char *group_number __P ((CHAR_T *, CHAR_T *, const CHAR_T *, wchar_t));
172 /* The function itself. */
174 vfprintf (FILE *s, const CHAR_T *format, va_list ap)
176 /* The character used as thousands separator. */
177 wchar_t thousands_sep;
179 /* The string describing the size of groups of digits. */
180 const char *grouping;
182 /* Place to accumulate the result. */
185 /* Current character in format string. */
188 /* End of leading constant string. */
189 const UCHAR_T *lead_str_end;
191 /* Points to next format specifier. */
192 const UCHAR_T *end_of_spec;
194 /* Buffer intermediate results. */
195 char work_buffer[1000];
196 #define workend (&work_buffer[sizeof (work_buffer) - 1])
198 /* State for restartable multibyte character handling functions. */
201 /* We have to save the original argument pointer. */
204 /* Count number of specifiers we already processed. */
208 /* This table maps a character into a number representing a
209 class. In each step there is a destination label for each
211 static const int jump_table[] =
213 /* ' ' */ 1, 0, 0, /* '#' */ 4,
214 0, /* '%' */ 14, 0, /* '\''*/ 6,
215 0, 0, /* '*' */ 7, /* '+' */ 2,
216 0, /* '-' */ 3, /* '.' */ 9, 0,
217 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
218 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
219 /* '8' */ 8, /* '9' */ 8, 0, 0,
222 0, /* 'E' */ 19, 0, /* 'G' */ 19,
224 /* 'L' */ 12, 0, 0, 0,
227 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
229 0, 0, 0, /* 'c' */ 20,
230 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
231 /* 'h' */ 10, /* 'i' */ 15, 0, 0,
232 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
233 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
234 0, /* 'u' */ 16, 0, 0,
238 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < ' ' || (Ch) > 'x')
239 #define CHAR_CLASS(Ch) (jump_table[(int) (Ch) - ' '])
240 #define JUMP(ChExpr, table) \
245 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
246 : table[CHAR_CLASS (spec)]; \
251 #define STEP0_3_TABLE \
252 /* Step 0: at the beginning. */ \
253 static const void *step0_jumps[25] = \
255 REF (form_unknown), \
256 REF (flag_space), /* for ' ' */ \
257 REF (flag_plus), /* for '+' */ \
258 REF (flag_minus), /* for '-' */ \
259 REF (flag_hash), /* for '<hash>' */ \
260 REF (flag_zero), /* for '0' */ \
261 REF (flag_quote), /* for '\'' */ \
262 REF (width_asterics), /* for '*' */ \
263 REF (width), /* for '1'...'9' */ \
264 REF (precision), /* for '.' */ \
265 REF (mod_half), /* for 'h' */ \
266 REF (mod_long), /* for 'l' */ \
267 REF (mod_longlong), /* for 'L', 'q' */ \
268 REF (mod_size_t), /* for 'Z' */ \
269 REF (form_percent), /* for '%' */ \
270 REF (form_integer), /* for 'd', 'i' */ \
271 REF (form_unsigned), /* for 'u' */ \
272 REF (form_octal), /* for 'o' */ \
273 REF (form_hexa), /* for 'X', 'x' */ \
274 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
275 REF (form_character), /* for 'c' */ \
276 REF (form_string), /* for 's' */ \
277 REF (form_pointer), /* for 'p' */ \
278 REF (form_number), /* for 'n' */ \
279 REF (form_strerror) /* for 'm' */ \
281 /* Step 1: after processing width. */ \
282 static const void *step1_jumps[25] = \
284 REF (form_unknown), \
285 REF (form_unknown), /* for ' ' */ \
286 REF (form_unknown), /* for '+' */ \
287 REF (form_unknown), /* for '-' */ \
288 REF (form_unknown), /* for '<hash>' */ \
289 REF (form_unknown), /* for '0' */ \
290 REF (form_unknown), /* for '\'' */ \
291 REF (form_unknown), /* for '*' */ \
292 REF (form_unknown), /* for '1'...'9' */ \
293 REF (precision), /* for '.' */ \
294 REF (mod_half), /* for 'h' */ \
295 REF (mod_long), /* for 'l' */ \
296 REF (mod_longlong), /* for 'L', 'q' */ \
297 REF (mod_size_t), /* for 'Z' */ \
298 REF (form_percent), /* for '%' */ \
299 REF (form_integer), /* for 'd', 'i' */ \
300 REF (form_unsigned), /* for 'u' */ \
301 REF (form_octal), /* for 'o' */ \
302 REF (form_hexa), /* for 'X', 'x' */ \
303 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
304 REF (form_character), /* for 'c' */ \
305 REF (form_string), /* for 's' */ \
306 REF (form_pointer), /* for 'p' */ \
307 REF (form_number), /* for 'n' */ \
308 REF (form_strerror) /* for 'm' */ \
310 /* Step 2: after processing precision. */ \
311 static const void *step2_jumps[25] = \
313 REF (form_unknown), \
314 REF (form_unknown), /* for ' ' */ \
315 REF (form_unknown), /* for '+' */ \
316 REF (form_unknown), /* for '-' */ \
317 REF (form_unknown), /* for '<hash>' */ \
318 REF (form_unknown), /* for '0' */ \
319 REF (form_unknown), /* for '\'' */ \
320 REF (form_unknown), /* for '*' */ \
321 REF (form_unknown), /* for '1'...'9' */ \
322 REF (form_unknown), /* for '.' */ \
323 REF (mod_half), /* for 'h' */ \
324 REF (mod_long), /* for 'l' */ \
325 REF (mod_longlong), /* for 'L', 'q' */ \
326 REF (mod_size_t), /* for 'Z' */ \
327 REF (form_percent), /* for '%' */ \
328 REF (form_integer), /* for 'd', 'i' */ \
329 REF (form_unsigned), /* for 'u' */ \
330 REF (form_octal), /* for 'o' */ \
331 REF (form_hexa), /* for 'X', 'x' */ \
332 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
333 REF (form_character), /* for 'c' */ \
334 REF (form_string), /* for 's' */ \
335 REF (form_pointer), /* for 'p' */ \
336 REF (form_number), /* for 'n' */ \
337 REF (form_strerror) /* for 'm' */ \
339 /* Step 3: after processing first 'l' modifier. */ \
340 static const void *step3_jumps[25] = \
342 REF (form_unknown), \
343 REF (form_unknown), /* for ' ' */ \
344 REF (form_unknown), /* for '+' */ \
345 REF (form_unknown), /* for '-' */ \
346 REF (form_unknown), /* for '<hash>' */ \
347 REF (form_unknown), /* for '0' */ \
348 REF (form_unknown), /* for '\'' */ \
349 REF (form_unknown), /* for '*' */ \
350 REF (form_unknown), /* for '1'...'9' */ \
351 REF (form_unknown), /* for '.' */ \
352 REF (form_unknown), /* for 'h' */ \
353 REF (mod_longlong), /* for 'l' */ \
354 REF (form_unknown), /* for 'L', 'q' */ \
355 REF (form_unknown), /* for 'Z' */ \
356 REF (form_percent), /* for '%' */ \
357 REF (form_integer), /* for 'd', 'i' */ \
358 REF (form_unsigned), /* for 'u' */ \
359 REF (form_octal), /* for 'o' */ \
360 REF (form_hexa), /* for 'X', 'x' */ \
361 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
362 REF (form_character), /* for 'c' */ \
363 REF (form_string), /* for 's' */ \
364 REF (form_pointer), /* for 'p' */ \
365 REF (form_number), /* for 'n' */ \
366 REF (form_strerror) /* for 'm' */ \
369 #define STEP4_TABLE \
370 /* Step 4: processing format specifier. */ \
371 static const void *step4_jumps[25] = \
373 REF (form_unknown), \
374 REF (form_unknown), /* for ' ' */ \
375 REF (form_unknown), /* for '+' */ \
376 REF (form_unknown), /* for '-' */ \
377 REF (form_unknown), /* for '<hash>' */ \
378 REF (form_unknown), /* for '0' */ \
379 REF (form_unknown), /* for '\'' */ \
380 REF (form_unknown), /* for '*' */ \
381 REF (form_unknown), /* for '1'...'9' */ \
382 REF (form_unknown), /* for '.' */ \
383 REF (form_unknown), /* for 'h' */ \
384 REF (form_unknown), /* for 'l' */ \
385 REF (form_unknown), /* for 'L', 'q' */ \
386 REF (form_unknown), /* for 'Z' */ \
387 REF (form_percent), /* for '%' */ \
388 REF (form_integer), /* for 'd', 'i' */ \
389 REF (form_unsigned), /* for 'u' */ \
390 REF (form_octal), /* for 'o' */ \
391 REF (form_hexa), /* for 'X', 'x' */ \
392 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
393 REF (form_character), /* for 'c' */ \
394 REF (form_string), /* for 's' */ \
395 REF (form_pointer), /* for 'p' */ \
396 REF (form_number), /* for 'n' */ \
397 REF (form_strerror) /* for 'm' */ \
401 #define process_arg(fspec) \
402 /* Start real work. We know about all flag and modifiers and \
403 now process the wanted format specifier. */ \
404 LABEL (form_percent): \
405 /* Write a literal "%". */ \
409 LABEL (form_integer): \
410 /* Signed decimal integer. */ \
415 long long int signed_number; \
418 signed_number = va_arg (ap, long long int); \
420 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
422 is_negative = signed_number < 0; \
423 number.longlong = is_negative ? (- signed_number) : signed_number; \
425 goto LABEL (longlong_number); \
429 long int signed_number; \
433 signed_number = va_arg (ap, long int); \
434 else /* `short int' will be promoted to `int'. */ \
435 signed_number = va_arg (ap, int); \
438 signed_number = args_value[fspec->data_arg].pa_long_int; \
440 signed_number = args_value[fspec->data_arg].pa_int; \
442 is_negative = signed_number < 0; \
443 number.word = is_negative ? (- signed_number) : signed_number; \
445 goto LABEL (number); \
449 LABEL (form_unsigned): \
450 /* Unsigned decimal integer. */ \
452 goto LABEL (unsigned_number); \
455 LABEL (form_octal): \
456 /* Unsigned octal integer. */ \
458 goto LABEL (unsigned_number); \
462 /* Unsigned hexadecimal integer. */ \
465 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
467 /* ANSI specifies the `+' and ` ' flags only for signed \
476 number.longlong = va_arg (ap, unsigned long long int); \
478 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
480 LABEL (longlong_number): \
482 /* Supply a default precision if none was given. */ \
485 /* We have to take care for the '0' flag. If a precision \
486 is given it must be ignored. */ \
489 /* If the precision is 0 and the number is 0 nothing has to \
490 be written for the number. */ \
491 if (prec == 0 && number.longlong == 0) \
495 /* Put the number in WORK. */ \
496 string = _itoa (number.longlong, workend + 1, base, \
499 if (group && grouping) \
500 string = group_number (string, workend, grouping, \
503 /* Simply further test for num != 0. */ \
504 number.word = number.longlong != 0; \
510 number.word = va_arg (ap, unsigned long int); \
511 else if (!is_short) \
512 number.word = va_arg (ap, unsigned int); \
514 number.word = (unsigned short int) va_arg (ap, unsigned int); \
517 number.word = args_value[fspec->data_arg].pa_u_long_int; \
518 else if (!is_short) \
519 number.word = args_value[fspec->data_arg].pa_u_int; \
521 number.word = (unsigned short int) \
522 args_value[fspec->data_arg].pa_u_short_int; \
526 /* Supply a default precision if none was given. */ \
529 /* We have to take care for the '0' flag. If a precision \
530 is given it must be ignored. */ \
533 /* If the precision is 0 and the number is 0 nothing has to \
534 be written for the number. */ \
535 if (prec == 0 && number.word == 0) \
539 /* Put the number in WORK. */ \
540 string = _itoa_word (number.word, workend + 1, base, \
543 if (group && grouping) \
544 string = group_number (string, workend, grouping, \
549 prec -= workend - string; \
552 /* Add zeros to the precision. */ \
555 else if (number.word != 0 && alt && base == 8) \
556 /* Add octal marker. */ \
561 width -= workend - string; \
563 if (number.word != 0 && alt && base == 16) \
564 /* Account for 0X hex marker. */ \
567 if (is_negative || showsign || space) \
572 while (width-- > 0) \
575 if (number.word != 0 && alt && base == 16) \
590 if (number.word != 0 && alt && base == 16) \
603 while (width-- > 0) \
607 outstring (string + 1, workend - string); \
613 if (number.word != 0 && alt && base == 16) \
626 width -= workend - string; \
627 outstring (string + 1, workend - string); \
633 LABEL (form_float): \
635 /* Floating-point number. This is handled by printf_fp.c. */ \
636 extern int __printf_fp __P ((FILE *, const struct printf_info *, \
637 const void **const)); \
643 struct printf_info info = { prec: prec, \
646 is_long_double: is_long_double, \
647 is_short: is_short, \
652 showsign: showsign, \
657 if (is_long_double) \
658 the_arg.pa_long_double = va_arg (ap, long double); \
660 the_arg.pa_double = va_arg (ap, double); \
661 ptr = (const void *) &the_arg; \
663 function_done = __printf_fp (s, &info, &ptr); \
667 ptr = (const void *) &args_value[fspec->data_arg]; \
669 function_done = __printf_fp (s, &fspec->info, &ptr); \
672 if (function_done < 0) \
673 /* Error in print handler. */ \
676 done += function_done; \
680 LABEL (form_character): \
682 --width; /* Account for the character itself. */ \
686 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
688 outchar ((unsigned char) args_value[fspec->data_arg].pa_char); \
693 LABEL (form_string): \
697 /* The string argument could in fact be `char *' or `wchar_t *'. \
698 But this should not make a difference here. */ \
700 string = (char *) va_arg (ap, const char *); \
702 string = (char *) args_value[fspec->data_arg].pa_string; \
704 /* Entry point for printing other strings. */ \
705 LABEL (print_string): \
707 if (string == NULL) \
709 /* Write "(null)" if there's space. */ \
710 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
712 string = (char *) null; \
713 len = sizeof (null) - 1; \
717 string = (char *) ""; \
725 /* Search for the end of the string, but don't search past \
726 the length specified by the precision. */ \
727 const char *end = memchr (string, '\0', prec); \
729 len = end - string; \
734 len = strlen (string); \
738 const wchar_t *s2 = (const wchar_t *) string; \
739 mbstate_t mbstate = 0; \
741 len = wcsrtombs (NULL, &s2, prec != -1 ? prec : UINT_MAX, \
743 if (len == (size_t) -1) \
744 /* Illegal wide-character string. */ \
747 s2 = (const wchar_t *) string; \
749 string = alloca (len + 1); \
750 (void) wcsrtombs (string, &s2, prec != -1 ? prec : UINT_MAX, \
754 if ((width -= len) < 0) \
756 outstring (string, len); \
762 outstring (string, len); \
768 LABEL (form_pointer): \
769 /* Generic pointer. */ \
773 ptr = va_arg (ap, void *); \
775 ptr = args_value[fspec->data_arg].pa_pointer; \
778 /* If the pointer is not NULL, write it as a %#x spec. */ \
780 number.word = (unsigned long int) ptr; \
785 goto LABEL (number); \
789 /* Write "(nil)" for a nil pointer. */ \
790 string = (char *) "(nil)"; \
791 /* Make sure the full string "(nil)" is printed. */ \
794 is_long = 0; /* This is no wide-char string. */ \
795 goto LABEL (print_string); \
800 LABEL (form_number): \
801 /* Answer the count of characters written. */ \
804 *(long long int *) va_arg (ap, void *) = done; \
806 *(long int *) va_arg (ap, void *) = done; \
807 else if (!is_short) \
808 *(int *) va_arg (ap, void *) = done; \
810 *(short int *) va_arg (ap, void *) = done; \
813 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
815 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
816 else if (!is_short) \
817 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
819 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
822 LABEL (form_strerror): \
823 /* Print description of error ERRNO. */ \
825 extern char *_strerror_internal __P ((int, char *buf, size_t)); \
828 _strerror_internal (errno, work_buffer, sizeof work_buffer); \
830 is_long = 0; /* This is no wide-char string. */ \
831 goto LABEL (print_string)
834 /* Sanity check of arguments. */
835 ARGCHECK (s, format);
837 if (UNBUFFERED_P (s))
838 /* Use a helper function which will allocate a local temporary buffer
839 for the stream and then call us again. */
840 return buffered_vfprintf (s, format, ap);
842 /* Initialize local variables. */
844 grouping = (const char *) -1;
849 /* Find the first format specifier. */
850 f = lead_str_end = find_spec (format, &mbstate);
855 /* Write the literal text before the first format. */
856 outstring ((const UCHAR_T *) format,
857 lead_str_end - (const UCHAR_T *) format);
859 /* If we only have to print a simple string, return now. */
866 /* Process whole format string. */
869 #define REF(Name) &&do_##Name
870 #define LABEL(Name) do_##Name
874 union printf_arg *args_value; /* This is not used here but ... */
875 int is_negative; /* Flag for negative number. */
878 unsigned long long int longlong;
879 unsigned long int word;
882 union printf_arg the_arg;
883 char *string; /* Pointer to argument string. */
884 int alt = 0; /* Alternate format. */
885 int space = 0; /* Use space prefix if no sign is needed. */
886 int left = 0; /* Left-justify output. */
887 int showsign = 0; /* Always begin with plus or minus sign. */
888 int group = 0; /* Print numbers according grouping rules. */
889 int is_long_double = 0; /* Argument is long double/ long long int. */
890 int is_short = 0; /* Argument is long int. */
891 int is_long = 0; /* Argument is short int. */
892 int width = 0; /* Width of output; 0 means none specified. */
893 int prec = -1; /* Precision of output; -1 means none specified. */
894 char pad = ' '; /* Padding character. */
897 /* Get current character in format string. */
898 JUMP (*++f, step0_jumps);
903 JUMP (*++f, step0_jumps);
908 JUMP (*++f, step0_jumps);
914 JUMP (*++f, step0_jumps);
919 JUMP (*++f, step0_jumps);
925 JUMP (*++f, step0_jumps);
931 /* XXX Completely wrong. Use wctob. */
932 if (grouping == (const char *) -1)
934 /* Figure out the thousands separator character. */
935 if (mbtowc (&thousands_sep,
936 _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
937 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
938 thousands_sep = (wchar_t)
939 *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
940 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
941 if (*grouping == '\0' || *grouping == CHAR_MAX
942 || thousands_sep == L'\0')
945 JUMP (*++f, step0_jumps);
947 /* Get width from argument. */
948 LABEL (width_asterics):
950 const UCHAR_T *tmp; /* Temporary value. */
953 if (ISDIGIT (*tmp) && read_int (&tmp) && *tmp == L_('$'))
954 /* The width comes from a positional parameter. */
957 width = va_arg (ap, int);
959 /* Negative width means left justified. */
967 JUMP (*f, step1_jumps);
969 /* Given width in format string. */
971 width = read_int (&f);
973 /* Oh, oh. The argument comes from a positional parameter. */
975 JUMP (*f, step1_jumps);
981 const UCHAR_T *tmp; /* Temporary value. */
984 if (ISDIGIT (*tmp) && read_int (&tmp) > 0 && *tmp == L_('$'))
985 /* The precision comes from a positional parameter. */
988 prec = va_arg (ap, int);
990 /* If the precision is negative the precision is omitted. */
994 else if (ISDIGIT (*f))
995 prec = read_int (&f);
998 JUMP (*f, step2_jumps);
1000 /* Process 'h' modifier. No other modifier is allowed to
1004 JUMP (*++f, step4_jumps);
1006 /* Process 'l' modifier. There might another 'l' follow. */
1009 JUMP (*++f, step3_jumps);
1011 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1012 allowed to follow. */
1013 LABEL (mod_longlong):
1015 JUMP (*++f, step4_jumps);
1018 is_longlong = sizeof (size_t) > sizeof (unsigned long int);
1019 is_long = sizeof (size_t) > sizeof (unsigned int);
1020 JUMP (*++f, step4_jumps);
1023 /* Process current format. */
1026 process_arg (((struct printf_spec *) NULL));
1028 LABEL (form_unknown):
1029 if (spec == L_('\0'))
1031 /* The format string ended before the specifier is complete. */
1036 /* If we are in the fast loop force entering the complicated
1041 /* Look for next format specifier. */
1042 f = find_spec ((end_of_spec = ++f), &mbstate);
1044 /* Write the following constant string. */
1045 outstring (end_of_spec, f - end_of_spec);
1047 while (*f != L_('\0'));
1049 /* Unlock stream. */
1052 /* We processed the whole format without any positional parameters. */
1055 /* Here starts the more complex loop to handle positional parameters. */
1058 /* Array with information about the needed arguments. This has to
1059 be dynamically extendable. */
1061 size_t nspecs_max = 32; /* A more or less arbitrary start value. */
1062 struct printf_spec *specs
1063 = alloca (nspecs_max * sizeof (struct printf_spec));
1065 /* The number of arguments the format string requests. This will
1066 determine the size of the array needed to store the argument
1070 union printf_arg *args_value;
1072 /* Positional parameters refer to arguments directly. This could
1073 also determine the maximum number of arguments. Track the
1075 size_t max_ref_arg = 0;
1077 /* Just a counter. */
1081 if (grouping == (const char *) -1)
1083 /* XXX Use wctob. But this is incompatible for now. */
1084 /* Figure out the thousands separator character. */
1085 if (mbtowc (&thousands_sep,
1086 _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
1087 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
1088 thousands_sep = (wchar_t) *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1089 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1090 if (*grouping == '\0' || *grouping == CHAR_MAX
1091 || thousands_sep == L'\0')
1095 for (f = lead_str_end; *f != '\0'; f = specs[nspecs++].next_fmt)
1097 if (nspecs >= nspecs_max)
1099 /* Extend the array of format specifiers. */
1100 struct printf_spec *old = specs;
1103 specs = alloca (nspecs_max * sizeof (struct printf_spec));
1105 if (specs == &old[nspecs])
1106 /* Stack grows up, OLD was the last thing allocated;
1108 nspecs_max += nspecs_max / 2;
1111 /* Copy the old array's elements to the new space. */
1112 memcpy (specs, old, nspecs * sizeof (struct printf_spec));
1113 if (old == &specs[nspecs])
1114 /* Stack grows down, OLD was just below the new
1115 SPECS. We can use that space when the new space
1117 nspecs_max += nspecs_max / 2;
1121 /* Parse the format specifier. */
1122 nargs += parse_one_spec (f, nargs, &specs[nspecs], &max_ref_arg, NULL);
1125 /* Determine the number of arguments the format string consumes. */
1126 nargs = MAX (nargs, max_ref_arg);
1128 /* Allocate memory for the argument descriptions. */
1129 args_type = alloca (nargs * sizeof (int));
1130 memset (args_type, 0, nargs * sizeof (int));
1131 args_value = alloca (nargs * sizeof (union printf_arg));
1133 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1134 still zero after this loop, format is invalid. For now we
1135 simply use 0 as the value. */
1137 /* Fill in the types of all the arguments. */
1138 for (cnt = 0; cnt < nspecs; ++cnt)
1140 /* If the width is determined by an argument this is an int. */
1141 if (specs[cnt].width_arg != -1)
1142 args_type[specs[cnt].width_arg] = PA_INT;
1144 /* If the precision is determined by an argument this is an int. */
1145 if (specs[cnt].prec_arg != -1)
1146 args_type[specs[cnt].prec_arg] = PA_INT;
1148 switch (specs[cnt].ndata_args)
1150 case 0: /* No arguments. */
1152 case 1: /* One argument; we already have the type. */
1153 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1156 /* We have more than one argument for this format spec.
1157 We must call the arginfo function again to determine
1159 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1161 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg]);
1166 /* Now we know all the types and the order. Fill in the argument
1168 for (cnt = 0, ap = ap_save; cnt < nargs; ++cnt)
1169 switch (args_type[cnt])
1171 #define T(tag, mem, type) \
1173 args_value[cnt].mem = va_arg (ap, type); \
1176 T (PA_CHAR, pa_char, int); /* Promoted. */
1177 T (PA_INT|PA_FLAG_SHORT, pa_short_int, int); /* Promoted. */
1178 T (PA_INT, pa_int, int);
1179 T (PA_INT|PA_FLAG_LONG, pa_long_int, long int);
1180 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1181 T (PA_FLOAT, pa_float, double); /* Promoted. */
1182 T (PA_DOUBLE, pa_double, double);
1183 T (PA_DOUBLE|PA_FLAG_LONG_DOUBLE, pa_long_double, long double);
1184 T (PA_STRING, pa_string, const char *);
1185 T (PA_POINTER, pa_pointer, void *);
1188 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1189 args_value[cnt].pa_pointer = va_arg (ap, void *);
1191 args_value[cnt].pa_long_double = 0.0;
1195 /* Now walk through all format specifiers and process them. */
1196 for (; nspecs_done < nspecs; ++nspecs_done)
1199 #define REF(Name) &&do2_##Name
1201 #define LABEL(Name) do2_##Name
1207 unsigned long long int longlong;
1208 unsigned long int word;
1211 union printf_arg the_arg;
1212 char *string; /* Pointer to argument string. */
1214 /* Fill variables from values in struct. */
1215 int alt = specs[nspecs_done].info.alt;
1216 int space = specs[nspecs_done].info.space;
1217 int left = specs[nspecs_done].info.left;
1218 int showsign = specs[nspecs_done].info.showsign;
1219 int group = specs[nspecs_done].info.group;
1220 int is_long_double = specs[nspecs_done].info.is_long_double;
1221 int is_short = specs[nspecs_done].info.is_short;
1222 int is_long = specs[nspecs_done].info.is_long;
1223 int width = specs[nspecs_done].info.width;
1224 int prec = specs[nspecs_done].info.prec;
1225 char pad = specs[nspecs_done].info.pad;
1226 CHAR_T spec = specs[nspecs_done].info.spec;
1228 /* Fill in last information. */
1229 if (specs[nspecs_done].width_arg != -1)
1231 /* Extract the field width from an argument. */
1232 specs[nspecs_done].info.width =
1233 args_value[specs[nspecs_done].width_arg].pa_int;
1235 if (specs[nspecs_done].info.width < 0)
1236 /* If the width value is negative left justification is
1237 selected and the value is taken as being positive. */
1239 specs[nspecs_done].info.width *= -1;
1240 left = specs[nspecs_done].info.left = 1;
1242 width = specs[nspecs_done].info.width;
1245 if (specs[nspecs_done].prec_arg != -1)
1247 /* Extract the precision from an argument. */
1248 specs[nspecs_done].info.prec =
1249 args_value[specs[nspecs_done].prec_arg].pa_int;
1251 if (specs[nspecs_done].info.prec < 0)
1252 /* If the precision is negative the precision is
1254 specs[nspecs_done].info.prec = -1;
1256 prec = specs[nspecs_done].info.prec;
1259 /* Process format specifiers. */
1262 JUMP (spec, step4_jumps);
1264 process_arg ((&specs[nspecs_done]));
1266 LABEL (form_unknown):
1268 extern printf_function **__printf_function_table;
1270 printf_function *function;
1275 (__printf_function_table == NULL ? NULL :
1276 __printf_function_table[specs[nspecs_done].info.spec]);
1278 if (function == NULL)
1279 function = &printf_unknown;
1281 ptr = alloca (specs[nspecs_done].ndata_args
1282 * sizeof (const void *));
1284 /* Fill in an array of pointers to the argument values. */
1285 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
1286 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1288 /* Call the function. */
1289 function_done = (*function) (s, &specs[nspecs_done].info, ptr);
1291 /* If an error occured we don't have information about #
1293 if (function_done < 0)
1299 done += function_done;
1304 /* Write the following constant string. */
1305 outstring (specs[nspecs_done].end_of_fmt,
1306 specs[nspecs_done].next_fmt
1307 - specs[nspecs_done].end_of_fmt);
1311 /* Unlock the stream. */
1319 # ifdef strong_alias
1320 /* This is for glibc. */
1321 strong_alias (_IO_vfprintf, vfprintf);
1323 # if defined __ELF__ || defined __GNU_LIBRARY__
1324 # include <gnu-stabs.h>
1326 weak_alias (_IO_vfprintf, vfprintf);
1332 /* Handle an unknown format specifier. This prints out a canonicalized
1333 representation of the format spec itself. */
1335 printf_unknown (FILE *s, const struct printf_info *info,
1336 const void *const *args)
1340 char work_buffer[BUFSIZ];
1351 else if (info->space)
1355 if (info->pad == '0')
1358 if (info->width != 0)
1360 w = _itoa_word (info->width, workend + 1, 10, 0);
1361 while (++w <= workend)
1365 if (info->prec != -1)
1368 w = _itoa_word (info->prec, workend + 1, 10, 0);
1369 while (++w <= workend)
1373 if (info->spec != '\0')
1374 outchar (info->spec);
1379 /* Group the digits according to the grouping rules of the current locale.
1380 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1382 group_number (CHAR_T *w, CHAR_T *rear_ptr, const CHAR_T *grouping,
1383 wchar_t thousands_sep)
1388 /* We treat all negative values like CHAR_MAX. */
1390 if (*grouping == CHAR_MAX || *grouping < 0)
1391 /* No grouping should be done. */
1396 /* Copy existing string so that nothing gets overwritten. */
1397 src = (char *) alloca (rear_ptr - w);
1398 memcpy (src, w + 1, rear_ptr - w);
1399 s = &src[rear_ptr - w - 1];
1402 /* Process all characters in the string. */
1407 if (--len == 0 && s >= src)
1409 /* A new group begins. */
1410 *w-- = thousands_sep;
1413 if (*grouping == '\0')
1414 /* The previous grouping repeats ad infinitum. */
1416 else if (*grouping == CHAR_MAX || *grouping < 0)
1418 /* No further grouping to be done.
1419 Copy the rest of the number. */
1431 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
1434 struct _IO_FILE_plus _f;
1435 _IO_FILE *_put_stream;
1439 _IO_helper_overflow (_IO_FILE *s, int c)
1441 _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
1442 int used = s->_IO_write_ptr - s->_IO_write_base;
1445 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
1446 s->_IO_write_ptr -= written;
1451 static const struct _IO_jump_t _IO_helper_jumps =
1454 JUMP_INIT (finish, _IO_default_finish),
1455 JUMP_INIT (overflow, _IO_helper_overflow),
1456 JUMP_INIT (underflow, _IO_default_underflow),
1457 JUMP_INIT (uflow, _IO_default_uflow),
1458 JUMP_INIT (pbackfail, _IO_default_pbackfail),
1459 JUMP_INIT (xsputn, _IO_default_xsputn),
1460 JUMP_INIT (xsgetn, _IO_default_xsgetn),
1461 JUMP_INIT (seekoff, _IO_default_seekoff),
1462 JUMP_INIT (seekpos, _IO_default_seekpos),
1463 JUMP_INIT (setbuf, _IO_default_setbuf),
1464 JUMP_INIT (sync, _IO_default_sync),
1465 JUMP_INIT (doallocate, _IO_default_doallocate),
1466 JUMP_INIT (read, _IO_default_read),
1467 JUMP_INIT (write, _IO_default_write),
1468 JUMP_INIT (seek, _IO_default_seek),
1469 JUMP_INIT (close, _IO_default_close),
1470 JUMP_INIT (stat, _IO_default_stat)
1474 buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format,
1477 char buf[_IO_BUFSIZ];
1478 struct helper_file helper;
1479 register _IO_FILE *hp = (_IO_FILE *) &helper;
1480 int result, to_flush;
1482 /* Initialize helper. */
1483 helper._put_stream = s;
1484 hp->_IO_write_base = buf;
1485 hp->_IO_write_ptr = buf;
1486 hp->_IO_write_end = buf + sizeof buf;
1487 hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS;
1488 _IO_JUMPS (hp) = (struct _IO_jump_t *) &_IO_helper_jumps;
1490 /* Now print to helper instead. */
1491 result = _IO_vfprintf (hp, format, args);
1493 /* Now flush anything from the helper to the S. */
1494 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
1496 if (_IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
1503 #else /* !USE_IN_LIBIO */
1506 buffered_vfprintf (register FILE *s, const CHAR_T *format, va_list args)
1511 s->__bufp = s->__buffer = buf;
1512 s->__bufsize = sizeof buf;
1513 s->__put_limit = s->__buffer + s->__bufsize;
1514 s->__get_limit = s->__buffer;
1516 /* Now use buffer to print. */
1517 result = vfprintf (s, format, args);
1519 if (fflush (s) == EOF)
1521 s->__buffer = s->__bufp = s->__get_limit = s->__put_limit = NULL;
1527 /* Pads string with given number of a specified character.
1528 This code is taken from iopadn.c of the GNU I/O library. */
1530 static const CHAR_T blanks[PADSIZE] =
1531 { L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '),
1532 L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' ') };
1533 static const CHAR_T zeroes[PADSIZE] =
1534 { L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'),
1535 L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0') };
1538 #ifndef COMPILE_WPRINTF
1539 __printf_pad (FILE *s, char pad, size_t count)
1541 __wprintf_pad (FILE *s, wchar_t pad, size_t count)
1544 const CHAR_T *padptr;
1547 padptr = pad == L_(' ') ? blanks : zeroes;
1549 for (i = count; i >= PADSIZE; i -= PADSIZE)
1550 if (PUT (s, padptr, PADSIZE) != PADSIZE)
1553 if (PUT (s, padptr, i) != i)
1559 #endif /* USE_IN_LIBIO */