Update.
[glibc.git] / stdio-common / vfprintf.c
blobbc005f9f32463a10270ce13fe1434bace39eb1ea
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) \
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 #define workend (&work_buffer[sizeof (work_buffer) - 1])
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 /* Get current character in format string. */
1088 JUMP (*++f, step0_jumps);
1090 /* ' ' flag. */
1091 LABEL (flag_space):
1092 space = 1;
1093 JUMP (*++f, step0_jumps);
1095 /* '+' flag. */
1096 LABEL (flag_plus):
1097 showsign = 1;
1098 JUMP (*++f, step0_jumps);
1100 /* The '-' flag. */
1101 LABEL (flag_minus):
1102 left = 1;
1103 pad = L_(' ');
1104 JUMP (*++f, step0_jumps);
1106 /* The '#' flag. */
1107 LABEL (flag_hash):
1108 alt = 1;
1109 JUMP (*++f, step0_jumps);
1111 /* The '0' flag. */
1112 LABEL (flag_zero):
1113 if (!left)
1114 pad = L_('0');
1115 JUMP (*++f, step0_jumps);
1117 /* The '\'' flag. */
1118 LABEL (flag_quote):
1119 group = 1;
1121 /* XXX Completely wrong. Use wctob. */
1122 if (grouping == (const char *) -1)
1124 /* Figure out the thousands separator character. */
1125 if (mbtowc (&thousands_sep,
1126 _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
1127 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
1128 thousands_sep = (wchar_t)
1129 *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1130 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1131 if (*grouping == '\0' || *grouping == CHAR_MAX
1132 || thousands_sep == L'\0')
1133 grouping = NULL;
1135 JUMP (*++f, step0_jumps);
1137 /* Get width from argument. */
1138 LABEL (width_asterics):
1140 const UCHAR_T *tmp; /* Temporary value. */
1142 tmp = ++f;
1143 if (ISDIGIT (*tmp) && read_int (&tmp) && *tmp == L_('$'))
1144 /* The width comes from a positional parameter. */
1145 goto do_positional;
1147 width = va_arg (ap, int);
1149 /* Negative width means left justified. */
1150 if (width < 0)
1152 width = -width;
1153 pad = L_(' ');
1154 left = 1;
1157 JUMP (*f, step1_jumps);
1159 /* Given width in format string. */
1160 LABEL (width):
1161 width = read_int (&f);
1162 if (*f == L_('$'))
1163 /* Oh, oh. The argument comes from a positional parameter. */
1164 goto do_positional;
1165 JUMP (*f, step1_jumps);
1167 LABEL (precision):
1168 ++f;
1169 if (*f == L_('*'))
1171 const UCHAR_T *tmp; /* Temporary value. */
1173 tmp = ++f;
1174 if (ISDIGIT (*tmp) && read_int (&tmp) > 0 && *tmp == L_('$'))
1175 /* The precision comes from a positional parameter. */
1176 goto do_positional;
1178 prec = va_arg (ap, int);
1180 /* If the precision is negative the precision is omitted. */
1181 if (prec < 0)
1182 prec = -1;
1184 else if (ISDIGIT (*f))
1185 prec = read_int (&f);
1186 else
1187 prec = 0;
1188 JUMP (*f, step2_jumps);
1190 /* Process 'h' modifier. There might another 'h' following. */
1191 LABEL (mod_half):
1192 is_short = 1;
1193 JUMP (*++f, step3a_jumps);
1195 /* Process 'hh' modifier. */
1196 LABEL (mod_halfhalf):
1197 is_short = 0;
1198 is_char = 1;
1199 JUMP (*++f, step4_jumps);
1201 /* Process 'l' modifier. There might another 'l' following. */
1202 LABEL (mod_long):
1203 is_long = 1;
1204 JUMP (*++f, step3b_jumps);
1206 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1207 allowed to follow. */
1208 LABEL (mod_longlong):
1209 is_long_double = 1;
1210 JUMP (*++f, step4_jumps);
1212 LABEL (mod_size_t):
1213 is_longlong = sizeof (size_t) > sizeof (unsigned long int);
1214 is_long = sizeof (size_t) > sizeof (unsigned int);
1215 JUMP (*++f, step4_jumps);
1217 LABEL (mod_ptrdiff_t):
1218 is_longlong = sizeof (ptrdiff_t) > sizeof (unsigned long int);
1219 is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
1220 JUMP (*++f, step4_jumps);
1222 LABEL (mod_intmax_t):
1223 is_longlong = sizeof (intmax_t) > sizeof (unsigned long int);
1224 is_long = sizeof (intmax_t) > sizeof (unsigned int);
1225 JUMP (*++f, step4_jumps);
1227 /* Process current format. */
1228 while (1)
1230 process_arg (((struct printf_spec *) NULL));
1232 LABEL (form_unknown):
1233 if (spec == L_('\0'))
1235 /* The format string ended before the specifier is complete. */
1236 done = -1;
1237 goto all_done;
1240 /* If we are in the fast loop force entering the complicated
1241 one. */
1242 goto do_positional;
1245 /* The format is correctly handled. */
1246 ++nspecs_done;
1248 /* Look for next format specifier. */
1249 f = find_spec ((end_of_spec = ++f), &mbstate);
1251 /* Write the following constant string. */
1252 outstring (end_of_spec, f - end_of_spec);
1254 while (*f != L_('\0'));
1256 /* Unlock stream and return. */
1257 goto all_done;
1259 /* Here starts the more complex loop to handle positional parameters. */
1260 do_positional:
1262 /* Array with information about the needed arguments. This has to
1263 be dynamically extensible. */
1264 size_t nspecs = 0;
1265 size_t nspecs_max = 32; /* A more or less arbitrary start value. */
1266 struct printf_spec *specs
1267 = alloca (nspecs_max * sizeof (struct printf_spec));
1269 /* The number of arguments the format string requests. This will
1270 determine the size of the array needed to store the argument
1271 attributes. */
1272 size_t nargs = 0;
1273 int *args_type;
1274 union printf_arg *args_value;
1276 /* Positional parameters refer to arguments directly. This could
1277 also determine the maximum number of arguments. Track the
1278 maximum number. */
1279 size_t max_ref_arg = 0;
1281 /* Just a counter. */
1282 size_t cnt;
1285 if (grouping == (const char *) -1)
1287 /* XXX Use wctob. But this is incompatible for now. */
1288 /* Figure out the thousands separator character. */
1289 if (mbtowc (&thousands_sep,
1290 _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
1291 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
1292 thousands_sep = (wchar_t) *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1293 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1294 if (*grouping == '\0' || *grouping == CHAR_MAX
1295 || thousands_sep == L'\0')
1296 grouping = NULL;
1299 for (f = lead_str_end; *f != '\0'; f = specs[nspecs++].next_fmt)
1301 if (nspecs >= nspecs_max)
1303 /* Extend the array of format specifiers. */
1304 struct printf_spec *old = specs;
1306 nspecs_max *= 2;
1307 specs = alloca (nspecs_max * sizeof (struct printf_spec));
1309 if (specs == &old[nspecs])
1310 /* Stack grows up, OLD was the last thing allocated;
1311 extend it. */
1312 nspecs_max += nspecs_max / 2;
1313 else
1315 /* Copy the old array's elements to the new space. */
1316 memcpy (specs, old, nspecs * sizeof (struct printf_spec));
1317 if (old == &specs[nspecs])
1318 /* Stack grows down, OLD was just below the new
1319 SPECS. We can use that space when the new space
1320 runs out. */
1321 nspecs_max += nspecs_max / 2;
1325 /* Parse the format specifier. */
1326 nargs += parse_one_spec (f, nargs, &specs[nspecs], &max_ref_arg,
1327 &mbstate);
1330 /* Determine the number of arguments the format string consumes. */
1331 nargs = MAX (nargs, max_ref_arg);
1333 /* Allocate memory for the argument descriptions. */
1334 args_type = alloca (nargs * sizeof (int));
1335 memset (args_type, 0, nargs * sizeof (int));
1336 args_value = alloca (nargs * sizeof (union printf_arg));
1338 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1339 still zero after this loop, format is invalid. For now we
1340 simply use 0 as the value. */
1342 /* Fill in the types of all the arguments. */
1343 for (cnt = 0; cnt < nspecs; ++cnt)
1345 /* If the width is determined by an argument this is an int. */
1346 if (specs[cnt].width_arg != -1)
1347 args_type[specs[cnt].width_arg] = PA_INT;
1349 /* If the precision is determined by an argument this is an int. */
1350 if (specs[cnt].prec_arg != -1)
1351 args_type[specs[cnt].prec_arg] = PA_INT;
1353 switch (specs[cnt].ndata_args)
1355 case 0: /* No arguments. */
1356 break;
1357 case 1: /* One argument; we already have the type. */
1358 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1359 break;
1360 default:
1361 /* We have more than one argument for this format spec.
1362 We must call the arginfo function again to determine
1363 all the types. */
1364 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1365 (&specs[cnt].info,
1366 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg]);
1367 break;
1371 /* Now we know all the types and the order. Fill in the argument
1372 values. */
1373 for (cnt = 0; cnt < nargs; ++cnt)
1374 switch (args_type[cnt])
1376 #define T(tag, mem, type) \
1377 case tag: \
1378 args_value[cnt].mem = va_arg (ap_save, type); \
1379 break
1381 T (PA_CHAR, pa_char, int); /* Promoted. */
1382 T (PA_WCHAR, pa_wchar, wint_t);
1383 T (PA_INT|PA_FLAG_SHORT, pa_short_int, int); /* Promoted. */
1384 T (PA_INT, pa_int, int);
1385 T (PA_INT|PA_FLAG_LONG, pa_long_int, long int);
1386 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1387 T (PA_FLOAT, pa_float, double); /* Promoted. */
1388 T (PA_DOUBLE, pa_double, double);
1389 T (PA_DOUBLE|PA_FLAG_LONG_DOUBLE, pa_long_double, long double);
1390 T (PA_STRING, pa_string, const char *);
1391 T (PA_WSTRING, pa_wstring, const wchar_t *);
1392 T (PA_POINTER, pa_pointer, void *);
1393 #undef T
1394 default:
1395 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1396 args_value[cnt].pa_pointer = va_arg (ap_save, void *);
1397 else
1398 args_value[cnt].pa_long_double = 0.0;
1399 break;
1402 /* Now walk through all format specifiers and process them. */
1403 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1405 #undef REF
1406 #define REF(Name) &&do2_##Name
1407 #undef LABEL
1408 #define LABEL(Name) do2_##Name
1409 STEP4_TABLE;
1411 int is_negative;
1412 union
1414 unsigned long long int longlong;
1415 unsigned long int word;
1416 } number;
1417 int base;
1418 union printf_arg the_arg;
1419 char *string; /* Pointer to argument string. */
1421 /* Fill variables from values in struct. */
1422 int alt = specs[nspecs_done].info.alt;
1423 int space = specs[nspecs_done].info.space;
1424 int left = specs[nspecs_done].info.left;
1425 int showsign = specs[nspecs_done].info.showsign;
1426 int group = specs[nspecs_done].info.group;
1427 int is_long_double = specs[nspecs_done].info.is_long_double;
1428 int is_short = specs[nspecs_done].info.is_short;
1429 int is_char = specs[nspecs_done].info.is_char;
1430 int is_long = specs[nspecs_done].info.is_long;
1431 int width = specs[nspecs_done].info.width;
1432 int prec = specs[nspecs_done].info.prec;
1433 char pad = specs[nspecs_done].info.pad;
1434 CHAR_T spec = specs[nspecs_done].info.spec;
1436 /* Fill in last information. */
1437 if (specs[nspecs_done].width_arg != -1)
1439 /* Extract the field width from an argument. */
1440 specs[nspecs_done].info.width =
1441 args_value[specs[nspecs_done].width_arg].pa_int;
1443 if (specs[nspecs_done].info.width < 0)
1444 /* If the width value is negative left justification is
1445 selected and the value is taken as being positive. */
1447 specs[nspecs_done].info.width *= -1;
1448 left = specs[nspecs_done].info.left = 1;
1450 width = specs[nspecs_done].info.width;
1453 if (specs[nspecs_done].prec_arg != -1)
1455 /* Extract the precision from an argument. */
1456 specs[nspecs_done].info.prec =
1457 args_value[specs[nspecs_done].prec_arg].pa_int;
1459 if (specs[nspecs_done].info.prec < 0)
1460 /* If the precision is negative the precision is
1461 omitted. */
1462 specs[nspecs_done].info.prec = -1;
1464 prec = specs[nspecs_done].info.prec;
1467 /* Process format specifiers. */
1468 while (1)
1470 JUMP (spec, step4_jumps);
1472 process_arg ((&specs[nspecs_done]));
1474 LABEL (form_unknown):
1476 extern printf_function **__printf_function_table;
1477 int function_done;
1478 printf_function *function;
1479 unsigned int i;
1480 const void **ptr;
1482 function =
1483 (__printf_function_table == NULL ? NULL :
1484 __printf_function_table[specs[nspecs_done].info.spec]);
1486 if (function == NULL)
1487 function = &printf_unknown;
1489 ptr = alloca (specs[nspecs_done].ndata_args
1490 * sizeof (const void *));
1492 /* Fill in an array of pointers to the argument values. */
1493 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
1494 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1496 /* Call the function. */
1497 function_done = (*function) (s, &specs[nspecs_done].info, ptr);
1499 /* If an error occurred we don't have information about #
1500 of chars. */
1501 if (function_done < 0)
1503 done = -1;
1504 goto all_done;
1507 done += function_done;
1509 break;
1512 /* Write the following constant string. */
1513 outstring (specs[nspecs_done].end_of_fmt,
1514 specs[nspecs_done].next_fmt
1515 - specs[nspecs_done].end_of_fmt);
1519 all_done:
1520 /* Unlock the stream. */
1521 #ifdef USE_IN_LIBIO
1522 _IO_funlockfile (s);
1523 #else
1524 __funlockfile (s);
1525 #endif
1526 __libc_cleanup_region_end (0);
1528 return done;
1531 #ifdef USE_IN_LIBIO
1532 # undef vfprintf
1533 # ifdef strong_alias
1534 /* This is for glibc. */
1535 strong_alias (_IO_vfprintf, vfprintf);
1536 # else
1537 # if defined __ELF__ || defined __GNU_LIBRARY__
1538 # include <gnu-stabs.h>
1539 # ifdef weak_alias
1540 weak_alias (_IO_vfprintf, vfprintf);
1541 # endif
1542 # endif
1543 # endif
1544 #endif
1546 /* Handle an unknown format specifier. This prints out a canonicalized
1547 representation of the format spec itself. */
1548 static int
1549 printf_unknown (FILE *s, const struct printf_info *info,
1550 const void *const *args)
1553 int done = 0;
1554 char work_buffer[BUFSIZ];
1555 register char *w;
1557 outchar ('%');
1559 if (info->alt)
1560 outchar ('#');
1561 if (info->group)
1562 outchar ('\'');
1563 if (info->showsign)
1564 outchar ('+');
1565 else if (info->space)
1566 outchar (' ');
1567 if (info->left)
1568 outchar ('-');
1569 if (info->pad == '0')
1570 outchar ('0');
1572 if (info->width != 0)
1574 w = _itoa_word (info->width, workend + 1, 10, 0);
1575 while (w <= workend)
1576 outchar (*w++);
1579 if (info->prec != -1)
1581 outchar ('.');
1582 w = _itoa_word (info->prec, workend + 1, 10, 0);
1583 while (w <= workend)
1584 outchar (*w++);
1587 if (info->spec != '\0')
1588 outchar (info->spec);
1590 all_done:
1591 return done;
1594 /* Group the digits according to the grouping rules of the current locale.
1595 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1596 static char *
1597 internal_function
1598 group_number (CHAR_T *w, CHAR_T *rear_ptr, const CHAR_T *grouping,
1599 wchar_t thousands_sep)
1601 int len;
1602 char *src, *s;
1604 /* We treat all negative values like CHAR_MAX. */
1606 if (*grouping == CHAR_MAX || *grouping <= 0)
1607 /* No grouping should be done. */
1608 return w;
1610 len = *grouping;
1612 /* Copy existing string so that nothing gets overwritten. */
1613 src = (char *) alloca (rear_ptr - w);
1614 s = (char *) __mempcpy (src, w + 1, rear_ptr - w) - 1;
1615 w = rear_ptr;
1617 /* Process all characters in the string. */
1618 while (s >= src)
1620 *w-- = *s--;
1622 if (--len == 0 && s >= src)
1624 /* A new group begins. */
1625 *w-- = thousands_sep;
1627 len = *grouping++;
1628 if (*grouping == '\0')
1629 /* The previous grouping repeats ad infinitum. */
1630 --grouping;
1631 else if (*grouping == CHAR_MAX
1632 #if CHAR_MIN < 0
1633 || *grouping < 0
1634 #endif
1637 /* No further grouping to be done.
1638 Copy the rest of the number. */
1640 *w-- = *s--;
1641 while (s >= src);
1642 break;
1646 return w;
1649 #ifdef USE_IN_LIBIO
1650 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
1651 struct helper_file
1653 struct _IO_FILE_plus _f;
1654 _IO_FILE *_put_stream;
1655 #ifdef _IO_MTSAFE_IO
1656 _IO_lock_t lock;
1657 #endif
1660 static int
1661 _IO_helper_overflow (_IO_FILE *s, int c)
1663 _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
1664 int used = s->_IO_write_ptr - s->_IO_write_base;
1665 if (used)
1667 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
1668 s->_IO_write_ptr -= written;
1670 return PUTC (c, s);
1673 static const struct _IO_jump_t _IO_helper_jumps =
1675 JUMP_INIT_DUMMY,
1676 JUMP_INIT (finish, _IO_default_finish),
1677 JUMP_INIT (overflow, _IO_helper_overflow),
1678 JUMP_INIT (underflow, _IO_default_underflow),
1679 JUMP_INIT (uflow, _IO_default_uflow),
1680 JUMP_INIT (pbackfail, _IO_default_pbackfail),
1681 JUMP_INIT (xsputn, _IO_default_xsputn),
1682 JUMP_INIT (xsgetn, _IO_default_xsgetn),
1683 JUMP_INIT (seekoff, _IO_default_seekoff),
1684 JUMP_INIT (seekpos, _IO_default_seekpos),
1685 JUMP_INIT (setbuf, _IO_default_setbuf),
1686 JUMP_INIT (sync, _IO_default_sync),
1687 JUMP_INIT (doallocate, _IO_default_doallocate),
1688 JUMP_INIT (read, _IO_default_read),
1689 JUMP_INIT (write, _IO_default_write),
1690 JUMP_INIT (seek, _IO_default_seek),
1691 JUMP_INIT (close, _IO_default_close),
1692 JUMP_INIT (stat, _IO_default_stat)
1695 static int
1696 internal_function
1697 buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format,
1698 _IO_va_list args)
1700 char buf[_IO_BUFSIZ];
1701 struct helper_file helper;
1702 register _IO_FILE *hp = (_IO_FILE *) &helper;
1703 int result, to_flush;
1705 /* Initialize helper. */
1706 helper._put_stream = s;
1707 hp->_IO_write_base = buf;
1708 hp->_IO_write_ptr = buf;
1709 hp->_IO_write_end = buf + sizeof buf;
1710 hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS;
1711 #if _IO_JUMPS_OFFSET
1712 hp->_vtable_offset = 0;
1713 #endif
1714 #ifdef _IO_MTSAFE_IO
1715 hp->_lock = &helper.lock;
1716 __libc_lock_init (*hp->_lock);
1717 #endif
1718 _IO_JUMPS (hp) = (struct _IO_jump_t *) &_IO_helper_jumps;
1720 /* Now print to helper instead. */
1721 result = _IO_vfprintf (hp, format, args);
1723 /* Now flush anything from the helper to the S. */
1724 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
1726 if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
1727 return -1;
1730 return result;
1733 #else /* !USE_IN_LIBIO */
1735 static int
1736 internal_function
1737 buffered_vfprintf (register FILE *s, const CHAR_T *format, va_list args)
1739 char buf[BUFSIZ];
1740 int result;
1742 s->__bufp = s->__buffer = buf;
1743 s->__bufsize = sizeof buf;
1744 s->__put_limit = s->__buffer + s->__bufsize;
1745 s->__get_limit = s->__buffer;
1747 /* Now use buffer to print. */
1748 result = vfprintf (s, format, args);
1750 if (fflush (s) == EOF)
1751 result = -1;
1752 s->__buffer = s->__bufp = s->__get_limit = s->__put_limit = NULL;
1753 s->__bufsize = 0;
1755 return result;
1758 /* Pads string with given number of a specified character.
1759 This code is taken from iopadn.c of the GNU I/O library. */
1760 #define PADSIZE 16
1761 static const CHAR_T blanks[PADSIZE] =
1762 { L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '),
1763 L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' ') };
1764 static const CHAR_T zeroes[PADSIZE] =
1765 { L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'),
1766 L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0') };
1768 ssize_t
1769 #ifndef COMPILE_WPRINTF
1770 __printf_pad (FILE *s, char pad, size_t count)
1771 #else
1772 __wprintf_pad (FILE *s, wchar_t pad, size_t count)
1773 #endif
1775 const CHAR_T *padptr;
1776 register size_t i;
1778 padptr = pad == L_(' ') ? blanks : zeroes;
1780 for (i = count; i >= PADSIZE; i -= PADSIZE)
1781 if (PUT (s, padptr, PADSIZE) != PADSIZE)
1782 return -1;
1783 if (i > 0)
1784 if (PUT (s, padptr, i) != i)
1785 return -1;
1787 return count;
1789 #undef PADSIZE
1790 #endif /* USE_IN_LIBIO */