improve out-of-bounds checking with GCC 10 attribute access [BZ #25219]
[glibc.git] / stdio-common / vfprintf-internal.c
blob7cb9333c533fb8a0e5b23a3fc2e9239a548dd707
1 /* Copyright (C) 1991-2020 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 <https://www.gnu.org/licenses/>. */
18 #include <array_length.h>
19 #include <ctype.h>
20 #include <limits.h>
21 #include <printf.h>
22 #include <stdarg.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <wchar.h>
28 #include <libc-lock.h>
29 #include <sys/param.h>
30 #include <_itoa.h>
31 #include <locale/localeinfo.h>
32 #include <stdio.h>
33 #include <scratch_buffer.h>
34 #include <intprops.h>
36 /* This code is shared between the standard stdio implementation found
37 in GNU C library and the libio implementation originally found in
38 GNU libg++.
40 Beside this it is also shared between the normal and wide character
41 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
43 #include <libioP.h>
45 #ifdef COMPILE_WPRINTF
46 #include <wctype.h>
47 #endif
49 /* In some cases we need extra space for all the output which is not
50 counted in the width of the string. We assume 32 characters is
51 enough. */
52 #define EXTSIZ 32
53 #define ARGCHECK(S, Format) \
54 do \
55 { \
56 /* Check file argument for consistence. */ \
57 CHECK_FILE (S, -1); \
58 if (S->_flags & _IO_NO_WRITES) \
59 { \
60 S->_flags |= _IO_ERR_SEEN; \
61 __set_errno (EBADF); \
62 return -1; \
63 } \
64 if (Format == NULL) \
65 { \
66 __set_errno (EINVAL); \
67 return -1; \
68 } \
69 } while (0)
70 #define UNBUFFERED_P(S) ((S)->_flags & _IO_UNBUFFERED)
72 #if __HAVE_FLOAT128_UNLIKE_LDBL
73 # define PARSE_FLOAT_VA_ARG_EXTENDED(INFO) \
74 do \
75 { \
76 if (is_long_double \
77 && (mode_flags & PRINTF_LDBL_USES_FLOAT128) != 0) \
78 { \
79 INFO.is_binary128 = 1; \
80 the_arg.pa_float128 = va_arg (ap, _Float128); \
81 } \
82 else \
83 { \
84 PARSE_FLOAT_VA_ARG (INFO); \
85 } \
86 } \
87 while (0)
88 #else
89 # define PARSE_FLOAT_VA_ARG_EXTENDED(INFO) \
90 PARSE_FLOAT_VA_ARG (INFO);
91 #endif
93 #define PARSE_FLOAT_VA_ARG(INFO) \
94 do \
95 { \
96 INFO.is_binary128 = 0; \
97 if (is_long_double) \
98 the_arg.pa_long_double = va_arg (ap, long double); \
99 else \
100 the_arg.pa_double = va_arg (ap, double); \
102 while (0)
104 #if __HAVE_FLOAT128_UNLIKE_LDBL
105 # define SETUP_FLOAT128_INFO(INFO) \
106 do \
108 if ((mode_flags & PRINTF_LDBL_USES_FLOAT128) != 0) \
109 INFO.is_binary128 = is_long_double; \
110 else \
111 INFO.is_binary128 = 0; \
113 while (0)
114 #else
115 # define SETUP_FLOAT128_INFO(INFO) \
116 do \
118 INFO.is_binary128 = 0; \
120 while (0)
121 #endif
123 /* Add LENGTH to DONE. Return the new value of DONE, or -1 on
124 overflow (and set errno accordingly). */
125 static inline int
126 done_add_func (size_t length, int done)
128 if (done < 0)
129 return done;
130 int ret;
131 if (INT_ADD_WRAPV (done, length, &ret))
133 __set_errno (EOVERFLOW);
134 return -1;
136 return ret;
139 #define done_add(val) \
140 do \
142 /* Ensure that VAL has a type similar to int. */ \
143 _Static_assert (sizeof (val) == sizeof (int), "value int size"); \
144 _Static_assert ((__typeof__ (val)) -1 < 0, "value signed"); \
145 done = done_add_func ((val), done); \
146 if (done < 0) \
147 goto all_done; \
149 while (0)
151 #ifndef COMPILE_WPRINTF
152 # define vfprintf __vfprintf_internal
153 # define CHAR_T char
154 # define OTHER_CHAR_T wchar_t
155 # define UCHAR_T unsigned char
156 # define INT_T int
157 typedef const char *THOUSANDS_SEP_T;
158 # define L_(Str) Str
159 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
160 # define STR_LEN(Str) strlen (Str)
162 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
163 # define PUTC(C, F) _IO_putc_unlocked (C, F)
164 # define ORIENT if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
165 return -1
166 # define CONVERT_FROM_OTHER_STRING __wcsrtombs
167 #else
168 # define vfprintf __vfwprintf_internal
169 # define CHAR_T wchar_t
170 # define OTHER_CHAR_T char
171 /* This is a hack!!! There should be a type uwchar_t. */
172 # define UCHAR_T unsigned int /* uwchar_t */
173 # define INT_T wint_t
174 typedef wchar_t THOUSANDS_SEP_T;
175 # define L_(Str) L##Str
176 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
177 # define STR_LEN(Str) __wcslen (Str)
179 # include <_itowa.h>
181 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
182 # define PUTC(C, F) _IO_putwc_unlocked (C, F)
183 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
184 # define CONVERT_FROM_OTHER_STRING __mbsrtowcs
186 # undef _itoa
187 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
188 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
189 # undef EOF
190 # define EOF WEOF
191 #endif
193 static inline int
194 pad_func (FILE *s, CHAR_T padchar, int width, int done)
196 if (width > 0)
198 ssize_t written;
199 #ifndef COMPILE_WPRINTF
200 written = _IO_padn (s, padchar, width);
201 #else
202 written = _IO_wpadn (s, padchar, width);
203 #endif
204 if (__glibc_unlikely (written != width))
205 return -1;
206 return done_add_func (width, done);
208 return done;
211 #define PAD(Padchar) \
212 do \
214 done = pad_func (s, (Padchar), width, done); \
215 if (done < 0) \
216 goto all_done; \
218 while (0)
220 #include "_i18n_number.h"
222 /* Include the shared code for parsing the format string. */
223 #include "printf-parse.h"
226 #define outchar(Ch) \
227 do \
229 const INT_T outc = (Ch); \
230 if (PUTC (outc, s) == EOF || done == INT_MAX) \
232 done = -1; \
233 goto all_done; \
235 ++done; \
237 while (0)
239 static inline int
240 outstring_func (FILE *s, const UCHAR_T *string, size_t length, int done)
242 assert ((size_t) done <= (size_t) INT_MAX);
243 if ((size_t) PUT (s, string, length) != (size_t) (length))
244 return -1;
245 return done_add_func (length, done);
248 #define outstring(String, Len) \
249 do \
251 const void *string_ = (String); \
252 done = outstring_func (s, string_, (Len), done); \
253 if (done < 0) \
254 goto all_done; \
256 while (0)
258 /* Write the string SRC to S. If PREC is non-negative, write at most
259 PREC bytes. If LEFT is true, perform left justification. */
260 static int
261 outstring_converted_wide_string (FILE *s, const OTHER_CHAR_T *src, int prec,
262 int width, bool left, int done)
264 /* Use a small buffer to combine processing of multiple characters.
265 CONVERT_FROM_OTHER_STRING expects the buffer size in (wide)
266 characters, and buf_length counts that. */
267 enum { buf_length = 256 / sizeof (CHAR_T) };
268 CHAR_T buf[buf_length];
269 _Static_assert (sizeof (buf) > MB_LEN_MAX,
270 "buffer is large enough for a single multi-byte character");
272 /* Add the initial padding if needed. */
273 if (width > 0 && !left)
275 /* Make a first pass to find the output width, so that we can
276 add the required padding. */
277 mbstate_t mbstate = { 0 };
278 const OTHER_CHAR_T *src_copy = src;
279 size_t total_written;
280 if (prec < 0)
281 total_written = CONVERT_FROM_OTHER_STRING
282 (NULL, &src_copy, 0, &mbstate);
283 else
285 /* The source might not be null-terminated. Enforce the
286 limit manually, based on the output length. */
287 total_written = 0;
288 size_t limit = prec;
289 while (limit > 0 && src_copy != NULL)
291 size_t write_limit = buf_length;
292 if (write_limit > limit)
293 write_limit = limit;
294 size_t written = CONVERT_FROM_OTHER_STRING
295 (buf, &src_copy, write_limit, &mbstate);
296 if (written == (size_t) -1)
297 return -1;
298 if (written == 0)
299 break;
300 total_written += written;
301 limit -= written;
305 /* Output initial padding. */
306 if (total_written < width)
308 done = pad_func (s, L_(' '), width - total_written, done);
309 if (done < 0)
310 return done;
314 /* Convert the input string, piece by piece. */
315 size_t total_written = 0;
317 mbstate_t mbstate = { 0 };
318 /* If prec is negative, remaining is not decremented, otherwise,
319 it serves as the write limit. */
320 size_t remaining = -1;
321 if (prec >= 0)
322 remaining = prec;
323 while (remaining > 0 && src != NULL)
325 size_t write_limit = buf_length;
326 if (remaining < write_limit)
327 write_limit = remaining;
328 size_t written = CONVERT_FROM_OTHER_STRING
329 (buf, &src, write_limit, &mbstate);
330 if (written == (size_t) -1)
331 return -1;
332 if (written == 0)
333 break;
334 done = outstring_func (s, (const UCHAR_T *) buf, written, done);
335 if (done < 0)
336 return done;
337 total_written += written;
338 if (prec >= 0)
339 remaining -= written;
343 /* Add final padding. */
344 if (width > 0 && left && total_written < width)
345 return pad_func (s, L_(' '), width - total_written, done);
346 return done;
349 /* For handling long_double and longlong we use the same flag. If
350 `long' and `long long' are effectively the same type define it to
351 zero. */
352 #if LONG_MAX == LONG_LONG_MAX
353 # define is_longlong 0
354 #else
355 # define is_longlong is_long_double
356 #endif
358 /* If `long' and `int' is effectively the same type we don't have to
359 handle `long separately. */
360 #if INT_MAX == LONG_MAX
361 # define is_long_num 0
362 #else
363 # define is_long_num is_long
364 #endif
367 /* Global constants. */
368 static const CHAR_T null[] = L_("(null)");
370 /* Size of the work_buffer variable (in characters, not bytes. */
371 enum { WORK_BUFFER_SIZE = 1000 / sizeof (CHAR_T) };
373 /* This table maps a character into a number representing a class. In
374 each step there is a destination label for each class. */
375 static const uint8_t jump_table[] =
377 /* ' ' */ 1, 0, 0, /* '#' */ 4,
378 0, /* '%' */ 14, 0, /* '\''*/ 6,
379 0, 0, /* '*' */ 7, /* '+' */ 2,
380 0, /* '-' */ 3, /* '.' */ 9, 0,
381 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
382 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
383 /* '8' */ 8, /* '9' */ 8, 0, 0,
384 0, 0, 0, 0,
385 0, /* 'A' */ 26, 0, /* 'C' */ 25,
386 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
387 0, /* 'I' */ 29, 0, 0,
388 /* 'L' */ 12, 0, 0, 0,
389 0, 0, 0, /* 'S' */ 21,
390 0, 0, 0, 0,
391 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
392 0, 0, 0, 0,
393 0, /* 'a' */ 26, 0, /* 'c' */ 20,
394 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
395 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
396 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
397 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
398 /* 't' */ 27, /* 'u' */ 16, 0, 0,
399 /* 'x' */ 18, 0, /* 'z' */ 13
402 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
403 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
404 #define LABEL(Name) do_##Name
405 #ifdef SHARED
406 /* 'int' is enough and it saves some space on 64 bit systems. */
407 # define JUMP_TABLE_TYPE const int
408 # define JUMP_TABLE_BASE_LABEL do_form_unknown
409 # define REF(Name) &&do_##Name - &&JUMP_TABLE_BASE_LABEL
410 # define JUMP(ChExpr, table) \
411 do \
413 int offset; \
414 void *ptr; \
415 spec = (ChExpr); \
416 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
417 : table[CHAR_CLASS (spec)]; \
418 ptr = &&JUMP_TABLE_BASE_LABEL + offset; \
419 goto *ptr; \
421 while (0)
422 #else
423 # define JUMP_TABLE_TYPE const void *const
424 # define REF(Name) &&do_##Name
425 # define JUMP(ChExpr, table) \
426 do \
428 const void *ptr; \
429 spec = (ChExpr); \
430 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
431 : table[CHAR_CLASS (spec)]; \
432 goto *ptr; \
434 while (0)
435 #endif
437 #define STEP0_3_TABLE \
438 /* Step 0: at the beginning. */ \
439 static JUMP_TABLE_TYPE step0_jumps[30] = \
441 REF (form_unknown), \
442 REF (flag_space), /* for ' ' */ \
443 REF (flag_plus), /* for '+' */ \
444 REF (flag_minus), /* for '-' */ \
445 REF (flag_hash), /* for '<hash>' */ \
446 REF (flag_zero), /* for '0' */ \
447 REF (flag_quote), /* for '\'' */ \
448 REF (width_asterics), /* for '*' */ \
449 REF (width), /* for '1'...'9' */ \
450 REF (precision), /* for '.' */ \
451 REF (mod_half), /* for 'h' */ \
452 REF (mod_long), /* for 'l' */ \
453 REF (mod_longlong), /* for 'L', 'q' */ \
454 REF (mod_size_t), /* for 'z', 'Z' */ \
455 REF (form_percent), /* for '%' */ \
456 REF (form_integer), /* for 'd', 'i' */ \
457 REF (form_unsigned), /* for 'u' */ \
458 REF (form_octal), /* for 'o' */ \
459 REF (form_hexa), /* for 'X', 'x' */ \
460 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
461 REF (form_character), /* for 'c' */ \
462 REF (form_string), /* for 's', 'S' */ \
463 REF (form_pointer), /* for 'p' */ \
464 REF (form_number), /* for 'n' */ \
465 REF (form_strerror), /* for 'm' */ \
466 REF (form_wcharacter), /* for 'C' */ \
467 REF (form_floathex), /* for 'A', 'a' */ \
468 REF (mod_ptrdiff_t), /* for 't' */ \
469 REF (mod_intmax_t), /* for 'j' */ \
470 REF (flag_i18n), /* for 'I' */ \
471 }; \
472 /* Step 1: after processing width. */ \
473 static JUMP_TABLE_TYPE step1_jumps[30] = \
475 REF (form_unknown), \
476 REF (form_unknown), /* for ' ' */ \
477 REF (form_unknown), /* for '+' */ \
478 REF (form_unknown), /* for '-' */ \
479 REF (form_unknown), /* for '<hash>' */ \
480 REF (form_unknown), /* for '0' */ \
481 REF (form_unknown), /* for '\'' */ \
482 REF (form_unknown), /* for '*' */ \
483 REF (form_unknown), /* for '1'...'9' */ \
484 REF (precision), /* for '.' */ \
485 REF (mod_half), /* for 'h' */ \
486 REF (mod_long), /* for 'l' */ \
487 REF (mod_longlong), /* for 'L', 'q' */ \
488 REF (mod_size_t), /* for 'z', 'Z' */ \
489 REF (form_percent), /* for '%' */ \
490 REF (form_integer), /* for 'd', 'i' */ \
491 REF (form_unsigned), /* for 'u' */ \
492 REF (form_octal), /* for 'o' */ \
493 REF (form_hexa), /* for 'X', 'x' */ \
494 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
495 REF (form_character), /* for 'c' */ \
496 REF (form_string), /* for 's', 'S' */ \
497 REF (form_pointer), /* for 'p' */ \
498 REF (form_number), /* for 'n' */ \
499 REF (form_strerror), /* for 'm' */ \
500 REF (form_wcharacter), /* for 'C' */ \
501 REF (form_floathex), /* for 'A', 'a' */ \
502 REF (mod_ptrdiff_t), /* for 't' */ \
503 REF (mod_intmax_t), /* for 'j' */ \
504 REF (form_unknown) /* for 'I' */ \
505 }; \
506 /* Step 2: after processing precision. */ \
507 static JUMP_TABLE_TYPE step2_jumps[30] = \
509 REF (form_unknown), \
510 REF (form_unknown), /* for ' ' */ \
511 REF (form_unknown), /* for '+' */ \
512 REF (form_unknown), /* for '-' */ \
513 REF (form_unknown), /* for '<hash>' */ \
514 REF (form_unknown), /* for '0' */ \
515 REF (form_unknown), /* for '\'' */ \
516 REF (form_unknown), /* for '*' */ \
517 REF (form_unknown), /* for '1'...'9' */ \
518 REF (form_unknown), /* for '.' */ \
519 REF (mod_half), /* for 'h' */ \
520 REF (mod_long), /* for 'l' */ \
521 REF (mod_longlong), /* for 'L', 'q' */ \
522 REF (mod_size_t), /* for 'z', 'Z' */ \
523 REF (form_percent), /* for '%' */ \
524 REF (form_integer), /* for 'd', 'i' */ \
525 REF (form_unsigned), /* for 'u' */ \
526 REF (form_octal), /* for 'o' */ \
527 REF (form_hexa), /* for 'X', 'x' */ \
528 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
529 REF (form_character), /* for 'c' */ \
530 REF (form_string), /* for 's', 'S' */ \
531 REF (form_pointer), /* for 'p' */ \
532 REF (form_number), /* for 'n' */ \
533 REF (form_strerror), /* for 'm' */ \
534 REF (form_wcharacter), /* for 'C' */ \
535 REF (form_floathex), /* for 'A', 'a' */ \
536 REF (mod_ptrdiff_t), /* for 't' */ \
537 REF (mod_intmax_t), /* for 'j' */ \
538 REF (form_unknown) /* for 'I' */ \
539 }; \
540 /* Step 3a: after processing first 'h' modifier. */ \
541 static JUMP_TABLE_TYPE step3a_jumps[30] = \
543 REF (form_unknown), \
544 REF (form_unknown), /* for ' ' */ \
545 REF (form_unknown), /* for '+' */ \
546 REF (form_unknown), /* for '-' */ \
547 REF (form_unknown), /* for '<hash>' */ \
548 REF (form_unknown), /* for '0' */ \
549 REF (form_unknown), /* for '\'' */ \
550 REF (form_unknown), /* for '*' */ \
551 REF (form_unknown), /* for '1'...'9' */ \
552 REF (form_unknown), /* for '.' */ \
553 REF (mod_halfhalf), /* for 'h' */ \
554 REF (form_unknown), /* for 'l' */ \
555 REF (form_unknown), /* for 'L', 'q' */ \
556 REF (form_unknown), /* for 'z', 'Z' */ \
557 REF (form_percent), /* for '%' */ \
558 REF (form_integer), /* for 'd', 'i' */ \
559 REF (form_unsigned), /* for 'u' */ \
560 REF (form_octal), /* for 'o' */ \
561 REF (form_hexa), /* for 'X', 'x' */ \
562 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
563 REF (form_unknown), /* for 'c' */ \
564 REF (form_unknown), /* for 's', 'S' */ \
565 REF (form_unknown), /* for 'p' */ \
566 REF (form_number), /* for 'n' */ \
567 REF (form_unknown), /* for 'm' */ \
568 REF (form_unknown), /* for 'C' */ \
569 REF (form_unknown), /* for 'A', 'a' */ \
570 REF (form_unknown), /* for 't' */ \
571 REF (form_unknown), /* for 'j' */ \
572 REF (form_unknown) /* for 'I' */ \
573 }; \
574 /* Step 3b: after processing first 'l' modifier. */ \
575 static JUMP_TABLE_TYPE step3b_jumps[30] = \
577 REF (form_unknown), \
578 REF (form_unknown), /* for ' ' */ \
579 REF (form_unknown), /* for '+' */ \
580 REF (form_unknown), /* for '-' */ \
581 REF (form_unknown), /* for '<hash>' */ \
582 REF (form_unknown), /* for '0' */ \
583 REF (form_unknown), /* for '\'' */ \
584 REF (form_unknown), /* for '*' */ \
585 REF (form_unknown), /* for '1'...'9' */ \
586 REF (form_unknown), /* for '.' */ \
587 REF (form_unknown), /* for 'h' */ \
588 REF (mod_longlong), /* for 'l' */ \
589 REF (form_unknown), /* for 'L', 'q' */ \
590 REF (form_unknown), /* for 'z', 'Z' */ \
591 REF (form_percent), /* for '%' */ \
592 REF (form_integer), /* for 'd', 'i' */ \
593 REF (form_unsigned), /* for 'u' */ \
594 REF (form_octal), /* for 'o' */ \
595 REF (form_hexa), /* for 'X', 'x' */ \
596 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
597 REF (form_character), /* for 'c' */ \
598 REF (form_string), /* for 's', 'S' */ \
599 REF (form_pointer), /* for 'p' */ \
600 REF (form_number), /* for 'n' */ \
601 REF (form_strerror), /* for 'm' */ \
602 REF (form_wcharacter), /* for 'C' */ \
603 REF (form_floathex), /* for 'A', 'a' */ \
604 REF (form_unknown), /* for 't' */ \
605 REF (form_unknown), /* for 'j' */ \
606 REF (form_unknown) /* for 'I' */ \
609 #define STEP4_TABLE \
610 /* Step 4: processing format specifier. */ \
611 static JUMP_TABLE_TYPE step4_jumps[30] = \
613 REF (form_unknown), \
614 REF (form_unknown), /* for ' ' */ \
615 REF (form_unknown), /* for '+' */ \
616 REF (form_unknown), /* for '-' */ \
617 REF (form_unknown), /* for '<hash>' */ \
618 REF (form_unknown), /* for '0' */ \
619 REF (form_unknown), /* for '\'' */ \
620 REF (form_unknown), /* for '*' */ \
621 REF (form_unknown), /* for '1'...'9' */ \
622 REF (form_unknown), /* for '.' */ \
623 REF (form_unknown), /* for 'h' */ \
624 REF (form_unknown), /* for 'l' */ \
625 REF (form_unknown), /* for 'L', 'q' */ \
626 REF (form_unknown), /* for 'z', 'Z' */ \
627 REF (form_percent), /* for '%' */ \
628 REF (form_integer), /* for 'd', 'i' */ \
629 REF (form_unsigned), /* for 'u' */ \
630 REF (form_octal), /* for 'o' */ \
631 REF (form_hexa), /* for 'X', 'x' */ \
632 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
633 REF (form_character), /* for 'c' */ \
634 REF (form_string), /* for 's', 'S' */ \
635 REF (form_pointer), /* for 'p' */ \
636 REF (form_number), /* for 'n' */ \
637 REF (form_strerror), /* for 'm' */ \
638 REF (form_wcharacter), /* for 'C' */ \
639 REF (form_floathex), /* for 'A', 'a' */ \
640 REF (form_unknown), /* for 't' */ \
641 REF (form_unknown), /* for 'j' */ \
642 REF (form_unknown) /* for 'I' */ \
646 #define process_arg(fspec) \
647 /* Start real work. We know about all flags and modifiers and \
648 now process the wanted format specifier. */ \
649 LABEL (form_percent): \
650 /* Write a literal "%". */ \
651 outchar (L_('%')); \
652 break; \
654 LABEL (form_integer): \
655 /* Signed decimal integer. */ \
656 base = 10; \
658 if (is_longlong) \
660 long long int signed_number; \
662 if (fspec == NULL) \
663 signed_number = va_arg (ap, long long int); \
664 else \
665 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
667 is_negative = signed_number < 0; \
668 number.longlong = is_negative ? (- signed_number) : signed_number; \
670 goto LABEL (longlong_number); \
672 else \
674 long int signed_number; \
676 if (fspec == NULL) \
678 if (is_long_num) \
679 signed_number = va_arg (ap, long int); \
680 else if (is_char) \
681 signed_number = (signed char) va_arg (ap, unsigned int); \
682 else if (!is_short) \
683 signed_number = va_arg (ap, int); \
684 else \
685 signed_number = (short int) va_arg (ap, unsigned int); \
687 else \
688 if (is_long_num) \
689 signed_number = args_value[fspec->data_arg].pa_long_int; \
690 else if (is_char) \
691 signed_number = (signed char) \
692 args_value[fspec->data_arg].pa_u_int; \
693 else if (!is_short) \
694 signed_number = args_value[fspec->data_arg].pa_int; \
695 else \
696 signed_number = (short int) \
697 args_value[fspec->data_arg].pa_u_int; \
699 is_negative = signed_number < 0; \
700 number.word = is_negative ? (- signed_number) : signed_number; \
702 goto LABEL (number); \
704 /* NOTREACHED */ \
706 LABEL (form_unsigned): \
707 /* Unsigned decimal integer. */ \
708 base = 10; \
709 goto LABEL (unsigned_number); \
710 /* NOTREACHED */ \
712 LABEL (form_octal): \
713 /* Unsigned octal integer. */ \
714 base = 8; \
715 goto LABEL (unsigned_number); \
716 /* NOTREACHED */ \
718 LABEL (form_hexa): \
719 /* Unsigned hexadecimal integer. */ \
720 base = 16; \
722 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
724 /* ISO specifies the `+' and ` ' flags only for signed \
725 conversions. */ \
726 is_negative = 0; \
727 showsign = 0; \
728 space = 0; \
730 if (is_longlong) \
732 if (fspec == NULL) \
733 number.longlong = va_arg (ap, unsigned long long int); \
734 else \
735 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
737 LABEL (longlong_number): \
738 if (prec < 0) \
739 /* Supply a default precision if none was given. */ \
740 prec = 1; \
741 else \
742 /* We have to take care for the '0' flag. If a precision \
743 is given it must be ignored. */ \
744 pad = L_(' '); \
746 /* If the precision is 0 and the number is 0 nothing has to \
747 be written for the number, except for the 'o' format in \
748 alternate form. */ \
749 if (prec == 0 && number.longlong == 0) \
751 string = workend; \
752 if (base == 8 && alt) \
753 *--string = L_('0'); \
755 else \
757 /* Put the number in WORK. */ \
758 string = _itoa (number.longlong, workend, base, \
759 spec == L_('X')); \
760 if (group && grouping) \
761 string = group_number (work_buffer, string, workend, \
762 grouping, thousands_sep); \
763 if (use_outdigits && base == 10) \
764 string = _i18n_number_rewrite (string, workend, workend); \
766 /* Simplify further test for num != 0. */ \
767 number.word = number.longlong != 0; \
769 else \
771 if (fspec == NULL) \
773 if (is_long_num) \
774 number.word = va_arg (ap, unsigned long int); \
775 else if (is_char) \
776 number.word = (unsigned char) va_arg (ap, unsigned int); \
777 else if (!is_short) \
778 number.word = va_arg (ap, unsigned int); \
779 else \
780 number.word = (unsigned short int) va_arg (ap, unsigned int); \
782 else \
783 if (is_long_num) \
784 number.word = args_value[fspec->data_arg].pa_u_long_int; \
785 else if (is_char) \
786 number.word = (unsigned char) \
787 args_value[fspec->data_arg].pa_u_int; \
788 else if (!is_short) \
789 number.word = args_value[fspec->data_arg].pa_u_int; \
790 else \
791 number.word = (unsigned short int) \
792 args_value[fspec->data_arg].pa_u_int; \
794 LABEL (number): \
795 if (prec < 0) \
796 /* Supply a default precision if none was given. */ \
797 prec = 1; \
798 else \
799 /* We have to take care for the '0' flag. If a precision \
800 is given it must be ignored. */ \
801 pad = L_(' '); \
803 /* If the precision is 0 and the number is 0 nothing has to \
804 be written for the number, except for the 'o' format in \
805 alternate form. */ \
806 if (prec == 0 && number.word == 0) \
808 string = workend; \
809 if (base == 8 && alt) \
810 *--string = L_('0'); \
812 else \
814 /* Put the number in WORK. */ \
815 string = _itoa_word (number.word, workend, base, \
816 spec == L_('X')); \
817 if (group && grouping) \
818 string = group_number (work_buffer, string, workend, \
819 grouping, thousands_sep); \
820 if (use_outdigits && base == 10) \
821 string = _i18n_number_rewrite (string, workend, workend); \
825 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
826 /* Add octal marker. */ \
827 *--string = L_('0'); \
829 prec = MAX (0, prec - (workend - string)); \
831 if (!left) \
833 width -= workend - string + prec; \
835 if (number.word != 0 && alt && base == 16) \
836 /* Account for 0X hex marker. */ \
837 width -= 2; \
839 if (is_negative || showsign || space) \
840 --width; \
842 if (pad == L_(' ')) \
844 PAD (L_(' ')); \
845 width = 0; \
848 if (is_negative) \
849 outchar (L_('-')); \
850 else if (showsign) \
851 outchar (L_('+')); \
852 else if (space) \
853 outchar (L_(' ')); \
855 if (number.word != 0 && alt && base == 16) \
857 outchar (L_('0')); \
858 outchar (spec); \
861 width += prec; \
862 PAD (L_('0')); \
864 outstring (string, workend - string); \
866 break; \
868 else \
870 if (is_negative) \
872 outchar (L_('-')); \
873 --width; \
875 else if (showsign) \
877 outchar (L_('+')); \
878 --width; \
880 else if (space) \
882 outchar (L_(' ')); \
883 --width; \
886 if (number.word != 0 && alt && base == 16) \
888 outchar (L_('0')); \
889 outchar (spec); \
890 width -= 2; \
893 width -= workend - string + prec; \
895 if (prec > 0) \
897 int temp = width; \
898 width = prec; \
899 PAD (L_('0')); \
900 width = temp; \
903 outstring (string, workend - string); \
905 PAD (L_(' ')); \
906 break; \
909 LABEL (form_float): \
911 /* Floating-point number. This is handled by printf_fp.c. */ \
912 const void *ptr; \
913 int function_done; \
915 if (fspec == NULL) \
917 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0)) \
918 is_long_double = 0; \
920 struct printf_info info = { .prec = prec, \
921 .width = width, \
922 .spec = spec, \
923 .is_long_double = is_long_double, \
924 .is_short = is_short, \
925 .is_long = is_long, \
926 .alt = alt, \
927 .space = space, \
928 .left = left, \
929 .showsign = showsign, \
930 .group = group, \
931 .pad = pad, \
932 .extra = 0, \
933 .i18n = use_outdigits, \
934 .wide = sizeof (CHAR_T) != 1, \
935 .is_binary128 = 0}; \
937 PARSE_FLOAT_VA_ARG_EXTENDED (info); \
938 ptr = (const void *) &the_arg; \
940 function_done = __printf_fp (s, &info, &ptr); \
942 else \
944 ptr = (const void *) &args_value[fspec->data_arg]; \
945 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0)) \
947 fspec->data_arg_type = PA_DOUBLE; \
948 fspec->info.is_long_double = 0; \
950 SETUP_FLOAT128_INFO (fspec->info); \
952 function_done = __printf_fp (s, &fspec->info, &ptr); \
955 if (function_done < 0) \
957 /* Error in print handler; up to handler to set errno. */ \
958 done = -1; \
959 goto all_done; \
962 done_add (function_done); \
964 break; \
966 LABEL (form_floathex): \
968 /* Floating point number printed as hexadecimal number. */ \
969 const void *ptr; \
970 int function_done; \
972 if (fspec == NULL) \
974 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0)) \
975 is_long_double = 0; \
977 struct printf_info info = { .prec = prec, \
978 .width = width, \
979 .spec = spec, \
980 .is_long_double = is_long_double, \
981 .is_short = is_short, \
982 .is_long = is_long, \
983 .alt = alt, \
984 .space = space, \
985 .left = left, \
986 .showsign = showsign, \
987 .group = group, \
988 .pad = pad, \
989 .extra = 0, \
990 .wide = sizeof (CHAR_T) != 1, \
991 .is_binary128 = 0}; \
993 PARSE_FLOAT_VA_ARG_EXTENDED (info); \
994 ptr = (const void *) &the_arg; \
996 function_done = __printf_fphex (s, &info, &ptr); \
998 else \
1000 ptr = (const void *) &args_value[fspec->data_arg]; \
1001 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0)) \
1002 fspec->info.is_long_double = 0; \
1003 SETUP_FLOAT128_INFO (fspec->info); \
1005 function_done = __printf_fphex (s, &fspec->info, &ptr); \
1008 if (function_done < 0) \
1010 /* Error in print handler; up to handler to set errno. */ \
1011 done = -1; \
1012 goto all_done; \
1015 done_add (function_done); \
1017 break; \
1019 LABEL (form_pointer): \
1020 /* Generic pointer. */ \
1022 const void *ptr; \
1023 if (fspec == NULL) \
1024 ptr = va_arg (ap, void *); \
1025 else \
1026 ptr = args_value[fspec->data_arg].pa_pointer; \
1027 if (ptr != NULL) \
1029 /* If the pointer is not NULL, write it as a %#x spec. */ \
1030 base = 16; \
1031 number.word = (unsigned long int) ptr; \
1032 is_negative = 0; \
1033 alt = 1; \
1034 group = 0; \
1035 spec = L_('x'); \
1036 goto LABEL (number); \
1038 else \
1040 /* Write "(nil)" for a nil pointer. */ \
1041 string = (CHAR_T *) L_("(nil)"); \
1042 /* Make sure the full string "(nil)" is printed. */ \
1043 if (prec < 5) \
1044 prec = 5; \
1045 /* This is a wide string iff compiling wprintf. */ \
1046 is_long = sizeof (CHAR_T) > 1; \
1047 goto LABEL (print_string); \
1050 /* NOTREACHED */ \
1052 LABEL (form_number): \
1053 if ((mode_flags & PRINTF_FORTIFY) != 0) \
1055 if (! readonly_format) \
1057 extern int __readonly_area (const void *, size_t) \
1058 attribute_hidden; \
1059 readonly_format \
1060 = __readonly_area (format, ((STR_LEN (format) + 1) \
1061 * sizeof (CHAR_T))); \
1063 if (readonly_format < 0) \
1064 __libc_fatal ("*** %n in writable segment detected ***\n"); \
1066 /* Answer the count of characters written. */ \
1067 if (fspec == NULL) \
1069 if (is_longlong) \
1070 *(long long int *) va_arg (ap, void *) = done; \
1071 else if (is_long_num) \
1072 *(long int *) va_arg (ap, void *) = done; \
1073 else if (is_char) \
1074 *(char *) va_arg (ap, void *) = done; \
1075 else if (!is_short) \
1076 *(int *) va_arg (ap, void *) = done; \
1077 else \
1078 *(short int *) va_arg (ap, void *) = done; \
1080 else \
1081 if (is_longlong) \
1082 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
1083 else if (is_long_num) \
1084 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
1085 else if (is_char) \
1086 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
1087 else if (!is_short) \
1088 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
1089 else \
1090 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
1091 break; \
1093 LABEL (form_strerror): \
1094 /* Print description of error ERRNO. */ \
1095 string = \
1096 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
1097 WORK_BUFFER_SIZE * sizeof (CHAR_T)); \
1098 is_long = 0; /* This is no wide-char string. */ \
1099 goto LABEL (print_string)
1101 #ifdef COMPILE_WPRINTF
1102 # define process_string_arg(fspec) \
1103 LABEL (form_character): \
1104 /* Character. */ \
1105 if (is_long) \
1106 goto LABEL (form_wcharacter); \
1107 --width; /* Account for the character itself. */ \
1108 if (!left) \
1109 PAD (L' '); \
1110 if (fspec == NULL) \
1111 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
1112 else \
1113 outchar (__btowc ((unsigned char) \
1114 args_value[fspec->data_arg].pa_int)); \
1115 if (left) \
1116 PAD (L' '); \
1117 break; \
1119 LABEL (form_wcharacter): \
1121 /* Wide character. */ \
1122 --width; \
1123 if (!left) \
1124 PAD (L' '); \
1125 if (fspec == NULL) \
1126 outchar (va_arg (ap, wchar_t)); \
1127 else \
1128 outchar (args_value[fspec->data_arg].pa_wchar); \
1129 if (left) \
1130 PAD (L' '); \
1132 break; \
1134 LABEL (form_string): \
1136 size_t len; \
1138 /* The string argument could in fact be `char *' or `wchar_t *'. \
1139 But this should not make a difference here. */ \
1140 if (fspec == NULL) \
1141 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
1142 else \
1143 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
1145 /* Entry point for printing other strings. */ \
1146 LABEL (print_string): \
1148 if (string == NULL) \
1150 /* Write "(null)" if there's space. */ \
1151 if (prec == -1 || prec >= (int) array_length (null) - 1) \
1153 string = (CHAR_T *) null; \
1154 len = array_length (null) - 1; \
1156 else \
1158 string = (CHAR_T *) L""; \
1159 len = 0; \
1162 else if (!is_long && spec != L_('S')) \
1164 done = outstring_converted_wide_string \
1165 (s, (const char *) string, prec, width, left, done); \
1166 if (done < 0) \
1167 goto all_done; \
1168 /* The padding has already been written. */ \
1169 break; \
1171 else \
1173 if (prec != -1) \
1174 /* Search for the end of the string, but don't search past \
1175 the length specified by the precision. */ \
1176 len = __wcsnlen (string, prec); \
1177 else \
1178 len = __wcslen (string); \
1181 if ((width -= len) < 0) \
1183 outstring (string, len); \
1184 break; \
1187 if (!left) \
1188 PAD (L' '); \
1189 outstring (string, len); \
1190 if (left) \
1191 PAD (L' '); \
1193 break;
1194 #else
1195 # define process_string_arg(fspec) \
1196 LABEL (form_character): \
1197 /* Character. */ \
1198 if (is_long) \
1199 goto LABEL (form_wcharacter); \
1200 --width; /* Account for the character itself. */ \
1201 if (!left) \
1202 PAD (' '); \
1203 if (fspec == NULL) \
1204 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1205 else \
1206 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1207 if (left) \
1208 PAD (' '); \
1209 break; \
1211 LABEL (form_wcharacter): \
1213 /* Wide character. */ \
1214 char buf[MB_LEN_MAX]; \
1215 mbstate_t mbstate; \
1216 size_t len; \
1218 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1219 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1220 : args_value[fspec->data_arg].pa_wchar), \
1221 &mbstate); \
1222 if (len == (size_t) -1) \
1224 /* Something went wrong during the conversion. Bail out. */ \
1225 done = -1; \
1226 goto all_done; \
1228 width -= len; \
1229 if (!left) \
1230 PAD (' '); \
1231 outstring (buf, len); \
1232 if (left) \
1233 PAD (' '); \
1235 break; \
1237 LABEL (form_string): \
1239 size_t len; \
1241 /* The string argument could in fact be `char *' or `wchar_t *'. \
1242 But this should not make a difference here. */ \
1243 if (fspec == NULL) \
1244 string = (char *) va_arg (ap, const char *); \
1245 else \
1246 string = (char *) args_value[fspec->data_arg].pa_string; \
1248 /* Entry point for printing other strings. */ \
1249 LABEL (print_string): \
1251 if (string == NULL) \
1253 /* Write "(null)" if there's space. */ \
1254 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1256 string = (char *) null; \
1257 len = sizeof (null) - 1; \
1259 else \
1261 string = (char *) ""; \
1262 len = 0; \
1265 else if (!is_long && spec != L_('S')) \
1267 if (prec != -1) \
1268 /* Search for the end of the string, but don't search past \
1269 the length (in bytes) specified by the precision. */ \
1270 len = __strnlen (string, prec); \
1271 else \
1272 len = strlen (string); \
1274 else \
1276 done = outstring_converted_wide_string \
1277 (s, (const wchar_t *) string, prec, width, left, done); \
1278 if (done < 0) \
1279 goto all_done; \
1280 /* The padding has already been written. */ \
1281 break; \
1284 if ((width -= len) < 0) \
1286 outstring (string, len); \
1287 break; \
1290 if (!left) \
1291 PAD (' '); \
1292 outstring (string, len); \
1293 if (left) \
1294 PAD (' '); \
1296 break;
1297 #endif
1299 /* Helper function to provide temporary buffering for unbuffered streams. */
1300 static int buffered_vfprintf (FILE *stream, const CHAR_T *fmt, va_list,
1301 unsigned int)
1302 __THROW __attribute__ ((noinline));
1304 /* Handle positional format specifiers. */
1305 static int printf_positional (FILE *s,
1306 const CHAR_T *format, int readonly_format,
1307 va_list ap, va_list *ap_savep, int done,
1308 int nspecs_done, const UCHAR_T *lead_str_end,
1309 CHAR_T *work_buffer, int save_errno,
1310 const char *grouping,
1311 THOUSANDS_SEP_T thousands_sep,
1312 unsigned int mode_flags);
1314 /* Handle unknown format specifier. */
1315 static int printf_unknown (FILE *, const struct printf_info *,
1316 const void *const *) __THROW;
1318 /* Group digits of number string. */
1319 static CHAR_T *group_number (CHAR_T *, CHAR_T *, CHAR_T *, const char *,
1320 THOUSANDS_SEP_T);
1322 /* The function itself. */
1324 vfprintf (FILE *s, const CHAR_T *format, va_list ap, unsigned int mode_flags)
1326 /* The character used as thousands separator. */
1327 THOUSANDS_SEP_T thousands_sep = 0;
1329 /* The string describing the size of groups of digits. */
1330 const char *grouping;
1332 /* Place to accumulate the result. */
1333 int done;
1335 /* Current character in format string. */
1336 const UCHAR_T *f;
1338 /* End of leading constant string. */
1339 const UCHAR_T *lead_str_end;
1341 /* Points to next format specifier. */
1342 const UCHAR_T *end_of_spec;
1344 /* Buffer intermediate results. */
1345 CHAR_T work_buffer[WORK_BUFFER_SIZE];
1346 CHAR_T *workstart = NULL;
1347 CHAR_T *workend;
1349 /* We have to save the original argument pointer. */
1350 va_list ap_save;
1352 /* Count number of specifiers we already processed. */
1353 int nspecs_done;
1355 /* For the %m format we may need the current `errno' value. */
1356 int save_errno = errno;
1358 /* 1 if format is in read-only memory, -1 if it is in writable memory,
1359 0 if unknown. */
1360 int readonly_format = 0;
1362 /* Orient the stream. */
1363 #ifdef ORIENT
1364 ORIENT;
1365 #endif
1367 /* Sanity check of arguments. */
1368 ARGCHECK (s, format);
1370 #ifdef ORIENT
1371 /* Check for correct orientation. */
1372 if (_IO_vtable_offset (s) == 0
1373 && _IO_fwide (s, sizeof (CHAR_T) == 1 ? -1 : 1)
1374 != (sizeof (CHAR_T) == 1 ? -1 : 1))
1375 /* The stream is already oriented otherwise. */
1376 return EOF;
1377 #endif
1379 if (UNBUFFERED_P (s))
1380 /* Use a helper function which will allocate a local temporary buffer
1381 for the stream and then call us again. */
1382 return buffered_vfprintf (s, format, ap, mode_flags);
1384 /* Initialize local variables. */
1385 done = 0;
1386 grouping = (const char *) -1;
1387 #ifdef __va_copy
1388 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1389 since on some systems `va_list' is not an integral type. */
1390 __va_copy (ap_save, ap);
1391 #else
1392 ap_save = ap;
1393 #endif
1394 nspecs_done = 0;
1396 #ifdef COMPILE_WPRINTF
1397 /* Find the first format specifier. */
1398 f = lead_str_end = __find_specwc ((const UCHAR_T *) format);
1399 #else
1400 /* Find the first format specifier. */
1401 f = lead_str_end = __find_specmb ((const UCHAR_T *) format);
1402 #endif
1404 /* Lock stream. */
1405 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
1406 _IO_flockfile (s);
1408 /* Write the literal text before the first format. */
1409 outstring ((const UCHAR_T *) format,
1410 lead_str_end - (const UCHAR_T *) format);
1412 /* If we only have to print a simple string, return now. */
1413 if (*f == L_('\0'))
1414 goto all_done;
1416 /* Use the slow path in case any printf handler is registered. */
1417 if (__glibc_unlikely (__printf_function_table != NULL
1418 || __printf_modifier_table != NULL
1419 || __printf_va_arg_table != NULL))
1420 goto do_positional;
1422 /* Process whole format string. */
1425 STEP0_3_TABLE;
1426 STEP4_TABLE;
1428 union printf_arg *args_value; /* This is not used here but ... */
1429 int is_negative; /* Flag for negative number. */
1430 union
1432 unsigned long long int longlong;
1433 unsigned long int word;
1434 } number;
1435 int base;
1436 union printf_arg the_arg;
1437 CHAR_T *string; /* Pointer to argument string. */
1438 int alt = 0; /* Alternate format. */
1439 int space = 0; /* Use space prefix if no sign is needed. */
1440 int left = 0; /* Left-justify output. */
1441 int showsign = 0; /* Always begin with plus or minus sign. */
1442 int group = 0; /* Print numbers according grouping rules. */
1443 int is_long_double = 0; /* Argument is long double/ long long int. */
1444 int is_short = 0; /* Argument is short int. */
1445 int is_long = 0; /* Argument is long int. */
1446 int is_char = 0; /* Argument is promoted (unsigned) char. */
1447 int width = 0; /* Width of output; 0 means none specified. */
1448 int prec = -1; /* Precision of output; -1 means none specified. */
1449 /* This flag is set by the 'I' modifier and selects the use of the
1450 `outdigits' as determined by the current locale. */
1451 int use_outdigits = 0;
1452 UCHAR_T pad = L_(' ');/* Padding character. */
1453 CHAR_T spec;
1455 workstart = NULL;
1456 workend = work_buffer + WORK_BUFFER_SIZE;
1458 /* Get current character in format string. */
1459 JUMP (*++f, step0_jumps);
1461 /* ' ' flag. */
1462 LABEL (flag_space):
1463 space = 1;
1464 JUMP (*++f, step0_jumps);
1466 /* '+' flag. */
1467 LABEL (flag_plus):
1468 showsign = 1;
1469 JUMP (*++f, step0_jumps);
1471 /* The '-' flag. */
1472 LABEL (flag_minus):
1473 left = 1;
1474 pad = L_(' ');
1475 JUMP (*++f, step0_jumps);
1477 /* The '#' flag. */
1478 LABEL (flag_hash):
1479 alt = 1;
1480 JUMP (*++f, step0_jumps);
1482 /* The '0' flag. */
1483 LABEL (flag_zero):
1484 if (!left)
1485 pad = L_('0');
1486 JUMP (*++f, step0_jumps);
1488 /* The '\'' flag. */
1489 LABEL (flag_quote):
1490 group = 1;
1492 if (grouping == (const char *) -1)
1494 #ifdef COMPILE_WPRINTF
1495 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1496 _NL_NUMERIC_THOUSANDS_SEP_WC);
1497 #else
1498 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1499 #endif
1501 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1502 if (*grouping == '\0' || *grouping == CHAR_MAX
1503 #ifdef COMPILE_WPRINTF
1504 || thousands_sep == L'\0'
1505 #else
1506 || *thousands_sep == '\0'
1507 #endif
1509 grouping = NULL;
1511 JUMP (*++f, step0_jumps);
1513 LABEL (flag_i18n):
1514 use_outdigits = 1;
1515 JUMP (*++f, step0_jumps);
1517 /* Get width from argument. */
1518 LABEL (width_asterics):
1520 const UCHAR_T *tmp; /* Temporary value. */
1522 tmp = ++f;
1523 if (ISDIGIT (*tmp))
1525 int pos = read_int (&tmp);
1527 if (pos == -1)
1529 __set_errno (EOVERFLOW);
1530 done = -1;
1531 goto all_done;
1534 if (pos && *tmp == L_('$'))
1535 /* The width comes from a positional parameter. */
1536 goto do_positional;
1538 width = va_arg (ap, int);
1540 /* Negative width means left justified. */
1541 if (width < 0)
1543 width = -width;
1544 pad = L_(' ');
1545 left = 1;
1548 if (__glibc_unlikely (width >= INT_MAX / sizeof (CHAR_T) - EXTSIZ))
1550 __set_errno (EOVERFLOW);
1551 done = -1;
1552 goto all_done;
1555 if (width >= WORK_BUFFER_SIZE - EXTSIZ)
1557 /* We have to use a special buffer. */
1558 size_t needed = ((size_t) width + EXTSIZ) * sizeof (CHAR_T);
1559 if (__libc_use_alloca (needed))
1560 workend = (CHAR_T *) alloca (needed) + width + EXTSIZ;
1561 else
1563 workstart = (CHAR_T *) malloc (needed);
1564 if (workstart == NULL)
1566 done = -1;
1567 goto all_done;
1569 workend = workstart + width + EXTSIZ;
1573 JUMP (*f, step1_jumps);
1575 /* Given width in format string. */
1576 LABEL (width):
1577 width = read_int (&f);
1579 if (__glibc_unlikely (width == -1
1580 || width >= INT_MAX / sizeof (CHAR_T) - EXTSIZ))
1582 __set_errno (EOVERFLOW);
1583 done = -1;
1584 goto all_done;
1587 if (width >= WORK_BUFFER_SIZE - EXTSIZ)
1589 /* We have to use a special buffer. */
1590 size_t needed = ((size_t) width + EXTSIZ) * sizeof (CHAR_T);
1591 if (__libc_use_alloca (needed))
1592 workend = (CHAR_T *) alloca (needed) + width + EXTSIZ;
1593 else
1595 workstart = (CHAR_T *) malloc (needed);
1596 if (workstart == NULL)
1598 done = -1;
1599 goto all_done;
1601 workend = workstart + width + EXTSIZ;
1604 if (*f == L_('$'))
1605 /* Oh, oh. The argument comes from a positional parameter. */
1606 goto do_positional;
1607 JUMP (*f, step1_jumps);
1609 LABEL (precision):
1610 ++f;
1611 if (*f == L_('*'))
1613 const UCHAR_T *tmp; /* Temporary value. */
1615 tmp = ++f;
1616 if (ISDIGIT (*tmp))
1618 int pos = read_int (&tmp);
1620 if (pos == -1)
1622 __set_errno (EOVERFLOW);
1623 done = -1;
1624 goto all_done;
1627 if (pos && *tmp == L_('$'))
1628 /* The precision comes from a positional parameter. */
1629 goto do_positional;
1631 prec = va_arg (ap, int);
1633 /* If the precision is negative the precision is omitted. */
1634 if (prec < 0)
1635 prec = -1;
1637 else if (ISDIGIT (*f))
1639 prec = read_int (&f);
1641 /* The precision was specified in this case as an extremely
1642 large positive value. */
1643 if (prec == -1)
1645 __set_errno (EOVERFLOW);
1646 done = -1;
1647 goto all_done;
1650 else
1651 prec = 0;
1652 if (prec > width && prec > WORK_BUFFER_SIZE - EXTSIZ)
1654 /* Deallocate any previously allocated buffer because it is
1655 too small. */
1656 if (__glibc_unlikely (workstart != NULL))
1657 free (workstart);
1658 workstart = NULL;
1659 if (__glibc_unlikely (prec >= INT_MAX / sizeof (CHAR_T) - EXTSIZ))
1661 __set_errno (EOVERFLOW);
1662 done = -1;
1663 goto all_done;
1665 size_t needed = ((size_t) prec + EXTSIZ) * sizeof (CHAR_T);
1667 if (__libc_use_alloca (needed))
1668 workend = (CHAR_T *) alloca (needed) + prec + EXTSIZ;
1669 else
1671 workstart = (CHAR_T *) malloc (needed);
1672 if (workstart == NULL)
1674 done = -1;
1675 goto all_done;
1677 workend = workstart + prec + EXTSIZ;
1680 JUMP (*f, step2_jumps);
1682 /* Process 'h' modifier. There might another 'h' following. */
1683 LABEL (mod_half):
1684 is_short = 1;
1685 JUMP (*++f, step3a_jumps);
1687 /* Process 'hh' modifier. */
1688 LABEL (mod_halfhalf):
1689 is_short = 0;
1690 is_char = 1;
1691 JUMP (*++f, step4_jumps);
1693 /* Process 'l' modifier. There might another 'l' following. */
1694 LABEL (mod_long):
1695 is_long = 1;
1696 JUMP (*++f, step3b_jumps);
1698 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1699 allowed to follow. */
1700 LABEL (mod_longlong):
1701 is_long_double = 1;
1702 is_long = 1;
1703 JUMP (*++f, step4_jumps);
1705 LABEL (mod_size_t):
1706 is_long_double = sizeof (size_t) > sizeof (unsigned long int);
1707 is_long = sizeof (size_t) > sizeof (unsigned int);
1708 JUMP (*++f, step4_jumps);
1710 LABEL (mod_ptrdiff_t):
1711 is_long_double = sizeof (ptrdiff_t) > sizeof (unsigned long int);
1712 is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
1713 JUMP (*++f, step4_jumps);
1715 LABEL (mod_intmax_t):
1716 is_long_double = sizeof (intmax_t) > sizeof (unsigned long int);
1717 is_long = sizeof (intmax_t) > sizeof (unsigned int);
1718 JUMP (*++f, step4_jumps);
1720 /* Process current format. */
1721 while (1)
1723 process_arg (((struct printf_spec *) NULL));
1724 process_string_arg (((struct printf_spec *) NULL));
1726 LABEL (form_unknown):
1727 if (spec == L_('\0'))
1729 /* The format string ended before the specifier is complete. */
1730 __set_errno (EINVAL);
1731 done = -1;
1732 goto all_done;
1735 /* If we are in the fast loop force entering the complicated
1736 one. */
1737 goto do_positional;
1740 /* The format is correctly handled. */
1741 ++nspecs_done;
1743 if (__glibc_unlikely (workstart != NULL))
1744 free (workstart);
1745 workstart = NULL;
1747 /* Look for next format specifier. */
1748 #ifdef COMPILE_WPRINTF
1749 f = __find_specwc ((end_of_spec = ++f));
1750 #else
1751 f = __find_specmb ((end_of_spec = ++f));
1752 #endif
1754 /* Write the following constant string. */
1755 outstring (end_of_spec, f - end_of_spec);
1757 while (*f != L_('\0'));
1759 /* Unlock stream and return. */
1760 goto all_done;
1762 /* Hand off processing for positional parameters. */
1763 do_positional:
1764 if (__glibc_unlikely (workstart != NULL))
1766 free (workstart);
1767 workstart = NULL;
1769 done = printf_positional (s, format, readonly_format, ap, &ap_save,
1770 done, nspecs_done, lead_str_end, work_buffer,
1771 save_errno, grouping, thousands_sep, mode_flags);
1773 all_done:
1774 if (__glibc_unlikely (workstart != NULL))
1775 free (workstart);
1776 /* Unlock the stream. */
1777 _IO_funlockfile (s);
1778 _IO_cleanup_region_end (0);
1780 return done;
1783 static int
1784 printf_positional (FILE *s, const CHAR_T *format, int readonly_format,
1785 va_list ap, va_list *ap_savep, int done, int nspecs_done,
1786 const UCHAR_T *lead_str_end,
1787 CHAR_T *work_buffer, int save_errno,
1788 const char *grouping, THOUSANDS_SEP_T thousands_sep,
1789 unsigned int mode_flags)
1791 /* For positional argument handling. */
1792 struct scratch_buffer specsbuf;
1793 scratch_buffer_init (&specsbuf);
1794 struct printf_spec *specs = specsbuf.data;
1795 size_t specs_limit = specsbuf.length / sizeof (specs[0]);
1797 /* Used as a backing store for args_value, args_size, args_type
1798 below. */
1799 struct scratch_buffer argsbuf;
1800 scratch_buffer_init (&argsbuf);
1802 /* Array with information about the needed arguments. This has to
1803 be dynamically extensible. */
1804 size_t nspecs = 0;
1806 /* The number of arguments the format string requests. This will
1807 determine the size of the array needed to store the argument
1808 attributes. */
1809 size_t nargs = 0;
1811 /* Positional parameters refer to arguments directly. This could
1812 also determine the maximum number of arguments. Track the
1813 maximum number. */
1814 size_t max_ref_arg = 0;
1816 /* Just a counter. */
1817 size_t cnt;
1819 CHAR_T *workstart = NULL;
1821 if (grouping == (const char *) -1)
1823 #ifdef COMPILE_WPRINTF
1824 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1825 _NL_NUMERIC_THOUSANDS_SEP_WC);
1826 #else
1827 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1828 #endif
1830 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1831 if (*grouping == '\0' || *grouping == CHAR_MAX)
1832 grouping = NULL;
1835 for (const UCHAR_T *f = lead_str_end; *f != L_('\0');
1836 f = specs[nspecs++].next_fmt)
1838 if (nspecs == specs_limit)
1840 if (!scratch_buffer_grow_preserve (&specsbuf))
1842 done = -1;
1843 goto all_done;
1845 specs = specsbuf.data;
1846 specs_limit = specsbuf.length / sizeof (specs[0]);
1849 /* Parse the format specifier. */
1850 #ifdef COMPILE_WPRINTF
1851 nargs += __parse_one_specwc (f, nargs, &specs[nspecs], &max_ref_arg);
1852 #else
1853 nargs += __parse_one_specmb (f, nargs, &specs[nspecs], &max_ref_arg);
1854 #endif
1857 /* Determine the number of arguments the format string consumes. */
1858 nargs = MAX (nargs, max_ref_arg);
1860 union printf_arg *args_value;
1861 int *args_size;
1862 int *args_type;
1864 /* Calculate total size needed to represent a single argument
1865 across all three argument-related arrays. */
1866 size_t bytes_per_arg
1867 = sizeof (*args_value) + sizeof (*args_size) + sizeof (*args_type);
1868 if (!scratch_buffer_set_array_size (&argsbuf, nargs, bytes_per_arg))
1870 done = -1;
1871 goto all_done;
1873 args_value = argsbuf.data;
1874 /* Set up the remaining two arrays to each point past the end of
1875 the prior array, since space for all three has been allocated
1876 now. */
1877 args_size = &args_value[nargs].pa_int;
1878 args_type = &args_size[nargs];
1879 memset (args_type, (mode_flags & PRINTF_FORTIFY) != 0 ? '\xff' : '\0',
1880 nargs * sizeof (*args_type));
1883 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1884 still zero after this loop, format is invalid. For now we
1885 simply use 0 as the value. */
1887 /* Fill in the types of all the arguments. */
1888 for (cnt = 0; cnt < nspecs; ++cnt)
1890 /* If the width is determined by an argument this is an int. */
1891 if (specs[cnt].width_arg != -1)
1892 args_type[specs[cnt].width_arg] = PA_INT;
1894 /* If the precision is determined by an argument this is an int. */
1895 if (specs[cnt].prec_arg != -1)
1896 args_type[specs[cnt].prec_arg] = PA_INT;
1898 switch (specs[cnt].ndata_args)
1900 case 0: /* No arguments. */
1901 break;
1902 case 1: /* One argument; we already have the
1903 type and size. */
1904 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1905 args_size[specs[cnt].data_arg] = specs[cnt].size;
1906 break;
1907 default:
1908 /* We have more than one argument for this format spec.
1909 We must call the arginfo function again to determine
1910 all the types. */
1911 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1912 (&specs[cnt].info,
1913 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg],
1914 &args_size[specs[cnt].data_arg]);
1915 break;
1919 /* Now we know all the types and the order. Fill in the argument
1920 values. */
1921 for (cnt = 0; cnt < nargs; ++cnt)
1922 switch (args_type[cnt])
1924 #define T(tag, mem, type) \
1925 case tag: \
1926 args_value[cnt].mem = va_arg (*ap_savep, type); \
1927 break
1929 T (PA_WCHAR, pa_wchar, wint_t);
1930 case PA_CHAR: /* Promoted. */
1931 case PA_INT|PA_FLAG_SHORT: /* Promoted. */
1932 #if LONG_MAX == INT_MAX
1933 case PA_INT|PA_FLAG_LONG:
1934 #endif
1935 T (PA_INT, pa_int, int);
1936 #if LONG_MAX == LONG_LONG_MAX
1937 case PA_INT|PA_FLAG_LONG:
1938 #endif
1939 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1940 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1941 # error "he?"
1942 #endif
1943 case PA_FLOAT: /* Promoted. */
1944 T (PA_DOUBLE, pa_double, double);
1945 case PA_DOUBLE|PA_FLAG_LONG_DOUBLE:
1946 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0))
1948 args_value[cnt].pa_double = va_arg (*ap_savep, double);
1949 args_type[cnt] &= ~PA_FLAG_LONG_DOUBLE;
1951 #if __HAVE_FLOAT128_UNLIKE_LDBL
1952 else if ((mode_flags & PRINTF_LDBL_USES_FLOAT128) != 0)
1953 args_value[cnt].pa_float128 = va_arg (*ap_savep, _Float128);
1954 #endif
1955 else
1956 args_value[cnt].pa_long_double = va_arg (*ap_savep, long double);
1957 break;
1958 case PA_STRING: /* All pointers are the same */
1959 case PA_WSTRING: /* All pointers are the same */
1960 T (PA_POINTER, pa_pointer, void *);
1961 #undef T
1962 default:
1963 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1964 args_value[cnt].pa_pointer = va_arg (*ap_savep, void *);
1965 else if (__glibc_unlikely (__printf_va_arg_table != NULL)
1966 && __printf_va_arg_table[args_type[cnt] - PA_LAST] != NULL)
1968 args_value[cnt].pa_user = alloca (args_size[cnt]);
1969 (*__printf_va_arg_table[args_type[cnt] - PA_LAST])
1970 (args_value[cnt].pa_user, ap_savep);
1972 else
1973 memset (&args_value[cnt], 0, sizeof (args_value[cnt]));
1974 break;
1975 case -1:
1976 /* Error case. Not all parameters appear in N$ format
1977 strings. We have no way to determine their type. */
1978 assert ((mode_flags & PRINTF_FORTIFY) != 0);
1979 __libc_fatal ("*** invalid %N$ use detected ***\n");
1982 /* Now walk through all format specifiers and process them. */
1983 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1985 STEP4_TABLE;
1987 int is_negative;
1988 union
1990 unsigned long long int longlong;
1991 unsigned long int word;
1992 } number;
1993 int base;
1994 union printf_arg the_arg;
1995 CHAR_T *string; /* Pointer to argument string. */
1997 /* Fill variables from values in struct. */
1998 int alt = specs[nspecs_done].info.alt;
1999 int space = specs[nspecs_done].info.space;
2000 int left = specs[nspecs_done].info.left;
2001 int showsign = specs[nspecs_done].info.showsign;
2002 int group = specs[nspecs_done].info.group;
2003 int is_long_double = specs[nspecs_done].info.is_long_double;
2004 int is_short = specs[nspecs_done].info.is_short;
2005 int is_char = specs[nspecs_done].info.is_char;
2006 int is_long = specs[nspecs_done].info.is_long;
2007 int width = specs[nspecs_done].info.width;
2008 int prec = specs[nspecs_done].info.prec;
2009 int use_outdigits = specs[nspecs_done].info.i18n;
2010 char pad = specs[nspecs_done].info.pad;
2011 CHAR_T spec = specs[nspecs_done].info.spec;
2013 workstart = NULL;
2014 CHAR_T *workend = work_buffer + WORK_BUFFER_SIZE;
2016 /* Fill in last information. */
2017 if (specs[nspecs_done].width_arg != -1)
2019 /* Extract the field width from an argument. */
2020 specs[nspecs_done].info.width =
2021 args_value[specs[nspecs_done].width_arg].pa_int;
2023 if (specs[nspecs_done].info.width < 0)
2024 /* If the width value is negative left justification is
2025 selected and the value is taken as being positive. */
2027 specs[nspecs_done].info.width *= -1;
2028 left = specs[nspecs_done].info.left = 1;
2030 width = specs[nspecs_done].info.width;
2033 if (specs[nspecs_done].prec_arg != -1)
2035 /* Extract the precision from an argument. */
2036 specs[nspecs_done].info.prec =
2037 args_value[specs[nspecs_done].prec_arg].pa_int;
2039 if (specs[nspecs_done].info.prec < 0)
2040 /* If the precision is negative the precision is
2041 omitted. */
2042 specs[nspecs_done].info.prec = -1;
2044 prec = specs[nspecs_done].info.prec;
2047 /* Maybe the buffer is too small. */
2048 if (MAX (prec, width) + EXTSIZ > WORK_BUFFER_SIZE)
2050 if (__libc_use_alloca ((MAX (prec, width) + EXTSIZ)
2051 * sizeof (CHAR_T)))
2052 workend = ((CHAR_T *) alloca ((MAX (prec, width) + EXTSIZ)
2053 * sizeof (CHAR_T))
2054 + (MAX (prec, width) + EXTSIZ));
2055 else
2057 workstart = (CHAR_T *) malloc ((MAX (prec, width) + EXTSIZ)
2058 * sizeof (CHAR_T));
2059 if (workstart == NULL)
2061 done = -1;
2062 goto all_done;
2064 workend = workstart + (MAX (prec, width) + EXTSIZ);
2068 /* Process format specifiers. */
2069 while (1)
2071 extern printf_function **__printf_function_table;
2072 int function_done;
2074 if (spec <= UCHAR_MAX
2075 && __printf_function_table != NULL
2076 && __printf_function_table[(size_t) spec] != NULL)
2078 const void **ptr = alloca (specs[nspecs_done].ndata_args
2079 * sizeof (const void *));
2081 /* Fill in an array of pointers to the argument values. */
2082 for (unsigned int i = 0; i < specs[nspecs_done].ndata_args;
2083 ++i)
2084 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
2086 /* Call the function. */
2087 function_done = __printf_function_table[(size_t) spec]
2088 (s, &specs[nspecs_done].info, ptr);
2090 if (function_done != -2)
2092 /* If an error occurred we don't have information
2093 about # of chars. */
2094 if (function_done < 0)
2096 /* Function has set errno. */
2097 done = -1;
2098 goto all_done;
2101 done_add (function_done);
2102 break;
2106 JUMP (spec, step4_jumps);
2108 process_arg ((&specs[nspecs_done]));
2109 process_string_arg ((&specs[nspecs_done]));
2111 LABEL (form_unknown):
2113 unsigned int i;
2114 const void **ptr;
2116 ptr = alloca (specs[nspecs_done].ndata_args
2117 * sizeof (const void *));
2119 /* Fill in an array of pointers to the argument values. */
2120 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
2121 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
2123 /* Call the function. */
2124 function_done = printf_unknown (s, &specs[nspecs_done].info,
2125 ptr);
2127 /* If an error occurred we don't have information about #
2128 of chars. */
2129 if (function_done < 0)
2131 /* Function has set errno. */
2132 done = -1;
2133 goto all_done;
2136 done_add (function_done);
2138 break;
2141 if (__glibc_unlikely (workstart != NULL))
2142 free (workstart);
2143 workstart = NULL;
2145 /* Write the following constant string. */
2146 outstring (specs[nspecs_done].end_of_fmt,
2147 specs[nspecs_done].next_fmt
2148 - specs[nspecs_done].end_of_fmt);
2150 all_done:
2151 if (__glibc_unlikely (workstart != NULL))
2152 free (workstart);
2153 scratch_buffer_free (&argsbuf);
2154 scratch_buffer_free (&specsbuf);
2155 return done;
2158 /* Handle an unknown format specifier. This prints out a canonicalized
2159 representation of the format spec itself. */
2160 static int
2161 printf_unknown (FILE *s, const struct printf_info *info,
2162 const void *const *args)
2165 int done = 0;
2166 CHAR_T work_buffer[MAX (sizeof (info->width), sizeof (info->prec)) * 3];
2167 CHAR_T *const workend
2168 = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
2169 CHAR_T *w;
2171 outchar (L_('%'));
2173 if (info->alt)
2174 outchar (L_('#'));
2175 if (info->group)
2176 outchar (L_('\''));
2177 if (info->showsign)
2178 outchar (L_('+'));
2179 else if (info->space)
2180 outchar (L_(' '));
2181 if (info->left)
2182 outchar (L_('-'));
2183 if (info->pad == L_('0'))
2184 outchar (L_('0'));
2185 if (info->i18n)
2186 outchar (L_('I'));
2188 if (info->width != 0)
2190 w = _itoa_word (info->width, workend, 10, 0);
2191 while (w < workend)
2192 outchar (*w++);
2195 if (info->prec != -1)
2197 outchar (L_('.'));
2198 w = _itoa_word (info->prec, workend, 10, 0);
2199 while (w < workend)
2200 outchar (*w++);
2203 if (info->spec != L_('\0'))
2204 outchar (info->spec);
2206 all_done:
2207 return done;
2210 /* Group the digits from W to REAR_PTR according to the grouping rules
2211 of the current locale. The interpretation of GROUPING is as in
2212 `struct lconv' from <locale.h>. The grouped number extends from
2213 the returned pointer until REAR_PTR. FRONT_PTR to W is used as a
2214 scratch area. */
2215 static CHAR_T *
2216 group_number (CHAR_T *front_ptr, CHAR_T *w, CHAR_T *rear_ptr,
2217 const char *grouping, THOUSANDS_SEP_T thousands_sep)
2219 /* Length of the current group. */
2220 int len;
2221 #ifndef COMPILE_WPRINTF
2222 /* Length of the separator (in wide mode, the separator is always a
2223 single wide character). */
2224 int tlen = strlen (thousands_sep);
2225 #endif
2227 /* We treat all negative values like CHAR_MAX. */
2229 if (*grouping == CHAR_MAX || *grouping <= 0)
2230 /* No grouping should be done. */
2231 return w;
2233 len = *grouping++;
2235 /* Copy existing string so that nothing gets overwritten. */
2236 memmove (front_ptr, w, (rear_ptr - w) * sizeof (CHAR_T));
2237 CHAR_T *s = front_ptr + (rear_ptr - w);
2239 w = rear_ptr;
2241 /* Process all characters in the string. */
2242 while (s > front_ptr)
2244 *--w = *--s;
2246 if (--len == 0 && s > front_ptr)
2248 /* A new group begins. */
2249 #ifdef COMPILE_WPRINTF
2250 if (w != s)
2251 *--w = thousands_sep;
2252 else
2253 /* Not enough room for the separator. */
2254 goto copy_rest;
2255 #else
2256 int cnt = tlen;
2257 if (tlen < w - s)
2259 *--w = thousands_sep[--cnt];
2260 while (cnt > 0);
2261 else
2262 /* Not enough room for the separator. */
2263 goto copy_rest;
2264 #endif
2266 if (*grouping == CHAR_MAX
2267 #if CHAR_MIN < 0
2268 || *grouping < 0
2269 #endif
2272 copy_rest:
2273 /* No further grouping to be done. Copy the rest of the
2274 number. */
2275 memmove (w, s, (front_ptr -s) * sizeof (CHAR_T));
2276 break;
2278 else if (*grouping != '\0')
2279 len = *grouping++;
2280 else
2281 /* The previous grouping repeats ad infinitum. */
2282 len = grouping[-1];
2285 return w;
2288 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2289 struct helper_file
2291 struct _IO_FILE_plus _f;
2292 #ifdef COMPILE_WPRINTF
2293 struct _IO_wide_data _wide_data;
2294 #endif
2295 FILE *_put_stream;
2296 #ifdef _IO_MTSAFE_IO
2297 _IO_lock_t lock;
2298 #endif
2301 static int
2302 _IO_helper_overflow (FILE *s, int c)
2304 FILE *target = ((struct helper_file*) s)->_put_stream;
2305 #ifdef COMPILE_WPRINTF
2306 int used = s->_wide_data->_IO_write_ptr - s->_wide_data->_IO_write_base;
2307 if (used)
2309 size_t written = _IO_sputn (target, s->_wide_data->_IO_write_base, used);
2310 if (written == 0 || written == WEOF)
2311 return WEOF;
2312 __wmemmove (s->_wide_data->_IO_write_base,
2313 s->_wide_data->_IO_write_base + written,
2314 used - written);
2315 s->_wide_data->_IO_write_ptr -= written;
2317 #else
2318 int used = s->_IO_write_ptr - s->_IO_write_base;
2319 if (used)
2321 size_t written = _IO_sputn (target, s->_IO_write_base, used);
2322 if (written == 0 || written == EOF)
2323 return EOF;
2324 memmove (s->_IO_write_base, s->_IO_write_base + written,
2325 used - written);
2326 s->_IO_write_ptr -= written;
2328 #endif
2329 return PUTC (c, s);
2332 #ifdef COMPILE_WPRINTF
2333 static const struct _IO_jump_t _IO_helper_jumps libio_vtable =
2335 JUMP_INIT_DUMMY,
2336 JUMP_INIT (finish, _IO_wdefault_finish),
2337 JUMP_INIT (overflow, _IO_helper_overflow),
2338 JUMP_INIT (underflow, _IO_default_underflow),
2339 JUMP_INIT (uflow, _IO_default_uflow),
2340 JUMP_INIT (pbackfail, (_IO_pbackfail_t) _IO_wdefault_pbackfail),
2341 JUMP_INIT (xsputn, _IO_wdefault_xsputn),
2342 JUMP_INIT (xsgetn, _IO_wdefault_xsgetn),
2343 JUMP_INIT (seekoff, _IO_default_seekoff),
2344 JUMP_INIT (seekpos, _IO_default_seekpos),
2345 JUMP_INIT (setbuf, _IO_default_setbuf),
2346 JUMP_INIT (sync, _IO_default_sync),
2347 JUMP_INIT (doallocate, _IO_wdefault_doallocate),
2348 JUMP_INIT (read, _IO_default_read),
2349 JUMP_INIT (write, _IO_default_write),
2350 JUMP_INIT (seek, _IO_default_seek),
2351 JUMP_INIT (close, _IO_default_close),
2352 JUMP_INIT (stat, _IO_default_stat)
2354 #else
2355 static const struct _IO_jump_t _IO_helper_jumps libio_vtable =
2357 JUMP_INIT_DUMMY,
2358 JUMP_INIT (finish, _IO_default_finish),
2359 JUMP_INIT (overflow, _IO_helper_overflow),
2360 JUMP_INIT (underflow, _IO_default_underflow),
2361 JUMP_INIT (uflow, _IO_default_uflow),
2362 JUMP_INIT (pbackfail, _IO_default_pbackfail),
2363 JUMP_INIT (xsputn, _IO_default_xsputn),
2364 JUMP_INIT (xsgetn, _IO_default_xsgetn),
2365 JUMP_INIT (seekoff, _IO_default_seekoff),
2366 JUMP_INIT (seekpos, _IO_default_seekpos),
2367 JUMP_INIT (setbuf, _IO_default_setbuf),
2368 JUMP_INIT (sync, _IO_default_sync),
2369 JUMP_INIT (doallocate, _IO_default_doallocate),
2370 JUMP_INIT (read, _IO_default_read),
2371 JUMP_INIT (write, _IO_default_write),
2372 JUMP_INIT (seek, _IO_default_seek),
2373 JUMP_INIT (close, _IO_default_close),
2374 JUMP_INIT (stat, _IO_default_stat)
2376 #endif
2378 static int
2379 buffered_vfprintf (FILE *s, const CHAR_T *format, va_list args,
2380 unsigned int mode_flags)
2382 CHAR_T buf[BUFSIZ];
2383 struct helper_file helper;
2384 FILE *hp = (FILE *) &helper._f;
2385 int result, to_flush;
2387 /* Orient the stream. */
2388 #ifdef ORIENT
2389 ORIENT;
2390 #endif
2392 /* Initialize helper. */
2393 helper._put_stream = s;
2394 #ifdef COMPILE_WPRINTF
2395 hp->_wide_data = &helper._wide_data;
2396 _IO_wsetp (hp, buf, buf + sizeof buf / sizeof (CHAR_T));
2397 hp->_mode = 1;
2398 #else
2399 _IO_setp (hp, buf, buf + sizeof buf);
2400 hp->_mode = -1;
2401 #endif
2402 hp->_flags = _IO_MAGIC|_IO_NO_READS|_IO_USER_LOCK;
2403 #if _IO_JUMPS_OFFSET
2404 hp->_vtable_offset = 0;
2405 #endif
2406 #ifdef _IO_MTSAFE_IO
2407 hp->_lock = NULL;
2408 #endif
2409 hp->_flags2 = s->_flags2;
2410 _IO_JUMPS (&helper._f) = (struct _IO_jump_t *) &_IO_helper_jumps;
2412 /* Now print to helper instead. */
2413 result = vfprintf (hp, format, args, mode_flags);
2415 /* Lock stream. */
2416 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, s);
2417 _IO_flockfile (s);
2419 /* Now flush anything from the helper to the S. */
2420 #ifdef COMPILE_WPRINTF
2421 if ((to_flush = (hp->_wide_data->_IO_write_ptr
2422 - hp->_wide_data->_IO_write_base)) > 0)
2424 if ((int) _IO_sputn (s, hp->_wide_data->_IO_write_base, to_flush)
2425 != to_flush)
2426 result = -1;
2428 #else
2429 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
2431 if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
2432 result = -1;
2434 #endif
2436 /* Unlock the stream. */
2437 _IO_funlockfile (s);
2438 __libc_cleanup_region_end (0);
2440 return result;