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