1
/* Copyright (C) 1991-2012 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>
32
#include <gnu/option-groups.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 (__builtin_expect (INT_MAX - done < _val, 0)) \
74
__set_errno (EOVERFLOW); \
80
#ifndef COMPILE_WPRINTF
81
# define vfprintf _IO_vfprintf_internal
83
# define UCHAR_T unsigned char
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) \
92
done_add (_IO_padn (s, (Padchar), width))
93
# define PUTC(C, F) _IO_putc_unlocked (C, F)
94
# define ORIENT if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
97
# define vfprintf _IO_vfwprintf
98
# define CHAR_T wchar_t
99
/* This is a hack!!! There should be a type uwchar_t. */
100
# define UCHAR_T unsigned int /* uwchar_t */
101
# define INT_T wint_t
102
# define L_(Str) L##Str
103
# define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
104
# define STR_LEN(Str) __wcslen (Str)
108
# define PUT(F, S, N) _IO_sputn ((F), (S), (N))
109
# define PAD(Padchar) \
111
done_add (_IO_wpadn (s, (Padchar), width))
112
# define PUTC(C, F) _IO_putwc_unlocked (C, F)
113
# define ORIENT if (_IO_fwide (s, 1) != 1) return -1
116
# define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
117
# define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
122
#if __OPTION_POSIX_C_LANG_WIDE_CHAR
123
# define MULTIBYTE_SUPPORT (1)
125
# define MULTIBYTE_SUPPORT (0)
128
#if __OPTION_EGLIBC_LOCALE_CODE
129
# define LOCALE_SUPPORT (1)
131
# define LOCALE_SUPPORT (0)
134
#include "_i18n_number.h"
136
/* Include the shared code for parsing the format string. */
137
#include "printf-parse.h"
140
#define outchar(Ch) \
143
register const INT_T outc = (Ch); \
144
if (PUTC (outc, s) == EOF || done == INT_MAX) \
153
#define outstring(String, Len) \
156
assert ((size_t) done <= (size_t) INT_MAX); \
157
if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
162
if (__builtin_expect (INT_MAX - done < (Len), 0)) \
165
__set_errno (EOVERFLOW); \
172
/* For handling long_double and longlong we use the same flag. If
173
`long' and `long long' are effectively the same type define it to
175
#if LONG_MAX == LONG_LONG_MAX
176
# define is_longlong 0
178
# define is_longlong is_long_double
181
/* If `long' and `int' is effectively the same type we don't have to
182
handle `long separately. */
183
#if INT_MAX == LONG_MAX
184
# define is_long_num 0
186
# define is_long_num is_long
190
/* Global variables. */
191
static const CHAR_T null[] = L_("(null)");
194
/* Helper function to provide temporary buffering for unbuffered streams. */
195
static int buffered_vfprintf (FILE *stream, const CHAR_T *fmt, va_list)
196
__THROW __attribute__ ((noinline)) internal_function;
198
/* Handle unknown format specifier. */
199
static int printf_unknown (FILE *, const struct printf_info *,
200
const void *const *) __THROW;
202
/* Group digits of number string. */
203
#ifdef COMPILE_WPRINTF
204
static CHAR_T *group_number (CHAR_T *, CHAR_T *, const char *, wchar_t)
205
__THROW internal_function;
207
static CHAR_T *group_number (CHAR_T *, CHAR_T *, const char *, const char *)
208
__THROW internal_function;
212
/* The function itself. */
214
vfprintf (FILE *s, const CHAR_T *format, va_list ap)
216
/* The character used as thousands separator. */
217
#ifdef COMPILE_WPRINTF
218
wchar_t thousands_sep = L'\0';
220
const char *thousands_sep = NULL;
223
/* The string describing the size of groups of digits. */
224
const char *grouping;
226
/* Place to accumulate the result. */
229
/* Current character in format string. */
232
/* End of leading constant string. */
233
const UCHAR_T *lead_str_end;
235
/* Points to next format specifier. */
236
const UCHAR_T *end_of_spec;
238
/* Buffer intermediate results. */
239
CHAR_T work_buffer[1000];
240
CHAR_T *workstart = NULL;
243
/* We have to save the original argument pointer. */
246
/* Count number of specifiers we already processed. */
249
/* For the %m format we may need the current `errno' value. */
250
int save_errno = errno;
252
/* 1 if format is in read-only memory, -1 if it is in writable memory,
254
int readonly_format = 0;
256
/* For the argument descriptions, which may be allocated on the heap. */
257
void *args_malloced = NULL;
259
/* This table maps a character into a number representing a
260
class. In each step there is a destination label for each
262
static const uint8_t jump_table[] =
264
/* ' ' */ 1, 0, 0, /* '#' */ 4,
265
0, /* '%' */ 14, 0, /* '\''*/ 6,
266
0, 0, /* '*' */ 7, /* '+' */ 2,
267
0, /* '-' */ 3, /* '.' */ 9, 0,
268
/* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
269
/* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
270
/* '8' */ 8, /* '9' */ 8, 0, 0,
272
0, /* 'A' */ 26, 0, /* 'C' */ 25,
273
0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
274
0, /* 'I' */ 29, 0, 0,
275
/* 'L' */ 12, 0, 0, 0,
276
0, 0, 0, /* 'S' */ 21,
278
/* 'X' */ 18, 0, /* 'Z' */ 13, 0,
280
0, /* 'a' */ 26, 0, /* 'c' */ 20,
281
/* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
282
/* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
283
/* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
284
/* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
285
/* 't' */ 27, /* 'u' */ 16, 0, 0,
286
/* 'x' */ 18, 0, /* 'z' */ 13
289
#define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
290
#define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
292
/* 'int' is enough and it saves some space on 64 bit systems. */
293
# define JUMP_TABLE_TYPE const int
294
# define JUMP(ChExpr, table) \
298
void *__unbounded ptr; \
300
offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
301
: table[CHAR_CLASS (spec)]; \
302
ptr = &&do_form_unknown + offset; \
307
# define JUMP_TABLE_TYPE const void *const
308
# define JUMP(ChExpr, table) \
311
const void *__unbounded ptr; \
313
ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
314
: table[CHAR_CLASS (spec)]; \
320
#define STEP0_3_TABLE \
321
/* Step 0: at the beginning. */ \
322
static JUMP_TABLE_TYPE step0_jumps[30] = \
324
REF (form_unknown), \
325
REF (flag_space), /* for ' ' */ \
326
REF (flag_plus), /* for '+' */ \
327
REF (flag_minus), /* for '-' */ \
328
REF (flag_hash), /* for '<hash>' */ \
329
REF (flag_zero), /* for '0' */ \
330
REF (flag_quote), /* for '\'' */ \
331
REF (width_asterics), /* for '*' */ \
332
REF (width), /* for '1'...'9' */ \
333
REF (precision), /* for '.' */ \
334
REF (mod_half), /* for 'h' */ \
335
REF (mod_long), /* for 'l' */ \
336
REF (mod_longlong), /* for 'L', 'q' */ \
337
REF (mod_size_t), /* for 'z', 'Z' */ \
338
REF (form_percent), /* for '%' */ \
339
REF (form_integer), /* for 'd', 'i' */ \
340
REF (form_unsigned), /* for 'u' */ \
341
REF (form_octal), /* for 'o' */ \
342
REF (form_hexa), /* for 'X', 'x' */ \
343
REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
344
REF (form_character), /* for 'c' */ \
345
REF (form_string), /* for 's', 'S' */ \
346
REF (form_pointer), /* for 'p' */ \
347
REF (form_number), /* for 'n' */ \
348
REF (form_strerror), /* for 'm' */ \
349
REF (form_wcharacter), /* for 'C' */ \
350
REF (form_floathex), /* for 'A', 'a' */ \
351
REF (mod_ptrdiff_t), /* for 't' */ \
352
REF (mod_intmax_t), /* for 'j' */ \
353
REF (flag_i18n), /* for 'I' */ \
355
/* Step 1: after processing width. */ \
356
static JUMP_TABLE_TYPE step1_jumps[30] = \
358
REF (form_unknown), \
359
REF (form_unknown), /* for ' ' */ \
360
REF (form_unknown), /* for '+' */ \
361
REF (form_unknown), /* for '-' */ \
362
REF (form_unknown), /* for '<hash>' */ \
363
REF (form_unknown), /* for '0' */ \
364
REF (form_unknown), /* for '\'' */ \
365
REF (form_unknown), /* for '*' */ \
366
REF (form_unknown), /* for '1'...'9' */ \
367
REF (precision), /* for '.' */ \
368
REF (mod_half), /* for 'h' */ \
369
REF (mod_long), /* for 'l' */ \
370
REF (mod_longlong), /* for 'L', 'q' */ \
371
REF (mod_size_t), /* for 'z', 'Z' */ \
372
REF (form_percent), /* for '%' */ \
373
REF (form_integer), /* for 'd', 'i' */ \
374
REF (form_unsigned), /* for 'u' */ \
375
REF (form_octal), /* for 'o' */ \
376
REF (form_hexa), /* for 'X', 'x' */ \
377
REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
378
REF (form_character), /* for 'c' */ \
379
REF (form_string), /* for 's', 'S' */ \
380
REF (form_pointer), /* for 'p' */ \
381
REF (form_number), /* for 'n' */ \
382
REF (form_strerror), /* for 'm' */ \
383
REF (form_wcharacter), /* for 'C' */ \
384
REF (form_floathex), /* for 'A', 'a' */ \
385
REF (mod_ptrdiff_t), /* for 't' */ \
386
REF (mod_intmax_t), /* for 'j' */ \
387
REF (form_unknown) /* for 'I' */ \
389
/* Step 2: after processing precision. */ \
390
static JUMP_TABLE_TYPE step2_jumps[30] = \
392
REF (form_unknown), \
393
REF (form_unknown), /* for ' ' */ \
394
REF (form_unknown), /* for '+' */ \
395
REF (form_unknown), /* for '-' */ \
396
REF (form_unknown), /* for '<hash>' */ \
397
REF (form_unknown), /* for '0' */ \
398
REF (form_unknown), /* for '\'' */ \
399
REF (form_unknown), /* for '*' */ \
400
REF (form_unknown), /* for '1'...'9' */ \
401
REF (form_unknown), /* for '.' */ \
402
REF (mod_half), /* for 'h' */ \
403
REF (mod_long), /* for 'l' */ \
404
REF (mod_longlong), /* for 'L', 'q' */ \
405
REF (mod_size_t), /* for 'z', 'Z' */ \
406
REF (form_percent), /* for '%' */ \
407
REF (form_integer), /* for 'd', 'i' */ \
408
REF (form_unsigned), /* for 'u' */ \
409
REF (form_octal), /* for 'o' */ \
410
REF (form_hexa), /* for 'X', 'x' */ \
411
REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
412
REF (form_character), /* for 'c' */ \
413
REF (form_string), /* for 's', 'S' */ \
414
REF (form_pointer), /* for 'p' */ \
415
REF (form_number), /* for 'n' */ \
416
REF (form_strerror), /* for 'm' */ \
417
REF (form_wcharacter), /* for 'C' */ \
418
REF (form_floathex), /* for 'A', 'a' */ \
419
REF (mod_ptrdiff_t), /* for 't' */ \
420
REF (mod_intmax_t), /* for 'j' */ \
421
REF (form_unknown) /* for 'I' */ \
423
/* Step 3a: after processing first 'h' modifier. */ \
424
static JUMP_TABLE_TYPE step3a_jumps[30] = \
426
REF (form_unknown), \
427
REF (form_unknown), /* for ' ' */ \
428
REF (form_unknown), /* for '+' */ \
429
REF (form_unknown), /* for '-' */ \
430
REF (form_unknown), /* for '<hash>' */ \
431
REF (form_unknown), /* for '0' */ \
432
REF (form_unknown), /* for '\'' */ \
433
REF (form_unknown), /* for '*' */ \
434
REF (form_unknown), /* for '1'...'9' */ \
435
REF (form_unknown), /* for '.' */ \
436
REF (mod_halfhalf), /* for 'h' */ \
437
REF (form_unknown), /* for 'l' */ \
438
REF (form_unknown), /* for 'L', 'q' */ \
439
REF (form_unknown), /* for 'z', 'Z' */ \
440
REF (form_percent), /* for '%' */ \
441
REF (form_integer), /* for 'd', 'i' */ \
442
REF (form_unsigned), /* for 'u' */ \
443
REF (form_octal), /* for 'o' */ \
444
REF (form_hexa), /* for 'X', 'x' */ \
445
REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
446
REF (form_unknown), /* for 'c' */ \
447
REF (form_unknown), /* for 's', 'S' */ \
448
REF (form_unknown), /* for 'p' */ \
449
REF (form_number), /* for 'n' */ \
450
REF (form_unknown), /* for 'm' */ \
451
REF (form_unknown), /* for 'C' */ \
452
REF (form_unknown), /* for 'A', 'a' */ \
453
REF (form_unknown), /* for 't' */ \
454
REF (form_unknown), /* for 'j' */ \
455
REF (form_unknown) /* for 'I' */ \
457
/* Step 3b: after processing first 'l' modifier. */ \
458
static JUMP_TABLE_TYPE step3b_jumps[30] = \
460
REF (form_unknown), \
461
REF (form_unknown), /* for ' ' */ \
462
REF (form_unknown), /* for '+' */ \
463
REF (form_unknown), /* for '-' */ \
464
REF (form_unknown), /* for '<hash>' */ \
465
REF (form_unknown), /* for '0' */ \
466
REF (form_unknown), /* for '\'' */ \
467
REF (form_unknown), /* for '*' */ \
468
REF (form_unknown), /* for '1'...'9' */ \
469
REF (form_unknown), /* for '.' */ \
470
REF (form_unknown), /* for 'h' */ \
471
REF (mod_longlong), /* for 'l' */ \
472
REF (form_unknown), /* for 'L', 'q' */ \
473
REF (form_unknown), /* for 'z', 'Z' */ \
474
REF (form_percent), /* for '%' */ \
475
REF (form_integer), /* for 'd', 'i' */ \
476
REF (form_unsigned), /* for 'u' */ \
477
REF (form_octal), /* for 'o' */ \
478
REF (form_hexa), /* for 'X', 'x' */ \
479
REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
480
REF (form_character), /* for 'c' */ \
481
REF (form_string), /* for 's', 'S' */ \
482
REF (form_pointer), /* for 'p' */ \
483
REF (form_number), /* for 'n' */ \
484
REF (form_strerror), /* for 'm' */ \
485
REF (form_wcharacter), /* for 'C' */ \
486
REF (form_floathex), /* for 'A', 'a' */ \
487
REF (form_unknown), /* for 't' */ \
488
REF (form_unknown), /* for 'j' */ \
489
REF (form_unknown) /* for 'I' */ \
492
#define STEP4_TABLE \
493
/* Step 4: processing format specifier. */ \
494
static JUMP_TABLE_TYPE step4_jumps[30] = \
496
REF (form_unknown), \
497
REF (form_unknown), /* for ' ' */ \
498
REF (form_unknown), /* for '+' */ \
499
REF (form_unknown), /* for '-' */ \
500
REF (form_unknown), /* for '<hash>' */ \
501
REF (form_unknown), /* for '0' */ \
502
REF (form_unknown), /* for '\'' */ \
503
REF (form_unknown), /* for '*' */ \
504
REF (form_unknown), /* for '1'...'9' */ \
505
REF (form_unknown), /* for '.' */ \
506
REF (form_unknown), /* for 'h' */ \
507
REF (form_unknown), /* for 'l' */ \
508
REF (form_unknown), /* for 'L', 'q' */ \
509
REF (form_unknown), /* for 'z', 'Z' */ \
510
REF (form_percent), /* for '%' */ \
511
REF (form_integer), /* for 'd', 'i' */ \
512
REF (form_unsigned), /* for 'u' */ \
513
REF (form_octal), /* for 'o' */ \
514
REF (form_hexa), /* for 'X', 'x' */ \
515
REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
516
REF (form_character), /* for 'c' */ \
517
REF (form_string), /* for 's', 'S' */ \
518
REF (form_pointer), /* for 'p' */ \
519
REF (form_number), /* for 'n' */ \
520
REF (form_strerror), /* for 'm' */ \
521
REF (form_wcharacter), /* for 'C' */ \
522
REF (form_floathex), /* for 'A', 'a' */ \
523
REF (form_unknown), /* for 't' */ \
524
REF (form_unknown), /* for 'j' */ \
525
REF (form_unknown) /* for 'I' */ \
529
#define process_arg(fspec) \
530
/* Start real work. We know about all flags and modifiers and \
531
now process the wanted format specifier. */ \
532
LABEL (form_percent): \
533
/* Write a literal "%". */ \
537
LABEL (form_integer): \
538
/* Signed decimal integer. */ \
543
long long int signed_number; \
546
signed_number = va_arg (ap, long long int); \
548
signed_number = args_value[fspec->data_arg].pa_long_long_int; \
550
is_negative = signed_number < 0; \
551
number.longlong = is_negative ? (- signed_number) : signed_number; \
553
goto LABEL (longlong_number); \
557
long int signed_number; \
562
signed_number = va_arg (ap, long int); \
564
signed_number = (signed char) va_arg (ap, unsigned int); \
565
else if (!is_short) \
566
signed_number = va_arg (ap, int); \
568
signed_number = (short int) va_arg (ap, unsigned int); \
572
signed_number = args_value[fspec->data_arg].pa_long_int; \
574
signed_number = (signed char) \
575
args_value[fspec->data_arg].pa_u_int; \
576
else if (!is_short) \
577
signed_number = args_value[fspec->data_arg].pa_int; \
579
signed_number = (short int) \
580
args_value[fspec->data_arg].pa_u_int; \
582
is_negative = signed_number < 0; \
583
number.word = is_negative ? (- signed_number) : signed_number; \
585
goto LABEL (number); \
589
LABEL (form_unsigned): \
590
/* Unsigned decimal integer. */ \
592
goto LABEL (unsigned_number); \
595
LABEL (form_octal): \
596
/* Unsigned octal integer. */ \
598
goto LABEL (unsigned_number); \
602
/* Unsigned hexadecimal integer. */ \
605
LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
607
/* ISO specifies the `+' and ` ' flags only for signed \
616
number.longlong = va_arg (ap, unsigned long long int); \
618
number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
620
LABEL (longlong_number): \
622
/* Supply a default precision if none was given. */ \
625
/* We have to take care for the '0' flag. If a precision \
626
is given it must be ignored. */ \
629
/* If the precision is 0 and the number is 0 nothing has to \
630
be written for the number, except for the 'o' format in \
632
if (prec == 0 && number.longlong == 0) \
635
if (base == 8 && alt) \
636
*--string = L_('0'); \
640
/* Put the number in WORK. */ \
641
string = _itoa (number.longlong, workend, base, \
643
if (group && grouping) \
644
string = group_number (string, workend, grouping, \
647
if (use_outdigits && base == 10) \
648
string = _i18n_number_rewrite (string, workend, workend); \
650
/* Simplify further test for num != 0. */ \
651
number.word = number.longlong != 0; \
658
number.word = va_arg (ap, unsigned long int); \
660
number.word = (unsigned char) va_arg (ap, unsigned int); \
661
else if (!is_short) \
662
number.word = va_arg (ap, unsigned int); \
664
number.word = (unsigned short int) va_arg (ap, unsigned int); \
668
number.word = args_value[fspec->data_arg].pa_u_long_int; \
670
number.word = (unsigned char) \
671
args_value[fspec->data_arg].pa_u_int; \
672
else if (!is_short) \
673
number.word = args_value[fspec->data_arg].pa_u_int; \
675
number.word = (unsigned short int) \
676
args_value[fspec->data_arg].pa_u_int; \
680
/* Supply a default precision if none was given. */ \
683
/* We have to take care for the '0' flag. If a precision \
684
is given it must be ignored. */ \
687
/* If the precision is 0 and the number is 0 nothing has to \
688
be written for the number, except for the 'o' format in \
690
if (prec == 0 && number.word == 0) \
693
if (base == 8 && alt) \
694
*--string = L_('0'); \
698
/* Put the number in WORK. */ \
699
string = _itoa_word (number.word, workend, base, \
701
if (group && grouping) \
702
string = group_number (string, workend, grouping, \
705
if (use_outdigits && base == 10) \
706
string = _i18n_number_rewrite (string, workend, workend); \
710
if (prec <= workend - string && number.word != 0 && alt && base == 8) \
711
/* Add octal marker. */ \
712
*--string = L_('0'); \
714
prec = MAX (0, prec - (workend - string)); \
718
width -= workend - string + prec; \
720
if (number.word != 0 && alt && base == 16) \
721
/* Account for 0X hex marker. */ \
724
if (is_negative || showsign || space) \
727
if (pad == L_(' ')) \
740
if (number.word != 0 && alt && base == 16) \
749
outstring (string, workend - string); \
771
if (number.word != 0 && alt && base == 16) \
778
width -= workend - string + prec; \
788
outstring (string, workend - string); \
794
LABEL (form_float): \
796
/* Floating-point number. This is handled by printf_fp.c. */ \
803
is_long_double = 0; \
805
struct printf_info info = { .prec = prec, \
808
.is_long_double = is_long_double, \
809
.is_short = is_short, \
810
.is_long = is_long, \
814
.showsign = showsign, \
818
.i18n = use_outdigits, \
819
.wide = sizeof (CHAR_T) != 1 }; \
821
if (is_long_double) \
822
the_arg.pa_long_double = va_arg (ap, long double); \
824
the_arg.pa_double = va_arg (ap, double); \
825
ptr = (const void *) &the_arg; \
827
function_done = __printf_fp (s, &info, &ptr); \
831
ptr = (const void *) &args_value[fspec->data_arg]; \
834
fspec->data_arg_type = PA_DOUBLE; \
835
fspec->info.is_long_double = 0; \
838
function_done = __printf_fp (s, &fspec->info, &ptr); \
841
if (function_done < 0) \
843
/* Error in print handler; up to handler to set errno. */ \
848
done_add (function_done); \
852
LABEL (form_floathex): \
854
/* Floating point number printed as hexadecimal number. */ \
861
is_long_double = 0; \
863
struct printf_info info = { .prec = prec, \
866
.is_long_double = is_long_double, \
867
.is_short = is_short, \
868
.is_long = is_long, \
872
.showsign = showsign, \
876
.wide = sizeof (CHAR_T) != 1 }; \
878
if (is_long_double) \
879
the_arg.pa_long_double = va_arg (ap, long double); \
881
the_arg.pa_double = va_arg (ap, double); \
882
ptr = (const void *) &the_arg; \
884
function_done = __printf_fphex (s, &info, &ptr); \
888
ptr = (const void *) &args_value[fspec->data_arg]; \
890
fspec->info.is_long_double = 0; \
892
function_done = __printf_fphex (s, &fspec->info, &ptr); \
895
if (function_done < 0) \
897
/* Error in print handler; up to handler to set errno. */ \
902
done_add (function_done); \
906
LABEL (form_pointer): \
907
/* Generic pointer. */ \
911
ptr = va_arg (ap, void *); \
913
ptr = args_value[fspec->data_arg].pa_pointer; \
916
/* If the pointer is not NULL, write it as a %#x spec. */ \
918
number.word = (unsigned long int) ptr; \
923
goto LABEL (number); \
927
/* Write "(nil)" for a nil pointer. */ \
928
string = (CHAR_T *) L_("(nil)"); \
929
/* Make sure the full string "(nil)" is printed. */ \
932
is_long = 0; /* This is no wide-char string. */ \
933
goto LABEL (print_string); \
938
LABEL (form_number): \
939
if (s->_flags2 & _IO_FLAGS2_FORTIFY) \
941
if (! readonly_format) \
943
extern int __readonly_area (const void *, size_t) \
946
= __readonly_area (format, ((STR_LEN (format) + 1) \
947
* sizeof (CHAR_T))); \
949
if (readonly_format < 0) \
950
__libc_fatal ("*** %n in writable segment detected ***\n"); \
952
/* Answer the count of characters written. */ \
956
*(long long int *) va_arg (ap, void *) = done; \
957
else if (is_long_num) \
958
*(long int *) va_arg (ap, void *) = done; \
960
*(char *) va_arg (ap, void *) = done; \
961
else if (!is_short) \
962
*(int *) va_arg (ap, void *) = done; \
964
*(short int *) va_arg (ap, void *) = done; \
968
*(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
969
else if (is_long_num) \
970
*(long int *) args_value[fspec->data_arg].pa_pointer = done; \
972
*(char *) args_value[fspec->data_arg].pa_pointer = done; \
973
else if (!is_short) \
974
*(int *) args_value[fspec->data_arg].pa_pointer = done; \
976
*(short int *) args_value[fspec->data_arg].pa_pointer = done; \
979
LABEL (form_strerror): \
980
/* Print description of error ERRNO. */ \
982
(CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
983
sizeof work_buffer); \
984
is_long = 0; /* This is no wide-char string. */ \
985
goto LABEL (print_string)
987
#ifdef COMPILE_WPRINTF
988
# define process_string_arg(fspec) \
989
LABEL (form_character): \
992
goto LABEL (form_wcharacter); \
993
--width; /* Account for the character itself. */ \
997
outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
999
outchar (__btowc ((unsigned char) \
1000
args_value[fspec->data_arg].pa_int)); \
1005
LABEL (form_wcharacter): \
1007
/* Wide character. */ \
1011
if (fspec == NULL) \
1012
outchar (va_arg (ap, wchar_t)); \
1014
outchar (args_value[fspec->data_arg].pa_wchar); \
1020
LABEL (form_string): \
1023
int string_malloced; \
1025
/* The string argument could in fact be `char *' or `wchar_t *'. \
1026
But this should not make a difference here. */ \
1027
if (fspec == NULL) \
1028
string = (CHAR_T *) va_arg (ap, const wchar_t *); \
1030
string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
1032
/* Entry point for printing other strings. */ \
1033
LABEL (print_string): \
1035
string_malloced = 0; \
1036
if (string == NULL) \
1038
/* Write "(null)" if there's space. */ \
1040
|| prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
1042
string = (CHAR_T *) null; \
1043
len = (sizeof (null) / sizeof (null[0])) - 1; \
1047
string = (CHAR_T *) L""; \
1051
else if (!is_long && spec != L_('S')) \
1053
/* This is complicated. We have to transform the multibyte \
1054
string into a wide character string. */ \
1055
const char *mbs = (const char *) string; \
1056
mbstate_t mbstate; \
1058
len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
1060
/* Allocate dynamically an array which definitely is long \
1061
enough for the wide character version. Each byte in the \
1062
multi-byte string can produce at most one wide character. */ \
1063
if (__libc_use_alloca (len * sizeof (wchar_t))) \
1064
string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1065
else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1072
string_malloced = 1; \
1074
memset (&mbstate, '\0', sizeof (mbstate_t)); \
1075
len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1076
if (len == (size_t) -1) \
1078
/* Illegal multibyte character. */ \
1086
/* Search for the end of the string, but don't search past \
1087
the length specified by the precision. */ \
1088
len = __wcsnlen (string, prec); \
1090
len = __wcslen (string); \
1093
if ((width -= len) < 0) \
1095
outstring (string, len); \
1101
outstring (string, len); \
1104
if (__builtin_expect (string_malloced, 0)) \
1109
# define process_string_arg(fspec) \
1110
LABEL (form_character): \
1114
assert (MULTIBYTE_SUPPORT); \
1115
goto LABEL (form_wcharacter); \
1117
--width; /* Account for the character itself. */ \
1120
if (fspec == NULL) \
1121
outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1123
outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1128
LABEL (form_wcharacter): \
1129
assert (MULTIBYTE_SUPPORT); \
1131
/* Wide character. */ \
1132
char buf[MB_CUR_MAX]; \
1133
mbstate_t mbstate; \
1136
memset (&mbstate, '\0', sizeof (mbstate_t)); \
1137
len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1138
: args_value[fspec->data_arg].pa_wchar), \
1140
if (len == (size_t) -1) \
1142
/* Something went wrong during the conversion. Bail out. */ \
1149
outstring (buf, len); \
1155
LABEL (form_string): \
1158
int string_malloced; \
1160
/* The string argument could in fact be `char *' or `wchar_t *'. \
1161
But this should not make a difference here. */ \
1162
if (fspec == NULL) \
1163
string = (char *) va_arg (ap, const char *); \
1165
string = (char *) args_value[fspec->data_arg].pa_string; \
1167
/* Entry point for printing other strings. */ \
1168
LABEL (print_string): \
1170
string_malloced = 0; \
1171
if (string == NULL) \
1173
/* Write "(null)" if there's space. */ \
1174
if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1176
string = (char *) null; \
1177
len = sizeof (null) - 1; \
1181
string = (char *) ""; \
1185
else if (!is_long && spec != L_('S')) \
1188
/* Search for the end of the string, but don't search past \
1189
the length (in bytes) specified by the precision. */ \
1190
len = __strnlen (string, prec); \
1192
len = strlen (string); \
1196
assert (MULTIBYTE_SUPPORT); \
1197
const wchar_t *s2 = (const wchar_t *) string; \
1198
mbstate_t mbstate; \
1200
memset (&mbstate, '\0', sizeof (mbstate_t)); \
1204
/* The string `s2' might not be NUL terminated. */ \
1205
if (__libc_use_alloca (prec)) \
1206
string = (char *) alloca (prec); \
1207
else if ((string = (char *) malloc (prec)) == NULL) \
1213
string_malloced = 1; \
1214
len = __wcsrtombs (string, &s2, prec, &mbstate); \
1218
len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1219
if (len != (size_t) -1) \
1221
assert (__mbsinit (&mbstate)); \
1222
s2 = (const wchar_t *) string; \
1223
if (__libc_use_alloca (len + 1)) \
1224
string = (char *) alloca (len + 1); \
1225
else if ((string = (char *) malloc (len + 1)) == NULL) \
1231
string_malloced = 1; \
1232
(void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1236
if (len == (size_t) -1) \
1238
/* Illegal wide-character string. */ \
1244
if ((width -= len) < 0) \
1246
outstring (string, len); \
1252
outstring (string, len); \
1255
if (__builtin_expect (string_malloced, 0)) \
1261
/* Orient the stream. */
1266
/* Sanity check of arguments. */
1267
ARGCHECK (s, format);
1270
/* Check for correct orientation. */
1271
if (_IO_vtable_offset (s) == 0 &&
1272
_IO_fwide (s, sizeof (CHAR_T) == 1 ? -1 : 1)
1273
!= (sizeof (CHAR_T) == 1 ? -1 : 1))
1274
/* The stream is already oriented otherwise. */
1278
if (UNBUFFERED_P (s))
1279
/* Use a helper function which will allocate a local temporary buffer
1280
for the stream and then call us again. */
1281
return buffered_vfprintf (s, format, ap);
1283
/* Initialize local variables. */
1285
grouping = (const char *) -1;
1287
/* This macro will be available soon in gcc's <stdarg.h>. We need it
1288
since on some systems `va_list' is not an integral type. */
1289
__va_copy (ap_save, ap);
1295
#ifdef COMPILE_WPRINTF
1296
/* Find the first format specifier. */
1297
f = lead_str_end = __find_specwc ((const UCHAR_T *) format);
1299
/* Find the first format specifier. */
1300
f = lead_str_end = __find_specmb ((const UCHAR_T *) format);
1304
_IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
1307
/* Write the literal text before the first format. */
1308
outstring ((const UCHAR_T *) format,
1309
lead_str_end - (const UCHAR_T *) format);
1311
/* If we only have to print a simple string, return now. */
1315
/* Use the slow path in case any printf handler is registered. */
1316
if (__builtin_expect (__printf_function_table != NULL
1317
|| __printf_modifier_table != NULL
1318
|| __printf_va_arg_table != NULL, 0))
1321
/* Process whole format string. */
1325
# define REF(Name) &&do_##Name - &&do_form_unknown
1327
# define REF(Name) &&do_##Name
1329
#define LABEL(Name) do_##Name
1333
union printf_arg *args_value; /* This is not used here but ... */
1334
int is_negative; /* Flag for negative number. */
1337
unsigned long long int longlong;
1338
unsigned long int word;
1341
union printf_arg the_arg;
1342
CHAR_T *string; /* Pointer to argument string. */
1343
int alt = 0; /* Alternate format. */
1344
int space = 0; /* Use space prefix if no sign is needed. */
1345
int left = 0; /* Left-justify output. */
1346
int showsign = 0; /* Always begin with plus or minus sign. */
1347
int group = 0; /* Print numbers according grouping rules. */
1348
int is_long_double = 0; /* Argument is long double/ long long int. */
1349
int is_short = 0; /* Argument is short int. */
1350
int is_long = 0; /* Argument is long int. */
1351
int is_char = 0; /* Argument is promoted (unsigned) char. */
1352
int width = 0; /* Width of output; 0 means none specified. */
1353
int prec = -1; /* Precision of output; -1 means none specified. */
1354
/* This flag is set by the 'I' modifier and selects the use of the
1355
`outdigits' as determined by the current locale. */
1356
int use_outdigits = 0;
1357
UCHAR_T pad = L_(' ');/* Padding character. */
1361
workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1363
/* Get current character in format string. */
1364
JUMP (*++f, step0_jumps);
1369
JUMP (*++f, step0_jumps);
1374
JUMP (*++f, step0_jumps);
1380
JUMP (*++f, step0_jumps);
1385
JUMP (*++f, step0_jumps);
1391
JUMP (*++f, step0_jumps);
1393
/* The '\'' flag. */
1397
if (! LOCALE_SUPPORT)
1399
else if (grouping == (const char *) -1)
1401
#ifdef COMPILE_WPRINTF
1402
thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1403
_NL_NUMERIC_THOUSANDS_SEP_WC);
1405
thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1408
grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1409
if (*grouping == '\0' || *grouping == CHAR_MAX
1410
#ifdef COMPILE_WPRINTF
1411
|| thousands_sep == L'\0'
1413
|| *thousands_sep == '\0'
1418
JUMP (*++f, step0_jumps);
1422
JUMP (*++f, step0_jumps);
1424
/* Get width from argument. */
1425
LABEL (width_asterics):
1427
const UCHAR_T *tmp; /* Temporary value. */
1432
int pos = read_int (&tmp);
1436
__set_errno (EOVERFLOW);
1441
if (pos && *tmp == L_('$'))
1442
/* The width comes from a positional parameter. */
1445
width = va_arg (ap, int);
1447
/* Negative width means left justified. */
1455
if (__builtin_expect (width >= INT_MAX / sizeof (CHAR_T) - 32, 0))
1457
__set_errno (EOVERFLOW);
1462
if (width >= sizeof (work_buffer) / sizeof (work_buffer[0]) - 32)
1464
/* We have to use a special buffer. The "32" is just a safe
1465
bet for all the output which is not counted in the width. */
1466
size_t needed = ((size_t) width + 32) * sizeof (CHAR_T);
1467
if (__libc_use_alloca (needed))
1468
workend = (CHAR_T *) alloca (needed) + width + 32;
1471
workstart = (CHAR_T *) malloc (needed);
1472
if (workstart == NULL)
1477
workend = workstart + width + 32;
1481
JUMP (*f, step1_jumps);
1483
/* Given width in format string. */
1485
width = read_int (&f);
1487
if (__builtin_expect (width == -1
1488
|| width >= INT_MAX / sizeof (CHAR_T) - 32, 0))
1490
__set_errno (EOVERFLOW);
1495
if (width >= sizeof (work_buffer) / sizeof (work_buffer[0]) - 32)
1497
/* We have to use a special buffer. The "32" is just a safe
1498
bet for all the output which is not counted in the width. */
1499
size_t needed = ((size_t) width + 32) * sizeof (CHAR_T);
1500
if (__libc_use_alloca (needed))
1501
workend = (CHAR_T *) alloca (needed) + width + 32;
1504
workstart = (CHAR_T *) malloc (needed);
1505
if (workstart == NULL)
1510
workend = workstart + width + 32;
1514
/* Oh, oh. The argument comes from a positional parameter. */
1516
JUMP (*f, step1_jumps);
1522
const UCHAR_T *tmp; /* Temporary value. */
1527
int pos = read_int (&tmp);
1531
__set_errno (EOVERFLOW);
1536
if (pos && *tmp == L_('$'))
1537
/* The precision comes from a positional parameter. */
1540
prec = va_arg (ap, int);
1542
/* If the precision is negative the precision is omitted. */
1546
else if (ISDIGIT (*f))
1548
prec = read_int (&f);
1550
/* The precision was specified in this case as an extremely
1551
large positive value. */
1554
__set_errno (EOVERFLOW);
1562
&& prec > sizeof (work_buffer) / sizeof (work_buffer[0]) - 32)
1564
if (__builtin_expect (prec >= INT_MAX / sizeof (CHAR_T) - 32, 0))
1566
__set_errno (EOVERFLOW);
1570
size_t needed = ((size_t) prec + 32) * sizeof (CHAR_T);
1572
if (__libc_use_alloca (needed))
1573
workend = (CHAR_T *) alloca (needed) + prec + 32;
1576
workstart = (CHAR_T *) malloc (needed);
1577
if (workstart == NULL)
1582
workend = workstart + prec + 32;
1585
JUMP (*f, step2_jumps);
1587
/* Process 'h' modifier. There might another 'h' following. */
1590
JUMP (*++f, step3a_jumps);
1592
/* Process 'hh' modifier. */
1593
LABEL (mod_halfhalf):
1596
JUMP (*++f, step4_jumps);
1598
/* Process 'l' modifier. There might another 'l' following. */
1601
JUMP (*++f, step3b_jumps);
1603
/* Process 'L', 'q', or 'll' modifier. No other modifier is
1604
allowed to follow. */
1605
LABEL (mod_longlong):
1608
JUMP (*++f, step4_jumps);
1611
is_long_double = sizeof (size_t) > sizeof (unsigned long int);
1612
is_long = sizeof (size_t) > sizeof (unsigned int);
1613
JUMP (*++f, step4_jumps);
1615
LABEL (mod_ptrdiff_t):
1616
is_long_double = sizeof (ptrdiff_t) > sizeof (unsigned long int);
1617
is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
1618
JUMP (*++f, step4_jumps);
1620
LABEL (mod_intmax_t):
1621
is_long_double = sizeof (intmax_t) > sizeof (unsigned long int);
1622
is_long = sizeof (intmax_t) > sizeof (unsigned int);
1623
JUMP (*++f, step4_jumps);
1625
/* Process current format. */
1628
process_arg (((struct printf_spec *) NULL));
1629
process_string_arg (((struct printf_spec *) NULL));
1631
LABEL (form_unknown):
1632
if (spec == L_('\0'))
1634
/* The format string ended before the specifier is complete. */
1635
__set_errno (EINVAL);
1640
/* If we are in the fast loop force entering the complicated
1645
/* The format is correctly handled. */
1648
if (__builtin_expect (workstart != NULL, 0))
1652
/* Look for next format specifier. */
1653
#ifdef COMPILE_WPRINTF
1654
f = __find_specwc ((end_of_spec = ++f));
1656
f = __find_specmb ((end_of_spec = ++f));
1659
/* Write the following constant string. */
1660
outstring (end_of_spec, f - end_of_spec);
1662
while (*f != L_('\0'));
1664
/* Unlock stream and return. */
1667
/* Here starts the more complex loop to handle positional parameters. */
1670
/* Array with information about the needed arguments. This has to
1671
be dynamically extensible. */
1673
/* A more or less arbitrary start value. */
1674
size_t nspecs_size = 32 * sizeof (struct printf_spec);
1675
struct printf_spec *specs = alloca (nspecs_size);
1677
/* The number of arguments the format string requests. This will
1678
determine the size of the array needed to store the argument
1681
size_t bytes_per_arg;
1682
union printf_arg *args_value;
1686
/* Positional parameters refer to arguments directly. This could
1687
also determine the maximum number of arguments. Track the
1689
size_t max_ref_arg = 0;
1691
/* Just a counter. */
1697
if (! LOCALE_SUPPORT)
1699
else if (grouping == (const char *) -1)
1701
#ifdef COMPILE_WPRINTF
1702
thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1703
_NL_NUMERIC_THOUSANDS_SEP_WC);
1705
thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1708
grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1709
if (*grouping == '\0' || *grouping == CHAR_MAX)
1713
for (f = lead_str_end; *f != L_('\0'); f = specs[nspecs++].next_fmt)
1715
if (nspecs * sizeof (*specs) >= nspecs_size)
1717
/* Extend the array of format specifiers. */
1718
struct printf_spec *old = specs;
1719
specs = extend_alloca (specs, nspecs_size, 2 * nspecs_size);
1721
/* Copy the old array's elements to the new space. */
1722
memmove (specs, old, nspecs * sizeof (*specs));
1725
/* Parse the format specifier. */
1726
#ifdef COMPILE_WPRINTF
1727
nargs += __parse_one_specwc (f, nargs, &specs[nspecs], &max_ref_arg);
1729
nargs += __parse_one_specmb (f, nargs, &specs[nspecs], &max_ref_arg);
1733
/* Determine the number of arguments the format string consumes. */
1734
nargs = MAX (nargs, max_ref_arg);
1735
/* Calculate total size needed to represent a single argument across
1736
all three argument-related arrays. */
1737
bytes_per_arg = (sizeof (*args_value) + sizeof (*args_size)
1738
+ sizeof (*args_type));
1740
/* Check for potential integer overflow. */
1741
if (__builtin_expect (nargs > INT_MAX / bytes_per_arg, 0))
1743
__set_errno (EOVERFLOW);
1748
/* Allocate memory for all three argument arrays. */
1749
if (__libc_use_alloca (nargs * bytes_per_arg))
1750
args_value = alloca (nargs * bytes_per_arg);
1753
args_value = args_malloced = malloc (nargs * bytes_per_arg);
1754
if (args_value == NULL)
1761
/* Set up the remaining two arrays to each point past the end of the
1762
prior array, since space for all three has been allocated now. */
1763
args_size = &args_value[nargs].pa_int;
1764
args_type = &args_size[nargs];
1765
memset (args_type, s->_flags2 & _IO_FLAGS2_FORTIFY ? '\xff' : '\0',
1766
nargs * sizeof (*args_type));
1768
/* XXX Could do sanity check here: If any element in ARGS_TYPE is
1769
still zero after this loop, format is invalid. For now we
1770
simply use 0 as the value. */
1772
/* Fill in the types of all the arguments. */
1773
for (cnt = 0; cnt < nspecs; ++cnt)
1775
/* If the width is determined by an argument this is an int. */
1776
if (specs[cnt].width_arg != -1)
1777
args_type[specs[cnt].width_arg] = PA_INT;
1779
/* If the precision is determined by an argument this is an int. */
1780
if (specs[cnt].prec_arg != -1)
1781
args_type[specs[cnt].prec_arg] = PA_INT;
1783
switch (specs[cnt].ndata_args)
1785
case 0: /* No arguments. */
1787
case 1: /* One argument; we already have the
1789
args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1790
args_size[specs[cnt].data_arg] = specs[cnt].size;
1793
/* We have more than one argument for this format spec.
1794
We must call the arginfo function again to determine
1796
(void) (*__printf_arginfo_table[specs[cnt].info.spec])
1798
specs[cnt].ndata_args, &args_type[specs[cnt].data_arg],
1799
&args_size[specs[cnt].data_arg]);
1804
/* Now we know all the types and the order. Fill in the argument
1806
for (cnt = 0; cnt < nargs; ++cnt)
1807
switch (args_type[cnt])
1809
#define T(tag, mem, type) \
1811
args_value[cnt].mem = va_arg (ap_save, type); \
1814
T (PA_WCHAR, pa_wchar, wint_t);
1815
case PA_CHAR: /* Promoted. */
1816
case PA_INT|PA_FLAG_SHORT: /* Promoted. */
1817
#if LONG_MAX == INT_MAX
1818
case PA_INT|PA_FLAG_LONG:
1820
T (PA_INT, pa_int, int);
1821
#if LONG_MAX == LONG_LONG_MAX
1822
case PA_INT|PA_FLAG_LONG:
1824
T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1825
#if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1828
case PA_FLOAT: /* Promoted. */
1829
T (PA_DOUBLE, pa_double, double);
1830
case PA_DOUBLE|PA_FLAG_LONG_DOUBLE:
1833
args_value[cnt].pa_double = va_arg (ap_save, double);
1834
args_type[cnt] &= ~PA_FLAG_LONG_DOUBLE;
1837
args_value[cnt].pa_long_double = va_arg (ap_save, long double);
1839
case PA_STRING: /* All pointers are the same */
1840
case PA_WSTRING: /* All pointers are the same */
1841
T (PA_POINTER, pa_pointer, void *);
1844
if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1845
args_value[cnt].pa_pointer = va_arg (ap_save, void *);
1846
else if (__builtin_expect (__printf_va_arg_table != NULL, 0)
1847
&& __printf_va_arg_table[args_type[cnt] - PA_LAST] != NULL)
1849
args_value[cnt].pa_user = alloca (args_size[cnt]);
1850
(*__printf_va_arg_table[args_type[cnt] - PA_LAST])
1851
(args_value[cnt].pa_user, &ap_save);
1854
args_value[cnt].pa_long_double = 0.0;
1857
/* Error case. Not all parameters appear in N$ format
1858
strings. We have no way to determine their type. */
1859
assert (s->_flags2 & _IO_FLAGS2_FORTIFY);
1860
__libc_fatal ("*** invalid %N$ use detected ***\n");
1863
/* Now walk through all format specifiers and process them. */
1864
for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1868
# define REF(Name) &&do2_##Name - &&do_form_unknown
1870
# define REF(Name) &&do2_##Name
1873
#define LABEL(Name) do2_##Name
1879
unsigned long long int longlong;
1880
unsigned long int word;
1883
union printf_arg the_arg;
1884
CHAR_T *string; /* Pointer to argument string. */
1886
/* Fill variables from values in struct. */
1887
int alt = specs[nspecs_done].info.alt;
1888
int space = specs[nspecs_done].info.space;
1889
int left = specs[nspecs_done].info.left;
1890
int showsign = specs[nspecs_done].info.showsign;
1891
int group = specs[nspecs_done].info.group;
1892
int is_long_double = specs[nspecs_done].info.is_long_double;
1893
int is_short = specs[nspecs_done].info.is_short;
1894
int is_char = specs[nspecs_done].info.is_char;
1895
int is_long = specs[nspecs_done].info.is_long;
1896
int width = specs[nspecs_done].info.width;
1897
int prec = specs[nspecs_done].info.prec;
1898
int use_outdigits = specs[nspecs_done].info.i18n;
1899
char pad = specs[nspecs_done].info.pad;
1900
CHAR_T spec = specs[nspecs_done].info.spec;
1903
workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1905
/* Fill in last information. */
1906
if (specs[nspecs_done].width_arg != -1)
1908
/* Extract the field width from an argument. */
1909
specs[nspecs_done].info.width =
1910
args_value[specs[nspecs_done].width_arg].pa_int;
1912
if (specs[nspecs_done].info.width < 0)
1913
/* If the width value is negative left justification is
1914
selected and the value is taken as being positive. */
1916
specs[nspecs_done].info.width *= -1;
1917
left = specs[nspecs_done].info.left = 1;
1919
width = specs[nspecs_done].info.width;
1922
if (specs[nspecs_done].prec_arg != -1)
1924
/* Extract the precision from an argument. */
1925
specs[nspecs_done].info.prec =
1926
args_value[specs[nspecs_done].prec_arg].pa_int;
1928
if (specs[nspecs_done].info.prec < 0)
1929
/* If the precision is negative the precision is
1931
specs[nspecs_done].info.prec = -1;
1933
prec = specs[nspecs_done].info.prec;
1936
/* Maybe the buffer is too small. */
1937
if (MAX (prec, width) + 32 > (int) (sizeof (work_buffer)
1940
if (__libc_use_alloca ((MAX (prec, width) + 32)
1942
workend = ((CHAR_T *) alloca ((MAX (prec, width) + 32)
1944
+ (MAX (prec, width) + 32));
1947
workstart = (CHAR_T *) malloc ((MAX (prec, width) + 32)
1949
workend = workstart + (MAX (prec, width) + 32);
1953
/* Process format specifiers. */
1956
extern printf_function **__printf_function_table;
1959
if (spec <= UCHAR_MAX
1960
&& __printf_function_table != NULL
1961
&& __printf_function_table[(size_t) spec] != NULL)
1963
const void **ptr = alloca (specs[nspecs_done].ndata_args
1964
* sizeof (const void *));
1966
/* Fill in an array of pointers to the argument values. */
1967
for (unsigned int i = 0; i < specs[nspecs_done].ndata_args;
1969
ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1971
/* Call the function. */
1972
function_done = __printf_function_table[(size_t) spec]
1973
(s, &specs[nspecs_done].info, ptr);
1975
if (function_done != -2)
1977
/* If an error occurred we don't have information
1978
about # of chars. */
1979
if (function_done < 0)
1981
/* Function has set errno. */
1986
done_add (function_done);
1991
JUMP (spec, step4_jumps);
1993
process_arg ((&specs[nspecs_done]));
1994
process_string_arg ((&specs[nspecs_done]));
1996
LABEL (form_unknown):
2001
ptr = alloca (specs[nspecs_done].ndata_args
2002
* sizeof (const void *));
2004
/* Fill in an array of pointers to the argument values. */
2005
for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
2006
ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
2008
/* Call the function. */
2009
function_done = printf_unknown (s, &specs[nspecs_done].info,
2012
/* If an error occurred we don't have information about #
2014
if (function_done < 0)
2016
/* Function has set errno. */
2021
done_add (function_done);
2029
/* Write the following constant string. */
2030
outstring (specs[nspecs_done].end_of_fmt,
2031
specs[nspecs_done].next_fmt
2032
- specs[nspecs_done].end_of_fmt);
2037
free (args_malloced);
2039
/* Unlock the stream. */
2040
_IO_funlockfile (s);
2041
_IO_cleanup_region_end (0);
2046
/* Handle an unknown format specifier. This prints out a canonicalized
2047
representation of the format spec itself. */
2049
printf_unknown (FILE *s, const struct printf_info *info,
2050
const void *const *args)
2054
CHAR_T work_buffer[MAX (sizeof (info->width), sizeof (info->prec)) * 3];
2055
CHAR_T *const workend
2056
= &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
2067
else if (info->space)
2071
if (info->pad == L_('0'))
2076
if (info->width != 0)
2078
w = _itoa_word (info->width, workend, 10, 0);
2083
if (info->prec != -1)
2086
w = _itoa_word (info->prec, workend, 10, 0);
2091
if (info->spec != L_('\0'))
2092
outchar (info->spec);
2098
/* Group the digits according to the grouping rules of the current locale.
2099
The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
2102
group_number (CHAR_T *w, CHAR_T *rear_ptr, const char *grouping,
2103
#ifdef COMPILE_WPRINTF
2104
wchar_t thousands_sep
2106
const char *thousands_sep
2112
#ifndef COMPILE_WPRINTF
2113
int tlen = strlen (thousands_sep);
2116
/* We treat all negative values like CHAR_MAX. */
2118
if (*grouping == CHAR_MAX || *grouping <= 0)
2119
/* No grouping should be done. */
2124
/* Copy existing string so that nothing gets overwritten. */
2125
src = (CHAR_T *) alloca ((rear_ptr - w) * sizeof (CHAR_T));
2126
s = (CHAR_T *) __mempcpy (src, w,
2127
(rear_ptr - w) * sizeof (CHAR_T));
2130
/* Process all characters in the string. */
2135
if (--len == 0 && s > src)
2137
/* A new group begins. */
2138
#ifdef COMPILE_WPRINTF
2139
*--w = thousands_sep;
2143
*--w = thousands_sep[--cnt];
2147
if (*grouping == CHAR_MAX
2153
/* No further grouping to be done.
2154
Copy the rest of the number. */
2160
else if (*grouping != '\0')
2161
/* The previous grouping repeats ad infinitum. */
2170
/* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2173
struct _IO_FILE_plus _f;
2174
#ifdef COMPILE_WPRINTF
2175
struct _IO_wide_data _wide_data;
2177
_IO_FILE *_put_stream;
2178
#ifdef _IO_MTSAFE_IO
2184
_IO_helper_overflow (_IO_FILE *s, int c)
2186
_IO_FILE *target = ((struct helper_file*) s)->_put_stream;
2187
#ifdef COMPILE_WPRINTF
2188
int used = s->_wide_data->_IO_write_ptr - s->_wide_data->_IO_write_base;
2191
_IO_size_t written = _IO_sputn (target, s->_wide_data->_IO_write_base,
2193
if (written == 0 || written == WEOF)
2195
__wmemmove (s->_wide_data->_IO_write_base,
2196
s->_wide_data->_IO_write_base + written,
2198
s->_wide_data->_IO_write_ptr -= written;
2201
int used = s->_IO_write_ptr - s->_IO_write_base;
2204
_IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
2205
if (written == 0 || written == EOF)
2207
memmove (s->_IO_write_base, s->_IO_write_base + written,
2209
s->_IO_write_ptr -= written;
2215
#ifdef COMPILE_WPRINTF
2216
static const struct _IO_jump_t _IO_helper_jumps =
2219
JUMP_INIT (finish, _IO_wdefault_finish),
2220
JUMP_INIT (overflow, _IO_helper_overflow),
2221
JUMP_INIT (underflow, _IO_default_underflow),
2222
JUMP_INIT (uflow, _IO_default_uflow),
2223
JUMP_INIT (pbackfail, (_IO_pbackfail_t) _IO_wdefault_pbackfail),
2224
JUMP_INIT (xsputn, _IO_wdefault_xsputn),
2225
JUMP_INIT (xsgetn, _IO_wdefault_xsgetn),
2226
JUMP_INIT (seekoff, _IO_default_seekoff),
2227
JUMP_INIT (seekpos, _IO_default_seekpos),
2228
JUMP_INIT (setbuf, _IO_default_setbuf),
2229
JUMP_INIT (sync, _IO_default_sync),
2230
JUMP_INIT (doallocate, _IO_wdefault_doallocate),
2231
JUMP_INIT (read, _IO_default_read),
2232
JUMP_INIT (write, _IO_default_write),
2233
JUMP_INIT (seek, _IO_default_seek),
2234
JUMP_INIT (close, _IO_default_close),
2235
JUMP_INIT (stat, _IO_default_stat)
2238
static const struct _IO_jump_t _IO_helper_jumps =
2241
JUMP_INIT (finish, _IO_default_finish),
2242
JUMP_INIT (overflow, _IO_helper_overflow),
2243
JUMP_INIT (underflow, _IO_default_underflow),
2244
JUMP_INIT (uflow, _IO_default_uflow),
2245
JUMP_INIT (pbackfail, _IO_default_pbackfail),
2246
JUMP_INIT (xsputn, _IO_default_xsputn),
2247
JUMP_INIT (xsgetn, _IO_default_xsgetn),
2248
JUMP_INIT (seekoff, _IO_default_seekoff),
2249
JUMP_INIT (seekpos, _IO_default_seekpos),
2250
JUMP_INIT (setbuf, _IO_default_setbuf),
2251
JUMP_INIT (sync, _IO_default_sync),
2252
JUMP_INIT (doallocate, _IO_default_doallocate),
2253
JUMP_INIT (read, _IO_default_read),
2254
JUMP_INIT (write, _IO_default_write),
2255
JUMP_INIT (seek, _IO_default_seek),
2256
JUMP_INIT (close, _IO_default_close),
2257
JUMP_INIT (stat, _IO_default_stat)
2263
buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format,
2266
CHAR_T buf[_IO_BUFSIZ];
2267
struct helper_file helper;
2268
register _IO_FILE *hp = (_IO_FILE *) &helper._f;
2269
int result, to_flush;
2271
/* Orient the stream. */
2276
/* Initialize helper. */
2277
helper._put_stream = s;
2278
#ifdef COMPILE_WPRINTF
2279
hp->_wide_data = &helper._wide_data;
2280
_IO_wsetp (hp, buf, buf + sizeof buf / sizeof (CHAR_T));
2283
_IO_setp (hp, buf, buf + sizeof buf);
2286
hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS|_IO_USER_LOCK;
2287
#if _IO_JUMPS_OFFSET
2288
hp->_vtable_offset = 0;
2290
#ifdef _IO_MTSAFE_IO
2293
hp->_flags2 = s->_flags2;
2294
_IO_JUMPS (&helper._f) = (struct _IO_jump_t *) &_IO_helper_jumps;
2296
/* Now print to helper instead. */
2297
#ifndef COMPILE_WPRINTF
2298
result = _IO_vfprintf (hp, format, args);
2300
result = vfprintf (hp, format, args);
2304
__libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, s);
2307
/* Now flush anything from the helper to the S. */
2308
#ifdef COMPILE_WPRINTF
2309
if ((to_flush = (hp->_wide_data->_IO_write_ptr
2310
- hp->_wide_data->_IO_write_base)) > 0)
2312
if ((int) _IO_sputn (s, hp->_wide_data->_IO_write_base, to_flush)
2317
if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
2319
if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
2324
/* Unlock the stream. */
2325
_IO_funlockfile (s);
2326
__libc_cleanup_region_end (0);
2332
#ifdef COMPILE_WPRINTF
2333
strong_alias (_IO_vfwprintf, __vfwprintf);
2334
ldbl_weak_alias (_IO_vfwprintf, vfwprintf);
2336
ldbl_strong_alias (_IO_vfprintf_internal, vfprintf);
2337
ldbl_hidden_def (_IO_vfprintf_internal, vfprintf)
2338
ldbl_strong_alias (_IO_vfprintf_internal, _IO_vfprintf);
2339
ldbl_hidden_def (_IO_vfprintf_internal, _IO_vfprintf)