1 /* Copyright (C) 1991-2016 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 #define ARGCHECK(S, Format) \
51 /* Check file argument for consistence. */ \
53 if (S->_flags & _IO_NO_WRITES) \
55 S->_flags |= _IO_ERR_SEEN; \
56 __set_errno (EBADF); \
65 #define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
67 #define done_add(val) \
69 unsigned int _val = val; \
70 assert ((unsigned int) done < (unsigned int) INT_MAX); \
71 if (__glibc_unlikely (INT_MAX - done < _val)) \
74 __set_errno (EOVERFLOW); \
80 #ifndef COMPILE_WPRINTF
81 # define vfprintf _IO_vfprintf_internal
83 # define UCHAR_T unsigned char
85 typedef const char *THOUSANDS_SEP_T
;
87 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
88 # define STR_LEN(Str) strlen (Str)
90 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
91 # define PAD(Padchar) \
95 _IO_ssize_t written = _IO_padn (s, (Padchar), width); \
96 if (__glibc_unlikely (written != width)) \
101 done_add (written); \
104 # define PUTC(C, F) _IO_putc_unlocked (C, F)
105 # define ORIENT if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
108 # define vfprintf _IO_vfwprintf
109 # define CHAR_T wchar_t
110 /* This is a hack!!! There should be a type uwchar_t. */
111 # define UCHAR_T unsigned int /* uwchar_t */
112 # define INT_T wint_t
113 typedef wchar_t THOUSANDS_SEP_T
;
114 # define L_(Str) L##Str
115 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
116 # define STR_LEN(Str) __wcslen (Str)
120 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
121 # define PAD(Padchar) \
125 _IO_ssize_t written = _IO_wpadn (s, (Padchar), width); \
126 if (__glibc_unlikely (written != width)) \
131 done_add (written); \
134 # define PUTC(C, F) _IO_putwc_unlocked (C, F)
135 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
138 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
139 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
144 #include "_i18n_number.h"
146 /* Include the shared code for parsing the format string. */
147 #include "printf-parse.h"
150 #define outchar(Ch) \
153 const INT_T outc = (Ch); \
154 if (PUTC (outc, s) == EOF || done == INT_MAX) \
163 #define outstring(String, Len) \
166 assert ((size_t) done <= (size_t) INT_MAX); \
167 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
172 if (__glibc_unlikely (INT_MAX - done < (Len))) \
175 __set_errno (EOVERFLOW); \
182 /* For handling long_double and longlong we use the same flag. If
183 `long' and `long long' are effectively the same type define it to
185 #if LONG_MAX == LONG_LONG_MAX
186 # define is_longlong 0
188 # define is_longlong is_long_double
191 /* If `long' and `int' is effectively the same type we don't have to
192 handle `long separately. */
193 #if INT_MAX == LONG_MAX
194 # define is_long_num 0
196 # define is_long_num is_long
200 /* Global constants. */
201 static const CHAR_T null
[] = L_("(null)");
203 /* Size of the work_buffer variable (in characters, not bytes. */
204 enum { WORK_BUFFER_SIZE
= 1000 };
206 /* This table maps a character into a number representing a class. In
207 each step there is a destination label for each class. */
208 static const uint8_t jump_table
[] =
210 /* ' ' */ 1, 0, 0, /* '#' */ 4,
211 0, /* '%' */ 14, 0, /* '\''*/ 6,
212 0, 0, /* '*' */ 7, /* '+' */ 2,
213 0, /* '-' */ 3, /* '.' */ 9, 0,
214 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
215 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
216 /* '8' */ 8, /* '9' */ 8, 0, 0,
218 0, /* 'A' */ 26, 0, /* 'C' */ 25,
219 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
220 0, /* 'I' */ 29, 0, 0,
221 /* 'L' */ 12, 0, 0, 0,
222 0, 0, 0, /* 'S' */ 21,
224 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
226 0, /* 'a' */ 26, 0, /* 'c' */ 20,
227 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
228 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
229 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
230 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
231 /* 't' */ 27, /* 'u' */ 16, 0, 0,
232 /* 'x' */ 18, 0, /* 'z' */ 13
235 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
236 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
237 #define LABEL(Name) do_##Name
239 /* 'int' is enough and it saves some space on 64 bit systems. */
240 # define JUMP_TABLE_TYPE const int
241 # define JUMP_TABLE_BASE_LABEL do_form_unknown
242 # define REF(Name) &&do_##Name - &&JUMP_TABLE_BASE_LABEL
243 # define JUMP(ChExpr, table) \
249 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
250 : table[CHAR_CLASS (spec)]; \
251 ptr = &&JUMP_TABLE_BASE_LABEL + offset; \
256 # define JUMP_TABLE_TYPE const void *const
257 # define REF(Name) &&do_##Name
258 # define JUMP(ChExpr, table) \
263 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
264 : table[CHAR_CLASS (spec)]; \
270 #define STEP0_3_TABLE \
271 /* Step 0: at the beginning. */ \
272 static JUMP_TABLE_TYPE step0_jumps[30] = \
274 REF (form_unknown), \
275 REF (flag_space), /* for ' ' */ \
276 REF (flag_plus), /* for '+' */ \
277 REF (flag_minus), /* for '-' */ \
278 REF (flag_hash), /* for '<hash>' */ \
279 REF (flag_zero), /* for '0' */ \
280 REF (flag_quote), /* for '\'' */ \
281 REF (width_asterics), /* for '*' */ \
282 REF (width), /* for '1'...'9' */ \
283 REF (precision), /* for '.' */ \
284 REF (mod_half), /* for 'h' */ \
285 REF (mod_long), /* for 'l' */ \
286 REF (mod_longlong), /* for 'L', 'q' */ \
287 REF (mod_size_t), /* for 'z', 'Z' */ \
288 REF (form_percent), /* for '%' */ \
289 REF (form_integer), /* for 'd', 'i' */ \
290 REF (form_unsigned), /* for 'u' */ \
291 REF (form_octal), /* for 'o' */ \
292 REF (form_hexa), /* for 'X', 'x' */ \
293 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
294 REF (form_character), /* for 'c' */ \
295 REF (form_string), /* for 's', 'S' */ \
296 REF (form_pointer), /* for 'p' */ \
297 REF (form_number), /* for 'n' */ \
298 REF (form_strerror), /* for 'm' */ \
299 REF (form_wcharacter), /* for 'C' */ \
300 REF (form_floathex), /* for 'A', 'a' */ \
301 REF (mod_ptrdiff_t), /* for 't' */ \
302 REF (mod_intmax_t), /* for 'j' */ \
303 REF (flag_i18n), /* for 'I' */ \
305 /* Step 1: after processing width. */ \
306 static JUMP_TABLE_TYPE step1_jumps[30] = \
308 REF (form_unknown), \
309 REF (form_unknown), /* for ' ' */ \
310 REF (form_unknown), /* for '+' */ \
311 REF (form_unknown), /* for '-' */ \
312 REF (form_unknown), /* for '<hash>' */ \
313 REF (form_unknown), /* for '0' */ \
314 REF (form_unknown), /* for '\'' */ \
315 REF (form_unknown), /* for '*' */ \
316 REF (form_unknown), /* for '1'...'9' */ \
317 REF (precision), /* for '.' */ \
318 REF (mod_half), /* for 'h' */ \
319 REF (mod_long), /* for 'l' */ \
320 REF (mod_longlong), /* for 'L', 'q' */ \
321 REF (mod_size_t), /* for 'z', 'Z' */ \
322 REF (form_percent), /* for '%' */ \
323 REF (form_integer), /* for 'd', 'i' */ \
324 REF (form_unsigned), /* for 'u' */ \
325 REF (form_octal), /* for 'o' */ \
326 REF (form_hexa), /* for 'X', 'x' */ \
327 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
328 REF (form_character), /* for 'c' */ \
329 REF (form_string), /* for 's', 'S' */ \
330 REF (form_pointer), /* for 'p' */ \
331 REF (form_number), /* for 'n' */ \
332 REF (form_strerror), /* for 'm' */ \
333 REF (form_wcharacter), /* for 'C' */ \
334 REF (form_floathex), /* for 'A', 'a' */ \
335 REF (mod_ptrdiff_t), /* for 't' */ \
336 REF (mod_intmax_t), /* for 'j' */ \
337 REF (form_unknown) /* for 'I' */ \
339 /* Step 2: after processing precision. */ \
340 static JUMP_TABLE_TYPE step2_jumps[30] = \
342 REF (form_unknown), \
343 REF (form_unknown), /* for ' ' */ \
344 REF (form_unknown), /* for '+' */ \
345 REF (form_unknown), /* for '-' */ \
346 REF (form_unknown), /* for '<hash>' */ \
347 REF (form_unknown), /* for '0' */ \
348 REF (form_unknown), /* for '\'' */ \
349 REF (form_unknown), /* for '*' */ \
350 REF (form_unknown), /* for '1'...'9' */ \
351 REF (form_unknown), /* for '.' */ \
352 REF (mod_half), /* for 'h' */ \
353 REF (mod_long), /* for 'l' */ \
354 REF (mod_longlong), /* for 'L', 'q' */ \
355 REF (mod_size_t), /* for 'z', 'Z' */ \
356 REF (form_percent), /* for '%' */ \
357 REF (form_integer), /* for 'd', 'i' */ \
358 REF (form_unsigned), /* for 'u' */ \
359 REF (form_octal), /* for 'o' */ \
360 REF (form_hexa), /* for 'X', 'x' */ \
361 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
362 REF (form_character), /* for 'c' */ \
363 REF (form_string), /* for 's', 'S' */ \
364 REF (form_pointer), /* for 'p' */ \
365 REF (form_number), /* for 'n' */ \
366 REF (form_strerror), /* for 'm' */ \
367 REF (form_wcharacter), /* for 'C' */ \
368 REF (form_floathex), /* for 'A', 'a' */ \
369 REF (mod_ptrdiff_t), /* for 't' */ \
370 REF (mod_intmax_t), /* for 'j' */ \
371 REF (form_unknown) /* for 'I' */ \
373 /* Step 3a: after processing first 'h' modifier. */ \
374 static JUMP_TABLE_TYPE step3a_jumps[30] = \
376 REF (form_unknown), \
377 REF (form_unknown), /* for ' ' */ \
378 REF (form_unknown), /* for '+' */ \
379 REF (form_unknown), /* for '-' */ \
380 REF (form_unknown), /* for '<hash>' */ \
381 REF (form_unknown), /* for '0' */ \
382 REF (form_unknown), /* for '\'' */ \
383 REF (form_unknown), /* for '*' */ \
384 REF (form_unknown), /* for '1'...'9' */ \
385 REF (form_unknown), /* for '.' */ \
386 REF (mod_halfhalf), /* for 'h' */ \
387 REF (form_unknown), /* for 'l' */ \
388 REF (form_unknown), /* for 'L', 'q' */ \
389 REF (form_unknown), /* for 'z', 'Z' */ \
390 REF (form_percent), /* for '%' */ \
391 REF (form_integer), /* for 'd', 'i' */ \
392 REF (form_unsigned), /* for 'u' */ \
393 REF (form_octal), /* for 'o' */ \
394 REF (form_hexa), /* for 'X', 'x' */ \
395 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
396 REF (form_unknown), /* for 'c' */ \
397 REF (form_unknown), /* for 's', 'S' */ \
398 REF (form_unknown), /* for 'p' */ \
399 REF (form_number), /* for 'n' */ \
400 REF (form_unknown), /* for 'm' */ \
401 REF (form_unknown), /* for 'C' */ \
402 REF (form_unknown), /* for 'A', 'a' */ \
403 REF (form_unknown), /* for 't' */ \
404 REF (form_unknown), /* for 'j' */ \
405 REF (form_unknown) /* for 'I' */ \
407 /* Step 3b: after processing first 'l' modifier. */ \
408 static JUMP_TABLE_TYPE step3b_jumps[30] = \
410 REF (form_unknown), \
411 REF (form_unknown), /* for ' ' */ \
412 REF (form_unknown), /* for '+' */ \
413 REF (form_unknown), /* for '-' */ \
414 REF (form_unknown), /* for '<hash>' */ \
415 REF (form_unknown), /* for '0' */ \
416 REF (form_unknown), /* for '\'' */ \
417 REF (form_unknown), /* for '*' */ \
418 REF (form_unknown), /* for '1'...'9' */ \
419 REF (form_unknown), /* for '.' */ \
420 REF (form_unknown), /* for 'h' */ \
421 REF (mod_longlong), /* for 'l' */ \
422 REF (form_unknown), /* for 'L', 'q' */ \
423 REF (form_unknown), /* for 'z', 'Z' */ \
424 REF (form_percent), /* for '%' */ \
425 REF (form_integer), /* for 'd', 'i' */ \
426 REF (form_unsigned), /* for 'u' */ \
427 REF (form_octal), /* for 'o' */ \
428 REF (form_hexa), /* for 'X', 'x' */ \
429 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
430 REF (form_character), /* for 'c' */ \
431 REF (form_string), /* for 's', 'S' */ \
432 REF (form_pointer), /* for 'p' */ \
433 REF (form_number), /* for 'n' */ \
434 REF (form_strerror), /* for 'm' */ \
435 REF (form_wcharacter), /* for 'C' */ \
436 REF (form_floathex), /* for 'A', 'a' */ \
437 REF (form_unknown), /* for 't' */ \
438 REF (form_unknown), /* for 'j' */ \
439 REF (form_unknown) /* for 'I' */ \
442 #define STEP4_TABLE \
443 /* Step 4: processing format specifier. */ \
444 static JUMP_TABLE_TYPE step4_jumps[30] = \
446 REF (form_unknown), \
447 REF (form_unknown), /* for ' ' */ \
448 REF (form_unknown), /* for '+' */ \
449 REF (form_unknown), /* for '-' */ \
450 REF (form_unknown), /* for '<hash>' */ \
451 REF (form_unknown), /* for '0' */ \
452 REF (form_unknown), /* for '\'' */ \
453 REF (form_unknown), /* for '*' */ \
454 REF (form_unknown), /* for '1'...'9' */ \
455 REF (form_unknown), /* for '.' */ \
456 REF (form_unknown), /* for 'h' */ \
457 REF (form_unknown), /* for 'l' */ \
458 REF (form_unknown), /* for 'L', 'q' */ \
459 REF (form_unknown), /* for 'z', 'Z' */ \
460 REF (form_percent), /* for '%' */ \
461 REF (form_integer), /* for 'd', 'i' */ \
462 REF (form_unsigned), /* for 'u' */ \
463 REF (form_octal), /* for 'o' */ \
464 REF (form_hexa), /* for 'X', 'x' */ \
465 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
466 REF (form_character), /* for 'c' */ \
467 REF (form_string), /* for 's', 'S' */ \
468 REF (form_pointer), /* for 'p' */ \
469 REF (form_number), /* for 'n' */ \
470 REF (form_strerror), /* for 'm' */ \
471 REF (form_wcharacter), /* for 'C' */ \
472 REF (form_floathex), /* for 'A', 'a' */ \
473 REF (form_unknown), /* for 't' */ \
474 REF (form_unknown), /* for 'j' */ \
475 REF (form_unknown) /* for 'I' */ \
479 #define process_arg(fspec) \
480 /* Start real work. We know about all flags and modifiers and \
481 now process the wanted format specifier. */ \
482 LABEL (form_percent): \
483 /* Write a literal "%". */ \
487 LABEL (form_integer): \
488 /* Signed decimal integer. */ \
493 long long int signed_number; \
496 signed_number = va_arg (ap, long long int); \
498 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
500 is_negative = signed_number < 0; \
501 number.longlong = is_negative ? (- signed_number) : signed_number; \
503 goto LABEL (longlong_number); \
507 long int signed_number; \
512 signed_number = va_arg (ap, long int); \
514 signed_number = (signed char) va_arg (ap, unsigned int); \
515 else if (!is_short) \
516 signed_number = va_arg (ap, int); \
518 signed_number = (short int) va_arg (ap, unsigned int); \
522 signed_number = args_value[fspec->data_arg].pa_long_int; \
524 signed_number = (signed char) \
525 args_value[fspec->data_arg].pa_u_int; \
526 else if (!is_short) \
527 signed_number = args_value[fspec->data_arg].pa_int; \
529 signed_number = (short int) \
530 args_value[fspec->data_arg].pa_u_int; \
532 is_negative = signed_number < 0; \
533 number.word = is_negative ? (- signed_number) : signed_number; \
535 goto LABEL (number); \
539 LABEL (form_unsigned): \
540 /* Unsigned decimal integer. */ \
542 goto LABEL (unsigned_number); \
545 LABEL (form_octal): \
546 /* Unsigned octal integer. */ \
548 goto LABEL (unsigned_number); \
552 /* Unsigned hexadecimal integer. */ \
555 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
557 /* ISO specifies the `+' and ` ' flags only for signed \
566 number.longlong = va_arg (ap, unsigned long long int); \
568 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
570 LABEL (longlong_number): \
572 /* Supply a default precision if none was given. */ \
575 /* We have to take care for the '0' flag. If a precision \
576 is given it must be ignored. */ \
579 /* If the precision is 0 and the number is 0 nothing has to \
580 be written for the number, except for the 'o' format in \
582 if (prec == 0 && number.longlong == 0) \
585 if (base == 8 && alt) \
586 *--string = L_('0'); \
590 /* Put the number in WORK. */ \
591 string = _itoa (number.longlong, workend, base, \
593 if (group && grouping) \
594 string = group_number (string, workend, grouping, \
597 if (use_outdigits && base == 10) \
598 string = _i18n_number_rewrite (string, workend, workend); \
600 /* Simplify further test for num != 0. */ \
601 number.word = number.longlong != 0; \
608 number.word = va_arg (ap, unsigned long int); \
610 number.word = (unsigned char) va_arg (ap, unsigned int); \
611 else if (!is_short) \
612 number.word = va_arg (ap, unsigned int); \
614 number.word = (unsigned short int) va_arg (ap, unsigned int); \
618 number.word = args_value[fspec->data_arg].pa_u_long_int; \
620 number.word = (unsigned char) \
621 args_value[fspec->data_arg].pa_u_int; \
622 else if (!is_short) \
623 number.word = args_value[fspec->data_arg].pa_u_int; \
625 number.word = (unsigned short int) \
626 args_value[fspec->data_arg].pa_u_int; \
630 /* Supply a default precision if none was given. */ \
633 /* We have to take care for the '0' flag. If a precision \
634 is given it must be ignored. */ \
637 /* If the precision is 0 and the number is 0 nothing has to \
638 be written for the number, except for the 'o' format in \
640 if (prec == 0 && number.word == 0) \
643 if (base == 8 && alt) \
644 *--string = L_('0'); \
648 /* Put the number in WORK. */ \
649 string = _itoa_word (number.word, workend, base, \
651 if (group && grouping) \
652 string = group_number (string, workend, grouping, \
655 if (use_outdigits && base == 10) \
656 string = _i18n_number_rewrite (string, workend, workend); \
660 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
661 /* Add octal marker. */ \
662 *--string = L_('0'); \
664 prec = MAX (0, prec - (workend - string)); \
668 width -= workend - string + prec; \
670 if (number.word != 0 && alt && base == 16) \
671 /* Account for 0X hex marker. */ \
674 if (is_negative || showsign || space) \
677 if (pad == L_(' ')) \
690 if (number.word != 0 && alt && base == 16) \
699 outstring (string, workend - string); \
721 if (number.word != 0 && alt && base == 16) \
728 width -= workend - string + prec; \
738 outstring (string, workend - string); \
744 LABEL (form_float): \
746 /* Floating-point number. This is handled by printf_fp.c. */ \
753 is_long_double = 0; \
755 struct printf_info info = { .prec = prec, \
758 .is_long_double = is_long_double, \
759 .is_short = is_short, \
760 .is_long = is_long, \
764 .showsign = showsign, \
768 .i18n = use_outdigits, \
769 .wide = sizeof (CHAR_T) != 1 }; \
771 if (is_long_double) \
772 the_arg.pa_long_double = va_arg (ap, long double); \
774 the_arg.pa_double = va_arg (ap, double); \
775 ptr = (const void *) &the_arg; \
777 function_done = __printf_fp (s, &info, &ptr); \
781 ptr = (const void *) &args_value[fspec->data_arg]; \
784 fspec->data_arg_type = PA_DOUBLE; \
785 fspec->info.is_long_double = 0; \
788 function_done = __printf_fp (s, &fspec->info, &ptr); \
791 if (function_done < 0) \
793 /* Error in print handler; up to handler to set errno. */ \
798 done_add (function_done); \
802 LABEL (form_floathex): \
804 /* Floating point number printed as hexadecimal number. */ \
811 is_long_double = 0; \
813 struct printf_info info = { .prec = prec, \
816 .is_long_double = is_long_double, \
817 .is_short = is_short, \
818 .is_long = is_long, \
822 .showsign = showsign, \
826 .wide = sizeof (CHAR_T) != 1 }; \
828 if (is_long_double) \
829 the_arg.pa_long_double = va_arg (ap, long double); \
831 the_arg.pa_double = va_arg (ap, double); \
832 ptr = (const void *) &the_arg; \
834 function_done = __printf_fphex (s, &info, &ptr); \
838 ptr = (const void *) &args_value[fspec->data_arg]; \
840 fspec->info.is_long_double = 0; \
842 function_done = __printf_fphex (s, &fspec->info, &ptr); \
845 if (function_done < 0) \
847 /* Error in print handler; up to handler to set errno. */ \
852 done_add (function_done); \
856 LABEL (form_pointer): \
857 /* Generic pointer. */ \
861 ptr = va_arg (ap, void *); \
863 ptr = args_value[fspec->data_arg].pa_pointer; \
866 /* If the pointer is not NULL, write it as a %#x spec. */ \
868 number.word = (unsigned long int) ptr; \
873 goto LABEL (number); \
877 /* Write "(nil)" for a nil pointer. */ \
878 string = (CHAR_T *) L_("(nil)"); \
879 /* Make sure the full string "(nil)" is printed. */ \
882 /* This is a wide string iff compiling wprintf. */ \
883 is_long = sizeof (CHAR_T) > 1; \
884 goto LABEL (print_string); \
889 LABEL (form_number): \
890 if (s->_flags2 & _IO_FLAGS2_FORTIFY) \
892 if (! readonly_format) \
894 extern int __readonly_area (const void *, size_t) \
897 = __readonly_area (format, ((STR_LEN (format) + 1) \
898 * sizeof (CHAR_T))); \
900 if (readonly_format < 0) \
901 __libc_fatal ("*** %n in writable segment detected ***\n"); \
903 /* Answer the count of characters written. */ \
907 *(long long int *) va_arg (ap, void *) = done; \
908 else if (is_long_num) \
909 *(long int *) va_arg (ap, void *) = done; \
911 *(char *) va_arg (ap, void *) = done; \
912 else if (!is_short) \
913 *(int *) va_arg (ap, void *) = done; \
915 *(short int *) va_arg (ap, void *) = done; \
919 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
920 else if (is_long_num) \
921 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
923 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
924 else if (!is_short) \
925 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
927 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
930 LABEL (form_strerror): \
931 /* Print description of error ERRNO. */ \
933 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
934 WORK_BUFFER_SIZE * sizeof (CHAR_T)); \
935 is_long = 0; /* This is no wide-char string. */ \
936 goto LABEL (print_string)
938 #ifdef COMPILE_WPRINTF
939 # define process_string_arg(fspec) \
940 LABEL (form_character): \
943 goto LABEL (form_wcharacter); \
944 --width; /* Account for the character itself. */ \
948 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
950 outchar (__btowc ((unsigned char) \
951 args_value[fspec->data_arg].pa_int)); \
956 LABEL (form_wcharacter): \
958 /* Wide character. */ \
963 outchar (va_arg (ap, wchar_t)); \
965 outchar (args_value[fspec->data_arg].pa_wchar); \
971 LABEL (form_string): \
974 int string_malloced; \
976 /* The string argument could in fact be `char *' or `wchar_t *'. \
977 But this should not make a difference here. */ \
979 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
981 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
983 /* Entry point for printing other strings. */ \
984 LABEL (print_string): \
986 string_malloced = 0; \
987 if (string == NULL) \
989 /* Write "(null)" if there's space. */ \
991 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
993 string = (CHAR_T *) null; \
994 len = (sizeof (null) / sizeof (null[0])) - 1; \
998 string = (CHAR_T *) L""; \
1002 else if (!is_long && spec != L_('S')) \
1004 /* This is complicated. We have to transform the multibyte \
1005 string into a wide character string. */ \
1006 const char *mbs = (const char *) string; \
1007 mbstate_t mbstate; \
1009 len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
1011 /* Allocate dynamically an array which definitely is long \
1012 enough for the wide character version. Each byte in the \
1013 multi-byte string can produce at most one wide character. */ \
1014 if (__glibc_unlikely (len > SIZE_MAX / sizeof (wchar_t))) \
1016 __set_errno (EOVERFLOW); \
1020 else if (__libc_use_alloca (len * sizeof (wchar_t))) \
1021 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1022 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1029 string_malloced = 1; \
1031 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1032 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1033 if (len == (size_t) -1) \
1035 /* Illegal multibyte character. */ \
1043 /* Search for the end of the string, but don't search past \
1044 the length specified by the precision. */ \
1045 len = __wcsnlen (string, prec); \
1047 len = __wcslen (string); \
1050 if ((width -= len) < 0) \
1052 outstring (string, len); \
1058 outstring (string, len); \
1061 if (__glibc_unlikely (string_malloced)) \
1066 # define process_string_arg(fspec) \
1067 LABEL (form_character): \
1070 goto LABEL (form_wcharacter); \
1071 --width; /* Account for the character itself. */ \
1074 if (fspec == NULL) \
1075 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1077 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1082 LABEL (form_wcharacter): \
1084 /* Wide character. */ \
1085 char buf[MB_CUR_MAX]; \
1086 mbstate_t mbstate; \
1089 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1090 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1091 : args_value[fspec->data_arg].pa_wchar), \
1093 if (len == (size_t) -1) \
1095 /* Something went wrong during the conversion. Bail out. */ \
1102 outstring (buf, len); \
1108 LABEL (form_string): \
1111 int string_malloced; \
1113 /* The string argument could in fact be `char *' or `wchar_t *'. \
1114 But this should not make a difference here. */ \
1115 if (fspec == NULL) \
1116 string = (char *) va_arg (ap, const char *); \
1118 string = (char *) args_value[fspec->data_arg].pa_string; \
1120 /* Entry point for printing other strings. */ \
1121 LABEL (print_string): \
1123 string_malloced = 0; \
1124 if (string == NULL) \
1126 /* Write "(null)" if there's space. */ \
1127 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1129 string = (char *) null; \
1130 len = sizeof (null) - 1; \
1134 string = (char *) ""; \
1138 else if (!is_long && spec != L_('S')) \
1141 /* Search for the end of the string, but don't search past \
1142 the length (in bytes) specified by the precision. */ \
1143 len = __strnlen (string, prec); \
1145 len = strlen (string); \
1149 const wchar_t *s2 = (const wchar_t *) string; \
1150 mbstate_t mbstate; \
1152 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1156 /* The string `s2' might not be NUL terminated. */ \
1157 if (__libc_use_alloca (prec)) \
1158 string = (char *) alloca (prec); \
1159 else if ((string = (char *) malloc (prec)) == NULL) \
1165 string_malloced = 1; \
1166 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1170 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1171 if (len != (size_t) -1) \
1173 assert (__mbsinit (&mbstate)); \
1174 s2 = (const wchar_t *) string; \
1175 if (__libc_use_alloca (len + 1)) \
1176 string = (char *) alloca (len + 1); \
1177 else if ((string = (char *) malloc (len + 1)) == NULL) \
1183 string_malloced = 1; \
1184 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1188 if (len == (size_t) -1) \
1190 /* Illegal wide-character string. */ \
1196 if ((width -= len) < 0) \
1198 outstring (string, len); \
1204 outstring (string, len); \
1207 if (__glibc_unlikely (string_malloced)) \
1213 /* Helper function to provide temporary buffering for unbuffered streams. */
1214 static int buffered_vfprintf (FILE *stream
, const CHAR_T
*fmt
, va_list)
1215 __THROW
__attribute__ ((noinline
)) internal_function
;
1217 /* Handle positional format specifiers. */
1218 static int printf_positional (_IO_FILE
*s
,
1219 const CHAR_T
*format
, int readonly_format
,
1220 va_list ap
, va_list *ap_savep
, int done
,
1221 int nspecs_done
, const UCHAR_T
*lead_str_end
,
1222 CHAR_T
*work_buffer
, int save_errno
,
1223 const char *grouping
, THOUSANDS_SEP_T
);
1225 /* Handle unknown format specifier. */
1226 static int printf_unknown (FILE *, const struct printf_info
*,
1227 const void *const *) __THROW
;
1229 /* Group digits of number string. */
1230 static CHAR_T
*group_number (CHAR_T
*, CHAR_T
*, const char *, THOUSANDS_SEP_T
)
1231 __THROW internal_function
;
1233 /* The function itself. */
1235 vfprintf (FILE *s
, const CHAR_T
*format
, va_list ap
)
1237 /* The character used as thousands separator. */
1238 THOUSANDS_SEP_T thousands_sep
= 0;
1240 /* The string describing the size of groups of digits. */
1241 const char *grouping
;
1243 /* Place to accumulate the result. */
1246 /* Current character in format string. */
1249 /* End of leading constant string. */
1250 const UCHAR_T
*lead_str_end
;
1252 /* Points to next format specifier. */
1253 const UCHAR_T
*end_of_spec
;
1255 /* Buffer intermediate results. */
1256 CHAR_T work_buffer
[WORK_BUFFER_SIZE
];
1257 CHAR_T
*workstart
= NULL
;
1260 /* We have to save the original argument pointer. */
1263 /* Count number of specifiers we already processed. */
1266 /* For the %m format we may need the current `errno' value. */
1267 int save_errno
= errno
;
1269 /* 1 if format is in read-only memory, -1 if it is in writable memory,
1271 int readonly_format
= 0;
1273 /* Orient the stream. */
1278 /* Sanity check of arguments. */
1279 ARGCHECK (s
, format
);
1282 /* Check for correct orientation. */
1283 if (_IO_vtable_offset (s
) == 0 &&
1284 _IO_fwide (s
, sizeof (CHAR_T
) == 1 ? -1 : 1)
1285 != (sizeof (CHAR_T
) == 1 ? -1 : 1))
1286 /* The stream is already oriented otherwise. */
1290 if (UNBUFFERED_P (s
))
1291 /* Use a helper function which will allocate a local temporary buffer
1292 for the stream and then call us again. */
1293 return buffered_vfprintf (s
, format
, ap
);
1295 /* Initialize local variables. */
1297 grouping
= (const char *) -1;
1299 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1300 since on some systems `va_list' is not an integral type. */
1301 __va_copy (ap_save
, ap
);
1307 #ifdef COMPILE_WPRINTF
1308 /* Find the first format specifier. */
1309 f
= lead_str_end
= __find_specwc ((const UCHAR_T
*) format
);
1311 /* Find the first format specifier. */
1312 f
= lead_str_end
= __find_specmb ((const UCHAR_T
*) format
);
1316 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile
, s
);
1319 /* Write the literal text before the first format. */
1320 outstring ((const UCHAR_T
*) format
,
1321 lead_str_end
- (const UCHAR_T
*) format
);
1323 /* If we only have to print a simple string, return now. */
1327 /* Use the slow path in case any printf handler is registered. */
1328 if (__glibc_unlikely (__printf_function_table
!= NULL
1329 || __printf_modifier_table
!= NULL
1330 || __printf_va_arg_table
!= NULL
))
1333 /* Process whole format string. */
1339 union printf_arg
*args_value
; /* This is not used here but ... */
1340 int is_negative
; /* Flag for negative number. */
1343 unsigned long long int longlong
;
1344 unsigned long int word
;
1347 union printf_arg the_arg
;
1348 CHAR_T
*string
; /* Pointer to argument string. */
1349 int alt
= 0; /* Alternate format. */
1350 int space
= 0; /* Use space prefix if no sign is needed. */
1351 int left
= 0; /* Left-justify output. */
1352 int showsign
= 0; /* Always begin with plus or minus sign. */
1353 int group
= 0; /* Print numbers according grouping rules. */
1354 int is_long_double
= 0; /* Argument is long double/ long long int. */
1355 int is_short
= 0; /* Argument is short int. */
1356 int is_long
= 0; /* Argument is long int. */
1357 int is_char
= 0; /* Argument is promoted (unsigned) char. */
1358 int width
= 0; /* Width of output; 0 means none specified. */
1359 int prec
= -1; /* Precision of output; -1 means none specified. */
1360 /* This flag is set by the 'I' modifier and selects the use of the
1361 `outdigits' as determined by the current locale. */
1362 int use_outdigits
= 0;
1363 UCHAR_T pad
= L_(' ');/* Padding character. */
1367 workend
= work_buffer
+ WORK_BUFFER_SIZE
;
1369 /* Get current character in format string. */
1370 JUMP (*++f
, step0_jumps
);
1375 JUMP (*++f
, step0_jumps
);
1380 JUMP (*++f
, step0_jumps
);
1386 JUMP (*++f
, step0_jumps
);
1391 JUMP (*++f
, step0_jumps
);
1397 JUMP (*++f
, step0_jumps
);
1399 /* The '\'' flag. */
1403 if (grouping
== (const char *) -1)
1405 #ifdef COMPILE_WPRINTF
1406 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1407 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1409 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1412 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1413 if (*grouping
== '\0' || *grouping
== CHAR_MAX
1414 #ifdef COMPILE_WPRINTF
1415 || thousands_sep
== L
'\0'
1417 || *thousands_sep
== '\0'
1422 JUMP (*++f
, step0_jumps
);
1426 JUMP (*++f
, step0_jumps
);
1428 /* Get width from argument. */
1429 LABEL (width_asterics
):
1431 const UCHAR_T
*tmp
; /* Temporary value. */
1436 int pos
= read_int (&tmp
);
1440 __set_errno (EOVERFLOW
);
1445 if (pos
&& *tmp
== L_('$'))
1446 /* The width comes from a positional parameter. */
1449 width
= va_arg (ap
, int);
1451 /* Negative width means left justified. */
1459 if (__glibc_unlikely (width
>= INT_MAX
/ sizeof (CHAR_T
) - 32))
1461 __set_errno (EOVERFLOW
);
1466 if (width
>= WORK_BUFFER_SIZE
- 32)
1468 /* We have to use a special buffer. The "32" is just a safe
1469 bet for all the output which is not counted in the width. */
1470 size_t needed
= ((size_t) width
+ 32) * sizeof (CHAR_T
);
1471 if (__libc_use_alloca (needed
))
1472 workend
= (CHAR_T
*) alloca (needed
) + width
+ 32;
1475 workstart
= (CHAR_T
*) malloc (needed
);
1476 if (workstart
== NULL
)
1481 workend
= workstart
+ width
+ 32;
1485 JUMP (*f
, step1_jumps
);
1487 /* Given width in format string. */
1489 width
= read_int (&f
);
1491 if (__glibc_unlikely (width
== -1
1492 || width
>= INT_MAX
/ sizeof (CHAR_T
) - 32))
1494 __set_errno (EOVERFLOW
);
1499 if (width
>= WORK_BUFFER_SIZE
- 32)
1501 /* We have to use a special buffer. The "32" is just a safe
1502 bet for all the output which is not counted in the width. */
1503 size_t needed
= ((size_t) width
+ 32) * sizeof (CHAR_T
);
1504 if (__libc_use_alloca (needed
))
1505 workend
= (CHAR_T
*) alloca (needed
) + width
+ 32;
1508 workstart
= (CHAR_T
*) malloc (needed
);
1509 if (workstart
== NULL
)
1514 workend
= workstart
+ width
+ 32;
1518 /* Oh, oh. The argument comes from a positional parameter. */
1520 JUMP (*f
, step1_jumps
);
1526 const UCHAR_T
*tmp
; /* Temporary value. */
1531 int pos
= read_int (&tmp
);
1535 __set_errno (EOVERFLOW
);
1540 if (pos
&& *tmp
== L_('$'))
1541 /* The precision comes from a positional parameter. */
1544 prec
= va_arg (ap
, int);
1546 /* If the precision is negative the precision is omitted. */
1550 else if (ISDIGIT (*f
))
1552 prec
= read_int (&f
);
1554 /* The precision was specified in this case as an extremely
1555 large positive value. */
1558 __set_errno (EOVERFLOW
);
1565 if (prec
> width
&& prec
> WORK_BUFFER_SIZE
- 32)
1567 /* Deallocate any previously allocated buffer because it is
1569 if (__glibc_unlikely (workstart
!= NULL
))
1572 if (__glibc_unlikely (prec
>= INT_MAX
/ sizeof (CHAR_T
) - 32))
1574 __set_errno (EOVERFLOW
);
1578 size_t needed
= ((size_t) prec
+ 32) * sizeof (CHAR_T
);
1580 if (__libc_use_alloca (needed
))
1581 workend
= (CHAR_T
*) alloca (needed
) + prec
+ 32;
1584 workstart
= (CHAR_T
*) malloc (needed
);
1585 if (workstart
== NULL
)
1590 workend
= workstart
+ prec
+ 32;
1593 JUMP (*f
, step2_jumps
);
1595 /* Process 'h' modifier. There might another 'h' following. */
1598 JUMP (*++f
, step3a_jumps
);
1600 /* Process 'hh' modifier. */
1601 LABEL (mod_halfhalf
):
1604 JUMP (*++f
, step4_jumps
);
1606 /* Process 'l' modifier. There might another 'l' following. */
1609 JUMP (*++f
, step3b_jumps
);
1611 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1612 allowed to follow. */
1613 LABEL (mod_longlong
):
1616 JUMP (*++f
, step4_jumps
);
1619 is_long_double
= sizeof (size_t) > sizeof (unsigned long int);
1620 is_long
= sizeof (size_t) > sizeof (unsigned int);
1621 JUMP (*++f
, step4_jumps
);
1623 LABEL (mod_ptrdiff_t
):
1624 is_long_double
= sizeof (ptrdiff_t) > sizeof (unsigned long int);
1625 is_long
= sizeof (ptrdiff_t) > sizeof (unsigned int);
1626 JUMP (*++f
, step4_jumps
);
1628 LABEL (mod_intmax_t
):
1629 is_long_double
= sizeof (intmax_t) > sizeof (unsigned long int);
1630 is_long
= sizeof (intmax_t) > sizeof (unsigned int);
1631 JUMP (*++f
, step4_jumps
);
1633 /* Process current format. */
1636 process_arg (((struct printf_spec
*) NULL
));
1637 process_string_arg (((struct printf_spec
*) NULL
));
1639 LABEL (form_unknown
):
1640 if (spec
== L_('\0'))
1642 /* The format string ended before the specifier is complete. */
1643 __set_errno (EINVAL
);
1648 /* If we are in the fast loop force entering the complicated
1653 /* The format is correctly handled. */
1656 if (__glibc_unlikely (workstart
!= NULL
))
1660 /* Look for next format specifier. */
1661 #ifdef COMPILE_WPRINTF
1662 f
= __find_specwc ((end_of_spec
= ++f
));
1664 f
= __find_specmb ((end_of_spec
= ++f
));
1667 /* Write the following constant string. */
1668 outstring (end_of_spec
, f
- end_of_spec
);
1670 while (*f
!= L_('\0'));
1672 /* Unlock stream and return. */
1675 /* Hand off processing for positional parameters. */
1677 if (__glibc_unlikely (workstart
!= NULL
))
1682 done
= printf_positional (s
, format
, readonly_format
, ap
, &ap_save
,
1683 done
, nspecs_done
, lead_str_end
, work_buffer
,
1684 save_errno
, grouping
, thousands_sep
);
1687 if (__glibc_unlikely (workstart
!= NULL
))
1689 /* Unlock the stream. */
1690 _IO_funlockfile (s
);
1691 _IO_cleanup_region_end (0);
1697 printf_positional (_IO_FILE
*s
, const CHAR_T
*format
, int readonly_format
,
1698 va_list ap
, va_list *ap_savep
, int done
, int nspecs_done
,
1699 const UCHAR_T
*lead_str_end
,
1700 CHAR_T
*work_buffer
, int save_errno
,
1701 const char *grouping
, THOUSANDS_SEP_T thousands_sep
)
1703 /* For the argument descriptions, which may be allocated on the heap. */
1704 void *args_malloced
= NULL
;
1706 /* For positional argument handling. */
1707 struct scratch_buffer specsbuf
;
1708 scratch_buffer_init (&specsbuf
);
1709 struct printf_spec
*specs
= specsbuf
.data
;
1710 size_t specs_limit
= specsbuf
.length
/ sizeof (specs
[0]);
1712 /* Array with information about the needed arguments. This has to
1713 be dynamically extensible. */
1716 /* The number of arguments the format string requests. This will
1717 determine the size of the array needed to store the argument
1720 size_t bytes_per_arg
;
1721 union printf_arg
*args_value
;
1725 /* Positional parameters refer to arguments directly. This could
1726 also determine the maximum number of arguments. Track the
1728 size_t max_ref_arg
= 0;
1730 /* Just a counter. */
1733 CHAR_T
*workstart
= NULL
;
1735 if (grouping
== (const char *) -1)
1737 #ifdef COMPILE_WPRINTF
1738 thousands_sep
= _NL_CURRENT_WORD (LC_NUMERIC
,
1739 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1741 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
1744 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
1745 if (*grouping
== '\0' || *grouping
== CHAR_MAX
)
1749 for (const UCHAR_T
*f
= lead_str_end
; *f
!= L_('\0');
1750 f
= specs
[nspecs
++].next_fmt
)
1752 if (nspecs
== specs_limit
)
1754 if (!scratch_buffer_grow_preserve (&specsbuf
))
1759 specs
= specsbuf
.data
;
1760 specs_limit
= specsbuf
.length
/ sizeof (specs
[0]);
1763 /* Parse the format specifier. */
1764 #ifdef COMPILE_WPRINTF
1765 nargs
+= __parse_one_specwc (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1767 nargs
+= __parse_one_specmb (f
, nargs
, &specs
[nspecs
], &max_ref_arg
);
1771 /* Determine the number of arguments the format string consumes. */
1772 nargs
= MAX (nargs
, max_ref_arg
);
1773 /* Calculate total size needed to represent a single argument across
1774 all three argument-related arrays. */
1775 bytes_per_arg
= (sizeof (*args_value
) + sizeof (*args_size
)
1776 + sizeof (*args_type
));
1778 /* Check for potential integer overflow. */
1779 if (__glibc_unlikely (nargs
> INT_MAX
/ bytes_per_arg
))
1781 __set_errno (EOVERFLOW
);
1786 /* Allocate memory for all three argument arrays. */
1787 if (__libc_use_alloca (nargs
* bytes_per_arg
))
1788 args_value
= alloca (nargs
* bytes_per_arg
);
1791 args_value
= args_malloced
= malloc (nargs
* bytes_per_arg
);
1792 if (args_value
== NULL
)
1799 /* Set up the remaining two arrays to each point past the end of the
1800 prior array, since space for all three has been allocated now. */
1801 args_size
= &args_value
[nargs
].pa_int
;
1802 args_type
= &args_size
[nargs
];
1803 memset (args_type
, s
->_flags2
& _IO_FLAGS2_FORTIFY
? '\xff' : '\0',
1804 nargs
* sizeof (*args_type
));
1806 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1807 still zero after this loop, format is invalid. For now we
1808 simply use 0 as the value. */
1810 /* Fill in the types of all the arguments. */
1811 for (cnt
= 0; cnt
< nspecs
; ++cnt
)
1813 /* If the width is determined by an argument this is an int. */
1814 if (specs
[cnt
].width_arg
!= -1)
1815 args_type
[specs
[cnt
].width_arg
] = PA_INT
;
1817 /* If the precision is determined by an argument this is an int. */
1818 if (specs
[cnt
].prec_arg
!= -1)
1819 args_type
[specs
[cnt
].prec_arg
] = PA_INT
;
1821 switch (specs
[cnt
].ndata_args
)
1823 case 0: /* No arguments. */
1825 case 1: /* One argument; we already have the
1827 args_type
[specs
[cnt
].data_arg
] = specs
[cnt
].data_arg_type
;
1828 args_size
[specs
[cnt
].data_arg
] = specs
[cnt
].size
;
1831 /* We have more than one argument for this format spec.
1832 We must call the arginfo function again to determine
1834 (void) (*__printf_arginfo_table
[specs
[cnt
].info
.spec
])
1836 specs
[cnt
].ndata_args
, &args_type
[specs
[cnt
].data_arg
],
1837 &args_size
[specs
[cnt
].data_arg
]);
1842 /* Now we know all the types and the order. Fill in the argument
1844 for (cnt
= 0; cnt
< nargs
; ++cnt
)
1845 switch (args_type
[cnt
])
1847 #define T(tag, mem, type) \
1849 args_value[cnt].mem = va_arg (*ap_savep, type); \
1852 T (PA_WCHAR
, pa_wchar
, wint_t);
1853 case PA_CHAR
: /* Promoted. */
1854 case PA_INT
|PA_FLAG_SHORT
: /* Promoted. */
1855 #if LONG_MAX == INT_MAX
1856 case PA_INT
|PA_FLAG_LONG
:
1858 T (PA_INT
, pa_int
, int);
1859 #if LONG_MAX == LONG_LONG_MAX
1860 case PA_INT
|PA_FLAG_LONG
:
1862 T (PA_INT
|PA_FLAG_LONG_LONG
, pa_long_long_int
, long long int);
1863 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1866 case PA_FLOAT
: /* Promoted. */
1867 T (PA_DOUBLE
, pa_double
, double);
1868 case PA_DOUBLE
|PA_FLAG_LONG_DOUBLE
:
1871 args_value
[cnt
].pa_double
= va_arg (*ap_savep
, double);
1872 args_type
[cnt
] &= ~PA_FLAG_LONG_DOUBLE
;
1875 args_value
[cnt
].pa_long_double
= va_arg (*ap_savep
, long double);
1877 case PA_STRING
: /* All pointers are the same */
1878 case PA_WSTRING
: /* All pointers are the same */
1879 T (PA_POINTER
, pa_pointer
, void *);
1882 if ((args_type
[cnt
] & PA_FLAG_PTR
) != 0)
1883 args_value
[cnt
].pa_pointer
= va_arg (*ap_savep
, void *);
1884 else if (__glibc_unlikely (__printf_va_arg_table
!= NULL
)
1885 && __printf_va_arg_table
[args_type
[cnt
] - PA_LAST
] != NULL
)
1887 args_value
[cnt
].pa_user
= alloca (args_size
[cnt
]);
1888 (*__printf_va_arg_table
[args_type
[cnt
] - PA_LAST
])
1889 (args_value
[cnt
].pa_user
, ap_savep
);
1892 args_value
[cnt
].pa_long_double
= 0.0;
1895 /* Error case. Not all parameters appear in N$ format
1896 strings. We have no way to determine their type. */
1897 assert (s
->_flags2
& _IO_FLAGS2_FORTIFY
);
1898 __libc_fatal ("*** invalid %N$ use detected ***\n");
1901 /* Now walk through all format specifiers and process them. */
1902 for (; (size_t) nspecs_done
< nspecs
; ++nspecs_done
)
1909 unsigned long long int longlong
;
1910 unsigned long int word
;
1913 union printf_arg the_arg
;
1914 CHAR_T
*string
; /* Pointer to argument string. */
1916 /* Fill variables from values in struct. */
1917 int alt
= specs
[nspecs_done
].info
.alt
;
1918 int space
= specs
[nspecs_done
].info
.space
;
1919 int left
= specs
[nspecs_done
].info
.left
;
1920 int showsign
= specs
[nspecs_done
].info
.showsign
;
1921 int group
= specs
[nspecs_done
].info
.group
;
1922 int is_long_double
= specs
[nspecs_done
].info
.is_long_double
;
1923 int is_short
= specs
[nspecs_done
].info
.is_short
;
1924 int is_char
= specs
[nspecs_done
].info
.is_char
;
1925 int is_long
= specs
[nspecs_done
].info
.is_long
;
1926 int width
= specs
[nspecs_done
].info
.width
;
1927 int prec
= specs
[nspecs_done
].info
.prec
;
1928 int use_outdigits
= specs
[nspecs_done
].info
.i18n
;
1929 char pad
= specs
[nspecs_done
].info
.pad
;
1930 CHAR_T spec
= specs
[nspecs_done
].info
.spec
;
1933 CHAR_T
*workend
= work_buffer
+ WORK_BUFFER_SIZE
;
1935 /* Fill in last information. */
1936 if (specs
[nspecs_done
].width_arg
!= -1)
1938 /* Extract the field width from an argument. */
1939 specs
[nspecs_done
].info
.width
=
1940 args_value
[specs
[nspecs_done
].width_arg
].pa_int
;
1942 if (specs
[nspecs_done
].info
.width
< 0)
1943 /* If the width value is negative left justification is
1944 selected and the value is taken as being positive. */
1946 specs
[nspecs_done
].info
.width
*= -1;
1947 left
= specs
[nspecs_done
].info
.left
= 1;
1949 width
= specs
[nspecs_done
].info
.width
;
1952 if (specs
[nspecs_done
].prec_arg
!= -1)
1954 /* Extract the precision from an argument. */
1955 specs
[nspecs_done
].info
.prec
=
1956 args_value
[specs
[nspecs_done
].prec_arg
].pa_int
;
1958 if (specs
[nspecs_done
].info
.prec
< 0)
1959 /* If the precision is negative the precision is
1961 specs
[nspecs_done
].info
.prec
= -1;
1963 prec
= specs
[nspecs_done
].info
.prec
;
1966 /* Maybe the buffer is too small. */
1967 if (MAX (prec
, width
) + 32 > WORK_BUFFER_SIZE
)
1969 if (__libc_use_alloca ((MAX (prec
, width
) + 32)
1971 workend
= ((CHAR_T
*) alloca ((MAX (prec
, width
) + 32)
1973 + (MAX (prec
, width
) + 32));
1976 workstart
= (CHAR_T
*) malloc ((MAX (prec
, width
) + 32)
1978 if (workstart
== NULL
)
1983 workend
= workstart
+ (MAX (prec
, width
) + 32);
1987 /* Process format specifiers. */
1990 extern printf_function
**__printf_function_table
;
1993 if (spec
<= UCHAR_MAX
1994 && __printf_function_table
!= NULL
1995 && __printf_function_table
[(size_t) spec
] != NULL
)
1997 const void **ptr
= alloca (specs
[nspecs_done
].ndata_args
1998 * sizeof (const void *));
2000 /* Fill in an array of pointers to the argument values. */
2001 for (unsigned int i
= 0; i
< specs
[nspecs_done
].ndata_args
;
2003 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
2005 /* Call the function. */
2006 function_done
= __printf_function_table
[(size_t) spec
]
2007 (s
, &specs
[nspecs_done
].info
, ptr
);
2009 if (function_done
!= -2)
2011 /* If an error occurred we don't have information
2012 about # of chars. */
2013 if (function_done
< 0)
2015 /* Function has set errno. */
2020 done_add (function_done
);
2025 JUMP (spec
, step4_jumps
);
2027 process_arg ((&specs
[nspecs_done
]));
2028 process_string_arg ((&specs
[nspecs_done
]));
2030 LABEL (form_unknown
):
2035 ptr
= alloca (specs
[nspecs_done
].ndata_args
2036 * sizeof (const void *));
2038 /* Fill in an array of pointers to the argument values. */
2039 for (i
= 0; i
< specs
[nspecs_done
].ndata_args
; ++i
)
2040 ptr
[i
] = &args_value
[specs
[nspecs_done
].data_arg
+ i
];
2042 /* Call the function. */
2043 function_done
= printf_unknown (s
, &specs
[nspecs_done
].info
,
2046 /* If an error occurred we don't have information about #
2048 if (function_done
< 0)
2050 /* Function has set errno. */
2055 done_add (function_done
);
2060 if (__glibc_unlikely (workstart
!= NULL
))
2064 /* Write the following constant string. */
2065 outstring (specs
[nspecs_done
].end_of_fmt
,
2066 specs
[nspecs_done
].next_fmt
2067 - specs
[nspecs_done
].end_of_fmt
);
2070 if (__glibc_unlikely (args_malloced
!= NULL
))
2071 free (args_malloced
);
2072 if (__glibc_unlikely (workstart
!= NULL
))
2074 scratch_buffer_free (&specsbuf
);
2078 /* Handle an unknown format specifier. This prints out a canonicalized
2079 representation of the format spec itself. */
2081 printf_unknown (FILE *s
, const struct printf_info
*info
,
2082 const void *const *args
)
2086 CHAR_T work_buffer
[MAX (sizeof (info
->width
), sizeof (info
->prec
)) * 3];
2087 CHAR_T
*const workend
2088 = &work_buffer
[sizeof (work_buffer
) / sizeof (CHAR_T
)];
2099 else if (info
->space
)
2103 if (info
->pad
== L_('0'))
2108 if (info
->width
!= 0)
2110 w
= _itoa_word (info
->width
, workend
, 10, 0);
2115 if (info
->prec
!= -1)
2118 w
= _itoa_word (info
->prec
, workend
, 10, 0);
2123 if (info
->spec
!= L_('\0'))
2124 outchar (info
->spec
);
2130 /* Group the digits according to the grouping rules of the current locale.
2131 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
2134 group_number (CHAR_T
*w
, CHAR_T
*rear_ptr
, const char *grouping
,
2135 THOUSANDS_SEP_T thousands_sep
)
2139 #ifndef COMPILE_WPRINTF
2140 int tlen
= strlen (thousands_sep
);
2143 /* We treat all negative values like CHAR_MAX. */
2145 if (*grouping
== CHAR_MAX
|| *grouping
<= 0)
2146 /* No grouping should be done. */
2151 /* Copy existing string so that nothing gets overwritten. */
2152 src
= (CHAR_T
*) alloca ((rear_ptr
- w
) * sizeof (CHAR_T
));
2153 s
= (CHAR_T
*) __mempcpy (src
, w
,
2154 (rear_ptr
- w
) * sizeof (CHAR_T
));
2157 /* Process all characters in the string. */
2162 if (--len
== 0 && s
> src
)
2164 /* A new group begins. */
2165 #ifdef COMPILE_WPRINTF
2166 *--w
= thousands_sep
;
2170 *--w
= thousands_sep
[--cnt
];
2174 if (*grouping
== CHAR_MAX
2180 /* No further grouping to be done.
2181 Copy the rest of the number. */
2187 else if (*grouping
!= '\0')
2188 /* The previous grouping repeats ad infinitum. */
2197 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2200 struct _IO_FILE_plus _f
;
2201 #ifdef COMPILE_WPRINTF
2202 struct _IO_wide_data _wide_data
;
2204 _IO_FILE
*_put_stream
;
2205 #ifdef _IO_MTSAFE_IO
2211 _IO_helper_overflow (_IO_FILE
*s
, int c
)
2213 _IO_FILE
*target
= ((struct helper_file
*) s
)->_put_stream
;
2214 #ifdef COMPILE_WPRINTF
2215 int used
= s
->_wide_data
->_IO_write_ptr
- s
->_wide_data
->_IO_write_base
;
2218 _IO_size_t written
= _IO_sputn (target
, s
->_wide_data
->_IO_write_base
,
2220 if (written
== 0 || written
== WEOF
)
2222 __wmemmove (s
->_wide_data
->_IO_write_base
,
2223 s
->_wide_data
->_IO_write_base
+ written
,
2225 s
->_wide_data
->_IO_write_ptr
-= written
;
2228 int used
= s
->_IO_write_ptr
- s
->_IO_write_base
;
2231 _IO_size_t written
= _IO_sputn (target
, s
->_IO_write_base
, used
);
2232 if (written
== 0 || written
== EOF
)
2234 memmove (s
->_IO_write_base
, s
->_IO_write_base
+ written
,
2236 s
->_IO_write_ptr
-= written
;
2242 #ifdef COMPILE_WPRINTF
2243 static const struct _IO_jump_t _IO_helper_jumps libio_vtable
=
2246 JUMP_INIT (finish
, _IO_wdefault_finish
),
2247 JUMP_INIT (overflow
, _IO_helper_overflow
),
2248 JUMP_INIT (underflow
, _IO_default_underflow
),
2249 JUMP_INIT (uflow
, _IO_default_uflow
),
2250 JUMP_INIT (pbackfail
, (_IO_pbackfail_t
) _IO_wdefault_pbackfail
),
2251 JUMP_INIT (xsputn
, _IO_wdefault_xsputn
),
2252 JUMP_INIT (xsgetn
, _IO_wdefault_xsgetn
),
2253 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2254 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2255 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2256 JUMP_INIT (sync
, _IO_default_sync
),
2257 JUMP_INIT (doallocate
, _IO_wdefault_doallocate
),
2258 JUMP_INIT (read
, _IO_default_read
),
2259 JUMP_INIT (write
, _IO_default_write
),
2260 JUMP_INIT (seek
, _IO_default_seek
),
2261 JUMP_INIT (close
, _IO_default_close
),
2262 JUMP_INIT (stat
, _IO_default_stat
)
2265 static const struct _IO_jump_t _IO_helper_jumps libio_vtable
=
2268 JUMP_INIT (finish
, _IO_default_finish
),
2269 JUMP_INIT (overflow
, _IO_helper_overflow
),
2270 JUMP_INIT (underflow
, _IO_default_underflow
),
2271 JUMP_INIT (uflow
, _IO_default_uflow
),
2272 JUMP_INIT (pbackfail
, _IO_default_pbackfail
),
2273 JUMP_INIT (xsputn
, _IO_default_xsputn
),
2274 JUMP_INIT (xsgetn
, _IO_default_xsgetn
),
2275 JUMP_INIT (seekoff
, _IO_default_seekoff
),
2276 JUMP_INIT (seekpos
, _IO_default_seekpos
),
2277 JUMP_INIT (setbuf
, _IO_default_setbuf
),
2278 JUMP_INIT (sync
, _IO_default_sync
),
2279 JUMP_INIT (doallocate
, _IO_default_doallocate
),
2280 JUMP_INIT (read
, _IO_default_read
),
2281 JUMP_INIT (write
, _IO_default_write
),
2282 JUMP_INIT (seek
, _IO_default_seek
),
2283 JUMP_INIT (close
, _IO_default_close
),
2284 JUMP_INIT (stat
, _IO_default_stat
)
2290 buffered_vfprintf (_IO_FILE
*s
, const CHAR_T
*format
,
2293 CHAR_T buf
[_IO_BUFSIZ
];
2294 struct helper_file helper
;
2295 _IO_FILE
*hp
= (_IO_FILE
*) &helper
._f
;
2296 int result
, to_flush
;
2298 /* Orient the stream. */
2303 /* Initialize helper. */
2304 helper
._put_stream
= s
;
2305 #ifdef COMPILE_WPRINTF
2306 hp
->_wide_data
= &helper
._wide_data
;
2307 _IO_wsetp (hp
, buf
, buf
+ sizeof buf
/ sizeof (CHAR_T
));
2310 _IO_setp (hp
, buf
, buf
+ sizeof buf
);
2313 hp
->_IO_file_flags
= _IO_MAGIC
|_IO_NO_READS
|_IO_USER_LOCK
;
2314 #if _IO_JUMPS_OFFSET
2315 hp
->_vtable_offset
= 0;
2317 #ifdef _IO_MTSAFE_IO
2320 hp
->_flags2
= s
->_flags2
;
2321 _IO_JUMPS (&helper
._f
) = (struct _IO_jump_t
*) &_IO_helper_jumps
;
2323 /* Now print to helper instead. */
2324 #ifndef COMPILE_WPRINTF
2325 result
= _IO_vfprintf (hp
, format
, args
);
2327 result
= vfprintf (hp
, format
, args
);
2331 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile
, s
);
2334 /* Now flush anything from the helper to the S. */
2335 #ifdef COMPILE_WPRINTF
2336 if ((to_flush
= (hp
->_wide_data
->_IO_write_ptr
2337 - hp
->_wide_data
->_IO_write_base
)) > 0)
2339 if ((int) _IO_sputn (s
, hp
->_wide_data
->_IO_write_base
, to_flush
)
2344 if ((to_flush
= hp
->_IO_write_ptr
- hp
->_IO_write_base
) > 0)
2346 if ((int) _IO_sputn (s
, hp
->_IO_write_base
, to_flush
) != to_flush
)
2351 /* Unlock the stream. */
2352 _IO_funlockfile (s
);
2353 __libc_cleanup_region_end (0);
2359 #ifdef COMPILE_WPRINTF
2360 strong_alias (_IO_vfwprintf
, __vfwprintf
);
2361 ldbl_weak_alias (_IO_vfwprintf
, vfwprintf
);
2363 ldbl_strong_alias (_IO_vfprintf_internal
, vfprintf
);
2364 ldbl_hidden_def (_IO_vfprintf_internal
, vfprintf
)
2365 ldbl_strong_alias (_IO_vfprintf_internal
, _IO_vfprintf
);
2366 ldbl_hidden_def (_IO_vfprintf_internal
, _IO_vfprintf
)