Update.
[glibc.git] / stdio-common / vfprintf.c
bloba3281c2e0ea0f026fcce6a3832cc94da78ee2db6
1 /* Copyright (C) 1991,92,93,94,95,96,97,98 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) return -1; \
58 done += __res; }
59 # endif
60 #else
61 # define vfprintf vfwprintf
62 # define CHAR_T wchar_t
63 # define UCHAR_T uwchar_t
64 # define INT_T wint_t
65 # define L_(Str) L##Str
66 # define ISDIGIT(Ch) iswdigit (Ch)
68 # ifdef USE_IN_LIBIO
69 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
70 # define PAD(Padchar) \
71 if (width > 0) \
72 done += _IO_wpadn (s, (Padchar), width)
73 # else
74 # define PUTC(C, F) wputc (C, F)
75 ssize_t __wprintf_pad __P ((FILE *, wchar_t pad, size_t n));
76 # define PAD(Padchar) \
77 if (width > 0) \
78 { ssize_t __res = __wprintf_pad (s, (Padchar), width); \
79 if (__res == -1) return -1; \
80 done += __res; }
81 # endif
82 #endif
84 /* Include the shared code for parsing the format string. */
85 #include "printf-parse.h"
88 #ifdef USE_IN_LIBIO
89 /* This code is for use in libio. */
90 # include <libioP.h>
91 # define PUTC(C, F) _IO_putc_unlocked (C, F)
92 # define vfprintf _IO_vfprintf
93 # define FILE _IO_FILE
94 # undef va_list
95 # define va_list _IO_va_list
96 # undef BUFSIZ
97 # define BUFSIZ _IO_BUFSIZ
98 # define ARGCHECK(S, Format) \
99 do \
101 /* Check file argument for consistence. */ \
102 CHECK_FILE (S, -1); \
103 if (S->_flags & _IO_NO_WRITES) \
105 __set_errno (EBADF); \
106 return -1; \
108 if (Format == NULL) \
110 MAYBE_SET_EINVAL; \
111 return -1; \
113 } while (0)
114 # define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
115 #else /* ! USE_IN_LIBIO */
116 /* This code is for use in the GNU C library. */
117 # include <stdio.h>
118 # define PUT(F, S, N) fwrite (S, 1, N, F)
119 # define ARGCHECK(S, Format) \
120 do \
122 /* Check file argument for consistence. */ \
123 if (!__validfp (S) || !S->__mode.__write) \
125 __set_errno (EBADF); \
126 return -1; \
128 if (Format == NULL) \
130 __set_errno (EINVAL); \
131 return -1; \
133 if (!S->__seen) \
135 if (__flshfp (S, EOF) == EOF) \
136 return -1; \
139 while (0)
140 # define UNBUFFERED_P(s) ((s)->__buffer == NULL)
142 /* XXX These declarations should go as soon as the stdio header files
143 have these prototypes. */
144 extern void __flockfile (FILE *);
145 extern void __funlockfile (FILE *);
146 #endif /* USE_IN_LIBIO */
149 #define outchar(Ch) \
150 do \
152 register const int outc = (Ch); \
153 if (PUTC (outc, s) == EOF) \
154 return -1; \
155 else \
156 ++done; \
158 while (0)
160 #define outstring(String, Len) \
161 do \
163 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
164 return -1; \
165 done += (Len); \
167 while (0)
169 /* For handling long_double and longlong we use the same flag. */
170 #ifndef is_longlong
171 # define is_longlong is_long_double
172 #endif
175 /* Global variables. */
176 static const char null[] = "(null)";
179 /* Helper function to provide temporary buffering for unbuffered streams. */
180 static int buffered_vfprintf __P ((FILE *stream, const CHAR_T *fmt, va_list))
181 internal_function;
183 /* Handle unknown format specifier. */
184 static int printf_unknown __P ((FILE *, const struct printf_info *,
185 const void *const *));
187 /* Group digits of number string. */
188 static char *group_number __P ((CHAR_T *, CHAR_T *, const CHAR_T *, wchar_t))
189 internal_function;
192 /* The function itself. */
194 vfprintf (FILE *s, const CHAR_T *format, va_list ap)
196 /* The character used as thousands separator. */
197 wchar_t thousands_sep;
199 /* The string describing the size of groups of digits. */
200 const char *grouping;
202 /* Place to accumulate the result. */
203 int done;
205 /* Current character in format string. */
206 const UCHAR_T *f;
208 /* End of leading constant string. */
209 const UCHAR_T *lead_str_end;
211 /* Points to next format specifier. */
212 const UCHAR_T *end_of_spec;
214 /* Buffer intermediate results. */
215 char work_buffer[1000];
216 #define workend (&work_buffer[sizeof (work_buffer) - 1])
218 /* State for restartable multibyte character handling functions. */
219 mbstate_t mbstate;
221 /* We have to save the original argument pointer. */
222 va_list ap_save;
224 /* Count number of specifiers we already processed. */
225 int nspecs_done;
227 /* For the %m format we may need the current `errno' value. */
228 int save_errno = errno;
231 /* This table maps a character into a number representing a
232 class. In each step there is a destination label for each
233 class. */
234 static const int jump_table[] =
236 /* ' ' */ 1, 0, 0, /* '#' */ 4,
237 0, /* '%' */ 14, 0, /* '\''*/ 6,
238 0, 0, /* '*' */ 7, /* '+' */ 2,
239 0, /* '-' */ 3, /* '.' */ 9, 0,
240 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
241 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
242 /* '8' */ 8, /* '9' */ 8, 0, 0,
243 0, 0, 0, 0,
244 0, /* 'A' */ 26, 0, /* 'C' */ 25,
245 0, /* 'E' */ 19, 0, /* 'G' */ 19,
246 0, 0, 0, 0,
247 /* 'L' */ 12, 0, 0, 0,
248 0, 0, 0, /* 'S' */ 21,
249 0, 0, 0, 0,
250 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
251 0, 0, 0, 0,
252 0, /* 'a' */ 26, 0, /* 'c' */ 20,
253 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
254 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
255 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
256 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
257 /* 't' */ 27, /* 'u' */ 16, 0, 0,
258 /* 'x' */ 18, 0, /* 'z' */ 13
261 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < ' ' || (Ch) > 'z')
262 #define CHAR_CLASS(Ch) (jump_table[(int) (Ch) - ' '])
263 #define JUMP(ChExpr, table) \
264 do \
266 const void *ptr; \
267 spec = (ChExpr); \
268 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
269 : table[CHAR_CLASS (spec)]; \
270 goto *ptr; \
272 while (0)
274 #define STEP0_3_TABLE \
275 /* Step 0: at the beginning. */ \
276 static const void *step0_jumps[29] = \
278 REF (form_unknown), \
279 REF (flag_space), /* for ' ' */ \
280 REF (flag_plus), /* for '+' */ \
281 REF (flag_minus), /* for '-' */ \
282 REF (flag_hash), /* for '<hash>' */ \
283 REF (flag_zero), /* for '0' */ \
284 REF (flag_quote), /* for '\'' */ \
285 REF (width_asterics), /* for '*' */ \
286 REF (width), /* for '1'...'9' */ \
287 REF (precision), /* for '.' */ \
288 REF (mod_half), /* for 'h' */ \
289 REF (mod_long), /* for 'l' */ \
290 REF (mod_longlong), /* for 'L', 'q' */ \
291 REF (mod_size_t), /* for 'z', 'Z' */ \
292 REF (form_percent), /* for '%' */ \
293 REF (form_integer), /* for 'd', 'i' */ \
294 REF (form_unsigned), /* for 'u' */ \
295 REF (form_octal), /* for 'o' */ \
296 REF (form_hexa), /* for 'X', 'x' */ \
297 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
298 REF (form_character), /* for 'c' */ \
299 REF (form_string), /* for 's', 'S' */ \
300 REF (form_pointer), /* for 'p' */ \
301 REF (form_number), /* for 'n' */ \
302 REF (form_strerror), /* for 'm' */ \
303 REF (form_wcharacter), /* for 'C' */ \
304 REF (form_floathex), /* for 'A', 'a' */ \
305 REF (mod_ptrdiff_t), /* for 't' */ \
306 REF (mod_intmax_t), /* for 'j' */ \
307 }; \
308 /* Step 1: after processing width. */ \
309 static const void *step1_jumps[29] = \
311 REF (form_unknown), \
312 REF (form_unknown), /* for ' ' */ \
313 REF (form_unknown), /* for '+' */ \
314 REF (form_unknown), /* for '-' */ \
315 REF (form_unknown), /* for '<hash>' */ \
316 REF (form_unknown), /* for '0' */ \
317 REF (form_unknown), /* for '\'' */ \
318 REF (form_unknown), /* for '*' */ \
319 REF (form_unknown), /* for '1'...'9' */ \
320 REF (precision), /* for '.' */ \
321 REF (mod_half), /* for 'h' */ \
322 REF (mod_long), /* for 'l' */ \
323 REF (mod_longlong), /* for 'L', 'q' */ \
324 REF (mod_size_t), /* for 'z', 'Z' */ \
325 REF (form_percent), /* for '%' */ \
326 REF (form_integer), /* for 'd', 'i' */ \
327 REF (form_unsigned), /* for 'u' */ \
328 REF (form_octal), /* for 'o' */ \
329 REF (form_hexa), /* for 'X', 'x' */ \
330 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
331 REF (form_character), /* for 'c' */ \
332 REF (form_string), /* for 's', 'S' */ \
333 REF (form_pointer), /* for 'p' */ \
334 REF (form_number), /* for 'n' */ \
335 REF (form_strerror), /* for 'm' */ \
336 REF (form_wcharacter), /* for 'C' */ \
337 REF (form_floathex), /* for 'A', 'a' */ \
338 REF (mod_ptrdiff_t), /* for 't' */ \
339 REF (mod_intmax_t) /* for 'j' */ \
340 }; \
341 /* Step 2: after processing precision. */ \
342 static const void *step2_jumps[29] = \
344 REF (form_unknown), \
345 REF (form_unknown), /* for ' ' */ \
346 REF (form_unknown), /* for '+' */ \
347 REF (form_unknown), /* for '-' */ \
348 REF (form_unknown), /* for '<hash>' */ \
349 REF (form_unknown), /* for '0' */ \
350 REF (form_unknown), /* for '\'' */ \
351 REF (form_unknown), /* for '*' */ \
352 REF (form_unknown), /* for '1'...'9' */ \
353 REF (form_unknown), /* for '.' */ \
354 REF (mod_half), /* for 'h' */ \
355 REF (mod_long), /* for 'l' */ \
356 REF (mod_longlong), /* for 'L', 'q' */ \
357 REF (mod_size_t), /* for 'z', 'Z' */ \
358 REF (form_percent), /* for '%' */ \
359 REF (form_integer), /* for 'd', 'i' */ \
360 REF (form_unsigned), /* for 'u' */ \
361 REF (form_octal), /* for 'o' */ \
362 REF (form_hexa), /* for 'X', 'x' */ \
363 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
364 REF (form_character), /* for 'c' */ \
365 REF (form_string), /* for 's', 'S' */ \
366 REF (form_pointer), /* for 'p' */ \
367 REF (form_number), /* for 'n' */ \
368 REF (form_strerror), /* for 'm' */ \
369 REF (form_wcharacter), /* for 'C' */ \
370 REF (form_floathex), /* for 'A', 'a' */ \
371 REF (mod_ptrdiff_t), /* for 't' */ \
372 REF (mod_intmax_t) /* for 'j' */ \
373 }; \
374 /* Step 3a: after processing first 'h' modifier. */ \
375 static const void *step3a_jumps[29] = \
377 REF (form_unknown), \
378 REF (form_unknown), /* for ' ' */ \
379 REF (form_unknown), /* for '+' */ \
380 REF (form_unknown), /* for '-' */ \
381 REF (form_unknown), /* for '<hash>' */ \
382 REF (form_unknown), /* for '0' */ \
383 REF (form_unknown), /* for '\'' */ \
384 REF (form_unknown), /* for '*' */ \
385 REF (form_unknown), /* for '1'...'9' */ \
386 REF (form_unknown), /* for '.' */ \
387 REF (mod_halfhalf), /* for 'h' */ \
388 REF (form_unknown), /* for 'l' */ \
389 REF (form_unknown), /* for 'L', 'q' */ \
390 REF (form_unknown), /* for 'z', 'Z' */ \
391 REF (form_percent), /* for '%' */ \
392 REF (form_integer), /* for 'd', 'i' */ \
393 REF (form_unsigned), /* for 'u' */ \
394 REF (form_octal), /* for 'o' */ \
395 REF (form_hexa), /* for 'X', 'x' */ \
396 REF (form_unknown), /* for 'E', 'e', 'f', 'G', 'g' */ \
397 REF (form_unknown), /* for 'c' */ \
398 REF (form_unknown), /* for 's', 'S' */ \
399 REF (form_unknown), /* for 'p' */ \
400 REF (form_number), /* for 'n' */ \
401 REF (form_unknown), /* for 'm' */ \
402 REF (form_unknown), /* for 'C' */ \
403 REF (form_unknown), /* for 'A', 'a' */ \
404 REF (form_unknown), /* for 't' */ \
405 REF (form_unknown) /* for 'j' */ \
406 }; \
407 /* Step 3b: after processing first 'l' modifier. */ \
408 static const void *step3b_jumps[29] = \
410 REF (form_unknown), \
411 REF (form_unknown), /* for ' ' */ \
412 REF (form_unknown), /* for '+' */ \
413 REF (form_unknown), /* for '-' */ \
414 REF (form_unknown), /* for '<hash>' */ \
415 REF (form_unknown), /* for '0' */ \
416 REF (form_unknown), /* for '\'' */ \
417 REF (form_unknown), /* for '*' */ \
418 REF (form_unknown), /* for '1'...'9' */ \
419 REF (form_unknown), /* for '.' */ \
420 REF (form_unknown), /* for 'h' */ \
421 REF (mod_longlong), /* for 'l' */ \
422 REF (form_unknown), /* for 'L', 'q' */ \
423 REF (form_unknown), /* for 'z', 'Z' */ \
424 REF (form_percent), /* for '%' */ \
425 REF (form_integer), /* for 'd', 'i' */ \
426 REF (form_unsigned), /* for 'u' */ \
427 REF (form_octal), /* for 'o' */ \
428 REF (form_hexa), /* for 'X', 'x' */ \
429 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
430 REF (form_character), /* for 'c' */ \
431 REF (form_string), /* for 's', 'S' */ \
432 REF (form_pointer), /* for 'p' */ \
433 REF (form_number), /* for 'n' */ \
434 REF (form_strerror), /* for 'm' */ \
435 REF (form_wcharacter), /* for 'C' */ \
436 REF (form_floathex), /* for 'A', 'a' */ \
437 REF (form_unknown), /* for 't' */ \
438 REF (form_unknown) /* for 'j' */ \
441 #define STEP4_TABLE \
442 /* Step 4: processing format specifier. */ \
443 static const void *step4_jumps[29] = \
445 REF (form_unknown), \
446 REF (form_unknown), /* for ' ' */ \
447 REF (form_unknown), /* for '+' */ \
448 REF (form_unknown), /* for '-' */ \
449 REF (form_unknown), /* for '<hash>' */ \
450 REF (form_unknown), /* for '0' */ \
451 REF (form_unknown), /* for '\'' */ \
452 REF (form_unknown), /* for '*' */ \
453 REF (form_unknown), /* for '1'...'9' */ \
454 REF (form_unknown), /* for '.' */ \
455 REF (form_unknown), /* for 'h' */ \
456 REF (form_unknown), /* for 'l' */ \
457 REF (form_unknown), /* for 'L', 'q' */ \
458 REF (form_unknown), /* for 'z', 'Z' */ \
459 REF (form_percent), /* for '%' */ \
460 REF (form_integer), /* for 'd', 'i' */ \
461 REF (form_unsigned), /* for 'u' */ \
462 REF (form_octal), /* for 'o' */ \
463 REF (form_hexa), /* for 'X', 'x' */ \
464 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
465 REF (form_character), /* for 'c' */ \
466 REF (form_string), /* for 's', 'S' */ \
467 REF (form_pointer), /* for 'p' */ \
468 REF (form_number), /* for 'n' */ \
469 REF (form_strerror), /* for 'm' */ \
470 REF (form_wcharacter), /* for 'C' */ \
471 REF (form_floathex), /* for 'A', 'a' */ \
472 REF (form_unknown), /* for 't' */ \
473 REF (form_unknown) /* for 'j' */ \
477 #define process_arg(fspec) \
478 /* Start real work. We know about all flags and modifiers and \
479 now process the wanted format specifier. */ \
480 LABEL (form_percent): \
481 /* Write a literal "%". */ \
482 outchar ('%'); \
483 break; \
485 LABEL (form_integer): \
486 /* Signed decimal integer. */ \
487 base = 10; \
489 if (is_longlong) \
491 long long int signed_number; \
493 if (fspec == NULL) \
494 signed_number = va_arg (ap, long long int); \
495 else \
496 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
498 is_negative = signed_number < 0; \
499 number.longlong = is_negative ? (- signed_number) : signed_number; \
501 goto LABEL (longlong_number); \
503 else \
505 long int signed_number; \
507 if (fspec == NULL) \
509 if (is_long) \
510 signed_number = va_arg (ap, long int); \
511 else /* `char' and `short int' will be promoted to `int'. */ \
512 signed_number = va_arg (ap, int); \
514 else \
515 if (is_long) \
516 signed_number = args_value[fspec->data_arg].pa_long_int; \
517 else \
518 signed_number = args_value[fspec->data_arg].pa_int; \
520 is_negative = signed_number < 0; \
521 number.word = is_negative ? (- signed_number) : signed_number; \
523 goto LABEL (number); \
525 /* NOTREACHED */ \
527 LABEL (form_unsigned): \
528 /* Unsigned decimal integer. */ \
529 base = 10; \
530 goto LABEL (unsigned_number); \
531 /* NOTREACHED */ \
533 LABEL (form_octal): \
534 /* Unsigned octal integer. */ \
535 base = 8; \
536 goto LABEL (unsigned_number); \
537 /* NOTREACHED */ \
539 LABEL (form_hexa): \
540 /* Unsigned hexadecimal integer. */ \
541 base = 16; \
543 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
545 /* ISO specifies the `+' and ` ' flags only for signed \
546 conversions. */ \
547 is_negative = 0; \
548 showsign = 0; \
549 space = 0; \
551 if (is_longlong) \
553 if (fspec == NULL) \
554 number.longlong = va_arg (ap, unsigned long long int); \
555 else \
556 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
558 LABEL (longlong_number): \
559 if (prec < 0) \
560 /* Supply a default precision if none was given. */ \
561 prec = 1; \
562 else \
563 /* We have to take care for the '0' flag. If a precision \
564 is given it must be ignored. */ \
565 pad = ' '; \
567 /* If the precision is 0 and the number is 0 nothing has to \
568 be written for the number, except for the 'o' format in \
569 alternate form. */ \
570 if (prec == 0 && number.longlong == 0) \
572 string = workend; \
573 if (base == 8 && alt) \
574 *string-- = '0'; \
576 else \
578 /* Put the number in WORK. */ \
579 string = _itoa (number.longlong, workend + 1, base, \
580 spec == 'X'); \
581 string -= 1; \
582 if (group && grouping) \
583 string = group_number (string, workend, grouping, \
584 thousands_sep); \
586 /* Simply further test for num != 0. */ \
587 number.word = number.longlong != 0; \
589 else \
591 if (fspec == NULL) \
593 if (is_long) \
594 number.word = va_arg (ap, unsigned long int); \
595 else if (!is_short) \
596 number.word = va_arg (ap, unsigned int); \
597 else \
598 number.word = (unsigned short int) va_arg (ap, unsigned int); \
600 else \
601 if (is_long) \
602 number.word = args_value[fspec->data_arg].pa_u_long_int; \
603 else if (is_char) \
604 number.word = (unsigned char) \
605 args_value[fspec->data_arg].pa_char; \
606 else if (!is_short) \
607 number.word = args_value[fspec->data_arg].pa_u_int; \
608 else \
609 number.word = (unsigned short int) \
610 args_value[fspec->data_arg].pa_u_short_int; \
612 LABEL (number): \
613 if (prec < 0) \
614 /* Supply a default precision if none was given. */ \
615 prec = 1; \
616 else \
617 /* We have to take care for the '0' flag. If a precision \
618 is given it must be ignored. */ \
619 pad = ' '; \
621 /* If the precision is 0 and the number is 0 nothing has to \
622 be written for the number, except for the 'o' format in \
623 alternate form. */ \
624 if (prec == 0 && number.word == 0) \
626 string = workend; \
627 if (base == 8 && alt) \
628 *string-- = '0'; \
630 else \
632 /* Put the number in WORK. */ \
633 string = _itoa_word (number.word, workend + 1, base, \
634 spec == 'X'); \
635 string -= 1; \
636 if (group && grouping) \
637 string = group_number (string, workend, grouping, \
638 thousands_sep); \
642 prec -= workend - string; \
644 if (prec > 0) \
645 /* Add zeros to the precision. */ \
646 while (prec-- > 0) \
647 *string-- = '0'; \
648 else if (number.word != 0 && alt && base == 8) \
649 /* Add octal marker. */ \
650 *string-- = '0'; \
652 if (!left) \
654 width -= workend - string; \
656 if (number.word != 0 && alt && base == 16) \
657 /* Account for 0X hex marker. */ \
658 width -= 2; \
660 if (is_negative || showsign || space) \
661 --width; \
663 if (pad == '0') \
665 while (width-- > 0) \
666 *string-- = '0'; \
668 if (number.word != 0 && alt && base == 16) \
670 *string-- = spec; \
671 *string-- = '0'; \
674 if (is_negative) \
675 *string-- = '-'; \
676 else if (showsign) \
677 *string-- = '+'; \
678 else if (space) \
679 *string-- = ' '; \
681 else \
683 if (number.word != 0 && alt && base == 16) \
685 *string-- = spec; \
686 *string-- = '0'; \
689 if (is_negative) \
690 *string-- = '-'; \
691 else if (showsign) \
692 *string-- = '+'; \
693 else if (space) \
694 *string-- = ' '; \
696 while (width-- > 0) \
697 *string-- = ' '; \
700 outstring (string + 1, workend - string); \
702 break; \
704 else \
706 if (number.word != 0 && alt && base == 16) \
708 *string-- = spec; \
709 *string-- = '0'; \
712 if (is_negative) \
713 *string-- = '-'; \
714 else if (showsign) \
715 *string-- = '+'; \
716 else if (space) \
717 *string-- = ' '; \
719 width -= workend - string; \
720 outstring (string + 1, workend - string); \
722 PAD (' '); \
723 break; \
726 LABEL (form_float): \
728 /* Floating-point number. This is handled by printf_fp.c. */ \
729 extern int __printf_fp __P ((FILE *, const struct printf_info *, \
730 const void **const)); \
731 const void *ptr; \
732 int function_done; \
734 if (fspec == NULL) \
736 struct printf_info info = { prec: prec, \
737 width: width, \
738 spec: spec, \
739 is_long_double: is_long_double, \
740 is_short: is_short, \
741 is_long: is_long, \
742 alt: alt, \
743 space: space, \
744 left: left, \
745 showsign: showsign, \
746 group: group, \
747 pad: pad, \
748 extra: 0 }; \
750 if (is_long_double) \
751 the_arg.pa_long_double = va_arg (ap, long double); \
752 else \
753 the_arg.pa_double = va_arg (ap, double); \
754 ptr = (const void *) &the_arg; \
756 function_done = __printf_fp (s, &info, &ptr); \
758 else \
760 ptr = (const void *) &args_value[fspec->data_arg]; \
762 function_done = __printf_fp (s, &fspec->info, &ptr); \
765 if (function_done < 0) \
766 /* Error in print handler. */ \
767 return -1; \
769 done += function_done; \
771 break; \
773 LABEL (form_floathex): \
775 /* FLoating point number printed as hexadecimal number. */ \
776 extern int __printf_fphex __P ((FILE *, const struct printf_info *, \
777 const void **const)); \
778 const void *ptr; \
779 int function_done; \
781 if (fspec == NULL) \
783 struct printf_info info = { prec: prec, \
784 width: width, \
785 spec: spec, \
786 is_long_double: is_long_double, \
787 is_short: is_short, \
788 is_long: is_long, \
789 alt: alt, \
790 space: space, \
791 left: left, \
792 showsign: showsign, \
793 group: group, \
794 pad: pad, \
795 extra: 0 }; \
797 if (is_long_double) \
798 the_arg.pa_long_double = va_arg (ap, long double); \
799 else \
800 the_arg.pa_double = va_arg (ap, double); \
801 ptr = (const void *) &the_arg; \
803 function_done = __printf_fphex (s, &info, &ptr); \
805 else \
807 ptr = (const void *) &args_value[fspec->data_arg]; \
809 function_done = __printf_fphex (s, &fspec->info, &ptr); \
812 if (function_done < 0) \
813 /* Error in print handler. */ \
814 return -1; \
816 done += function_done; \
818 break; \
820 LABEL (form_character): \
821 /* Character. */ \
822 if (is_long) \
823 goto LABEL (form_wcharacter); \
824 --width; /* Account for the character itself. */ \
825 if (!left) \
826 PAD (' '); \
827 if (fspec == NULL) \
828 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
829 else \
830 outchar ((unsigned char) args_value[fspec->data_arg].pa_char); \
831 if (left) \
832 PAD (' '); \
833 break; \
835 LABEL (form_wcharacter): \
837 /* Wide character. */ \
838 char buf[MB_CUR_MAX]; \
839 mbstate_t mbstate; \
840 size_t len; \
842 memset (&mbstate, '\0', sizeof (mbstate_t)); \
843 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wint_t) \
844 : args_value[fspec->data_arg].pa_wchar), \
845 &mbstate); \
846 width -= len; \
847 if (!left) \
848 PAD (' '); \
849 outstring (buf, len); \
850 if (left) \
851 PAD (' '); \
853 break; \
855 LABEL (form_string): \
857 size_t len; \
859 /* The string argument could in fact be `char *' or `wchar_t *'. \
860 But this should not make a difference here. */ \
861 if (fspec == NULL) \
862 string = (char *) va_arg (ap, const char *); \
863 else \
864 string = (char *) args_value[fspec->data_arg].pa_string; \
866 /* Entry point for printing other strings. */ \
867 LABEL (print_string): \
869 if (string == NULL) \
871 /* Write "(null)" if there's space. */ \
872 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
874 string = (char *) null; \
875 len = sizeof (null) - 1; \
877 else \
879 string = (char *) ""; \
880 len = 0; \
883 else if (!is_long && spec != L_('S')) \
885 if (prec != -1) \
886 /* Search for the end of the string, but don't search past \
887 the length specified by the precision. */ \
888 len = strnlen (string, prec); \
889 else \
890 len = strlen (string); \
892 else \
894 const wchar_t *s2 = (const wchar_t *) string; \
895 mbstate_t mbstate; \
897 memset (&mbstate, '\0', sizeof (mbstate_t)); \
898 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
899 if (len == (size_t) -1) \
900 /* Illegal wide-character string. */ \
901 return -1; \
903 assert (__mbsinit (&mbstate)); \
904 s2 = (const wchar_t *) string; \
905 string = alloca (len + 1); \
906 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
907 if (prec < len) \
908 len = prec; \
911 if ((width -= len) < 0) \
913 outstring (string, len); \
914 break; \
917 if (!left) \
918 PAD (' '); \
919 outstring (string, len); \
920 if (left) \
921 PAD (' '); \
923 break; \
925 LABEL (form_pointer): \
926 /* Generic pointer. */ \
928 const void *ptr; \
929 if (fspec == NULL) \
930 ptr = va_arg (ap, void *); \
931 else \
932 ptr = args_value[fspec->data_arg].pa_pointer; \
933 if (ptr != NULL) \
935 /* If the pointer is not NULL, write it as a %#x spec. */ \
936 base = 16; \
937 number.word = (unsigned long int) ptr; \
938 is_negative = 0; \
939 alt = 1; \
940 group = 0; \
941 spec = 'x'; \
942 goto LABEL (number); \
944 else \
946 /* Write "(nil)" for a nil pointer. */ \
947 string = (char *) "(nil)"; \
948 /* Make sure the full string "(nil)" is printed. */ \
949 if (prec < 5) \
950 prec = 5; \
951 is_long = 0; /* This is no wide-char string. */ \
952 goto LABEL (print_string); \
955 /* NOTREACHED */ \
957 LABEL (form_number): \
958 /* Answer the count of characters written. */ \
959 if (fspec == NULL) \
961 if (is_longlong) \
962 *(long long int *) va_arg (ap, void *) = done; \
963 else if (is_long) \
964 *(long int *) va_arg (ap, void *) = done; \
965 else if (!is_short) \
966 *(int *) va_arg (ap, void *) = done; \
967 else \
968 *(short int *) va_arg (ap, void *) = done; \
970 else \
971 if (is_longlong) \
972 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
973 else if (is_long) \
974 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
975 else if (!is_short) \
976 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
977 else \
978 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
979 break; \
981 LABEL (form_strerror): \
982 /* Print description of error ERRNO. */ \
983 string = \
984 (char *) __strerror_r (save_errno, work_buffer, sizeof work_buffer); \
985 is_long = 0; /* This is no wide-char string. */ \
986 goto LABEL (print_string)
989 /* Sanity check of arguments. */
990 ARGCHECK (s, format);
992 if (UNBUFFERED_P (s))
993 /* Use a helper function which will allocate a local temporary buffer
994 for the stream and then call us again. */
995 return buffered_vfprintf (s, format, ap);
997 /* Initialize local variables. */
998 done = 0;
999 grouping = (const char *) -1;
1000 #ifdef __va_copy
1001 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1002 since on some systems `va_list' is not an integral type. */
1003 __va_copy (ap_save, ap);
1004 #else
1005 ap_save = ap;
1006 #endif
1007 nspecs_done = 0;
1009 /* Put state for processing format string in initial state. */
1010 memset (&mbstate, '\0', sizeof (mbstate_t));
1012 /* Find the first format specifier. */
1013 f = lead_str_end = find_spec (format, &mbstate);
1015 /* Lock stream. */
1016 #ifdef USE_IN_LIBIO
1017 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
1018 _IO_flockfile (s);
1019 #else
1020 __libc_cleanup_region_start ((void (*) (void *)) &__funlockfile, s);
1021 __flockfile (s);
1022 #endif
1024 /* Write the literal text before the first format. */
1025 outstring ((const UCHAR_T *) format,
1026 lead_str_end - (const UCHAR_T *) format);
1028 /* If we only have to print a simple string, return now. */
1029 if (*f == L_('\0'))
1030 goto all_done;
1032 /* Process whole format string. */
1035 #define REF(Name) &&do_##Name
1036 #define LABEL(Name) do_##Name
1037 STEP0_3_TABLE;
1038 STEP4_TABLE;
1040 union printf_arg *args_value; /* This is not used here but ... */
1041 int is_negative; /* Flag for negative number. */
1042 union
1044 unsigned long long int longlong;
1045 unsigned long int word;
1046 } number;
1047 int base;
1048 union printf_arg the_arg;
1049 char *string; /* Pointer to argument string. */
1050 int alt = 0; /* Alternate format. */
1051 int space = 0; /* Use space prefix if no sign is needed. */
1052 int left = 0; /* Left-justify output. */
1053 int showsign = 0; /* Always begin with plus or minus sign. */
1054 int group = 0; /* Print numbers according grouping rules. */
1055 int is_long_double = 0; /* Argument is long double/ long long int. */
1056 int is_short = 0; /* Argument is long int. */
1057 int is_long = 0; /* Argument is short int. */
1058 int is_char = 0; /* Argument is promoted (unsigned) char. */
1059 int width = 0; /* Width of output; 0 means none specified. */
1060 int prec = -1; /* Precision of output; -1 means none specified. */
1061 char pad = ' '; /* Padding character. */
1062 CHAR_T spec;
1064 /* Get current character in format string. */
1065 JUMP (*++f, step0_jumps);
1067 /* ' ' flag. */
1068 LABEL (flag_space):
1069 space = 1;
1070 JUMP (*++f, step0_jumps);
1072 /* '+' flag. */
1073 LABEL (flag_plus):
1074 showsign = 1;
1075 JUMP (*++f, step0_jumps);
1077 /* The '-' flag. */
1078 LABEL (flag_minus):
1079 left = 1;
1080 pad = L_(' ');
1081 JUMP (*++f, step0_jumps);
1083 /* The '#' flag. */
1084 LABEL (flag_hash):
1085 alt = 1;
1086 JUMP (*++f, step0_jumps);
1088 /* The '0' flag. */
1089 LABEL (flag_zero):
1090 if (!left)
1091 pad = L_('0');
1092 JUMP (*++f, step0_jumps);
1094 /* The '\'' flag. */
1095 LABEL (flag_quote):
1096 group = 1;
1098 /* XXX Completely wrong. Use wctob. */
1099 if (grouping == (const char *) -1)
1101 /* Figure out the thousands separator character. */
1102 if (mbtowc (&thousands_sep,
1103 _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
1104 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
1105 thousands_sep = (wchar_t)
1106 *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1107 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1108 if (*grouping == '\0' || *grouping == CHAR_MAX
1109 || thousands_sep == L'\0')
1110 grouping = NULL;
1112 JUMP (*++f, step0_jumps);
1114 /* Get width from argument. */
1115 LABEL (width_asterics):
1117 const UCHAR_T *tmp; /* Temporary value. */
1119 tmp = ++f;
1120 if (ISDIGIT (*tmp) && read_int (&tmp) && *tmp == L_('$'))
1121 /* The width comes from a positional parameter. */
1122 goto do_positional;
1124 width = va_arg (ap, int);
1126 /* Negative width means left justified. */
1127 if (width < 0)
1129 width = -width;
1130 pad = L_(' ');
1131 left = 1;
1134 JUMP (*f, step1_jumps);
1136 /* Given width in format string. */
1137 LABEL (width):
1138 width = read_int (&f);
1139 if (*f == L_('$'))
1140 /* Oh, oh. The argument comes from a positional parameter. */
1141 goto do_positional;
1142 JUMP (*f, step1_jumps);
1144 LABEL (precision):
1145 ++f;
1146 if (*f == L_('*'))
1148 const UCHAR_T *tmp; /* Temporary value. */
1150 tmp = ++f;
1151 if (ISDIGIT (*tmp) && read_int (&tmp) > 0 && *tmp == L_('$'))
1152 /* The precision comes from a positional parameter. */
1153 goto do_positional;
1155 prec = va_arg (ap, int);
1157 /* If the precision is negative the precision is omitted. */
1158 if (prec < 0)
1159 prec = -1;
1161 else if (ISDIGIT (*f))
1162 prec = read_int (&f);
1163 else
1164 prec = 0;
1165 JUMP (*f, step2_jumps);
1167 /* Process 'h' modifier. There might another 'h' following. */
1168 LABEL (mod_half):
1169 is_short = 1;
1170 JUMP (*++f, step3a_jumps);
1172 /* Process 'hh' modifier. */
1173 LABEL (mod_halfhalf):
1174 is_short = 0;
1175 is_char = 1;
1176 JUMP (*++f, step4_jumps);
1178 /* Process 'l' modifier. There might another 'l' following. */
1179 LABEL (mod_long):
1180 is_long = 1;
1181 JUMP (*++f, step3b_jumps);
1183 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1184 allowed to follow. */
1185 LABEL (mod_longlong):
1186 is_long_double = 1;
1187 JUMP (*++f, step4_jumps);
1189 LABEL (mod_size_t):
1190 is_longlong = sizeof (size_t) > sizeof (unsigned long int);
1191 is_long = sizeof (size_t) > sizeof (unsigned int);
1192 JUMP (*++f, step4_jumps);
1194 LABEL (mod_ptrdiff_t):
1195 is_longlong = sizeof (ptrdiff_t) > sizeof (unsigned long int);
1196 is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
1197 JUMP (*++f, step4_jumps);
1199 LABEL (mod_intmax_t):
1200 is_longlong = sizeof (intmax_t) > sizeof (unsigned long int);
1201 is_long = sizeof (intmax_t) > sizeof (unsigned int);
1202 JUMP (*++f, step4_jumps);
1204 /* Process current format. */
1205 while (1)
1207 process_arg (((struct printf_spec *) NULL));
1209 LABEL (form_unknown):
1210 if (spec == L_('\0'))
1212 /* The format string ended before the specifier is complete. */
1213 done = -1;
1214 goto all_done;
1217 /* If we are in the fast loop force entering the complicated
1218 one. */
1219 goto do_positional;
1222 /* The format is correctly handled. */
1223 ++nspecs_done;
1225 /* Look for next format specifier. */
1226 f = find_spec ((end_of_spec = ++f), &mbstate);
1228 /* Write the following constant string. */
1229 outstring (end_of_spec, f - end_of_spec);
1231 while (*f != L_('\0'));
1233 /* Unlock stream and return. */
1234 goto all_done;
1236 /* Here starts the more complex loop to handle positional parameters. */
1237 do_positional:
1239 /* Array with information about the needed arguments. This has to
1240 be dynamically extensible. */
1241 size_t nspecs = 0;
1242 size_t nspecs_max = 32; /* A more or less arbitrary start value. */
1243 struct printf_spec *specs
1244 = alloca (nspecs_max * sizeof (struct printf_spec));
1246 /* The number of arguments the format string requests. This will
1247 determine the size of the array needed to store the argument
1248 attributes. */
1249 size_t nargs = 0;
1250 int *args_type;
1251 union printf_arg *args_value;
1253 /* Positional parameters refer to arguments directly. This could
1254 also determine the maximum number of arguments. Track the
1255 maximum number. */
1256 size_t max_ref_arg = 0;
1258 /* Just a counter. */
1259 size_t cnt;
1262 if (grouping == (const char *) -1)
1264 /* XXX Use wctob. But this is incompatible for now. */
1265 /* Figure out the thousands separator character. */
1266 if (mbtowc (&thousands_sep,
1267 _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
1268 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
1269 thousands_sep = (wchar_t) *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1270 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1271 if (*grouping == '\0' || *grouping == CHAR_MAX
1272 || thousands_sep == L'\0')
1273 grouping = NULL;
1276 for (f = lead_str_end; *f != '\0'; f = specs[nspecs++].next_fmt)
1278 if (nspecs >= nspecs_max)
1280 /* Extend the array of format specifiers. */
1281 struct printf_spec *old = specs;
1283 nspecs_max *= 2;
1284 specs = alloca (nspecs_max * sizeof (struct printf_spec));
1286 if (specs == &old[nspecs])
1287 /* Stack grows up, OLD was the last thing allocated;
1288 extend it. */
1289 nspecs_max += nspecs_max / 2;
1290 else
1292 /* Copy the old array's elements to the new space. */
1293 memcpy (specs, old, nspecs * sizeof (struct printf_spec));
1294 if (old == &specs[nspecs])
1295 /* Stack grows down, OLD was just below the new
1296 SPECS. We can use that space when the new space
1297 runs out. */
1298 nspecs_max += nspecs_max / 2;
1302 /* Parse the format specifier. */
1303 nargs += parse_one_spec (f, nargs, &specs[nspecs], &max_ref_arg,
1304 &mbstate);
1307 /* Determine the number of arguments the format string consumes. */
1308 nargs = MAX (nargs, max_ref_arg);
1310 /* Allocate memory for the argument descriptions. */
1311 args_type = alloca (nargs * sizeof (int));
1312 memset (args_type, 0, nargs * sizeof (int));
1313 args_value = alloca (nargs * sizeof (union printf_arg));
1315 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1316 still zero after this loop, format is invalid. For now we
1317 simply use 0 as the value. */
1319 /* Fill in the types of all the arguments. */
1320 for (cnt = 0; cnt < nspecs; ++cnt)
1322 /* If the width is determined by an argument this is an int. */
1323 if (specs[cnt].width_arg != -1)
1324 args_type[specs[cnt].width_arg] = PA_INT;
1326 /* If the precision is determined by an argument this is an int. */
1327 if (specs[cnt].prec_arg != -1)
1328 args_type[specs[cnt].prec_arg] = PA_INT;
1330 switch (specs[cnt].ndata_args)
1332 case 0: /* No arguments. */
1333 break;
1334 case 1: /* One argument; we already have the type. */
1335 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1336 break;
1337 default:
1338 /* We have more than one argument for this format spec.
1339 We must call the arginfo function again to determine
1340 all the types. */
1341 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1342 (&specs[cnt].info,
1343 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg]);
1344 break;
1348 /* Now we know all the types and the order. Fill in the argument
1349 values. */
1350 for (cnt = 0; cnt < nargs; ++cnt)
1351 switch (args_type[cnt])
1353 #define T(tag, mem, type) \
1354 case tag: \
1355 args_value[cnt].mem = va_arg (ap_save, type); \
1356 break
1358 T (PA_CHAR, pa_char, int); /* Promoted. */
1359 T (PA_WCHAR, pa_wchar, wint_t);
1360 T (PA_INT|PA_FLAG_SHORT, pa_short_int, int); /* Promoted. */
1361 T (PA_INT, pa_int, int);
1362 T (PA_INT|PA_FLAG_LONG, pa_long_int, long int);
1363 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1364 T (PA_FLOAT, pa_float, double); /* Promoted. */
1365 T (PA_DOUBLE, pa_double, double);
1366 T (PA_DOUBLE|PA_FLAG_LONG_DOUBLE, pa_long_double, long double);
1367 T (PA_STRING, pa_string, const char *);
1368 T (PA_WSTRING, pa_wstring, const wchar_t *);
1369 T (PA_POINTER, pa_pointer, void *);
1370 #undef T
1371 default:
1372 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1373 args_value[cnt].pa_pointer = va_arg (ap_save, void *);
1374 else
1375 args_value[cnt].pa_long_double = 0.0;
1376 break;
1379 /* Now walk through all format specifiers and process them. */
1380 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1382 #undef REF
1383 #define REF(Name) &&do2_##Name
1384 #undef LABEL
1385 #define LABEL(Name) do2_##Name
1386 STEP4_TABLE;
1388 int is_negative;
1389 union
1391 unsigned long long int longlong;
1392 unsigned long int word;
1393 } number;
1394 int base;
1395 union printf_arg the_arg;
1396 char *string; /* Pointer to argument string. */
1398 /* Fill variables from values in struct. */
1399 int alt = specs[nspecs_done].info.alt;
1400 int space = specs[nspecs_done].info.space;
1401 int left = specs[nspecs_done].info.left;
1402 int showsign = specs[nspecs_done].info.showsign;
1403 int group = specs[nspecs_done].info.group;
1404 int is_long_double = specs[nspecs_done].info.is_long_double;
1405 int is_short = specs[nspecs_done].info.is_short;
1406 int is_char = specs[nspecs_done].info.is_char;
1407 int is_long = specs[nspecs_done].info.is_long;
1408 int width = specs[nspecs_done].info.width;
1409 int prec = specs[nspecs_done].info.prec;
1410 char pad = specs[nspecs_done].info.pad;
1411 CHAR_T spec = specs[nspecs_done].info.spec;
1413 /* Fill in last information. */
1414 if (specs[nspecs_done].width_arg != -1)
1416 /* Extract the field width from an argument. */
1417 specs[nspecs_done].info.width =
1418 args_value[specs[nspecs_done].width_arg].pa_int;
1420 if (specs[nspecs_done].info.width < 0)
1421 /* If the width value is negative left justification is
1422 selected and the value is taken as being positive. */
1424 specs[nspecs_done].info.width *= -1;
1425 left = specs[nspecs_done].info.left = 1;
1427 width = specs[nspecs_done].info.width;
1430 if (specs[nspecs_done].prec_arg != -1)
1432 /* Extract the precision from an argument. */
1433 specs[nspecs_done].info.prec =
1434 args_value[specs[nspecs_done].prec_arg].pa_int;
1436 if (specs[nspecs_done].info.prec < 0)
1437 /* If the precision is negative the precision is
1438 omitted. */
1439 specs[nspecs_done].info.prec = -1;
1441 prec = specs[nspecs_done].info.prec;
1444 /* Process format specifiers. */
1445 while (1)
1447 JUMP (spec, step4_jumps);
1449 process_arg ((&specs[nspecs_done]));
1451 LABEL (form_unknown):
1453 extern printf_function **__printf_function_table;
1454 int function_done;
1455 printf_function *function;
1456 unsigned int i;
1457 const void **ptr;
1459 function =
1460 (__printf_function_table == NULL ? NULL :
1461 __printf_function_table[specs[nspecs_done].info.spec]);
1463 if (function == NULL)
1464 function = &printf_unknown;
1466 ptr = alloca (specs[nspecs_done].ndata_args
1467 * sizeof (const void *));
1469 /* Fill in an array of pointers to the argument values. */
1470 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
1471 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1473 /* Call the function. */
1474 function_done = (*function) (s, &specs[nspecs_done].info, ptr);
1476 /* If an error occurred we don't have information about #
1477 of chars. */
1478 if (function_done < 0)
1480 done = -1;
1481 goto all_done;
1484 done += function_done;
1486 break;
1489 /* Write the following constant string. */
1490 outstring (specs[nspecs_done].end_of_fmt,
1491 specs[nspecs_done].next_fmt
1492 - specs[nspecs_done].end_of_fmt);
1496 all_done:
1497 /* Unlock the stream. */
1498 #ifdef USE_IN_LIBIO
1499 _IO_funlockfile (s);
1500 #else
1501 __funlockfile (s);
1502 #endif
1503 __libc_cleanup_region_end (0);
1505 return done;
1508 #ifdef USE_IN_LIBIO
1509 # undef vfprintf
1510 # ifdef strong_alias
1511 /* This is for glibc. */
1512 strong_alias (_IO_vfprintf, vfprintf);
1513 # else
1514 # if defined __ELF__ || defined __GNU_LIBRARY__
1515 # include <gnu-stabs.h>
1516 # ifdef weak_alias
1517 weak_alias (_IO_vfprintf, vfprintf);
1518 # endif
1519 # endif
1520 # endif
1521 #endif
1523 /* Handle an unknown format specifier. This prints out a canonicalized
1524 representation of the format spec itself. */
1525 static int
1526 printf_unknown (FILE *s, const struct printf_info *info,
1527 const void *const *args)
1530 int done = 0;
1531 char work_buffer[BUFSIZ];
1532 register char *w;
1534 outchar ('%');
1536 if (info->alt)
1537 outchar ('#');
1538 if (info->group)
1539 outchar ('\'');
1540 if (info->showsign)
1541 outchar ('+');
1542 else if (info->space)
1543 outchar (' ');
1544 if (info->left)
1545 outchar ('-');
1546 if (info->pad == '0')
1547 outchar ('0');
1549 if (info->width != 0)
1551 w = _itoa_word (info->width, workend + 1, 10, 0);
1552 while (w <= workend)
1553 outchar (*w++);
1556 if (info->prec != -1)
1558 outchar ('.');
1559 w = _itoa_word (info->prec, workend + 1, 10, 0);
1560 while (w <= workend)
1561 outchar (*w++);
1564 if (info->spec != '\0')
1565 outchar (info->spec);
1567 return done;
1570 /* Group the digits according to the grouping rules of the current locale.
1571 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1572 static char *
1573 internal_function
1574 group_number (CHAR_T *w, CHAR_T *rear_ptr, const CHAR_T *grouping,
1575 wchar_t thousands_sep)
1577 int len;
1578 char *src, *s;
1580 /* We treat all negative values like CHAR_MAX. */
1582 if (*grouping == CHAR_MAX || *grouping <= 0)
1583 /* No grouping should be done. */
1584 return w;
1586 len = *grouping;
1588 /* Copy existing string so that nothing gets overwritten. */
1589 src = (char *) alloca (rear_ptr - w);
1590 s = (char *) __mempcpy (src, w + 1, rear_ptr - w) - 1;
1591 w = rear_ptr;
1593 /* Process all characters in the string. */
1594 while (s >= src)
1596 *w-- = *s--;
1598 if (--len == 0 && s >= src)
1600 /* A new group begins. */
1601 *w-- = thousands_sep;
1603 len = *grouping++;
1604 if (*grouping == '\0')
1605 /* The previous grouping repeats ad infinitum. */
1606 --grouping;
1607 else if (*grouping == CHAR_MAX
1608 #if CHAR_MIN < 0
1609 || *grouping < 0
1610 #endif
1613 /* No further grouping to be done.
1614 Copy the rest of the number. */
1616 *w-- = *s--;
1617 while (s >= src);
1618 break;
1622 return w;
1625 #ifdef USE_IN_LIBIO
1626 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
1627 struct helper_file
1629 struct _IO_FILE_plus _f;
1630 _IO_FILE *_put_stream;
1631 #ifdef _IO_MTSAFE_IO
1632 _IO_lock_t lock;
1633 #endif
1636 static int
1637 _IO_helper_overflow (_IO_FILE *s, int c)
1639 _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
1640 int used = s->_IO_write_ptr - s->_IO_write_base;
1641 if (used)
1643 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
1644 s->_IO_write_ptr -= written;
1646 return PUTC (c, s);
1649 static const struct _IO_jump_t _IO_helper_jumps =
1651 JUMP_INIT_DUMMY,
1652 JUMP_INIT (finish, _IO_default_finish),
1653 JUMP_INIT (overflow, _IO_helper_overflow),
1654 JUMP_INIT (underflow, _IO_default_underflow),
1655 JUMP_INIT (uflow, _IO_default_uflow),
1656 JUMP_INIT (pbackfail, _IO_default_pbackfail),
1657 JUMP_INIT (xsputn, _IO_default_xsputn),
1658 JUMP_INIT (xsgetn, _IO_default_xsgetn),
1659 JUMP_INIT (seekoff, _IO_default_seekoff),
1660 JUMP_INIT (seekpos, _IO_default_seekpos),
1661 JUMP_INIT (setbuf, _IO_default_setbuf),
1662 JUMP_INIT (sync, _IO_default_sync),
1663 JUMP_INIT (doallocate, _IO_default_doallocate),
1664 JUMP_INIT (read, _IO_default_read),
1665 JUMP_INIT (write, _IO_default_write),
1666 JUMP_INIT (seek, _IO_default_seek),
1667 JUMP_INIT (close, _IO_default_close),
1668 JUMP_INIT (stat, _IO_default_stat)
1671 static int
1672 internal_function
1673 buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format,
1674 _IO_va_list args)
1676 char buf[_IO_BUFSIZ];
1677 struct helper_file helper;
1678 register _IO_FILE *hp = (_IO_FILE *) &helper;
1679 int result, to_flush;
1681 /* Initialize helper. */
1682 helper._put_stream = s;
1683 hp->_IO_write_base = buf;
1684 hp->_IO_write_ptr = buf;
1685 hp->_IO_write_end = buf + sizeof buf;
1686 hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS;
1687 #if _IO_JUMPS_OFFSET
1688 hp->_vtable_offset = 0;
1689 #endif
1690 #ifdef _IO_MTSAFE_IO
1691 hp->_lock = &helper.lock;
1692 __libc_lock_init (*hp->_lock);
1693 #endif
1694 _IO_JUMPS (hp) = (struct _IO_jump_t *) &_IO_helper_jumps;
1696 /* Now print to helper instead. */
1697 result = _IO_vfprintf (hp, format, args);
1699 /* Now flush anything from the helper to the S. */
1700 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
1702 if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
1703 return -1;
1706 return result;
1709 #else /* !USE_IN_LIBIO */
1711 static int
1712 internal_function
1713 buffered_vfprintf (register FILE *s, const CHAR_T *format, va_list args)
1715 char buf[BUFSIZ];
1716 int result;
1718 s->__bufp = s->__buffer = buf;
1719 s->__bufsize = sizeof buf;
1720 s->__put_limit = s->__buffer + s->__bufsize;
1721 s->__get_limit = s->__buffer;
1723 /* Now use buffer to print. */
1724 result = vfprintf (s, format, args);
1726 if (fflush (s) == EOF)
1727 result = -1;
1728 s->__buffer = s->__bufp = s->__get_limit = s->__put_limit = NULL;
1729 s->__bufsize = 0;
1731 return result;
1734 /* Pads string with given number of a specified character.
1735 This code is taken from iopadn.c of the GNU I/O library. */
1736 #define PADSIZE 16
1737 static const CHAR_T blanks[PADSIZE] =
1738 { L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '),
1739 L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' ') };
1740 static const CHAR_T zeroes[PADSIZE] =
1741 { L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'),
1742 L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0') };
1744 ssize_t
1745 #ifndef COMPILE_WPRINTF
1746 __printf_pad (FILE *s, char pad, size_t count)
1747 #else
1748 __wprintf_pad (FILE *s, wchar_t pad, size_t count)
1749 #endif
1751 const CHAR_T *padptr;
1752 register size_t i;
1754 padptr = pad == L_(' ') ? blanks : zeroes;
1756 for (i = count; i >= PADSIZE; i -= PADSIZE)
1757 if (PUT (s, padptr, PADSIZE) != PADSIZE)
1758 return -1;
1759 if (i > 0)
1760 if (PUT (s, padptr, i) != i)
1761 return -1;
1763 return count;
1765 #undef PADSIZE
1766 #endif /* USE_IN_LIBIO */