]> git.ipfire.org Git - thirdparty/glibc.git/blob - stdio-common/vfprintf.c
update from main archvie 961013
[thirdparty/glibc.git] / stdio-common / vfprintf.c
1 /* Copyright (C) 1991, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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. */
18
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"
29
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++.
33
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. */
36
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)
43
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)
64
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
79
80 /* Include the shared code for parsing the format string. */
81 #include "printf-parse.h"
82
83
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) \
100 { \
101 MAYBE_SET_EINVAL; \
102 return -1; \
103 } \
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 \
112 { \
113 /* Check file argument for consistence. */ \
114 if (!__validfp(S) || !S->__mode.__write || Format == NULL) \
115 { \
116 __set_errno (EINVAL); \
117 return -1; \
118 } \
119 if (!S->__seen) \
120 { \
121 if (__flshfp (S, EOF) == EOF) \
122 return -1; \
123 } \
124 } \
125 while (0)
126 # define UNBUFFERED_P(s) ((s)->__buffer == NULL)
127 #endif /* USE_IN_LIBIO */
128
129
130 #define outchar(Ch) \
131 do \
132 { \
133 register const int outc = (Ch); \
134 if (PUTC (outc, s) == EOF) \
135 return -1; \
136 else \
137 ++done; \
138 } \
139 while (0)
140
141 #define outstring(String, Len) \
142 do \
143 { \
144 if (PUT (s, String, Len) != Len) \
145 return -1; \
146 done += Len; \
147 } \
148 while (0)
149
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
154
155 extern void __flockfile (FILE *);
156 weak_extern (__flockfile);
157 extern void __funlockfile (FILE *);
158
159 /* Global variables. */
160 static const char null[] = "(null)";
161
162
163 /* Helper function to provide temporary buffering for unbuffered streams. */
164 static int buffered_vfprintf __P ((FILE *stream, const CHAR_T *fmt, va_list));
165
166 /* Handle unknown format specifier. */
167 static int printf_unknown __P ((FILE *, const struct printf_info *,
168 const void *const *));
169
170 /* Group digits of number string. */
171 static char *group_number __P ((CHAR_T *, CHAR_T *, const CHAR_T *, wchar_t));
172
173
174 /* The function itself. */
175 int
176 vfprintf (FILE *s, const CHAR_T *format, va_list ap)
177 {
178 /* The character used as thousands separator. */
179 wchar_t thousands_sep;
180
181 /* The string describing the size of groups of digits. */
182 const char *grouping;
183
184 /* Place to accumulate the result. */
185 int done;
186
187 /* Current character in format string. */
188 const UCHAR_T *f;
189
190 /* End of leading constant string. */
191 const UCHAR_T *lead_str_end;
192
193 /* Points to next format specifier. */
194 const UCHAR_T *end_of_spec;
195
196 /* Buffer intermediate results. */
197 char work_buffer[1000];
198 #define workend (&work_buffer[sizeof (work_buffer) - 1])
199
200 /* State for restartable multibyte character handling functions. */
201 mbstate_t mbstate;
202
203 /* We have to save the original argument pointer. */
204 va_list ap_save;
205
206 /* Count number of specifiers we already processed. */
207 int nspecs_done;
208
209
210 /* This table maps a character into a number representing a
211 class. In each step there is a destination label for each
212 class. */
213 static const int jump_table[] =
214 {
215 /* ' ' */ 1, 0, 0, /* '#' */ 4,
216 0, /* '%' */ 14, 0, /* '\''*/ 6,
217 0, 0, /* '*' */ 7, /* '+' */ 2,
218 0, /* '-' */ 3, /* '.' */ 9, 0,
219 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
220 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
221 /* '8' */ 8, /* '9' */ 8, 0, 0,
222 0, 0, 0, 0,
223 0, 0, 0, 0,
224 0, /* 'E' */ 19, 0, /* 'G' */ 19,
225 0, 0, 0, 0,
226 /* 'L' */ 12, 0, 0, 0,
227 0, 0, 0, 0,
228 0, 0, 0, 0,
229 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
230 0, 0, 0, 0,
231 0, 0, 0, /* 'c' */ 20,
232 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
233 /* 'h' */ 10, /* 'i' */ 15, 0, 0,
234 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
235 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
236 0, /* 'u' */ 16, 0, 0,
237 /* 'x' */ 18
238 };
239
240 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < ' ' || (Ch) > 'x')
241 #define CHAR_CLASS(Ch) (jump_table[(int) (Ch) - ' '])
242 #define JUMP(ChExpr, table) \
243 do \
244 { \
245 const void *ptr; \
246 spec = (ChExpr); \
247 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
248 : table[CHAR_CLASS (spec)]; \
249 goto *ptr; \
250 } \
251 while (0)
252
253 #define STEP0_3_TABLE \
254 /* Step 0: at the beginning. */ \
255 static const void *step0_jumps[25] = \
256 { \
257 REF (form_unknown), \
258 REF (flag_space), /* for ' ' */ \
259 REF (flag_plus), /* for '+' */ \
260 REF (flag_minus), /* for '-' */ \
261 REF (flag_hash), /* for '<hash>' */ \
262 REF (flag_zero), /* for '0' */ \
263 REF (flag_quote), /* for '\'' */ \
264 REF (width_asterics), /* for '*' */ \
265 REF (width), /* for '1'...'9' */ \
266 REF (precision), /* for '.' */ \
267 REF (mod_half), /* for 'h' */ \
268 REF (mod_long), /* for 'l' */ \
269 REF (mod_longlong), /* for 'L', 'q' */ \
270 REF (mod_size_t), /* for 'Z' */ \
271 REF (form_percent), /* for '%' */ \
272 REF (form_integer), /* for 'd', 'i' */ \
273 REF (form_unsigned), /* for 'u' */ \
274 REF (form_octal), /* for 'o' */ \
275 REF (form_hexa), /* for 'X', 'x' */ \
276 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
277 REF (form_character), /* for 'c' */ \
278 REF (form_string), /* for 's' */ \
279 REF (form_pointer), /* for 'p' */ \
280 REF (form_number), /* for 'n' */ \
281 REF (form_strerror) /* for 'm' */ \
282 }; \
283 /* Step 1: after processing width. */ \
284 static const void *step1_jumps[25] = \
285 { \
286 REF (form_unknown), \
287 REF (form_unknown), /* for ' ' */ \
288 REF (form_unknown), /* for '+' */ \
289 REF (form_unknown), /* for '-' */ \
290 REF (form_unknown), /* for '<hash>' */ \
291 REF (form_unknown), /* for '0' */ \
292 REF (form_unknown), /* for '\'' */ \
293 REF (form_unknown), /* for '*' */ \
294 REF (form_unknown), /* for '1'...'9' */ \
295 REF (precision), /* for '.' */ \
296 REF (mod_half), /* for 'h' */ \
297 REF (mod_long), /* for 'l' */ \
298 REF (mod_longlong), /* for 'L', 'q' */ \
299 REF (mod_size_t), /* for 'Z' */ \
300 REF (form_percent), /* for '%' */ \
301 REF (form_integer), /* for 'd', 'i' */ \
302 REF (form_unsigned), /* for 'u' */ \
303 REF (form_octal), /* for 'o' */ \
304 REF (form_hexa), /* for 'X', 'x' */ \
305 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
306 REF (form_character), /* for 'c' */ \
307 REF (form_string), /* for 's' */ \
308 REF (form_pointer), /* for 'p' */ \
309 REF (form_number), /* for 'n' */ \
310 REF (form_strerror) /* for 'm' */ \
311 }; \
312 /* Step 2: after processing precision. */ \
313 static const void *step2_jumps[25] = \
314 { \
315 REF (form_unknown), \
316 REF (form_unknown), /* for ' ' */ \
317 REF (form_unknown), /* for '+' */ \
318 REF (form_unknown), /* for '-' */ \
319 REF (form_unknown), /* for '<hash>' */ \
320 REF (form_unknown), /* for '0' */ \
321 REF (form_unknown), /* for '\'' */ \
322 REF (form_unknown), /* for '*' */ \
323 REF (form_unknown), /* for '1'...'9' */ \
324 REF (form_unknown), /* for '.' */ \
325 REF (mod_half), /* for 'h' */ \
326 REF (mod_long), /* for 'l' */ \
327 REF (mod_longlong), /* for 'L', 'q' */ \
328 REF (mod_size_t), /* for 'Z' */ \
329 REF (form_percent), /* for '%' */ \
330 REF (form_integer), /* for 'd', 'i' */ \
331 REF (form_unsigned), /* for 'u' */ \
332 REF (form_octal), /* for 'o' */ \
333 REF (form_hexa), /* for 'X', 'x' */ \
334 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
335 REF (form_character), /* for 'c' */ \
336 REF (form_string), /* for 's' */ \
337 REF (form_pointer), /* for 'p' */ \
338 REF (form_number), /* for 'n' */ \
339 REF (form_strerror) /* for 'm' */ \
340 }; \
341 /* Step 3: after processing first 'l' modifier. */ \
342 static const void *step3_jumps[25] = \
343 { \
344 REF (form_unknown), \
345 REF (form_unknown), /* for ' ' */ \
346 REF (form_unknown), /* for '+' */ \
347 REF (form_unknown), /* for '-' */ \
348 REF (form_unknown), /* for '<hash>' */ \
349 REF (form_unknown), /* for '0' */ \
350 REF (form_unknown), /* for '\'' */ \
351 REF (form_unknown), /* for '*' */ \
352 REF (form_unknown), /* for '1'...'9' */ \
353 REF (form_unknown), /* for '.' */ \
354 REF (form_unknown), /* for 'h' */ \
355 REF (mod_longlong), /* for 'l' */ \
356 REF (form_unknown), /* for 'L', 'q' */ \
357 REF (form_unknown), /* for 'Z' */ \
358 REF (form_percent), /* for '%' */ \
359 REF (form_integer), /* for 'd', 'i' */ \
360 REF (form_unsigned), /* for 'u' */ \
361 REF (form_octal), /* for 'o' */ \
362 REF (form_hexa), /* for 'X', 'x' */ \
363 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
364 REF (form_character), /* for 'c' */ \
365 REF (form_string), /* for 's' */ \
366 REF (form_pointer), /* for 'p' */ \
367 REF (form_number), /* for 'n' */ \
368 REF (form_strerror) /* for 'm' */ \
369 }
370
371 #define STEP4_TABLE \
372 /* Step 4: processing format specifier. */ \
373 static const void *step4_jumps[25] = \
374 { \
375 REF (form_unknown), \
376 REF (form_unknown), /* for ' ' */ \
377 REF (form_unknown), /* for '+' */ \
378 REF (form_unknown), /* for '-' */ \
379 REF (form_unknown), /* for '<hash>' */ \
380 REF (form_unknown), /* for '0' */ \
381 REF (form_unknown), /* for '\'' */ \
382 REF (form_unknown), /* for '*' */ \
383 REF (form_unknown), /* for '1'...'9' */ \
384 REF (form_unknown), /* for '.' */ \
385 REF (form_unknown), /* for 'h' */ \
386 REF (form_unknown), /* for 'l' */ \
387 REF (form_unknown), /* for 'L', 'q' */ \
388 REF (form_unknown), /* for 'Z' */ \
389 REF (form_percent), /* for '%' */ \
390 REF (form_integer), /* for 'd', 'i' */ \
391 REF (form_unsigned), /* for 'u' */ \
392 REF (form_octal), /* for 'o' */ \
393 REF (form_hexa), /* for 'X', 'x' */ \
394 REF (form_float), /* for 'E', 'e', 'f', 'G', 'g' */ \
395 REF (form_character), /* for 'c' */ \
396 REF (form_string), /* for 's' */ \
397 REF (form_pointer), /* for 'p' */ \
398 REF (form_number), /* for 'n' */ \
399 REF (form_strerror) /* for 'm' */ \
400 }
401
402
403 #define process_arg(fspec) \
404 /* Start real work. We know about all flags and modifiers and \
405 now process the wanted format specifier. */ \
406 LABEL (form_percent): \
407 /* Write a literal "%". */ \
408 outchar ('%'); \
409 break; \
410 \
411 LABEL (form_integer): \
412 /* Signed decimal integer. */ \
413 base = 10; \
414 \
415 if (is_longlong) \
416 { \
417 long long int signed_number; \
418 \
419 if (fspec == NULL) \
420 signed_number = va_arg (ap, long long int); \
421 else \
422 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
423 \
424 is_negative = signed_number < 0; \
425 number.longlong = is_negative ? (- signed_number) : signed_number; \
426 \
427 goto LABEL (longlong_number); \
428 } \
429 else \
430 { \
431 long int signed_number; \
432 \
433 if (fspec == NULL) \
434 if (is_long) \
435 signed_number = va_arg (ap, long int); \
436 else /* `short int' will be promoted to `int'. */ \
437 signed_number = va_arg (ap, int); \
438 else \
439 if (is_long) \
440 signed_number = args_value[fspec->data_arg].pa_long_int; \
441 else \
442 signed_number = args_value[fspec->data_arg].pa_int; \
443 \
444 is_negative = signed_number < 0; \
445 number.word = is_negative ? (- signed_number) : signed_number; \
446 \
447 goto LABEL (number); \
448 } \
449 /* NOTREACHED */ \
450 \
451 LABEL (form_unsigned): \
452 /* Unsigned decimal integer. */ \
453 base = 10; \
454 goto LABEL (unsigned_number); \
455 /* NOTREACHED */ \
456 \
457 LABEL (form_octal): \
458 /* Unsigned octal integer. */ \
459 base = 8; \
460 goto LABEL (unsigned_number); \
461 /* NOTREACHED */ \
462 \
463 LABEL (form_hexa): \
464 /* Unsigned hexadecimal integer. */ \
465 base = 16; \
466 \
467 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
468 \
469 /* ANSI specifies the `+' and ` ' flags only for signed \
470 conversions. */ \
471 is_negative = 0; \
472 showsign = 0; \
473 space = 0; \
474 \
475 if (is_longlong) \
476 { \
477 if (fspec == NULL) \
478 number.longlong = va_arg (ap, unsigned long long int); \
479 else \
480 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
481 \
482 LABEL (longlong_number): \
483 if (prec < 0) \
484 /* Supply a default precision if none was given. */ \
485 prec = 1; \
486 else \
487 /* We have to take care for the '0' flag. If a precision \
488 is given it must be ignored. */ \
489 pad = ' '; \
490 \
491 /* If the precision is 0 and the number is 0 nothing has to \
492 be written for the number. */ \
493 if (prec == 0 && number.longlong == 0) \
494 string = workend; \
495 else \
496 { \
497 /* Put the number in WORK. */ \
498 string = _itoa (number.longlong, workend + 1, base, \
499 spec == 'X'); \
500 string -= 1; \
501 if (group && grouping) \
502 string = group_number (string, workend, grouping, \
503 thousands_sep); \
504 } \
505 /* Simply further test for num != 0. */ \
506 number.word = number.longlong != 0; \
507 } \
508 else \
509 { \
510 if (fspec == NULL) \
511 if (is_long) \
512 number.word = va_arg (ap, unsigned long int); \
513 else if (!is_short) \
514 number.word = va_arg (ap, unsigned int); \
515 else \
516 number.word = (unsigned short int) va_arg (ap, unsigned int); \
517 else \
518 if (is_long) \
519 number.word = args_value[fspec->data_arg].pa_u_long_int; \
520 else if (!is_short) \
521 number.word = args_value[fspec->data_arg].pa_u_int; \
522 else \
523 number.word = (unsigned short int) \
524 args_value[fspec->data_arg].pa_u_short_int; \
525 \
526 LABEL (number): \
527 if (prec < 0) \
528 /* Supply a default precision if none was given. */ \
529 prec = 1; \
530 else \
531 /* We have to take care for the '0' flag. If a precision \
532 is given it must be ignored. */ \
533 pad = ' '; \
534 \
535 /* If the precision is 0 and the number is 0 nothing has to \
536 be written for the number. */ \
537 if (prec == 0 && number.word == 0) \
538 string = workend; \
539 else \
540 { \
541 /* Put the number in WORK. */ \
542 string = _itoa_word (number.word, workend + 1, base, \
543 spec == 'X'); \
544 string -= 1; \
545 if (group && grouping) \
546 string = group_number (string, workend, grouping, \
547 thousands_sep); \
548 } \
549 } \
550 \
551 prec -= workend - string; \
552 \
553 if (prec > 0) \
554 /* Add zeros to the precision. */ \
555 while (prec-- > 0) \
556 *string-- = '0'; \
557 else if (number.word != 0 && alt && base == 8) \
558 /* Add octal marker. */ \
559 *string-- = '0'; \
560 \
561 if (!left) \
562 { \
563 width -= workend - string; \
564 \
565 if (number.word != 0 && alt && base == 16) \
566 /* Account for 0X hex marker. */ \
567 width -= 2; \
568 \
569 if (is_negative || showsign || space) \
570 --width; \
571 \
572 if (pad == '0') \
573 { \
574 while (width-- > 0) \
575 *string-- = '0'; \
576 \
577 if (number.word != 0 && alt && base == 16) \
578 { \
579 *string-- = spec; \
580 *string-- = '0'; \
581 } \
582 \
583 if (is_negative) \
584 *string-- = '-'; \
585 else if (showsign) \
586 *string-- = '+'; \
587 else if (space) \
588 *string-- = ' '; \
589 } \
590 else \
591 { \
592 if (number.word != 0 && alt && base == 16) \
593 { \
594 *string-- = spec; \
595 *string-- = '0'; \
596 } \
597 \
598 if (is_negative) \
599 *string-- = '-'; \
600 else if (showsign) \
601 *string-- = '+'; \
602 else if (space) \
603 *string-- = ' '; \
604 \
605 while (width-- > 0) \
606 *string-- = ' '; \
607 } \
608 \
609 outstring (string + 1, workend - string); \
610 \
611 break; \
612 } \
613 else \
614 { \
615 if (number.word != 0 && alt && base == 16) \
616 { \
617 *string-- = spec; \
618 *string-- = '0'; \
619 } \
620 \
621 if (is_negative) \
622 *string-- = '-'; \
623 else if (showsign) \
624 *string-- = '+'; \
625 else if (space) \
626 *string-- = ' '; \
627 \
628 width -= workend - string; \
629 outstring (string + 1, workend - string); \
630 \
631 PAD (' '); \
632 break; \
633 } \
634 \
635 LABEL (form_float): \
636 { \
637 /* Floating-point number. This is handled by printf_fp.c. */ \
638 extern int __printf_fp __P ((FILE *, const struct printf_info *, \
639 const void **const)); \
640 const void *ptr; \
641 int function_done; \
642 \
643 if (fspec == NULL) \
644 { \
645 struct printf_info info = { prec: prec, \
646 width: width, \
647 spec: spec, \
648 is_long_double: is_long_double, \
649 is_short: is_short, \
650 is_long: is_long, \
651 alt: alt, \
652 space: space, \
653 left: left, \
654 showsign: showsign, \
655 group: group, \
656 pad: pad, \
657 extra: 0 }; \
658 \
659 if (is_long_double) \
660 the_arg.pa_long_double = va_arg (ap, long double); \
661 else \
662 the_arg.pa_double = va_arg (ap, double); \
663 ptr = (const void *) &the_arg; \
664 \
665 function_done = __printf_fp (s, &info, &ptr); \
666 } \
667 else \
668 { \
669 ptr = (const void *) &args_value[fspec->data_arg]; \
670 \
671 function_done = __printf_fp (s, &fspec->info, &ptr); \
672 } \
673 \
674 if (function_done < 0) \
675 /* Error in print handler. */ \
676 return -1; \
677 \
678 done += function_done; \
679 } \
680 break; \
681 \
682 LABEL (form_character): \
683 /* Character. */ \
684 --width; /* Account for the character itself. */ \
685 if (!left) \
686 PAD (' '); \
687 if (fspec == NULL) \
688 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
689 else \
690 outchar ((unsigned char) args_value[fspec->data_arg].pa_char); \
691 if (left) \
692 PAD (' '); \
693 break; \
694 \
695 LABEL (form_string): \
696 { \
697 size_t len; \
698 \
699 /* The string argument could in fact be `char *' or `wchar_t *'. \
700 But this should not make a difference here. */ \
701 if (fspec == NULL) \
702 string = (char *) va_arg (ap, const char *); \
703 else \
704 string = (char *) args_value[fspec->data_arg].pa_string; \
705 \
706 /* Entry point for printing other strings. */ \
707 LABEL (print_string): \
708 \
709 if (string == NULL) \
710 { \
711 /* Write "(null)" if there's space. */ \
712 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
713 { \
714 string = (char *) null; \
715 len = sizeof (null) - 1; \
716 } \
717 else \
718 { \
719 string = (char *) ""; \
720 len = 0; \
721 } \
722 } \
723 else if (!is_long) \
724 { \
725 if (prec != -1) \
726 { \
727 /* Search for the end of the string, but don't search past \
728 the length specified by the precision. */ \
729 const char *end = memchr (string, '\0', prec); \
730 if (end) \
731 len = end - string; \
732 else \
733 len = prec; \
734 } \
735 else \
736 len = strlen (string); \
737 } \
738 else \
739 { \
740 const wchar_t *s2 = (const wchar_t *) string; \
741 mbstate_t mbstate; \
742 \
743 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
744 if (len == (size_t) -1) \
745 /* Illegal wide-character string. */ \
746 return -1; \
747 \
748 s2 = (const wchar_t *) string; \
749 string = alloca (len + 1); \
750 (void) __wcsrtombs (string, &s2, prec != -1 ? prec : UINT_MAX, \
751 &mbstate); \
752 } \
753 \
754 if ((width -= len) < 0) \
755 { \
756 outstring (string, len); \
757 break; \
758 } \
759 \
760 if (!left) \
761 PAD (' '); \
762 outstring (string, len); \
763 if (left) \
764 PAD (' '); \
765 } \
766 break; \
767 \
768 LABEL (form_pointer): \
769 /* Generic pointer. */ \
770 { \
771 const void *ptr; \
772 if (fspec == NULL) \
773 ptr = va_arg (ap, void *); \
774 else \
775 ptr = args_value[fspec->data_arg].pa_pointer; \
776 if (ptr != NULL) \
777 { \
778 /* If the pointer is not NULL, write it as a %#x spec. */ \
779 base = 16; \
780 number.word = (unsigned long int) ptr; \
781 is_negative = 0; \
782 alt = 1; \
783 group = 0; \
784 spec = 'x'; \
785 goto LABEL (number); \
786 } \
787 else \
788 { \
789 /* Write "(nil)" for a nil pointer. */ \
790 string = (char *) "(nil)"; \
791 /* Make sure the full string "(nil)" is printed. */ \
792 if (prec < 5) \
793 prec = 5; \
794 is_long = 0; /* This is no wide-char string. */ \
795 goto LABEL (print_string); \
796 } \
797 } \
798 /* NOTREACHED */ \
799 \
800 LABEL (form_number): \
801 /* Answer the count of characters written. */ \
802 if (fspec == NULL) \
803 if (is_longlong) \
804 *(long long int *) va_arg (ap, void *) = done; \
805 else if (is_long) \
806 *(long int *) va_arg (ap, void *) = done; \
807 else if (!is_short) \
808 *(int *) va_arg (ap, void *) = done; \
809 else \
810 *(short int *) va_arg (ap, void *) = done; \
811 else \
812 if (is_longlong) \
813 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
814 else if (is_long) \
815 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
816 else if (!is_short) \
817 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
818 else \
819 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
820 break; \
821 \
822 LABEL (form_strerror): \
823 /* Print description of error ERRNO. */ \
824 { \
825 extern char *_strerror_internal __P ((int, char *buf, size_t)); \
826 \
827 string = (char *) \
828 _strerror_internal (errno, work_buffer, sizeof work_buffer); \
829 } \
830 is_long = 0; /* This is no wide-char string. */ \
831 goto LABEL (print_string)
832
833
834 /* Sanity check of arguments. */
835 ARGCHECK (s, format);
836
837 if (UNBUFFERED_P (s))
838 /* Use a helper function which will allocate a local temporary buffer
839 for the stream and then call us again. */
840 return buffered_vfprintf (s, format, ap);
841
842 /* Initialize local variables. */
843 done = 0;
844 grouping = (const char *) -1;
845 ap_save = ap;
846 nspecs_done = 0;
847
848 /* Find the first format specifier. */
849 f = lead_str_end = find_spec (format, &mbstate);
850
851 /* Lock stream. */
852 __libc_cleanup_region_start ((void (*) (void *)) &__funlockfile, s);
853 __flockfile (s);
854
855 /* Write the literal text before the first format. */
856 outstring ((const UCHAR_T *) format,
857 lead_str_end - (const UCHAR_T *) format);
858
859 /* If we only have to print a simple string, return now. */
860 if (*f == L_('\0'))
861 goto all_done;
862
863 /* Process whole format string. */
864 do
865 {
866 #define REF(Name) &&do_##Name
867 #define LABEL(Name) do_##Name
868 STEP0_3_TABLE;
869 STEP4_TABLE;
870
871 union printf_arg *args_value; /* This is not used here but ... */
872 int is_negative; /* Flag for negative number. */
873 union
874 {
875 unsigned long long int longlong;
876 unsigned long int word;
877 } number;
878 int base;
879 union printf_arg the_arg;
880 char *string; /* Pointer to argument string. */
881 int alt = 0; /* Alternate format. */
882 int space = 0; /* Use space prefix if no sign is needed. */
883 int left = 0; /* Left-justify output. */
884 int showsign = 0; /* Always begin with plus or minus sign. */
885 int group = 0; /* Print numbers according grouping rules. */
886 int is_long_double = 0; /* Argument is long double/ long long int. */
887 int is_short = 0; /* Argument is long int. */
888 int is_long = 0; /* Argument is short int. */
889 int width = 0; /* Width of output; 0 means none specified. */
890 int prec = -1; /* Precision of output; -1 means none specified. */
891 char pad = ' '; /* Padding character. */
892 CHAR_T spec;
893
894 /* Get current character in format string. */
895 JUMP (*++f, step0_jumps);
896
897 /* ' ' flag. */
898 LABEL (flag_space):
899 space = 1;
900 JUMP (*++f, step0_jumps);
901
902 /* '+' flag. */
903 LABEL (flag_plus):
904 showsign = 1;
905 JUMP (*++f, step0_jumps);
906
907 /* The '-' flag. */
908 LABEL (flag_minus):
909 left = 1;
910 pad = L_(' ');
911 JUMP (*++f, step0_jumps);
912
913 /* The '#' flag. */
914 LABEL (flag_hash):
915 alt = 1;
916 JUMP (*++f, step0_jumps);
917
918 /* The '0' flag. */
919 LABEL (flag_zero):
920 if (!left)
921 pad = L_('0');
922 JUMP (*++f, step0_jumps);
923
924 /* The '\'' flag. */
925 LABEL (flag_quote):
926 group = 1;
927
928 /* XXX Completely wrong. Use wctob. */
929 if (grouping == (const char *) -1)
930 {
931 /* Figure out the thousands separator character. */
932 if (mbtowc (&thousands_sep,
933 _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
934 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
935 thousands_sep = (wchar_t)
936 *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
937 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
938 if (*grouping == '\0' || *grouping == CHAR_MAX
939 || thousands_sep == L'\0')
940 grouping = NULL;
941 }
942 JUMP (*++f, step0_jumps);
943
944 /* Get width from argument. */
945 LABEL (width_asterics):
946 {
947 const UCHAR_T *tmp; /* Temporary value. */
948
949 tmp = ++f;
950 if (ISDIGIT (*tmp) && read_int (&tmp) && *tmp == L_('$'))
951 /* The width comes from a positional parameter. */
952 goto do_positional;
953
954 width = va_arg (ap, int);
955
956 /* Negative width means left justified. */
957 if (width < 0)
958 {
959 width = -width;
960 pad = L_(' ');
961 left = 1;
962 }
963 }
964 JUMP (*f, step1_jumps);
965
966 /* Given width in format string. */
967 LABEL (width):
968 width = read_int (&f);
969 if (*f == L_('$'))
970 /* Oh, oh. The argument comes from a positional parameter. */
971 goto do_positional;
972 JUMP (*f, step1_jumps);
973
974 LABEL (precision):
975 ++f;
976 if (*f == L_('*'))
977 {
978 const UCHAR_T *tmp; /* Temporary value. */
979
980 tmp = ++f;
981 if (ISDIGIT (*tmp) && read_int (&tmp) > 0 && *tmp == L_('$'))
982 /* The precision comes from a positional parameter. */
983 goto do_positional;
984
985 prec = va_arg (ap, int);
986
987 /* If the precision is negative the precision is omitted. */
988 if (prec < 0)
989 prec = -1;
990 }
991 else if (ISDIGIT (*f))
992 prec = read_int (&f);
993 else
994 prec = 0;
995 JUMP (*f, step2_jumps);
996
997 /* Process 'h' modifier. No other modifier is allowed to
998 follow. */
999 LABEL (mod_half):
1000 is_short = 1;
1001 JUMP (*++f, step4_jumps);
1002
1003 /* Process 'l' modifier. There might another 'l' follow. */
1004 LABEL (mod_long):
1005 is_long = 1;
1006 JUMP (*++f, step3_jumps);
1007
1008 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1009 allowed to follow. */
1010 LABEL (mod_longlong):
1011 is_long_double = 1;
1012 JUMP (*++f, step4_jumps);
1013
1014 LABEL (mod_size_t):
1015 is_longlong = sizeof (size_t) > sizeof (unsigned long int);
1016 is_long = sizeof (size_t) > sizeof (unsigned int);
1017 JUMP (*++f, step4_jumps);
1018
1019
1020 /* Process current format. */
1021 while (1)
1022 {
1023 process_arg (((struct printf_spec *) NULL));
1024
1025 LABEL (form_unknown):
1026 if (spec == L_('\0'))
1027 {
1028 /* The format string ended before the specifier is complete. */
1029 done = -1;
1030 goto all_done;
1031 }
1032
1033 /* If we are in the fast loop force entering the complicated
1034 one. */
1035 goto do_positional;
1036 }
1037
1038 /* Look for next format specifier. */
1039 f = find_spec ((end_of_spec = ++f), &mbstate);
1040
1041 /* Write the following constant string. */
1042 outstring (end_of_spec, f - end_of_spec);
1043 }
1044 while (*f != L_('\0'));
1045
1046 /* Unlock stream and return. */
1047 goto all_done;
1048
1049 /* Here starts the more complex loop to handle positional parameters. */
1050 do_positional:
1051 {
1052 /* Array with information about the needed arguments. This has to
1053 be dynamically extendable. */
1054 size_t nspecs = 0;
1055 size_t nspecs_max = 32; /* A more or less arbitrary start value. */
1056 struct printf_spec *specs
1057 = alloca (nspecs_max * sizeof (struct printf_spec));
1058
1059 /* The number of arguments the format string requests. This will
1060 determine the size of the array needed to store the argument
1061 attributes. */
1062 size_t nargs = 0;
1063 int *args_type;
1064 union printf_arg *args_value;
1065
1066 /* Positional parameters refer to arguments directly. This could
1067 also determine the maximum number of arguments. Track the
1068 maximum number. */
1069 size_t max_ref_arg = 0;
1070
1071 /* Just a counter. */
1072 int cnt;
1073
1074
1075 if (grouping == (const char *) -1)
1076 {
1077 /* XXX Use wctob. But this is incompatible for now. */
1078 /* Figure out the thousands separator character. */
1079 if (mbtowc (&thousands_sep,
1080 _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
1081 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
1082 thousands_sep = (wchar_t) *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1083 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1084 if (*grouping == '\0' || *grouping == CHAR_MAX
1085 || thousands_sep == L'\0')
1086 grouping = NULL;
1087 }
1088
1089 for (f = lead_str_end; *f != '\0'; f = specs[nspecs++].next_fmt)
1090 {
1091 if (nspecs >= nspecs_max)
1092 {
1093 /* Extend the array of format specifiers. */
1094 struct printf_spec *old = specs;
1095
1096 nspecs_max *= 2;
1097 specs = alloca (nspecs_max * sizeof (struct printf_spec));
1098
1099 if (specs == &old[nspecs])
1100 /* Stack grows up, OLD was the last thing allocated;
1101 extend it. */
1102 nspecs_max += nspecs_max / 2;
1103 else
1104 {
1105 /* Copy the old array's elements to the new space. */
1106 memcpy (specs, old, nspecs * sizeof (struct printf_spec));
1107 if (old == &specs[nspecs])
1108 /* Stack grows down, OLD was just below the new
1109 SPECS. We can use that space when the new space
1110 runs out. */
1111 nspecs_max += nspecs_max / 2;
1112 }
1113 }
1114
1115 /* Parse the format specifier. */
1116 nargs += parse_one_spec (f, nargs, &specs[nspecs], &max_ref_arg,
1117 &mbstate);
1118 }
1119
1120 /* Determine the number of arguments the format string consumes. */
1121 nargs = MAX (nargs, max_ref_arg);
1122
1123 /* Allocate memory for the argument descriptions. */
1124 args_type = alloca (nargs * sizeof (int));
1125 memset (args_type, 0, nargs * sizeof (int));
1126 args_value = alloca (nargs * sizeof (union printf_arg));
1127
1128 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1129 still zero after this loop, format is invalid. For now we
1130 simply use 0 as the value. */
1131
1132 /* Fill in the types of all the arguments. */
1133 for (cnt = 0; cnt < nspecs; ++cnt)
1134 {
1135 /* If the width is determined by an argument this is an int. */
1136 if (specs[cnt].width_arg != -1)
1137 args_type[specs[cnt].width_arg] = PA_INT;
1138
1139 /* If the precision is determined by an argument this is an int. */
1140 if (specs[cnt].prec_arg != -1)
1141 args_type[specs[cnt].prec_arg] = PA_INT;
1142
1143 switch (specs[cnt].ndata_args)
1144 {
1145 case 0: /* No arguments. */
1146 break;
1147 case 1: /* One argument; we already have the type. */
1148 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1149 break;
1150 default:
1151 /* We have more than one argument for this format spec.
1152 We must call the arginfo function again to determine
1153 all the types. */
1154 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1155 (&specs[cnt].info,
1156 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg]);
1157 break;
1158 }
1159 }
1160
1161 /* Now we know all the types and the order. Fill in the argument
1162 values. */
1163 for (cnt = 0, ap = ap_save; cnt < nargs; ++cnt)
1164 switch (args_type[cnt])
1165 {
1166 #define T(tag, mem, type) \
1167 case tag: \
1168 args_value[cnt].mem = va_arg (ap, type); \
1169 break
1170
1171 T (PA_CHAR, pa_char, int); /* Promoted. */
1172 T (PA_INT|PA_FLAG_SHORT, pa_short_int, int); /* Promoted. */
1173 T (PA_INT, pa_int, int);
1174 T (PA_INT|PA_FLAG_LONG, pa_long_int, long int);
1175 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1176 T (PA_FLOAT, pa_float, double); /* Promoted. */
1177 T (PA_DOUBLE, pa_double, double);
1178 T (PA_DOUBLE|PA_FLAG_LONG_DOUBLE, pa_long_double, long double);
1179 T (PA_STRING, pa_string, const char *);
1180 T (PA_POINTER, pa_pointer, void *);
1181 #undef T
1182 default:
1183 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1184 args_value[cnt].pa_pointer = va_arg (ap, void *);
1185 else
1186 args_value[cnt].pa_long_double = 0.0;
1187 break;
1188 }
1189
1190 /* Now walk through all format specifiers and process them. */
1191 for (; nspecs_done < nspecs; ++nspecs_done)
1192 {
1193 #undef REF
1194 #define REF(Name) &&do2_##Name
1195 #undef LABEL
1196 #define LABEL(Name) do2_##Name
1197 STEP4_TABLE;
1198
1199 int is_negative;
1200 union
1201 {
1202 unsigned long long int longlong;
1203 unsigned long int word;
1204 } number;
1205 int base;
1206 union printf_arg the_arg;
1207 char *string; /* Pointer to argument string. */
1208
1209 /* Fill variables from values in struct. */
1210 int alt = specs[nspecs_done].info.alt;
1211 int space = specs[nspecs_done].info.space;
1212 int left = specs[nspecs_done].info.left;
1213 int showsign = specs[nspecs_done].info.showsign;
1214 int group = specs[nspecs_done].info.group;
1215 int is_long_double = specs[nspecs_done].info.is_long_double;
1216 int is_short = specs[nspecs_done].info.is_short;
1217 int is_long = specs[nspecs_done].info.is_long;
1218 int width = specs[nspecs_done].info.width;
1219 int prec = specs[nspecs_done].info.prec;
1220 char pad = specs[nspecs_done].info.pad;
1221 CHAR_T spec = specs[nspecs_done].info.spec;
1222
1223 /* Fill in last information. */
1224 if (specs[nspecs_done].width_arg != -1)
1225 {
1226 /* Extract the field width from an argument. */
1227 specs[nspecs_done].info.width =
1228 args_value[specs[nspecs_done].width_arg].pa_int;
1229
1230 if (specs[nspecs_done].info.width < 0)
1231 /* If the width value is negative left justification is
1232 selected and the value is taken as being positive. */
1233 {
1234 specs[nspecs_done].info.width *= -1;
1235 left = specs[nspecs_done].info.left = 1;
1236 }
1237 width = specs[nspecs_done].info.width;
1238 }
1239
1240 if (specs[nspecs_done].prec_arg != -1)
1241 {
1242 /* Extract the precision from an argument. */
1243 specs[nspecs_done].info.prec =
1244 args_value[specs[nspecs_done].prec_arg].pa_int;
1245
1246 if (specs[nspecs_done].info.prec < 0)
1247 /* If the precision is negative the precision is
1248 omitted. */
1249 specs[nspecs_done].info.prec = -1;
1250
1251 prec = specs[nspecs_done].info.prec;
1252 }
1253
1254 /* Process format specifiers. */
1255 while (1)
1256 {
1257 JUMP (spec, step4_jumps);
1258
1259 process_arg ((&specs[nspecs_done]));
1260
1261 LABEL (form_unknown):
1262 {
1263 extern printf_function **__printf_function_table;
1264 int function_done;
1265 printf_function *function;
1266 unsigned int i;
1267 const void **ptr;
1268
1269 function =
1270 (__printf_function_table == NULL ? NULL :
1271 __printf_function_table[specs[nspecs_done].info.spec]);
1272
1273 if (function == NULL)
1274 function = &printf_unknown;
1275
1276 ptr = alloca (specs[nspecs_done].ndata_args
1277 * sizeof (const void *));
1278
1279 /* Fill in an array of pointers to the argument values. */
1280 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
1281 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1282
1283 /* Call the function. */
1284 function_done = (*function) (s, &specs[nspecs_done].info, ptr);
1285
1286 /* If an error occured we don't have information about #
1287 of chars. */
1288 if (function_done < 0)
1289 {
1290 done = -1;
1291 goto all_done;
1292 }
1293
1294 done += function_done;
1295 }
1296 break;
1297 }
1298
1299 /* Write the following constant string. */
1300 outstring (specs[nspecs_done].end_of_fmt,
1301 specs[nspecs_done].next_fmt
1302 - specs[nspecs_done].end_of_fmt);
1303 }
1304 }
1305
1306 all_done:
1307 /* Unlock the stream. */
1308 __libc_cleanup_region_end (1);
1309
1310 return done;
1311 }
1312
1313 #ifdef USE_IN_LIBIO
1314 # undef vfprintf
1315 # ifdef strong_alias
1316 /* This is for glibc. */
1317 strong_alias (_IO_vfprintf, vfprintf);
1318 # else
1319 # if defined __ELF__ || defined __GNU_LIBRARY__
1320 # include <gnu-stabs.h>
1321 # ifdef weak_alias
1322 weak_alias (_IO_vfprintf, vfprintf);
1323 # endif
1324 # endif
1325 # endif
1326 #endif
1327 \f
1328 /* Handle an unknown format specifier. This prints out a canonicalized
1329 representation of the format spec itself. */
1330 static int
1331 printf_unknown (FILE *s, const struct printf_info *info,
1332 const void *const *args)
1333
1334 {
1335 int done = 0;
1336 char work_buffer[BUFSIZ];
1337 register char *w;
1338
1339 outchar ('%');
1340
1341 if (info->alt)
1342 outchar ('#');
1343 if (info->group)
1344 outchar ('\'');
1345 if (info->showsign)
1346 outchar ('+');
1347 else if (info->space)
1348 outchar (' ');
1349 if (info->left)
1350 outchar ('-');
1351 if (info->pad == '0')
1352 outchar ('0');
1353
1354 if (info->width != 0)
1355 {
1356 w = _itoa_word (info->width, workend + 1, 10, 0);
1357 while (++w <= workend)
1358 outchar (*w);
1359 }
1360
1361 if (info->prec != -1)
1362 {
1363 outchar ('.');
1364 w = _itoa_word (info->prec, workend + 1, 10, 0);
1365 while (++w <= workend)
1366 outchar (*w);
1367 }
1368
1369 if (info->spec != '\0')
1370 outchar (info->spec);
1371
1372 return done;
1373 }
1374 \f
1375 /* Group the digits according to the grouping rules of the current locale.
1376 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1377 static char *
1378 group_number (CHAR_T *w, CHAR_T *rear_ptr, const CHAR_T *grouping,
1379 wchar_t thousands_sep)
1380 {
1381 int len;
1382 char *src, *s;
1383
1384 /* We treat all negative values like CHAR_MAX. */
1385
1386 if (*grouping == CHAR_MAX || *grouping < 0)
1387 /* No grouping should be done. */
1388 return w;
1389
1390 len = *grouping;
1391
1392 /* Copy existing string so that nothing gets overwritten. */
1393 src = (char *) alloca (rear_ptr - w);
1394 memcpy (src, w + 1, rear_ptr - w);
1395 s = &src[rear_ptr - w - 1];
1396 w = rear_ptr;
1397
1398 /* Process all characters in the string. */
1399 while (s >= src)
1400 {
1401 *w-- = *s--;
1402
1403 if (--len == 0 && s >= src)
1404 {
1405 /* A new group begins. */
1406 *w-- = thousands_sep;
1407
1408 len = *grouping++;
1409 if (*grouping == '\0')
1410 /* The previous grouping repeats ad infinitum. */
1411 --grouping;
1412 else if (*grouping == CHAR_MAX || *grouping < 0)
1413 {
1414 /* No further grouping to be done.
1415 Copy the rest of the number. */
1416 do
1417 *w-- = *s--;
1418 while (s >= src);
1419 break;
1420 }
1421 }
1422 }
1423 return w;
1424 }
1425 \f
1426 #ifdef USE_IN_LIBIO
1427 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
1428 struct helper_file
1429 {
1430 struct _IO_FILE_plus _f;
1431 _IO_FILE *_put_stream;
1432 #ifdef _IO_MTSAFE_IO
1433 _IO_lock_t lock;
1434 #endif
1435 };
1436
1437 static int
1438 _IO_helper_overflow (_IO_FILE *s, int c)
1439 {
1440 _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
1441 int used = s->_IO_write_ptr - s->_IO_write_base;
1442 if (used)
1443 {
1444 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
1445 s->_IO_write_ptr -= written;
1446 }
1447 return PUTC (c, s);
1448 }
1449
1450 static const struct _IO_jump_t _IO_helper_jumps =
1451 {
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)
1470 };
1471
1472 static int
1473 buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format,
1474 _IO_va_list args)
1475 {
1476 char buf[_IO_BUFSIZ];
1477 struct helper_file helper;
1478 register _IO_FILE *hp = (_IO_FILE *) &helper;
1479 int result, to_flush;
1480
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 #ifdef _IO_MTSAFE_IO
1488 hp->_lock = &helper.lock;
1489 #endif
1490 _IO_JUMPS (hp) = (struct _IO_jump_t *) &_IO_helper_jumps;
1491
1492 /* Now print to helper instead. */
1493 result = _IO_vfprintf (hp, format, args);
1494
1495 /* Now flush anything from the helper to the S. */
1496 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
1497 {
1498 if (_IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
1499 return -1;
1500 }
1501
1502 return result;
1503 }
1504
1505 #else /* !USE_IN_LIBIO */
1506
1507 static int
1508 buffered_vfprintf (register FILE *s, const CHAR_T *format, va_list args)
1509 {
1510 char buf[BUFSIZ];
1511 int result;
1512
1513 s->__bufp = s->__buffer = buf;
1514 s->__bufsize = sizeof buf;
1515 s->__put_limit = s->__buffer + s->__bufsize;
1516 s->__get_limit = s->__buffer;
1517
1518 /* Now use buffer to print. */
1519 result = vfprintf (s, format, args);
1520
1521 if (fflush (s) == EOF)
1522 result = -1;
1523 s->__buffer = s->__bufp = s->__get_limit = s->__put_limit = NULL;
1524 s->__bufsize = 0;
1525
1526 return result;
1527 }
1528 \f
1529 /* Pads string with given number of a specified character.
1530 This code is taken from iopadn.c of the GNU I/O library. */
1531 #define PADSIZE 16
1532 static const CHAR_T blanks[PADSIZE] =
1533 { L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '),
1534 L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' ') };
1535 static const CHAR_T zeroes[PADSIZE] =
1536 { L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'),
1537 L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0') };
1538
1539 ssize_t
1540 #ifndef COMPILE_WPRINTF
1541 __printf_pad (FILE *s, char pad, size_t count)
1542 #else
1543 __wprintf_pad (FILE *s, wchar_t pad, size_t count)
1544 #endif
1545 {
1546 const CHAR_T *padptr;
1547 register size_t i;
1548
1549 padptr = pad == L_(' ') ? blanks : zeroes;
1550
1551 for (i = count; i >= PADSIZE; i -= PADSIZE)
1552 if (PUT (s, padptr, PADSIZE) != PADSIZE)
1553 return -1;
1554 if (i > 0)
1555 if (PUT (s, padptr, i) != i)
1556 return -1;
1557
1558 return count;
1559 }
1560 #undef PADSIZE
1561 #endif /* USE_IN_LIBIO */