Update.
[glibc.git] / stdio-common / vfprintf.c
bloba40acb75d4c1fd55f3b1dc3e8b18699bc9a844dd
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 <stdlib.h>
24 #include <errno.h>
25 #include <wchar.h>
26 #include <bits/libc-lock.h>
27 #include <sys/param.h>
28 #include "_itoa.h"
29 #include <locale/localeinfo.h>
31 /* This code is shared between the standard stdio implementation found
32 in GNU C library and the libio implementation originally found in
33 GNU libg++.
35 Beside this it is also shared between the normal and wide character
36 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
38 #ifndef COMPILE_WPRINTF
39 # define CHAR_T char
40 # define UCHAR_T unsigned char
41 # define INT_T int
42 # define L_(Str) Str
43 # define ISDIGIT(Ch) isdigit (Ch)
45 # ifdef USE_IN_LIBIO
46 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
47 # define PAD(Padchar) \
48 if (width > 0) \
49 done += _IO_padn (s, (Padchar), width)
50 # else
51 # define PUTC(C, F) putc (C, F)
52 ssize_t __printf_pad __P ((FILE *, char pad, size_t n));
53 # define PAD(Padchar) \
54 if (width > 0) \
55 { ssize_t __res = __printf_pad (s, (Padchar), width); \
56 if (__res == -1) return -1; \
57 done += __res; }
58 # endif
59 #else
60 # define vfprintf vfwprintf
61 # define CHAR_T wchar_t
62 # define UCHAR_T uwchar_t
63 # define INT_T wint_t
64 # define L_(Str) L##Str
65 # define ISDIGIT(Ch) iswdigit (Ch)
67 # ifdef USE_IN_LIBIO
68 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
69 # define PAD(Padchar) \
70 if (width > 0) \
71 done += _IO_wpadn (s, (Padchar), width)
72 # else
73 # define PUTC(C, F) wputc (C, F)
74 ssize_t __wprintf_pad __P ((FILE *, wchar_t pad, size_t n));
75 # define PAD(Padchar) \
76 if (width > 0) \
77 { ssize_t __res = __wprintf_pad (s, (Padchar), width); \
78 if (__res == -1) return -1; \
79 done += __res; }
80 # endif
81 #endif
83 /* Include the shared code for parsing the format string. */
84 #include "printf-parse.h"
87 #ifdef USE_IN_LIBIO
88 /* This code is for use in libio. */
89 # include <libioP.h>
90 # define PUTC(C, F) _IO_putc_unlocked (C, F)
91 # define vfprintf _IO_vfprintf
92 # define FILE _IO_FILE
93 # undef va_list
94 # define va_list _IO_va_list
95 # undef BUFSIZ
96 # define BUFSIZ _IO_BUFSIZ
97 # define ARGCHECK(S, Format) \
98 do \
99 { \
100 /* Check file argument for consistence. */ \
101 CHECK_FILE (S, -1); \
102 if (S->_flags & _IO_NO_WRITES) \
104 __set_errno (EBADF); \
105 return -1; \
107 if (Format == NULL) \
109 MAYBE_SET_EINVAL; \
110 return -1; \
112 } while (0)
113 # define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
114 #else /* ! USE_IN_LIBIO */
115 /* This code is for use in the GNU C library. */
116 # include <stdio.h>
117 # define PUT(F, S, N) fwrite (S, 1, N, F)
118 # define ARGCHECK(S, Format) \
119 do \
121 /* Check file argument for consistence. */ \
122 if (!__validfp (S) || !S->__mode.__write) \
124 __set_errno (EBADF); \
125 return -1; \
127 if (Format == NULL) \
129 __set_errno (EINVAL); \
130 return -1; \
132 if (!S->__seen) \
134 if (__flshfp (S, EOF) == EOF) \
135 return -1; \
138 while (0)
139 # define UNBUFFERED_P(s) ((s)->__buffer == NULL)
141 /* XXX These declarations should go as soon as the stdio header files
142 have these prototypes. */
143 extern void __flockfile (FILE *);
144 extern void __funlockfile (FILE *);
145 #endif /* USE_IN_LIBIO */
148 #define outchar(Ch) \
149 do \
151 register const int outc = (Ch); \
152 if (PUTC (outc, s) == EOF) \
153 return -1; \
154 else \
155 ++done; \
157 while (0)
159 #define outstring(String, Len) \
160 do \
162 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
163 return -1; \
164 done += (Len); \
166 while (0)
168 /* For handling long_double and longlong we use the same flag. */
169 #ifndef is_longlong
170 # define is_longlong is_long_double
171 #endif
174 /* Global variables. */
175 static const char null[] = "(null)";
178 /* Helper function to provide temporary buffering for unbuffered streams. */
179 static int buffered_vfprintf __P ((FILE *stream, const CHAR_T *fmt, va_list))
180 internal_function;
182 /* Handle unknown format specifier. */
183 static int printf_unknown __P ((FILE *, const struct printf_info *,
184 const void *const *));
186 /* Group digits of number string. */
187 static char *group_number __P ((CHAR_T *, CHAR_T *, const CHAR_T *, wchar_t))
188 internal_function;
191 /* The function itself. */
193 vfprintf (FILE *s, const CHAR_T *format, va_list ap)
195 /* The character used as thousands separator. */
196 wchar_t thousands_sep;
198 /* The string describing the size of groups of digits. */
199 const char *grouping;
201 /* Place to accumulate the result. */
202 int done;
204 /* Current character in format string. */
205 const UCHAR_T *f;
207 /* End of leading constant string. */
208 const UCHAR_T *lead_str_end;
210 /* Points to next format specifier. */
211 const UCHAR_T *end_of_spec;
213 /* Buffer intermediate results. */
214 char work_buffer[1000];
215 #define workend (&work_buffer[sizeof (work_buffer) - 1])
217 /* State for restartable multibyte character handling functions. */
218 mbstate_t mbstate;
220 /* We have to save the original argument pointer. */
221 va_list ap_save;
223 /* Count number of specifiers we already processed. */
224 int nspecs_done;
226 /* For the %m format we may need the current `errno' value. */
227 int save_errno = errno;
230 /* This table maps a character into a number representing a
231 class. In each step there is a destination label for each
232 class. */
233 static const int jump_table[] =
235 /* ' ' */ 1, 0, 0, /* '#' */ 4,
236 0, /* '%' */ 14, 0, /* '\''*/ 6,
237 0, 0, /* '*' */ 7, /* '+' */ 2,
238 0, /* '-' */ 3, /* '.' */ 9, 0,
239 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
240 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
241 /* '8' */ 8, /* '9' */ 8, 0, 0,
242 0, 0, 0, 0,
243 0, /* 'A' */ 26, 0, /* 'C' */ 25,
244 0, /* 'E' */ 19, 0, /* 'G' */ 19,
245 0, 0, 0, 0,
246 /* 'L' */ 12, 0, 0, 0,
247 0, 0, 0, /* 'S' */ 21,
248 0, 0, 0, 0,
249 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
250 0, 0, 0, 0,
251 0, /* 'a' */ 26, 0, /* 'c' */ 20,
252 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
253 /* 'h' */ 10, /* 'i' */ 15, 0, 0,
254 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
255 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
256 0, /* 'u' */ 16, 0, 0,
257 /* 'x' */ 18
260 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < ' ' || (Ch) > 'x')
261 #define CHAR_CLASS(Ch) (jump_table[(int) (Ch) - ' '])
262 #define JUMP(ChExpr, table) \
263 do \
265 const void *ptr; \
266 spec = (ChExpr); \
267 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
268 : table[CHAR_CLASS (spec)]; \
269 goto *ptr; \
271 while (0)
273 #define STEP0_3_TABLE \
274 /* Step 0: at the beginning. */ \
275 static const void *step0_jumps[27] = \
277 REF (form_unknown), \
278 REF (flag_space), /* for ' ' */ \
279 REF (flag_plus), /* for '+' */ \
280 REF (flag_minus), /* for '-' */ \
281 REF (flag_hash), /* for '<hash>' */ \
282 REF (flag_zero), /* for '0' */ \
283 REF (flag_quote), /* for '\'' */ \
284 REF (width_asterics), /* for '*' */ \
285 REF (width), /* for '1'...'9' */ \
286 REF (precision), /* for '.' */ \
287 REF (mod_half), /* for 'h' */ \
288 REF (mod_long), /* for 'l' */ \
289 REF (mod_longlong), /* for 'L', 'q' */ \
290 REF (mod_size_t), /* for 'Z' */ \
291 REF (form_percent), /* for '%' */ \
292 REF (form_integer), /* for 'd', 'i' */ \
293 REF (form_unsigned), /* for 'u' */ \
294 REF (form_octal), /* for 'o' */ \
295 REF (form_hexa), /* for 'X', 'x' */ \
296 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
297 REF (form_character), /* for 'c' */ \
298 REF (form_string), /* for 's', 'S' */ \
299 REF (form_pointer), /* for 'p' */ \
300 REF (form_number), /* for 'n' */ \
301 REF (form_strerror), /* for 'm' */ \
302 REF (form_wcharacter), /* for 'C' */ \
303 REF (form_floathex) /* for 'A', 'a' */ \
304 }; \
305 /* Step 1: after processing width. */ \
306 static const void *step1_jumps[27] = \
308 REF (form_unknown), \
309 REF (form_unknown), /* for ' ' */ \
310 REF (form_unknown), /* for '+' */ \
311 REF (form_unknown), /* for '-' */ \
312 REF (form_unknown), /* for '<hash>' */ \
313 REF (form_unknown), /* for '0' */ \
314 REF (form_unknown), /* for '\'' */ \
315 REF (form_unknown), /* for '*' */ \
316 REF (form_unknown), /* for '1'...'9' */ \
317 REF (precision), /* for '.' */ \
318 REF (mod_half), /* for 'h' */ \
319 REF (mod_long), /* for 'l' */ \
320 REF (mod_longlong), /* for 'L', 'q' */ \
321 REF (mod_size_t), /* for 'Z' */ \
322 REF (form_percent), /* for '%' */ \
323 REF (form_integer), /* for 'd', 'i' */ \
324 REF (form_unsigned), /* for 'u' */ \
325 REF (form_octal), /* for 'o' */ \
326 REF (form_hexa), /* for 'X', 'x' */ \
327 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
328 REF (form_character), /* for 'c' */ \
329 REF (form_string), /* for 's', 'S' */ \
330 REF (form_pointer), /* for 'p' */ \
331 REF (form_number), /* for 'n' */ \
332 REF (form_strerror), /* for 'm' */ \
333 REF (form_wcharacter), /* for 'C' */ \
334 REF (form_floathex) /* for 'A', 'a' */ \
335 }; \
336 /* Step 2: after processing precision. */ \
337 static const void *step2_jumps[27] = \
339 REF (form_unknown), \
340 REF (form_unknown), /* for ' ' */ \
341 REF (form_unknown), /* for '+' */ \
342 REF (form_unknown), /* for '-' */ \
343 REF (form_unknown), /* for '<hash>' */ \
344 REF (form_unknown), /* for '0' */ \
345 REF (form_unknown), /* for '\'' */ \
346 REF (form_unknown), /* for '*' */ \
347 REF (form_unknown), /* for '1'...'9' */ \
348 REF (form_unknown), /* for '.' */ \
349 REF (mod_half), /* for 'h' */ \
350 REF (mod_long), /* for 'l' */ \
351 REF (mod_longlong), /* for 'L', 'q' */ \
352 REF (mod_size_t), /* for 'Z' */ \
353 REF (form_percent), /* for '%' */ \
354 REF (form_integer), /* for 'd', 'i' */ \
355 REF (form_unsigned), /* for 'u' */ \
356 REF (form_octal), /* for 'o' */ \
357 REF (form_hexa), /* for 'X', 'x' */ \
358 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
359 REF (form_character), /* for 'c' */ \
360 REF (form_string), /* for 's', 'S' */ \
361 REF (form_pointer), /* for 'p' */ \
362 REF (form_number), /* for 'n' */ \
363 REF (form_strerror), /* for 'm' */ \
364 REF (form_wcharacter), /* for 'C' */ \
365 REF (form_floathex) /* for 'A', 'a' */ \
366 }; \
367 /* Step 3a: after processing first 'h' modifier. */ \
368 static const void *step3a_jumps[27] = \
370 REF (form_unknown), \
371 REF (form_unknown), /* for ' ' */ \
372 REF (form_unknown), /* for '+' */ \
373 REF (form_unknown), /* for '-' */ \
374 REF (form_unknown), /* for '<hash>' */ \
375 REF (form_unknown), /* for '0' */ \
376 REF (form_unknown), /* for '\'' */ \
377 REF (form_unknown), /* for '*' */ \
378 REF (form_unknown), /* for '1'...'9' */ \
379 REF (form_unknown), /* for '.' */ \
380 REF (mod_halfhalf), /* for 'h' */ \
381 REF (form_unknown), /* for 'l' */ \
382 REF (form_unknown), /* for 'L', 'q' */ \
383 REF (form_unknown), /* for 'Z' */ \
384 REF (form_percent), /* for '%' */ \
385 REF (form_integer), /* for 'd', 'i' */ \
386 REF (form_unsigned), /* for 'u' */ \
387 REF (form_octal), /* for 'o' */ \
388 REF (form_hexa), /* for 'X', 'x' */ \
389 REF (form_unknown), /* for 'E', 'e', 'f', 'G', 'g' */ \
390 REF (form_unknown), /* for 'c' */ \
391 REF (form_unknown), /* for 's', 'S' */ \
392 REF (form_unknown), /* for 'p' */ \
393 REF (form_number), /* for 'n' */ \
394 REF (form_unknown), /* for 'm' */ \
395 REF (form_unknown), /* for 'C' */ \
396 REF (form_unknown) /* for 'A', 'a' */ \
397 }; \
398 /* Step 3b: after processing first 'l' modifier. */ \
399 static const void *step3b_jumps[27] = \
401 REF (form_unknown), \
402 REF (form_unknown), /* for ' ' */ \
403 REF (form_unknown), /* for '+' */ \
404 REF (form_unknown), /* for '-' */ \
405 REF (form_unknown), /* for '<hash>' */ \
406 REF (form_unknown), /* for '0' */ \
407 REF (form_unknown), /* for '\'' */ \
408 REF (form_unknown), /* for '*' */ \
409 REF (form_unknown), /* for '1'...'9' */ \
410 REF (form_unknown), /* for '.' */ \
411 REF (form_unknown), /* for 'h' */ \
412 REF (mod_longlong), /* for 'l' */ \
413 REF (form_unknown), /* for 'L', 'q' */ \
414 REF (form_unknown), /* for 'Z' */ \
415 REF (form_percent), /* for '%' */ \
416 REF (form_integer), /* for 'd', 'i' */ \
417 REF (form_unsigned), /* for 'u' */ \
418 REF (form_octal), /* for 'o' */ \
419 REF (form_hexa), /* for 'X', 'x' */ \
420 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
421 REF (form_character), /* for 'c' */ \
422 REF (form_string), /* for 's', 'S' */ \
423 REF (form_pointer), /* for 'p' */ \
424 REF (form_number), /* for 'n' */ \
425 REF (form_strerror), /* for 'm' */ \
426 REF (form_wcharacter), /* for 'C' */ \
427 REF (form_floathex) /* for 'A', 'a' */ \
430 #define STEP4_TABLE \
431 /* Step 4: processing format specifier. */ \
432 static const void *step4_jumps[27] = \
434 REF (form_unknown), \
435 REF (form_unknown), /* for ' ' */ \
436 REF (form_unknown), /* for '+' */ \
437 REF (form_unknown), /* for '-' */ \
438 REF (form_unknown), /* for '<hash>' */ \
439 REF (form_unknown), /* for '0' */ \
440 REF (form_unknown), /* for '\'' */ \
441 REF (form_unknown), /* for '*' */ \
442 REF (form_unknown), /* for '1'...'9' */ \
443 REF (form_unknown), /* for '.' */ \
444 REF (form_unknown), /* for 'h' */ \
445 REF (form_unknown), /* for 'l' */ \
446 REF (form_unknown), /* for 'L', 'q' */ \
447 REF (form_unknown), /* for 'Z' */ \
448 REF (form_percent), /* for '%' */ \
449 REF (form_integer), /* for 'd', 'i' */ \
450 REF (form_unsigned), /* for 'u' */ \
451 REF (form_octal), /* for 'o' */ \
452 REF (form_hexa), /* for 'X', 'x' */ \
453 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
454 REF (form_character), /* for 'c' */ \
455 REF (form_string), /* for 's', 'S' */ \
456 REF (form_pointer), /* for 'p' */ \
457 REF (form_number), /* for 'n' */ \
458 REF (form_strerror), /* for 'm' */ \
459 REF (form_wcharacter), /* for 'C' */ \
460 REF (form_floathex) /* for 'A', 'a' */ \
464 #define process_arg(fspec) \
465 /* Start real work. We know about all flags and modifiers and \
466 now process the wanted format specifier. */ \
467 LABEL (form_percent): \
468 /* Write a literal "%". */ \
469 outchar ('%'); \
470 break; \
472 LABEL (form_integer): \
473 /* Signed decimal integer. */ \
474 base = 10; \
476 if (is_longlong) \
478 long long int signed_number; \
480 if (fspec == NULL) \
481 signed_number = va_arg (ap, long long int); \
482 else \
483 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
485 is_negative = signed_number < 0; \
486 number.longlong = is_negative ? (- signed_number) : signed_number; \
488 goto LABEL (longlong_number); \
490 else \
492 long int signed_number; \
494 if (fspec == NULL) \
496 if (is_long) \
497 signed_number = va_arg (ap, long int); \
498 else /* `char' and `short int' will be promoted to `int'. */ \
499 signed_number = va_arg (ap, int); \
501 else \
502 if (is_long) \
503 signed_number = args_value[fspec->data_arg].pa_long_int; \
504 else \
505 signed_number = args_value[fspec->data_arg].pa_int; \
507 is_negative = signed_number < 0; \
508 number.word = is_negative ? (- signed_number) : signed_number; \
510 goto LABEL (number); \
512 /* NOTREACHED */ \
514 LABEL (form_unsigned): \
515 /* Unsigned decimal integer. */ \
516 base = 10; \
517 goto LABEL (unsigned_number); \
518 /* NOTREACHED */ \
520 LABEL (form_octal): \
521 /* Unsigned octal integer. */ \
522 base = 8; \
523 goto LABEL (unsigned_number); \
524 /* NOTREACHED */ \
526 LABEL (form_hexa): \
527 /* Unsigned hexadecimal integer. */ \
528 base = 16; \
530 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
532 /* ISO specifies the `+' and ` ' flags only for signed \
533 conversions. */ \
534 is_negative = 0; \
535 showsign = 0; \
536 space = 0; \
538 if (is_longlong) \
540 if (fspec == NULL) \
541 number.longlong = va_arg (ap, unsigned long long int); \
542 else \
543 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
545 LABEL (longlong_number): \
546 if (prec < 0) \
547 /* Supply a default precision if none was given. */ \
548 prec = 1; \
549 else \
550 /* We have to take care for the '0' flag. If a precision \
551 is given it must be ignored. */ \
552 pad = ' '; \
554 /* If the precision is 0 and the number is 0 nothing has to \
555 be written for the number, except for the 'o' format in \
556 alternate form. */ \
557 if (prec == 0 && number.longlong == 0) \
559 string = workend; \
560 if (base == 8 && alt) \
561 *string-- = '0'; \
563 else \
565 /* Put the number in WORK. */ \
566 string = _itoa (number.longlong, workend + 1, base, \
567 spec == 'X'); \
568 string -= 1; \
569 if (group && grouping) \
570 string = group_number (string, workend, grouping, \
571 thousands_sep); \
573 /* Simply further test for num != 0. */ \
574 number.word = number.longlong != 0; \
576 else \
578 if (fspec == NULL) \
580 if (is_long) \
581 number.word = va_arg (ap, unsigned long int); \
582 else if (!is_short) \
583 number.word = va_arg (ap, unsigned int); \
584 else \
585 number.word = (unsigned short int) va_arg (ap, unsigned int); \
587 else \
588 if (is_long) \
589 number.word = args_value[fspec->data_arg].pa_u_long_int; \
590 else if (is_char) \
591 number.word = (unsigned char) \
592 args_value[fspec->data_arg].pa_char; \
593 else if (!is_short) \
594 number.word = args_value[fspec->data_arg].pa_u_int; \
595 else \
596 number.word = (unsigned short int) \
597 args_value[fspec->data_arg].pa_u_short_int; \
599 LABEL (number): \
600 if (prec < 0) \
601 /* Supply a default precision if none was given. */ \
602 prec = 1; \
603 else \
604 /* We have to take care for the '0' flag. If a precision \
605 is given it must be ignored. */ \
606 pad = ' '; \
608 /* If the precision is 0 and the number is 0 nothing has to \
609 be written for the number, except for the 'o' format in \
610 alternate form. */ \
611 if (prec == 0 && number.word == 0) \
613 string = workend; \
614 if (base == 8 && alt) \
615 *string-- = '0'; \
617 else \
619 /* Put the number in WORK. */ \
620 string = _itoa_word (number.word, workend + 1, base, \
621 spec == 'X'); \
622 string -= 1; \
623 if (group && grouping) \
624 string = group_number (string, workend, grouping, \
625 thousands_sep); \
629 prec -= workend - string; \
631 if (prec > 0) \
632 /* Add zeros to the precision. */ \
633 while (prec-- > 0) \
634 *string-- = '0'; \
635 else if (number.word != 0 && alt && base == 8) \
636 /* Add octal marker. */ \
637 *string-- = '0'; \
639 if (!left) \
641 width -= workend - string; \
643 if (number.word != 0 && alt && base == 16) \
644 /* Account for 0X hex marker. */ \
645 width -= 2; \
647 if (is_negative || showsign || space) \
648 --width; \
650 if (pad == '0') \
652 while (width-- > 0) \
653 *string-- = '0'; \
655 if (number.word != 0 && alt && base == 16) \
657 *string-- = spec; \
658 *string-- = '0'; \
661 if (is_negative) \
662 *string-- = '-'; \
663 else if (showsign) \
664 *string-- = '+'; \
665 else if (space) \
666 *string-- = ' '; \
668 else \
670 if (number.word != 0 && alt && base == 16) \
672 *string-- = spec; \
673 *string-- = '0'; \
676 if (is_negative) \
677 *string-- = '-'; \
678 else if (showsign) \
679 *string-- = '+'; \
680 else if (space) \
681 *string-- = ' '; \
683 while (width-- > 0) \
684 *string-- = ' '; \
687 outstring (string + 1, workend - string); \
689 break; \
691 else \
693 if (number.word != 0 && alt && base == 16) \
695 *string-- = spec; \
696 *string-- = '0'; \
699 if (is_negative) \
700 *string-- = '-'; \
701 else if (showsign) \
702 *string-- = '+'; \
703 else if (space) \
704 *string-- = ' '; \
706 width -= workend - string; \
707 outstring (string + 1, workend - string); \
709 PAD (' '); \
710 break; \
713 LABEL (form_float): \
715 /* Floating-point number. This is handled by printf_fp.c. */ \
716 extern int __printf_fp __P ((FILE *, const struct printf_info *, \
717 const void **const)); \
718 const void *ptr; \
719 int function_done; \
721 if (fspec == NULL) \
723 struct printf_info info = { prec: prec, \
724 width: width, \
725 spec: spec, \
726 is_long_double: is_long_double, \
727 is_short: is_short, \
728 is_long: is_long, \
729 alt: alt, \
730 space: space, \
731 left: left, \
732 showsign: showsign, \
733 group: group, \
734 pad: pad, \
735 extra: 0 }; \
737 if (is_long_double) \
738 the_arg.pa_long_double = va_arg (ap, long double); \
739 else \
740 the_arg.pa_double = va_arg (ap, double); \
741 ptr = (const void *) &the_arg; \
743 function_done = __printf_fp (s, &info, &ptr); \
745 else \
747 ptr = (const void *) &args_value[fspec->data_arg]; \
749 function_done = __printf_fp (s, &fspec->info, &ptr); \
752 if (function_done < 0) \
753 /* Error in print handler. */ \
754 return -1; \
756 done += function_done; \
758 break; \
760 LABEL (form_floathex): \
762 /* FLoating point number printed as hexadecimal number. */ \
763 extern int __printf_fphex __P ((FILE *, const struct printf_info *, \
764 const void **const)); \
765 const void *ptr; \
766 int function_done; \
768 if (fspec == NULL) \
770 struct printf_info info = { prec: prec, \
771 width: width, \
772 spec: spec, \
773 is_long_double: is_long_double, \
774 is_short: is_short, \
775 is_long: is_long, \
776 alt: alt, \
777 space: space, \
778 left: left, \
779 showsign: showsign, \
780 group: group, \
781 pad: pad, \
782 extra: 0 }; \
784 if (is_long_double) \
785 the_arg.pa_long_double = va_arg (ap, long double); \
786 else \
787 the_arg.pa_double = va_arg (ap, double); \
788 ptr = (const void *) &the_arg; \
790 function_done = __printf_fphex (s, &info, &ptr); \
792 else \
794 ptr = (const void *) &args_value[fspec->data_arg]; \
796 function_done = __printf_fphex (s, &fspec->info, &ptr); \
799 if (function_done < 0) \
800 /* Error in print handler. */ \
801 return -1; \
803 done += function_done; \
805 break; \
807 LABEL (form_character): \
808 /* Character. */ \
809 if (is_long) \
810 goto LABEL (form_wcharacter); \
811 --width; /* Account for the character itself. */ \
812 if (!left) \
813 PAD (' '); \
814 if (fspec == NULL) \
815 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
816 else \
817 outchar ((unsigned char) args_value[fspec->data_arg].pa_char); \
818 if (left) \
819 PAD (' '); \
820 break; \
822 LABEL (form_wcharacter): \
824 /* Wide character. */ \
825 char buf[MB_CUR_MAX]; \
826 mbstate_t mbstate; \
827 size_t len; \
829 memset (&mbstate, '\0', sizeof (mbstate_t)); \
830 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wint_t) \
831 : args_value[fspec->data_arg].pa_wchar), \
832 &mbstate); \
833 width -= len; \
834 if (!left) \
835 PAD (' '); \
836 outstring (buf, len); \
837 if (left) \
838 PAD (' '); \
840 break; \
842 LABEL (form_string): \
844 size_t len; \
846 /* The string argument could in fact be `char *' or `wchar_t *'. \
847 But this should not make a difference here. */ \
848 if (fspec == NULL) \
849 string = (char *) va_arg (ap, const char *); \
850 else \
851 string = (char *) args_value[fspec->data_arg].pa_string; \
853 /* Entry point for printing other strings. */ \
854 LABEL (print_string): \
856 if (string == NULL) \
858 /* Write "(null)" if there's space. */ \
859 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
861 string = (char *) null; \
862 len = sizeof (null) - 1; \
864 else \
866 string = (char *) ""; \
867 len = 0; \
870 else if (!is_long && spec != L_('S')) \
872 if (prec != -1) \
873 /* Search for the end of the string, but don't search past \
874 the length specified by the precision. */ \
875 len = strnlen (string, prec); \
876 else \
877 len = strlen (string); \
879 else \
881 const wchar_t *s2 = (const wchar_t *) string; \
882 mbstate_t mbstate; \
884 memset (&mbstate, '\0', sizeof (mbstate_t)); \
885 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
886 if (len == (size_t) -1) \
887 /* Illegal wide-character string. */ \
888 return -1; \
890 assert (__mbsinit (&mbstate)); \
891 s2 = (const wchar_t *) string; \
892 string = alloca (len + 1); \
893 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
894 if (prec < len) \
895 len = prec; \
898 if ((width -= len) < 0) \
900 outstring (string, len); \
901 break; \
904 if (!left) \
905 PAD (' '); \
906 outstring (string, len); \
907 if (left) \
908 PAD (' '); \
910 break; \
912 LABEL (form_pointer): \
913 /* Generic pointer. */ \
915 const void *ptr; \
916 if (fspec == NULL) \
917 ptr = va_arg (ap, void *); \
918 else \
919 ptr = args_value[fspec->data_arg].pa_pointer; \
920 if (ptr != NULL) \
922 /* If the pointer is not NULL, write it as a %#x spec. */ \
923 base = 16; \
924 number.word = (unsigned long int) ptr; \
925 is_negative = 0; \
926 alt = 1; \
927 group = 0; \
928 spec = 'x'; \
929 goto LABEL (number); \
931 else \
933 /* Write "(nil)" for a nil pointer. */ \
934 string = (char *) "(nil)"; \
935 /* Make sure the full string "(nil)" is printed. */ \
936 if (prec < 5) \
937 prec = 5; \
938 is_long = 0; /* This is no wide-char string. */ \
939 goto LABEL (print_string); \
942 /* NOTREACHED */ \
944 LABEL (form_number): \
945 /* Answer the count of characters written. */ \
946 if (fspec == NULL) \
948 if (is_longlong) \
949 *(long long int *) va_arg (ap, void *) = done; \
950 else if (is_long) \
951 *(long int *) va_arg (ap, void *) = done; \
952 else if (!is_short) \
953 *(int *) va_arg (ap, void *) = done; \
954 else \
955 *(short int *) va_arg (ap, void *) = done; \
957 else \
958 if (is_longlong) \
959 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
960 else if (is_long) \
961 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
962 else if (!is_short) \
963 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
964 else \
965 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
966 break; \
968 LABEL (form_strerror): \
969 /* Print description of error ERRNO. */ \
970 string = \
971 (char *) __strerror_r (save_errno, work_buffer, sizeof work_buffer); \
972 is_long = 0; /* This is no wide-char string. */ \
973 goto LABEL (print_string)
976 /* Sanity check of arguments. */
977 ARGCHECK (s, format);
979 if (UNBUFFERED_P (s))
980 /* Use a helper function which will allocate a local temporary buffer
981 for the stream and then call us again. */
982 return buffered_vfprintf (s, format, ap);
984 /* Initialize local variables. */
985 done = 0;
986 grouping = (const char *) -1;
987 #ifdef __va_copy
988 /* This macro will be available soon in gcc's <stdarg.h>. We need it
989 since on some systems `va_list' is not an integral type. */
990 __va_copy (ap_save, ap);
991 #else
992 ap_save = ap;
993 #endif
994 nspecs_done = 0;
996 /* Put state for processing format string in initial state. */
997 memset (&mbstate, '\0', sizeof (mbstate_t));
999 /* Find the first format specifier. */
1000 f = lead_str_end = find_spec (format, &mbstate);
1002 /* Lock stream. */
1003 #ifdef USE_IN_LIBIO
1004 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
1005 _IO_flockfile (s);
1006 #else
1007 __libc_cleanup_region_start ((void (*) (void *)) &__funlockfile, s);
1008 __flockfile (s);
1009 #endif
1011 /* Write the literal text before the first format. */
1012 outstring ((const UCHAR_T *) format,
1013 lead_str_end - (const UCHAR_T *) format);
1015 /* If we only have to print a simple string, return now. */
1016 if (*f == L_('\0'))
1017 goto all_done;
1019 /* Process whole format string. */
1022 #define REF(Name) &&do_##Name
1023 #define LABEL(Name) do_##Name
1024 STEP0_3_TABLE;
1025 STEP4_TABLE;
1027 union printf_arg *args_value; /* This is not used here but ... */
1028 int is_negative; /* Flag for negative number. */
1029 union
1031 unsigned long long int longlong;
1032 unsigned long int word;
1033 } number;
1034 int base;
1035 union printf_arg the_arg;
1036 char *string; /* Pointer to argument string. */
1037 int alt = 0; /* Alternate format. */
1038 int space = 0; /* Use space prefix if no sign is needed. */
1039 int left = 0; /* Left-justify output. */
1040 int showsign = 0; /* Always begin with plus or minus sign. */
1041 int group = 0; /* Print numbers according grouping rules. */
1042 int is_long_double = 0; /* Argument is long double/ long long int. */
1043 int is_short = 0; /* Argument is long int. */
1044 int is_long = 0; /* Argument is short int. */
1045 int is_char = 0; /* Argument is promoted (unsigned) char. */
1046 int width = 0; /* Width of output; 0 means none specified. */
1047 int prec = -1; /* Precision of output; -1 means none specified. */
1048 char pad = ' '; /* Padding character. */
1049 CHAR_T spec;
1051 /* Get current character in format string. */
1052 JUMP (*++f, step0_jumps);
1054 /* ' ' flag. */
1055 LABEL (flag_space):
1056 space = 1;
1057 JUMP (*++f, step0_jumps);
1059 /* '+' flag. */
1060 LABEL (flag_plus):
1061 showsign = 1;
1062 JUMP (*++f, step0_jumps);
1064 /* The '-' flag. */
1065 LABEL (flag_minus):
1066 left = 1;
1067 pad = L_(' ');
1068 JUMP (*++f, step0_jumps);
1070 /* The '#' flag. */
1071 LABEL (flag_hash):
1072 alt = 1;
1073 JUMP (*++f, step0_jumps);
1075 /* The '0' flag. */
1076 LABEL (flag_zero):
1077 if (!left)
1078 pad = L_('0');
1079 JUMP (*++f, step0_jumps);
1081 /* The '\'' flag. */
1082 LABEL (flag_quote):
1083 group = 1;
1085 /* XXX Completely wrong. Use wctob. */
1086 if (grouping == (const char *) -1)
1088 /* Figure out the thousands separator character. */
1089 if (mbtowc (&thousands_sep,
1090 _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
1091 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
1092 thousands_sep = (wchar_t)
1093 *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1094 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1095 if (*grouping == '\0' || *grouping == CHAR_MAX
1096 || thousands_sep == L'\0')
1097 grouping = NULL;
1099 JUMP (*++f, step0_jumps);
1101 /* Get width from argument. */
1102 LABEL (width_asterics):
1104 const UCHAR_T *tmp; /* Temporary value. */
1106 tmp = ++f;
1107 if (ISDIGIT (*tmp) && read_int (&tmp) && *tmp == L_('$'))
1108 /* The width comes from a positional parameter. */
1109 goto do_positional;
1111 width = va_arg (ap, int);
1113 /* Negative width means left justified. */
1114 if (width < 0)
1116 width = -width;
1117 pad = L_(' ');
1118 left = 1;
1121 JUMP (*f, step1_jumps);
1123 /* Given width in format string. */
1124 LABEL (width):
1125 width = read_int (&f);
1126 if (*f == L_('$'))
1127 /* Oh, oh. The argument comes from a positional parameter. */
1128 goto do_positional;
1129 JUMP (*f, step1_jumps);
1131 LABEL (precision):
1132 ++f;
1133 if (*f == L_('*'))
1135 const UCHAR_T *tmp; /* Temporary value. */
1137 tmp = ++f;
1138 if (ISDIGIT (*tmp) && read_int (&tmp) > 0 && *tmp == L_('$'))
1139 /* The precision comes from a positional parameter. */
1140 goto do_positional;
1142 prec = va_arg (ap, int);
1144 /* If the precision is negative the precision is omitted. */
1145 if (prec < 0)
1146 prec = -1;
1148 else if (ISDIGIT (*f))
1149 prec = read_int (&f);
1150 else
1151 prec = 0;
1152 JUMP (*f, step2_jumps);
1154 /* Process 'h' modifier. There might another 'h' following. */
1155 LABEL (mod_half):
1156 is_short = 1;
1157 JUMP (*++f, step3a_jumps);
1159 /* Process 'hh' modifier. */
1160 LABEL (mod_halfhalf):
1161 is_short = 0;
1162 is_char = 1;
1163 JUMP (*++f, step4_jumps);
1165 /* Process 'l' modifier. There might another 'l' following. */
1166 LABEL (mod_long):
1167 is_long = 1;
1168 JUMP (*++f, step3b_jumps);
1170 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1171 allowed to follow. */
1172 LABEL (mod_longlong):
1173 is_long_double = 1;
1174 JUMP (*++f, step4_jumps);
1176 LABEL (mod_size_t):
1177 is_longlong = sizeof (size_t) > sizeof (unsigned long int);
1178 is_long = sizeof (size_t) > sizeof (unsigned int);
1179 JUMP (*++f, step4_jumps);
1181 /* Process current format. */
1182 while (1)
1184 process_arg (((struct printf_spec *) NULL));
1186 LABEL (form_unknown):
1187 if (spec == L_('\0'))
1189 /* The format string ended before the specifier is complete. */
1190 done = -1;
1191 goto all_done;
1194 /* If we are in the fast loop force entering the complicated
1195 one. */
1196 goto do_positional;
1199 /* The format is correctly handled. */
1200 ++nspecs_done;
1202 /* Look for next format specifier. */
1203 f = find_spec ((end_of_spec = ++f), &mbstate);
1205 /* Write the following constant string. */
1206 outstring (end_of_spec, f - end_of_spec);
1208 while (*f != L_('\0'));
1210 /* Unlock stream and return. */
1211 goto all_done;
1213 /* Here starts the more complex loop to handle positional parameters. */
1214 do_positional:
1216 /* Array with information about the needed arguments. This has to
1217 be dynamically extensible. */
1218 size_t nspecs = 0;
1219 size_t nspecs_max = 32; /* A more or less arbitrary start value. */
1220 struct printf_spec *specs
1221 = alloca (nspecs_max * sizeof (struct printf_spec));
1223 /* The number of arguments the format string requests. This will
1224 determine the size of the array needed to store the argument
1225 attributes. */
1226 size_t nargs = 0;
1227 int *args_type;
1228 union printf_arg *args_value;
1230 /* Positional parameters refer to arguments directly. This could
1231 also determine the maximum number of arguments. Track the
1232 maximum number. */
1233 size_t max_ref_arg = 0;
1235 /* Just a counter. */
1236 size_t cnt;
1239 if (grouping == (const char *) -1)
1241 /* XXX Use wctob. But this is incompatible for now. */
1242 /* Figure out the thousands separator character. */
1243 if (mbtowc (&thousands_sep,
1244 _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
1245 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
1246 thousands_sep = (wchar_t) *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1247 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1248 if (*grouping == '\0' || *grouping == CHAR_MAX
1249 || thousands_sep == L'\0')
1250 grouping = NULL;
1253 for (f = lead_str_end; *f != '\0'; f = specs[nspecs++].next_fmt)
1255 if (nspecs >= nspecs_max)
1257 /* Extend the array of format specifiers. */
1258 struct printf_spec *old = specs;
1260 nspecs_max *= 2;
1261 specs = alloca (nspecs_max * sizeof (struct printf_spec));
1263 if (specs == &old[nspecs])
1264 /* Stack grows up, OLD was the last thing allocated;
1265 extend it. */
1266 nspecs_max += nspecs_max / 2;
1267 else
1269 /* Copy the old array's elements to the new space. */
1270 memcpy (specs, old, nspecs * sizeof (struct printf_spec));
1271 if (old == &specs[nspecs])
1272 /* Stack grows down, OLD was just below the new
1273 SPECS. We can use that space when the new space
1274 runs out. */
1275 nspecs_max += nspecs_max / 2;
1279 /* Parse the format specifier. */
1280 nargs += parse_one_spec (f, nargs, &specs[nspecs], &max_ref_arg,
1281 &mbstate);
1284 /* Determine the number of arguments the format string consumes. */
1285 nargs = MAX (nargs, max_ref_arg);
1287 /* Allocate memory for the argument descriptions. */
1288 args_type = alloca (nargs * sizeof (int));
1289 memset (args_type, 0, nargs * sizeof (int));
1290 args_value = alloca (nargs * sizeof (union printf_arg));
1292 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1293 still zero after this loop, format is invalid. For now we
1294 simply use 0 as the value. */
1296 /* Fill in the types of all the arguments. */
1297 for (cnt = 0; cnt < nspecs; ++cnt)
1299 /* If the width is determined by an argument this is an int. */
1300 if (specs[cnt].width_arg != -1)
1301 args_type[specs[cnt].width_arg] = PA_INT;
1303 /* If the precision is determined by an argument this is an int. */
1304 if (specs[cnt].prec_arg != -1)
1305 args_type[specs[cnt].prec_arg] = PA_INT;
1307 switch (specs[cnt].ndata_args)
1309 case 0: /* No arguments. */
1310 break;
1311 case 1: /* One argument; we already have the type. */
1312 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1313 break;
1314 default:
1315 /* We have more than one argument for this format spec.
1316 We must call the arginfo function again to determine
1317 all the types. */
1318 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1319 (&specs[cnt].info,
1320 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg]);
1321 break;
1325 /* Now we know all the types and the order. Fill in the argument
1326 values. */
1327 for (cnt = 0; cnt < nargs; ++cnt)
1328 switch (args_type[cnt])
1330 #define T(tag, mem, type) \
1331 case tag: \
1332 args_value[cnt].mem = va_arg (ap_save, type); \
1333 break
1335 T (PA_CHAR, pa_char, int); /* Promoted. */
1336 T (PA_WCHAR, pa_wchar, wint_t);
1337 T (PA_INT|PA_FLAG_SHORT, pa_short_int, int); /* Promoted. */
1338 T (PA_INT, pa_int, int);
1339 T (PA_INT|PA_FLAG_LONG, pa_long_int, long int);
1340 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1341 T (PA_FLOAT, pa_float, double); /* Promoted. */
1342 T (PA_DOUBLE, pa_double, double);
1343 T (PA_DOUBLE|PA_FLAG_LONG_DOUBLE, pa_long_double, long double);
1344 T (PA_STRING, pa_string, const char *);
1345 T (PA_WSTRING, pa_wstring, const wchar_t *);
1346 T (PA_POINTER, pa_pointer, void *);
1347 #undef T
1348 default:
1349 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1350 args_value[cnt].pa_pointer = va_arg (ap_save, void *);
1351 else
1352 args_value[cnt].pa_long_double = 0.0;
1353 break;
1356 /* Now walk through all format specifiers and process them. */
1357 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1359 #undef REF
1360 #define REF(Name) &&do2_##Name
1361 #undef LABEL
1362 #define LABEL(Name) do2_##Name
1363 STEP4_TABLE;
1365 int is_negative;
1366 union
1368 unsigned long long int longlong;
1369 unsigned long int word;
1370 } number;
1371 int base;
1372 union printf_arg the_arg;
1373 char *string; /* Pointer to argument string. */
1375 /* Fill variables from values in struct. */
1376 int alt = specs[nspecs_done].info.alt;
1377 int space = specs[nspecs_done].info.space;
1378 int left = specs[nspecs_done].info.left;
1379 int showsign = specs[nspecs_done].info.showsign;
1380 int group = specs[nspecs_done].info.group;
1381 int is_long_double = specs[nspecs_done].info.is_long_double;
1382 int is_short = specs[nspecs_done].info.is_short;
1383 int is_char = specs[nspecs_done].info.is_char;
1384 int is_long = specs[nspecs_done].info.is_long;
1385 int width = specs[nspecs_done].info.width;
1386 int prec = specs[nspecs_done].info.prec;
1387 char pad = specs[nspecs_done].info.pad;
1388 CHAR_T spec = specs[nspecs_done].info.spec;
1390 /* Fill in last information. */
1391 if (specs[nspecs_done].width_arg != -1)
1393 /* Extract the field width from an argument. */
1394 specs[nspecs_done].info.width =
1395 args_value[specs[nspecs_done].width_arg].pa_int;
1397 if (specs[nspecs_done].info.width < 0)
1398 /* If the width value is negative left justification is
1399 selected and the value is taken as being positive. */
1401 specs[nspecs_done].info.width *= -1;
1402 left = specs[nspecs_done].info.left = 1;
1404 width = specs[nspecs_done].info.width;
1407 if (specs[nspecs_done].prec_arg != -1)
1409 /* Extract the precision from an argument. */
1410 specs[nspecs_done].info.prec =
1411 args_value[specs[nspecs_done].prec_arg].pa_int;
1413 if (specs[nspecs_done].info.prec < 0)
1414 /* If the precision is negative the precision is
1415 omitted. */
1416 specs[nspecs_done].info.prec = -1;
1418 prec = specs[nspecs_done].info.prec;
1421 /* Process format specifiers. */
1422 while (1)
1424 JUMP (spec, step4_jumps);
1426 process_arg ((&specs[nspecs_done]));
1428 LABEL (form_unknown):
1430 extern printf_function **__printf_function_table;
1431 int function_done;
1432 printf_function *function;
1433 unsigned int i;
1434 const void **ptr;
1436 function =
1437 (__printf_function_table == NULL ? NULL :
1438 __printf_function_table[specs[nspecs_done].info.spec]);
1440 if (function == NULL)
1441 function = &printf_unknown;
1443 ptr = alloca (specs[nspecs_done].ndata_args
1444 * sizeof (const void *));
1446 /* Fill in an array of pointers to the argument values. */
1447 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
1448 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1450 /* Call the function. */
1451 function_done = (*function) (s, &specs[nspecs_done].info, ptr);
1453 /* If an error occurred we don't have information about #
1454 of chars. */
1455 if (function_done < 0)
1457 done = -1;
1458 goto all_done;
1461 done += function_done;
1463 break;
1466 /* Write the following constant string. */
1467 outstring (specs[nspecs_done].end_of_fmt,
1468 specs[nspecs_done].next_fmt
1469 - specs[nspecs_done].end_of_fmt);
1473 all_done:
1474 /* Unlock the stream. */
1475 #ifdef USE_IN_LIBIO
1476 _IO_funlockfile (s);
1477 #else
1478 __funlockfile (s);
1479 #endif
1480 __libc_cleanup_region_end (0);
1482 return done;
1485 #ifdef USE_IN_LIBIO
1486 # undef vfprintf
1487 # ifdef strong_alias
1488 /* This is for glibc. */
1489 strong_alias (_IO_vfprintf, vfprintf);
1490 # else
1491 # if defined __ELF__ || defined __GNU_LIBRARY__
1492 # include <gnu-stabs.h>
1493 # ifdef weak_alias
1494 weak_alias (_IO_vfprintf, vfprintf);
1495 # endif
1496 # endif
1497 # endif
1498 #endif
1500 /* Handle an unknown format specifier. This prints out a canonicalized
1501 representation of the format spec itself. */
1502 static int
1503 printf_unknown (FILE *s, const struct printf_info *info,
1504 const void *const *args)
1507 int done = 0;
1508 char work_buffer[BUFSIZ];
1509 register char *w;
1511 outchar ('%');
1513 if (info->alt)
1514 outchar ('#');
1515 if (info->group)
1516 outchar ('\'');
1517 if (info->showsign)
1518 outchar ('+');
1519 else if (info->space)
1520 outchar (' ');
1521 if (info->left)
1522 outchar ('-');
1523 if (info->pad == '0')
1524 outchar ('0');
1526 if (info->width != 0)
1528 w = _itoa_word (info->width, workend + 1, 10, 0);
1529 while (w <= workend)
1530 outchar (*w++);
1533 if (info->prec != -1)
1535 outchar ('.');
1536 w = _itoa_word (info->prec, workend + 1, 10, 0);
1537 while (w <= workend)
1538 outchar (*w++);
1541 if (info->spec != '\0')
1542 outchar (info->spec);
1544 return done;
1547 /* Group the digits according to the grouping rules of the current locale.
1548 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1549 static char *
1550 internal_function
1551 group_number (CHAR_T *w, CHAR_T *rear_ptr, const CHAR_T *grouping,
1552 wchar_t thousands_sep)
1554 int len;
1555 char *src, *s;
1557 /* We treat all negative values like CHAR_MAX. */
1559 if (*grouping == CHAR_MAX || *grouping <= 0)
1560 /* No grouping should be done. */
1561 return w;
1563 len = *grouping;
1565 /* Copy existing string so that nothing gets overwritten. */
1566 src = (char *) alloca (rear_ptr - w);
1567 s = (char *) __mempcpy (src, w + 1, rear_ptr - w) - 1;
1568 w = rear_ptr;
1570 /* Process all characters in the string. */
1571 while (s >= src)
1573 *w-- = *s--;
1575 if (--len == 0 && s >= src)
1577 /* A new group begins. */
1578 *w-- = thousands_sep;
1580 len = *grouping++;
1581 if (*grouping == '\0')
1582 /* The previous grouping repeats ad infinitum. */
1583 --grouping;
1584 else if (*grouping == CHAR_MAX
1585 #if CHAR_MIN < 0
1586 || *grouping < 0
1587 #endif
1590 /* No further grouping to be done.
1591 Copy the rest of the number. */
1593 *w-- = *s--;
1594 while (s >= src);
1595 break;
1599 return w;
1602 #ifdef USE_IN_LIBIO
1603 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
1604 struct helper_file
1606 struct _IO_FILE_plus _f;
1607 _IO_FILE *_put_stream;
1608 #ifdef _IO_MTSAFE_IO
1609 _IO_lock_t lock;
1610 #endif
1613 static int
1614 _IO_helper_overflow (_IO_FILE *s, int c)
1616 _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
1617 int used = s->_IO_write_ptr - s->_IO_write_base;
1618 if (used)
1620 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
1621 s->_IO_write_ptr -= written;
1623 return PUTC (c, s);
1626 static const struct _IO_jump_t _IO_helper_jumps =
1628 JUMP_INIT_DUMMY,
1629 JUMP_INIT (finish, _IO_default_finish),
1630 JUMP_INIT (overflow, _IO_helper_overflow),
1631 JUMP_INIT (underflow, _IO_default_underflow),
1632 JUMP_INIT (uflow, _IO_default_uflow),
1633 JUMP_INIT (pbackfail, _IO_default_pbackfail),
1634 JUMP_INIT (xsputn, _IO_default_xsputn),
1635 JUMP_INIT (xsgetn, _IO_default_xsgetn),
1636 JUMP_INIT (seekoff, _IO_default_seekoff),
1637 JUMP_INIT (seekpos, _IO_default_seekpos),
1638 JUMP_INIT (setbuf, _IO_default_setbuf),
1639 JUMP_INIT (sync, _IO_default_sync),
1640 JUMP_INIT (doallocate, _IO_default_doallocate),
1641 JUMP_INIT (read, _IO_default_read),
1642 JUMP_INIT (write, _IO_default_write),
1643 JUMP_INIT (seek, _IO_default_seek),
1644 JUMP_INIT (close, _IO_default_close),
1645 JUMP_INIT (stat, _IO_default_stat)
1648 static int
1649 internal_function
1650 buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format,
1651 _IO_va_list args)
1653 char buf[_IO_BUFSIZ];
1654 struct helper_file helper;
1655 register _IO_FILE *hp = (_IO_FILE *) &helper;
1656 int result, to_flush;
1658 /* Initialize helper. */
1659 helper._put_stream = s;
1660 hp->_IO_write_base = buf;
1661 hp->_IO_write_ptr = buf;
1662 hp->_IO_write_end = buf + sizeof buf;
1663 hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS;
1664 #if _IO_JUMPS_OFFSET
1665 hp->_vtable_offset = 0;
1666 #endif
1667 #ifdef _IO_MTSAFE_IO
1668 hp->_lock = &helper.lock;
1669 __libc_lock_init (*hp->_lock);
1670 #endif
1671 _IO_JUMPS (hp) = (struct _IO_jump_t *) &_IO_helper_jumps;
1673 /* Now print to helper instead. */
1674 result = _IO_vfprintf (hp, format, args);
1676 /* Now flush anything from the helper to the S. */
1677 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
1679 if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
1680 return -1;
1683 return result;
1686 #else /* !USE_IN_LIBIO */
1688 static int
1689 internal_function
1690 buffered_vfprintf (register FILE *s, const CHAR_T *format, va_list args)
1692 char buf[BUFSIZ];
1693 int result;
1695 s->__bufp = s->__buffer = buf;
1696 s->__bufsize = sizeof buf;
1697 s->__put_limit = s->__buffer + s->__bufsize;
1698 s->__get_limit = s->__buffer;
1700 /* Now use buffer to print. */
1701 result = vfprintf (s, format, args);
1703 if (fflush (s) == EOF)
1704 result = -1;
1705 s->__buffer = s->__bufp = s->__get_limit = s->__put_limit = NULL;
1706 s->__bufsize = 0;
1708 return result;
1711 /* Pads string with given number of a specified character.
1712 This code is taken from iopadn.c of the GNU I/O library. */
1713 #define PADSIZE 16
1714 static const CHAR_T blanks[PADSIZE] =
1715 { L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '),
1716 L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' ') };
1717 static const CHAR_T zeroes[PADSIZE] =
1718 { L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'),
1719 L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0') };
1721 ssize_t
1722 #ifndef COMPILE_WPRINTF
1723 __printf_pad (FILE *s, char pad, size_t count)
1724 #else
1725 __wprintf_pad (FILE *s, wchar_t pad, size_t count)
1726 #endif
1728 const CHAR_T *padptr;
1729 register size_t i;
1731 padptr = pad == L_(' ') ? blanks : zeroes;
1733 for (i = count; i >= PADSIZE; i -= PADSIZE)
1734 if (PUT (s, padptr, PADSIZE) != PADSIZE)
1735 return -1;
1736 if (i > 0)
1737 if (PUT (s, padptr, i) != i)
1738 return -1;
1740 return count;
1742 #undef PADSIZE
1743 #endif /* USE_IN_LIBIO */