Properly handle fencepost with MALLOC_ALIGN_MASK
[glibc.git] / stdio-common / vfprintf.c
blobd5690342536bc8cf948c786f663bb63f73f91f3a
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) \
1172 /* Search for the end of the string, but don't search past \
1173 the length (in bytes) specified by the precision. Also \
1174 don't use incomplete characters. */ \
1175 if (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MB_CUR_MAX) == 1) \
1176 len = __strnlen (string, prec); \
1177 else \
1179 /* In case we have a multibyte character set the \
1180 situation is more complicated. We must not copy \
1181 bytes at the end which form an incomplete character. */\
1182 size_t ignore_size = (unsigned) prec > 1024 ? 1024 : prec;\
1183 wchar_t ignore[ignore_size]; \
1184 const char *str2 = string; \
1185 const char *strend = string + prec; \
1186 if (strend < string) \
1187 strend = (const char *) UINTPTR_MAX; \
1189 mbstate_t ps; \
1190 memset (&ps, '\0', sizeof (ps)); \
1192 while (str2 != NULL && str2 < strend) \
1193 if (__mbsnrtowcs (ignore, &str2, strend - str2, \
1194 ignore_size, &ps) == (size_t) -1) \
1196 /* Conversion function has set errno. */ \
1197 done = -1; \
1198 goto all_done; \
1201 if (str2 == NULL) \
1202 len = strlen (string); \
1203 else \
1204 len = str2 - string - (ps.__count & 7); \
1207 else \
1208 len = strlen (string); \
1210 else \
1212 const wchar_t *s2 = (const wchar_t *) string; \
1213 mbstate_t mbstate; \
1215 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1217 if (prec >= 0) \
1219 /* The string `s2' might not be NUL terminated. */ \
1220 if (__libc_use_alloca (prec)) \
1221 string = (char *) alloca (prec); \
1222 else if ((string = (char *) malloc (prec)) == NULL) \
1224 done = -1; \
1225 goto all_done; \
1227 else \
1228 string_malloced = 1; \
1229 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1231 else \
1233 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1234 if (len != (size_t) -1) \
1236 assert (__mbsinit (&mbstate)); \
1237 s2 = (const wchar_t *) string; \
1238 if (__libc_use_alloca (len + 1)) \
1239 string = (char *) alloca (len + 1); \
1240 else if ((string = (char *) malloc (len + 1)) == NULL) \
1242 done = -1; \
1243 goto all_done; \
1245 else \
1246 string_malloced = 1; \
1247 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1251 if (len == (size_t) -1) \
1253 /* Illegal wide-character string. */ \
1254 done = -1; \
1255 goto all_done; \
1259 if ((width -= len) < 0) \
1261 outstring (string, len); \
1262 break; \
1265 if (!left) \
1266 PAD (' '); \
1267 outstring (string, len); \
1268 if (left) \
1269 PAD (' '); \
1270 if (__builtin_expect (string_malloced, 0)) \
1271 free (string); \
1273 break;
1274 #endif
1276 /* Orient the stream. */
1277 #ifdef ORIENT
1278 ORIENT;
1279 #endif
1281 /* Sanity check of arguments. */
1282 ARGCHECK (s, format);
1284 #ifdef ORIENT
1285 /* Check for correct orientation. */
1286 if (_IO_vtable_offset (s) == 0 &&
1287 _IO_fwide (s, sizeof (CHAR_T) == 1 ? -1 : 1)
1288 != (sizeof (CHAR_T) == 1 ? -1 : 1))
1289 /* The stream is already oriented otherwise. */
1290 return EOF;
1291 #endif
1293 if (UNBUFFERED_P (s))
1294 /* Use a helper function which will allocate a local temporary buffer
1295 for the stream and then call us again. */
1296 return buffered_vfprintf (s, format, ap);
1298 /* Initialize local variables. */
1299 done = 0;
1300 grouping = (const char *) -1;
1301 #ifdef __va_copy
1302 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1303 since on some systems `va_list' is not an integral type. */
1304 __va_copy (ap_save, ap);
1305 #else
1306 ap_save = ap;
1307 #endif
1308 nspecs_done = 0;
1310 #ifdef COMPILE_WPRINTF
1311 /* Find the first format specifier. */
1312 f = lead_str_end = __find_specwc ((const UCHAR_T *) format);
1313 #else
1314 /* Find the first format specifier. */
1315 f = lead_str_end = __find_specmb ((const UCHAR_T *) format);
1316 #endif
1318 /* Lock stream. */
1319 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
1320 _IO_flockfile (s);
1322 /* Write the literal text before the first format. */
1323 outstring ((const UCHAR_T *) format,
1324 lead_str_end - (const UCHAR_T *) format);
1326 /* If we only have to print a simple string, return now. */
1327 if (*f == L_('\0'))
1328 goto all_done;
1330 /* Use the slow path in case any printf handler is registered. */
1331 if (__builtin_expect (__printf_function_table != NULL
1332 || __printf_modifier_table != NULL
1333 || __printf_va_arg_table != NULL, 0))
1334 goto do_positional;
1336 /* Process whole format string. */
1339 #ifdef SHARED
1340 # define REF(Name) &&do_##Name - &&do_form_unknown
1341 #else
1342 # define REF(Name) &&do_##Name
1343 #endif
1344 #define LABEL(Name) do_##Name
1345 STEP0_3_TABLE;
1346 STEP4_TABLE;
1348 union printf_arg *args_value; /* This is not used here but ... */
1349 int is_negative; /* Flag for negative number. */
1350 union
1352 unsigned long long int longlong;
1353 unsigned long int word;
1354 } number;
1355 int base;
1356 union printf_arg the_arg;
1357 CHAR_T *string; /* Pointer to argument string. */
1358 int alt = 0; /* Alternate format. */
1359 int space = 0; /* Use space prefix if no sign is needed. */
1360 int left = 0; /* Left-justify output. */
1361 int showsign = 0; /* Always begin with plus or minus sign. */
1362 int group = 0; /* Print numbers according grouping rules. */
1363 int is_long_double = 0; /* Argument is long double/ long long int. */
1364 int is_short = 0; /* Argument is short int. */
1365 int is_long = 0; /* Argument is long int. */
1366 int is_char = 0; /* Argument is promoted (unsigned) char. */
1367 int width = 0; /* Width of output; 0 means none specified. */
1368 int prec = -1; /* Precision of output; -1 means none specified. */
1369 /* This flag is set by the 'I' modifier and selects the use of the
1370 `outdigits' as determined by the current locale. */
1371 int use_outdigits = 0;
1372 UCHAR_T pad = L_(' ');/* Padding character. */
1373 CHAR_T spec;
1375 workstart = NULL;
1376 workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1378 /* Get current character in format string. */
1379 JUMP (*++f, step0_jumps);
1381 /* ' ' flag. */
1382 LABEL (flag_space):
1383 space = 1;
1384 JUMP (*++f, step0_jumps);
1386 /* '+' flag. */
1387 LABEL (flag_plus):
1388 showsign = 1;
1389 JUMP (*++f, step0_jumps);
1391 /* The '-' flag. */
1392 LABEL (flag_minus):
1393 left = 1;
1394 pad = L_(' ');
1395 JUMP (*++f, step0_jumps);
1397 /* The '#' flag. */
1398 LABEL (flag_hash):
1399 alt = 1;
1400 JUMP (*++f, step0_jumps);
1402 /* The '0' flag. */
1403 LABEL (flag_zero):
1404 if (!left)
1405 pad = L_('0');
1406 JUMP (*++f, step0_jumps);
1408 /* The '\'' flag. */
1409 LABEL (flag_quote):
1410 group = 1;
1412 if (grouping == (const char *) -1)
1414 #ifdef COMPILE_WPRINTF
1415 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1416 _NL_NUMERIC_THOUSANDS_SEP_WC);
1417 #else
1418 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1419 #endif
1421 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1422 if (*grouping == '\0' || *grouping == CHAR_MAX
1423 #ifdef COMPILE_WPRINTF
1424 || thousands_sep == L'\0'
1425 #else
1426 || *thousands_sep == '\0'
1427 #endif
1429 grouping = NULL;
1431 JUMP (*++f, step0_jumps);
1433 LABEL (flag_i18n):
1434 use_outdigits = 1;
1435 JUMP (*++f, step0_jumps);
1437 /* Get width from argument. */
1438 LABEL (width_asterics):
1440 const UCHAR_T *tmp; /* Temporary value. */
1442 tmp = ++f;
1443 if (ISDIGIT (*tmp))
1445 int pos = read_int (&tmp);
1447 if (pos == -1)
1449 __set_errno (EOVERFLOW);
1450 done = -1;
1451 goto all_done;
1454 if (pos && *tmp == L_('$'))
1455 /* The width comes from a positional parameter. */
1456 goto do_positional;
1458 width = va_arg (ap, int);
1460 /* Negative width means left justified. */
1461 if (width < 0)
1463 width = -width;
1464 pad = L_(' ');
1465 left = 1;
1468 if (__builtin_expect (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;
1494 JUMP (*f, step1_jumps);
1496 /* Given width in format string. */
1497 LABEL (width):
1498 width = read_int (&f);
1500 if (__builtin_expect (width == -1
1501 || width >= INT_MAX / sizeof (CHAR_T) - 32, 0))
1503 __set_errno (EOVERFLOW);
1504 done = -1;
1505 goto all_done;
1508 if (width >= sizeof (work_buffer) / sizeof (work_buffer[0]) - 32)
1510 /* We have to use a special buffer. The "32" is just a safe
1511 bet for all the output which is not counted in the width. */
1512 size_t needed = ((size_t) width + 32) * sizeof (CHAR_T);
1513 if (__libc_use_alloca (needed))
1514 workend = (CHAR_T *) alloca (needed) + width + 32;
1515 else
1517 workstart = (CHAR_T *) malloc (needed);
1518 if (workstart == NULL)
1520 done = -1;
1521 goto all_done;
1523 workend = workstart + width + 32;
1526 if (*f == L_('$'))
1527 /* Oh, oh. The argument comes from a positional parameter. */
1528 goto do_positional;
1529 JUMP (*f, step1_jumps);
1531 LABEL (precision):
1532 ++f;
1533 if (*f == L_('*'))
1535 const UCHAR_T *tmp; /* Temporary value. */
1537 tmp = ++f;
1538 if (ISDIGIT (*tmp))
1540 int pos = read_int (&tmp);
1542 if (pos == -1)
1544 __set_errno (EOVERFLOW);
1545 done = -1;
1546 goto all_done;
1549 if (pos && *tmp == L_('$'))
1550 /* The precision comes from a positional parameter. */
1551 goto do_positional;
1553 prec = va_arg (ap, int);
1555 /* If the precision is negative the precision is omitted. */
1556 if (prec < 0)
1557 prec = -1;
1559 else if (ISDIGIT (*f))
1561 prec = read_int (&f);
1563 /* The precision was specified in this case as an extremely
1564 large positive value. */
1565 if (prec == -1)
1567 __set_errno (EOVERFLOW);
1568 done = -1;
1569 goto all_done;
1572 else
1573 prec = 0;
1574 if (prec > width
1575 && prec > sizeof (work_buffer) / sizeof (work_buffer[0]) - 32)
1577 if (__builtin_expect (prec >= INT_MAX / sizeof (CHAR_T) - 32, 0))
1579 __set_errno (EOVERFLOW);
1580 done = -1;
1581 goto all_done;
1583 size_t needed = ((size_t) prec + 32) * sizeof (CHAR_T);
1585 if (__libc_use_alloca (needed))
1586 workend = (CHAR_T *) alloca (needed) + prec + 32;
1587 else
1589 workstart = (CHAR_T *) malloc (needed);
1590 if (workstart == NULL)
1592 done = -1;
1593 goto all_done;
1595 workend = workstart + prec + 32;
1598 JUMP (*f, step2_jumps);
1600 /* Process 'h' modifier. There might another 'h' following. */
1601 LABEL (mod_half):
1602 is_short = 1;
1603 JUMP (*++f, step3a_jumps);
1605 /* Process 'hh' modifier. */
1606 LABEL (mod_halfhalf):
1607 is_short = 0;
1608 is_char = 1;
1609 JUMP (*++f, step4_jumps);
1611 /* Process 'l' modifier. There might another 'l' following. */
1612 LABEL (mod_long):
1613 is_long = 1;
1614 JUMP (*++f, step3b_jumps);
1616 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1617 allowed to follow. */
1618 LABEL (mod_longlong):
1619 is_long_double = 1;
1620 is_long = 1;
1621 JUMP (*++f, step4_jumps);
1623 LABEL (mod_size_t):
1624 is_long_double = sizeof (size_t) > sizeof (unsigned long int);
1625 is_long = sizeof (size_t) > sizeof (unsigned int);
1626 JUMP (*++f, step4_jumps);
1628 LABEL (mod_ptrdiff_t):
1629 is_long_double = sizeof (ptrdiff_t) > sizeof (unsigned long int);
1630 is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
1631 JUMP (*++f, step4_jumps);
1633 LABEL (mod_intmax_t):
1634 is_long_double = sizeof (intmax_t) > sizeof (unsigned long int);
1635 is_long = sizeof (intmax_t) > sizeof (unsigned int);
1636 JUMP (*++f, step4_jumps);
1638 /* Process current format. */
1639 while (1)
1641 process_arg (((struct printf_spec *) NULL));
1642 process_string_arg (((struct printf_spec *) NULL));
1644 LABEL (form_unknown):
1645 if (spec == L_('\0'))
1647 /* The format string ended before the specifier is complete. */
1648 __set_errno (EINVAL);
1649 done = -1;
1650 goto all_done;
1653 /* If we are in the fast loop force entering the complicated
1654 one. */
1655 goto do_positional;
1658 /* The format is correctly handled. */
1659 ++nspecs_done;
1661 if (__builtin_expect (workstart != NULL, 0))
1662 free (workstart);
1663 workstart = NULL;
1665 /* Look for next format specifier. */
1666 #ifdef COMPILE_WPRINTF
1667 f = __find_specwc ((end_of_spec = ++f));
1668 #else
1669 f = __find_specmb ((end_of_spec = ++f));
1670 #endif
1672 /* Write the following constant string. */
1673 outstring (end_of_spec, f - end_of_spec);
1675 while (*f != L_('\0'));
1677 /* Unlock stream and return. */
1678 goto all_done;
1680 /* Here starts the more complex loop to handle positional parameters. */
1681 do_positional:
1683 /* Array with information about the needed arguments. This has to
1684 be dynamically extensible. */
1685 size_t nspecs = 0;
1686 /* A more or less arbitrary start value. */
1687 size_t nspecs_size = 32 * sizeof (struct printf_spec);
1688 struct printf_spec *specs = alloca (nspecs_size);
1690 /* The number of arguments the format string requests. This will
1691 determine the size of the array needed to store the argument
1692 attributes. */
1693 size_t nargs = 0;
1694 size_t bytes_per_arg;
1695 union printf_arg *args_value;
1696 int *args_size;
1697 int *args_type;
1699 /* Positional parameters refer to arguments directly. This could
1700 also determine the maximum number of arguments. Track the
1701 maximum number. */
1702 size_t max_ref_arg = 0;
1704 /* Just a counter. */
1705 size_t cnt;
1707 free (workstart);
1708 workstart = NULL;
1710 if (grouping == (const char *) -1)
1712 #ifdef COMPILE_WPRINTF
1713 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1714 _NL_NUMERIC_THOUSANDS_SEP_WC);
1715 #else
1716 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1717 #endif
1719 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1720 if (*grouping == '\0' || *grouping == CHAR_MAX)
1721 grouping = NULL;
1724 for (f = lead_str_end; *f != L_('\0'); f = specs[nspecs++].next_fmt)
1726 if (nspecs * sizeof (*specs) >= nspecs_size)
1728 /* Extend the array of format specifiers. */
1729 struct printf_spec *old = specs;
1730 specs = extend_alloca (specs, nspecs_size, 2 * nspecs_size);
1732 /* Copy the old array's elements to the new space. */
1733 memmove (specs, old, nspecs * sizeof (*specs));
1736 /* Parse the format specifier. */
1737 #ifdef COMPILE_WPRINTF
1738 nargs += __parse_one_specwc (f, nargs, &specs[nspecs], &max_ref_arg);
1739 #else
1740 nargs += __parse_one_specmb (f, nargs, &specs[nspecs], &max_ref_arg);
1741 #endif
1744 /* Determine the number of arguments the format string consumes. */
1745 nargs = MAX (nargs, max_ref_arg);
1746 /* Calculate total size needed to represent a single argument across
1747 all three argument-related arrays. */
1748 bytes_per_arg = (sizeof (*args_value) + sizeof (*args_size)
1749 + sizeof (*args_type));
1751 /* Check for potential integer overflow. */
1752 if (__builtin_expect (nargs > INT_MAX / bytes_per_arg, 0))
1754 __set_errno (EOVERFLOW);
1755 done = -1;
1756 goto all_done;
1759 /* Allocate memory for all three argument arrays. */
1760 if (__libc_use_alloca (nargs * bytes_per_arg))
1761 args_value = alloca (nargs * bytes_per_arg);
1762 else
1764 args_value = args_malloced = malloc (nargs * bytes_per_arg);
1765 if (args_value == NULL)
1767 done = -1;
1768 goto all_done;
1772 /* Set up the remaining two arrays to each point past the end of the
1773 prior array, since space for all three has been allocated now. */
1774 args_size = &args_value[nargs].pa_int;
1775 args_type = &args_size[nargs];
1776 memset (args_type, s->_flags2 & _IO_FLAGS2_FORTIFY ? '\xff' : '\0',
1777 nargs * sizeof (*args_type));
1779 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1780 still zero after this loop, format is invalid. For now we
1781 simply use 0 as the value. */
1783 /* Fill in the types of all the arguments. */
1784 for (cnt = 0; cnt < nspecs; ++cnt)
1786 /* If the width is determined by an argument this is an int. */
1787 if (specs[cnt].width_arg != -1)
1788 args_type[specs[cnt].width_arg] = PA_INT;
1790 /* If the precision is determined by an argument this is an int. */
1791 if (specs[cnt].prec_arg != -1)
1792 args_type[specs[cnt].prec_arg] = PA_INT;
1794 switch (specs[cnt].ndata_args)
1796 case 0: /* No arguments. */
1797 break;
1798 case 1: /* One argument; we already have the
1799 type and size. */
1800 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1801 args_size[specs[cnt].data_arg] = specs[cnt].size;
1802 break;
1803 default:
1804 /* We have more than one argument for this format spec.
1805 We must call the arginfo function again to determine
1806 all the types. */
1807 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1808 (&specs[cnt].info,
1809 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg],
1810 &args_size[specs[cnt].data_arg]);
1811 break;
1815 /* Now we know all the types and the order. Fill in the argument
1816 values. */
1817 for (cnt = 0; cnt < nargs; ++cnt)
1818 switch (args_type[cnt])
1820 #define T(tag, mem, type) \
1821 case tag: \
1822 args_value[cnt].mem = va_arg (ap_save, type); \
1823 break
1825 T (PA_WCHAR, pa_wchar, wint_t);
1826 case PA_CHAR: /* Promoted. */
1827 case PA_INT|PA_FLAG_SHORT: /* Promoted. */
1828 #if LONG_MAX == INT_MAX
1829 case PA_INT|PA_FLAG_LONG:
1830 #endif
1831 T (PA_INT, pa_int, int);
1832 #if LONG_MAX == LONG_LONG_MAX
1833 case PA_INT|PA_FLAG_LONG:
1834 #endif
1835 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1836 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1837 # error "he?"
1838 #endif
1839 case PA_FLOAT: /* Promoted. */
1840 T (PA_DOUBLE, pa_double, double);
1841 case PA_DOUBLE|PA_FLAG_LONG_DOUBLE:
1842 if (__ldbl_is_dbl)
1844 args_value[cnt].pa_double = va_arg (ap_save, double);
1845 args_type[cnt] &= ~PA_FLAG_LONG_DOUBLE;
1847 else
1848 args_value[cnt].pa_long_double = va_arg (ap_save, long double);
1849 break;
1850 case PA_STRING: /* All pointers are the same */
1851 case PA_WSTRING: /* All pointers are the same */
1852 T (PA_POINTER, pa_pointer, void *);
1853 #undef T
1854 default:
1855 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1856 args_value[cnt].pa_pointer = va_arg (ap_save, void *);
1857 else if (__builtin_expect (__printf_va_arg_table != NULL, 0)
1858 && __printf_va_arg_table[args_type[cnt] - PA_LAST] != NULL)
1860 args_value[cnt].pa_user = alloca (args_size[cnt]);
1861 (*__printf_va_arg_table[args_type[cnt] - PA_LAST])
1862 (args_value[cnt].pa_user, &ap_save);
1864 else
1865 args_value[cnt].pa_long_double = 0.0;
1866 break;
1867 case -1:
1868 /* Error case. Not all parameters appear in N$ format
1869 strings. We have no way to determine their type. */
1870 assert (s->_flags2 & _IO_FLAGS2_FORTIFY);
1871 __libc_fatal ("*** invalid %N$ use detected ***\n");
1874 /* Now walk through all format specifiers and process them. */
1875 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1877 #undef REF
1878 #ifdef SHARED
1879 # define REF(Name) &&do2_##Name - &&do_form_unknown
1880 #else
1881 # define REF(Name) &&do2_##Name
1882 #endif
1883 #undef LABEL
1884 #define LABEL(Name) do2_##Name
1885 STEP4_TABLE;
1887 int is_negative;
1888 union
1890 unsigned long long int longlong;
1891 unsigned long int word;
1892 } number;
1893 int base;
1894 union printf_arg the_arg;
1895 CHAR_T *string; /* Pointer to argument string. */
1897 /* Fill variables from values in struct. */
1898 int alt = specs[nspecs_done].info.alt;
1899 int space = specs[nspecs_done].info.space;
1900 int left = specs[nspecs_done].info.left;
1901 int showsign = specs[nspecs_done].info.showsign;
1902 int group = specs[nspecs_done].info.group;
1903 int is_long_double = specs[nspecs_done].info.is_long_double;
1904 int is_short = specs[nspecs_done].info.is_short;
1905 int is_char = specs[nspecs_done].info.is_char;
1906 int is_long = specs[nspecs_done].info.is_long;
1907 int width = specs[nspecs_done].info.width;
1908 int prec = specs[nspecs_done].info.prec;
1909 int use_outdigits = specs[nspecs_done].info.i18n;
1910 char pad = specs[nspecs_done].info.pad;
1911 CHAR_T spec = specs[nspecs_done].info.spec;
1913 workstart = NULL;
1914 workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1916 /* Fill in last information. */
1917 if (specs[nspecs_done].width_arg != -1)
1919 /* Extract the field width from an argument. */
1920 specs[nspecs_done].info.width =
1921 args_value[specs[nspecs_done].width_arg].pa_int;
1923 if (specs[nspecs_done].info.width < 0)
1924 /* If the width value is negative left justification is
1925 selected and the value is taken as being positive. */
1927 specs[nspecs_done].info.width *= -1;
1928 left = specs[nspecs_done].info.left = 1;
1930 width = specs[nspecs_done].info.width;
1933 if (specs[nspecs_done].prec_arg != -1)
1935 /* Extract the precision from an argument. */
1936 specs[nspecs_done].info.prec =
1937 args_value[specs[nspecs_done].prec_arg].pa_int;
1939 if (specs[nspecs_done].info.prec < 0)
1940 /* If the precision is negative the precision is
1941 omitted. */
1942 specs[nspecs_done].info.prec = -1;
1944 prec = specs[nspecs_done].info.prec;
1947 /* Maybe the buffer is too small. */
1948 if (MAX (prec, width) + 32 > (int) (sizeof (work_buffer)
1949 / sizeof (CHAR_T)))
1951 if (__libc_use_alloca ((MAX (prec, width) + 32)
1952 * sizeof (CHAR_T)))
1953 workend = ((CHAR_T *) alloca ((MAX (prec, width) + 32)
1954 * sizeof (CHAR_T))
1955 + (MAX (prec, width) + 32));
1956 else
1958 workstart = (CHAR_T *) malloc ((MAX (prec, width) + 32)
1959 * sizeof (CHAR_T));
1960 workend = workstart + (MAX (prec, width) + 32);
1964 /* Process format specifiers. */
1965 while (1)
1967 extern printf_function **__printf_function_table;
1968 int function_done;
1970 if (spec <= UCHAR_MAX
1971 && __printf_function_table != NULL
1972 && __printf_function_table[(size_t) spec] != NULL)
1974 const void **ptr = alloca (specs[nspecs_done].ndata_args
1975 * sizeof (const void *));
1977 /* Fill in an array of pointers to the argument values. */
1978 for (unsigned int i = 0; i < specs[nspecs_done].ndata_args;
1979 ++i)
1980 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1982 /* Call the function. */
1983 function_done = __printf_function_table[(size_t) spec]
1984 (s, &specs[nspecs_done].info, ptr);
1986 if (function_done != -2)
1988 /* If an error occurred we don't have information
1989 about # of chars. */
1990 if (function_done < 0)
1992 /* Function has set errno. */
1993 done = -1;
1994 goto all_done;
1997 done_add (function_done);
1998 break;
2002 JUMP (spec, step4_jumps);
2004 process_arg ((&specs[nspecs_done]));
2005 process_string_arg ((&specs[nspecs_done]));
2007 LABEL (form_unknown):
2009 unsigned int i;
2010 const void **ptr;
2012 ptr = alloca (specs[nspecs_done].ndata_args
2013 * sizeof (const void *));
2015 /* Fill in an array of pointers to the argument values. */
2016 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
2017 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
2019 /* Call the function. */
2020 function_done = printf_unknown (s, &specs[nspecs_done].info,
2021 ptr);
2023 /* If an error occurred we don't have information about #
2024 of chars. */
2025 if (function_done < 0)
2027 /* Function has set errno. */
2028 done = -1;
2029 goto all_done;
2032 done_add (function_done);
2034 break;
2037 free (workstart);
2038 workstart = NULL;
2040 /* Write the following constant string. */
2041 outstring (specs[nspecs_done].end_of_fmt,
2042 specs[nspecs_done].next_fmt
2043 - specs[nspecs_done].end_of_fmt);
2047 all_done:
2048 free (args_malloced);
2049 free (workstart);
2050 /* Unlock the stream. */
2051 _IO_funlockfile (s);
2052 _IO_cleanup_region_end (0);
2054 return done;
2057 /* Handle an unknown format specifier. This prints out a canonicalized
2058 representation of the format spec itself. */
2059 static int
2060 printf_unknown (FILE *s, const struct printf_info *info,
2061 const void *const *args)
2064 int done = 0;
2065 CHAR_T work_buffer[MAX (sizeof (info->width), sizeof (info->prec)) * 3];
2066 CHAR_T *const workend
2067 = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
2068 register CHAR_T *w;
2070 outchar (L_('%'));
2072 if (info->alt)
2073 outchar (L_('#'));
2074 if (info->group)
2075 outchar (L_('\''));
2076 if (info->showsign)
2077 outchar (L_('+'));
2078 else if (info->space)
2079 outchar (L_(' '));
2080 if (info->left)
2081 outchar (L_('-'));
2082 if (info->pad == L_('0'))
2083 outchar (L_('0'));
2084 if (info->i18n)
2085 outchar (L_('I'));
2087 if (info->width != 0)
2089 w = _itoa_word (info->width, workend, 10, 0);
2090 while (w < workend)
2091 outchar (*w++);
2094 if (info->prec != -1)
2096 outchar (L_('.'));
2097 w = _itoa_word (info->prec, workend, 10, 0);
2098 while (w < workend)
2099 outchar (*w++);
2102 if (info->spec != L_('\0'))
2103 outchar (info->spec);
2105 all_done:
2106 return done;
2109 /* Group the digits according to the grouping rules of the current locale.
2110 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
2111 static CHAR_T *
2112 internal_function
2113 group_number (CHAR_T *w, CHAR_T *rear_ptr, const char *grouping,
2114 #ifdef COMPILE_WPRINTF
2115 wchar_t thousands_sep
2116 #else
2117 const char *thousands_sep
2118 #endif
2121 int len;
2122 CHAR_T *src, *s;
2123 #ifndef COMPILE_WPRINTF
2124 int tlen = strlen (thousands_sep);
2125 #endif
2127 /* We treat all negative values like CHAR_MAX. */
2129 if (*grouping == CHAR_MAX || *grouping <= 0)
2130 /* No grouping should be done. */
2131 return w;
2133 len = *grouping++;
2135 /* Copy existing string so that nothing gets overwritten. */
2136 src = (CHAR_T *) alloca ((rear_ptr - w) * sizeof (CHAR_T));
2137 s = (CHAR_T *) __mempcpy (src, w,
2138 (rear_ptr - w) * sizeof (CHAR_T));
2139 w = rear_ptr;
2141 /* Process all characters in the string. */
2142 while (s > src)
2144 *--w = *--s;
2146 if (--len == 0 && s > src)
2148 /* A new group begins. */
2149 #ifdef COMPILE_WPRINTF
2150 *--w = thousands_sep;
2151 #else
2152 int cnt = tlen;
2154 *--w = thousands_sep[--cnt];
2155 while (cnt > 0);
2156 #endif
2158 if (*grouping == CHAR_MAX
2159 #if CHAR_MIN < 0
2160 || *grouping < 0
2161 #endif
2164 /* No further grouping to be done.
2165 Copy the rest of the number. */
2167 *--w = *--s;
2168 while (s > src);
2169 break;
2171 else if (*grouping != '\0')
2172 /* The previous grouping repeats ad infinitum. */
2173 len = *grouping++;
2174 else
2175 len = grouping[-1];
2178 return w;
2181 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2182 struct helper_file
2184 struct _IO_FILE_plus _f;
2185 #ifdef COMPILE_WPRINTF
2186 struct _IO_wide_data _wide_data;
2187 #endif
2188 _IO_FILE *_put_stream;
2189 #ifdef _IO_MTSAFE_IO
2190 _IO_lock_t lock;
2191 #endif
2194 static int
2195 _IO_helper_overflow (_IO_FILE *s, int c)
2197 _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
2198 #ifdef COMPILE_WPRINTF
2199 int used = s->_wide_data->_IO_write_ptr - s->_wide_data->_IO_write_base;
2200 if (used)
2202 _IO_size_t written = _IO_sputn (target, s->_wide_data->_IO_write_base,
2203 used);
2204 if (written == 0 || written == WEOF)
2205 return WEOF;
2206 __wmemmove (s->_wide_data->_IO_write_base,
2207 s->_wide_data->_IO_write_base + written,
2208 used - written);
2209 s->_wide_data->_IO_write_ptr -= written;
2211 #else
2212 int used = s->_IO_write_ptr - s->_IO_write_base;
2213 if (used)
2215 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
2216 if (written == 0 || written == EOF)
2217 return EOF;
2218 memmove (s->_IO_write_base, s->_IO_write_base + written,
2219 used - written);
2220 s->_IO_write_ptr -= written;
2222 #endif
2223 return PUTC (c, s);
2226 #ifdef COMPILE_WPRINTF
2227 static const struct _IO_jump_t _IO_helper_jumps =
2229 JUMP_INIT_DUMMY,
2230 JUMP_INIT (finish, _IO_wdefault_finish),
2231 JUMP_INIT (overflow, _IO_helper_overflow),
2232 JUMP_INIT (underflow, _IO_default_underflow),
2233 JUMP_INIT (uflow, _IO_default_uflow),
2234 JUMP_INIT (pbackfail, (_IO_pbackfail_t) _IO_wdefault_pbackfail),
2235 JUMP_INIT (xsputn, _IO_wdefault_xsputn),
2236 JUMP_INIT (xsgetn, _IO_wdefault_xsgetn),
2237 JUMP_INIT (seekoff, _IO_default_seekoff),
2238 JUMP_INIT (seekpos, _IO_default_seekpos),
2239 JUMP_INIT (setbuf, _IO_default_setbuf),
2240 JUMP_INIT (sync, _IO_default_sync),
2241 JUMP_INIT (doallocate, _IO_wdefault_doallocate),
2242 JUMP_INIT (read, _IO_default_read),
2243 JUMP_INIT (write, _IO_default_write),
2244 JUMP_INIT (seek, _IO_default_seek),
2245 JUMP_INIT (close, _IO_default_close),
2246 JUMP_INIT (stat, _IO_default_stat)
2248 #else
2249 static const struct _IO_jump_t _IO_helper_jumps =
2251 JUMP_INIT_DUMMY,
2252 JUMP_INIT (finish, _IO_default_finish),
2253 JUMP_INIT (overflow, _IO_helper_overflow),
2254 JUMP_INIT (underflow, _IO_default_underflow),
2255 JUMP_INIT (uflow, _IO_default_uflow),
2256 JUMP_INIT (pbackfail, _IO_default_pbackfail),
2257 JUMP_INIT (xsputn, _IO_default_xsputn),
2258 JUMP_INIT (xsgetn, _IO_default_xsgetn),
2259 JUMP_INIT (seekoff, _IO_default_seekoff),
2260 JUMP_INIT (seekpos, _IO_default_seekpos),
2261 JUMP_INIT (setbuf, _IO_default_setbuf),
2262 JUMP_INIT (sync, _IO_default_sync),
2263 JUMP_INIT (doallocate, _IO_default_doallocate),
2264 JUMP_INIT (read, _IO_default_read),
2265 JUMP_INIT (write, _IO_default_write),
2266 JUMP_INIT (seek, _IO_default_seek),
2267 JUMP_INIT (close, _IO_default_close),
2268 JUMP_INIT (stat, _IO_default_stat)
2270 #endif
2272 static int
2273 internal_function
2274 buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format,
2275 _IO_va_list args)
2277 CHAR_T buf[_IO_BUFSIZ];
2278 struct helper_file helper;
2279 register _IO_FILE *hp = (_IO_FILE *) &helper._f;
2280 int result, to_flush;
2282 /* Orient the stream. */
2283 #ifdef ORIENT
2284 ORIENT;
2285 #endif
2287 /* Initialize helper. */
2288 helper._put_stream = s;
2289 #ifdef COMPILE_WPRINTF
2290 hp->_wide_data = &helper._wide_data;
2291 _IO_wsetp (hp, buf, buf + sizeof buf / sizeof (CHAR_T));
2292 hp->_mode = 1;
2293 #else
2294 _IO_setp (hp, buf, buf + sizeof buf);
2295 hp->_mode = -1;
2296 #endif
2297 hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS|_IO_USER_LOCK;
2298 #if _IO_JUMPS_OFFSET
2299 hp->_vtable_offset = 0;
2300 #endif
2301 #ifdef _IO_MTSAFE_IO
2302 hp->_lock = NULL;
2303 #endif
2304 hp->_flags2 = s->_flags2;
2305 _IO_JUMPS (&helper._f) = (struct _IO_jump_t *) &_IO_helper_jumps;
2307 /* Now print to helper instead. */
2308 #ifndef COMPILE_WPRINTF
2309 result = _IO_vfprintf (hp, format, args);
2310 #else
2311 result = vfprintf (hp, format, args);
2312 #endif
2314 /* Lock stream. */
2315 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, s);
2316 _IO_flockfile (s);
2318 /* Now flush anything from the helper to the S. */
2319 #ifdef COMPILE_WPRINTF
2320 if ((to_flush = (hp->_wide_data->_IO_write_ptr
2321 - hp->_wide_data->_IO_write_base)) > 0)
2323 if ((int) _IO_sputn (s, hp->_wide_data->_IO_write_base, to_flush)
2324 != to_flush)
2325 result = -1;
2327 #else
2328 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
2330 if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
2331 result = -1;
2333 #endif
2335 /* Unlock the stream. */
2336 _IO_funlockfile (s);
2337 __libc_cleanup_region_end (0);
2339 return result;
2342 #undef vfprintf
2343 #ifdef COMPILE_WPRINTF
2344 strong_alias (_IO_vfwprintf, __vfwprintf);
2345 ldbl_weak_alias (_IO_vfwprintf, vfwprintf);
2346 #else
2347 ldbl_strong_alias (_IO_vfprintf_internal, vfprintf);
2348 ldbl_hidden_def (_IO_vfprintf_internal, vfprintf)
2349 ldbl_strong_alias (_IO_vfprintf_internal, _IO_vfprintf);
2350 ldbl_hidden_def (_IO_vfprintf_internal, _IO_vfprintf)
2351 #endif