2012-03-02 Kees Cook <keescook@chromium.org>
[glibc.git] / stdio-common / vfprintf.c
blobc802e4634914ce86e279d2693a3fa0093b32d782
1 /* Copyright (C) 1991-2008, 2009, 2010, 2011 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 ((unsigned int) INT_MAX - (unsigned int) done \
71 < _val, 0)) \
72 { \
73 done = -1; \
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 (INTUSE(_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) \
145 || (size_t) INT_MAX - (size_t) done < (size_t) (Len)) \
147 done = -1; \
148 goto all_done; \
150 done += (Len); \
152 while (0)
154 /* For handling long_double and longlong we use the same flag. If
155 `long' and `long long' are effectively the same type define it to
156 zero. */
157 #if LONG_MAX == LONG_LONG_MAX
158 # define is_longlong 0
159 #else
160 # define is_longlong is_long_double
161 #endif
163 /* If `long' and `int' is effectively the same type we don't have to
164 handle `long separately. */
165 #if INT_MAX == LONG_MAX
166 # define is_long_num 0
167 #else
168 # define is_long_num is_long
169 #endif
172 /* Global variables. */
173 static const CHAR_T null[] = L_("(null)");
176 /* Helper function to provide temporary buffering for unbuffered streams. */
177 static int buffered_vfprintf (FILE *stream, const CHAR_T *fmt, va_list)
178 __THROW __attribute__ ((noinline)) internal_function;
180 /* Handle unknown format specifier. */
181 static int printf_unknown (FILE *, const struct printf_info *,
182 const void *const *) __THROW;
184 /* Group digits of number string. */
185 #ifdef COMPILE_WPRINTF
186 static CHAR_T *group_number (CHAR_T *, CHAR_T *, const char *, wchar_t)
187 __THROW internal_function;
188 #else
189 static CHAR_T *group_number (CHAR_T *, CHAR_T *, const char *, const char *)
190 __THROW internal_function;
191 #endif
194 /* The function itself. */
196 vfprintf (FILE *s, const CHAR_T *format, va_list ap)
198 /* The character used as thousands separator. */
199 #ifdef COMPILE_WPRINTF
200 wchar_t thousands_sep = L'\0';
201 #else
202 const char *thousands_sep = NULL;
203 #endif
205 /* The string describing the size of groups of digits. */
206 const char *grouping;
208 /* Place to accumulate the result. */
209 int done;
211 /* Current character in format string. */
212 const UCHAR_T *f;
214 /* End of leading constant string. */
215 const UCHAR_T *lead_str_end;
217 /* Points to next format specifier. */
218 const UCHAR_T *end_of_spec;
220 /* Buffer intermediate results. */
221 CHAR_T work_buffer[1000];
222 CHAR_T *workstart = NULL;
223 CHAR_T *workend;
225 /* We have to save the original argument pointer. */
226 va_list ap_save;
228 /* Count number of specifiers we already processed. */
229 int nspecs_done;
231 /* For the %m format we may need the current `errno' value. */
232 int save_errno = errno;
234 /* 1 if format is in read-only memory, -1 if it is in writable memory,
235 0 if unknown. */
236 int readonly_format = 0;
238 /* For the argument descriptions, which may be allocated on the heap. */
239 void *args_malloced = NULL;
241 /* This table maps a character into a number representing a
242 class. In each step there is a destination label for each
243 class. */
244 static const uint8_t jump_table[] =
246 /* ' ' */ 1, 0, 0, /* '#' */ 4,
247 0, /* '%' */ 14, 0, /* '\''*/ 6,
248 0, 0, /* '*' */ 7, /* '+' */ 2,
249 0, /* '-' */ 3, /* '.' */ 9, 0,
250 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
251 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
252 /* '8' */ 8, /* '9' */ 8, 0, 0,
253 0, 0, 0, 0,
254 0, /* 'A' */ 26, 0, /* 'C' */ 25,
255 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
256 0, /* 'I' */ 29, 0, 0,
257 /* 'L' */ 12, 0, 0, 0,
258 0, 0, 0, /* 'S' */ 21,
259 0, 0, 0, 0,
260 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
261 0, 0, 0, 0,
262 0, /* 'a' */ 26, 0, /* 'c' */ 20,
263 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
264 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
265 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
266 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
267 /* 't' */ 27, /* 'u' */ 16, 0, 0,
268 /* 'x' */ 18, 0, /* 'z' */ 13
271 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
272 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
273 #ifdef SHARED
274 /* 'int' is enough and it saves some space on 64 bit systems. */
275 # define JUMP_TABLE_TYPE const int
276 # define JUMP(ChExpr, table) \
277 do \
279 int offset; \
280 void *__unbounded ptr; \
281 spec = (ChExpr); \
282 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
283 : table[CHAR_CLASS (spec)]; \
284 ptr = &&do_form_unknown + offset; \
285 goto *ptr; \
287 while (0)
288 #else
289 # define JUMP_TABLE_TYPE const void *const
290 # define JUMP(ChExpr, table) \
291 do \
293 const void *__unbounded ptr; \
294 spec = (ChExpr); \
295 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
296 : table[CHAR_CLASS (spec)]; \
297 goto *ptr; \
299 while (0)
300 #endif
302 #define STEP0_3_TABLE \
303 /* Step 0: at the beginning. */ \
304 static JUMP_TABLE_TYPE step0_jumps[30] = \
306 REF (form_unknown), \
307 REF (flag_space), /* for ' ' */ \
308 REF (flag_plus), /* for '+' */ \
309 REF (flag_minus), /* for '-' */ \
310 REF (flag_hash), /* for '<hash>' */ \
311 REF (flag_zero), /* for '0' */ \
312 REF (flag_quote), /* for '\'' */ \
313 REF (width_asterics), /* for '*' */ \
314 REF (width), /* for '1'...'9' */ \
315 REF (precision), /* for '.' */ \
316 REF (mod_half), /* for 'h' */ \
317 REF (mod_long), /* for 'l' */ \
318 REF (mod_longlong), /* for 'L', 'q' */ \
319 REF (mod_size_t), /* for 'z', 'Z' */ \
320 REF (form_percent), /* for '%' */ \
321 REF (form_integer), /* for 'd', 'i' */ \
322 REF (form_unsigned), /* for 'u' */ \
323 REF (form_octal), /* for 'o' */ \
324 REF (form_hexa), /* for 'X', 'x' */ \
325 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
326 REF (form_character), /* for 'c' */ \
327 REF (form_string), /* for 's', 'S' */ \
328 REF (form_pointer), /* for 'p' */ \
329 REF (form_number), /* for 'n' */ \
330 REF (form_strerror), /* for 'm' */ \
331 REF (form_wcharacter), /* for 'C' */ \
332 REF (form_floathex), /* for 'A', 'a' */ \
333 REF (mod_ptrdiff_t), /* for 't' */ \
334 REF (mod_intmax_t), /* for 'j' */ \
335 REF (flag_i18n), /* for 'I' */ \
336 }; \
337 /* Step 1: after processing width. */ \
338 static JUMP_TABLE_TYPE step1_jumps[30] = \
340 REF (form_unknown), \
341 REF (form_unknown), /* for ' ' */ \
342 REF (form_unknown), /* for '+' */ \
343 REF (form_unknown), /* for '-' */ \
344 REF (form_unknown), /* for '<hash>' */ \
345 REF (form_unknown), /* for '0' */ \
346 REF (form_unknown), /* for '\'' */ \
347 REF (form_unknown), /* for '*' */ \
348 REF (form_unknown), /* for '1'...'9' */ \
349 REF (precision), /* for '.' */ \
350 REF (mod_half), /* for 'h' */ \
351 REF (mod_long), /* for 'l' */ \
352 REF (mod_longlong), /* for 'L', 'q' */ \
353 REF (mod_size_t), /* for 'z', 'Z' */ \
354 REF (form_percent), /* for '%' */ \
355 REF (form_integer), /* for 'd', 'i' */ \
356 REF (form_unsigned), /* for 'u' */ \
357 REF (form_octal), /* for 'o' */ \
358 REF (form_hexa), /* for 'X', 'x' */ \
359 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
360 REF (form_character), /* for 'c' */ \
361 REF (form_string), /* for 's', 'S' */ \
362 REF (form_pointer), /* for 'p' */ \
363 REF (form_number), /* for 'n' */ \
364 REF (form_strerror), /* for 'm' */ \
365 REF (form_wcharacter), /* for 'C' */ \
366 REF (form_floathex), /* for 'A', 'a' */ \
367 REF (mod_ptrdiff_t), /* for 't' */ \
368 REF (mod_intmax_t), /* for 'j' */ \
369 REF (form_unknown) /* for 'I' */ \
370 }; \
371 /* Step 2: after processing precision. */ \
372 static JUMP_TABLE_TYPE step2_jumps[30] = \
374 REF (form_unknown), \
375 REF (form_unknown), /* for ' ' */ \
376 REF (form_unknown), /* for '+' */ \
377 REF (form_unknown), /* for '-' */ \
378 REF (form_unknown), /* for '<hash>' */ \
379 REF (form_unknown), /* for '0' */ \
380 REF (form_unknown), /* for '\'' */ \
381 REF (form_unknown), /* for '*' */ \
382 REF (form_unknown), /* for '1'...'9' */ \
383 REF (form_unknown), /* for '.' */ \
384 REF (mod_half), /* for 'h' */ \
385 REF (mod_long), /* for 'l' */ \
386 REF (mod_longlong), /* for 'L', 'q' */ \
387 REF (mod_size_t), /* for 'z', 'Z' */ \
388 REF (form_percent), /* for '%' */ \
389 REF (form_integer), /* for 'd', 'i' */ \
390 REF (form_unsigned), /* for 'u' */ \
391 REF (form_octal), /* for 'o' */ \
392 REF (form_hexa), /* for 'X', 'x' */ \
393 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
394 REF (form_character), /* for 'c' */ \
395 REF (form_string), /* for 's', 'S' */ \
396 REF (form_pointer), /* for 'p' */ \
397 REF (form_number), /* for 'n' */ \
398 REF (form_strerror), /* for 'm' */ \
399 REF (form_wcharacter), /* for 'C' */ \
400 REF (form_floathex), /* for 'A', 'a' */ \
401 REF (mod_ptrdiff_t), /* for 't' */ \
402 REF (mod_intmax_t), /* for 'j' */ \
403 REF (form_unknown) /* for 'I' */ \
404 }; \
405 /* Step 3a: after processing first 'h' modifier. */ \
406 static JUMP_TABLE_TYPE step3a_jumps[30] = \
408 REF (form_unknown), \
409 REF (form_unknown), /* for ' ' */ \
410 REF (form_unknown), /* for '+' */ \
411 REF (form_unknown), /* for '-' */ \
412 REF (form_unknown), /* for '<hash>' */ \
413 REF (form_unknown), /* for '0' */ \
414 REF (form_unknown), /* for '\'' */ \
415 REF (form_unknown), /* for '*' */ \
416 REF (form_unknown), /* for '1'...'9' */ \
417 REF (form_unknown), /* for '.' */ \
418 REF (mod_halfhalf), /* for 'h' */ \
419 REF (form_unknown), /* for 'l' */ \
420 REF (form_unknown), /* for 'L', 'q' */ \
421 REF (form_unknown), /* for 'z', 'Z' */ \
422 REF (form_percent), /* for '%' */ \
423 REF (form_integer), /* for 'd', 'i' */ \
424 REF (form_unsigned), /* for 'u' */ \
425 REF (form_octal), /* for 'o' */ \
426 REF (form_hexa), /* for 'X', 'x' */ \
427 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
428 REF (form_unknown), /* for 'c' */ \
429 REF (form_unknown), /* for 's', 'S' */ \
430 REF (form_unknown), /* for 'p' */ \
431 REF (form_number), /* for 'n' */ \
432 REF (form_unknown), /* for 'm' */ \
433 REF (form_unknown), /* for 'C' */ \
434 REF (form_unknown), /* for 'A', 'a' */ \
435 REF (form_unknown), /* for 't' */ \
436 REF (form_unknown), /* for 'j' */ \
437 REF (form_unknown) /* for 'I' */ \
438 }; \
439 /* Step 3b: after processing first 'l' modifier. */ \
440 static JUMP_TABLE_TYPE step3b_jumps[30] = \
442 REF (form_unknown), \
443 REF (form_unknown), /* for ' ' */ \
444 REF (form_unknown), /* for '+' */ \
445 REF (form_unknown), /* for '-' */ \
446 REF (form_unknown), /* for '<hash>' */ \
447 REF (form_unknown), /* for '0' */ \
448 REF (form_unknown), /* for '\'' */ \
449 REF (form_unknown), /* for '*' */ \
450 REF (form_unknown), /* for '1'...'9' */ \
451 REF (form_unknown), /* for '.' */ \
452 REF (form_unknown), /* for 'h' */ \
453 REF (mod_longlong), /* for 'l' */ \
454 REF (form_unknown), /* for 'L', 'q' */ \
455 REF (form_unknown), /* for 'z', 'Z' */ \
456 REF (form_percent), /* for '%' */ \
457 REF (form_integer), /* for 'd', 'i' */ \
458 REF (form_unsigned), /* for 'u' */ \
459 REF (form_octal), /* for 'o' */ \
460 REF (form_hexa), /* for 'X', 'x' */ \
461 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
462 REF (form_character), /* for 'c' */ \
463 REF (form_string), /* for 's', 'S' */ \
464 REF (form_pointer), /* for 'p' */ \
465 REF (form_number), /* for 'n' */ \
466 REF (form_strerror), /* for 'm' */ \
467 REF (form_wcharacter), /* for 'C' */ \
468 REF (form_floathex), /* for 'A', 'a' */ \
469 REF (form_unknown), /* for 't' */ \
470 REF (form_unknown), /* for 'j' */ \
471 REF (form_unknown) /* for 'I' */ \
474 #define STEP4_TABLE \
475 /* Step 4: processing format specifier. */ \
476 static JUMP_TABLE_TYPE step4_jumps[30] = \
478 REF (form_unknown), \
479 REF (form_unknown), /* for ' ' */ \
480 REF (form_unknown), /* for '+' */ \
481 REF (form_unknown), /* for '-' */ \
482 REF (form_unknown), /* for '<hash>' */ \
483 REF (form_unknown), /* for '0' */ \
484 REF (form_unknown), /* for '\'' */ \
485 REF (form_unknown), /* for '*' */ \
486 REF (form_unknown), /* for '1'...'9' */ \
487 REF (form_unknown), /* for '.' */ \
488 REF (form_unknown), /* for 'h' */ \
489 REF (form_unknown), /* for 'l' */ \
490 REF (form_unknown), /* for 'L', 'q' */ \
491 REF (form_unknown), /* for 'z', 'Z' */ \
492 REF (form_percent), /* for '%' */ \
493 REF (form_integer), /* for 'd', 'i' */ \
494 REF (form_unsigned), /* for 'u' */ \
495 REF (form_octal), /* for 'o' */ \
496 REF (form_hexa), /* for 'X', 'x' */ \
497 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
498 REF (form_character), /* for 'c' */ \
499 REF (form_string), /* for 's', 'S' */ \
500 REF (form_pointer), /* for 'p' */ \
501 REF (form_number), /* for 'n' */ \
502 REF (form_strerror), /* for 'm' */ \
503 REF (form_wcharacter), /* for 'C' */ \
504 REF (form_floathex), /* for 'A', 'a' */ \
505 REF (form_unknown), /* for 't' */ \
506 REF (form_unknown), /* for 'j' */ \
507 REF (form_unknown) /* for 'I' */ \
511 #define process_arg(fspec) \
512 /* Start real work. We know about all flags and modifiers and \
513 now process the wanted format specifier. */ \
514 LABEL (form_percent): \
515 /* Write a literal "%". */ \
516 outchar (L_('%')); \
517 break; \
519 LABEL (form_integer): \
520 /* Signed decimal integer. */ \
521 base = 10; \
523 if (is_longlong) \
525 long long int signed_number; \
527 if (fspec == NULL) \
528 signed_number = va_arg (ap, long long int); \
529 else \
530 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
532 is_negative = signed_number < 0; \
533 number.longlong = is_negative ? (- signed_number) : signed_number; \
535 goto LABEL (longlong_number); \
537 else \
539 long int signed_number; \
541 if (fspec == NULL) \
543 if (is_long_num) \
544 signed_number = va_arg (ap, long int); \
545 else if (is_char) \
546 signed_number = (signed char) va_arg (ap, unsigned int); \
547 else if (!is_short) \
548 signed_number = va_arg (ap, int); \
549 else \
550 signed_number = (short int) va_arg (ap, unsigned int); \
552 else \
553 if (is_long_num) \
554 signed_number = args_value[fspec->data_arg].pa_long_int; \
555 else if (is_char) \
556 signed_number = (signed char) \
557 args_value[fspec->data_arg].pa_u_int; \
558 else if (!is_short) \
559 signed_number = args_value[fspec->data_arg].pa_int; \
560 else \
561 signed_number = (short int) \
562 args_value[fspec->data_arg].pa_u_int; \
564 is_negative = signed_number < 0; \
565 number.word = is_negative ? (- signed_number) : signed_number; \
567 goto LABEL (number); \
569 /* NOTREACHED */ \
571 LABEL (form_unsigned): \
572 /* Unsigned decimal integer. */ \
573 base = 10; \
574 goto LABEL (unsigned_number); \
575 /* NOTREACHED */ \
577 LABEL (form_octal): \
578 /* Unsigned octal integer. */ \
579 base = 8; \
580 goto LABEL (unsigned_number); \
581 /* NOTREACHED */ \
583 LABEL (form_hexa): \
584 /* Unsigned hexadecimal integer. */ \
585 base = 16; \
587 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
589 /* ISO specifies the `+' and ` ' flags only for signed \
590 conversions. */ \
591 is_negative = 0; \
592 showsign = 0; \
593 space = 0; \
595 if (is_longlong) \
597 if (fspec == NULL) \
598 number.longlong = va_arg (ap, unsigned long long int); \
599 else \
600 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
602 LABEL (longlong_number): \
603 if (prec < 0) \
604 /* Supply a default precision if none was given. */ \
605 prec = 1; \
606 else \
607 /* We have to take care for the '0' flag. If a precision \
608 is given it must be ignored. */ \
609 pad = L_(' '); \
611 /* If the precision is 0 and the number is 0 nothing has to \
612 be written for the number, except for the 'o' format in \
613 alternate form. */ \
614 if (prec == 0 && number.longlong == 0) \
616 string = workend; \
617 if (base == 8 && alt) \
618 *--string = L_('0'); \
620 else \
622 /* Put the number in WORK. */ \
623 string = _itoa (number.longlong, workend, base, \
624 spec == L_('X')); \
625 if (group && grouping) \
626 string = group_number (string, workend, grouping, \
627 thousands_sep); \
629 if (use_outdigits && base == 10) \
630 string = _i18n_number_rewrite (string, workend, workend); \
632 /* Simplify further test for num != 0. */ \
633 number.word = number.longlong != 0; \
635 else \
637 if (fspec == NULL) \
639 if (is_long_num) \
640 number.word = va_arg (ap, unsigned long int); \
641 else if (is_char) \
642 number.word = (unsigned char) va_arg (ap, unsigned int); \
643 else if (!is_short) \
644 number.word = va_arg (ap, unsigned int); \
645 else \
646 number.word = (unsigned short int) va_arg (ap, unsigned int); \
648 else \
649 if (is_long_num) \
650 number.word = args_value[fspec->data_arg].pa_u_long_int; \
651 else if (is_char) \
652 number.word = (unsigned char) \
653 args_value[fspec->data_arg].pa_u_int; \
654 else if (!is_short) \
655 number.word = args_value[fspec->data_arg].pa_u_int; \
656 else \
657 number.word = (unsigned short int) \
658 args_value[fspec->data_arg].pa_u_int; \
660 LABEL (number): \
661 if (prec < 0) \
662 /* Supply a default precision if none was given. */ \
663 prec = 1; \
664 else \
665 /* We have to take care for the '0' flag. If a precision \
666 is given it must be ignored. */ \
667 pad = L_(' '); \
669 /* If the precision is 0 and the number is 0 nothing has to \
670 be written for the number, except for the 'o' format in \
671 alternate form. */ \
672 if (prec == 0 && number.word == 0) \
674 string = workend; \
675 if (base == 8 && alt) \
676 *--string = L_('0'); \
678 else \
680 /* Put the number in WORK. */ \
681 string = _itoa_word (number.word, workend, base, \
682 spec == L_('X')); \
683 if (group && grouping) \
684 string = group_number (string, workend, grouping, \
685 thousands_sep); \
687 if (use_outdigits && base == 10) \
688 string = _i18n_number_rewrite (string, workend, workend); \
692 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
693 /* Add octal marker. */ \
694 *--string = L_('0'); \
696 prec = MAX (0, prec - (workend - string)); \
698 if (!left) \
700 width -= workend - string + prec; \
702 if (number.word != 0 && alt && base == 16) \
703 /* Account for 0X hex marker. */ \
704 width -= 2; \
706 if (is_negative || showsign || space) \
707 --width; \
709 if (pad == L_(' ')) \
711 PAD (L_(' ')); \
712 width = 0; \
715 if (is_negative) \
716 outchar (L_('-')); \
717 else if (showsign) \
718 outchar (L_('+')); \
719 else if (space) \
720 outchar (L_(' ')); \
722 if (number.word != 0 && alt && base == 16) \
724 outchar (L_('0')); \
725 outchar (spec); \
728 width += prec; \
729 PAD (L_('0')); \
731 outstring (string, workend - string); \
733 break; \
735 else \
737 if (is_negative) \
739 outchar (L_('-')); \
740 --width; \
742 else if (showsign) \
744 outchar (L_('+')); \
745 --width; \
747 else if (space) \
749 outchar (L_(' ')); \
750 --width; \
753 if (number.word != 0 && alt && base == 16) \
755 outchar (L_('0')); \
756 outchar (spec); \
757 width -= 2; \
760 width -= workend - string + prec; \
762 if (prec > 0) \
764 int temp = width; \
765 width = prec; \
766 PAD (L_('0')); \
767 width = temp; \
770 outstring (string, workend - string); \
772 PAD (L_(' ')); \
773 break; \
776 LABEL (form_float): \
778 /* Floating-point number. This is handled by printf_fp.c. */ \
779 const void *ptr; \
780 int function_done; \
782 if (fspec == NULL) \
784 if (__ldbl_is_dbl) \
785 is_long_double = 0; \
787 struct printf_info info = { .prec = prec, \
788 .width = width, \
789 .spec = spec, \
790 .is_long_double = is_long_double, \
791 .is_short = is_short, \
792 .is_long = is_long, \
793 .alt = alt, \
794 .space = space, \
795 .left = left, \
796 .showsign = showsign, \
797 .group = group, \
798 .pad = pad, \
799 .extra = 0, \
800 .i18n = use_outdigits, \
801 .wide = sizeof (CHAR_T) != 1 }; \
803 if (is_long_double) \
804 the_arg.pa_long_double = va_arg (ap, long double); \
805 else \
806 the_arg.pa_double = va_arg (ap, double); \
807 ptr = (const void *) &the_arg; \
809 function_done = __printf_fp (s, &info, &ptr); \
811 else \
813 ptr = (const void *) &args_value[fspec->data_arg]; \
814 if (__ldbl_is_dbl) \
816 fspec->data_arg_type = PA_DOUBLE; \
817 fspec->info.is_long_double = 0; \
820 function_done = __printf_fp (s, &fspec->info, &ptr); \
823 if (function_done < 0) \
825 /* Error in print handler. */ \
826 done = -1; \
827 goto all_done; \
830 done_add (function_done); \
832 break; \
834 LABEL (form_floathex): \
836 /* Floating point number printed as hexadecimal number. */ \
837 const void *ptr; \
838 int function_done; \
840 if (fspec == NULL) \
842 if (__ldbl_is_dbl) \
843 is_long_double = 0; \
845 struct printf_info info = { .prec = prec, \
846 .width = width, \
847 .spec = spec, \
848 .is_long_double = is_long_double, \
849 .is_short = is_short, \
850 .is_long = is_long, \
851 .alt = alt, \
852 .space = space, \
853 .left = left, \
854 .showsign = showsign, \
855 .group = group, \
856 .pad = pad, \
857 .extra = 0, \
858 .wide = sizeof (CHAR_T) != 1 }; \
860 if (is_long_double) \
861 the_arg.pa_long_double = va_arg (ap, long double); \
862 else \
863 the_arg.pa_double = va_arg (ap, double); \
864 ptr = (const void *) &the_arg; \
866 function_done = __printf_fphex (s, &info, &ptr); \
868 else \
870 ptr = (const void *) &args_value[fspec->data_arg]; \
871 if (__ldbl_is_dbl) \
872 fspec->info.is_long_double = 0; \
874 function_done = __printf_fphex (s, &fspec->info, &ptr); \
877 if (function_done < 0) \
879 /* Error in print handler. */ \
880 done = -1; \
881 goto all_done; \
884 done_add (function_done); \
886 break; \
888 LABEL (form_pointer): \
889 /* Generic pointer. */ \
891 const void *ptr; \
892 if (fspec == NULL) \
893 ptr = va_arg (ap, void *); \
894 else \
895 ptr = args_value[fspec->data_arg].pa_pointer; \
896 if (ptr != NULL) \
898 /* If the pointer is not NULL, write it as a %#x spec. */ \
899 base = 16; \
900 number.word = (unsigned long int) ptr; \
901 is_negative = 0; \
902 alt = 1; \
903 group = 0; \
904 spec = L_('x'); \
905 goto LABEL (number); \
907 else \
909 /* Write "(nil)" for a nil pointer. */ \
910 string = (CHAR_T *) L_("(nil)"); \
911 /* Make sure the full string "(nil)" is printed. */ \
912 if (prec < 5) \
913 prec = 5; \
914 is_long = 0; /* This is no wide-char string. */ \
915 goto LABEL (print_string); \
918 /* NOTREACHED */ \
920 LABEL (form_number): \
921 if (s->_flags2 & _IO_FLAGS2_FORTIFY) \
923 if (! readonly_format) \
925 extern int __readonly_area (const void *, size_t) \
926 attribute_hidden; \
927 readonly_format \
928 = __readonly_area (format, ((STR_LEN (format) + 1) \
929 * sizeof (CHAR_T))); \
931 if (readonly_format < 0) \
932 __libc_fatal ("*** %n in writable segment detected ***\n"); \
934 /* Answer the count of characters written. */ \
935 if (fspec == NULL) \
937 if (is_longlong) \
938 *(long long int *) va_arg (ap, void *) = done; \
939 else if (is_long_num) \
940 *(long int *) va_arg (ap, void *) = done; \
941 else if (is_char) \
942 *(char *) va_arg (ap, void *) = done; \
943 else if (!is_short) \
944 *(int *) va_arg (ap, void *) = done; \
945 else \
946 *(short int *) va_arg (ap, void *) = done; \
948 else \
949 if (is_longlong) \
950 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
951 else if (is_long_num) \
952 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
953 else if (is_char) \
954 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
955 else if (!is_short) \
956 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
957 else \
958 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
959 break; \
961 LABEL (form_strerror): \
962 /* Print description of error ERRNO. */ \
963 string = \
964 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
965 sizeof work_buffer); \
966 is_long = 0; /* This is no wide-char string. */ \
967 goto LABEL (print_string)
969 #ifdef COMPILE_WPRINTF
970 # define process_string_arg(fspec) \
971 LABEL (form_character): \
972 /* Character. */ \
973 if (is_long) \
974 goto LABEL (form_wcharacter); \
975 --width; /* Account for the character itself. */ \
976 if (!left) \
977 PAD (L' '); \
978 if (fspec == NULL) \
979 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
980 else \
981 outchar (__btowc ((unsigned char) \
982 args_value[fspec->data_arg].pa_int)); \
983 if (left) \
984 PAD (L' '); \
985 break; \
987 LABEL (form_wcharacter): \
989 /* Wide character. */ \
990 --width; \
991 if (!left) \
992 PAD (L' '); \
993 if (fspec == NULL) \
994 outchar (va_arg (ap, wchar_t)); \
995 else \
996 outchar (args_value[fspec->data_arg].pa_wchar); \
997 if (left) \
998 PAD (L' '); \
1000 break; \
1002 LABEL (form_string): \
1004 size_t len; \
1005 int string_malloced; \
1007 /* The string argument could in fact be `char *' or `wchar_t *'. \
1008 But this should not make a difference here. */ \
1009 if (fspec == NULL) \
1010 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
1011 else \
1012 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
1014 /* Entry point for printing other strings. */ \
1015 LABEL (print_string): \
1017 string_malloced = 0; \
1018 if (string == NULL) \
1020 /* Write "(null)" if there's space. */ \
1021 if (prec == -1 \
1022 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
1024 string = (CHAR_T *) null; \
1025 len = (sizeof (null) / sizeof (null[0])) - 1; \
1027 else \
1029 string = (CHAR_T *) L""; \
1030 len = 0; \
1033 else if (!is_long && spec != L_('S')) \
1035 /* This is complicated. We have to transform the multibyte \
1036 string into a wide character string. */ \
1037 const char *mbs = (const char *) string; \
1038 mbstate_t mbstate; \
1040 len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
1042 /* Allocate dynamically an array which definitely is long \
1043 enough for the wide character version. Each byte in the \
1044 multi-byte string can produce at most one wide character. */ \
1045 if (__libc_use_alloca (len * sizeof (wchar_t))) \
1046 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1047 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1048 == NULL) \
1050 done = -1; \
1051 goto all_done; \
1053 else \
1054 string_malloced = 1; \
1056 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1057 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1058 if (len == (size_t) -1) \
1060 /* Illegal multibyte character. */ \
1061 done = -1; \
1062 goto all_done; \
1065 else \
1067 if (prec != -1) \
1068 /* Search for the end of the string, but don't search past \
1069 the length specified by the precision. */ \
1070 len = __wcsnlen (string, prec); \
1071 else \
1072 len = __wcslen (string); \
1075 if ((width -= len) < 0) \
1077 outstring (string, len); \
1078 break; \
1081 if (!left) \
1082 PAD (L' '); \
1083 outstring (string, len); \
1084 if (left) \
1085 PAD (L' '); \
1086 if (__builtin_expect (string_malloced, 0)) \
1087 free (string); \
1089 break;
1090 #else
1091 # define process_string_arg(fspec) \
1092 LABEL (form_character): \
1093 /* Character. */ \
1094 if (is_long) \
1095 goto LABEL (form_wcharacter); \
1096 --width; /* Account for the character itself. */ \
1097 if (!left) \
1098 PAD (' '); \
1099 if (fspec == NULL) \
1100 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1101 else \
1102 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1103 if (left) \
1104 PAD (' '); \
1105 break; \
1107 LABEL (form_wcharacter): \
1109 /* Wide character. */ \
1110 char buf[MB_CUR_MAX]; \
1111 mbstate_t mbstate; \
1112 size_t len; \
1114 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1115 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1116 : args_value[fspec->data_arg].pa_wchar), \
1117 &mbstate); \
1118 if (len == (size_t) -1) \
1120 /* Something went wron gduring the conversion. Bail out. */ \
1121 done = -1; \
1122 goto all_done; \
1124 width -= len; \
1125 if (!left) \
1126 PAD (' '); \
1127 outstring (buf, len); \
1128 if (left) \
1129 PAD (' '); \
1131 break; \
1133 LABEL (form_string): \
1135 size_t len; \
1136 int string_malloced; \
1138 /* The string argument could in fact be `char *' or `wchar_t *'. \
1139 But this should not make a difference here. */ \
1140 if (fspec == NULL) \
1141 string = (char *) va_arg (ap, const char *); \
1142 else \
1143 string = (char *) args_value[fspec->data_arg].pa_string; \
1145 /* Entry point for printing other strings. */ \
1146 LABEL (print_string): \
1148 string_malloced = 0; \
1149 if (string == NULL) \
1151 /* Write "(null)" if there's space. */ \
1152 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1154 string = (char *) null; \
1155 len = sizeof (null) - 1; \
1157 else \
1159 string = (char *) ""; \
1160 len = 0; \
1163 else if (!is_long && spec != L_('S')) \
1165 if (prec != -1) \
1167 /* Search for the end of the string, but don't search past \
1168 the length (in bytes) specified by the precision. Also \
1169 don't use incomplete characters. */ \
1170 if (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MB_CUR_MAX) == 1) \
1171 len = __strnlen (string, prec); \
1172 else \
1174 /* In case we have a multibyte character set the \
1175 situation is more complicated. We must not copy \
1176 bytes at the end which form an incomplete character. */\
1177 size_t ignore_size = (unsigned) prec > 1024 ? 1024 : prec;\
1178 wchar_t ignore[ignore_size]; \
1179 const char *str2 = string; \
1180 const char *strend = string + prec; \
1181 if (strend < string) \
1182 strend = (const char *) UINTPTR_MAX; \
1184 mbstate_t ps; \
1185 memset (&ps, '\0', sizeof (ps)); \
1187 while (str2 != NULL && str2 < strend) \
1188 if (__mbsnrtowcs (ignore, &str2, strend - str2, \
1189 ignore_size, &ps) == (size_t) -1) \
1191 done = -1; \
1192 goto all_done; \
1195 if (str2 == NULL) \
1196 len = strlen (string); \
1197 else \
1198 len = str2 - string - (ps.__count & 7); \
1201 else \
1202 len = strlen (string); \
1204 else \
1206 const wchar_t *s2 = (const wchar_t *) string; \
1207 mbstate_t mbstate; \
1209 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1211 if (prec >= 0) \
1213 /* The string `s2' might not be NUL terminated. */ \
1214 if (__libc_use_alloca (prec)) \
1215 string = (char *) alloca (prec); \
1216 else if ((string = (char *) malloc (prec)) == NULL) \
1218 done = -1; \
1219 goto all_done; \
1221 else \
1222 string_malloced = 1; \
1223 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1225 else \
1227 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1228 if (len != (size_t) -1) \
1230 assert (__mbsinit (&mbstate)); \
1231 s2 = (const wchar_t *) string; \
1232 if (__libc_use_alloca (len + 1)) \
1233 string = (char *) alloca (len + 1); \
1234 else if ((string = (char *) malloc (len + 1)) == NULL) \
1236 done = -1; \
1237 goto all_done; \
1239 else \
1240 string_malloced = 1; \
1241 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1245 if (len == (size_t) -1) \
1247 /* Illegal wide-character string. */ \
1248 done = -1; \
1249 goto all_done; \
1253 if ((width -= len) < 0) \
1255 outstring (string, len); \
1256 break; \
1259 if (!left) \
1260 PAD (' '); \
1261 outstring (string, len); \
1262 if (left) \
1263 PAD (' '); \
1264 if (__builtin_expect (string_malloced, 0)) \
1265 free (string); \
1267 break;
1268 #endif
1270 /* Orient the stream. */
1271 #ifdef ORIENT
1272 ORIENT;
1273 #endif
1275 /* Sanity check of arguments. */
1276 ARGCHECK (s, format);
1278 #ifdef ORIENT
1279 /* Check for correct orientation. */
1280 if (_IO_vtable_offset (s) == 0 &&
1281 _IO_fwide (s, sizeof (CHAR_T) == 1 ? -1 : 1)
1282 != (sizeof (CHAR_T) == 1 ? -1 : 1))
1283 /* The stream is already oriented otherwise. */
1284 return EOF;
1285 #endif
1287 if (UNBUFFERED_P (s))
1288 /* Use a helper function which will allocate a local temporary buffer
1289 for the stream and then call us again. */
1290 return buffered_vfprintf (s, format, ap);
1292 /* Initialize local variables. */
1293 done = 0;
1294 grouping = (const char *) -1;
1295 #ifdef __va_copy
1296 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1297 since on some systems `va_list' is not an integral type. */
1298 __va_copy (ap_save, ap);
1299 #else
1300 ap_save = ap;
1301 #endif
1302 nspecs_done = 0;
1304 #ifdef COMPILE_WPRINTF
1305 /* Find the first format specifier. */
1306 f = lead_str_end = __find_specwc ((const UCHAR_T *) format);
1307 #else
1308 /* Find the first format specifier. */
1309 f = lead_str_end = __find_specmb ((const UCHAR_T *) format);
1310 #endif
1312 /* Lock stream. */
1313 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
1314 _IO_flockfile (s);
1316 /* Write the literal text before the first format. */
1317 outstring ((const UCHAR_T *) format,
1318 lead_str_end - (const UCHAR_T *) format);
1320 /* If we only have to print a simple string, return now. */
1321 if (*f == L_('\0'))
1322 goto all_done;
1324 /* Use the slow path in case any printf handler is registered. */
1325 if (__builtin_expect (__printf_function_table != NULL
1326 || __printf_modifier_table != NULL
1327 || __printf_va_arg_table != NULL, 0))
1328 goto do_positional;
1330 /* Process whole format string. */
1333 #ifdef SHARED
1334 # define REF(Name) &&do_##Name - &&do_form_unknown
1335 #else
1336 # define REF(Name) &&do_##Name
1337 #endif
1338 #define LABEL(Name) do_##Name
1339 STEP0_3_TABLE;
1340 STEP4_TABLE;
1342 union printf_arg *args_value; /* This is not used here but ... */
1343 int is_negative; /* Flag for negative number. */
1344 union
1346 unsigned long long int longlong;
1347 unsigned long int word;
1348 } number;
1349 int base;
1350 union printf_arg the_arg;
1351 CHAR_T *string; /* Pointer to argument string. */
1352 int alt = 0; /* Alternate format. */
1353 int space = 0; /* Use space prefix if no sign is needed. */
1354 int left = 0; /* Left-justify output. */
1355 int showsign = 0; /* Always begin with plus or minus sign. */
1356 int group = 0; /* Print numbers according grouping rules. */
1357 int is_long_double = 0; /* Argument is long double/ long long int. */
1358 int is_short = 0; /* Argument is short int. */
1359 int is_long = 0; /* Argument is long int. */
1360 int is_char = 0; /* Argument is promoted (unsigned) char. */
1361 int width = 0; /* Width of output; 0 means none specified. */
1362 int prec = -1; /* Precision of output; -1 means none specified. */
1363 /* This flag is set by the 'I' modifier and selects the use of the
1364 `outdigits' as determined by the current locale. */
1365 int use_outdigits = 0;
1366 UCHAR_T pad = L_(' ');/* Padding character. */
1367 CHAR_T spec;
1369 workstart = NULL;
1370 workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1372 /* Get current character in format string. */
1373 JUMP (*++f, step0_jumps);
1375 /* ' ' flag. */
1376 LABEL (flag_space):
1377 space = 1;
1378 JUMP (*++f, step0_jumps);
1380 /* '+' flag. */
1381 LABEL (flag_plus):
1382 showsign = 1;
1383 JUMP (*++f, step0_jumps);
1385 /* The '-' flag. */
1386 LABEL (flag_minus):
1387 left = 1;
1388 pad = L_(' ');
1389 JUMP (*++f, step0_jumps);
1391 /* The '#' flag. */
1392 LABEL (flag_hash):
1393 alt = 1;
1394 JUMP (*++f, step0_jumps);
1396 /* The '0' flag. */
1397 LABEL (flag_zero):
1398 if (!left)
1399 pad = L_('0');
1400 JUMP (*++f, step0_jumps);
1402 /* The '\'' flag. */
1403 LABEL (flag_quote):
1404 group = 1;
1406 if (grouping == (const char *) -1)
1408 #ifdef COMPILE_WPRINTF
1409 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1410 _NL_NUMERIC_THOUSANDS_SEP_WC);
1411 #else
1412 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1413 #endif
1415 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1416 if (*grouping == '\0' || *grouping == CHAR_MAX
1417 #ifdef COMPILE_WPRINTF
1418 || thousands_sep == L'\0'
1419 #else
1420 || *thousands_sep == '\0'
1421 #endif
1423 grouping = NULL;
1425 JUMP (*++f, step0_jumps);
1427 LABEL (flag_i18n):
1428 use_outdigits = 1;
1429 JUMP (*++f, step0_jumps);
1431 /* Get width from argument. */
1432 LABEL (width_asterics):
1434 const UCHAR_T *tmp; /* Temporary value. */
1436 tmp = ++f;
1437 if (ISDIGIT (*tmp) && read_int (&tmp) && *tmp == L_('$'))
1438 /* The width comes from a positional parameter. */
1439 goto do_positional;
1441 width = va_arg (ap, int);
1443 /* Negative width means left justified. */
1444 if (width < 0)
1446 width = -width;
1447 pad = L_(' ');
1448 left = 1;
1451 if (__builtin_expect (width >= (size_t) -1 / sizeof (CHAR_T) - 32, 0))
1453 __set_errno (ERANGE);
1454 done = -1;
1455 goto all_done;
1458 if (width >= sizeof (work_buffer) / sizeof (work_buffer[0]) - 32)
1460 /* We have to use a special buffer. The "32" is just a safe
1461 bet for all the output which is not counted in the width. */
1462 size_t needed = ((size_t) width + 32) * sizeof (CHAR_T);
1463 if (__libc_use_alloca (needed))
1464 workend = (CHAR_T *) alloca (needed) + width + 32;
1465 else
1467 workstart = (CHAR_T *) malloc (needed);
1468 if (workstart == NULL)
1470 done = -1;
1471 goto all_done;
1473 workend = workstart + width + 32;
1477 JUMP (*f, step1_jumps);
1479 /* Given width in format string. */
1480 LABEL (width):
1481 width = read_int (&f);
1483 if (__builtin_expect (width >= (size_t) -1 / sizeof (CHAR_T) - 32, 0))
1485 __set_errno (ERANGE);
1486 done = -1;
1487 goto all_done;
1490 if (width >= sizeof (work_buffer) / sizeof (work_buffer[0]) - 32)
1492 /* We have to use a special buffer. The "32" is just a safe
1493 bet for all the output which is not counted in the width. */
1494 size_t needed = ((size_t) width + 32) * sizeof (CHAR_T);
1495 if (__libc_use_alloca (needed))
1496 workend = (CHAR_T *) alloca (needed) + width + 32;
1497 else
1499 workstart = (CHAR_T *) malloc (needed);
1500 if (workstart == NULL)
1502 done = -1;
1503 goto all_done;
1505 workend = workstart + width + 32;
1508 if (*f == L_('$'))
1509 /* Oh, oh. The argument comes from a positional parameter. */
1510 goto do_positional;
1511 JUMP (*f, step1_jumps);
1513 LABEL (precision):
1514 ++f;
1515 if (*f == L_('*'))
1517 const UCHAR_T *tmp; /* Temporary value. */
1519 tmp = ++f;
1520 if (ISDIGIT (*tmp) && read_int (&tmp) > 0 && *tmp == L_('$'))
1521 /* The precision comes from a positional parameter. */
1522 goto do_positional;
1524 prec = va_arg (ap, int);
1526 /* If the precision is negative the precision is omitted. */
1527 if (prec < 0)
1528 prec = -1;
1530 else if (ISDIGIT (*f))
1531 prec = read_int (&f);
1532 else
1533 prec = 0;
1534 if (prec > width
1535 && prec > sizeof (work_buffer) / sizeof (work_buffer[0]) - 32)
1537 if (__builtin_expect (prec >= (size_t) -1 / sizeof (CHAR_T) - 32, 0))
1539 __set_errno (ERANGE);
1540 done = -1;
1541 goto all_done;
1543 size_t needed = ((size_t) prec + 32) * sizeof (CHAR_T);
1545 if (__libc_use_alloca (needed))
1546 workend = (CHAR_T *) alloca (needed) + prec + 32;
1547 else
1549 workstart = (CHAR_T *) malloc (needed);
1550 if (workstart == NULL)
1552 done = -1;
1553 goto all_done;
1555 workend = workstart + prec + 32;
1558 JUMP (*f, step2_jumps);
1560 /* Process 'h' modifier. There might another 'h' following. */
1561 LABEL (mod_half):
1562 is_short = 1;
1563 JUMP (*++f, step3a_jumps);
1565 /* Process 'hh' modifier. */
1566 LABEL (mod_halfhalf):
1567 is_short = 0;
1568 is_char = 1;
1569 JUMP (*++f, step4_jumps);
1571 /* Process 'l' modifier. There might another 'l' following. */
1572 LABEL (mod_long):
1573 is_long = 1;
1574 JUMP (*++f, step3b_jumps);
1576 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1577 allowed to follow. */
1578 LABEL (mod_longlong):
1579 is_long_double = 1;
1580 is_long = 1;
1581 JUMP (*++f, step4_jumps);
1583 LABEL (mod_size_t):
1584 is_long_double = sizeof (size_t) > sizeof (unsigned long int);
1585 is_long = sizeof (size_t) > sizeof (unsigned int);
1586 JUMP (*++f, step4_jumps);
1588 LABEL (mod_ptrdiff_t):
1589 is_long_double = sizeof (ptrdiff_t) > sizeof (unsigned long int);
1590 is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
1591 JUMP (*++f, step4_jumps);
1593 LABEL (mod_intmax_t):
1594 is_long_double = sizeof (intmax_t) > sizeof (unsigned long int);
1595 is_long = sizeof (intmax_t) > sizeof (unsigned int);
1596 JUMP (*++f, step4_jumps);
1598 /* Process current format. */
1599 while (1)
1601 process_arg (((struct printf_spec *) NULL));
1602 process_string_arg (((struct printf_spec *) NULL));
1604 LABEL (form_unknown):
1605 if (spec == L_('\0'))
1607 /* The format string ended before the specifier is complete. */
1608 done = -1;
1609 goto all_done;
1612 /* If we are in the fast loop force entering the complicated
1613 one. */
1614 goto do_positional;
1617 /* The format is correctly handled. */
1618 ++nspecs_done;
1620 if (__builtin_expect (workstart != NULL, 0))
1621 free (workstart);
1622 workstart = NULL;
1624 /* Look for next format specifier. */
1625 #ifdef COMPILE_WPRINTF
1626 f = __find_specwc ((end_of_spec = ++f));
1627 #else
1628 f = __find_specmb ((end_of_spec = ++f));
1629 #endif
1631 /* Write the following constant string. */
1632 outstring (end_of_spec, f - end_of_spec);
1634 while (*f != L_('\0'));
1636 /* Unlock stream and return. */
1637 goto all_done;
1639 /* Here starts the more complex loop to handle positional parameters. */
1640 do_positional:
1642 /* Array with information about the needed arguments. This has to
1643 be dynamically extensible. */
1644 size_t nspecs = 0;
1645 /* A more or less arbitrary start value. */
1646 size_t nspecs_size = 32 * sizeof (struct printf_spec);
1647 struct printf_spec *specs = alloca (nspecs_size);
1649 /* The number of arguments the format string requests. This will
1650 determine the size of the array needed to store the argument
1651 attributes. */
1652 size_t nargs = 0;
1653 size_t bytes_per_arg;
1654 union printf_arg *args_value;
1655 int *args_size;
1656 int *args_type;
1658 /* Positional parameters refer to arguments directly. This could
1659 also determine the maximum number of arguments. Track the
1660 maximum number. */
1661 size_t max_ref_arg = 0;
1663 /* Just a counter. */
1664 size_t cnt;
1666 free (workstart);
1667 workstart = NULL;
1669 if (grouping == (const char *) -1)
1671 #ifdef COMPILE_WPRINTF
1672 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1673 _NL_NUMERIC_THOUSANDS_SEP_WC);
1674 #else
1675 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1676 #endif
1678 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1679 if (*grouping == '\0' || *grouping == CHAR_MAX)
1680 grouping = NULL;
1683 for (f = lead_str_end; *f != L_('\0'); f = specs[nspecs++].next_fmt)
1685 if (nspecs * sizeof (*specs) >= nspecs_size)
1687 /* Extend the array of format specifiers. */
1688 struct printf_spec *old = specs;
1689 specs = extend_alloca (specs, nspecs_size, 2 * nspecs_size);
1691 /* Copy the old array's elements to the new space. */
1692 memmove (specs, old, nspecs * sizeof (*specs));
1695 /* Parse the format specifier. */
1696 #ifdef COMPILE_WPRINTF
1697 nargs += __parse_one_specwc (f, nargs, &specs[nspecs], &max_ref_arg);
1698 #else
1699 nargs += __parse_one_specmb (f, nargs, &specs[nspecs], &max_ref_arg);
1700 #endif
1703 /* Determine the number of arguments the format string consumes. */
1704 nargs = MAX (nargs, max_ref_arg);
1705 /* Calculate total size needed to represent a single argument across
1706 all three argument-related arrays. */
1707 bytes_per_arg = sizeof (*args_value) + sizeof (*args_size)
1708 + sizeof (*args_type);
1710 /* Check for potential integer overflow. */
1711 if (__builtin_expect (nargs > SIZE_MAX / bytes_per_arg, 0))
1713 __set_errno (ERANGE);
1714 done = -1;
1715 goto all_done;
1718 /* Allocate memory for all three argument arrays. */
1719 if (__libc_use_alloca (nargs * bytes_per_arg))
1720 args_value = alloca (nargs * bytes_per_arg);
1721 else
1723 args_value = args_malloced = malloc (nargs * bytes_per_arg);
1724 if (args_value == NULL)
1726 done = -1;
1727 goto all_done;
1731 /* Set up the remaining two arrays to each point past the end of the
1732 prior array, since space for all three has been allocated now. */
1733 args_size = &args_value[nargs].pa_int;
1734 args_type = &args_size[nargs];
1735 memset (args_type, s->_flags2 & _IO_FLAGS2_FORTIFY ? '\xff' : '\0',
1736 nargs * sizeof (*args_type));
1738 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1739 still zero after this loop, format is invalid. For now we
1740 simply use 0 as the value. */
1742 /* Fill in the types of all the arguments. */
1743 for (cnt = 0; cnt < nspecs; ++cnt)
1745 /* If the width is determined by an argument this is an int. */
1746 if (specs[cnt].width_arg != -1)
1747 args_type[specs[cnt].width_arg] = PA_INT;
1749 /* If the precision is determined by an argument this is an int. */
1750 if (specs[cnt].prec_arg != -1)
1751 args_type[specs[cnt].prec_arg] = PA_INT;
1753 switch (specs[cnt].ndata_args)
1755 case 0: /* No arguments. */
1756 break;
1757 case 1: /* One argument; we already have the
1758 type and size. */
1759 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1760 args_size[specs[cnt].data_arg] = specs[cnt].size;
1761 break;
1762 default:
1763 /* We have more than one argument for this format spec.
1764 We must call the arginfo function again to determine
1765 all the types. */
1766 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1767 (&specs[cnt].info,
1768 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg],
1769 &args_size[specs[cnt].data_arg]);
1770 break;
1774 /* Now we know all the types and the order. Fill in the argument
1775 values. */
1776 for (cnt = 0; cnt < nargs; ++cnt)
1777 switch (args_type[cnt])
1779 #define T(tag, mem, type) \
1780 case tag: \
1781 args_value[cnt].mem = va_arg (ap_save, type); \
1782 break
1784 T (PA_WCHAR, pa_wchar, wint_t);
1785 case PA_CHAR: /* Promoted. */
1786 case PA_INT|PA_FLAG_SHORT: /* Promoted. */
1787 #if LONG_MAX == INT_MAX
1788 case PA_INT|PA_FLAG_LONG:
1789 #endif
1790 T (PA_INT, pa_int, int);
1791 #if LONG_MAX == LONG_LONG_MAX
1792 case PA_INT|PA_FLAG_LONG:
1793 #endif
1794 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1795 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1796 # error "he?"
1797 #endif
1798 case PA_FLOAT: /* Promoted. */
1799 T (PA_DOUBLE, pa_double, double);
1800 case PA_DOUBLE|PA_FLAG_LONG_DOUBLE:
1801 if (__ldbl_is_dbl)
1803 args_value[cnt].pa_double = va_arg (ap_save, double);
1804 args_type[cnt] &= ~PA_FLAG_LONG_DOUBLE;
1806 else
1807 args_value[cnt].pa_long_double = va_arg (ap_save, long double);
1808 break;
1809 case PA_STRING: /* All pointers are the same */
1810 case PA_WSTRING: /* All pointers are the same */
1811 T (PA_POINTER, pa_pointer, void *);
1812 #undef T
1813 default:
1814 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1815 args_value[cnt].pa_pointer = va_arg (ap_save, void *);
1816 else if (__builtin_expect (__printf_va_arg_table != NULL, 0)
1817 && __printf_va_arg_table[args_type[cnt] - PA_LAST] != NULL)
1819 args_value[cnt].pa_user = alloca (args_size[cnt]);
1820 (*__printf_va_arg_table[args_type[cnt] - PA_LAST])
1821 (args_value[cnt].pa_user, &ap_save);
1823 else
1824 args_value[cnt].pa_long_double = 0.0;
1825 break;
1826 case -1:
1827 /* Error case. Not all parameters appear in N$ format
1828 strings. We have no way to determine their type. */
1829 assert (s->_flags2 & _IO_FLAGS2_FORTIFY);
1830 __libc_fatal ("*** invalid %N$ use detected ***\n");
1833 /* Now walk through all format specifiers and process them. */
1834 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1836 #undef REF
1837 #ifdef SHARED
1838 # define REF(Name) &&do2_##Name - &&do_form_unknown
1839 #else
1840 # define REF(Name) &&do2_##Name
1841 #endif
1842 #undef LABEL
1843 #define LABEL(Name) do2_##Name
1844 STEP4_TABLE;
1846 int is_negative;
1847 union
1849 unsigned long long int longlong;
1850 unsigned long int word;
1851 } number;
1852 int base;
1853 union printf_arg the_arg;
1854 CHAR_T *string; /* Pointer to argument string. */
1856 /* Fill variables from values in struct. */
1857 int alt = specs[nspecs_done].info.alt;
1858 int space = specs[nspecs_done].info.space;
1859 int left = specs[nspecs_done].info.left;
1860 int showsign = specs[nspecs_done].info.showsign;
1861 int group = specs[nspecs_done].info.group;
1862 int is_long_double = specs[nspecs_done].info.is_long_double;
1863 int is_short = specs[nspecs_done].info.is_short;
1864 int is_char = specs[nspecs_done].info.is_char;
1865 int is_long = specs[nspecs_done].info.is_long;
1866 int width = specs[nspecs_done].info.width;
1867 int prec = specs[nspecs_done].info.prec;
1868 int use_outdigits = specs[nspecs_done].info.i18n;
1869 char pad = specs[nspecs_done].info.pad;
1870 CHAR_T spec = specs[nspecs_done].info.spec;
1872 workstart = NULL;
1873 workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1875 /* Fill in last information. */
1876 if (specs[nspecs_done].width_arg != -1)
1878 /* Extract the field width from an argument. */
1879 specs[nspecs_done].info.width =
1880 args_value[specs[nspecs_done].width_arg].pa_int;
1882 if (specs[nspecs_done].info.width < 0)
1883 /* If the width value is negative left justification is
1884 selected and the value is taken as being positive. */
1886 specs[nspecs_done].info.width *= -1;
1887 left = specs[nspecs_done].info.left = 1;
1889 width = specs[nspecs_done].info.width;
1892 if (specs[nspecs_done].prec_arg != -1)
1894 /* Extract the precision from an argument. */
1895 specs[nspecs_done].info.prec =
1896 args_value[specs[nspecs_done].prec_arg].pa_int;
1898 if (specs[nspecs_done].info.prec < 0)
1899 /* If the precision is negative the precision is
1900 omitted. */
1901 specs[nspecs_done].info.prec = -1;
1903 prec = specs[nspecs_done].info.prec;
1906 /* Maybe the buffer is too small. */
1907 if (MAX (prec, width) + 32 > (int) (sizeof (work_buffer)
1908 / sizeof (CHAR_T)))
1910 if (__libc_use_alloca ((MAX (prec, width) + 32)
1911 * sizeof (CHAR_T)))
1912 workend = ((CHAR_T *) alloca ((MAX (prec, width) + 32)
1913 * sizeof (CHAR_T))
1914 + (MAX (prec, width) + 32));
1915 else
1917 workstart = (CHAR_T *) malloc ((MAX (prec, width) + 32)
1918 * sizeof (CHAR_T));
1919 workend = workstart + (MAX (prec, width) + 32);
1923 /* Process format specifiers. */
1924 while (1)
1926 extern printf_function **__printf_function_table;
1927 int function_done;
1929 if (spec <= UCHAR_MAX
1930 && __printf_function_table != NULL
1931 && __printf_function_table[(size_t) spec] != NULL)
1933 const void **ptr = alloca (specs[nspecs_done].ndata_args
1934 * sizeof (const void *));
1936 /* Fill in an array of pointers to the argument values. */
1937 for (unsigned int i = 0; i < specs[nspecs_done].ndata_args;
1938 ++i)
1939 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1941 /* Call the function. */
1942 function_done = __printf_function_table[(size_t) spec]
1943 (s, &specs[nspecs_done].info, ptr);
1945 if (function_done != -2)
1947 /* If an error occurred we don't have information
1948 about # of chars. */
1949 if (function_done < 0)
1951 done = -1;
1952 goto all_done;
1955 done_add (function_done);
1956 break;
1960 JUMP (spec, step4_jumps);
1962 process_arg ((&specs[nspecs_done]));
1963 process_string_arg ((&specs[nspecs_done]));
1965 LABEL (form_unknown):
1967 unsigned int i;
1968 const void **ptr;
1970 ptr = alloca (specs[nspecs_done].ndata_args
1971 * sizeof (const void *));
1973 /* Fill in an array of pointers to the argument values. */
1974 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
1975 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1977 /* Call the function. */
1978 function_done = printf_unknown (s, &specs[nspecs_done].info,
1979 ptr);
1981 /* If an error occurred we don't have information about #
1982 of chars. */
1983 if (function_done < 0)
1985 done = -1;
1986 goto all_done;
1989 done_add (function_done);
1991 break;
1994 free (workstart);
1995 workstart = NULL;
1997 /* Write the following constant string. */
1998 outstring (specs[nspecs_done].end_of_fmt,
1999 specs[nspecs_done].next_fmt
2000 - specs[nspecs_done].end_of_fmt);
2004 all_done:
2005 free (args_malloced);
2006 free (workstart);
2007 /* Unlock the stream. */
2008 _IO_funlockfile (s);
2009 _IO_cleanup_region_end (0);
2011 return done;
2014 /* Handle an unknown format specifier. This prints out a canonicalized
2015 representation of the format spec itself. */
2016 static int
2017 printf_unknown (FILE *s, const struct printf_info *info,
2018 const void *const *args)
2021 int done = 0;
2022 CHAR_T work_buffer[MAX (sizeof (info->width), sizeof (info->prec)) * 3];
2023 CHAR_T *const workend
2024 = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
2025 register CHAR_T *w;
2027 outchar (L_('%'));
2029 if (info->alt)
2030 outchar (L_('#'));
2031 if (info->group)
2032 outchar (L_('\''));
2033 if (info->showsign)
2034 outchar (L_('+'));
2035 else if (info->space)
2036 outchar (L_(' '));
2037 if (info->left)
2038 outchar (L_('-'));
2039 if (info->pad == L_('0'))
2040 outchar (L_('0'));
2041 if (info->i18n)
2042 outchar (L_('I'));
2044 if (info->width != 0)
2046 w = _itoa_word (info->width, workend, 10, 0);
2047 while (w < workend)
2048 outchar (*w++);
2051 if (info->prec != -1)
2053 outchar (L_('.'));
2054 w = _itoa_word (info->prec, workend, 10, 0);
2055 while (w < workend)
2056 outchar (*w++);
2059 if (info->spec != L_('\0'))
2060 outchar (info->spec);
2062 all_done:
2063 return done;
2066 /* Group the digits according to the grouping rules of the current locale.
2067 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
2068 static CHAR_T *
2069 internal_function
2070 group_number (CHAR_T *w, CHAR_T *rear_ptr, const char *grouping,
2071 #ifdef COMPILE_WPRINTF
2072 wchar_t thousands_sep
2073 #else
2074 const char *thousands_sep
2075 #endif
2078 int len;
2079 CHAR_T *src, *s;
2080 #ifndef COMPILE_WPRINTF
2081 int tlen = strlen (thousands_sep);
2082 #endif
2084 /* We treat all negative values like CHAR_MAX. */
2086 if (*grouping == CHAR_MAX || *grouping <= 0)
2087 /* No grouping should be done. */
2088 return w;
2090 len = *grouping++;
2092 /* Copy existing string so that nothing gets overwritten. */
2093 src = (CHAR_T *) alloca ((rear_ptr - w) * sizeof (CHAR_T));
2094 s = (CHAR_T *) __mempcpy (src, w,
2095 (rear_ptr - w) * sizeof (CHAR_T));
2096 w = rear_ptr;
2098 /* Process all characters in the string. */
2099 while (s > src)
2101 *--w = *--s;
2103 if (--len == 0 && s > src)
2105 /* A new group begins. */
2106 #ifdef COMPILE_WPRINTF
2107 *--w = thousands_sep;
2108 #else
2109 int cnt = tlen;
2111 *--w = thousands_sep[--cnt];
2112 while (cnt > 0);
2113 #endif
2115 if (*grouping == CHAR_MAX
2116 #if CHAR_MIN < 0
2117 || *grouping < 0
2118 #endif
2121 /* No further grouping to be done.
2122 Copy the rest of the number. */
2124 *--w = *--s;
2125 while (s > src);
2126 break;
2128 else if (*grouping != '\0')
2129 /* The previous grouping repeats ad infinitum. */
2130 len = *grouping++;
2131 else
2132 len = grouping[-1];
2135 return w;
2138 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2139 struct helper_file
2141 struct _IO_FILE_plus _f;
2142 #ifdef COMPILE_WPRINTF
2143 struct _IO_wide_data _wide_data;
2144 #endif
2145 _IO_FILE *_put_stream;
2146 #ifdef _IO_MTSAFE_IO
2147 _IO_lock_t lock;
2148 #endif
2151 static int
2152 _IO_helper_overflow (_IO_FILE *s, int c)
2154 _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
2155 #ifdef COMPILE_WPRINTF
2156 int used = s->_wide_data->_IO_write_ptr - s->_wide_data->_IO_write_base;
2157 if (used)
2159 _IO_size_t written = _IO_sputn (target, s->_wide_data->_IO_write_base,
2160 used);
2161 if (written == 0 || written == WEOF)
2162 return WEOF;
2163 __wmemmove (s->_wide_data->_IO_write_base,
2164 s->_wide_data->_IO_write_base + written,
2165 used - written);
2166 s->_wide_data->_IO_write_ptr -= written;
2168 #else
2169 int used = s->_IO_write_ptr - s->_IO_write_base;
2170 if (used)
2172 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
2173 if (written == 0 || written == EOF)
2174 return EOF;
2175 memmove (s->_IO_write_base, s->_IO_write_base + written,
2176 used - written);
2177 s->_IO_write_ptr -= written;
2179 #endif
2180 return PUTC (c, s);
2183 #ifdef COMPILE_WPRINTF
2184 static const struct _IO_jump_t _IO_helper_jumps =
2186 JUMP_INIT_DUMMY,
2187 JUMP_INIT (finish, INTUSE(_IO_wdefault_finish)),
2188 JUMP_INIT (overflow, _IO_helper_overflow),
2189 JUMP_INIT (underflow, _IO_default_underflow),
2190 JUMP_INIT (uflow, INTUSE(_IO_default_uflow)),
2191 JUMP_INIT (pbackfail, (_IO_pbackfail_t) INTUSE(_IO_wdefault_pbackfail)),
2192 JUMP_INIT (xsputn, INTUSE(_IO_wdefault_xsputn)),
2193 JUMP_INIT (xsgetn, INTUSE(_IO_wdefault_xsgetn)),
2194 JUMP_INIT (seekoff, _IO_default_seekoff),
2195 JUMP_INIT (seekpos, _IO_default_seekpos),
2196 JUMP_INIT (setbuf, _IO_default_setbuf),
2197 JUMP_INIT (sync, _IO_default_sync),
2198 JUMP_INIT (doallocate, INTUSE(_IO_wdefault_doallocate)),
2199 JUMP_INIT (read, _IO_default_read),
2200 JUMP_INIT (write, _IO_default_write),
2201 JUMP_INIT (seek, _IO_default_seek),
2202 JUMP_INIT (close, _IO_default_close),
2203 JUMP_INIT (stat, _IO_default_stat)
2205 #else
2206 static const struct _IO_jump_t _IO_helper_jumps =
2208 JUMP_INIT_DUMMY,
2209 JUMP_INIT (finish, INTUSE(_IO_default_finish)),
2210 JUMP_INIT (overflow, _IO_helper_overflow),
2211 JUMP_INIT (underflow, _IO_default_underflow),
2212 JUMP_INIT (uflow, INTUSE(_IO_default_uflow)),
2213 JUMP_INIT (pbackfail, INTUSE(_IO_default_pbackfail)),
2214 JUMP_INIT (xsputn, INTUSE(_IO_default_xsputn)),
2215 JUMP_INIT (xsgetn, INTUSE(_IO_default_xsgetn)),
2216 JUMP_INIT (seekoff, _IO_default_seekoff),
2217 JUMP_INIT (seekpos, _IO_default_seekpos),
2218 JUMP_INIT (setbuf, _IO_default_setbuf),
2219 JUMP_INIT (sync, _IO_default_sync),
2220 JUMP_INIT (doallocate, INTUSE(_IO_default_doallocate)),
2221 JUMP_INIT (read, _IO_default_read),
2222 JUMP_INIT (write, _IO_default_write),
2223 JUMP_INIT (seek, _IO_default_seek),
2224 JUMP_INIT (close, _IO_default_close),
2225 JUMP_INIT (stat, _IO_default_stat)
2227 #endif
2229 static int
2230 internal_function
2231 buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format,
2232 _IO_va_list args)
2234 CHAR_T buf[_IO_BUFSIZ];
2235 struct helper_file helper;
2236 register _IO_FILE *hp = (_IO_FILE *) &helper._f;
2237 int result, to_flush;
2239 /* Orient the stream. */
2240 #ifdef ORIENT
2241 ORIENT;
2242 #endif
2244 /* Initialize helper. */
2245 helper._put_stream = s;
2246 #ifdef COMPILE_WPRINTF
2247 hp->_wide_data = &helper._wide_data;
2248 _IO_wsetp (hp, buf, buf + sizeof buf / sizeof (CHAR_T));
2249 hp->_mode = 1;
2250 #else
2251 _IO_setp (hp, buf, buf + sizeof buf);
2252 hp->_mode = -1;
2253 #endif
2254 hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS|_IO_USER_LOCK;
2255 #if _IO_JUMPS_OFFSET
2256 hp->_vtable_offset = 0;
2257 #endif
2258 #ifdef _IO_MTSAFE_IO
2259 hp->_lock = NULL;
2260 #endif
2261 hp->_flags2 = s->_flags2;
2262 _IO_JUMPS (&helper._f) = (struct _IO_jump_t *) &_IO_helper_jumps;
2264 /* Now print to helper instead. */
2265 #ifndef COMPILE_WPRINTF
2266 result = INTUSE(_IO_vfprintf) (hp, format, args);
2267 #else
2268 result = vfprintf (hp, format, args);
2269 #endif
2271 /* Lock stream. */
2272 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, s);
2273 _IO_flockfile (s);
2275 /* Now flush anything from the helper to the S. */
2276 #ifdef COMPILE_WPRINTF
2277 if ((to_flush = (hp->_wide_data->_IO_write_ptr
2278 - hp->_wide_data->_IO_write_base)) > 0)
2280 if ((int) _IO_sputn (s, hp->_wide_data->_IO_write_base, to_flush)
2281 != to_flush)
2282 result = -1;
2284 #else
2285 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
2287 if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
2288 result = -1;
2290 #endif
2292 /* Unlock the stream. */
2293 _IO_funlockfile (s);
2294 __libc_cleanup_region_end (0);
2296 return result;
2299 #undef vfprintf
2300 #ifdef COMPILE_WPRINTF
2301 strong_alias (_IO_vfwprintf, __vfwprintf);
2302 ldbl_weak_alias (_IO_vfwprintf, vfwprintf);
2303 #else
2304 ldbl_strong_alias (_IO_vfprintf_internal, vfprintf);
2305 ldbl_hidden_def (_IO_vfprintf_internal, vfprintf)
2306 ldbl_strong_alias (_IO_vfprintf_internal, _IO_vfprintf);
2307 #endif