Update.
[glibc.git] / stdio-common / vfprintf.c
bloba3f67544334d5a7d601a31653f29e37b143a0681
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. */
19 #include <ctype.h>
20 #include <limits.h>
21 #include <printf.h>
22 #include <stdarg.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <wchar.h>
27 #include <bits/libc-lock.h>
28 #include <sys/param.h>
29 #include "_itoa.h"
30 #include "_i18n_itoa.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
35 GNU libg++.
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. */
41 #ifdef USE_IN_LIBIO
42 /* This code is for use in libio. */
43 # include <libioP.h>
44 # define FILE _IO_FILE
45 # undef va_list
46 # define va_list _IO_va_list
47 # undef BUFSIZ
48 # define BUFSIZ _IO_BUFSIZ
49 # define ARGCHECK(S, Format) \
50 do \
51 { \
52 /* Check file argument for consistence. */ \
53 CHECK_FILE (S, -1); \
54 if (S->_flags & _IO_NO_WRITES) \
55 { \
56 __set_errno (EBADF); \
57 return -1; \
58 } \
59 if (Format == NULL) \
60 { \
61 MAYBE_SET_EINVAL; \
62 return -1; \
63 } \
64 } while (0)
65 # define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
67 # ifndef COMPILE_WPRINTF
68 # define vfprintf _IO_vfprintf
69 # define CHAR_T char
70 # define UCHAR_T unsigned char
71 # define INT_T int
72 # define L_(Str) Str
73 # define ISDIGIT(Ch) isdigit (Ch)
75 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
76 # define PAD(Padchar) \
77 if (width > 0) \
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)\
81 return -1
82 # else
83 # include "_itowa.h"
84 # include "_i18n_itowa.h"
86 # define vfprintf _IO_vfwprintf
87 # define CHAR_T wchar_t
88 /* This is a hack!!! There should be a type uwchar_t. */
89 # define UCHAR_T unsigned int /* uwchar_t */
90 # define INT_T wint_t
91 # define L_(Str) L##Str
92 # define ISDIGIT(Ch) iswdigit (Ch)
94 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
95 # define PAD(Padchar) \
96 if (width > 0) \
97 done += _IO_wpadn (s, (Padchar), width)
98 # define PUTC(C, F) _IO_putwc_unlocked (C, F)
99 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
101 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
102 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
103 # define _i18n_itoa(Val, Buf) _i18n_itowa (Val, Buf)
104 # define _i18n_itoa_word(Val, Buf) _i18n_itowa_word (Val, Buf)
105 # undef EOF
106 # define EOF WEOF
107 # endif
108 #else /* ! USE_IN_LIBIO */
109 /* This code is for use in the GNU C library. */
110 # include <stdio.h>
111 # define ARGCHECK(S, Format) \
112 do \
114 /* Check file argument for consistence. */ \
115 if (!__validfp (S) || !S->__mode.__write) \
117 __set_errno (EBADF); \
118 return -1; \
120 if (Format == NULL) \
122 __set_errno (EINVAL); \
123 return -1; \
125 if (!S->__seen) \
127 if (__flshfp (S, EOF) == EOF) \
128 return -1; \
131 while (0)
132 # define UNBUFFERED_P(s) ((s)->__buffer == NULL)
134 # define CHAR_T char
135 # define UCHAR_T unsigned char
136 # define INT_T int
137 # define L_(Str) Str
138 # define ISDIGIT(Ch) isdigit (Ch)
140 # define PUT(F, S, N) fwrite (S, 1, N, F)
141 ssize_t __printf_pad __P ((FILE *, char pad, size_t n));
142 # define PAD(Padchar) \
143 if (width > 0) \
144 { ssize_t __res = __printf_pad (s, (Padchar), width); \
145 if (__res == -1) \
147 done = -1; \
148 goto all_done; \
150 done += __res; }
151 # define PUTC(C, F) putc (C, F)
153 /* XXX These declarations should go as soon as the stdio header files
154 have these prototypes. */
155 extern void __flockfile (FILE *);
156 extern void __funlockfile (FILE *);
157 #endif /* USE_IN_LIBIO */
159 /* Include the shared code for parsing the format string. */
160 #include "printf-parse.h"
163 #define outchar(Ch) \
164 do \
166 register const INT_T outc = (Ch); \
167 if (PUTC (outc, s) == EOF) \
169 done = -1; \
170 goto all_done; \
172 else \
173 ++done; \
175 while (0)
177 #define outstring(String, Len) \
178 do \
180 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
182 done = -1; \
183 goto all_done; \
185 done += (Len); \
187 while (0)
189 /* For handling long_double and longlong we use the same flag. If
190 `long' and `long long' are effectively the same type define it to
191 zero. */
192 #if LONG_MAX == LONG_LONG_MAX
193 # define is_longlong 0
194 #else
195 # define is_longlong is_long_double
196 #endif
198 /* If `long' and `int' is effectively the same type we don't have to
199 handle `long separately. */
200 #if INT_MAX == LONG_MAX
201 # define is_long_num 0
202 #else
203 # define is_long_num is_long
204 #endif
207 /* Global variables. */
208 static const CHAR_T null[] = L_("(null)");
211 /* Helper function to provide temporary buffering for unbuffered streams. */
212 static int buffered_vfprintf __P ((FILE *stream, const CHAR_T *fmt, va_list))
213 internal_function;
215 /* Handle unknown format specifier. */
216 static int printf_unknown __P ((FILE *, const struct printf_info *,
217 const void *const *));
219 /* Group digits of number string. */
220 #ifdef COMPILE_WPRINTF
221 static CHAR_T *group_number __P ((CHAR_T *, CHAR_T *, const char *, wchar_t))
222 internal_function;
223 #else
224 static CHAR_T *group_number __P ((CHAR_T *, CHAR_T *, const char *,
225 const char *)) internal_function;
226 #endif
229 /* The function itself. */
231 vfprintf (FILE *s, const CHAR_T *format, va_list ap)
233 /* The character used as thousands separator. */
234 #ifdef COMPILE_WPRINTF
235 wchar_t thousands_sep = L'\0';
236 #else
237 const char *thousands_sep = NULL;
238 #endif
240 /* The string describing the size of groups of digits. */
241 const char *grouping;
243 /* Place to accumulate the result. */
244 int done;
246 /* Current character in format string. */
247 const UCHAR_T *f;
249 /* End of leading constant string. */
250 const UCHAR_T *lead_str_end;
252 /* Points to next format specifier. */
253 const UCHAR_T *end_of_spec;
255 /* Buffer intermediate results. */
256 CHAR_T work_buffer[1000];
257 CHAR_T *workend;
259 /* State for restartable multibyte character handling functions. */
260 #ifndef COMPILE_WPRINTF
261 mbstate_t mbstate;
262 #endif
264 /* We have to save the original argument pointer. */
265 va_list ap_save;
267 /* Count number of specifiers we already processed. */
268 int nspecs_done;
270 /* For the %m format we may need the current `errno' value. */
271 int save_errno = errno;
274 /* This table maps a character into a number representing a
275 class. In each step there is a destination label for each
276 class. */
277 static const int jump_table[] =
279 /* ' ' */ 1, 0, 0, /* '#' */ 4,
280 0, /* '%' */ 14, 0, /* '\''*/ 6,
281 0, 0, /* '*' */ 7, /* '+' */ 2,
282 0, /* '-' */ 3, /* '.' */ 9, 0,
283 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
284 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
285 /* '8' */ 8, /* '9' */ 8, 0, 0,
286 0, 0, 0, 0,
287 0, /* 'A' */ 26, 0, /* 'C' */ 25,
288 0, /* 'E' */ 19, 0, /* 'G' */ 19,
289 0, /* 'I' */ 29, 0, 0,
290 /* 'L' */ 12, 0, 0, 0,
291 0, 0, 0, /* 'S' */ 21,
292 0, 0, 0, 0,
293 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
294 0, 0, 0, 0,
295 0, /* 'a' */ 26, 0, /* 'c' */ 20,
296 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
297 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
298 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
299 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
300 /* 't' */ 27, /* 'u' */ 16, 0, 0,
301 /* 'x' */ 18, 0, /* 'z' */ 13
304 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
305 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
306 #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
307 /* 'int' is enough and it saves some space on 64 bit systems. */
308 # define JUMP_TABLE_TYPE const int
309 # define JUMP(ChExpr, table) \
310 do \
312 int offset; \
313 void *ptr; \
314 spec = (ChExpr); \
315 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
316 : table[CHAR_CLASS (spec)]; \
317 ptr = &&do_form_unknown + offset; \
318 goto *ptr; \
320 while (0)
321 #else
322 # define JUMP_TABLE_TYPE const void *const
323 # define JUMP(ChExpr, table) \
324 do \
326 const void *ptr; \
327 spec = (ChExpr); \
328 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
329 : table[CHAR_CLASS (spec)]; \
330 goto *ptr; \
332 while (0)
333 #endif
335 #define STEP0_3_TABLE \
336 /* Step 0: at the beginning. */ \
337 static JUMP_TABLE_TYPE step0_jumps[30] = \
339 REF (form_unknown), \
340 REF (flag_space), /* for ' ' */ \
341 REF (flag_plus), /* for '+' */ \
342 REF (flag_minus), /* for '-' */ \
343 REF (flag_hash), /* for '<hash>' */ \
344 REF (flag_zero), /* for '0' */ \
345 REF (flag_quote), /* for '\'' */ \
346 REF (width_asterics), /* for '*' */ \
347 REF (width), /* for '1'...'9' */ \
348 REF (precision), /* for '.' */ \
349 REF (mod_half), /* for 'h' */ \
350 REF (mod_long), /* for 'l' */ \
351 REF (mod_longlong), /* for 'L', 'q' */ \
352 REF (mod_size_t), /* for 'z', 'Z' */ \
353 REF (form_percent), /* for '%' */ \
354 REF (form_integer), /* for 'd', 'i' */ \
355 REF (form_unsigned), /* for 'u' */ \
356 REF (form_octal), /* for 'o' */ \
357 REF (form_hexa), /* for 'X', 'x' */ \
358 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
359 REF (form_character), /* for 'c' */ \
360 REF (form_string), /* for 's', 'S' */ \
361 REF (form_pointer), /* for 'p' */ \
362 REF (form_number), /* for 'n' */ \
363 REF (form_strerror), /* for 'm' */ \
364 REF (form_wcharacter), /* for 'C' */ \
365 REF (form_floathex), /* for 'A', 'a' */ \
366 REF (mod_ptrdiff_t), /* for 't' */ \
367 REF (mod_intmax_t), /* for 'j' */ \
368 REF (flag_i18n), /* for 'I' */ \
369 }; \
370 /* Step 1: after processing width. */ \
371 static JUMP_TABLE_TYPE step1_jumps[30] = \
373 REF (form_unknown), \
374 REF (form_unknown), /* for ' ' */ \
375 REF (form_unknown), /* for '+' */ \
376 REF (form_unknown), /* for '-' */ \
377 REF (form_unknown), /* for '<hash>' */ \
378 REF (form_unknown), /* for '0' */ \
379 REF (form_unknown), /* for '\'' */ \
380 REF (form_unknown), /* for '*' */ \
381 REF (form_unknown), /* for '1'...'9' */ \
382 REF (precision), /* for '.' */ \
383 REF (mod_half), /* for 'h' */ \
384 REF (mod_long), /* for 'l' */ \
385 REF (mod_longlong), /* for 'L', 'q' */ \
386 REF (mod_size_t), /* for 'z', 'Z' */ \
387 REF (form_percent), /* for '%' */ \
388 REF (form_integer), /* for 'd', 'i' */ \
389 REF (form_unsigned), /* for 'u' */ \
390 REF (form_octal), /* for 'o' */ \
391 REF (form_hexa), /* for 'X', 'x' */ \
392 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
393 REF (form_character), /* for 'c' */ \
394 REF (form_string), /* for 's', 'S' */ \
395 REF (form_pointer), /* for 'p' */ \
396 REF (form_number), /* for 'n' */ \
397 REF (form_strerror), /* for 'm' */ \
398 REF (form_wcharacter), /* for 'C' */ \
399 REF (form_floathex), /* for 'A', 'a' */ \
400 REF (mod_ptrdiff_t), /* for 't' */ \
401 REF (mod_intmax_t), /* for 'j' */ \
402 REF (flag_i18n) /* for 'I' */ \
403 }; \
404 /* Step 2: after processing precision. */ \
405 static JUMP_TABLE_TYPE step2_jumps[30] = \
407 REF (form_unknown), \
408 REF (form_unknown), /* for ' ' */ \
409 REF (form_unknown), /* for '+' */ \
410 REF (form_unknown), /* for '-' */ \
411 REF (form_unknown), /* for '<hash>' */ \
412 REF (form_unknown), /* for '0' */ \
413 REF (form_unknown), /* for '\'' */ \
414 REF (form_unknown), /* for '*' */ \
415 REF (form_unknown), /* for '1'...'9' */ \
416 REF (form_unknown), /* for '.' */ \
417 REF (mod_half), /* for 'h' */ \
418 REF (mod_long), /* for 'l' */ \
419 REF (mod_longlong), /* for 'L', 'q' */ \
420 REF (mod_size_t), /* for 'z', 'Z' */ \
421 REF (form_percent), /* for '%' */ \
422 REF (form_integer), /* for 'd', 'i' */ \
423 REF (form_unsigned), /* for 'u' */ \
424 REF (form_octal), /* for 'o' */ \
425 REF (form_hexa), /* for 'X', 'x' */ \
426 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
427 REF (form_character), /* for 'c' */ \
428 REF (form_string), /* for 's', 'S' */ \
429 REF (form_pointer), /* for 'p' */ \
430 REF (form_number), /* for 'n' */ \
431 REF (form_strerror), /* for 'm' */ \
432 REF (form_wcharacter), /* for 'C' */ \
433 REF (form_floathex), /* for 'A', 'a' */ \
434 REF (mod_ptrdiff_t), /* for 't' */ \
435 REF (mod_intmax_t), /* for 'j' */ \
436 REF (flag_i18n) /* for 'I' */ \
437 }; \
438 /* Step 3a: after processing first 'h' modifier. */ \
439 static JUMP_TABLE_TYPE step3a_jumps[30] = \
441 REF (form_unknown), \
442 REF (form_unknown), /* for ' ' */ \
443 REF (form_unknown), /* for '+' */ \
444 REF (form_unknown), /* for '-' */ \
445 REF (form_unknown), /* for '<hash>' */ \
446 REF (form_unknown), /* for '0' */ \
447 REF (form_unknown), /* for '\'' */ \
448 REF (form_unknown), /* for '*' */ \
449 REF (form_unknown), /* for '1'...'9' */ \
450 REF (form_unknown), /* for '.' */ \
451 REF (mod_halfhalf), /* for 'h' */ \
452 REF (form_unknown), /* for 'l' */ \
453 REF (form_unknown), /* for 'L', 'q' */ \
454 REF (form_unknown), /* for 'z', 'Z' */ \
455 REF (form_percent), /* for '%' */ \
456 REF (form_integer), /* for 'd', 'i' */ \
457 REF (form_unsigned), /* for 'u' */ \
458 REF (form_octal), /* for 'o' */ \
459 REF (form_hexa), /* for 'X', 'x' */ \
460 REF (form_unknown), /* for 'E', 'e', 'f', 'G', 'g' */ \
461 REF (form_unknown), /* for 'c' */ \
462 REF (form_unknown), /* for 's', 'S' */ \
463 REF (form_unknown), /* for 'p' */ \
464 REF (form_number), /* for 'n' */ \
465 REF (form_unknown), /* for 'm' */ \
466 REF (form_unknown), /* for 'C' */ \
467 REF (form_unknown), /* for 'A', 'a' */ \
468 REF (form_unknown), /* for 't' */ \
469 REF (form_unknown), /* for 'j' */ \
470 REF (form_unknown) /* for 'I' */ \
471 }; \
472 /* Step 3b: after processing first 'l' modifier. */ \
473 static JUMP_TABLE_TYPE step3b_jumps[30] = \
475 REF (form_unknown), \
476 REF (form_unknown), /* for ' ' */ \
477 REF (form_unknown), /* for '+' */ \
478 REF (form_unknown), /* for '-' */ \
479 REF (form_unknown), /* for '<hash>' */ \
480 REF (form_unknown), /* for '0' */ \
481 REF (form_unknown), /* for '\'' */ \
482 REF (form_unknown), /* for '*' */ \
483 REF (form_unknown), /* for '1'...'9' */ \
484 REF (form_unknown), /* for '.' */ \
485 REF (form_unknown), /* for 'h' */ \
486 REF (mod_longlong), /* for 'l' */ \
487 REF (form_unknown), /* for 'L', 'q' */ \
488 REF (form_unknown), /* for 'z', 'Z' */ \
489 REF (form_percent), /* for '%' */ \
490 REF (form_integer), /* for 'd', 'i' */ \
491 REF (form_unsigned), /* for 'u' */ \
492 REF (form_octal), /* for 'o' */ \
493 REF (form_hexa), /* for 'X', 'x' */ \
494 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
495 REF (form_character), /* for 'c' */ \
496 REF (form_string), /* for 's', 'S' */ \
497 REF (form_pointer), /* for 'p' */ \
498 REF (form_number), /* for 'n' */ \
499 REF (form_strerror), /* for 'm' */ \
500 REF (form_wcharacter), /* for 'C' */ \
501 REF (form_floathex), /* for 'A', 'a' */ \
502 REF (form_unknown), /* for 't' */ \
503 REF (form_unknown), /* for 'j' */ \
504 REF (form_unknown) /* for 'I' */ \
507 #define STEP4_TABLE \
508 /* Step 4: processing format specifier. */ \
509 static JUMP_TABLE_TYPE step4_jumps[30] = \
511 REF (form_unknown), \
512 REF (form_unknown), /* for ' ' */ \
513 REF (form_unknown), /* for '+' */ \
514 REF (form_unknown), /* for '-' */ \
515 REF (form_unknown), /* for '<hash>' */ \
516 REF (form_unknown), /* for '0' */ \
517 REF (form_unknown), /* for '\'' */ \
518 REF (form_unknown), /* for '*' */ \
519 REF (form_unknown), /* for '1'...'9' */ \
520 REF (form_unknown), /* for '.' */ \
521 REF (form_unknown), /* for 'h' */ \
522 REF (form_unknown), /* for 'l' */ \
523 REF (form_unknown), /* for 'L', 'q' */ \
524 REF (form_unknown), /* for 'z', 'Z' */ \
525 REF (form_percent), /* for '%' */ \
526 REF (form_integer), /* for 'd', 'i' */ \
527 REF (form_unsigned), /* for 'u' */ \
528 REF (form_octal), /* for 'o' */ \
529 REF (form_hexa), /* for 'X', 'x' */ \
530 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
531 REF (form_character), /* for 'c' */ \
532 REF (form_string), /* for 's', 'S' */ \
533 REF (form_pointer), /* for 'p' */ \
534 REF (form_number), /* for 'n' */ \
535 REF (form_strerror), /* for 'm' */ \
536 REF (form_wcharacter), /* for 'C' */ \
537 REF (form_floathex), /* for 'A', 'a' */ \
538 REF (form_unknown), /* for 't' */ \
539 REF (form_unknown), /* for 'j' */ \
540 REF (form_unknown) /* for 'I' */ \
544 #define process_arg(fspec) \
545 /* Start real work. We know about all flags and modifiers and \
546 now process the wanted format specifier. */ \
547 LABEL (form_percent): \
548 /* Write a literal "%". */ \
549 outchar (L_('%')); \
550 break; \
552 LABEL (form_integer): \
553 /* Signed decimal integer. */ \
554 base = 10; \
556 if (is_longlong) \
558 long long int signed_number; \
560 if (fspec == NULL) \
561 signed_number = va_arg (ap, long long int); \
562 else \
563 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
565 is_negative = signed_number < 0; \
566 number.longlong = is_negative ? (- signed_number) : signed_number; \
568 goto LABEL (longlong_number); \
570 else \
572 long int signed_number; \
574 if (fspec == NULL) \
576 if (is_long_num) \
577 signed_number = va_arg (ap, long int); \
578 else /* `char' and `short int' will be promoted to `int'. */ \
579 signed_number = va_arg (ap, int); \
581 else \
582 if (is_long_num) \
583 signed_number = args_value[fspec->data_arg].pa_long_int; \
584 else \
585 signed_number = args_value[fspec->data_arg].pa_int; \
587 is_negative = signed_number < 0; \
588 number.word = is_negative ? (- signed_number) : signed_number; \
590 goto LABEL (number); \
592 /* NOTREACHED */ \
594 LABEL (form_unsigned): \
595 /* Unsigned decimal integer. */ \
596 base = 10; \
597 goto LABEL (unsigned_number); \
598 /* NOTREACHED */ \
600 LABEL (form_octal): \
601 /* Unsigned octal integer. */ \
602 base = 8; \
603 goto LABEL (unsigned_number); \
604 /* NOTREACHED */ \
606 LABEL (form_hexa): \
607 /* Unsigned hexadecimal integer. */ \
608 base = 16; \
610 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
612 /* ISO specifies the `+' and ` ' flags only for signed \
613 conversions. */ \
614 is_negative = 0; \
615 showsign = 0; \
616 space = 0; \
618 if (is_longlong) \
620 if (fspec == NULL) \
621 number.longlong = va_arg (ap, unsigned long long int); \
622 else \
623 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
625 LABEL (longlong_number): \
626 if (prec < 0) \
627 /* Supply a default precision if none was given. */ \
628 prec = 1; \
629 else \
630 /* We have to take care for the '0' flag. If a precision \
631 is given it must be ignored. */ \
632 pad = L_(' '); \
634 /* If the precision is 0 and the number is 0 nothing has to \
635 be written for the number, except for the 'o' format in \
636 alternate form. */ \
637 if (prec == 0 && number.longlong == 0) \
639 string = workend; \
640 if (base == 8 && alt) \
641 *string-- = L_('0'); \
643 else \
645 /* Put the number in WORK. */ \
646 if (use_outdigits && base == 10) \
647 string = _i18n_itoa (number.longlong, workend + 1); \
648 else \
649 string = _itoa (number.longlong, workend + 1, base, \
650 spec == L_('X')); \
651 string -= 1; \
652 if (group && grouping) \
653 string = group_number (string, workend, grouping, \
654 thousands_sep); \
656 /* Simplify further test for num != 0. */ \
657 number.word = number.longlong != 0; \
659 else \
661 if (fspec == NULL) \
663 if (is_long_num) \
664 number.word = va_arg (ap, unsigned long int); \
665 else if (!is_short) \
666 number.word = va_arg (ap, unsigned int); \
667 else \
668 number.word = (unsigned short int) va_arg (ap, unsigned int); \
670 else \
671 if (is_long_num) \
672 number.word = args_value[fspec->data_arg].pa_u_long_int; \
673 else if (is_char) \
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; \
678 else \
679 number.word = (unsigned short int) \
680 args_value[fspec->data_arg].pa_u_short_int; \
682 LABEL (number): \
683 if (prec < 0) \
684 /* Supply a default precision if none was given. */ \
685 prec = 1; \
686 else \
687 /* We have to take care for the '0' flag. If a precision \
688 is given it must be ignored. */ \
689 pad = L_(' '); \
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 \
693 alternate form. */ \
694 if (prec == 0 && number.word == 0) \
696 string = workend; \
697 if (base == 8 && alt) \
698 *string-- = L_('0'); \
700 else \
702 /* Put the number in WORK. */ \
703 if (use_outdigits && base == 10) \
704 string = _i18n_itoa_word (number.word, workend + 1); \
705 else \
706 string = _itoa_word (number.word, workend + 1, base, \
707 spec == L_('X')); \
708 string -= 1; \
709 if (group && grouping) \
710 string = group_number (string, workend, grouping, \
711 thousands_sep); \
715 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
716 /* Add octal marker. */ \
717 *string-- = L_('0'); \
719 prec = MAX (0, prec - (workend - string)); \
721 if (!left) \
723 width -= workend - string + prec; \
725 if (number.word != 0 && alt && base == 16) \
726 /* Account for 0X hex marker. */ \
727 width -= 2; \
729 if (is_negative || showsign || space) \
730 --width; \
732 if (pad == L_(' ')) \
734 PAD (L_(' ')); \
735 width = 0; \
738 if (is_negative) \
739 outchar (L_('-')); \
740 else if (showsign) \
741 outchar (L_('+')); \
742 else if (space) \
743 outchar (L_(' ')); \
745 if (number.word != 0 && alt && base == 16) \
747 outchar (L_('0')); \
748 outchar (spec); \
751 width += prec; \
752 PAD (L_('0')); \
754 outstring (string + 1, workend - string); \
756 break; \
758 else \
760 if (is_negative) \
762 outchar (L_('-')); \
763 --width; \
765 else if (showsign) \
767 outchar (L_('+')); \
768 --width; \
770 else if (space) \
772 outchar (L_(' ')); \
773 --width; \
776 if (number.word != 0 && alt && base == 16) \
778 outchar (L_('0')); \
779 outchar (spec); \
780 width -= 2; \
783 width -= workend - string + prec; \
785 if (prec > 0) \
787 int temp = width; \
788 width = prec; \
789 PAD (L_('0'));; \
790 width = temp; \
793 outstring (string + 1, workend - string); \
795 PAD (L_(' ')); \
796 break; \
799 LABEL (form_float): \
801 /* Floating-point number. This is handled by printf_fp.c. */ \
802 extern int __printf_fp __P ((FILE *, const struct printf_info *, \
803 const void **const)); \
804 const void *ptr; \
805 int function_done; \
807 if (fspec == NULL) \
809 struct printf_info info = { prec: prec, \
810 width: width, \
811 spec: spec, \
812 is_long_double: is_long_double, \
813 is_short: is_short, \
814 is_long: is_long, \
815 alt: alt, \
816 space: space, \
817 left: left, \
818 showsign: showsign, \
819 group: group, \
820 pad: pad, \
821 extra: 0, \
822 wide: sizeof (CHAR_T) != 1 }; \
824 if (is_long_double) \
825 the_arg.pa_long_double = va_arg (ap, long double); \
826 else \
827 the_arg.pa_double = va_arg (ap, double); \
828 ptr = (const void *) &the_arg; \
830 function_done = __printf_fp (s, &info, &ptr); \
832 else \
834 ptr = (const void *) &args_value[fspec->data_arg]; \
836 function_done = __printf_fp (s, &fspec->info, &ptr); \
839 if (function_done < 0) \
841 /* Error in print handler. */ \
842 done = -1; \
843 goto all_done; \
846 done += function_done; \
848 break; \
850 LABEL (form_floathex): \
852 /* FLoating point number printed as hexadecimal number. */ \
853 extern int __printf_fphex __P ((FILE *, const struct printf_info *, \
854 const void **const)); \
855 const void *ptr; \
856 int function_done; \
858 if (fspec == NULL) \
860 struct printf_info info = { prec: prec, \
861 width: width, \
862 spec: spec, \
863 is_long_double: is_long_double, \
864 is_short: is_short, \
865 is_long: is_long, \
866 alt: alt, \
867 space: space, \
868 left: left, \
869 showsign: showsign, \
870 group: group, \
871 pad: pad, \
872 extra: 0, \
873 wide: sizeof (CHAR_T) != 1 }; \
875 if (is_long_double) \
876 the_arg.pa_long_double = va_arg (ap, long double); \
877 else \
878 the_arg.pa_double = va_arg (ap, double); \
879 ptr = (const void *) &the_arg; \
881 function_done = __printf_fphex (s, &info, &ptr); \
883 else \
885 ptr = (const void *) &args_value[fspec->data_arg]; \
887 function_done = __printf_fphex (s, &fspec->info, &ptr); \
890 if (function_done < 0) \
892 /* Error in print handler. */ \
893 done = -1; \
894 goto all_done; \
897 done += function_done; \
899 break; \
901 LABEL (form_pointer): \
902 /* Generic pointer. */ \
904 const void *ptr; \
905 if (fspec == NULL) \
906 ptr = va_arg (ap, void *); \
907 else \
908 ptr = args_value[fspec->data_arg].pa_pointer; \
909 if (ptr != NULL) \
911 /* If the pointer is not NULL, write it as a %#x spec. */ \
912 base = 16; \
913 number.word = (unsigned long int) ptr; \
914 is_negative = 0; \
915 alt = 1; \
916 group = 0; \
917 spec = L_('x'); \
918 goto LABEL (number); \
920 else \
922 /* Write "(nil)" for a nil pointer. */ \
923 string = (CHAR_T *) L_("(nil)"); \
924 /* Make sure the full string "(nil)" is printed. */ \
925 if (prec < 5) \
926 prec = 5; \
927 is_long = 0; /* This is no wide-char string. */ \
928 goto LABEL (print_string); \
931 /* NOTREACHED */ \
933 LABEL (form_number): \
934 /* Answer the count of characters written. */ \
935 if (fspec == NULL) \
937 if (is_longlong) \
938 *(long long int *) va_arg (ap, void *) = done; \
939 else if (is_long_num) \
940 *(long int *) va_arg (ap, void *) = done; \
941 else if (!is_short) \
942 *(int *) va_arg (ap, void *) = done; \
943 else \
944 *(short int *) va_arg (ap, void *) = done; \
946 else \
947 if (is_longlong) \
948 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
949 else if (is_long_num) \
950 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
951 else if (!is_short) \
952 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
953 else \
954 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
955 break; \
957 LABEL (form_strerror): \
958 /* Print description of error ERRNO. */ \
959 string = \
960 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
961 sizeof work_buffer); \
962 is_long = 0; /* This is no wide-char string. */ \
963 goto LABEL (print_string)
965 #ifdef COMPILE_WPRINTF
966 # define process_string_arg(fspec) \
967 LABEL (form_character): \
968 /* Character. */ \
969 if (is_long) \
970 goto LABEL (form_wcharacter); \
971 --width; /* Account for the character itself. */ \
972 if (!left) \
973 PAD (L' '); \
974 if (fspec == NULL) \
975 outchar (btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
976 else \
977 outchar (btowc ((unsigned char) args_value[fspec->data_arg].pa_char));\
978 if (left) \
979 PAD (L' '); \
980 break; \
982 LABEL (form_wcharacter): \
984 /* Wide character. */ \
985 --width; \
986 if (!left) \
987 PAD (L' '); \
988 if (fspec == NULL) \
989 outchar (va_arg (ap, wint_t)); \
990 else \
991 outchar (args_value[fspec->data_arg].pa_wchar); \
992 if (left) \
993 PAD (L' '); \
995 break; \
997 LABEL (form_string): \
999 size_t len; \
1001 /* The string argument could in fact be `char *' or `wchar_t *'. \
1002 But this should not make a difference here. */ \
1003 if (fspec == NULL) \
1004 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
1005 else \
1006 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
1008 /* Entry point for printing other strings. */ \
1009 LABEL (print_string): \
1011 if (string == NULL) \
1013 /* Write "(null)" if there's space. */ \
1014 if (prec == -1 \
1015 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
1017 string = (CHAR_T *) null; \
1018 len = (sizeof (null) / sizeof (null[0])) - 1; \
1020 else \
1022 string = (CHAR_T *) L""; \
1023 len = 0; \
1026 else if (!is_long && spec != L_('S')) \
1028 /* This is complicated. We have to transform the multibyte \
1029 string into a wide character string. */ \
1030 const char *mbs = (const char *) string; \
1031 mbstate_t mbstate; \
1033 len = prec == -1 ? strnlen (mbs, prec) : strlen (mbs); \
1035 /* Allocate dynamically an array which definitely is long \
1036 enough for the wide character version. */ \
1037 string = (CHAR_T *) alloca ((len + 1) * sizeof (wchar_t)); \
1039 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1040 len = __mbsrtowcs (string, &mbs, len + 1, &mbstate); \
1041 if (len == (size_t) -1) \
1043 /* Illegal multibyte character. */ \
1044 done = -1; \
1045 goto all_done; \
1048 else \
1050 if (prec != -1) \
1051 /* Search for the end of the string, but don't search past \
1052 the length specified by the precision. */ \
1053 len = __wcsnlen (string, prec); \
1054 else \
1055 len = __wcslen (string); \
1058 if ((width -= len) < 0) \
1060 outstring (string, len); \
1061 break; \
1064 if (!left) \
1065 PAD (L' '); \
1066 outstring (string, len); \
1067 if (left) \
1068 PAD (L' '); \
1070 break;
1071 #else
1072 # define process_string_arg(fspec) \
1073 LABEL (form_character): \
1074 /* Character. */ \
1075 if (is_long) \
1076 goto LABEL (form_wcharacter); \
1077 --width; /* Account for the character itself. */ \
1078 if (!left) \
1079 PAD (' '); \
1080 if (fspec == NULL) \
1081 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1082 else \
1083 outchar ((unsigned char) args_value[fspec->data_arg].pa_char); \
1084 if (left) \
1085 PAD (' '); \
1086 break; \
1088 LABEL (form_wcharacter): \
1090 /* Wide character. */ \
1091 char buf[MB_CUR_MAX]; \
1092 mbstate_t mbstate; \
1093 size_t len; \
1095 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1096 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wint_t) \
1097 : args_value[fspec->data_arg].pa_wchar), \
1098 &mbstate); \
1099 width -= len; \
1100 if (!left) \
1101 PAD (' '); \
1102 outstring (buf, len); \
1103 if (left) \
1104 PAD (' '); \
1106 break; \
1108 LABEL (form_string): \
1110 size_t len; \
1112 /* The string argument could in fact be `char *' or `wchar_t *'. \
1113 But this should not make a difference here. */ \
1114 if (fspec == NULL) \
1115 string = (char *) va_arg (ap, const char *); \
1116 else \
1117 string = (char *) args_value[fspec->data_arg].pa_string; \
1119 /* Entry point for printing other strings. */ \
1120 LABEL (print_string): \
1122 if (string == NULL) \
1124 /* Write "(null)" if there's space. */ \
1125 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1127 string = (char *) null; \
1128 len = sizeof (null) - 1; \
1130 else \
1132 string = (char *) ""; \
1133 len = 0; \
1136 else if (!is_long && spec != L_('S')) \
1138 if (prec != -1) \
1139 /* Search for the end of the string, but don't search past \
1140 the length specified by the precision. */ \
1141 len = __strnlen (string, prec); \
1142 else \
1143 len = strlen (string); \
1145 else \
1147 const wchar_t *s2 = (const wchar_t *) string; \
1148 mbstate_t mbstate; \
1150 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1152 if (prec > 0) \
1154 /* The string `s2' might not be NUL terminated. */ \
1155 string = (char *) alloca (prec); \
1156 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1158 else \
1160 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1161 if (len != (size_t) -1) \
1163 assert (__mbsinit (&mbstate)); \
1164 s2 = (const wchar_t *) string; \
1165 string = (char *) alloca (len + 1); \
1166 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1170 if (len == (size_t) -1) \
1172 /* Illegal wide-character string. */ \
1173 done = -1; \
1174 goto all_done; \
1178 if ((width -= len) < 0) \
1180 outstring (string, len); \
1181 break; \
1184 if (!left) \
1185 PAD (' '); \
1186 outstring (string, len); \
1187 if (left) \
1188 PAD (' '); \
1190 break;
1191 #endif
1193 /* Orient the stream. */
1194 #ifdef ORIENT
1195 ORIENT;
1196 #endif
1198 /* Sanity check of arguments. */
1199 ARGCHECK (s, format);
1201 #ifdef ORIENT
1202 /* Check for correct orientation. */
1203 if (
1204 # ifdef USE_IN_LIBIO
1205 s->_vtable_offset == 0 &&
1206 # endif
1207 _IO_fwide (s, sizeof (CHAR_T) == 1 ? -1 : 1)
1208 != (sizeof (CHAR_T) == 1 ? -1 : 1))
1209 /* The stream is already oriented otherwise. */
1210 return EOF;
1211 #endif
1213 if (UNBUFFERED_P (s))
1214 /* Use a helper function which will allocate a local temporary buffer
1215 for the stream and then call us again. */
1216 return buffered_vfprintf (s, format, ap);
1218 /* Initialize local variables. */
1219 done = 0;
1220 grouping = (const char *) -1;
1221 #ifdef __va_copy
1222 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1223 since on some systems `va_list' is not an integral type. */
1224 __va_copy (ap_save, ap);
1225 #else
1226 ap_save = ap;
1227 #endif
1228 nspecs_done = 0;
1230 #ifdef COMPILE_WPRINTF
1231 /* Find the first format specifier. */
1232 f = lead_str_end = find_spec ((const UCHAR_T *) format);
1233 #else
1234 /* Put state for processing format string in initial state. */
1235 memset (&mbstate, '\0', sizeof (mbstate_t));
1237 /* Find the first format specifier. */
1238 f = lead_str_end = find_spec (format, &mbstate);
1239 #endif
1241 /* Lock stream. */
1242 #ifdef USE_IN_LIBIO
1243 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
1244 _IO_flockfile (s);
1245 #else
1246 __libc_cleanup_region_start ((void (*) (void *)) &__funlockfile, s);
1247 __flockfile (s);
1248 #endif
1250 /* Write the literal text before the first format. */
1251 outstring ((const UCHAR_T *) format,
1252 lead_str_end - (const UCHAR_T *) format);
1254 /* If we only have to print a simple string, return now. */
1255 if (*f == L_('\0'))
1256 goto all_done;
1258 /* Process whole format string. */
1261 #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
1262 # define REF(Name) &&do_##Name - &&do_form_unknown
1263 #else
1264 # define REF(Name) &&do_##Name
1265 #endif
1266 #define LABEL(Name) do_##Name
1267 STEP0_3_TABLE;
1268 STEP4_TABLE;
1270 union printf_arg *args_value; /* This is not used here but ... */
1271 int is_negative; /* Flag for negative number. */
1272 union
1274 unsigned long long int longlong;
1275 unsigned long int word;
1276 } number;
1277 int base;
1278 union printf_arg the_arg;
1279 CHAR_T *string; /* Pointer to argument string. */
1280 int alt = 0; /* Alternate format. */
1281 int space = 0; /* Use space prefix if no sign is needed. */
1282 int left = 0; /* Left-justify output. */
1283 int showsign = 0; /* Always begin with plus or minus sign. */
1284 int group = 0; /* Print numbers according grouping rules. */
1285 int is_long_double = 0; /* Argument is long double/ long long int. */
1286 int is_short = 0; /* Argument is short int. */
1287 int is_long = 0; /* Argument is long int. */
1288 int is_char = 0; /* Argument is promoted (unsigned) char. */
1289 int width = 0; /* Width of output; 0 means none specified. */
1290 int prec = -1; /* Precision of output; -1 means none specified. */
1291 /* This flag is set by the 'I' modifier and selects the use of the
1292 `outdigits' as determined by the current locale. */
1293 int use_outdigits = 0;
1294 UCHAR_T pad = L_(' ');/* Padding character. */
1295 CHAR_T spec;
1297 workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T) - 1];
1299 /* Get current character in format string. */
1300 JUMP (*++f, step0_jumps);
1302 /* ' ' flag. */
1303 LABEL (flag_space):
1304 space = 1;
1305 JUMP (*++f, step0_jumps);
1307 /* '+' flag. */
1308 LABEL (flag_plus):
1309 showsign = 1;
1310 JUMP (*++f, step0_jumps);
1312 /* The '-' flag. */
1313 LABEL (flag_minus):
1314 left = 1;
1315 pad = L_(' ');
1316 JUMP (*++f, step0_jumps);
1318 /* The '#' flag. */
1319 LABEL (flag_hash):
1320 alt = 1;
1321 JUMP (*++f, step0_jumps);
1323 /* The '0' flag. */
1324 LABEL (flag_zero):
1325 if (!left)
1326 pad = L_('0');
1327 JUMP (*++f, step0_jumps);
1329 /* The '\'' flag. */
1330 LABEL (flag_quote):
1331 group = 1;
1333 if (grouping == (const char *) -1)
1335 #ifdef COMPILE_WPRINTF
1336 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1337 _NL_NUMERIC_THOUSANDS_SEP_WC);
1338 #else
1339 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1340 #endif
1342 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1343 if (*grouping == '\0' || *grouping == CHAR_MAX
1344 #ifdef COMPILE_WPRINTF
1345 || thousands_sep == L'\0'
1346 #else
1347 || *thousands_sep == '\0'
1348 #endif
1350 grouping = NULL;
1352 JUMP (*++f, step0_jumps);
1354 LABEL (flag_i18n):
1355 use_outdigits = 1;
1356 break;
1358 /* Get width from argument. */
1359 LABEL (width_asterics):
1361 const UCHAR_T *tmp; /* Temporary value. */
1363 tmp = ++f;
1364 if (ISDIGIT (*tmp) && read_int (&tmp) && *tmp == L_('$'))
1365 /* The width comes from a positional parameter. */
1366 goto do_positional;
1368 width = va_arg (ap, int);
1370 /* Negative width means left justified. */
1371 if (width < 0)
1373 width = -width;
1374 pad = L_(' ');
1375 left = 1;
1378 if (width + 32 >= sizeof (work_buffer) / sizeof (work_buffer[0]))
1379 /* We have to use a special buffer. The "32" is just a safe
1380 bet for all the output which is not counted in the width. */
1381 workend = ((CHAR_T *) alloca ((width + 32) * sizeof (CHAR_T))
1382 + (width + 31));
1384 JUMP (*f, step1_jumps);
1386 /* Given width in format string. */
1387 LABEL (width):
1388 width = read_int (&f);
1390 if (width + 32 >= sizeof (work_buffer) / sizeof (work_buffer[0]))
1391 /* We have to use a special buffer. The "32" is just a safe
1392 bet for all the output which is not counted in the width. */
1393 workend = ((CHAR_T *) alloca ((width + 32) * sizeof (CHAR_T))
1394 + (width + 31));
1395 if (*f == L_('$'))
1396 /* Oh, oh. The argument comes from a positional parameter. */
1397 goto do_positional;
1398 JUMP (*f, step1_jumps);
1400 LABEL (precision):
1401 ++f;
1402 if (*f == L_('*'))
1404 const UCHAR_T *tmp; /* Temporary value. */
1406 tmp = ++f;
1407 if (ISDIGIT (*tmp) && read_int (&tmp) > 0 && *tmp == L_('$'))
1408 /* The precision comes from a positional parameter. */
1409 goto do_positional;
1411 prec = va_arg (ap, int);
1413 /* If the precision is negative the precision is omitted. */
1414 if (prec < 0)
1415 prec = -1;
1417 else if (ISDIGIT (*f))
1418 prec = read_int (&f);
1419 else
1420 prec = 0;
1421 if (prec > width
1422 && prec + 32 > sizeof (work_buffer) / sizeof (work_buffer[0]))
1423 workend = alloca (spec + 32) + (spec + 31);
1424 JUMP (*f, step2_jumps);
1426 /* Process 'h' modifier. There might another 'h' following. */
1427 LABEL (mod_half):
1428 is_short = 1;
1429 JUMP (*++f, step3a_jumps);
1431 /* Process 'hh' modifier. */
1432 LABEL (mod_halfhalf):
1433 is_short = 0;
1434 is_char = 1;
1435 JUMP (*++f, step4_jumps);
1437 /* Process 'l' modifier. There might another 'l' following. */
1438 LABEL (mod_long):
1439 is_long = 1;
1440 JUMP (*++f, step3b_jumps);
1442 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1443 allowed to follow. */
1444 LABEL (mod_longlong):
1445 is_long_double = 1;
1446 JUMP (*++f, step4_jumps);
1448 LABEL (mod_size_t):
1449 is_long_double = sizeof (size_t) > sizeof (unsigned long int);
1450 is_long = sizeof (size_t) > sizeof (unsigned int);
1451 JUMP (*++f, step4_jumps);
1453 LABEL (mod_ptrdiff_t):
1454 is_long_double = sizeof (ptrdiff_t) > sizeof (unsigned long int);
1455 is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
1456 JUMP (*++f, step4_jumps);
1458 LABEL (mod_intmax_t):
1459 is_long_double = sizeof (intmax_t) > sizeof (unsigned long int);
1460 is_long = sizeof (intmax_t) > sizeof (unsigned int);
1461 JUMP (*++f, step4_jumps);
1463 /* Process current format. */
1464 while (1)
1466 process_arg (((struct printf_spec *) NULL));
1467 process_string_arg (((struct printf_spec *) NULL));
1469 LABEL (form_unknown):
1470 if (spec == L_('\0'))
1472 /* The format string ended before the specifier is complete. */
1473 done = -1;
1474 goto all_done;
1477 /* If we are in the fast loop force entering the complicated
1478 one. */
1479 goto do_positional;
1482 /* The format is correctly handled. */
1483 ++nspecs_done;
1485 /* Look for next format specifier. */
1486 #ifdef COMPILE_WPRINTF
1487 f = find_spec ((end_of_spec = ++f));
1488 #else
1489 f = find_spec ((end_of_spec = ++f), &mbstate);
1490 #endif
1492 /* Write the following constant string. */
1493 outstring (end_of_spec, f - end_of_spec);
1495 while (*f != L_('\0'));
1497 /* Unlock stream and return. */
1498 goto all_done;
1500 /* Here starts the more complex loop to handle positional parameters. */
1501 do_positional:
1503 /* Array with information about the needed arguments. This has to
1504 be dynamically extensible. */
1505 size_t nspecs = 0;
1506 size_t nspecs_max = 32; /* A more or less arbitrary start value. */
1507 struct printf_spec *specs
1508 = alloca (nspecs_max * sizeof (struct printf_spec));
1510 /* The number of arguments the format string requests. This will
1511 determine the size of the array needed to store the argument
1512 attributes. */
1513 size_t nargs = 0;
1514 int *args_type;
1515 union printf_arg *args_value = NULL;
1517 /* Positional parameters refer to arguments directly. This could
1518 also determine the maximum number of arguments. Track the
1519 maximum number. */
1520 size_t max_ref_arg = 0;
1522 /* Just a counter. */
1523 size_t cnt;
1526 if (grouping == (const char *) -1)
1528 #ifdef COMPILE_WPRINTF
1529 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1530 _NL_NUMERIC_THOUSANDS_SEP_WC);
1531 #else
1532 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1533 #endif
1535 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1536 if (*grouping == '\0' || *grouping == CHAR_MAX)
1537 grouping = NULL;
1540 for (f = lead_str_end; *f != L_('\0'); f = specs[nspecs++].next_fmt)
1542 if (nspecs >= nspecs_max)
1544 /* Extend the array of format specifiers. */
1545 struct printf_spec *old = specs;
1547 nspecs_max *= 2;
1548 specs = alloca (nspecs_max * sizeof (struct printf_spec));
1550 if (specs == &old[nspecs])
1551 /* Stack grows up, OLD was the last thing allocated;
1552 extend it. */
1553 nspecs_max += nspecs_max / 2;
1554 else
1556 /* Copy the old array's elements to the new space. */
1557 memcpy (specs, old, nspecs * sizeof (struct printf_spec));
1558 if (old == &specs[nspecs])
1559 /* Stack grows down, OLD was just below the new
1560 SPECS. We can use that space when the new space
1561 runs out. */
1562 nspecs_max += nspecs_max / 2;
1566 /* Parse the format specifier. */
1567 #ifdef COMPILE_WPRINTF
1568 nargs += parse_one_spec (f, nargs, &specs[nspecs], &max_ref_arg);
1569 #else
1570 nargs += parse_one_spec (f, nargs, &specs[nspecs], &max_ref_arg,
1571 &mbstate);
1572 #endif
1575 /* Determine the number of arguments the format string consumes. */
1576 nargs = MAX (nargs, max_ref_arg);
1578 /* Allocate memory for the argument descriptions. */
1579 args_type = alloca (nargs * sizeof (int));
1580 memset (args_type, 0, nargs * sizeof (int));
1581 args_value = alloca (nargs * sizeof (union printf_arg));
1583 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1584 still zero after this loop, format is invalid. For now we
1585 simply use 0 as the value. */
1587 /* Fill in the types of all the arguments. */
1588 for (cnt = 0; cnt < nspecs; ++cnt)
1590 /* If the width is determined by an argument this is an int. */
1591 if (specs[cnt].width_arg != -1)
1592 args_type[specs[cnt].width_arg] = PA_INT;
1594 /* If the precision is determined by an argument this is an int. */
1595 if (specs[cnt].prec_arg != -1)
1596 args_type[specs[cnt].prec_arg] = PA_INT;
1598 switch (specs[cnt].ndata_args)
1600 case 0: /* No arguments. */
1601 break;
1602 case 1: /* One argument; we already have the type. */
1603 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1604 break;
1605 default:
1606 /* We have more than one argument for this format spec.
1607 We must call the arginfo function again to determine
1608 all the types. */
1609 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1610 (&specs[cnt].info,
1611 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg]);
1612 break;
1616 /* Now we know all the types and the order. Fill in the argument
1617 values. */
1618 for (cnt = 0; cnt < nargs; ++cnt)
1619 switch (args_type[cnt])
1621 #define T(tag, mem, type) \
1622 case tag: \
1623 args_value[cnt].mem = va_arg (ap_save, type); \
1624 break
1626 T (PA_CHAR, pa_char, int); /* Promoted. */
1627 T (PA_WCHAR, pa_wchar, wint_t);
1628 T (PA_INT|PA_FLAG_SHORT, pa_short_int, int); /* Promoted. */
1629 T (PA_INT, pa_int, int);
1630 T (PA_INT|PA_FLAG_LONG, pa_long_int, long int);
1631 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1632 T (PA_FLOAT, pa_float, double); /* Promoted. */
1633 T (PA_DOUBLE, pa_double, double);
1634 T (PA_DOUBLE|PA_FLAG_LONG_DOUBLE, pa_long_double, long double);
1635 T (PA_STRING, pa_string, const char *);
1636 T (PA_WSTRING, pa_wstring, const wchar_t *);
1637 T (PA_POINTER, pa_pointer, void *);
1638 #undef T
1639 default:
1640 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1641 args_value[cnt].pa_pointer = va_arg (ap_save, void *);
1642 else
1643 args_value[cnt].pa_long_double = 0.0;
1644 break;
1647 /* Now walk through all format specifiers and process them. */
1648 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1650 #undef REF
1651 #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
1652 # define REF(Name) &&do2_##Name - &&do_form_unknown
1653 #else
1654 # define REF(Name) &&do2_##Name
1655 #endif
1656 #undef LABEL
1657 #define LABEL(Name) do2_##Name
1658 STEP4_TABLE;
1660 int is_negative;
1661 union
1663 unsigned long long int longlong;
1664 unsigned long int word;
1665 } number;
1666 int base;
1667 union printf_arg the_arg;
1668 CHAR_T *string; /* Pointer to argument string. */
1670 /* Fill variables from values in struct. */
1671 int alt = specs[nspecs_done].info.alt;
1672 int space = specs[nspecs_done].info.space;
1673 int left = specs[nspecs_done].info.left;
1674 int showsign = specs[nspecs_done].info.showsign;
1675 int group = specs[nspecs_done].info.group;
1676 int is_long_double = specs[nspecs_done].info.is_long_double;
1677 int is_short = specs[nspecs_done].info.is_short;
1678 int is_char = specs[nspecs_done].info.is_char;
1679 int is_long = specs[nspecs_done].info.is_long;
1680 int width = specs[nspecs_done].info.width;
1681 int prec = specs[nspecs_done].info.prec;
1682 int use_outdigits = specs[nspecs_done].info.i18n;
1683 char pad = specs[nspecs_done].info.pad;
1684 CHAR_T spec = specs[nspecs_done].info.spec;
1686 /* Fill in last information. */
1687 if (specs[nspecs_done].width_arg != -1)
1689 /* Extract the field width from an argument. */
1690 specs[nspecs_done].info.width =
1691 args_value[specs[nspecs_done].width_arg].pa_int;
1693 if (specs[nspecs_done].info.width < 0)
1694 /* If the width value is negative left justification is
1695 selected and the value is taken as being positive. */
1697 specs[nspecs_done].info.width *= -1;
1698 left = specs[nspecs_done].info.left = 1;
1700 width = specs[nspecs_done].info.width;
1703 if (specs[nspecs_done].prec_arg != -1)
1705 /* Extract the precision from an argument. */
1706 specs[nspecs_done].info.prec =
1707 args_value[specs[nspecs_done].prec_arg].pa_int;
1709 if (specs[nspecs_done].info.prec < 0)
1710 /* If the precision is negative the precision is
1711 omitted. */
1712 specs[nspecs_done].info.prec = -1;
1714 prec = specs[nspecs_done].info.prec;
1717 /* Maybe the buffer is too small. */
1718 if (MAX (prec, width) + 32 > sizeof (work_buffer) / sizeof (CHAR_T))
1719 workend = ((CHAR_T *) alloca ((MAX (prec, width) + 32)
1720 * sizeof (CHAR_T))
1721 + (MAX (prec, width) + 31));
1723 /* Process format specifiers. */
1724 while (1)
1726 JUMP (spec, step4_jumps);
1728 process_arg ((&specs[nspecs_done]));
1729 process_string_arg ((&specs[nspecs_done]));
1731 LABEL (form_unknown):
1733 extern printf_function **__printf_function_table;
1734 int function_done;
1735 printf_function *function;
1736 unsigned int i;
1737 const void **ptr;
1739 function =
1740 (__printf_function_table == NULL ? NULL :
1741 __printf_function_table[specs[nspecs_done].info.spec]);
1743 if (function == NULL)
1744 function = &printf_unknown;
1746 ptr = alloca (specs[nspecs_done].ndata_args
1747 * sizeof (const void *));
1749 /* Fill in an array of pointers to the argument values. */
1750 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
1751 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1753 /* Call the function. */
1754 function_done = (*function) (s, &specs[nspecs_done].info, ptr);
1756 /* If an error occurred we don't have information about #
1757 of chars. */
1758 if (function_done < 0)
1760 done = -1;
1761 goto all_done;
1764 done += function_done;
1766 break;
1769 /* Write the following constant string. */
1770 outstring (specs[nspecs_done].end_of_fmt,
1771 specs[nspecs_done].next_fmt
1772 - specs[nspecs_done].end_of_fmt);
1776 all_done:
1777 /* Unlock the stream. */
1778 #ifdef USE_IN_LIBIO
1779 _IO_funlockfile (s);
1780 #else
1781 __funlockfile (s);
1782 #endif
1783 __libc_cleanup_region_end (0);
1785 return done;
1788 /* Handle an unknown format specifier. This prints out a canonicalized
1789 representation of the format spec itself. */
1790 static int
1791 printf_unknown (FILE *s, const struct printf_info *info,
1792 const void *const *args)
1795 int done = 0;
1796 CHAR_T work_buffer[MAX (info->width, info->spec) + 32];
1797 CHAR_T *const workend
1798 = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T) - 1];
1799 register CHAR_T *w;
1801 outchar (L_('%'));
1803 if (info->alt)
1804 outchar (L_('#'));
1805 if (info->group)
1806 outchar (L_('\''));
1807 if (info->showsign)
1808 outchar (L_('+'));
1809 else if (info->space)
1810 outchar (L_(' '));
1811 if (info->left)
1812 outchar (L_('-'));
1813 if (info->pad == L_('0'))
1814 outchar (L_('0'));
1815 if (info->i18n)
1816 outchar (L_('I'));
1818 if (info->width != 0)
1820 w = _itoa_word (info->width, workend + 1, 10, 0);
1821 while (w <= workend)
1822 outchar (*w++);
1825 if (info->prec != -1)
1827 outchar (L_('.'));
1828 w = _itoa_word (info->prec, workend + 1, 10, 0);
1829 while (w <= workend)
1830 outchar (*w++);
1833 if (info->spec != L_('\0'))
1834 outchar (info->spec);
1836 all_done:
1837 return done;
1840 /* Group the digits according to the grouping rules of the current locale.
1841 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1842 static CHAR_T *
1843 internal_function
1844 group_number (CHAR_T *w, CHAR_T *rear_ptr, const char *grouping,
1845 #ifdef COMPILE_WPRINTF
1846 wchar_t thousands_sep
1847 #else
1848 const char *thousands_sep
1849 #endif
1852 int len;
1853 CHAR_T *src, *s;
1854 #ifndef COMPILE_WPRINTF
1855 int tlen = strlen (thousands_sep);
1856 #endif
1858 /* We treat all negative values like CHAR_MAX. */
1860 if (*grouping == CHAR_MAX || *grouping <= 0)
1861 /* No grouping should be done. */
1862 return w;
1864 len = *grouping;
1866 /* Copy existing string so that nothing gets overwritten. */
1867 src = (CHAR_T *) alloca ((rear_ptr - w) * sizeof (CHAR_T));
1868 s = (CHAR_T *) __mempcpy (src, w + 1,
1869 (rear_ptr - w) * sizeof (CHAR_T)) - 1;
1870 w = rear_ptr;
1872 /* Process all characters in the string. */
1873 while (s >= src)
1875 *w-- = *s--;
1877 if (--len == 0 && s >= src)
1879 /* A new group begins. */
1880 #ifdef COMPILE_WPRINTF
1881 *w-- = thousands_sep;
1882 #else
1883 int cnt = tlen;
1885 *w-- = thousands_sep[--cnt];
1886 while (cnt > 0);
1887 #endif
1889 len = *grouping++;
1890 if (*grouping == '\0')
1891 /* The previous grouping repeats ad infinitum. */
1892 --grouping;
1893 else if (*grouping == CHAR_MAX
1894 #if CHAR_MIN < 0
1895 || *grouping < 0
1896 #endif
1899 /* No further grouping to be done.
1900 Copy the rest of the number. */
1902 *w-- = *s--;
1903 while (s >= src);
1904 break;
1908 return w;
1911 #ifdef USE_IN_LIBIO
1912 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
1913 struct helper_file
1915 struct _IO_FILE_plus _f;
1916 _IO_FILE *_put_stream;
1917 #ifdef _IO_MTSAFE_IO
1918 _IO_lock_t lock;
1919 #endif
1922 static int
1923 _IO_helper_overflow (_IO_FILE *s, int c)
1925 _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
1926 #ifdef COMPILE_WPRINTF
1927 int used = s->_wide_data->_IO_write_ptr - s->_wide_data->_IO_write_base;
1928 if (used)
1930 _IO_size_t written = _IO_sputn (target, s->_wide_data->_IO_write_base,
1931 used);
1932 s->_wide_data->_IO_write_ptr -= written;
1934 #else
1935 int used = s->_IO_write_ptr - s->_IO_write_base;
1936 if (used)
1938 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
1939 s->_IO_write_ptr -= written;
1941 #endif
1942 return PUTC (c, s);
1945 static const struct _IO_jump_t _IO_helper_jumps =
1947 JUMP_INIT_DUMMY,
1948 JUMP_INIT (finish, _IO_default_finish),
1949 JUMP_INIT (overflow, _IO_helper_overflow),
1950 JUMP_INIT (underflow, _IO_default_underflow),
1951 JUMP_INIT (uflow, _IO_default_uflow),
1952 JUMP_INIT (pbackfail, _IO_default_pbackfail),
1953 JUMP_INIT (xsputn, _IO_default_xsputn),
1954 JUMP_INIT (xsgetn, _IO_default_xsgetn),
1955 JUMP_INIT (seekoff, _IO_default_seekoff),
1956 JUMP_INIT (seekpos, _IO_default_seekpos),
1957 JUMP_INIT (setbuf, _IO_default_setbuf),
1958 JUMP_INIT (sync, _IO_default_sync),
1959 JUMP_INIT (doallocate, _IO_default_doallocate),
1960 JUMP_INIT (read, _IO_default_read),
1961 JUMP_INIT (write, _IO_default_write),
1962 JUMP_INIT (seek, _IO_default_seek),
1963 JUMP_INIT (close, _IO_default_close),
1964 JUMP_INIT (stat, _IO_default_stat)
1967 static int
1968 internal_function
1969 buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format,
1970 _IO_va_list args)
1972 CHAR_T buf[_IO_BUFSIZ];
1973 struct helper_file helper;
1974 register _IO_FILE *hp = &helper._f.file;
1975 int result, to_flush;
1977 /* Initialize helper. */
1978 helper._put_stream = s;
1979 #ifdef COMPILE_WPRINTF
1980 _IO_wsetp (hp, buf, buf + sizeof buf / sizeof (CHAR_T));
1981 hp->_mode = 1;
1982 #else
1983 _IO_setp (hp, buf, buf + sizeof buf);
1984 hp->_mode = -1;
1985 #endif
1986 hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS;
1987 #if _IO_JUMPS_OFFSET
1988 hp->_vtable_offset = 0;
1989 #endif
1990 #ifdef _IO_MTSAFE_IO
1991 hp->_lock = &helper.lock;
1992 __libc_lock_init (*hp->_lock);
1993 #endif
1994 _IO_JUMPS (hp) = (struct _IO_jump_t *) &_IO_helper_jumps;
1996 /* Now print to helper instead. */
1997 result = vfprintf (hp, format, args);
1999 /* Lock stream. */
2000 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
2001 _IO_flockfile (s);
2003 /* Now flush anything from the helper to the S. */
2004 #ifdef COMPILE_WPRINTF
2005 if ((to_flush = (hp->_wide_data->_IO_write_ptr
2006 - hp->_wide_data->_IO_write_base)) > 0)
2008 if ((int) _IO_sputn (s, hp->_wide_data->_IO_write_base, to_flush)
2009 != to_flush)
2010 result = -1;
2012 #else
2013 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
2015 if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
2016 result = -1;
2018 #endif
2020 /* Unlock the stream. */
2021 _IO_funlockfile (s);
2022 __libc_cleanup_region_end (0);
2024 return result;
2027 #else /* !USE_IN_LIBIO */
2029 static int
2030 internal_function
2031 buffered_vfprintf (register FILE *s, const CHAR_T *format, va_list args)
2033 char buf[BUFSIZ];
2034 int result;
2036 s->__bufp = s->__buffer = buf;
2037 s->__bufsize = sizeof buf;
2038 s->__put_limit = s->__buffer + s->__bufsize;
2039 s->__get_limit = s->__buffer;
2041 /* Now use buffer to print. */
2042 result = vfprintf (s, format, args);
2044 if (fflush (s) == EOF)
2045 result = -1;
2046 s->__buffer = s->__bufp = s->__get_limit = s->__put_limit = NULL;
2047 s->__bufsize = 0;
2049 return result;
2052 /* Pads string with given number of a specified character.
2053 This code is taken from iopadn.c of the GNU I/O library. */
2054 #define PADSIZE 16
2055 static const CHAR_T blanks[PADSIZE] =
2056 { L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '),
2057 L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' ') };
2058 static const CHAR_T zeroes[PADSIZE] =
2059 { L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'),
2060 L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0') };
2062 ssize_t
2063 #ifndef COMPILE_WPRINTF
2064 __printf_pad (FILE *s, char pad, size_t count)
2065 #else
2066 __wprintf_pad (FILE *s, wchar_t pad, size_t count)
2067 #endif
2069 const CHAR_T *padptr;
2070 register size_t i;
2072 padptr = pad == L_(' ') ? blanks : zeroes;
2074 for (i = count; i >= PADSIZE; i -= PADSIZE)
2075 if (PUT (s, padptr, PADSIZE) != PADSIZE)
2076 return -1;
2077 if (i > 0)
2078 if (PUT (s, padptr, i) != i)
2079 return -1;
2081 return count;
2083 #undef PADSIZE
2084 #endif /* USE_IN_LIBIO */
2086 #ifdef USE_IN_LIBIO
2087 # undef vfprintf
2088 # ifdef strong_alias
2089 /* This is for glibc. */
2090 # ifdef COMPILE_WPRINTF
2091 strong_alias (_IO_vfwprintf, vfwprintf);
2092 # else
2093 strong_alias (_IO_vfprintf, vfprintf);
2094 # endif
2095 # else
2096 # if defined __ELF__ || defined __GNU_LIBRARY__
2097 # include <gnu-stabs.h>
2098 # ifdef weak_alias
2099 # ifdef COMPILE_WPRINTF
2100 weak_alias (_IO_vfwprintf, vfwprintf);
2101 # else
2102 weak_alias (_IO_vfprintf, vfprintf);
2103 # endif
2104 # endif
2105 # endif
2106 # endif
2107 #endif