Add a testcase for BZ #14716
[glibc.git] / stdio-common / vfprintf.c
blob17d3f42a9752557f1db4c84e179c521cac47944e
1 /* Copyright (C) 1991-2012 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, see
16 <http://www.gnu.org/licenses/>. */
18 #include <ctype.h>
19 #include <limits.h>
20 #include <printf.h>
21 #include <stdarg.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <wchar.h>
27 #include <bits/libc-lock.h>
28 #include <sys/param.h>
29 #include <_itoa.h>
30 #include <locale/localeinfo.h>
31 #include <stdio.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 #include <libioP.h>
42 #define FILE _IO_FILE
43 #undef va_list
44 #define va_list _IO_va_list
45 #undef BUFSIZ
46 #define BUFSIZ _IO_BUFSIZ
47 #define ARGCHECK(S, Format) \
48 do \
49 { \
50 /* Check file argument for consistence. */ \
51 CHECK_FILE (S, -1); \
52 if (S->_flags & _IO_NO_WRITES) \
53 { \
54 S->_flags |= _IO_ERR_SEEN; \
55 __set_errno (EBADF); \
56 return -1; \
57 } \
58 if (Format == NULL) \
59 { \
60 MAYBE_SET_EINVAL; \
61 return -1; \
62 } \
63 } while (0)
64 #define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
66 #define done_add(val) \
67 do { \
68 unsigned int _val = val; \
69 assert ((unsigned int) done < (unsigned int) INT_MAX); \
70 if (__builtin_expect (INT_MAX - done < _val, 0)) \
71 { \
72 done = -1; \
73 __set_errno (EOVERFLOW); \
74 goto all_done; \
75 } \
76 done += _val; \
77 } while (0)
79 #ifndef COMPILE_WPRINTF
80 # define vfprintf _IO_vfprintf_internal
81 # define CHAR_T char
82 # define UCHAR_T unsigned char
83 # define INT_T int
84 # define L_(Str) Str
85 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
86 # define STR_LEN(Str) strlen (Str)
88 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
89 # define PAD(Padchar) \
90 if (width > 0) \
91 done_add (_IO_padn (s, (Padchar), width))
92 # define PUTC(C, F) _IO_putc_unlocked (C, F)
93 # define ORIENT if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
94 return -1
95 #else
96 # define vfprintf _IO_vfwprintf
97 # define CHAR_T wchar_t
98 /* This is a hack!!! There should be a type uwchar_t. */
99 # define UCHAR_T unsigned int /* uwchar_t */
100 # define INT_T wint_t
101 # define L_(Str) L##Str
102 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
103 # define STR_LEN(Str) __wcslen (Str)
105 # include <_itowa.h>
107 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
108 # define PAD(Padchar) \
109 if (width > 0) \
110 done_add (_IO_wpadn (s, (Padchar), width))
111 # define PUTC(C, F) _IO_putwc_unlocked (C, F)
112 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
114 # undef _itoa
115 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
116 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
117 # undef EOF
118 # define EOF WEOF
119 #endif
121 #include "_i18n_number.h"
123 /* Include the shared code for parsing the format string. */
124 #include "printf-parse.h"
127 #define outchar(Ch) \
128 do \
130 register const INT_T outc = (Ch); \
131 if (PUTC (outc, s) == EOF || done == INT_MAX) \
133 done = -1; \
134 goto all_done; \
136 ++done; \
138 while (0)
140 #define outstring(String, Len) \
141 do \
143 assert ((size_t) done <= (size_t) INT_MAX); \
144 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
146 done = -1; \
147 goto all_done; \
149 if (__builtin_expect (INT_MAX - done < (Len), 0)) \
151 done = -1; \
152 __set_errno (EOVERFLOW); \
153 goto all_done; \
155 done += (Len); \
157 while (0)
159 /* For handling long_double and longlong we use the same flag. If
160 `long' and `long long' are effectively the same type define it to
161 zero. */
162 #if LONG_MAX == LONG_LONG_MAX
163 # define is_longlong 0
164 #else
165 # define is_longlong is_long_double
166 #endif
168 /* If `long' and `int' is effectively the same type we don't have to
169 handle `long separately. */
170 #if INT_MAX == LONG_MAX
171 # define is_long_num 0
172 #else
173 # define is_long_num is_long
174 #endif
177 /* Global variables. */
178 static const CHAR_T null[] = L_("(null)");
181 /* Helper function to provide temporary buffering for unbuffered streams. */
182 static int buffered_vfprintf (FILE *stream, const CHAR_T *fmt, va_list)
183 __THROW __attribute__ ((noinline)) internal_function;
185 /* Handle unknown format specifier. */
186 static int printf_unknown (FILE *, const struct printf_info *,
187 const void *const *) __THROW;
189 /* Group digits of number string. */
190 #ifdef COMPILE_WPRINTF
191 static CHAR_T *group_number (CHAR_T *, CHAR_T *, const char *, wchar_t)
192 __THROW internal_function;
193 #else
194 static CHAR_T *group_number (CHAR_T *, CHAR_T *, const char *, const char *)
195 __THROW internal_function;
196 #endif
199 /* The function itself. */
201 vfprintf (FILE *s, const CHAR_T *format, va_list ap)
203 /* The character used as thousands separator. */
204 #ifdef COMPILE_WPRINTF
205 wchar_t thousands_sep = L'\0';
206 #else
207 const char *thousands_sep = NULL;
208 #endif
210 /* The string describing the size of groups of digits. */
211 const char *grouping;
213 /* Place to accumulate the result. */
214 int done;
216 /* Current character in format string. */
217 const UCHAR_T *f;
219 /* End of leading constant string. */
220 const UCHAR_T *lead_str_end;
222 /* Points to next format specifier. */
223 const UCHAR_T *end_of_spec;
225 /* Buffer intermediate results. */
226 CHAR_T work_buffer[1000];
227 CHAR_T *workstart = NULL;
228 CHAR_T *workend;
230 /* We have to save the original argument pointer. */
231 va_list ap_save;
233 /* Count number of specifiers we already processed. */
234 int nspecs_done;
236 /* For the %m format we may need the current `errno' value. */
237 int save_errno = errno;
239 /* 1 if format is in read-only memory, -1 if it is in writable memory,
240 0 if unknown. */
241 int readonly_format = 0;
243 /* For the argument descriptions, which may be allocated on the heap. */
244 void *args_malloced = NULL;
246 /* This table maps a character into a number representing a
247 class. In each step there is a destination label for each
248 class. */
249 static const uint8_t jump_table[] =
251 /* ' ' */ 1, 0, 0, /* '#' */ 4,
252 0, /* '%' */ 14, 0, /* '\''*/ 6,
253 0, 0, /* '*' */ 7, /* '+' */ 2,
254 0, /* '-' */ 3, /* '.' */ 9, 0,
255 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
256 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
257 /* '8' */ 8, /* '9' */ 8, 0, 0,
258 0, 0, 0, 0,
259 0, /* 'A' */ 26, 0, /* 'C' */ 25,
260 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
261 0, /* 'I' */ 29, 0, 0,
262 /* 'L' */ 12, 0, 0, 0,
263 0, 0, 0, /* 'S' */ 21,
264 0, 0, 0, 0,
265 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
266 0, 0, 0, 0,
267 0, /* 'a' */ 26, 0, /* 'c' */ 20,
268 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
269 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
270 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
271 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
272 /* 't' */ 27, /* 'u' */ 16, 0, 0,
273 /* 'x' */ 18, 0, /* 'z' */ 13
276 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
277 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
278 #ifdef SHARED
279 /* 'int' is enough and it saves some space on 64 bit systems. */
280 # define JUMP_TABLE_TYPE const int
281 # define JUMP(ChExpr, table) \
282 do \
284 int offset; \
285 void *__unbounded ptr; \
286 spec = (ChExpr); \
287 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
288 : table[CHAR_CLASS (spec)]; \
289 ptr = &&do_form_unknown + offset; \
290 goto *ptr; \
292 while (0)
293 #else
294 # define JUMP_TABLE_TYPE const void *const
295 # define JUMP(ChExpr, table) \
296 do \
298 const void *__unbounded ptr; \
299 spec = (ChExpr); \
300 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
301 : table[CHAR_CLASS (spec)]; \
302 goto *ptr; \
304 while (0)
305 #endif
307 #define STEP0_3_TABLE \
308 /* Step 0: at the beginning. */ \
309 static JUMP_TABLE_TYPE step0_jumps[30] = \
311 REF (form_unknown), \
312 REF (flag_space), /* for ' ' */ \
313 REF (flag_plus), /* for '+' */ \
314 REF (flag_minus), /* for '-' */ \
315 REF (flag_hash), /* for '<hash>' */ \
316 REF (flag_zero), /* for '0' */ \
317 REF (flag_quote), /* for '\'' */ \
318 REF (width_asterics), /* for '*' */ \
319 REF (width), /* for '1'...'9' */ \
320 REF (precision), /* for '.' */ \
321 REF (mod_half), /* for 'h' */ \
322 REF (mod_long), /* for 'l' */ \
323 REF (mod_longlong), /* for 'L', 'q' */ \
324 REF (mod_size_t), /* for 'z', 'Z' */ \
325 REF (form_percent), /* for '%' */ \
326 REF (form_integer), /* for 'd', 'i' */ \
327 REF (form_unsigned), /* for 'u' */ \
328 REF (form_octal), /* for 'o' */ \
329 REF (form_hexa), /* for 'X', 'x' */ \
330 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
331 REF (form_character), /* for 'c' */ \
332 REF (form_string), /* for 's', 'S' */ \
333 REF (form_pointer), /* for 'p' */ \
334 REF (form_number), /* for 'n' */ \
335 REF (form_strerror), /* for 'm' */ \
336 REF (form_wcharacter), /* for 'C' */ \
337 REF (form_floathex), /* for 'A', 'a' */ \
338 REF (mod_ptrdiff_t), /* for 't' */ \
339 REF (mod_intmax_t), /* for 'j' */ \
340 REF (flag_i18n), /* for 'I' */ \
341 }; \
342 /* Step 1: after processing width. */ \
343 static JUMP_TABLE_TYPE step1_jumps[30] = \
345 REF (form_unknown), \
346 REF (form_unknown), /* for ' ' */ \
347 REF (form_unknown), /* for '+' */ \
348 REF (form_unknown), /* for '-' */ \
349 REF (form_unknown), /* for '<hash>' */ \
350 REF (form_unknown), /* for '0' */ \
351 REF (form_unknown), /* for '\'' */ \
352 REF (form_unknown), /* for '*' */ \
353 REF (form_unknown), /* for '1'...'9' */ \
354 REF (precision), /* for '.' */ \
355 REF (mod_half), /* for 'h' */ \
356 REF (mod_long), /* for 'l' */ \
357 REF (mod_longlong), /* for 'L', 'q' */ \
358 REF (mod_size_t), /* for 'z', 'Z' */ \
359 REF (form_percent), /* for '%' */ \
360 REF (form_integer), /* for 'd', 'i' */ \
361 REF (form_unsigned), /* for 'u' */ \
362 REF (form_octal), /* for 'o' */ \
363 REF (form_hexa), /* for 'X', 'x' */ \
364 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
365 REF (form_character), /* for 'c' */ \
366 REF (form_string), /* for 's', 'S' */ \
367 REF (form_pointer), /* for 'p' */ \
368 REF (form_number), /* for 'n' */ \
369 REF (form_strerror), /* for 'm' */ \
370 REF (form_wcharacter), /* for 'C' */ \
371 REF (form_floathex), /* for 'A', 'a' */ \
372 REF (mod_ptrdiff_t), /* for 't' */ \
373 REF (mod_intmax_t), /* for 'j' */ \
374 REF (form_unknown) /* for 'I' */ \
375 }; \
376 /* Step 2: after processing precision. */ \
377 static JUMP_TABLE_TYPE step2_jumps[30] = \
379 REF (form_unknown), \
380 REF (form_unknown), /* for ' ' */ \
381 REF (form_unknown), /* for '+' */ \
382 REF (form_unknown), /* for '-' */ \
383 REF (form_unknown), /* for '<hash>' */ \
384 REF (form_unknown), /* for '0' */ \
385 REF (form_unknown), /* for '\'' */ \
386 REF (form_unknown), /* for '*' */ \
387 REF (form_unknown), /* for '1'...'9' */ \
388 REF (form_unknown), /* for '.' */ \
389 REF (mod_half), /* for 'h' */ \
390 REF (mod_long), /* for 'l' */ \
391 REF (mod_longlong), /* for 'L', 'q' */ \
392 REF (mod_size_t), /* for 'z', 'Z' */ \
393 REF (form_percent), /* for '%' */ \
394 REF (form_integer), /* for 'd', 'i' */ \
395 REF (form_unsigned), /* for 'u' */ \
396 REF (form_octal), /* for 'o' */ \
397 REF (form_hexa), /* for 'X', 'x' */ \
398 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
399 REF (form_character), /* for 'c' */ \
400 REF (form_string), /* for 's', 'S' */ \
401 REF (form_pointer), /* for 'p' */ \
402 REF (form_number), /* for 'n' */ \
403 REF (form_strerror), /* for 'm' */ \
404 REF (form_wcharacter), /* for 'C' */ \
405 REF (form_floathex), /* for 'A', 'a' */ \
406 REF (mod_ptrdiff_t), /* for 't' */ \
407 REF (mod_intmax_t), /* for 'j' */ \
408 REF (form_unknown) /* for 'I' */ \
409 }; \
410 /* Step 3a: after processing first 'h' modifier. */ \
411 static JUMP_TABLE_TYPE step3a_jumps[30] = \
413 REF (form_unknown), \
414 REF (form_unknown), /* for ' ' */ \
415 REF (form_unknown), /* for '+' */ \
416 REF (form_unknown), /* for '-' */ \
417 REF (form_unknown), /* for '<hash>' */ \
418 REF (form_unknown), /* for '0' */ \
419 REF (form_unknown), /* for '\'' */ \
420 REF (form_unknown), /* for '*' */ \
421 REF (form_unknown), /* for '1'...'9' */ \
422 REF (form_unknown), /* for '.' */ \
423 REF (mod_halfhalf), /* for 'h' */ \
424 REF (form_unknown), /* for 'l' */ \
425 REF (form_unknown), /* for 'L', 'q' */ \
426 REF (form_unknown), /* for 'z', 'Z' */ \
427 REF (form_percent), /* for '%' */ \
428 REF (form_integer), /* for 'd', 'i' */ \
429 REF (form_unsigned), /* for 'u' */ \
430 REF (form_octal), /* for 'o' */ \
431 REF (form_hexa), /* for 'X', 'x' */ \
432 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
433 REF (form_unknown), /* for 'c' */ \
434 REF (form_unknown), /* for 's', 'S' */ \
435 REF (form_unknown), /* for 'p' */ \
436 REF (form_number), /* for 'n' */ \
437 REF (form_unknown), /* for 'm' */ \
438 REF (form_unknown), /* for 'C' */ \
439 REF (form_unknown), /* for 'A', 'a' */ \
440 REF (form_unknown), /* for 't' */ \
441 REF (form_unknown), /* for 'j' */ \
442 REF (form_unknown) /* for 'I' */ \
443 }; \
444 /* Step 3b: after processing first 'l' modifier. */ \
445 static JUMP_TABLE_TYPE step3b_jumps[30] = \
447 REF (form_unknown), \
448 REF (form_unknown), /* for ' ' */ \
449 REF (form_unknown), /* for '+' */ \
450 REF (form_unknown), /* for '-' */ \
451 REF (form_unknown), /* for '<hash>' */ \
452 REF (form_unknown), /* for '0' */ \
453 REF (form_unknown), /* for '\'' */ \
454 REF (form_unknown), /* for '*' */ \
455 REF (form_unknown), /* for '1'...'9' */ \
456 REF (form_unknown), /* for '.' */ \
457 REF (form_unknown), /* for 'h' */ \
458 REF (mod_longlong), /* for 'l' */ \
459 REF (form_unknown), /* for 'L', 'q' */ \
460 REF (form_unknown), /* for 'z', 'Z' */ \
461 REF (form_percent), /* for '%' */ \
462 REF (form_integer), /* for 'd', 'i' */ \
463 REF (form_unsigned), /* for 'u' */ \
464 REF (form_octal), /* for 'o' */ \
465 REF (form_hexa), /* for 'X', 'x' */ \
466 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
467 REF (form_character), /* for 'c' */ \
468 REF (form_string), /* for 's', 'S' */ \
469 REF (form_pointer), /* for 'p' */ \
470 REF (form_number), /* for 'n' */ \
471 REF (form_strerror), /* for 'm' */ \
472 REF (form_wcharacter), /* for 'C' */ \
473 REF (form_floathex), /* for 'A', 'a' */ \
474 REF (form_unknown), /* for 't' */ \
475 REF (form_unknown), /* for 'j' */ \
476 REF (form_unknown) /* for 'I' */ \
479 #define STEP4_TABLE \
480 /* Step 4: processing format specifier. */ \
481 static JUMP_TABLE_TYPE step4_jumps[30] = \
483 REF (form_unknown), \
484 REF (form_unknown), /* for ' ' */ \
485 REF (form_unknown), /* for '+' */ \
486 REF (form_unknown), /* for '-' */ \
487 REF (form_unknown), /* for '<hash>' */ \
488 REF (form_unknown), /* for '0' */ \
489 REF (form_unknown), /* for '\'' */ \
490 REF (form_unknown), /* for '*' */ \
491 REF (form_unknown), /* for '1'...'9' */ \
492 REF (form_unknown), /* for '.' */ \
493 REF (form_unknown), /* for 'h' */ \
494 REF (form_unknown), /* for 'l' */ \
495 REF (form_unknown), /* for 'L', 'q' */ \
496 REF (form_unknown), /* for 'z', 'Z' */ \
497 REF (form_percent), /* for '%' */ \
498 REF (form_integer), /* for 'd', 'i' */ \
499 REF (form_unsigned), /* for 'u' */ \
500 REF (form_octal), /* for 'o' */ \
501 REF (form_hexa), /* for 'X', 'x' */ \
502 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
503 REF (form_character), /* for 'c' */ \
504 REF (form_string), /* for 's', 'S' */ \
505 REF (form_pointer), /* for 'p' */ \
506 REF (form_number), /* for 'n' */ \
507 REF (form_strerror), /* for 'm' */ \
508 REF (form_wcharacter), /* for 'C' */ \
509 REF (form_floathex), /* for 'A', 'a' */ \
510 REF (form_unknown), /* for 't' */ \
511 REF (form_unknown), /* for 'j' */ \
512 REF (form_unknown) /* for 'I' */ \
516 #define process_arg(fspec) \
517 /* Start real work. We know about all flags and modifiers and \
518 now process the wanted format specifier. */ \
519 LABEL (form_percent): \
520 /* Write a literal "%". */ \
521 outchar (L_('%')); \
522 break; \
524 LABEL (form_integer): \
525 /* Signed decimal integer. */ \
526 base = 10; \
528 if (is_longlong) \
530 long long int signed_number; \
532 if (fspec == NULL) \
533 signed_number = va_arg (ap, long long int); \
534 else \
535 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
537 is_negative = signed_number < 0; \
538 number.longlong = is_negative ? (- signed_number) : signed_number; \
540 goto LABEL (longlong_number); \
542 else \
544 long int signed_number; \
546 if (fspec == NULL) \
548 if (is_long_num) \
549 signed_number = va_arg (ap, long int); \
550 else if (is_char) \
551 signed_number = (signed char) va_arg (ap, unsigned int); \
552 else if (!is_short) \
553 signed_number = va_arg (ap, int); \
554 else \
555 signed_number = (short int) va_arg (ap, unsigned int); \
557 else \
558 if (is_long_num) \
559 signed_number = args_value[fspec->data_arg].pa_long_int; \
560 else if (is_char) \
561 signed_number = (signed char) \
562 args_value[fspec->data_arg].pa_u_int; \
563 else if (!is_short) \
564 signed_number = args_value[fspec->data_arg].pa_int; \
565 else \
566 signed_number = (short int) \
567 args_value[fspec->data_arg].pa_u_int; \
569 is_negative = signed_number < 0; \
570 number.word = is_negative ? (- signed_number) : signed_number; \
572 goto LABEL (number); \
574 /* NOTREACHED */ \
576 LABEL (form_unsigned): \
577 /* Unsigned decimal integer. */ \
578 base = 10; \
579 goto LABEL (unsigned_number); \
580 /* NOTREACHED */ \
582 LABEL (form_octal): \
583 /* Unsigned octal integer. */ \
584 base = 8; \
585 goto LABEL (unsigned_number); \
586 /* NOTREACHED */ \
588 LABEL (form_hexa): \
589 /* Unsigned hexadecimal integer. */ \
590 base = 16; \
592 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
594 /* ISO specifies the `+' and ` ' flags only for signed \
595 conversions. */ \
596 is_negative = 0; \
597 showsign = 0; \
598 space = 0; \
600 if (is_longlong) \
602 if (fspec == NULL) \
603 number.longlong = va_arg (ap, unsigned long long int); \
604 else \
605 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
607 LABEL (longlong_number): \
608 if (prec < 0) \
609 /* Supply a default precision if none was given. */ \
610 prec = 1; \
611 else \
612 /* We have to take care for the '0' flag. If a precision \
613 is given it must be ignored. */ \
614 pad = L_(' '); \
616 /* If the precision is 0 and the number is 0 nothing has to \
617 be written for the number, except for the 'o' format in \
618 alternate form. */ \
619 if (prec == 0 && number.longlong == 0) \
621 string = workend; \
622 if (base == 8 && alt) \
623 *--string = L_('0'); \
625 else \
627 /* Put the number in WORK. */ \
628 string = _itoa (number.longlong, workend, base, \
629 spec == L_('X')); \
630 if (group && grouping) \
631 string = group_number (string, workend, grouping, \
632 thousands_sep); \
634 if (use_outdigits && base == 10) \
635 string = _i18n_number_rewrite (string, workend, workend); \
637 /* Simplify further test for num != 0. */ \
638 number.word = number.longlong != 0; \
640 else \
642 if (fspec == NULL) \
644 if (is_long_num) \
645 number.word = va_arg (ap, unsigned long int); \
646 else if (is_char) \
647 number.word = (unsigned char) va_arg (ap, unsigned int); \
648 else if (!is_short) \
649 number.word = va_arg (ap, unsigned int); \
650 else \
651 number.word = (unsigned short int) va_arg (ap, unsigned int); \
653 else \
654 if (is_long_num) \
655 number.word = args_value[fspec->data_arg].pa_u_long_int; \
656 else if (is_char) \
657 number.word = (unsigned char) \
658 args_value[fspec->data_arg].pa_u_int; \
659 else if (!is_short) \
660 number.word = args_value[fspec->data_arg].pa_u_int; \
661 else \
662 number.word = (unsigned short int) \
663 args_value[fspec->data_arg].pa_u_int; \
665 LABEL (number): \
666 if (prec < 0) \
667 /* Supply a default precision if none was given. */ \
668 prec = 1; \
669 else \
670 /* We have to take care for the '0' flag. If a precision \
671 is given it must be ignored. */ \
672 pad = L_(' '); \
674 /* If the precision is 0 and the number is 0 nothing has to \
675 be written for the number, except for the 'o' format in \
676 alternate form. */ \
677 if (prec == 0 && number.word == 0) \
679 string = workend; \
680 if (base == 8 && alt) \
681 *--string = L_('0'); \
683 else \
685 /* Put the number in WORK. */ \
686 string = _itoa_word (number.word, workend, base, \
687 spec == L_('X')); \
688 if (group && grouping) \
689 string = group_number (string, workend, grouping, \
690 thousands_sep); \
692 if (use_outdigits && base == 10) \
693 string = _i18n_number_rewrite (string, workend, workend); \
697 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
698 /* Add octal marker. */ \
699 *--string = L_('0'); \
701 prec = MAX (0, prec - (workend - string)); \
703 if (!left) \
705 width -= workend - string + prec; \
707 if (number.word != 0 && alt && base == 16) \
708 /* Account for 0X hex marker. */ \
709 width -= 2; \
711 if (is_negative || showsign || space) \
712 --width; \
714 if (pad == L_(' ')) \
716 PAD (L_(' ')); \
717 width = 0; \
720 if (is_negative) \
721 outchar (L_('-')); \
722 else if (showsign) \
723 outchar (L_('+')); \
724 else if (space) \
725 outchar (L_(' ')); \
727 if (number.word != 0 && alt && base == 16) \
729 outchar (L_('0')); \
730 outchar (spec); \
733 width += prec; \
734 PAD (L_('0')); \
736 outstring (string, workend - string); \
738 break; \
740 else \
742 if (is_negative) \
744 outchar (L_('-')); \
745 --width; \
747 else if (showsign) \
749 outchar (L_('+')); \
750 --width; \
752 else if (space) \
754 outchar (L_(' ')); \
755 --width; \
758 if (number.word != 0 && alt && base == 16) \
760 outchar (L_('0')); \
761 outchar (spec); \
762 width -= 2; \
765 width -= workend - string + prec; \
767 if (prec > 0) \
769 int temp = width; \
770 width = prec; \
771 PAD (L_('0')); \
772 width = temp; \
775 outstring (string, workend - string); \
777 PAD (L_(' ')); \
778 break; \
781 LABEL (form_float): \
783 /* Floating-point number. This is handled by printf_fp.c. */ \
784 const void *ptr; \
785 int function_done; \
787 if (fspec == NULL) \
789 if (__ldbl_is_dbl) \
790 is_long_double = 0; \
792 struct printf_info info = { .prec = prec, \
793 .width = width, \
794 .spec = spec, \
795 .is_long_double = is_long_double, \
796 .is_short = is_short, \
797 .is_long = is_long, \
798 .alt = alt, \
799 .space = space, \
800 .left = left, \
801 .showsign = showsign, \
802 .group = group, \
803 .pad = pad, \
804 .extra = 0, \
805 .i18n = use_outdigits, \
806 .wide = sizeof (CHAR_T) != 1 }; \
808 if (is_long_double) \
809 the_arg.pa_long_double = va_arg (ap, long double); \
810 else \
811 the_arg.pa_double = va_arg (ap, double); \
812 ptr = (const void *) &the_arg; \
814 function_done = __printf_fp (s, &info, &ptr); \
816 else \
818 ptr = (const void *) &args_value[fspec->data_arg]; \
819 if (__ldbl_is_dbl) \
821 fspec->data_arg_type = PA_DOUBLE; \
822 fspec->info.is_long_double = 0; \
825 function_done = __printf_fp (s, &fspec->info, &ptr); \
828 if (function_done < 0) \
830 /* Error in print handler; up to handler to set errno. */ \
831 done = -1; \
832 goto all_done; \
835 done_add (function_done); \
837 break; \
839 LABEL (form_floathex): \
841 /* Floating point number printed as hexadecimal number. */ \
842 const void *ptr; \
843 int function_done; \
845 if (fspec == NULL) \
847 if (__ldbl_is_dbl) \
848 is_long_double = 0; \
850 struct printf_info info = { .prec = prec, \
851 .width = width, \
852 .spec = spec, \
853 .is_long_double = is_long_double, \
854 .is_short = is_short, \
855 .is_long = is_long, \
856 .alt = alt, \
857 .space = space, \
858 .left = left, \
859 .showsign = showsign, \
860 .group = group, \
861 .pad = pad, \
862 .extra = 0, \
863 .wide = sizeof (CHAR_T) != 1 }; \
865 if (is_long_double) \
866 the_arg.pa_long_double = va_arg (ap, long double); \
867 else \
868 the_arg.pa_double = va_arg (ap, double); \
869 ptr = (const void *) &the_arg; \
871 function_done = __printf_fphex (s, &info, &ptr); \
873 else \
875 ptr = (const void *) &args_value[fspec->data_arg]; \
876 if (__ldbl_is_dbl) \
877 fspec->info.is_long_double = 0; \
879 function_done = __printf_fphex (s, &fspec->info, &ptr); \
882 if (function_done < 0) \
884 /* Error in print handler; up to handler to set errno. */ \
885 done = -1; \
886 goto all_done; \
889 done_add (function_done); \
891 break; \
893 LABEL (form_pointer): \
894 /* Generic pointer. */ \
896 const void *ptr; \
897 if (fspec == NULL) \
898 ptr = va_arg (ap, void *); \
899 else \
900 ptr = args_value[fspec->data_arg].pa_pointer; \
901 if (ptr != NULL) \
903 /* If the pointer is not NULL, write it as a %#x spec. */ \
904 base = 16; \
905 number.word = (unsigned long int) ptr; \
906 is_negative = 0; \
907 alt = 1; \
908 group = 0; \
909 spec = L_('x'); \
910 goto LABEL (number); \
912 else \
914 /* Write "(nil)" for a nil pointer. */ \
915 string = (CHAR_T *) L_("(nil)"); \
916 /* Make sure the full string "(nil)" is printed. */ \
917 if (prec < 5) \
918 prec = 5; \
919 is_long = 0; /* This is no wide-char string. */ \
920 goto LABEL (print_string); \
923 /* NOTREACHED */ \
925 LABEL (form_number): \
926 if (s->_flags2 & _IO_FLAGS2_FORTIFY) \
928 if (! readonly_format) \
930 extern int __readonly_area (const void *, size_t) \
931 attribute_hidden; \
932 readonly_format \
933 = __readonly_area (format, ((STR_LEN (format) + 1) \
934 * sizeof (CHAR_T))); \
936 if (readonly_format < 0) \
937 __libc_fatal ("*** %n in writable segment detected ***\n"); \
939 /* Answer the count of characters written. */ \
940 if (fspec == NULL) \
942 if (is_longlong) \
943 *(long long int *) va_arg (ap, void *) = done; \
944 else if (is_long_num) \
945 *(long int *) va_arg (ap, void *) = done; \
946 else if (is_char) \
947 *(char *) va_arg (ap, void *) = done; \
948 else if (!is_short) \
949 *(int *) va_arg (ap, void *) = done; \
950 else \
951 *(short int *) va_arg (ap, void *) = done; \
953 else \
954 if (is_longlong) \
955 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
956 else if (is_long_num) \
957 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
958 else if (is_char) \
959 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
960 else if (!is_short) \
961 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
962 else \
963 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
964 break; \
966 LABEL (form_strerror): \
967 /* Print description of error ERRNO. */ \
968 string = \
969 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
970 sizeof work_buffer); \
971 is_long = 0; /* This is no wide-char string. */ \
972 goto LABEL (print_string)
974 #ifdef COMPILE_WPRINTF
975 # define process_string_arg(fspec) \
976 LABEL (form_character): \
977 /* Character. */ \
978 if (is_long) \
979 goto LABEL (form_wcharacter); \
980 --width; /* Account for the character itself. */ \
981 if (!left) \
982 PAD (L' '); \
983 if (fspec == NULL) \
984 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
985 else \
986 outchar (__btowc ((unsigned char) \
987 args_value[fspec->data_arg].pa_int)); \
988 if (left) \
989 PAD (L' '); \
990 break; \
992 LABEL (form_wcharacter): \
994 /* Wide character. */ \
995 --width; \
996 if (!left) \
997 PAD (L' '); \
998 if (fspec == NULL) \
999 outchar (va_arg (ap, wchar_t)); \
1000 else \
1001 outchar (args_value[fspec->data_arg].pa_wchar); \
1002 if (left) \
1003 PAD (L' '); \
1005 break; \
1007 LABEL (form_string): \
1009 size_t len; \
1010 int string_malloced; \
1012 /* The string argument could in fact be `char *' or `wchar_t *'. \
1013 But this should not make a difference here. */ \
1014 if (fspec == NULL) \
1015 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
1016 else \
1017 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
1019 /* Entry point for printing other strings. */ \
1020 LABEL (print_string): \
1022 string_malloced = 0; \
1023 if (string == NULL) \
1025 /* Write "(null)" if there's space. */ \
1026 if (prec == -1 \
1027 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
1029 string = (CHAR_T *) null; \
1030 len = (sizeof (null) / sizeof (null[0])) - 1; \
1032 else \
1034 string = (CHAR_T *) L""; \
1035 len = 0; \
1038 else if (!is_long && spec != L_('S')) \
1040 /* This is complicated. We have to transform the multibyte \
1041 string into a wide character string. */ \
1042 const char *mbs = (const char *) string; \
1043 mbstate_t mbstate; \
1045 len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
1047 /* Allocate dynamically an array which definitely is long \
1048 enough for the wide character version. Each byte in the \
1049 multi-byte string can produce at most one wide character. */ \
1050 if (__libc_use_alloca (len * sizeof (wchar_t))) \
1051 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1052 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1053 == NULL) \
1055 done = -1; \
1056 goto all_done; \
1058 else \
1059 string_malloced = 1; \
1061 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1062 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1063 if (len == (size_t) -1) \
1065 /* Illegal multibyte character. */ \
1066 done = -1; \
1067 goto all_done; \
1070 else \
1072 if (prec != -1) \
1073 /* Search for the end of the string, but don't search past \
1074 the length specified by the precision. */ \
1075 len = __wcsnlen (string, prec); \
1076 else \
1077 len = __wcslen (string); \
1080 if ((width -= len) < 0) \
1082 outstring (string, len); \
1083 break; \
1086 if (!left) \
1087 PAD (L' '); \
1088 outstring (string, len); \
1089 if (left) \
1090 PAD (L' '); \
1091 if (__builtin_expect (string_malloced, 0)) \
1092 free (string); \
1094 break;
1095 #else
1096 # define process_string_arg(fspec) \
1097 LABEL (form_character): \
1098 /* Character. */ \
1099 if (is_long) \
1100 goto LABEL (form_wcharacter); \
1101 --width; /* Account for the character itself. */ \
1102 if (!left) \
1103 PAD (' '); \
1104 if (fspec == NULL) \
1105 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1106 else \
1107 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1108 if (left) \
1109 PAD (' '); \
1110 break; \
1112 LABEL (form_wcharacter): \
1114 /* Wide character. */ \
1115 char buf[MB_CUR_MAX]; \
1116 mbstate_t mbstate; \
1117 size_t len; \
1119 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1120 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1121 : args_value[fspec->data_arg].pa_wchar), \
1122 &mbstate); \
1123 if (len == (size_t) -1) \
1125 /* Something went wrong during the conversion. Bail out. */ \
1126 done = -1; \
1127 goto all_done; \
1129 width -= len; \
1130 if (!left) \
1131 PAD (' '); \
1132 outstring (buf, len); \
1133 if (left) \
1134 PAD (' '); \
1136 break; \
1138 LABEL (form_string): \
1140 size_t len; \
1141 int string_malloced; \
1143 /* The string argument could in fact be `char *' or `wchar_t *'. \
1144 But this should not make a difference here. */ \
1145 if (fspec == NULL) \
1146 string = (char *) va_arg (ap, const char *); \
1147 else \
1148 string = (char *) args_value[fspec->data_arg].pa_string; \
1150 /* Entry point for printing other strings. */ \
1151 LABEL (print_string): \
1153 string_malloced = 0; \
1154 if (string == NULL) \
1156 /* Write "(null)" if there's space. */ \
1157 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1159 string = (char *) null; \
1160 len = sizeof (null) - 1; \
1162 else \
1164 string = (char *) ""; \
1165 len = 0; \
1168 else if (!is_long && spec != L_('S')) \
1170 if (prec != -1) \
1171 /* Search for the end of the string, but don't search past \
1172 the length (in bytes) specified by the precision. */ \
1173 len = __strnlen (string, prec); \
1174 else \
1175 len = strlen (string); \
1177 else \
1179 const wchar_t *s2 = (const wchar_t *) string; \
1180 mbstate_t mbstate; \
1182 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1184 if (prec >= 0) \
1186 /* The string `s2' might not be NUL terminated. */ \
1187 if (__libc_use_alloca (prec)) \
1188 string = (char *) alloca (prec); \
1189 else if ((string = (char *) malloc (prec)) == NULL) \
1191 done = -1; \
1192 goto all_done; \
1194 else \
1195 string_malloced = 1; \
1196 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1198 else \
1200 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1201 if (len != (size_t) -1) \
1203 assert (__mbsinit (&mbstate)); \
1204 s2 = (const wchar_t *) string; \
1205 if (__libc_use_alloca (len + 1)) \
1206 string = (char *) alloca (len + 1); \
1207 else if ((string = (char *) malloc (len + 1)) == NULL) \
1209 done = -1; \
1210 goto all_done; \
1212 else \
1213 string_malloced = 1; \
1214 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1218 if (len == (size_t) -1) \
1220 /* Illegal wide-character string. */ \
1221 done = -1; \
1222 goto all_done; \
1226 if ((width -= len) < 0) \
1228 outstring (string, len); \
1229 break; \
1232 if (!left) \
1233 PAD (' '); \
1234 outstring (string, len); \
1235 if (left) \
1236 PAD (' '); \
1237 if (__builtin_expect (string_malloced, 0)) \
1238 free (string); \
1240 break;
1241 #endif
1243 /* Orient the stream. */
1244 #ifdef ORIENT
1245 ORIENT;
1246 #endif
1248 /* Sanity check of arguments. */
1249 ARGCHECK (s, format);
1251 #ifdef ORIENT
1252 /* Check for correct orientation. */
1253 if (_IO_vtable_offset (s) == 0 &&
1254 _IO_fwide (s, sizeof (CHAR_T) == 1 ? -1 : 1)
1255 != (sizeof (CHAR_T) == 1 ? -1 : 1))
1256 /* The stream is already oriented otherwise. */
1257 return EOF;
1258 #endif
1260 if (UNBUFFERED_P (s))
1261 /* Use a helper function which will allocate a local temporary buffer
1262 for the stream and then call us again. */
1263 return buffered_vfprintf (s, format, ap);
1265 /* Initialize local variables. */
1266 done = 0;
1267 grouping = (const char *) -1;
1268 #ifdef __va_copy
1269 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1270 since on some systems `va_list' is not an integral type. */
1271 __va_copy (ap_save, ap);
1272 #else
1273 ap_save = ap;
1274 #endif
1275 nspecs_done = 0;
1277 #ifdef COMPILE_WPRINTF
1278 /* Find the first format specifier. */
1279 f = lead_str_end = __find_specwc ((const UCHAR_T *) format);
1280 #else
1281 /* Find the first format specifier. */
1282 f = lead_str_end = __find_specmb ((const UCHAR_T *) format);
1283 #endif
1285 /* Lock stream. */
1286 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
1287 _IO_flockfile (s);
1289 /* Write the literal text before the first format. */
1290 outstring ((const UCHAR_T *) format,
1291 lead_str_end - (const UCHAR_T *) format);
1293 /* If we only have to print a simple string, return now. */
1294 if (*f == L_('\0'))
1295 goto all_done;
1297 /* Use the slow path in case any printf handler is registered. */
1298 if (__builtin_expect (__printf_function_table != NULL
1299 || __printf_modifier_table != NULL
1300 || __printf_va_arg_table != NULL, 0))
1301 goto do_positional;
1303 /* Process whole format string. */
1306 #ifdef SHARED
1307 # define REF(Name) &&do_##Name - &&do_form_unknown
1308 #else
1309 # define REF(Name) &&do_##Name
1310 #endif
1311 #define LABEL(Name) do_##Name
1312 STEP0_3_TABLE;
1313 STEP4_TABLE;
1315 union printf_arg *args_value; /* This is not used here but ... */
1316 int is_negative; /* Flag for negative number. */
1317 union
1319 unsigned long long int longlong;
1320 unsigned long int word;
1321 } number;
1322 int base;
1323 union printf_arg the_arg;
1324 CHAR_T *string; /* Pointer to argument string. */
1325 int alt = 0; /* Alternate format. */
1326 int space = 0; /* Use space prefix if no sign is needed. */
1327 int left = 0; /* Left-justify output. */
1328 int showsign = 0; /* Always begin with plus or minus sign. */
1329 int group = 0; /* Print numbers according grouping rules. */
1330 int is_long_double = 0; /* Argument is long double/ long long int. */
1331 int is_short = 0; /* Argument is short int. */
1332 int is_long = 0; /* Argument is long int. */
1333 int is_char = 0; /* Argument is promoted (unsigned) char. */
1334 int width = 0; /* Width of output; 0 means none specified. */
1335 int prec = -1; /* Precision of output; -1 means none specified. */
1336 /* This flag is set by the 'I' modifier and selects the use of the
1337 `outdigits' as determined by the current locale. */
1338 int use_outdigits = 0;
1339 UCHAR_T pad = L_(' ');/* Padding character. */
1340 CHAR_T spec;
1342 workstart = NULL;
1343 workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1345 /* Get current character in format string. */
1346 JUMP (*++f, step0_jumps);
1348 /* ' ' flag. */
1349 LABEL (flag_space):
1350 space = 1;
1351 JUMP (*++f, step0_jumps);
1353 /* '+' flag. */
1354 LABEL (flag_plus):
1355 showsign = 1;
1356 JUMP (*++f, step0_jumps);
1358 /* The '-' flag. */
1359 LABEL (flag_minus):
1360 left = 1;
1361 pad = L_(' ');
1362 JUMP (*++f, step0_jumps);
1364 /* The '#' flag. */
1365 LABEL (flag_hash):
1366 alt = 1;
1367 JUMP (*++f, step0_jumps);
1369 /* The '0' flag. */
1370 LABEL (flag_zero):
1371 if (!left)
1372 pad = L_('0');
1373 JUMP (*++f, step0_jumps);
1375 /* The '\'' flag. */
1376 LABEL (flag_quote):
1377 group = 1;
1379 if (grouping == (const char *) -1)
1381 #ifdef COMPILE_WPRINTF
1382 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1383 _NL_NUMERIC_THOUSANDS_SEP_WC);
1384 #else
1385 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1386 #endif
1388 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1389 if (*grouping == '\0' || *grouping == CHAR_MAX
1390 #ifdef COMPILE_WPRINTF
1391 || thousands_sep == L'\0'
1392 #else
1393 || *thousands_sep == '\0'
1394 #endif
1396 grouping = NULL;
1398 JUMP (*++f, step0_jumps);
1400 LABEL (flag_i18n):
1401 use_outdigits = 1;
1402 JUMP (*++f, step0_jumps);
1404 /* Get width from argument. */
1405 LABEL (width_asterics):
1407 const UCHAR_T *tmp; /* Temporary value. */
1409 tmp = ++f;
1410 if (ISDIGIT (*tmp))
1412 int pos = read_int (&tmp);
1414 if (pos == -1)
1416 __set_errno (EOVERFLOW);
1417 done = -1;
1418 goto all_done;
1421 if (pos && *tmp == L_('$'))
1422 /* The width comes from a positional parameter. */
1423 goto do_positional;
1425 width = va_arg (ap, int);
1427 /* Negative width means left justified. */
1428 if (width < 0)
1430 width = -width;
1431 pad = L_(' ');
1432 left = 1;
1435 if (__builtin_expect (width >= INT_MAX / sizeof (CHAR_T) - 32, 0))
1437 __set_errno (EOVERFLOW);
1438 done = -1;
1439 goto all_done;
1442 if (width >= sizeof (work_buffer) / sizeof (work_buffer[0]) - 32)
1444 /* We have to use a special buffer. The "32" is just a safe
1445 bet for all the output which is not counted in the width. */
1446 size_t needed = ((size_t) width + 32) * sizeof (CHAR_T);
1447 if (__libc_use_alloca (needed))
1448 workend = (CHAR_T *) alloca (needed) + width + 32;
1449 else
1451 workstart = (CHAR_T *) malloc (needed);
1452 if (workstart == NULL)
1454 done = -1;
1455 goto all_done;
1457 workend = workstart + width + 32;
1461 JUMP (*f, step1_jumps);
1463 /* Given width in format string. */
1464 LABEL (width):
1465 width = read_int (&f);
1467 if (__builtin_expect (width == -1
1468 || width >= INT_MAX / sizeof (CHAR_T) - 32, 0))
1470 __set_errno (EOVERFLOW);
1471 done = -1;
1472 goto all_done;
1475 if (width >= sizeof (work_buffer) / sizeof (work_buffer[0]) - 32)
1477 /* We have to use a special buffer. The "32" is just a safe
1478 bet for all the output which is not counted in the width. */
1479 size_t needed = ((size_t) width + 32) * sizeof (CHAR_T);
1480 if (__libc_use_alloca (needed))
1481 workend = (CHAR_T *) alloca (needed) + width + 32;
1482 else
1484 workstart = (CHAR_T *) malloc (needed);
1485 if (workstart == NULL)
1487 done = -1;
1488 goto all_done;
1490 workend = workstart + width + 32;
1493 if (*f == L_('$'))
1494 /* Oh, oh. The argument comes from a positional parameter. */
1495 goto do_positional;
1496 JUMP (*f, step1_jumps);
1498 LABEL (precision):
1499 ++f;
1500 if (*f == L_('*'))
1502 const UCHAR_T *tmp; /* Temporary value. */
1504 tmp = ++f;
1505 if (ISDIGIT (*tmp))
1507 int pos = read_int (&tmp);
1509 if (pos == -1)
1511 __set_errno (EOVERFLOW);
1512 done = -1;
1513 goto all_done;
1516 if (pos && *tmp == L_('$'))
1517 /* The precision comes from a positional parameter. */
1518 goto do_positional;
1520 prec = va_arg (ap, int);
1522 /* If the precision is negative the precision is omitted. */
1523 if (prec < 0)
1524 prec = -1;
1526 else if (ISDIGIT (*f))
1528 prec = read_int (&f);
1530 /* The precision was specified in this case as an extremely
1531 large positive value. */
1532 if (prec == -1)
1534 __set_errno (EOVERFLOW);
1535 done = -1;
1536 goto all_done;
1539 else
1540 prec = 0;
1541 if (prec > width
1542 && prec > sizeof (work_buffer) / sizeof (work_buffer[0]) - 32)
1544 if (__builtin_expect (prec >= INT_MAX / sizeof (CHAR_T) - 32, 0))
1546 __set_errno (EOVERFLOW);
1547 done = -1;
1548 goto all_done;
1550 size_t needed = ((size_t) prec + 32) * sizeof (CHAR_T);
1552 if (__libc_use_alloca (needed))
1553 workend = (CHAR_T *) alloca (needed) + prec + 32;
1554 else
1556 workstart = (CHAR_T *) malloc (needed);
1557 if (workstart == NULL)
1559 done = -1;
1560 goto all_done;
1562 workend = workstart + prec + 32;
1565 JUMP (*f, step2_jumps);
1567 /* Process 'h' modifier. There might another 'h' following. */
1568 LABEL (mod_half):
1569 is_short = 1;
1570 JUMP (*++f, step3a_jumps);
1572 /* Process 'hh' modifier. */
1573 LABEL (mod_halfhalf):
1574 is_short = 0;
1575 is_char = 1;
1576 JUMP (*++f, step4_jumps);
1578 /* Process 'l' modifier. There might another 'l' following. */
1579 LABEL (mod_long):
1580 is_long = 1;
1581 JUMP (*++f, step3b_jumps);
1583 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1584 allowed to follow. */
1585 LABEL (mod_longlong):
1586 is_long_double = 1;
1587 is_long = 1;
1588 JUMP (*++f, step4_jumps);
1590 LABEL (mod_size_t):
1591 is_long_double = sizeof (size_t) > sizeof (unsigned long int);
1592 is_long = sizeof (size_t) > sizeof (unsigned int);
1593 JUMP (*++f, step4_jumps);
1595 LABEL (mod_ptrdiff_t):
1596 is_long_double = sizeof (ptrdiff_t) > sizeof (unsigned long int);
1597 is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
1598 JUMP (*++f, step4_jumps);
1600 LABEL (mod_intmax_t):
1601 is_long_double = sizeof (intmax_t) > sizeof (unsigned long int);
1602 is_long = sizeof (intmax_t) > sizeof (unsigned int);
1603 JUMP (*++f, step4_jumps);
1605 /* Process current format. */
1606 while (1)
1608 process_arg (((struct printf_spec *) NULL));
1609 process_string_arg (((struct printf_spec *) NULL));
1611 LABEL (form_unknown):
1612 if (spec == L_('\0'))
1614 /* The format string ended before the specifier is complete. */
1615 __set_errno (EINVAL);
1616 done = -1;
1617 goto all_done;
1620 /* If we are in the fast loop force entering the complicated
1621 one. */
1622 goto do_positional;
1625 /* The format is correctly handled. */
1626 ++nspecs_done;
1628 if (__builtin_expect (workstart != NULL, 0))
1629 free (workstart);
1630 workstart = NULL;
1632 /* Look for next format specifier. */
1633 #ifdef COMPILE_WPRINTF
1634 f = __find_specwc ((end_of_spec = ++f));
1635 #else
1636 f = __find_specmb ((end_of_spec = ++f));
1637 #endif
1639 /* Write the following constant string. */
1640 outstring (end_of_spec, f - end_of_spec);
1642 while (*f != L_('\0'));
1644 /* Unlock stream and return. */
1645 goto all_done;
1647 /* Here starts the more complex loop to handle positional parameters. */
1648 do_positional:
1650 /* Array with information about the needed arguments. This has to
1651 be dynamically extensible. */
1652 size_t nspecs = 0;
1653 /* A more or less arbitrary start value. */
1654 size_t nspecs_size = 32 * sizeof (struct printf_spec);
1655 struct printf_spec *specs = alloca (nspecs_size);
1657 /* The number of arguments the format string requests. This will
1658 determine the size of the array needed to store the argument
1659 attributes. */
1660 size_t nargs = 0;
1661 size_t bytes_per_arg;
1662 union printf_arg *args_value;
1663 int *args_size;
1664 int *args_type;
1666 /* Positional parameters refer to arguments directly. This could
1667 also determine the maximum number of arguments. Track the
1668 maximum number. */
1669 size_t max_ref_arg = 0;
1671 /* Just a counter. */
1672 size_t cnt;
1674 free (workstart);
1675 workstart = NULL;
1677 if (grouping == (const char *) -1)
1679 #ifdef COMPILE_WPRINTF
1680 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1681 _NL_NUMERIC_THOUSANDS_SEP_WC);
1682 #else
1683 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1684 #endif
1686 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1687 if (*grouping == '\0' || *grouping == CHAR_MAX)
1688 grouping = NULL;
1691 for (f = lead_str_end; *f != L_('\0'); f = specs[nspecs++].next_fmt)
1693 if (nspecs * sizeof (*specs) >= nspecs_size)
1695 /* Extend the array of format specifiers. */
1696 struct printf_spec *old = specs;
1697 specs = extend_alloca (specs, nspecs_size, 2 * nspecs_size);
1699 /* Copy the old array's elements to the new space. */
1700 memmove (specs, old, nspecs * sizeof (*specs));
1703 /* Parse the format specifier. */
1704 #ifdef COMPILE_WPRINTF
1705 nargs += __parse_one_specwc (f, nargs, &specs[nspecs], &max_ref_arg);
1706 #else
1707 nargs += __parse_one_specmb (f, nargs, &specs[nspecs], &max_ref_arg);
1708 #endif
1711 /* Determine the number of arguments the format string consumes. */
1712 nargs = MAX (nargs, max_ref_arg);
1713 /* Calculate total size needed to represent a single argument across
1714 all three argument-related arrays. */
1715 bytes_per_arg = (sizeof (*args_value) + sizeof (*args_size)
1716 + sizeof (*args_type));
1718 /* Check for potential integer overflow. */
1719 if (__builtin_expect (nargs > INT_MAX / bytes_per_arg, 0))
1721 __set_errno (EOVERFLOW);
1722 done = -1;
1723 goto all_done;
1726 /* Allocate memory for all three argument arrays. */
1727 if (__libc_use_alloca (nargs * bytes_per_arg))
1728 args_value = alloca (nargs * bytes_per_arg);
1729 else
1731 args_value = args_malloced = malloc (nargs * bytes_per_arg);
1732 if (args_value == NULL)
1734 done = -1;
1735 goto all_done;
1739 /* Set up the remaining two arrays to each point past the end of the
1740 prior array, since space for all three has been allocated now. */
1741 args_size = &args_value[nargs].pa_int;
1742 args_type = &args_size[nargs];
1743 memset (args_type, s->_flags2 & _IO_FLAGS2_FORTIFY ? '\xff' : '\0',
1744 nargs * sizeof (*args_type));
1746 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1747 still zero after this loop, format is invalid. For now we
1748 simply use 0 as the value. */
1750 /* Fill in the types of all the arguments. */
1751 for (cnt = 0; cnt < nspecs; ++cnt)
1753 /* If the width is determined by an argument this is an int. */
1754 if (specs[cnt].width_arg != -1)
1755 args_type[specs[cnt].width_arg] = PA_INT;
1757 /* If the precision is determined by an argument this is an int. */
1758 if (specs[cnt].prec_arg != -1)
1759 args_type[specs[cnt].prec_arg] = PA_INT;
1761 switch (specs[cnt].ndata_args)
1763 case 0: /* No arguments. */
1764 break;
1765 case 1: /* One argument; we already have the
1766 type and size. */
1767 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1768 args_size[specs[cnt].data_arg] = specs[cnt].size;
1769 break;
1770 default:
1771 /* We have more than one argument for this format spec.
1772 We must call the arginfo function again to determine
1773 all the types. */
1774 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1775 (&specs[cnt].info,
1776 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg],
1777 &args_size[specs[cnt].data_arg]);
1778 break;
1782 /* Now we know all the types and the order. Fill in the argument
1783 values. */
1784 for (cnt = 0; cnt < nargs; ++cnt)
1785 switch (args_type[cnt])
1787 #define T(tag, mem, type) \
1788 case tag: \
1789 args_value[cnt].mem = va_arg (ap_save, type); \
1790 break
1792 T (PA_WCHAR, pa_wchar, wint_t);
1793 case PA_CHAR: /* Promoted. */
1794 case PA_INT|PA_FLAG_SHORT: /* Promoted. */
1795 #if LONG_MAX == INT_MAX
1796 case PA_INT|PA_FLAG_LONG:
1797 #endif
1798 T (PA_INT, pa_int, int);
1799 #if LONG_MAX == LONG_LONG_MAX
1800 case PA_INT|PA_FLAG_LONG:
1801 #endif
1802 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1803 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1804 # error "he?"
1805 #endif
1806 case PA_FLOAT: /* Promoted. */
1807 T (PA_DOUBLE, pa_double, double);
1808 case PA_DOUBLE|PA_FLAG_LONG_DOUBLE:
1809 if (__ldbl_is_dbl)
1811 args_value[cnt].pa_double = va_arg (ap_save, double);
1812 args_type[cnt] &= ~PA_FLAG_LONG_DOUBLE;
1814 else
1815 args_value[cnt].pa_long_double = va_arg (ap_save, long double);
1816 break;
1817 case PA_STRING: /* All pointers are the same */
1818 case PA_WSTRING: /* All pointers are the same */
1819 T (PA_POINTER, pa_pointer, void *);
1820 #undef T
1821 default:
1822 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1823 args_value[cnt].pa_pointer = va_arg (ap_save, void *);
1824 else if (__builtin_expect (__printf_va_arg_table != NULL, 0)
1825 && __printf_va_arg_table[args_type[cnt] - PA_LAST] != NULL)
1827 args_value[cnt].pa_user = alloca (args_size[cnt]);
1828 (*__printf_va_arg_table[args_type[cnt] - PA_LAST])
1829 (args_value[cnt].pa_user, &ap_save);
1831 else
1832 args_value[cnt].pa_long_double = 0.0;
1833 break;
1834 case -1:
1835 /* Error case. Not all parameters appear in N$ format
1836 strings. We have no way to determine their type. */
1837 assert (s->_flags2 & _IO_FLAGS2_FORTIFY);
1838 __libc_fatal ("*** invalid %N$ use detected ***\n");
1841 /* Now walk through all format specifiers and process them. */
1842 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1844 #undef REF
1845 #ifdef SHARED
1846 # define REF(Name) &&do2_##Name - &&do_form_unknown
1847 #else
1848 # define REF(Name) &&do2_##Name
1849 #endif
1850 #undef LABEL
1851 #define LABEL(Name) do2_##Name
1852 STEP4_TABLE;
1854 int is_negative;
1855 union
1857 unsigned long long int longlong;
1858 unsigned long int word;
1859 } number;
1860 int base;
1861 union printf_arg the_arg;
1862 CHAR_T *string; /* Pointer to argument string. */
1864 /* Fill variables from values in struct. */
1865 int alt = specs[nspecs_done].info.alt;
1866 int space = specs[nspecs_done].info.space;
1867 int left = specs[nspecs_done].info.left;
1868 int showsign = specs[nspecs_done].info.showsign;
1869 int group = specs[nspecs_done].info.group;
1870 int is_long_double = specs[nspecs_done].info.is_long_double;
1871 int is_short = specs[nspecs_done].info.is_short;
1872 int is_char = specs[nspecs_done].info.is_char;
1873 int is_long = specs[nspecs_done].info.is_long;
1874 int width = specs[nspecs_done].info.width;
1875 int prec = specs[nspecs_done].info.prec;
1876 int use_outdigits = specs[nspecs_done].info.i18n;
1877 char pad = specs[nspecs_done].info.pad;
1878 CHAR_T spec = specs[nspecs_done].info.spec;
1880 workstart = NULL;
1881 workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1883 /* Fill in last information. */
1884 if (specs[nspecs_done].width_arg != -1)
1886 /* Extract the field width from an argument. */
1887 specs[nspecs_done].info.width =
1888 args_value[specs[nspecs_done].width_arg].pa_int;
1890 if (specs[nspecs_done].info.width < 0)
1891 /* If the width value is negative left justification is
1892 selected and the value is taken as being positive. */
1894 specs[nspecs_done].info.width *= -1;
1895 left = specs[nspecs_done].info.left = 1;
1897 width = specs[nspecs_done].info.width;
1900 if (specs[nspecs_done].prec_arg != -1)
1902 /* Extract the precision from an argument. */
1903 specs[nspecs_done].info.prec =
1904 args_value[specs[nspecs_done].prec_arg].pa_int;
1906 if (specs[nspecs_done].info.prec < 0)
1907 /* If the precision is negative the precision is
1908 omitted. */
1909 specs[nspecs_done].info.prec = -1;
1911 prec = specs[nspecs_done].info.prec;
1914 /* Maybe the buffer is too small. */
1915 if (MAX (prec, width) + 32 > (int) (sizeof (work_buffer)
1916 / sizeof (CHAR_T)))
1918 if (__libc_use_alloca ((MAX (prec, width) + 32)
1919 * sizeof (CHAR_T)))
1920 workend = ((CHAR_T *) alloca ((MAX (prec, width) + 32)
1921 * sizeof (CHAR_T))
1922 + (MAX (prec, width) + 32));
1923 else
1925 workstart = (CHAR_T *) malloc ((MAX (prec, width) + 32)
1926 * sizeof (CHAR_T));
1927 workend = workstart + (MAX (prec, width) + 32);
1931 /* Process format specifiers. */
1932 while (1)
1934 extern printf_function **__printf_function_table;
1935 int function_done;
1937 if (spec <= UCHAR_MAX
1938 && __printf_function_table != NULL
1939 && __printf_function_table[(size_t) spec] != NULL)
1941 const void **ptr = alloca (specs[nspecs_done].ndata_args
1942 * sizeof (const void *));
1944 /* Fill in an array of pointers to the argument values. */
1945 for (unsigned int i = 0; i < specs[nspecs_done].ndata_args;
1946 ++i)
1947 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1949 /* Call the function. */
1950 function_done = __printf_function_table[(size_t) spec]
1951 (s, &specs[nspecs_done].info, ptr);
1953 if (function_done != -2)
1955 /* If an error occurred we don't have information
1956 about # of chars. */
1957 if (function_done < 0)
1959 /* Function has set errno. */
1960 done = -1;
1961 goto all_done;
1964 done_add (function_done);
1965 break;
1969 JUMP (spec, step4_jumps);
1971 process_arg ((&specs[nspecs_done]));
1972 process_string_arg ((&specs[nspecs_done]));
1974 LABEL (form_unknown):
1976 unsigned int i;
1977 const void **ptr;
1979 ptr = alloca (specs[nspecs_done].ndata_args
1980 * sizeof (const void *));
1982 /* Fill in an array of pointers to the argument values. */
1983 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
1984 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1986 /* Call the function. */
1987 function_done = printf_unknown (s, &specs[nspecs_done].info,
1988 ptr);
1990 /* If an error occurred we don't have information about #
1991 of chars. */
1992 if (function_done < 0)
1994 /* Function has set errno. */
1995 done = -1;
1996 goto all_done;
1999 done_add (function_done);
2001 break;
2004 free (workstart);
2005 workstart = NULL;
2007 /* Write the following constant string. */
2008 outstring (specs[nspecs_done].end_of_fmt,
2009 specs[nspecs_done].next_fmt
2010 - specs[nspecs_done].end_of_fmt);
2014 all_done:
2015 free (args_malloced);
2016 free (workstart);
2017 /* Unlock the stream. */
2018 _IO_funlockfile (s);
2019 _IO_cleanup_region_end (0);
2021 return done;
2024 /* Handle an unknown format specifier. This prints out a canonicalized
2025 representation of the format spec itself. */
2026 static int
2027 printf_unknown (FILE *s, const struct printf_info *info,
2028 const void *const *args)
2031 int done = 0;
2032 CHAR_T work_buffer[MAX (sizeof (info->width), sizeof (info->prec)) * 3];
2033 CHAR_T *const workend
2034 = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
2035 register CHAR_T *w;
2037 outchar (L_('%'));
2039 if (info->alt)
2040 outchar (L_('#'));
2041 if (info->group)
2042 outchar (L_('\''));
2043 if (info->showsign)
2044 outchar (L_('+'));
2045 else if (info->space)
2046 outchar (L_(' '));
2047 if (info->left)
2048 outchar (L_('-'));
2049 if (info->pad == L_('0'))
2050 outchar (L_('0'));
2051 if (info->i18n)
2052 outchar (L_('I'));
2054 if (info->width != 0)
2056 w = _itoa_word (info->width, workend, 10, 0);
2057 while (w < workend)
2058 outchar (*w++);
2061 if (info->prec != -1)
2063 outchar (L_('.'));
2064 w = _itoa_word (info->prec, workend, 10, 0);
2065 while (w < workend)
2066 outchar (*w++);
2069 if (info->spec != L_('\0'))
2070 outchar (info->spec);
2072 all_done:
2073 return done;
2076 /* Group the digits according to the grouping rules of the current locale.
2077 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
2078 static CHAR_T *
2079 internal_function
2080 group_number (CHAR_T *w, CHAR_T *rear_ptr, const char *grouping,
2081 #ifdef COMPILE_WPRINTF
2082 wchar_t thousands_sep
2083 #else
2084 const char *thousands_sep
2085 #endif
2088 int len;
2089 CHAR_T *src, *s;
2090 #ifndef COMPILE_WPRINTF
2091 int tlen = strlen (thousands_sep);
2092 #endif
2094 /* We treat all negative values like CHAR_MAX. */
2096 if (*grouping == CHAR_MAX || *grouping <= 0)
2097 /* No grouping should be done. */
2098 return w;
2100 len = *grouping++;
2102 /* Copy existing string so that nothing gets overwritten. */
2103 src = (CHAR_T *) alloca ((rear_ptr - w) * sizeof (CHAR_T));
2104 s = (CHAR_T *) __mempcpy (src, w,
2105 (rear_ptr - w) * sizeof (CHAR_T));
2106 w = rear_ptr;
2108 /* Process all characters in the string. */
2109 while (s > src)
2111 *--w = *--s;
2113 if (--len == 0 && s > src)
2115 /* A new group begins. */
2116 #ifdef COMPILE_WPRINTF
2117 *--w = thousands_sep;
2118 #else
2119 int cnt = tlen;
2121 *--w = thousands_sep[--cnt];
2122 while (cnt > 0);
2123 #endif
2125 if (*grouping == CHAR_MAX
2126 #if CHAR_MIN < 0
2127 || *grouping < 0
2128 #endif
2131 /* No further grouping to be done.
2132 Copy the rest of the number. */
2134 *--w = *--s;
2135 while (s > src);
2136 break;
2138 else if (*grouping != '\0')
2139 /* The previous grouping repeats ad infinitum. */
2140 len = *grouping++;
2141 else
2142 len = grouping[-1];
2145 return w;
2148 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2149 struct helper_file
2151 struct _IO_FILE_plus _f;
2152 #ifdef COMPILE_WPRINTF
2153 struct _IO_wide_data _wide_data;
2154 #endif
2155 _IO_FILE *_put_stream;
2156 #ifdef _IO_MTSAFE_IO
2157 _IO_lock_t lock;
2158 #endif
2161 static int
2162 _IO_helper_overflow (_IO_FILE *s, int c)
2164 _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
2165 #ifdef COMPILE_WPRINTF
2166 int used = s->_wide_data->_IO_write_ptr - s->_wide_data->_IO_write_base;
2167 if (used)
2169 _IO_size_t written = _IO_sputn (target, s->_wide_data->_IO_write_base,
2170 used);
2171 if (written == 0 || written == WEOF)
2172 return WEOF;
2173 __wmemmove (s->_wide_data->_IO_write_base,
2174 s->_wide_data->_IO_write_base + written,
2175 used - written);
2176 s->_wide_data->_IO_write_ptr -= written;
2178 #else
2179 int used = s->_IO_write_ptr - s->_IO_write_base;
2180 if (used)
2182 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
2183 if (written == 0 || written == EOF)
2184 return EOF;
2185 memmove (s->_IO_write_base, s->_IO_write_base + written,
2186 used - written);
2187 s->_IO_write_ptr -= written;
2189 #endif
2190 return PUTC (c, s);
2193 #ifdef COMPILE_WPRINTF
2194 static const struct _IO_jump_t _IO_helper_jumps =
2196 JUMP_INIT_DUMMY,
2197 JUMP_INIT (finish, _IO_wdefault_finish),
2198 JUMP_INIT (overflow, _IO_helper_overflow),
2199 JUMP_INIT (underflow, _IO_default_underflow),
2200 JUMP_INIT (uflow, _IO_default_uflow),
2201 JUMP_INIT (pbackfail, (_IO_pbackfail_t) _IO_wdefault_pbackfail),
2202 JUMP_INIT (xsputn, _IO_wdefault_xsputn),
2203 JUMP_INIT (xsgetn, _IO_wdefault_xsgetn),
2204 JUMP_INIT (seekoff, _IO_default_seekoff),
2205 JUMP_INIT (seekpos, _IO_default_seekpos),
2206 JUMP_INIT (setbuf, _IO_default_setbuf),
2207 JUMP_INIT (sync, _IO_default_sync),
2208 JUMP_INIT (doallocate, _IO_wdefault_doallocate),
2209 JUMP_INIT (read, _IO_default_read),
2210 JUMP_INIT (write, _IO_default_write),
2211 JUMP_INIT (seek, _IO_default_seek),
2212 JUMP_INIT (close, _IO_default_close),
2213 JUMP_INIT (stat, _IO_default_stat)
2215 #else
2216 static const struct _IO_jump_t _IO_helper_jumps =
2218 JUMP_INIT_DUMMY,
2219 JUMP_INIT (finish, _IO_default_finish),
2220 JUMP_INIT (overflow, _IO_helper_overflow),
2221 JUMP_INIT (underflow, _IO_default_underflow),
2222 JUMP_INIT (uflow, _IO_default_uflow),
2223 JUMP_INIT (pbackfail, _IO_default_pbackfail),
2224 JUMP_INIT (xsputn, _IO_default_xsputn),
2225 JUMP_INIT (xsgetn, _IO_default_xsgetn),
2226 JUMP_INIT (seekoff, _IO_default_seekoff),
2227 JUMP_INIT (seekpos, _IO_default_seekpos),
2228 JUMP_INIT (setbuf, _IO_default_setbuf),
2229 JUMP_INIT (sync, _IO_default_sync),
2230 JUMP_INIT (doallocate, _IO_default_doallocate),
2231 JUMP_INIT (read, _IO_default_read),
2232 JUMP_INIT (write, _IO_default_write),
2233 JUMP_INIT (seek, _IO_default_seek),
2234 JUMP_INIT (close, _IO_default_close),
2235 JUMP_INIT (stat, _IO_default_stat)
2237 #endif
2239 static int
2240 internal_function
2241 buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format,
2242 _IO_va_list args)
2244 CHAR_T buf[_IO_BUFSIZ];
2245 struct helper_file helper;
2246 register _IO_FILE *hp = (_IO_FILE *) &helper._f;
2247 int result, to_flush;
2249 /* Orient the stream. */
2250 #ifdef ORIENT
2251 ORIENT;
2252 #endif
2254 /* Initialize helper. */
2255 helper._put_stream = s;
2256 #ifdef COMPILE_WPRINTF
2257 hp->_wide_data = &helper._wide_data;
2258 _IO_wsetp (hp, buf, buf + sizeof buf / sizeof (CHAR_T));
2259 hp->_mode = 1;
2260 #else
2261 _IO_setp (hp, buf, buf + sizeof buf);
2262 hp->_mode = -1;
2263 #endif
2264 hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS|_IO_USER_LOCK;
2265 #if _IO_JUMPS_OFFSET
2266 hp->_vtable_offset = 0;
2267 #endif
2268 #ifdef _IO_MTSAFE_IO
2269 hp->_lock = NULL;
2270 #endif
2271 hp->_flags2 = s->_flags2;
2272 _IO_JUMPS (&helper._f) = (struct _IO_jump_t *) &_IO_helper_jumps;
2274 /* Now print to helper instead. */
2275 #ifndef COMPILE_WPRINTF
2276 result = _IO_vfprintf (hp, format, args);
2277 #else
2278 result = vfprintf (hp, format, args);
2279 #endif
2281 /* Lock stream. */
2282 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, s);
2283 _IO_flockfile (s);
2285 /* Now flush anything from the helper to the S. */
2286 #ifdef COMPILE_WPRINTF
2287 if ((to_flush = (hp->_wide_data->_IO_write_ptr
2288 - hp->_wide_data->_IO_write_base)) > 0)
2290 if ((int) _IO_sputn (s, hp->_wide_data->_IO_write_base, to_flush)
2291 != to_flush)
2292 result = -1;
2294 #else
2295 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
2297 if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
2298 result = -1;
2300 #endif
2302 /* Unlock the stream. */
2303 _IO_funlockfile (s);
2304 __libc_cleanup_region_end (0);
2306 return result;
2309 #undef vfprintf
2310 #ifdef COMPILE_WPRINTF
2311 strong_alias (_IO_vfwprintf, __vfwprintf);
2312 ldbl_weak_alias (_IO_vfwprintf, vfwprintf);
2313 #else
2314 ldbl_strong_alias (_IO_vfprintf_internal, vfprintf);
2315 ldbl_hidden_def (_IO_vfprintf_internal, vfprintf)
2316 ldbl_strong_alias (_IO_vfprintf_internal, _IO_vfprintf);
2317 ldbl_hidden_def (_IO_vfprintf_internal, _IO_vfprintf)
2318 #endif