Update.
[glibc.git] / stdio-common / vfprintf.c
blobb61b637af9c6c03412de50ac1f62b820d3586cdf
1 /* Copyright (C) 1991,92,93,94,95,96,97,98,99 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 <locale/localeinfo.h>
32 /* This code is shared between the standard stdio implementation found
33 in GNU C library and the libio implementation originally found in
34 GNU libg++.
36 Beside this it is also shared between the normal and wide character
37 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
39 #ifndef COMPILE_WPRINTF
40 # define CHAR_T char
41 # define UCHAR_T unsigned char
42 # define INT_T int
43 # define L_(Str) Str
44 # define ISDIGIT(Ch) isdigit (Ch)
46 # ifdef USE_IN_LIBIO
47 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
48 # define PAD(Padchar) \
49 if (width > 0) \
50 done += _IO_padn (s, (Padchar), width)
51 # else
52 # define PUTC(C, F) putc (C, F)
53 ssize_t __printf_pad __P ((FILE *, char pad, size_t n));
54 # define PAD(Padchar) \
55 if (width > 0) \
56 { ssize_t __res = __printf_pad (s, (Padchar), width); \
57 if (__res == -1) \
58 { \
59 done = -1; \
60 goto all_done; \
61 } \
62 done += __res; }
63 # endif
64 #else
65 # define vfprintf vfwprintf
66 # define CHAR_T wchar_t
67 # define UCHAR_T uwchar_t
68 # define INT_T wint_t
69 # define L_(Str) L##Str
70 # define ISDIGIT(Ch) iswdigit (Ch)
72 # ifdef USE_IN_LIBIO
73 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
74 # define PAD(Padchar) \
75 if (width > 0) \
76 done += _IO_wpadn (s, (Padchar), width)
77 # else
78 # define PUTC(C, F) wputc (C, F)
79 ssize_t __wprintf_pad __P ((FILE *, wchar_t pad, size_t n));
80 # define PAD(Padchar) \
81 if (width > 0) \
82 { ssize_t __res = __wprintf_pad (s, (Padchar), width); \
83 if (__res == -1) \
84 { \
85 done = -1; \
86 goto all_done; \
87 } \
88 done += __res; }
89 # endif
90 #endif
92 /* Include the shared code for parsing the format string. */
93 #include "printf-parse.h"
96 #ifdef USE_IN_LIBIO
97 /* This code is for use in libio. */
98 # include <libioP.h>
99 # define PUTC(C, F) _IO_putc_unlocked (C, F)
100 # define vfprintf _IO_vfprintf
101 # define FILE _IO_FILE
102 # undef va_list
103 # define va_list _IO_va_list
104 # undef BUFSIZ
105 # define BUFSIZ _IO_BUFSIZ
106 # define ARGCHECK(S, Format) \
107 do \
109 /* Check file argument for consistence. */ \
110 CHECK_FILE (S, -1); \
111 if (S->_flags & _IO_NO_WRITES) \
113 __set_errno (EBADF); \
114 return -1; \
116 if (Format == NULL) \
118 MAYBE_SET_EINVAL; \
119 return -1; \
121 } while (0)
122 # define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
123 #else /* ! USE_IN_LIBIO */
124 /* This code is for use in the GNU C library. */
125 # include <stdio.h>
126 # define PUT(F, S, N) fwrite (S, 1, N, F)
127 # define ARGCHECK(S, Format) \
128 do \
130 /* Check file argument for consistence. */ \
131 if (!__validfp (S) || !S->__mode.__write) \
133 __set_errno (EBADF); \
134 return -1; \
136 if (Format == NULL) \
138 __set_errno (EINVAL); \
139 return -1; \
141 if (!S->__seen) \
143 if (__flshfp (S, EOF) == EOF) \
144 return -1; \
147 while (0)
148 # define UNBUFFERED_P(s) ((s)->__buffer == NULL)
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 */
157 #define outchar(Ch) \
158 do \
160 register const int outc = (Ch); \
161 if (PUTC (outc, s) == EOF) \
163 done = -1; \
164 goto all_done; \
166 else \
167 ++done; \
169 while (0)
171 #define outstring(String, Len) \
172 do \
174 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
176 done = -1; \
177 goto all_done; \
179 done += (Len); \
181 while (0)
183 /* For handling long_double and longlong we use the same flag. */
184 #ifndef is_longlong
185 # define is_longlong is_long_double
186 #endif
189 /* Global variables. */
190 static const char null[] = "(null)";
193 /* Helper function to provide temporary buffering for unbuffered streams. */
194 static int buffered_vfprintf __P ((FILE *stream, const CHAR_T *fmt, va_list))
195 internal_function;
197 /* Handle unknown format specifier. */
198 static int printf_unknown __P ((FILE *, const struct printf_info *,
199 const void *const *));
201 /* Group digits of number string. */
202 static char *group_number __P ((CHAR_T *, CHAR_T *, const CHAR_T *, wchar_t))
203 internal_function;
206 /* The function itself. */
208 vfprintf (FILE *s, const CHAR_T *format, va_list ap)
210 /* The character used as thousands separator. */
211 wchar_t thousands_sep;
213 /* The string describing the size of groups of digits. */
214 const char *grouping;
216 /* Place to accumulate the result. */
217 int done;
219 /* Current character in format string. */
220 const UCHAR_T *f;
222 /* End of leading constant string. */
223 const UCHAR_T *lead_str_end;
225 /* Points to next format specifier. */
226 const UCHAR_T *end_of_spec;
228 /* Buffer intermediate results. */
229 char work_buffer[1000];
230 char *workend;
232 /* State for restartable multibyte character handling functions. */
233 mbstate_t mbstate;
235 /* We have to save the original argument pointer. */
236 va_list ap_save;
238 /* Count number of specifiers we already processed. */
239 int nspecs_done;
241 /* For the %m format we may need the current `errno' value. */
242 int save_errno = errno;
245 /* This table maps a character into a number representing a
246 class. In each step there is a destination label for each
247 class. */
248 static const int jump_table[] =
250 /* ' ' */ 1, 0, 0, /* '#' */ 4,
251 0, /* '%' */ 14, 0, /* '\''*/ 6,
252 0, 0, /* '*' */ 7, /* '+' */ 2,
253 0, /* '-' */ 3, /* '.' */ 9, 0,
254 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
255 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
256 /* '8' */ 8, /* '9' */ 8, 0, 0,
257 0, 0, 0, 0,
258 0, /* 'A' */ 26, 0, /* 'C' */ 25,
259 0, /* 'E' */ 19, 0, /* 'G' */ 19,
260 0, 0, 0, 0,
261 /* 'L' */ 12, 0, 0, 0,
262 0, 0, 0, /* 'S' */ 21,
263 0, 0, 0, 0,
264 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
265 0, 0, 0, 0,
266 0, /* 'a' */ 26, 0, /* 'c' */ 20,
267 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
268 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
269 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
270 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
271 /* 't' */ 27, /* 'u' */ 16, 0, 0,
272 /* 'x' */ 18, 0, /* 'z' */ 13
275 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < ' ' || (Ch) > 'z')
276 #define CHAR_CLASS(Ch) (jump_table[(int) (Ch) - ' '])
277 #define JUMP(ChExpr, table) \
278 do \
280 const void *ptr; \
281 spec = (ChExpr); \
282 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
283 : table[CHAR_CLASS (spec)]; \
284 goto *ptr; \
286 while (0)
288 #define STEP0_3_TABLE \
289 /* Step 0: at the beginning. */ \
290 static const void *step0_jumps[29] = \
292 REF (form_unknown), \
293 REF (flag_space), /* for ' ' */ \
294 REF (flag_plus), /* for '+' */ \
295 REF (flag_minus), /* for '-' */ \
296 REF (flag_hash), /* for '<hash>' */ \
297 REF (flag_zero), /* for '0' */ \
298 REF (flag_quote), /* for '\'' */ \
299 REF (width_asterics), /* for '*' */ \
300 REF (width), /* for '1'...'9' */ \
301 REF (precision), /* for '.' */ \
302 REF (mod_half), /* for 'h' */ \
303 REF (mod_long), /* for 'l' */ \
304 REF (mod_longlong), /* for 'L', 'q' */ \
305 REF (mod_size_t), /* for 'z', 'Z' */ \
306 REF (form_percent), /* for '%' */ \
307 REF (form_integer), /* for 'd', 'i' */ \
308 REF (form_unsigned), /* for 'u' */ \
309 REF (form_octal), /* for 'o' */ \
310 REF (form_hexa), /* for 'X', 'x' */ \
311 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
312 REF (form_character), /* for 'c' */ \
313 REF (form_string), /* for 's', 'S' */ \
314 REF (form_pointer), /* for 'p' */ \
315 REF (form_number), /* for 'n' */ \
316 REF (form_strerror), /* for 'm' */ \
317 REF (form_wcharacter), /* for 'C' */ \
318 REF (form_floathex), /* for 'A', 'a' */ \
319 REF (mod_ptrdiff_t), /* for 't' */ \
320 REF (mod_intmax_t), /* for 'j' */ \
321 }; \
322 /* Step 1: after processing width. */ \
323 static const void *step1_jumps[29] = \
325 REF (form_unknown), \
326 REF (form_unknown), /* for ' ' */ \
327 REF (form_unknown), /* for '+' */ \
328 REF (form_unknown), /* for '-' */ \
329 REF (form_unknown), /* for '<hash>' */ \
330 REF (form_unknown), /* for '0' */ \
331 REF (form_unknown), /* for '\'' */ \
332 REF (form_unknown), /* for '*' */ \
333 REF (form_unknown), /* for '1'...'9' */ \
334 REF (precision), /* for '.' */ \
335 REF (mod_half), /* for 'h' */ \
336 REF (mod_long), /* for 'l' */ \
337 REF (mod_longlong), /* for 'L', 'q' */ \
338 REF (mod_size_t), /* for 'z', 'Z' */ \
339 REF (form_percent), /* for '%' */ \
340 REF (form_integer), /* for 'd', 'i' */ \
341 REF (form_unsigned), /* for 'u' */ \
342 REF (form_octal), /* for 'o' */ \
343 REF (form_hexa), /* for 'X', 'x' */ \
344 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
345 REF (form_character), /* for 'c' */ \
346 REF (form_string), /* for 's', 'S' */ \
347 REF (form_pointer), /* for 'p' */ \
348 REF (form_number), /* for 'n' */ \
349 REF (form_strerror), /* for 'm' */ \
350 REF (form_wcharacter), /* for 'C' */ \
351 REF (form_floathex), /* for 'A', 'a' */ \
352 REF (mod_ptrdiff_t), /* for 't' */ \
353 REF (mod_intmax_t) /* for 'j' */ \
354 }; \
355 /* Step 2: after processing precision. */ \
356 static const void *step2_jumps[29] = \
358 REF (form_unknown), \
359 REF (form_unknown), /* for ' ' */ \
360 REF (form_unknown), /* for '+' */ \
361 REF (form_unknown), /* for '-' */ \
362 REF (form_unknown), /* for '<hash>' */ \
363 REF (form_unknown), /* for '0' */ \
364 REF (form_unknown), /* for '\'' */ \
365 REF (form_unknown), /* for '*' */ \
366 REF (form_unknown), /* for '1'...'9' */ \
367 REF (form_unknown), /* for '.' */ \
368 REF (mod_half), /* for 'h' */ \
369 REF (mod_long), /* for 'l' */ \
370 REF (mod_longlong), /* for 'L', 'q' */ \
371 REF (mod_size_t), /* for 'z', 'Z' */ \
372 REF (form_percent), /* for '%' */ \
373 REF (form_integer), /* for 'd', 'i' */ \
374 REF (form_unsigned), /* for 'u' */ \
375 REF (form_octal), /* for 'o' */ \
376 REF (form_hexa), /* for 'X', 'x' */ \
377 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
378 REF (form_character), /* for 'c' */ \
379 REF (form_string), /* for 's', 'S' */ \
380 REF (form_pointer), /* for 'p' */ \
381 REF (form_number), /* for 'n' */ \
382 REF (form_strerror), /* for 'm' */ \
383 REF (form_wcharacter), /* for 'C' */ \
384 REF (form_floathex), /* for 'A', 'a' */ \
385 REF (mod_ptrdiff_t), /* for 't' */ \
386 REF (mod_intmax_t) /* for 'j' */ \
387 }; \
388 /* Step 3a: after processing first 'h' modifier. */ \
389 static const void *step3a_jumps[29] = \
391 REF (form_unknown), \
392 REF (form_unknown), /* for ' ' */ \
393 REF (form_unknown), /* for '+' */ \
394 REF (form_unknown), /* for '-' */ \
395 REF (form_unknown), /* for '<hash>' */ \
396 REF (form_unknown), /* for '0' */ \
397 REF (form_unknown), /* for '\'' */ \
398 REF (form_unknown), /* for '*' */ \
399 REF (form_unknown), /* for '1'...'9' */ \
400 REF (form_unknown), /* for '.' */ \
401 REF (mod_halfhalf), /* for 'h' */ \
402 REF (form_unknown), /* for 'l' */ \
403 REF (form_unknown), /* for 'L', 'q' */ \
404 REF (form_unknown), /* for 'z', 'Z' */ \
405 REF (form_percent), /* for '%' */ \
406 REF (form_integer), /* for 'd', 'i' */ \
407 REF (form_unsigned), /* for 'u' */ \
408 REF (form_octal), /* for 'o' */ \
409 REF (form_hexa), /* for 'X', 'x' */ \
410 REF (form_unknown), /* for 'E', 'e', 'f', 'G', 'g' */ \
411 REF (form_unknown), /* for 'c' */ \
412 REF (form_unknown), /* for 's', 'S' */ \
413 REF (form_unknown), /* for 'p' */ \
414 REF (form_number), /* for 'n' */ \
415 REF (form_unknown), /* for 'm' */ \
416 REF (form_unknown), /* for 'C' */ \
417 REF (form_unknown), /* for 'A', 'a' */ \
418 REF (form_unknown), /* for 't' */ \
419 REF (form_unknown) /* for 'j' */ \
420 }; \
421 /* Step 3b: after processing first 'l' modifier. */ \
422 static const void *step3b_jumps[29] = \
424 REF (form_unknown), \
425 REF (form_unknown), /* for ' ' */ \
426 REF (form_unknown), /* for '+' */ \
427 REF (form_unknown), /* for '-' */ \
428 REF (form_unknown), /* for '<hash>' */ \
429 REF (form_unknown), /* for '0' */ \
430 REF (form_unknown), /* for '\'' */ \
431 REF (form_unknown), /* for '*' */ \
432 REF (form_unknown), /* for '1'...'9' */ \
433 REF (form_unknown), /* for '.' */ \
434 REF (form_unknown), /* for 'h' */ \
435 REF (mod_longlong), /* for 'l' */ \
436 REF (form_unknown), /* for 'L', 'q' */ \
437 REF (form_unknown), /* for 'z', 'Z' */ \
438 REF (form_percent), /* for '%' */ \
439 REF (form_integer), /* for 'd', 'i' */ \
440 REF (form_unsigned), /* for 'u' */ \
441 REF (form_octal), /* for 'o' */ \
442 REF (form_hexa), /* for 'X', 'x' */ \
443 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
444 REF (form_character), /* for 'c' */ \
445 REF (form_string), /* for 's', 'S' */ \
446 REF (form_pointer), /* for 'p' */ \
447 REF (form_number), /* for 'n' */ \
448 REF (form_strerror), /* for 'm' */ \
449 REF (form_wcharacter), /* for 'C' */ \
450 REF (form_floathex), /* for 'A', 'a' */ \
451 REF (form_unknown), /* for 't' */ \
452 REF (form_unknown) /* for 'j' */ \
455 #define STEP4_TABLE \
456 /* Step 4: processing format specifier. */ \
457 static const void *step4_jumps[29] = \
459 REF (form_unknown), \
460 REF (form_unknown), /* for ' ' */ \
461 REF (form_unknown), /* for '+' */ \
462 REF (form_unknown), /* for '-' */ \
463 REF (form_unknown), /* for '<hash>' */ \
464 REF (form_unknown), /* for '0' */ \
465 REF (form_unknown), /* for '\'' */ \
466 REF (form_unknown), /* for '*' */ \
467 REF (form_unknown), /* for '1'...'9' */ \
468 REF (form_unknown), /* for '.' */ \
469 REF (form_unknown), /* for 'h' */ \
470 REF (form_unknown), /* for 'l' */ \
471 REF (form_unknown), /* for 'L', 'q' */ \
472 REF (form_unknown), /* for 'z', 'Z' */ \
473 REF (form_percent), /* for '%' */ \
474 REF (form_integer), /* for 'd', 'i' */ \
475 REF (form_unsigned), /* for 'u' */ \
476 REF (form_octal), /* for 'o' */ \
477 REF (form_hexa), /* for 'X', 'x' */ \
478 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
479 REF (form_character), /* for 'c' */ \
480 REF (form_string), /* for 's', 'S' */ \
481 REF (form_pointer), /* for 'p' */ \
482 REF (form_number), /* for 'n' */ \
483 REF (form_strerror), /* for 'm' */ \
484 REF (form_wcharacter), /* for 'C' */ \
485 REF (form_floathex), /* for 'A', 'a' */ \
486 REF (form_unknown), /* for 't' */ \
487 REF (form_unknown) /* for 'j' */ \
491 #define process_arg(fspec) \
492 /* Start real work. We know about all flags and modifiers and \
493 now process the wanted format specifier. */ \
494 LABEL (form_percent): \
495 /* Write a literal "%". */ \
496 outchar ('%'); \
497 break; \
499 LABEL (form_integer): \
500 /* Signed decimal integer. */ \
501 base = 10; \
503 if (is_longlong) \
505 long long int signed_number; \
507 if (fspec == NULL) \
508 signed_number = va_arg (ap, long long int); \
509 else \
510 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
512 is_negative = signed_number < 0; \
513 number.longlong = is_negative ? (- signed_number) : signed_number; \
515 goto LABEL (longlong_number); \
517 else \
519 long int signed_number; \
521 if (fspec == NULL) \
523 if (is_long) \
524 signed_number = va_arg (ap, long int); \
525 else /* `char' and `short int' will be promoted to `int'. */ \
526 signed_number = va_arg (ap, int); \
528 else \
529 if (is_long) \
530 signed_number = args_value[fspec->data_arg].pa_long_int; \
531 else \
532 signed_number = args_value[fspec->data_arg].pa_int; \
534 is_negative = signed_number < 0; \
535 number.word = is_negative ? (- signed_number) : signed_number; \
537 goto LABEL (number); \
539 /* NOTREACHED */ \
541 LABEL (form_unsigned): \
542 /* Unsigned decimal integer. */ \
543 base = 10; \
544 goto LABEL (unsigned_number); \
545 /* NOTREACHED */ \
547 LABEL (form_octal): \
548 /* Unsigned octal integer. */ \
549 base = 8; \
550 goto LABEL (unsigned_number); \
551 /* NOTREACHED */ \
553 LABEL (form_hexa): \
554 /* Unsigned hexadecimal integer. */ \
555 base = 16; \
557 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
559 /* ISO specifies the `+' and ` ' flags only for signed \
560 conversions. */ \
561 is_negative = 0; \
562 showsign = 0; \
563 space = 0; \
565 if (is_longlong) \
567 if (fspec == NULL) \
568 number.longlong = va_arg (ap, unsigned long long int); \
569 else \
570 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
572 LABEL (longlong_number): \
573 if (prec < 0) \
574 /* Supply a default precision if none was given. */ \
575 prec = 1; \
576 else \
577 /* We have to take care for the '0' flag. If a precision \
578 is given it must be ignored. */ \
579 pad = ' '; \
581 /* If the precision is 0 and the number is 0 nothing has to \
582 be written for the number, except for the 'o' format in \
583 alternate form. */ \
584 if (prec == 0 && number.longlong == 0) \
586 string = workend; \
587 if (base == 8 && alt) \
588 *string-- = '0'; \
590 else \
592 /* Put the number in WORK. */ \
593 string = _itoa (number.longlong, workend + 1, base, \
594 spec == 'X'); \
595 string -= 1; \
596 if (group && grouping) \
597 string = group_number (string, workend, grouping, \
598 thousands_sep); \
600 /* Simply further test for num != 0. */ \
601 number.word = number.longlong != 0; \
603 else \
605 if (fspec == NULL) \
607 if (is_long) \
608 number.word = va_arg (ap, unsigned long int); \
609 else if (!is_short) \
610 number.word = va_arg (ap, unsigned int); \
611 else \
612 number.word = (unsigned short int) va_arg (ap, unsigned int); \
614 else \
615 if (is_long) \
616 number.word = args_value[fspec->data_arg].pa_u_long_int; \
617 else if (is_char) \
618 number.word = (unsigned char) \
619 args_value[fspec->data_arg].pa_char; \
620 else if (!is_short) \
621 number.word = args_value[fspec->data_arg].pa_u_int; \
622 else \
623 number.word = (unsigned short int) \
624 args_value[fspec->data_arg].pa_u_short_int; \
626 LABEL (number): \
627 if (prec < 0) \
628 /* Supply a default precision if none was given. */ \
629 prec = 1; \
630 else \
631 /* We have to take care for the '0' flag. If a precision \
632 is given it must be ignored. */ \
633 pad = ' '; \
635 /* If the precision is 0 and the number is 0 nothing has to \
636 be written for the number, except for the 'o' format in \
637 alternate form. */ \
638 if (prec == 0 && number.word == 0) \
640 string = workend; \
641 if (base == 8 && alt) \
642 *string-- = '0'; \
644 else \
646 /* Put the number in WORK. */ \
647 string = _itoa_word (number.word, workend + 1, base, \
648 spec == 'X'); \
649 string -= 1; \
650 if (group && grouping) \
651 string = group_number (string, workend, grouping, \
652 thousands_sep); \
656 prec -= workend - string; \
658 if (prec > 0) \
659 /* Add zeros to the precision. */ \
660 while (prec-- > 0) \
661 *string-- = '0'; \
662 else if (number.word != 0 && alt && base == 8) \
663 /* Add octal marker. */ \
664 *string-- = '0'; \
666 if (!left) \
668 width -= workend - string; \
670 if (number.word != 0 && alt && base == 16) \
671 /* Account for 0X hex marker. */ \
672 width -= 2; \
674 if (is_negative || showsign || space) \
675 --width; \
677 if (pad == '0') \
679 while (width-- > 0) \
680 *string-- = '0'; \
682 if (number.word != 0 && alt && base == 16) \
684 *string-- = spec; \
685 *string-- = '0'; \
688 if (is_negative) \
689 *string-- = '-'; \
690 else if (showsign) \
691 *string-- = '+'; \
692 else if (space) \
693 *string-- = ' '; \
695 else \
697 if (number.word != 0 && alt && base == 16) \
699 *string-- = spec; \
700 *string-- = '0'; \
703 if (is_negative) \
704 *string-- = '-'; \
705 else if (showsign) \
706 *string-- = '+'; \
707 else if (space) \
708 *string-- = ' '; \
710 while (width-- > 0) \
711 *string-- = ' '; \
714 outstring (string + 1, workend - string); \
716 break; \
718 else \
720 if (number.word != 0 && alt && base == 16) \
722 *string-- = spec; \
723 *string-- = '0'; \
726 if (is_negative) \
727 *string-- = '-'; \
728 else if (showsign) \
729 *string-- = '+'; \
730 else if (space) \
731 *string-- = ' '; \
733 width -= workend - string; \
734 outstring (string + 1, workend - string); \
736 PAD (' '); \
737 break; \
740 LABEL (form_float): \
742 /* Floating-point number. This is handled by printf_fp.c. */ \
743 extern int __printf_fp __P ((FILE *, const struct printf_info *, \
744 const void **const)); \
745 const void *ptr; \
746 int function_done; \
748 if (fspec == NULL) \
750 struct printf_info info = { prec: prec, \
751 width: width, \
752 spec: spec, \
753 is_long_double: is_long_double, \
754 is_short: is_short, \
755 is_long: is_long, \
756 alt: alt, \
757 space: space, \
758 left: left, \
759 showsign: showsign, \
760 group: group, \
761 pad: pad, \
762 extra: 0 }; \
764 if (is_long_double) \
765 the_arg.pa_long_double = va_arg (ap, long double); \
766 else \
767 the_arg.pa_double = va_arg (ap, double); \
768 ptr = (const void *) &the_arg; \
770 function_done = __printf_fp (s, &info, &ptr); \
772 else \
774 ptr = (const void *) &args_value[fspec->data_arg]; \
776 function_done = __printf_fp (s, &fspec->info, &ptr); \
779 if (function_done < 0) \
781 /* Error in print handler. */ \
782 done = -1; \
783 goto all_done; \
786 done += function_done; \
788 break; \
790 LABEL (form_floathex): \
792 /* FLoating point number printed as hexadecimal number. */ \
793 extern int __printf_fphex __P ((FILE *, const struct printf_info *, \
794 const void **const)); \
795 const void *ptr; \
796 int function_done; \
798 if (fspec == NULL) \
800 struct printf_info info = { prec: prec, \
801 width: width, \
802 spec: spec, \
803 is_long_double: is_long_double, \
804 is_short: is_short, \
805 is_long: is_long, \
806 alt: alt, \
807 space: space, \
808 left: left, \
809 showsign: showsign, \
810 group: group, \
811 pad: pad, \
812 extra: 0 }; \
814 if (is_long_double) \
815 the_arg.pa_long_double = va_arg (ap, long double); \
816 else \
817 the_arg.pa_double = va_arg (ap, double); \
818 ptr = (const void *) &the_arg; \
820 function_done = __printf_fphex (s, &info, &ptr); \
822 else \
824 ptr = (const void *) &args_value[fspec->data_arg]; \
826 function_done = __printf_fphex (s, &fspec->info, &ptr); \
829 if (function_done < 0) \
831 /* Error in print handler. */ \
832 done = -1; \
833 goto all_done; \
836 done += function_done; \
838 break; \
840 LABEL (form_character): \
841 /* Character. */ \
842 if (is_long) \
843 goto LABEL (form_wcharacter); \
844 --width; /* Account for the character itself. */ \
845 if (!left) \
846 PAD (' '); \
847 if (fspec == NULL) \
848 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
849 else \
850 outchar ((unsigned char) args_value[fspec->data_arg].pa_char); \
851 if (left) \
852 PAD (' '); \
853 break; \
855 LABEL (form_wcharacter): \
857 /* Wide character. */ \
858 char buf[MB_CUR_MAX]; \
859 mbstate_t mbstate; \
860 size_t len; \
862 memset (&mbstate, '\0', sizeof (mbstate_t)); \
863 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wint_t) \
864 : args_value[fspec->data_arg].pa_wchar), \
865 &mbstate); \
866 width -= len; \
867 if (!left) \
868 PAD (' '); \
869 outstring (buf, len); \
870 if (left) \
871 PAD (' '); \
873 break; \
875 LABEL (form_string): \
877 size_t len; \
879 /* The string argument could in fact be `char *' or `wchar_t *'. \
880 But this should not make a difference here. */ \
881 if (fspec == NULL) \
882 string = (char *) va_arg (ap, const char *); \
883 else \
884 string = (char *) args_value[fspec->data_arg].pa_string; \
886 /* Entry point for printing other strings. */ \
887 LABEL (print_string): \
889 if (string == NULL) \
891 /* Write "(null)" if there's space. */ \
892 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
894 string = (char *) null; \
895 len = sizeof (null) - 1; \
897 else \
899 string = (char *) ""; \
900 len = 0; \
903 else if (!is_long && spec != L_('S')) \
905 if (prec != -1) \
906 /* Search for the end of the string, but don't search past \
907 the length specified by the precision. */ \
908 len = strnlen (string, prec); \
909 else \
910 len = strlen (string); \
912 else \
914 const wchar_t *s2 = (const wchar_t *) string; \
915 mbstate_t mbstate; \
917 memset (&mbstate, '\0', sizeof (mbstate_t)); \
918 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
919 if (len == (size_t) -1) \
921 /* Illegal wide-character string. */ \
922 done = -1; \
923 goto all_done; \
926 assert (__mbsinit (&mbstate)); \
927 s2 = (const wchar_t *) string; \
928 string = alloca (len + 1); \
929 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
930 if (prec < len) \
931 len = prec; \
934 if ((width -= len) < 0) \
936 outstring (string, len); \
937 break; \
940 if (!left) \
941 PAD (' '); \
942 outstring (string, len); \
943 if (left) \
944 PAD (' '); \
946 break; \
948 LABEL (form_pointer): \
949 /* Generic pointer. */ \
951 const void *ptr; \
952 if (fspec == NULL) \
953 ptr = va_arg (ap, void *); \
954 else \
955 ptr = args_value[fspec->data_arg].pa_pointer; \
956 if (ptr != NULL) \
958 /* If the pointer is not NULL, write it as a %#x spec. */ \
959 base = 16; \
960 number.word = (unsigned long int) ptr; \
961 is_negative = 0; \
962 alt = 1; \
963 group = 0; \
964 spec = 'x'; \
965 goto LABEL (number); \
967 else \
969 /* Write "(nil)" for a nil pointer. */ \
970 string = (char *) "(nil)"; \
971 /* Make sure the full string "(nil)" is printed. */ \
972 if (prec < 5) \
973 prec = 5; \
974 is_long = 0; /* This is no wide-char string. */ \
975 goto LABEL (print_string); \
978 /* NOTREACHED */ \
980 LABEL (form_number): \
981 /* Answer the count of characters written. */ \
982 if (fspec == NULL) \
984 if (is_longlong) \
985 *(long long int *) va_arg (ap, void *) = done; \
986 else if (is_long) \
987 *(long int *) va_arg (ap, void *) = done; \
988 else if (!is_short) \
989 *(int *) va_arg (ap, void *) = done; \
990 else \
991 *(short int *) va_arg (ap, void *) = done; \
993 else \
994 if (is_longlong) \
995 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
996 else if (is_long) \
997 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
998 else if (!is_short) \
999 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
1000 else \
1001 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
1002 break; \
1004 LABEL (form_strerror): \
1005 /* Print description of error ERRNO. */ \
1006 string = \
1007 (char *) __strerror_r (save_errno, work_buffer, sizeof work_buffer); \
1008 is_long = 0; /* This is no wide-char string. */ \
1009 goto LABEL (print_string)
1012 /* Sanity check of arguments. */
1013 ARGCHECK (s, format);
1015 if (UNBUFFERED_P (s))
1016 /* Use a helper function which will allocate a local temporary buffer
1017 for the stream and then call us again. */
1018 return buffered_vfprintf (s, format, ap);
1020 /* Initialize local variables. */
1021 done = 0;
1022 grouping = (const char *) -1;
1023 #ifdef __va_copy
1024 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1025 since on some systems `va_list' is not an integral type. */
1026 __va_copy (ap_save, ap);
1027 #else
1028 ap_save = ap;
1029 #endif
1030 nspecs_done = 0;
1032 /* Put state for processing format string in initial state. */
1033 memset (&mbstate, '\0', sizeof (mbstate_t));
1035 /* Find the first format specifier. */
1036 f = lead_str_end = find_spec (format, &mbstate);
1038 /* Lock stream. */
1039 #ifdef USE_IN_LIBIO
1040 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
1041 _IO_flockfile (s);
1042 #else
1043 __libc_cleanup_region_start ((void (*) (void *)) &__funlockfile, s);
1044 __flockfile (s);
1045 #endif
1047 /* Write the literal text before the first format. */
1048 outstring ((const UCHAR_T *) format,
1049 lead_str_end - (const UCHAR_T *) format);
1051 /* If we only have to print a simple string, return now. */
1052 if (*f == L_('\0'))
1053 goto all_done;
1055 /* Process whole format string. */
1058 #define REF(Name) &&do_##Name
1059 #define LABEL(Name) do_##Name
1060 STEP0_3_TABLE;
1061 STEP4_TABLE;
1063 union printf_arg *args_value; /* This is not used here but ... */
1064 int is_negative; /* Flag for negative number. */
1065 union
1067 unsigned long long int longlong;
1068 unsigned long int word;
1069 } number;
1070 int base;
1071 union printf_arg the_arg;
1072 char *string; /* Pointer to argument string. */
1073 int alt = 0; /* Alternate format. */
1074 int space = 0; /* Use space prefix if no sign is needed. */
1075 int left = 0; /* Left-justify output. */
1076 int showsign = 0; /* Always begin with plus or minus sign. */
1077 int group = 0; /* Print numbers according grouping rules. */
1078 int is_long_double = 0; /* Argument is long double/ long long int. */
1079 int is_short = 0; /* Argument is long int. */
1080 int is_long = 0; /* Argument is short int. */
1081 int is_char = 0; /* Argument is promoted (unsigned) char. */
1082 int width = 0; /* Width of output; 0 means none specified. */
1083 int prec = -1; /* Precision of output; -1 means none specified. */
1084 char pad = ' '; /* Padding character. */
1085 CHAR_T spec;
1087 workend = &work_buffer[sizeof (work_buffer) - 1];
1089 /* Get current character in format string. */
1090 JUMP (*++f, step0_jumps);
1092 /* ' ' flag. */
1093 LABEL (flag_space):
1094 space = 1;
1095 JUMP (*++f, step0_jumps);
1097 /* '+' flag. */
1098 LABEL (flag_plus):
1099 showsign = 1;
1100 JUMP (*++f, step0_jumps);
1102 /* The '-' flag. */
1103 LABEL (flag_minus):
1104 left = 1;
1105 pad = L_(' ');
1106 JUMP (*++f, step0_jumps);
1108 /* The '#' flag. */
1109 LABEL (flag_hash):
1110 alt = 1;
1111 JUMP (*++f, step0_jumps);
1113 /* The '0' flag. */
1114 LABEL (flag_zero):
1115 if (!left)
1116 pad = L_('0');
1117 JUMP (*++f, step0_jumps);
1119 /* The '\'' flag. */
1120 LABEL (flag_quote):
1121 group = 1;
1123 /* XXX Completely wrong. Use wctob. */
1124 if (grouping == (const char *) -1)
1126 mbstate_t mbstate;
1128 /* Figure out the thousands separator character. */
1129 memset (&mbstate, '\0', sizeof (mbstate));
1130 if (__mbrtowc (&thousands_sep,
1131 _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
1132 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP)),
1133 &mbstate) <= 0)
1134 thousands_sep = (wchar_t)
1135 *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1136 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1137 if (*grouping == '\0' || *grouping == CHAR_MAX
1138 || thousands_sep == L'\0')
1139 grouping = NULL;
1141 JUMP (*++f, step0_jumps);
1143 /* Get width from argument. */
1144 LABEL (width_asterics):
1146 const UCHAR_T *tmp; /* Temporary value. */
1148 tmp = ++f;
1149 if (ISDIGIT (*tmp) && read_int (&tmp) && *tmp == L_('$'))
1150 /* The width comes from a positional parameter. */
1151 goto do_positional;
1153 width = va_arg (ap, int);
1155 /* Negative width means left justified. */
1156 if (width < 0)
1158 width = -width;
1159 pad = L_(' ');
1160 left = 1;
1163 if (width + 32 >= sizeof (work_buffer))
1164 /* We have to use a special buffer. The "32" is just a safe
1165 bet for all the output which is not counted in the width. */
1166 workend = alloca (width + 32) + (width + 31);
1168 JUMP (*f, step1_jumps);
1170 /* Given width in format string. */
1171 LABEL (width):
1172 width = read_int (&f);
1174 if (width + 32 >= sizeof (work_buffer))
1175 /* We have to use a special buffer. The "32" is just a safe
1176 bet for all the output which is not counted in the width. */
1177 workend = alloca (width + 32) + (width + 31);
1178 if (*f == L_('$'))
1179 /* Oh, oh. The argument comes from a positional parameter. */
1180 goto do_positional;
1181 JUMP (*f, step1_jumps);
1183 LABEL (precision):
1184 ++f;
1185 if (*f == L_('*'))
1187 const UCHAR_T *tmp; /* Temporary value. */
1189 tmp = ++f;
1190 if (ISDIGIT (*tmp) && read_int (&tmp) > 0 && *tmp == L_('$'))
1191 /* The precision comes from a positional parameter. */
1192 goto do_positional;
1194 prec = va_arg (ap, int);
1196 /* If the precision is negative the precision is omitted. */
1197 if (prec < 0)
1198 prec = -1;
1200 else if (ISDIGIT (*f))
1201 prec = read_int (&f);
1202 else
1203 prec = 0;
1204 if (prec > width && prec + 32 > sizeof (work_buffer))
1205 workend = alloca (spec + 32) + (spec + 31);
1206 JUMP (*f, step2_jumps);
1208 /* Process 'h' modifier. There might another 'h' following. */
1209 LABEL (mod_half):
1210 is_short = 1;
1211 JUMP (*++f, step3a_jumps);
1213 /* Process 'hh' modifier. */
1214 LABEL (mod_halfhalf):
1215 is_short = 0;
1216 is_char = 1;
1217 JUMP (*++f, step4_jumps);
1219 /* Process 'l' modifier. There might another 'l' following. */
1220 LABEL (mod_long):
1221 is_long = 1;
1222 JUMP (*++f, step3b_jumps);
1224 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1225 allowed to follow. */
1226 LABEL (mod_longlong):
1227 is_long_double = 1;
1228 JUMP (*++f, step4_jumps);
1230 LABEL (mod_size_t):
1231 is_longlong = sizeof (size_t) > sizeof (unsigned long int);
1232 is_long = sizeof (size_t) > sizeof (unsigned int);
1233 JUMP (*++f, step4_jumps);
1235 LABEL (mod_ptrdiff_t):
1236 is_longlong = sizeof (ptrdiff_t) > sizeof (unsigned long int);
1237 is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
1238 JUMP (*++f, step4_jumps);
1240 LABEL (mod_intmax_t):
1241 is_longlong = sizeof (intmax_t) > sizeof (unsigned long int);
1242 is_long = sizeof (intmax_t) > sizeof (unsigned int);
1243 JUMP (*++f, step4_jumps);
1245 /* Process current format. */
1246 while (1)
1248 process_arg (((struct printf_spec *) NULL));
1250 LABEL (form_unknown):
1251 if (spec == L_('\0'))
1253 /* The format string ended before the specifier is complete. */
1254 done = -1;
1255 goto all_done;
1258 /* If we are in the fast loop force entering the complicated
1259 one. */
1260 goto do_positional;
1263 /* The format is correctly handled. */
1264 ++nspecs_done;
1266 /* Look for next format specifier. */
1267 f = find_spec ((end_of_spec = ++f), &mbstate);
1269 /* Write the following constant string. */
1270 outstring (end_of_spec, f - end_of_spec);
1272 while (*f != L_('\0'));
1274 /* Unlock stream and return. */
1275 goto all_done;
1277 /* Here starts the more complex loop to handle positional parameters. */
1278 do_positional:
1280 /* Array with information about the needed arguments. This has to
1281 be dynamically extensible. */
1282 size_t nspecs = 0;
1283 size_t nspecs_max = 32; /* A more or less arbitrary start value. */
1284 struct printf_spec *specs
1285 = alloca (nspecs_max * sizeof (struct printf_spec));
1287 /* The number of arguments the format string requests. This will
1288 determine the size of the array needed to store the argument
1289 attributes. */
1290 size_t nargs = 0;
1291 int *args_type;
1292 union printf_arg *args_value;
1294 /* Positional parameters refer to arguments directly. This could
1295 also determine the maximum number of arguments. Track the
1296 maximum number. */
1297 size_t max_ref_arg = 0;
1299 /* Just a counter. */
1300 size_t cnt;
1303 if (grouping == (const char *) -1)
1305 mbstate_t mbstate;
1307 /* Figure out the thousands separator character. */
1308 memset (&mbstate, '\0', sizeof (mbstate));
1309 if (__mbrtowc (&thousands_sep,
1310 _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
1311 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP)),
1312 &mbstate) <= 0)
1313 thousands_sep = (wchar_t) *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1314 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1315 if (*grouping == '\0' || *grouping == CHAR_MAX
1316 || thousands_sep == L'\0')
1317 grouping = NULL;
1320 for (f = lead_str_end; *f != '\0'; f = specs[nspecs++].next_fmt)
1322 if (nspecs >= nspecs_max)
1324 /* Extend the array of format specifiers. */
1325 struct printf_spec *old = specs;
1327 nspecs_max *= 2;
1328 specs = alloca (nspecs_max * sizeof (struct printf_spec));
1330 if (specs == &old[nspecs])
1331 /* Stack grows up, OLD was the last thing allocated;
1332 extend it. */
1333 nspecs_max += nspecs_max / 2;
1334 else
1336 /* Copy the old array's elements to the new space. */
1337 memcpy (specs, old, nspecs * sizeof (struct printf_spec));
1338 if (old == &specs[nspecs])
1339 /* Stack grows down, OLD was just below the new
1340 SPECS. We can use that space when the new space
1341 runs out. */
1342 nspecs_max += nspecs_max / 2;
1346 /* Parse the format specifier. */
1347 nargs += parse_one_spec (f, nargs, &specs[nspecs], &max_ref_arg,
1348 &mbstate);
1351 /* Determine the number of arguments the format string consumes. */
1352 nargs = MAX (nargs, max_ref_arg);
1354 /* Allocate memory for the argument descriptions. */
1355 args_type = alloca (nargs * sizeof (int));
1356 memset (args_type, 0, nargs * sizeof (int));
1357 args_value = alloca (nargs * sizeof (union printf_arg));
1359 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1360 still zero after this loop, format is invalid. For now we
1361 simply use 0 as the value. */
1363 /* Fill in the types of all the arguments. */
1364 for (cnt = 0; cnt < nspecs; ++cnt)
1366 /* If the width is determined by an argument this is an int. */
1367 if (specs[cnt].width_arg != -1)
1368 args_type[specs[cnt].width_arg] = PA_INT;
1370 /* If the precision is determined by an argument this is an int. */
1371 if (specs[cnt].prec_arg != -1)
1372 args_type[specs[cnt].prec_arg] = PA_INT;
1374 switch (specs[cnt].ndata_args)
1376 case 0: /* No arguments. */
1377 break;
1378 case 1: /* One argument; we already have the type. */
1379 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1380 break;
1381 default:
1382 /* We have more than one argument for this format spec.
1383 We must call the arginfo function again to determine
1384 all the types. */
1385 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1386 (&specs[cnt].info,
1387 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg]);
1388 break;
1392 /* Now we know all the types and the order. Fill in the argument
1393 values. */
1394 for (cnt = 0; cnt < nargs; ++cnt)
1395 switch (args_type[cnt])
1397 #define T(tag, mem, type) \
1398 case tag: \
1399 args_value[cnt].mem = va_arg (ap_save, type); \
1400 break
1402 T (PA_CHAR, pa_char, int); /* Promoted. */
1403 T (PA_WCHAR, pa_wchar, wint_t);
1404 T (PA_INT|PA_FLAG_SHORT, pa_short_int, int); /* Promoted. */
1405 T (PA_INT, pa_int, int);
1406 T (PA_INT|PA_FLAG_LONG, pa_long_int, long int);
1407 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1408 T (PA_FLOAT, pa_float, double); /* Promoted. */
1409 T (PA_DOUBLE, pa_double, double);
1410 T (PA_DOUBLE|PA_FLAG_LONG_DOUBLE, pa_long_double, long double);
1411 T (PA_STRING, pa_string, const char *);
1412 T (PA_WSTRING, pa_wstring, const wchar_t *);
1413 T (PA_POINTER, pa_pointer, void *);
1414 #undef T
1415 default:
1416 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1417 args_value[cnt].pa_pointer = va_arg (ap_save, void *);
1418 else
1419 args_value[cnt].pa_long_double = 0.0;
1420 break;
1423 /* Now walk through all format specifiers and process them. */
1424 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1426 #undef REF
1427 #define REF(Name) &&do2_##Name
1428 #undef LABEL
1429 #define LABEL(Name) do2_##Name
1430 STEP4_TABLE;
1432 int is_negative;
1433 union
1435 unsigned long long int longlong;
1436 unsigned long int word;
1437 } number;
1438 int base;
1439 union printf_arg the_arg;
1440 char *string; /* Pointer to argument string. */
1442 /* Fill variables from values in struct. */
1443 int alt = specs[nspecs_done].info.alt;
1444 int space = specs[nspecs_done].info.space;
1445 int left = specs[nspecs_done].info.left;
1446 int showsign = specs[nspecs_done].info.showsign;
1447 int group = specs[nspecs_done].info.group;
1448 int is_long_double = specs[nspecs_done].info.is_long_double;
1449 int is_short = specs[nspecs_done].info.is_short;
1450 int is_char = specs[nspecs_done].info.is_char;
1451 int is_long = specs[nspecs_done].info.is_long;
1452 int width = specs[nspecs_done].info.width;
1453 int prec = specs[nspecs_done].info.prec;
1454 char pad = specs[nspecs_done].info.pad;
1455 CHAR_T spec = specs[nspecs_done].info.spec;
1457 /* Fill in last information. */
1458 if (specs[nspecs_done].width_arg != -1)
1460 /* Extract the field width from an argument. */
1461 specs[nspecs_done].info.width =
1462 args_value[specs[nspecs_done].width_arg].pa_int;
1464 if (specs[nspecs_done].info.width < 0)
1465 /* If the width value is negative left justification is
1466 selected and the value is taken as being positive. */
1468 specs[nspecs_done].info.width *= -1;
1469 left = specs[nspecs_done].info.left = 1;
1471 width = specs[nspecs_done].info.width;
1474 if (specs[nspecs_done].prec_arg != -1)
1476 /* Extract the precision from an argument. */
1477 specs[nspecs_done].info.prec =
1478 args_value[specs[nspecs_done].prec_arg].pa_int;
1480 if (specs[nspecs_done].info.prec < 0)
1481 /* If the precision is negative the precision is
1482 omitted. */
1483 specs[nspecs_done].info.prec = -1;
1485 prec = specs[nspecs_done].info.prec;
1488 /* Maybe the buffer is too small. */
1489 if (MAX (prec, width) + 32 > sizeof (work_buffer))
1490 workend = alloca (MAX (prec, width) + 32) + (MAX (prec, width) + 31);
1492 /* Process format specifiers. */
1493 while (1)
1495 JUMP (spec, step4_jumps);
1497 process_arg ((&specs[nspecs_done]));
1499 LABEL (form_unknown):
1501 extern printf_function **__printf_function_table;
1502 int function_done;
1503 printf_function *function;
1504 unsigned int i;
1505 const void **ptr;
1507 function =
1508 (__printf_function_table == NULL ? NULL :
1509 __printf_function_table[specs[nspecs_done].info.spec]);
1511 if (function == NULL)
1512 function = &printf_unknown;
1514 ptr = alloca (specs[nspecs_done].ndata_args
1515 * sizeof (const void *));
1517 /* Fill in an array of pointers to the argument values. */
1518 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
1519 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1521 /* Call the function. */
1522 function_done = (*function) (s, &specs[nspecs_done].info, ptr);
1524 /* If an error occurred we don't have information about #
1525 of chars. */
1526 if (function_done < 0)
1528 done = -1;
1529 goto all_done;
1532 done += function_done;
1534 break;
1537 /* Write the following constant string. */
1538 outstring (specs[nspecs_done].end_of_fmt,
1539 specs[nspecs_done].next_fmt
1540 - specs[nspecs_done].end_of_fmt);
1544 all_done:
1545 /* Unlock the stream. */
1546 #ifdef USE_IN_LIBIO
1547 _IO_funlockfile (s);
1548 #else
1549 __funlockfile (s);
1550 #endif
1551 __libc_cleanup_region_end (0);
1553 return done;
1556 #ifdef USE_IN_LIBIO
1557 # undef vfprintf
1558 # ifdef strong_alias
1559 /* This is for glibc. */
1560 strong_alias (_IO_vfprintf, vfprintf);
1561 # else
1562 # if defined __ELF__ || defined __GNU_LIBRARY__
1563 # include <gnu-stabs.h>
1564 # ifdef weak_alias
1565 weak_alias (_IO_vfprintf, vfprintf);
1566 # endif
1567 # endif
1568 # endif
1569 #endif
1571 /* Handle an unknown format specifier. This prints out a canonicalized
1572 representation of the format spec itself. */
1573 static int
1574 printf_unknown (FILE *s, const struct printf_info *info,
1575 const void *const *args)
1578 int done = 0;
1579 char work_buffer[MAX (info->width, info->spec) + 32];
1580 char *const workend = &work_buffer[sizeof (work_buffer) - 1];
1581 register char *w;
1583 outchar ('%');
1585 if (info->alt)
1586 outchar ('#');
1587 if (info->group)
1588 outchar ('\'');
1589 if (info->showsign)
1590 outchar ('+');
1591 else if (info->space)
1592 outchar (' ');
1593 if (info->left)
1594 outchar ('-');
1595 if (info->pad == '0')
1596 outchar ('0');
1598 if (info->width != 0)
1600 w = _itoa_word (info->width, workend + 1, 10, 0);
1601 while (w <= workend)
1602 outchar (*w++);
1605 if (info->prec != -1)
1607 outchar ('.');
1608 w = _itoa_word (info->prec, workend + 1, 10, 0);
1609 while (w <= workend)
1610 outchar (*w++);
1613 if (info->spec != '\0')
1614 outchar (info->spec);
1616 all_done:
1617 return done;
1620 /* Group the digits according to the grouping rules of the current locale.
1621 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1622 static char *
1623 internal_function
1624 group_number (CHAR_T *w, CHAR_T *rear_ptr, const CHAR_T *grouping,
1625 wchar_t thousands_sep)
1627 int len;
1628 char *src, *s;
1630 /* We treat all negative values like CHAR_MAX. */
1632 if (*grouping == CHAR_MAX || *grouping <= 0)
1633 /* No grouping should be done. */
1634 return w;
1636 len = *grouping;
1638 /* Copy existing string so that nothing gets overwritten. */
1639 src = (char *) alloca (rear_ptr - w);
1640 s = (char *) __mempcpy (src, w + 1, rear_ptr - w) - 1;
1641 w = rear_ptr;
1643 /* Process all characters in the string. */
1644 while (s >= src)
1646 *w-- = *s--;
1648 if (--len == 0 && s >= src)
1650 /* A new group begins. */
1651 *w-- = thousands_sep;
1653 len = *grouping++;
1654 if (*grouping == '\0')
1655 /* The previous grouping repeats ad infinitum. */
1656 --grouping;
1657 else if (*grouping == CHAR_MAX
1658 #if CHAR_MIN < 0
1659 || *grouping < 0
1660 #endif
1663 /* No further grouping to be done.
1664 Copy the rest of the number. */
1666 *w-- = *s--;
1667 while (s >= src);
1668 break;
1672 return w;
1675 #ifdef USE_IN_LIBIO
1676 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
1677 struct helper_file
1679 struct _IO_FILE_plus _f;
1680 _IO_FILE *_put_stream;
1681 #ifdef _IO_MTSAFE_IO
1682 _IO_lock_t lock;
1683 #endif
1686 static int
1687 _IO_helper_overflow (_IO_FILE *s, int c)
1689 _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
1690 int used = s->_IO_write_ptr - s->_IO_write_base;
1691 if (used)
1693 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
1694 s->_IO_write_ptr -= written;
1696 return PUTC (c, s);
1699 static const struct _IO_jump_t _IO_helper_jumps =
1701 JUMP_INIT_DUMMY,
1702 JUMP_INIT (finish, _IO_default_finish),
1703 JUMP_INIT (overflow, _IO_helper_overflow),
1704 JUMP_INIT (underflow, _IO_default_underflow),
1705 JUMP_INIT (uflow, _IO_default_uflow),
1706 JUMP_INIT (pbackfail, _IO_default_pbackfail),
1707 JUMP_INIT (xsputn, _IO_default_xsputn),
1708 JUMP_INIT (xsgetn, _IO_default_xsgetn),
1709 JUMP_INIT (seekoff, _IO_default_seekoff),
1710 JUMP_INIT (seekpos, _IO_default_seekpos),
1711 JUMP_INIT (setbuf, _IO_default_setbuf),
1712 JUMP_INIT (sync, _IO_default_sync),
1713 JUMP_INIT (doallocate, _IO_default_doallocate),
1714 JUMP_INIT (read, _IO_default_read),
1715 JUMP_INIT (write, _IO_default_write),
1716 JUMP_INIT (seek, _IO_default_seek),
1717 JUMP_INIT (close, _IO_default_close),
1718 JUMP_INIT (stat, _IO_default_stat)
1721 static int
1722 internal_function
1723 buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format,
1724 _IO_va_list args)
1726 char buf[_IO_BUFSIZ];
1727 struct helper_file helper;
1728 register _IO_FILE *hp = (_IO_FILE *) &helper;
1729 int result, to_flush;
1731 /* Initialize helper. */
1732 helper._put_stream = s;
1733 hp->_IO_write_base = buf;
1734 hp->_IO_write_ptr = buf;
1735 hp->_IO_write_end = buf + sizeof buf;
1736 hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS;
1737 #if _IO_JUMPS_OFFSET
1738 hp->_vtable_offset = 0;
1739 #endif
1740 #ifdef _IO_MTSAFE_IO
1741 hp->_lock = &helper.lock;
1742 __libc_lock_init (*hp->_lock);
1743 #endif
1744 _IO_JUMPS (hp) = (struct _IO_jump_t *) &_IO_helper_jumps;
1746 /* Now print to helper instead. */
1747 result = _IO_vfprintf (hp, format, args);
1749 /* Now flush anything from the helper to the S. */
1750 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
1752 if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
1753 return -1;
1756 return result;
1759 #else /* !USE_IN_LIBIO */
1761 static int
1762 internal_function
1763 buffered_vfprintf (register FILE *s, const CHAR_T *format, va_list args)
1765 char buf[BUFSIZ];
1766 int result;
1768 s->__bufp = s->__buffer = buf;
1769 s->__bufsize = sizeof buf;
1770 s->__put_limit = s->__buffer + s->__bufsize;
1771 s->__get_limit = s->__buffer;
1773 /* Now use buffer to print. */
1774 result = vfprintf (s, format, args);
1776 if (fflush (s) == EOF)
1777 result = -1;
1778 s->__buffer = s->__bufp = s->__get_limit = s->__put_limit = NULL;
1779 s->__bufsize = 0;
1781 return result;
1784 /* Pads string with given number of a specified character.
1785 This code is taken from iopadn.c of the GNU I/O library. */
1786 #define PADSIZE 16
1787 static const CHAR_T blanks[PADSIZE] =
1788 { L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '),
1789 L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' ') };
1790 static const CHAR_T zeroes[PADSIZE] =
1791 { L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'),
1792 L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0') };
1794 ssize_t
1795 #ifndef COMPILE_WPRINTF
1796 __printf_pad (FILE *s, char pad, size_t count)
1797 #else
1798 __wprintf_pad (FILE *s, wchar_t pad, size_t count)
1799 #endif
1801 const CHAR_T *padptr;
1802 register size_t i;
1804 padptr = pad == L_(' ') ? blanks : zeroes;
1806 for (i = count; i >= PADSIZE; i -= PADSIZE)
1807 if (PUT (s, padptr, PADSIZE) != PADSIZE)
1808 return -1;
1809 if (i > 0)
1810 if (PUT (s, padptr, i) != i)
1811 return -1;
1813 return count;
1815 #undef PADSIZE
1816 #endif /* USE_IN_LIBIO */