1 /* Copyright (C) 1991-2017 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/>. */
27 #include <libc-lock.h>
28 #include <sys/param.h>
30 #include <locale/localeinfo.h>
32 #include <scratch_buffer.h>
34 /* This code is shared between the standard stdio implementation found
35 in GNU C library and the libio implementation originally found in
38 Beside this it is also shared between the normal and wide character
39 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
45 #define va_list _IO_va_list
47 #define BUFSIZ _IO_BUFSIZ
48 /* In some cases we need extra space for all the output which is not
49 counted in the width of the string. We assume 32 characters is
52 #define ARGCHECK(S, Format) \
55 /* Check file argument for consistence. */ \
57 if (S->_flags & _IO_NO_WRITES) \
59 S->_flags |= _IO_ERR_SEEN; \
60 __set_errno (EBADF); \
69 #define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
71 #define done_add(val) \
73 unsigned int _val = val; \
74 assert ((unsigned int) done < (unsigned int) INT_MAX); \
75 if (__glibc_unlikely (INT_MAX - done < _val)) \
78 __set_errno (EOVERFLOW); \
84 #ifndef COMPILE_WPRINTF
85 # define vfprintf _IO_vfprintf_internal
87 # define UCHAR_T unsigned char
89 typedef const char *THOUSANDS_SEP_T
;
91 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
92 # define STR_LEN(Str) strlen (Str)
94 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
95 # define PAD(Padchar) \
99 _IO_ssize_t written = _IO_padn (s, (Padchar), width); \
100 if (__glibc_unlikely (written != width)) \
105 done_add (written); \
108 # define PUTC(C, F) _IO_putc_unlocked (C, F)
109 # define ORIENT if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
112 # define vfprintf _IO_vfwprintf
113 # define CHAR_T wchar_t
114 /* This is a hack!!! There should be a type uwchar_t. */
115 # define UCHAR_T unsigned int /* uwchar_t */
116 # define INT_T wint_t
117 typedef wchar_t THOUSANDS_SEP_T
;
118 # define L_(Str) L##Str
119 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
120 # define STR_LEN(Str) __wcslen (Str)
124 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
125 # define PAD(Padchar) \
129 _IO_ssize_t written = _IO_wpadn (s, (Padchar), width); \
130 if (__glibc_unlikely (written != width)) \
135 done_add (written); \
138 # define PUTC(C, F) _IO_putwc_unlocked (C, F)
139 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
142 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
143 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
148 #include "_i18n_number.h"
150 /* Include the shared code for parsing the format string. */
151 #include "printf-parse.h"
154 #define outchar(Ch) \
157 const INT_T outc = (Ch); \
158 if (PUTC (outc, s) == EOF || done == INT_MAX) \
167 #define outstring(String, Len) \
170 assert ((size_t) done <= (size_t) INT_MAX); \
171 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
176 if (__glibc_unlikely (INT_MAX - done < (Len))) \
179 __set_errno (EOVERFLOW); \
186 /* For handling long_double and longlong we use the same flag. If
187 `long' and `long long' are effectively the same type define it to
189 #if LONG_MAX == LONG_LONG_MAX
190 # define is_longlong 0
192 # define is_longlong is_long_double
195 /* If `long' and `int' is effectively the same type we don't have to
196 handle `long separately. */
197 #if INT_MAX == LONG_MAX
198 # define is_long_num 0
200 # define is_long_num is_long
204 /* Global constants. */
205 static const CHAR_T null
[] = L_("(null)");
207 /* Size of the work_buffer variable (in characters, not bytes. */
208 enum { WORK_BUFFER_SIZE
= 1000 };
210 /* This table maps a character into a number representing a class. In
211 each step there is a destination label for each class. */
212 static const uint8_t jump_table
[] =
214 /* ' ' */ 1, 0, 0, /* '#' */ 4,
215 0, /* '%' */ 14, 0, /* '\''*/ 6,
216 0, 0, /* '*' */ 7, /* '+' */ 2,
217 0, /* '-' */ 3, /* '.' */ 9, 0,
218 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
219 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
220 /* '8' */ 8, /* '9' */ 8, 0, 0,
222 0, /* 'A' */ 26, 0, /* 'C' */ 25,
223 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
224 0, /* 'I' */ 29, 0, 0,
225 /* 'L' */ 12, 0, 0, 0,
226 0, 0, 0, /* 'S' */ 21,
228 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
230 0, /* 'a' */ 26, 0, /* 'c' */ 20,
231 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
232 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
233 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
234 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
235 /* 't' */ 27, /* 'u' */ 16, 0, 0,
236 /* 'x' */ 18, 0, /* 'z' */ 13
239 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
240 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
241 #define LABEL(Name) do_##Name
243 /* 'int' is enough and it saves some space on 64 bit systems. */
244 # define JUMP_TABLE_TYPE const int
245 # define JUMP_TABLE_BASE_LABEL do_form_unknown
246 # define REF(Name) &&do_##Name - &&JUMP_TABLE_BASE_LABEL
247 # define JUMP(ChExpr, table) \
253 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
254 : table[CHAR_CLASS (spec)]; \
255 ptr = &&JUMP_TABLE_BASE_LABEL + offset; \
260 # define JUMP_TABLE_TYPE const void *const
261 # define REF(Name) &&do_##Name
262 # define JUMP(ChExpr, table) \
267 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
268 : table[CHAR_CLASS (spec)]; \
274 #define STEP0_3_TABLE \
275 /* Step 0: at the beginning. */ \
276 static JUMP_TABLE_TYPE step0_jumps[30] = \
278 REF (form_unknown), \
279 REF (flag_space), /* for ' ' */ \
280 REF (flag_plus), /* for '+' */ \
281 REF (flag_minus), /* for '-' */ \
282 REF (flag_hash), /* for '<hash>' */ \
283 REF (flag_zero), /* for '0' */ \
284 REF (flag_quote), /* for '\'' */ \
285 REF (width_asterics), /* for '*' */ \
286 REF (width), /* for '1'...'9' */ \
287 REF (precision), /* for '.' */ \
288 REF (mod_half), /* for 'h' */ \
289 REF (mod_long), /* for 'l' */ \
290 REF (mod_longlong), /* for 'L', 'q' */ \
291 REF (mod_size_t), /* for 'z', 'Z' */ \
292 REF (form_percent), /* for '%' */ \
293 REF (form_integer), /* for 'd', 'i' */ \
294 REF (form_unsigned), /* for 'u' */ \
295 REF (form_octal), /* for 'o' */ \
296 REF (form_hexa), /* for 'X', 'x' */ \
297 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
298 REF (form_character), /* for 'c' */ \
299 REF (form_string), /* for 's', 'S' */ \
300 REF (form_pointer), /* for 'p' */ \
301 REF (form_number), /* for 'n' */ \
302 REF (form_strerror), /* for 'm' */ \
303 REF (form_wcharacter), /* for 'C' */ \
304 REF (form_floathex), /* for 'A', 'a' */ \
305 REF (mod_ptrdiff_t), /* for 't' */ \
306 REF (mod_intmax_t), /* for 'j' */ \
307 REF (flag_i18n), /* for 'I' */ \
309 /* Step 1: after processing width. */ \
310 static JUMP_TABLE_TYPE step1_jumps[30] = \
312 REF (form_unknown), \
313 REF (form_unknown), /* for ' ' */ \
314 REF (form_unknown), /* for '+' */ \
315 REF (form_unknown), /* for '-' */ \
316 REF (form_unknown), /* for '<hash>' */ \
317 REF (form_unknown), /* for '0' */ \
318 REF (form_unknown), /* for '\'' */ \
319 REF (form_unknown), /* for '*' */ \
320 REF (form_unknown), /* for '1'...'9' */ \
321 REF (precision), /* for '.' */ \
322 REF (mod_half), /* for 'h' */ \
323 REF (mod_long), /* for 'l' */ \
324 REF (mod_longlong), /* for 'L', 'q' */ \
325 REF (mod_size_t), /* for 'z', 'Z' */ \
326 REF (form_percent), /* for '%' */ \
327 REF (form_integer), /* for 'd', 'i' */ \
328 REF (form_unsigned), /* for 'u' */ \
329 REF (form_octal), /* for 'o' */ \
330 REF (form_hexa), /* for 'X', 'x' */ \
331 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
332 REF (form_character), /* for 'c' */ \
333 REF (form_string), /* for 's', 'S' */ \
334 REF (form_pointer), /* for 'p' */ \
335 REF (form_number), /* for 'n' */ \
336 REF (form_strerror), /* for 'm' */ \
337 REF (form_wcharacter), /* for 'C' */ \
338 REF (form_floathex), /* for 'A', 'a' */ \
339 REF (mod_ptrdiff_t), /* for 't' */ \
340 REF (mod_intmax_t), /* for 'j' */ \
341 REF (form_unknown) /* for 'I' */ \
343 /* Step 2: after processing precision. */ \
344 static JUMP_TABLE_TYPE step2_jumps[30] = \
346 REF (form_unknown), \
347 REF (form_unknown), /* for ' ' */ \
348 REF (form_unknown), /* for '+' */ \
349 REF (form_unknown), /* for '-' */ \
350 REF (form_unknown), /* for '<hash>' */ \
351 REF (form_unknown), /* for '0' */ \
352 REF (form_unknown), /* for '\'' */ \
353 REF (form_unknown), /* for '*' */ \
354 REF (form_unknown), /* for '1'...'9' */ \
355 REF (form_unknown), /* for '.' */ \
356 REF (mod_half), /* for 'h' */ \
357 REF (mod_long), /* for 'l' */ \
358 REF (mod_longlong), /* for 'L', 'q' */ \
359 REF (mod_size_t), /* for 'z', 'Z' */ \
360 REF (form_percent), /* for '%' */ \
361 REF (form_integer), /* for 'd', 'i' */ \
362 REF (form_unsigned), /* for 'u' */ \
363 REF (form_octal), /* for 'o' */ \
364 REF (form_hexa), /* for 'X', 'x' */ \
365 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
366 REF (form_character), /* for 'c' */ \
367 REF (form_string), /* for 's', 'S' */ \
368 REF (form_pointer), /* for 'p' */ \
369 REF (form_number), /* for 'n' */ \
370 REF (form_strerror), /* for 'm' */ \
371 REF (form_wcharacter), /* for 'C' */ \
372 REF (form_floathex), /* for 'A', 'a' */ \
373 REF (mod_ptrdiff_t), /* for 't' */ \
374 REF (mod_intmax_t), /* for 'j' */ \
375 REF (form_unknown) /* for 'I' */ \
377 /* Step 3a: after processing first 'h' modifier. */ \
378 static JUMP_TABLE_TYPE step3a_jumps[30] = \
380 REF (form_unknown), \
381 REF (form_unknown), /* for ' ' */ \
382 REF (form_unknown), /* for '+' */ \
383 REF (form_unknown), /* for '-' */ \
384 REF (form_unknown), /* for '<hash>' */ \
385 REF (form_unknown), /* for '0' */ \
386 REF (form_unknown), /* for '\'' */ \
387 REF (form_unknown), /* for '*' */ \
388 REF (form_unknown), /* for '1'...'9' */ \
389 REF (form_unknown), /* for '.' */ \
390 REF (mod_halfhalf), /* for 'h' */ \
391 REF (form_unknown), /* for 'l' */ \
392 REF (form_unknown), /* for 'L', 'q' */ \
393 REF (form_unknown), /* for 'z', 'Z' */ \
394 REF (form_percent), /* for '%' */ \
395 REF (form_integer), /* for 'd', 'i' */ \
396 REF (form_unsigned), /* for 'u' */ \
397 REF (form_octal), /* for 'o' */ \
398 REF (form_hexa), /* for 'X', 'x' */ \
399 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
400 REF (form_unknown), /* for 'c' */ \
401 REF (form_unknown), /* for 's', 'S' */ \
402 REF (form_unknown), /* for 'p' */ \
403 REF (form_number), /* for 'n' */ \
404 REF (form_unknown), /* for 'm' */ \
405 REF (form_unknown), /* for 'C' */ \
406 REF (form_unknown), /* for 'A', 'a' */ \
407 REF (form_unknown), /* for 't' */ \
408 REF (form_unknown), /* for 'j' */ \
409 REF (form_unknown) /* for 'I' */ \
411 /* Step 3b: after processing first 'l' modifier. */ \
412 static JUMP_TABLE_TYPE step3b_jumps[30] = \
414 REF (form_unknown), \
415 REF (form_unknown), /* for ' ' */ \
416 REF (form_unknown), /* for '+' */ \
417 REF (form_unknown), /* for '-' */ \
418 REF (form_unknown), /* for '<hash>' */ \
419 REF (form_unknown), /* for '0' */ \
420 REF (form_unknown), /* for '\'' */ \
421 REF (form_unknown), /* for '*' */ \
422 REF (form_unknown), /* for '1'...'9' */ \
423 REF (form_unknown), /* for '.' */ \
424 REF (form_unknown), /* for 'h' */ \
425 REF (mod_longlong), /* for 'l' */ \
426 REF (form_unknown), /* for 'L', 'q' */ \
427 REF (form_unknown), /* for 'z', 'Z' */ \
428 REF (form_percent), /* for '%' */ \
429 REF (form_integer), /* for 'd', 'i' */ \
430 REF (form_unsigned), /* for 'u' */ \
431 REF (form_octal), /* for 'o' */ \
432 REF (form_hexa), /* for 'X', 'x' */ \
433 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
434 REF (form_character), /* for 'c' */ \
435 REF (form_string), /* for 's', 'S' */ \
436 REF (form_pointer), /* for 'p' */ \
437 REF (form_number), /* for 'n' */ \
438 REF (form_strerror), /* for 'm' */ \
439 REF (form_wcharacter), /* for 'C' */ \
440 REF (form_floathex), /* for 'A', 'a' */ \
441 REF (form_unknown), /* for 't' */ \
442 REF (form_unknown), /* for 'j' */ \
443 REF (form_unknown) /* for 'I' */ \
446 #define STEP4_TABLE \
447 /* Step 4: processing format specifier. */ \
448 static JUMP_TABLE_TYPE step4_jumps[30] = \
450 REF (form_unknown), \
451 REF (form_unknown), /* for ' ' */ \
452 REF (form_unknown), /* for '+' */ \
453 REF (form_unknown), /* for '-' */ \
454 REF (form_unknown), /* for '<hash>' */ \
455 REF (form_unknown), /* for '0' */ \
456 REF (form_unknown), /* for '\'' */ \
457 REF (form_unknown), /* for '*' */ \
458 REF (form_unknown), /* for '1'...'9' */ \
459 REF (form_unknown), /* for '.' */ \
460 REF (form_unknown), /* for 'h' */ \
461 REF (form_unknown), /* for 'l' */ \
462 REF (form_unknown), /* for 'L', 'q' */ \
463 REF (form_unknown), /* for 'z', 'Z' */ \
464 REF (form_percent), /* for '%' */ \
465 REF (form_integer), /* for 'd', 'i' */ \
466 REF (form_unsigned), /* for 'u' */ \
467 REF (form_octal), /* for 'o' */ \
468 REF (form_hexa), /* for 'X', 'x' */ \
469 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
470 REF (form_character), /* for 'c' */ \
471 REF (form_string), /* for 's', 'S' */ \
472 REF (form_pointer), /* for 'p' */ \
473 REF (form_number), /* for 'n' */ \
474 REF (form_strerror), /* for 'm' */ \
475 REF (form_wcharacter), /* for 'C' */ \
476 REF (form_floathex), /* for 'A', 'a' */ \
477 REF (form_unknown), /* for 't' */ \
478 REF (form_unknown), /* for 'j' */ \
479 REF (form_unknown) /* for 'I' */ \
483 #define process_arg(fspec) \
484 /* Start real work. We know about all flags and modifiers and \
485 now process the wanted format specifier. */ \
486 LABEL (form_percent): \
487 /* Write a literal "%". */ \
491 LABEL (form_integer): \
492 /* Signed decimal integer. */ \
497 long long int signed_number; \
500 signed_number = va_arg (ap, long long int); \
502 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
504 is_negative = signed_number < 0; \
505 number.longlong = is_negative ? (- signed_number) : signed_number; \
507 goto LABEL (longlong_number); \
511 long int signed_number; \
516 signed_number = va_arg (ap, long int); \
518 signed_number = (signed char) va_arg (ap, unsigned int); \
519 else if (!is_short) \
520 signed_number = va_arg (ap, int); \
522 signed_number = (short int) va_arg (ap, unsigned int); \
526 signed_number = args_value[fspec->data_arg].pa_long_int; \
528 signed_number = (signed char) \
529 args_value[fspec->data_arg].pa_u_int; \
530 else if (!is_short) \
531 signed_number = args_value[fspec->data_arg].pa_int; \
533 signed_number = (short int) \
534 args_value[fspec->data_arg].pa_u_int; \
536 is_negative = signed_number < 0; \
537 number.word = is_negative ? (- signed_number) : signed_number; \
539 goto LABEL (number); \
543 LABEL (form_unsigned): \
544 /* Unsigned decimal integer. */ \
546 goto LABEL (unsigned_number); \
549 LABEL (form_octal): \
550 /* Unsigned octal integer. */ \
552 goto LABEL (unsigned_number); \
556 /* Unsigned hexadecimal integer. */ \
559 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
561 /* ISO specifies the `+' and ` ' flags only for signed \
570 number.longlong = va_arg (ap, unsigned long long int); \
572 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
574 LABEL (longlong_number): \
576 /* Supply a default precision if none was given. */ \
579 /* We have to take care for the '0' flag. If a precision \
580 is given it must be ignored. */ \
583 /* If the precision is 0 and the number is 0 nothing has to \
584 be written for the number, except for the 'o' format in \
586 if (prec == 0 && number.longlong == 0) \
589 if (base == 8 && alt) \
590 *--string = L_('0'); \
594 /* Put the number in WORK. */ \
595 string = _itoa (number.longlong, workend, base, \
597 if (group && grouping) \
598 string = group_number (string, workend, grouping, \
601 if (use_outdigits && base == 10) \
602 string = _i18n_number_rewrite (string, workend, workend); \
604 /* Simplify further test for num != 0. */ \
605 number.word = number.longlong != 0; \
612 number.word = va_arg (ap, unsigned long int); \
614 number.word = (unsigned char) va_arg (ap, unsigned int); \
615 else if (!is_short) \
616 number.word = va_arg (ap, unsigned int); \
618 number.word = (unsigned short int) va_arg (ap, unsigned int); \
622 number.word = args_value[fspec->data_arg].pa_u_long_int; \
624 number.word = (unsigned char) \
625 args_value[fspec->data_arg].pa_u_int; \
626 else if (!is_short) \
627 number.word = args_value[fspec->data_arg].pa_u_int; \
629 number.word = (unsigned short int) \
630 args_value[fspec->data_arg].pa_u_int; \
634 /* Supply a default precision if none was given. */ \
637 /* We have to take care for the '0' flag. If a precision \
638 is given it must be ignored. */ \
641 /* If the precision is 0 and the number is 0 nothing has to \
642 be written for the number, except for the 'o' format in \
644 if (prec == 0 && number.word == 0) \
647 if (base == 8 && alt) \
648 *--string = L_('0'); \
652 /* Put the number in WORK. */ \
653 string = _itoa_word (number.word, workend, base, \
655 if (group && grouping) \
656 string = group_number (string, workend, grouping, \
659 if (use_outdigits && base == 10) \
660 string = _i18n_number_rewrite (string, workend, workend); \
664 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
665 /* Add octal marker. */ \
666 *--string = L_('0'); \
668 prec = MAX (0, prec - (workend - string)); \
672 width -= workend - string + prec; \
674 if (number.word != 0 && alt && base == 16) \
675 /* Account for 0X hex marker. */ \
678 if (is_negative || showsign || space) \
681 if (pad == L_(' ')) \
694 if (number.word != 0 && alt && base == 16) \
703 outstring (string, workend - string); \
725 if (number.word != 0 && alt && base == 16) \
732 width -= workend - string + prec; \
742 outstring (string, workend - string); \
748 LABEL (form_float): \
750 /* Floating-point number. This is handled by printf_fp.c. */ \
757 is_long_double = 0; \
759 struct printf_info info = { .prec = prec, \
762 .is_long_double = is_long_double, \
763 .is_short = is_short, \
764 .is_long = is_long, \
768 .showsign = showsign, \
772 .i18n = use_outdigits, \
773 .wide = sizeof (CHAR_T) != 1, \
774 .is_binary128 = 0}; \
776 if (is_long_double) \
777 the_arg.pa_long_double = va_arg (ap, long double); \
779 the_arg.pa_double = va_arg (ap, double); \
780 ptr = (const void *) &the_arg; \
782 function_done = __printf_fp (s, &info, &ptr); \
786 ptr = (const void *) &args_value[fspec->data_arg]; \
789 fspec->data_arg_type = PA_DOUBLE; \
790 fspec->info.is_long_double = 0; \
792 /* Not supported by *printf functions. */ \
793 fspec->info.is_binary128 = 0; \
795 function_done = __printf_fp (s, &fspec->info, &ptr); \
798 if (function_done < 0) \
800 /* Error in print handler; up to handler to set errno. */ \
805 done_add (function_done); \
809 LABEL (form_floathex): \
811 /* Floating point number printed as hexadecimal number. */ \
818 is_long_double = 0; \
820 struct printf_info info = { .prec = prec, \
823 .is_long_double = is_long_double, \
824 .is_short = is_short, \
825 .is_long = is_long, \
829 .showsign = showsign, \
833 .wide = sizeof (CHAR_T) != 1, \
834 .is_binary128 = 0}; \
836 if (is_long_double) \
837 the_arg.pa_long_double = va_arg (ap, long double); \
839 the_arg.pa_double = va_arg (ap, double); \
840 ptr = (const void *) &the_arg; \
842 function_done = __printf_fphex (s, &info, &ptr); \
846 ptr = (const void *) &args_value[fspec->data_arg]; \
848 fspec->info.is_long_double = 0; \
849 /* Not supported by *printf functions. */ \
850 fspec->info.is_binary128 = 0; \
852 function_done = __printf_fphex (s, &fspec->info, &ptr); \
855 if (function_done < 0) \
857 /* Error in print handler; up to handler to set errno. */ \
862 done_add (function_done); \
866 LABEL (form_pointer): \
867 /* Generic pointer. */ \
871 ptr = va_arg (ap, void *); \
873 ptr = args_value[fspec->data_arg].pa_pointer; \
876 /* If the pointer is not NULL, write it as a %#x spec. */ \
878 number.word = (unsigned long int) ptr; \
883 goto LABEL (number); \
887 /* Write "(nil)" for a nil pointer. */ \
888 string = (CHAR_T *) L_("(nil)"); \
889 /* Make sure the full string "(nil)" is printed. */ \
892 /* This is a wide string iff compiling wprintf. */ \
893 is_long = sizeof (CHAR_T) > 1; \
894 goto LABEL (print_string); \
899 LABEL (form_number): \
900 if (s->_flags2 & _IO_FLAGS2_FORTIFY) \
902 if (! readonly_format) \
904 extern int __readonly_area (const void *, size_t) \
907 = __readonly_area (format, ((STR_LEN (format) + 1) \
908 * sizeof (CHAR_T))); \
910 if (readonly_format < 0) \
911 __libc_fatal ("*** %n in writable segment detected ***\n"); \
913 /* Answer the count of characters written. */ \
917 *(long long int *) va_arg (ap, void *) = done; \
918 else if (is_long_num) \
919 *(long int *) va_arg (ap, void *) = done; \
921 *(char *) va_arg (ap, void *) = done; \
922 else if (!is_short) \
923 *(int *) va_arg (ap, void *) = done; \
925 *(short int *) va_arg (ap, void *) = done; \
929 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
930 else if (is_long_num) \
931 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
933 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
934 else if (!is_short) \
935 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
937 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
940 LABEL (form_strerror): \
941 /* Print description of error ERRNO. */ \
943 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
944 WORK_BUFFER_SIZE * sizeof (CHAR_T)); \
945 is_long = 0; /* This is no wide-char string. */ \
946 goto LABEL (print_string)
948 #ifdef COMPILE_WPRINTF
949 # define process_string_arg(fspec) \
950 LABEL (form_character): \
953 goto LABEL (form_wcharacter); \
954 --width; /* Account for the character itself. */ \
958 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
960 outchar (__btowc ((unsigned char) \
961 args_value[fspec->data_arg].pa_int)); \
966 LABEL (form_wcharacter): \
968 /* Wide character. */ \
973 outchar (va_arg (ap, wchar_t)); \
975 outchar (args_value[fspec->data_arg].pa_wchar); \
981 LABEL (form_string): \
984 int string_malloced; \
986 /* The string argument could in fact be `char *' or `wchar_t *'. \
987 But this should not make a difference here. */ \
989 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
991 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
993 /* Entry point for printing other strings. */ \
994 LABEL (print_string): \
996 string_malloced = 0; \
997 if (string == NULL) \
999 /* Write "(null)" if there's space. */ \
1001 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
1003 string = (CHAR_T *) null; \
1004 len = (sizeof (null) / sizeof (null[0])) - 1; \
1008 string = (CHAR_T *) L""; \
1012 else if (!is_long && spec != L_('S')) \
1014 /* This is complicated. We have to transform the multibyte \
1015 string into a wide character string. */ \
1016 const char *mbs = (const char *) string; \
1017 mbstate_t mbstate; \
1019 len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
1021 /* Allocate dynamically an array which definitely is long \
1022 enough for the wide character version. Each byte in the \
1023 multi-byte string can produce at most one wide character. */ \
1024 if (__glibc_unlikely (len > SIZE_MAX / sizeof (wchar_t))) \
1026 __set_errno (EOVERFLOW); \
1030 else if (__libc_use_alloca (len * sizeof (wchar_t))) \
1031 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1032 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1039 string_malloced = 1; \
1041 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1042 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1043 if (len == (size_t) -1) \
1045 /* Illegal multibyte character. */ \
1053 /* Search for the end of the string, but don't search past \
1054 the length specified by the precision. */ \
1055 len = __wcsnlen (string, prec); \
1057 len = __wcslen (string); \
1060 if ((width -= len) < 0) \
1062 outstring (string, len); \
1068 outstring (string, len); \
1071 if (__glibc_unlikely (string_malloced)) \
1076 # define process_string_arg(fspec) \
1077 LABEL (form_character): \
1080 goto LABEL (form_wcharacter); \
1081 --width; /* Account for the character itself. */ \
1084 if (fspec == NULL) \
1085 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1087 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1092 LABEL (form_wcharacter): \
1094 /* Wide character. */ \
1095 char buf[MB_LEN_MAX]; \
1096 mbstate_t mbstate; \
1099 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1100 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1101 : args_value[fspec->data_arg].pa_wchar), \
1103 if (len == (size_t) -1) \
1105 /* Something went wrong during the conversion. Bail out. */ \
1112 outstring (buf, len); \
1118 LABEL (form_string): \
1121 int string_malloced; \
1123 /* The string argument could in fact be `char *' or `wchar_t *'. \
1124 But this should not make a difference here. */ \
1125 if (fspec == NULL) \
1126 string = (char *) va_arg (ap, const char *); \
1128 string = (char *) args_value[fspec->data_arg].pa_string; \
1130 /* Entry point for printing other strings. */ \
1131 LABEL (print_string): \
1133 string_malloced = 0; \
1134 if (string == NULL) \
1136 /* Write "(null)" if there's space. */ \
1137 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1139 string = (char *) null; \
1140 len = sizeof (null) - 1; \
1144 string = (char *) ""; \
1148 else if (!is_long && spec != L_('S')) \
1151 /* Search for the end of the string, but don't search past \
1152 the length (in bytes) specified by the precision. */ \
1153 len = __strnlen (string, prec); \
1155 len = strlen (string); \
1159 const wchar_t *s2 = (const wchar_t *) string; \
1160 mbstate_t mbstate; \
1162 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1166 /* The string `s2' might not be NUL terminated. */ \
1167 if (__libc_use_alloca (prec)) \
1168 string = (char *) alloca (prec); \
1169 else if ((string = (char *) malloc (prec)) == NULL) \
1175 string_malloced = 1; \
1176 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1180 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1181 if (len != (size_t) -1) \
1183 assert (__mbsinit (&mbstate)); \
1184 s2 = (const wchar_t *) string; \
1185 if (__libc_use_alloca (len + 1)) \
1186 string = (char *) alloca (len + 1); \
1187 else if ((string = (char *) malloc (len + 1)) == NULL) \
1193 string_malloced = 1; \
1194 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1198 if (len == (size_t) -1) \
1200 /* Illegal wide-character string. */ \
1206 if ((width -= len) < 0) \
1208 outstring (string, len); \
1214 outstring (string, len); \
1217 if (__glibc_unlikely (string_malloced)) \
1223 /* Helper function to provide temporary buffering for unbuffered streams. */
1224 static int buffered_vfprintf (FILE *stream
, const CHAR_T
*fmt
, va_list)
1225 __THROW
__attribute__ ((noinline
)) internal_function
;
1227 /* Handle positional format specifiers. */
1228 static int printf_positional (_IO_FILE
*s
,
1229 const CHAR_T
*format
, int readonly_format
,
1230 va_list ap
, va_list *ap_savep
, int done
,
1231 int nspecs_done
, const UCHAR_T
*lead_str_end
,
1232 CHAR_T
*work_buffer
, int save_errno
,
1233 const char *grouping
, THOUSANDS_SEP_T
);
1235 /* Handle unknown format specifier. */
1236 static int printf_unknown (FILE *, const struct printf_info
*,
1237 const void *const *) __THROW
;
1239 /* Group digits of number string. */
1240 static CHAR_T
*group_number (CHAR_T
*, CHAR_T
*, const char *, THOUSANDS_SEP_T
)
1241 __THROW internal_function
;
1243 /* The function itself. */
1245 vfprintf (FILE *s
, const CHAR_T
*format
, va_list ap
)
1247 /* The character used as thousands separator. */
1248 THOUSANDS_SEP_T thousands_sep
= 0;
1250 /* The string describing the size of groups of digits. */
1251 const char *grouping
;
1253 /* Place to accumulate the result. */
1256 /* Current character in format string. */
1259 /* End of leading constant string. */
1260 const UCHAR_T
*lead_str_end
;
1262 /* Points to next format specifier. */
1263 const UCHAR_T
*end_of_spec
;
1265 /* Buffer intermediate results. */
1266 CHAR_T work_buffer
[WORK_BUFFER_SIZE
];
1267 CHAR_T
*workstart
= NULL
;
1270 /* We have to save the original argument pointer. */
1273 /* Count number of specifiers we already processed. */
1276 /* For the %m format we may need the current `errno' value. */
1277 int save_errno
= errno
;
1279 /* 1 if format is in read-only memory, -1 if it is in writable memory,
1281 int readonly_format
= 0;
1283 /* Orient the stream. */
1288 /* Sanity check of arguments. */
1289 ARGCHECK (s
, format
);
1292 /* Check for correct orientation. */
1293 if (_IO_vtable_offset (s
) == 0 &&
1294 _IO_fwide (s
, sizeof (CHAR_T
) == 1 ? -1 : 1)
1295 != (sizeof (CHAR_T
) == 1 ? -1 : 1))
1296 /* The stream is already oriented otherwise. */
1300 if (UNBUFFERED_P (s
))
1301 /* Use a helper function which will allocate a local temporary buffer
1302 for the stream and then call us again. */
1303 return buffered_vfprintf (s
, format
, ap
);
1305 /* Initialize local variables. */
1307 grouping
= (const char *) -1;
1309 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1310 since on some systems `va_list' is not an integral type. */
1311 __va_copy (ap_save
, ap
);
1317 #ifdef COMPILE_WPRINTF
1318 /* Find the first format specifier. */
1319 f
= lead_str_end
= __find_specwc ((const UCHAR_T
*) format
);
1321 /* Find the first format specifier. */
1322 f
= lead_str_end
= __find_specmb ((const UCHAR_T
*) format
);
1326 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile
, s
);
1329 /* Write the literal text before the first format. */
1330 outstring ((const UCHAR_T
*) format
,
1331 lead_str_end
- (const UCHAR_T
*) format
);
1333 /* If we only have to print a simple string, return now. */
1337 /* Use the slow path in case any printf handler is registered. */
1338 if (__glibc_unlikely (__printf_function_table
!= NULL
1339 || __printf_modifier_table
!= NULL
1340 || __printf_va_arg_table
!= NULL
))
1343 /* Process whole format string. */
1349 union printf_arg
*args_value
; /* This is not used here but ... */
1350 int is_negative
; /* Flag for negative number. */
1353 unsigned long long int longlong
;
1354 unsigned long int word
;
1357 union printf_arg the_arg
;
1358 CHAR_T
*string
; /* Pointer to argument string. */
1359 int alt
= 0; /* Alternate format. */
1360 int space
= 0; /* Use space prefix if no sign is needed. */
1361 int left
= 0; /* Left-justify output. */
1362 int showsign
= 0; /* Always begin with plus or minus sign. */
1363 int group
= 0; /* Print numbers according grouping rules. */
1364 int is_long_double
= 0; /* Argument is long double/ long long int. */
1365 int is_short
= 0; /* Argument is short int. */
1366 int is_long
= 0; /* Argument is long int. */
1367 int is_char
= 0; /* Argument is promoted (unsigned) char. */
1368 int width
= 0; /* Width of output; 0 means none specified. */
1369 int prec
= -1; /* Precision of output; -1 means none specified. */
1370 /* This flag is set by the 'I' modifier and selects the use of the
1371 `outdigits' as determined by the current locale. */
1372 int use_outdigits
= 0;
1373 UCHAR_T pad
= L_(' ');/* Padding character. */
1377 workend
= work_buffer
+ WORK_BUFFER_SIZE
;
1379 /* Get current character in format string. */
1380 JUMP (*++f
, step0_jumps
);
1385 JUMP (*++f
, step0_jumps
);
1390 JUMP (*++f
, step0_jumps
);
1396 JUMP (*++f
, step0_jumps
);
1401 JUMP (*++f
, step0_jumps
);
1407 JUMP (*++f
, step0_jumps
);
1409 /* The '\'' flag. */
1413 if (grouping
== (const char *) -1)
1415 #ifdef COMPILE_WPRINTF
1416 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1417 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1419 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1422 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1423 if (*grouping
== '\0' || *grouping
== CHAR_MAX
1424 #ifdef COMPILE_WPRINTF
1425 || thousands_sep
== L
'\0'
1427 || *thousands_sep
== '\0'
1432 JUMP (*++f
, step0_jumps
);
1436 JUMP (*++f
, step0_jumps
);
1438 /* Get width from argument. */
1439 LABEL (width_asterics
):
1441 const UCHAR_T
*tmp
; /* Temporary value. */
1446 int pos
= read_int (&tmp
);
1450 __set_errno (EOVERFLOW
);
1455 if (pos
&& *tmp
== L_('$'))
1456 /* The width comes from a positional parameter. */
1459 width
= va_arg (ap
, int);
1461 /* Negative width means left justified. */
1469 if (__glibc_unlikely (width
>= INT_MAX
/ sizeof (CHAR_T
) - EXTSIZ
))
1471 __set_errno (EOVERFLOW
);
1476 if (width
>= WORK_BUFFER_SIZE
- EXTSIZ
)
1478 /* We have to use a special buffer. */
1479 size_t needed
= ((size_t) width
+ EXTSIZ
) * sizeof (CHAR_T
);
1480 if (__libc_use_alloca (needed
))
1481 workend
= (CHAR_T
*) alloca (needed
) + width
+ EXTSIZ
;
1484 workstart
= (CHAR_T
*) malloc (needed
);
1485 if (workstart
== NULL
)
1490 workend
= workstart
+ width
+ EXTSIZ
;
1494 JUMP (*f
, step1_jumps
);
1496 /* Given width in format string. */
1498 width
= read_int (&f
);
1500 if (__glibc_unlikely (width
== -1
1501 || width
>= INT_MAX
/ sizeof (CHAR_T
) - EXTSIZ
))
1503 __set_errno (EOVERFLOW
);
1508 if (width
>= WORK_BUFFER_SIZE
- EXTSIZ
)
1510 /* We have to use a special buffer. */
1511 size_t needed
= ((size_t) width
+ EXTSIZ
) * sizeof (CHAR_T
);
1512 if (__libc_use_alloca (needed
))
1513 workend
= (CHAR_T
*) alloca (needed
) + width
+ EXTSIZ
;
1516 workstart
= (CHAR_T
*) malloc (needed
);
1517 if (workstart
== NULL
)
1522 workend
= workstart
+ width
+ EXTSIZ
;
1526 /* Oh, oh. The argument comes from a positional parameter. */
1528 JUMP (*f
, step1_jumps
);
1534 const UCHAR_T
*tmp
; /* Temporary value. */
1539 int pos
= read_int (&tmp
);
1543 __set_errno (EOVERFLOW
);
1548 if (pos
&& *tmp
== L_('$'))
1549 /* The precision comes from a positional parameter. */
1552 prec
= va_arg (ap
, int);
1554 /* If the precision is negative the precision is omitted. */
1558 else if (ISDIGIT (*f
))
1560 prec
= read_int (&f
);
1562 /* The precision was specified in this case as an extremely
1563 large positive value. */
1566 __set_errno (EOVERFLOW
);
1573 if (prec
> width
&& prec
> WORK_BUFFER_SIZE
- EXTSIZ
)
1575 /* Deallocate any previously allocated buffer because it is
1577 if (__glibc_unlikely (workstart
!= NULL
))
1580 if (__glibc_unlikely (prec
>= INT_MAX
/ sizeof (CHAR_T
) - EXTSIZ
))
1582 __set_errno (EOVERFLOW
);
1586 size_t needed
= ((size_t) prec
+ EXTSIZ
) * sizeof (CHAR_T
);
1588 if (__libc_use_alloca (needed
))
1589 workend
= (CHAR_T
*) alloca (needed
) + prec
+ EXTSIZ
;
1592 workstart
= (CHAR_T
*) malloc (needed
);
1593 if (workstart
== NULL
)
1598 workend
= workstart
+ prec
+ EXTSIZ
;
1601 JUMP (*f
, step2_jumps
);
1603 /* Process 'h' modifier. There might another 'h' following. */
1606 JUMP (*++f
, step3a_jumps
);
1608 /* Process 'hh' modifier. */
1609 LABEL (mod_halfhalf
):
1612 JUMP (*++f
, step4_jumps
);
1614 /* Process 'l' modifier. There might another 'l' following. */
1617 JUMP (*++f
, step3b_jumps
);
1619 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1620 allowed to follow. */
1621 LABEL (mod_longlong
):
1624 JUMP (*++f
, step4_jumps
);
1627 is_long_double
= sizeof (size_t) > sizeof (unsigned long int);
1628 is_long
= sizeof (size_t) > sizeof (unsigned int);
1629 JUMP (*++f
, step4_jumps
);
1631 LABEL (mod_ptrdiff_t
):
1632 is_long_double
= sizeof (ptrdiff_t) > sizeof (unsigned long int);
1633 is_long
= sizeof (ptrdiff_t) > sizeof (unsigned int);
1634 JUMP (*++f
, step4_jumps
);
1636 LABEL (mod_intmax_t
):
1637 is_long_double
= sizeof (intmax_t) > sizeof (unsigned long int);
1638 is_long
= sizeof (intmax_t) > sizeof (unsigned int);
1639 JUMP (*++f
, step4_jumps
);
1641 /* Process current format. */
1644 process_arg (((struct printf_spec
*) NULL
));
1645 process_string_arg (((struct printf_spec
*) NULL
));
1647 LABEL (form_unknown
):
1648 if (spec
== L_('\0'))
1650 /* The format string ended before the specifier is complete. */
1651 __set_errno (EINVAL
);
1656 /* If we are in the fast loop force entering the complicated
1661 /* The format is correctly handled. */
1664 if (__glibc_unlikely (workstart
!= NULL
))
1668 /* Look for next format specifier. */
1669 #ifdef COMPILE_WPRINTF
1670 f
= __find_specwc ((end_of_spec
= ++f
));
1672 f
= __find_specmb ((end_of_spec
= ++f
));
1675 /* Write the following constant string. */
1676 outstring (end_of_spec
, f
- end_of_spec
);
1678 while (*f
!= L_('\0'));
1680 /* Unlock stream and return. */
1683 /* Hand off processing for positional parameters. */
1685 if (__glibc_unlikely (workstart
!= NULL
))
1690 done
= printf_positional (s
, format
, readonly_format
, ap
, &ap_save
,
1691 done
, nspecs_done
, lead_str_end
, work_buffer
,
1692 save_errno
, grouping
, thousands_sep
);
1695 if (__glibc_unlikely (workstart
!= NULL
))
1697 /* Unlock the stream. */
1698 _IO_funlockfile (s
);
1699 _IO_cleanup_region_end (0);
1705 printf_positional (_IO_FILE
*s
, const CHAR_T
*format
, int readonly_format
,
1706 va_list ap
, va_list *ap_savep
, int done
, int nspecs_done
,
1707 const UCHAR_T
*lead_str_end
,
1708 CHAR_T
*work_buffer
, int save_errno
,
1709 const char *grouping
, THOUSANDS_SEP_T thousands_sep
)
1711 /* For the argument descriptions, which may be allocated on the heap. */
1712 void *args_malloced
= NULL
;
1714 /* For positional argument handling. */
1715 struct scratch_buffer specsbuf
;
1716 scratch_buffer_init (&specsbuf
);
1717 struct printf_spec
*specs
= specsbuf
.data
;
1718 size_t specs_limit
= specsbuf
.length
/ sizeof (specs
[0]);
1720 /* Array with information about the needed arguments. This has to
1721 be dynamically extensible. */
1724 /* The number of arguments the format string requests. This will
1725 determine the size of the array needed to store the argument
1728 size_t bytes_per_arg
;
1729 union printf_arg
*args_value
;
1733 /* Positional parameters refer to arguments directly. This could
1734 also determine the maximum number of arguments. Track the
1736 size_t max_ref_arg
= 0;
1738 /* Just a counter. */
1741 CHAR_T
*workstart
= NULL
;
1743 if (grouping
== (const char *) -1)
1745 #ifdef COMPILE_WPRINTF
1746 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1747 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1749 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1752 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1753 if (*grouping
== '\0' || *grouping
== CHAR_MAX
)
1757 for (const UCHAR_T
*f
= lead_str_end
; *f
!= L_('\0');
1758 f
= specs
[nspecs
++].next_fmt
)
1760 if (nspecs
== specs_limit
)
1762 if (!scratch_buffer_grow_preserve (&specsbuf
))
1767 specs
= specsbuf
.data
;
1768 specs_limit
= specsbuf
.length
/ sizeof (specs
[0]);
1771 /* Parse the format specifier. */
1772 #ifdef COMPILE_WPRINTF
1773 nargs
+= __parse_one_specwc (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1775 nargs
+= __parse_one_specmb (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1779 /* Determine the number of arguments the format string consumes. */
1780 nargs
= MAX (nargs
, max_ref_arg
);
1781 /* Calculate total size needed to represent a single argument across
1782 all three argument-related arrays. */
1783 bytes_per_arg
= (sizeof (*args_value
) + sizeof (*args_size
)
1784 + sizeof (*args_type
));
1786 /* Check for potential integer overflow. */
1787 if (__glibc_unlikely (nargs
> INT_MAX
/ bytes_per_arg
))
1789 __set_errno (EOVERFLOW
);
1794 /* Allocate memory for all three argument arrays. */
1795 if (__libc_use_alloca (nargs
* bytes_per_arg
))
1796 args_value
= alloca (nargs
* bytes_per_arg
);
1799 args_value
= args_malloced
= malloc (nargs
* bytes_per_arg
);
1800 if (args_value
== NULL
)
1807 /* Set up the remaining two arrays to each point past the end of the
1808 prior array, since space for all three has been allocated now. */
1809 args_size
= &args_value
[nargs
].pa_int
;
1810 args_type
= &args_size
[nargs
];
1811 memset (args_type
, s
->_flags2
& _IO_FLAGS2_FORTIFY
? '\xff' : '\0',
1812 nargs
* sizeof (*args_type
));
1814 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1815 still zero after this loop, format is invalid. For now we
1816 simply use 0 as the value. */
1818 /* Fill in the types of all the arguments. */
1819 for (cnt
= 0; cnt
< nspecs
; ++cnt
)
1821 /* If the width is determined by an argument this is an int. */
1822 if (specs
[cnt
].width_arg
!= -1)
1823 args_type
[specs
[cnt
].width_arg
] = PA_INT
;
1825 /* If the precision is determined by an argument this is an int. */
1826 if (specs
[cnt
].prec_arg
!= -1)
1827 args_type
[specs
[cnt
].prec_arg
] = PA_INT
;
1829 switch (specs
[cnt
].ndata_args
)
1831 case 0: /* No arguments. */
1833 case 1: /* One argument; we already have the
1835 args_type
[specs
[cnt
].data_arg
] = specs
[cnt
].data_arg_type
;
1836 args_size
[specs
[cnt
].data_arg
] = specs
[cnt
].size
;
1839 /* We have more than one argument for this format spec.
1840 We must call the arginfo function again to determine
1842 (void) (*__printf_arginfo_table
[specs
[cnt
].info
.spec
])
1844 specs
[cnt
].ndata_args
, &args_type
[specs
[cnt
].data_arg
],
1845 &args_size
[specs
[cnt
].data_arg
]);
1850 /* Now we know all the types and the order. Fill in the argument
1852 for (cnt
= 0; cnt
< nargs
; ++cnt
)
1853 switch (args_type
[cnt
])
1855 #define T(tag, mem, type) \
1857 args_value[cnt].mem = va_arg (*ap_savep, type); \
1860 T (PA_WCHAR
, pa_wchar
, wint_t);
1861 case PA_CHAR
: /* Promoted. */
1862 case PA_INT
|PA_FLAG_SHORT
: /* Promoted. */
1863 #if LONG_MAX == INT_MAX
1864 case PA_INT
|PA_FLAG_LONG
:
1866 T (PA_INT
, pa_int
, int);
1867 #if LONG_MAX == LONG_LONG_MAX
1868 case PA_INT
|PA_FLAG_LONG
:
1870 T (PA_INT
|PA_FLAG_LONG_LONG
, pa_long_long_int
, long long int);
1871 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1874 case PA_FLOAT
: /* Promoted. */
1875 T (PA_DOUBLE
, pa_double
, double);
1876 case PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
:
1879 args_value
[cnt
].pa_double
= va_arg (*ap_savep
, double);
1880 args_type
[cnt
] &= ~PA_FLAG_LONG_DOUBLE
;
1883 args_value
[cnt
].pa_long_double
= va_arg (*ap_savep
, long double);
1885 case PA_STRING
: /* All pointers are the same */
1886 case PA_WSTRING
: /* All pointers are the same */
1887 T (PA_POINTER
, pa_pointer
, void *);
1890 if ((args_type
[cnt
] & PA_FLAG_PTR
) != 0)
1891 args_value
[cnt
].pa_pointer
= va_arg (*ap_savep
, void *);
1892 else if (__glibc_unlikely (__printf_va_arg_table
!= NULL
)
1893 && __printf_va_arg_table
[args_type
[cnt
] - PA_LAST
] != NULL
)
1895 args_value
[cnt
].pa_user
= alloca (args_size
[cnt
]);
1896 (*__printf_va_arg_table
[args_type
[cnt
] - PA_LAST
])
1897 (args_value
[cnt
].pa_user
, ap_savep
);
1900 args_value
[cnt
].pa_long_double
= 0.0;
1903 /* Error case. Not all parameters appear in N$ format
1904 strings. We have no way to determine their type. */
1905 assert (s
->_flags2
& _IO_FLAGS2_FORTIFY
);
1906 __libc_fatal ("*** invalid %N$ use detected ***\n");
1909 /* Now walk through all format specifiers and process them. */
1910 for (; (size_t) nspecs_done
< nspecs
; ++nspecs_done
)
1917 unsigned long long int longlong
;
1918 unsigned long int word
;
1921 union printf_arg the_arg
;
1922 CHAR_T
*string
; /* Pointer to argument string. */
1924 /* Fill variables from values in struct. */
1925 int alt
= specs
[nspecs_done
].info
.alt
;
1926 int space
= specs
[nspecs_done
].info
.space
;
1927 int left
= specs
[nspecs_done
].info
.left
;
1928 int showsign
= specs
[nspecs_done
].info
.showsign
;
1929 int group
= specs
[nspecs_done
].info
.group
;
1930 int is_long_double
= specs
[nspecs_done
].info
.is_long_double
;
1931 int is_short
= specs
[nspecs_done
].info
.is_short
;
1932 int is_char
= specs
[nspecs_done
].info
.is_char
;
1933 int is_long
= specs
[nspecs_done
].info
.is_long
;
1934 int width
= specs
[nspecs_done
].info
.width
;
1935 int prec
= specs
[nspecs_done
].info
.prec
;
1936 int use_outdigits
= specs
[nspecs_done
].info
.i18n
;
1937 char pad
= specs
[nspecs_done
].info
.pad
;
1938 CHAR_T spec
= specs
[nspecs_done
].info
.spec
;
1941 CHAR_T
*workend
= work_buffer
+ WORK_BUFFER_SIZE
;
1943 /* Fill in last information. */
1944 if (specs
[nspecs_done
].width_arg
!= -1)
1946 /* Extract the field width from an argument. */
1947 specs
[nspecs_done
].info
.width
=
1948 args_value
[specs
[nspecs_done
].width_arg
].pa_int
;
1950 if (specs
[nspecs_done
].info
.width
< 0)
1951 /* If the width value is negative left justification is
1952 selected and the value is taken as being positive. */
1954 specs
[nspecs_done
].info
.width
*= -1;
1955 left
= specs
[nspecs_done
].info
.left
= 1;
1957 width
= specs
[nspecs_done
].info
.width
;
1960 if (specs
[nspecs_done
].prec_arg
!= -1)
1962 /* Extract the precision from an argument. */
1963 specs
[nspecs_done
].info
.prec
=
1964 args_value
[specs
[nspecs_done
].prec_arg
].pa_int
;
1966 if (specs
[nspecs_done
].info
.prec
< 0)
1967 /* If the precision is negative the precision is
1969 specs
[nspecs_done
].info
.prec
= -1;
1971 prec
= specs
[nspecs_done
].info
.prec
;
1974 /* Maybe the buffer is too small. */
1975 if (MAX (prec
, width
) + EXTSIZ
> WORK_BUFFER_SIZE
)
1977 if (__libc_use_alloca ((MAX (prec
, width
) + EXTSIZ
)
1979 workend
= ((CHAR_T
*) alloca ((MAX (prec
, width
) + EXTSIZ
)
1981 + (MAX (prec
, width
) + EXTSIZ
));
1984 workstart
= (CHAR_T
*) malloc ((MAX (prec
, width
) + EXTSIZ
)
1986 if (workstart
== NULL
)
1991 workend
= workstart
+ (MAX (prec
, width
) + EXTSIZ
);
1995 /* Process format specifiers. */
1998 extern printf_function
**__printf_function_table
;
2001 if (spec
<= UCHAR_MAX
2002 && __printf_function_table
!= NULL
2003 && __printf_function_table
[(size_t) spec
] != NULL
)
2005 const void **ptr
= alloca (specs
[nspecs_done
].ndata_args
2006 * sizeof (const void *));
2008 /* Fill in an array of pointers to the argument values. */
2009 for (unsigned int i
= 0; i
< specs
[nspecs_done
].ndata_args
;
2011 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
2013 /* Call the function. */
2014 function_done
= __printf_function_table
[(size_t) spec
]
2015 (s
, &specs
[nspecs_done
].info
, ptr
);
2017 if (function_done
!= -2)
2019 /* If an error occurred we don't have information
2020 about # of chars. */
2021 if (function_done
< 0)
2023 /* Function has set errno. */
2028 done_add (function_done
);
2033 JUMP (spec
, step4_jumps
);
2035 process_arg ((&specs
[nspecs_done
]));
2036 process_string_arg ((&specs
[nspecs_done
]));
2038 LABEL (form_unknown
):
2043 ptr
= alloca (specs
[nspecs_done
].ndata_args
2044 * sizeof (const void *));
2046 /* Fill in an array of pointers to the argument values. */
2047 for (i
= 0; i
< specs
[nspecs_done
].ndata_args
; ++i
)
2048 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
2050 /* Call the function. */
2051 function_done
= printf_unknown (s
, &specs
[nspecs_done
].info
,
2054 /* If an error occurred we don't have information about #
2056 if (function_done
< 0)
2058 /* Function has set errno. */
2063 done_add (function_done
);
2068 if (__glibc_unlikely (workstart
!= NULL
))
2072 /* Write the following constant string. */
2073 outstring (specs
[nspecs_done
].end_of_fmt
,
2074 specs
[nspecs_done
].next_fmt
2075 - specs
[nspecs_done
].end_of_fmt
);
2078 if (__glibc_unlikely (args_malloced
!= NULL
))
2079 free (args_malloced
);
2080 if (__glibc_unlikely (workstart
!= NULL
))
2082 scratch_buffer_free (&specsbuf
);
2086 /* Handle an unknown format specifier. This prints out a canonicalized
2087 representation of the format spec itself. */
2089 printf_unknown (FILE *s
, const struct printf_info
*info
,
2090 const void *const *args
)
2094 CHAR_T work_buffer
[MAX (sizeof (info
->width
), sizeof (info
->prec
)) * 3];
2095 CHAR_T
*const workend
2096 = &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
2107 else if (info
->space
)
2111 if (info
->pad
== L_('0'))
2116 if (info
->width
!= 0)
2118 w
= _itoa_word (info
->width
, workend
, 10, 0);
2123 if (info
->prec
!= -1)
2126 w
= _itoa_word (info
->prec
, workend
, 10, 0);
2131 if (info
->spec
!= L_('\0'))
2132 outchar (info
->spec
);
2138 /* Group the digits according to the grouping rules of the current locale.
2139 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
2142 group_number (CHAR_T
*w
, CHAR_T
*rear_ptr
, const char *grouping
,
2143 THOUSANDS_SEP_T thousands_sep
)
2147 #ifndef COMPILE_WPRINTF
2148 int tlen
= strlen (thousands_sep
);
2151 /* We treat all negative values like CHAR_MAX. */
2153 if (*grouping
== CHAR_MAX
|| *grouping
<= 0)
2154 /* No grouping should be done. */
2159 /* Copy existing string so that nothing gets overwritten. */
2160 src
= (CHAR_T
*) alloca ((rear_ptr
- w
) * sizeof (CHAR_T
));
2161 s
= (CHAR_T
*) __mempcpy (src
, w
,
2162 (rear_ptr
- w
) * sizeof (CHAR_T
));
2165 /* Process all characters in the string. */
2170 if (--len
== 0 && s
> src
)
2172 /* A new group begins. */
2173 #ifdef COMPILE_WPRINTF
2174 *--w
= thousands_sep
;
2178 *--w
= thousands_sep
[--cnt
];
2182 if (*grouping
== CHAR_MAX
2188 /* No further grouping to be done.
2189 Copy the rest of the number. */
2195 else if (*grouping
!= '\0')
2196 /* The previous grouping repeats ad infinitum. */
2205 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2208 struct _IO_FILE_plus _f
;
2209 #ifdef COMPILE_WPRINTF
2210 struct _IO_wide_data _wide_data
;
2212 _IO_FILE
*_put_stream
;
2213 #ifdef _IO_MTSAFE_IO
2219 _IO_helper_overflow (_IO_FILE
*s
, int c
)
2221 _IO_FILE
*target
= ((struct helper_file
*) s
)->_put_stream
;
2222 #ifdef COMPILE_WPRINTF
2223 int used
= s
->_wide_data
->_IO_write_ptr
- s
->_wide_data
->_IO_write_base
;
2226 _IO_size_t written
= _IO_sputn (target
, s
->_wide_data
->_IO_write_base
,
2228 if (written
== 0 || written
== WEOF
)
2230 __wmemmove (s
->_wide_data
->_IO_write_base
,
2231 s
->_wide_data
->_IO_write_base
+ written
,
2233 s
->_wide_data
->_IO_write_ptr
-= written
;
2236 int used
= s
->_IO_write_ptr
- s
->_IO_write_base
;
2239 _IO_size_t written
= _IO_sputn (target
, s
->_IO_write_base
, used
);
2240 if (written
== 0 || written
== EOF
)
2242 memmove (s
->_IO_write_base
, s
->_IO_write_base
+ written
,
2244 s
->_IO_write_ptr
-= written
;
2250 #ifdef COMPILE_WPRINTF
2251 static const struct _IO_jump_t _IO_helper_jumps libio_vtable
=
2254 JUMP_INIT (finish
, _IO_wdefault_finish
),
2255 JUMP_INIT (overflow
, _IO_helper_overflow
),
2256 JUMP_INIT (underflow
, _IO_default_underflow
),
2257 JUMP_INIT (uflow
, _IO_default_uflow
),
2258 JUMP_INIT (pbackfail
, (_IO_pbackfail_t
) _IO_wdefault_pbackfail
),
2259 JUMP_INIT (xsputn
, _IO_wdefault_xsputn
),
2260 JUMP_INIT (xsgetn
, _IO_wdefault_xsgetn
),
2261 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2262 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2263 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2264 JUMP_INIT (sync
, _IO_default_sync
),
2265 JUMP_INIT (doallocate
, _IO_wdefault_doallocate
),
2266 JUMP_INIT (read
, _IO_default_read
),
2267 JUMP_INIT (write
, _IO_default_write
),
2268 JUMP_INIT (seek
, _IO_default_seek
),
2269 JUMP_INIT (close
, _IO_default_close
),
2270 JUMP_INIT (stat
, _IO_default_stat
)
2273 static const struct _IO_jump_t _IO_helper_jumps libio_vtable
=
2276 JUMP_INIT (finish
, _IO_default_finish
),
2277 JUMP_INIT (overflow
, _IO_helper_overflow
),
2278 JUMP_INIT (underflow
, _IO_default_underflow
),
2279 JUMP_INIT (uflow
, _IO_default_uflow
),
2280 JUMP_INIT (pbackfail
, _IO_default_pbackfail
),
2281 JUMP_INIT (xsputn
, _IO_default_xsputn
),
2282 JUMP_INIT (xsgetn
, _IO_default_xsgetn
),
2283 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2284 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2285 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2286 JUMP_INIT (sync
, _IO_default_sync
),
2287 JUMP_INIT (doallocate
, _IO_default_doallocate
),
2288 JUMP_INIT (read
, _IO_default_read
),
2289 JUMP_INIT (write
, _IO_default_write
),
2290 JUMP_INIT (seek
, _IO_default_seek
),
2291 JUMP_INIT (close
, _IO_default_close
),
2292 JUMP_INIT (stat
, _IO_default_stat
)
2298 buffered_vfprintf (_IO_FILE
*s
, const CHAR_T
*format
,
2301 CHAR_T buf
[_IO_BUFSIZ
];
2302 struct helper_file helper
;
2303 _IO_FILE
*hp
= (_IO_FILE
*) &helper
._f
;
2304 int result
, to_flush
;
2306 /* Orient the stream. */
2311 /* Initialize helper. */
2312 helper
._put_stream
= s
;
2313 #ifdef COMPILE_WPRINTF
2314 hp
->_wide_data
= &helper
._wide_data
;
2315 _IO_wsetp (hp
, buf
, buf
+ sizeof buf
/ sizeof (CHAR_T
));
2318 _IO_setp (hp
, buf
, buf
+ sizeof buf
);
2321 hp
->_IO_file_flags
= _IO_MAGIC
|_IO_NO_READS
|_IO_USER_LOCK
;
2322 #if _IO_JUMPS_OFFSET
2323 hp
->_vtable_offset
= 0;
2325 #ifdef _IO_MTSAFE_IO
2328 hp
->_flags2
= s
->_flags2
;
2329 _IO_JUMPS (&helper
._f
) = (struct _IO_jump_t
*) &_IO_helper_jumps
;
2331 /* Now print to helper instead. */
2332 #ifndef COMPILE_WPRINTF
2333 result
= _IO_vfprintf (hp
, format
, args
);
2335 result
= vfprintf (hp
, format
, args
);
2339 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile
, s
);
2342 /* Now flush anything from the helper to the S. */
2343 #ifdef COMPILE_WPRINTF
2344 if ((to_flush
= (hp
->_wide_data
->_IO_write_ptr
2345 - hp
->_wide_data
->_IO_write_base
)) > 0)
2347 if ((int) _IO_sputn (s
, hp
->_wide_data
->_IO_write_base
, to_flush
)
2352 if ((to_flush
= hp
->_IO_write_ptr
- hp
->_IO_write_base
) > 0)
2354 if ((int) _IO_sputn (s
, hp
->_IO_write_base
, to_flush
) != to_flush
)
2359 /* Unlock the stream. */
2360 _IO_funlockfile (s
);
2361 __libc_cleanup_region_end (0);
2367 #ifdef COMPILE_WPRINTF
2368 strong_alias (_IO_vfwprintf
, __vfwprintf
);
2369 ldbl_weak_alias (_IO_vfwprintf
, vfwprintf
);
2371 ldbl_strong_alias (_IO_vfprintf_internal
, vfprintf
);
2372 ldbl_hidden_def (_IO_vfprintf_internal
, vfprintf
)
2373 ldbl_strong_alias (_IO_vfprintf_internal
, _IO_vfprintf
);
2374 ldbl_hidden_def (_IO_vfprintf_internal
, _IO_vfprintf
)