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 (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 #endif /* USE_IN_LIBIO */
129 #define outchar(Ch) \
132 register const int outc = (Ch); \
133 if (PUTC (outc, s) == EOF) \
140 #define outstring(String, Len) \
143 if (PUT (s, String, Len) != Len) \
149 /* For handling long_double and longlong we use the same flag. */
151 # define is_longlong is_long_double
155 /* Global variables. */
156 static const char null
[] = "(null)";
159 /* Helper function to provide temporary buffering for unbuffered streams. */
160 static int buffered_vfprintf
__P ((FILE *stream
, const CHAR_T
*fmt
, va_list));
162 /* Handle unknown format specifier. */
163 static int printf_unknown
__P ((FILE *, const struct printf_info
*,
164 const void *const *));
166 /* Group digits of number string. */
167 static char *group_number
__P ((CHAR_T
*, CHAR_T
*, const CHAR_T
*, wchar_t));
170 /* The function itself. */
172 vfprintf (FILE *s
, const CHAR_T
*format
, va_list ap
)
174 /* The character used as thousands separator. */
175 wchar_t thousands_sep
;
177 /* The string describing the size of groups of digits. */
178 const char *grouping
;
180 /* Place to accumulate the result. */
183 /* Current character in format string. */
186 /* End of leading constant string. */
187 const UCHAR_T
*lead_str_end
;
189 /* Points to next format specifier. */
190 const UCHAR_T
*end_of_spec
;
192 /* Buffer intermediate results. */
193 char work_buffer
[1000];
194 #define workend (&work_buffer[sizeof (work_buffer) - 1])
196 /* State for restartable multibyte character handling functions. */
199 /* We have to save the original argument pointer. */
202 /* Count number of specifiers we already processed. */
206 /* This table maps a character into a number representing a
207 class. In each step there is a destination label for each
209 static const int jump_table
[] =
211 /* ' ' */ 1, 0, 0, /* '#' */ 4,
212 0, /* '%' */ 14, 0, /* '\''*/ 6,
213 0, 0, /* '*' */ 7, /* '+' */ 2,
214 0, /* '-' */ 3, /* '.' */ 9, 0,
215 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
216 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
217 /* '8' */ 8, /* '9' */ 8, 0, 0,
220 0, /* 'E' */ 19, 0, /* 'G' */ 19,
222 /* 'L' */ 12, 0, 0, 0,
225 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
227 0, 0, 0, /* 'c' */ 20,
228 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
229 /* 'h' */ 10, /* 'i' */ 15, 0, 0,
230 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
231 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
232 0, /* 'u' */ 16, 0, 0,
236 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < ' ' || (Ch) > 'x')
237 #define CHAR_CLASS(Ch) (jump_table[(int) (Ch) - ' '])
238 #define JUMP(ChExpr, table) \
243 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
244 : table[CHAR_CLASS (spec)]; \
249 #define STEP0_3_TABLE \
250 /* Step 0: at the beginning. */ \
251 static const void *step0_jumps[25] = \
253 REF (form_unknown), \
254 REF (flag_space), /* for ' ' */ \
255 REF (flag_plus), /* for '+' */ \
256 REF (flag_minus), /* for '-' */ \
257 REF (flag_hash), /* for '<hash>' */ \
258 REF (flag_zero), /* for '0' */ \
259 REF (flag_quote), /* for '\'' */ \
260 REF (width_asterics), /* for '*' */ \
261 REF (width), /* for '1'...'9' */ \
262 REF (precision), /* for '.' */ \
263 REF (mod_half), /* for 'h' */ \
264 REF (mod_long), /* for 'l' */ \
265 REF (mod_longlong), /* for 'L', 'q' */ \
266 REF (mod_size_t), /* for 'Z' */ \
267 REF (form_percent), /* for '%' */ \
268 REF (form_integer), /* for 'd', 'i' */ \
269 REF (form_unsigned), /* for 'u' */ \
270 REF (form_octal), /* for 'o' */ \
271 REF (form_hexa), /* for 'X', 'x' */ \
272 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
273 REF (form_character), /* for 'c' */ \
274 REF (form_string), /* for 's' */ \
275 REF (form_pointer), /* for 'p' */ \
276 REF (form_number), /* for 'n' */ \
277 REF (form_strerror) /* for 'm' */ \
279 /* Step 1: after processing width. */ \
280 static const void *step1_jumps[25] = \
282 REF (form_unknown), \
283 REF (form_unknown), /* for ' ' */ \
284 REF (form_unknown), /* for '+' */ \
285 REF (form_unknown), /* for '-' */ \
286 REF (form_unknown), /* for '<hash>' */ \
287 REF (form_unknown), /* for '0' */ \
288 REF (form_unknown), /* for '\'' */ \
289 REF (form_unknown), /* for '*' */ \
290 REF (form_unknown), /* for '1'...'9' */ \
291 REF (precision), /* for '.' */ \
292 REF (mod_half), /* for 'h' */ \
293 REF (mod_long), /* for 'l' */ \
294 REF (mod_longlong), /* for 'L', 'q' */ \
295 REF (mod_size_t), /* for 'Z' */ \
296 REF (form_percent), /* for '%' */ \
297 REF (form_integer), /* for 'd', 'i' */ \
298 REF (form_unsigned), /* for 'u' */ \
299 REF (form_octal), /* for 'o' */ \
300 REF (form_hexa), /* for 'X', 'x' */ \
301 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
302 REF (form_character), /* for 'c' */ \
303 REF (form_string), /* for 's' */ \
304 REF (form_pointer), /* for 'p' */ \
305 REF (form_number), /* for 'n' */ \
306 REF (form_strerror) /* for 'm' */ \
308 /* Step 2: after processing precision. */ \
309 static const void *step2_jumps[25] = \
311 REF (form_unknown), \
312 REF (form_unknown), /* for ' ' */ \
313 REF (form_unknown), /* for '+' */ \
314 REF (form_unknown), /* for '-' */ \
315 REF (form_unknown), /* for '<hash>' */ \
316 REF (form_unknown), /* for '0' */ \
317 REF (form_unknown), /* for '\'' */ \
318 REF (form_unknown), /* for '*' */ \
319 REF (form_unknown), /* for '1'...'9' */ \
320 REF (form_unknown), /* for '.' */ \
321 REF (mod_half), /* for 'h' */ \
322 REF (mod_long), /* for 'l' */ \
323 REF (mod_longlong), /* for 'L', 'q' */ \
324 REF (mod_size_t), /* for 'Z' */ \
325 REF (form_percent), /* for '%' */ \
326 REF (form_integer), /* for 'd', 'i' */ \
327 REF (form_unsigned), /* for 'u' */ \
328 REF (form_octal), /* for 'o' */ \
329 REF (form_hexa), /* for 'X', 'x' */ \
330 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
331 REF (form_character), /* for 'c' */ \
332 REF (form_string), /* for 's' */ \
333 REF (form_pointer), /* for 'p' */ \
334 REF (form_number), /* for 'n' */ \
335 REF (form_strerror) /* for 'm' */ \
337 /* Step 3: after processing first 'l' modifier. */ \
338 static const void *step3_jumps[25] = \
340 REF (form_unknown), \
341 REF (form_unknown), /* for ' ' */ \
342 REF (form_unknown), /* for '+' */ \
343 REF (form_unknown), /* for '-' */ \
344 REF (form_unknown), /* for '<hash>' */ \
345 REF (form_unknown), /* for '0' */ \
346 REF (form_unknown), /* for '\'' */ \
347 REF (form_unknown), /* for '*' */ \
348 REF (form_unknown), /* for '1'...'9' */ \
349 REF (form_unknown), /* for '.' */ \
350 REF (form_unknown), /* for 'h' */ \
351 REF (mod_longlong), /* for 'l' */ \
352 REF (form_unknown), /* for 'L', 'q' */ \
353 REF (form_unknown), /* for 'Z' */ \
354 REF (form_percent), /* for '%' */ \
355 REF (form_integer), /* for 'd', 'i' */ \
356 REF (form_unsigned), /* for 'u' */ \
357 REF (form_octal), /* for 'o' */ \
358 REF (form_hexa), /* for 'X', 'x' */ \
359 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
360 REF (form_character), /* for 'c' */ \
361 REF (form_string), /* for 's' */ \
362 REF (form_pointer), /* for 'p' */ \
363 REF (form_number), /* for 'n' */ \
364 REF (form_strerror) /* for 'm' */ \
367 #define STEP4_TABLE \
368 /* Step 4: processing format specifier. */ \
369 static const void *step4_jumps[25] = \
371 REF (form_unknown), \
372 REF (form_unknown), /* for ' ' */ \
373 REF (form_unknown), /* for '+' */ \
374 REF (form_unknown), /* for '-' */ \
375 REF (form_unknown), /* for '<hash>' */ \
376 REF (form_unknown), /* for '0' */ \
377 REF (form_unknown), /* for '\'' */ \
378 REF (form_unknown), /* for '*' */ \
379 REF (form_unknown), /* for '1'...'9' */ \
380 REF (form_unknown), /* for '.' */ \
381 REF (form_unknown), /* for 'h' */ \
382 REF (form_unknown), /* for 'l' */ \
383 REF (form_unknown), /* for 'L', 'q' */ \
384 REF (form_unknown), /* for 'Z' */ \
385 REF (form_percent), /* for '%' */ \
386 REF (form_integer), /* for 'd', 'i' */ \
387 REF (form_unsigned), /* for 'u' */ \
388 REF (form_octal), /* for 'o' */ \
389 REF (form_hexa), /* for 'X', 'x' */ \
390 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
391 REF (form_character), /* for 'c' */ \
392 REF (form_string), /* for 's' */ \
393 REF (form_pointer), /* for 'p' */ \
394 REF (form_number), /* for 'n' */ \
395 REF (form_strerror) /* for 'm' */ \
399 #define process_arg(fspec) \
400 /* Start real work. We know about all flag and modifiers and \
401 now process the wanted format specifier. */ \
402 LABEL (form_percent): \
403 /* Write a literal "%". */ \
407 LABEL (form_integer): \
408 /* Signed decimal integer. */ \
413 long long int signed_number; \
415 signed_number = va_arg (ap, long long int); \
417 is_negative = signed_number < 0; \
418 number.longlong = is_negative ? (- signed_number) : signed_number; \
420 goto LABEL (longlong_number); \
424 long int signed_number; \
427 signed_number = va_arg (ap, long int); \
428 else /* `short int' will be promoted to `int'. */ \
429 signed_number = va_arg (ap, int); \
431 is_negative = signed_number < 0; \
432 number.word = is_negative ? (- signed_number) : signed_number; \
434 goto LABEL (number); \
438 LABEL (form_unsigned): \
439 /* Unsigned decimal integer. */ \
441 goto LABEL (unsigned_number); \
444 LABEL (form_octal): \
445 /* Unsigned octal integer. */ \
447 goto LABEL (unsigned_number); \
451 /* Unsigned hexadecimal integer. */ \
454 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
456 /* ANSI specifies the `+' and ` ' flags only for signed \
464 number.longlong = va_arg (ap, unsigned long long int); \
466 LABEL (longlong_number): \
468 /* Supply a default precision if none was given. */ \
471 /* We have to take care for the '0' flag. If a precision \
472 is given it must be ignored. */ \
475 /* If the precision is 0 and the number is 0 nothing has to \
476 be written for the number. */ \
477 if (prec == 0 && number.longlong == 0) \
481 /* Put the number in WORK. */ \
482 string = _itoa (number.longlong, workend + 1, base, \
485 if (group && grouping) \
486 string = group_number (string, workend, grouping, \
489 /* Simply further test for num != 0. */ \
490 number.word = number.longlong != 0; \
495 number.word = va_arg (ap, unsigned long int); \
497 number.word = va_arg (ap, unsigned int); /* Promoted. */ \
501 /* Supply a default precision if none was given. */ \
504 /* We have to take care for the '0' flag. If a precision \
505 is given it must be ignored. */ \
508 /* If the precision is 0 and the number is 0 nothing has to \
509 be written for the number. */ \
510 if (prec == 0 && number.word == 0) \
514 /* Put the number in WORK. */ \
515 string = _itoa_word (number.word, workend + 1, base, \
518 if (group && grouping) \
519 string = group_number (string, workend, grouping, \
524 prec -= workend - string; \
527 /* Add zeros to the precision. */ \
530 else if (number.word != 0 && alt && base == 8) \
531 /* Add octal marker. */ \
536 width -= workend - string; \
538 if (number.word != 0 && alt && base == 16) \
539 /* Account for 0X hex marker. */ \
542 if (is_negative || showsign || space) \
547 while (width-- > 0) \
550 if (number.word != 0 && alt && base == 16) \
565 if (number.word != 0 && alt && base == 16) \
578 while (width-- > 0) \
582 outstring (string + 1, workend - string); \
588 if (number.word != 0 && alt && base == 16) \
601 width -= workend - string; \
602 outstring (string + 1, workend - string); \
608 LABEL (form_float): \
610 /* Floating-point number. This is handled by printf_fp.c. */ \
611 extern int __printf_fp __P ((FILE *, const struct printf_info *, \
612 const void **const)); \
616 if (is_long_double) \
617 the_arg.pa_long_double = va_arg (ap, long double); \
619 the_arg.pa_double = va_arg (ap, double); \
621 ptr = (const void *) &the_arg; \
625 struct printf_info info = { prec: prec, \
628 is_long_double: is_long_double, \
629 is_short: is_short, \
634 showsign: showsign, \
638 function_done = __printf_fp (s, &info, &ptr); \
641 function_done = __printf_fp (s, &fspec->info, &ptr); \
643 if (function_done < 0) \
644 /* Error in print handler. */ \
647 done += function_done; \
651 LABEL (form_character): \
653 --width; /* Account for the character itself. */ \
656 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
661 LABEL (form_string): \
665 /* The string argument could in fact be `char *' or `wchar_t *'. \
666 But this should not make a difference here. */ \
667 string = (char *) va_arg (ap, const char *); \
669 /* Entry point for printing other strings. */ \
670 LABEL (print_string): \
672 if (string == NULL) \
674 /* Write "(null)" if there's space. */ \
675 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
677 string = (char *) null; \
678 len = sizeof (null) - 1; \
682 string = (char *) ""; \
690 /* Search for the end of the string, but don't search past \
691 the length specified by the precision. */ \
692 const char *end = memchr (string, '\0', prec); \
694 len = end - string; \
699 len = strlen (string); \
703 const wchar_t *s2 = (const wchar_t *) string; \
704 mbstate_t mbstate = 0; \
706 len = wcsrtombs (NULL, &s2, prec != -1 ? prec : UINT_MAX, \
708 if (len == (size_t) -1) \
709 /* Illegal wide-character string. */ \
712 s2 = (const wchar_t *) string; \
714 string = alloca (len + 1); \
715 (void) wcsrtombs (string, &s2, prec != -1 ? prec : UINT_MAX, \
719 if ((width -= len) < 0) \
721 outstring (string, len); \
727 outstring (string, len); \
733 LABEL (form_pointer): \
734 /* Generic pointer. */ \
737 ptr = va_arg (ap, void *); \
740 /* If the pointer is not NULL, write it as a %#x spec. */ \
742 number.word = (unsigned long int) ptr; \
747 goto LABEL (number); \
751 /* Write "(nil)" for a nil pointer. */ \
752 string = (char *) "(nil)"; \
753 /* Make sure the full string "(nil)" is printed. */ \
756 is_long = 0; /* This is no wide-char string. */ \
757 goto LABEL (print_string); \
762 LABEL (form_number): \
763 /* Answer the count of characters written. */ \
765 *(long long int *) va_arg (ap, void *) = done; \
767 *(long int *) va_arg (ap, void *) = done; \
768 else if (!is_short) \
769 *(int *) va_arg (ap, void *) = done; \
771 *(short int *) va_arg (ap, void *) = done; \
774 LABEL (form_strerror): \
775 /* Print description of error ERRNO. */ \
777 extern char *_strerror_internal __P ((int, char *buf, size_t)); \
780 _strerror_internal (errno, work_buffer, sizeof work_buffer); \
782 is_long = 0; /* This is no wide-char string. */ \
783 goto LABEL (print_string)
786 /* Sanity check of arguments. */
787 ARGCHECK (s
, format
);
789 if (UNBUFFERED_P (s
))
790 /* Use a helper function which will allocate a local temporary buffer
791 for the stream and then call us again. */
792 return buffered_vfprintf (s
, format
, ap
);
794 /* Initialize local variables. */
796 grouping
= (const char *) -1;
801 /* Find the first format specifier. */
802 f
= lead_str_end
= find_spec (format
, &mbstate
);
804 /* Write the literal text before the first format. */
805 outstring ((const UCHAR_T
*) format
,
806 lead_str_end
- (const UCHAR_T
*) format
);
808 /* If we only have to print a simple string, return now. */
812 /* Process whole format string. */
815 #define REF(Name) &&do_##Name
816 #define LABEL(Name) do_##Name
820 int is_negative
; /* Flag for negative number. */
823 unsigned long long int longlong
;
824 unsigned long int word
;
827 union printf_arg the_arg
;
828 char *string
; /* Pointer to argument string. */
829 int alt
= 0; /* Alternate format. */
830 int space
= 0; /* Use space prefix if no sign is needed. */
831 int left
= 0; /* Left-justify output. */
832 int showsign
= 0; /* Always begin with plus or minus sign. */
833 int group
= 0; /* Print numbers according grouping rules. */
834 int is_long_double
= 0; /* Argument is long double/ long long int. */
835 int is_short
= 0; /* Argument is long int. */
836 int is_long
= 0; /* Argument is short int. */
837 int width
= 0; /* Width of output; 0 means none specified. */
838 int prec
= -1; /* Precision of output; -1 means none specified. */
839 char pad
= ' '; /* Padding character. */
842 /* Get current character in format string. */
843 JUMP (*++f
, step0_jumps
);
848 JUMP (*++f
, step0_jumps
);
853 JUMP (*++f
, step0_jumps
);
859 JUMP (*++f
, step0_jumps
);
864 JUMP (*++f
, step0_jumps
);
870 JUMP (*++f
, step0_jumps
);
876 /* XXX Completely wrong. Use wctob. */
877 if (grouping
== (const char *) -1)
879 /* Figure out the thousands separator character. */
880 if (mbtowc (&thousands_sep
,
881 _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
),
882 strlen (_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
))) <= 0)
883 thousands_sep
= (wchar_t)
884 *_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
885 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
886 if (*grouping
== '\0' || *grouping
== CHAR_MAX
887 || thousands_sep
== L
'\0')
890 JUMP (*++f
, step0_jumps
);
892 /* Get width from argument. */
893 LABEL (width_asterics
):
895 const UCHAR_T
*tmp
; /* Temporary value. */
898 if (ISDIGIT (*tmp
) && read_int (&tmp
) && *tmp
== L_('$'))
899 /* The width comes from a positional parameter. */
902 width
= va_arg (ap
, int);
904 /* Negative width means left justified. */
912 JUMP (*f
, step1_jumps
);
914 /* Given width in format string. */
916 width
= read_int (&f
);
918 /* Oh, oh. The argument comes from a positional parameter. */
920 JUMP (*f
, step1_jumps
);
926 const UCHAR_T
*tmp
; /* Temporary value. */
929 if (ISDIGIT (*tmp
) && read_int (&tmp
) > 0 && *tmp
== L_('$'))
930 /* The precision comes from a positional parameter. */
933 prec
= va_arg (ap
, int);
935 /* If the precision is negative the precision is omitted. */
939 else if (ISDIGIT (*f
))
940 prec
= read_int (&f
);
943 JUMP (*f
, step2_jumps
);
945 /* Process 'h' modifier. No other modifier is allowed to
949 JUMP (*++f
, step4_jumps
);
951 /* Process 'l' modifier. There might another 'l' follow. */
954 JUMP (*++f
, step3_jumps
);
956 /* Process 'L', 'q', or 'll' modifier. No other modifier is
957 allowed to follow. */
958 LABEL (mod_longlong
):
960 JUMP (*++f
, step4_jumps
);
963 is_longlong
= sizeof (size_t) > sizeof (unsigned long int);
964 is_long
= sizeof (size_t) > sizeof (unsigned int);
965 JUMP (*++f
, step4_jumps
);
968 /* Process current format. */
971 process_arg (((struct printf_spec
*) NULL
));
973 LABEL (form_unknown
):
974 if (spec
== L_('\0'))
975 /* The format string ended before the specifier is complete. */
978 /* If we are in the fast loop force entering the complicated
983 /* Look for next format specifier. */
984 f
= find_spec ((end_of_spec
= ++f
), &mbstate
);
986 /* Write the following constant string. */
987 outstring (end_of_spec
, f
- end_of_spec
);
989 while (*f
!= L_('\0'));
991 /* We processed the whole format without any positional parameters. */
994 /* Here starts the more complex loop to handle positional parameters. */
997 /* Array with information about the needed arguments. This has to
998 be dynamically extendable. */
1000 size_t nspecs_max
= 32; /* A more or less arbitrary start value. */
1001 struct printf_spec
*specs
1002 = alloca (nspecs_max
* sizeof (struct printf_spec
));
1004 /* The number of arguments the format string requests. This will
1005 determine the size of the array needed to store the argument
1009 union printf_arg
*args_value
;
1011 /* Positional parameters refer to arguments directly. This could
1012 also determine the maximum number of arguments. Track the
1014 size_t max_ref_arg
= 0;
1016 /* Just a counter. */
1020 if (grouping
== (const char *) -1)
1022 /* XXX Use wctob. But this is incompatible for now. */
1023 /* Figure out the thousands separator character. */
1024 if (mbtowc (&thousands_sep
,
1025 _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
),
1026 strlen (_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
))) <= 0)
1027 thousands_sep
= (wchar_t) *_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1028 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1029 if (*grouping
== '\0' || *grouping
== CHAR_MAX
1030 || thousands_sep
== L
'\0')
1034 for (f
= lead_str_end
; *f
!= '\0'; f
= specs
[nspecs
++].next_fmt
)
1036 if (nspecs
>= nspecs_max
)
1038 /* Extend the array of format specifiers. */
1039 struct printf_spec
*old
= specs
;
1042 specs
= alloca (nspecs_max
* sizeof (struct printf_spec
));
1044 if (specs
== &old
[nspecs
])
1045 /* Stack grows up, OLD was the last thing allocated;
1047 nspecs_max
+= nspecs_max
/ 2;
1050 /* Copy the old array's elements to the new space. */
1051 memcpy (specs
, old
, nspecs
* sizeof (struct printf_spec
));
1052 if (old
== &specs
[nspecs
])
1053 /* Stack grows down, OLD was just below the new
1054 SPECS. We can use that space when the new space
1056 nspecs_max
+= nspecs_max
/ 2;
1060 /* Parse the format specifier. */
1061 nargs
+= parse_one_spec (f
, nargs
, &specs
[nspecs
], &max_ref_arg
, NULL
);
1064 /* Determine the number of arguments the format string consumes. */
1065 nargs
= MAX (nargs
, max_ref_arg
);
1067 /* Allocate memory for the argument descriptions. */
1068 args_type
= alloca (nargs
* sizeof (int));
1069 memset (args_type
, 0, nargs
* sizeof (int));
1070 args_value
= alloca (nargs
* sizeof (union printf_arg
));
1072 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1073 still zero after this loop, format is invalid. For now we
1074 simply use 0 as the value. */
1076 /* Fill in the types of all the arguments. */
1077 for (cnt
= 0; cnt
< nspecs
; ++cnt
)
1079 /* If the width is determined by an argument this is an int. */
1080 if (specs
[cnt
].width_arg
!= -1)
1081 args_type
[specs
[cnt
].width_arg
] = PA_INT
;
1083 /* If the precision is determined by an argument this is an int. */
1084 if (specs
[cnt
].prec_arg
!= -1)
1085 args_type
[specs
[cnt
].prec_arg
] = PA_INT
;
1087 switch (specs
[cnt
].ndata_args
)
1089 case 0: /* No arguments. */
1091 case 1: /* One argument; we already have the type. */
1092 args_type
[specs
[cnt
].data_arg
] = specs
[cnt
].data_arg_type
;
1095 /* We have more than one argument for this format spec.
1096 We must call the arginfo function again to determine
1098 (void) (*__printf_arginfo_table
[specs
[cnt
].info
.spec
])
1100 specs
[cnt
].ndata_args
, &args_type
[specs
[cnt
].data_arg
]);
1105 /* Now we know all the types and the order. Fill in the argument
1107 for (cnt
= 0, ap
= ap_save
; cnt
< nargs
; ++cnt
)
1108 switch (args_type
[cnt
])
1110 #define T(tag, mem, type) \
1112 args_value[cnt].mem = va_arg (ap, type); \
1115 T (PA_CHAR
, pa_char
, int); /* Promoted. */
1116 T (PA_INT
|PA_FLAG_SHORT
, pa_short_int
, int); /* Promoted. */
1117 T (PA_INT
, pa_int
, int);
1118 T (PA_INT
|PA_FLAG_LONG
, pa_long_int
, long int);
1119 T (PA_INT
|PA_FLAG_LONG_LONG
, pa_long_long_int
, long long int);
1120 T (PA_FLOAT
, pa_float
, double); /* Promoted. */
1121 T (PA_DOUBLE
, pa_double
, double);
1122 T (PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
, pa_long_double
, long double);
1123 T (PA_STRING
, pa_string
, const char *);
1124 T (PA_POINTER
, pa_pointer
, void *);
1127 if ((args_type
[cnt
] & PA_FLAG_PTR
) != 0)
1128 args_value
[cnt
].pa_pointer
= va_arg (ap
, void *);
1130 args_value
[cnt
].pa_long_double
= 0.0;
1134 /* Now walk through all format specifiers and process them. */
1135 for (; nspecs_done
< nspecs
; ++nspecs_done
)
1138 #define REF(Name) &&do2_##Name
1140 #define LABEL(Name) do2_##Name
1146 unsigned long long int longlong
;
1147 unsigned long int word
;
1150 union printf_arg the_arg
;
1151 char *string
; /* Pointer to argument string. */
1153 /* Fill variables from values in struct. */
1154 int alt
= specs
[nspecs_done
].info
.alt
;
1155 int space
= specs
[nspecs_done
].info
.space
;
1156 int left
= specs
[nspecs_done
].info
.left
;
1157 int showsign
= specs
[nspecs_done
].info
.showsign
;
1158 int group
= specs
[nspecs_done
].info
.group
;
1159 int is_long_double
= specs
[nspecs_done
].info
.is_long_double
;
1160 int is_short
= specs
[nspecs_done
].info
.is_short
;
1161 int is_long
= specs
[nspecs_done
].info
.is_long
;
1162 int width
= specs
[nspecs_done
].info
.width
;
1163 int prec
= specs
[nspecs_done
].info
.prec
;
1164 char pad
= specs
[nspecs_done
].info
.pad
;
1165 CHAR_T spec
= specs
[nspecs_done
].info
.spec
;
1167 /* Fill in last information. */
1168 if (specs
[nspecs_done
].width_arg
!= -1)
1170 /* Extract the field width from an argument. */
1171 specs
[nspecs_done
].info
.width
=
1172 args_value
[specs
[nspecs_done
].width_arg
].pa_int
;
1174 if (specs
[nspecs_done
].info
.width
< 0)
1175 /* If the width value is negative left justification is
1176 selected and the value is taken as being positive. */
1178 specs
[nspecs_done
].info
.width
*= -1;
1179 left
= specs
[nspecs_done
].info
.left
= 1;
1181 width
= specs
[nspecs_done
].info
.width
;
1184 if (specs
[nspecs_done
].prec_arg
!= -1)
1186 /* Extract the precision from an argument. */
1187 specs
[nspecs_done
].info
.prec
=
1188 args_value
[specs
[nspecs_done
].prec_arg
].pa_int
;
1190 if (specs
[nspecs_done
].info
.prec
< 0)
1191 /* If the precision is negative the precision is
1193 specs
[nspecs_done
].info
.prec
= -1;
1195 prec
= specs
[nspecs_done
].info
.prec
;
1198 /* Process format specifiers. */
1201 JUMP (spec
, step4_jumps
);
1203 process_arg ((&specs
[nspecs_done
]));
1205 LABEL (form_unknown
):
1207 extern printf_function
**__printf_function_table
;
1209 printf_function
*function
;
1214 (__printf_function_table
== NULL
? NULL
:
1215 __printf_function_table
[specs
[nspecs_done
].info
.spec
]);
1217 if (function
== NULL
)
1218 function
= &printf_unknown
;
1220 ptr
= alloca (specs
[nspecs_done
].ndata_args
1221 * sizeof (const void *));
1223 /* Fill in an array of pointers to the argument values. */
1224 for (i
= 0; i
< specs
[nspecs_done
].ndata_args
; ++i
)
1225 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
1227 /* Call the function. */
1228 function_done
= (*function
) (s
, &specs
[nspecs_done
].info
, ptr
);
1230 /* If an error occured we don't have information about #
1232 if (function_done
< 0)
1235 done
+= function_done
;
1240 /* Write the following constant string. */
1241 outstring (specs
[nspecs_done
].end_of_fmt
,
1242 specs
[nspecs_done
].next_fmt
1243 - specs
[nspecs_done
].end_of_fmt
);
1252 # ifdef strong_alias
1253 /* This is for glibc. */
1254 strong_alias (_IO_vfprintf
, vfprintf
);
1256 # if defined __ELF__ || defined __GNU_LIBRARY__
1257 # include <gnu-stabs.h>
1259 weak_alias (_IO_vfprintf
, vfprintf
);
1265 /* Handle an unknown format specifier. This prints out a canonicalized
1266 representation of the format spec itself. */
1268 printf_unknown (FILE *s
, const struct printf_info
*info
,
1269 const void *const *args
)
1273 char work_buffer
[BUFSIZ
];
1284 else if (info
->space
)
1288 if (info
->pad
== '0')
1291 if (info
->width
!= 0)
1293 w
= _itoa_word (info
->width
, workend
+ 1, 10, 0);
1294 while (++w
<= workend
)
1298 if (info
->prec
!= -1)
1301 w
= _itoa_word (info
->prec
, workend
+ 1, 10, 0);
1302 while (++w
<= workend
)
1306 if (info
->spec
!= '\0')
1307 outchar (info
->spec
);
1312 /* Group the digits according to the grouping rules of the current locale.
1313 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1315 group_number (CHAR_T
*w
, CHAR_T
*rear_ptr
, const CHAR_T
*grouping
,
1316 wchar_t thousands_sep
)
1321 /* We treat all negative values like CHAR_MAX. */
1323 if (*grouping
== CHAR_MAX
|| *grouping
< 0)
1324 /* No grouping should be done. */
1329 /* Copy existing string so that nothing gets overwritten. */
1330 src
= (char *) alloca (rear_ptr
- w
);
1331 memcpy (src
, w
+ 1, rear_ptr
- w
);
1332 s
= &src
[rear_ptr
- w
- 1];
1335 /* Process all characters in the string. */
1340 if (--len
== 0 && s
>= src
)
1342 /* A new group begins. */
1343 *w
-- = thousands_sep
;
1346 if (*grouping
== '\0')
1347 /* The previous grouping repeats ad infinitum. */
1349 else if (*grouping
== CHAR_MAX
|| *grouping
< 0)
1351 /* No further grouping to be done.
1352 Copy the rest of the number. */
1364 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
1367 struct _IO_FILE_plus _f
;
1368 _IO_FILE
*_put_stream
;
1372 _IO_helper_overflow (_IO_FILE
*s
, int c
)
1374 _IO_FILE
*target
= ((struct helper_file
*) s
)->_put_stream
;
1375 int used
= s
->_IO_write_ptr
- s
->_IO_write_base
;
1378 _IO_size_t written
= _IO_sputn (target
, s
->_IO_write_base
, used
);
1379 s
->_IO_write_ptr
-= written
;
1381 return _IO_putc (c
, s
);
1384 static const struct _IO_jump_t _IO_helper_jumps
=
1387 JUMP_INIT (finish
, _IO_default_finish
),
1388 JUMP_INIT (overflow
, _IO_helper_overflow
),
1389 JUMP_INIT (underflow
, _IO_default_underflow
),
1390 JUMP_INIT (uflow
, _IO_default_uflow
),
1391 JUMP_INIT (pbackfail
, _IO_default_pbackfail
),
1392 JUMP_INIT (xsputn
, _IO_default_xsputn
),
1393 JUMP_INIT (xsgetn
, _IO_default_xsgetn
),
1394 JUMP_INIT (seekoff
, _IO_default_seekoff
),
1395 JUMP_INIT (seekpos
, _IO_default_seekpos
),
1396 JUMP_INIT (setbuf
, _IO_default_setbuf
),
1397 JUMP_INIT (sync
, _IO_default_sync
),
1398 JUMP_INIT (doallocate
, _IO_default_doallocate
),
1399 JUMP_INIT (read
, _IO_default_read
),
1400 JUMP_INIT (write
, _IO_default_write
),
1401 JUMP_INIT (seek
, _IO_default_seek
),
1402 JUMP_INIT (close
, _IO_default_close
),
1403 JUMP_INIT (stat
, _IO_default_stat
)
1407 buffered_vfprintf (register _IO_FILE
*s
, const CHAR_T
*format
,
1410 char buf
[_IO_BUFSIZ
];
1411 struct helper_file helper
;
1412 register _IO_FILE
*hp
= (_IO_FILE
*) &helper
;
1413 int result
, to_flush
;
1415 /* Initialize helper. */
1416 helper
._put_stream
= s
;
1417 hp
->_IO_write_base
= buf
;
1418 hp
->_IO_write_ptr
= buf
;
1419 hp
->_IO_write_end
= buf
+ sizeof buf
;
1420 hp
->_IO_file_flags
= _IO_MAGIC
|_IO_NO_READS
;
1421 _IO_JUMPS (hp
) = (struct _IO_jump_t
*) &_IO_helper_jumps
;
1423 /* Now print to helper instead. */
1424 result
= _IO_vfprintf (hp
, format
, args
);
1426 /* Now flush anything from the helper to the S. */
1427 if ((to_flush
= hp
->_IO_write_ptr
- hp
->_IO_write_base
) > 0)
1429 if (_IO_sputn (s
, hp
->_IO_write_base
, to_flush
) != to_flush
)
1436 #else /* !USE_IN_LIBIO */
1439 buffered_vfprintf (register FILE *s
, const CHAR_T
*format
, va_list args
)
1444 s
->__bufp
= s
->__buffer
= buf
;
1445 s
->__bufsize
= sizeof buf
;
1446 s
->__put_limit
= s
->__buffer
+ s
->__bufsize
;
1447 s
->__get_limit
= s
->__buffer
;
1449 /* Now use buffer to print. */
1450 result
= vfprintf (s
, format
, args
);
1452 if (fflush (s
) == EOF
)
1454 s
->__buffer
= s
->__bufp
= s
->__get_limit
= s
->__put_limit
= NULL
;
1460 /* Pads string with given number of a specified character.
1461 This code is taken from iopadn.c of the GNU I/O library. */
1463 static const CHAR_T blanks
[PADSIZE
] =
1464 { L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '),
1465 L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' ') };
1466 static const CHAR_T zeroes
[PADSIZE
] =
1467 { L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'),
1468 L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0') };
1471 #ifndef COMPILE_WPRINTF
1472 __printf_pad (FILE *s
, char pad
, size_t count
)
1474 __wprintf_pad (FILE *s
, wchar_t pad
, size_t count
)
1477 const CHAR_T
*padptr
;
1480 padptr
= pad
== L_(' ') ? blanks
: zeroes
;
1482 for (i
= count
; i
>= PADSIZE
; i
-= PADSIZE
)
1483 if (PUT (s
, padptr
, PADSIZE
) != PADSIZE
)
1486 if (PUT (s
, padptr
, i
) != i
)
1492 #endif /* USE_IN_LIBIO */