update from main archive 961105
[glibc.git] / stdio-common / vfprintf.c
blobc2324d09a85ffb4aeb14f892ade89b6915fe3fdc
1 /* Copyright (C) 1991, 92, 93, 94, 95, 96 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 <libc-lock.h>
27 #include "_itoa.h"
28 #include "../locale/localeinfo.h"
30 /* This code is shared between the standard stdio implementation found
31 in GNU C library and the libio implementation originally found in
32 GNU libg++.
34 Beside this it is also shared between the normal and wide character
35 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
37 #ifndef COMPILE_WPRINTF
38 # define CHAR_T char
39 # define UCHAR_T unsigned char
40 # define INT_T int
41 # define L_(Str) Str
42 # define ISDIGIT(Ch) isdigit (Ch)
44 # ifdef USE_IN_LIBIO
45 # define PUT(F, S, N) _IO_sputn (F, S, N)
46 # define PAD(Padchar) \
47 if (width > 0) \
48 done += _IO_padn (s, Padchar, width)
49 # else
50 # define PUTC(C, F) putc (C, F)
51 ssize_t __printf_pad __P ((FILE *, char pad, size_t n));
52 # define PAD(Padchar) \
53 if (width > 0) \
54 { if (__printf_pad (s, Padchar, width) == -1) \
55 return -1; else done += width; }
56 # endif
57 #else
58 # define vfprintf vfwprintf
59 # define CHAR_T wchar_t
60 # define UCHAR_T uwchar_t
61 # define INT_T wint_t
62 # define L_(Str) L##Str
63 # define ISDIGIT(Ch) iswdigit (Ch)
65 # ifdef USE_IN_LIBIO
66 # define PUT(F, S, N) _IO_sputn (F, S, N)
67 # define PAD(Padchar) \
68 if (width > 0) \
69 done += _IO_wpadn (s, Padchar, width)
70 # else
71 # define PUTC(C, F) wputc (C, F)
72 ssize_t __wprintf_pad __P ((FILE *, wchar_t pad, size_t n));
73 # define PAD(Padchar) \
74 if (width > 0) \
75 { if (__wprintf_pad (s, Padchar, width) == -1) \
76 return -1; else done += width; }
77 # endif
78 #endif
80 /* Include the shared code for parsing the format string. */
81 #include "printf-parse.h"
84 #ifdef USE_IN_LIBIO
85 /* This code is for use in libio. */
86 # include <libioP.h>
87 # define PUTC(C, F) _IO_putc_unlocked (C, F)
88 # define vfprintf _IO_vfprintf
89 # define FILE _IO_FILE
90 # undef va_list
91 # define va_list _IO_va_list
92 # undef BUFSIZ
93 # define BUFSIZ _IO_BUFSIZ
94 # define ARGCHECK(S, Format) \
95 do \
96 { \
97 /* Check file argument for consistence. */ \
98 CHECK_FILE (S, -1); \
99 if (S->_flags & _IO_NO_WRITES || Format == NULL) \
101 MAYBE_SET_EINVAL; \
102 return -1; \
104 } while (0)
105 # define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
106 #else /* ! USE_IN_LIBIO */
107 /* This code is for use in the GNU C library. */
108 # include <stdio.h>
109 # define PUT(F, S, N) fwrite (S, 1, N, F)
110 # define ARGCHECK(S, Format) \
111 do \
113 /* Check file argument for consistence. */ \
114 if (!__validfp(S) || !S->__mode.__write || Format == NULL) \
116 __set_errno (EINVAL); \
117 return -1; \
119 if (!S->__seen) \
121 if (__flshfp (S, EOF) == EOF) \
122 return -1; \
125 while (0)
126 # define UNBUFFERED_P(s) ((s)->__buffer == NULL)
128 /* XXX These declarations should go as soon as the stdio header files
129 have these prototypes. */
130 extern void __flockfile (FILE *);
131 extern void __funlockfile (FILE *);
132 #endif /* USE_IN_LIBIO */
135 #define outchar(Ch) \
136 do \
138 register const int outc = (Ch); \
139 if (PUTC (outc, s) == EOF) \
140 return -1; \
141 else \
142 ++done; \
144 while (0)
146 #define outstring(String, Len) \
147 do \
149 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
150 return -1; \
151 done += (Len); \
153 while (0)
155 /* For handling long_double and longlong we use the same flag. */
156 #ifndef is_longlong
157 # define is_longlong is_long_double
158 #endif
161 /* Global variables. */
162 static const char null[] = "(null)";
165 /* Helper function to provide temporary buffering for unbuffered streams. */
166 static int buffered_vfprintf __P ((FILE *stream, const CHAR_T *fmt, va_list));
168 /* Handle unknown format specifier. */
169 static int printf_unknown __P ((FILE *, const struct printf_info *,
170 const void *const *));
172 /* Group digits of number string. */
173 static char *group_number __P ((CHAR_T *, CHAR_T *, const CHAR_T *, wchar_t));
176 /* The function itself. */
178 vfprintf (FILE *s, const CHAR_T *format, va_list ap)
180 /* The character used as thousands separator. */
181 wchar_t thousands_sep;
183 /* The string describing the size of groups of digits. */
184 const char *grouping;
186 /* Place to accumulate the result. */
187 int done;
189 /* Current character in format string. */
190 const UCHAR_T *f;
192 /* End of leading constant string. */
193 const UCHAR_T *lead_str_end;
195 /* Points to next format specifier. */
196 const UCHAR_T *end_of_spec;
198 /* Buffer intermediate results. */
199 char work_buffer[1000];
200 #define workend (&work_buffer[sizeof (work_buffer) - 1])
202 /* State for restartable multibyte character handling functions. */
203 mbstate_t mbstate;
205 /* We have to save the original argument pointer. */
206 va_list ap_save;
208 /* Count number of specifiers we already processed. */
209 int nspecs_done;
212 /* This table maps a character into a number representing a
213 class. In each step there is a destination label for each
214 class. */
215 static const int jump_table[] =
217 /* ' ' */ 1, 0, 0, /* '#' */ 4,
218 0, /* '%' */ 14, 0, /* '\''*/ 6,
219 0, 0, /* '*' */ 7, /* '+' */ 2,
220 0, /* '-' */ 3, /* '.' */ 9, 0,
221 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
222 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
223 /* '8' */ 8, /* '9' */ 8, 0, 0,
224 0, 0, 0, 0,
225 0, 0, 0, /* 'C' */ 25,
226 0, /* 'E' */ 19, 0, /* 'G' */ 19,
227 0, 0, 0, 0,
228 /* 'L' */ 12, 0, 0, 0,
229 0, 0, 0, /* 'S' */ 21,
230 0, 0, 0, 0,
231 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
232 0, 0, 0, 0,
233 0, 0, 0, /* 'c' */ 20,
234 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
235 /* 'h' */ 10, /* 'i' */ 15, 0, 0,
236 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
237 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
238 0, /* 'u' */ 16, 0, 0,
239 /* 'x' */ 18
242 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < ' ' || (Ch) > 'x')
243 #define CHAR_CLASS(Ch) (jump_table[(int) (Ch) - ' '])
244 #define JUMP(ChExpr, table) \
245 do \
247 const void *ptr; \
248 spec = (ChExpr); \
249 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
250 : table[CHAR_CLASS (spec)]; \
251 goto *ptr; \
253 while (0)
255 #define STEP0_3_TABLE \
256 /* Step 0: at the beginning. */ \
257 static const void *step0_jumps[26] = \
259 REF (form_unknown), \
260 REF (flag_space), /* for ' ' */ \
261 REF (flag_plus), /* for '+' */ \
262 REF (flag_minus), /* for '-' */ \
263 REF (flag_hash), /* for '<hash>' */ \
264 REF (flag_zero), /* for '0' */ \
265 REF (flag_quote), /* for '\'' */ \
266 REF (width_asterics), /* for '*' */ \
267 REF (width), /* for '1'...'9' */ \
268 REF (precision), /* for '.' */ \
269 REF (mod_half), /* for 'h' */ \
270 REF (mod_long), /* for 'l' */ \
271 REF (mod_longlong), /* for 'L', 'q' */ \
272 REF (mod_size_t), /* for 'Z' */ \
273 REF (form_percent), /* for '%' */ \
274 REF (form_integer), /* for 'd', 'i' */ \
275 REF (form_unsigned), /* for 'u' */ \
276 REF (form_octal), /* for 'o' */ \
277 REF (form_hexa), /* for 'X', 'x' */ \
278 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
279 REF (form_character), /* for 'c' */ \
280 REF (form_string), /* for 's', 'S' */ \
281 REF (form_pointer), /* for 'p' */ \
282 REF (form_number), /* for 'n' */ \
283 REF (form_strerror), /* for 'm' */ \
284 REF (form_wcharacter) /* for 'C' */ \
285 }; \
286 /* Step 1: after processing width. */ \
287 static const void *step1_jumps[26] = \
289 REF (form_unknown), \
290 REF (form_unknown), /* for ' ' */ \
291 REF (form_unknown), /* for '+' */ \
292 REF (form_unknown), /* for '-' */ \
293 REF (form_unknown), /* for '<hash>' */ \
294 REF (form_unknown), /* for '0' */ \
295 REF (form_unknown), /* for '\'' */ \
296 REF (form_unknown), /* for '*' */ \
297 REF (form_unknown), /* for '1'...'9' */ \
298 REF (precision), /* for '.' */ \
299 REF (mod_half), /* for 'h' */ \
300 REF (mod_long), /* for 'l' */ \
301 REF (mod_longlong), /* for 'L', 'q' */ \
302 REF (mod_size_t), /* for 'Z' */ \
303 REF (form_percent), /* for '%' */ \
304 REF (form_integer), /* for 'd', 'i' */ \
305 REF (form_unsigned), /* for 'u' */ \
306 REF (form_octal), /* for 'o' */ \
307 REF (form_hexa), /* for 'X', 'x' */ \
308 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
309 REF (form_character), /* for 'c' */ \
310 REF (form_string), /* for 's', 'S' */ \
311 REF (form_pointer), /* for 'p' */ \
312 REF (form_number), /* for 'n' */ \
313 REF (form_strerror), /* for 'm' */ \
314 REF (form_wcharacter) /* for 'C' */ \
315 }; \
316 /* Step 2: after processing precision. */ \
317 static const void *step2_jumps[26] = \
319 REF (form_unknown), \
320 REF (form_unknown), /* for ' ' */ \
321 REF (form_unknown), /* for '+' */ \
322 REF (form_unknown), /* for '-' */ \
323 REF (form_unknown), /* for '<hash>' */ \
324 REF (form_unknown), /* for '0' */ \
325 REF (form_unknown), /* for '\'' */ \
326 REF (form_unknown), /* for '*' */ \
327 REF (form_unknown), /* for '1'...'9' */ \
328 REF (form_unknown), /* for '.' */ \
329 REF (mod_half), /* for 'h' */ \
330 REF (mod_long), /* for 'l' */ \
331 REF (mod_longlong), /* for 'L', 'q' */ \
332 REF (mod_size_t), /* for 'Z' */ \
333 REF (form_percent), /* for '%' */ \
334 REF (form_integer), /* for 'd', 'i' */ \
335 REF (form_unsigned), /* for 'u' */ \
336 REF (form_octal), /* for 'o' */ \
337 REF (form_hexa), /* for 'X', 'x' */ \
338 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
339 REF (form_character), /* for 'c' */ \
340 REF (form_string), /* for 's', 'S' */ \
341 REF (form_pointer), /* for 'p' */ \
342 REF (form_number), /* for 'n' */ \
343 REF (form_strerror), /* for 'm' */ \
344 REF (form_wcharacter) /* for 'C' */ \
345 }; \
346 /* Step 3: after processing first 'l' modifier. */ \
347 static const void *step3_jumps[26] = \
349 REF (form_unknown), \
350 REF (form_unknown), /* for ' ' */ \
351 REF (form_unknown), /* for '+' */ \
352 REF (form_unknown), /* for '-' */ \
353 REF (form_unknown), /* for '<hash>' */ \
354 REF (form_unknown), /* for '0' */ \
355 REF (form_unknown), /* for '\'' */ \
356 REF (form_unknown), /* for '*' */ \
357 REF (form_unknown), /* for '1'...'9' */ \
358 REF (form_unknown), /* for '.' */ \
359 REF (form_unknown), /* for 'h' */ \
360 REF (mod_longlong), /* for 'l' */ \
361 REF (form_unknown), /* for 'L', 'q' */ \
362 REF (form_unknown), /* for 'Z' */ \
363 REF (form_percent), /* for '%' */ \
364 REF (form_integer), /* for 'd', 'i' */ \
365 REF (form_unsigned), /* for 'u' */ \
366 REF (form_octal), /* for 'o' */ \
367 REF (form_hexa), /* for 'X', 'x' */ \
368 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
369 REF (form_character), /* for 'c' */ \
370 REF (form_string), /* for 's', 'S' */ \
371 REF (form_pointer), /* for 'p' */ \
372 REF (form_number), /* for 'n' */ \
373 REF (form_strerror), /* for 'm' */ \
374 REF (form_wcharacter) /* for 'C' */ \
377 #define STEP4_TABLE \
378 /* Step 4: processing format specifier. */ \
379 static const void *step4_jumps[26] = \
381 REF (form_unknown), \
382 REF (form_unknown), /* for ' ' */ \
383 REF (form_unknown), /* for '+' */ \
384 REF (form_unknown), /* for '-' */ \
385 REF (form_unknown), /* for '<hash>' */ \
386 REF (form_unknown), /* for '0' */ \
387 REF (form_unknown), /* for '\'' */ \
388 REF (form_unknown), /* for '*' */ \
389 REF (form_unknown), /* for '1'...'9' */ \
390 REF (form_unknown), /* for '.' */ \
391 REF (form_unknown), /* for 'h' */ \
392 REF (form_unknown), /* for 'l' */ \
393 REF (form_unknown), /* for 'L', 'q' */ \
394 REF (form_unknown), /* for 'Z' */ \
395 REF (form_percent), /* for '%' */ \
396 REF (form_integer), /* for 'd', 'i' */ \
397 REF (form_unsigned), /* for 'u' */ \
398 REF (form_octal), /* for 'o' */ \
399 REF (form_hexa), /* for 'X', 'x' */ \
400 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
401 REF (form_character), /* for 'c' */ \
402 REF (form_string), /* for 's', 'S' */ \
403 REF (form_pointer), /* for 'p' */ \
404 REF (form_number), /* for 'n' */ \
405 REF (form_strerror), /* for 'm' */ \
406 REF (form_wcharacter) /* for 'C' */ \
410 #define process_arg(fspec) \
411 /* Start real work. We know about all flags and modifiers and \
412 now process the wanted format specifier. */ \
413 LABEL (form_percent): \
414 /* Write a literal "%". */ \
415 outchar ('%'); \
416 break; \
418 LABEL (form_integer): \
419 /* Signed decimal integer. */ \
420 base = 10; \
422 if (is_longlong) \
424 long long int signed_number; \
426 if (fspec == NULL) \
427 signed_number = va_arg (ap, long long int); \
428 else \
429 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
431 is_negative = signed_number < 0; \
432 number.longlong = is_negative ? (- signed_number) : signed_number; \
434 goto LABEL (longlong_number); \
436 else \
438 long int signed_number; \
440 if (fspec == NULL) \
441 if (is_long) \
442 signed_number = va_arg (ap, long int); \
443 else /* `short int' will be promoted to `int'. */ \
444 signed_number = va_arg (ap, int); \
445 else \
446 if (is_long) \
447 signed_number = args_value[fspec->data_arg].pa_long_int; \
448 else \
449 signed_number = args_value[fspec->data_arg].pa_int; \
451 is_negative = signed_number < 0; \
452 number.word = is_negative ? (- signed_number) : signed_number; \
454 goto LABEL (number); \
456 /* NOTREACHED */ \
458 LABEL (form_unsigned): \
459 /* Unsigned decimal integer. */ \
460 base = 10; \
461 goto LABEL (unsigned_number); \
462 /* NOTREACHED */ \
464 LABEL (form_octal): \
465 /* Unsigned octal integer. */ \
466 base = 8; \
467 goto LABEL (unsigned_number); \
468 /* NOTREACHED */ \
470 LABEL (form_hexa): \
471 /* Unsigned hexadecimal integer. */ \
472 base = 16; \
474 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
476 /* ANSI specifies the `+' and ` ' flags only for signed \
477 conversions. */ \
478 is_negative = 0; \
479 showsign = 0; \
480 space = 0; \
482 if (is_longlong) \
484 if (fspec == NULL) \
485 number.longlong = va_arg (ap, unsigned long long int); \
486 else \
487 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
489 LABEL (longlong_number): \
490 if (prec < 0) \
491 /* Supply a default precision if none was given. */ \
492 prec = 1; \
493 else \
494 /* We have to take care for the '0' flag. If a precision \
495 is given it must be ignored. */ \
496 pad = ' '; \
498 /* If the precision is 0 and the number is 0 nothing has to \
499 be written for the number. */ \
500 if (prec == 0 && number.longlong == 0) \
501 string = workend; \
502 else \
504 /* Put the number in WORK. */ \
505 string = _itoa (number.longlong, workend + 1, base, \
506 spec == 'X'); \
507 string -= 1; \
508 if (group && grouping) \
509 string = group_number (string, workend, grouping, \
510 thousands_sep); \
512 /* Simply further test for num != 0. */ \
513 number.word = number.longlong != 0; \
515 else \
517 if (fspec == NULL) \
518 if (is_long) \
519 number.word = va_arg (ap, unsigned long int); \
520 else if (!is_short) \
521 number.word = va_arg (ap, unsigned int); \
522 else \
523 number.word = (unsigned short int) va_arg (ap, unsigned int); \
524 else \
525 if (is_long) \
526 number.word = args_value[fspec->data_arg].pa_u_long_int; \
527 else if (!is_short) \
528 number.word = args_value[fspec->data_arg].pa_u_int; \
529 else \
530 number.word = (unsigned short int) \
531 args_value[fspec->data_arg].pa_u_short_int; \
533 LABEL (number): \
534 if (prec < 0) \
535 /* Supply a default precision if none was given. */ \
536 prec = 1; \
537 else \
538 /* We have to take care for the '0' flag. If a precision \
539 is given it must be ignored. */ \
540 pad = ' '; \
542 /* If the precision is 0 and the number is 0 nothing has to \
543 be written for the number. */ \
544 if (prec == 0 && number.word == 0) \
545 string = workend; \
546 else \
548 /* Put the number in WORK. */ \
549 string = _itoa_word (number.word, workend + 1, base, \
550 spec == 'X'); \
551 string -= 1; \
552 if (group && grouping) \
553 string = group_number (string, workend, grouping, \
554 thousands_sep); \
558 prec -= workend - string; \
560 if (prec > 0) \
561 /* Add zeros to the precision. */ \
562 while (prec-- > 0) \
563 *string-- = '0'; \
564 else if (number.word != 0 && alt && base == 8) \
565 /* Add octal marker. */ \
566 *string-- = '0'; \
568 if (!left) \
570 width -= workend - string; \
572 if (number.word != 0 && alt && base == 16) \
573 /* Account for 0X hex marker. */ \
574 width -= 2; \
576 if (is_negative || showsign || space) \
577 --width; \
579 if (pad == '0') \
581 while (width-- > 0) \
582 *string-- = '0'; \
584 if (number.word != 0 && alt && base == 16) \
586 *string-- = spec; \
587 *string-- = '0'; \
590 if (is_negative) \
591 *string-- = '-'; \
592 else if (showsign) \
593 *string-- = '+'; \
594 else if (space) \
595 *string-- = ' '; \
597 else \
599 if (number.word != 0 && alt && base == 16) \
601 *string-- = spec; \
602 *string-- = '0'; \
605 if (is_negative) \
606 *string-- = '-'; \
607 else if (showsign) \
608 *string-- = '+'; \
609 else if (space) \
610 *string-- = ' '; \
612 while (width-- > 0) \
613 *string-- = ' '; \
616 outstring (string + 1, workend - string); \
618 break; \
620 else \
622 if (number.word != 0 && alt && base == 16) \
624 *string-- = spec; \
625 *string-- = '0'; \
628 if (is_negative) \
629 *string-- = '-'; \
630 else if (showsign) \
631 *string-- = '+'; \
632 else if (space) \
633 *string-- = ' '; \
635 width -= workend - string; \
636 outstring (string + 1, workend - string); \
638 PAD (' '); \
639 break; \
642 LABEL (form_float): \
644 /* Floating-point number. This is handled by printf_fp.c. */ \
645 extern int __printf_fp __P ((FILE *, const struct printf_info *, \
646 const void **const)); \
647 const void *ptr; \
648 int function_done; \
650 if (fspec == NULL) \
652 struct printf_info info = { prec: prec, \
653 width: width, \
654 spec: spec, \
655 is_long_double: is_long_double, \
656 is_short: is_short, \
657 is_long: is_long, \
658 alt: alt, \
659 space: space, \
660 left: left, \
661 showsign: showsign, \
662 group: group, \
663 pad: pad, \
664 extra: 0 }; \
666 if (is_long_double) \
667 the_arg.pa_long_double = va_arg (ap, long double); \
668 else \
669 the_arg.pa_double = va_arg (ap, double); \
670 ptr = (const void *) &the_arg; \
672 function_done = __printf_fp (s, &info, &ptr); \
674 else \
676 ptr = (const void *) &args_value[fspec->data_arg]; \
678 function_done = __printf_fp (s, &fspec->info, &ptr); \
681 if (function_done < 0) \
682 /* Error in print handler. */ \
683 return -1; \
685 done += function_done; \
687 break; \
689 LABEL (form_character): \
690 /* Character. */ \
691 if (is_long) \
692 goto LABEL (form_wcharacter); \
693 --width; /* Account for the character itself. */ \
694 if (!left) \
695 PAD (' '); \
696 if (fspec == NULL) \
697 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
698 else \
699 outchar ((unsigned char) args_value[fspec->data_arg].pa_char); \
700 if (left) \
701 PAD (' '); \
702 break; \
704 LABEL (form_wcharacter): \
706 /* Wide character. */ \
707 char buf[MB_CUR_MAX]; \
708 mbstate_t mbstate; \
709 size_t len; \
711 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wint_t) \
712 : args_value[fspec->data_arg].pa_wchar), \
713 &mbstate); \
714 width -= len; \
715 if (!left) \
716 PAD (' '); \
717 outstring (buf, len); \
718 if (left) \
719 PAD (' '); \
721 break; \
723 LABEL (form_string): \
725 size_t len; \
727 /* The string argument could in fact be `char *' or `wchar_t *'. \
728 But this should not make a difference here. */ \
729 if (fspec == NULL) \
730 string = (char *) va_arg (ap, const char *); \
731 else \
732 string = (char *) args_value[fspec->data_arg].pa_string; \
734 /* Entry point for printing other strings. */ \
735 LABEL (print_string): \
737 if (string == NULL) \
739 /* Write "(null)" if there's space. */ \
740 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
742 string = (char *) null; \
743 len = sizeof (null) - 1; \
745 else \
747 string = (char *) ""; \
748 len = 0; \
751 else if (!is_long && spec != L_('S')) \
753 if (prec != -1) \
754 /* Search for the end of the string, but don't search past \
755 the length specified by the precision. */ \
756 len = strnlen (string, prec); \
757 else \
758 len = strlen (string); \
760 else \
762 const wchar_t *s2 = (const wchar_t *) string; \
763 mbstate_t mbstate; \
765 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
766 if (len == (size_t) -1) \
767 /* Illegal wide-character string. */ \
768 return -1; \
770 s2 = (const wchar_t *) string; \
771 string = alloca (len + 1); \
772 (void) __wcsrtombs (string, &s2, prec != -1 ? prec : UINT_MAX, \
773 &mbstate); \
776 if ((width -= len) < 0) \
778 outstring (string, len); \
779 break; \
782 if (!left) \
783 PAD (' '); \
784 outstring (string, len); \
785 if (left) \
786 PAD (' '); \
788 break; \
790 LABEL (form_pointer): \
791 /* Generic pointer. */ \
793 const void *ptr; \
794 if (fspec == NULL) \
795 ptr = va_arg (ap, void *); \
796 else \
797 ptr = args_value[fspec->data_arg].pa_pointer; \
798 if (ptr != NULL) \
800 /* If the pointer is not NULL, write it as a %#x spec. */ \
801 base = 16; \
802 number.word = (unsigned long int) ptr; \
803 is_negative = 0; \
804 alt = 1; \
805 group = 0; \
806 spec = 'x'; \
807 goto LABEL (number); \
809 else \
811 /* Write "(nil)" for a nil pointer. */ \
812 string = (char *) "(nil)"; \
813 /* Make sure the full string "(nil)" is printed. */ \
814 if (prec < 5) \
815 prec = 5; \
816 is_long = 0; /* This is no wide-char string. */ \
817 goto LABEL (print_string); \
820 /* NOTREACHED */ \
822 LABEL (form_number): \
823 /* Answer the count of characters written. */ \
824 if (fspec == NULL) \
825 if (is_longlong) \
826 *(long long int *) va_arg (ap, void *) = done; \
827 else if (is_long) \
828 *(long int *) va_arg (ap, void *) = done; \
829 else if (!is_short) \
830 *(int *) va_arg (ap, void *) = done; \
831 else \
832 *(short int *) va_arg (ap, void *) = done; \
833 else \
834 if (is_longlong) \
835 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
836 else if (is_long) \
837 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
838 else if (!is_short) \
839 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
840 else \
841 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
842 break; \
844 LABEL (form_strerror): \
845 /* Print description of error ERRNO. */ \
847 extern char *_strerror_internal __P ((int, char *buf, size_t)); \
849 string = (char *) \
850 _strerror_internal (errno, work_buffer, sizeof work_buffer); \
852 is_long = 0; /* This is no wide-char string. */ \
853 goto LABEL (print_string)
856 /* Sanity check of arguments. */
857 ARGCHECK (s, format);
859 if (UNBUFFERED_P (s))
860 /* Use a helper function which will allocate a local temporary buffer
861 for the stream and then call us again. */
862 return buffered_vfprintf (s, format, ap);
864 /* Initialize local variables. */
865 done = 0;
866 grouping = (const char *) -1;
867 ap_save = ap;
868 nspecs_done = 0;
870 /* Find the first format specifier. */
871 f = lead_str_end = find_spec (format, &mbstate);
873 /* Lock stream. */
874 #ifdef USE_IN_LIBIO
875 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
876 _IO_flockfile (s);
877 #else
878 __libc_cleanup_region_start ((void (*) (void *)) &__funlockfile, s);
879 __flockfile (s);
880 #endif
882 /* Write the literal text before the first format. */
883 outstring ((const UCHAR_T *) format,
884 lead_str_end - (const UCHAR_T *) format);
886 /* If we only have to print a simple string, return now. */
887 if (*f == L_('\0'))
888 goto all_done;
890 /* Process whole format string. */
893 #define REF(Name) &&do_##Name
894 #define LABEL(Name) do_##Name
895 STEP0_3_TABLE;
896 STEP4_TABLE;
898 union printf_arg *args_value; /* This is not used here but ... */
899 int is_negative; /* Flag for negative number. */
900 union
902 unsigned long long int longlong;
903 unsigned long int word;
904 } number;
905 int base;
906 union printf_arg the_arg;
907 char *string; /* Pointer to argument string. */
908 int alt = 0; /* Alternate format. */
909 int space = 0; /* Use space prefix if no sign is needed. */
910 int left = 0; /* Left-justify output. */
911 int showsign = 0; /* Always begin with plus or minus sign. */
912 int group = 0; /* Print numbers according grouping rules. */
913 int is_long_double = 0; /* Argument is long double/ long long int. */
914 int is_short = 0; /* Argument is long int. */
915 int is_long = 0; /* Argument is short int. */
916 int width = 0; /* Width of output; 0 means none specified. */
917 int prec = -1; /* Precision of output; -1 means none specified. */
918 char pad = ' '; /* Padding character. */
919 CHAR_T spec;
921 /* Get current character in format string. */
922 JUMP (*++f, step0_jumps);
924 /* ' ' flag. */
925 LABEL (flag_space):
926 space = 1;
927 JUMP (*++f, step0_jumps);
929 /* '+' flag. */
930 LABEL (flag_plus):
931 showsign = 1;
932 JUMP (*++f, step0_jumps);
934 /* The '-' flag. */
935 LABEL (flag_minus):
936 left = 1;
937 pad = L_(' ');
938 JUMP (*++f, step0_jumps);
940 /* The '#' flag. */
941 LABEL (flag_hash):
942 alt = 1;
943 JUMP (*++f, step0_jumps);
945 /* The '0' flag. */
946 LABEL (flag_zero):
947 if (!left)
948 pad = L_('0');
949 JUMP (*++f, step0_jumps);
951 /* The '\'' flag. */
952 LABEL (flag_quote):
953 group = 1;
955 /* XXX Completely wrong. Use wctob. */
956 if (grouping == (const char *) -1)
958 /* Figure out the thousands separator character. */
959 if (mbtowc (&thousands_sep,
960 _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
961 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
962 thousands_sep = (wchar_t)
963 *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
964 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
965 if (*grouping == '\0' || *grouping == CHAR_MAX
966 || thousands_sep == L'\0')
967 grouping = NULL;
969 JUMP (*++f, step0_jumps);
971 /* Get width from argument. */
972 LABEL (width_asterics):
974 const UCHAR_T *tmp; /* Temporary value. */
976 tmp = ++f;
977 if (ISDIGIT (*tmp) && read_int (&tmp) && *tmp == L_('$'))
978 /* The width comes from a positional parameter. */
979 goto do_positional;
981 width = va_arg (ap, int);
983 /* Negative width means left justified. */
984 if (width < 0)
986 width = -width;
987 pad = L_(' ');
988 left = 1;
991 JUMP (*f, step1_jumps);
993 /* Given width in format string. */
994 LABEL (width):
995 width = read_int (&f);
996 if (*f == L_('$'))
997 /* Oh, oh. The argument comes from a positional parameter. */
998 goto do_positional;
999 JUMP (*f, step1_jumps);
1001 LABEL (precision):
1002 ++f;
1003 if (*f == L_('*'))
1005 const UCHAR_T *tmp; /* Temporary value. */
1007 tmp = ++f;
1008 if (ISDIGIT (*tmp) && read_int (&tmp) > 0 && *tmp == L_('$'))
1009 /* The precision comes from a positional parameter. */
1010 goto do_positional;
1012 prec = va_arg (ap, int);
1014 /* If the precision is negative the precision is omitted. */
1015 if (prec < 0)
1016 prec = -1;
1018 else if (ISDIGIT (*f))
1019 prec = read_int (&f);
1020 else
1021 prec = 0;
1022 JUMP (*f, step2_jumps);
1024 /* Process 'h' modifier. No other modifier is allowed to
1025 follow. */
1026 LABEL (mod_half):
1027 is_short = 1;
1028 JUMP (*++f, step4_jumps);
1030 /* Process 'l' modifier. There might another 'l' follow. */
1031 LABEL (mod_long):
1032 is_long = 1;
1033 JUMP (*++f, step3_jumps);
1035 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1036 allowed to follow. */
1037 LABEL (mod_longlong):
1038 is_long_double = 1;
1039 JUMP (*++f, step4_jumps);
1041 LABEL (mod_size_t):
1042 is_longlong = sizeof (size_t) > sizeof (unsigned long int);
1043 is_long = sizeof (size_t) > sizeof (unsigned int);
1044 JUMP (*++f, step4_jumps);
1047 /* Process current format. */
1048 while (1)
1050 process_arg (((struct printf_spec *) NULL));
1052 LABEL (form_unknown):
1053 if (spec == L_('\0'))
1055 /* The format string ended before the specifier is complete. */
1056 done = -1;
1057 goto all_done;
1060 /* If we are in the fast loop force entering the complicated
1061 one. */
1062 goto do_positional;
1065 /* Look for next format specifier. */
1066 f = find_spec ((end_of_spec = ++f), &mbstate);
1068 /* Write the following constant string. */
1069 outstring (end_of_spec, f - end_of_spec);
1071 while (*f != L_('\0'));
1073 /* Unlock stream and return. */
1074 goto all_done;
1076 /* Here starts the more complex loop to handle positional parameters. */
1077 do_positional:
1079 /* Array with information about the needed arguments. This has to
1080 be dynamically extendable. */
1081 size_t nspecs = 0;
1082 size_t nspecs_max = 32; /* A more or less arbitrary start value. */
1083 struct printf_spec *specs
1084 = alloca (nspecs_max * sizeof (struct printf_spec));
1086 /* The number of arguments the format string requests. This will
1087 determine the size of the array needed to store the argument
1088 attributes. */
1089 size_t nargs = 0;
1090 int *args_type;
1091 union printf_arg *args_value;
1093 /* Positional parameters refer to arguments directly. This could
1094 also determine the maximum number of arguments. Track the
1095 maximum number. */
1096 size_t max_ref_arg = 0;
1098 /* Just a counter. */
1099 size_t cnt;
1102 if (grouping == (const char *) -1)
1104 /* XXX Use wctob. But this is incompatible for now. */
1105 /* Figure out the thousands separator character. */
1106 if (mbtowc (&thousands_sep,
1107 _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
1108 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
1109 thousands_sep = (wchar_t) *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1110 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1111 if (*grouping == '\0' || *grouping == CHAR_MAX
1112 || thousands_sep == L'\0')
1113 grouping = NULL;
1116 for (f = lead_str_end; *f != '\0'; f = specs[nspecs++].next_fmt)
1118 if (nspecs >= nspecs_max)
1120 /* Extend the array of format specifiers. */
1121 struct printf_spec *old = specs;
1123 nspecs_max *= 2;
1124 specs = alloca (nspecs_max * sizeof (struct printf_spec));
1126 if (specs == &old[nspecs])
1127 /* Stack grows up, OLD was the last thing allocated;
1128 extend it. */
1129 nspecs_max += nspecs_max / 2;
1130 else
1132 /* Copy the old array's elements to the new space. */
1133 memcpy (specs, old, nspecs * sizeof (struct printf_spec));
1134 if (old == &specs[nspecs])
1135 /* Stack grows down, OLD was just below the new
1136 SPECS. We can use that space when the new space
1137 runs out. */
1138 nspecs_max += nspecs_max / 2;
1142 /* Parse the format specifier. */
1143 nargs += parse_one_spec (f, nargs, &specs[nspecs], &max_ref_arg,
1144 &mbstate);
1147 /* Determine the number of arguments the format string consumes. */
1148 nargs = MAX (nargs, max_ref_arg);
1150 /* Allocate memory for the argument descriptions. */
1151 args_type = alloca (nargs * sizeof (int));
1152 memset (args_type, 0, nargs * sizeof (int));
1153 args_value = alloca (nargs * sizeof (union printf_arg));
1155 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1156 still zero after this loop, format is invalid. For now we
1157 simply use 0 as the value. */
1159 /* Fill in the types of all the arguments. */
1160 for (cnt = 0; cnt < nspecs; ++cnt)
1162 /* If the width is determined by an argument this is an int. */
1163 if (specs[cnt].width_arg != -1)
1164 args_type[specs[cnt].width_arg] = PA_INT;
1166 /* If the precision is determined by an argument this is an int. */
1167 if (specs[cnt].prec_arg != -1)
1168 args_type[specs[cnt].prec_arg] = PA_INT;
1170 switch (specs[cnt].ndata_args)
1172 case 0: /* No arguments. */
1173 break;
1174 case 1: /* One argument; we already have the type. */
1175 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1176 break;
1177 default:
1178 /* We have more than one argument for this format spec.
1179 We must call the arginfo function again to determine
1180 all the types. */
1181 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1182 (&specs[cnt].info,
1183 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg]);
1184 break;
1188 /* Now we know all the types and the order. Fill in the argument
1189 values. */
1190 for (cnt = 0, ap = ap_save; cnt < nargs; ++cnt)
1191 switch (args_type[cnt])
1193 #define T(tag, mem, type) \
1194 case tag: \
1195 args_value[cnt].mem = va_arg (ap, type); \
1196 break
1198 T (PA_CHAR, pa_char, int); /* Promoted. */
1199 T (PA_WCHAR, pa_wchar, wint_t);
1200 T (PA_INT|PA_FLAG_SHORT, pa_short_int, int); /* Promoted. */
1201 T (PA_INT, pa_int, int);
1202 T (PA_INT|PA_FLAG_LONG, pa_long_int, long int);
1203 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1204 T (PA_FLOAT, pa_float, double); /* Promoted. */
1205 T (PA_DOUBLE, pa_double, double);
1206 T (PA_DOUBLE|PA_FLAG_LONG_DOUBLE, pa_long_double, long double);
1207 T (PA_STRING, pa_string, const char *);
1208 T (PA_WSTRING, pa_wstring, const wchar_t *);
1209 T (PA_POINTER, pa_pointer, void *);
1210 #undef T
1211 default:
1212 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1213 args_value[cnt].pa_pointer = va_arg (ap, void *);
1214 else
1215 args_value[cnt].pa_long_double = 0.0;
1216 break;
1219 /* Now walk through all format specifiers and process them. */
1220 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1222 #undef REF
1223 #define REF(Name) &&do2_##Name
1224 #undef LABEL
1225 #define LABEL(Name) do2_##Name
1226 STEP4_TABLE;
1228 int is_negative;
1229 union
1231 unsigned long long int longlong;
1232 unsigned long int word;
1233 } number;
1234 int base;
1235 union printf_arg the_arg;
1236 char *string; /* Pointer to argument string. */
1238 /* Fill variables from values in struct. */
1239 int alt = specs[nspecs_done].info.alt;
1240 int space = specs[nspecs_done].info.space;
1241 int left = specs[nspecs_done].info.left;
1242 int showsign = specs[nspecs_done].info.showsign;
1243 int group = specs[nspecs_done].info.group;
1244 int is_long_double = specs[nspecs_done].info.is_long_double;
1245 int is_short = specs[nspecs_done].info.is_short;
1246 int is_long = specs[nspecs_done].info.is_long;
1247 int width = specs[nspecs_done].info.width;
1248 int prec = specs[nspecs_done].info.prec;
1249 char pad = specs[nspecs_done].info.pad;
1250 CHAR_T spec = specs[nspecs_done].info.spec;
1252 /* Fill in last information. */
1253 if (specs[nspecs_done].width_arg != -1)
1255 /* Extract the field width from an argument. */
1256 specs[nspecs_done].info.width =
1257 args_value[specs[nspecs_done].width_arg].pa_int;
1259 if (specs[nspecs_done].info.width < 0)
1260 /* If the width value is negative left justification is
1261 selected and the value is taken as being positive. */
1263 specs[nspecs_done].info.width *= -1;
1264 left = specs[nspecs_done].info.left = 1;
1266 width = specs[nspecs_done].info.width;
1269 if (specs[nspecs_done].prec_arg != -1)
1271 /* Extract the precision from an argument. */
1272 specs[nspecs_done].info.prec =
1273 args_value[specs[nspecs_done].prec_arg].pa_int;
1275 if (specs[nspecs_done].info.prec < 0)
1276 /* If the precision is negative the precision is
1277 omitted. */
1278 specs[nspecs_done].info.prec = -1;
1280 prec = specs[nspecs_done].info.prec;
1283 /* Process format specifiers. */
1284 while (1)
1286 JUMP (spec, step4_jumps);
1288 process_arg ((&specs[nspecs_done]));
1290 LABEL (form_unknown):
1292 extern printf_function **__printf_function_table;
1293 int function_done;
1294 printf_function *function;
1295 unsigned int i;
1296 const void **ptr;
1298 function =
1299 (__printf_function_table == NULL ? NULL :
1300 __printf_function_table[specs[nspecs_done].info.spec]);
1302 if (function == NULL)
1303 function = &printf_unknown;
1305 ptr = alloca (specs[nspecs_done].ndata_args
1306 * sizeof (const void *));
1308 /* Fill in an array of pointers to the argument values. */
1309 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
1310 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1312 /* Call the function. */
1313 function_done = (*function) (s, &specs[nspecs_done].info, ptr);
1315 /* If an error occured we don't have information about #
1316 of chars. */
1317 if (function_done < 0)
1319 done = -1;
1320 goto all_done;
1323 done += function_done;
1325 break;
1328 /* Write the following constant string. */
1329 outstring (specs[nspecs_done].end_of_fmt,
1330 specs[nspecs_done].next_fmt
1331 - specs[nspecs_done].end_of_fmt);
1335 all_done:
1336 /* Unlock the stream. */
1337 __libc_cleanup_region_end (1);
1339 return done;
1342 #ifdef USE_IN_LIBIO
1343 # undef vfprintf
1344 # ifdef strong_alias
1345 /* This is for glibc. */
1346 strong_alias (_IO_vfprintf, vfprintf);
1347 # else
1348 # if defined __ELF__ || defined __GNU_LIBRARY__
1349 # include <gnu-stabs.h>
1350 # ifdef weak_alias
1351 weak_alias (_IO_vfprintf, vfprintf);
1352 # endif
1353 # endif
1354 # endif
1355 #endif
1357 /* Handle an unknown format specifier. This prints out a canonicalized
1358 representation of the format spec itself. */
1359 static int
1360 printf_unknown (FILE *s, const struct printf_info *info,
1361 const void *const *args)
1364 int done = 0;
1365 char work_buffer[BUFSIZ];
1366 register char *w;
1368 outchar ('%');
1370 if (info->alt)
1371 outchar ('#');
1372 if (info->group)
1373 outchar ('\'');
1374 if (info->showsign)
1375 outchar ('+');
1376 else if (info->space)
1377 outchar (' ');
1378 if (info->left)
1379 outchar ('-');
1380 if (info->pad == '0')
1381 outchar ('0');
1383 if (info->width != 0)
1385 w = _itoa_word (info->width, workend + 1, 10, 0);
1386 while (++w <= workend)
1387 outchar (*w);
1390 if (info->prec != -1)
1392 outchar ('.');
1393 w = _itoa_word (info->prec, workend + 1, 10, 0);
1394 while (++w <= workend)
1395 outchar (*w);
1398 if (info->spec != '\0')
1399 outchar (info->spec);
1401 return done;
1404 /* Group the digits according to the grouping rules of the current locale.
1405 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1406 static char *
1407 group_number (CHAR_T *w, CHAR_T *rear_ptr, const CHAR_T *grouping,
1408 wchar_t thousands_sep)
1410 int len;
1411 char *src, *s;
1413 /* We treat all negative values like CHAR_MAX. */
1415 if (*grouping == CHAR_MAX || *grouping < 0)
1416 /* No grouping should be done. */
1417 return w;
1419 len = *grouping;
1421 /* Copy existing string so that nothing gets overwritten. */
1422 src = (char *) alloca (rear_ptr - w);
1423 memcpy (src, w + 1, rear_ptr - w);
1424 s = &src[rear_ptr - w - 1];
1425 w = rear_ptr;
1427 /* Process all characters in the string. */
1428 while (s >= src)
1430 *w-- = *s--;
1432 if (--len == 0 && s >= src)
1434 /* A new group begins. */
1435 *w-- = thousands_sep;
1437 len = *grouping++;
1438 if (*grouping == '\0')
1439 /* The previous grouping repeats ad infinitum. */
1440 --grouping;
1441 else if (*grouping == CHAR_MAX || *grouping < 0)
1443 /* No further grouping to be done.
1444 Copy the rest of the number. */
1446 *w-- = *s--;
1447 while (s >= src);
1448 break;
1452 return w;
1455 #ifdef USE_IN_LIBIO
1456 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
1457 struct helper_file
1459 struct _IO_FILE_plus _f;
1460 _IO_FILE *_put_stream;
1461 #ifdef _IO_MTSAFE_IO
1462 _IO_lock_t lock;
1463 #endif
1466 static int
1467 _IO_helper_overflow (_IO_FILE *s, int c)
1469 _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
1470 int used = s->_IO_write_ptr - s->_IO_write_base;
1471 if (used)
1473 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
1474 s->_IO_write_ptr -= written;
1476 return PUTC (c, s);
1479 static const struct _IO_jump_t _IO_helper_jumps =
1481 JUMP_INIT_DUMMY,
1482 JUMP_INIT (finish, _IO_default_finish),
1483 JUMP_INIT (overflow, _IO_helper_overflow),
1484 JUMP_INIT (underflow, _IO_default_underflow),
1485 JUMP_INIT (uflow, _IO_default_uflow),
1486 JUMP_INIT (pbackfail, _IO_default_pbackfail),
1487 JUMP_INIT (xsputn, _IO_default_xsputn),
1488 JUMP_INIT (xsgetn, _IO_default_xsgetn),
1489 JUMP_INIT (seekoff, _IO_default_seekoff),
1490 JUMP_INIT (seekpos, _IO_default_seekpos),
1491 JUMP_INIT (setbuf, _IO_default_setbuf),
1492 JUMP_INIT (sync, _IO_default_sync),
1493 JUMP_INIT (doallocate, _IO_default_doallocate),
1494 JUMP_INIT (read, _IO_default_read),
1495 JUMP_INIT (write, _IO_default_write),
1496 JUMP_INIT (seek, _IO_default_seek),
1497 JUMP_INIT (close, _IO_default_close),
1498 JUMP_INIT (stat, _IO_default_stat)
1501 static int
1502 buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format,
1503 _IO_va_list args)
1505 char buf[_IO_BUFSIZ];
1506 struct helper_file helper;
1507 register _IO_FILE *hp = (_IO_FILE *) &helper;
1508 int result, to_flush;
1510 /* Initialize helper. */
1511 helper._put_stream = s;
1512 hp->_IO_write_base = buf;
1513 hp->_IO_write_ptr = buf;
1514 hp->_IO_write_end = buf + sizeof buf;
1515 hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS;
1516 #ifdef _IO_MTSAFE_IO
1517 hp->_lock = &helper.lock;
1518 #endif
1519 _IO_JUMPS (hp) = (struct _IO_jump_t *) &_IO_helper_jumps;
1521 /* Now print to helper instead. */
1522 result = _IO_vfprintf (hp, format, args);
1524 /* Now flush anything from the helper to the S. */
1525 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
1527 if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
1528 return -1;
1531 return result;
1534 #else /* !USE_IN_LIBIO */
1536 static int
1537 buffered_vfprintf (register FILE *s, const CHAR_T *format, va_list args)
1539 char buf[BUFSIZ];
1540 int result;
1542 s->__bufp = s->__buffer = buf;
1543 s->__bufsize = sizeof buf;
1544 s->__put_limit = s->__buffer + s->__bufsize;
1545 s->__get_limit = s->__buffer;
1547 /* Now use buffer to print. */
1548 result = vfprintf (s, format, args);
1550 if (fflush (s) == EOF)
1551 result = -1;
1552 s->__buffer = s->__bufp = s->__get_limit = s->__put_limit = NULL;
1553 s->__bufsize = 0;
1555 return result;
1558 /* Pads string with given number of a specified character.
1559 This code is taken from iopadn.c of the GNU I/O library. */
1560 #define PADSIZE 16
1561 static const CHAR_T blanks[PADSIZE] =
1562 { L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '),
1563 L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' ') };
1564 static const CHAR_T zeroes[PADSIZE] =
1565 { L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'),
1566 L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0') };
1568 ssize_t
1569 #ifndef COMPILE_WPRINTF
1570 __printf_pad (FILE *s, char pad, size_t count)
1571 #else
1572 __wprintf_pad (FILE *s, wchar_t pad, size_t count)
1573 #endif
1575 const CHAR_T *padptr;
1576 register size_t i;
1578 padptr = pad == L_(' ') ? blanks : zeroes;
1580 for (i = count; i >= PADSIZE; i -= PADSIZE)
1581 if (PUT (s, padptr, PADSIZE) != PADSIZE)
1582 return -1;
1583 if (i > 0)
1584 if (PUT (s, padptr, i) != i)
1585 return -1;
1587 return count;
1589 #undef PADSIZE
1590 #endif /* USE_IN_LIBIO */