(pututline_r): Since we assign RESULT from lseek now, check that it's >= 0, not...
[glibc.git] / stdio-common / vfprintf.c
blob8031b99ae963ac3a30e7f652a3a23c1a64ab9f8f
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
16 not, 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 "_itoa.h"
27 #include "../locale/localeinfo.h"
29 /* This code is shared between the standard stdio implementation found
30 in GNU C library and the libio implementation originally found in
31 GNU libg++.
33 Beside this it is also shared between the normal and wide character
34 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
36 #ifndef COMPILE_WPRINTF
37 # define CHAR_T char
38 # define UCHAR_T unsigned char
39 # define INT_T int
40 # define L_(Str) Str
41 # define ISDIGIT(Ch) isdigit (Ch)
43 # ifdef USE_IN_LIBIO
44 # define PUT(F, S, N) _IO_sputn (F, S, N)
45 # define PAD(Padchar) \
46 if (width > 0) \
47 done += _IO_padn (s, Padchar, width)
48 # else
49 # define PUTC(C, F) putc (C, F)
50 ssize_t __printf_pad __P ((FILE *, char pad, size_t n));
51 # define PAD(Padchar) \
52 if (width > 0) \
53 { if (__printf_pad (s, Padchar, width) == -1) \
54 return -1; else done += width; }
55 # endif
56 #else
57 # define vfprintf vfwprintf
58 # define CHAR_T wchar_t
59 # define UCHAR_T uwchar_t
60 # define INT_T wint_t
61 # define L_(Str) L##Str
62 # define ISDIGIT(Ch) iswdigit (Ch)
64 # ifdef USE_IN_LIBIO
65 # define PUT(F, S, N) _IO_sputn (F, S, N)
66 # define PAD(Padchar) \
67 if (width > 0) \
68 done += _IO_wpadn (s, Padchar, width)
69 # else
70 # define PUTC(C, F) wputc (C, F)
71 ssize_t __wprintf_pad __P ((FILE *, wchar_t pad, size_t n));
72 # define PAD(Padchar) \
73 if (width > 0) \
74 { if (__wprintf_pad (s, Padchar, width) == -1) \
75 return -1; else done += width; }
76 # endif
77 #endif
79 /* Include the shared code for parsing the format string. */
80 #include "printf-parse.h"
83 #ifdef USE_IN_LIBIO
84 /* This code is for use in libio. */
85 # include <libioP.h>
86 # define PUTC(C, F) _IO_putc_unlocked (C, F)
87 # define vfprintf _IO_vfprintf
88 # define FILE _IO_FILE
89 # define va_list _IO_va_list
90 # undef BUFSIZ
91 # define BUFSIZ _IO_BUFSIZ
92 # define ARGCHECK(S, Format) \
93 do \
94 { \
95 /* Check file argument for consistence. */ \
96 CHECK_FILE (S, -1); \
97 if (S->_flags & _IO_NO_WRITES || Format == NULL) \
98 { \
99 MAYBE_SET_EINVAL; \
100 return -1; \
102 } while (0)
103 # define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
104 #else /* ! USE_IN_LIBIO */
105 /* This code is for use in the GNU C library. */
106 # include <stdio.h>
107 # define PUT(F, S, N) fwrite (S, 1, N, F)
108 # define ARGCHECK(S, Format) \
109 do \
111 /* Check file argument for consistence. */ \
112 if (!__validfp(S) || !S->__mode.__write || Format == NULL) \
114 errno = EINVAL; \
115 return -1; \
117 if (!S->__seen) \
119 if (__flshfp (S, EOF) == EOF) \
120 return -1; \
123 while (0)
124 # define UNBUFFERED_P(s) ((s)->__buffer == NULL)
125 # define flockfile(S) /* nothing */
126 # define funlockfile(S) /* nothing */
127 #endif /* USE_IN_LIBIO */
130 #define outchar(Ch) \
131 do \
133 register const int outc = (Ch); \
134 if (PUTC (outc, s) == EOF) \
135 return -1; \
136 else \
137 ++done; \
139 while (0)
141 #define outstring(String, Len) \
142 do \
144 if (PUT (s, String, Len) != Len) \
145 return -1; \
146 done += Len; \
148 while (0)
150 /* For handling long_double and longlong we use the same flag. */
151 #ifndef is_longlong
152 # define is_longlong is_long_double
153 #endif
156 /* Global variables. */
157 static const char null[] = "(null)";
160 /* Helper function to provide temporary buffering for unbuffered streams. */
161 static int buffered_vfprintf __P ((FILE *stream, const CHAR_T *fmt, va_list));
163 /* Handle unknown format specifier. */
164 static int printf_unknown __P ((FILE *, const struct printf_info *,
165 const void *const *));
167 /* Group digits of number string. */
168 static char *group_number __P ((CHAR_T *, CHAR_T *, const CHAR_T *, wchar_t));
171 /* The function itself. */
173 vfprintf (FILE *s, const CHAR_T *format, va_list ap)
175 /* The character used as thousands separator. */
176 wchar_t thousands_sep;
178 /* The string describing the size of groups of digits. */
179 const char *grouping;
181 /* Place to accumulate the result. */
182 int done;
184 /* Current character in format string. */
185 const UCHAR_T *f;
187 /* End of leading constant string. */
188 const UCHAR_T *lead_str_end;
190 /* Points to next format specifier. */
191 const UCHAR_T *end_of_spec;
193 /* Buffer intermediate results. */
194 char work_buffer[1000];
195 #define workend (&work_buffer[sizeof (work_buffer) - 1])
197 /* State for restartable multibyte character handling functions. */
198 mbstate_t mbstate;
200 /* We have to save the original argument pointer. */
201 va_list ap_save;
203 /* Count number of specifiers we already processed. */
204 int nspecs_done;
207 /* This table maps a character into a number representing a
208 class. In each step there is a destination label for each
209 class. */
210 static const int jump_table[] =
212 /* ' ' */ 1, 0, 0, /* '#' */ 4,
213 0, /* '%' */ 14, 0, /* '\''*/ 6,
214 0, 0, /* '*' */ 7, /* '+' */ 2,
215 0, /* '-' */ 3, /* '.' */ 9, 0,
216 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
217 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
218 /* '8' */ 8, /* '9' */ 8, 0, 0,
219 0, 0, 0, 0,
220 0, 0, 0, 0,
221 0, /* 'E' */ 19, 0, /* 'G' */ 19,
222 0, 0, 0, 0,
223 /* 'L' */ 12, 0, 0, 0,
224 0, 0, 0, 0,
225 0, 0, 0, 0,
226 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
227 0, 0, 0, 0,
228 0, 0, 0, /* 'c' */ 20,
229 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
230 /* 'h' */ 10, /* 'i' */ 15, 0, 0,
231 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
232 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
233 0, /* 'u' */ 16, 0, 0,
234 /* 'x' */ 18
237 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < ' ' || (Ch) > 'x')
238 #define CHAR_CLASS(Ch) (jump_table[(int) (Ch) - ' '])
239 #define JUMP(ChExpr, table) \
240 do \
242 const void *ptr; \
243 spec = (ChExpr); \
244 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
245 : table[CHAR_CLASS (spec)]; \
246 goto *ptr; \
248 while (0)
250 #define STEP0_3_TABLE \
251 /* Step 0: at the beginning. */ \
252 static const void *step0_jumps[25] = \
254 REF (form_unknown), \
255 REF (flag_space), /* for ' ' */ \
256 REF (flag_plus), /* for '+' */ \
257 REF (flag_minus), /* for '-' */ \
258 REF (flag_hash), /* for '<hash>' */ \
259 REF (flag_zero), /* for '0' */ \
260 REF (flag_quote), /* for '\'' */ \
261 REF (width_asterics), /* for '*' */ \
262 REF (width), /* for '1'...'9' */ \
263 REF (precision), /* for '.' */ \
264 REF (mod_half), /* for 'h' */ \
265 REF (mod_long), /* for 'l' */ \
266 REF (mod_longlong), /* for 'L', 'q' */ \
267 REF (mod_size_t), /* for 'Z' */ \
268 REF (form_percent), /* for '%' */ \
269 REF (form_integer), /* for 'd', 'i' */ \
270 REF (form_unsigned), /* for 'u' */ \
271 REF (form_octal), /* for 'o' */ \
272 REF (form_hexa), /* for 'X', 'x' */ \
273 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
274 REF (form_character), /* for 'c' */ \
275 REF (form_string), /* for 's' */ \
276 REF (form_pointer), /* for 'p' */ \
277 REF (form_number), /* for 'n' */ \
278 REF (form_strerror) /* for 'm' */ \
279 }; \
280 /* Step 1: after processing width. */ \
281 static const void *step1_jumps[25] = \
283 REF (form_unknown), \
284 REF (form_unknown), /* for ' ' */ \
285 REF (form_unknown), /* for '+' */ \
286 REF (form_unknown), /* for '-' */ \
287 REF (form_unknown), /* for '<hash>' */ \
288 REF (form_unknown), /* for '0' */ \
289 REF (form_unknown), /* for '\'' */ \
290 REF (form_unknown), /* for '*' */ \
291 REF (form_unknown), /* for '1'...'9' */ \
292 REF (precision), /* for '.' */ \
293 REF (mod_half), /* for 'h' */ \
294 REF (mod_long), /* for 'l' */ \
295 REF (mod_longlong), /* for 'L', 'q' */ \
296 REF (mod_size_t), /* for 'Z' */ \
297 REF (form_percent), /* for '%' */ \
298 REF (form_integer), /* for 'd', 'i' */ \
299 REF (form_unsigned), /* for 'u' */ \
300 REF (form_octal), /* for 'o' */ \
301 REF (form_hexa), /* for 'X', 'x' */ \
302 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
303 REF (form_character), /* for 'c' */ \
304 REF (form_string), /* for 's' */ \
305 REF (form_pointer), /* for 'p' */ \
306 REF (form_number), /* for 'n' */ \
307 REF (form_strerror) /* for 'm' */ \
308 }; \
309 /* Step 2: after processing precision. */ \
310 static const void *step2_jumps[25] = \
312 REF (form_unknown), \
313 REF (form_unknown), /* for ' ' */ \
314 REF (form_unknown), /* for '+' */ \
315 REF (form_unknown), /* for '-' */ \
316 REF (form_unknown), /* for '<hash>' */ \
317 REF (form_unknown), /* for '0' */ \
318 REF (form_unknown), /* for '\'' */ \
319 REF (form_unknown), /* for '*' */ \
320 REF (form_unknown), /* for '1'...'9' */ \
321 REF (form_unknown), /* for '.' */ \
322 REF (mod_half), /* for 'h' */ \
323 REF (mod_long), /* for 'l' */ \
324 REF (mod_longlong), /* for 'L', 'q' */ \
325 REF (mod_size_t), /* for 'Z' */ \
326 REF (form_percent), /* for '%' */ \
327 REF (form_integer), /* for 'd', 'i' */ \
328 REF (form_unsigned), /* for 'u' */ \
329 REF (form_octal), /* for 'o' */ \
330 REF (form_hexa), /* for 'X', 'x' */ \
331 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
332 REF (form_character), /* for 'c' */ \
333 REF (form_string), /* for 's' */ \
334 REF (form_pointer), /* for 'p' */ \
335 REF (form_number), /* for 'n' */ \
336 REF (form_strerror) /* for 'm' */ \
337 }; \
338 /* Step 3: after processing first 'l' modifier. */ \
339 static const void *step3_jumps[25] = \
341 REF (form_unknown), \
342 REF (form_unknown), /* for ' ' */ \
343 REF (form_unknown), /* for '+' */ \
344 REF (form_unknown), /* for '-' */ \
345 REF (form_unknown), /* for '<hash>' */ \
346 REF (form_unknown), /* for '0' */ \
347 REF (form_unknown), /* for '\'' */ \
348 REF (form_unknown), /* for '*' */ \
349 REF (form_unknown), /* for '1'...'9' */ \
350 REF (form_unknown), /* for '.' */ \
351 REF (form_unknown), /* for 'h' */ \
352 REF (mod_longlong), /* for 'l' */ \
353 REF (form_unknown), /* for 'L', 'q' */ \
354 REF (form_unknown), /* for 'Z' */ \
355 REF (form_percent), /* for '%' */ \
356 REF (form_integer), /* for 'd', 'i' */ \
357 REF (form_unsigned), /* for 'u' */ \
358 REF (form_octal), /* for 'o' */ \
359 REF (form_hexa), /* for 'X', 'x' */ \
360 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
361 REF (form_character), /* for 'c' */ \
362 REF (form_string), /* for 's' */ \
363 REF (form_pointer), /* for 'p' */ \
364 REF (form_number), /* for 'n' */ \
365 REF (form_strerror) /* for 'm' */ \
368 #define STEP4_TABLE \
369 /* Step 4: processing format specifier. */ \
370 static const void *step4_jumps[25] = \
372 REF (form_unknown), \
373 REF (form_unknown), /* for ' ' */ \
374 REF (form_unknown), /* for '+' */ \
375 REF (form_unknown), /* for '-' */ \
376 REF (form_unknown), /* for '<hash>' */ \
377 REF (form_unknown), /* for '0' */ \
378 REF (form_unknown), /* for '\'' */ \
379 REF (form_unknown), /* for '*' */ \
380 REF (form_unknown), /* for '1'...'9' */ \
381 REF (form_unknown), /* for '.' */ \
382 REF (form_unknown), /* for 'h' */ \
383 REF (form_unknown), /* for 'l' */ \
384 REF (form_unknown), /* for 'L', 'q' */ \
385 REF (form_unknown), /* for 'Z' */ \
386 REF (form_percent), /* for '%' */ \
387 REF (form_integer), /* for 'd', 'i' */ \
388 REF (form_unsigned), /* for 'u' */ \
389 REF (form_octal), /* for 'o' */ \
390 REF (form_hexa), /* for 'X', 'x' */ \
391 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
392 REF (form_character), /* for 'c' */ \
393 REF (form_string), /* for 's' */ \
394 REF (form_pointer), /* for 'p' */ \
395 REF (form_number), /* for 'n' */ \
396 REF (form_strerror) /* for 'm' */ \
400 #define process_arg(fspec) \
401 /* Start real work. We know about all flag and modifiers and \
402 now process the wanted format specifier. */ \
403 LABEL (form_percent): \
404 /* Write a literal "%". */ \
405 outchar ('%'); \
406 break; \
408 LABEL (form_integer): \
409 /* Signed decimal integer. */ \
410 base = 10; \
412 if (is_longlong) \
414 long long int signed_number; \
416 if (fspec == NULL) \
417 signed_number = va_arg (ap, long long int); \
418 else \
419 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
421 is_negative = signed_number < 0; \
422 number.longlong = is_negative ? (- signed_number) : signed_number; \
424 goto LABEL (longlong_number); \
426 else \
428 long int signed_number; \
430 if (fspec == NULL) \
431 if (is_long) \
432 signed_number = va_arg (ap, long int); \
433 else /* `short int' will be promoted to `int'. */ \
434 signed_number = va_arg (ap, int); \
435 else \
436 if (is_long) \
437 signed_number = args_value[fspec->data_arg].pa_long_int; \
438 else \
439 signed_number = args_value[fspec->data_arg].pa_int; \
441 is_negative = signed_number < 0; \
442 number.word = is_negative ? (- signed_number) : signed_number; \
444 goto LABEL (number); \
446 /* NOTREACHED */ \
448 LABEL (form_unsigned): \
449 /* Unsigned decimal integer. */ \
450 base = 10; \
451 goto LABEL (unsigned_number); \
452 /* NOTREACHED */ \
454 LABEL (form_octal): \
455 /* Unsigned octal integer. */ \
456 base = 8; \
457 goto LABEL (unsigned_number); \
458 /* NOTREACHED */ \
460 LABEL (form_hexa): \
461 /* Unsigned hexadecimal integer. */ \
462 base = 16; \
464 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
466 /* ANSI specifies the `+' and ` ' flags only for signed \
467 conversions. */ \
468 is_negative = 0; \
469 showsign = 0; \
470 space = 0; \
472 if (is_longlong) \
474 if (fspec == NULL) \
475 number.longlong = va_arg (ap, unsigned long long int); \
476 else \
477 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
479 LABEL (longlong_number): \
480 if (prec < 0) \
481 /* Supply a default precision if none was given. */ \
482 prec = 1; \
483 else \
484 /* We have to take care for the '0' flag. If a precision \
485 is given it must be ignored. */ \
486 pad = ' '; \
488 /* If the precision is 0 and the number is 0 nothing has to \
489 be written for the number. */ \
490 if (prec == 0 && number.longlong == 0) \
491 string = workend; \
492 else \
494 /* Put the number in WORK. */ \
495 string = _itoa (number.longlong, workend + 1, base, \
496 spec == 'X'); \
497 string -= 1; \
498 if (group && grouping) \
499 string = group_number (string, workend, grouping, \
500 thousands_sep); \
502 /* Simply further test for num != 0. */ \
503 number.word = number.longlong != 0; \
505 else \
507 if (fspec == NULL) \
508 if (is_long) \
509 number.word = va_arg (ap, unsigned long int); \
510 else if (!is_short) \
511 number.word = va_arg (ap, unsigned int); \
512 else \
513 number.word = (unsigned short int) va_arg (ap, unsigned int); \
514 else \
515 if (is_long) \
516 number.word = args_value[fspec->data_arg].pa_u_long_int; \
517 else if (!is_short) \
518 number.word = args_value[fspec->data_arg].pa_u_int; \
519 else \
520 number.word = (unsigned short int) \
521 args_value[fspec->data_arg].pa_u_short_int; \
523 LABEL (number): \
524 if (prec < 0) \
525 /* Supply a default precision if none was given. */ \
526 prec = 1; \
527 else \
528 /* We have to take care for the '0' flag. If a precision \
529 is given it must be ignored. */ \
530 pad = ' '; \
532 /* If the precision is 0 and the number is 0 nothing has to \
533 be written for the number. */ \
534 if (prec == 0 && number.word == 0) \
535 string = workend; \
536 else \
538 /* Put the number in WORK. */ \
539 string = _itoa_word (number.word, workend + 1, base, \
540 spec == 'X'); \
541 string -= 1; \
542 if (group && grouping) \
543 string = group_number (string, workend, grouping, \
544 thousands_sep); \
548 prec -= workend - string; \
550 if (prec > 0) \
551 /* Add zeros to the precision. */ \
552 while (prec-- > 0) \
553 *string-- = '0'; \
554 else if (number.word != 0 && alt && base == 8) \
555 /* Add octal marker. */ \
556 *string-- = '0'; \
558 if (!left) \
560 width -= workend - string; \
562 if (number.word != 0 && alt && base == 16) \
563 /* Account for 0X hex marker. */ \
564 width -= 2; \
566 if (is_negative || showsign || space) \
567 --width; \
569 if (pad == '0') \
571 while (width-- > 0) \
572 *string-- = '0'; \
574 if (number.word != 0 && alt && base == 16) \
576 *string-- = spec; \
577 *string-- = '0'; \
580 if (is_negative) \
581 *string-- = '-'; \
582 else if (showsign) \
583 *string-- = '+'; \
584 else if (space) \
585 *string-- = ' '; \
587 else \
589 if (number.word != 0 && alt && base == 16) \
591 *string-- = spec; \
592 *string-- = '0'; \
595 if (is_negative) \
596 *string-- = '-'; \
597 else if (showsign) \
598 *string-- = '+'; \
599 else if (space) \
600 *string-- = ' '; \
602 while (width-- > 0) \
603 *string-- = ' '; \
606 outstring (string + 1, workend - string); \
608 break; \
610 else \
612 if (number.word != 0 && alt && base == 16) \
614 *string-- = spec; \
615 *string-- = '0'; \
618 if (is_negative) \
619 *string-- = '-'; \
620 else if (showsign) \
621 *string-- = '+'; \
622 else if (space) \
623 *string-- = ' '; \
625 width -= workend - string; \
626 outstring (string + 1, workend - string); \
628 PAD (' '); \
629 break; \
632 LABEL (form_float): \
634 /* Floating-point number. This is handled by printf_fp.c. */ \
635 extern int __printf_fp __P ((FILE *, const struct printf_info *, \
636 const void **const)); \
637 const void *ptr; \
638 int function_done; \
640 if (fspec == NULL) \
642 struct printf_info info = { prec: prec, \
643 width: width, \
644 spec: spec, \
645 is_long_double: is_long_double, \
646 is_short: is_short, \
647 is_long: is_long, \
648 alt: alt, \
649 space: space, \
650 left: left, \
651 showsign: showsign, \
652 group: group, \
653 pad: pad, \
654 extra: 0 }; \
656 if (is_long_double) \
657 the_arg.pa_long_double = va_arg (ap, long double); \
658 else \
659 the_arg.pa_double = va_arg (ap, double); \
660 ptr = (const void *) &the_arg; \
662 function_done = __printf_fp (s, &info, &ptr); \
664 else \
666 ptr = (const void *) &args_value[fspec->data_arg]; \
668 function_done = __printf_fp (s, &fspec->info, &ptr); \
671 if (function_done < 0) \
672 /* Error in print handler. */ \
673 return -1; \
675 done += function_done; \
677 break; \
679 LABEL (form_character): \
680 /* Character. */ \
681 --width; /* Account for the character itself. */ \
682 if (!left) \
683 PAD (' '); \
684 if (fspec == NULL) \
685 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
686 else \
687 outchar ((unsigned char) args_value[fspec->data_arg].pa_char); \
688 if (left) \
689 PAD (' '); \
690 break; \
692 LABEL (form_string): \
694 size_t len; \
696 /* The string argument could in fact be `char *' or `wchar_t *'. \
697 But this should not make a difference here. */ \
698 if (fspec == NULL) \
699 string = (char *) va_arg (ap, const char *); \
700 else \
701 string = (char *) args_value[fspec->data_arg].pa_string; \
703 /* Entry point for printing other strings. */ \
704 LABEL (print_string): \
706 if (string == NULL) \
708 /* Write "(null)" if there's space. */ \
709 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
711 string = (char *) null; \
712 len = sizeof (null) - 1; \
714 else \
716 string = (char *) ""; \
717 len = 0; \
720 else if (!is_long) \
722 if (prec != -1) \
724 /* Search for the end of the string, but don't search past \
725 the length specified by the precision. */ \
726 const char *end = memchr (string, '\0', prec); \
727 if (end) \
728 len = end - string; \
729 else \
730 len = prec; \
732 else \
733 len = strlen (string); \
735 else \
737 const wchar_t *s2 = (const wchar_t *) string; \
738 mbstate_t mbstate = 0; \
740 len = wcsrtombs (NULL, &s2, prec != -1 ? prec : UINT_MAX, \
741 &mbstate); \
742 if (len == (size_t) -1) \
743 /* Illegal wide-character string. */ \
744 return -1; \
746 s2 = (const wchar_t *) string; \
747 mbstate = 0; \
748 string = alloca (len + 1); \
749 (void) wcsrtombs (string, &s2, prec != -1 ? prec : UINT_MAX, \
750 &mbstate); \
753 if ((width -= len) < 0) \
755 outstring (string, len); \
756 break; \
759 if (!left) \
760 PAD (' '); \
761 outstring (string, len); \
762 if (left) \
763 PAD (' '); \
765 break; \
767 LABEL (form_pointer): \
768 /* Generic pointer. */ \
770 const void *ptr; \
771 if (fspec == NULL) \
772 ptr = va_arg (ap, void *); \
773 else \
774 ptr = args_value[fspec->data_arg].pa_pointer; \
775 if (ptr != NULL) \
777 /* If the pointer is not NULL, write it as a %#x spec. */ \
778 base = 16; \
779 number.word = (unsigned long int) ptr; \
780 is_negative = 0; \
781 alt = 1; \
782 group = 0; \
783 spec = 'x'; \
784 goto LABEL (number); \
786 else \
788 /* Write "(nil)" for a nil pointer. */ \
789 string = (char *) "(nil)"; \
790 /* Make sure the full string "(nil)" is printed. */ \
791 if (prec < 5) \
792 prec = 5; \
793 is_long = 0; /* This is no wide-char string. */ \
794 goto LABEL (print_string); \
797 /* NOTREACHED */ \
799 LABEL (form_number): \
800 /* Answer the count of characters written. */ \
801 if (fspec == NULL) \
802 if (is_longlong) \
803 *(long long int *) va_arg (ap, void *) = done; \
804 else if (is_long) \
805 *(long int *) va_arg (ap, void *) = done; \
806 else if (!is_short) \
807 *(int *) va_arg (ap, void *) = done; \
808 else \
809 *(short int *) va_arg (ap, void *) = done; \
810 else \
811 if (is_longlong) \
812 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
813 else if (is_long) \
814 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
815 else if (!is_short) \
816 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
817 else \
818 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
819 break; \
821 LABEL (form_strerror): \
822 /* Print description of error ERRNO. */ \
824 extern char *_strerror_internal __P ((int, char *buf, size_t)); \
826 string = (char *) \
827 _strerror_internal (errno, work_buffer, sizeof work_buffer); \
829 is_long = 0; /* This is no wide-char string. */ \
830 goto LABEL (print_string)
833 /* Sanity check of arguments. */
834 ARGCHECK (s, format);
836 if (UNBUFFERED_P (s))
837 /* Use a helper function which will allocate a local temporary buffer
838 for the stream and then call us again. */
839 return buffered_vfprintf (s, format, ap);
841 /* Initialize local variables. */
842 done = 0;
843 grouping = (const char *) -1;
844 mbstate = 0;
845 ap_save = ap;
846 nspecs_done = 0;
848 /* Find the first format specifier. */
849 f = lead_str_end = find_spec (format, &mbstate);
851 /* Lock stream. */
852 flockfile (s);
854 /* Write the literal text before the first format. */
855 outstring ((const UCHAR_T *) format,
856 lead_str_end - (const UCHAR_T *) format);
858 /* If we only have to print a simple string, return now. */
859 if (*f == L_('\0'))
861 funlockfile (s);
862 return done;
865 /* Process whole format string. */
868 #define REF(Name) &&do_##Name
869 #define LABEL(Name) do_##Name
870 STEP0_3_TABLE;
871 STEP4_TABLE;
873 union printf_arg *args_value; /* This is not used here but ... */
874 int is_negative; /* Flag for negative number. */
875 union
877 unsigned long long int longlong;
878 unsigned long int word;
879 } number;
880 int base;
881 union printf_arg the_arg;
882 char *string; /* Pointer to argument string. */
883 int alt = 0; /* Alternate format. */
884 int space = 0; /* Use space prefix if no sign is needed. */
885 int left = 0; /* Left-justify output. */
886 int showsign = 0; /* Always begin with plus or minus sign. */
887 int group = 0; /* Print numbers according grouping rules. */
888 int is_long_double = 0; /* Argument is long double/ long long int. */
889 int is_short = 0; /* Argument is long int. */
890 int is_long = 0; /* Argument is short int. */
891 int width = 0; /* Width of output; 0 means none specified. */
892 int prec = -1; /* Precision of output; -1 means none specified. */
893 char pad = ' '; /* Padding character. */
894 CHAR_T spec;
896 /* Get current character in format string. */
897 JUMP (*++f, step0_jumps);
899 /* ' ' flag. */
900 LABEL (flag_space):
901 space = 1;
902 JUMP (*++f, step0_jumps);
904 /* '+' flag. */
905 LABEL (flag_plus):
906 showsign = 1;
907 JUMP (*++f, step0_jumps);
909 /* The '-' flag. */
910 LABEL (flag_minus):
911 left = 1;
912 pad = L_(' ');
913 JUMP (*++f, step0_jumps);
915 /* The '#' flag. */
916 LABEL (flag_hash):
917 alt = 1;
918 JUMP (*++f, step0_jumps);
920 /* The '0' flag. */
921 LABEL (flag_zero):
922 if (!left)
923 pad = L_('0');
924 JUMP (*++f, step0_jumps);
926 /* The '\'' flag. */
927 LABEL (flag_quote):
928 group = 1;
930 /* XXX Completely wrong. Use wctob. */
931 if (grouping == (const char *) -1)
933 /* Figure out the thousands separator character. */
934 if (mbtowc (&thousands_sep,
935 _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
936 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
937 thousands_sep = (wchar_t)
938 *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
939 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
940 if (*grouping == '\0' || *grouping == CHAR_MAX
941 || thousands_sep == L'\0')
942 grouping = NULL;
944 JUMP (*++f, step0_jumps);
946 /* Get width from argument. */
947 LABEL (width_asterics):
949 const UCHAR_T *tmp; /* Temporary value. */
951 tmp = ++f;
952 if (ISDIGIT (*tmp) && read_int (&tmp) && *tmp == L_('$'))
953 /* The width comes from a positional parameter. */
954 goto do_positional;
956 width = va_arg (ap, int);
958 /* Negative width means left justified. */
959 if (width < 0)
961 width = -width;
962 pad = L_(' ');
963 left = 1;
966 JUMP (*f, step1_jumps);
968 /* Given width in format string. */
969 LABEL (width):
970 width = read_int (&f);
971 if (*f == L_('$'))
972 /* Oh, oh. The argument comes from a positional parameter. */
973 goto do_positional;
974 JUMP (*f, step1_jumps);
976 LABEL (precision):
977 ++f;
978 if (*f == L_('*'))
980 const UCHAR_T *tmp; /* Temporary value. */
982 tmp = ++f;
983 if (ISDIGIT (*tmp) && read_int (&tmp) > 0 && *tmp == L_('$'))
984 /* The precision comes from a positional parameter. */
985 goto do_positional;
987 prec = va_arg (ap, int);
989 /* If the precision is negative the precision is omitted. */
990 if (prec < 0)
991 prec = -1;
993 else if (ISDIGIT (*f))
994 prec = read_int (&f);
995 else
996 prec = 0;
997 JUMP (*f, step2_jumps);
999 /* Process 'h' modifier. No other modifier is allowed to
1000 follow. */
1001 LABEL (mod_half):
1002 is_short = 1;
1003 JUMP (*++f, step4_jumps);
1005 /* Process 'l' modifier. There might another 'l' follow. */
1006 LABEL (mod_long):
1007 is_long = 1;
1008 JUMP (*++f, step3_jumps);
1010 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1011 allowed to follow. */
1012 LABEL (mod_longlong):
1013 is_long_double = 1;
1014 JUMP (*++f, step4_jumps);
1016 LABEL (mod_size_t):
1017 is_longlong = sizeof (size_t) > sizeof (unsigned long int);
1018 is_long = sizeof (size_t) > sizeof (unsigned int);
1019 JUMP (*++f, step4_jumps);
1022 /* Process current format. */
1023 while (1)
1025 process_arg (((struct printf_spec *) NULL));
1027 LABEL (form_unknown):
1028 if (spec == L_('\0'))
1030 /* The format string ended before the specifier is complete. */
1031 funlockfile (s);
1032 return -1;
1035 /* If we are in the fast loop force entering the complicated
1036 one. */
1037 goto do_positional;
1040 /* Look for next format specifier. */
1041 f = find_spec ((end_of_spec = ++f), &mbstate);
1043 /* Write the following constant string. */
1044 outstring (end_of_spec, f - end_of_spec);
1046 while (*f != L_('\0'));
1048 /* Unlock stream. */
1049 funlockfile (s);
1051 /* We processed the whole format without any positional parameters. */
1052 return done;
1054 /* Here starts the more complex loop to handle positional parameters. */
1055 do_positional:
1057 /* Array with information about the needed arguments. This has to
1058 be dynamically extendable. */
1059 size_t nspecs = 0;
1060 size_t nspecs_max = 32; /* A more or less arbitrary start value. */
1061 struct printf_spec *specs
1062 = alloca (nspecs_max * sizeof (struct printf_spec));
1064 /* The number of arguments the format string requests. This will
1065 determine the size of the array needed to store the argument
1066 attributes. */
1067 size_t nargs = 0;
1068 int *args_type;
1069 union printf_arg *args_value;
1071 /* Positional parameters refer to arguments directly. This could
1072 also determine the maximum number of arguments. Track the
1073 maximum number. */
1074 size_t max_ref_arg = 0;
1076 /* Just a counter. */
1077 int cnt;
1080 if (grouping == (const char *) -1)
1082 /* XXX Use wctob. But this is incompatible for now. */
1083 /* Figure out the thousands separator character. */
1084 if (mbtowc (&thousands_sep,
1085 _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
1086 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
1087 thousands_sep = (wchar_t) *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1088 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1089 if (*grouping == '\0' || *grouping == CHAR_MAX
1090 || thousands_sep == L'\0')
1091 grouping = NULL;
1094 for (f = lead_str_end; *f != '\0'; f = specs[nspecs++].next_fmt)
1096 if (nspecs >= nspecs_max)
1098 /* Extend the array of format specifiers. */
1099 struct printf_spec *old = specs;
1101 nspecs_max *= 2;
1102 specs = alloca (nspecs_max * sizeof (struct printf_spec));
1104 if (specs == &old[nspecs])
1105 /* Stack grows up, OLD was the last thing allocated;
1106 extend it. */
1107 nspecs_max += nspecs_max / 2;
1108 else
1110 /* Copy the old array's elements to the new space. */
1111 memcpy (specs, old, nspecs * sizeof (struct printf_spec));
1112 if (old == &specs[nspecs])
1113 /* Stack grows down, OLD was just below the new
1114 SPECS. We can use that space when the new space
1115 runs out. */
1116 nspecs_max += nspecs_max / 2;
1120 /* Parse the format specifier. */
1121 nargs += parse_one_spec (f, nargs, &specs[nspecs], &max_ref_arg, NULL);
1124 /* Determine the number of arguments the format string consumes. */
1125 nargs = MAX (nargs, max_ref_arg);
1127 /* Allocate memory for the argument descriptions. */
1128 args_type = alloca (nargs * sizeof (int));
1129 memset (args_type, 0, nargs * sizeof (int));
1130 args_value = alloca (nargs * sizeof (union printf_arg));
1132 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1133 still zero after this loop, format is invalid. For now we
1134 simply use 0 as the value. */
1136 /* Fill in the types of all the arguments. */
1137 for (cnt = 0; cnt < nspecs; ++cnt)
1139 /* If the width is determined by an argument this is an int. */
1140 if (specs[cnt].width_arg != -1)
1141 args_type[specs[cnt].width_arg] = PA_INT;
1143 /* If the precision is determined by an argument this is an int. */
1144 if (specs[cnt].prec_arg != -1)
1145 args_type[specs[cnt].prec_arg] = PA_INT;
1147 switch (specs[cnt].ndata_args)
1149 case 0: /* No arguments. */
1150 break;
1151 case 1: /* One argument; we already have the type. */
1152 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1153 break;
1154 default:
1155 /* We have more than one argument for this format spec.
1156 We must call the arginfo function again to determine
1157 all the types. */
1158 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1159 (&specs[cnt].info,
1160 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg]);
1161 break;
1165 /* Now we know all the types and the order. Fill in the argument
1166 values. */
1167 for (cnt = 0, ap = ap_save; cnt < nargs; ++cnt)
1168 switch (args_type[cnt])
1170 #define T(tag, mem, type) \
1171 case tag: \
1172 args_value[cnt].mem = va_arg (ap, type); \
1173 break
1175 T (PA_CHAR, pa_char, int); /* Promoted. */
1176 T (PA_INT|PA_FLAG_SHORT, pa_short_int, int); /* Promoted. */
1177 T (PA_INT, pa_int, int);
1178 T (PA_INT|PA_FLAG_LONG, pa_long_int, long int);
1179 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1180 T (PA_FLOAT, pa_float, double); /* Promoted. */
1181 T (PA_DOUBLE, pa_double, double);
1182 T (PA_DOUBLE|PA_FLAG_LONG_DOUBLE, pa_long_double, long double);
1183 T (PA_STRING, pa_string, const char *);
1184 T (PA_POINTER, pa_pointer, void *);
1185 #undef T
1186 default:
1187 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1188 args_value[cnt].pa_pointer = va_arg (ap, void *);
1189 else
1190 args_value[cnt].pa_long_double = 0.0;
1191 break;
1194 /* Now walk through all format specifiers and process them. */
1195 for (; nspecs_done < nspecs; ++nspecs_done)
1197 #undef REF
1198 #define REF(Name) &&do2_##Name
1199 #undef LABEL
1200 #define LABEL(Name) do2_##Name
1201 STEP4_TABLE;
1203 int is_negative;
1204 union
1206 unsigned long long int longlong;
1207 unsigned long int word;
1208 } number;
1209 int base;
1210 union printf_arg the_arg;
1211 char *string; /* Pointer to argument string. */
1213 /* Fill variables from values in struct. */
1214 int alt = specs[nspecs_done].info.alt;
1215 int space = specs[nspecs_done].info.space;
1216 int left = specs[nspecs_done].info.left;
1217 int showsign = specs[nspecs_done].info.showsign;
1218 int group = specs[nspecs_done].info.group;
1219 int is_long_double = specs[nspecs_done].info.is_long_double;
1220 int is_short = specs[nspecs_done].info.is_short;
1221 int is_long = specs[nspecs_done].info.is_long;
1222 int width = specs[nspecs_done].info.width;
1223 int prec = specs[nspecs_done].info.prec;
1224 char pad = specs[nspecs_done].info.pad;
1225 CHAR_T spec = specs[nspecs_done].info.spec;
1227 /* Fill in last information. */
1228 if (specs[nspecs_done].width_arg != -1)
1230 /* Extract the field width from an argument. */
1231 specs[nspecs_done].info.width =
1232 args_value[specs[nspecs_done].width_arg].pa_int;
1234 if (specs[nspecs_done].info.width < 0)
1235 /* If the width value is negative left justification is
1236 selected and the value is taken as being positive. */
1238 specs[nspecs_done].info.width *= -1;
1239 left = specs[nspecs_done].info.left = 1;
1241 width = specs[nspecs_done].info.width;
1244 if (specs[nspecs_done].prec_arg != -1)
1246 /* Extract the precision from an argument. */
1247 specs[nspecs_done].info.prec =
1248 args_value[specs[nspecs_done].prec_arg].pa_int;
1250 if (specs[nspecs_done].info.prec < 0)
1251 /* If the precision is negative the precision is
1252 omitted. */
1253 specs[nspecs_done].info.prec = -1;
1255 prec = specs[nspecs_done].info.prec;
1258 /* Process format specifiers. */
1259 while (1)
1261 JUMP (spec, step4_jumps);
1263 process_arg ((&specs[nspecs_done]));
1265 LABEL (form_unknown):
1267 extern printf_function **__printf_function_table;
1268 int function_done;
1269 printf_function *function;
1270 unsigned int i;
1271 const void **ptr;
1273 function =
1274 (__printf_function_table == NULL ? NULL :
1275 __printf_function_table[specs[nspecs_done].info.spec]);
1277 if (function == NULL)
1278 function = &printf_unknown;
1280 ptr = alloca (specs[nspecs_done].ndata_args
1281 * sizeof (const void *));
1283 /* Fill in an array of pointers to the argument values. */
1284 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
1285 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1287 /* Call the function. */
1288 function_done = (*function) (s, &specs[nspecs_done].info, ptr);
1290 /* If an error occured we don't have information about #
1291 of chars. */
1292 if (function_done < 0)
1294 funlockfile (s);
1295 return -1;
1298 done += function_done;
1300 break;
1303 /* Write the following constant string. */
1304 outstring (specs[nspecs_done].end_of_fmt,
1305 specs[nspecs_done].next_fmt
1306 - specs[nspecs_done].end_of_fmt);
1310 /* Unlock the stream. */
1311 funlockfile (s);
1313 return done;
1316 #ifdef USE_IN_LIBIO
1317 # undef vfprintf
1318 # ifdef strong_alias
1319 /* This is for glibc. */
1320 strong_alias (_IO_vfprintf, vfprintf);
1321 # else
1322 # if defined __ELF__ || defined __GNU_LIBRARY__
1323 # include <gnu-stabs.h>
1324 # ifdef weak_alias
1325 weak_alias (_IO_vfprintf, vfprintf);
1326 # endif
1327 # endif
1328 # endif
1329 #endif
1331 /* Handle an unknown format specifier. This prints out a canonicalized
1332 representation of the format spec itself. */
1333 static int
1334 printf_unknown (FILE *s, const struct printf_info *info,
1335 const void *const *args)
1338 int done = 0;
1339 char work_buffer[BUFSIZ];
1340 register char *w;
1342 outchar ('%');
1344 if (info->alt)
1345 outchar ('#');
1346 if (info->group)
1347 outchar ('\'');
1348 if (info->showsign)
1349 outchar ('+');
1350 else if (info->space)
1351 outchar (' ');
1352 if (info->left)
1353 outchar ('-');
1354 if (info->pad == '0')
1355 outchar ('0');
1357 if (info->width != 0)
1359 w = _itoa_word (info->width, workend + 1, 10, 0);
1360 while (++w <= workend)
1361 outchar (*w);
1364 if (info->prec != -1)
1366 outchar ('.');
1367 w = _itoa_word (info->prec, workend + 1, 10, 0);
1368 while (++w <= workend)
1369 outchar (*w);
1372 if (info->spec != '\0')
1373 outchar (info->spec);
1375 return done;
1378 /* Group the digits according to the grouping rules of the current locale.
1379 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1380 static char *
1381 group_number (CHAR_T *w, CHAR_T *rear_ptr, const CHAR_T *grouping,
1382 wchar_t thousands_sep)
1384 int len;
1385 char *src, *s;
1387 /* We treat all negative values like CHAR_MAX. */
1389 if (*grouping == CHAR_MAX || *grouping < 0)
1390 /* No grouping should be done. */
1391 return w;
1393 len = *grouping;
1395 /* Copy existing string so that nothing gets overwritten. */
1396 src = (char *) alloca (rear_ptr - w);
1397 memcpy (src, w + 1, rear_ptr - w);
1398 s = &src[rear_ptr - w - 1];
1399 w = rear_ptr;
1401 /* Process all characters in the string. */
1402 while (s >= src)
1404 *w-- = *s--;
1406 if (--len == 0 && s >= src)
1408 /* A new group begins. */
1409 *w-- = thousands_sep;
1411 len = *grouping++;
1412 if (*grouping == '\0')
1413 /* The previous grouping repeats ad infinitum. */
1414 --grouping;
1415 else if (*grouping == CHAR_MAX || *grouping < 0)
1417 /* No further grouping to be done.
1418 Copy the rest of the number. */
1420 *w-- = *s--;
1421 while (s >= src);
1422 break;
1426 return w;
1429 #ifdef USE_IN_LIBIO
1430 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
1431 struct helper_file
1433 struct _IO_FILE_plus _f;
1434 _IO_FILE *_put_stream;
1437 static int
1438 _IO_helper_overflow (_IO_FILE *s, int c)
1440 _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
1441 int used = s->_IO_write_ptr - s->_IO_write_base;
1442 if (used)
1444 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
1445 s->_IO_write_ptr -= written;
1447 return PUTC (c, s);
1450 static const struct _IO_jump_t _IO_helper_jumps =
1452 JUMP_INIT_DUMMY,
1453 JUMP_INIT (finish, _IO_default_finish),
1454 JUMP_INIT (overflow, _IO_helper_overflow),
1455 JUMP_INIT (underflow, _IO_default_underflow),
1456 JUMP_INIT (uflow, _IO_default_uflow),
1457 JUMP_INIT (pbackfail, _IO_default_pbackfail),
1458 JUMP_INIT (xsputn, _IO_default_xsputn),
1459 JUMP_INIT (xsgetn, _IO_default_xsgetn),
1460 JUMP_INIT (seekoff, _IO_default_seekoff),
1461 JUMP_INIT (seekpos, _IO_default_seekpos),
1462 JUMP_INIT (setbuf, _IO_default_setbuf),
1463 JUMP_INIT (sync, _IO_default_sync),
1464 JUMP_INIT (doallocate, _IO_default_doallocate),
1465 JUMP_INIT (read, _IO_default_read),
1466 JUMP_INIT (write, _IO_default_write),
1467 JUMP_INIT (seek, _IO_default_seek),
1468 JUMP_INIT (close, _IO_default_close),
1469 JUMP_INIT (stat, _IO_default_stat)
1472 static int
1473 buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format,
1474 _IO_va_list args)
1476 char buf[_IO_BUFSIZ];
1477 struct helper_file helper;
1478 register _IO_FILE *hp = (_IO_FILE *) &helper;
1479 int result, to_flush;
1481 /* Initialize helper. */
1482 helper._put_stream = s;
1483 hp->_IO_write_base = buf;
1484 hp->_IO_write_ptr = buf;
1485 hp->_IO_write_end = buf + sizeof buf;
1486 hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS;
1487 _IO_JUMPS (hp) = (struct _IO_jump_t *) &_IO_helper_jumps;
1489 /* Now print to helper instead. */
1490 result = _IO_vfprintf (hp, format, args);
1492 /* Now flush anything from the helper to the S. */
1493 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
1495 if (_IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
1496 return -1;
1499 return result;
1502 #else /* !USE_IN_LIBIO */
1504 static int
1505 buffered_vfprintf (register FILE *s, const CHAR_T *format, va_list args)
1507 char buf[BUFSIZ];
1508 int result;
1510 s->__bufp = s->__buffer = buf;
1511 s->__bufsize = sizeof buf;
1512 s->__put_limit = s->__buffer + s->__bufsize;
1513 s->__get_limit = s->__buffer;
1515 /* Now use buffer to print. */
1516 result = vfprintf (s, format, args);
1518 if (fflush (s) == EOF)
1519 result = -1;
1520 s->__buffer = s->__bufp = s->__get_limit = s->__put_limit = NULL;
1521 s->__bufsize = 0;
1523 return result;
1526 /* Pads string with given number of a specified character.
1527 This code is taken from iopadn.c of the GNU I/O library. */
1528 #define PADSIZE 16
1529 static const CHAR_T blanks[PADSIZE] =
1530 { L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '),
1531 L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' ') };
1532 static const CHAR_T zeroes[PADSIZE] =
1533 { L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'),
1534 L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0') };
1536 ssize_t
1537 #ifndef COMPILE_WPRINTF
1538 __printf_pad (FILE *s, char pad, size_t count)
1539 #else
1540 __wprintf_pad (FILE *s, wchar_t pad, size_t count)
1541 #endif
1543 const CHAR_T *padptr;
1544 register size_t i;
1546 padptr = pad == L_(' ') ? blanks : zeroes;
1548 for (i = count; i >= PADSIZE; i -= PADSIZE)
1549 if (PUT (s, padptr, PADSIZE) != PADSIZE)
1550 return -1;
1551 if (i > 0)
1552 if (PUT (s, padptr, i) != i)
1553 return -1;
1555 return count;
1557 #undef PADSIZE
1558 #endif /* USE_IN_LIBIO */