vfprintf: Introduce JUMP_TABLE_BASE_LABEL
[glibc.git] / stdio-common / vfprintf.c
blob030d33d981052e9608ccbb9a8a28cd974103e21e
1 /* Copyright (C) 1991-2015 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 (__glibc_unlikely (INT_MAX - done < _val)) \
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 typedef const char *THOUSANDS_SEP_T;
85 # define L_(Str) Str
86 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
87 # define STR_LEN(Str) strlen (Str)
89 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
90 # define PAD(Padchar) \
91 do { \
92 if (width > 0) \
93 { \
94 _IO_ssize_t written = _IO_padn (s, (Padchar), width); \
95 if (__glibc_unlikely (written != width)) \
96 { \
97 done = -1; \
98 goto all_done; \
99 } \
100 done_add (written); \
102 } while (0)
103 # define PUTC(C, F) _IO_putc_unlocked (C, F)
104 # define ORIENT if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
105 return -1
106 #else
107 # define vfprintf _IO_vfwprintf
108 # define CHAR_T wchar_t
109 /* This is a hack!!! There should be a type uwchar_t. */
110 # define UCHAR_T unsigned int /* uwchar_t */
111 # define INT_T wint_t
112 typedef wchar_t THOUSANDS_SEP_T;
113 # define L_(Str) L##Str
114 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
115 # define STR_LEN(Str) __wcslen (Str)
117 # include <_itowa.h>
119 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
120 # define PAD(Padchar) \
121 do { \
122 if (width > 0) \
124 _IO_ssize_t written = _IO_wpadn (s, (Padchar), width); \
125 if (__glibc_unlikely (written != width)) \
127 done = -1; \
128 goto all_done; \
130 done_add (written); \
132 } while (0)
133 # define PUTC(C, F) _IO_putwc_unlocked (C, F)
134 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
136 # undef _itoa
137 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
138 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
139 # undef EOF
140 # define EOF WEOF
141 #endif
143 #include "_i18n_number.h"
145 /* Include the shared code for parsing the format string. */
146 #include "printf-parse.h"
149 #define outchar(Ch) \
150 do \
152 const INT_T outc = (Ch); \
153 if (PUTC (outc, s) == EOF || done == INT_MAX) \
155 done = -1; \
156 goto all_done; \
158 ++done; \
160 while (0)
162 #define outstring(String, Len) \
163 do \
165 assert ((size_t) done <= (size_t) INT_MAX); \
166 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
168 done = -1; \
169 goto all_done; \
171 if (__glibc_unlikely (INT_MAX - done < (Len))) \
173 done = -1; \
174 __set_errno (EOVERFLOW); \
175 goto all_done; \
177 done += (Len); \
179 while (0)
181 /* For handling long_double and longlong we use the same flag. If
182 `long' and `long long' are effectively the same type define it to
183 zero. */
184 #if LONG_MAX == LONG_LONG_MAX
185 # define is_longlong 0
186 #else
187 # define is_longlong is_long_double
188 #endif
190 /* If `long' and `int' is effectively the same type we don't have to
191 handle `long separately. */
192 #if INT_MAX == LONG_MAX
193 # define is_long_num 0
194 #else
195 # define is_long_num is_long
196 #endif
199 /* Global variables. */
200 static const CHAR_T null[] = L_("(null)");
203 /* Helper function to provide temporary buffering for unbuffered streams. */
204 static int buffered_vfprintf (FILE *stream, const CHAR_T *fmt, va_list)
205 __THROW __attribute__ ((noinline)) internal_function;
207 /* Handle unknown format specifier. */
208 static int printf_unknown (FILE *, const struct printf_info *,
209 const void *const *) __THROW;
211 /* Group digits of number string. */
212 static CHAR_T *group_number (CHAR_T *, CHAR_T *, const char *, THOUSANDS_SEP_T)
213 __THROW internal_function;
215 /* The function itself. */
217 vfprintf (FILE *s, const CHAR_T *format, va_list ap)
219 /* The character used as thousands separator. */
220 THOUSANDS_SEP_T thousands_sep = 0;
222 /* The string describing the size of groups of digits. */
223 const char *grouping;
225 /* Place to accumulate the result. */
226 int done;
228 /* Current character in format string. */
229 const UCHAR_T *f;
231 /* End of leading constant string. */
232 const UCHAR_T *lead_str_end;
234 /* Points to next format specifier. */
235 const UCHAR_T *end_of_spec;
237 /* Buffer intermediate results. */
238 CHAR_T work_buffer[1000];
239 CHAR_T *workstart = NULL;
240 CHAR_T *workend;
242 /* We have to save the original argument pointer. */
243 va_list ap_save;
245 /* Count number of specifiers we already processed. */
246 int nspecs_done;
248 /* For the %m format we may need the current `errno' value. */
249 int save_errno = errno;
251 /* 1 if format is in read-only memory, -1 if it is in writable memory,
252 0 if unknown. */
253 int readonly_format = 0;
255 /* For the argument descriptions, which may be allocated on the heap. */
256 void *args_malloced = NULL;
258 /* For positional argument handling. */
259 struct printf_spec *specs;
261 /* Track if we malloced the SPECS array and thus must free it. */
262 bool specs_malloced = false;
264 /* This table maps a character into a number representing a
265 class. In each step there is a destination label for each
266 class. */
267 static const uint8_t jump_table[] =
269 /* ' ' */ 1, 0, 0, /* '#' */ 4,
270 0, /* '%' */ 14, 0, /* '\''*/ 6,
271 0, 0, /* '*' */ 7, /* '+' */ 2,
272 0, /* '-' */ 3, /* '.' */ 9, 0,
273 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
274 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
275 /* '8' */ 8, /* '9' */ 8, 0, 0,
276 0, 0, 0, 0,
277 0, /* 'A' */ 26, 0, /* 'C' */ 25,
278 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
279 0, /* 'I' */ 29, 0, 0,
280 /* 'L' */ 12, 0, 0, 0,
281 0, 0, 0, /* 'S' */ 21,
282 0, 0, 0, 0,
283 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
284 0, 0, 0, 0,
285 0, /* 'a' */ 26, 0, /* 'c' */ 20,
286 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
287 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
288 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
289 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
290 /* 't' */ 27, /* 'u' */ 16, 0, 0,
291 /* 'x' */ 18, 0, /* 'z' */ 13
294 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
295 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
296 #ifdef SHARED
297 /* 'int' is enough and it saves some space on 64 bit systems. */
298 # define JUMP_TABLE_TYPE const int
299 # define JUMP(ChExpr, table) \
300 do \
302 int offset; \
303 void *ptr; \
304 spec = (ChExpr); \
305 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
306 : table[CHAR_CLASS (spec)]; \
307 ptr = &&JUMP_TABLE_BASE_LABEL + offset; \
308 goto *ptr; \
310 while (0)
311 #else
312 # define JUMP_TABLE_TYPE const void *const
313 # define JUMP(ChExpr, table) \
314 do \
316 const void *ptr; \
317 spec = (ChExpr); \
318 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
319 : table[CHAR_CLASS (spec)]; \
320 goto *ptr; \
322 while (0)
323 #endif
325 #define STEP0_3_TABLE \
326 /* Step 0: at the beginning. */ \
327 static JUMP_TABLE_TYPE step0_jumps[30] = \
329 REF (form_unknown), \
330 REF (flag_space), /* for ' ' */ \
331 REF (flag_plus), /* for '+' */ \
332 REF (flag_minus), /* for '-' */ \
333 REF (flag_hash), /* for '<hash>' */ \
334 REF (flag_zero), /* for '0' */ \
335 REF (flag_quote), /* for '\'' */ \
336 REF (width_asterics), /* for '*' */ \
337 REF (width), /* for '1'...'9' */ \
338 REF (precision), /* for '.' */ \
339 REF (mod_half), /* for 'h' */ \
340 REF (mod_long), /* for 'l' */ \
341 REF (mod_longlong), /* for 'L', 'q' */ \
342 REF (mod_size_t), /* for 'z', 'Z' */ \
343 REF (form_percent), /* for '%' */ \
344 REF (form_integer), /* for 'd', 'i' */ \
345 REF (form_unsigned), /* for 'u' */ \
346 REF (form_octal), /* for 'o' */ \
347 REF (form_hexa), /* for 'X', 'x' */ \
348 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
349 REF (form_character), /* for 'c' */ \
350 REF (form_string), /* for 's', 'S' */ \
351 REF (form_pointer), /* for 'p' */ \
352 REF (form_number), /* for 'n' */ \
353 REF (form_strerror), /* for 'm' */ \
354 REF (form_wcharacter), /* for 'C' */ \
355 REF (form_floathex), /* for 'A', 'a' */ \
356 REF (mod_ptrdiff_t), /* for 't' */ \
357 REF (mod_intmax_t), /* for 'j' */ \
358 REF (flag_i18n), /* for 'I' */ \
359 }; \
360 /* Step 1: after processing width. */ \
361 static JUMP_TABLE_TYPE step1_jumps[30] = \
363 REF (form_unknown), \
364 REF (form_unknown), /* for ' ' */ \
365 REF (form_unknown), /* for '+' */ \
366 REF (form_unknown), /* for '-' */ \
367 REF (form_unknown), /* for '<hash>' */ \
368 REF (form_unknown), /* for '0' */ \
369 REF (form_unknown), /* for '\'' */ \
370 REF (form_unknown), /* for '*' */ \
371 REF (form_unknown), /* for '1'...'9' */ \
372 REF (precision), /* for '.' */ \
373 REF (mod_half), /* for 'h' */ \
374 REF (mod_long), /* for 'l' */ \
375 REF (mod_longlong), /* for 'L', 'q' */ \
376 REF (mod_size_t), /* for 'z', 'Z' */ \
377 REF (form_percent), /* for '%' */ \
378 REF (form_integer), /* for 'd', 'i' */ \
379 REF (form_unsigned), /* for 'u' */ \
380 REF (form_octal), /* for 'o' */ \
381 REF (form_hexa), /* for 'X', 'x' */ \
382 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
383 REF (form_character), /* for 'c' */ \
384 REF (form_string), /* for 's', 'S' */ \
385 REF (form_pointer), /* for 'p' */ \
386 REF (form_number), /* for 'n' */ \
387 REF (form_strerror), /* for 'm' */ \
388 REF (form_wcharacter), /* for 'C' */ \
389 REF (form_floathex), /* for 'A', 'a' */ \
390 REF (mod_ptrdiff_t), /* for 't' */ \
391 REF (mod_intmax_t), /* for 'j' */ \
392 REF (form_unknown) /* for 'I' */ \
393 }; \
394 /* Step 2: after processing precision. */ \
395 static JUMP_TABLE_TYPE step2_jumps[30] = \
397 REF (form_unknown), \
398 REF (form_unknown), /* for ' ' */ \
399 REF (form_unknown), /* for '+' */ \
400 REF (form_unknown), /* for '-' */ \
401 REF (form_unknown), /* for '<hash>' */ \
402 REF (form_unknown), /* for '0' */ \
403 REF (form_unknown), /* for '\'' */ \
404 REF (form_unknown), /* for '*' */ \
405 REF (form_unknown), /* for '1'...'9' */ \
406 REF (form_unknown), /* for '.' */ \
407 REF (mod_half), /* for 'h' */ \
408 REF (mod_long), /* for 'l' */ \
409 REF (mod_longlong), /* for 'L', 'q' */ \
410 REF (mod_size_t), /* for 'z', 'Z' */ \
411 REF (form_percent), /* for '%' */ \
412 REF (form_integer), /* for 'd', 'i' */ \
413 REF (form_unsigned), /* for 'u' */ \
414 REF (form_octal), /* for 'o' */ \
415 REF (form_hexa), /* for 'X', 'x' */ \
416 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
417 REF (form_character), /* for 'c' */ \
418 REF (form_string), /* for 's', 'S' */ \
419 REF (form_pointer), /* for 'p' */ \
420 REF (form_number), /* for 'n' */ \
421 REF (form_strerror), /* for 'm' */ \
422 REF (form_wcharacter), /* for 'C' */ \
423 REF (form_floathex), /* for 'A', 'a' */ \
424 REF (mod_ptrdiff_t), /* for 't' */ \
425 REF (mod_intmax_t), /* for 'j' */ \
426 REF (form_unknown) /* for 'I' */ \
427 }; \
428 /* Step 3a: after processing first 'h' modifier. */ \
429 static JUMP_TABLE_TYPE step3a_jumps[30] = \
431 REF (form_unknown), \
432 REF (form_unknown), /* for ' ' */ \
433 REF (form_unknown), /* for '+' */ \
434 REF (form_unknown), /* for '-' */ \
435 REF (form_unknown), /* for '<hash>' */ \
436 REF (form_unknown), /* for '0' */ \
437 REF (form_unknown), /* for '\'' */ \
438 REF (form_unknown), /* for '*' */ \
439 REF (form_unknown), /* for '1'...'9' */ \
440 REF (form_unknown), /* for '.' */ \
441 REF (mod_halfhalf), /* for 'h' */ \
442 REF (form_unknown), /* for 'l' */ \
443 REF (form_unknown), /* for 'L', 'q' */ \
444 REF (form_unknown), /* for 'z', 'Z' */ \
445 REF (form_percent), /* for '%' */ \
446 REF (form_integer), /* for 'd', 'i' */ \
447 REF (form_unsigned), /* for 'u' */ \
448 REF (form_octal), /* for 'o' */ \
449 REF (form_hexa), /* for 'X', 'x' */ \
450 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
451 REF (form_unknown), /* for 'c' */ \
452 REF (form_unknown), /* for 's', 'S' */ \
453 REF (form_unknown), /* for 'p' */ \
454 REF (form_number), /* for 'n' */ \
455 REF (form_unknown), /* for 'm' */ \
456 REF (form_unknown), /* for 'C' */ \
457 REF (form_unknown), /* for 'A', 'a' */ \
458 REF (form_unknown), /* for 't' */ \
459 REF (form_unknown), /* for 'j' */ \
460 REF (form_unknown) /* for 'I' */ \
461 }; \
462 /* Step 3b: after processing first 'l' modifier. */ \
463 static JUMP_TABLE_TYPE step3b_jumps[30] = \
465 REF (form_unknown), \
466 REF (form_unknown), /* for ' ' */ \
467 REF (form_unknown), /* for '+' */ \
468 REF (form_unknown), /* for '-' */ \
469 REF (form_unknown), /* for '<hash>' */ \
470 REF (form_unknown), /* for '0' */ \
471 REF (form_unknown), /* for '\'' */ \
472 REF (form_unknown), /* for '*' */ \
473 REF (form_unknown), /* for '1'...'9' */ \
474 REF (form_unknown), /* for '.' */ \
475 REF (form_unknown), /* for 'h' */ \
476 REF (mod_longlong), /* for 'l' */ \
477 REF (form_unknown), /* for 'L', 'q' */ \
478 REF (form_unknown), /* for 'z', 'Z' */ \
479 REF (form_percent), /* for '%' */ \
480 REF (form_integer), /* for 'd', 'i' */ \
481 REF (form_unsigned), /* for 'u' */ \
482 REF (form_octal), /* for 'o' */ \
483 REF (form_hexa), /* for 'X', 'x' */ \
484 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
485 REF (form_character), /* for 'c' */ \
486 REF (form_string), /* for 's', 'S' */ \
487 REF (form_pointer), /* for 'p' */ \
488 REF (form_number), /* for 'n' */ \
489 REF (form_strerror), /* for 'm' */ \
490 REF (form_wcharacter), /* for 'C' */ \
491 REF (form_floathex), /* for 'A', 'a' */ \
492 REF (form_unknown), /* for 't' */ \
493 REF (form_unknown), /* for 'j' */ \
494 REF (form_unknown) /* for 'I' */ \
497 #define STEP4_TABLE \
498 /* Step 4: processing format specifier. */ \
499 static JUMP_TABLE_TYPE step4_jumps[30] = \
501 REF (form_unknown), \
502 REF (form_unknown), /* for ' ' */ \
503 REF (form_unknown), /* for '+' */ \
504 REF (form_unknown), /* for '-' */ \
505 REF (form_unknown), /* for '<hash>' */ \
506 REF (form_unknown), /* for '0' */ \
507 REF (form_unknown), /* for '\'' */ \
508 REF (form_unknown), /* for '*' */ \
509 REF (form_unknown), /* for '1'...'9' */ \
510 REF (form_unknown), /* for '.' */ \
511 REF (form_unknown), /* for 'h' */ \
512 REF (form_unknown), /* for 'l' */ \
513 REF (form_unknown), /* for 'L', 'q' */ \
514 REF (form_unknown), /* for 'z', 'Z' */ \
515 REF (form_percent), /* for '%' */ \
516 REF (form_integer), /* for 'd', 'i' */ \
517 REF (form_unsigned), /* for 'u' */ \
518 REF (form_octal), /* for 'o' */ \
519 REF (form_hexa), /* for 'X', 'x' */ \
520 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
521 REF (form_character), /* for 'c' */ \
522 REF (form_string), /* for 's', 'S' */ \
523 REF (form_pointer), /* for 'p' */ \
524 REF (form_number), /* for 'n' */ \
525 REF (form_strerror), /* for 'm' */ \
526 REF (form_wcharacter), /* for 'C' */ \
527 REF (form_floathex), /* for 'A', 'a' */ \
528 REF (form_unknown), /* for 't' */ \
529 REF (form_unknown), /* for 'j' */ \
530 REF (form_unknown) /* for 'I' */ \
534 #define process_arg(fspec) \
535 /* Start real work. We know about all flags and modifiers and \
536 now process the wanted format specifier. */ \
537 LABEL (form_percent): \
538 /* Write a literal "%". */ \
539 outchar (L_('%')); \
540 break; \
542 LABEL (form_integer): \
543 /* Signed decimal integer. */ \
544 base = 10; \
546 if (is_longlong) \
548 long long int signed_number; \
550 if (fspec == NULL) \
551 signed_number = va_arg (ap, long long int); \
552 else \
553 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
555 is_negative = signed_number < 0; \
556 number.longlong = is_negative ? (- signed_number) : signed_number; \
558 goto LABEL (longlong_number); \
560 else \
562 long int signed_number; \
564 if (fspec == NULL) \
566 if (is_long_num) \
567 signed_number = va_arg (ap, long int); \
568 else if (is_char) \
569 signed_number = (signed char) va_arg (ap, unsigned int); \
570 else if (!is_short) \
571 signed_number = va_arg (ap, int); \
572 else \
573 signed_number = (short int) va_arg (ap, unsigned int); \
575 else \
576 if (is_long_num) \
577 signed_number = args_value[fspec->data_arg].pa_long_int; \
578 else if (is_char) \
579 signed_number = (signed char) \
580 args_value[fspec->data_arg].pa_u_int; \
581 else if (!is_short) \
582 signed_number = args_value[fspec->data_arg].pa_int; \
583 else \
584 signed_number = (short int) \
585 args_value[fspec->data_arg].pa_u_int; \
587 is_negative = signed_number < 0; \
588 number.word = is_negative ? (- signed_number) : signed_number; \
590 goto LABEL (number); \
592 /* NOTREACHED */ \
594 LABEL (form_unsigned): \
595 /* Unsigned decimal integer. */ \
596 base = 10; \
597 goto LABEL (unsigned_number); \
598 /* NOTREACHED */ \
600 LABEL (form_octal): \
601 /* Unsigned octal integer. */ \
602 base = 8; \
603 goto LABEL (unsigned_number); \
604 /* NOTREACHED */ \
606 LABEL (form_hexa): \
607 /* Unsigned hexadecimal integer. */ \
608 base = 16; \
610 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
612 /* ISO specifies the `+' and ` ' flags only for signed \
613 conversions. */ \
614 is_negative = 0; \
615 showsign = 0; \
616 space = 0; \
618 if (is_longlong) \
620 if (fspec == NULL) \
621 number.longlong = va_arg (ap, unsigned long long int); \
622 else \
623 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
625 LABEL (longlong_number): \
626 if (prec < 0) \
627 /* Supply a default precision if none was given. */ \
628 prec = 1; \
629 else \
630 /* We have to take care for the '0' flag. If a precision \
631 is given it must be ignored. */ \
632 pad = L_(' '); \
634 /* If the precision is 0 and the number is 0 nothing has to \
635 be written for the number, except for the 'o' format in \
636 alternate form. */ \
637 if (prec == 0 && number.longlong == 0) \
639 string = workend; \
640 if (base == 8 && alt) \
641 *--string = L_('0'); \
643 else \
645 /* Put the number in WORK. */ \
646 string = _itoa (number.longlong, workend, base, \
647 spec == L_('X')); \
648 if (group && grouping) \
649 string = group_number (string, workend, grouping, \
650 thousands_sep); \
652 if (use_outdigits && base == 10) \
653 string = _i18n_number_rewrite (string, workend, workend); \
655 /* Simplify further test for num != 0. */ \
656 number.word = number.longlong != 0; \
658 else \
660 if (fspec == NULL) \
662 if (is_long_num) \
663 number.word = va_arg (ap, unsigned long int); \
664 else if (is_char) \
665 number.word = (unsigned char) va_arg (ap, unsigned int); \
666 else if (!is_short) \
667 number.word = va_arg (ap, unsigned int); \
668 else \
669 number.word = (unsigned short int) va_arg (ap, unsigned int); \
671 else \
672 if (is_long_num) \
673 number.word = args_value[fspec->data_arg].pa_u_long_int; \
674 else if (is_char) \
675 number.word = (unsigned char) \
676 args_value[fspec->data_arg].pa_u_int; \
677 else if (!is_short) \
678 number.word = args_value[fspec->data_arg].pa_u_int; \
679 else \
680 number.word = (unsigned short int) \
681 args_value[fspec->data_arg].pa_u_int; \
683 LABEL (number): \
684 if (prec < 0) \
685 /* Supply a default precision if none was given. */ \
686 prec = 1; \
687 else \
688 /* We have to take care for the '0' flag. If a precision \
689 is given it must be ignored. */ \
690 pad = L_(' '); \
692 /* If the precision is 0 and the number is 0 nothing has to \
693 be written for the number, except for the 'o' format in \
694 alternate form. */ \
695 if (prec == 0 && number.word == 0) \
697 string = workend; \
698 if (base == 8 && alt) \
699 *--string = L_('0'); \
701 else \
703 /* Put the number in WORK. */ \
704 string = _itoa_word (number.word, workend, base, \
705 spec == L_('X')); \
706 if (group && grouping) \
707 string = group_number (string, workend, grouping, \
708 thousands_sep); \
710 if (use_outdigits && base == 10) \
711 string = _i18n_number_rewrite (string, workend, workend); \
715 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
716 /* Add octal marker. */ \
717 *--string = L_('0'); \
719 prec = MAX (0, prec - (workend - string)); \
721 if (!left) \
723 width -= workend - string + prec; \
725 if (number.word != 0 && alt && base == 16) \
726 /* Account for 0X hex marker. */ \
727 width -= 2; \
729 if (is_negative || showsign || space) \
730 --width; \
732 if (pad == L_(' ')) \
734 PAD (L_(' ')); \
735 width = 0; \
738 if (is_negative) \
739 outchar (L_('-')); \
740 else if (showsign) \
741 outchar (L_('+')); \
742 else if (space) \
743 outchar (L_(' ')); \
745 if (number.word != 0 && alt && base == 16) \
747 outchar (L_('0')); \
748 outchar (spec); \
751 width += prec; \
752 PAD (L_('0')); \
754 outstring (string, workend - string); \
756 break; \
758 else \
760 if (is_negative) \
762 outchar (L_('-')); \
763 --width; \
765 else if (showsign) \
767 outchar (L_('+')); \
768 --width; \
770 else if (space) \
772 outchar (L_(' ')); \
773 --width; \
776 if (number.word != 0 && alt && base == 16) \
778 outchar (L_('0')); \
779 outchar (spec); \
780 width -= 2; \
783 width -= workend - string + prec; \
785 if (prec > 0) \
787 int temp = width; \
788 width = prec; \
789 PAD (L_('0')); \
790 width = temp; \
793 outstring (string, workend - string); \
795 PAD (L_(' ')); \
796 break; \
799 LABEL (form_float): \
801 /* Floating-point number. This is handled by printf_fp.c. */ \
802 const void *ptr; \
803 int function_done; \
805 if (fspec == NULL) \
807 if (__ldbl_is_dbl) \
808 is_long_double = 0; \
810 struct printf_info info = { .prec = prec, \
811 .width = width, \
812 .spec = spec, \
813 .is_long_double = is_long_double, \
814 .is_short = is_short, \
815 .is_long = is_long, \
816 .alt = alt, \
817 .space = space, \
818 .left = left, \
819 .showsign = showsign, \
820 .group = group, \
821 .pad = pad, \
822 .extra = 0, \
823 .i18n = use_outdigits, \
824 .wide = sizeof (CHAR_T) != 1 }; \
826 if (is_long_double) \
827 the_arg.pa_long_double = va_arg (ap, long double); \
828 else \
829 the_arg.pa_double = va_arg (ap, double); \
830 ptr = (const void *) &the_arg; \
832 function_done = __printf_fp (s, &info, &ptr); \
834 else \
836 ptr = (const void *) &args_value[fspec->data_arg]; \
837 if (__ldbl_is_dbl) \
839 fspec->data_arg_type = PA_DOUBLE; \
840 fspec->info.is_long_double = 0; \
843 function_done = __printf_fp (s, &fspec->info, &ptr); \
846 if (function_done < 0) \
848 /* Error in print handler; up to handler to set errno. */ \
849 done = -1; \
850 goto all_done; \
853 done_add (function_done); \
855 break; \
857 LABEL (form_floathex): \
859 /* Floating point number printed as hexadecimal number. */ \
860 const void *ptr; \
861 int function_done; \
863 if (fspec == NULL) \
865 if (__ldbl_is_dbl) \
866 is_long_double = 0; \
868 struct printf_info info = { .prec = prec, \
869 .width = width, \
870 .spec = spec, \
871 .is_long_double = is_long_double, \
872 .is_short = is_short, \
873 .is_long = is_long, \
874 .alt = alt, \
875 .space = space, \
876 .left = left, \
877 .showsign = showsign, \
878 .group = group, \
879 .pad = pad, \
880 .extra = 0, \
881 .wide = sizeof (CHAR_T) != 1 }; \
883 if (is_long_double) \
884 the_arg.pa_long_double = va_arg (ap, long double); \
885 else \
886 the_arg.pa_double = va_arg (ap, double); \
887 ptr = (const void *) &the_arg; \
889 function_done = __printf_fphex (s, &info, &ptr); \
891 else \
893 ptr = (const void *) &args_value[fspec->data_arg]; \
894 if (__ldbl_is_dbl) \
895 fspec->info.is_long_double = 0; \
897 function_done = __printf_fphex (s, &fspec->info, &ptr); \
900 if (function_done < 0) \
902 /* Error in print handler; up to handler to set errno. */ \
903 done = -1; \
904 goto all_done; \
907 done_add (function_done); \
909 break; \
911 LABEL (form_pointer): \
912 /* Generic pointer. */ \
914 const void *ptr; \
915 if (fspec == NULL) \
916 ptr = va_arg (ap, void *); \
917 else \
918 ptr = args_value[fspec->data_arg].pa_pointer; \
919 if (ptr != NULL) \
921 /* If the pointer is not NULL, write it as a %#x spec. */ \
922 base = 16; \
923 number.word = (unsigned long int) ptr; \
924 is_negative = 0; \
925 alt = 1; \
926 group = 0; \
927 spec = L_('x'); \
928 goto LABEL (number); \
930 else \
932 /* Write "(nil)" for a nil pointer. */ \
933 string = (CHAR_T *) L_("(nil)"); \
934 /* Make sure the full string "(nil)" is printed. */ \
935 if (prec < 5) \
936 prec = 5; \
937 /* This is a wide string iff compiling wprintf. */ \
938 is_long = sizeof (CHAR_T) > 1; \
939 goto LABEL (print_string); \
942 /* NOTREACHED */ \
944 LABEL (form_number): \
945 if (s->_flags2 & _IO_FLAGS2_FORTIFY) \
947 if (! readonly_format) \
949 extern int __readonly_area (const void *, size_t) \
950 attribute_hidden; \
951 readonly_format \
952 = __readonly_area (format, ((STR_LEN (format) + 1) \
953 * sizeof (CHAR_T))); \
955 if (readonly_format < 0) \
956 __libc_fatal ("*** %n in writable segment detected ***\n"); \
958 /* Answer the count of characters written. */ \
959 if (fspec == NULL) \
961 if (is_longlong) \
962 *(long long int *) va_arg (ap, void *) = done; \
963 else if (is_long_num) \
964 *(long int *) va_arg (ap, void *) = done; \
965 else if (is_char) \
966 *(char *) va_arg (ap, void *) = done; \
967 else if (!is_short) \
968 *(int *) va_arg (ap, void *) = done; \
969 else \
970 *(short int *) va_arg (ap, void *) = done; \
972 else \
973 if (is_longlong) \
974 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
975 else if (is_long_num) \
976 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
977 else if (is_char) \
978 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
979 else if (!is_short) \
980 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
981 else \
982 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
983 break; \
985 LABEL (form_strerror): \
986 /* Print description of error ERRNO. */ \
987 string = \
988 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
989 sizeof work_buffer); \
990 is_long = 0; /* This is no wide-char string. */ \
991 goto LABEL (print_string)
993 #ifdef COMPILE_WPRINTF
994 # define process_string_arg(fspec) \
995 LABEL (form_character): \
996 /* Character. */ \
997 if (is_long) \
998 goto LABEL (form_wcharacter); \
999 --width; /* Account for the character itself. */ \
1000 if (!left) \
1001 PAD (L' '); \
1002 if (fspec == NULL) \
1003 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
1004 else \
1005 outchar (__btowc ((unsigned char) \
1006 args_value[fspec->data_arg].pa_int)); \
1007 if (left) \
1008 PAD (L' '); \
1009 break; \
1011 LABEL (form_wcharacter): \
1013 /* Wide character. */ \
1014 --width; \
1015 if (!left) \
1016 PAD (L' '); \
1017 if (fspec == NULL) \
1018 outchar (va_arg (ap, wchar_t)); \
1019 else \
1020 outchar (args_value[fspec->data_arg].pa_wchar); \
1021 if (left) \
1022 PAD (L' '); \
1024 break; \
1026 LABEL (form_string): \
1028 size_t len; \
1029 int string_malloced; \
1031 /* The string argument could in fact be `char *' or `wchar_t *'. \
1032 But this should not make a difference here. */ \
1033 if (fspec == NULL) \
1034 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
1035 else \
1036 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
1038 /* Entry point for printing other strings. */ \
1039 LABEL (print_string): \
1041 string_malloced = 0; \
1042 if (string == NULL) \
1044 /* Write "(null)" if there's space. */ \
1045 if (prec == -1 \
1046 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
1048 string = (CHAR_T *) null; \
1049 len = (sizeof (null) / sizeof (null[0])) - 1; \
1051 else \
1053 string = (CHAR_T *) L""; \
1054 len = 0; \
1057 else if (!is_long && spec != L_('S')) \
1059 /* This is complicated. We have to transform the multibyte \
1060 string into a wide character string. */ \
1061 const char *mbs = (const char *) string; \
1062 mbstate_t mbstate; \
1064 len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
1066 /* Allocate dynamically an array which definitely is long \
1067 enough for the wide character version. Each byte in the \
1068 multi-byte string can produce at most one wide character. */ \
1069 if (__glibc_unlikely (len > SIZE_MAX / sizeof (wchar_t))) \
1071 __set_errno (EOVERFLOW); \
1072 done = -1; \
1073 goto all_done; \
1075 else if (__libc_use_alloca (len * sizeof (wchar_t))) \
1076 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1077 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1078 == NULL) \
1080 done = -1; \
1081 goto all_done; \
1083 else \
1084 string_malloced = 1; \
1086 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1087 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1088 if (len == (size_t) -1) \
1090 /* Illegal multibyte character. */ \
1091 done = -1; \
1092 goto all_done; \
1095 else \
1097 if (prec != -1) \
1098 /* Search for the end of the string, but don't search past \
1099 the length specified by the precision. */ \
1100 len = __wcsnlen (string, prec); \
1101 else \
1102 len = __wcslen (string); \
1105 if ((width -= len) < 0) \
1107 outstring (string, len); \
1108 break; \
1111 if (!left) \
1112 PAD (L' '); \
1113 outstring (string, len); \
1114 if (left) \
1115 PAD (L' '); \
1116 if (__glibc_unlikely (string_malloced)) \
1117 free (string); \
1119 break;
1120 #else
1121 # define process_string_arg(fspec) \
1122 LABEL (form_character): \
1123 /* Character. */ \
1124 if (is_long) \
1125 goto LABEL (form_wcharacter); \
1126 --width; /* Account for the character itself. */ \
1127 if (!left) \
1128 PAD (' '); \
1129 if (fspec == NULL) \
1130 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1131 else \
1132 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1133 if (left) \
1134 PAD (' '); \
1135 break; \
1137 LABEL (form_wcharacter): \
1139 /* Wide character. */ \
1140 char buf[MB_CUR_MAX]; \
1141 mbstate_t mbstate; \
1142 size_t len; \
1144 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1145 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1146 : args_value[fspec->data_arg].pa_wchar), \
1147 &mbstate); \
1148 if (len == (size_t) -1) \
1150 /* Something went wrong during the conversion. Bail out. */ \
1151 done = -1; \
1152 goto all_done; \
1154 width -= len; \
1155 if (!left) \
1156 PAD (' '); \
1157 outstring (buf, len); \
1158 if (left) \
1159 PAD (' '); \
1161 break; \
1163 LABEL (form_string): \
1165 size_t len; \
1166 int string_malloced; \
1168 /* The string argument could in fact be `char *' or `wchar_t *'. \
1169 But this should not make a difference here. */ \
1170 if (fspec == NULL) \
1171 string = (char *) va_arg (ap, const char *); \
1172 else \
1173 string = (char *) args_value[fspec->data_arg].pa_string; \
1175 /* Entry point for printing other strings. */ \
1176 LABEL (print_string): \
1178 string_malloced = 0; \
1179 if (string == NULL) \
1181 /* Write "(null)" if there's space. */ \
1182 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1184 string = (char *) null; \
1185 len = sizeof (null) - 1; \
1187 else \
1189 string = (char *) ""; \
1190 len = 0; \
1193 else if (!is_long && spec != L_('S')) \
1195 if (prec != -1) \
1196 /* Search for the end of the string, but don't search past \
1197 the length (in bytes) specified by the precision. */ \
1198 len = __strnlen (string, prec); \
1199 else \
1200 len = strlen (string); \
1202 else \
1204 const wchar_t *s2 = (const wchar_t *) string; \
1205 mbstate_t mbstate; \
1207 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1209 if (prec >= 0) \
1211 /* The string `s2' might not be NUL terminated. */ \
1212 if (__libc_use_alloca (prec)) \
1213 string = (char *) alloca (prec); \
1214 else if ((string = (char *) malloc (prec)) == NULL) \
1216 done = -1; \
1217 goto all_done; \
1219 else \
1220 string_malloced = 1; \
1221 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1223 else \
1225 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1226 if (len != (size_t) -1) \
1228 assert (__mbsinit (&mbstate)); \
1229 s2 = (const wchar_t *) string; \
1230 if (__libc_use_alloca (len + 1)) \
1231 string = (char *) alloca (len + 1); \
1232 else if ((string = (char *) malloc (len + 1)) == NULL) \
1234 done = -1; \
1235 goto all_done; \
1237 else \
1238 string_malloced = 1; \
1239 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1243 if (len == (size_t) -1) \
1245 /* Illegal wide-character string. */ \
1246 done = -1; \
1247 goto all_done; \
1251 if ((width -= len) < 0) \
1253 outstring (string, len); \
1254 break; \
1257 if (!left) \
1258 PAD (' '); \
1259 outstring (string, len); \
1260 if (left) \
1261 PAD (' '); \
1262 if (__glibc_unlikely (string_malloced)) \
1263 free (string); \
1265 break;
1266 #endif
1268 /* Orient the stream. */
1269 #ifdef ORIENT
1270 ORIENT;
1271 #endif
1273 /* Sanity check of arguments. */
1274 ARGCHECK (s, format);
1276 #ifdef ORIENT
1277 /* Check for correct orientation. */
1278 if (_IO_vtable_offset (s) == 0 &&
1279 _IO_fwide (s, sizeof (CHAR_T) == 1 ? -1 : 1)
1280 != (sizeof (CHAR_T) == 1 ? -1 : 1))
1281 /* The stream is already oriented otherwise. */
1282 return EOF;
1283 #endif
1285 if (UNBUFFERED_P (s))
1286 /* Use a helper function which will allocate a local temporary buffer
1287 for the stream and then call us again. */
1288 return buffered_vfprintf (s, format, ap);
1290 /* Initialize local variables. */
1291 done = 0;
1292 grouping = (const char *) -1;
1293 #ifdef __va_copy
1294 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1295 since on some systems `va_list' is not an integral type. */
1296 __va_copy (ap_save, ap);
1297 #else
1298 ap_save = ap;
1299 #endif
1300 nspecs_done = 0;
1302 #ifdef COMPILE_WPRINTF
1303 /* Find the first format specifier. */
1304 f = lead_str_end = __find_specwc ((const UCHAR_T *) format);
1305 #else
1306 /* Find the first format specifier. */
1307 f = lead_str_end = __find_specmb ((const UCHAR_T *) format);
1308 #endif
1310 /* Lock stream. */
1311 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
1312 _IO_flockfile (s);
1314 /* Write the literal text before the first format. */
1315 outstring ((const UCHAR_T *) format,
1316 lead_str_end - (const UCHAR_T *) format);
1318 /* If we only have to print a simple string, return now. */
1319 if (*f == L_('\0'))
1320 goto all_done;
1322 /* Use the slow path in case any printf handler is registered. */
1323 if (__glibc_unlikely (__printf_function_table != NULL
1324 || __printf_modifier_table != NULL
1325 || __printf_va_arg_table != NULL))
1326 goto do_positional;
1328 /* Process whole format string. */
1331 #ifdef SHARED
1332 # define JUMP_TABLE_BASE_LABEL do_form_unknown
1333 # define REF(Name) (&&do_##Name - &&JUMP_TABLE_BASE_LABEL)
1334 #else
1335 # define REF(Name) &&do_##Name
1336 #endif
1337 #define LABEL(Name) do_##Name
1338 STEP0_3_TABLE;
1339 STEP4_TABLE;
1341 union printf_arg *args_value; /* This is not used here but ... */
1342 int is_negative; /* Flag for negative number. */
1343 union
1345 unsigned long long int longlong;
1346 unsigned long int word;
1347 } number;
1348 int base;
1349 union printf_arg the_arg;
1350 CHAR_T *string; /* Pointer to argument string. */
1351 int alt = 0; /* Alternate format. */
1352 int space = 0; /* Use space prefix if no sign is needed. */
1353 int left = 0; /* Left-justify output. */
1354 int showsign = 0; /* Always begin with plus or minus sign. */
1355 int group = 0; /* Print numbers according grouping rules. */
1356 int is_long_double = 0; /* Argument is long double/ long long int. */
1357 int is_short = 0; /* Argument is short int. */
1358 int is_long = 0; /* Argument is long int. */
1359 int is_char = 0; /* Argument is promoted (unsigned) char. */
1360 int width = 0; /* Width of output; 0 means none specified. */
1361 int prec = -1; /* Precision of output; -1 means none specified. */
1362 /* This flag is set by the 'I' modifier and selects the use of the
1363 `outdigits' as determined by the current locale. */
1364 int use_outdigits = 0;
1365 UCHAR_T pad = L_(' ');/* Padding character. */
1366 CHAR_T spec;
1368 workstart = NULL;
1369 workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1371 /* Get current character in format string. */
1372 JUMP (*++f, step0_jumps);
1374 /* ' ' flag. */
1375 LABEL (flag_space):
1376 space = 1;
1377 JUMP (*++f, step0_jumps);
1379 /* '+' flag. */
1380 LABEL (flag_plus):
1381 showsign = 1;
1382 JUMP (*++f, step0_jumps);
1384 /* The '-' flag. */
1385 LABEL (flag_minus):
1386 left = 1;
1387 pad = L_(' ');
1388 JUMP (*++f, step0_jumps);
1390 /* The '#' flag. */
1391 LABEL (flag_hash):
1392 alt = 1;
1393 JUMP (*++f, step0_jumps);
1395 /* The '0' flag. */
1396 LABEL (flag_zero):
1397 if (!left)
1398 pad = L_('0');
1399 JUMP (*++f, step0_jumps);
1401 /* The '\'' flag. */
1402 LABEL (flag_quote):
1403 group = 1;
1405 if (grouping == (const char *) -1)
1407 #ifdef COMPILE_WPRINTF
1408 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1409 _NL_NUMERIC_THOUSANDS_SEP_WC);
1410 #else
1411 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1412 #endif
1414 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1415 if (*grouping == '\0' || *grouping == CHAR_MAX
1416 #ifdef COMPILE_WPRINTF
1417 || thousands_sep == L'\0'
1418 #else
1419 || *thousands_sep == '\0'
1420 #endif
1422 grouping = NULL;
1424 JUMP (*++f, step0_jumps);
1426 LABEL (flag_i18n):
1427 use_outdigits = 1;
1428 JUMP (*++f, step0_jumps);
1430 /* Get width from argument. */
1431 LABEL (width_asterics):
1433 const UCHAR_T *tmp; /* Temporary value. */
1435 tmp = ++f;
1436 if (ISDIGIT (*tmp))
1438 int pos = read_int (&tmp);
1440 if (pos == -1)
1442 __set_errno (EOVERFLOW);
1443 done = -1;
1444 goto all_done;
1447 if (pos && *tmp == L_('$'))
1448 /* The width comes from a positional parameter. */
1449 goto do_positional;
1451 width = va_arg (ap, int);
1453 /* Negative width means left justified. */
1454 if (width < 0)
1456 width = -width;
1457 pad = L_(' ');
1458 left = 1;
1461 if (__glibc_unlikely (width >= INT_MAX / sizeof (CHAR_T) - 32))
1463 __set_errno (EOVERFLOW);
1464 done = -1;
1465 goto all_done;
1468 if (width >= sizeof (work_buffer) / sizeof (work_buffer[0]) - 32)
1470 /* We have to use a special buffer. The "32" is just a safe
1471 bet for all the output which is not counted in the width. */
1472 size_t needed = ((size_t) width + 32) * sizeof (CHAR_T);
1473 if (__libc_use_alloca (needed))
1474 workend = (CHAR_T *) alloca (needed) + width + 32;
1475 else
1477 workstart = (CHAR_T *) malloc (needed);
1478 if (workstart == NULL)
1480 done = -1;
1481 goto all_done;
1483 workend = workstart + width + 32;
1487 JUMP (*f, step1_jumps);
1489 /* Given width in format string. */
1490 LABEL (width):
1491 width = read_int (&f);
1493 if (__glibc_unlikely (width == -1
1494 || width >= INT_MAX / sizeof (CHAR_T) - 32))
1496 __set_errno (EOVERFLOW);
1497 done = -1;
1498 goto all_done;
1501 if (width >= sizeof (work_buffer) / sizeof (work_buffer[0]) - 32)
1503 /* We have to use a special buffer. The "32" is just a safe
1504 bet for all the output which is not counted in the width. */
1505 size_t needed = ((size_t) width + 32) * sizeof (CHAR_T);
1506 if (__libc_use_alloca (needed))
1507 workend = (CHAR_T *) alloca (needed) + width + 32;
1508 else
1510 workstart = (CHAR_T *) malloc (needed);
1511 if (workstart == NULL)
1513 done = -1;
1514 goto all_done;
1516 workend = workstart + width + 32;
1519 if (*f == L_('$'))
1520 /* Oh, oh. The argument comes from a positional parameter. */
1521 goto do_positional;
1522 JUMP (*f, step1_jumps);
1524 LABEL (precision):
1525 ++f;
1526 if (*f == L_('*'))
1528 const UCHAR_T *tmp; /* Temporary value. */
1530 tmp = ++f;
1531 if (ISDIGIT (*tmp))
1533 int pos = read_int (&tmp);
1535 if (pos == -1)
1537 __set_errno (EOVERFLOW);
1538 done = -1;
1539 goto all_done;
1542 if (pos && *tmp == L_('$'))
1543 /* The precision comes from a positional parameter. */
1544 goto do_positional;
1546 prec = va_arg (ap, int);
1548 /* If the precision is negative the precision is omitted. */
1549 if (prec < 0)
1550 prec = -1;
1552 else if (ISDIGIT (*f))
1554 prec = read_int (&f);
1556 /* The precision was specified in this case as an extremely
1557 large positive value. */
1558 if (prec == -1)
1560 __set_errno (EOVERFLOW);
1561 done = -1;
1562 goto all_done;
1565 else
1566 prec = 0;
1567 if (prec > width
1568 && prec > sizeof (work_buffer) / sizeof (work_buffer[0]) - 32)
1570 if (__glibc_unlikely (prec >= INT_MAX / sizeof (CHAR_T) - 32))
1572 __set_errno (EOVERFLOW);
1573 done = -1;
1574 goto all_done;
1576 size_t needed = ((size_t) prec + 32) * sizeof (CHAR_T);
1578 if (__libc_use_alloca (needed))
1579 workend = (CHAR_T *) alloca (needed) + prec + 32;
1580 else
1582 workstart = (CHAR_T *) malloc (needed);
1583 if (workstart == NULL)
1585 done = -1;
1586 goto all_done;
1588 workend = workstart + prec + 32;
1591 JUMP (*f, step2_jumps);
1593 /* Process 'h' modifier. There might another 'h' following. */
1594 LABEL (mod_half):
1595 is_short = 1;
1596 JUMP (*++f, step3a_jumps);
1598 /* Process 'hh' modifier. */
1599 LABEL (mod_halfhalf):
1600 is_short = 0;
1601 is_char = 1;
1602 JUMP (*++f, step4_jumps);
1604 /* Process 'l' modifier. There might another 'l' following. */
1605 LABEL (mod_long):
1606 is_long = 1;
1607 JUMP (*++f, step3b_jumps);
1609 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1610 allowed to follow. */
1611 LABEL (mod_longlong):
1612 is_long_double = 1;
1613 is_long = 1;
1614 JUMP (*++f, step4_jumps);
1616 LABEL (mod_size_t):
1617 is_long_double = sizeof (size_t) > sizeof (unsigned long int);
1618 is_long = sizeof (size_t) > sizeof (unsigned int);
1619 JUMP (*++f, step4_jumps);
1621 LABEL (mod_ptrdiff_t):
1622 is_long_double = sizeof (ptrdiff_t) > sizeof (unsigned long int);
1623 is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
1624 JUMP (*++f, step4_jumps);
1626 LABEL (mod_intmax_t):
1627 is_long_double = sizeof (intmax_t) > sizeof (unsigned long int);
1628 is_long = sizeof (intmax_t) > sizeof (unsigned int);
1629 JUMP (*++f, step4_jumps);
1631 /* Process current format. */
1632 while (1)
1634 process_arg (((struct printf_spec *) NULL));
1635 process_string_arg (((struct printf_spec *) NULL));
1637 LABEL (form_unknown):
1638 if (spec == L_('\0'))
1640 /* The format string ended before the specifier is complete. */
1641 __set_errno (EINVAL);
1642 done = -1;
1643 goto all_done;
1646 /* If we are in the fast loop force entering the complicated
1647 one. */
1648 goto do_positional;
1651 /* The format is correctly handled. */
1652 ++nspecs_done;
1654 if (__glibc_unlikely (workstart != NULL))
1655 free (workstart);
1656 workstart = NULL;
1658 /* Look for next format specifier. */
1659 #ifdef COMPILE_WPRINTF
1660 f = __find_specwc ((end_of_spec = ++f));
1661 #else
1662 f = __find_specmb ((end_of_spec = ++f));
1663 #endif
1665 /* Write the following constant string. */
1666 outstring (end_of_spec, f - end_of_spec);
1668 while (*f != L_('\0'));
1670 /* Unlock stream and return. */
1671 goto all_done;
1673 /* Here starts the more complex loop to handle positional parameters. */
1674 do_positional:
1676 /* Array with information about the needed arguments. This has to
1677 be dynamically extensible. */
1678 size_t nspecs = 0;
1679 /* A more or less arbitrary start value. */
1680 size_t nspecs_size = 32 * sizeof (struct printf_spec);
1682 specs = alloca (nspecs_size);
1683 /* The number of arguments the format string requests. This will
1684 determine the size of the array needed to store the argument
1685 attributes. */
1686 size_t nargs = 0;
1687 size_t bytes_per_arg;
1688 union printf_arg *args_value;
1689 int *args_size;
1690 int *args_type;
1692 /* Positional parameters refer to arguments directly. This could
1693 also determine the maximum number of arguments. Track the
1694 maximum number. */
1695 size_t max_ref_arg = 0;
1697 /* Just a counter. */
1698 size_t cnt;
1700 if (__glibc_unlikely (workstart != NULL))
1701 free (workstart);
1702 workstart = NULL;
1704 if (grouping == (const char *) -1)
1706 #ifdef COMPILE_WPRINTF
1707 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1708 _NL_NUMERIC_THOUSANDS_SEP_WC);
1709 #else
1710 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1711 #endif
1713 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1714 if (*grouping == '\0' || *grouping == CHAR_MAX)
1715 grouping = NULL;
1718 for (f = lead_str_end; *f != L_('\0'); f = specs[nspecs++].next_fmt)
1720 if (nspecs * sizeof (*specs) >= nspecs_size)
1722 /* Extend the array of format specifiers. */
1723 if (nspecs_size * 2 < nspecs_size)
1725 __set_errno (ENOMEM);
1726 done = -1;
1727 goto all_done;
1729 struct printf_spec *old = specs;
1730 if (__libc_use_alloca (2 * nspecs_size))
1731 specs = extend_alloca (specs, nspecs_size, 2 * nspecs_size);
1732 else
1734 nspecs_size *= 2;
1735 specs = malloc (nspecs_size);
1736 if (specs == NULL)
1738 __set_errno (ENOMEM);
1739 specs = old;
1740 done = -1;
1741 goto all_done;
1745 /* Copy the old array's elements to the new space. */
1746 memmove (specs, old, nspecs * sizeof (*specs));
1748 /* If we had previously malloc'd space for SPECS, then
1749 release it after the copy is complete. */
1750 if (specs_malloced)
1751 free (old);
1753 /* Now set SPECS_MALLOCED if needed. */
1754 if (!__libc_use_alloca (nspecs_size))
1755 specs_malloced = true;
1758 /* Parse the format specifier. */
1759 #ifdef COMPILE_WPRINTF
1760 nargs += __parse_one_specwc (f, nargs, &specs[nspecs], &max_ref_arg);
1761 #else
1762 nargs += __parse_one_specmb (f, nargs, &specs[nspecs], &max_ref_arg);
1763 #endif
1766 /* Determine the number of arguments the format string consumes. */
1767 nargs = MAX (nargs, max_ref_arg);
1768 /* Calculate total size needed to represent a single argument across
1769 all three argument-related arrays. */
1770 bytes_per_arg = (sizeof (*args_value) + sizeof (*args_size)
1771 + sizeof (*args_type));
1773 /* Check for potential integer overflow. */
1774 if (__glibc_unlikely (nargs > INT_MAX / bytes_per_arg))
1776 __set_errno (EOVERFLOW);
1777 done = -1;
1778 goto all_done;
1781 /* Allocate memory for all three argument arrays. */
1782 if (__libc_use_alloca (nargs * bytes_per_arg))
1783 args_value = alloca (nargs * bytes_per_arg);
1784 else
1786 args_value = args_malloced = malloc (nargs * bytes_per_arg);
1787 if (args_value == NULL)
1789 done = -1;
1790 goto all_done;
1794 /* Set up the remaining two arrays to each point past the end of the
1795 prior array, since space for all three has been allocated now. */
1796 args_size = &args_value[nargs].pa_int;
1797 args_type = &args_size[nargs];
1798 memset (args_type, s->_flags2 & _IO_FLAGS2_FORTIFY ? '\xff' : '\0',
1799 nargs * sizeof (*args_type));
1801 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1802 still zero after this loop, format is invalid. For now we
1803 simply use 0 as the value. */
1805 /* Fill in the types of all the arguments. */
1806 for (cnt = 0; cnt < nspecs; ++cnt)
1808 /* If the width is determined by an argument this is an int. */
1809 if (specs[cnt].width_arg != -1)
1810 args_type[specs[cnt].width_arg] = PA_INT;
1812 /* If the precision is determined by an argument this is an int. */
1813 if (specs[cnt].prec_arg != -1)
1814 args_type[specs[cnt].prec_arg] = PA_INT;
1816 switch (specs[cnt].ndata_args)
1818 case 0: /* No arguments. */
1819 break;
1820 case 1: /* One argument; we already have the
1821 type and size. */
1822 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1823 args_size[specs[cnt].data_arg] = specs[cnt].size;
1824 break;
1825 default:
1826 /* We have more than one argument for this format spec.
1827 We must call the arginfo function again to determine
1828 all the types. */
1829 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1830 (&specs[cnt].info,
1831 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg],
1832 &args_size[specs[cnt].data_arg]);
1833 break;
1837 /* Now we know all the types and the order. Fill in the argument
1838 values. */
1839 for (cnt = 0; cnt < nargs; ++cnt)
1840 switch (args_type[cnt])
1842 #define T(tag, mem, type) \
1843 case tag: \
1844 args_value[cnt].mem = va_arg (ap_save, type); \
1845 break
1847 T (PA_WCHAR, pa_wchar, wint_t);
1848 case PA_CHAR: /* Promoted. */
1849 case PA_INT|PA_FLAG_SHORT: /* Promoted. */
1850 #if LONG_MAX == INT_MAX
1851 case PA_INT|PA_FLAG_LONG:
1852 #endif
1853 T (PA_INT, pa_int, int);
1854 #if LONG_MAX == LONG_LONG_MAX
1855 case PA_INT|PA_FLAG_LONG:
1856 #endif
1857 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1858 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1859 # error "he?"
1860 #endif
1861 case PA_FLOAT: /* Promoted. */
1862 T (PA_DOUBLE, pa_double, double);
1863 case PA_DOUBLE|PA_FLAG_LONG_DOUBLE:
1864 if (__ldbl_is_dbl)
1866 args_value[cnt].pa_double = va_arg (ap_save, double);
1867 args_type[cnt] &= ~PA_FLAG_LONG_DOUBLE;
1869 else
1870 args_value[cnt].pa_long_double = va_arg (ap_save, long double);
1871 break;
1872 case PA_STRING: /* All pointers are the same */
1873 case PA_WSTRING: /* All pointers are the same */
1874 T (PA_POINTER, pa_pointer, void *);
1875 #undef T
1876 default:
1877 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1878 args_value[cnt].pa_pointer = va_arg (ap_save, void *);
1879 else if (__glibc_unlikely (__printf_va_arg_table != NULL)
1880 && __printf_va_arg_table[args_type[cnt] - PA_LAST] != NULL)
1882 args_value[cnt].pa_user = alloca (args_size[cnt]);
1883 (*__printf_va_arg_table[args_type[cnt] - PA_LAST])
1884 (args_value[cnt].pa_user, &ap_save);
1886 else
1887 args_value[cnt].pa_long_double = 0.0;
1888 break;
1889 case -1:
1890 /* Error case. Not all parameters appear in N$ format
1891 strings. We have no way to determine their type. */
1892 assert (s->_flags2 & _IO_FLAGS2_FORTIFY);
1893 __libc_fatal ("*** invalid %N$ use detected ***\n");
1896 /* Now walk through all format specifiers and process them. */
1897 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1899 #undef REF
1900 #ifdef SHARED
1901 # undef JUMP_TABLE_BASE_LABEL
1902 # define JUMP_TABLE_BASE_LABEL do2_form_unknown
1903 # define REF(Name) (&&do2_##Name - &&JUMP_TABLE_BASE_LABEL)
1904 #else
1905 # define REF(Name) &&do2_##Name
1906 #endif
1907 #undef LABEL
1908 #define LABEL(Name) do2_##Name
1909 STEP4_TABLE;
1911 int is_negative;
1912 union
1914 unsigned long long int longlong;
1915 unsigned long int word;
1916 } number;
1917 int base;
1918 union printf_arg the_arg;
1919 CHAR_T *string; /* Pointer to argument string. */
1921 /* Fill variables from values in struct. */
1922 int alt = specs[nspecs_done].info.alt;
1923 int space = specs[nspecs_done].info.space;
1924 int left = specs[nspecs_done].info.left;
1925 int showsign = specs[nspecs_done].info.showsign;
1926 int group = specs[nspecs_done].info.group;
1927 int is_long_double = specs[nspecs_done].info.is_long_double;
1928 int is_short = specs[nspecs_done].info.is_short;
1929 int is_char = specs[nspecs_done].info.is_char;
1930 int is_long = specs[nspecs_done].info.is_long;
1931 int width = specs[nspecs_done].info.width;
1932 int prec = specs[nspecs_done].info.prec;
1933 int use_outdigits = specs[nspecs_done].info.i18n;
1934 char pad = specs[nspecs_done].info.pad;
1935 CHAR_T spec = specs[nspecs_done].info.spec;
1937 workstart = NULL;
1938 workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1940 /* Fill in last information. */
1941 if (specs[nspecs_done].width_arg != -1)
1943 /* Extract the field width from an argument. */
1944 specs[nspecs_done].info.width =
1945 args_value[specs[nspecs_done].width_arg].pa_int;
1947 if (specs[nspecs_done].info.width < 0)
1948 /* If the width value is negative left justification is
1949 selected and the value is taken as being positive. */
1951 specs[nspecs_done].info.width *= -1;
1952 left = specs[nspecs_done].info.left = 1;
1954 width = specs[nspecs_done].info.width;
1957 if (specs[nspecs_done].prec_arg != -1)
1959 /* Extract the precision from an argument. */
1960 specs[nspecs_done].info.prec =
1961 args_value[specs[nspecs_done].prec_arg].pa_int;
1963 if (specs[nspecs_done].info.prec < 0)
1964 /* If the precision is negative the precision is
1965 omitted. */
1966 specs[nspecs_done].info.prec = -1;
1968 prec = specs[nspecs_done].info.prec;
1971 /* Maybe the buffer is too small. */
1972 if (MAX (prec, width) + 32 > (int) (sizeof (work_buffer)
1973 / sizeof (CHAR_T)))
1975 if (__libc_use_alloca ((MAX (prec, width) + 32)
1976 * sizeof (CHAR_T)))
1977 workend = ((CHAR_T *) alloca ((MAX (prec, width) + 32)
1978 * sizeof (CHAR_T))
1979 + (MAX (prec, width) + 32));
1980 else
1982 workstart = (CHAR_T *) malloc ((MAX (prec, width) + 32)
1983 * sizeof (CHAR_T));
1984 if (workstart == NULL)
1986 done = -1;
1987 goto all_done;
1989 workend = workstart + (MAX (prec, width) + 32);
1993 /* Process format specifiers. */
1994 while (1)
1996 extern printf_function **__printf_function_table;
1997 int function_done;
1999 if (spec <= UCHAR_MAX
2000 && __printf_function_table != NULL
2001 && __printf_function_table[(size_t) spec] != NULL)
2003 const void **ptr = alloca (specs[nspecs_done].ndata_args
2004 * sizeof (const void *));
2006 /* Fill in an array of pointers to the argument values. */
2007 for (unsigned int i = 0; i < specs[nspecs_done].ndata_args;
2008 ++i)
2009 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
2011 /* Call the function. */
2012 function_done = __printf_function_table[(size_t) spec]
2013 (s, &specs[nspecs_done].info, ptr);
2015 if (function_done != -2)
2017 /* If an error occurred we don't have information
2018 about # of chars. */
2019 if (function_done < 0)
2021 /* Function has set errno. */
2022 done = -1;
2023 goto all_done;
2026 done_add (function_done);
2027 break;
2031 JUMP (spec, step4_jumps);
2033 process_arg ((&specs[nspecs_done]));
2034 process_string_arg ((&specs[nspecs_done]));
2036 LABEL (form_unknown):
2038 unsigned int i;
2039 const void **ptr;
2041 ptr = alloca (specs[nspecs_done].ndata_args
2042 * sizeof (const void *));
2044 /* Fill in an array of pointers to the argument values. */
2045 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
2046 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
2048 /* Call the function. */
2049 function_done = printf_unknown (s, &specs[nspecs_done].info,
2050 ptr);
2052 /* If an error occurred we don't have information about #
2053 of chars. */
2054 if (function_done < 0)
2056 /* Function has set errno. */
2057 done = -1;
2058 goto all_done;
2061 done_add (function_done);
2063 break;
2066 if (__glibc_unlikely (workstart != NULL))
2067 free (workstart);
2068 workstart = NULL;
2070 /* Write the following constant string. */
2071 outstring (specs[nspecs_done].end_of_fmt,
2072 specs[nspecs_done].next_fmt
2073 - specs[nspecs_done].end_of_fmt);
2077 all_done:
2078 if (specs_malloced)
2079 free (specs);
2080 if (__glibc_unlikely (args_malloced != NULL))
2081 free (args_malloced);
2082 if (__glibc_unlikely (workstart != NULL))
2083 free (workstart);
2084 /* Unlock the stream. */
2085 _IO_funlockfile (s);
2086 _IO_cleanup_region_end (0);
2088 return done;
2091 /* Handle an unknown format specifier. This prints out a canonicalized
2092 representation of the format spec itself. */
2093 static int
2094 printf_unknown (FILE *s, const struct printf_info *info,
2095 const void *const *args)
2098 int done = 0;
2099 CHAR_T work_buffer[MAX (sizeof (info->width), sizeof (info->prec)) * 3];
2100 CHAR_T *const workend
2101 = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
2102 CHAR_T *w;
2104 outchar (L_('%'));
2106 if (info->alt)
2107 outchar (L_('#'));
2108 if (info->group)
2109 outchar (L_('\''));
2110 if (info->showsign)
2111 outchar (L_('+'));
2112 else if (info->space)
2113 outchar (L_(' '));
2114 if (info->left)
2115 outchar (L_('-'));
2116 if (info->pad == L_('0'))
2117 outchar (L_('0'));
2118 if (info->i18n)
2119 outchar (L_('I'));
2121 if (info->width != 0)
2123 w = _itoa_word (info->width, workend, 10, 0);
2124 while (w < workend)
2125 outchar (*w++);
2128 if (info->prec != -1)
2130 outchar (L_('.'));
2131 w = _itoa_word (info->prec, workend, 10, 0);
2132 while (w < workend)
2133 outchar (*w++);
2136 if (info->spec != L_('\0'))
2137 outchar (info->spec);
2139 all_done:
2140 return done;
2143 /* Group the digits according to the grouping rules of the current locale.
2144 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
2145 static CHAR_T *
2146 internal_function
2147 group_number (CHAR_T *w, CHAR_T *rear_ptr, const char *grouping,
2148 THOUSANDS_SEP_T thousands_sep)
2150 int len;
2151 CHAR_T *src, *s;
2152 #ifndef COMPILE_WPRINTF
2153 int tlen = strlen (thousands_sep);
2154 #endif
2156 /* We treat all negative values like CHAR_MAX. */
2158 if (*grouping == CHAR_MAX || *grouping <= 0)
2159 /* No grouping should be done. */
2160 return w;
2162 len = *grouping++;
2164 /* Copy existing string so that nothing gets overwritten. */
2165 src = (CHAR_T *) alloca ((rear_ptr - w) * sizeof (CHAR_T));
2166 s = (CHAR_T *) __mempcpy (src, w,
2167 (rear_ptr - w) * sizeof (CHAR_T));
2168 w = rear_ptr;
2170 /* Process all characters in the string. */
2171 while (s > src)
2173 *--w = *--s;
2175 if (--len == 0 && s > src)
2177 /* A new group begins. */
2178 #ifdef COMPILE_WPRINTF
2179 *--w = thousands_sep;
2180 #else
2181 int cnt = tlen;
2183 *--w = thousands_sep[--cnt];
2184 while (cnt > 0);
2185 #endif
2187 if (*grouping == CHAR_MAX
2188 #if CHAR_MIN < 0
2189 || *grouping < 0
2190 #endif
2193 /* No further grouping to be done.
2194 Copy the rest of the number. */
2196 *--w = *--s;
2197 while (s > src);
2198 break;
2200 else if (*grouping != '\0')
2201 /* The previous grouping repeats ad infinitum. */
2202 len = *grouping++;
2203 else
2204 len = grouping[-1];
2207 return w;
2210 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2211 struct helper_file
2213 struct _IO_FILE_plus _f;
2214 #ifdef COMPILE_WPRINTF
2215 struct _IO_wide_data _wide_data;
2216 #endif
2217 _IO_FILE *_put_stream;
2218 #ifdef _IO_MTSAFE_IO
2219 _IO_lock_t lock;
2220 #endif
2223 static int
2224 _IO_helper_overflow (_IO_FILE *s, int c)
2226 _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
2227 #ifdef COMPILE_WPRINTF
2228 int used = s->_wide_data->_IO_write_ptr - s->_wide_data->_IO_write_base;
2229 if (used)
2231 _IO_size_t written = _IO_sputn (target, s->_wide_data->_IO_write_base,
2232 used);
2233 if (written == 0 || written == WEOF)
2234 return WEOF;
2235 __wmemmove (s->_wide_data->_IO_write_base,
2236 s->_wide_data->_IO_write_base + written,
2237 used - written);
2238 s->_wide_data->_IO_write_ptr -= written;
2240 #else
2241 int used = s->_IO_write_ptr - s->_IO_write_base;
2242 if (used)
2244 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
2245 if (written == 0 || written == EOF)
2246 return EOF;
2247 memmove (s->_IO_write_base, s->_IO_write_base + written,
2248 used - written);
2249 s->_IO_write_ptr -= written;
2251 #endif
2252 return PUTC (c, s);
2255 #ifdef COMPILE_WPRINTF
2256 static const struct _IO_jump_t _IO_helper_jumps =
2258 JUMP_INIT_DUMMY,
2259 JUMP_INIT (finish, _IO_wdefault_finish),
2260 JUMP_INIT (overflow, _IO_helper_overflow),
2261 JUMP_INIT (underflow, _IO_default_underflow),
2262 JUMP_INIT (uflow, _IO_default_uflow),
2263 JUMP_INIT (pbackfail, (_IO_pbackfail_t) _IO_wdefault_pbackfail),
2264 JUMP_INIT (xsputn, _IO_wdefault_xsputn),
2265 JUMP_INIT (xsgetn, _IO_wdefault_xsgetn),
2266 JUMP_INIT (seekoff, _IO_default_seekoff),
2267 JUMP_INIT (seekpos, _IO_default_seekpos),
2268 JUMP_INIT (setbuf, _IO_default_setbuf),
2269 JUMP_INIT (sync, _IO_default_sync),
2270 JUMP_INIT (doallocate, _IO_wdefault_doallocate),
2271 JUMP_INIT (read, _IO_default_read),
2272 JUMP_INIT (write, _IO_default_write),
2273 JUMP_INIT (seek, _IO_default_seek),
2274 JUMP_INIT (close, _IO_default_close),
2275 JUMP_INIT (stat, _IO_default_stat)
2277 #else
2278 static const struct _IO_jump_t _IO_helper_jumps =
2280 JUMP_INIT_DUMMY,
2281 JUMP_INIT (finish, _IO_default_finish),
2282 JUMP_INIT (overflow, _IO_helper_overflow),
2283 JUMP_INIT (underflow, _IO_default_underflow),
2284 JUMP_INIT (uflow, _IO_default_uflow),
2285 JUMP_INIT (pbackfail, _IO_default_pbackfail),
2286 JUMP_INIT (xsputn, _IO_default_xsputn),
2287 JUMP_INIT (xsgetn, _IO_default_xsgetn),
2288 JUMP_INIT (seekoff, _IO_default_seekoff),
2289 JUMP_INIT (seekpos, _IO_default_seekpos),
2290 JUMP_INIT (setbuf, _IO_default_setbuf),
2291 JUMP_INIT (sync, _IO_default_sync),
2292 JUMP_INIT (doallocate, _IO_default_doallocate),
2293 JUMP_INIT (read, _IO_default_read),
2294 JUMP_INIT (write, _IO_default_write),
2295 JUMP_INIT (seek, _IO_default_seek),
2296 JUMP_INIT (close, _IO_default_close),
2297 JUMP_INIT (stat, _IO_default_stat)
2299 #endif
2301 static int
2302 internal_function
2303 buffered_vfprintf (_IO_FILE *s, const CHAR_T *format,
2304 _IO_va_list args)
2306 CHAR_T buf[_IO_BUFSIZ];
2307 struct helper_file helper;
2308 _IO_FILE *hp = (_IO_FILE *) &helper._f;
2309 int result, to_flush;
2311 /* Orient the stream. */
2312 #ifdef ORIENT
2313 ORIENT;
2314 #endif
2316 /* Initialize helper. */
2317 helper._put_stream = s;
2318 #ifdef COMPILE_WPRINTF
2319 hp->_wide_data = &helper._wide_data;
2320 _IO_wsetp (hp, buf, buf + sizeof buf / sizeof (CHAR_T));
2321 hp->_mode = 1;
2322 #else
2323 _IO_setp (hp, buf, buf + sizeof buf);
2324 hp->_mode = -1;
2325 #endif
2326 hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS|_IO_USER_LOCK;
2327 #if _IO_JUMPS_OFFSET
2328 hp->_vtable_offset = 0;
2329 #endif
2330 #ifdef _IO_MTSAFE_IO
2331 hp->_lock = NULL;
2332 #endif
2333 hp->_flags2 = s->_flags2;
2334 _IO_JUMPS (&helper._f) = (struct _IO_jump_t *) &_IO_helper_jumps;
2336 /* Now print to helper instead. */
2337 #ifndef COMPILE_WPRINTF
2338 result = _IO_vfprintf (hp, format, args);
2339 #else
2340 result = vfprintf (hp, format, args);
2341 #endif
2343 /* Lock stream. */
2344 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, s);
2345 _IO_flockfile (s);
2347 /* Now flush anything from the helper to the S. */
2348 #ifdef COMPILE_WPRINTF
2349 if ((to_flush = (hp->_wide_data->_IO_write_ptr
2350 - hp->_wide_data->_IO_write_base)) > 0)
2352 if ((int) _IO_sputn (s, hp->_wide_data->_IO_write_base, to_flush)
2353 != to_flush)
2354 result = -1;
2356 #else
2357 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
2359 if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
2360 result = -1;
2362 #endif
2364 /* Unlock the stream. */
2365 _IO_funlockfile (s);
2366 __libc_cleanup_region_end (0);
2368 return result;
2371 #undef vfprintf
2372 #ifdef COMPILE_WPRINTF
2373 strong_alias (_IO_vfwprintf, __vfwprintf);
2374 ldbl_weak_alias (_IO_vfwprintf, vfwprintf);
2375 #else
2376 ldbl_strong_alias (_IO_vfprintf_internal, vfprintf);
2377 ldbl_hidden_def (_IO_vfprintf_internal, vfprintf)
2378 ldbl_strong_alias (_IO_vfprintf_internal, _IO_vfprintf);
2379 ldbl_hidden_def (_IO_vfprintf_internal, _IO_vfprintf)
2380 #endif