1 /* Copyright (C) 1991-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
27 #include <bits/libc-lock.h>
28 #include <sys/param.h>
30 #include <locale/localeinfo.h>
33 /* This code is shared between the standard stdio implementation found
34 in GNU C library and the libio implementation originally found in
37 Beside this it is also shared between the normal and wide character
38 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
44 #define va_list _IO_va_list
46 #define BUFSIZ _IO_BUFSIZ
47 #define ARGCHECK(S, Format) \
50 /* Check file argument for consistence. */ \
52 if (S->_flags & _IO_NO_WRITES) \
54 S->_flags |= _IO_ERR_SEEN; \
55 __set_errno (EBADF); \
64 #define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
66 #define done_add(val) \
68 unsigned int _val = val; \
69 assert ((unsigned int) done < (unsigned int) INT_MAX); \
70 if (__glibc_unlikely (INT_MAX - done < _val)) \
73 __set_errno (EOVERFLOW); \
79 #ifndef COMPILE_WPRINTF
80 # define vfprintf _IO_vfprintf_internal
82 # define UCHAR_T unsigned char
84 typedef const char *THOUSANDS_SEP_T
;
86 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
87 # define STR_LEN(Str) strlen (Str)
89 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
90 # define PAD(Padchar) \
94 _IO_ssize_t written = _IO_padn (s, (Padchar), width); \
95 if (__glibc_unlikely (written != width)) \
100 done_add (written); \
103 # define PUTC(C, F) _IO_putc_unlocked (C, F)
104 # define ORIENT if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
107 # define vfprintf _IO_vfwprintf
108 # define CHAR_T wchar_t
109 /* This is a hack!!! There should be a type uwchar_t. */
110 # define UCHAR_T unsigned int /* uwchar_t */
111 # define INT_T wint_t
112 typedef wchar_t THOUSANDS_SEP_T
;
113 # define L_(Str) L##Str
114 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
115 # define STR_LEN(Str) __wcslen (Str)
119 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
120 # define PAD(Padchar) \
124 _IO_ssize_t written = _IO_wpadn (s, (Padchar), width); \
125 if (__glibc_unlikely (written != width)) \
130 done_add (written); \
133 # define PUTC(C, F) _IO_putwc_unlocked (C, F)
134 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
137 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
138 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
143 #include "_i18n_number.h"
145 /* Include the shared code for parsing the format string. */
146 #include "printf-parse.h"
149 #define outchar(Ch) \
152 const INT_T outc = (Ch); \
153 if (PUTC (outc, s) == EOF || done == INT_MAX) \
162 #define outstring(String, Len) \
165 assert ((size_t) done <= (size_t) INT_MAX); \
166 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
171 if (__glibc_unlikely (INT_MAX - done < (Len))) \
174 __set_errno (EOVERFLOW); \
181 /* For handling long_double and longlong we use the same flag. If
182 `long' and `long long' are effectively the same type define it to
184 #if LONG_MAX == LONG_LONG_MAX
185 # define is_longlong 0
187 # define is_longlong is_long_double
190 /* If `long' and `int' is effectively the same type we don't have to
191 handle `long separately. */
192 #if INT_MAX == LONG_MAX
193 # define is_long_num 0
195 # define is_long_num is_long
199 /* Global constants. */
200 static const CHAR_T null
[] = L_("(null)");
202 /* Size of the work_buffer variable (in characters, not bytes. */
203 enum { WORK_BUFFER_SIZE
= 1000 };
205 /* This table maps a character into a number representing a class. In
206 each step there is a destination label for each class. */
207 static const uint8_t jump_table
[] =
209 /* ' ' */ 1, 0, 0, /* '#' */ 4,
210 0, /* '%' */ 14, 0, /* '\''*/ 6,
211 0, 0, /* '*' */ 7, /* '+' */ 2,
212 0, /* '-' */ 3, /* '.' */ 9, 0,
213 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
214 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
215 /* '8' */ 8, /* '9' */ 8, 0, 0,
217 0, /* 'A' */ 26, 0, /* 'C' */ 25,
218 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
219 0, /* 'I' */ 29, 0, 0,
220 /* 'L' */ 12, 0, 0, 0,
221 0, 0, 0, /* 'S' */ 21,
223 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
225 0, /* 'a' */ 26, 0, /* 'c' */ 20,
226 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
227 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
228 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
229 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
230 /* 't' */ 27, /* 'u' */ 16, 0, 0,
231 /* 'x' */ 18, 0, /* 'z' */ 13
234 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
235 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
236 #define LABEL(Name) do_##Name
238 /* 'int' is enough and it saves some space on 64 bit systems. */
239 # define JUMP_TABLE_TYPE const int
240 # define JUMP_TABLE_BASE_LABEL do_form_unknown
241 # define REF(Name) &&do_##Name - &&JUMP_TABLE_BASE_LABEL
242 # define JUMP(ChExpr, table) \
248 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
249 : table[CHAR_CLASS (spec)]; \
250 ptr = &&JUMP_TABLE_BASE_LABEL + offset; \
255 # define JUMP_TABLE_TYPE const void *const
256 # define REF(Name) &&do_##Name
257 # define JUMP(ChExpr, table) \
262 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
263 : table[CHAR_CLASS (spec)]; \
269 #define STEP0_3_TABLE \
270 /* Step 0: at the beginning. */ \
271 static JUMP_TABLE_TYPE step0_jumps[30] = \
273 REF (form_unknown), \
274 REF (flag_space), /* for ' ' */ \
275 REF (flag_plus), /* for '+' */ \
276 REF (flag_minus), /* for '-' */ \
277 REF (flag_hash), /* for '<hash>' */ \
278 REF (flag_zero), /* for '0' */ \
279 REF (flag_quote), /* for '\'' */ \
280 REF (width_asterics), /* for '*' */ \
281 REF (width), /* for '1'...'9' */ \
282 REF (precision), /* for '.' */ \
283 REF (mod_half), /* for 'h' */ \
284 REF (mod_long), /* for 'l' */ \
285 REF (mod_longlong), /* for 'L', 'q' */ \
286 REF (mod_size_t), /* for 'z', 'Z' */ \
287 REF (form_percent), /* for '%' */ \
288 REF (form_integer), /* for 'd', 'i' */ \
289 REF (form_unsigned), /* for 'u' */ \
290 REF (form_octal), /* for 'o' */ \
291 REF (form_hexa), /* for 'X', 'x' */ \
292 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
293 REF (form_character), /* for 'c' */ \
294 REF (form_string), /* for 's', 'S' */ \
295 REF (form_pointer), /* for 'p' */ \
296 REF (form_number), /* for 'n' */ \
297 REF (form_strerror), /* for 'm' */ \
298 REF (form_wcharacter), /* for 'C' */ \
299 REF (form_floathex), /* for 'A', 'a' */ \
300 REF (mod_ptrdiff_t), /* for 't' */ \
301 REF (mod_intmax_t), /* for 'j' */ \
302 REF (flag_i18n), /* for 'I' */ \
304 /* Step 1: after processing width. */ \
305 static JUMP_TABLE_TYPE step1_jumps[30] = \
307 REF (form_unknown), \
308 REF (form_unknown), /* for ' ' */ \
309 REF (form_unknown), /* for '+' */ \
310 REF (form_unknown), /* for '-' */ \
311 REF (form_unknown), /* for '<hash>' */ \
312 REF (form_unknown), /* for '0' */ \
313 REF (form_unknown), /* for '\'' */ \
314 REF (form_unknown), /* for '*' */ \
315 REF (form_unknown), /* for '1'...'9' */ \
316 REF (precision), /* for '.' */ \
317 REF (mod_half), /* for 'h' */ \
318 REF (mod_long), /* for 'l' */ \
319 REF (mod_longlong), /* for 'L', 'q' */ \
320 REF (mod_size_t), /* for 'z', 'Z' */ \
321 REF (form_percent), /* for '%' */ \
322 REF (form_integer), /* for 'd', 'i' */ \
323 REF (form_unsigned), /* for 'u' */ \
324 REF (form_octal), /* for 'o' */ \
325 REF (form_hexa), /* for 'X', 'x' */ \
326 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
327 REF (form_character), /* for 'c' */ \
328 REF (form_string), /* for 's', 'S' */ \
329 REF (form_pointer), /* for 'p' */ \
330 REF (form_number), /* for 'n' */ \
331 REF (form_strerror), /* for 'm' */ \
332 REF (form_wcharacter), /* for 'C' */ \
333 REF (form_floathex), /* for 'A', 'a' */ \
334 REF (mod_ptrdiff_t), /* for 't' */ \
335 REF (mod_intmax_t), /* for 'j' */ \
336 REF (form_unknown) /* for 'I' */ \
338 /* Step 2: after processing precision. */ \
339 static JUMP_TABLE_TYPE step2_jumps[30] = \
341 REF (form_unknown), \
342 REF (form_unknown), /* for ' ' */ \
343 REF (form_unknown), /* for '+' */ \
344 REF (form_unknown), /* for '-' */ \
345 REF (form_unknown), /* for '<hash>' */ \
346 REF (form_unknown), /* for '0' */ \
347 REF (form_unknown), /* for '\'' */ \
348 REF (form_unknown), /* for '*' */ \
349 REF (form_unknown), /* for '1'...'9' */ \
350 REF (form_unknown), /* for '.' */ \
351 REF (mod_half), /* for 'h' */ \
352 REF (mod_long), /* for 'l' */ \
353 REF (mod_longlong), /* for 'L', 'q' */ \
354 REF (mod_size_t), /* for 'z', 'Z' */ \
355 REF (form_percent), /* for '%' */ \
356 REF (form_integer), /* for 'd', 'i' */ \
357 REF (form_unsigned), /* for 'u' */ \
358 REF (form_octal), /* for 'o' */ \
359 REF (form_hexa), /* for 'X', 'x' */ \
360 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
361 REF (form_character), /* for 'c' */ \
362 REF (form_string), /* for 's', 'S' */ \
363 REF (form_pointer), /* for 'p' */ \
364 REF (form_number), /* for 'n' */ \
365 REF (form_strerror), /* for 'm' */ \
366 REF (form_wcharacter), /* for 'C' */ \
367 REF (form_floathex), /* for 'A', 'a' */ \
368 REF (mod_ptrdiff_t), /* for 't' */ \
369 REF (mod_intmax_t), /* for 'j' */ \
370 REF (form_unknown) /* for 'I' */ \
372 /* Step 3a: after processing first 'h' modifier. */ \
373 static JUMP_TABLE_TYPE step3a_jumps[30] = \
375 REF (form_unknown), \
376 REF (form_unknown), /* for ' ' */ \
377 REF (form_unknown), /* for '+' */ \
378 REF (form_unknown), /* for '-' */ \
379 REF (form_unknown), /* for '<hash>' */ \
380 REF (form_unknown), /* for '0' */ \
381 REF (form_unknown), /* for '\'' */ \
382 REF (form_unknown), /* for '*' */ \
383 REF (form_unknown), /* for '1'...'9' */ \
384 REF (form_unknown), /* for '.' */ \
385 REF (mod_halfhalf), /* for 'h' */ \
386 REF (form_unknown), /* for 'l' */ \
387 REF (form_unknown), /* for 'L', 'q' */ \
388 REF (form_unknown), /* for 'z', 'Z' */ \
389 REF (form_percent), /* for '%' */ \
390 REF (form_integer), /* for 'd', 'i' */ \
391 REF (form_unsigned), /* for 'u' */ \
392 REF (form_octal), /* for 'o' */ \
393 REF (form_hexa), /* for 'X', 'x' */ \
394 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
395 REF (form_unknown), /* for 'c' */ \
396 REF (form_unknown), /* for 's', 'S' */ \
397 REF (form_unknown), /* for 'p' */ \
398 REF (form_number), /* for 'n' */ \
399 REF (form_unknown), /* for 'm' */ \
400 REF (form_unknown), /* for 'C' */ \
401 REF (form_unknown), /* for 'A', 'a' */ \
402 REF (form_unknown), /* for 't' */ \
403 REF (form_unknown), /* for 'j' */ \
404 REF (form_unknown) /* for 'I' */ \
406 /* Step 3b: after processing first 'l' modifier. */ \
407 static JUMP_TABLE_TYPE step3b_jumps[30] = \
409 REF (form_unknown), \
410 REF (form_unknown), /* for ' ' */ \
411 REF (form_unknown), /* for '+' */ \
412 REF (form_unknown), /* for '-' */ \
413 REF (form_unknown), /* for '<hash>' */ \
414 REF (form_unknown), /* for '0' */ \
415 REF (form_unknown), /* for '\'' */ \
416 REF (form_unknown), /* for '*' */ \
417 REF (form_unknown), /* for '1'...'9' */ \
418 REF (form_unknown), /* for '.' */ \
419 REF (form_unknown), /* for 'h' */ \
420 REF (mod_longlong), /* for 'l' */ \
421 REF (form_unknown), /* for 'L', 'q' */ \
422 REF (form_unknown), /* for 'z', 'Z' */ \
423 REF (form_percent), /* for '%' */ \
424 REF (form_integer), /* for 'd', 'i' */ \
425 REF (form_unsigned), /* for 'u' */ \
426 REF (form_octal), /* for 'o' */ \
427 REF (form_hexa), /* for 'X', 'x' */ \
428 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
429 REF (form_character), /* for 'c' */ \
430 REF (form_string), /* for 's', 'S' */ \
431 REF (form_pointer), /* for 'p' */ \
432 REF (form_number), /* for 'n' */ \
433 REF (form_strerror), /* for 'm' */ \
434 REF (form_wcharacter), /* for 'C' */ \
435 REF (form_floathex), /* for 'A', 'a' */ \
436 REF (form_unknown), /* for 't' */ \
437 REF (form_unknown), /* for 'j' */ \
438 REF (form_unknown) /* for 'I' */ \
441 #define STEP4_TABLE \
442 /* Step 4: processing format specifier. */ \
443 static JUMP_TABLE_TYPE step4_jumps[30] = \
445 REF (form_unknown), \
446 REF (form_unknown), /* for ' ' */ \
447 REF (form_unknown), /* for '+' */ \
448 REF (form_unknown), /* for '-' */ \
449 REF (form_unknown), /* for '<hash>' */ \
450 REF (form_unknown), /* for '0' */ \
451 REF (form_unknown), /* for '\'' */ \
452 REF (form_unknown), /* for '*' */ \
453 REF (form_unknown), /* for '1'...'9' */ \
454 REF (form_unknown), /* for '.' */ \
455 REF (form_unknown), /* for 'h' */ \
456 REF (form_unknown), /* for 'l' */ \
457 REF (form_unknown), /* for 'L', 'q' */ \
458 REF (form_unknown), /* for 'z', 'Z' */ \
459 REF (form_percent), /* for '%' */ \
460 REF (form_integer), /* for 'd', 'i' */ \
461 REF (form_unsigned), /* for 'u' */ \
462 REF (form_octal), /* for 'o' */ \
463 REF (form_hexa), /* for 'X', 'x' */ \
464 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
465 REF (form_character), /* for 'c' */ \
466 REF (form_string), /* for 's', 'S' */ \
467 REF (form_pointer), /* for 'p' */ \
468 REF (form_number), /* for 'n' */ \
469 REF (form_strerror), /* for 'm' */ \
470 REF (form_wcharacter), /* for 'C' */ \
471 REF (form_floathex), /* for 'A', 'a' */ \
472 REF (form_unknown), /* for 't' */ \
473 REF (form_unknown), /* for 'j' */ \
474 REF (form_unknown) /* for 'I' */ \
478 #define process_arg(fspec) \
479 /* Start real work. We know about all flags and modifiers and \
480 now process the wanted format specifier. */ \
481 LABEL (form_percent): \
482 /* Write a literal "%". */ \
486 LABEL (form_integer): \
487 /* Signed decimal integer. */ \
492 long long int signed_number; \
495 signed_number = va_arg (ap, long long int); \
497 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
499 is_negative = signed_number < 0; \
500 number.longlong = is_negative ? (- signed_number) : signed_number; \
502 goto LABEL (longlong_number); \
506 long int signed_number; \
511 signed_number = va_arg (ap, long int); \
513 signed_number = (signed char) va_arg (ap, unsigned int); \
514 else if (!is_short) \
515 signed_number = va_arg (ap, int); \
517 signed_number = (short int) va_arg (ap, unsigned int); \
521 signed_number = args_value[fspec->data_arg].pa_long_int; \
523 signed_number = (signed char) \
524 args_value[fspec->data_arg].pa_u_int; \
525 else if (!is_short) \
526 signed_number = args_value[fspec->data_arg].pa_int; \
528 signed_number = (short int) \
529 args_value[fspec->data_arg].pa_u_int; \
531 is_negative = signed_number < 0; \
532 number.word = is_negative ? (- signed_number) : signed_number; \
534 goto LABEL (number); \
538 LABEL (form_unsigned): \
539 /* Unsigned decimal integer. */ \
541 goto LABEL (unsigned_number); \
544 LABEL (form_octal): \
545 /* Unsigned octal integer. */ \
547 goto LABEL (unsigned_number); \
551 /* Unsigned hexadecimal integer. */ \
554 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
556 /* ISO specifies the `+' and ` ' flags only for signed \
565 number.longlong = va_arg (ap, unsigned long long int); \
567 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
569 LABEL (longlong_number): \
571 /* Supply a default precision if none was given. */ \
574 /* We have to take care for the '0' flag. If a precision \
575 is given it must be ignored. */ \
578 /* If the precision is 0 and the number is 0 nothing has to \
579 be written for the number, except for the 'o' format in \
581 if (prec == 0 && number.longlong == 0) \
584 if (base == 8 && alt) \
585 *--string = L_('0'); \
589 /* Put the number in WORK. */ \
590 string = _itoa (number.longlong, workend, base, \
592 if (group && grouping) \
593 string = group_number (string, workend, grouping, \
596 if (use_outdigits && base == 10) \
597 string = _i18n_number_rewrite (string, workend, workend); \
599 /* Simplify further test for num != 0. */ \
600 number.word = number.longlong != 0; \
607 number.word = va_arg (ap, unsigned long int); \
609 number.word = (unsigned char) va_arg (ap, unsigned int); \
610 else if (!is_short) \
611 number.word = va_arg (ap, unsigned int); \
613 number.word = (unsigned short int) va_arg (ap, unsigned int); \
617 number.word = args_value[fspec->data_arg].pa_u_long_int; \
619 number.word = (unsigned char) \
620 args_value[fspec->data_arg].pa_u_int; \
621 else if (!is_short) \
622 number.word = args_value[fspec->data_arg].pa_u_int; \
624 number.word = (unsigned short int) \
625 args_value[fspec->data_arg].pa_u_int; \
629 /* Supply a default precision if none was given. */ \
632 /* We have to take care for the '0' flag. If a precision \
633 is given it must be ignored. */ \
636 /* If the precision is 0 and the number is 0 nothing has to \
637 be written for the number, except for the 'o' format in \
639 if (prec == 0 && number.word == 0) \
642 if (base == 8 && alt) \
643 *--string = L_('0'); \
647 /* Put the number in WORK. */ \
648 string = _itoa_word (number.word, workend, base, \
650 if (group && grouping) \
651 string = group_number (string, workend, grouping, \
654 if (use_outdigits && base == 10) \
655 string = _i18n_number_rewrite (string, workend, workend); \
659 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
660 /* Add octal marker. */ \
661 *--string = L_('0'); \
663 prec = MAX (0, prec - (workend - string)); \
667 width -= workend - string + prec; \
669 if (number.word != 0 && alt && base == 16) \
670 /* Account for 0X hex marker. */ \
673 if (is_negative || showsign || space) \
676 if (pad == L_(' ')) \
689 if (number.word != 0 && alt && base == 16) \
698 outstring (string, workend - string); \
720 if (number.word != 0 && alt && base == 16) \
727 width -= workend - string + prec; \
737 outstring (string, workend - string); \
743 LABEL (form_float): \
745 /* Floating-point number. This is handled by printf_fp.c. */ \
752 is_long_double = 0; \
754 struct printf_info info = { .prec = prec, \
757 .is_long_double = is_long_double, \
758 .is_short = is_short, \
759 .is_long = is_long, \
763 .showsign = showsign, \
767 .i18n = use_outdigits, \
768 .wide = sizeof (CHAR_T) != 1 }; \
770 if (is_long_double) \
771 the_arg.pa_long_double = va_arg (ap, long double); \
773 the_arg.pa_double = va_arg (ap, double); \
774 ptr = (const void *) &the_arg; \
776 function_done = __printf_fp (s, &info, &ptr); \
780 ptr = (const void *) &args_value[fspec->data_arg]; \
783 fspec->data_arg_type = PA_DOUBLE; \
784 fspec->info.is_long_double = 0; \
787 function_done = __printf_fp (s, &fspec->info, &ptr); \
790 if (function_done < 0) \
792 /* Error in print handler; up to handler to set errno. */ \
797 done_add (function_done); \
801 LABEL (form_floathex): \
803 /* Floating point number printed as hexadecimal number. */ \
810 is_long_double = 0; \
812 struct printf_info info = { .prec = prec, \
815 .is_long_double = is_long_double, \
816 .is_short = is_short, \
817 .is_long = is_long, \
821 .showsign = showsign, \
825 .wide = sizeof (CHAR_T) != 1 }; \
827 if (is_long_double) \
828 the_arg.pa_long_double = va_arg (ap, long double); \
830 the_arg.pa_double = va_arg (ap, double); \
831 ptr = (const void *) &the_arg; \
833 function_done = __printf_fphex (s, &info, &ptr); \
837 ptr = (const void *) &args_value[fspec->data_arg]; \
839 fspec->info.is_long_double = 0; \
841 function_done = __printf_fphex (s, &fspec->info, &ptr); \
844 if (function_done < 0) \
846 /* Error in print handler; up to handler to set errno. */ \
851 done_add (function_done); \
855 LABEL (form_pointer): \
856 /* Generic pointer. */ \
860 ptr = va_arg (ap, void *); \
862 ptr = args_value[fspec->data_arg].pa_pointer; \
865 /* If the pointer is not NULL, write it as a %#x spec. */ \
867 number.word = (unsigned long int) ptr; \
872 goto LABEL (number); \
876 /* Write "(nil)" for a nil pointer. */ \
877 string = (CHAR_T *) L_("(nil)"); \
878 /* Make sure the full string "(nil)" is printed. */ \
881 /* This is a wide string iff compiling wprintf. */ \
882 is_long = sizeof (CHAR_T) > 1; \
883 goto LABEL (print_string); \
888 LABEL (form_number): \
889 if (s->_flags2 & _IO_FLAGS2_FORTIFY) \
891 if (! readonly_format) \
893 extern int __readonly_area (const void *, size_t) \
896 = __readonly_area (format, ((STR_LEN (format) + 1) \
897 * sizeof (CHAR_T))); \
899 if (readonly_format < 0) \
900 __libc_fatal ("*** %n in writable segment detected ***\n"); \
902 /* Answer the count of characters written. */ \
906 *(long long int *) va_arg (ap, void *) = done; \
907 else if (is_long_num) \
908 *(long int *) va_arg (ap, void *) = done; \
910 *(char *) va_arg (ap, void *) = done; \
911 else if (!is_short) \
912 *(int *) va_arg (ap, void *) = done; \
914 *(short int *) va_arg (ap, void *) = done; \
918 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
919 else if (is_long_num) \
920 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
922 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
923 else if (!is_short) \
924 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
926 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
929 LABEL (form_strerror): \
930 /* Print description of error ERRNO. */ \
932 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
933 WORK_BUFFER_SIZE * sizeof (CHAR_T)); \
934 is_long = 0; /* This is no wide-char string. */ \
935 goto LABEL (print_string)
937 #ifdef COMPILE_WPRINTF
938 # define process_string_arg(fspec) \
939 LABEL (form_character): \
942 goto LABEL (form_wcharacter); \
943 --width; /* Account for the character itself. */ \
947 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
949 outchar (__btowc ((unsigned char) \
950 args_value[fspec->data_arg].pa_int)); \
955 LABEL (form_wcharacter): \
957 /* Wide character. */ \
962 outchar (va_arg (ap, wchar_t)); \
964 outchar (args_value[fspec->data_arg].pa_wchar); \
970 LABEL (form_string): \
973 int string_malloced; \
975 /* The string argument could in fact be `char *' or `wchar_t *'. \
976 But this should not make a difference here. */ \
978 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
980 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
982 /* Entry point for printing other strings. */ \
983 LABEL (print_string): \
985 string_malloced = 0; \
986 if (string == NULL) \
988 /* Write "(null)" if there's space. */ \
990 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
992 string = (CHAR_T *) null; \
993 len = (sizeof (null) / sizeof (null[0])) - 1; \
997 string = (CHAR_T *) L""; \
1001 else if (!is_long && spec != L_('S')) \
1003 /* This is complicated. We have to transform the multibyte \
1004 string into a wide character string. */ \
1005 const char *mbs = (const char *) string; \
1006 mbstate_t mbstate; \
1008 len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
1010 /* Allocate dynamically an array which definitely is long \
1011 enough for the wide character version. Each byte in the \
1012 multi-byte string can produce at most one wide character. */ \
1013 if (__glibc_unlikely (len > SIZE_MAX / sizeof (wchar_t))) \
1015 __set_errno (EOVERFLOW); \
1019 else if (__libc_use_alloca (len * sizeof (wchar_t))) \
1020 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1021 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1028 string_malloced = 1; \
1030 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1031 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1032 if (len == (size_t) -1) \
1034 /* Illegal multibyte character. */ \
1042 /* Search for the end of the string, but don't search past \
1043 the length specified by the precision. */ \
1044 len = __wcsnlen (string, prec); \
1046 len = __wcslen (string); \
1049 if ((width -= len) < 0) \
1051 outstring (string, len); \
1057 outstring (string, len); \
1060 if (__glibc_unlikely (string_malloced)) \
1065 # define process_string_arg(fspec) \
1066 LABEL (form_character): \
1069 goto LABEL (form_wcharacter); \
1070 --width; /* Account for the character itself. */ \
1073 if (fspec == NULL) \
1074 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1076 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1081 LABEL (form_wcharacter): \
1083 /* Wide character. */ \
1084 char buf[MB_CUR_MAX]; \
1085 mbstate_t mbstate; \
1088 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1089 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1090 : args_value[fspec->data_arg].pa_wchar), \
1092 if (len == (size_t) -1) \
1094 /* Something went wrong during the conversion. Bail out. */ \
1101 outstring (buf, len); \
1107 LABEL (form_string): \
1110 int string_malloced; \
1112 /* The string argument could in fact be `char *' or `wchar_t *'. \
1113 But this should not make a difference here. */ \
1114 if (fspec == NULL) \
1115 string = (char *) va_arg (ap, const char *); \
1117 string = (char *) args_value[fspec->data_arg].pa_string; \
1119 /* Entry point for printing other strings. */ \
1120 LABEL (print_string): \
1122 string_malloced = 0; \
1123 if (string == NULL) \
1125 /* Write "(null)" if there's space. */ \
1126 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1128 string = (char *) null; \
1129 len = sizeof (null) - 1; \
1133 string = (char *) ""; \
1137 else if (!is_long && spec != L_('S')) \
1140 /* Search for the end of the string, but don't search past \
1141 the length (in bytes) specified by the precision. */ \
1142 len = __strnlen (string, prec); \
1144 len = strlen (string); \
1148 const wchar_t *s2 = (const wchar_t *) string; \
1149 mbstate_t mbstate; \
1151 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1155 /* The string `s2' might not be NUL terminated. */ \
1156 if (__libc_use_alloca (prec)) \
1157 string = (char *) alloca (prec); \
1158 else if ((string = (char *) malloc (prec)) == NULL) \
1164 string_malloced = 1; \
1165 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1169 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1170 if (len != (size_t) -1) \
1172 assert (__mbsinit (&mbstate)); \
1173 s2 = (const wchar_t *) string; \
1174 if (__libc_use_alloca (len + 1)) \
1175 string = (char *) alloca (len + 1); \
1176 else if ((string = (char *) malloc (len + 1)) == NULL) \
1182 string_malloced = 1; \
1183 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1187 if (len == (size_t) -1) \
1189 /* Illegal wide-character string. */ \
1195 if ((width -= len) < 0) \
1197 outstring (string, len); \
1203 outstring (string, len); \
1206 if (__glibc_unlikely (string_malloced)) \
1212 /* Helper function to provide temporary buffering for unbuffered streams. */
1213 static int buffered_vfprintf (FILE *stream
, const CHAR_T
*fmt
, va_list)
1214 __THROW
__attribute__ ((noinline
)) internal_function
;
1216 /* Handle positional format specifiers. */
1217 static int printf_positional (_IO_FILE
*s
,
1218 const CHAR_T
*format
, int readonly_format
,
1219 va_list ap
, va_list *ap_savep
, int done
,
1220 int nspecs_done
, const UCHAR_T
*lead_str_end
,
1221 CHAR_T
*work_buffer
, int save_errno
,
1222 const char *grouping
, THOUSANDS_SEP_T
);
1224 /* Handle unknown format specifier. */
1225 static int printf_unknown (FILE *, const struct printf_info
*,
1226 const void *const *) __THROW
;
1228 /* Group digits of number string. */
1229 static CHAR_T
*group_number (CHAR_T
*, CHAR_T
*, const char *, THOUSANDS_SEP_T
)
1230 __THROW internal_function
;
1232 /* The function itself. */
1234 vfprintf (FILE *s
, const CHAR_T
*format
, va_list ap
)
1236 /* The character used as thousands separator. */
1237 THOUSANDS_SEP_T thousands_sep
= 0;
1239 /* The string describing the size of groups of digits. */
1240 const char *grouping
;
1242 /* Place to accumulate the result. */
1245 /* Current character in format string. */
1248 /* End of leading constant string. */
1249 const UCHAR_T
*lead_str_end
;
1251 /* Points to next format specifier. */
1252 const UCHAR_T
*end_of_spec
;
1254 /* Buffer intermediate results. */
1255 CHAR_T work_buffer
[WORK_BUFFER_SIZE
];
1256 CHAR_T
*workstart
= NULL
;
1259 /* We have to save the original argument pointer. */
1262 /* Count number of specifiers we already processed. */
1265 /* For the %m format we may need the current `errno' value. */
1266 int save_errno
= errno
;
1268 /* 1 if format is in read-only memory, -1 if it is in writable memory,
1270 int readonly_format
= 0;
1272 /* Orient the stream. */
1277 /* Sanity check of arguments. */
1278 ARGCHECK (s
, format
);
1281 /* Check for correct orientation. */
1282 if (_IO_vtable_offset (s
) == 0 &&
1283 _IO_fwide (s
, sizeof (CHAR_T
) == 1 ? -1 : 1)
1284 != (sizeof (CHAR_T
) == 1 ? -1 : 1))
1285 /* The stream is already oriented otherwise. */
1289 if (UNBUFFERED_P (s
))
1290 /* Use a helper function which will allocate a local temporary buffer
1291 for the stream and then call us again. */
1292 return buffered_vfprintf (s
, format
, ap
);
1294 /* Initialize local variables. */
1296 grouping
= (const char *) -1;
1298 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1299 since on some systems `va_list' is not an integral type. */
1300 __va_copy (ap_save
, ap
);
1306 #ifdef COMPILE_WPRINTF
1307 /* Find the first format specifier. */
1308 f
= lead_str_end
= __find_specwc ((const UCHAR_T
*) format
);
1310 /* Find the first format specifier. */
1311 f
= lead_str_end
= __find_specmb ((const UCHAR_T
*) format
);
1315 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile
, s
);
1318 /* Write the literal text before the first format. */
1319 outstring ((const UCHAR_T
*) format
,
1320 lead_str_end
- (const UCHAR_T
*) format
);
1322 /* If we only have to print a simple string, return now. */
1326 /* Use the slow path in case any printf handler is registered. */
1327 if (__glibc_unlikely (__printf_function_table
!= NULL
1328 || __printf_modifier_table
!= NULL
1329 || __printf_va_arg_table
!= NULL
))
1332 /* Process whole format string. */
1338 union printf_arg
*args_value
; /* This is not used here but ... */
1339 int is_negative
; /* Flag for negative number. */
1342 unsigned long long int longlong
;
1343 unsigned long int word
;
1346 union printf_arg the_arg
;
1347 CHAR_T
*string
; /* Pointer to argument string. */
1348 int alt
= 0; /* Alternate format. */
1349 int space
= 0; /* Use space prefix if no sign is needed. */
1350 int left
= 0; /* Left-justify output. */
1351 int showsign
= 0; /* Always begin with plus or minus sign. */
1352 int group
= 0; /* Print numbers according grouping rules. */
1353 int is_long_double
= 0; /* Argument is long double/ long long int. */
1354 int is_short
= 0; /* Argument is short int. */
1355 int is_long
= 0; /* Argument is long int. */
1356 int is_char
= 0; /* Argument is promoted (unsigned) char. */
1357 int width
= 0; /* Width of output; 0 means none specified. */
1358 int prec
= -1; /* Precision of output; -1 means none specified. */
1359 /* This flag is set by the 'I' modifier and selects the use of the
1360 `outdigits' as determined by the current locale. */
1361 int use_outdigits
= 0;
1362 UCHAR_T pad
= L_(' ');/* Padding character. */
1366 workend
= work_buffer
+ WORK_BUFFER_SIZE
;
1368 /* Get current character in format string. */
1369 JUMP (*++f
, step0_jumps
);
1374 JUMP (*++f
, step0_jumps
);
1379 JUMP (*++f
, step0_jumps
);
1385 JUMP (*++f
, step0_jumps
);
1390 JUMP (*++f
, step0_jumps
);
1396 JUMP (*++f
, step0_jumps
);
1398 /* The '\'' flag. */
1402 if (grouping
== (const char *) -1)
1404 #ifdef COMPILE_WPRINTF
1405 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1406 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1408 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1411 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1412 if (*grouping
== '\0' || *grouping
== CHAR_MAX
1413 #ifdef COMPILE_WPRINTF
1414 || thousands_sep
== L
'\0'
1416 || *thousands_sep
== '\0'
1421 JUMP (*++f
, step0_jumps
);
1425 JUMP (*++f
, step0_jumps
);
1427 /* Get width from argument. */
1428 LABEL (width_asterics
):
1430 const UCHAR_T
*tmp
; /* Temporary value. */
1435 int pos
= read_int (&tmp
);
1439 __set_errno (EOVERFLOW
);
1444 if (pos
&& *tmp
== L_('$'))
1445 /* The width comes from a positional parameter. */
1448 width
= va_arg (ap
, int);
1450 /* Negative width means left justified. */
1458 if (__glibc_unlikely (width
>= INT_MAX
/ sizeof (CHAR_T
) - 32))
1460 __set_errno (EOVERFLOW
);
1465 if (width
>= WORK_BUFFER_SIZE
- 32)
1467 /* We have to use a special buffer. The "32" is just a safe
1468 bet for all the output which is not counted in the width. */
1469 size_t needed
= ((size_t) width
+ 32) * sizeof (CHAR_T
);
1470 if (__libc_use_alloca (needed
))
1471 workend
= (CHAR_T
*) alloca (needed
) + width
+ 32;
1474 workstart
= (CHAR_T
*) malloc (needed
);
1475 if (workstart
== NULL
)
1480 workend
= workstart
+ width
+ 32;
1484 JUMP (*f
, step1_jumps
);
1486 /* Given width in format string. */
1488 width
= read_int (&f
);
1490 if (__glibc_unlikely (width
== -1
1491 || width
>= INT_MAX
/ sizeof (CHAR_T
) - 32))
1493 __set_errno (EOVERFLOW
);
1498 if (width
>= WORK_BUFFER_SIZE
- 32)
1500 /* We have to use a special buffer. The "32" is just a safe
1501 bet for all the output which is not counted in the width. */
1502 size_t needed
= ((size_t) width
+ 32) * sizeof (CHAR_T
);
1503 if (__libc_use_alloca (needed
))
1504 workend
= (CHAR_T
*) alloca (needed
) + width
+ 32;
1507 workstart
= (CHAR_T
*) malloc (needed
);
1508 if (workstart
== NULL
)
1513 workend
= workstart
+ width
+ 32;
1517 /* Oh, oh. The argument comes from a positional parameter. */
1519 JUMP (*f
, step1_jumps
);
1525 const UCHAR_T
*tmp
; /* Temporary value. */
1530 int pos
= read_int (&tmp
);
1534 __set_errno (EOVERFLOW
);
1539 if (pos
&& *tmp
== L_('$'))
1540 /* The precision comes from a positional parameter. */
1543 prec
= va_arg (ap
, int);
1545 /* If the precision is negative the precision is omitted. */
1549 else if (ISDIGIT (*f
))
1551 prec
= read_int (&f
);
1553 /* The precision was specified in this case as an extremely
1554 large positive value. */
1557 __set_errno (EOVERFLOW
);
1564 if (prec
> width
&& prec
> WORK_BUFFER_SIZE
- 32)
1566 if (__glibc_unlikely (prec
>= INT_MAX
/ sizeof (CHAR_T
) - 32))
1568 __set_errno (EOVERFLOW
);
1572 size_t needed
= ((size_t) prec
+ 32) * sizeof (CHAR_T
);
1574 if (__libc_use_alloca (needed
))
1575 workend
= (CHAR_T
*) alloca (needed
) + prec
+ 32;
1578 workstart
= (CHAR_T
*) malloc (needed
);
1579 if (workstart
== NULL
)
1584 workend
= workstart
+ prec
+ 32;
1587 JUMP (*f
, step2_jumps
);
1589 /* Process 'h' modifier. There might another 'h' following. */
1592 JUMP (*++f
, step3a_jumps
);
1594 /* Process 'hh' modifier. */
1595 LABEL (mod_halfhalf
):
1598 JUMP (*++f
, step4_jumps
);
1600 /* Process 'l' modifier. There might another 'l' following. */
1603 JUMP (*++f
, step3b_jumps
);
1605 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1606 allowed to follow. */
1607 LABEL (mod_longlong
):
1610 JUMP (*++f
, step4_jumps
);
1613 is_long_double
= sizeof (size_t) > sizeof (unsigned long int);
1614 is_long
= sizeof (size_t) > sizeof (unsigned int);
1615 JUMP (*++f
, step4_jumps
);
1617 LABEL (mod_ptrdiff_t
):
1618 is_long_double
= sizeof (ptrdiff_t) > sizeof (unsigned long int);
1619 is_long
= sizeof (ptrdiff_t) > sizeof (unsigned int);
1620 JUMP (*++f
, step4_jumps
);
1622 LABEL (mod_intmax_t
):
1623 is_long_double
= sizeof (intmax_t) > sizeof (unsigned long int);
1624 is_long
= sizeof (intmax_t) > sizeof (unsigned int);
1625 JUMP (*++f
, step4_jumps
);
1627 /* Process current format. */
1630 process_arg (((struct printf_spec
*) NULL
));
1631 process_string_arg (((struct printf_spec
*) NULL
));
1633 LABEL (form_unknown
):
1634 if (spec
== L_('\0'))
1636 /* The format string ended before the specifier is complete. */
1637 __set_errno (EINVAL
);
1642 /* If we are in the fast loop force entering the complicated
1647 /* The format is correctly handled. */
1650 if (__glibc_unlikely (workstart
!= NULL
))
1654 /* Look for next format specifier. */
1655 #ifdef COMPILE_WPRINTF
1656 f
= __find_specwc ((end_of_spec
= ++f
));
1658 f
= __find_specmb ((end_of_spec
= ++f
));
1661 /* Write the following constant string. */
1662 outstring (end_of_spec
, f
- end_of_spec
);
1664 while (*f
!= L_('\0'));
1666 /* Unlock stream and return. */
1669 /* Hand off processing for positional parameters. */
1671 if (__glibc_unlikely (workstart
!= NULL
))
1676 done
= printf_positional (s
, format
, readonly_format
, ap
, &ap_save
,
1677 done
, nspecs_done
, lead_str_end
, work_buffer
,
1678 save_errno
, grouping
, thousands_sep
);
1681 if (__glibc_unlikely (workstart
!= NULL
))
1683 /* Unlock the stream. */
1684 _IO_funlockfile (s
);
1685 _IO_cleanup_region_end (0);
1691 printf_positional (_IO_FILE
*s
, const CHAR_T
*format
, int readonly_format
,
1692 va_list ap
, va_list *ap_savep
, int done
, int nspecs_done
,
1693 const UCHAR_T
*lead_str_end
,
1694 CHAR_T
*work_buffer
, int save_errno
,
1695 const char *grouping
, THOUSANDS_SEP_T thousands_sep
)
1697 /* For the argument descriptions, which may be allocated on the heap. */
1698 void *args_malloced
= NULL
;
1700 /* For positional argument handling. */
1701 struct printf_spec
*specs
;
1703 /* Track if we malloced the SPECS array and thus must free it. */
1704 bool specs_malloced
= false;
1706 /* Array with information about the needed arguments. This has to
1707 be dynamically extensible. */
1709 /* A more or less arbitrary start value. */
1710 size_t nspecs_size
= 32 * sizeof (struct printf_spec
);
1712 specs
= alloca (nspecs_size
);
1713 /* The number of arguments the format string requests. This will
1714 determine the size of the array needed to store the argument
1717 size_t bytes_per_arg
;
1718 union printf_arg
*args_value
;
1722 /* Positional parameters refer to arguments directly. This could
1723 also determine the maximum number of arguments. Track the
1725 size_t max_ref_arg
= 0;
1727 /* Just a counter. */
1730 CHAR_T
*workstart
= NULL
;
1732 if (grouping
== (const char *) -1)
1734 #ifdef COMPILE_WPRINTF
1735 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1736 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1738 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1741 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1742 if (*grouping
== '\0' || *grouping
== CHAR_MAX
)
1746 for (const UCHAR_T
*f
= lead_str_end
; *f
!= L_('\0');
1747 f
= specs
[nspecs
++].next_fmt
)
1749 if (nspecs
* sizeof (*specs
) >= nspecs_size
)
1751 /* Extend the array of format specifiers. */
1752 if (nspecs_size
* 2 < nspecs_size
)
1754 __set_errno (ENOMEM
);
1758 struct printf_spec
*old
= specs
;
1759 if (__libc_use_alloca (2 * nspecs_size
))
1760 specs
= extend_alloca (specs
, nspecs_size
, 2 * nspecs_size
);
1764 specs
= malloc (nspecs_size
);
1767 __set_errno (ENOMEM
);
1774 /* Copy the old array's elements to the new space. */
1775 memmove (specs
, old
, nspecs
* sizeof (*specs
));
1777 /* If we had previously malloc'd space for SPECS, then
1778 release it after the copy is complete. */
1782 /* Now set SPECS_MALLOCED if needed. */
1783 if (!__libc_use_alloca (nspecs_size
))
1784 specs_malloced
= true;
1787 /* Parse the format specifier. */
1788 #ifdef COMPILE_WPRINTF
1789 nargs
+= __parse_one_specwc (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1791 nargs
+= __parse_one_specmb (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1795 /* Determine the number of arguments the format string consumes. */
1796 nargs
= MAX (nargs
, max_ref_arg
);
1797 /* Calculate total size needed to represent a single argument across
1798 all three argument-related arrays. */
1799 bytes_per_arg
= (sizeof (*args_value
) + sizeof (*args_size
)
1800 + sizeof (*args_type
));
1802 /* Check for potential integer overflow. */
1803 if (__glibc_unlikely (nargs
> INT_MAX
/ bytes_per_arg
))
1805 __set_errno (EOVERFLOW
);
1810 /* Allocate memory for all three argument arrays. */
1811 if (__libc_use_alloca (nargs
* bytes_per_arg
))
1812 args_value
= alloca (nargs
* bytes_per_arg
);
1815 args_value
= args_malloced
= malloc (nargs
* bytes_per_arg
);
1816 if (args_value
== NULL
)
1823 /* Set up the remaining two arrays to each point past the end of the
1824 prior array, since space for all three has been allocated now. */
1825 args_size
= &args_value
[nargs
].pa_int
;
1826 args_type
= &args_size
[nargs
];
1827 memset (args_type
, s
->_flags2
& _IO_FLAGS2_FORTIFY
? '\xff' : '\0',
1828 nargs
* sizeof (*args_type
));
1830 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1831 still zero after this loop, format is invalid. For now we
1832 simply use 0 as the value. */
1834 /* Fill in the types of all the arguments. */
1835 for (cnt
= 0; cnt
< nspecs
; ++cnt
)
1837 /* If the width is determined by an argument this is an int. */
1838 if (specs
[cnt
].width_arg
!= -1)
1839 args_type
[specs
[cnt
].width_arg
] = PA_INT
;
1841 /* If the precision is determined by an argument this is an int. */
1842 if (specs
[cnt
].prec_arg
!= -1)
1843 args_type
[specs
[cnt
].prec_arg
] = PA_INT
;
1845 switch (specs
[cnt
].ndata_args
)
1847 case 0: /* No arguments. */
1849 case 1: /* One argument; we already have the
1851 args_type
[specs
[cnt
].data_arg
] = specs
[cnt
].data_arg_type
;
1852 args_size
[specs
[cnt
].data_arg
] = specs
[cnt
].size
;
1855 /* We have more than one argument for this format spec.
1856 We must call the arginfo function again to determine
1858 (void) (*__printf_arginfo_table
[specs
[cnt
].info
.spec
])
1860 specs
[cnt
].ndata_args
, &args_type
[specs
[cnt
].data_arg
],
1861 &args_size
[specs
[cnt
].data_arg
]);
1866 /* Now we know all the types and the order. Fill in the argument
1868 for (cnt
= 0; cnt
< nargs
; ++cnt
)
1869 switch (args_type
[cnt
])
1871 #define T(tag, mem, type) \
1873 args_value[cnt].mem = va_arg (*ap_savep, type); \
1876 T (PA_WCHAR
, pa_wchar
, wint_t);
1877 case PA_CHAR
: /* Promoted. */
1878 case PA_INT
|PA_FLAG_SHORT
: /* Promoted. */
1879 #if LONG_MAX == INT_MAX
1880 case PA_INT
|PA_FLAG_LONG
:
1882 T (PA_INT
, pa_int
, int);
1883 #if LONG_MAX == LONG_LONG_MAX
1884 case PA_INT
|PA_FLAG_LONG
:
1886 T (PA_INT
|PA_FLAG_LONG_LONG
, pa_long_long_int
, long long int);
1887 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1890 case PA_FLOAT
: /* Promoted. */
1891 T (PA_DOUBLE
, pa_double
, double);
1892 case PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
:
1895 args_value
[cnt
].pa_double
= va_arg (*ap_savep
, double);
1896 args_type
[cnt
] &= ~PA_FLAG_LONG_DOUBLE
;
1899 args_value
[cnt
].pa_long_double
= va_arg (*ap_savep
, long double);
1901 case PA_STRING
: /* All pointers are the same */
1902 case PA_WSTRING
: /* All pointers are the same */
1903 T (PA_POINTER
, pa_pointer
, void *);
1906 if ((args_type
[cnt
] & PA_FLAG_PTR
) != 0)
1907 args_value
[cnt
].pa_pointer
= va_arg (*ap_savep
, void *);
1908 else if (__glibc_unlikely (__printf_va_arg_table
!= NULL
)
1909 && __printf_va_arg_table
[args_type
[cnt
] - PA_LAST
] != NULL
)
1911 args_value
[cnt
].pa_user
= alloca (args_size
[cnt
]);
1912 (*__printf_va_arg_table
[args_type
[cnt
] - PA_LAST
])
1913 (args_value
[cnt
].pa_user
, ap_savep
);
1916 args_value
[cnt
].pa_long_double
= 0.0;
1919 /* Error case. Not all parameters appear in N$ format
1920 strings. We have no way to determine their type. */
1921 assert (s
->_flags2
& _IO_FLAGS2_FORTIFY
);
1922 __libc_fatal ("*** invalid %N$ use detected ***\n");
1925 /* Now walk through all format specifiers and process them. */
1926 for (; (size_t) nspecs_done
< nspecs
; ++nspecs_done
)
1933 unsigned long long int longlong
;
1934 unsigned long int word
;
1937 union printf_arg the_arg
;
1938 CHAR_T
*string
; /* Pointer to argument string. */
1940 /* Fill variables from values in struct. */
1941 int alt
= specs
[nspecs_done
].info
.alt
;
1942 int space
= specs
[nspecs_done
].info
.space
;
1943 int left
= specs
[nspecs_done
].info
.left
;
1944 int showsign
= specs
[nspecs_done
].info
.showsign
;
1945 int group
= specs
[nspecs_done
].info
.group
;
1946 int is_long_double
= specs
[nspecs_done
].info
.is_long_double
;
1947 int is_short
= specs
[nspecs_done
].info
.is_short
;
1948 int is_char
= specs
[nspecs_done
].info
.is_char
;
1949 int is_long
= specs
[nspecs_done
].info
.is_long
;
1950 int width
= specs
[nspecs_done
].info
.width
;
1951 int prec
= specs
[nspecs_done
].info
.prec
;
1952 int use_outdigits
= specs
[nspecs_done
].info
.i18n
;
1953 char pad
= specs
[nspecs_done
].info
.pad
;
1954 CHAR_T spec
= specs
[nspecs_done
].info
.spec
;
1957 CHAR_T
*workend
= work_buffer
+ WORK_BUFFER_SIZE
;
1959 /* Fill in last information. */
1960 if (specs
[nspecs_done
].width_arg
!= -1)
1962 /* Extract the field width from an argument. */
1963 specs
[nspecs_done
].info
.width
=
1964 args_value
[specs
[nspecs_done
].width_arg
].pa_int
;
1966 if (specs
[nspecs_done
].info
.width
< 0)
1967 /* If the width value is negative left justification is
1968 selected and the value is taken as being positive. */
1970 specs
[nspecs_done
].info
.width
*= -1;
1971 left
= specs
[nspecs_done
].info
.left
= 1;
1973 width
= specs
[nspecs_done
].info
.width
;
1976 if (specs
[nspecs_done
].prec_arg
!= -1)
1978 /* Extract the precision from an argument. */
1979 specs
[nspecs_done
].info
.prec
=
1980 args_value
[specs
[nspecs_done
].prec_arg
].pa_int
;
1982 if (specs
[nspecs_done
].info
.prec
< 0)
1983 /* If the precision is negative the precision is
1985 specs
[nspecs_done
].info
.prec
= -1;
1987 prec
= specs
[nspecs_done
].info
.prec
;
1990 /* Maybe the buffer is too small. */
1991 if (MAX (prec
, width
) + 32 > WORK_BUFFER_SIZE
)
1993 if (__libc_use_alloca ((MAX (prec
, width
) + 32)
1995 workend
= ((CHAR_T
*) alloca ((MAX (prec
, width
) + 32)
1997 + (MAX (prec
, width
) + 32));
2000 workstart
= (CHAR_T
*) malloc ((MAX (prec
, width
) + 32)
2002 if (workstart
== NULL
)
2007 workend
= workstart
+ (MAX (prec
, width
) + 32);
2011 /* Process format specifiers. */
2014 extern printf_function
**__printf_function_table
;
2017 if (spec
<= UCHAR_MAX
2018 && __printf_function_table
!= NULL
2019 && __printf_function_table
[(size_t) spec
] != NULL
)
2021 const void **ptr
= alloca (specs
[nspecs_done
].ndata_args
2022 * sizeof (const void *));
2024 /* Fill in an array of pointers to the argument values. */
2025 for (unsigned int i
= 0; i
< specs
[nspecs_done
].ndata_args
;
2027 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
2029 /* Call the function. */
2030 function_done
= __printf_function_table
[(size_t) spec
]
2031 (s
, &specs
[nspecs_done
].info
, ptr
);
2033 if (function_done
!= -2)
2035 /* If an error occurred we don't have information
2036 about # of chars. */
2037 if (function_done
< 0)
2039 /* Function has set errno. */
2044 done_add (function_done
);
2049 JUMP (spec
, step4_jumps
);
2051 process_arg ((&specs
[nspecs_done
]));
2052 process_string_arg ((&specs
[nspecs_done
]));
2054 LABEL (form_unknown
):
2059 ptr
= alloca (specs
[nspecs_done
].ndata_args
2060 * sizeof (const void *));
2062 /* Fill in an array of pointers to the argument values. */
2063 for (i
= 0; i
< specs
[nspecs_done
].ndata_args
; ++i
)
2064 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
2066 /* Call the function. */
2067 function_done
= printf_unknown (s
, &specs
[nspecs_done
].info
,
2070 /* If an error occurred we don't have information about #
2072 if (function_done
< 0)
2074 /* Function has set errno. */
2079 done_add (function_done
);
2084 if (__glibc_unlikely (workstart
!= NULL
))
2088 /* Write the following constant string. */
2089 outstring (specs
[nspecs_done
].end_of_fmt
,
2090 specs
[nspecs_done
].next_fmt
2091 - specs
[nspecs_done
].end_of_fmt
);
2094 if (__glibc_unlikely (workstart
!= NULL
))
2099 /* Handle an unknown format specifier. This prints out a canonicalized
2100 representation of the format spec itself. */
2102 printf_unknown (FILE *s
, const struct printf_info
*info
,
2103 const void *const *args
)
2107 CHAR_T work_buffer
[MAX (sizeof (info
->width
), sizeof (info
->prec
)) * 3];
2108 CHAR_T
*const workend
2109 = &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
2120 else if (info
->space
)
2124 if (info
->pad
== L_('0'))
2129 if (info
->width
!= 0)
2131 w
= _itoa_word (info
->width
, workend
, 10, 0);
2136 if (info
->prec
!= -1)
2139 w
= _itoa_word (info
->prec
, workend
, 10, 0);
2144 if (info
->spec
!= L_('\0'))
2145 outchar (info
->spec
);
2151 /* Group the digits according to the grouping rules of the current locale.
2152 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
2155 group_number (CHAR_T
*w
, CHAR_T
*rear_ptr
, const char *grouping
,
2156 THOUSANDS_SEP_T thousands_sep
)
2160 #ifndef COMPILE_WPRINTF
2161 int tlen
= strlen (thousands_sep
);
2164 /* We treat all negative values like CHAR_MAX. */
2166 if (*grouping
== CHAR_MAX
|| *grouping
<= 0)
2167 /* No grouping should be done. */
2172 /* Copy existing string so that nothing gets overwritten. */
2173 src
= (CHAR_T
*) alloca ((rear_ptr
- w
) * sizeof (CHAR_T
));
2174 s
= (CHAR_T
*) __mempcpy (src
, w
,
2175 (rear_ptr
- w
) * sizeof (CHAR_T
));
2178 /* Process all characters in the string. */
2183 if (--len
== 0 && s
> src
)
2185 /* A new group begins. */
2186 #ifdef COMPILE_WPRINTF
2187 *--w
= thousands_sep
;
2191 *--w
= thousands_sep
[--cnt
];
2195 if (*grouping
== CHAR_MAX
2201 /* No further grouping to be done.
2202 Copy the rest of the number. */
2208 else if (*grouping
!= '\0')
2209 /* The previous grouping repeats ad infinitum. */
2218 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2221 struct _IO_FILE_plus _f
;
2222 #ifdef COMPILE_WPRINTF
2223 struct _IO_wide_data _wide_data
;
2225 _IO_FILE
*_put_stream
;
2226 #ifdef _IO_MTSAFE_IO
2232 _IO_helper_overflow (_IO_FILE
*s
, int c
)
2234 _IO_FILE
*target
= ((struct helper_file
*) s
)->_put_stream
;
2235 #ifdef COMPILE_WPRINTF
2236 int used
= s
->_wide_data
->_IO_write_ptr
- s
->_wide_data
->_IO_write_base
;
2239 _IO_size_t written
= _IO_sputn (target
, s
->_wide_data
->_IO_write_base
,
2241 if (written
== 0 || written
== WEOF
)
2243 __wmemmove (s
->_wide_data
->_IO_write_base
,
2244 s
->_wide_data
->_IO_write_base
+ written
,
2246 s
->_wide_data
->_IO_write_ptr
-= written
;
2249 int used
= s
->_IO_write_ptr
- s
->_IO_write_base
;
2252 _IO_size_t written
= _IO_sputn (target
, s
->_IO_write_base
, used
);
2253 if (written
== 0 || written
== EOF
)
2255 memmove (s
->_IO_write_base
, s
->_IO_write_base
+ written
,
2257 s
->_IO_write_ptr
-= written
;
2263 #ifdef COMPILE_WPRINTF
2264 static const struct _IO_jump_t _IO_helper_jumps
=
2267 JUMP_INIT (finish
, _IO_wdefault_finish
),
2268 JUMP_INIT (overflow
, _IO_helper_overflow
),
2269 JUMP_INIT (underflow
, _IO_default_underflow
),
2270 JUMP_INIT (uflow
, _IO_default_uflow
),
2271 JUMP_INIT (pbackfail
, (_IO_pbackfail_t
) _IO_wdefault_pbackfail
),
2272 JUMP_INIT (xsputn
, _IO_wdefault_xsputn
),
2273 JUMP_INIT (xsgetn
, _IO_wdefault_xsgetn
),
2274 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2275 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2276 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2277 JUMP_INIT (sync
, _IO_default_sync
),
2278 JUMP_INIT (doallocate
, _IO_wdefault_doallocate
),
2279 JUMP_INIT (read
, _IO_default_read
),
2280 JUMP_INIT (write
, _IO_default_write
),
2281 JUMP_INIT (seek
, _IO_default_seek
),
2282 JUMP_INIT (close
, _IO_default_close
),
2283 JUMP_INIT (stat
, _IO_default_stat
)
2286 static const struct _IO_jump_t _IO_helper_jumps
=
2289 JUMP_INIT (finish
, _IO_default_finish
),
2290 JUMP_INIT (overflow
, _IO_helper_overflow
),
2291 JUMP_INIT (underflow
, _IO_default_underflow
),
2292 JUMP_INIT (uflow
, _IO_default_uflow
),
2293 JUMP_INIT (pbackfail
, _IO_default_pbackfail
),
2294 JUMP_INIT (xsputn
, _IO_default_xsputn
),
2295 JUMP_INIT (xsgetn
, _IO_default_xsgetn
),
2296 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2297 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2298 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2299 JUMP_INIT (sync
, _IO_default_sync
),
2300 JUMP_INIT (doallocate
, _IO_default_doallocate
),
2301 JUMP_INIT (read
, _IO_default_read
),
2302 JUMP_INIT (write
, _IO_default_write
),
2303 JUMP_INIT (seek
, _IO_default_seek
),
2304 JUMP_INIT (close
, _IO_default_close
),
2305 JUMP_INIT (stat
, _IO_default_stat
)
2311 buffered_vfprintf (_IO_FILE
*s
, const CHAR_T
*format
,
2314 CHAR_T buf
[_IO_BUFSIZ
];
2315 struct helper_file helper
;
2316 _IO_FILE
*hp
= (_IO_FILE
*) &helper
._f
;
2317 int result
, to_flush
;
2319 /* Orient the stream. */
2324 /* Initialize helper. */
2325 helper
._put_stream
= s
;
2326 #ifdef COMPILE_WPRINTF
2327 hp
->_wide_data
= &helper
._wide_data
;
2328 _IO_wsetp (hp
, buf
, buf
+ sizeof buf
/ sizeof (CHAR_T
));
2331 _IO_setp (hp
, buf
, buf
+ sizeof buf
);
2334 hp
->_IO_file_flags
= _IO_MAGIC
|_IO_NO_READS
|_IO_USER_LOCK
;
2335 #if _IO_JUMPS_OFFSET
2336 hp
->_vtable_offset
= 0;
2338 #ifdef _IO_MTSAFE_IO
2341 hp
->_flags2
= s
->_flags2
;
2342 _IO_JUMPS (&helper
._f
) = (struct _IO_jump_t
*) &_IO_helper_jumps
;
2344 /* Now print to helper instead. */
2345 #ifndef COMPILE_WPRINTF
2346 result
= _IO_vfprintf (hp
, format
, args
);
2348 result
= vfprintf (hp
, format
, args
);
2352 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile
, s
);
2355 /* Now flush anything from the helper to the S. */
2356 #ifdef COMPILE_WPRINTF
2357 if ((to_flush
= (hp
->_wide_data
->_IO_write_ptr
2358 - hp
->_wide_data
->_IO_write_base
)) > 0)
2360 if ((int) _IO_sputn (s
, hp
->_wide_data
->_IO_write_base
, to_flush
)
2365 if ((to_flush
= hp
->_IO_write_ptr
- hp
->_IO_write_base
) > 0)
2367 if ((int) _IO_sputn (s
, hp
->_IO_write_base
, to_flush
) != to_flush
)
2372 /* Unlock the stream. */
2373 _IO_funlockfile (s
);
2374 __libc_cleanup_region_end (0);
2380 #ifdef COMPILE_WPRINTF
2381 strong_alias (_IO_vfwprintf
, __vfwprintf
);
2382 ldbl_weak_alias (_IO_vfwprintf
, vfwprintf
);
2384 ldbl_strong_alias (_IO_vfprintf_internal
, vfprintf
);
2385 ldbl_hidden_def (_IO_vfprintf_internal
, vfprintf
)
2386 ldbl_strong_alias (_IO_vfprintf_internal
, _IO_vfprintf
);
2387 ldbl_hidden_def (_IO_vfprintf_internal
, _IO_vfprintf
)