1 /* Copyright (C) 1991-1999, 2000, 2001 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 Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 #include <bits/libc-lock.h>
29 #include <sys/param.h>
31 #include <locale/localeinfo.h>
33 /* This code is shared between the standard stdio implementation found
34 in GNU C library and the libio implementation originally found in
37 Beside this it is also shared between the normal and wide character
38 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
42 /* This code is for use in libio. */
44 # define FILE _IO_FILE
46 # define va_list _IO_va_list
48 # define BUFSIZ _IO_BUFSIZ
49 # define ARGCHECK(S, Format) \
52 /* Check file argument for consistence. */ \
54 if (S->_flags & _IO_NO_WRITES) \
56 __set_errno (EBADF); \
65 # define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
67 # ifndef COMPILE_WPRINTF
68 # define vfprintf _IO_vfprintf
70 # define UCHAR_T unsigned char
73 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
75 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
76 # define PAD(Padchar) \
78 done += _IO_padn (s, (Padchar), width)
79 # define PUTC(C, F) _IO_putc_unlocked (C, F)
80 # define ORIENT if (s->_vtable_offset == 0 && _IO_fwide (s, -1) != -1)\
83 # define vfprintf _IO_vfwprintf
84 # define CHAR_T wchar_t
85 /* This is a hack!!! There should be a type uwchar_t. */
86 # define UCHAR_T unsigned int /* uwchar_t */
88 # define L_(Str) L##Str
89 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
93 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
94 # define PAD(Padchar) \
96 done += _IO_wpadn (s, (Padchar), width)
97 # define PUTC(C, F) _IO_putwc_unlocked (C, F)
98 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
100 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
101 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
105 #else /* ! USE_IN_LIBIO */
106 /* This code is for use in the GNU C library. */
108 # define ARGCHECK(S, Format) \
111 /* Check file argument for consistence. */ \
112 if (!__validfp (S) || !S->__mode.__write) \
114 __set_errno (EBADF); \
117 if (Format == NULL) \
119 __set_errno (EINVAL); \
124 if (__flshfp (S, EOF) == EOF) \
129 # define UNBUFFERED_P(s) ((s)->__buffer == NULL)
132 # define UCHAR_T unsigned char
135 # define ISDIGIT(Ch) isdigit (Ch)
137 # define PUT(F, S, N) fwrite (S, 1, N, F)
138 ssize_t __printf_pad __P ((FILE *, char pad, size_t n));
139 # define PAD(Padchar) \
141 { ssize_t __res = __printf_pad (s, (Padchar), width); \
148 # define PUTC(C, F) putc (C, F)
150 /* XXX These declarations should go as soon as the stdio header files
151 have these prototypes. */
152 extern void __flockfile (FILE *);
153 extern void __funlockfile (FILE *);
154 #endif /* USE_IN_LIBIO */
156 #include "_i18n_number.h"
158 /* Include the shared code for parsing the format string. */
159 #include "printf-parse.h"
162 #define outchar(Ch) \
165 register const INT_T outc = (Ch); \
166 if (PUTC (outc, s) == EOF) \
176 #define outstring(String, Len) \
179 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
188 /* For handling long_double and longlong we use the same flag. If
189 `long' and `long long' are effectively the same type define it to
191 #if LONG_MAX == LONG_LONG_MAX
192 # define is_longlong 0
194 # define is_longlong is_long_double
197 /* If `long' and `int' is effectively the same type we don't have to
198 handle `long separately. */
199 #if INT_MAX == LONG_MAX
200 # define is_long_num 0
202 # define is_long_num is_long
206 /* Global variables. */
207 static const CHAR_T null[] = L_("(null)");
210 /* Helper function to provide temporary buffering for unbuffered streams. */
211 static int buffered_vfprintf __P ((FILE *stream, const CHAR_T *fmt, va_list))
214 /* Handle unknown format specifier. */
215 static int printf_unknown __P ((FILE *, const struct printf_info *,
216 const void *const *));
218 /* Group digits of number string. */
219 #ifdef COMPILE_WPRINTF
220 static CHAR_T *group_number __P ((CHAR_T *, CHAR_T *, const char *, wchar_t))
223 static CHAR_T *group_number __P ((CHAR_T *, CHAR_T *, const char *,
224 const char *)) internal_function;
228 /* The function itself. */
230 vfprintf (FILE *s, const CHAR_T *format, va_list ap)
232 /* The character used as thousands separator. */
233 #ifdef COMPILE_WPRINTF
234 wchar_t thousands_sep = L'\0';
236 const char *thousands_sep = NULL;
239 /* The string describing the size of groups of digits. */
240 const char *grouping;
242 /* Place to accumulate the result. */
245 /* Current character in format string. */
248 /* End of leading constant string. */
249 const UCHAR_T *lead_str_end;
251 /* Points to next format specifier. */
252 const UCHAR_T *end_of_spec;
254 /* Buffer intermediate results. */
255 CHAR_T work_buffer[1000];
258 /* State for restartable multibyte character handling functions. */
259 #ifndef COMPILE_WPRINTF
263 /* We have to save the original argument pointer. */
266 /* Count number of specifiers we already processed. */
269 /* For the %m format we may need the current `errno' value. */
270 int save_errno = errno;
273 /* This table maps a character into a number representing a
274 class. In each step there is a destination label for each
276 static const int jump_table[] =
278 /* ' ' */ 1, 0, 0, /* '#' */ 4,
279 0, /* '%' */ 14, 0, /* '\''*/ 6,
280 0, 0, /* '*' */ 7, /* '+' */ 2,
281 0, /* '-' */ 3, /* '.' */ 9, 0,
282 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
283 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
284 /* '8' */ 8, /* '9' */ 8, 0, 0,
286 0, /* 'A' */ 26, 0, /* 'C' */ 25,
287 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
288 0, /* 'I' */ 29, 0, 0,
289 /* 'L' */ 12, 0, 0, 0,
290 0, 0, 0, /* 'S' */ 21,
292 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
294 0, /* 'a' */ 26, 0, /* 'c' */ 20,
295 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
296 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
297 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
298 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
299 /* 't' */ 27, /* 'u' */ 16, 0, 0,
300 /* 'x' */ 18, 0, /* 'z' */ 13
303 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
304 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
305 #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
306 /* 'int' is enough and it saves some space on 64 bit systems. */
307 # define JUMP_TABLE_TYPE const int
308 # define JUMP(ChExpr, table) \
312 void *__unbounded ptr; \
314 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
315 : table[CHAR_CLASS (spec)]; \
316 ptr = &&do_form_unknown + offset; \
321 # define JUMP_TABLE_TYPE const void *const
322 # define JUMP(ChExpr, table) \
325 const void *__unbounded ptr; \
327 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
328 : table[CHAR_CLASS (spec)]; \
334 #define STEP0_3_TABLE \
335 /* Step 0: at the beginning. */ \
336 static JUMP_TABLE_TYPE step0_jumps[30] = \
338 REF (form_unknown), \
339 REF (flag_space), /* for ' ' */ \
340 REF (flag_plus), /* for '+' */ \
341 REF (flag_minus), /* for '-' */ \
342 REF (flag_hash), /* for '<hash>' */ \
343 REF (flag_zero), /* for '0' */ \
344 REF (flag_quote), /* for '\'' */ \
345 REF (width_asterics), /* for '*' */ \
346 REF (width), /* for '1'...'9' */ \
347 REF (precision), /* for '.' */ \
348 REF (mod_half), /* for 'h' */ \
349 REF (mod_long), /* for 'l' */ \
350 REF (mod_longlong), /* for 'L', 'q' */ \
351 REF (mod_size_t), /* for 'z', 'Z' */ \
352 REF (form_percent), /* for '%' */ \
353 REF (form_integer), /* for 'd', 'i' */ \
354 REF (form_unsigned), /* for 'u' */ \
355 REF (form_octal), /* for 'o' */ \
356 REF (form_hexa), /* for 'X', 'x' */ \
357 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
358 REF (form_character), /* for 'c' */ \
359 REF (form_string), /* for 's', 'S' */ \
360 REF (form_pointer), /* for 'p' */ \
361 REF (form_number), /* for 'n' */ \
362 REF (form_strerror), /* for 'm' */ \
363 REF (form_wcharacter), /* for 'C' */ \
364 REF (form_floathex), /* for 'A', 'a' */ \
365 REF (mod_ptrdiff_t), /* for 't' */ \
366 REF (mod_intmax_t), /* for 'j' */ \
367 REF (flag_i18n), /* for 'I' */ \
369 /* Step 1: after processing width. */ \
370 static JUMP_TABLE_TYPE step1_jumps[30] = \
372 REF (form_unknown), \
373 REF (form_unknown), /* for ' ' */ \
374 REF (form_unknown), /* for '+' */ \
375 REF (form_unknown), /* for '-' */ \
376 REF (form_unknown), /* for '<hash>' */ \
377 REF (form_unknown), /* for '0' */ \
378 REF (form_unknown), /* for '\'' */ \
379 REF (form_unknown), /* for '*' */ \
380 REF (form_unknown), /* for '1'...'9' */ \
381 REF (precision), /* for '.' */ \
382 REF (mod_half), /* for 'h' */ \
383 REF (mod_long), /* for 'l' */ \
384 REF (mod_longlong), /* for 'L', 'q' */ \
385 REF (mod_size_t), /* for 'z', 'Z' */ \
386 REF (form_percent), /* for '%' */ \
387 REF (form_integer), /* for 'd', 'i' */ \
388 REF (form_unsigned), /* for 'u' */ \
389 REF (form_octal), /* for 'o' */ \
390 REF (form_hexa), /* for 'X', 'x' */ \
391 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
392 REF (form_character), /* for 'c' */ \
393 REF (form_string), /* for 's', 'S' */ \
394 REF (form_pointer), /* for 'p' */ \
395 REF (form_number), /* for 'n' */ \
396 REF (form_strerror), /* for 'm' */ \
397 REF (form_wcharacter), /* for 'C' */ \
398 REF (form_floathex), /* for 'A', 'a' */ \
399 REF (mod_ptrdiff_t), /* for 't' */ \
400 REF (mod_intmax_t), /* for 'j' */ \
401 REF (flag_i18n) /* for 'I' */ \
403 /* Step 2: after processing precision. */ \
404 static JUMP_TABLE_TYPE step2_jumps[30] = \
406 REF (form_unknown), \
407 REF (form_unknown), /* for ' ' */ \
408 REF (form_unknown), /* for '+' */ \
409 REF (form_unknown), /* for '-' */ \
410 REF (form_unknown), /* for '<hash>' */ \
411 REF (form_unknown), /* for '0' */ \
412 REF (form_unknown), /* for '\'' */ \
413 REF (form_unknown), /* for '*' */ \
414 REF (form_unknown), /* for '1'...'9' */ \
415 REF (form_unknown), /* for '.' */ \
416 REF (mod_half), /* for 'h' */ \
417 REF (mod_long), /* for 'l' */ \
418 REF (mod_longlong), /* for 'L', 'q' */ \
419 REF (mod_size_t), /* for 'z', 'Z' */ \
420 REF (form_percent), /* for '%' */ \
421 REF (form_integer), /* for 'd', 'i' */ \
422 REF (form_unsigned), /* for 'u' */ \
423 REF (form_octal), /* for 'o' */ \
424 REF (form_hexa), /* for 'X', 'x' */ \
425 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
426 REF (form_character), /* for 'c' */ \
427 REF (form_string), /* for 's', 'S' */ \
428 REF (form_pointer), /* for 'p' */ \
429 REF (form_number), /* for 'n' */ \
430 REF (form_strerror), /* for 'm' */ \
431 REF (form_wcharacter), /* for 'C' */ \
432 REF (form_floathex), /* for 'A', 'a' */ \
433 REF (mod_ptrdiff_t), /* for 't' */ \
434 REF (mod_intmax_t), /* for 'j' */ \
435 REF (flag_i18n) /* for 'I' */ \
437 /* Step 3a: after processing first 'h' modifier. */ \
438 static JUMP_TABLE_TYPE step3a_jumps[30] = \
440 REF (form_unknown), \
441 REF (form_unknown), /* for ' ' */ \
442 REF (form_unknown), /* for '+' */ \
443 REF (form_unknown), /* for '-' */ \
444 REF (form_unknown), /* for '<hash>' */ \
445 REF (form_unknown), /* for '0' */ \
446 REF (form_unknown), /* for '\'' */ \
447 REF (form_unknown), /* for '*' */ \
448 REF (form_unknown), /* for '1'...'9' */ \
449 REF (form_unknown), /* for '.' */ \
450 REF (mod_halfhalf), /* for 'h' */ \
451 REF (form_unknown), /* for 'l' */ \
452 REF (form_unknown), /* for 'L', 'q' */ \
453 REF (form_unknown), /* for 'z', 'Z' */ \
454 REF (form_percent), /* for '%' */ \
455 REF (form_integer), /* for 'd', 'i' */ \
456 REF (form_unsigned), /* for 'u' */ \
457 REF (form_octal), /* for 'o' */ \
458 REF (form_hexa), /* for 'X', 'x' */ \
459 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
460 REF (form_unknown), /* for 'c' */ \
461 REF (form_unknown), /* for 's', 'S' */ \
462 REF (form_unknown), /* for 'p' */ \
463 REF (form_number), /* for 'n' */ \
464 REF (form_unknown), /* for 'm' */ \
465 REF (form_unknown), /* for 'C' */ \
466 REF (form_unknown), /* for 'A', 'a' */ \
467 REF (form_unknown), /* for 't' */ \
468 REF (form_unknown), /* for 'j' */ \
469 REF (form_unknown) /* for 'I' */ \
471 /* Step 3b: after processing first 'l' modifier. */ \
472 static JUMP_TABLE_TYPE step3b_jumps[30] = \
474 REF (form_unknown), \
475 REF (form_unknown), /* for ' ' */ \
476 REF (form_unknown), /* for '+' */ \
477 REF (form_unknown), /* for '-' */ \
478 REF (form_unknown), /* for '<hash>' */ \
479 REF (form_unknown), /* for '0' */ \
480 REF (form_unknown), /* for '\'' */ \
481 REF (form_unknown), /* for '*' */ \
482 REF (form_unknown), /* for '1'...'9' */ \
483 REF (form_unknown), /* for '.' */ \
484 REF (form_unknown), /* for 'h' */ \
485 REF (mod_longlong), /* for 'l' */ \
486 REF (form_unknown), /* for 'L', 'q' */ \
487 REF (form_unknown), /* for 'z', 'Z' */ \
488 REF (form_percent), /* for '%' */ \
489 REF (form_integer), /* for 'd', 'i' */ \
490 REF (form_unsigned), /* for 'u' */ \
491 REF (form_octal), /* for 'o' */ \
492 REF (form_hexa), /* for 'X', 'x' */ \
493 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
494 REF (form_character), /* for 'c' */ \
495 REF (form_string), /* for 's', 'S' */ \
496 REF (form_pointer), /* for 'p' */ \
497 REF (form_number), /* for 'n' */ \
498 REF (form_strerror), /* for 'm' */ \
499 REF (form_wcharacter), /* for 'C' */ \
500 REF (form_floathex), /* for 'A', 'a' */ \
501 REF (form_unknown), /* for 't' */ \
502 REF (form_unknown), /* for 'j' */ \
503 REF (form_unknown) /* for 'I' */ \
506 #define STEP4_TABLE \
507 /* Step 4: processing format specifier. */ \
508 static JUMP_TABLE_TYPE step4_jumps[30] = \
510 REF (form_unknown), \
511 REF (form_unknown), /* for ' ' */ \
512 REF (form_unknown), /* for '+' */ \
513 REF (form_unknown), /* for '-' */ \
514 REF (form_unknown), /* for '<hash>' */ \
515 REF (form_unknown), /* for '0' */ \
516 REF (form_unknown), /* for '\'' */ \
517 REF (form_unknown), /* for '*' */ \
518 REF (form_unknown), /* for '1'...'9' */ \
519 REF (form_unknown), /* for '.' */ \
520 REF (form_unknown), /* for 'h' */ \
521 REF (form_unknown), /* for 'l' */ \
522 REF (form_unknown), /* for 'L', 'q' */ \
523 REF (form_unknown), /* for 'z', 'Z' */ \
524 REF (form_percent), /* for '%' */ \
525 REF (form_integer), /* for 'd', 'i' */ \
526 REF (form_unsigned), /* for 'u' */ \
527 REF (form_octal), /* for 'o' */ \
528 REF (form_hexa), /* for 'X', 'x' */ \
529 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
530 REF (form_character), /* for 'c' */ \
531 REF (form_string), /* for 's', 'S' */ \
532 REF (form_pointer), /* for 'p' */ \
533 REF (form_number), /* for 'n' */ \
534 REF (form_strerror), /* for 'm' */ \
535 REF (form_wcharacter), /* for 'C' */ \
536 REF (form_floathex), /* for 'A', 'a' */ \
537 REF (form_unknown), /* for 't' */ \
538 REF (form_unknown), /* for 'j' */ \
539 REF (form_unknown) /* for 'I' */ \
543 #define process_arg(fspec) \
544 /* Start real work. We know about all flags and modifiers and \
545 now process the wanted format specifier. */ \
546 LABEL (form_percent): \
547 /* Write a literal "%". */ \
551 LABEL (form_integer): \
552 /* Signed decimal integer. */ \
557 long long int signed_number; \
560 signed_number = va_arg (ap, long long int); \
562 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
564 is_negative = signed_number < 0; \
565 number.longlong = is_negative ? (- signed_number) : signed_number; \
567 goto LABEL (longlong_number); \
571 long int signed_number; \
576 signed_number = va_arg (ap, long int); \
577 else /* `char' and `short int' will be promoted to `int'. */ \
578 signed_number = va_arg (ap, int); \
582 signed_number = args_value[fspec->data_arg].pa_long_int; \
584 signed_number = args_value[fspec->data_arg].pa_int; \
586 is_negative = signed_number < 0; \
587 number.word = is_negative ? (- signed_number) : signed_number; \
589 goto LABEL (number); \
593 LABEL (form_unsigned): \
594 /* Unsigned decimal integer. */ \
596 goto LABEL (unsigned_number); \
599 LABEL (form_octal): \
600 /* Unsigned octal integer. */ \
602 goto LABEL (unsigned_number); \
606 /* Unsigned hexadecimal integer. */ \
609 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
611 /* ISO specifies the `+' and ` ' flags only for signed \
620 number.longlong = va_arg (ap, unsigned long long int); \
622 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
624 LABEL (longlong_number): \
626 /* Supply a default precision if none was given. */ \
629 /* We have to take care for the '0' flag. If a precision \
630 is given it must be ignored. */ \
633 /* If the precision is 0 and the number is 0 nothing has to \
634 be written for the number, except for the 'o' format in \
636 if (prec == 0 && number.longlong == 0) \
639 if (base == 8 && alt) \
640 *--string = L_('0'); \
644 /* Put the number in WORK. */ \
645 string = _itoa (number.longlong, workend, base, \
647 if (group && grouping) \
648 string = group_number (string, workend, grouping, \
651 if (use_outdigits && base == 10) \
652 string = _i18n_number_rewrite (string, workend); \
654 /* Simplify further test for num != 0. */ \
655 number.word = number.longlong != 0; \
662 number.word = va_arg (ap, unsigned long int); \
664 number.word = (unsigned char) va_arg (ap, unsigned int); \
665 else if (!is_short) \
666 number.word = va_arg (ap, unsigned int); \
668 number.word = (unsigned short int) va_arg (ap, unsigned int); \
672 number.word = args_value[fspec->data_arg].pa_u_long_int; \
674 number.word = (unsigned char) \
675 args_value[fspec->data_arg].pa_char; \
676 else if (!is_short) \
677 number.word = args_value[fspec->data_arg].pa_u_int; \
679 number.word = (unsigned short int) \
680 args_value[fspec->data_arg].pa_u_short_int; \
684 /* Supply a default precision if none was given. */ \
687 /* We have to take care for the '0' flag. If a precision \
688 is given it must be ignored. */ \
691 /* If the precision is 0 and the number is 0 nothing has to \
692 be written for the number, except for the 'o' format in \
694 if (prec == 0 && number.word == 0) \
697 if (base == 8 && alt) \
698 *--string = L_('0'); \
702 /* Put the number in WORK. */ \
703 string = _itoa_word (number.word, workend, base, \
705 if (group && grouping) \
706 string = group_number (string, workend, grouping, \
709 if (use_outdigits && base == 10) \
710 string = _i18n_number_rewrite (string, workend); \
714 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
715 /* Add octal marker. */ \
716 *--string = L_('0'); \
718 prec = MAX (0, prec - (workend - string)); \
722 width -= workend - string + prec; \
724 if (number.word != 0 && alt && base == 16) \
725 /* Account for 0X hex marker. */ \
728 if (is_negative || showsign || space) \
731 if (pad == L_(' ')) \
744 if (number.word != 0 && alt && base == 16) \
753 outstring (string, workend - string); \
775 if (number.word != 0 && alt && base == 16) \
782 width -= workend - string + prec; \
792 outstring (string, workend - string); \
798 LABEL (form_float): \
800 /* Floating-point number. This is handled by printf_fp.c. */ \
806 struct printf_info info = { prec: prec, \
809 is_long_double: is_long_double, \
810 is_short: is_short, \
815 showsign: showsign, \
819 wide: sizeof (CHAR_T) != 1 }; \
821 if (is_long_double) \
822 the_arg.pa_long_double = va_arg (ap, long double); \
824 the_arg.pa_double = va_arg (ap, double); \
825 ptr = (const void *) &the_arg; \
827 function_done = __printf_fp (s, &info, &ptr); \
831 ptr = (const void *) &args_value[fspec->data_arg]; \
833 function_done = __printf_fp (s, &fspec->info, &ptr); \
836 if (function_done < 0) \
838 /* Error in print handler. */ \
843 done += function_done; \
847 LABEL (form_floathex): \
849 /* Floating point number printed as hexadecimal number. */ \
855 struct printf_info info = { prec: prec, \
858 is_long_double: is_long_double, \
859 is_short: is_short, \
864 showsign: showsign, \
868 wide: sizeof (CHAR_T) != 1 }; \
870 if (is_long_double) \
871 the_arg.pa_long_double = va_arg (ap, long double); \
873 the_arg.pa_double = va_arg (ap, double); \
874 ptr = (const void *) &the_arg; \
876 function_done = __printf_fphex (s, &info, &ptr); \
880 ptr = (const void *) &args_value[fspec->data_arg]; \
882 function_done = __printf_fphex (s, &fspec->info, &ptr); \
885 if (function_done < 0) \
887 /* Error in print handler. */ \
892 done += function_done; \
896 LABEL (form_pointer): \
897 /* Generic pointer. */ \
901 ptr = va_arg (ap, void *); \
903 ptr = args_value[fspec->data_arg].pa_pointer; \
906 /* If the pointer is not NULL, write it as a %#x spec. */ \
908 number.word = (unsigned long int) ptr; \
913 goto LABEL (number); \
917 /* Write "(nil)" for a nil pointer. */ \
918 string = (CHAR_T *) L_("(nil)"); \
919 /* Make sure the full string "(nil)" is printed. */ \
922 is_long = 0; /* This is no wide-char string. */ \
923 goto LABEL (print_string); \
928 LABEL (form_number): \
929 /* Answer the count of characters written. */ \
933 *(long long int *) va_arg (ap, void *) = done; \
934 else if (is_long_num) \
935 *(long int *) va_arg (ap, void *) = done; \
937 *(char *) va_arg (ap, void *) = done; \
938 else if (is_long_num) \
939 *(long int *) va_arg (ap, void *) = done; \
940 else if (!is_short) \
941 *(int *) va_arg (ap, void *) = done; \
943 *(short int *) va_arg (ap, void *) = done; \
947 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
948 else if (is_long_num) \
949 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
950 else if (is_long_num) \
951 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
953 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
954 else if (!is_short) \
955 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
957 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
960 LABEL (form_strerror): \
961 /* Print description of error ERRNO. */ \
963 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
964 sizeof work_buffer); \
965 is_long = 0; /* This is no wide-char string. */ \
966 goto LABEL (print_string)
968 #ifdef COMPILE_WPRINTF
969 # define process_string_arg(fspec) \
970 LABEL (form_character): \
973 goto LABEL (form_wcharacter); \
974 --width; /* Account for the character itself. */ \
978 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
980 outchar (__btowc ((unsigned char) \
981 args_value[fspec->data_arg].pa_char)); \
986 LABEL (form_wcharacter): \
988 /* Wide character. */ \
993 outchar (va_arg (ap, wint_t)); \
995 outchar (args_value[fspec->data_arg].pa_wchar); \
1001 LABEL (form_string): \
1004 int string_malloced; \
1006 /* The string argument could in fact be `char *' or `wchar_t *'. \
1007 But this should not make a difference here. */ \
1008 if (fspec == NULL) \
1009 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
1011 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
1013 /* Entry point for printing other strings. */ \
1014 LABEL (print_string): \
1016 string_malloced = 0; \
1017 if (string == NULL) \
1019 /* Write "(null)" if there's space. */ \
1021 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
1023 string = (CHAR_T *) null; \
1024 len = (sizeof (null) / sizeof (null[0])) - 1; \
1028 string = (CHAR_T *) L""; \
1032 else if (!is_long && spec != L_('S')) \
1034 /* This is complicated. We have to transform the multibyte \
1035 string into a wide character string. */ \
1036 const char *mbs = (const char *) string; \
1037 mbstate_t mbstate; \
1039 len = prec != -1 ? prec : strlen (mbs); \
1041 /* Allocate dynamically an array which definitely is long \
1042 enough for the wide character version. */ \
1044 || ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1046 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1048 string_malloced = 1; \
1050 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1051 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1052 if (len == (size_t) -1) \
1054 /* Illegal multibyte character. */ \
1062 /* Search for the end of the string, but don't search past \
1063 the length specified by the precision. */ \
1064 len = __wcsnlen (string, prec); \
1066 len = __wcslen (string); \
1069 if ((width -= len) < 0) \
1071 outstring (string, len); \
1077 outstring (string, len); \
1080 if (string_malloced) \
1085 # define process_string_arg(fspec) \
1086 LABEL (form_character): \
1089 goto LABEL (form_wcharacter); \
1090 --width; /* Account for the character itself. */ \
1093 if (fspec == NULL) \
1094 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1096 outchar ((unsigned char) args_value[fspec->data_arg].pa_char); \
1101 LABEL (form_wcharacter): \
1103 /* Wide character. */ \
1104 char buf[MB_CUR_MAX]; \
1105 mbstate_t mbstate; \
1108 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1109 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wint_t) \
1110 : args_value[fspec->data_arg].pa_wchar), \
1112 if (len == (size_t) -1) \
1114 /* Something went wron gduring the conversion. Bail out. */ \
1121 outstring (buf, len); \
1127 LABEL (form_string): \
1130 int string_malloced; \
1132 /* The string argument could in fact be `char *' or `wchar_t *'. \
1133 But this should not make a difference here. */ \
1134 if (fspec == NULL) \
1135 string = (char *) va_arg (ap, const char *); \
1137 string = (char *) args_value[fspec->data_arg].pa_string; \
1139 /* Entry point for printing other strings. */ \
1140 LABEL (print_string): \
1142 string_malloced = 0; \
1143 if (string == NULL) \
1145 /* Write "(null)" if there's space. */ \
1146 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1148 string = (char *) null; \
1149 len = sizeof (null) - 1; \
1153 string = (char *) ""; \
1157 else if (!is_long && spec != L_('S')) \
1161 /* Search for the end of the string, but don't search past \
1162 the length (in bytes) specified by the precision. Also \
1163 don't use incomplete characters. */ \
1164 if (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MB_CUR_MAX) == 1) \
1165 len = __strnlen (string, prec); \
1168 /* In case we have a multibyte character set the \
1169 situation is more compilcated. We must not copy \
1170 bytes at the end which form an incomplete character. */\
1171 wchar_t ignore[prec]; \
1172 const char *str2 = string; \
1175 memset (&ps, '\0', sizeof (ps)); \
1176 if (__mbsnrtowcs (ignore, &str2, prec, prec, &ps) \
1183 len = strlen (string); \
1185 len = str2 - string - (ps.__count); \
1189 len = strlen (string); \
1193 const wchar_t *s2 = (const wchar_t *) string; \
1194 mbstate_t mbstate; \
1196 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1200 /* The string `s2' might not be NUL terminated. */ \
1202 || (string = (char *) malloc (prec)) == NULL) \
1203 string = (char *) alloca (prec); \
1205 string_malloced = 1; \
1206 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1210 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1211 if (len != (size_t) -1) \
1213 assert (__mbsinit (&mbstate)); \
1214 s2 = (const wchar_t *) string; \
1215 if (len + 1 < 32768 \
1216 || (string = (char *) malloc (len + 1)) == NULL) \
1217 string = (char *) alloca (len + 1); \
1219 string_malloced = 1; \
1220 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1224 if (len == (size_t) -1) \
1226 /* Illegal wide-character string. */ \
1232 if ((width -= len) < 0) \
1234 outstring (string, len); \
1240 outstring (string, len); \
1243 if (string_malloced) \
1249 /* Orient the stream. */
1254 /* Sanity check of arguments. */
1255 ARGCHECK (s, format);
1258 /* Check for correct orientation. */
1260 # ifdef USE_IN_LIBIO
1261 s->_vtable_offset == 0 &&
1263 _IO_fwide (s, sizeof (CHAR_T) == 1 ? -1 : 1)
1264 != (sizeof (CHAR_T) == 1 ? -1 : 1))
1265 /* The stream is already oriented otherwise. */
1269 if (UNBUFFERED_P (s))
1270 /* Use a helper function which will allocate a local temporary buffer
1271 for the stream and then call us again. */
1272 return buffered_vfprintf (s, format, ap);
1274 /* Initialize local variables. */
1276 grouping = (const char *) -1;
1278 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1279 since on some systems `va_list' is not an integral type. */
1280 __va_copy (ap_save, ap);
1286 #ifdef COMPILE_WPRINTF
1287 /* Find the first format specifier. */
1288 f = lead_str_end = find_spec ((const UCHAR_T *) format);
1290 /* Put state for processing format string in initial state. */
1291 memset (&mbstate, '\0', sizeof (mbstate_t));
1293 /* Find the first format specifier. */
1294 f = lead_str_end = find_spec (format, &mbstate);
1299 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
1302 __libc_cleanup_region_start ((void (*) (void *)) &__funlockfile, s);
1306 /* Write the literal text before the first format. */
1307 outstring ((const UCHAR_T *) format,
1308 lead_str_end - (const UCHAR_T *) format);
1310 /* If we only have to print a simple string, return now. */
1314 /* Process whole format string. */
1317 #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
1318 # define REF(Name) &&do_##Name - &&do_form_unknown
1320 # define REF(Name) &&do_##Name
1322 #define LABEL(Name) do_##Name
1326 union printf_arg *args_value; /* This is not used here but ... */
1327 int is_negative; /* Flag for negative number. */
1330 unsigned long long int longlong;
1331 unsigned long int word;
1334 union printf_arg the_arg;
1335 CHAR_T *string; /* Pointer to argument string. */
1336 int alt = 0; /* Alternate format. */
1337 int space = 0; /* Use space prefix if no sign is needed. */
1338 int left = 0; /* Left-justify output. */
1339 int showsign = 0; /* Always begin with plus or minus sign. */
1340 int group = 0; /* Print numbers according grouping rules. */
1341 int is_long_double = 0; /* Argument is long double/ long long int. */
1342 int is_short = 0; /* Argument is short int. */
1343 int is_long = 0; /* Argument is long int. */
1344 int is_char = 0; /* Argument is promoted (unsigned) char. */
1345 int width = 0; /* Width of output; 0 means none specified. */
1346 int prec = -1; /* Precision of output; -1 means none specified. */
1347 /* This flag is set by the 'I' modifier and selects the use of the
1348 `outdigits' as determined by the current locale. */
1349 int use_outdigits = 0;
1350 UCHAR_T pad = L_(' ');/* Padding character. */
1353 workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1355 /* Get current character in format string. */
1356 JUMP (*++f, step0_jumps);
1361 JUMP (*++f, step0_jumps);
1366 JUMP (*++f, step0_jumps);
1372 JUMP (*++f, step0_jumps);
1377 JUMP (*++f, step0_jumps);
1383 JUMP (*++f, step0_jumps);
1385 /* The '\'' flag. */
1389 if (grouping == (const char *) -1)
1391 #ifdef COMPILE_WPRINTF
1392 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1393 _NL_NUMERIC_THOUSANDS_SEP_WC);
1395 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1398 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1399 if (*grouping == '\0' || *grouping == CHAR_MAX
1400 #ifdef COMPILE_WPRINTF
1401 || thousands_sep == L'\0'
1403 || *thousands_sep == '\0'
1408 JUMP (*++f, step0_jumps);
1412 JUMP (*++f, step0_jumps);
1414 /* Get width from argument. */
1415 LABEL (width_asterics):
1417 const UCHAR_T *tmp; /* Temporary value. */
1420 if (ISDIGIT (*tmp) && read_int (&tmp) && *tmp == L_('$'))
1421 /* The width comes from a positional parameter. */
1424 width = va_arg (ap, int);
1426 /* Negative width means left justified. */
1434 if (width + 32 >= (int) (sizeof (work_buffer) / sizeof (work_buffer[0])))
1435 /* We have to use a special buffer. The "32" is just a safe
1436 bet for all the output which is not counted in the width. */
1437 workend = ((CHAR_T *) alloca ((width + 32) * sizeof (CHAR_T))
1440 JUMP (*f, step1_jumps);
1442 /* Given width in format string. */
1444 width = read_int (&f);
1446 if (width + 32 >= (int) (sizeof (work_buffer) / sizeof (work_buffer[0])))
1447 /* We have to use a special buffer. The "32" is just a safe
1448 bet for all the output which is not counted in the width. */
1449 workend = ((CHAR_T *) alloca ((width + 32) * sizeof (CHAR_T))
1452 /* Oh, oh. The argument comes from a positional parameter. */
1454 JUMP (*f, step1_jumps);
1460 const UCHAR_T *tmp; /* Temporary value. */
1463 if (ISDIGIT (*tmp) && read_int (&tmp) > 0 && *tmp == L_('$'))
1464 /* The precision comes from a positional parameter. */
1467 prec = va_arg (ap, int);
1469 /* If the precision is negative the precision is omitted. */
1473 else if (ISDIGIT (*f))
1474 prec = read_int (&f);
1478 && prec + 32 > (int)(sizeof (work_buffer) / sizeof (work_buffer[0])))
1479 workend = alloca (spec + 32) + (spec + 32);
1480 JUMP (*f, step2_jumps);
1482 /* Process 'h' modifier. There might another 'h' following. */
1485 JUMP (*++f, step3a_jumps);
1487 /* Process 'hh' modifier. */
1488 LABEL (mod_halfhalf):
1491 JUMP (*++f, step4_jumps);
1493 /* Process 'l' modifier. There might another 'l' following. */
1496 JUMP (*++f, step3b_jumps);
1498 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1499 allowed to follow. */
1500 LABEL (mod_longlong):
1503 JUMP (*++f, step4_jumps);
1506 is_long_double = sizeof (size_t) > sizeof (unsigned long int);
1507 is_long = sizeof (size_t) > sizeof (unsigned int);
1508 JUMP (*++f, step4_jumps);
1510 LABEL (mod_ptrdiff_t):
1511 is_long_double = sizeof (ptrdiff_t) > sizeof (unsigned long int);
1512 is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
1513 JUMP (*++f, step4_jumps);
1515 LABEL (mod_intmax_t):
1516 is_long_double = sizeof (intmax_t) > sizeof (unsigned long int);
1517 is_long = sizeof (intmax_t) > sizeof (unsigned int);
1518 JUMP (*++f, step4_jumps);
1520 /* Process current format. */
1523 process_arg (((struct printf_spec *) NULL));
1524 process_string_arg (((struct printf_spec *) NULL));
1526 LABEL (form_unknown):
1527 if (spec == L_('\0'))
1529 /* The format string ended before the specifier is complete. */
1534 /* If we are in the fast loop force entering the complicated
1539 /* The format is correctly handled. */
1542 /* Look for next format specifier. */
1543 #ifdef COMPILE_WPRINTF
1544 f = find_spec ((end_of_spec = ++f));
1546 f = find_spec ((end_of_spec = ++f), &mbstate);
1549 /* Write the following constant string. */
1550 outstring (end_of_spec, f - end_of_spec);
1552 while (*f != L_('\0'));
1554 /* Unlock stream and return. */
1557 /* Here starts the more complex loop to handle positional parameters. */
1560 /* Array with information about the needed arguments. This has to
1561 be dynamically extensible. */
1563 size_t nspecs_max = 32; /* A more or less arbitrary start value. */
1564 struct printf_spec *specs
1565 = alloca (nspecs_max * sizeof (struct printf_spec));
1567 /* The number of arguments the format string requests. This will
1568 determine the size of the array needed to store the argument
1572 union printf_arg *args_value = NULL;
1574 /* Positional parameters refer to arguments directly. This could
1575 also determine the maximum number of arguments. Track the
1577 size_t max_ref_arg = 0;
1579 /* Just a counter. */
1583 if (grouping == (const char *) -1)
1585 #ifdef COMPILE_WPRINTF
1586 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1587 _NL_NUMERIC_THOUSANDS_SEP_WC);
1589 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1592 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1593 if (*grouping == '\0' || *grouping == CHAR_MAX)
1597 for (f = lead_str_end; *f != L_('\0'); f = specs[nspecs++].next_fmt)
1599 if (nspecs >= nspecs_max)
1601 /* Extend the array of format specifiers. */
1602 struct printf_spec *old = specs;
1605 specs = alloca (nspecs_max * sizeof (struct printf_spec));
1607 if (specs == &old[nspecs])
1608 /* Stack grows up, OLD was the last thing allocated;
1610 nspecs_max += nspecs_max / 2;
1613 /* Copy the old array's elements to the new space. */
1614 memcpy (specs, old, nspecs * sizeof (struct printf_spec));
1615 if (old == &specs[nspecs])
1616 /* Stack grows down, OLD was just below the new
1617 SPECS. We can use that space when the new space
1619 nspecs_max += nspecs_max / 2;
1623 /* Parse the format specifier. */
1624 #ifdef COMPILE_WPRINTF
1625 nargs += parse_one_spec (f, nargs, &specs[nspecs], &max_ref_arg);
1627 nargs += parse_one_spec (f, nargs, &specs[nspecs], &max_ref_arg,
1632 /* Determine the number of arguments the format string consumes. */
1633 nargs = MAX (nargs, max_ref_arg);
1635 /* Allocate memory for the argument descriptions. */
1636 args_type = alloca (nargs * sizeof (int));
1637 memset (args_type, 0, nargs * sizeof (int));
1638 args_value = alloca (nargs * sizeof (union printf_arg));
1640 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1641 still zero after this loop, format is invalid. For now we
1642 simply use 0 as the value. */
1644 /* Fill in the types of all the arguments. */
1645 for (cnt = 0; cnt < nspecs; ++cnt)
1647 /* If the width is determined by an argument this is an int. */
1648 if (specs[cnt].width_arg != -1)
1649 args_type[specs[cnt].width_arg] = PA_INT;
1651 /* If the precision is determined by an argument this is an int. */
1652 if (specs[cnt].prec_arg != -1)
1653 args_type[specs[cnt].prec_arg] = PA_INT;
1655 switch (specs[cnt].ndata_args)
1657 case 0: /* No arguments. */
1659 case 1: /* One argument; we already have the type. */
1660 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1663 /* We have more than one argument for this format spec.
1664 We must call the arginfo function again to determine
1666 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1668 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg]);
1673 /* Now we know all the types and the order. Fill in the argument
1675 for (cnt = 0; cnt < nargs; ++cnt)
1676 switch (args_type[cnt])
1678 #define T(tag, mem, type) \
1680 args_value[cnt].mem = va_arg (ap_save, type); \
1683 T (PA_CHAR, pa_char, int); /* Promoted. */
1684 T (PA_WCHAR, pa_wchar, wint_t);
1685 T (PA_INT|PA_FLAG_SHORT, pa_short_int, int); /* Promoted. */
1686 T (PA_INT, pa_int, int);
1687 T (PA_INT|PA_FLAG_LONG, pa_long_int, long int);
1688 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1689 T (PA_FLOAT, pa_float, double); /* Promoted. */
1690 T (PA_DOUBLE, pa_double, double);
1691 T (PA_DOUBLE|PA_FLAG_LONG_DOUBLE, pa_long_double, long double);
1692 T (PA_STRING, pa_string, const char *);
1693 T (PA_WSTRING, pa_wstring, const wchar_t *);
1694 T (PA_POINTER, pa_pointer, void *);
1697 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1698 args_value[cnt].pa_pointer = va_arg (ap_save, void *);
1700 args_value[cnt].pa_long_double = 0.0;
1704 /* Now walk through all format specifiers and process them. */
1705 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1708 #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
1709 # define REF(Name) &&do2_##Name - &&do_form_unknown
1711 # define REF(Name) &&do2_##Name
1714 #define LABEL(Name) do2_##Name
1720 unsigned long long int longlong;
1721 unsigned long int word;
1724 union printf_arg the_arg;
1725 CHAR_T *string; /* Pointer to argument string. */
1727 /* Fill variables from values in struct. */
1728 int alt = specs[nspecs_done].info.alt;
1729 int space = specs[nspecs_done].info.space;
1730 int left = specs[nspecs_done].info.left;
1731 int showsign = specs[nspecs_done].info.showsign;
1732 int group = specs[nspecs_done].info.group;
1733 int is_long_double = specs[nspecs_done].info.is_long_double;
1734 int is_short = specs[nspecs_done].info.is_short;
1735 int is_char = specs[nspecs_done].info.is_char;
1736 int is_long = specs[nspecs_done].info.is_long;
1737 int width = specs[nspecs_done].info.width;
1738 int prec = specs[nspecs_done].info.prec;
1739 int use_outdigits = specs[nspecs_done].info.i18n;
1740 char pad = specs[nspecs_done].info.pad;
1741 CHAR_T spec = specs[nspecs_done].info.spec;
1743 /* Fill in last information. */
1744 if (specs[nspecs_done].width_arg != -1)
1746 /* Extract the field width from an argument. */
1747 specs[nspecs_done].info.width =
1748 args_value[specs[nspecs_done].width_arg].pa_int;
1750 if (specs[nspecs_done].info.width < 0)
1751 /* If the width value is negative left justification is
1752 selected and the value is taken as being positive. */
1754 specs[nspecs_done].info.width *= -1;
1755 left = specs[nspecs_done].info.left = 1;
1757 width = specs[nspecs_done].info.width;
1760 if (specs[nspecs_done].prec_arg != -1)
1762 /* Extract the precision from an argument. */
1763 specs[nspecs_done].info.prec =
1764 args_value[specs[nspecs_done].prec_arg].pa_int;
1766 if (specs[nspecs_done].info.prec < 0)
1767 /* If the precision is negative the precision is
1769 specs[nspecs_done].info.prec = -1;
1771 prec = specs[nspecs_done].info.prec;
1774 /* Maybe the buffer is too small. */
1775 if (MAX (prec, width) + 32 > (int) (sizeof (work_buffer) / sizeof (CHAR_T)))
1776 workend = ((CHAR_T *) alloca ((MAX (prec, width) + 32)
1778 + (MAX (prec, width) + 32));
1780 /* Process format specifiers. */
1783 JUMP (spec, step4_jumps);
1785 process_arg ((&specs[nspecs_done]));
1786 process_string_arg ((&specs[nspecs_done]));
1788 LABEL (form_unknown):
1790 extern printf_function **__printf_function_table;
1792 printf_function *function;
1797 (__printf_function_table == NULL ? NULL :
1798 __printf_function_table[specs[nspecs_done].info.spec]);
1800 if (function == NULL)
1801 function = &printf_unknown;
1803 ptr = alloca (specs[nspecs_done].ndata_args
1804 * sizeof (const void *));
1806 /* Fill in an array of pointers to the argument values. */
1807 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
1808 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1810 /* Call the function. */
1811 function_done = (*function) (s, &specs[nspecs_done].info, ptr);
1813 /* If an error occurred we don't have information about #
1815 if (function_done < 0)
1821 done += function_done;
1826 /* Write the following constant string. */
1827 outstring (specs[nspecs_done].end_of_fmt,
1828 specs[nspecs_done].next_fmt
1829 - specs[nspecs_done].end_of_fmt);
1834 /* Unlock the stream. */
1836 _IO_funlockfile (s);
1840 __libc_cleanup_region_end (0);
1845 /* Handle an unknown format specifier. This prints out a canonicalized
1846 representation of the format spec itself. */
1848 printf_unknown (FILE *s, const struct printf_info *info,
1849 const void *const *args)
1853 CHAR_T work_buffer[MAX (info->width, info->spec) + 32];
1854 CHAR_T *const workend
1855 = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1866 else if (info->space)
1870 if (info->pad == L_('0'))
1875 if (info->width != 0)
1877 w = _itoa_word (info->width, workend, 10, 0);
1882 if (info->prec != -1)
1885 w = _itoa_word (info->prec, workend, 10, 0);
1890 if (info->spec != L_('\0'))
1891 outchar (info->spec);
1897 /* Group the digits according to the grouping rules of the current locale.
1898 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1901 group_number (CHAR_T *w, CHAR_T *rear_ptr, const char *grouping,
1902 #ifdef COMPILE_WPRINTF
1903 wchar_t thousands_sep
1905 const char *thousands_sep
1911 #ifndef COMPILE_WPRINTF
1912 int tlen = strlen (thousands_sep);
1915 /* We treat all negative values like CHAR_MAX. */
1917 if (*grouping == CHAR_MAX || *grouping <= 0)
1918 /* No grouping should be done. */
1923 /* Copy existing string so that nothing gets overwritten. */
1924 src = (CHAR_T *) alloca ((rear_ptr - w) * sizeof (CHAR_T));
1925 s = (CHAR_T *) __mempcpy (src, w,
1926 (rear_ptr - w) * sizeof (CHAR_T));
1929 /* Process all characters in the string. */
1934 if (--len == 0 && s > src)
1936 /* A new group begins. */
1937 #ifdef COMPILE_WPRINTF
1938 *--w = thousands_sep;
1942 *--w = thousands_sep[--cnt];
1947 if (*grouping == '\0')
1948 /* The previous grouping repeats ad infinitum. */
1950 else if (*grouping == CHAR_MAX
1956 /* No further grouping to be done.
1957 Copy the rest of the number. */
1969 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
1972 struct _IO_FILE_plus _f;
1973 #ifdef COMPILE_WPRINTF
1974 struct _IO_wide_data _wide_data;
1976 _IO_FILE *_put_stream;
1977 #ifdef _IO_MTSAFE_IO
1983 _IO_helper_overflow (_IO_FILE *s, int c)
1985 _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
1986 #ifdef COMPILE_WPRINTF
1987 int used = s->_wide_data->_IO_write_ptr - s->_wide_data->_IO_write_base;
1990 _IO_size_t written = _IO_sputn (target, s->_wide_data->_IO_write_base,
1992 s->_wide_data->_IO_write_ptr -= written;
1995 int used = s->_IO_write_ptr - s->_IO_write_base;
1998 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
1999 s->_IO_write_ptr -= written;
2005 #ifdef COMPILE_WPRINTF
2006 static const struct _IO_jump_t _IO_helper_jumps =
2009 JUMP_INIT (finish, _IO_wdefault_finish),
2010 JUMP_INIT (overflow, _IO_helper_overflow),
2011 JUMP_INIT (underflow, _IO_default_underflow),
2012 JUMP_INIT (uflow, _IO_default_uflow),
2013 JUMP_INIT (pbackfail, (_IO_pbackfail_t) _IO_wdefault_pbackfail),
2014 JUMP_INIT (xsputn, _IO_wdefault_xsputn),
2015 JUMP_INIT (xsgetn, _IO_wdefault_xsgetn),
2016 JUMP_INIT (seekoff, _IO_default_seekoff),
2017 JUMP_INIT (seekpos, _IO_default_seekpos),
2018 JUMP_INIT (setbuf,(_IO_setbuf_t) _IO_wdefault_setbuf),
2019 JUMP_INIT (sync, _IO_default_sync),
2020 JUMP_INIT (doallocate, _IO_wdefault_doallocate),
2021 JUMP_INIT (read, _IO_default_read),
2022 JUMP_INIT (write, _IO_default_write),
2023 JUMP_INIT (seek, _IO_default_seek),
2024 JUMP_INIT (close, _IO_default_close),
2025 JUMP_INIT (stat, _IO_default_stat)
2028 static const struct _IO_jump_t _IO_helper_jumps =
2031 JUMP_INIT (finish, _IO_default_finish),
2032 JUMP_INIT (overflow, _IO_helper_overflow),
2033 JUMP_INIT (underflow, _IO_default_underflow),
2034 JUMP_INIT (uflow, _IO_default_uflow),
2035 JUMP_INIT (pbackfail, _IO_default_pbackfail),
2036 JUMP_INIT (xsputn, _IO_default_xsputn),
2037 JUMP_INIT (xsgetn, _IO_default_xsgetn),
2038 JUMP_INIT (seekoff, _IO_default_seekoff),
2039 JUMP_INIT (seekpos, _IO_default_seekpos),
2040 JUMP_INIT (setbuf, _IO_default_setbuf),
2041 JUMP_INIT (sync, _IO_default_sync),
2042 JUMP_INIT (doallocate, _IO_default_doallocate),
2043 JUMP_INIT (read, _IO_default_read),
2044 JUMP_INIT (write, _IO_default_write),
2045 JUMP_INIT (seek, _IO_default_seek),
2046 JUMP_INIT (close, _IO_default_close),
2047 JUMP_INIT (stat, _IO_default_stat)
2053 buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format,
2056 CHAR_T buf[_IO_BUFSIZ];
2057 struct helper_file helper;
2058 register _IO_FILE *hp = (_IO_FILE *) &helper._f;
2059 int result, to_flush;
2061 /* Orient the stream. */
2066 /* Initialize helper. */
2067 helper._put_stream = s;
2068 #ifdef COMPILE_WPRINTF
2069 hp->_wide_data = &helper._wide_data;
2070 _IO_wsetp (hp, buf, buf + sizeof buf / sizeof (CHAR_T));
2073 _IO_setp (hp, buf, buf + sizeof buf);
2076 hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS|_IO_USER_LOCK;
2077 #if _IO_JUMPS_OFFSET
2078 hp->_vtable_offset = 0;
2080 #ifdef _IO_MTSAFE_IO
2083 _IO_JUMPS (&helper._f) = (struct _IO_jump_t *) &_IO_helper_jumps;
2085 /* Now print to helper instead. */
2086 result = vfprintf (hp, format, args);
2089 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
2092 /* Now flush anything from the helper to the S. */
2093 #ifdef COMPILE_WPRINTF
2094 if ((to_flush = (hp->_wide_data->_IO_write_ptr
2095 - hp->_wide_data->_IO_write_base)) > 0)
2097 if ((int) _IO_sputn (s, hp->_wide_data->_IO_write_base, to_flush)
2102 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
2104 if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
2109 /* Unlock the stream. */
2110 _IO_funlockfile (s);
2111 __libc_cleanup_region_end (0);
2116 #else /* !USE_IN_LIBIO */
2120 buffered_vfprintf (register FILE *s, const CHAR_T *format, va_list args)
2125 /* Orient the stream. */
2130 s->__bufp = s->__buffer = buf;
2131 s->__bufsize = sizeof buf;
2132 s->__put_limit = s->__buffer + s->__bufsize;
2133 s->__get_limit = s->__buffer;
2135 /* Now use buffer to print. */
2136 result = vfprintf (s, format, args);
2138 if (fflush (s) == EOF)
2140 s->__buffer = s->__bufp = s->__get_limit = s->__put_limit = NULL;
2146 /* Pads string with given number of a specified character.
2147 This code is taken from iopadn.c of the GNU I/O library. */
2149 static const CHAR_T blanks[PADSIZE] =
2150 { L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '),
2151 L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' ') };
2152 static const CHAR_T zeroes[PADSIZE] =
2153 { L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'),
2154 L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0') };
2157 #ifndef COMPILE_WPRINTF
2158 __printf_pad (FILE *s, char pad, size_t count)
2160 __wprintf_pad (FILE *s, wchar_t pad, size_t count)
2163 const CHAR_T *padptr;
2166 padptr = pad == L_(' ') ? blanks : zeroes;
2168 for (i = count; i >= PADSIZE; i -= PADSIZE)
2169 if (PUT (s, padptr, PADSIZE) != PADSIZE)
2172 if (PUT (s, padptr, i) != i)
2178 #endif /* USE_IN_LIBIO */
2182 # ifdef strong_alias
2183 /* This is for glibc. */
2184 # ifdef COMPILE_WPRINTF
2185 strong_alias (_IO_vfwprintf, __vfwprintf);
2186 weak_alias (_IO_vfwprintf, vfwprintf);
2188 strong_alias (_IO_vfprintf, vfprintf);
2191 # if defined __ELF__ || defined __GNU_LIBRARY__
2192 # include <gnu-stabs.h>
2194 # ifdef COMPILE_WPRINTF
2195 weak_alias (_IO_vfwprintf, vfwprintf);
2197 weak_alias (_IO_vfprintf, vfprintf);