]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdio-common/vfprintf-internal.c
Update syscall lists for Linux 5.15
[thirdparty/glibc.git] / stdio-common / vfprintf-internal.c
CommitLineData
2b778ceb 1/* Copyright (C) 1991-2021 Free Software Foundation, Inc.
698fb75b
ZW
2 This file is part of the GNU C Library.
3
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.
8
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.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
5a82c748 16 <https://www.gnu.org/licenses/>. */
698fb75b
ZW
17
18#include <array_length.h>
19#include <ctype.h>
20#include <limits.h>
21#include <printf.h>
22#include <stdarg.h>
23#include <stdint.h>
24#include <stdlib.h>
25#include <string.h>
26#include <errno.h>
27#include <wchar.h>
28#include <libc-lock.h>
29#include <sys/param.h>
30#include <_itoa.h>
31#include <locale/localeinfo.h>
32#include <stdio.h>
33#include <scratch_buffer.h>
3cc4a836 34#include <intprops.h>
698fb75b
ZW
35
36/* This code is shared between the standard stdio implementation found
37 in GNU C library and the libio implementation originally found in
38 GNU libg++.
39
40 Beside this it is also shared between the normal and wide character
41 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
42
43#include <libioP.h>
44
45#ifdef COMPILE_WPRINTF
46#include <wctype.h>
47#endif
48
698fb75b
ZW
49#define ARGCHECK(S, Format) \
50 do \
51 { \
52 /* Check file argument for consistence. */ \
53 CHECK_FILE (S, -1); \
54 if (S->_flags & _IO_NO_WRITES) \
55 { \
56 S->_flags |= _IO_ERR_SEEN; \
57 __set_errno (EBADF); \
58 return -1; \
59 } \
60 if (Format == NULL) \
61 { \
62 __set_errno (EINVAL); \
63 return -1; \
64 } \
65 } while (0)
66#define UNBUFFERED_P(S) ((S)->_flags & _IO_UNBUFFERED)
67
1626f499
GG
68#if __HAVE_FLOAT128_UNLIKE_LDBL
69# define PARSE_FLOAT_VA_ARG_EXTENDED(INFO) \
70 do \
71 { \
72 if (is_long_double \
73 && (mode_flags & PRINTF_LDBL_USES_FLOAT128) != 0) \
74 { \
75 INFO.is_binary128 = 1; \
76 the_arg.pa_float128 = va_arg (ap, _Float128); \
77 } \
78 else \
79 { \
80 PARSE_FLOAT_VA_ARG (INFO); \
81 } \
82 } \
83 while (0)
84#else
85# define PARSE_FLOAT_VA_ARG_EXTENDED(INFO) \
86 PARSE_FLOAT_VA_ARG (INFO);
87#endif
88
89#define PARSE_FLOAT_VA_ARG(INFO) \
90 do \
91 { \
92 INFO.is_binary128 = 0; \
93 if (is_long_double) \
94 the_arg.pa_long_double = va_arg (ap, long double); \
95 else \
96 the_arg.pa_double = va_arg (ap, double); \
97 } \
98 while (0)
99
100#if __HAVE_FLOAT128_UNLIKE_LDBL
101# define SETUP_FLOAT128_INFO(INFO) \
102 do \
103 { \
104 if ((mode_flags & PRINTF_LDBL_USES_FLOAT128) != 0) \
105 INFO.is_binary128 = is_long_double; \
106 else \
107 INFO.is_binary128 = 0; \
108 } \
109 while (0)
110#else
111# define SETUP_FLOAT128_INFO(INFO) \
112 do \
113 { \
114 INFO.is_binary128 = 0; \
115 } \
116 while (0)
117#endif
118
3cc4a836
FW
119/* Add LENGTH to DONE. Return the new value of DONE, or -1 on
120 overflow (and set errno accordingly). */
121static inline int
122done_add_func (size_t length, int done)
123{
124 if (done < 0)
125 return done;
126 int ret;
127 if (INT_ADD_WRAPV (done, length, &ret))
128 {
129 __set_errno (EOVERFLOW);
130 return -1;
131 }
132 return ret;
133}
134
135#define done_add(val) \
136 do \
137 { \
138 /* Ensure that VAL has a type similar to int. */ \
139 _Static_assert (sizeof (val) == sizeof (int), "value int size"); \
140 _Static_assert ((__typeof__ (val)) -1 < 0, "value signed"); \
141 done = done_add_func ((val), done); \
142 if (done < 0) \
143 goto all_done; \
144 } \
145 while (0)
698fb75b
ZW
146
147#ifndef COMPILE_WPRINTF
148# define vfprintf __vfprintf_internal
149# define CHAR_T char
3cc4a836 150# define OTHER_CHAR_T wchar_t
698fb75b
ZW
151# define UCHAR_T unsigned char
152# define INT_T int
153typedef const char *THOUSANDS_SEP_T;
154# define L_(Str) Str
155# define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
156# define STR_LEN(Str) strlen (Str)
157
158# define PUT(F, S, N) _IO_sputn ((F), (S), (N))
698fb75b
ZW
159# define PUTC(C, F) _IO_putc_unlocked (C, F)
160# define ORIENT if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
161 return -1
3cc4a836 162# define CONVERT_FROM_OTHER_STRING __wcsrtombs
698fb75b
ZW
163#else
164# define vfprintf __vfwprintf_internal
165# define CHAR_T wchar_t
3cc4a836 166# define OTHER_CHAR_T char
698fb75b
ZW
167/* This is a hack!!! There should be a type uwchar_t. */
168# define UCHAR_T unsigned int /* uwchar_t */
169# define INT_T wint_t
170typedef wchar_t THOUSANDS_SEP_T;
171# define L_(Str) L##Str
172# define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
173# define STR_LEN(Str) __wcslen (Str)
174
175# include <_itowa.h>
176
177# define PUT(F, S, N) _IO_sputn ((F), (S), (N))
698fb75b
ZW
178# define PUTC(C, F) _IO_putwc_unlocked (C, F)
179# define ORIENT if (_IO_fwide (s, 1) != 1) return -1
3cc4a836 180# define CONVERT_FROM_OTHER_STRING __mbsrtowcs
698fb75b
ZW
181
182# undef _itoa
183# define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
184# define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
185# undef EOF
186# define EOF WEOF
187#endif
188
3cc4a836
FW
189static inline int
190pad_func (FILE *s, CHAR_T padchar, int width, int done)
191{
192 if (width > 0)
193 {
194 ssize_t written;
195#ifndef COMPILE_WPRINTF
196 written = _IO_padn (s, padchar, width);
197#else
198 written = _IO_wpadn (s, padchar, width);
199#endif
200 if (__glibc_unlikely (written != width))
201 return -1;
202 return done_add_func (width, done);
203 }
204 return done;
205}
206
207#define PAD(Padchar) \
208 do \
209 { \
210 done = pad_func (s, (Padchar), width, done); \
211 if (done < 0) \
212 goto all_done; \
213 } \
214 while (0)
215
698fb75b
ZW
216#include "_i18n_number.h"
217
218/* Include the shared code for parsing the format string. */
219#include "printf-parse.h"
220
221
222#define outchar(Ch) \
223 do \
224 { \
225 const INT_T outc = (Ch); \
226 if (PUTC (outc, s) == EOF || done == INT_MAX) \
227 { \
228 done = -1; \
229 goto all_done; \
230 } \
231 ++done; \
232 } \
233 while (0)
234
3cc4a836
FW
235static inline int
236outstring_func (FILE *s, const UCHAR_T *string, size_t length, int done)
237{
238 assert ((size_t) done <= (size_t) INT_MAX);
239 if ((size_t) PUT (s, string, length) != (size_t) (length))
240 return -1;
241 return done_add_func (length, done);
242}
243
244#define outstring(String, Len) \
245 do \
246 { \
247 const void *string_ = (String); \
248 done = outstring_func (s, string_, (Len), done); \
249 if (done < 0) \
250 goto all_done; \
251 } \
252 while (0)
253
254/* Write the string SRC to S. If PREC is non-negative, write at most
255 PREC bytes. If LEFT is true, perform left justification. */
256static int
257outstring_converted_wide_string (FILE *s, const OTHER_CHAR_T *src, int prec,
258 int width, bool left, int done)
259{
260 /* Use a small buffer to combine processing of multiple characters.
261 CONVERT_FROM_OTHER_STRING expects the buffer size in (wide)
262 characters, and buf_length counts that. */
263 enum { buf_length = 256 / sizeof (CHAR_T) };
264 CHAR_T buf[buf_length];
265 _Static_assert (sizeof (buf) > MB_LEN_MAX,
266 "buffer is large enough for a single multi-byte character");
267
268 /* Add the initial padding if needed. */
269 if (width > 0 && !left)
270 {
271 /* Make a first pass to find the output width, so that we can
272 add the required padding. */
273 mbstate_t mbstate = { 0 };
274 const OTHER_CHAR_T *src_copy = src;
275 size_t total_written;
276 if (prec < 0)
277 total_written = CONVERT_FROM_OTHER_STRING
278 (NULL, &src_copy, 0, &mbstate);
279 else
280 {
281 /* The source might not be null-terminated. Enforce the
282 limit manually, based on the output length. */
283 total_written = 0;
284 size_t limit = prec;
285 while (limit > 0 && src_copy != NULL)
286 {
287 size_t write_limit = buf_length;
288 if (write_limit > limit)
289 write_limit = limit;
290 size_t written = CONVERT_FROM_OTHER_STRING
291 (buf, &src_copy, write_limit, &mbstate);
292 if (written == (size_t) -1)
293 return -1;
294 if (written == 0)
295 break;
296 total_written += written;
297 limit -= written;
298 }
299 }
300
301 /* Output initial padding. */
302 if (total_written < width)
303 {
304 done = pad_func (s, L_(' '), width - total_written, done);
305 if (done < 0)
306 return done;
307 }
308 }
309
310 /* Convert the input string, piece by piece. */
311 size_t total_written = 0;
312 {
313 mbstate_t mbstate = { 0 };
314 /* If prec is negative, remaining is not decremented, otherwise,
315 it serves as the write limit. */
316 size_t remaining = -1;
317 if (prec >= 0)
318 remaining = prec;
319 while (remaining > 0 && src != NULL)
320 {
321 size_t write_limit = buf_length;
322 if (remaining < write_limit)
323 write_limit = remaining;
324 size_t written = CONVERT_FROM_OTHER_STRING
325 (buf, &src, write_limit, &mbstate);
326 if (written == (size_t) -1)
327 return -1;
328 if (written == 0)
329 break;
330 done = outstring_func (s, (const UCHAR_T *) buf, written, done);
331 if (done < 0)
332 return done;
333 total_written += written;
334 if (prec >= 0)
335 remaining -= written;
336 }
337 }
338
339 /* Add final padding. */
340 if (width > 0 && left && total_written < width)
341 return pad_func (s, L_(' '), width - total_written, done);
342 return done;
343}
698fb75b 344
0a5e270b
FW
345/* Calls __printf_fp or __printf_fphex based on the value of the
346 format specifier INFO->spec. */
347static inline int
348__printf_fp_spec (FILE *fp, const struct printf_info *info,
349 const void *const *args)
350{
351 if (info->spec == 'a' || info->spec == 'A')
352 return __printf_fphex (fp, info, args);
353 else
354 return __printf_fp (fp, info, args);
355}
356
698fb75b
ZW
357/* For handling long_double and longlong we use the same flag. If
358 `long' and `long long' are effectively the same type define it to
359 zero. */
360#if LONG_MAX == LONG_LONG_MAX
361# define is_longlong 0
362#else
363# define is_longlong is_long_double
364#endif
365
366/* If `long' and `int' is effectively the same type we don't have to
367 handle `long separately. */
368#if INT_MAX == LONG_MAX
369# define is_long_num 0
370#else
371# define is_long_num is_long
372#endif
373
374
375/* Global constants. */
376static const CHAR_T null[] = L_("(null)");
377
378/* Size of the work_buffer variable (in characters, not bytes. */
379enum { WORK_BUFFER_SIZE = 1000 / sizeof (CHAR_T) };
380
381/* This table maps a character into a number representing a class. In
382 each step there is a destination label for each class. */
383static const uint8_t jump_table[] =
384 {
385 /* ' ' */ 1, 0, 0, /* '#' */ 4,
386 0, /* '%' */ 14, 0, /* '\''*/ 6,
387 0, 0, /* '*' */ 7, /* '+' */ 2,
388 0, /* '-' */ 3, /* '.' */ 9, 0,
389 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
390 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
391 /* '8' */ 8, /* '9' */ 8, 0, 0,
392 0, 0, 0, 0,
393 0, /* 'A' */ 26, 0, /* 'C' */ 25,
394 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
395 0, /* 'I' */ 29, 0, 0,
396 /* 'L' */ 12, 0, 0, 0,
397 0, 0, 0, /* 'S' */ 21,
398 0, 0, 0, 0,
399 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
400 0, 0, 0, 0,
401 0, /* 'a' */ 26, 0, /* 'c' */ 20,
402 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
403 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
404 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
405 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
406 /* 't' */ 27, /* 'u' */ 16, 0, 0,
407 /* 'x' */ 18, 0, /* 'z' */ 13
408 };
409
410#define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
411#define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
412#define LABEL(Name) do_##Name
413#ifdef SHARED
414 /* 'int' is enough and it saves some space on 64 bit systems. */
415# define JUMP_TABLE_TYPE const int
416# define JUMP_TABLE_BASE_LABEL do_form_unknown
417# define REF(Name) &&do_##Name - &&JUMP_TABLE_BASE_LABEL
418# define JUMP(ChExpr, table) \
419 do \
420 { \
421 int offset; \
422 void *ptr; \
423 spec = (ChExpr); \
424 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
425 : table[CHAR_CLASS (spec)]; \
426 ptr = &&JUMP_TABLE_BASE_LABEL + offset; \
427 goto *ptr; \
428 } \
429 while (0)
430#else
431# define JUMP_TABLE_TYPE const void *const
432# define REF(Name) &&do_##Name
433# define JUMP(ChExpr, table) \
434 do \
435 { \
436 const void *ptr; \
437 spec = (ChExpr); \
438 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
439 : table[CHAR_CLASS (spec)]; \
440 goto *ptr; \
441 } \
442 while (0)
443#endif
444
445#define STEP0_3_TABLE \
446 /* Step 0: at the beginning. */ \
447 static JUMP_TABLE_TYPE step0_jumps[30] = \
448 { \
449 REF (form_unknown), \
450 REF (flag_space), /* for ' ' */ \
451 REF (flag_plus), /* for '+' */ \
452 REF (flag_minus), /* for '-' */ \
453 REF (flag_hash), /* for '<hash>' */ \
454 REF (flag_zero), /* for '0' */ \
455 REF (flag_quote), /* for '\'' */ \
456 REF (width_asterics), /* for '*' */ \
457 REF (width), /* for '1'...'9' */ \
458 REF (precision), /* for '.' */ \
459 REF (mod_half), /* for 'h' */ \
460 REF (mod_long), /* for 'l' */ \
461 REF (mod_longlong), /* for 'L', 'q' */ \
462 REF (mod_size_t), /* for 'z', 'Z' */ \
463 REF (form_percent), /* for '%' */ \
464 REF (form_integer), /* for 'd', 'i' */ \
465 REF (form_unsigned), /* for 'u' */ \
466 REF (form_octal), /* for 'o' */ \
467 REF (form_hexa), /* for 'X', 'x' */ \
468 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
469 REF (form_character), /* for 'c' */ \
470 REF (form_string), /* for 's', 'S' */ \
471 REF (form_pointer), /* for 'p' */ \
472 REF (form_number), /* for 'n' */ \
473 REF (form_strerror), /* for 'm' */ \
474 REF (form_wcharacter), /* for 'C' */ \
475 REF (form_floathex), /* for 'A', 'a' */ \
476 REF (mod_ptrdiff_t), /* for 't' */ \
477 REF (mod_intmax_t), /* for 'j' */ \
478 REF (flag_i18n), /* for 'I' */ \
479 }; \
480 /* Step 1: after processing width. */ \
481 static JUMP_TABLE_TYPE step1_jumps[30] = \
482 { \
483 REF (form_unknown), \
484 REF (form_unknown), /* for ' ' */ \
485 REF (form_unknown), /* for '+' */ \
486 REF (form_unknown), /* for '-' */ \
487 REF (form_unknown), /* for '<hash>' */ \
488 REF (form_unknown), /* for '0' */ \
489 REF (form_unknown), /* for '\'' */ \
490 REF (form_unknown), /* for '*' */ \
491 REF (form_unknown), /* for '1'...'9' */ \
492 REF (precision), /* for '.' */ \
493 REF (mod_half), /* for 'h' */ \
494 REF (mod_long), /* for 'l' */ \
495 REF (mod_longlong), /* for 'L', 'q' */ \
496 REF (mod_size_t), /* for 'z', 'Z' */ \
497 REF (form_percent), /* for '%' */ \
498 REF (form_integer), /* for 'd', 'i' */ \
499 REF (form_unsigned), /* for 'u' */ \
500 REF (form_octal), /* for 'o' */ \
501 REF (form_hexa), /* for 'X', 'x' */ \
502 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
503 REF (form_character), /* for 'c' */ \
504 REF (form_string), /* for 's', 'S' */ \
505 REF (form_pointer), /* for 'p' */ \
506 REF (form_number), /* for 'n' */ \
507 REF (form_strerror), /* for 'm' */ \
508 REF (form_wcharacter), /* for 'C' */ \
509 REF (form_floathex), /* for 'A', 'a' */ \
510 REF (mod_ptrdiff_t), /* for 't' */ \
511 REF (mod_intmax_t), /* for 'j' */ \
512 REF (form_unknown) /* for 'I' */ \
513 }; \
514 /* Step 2: after processing precision. */ \
515 static JUMP_TABLE_TYPE step2_jumps[30] = \
516 { \
517 REF (form_unknown), \
518 REF (form_unknown), /* for ' ' */ \
519 REF (form_unknown), /* for '+' */ \
520 REF (form_unknown), /* for '-' */ \
521 REF (form_unknown), /* for '<hash>' */ \
522 REF (form_unknown), /* for '0' */ \
523 REF (form_unknown), /* for '\'' */ \
524 REF (form_unknown), /* for '*' */ \
525 REF (form_unknown), /* for '1'...'9' */ \
526 REF (form_unknown), /* for '.' */ \
527 REF (mod_half), /* for 'h' */ \
528 REF (mod_long), /* for 'l' */ \
529 REF (mod_longlong), /* for 'L', 'q' */ \
530 REF (mod_size_t), /* for 'z', 'Z' */ \
531 REF (form_percent), /* for '%' */ \
532 REF (form_integer), /* for 'd', 'i' */ \
533 REF (form_unsigned), /* for 'u' */ \
534 REF (form_octal), /* for 'o' */ \
535 REF (form_hexa), /* for 'X', 'x' */ \
536 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
537 REF (form_character), /* for 'c' */ \
538 REF (form_string), /* for 's', 'S' */ \
539 REF (form_pointer), /* for 'p' */ \
540 REF (form_number), /* for 'n' */ \
541 REF (form_strerror), /* for 'm' */ \
542 REF (form_wcharacter), /* for 'C' */ \
543 REF (form_floathex), /* for 'A', 'a' */ \
544 REF (mod_ptrdiff_t), /* for 't' */ \
545 REF (mod_intmax_t), /* for 'j' */ \
546 REF (form_unknown) /* for 'I' */ \
547 }; \
548 /* Step 3a: after processing first 'h' modifier. */ \
549 static JUMP_TABLE_TYPE step3a_jumps[30] = \
550 { \
551 REF (form_unknown), \
552 REF (form_unknown), /* for ' ' */ \
553 REF (form_unknown), /* for '+' */ \
554 REF (form_unknown), /* for '-' */ \
555 REF (form_unknown), /* for '<hash>' */ \
556 REF (form_unknown), /* for '0' */ \
557 REF (form_unknown), /* for '\'' */ \
558 REF (form_unknown), /* for '*' */ \
559 REF (form_unknown), /* for '1'...'9' */ \
560 REF (form_unknown), /* for '.' */ \
561 REF (mod_halfhalf), /* for 'h' */ \
562 REF (form_unknown), /* for 'l' */ \
563 REF (form_unknown), /* for 'L', 'q' */ \
564 REF (form_unknown), /* for 'z', 'Z' */ \
565 REF (form_percent), /* for '%' */ \
566 REF (form_integer), /* for 'd', 'i' */ \
567 REF (form_unsigned), /* for 'u' */ \
568 REF (form_octal), /* for 'o' */ \
569 REF (form_hexa), /* for 'X', 'x' */ \
570 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
571 REF (form_unknown), /* for 'c' */ \
572 REF (form_unknown), /* for 's', 'S' */ \
573 REF (form_unknown), /* for 'p' */ \
574 REF (form_number), /* for 'n' */ \
575 REF (form_unknown), /* for 'm' */ \
576 REF (form_unknown), /* for 'C' */ \
577 REF (form_unknown), /* for 'A', 'a' */ \
578 REF (form_unknown), /* for 't' */ \
579 REF (form_unknown), /* for 'j' */ \
580 REF (form_unknown) /* for 'I' */ \
581 }; \
582 /* Step 3b: after processing first 'l' modifier. */ \
583 static JUMP_TABLE_TYPE step3b_jumps[30] = \
584 { \
585 REF (form_unknown), \
586 REF (form_unknown), /* for ' ' */ \
587 REF (form_unknown), /* for '+' */ \
588 REF (form_unknown), /* for '-' */ \
589 REF (form_unknown), /* for '<hash>' */ \
590 REF (form_unknown), /* for '0' */ \
591 REF (form_unknown), /* for '\'' */ \
592 REF (form_unknown), /* for '*' */ \
593 REF (form_unknown), /* for '1'...'9' */ \
594 REF (form_unknown), /* for '.' */ \
595 REF (form_unknown), /* for 'h' */ \
596 REF (mod_longlong), /* for 'l' */ \
597 REF (form_unknown), /* for 'L', 'q' */ \
598 REF (form_unknown), /* for 'z', 'Z' */ \
599 REF (form_percent), /* for '%' */ \
600 REF (form_integer), /* for 'd', 'i' */ \
601 REF (form_unsigned), /* for 'u' */ \
602 REF (form_octal), /* for 'o' */ \
603 REF (form_hexa), /* for 'X', 'x' */ \
604 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
605 REF (form_character), /* for 'c' */ \
606 REF (form_string), /* for 's', 'S' */ \
607 REF (form_pointer), /* for 'p' */ \
608 REF (form_number), /* for 'n' */ \
609 REF (form_strerror), /* for 'm' */ \
610 REF (form_wcharacter), /* for 'C' */ \
611 REF (form_floathex), /* for 'A', 'a' */ \
612 REF (form_unknown), /* for 't' */ \
613 REF (form_unknown), /* for 'j' */ \
614 REF (form_unknown) /* for 'I' */ \
615 }
616
617#define STEP4_TABLE \
618 /* Step 4: processing format specifier. */ \
619 static JUMP_TABLE_TYPE step4_jumps[30] = \
620 { \
621 REF (form_unknown), \
622 REF (form_unknown), /* for ' ' */ \
623 REF (form_unknown), /* for '+' */ \
624 REF (form_unknown), /* for '-' */ \
625 REF (form_unknown), /* for '<hash>' */ \
626 REF (form_unknown), /* for '0' */ \
627 REF (form_unknown), /* for '\'' */ \
628 REF (form_unknown), /* for '*' */ \
629 REF (form_unknown), /* for '1'...'9' */ \
630 REF (form_unknown), /* for '.' */ \
631 REF (form_unknown), /* for 'h' */ \
632 REF (form_unknown), /* for 'l' */ \
633 REF (form_unknown), /* for 'L', 'q' */ \
634 REF (form_unknown), /* for 'z', 'Z' */ \
635 REF (form_percent), /* for '%' */ \
636 REF (form_integer), /* for 'd', 'i' */ \
637 REF (form_unsigned), /* for 'u' */ \
638 REF (form_octal), /* for 'o' */ \
639 REF (form_hexa), /* for 'X', 'x' */ \
640 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
641 REF (form_character), /* for 'c' */ \
642 REF (form_string), /* for 's', 'S' */ \
643 REF (form_pointer), /* for 'p' */ \
644 REF (form_number), /* for 'n' */ \
645 REF (form_strerror), /* for 'm' */ \
646 REF (form_wcharacter), /* for 'C' */ \
647 REF (form_floathex), /* for 'A', 'a' */ \
648 REF (form_unknown), /* for 't' */ \
649 REF (form_unknown), /* for 'j' */ \
650 REF (form_unknown) /* for 'I' */ \
651 }
652
475b0b92
FW
653/* Before invoking this macro, process_arg_int etc. macros have to be
654 defined to extract one argument of the appropriate type. */
655#define process_arg() \
698fb75b
ZW
656 /* Start real work. We know about all flags and modifiers and \
657 now process the wanted format specifier. */ \
658 LABEL (form_percent): \
659 /* Write a literal "%". */ \
660 outchar (L_('%')); \
661 break; \
662 \
663 LABEL (form_integer): \
664 /* Signed decimal integer. */ \
665 base = 10; \
666 \
667 if (is_longlong) \
668 { \
475b0b92 669 long long int signed_number = process_arg_long_long_int (); \
698fb75b
ZW
670 is_negative = signed_number < 0; \
671 number.longlong = is_negative ? (- signed_number) : signed_number; \
672 \
673 goto LABEL (longlong_number); \
674 } \
675 else \
676 { \
677 long int signed_number; \
475b0b92
FW
678 if (is_long_num) \
679 signed_number = process_arg_long_int (); \
680 else if (is_char) \
681 signed_number = (signed char) process_arg_unsigned_int (); \
682 else if (!is_short) \
683 signed_number = process_arg_int (); \
698fb75b 684 else \
475b0b92 685 signed_number = (short int) process_arg_unsigned_int (); \
698fb75b
ZW
686 \
687 is_negative = signed_number < 0; \
688 number.word = is_negative ? (- signed_number) : signed_number; \
689 \
690 goto LABEL (number); \
691 } \
692 /* NOTREACHED */ \
693 \
694 LABEL (form_unsigned): \
695 /* Unsigned decimal integer. */ \
696 base = 10; \
697 goto LABEL (unsigned_number); \
698 /* NOTREACHED */ \
699 \
700 LABEL (form_octal): \
701 /* Unsigned octal integer. */ \
702 base = 8; \
703 goto LABEL (unsigned_number); \
704 /* NOTREACHED */ \
705 \
706 LABEL (form_hexa): \
707 /* Unsigned hexadecimal integer. */ \
708 base = 16; \
709 \
710 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
711 \
712 /* ISO specifies the `+' and ` ' flags only for signed \
713 conversions. */ \
714 is_negative = 0; \
715 showsign = 0; \
716 space = 0; \
717 \
718 if (is_longlong) \
719 { \
475b0b92 720 number.longlong = process_arg_unsigned_long_long_int (); \
698fb75b
ZW
721 \
722 LABEL (longlong_number): \
723 if (prec < 0) \
724 /* Supply a default precision if none was given. */ \
725 prec = 1; \
726 else \
727 /* We have to take care for the '0' flag. If a precision \
728 is given it must be ignored. */ \
729 pad = L_(' '); \
730 \
731 /* If the precision is 0 and the number is 0 nothing has to \
732 be written for the number, except for the 'o' format in \
733 alternate form. */ \
734 if (prec == 0 && number.longlong == 0) \
735 { \
736 string = workend; \
737 if (base == 8 && alt) \
738 *--string = L_('0'); \
739 } \
740 else \
741 { \
742 /* Put the number in WORK. */ \
743 string = _itoa (number.longlong, workend, base, \
744 spec == L_('X')); \
745 if (group && grouping) \
746 string = group_number (work_buffer, string, workend, \
747 grouping, thousands_sep); \
748 if (use_outdigits && base == 10) \
749 string = _i18n_number_rewrite (string, workend, workend); \
750 } \
751 /* Simplify further test for num != 0. */ \
752 number.word = number.longlong != 0; \
753 } \
754 else \
755 { \
475b0b92
FW
756 if (is_long_num) \
757 number.word = process_arg_unsigned_long_int (); \
758 else if (is_char) \
759 number.word = (unsigned char) process_arg_unsigned_int (); \
760 else if (!is_short) \
761 number.word = process_arg_unsigned_int (); \
698fb75b 762 else \
475b0b92 763 number.word = (unsigned short int) process_arg_unsigned_int (); \
698fb75b
ZW
764 \
765 LABEL (number): \
766 if (prec < 0) \
767 /* Supply a default precision if none was given. */ \
768 prec = 1; \
769 else \
770 /* We have to take care for the '0' flag. If a precision \
771 is given it must be ignored. */ \
772 pad = L_(' '); \
773 \
774 /* If the precision is 0 and the number is 0 nothing has to \
775 be written for the number, except for the 'o' format in \
776 alternate form. */ \
777 if (prec == 0 && number.word == 0) \
778 { \
779 string = workend; \
780 if (base == 8 && alt) \
781 *--string = L_('0'); \
782 } \
783 else \
784 { \
785 /* Put the number in WORK. */ \
786 string = _itoa_word (number.word, workend, base, \
787 spec == L_('X')); \
788 if (group && grouping) \
789 string = group_number (work_buffer, string, workend, \
790 grouping, thousands_sep); \
791 if (use_outdigits && base == 10) \
792 string = _i18n_number_rewrite (string, workend, workend); \
793 } \
794 } \
795 \
796 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
797 /* Add octal marker. */ \
798 *--string = L_('0'); \
799 \
800 prec = MAX (0, prec - (workend - string)); \
801 \
802 if (!left) \
803 { \
804 width -= workend - string + prec; \
805 \
806 if (number.word != 0 && alt && base == 16) \
807 /* Account for 0X hex marker. */ \
808 width -= 2; \
809 \
810 if (is_negative || showsign || space) \
811 --width; \
812 \
813 if (pad == L_(' ')) \
814 { \
815 PAD (L_(' ')); \
816 width = 0; \
817 } \
818 \
819 if (is_negative) \
820 outchar (L_('-')); \
821 else if (showsign) \
822 outchar (L_('+')); \
823 else if (space) \
824 outchar (L_(' ')); \
825 \
826 if (number.word != 0 && alt && base == 16) \
827 { \
828 outchar (L_('0')); \
829 outchar (spec); \
830 } \
831 \
832 width += prec; \
833 PAD (L_('0')); \
834 \
835 outstring (string, workend - string); \
836 \
837 break; \
838 } \
839 else \
840 { \
841 if (is_negative) \
842 { \
843 outchar (L_('-')); \
844 --width; \
845 } \
846 else if (showsign) \
847 { \
848 outchar (L_('+')); \
849 --width; \
850 } \
851 else if (space) \
852 { \
853 outchar (L_(' ')); \
854 --width; \
855 } \
856 \
857 if (number.word != 0 && alt && base == 16) \
858 { \
859 outchar (L_('0')); \
860 outchar (spec); \
861 width -= 2; \
862 } \
863 \
864 width -= workend - string + prec; \
865 \
866 if (prec > 0) \
867 { \
868 int temp = width; \
869 width = prec; \
870 PAD (L_('0')); \
871 width = temp; \
872 } \
873 \
874 outstring (string, workend - string); \
875 \
876 PAD (L_(' ')); \
877 break; \
878 } \
879 \
698fb75b
ZW
880 LABEL (form_pointer): \
881 /* Generic pointer. */ \
882 { \
475b0b92 883 const void *ptr = process_arg_pointer (); \
698fb75b
ZW
884 if (ptr != NULL) \
885 { \
886 /* If the pointer is not NULL, write it as a %#x spec. */ \
887 base = 16; \
888 number.word = (unsigned long int) ptr; \
889 is_negative = 0; \
890 alt = 1; \
891 group = 0; \
892 spec = L_('x'); \
893 goto LABEL (number); \
894 } \
895 else \
896 { \
897 /* Write "(nil)" for a nil pointer. */ \
898 string = (CHAR_T *) L_("(nil)"); \
899 /* Make sure the full string "(nil)" is printed. */ \
900 if (prec < 5) \
901 prec = 5; \
902 /* This is a wide string iff compiling wprintf. */ \
903 is_long = sizeof (CHAR_T) > 1; \
904 goto LABEL (print_string); \
905 } \
906 } \
907 /* NOTREACHED */ \
908 \
909 LABEL (form_number): \
910 if ((mode_flags & PRINTF_FORTIFY) != 0) \
911 { \
912 if (! readonly_format) \
913 { \
914 extern int __readonly_area (const void *, size_t) \
915 attribute_hidden; \
916 readonly_format \
917 = __readonly_area (format, ((STR_LEN (format) + 1) \
918 * sizeof (CHAR_T))); \
919 } \
920 if (readonly_format < 0) \
921 __libc_fatal ("*** %n in writable segment detected ***\n"); \
922 } \
923 /* Answer the count of characters written. */ \
475b0b92
FW
924 void *ptrptr = process_arg_pointer (); \
925 if (is_longlong) \
926 *(long long int *) ptrptr = done; \
927 else if (is_long_num) \
928 *(long int *) ptrptr = done; \
929 else if (is_char) \
930 *(char *) ptrptr = done; \
931 else if (!is_short) \
932 *(int *) ptrptr = done; \
698fb75b 933 else \
475b0b92 934 *(short int *) ptrptr = done; \
698fb75b
ZW
935 break; \
936 \
937 LABEL (form_strerror): \
938 /* Print description of error ERRNO. */ \
939 string = \
940 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
941 WORK_BUFFER_SIZE * sizeof (CHAR_T)); \
942 is_long = 0; /* This is no wide-char string. */ \
943 goto LABEL (print_string)
944
945#ifdef COMPILE_WPRINTF
475b0b92 946# define process_string_arg() \
698fb75b
ZW
947 LABEL (form_character): \
948 /* Character. */ \
949 if (is_long) \
950 goto LABEL (form_wcharacter); \
951 --width; /* Account for the character itself. */ \
952 if (!left) \
953 PAD (L' '); \
475b0b92 954 outchar (__btowc ((unsigned char) process_arg_int ())); /* Promoted. */ \
698fb75b
ZW
955 if (left) \
956 PAD (L' '); \
957 break; \
958 \
959 LABEL (form_wcharacter): \
960 { \
961 /* Wide character. */ \
962 --width; \
963 if (!left) \
964 PAD (L' '); \
475b0b92 965 outchar (process_arg_wchar_t ()); \
698fb75b
ZW
966 if (left) \
967 PAD (L' '); \
968 } \
969 break; \
970 \
971 LABEL (form_string): \
972 { \
973 size_t len; \
698fb75b
ZW
974 \
975 /* The string argument could in fact be `char *' or `wchar_t *'. \
976 But this should not make a difference here. */ \
475b0b92 977 string = (CHAR_T *) process_arg_wstring (); \
698fb75b
ZW
978 \
979 /* Entry point for printing other strings. */ \
980 LABEL (print_string): \
981 \
698fb75b
ZW
982 if (string == NULL) \
983 { \
984 /* Write "(null)" if there's space. */ \
985 if (prec == -1 || prec >= (int) array_length (null) - 1) \
986 { \
987 string = (CHAR_T *) null; \
988 len = array_length (null) - 1; \
989 } \
990 else \
991 { \
992 string = (CHAR_T *) L""; \
993 len = 0; \
994 } \
995 } \
996 else if (!is_long && spec != L_('S')) \
997 { \
3cc4a836
FW
998 done = outstring_converted_wide_string \
999 (s, (const char *) string, prec, width, left, done); \
1000 if (done < 0) \
1001 goto all_done; \
1002 /* The padding has already been written. */ \
1003 break; \
698fb75b
ZW
1004 } \
1005 else \
1006 { \
1007 if (prec != -1) \
1008 /* Search for the end of the string, but don't search past \
1009 the length specified by the precision. */ \
1010 len = __wcsnlen (string, prec); \
1011 else \
1012 len = __wcslen (string); \
1013 } \
1014 \
1015 if ((width -= len) < 0) \
1016 { \
1017 outstring (string, len); \
1018 break; \
1019 } \
1020 \
1021 if (!left) \
1022 PAD (L' '); \
1023 outstring (string, len); \
1024 if (left) \
1025 PAD (L' '); \
698fb75b
ZW
1026 } \
1027 break;
1028#else
475b0b92 1029# define process_string_arg() \
698fb75b
ZW
1030 LABEL (form_character): \
1031 /* Character. */ \
1032 if (is_long) \
1033 goto LABEL (form_wcharacter); \
1034 --width; /* Account for the character itself. */ \
1035 if (!left) \
1036 PAD (' '); \
475b0b92 1037 outchar ((unsigned char) process_arg_int ()); /* Promoted. */ \
698fb75b
ZW
1038 if (left) \
1039 PAD (' '); \
1040 break; \
1041 \
1042 LABEL (form_wcharacter): \
1043 { \
1044 /* Wide character. */ \
1045 char buf[MB_LEN_MAX]; \
1046 mbstate_t mbstate; \
1047 size_t len; \
1048 \
1049 memset (&mbstate, '\0', sizeof (mbstate_t)); \
475b0b92 1050 len = __wcrtomb (buf, process_arg_wchar_t (), &mbstate); \
698fb75b
ZW
1051 if (len == (size_t) -1) \
1052 { \
1053 /* Something went wrong during the conversion. Bail out. */ \
1054 done = -1; \
1055 goto all_done; \
1056 } \
1057 width -= len; \
1058 if (!left) \
1059 PAD (' '); \
1060 outstring (buf, len); \
1061 if (left) \
1062 PAD (' '); \
1063 } \
1064 break; \
1065 \
1066 LABEL (form_string): \
1067 { \
1068 size_t len; \
698fb75b
ZW
1069 \
1070 /* The string argument could in fact be `char *' or `wchar_t *'. \
1071 But this should not make a difference here. */ \
475b0b92 1072 string = (char *) process_arg_string (); \
698fb75b
ZW
1073 \
1074 /* Entry point for printing other strings. */ \
1075 LABEL (print_string): \
1076 \
698fb75b
ZW
1077 if (string == NULL) \
1078 { \
1079 /* Write "(null)" if there's space. */ \
1080 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1081 { \
1082 string = (char *) null; \
1083 len = sizeof (null) - 1; \
1084 } \
1085 else \
1086 { \
1087 string = (char *) ""; \
1088 len = 0; \
1089 } \
1090 } \
1091 else if (!is_long && spec != L_('S')) \
1092 { \
1093 if (prec != -1) \
1094 /* Search for the end of the string, but don't search past \
1095 the length (in bytes) specified by the precision. */ \
1096 len = __strnlen (string, prec); \
1097 else \
1098 len = strlen (string); \
1099 } \
1100 else \
1101 { \
3cc4a836
FW
1102 done = outstring_converted_wide_string \
1103 (s, (const wchar_t *) string, prec, width, left, done); \
1104 if (done < 0) \
1105 goto all_done; \
1106 /* The padding has already been written. */ \
1107 break; \
698fb75b
ZW
1108 } \
1109 \
1110 if ((width -= len) < 0) \
1111 { \
1112 outstring (string, len); \
1113 break; \
1114 } \
1115 \
1116 if (!left) \
1117 PAD (' '); \
1118 outstring (string, len); \
1119 if (left) \
1120 PAD (' '); \
698fb75b
ZW
1121 } \
1122 break;
1123#endif
1124
1125/* Helper function to provide temporary buffering for unbuffered streams. */
1126static int buffered_vfprintf (FILE *stream, const CHAR_T *fmt, va_list,
1127 unsigned int)
1128 __THROW __attribute__ ((noinline));
1129
1130/* Handle positional format specifiers. */
1131static int printf_positional (FILE *s,
1132 const CHAR_T *format, int readonly_format,
1133 va_list ap, va_list *ap_savep, int done,
1134 int nspecs_done, const UCHAR_T *lead_str_end,
1135 CHAR_T *work_buffer, int save_errno,
1136 const char *grouping,
1137 THOUSANDS_SEP_T thousands_sep,
1138 unsigned int mode_flags);
1139
1140/* Handle unknown format specifier. */
1141static int printf_unknown (FILE *, const struct printf_info *,
1142 const void *const *) __THROW;
1143
1144/* Group digits of number string. */
1145static CHAR_T *group_number (CHAR_T *, CHAR_T *, CHAR_T *, const char *,
1146 THOUSANDS_SEP_T);
1147
1148/* The function itself. */
1149int
1150vfprintf (FILE *s, const CHAR_T *format, va_list ap, unsigned int mode_flags)
1151{
1152 /* The character used as thousands separator. */
1153 THOUSANDS_SEP_T thousands_sep = 0;
1154
1155 /* The string describing the size of groups of digits. */
1156 const char *grouping;
1157
1158 /* Place to accumulate the result. */
1159 int done;
1160
1161 /* Current character in format string. */
1162 const UCHAR_T *f;
1163
1164 /* End of leading constant string. */
1165 const UCHAR_T *lead_str_end;
1166
1167 /* Points to next format specifier. */
1168 const UCHAR_T *end_of_spec;
1169
1170 /* Buffer intermediate results. */
1171 CHAR_T work_buffer[WORK_BUFFER_SIZE];
698fb75b
ZW
1172 CHAR_T *workend;
1173
1174 /* We have to save the original argument pointer. */
1175 va_list ap_save;
1176
1177 /* Count number of specifiers we already processed. */
1178 int nspecs_done;
1179
1180 /* For the %m format we may need the current `errno' value. */
1181 int save_errno = errno;
1182
1183 /* 1 if format is in read-only memory, -1 if it is in writable memory,
1184 0 if unknown. */
1185 int readonly_format = 0;
1186
698fb75b
ZW
1187 /* Orient the stream. */
1188#ifdef ORIENT
1189 ORIENT;
1190#endif
1191
1192 /* Sanity check of arguments. */
1193 ARGCHECK (s, format);
1194
1195#ifdef ORIENT
1196 /* Check for correct orientation. */
34a5a146
JM
1197 if (_IO_vtable_offset (s) == 0
1198 && _IO_fwide (s, sizeof (CHAR_T) == 1 ? -1 : 1)
698fb75b
ZW
1199 != (sizeof (CHAR_T) == 1 ? -1 : 1))
1200 /* The stream is already oriented otherwise. */
1201 return EOF;
1202#endif
1203
1204 if (UNBUFFERED_P (s))
1205 /* Use a helper function which will allocate a local temporary buffer
1206 for the stream and then call us again. */
1207 return buffered_vfprintf (s, format, ap, mode_flags);
1208
1209 /* Initialize local variables. */
1210 done = 0;
1211 grouping = (const char *) -1;
1212#ifdef __va_copy
1213 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1214 since on some systems `va_list' is not an integral type. */
1215 __va_copy (ap_save, ap);
1216#else
1217 ap_save = ap;
1218#endif
1219 nspecs_done = 0;
1220
1221#ifdef COMPILE_WPRINTF
1222 /* Find the first format specifier. */
1223 f = lead_str_end = __find_specwc ((const UCHAR_T *) format);
1224#else
1225 /* Find the first format specifier. */
1226 f = lead_str_end = __find_specmb ((const UCHAR_T *) format);
1227#endif
1228
1229 /* Lock stream. */
1230 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
1231 _IO_flockfile (s);
1232
1233 /* Write the literal text before the first format. */
1234 outstring ((const UCHAR_T *) format,
1235 lead_str_end - (const UCHAR_T *) format);
1236
1237 /* If we only have to print a simple string, return now. */
1238 if (*f == L_('\0'))
1239 goto all_done;
1240
1241 /* Use the slow path in case any printf handler is registered. */
1242 if (__glibc_unlikely (__printf_function_table != NULL
1243 || __printf_modifier_table != NULL
1244 || __printf_va_arg_table != NULL))
1245 goto do_positional;
1246
1247 /* Process whole format string. */
1248 do
1249 {
1250 STEP0_3_TABLE;
1251 STEP4_TABLE;
1252
698fb75b
ZW
1253 int is_negative; /* Flag for negative number. */
1254 union
1255 {
1256 unsigned long long int longlong;
1257 unsigned long int word;
1258 } number;
1259 int base;
1260 union printf_arg the_arg;
1261 CHAR_T *string; /* Pointer to argument string. */
1262 int alt = 0; /* Alternate format. */
1263 int space = 0; /* Use space prefix if no sign is needed. */
1264 int left = 0; /* Left-justify output. */
1265 int showsign = 0; /* Always begin with plus or minus sign. */
1266 int group = 0; /* Print numbers according grouping rules. */
475b0b92
FW
1267 /* Argument is long double/long long int. Only used if
1268 double/long double or long int/long long int are distinct. */
1269 int is_long_double __attribute__ ((unused)) = 0;
698fb75b
ZW
1270 int is_short = 0; /* Argument is short int. */
1271 int is_long = 0; /* Argument is long int. */
1272 int is_char = 0; /* Argument is promoted (unsigned) char. */
1273 int width = 0; /* Width of output; 0 means none specified. */
1274 int prec = -1; /* Precision of output; -1 means none specified. */
1275 /* This flag is set by the 'I' modifier and selects the use of the
1276 `outdigits' as determined by the current locale. */
1277 int use_outdigits = 0;
1278 UCHAR_T pad = L_(' ');/* Padding character. */
1279 CHAR_T spec;
1280
698fb75b
ZW
1281 workend = work_buffer + WORK_BUFFER_SIZE;
1282
1283 /* Get current character in format string. */
1284 JUMP (*++f, step0_jumps);
1285
1286 /* ' ' flag. */
1287 LABEL (flag_space):
1288 space = 1;
1289 JUMP (*++f, step0_jumps);
1290
1291 /* '+' flag. */
1292 LABEL (flag_plus):
1293 showsign = 1;
1294 JUMP (*++f, step0_jumps);
1295
1296 /* The '-' flag. */
1297 LABEL (flag_minus):
1298 left = 1;
1299 pad = L_(' ');
1300 JUMP (*++f, step0_jumps);
1301
1302 /* The '#' flag. */
1303 LABEL (flag_hash):
1304 alt = 1;
1305 JUMP (*++f, step0_jumps);
1306
1307 /* The '0' flag. */
1308 LABEL (flag_zero):
1309 if (!left)
1310 pad = L_('0');
1311 JUMP (*++f, step0_jumps);
1312
1313 /* The '\'' flag. */
1314 LABEL (flag_quote):
1315 group = 1;
1316
1317 if (grouping == (const char *) -1)
1318 {
1319#ifdef COMPILE_WPRINTF
1320 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1321 _NL_NUMERIC_THOUSANDS_SEP_WC);
1322#else
1323 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1324#endif
1325
1326 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1327 if (*grouping == '\0' || *grouping == CHAR_MAX
1328#ifdef COMPILE_WPRINTF
1329 || thousands_sep == L'\0'
1330#else
1331 || *thousands_sep == '\0'
1332#endif
1333 )
1334 grouping = NULL;
1335 }
1336 JUMP (*++f, step0_jumps);
1337
1338 LABEL (flag_i18n):
1339 use_outdigits = 1;
1340 JUMP (*++f, step0_jumps);
1341
1342 /* Get width from argument. */
1343 LABEL (width_asterics):
1344 {
1345 const UCHAR_T *tmp; /* Temporary value. */
1346
1347 tmp = ++f;
1348 if (ISDIGIT (*tmp))
1349 {
1350 int pos = read_int (&tmp);
1351
1352 if (pos == -1)
1353 {
1354 __set_errno (EOVERFLOW);
1355 done = -1;
1356 goto all_done;
1357 }
1358
1359 if (pos && *tmp == L_('$'))
1360 /* The width comes from a positional parameter. */
1361 goto do_positional;
1362 }
1363 width = va_arg (ap, int);
1364
1365 /* Negative width means left justified. */
1366 if (width < 0)
1367 {
1368 width = -width;
1369 pad = L_(' ');
1370 left = 1;
1371 }
698fb75b
ZW
1372 }
1373 JUMP (*f, step1_jumps);
1374
1375 /* Given width in format string. */
1376 LABEL (width):
1377 width = read_int (&f);
1378
6caddd34 1379 if (__glibc_unlikely (width == -1))
698fb75b
ZW
1380 {
1381 __set_errno (EOVERFLOW);
1382 done = -1;
1383 goto all_done;
1384 }
1385
698fb75b
ZW
1386 if (*f == L_('$'))
1387 /* Oh, oh. The argument comes from a positional parameter. */
1388 goto do_positional;
1389 JUMP (*f, step1_jumps);
1390
1391 LABEL (precision):
1392 ++f;
1393 if (*f == L_('*'))
1394 {
1395 const UCHAR_T *tmp; /* Temporary value. */
1396
1397 tmp = ++f;
1398 if (ISDIGIT (*tmp))
1399 {
1400 int pos = read_int (&tmp);
1401
1402 if (pos == -1)
1403 {
1404 __set_errno (EOVERFLOW);
1405 done = -1;
1406 goto all_done;
1407 }
1408
1409 if (pos && *tmp == L_('$'))
1410 /* The precision comes from a positional parameter. */
1411 goto do_positional;
1412 }
1413 prec = va_arg (ap, int);
1414
1415 /* If the precision is negative the precision is omitted. */
1416 if (prec < 0)
1417 prec = -1;
1418 }
1419 else if (ISDIGIT (*f))
1420 {
1421 prec = read_int (&f);
1422
1423 /* The precision was specified in this case as an extremely
1424 large positive value. */
1425 if (prec == -1)
1426 {
1427 __set_errno (EOVERFLOW);
1428 done = -1;
1429 goto all_done;
1430 }
1431 }
1432 else
1433 prec = 0;
698fb75b
ZW
1434 JUMP (*f, step2_jumps);
1435
1436 /* Process 'h' modifier. There might another 'h' following. */
1437 LABEL (mod_half):
1438 is_short = 1;
1439 JUMP (*++f, step3a_jumps);
1440
1441 /* Process 'hh' modifier. */
1442 LABEL (mod_halfhalf):
1443 is_short = 0;
1444 is_char = 1;
1445 JUMP (*++f, step4_jumps);
1446
1447 /* Process 'l' modifier. There might another 'l' following. */
1448 LABEL (mod_long):
1449 is_long = 1;
1450 JUMP (*++f, step3b_jumps);
1451
1452 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1453 allowed to follow. */
1454 LABEL (mod_longlong):
1455 is_long_double = 1;
1456 is_long = 1;
1457 JUMP (*++f, step4_jumps);
1458
1459 LABEL (mod_size_t):
1460 is_long_double = sizeof (size_t) > sizeof (unsigned long int);
1461 is_long = sizeof (size_t) > sizeof (unsigned int);
1462 JUMP (*++f, step4_jumps);
1463
1464 LABEL (mod_ptrdiff_t):
1465 is_long_double = sizeof (ptrdiff_t) > sizeof (unsigned long int);
1466 is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
1467 JUMP (*++f, step4_jumps);
1468
1469 LABEL (mod_intmax_t):
1470 is_long_double = sizeof (intmax_t) > sizeof (unsigned long int);
1471 is_long = sizeof (intmax_t) > sizeof (unsigned int);
1472 JUMP (*++f, step4_jumps);
1473
1474 /* Process current format. */
1475 while (1)
1476 {
475b0b92
FW
1477#define process_arg_int() va_arg (ap, int)
1478#define process_arg_long_int() va_arg (ap, long int)
1479#define process_arg_long_long_int() va_arg (ap, long long int)
1480#define process_arg_pointer() va_arg (ap, void *)
1481#define process_arg_string() va_arg (ap, const char *)
1482#define process_arg_unsigned_int() va_arg (ap, unsigned int)
1483#define process_arg_unsigned_long_int() va_arg (ap, unsigned long int)
1484#define process_arg_unsigned_long_long_int() va_arg (ap, unsigned long long int)
1485#define process_arg_wchar_t() va_arg (ap, wchar_t)
1486#define process_arg_wstring() va_arg (ap, const wchar_t *)
1487 process_arg ();
1488 process_string_arg ();
1489#undef process_arg_int
1490#undef process_arg_long_int
1491#undef process_arg_long_long_int
1492#undef process_arg_pointer
1493#undef process_arg_string
1494#undef process_arg_unsigned_int
1495#undef process_arg_unsigned_long_int
1496#undef process_arg_unsigned_long_long_int
1497#undef process_arg_wchar_t
1498#undef process_arg_wstring
698fb75b 1499
0a5e270b
FW
1500 LABEL (form_float):
1501 LABEL (form_floathex):
1502 {
1503 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0))
1504 is_long_double = 0;
1505
1506 struct printf_info info =
1507 {
1508 .prec = prec,
1509 .width = width,
1510 .spec = spec,
1511 .is_long_double = is_long_double,
1512 .is_short = is_short,
1513 .is_long = is_long,
1514 .alt = alt,
1515 .space = space,
1516 .left = left,
1517 .showsign = showsign,
1518 .group = group,
1519 .pad = pad,
1520 .extra = 0,
1521 .i18n = use_outdigits,
1522 .wide = sizeof (CHAR_T) != 1,
1523 .is_binary128 = 0
1524 };
1525
1526 PARSE_FLOAT_VA_ARG_EXTENDED (info);
1527 const void *ptr = &the_arg;
1528
1529 int function_done = __printf_fp_spec (s, &info, &ptr);
1530 if (function_done < 0)
1531 {
1532 done = -1;
1533 goto all_done;
1534 }
1535 done_add (function_done);
1536 }
1537 break;
1538
698fb75b
ZW
1539 LABEL (form_unknown):
1540 if (spec == L_('\0'))
1541 {
1542 /* The format string ended before the specifier is complete. */
1543 __set_errno (EINVAL);
1544 done = -1;
1545 goto all_done;
1546 }
1547
1548 /* If we are in the fast loop force entering the complicated
1549 one. */
1550 goto do_positional;
1551 }
1552
1553 /* The format is correctly handled. */
1554 ++nspecs_done;
1555
698fb75b
ZW
1556 /* Look for next format specifier. */
1557#ifdef COMPILE_WPRINTF
1558 f = __find_specwc ((end_of_spec = ++f));
1559#else
1560 f = __find_specmb ((end_of_spec = ++f));
1561#endif
1562
1563 /* Write the following constant string. */
1564 outstring (end_of_spec, f - end_of_spec);
1565 }
1566 while (*f != L_('\0'));
1567
1568 /* Unlock stream and return. */
1569 goto all_done;
1570
1571 /* Hand off processing for positional parameters. */
1572do_positional:
698fb75b
ZW
1573 done = printf_positional (s, format, readonly_format, ap, &ap_save,
1574 done, nspecs_done, lead_str_end, work_buffer,
1575 save_errno, grouping, thousands_sep, mode_flags);
1576
1577 all_done:
698fb75b
ZW
1578 /* Unlock the stream. */
1579 _IO_funlockfile (s);
1580 _IO_cleanup_region_end (0);
1581
1582 return done;
1583}
1584\f
1585static int
1586printf_positional (FILE *s, const CHAR_T *format, int readonly_format,
1587 va_list ap, va_list *ap_savep, int done, int nspecs_done,
1588 const UCHAR_T *lead_str_end,
1589 CHAR_T *work_buffer, int save_errno,
1590 const char *grouping, THOUSANDS_SEP_T thousands_sep,
1591 unsigned int mode_flags)
1592{
1593 /* For positional argument handling. */
1594 struct scratch_buffer specsbuf;
1595 scratch_buffer_init (&specsbuf);
1596 struct printf_spec *specs = specsbuf.data;
1597 size_t specs_limit = specsbuf.length / sizeof (specs[0]);
1598
1599 /* Used as a backing store for args_value, args_size, args_type
1600 below. */
1601 struct scratch_buffer argsbuf;
1602 scratch_buffer_init (&argsbuf);
1603
1604 /* Array with information about the needed arguments. This has to
1605 be dynamically extensible. */
1606 size_t nspecs = 0;
1607
1608 /* The number of arguments the format string requests. This will
1609 determine the size of the array needed to store the argument
1610 attributes. */
1611 size_t nargs = 0;
1612
1613 /* Positional parameters refer to arguments directly. This could
1614 also determine the maximum number of arguments. Track the
1615 maximum number. */
1616 size_t max_ref_arg = 0;
1617
1618 /* Just a counter. */
1619 size_t cnt;
1620
698fb75b
ZW
1621 if (grouping == (const char *) -1)
1622 {
1623#ifdef COMPILE_WPRINTF
1624 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1625 _NL_NUMERIC_THOUSANDS_SEP_WC);
1626#else
1627 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1628#endif
1629
1630 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1631 if (*grouping == '\0' || *grouping == CHAR_MAX)
1632 grouping = NULL;
1633 }
1634
1635 for (const UCHAR_T *f = lead_str_end; *f != L_('\0');
1636 f = specs[nspecs++].next_fmt)
1637 {
1638 if (nspecs == specs_limit)
1639 {
1640 if (!scratch_buffer_grow_preserve (&specsbuf))
1641 {
1642 done = -1;
1643 goto all_done;
1644 }
1645 specs = specsbuf.data;
1646 specs_limit = specsbuf.length / sizeof (specs[0]);
1647 }
1648
1649 /* Parse the format specifier. */
1650#ifdef COMPILE_WPRINTF
1651 nargs += __parse_one_specwc (f, nargs, &specs[nspecs], &max_ref_arg);
1652#else
1653 nargs += __parse_one_specmb (f, nargs, &specs[nspecs], &max_ref_arg);
1654#endif
1655 }
1656
1657 /* Determine the number of arguments the format string consumes. */
1658 nargs = MAX (nargs, max_ref_arg);
1659
1660 union printf_arg *args_value;
1661 int *args_size;
1662 int *args_type;
1663 {
1664 /* Calculate total size needed to represent a single argument
1665 across all three argument-related arrays. */
1666 size_t bytes_per_arg
1667 = sizeof (*args_value) + sizeof (*args_size) + sizeof (*args_type);
1668 if (!scratch_buffer_set_array_size (&argsbuf, nargs, bytes_per_arg))
1669 {
1670 done = -1;
1671 goto all_done;
1672 }
1673 args_value = argsbuf.data;
1674 /* Set up the remaining two arrays to each point past the end of
1675 the prior array, since space for all three has been allocated
1676 now. */
1677 args_size = &args_value[nargs].pa_int;
1678 args_type = &args_size[nargs];
1679 memset (args_type, (mode_flags & PRINTF_FORTIFY) != 0 ? '\xff' : '\0',
1680 nargs * sizeof (*args_type));
1681 }
1682
1683 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1684 still zero after this loop, format is invalid. For now we
1685 simply use 0 as the value. */
1686
1687 /* Fill in the types of all the arguments. */
1688 for (cnt = 0; cnt < nspecs; ++cnt)
1689 {
1690 /* If the width is determined by an argument this is an int. */
1691 if (specs[cnt].width_arg != -1)
1692 args_type[specs[cnt].width_arg] = PA_INT;
1693
1694 /* If the precision is determined by an argument this is an int. */
1695 if (specs[cnt].prec_arg != -1)
1696 args_type[specs[cnt].prec_arg] = PA_INT;
1697
1698 switch (specs[cnt].ndata_args)
1699 {
1700 case 0: /* No arguments. */
1701 break;
1702 case 1: /* One argument; we already have the
1703 type and size. */
1704 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1705 args_size[specs[cnt].data_arg] = specs[cnt].size;
1706 break;
1707 default:
1708 /* We have more than one argument for this format spec.
1709 We must call the arginfo function again to determine
1710 all the types. */
1711 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1712 (&specs[cnt].info,
1713 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg],
1714 &args_size[specs[cnt].data_arg]);
1715 break;
1716 }
1717 }
1718
1719 /* Now we know all the types and the order. Fill in the argument
1720 values. */
1721 for (cnt = 0; cnt < nargs; ++cnt)
1722 switch (args_type[cnt])
1723 {
1724#define T(tag, mem, type) \
1725 case tag: \
1726 args_value[cnt].mem = va_arg (*ap_savep, type); \
1727 break
1728
1729 T (PA_WCHAR, pa_wchar, wint_t);
1730 case PA_CHAR: /* Promoted. */
1731 case PA_INT|PA_FLAG_SHORT: /* Promoted. */
1732#if LONG_MAX == INT_MAX
1733 case PA_INT|PA_FLAG_LONG:
1734#endif
1735 T (PA_INT, pa_int, int);
1736#if LONG_MAX == LONG_LONG_MAX
1737 case PA_INT|PA_FLAG_LONG:
1738#endif
1739 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1740#if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1741# error "he?"
1742#endif
1743 case PA_FLOAT: /* Promoted. */
1744 T (PA_DOUBLE, pa_double, double);
1745 case PA_DOUBLE|PA_FLAG_LONG_DOUBLE:
1746 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0))
1747 {
1748 args_value[cnt].pa_double = va_arg (*ap_savep, double);
1749 args_type[cnt] &= ~PA_FLAG_LONG_DOUBLE;
1750 }
1626f499
GG
1751#if __HAVE_FLOAT128_UNLIKE_LDBL
1752 else if ((mode_flags & PRINTF_LDBL_USES_FLOAT128) != 0)
1753 args_value[cnt].pa_float128 = va_arg (*ap_savep, _Float128);
1754#endif
698fb75b
ZW
1755 else
1756 args_value[cnt].pa_long_double = va_arg (*ap_savep, long double);
1757 break;
1758 case PA_STRING: /* All pointers are the same */
1759 case PA_WSTRING: /* All pointers are the same */
1760 T (PA_POINTER, pa_pointer, void *);
1761#undef T
1762 default:
1763 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1764 args_value[cnt].pa_pointer = va_arg (*ap_savep, void *);
1765 else if (__glibc_unlikely (__printf_va_arg_table != NULL)
1766 && __printf_va_arg_table[args_type[cnt] - PA_LAST] != NULL)
1767 {
1768 args_value[cnt].pa_user = alloca (args_size[cnt]);
1769 (*__printf_va_arg_table[args_type[cnt] - PA_LAST])
1770 (args_value[cnt].pa_user, ap_savep);
1771 }
1772 else
1626f499 1773 memset (&args_value[cnt], 0, sizeof (args_value[cnt]));
698fb75b
ZW
1774 break;
1775 case -1:
1776 /* Error case. Not all parameters appear in N$ format
1777 strings. We have no way to determine their type. */
1778 assert ((mode_flags & PRINTF_FORTIFY) != 0);
1779 __libc_fatal ("*** invalid %N$ use detected ***\n");
1780 }
1781
1782 /* Now walk through all format specifiers and process them. */
1783 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1784 {
1785 STEP4_TABLE;
1786
1787 int is_negative;
1788 union
1789 {
1790 unsigned long long int longlong;
1791 unsigned long int word;
1792 } number;
1793 int base;
698fb75b
ZW
1794 CHAR_T *string; /* Pointer to argument string. */
1795
1796 /* Fill variables from values in struct. */
1797 int alt = specs[nspecs_done].info.alt;
1798 int space = specs[nspecs_done].info.space;
1799 int left = specs[nspecs_done].info.left;
1800 int showsign = specs[nspecs_done].info.showsign;
1801 int group = specs[nspecs_done].info.group;
475b0b92
FW
1802 int is_long_double __attribute__ ((unused))
1803 = specs[nspecs_done].info.is_long_double;
698fb75b
ZW
1804 int is_short = specs[nspecs_done].info.is_short;
1805 int is_char = specs[nspecs_done].info.is_char;
1806 int is_long = specs[nspecs_done].info.is_long;
1807 int width = specs[nspecs_done].info.width;
1808 int prec = specs[nspecs_done].info.prec;
1809 int use_outdigits = specs[nspecs_done].info.i18n;
1810 char pad = specs[nspecs_done].info.pad;
1811 CHAR_T spec = specs[nspecs_done].info.spec;
1812
698fb75b
ZW
1813 CHAR_T *workend = work_buffer + WORK_BUFFER_SIZE;
1814
1815 /* Fill in last information. */
1816 if (specs[nspecs_done].width_arg != -1)
1817 {
1818 /* Extract the field width from an argument. */
1819 specs[nspecs_done].info.width =
1820 args_value[specs[nspecs_done].width_arg].pa_int;
1821
1822 if (specs[nspecs_done].info.width < 0)
1823 /* If the width value is negative left justification is
1824 selected and the value is taken as being positive. */
1825 {
1826 specs[nspecs_done].info.width *= -1;
1827 left = specs[nspecs_done].info.left = 1;
1828 }
1829 width = specs[nspecs_done].info.width;
1830 }
1831
1832 if (specs[nspecs_done].prec_arg != -1)
1833 {
1834 /* Extract the precision from an argument. */
1835 specs[nspecs_done].info.prec =
1836 args_value[specs[nspecs_done].prec_arg].pa_int;
1837
1838 if (specs[nspecs_done].info.prec < 0)
1839 /* If the precision is negative the precision is
1840 omitted. */
1841 specs[nspecs_done].info.prec = -1;
1842
1843 prec = specs[nspecs_done].info.prec;
1844 }
1845
698fb75b
ZW
1846 /* Process format specifiers. */
1847 while (1)
1848 {
1849 extern printf_function **__printf_function_table;
1850 int function_done;
1851
1852 if (spec <= UCHAR_MAX
1853 && __printf_function_table != NULL
1854 && __printf_function_table[(size_t) spec] != NULL)
1855 {
1856 const void **ptr = alloca (specs[nspecs_done].ndata_args
1857 * sizeof (const void *));
1858
1859 /* Fill in an array of pointers to the argument values. */
1860 for (unsigned int i = 0; i < specs[nspecs_done].ndata_args;
1861 ++i)
1862 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1863
1864 /* Call the function. */
1865 function_done = __printf_function_table[(size_t) spec]
1866 (s, &specs[nspecs_done].info, ptr);
1867
1868 if (function_done != -2)
1869 {
1870 /* If an error occurred we don't have information
1871 about # of chars. */
1872 if (function_done < 0)
1873 {
1874 /* Function has set errno. */
1875 done = -1;
1876 goto all_done;
1877 }
1878
1879 done_add (function_done);
1880 break;
1881 }
1882 }
1883
1884 JUMP (spec, step4_jumps);
1885
475b0b92
FW
1886#define process_arg_data args_value[specs[nspecs_done].data_arg]
1887#define process_arg_int() process_arg_data.pa_int
1888#define process_arg_long_int() process_arg_data.pa_long_int
1889#define process_arg_long_long_int() process_arg_data.pa_long_long_int
1890#define process_arg_pointer() process_arg_data.pa_pointer
1891#define process_arg_string() process_arg_data.pa_string
1892#define process_arg_unsigned_int() process_arg_data.pa_u_int
1893#define process_arg_unsigned_long_int() process_arg_data.pa_u_long_int
1894#define process_arg_unsigned_long_long_int() process_arg_data.pa_u_long_long_int
1895#define process_arg_wchar_t() process_arg_data.pa_wchar
1896#define process_arg_wstring() process_arg_data.pa_wstring
1897 process_arg ();
1898 process_string_arg ();
1899#undef process_arg_data
1900#undef process_arg_int
1901#undef process_arg_long_int
1902#undef process_arg_long_long_int
1903#undef process_arg_pointer
1904#undef process_arg_string
1905#undef process_arg_unsigned_int
1906#undef process_arg_unsigned_long_int
1907#undef process_arg_unsigned_long_long_int
1908#undef process_arg_wchar_t
1909#undef process_arg_wstring
698fb75b 1910
0a5e270b
FW
1911 LABEL (form_float):
1912 LABEL (form_floathex):
1913 {
1914 const void *ptr
1915 = (const void *) &args_value[specs[nspecs_done].data_arg];
1916 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0))
1917 {
1918 specs[nspecs_done].data_arg_type = PA_DOUBLE;
1919 specs[nspecs_done].info.is_long_double = 0;
1920 }
1921 SETUP_FLOAT128_INFO (specs[nspecs_done].info);
1922
1923 int function_done
1924 = __printf_fp_spec (s, &specs[nspecs_done].info, &ptr);
1925 if (function_done < 0)
1926 {
1927 /* Error in print handler; up to handler to set errno. */
1928 done = -1;
1929 goto all_done;
1930 }
1931 done_add (function_done);
1932 }
1933 break;
1934
698fb75b
ZW
1935 LABEL (form_unknown):
1936 {
1937 unsigned int i;
1938 const void **ptr;
1939
1940 ptr = alloca (specs[nspecs_done].ndata_args
1941 * sizeof (const void *));
1942
1943 /* Fill in an array of pointers to the argument values. */
1944 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
1945 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1946
1947 /* Call the function. */
1948 function_done = printf_unknown (s, &specs[nspecs_done].info,
1949 ptr);
1950
1951 /* If an error occurred we don't have information about #
1952 of chars. */
1953 if (function_done < 0)
1954 {
1955 /* Function has set errno. */
1956 done = -1;
1957 goto all_done;
1958 }
1959
1960 done_add (function_done);
1961 }
1962 break;
1963 }
1964
698fb75b
ZW
1965 /* Write the following constant string. */
1966 outstring (specs[nspecs_done].end_of_fmt,
1967 specs[nspecs_done].next_fmt
1968 - specs[nspecs_done].end_of_fmt);
1969 }
1970 all_done:
698fb75b
ZW
1971 scratch_buffer_free (&argsbuf);
1972 scratch_buffer_free (&specsbuf);
1973 return done;
1974}
1975\f
1976/* Handle an unknown format specifier. This prints out a canonicalized
1977 representation of the format spec itself. */
1978static int
1979printf_unknown (FILE *s, const struct printf_info *info,
1980 const void *const *args)
1981
1982{
1983 int done = 0;
1984 CHAR_T work_buffer[MAX (sizeof (info->width), sizeof (info->prec)) * 3];
1985 CHAR_T *const workend
1986 = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1987 CHAR_T *w;
1988
1989 outchar (L_('%'));
1990
1991 if (info->alt)
1992 outchar (L_('#'));
1993 if (info->group)
1994 outchar (L_('\''));
1995 if (info->showsign)
1996 outchar (L_('+'));
1997 else if (info->space)
1998 outchar (L_(' '));
1999 if (info->left)
2000 outchar (L_('-'));
2001 if (info->pad == L_('0'))
2002 outchar (L_('0'));
2003 if (info->i18n)
2004 outchar (L_('I'));
2005
2006 if (info->width != 0)
2007 {
2008 w = _itoa_word (info->width, workend, 10, 0);
2009 while (w < workend)
2010 outchar (*w++);
2011 }
2012
2013 if (info->prec != -1)
2014 {
2015 outchar (L_('.'));
2016 w = _itoa_word (info->prec, workend, 10, 0);
2017 while (w < workend)
2018 outchar (*w++);
2019 }
2020
2021 if (info->spec != L_('\0'))
2022 outchar (info->spec);
2023
2024 all_done:
2025 return done;
2026}
2027\f
2028/* Group the digits from W to REAR_PTR according to the grouping rules
2029 of the current locale. The interpretation of GROUPING is as in
2030 `struct lconv' from <locale.h>. The grouped number extends from
2031 the returned pointer until REAR_PTR. FRONT_PTR to W is used as a
2032 scratch area. */
2033static CHAR_T *
2034group_number (CHAR_T *front_ptr, CHAR_T *w, CHAR_T *rear_ptr,
2035 const char *grouping, THOUSANDS_SEP_T thousands_sep)
2036{
2037 /* Length of the current group. */
2038 int len;
2039#ifndef COMPILE_WPRINTF
2040 /* Length of the separator (in wide mode, the separator is always a
2041 single wide character). */
2042 int tlen = strlen (thousands_sep);
2043#endif
2044
2045 /* We treat all negative values like CHAR_MAX. */
2046
2047 if (*grouping == CHAR_MAX || *grouping <= 0)
2048 /* No grouping should be done. */
2049 return w;
2050
2051 len = *grouping++;
2052
2053 /* Copy existing string so that nothing gets overwritten. */
2054 memmove (front_ptr, w, (rear_ptr - w) * sizeof (CHAR_T));
2055 CHAR_T *s = front_ptr + (rear_ptr - w);
2056
2057 w = rear_ptr;
2058
2059 /* Process all characters in the string. */
2060 while (s > front_ptr)
2061 {
2062 *--w = *--s;
2063
2064 if (--len == 0 && s > front_ptr)
2065 {
2066 /* A new group begins. */
2067#ifdef COMPILE_WPRINTF
2068 if (w != s)
2069 *--w = thousands_sep;
2070 else
2071 /* Not enough room for the separator. */
2072 goto copy_rest;
2073#else
2074 int cnt = tlen;
2075 if (tlen < w - s)
2076 do
2077 *--w = thousands_sep[--cnt];
2078 while (cnt > 0);
2079 else
2080 /* Not enough room for the separator. */
2081 goto copy_rest;
2082#endif
2083
2084 if (*grouping == CHAR_MAX
2085#if CHAR_MIN < 0
2086 || *grouping < 0
2087#endif
2088 )
2089 {
2090 copy_rest:
2091 /* No further grouping to be done. Copy the rest of the
2092 number. */
db6c4935
JM
2093 w -= s - front_ptr;
2094 memmove (w, front_ptr, (s - front_ptr) * sizeof (CHAR_T));
698fb75b
ZW
2095 break;
2096 }
2097 else if (*grouping != '\0')
2098 len = *grouping++;
2099 else
2100 /* The previous grouping repeats ad infinitum. */
2101 len = grouping[-1];
2102 }
2103 }
2104 return w;
2105}
2106\f
2107/* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2108struct helper_file
2109 {
2110 struct _IO_FILE_plus _f;
2111#ifdef COMPILE_WPRINTF
2112 struct _IO_wide_data _wide_data;
2113#endif
2114 FILE *_put_stream;
2115#ifdef _IO_MTSAFE_IO
2116 _IO_lock_t lock;
2117#endif
2118 };
2119
2120static int
2121_IO_helper_overflow (FILE *s, int c)
2122{
2123 FILE *target = ((struct helper_file*) s)->_put_stream;
2124#ifdef COMPILE_WPRINTF
2125 int used = s->_wide_data->_IO_write_ptr - s->_wide_data->_IO_write_base;
2126 if (used)
2127 {
2128 size_t written = _IO_sputn (target, s->_wide_data->_IO_write_base, used);
2129 if (written == 0 || written == WEOF)
2130 return WEOF;
2131 __wmemmove (s->_wide_data->_IO_write_base,
2132 s->_wide_data->_IO_write_base + written,
2133 used - written);
2134 s->_wide_data->_IO_write_ptr -= written;
2135 }
2136#else
2137 int used = s->_IO_write_ptr - s->_IO_write_base;
2138 if (used)
2139 {
2140 size_t written = _IO_sputn (target, s->_IO_write_base, used);
2141 if (written == 0 || written == EOF)
2142 return EOF;
2143 memmove (s->_IO_write_base, s->_IO_write_base + written,
2144 used - written);
2145 s->_IO_write_ptr -= written;
2146 }
2147#endif
2148 return PUTC (c, s);
2149}
2150
2151#ifdef COMPILE_WPRINTF
2152static const struct _IO_jump_t _IO_helper_jumps libio_vtable =
2153{
2154 JUMP_INIT_DUMMY,
2155 JUMP_INIT (finish, _IO_wdefault_finish),
2156 JUMP_INIT (overflow, _IO_helper_overflow),
2157 JUMP_INIT (underflow, _IO_default_underflow),
2158 JUMP_INIT (uflow, _IO_default_uflow),
2159 JUMP_INIT (pbackfail, (_IO_pbackfail_t) _IO_wdefault_pbackfail),
2160 JUMP_INIT (xsputn, _IO_wdefault_xsputn),
2161 JUMP_INIT (xsgetn, _IO_wdefault_xsgetn),
2162 JUMP_INIT (seekoff, _IO_default_seekoff),
2163 JUMP_INIT (seekpos, _IO_default_seekpos),
2164 JUMP_INIT (setbuf, _IO_default_setbuf),
2165 JUMP_INIT (sync, _IO_default_sync),
2166 JUMP_INIT (doallocate, _IO_wdefault_doallocate),
2167 JUMP_INIT (read, _IO_default_read),
2168 JUMP_INIT (write, _IO_default_write),
2169 JUMP_INIT (seek, _IO_default_seek),
2170 JUMP_INIT (close, _IO_default_close),
2171 JUMP_INIT (stat, _IO_default_stat)
2172};
2173#else
2174static const struct _IO_jump_t _IO_helper_jumps libio_vtable =
2175{
2176 JUMP_INIT_DUMMY,
2177 JUMP_INIT (finish, _IO_default_finish),
2178 JUMP_INIT (overflow, _IO_helper_overflow),
2179 JUMP_INIT (underflow, _IO_default_underflow),
2180 JUMP_INIT (uflow, _IO_default_uflow),
2181 JUMP_INIT (pbackfail, _IO_default_pbackfail),
2182 JUMP_INIT (xsputn, _IO_default_xsputn),
2183 JUMP_INIT (xsgetn, _IO_default_xsgetn),
2184 JUMP_INIT (seekoff, _IO_default_seekoff),
2185 JUMP_INIT (seekpos, _IO_default_seekpos),
2186 JUMP_INIT (setbuf, _IO_default_setbuf),
2187 JUMP_INIT (sync, _IO_default_sync),
2188 JUMP_INIT (doallocate, _IO_default_doallocate),
2189 JUMP_INIT (read, _IO_default_read),
2190 JUMP_INIT (write, _IO_default_write),
2191 JUMP_INIT (seek, _IO_default_seek),
2192 JUMP_INIT (close, _IO_default_close),
2193 JUMP_INIT (stat, _IO_default_stat)
2194};
2195#endif
2196
2197static int
2198buffered_vfprintf (FILE *s, const CHAR_T *format, va_list args,
2199 unsigned int mode_flags)
2200{
2201 CHAR_T buf[BUFSIZ];
2202 struct helper_file helper;
2203 FILE *hp = (FILE *) &helper._f;
2204 int result, to_flush;
2205
2206 /* Orient the stream. */
2207#ifdef ORIENT
2208 ORIENT;
2209#endif
2210
2211 /* Initialize helper. */
2212 helper._put_stream = s;
2213#ifdef COMPILE_WPRINTF
2214 hp->_wide_data = &helper._wide_data;
2215 _IO_wsetp (hp, buf, buf + sizeof buf / sizeof (CHAR_T));
2216 hp->_mode = 1;
2217#else
2218 _IO_setp (hp, buf, buf + sizeof buf);
2219 hp->_mode = -1;
2220#endif
2221 hp->_flags = _IO_MAGIC|_IO_NO_READS|_IO_USER_LOCK;
2222#if _IO_JUMPS_OFFSET
2223 hp->_vtable_offset = 0;
2224#endif
2225#ifdef _IO_MTSAFE_IO
2226 hp->_lock = NULL;
2227#endif
2228 hp->_flags2 = s->_flags2;
2229 _IO_JUMPS (&helper._f) = (struct _IO_jump_t *) &_IO_helper_jumps;
2230
2231 /* Now print to helper instead. */
2232 result = vfprintf (hp, format, args, mode_flags);
2233
2234 /* Lock stream. */
2235 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, s);
2236 _IO_flockfile (s);
2237
2238 /* Now flush anything from the helper to the S. */
2239#ifdef COMPILE_WPRINTF
2240 if ((to_flush = (hp->_wide_data->_IO_write_ptr
2241 - hp->_wide_data->_IO_write_base)) > 0)
2242 {
2243 if ((int) _IO_sputn (s, hp->_wide_data->_IO_write_base, to_flush)
2244 != to_flush)
2245 result = -1;
2246 }
2247#else
2248 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
2249 {
2250 if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
2251 result = -1;
2252 }
2253#endif
2254
2255 /* Unlock the stream. */
2256 _IO_funlockfile (s);
2257 __libc_cleanup_region_end (0);
2258
2259 return result;
2260}