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 }; \
775 if (is_long_double) \
776 the_arg.pa_long_double = va_arg (ap, long double); \
778 the_arg.pa_double = va_arg (ap, double); \
779 ptr = (const void *) &the_arg; \
781 function_done = __printf_fp (s, &info, &ptr); \
785 ptr = (const void *) &args_value[fspec->data_arg]; \
788 fspec->data_arg_type = PA_DOUBLE; \
789 fspec->info.is_long_double = 0; \
792 function_done = __printf_fp (s, &fspec->info, &ptr); \
795 if (function_done < 0) \
797 /* Error in print handler; up to handler to set errno. */ \
802 done_add (function_done); \
806 LABEL (form_floathex): \
808 /* Floating point number printed as hexadecimal number. */ \
815 is_long_double = 0; \
817 struct printf_info info = { .prec = prec, \
820 .is_long_double = is_long_double, \
821 .is_short = is_short, \
822 .is_long = is_long, \
826 .showsign = showsign, \
830 .wide = sizeof (CHAR_T) != 1 }; \
832 if (is_long_double) \
833 the_arg.pa_long_double = va_arg (ap, long double); \
835 the_arg.pa_double = va_arg (ap, double); \
836 ptr = (const void *) &the_arg; \
838 function_done = __printf_fphex (s, &info, &ptr); \
842 ptr = (const void *) &args_value[fspec->data_arg]; \
844 fspec->info.is_long_double = 0; \
846 function_done = __printf_fphex (s, &fspec->info, &ptr); \
849 if (function_done < 0) \
851 /* Error in print handler; up to handler to set errno. */ \
856 done_add (function_done); \
860 LABEL (form_pointer): \
861 /* Generic pointer. */ \
865 ptr = va_arg (ap, void *); \
867 ptr = args_value[fspec->data_arg].pa_pointer; \
870 /* If the pointer is not NULL, write it as a %#x spec. */ \
872 number.word = (unsigned long int) ptr; \
877 goto LABEL (number); \
881 /* Write "(nil)" for a nil pointer. */ \
882 string = (CHAR_T *) L_("(nil)"); \
883 /* Make sure the full string "(nil)" is printed. */ \
886 /* This is a wide string iff compiling wprintf. */ \
887 is_long = sizeof (CHAR_T) > 1; \
888 goto LABEL (print_string); \
893 LABEL (form_number): \
894 if (s->_flags2 & _IO_FLAGS2_FORTIFY) \
896 if (! readonly_format) \
898 extern int __readonly_area (const void *, size_t) \
901 = __readonly_area (format, ((STR_LEN (format) + 1) \
902 * sizeof (CHAR_T))); \
904 if (readonly_format < 0) \
905 __libc_fatal ("*** %n in writable segment detected ***\n"); \
907 /* Answer the count of characters written. */ \
911 *(long long int *) va_arg (ap, void *) = done; \
912 else if (is_long_num) \
913 *(long int *) va_arg (ap, void *) = done; \
915 *(char *) va_arg (ap, void *) = done; \
916 else if (!is_short) \
917 *(int *) va_arg (ap, void *) = done; \
919 *(short int *) va_arg (ap, void *) = done; \
923 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
924 else if (is_long_num) \
925 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
927 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
928 else if (!is_short) \
929 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
931 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
934 LABEL (form_strerror): \
935 /* Print description of error ERRNO. */ \
937 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
938 WORK_BUFFER_SIZE * sizeof (CHAR_T)); \
939 is_long = 0; /* This is no wide-char string. */ \
940 goto LABEL (print_string)
942 #ifdef COMPILE_WPRINTF
943 # define process_string_arg(fspec) \
944 LABEL (form_character): \
947 goto LABEL (form_wcharacter); \
948 --width; /* Account for the character itself. */ \
952 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
954 outchar (__btowc ((unsigned char) \
955 args_value[fspec->data_arg].pa_int)); \
960 LABEL (form_wcharacter): \
962 /* Wide character. */ \
967 outchar (va_arg (ap, wchar_t)); \
969 outchar (args_value[fspec->data_arg].pa_wchar); \
975 LABEL (form_string): \
978 int string_malloced; \
980 /* The string argument could in fact be `char *' or `wchar_t *'. \
981 But this should not make a difference here. */ \
983 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
985 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
987 /* Entry point for printing other strings. */ \
988 LABEL (print_string): \
990 string_malloced = 0; \
991 if (string == NULL) \
993 /* Write "(null)" if there's space. */ \
995 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
997 string = (CHAR_T *) null; \
998 len = (sizeof (null) / sizeof (null[0])) - 1; \
1002 string = (CHAR_T *) L""; \
1006 else if (!is_long && spec != L_('S')) \
1008 /* This is complicated. We have to transform the multibyte \
1009 string into a wide character string. */ \
1010 const char *mbs = (const char *) string; \
1011 mbstate_t mbstate; \
1013 len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
1015 /* Allocate dynamically an array which definitely is long \
1016 enough for the wide character version. Each byte in the \
1017 multi-byte string can produce at most one wide character. */ \
1018 if (__glibc_unlikely (len > SIZE_MAX / sizeof (wchar_t))) \
1020 __set_errno (EOVERFLOW); \
1024 else if (__libc_use_alloca (len * sizeof (wchar_t))) \
1025 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1026 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1033 string_malloced = 1; \
1035 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1036 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1037 if (len == (size_t) -1) \
1039 /* Illegal multibyte character. */ \
1047 /* Search for the end of the string, but don't search past \
1048 the length specified by the precision. */ \
1049 len = __wcsnlen (string, prec); \
1051 len = __wcslen (string); \
1054 if ((width -= len) < 0) \
1056 outstring (string, len); \
1062 outstring (string, len); \
1065 if (__glibc_unlikely (string_malloced)) \
1070 # define process_string_arg(fspec) \
1071 LABEL (form_character): \
1074 goto LABEL (form_wcharacter); \
1075 --width; /* Account for the character itself. */ \
1078 if (fspec == NULL) \
1079 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1081 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1086 LABEL (form_wcharacter): \
1088 /* Wide character. */ \
1089 char buf[MB_LEN_MAX]; \
1090 mbstate_t mbstate; \
1093 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1094 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1095 : args_value[fspec->data_arg].pa_wchar), \
1097 if (len == (size_t) -1) \
1099 /* Something went wrong during the conversion. Bail out. */ \
1106 outstring (buf, len); \
1112 LABEL (form_string): \
1115 int string_malloced; \
1117 /* The string argument could in fact be `char *' or `wchar_t *'. \
1118 But this should not make a difference here. */ \
1119 if (fspec == NULL) \
1120 string = (char *) va_arg (ap, const char *); \
1122 string = (char *) args_value[fspec->data_arg].pa_string; \
1124 /* Entry point for printing other strings. */ \
1125 LABEL (print_string): \
1127 string_malloced = 0; \
1128 if (string == NULL) \
1130 /* Write "(null)" if there's space. */ \
1131 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1133 string = (char *) null; \
1134 len = sizeof (null) - 1; \
1138 string = (char *) ""; \
1142 else if (!is_long && spec != L_('S')) \
1145 /* Search for the end of the string, but don't search past \
1146 the length (in bytes) specified by the precision. */ \
1147 len = __strnlen (string, prec); \
1149 len = strlen (string); \
1153 const wchar_t *s2 = (const wchar_t *) string; \
1154 mbstate_t mbstate; \
1156 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1160 /* The string `s2' might not be NUL terminated. */ \
1161 if (__libc_use_alloca (prec)) \
1162 string = (char *) alloca (prec); \
1163 else if ((string = (char *) malloc (prec)) == NULL) \
1169 string_malloced = 1; \
1170 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1174 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1175 if (len != (size_t) -1) \
1177 assert (__mbsinit (&mbstate)); \
1178 s2 = (const wchar_t *) string; \
1179 if (__libc_use_alloca (len + 1)) \
1180 string = (char *) alloca (len + 1); \
1181 else if ((string = (char *) malloc (len + 1)) == NULL) \
1187 string_malloced = 1; \
1188 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1192 if (len == (size_t) -1) \
1194 /* Illegal wide-character string. */ \
1200 if ((width -= len) < 0) \
1202 outstring (string, len); \
1208 outstring (string, len); \
1211 if (__glibc_unlikely (string_malloced)) \
1217 /* Helper function to provide temporary buffering for unbuffered streams. */
1218 static int buffered_vfprintf (FILE *stream
, const CHAR_T
*fmt
, va_list)
1219 __THROW
__attribute__ ((noinline
)) internal_function
;
1221 /* Handle positional format specifiers. */
1222 static int printf_positional (_IO_FILE
*s
,
1223 const CHAR_T
*format
, int readonly_format
,
1224 va_list ap
, va_list *ap_savep
, int done
,
1225 int nspecs_done
, const UCHAR_T
*lead_str_end
,
1226 CHAR_T
*work_buffer
, int save_errno
,
1227 const char *grouping
, THOUSANDS_SEP_T
);
1229 /* Handle unknown format specifier. */
1230 static int printf_unknown (FILE *, const struct printf_info
*,
1231 const void *const *) __THROW
;
1233 /* Group digits of number string. */
1234 static CHAR_T
*group_number (CHAR_T
*, CHAR_T
*, const char *, THOUSANDS_SEP_T
)
1235 __THROW internal_function
;
1237 /* The function itself. */
1239 vfprintf (FILE *s
, const CHAR_T
*format
, va_list ap
)
1241 /* The character used as thousands separator. */
1242 THOUSANDS_SEP_T thousands_sep
= 0;
1244 /* The string describing the size of groups of digits. */
1245 const char *grouping
;
1247 /* Place to accumulate the result. */
1250 /* Current character in format string. */
1253 /* End of leading constant string. */
1254 const UCHAR_T
*lead_str_end
;
1256 /* Points to next format specifier. */
1257 const UCHAR_T
*end_of_spec
;
1259 /* Buffer intermediate results. */
1260 CHAR_T work_buffer
[WORK_BUFFER_SIZE
];
1261 CHAR_T
*workstart
= NULL
;
1264 /* We have to save the original argument pointer. */
1267 /* Count number of specifiers we already processed. */
1270 /* For the %m format we may need the current `errno' value. */
1271 int save_errno
= errno
;
1273 /* 1 if format is in read-only memory, -1 if it is in writable memory,
1275 int readonly_format
= 0;
1277 /* Orient the stream. */
1282 /* Sanity check of arguments. */
1283 ARGCHECK (s
, format
);
1286 /* Check for correct orientation. */
1287 if (_IO_vtable_offset (s
) == 0 &&
1288 _IO_fwide (s
, sizeof (CHAR_T
) == 1 ? -1 : 1)
1289 != (sizeof (CHAR_T
) == 1 ? -1 : 1))
1290 /* The stream is already oriented otherwise. */
1294 if (UNBUFFERED_P (s
))
1295 /* Use a helper function which will allocate a local temporary buffer
1296 for the stream and then call us again. */
1297 return buffered_vfprintf (s
, format
, ap
);
1299 /* Initialize local variables. */
1301 grouping
= (const char *) -1;
1303 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1304 since on some systems `va_list' is not an integral type. */
1305 __va_copy (ap_save
, ap
);
1311 #ifdef COMPILE_WPRINTF
1312 /* Find the first format specifier. */
1313 f
= lead_str_end
= __find_specwc ((const UCHAR_T
*) format
);
1315 /* Find the first format specifier. */
1316 f
= lead_str_end
= __find_specmb ((const UCHAR_T
*) format
);
1320 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile
, s
);
1323 /* Write the literal text before the first format. */
1324 outstring ((const UCHAR_T
*) format
,
1325 lead_str_end
- (const UCHAR_T
*) format
);
1327 /* If we only have to print a simple string, return now. */
1331 /* Use the slow path in case any printf handler is registered. */
1332 if (__glibc_unlikely (__printf_function_table
!= NULL
1333 || __printf_modifier_table
!= NULL
1334 || __printf_va_arg_table
!= NULL
))
1337 /* Process whole format string. */
1343 union printf_arg
*args_value
; /* This is not used here but ... */
1344 int is_negative
; /* Flag for negative number. */
1347 unsigned long long int longlong
;
1348 unsigned long int word
;
1351 union printf_arg the_arg
;
1352 CHAR_T
*string
; /* Pointer to argument string. */
1353 int alt
= 0; /* Alternate format. */
1354 int space
= 0; /* Use space prefix if no sign is needed. */
1355 int left
= 0; /* Left-justify output. */
1356 int showsign
= 0; /* Always begin with plus or minus sign. */
1357 int group
= 0; /* Print numbers according grouping rules. */
1358 int is_long_double
= 0; /* Argument is long double/ long long int. */
1359 int is_short
= 0; /* Argument is short int. */
1360 int is_long
= 0; /* Argument is long int. */
1361 int is_char
= 0; /* Argument is promoted (unsigned) char. */
1362 int width
= 0; /* Width of output; 0 means none specified. */
1363 int prec
= -1; /* Precision of output; -1 means none specified. */
1364 /* This flag is set by the 'I' modifier and selects the use of the
1365 `outdigits' as determined by the current locale. */
1366 int use_outdigits
= 0;
1367 UCHAR_T pad
= L_(' ');/* Padding character. */
1371 workend
= work_buffer
+ WORK_BUFFER_SIZE
;
1373 /* Get current character in format string. */
1374 JUMP (*++f
, step0_jumps
);
1379 JUMP (*++f
, step0_jumps
);
1384 JUMP (*++f
, step0_jumps
);
1390 JUMP (*++f
, step0_jumps
);
1395 JUMP (*++f
, step0_jumps
);
1401 JUMP (*++f
, step0_jumps
);
1403 /* The '\'' flag. */
1407 if (grouping
== (const char *) -1)
1409 #ifdef COMPILE_WPRINTF
1410 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1411 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1413 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1416 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1417 if (*grouping
== '\0' || *grouping
== CHAR_MAX
1418 #ifdef COMPILE_WPRINTF
1419 || thousands_sep
== L
'\0'
1421 || *thousands_sep
== '\0'
1426 JUMP (*++f
, step0_jumps
);
1430 JUMP (*++f
, step0_jumps
);
1432 /* Get width from argument. */
1433 LABEL (width_asterics
):
1435 const UCHAR_T
*tmp
; /* Temporary value. */
1440 int pos
= read_int (&tmp
);
1444 __set_errno (EOVERFLOW
);
1449 if (pos
&& *tmp
== L_('$'))
1450 /* The width comes from a positional parameter. */
1453 width
= va_arg (ap
, int);
1455 /* Negative width means left justified. */
1463 if (__glibc_unlikely (width
>= INT_MAX
/ sizeof (CHAR_T
) - EXTSIZ
))
1465 __set_errno (EOVERFLOW
);
1470 if (width
>= WORK_BUFFER_SIZE
- EXTSIZ
)
1472 /* We have to use a special buffer. */
1473 size_t needed
= ((size_t) width
+ EXTSIZ
) * sizeof (CHAR_T
);
1474 if (__libc_use_alloca (needed
))
1475 workend
= (CHAR_T
*) alloca (needed
) + width
+ EXTSIZ
;
1478 workstart
= (CHAR_T
*) malloc (needed
);
1479 if (workstart
== NULL
)
1484 workend
= workstart
+ width
+ EXTSIZ
;
1488 JUMP (*f
, step1_jumps
);
1490 /* Given width in format string. */
1492 width
= read_int (&f
);
1494 if (__glibc_unlikely (width
== -1
1495 || width
>= INT_MAX
/ sizeof (CHAR_T
) - EXTSIZ
))
1497 __set_errno (EOVERFLOW
);
1502 if (width
>= WORK_BUFFER_SIZE
- EXTSIZ
)
1504 /* We have to use a special buffer. */
1505 size_t needed
= ((size_t) width
+ EXTSIZ
) * sizeof (CHAR_T
);
1506 if (__libc_use_alloca (needed
))
1507 workend
= (CHAR_T
*) alloca (needed
) + width
+ EXTSIZ
;
1510 workstart
= (CHAR_T
*) malloc (needed
);
1511 if (workstart
== NULL
)
1516 workend
= workstart
+ width
+ EXTSIZ
;
1520 /* Oh, oh. The argument comes from a positional parameter. */
1522 JUMP (*f
, step1_jumps
);
1528 const UCHAR_T
*tmp
; /* Temporary value. */
1533 int pos
= read_int (&tmp
);
1537 __set_errno (EOVERFLOW
);
1542 if (pos
&& *tmp
== L_('$'))
1543 /* The precision comes from a positional parameter. */
1546 prec
= va_arg (ap
, int);
1548 /* If the precision is negative the precision is omitted. */
1552 else if (ISDIGIT (*f
))
1554 prec
= read_int (&f
);
1556 /* The precision was specified in this case as an extremely
1557 large positive value. */
1560 __set_errno (EOVERFLOW
);
1567 if (prec
> width
&& prec
> WORK_BUFFER_SIZE
- EXTSIZ
)
1569 /* Deallocate any previously allocated buffer because it is
1571 if (__glibc_unlikely (workstart
!= NULL
))
1574 if (__glibc_unlikely (prec
>= INT_MAX
/ sizeof (CHAR_T
) - EXTSIZ
))
1576 __set_errno (EOVERFLOW
);
1580 size_t needed
= ((size_t) prec
+ EXTSIZ
) * sizeof (CHAR_T
);
1582 if (__libc_use_alloca (needed
))
1583 workend
= (CHAR_T
*) alloca (needed
) + prec
+ EXTSIZ
;
1586 workstart
= (CHAR_T
*) malloc (needed
);
1587 if (workstart
== NULL
)
1592 workend
= workstart
+ prec
+ EXTSIZ
;
1595 JUMP (*f
, step2_jumps
);
1597 /* Process 'h' modifier. There might another 'h' following. */
1600 JUMP (*++f
, step3a_jumps
);
1602 /* Process 'hh' modifier. */
1603 LABEL (mod_halfhalf
):
1606 JUMP (*++f
, step4_jumps
);
1608 /* Process 'l' modifier. There might another 'l' following. */
1611 JUMP (*++f
, step3b_jumps
);
1613 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1614 allowed to follow. */
1615 LABEL (mod_longlong
):
1618 JUMP (*++f
, step4_jumps
);
1621 is_long_double
= sizeof (size_t) > sizeof (unsigned long int);
1622 is_long
= sizeof (size_t) > sizeof (unsigned int);
1623 JUMP (*++f
, step4_jumps
);
1625 LABEL (mod_ptrdiff_t
):
1626 is_long_double
= sizeof (ptrdiff_t) > sizeof (unsigned long int);
1627 is_long
= sizeof (ptrdiff_t) > sizeof (unsigned int);
1628 JUMP (*++f
, step4_jumps
);
1630 LABEL (mod_intmax_t
):
1631 is_long_double
= sizeof (intmax_t) > sizeof (unsigned long int);
1632 is_long
= sizeof (intmax_t) > sizeof (unsigned int);
1633 JUMP (*++f
, step4_jumps
);
1635 /* Process current format. */
1638 process_arg (((struct printf_spec
*) NULL
));
1639 process_string_arg (((struct printf_spec
*) NULL
));
1641 LABEL (form_unknown
):
1642 if (spec
== L_('\0'))
1644 /* The format string ended before the specifier is complete. */
1645 __set_errno (EINVAL
);
1650 /* If we are in the fast loop force entering the complicated
1655 /* The format is correctly handled. */
1658 if (__glibc_unlikely (workstart
!= NULL
))
1662 /* Look for next format specifier. */
1663 #ifdef COMPILE_WPRINTF
1664 f
= __find_specwc ((end_of_spec
= ++f
));
1666 f
= __find_specmb ((end_of_spec
= ++f
));
1669 /* Write the following constant string. */
1670 outstring (end_of_spec
, f
- end_of_spec
);
1672 while (*f
!= L_('\0'));
1674 /* Unlock stream and return. */
1677 /* Hand off processing for positional parameters. */
1679 if (__glibc_unlikely (workstart
!= NULL
))
1684 done
= printf_positional (s
, format
, readonly_format
, ap
, &ap_save
,
1685 done
, nspecs_done
, lead_str_end
, work_buffer
,
1686 save_errno
, grouping
, thousands_sep
);
1689 if (__glibc_unlikely (workstart
!= NULL
))
1691 /* Unlock the stream. */
1692 _IO_funlockfile (s
);
1693 _IO_cleanup_region_end (0);
1699 printf_positional (_IO_FILE
*s
, const CHAR_T
*format
, int readonly_format
,
1700 va_list ap
, va_list *ap_savep
, int done
, int nspecs_done
,
1701 const UCHAR_T
*lead_str_end
,
1702 CHAR_T
*work_buffer
, int save_errno
,
1703 const char *grouping
, THOUSANDS_SEP_T thousands_sep
)
1705 /* For the argument descriptions, which may be allocated on the heap. */
1706 void *args_malloced
= NULL
;
1708 /* For positional argument handling. */
1709 struct scratch_buffer specsbuf
;
1710 scratch_buffer_init (&specsbuf
);
1711 struct printf_spec
*specs
= specsbuf
.data
;
1712 size_t specs_limit
= specsbuf
.length
/ sizeof (specs
[0]);
1714 /* Array with information about the needed arguments. This has to
1715 be dynamically extensible. */
1718 /* The number of arguments the format string requests. This will
1719 determine the size of the array needed to store the argument
1722 size_t bytes_per_arg
;
1723 union printf_arg
*args_value
;
1727 /* Positional parameters refer to arguments directly. This could
1728 also determine the maximum number of arguments. Track the
1730 size_t max_ref_arg
= 0;
1732 /* Just a counter. */
1735 CHAR_T
*workstart
= NULL
;
1737 if (grouping
== (const char *) -1)
1739 #ifdef COMPILE_WPRINTF
1740 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1741 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1743 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1746 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1747 if (*grouping
== '\0' || *grouping
== CHAR_MAX
)
1751 for (const UCHAR_T
*f
= lead_str_end
; *f
!= L_('\0');
1752 f
= specs
[nspecs
++].next_fmt
)
1754 if (nspecs
== specs_limit
)
1756 if (!scratch_buffer_grow_preserve (&specsbuf
))
1761 specs
= specsbuf
.data
;
1762 specs_limit
= specsbuf
.length
/ sizeof (specs
[0]);
1765 /* Parse the format specifier. */
1766 #ifdef COMPILE_WPRINTF
1767 nargs
+= __parse_one_specwc (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1769 nargs
+= __parse_one_specmb (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1773 /* Determine the number of arguments the format string consumes. */
1774 nargs
= MAX (nargs
, max_ref_arg
);
1775 /* Calculate total size needed to represent a single argument across
1776 all three argument-related arrays. */
1777 bytes_per_arg
= (sizeof (*args_value
) + sizeof (*args_size
)
1778 + sizeof (*args_type
));
1780 /* Check for potential integer overflow. */
1781 if (__glibc_unlikely (nargs
> INT_MAX
/ bytes_per_arg
))
1783 __set_errno (EOVERFLOW
);
1788 /* Allocate memory for all three argument arrays. */
1789 if (__libc_use_alloca (nargs
* bytes_per_arg
))
1790 args_value
= alloca (nargs
* bytes_per_arg
);
1793 args_value
= args_malloced
= malloc (nargs
* bytes_per_arg
);
1794 if (args_value
== NULL
)
1801 /* Set up the remaining two arrays to each point past the end of the
1802 prior array, since space for all three has been allocated now. */
1803 args_size
= &args_value
[nargs
].pa_int
;
1804 args_type
= &args_size
[nargs
];
1805 memset (args_type
, s
->_flags2
& _IO_FLAGS2_FORTIFY
? '\xff' : '\0',
1806 nargs
* sizeof (*args_type
));
1808 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1809 still zero after this loop, format is invalid. For now we
1810 simply use 0 as the value. */
1812 /* Fill in the types of all the arguments. */
1813 for (cnt
= 0; cnt
< nspecs
; ++cnt
)
1815 /* If the width is determined by an argument this is an int. */
1816 if (specs
[cnt
].width_arg
!= -1)
1817 args_type
[specs
[cnt
].width_arg
] = PA_INT
;
1819 /* If the precision is determined by an argument this is an int. */
1820 if (specs
[cnt
].prec_arg
!= -1)
1821 args_type
[specs
[cnt
].prec_arg
] = PA_INT
;
1823 switch (specs
[cnt
].ndata_args
)
1825 case 0: /* No arguments. */
1827 case 1: /* One argument; we already have the
1829 args_type
[specs
[cnt
].data_arg
] = specs
[cnt
].data_arg_type
;
1830 args_size
[specs
[cnt
].data_arg
] = specs
[cnt
].size
;
1833 /* We have more than one argument for this format spec.
1834 We must call the arginfo function again to determine
1836 (void) (*__printf_arginfo_table
[specs
[cnt
].info
.spec
])
1838 specs
[cnt
].ndata_args
, &args_type
[specs
[cnt
].data_arg
],
1839 &args_size
[specs
[cnt
].data_arg
]);
1844 /* Now we know all the types and the order. Fill in the argument
1846 for (cnt
= 0; cnt
< nargs
; ++cnt
)
1847 switch (args_type
[cnt
])
1849 #define T(tag, mem, type) \
1851 args_value[cnt].mem = va_arg (*ap_savep, type); \
1854 T (PA_WCHAR
, pa_wchar
, wint_t);
1855 case PA_CHAR
: /* Promoted. */
1856 case PA_INT
|PA_FLAG_SHORT
: /* Promoted. */
1857 #if LONG_MAX == INT_MAX
1858 case PA_INT
|PA_FLAG_LONG
:
1860 T (PA_INT
, pa_int
, int);
1861 #if LONG_MAX == LONG_LONG_MAX
1862 case PA_INT
|PA_FLAG_LONG
:
1864 T (PA_INT
|PA_FLAG_LONG_LONG
, pa_long_long_int
, long long int);
1865 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1868 case PA_FLOAT
: /* Promoted. */
1869 T (PA_DOUBLE
, pa_double
, double);
1870 case PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
:
1873 args_value
[cnt
].pa_double
= va_arg (*ap_savep
, double);
1874 args_type
[cnt
] &= ~PA_FLAG_LONG_DOUBLE
;
1877 args_value
[cnt
].pa_long_double
= va_arg (*ap_savep
, long double);
1879 case PA_STRING
: /* All pointers are the same */
1880 case PA_WSTRING
: /* All pointers are the same */
1881 T (PA_POINTER
, pa_pointer
, void *);
1884 if ((args_type
[cnt
] & PA_FLAG_PTR
) != 0)
1885 args_value
[cnt
].pa_pointer
= va_arg (*ap_savep
, void *);
1886 else if (__glibc_unlikely (__printf_va_arg_table
!= NULL
)
1887 && __printf_va_arg_table
[args_type
[cnt
] - PA_LAST
] != NULL
)
1889 args_value
[cnt
].pa_user
= alloca (args_size
[cnt
]);
1890 (*__printf_va_arg_table
[args_type
[cnt
] - PA_LAST
])
1891 (args_value
[cnt
].pa_user
, ap_savep
);
1894 args_value
[cnt
].pa_long_double
= 0.0;
1897 /* Error case. Not all parameters appear in N$ format
1898 strings. We have no way to determine their type. */
1899 assert (s
->_flags2
& _IO_FLAGS2_FORTIFY
);
1900 __libc_fatal ("*** invalid %N$ use detected ***\n");
1903 /* Now walk through all format specifiers and process them. */
1904 for (; (size_t) nspecs_done
< nspecs
; ++nspecs_done
)
1911 unsigned long long int longlong
;
1912 unsigned long int word
;
1915 union printf_arg the_arg
;
1916 CHAR_T
*string
; /* Pointer to argument string. */
1918 /* Fill variables from values in struct. */
1919 int alt
= specs
[nspecs_done
].info
.alt
;
1920 int space
= specs
[nspecs_done
].info
.space
;
1921 int left
= specs
[nspecs_done
].info
.left
;
1922 int showsign
= specs
[nspecs_done
].info
.showsign
;
1923 int group
= specs
[nspecs_done
].info
.group
;
1924 int is_long_double
= specs
[nspecs_done
].info
.is_long_double
;
1925 int is_short
= specs
[nspecs_done
].info
.is_short
;
1926 int is_char
= specs
[nspecs_done
].info
.is_char
;
1927 int is_long
= specs
[nspecs_done
].info
.is_long
;
1928 int width
= specs
[nspecs_done
].info
.width
;
1929 int prec
= specs
[nspecs_done
].info
.prec
;
1930 int use_outdigits
= specs
[nspecs_done
].info
.i18n
;
1931 char pad
= specs
[nspecs_done
].info
.pad
;
1932 CHAR_T spec
= specs
[nspecs_done
].info
.spec
;
1935 CHAR_T
*workend
= work_buffer
+ WORK_BUFFER_SIZE
;
1937 /* Fill in last information. */
1938 if (specs
[nspecs_done
].width_arg
!= -1)
1940 /* Extract the field width from an argument. */
1941 specs
[nspecs_done
].info
.width
=
1942 args_value
[specs
[nspecs_done
].width_arg
].pa_int
;
1944 if (specs
[nspecs_done
].info
.width
< 0)
1945 /* If the width value is negative left justification is
1946 selected and the value is taken as being positive. */
1948 specs
[nspecs_done
].info
.width
*= -1;
1949 left
= specs
[nspecs_done
].info
.left
= 1;
1951 width
= specs
[nspecs_done
].info
.width
;
1954 if (specs
[nspecs_done
].prec_arg
!= -1)
1956 /* Extract the precision from an argument. */
1957 specs
[nspecs_done
].info
.prec
=
1958 args_value
[specs
[nspecs_done
].prec_arg
].pa_int
;
1960 if (specs
[nspecs_done
].info
.prec
< 0)
1961 /* If the precision is negative the precision is
1963 specs
[nspecs_done
].info
.prec
= -1;
1965 prec
= specs
[nspecs_done
].info
.prec
;
1968 /* Maybe the buffer is too small. */
1969 if (MAX (prec
, width
) + EXTSIZ
> WORK_BUFFER_SIZE
)
1971 if (__libc_use_alloca ((MAX (prec
, width
) + EXTSIZ
)
1973 workend
= ((CHAR_T
*) alloca ((MAX (prec
, width
) + EXTSIZ
)
1975 + (MAX (prec
, width
) + EXTSIZ
));
1978 workstart
= (CHAR_T
*) malloc ((MAX (prec
, width
) + EXTSIZ
)
1980 if (workstart
== NULL
)
1985 workend
= workstart
+ (MAX (prec
, width
) + EXTSIZ
);
1989 /* Process format specifiers. */
1992 extern printf_function
**__printf_function_table
;
1995 if (spec
<= UCHAR_MAX
1996 && __printf_function_table
!= NULL
1997 && __printf_function_table
[(size_t) spec
] != NULL
)
1999 const void **ptr
= alloca (specs
[nspecs_done
].ndata_args
2000 * sizeof (const void *));
2002 /* Fill in an array of pointers to the argument values. */
2003 for (unsigned int i
= 0; i
< specs
[nspecs_done
].ndata_args
;
2005 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
2007 /* Call the function. */
2008 function_done
= __printf_function_table
[(size_t) spec
]
2009 (s
, &specs
[nspecs_done
].info
, ptr
);
2011 if (function_done
!= -2)
2013 /* If an error occurred we don't have information
2014 about # of chars. */
2015 if (function_done
< 0)
2017 /* Function has set errno. */
2022 done_add (function_done
);
2027 JUMP (spec
, step4_jumps
);
2029 process_arg ((&specs
[nspecs_done
]));
2030 process_string_arg ((&specs
[nspecs_done
]));
2032 LABEL (form_unknown
):
2037 ptr
= alloca (specs
[nspecs_done
].ndata_args
2038 * sizeof (const void *));
2040 /* Fill in an array of pointers to the argument values. */
2041 for (i
= 0; i
< specs
[nspecs_done
].ndata_args
; ++i
)
2042 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
2044 /* Call the function. */
2045 function_done
= printf_unknown (s
, &specs
[nspecs_done
].info
,
2048 /* If an error occurred we don't have information about #
2050 if (function_done
< 0)
2052 /* Function has set errno. */
2057 done_add (function_done
);
2062 if (__glibc_unlikely (workstart
!= NULL
))
2066 /* Write the following constant string. */
2067 outstring (specs
[nspecs_done
].end_of_fmt
,
2068 specs
[nspecs_done
].next_fmt
2069 - specs
[nspecs_done
].end_of_fmt
);
2072 if (__glibc_unlikely (args_malloced
!= NULL
))
2073 free (args_malloced
);
2074 if (__glibc_unlikely (workstart
!= NULL
))
2076 scratch_buffer_free (&specsbuf
);
2080 /* Handle an unknown format specifier. This prints out a canonicalized
2081 representation of the format spec itself. */
2083 printf_unknown (FILE *s
, const struct printf_info
*info
,
2084 const void *const *args
)
2088 CHAR_T work_buffer
[MAX (sizeof (info
->width
), sizeof (info
->prec
)) * 3];
2089 CHAR_T
*const workend
2090 = &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
2101 else if (info
->space
)
2105 if (info
->pad
== L_('0'))
2110 if (info
->width
!= 0)
2112 w
= _itoa_word (info
->width
, workend
, 10, 0);
2117 if (info
->prec
!= -1)
2120 w
= _itoa_word (info
->prec
, workend
, 10, 0);
2125 if (info
->spec
!= L_('\0'))
2126 outchar (info
->spec
);
2132 /* Group the digits according to the grouping rules of the current locale.
2133 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
2136 group_number (CHAR_T
*w
, CHAR_T
*rear_ptr
, const char *grouping
,
2137 THOUSANDS_SEP_T thousands_sep
)
2141 #ifndef COMPILE_WPRINTF
2142 int tlen
= strlen (thousands_sep
);
2145 /* We treat all negative values like CHAR_MAX. */
2147 if (*grouping
== CHAR_MAX
|| *grouping
<= 0)
2148 /* No grouping should be done. */
2153 /* Copy existing string so that nothing gets overwritten. */
2154 src
= (CHAR_T
*) alloca ((rear_ptr
- w
) * sizeof (CHAR_T
));
2155 s
= (CHAR_T
*) __mempcpy (src
, w
,
2156 (rear_ptr
- w
) * sizeof (CHAR_T
));
2159 /* Process all characters in the string. */
2164 if (--len
== 0 && s
> src
)
2166 /* A new group begins. */
2167 #ifdef COMPILE_WPRINTF
2168 *--w
= thousands_sep
;
2172 *--w
= thousands_sep
[--cnt
];
2176 if (*grouping
== CHAR_MAX
2182 /* No further grouping to be done.
2183 Copy the rest of the number. */
2189 else if (*grouping
!= '\0')
2190 /* The previous grouping repeats ad infinitum. */
2199 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2202 struct _IO_FILE_plus _f
;
2203 #ifdef COMPILE_WPRINTF
2204 struct _IO_wide_data _wide_data
;
2206 _IO_FILE
*_put_stream
;
2207 #ifdef _IO_MTSAFE_IO
2213 _IO_helper_overflow (_IO_FILE
*s
, int c
)
2215 _IO_FILE
*target
= ((struct helper_file
*) s
)->_put_stream
;
2216 #ifdef COMPILE_WPRINTF
2217 int used
= s
->_wide_data
->_IO_write_ptr
- s
->_wide_data
->_IO_write_base
;
2220 _IO_size_t written
= _IO_sputn (target
, s
->_wide_data
->_IO_write_base
,
2222 if (written
== 0 || written
== WEOF
)
2224 __wmemmove (s
->_wide_data
->_IO_write_base
,
2225 s
->_wide_data
->_IO_write_base
+ written
,
2227 s
->_wide_data
->_IO_write_ptr
-= written
;
2230 int used
= s
->_IO_write_ptr
- s
->_IO_write_base
;
2233 _IO_size_t written
= _IO_sputn (target
, s
->_IO_write_base
, used
);
2234 if (written
== 0 || written
== EOF
)
2236 memmove (s
->_IO_write_base
, s
->_IO_write_base
+ written
,
2238 s
->_IO_write_ptr
-= written
;
2244 #ifdef COMPILE_WPRINTF
2245 static const struct _IO_jump_t _IO_helper_jumps libio_vtable
=
2248 JUMP_INIT (finish
, _IO_wdefault_finish
),
2249 JUMP_INIT (overflow
, _IO_helper_overflow
),
2250 JUMP_INIT (underflow
, _IO_default_underflow
),
2251 JUMP_INIT (uflow
, _IO_default_uflow
),
2252 JUMP_INIT (pbackfail
, (_IO_pbackfail_t
) _IO_wdefault_pbackfail
),
2253 JUMP_INIT (xsputn
, _IO_wdefault_xsputn
),
2254 JUMP_INIT (xsgetn
, _IO_wdefault_xsgetn
),
2255 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2256 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2257 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2258 JUMP_INIT (sync
, _IO_default_sync
),
2259 JUMP_INIT (doallocate
, _IO_wdefault_doallocate
),
2260 JUMP_INIT (read
, _IO_default_read
),
2261 JUMP_INIT (write
, _IO_default_write
),
2262 JUMP_INIT (seek
, _IO_default_seek
),
2263 JUMP_INIT (close
, _IO_default_close
),
2264 JUMP_INIT (stat
, _IO_default_stat
)
2267 static const struct _IO_jump_t _IO_helper_jumps libio_vtable
=
2270 JUMP_INIT (finish
, _IO_default_finish
),
2271 JUMP_INIT (overflow
, _IO_helper_overflow
),
2272 JUMP_INIT (underflow
, _IO_default_underflow
),
2273 JUMP_INIT (uflow
, _IO_default_uflow
),
2274 JUMP_INIT (pbackfail
, _IO_default_pbackfail
),
2275 JUMP_INIT (xsputn
, _IO_default_xsputn
),
2276 JUMP_INIT (xsgetn
, _IO_default_xsgetn
),
2277 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2278 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2279 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2280 JUMP_INIT (sync
, _IO_default_sync
),
2281 JUMP_INIT (doallocate
, _IO_default_doallocate
),
2282 JUMP_INIT (read
, _IO_default_read
),
2283 JUMP_INIT (write
, _IO_default_write
),
2284 JUMP_INIT (seek
, _IO_default_seek
),
2285 JUMP_INIT (close
, _IO_default_close
),
2286 JUMP_INIT (stat
, _IO_default_stat
)
2292 buffered_vfprintf (_IO_FILE
*s
, const CHAR_T
*format
,
2295 CHAR_T buf
[_IO_BUFSIZ
];
2296 struct helper_file helper
;
2297 _IO_FILE
*hp
= (_IO_FILE
*) &helper
._f
;
2298 int result
, to_flush
;
2300 /* Orient the stream. */
2305 /* Initialize helper. */
2306 helper
._put_stream
= s
;
2307 #ifdef COMPILE_WPRINTF
2308 hp
->_wide_data
= &helper
._wide_data
;
2309 _IO_wsetp (hp
, buf
, buf
+ sizeof buf
/ sizeof (CHAR_T
));
2312 _IO_setp (hp
, buf
, buf
+ sizeof buf
);
2315 hp
->_IO_file_flags
= _IO_MAGIC
|_IO_NO_READS
|_IO_USER_LOCK
;
2316 #if _IO_JUMPS_OFFSET
2317 hp
->_vtable_offset
= 0;
2319 #ifdef _IO_MTSAFE_IO
2322 hp
->_flags2
= s
->_flags2
;
2323 _IO_JUMPS (&helper
._f
) = (struct _IO_jump_t
*) &_IO_helper_jumps
;
2325 /* Now print to helper instead. */
2326 #ifndef COMPILE_WPRINTF
2327 result
= _IO_vfprintf (hp
, format
, args
);
2329 result
= vfprintf (hp
, format
, args
);
2333 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile
, s
);
2336 /* Now flush anything from the helper to the S. */
2337 #ifdef COMPILE_WPRINTF
2338 if ((to_flush
= (hp
->_wide_data
->_IO_write_ptr
2339 - hp
->_wide_data
->_IO_write_base
)) > 0)
2341 if ((int) _IO_sputn (s
, hp
->_wide_data
->_IO_write_base
, to_flush
)
2346 if ((to_flush
= hp
->_IO_write_ptr
- hp
->_IO_write_base
) > 0)
2348 if ((int) _IO_sputn (s
, hp
->_IO_write_base
, to_flush
) != to_flush
)
2353 /* Unlock the stream. */
2354 _IO_funlockfile (s
);
2355 __libc_cleanup_region_end (0);
2361 #ifdef COMPILE_WPRINTF
2362 strong_alias (_IO_vfwprintf
, __vfwprintf
);
2363 ldbl_weak_alias (_IO_vfwprintf
, vfwprintf
);
2365 ldbl_strong_alias (_IO_vfprintf_internal
, vfprintf
);
2366 ldbl_hidden_def (_IO_vfprintf_internal
, vfprintf
)
2367 ldbl_strong_alias (_IO_vfprintf_internal
, _IO_vfprintf
);
2368 ldbl_hidden_def (_IO_vfprintf_internal
, _IO_vfprintf
)