1 /* Copyright (C) 1991-1999, 2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
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. */ \
801 extern int __printf_fp __P ((FILE *, const struct printf_info *, \
802 const void **const)); \
808 struct printf_info info = { prec: prec, \
811 is_long_double: is_long_double, \
812 is_short: is_short, \
817 showsign: showsign, \
821 wide: sizeof (CHAR_T) != 1 }; \
823 if (is_long_double) \
824 the_arg.pa_long_double = va_arg (ap, long double); \
826 the_arg.pa_double = va_arg (ap, double); \
827 ptr = (const void *) &the_arg; \
829 function_done = __printf_fp (s, &info, &ptr); \
833 ptr = (const void *) &args_value[fspec->data_arg]; \
835 function_done = __printf_fp (s, &fspec->info, &ptr); \
838 if (function_done < 0) \
840 /* Error in print handler. */ \
845 done += function_done; \
849 LABEL (form_floathex): \
851 /* FLoating point number printed as hexadecimal number. */ \
852 extern int __printf_fphex __P ((FILE *, const struct printf_info *, \
853 const void **const)); \
859 struct printf_info info = { prec: prec, \
862 is_long_double: is_long_double, \
863 is_short: is_short, \
868 showsign: showsign, \
872 wide: sizeof (CHAR_T) != 1 }; \
874 if (is_long_double) \
875 the_arg.pa_long_double = va_arg (ap, long double); \
877 the_arg.pa_double = va_arg (ap, double); \
878 ptr = (const void *) &the_arg; \
880 function_done = __printf_fphex (s, &info, &ptr); \
884 ptr = (const void *) &args_value[fspec->data_arg]; \
886 function_done = __printf_fphex (s, &fspec->info, &ptr); \
889 if (function_done < 0) \
891 /* Error in print handler. */ \
896 done += function_done; \
900 LABEL (form_pointer): \
901 /* Generic pointer. */ \
905 ptr = va_arg (ap, void *); \
907 ptr = args_value[fspec->data_arg].pa_pointer; \
910 /* If the pointer is not NULL, write it as a %#x spec. */ \
912 number.word = (unsigned long int) ptr; \
917 goto LABEL (number); \
921 /* Write "(nil)" for a nil pointer. */ \
922 string = (CHAR_T *) L_("(nil)"); \
923 /* Make sure the full string "(nil)" is printed. */ \
926 is_long = 0; /* This is no wide-char string. */ \
927 goto LABEL (print_string); \
932 LABEL (form_number): \
933 /* Answer the count of characters written. */ \
937 *(long long int *) va_arg (ap, void *) = done; \
938 else if (is_long_num) \
939 *(long int *) va_arg (ap, void *) = done; \
941 *(char *) va_arg (ap, void *) = done; \
942 else if (is_long_num) \
943 *(long int *) va_arg (ap, void *) = done; \
944 else if (!is_short) \
945 *(int *) va_arg (ap, void *) = done; \
947 *(short int *) va_arg (ap, void *) = done; \
951 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
952 else if (is_long_num) \
953 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
954 else if (is_long_num) \
955 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
957 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
958 else if (!is_short) \
959 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
961 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
964 LABEL (form_strerror): \
965 /* Print description of error ERRNO. */ \
967 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
968 sizeof work_buffer); \
969 is_long = 0; /* This is no wide-char string. */ \
970 goto LABEL (print_string)
972 #ifdef COMPILE_WPRINTF
973 # define process_string_arg(fspec) \
974 LABEL (form_character): \
977 goto LABEL (form_wcharacter); \
978 --width; /* Account for the character itself. */ \
982 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
984 outchar (__btowc ((unsigned char) \
985 args_value[fspec->data_arg].pa_char)); \
990 LABEL (form_wcharacter): \
992 /* Wide character. */ \
997 outchar (va_arg (ap, wint_t)); \
999 outchar (args_value[fspec->data_arg].pa_wchar); \
1005 LABEL (form_string): \
1008 int string_malloced; \
1010 /* The string argument could in fact be `char *' or `wchar_t *'. \
1011 But this should not make a difference here. */ \
1012 if (fspec == NULL) \
1013 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
1015 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
1017 /* Entry point for printing other strings. */ \
1018 LABEL (print_string): \
1020 string_malloced = 0; \
1021 if (string == NULL) \
1023 /* Write "(null)" if there's space. */ \
1025 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
1027 string = (CHAR_T *) null; \
1028 len = (sizeof (null) / sizeof (null[0])) - 1; \
1032 string = (CHAR_T *) L""; \
1036 else if (!is_long && spec != L_('S')) \
1038 /* This is complicated. We have to transform the multibyte \
1039 string into a wide character string. */ \
1040 const char *mbs = (const char *) string; \
1041 mbstate_t mbstate; \
1043 len = prec != -1 ? prec : strlen (mbs); \
1045 /* Allocate dynamically an array which definitely is long \
1046 enough for the wide character version. */ \
1048 || ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1050 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1052 string_malloced = 1; \
1054 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1055 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1056 if (len == (size_t) -1) \
1058 /* Illegal multibyte character. */ \
1066 /* Search for the end of the string, but don't search past \
1067 the length specified by the precision. */ \
1068 len = __wcsnlen (string, prec); \
1070 len = __wcslen (string); \
1073 if ((width -= len) < 0) \
1075 outstring (string, len); \
1081 outstring (string, len); \
1084 if (string_malloced) \
1089 # define process_string_arg(fspec) \
1090 LABEL (form_character): \
1093 goto LABEL (form_wcharacter); \
1094 --width; /* Account for the character itself. */ \
1097 if (fspec == NULL) \
1098 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1100 outchar ((unsigned char) args_value[fspec->data_arg].pa_char); \
1105 LABEL (form_wcharacter): \
1107 /* Wide character. */ \
1108 char buf[MB_CUR_MAX]; \
1109 mbstate_t mbstate; \
1112 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1113 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wint_t) \
1114 : args_value[fspec->data_arg].pa_wchar), \
1116 if (len == (size_t) -1) \
1118 /* Something went wron gduring the conversion. Bail out. */ \
1125 outstring (buf, len); \
1131 LABEL (form_string): \
1134 int string_malloced; \
1136 /* The string argument could in fact be `char *' or `wchar_t *'. \
1137 But this should not make a difference here. */ \
1138 if (fspec == NULL) \
1139 string = (char *) va_arg (ap, const char *); \
1141 string = (char *) args_value[fspec->data_arg].pa_string; \
1143 /* Entry point for printing other strings. */ \
1144 LABEL (print_string): \
1146 string_malloced = 0; \
1147 if (string == NULL) \
1149 /* Write "(null)" if there's space. */ \
1150 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1152 string = (char *) null; \
1153 len = sizeof (null) - 1; \
1157 string = (char *) ""; \
1161 else if (!is_long && spec != L_('S')) \
1165 /* Search for the end of the string, but don't search past \
1166 the length (in bytes) specified by the precision. Also \
1167 don't use incomplete characters. */ \
1168 if (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MB_CUR_MAX) == 1) \
1169 len = __strnlen (string, prec); \
1172 /* In case we have a multibyte character set the \
1173 situation is more compilcated. We must not copy \
1174 bytes at the end which form an incomplete character. */\
1175 wchar_t ignore[prec]; \
1176 const char *str2 = string; \
1179 memset (&ps, '\0', sizeof (ps)); \
1180 if (__mbsnrtowcs (ignore, &str2, prec, prec, &ps) \
1187 len = strlen (string); \
1189 len = str2 - string - (ps.__count); \
1193 len = strlen (string); \
1197 const wchar_t *s2 = (const wchar_t *) string; \
1198 mbstate_t mbstate; \
1200 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1204 /* The string `s2' might not be NUL terminated. */ \
1206 || (string = (char *) malloc (prec)) == NULL) \
1207 string = (char *) alloca (prec); \
1209 string_malloced = 1; \
1210 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1214 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1215 if (len != (size_t) -1) \
1217 assert (__mbsinit (&mbstate)); \
1218 s2 = (const wchar_t *) string; \
1219 if (len + 1 < 32768 \
1220 || (string = (char *) malloc (len + 1)) == NULL) \
1221 string = (char *) alloca (len + 1); \
1223 string_malloced = 1; \
1224 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1228 if (len == (size_t) -1) \
1230 /* Illegal wide-character string. */ \
1236 if ((width -= len) < 0) \
1238 outstring (string, len); \
1244 outstring (string, len); \
1247 if (string_malloced) \
1253 /* Orient the stream. */
1258 /* Sanity check of arguments. */
1259 ARGCHECK (s
, format
);
1262 /* Check for correct orientation. */
1264 # ifdef USE_IN_LIBIO
1265 s
->_vtable_offset
== 0 &&
1267 _IO_fwide (s
, sizeof (CHAR_T
) == 1 ? -1 : 1)
1268 != (sizeof (CHAR_T
) == 1 ? -1 : 1))
1269 /* The stream is already oriented otherwise. */
1273 if (UNBUFFERED_P (s
))
1274 /* Use a helper function which will allocate a local temporary buffer
1275 for the stream and then call us again. */
1276 return buffered_vfprintf (s
, format
, ap
);
1278 /* Initialize local variables. */
1280 grouping
= (const char *) -1;
1282 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1283 since on some systems `va_list' is not an integral type. */
1284 __va_copy (ap_save
, ap
);
1290 #ifdef COMPILE_WPRINTF
1291 /* Find the first format specifier. */
1292 f
= lead_str_end
= find_spec ((const UCHAR_T
*) format
);
1294 /* Put state for processing format string in initial state. */
1295 memset (&mbstate
, '\0', sizeof (mbstate_t));
1297 /* Find the first format specifier. */
1298 f
= lead_str_end
= find_spec (format
, &mbstate
);
1303 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile
, s
);
1306 __libc_cleanup_region_start ((void (*) (void *)) &__funlockfile
, s
);
1310 /* Write the literal text before the first format. */
1311 outstring ((const UCHAR_T
*) format
,
1312 lead_str_end
- (const UCHAR_T
*) format
);
1314 /* If we only have to print a simple string, return now. */
1318 /* Process whole format string. */
1321 #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
1322 # define REF(Name) &&do_##Name - &&do_form_unknown
1324 # define REF(Name) &&do_##Name
1326 #define LABEL(Name) do_##Name
1330 union printf_arg
*args_value
; /* This is not used here but ... */
1331 int is_negative
; /* Flag for negative number. */
1334 unsigned long long int longlong
;
1335 unsigned long int word
;
1338 union printf_arg the_arg
;
1339 CHAR_T
*string
; /* Pointer to argument string. */
1340 int alt
= 0; /* Alternate format. */
1341 int space
= 0; /* Use space prefix if no sign is needed. */
1342 int left
= 0; /* Left-justify output. */
1343 int showsign
= 0; /* Always begin with plus or minus sign. */
1344 int group
= 0; /* Print numbers according grouping rules. */
1345 int is_long_double
= 0; /* Argument is long double/ long long int. */
1346 int is_short
= 0; /* Argument is short int. */
1347 int is_long
= 0; /* Argument is long int. */
1348 int is_char
= 0; /* Argument is promoted (unsigned) char. */
1349 int width
= 0; /* Width of output; 0 means none specified. */
1350 int prec
= -1; /* Precision of output; -1 means none specified. */
1351 /* This flag is set by the 'I' modifier and selects the use of the
1352 `outdigits' as determined by the current locale. */
1353 int use_outdigits
= 0;
1354 UCHAR_T pad
= L_(' ');/* Padding character. */
1357 workend
= &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
1359 /* Get current character in format string. */
1360 JUMP (*++f
, step0_jumps
);
1365 JUMP (*++f
, step0_jumps
);
1370 JUMP (*++f
, step0_jumps
);
1376 JUMP (*++f
, step0_jumps
);
1381 JUMP (*++f
, step0_jumps
);
1387 JUMP (*++f
, step0_jumps
);
1389 /* The '\'' flag. */
1393 if (grouping
== (const char *) -1)
1395 #ifdef COMPILE_WPRINTF
1396 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1397 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1399 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1402 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1403 if (*grouping
== '\0' || *grouping
== CHAR_MAX
1404 #ifdef COMPILE_WPRINTF
1405 || thousands_sep
== L
'\0'
1407 || *thousands_sep
== '\0'
1412 JUMP (*++f
, step0_jumps
);
1416 JUMP (*++f
, step0_jumps
);
1418 /* Get width from argument. */
1419 LABEL (width_asterics
):
1421 const UCHAR_T
*tmp
; /* Temporary value. */
1424 if (ISDIGIT (*tmp
) && read_int (&tmp
) && *tmp
== L_('$'))
1425 /* The width comes from a positional parameter. */
1428 width
= va_arg (ap
, int);
1430 /* Negative width means left justified. */
1438 if (width
+ 32 >= sizeof (work_buffer
) / sizeof (work_buffer
[0]))
1439 /* We have to use a special buffer. The "32" is just a safe
1440 bet for all the output which is not counted in the width. */
1441 workend
= ((CHAR_T
*) alloca ((width
+ 32) * sizeof (CHAR_T
))
1444 JUMP (*f
, step1_jumps
);
1446 /* Given width in format string. */
1448 width
= read_int (&f
);
1450 if (width
+ 32 >= sizeof (work_buffer
) / sizeof (work_buffer
[0]))
1451 /* We have to use a special buffer. The "32" is just a safe
1452 bet for all the output which is not counted in the width. */
1453 workend
= ((CHAR_T
*) alloca ((width
+ 32) * sizeof (CHAR_T
))
1456 /* Oh, oh. The argument comes from a positional parameter. */
1458 JUMP (*f
, step1_jumps
);
1464 const UCHAR_T
*tmp
; /* Temporary value. */
1467 if (ISDIGIT (*tmp
) && read_int (&tmp
) > 0 && *tmp
== L_('$'))
1468 /* The precision comes from a positional parameter. */
1471 prec
= va_arg (ap
, int);
1473 /* If the precision is negative the precision is omitted. */
1477 else if (ISDIGIT (*f
))
1478 prec
= read_int (&f
);
1482 && prec
+ 32 > sizeof (work_buffer
) / sizeof (work_buffer
[0]))
1483 workend
= alloca (spec
+ 32) + (spec
+ 32);
1484 JUMP (*f
, step2_jumps
);
1486 /* Process 'h' modifier. There might another 'h' following. */
1489 JUMP (*++f
, step3a_jumps
);
1491 /* Process 'hh' modifier. */
1492 LABEL (mod_halfhalf
):
1495 JUMP (*++f
, step4_jumps
);
1497 /* Process 'l' modifier. There might another 'l' following. */
1500 JUMP (*++f
, step3b_jumps
);
1502 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1503 allowed to follow. */
1504 LABEL (mod_longlong
):
1507 JUMP (*++f
, step4_jumps
);
1510 is_long_double
= sizeof (size_t) > sizeof (unsigned long int);
1511 is_long
= sizeof (size_t) > sizeof (unsigned int);
1512 JUMP (*++f
, step4_jumps
);
1514 LABEL (mod_ptrdiff_t
):
1515 is_long_double
= sizeof (ptrdiff_t) > sizeof (unsigned long int);
1516 is_long
= sizeof (ptrdiff_t) > sizeof (unsigned int);
1517 JUMP (*++f
, step4_jumps
);
1519 LABEL (mod_intmax_t
):
1520 is_long_double
= sizeof (intmax_t) > sizeof (unsigned long int);
1521 is_long
= sizeof (intmax_t) > sizeof (unsigned int);
1522 JUMP (*++f
, step4_jumps
);
1524 /* Process current format. */
1527 process_arg (((struct printf_spec
*) NULL
));
1528 process_string_arg (((struct printf_spec
*) NULL
));
1530 LABEL (form_unknown
):
1531 if (spec
== L_('\0'))
1533 /* The format string ended before the specifier is complete. */
1538 /* If we are in the fast loop force entering the complicated
1543 /* The format is correctly handled. */
1546 /* Look for next format specifier. */
1547 #ifdef COMPILE_WPRINTF
1548 f
= find_spec ((end_of_spec
= ++f
));
1550 f
= find_spec ((end_of_spec
= ++f
), &mbstate
);
1553 /* Write the following constant string. */
1554 outstring (end_of_spec
, f
- end_of_spec
);
1556 while (*f
!= L_('\0'));
1558 /* Unlock stream and return. */
1561 /* Here starts the more complex loop to handle positional parameters. */
1564 /* Array with information about the needed arguments. This has to
1565 be dynamically extensible. */
1567 size_t nspecs_max
= 32; /* A more or less arbitrary start value. */
1568 struct printf_spec
*specs
1569 = alloca (nspecs_max
* sizeof (struct printf_spec
));
1571 /* The number of arguments the format string requests. This will
1572 determine the size of the array needed to store the argument
1576 union printf_arg
*args_value
= NULL
;
1578 /* Positional parameters refer to arguments directly. This could
1579 also determine the maximum number of arguments. Track the
1581 size_t max_ref_arg
= 0;
1583 /* Just a counter. */
1587 if (grouping
== (const char *) -1)
1589 #ifdef COMPILE_WPRINTF
1590 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1591 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1593 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1596 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1597 if (*grouping
== '\0' || *grouping
== CHAR_MAX
)
1601 for (f
= lead_str_end
; *f
!= L_('\0'); f
= specs
[nspecs
++].next_fmt
)
1603 if (nspecs
>= nspecs_max
)
1605 /* Extend the array of format specifiers. */
1606 struct printf_spec
*old
= specs
;
1609 specs
= alloca (nspecs_max
* sizeof (struct printf_spec
));
1611 if (specs
== &old
[nspecs
])
1612 /* Stack grows up, OLD was the last thing allocated;
1614 nspecs_max
+= nspecs_max
/ 2;
1617 /* Copy the old array's elements to the new space. */
1618 memcpy (specs
, old
, nspecs
* sizeof (struct printf_spec
));
1619 if (old
== &specs
[nspecs
])
1620 /* Stack grows down, OLD was just below the new
1621 SPECS. We can use that space when the new space
1623 nspecs_max
+= nspecs_max
/ 2;
1627 /* Parse the format specifier. */
1628 #ifdef COMPILE_WPRINTF
1629 nargs
+= parse_one_spec (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1631 nargs
+= parse_one_spec (f
, nargs
, &specs
[nspecs
], &max_ref_arg
,
1636 /* Determine the number of arguments the format string consumes. */
1637 nargs
= MAX (nargs
, max_ref_arg
);
1639 /* Allocate memory for the argument descriptions. */
1640 args_type
= alloca (nargs
* sizeof (int));
1641 memset (args_type
, 0, nargs
* sizeof (int));
1642 args_value
= alloca (nargs
* sizeof (union printf_arg
));
1644 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1645 still zero after this loop, format is invalid. For now we
1646 simply use 0 as the value. */
1648 /* Fill in the types of all the arguments. */
1649 for (cnt
= 0; cnt
< nspecs
; ++cnt
)
1651 /* If the width is determined by an argument this is an int. */
1652 if (specs
[cnt
].width_arg
!= -1)
1653 args_type
[specs
[cnt
].width_arg
] = PA_INT
;
1655 /* If the precision is determined by an argument this is an int. */
1656 if (specs
[cnt
].prec_arg
!= -1)
1657 args_type
[specs
[cnt
].prec_arg
] = PA_INT
;
1659 switch (specs
[cnt
].ndata_args
)
1661 case 0: /* No arguments. */
1663 case 1: /* One argument; we already have the type. */
1664 args_type
[specs
[cnt
].data_arg
] = specs
[cnt
].data_arg_type
;
1667 /* We have more than one argument for this format spec.
1668 We must call the arginfo function again to determine
1670 (void) (*__printf_arginfo_table
[specs
[cnt
].info
.spec
])
1672 specs
[cnt
].ndata_args
, &args_type
[specs
[cnt
].data_arg
]);
1677 /* Now we know all the types and the order. Fill in the argument
1679 for (cnt
= 0; cnt
< nargs
; ++cnt
)
1680 switch (args_type
[cnt
])
1682 #define T(tag, mem, type) \
1684 args_value[cnt].mem = va_arg (ap_save, type); \
1687 T (PA_CHAR
, pa_char
, int); /* Promoted. */
1688 T (PA_WCHAR
, pa_wchar
, wint_t);
1689 T (PA_INT
|PA_FLAG_SHORT
, pa_short_int
, int); /* Promoted. */
1690 T (PA_INT
, pa_int
, int);
1691 T (PA_INT
|PA_FLAG_LONG
, pa_long_int
, long int);
1692 T (PA_INT
|PA_FLAG_LONG_LONG
, pa_long_long_int
, long long int);
1693 T (PA_FLOAT
, pa_float
, double); /* Promoted. */
1694 T (PA_DOUBLE
, pa_double
, double);
1695 T (PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
, pa_long_double
, long double);
1696 T (PA_STRING
, pa_string
, const char *);
1697 T (PA_WSTRING
, pa_wstring
, const wchar_t *);
1698 T (PA_POINTER
, pa_pointer
, void *);
1701 if ((args_type
[cnt
] & PA_FLAG_PTR
) != 0)
1702 args_value
[cnt
].pa_pointer
= va_arg (ap_save
, void *);
1704 args_value
[cnt
].pa_long_double
= 0.0;
1708 /* Now walk through all format specifiers and process them. */
1709 for (; (size_t) nspecs_done
< nspecs
; ++nspecs_done
)
1712 #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
1713 # define REF(Name) &&do2_##Name - &&do_form_unknown
1715 # define REF(Name) &&do2_##Name
1718 #define LABEL(Name) do2_##Name
1724 unsigned long long int longlong
;
1725 unsigned long int word
;
1728 union printf_arg the_arg
;
1729 CHAR_T
*string
; /* Pointer to argument string. */
1731 /* Fill variables from values in struct. */
1732 int alt
= specs
[nspecs_done
].info
.alt
;
1733 int space
= specs
[nspecs_done
].info
.space
;
1734 int left
= specs
[nspecs_done
].info
.left
;
1735 int showsign
= specs
[nspecs_done
].info
.showsign
;
1736 int group
= specs
[nspecs_done
].info
.group
;
1737 int is_long_double
= specs
[nspecs_done
].info
.is_long_double
;
1738 int is_short
= specs
[nspecs_done
].info
.is_short
;
1739 int is_char
= specs
[nspecs_done
].info
.is_char
;
1740 int is_long
= specs
[nspecs_done
].info
.is_long
;
1741 int width
= specs
[nspecs_done
].info
.width
;
1742 int prec
= specs
[nspecs_done
].info
.prec
;
1743 int use_outdigits
= specs
[nspecs_done
].info
.i18n
;
1744 char pad
= specs
[nspecs_done
].info
.pad
;
1745 CHAR_T spec
= specs
[nspecs_done
].info
.spec
;
1747 /* Fill in last information. */
1748 if (specs
[nspecs_done
].width_arg
!= -1)
1750 /* Extract the field width from an argument. */
1751 specs
[nspecs_done
].info
.width
=
1752 args_value
[specs
[nspecs_done
].width_arg
].pa_int
;
1754 if (specs
[nspecs_done
].info
.width
< 0)
1755 /* If the width value is negative left justification is
1756 selected and the value is taken as being positive. */
1758 specs
[nspecs_done
].info
.width
*= -1;
1759 left
= specs
[nspecs_done
].info
.left
= 1;
1761 width
= specs
[nspecs_done
].info
.width
;
1764 if (specs
[nspecs_done
].prec_arg
!= -1)
1766 /* Extract the precision from an argument. */
1767 specs
[nspecs_done
].info
.prec
=
1768 args_value
[specs
[nspecs_done
].prec_arg
].pa_int
;
1770 if (specs
[nspecs_done
].info
.prec
< 0)
1771 /* If the precision is negative the precision is
1773 specs
[nspecs_done
].info
.prec
= -1;
1775 prec
= specs
[nspecs_done
].info
.prec
;
1778 /* Maybe the buffer is too small. */
1779 if (MAX (prec
, width
) + 32 > sizeof (work_buffer
) / sizeof (CHAR_T
))
1780 workend
= ((CHAR_T
*) alloca ((MAX (prec
, width
) + 32)
1782 + (MAX (prec
, width
) + 32));
1784 /* Process format specifiers. */
1787 JUMP (spec
, step4_jumps
);
1789 process_arg ((&specs
[nspecs_done
]));
1790 process_string_arg ((&specs
[nspecs_done
]));
1792 LABEL (form_unknown
):
1794 extern printf_function
**__printf_function_table
;
1796 printf_function
*function
;
1801 (__printf_function_table
== NULL
? NULL
:
1802 __printf_function_table
[specs
[nspecs_done
].info
.spec
]);
1804 if (function
== NULL
)
1805 function
= &printf_unknown
;
1807 ptr
= alloca (specs
[nspecs_done
].ndata_args
1808 * sizeof (const void *));
1810 /* Fill in an array of pointers to the argument values. */
1811 for (i
= 0; i
< specs
[nspecs_done
].ndata_args
; ++i
)
1812 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
1814 /* Call the function. */
1815 function_done
= (*function
) (s
, &specs
[nspecs_done
].info
, ptr
);
1817 /* If an error occurred we don't have information about #
1819 if (function_done
< 0)
1825 done
+= function_done
;
1830 /* Write the following constant string. */
1831 outstring (specs
[nspecs_done
].end_of_fmt
,
1832 specs
[nspecs_done
].next_fmt
1833 - specs
[nspecs_done
].end_of_fmt
);
1838 /* Unlock the stream. */
1840 _IO_funlockfile (s
);
1844 __libc_cleanup_region_end (0);
1849 /* Handle an unknown format specifier. This prints out a canonicalized
1850 representation of the format spec itself. */
1852 printf_unknown (FILE *s
, const struct printf_info
*info
,
1853 const void *const *args
)
1857 CHAR_T work_buffer
[MAX (info
->width
, info
->spec
) + 32];
1858 CHAR_T
*const workend
1859 = &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
1870 else if (info
->space
)
1874 if (info
->pad
== L_('0'))
1879 if (info
->width
!= 0)
1881 w
= _itoa_word (info
->width
, workend
, 10, 0);
1886 if (info
->prec
!= -1)
1889 w
= _itoa_word (info
->prec
, workend
, 10, 0);
1894 if (info
->spec
!= L_('\0'))
1895 outchar (info
->spec
);
1901 /* Group the digits according to the grouping rules of the current locale.
1902 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1905 group_number (CHAR_T
*w
, CHAR_T
*rear_ptr
, const char *grouping
,
1906 #ifdef COMPILE_WPRINTF
1907 wchar_t thousands_sep
1909 const char *thousands_sep
1915 #ifndef COMPILE_WPRINTF
1916 int tlen
= strlen (thousands_sep
);
1919 /* We treat all negative values like CHAR_MAX. */
1921 if (*grouping
== CHAR_MAX
|| *grouping
<= 0)
1922 /* No grouping should be done. */
1927 /* Copy existing string so that nothing gets overwritten. */
1928 src
= (CHAR_T
*) alloca ((rear_ptr
- w
) * sizeof (CHAR_T
));
1929 s
= (CHAR_T
*) __mempcpy (src
, w
,
1930 (rear_ptr
- w
) * sizeof (CHAR_T
));
1933 /* Process all characters in the string. */
1938 if (--len
== 0 && s
> src
)
1940 /* A new group begins. */
1941 #ifdef COMPILE_WPRINTF
1942 *--w
= thousands_sep
;
1946 *--w
= thousands_sep
[--cnt
];
1951 if (*grouping
== '\0')
1952 /* The previous grouping repeats ad infinitum. */
1954 else if (*grouping
== CHAR_MAX
1960 /* No further grouping to be done.
1961 Copy the rest of the number. */
1973 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
1976 struct _IO_FILE_plus _f
;
1977 #ifdef COMPILE_WPRINTF
1978 struct _IO_wide_data _wide_data
;
1980 _IO_FILE
*_put_stream
;
1981 #ifdef _IO_MTSAFE_IO
1987 _IO_helper_overflow (_IO_FILE
*s
, int c
)
1989 _IO_FILE
*target
= ((struct helper_file
*) s
)->_put_stream
;
1990 #ifdef COMPILE_WPRINTF
1991 int used
= s
->_wide_data
->_IO_write_ptr
- s
->_wide_data
->_IO_write_base
;
1994 _IO_size_t written
= _IO_sputn (target
, s
->_wide_data
->_IO_write_base
,
1996 s
->_wide_data
->_IO_write_ptr
-= written
;
1999 int used
= s
->_IO_write_ptr
- s
->_IO_write_base
;
2002 _IO_size_t written
= _IO_sputn (target
, s
->_IO_write_base
, used
);
2003 s
->_IO_write_ptr
-= written
;
2009 #ifdef COMPILE_WPRINTF
2010 static const struct _IO_jump_t _IO_helper_jumps
=
2013 JUMP_INIT (finish
, _IO_wdefault_finish
),
2014 JUMP_INIT (overflow
, _IO_helper_overflow
),
2015 JUMP_INIT (underflow
, _IO_default_underflow
),
2016 JUMP_INIT (uflow
, _IO_default_uflow
),
2017 JUMP_INIT (pbackfail
, (_IO_pbackfail_t
) _IO_wdefault_pbackfail
),
2018 JUMP_INIT (xsputn
, _IO_wdefault_xsputn
),
2019 JUMP_INIT (xsgetn
, _IO_wdefault_xsgetn
),
2020 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2021 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2022 JUMP_INIT (setbuf
,(_IO_setbuf_t
) _IO_wdefault_setbuf
),
2023 JUMP_INIT (sync
, _IO_default_sync
),
2024 JUMP_INIT (doallocate
, _IO_wdefault_doallocate
),
2025 JUMP_INIT (read
, _IO_default_read
),
2026 JUMP_INIT (write
, _IO_default_write
),
2027 JUMP_INIT (seek
, _IO_default_seek
),
2028 JUMP_INIT (close
, _IO_default_close
),
2029 JUMP_INIT (stat
, _IO_default_stat
)
2032 static const struct _IO_jump_t _IO_helper_jumps
=
2035 JUMP_INIT (finish
, _IO_default_finish
),
2036 JUMP_INIT (overflow
, _IO_helper_overflow
),
2037 JUMP_INIT (underflow
, _IO_default_underflow
),
2038 JUMP_INIT (uflow
, _IO_default_uflow
),
2039 JUMP_INIT (pbackfail
, _IO_default_pbackfail
),
2040 JUMP_INIT (xsputn
, _IO_default_xsputn
),
2041 JUMP_INIT (xsgetn
, _IO_default_xsgetn
),
2042 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2043 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2044 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2045 JUMP_INIT (sync
, _IO_default_sync
),
2046 JUMP_INIT (doallocate
, _IO_default_doallocate
),
2047 JUMP_INIT (read
, _IO_default_read
),
2048 JUMP_INIT (write
, _IO_default_write
),
2049 JUMP_INIT (seek
, _IO_default_seek
),
2050 JUMP_INIT (close
, _IO_default_close
),
2051 JUMP_INIT (stat
, _IO_default_stat
)
2057 buffered_vfprintf (register _IO_FILE
*s
, const CHAR_T
*format
,
2060 CHAR_T buf
[_IO_BUFSIZ
];
2061 struct helper_file helper
;
2062 register _IO_FILE
*hp
= (_IO_FILE
*) &helper
._f
;
2063 int result
, to_flush
;
2065 /* Orient the stream. */
2070 /* Initialize helper. */
2071 helper
._put_stream
= s
;
2072 #ifdef COMPILE_WPRINTF
2073 hp
->_wide_data
= &helper
._wide_data
;
2074 _IO_wsetp (hp
, buf
, buf
+ sizeof buf
/ sizeof (CHAR_T
));
2077 _IO_setp (hp
, buf
, buf
+ sizeof buf
);
2080 hp
->_IO_file_flags
= _IO_MAGIC
|_IO_NO_READS
;
2081 #if _IO_JUMPS_OFFSET
2082 hp
->_vtable_offset
= 0;
2084 #ifdef _IO_MTSAFE_IO
2085 hp
->_lock
= &helper
.lock
;
2086 __libc_lock_init (*hp
->_lock
);
2088 _IO_JUMPS (&helper
._f
) = (struct _IO_jump_t
*) &_IO_helper_jumps
;
2090 /* Now print to helper instead. */
2091 result
= vfprintf (hp
, format
, args
);
2094 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile
, s
);
2097 /* Now flush anything from the helper to the S. */
2098 #ifdef COMPILE_WPRINTF
2099 if ((to_flush
= (hp
->_wide_data
->_IO_write_ptr
2100 - hp
->_wide_data
->_IO_write_base
)) > 0)
2102 if ((int) _IO_sputn (s
, hp
->_wide_data
->_IO_write_base
, to_flush
)
2107 if ((to_flush
= hp
->_IO_write_ptr
- hp
->_IO_write_base
) > 0)
2109 if ((int) _IO_sputn (s
, hp
->_IO_write_base
, to_flush
) != to_flush
)
2114 /* Unlock the stream. */
2115 _IO_funlockfile (s
);
2116 __libc_cleanup_region_end (0);
2121 #else /* !USE_IN_LIBIO */
2125 buffered_vfprintf (register FILE *s
, const CHAR_T
*format
, va_list args
)
2130 /* Orient the stream. */
2135 s
->__bufp
= s
->__buffer
= buf
;
2136 s
->__bufsize
= sizeof buf
;
2137 s
->__put_limit
= s
->__buffer
+ s
->__bufsize
;
2138 s
->__get_limit
= s
->__buffer
;
2140 /* Now use buffer to print. */
2141 result
= vfprintf (s
, format
, args
);
2143 if (fflush (s
) == EOF
)
2145 s
->__buffer
= s
->__bufp
= s
->__get_limit
= s
->__put_limit
= NULL
;
2151 /* Pads string with given number of a specified character.
2152 This code is taken from iopadn.c of the GNU I/O library. */
2154 static const CHAR_T blanks
[PADSIZE
] =
2155 { L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '),
2156 L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' ') };
2157 static const CHAR_T zeroes
[PADSIZE
] =
2158 { L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'),
2159 L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0') };
2162 #ifndef COMPILE_WPRINTF
2163 __printf_pad (FILE *s
, char pad
, size_t count
)
2165 __wprintf_pad (FILE *s
, wchar_t pad
, size_t count
)
2168 const CHAR_T
*padptr
;
2171 padptr
= pad
== L_(' ') ? blanks
: zeroes
;
2173 for (i
= count
; i
>= PADSIZE
; i
-= PADSIZE
)
2174 if (PUT (s
, padptr
, PADSIZE
) != PADSIZE
)
2177 if (PUT (s
, padptr
, i
) != i
)
2183 #endif /* USE_IN_LIBIO */
2187 # ifdef strong_alias
2188 /* This is for glibc. */
2189 # ifdef COMPILE_WPRINTF
2190 strong_alias (_IO_vfwprintf
, vfwprintf
);
2192 strong_alias (_IO_vfprintf
, vfprintf
);
2195 # if defined __ELF__ || defined __GNU_LIBRARY__
2196 # include <gnu-stabs.h>
2198 # ifdef COMPILE_WPRINTF
2199 weak_alias (_IO_vfwprintf
, vfwprintf
);
2201 weak_alias (_IO_vfprintf
, vfprintf
);