* sysdeps/unix/sysv/linux/alpha/clone.S: Use HIDDEN_JUMPTARGET. ...
[glibc.git] / stdio-common / vfprintf.c
blob16f280ed4f0e85388e7876a58475bd6ef5474285
1 /* Copyright (C) 1991-2002, 2003 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 Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 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>
32 #include <stdio.h>
34 /* This code is shared between the standard stdio implementation found
35 in GNU C library and the libio implementation originally found in
36 GNU libg++.
38 Beside this it is also shared between the normal and wide character
39 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
42 #ifdef USE_IN_LIBIO
43 /* This code is for use in libio. */
44 # include <libioP.h>
45 # define FILE _IO_FILE
46 # undef va_list
47 # define va_list _IO_va_list
48 # undef BUFSIZ
49 # define BUFSIZ _IO_BUFSIZ
50 # define ARGCHECK(S, Format) \
51 do \
52 { \
53 /* Check file argument for consistence. */ \
54 CHECK_FILE (S, -1); \
55 if (S->_flags & _IO_NO_WRITES) \
56 { \
57 __set_errno (EBADF); \
58 return -1; \
59 } \
60 if (Format == NULL) \
61 { \
62 MAYBE_SET_EINVAL; \
63 return -1; \
64 } \
65 } while (0)
66 # define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
68 # ifndef COMPILE_WPRINTF
69 # define vfprintf _IO_vfprintf
70 # define CHAR_T char
71 # define UCHAR_T unsigned char
72 # define INT_T int
73 # define L_(Str) Str
74 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
76 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
77 # define PAD(Padchar) \
78 if (width > 0) \
79 done += INTUSE(_IO_padn) (s, (Padchar), width)
80 # define PUTC(C, F) _IO_putc_unlocked (C, F)
81 # define ORIENT if (s->_vtable_offset == 0 && _IO_fwide (s, -1) != -1)\
82 return -1
83 # else
84 # define vfprintf _IO_vfwprintf
85 # define CHAR_T wchar_t
86 /* This is a hack!!! There should be a type uwchar_t. */
87 # define UCHAR_T unsigned int /* uwchar_t */
88 # define INT_T wint_t
89 # define L_(Str) L##Str
90 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
92 # include "_itowa.h"
94 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
95 # define PAD(Padchar) \
96 if (width > 0) \
97 done += _IO_wpadn (s, (Padchar), width)
98 # define PUTC(C, F) _IO_putwc_unlocked (C, F)
99 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
101 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
102 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
103 # undef EOF
104 # define EOF WEOF
105 # endif
106 #else /* ! USE_IN_LIBIO */
107 /* This code is for use in the GNU C library. */
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 *workstart = NULL;
257 CHAR_T *workend;
259 /* State for restartable multibyte character handling functions. */
260 #ifndef COMPILE_WPRINTF
261 mbstate_t mbstate;
262 #endif
264 /* We have to save the original argument pointer. */
265 va_list ap_save;
267 /* Count number of specifiers we already processed. */
268 int nspecs_done;
270 /* For the %m format we may need the current `errno' value. */
271 int save_errno = errno;
274 /* This table maps a character into a number representing a
275 class. In each step there is a destination label for each
276 class. */
277 static const int jump_table[] =
279 /* ' ' */ 1, 0, 0, /* '#' */ 4,
280 0, /* '%' */ 14, 0, /* '\''*/ 6,
281 0, 0, /* '*' */ 7, /* '+' */ 2,
282 0, /* '-' */ 3, /* '.' */ 9, 0,
283 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
284 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
285 /* '8' */ 8, /* '9' */ 8, 0, 0,
286 0, 0, 0, 0,
287 0, /* 'A' */ 26, 0, /* 'C' */ 25,
288 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
289 0, /* 'I' */ 29, 0, 0,
290 /* 'L' */ 12, 0, 0, 0,
291 0, 0, 0, /* 'S' */ 21,
292 0, 0, 0, 0,
293 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
294 0, 0, 0, 0,
295 0, /* 'a' */ 26, 0, /* 'c' */ 20,
296 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
297 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
298 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
299 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
300 /* 't' */ 27, /* 'u' */ 16, 0, 0,
301 /* 'x' */ 18, 0, /* 'z' */ 13
304 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
305 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
306 #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
307 /* 'int' is enough and it saves some space on 64 bit systems. */
308 # define JUMP_TABLE_TYPE const int
309 # define JUMP(ChExpr, table) \
310 do \
312 int offset; \
313 void *__unbounded ptr; \
314 spec = (ChExpr); \
315 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
316 : table[CHAR_CLASS (spec)]; \
317 ptr = &&do_form_unknown + offset; \
318 goto *ptr; \
320 while (0)
321 #else
322 # define JUMP_TABLE_TYPE const void *const
323 # define JUMP(ChExpr, table) \
324 do \
326 const void *__unbounded ptr; \
327 spec = (ChExpr); \
328 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
329 : table[CHAR_CLASS (spec)]; \
330 goto *ptr; \
332 while (0)
333 #endif
335 #define STEP0_3_TABLE \
336 /* Step 0: at the beginning. */ \
337 static JUMP_TABLE_TYPE step0_jumps[30] = \
339 REF (form_unknown), \
340 REF (flag_space), /* for ' ' */ \
341 REF (flag_plus), /* for '+' */ \
342 REF (flag_minus), /* for '-' */ \
343 REF (flag_hash), /* for '<hash>' */ \
344 REF (flag_zero), /* for '0' */ \
345 REF (flag_quote), /* for '\'' */ \
346 REF (width_asterics), /* for '*' */ \
347 REF (width), /* for '1'...'9' */ \
348 REF (precision), /* 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', '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', '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 REF (mod_ptrdiff_t), /* for 't' */ \
367 REF (mod_intmax_t), /* for 'j' */ \
368 REF (flag_i18n), /* for 'I' */ \
369 }; \
370 /* Step 1: after processing width. */ \
371 static JUMP_TABLE_TYPE step1_jumps[30] = \
373 REF (form_unknown), \
374 REF (form_unknown), /* for ' ' */ \
375 REF (form_unknown), /* for '+' */ \
376 REF (form_unknown), /* for '-' */ \
377 REF (form_unknown), /* for '<hash>' */ \
378 REF (form_unknown), /* for '0' */ \
379 REF (form_unknown), /* for '\'' */ \
380 REF (form_unknown), /* for '*' */ \
381 REF (form_unknown), /* for '1'...'9' */ \
382 REF (precision), /* for '.' */ \
383 REF (mod_half), /* for 'h' */ \
384 REF (mod_long), /* for 'l' */ \
385 REF (mod_longlong), /* for 'L', 'q' */ \
386 REF (mod_size_t), /* for 'z', 'Z' */ \
387 REF (form_percent), /* for '%' */ \
388 REF (form_integer), /* for 'd', 'i' */ \
389 REF (form_unsigned), /* for 'u' */ \
390 REF (form_octal), /* for 'o' */ \
391 REF (form_hexa), /* for 'X', 'x' */ \
392 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
393 REF (form_character), /* for 'c' */ \
394 REF (form_string), /* for 's', 'S' */ \
395 REF (form_pointer), /* for 'p' */ \
396 REF (form_number), /* for 'n' */ \
397 REF (form_strerror), /* for 'm' */ \
398 REF (form_wcharacter), /* for 'C' */ \
399 REF (form_floathex), /* for 'A', 'a' */ \
400 REF (mod_ptrdiff_t), /* for 't' */ \
401 REF (mod_intmax_t), /* for 'j' */ \
402 REF (flag_i18n) /* for 'I' */ \
403 }; \
404 /* Step 2: after processing precision. */ \
405 static JUMP_TABLE_TYPE step2_jumps[30] = \
407 REF (form_unknown), \
408 REF (form_unknown), /* for ' ' */ \
409 REF (form_unknown), /* for '+' */ \
410 REF (form_unknown), /* for '-' */ \
411 REF (form_unknown), /* for '<hash>' */ \
412 REF (form_unknown), /* for '0' */ \
413 REF (form_unknown), /* for '\'' */ \
414 REF (form_unknown), /* for '*' */ \
415 REF (form_unknown), /* for '1'...'9' */ \
416 REF (form_unknown), /* for '.' */ \
417 REF (mod_half), /* for 'h' */ \
418 REF (mod_long), /* for 'l' */ \
419 REF (mod_longlong), /* for 'L', 'q' */ \
420 REF (mod_size_t), /* for 'z', 'Z' */ \
421 REF (form_percent), /* for '%' */ \
422 REF (form_integer), /* for 'd', 'i' */ \
423 REF (form_unsigned), /* for 'u' */ \
424 REF (form_octal), /* for 'o' */ \
425 REF (form_hexa), /* for 'X', 'x' */ \
426 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
427 REF (form_character), /* for 'c' */ \
428 REF (form_string), /* for 's', 'S' */ \
429 REF (form_pointer), /* for 'p' */ \
430 REF (form_number), /* for 'n' */ \
431 REF (form_strerror), /* for 'm' */ \
432 REF (form_wcharacter), /* for 'C' */ \
433 REF (form_floathex), /* for 'A', 'a' */ \
434 REF (mod_ptrdiff_t), /* for 't' */ \
435 REF (mod_intmax_t), /* for 'j' */ \
436 REF (flag_i18n) /* for 'I' */ \
437 }; \
438 /* Step 3a: after processing first 'h' modifier. */ \
439 static JUMP_TABLE_TYPE step3a_jumps[30] = \
441 REF (form_unknown), \
442 REF (form_unknown), /* for ' ' */ \
443 REF (form_unknown), /* for '+' */ \
444 REF (form_unknown), /* for '-' */ \
445 REF (form_unknown), /* for '<hash>' */ \
446 REF (form_unknown), /* for '0' */ \
447 REF (form_unknown), /* for '\'' */ \
448 REF (form_unknown), /* for '*' */ \
449 REF (form_unknown), /* for '1'...'9' */ \
450 REF (form_unknown), /* for '.' */ \
451 REF (mod_halfhalf), /* for 'h' */ \
452 REF (form_unknown), /* for 'l' */ \
453 REF (form_unknown), /* for 'L', 'q' */ \
454 REF (form_unknown), /* for 'z', 'Z' */ \
455 REF (form_percent), /* for '%' */ \
456 REF (form_integer), /* for 'd', 'i' */ \
457 REF (form_unsigned), /* for 'u' */ \
458 REF (form_octal), /* for 'o' */ \
459 REF (form_hexa), /* for 'X', 'x' */ \
460 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
461 REF (form_unknown), /* for 'c' */ \
462 REF (form_unknown), /* for 's', 'S' */ \
463 REF (form_unknown), /* for 'p' */ \
464 REF (form_number), /* for 'n' */ \
465 REF (form_unknown), /* for 'm' */ \
466 REF (form_unknown), /* for 'C' */ \
467 REF (form_unknown), /* for 'A', 'a' */ \
468 REF (form_unknown), /* for 't' */ \
469 REF (form_unknown), /* for 'j' */ \
470 REF (form_unknown) /* for 'I' */ \
471 }; \
472 /* Step 3b: after processing first 'l' modifier. */ \
473 static JUMP_TABLE_TYPE step3b_jumps[30] = \
475 REF (form_unknown), \
476 REF (form_unknown), /* for ' ' */ \
477 REF (form_unknown), /* for '+' */ \
478 REF (form_unknown), /* for '-' */ \
479 REF (form_unknown), /* for '<hash>' */ \
480 REF (form_unknown), /* for '0' */ \
481 REF (form_unknown), /* for '\'' */ \
482 REF (form_unknown), /* for '*' */ \
483 REF (form_unknown), /* for '1'...'9' */ \
484 REF (form_unknown), /* for '.' */ \
485 REF (form_unknown), /* for 'h' */ \
486 REF (mod_longlong), /* for 'l' */ \
487 REF (form_unknown), /* for 'L', 'q' */ \
488 REF (form_unknown), /* for 'z', 'Z' */ \
489 REF (form_percent), /* for '%' */ \
490 REF (form_integer), /* for 'd', 'i' */ \
491 REF (form_unsigned), /* for 'u' */ \
492 REF (form_octal), /* for 'o' */ \
493 REF (form_hexa), /* for 'X', 'x' */ \
494 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
495 REF (form_character), /* for 'c' */ \
496 REF (form_string), /* for 's', 'S' */ \
497 REF (form_pointer), /* for 'p' */ \
498 REF (form_number), /* for 'n' */ \
499 REF (form_strerror), /* for 'm' */ \
500 REF (form_wcharacter), /* for 'C' */ \
501 REF (form_floathex), /* for 'A', 'a' */ \
502 REF (form_unknown), /* for 't' */ \
503 REF (form_unknown), /* for 'j' */ \
504 REF (form_unknown) /* for 'I' */ \
507 #define STEP4_TABLE \
508 /* Step 4: processing format specifier. */ \
509 static JUMP_TABLE_TYPE step4_jumps[30] = \
511 REF (form_unknown), \
512 REF (form_unknown), /* for ' ' */ \
513 REF (form_unknown), /* for '+' */ \
514 REF (form_unknown), /* for '-' */ \
515 REF (form_unknown), /* for '<hash>' */ \
516 REF (form_unknown), /* for '0' */ \
517 REF (form_unknown), /* for '\'' */ \
518 REF (form_unknown), /* for '*' */ \
519 REF (form_unknown), /* for '1'...'9' */ \
520 REF (form_unknown), /* for '.' */ \
521 REF (form_unknown), /* for 'h' */ \
522 REF (form_unknown), /* for 'l' */ \
523 REF (form_unknown), /* for 'L', 'q' */ \
524 REF (form_unknown), /* for 'z', 'Z' */ \
525 REF (form_percent), /* for '%' */ \
526 REF (form_integer), /* for 'd', 'i' */ \
527 REF (form_unsigned), /* for 'u' */ \
528 REF (form_octal), /* for 'o' */ \
529 REF (form_hexa), /* for 'X', 'x' */ \
530 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
531 REF (form_character), /* for 'c' */ \
532 REF (form_string), /* for 's', 'S' */ \
533 REF (form_pointer), /* for 'p' */ \
534 REF (form_number), /* for 'n' */ \
535 REF (form_strerror), /* for 'm' */ \
536 REF (form_wcharacter), /* for 'C' */ \
537 REF (form_floathex), /* for 'A', 'a' */ \
538 REF (form_unknown), /* for 't' */ \
539 REF (form_unknown), /* for 'j' */ \
540 REF (form_unknown) /* for 'I' */ \
544 #define process_arg(fspec) \
545 /* Start real work. We know about all flags and modifiers and \
546 now process the wanted format specifier. */ \
547 LABEL (form_percent): \
548 /* Write a literal "%". */ \
549 outchar (L_('%')); \
550 break; \
552 LABEL (form_integer): \
553 /* Signed decimal integer. */ \
554 base = 10; \
556 if (is_longlong) \
558 long long int signed_number; \
560 if (fspec == NULL) \
561 signed_number = va_arg (ap, long long int); \
562 else \
563 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
565 is_negative = signed_number < 0; \
566 number.longlong = is_negative ? (- signed_number) : signed_number; \
568 goto LABEL (longlong_number); \
570 else \
572 long int signed_number; \
574 if (fspec == NULL) \
576 if (is_long_num) \
577 signed_number = va_arg (ap, long int); \
578 else /* `char' and `short int' will be promoted to `int'. */ \
579 signed_number = va_arg (ap, int); \
581 else \
582 if (is_long_num) \
583 signed_number = args_value[fspec->data_arg].pa_long_int; \
584 else /* `char' and `short int' will be promoted to `int'. */ \
585 signed_number = args_value[fspec->data_arg].pa_int; \
587 is_negative = signed_number < 0; \
588 number.word = is_negative ? (- signed_number) : signed_number; \
590 goto LABEL (number); \
592 /* NOTREACHED */ \
594 LABEL (form_unsigned): \
595 /* Unsigned decimal integer. */ \
596 base = 10; \
597 goto LABEL (unsigned_number); \
598 /* NOTREACHED */ \
600 LABEL (form_octal): \
601 /* Unsigned octal integer. */ \
602 base = 8; \
603 goto LABEL (unsigned_number); \
604 /* NOTREACHED */ \
606 LABEL (form_hexa): \
607 /* Unsigned hexadecimal integer. */ \
608 base = 16; \
610 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
612 /* ISO specifies the `+' and ` ' flags only for signed \
613 conversions. */ \
614 is_negative = 0; \
615 showsign = 0; \
616 space = 0; \
618 if (is_longlong) \
620 if (fspec == NULL) \
621 number.longlong = va_arg (ap, unsigned long long int); \
622 else \
623 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
625 LABEL (longlong_number): \
626 if (prec < 0) \
627 /* Supply a default precision if none was given. */ \
628 prec = 1; \
629 else \
630 /* We have to take care for the '0' flag. If a precision \
631 is given it must be ignored. */ \
632 pad = L_(' '); \
634 /* If the precision is 0 and the number is 0 nothing has to \
635 be written for the number, except for the 'o' format in \
636 alternate form. */ \
637 if (prec == 0 && number.longlong == 0) \
639 string = workend; \
640 if (base == 8 && alt) \
641 *--string = L_('0'); \
643 else \
645 /* Put the number in WORK. */ \
646 string = _itoa (number.longlong, workend, base, \
647 spec == L_('X')); \
648 if (group && grouping) \
649 string = group_number (string, workend, grouping, \
650 thousands_sep); \
652 if (use_outdigits && base == 10) \
653 string = _i18n_number_rewrite (string, workend); \
655 /* Simplify further test for num != 0. */ \
656 number.word = number.longlong != 0; \
658 else \
660 if (fspec == NULL) \
662 if (is_long_num) \
663 number.word = va_arg (ap, unsigned long int); \
664 else if (is_char) \
665 number.word = (unsigned char) va_arg (ap, unsigned int); \
666 else if (!is_short) \
667 number.word = va_arg (ap, unsigned int); \
668 else \
669 number.word = (unsigned short int) va_arg (ap, unsigned int); \
671 else \
672 if (is_long_num) \
673 number.word = args_value[fspec->data_arg].pa_u_long_int; \
674 else if (is_char) \
675 number.word = (unsigned char) \
676 args_value[fspec->data_arg].pa_u_int; \
677 else if (!is_short) \
678 number.word = args_value[fspec->data_arg].pa_u_int; \
679 else \
680 number.word = (unsigned short int) \
681 args_value[fspec->data_arg].pa_u_int; \
683 LABEL (number): \
684 if (prec < 0) \
685 /* Supply a default precision if none was given. */ \
686 prec = 1; \
687 else \
688 /* We have to take care for the '0' flag. If a precision \
689 is given it must be ignored. */ \
690 pad = L_(' '); \
692 /* If the precision is 0 and the number is 0 nothing has to \
693 be written for the number, except for the 'o' format in \
694 alternate form. */ \
695 if (prec == 0 && number.word == 0) \
697 string = workend; \
698 if (base == 8 && alt) \
699 *--string = L_('0'); \
701 else \
703 /* Put the number in WORK. */ \
704 string = _itoa_word (number.word, workend, base, \
705 spec == L_('X')); \
706 if (group && grouping) \
707 string = group_number (string, workend, grouping, \
708 thousands_sep); \
710 if (use_outdigits && base == 10) \
711 string = _i18n_number_rewrite (string, workend); \
715 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
716 /* Add octal marker. */ \
717 *--string = L_('0'); \
719 prec = MAX (0, prec - (workend - string)); \
721 if (!left) \
723 width -= workend - string + prec; \
725 if (number.word != 0 && alt && base == 16) \
726 /* Account for 0X hex marker. */ \
727 width -= 2; \
729 if (is_negative || showsign || space) \
730 --width; \
732 if (pad == L_(' ')) \
734 PAD (L_(' ')); \
735 width = 0; \
738 if (is_negative) \
739 outchar (L_('-')); \
740 else if (showsign) \
741 outchar (L_('+')); \
742 else if (space) \
743 outchar (L_(' ')); \
745 if (number.word != 0 && alt && base == 16) \
747 outchar (L_('0')); \
748 outchar (spec); \
751 width += prec; \
752 PAD (L_('0')); \
754 outstring (string, workend - string); \
756 break; \
758 else \
760 if (is_negative) \
762 outchar (L_('-')); \
763 --width; \
765 else if (showsign) \
767 outchar (L_('+')); \
768 --width; \
770 else if (space) \
772 outchar (L_(' ')); \
773 --width; \
776 if (number.word != 0 && alt && base == 16) \
778 outchar (L_('0')); \
779 outchar (spec); \
780 width -= 2; \
783 width -= workend - string + prec; \
785 if (prec > 0) \
787 int temp = width; \
788 width = prec; \
789 PAD (L_('0'));; \
790 width = temp; \
793 outstring (string, workend - string); \
795 PAD (L_(' ')); \
796 break; \
799 LABEL (form_float): \
801 /* Floating-point number. This is handled by printf_fp.c. */ \
802 const void *ptr; \
803 int function_done; \
805 if (fspec == NULL) \
807 struct printf_info info = { .prec = prec, \
808 .width = width, \
809 .spec = spec, \
810 .is_long_double = is_long_double, \
811 .is_short = is_short, \
812 .is_long = is_long, \
813 .alt = alt, \
814 .space = space, \
815 .left = left, \
816 .showsign = showsign, \
817 .group = group, \
818 .pad = pad, \
819 .extra = 0, \
820 .wide = sizeof (CHAR_T) != 1 }; \
822 if (is_long_double) \
823 the_arg.pa_long_double = va_arg (ap, long double); \
824 else \
825 the_arg.pa_double = va_arg (ap, double); \
826 ptr = (const void *) &the_arg; \
828 function_done = __printf_fp (s, &info, &ptr); \
830 else \
832 ptr = (const void *) &args_value[fspec->data_arg]; \
834 function_done = __printf_fp (s, &fspec->info, &ptr); \
837 if (function_done < 0) \
839 /* Error in print handler. */ \
840 done = -1; \
841 goto all_done; \
844 done += function_done; \
846 break; \
848 LABEL (form_floathex): \
850 /* Floating point number printed as hexadecimal number. */ \
851 const void *ptr; \
852 int function_done; \
854 if (fspec == NULL) \
856 struct printf_info info = { .prec = prec, \
857 .width = width, \
858 .spec = spec, \
859 .is_long_double = is_long_double, \
860 .is_short = is_short, \
861 .is_long = is_long, \
862 .alt = alt, \
863 .space = space, \
864 .left = left, \
865 .showsign = showsign, \
866 .group = group, \
867 .pad = pad, \
868 .extra = 0, \
869 .wide = sizeof (CHAR_T) != 1 }; \
871 if (is_long_double) \
872 the_arg.pa_long_double = va_arg (ap, long double); \
873 else \
874 the_arg.pa_double = va_arg (ap, double); \
875 ptr = (const void *) &the_arg; \
877 function_done = __printf_fphex (s, &info, &ptr); \
879 else \
881 ptr = (const void *) &args_value[fspec->data_arg]; \
883 function_done = __printf_fphex (s, &fspec->info, &ptr); \
886 if (function_done < 0) \
888 /* Error in print handler. */ \
889 done = -1; \
890 goto all_done; \
893 done += function_done; \
895 break; \
897 LABEL (form_pointer): \
898 /* Generic pointer. */ \
900 const void *ptr; \
901 if (fspec == NULL) \
902 ptr = va_arg (ap, void *); \
903 else \
904 ptr = args_value[fspec->data_arg].pa_pointer; \
905 if (ptr != NULL) \
907 /* If the pointer is not NULL, write it as a %#x spec. */ \
908 base = 16; \
909 number.word = (unsigned long int) ptr; \
910 is_negative = 0; \
911 alt = 1; \
912 group = 0; \
913 spec = L_('x'); \
914 goto LABEL (number); \
916 else \
918 /* Write "(nil)" for a nil pointer. */ \
919 string = (CHAR_T *) L_("(nil)"); \
920 /* Make sure the full string "(nil)" is printed. */ \
921 if (prec < 5) \
922 prec = 5; \
923 is_long = 0; /* This is no wide-char string. */ \
924 goto LABEL (print_string); \
927 /* NOTREACHED */ \
929 LABEL (form_number): \
930 /* Answer the count of characters written. */ \
931 if (fspec == NULL) \
933 if (is_longlong) \
934 *(long long int *) va_arg (ap, void *) = done; \
935 else if (is_long_num) \
936 *(long int *) va_arg (ap, void *) = done; \
937 else if (is_char) \
938 *(char *) va_arg (ap, void *) = done; \
939 else if (!is_short) \
940 *(int *) va_arg (ap, void *) = done; \
941 else \
942 *(short int *) va_arg (ap, void *) = done; \
944 else \
945 if (is_longlong) \
946 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
947 else if (is_long_num) \
948 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
949 else if (is_char) \
950 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
951 else if (!is_short) \
952 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
953 else \
954 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
955 break; \
957 LABEL (form_strerror): \
958 /* Print description of error ERRNO. */ \
959 string = \
960 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
961 sizeof work_buffer); \
962 is_long = 0; /* This is no wide-char string. */ \
963 goto LABEL (print_string)
965 #ifdef COMPILE_WPRINTF
966 # define process_string_arg(fspec) \
967 LABEL (form_character): \
968 /* Character. */ \
969 if (is_long) \
970 goto LABEL (form_wcharacter); \
971 --width; /* Account for the character itself. */ \
972 if (!left) \
973 PAD (L' '); \
974 if (fspec == NULL) \
975 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
976 else \
977 outchar (__btowc ((unsigned char) \
978 args_value[fspec->data_arg].pa_int)); \
979 if (left) \
980 PAD (L' '); \
981 break; \
983 LABEL (form_wcharacter): \
985 /* Wide character. */ \
986 --width; \
987 if (!left) \
988 PAD (L' '); \
989 if (fspec == NULL) \
990 outchar (va_arg (ap, wchar_t)); \
991 else \
992 outchar (args_value[fspec->data_arg].pa_wchar); \
993 if (left) \
994 PAD (L' '); \
996 break; \
998 LABEL (form_string): \
1000 size_t len; \
1001 int string_malloced; \
1003 /* The string argument could in fact be `char *' or `wchar_t *'. \
1004 But this should not make a difference here. */ \
1005 if (fspec == NULL) \
1006 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
1007 else \
1008 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
1010 /* Entry point for printing other strings. */ \
1011 LABEL (print_string): \
1013 string_malloced = 0; \
1014 if (string == NULL) \
1016 /* Write "(null)" if there's space. */ \
1017 if (prec == -1 \
1018 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
1020 string = (CHAR_T *) null; \
1021 len = (sizeof (null) / sizeof (null[0])) - 1; \
1023 else \
1025 string = (CHAR_T *) L""; \
1026 len = 0; \
1029 else if (!is_long && spec != L_('S')) \
1031 /* This is complicated. We have to transform the multibyte \
1032 string into a wide character string. */ \
1033 const char *mbs = (const char *) string; \
1034 mbstate_t mbstate; \
1036 len = prec != -1 ? (size_t) prec : strlen (mbs); \
1038 /* Allocate dynamically an array which definitely is long \
1039 enough for the wide character version. */ \
1040 if (__libc_use_alloca (len * sizeof (wchar_t))) \
1041 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1042 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1043 == NULL) \
1045 done = -1; \
1046 goto all_done; \
1048 else \
1049 string_malloced = 1; \
1051 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1052 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1053 if (len == (size_t) -1) \
1055 /* Illegal multibyte character. */ \
1056 done = -1; \
1057 goto all_done; \
1060 else \
1062 if (prec != -1) \
1063 /* Search for the end of the string, but don't search past \
1064 the length specified by the precision. */ \
1065 len = __wcsnlen (string, prec); \
1066 else \
1067 len = __wcslen (string); \
1070 if ((width -= len) < 0) \
1072 outstring (string, len); \
1073 break; \
1076 if (!left) \
1077 PAD (L' '); \
1078 outstring (string, len); \
1079 if (left) \
1080 PAD (L' '); \
1081 if (__builtin_expect (string_malloced, 0)) \
1082 free (string); \
1084 break;
1085 #else
1086 # define process_string_arg(fspec) \
1087 LABEL (form_character): \
1088 /* Character. */ \
1089 if (is_long) \
1090 goto LABEL (form_wcharacter); \
1091 --width; /* Account for the character itself. */ \
1092 if (!left) \
1093 PAD (' '); \
1094 if (fspec == NULL) \
1095 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1096 else \
1097 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1098 if (left) \
1099 PAD (' '); \
1100 break; \
1102 LABEL (form_wcharacter): \
1104 /* Wide character. */ \
1105 char buf[MB_CUR_MAX]; \
1106 mbstate_t mbstate; \
1107 size_t len; \
1109 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1110 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1111 : args_value[fspec->data_arg].pa_wchar), \
1112 &mbstate); \
1113 if (len == (size_t) -1) \
1115 /* Something went wron gduring the conversion. Bail out. */ \
1116 done = -1; \
1117 goto all_done; \
1119 width -= len; \
1120 if (!left) \
1121 PAD (' '); \
1122 outstring (buf, len); \
1123 if (left) \
1124 PAD (' '); \
1126 break; \
1128 LABEL (form_string): \
1130 size_t len; \
1131 int string_malloced; \
1133 /* The string argument could in fact be `char *' or `wchar_t *'. \
1134 But this should not make a difference here. */ \
1135 if (fspec == NULL) \
1136 string = (char *) va_arg (ap, const char *); \
1137 else \
1138 string = (char *) args_value[fspec->data_arg].pa_string; \
1140 /* Entry point for printing other strings. */ \
1141 LABEL (print_string): \
1143 string_malloced = 0; \
1144 if (string == NULL) \
1146 /* Write "(null)" if there's space. */ \
1147 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1149 string = (char *) null; \
1150 len = sizeof (null) - 1; \
1152 else \
1154 string = (char *) ""; \
1155 len = 0; \
1158 else if (!is_long && spec != L_('S')) \
1160 if (prec != -1) \
1162 /* Search for the end of the string, but don't search past \
1163 the length (in bytes) specified by the precision. Also \
1164 don't use incomplete characters. */ \
1165 if (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MB_CUR_MAX) == 1) \
1166 len = __strnlen (string, prec); \
1167 else \
1169 /* In case we have a multibyte character set the \
1170 situation is more compilcated. We must not copy \
1171 bytes at the end which form an incomplete character. */\
1172 wchar_t ignore[prec]; \
1173 const char *str2 = string; \
1174 mbstate_t ps; \
1176 memset (&ps, '\0', sizeof (ps)); \
1177 if (__mbsnrtowcs (ignore, &str2, prec, prec, &ps) \
1178 == (size_t) -1) \
1180 done = -1; \
1181 goto all_done; \
1183 if (str2 == NULL) \
1184 len = strlen (string); \
1185 else \
1186 len = str2 - string - (ps.__count & 7); \
1189 else \
1190 len = strlen (string); \
1192 else \
1194 const wchar_t *s2 = (const wchar_t *) string; \
1195 mbstate_t mbstate; \
1197 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1199 if (prec >= 0) \
1201 /* The string `s2' might not be NUL terminated. */ \
1202 if (__libc_use_alloca (prec)) \
1203 string = (char *) alloca (prec); \
1204 else if ((string = (char *) malloc (prec)) == NULL) \
1206 done = -1; \
1207 goto all_done; \
1209 else \
1210 string_malloced = 1; \
1211 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1213 else \
1215 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1216 if (len != (size_t) -1) \
1218 assert (__mbsinit (&mbstate)); \
1219 s2 = (const wchar_t *) string; \
1220 if (__libc_use_alloca (len + 1)) \
1221 string = (char *) alloca (len + 1); \
1222 else if ((string = (char *) malloc (len + 1)) == NULL) \
1224 done = -1; \
1225 goto all_done; \
1227 else \
1228 string_malloced = 1; \
1229 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1233 if (len == (size_t) -1) \
1235 /* Illegal wide-character string. */ \
1236 done = -1; \
1237 goto all_done; \
1241 if ((width -= len) < 0) \
1243 outstring (string, len); \
1244 break; \
1247 if (!left) \
1248 PAD (' '); \
1249 outstring (string, len); \
1250 if (left) \
1251 PAD (' '); \
1252 if (__builtin_expect (string_malloced, 0)) \
1253 free (string); \
1255 break;
1256 #endif
1258 /* Orient the stream. */
1259 #ifdef ORIENT
1260 ORIENT;
1261 #endif
1263 /* Sanity check of arguments. */
1264 ARGCHECK (s, format);
1266 #ifdef ORIENT
1267 /* Check for correct orientation. */
1268 if (
1269 # ifdef USE_IN_LIBIO
1270 s->_vtable_offset == 0 &&
1271 # endif
1272 _IO_fwide (s, sizeof (CHAR_T) == 1 ? -1 : 1)
1273 != (sizeof (CHAR_T) == 1 ? -1 : 1))
1274 /* The stream is already oriented otherwise. */
1275 return EOF;
1276 #endif
1278 if (UNBUFFERED_P (s))
1279 /* Use a helper function which will allocate a local temporary buffer
1280 for the stream and then call us again. */
1281 return buffered_vfprintf (s, format, ap);
1283 /* Initialize local variables. */
1284 done = 0;
1285 grouping = (const char *) -1;
1286 #ifdef __va_copy
1287 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1288 since on some systems `va_list' is not an integral type. */
1289 __va_copy (ap_save, ap);
1290 #else
1291 ap_save = ap;
1292 #endif
1293 nspecs_done = 0;
1295 #ifdef COMPILE_WPRINTF
1296 /* Find the first format specifier. */
1297 f = lead_str_end = __find_specwc ((const UCHAR_T *) format);
1298 #else
1299 /* Put state for processing format string in initial state. */
1300 memset (&mbstate, '\0', sizeof (mbstate_t));
1302 /* Find the first format specifier. */
1303 f = lead_str_end = __find_specmb (format, &mbstate);
1304 #endif
1306 /* Lock stream. */
1307 #ifdef USE_IN_LIBIO
1308 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
1309 _IO_flockfile (s);
1310 #else
1311 __libc_cleanup_region_start (1, (void (*) (void *)) &__funlockfile, s);
1312 __flockfile (s);
1313 #endif
1315 /* Write the literal text before the first format. */
1316 outstring ((const UCHAR_T *) format,
1317 lead_str_end - (const UCHAR_T *) format);
1319 /* If we only have to print a simple string, return now. */
1320 if (*f == L_('\0'))
1321 goto all_done;
1323 /* Process whole format string. */
1326 #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
1327 # define REF(Name) &&do_##Name - &&do_form_unknown
1328 #else
1329 # define REF(Name) &&do_##Name
1330 #endif
1331 #define LABEL(Name) do_##Name
1332 STEP0_3_TABLE;
1333 STEP4_TABLE;
1335 union printf_arg *args_value; /* This is not used here but ... */
1336 int is_negative; /* Flag for negative number. */
1337 union
1339 unsigned long long int longlong;
1340 unsigned long int word;
1341 } number;
1342 int base;
1343 union printf_arg the_arg;
1344 CHAR_T *string; /* Pointer to argument string. */
1345 int alt = 0; /* Alternate format. */
1346 int space = 0; /* Use space prefix if no sign is needed. */
1347 int left = 0; /* Left-justify output. */
1348 int showsign = 0; /* Always begin with plus or minus sign. */
1349 int group = 0; /* Print numbers according grouping rules. */
1350 int is_long_double = 0; /* Argument is long double/ long long int. */
1351 int is_short = 0; /* Argument is short int. */
1352 int is_long = 0; /* Argument is long int. */
1353 int is_char = 0; /* Argument is promoted (unsigned) char. */
1354 int width = 0; /* Width of output; 0 means none specified. */
1355 int prec = -1; /* Precision of output; -1 means none specified. */
1356 /* This flag is set by the 'I' modifier and selects the use of the
1357 `outdigits' as determined by the current locale. */
1358 int use_outdigits = 0;
1359 UCHAR_T pad = L_(' ');/* Padding character. */
1360 CHAR_T spec;
1362 workstart = NULL;
1363 workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1365 /* Get current character in format string. */
1366 JUMP (*++f, step0_jumps);
1368 /* ' ' flag. */
1369 LABEL (flag_space):
1370 space = 1;
1371 JUMP (*++f, step0_jumps);
1373 /* '+' flag. */
1374 LABEL (flag_plus):
1375 showsign = 1;
1376 JUMP (*++f, step0_jumps);
1378 /* The '-' flag. */
1379 LABEL (flag_minus):
1380 left = 1;
1381 pad = L_(' ');
1382 JUMP (*++f, step0_jumps);
1384 /* The '#' flag. */
1385 LABEL (flag_hash):
1386 alt = 1;
1387 JUMP (*++f, step0_jumps);
1389 /* The '0' flag. */
1390 LABEL (flag_zero):
1391 if (!left)
1392 pad = L_('0');
1393 JUMP (*++f, step0_jumps);
1395 /* The '\'' flag. */
1396 LABEL (flag_quote):
1397 group = 1;
1399 if (grouping == (const char *) -1)
1401 #ifdef COMPILE_WPRINTF
1402 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1403 _NL_NUMERIC_THOUSANDS_SEP_WC);
1404 #else
1405 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1406 #endif
1408 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1409 if (*grouping == '\0' || *grouping == CHAR_MAX
1410 #ifdef COMPILE_WPRINTF
1411 || thousands_sep == L'\0'
1412 #else
1413 || *thousands_sep == '\0'
1414 #endif
1416 grouping = NULL;
1418 JUMP (*++f, step0_jumps);
1420 LABEL (flag_i18n):
1421 use_outdigits = 1;
1422 JUMP (*++f, step0_jumps);
1424 /* Get width from argument. */
1425 LABEL (width_asterics):
1427 const UCHAR_T *tmp; /* Temporary value. */
1429 tmp = ++f;
1430 if (ISDIGIT (*tmp) && read_int (&tmp) && *tmp == L_('$'))
1431 /* The width comes from a positional parameter. */
1432 goto do_positional;
1434 width = va_arg (ap, int);
1436 /* Negative width means left justified. */
1437 if (width < 0)
1439 width = -width;
1440 pad = L_(' ');
1441 left = 1;
1444 if (width + 32 >= (int) (sizeof (work_buffer)
1445 / sizeof (work_buffer[0])))
1447 /* We have to use a special buffer. The "32" is just a safe
1448 bet for all the output which is not counted in the width. */
1449 if (__libc_use_alloca ((width + 32) * sizeof (CHAR_T)))
1450 workend = ((CHAR_T *) alloca ((width + 32) * sizeof (CHAR_T))
1451 + (width + 32));
1452 else
1454 workstart = (CHAR_T *) malloc ((width + 32) * sizeof (CHAR_T));
1455 if (workstart == NULL)
1457 done = -1;
1458 goto all_done;
1460 workend = workstart + (width + 32);
1464 JUMP (*f, step1_jumps);
1466 /* Given width in format string. */
1467 LABEL (width):
1468 width = read_int (&f);
1470 if (width + 32 >= (int) (sizeof (work_buffer) / sizeof (work_buffer[0])))
1472 /* We have to use a special buffer. The "32" is just a safe
1473 bet for all the output which is not counted in the width. */
1474 if (__libc_use_alloca ((width + 32) * sizeof (CHAR_T)))
1475 workend = ((CHAR_T *) alloca ((width + 32) * sizeof (CHAR_T))
1476 + (width + 32));
1477 else
1479 workstart = (CHAR_T *) malloc ((width + 32) * sizeof (CHAR_T));
1480 if (workstart == NULL)
1482 done = -1;
1483 goto all_done;
1485 workend = workstart + (width + 32);
1488 if (*f == L_('$'))
1489 /* Oh, oh. The argument comes from a positional parameter. */
1490 goto do_positional;
1491 JUMP (*f, step1_jumps);
1493 LABEL (precision):
1494 ++f;
1495 if (*f == L_('*'))
1497 const UCHAR_T *tmp; /* Temporary value. */
1499 tmp = ++f;
1500 if (ISDIGIT (*tmp) && read_int (&tmp) > 0 && *tmp == L_('$'))
1501 /* The precision comes from a positional parameter. */
1502 goto do_positional;
1504 prec = va_arg (ap, int);
1506 /* If the precision is negative the precision is omitted. */
1507 if (prec < 0)
1508 prec = -1;
1510 else if (ISDIGIT (*f))
1511 prec = read_int (&f);
1512 else
1513 prec = 0;
1514 if (prec > width
1515 && prec + 32 > (int)(sizeof (work_buffer) / sizeof (work_buffer[0])))
1517 if (__libc_use_alloca ((prec + 32) * sizeof (CHAR_T)))
1518 workend = ((CHAR_T *) alloca ((prec + 32) * sizeof (CHAR_T)))
1519 + (prec + 32);
1520 else
1522 workstart = (CHAR_T *) malloc ((prec + 32) * sizeof (CHAR_T));
1523 if (workstart == NULL)
1525 done = -1;
1526 goto all_done;
1528 workend = workstart + (prec + 32);
1531 JUMP (*f, step2_jumps);
1533 /* Process 'h' modifier. There might another 'h' following. */
1534 LABEL (mod_half):
1535 is_short = 1;
1536 JUMP (*++f, step3a_jumps);
1538 /* Process 'hh' modifier. */
1539 LABEL (mod_halfhalf):
1540 is_short = 0;
1541 is_char = 1;
1542 JUMP (*++f, step4_jumps);
1544 /* Process 'l' modifier. There might another 'l' following. */
1545 LABEL (mod_long):
1546 is_long = 1;
1547 JUMP (*++f, step3b_jumps);
1549 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1550 allowed to follow. */
1551 LABEL (mod_longlong):
1552 is_long_double = 1;
1553 is_long = 1;
1554 JUMP (*++f, step4_jumps);
1556 LABEL (mod_size_t):
1557 is_long_double = sizeof (size_t) > sizeof (unsigned long int);
1558 is_long = sizeof (size_t) > sizeof (unsigned int);
1559 JUMP (*++f, step4_jumps);
1561 LABEL (mod_ptrdiff_t):
1562 is_long_double = sizeof (ptrdiff_t) > sizeof (unsigned long int);
1563 is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
1564 JUMP (*++f, step4_jumps);
1566 LABEL (mod_intmax_t):
1567 is_long_double = sizeof (intmax_t) > sizeof (unsigned long int);
1568 is_long = sizeof (intmax_t) > sizeof (unsigned int);
1569 JUMP (*++f, step4_jumps);
1571 /* Process current format. */
1572 while (1)
1574 process_arg (((struct printf_spec *) NULL));
1575 process_string_arg (((struct printf_spec *) NULL));
1577 LABEL (form_unknown):
1578 if (spec == L_('\0'))
1580 /* The format string ended before the specifier is complete. */
1581 done = -1;
1582 goto all_done;
1585 /* If we are in the fast loop force entering the complicated
1586 one. */
1587 goto do_positional;
1590 /* The format is correctly handled. */
1591 ++nspecs_done;
1593 if (__builtin_expect (workstart != NULL, 0))
1594 free (workstart);
1595 workstart = NULL;
1597 /* Look for next format specifier. */
1598 #ifdef COMPILE_WPRINTF
1599 f = __find_specwc ((end_of_spec = ++f));
1600 #else
1601 f = __find_specmb ((end_of_spec = ++f), &mbstate);
1602 #endif
1604 /* Write the following constant string. */
1605 outstring (end_of_spec, f - end_of_spec);
1607 while (*f != L_('\0'));
1609 /* Unlock stream and return. */
1610 goto all_done;
1612 /* Here starts the more complex loop to handle positional parameters. */
1613 do_positional:
1615 /* Array with information about the needed arguments. This has to
1616 be dynamically extensible. */
1617 size_t nspecs = 0;
1618 size_t nspecs_max = 32; /* A more or less arbitrary start value. */
1619 struct printf_spec *specs
1620 = alloca (nspecs_max * sizeof (struct printf_spec));
1622 /* The number of arguments the format string requests. This will
1623 determine the size of the array needed to store the argument
1624 attributes. */
1625 size_t nargs = 0;
1626 int *args_type;
1627 union printf_arg *args_value = NULL;
1629 /* Positional parameters refer to arguments directly. This could
1630 also determine the maximum number of arguments. Track the
1631 maximum number. */
1632 size_t max_ref_arg = 0;
1634 /* Just a counter. */
1635 size_t cnt;
1638 if (grouping == (const char *) -1)
1640 #ifdef COMPILE_WPRINTF
1641 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1642 _NL_NUMERIC_THOUSANDS_SEP_WC);
1643 #else
1644 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1645 #endif
1647 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1648 if (*grouping == '\0' || *grouping == CHAR_MAX)
1649 grouping = NULL;
1652 for (f = lead_str_end; *f != L_('\0'); f = specs[nspecs++].next_fmt)
1654 if (nspecs >= nspecs_max)
1656 /* Extend the array of format specifiers. */
1657 struct printf_spec *old = specs;
1659 nspecs_max *= 2;
1660 specs = alloca (nspecs_max * sizeof (struct printf_spec));
1662 if (specs == &old[nspecs])
1663 /* Stack grows up, OLD was the last thing allocated;
1664 extend it. */
1665 nspecs_max += nspecs_max / 2;
1666 else
1668 /* Copy the old array's elements to the new space. */
1669 memcpy (specs, old, nspecs * sizeof (struct printf_spec));
1670 if (old == &specs[nspecs])
1671 /* Stack grows down, OLD was just below the new
1672 SPECS. We can use that space when the new space
1673 runs out. */
1674 nspecs_max += nspecs_max / 2;
1678 /* Parse the format specifier. */
1679 #ifdef COMPILE_WPRINTF
1680 nargs += __parse_one_specwc (f, nargs, &specs[nspecs], &max_ref_arg);
1681 #else
1682 nargs += __parse_one_specmb (f, nargs, &specs[nspecs], &max_ref_arg,
1683 &mbstate);
1684 #endif
1687 /* Determine the number of arguments the format string consumes. */
1688 nargs = MAX (nargs, max_ref_arg);
1690 /* Allocate memory for the argument descriptions. */
1691 args_type = alloca (nargs * sizeof (int));
1692 memset (args_type, 0, nargs * sizeof (int));
1693 args_value = alloca (nargs * sizeof (union printf_arg));
1695 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1696 still zero after this loop, format is invalid. For now we
1697 simply use 0 as the value. */
1699 /* Fill in the types of all the arguments. */
1700 for (cnt = 0; cnt < nspecs; ++cnt)
1702 /* If the width is determined by an argument this is an int. */
1703 if (specs[cnt].width_arg != -1)
1704 args_type[specs[cnt].width_arg] = PA_INT;
1706 /* If the precision is determined by an argument this is an int. */
1707 if (specs[cnt].prec_arg != -1)
1708 args_type[specs[cnt].prec_arg] = PA_INT;
1710 switch (specs[cnt].ndata_args)
1712 case 0: /* No arguments. */
1713 break;
1714 case 1: /* One argument; we already have the type. */
1715 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1716 break;
1717 default:
1718 /* We have more than one argument for this format spec.
1719 We must call the arginfo function again to determine
1720 all the types. */
1721 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1722 (&specs[cnt].info,
1723 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg]);
1724 break;
1728 /* Now we know all the types and the order. Fill in the argument
1729 values. */
1730 for (cnt = 0; cnt < nargs; ++cnt)
1731 switch (args_type[cnt])
1733 #define T(tag, mem, type) \
1734 case tag: \
1735 args_value[cnt].mem = va_arg (ap_save, type); \
1736 break
1738 T (PA_CHAR, pa_int, int); /* Promoted. */
1739 T (PA_WCHAR, pa_wchar, wint_t);
1740 T (PA_INT|PA_FLAG_SHORT, pa_int, int); /* Promoted. */
1741 T (PA_INT, pa_int, int);
1742 T (PA_INT|PA_FLAG_LONG, pa_long_int, long int);
1743 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1744 T (PA_FLOAT, pa_double, double); /* Promoted. */
1745 T (PA_DOUBLE, pa_double, double);
1746 T (PA_DOUBLE|PA_FLAG_LONG_DOUBLE, pa_long_double, long double);
1747 T (PA_STRING, pa_string, const char *);
1748 T (PA_WSTRING, pa_wstring, const wchar_t *);
1749 T (PA_POINTER, pa_pointer, void *);
1750 #undef T
1751 default:
1752 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1753 args_value[cnt].pa_pointer = va_arg (ap_save, void *);
1754 else
1755 args_value[cnt].pa_long_double = 0.0;
1756 break;
1759 /* Now walk through all format specifiers and process them. */
1760 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1762 #undef REF
1763 #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
1764 # define REF(Name) &&do2_##Name - &&do_form_unknown
1765 #else
1766 # define REF(Name) &&do2_##Name
1767 #endif
1768 #undef LABEL
1769 #define LABEL(Name) do2_##Name
1770 STEP4_TABLE;
1772 int is_negative;
1773 union
1775 unsigned long long int longlong;
1776 unsigned long int word;
1777 } number;
1778 int base;
1779 union printf_arg the_arg;
1780 CHAR_T *string; /* Pointer to argument string. */
1782 /* Fill variables from values in struct. */
1783 int alt = specs[nspecs_done].info.alt;
1784 int space = specs[nspecs_done].info.space;
1785 int left = specs[nspecs_done].info.left;
1786 int showsign = specs[nspecs_done].info.showsign;
1787 int group = specs[nspecs_done].info.group;
1788 int is_long_double = specs[nspecs_done].info.is_long_double;
1789 int is_short = specs[nspecs_done].info.is_short;
1790 int is_char = specs[nspecs_done].info.is_char;
1791 int is_long = specs[nspecs_done].info.is_long;
1792 int width = specs[nspecs_done].info.width;
1793 int prec = specs[nspecs_done].info.prec;
1794 int use_outdigits = specs[nspecs_done].info.i18n;
1795 char pad = specs[nspecs_done].info.pad;
1796 CHAR_T spec = specs[nspecs_done].info.spec;
1797 CHAR_T *workstart = NULL;
1799 /* Fill in last information. */
1800 if (specs[nspecs_done].width_arg != -1)
1802 /* Extract the field width from an argument. */
1803 specs[nspecs_done].info.width =
1804 args_value[specs[nspecs_done].width_arg].pa_int;
1806 if (specs[nspecs_done].info.width < 0)
1807 /* If the width value is negative left justification is
1808 selected and the value is taken as being positive. */
1810 specs[nspecs_done].info.width *= -1;
1811 left = specs[nspecs_done].info.left = 1;
1813 width = specs[nspecs_done].info.width;
1816 if (specs[nspecs_done].prec_arg != -1)
1818 /* Extract the precision from an argument. */
1819 specs[nspecs_done].info.prec =
1820 args_value[specs[nspecs_done].prec_arg].pa_int;
1822 if (specs[nspecs_done].info.prec < 0)
1823 /* If the precision is negative the precision is
1824 omitted. */
1825 specs[nspecs_done].info.prec = -1;
1827 prec = specs[nspecs_done].info.prec;
1830 /* Maybe the buffer is too small. */
1831 if (MAX (prec, width) + 32 > (int) (sizeof (work_buffer)
1832 / sizeof (CHAR_T)))
1834 if (__libc_use_alloca ((MAX (prec, width) + 32)
1835 * sizeof (CHAR_T)))
1836 workend = ((CHAR_T *) alloca ((MAX (prec, width) + 32)
1837 * sizeof (CHAR_T))
1838 + (MAX (prec, width) + 32));
1839 else
1841 workstart = (CHAR_T *) malloc ((MAX (prec, width) + 32)
1842 * sizeof (CHAR_T));
1843 workend = workstart + (MAX (prec, width) + 32);
1847 /* Process format specifiers. */
1848 while (1)
1850 JUMP (spec, step4_jumps);
1852 process_arg ((&specs[nspecs_done]));
1853 process_string_arg ((&specs[nspecs_done]));
1855 LABEL (form_unknown):
1857 extern printf_function **__printf_function_table;
1858 int function_done;
1859 printf_function *function;
1860 unsigned int i;
1861 const void **ptr;
1863 function =
1864 (__printf_function_table == NULL ? NULL :
1865 __printf_function_table[specs[nspecs_done].info.spec]);
1867 if (function == NULL)
1868 function = &printf_unknown;
1870 ptr = alloca (specs[nspecs_done].ndata_args
1871 * sizeof (const void *));
1873 /* Fill in an array of pointers to the argument values. */
1874 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
1875 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1877 /* Call the function. */
1878 function_done = (*function) (s, &specs[nspecs_done].info, ptr);
1880 /* If an error occurred we don't have information about #
1881 of chars. */
1882 if (function_done < 0)
1884 done = -1;
1885 goto all_done;
1888 done += function_done;
1890 break;
1893 if (__builtin_expect (workstart != NULL, 0))
1894 free (workstart);
1895 workstart = NULL;
1897 /* Write the following constant string. */
1898 outstring (specs[nspecs_done].end_of_fmt,
1899 specs[nspecs_done].next_fmt
1900 - specs[nspecs_done].end_of_fmt);
1904 all_done:
1905 if (__builtin_expect (workstart != NULL, 0))
1906 free (workstart);
1907 /* Unlock the stream. */
1908 #ifdef USE_IN_LIBIO
1909 _IO_funlockfile (s);
1910 _IO_cleanup_region_end (0);
1911 #else
1912 __funlockfile (s);
1913 __libc_cleanup_region_end (0);
1914 #endif
1916 return done;
1919 /* Handle an unknown format specifier. This prints out a canonicalized
1920 representation of the format spec itself. */
1921 static int
1922 printf_unknown (FILE *s, const struct printf_info *info,
1923 const void *const *args)
1926 int done = 0;
1927 CHAR_T work_buffer[MAX (info->width, info->spec) + 32];
1928 CHAR_T *const workend
1929 = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1930 register CHAR_T *w;
1932 outchar (L_('%'));
1934 if (info->alt)
1935 outchar (L_('#'));
1936 if (info->group)
1937 outchar (L_('\''));
1938 if (info->showsign)
1939 outchar (L_('+'));
1940 else if (info->space)
1941 outchar (L_(' '));
1942 if (info->left)
1943 outchar (L_('-'));
1944 if (info->pad == L_('0'))
1945 outchar (L_('0'));
1946 if (info->i18n)
1947 outchar (L_('I'));
1949 if (info->width != 0)
1951 w = _itoa_word (info->width, workend, 10, 0);
1952 while (w < workend)
1953 outchar (*w++);
1956 if (info->prec != -1)
1958 outchar (L_('.'));
1959 w = _itoa_word (info->prec, workend, 10, 0);
1960 while (w < workend)
1961 outchar (*w++);
1964 if (info->spec != L_('\0'))
1965 outchar (info->spec);
1967 all_done:
1968 return done;
1971 /* Group the digits according to the grouping rules of the current locale.
1972 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1973 static CHAR_T *
1974 internal_function
1975 group_number (CHAR_T *w, CHAR_T *rear_ptr, const char *grouping,
1976 #ifdef COMPILE_WPRINTF
1977 wchar_t thousands_sep
1978 #else
1979 const char *thousands_sep
1980 #endif
1983 int len;
1984 CHAR_T *src, *s;
1985 #ifndef COMPILE_WPRINTF
1986 int tlen = strlen (thousands_sep);
1987 #endif
1989 /* We treat all negative values like CHAR_MAX. */
1991 if (*grouping == CHAR_MAX || *grouping <= 0)
1992 /* No grouping should be done. */
1993 return w;
1995 len = *grouping;
1997 /* Copy existing string so that nothing gets overwritten. */
1998 src = (CHAR_T *) alloca ((rear_ptr - w) * sizeof (CHAR_T));
1999 s = (CHAR_T *) __mempcpy (src, w,
2000 (rear_ptr - w) * sizeof (CHAR_T));
2001 w = rear_ptr;
2003 /* Process all characters in the string. */
2004 while (s > src)
2006 *--w = *--s;
2008 if (--len == 0 && s > src)
2010 /* A new group begins. */
2011 #ifdef COMPILE_WPRINTF
2012 *--w = thousands_sep;
2013 #else
2014 int cnt = tlen;
2016 *--w = thousands_sep[--cnt];
2017 while (cnt > 0);
2018 #endif
2020 len = *grouping++;
2021 if (*grouping == '\0')
2022 /* The previous grouping repeats ad infinitum. */
2023 --grouping;
2024 else if (*grouping == CHAR_MAX
2025 #if CHAR_MIN < 0
2026 || *grouping < 0
2027 #endif
2030 /* No further grouping to be done.
2031 Copy the rest of the number. */
2033 *--w = *--s;
2034 while (s > src);
2035 break;
2039 return w;
2042 #ifdef USE_IN_LIBIO
2043 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2044 struct helper_file
2046 struct _IO_FILE_plus _f;
2047 #ifdef COMPILE_WPRINTF
2048 struct _IO_wide_data _wide_data;
2049 #endif
2050 _IO_FILE *_put_stream;
2051 #ifdef _IO_MTSAFE_IO
2052 _IO_lock_t lock;
2053 #endif
2056 static int
2057 _IO_helper_overflow (_IO_FILE *s, int c)
2059 _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
2060 #ifdef COMPILE_WPRINTF
2061 int used = s->_wide_data->_IO_write_ptr - s->_wide_data->_IO_write_base;
2062 if (used)
2064 _IO_size_t written = _IO_sputn (target, s->_wide_data->_IO_write_base,
2065 used);
2066 s->_wide_data->_IO_write_ptr -= written;
2068 #else
2069 int used = s->_IO_write_ptr - s->_IO_write_base;
2070 if (used)
2072 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
2073 s->_IO_write_ptr -= written;
2075 #endif
2076 return PUTC (c, s);
2079 #ifdef COMPILE_WPRINTF
2080 static const struct _IO_jump_t _IO_helper_jumps =
2082 JUMP_INIT_DUMMY,
2083 JUMP_INIT (finish, INTUSE(_IO_wdefault_finish)),
2084 JUMP_INIT (overflow, _IO_helper_overflow),
2085 JUMP_INIT (underflow, _IO_default_underflow),
2086 JUMP_INIT (uflow, INTUSE(_IO_default_uflow)),
2087 JUMP_INIT (pbackfail, (_IO_pbackfail_t) INTUSE(_IO_wdefault_pbackfail)),
2088 JUMP_INIT (xsputn, INTUSE(_IO_wdefault_xsputn)),
2089 JUMP_INIT (xsgetn, INTUSE(_IO_wdefault_xsgetn)),
2090 JUMP_INIT (seekoff, _IO_default_seekoff),
2091 JUMP_INIT (seekpos, _IO_default_seekpos),
2092 JUMP_INIT (setbuf, _IO_default_setbuf),
2093 JUMP_INIT (sync, _IO_default_sync),
2094 JUMP_INIT (doallocate, INTUSE(_IO_wdefault_doallocate)),
2095 JUMP_INIT (read, _IO_default_read),
2096 JUMP_INIT (write, _IO_default_write),
2097 JUMP_INIT (seek, _IO_default_seek),
2098 JUMP_INIT (close, _IO_default_close),
2099 JUMP_INIT (stat, _IO_default_stat)
2101 #else
2102 static const struct _IO_jump_t _IO_helper_jumps =
2104 JUMP_INIT_DUMMY,
2105 JUMP_INIT (finish, INTUSE(_IO_default_finish)),
2106 JUMP_INIT (overflow, _IO_helper_overflow),
2107 JUMP_INIT (underflow, _IO_default_underflow),
2108 JUMP_INIT (uflow, INTUSE(_IO_default_uflow)),
2109 JUMP_INIT (pbackfail, INTUSE(_IO_default_pbackfail)),
2110 JUMP_INIT (xsputn, INTUSE(_IO_default_xsputn)),
2111 JUMP_INIT (xsgetn, INTUSE(_IO_default_xsgetn)),
2112 JUMP_INIT (seekoff, _IO_default_seekoff),
2113 JUMP_INIT (seekpos, _IO_default_seekpos),
2114 JUMP_INIT (setbuf, _IO_default_setbuf),
2115 JUMP_INIT (sync, _IO_default_sync),
2116 JUMP_INIT (doallocate, INTUSE(_IO_default_doallocate)),
2117 JUMP_INIT (read, _IO_default_read),
2118 JUMP_INIT (write, _IO_default_write),
2119 JUMP_INIT (seek, _IO_default_seek),
2120 JUMP_INIT (close, _IO_default_close),
2121 JUMP_INIT (stat, _IO_default_stat)
2123 #endif
2125 static int
2126 internal_function
2127 buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format,
2128 _IO_va_list args)
2130 CHAR_T buf[_IO_BUFSIZ];
2131 struct helper_file helper;
2132 register _IO_FILE *hp = (_IO_FILE *) &helper._f;
2133 int result, to_flush;
2135 /* Orient the stream. */
2136 #ifdef ORIENT
2137 ORIENT;
2138 #endif
2140 /* Initialize helper. */
2141 helper._put_stream = s;
2142 #ifdef COMPILE_WPRINTF
2143 hp->_wide_data = &helper._wide_data;
2144 _IO_wsetp (hp, buf, buf + sizeof buf / sizeof (CHAR_T));
2145 hp->_mode = 1;
2146 #else
2147 _IO_setp (hp, buf, buf + sizeof buf);
2148 hp->_mode = -1;
2149 #endif
2150 hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS|_IO_USER_LOCK;
2151 #if _IO_JUMPS_OFFSET
2152 hp->_vtable_offset = 0;
2153 #endif
2154 #ifdef _IO_MTSAFE_IO
2155 hp->_lock = NULL;
2156 #endif
2157 _IO_JUMPS (&helper._f) = (struct _IO_jump_t *) &_IO_helper_jumps;
2159 /* Now print to helper instead. */
2160 #if defined USE_IN_LIBIO && !defined COMPILE_WPRINTF
2161 result = INTUSE(_IO_vfprintf) (hp, format, args);
2162 #else
2163 result = vfprintf (hp, format, args);
2164 #endif
2166 /* Lock stream. */
2167 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, s);
2168 _IO_flockfile (s);
2170 /* Now flush anything from the helper to the S. */
2171 #ifdef COMPILE_WPRINTF
2172 if ((to_flush = (hp->_wide_data->_IO_write_ptr
2173 - hp->_wide_data->_IO_write_base)) > 0)
2175 if ((int) _IO_sputn (s, hp->_wide_data->_IO_write_base, to_flush)
2176 != to_flush)
2177 result = -1;
2179 #else
2180 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
2182 if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
2183 result = -1;
2185 #endif
2187 /* Unlock the stream. */
2188 _IO_funlockfile (s);
2189 __libc_cleanup_region_end (0);
2191 return result;
2194 #else /* !USE_IN_LIBIO */
2196 static int
2197 internal_function
2198 buffered_vfprintf (register FILE *s, const CHAR_T *format, va_list args)
2200 char buf[BUFSIZ];
2201 int result;
2203 /* Orient the stream. */
2204 #ifdef ORIENT
2205 ORIENT;
2206 #endif
2208 s->__bufp = s->__buffer = buf;
2209 s->__bufsize = sizeof buf;
2210 s->__put_limit = s->__buffer + s->__bufsize;
2211 s->__get_limit = s->__buffer;
2213 /* Now use buffer to print. */
2214 result = vfprintf (s, format, args);
2216 if (fflush (s) == EOF)
2217 result = -1;
2218 s->__buffer = s->__bufp = s->__get_limit = s->__put_limit = NULL;
2219 s->__bufsize = 0;
2221 return result;
2224 /* Pads string with given number of a specified character.
2225 This code is taken from iopadn.c of the GNU I/O library. */
2226 #define PADSIZE 16
2227 static const CHAR_T blanks[PADSIZE] =
2228 { L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '),
2229 L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' ') };
2230 static const CHAR_T zeroes[PADSIZE] =
2231 { L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'),
2232 L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0') };
2234 ssize_t
2235 #ifndef COMPILE_WPRINTF
2236 __printf_pad (FILE *s, char pad, size_t count)
2237 #else
2238 __wprintf_pad (FILE *s, wchar_t pad, size_t count)
2239 #endif
2241 const CHAR_T *padptr;
2242 register size_t i;
2244 padptr = pad == L_(' ') ? blanks : zeroes;
2246 for (i = count; i >= PADSIZE; i -= PADSIZE)
2247 if (PUT (s, padptr, PADSIZE) != PADSIZE)
2248 return -1;
2249 if (i > 0)
2250 if (PUT (s, padptr, i) != i)
2251 return -1;
2253 return count;
2255 #undef PADSIZE
2256 #endif /* USE_IN_LIBIO */
2258 #ifdef USE_IN_LIBIO
2259 # undef vfprintf
2260 # ifdef strong_alias
2261 /* This is for glibc. */
2262 # ifdef COMPILE_WPRINTF
2263 strong_alias (_IO_vfwprintf, __vfwprintf);
2264 weak_alias (_IO_vfwprintf, vfwprintf);
2265 # else
2266 strong_alias (_IO_vfprintf, vfprintf);
2267 libc_hidden_def (vfprintf)
2268 INTDEF(_IO_vfprintf)
2269 # endif
2270 # else
2271 # if defined __ELF__ || defined __GNU_LIBRARY__
2272 # include <gnu-stabs.h>
2273 # ifdef weak_alias
2274 # ifdef COMPILE_WPRINTF
2275 weak_alias (_IO_vfwprintf, vfwprintf);
2276 # else
2277 weak_alias (_IO_vfprintf, vfprintf);
2278 # endif
2279 # endif
2280 # endif
2281 # endif
2282 #endif