Update.
[glibc.git] / stdio-common / vfprintf.c
blob822728e7e761fdcd7e5b95c5807f3e8ab774e297
1 /* Copyright (C) 1991-1999, 2000 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 <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <wchar.h>
28 #include <bits/libc-lock.h>
29 #include <sys/param.h>
30 #include "_itoa.h"
31 #include <locale/localeinfo.h>
33 /* This code is shared between the standard stdio implementation found
34 in GNU C library and the libio implementation originally found in
35 GNU libg++.
37 Beside this it is also shared between the normal and wide character
38 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
41 #ifdef USE_IN_LIBIO
42 /* This code is for use in libio. */
43 # include <libioP.h>
44 # define FILE _IO_FILE
45 # undef va_list
46 # define va_list _IO_va_list
47 # undef BUFSIZ
48 # define BUFSIZ _IO_BUFSIZ
49 # define ARGCHECK(S, Format) \
50 do \
51 { \
52 /* Check file argument for consistence. */ \
53 CHECK_FILE (S, -1); \
54 if (S->_flags & _IO_NO_WRITES) \
55 { \
56 __set_errno (EBADF); \
57 return -1; \
58 } \
59 if (Format == NULL) \
60 { \
61 MAYBE_SET_EINVAL; \
62 return -1; \
63 } \
64 } while (0)
65 # define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
67 # ifndef COMPILE_WPRINTF
68 # define vfprintf _IO_vfprintf
69 # define CHAR_T char
70 # define UCHAR_T unsigned char
71 # define INT_T int
72 # define L_(Str) Str
73 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
75 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
76 # define PAD(Padchar) \
77 if (width > 0) \
78 done += _IO_padn (s, (Padchar), width)
79 # define PUTC(C, F) _IO_putc_unlocked (C, F)
80 # define ORIENT if (s->_vtable_offset == 0 && _IO_fwide (s, -1) != -1)\
81 return -1
82 # else
83 # define vfprintf _IO_vfwprintf
84 # define CHAR_T wchar_t
85 /* This is a hack!!! There should be a type uwchar_t. */
86 # define UCHAR_T unsigned int /* uwchar_t */
87 # define INT_T wint_t
88 # define L_(Str) L##Str
89 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
91 # include "_itowa.h"
93 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
94 # define PAD(Padchar) \
95 if (width > 0) \
96 done += _IO_wpadn (s, (Padchar), width)
97 # define PUTC(C, F) _IO_putwc_unlocked (C, F)
98 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
100 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
101 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
102 # undef EOF
103 # define EOF WEOF
104 # endif
105 #else /* ! USE_IN_LIBIO */
106 /* This code is for use in the GNU C library. */
107 # include <stdio.h>
108 # define ARGCHECK(S, Format) \
109 do \
111 /* Check file argument for consistence. */ \
112 if (!__validfp (S) || !S->__mode.__write) \
114 __set_errno (EBADF); \
115 return -1; \
117 if (Format == NULL) \
119 __set_errno (EINVAL); \
120 return -1; \
122 if (!S->__seen) \
124 if (__flshfp (S, EOF) == EOF) \
125 return -1; \
128 while (0)
129 # define UNBUFFERED_P(s) ((s)->__buffer == NULL)
131 # define CHAR_T char
132 # define UCHAR_T unsigned char
133 # define INT_T int
134 # define L_(Str) Str
135 # define ISDIGIT(Ch) isdigit (Ch)
137 # define PUT(F, S, N) fwrite (S, 1, N, F)
138 ssize_t __printf_pad __P ((FILE *, char pad, size_t n));
139 # define PAD(Padchar) \
140 if (width > 0) \
141 { ssize_t __res = __printf_pad (s, (Padchar), width); \
142 if (__res == -1) \
144 done = -1; \
145 goto all_done; \
147 done += __res; }
148 # define PUTC(C, F) putc (C, F)
150 /* XXX These declarations should go as soon as the stdio header files
151 have these prototypes. */
152 extern void __flockfile (FILE *);
153 extern void __funlockfile (FILE *);
154 #endif /* USE_IN_LIBIO */
156 #include "_i18n_number.h"
158 /* Include the shared code for parsing the format string. */
159 #include "printf-parse.h"
162 #define outchar(Ch) \
163 do \
165 register const INT_T outc = (Ch); \
166 if (PUTC (outc, s) == EOF) \
168 done = -1; \
169 goto all_done; \
171 else \
172 ++done; \
174 while (0)
176 #define outstring(String, Len) \
177 do \
179 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
181 done = -1; \
182 goto all_done; \
184 done += (Len); \
186 while (0)
188 /* For handling long_double and longlong we use the same flag. If
189 `long' and `long long' are effectively the same type define it to
190 zero. */
191 #if LONG_MAX == LONG_LONG_MAX
192 # define is_longlong 0
193 #else
194 # define is_longlong is_long_double
195 #endif
197 /* If `long' and `int' is effectively the same type we don't have to
198 handle `long separately. */
199 #if INT_MAX == LONG_MAX
200 # define is_long_num 0
201 #else
202 # define is_long_num is_long
203 #endif
206 /* Global variables. */
207 static const CHAR_T null[] = L_("(null)");
210 /* Helper function to provide temporary buffering for unbuffered streams. */
211 static int buffered_vfprintf __P ((FILE *stream, const CHAR_T *fmt, va_list))
212 internal_function;
214 /* Handle unknown format specifier. */
215 static int printf_unknown __P ((FILE *, const struct printf_info *,
216 const void *const *));
218 /* Group digits of number string. */
219 #ifdef COMPILE_WPRINTF
220 static CHAR_T *group_number __P ((CHAR_T *, CHAR_T *, const char *, wchar_t))
221 internal_function;
222 #else
223 static CHAR_T *group_number __P ((CHAR_T *, CHAR_T *, const char *,
224 const char *)) internal_function;
225 #endif
228 /* The function itself. */
230 vfprintf (FILE *s, const CHAR_T *format, va_list ap)
232 /* The character used as thousands separator. */
233 #ifdef COMPILE_WPRINTF
234 wchar_t thousands_sep = L'\0';
235 #else
236 const char *thousands_sep = NULL;
237 #endif
239 /* The string describing the size of groups of digits. */
240 const char *grouping;
242 /* Place to accumulate the result. */
243 int done;
245 /* Current character in format string. */
246 const UCHAR_T *f;
248 /* End of leading constant string. */
249 const UCHAR_T *lead_str_end;
251 /* Points to next format specifier. */
252 const UCHAR_T *end_of_spec;
254 /* Buffer intermediate results. */
255 CHAR_T work_buffer[1000];
256 CHAR_T *workend;
258 /* State for restartable multibyte character handling functions. */
259 #ifndef COMPILE_WPRINTF
260 mbstate_t mbstate;
261 #endif
263 /* We have to save the original argument pointer. */
264 va_list ap_save;
266 /* Count number of specifiers we already processed. */
267 int nspecs_done;
269 /* For the %m format we may need the current `errno' value. */
270 int save_errno = errno;
273 /* This table maps a character into a number representing a
274 class. In each step there is a destination label for each
275 class. */
276 static const int jump_table[] =
278 /* ' ' */ 1, 0, 0, /* '#' */ 4,
279 0, /* '%' */ 14, 0, /* '\''*/ 6,
280 0, 0, /* '*' */ 7, /* '+' */ 2,
281 0, /* '-' */ 3, /* '.' */ 9, 0,
282 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
283 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
284 /* '8' */ 8, /* '9' */ 8, 0, 0,
285 0, 0, 0, 0,
286 0, /* 'A' */ 26, 0, /* 'C' */ 25,
287 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
288 0, /* 'I' */ 29, 0, 0,
289 /* 'L' */ 12, 0, 0, 0,
290 0, 0, 0, /* 'S' */ 21,
291 0, 0, 0, 0,
292 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
293 0, 0, 0, 0,
294 0, /* 'a' */ 26, 0, /* 'c' */ 20,
295 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
296 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
297 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
298 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
299 /* 't' */ 27, /* 'u' */ 16, 0, 0,
300 /* 'x' */ 18, 0, /* 'z' */ 13
303 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
304 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
305 #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
306 /* 'int' is enough and it saves some space on 64 bit systems. */
307 # define JUMP_TABLE_TYPE const int
308 # define JUMP(ChExpr, table) \
309 do \
311 int offset; \
312 void *__unbounded ptr; \
313 spec = (ChExpr); \
314 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
315 : table[CHAR_CLASS (spec)]; \
316 ptr = &&do_form_unknown + offset; \
317 goto *ptr; \
319 while (0)
320 #else
321 # define JUMP_TABLE_TYPE const void *const
322 # define JUMP(ChExpr, table) \
323 do \
325 const void *__unbounded ptr; \
326 spec = (ChExpr); \
327 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
328 : table[CHAR_CLASS (spec)]; \
329 goto *ptr; \
331 while (0)
332 #endif
334 #define STEP0_3_TABLE \
335 /* Step 0: at the beginning. */ \
336 static JUMP_TABLE_TYPE step0_jumps[30] = \
338 REF (form_unknown), \
339 REF (flag_space), /* for ' ' */ \
340 REF (flag_plus), /* for '+' */ \
341 REF (flag_minus), /* for '-' */ \
342 REF (flag_hash), /* for '<hash>' */ \
343 REF (flag_zero), /* for '0' */ \
344 REF (flag_quote), /* for '\'' */ \
345 REF (width_asterics), /* for '*' */ \
346 REF (width), /* for '1'...'9' */ \
347 REF (precision), /* for '.' */ \
348 REF (mod_half), /* for 'h' */ \
349 REF (mod_long), /* for 'l' */ \
350 REF (mod_longlong), /* for 'L', 'q' */ \
351 REF (mod_size_t), /* for 'z', 'Z' */ \
352 REF (form_percent), /* for '%' */ \
353 REF (form_integer), /* for 'd', 'i' */ \
354 REF (form_unsigned), /* for 'u' */ \
355 REF (form_octal), /* for 'o' */ \
356 REF (form_hexa), /* for 'X', 'x' */ \
357 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
358 REF (form_character), /* for 'c' */ \
359 REF (form_string), /* for 's', 'S' */ \
360 REF (form_pointer), /* for 'p' */ \
361 REF (form_number), /* for 'n' */ \
362 REF (form_strerror), /* for 'm' */ \
363 REF (form_wcharacter), /* for 'C' */ \
364 REF (form_floathex), /* for 'A', 'a' */ \
365 REF (mod_ptrdiff_t), /* for 't' */ \
366 REF (mod_intmax_t), /* for 'j' */ \
367 REF (flag_i18n), /* for 'I' */ \
368 }; \
369 /* Step 1: after processing width. */ \
370 static JUMP_TABLE_TYPE step1_jumps[30] = \
372 REF (form_unknown), \
373 REF (form_unknown), /* for ' ' */ \
374 REF (form_unknown), /* for '+' */ \
375 REF (form_unknown), /* for '-' */ \
376 REF (form_unknown), /* for '<hash>' */ \
377 REF (form_unknown), /* for '0' */ \
378 REF (form_unknown), /* for '\'' */ \
379 REF (form_unknown), /* for '*' */ \
380 REF (form_unknown), /* for '1'...'9' */ \
381 REF (precision), /* for '.' */ \
382 REF (mod_half), /* for 'h' */ \
383 REF (mod_long), /* for 'l' */ \
384 REF (mod_longlong), /* for 'L', 'q' */ \
385 REF (mod_size_t), /* for 'z', 'Z' */ \
386 REF (form_percent), /* for '%' */ \
387 REF (form_integer), /* for 'd', 'i' */ \
388 REF (form_unsigned), /* for 'u' */ \
389 REF (form_octal), /* for 'o' */ \
390 REF (form_hexa), /* for 'X', 'x' */ \
391 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
392 REF (form_character), /* for 'c' */ \
393 REF (form_string), /* for 's', 'S' */ \
394 REF (form_pointer), /* for 'p' */ \
395 REF (form_number), /* for 'n' */ \
396 REF (form_strerror), /* for 'm' */ \
397 REF (form_wcharacter), /* for 'C' */ \
398 REF (form_floathex), /* for 'A', 'a' */ \
399 REF (mod_ptrdiff_t), /* for 't' */ \
400 REF (mod_intmax_t), /* for 'j' */ \
401 REF (flag_i18n) /* for 'I' */ \
402 }; \
403 /* Step 2: after processing precision. */ \
404 static JUMP_TABLE_TYPE step2_jumps[30] = \
406 REF (form_unknown), \
407 REF (form_unknown), /* for ' ' */ \
408 REF (form_unknown), /* for '+' */ \
409 REF (form_unknown), /* for '-' */ \
410 REF (form_unknown), /* for '<hash>' */ \
411 REF (form_unknown), /* for '0' */ \
412 REF (form_unknown), /* for '\'' */ \
413 REF (form_unknown), /* for '*' */ \
414 REF (form_unknown), /* for '1'...'9' */ \
415 REF (form_unknown), /* for '.' */ \
416 REF (mod_half), /* for 'h' */ \
417 REF (mod_long), /* for 'l' */ \
418 REF (mod_longlong), /* for 'L', 'q' */ \
419 REF (mod_size_t), /* for 'z', 'Z' */ \
420 REF (form_percent), /* for '%' */ \
421 REF (form_integer), /* for 'd', 'i' */ \
422 REF (form_unsigned), /* for 'u' */ \
423 REF (form_octal), /* for 'o' */ \
424 REF (form_hexa), /* for 'X', 'x' */ \
425 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
426 REF (form_character), /* for 'c' */ \
427 REF (form_string), /* for 's', 'S' */ \
428 REF (form_pointer), /* for 'p' */ \
429 REF (form_number), /* for 'n' */ \
430 REF (form_strerror), /* for 'm' */ \
431 REF (form_wcharacter), /* for 'C' */ \
432 REF (form_floathex), /* for 'A', 'a' */ \
433 REF (mod_ptrdiff_t), /* for 't' */ \
434 REF (mod_intmax_t), /* for 'j' */ \
435 REF (flag_i18n) /* for 'I' */ \
436 }; \
437 /* Step 3a: after processing first 'h' modifier. */ \
438 static JUMP_TABLE_TYPE step3a_jumps[30] = \
440 REF (form_unknown), \
441 REF (form_unknown), /* for ' ' */ \
442 REF (form_unknown), /* for '+' */ \
443 REF (form_unknown), /* for '-' */ \
444 REF (form_unknown), /* for '<hash>' */ \
445 REF (form_unknown), /* for '0' */ \
446 REF (form_unknown), /* for '\'' */ \
447 REF (form_unknown), /* for '*' */ \
448 REF (form_unknown), /* for '1'...'9' */ \
449 REF (form_unknown), /* for '.' */ \
450 REF (mod_halfhalf), /* for 'h' */ \
451 REF (form_unknown), /* for 'l' */ \
452 REF (form_unknown), /* for 'L', 'q' */ \
453 REF (form_unknown), /* for 'z', 'Z' */ \
454 REF (form_percent), /* for '%' */ \
455 REF (form_integer), /* for 'd', 'i' */ \
456 REF (form_unsigned), /* for 'u' */ \
457 REF (form_octal), /* for 'o' */ \
458 REF (form_hexa), /* for 'X', 'x' */ \
459 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
460 REF (form_unknown), /* for 'c' */ \
461 REF (form_unknown), /* for 's', 'S' */ \
462 REF (form_unknown), /* for 'p' */ \
463 REF (form_number), /* for 'n' */ \
464 REF (form_unknown), /* for 'm' */ \
465 REF (form_unknown), /* for 'C' */ \
466 REF (form_unknown), /* for 'A', 'a' */ \
467 REF (form_unknown), /* for 't' */ \
468 REF (form_unknown), /* for 'j' */ \
469 REF (form_unknown) /* for 'I' */ \
470 }; \
471 /* Step 3b: after processing first 'l' modifier. */ \
472 static JUMP_TABLE_TYPE step3b_jumps[30] = \
474 REF (form_unknown), \
475 REF (form_unknown), /* for ' ' */ \
476 REF (form_unknown), /* for '+' */ \
477 REF (form_unknown), /* for '-' */ \
478 REF (form_unknown), /* for '<hash>' */ \
479 REF (form_unknown), /* for '0' */ \
480 REF (form_unknown), /* for '\'' */ \
481 REF (form_unknown), /* for '*' */ \
482 REF (form_unknown), /* for '1'...'9' */ \
483 REF (form_unknown), /* for '.' */ \
484 REF (form_unknown), /* for 'h' */ \
485 REF (mod_longlong), /* for 'l' */ \
486 REF (form_unknown), /* for 'L', 'q' */ \
487 REF (form_unknown), /* for 'z', 'Z' */ \
488 REF (form_percent), /* for '%' */ \
489 REF (form_integer), /* for 'd', 'i' */ \
490 REF (form_unsigned), /* for 'u' */ \
491 REF (form_octal), /* for 'o' */ \
492 REF (form_hexa), /* for 'X', 'x' */ \
493 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
494 REF (form_character), /* for 'c' */ \
495 REF (form_string), /* for 's', 'S' */ \
496 REF (form_pointer), /* for 'p' */ \
497 REF (form_number), /* for 'n' */ \
498 REF (form_strerror), /* for 'm' */ \
499 REF (form_wcharacter), /* for 'C' */ \
500 REF (form_floathex), /* for 'A', 'a' */ \
501 REF (form_unknown), /* for 't' */ \
502 REF (form_unknown), /* for 'j' */ \
503 REF (form_unknown) /* for 'I' */ \
506 #define STEP4_TABLE \
507 /* Step 4: processing format specifier. */ \
508 static JUMP_TABLE_TYPE step4_jumps[30] = \
510 REF (form_unknown), \
511 REF (form_unknown), /* for ' ' */ \
512 REF (form_unknown), /* for '+' */ \
513 REF (form_unknown), /* for '-' */ \
514 REF (form_unknown), /* for '<hash>' */ \
515 REF (form_unknown), /* for '0' */ \
516 REF (form_unknown), /* for '\'' */ \
517 REF (form_unknown), /* for '*' */ \
518 REF (form_unknown), /* for '1'...'9' */ \
519 REF (form_unknown), /* for '.' */ \
520 REF (form_unknown), /* for 'h' */ \
521 REF (form_unknown), /* for 'l' */ \
522 REF (form_unknown), /* for 'L', 'q' */ \
523 REF (form_unknown), /* for 'z', 'Z' */ \
524 REF (form_percent), /* for '%' */ \
525 REF (form_integer), /* for 'd', 'i' */ \
526 REF (form_unsigned), /* for 'u' */ \
527 REF (form_octal), /* for 'o' */ \
528 REF (form_hexa), /* for 'X', 'x' */ \
529 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
530 REF (form_character), /* for 'c' */ \
531 REF (form_string), /* for 's', 'S' */ \
532 REF (form_pointer), /* for 'p' */ \
533 REF (form_number), /* for 'n' */ \
534 REF (form_strerror), /* for 'm' */ \
535 REF (form_wcharacter), /* for 'C' */ \
536 REF (form_floathex), /* for 'A', 'a' */ \
537 REF (form_unknown), /* for 't' */ \
538 REF (form_unknown), /* for 'j' */ \
539 REF (form_unknown) /* for 'I' */ \
543 #define process_arg(fspec) \
544 /* Start real work. We know about all flags and modifiers and \
545 now process the wanted format specifier. */ \
546 LABEL (form_percent): \
547 /* Write a literal "%". */ \
548 outchar (L_('%')); \
549 break; \
551 LABEL (form_integer): \
552 /* Signed decimal integer. */ \
553 base = 10; \
555 if (is_longlong) \
557 long long int signed_number; \
559 if (fspec == NULL) \
560 signed_number = va_arg (ap, long long int); \
561 else \
562 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
564 is_negative = signed_number < 0; \
565 number.longlong = is_negative ? (- signed_number) : signed_number; \
567 goto LABEL (longlong_number); \
569 else \
571 long int signed_number; \
573 if (fspec == NULL) \
575 if (is_long_num) \
576 signed_number = va_arg (ap, long int); \
577 else /* `char' and `short int' will be promoted to `int'. */ \
578 signed_number = va_arg (ap, int); \
580 else \
581 if (is_long_num) \
582 signed_number = args_value[fspec->data_arg].pa_long_int; \
583 else \
584 signed_number = args_value[fspec->data_arg].pa_int; \
586 is_negative = signed_number < 0; \
587 number.word = is_negative ? (- signed_number) : signed_number; \
589 goto LABEL (number); \
591 /* NOTREACHED */ \
593 LABEL (form_unsigned): \
594 /* Unsigned decimal integer. */ \
595 base = 10; \
596 goto LABEL (unsigned_number); \
597 /* NOTREACHED */ \
599 LABEL (form_octal): \
600 /* Unsigned octal integer. */ \
601 base = 8; \
602 goto LABEL (unsigned_number); \
603 /* NOTREACHED */ \
605 LABEL (form_hexa): \
606 /* Unsigned hexadecimal integer. */ \
607 base = 16; \
609 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
611 /* ISO specifies the `+' and ` ' flags only for signed \
612 conversions. */ \
613 is_negative = 0; \
614 showsign = 0; \
615 space = 0; \
617 if (is_longlong) \
619 if (fspec == NULL) \
620 number.longlong = va_arg (ap, unsigned long long int); \
621 else \
622 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
624 LABEL (longlong_number): \
625 if (prec < 0) \
626 /* Supply a default precision if none was given. */ \
627 prec = 1; \
628 else \
629 /* We have to take care for the '0' flag. If a precision \
630 is given it must be ignored. */ \
631 pad = L_(' '); \
633 /* If the precision is 0 and the number is 0 nothing has to \
634 be written for the number, except for the 'o' format in \
635 alternate form. */ \
636 if (prec == 0 && number.longlong == 0) \
638 string = workend; \
639 if (base == 8 && alt) \
640 *--string = L_('0'); \
642 else \
644 /* Put the number in WORK. */ \
645 string = _itoa (number.longlong, workend, base, \
646 spec == L_('X')); \
647 if (group && grouping) \
648 string = group_number (string, workend, grouping, \
649 thousands_sep); \
651 if (use_outdigits && base == 10) \
652 string = _i18n_number_rewrite (string, workend); \
654 /* Simplify further test for num != 0. */ \
655 number.word = number.longlong != 0; \
657 else \
659 if (fspec == NULL) \
661 if (is_long_num) \
662 number.word = va_arg (ap, unsigned long int); \
663 else if (is_char) \
664 number.word = (unsigned char) va_arg (ap, unsigned int); \
665 else if (!is_short) \
666 number.word = va_arg (ap, unsigned int); \
667 else \
668 number.word = (unsigned short int) va_arg (ap, unsigned int); \
670 else \
671 if (is_long_num) \
672 number.word = args_value[fspec->data_arg].pa_u_long_int; \
673 else if (is_char) \
674 number.word = (unsigned char) \
675 args_value[fspec->data_arg].pa_char; \
676 else if (!is_short) \
677 number.word = args_value[fspec->data_arg].pa_u_int; \
678 else \
679 number.word = (unsigned short int) \
680 args_value[fspec->data_arg].pa_u_short_int; \
682 LABEL (number): \
683 if (prec < 0) \
684 /* Supply a default precision if none was given. */ \
685 prec = 1; \
686 else \
687 /* We have to take care for the '0' flag. If a precision \
688 is given it must be ignored. */ \
689 pad = L_(' '); \
691 /* If the precision is 0 and the number is 0 nothing has to \
692 be written for the number, except for the 'o' format in \
693 alternate form. */ \
694 if (prec == 0 && number.word == 0) \
696 string = workend; \
697 if (base == 8 && alt) \
698 *--string = L_('0'); \
700 else \
702 /* Put the number in WORK. */ \
703 string = _itoa_word (number.word, workend, base, \
704 spec == L_('X')); \
705 if (group && grouping) \
706 string = group_number (string, workend, grouping, \
707 thousands_sep); \
709 if (use_outdigits && base == 10) \
710 string = _i18n_number_rewrite (string, workend); \
714 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
715 /* Add octal marker. */ \
716 *--string = L_('0'); \
718 prec = MAX (0, prec - (workend - string)); \
720 if (!left) \
722 width -= workend - string + prec; \
724 if (number.word != 0 && alt && base == 16) \
725 /* Account for 0X hex marker. */ \
726 width -= 2; \
728 if (is_negative || showsign || space) \
729 --width; \
731 if (pad == L_(' ')) \
733 PAD (L_(' ')); \
734 width = 0; \
737 if (is_negative) \
738 outchar (L_('-')); \
739 else if (showsign) \
740 outchar (L_('+')); \
741 else if (space) \
742 outchar (L_(' ')); \
744 if (number.word != 0 && alt && base == 16) \
746 outchar (L_('0')); \
747 outchar (spec); \
750 width += prec; \
751 PAD (L_('0')); \
753 outstring (string, workend - string); \
755 break; \
757 else \
759 if (is_negative) \
761 outchar (L_('-')); \
762 --width; \
764 else if (showsign) \
766 outchar (L_('+')); \
767 --width; \
769 else if (space) \
771 outchar (L_(' ')); \
772 --width; \
775 if (number.word != 0 && alt && base == 16) \
777 outchar (L_('0')); \
778 outchar (spec); \
779 width -= 2; \
782 width -= workend - string + prec; \
784 if (prec > 0) \
786 int temp = width; \
787 width = prec; \
788 PAD (L_('0'));; \
789 width = temp; \
792 outstring (string, workend - string); \
794 PAD (L_(' ')); \
795 break; \
798 LABEL (form_float): \
800 /* Floating-point number. This is handled by printf_fp.c. */ \
801 extern int __printf_fp __P ((FILE *, const struct printf_info *, \
802 const void **const)); \
803 const void *ptr; \
804 int function_done; \
806 if (fspec == NULL) \
808 struct printf_info info = { prec: prec, \
809 width: width, \
810 spec: spec, \
811 is_long_double: is_long_double, \
812 is_short: is_short, \
813 is_long: is_long, \
814 alt: alt, \
815 space: space, \
816 left: left, \
817 showsign: showsign, \
818 group: group, \
819 pad: pad, \
820 extra: 0, \
821 wide: sizeof (CHAR_T) != 1 }; \
823 if (is_long_double) \
824 the_arg.pa_long_double = va_arg (ap, long double); \
825 else \
826 the_arg.pa_double = va_arg (ap, double); \
827 ptr = (const void *) &the_arg; \
829 function_done = __printf_fp (s, &info, &ptr); \
831 else \
833 ptr = (const void *) &args_value[fspec->data_arg]; \
835 function_done = __printf_fp (s, &fspec->info, &ptr); \
838 if (function_done < 0) \
840 /* Error in print handler. */ \
841 done = -1; \
842 goto all_done; \
845 done += function_done; \
847 break; \
849 LABEL (form_floathex): \
851 /* FLoating point number printed as hexadecimal number. */ \
852 extern int __printf_fphex __P ((FILE *, const struct printf_info *, \
853 const void **const)); \
854 const void *ptr; \
855 int function_done; \
857 if (fspec == NULL) \
859 struct printf_info info = { prec: prec, \
860 width: width, \
861 spec: spec, \
862 is_long_double: is_long_double, \
863 is_short: is_short, \
864 is_long: is_long, \
865 alt: alt, \
866 space: space, \
867 left: left, \
868 showsign: showsign, \
869 group: group, \
870 pad: pad, \
871 extra: 0, \
872 wide: sizeof (CHAR_T) != 1 }; \
874 if (is_long_double) \
875 the_arg.pa_long_double = va_arg (ap, long double); \
876 else \
877 the_arg.pa_double = va_arg (ap, double); \
878 ptr = (const void *) &the_arg; \
880 function_done = __printf_fphex (s, &info, &ptr); \
882 else \
884 ptr = (const void *) &args_value[fspec->data_arg]; \
886 function_done = __printf_fphex (s, &fspec->info, &ptr); \
889 if (function_done < 0) \
891 /* Error in print handler. */ \
892 done = -1; \
893 goto all_done; \
896 done += function_done; \
898 break; \
900 LABEL (form_pointer): \
901 /* Generic pointer. */ \
903 const void *ptr; \
904 if (fspec == NULL) \
905 ptr = va_arg (ap, void *); \
906 else \
907 ptr = args_value[fspec->data_arg].pa_pointer; \
908 if (ptr != NULL) \
910 /* If the pointer is not NULL, write it as a %#x spec. */ \
911 base = 16; \
912 number.word = (unsigned long int) ptr; \
913 is_negative = 0; \
914 alt = 1; \
915 group = 0; \
916 spec = L_('x'); \
917 goto LABEL (number); \
919 else \
921 /* Write "(nil)" for a nil pointer. */ \
922 string = (CHAR_T *) L_("(nil)"); \
923 /* Make sure the full string "(nil)" is printed. */ \
924 if (prec < 5) \
925 prec = 5; \
926 is_long = 0; /* This is no wide-char string. */ \
927 goto LABEL (print_string); \
930 /* NOTREACHED */ \
932 LABEL (form_number): \
933 /* Answer the count of characters written. */ \
934 if (fspec == NULL) \
936 if (is_longlong) \
937 *(long long int *) va_arg (ap, void *) = done; \
938 else if (is_long_num) \
939 *(long int *) va_arg (ap, void *) = done; \
940 else if (is_char) \
941 *(char *) va_arg (ap, void *) = done; \
942 else if (is_long_num) \
943 *(long int *) va_arg (ap, void *) = done; \
944 else if (!is_short) \
945 *(int *) va_arg (ap, void *) = done; \
946 else \
947 *(short int *) va_arg (ap, void *) = done; \
949 else \
950 if (is_longlong) \
951 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
952 else if (is_long_num) \
953 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
954 else if (is_long_num) \
955 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
956 else if (is_char) \
957 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
958 else if (!is_short) \
959 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
960 else \
961 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
962 break; \
964 LABEL (form_strerror): \
965 /* Print description of error ERRNO. */ \
966 string = \
967 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
968 sizeof work_buffer); \
969 is_long = 0; /* This is no wide-char string. */ \
970 goto LABEL (print_string)
972 #ifdef COMPILE_WPRINTF
973 # define process_string_arg(fspec) \
974 LABEL (form_character): \
975 /* Character. */ \
976 if (is_long) \
977 goto LABEL (form_wcharacter); \
978 --width; /* Account for the character itself. */ \
979 if (!left) \
980 PAD (L' '); \
981 if (fspec == NULL) \
982 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
983 else \
984 outchar (__btowc ((unsigned char) \
985 args_value[fspec->data_arg].pa_char)); \
986 if (left) \
987 PAD (L' '); \
988 break; \
990 LABEL (form_wcharacter): \
992 /* Wide character. */ \
993 --width; \
994 if (!left) \
995 PAD (L' '); \
996 if (fspec == NULL) \
997 outchar (va_arg (ap, wint_t)); \
998 else \
999 outchar (args_value[fspec->data_arg].pa_wchar); \
1000 if (left) \
1001 PAD (L' '); \
1003 break; \
1005 LABEL (form_string): \
1007 size_t len; \
1008 int string_malloced; \
1010 /* The string argument could in fact be `char *' or `wchar_t *'. \
1011 But this should not make a difference here. */ \
1012 if (fspec == NULL) \
1013 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
1014 else \
1015 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
1017 /* Entry point for printing other strings. */ \
1018 LABEL (print_string): \
1020 string_malloced = 0; \
1021 if (string == NULL) \
1023 /* Write "(null)" if there's space. */ \
1024 if (prec == -1 \
1025 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
1027 string = (CHAR_T *) null; \
1028 len = (sizeof (null) / sizeof (null[0])) - 1; \
1030 else \
1032 string = (CHAR_T *) L""; \
1033 len = 0; \
1036 else if (!is_long && spec != L_('S')) \
1038 /* This is complicated. We have to transform the multibyte \
1039 string into a wide character string. */ \
1040 const char *mbs = (const char *) string; \
1041 mbstate_t mbstate; \
1043 len = prec != -1 ? prec : strlen (mbs); \
1045 /* Allocate dynamically an array which definitely is long \
1046 enough for the wide character version. */ \
1047 if (len < 8192 \
1048 || ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1049 == NULL)) \
1050 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1051 else \
1052 string_malloced = 1; \
1054 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1055 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1056 if (len == (size_t) -1) \
1058 /* Illegal multibyte character. */ \
1059 done = -1; \
1060 goto all_done; \
1063 else \
1065 if (prec != -1) \
1066 /* Search for the end of the string, but don't search past \
1067 the length specified by the precision. */ \
1068 len = __wcsnlen (string, prec); \
1069 else \
1070 len = __wcslen (string); \
1073 if ((width -= len) < 0) \
1075 outstring (string, len); \
1076 break; \
1079 if (!left) \
1080 PAD (L' '); \
1081 outstring (string, len); \
1082 if (left) \
1083 PAD (L' '); \
1084 if (string_malloced) \
1085 free (string); \
1087 break;
1088 #else
1089 # define process_string_arg(fspec) \
1090 LABEL (form_character): \
1091 /* Character. */ \
1092 if (is_long) \
1093 goto LABEL (form_wcharacter); \
1094 --width; /* Account for the character itself. */ \
1095 if (!left) \
1096 PAD (' '); \
1097 if (fspec == NULL) \
1098 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1099 else \
1100 outchar ((unsigned char) args_value[fspec->data_arg].pa_char); \
1101 if (left) \
1102 PAD (' '); \
1103 break; \
1105 LABEL (form_wcharacter): \
1107 /* Wide character. */ \
1108 char buf[MB_CUR_MAX]; \
1109 mbstate_t mbstate; \
1110 size_t len; \
1112 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1113 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wint_t) \
1114 : args_value[fspec->data_arg].pa_wchar), \
1115 &mbstate); \
1116 if (len == (size_t) -1) \
1118 /* Something went wron gduring the conversion. Bail out. */ \
1119 done = -1; \
1120 goto all_done; \
1122 width -= len; \
1123 if (!left) \
1124 PAD (' '); \
1125 outstring (buf, len); \
1126 if (left) \
1127 PAD (' '); \
1129 break; \
1131 LABEL (form_string): \
1133 size_t len; \
1134 int string_malloced; \
1136 /* The string argument could in fact be `char *' or `wchar_t *'. \
1137 But this should not make a difference here. */ \
1138 if (fspec == NULL) \
1139 string = (char *) va_arg (ap, const char *); \
1140 else \
1141 string = (char *) args_value[fspec->data_arg].pa_string; \
1143 /* Entry point for printing other strings. */ \
1144 LABEL (print_string): \
1146 string_malloced = 0; \
1147 if (string == NULL) \
1149 /* Write "(null)" if there's space. */ \
1150 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1152 string = (char *) null; \
1153 len = sizeof (null) - 1; \
1155 else \
1157 string = (char *) ""; \
1158 len = 0; \
1161 else if (!is_long && spec != L_('S')) \
1163 if (prec != -1) \
1165 /* Search for the end of the string, but don't search past \
1166 the length (in bytes) specified by the precision. Also \
1167 don't use incomplete characters. */ \
1168 if (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MB_CUR_MAX) == 1) \
1169 len = __strnlen (string, prec); \
1170 else \
1172 /* In case we have a multibyte character set the \
1173 situation is more compilcated. We must not copy \
1174 bytes at the end which form an incomplete character. */\
1175 wchar_t ignore[prec]; \
1176 const char *str2 = string; \
1177 mbstate_t ps; \
1179 memset (&ps, '\0', sizeof (ps)); \
1180 if (__mbsnrtowcs (ignore, &str2, prec, prec, &ps) \
1181 == (size_t) -1) \
1183 done = -1; \
1184 goto all_done; \
1186 if (str2 == NULL) \
1187 len = strlen (string); \
1188 else \
1189 len = str2 - string - (ps.__count); \
1192 else \
1193 len = strlen (string); \
1195 else \
1197 const wchar_t *s2 = (const wchar_t *) string; \
1198 mbstate_t mbstate; \
1200 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1202 if (prec > 0) \
1204 /* The string `s2' might not be NUL terminated. */ \
1205 if (prec < 32768 \
1206 || (string = (char *) malloc (prec)) == NULL) \
1207 string = (char *) alloca (prec); \
1208 else \
1209 string_malloced = 1; \
1210 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1212 else \
1214 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1215 if (len != (size_t) -1) \
1217 assert (__mbsinit (&mbstate)); \
1218 s2 = (const wchar_t *) string; \
1219 if (len + 1 < 32768 \
1220 || (string = (char *) malloc (len + 1)) == NULL) \
1221 string = (char *) alloca (len + 1); \
1222 else \
1223 string_malloced = 1; \
1224 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1228 if (len == (size_t) -1) \
1230 /* Illegal wide-character string. */ \
1231 done = -1; \
1232 goto all_done; \
1236 if ((width -= len) < 0) \
1238 outstring (string, len); \
1239 break; \
1242 if (!left) \
1243 PAD (' '); \
1244 outstring (string, len); \
1245 if (left) \
1246 PAD (' '); \
1247 if (string_malloced) \
1248 free (string); \
1250 break;
1251 #endif
1253 /* Orient the stream. */
1254 #ifdef ORIENT
1255 ORIENT;
1256 #endif
1258 /* Sanity check of arguments. */
1259 ARGCHECK (s, format);
1261 #ifdef ORIENT
1262 /* Check for correct orientation. */
1263 if (
1264 # ifdef USE_IN_LIBIO
1265 s->_vtable_offset == 0 &&
1266 # endif
1267 _IO_fwide (s, sizeof (CHAR_T) == 1 ? -1 : 1)
1268 != (sizeof (CHAR_T) == 1 ? -1 : 1))
1269 /* The stream is already oriented otherwise. */
1270 return EOF;
1271 #endif
1273 if (UNBUFFERED_P (s))
1274 /* Use a helper function which will allocate a local temporary buffer
1275 for the stream and then call us again. */
1276 return buffered_vfprintf (s, format, ap);
1278 /* Initialize local variables. */
1279 done = 0;
1280 grouping = (const char *) -1;
1281 #ifdef __va_copy
1282 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1283 since on some systems `va_list' is not an integral type. */
1284 __va_copy (ap_save, ap);
1285 #else
1286 ap_save = ap;
1287 #endif
1288 nspecs_done = 0;
1290 #ifdef COMPILE_WPRINTF
1291 /* Find the first format specifier. */
1292 f = lead_str_end = find_spec ((const UCHAR_T *) format);
1293 #else
1294 /* Put state for processing format string in initial state. */
1295 memset (&mbstate, '\0', sizeof (mbstate_t));
1297 /* Find the first format specifier. */
1298 f = lead_str_end = find_spec (format, &mbstate);
1299 #endif
1301 /* Lock stream. */
1302 #ifdef USE_IN_LIBIO
1303 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
1304 _IO_flockfile (s);
1305 #else
1306 __libc_cleanup_region_start ((void (*) (void *)) &__funlockfile, s);
1307 __flockfile (s);
1308 #endif
1310 /* Write the literal text before the first format. */
1311 outstring ((const UCHAR_T *) format,
1312 lead_str_end - (const UCHAR_T *) format);
1314 /* If we only have to print a simple string, return now. */
1315 if (*f == L_('\0'))
1316 goto all_done;
1318 /* Process whole format string. */
1321 #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
1322 # define REF(Name) &&do_##Name - &&do_form_unknown
1323 #else
1324 # define REF(Name) &&do_##Name
1325 #endif
1326 #define LABEL(Name) do_##Name
1327 STEP0_3_TABLE;
1328 STEP4_TABLE;
1330 union printf_arg *args_value; /* This is not used here but ... */
1331 int is_negative; /* Flag for negative number. */
1332 union
1334 unsigned long long int longlong;
1335 unsigned long int word;
1336 } number;
1337 int base;
1338 union printf_arg the_arg;
1339 CHAR_T *string; /* Pointer to argument string. */
1340 int alt = 0; /* Alternate format. */
1341 int space = 0; /* Use space prefix if no sign is needed. */
1342 int left = 0; /* Left-justify output. */
1343 int showsign = 0; /* Always begin with plus or minus sign. */
1344 int group = 0; /* Print numbers according grouping rules. */
1345 int is_long_double = 0; /* Argument is long double/ long long int. */
1346 int is_short = 0; /* Argument is short int. */
1347 int is_long = 0; /* Argument is long int. */
1348 int is_char = 0; /* Argument is promoted (unsigned) char. */
1349 int width = 0; /* Width of output; 0 means none specified. */
1350 int prec = -1; /* Precision of output; -1 means none specified. */
1351 /* This flag is set by the 'I' modifier and selects the use of the
1352 `outdigits' as determined by the current locale. */
1353 int use_outdigits = 0;
1354 UCHAR_T pad = L_(' ');/* Padding character. */
1355 CHAR_T spec;
1357 workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1359 /* Get current character in format string. */
1360 JUMP (*++f, step0_jumps);
1362 /* ' ' flag. */
1363 LABEL (flag_space):
1364 space = 1;
1365 JUMP (*++f, step0_jumps);
1367 /* '+' flag. */
1368 LABEL (flag_plus):
1369 showsign = 1;
1370 JUMP (*++f, step0_jumps);
1372 /* The '-' flag. */
1373 LABEL (flag_minus):
1374 left = 1;
1375 pad = L_(' ');
1376 JUMP (*++f, step0_jumps);
1378 /* The '#' flag. */
1379 LABEL (flag_hash):
1380 alt = 1;
1381 JUMP (*++f, step0_jumps);
1383 /* The '0' flag. */
1384 LABEL (flag_zero):
1385 if (!left)
1386 pad = L_('0');
1387 JUMP (*++f, step0_jumps);
1389 /* The '\'' flag. */
1390 LABEL (flag_quote):
1391 group = 1;
1393 if (grouping == (const char *) -1)
1395 #ifdef COMPILE_WPRINTF
1396 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1397 _NL_NUMERIC_THOUSANDS_SEP_WC);
1398 #else
1399 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1400 #endif
1402 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1403 if (*grouping == '\0' || *grouping == CHAR_MAX
1404 #ifdef COMPILE_WPRINTF
1405 || thousands_sep == L'\0'
1406 #else
1407 || *thousands_sep == '\0'
1408 #endif
1410 grouping = NULL;
1412 JUMP (*++f, step0_jumps);
1414 LABEL (flag_i18n):
1415 use_outdigits = 1;
1416 JUMP (*++f, step0_jumps);
1418 /* Get width from argument. */
1419 LABEL (width_asterics):
1421 const UCHAR_T *tmp; /* Temporary value. */
1423 tmp = ++f;
1424 if (ISDIGIT (*tmp) && read_int (&tmp) && *tmp == L_('$'))
1425 /* The width comes from a positional parameter. */
1426 goto do_positional;
1428 width = va_arg (ap, int);
1430 /* Negative width means left justified. */
1431 if (width < 0)
1433 width = -width;
1434 pad = L_(' ');
1435 left = 1;
1438 if (width + 32 >= sizeof (work_buffer) / sizeof (work_buffer[0]))
1439 /* We have to use a special buffer. The "32" is just a safe
1440 bet for all the output which is not counted in the width. */
1441 workend = ((CHAR_T *) alloca ((width + 32) * sizeof (CHAR_T))
1442 + (width + 32));
1444 JUMP (*f, step1_jumps);
1446 /* Given width in format string. */
1447 LABEL (width):
1448 width = read_int (&f);
1450 if (width + 32 >= sizeof (work_buffer) / sizeof (work_buffer[0]))
1451 /* We have to use a special buffer. The "32" is just a safe
1452 bet for all the output which is not counted in the width. */
1453 workend = ((CHAR_T *) alloca ((width + 32) * sizeof (CHAR_T))
1454 + (width + 32));
1455 if (*f == L_('$'))
1456 /* Oh, oh. The argument comes from a positional parameter. */
1457 goto do_positional;
1458 JUMP (*f, step1_jumps);
1460 LABEL (precision):
1461 ++f;
1462 if (*f == L_('*'))
1464 const UCHAR_T *tmp; /* Temporary value. */
1466 tmp = ++f;
1467 if (ISDIGIT (*tmp) && read_int (&tmp) > 0 && *tmp == L_('$'))
1468 /* The precision comes from a positional parameter. */
1469 goto do_positional;
1471 prec = va_arg (ap, int);
1473 /* If the precision is negative the precision is omitted. */
1474 if (prec < 0)
1475 prec = -1;
1477 else if (ISDIGIT (*f))
1478 prec = read_int (&f);
1479 else
1480 prec = 0;
1481 if (prec > width
1482 && prec + 32 > sizeof (work_buffer) / sizeof (work_buffer[0]))
1483 workend = alloca (spec + 32) + (spec + 32);
1484 JUMP (*f, step2_jumps);
1486 /* Process 'h' modifier. There might another 'h' following. */
1487 LABEL (mod_half):
1488 is_short = 1;
1489 JUMP (*++f, step3a_jumps);
1491 /* Process 'hh' modifier. */
1492 LABEL (mod_halfhalf):
1493 is_short = 0;
1494 is_char = 1;
1495 JUMP (*++f, step4_jumps);
1497 /* Process 'l' modifier. There might another 'l' following. */
1498 LABEL (mod_long):
1499 is_long = 1;
1500 JUMP (*++f, step3b_jumps);
1502 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1503 allowed to follow. */
1504 LABEL (mod_longlong):
1505 is_long_double = 1;
1506 is_long = 1;
1507 JUMP (*++f, step4_jumps);
1509 LABEL (mod_size_t):
1510 is_long_double = sizeof (size_t) > sizeof (unsigned long int);
1511 is_long = sizeof (size_t) > sizeof (unsigned int);
1512 JUMP (*++f, step4_jumps);
1514 LABEL (mod_ptrdiff_t):
1515 is_long_double = sizeof (ptrdiff_t) > sizeof (unsigned long int);
1516 is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
1517 JUMP (*++f, step4_jumps);
1519 LABEL (mod_intmax_t):
1520 is_long_double = sizeof (intmax_t) > sizeof (unsigned long int);
1521 is_long = sizeof (intmax_t) > sizeof (unsigned int);
1522 JUMP (*++f, step4_jumps);
1524 /* Process current format. */
1525 while (1)
1527 process_arg (((struct printf_spec *) NULL));
1528 process_string_arg (((struct printf_spec *) NULL));
1530 LABEL (form_unknown):
1531 if (spec == L_('\0'))
1533 /* The format string ended before the specifier is complete. */
1534 done = -1;
1535 goto all_done;
1538 /* If we are in the fast loop force entering the complicated
1539 one. */
1540 goto do_positional;
1543 /* The format is correctly handled. */
1544 ++nspecs_done;
1546 /* Look for next format specifier. */
1547 #ifdef COMPILE_WPRINTF
1548 f = find_spec ((end_of_spec = ++f));
1549 #else
1550 f = find_spec ((end_of_spec = ++f), &mbstate);
1551 #endif
1553 /* Write the following constant string. */
1554 outstring (end_of_spec, f - end_of_spec);
1556 while (*f != L_('\0'));
1558 /* Unlock stream and return. */
1559 goto all_done;
1561 /* Here starts the more complex loop to handle positional parameters. */
1562 do_positional:
1564 /* Array with information about the needed arguments. This has to
1565 be dynamically extensible. */
1566 size_t nspecs = 0;
1567 size_t nspecs_max = 32; /* A more or less arbitrary start value. */
1568 struct printf_spec *specs
1569 = alloca (nspecs_max * sizeof (struct printf_spec));
1571 /* The number of arguments the format string requests. This will
1572 determine the size of the array needed to store the argument
1573 attributes. */
1574 size_t nargs = 0;
1575 int *args_type;
1576 union printf_arg *args_value = NULL;
1578 /* Positional parameters refer to arguments directly. This could
1579 also determine the maximum number of arguments. Track the
1580 maximum number. */
1581 size_t max_ref_arg = 0;
1583 /* Just a counter. */
1584 size_t cnt;
1587 if (grouping == (const char *) -1)
1589 #ifdef COMPILE_WPRINTF
1590 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1591 _NL_NUMERIC_THOUSANDS_SEP_WC);
1592 #else
1593 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1594 #endif
1596 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1597 if (*grouping == '\0' || *grouping == CHAR_MAX)
1598 grouping = NULL;
1601 for (f = lead_str_end; *f != L_('\0'); f = specs[nspecs++].next_fmt)
1603 if (nspecs >= nspecs_max)
1605 /* Extend the array of format specifiers. */
1606 struct printf_spec *old = specs;
1608 nspecs_max *= 2;
1609 specs = alloca (nspecs_max * sizeof (struct printf_spec));
1611 if (specs == &old[nspecs])
1612 /* Stack grows up, OLD was the last thing allocated;
1613 extend it. */
1614 nspecs_max += nspecs_max / 2;
1615 else
1617 /* Copy the old array's elements to the new space. */
1618 memcpy (specs, old, nspecs * sizeof (struct printf_spec));
1619 if (old == &specs[nspecs])
1620 /* Stack grows down, OLD was just below the new
1621 SPECS. We can use that space when the new space
1622 runs out. */
1623 nspecs_max += nspecs_max / 2;
1627 /* Parse the format specifier. */
1628 #ifdef COMPILE_WPRINTF
1629 nargs += parse_one_spec (f, nargs, &specs[nspecs], &max_ref_arg);
1630 #else
1631 nargs += parse_one_spec (f, nargs, &specs[nspecs], &max_ref_arg,
1632 &mbstate);
1633 #endif
1636 /* Determine the number of arguments the format string consumes. */
1637 nargs = MAX (nargs, max_ref_arg);
1639 /* Allocate memory for the argument descriptions. */
1640 args_type = alloca (nargs * sizeof (int));
1641 memset (args_type, 0, nargs * sizeof (int));
1642 args_value = alloca (nargs * sizeof (union printf_arg));
1644 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1645 still zero after this loop, format is invalid. For now we
1646 simply use 0 as the value. */
1648 /* Fill in the types of all the arguments. */
1649 for (cnt = 0; cnt < nspecs; ++cnt)
1651 /* If the width is determined by an argument this is an int. */
1652 if (specs[cnt].width_arg != -1)
1653 args_type[specs[cnt].width_arg] = PA_INT;
1655 /* If the precision is determined by an argument this is an int. */
1656 if (specs[cnt].prec_arg != -1)
1657 args_type[specs[cnt].prec_arg] = PA_INT;
1659 switch (specs[cnt].ndata_args)
1661 case 0: /* No arguments. */
1662 break;
1663 case 1: /* One argument; we already have the type. */
1664 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1665 break;
1666 default:
1667 /* We have more than one argument for this format spec.
1668 We must call the arginfo function again to determine
1669 all the types. */
1670 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1671 (&specs[cnt].info,
1672 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg]);
1673 break;
1677 /* Now we know all the types and the order. Fill in the argument
1678 values. */
1679 for (cnt = 0; cnt < nargs; ++cnt)
1680 switch (args_type[cnt])
1682 #define T(tag, mem, type) \
1683 case tag: \
1684 args_value[cnt].mem = va_arg (ap_save, type); \
1685 break
1687 T (PA_CHAR, pa_char, int); /* Promoted. */
1688 T (PA_WCHAR, pa_wchar, wint_t);
1689 T (PA_INT|PA_FLAG_SHORT, pa_short_int, int); /* Promoted. */
1690 T (PA_INT, pa_int, int);
1691 T (PA_INT|PA_FLAG_LONG, pa_long_int, long int);
1692 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1693 T (PA_FLOAT, pa_float, double); /* Promoted. */
1694 T (PA_DOUBLE, pa_double, double);
1695 T (PA_DOUBLE|PA_FLAG_LONG_DOUBLE, pa_long_double, long double);
1696 T (PA_STRING, pa_string, const char *);
1697 T (PA_WSTRING, pa_wstring, const wchar_t *);
1698 T (PA_POINTER, pa_pointer, void *);
1699 #undef T
1700 default:
1701 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1702 args_value[cnt].pa_pointer = va_arg (ap_save, void *);
1703 else
1704 args_value[cnt].pa_long_double = 0.0;
1705 break;
1708 /* Now walk through all format specifiers and process them. */
1709 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1711 #undef REF
1712 #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
1713 # define REF(Name) &&do2_##Name - &&do_form_unknown
1714 #else
1715 # define REF(Name) &&do2_##Name
1716 #endif
1717 #undef LABEL
1718 #define LABEL(Name) do2_##Name
1719 STEP4_TABLE;
1721 int is_negative;
1722 union
1724 unsigned long long int longlong;
1725 unsigned long int word;
1726 } number;
1727 int base;
1728 union printf_arg the_arg;
1729 CHAR_T *string; /* Pointer to argument string. */
1731 /* Fill variables from values in struct. */
1732 int alt = specs[nspecs_done].info.alt;
1733 int space = specs[nspecs_done].info.space;
1734 int left = specs[nspecs_done].info.left;
1735 int showsign = specs[nspecs_done].info.showsign;
1736 int group = specs[nspecs_done].info.group;
1737 int is_long_double = specs[nspecs_done].info.is_long_double;
1738 int is_short = specs[nspecs_done].info.is_short;
1739 int is_char = specs[nspecs_done].info.is_char;
1740 int is_long = specs[nspecs_done].info.is_long;
1741 int width = specs[nspecs_done].info.width;
1742 int prec = specs[nspecs_done].info.prec;
1743 int use_outdigits = specs[nspecs_done].info.i18n;
1744 char pad = specs[nspecs_done].info.pad;
1745 CHAR_T spec = specs[nspecs_done].info.spec;
1747 /* Fill in last information. */
1748 if (specs[nspecs_done].width_arg != -1)
1750 /* Extract the field width from an argument. */
1751 specs[nspecs_done].info.width =
1752 args_value[specs[nspecs_done].width_arg].pa_int;
1754 if (specs[nspecs_done].info.width < 0)
1755 /* If the width value is negative left justification is
1756 selected and the value is taken as being positive. */
1758 specs[nspecs_done].info.width *= -1;
1759 left = specs[nspecs_done].info.left = 1;
1761 width = specs[nspecs_done].info.width;
1764 if (specs[nspecs_done].prec_arg != -1)
1766 /* Extract the precision from an argument. */
1767 specs[nspecs_done].info.prec =
1768 args_value[specs[nspecs_done].prec_arg].pa_int;
1770 if (specs[nspecs_done].info.prec < 0)
1771 /* If the precision is negative the precision is
1772 omitted. */
1773 specs[nspecs_done].info.prec = -1;
1775 prec = specs[nspecs_done].info.prec;
1778 /* Maybe the buffer is too small. */
1779 if (MAX (prec, width) + 32 > sizeof (work_buffer) / sizeof (CHAR_T))
1780 workend = ((CHAR_T *) alloca ((MAX (prec, width) + 32)
1781 * sizeof (CHAR_T))
1782 + (MAX (prec, width) + 32));
1784 /* Process format specifiers. */
1785 while (1)
1787 JUMP (spec, step4_jumps);
1789 process_arg ((&specs[nspecs_done]));
1790 process_string_arg ((&specs[nspecs_done]));
1792 LABEL (form_unknown):
1794 extern printf_function **__printf_function_table;
1795 int function_done;
1796 printf_function *function;
1797 unsigned int i;
1798 const void **ptr;
1800 function =
1801 (__printf_function_table == NULL ? NULL :
1802 __printf_function_table[specs[nspecs_done].info.spec]);
1804 if (function == NULL)
1805 function = &printf_unknown;
1807 ptr = alloca (specs[nspecs_done].ndata_args
1808 * sizeof (const void *));
1810 /* Fill in an array of pointers to the argument values. */
1811 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
1812 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1814 /* Call the function. */
1815 function_done = (*function) (s, &specs[nspecs_done].info, ptr);
1817 /* If an error occurred we don't have information about #
1818 of chars. */
1819 if (function_done < 0)
1821 done = -1;
1822 goto all_done;
1825 done += function_done;
1827 break;
1830 /* Write the following constant string. */
1831 outstring (specs[nspecs_done].end_of_fmt,
1832 specs[nspecs_done].next_fmt
1833 - specs[nspecs_done].end_of_fmt);
1837 all_done:
1838 /* Unlock the stream. */
1839 #ifdef USE_IN_LIBIO
1840 _IO_funlockfile (s);
1841 #else
1842 __funlockfile (s);
1843 #endif
1844 __libc_cleanup_region_end (0);
1846 return done;
1849 /* Handle an unknown format specifier. This prints out a canonicalized
1850 representation of the format spec itself. */
1851 static int
1852 printf_unknown (FILE *s, const struct printf_info *info,
1853 const void *const *args)
1856 int done = 0;
1857 CHAR_T work_buffer[MAX (info->width, info->spec) + 32];
1858 CHAR_T *const workend
1859 = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1860 register CHAR_T *w;
1862 outchar (L_('%'));
1864 if (info->alt)
1865 outchar (L_('#'));
1866 if (info->group)
1867 outchar (L_('\''));
1868 if (info->showsign)
1869 outchar (L_('+'));
1870 else if (info->space)
1871 outchar (L_(' '));
1872 if (info->left)
1873 outchar (L_('-'));
1874 if (info->pad == L_('0'))
1875 outchar (L_('0'));
1876 if (info->i18n)
1877 outchar (L_('I'));
1879 if (info->width != 0)
1881 w = _itoa_word (info->width, workend, 10, 0);
1882 while (w < workend)
1883 outchar (*w++);
1886 if (info->prec != -1)
1888 outchar (L_('.'));
1889 w = _itoa_word (info->prec, workend, 10, 0);
1890 while (w < workend)
1891 outchar (*w++);
1894 if (info->spec != L_('\0'))
1895 outchar (info->spec);
1897 all_done:
1898 return done;
1901 /* Group the digits according to the grouping rules of the current locale.
1902 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1903 static CHAR_T *
1904 internal_function
1905 group_number (CHAR_T *w, CHAR_T *rear_ptr, const char *grouping,
1906 #ifdef COMPILE_WPRINTF
1907 wchar_t thousands_sep
1908 #else
1909 const char *thousands_sep
1910 #endif
1913 int len;
1914 CHAR_T *src, *s;
1915 #ifndef COMPILE_WPRINTF
1916 int tlen = strlen (thousands_sep);
1917 #endif
1919 /* We treat all negative values like CHAR_MAX. */
1921 if (*grouping == CHAR_MAX || *grouping <= 0)
1922 /* No grouping should be done. */
1923 return w;
1925 len = *grouping;
1927 /* Copy existing string so that nothing gets overwritten. */
1928 src = (CHAR_T *) alloca ((rear_ptr - w) * sizeof (CHAR_T));
1929 s = (CHAR_T *) __mempcpy (src, w,
1930 (rear_ptr - w) * sizeof (CHAR_T));
1931 w = rear_ptr;
1933 /* Process all characters in the string. */
1934 while (s > src)
1936 *--w = *--s;
1938 if (--len == 0 && s > src)
1940 /* A new group begins. */
1941 #ifdef COMPILE_WPRINTF
1942 *--w = thousands_sep;
1943 #else
1944 int cnt = tlen;
1946 *--w = thousands_sep[--cnt];
1947 while (cnt > 0);
1948 #endif
1950 len = *grouping++;
1951 if (*grouping == '\0')
1952 /* The previous grouping repeats ad infinitum. */
1953 --grouping;
1954 else if (*grouping == CHAR_MAX
1955 #if CHAR_MIN < 0
1956 || *grouping < 0
1957 #endif
1960 /* No further grouping to be done.
1961 Copy the rest of the number. */
1963 *--w = *--s;
1964 while (s > src);
1965 break;
1969 return w;
1972 #ifdef USE_IN_LIBIO
1973 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
1974 struct helper_file
1976 struct _IO_FILE_plus _f;
1977 #ifdef COMPILE_WPRINTF
1978 struct _IO_wide_data _wide_data;
1979 #endif
1980 _IO_FILE *_put_stream;
1981 #ifdef _IO_MTSAFE_IO
1982 _IO_lock_t lock;
1983 #endif
1986 static int
1987 _IO_helper_overflow (_IO_FILE *s, int c)
1989 _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
1990 #ifdef COMPILE_WPRINTF
1991 int used = s->_wide_data->_IO_write_ptr - s->_wide_data->_IO_write_base;
1992 if (used)
1994 _IO_size_t written = _IO_sputn (target, s->_wide_data->_IO_write_base,
1995 used);
1996 s->_wide_data->_IO_write_ptr -= written;
1998 #else
1999 int used = s->_IO_write_ptr - s->_IO_write_base;
2000 if (used)
2002 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
2003 s->_IO_write_ptr -= written;
2005 #endif
2006 return PUTC (c, s);
2009 #ifdef COMPILE_WPRINTF
2010 static const struct _IO_jump_t _IO_helper_jumps =
2012 JUMP_INIT_DUMMY,
2013 JUMP_INIT (finish, _IO_wdefault_finish),
2014 JUMP_INIT (overflow, _IO_helper_overflow),
2015 JUMP_INIT (underflow, _IO_default_underflow),
2016 JUMP_INIT (uflow, _IO_default_uflow),
2017 JUMP_INIT (pbackfail, (_IO_pbackfail_t) _IO_wdefault_pbackfail),
2018 JUMP_INIT (xsputn, _IO_wdefault_xsputn),
2019 JUMP_INIT (xsgetn, _IO_wdefault_xsgetn),
2020 JUMP_INIT (seekoff, _IO_default_seekoff),
2021 JUMP_INIT (seekpos, _IO_default_seekpos),
2022 JUMP_INIT (setbuf,(_IO_setbuf_t) _IO_wdefault_setbuf),
2023 JUMP_INIT (sync, _IO_default_sync),
2024 JUMP_INIT (doallocate, _IO_wdefault_doallocate),
2025 JUMP_INIT (read, _IO_default_read),
2026 JUMP_INIT (write, _IO_default_write),
2027 JUMP_INIT (seek, _IO_default_seek),
2028 JUMP_INIT (close, _IO_default_close),
2029 JUMP_INIT (stat, _IO_default_stat)
2031 #else
2032 static const struct _IO_jump_t _IO_helper_jumps =
2034 JUMP_INIT_DUMMY,
2035 JUMP_INIT (finish, _IO_default_finish),
2036 JUMP_INIT (overflow, _IO_helper_overflow),
2037 JUMP_INIT (underflow, _IO_default_underflow),
2038 JUMP_INIT (uflow, _IO_default_uflow),
2039 JUMP_INIT (pbackfail, _IO_default_pbackfail),
2040 JUMP_INIT (xsputn, _IO_default_xsputn),
2041 JUMP_INIT (xsgetn, _IO_default_xsgetn),
2042 JUMP_INIT (seekoff, _IO_default_seekoff),
2043 JUMP_INIT (seekpos, _IO_default_seekpos),
2044 JUMP_INIT (setbuf, _IO_default_setbuf),
2045 JUMP_INIT (sync, _IO_default_sync),
2046 JUMP_INIT (doallocate, _IO_default_doallocate),
2047 JUMP_INIT (read, _IO_default_read),
2048 JUMP_INIT (write, _IO_default_write),
2049 JUMP_INIT (seek, _IO_default_seek),
2050 JUMP_INIT (close, _IO_default_close),
2051 JUMP_INIT (stat, _IO_default_stat)
2053 #endif
2055 static int
2056 internal_function
2057 buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format,
2058 _IO_va_list args)
2060 CHAR_T buf[_IO_BUFSIZ];
2061 struct helper_file helper;
2062 register _IO_FILE *hp = (_IO_FILE *) &helper._f;
2063 int result, to_flush;
2065 /* Orient the stream. */
2066 #ifdef ORIENT
2067 ORIENT;
2068 #endif
2070 /* Initialize helper. */
2071 helper._put_stream = s;
2072 #ifdef COMPILE_WPRINTF
2073 hp->_wide_data = &helper._wide_data;
2074 _IO_wsetp (hp, buf, buf + sizeof buf / sizeof (CHAR_T));
2075 hp->_mode = 1;
2076 #else
2077 _IO_setp (hp, buf, buf + sizeof buf);
2078 hp->_mode = -1;
2079 #endif
2080 hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS;
2081 #if _IO_JUMPS_OFFSET
2082 hp->_vtable_offset = 0;
2083 #endif
2084 #ifdef _IO_MTSAFE_IO
2085 hp->_lock = &helper.lock;
2086 __libc_lock_init (*hp->_lock);
2087 #endif
2088 _IO_JUMPS (&helper._f) = (struct _IO_jump_t *) &_IO_helper_jumps;
2090 /* Now print to helper instead. */
2091 result = vfprintf (hp, format, args);
2093 /* Lock stream. */
2094 __libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
2095 _IO_flockfile (s);
2097 /* Now flush anything from the helper to the S. */
2098 #ifdef COMPILE_WPRINTF
2099 if ((to_flush = (hp->_wide_data->_IO_write_ptr
2100 - hp->_wide_data->_IO_write_base)) > 0)
2102 if ((int) _IO_sputn (s, hp->_wide_data->_IO_write_base, to_flush)
2103 != to_flush)
2104 result = -1;
2106 #else
2107 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
2109 if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
2110 result = -1;
2112 #endif
2114 /* Unlock the stream. */
2115 _IO_funlockfile (s);
2116 __libc_cleanup_region_end (0);
2118 return result;
2121 #else /* !USE_IN_LIBIO */
2123 static int
2124 internal_function
2125 buffered_vfprintf (register FILE *s, const CHAR_T *format, va_list args)
2127 char buf[BUFSIZ];
2128 int result;
2130 /* Orient the stream. */
2131 #ifdef ORIENT
2132 ORIENT;
2133 #endif
2135 s->__bufp = s->__buffer = buf;
2136 s->__bufsize = sizeof buf;
2137 s->__put_limit = s->__buffer + s->__bufsize;
2138 s->__get_limit = s->__buffer;
2140 /* Now use buffer to print. */
2141 result = vfprintf (s, format, args);
2143 if (fflush (s) == EOF)
2144 result = -1;
2145 s->__buffer = s->__bufp = s->__get_limit = s->__put_limit = NULL;
2146 s->__bufsize = 0;
2148 return result;
2151 /* Pads string with given number of a specified character.
2152 This code is taken from iopadn.c of the GNU I/O library. */
2153 #define PADSIZE 16
2154 static const CHAR_T blanks[PADSIZE] =
2155 { L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '),
2156 L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' ') };
2157 static const CHAR_T zeroes[PADSIZE] =
2158 { L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'),
2159 L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0') };
2161 ssize_t
2162 #ifndef COMPILE_WPRINTF
2163 __printf_pad (FILE *s, char pad, size_t count)
2164 #else
2165 __wprintf_pad (FILE *s, wchar_t pad, size_t count)
2166 #endif
2168 const CHAR_T *padptr;
2169 register size_t i;
2171 padptr = pad == L_(' ') ? blanks : zeroes;
2173 for (i = count; i >= PADSIZE; i -= PADSIZE)
2174 if (PUT (s, padptr, PADSIZE) != PADSIZE)
2175 return -1;
2176 if (i > 0)
2177 if (PUT (s, padptr, i) != i)
2178 return -1;
2180 return count;
2182 #undef PADSIZE
2183 #endif /* USE_IN_LIBIO */
2185 #ifdef USE_IN_LIBIO
2186 # undef vfprintf
2187 # ifdef strong_alias
2188 /* This is for glibc. */
2189 # ifdef COMPILE_WPRINTF
2190 strong_alias (_IO_vfwprintf, vfwprintf);
2191 # else
2192 strong_alias (_IO_vfprintf, vfprintf);
2193 # endif
2194 # else
2195 # if defined __ELF__ || defined __GNU_LIBRARY__
2196 # include <gnu-stabs.h>
2197 # ifdef weak_alias
2198 # ifdef COMPILE_WPRINTF
2199 weak_alias (_IO_vfwprintf, vfwprintf);
2200 # else
2201 weak_alias (_IO_vfprintf, vfprintf);
2202 # endif
2203 # endif
2204 # endif
2205 # endif
2206 #endif