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