Net interface definition
[glibc.git] / stdio-common / vfprintf.c
blob82348af2377dfc89754e4cc324272d1f3ceb9bc2
1 /* Copyright (C) 1991, 92, 93, 94, 95, 96, 97 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 <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));
181 /* Handle unknown format specifier. */
182 static int printf_unknown __P ((FILE *, const struct printf_info *,
183 const void *const *));
185 /* Group digits of number string. */
186 static char *group_number __P ((CHAR_T *, CHAR_T *, const CHAR_T *, wchar_t));
189 /* The function itself. */
191 vfprintf (FILE *s, const CHAR_T *format, va_list ap)
193 /* The character used as thousands separator. */
194 wchar_t thousands_sep;
196 /* The string describing the size of groups of digits. */
197 const char *grouping;
199 /* Place to accumulate the result. */
200 int done;
202 /* Current character in format string. */
203 const UCHAR_T *f;
205 /* End of leading constant string. */
206 const UCHAR_T *lead_str_end;
208 /* Points to next format specifier. */
209 const UCHAR_T *end_of_spec;
211 /* Buffer intermediate results. */
212 char work_buffer[1000];
213 #define workend (&work_buffer[sizeof (work_buffer) - 1])
215 /* State for restartable multibyte character handling functions. */
216 mbstate_t mbstate;
218 /* We have to save the original argument pointer. */
219 va_list ap_save;
221 /* Count number of specifiers we already processed. */
222 int nspecs_done;
224 /* For the %m format we may need the current `errno' value. */
225 int save_errno = errno;
228 /* This table maps a character into a number representing a
229 class. In each step there is a destination label for each
230 class. */
231 static const int jump_table[] =
233 /* ' ' */ 1, 0, 0, /* '#' */ 4,
234 0, /* '%' */ 14, 0, /* '\''*/ 6,
235 0, 0, /* '*' */ 7, /* '+' */ 2,
236 0, /* '-' */ 3, /* '.' */ 9, 0,
237 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
238 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
239 /* '8' */ 8, /* '9' */ 8, 0, 0,
240 0, 0, 0, 0,
241 0, 0, 0, /* 'C' */ 25,
242 0, /* 'E' */ 19, 0, /* 'G' */ 19,
243 0, 0, 0, 0,
244 /* 'L' */ 12, 0, 0, 0,
245 0, 0, 0, /* 'S' */ 21,
246 0, 0, 0, 0,
247 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
248 0, 0, 0, 0,
249 0, 0, 0, /* 'c' */ 20,
250 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
251 /* 'h' */ 10, /* 'i' */ 15, 0, 0,
252 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
253 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
254 0, /* 'u' */ 16, 0, 0,
255 /* 'x' */ 18
258 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < ' ' || (Ch) > 'x')
259 #define CHAR_CLASS(Ch) (jump_table[(int) (Ch) - ' '])
260 #define JUMP(ChExpr, table) \
261 do \
263 const void *ptr; \
264 spec = (ChExpr); \
265 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
266 : table[CHAR_CLASS (spec)]; \
267 goto *ptr; \
269 while (0)
271 #define STEP0_3_TABLE \
272 /* Step 0: at the beginning. */ \
273 static const void *step0_jumps[26] = \
275 REF (form_unknown), \
276 REF (flag_space), /* for ' ' */ \
277 REF (flag_plus), /* for '+' */ \
278 REF (flag_minus), /* for '-' */ \
279 REF (flag_hash), /* for '<hash>' */ \
280 REF (flag_zero), /* for '0' */ \
281 REF (flag_quote), /* for '\'' */ \
282 REF (width_asterics), /* for '*' */ \
283 REF (width), /* for '1'...'9' */ \
284 REF (precision), /* for '.' */ \
285 REF (mod_half), /* for 'h' */ \
286 REF (mod_long), /* for 'l' */ \
287 REF (mod_longlong), /* for 'L', 'q' */ \
288 REF (mod_size_t), /* for 'Z' */ \
289 REF (form_percent), /* for '%' */ \
290 REF (form_integer), /* for 'd', 'i' */ \
291 REF (form_unsigned), /* for 'u' */ \
292 REF (form_octal), /* for 'o' */ \
293 REF (form_hexa), /* for 'X', 'x' */ \
294 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
295 REF (form_character), /* for 'c' */ \
296 REF (form_string), /* for 's', 'S' */ \
297 REF (form_pointer), /* for 'p' */ \
298 REF (form_number), /* for 'n' */ \
299 REF (form_strerror), /* for 'm' */ \
300 REF (form_wcharacter) /* for 'C' */ \
301 }; \
302 /* Step 1: after processing width. */ \
303 static const void *step1_jumps[26] = \
305 REF (form_unknown), \
306 REF (form_unknown), /* for ' ' */ \
307 REF (form_unknown), /* for '+' */ \
308 REF (form_unknown), /* for '-' */ \
309 REF (form_unknown), /* for '<hash>' */ \
310 REF (form_unknown), /* for '0' */ \
311 REF (form_unknown), /* for '\'' */ \
312 REF (form_unknown), /* for '*' */ \
313 REF (form_unknown), /* for '1'...'9' */ \
314 REF (precision), /* for '.' */ \
315 REF (mod_half), /* for 'h' */ \
316 REF (mod_long), /* for 'l' */ \
317 REF (mod_longlong), /* for 'L', 'q' */ \
318 REF (mod_size_t), /* for 'Z' */ \
319 REF (form_percent), /* for '%' */ \
320 REF (form_integer), /* for 'd', 'i' */ \
321 REF (form_unsigned), /* for 'u' */ \
322 REF (form_octal), /* for 'o' */ \
323 REF (form_hexa), /* for 'X', 'x' */ \
324 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
325 REF (form_character), /* for 'c' */ \
326 REF (form_string), /* for 's', 'S' */ \
327 REF (form_pointer), /* for 'p' */ \
328 REF (form_number), /* for 'n' */ \
329 REF (form_strerror), /* for 'm' */ \
330 REF (form_wcharacter) /* for 'C' */ \
331 }; \
332 /* Step 2: after processing precision. */ \
333 static const void *step2_jumps[26] = \
335 REF (form_unknown), \
336 REF (form_unknown), /* for ' ' */ \
337 REF (form_unknown), /* for '+' */ \
338 REF (form_unknown), /* for '-' */ \
339 REF (form_unknown), /* for '<hash>' */ \
340 REF (form_unknown), /* for '0' */ \
341 REF (form_unknown), /* for '\'' */ \
342 REF (form_unknown), /* for '*' */ \
343 REF (form_unknown), /* for '1'...'9' */ \
344 REF (form_unknown), /* for '.' */ \
345 REF (mod_half), /* for 'h' */ \
346 REF (mod_long), /* for 'l' */ \
347 REF (mod_longlong), /* for 'L', 'q' */ \
348 REF (mod_size_t), /* for 'Z' */ \
349 REF (form_percent), /* for '%' */ \
350 REF (form_integer), /* for 'd', 'i' */ \
351 REF (form_unsigned), /* for 'u' */ \
352 REF (form_octal), /* for 'o' */ \
353 REF (form_hexa), /* for 'X', 'x' */ \
354 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
355 REF (form_character), /* for 'c' */ \
356 REF (form_string), /* for 's', 'S' */ \
357 REF (form_pointer), /* for 'p' */ \
358 REF (form_number), /* for 'n' */ \
359 REF (form_strerror), /* for 'm' */ \
360 REF (form_wcharacter) /* for 'C' */ \
361 }; \
362 /* Step 3: after processing first 'l' modifier. */ \
363 static const void *step3_jumps[26] = \
365 REF (form_unknown), \
366 REF (form_unknown), /* for ' ' */ \
367 REF (form_unknown), /* for '+' */ \
368 REF (form_unknown), /* for '-' */ \
369 REF (form_unknown), /* for '<hash>' */ \
370 REF (form_unknown), /* for '0' */ \
371 REF (form_unknown), /* for '\'' */ \
372 REF (form_unknown), /* for '*' */ \
373 REF (form_unknown), /* for '1'...'9' */ \
374 REF (form_unknown), /* for '.' */ \
375 REF (form_unknown), /* for 'h' */ \
376 REF (mod_longlong), /* for 'l' */ \
377 REF (form_unknown), /* for 'L', 'q' */ \
378 REF (form_unknown), /* for 'Z' */ \
379 REF (form_percent), /* for '%' */ \
380 REF (form_integer), /* for 'd', 'i' */ \
381 REF (form_unsigned), /* for 'u' */ \
382 REF (form_octal), /* for 'o' */ \
383 REF (form_hexa), /* for 'X', 'x' */ \
384 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
385 REF (form_character), /* for 'c' */ \
386 REF (form_string), /* for 's', 'S' */ \
387 REF (form_pointer), /* for 'p' */ \
388 REF (form_number), /* for 'n' */ \
389 REF (form_strerror), /* for 'm' */ \
390 REF (form_wcharacter) /* for 'C' */ \
393 #define STEP4_TABLE \
394 /* Step 4: processing format specifier. */ \
395 static const void *step4_jumps[26] = \
397 REF (form_unknown), \
398 REF (form_unknown), /* for ' ' */ \
399 REF (form_unknown), /* for '+' */ \
400 REF (form_unknown), /* for '-' */ \
401 REF (form_unknown), /* for '<hash>' */ \
402 REF (form_unknown), /* for '0' */ \
403 REF (form_unknown), /* for '\'' */ \
404 REF (form_unknown), /* for '*' */ \
405 REF (form_unknown), /* for '1'...'9' */ \
406 REF (form_unknown), /* for '.' */ \
407 REF (form_unknown), /* for 'h' */ \
408 REF (form_unknown), /* for 'l' */ \
409 REF (form_unknown), /* for 'L', 'q' */ \
410 REF (form_unknown), /* for 'Z' */ \
411 REF (form_percent), /* for '%' */ \
412 REF (form_integer), /* for 'd', 'i' */ \
413 REF (form_unsigned), /* for 'u' */ \
414 REF (form_octal), /* for 'o' */ \
415 REF (form_hexa), /* for 'X', 'x' */ \
416 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
417 REF (form_character), /* for 'c' */ \
418 REF (form_string), /* for 's', 'S' */ \
419 REF (form_pointer), /* for 'p' */ \
420 REF (form_number), /* for 'n' */ \
421 REF (form_strerror), /* for 'm' */ \
422 REF (form_wcharacter) /* for 'C' */ \
426 #define process_arg(fspec) \
427 /* Start real work. We know about all flags and modifiers and \
428 now process the wanted format specifier. */ \
429 LABEL (form_percent): \
430 /* Write a literal "%". */ \
431 outchar ('%'); \
432 break; \
434 LABEL (form_integer): \
435 /* Signed decimal integer. */ \
436 base = 10; \
438 if (is_longlong) \
440 long long int signed_number; \
442 if (fspec == NULL) \
443 signed_number = va_arg (ap, long long int); \
444 else \
445 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
447 is_negative = signed_number < 0; \
448 number.longlong = is_negative ? (- signed_number) : signed_number; \
450 goto LABEL (longlong_number); \
452 else \
454 long int signed_number; \
456 if (fspec == NULL) \
457 if (is_long) \
458 signed_number = va_arg (ap, long int); \
459 else /* `short int' will be promoted to `int'. */ \
460 signed_number = va_arg (ap, int); \
461 else \
462 if (is_long) \
463 signed_number = args_value[fspec->data_arg].pa_long_int; \
464 else \
465 signed_number = args_value[fspec->data_arg].pa_int; \
467 is_negative = signed_number < 0; \
468 number.word = is_negative ? (- signed_number) : signed_number; \
470 goto LABEL (number); \
472 /* NOTREACHED */ \
474 LABEL (form_unsigned): \
475 /* Unsigned decimal integer. */ \
476 base = 10; \
477 goto LABEL (unsigned_number); \
478 /* NOTREACHED */ \
480 LABEL (form_octal): \
481 /* Unsigned octal integer. */ \
482 base = 8; \
483 goto LABEL (unsigned_number); \
484 /* NOTREACHED */ \
486 LABEL (form_hexa): \
487 /* Unsigned hexadecimal integer. */ \
488 base = 16; \
490 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
492 /* ANSI specifies the `+' and ` ' flags only for signed \
493 conversions. */ \
494 is_negative = 0; \
495 showsign = 0; \
496 space = 0; \
498 if (is_longlong) \
500 if (fspec == NULL) \
501 number.longlong = va_arg (ap, unsigned long long int); \
502 else \
503 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
505 LABEL (longlong_number): \
506 if (prec < 0) \
507 /* Supply a default precision if none was given. */ \
508 prec = 1; \
509 else \
510 /* We have to take care for the '0' flag. If a precision \
511 is given it must be ignored. */ \
512 pad = ' '; \
514 /* If the precision is 0 and the number is 0 nothing has to \
515 be written for the number. */ \
516 if (prec == 0 && number.longlong == 0) \
517 string = workend; \
518 else \
520 /* Put the number in WORK. */ \
521 string = _itoa (number.longlong, workend + 1, base, \
522 spec == 'X'); \
523 string -= 1; \
524 if (group && grouping) \
525 string = group_number (string, workend, grouping, \
526 thousands_sep); \
528 /* Simply further test for num != 0. */ \
529 number.word = number.longlong != 0; \
531 else \
533 if (fspec == NULL) \
534 if (is_long) \
535 number.word = va_arg (ap, unsigned long int); \
536 else if (!is_short) \
537 number.word = va_arg (ap, unsigned int); \
538 else \
539 number.word = (unsigned short int) va_arg (ap, unsigned int); \
540 else \
541 if (is_long) \
542 number.word = args_value[fspec->data_arg].pa_u_long_int; \
543 else if (!is_short) \
544 number.word = args_value[fspec->data_arg].pa_u_int; \
545 else \
546 number.word = (unsigned short int) \
547 args_value[fspec->data_arg].pa_u_short_int; \
549 LABEL (number): \
550 if (prec < 0) \
551 /* Supply a default precision if none was given. */ \
552 prec = 1; \
553 else \
554 /* We have to take care for the '0' flag. If a precision \
555 is given it must be ignored. */ \
556 pad = ' '; \
558 /* If the precision is 0 and the number is 0 nothing has to \
559 be written for the number. */ \
560 if (prec == 0 && number.word == 0) \
561 string = workend; \
562 else \
564 /* Put the number in WORK. */ \
565 string = _itoa_word (number.word, workend + 1, base, \
566 spec == 'X'); \
567 string -= 1; \
568 if (group && grouping) \
569 string = group_number (string, workend, grouping, \
570 thousands_sep); \
574 prec -= workend - string; \
576 if (prec > 0) \
577 /* Add zeros to the precision. */ \
578 while (prec-- > 0) \
579 *string-- = '0'; \
580 else if (number.word != 0 && alt && base == 8) \
581 /* Add octal marker. */ \
582 *string-- = '0'; \
584 if (!left) \
586 width -= workend - string; \
588 if (number.word != 0 && alt && base == 16) \
589 /* Account for 0X hex marker. */ \
590 width -= 2; \
592 if (is_negative || showsign || space) \
593 --width; \
595 if (pad == '0') \
597 while (width-- > 0) \
598 *string-- = '0'; \
600 if (number.word != 0 && alt && base == 16) \
602 *string-- = spec; \
603 *string-- = '0'; \
606 if (is_negative) \
607 *string-- = '-'; \
608 else if (showsign) \
609 *string-- = '+'; \
610 else if (space) \
611 *string-- = ' '; \
613 else \
615 if (number.word != 0 && alt && base == 16) \
617 *string-- = spec; \
618 *string-- = '0'; \
621 if (is_negative) \
622 *string-- = '-'; \
623 else if (showsign) \
624 *string-- = '+'; \
625 else if (space) \
626 *string-- = ' '; \
628 while (width-- > 0) \
629 *string-- = ' '; \
632 outstring (string + 1, workend - string); \
634 break; \
636 else \
638 if (number.word != 0 && alt && base == 16) \
640 *string-- = spec; \
641 *string-- = '0'; \
644 if (is_negative) \
645 *string-- = '-'; \
646 else if (showsign) \
647 *string-- = '+'; \
648 else if (space) \
649 *string-- = ' '; \
651 width -= workend - string; \
652 outstring (string + 1, workend - string); \
654 PAD (' '); \
655 break; \
658 LABEL (form_float): \
660 /* Floating-point number. This is handled by printf_fp.c. */ \
661 extern int __printf_fp __P ((FILE *, const struct printf_info *, \
662 const void **const)); \
663 const void *ptr; \
664 int function_done; \
666 if (fspec == NULL) \
668 struct printf_info info = { prec: prec, \
669 width: width, \
670 spec: spec, \
671 is_long_double: is_long_double, \
672 is_short: is_short, \
673 is_long: is_long, \
674 alt: alt, \
675 space: space, \
676 left: left, \
677 showsign: showsign, \
678 group: group, \
679 pad: pad, \
680 extra: 0 }; \
682 if (is_long_double) \
683 the_arg.pa_long_double = va_arg (ap, long double); \
684 else \
685 the_arg.pa_double = va_arg (ap, double); \
686 ptr = (const void *) &the_arg; \
688 function_done = __printf_fp (s, &info, &ptr); \
690 else \
692 ptr = (const void *) &args_value[fspec->data_arg]; \
694 function_done = __printf_fp (s, &fspec->info, &ptr); \
697 if (function_done < 0) \
698 /* Error in print handler. */ \
699 return -1; \
701 done += function_done; \
703 break; \
705 LABEL (form_character): \
706 /* Character. */ \
707 if (is_long) \
708 goto LABEL (form_wcharacter); \
709 --width; /* Account for the character itself. */ \
710 if (!left) \
711 PAD (' '); \
712 if (fspec == NULL) \
713 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
714 else \
715 outchar ((unsigned char) args_value[fspec->data_arg].pa_char); \
716 if (left) \
717 PAD (' '); \
718 break; \
720 LABEL (form_wcharacter): \
722 /* Wide character. */ \
723 char buf[MB_CUR_MAX]; \
724 mbstate_t mbstate; \
725 size_t len; \
727 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wint_t) \
728 : args_value[fspec->data_arg].pa_wchar), \
729 &mbstate); \
730 width -= len; \
731 if (!left) \
732 PAD (' '); \
733 outstring (buf, len); \
734 if (left) \
735 PAD (' '); \
737 break; \
739 LABEL (form_string): \
741 size_t len; \
743 /* The string argument could in fact be `char *' or `wchar_t *'. \
744 But this should not make a difference here. */ \
745 if (fspec == NULL) \
746 string = (char *) va_arg (ap, const char *); \
747 else \
748 string = (char *) args_value[fspec->data_arg].pa_string; \
750 /* Entry point for printing other strings. */ \
751 LABEL (print_string): \
753 if (string == NULL) \
755 /* Write "(null)" if there's space. */ \
756 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
758 string = (char *) null; \
759 len = sizeof (null) - 1; \
761 else \
763 string = (char *) ""; \
764 len = 0; \
767 else if (!is_long && spec != L_('S')) \
769 if (prec != -1) \
770 /* Search for the end of the string, but don't search past \
771 the length specified by the precision. */ \
772 len = strnlen (string, prec); \
773 else \
774 len = strlen (string); \
776 else \
778 const wchar_t *s2 = (const wchar_t *) string; \
779 mbstate_t mbstate; \
781 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
782 if (len == (size_t) -1) \
783 /* Illegal wide-character string. */ \
784 return -1; \
786 s2 = (const wchar_t *) string; \
787 string = alloca (len + 1); \
788 (void) __wcsrtombs (string, &s2, prec != -1 ? prec : UINT_MAX, \
789 &mbstate); \
792 if ((width -= len) < 0) \
794 outstring (string, len); \
795 break; \
798 if (!left) \
799 PAD (' '); \
800 outstring (string, len); \
801 if (left) \
802 PAD (' '); \
804 break; \
806 LABEL (form_pointer): \
807 /* Generic pointer. */ \
809 const void *ptr; \
810 if (fspec == NULL) \
811 ptr = va_arg (ap, void *); \
812 else \
813 ptr = args_value[fspec->data_arg].pa_pointer; \
814 if (ptr != NULL) \
816 /* If the pointer is not NULL, write it as a %#x spec. */ \
817 base = 16; \
818 number.word = (unsigned long int) ptr; \
819 is_negative = 0; \
820 alt = 1; \
821 group = 0; \
822 spec = 'x'; \
823 goto LABEL (number); \
825 else \
827 /* Write "(nil)" for a nil pointer. */ \
828 string = (char *) "(nil)"; \
829 /* Make sure the full string "(nil)" is printed. */ \
830 if (prec < 5) \
831 prec = 5; \
832 is_long = 0; /* This is no wide-char string. */ \
833 goto LABEL (print_string); \
836 /* NOTREACHED */ \
838 LABEL (form_number): \
839 /* Answer the count of characters written. */ \
840 if (fspec == NULL) \
841 if (is_longlong) \
842 *(long long int *) va_arg (ap, void *) = done; \
843 else if (is_long) \
844 *(long int *) va_arg (ap, void *) = done; \
845 else if (!is_short) \
846 *(int *) va_arg (ap, void *) = done; \
847 else \
848 *(short int *) va_arg (ap, void *) = done; \
849 else \
850 if (is_longlong) \
851 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
852 else if (is_long) \
853 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
854 else if (!is_short) \
855 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
856 else \
857 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
858 break; \
860 LABEL (form_strerror): \
861 /* Print description of error ERRNO. */ \
863 extern char *_strerror_internal __P ((int, char *buf, size_t)); \
865 string = (char *) \
866 _strerror_internal (save_errno, work_buffer, sizeof work_buffer); \
868 is_long = 0; /* This is no wide-char string. */ \
869 goto LABEL (print_string)
872 /* Sanity check of arguments. */
873 ARGCHECK (s, format);
875 if (UNBUFFERED_P (s))
876 /* Use a helper function which will allocate a local temporary buffer
877 for the stream and then call us again. */
878 return buffered_vfprintf (s, format, ap);
880 /* Initialize local variables. */
881 done = 0;
882 grouping = (const char *) -1;
883 #ifdef __va_copy
884 /* This macro will be available soon in gcc's <stdarg.h>. Me need it
885 since on some systems `va_list' is not an integral type. */
886 __va_copy (ap_save, ap);
887 #else
888 ap_save = ap;
889 #endif
890 nspecs_done = 0;
892 /* Find the first format specifier. */
893 f = lead_str_end = find_spec (format, &mbstate);
895 /* Lock stream. */
896 #ifdef USE_IN_LIBIO
897 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
898 _IO_flockfile (s);
899 #else
900 __libc_cleanup_region_start ((void (*) (void *)) &__funlockfile, s);
901 __flockfile (s);
902 #endif
904 /* Write the literal text before the first format. */
905 outstring ((const UCHAR_T *) format,
906 lead_str_end - (const UCHAR_T *) format);
908 /* If we only have to print a simple string, return now. */
909 if (*f == L_('\0'))
910 goto all_done;
912 /* Process whole format string. */
915 #define REF(Name) &&do_##Name
916 #define LABEL(Name) do_##Name
917 STEP0_3_TABLE;
918 STEP4_TABLE;
920 union printf_arg *args_value; /* This is not used here but ... */
921 int is_negative; /* Flag for negative number. */
922 union
924 unsigned long long int longlong;
925 unsigned long int word;
926 } number;
927 int base;
928 union printf_arg the_arg;
929 char *string; /* Pointer to argument string. */
930 int alt = 0; /* Alternate format. */
931 int space = 0; /* Use space prefix if no sign is needed. */
932 int left = 0; /* Left-justify output. */
933 int showsign = 0; /* Always begin with plus or minus sign. */
934 int group = 0; /* Print numbers according grouping rules. */
935 int is_long_double = 0; /* Argument is long double/ long long int. */
936 int is_short = 0; /* Argument is long int. */
937 int is_long = 0; /* Argument is short int. */
938 int width = 0; /* Width of output; 0 means none specified. */
939 int prec = -1; /* Precision of output; -1 means none specified. */
940 char pad = ' '; /* Padding character. */
941 CHAR_T spec;
943 /* Get current character in format string. */
944 JUMP (*++f, step0_jumps);
946 /* ' ' flag. */
947 LABEL (flag_space):
948 space = 1;
949 JUMP (*++f, step0_jumps);
951 /* '+' flag. */
952 LABEL (flag_plus):
953 showsign = 1;
954 JUMP (*++f, step0_jumps);
956 /* The '-' flag. */
957 LABEL (flag_minus):
958 left = 1;
959 pad = L_(' ');
960 JUMP (*++f, step0_jumps);
962 /* The '#' flag. */
963 LABEL (flag_hash):
964 alt = 1;
965 JUMP (*++f, step0_jumps);
967 /* The '0' flag. */
968 LABEL (flag_zero):
969 if (!left)
970 pad = L_('0');
971 JUMP (*++f, step0_jumps);
973 /* The '\'' flag. */
974 LABEL (flag_quote):
975 group = 1;
977 /* XXX Completely wrong. Use wctob. */
978 if (grouping == (const char *) -1)
980 /* Figure out the thousands separator character. */
981 if (mbtowc (&thousands_sep,
982 _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
983 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
984 thousands_sep = (wchar_t)
985 *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
986 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
987 if (*grouping == '\0' || *grouping == CHAR_MAX
988 || thousands_sep == L'\0')
989 grouping = NULL;
991 JUMP (*++f, step0_jumps);
993 /* Get width from argument. */
994 LABEL (width_asterics):
996 const UCHAR_T *tmp; /* Temporary value. */
998 tmp = ++f;
999 if (ISDIGIT (*tmp) && read_int (&tmp) && *tmp == L_('$'))
1000 /* The width comes from a positional parameter. */
1001 goto do_positional;
1003 width = va_arg (ap, int);
1005 /* Negative width means left justified. */
1006 if (width < 0)
1008 width = -width;
1009 pad = L_(' ');
1010 left = 1;
1013 JUMP (*f, step1_jumps);
1015 /* Given width in format string. */
1016 LABEL (width):
1017 width = read_int (&f);
1018 if (*f == L_('$'))
1019 /* Oh, oh. The argument comes from a positional parameter. */
1020 goto do_positional;
1021 JUMP (*f, step1_jumps);
1023 LABEL (precision):
1024 ++f;
1025 if (*f == L_('*'))
1027 const UCHAR_T *tmp; /* Temporary value. */
1029 tmp = ++f;
1030 if (ISDIGIT (*tmp) && read_int (&tmp) > 0 && *tmp == L_('$'))
1031 /* The precision comes from a positional parameter. */
1032 goto do_positional;
1034 prec = va_arg (ap, int);
1036 /* If the precision is negative the precision is omitted. */
1037 if (prec < 0)
1038 prec = -1;
1040 else if (ISDIGIT (*f))
1041 prec = read_int (&f);
1042 else
1043 prec = 0;
1044 JUMP (*f, step2_jumps);
1046 /* Process 'h' modifier. No other modifier is allowed to
1047 follow. */
1048 LABEL (mod_half):
1049 is_short = 1;
1050 JUMP (*++f, step4_jumps);
1052 /* Process 'l' modifier. There might another 'l' follow. */
1053 LABEL (mod_long):
1054 is_long = 1;
1055 JUMP (*++f, step3_jumps);
1057 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1058 allowed to follow. */
1059 LABEL (mod_longlong):
1060 is_long_double = 1;
1061 JUMP (*++f, step4_jumps);
1063 LABEL (mod_size_t):
1064 is_longlong = sizeof (size_t) > sizeof (unsigned long int);
1065 is_long = sizeof (size_t) > sizeof (unsigned int);
1066 JUMP (*++f, step4_jumps);
1069 /* Process current format. */
1070 while (1)
1072 process_arg (((struct printf_spec *) NULL));
1074 LABEL (form_unknown):
1075 if (spec == L_('\0'))
1077 /* The format string ended before the specifier is complete. */
1078 done = -1;
1079 goto all_done;
1082 /* If we are in the fast loop force entering the complicated
1083 one. */
1084 goto do_positional;
1087 /* The format is correctly handled. */
1088 ++nspecs_done;
1090 /* Look for next format specifier. */
1091 f = find_spec ((end_of_spec = ++f), &mbstate);
1093 /* Write the following constant string. */
1094 outstring (end_of_spec, f - end_of_spec);
1096 while (*f != L_('\0'));
1098 /* Unlock stream and return. */
1099 goto all_done;
1101 /* Here starts the more complex loop to handle positional parameters. */
1102 do_positional:
1104 /* Array with information about the needed arguments. This has to
1105 be dynamically extensible. */
1106 size_t nspecs = 0;
1107 size_t nspecs_max = 32; /* A more or less arbitrary start value. */
1108 struct printf_spec *specs
1109 = alloca (nspecs_max * sizeof (struct printf_spec));
1111 /* The number of arguments the format string requests. This will
1112 determine the size of the array needed to store the argument
1113 attributes. */
1114 size_t nargs = 0;
1115 int *args_type;
1116 union printf_arg *args_value;
1118 /* Positional parameters refer to arguments directly. This could
1119 also determine the maximum number of arguments. Track the
1120 maximum number. */
1121 size_t max_ref_arg = 0;
1123 /* Just a counter. */
1124 size_t cnt;
1127 if (grouping == (const char *) -1)
1129 /* XXX Use wctob. But this is incompatible for now. */
1130 /* Figure out the thousands separator character. */
1131 if (mbtowc (&thousands_sep,
1132 _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
1133 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
1134 thousands_sep = (wchar_t) *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1135 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1136 if (*grouping == '\0' || *grouping == CHAR_MAX
1137 || thousands_sep == L'\0')
1138 grouping = NULL;
1141 for (f = lead_str_end; *f != '\0'; f = specs[nspecs++].next_fmt)
1143 if (nspecs >= nspecs_max)
1145 /* Extend the array of format specifiers. */
1146 struct printf_spec *old = specs;
1148 nspecs_max *= 2;
1149 specs = alloca (nspecs_max * sizeof (struct printf_spec));
1151 if (specs == &old[nspecs])
1152 /* Stack grows up, OLD was the last thing allocated;
1153 extend it. */
1154 nspecs_max += nspecs_max / 2;
1155 else
1157 /* Copy the old array's elements to the new space. */
1158 memcpy (specs, old, nspecs * sizeof (struct printf_spec));
1159 if (old == &specs[nspecs])
1160 /* Stack grows down, OLD was just below the new
1161 SPECS. We can use that space when the new space
1162 runs out. */
1163 nspecs_max += nspecs_max / 2;
1167 /* Parse the format specifier. */
1168 nargs += parse_one_spec (f, nargs, &specs[nspecs], &max_ref_arg,
1169 &mbstate);
1172 /* Determine the number of arguments the format string consumes. */
1173 nargs = MAX (nargs, max_ref_arg);
1175 /* Allocate memory for the argument descriptions. */
1176 args_type = alloca (nargs * sizeof (int));
1177 memset (args_type, 0, nargs * sizeof (int));
1178 args_value = alloca (nargs * sizeof (union printf_arg));
1180 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1181 still zero after this loop, format is invalid. For now we
1182 simply use 0 as the value. */
1184 /* Fill in the types of all the arguments. */
1185 for (cnt = 0; cnt < nspecs; ++cnt)
1187 /* If the width is determined by an argument this is an int. */
1188 if (specs[cnt].width_arg != -1)
1189 args_type[specs[cnt].width_arg] = PA_INT;
1191 /* If the precision is determined by an argument this is an int. */
1192 if (specs[cnt].prec_arg != -1)
1193 args_type[specs[cnt].prec_arg] = PA_INT;
1195 switch (specs[cnt].ndata_args)
1197 case 0: /* No arguments. */
1198 break;
1199 case 1: /* One argument; we already have the type. */
1200 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1201 break;
1202 default:
1203 /* We have more than one argument for this format spec.
1204 We must call the arginfo function again to determine
1205 all the types. */
1206 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1207 (&specs[cnt].info,
1208 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg]);
1209 break;
1213 /* Now we know all the types and the order. Fill in the argument
1214 values. */
1215 for (cnt = 0; cnt < nargs; ++cnt)
1216 switch (args_type[cnt])
1218 #define T(tag, mem, type) \
1219 case tag: \
1220 args_value[cnt].mem = va_arg (ap_save, type); \
1221 break
1223 T (PA_CHAR, pa_char, int); /* Promoted. */
1224 T (PA_WCHAR, pa_wchar, wint_t);
1225 T (PA_INT|PA_FLAG_SHORT, pa_short_int, int); /* Promoted. */
1226 T (PA_INT, pa_int, int);
1227 T (PA_INT|PA_FLAG_LONG, pa_long_int, long int);
1228 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1229 T (PA_FLOAT, pa_float, double); /* Promoted. */
1230 T (PA_DOUBLE, pa_double, double);
1231 T (PA_DOUBLE|PA_FLAG_LONG_DOUBLE, pa_long_double, long double);
1232 T (PA_STRING, pa_string, const char *);
1233 T (PA_WSTRING, pa_wstring, const wchar_t *);
1234 T (PA_POINTER, pa_pointer, void *);
1235 #undef T
1236 default:
1237 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1238 args_value[cnt].pa_pointer = va_arg (ap_save, void *);
1239 else
1240 args_value[cnt].pa_long_double = 0.0;
1241 break;
1244 /* Now walk through all format specifiers and process them. */
1245 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1247 #undef REF
1248 #define REF(Name) &&do2_##Name
1249 #undef LABEL
1250 #define LABEL(Name) do2_##Name
1251 STEP4_TABLE;
1253 int is_negative;
1254 union
1256 unsigned long long int longlong;
1257 unsigned long int word;
1258 } number;
1259 int base;
1260 union printf_arg the_arg;
1261 char *string; /* Pointer to argument string. */
1263 /* Fill variables from values in struct. */
1264 int alt = specs[nspecs_done].info.alt;
1265 int space = specs[nspecs_done].info.space;
1266 int left = specs[nspecs_done].info.left;
1267 int showsign = specs[nspecs_done].info.showsign;
1268 int group = specs[nspecs_done].info.group;
1269 int is_long_double = specs[nspecs_done].info.is_long_double;
1270 int is_short = specs[nspecs_done].info.is_short;
1271 int is_long = specs[nspecs_done].info.is_long;
1272 int width = specs[nspecs_done].info.width;
1273 int prec = specs[nspecs_done].info.prec;
1274 char pad = specs[nspecs_done].info.pad;
1275 CHAR_T spec = specs[nspecs_done].info.spec;
1277 /* Fill in last information. */
1278 if (specs[nspecs_done].width_arg != -1)
1280 /* Extract the field width from an argument. */
1281 specs[nspecs_done].info.width =
1282 args_value[specs[nspecs_done].width_arg].pa_int;
1284 if (specs[nspecs_done].info.width < 0)
1285 /* If the width value is negative left justification is
1286 selected and the value is taken as being positive. */
1288 specs[nspecs_done].info.width *= -1;
1289 left = specs[nspecs_done].info.left = 1;
1291 width = specs[nspecs_done].info.width;
1294 if (specs[nspecs_done].prec_arg != -1)
1296 /* Extract the precision from an argument. */
1297 specs[nspecs_done].info.prec =
1298 args_value[specs[nspecs_done].prec_arg].pa_int;
1300 if (specs[nspecs_done].info.prec < 0)
1301 /* If the precision is negative the precision is
1302 omitted. */
1303 specs[nspecs_done].info.prec = -1;
1305 prec = specs[nspecs_done].info.prec;
1308 /* Process format specifiers. */
1309 while (1)
1311 JUMP (spec, step4_jumps);
1313 process_arg ((&specs[nspecs_done]));
1315 LABEL (form_unknown):
1317 extern printf_function **__printf_function_table;
1318 int function_done;
1319 printf_function *function;
1320 unsigned int i;
1321 const void **ptr;
1323 function =
1324 (__printf_function_table == NULL ? NULL :
1325 __printf_function_table[specs[nspecs_done].info.spec]);
1327 if (function == NULL)
1328 function = &printf_unknown;
1330 ptr = alloca (specs[nspecs_done].ndata_args
1331 * sizeof (const void *));
1333 /* Fill in an array of pointers to the argument values. */
1334 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
1335 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1337 /* Call the function. */
1338 function_done = (*function) (s, &specs[nspecs_done].info, ptr);
1340 /* If an error occurred we don't have information about #
1341 of chars. */
1342 if (function_done < 0)
1344 done = -1;
1345 goto all_done;
1348 done += function_done;
1350 break;
1353 /* Write the following constant string. */
1354 outstring (specs[nspecs_done].end_of_fmt,
1355 specs[nspecs_done].next_fmt
1356 - specs[nspecs_done].end_of_fmt);
1360 all_done:
1361 /* Unlock the stream. */
1362 __libc_cleanup_region_end (1);
1364 return done;
1367 #ifdef USE_IN_LIBIO
1368 # undef vfprintf
1369 # ifdef strong_alias
1370 /* This is for glibc. */
1371 strong_alias (_IO_vfprintf, vfprintf);
1372 # else
1373 # if defined __ELF__ || defined __GNU_LIBRARY__
1374 # include <gnu-stabs.h>
1375 # ifdef weak_alias
1376 weak_alias (_IO_vfprintf, vfprintf);
1377 # endif
1378 # endif
1379 # endif
1380 #endif
1382 /* Handle an unknown format specifier. This prints out a canonicalized
1383 representation of the format spec itself. */
1384 static int
1385 printf_unknown (FILE *s, const struct printf_info *info,
1386 const void *const *args)
1389 int done = 0;
1390 char work_buffer[BUFSIZ];
1391 register char *w;
1393 outchar ('%');
1395 if (info->alt)
1396 outchar ('#');
1397 if (info->group)
1398 outchar ('\'');
1399 if (info->showsign)
1400 outchar ('+');
1401 else if (info->space)
1402 outchar (' ');
1403 if (info->left)
1404 outchar ('-');
1405 if (info->pad == '0')
1406 outchar ('0');
1408 if (info->width != 0)
1410 w = _itoa_word (info->width, workend + 1, 10, 0);
1411 while (w <= workend)
1412 outchar (*w++);
1415 if (info->prec != -1)
1417 outchar ('.');
1418 w = _itoa_word (info->prec, workend + 1, 10, 0);
1419 while (w <= workend)
1420 outchar (*w++);
1423 if (info->spec != '\0')
1424 outchar (info->spec);
1426 return done;
1429 /* Group the digits according to the grouping rules of the current locale.
1430 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1431 static char *
1432 group_number (CHAR_T *w, CHAR_T *rear_ptr, const CHAR_T *grouping,
1433 wchar_t thousands_sep)
1435 int len;
1436 char *src, *s;
1438 /* We treat all negative values like CHAR_MAX. */
1440 if (*grouping == CHAR_MAX || *grouping <= 0)
1441 /* No grouping should be done. */
1442 return w;
1444 len = *grouping;
1446 /* Copy existing string so that nothing gets overwritten. */
1447 src = (char *) alloca (rear_ptr - w);
1448 memcpy (src, w + 1, rear_ptr - w);
1449 s = &src[rear_ptr - w - 1];
1450 w = rear_ptr;
1452 /* Process all characters in the string. */
1453 while (s >= src)
1455 *w-- = *s--;
1457 if (--len == 0 && s >= src)
1459 /* A new group begins. */
1460 *w-- = thousands_sep;
1462 len = *grouping++;
1463 if (*grouping == '\0')
1464 /* The previous grouping repeats ad infinitum. */
1465 --grouping;
1466 else if (*grouping == CHAR_MAX || *grouping < 0)
1468 /* No further grouping to be done.
1469 Copy the rest of the number. */
1471 *w-- = *s--;
1472 while (s >= src);
1473 break;
1477 return w;
1480 #ifdef USE_IN_LIBIO
1481 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
1482 struct helper_file
1484 struct _IO_FILE_plus _f;
1485 _IO_FILE *_put_stream;
1486 #ifdef _IO_MTSAFE_IO
1487 _IO_lock_t lock;
1488 #endif
1491 static int
1492 _IO_helper_overflow (_IO_FILE *s, int c)
1494 _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
1495 int used = s->_IO_write_ptr - s->_IO_write_base;
1496 if (used)
1498 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
1499 s->_IO_write_ptr -= written;
1501 return PUTC (c, s);
1504 static const struct _IO_jump_t _IO_helper_jumps =
1506 JUMP_INIT_DUMMY,
1507 JUMP_INIT (finish, _IO_default_finish),
1508 JUMP_INIT (overflow, _IO_helper_overflow),
1509 JUMP_INIT (underflow, _IO_default_underflow),
1510 JUMP_INIT (uflow, _IO_default_uflow),
1511 JUMP_INIT (pbackfail, _IO_default_pbackfail),
1512 JUMP_INIT (xsputn, _IO_default_xsputn),
1513 JUMP_INIT (xsgetn, _IO_default_xsgetn),
1514 JUMP_INIT (seekoff, _IO_default_seekoff),
1515 JUMP_INIT (seekpos, _IO_default_seekpos),
1516 JUMP_INIT (setbuf, _IO_default_setbuf),
1517 JUMP_INIT (sync, _IO_default_sync),
1518 JUMP_INIT (doallocate, _IO_default_doallocate),
1519 JUMP_INIT (read, _IO_default_read),
1520 JUMP_INIT (write, _IO_default_write),
1521 JUMP_INIT (seek, _IO_default_seek),
1522 JUMP_INIT (close, _IO_default_close),
1523 JUMP_INIT (stat, _IO_default_stat)
1526 static int
1527 buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format,
1528 _IO_va_list args)
1530 char buf[_IO_BUFSIZ];
1531 struct helper_file helper;
1532 register _IO_FILE *hp = (_IO_FILE *) &helper;
1533 int result, to_flush;
1535 /* Initialize helper. */
1536 helper._put_stream = s;
1537 hp->_IO_write_base = buf;
1538 hp->_IO_write_ptr = buf;
1539 hp->_IO_write_end = buf + sizeof buf;
1540 hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS;
1541 #ifdef _IO_MTSAFE_IO
1542 hp->_lock = &helper.lock;
1543 __libc_lock_init (*hp->_lock);
1544 #endif
1545 _IO_JUMPS (hp) = (struct _IO_jump_t *) &_IO_helper_jumps;
1547 /* Now print to helper instead. */
1548 result = _IO_vfprintf (hp, format, args);
1550 /* Now flush anything from the helper to the S. */
1551 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
1553 if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
1554 return -1;
1557 return result;
1560 #else /* !USE_IN_LIBIO */
1562 static int
1563 buffered_vfprintf (register FILE *s, const CHAR_T *format, va_list args)
1565 char buf[BUFSIZ];
1566 int result;
1568 s->__bufp = s->__buffer = buf;
1569 s->__bufsize = sizeof buf;
1570 s->__put_limit = s->__buffer + s->__bufsize;
1571 s->__get_limit = s->__buffer;
1573 /* Now use buffer to print. */
1574 result = vfprintf (s, format, args);
1576 if (fflush (s) == EOF)
1577 result = -1;
1578 s->__buffer = s->__bufp = s->__get_limit = s->__put_limit = NULL;
1579 s->__bufsize = 0;
1581 return result;
1584 /* Pads string with given number of a specified character.
1585 This code is taken from iopadn.c of the GNU I/O library. */
1586 #define PADSIZE 16
1587 static const CHAR_T blanks[PADSIZE] =
1588 { L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '),
1589 L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' ') };
1590 static const CHAR_T zeroes[PADSIZE] =
1591 { L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'),
1592 L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0') };
1594 ssize_t
1595 #ifndef COMPILE_WPRINTF
1596 __printf_pad (FILE *s, char pad, size_t count)
1597 #else
1598 __wprintf_pad (FILE *s, wchar_t pad, size_t count)
1599 #endif
1601 const CHAR_T *padptr;
1602 register size_t i;
1604 padptr = pad == L_(' ') ? blanks : zeroes;
1606 for (i = count; i >= PADSIZE; i -= PADSIZE)
1607 if (PUT (s, padptr, PADSIZE) != PADSIZE)
1608 return -1;
1609 if (i > 0)
1610 if (PUT (s, padptr, i) != i)
1611 return -1;
1613 return count;
1615 #undef PADSIZE
1616 #endif /* USE_IN_LIBIO */