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