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. */
26 #include <libc-lock.h>
28 #include "../locale/localeinfo.h"
30 /* This code is shared between the standard stdio implementation found
31 in GNU C library and the libio implementation originally found in
34 Beside this it is also shared between the normal and wide character
35 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
37 #ifndef COMPILE_WPRINTF
39 # define UCHAR_T unsigned char
42 # define ISDIGIT(Ch) isdigit (Ch)
45 # define PUT(F, S, N) _IO_sputn (F, S, N)
46 # define PAD(Padchar) \
48 done += _IO_padn (s, Padchar, width)
50 # define PUTC(C, F) putc (C, F)
51 ssize_t __printf_pad
__P ((FILE *, char pad
, size_t n
));
52 # define PAD(Padchar) \
54 { if (__printf_pad (s, Padchar, width) == -1) \
55 return -1; else done += width; }
58 # define vfprintf vfwprintf
59 # define CHAR_T wchar_t
60 # define UCHAR_T uwchar_t
62 # define L_(Str) L##Str
63 # define ISDIGIT(Ch) iswdigit (Ch)
66 # define PUT(F, S, N) _IO_sputn (F, S, N)
67 # define PAD(Padchar) \
69 done += _IO_wpadn (s, Padchar, width)
71 # define PUTC(C, F) wputc (C, F)
72 ssize_t __wprintf_pad
__P ((FILE *, wchar_t pad
, size_t n
));
73 # define PAD(Padchar) \
75 { if (__wprintf_pad (s, Padchar, width) == -1) \
76 return -1; else done += width; }
80 /* Include the shared code for parsing the format string. */
81 #include "printf-parse.h"
85 /* This code is for use in libio. */
87 # define PUTC(C, F) _IO_putc_unlocked (C, F)
88 # define vfprintf _IO_vfprintf
89 # define FILE _IO_FILE
91 # define va_list _IO_va_list
93 # define BUFSIZ _IO_BUFSIZ
94 # define ARGCHECK(S, Format) \
97 /* Check file argument for consistence. */ \
99 if (S->_flags & _IO_NO_WRITES || Format == NULL) \
105 # define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
106 #else /* ! USE_IN_LIBIO */
107 /* This code is for use in the GNU C library. */
109 # define PUT(F, S, N) fwrite (S, 1, N, F)
110 # define ARGCHECK(S, Format) \
113 /* Check file argument for consistence. */ \
114 if (!__validfp(S) || !S->__mode.__write || Format == NULL) \
116 __set_errno (EINVAL); \
121 if (__flshfp (S, EOF) == EOF) \
126 # define UNBUFFERED_P(s) ((s)->__buffer == NULL)
127 #endif /* USE_IN_LIBIO */
130 #define outchar(Ch) \
133 register const int outc = (Ch); \
134 if (PUTC (outc, s) == EOF) \
141 #define outstring(String, Len) \
144 if (PUT (s, String, Len) != Len) \
150 /* For handling long_double and longlong we use the same flag. */
152 # define is_longlong is_long_double
155 extern void __flockfile (FILE *);
156 weak_extern (__flockfile
);
157 extern void __funlockfile (FILE *);
159 /* Global variables. */
160 static const char null
[] = "(null)";
163 /* Helper function to provide temporary buffering for unbuffered streams. */
164 static int buffered_vfprintf
__P ((FILE *stream
, const CHAR_T
*fmt
, va_list));
166 /* Handle unknown format specifier. */
167 static int printf_unknown
__P ((FILE *, const struct printf_info
*,
168 const void *const *));
170 /* Group digits of number string. */
171 static char *group_number
__P ((CHAR_T
*, CHAR_T
*, const CHAR_T
*, wchar_t));
174 /* The function itself. */
176 vfprintf (FILE *s
, const CHAR_T
*format
, va_list ap
)
178 /* The character used as thousands separator. */
179 wchar_t thousands_sep
;
181 /* The string describing the size of groups of digits. */
182 const char *grouping
;
184 /* Place to accumulate the result. */
187 /* Current character in format string. */
190 /* End of leading constant string. */
191 const UCHAR_T
*lead_str_end
;
193 /* Points to next format specifier. */
194 const UCHAR_T
*end_of_spec
;
196 /* Buffer intermediate results. */
197 char work_buffer
[1000];
198 #define workend (&work_buffer[sizeof (work_buffer) - 1])
200 /* State for restartable multibyte character handling functions. */
203 /* We have to save the original argument pointer. */
206 /* Count number of specifiers we already processed. */
210 /* This table maps a character into a number representing a
211 class. In each step there is a destination label for each
213 static const int jump_table
[] =
215 /* ' ' */ 1, 0, 0, /* '#' */ 4,
216 0, /* '%' */ 14, 0, /* '\''*/ 6,
217 0, 0, /* '*' */ 7, /* '+' */ 2,
218 0, /* '-' */ 3, /* '.' */ 9, 0,
219 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
220 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
221 /* '8' */ 8, /* '9' */ 8, 0, 0,
224 0, /* 'E' */ 19, 0, /* 'G' */ 19,
226 /* 'L' */ 12, 0, 0, 0,
229 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
231 0, 0, 0, /* 'c' */ 20,
232 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
233 /* 'h' */ 10, /* 'i' */ 15, 0, 0,
234 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
235 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
236 0, /* 'u' */ 16, 0, 0,
240 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < ' ' || (Ch) > 'x')
241 #define CHAR_CLASS(Ch) (jump_table[(int) (Ch) - ' '])
242 #define JUMP(ChExpr, table) \
247 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
248 : table[CHAR_CLASS (spec)]; \
253 #define STEP0_3_TABLE \
254 /* Step 0: at the beginning. */ \
255 static const void *step0_jumps[25] = \
257 REF (form_unknown), \
258 REF (flag_space), /* for ' ' */ \
259 REF (flag_plus), /* for '+' */ \
260 REF (flag_minus), /* for '-' */ \
261 REF (flag_hash), /* for '<hash>' */ \
262 REF (flag_zero), /* for '0' */ \
263 REF (flag_quote), /* for '\'' */ \
264 REF (width_asterics), /* for '*' */ \
265 REF (width), /* for '1'...'9' */ \
266 REF (precision), /* for '.' */ \
267 REF (mod_half), /* for 'h' */ \
268 REF (mod_long), /* for 'l' */ \
269 REF (mod_longlong), /* for 'L', 'q' */ \
270 REF (mod_size_t), /* for 'Z' */ \
271 REF (form_percent), /* for '%' */ \
272 REF (form_integer), /* for 'd', 'i' */ \
273 REF (form_unsigned), /* for 'u' */ \
274 REF (form_octal), /* for 'o' */ \
275 REF (form_hexa), /* for 'X', 'x' */ \
276 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
277 REF (form_character), /* for 'c' */ \
278 REF (form_string), /* for 's' */ \
279 REF (form_pointer), /* for 'p' */ \
280 REF (form_number), /* for 'n' */ \
281 REF (form_strerror) /* for 'm' */ \
283 /* Step 1: after processing width. */ \
284 static const void *step1_jumps[25] = \
286 REF (form_unknown), \
287 REF (form_unknown), /* for ' ' */ \
288 REF (form_unknown), /* for '+' */ \
289 REF (form_unknown), /* for '-' */ \
290 REF (form_unknown), /* for '<hash>' */ \
291 REF (form_unknown), /* for '0' */ \
292 REF (form_unknown), /* for '\'' */ \
293 REF (form_unknown), /* for '*' */ \
294 REF (form_unknown), /* for '1'...'9' */ \
295 REF (precision), /* for '.' */ \
296 REF (mod_half), /* for 'h' */ \
297 REF (mod_long), /* for 'l' */ \
298 REF (mod_longlong), /* for 'L', 'q' */ \
299 REF (mod_size_t), /* for 'Z' */ \
300 REF (form_percent), /* for '%' */ \
301 REF (form_integer), /* for 'd', 'i' */ \
302 REF (form_unsigned), /* for 'u' */ \
303 REF (form_octal), /* for 'o' */ \
304 REF (form_hexa), /* for 'X', 'x' */ \
305 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
306 REF (form_character), /* for 'c' */ \
307 REF (form_string), /* for 's' */ \
308 REF (form_pointer), /* for 'p' */ \
309 REF (form_number), /* for 'n' */ \
310 REF (form_strerror) /* for 'm' */ \
312 /* Step 2: after processing precision. */ \
313 static const void *step2_jumps[25] = \
315 REF (form_unknown), \
316 REF (form_unknown), /* for ' ' */ \
317 REF (form_unknown), /* for '+' */ \
318 REF (form_unknown), /* for '-' */ \
319 REF (form_unknown), /* for '<hash>' */ \
320 REF (form_unknown), /* for '0' */ \
321 REF (form_unknown), /* for '\'' */ \
322 REF (form_unknown), /* for '*' */ \
323 REF (form_unknown), /* for '1'...'9' */ \
324 REF (form_unknown), /* for '.' */ \
325 REF (mod_half), /* for 'h' */ \
326 REF (mod_long), /* for 'l' */ \
327 REF (mod_longlong), /* for 'L', 'q' */ \
328 REF (mod_size_t), /* for 'Z' */ \
329 REF (form_percent), /* for '%' */ \
330 REF (form_integer), /* for 'd', 'i' */ \
331 REF (form_unsigned), /* for 'u' */ \
332 REF (form_octal), /* for 'o' */ \
333 REF (form_hexa), /* for 'X', 'x' */ \
334 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
335 REF (form_character), /* for 'c' */ \
336 REF (form_string), /* for 's' */ \
337 REF (form_pointer), /* for 'p' */ \
338 REF (form_number), /* for 'n' */ \
339 REF (form_strerror) /* for 'm' */ \
341 /* Step 3: after processing first 'l' modifier. */ \
342 static const void *step3_jumps[25] = \
344 REF (form_unknown), \
345 REF (form_unknown), /* for ' ' */ \
346 REF (form_unknown), /* for '+' */ \
347 REF (form_unknown), /* for '-' */ \
348 REF (form_unknown), /* for '<hash>' */ \
349 REF (form_unknown), /* for '0' */ \
350 REF (form_unknown), /* for '\'' */ \
351 REF (form_unknown), /* for '*' */ \
352 REF (form_unknown), /* for '1'...'9' */ \
353 REF (form_unknown), /* for '.' */ \
354 REF (form_unknown), /* for 'h' */ \
355 REF (mod_longlong), /* for 'l' */ \
356 REF (form_unknown), /* for 'L', 'q' */ \
357 REF (form_unknown), /* for 'Z' */ \
358 REF (form_percent), /* for '%' */ \
359 REF (form_integer), /* for 'd', 'i' */ \
360 REF (form_unsigned), /* for 'u' */ \
361 REF (form_octal), /* for 'o' */ \
362 REF (form_hexa), /* for 'X', 'x' */ \
363 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
364 REF (form_character), /* for 'c' */ \
365 REF (form_string), /* for 's' */ \
366 REF (form_pointer), /* for 'p' */ \
367 REF (form_number), /* for 'n' */ \
368 REF (form_strerror) /* for 'm' */ \
371 #define STEP4_TABLE \
372 /* Step 4: processing format specifier. */ \
373 static const void *step4_jumps[25] = \
375 REF (form_unknown), \
376 REF (form_unknown), /* for ' ' */ \
377 REF (form_unknown), /* for '+' */ \
378 REF (form_unknown), /* for '-' */ \
379 REF (form_unknown), /* for '<hash>' */ \
380 REF (form_unknown), /* for '0' */ \
381 REF (form_unknown), /* for '\'' */ \
382 REF (form_unknown), /* for '*' */ \
383 REF (form_unknown), /* for '1'...'9' */ \
384 REF (form_unknown), /* for '.' */ \
385 REF (form_unknown), /* for 'h' */ \
386 REF (form_unknown), /* for 'l' */ \
387 REF (form_unknown), /* for 'L', 'q' */ \
388 REF (form_unknown), /* for 'Z' */ \
389 REF (form_percent), /* for '%' */ \
390 REF (form_integer), /* for 'd', 'i' */ \
391 REF (form_unsigned), /* for 'u' */ \
392 REF (form_octal), /* for 'o' */ \
393 REF (form_hexa), /* for 'X', 'x' */ \
394 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
395 REF (form_character), /* for 'c' */ \
396 REF (form_string), /* for 's' */ \
397 REF (form_pointer), /* for 'p' */ \
398 REF (form_number), /* for 'n' */ \
399 REF (form_strerror) /* for 'm' */ \
403 #define process_arg(fspec) \
404 /* Start real work. We know about all flags and modifiers and \
405 now process the wanted format specifier. */ \
406 LABEL (form_percent): \
407 /* Write a literal "%". */ \
411 LABEL (form_integer): \
412 /* Signed decimal integer. */ \
417 long long int signed_number; \
420 signed_number = va_arg (ap, long long int); \
422 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
424 is_negative = signed_number < 0; \
425 number.longlong = is_negative ? (- signed_number) : signed_number; \
427 goto LABEL (longlong_number); \
431 long int signed_number; \
435 signed_number = va_arg (ap, long int); \
436 else /* `short int' will be promoted to `int'. */ \
437 signed_number = va_arg (ap, int); \
440 signed_number = args_value[fspec->data_arg].pa_long_int; \
442 signed_number = args_value[fspec->data_arg].pa_int; \
444 is_negative = signed_number < 0; \
445 number.word = is_negative ? (- signed_number) : signed_number; \
447 goto LABEL (number); \
451 LABEL (form_unsigned): \
452 /* Unsigned decimal integer. */ \
454 goto LABEL (unsigned_number); \
457 LABEL (form_octal): \
458 /* Unsigned octal integer. */ \
460 goto LABEL (unsigned_number); \
464 /* Unsigned hexadecimal integer. */ \
467 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
469 /* ANSI specifies the `+' and ` ' flags only for signed \
478 number.longlong = va_arg (ap, unsigned long long int); \
480 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
482 LABEL (longlong_number): \
484 /* Supply a default precision if none was given. */ \
487 /* We have to take care for the '0' flag. If a precision \
488 is given it must be ignored. */ \
491 /* If the precision is 0 and the number is 0 nothing has to \
492 be written for the number. */ \
493 if (prec == 0 && number.longlong == 0) \
497 /* Put the number in WORK. */ \
498 string = _itoa (number.longlong, workend + 1, base, \
501 if (group && grouping) \
502 string = group_number (string, workend, grouping, \
505 /* Simply further test for num != 0. */ \
506 number.word = number.longlong != 0; \
512 number.word = va_arg (ap, unsigned long int); \
513 else if (!is_short) \
514 number.word = va_arg (ap, unsigned int); \
516 number.word = (unsigned short int) va_arg (ap, unsigned int); \
519 number.word = args_value[fspec->data_arg].pa_u_long_int; \
520 else if (!is_short) \
521 number.word = args_value[fspec->data_arg].pa_u_int; \
523 number.word = (unsigned short int) \
524 args_value[fspec->data_arg].pa_u_short_int; \
528 /* Supply a default precision if none was given. */ \
531 /* We have to take care for the '0' flag. If a precision \
532 is given it must be ignored. */ \
535 /* If the precision is 0 and the number is 0 nothing has to \
536 be written for the number. */ \
537 if (prec == 0 && number.word == 0) \
541 /* Put the number in WORK. */ \
542 string = _itoa_word (number.word, workend + 1, base, \
545 if (group && grouping) \
546 string = group_number (string, workend, grouping, \
551 prec -= workend - string; \
554 /* Add zeros to the precision. */ \
557 else if (number.word != 0 && alt && base == 8) \
558 /* Add octal marker. */ \
563 width -= workend - string; \
565 if (number.word != 0 && alt && base == 16) \
566 /* Account for 0X hex marker. */ \
569 if (is_negative || showsign || space) \
574 while (width-- > 0) \
577 if (number.word != 0 && alt && base == 16) \
592 if (number.word != 0 && alt && base == 16) \
605 while (width-- > 0) \
609 outstring (string + 1, workend - string); \
615 if (number.word != 0 && alt && base == 16) \
628 width -= workend - string; \
629 outstring (string + 1, workend - string); \
635 LABEL (form_float): \
637 /* Floating-point number. This is handled by printf_fp.c. */ \
638 extern int __printf_fp __P ((FILE *, const struct printf_info *, \
639 const void **const)); \
645 struct printf_info info = { prec: prec, \
648 is_long_double: is_long_double, \
649 is_short: is_short, \
654 showsign: showsign, \
659 if (is_long_double) \
660 the_arg.pa_long_double = va_arg (ap, long double); \
662 the_arg.pa_double = va_arg (ap, double); \
663 ptr = (const void *) &the_arg; \
665 function_done = __printf_fp (s, &info, &ptr); \
669 ptr = (const void *) &args_value[fspec->data_arg]; \
671 function_done = __printf_fp (s, &fspec->info, &ptr); \
674 if (function_done < 0) \
675 /* Error in print handler. */ \
678 done += function_done; \
682 LABEL (form_character): \
684 --width; /* Account for the character itself. */ \
688 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
690 outchar ((unsigned char) args_value[fspec->data_arg].pa_char); \
695 LABEL (form_string): \
699 /* The string argument could in fact be `char *' or `wchar_t *'. \
700 But this should not make a difference here. */ \
702 string = (char *) va_arg (ap, const char *); \
704 string = (char *) args_value[fspec->data_arg].pa_string; \
706 /* Entry point for printing other strings. */ \
707 LABEL (print_string): \
709 if (string == NULL) \
711 /* Write "(null)" if there's space. */ \
712 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
714 string = (char *) null; \
715 len = sizeof (null) - 1; \
719 string = (char *) ""; \
727 /* Search for the end of the string, but don't search past \
728 the length specified by the precision. */ \
729 const char *end = memchr (string, '\0', prec); \
731 len = end - string; \
736 len = strlen (string); \
740 const wchar_t *s2 = (const wchar_t *) string; \
743 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
744 if (len == (size_t) -1) \
745 /* Illegal wide-character string. */ \
748 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;
848 /* Find the first format specifier. */
849 f
= lead_str_end
= find_spec (format
, &mbstate
);
852 __libc_cleanup_region_start ((void (*) (void *)) &__funlockfile
, s
);
854 if (__flockfile
!= NULL
)
857 /* Write the literal text before the first format. */
858 outstring ((const UCHAR_T
*) format
,
859 lead_str_end
- (const UCHAR_T
*) format
);
861 /* If we only have to print a simple string, return now. */
865 /* Process whole format string. */
868 #define REF(Name) &&do_##Name
869 #define LABEL(Name) do_##Name
873 union printf_arg
*args_value
; /* This is not used here but ... */
874 int is_negative
; /* Flag for negative number. */
877 unsigned long long int longlong
;
878 unsigned long int word
;
881 union printf_arg the_arg
;
882 char *string
; /* Pointer to argument string. */
883 int alt
= 0; /* Alternate format. */
884 int space
= 0; /* Use space prefix if no sign is needed. */
885 int left
= 0; /* Left-justify output. */
886 int showsign
= 0; /* Always begin with plus or minus sign. */
887 int group
= 0; /* Print numbers according grouping rules. */
888 int is_long_double
= 0; /* Argument is long double/ long long int. */
889 int is_short
= 0; /* Argument is long int. */
890 int is_long
= 0; /* Argument is short int. */
891 int width
= 0; /* Width of output; 0 means none specified. */
892 int prec
= -1; /* Precision of output; -1 means none specified. */
893 char pad
= ' '; /* Padding character. */
896 /* Get current character in format string. */
897 JUMP (*++f
, step0_jumps
);
902 JUMP (*++f
, step0_jumps
);
907 JUMP (*++f
, step0_jumps
);
913 JUMP (*++f
, step0_jumps
);
918 JUMP (*++f
, step0_jumps
);
924 JUMP (*++f
, step0_jumps
);
930 /* XXX Completely wrong. Use wctob. */
931 if (grouping
== (const char *) -1)
933 /* Figure out the thousands separator character. */
934 if (mbtowc (&thousands_sep
,
935 _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
),
936 strlen (_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
))) <= 0)
937 thousands_sep
= (wchar_t)
938 *_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
939 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
940 if (*grouping
== '\0' || *grouping
== CHAR_MAX
941 || thousands_sep
== L
'\0')
944 JUMP (*++f
, step0_jumps
);
946 /* Get width from argument. */
947 LABEL (width_asterics
):
949 const UCHAR_T
*tmp
; /* Temporary value. */
952 if (ISDIGIT (*tmp
) && read_int (&tmp
) && *tmp
== L_('$'))
953 /* The width comes from a positional parameter. */
956 width
= va_arg (ap
, int);
958 /* Negative width means left justified. */
966 JUMP (*f
, step1_jumps
);
968 /* Given width in format string. */
970 width
= read_int (&f
);
972 /* Oh, oh. The argument comes from a positional parameter. */
974 JUMP (*f
, step1_jumps
);
980 const UCHAR_T
*tmp
; /* Temporary value. */
983 if (ISDIGIT (*tmp
) && read_int (&tmp
) > 0 && *tmp
== L_('$'))
984 /* The precision comes from a positional parameter. */
987 prec
= va_arg (ap
, int);
989 /* If the precision is negative the precision is omitted. */
993 else if (ISDIGIT (*f
))
994 prec
= read_int (&f
);
997 JUMP (*f
, step2_jumps
);
999 /* Process 'h' modifier. No other modifier is allowed to
1003 JUMP (*++f
, step4_jumps
);
1005 /* Process 'l' modifier. There might another 'l' follow. */
1008 JUMP (*++f
, step3_jumps
);
1010 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1011 allowed to follow. */
1012 LABEL (mod_longlong
):
1014 JUMP (*++f
, step4_jumps
);
1017 is_longlong
= sizeof (size_t) > sizeof (unsigned long int);
1018 is_long
= sizeof (size_t) > sizeof (unsigned int);
1019 JUMP (*++f
, step4_jumps
);
1022 /* Process current format. */
1025 process_arg (((struct printf_spec
*) NULL
));
1027 LABEL (form_unknown
):
1028 if (spec
== L_('\0'))
1030 /* The format string ended before the specifier is complete. */
1035 /* If we are in the fast loop force entering the complicated
1040 /* Look for next format specifier. */
1041 f
= find_spec ((end_of_spec
= ++f
), &mbstate
);
1043 /* Write the following constant string. */
1044 outstring (end_of_spec
, f
- end_of_spec
);
1046 while (*f
!= L_('\0'));
1048 /* Unlock stream and return. */
1051 /* Here starts the more complex loop to handle positional parameters. */
1054 /* Array with information about the needed arguments. This has to
1055 be dynamically extendable. */
1057 size_t nspecs_max
= 32; /* A more or less arbitrary start value. */
1058 struct printf_spec
*specs
1059 = alloca (nspecs_max
* sizeof (struct printf_spec
));
1061 /* The number of arguments the format string requests. This will
1062 determine the size of the array needed to store the argument
1066 union printf_arg
*args_value
;
1068 /* Positional parameters refer to arguments directly. This could
1069 also determine the maximum number of arguments. Track the
1071 size_t max_ref_arg
= 0;
1073 /* Just a counter. */
1077 if (grouping
== (const char *) -1)
1079 /* XXX Use wctob. But this is incompatible for now. */
1080 /* Figure out the thousands separator character. */
1081 if (mbtowc (&thousands_sep
,
1082 _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
),
1083 strlen (_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
))) <= 0)
1084 thousands_sep
= (wchar_t) *_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1085 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1086 if (*grouping
== '\0' || *grouping
== CHAR_MAX
1087 || thousands_sep
== L
'\0')
1091 for (f
= lead_str_end
; *f
!= '\0'; f
= specs
[nspecs
++].next_fmt
)
1093 if (nspecs
>= nspecs_max
)
1095 /* Extend the array of format specifiers. */
1096 struct printf_spec
*old
= specs
;
1099 specs
= alloca (nspecs_max
* sizeof (struct printf_spec
));
1101 if (specs
== &old
[nspecs
])
1102 /* Stack grows up, OLD was the last thing allocated;
1104 nspecs_max
+= nspecs_max
/ 2;
1107 /* Copy the old array's elements to the new space. */
1108 memcpy (specs
, old
, nspecs
* sizeof (struct printf_spec
));
1109 if (old
== &specs
[nspecs
])
1110 /* Stack grows down, OLD was just below the new
1111 SPECS. We can use that space when the new space
1113 nspecs_max
+= nspecs_max
/ 2;
1117 /* Parse the format specifier. */
1118 nargs
+= parse_one_spec (f
, nargs
, &specs
[nspecs
], &max_ref_arg
,
1122 /* Determine the number of arguments the format string consumes. */
1123 nargs
= MAX (nargs
, max_ref_arg
);
1125 /* Allocate memory for the argument descriptions. */
1126 args_type
= alloca (nargs
* sizeof (int));
1127 memset (args_type
, 0, nargs
* sizeof (int));
1128 args_value
= alloca (nargs
* sizeof (union printf_arg
));
1130 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1131 still zero after this loop, format is invalid. For now we
1132 simply use 0 as the value. */
1134 /* Fill in the types of all the arguments. */
1135 for (cnt
= 0; cnt
< nspecs
; ++cnt
)
1137 /* If the width is determined by an argument this is an int. */
1138 if (specs
[cnt
].width_arg
!= -1)
1139 args_type
[specs
[cnt
].width_arg
] = PA_INT
;
1141 /* If the precision is determined by an argument this is an int. */
1142 if (specs
[cnt
].prec_arg
!= -1)
1143 args_type
[specs
[cnt
].prec_arg
] = PA_INT
;
1145 switch (specs
[cnt
].ndata_args
)
1147 case 0: /* No arguments. */
1149 case 1: /* One argument; we already have the type. */
1150 args_type
[specs
[cnt
].data_arg
] = specs
[cnt
].data_arg_type
;
1153 /* We have more than one argument for this format spec.
1154 We must call the arginfo function again to determine
1156 (void) (*__printf_arginfo_table
[specs
[cnt
].info
.spec
])
1158 specs
[cnt
].ndata_args
, &args_type
[specs
[cnt
].data_arg
]);
1163 /* Now we know all the types and the order. Fill in the argument
1165 for (cnt
= 0, ap
= ap_save
; cnt
< nargs
; ++cnt
)
1166 switch (args_type
[cnt
])
1168 #define T(tag, mem, type) \
1170 args_value[cnt].mem = va_arg (ap, type); \
1173 T (PA_CHAR
, pa_char
, int); /* Promoted. */
1174 T (PA_INT
|PA_FLAG_SHORT
, pa_short_int
, int); /* Promoted. */
1175 T (PA_INT
, pa_int
, int);
1176 T (PA_INT
|PA_FLAG_LONG
, pa_long_int
, long int);
1177 T (PA_INT
|PA_FLAG_LONG_LONG
, pa_long_long_int
, long long int);
1178 T (PA_FLOAT
, pa_float
, double); /* Promoted. */
1179 T (PA_DOUBLE
, pa_double
, double);
1180 T (PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
, pa_long_double
, long double);
1181 T (PA_STRING
, pa_string
, const char *);
1182 T (PA_POINTER
, pa_pointer
, void *);
1185 if ((args_type
[cnt
] & PA_FLAG_PTR
) != 0)
1186 args_value
[cnt
].pa_pointer
= va_arg (ap
, void *);
1188 args_value
[cnt
].pa_long_double
= 0.0;
1192 /* Now walk through all format specifiers and process them. */
1193 for (; nspecs_done
< nspecs
; ++nspecs_done
)
1196 #define REF(Name) &&do2_##Name
1198 #define LABEL(Name) do2_##Name
1204 unsigned long long int longlong
;
1205 unsigned long int word
;
1208 union printf_arg the_arg
;
1209 char *string
; /* Pointer to argument string. */
1211 /* Fill variables from values in struct. */
1212 int alt
= specs
[nspecs_done
].info
.alt
;
1213 int space
= specs
[nspecs_done
].info
.space
;
1214 int left
= specs
[nspecs_done
].info
.left
;
1215 int showsign
= specs
[nspecs_done
].info
.showsign
;
1216 int group
= specs
[nspecs_done
].info
.group
;
1217 int is_long_double
= specs
[nspecs_done
].info
.is_long_double
;
1218 int is_short
= specs
[nspecs_done
].info
.is_short
;
1219 int is_long
= specs
[nspecs_done
].info
.is_long
;
1220 int width
= specs
[nspecs_done
].info
.width
;
1221 int prec
= specs
[nspecs_done
].info
.prec
;
1222 char pad
= specs
[nspecs_done
].info
.pad
;
1223 CHAR_T spec
= specs
[nspecs_done
].info
.spec
;
1225 /* Fill in last information. */
1226 if (specs
[nspecs_done
].width_arg
!= -1)
1228 /* Extract the field width from an argument. */
1229 specs
[nspecs_done
].info
.width
=
1230 args_value
[specs
[nspecs_done
].width_arg
].pa_int
;
1232 if (specs
[nspecs_done
].info
.width
< 0)
1233 /* If the width value is negative left justification is
1234 selected and the value is taken as being positive. */
1236 specs
[nspecs_done
].info
.width
*= -1;
1237 left
= specs
[nspecs_done
].info
.left
= 1;
1239 width
= specs
[nspecs_done
].info
.width
;
1242 if (specs
[nspecs_done
].prec_arg
!= -1)
1244 /* Extract the precision from an argument. */
1245 specs
[nspecs_done
].info
.prec
=
1246 args_value
[specs
[nspecs_done
].prec_arg
].pa_int
;
1248 if (specs
[nspecs_done
].info
.prec
< 0)
1249 /* If the precision is negative the precision is
1251 specs
[nspecs_done
].info
.prec
= -1;
1253 prec
= specs
[nspecs_done
].info
.prec
;
1256 /* Process format specifiers. */
1259 JUMP (spec
, step4_jumps
);
1261 process_arg ((&specs
[nspecs_done
]));
1263 LABEL (form_unknown
):
1265 extern printf_function
**__printf_function_table
;
1267 printf_function
*function
;
1272 (__printf_function_table
== NULL
? NULL
:
1273 __printf_function_table
[specs
[nspecs_done
].info
.spec
]);
1275 if (function
== NULL
)
1276 function
= &printf_unknown
;
1278 ptr
= alloca (specs
[nspecs_done
].ndata_args
1279 * sizeof (const void *));
1281 /* Fill in an array of pointers to the argument values. */
1282 for (i
= 0; i
< specs
[nspecs_done
].ndata_args
; ++i
)
1283 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
1285 /* Call the function. */
1286 function_done
= (*function
) (s
, &specs
[nspecs_done
].info
, ptr
);
1288 /* If an error occured we don't have information about #
1290 if (function_done
< 0)
1296 done
+= function_done
;
1301 /* Write the following constant string. */
1302 outstring (specs
[nspecs_done
].end_of_fmt
,
1303 specs
[nspecs_done
].next_fmt
1304 - specs
[nspecs_done
].end_of_fmt
);
1309 /* Unlock the stream. */
1310 __libc_cleanup_region_end (1);
1317 # ifdef strong_alias
1318 /* This is for glibc. */
1319 strong_alias (_IO_vfprintf
, vfprintf
);
1321 # if defined __ELF__ || defined __GNU_LIBRARY__
1322 # include <gnu-stabs.h>
1324 weak_alias (_IO_vfprintf
, vfprintf
);
1330 /* Handle an unknown format specifier. This prints out a canonicalized
1331 representation of the format spec itself. */
1333 printf_unknown (FILE *s
, const struct printf_info
*info
,
1334 const void *const *args
)
1338 char work_buffer
[BUFSIZ
];
1349 else if (info
->space
)
1353 if (info
->pad
== '0')
1356 if (info
->width
!= 0)
1358 w
= _itoa_word (info
->width
, workend
+ 1, 10, 0);
1359 while (++w
<= workend
)
1363 if (info
->prec
!= -1)
1366 w
= _itoa_word (info
->prec
, workend
+ 1, 10, 0);
1367 while (++w
<= workend
)
1371 if (info
->spec
!= '\0')
1372 outchar (info
->spec
);
1377 /* Group the digits according to the grouping rules of the current locale.
1378 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1380 group_number (CHAR_T
*w
, CHAR_T
*rear_ptr
, const CHAR_T
*grouping
,
1381 wchar_t thousands_sep
)
1386 /* We treat all negative values like CHAR_MAX. */
1388 if (*grouping
== CHAR_MAX
|| *grouping
< 0)
1389 /* No grouping should be done. */
1394 /* Copy existing string so that nothing gets overwritten. */
1395 src
= (char *) alloca (rear_ptr
- w
);
1396 memcpy (src
, w
+ 1, rear_ptr
- w
);
1397 s
= &src
[rear_ptr
- w
- 1];
1400 /* Process all characters in the string. */
1405 if (--len
== 0 && s
>= src
)
1407 /* A new group begins. */
1408 *w
-- = thousands_sep
;
1411 if (*grouping
== '\0')
1412 /* The previous grouping repeats ad infinitum. */
1414 else if (*grouping
== CHAR_MAX
|| *grouping
< 0)
1416 /* No further grouping to be done.
1417 Copy the rest of the number. */
1429 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
1432 struct _IO_FILE_plus _f
;
1433 _IO_FILE
*_put_stream
;
1434 #ifdef _IO_MTSAFE_IO
1440 _IO_helper_overflow (_IO_FILE
*s
, int c
)
1442 _IO_FILE
*target
= ((struct helper_file
*) s
)->_put_stream
;
1443 int used
= s
->_IO_write_ptr
- s
->_IO_write_base
;
1446 _IO_size_t written
= _IO_sputn (target
, s
->_IO_write_base
, used
);
1447 s
->_IO_write_ptr
-= written
;
1452 static const struct _IO_jump_t _IO_helper_jumps
=
1455 JUMP_INIT (finish
, _IO_default_finish
),
1456 JUMP_INIT (overflow
, _IO_helper_overflow
),
1457 JUMP_INIT (underflow
, _IO_default_underflow
),
1458 JUMP_INIT (uflow
, _IO_default_uflow
),
1459 JUMP_INIT (pbackfail
, _IO_default_pbackfail
),
1460 JUMP_INIT (xsputn
, _IO_default_xsputn
),
1461 JUMP_INIT (xsgetn
, _IO_default_xsgetn
),
1462 JUMP_INIT (seekoff
, _IO_default_seekoff
),
1463 JUMP_INIT (seekpos
, _IO_default_seekpos
),
1464 JUMP_INIT (setbuf
, _IO_default_setbuf
),
1465 JUMP_INIT (sync
, _IO_default_sync
),
1466 JUMP_INIT (doallocate
, _IO_default_doallocate
),
1467 JUMP_INIT (read
, _IO_default_read
),
1468 JUMP_INIT (write
, _IO_default_write
),
1469 JUMP_INIT (seek
, _IO_default_seek
),
1470 JUMP_INIT (close
, _IO_default_close
),
1471 JUMP_INIT (stat
, _IO_default_stat
)
1475 buffered_vfprintf (register _IO_FILE
*s
, const CHAR_T
*format
,
1478 char buf
[_IO_BUFSIZ
];
1479 struct helper_file helper
;
1480 register _IO_FILE
*hp
= (_IO_FILE
*) &helper
;
1481 int result
, to_flush
;
1483 /* Initialize helper. */
1484 helper
._put_stream
= s
;
1485 hp
->_IO_write_base
= buf
;
1486 hp
->_IO_write_ptr
= buf
;
1487 hp
->_IO_write_end
= buf
+ sizeof buf
;
1488 hp
->_IO_file_flags
= _IO_MAGIC
|_IO_NO_READS
;
1489 #ifdef _IO_MTSAFE_IO
1490 hp
->_lock
= &helper
.lock
;
1492 _IO_JUMPS (hp
) = (struct _IO_jump_t
*) &_IO_helper_jumps
;
1494 /* Now print to helper instead. */
1495 result
= _IO_vfprintf (hp
, format
, args
);
1497 /* Now flush anything from the helper to the S. */
1498 if ((to_flush
= hp
->_IO_write_ptr
- hp
->_IO_write_base
) > 0)
1500 if (_IO_sputn (s
, hp
->_IO_write_base
, to_flush
) != to_flush
)
1507 #else /* !USE_IN_LIBIO */
1510 buffered_vfprintf (register FILE *s
, const CHAR_T
*format
, va_list args
)
1515 s
->__bufp
= s
->__buffer
= buf
;
1516 s
->__bufsize
= sizeof buf
;
1517 s
->__put_limit
= s
->__buffer
+ s
->__bufsize
;
1518 s
->__get_limit
= s
->__buffer
;
1520 /* Now use buffer to print. */
1521 result
= vfprintf (s
, format
, args
);
1523 if (fflush (s
) == EOF
)
1525 s
->__buffer
= s
->__bufp
= s
->__get_limit
= s
->__put_limit
= NULL
;
1531 /* Pads string with given number of a specified character.
1532 This code is taken from iopadn.c of the GNU I/O library. */
1534 static const CHAR_T blanks
[PADSIZE
] =
1535 { L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '),
1536 L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' ') };
1537 static const CHAR_T zeroes
[PADSIZE
] =
1538 { L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'),
1539 L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0') };
1542 #ifndef COMPILE_WPRINTF
1543 __printf_pad (FILE *s
, char pad
, size_t count
)
1545 __wprintf_pad (FILE *s
, wchar_t pad
, size_t count
)
1548 const CHAR_T
*padptr
;
1551 padptr
= pad
== L_(' ') ? blanks
: zeroes
;
1553 for (i
= count
; i
>= PADSIZE
; i
-= PADSIZE
)
1554 if (PUT (s
, padptr
, PADSIZE
) != PADSIZE
)
1557 if (PUT (s
, padptr
, i
) != i
)
1563 #endif /* USE_IN_LIBIO */