]> git.ipfire.org Git - thirdparty/glibc.git/blob - stdio-common/vfprintf-internal.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / stdio-common / vfprintf-internal.c
1 /* Copyright (C) 1991-2019 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 #include <libioP.h>
43
44 #ifdef COMPILE_WPRINTF
45 #include <wctype.h>
46 #endif
47
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 __set_errno (EINVAL); \
66 return -1; \
67 } \
68 } while (0)
69 #define UNBUFFERED_P(S) ((S)->_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 __vfprintf_internal
86 # define CHAR_T char
87 # define UCHAR_T unsigned char
88 # define INT_T int
89 typedef 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 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 __vfwprintf_internal
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
117 typedef 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 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. */
205 static const CHAR_T null[] = L_("(null)");
206
207 /* Size of the work_buffer variable (in characters, not bytes. */
208 enum { WORK_BUFFER_SIZE = 1000 / sizeof (CHAR_T) };
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. */
212 static 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 (work_buffer, string, workend, \
599 grouping, thousands_sep); \
600 if (use_outdigits && base == 10) \
601 string = _i18n_number_rewrite (string, workend, workend); \
602 } \
603 /* Simplify further test for num != 0. */ \
604 number.word = number.longlong != 0; \
605 } \
606 else \
607 { \
608 if (fspec == NULL) \
609 { \
610 if (is_long_num) \
611 number.word = va_arg (ap, unsigned long int); \
612 else if (is_char) \
613 number.word = (unsigned char) va_arg (ap, unsigned int); \
614 else if (!is_short) \
615 number.word = va_arg (ap, unsigned int); \
616 else \
617 number.word = (unsigned short int) va_arg (ap, unsigned int); \
618 } \
619 else \
620 if (is_long_num) \
621 number.word = args_value[fspec->data_arg].pa_u_long_int; \
622 else if (is_char) \
623 number.word = (unsigned char) \
624 args_value[fspec->data_arg].pa_u_int; \
625 else if (!is_short) \
626 number.word = args_value[fspec->data_arg].pa_u_int; \
627 else \
628 number.word = (unsigned short int) \
629 args_value[fspec->data_arg].pa_u_int; \
630 \
631 LABEL (number): \
632 if (prec < 0) \
633 /* Supply a default precision if none was given. */ \
634 prec = 1; \
635 else \
636 /* We have to take care for the '0' flag. If a precision \
637 is given it must be ignored. */ \
638 pad = L_(' '); \
639 \
640 /* If the precision is 0 and the number is 0 nothing has to \
641 be written for the number, except for the 'o' format in \
642 alternate form. */ \
643 if (prec == 0 && number.word == 0) \
644 { \
645 string = workend; \
646 if (base == 8 && alt) \
647 *--string = L_('0'); \
648 } \
649 else \
650 { \
651 /* Put the number in WORK. */ \
652 string = _itoa_word (number.word, workend, base, \
653 spec == L_('X')); \
654 if (group && grouping) \
655 string = group_number (work_buffer, string, workend, \
656 grouping, thousands_sep); \
657 if (use_outdigits && base == 10) \
658 string = _i18n_number_rewrite (string, workend, workend); \
659 } \
660 } \
661 \
662 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
663 /* Add octal marker. */ \
664 *--string = L_('0'); \
665 \
666 prec = MAX (0, prec - (workend - string)); \
667 \
668 if (!left) \
669 { \
670 width -= workend - string + prec; \
671 \
672 if (number.word != 0 && alt && base == 16) \
673 /* Account for 0X hex marker. */ \
674 width -= 2; \
675 \
676 if (is_negative || showsign || space) \
677 --width; \
678 \
679 if (pad == L_(' ')) \
680 { \
681 PAD (L_(' ')); \
682 width = 0; \
683 } \
684 \
685 if (is_negative) \
686 outchar (L_('-')); \
687 else if (showsign) \
688 outchar (L_('+')); \
689 else if (space) \
690 outchar (L_(' ')); \
691 \
692 if (number.word != 0 && alt && base == 16) \
693 { \
694 outchar (L_('0')); \
695 outchar (spec); \
696 } \
697 \
698 width += prec; \
699 PAD (L_('0')); \
700 \
701 outstring (string, workend - string); \
702 \
703 break; \
704 } \
705 else \
706 { \
707 if (is_negative) \
708 { \
709 outchar (L_('-')); \
710 --width; \
711 } \
712 else if (showsign) \
713 { \
714 outchar (L_('+')); \
715 --width; \
716 } \
717 else if (space) \
718 { \
719 outchar (L_(' ')); \
720 --width; \
721 } \
722 \
723 if (number.word != 0 && alt && base == 16) \
724 { \
725 outchar (L_('0')); \
726 outchar (spec); \
727 width -= 2; \
728 } \
729 \
730 width -= workend - string + prec; \
731 \
732 if (prec > 0) \
733 { \
734 int temp = width; \
735 width = prec; \
736 PAD (L_('0')); \
737 width = temp; \
738 } \
739 \
740 outstring (string, workend - string); \
741 \
742 PAD (L_(' ')); \
743 break; \
744 } \
745 \
746 LABEL (form_float): \
747 { \
748 /* Floating-point number. This is handled by printf_fp.c. */ \
749 const void *ptr; \
750 int function_done; \
751 \
752 if (fspec == NULL) \
753 { \
754 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0)) \
755 is_long_double = 0; \
756 \
757 struct printf_info info = { .prec = prec, \
758 .width = width, \
759 .spec = spec, \
760 .is_long_double = is_long_double, \
761 .is_short = is_short, \
762 .is_long = is_long, \
763 .alt = alt, \
764 .space = space, \
765 .left = left, \
766 .showsign = showsign, \
767 .group = group, \
768 .pad = pad, \
769 .extra = 0, \
770 .i18n = use_outdigits, \
771 .wide = sizeof (CHAR_T) != 1, \
772 .is_binary128 = 0}; \
773 \
774 if (is_long_double) \
775 the_arg.pa_long_double = va_arg (ap, long double); \
776 else \
777 the_arg.pa_double = va_arg (ap, double); \
778 ptr = (const void *) &the_arg; \
779 \
780 function_done = __printf_fp (s, &info, &ptr); \
781 } \
782 else \
783 { \
784 ptr = (const void *) &args_value[fspec->data_arg]; \
785 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0)) \
786 { \
787 fspec->data_arg_type = PA_DOUBLE; \
788 fspec->info.is_long_double = 0; \
789 } \
790 /* Not supported by *printf functions. */ \
791 fspec->info.is_binary128 = 0; \
792 \
793 function_done = __printf_fp (s, &fspec->info, &ptr); \
794 } \
795 \
796 if (function_done < 0) \
797 { \
798 /* Error in print handler; up to handler to set errno. */ \
799 done = -1; \
800 goto all_done; \
801 } \
802 \
803 done_add (function_done); \
804 } \
805 break; \
806 \
807 LABEL (form_floathex): \
808 { \
809 /* Floating point number printed as hexadecimal number. */ \
810 const void *ptr; \
811 int function_done; \
812 \
813 if (fspec == NULL) \
814 { \
815 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0)) \
816 is_long_double = 0; \
817 \
818 struct printf_info info = { .prec = prec, \
819 .width = width, \
820 .spec = spec, \
821 .is_long_double = is_long_double, \
822 .is_short = is_short, \
823 .is_long = is_long, \
824 .alt = alt, \
825 .space = space, \
826 .left = left, \
827 .showsign = showsign, \
828 .group = group, \
829 .pad = pad, \
830 .extra = 0, \
831 .wide = sizeof (CHAR_T) != 1, \
832 .is_binary128 = 0}; \
833 \
834 if (is_long_double) \
835 the_arg.pa_long_double = va_arg (ap, long double); \
836 else \
837 the_arg.pa_double = va_arg (ap, double); \
838 ptr = (const void *) &the_arg; \
839 \
840 function_done = __printf_fphex (s, &info, &ptr); \
841 } \
842 else \
843 { \
844 ptr = (const void *) &args_value[fspec->data_arg]; \
845 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0)) \
846 fspec->info.is_long_double = 0; \
847 /* Not supported by *printf functions. */ \
848 fspec->info.is_binary128 = 0; \
849 \
850 function_done = __printf_fphex (s, &fspec->info, &ptr); \
851 } \
852 \
853 if (function_done < 0) \
854 { \
855 /* Error in print handler; up to handler to set errno. */ \
856 done = -1; \
857 goto all_done; \
858 } \
859 \
860 done_add (function_done); \
861 } \
862 break; \
863 \
864 LABEL (form_pointer): \
865 /* Generic pointer. */ \
866 { \
867 const void *ptr; \
868 if (fspec == NULL) \
869 ptr = va_arg (ap, void *); \
870 else \
871 ptr = args_value[fspec->data_arg].pa_pointer; \
872 if (ptr != NULL) \
873 { \
874 /* If the pointer is not NULL, write it as a %#x spec. */ \
875 base = 16; \
876 number.word = (unsigned long int) ptr; \
877 is_negative = 0; \
878 alt = 1; \
879 group = 0; \
880 spec = L_('x'); \
881 goto LABEL (number); \
882 } \
883 else \
884 { \
885 /* Write "(nil)" for a nil pointer. */ \
886 string = (CHAR_T *) L_("(nil)"); \
887 /* Make sure the full string "(nil)" is printed. */ \
888 if (prec < 5) \
889 prec = 5; \
890 /* This is a wide string iff compiling wprintf. */ \
891 is_long = sizeof (CHAR_T) > 1; \
892 goto LABEL (print_string); \
893 } \
894 } \
895 /* NOTREACHED */ \
896 \
897 LABEL (form_number): \
898 if ((mode_flags & PRINTF_FORTIFY) != 0) \
899 { \
900 if (! readonly_format) \
901 { \
902 extern int __readonly_area (const void *, size_t) \
903 attribute_hidden; \
904 readonly_format \
905 = __readonly_area (format, ((STR_LEN (format) + 1) \
906 * sizeof (CHAR_T))); \
907 } \
908 if (readonly_format < 0) \
909 __libc_fatal ("*** %n in writable segment detected ***\n"); \
910 } \
911 /* Answer the count of characters written. */ \
912 if (fspec == NULL) \
913 { \
914 if (is_longlong) \
915 *(long long int *) va_arg (ap, void *) = done; \
916 else if (is_long_num) \
917 *(long int *) va_arg (ap, void *) = done; \
918 else if (is_char) \
919 *(char *) va_arg (ap, void *) = done; \
920 else if (!is_short) \
921 *(int *) va_arg (ap, void *) = done; \
922 else \
923 *(short int *) va_arg (ap, void *) = done; \
924 } \
925 else \
926 if (is_longlong) \
927 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
928 else if (is_long_num) \
929 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
930 else if (is_char) \
931 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
932 else if (!is_short) \
933 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
934 else \
935 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
936 break; \
937 \
938 LABEL (form_strerror): \
939 /* Print description of error ERRNO. */ \
940 string = \
941 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
942 WORK_BUFFER_SIZE * sizeof (CHAR_T)); \
943 is_long = 0; /* This is no wide-char string. */ \
944 goto LABEL (print_string)
945
946 #ifdef COMPILE_WPRINTF
947 # define process_string_arg(fspec) \
948 LABEL (form_character): \
949 /* Character. */ \
950 if (is_long) \
951 goto LABEL (form_wcharacter); \
952 --width; /* Account for the character itself. */ \
953 if (!left) \
954 PAD (L' '); \
955 if (fspec == NULL) \
956 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
957 else \
958 outchar (__btowc ((unsigned char) \
959 args_value[fspec->data_arg].pa_int)); \
960 if (left) \
961 PAD (L' '); \
962 break; \
963 \
964 LABEL (form_wcharacter): \
965 { \
966 /* Wide character. */ \
967 --width; \
968 if (!left) \
969 PAD (L' '); \
970 if (fspec == NULL) \
971 outchar (va_arg (ap, wchar_t)); \
972 else \
973 outchar (args_value[fspec->data_arg].pa_wchar); \
974 if (left) \
975 PAD (L' '); \
976 } \
977 break; \
978 \
979 LABEL (form_string): \
980 { \
981 size_t len; \
982 int string_malloced; \
983 \
984 /* The string argument could in fact be `char *' or `wchar_t *'. \
985 But this should not make a difference here. */ \
986 if (fspec == NULL) \
987 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
988 else \
989 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
990 \
991 /* Entry point for printing other strings. */ \
992 LABEL (print_string): \
993 \
994 string_malloced = 0; \
995 if (string == NULL) \
996 { \
997 /* Write "(null)" if there's space. */ \
998 if (prec == -1 || prec >= (int) array_length (null) - 1) \
999 { \
1000 string = (CHAR_T *) null; \
1001 len = array_length (null) - 1; \
1002 } \
1003 else \
1004 { \
1005 string = (CHAR_T *) L""; \
1006 len = 0; \
1007 } \
1008 } \
1009 else if (!is_long && spec != L_('S')) \
1010 { \
1011 /* This is complicated. We have to transform the multibyte \
1012 string into a wide character string. */ \
1013 const char *mbs = (const char *) string; \
1014 mbstate_t mbstate; \
1015 \
1016 len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
1017 \
1018 /* Allocate dynamically an array which definitely is long \
1019 enough for the wide character version. Each byte in the \
1020 multi-byte string can produce at most one wide character. */ \
1021 if (__glibc_unlikely (len > SIZE_MAX / sizeof (wchar_t))) \
1022 { \
1023 __set_errno (EOVERFLOW); \
1024 done = -1; \
1025 goto all_done; \
1026 } \
1027 else if (__libc_use_alloca (len * sizeof (wchar_t))) \
1028 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1029 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1030 == NULL) \
1031 { \
1032 done = -1; \
1033 goto all_done; \
1034 } \
1035 else \
1036 string_malloced = 1; \
1037 \
1038 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1039 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1040 if (len == (size_t) -1) \
1041 { \
1042 /* Illegal multibyte character. */ \
1043 done = -1; \
1044 goto all_done; \
1045 } \
1046 } \
1047 else \
1048 { \
1049 if (prec != -1) \
1050 /* Search for the end of the string, but don't search past \
1051 the length specified by the precision. */ \
1052 len = __wcsnlen (string, prec); \
1053 else \
1054 len = __wcslen (string); \
1055 } \
1056 \
1057 if ((width -= len) < 0) \
1058 { \
1059 outstring (string, len); \
1060 break; \
1061 } \
1062 \
1063 if (!left) \
1064 PAD (L' '); \
1065 outstring (string, len); \
1066 if (left) \
1067 PAD (L' '); \
1068 if (__glibc_unlikely (string_malloced)) \
1069 free (string); \
1070 } \
1071 break;
1072 #else
1073 # define process_string_arg(fspec) \
1074 LABEL (form_character): \
1075 /* Character. */ \
1076 if (is_long) \
1077 goto LABEL (form_wcharacter); \
1078 --width; /* Account for the character itself. */ \
1079 if (!left) \
1080 PAD (' '); \
1081 if (fspec == NULL) \
1082 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1083 else \
1084 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1085 if (left) \
1086 PAD (' '); \
1087 break; \
1088 \
1089 LABEL (form_wcharacter): \
1090 { \
1091 /* Wide character. */ \
1092 char buf[MB_LEN_MAX]; \
1093 mbstate_t mbstate; \
1094 size_t len; \
1095 \
1096 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1097 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1098 : args_value[fspec->data_arg].pa_wchar), \
1099 &mbstate); \
1100 if (len == (size_t) -1) \
1101 { \
1102 /* Something went wrong during the conversion. Bail out. */ \
1103 done = -1; \
1104 goto all_done; \
1105 } \
1106 width -= len; \
1107 if (!left) \
1108 PAD (' '); \
1109 outstring (buf, len); \
1110 if (left) \
1111 PAD (' '); \
1112 } \
1113 break; \
1114 \
1115 LABEL (form_string): \
1116 { \
1117 size_t len; \
1118 int string_malloced; \
1119 \
1120 /* The string argument could in fact be `char *' or `wchar_t *'. \
1121 But this should not make a difference here. */ \
1122 if (fspec == NULL) \
1123 string = (char *) va_arg (ap, const char *); \
1124 else \
1125 string = (char *) args_value[fspec->data_arg].pa_string; \
1126 \
1127 /* Entry point for printing other strings. */ \
1128 LABEL (print_string): \
1129 \
1130 string_malloced = 0; \
1131 if (string == NULL) \
1132 { \
1133 /* Write "(null)" if there's space. */ \
1134 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1135 { \
1136 string = (char *) null; \
1137 len = sizeof (null) - 1; \
1138 } \
1139 else \
1140 { \
1141 string = (char *) ""; \
1142 len = 0; \
1143 } \
1144 } \
1145 else if (!is_long && spec != L_('S')) \
1146 { \
1147 if (prec != -1) \
1148 /* Search for the end of the string, but don't search past \
1149 the length (in bytes) specified by the precision. */ \
1150 len = __strnlen (string, prec); \
1151 else \
1152 len = strlen (string); \
1153 } \
1154 else \
1155 { \
1156 const wchar_t *s2 = (const wchar_t *) string; \
1157 mbstate_t mbstate; \
1158 \
1159 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1160 \
1161 if (prec >= 0) \
1162 { \
1163 /* The string `s2' might not be NUL terminated. */ \
1164 if (__libc_use_alloca (prec)) \
1165 string = (char *) alloca (prec); \
1166 else if ((string = (char *) malloc (prec)) == NULL) \
1167 { \
1168 done = -1; \
1169 goto all_done; \
1170 } \
1171 else \
1172 string_malloced = 1; \
1173 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1174 } \
1175 else \
1176 { \
1177 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1178 if (len != (size_t) -1) \
1179 { \
1180 assert (__mbsinit (&mbstate)); \
1181 s2 = (const wchar_t *) string; \
1182 if (__libc_use_alloca (len + 1)) \
1183 string = (char *) alloca (len + 1); \
1184 else if ((string = (char *) malloc (len + 1)) == NULL) \
1185 { \
1186 done = -1; \
1187 goto all_done; \
1188 } \
1189 else \
1190 string_malloced = 1; \
1191 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1192 } \
1193 } \
1194 \
1195 if (len == (size_t) -1) \
1196 { \
1197 /* Illegal wide-character string. */ \
1198 done = -1; \
1199 goto all_done; \
1200 } \
1201 } \
1202 \
1203 if ((width -= len) < 0) \
1204 { \
1205 outstring (string, len); \
1206 break; \
1207 } \
1208 \
1209 if (!left) \
1210 PAD (' '); \
1211 outstring (string, len); \
1212 if (left) \
1213 PAD (' '); \
1214 if (__glibc_unlikely (string_malloced)) \
1215 free (string); \
1216 } \
1217 break;
1218 #endif
1219
1220 /* Helper function to provide temporary buffering for unbuffered streams. */
1221 static int buffered_vfprintf (FILE *stream, const CHAR_T *fmt, va_list,
1222 unsigned int)
1223 __THROW __attribute__ ((noinline));
1224
1225 /* Handle positional format specifiers. */
1226 static int printf_positional (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,
1232 THOUSANDS_SEP_T thousands_sep,
1233 unsigned int mode_flags);
1234
1235 /* Handle unknown format specifier. */
1236 static int printf_unknown (FILE *, const struct printf_info *,
1237 const void *const *) __THROW;
1238
1239 /* Group digits of number string. */
1240 static CHAR_T *group_number (CHAR_T *, CHAR_T *, CHAR_T *, const char *,
1241 THOUSANDS_SEP_T);
1242
1243 /* The function itself. */
1244 int
1245 vfprintf (FILE *s, const CHAR_T *format, va_list ap, unsigned int mode_flags)
1246 {
1247 /* The character used as thousands separator. */
1248 THOUSANDS_SEP_T thousands_sep = 0;
1249
1250 /* The string describing the size of groups of digits. */
1251 const char *grouping;
1252
1253 /* Place to accumulate the result. */
1254 int done;
1255
1256 /* Current character in format string. */
1257 const UCHAR_T *f;
1258
1259 /* End of leading constant string. */
1260 const UCHAR_T *lead_str_end;
1261
1262 /* Points to next format specifier. */
1263 const UCHAR_T *end_of_spec;
1264
1265 /* Buffer intermediate results. */
1266 CHAR_T work_buffer[WORK_BUFFER_SIZE];
1267 CHAR_T *workstart = NULL;
1268 CHAR_T *workend;
1269
1270 /* We have to save the original argument pointer. */
1271 va_list ap_save;
1272
1273 /* Count number of specifiers we already processed. */
1274 int nspecs_done;
1275
1276 /* For the %m format we may need the current `errno' value. */
1277 int save_errno = errno;
1278
1279 /* 1 if format is in read-only memory, -1 if it is in writable memory,
1280 0 if unknown. */
1281 int readonly_format = 0;
1282
1283 /* Orient the stream. */
1284 #ifdef ORIENT
1285 ORIENT;
1286 #endif
1287
1288 /* Sanity check of arguments. */
1289 ARGCHECK (s, format);
1290
1291 #ifdef ORIENT
1292 /* Check for correct orientation. */
1293 if (_IO_vtable_offset (s) == 0 &&
1294 _IO_fwide (s, sizeof (CHAR_T) == 1 ? -1 : 1)
1295 != (sizeof (CHAR_T) == 1 ? -1 : 1))
1296 /* The stream is already oriented otherwise. */
1297 return EOF;
1298 #endif
1299
1300 if (UNBUFFERED_P (s))
1301 /* Use a helper function which will allocate a local temporary buffer
1302 for the stream and then call us again. */
1303 return buffered_vfprintf (s, format, ap, mode_flags);
1304
1305 /* Initialize local variables. */
1306 done = 0;
1307 grouping = (const char *) -1;
1308 #ifdef __va_copy
1309 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1310 since on some systems `va_list' is not an integral type. */
1311 __va_copy (ap_save, ap);
1312 #else
1313 ap_save = ap;
1314 #endif
1315 nspecs_done = 0;
1316
1317 #ifdef COMPILE_WPRINTF
1318 /* Find the first format specifier. */
1319 f = lead_str_end = __find_specwc ((const UCHAR_T *) format);
1320 #else
1321 /* Find the first format specifier. */
1322 f = lead_str_end = __find_specmb ((const UCHAR_T *) format);
1323 #endif
1324
1325 /* Lock stream. */
1326 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
1327 _IO_flockfile (s);
1328
1329 /* Write the literal text before the first format. */
1330 outstring ((const UCHAR_T *) format,
1331 lead_str_end - (const UCHAR_T *) format);
1332
1333 /* If we only have to print a simple string, return now. */
1334 if (*f == L_('\0'))
1335 goto all_done;
1336
1337 /* Use the slow path in case any printf handler is registered. */
1338 if (__glibc_unlikely (__printf_function_table != NULL
1339 || __printf_modifier_table != NULL
1340 || __printf_va_arg_table != NULL))
1341 goto do_positional;
1342
1343 /* Process whole format string. */
1344 do
1345 {
1346 STEP0_3_TABLE;
1347 STEP4_TABLE;
1348
1349 union printf_arg *args_value; /* This is not used here but ... */
1350 int is_negative; /* Flag for negative number. */
1351 union
1352 {
1353 unsigned long long int longlong;
1354 unsigned long int word;
1355 } number;
1356 int base;
1357 union printf_arg the_arg;
1358 CHAR_T *string; /* Pointer to argument string. */
1359 int alt = 0; /* Alternate format. */
1360 int space = 0; /* Use space prefix if no sign is needed. */
1361 int left = 0; /* Left-justify output. */
1362 int showsign = 0; /* Always begin with plus or minus sign. */
1363 int group = 0; /* Print numbers according grouping rules. */
1364 int is_long_double = 0; /* Argument is long double/ long long int. */
1365 int is_short = 0; /* Argument is short int. */
1366 int is_long = 0; /* Argument is long int. */
1367 int is_char = 0; /* Argument is promoted (unsigned) char. */
1368 int width = 0; /* Width of output; 0 means none specified. */
1369 int prec = -1; /* Precision of output; -1 means none specified. */
1370 /* This flag is set by the 'I' modifier and selects the use of the
1371 `outdigits' as determined by the current locale. */
1372 int use_outdigits = 0;
1373 UCHAR_T pad = L_(' ');/* Padding character. */
1374 CHAR_T spec;
1375
1376 workstart = NULL;
1377 workend = work_buffer + WORK_BUFFER_SIZE;
1378
1379 /* Get current character in format string. */
1380 JUMP (*++f, step0_jumps);
1381
1382 /* ' ' flag. */
1383 LABEL (flag_space):
1384 space = 1;
1385 JUMP (*++f, step0_jumps);
1386
1387 /* '+' flag. */
1388 LABEL (flag_plus):
1389 showsign = 1;
1390 JUMP (*++f, step0_jumps);
1391
1392 /* The '-' flag. */
1393 LABEL (flag_minus):
1394 left = 1;
1395 pad = L_(' ');
1396 JUMP (*++f, step0_jumps);
1397
1398 /* The '#' flag. */
1399 LABEL (flag_hash):
1400 alt = 1;
1401 JUMP (*++f, step0_jumps);
1402
1403 /* The '0' flag. */
1404 LABEL (flag_zero):
1405 if (!left)
1406 pad = L_('0');
1407 JUMP (*++f, step0_jumps);
1408
1409 /* The '\'' flag. */
1410 LABEL (flag_quote):
1411 group = 1;
1412
1413 if (grouping == (const char *) -1)
1414 {
1415 #ifdef COMPILE_WPRINTF
1416 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1417 _NL_NUMERIC_THOUSANDS_SEP_WC);
1418 #else
1419 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1420 #endif
1421
1422 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1423 if (*grouping == '\0' || *grouping == CHAR_MAX
1424 #ifdef COMPILE_WPRINTF
1425 || thousands_sep == L'\0'
1426 #else
1427 || *thousands_sep == '\0'
1428 #endif
1429 )
1430 grouping = NULL;
1431 }
1432 JUMP (*++f, step0_jumps);
1433
1434 LABEL (flag_i18n):
1435 use_outdigits = 1;
1436 JUMP (*++f, step0_jumps);
1437
1438 /* Get width from argument. */
1439 LABEL (width_asterics):
1440 {
1441 const UCHAR_T *tmp; /* Temporary value. */
1442
1443 tmp = ++f;
1444 if (ISDIGIT (*tmp))
1445 {
1446 int pos = read_int (&tmp);
1447
1448 if (pos == -1)
1449 {
1450 __set_errno (EOVERFLOW);
1451 done = -1;
1452 goto all_done;
1453 }
1454
1455 if (pos && *tmp == L_('$'))
1456 /* The width comes from a positional parameter. */
1457 goto do_positional;
1458 }
1459 width = va_arg (ap, int);
1460
1461 /* Negative width means left justified. */
1462 if (width < 0)
1463 {
1464 width = -width;
1465 pad = L_(' ');
1466 left = 1;
1467 }
1468
1469 if (__glibc_unlikely (width >= INT_MAX / sizeof (CHAR_T) - EXTSIZ))
1470 {
1471 __set_errno (EOVERFLOW);
1472 done = -1;
1473 goto all_done;
1474 }
1475
1476 if (width >= WORK_BUFFER_SIZE - EXTSIZ)
1477 {
1478 /* We have to use a special buffer. */
1479 size_t needed = ((size_t) width + EXTSIZ) * sizeof (CHAR_T);
1480 if (__libc_use_alloca (needed))
1481 workend = (CHAR_T *) alloca (needed) + width + EXTSIZ;
1482 else
1483 {
1484 workstart = (CHAR_T *) malloc (needed);
1485 if (workstart == NULL)
1486 {
1487 done = -1;
1488 goto all_done;
1489 }
1490 workend = workstart + width + EXTSIZ;
1491 }
1492 }
1493 }
1494 JUMP (*f, step1_jumps);
1495
1496 /* Given width in format string. */
1497 LABEL (width):
1498 width = read_int (&f);
1499
1500 if (__glibc_unlikely (width == -1
1501 || width >= INT_MAX / sizeof (CHAR_T) - EXTSIZ))
1502 {
1503 __set_errno (EOVERFLOW);
1504 done = -1;
1505 goto all_done;
1506 }
1507
1508 if (width >= WORK_BUFFER_SIZE - EXTSIZ)
1509 {
1510 /* We have to use a special buffer. */
1511 size_t needed = ((size_t) width + EXTSIZ) * sizeof (CHAR_T);
1512 if (__libc_use_alloca (needed))
1513 workend = (CHAR_T *) alloca (needed) + width + EXTSIZ;
1514 else
1515 {
1516 workstart = (CHAR_T *) malloc (needed);
1517 if (workstart == NULL)
1518 {
1519 done = -1;
1520 goto all_done;
1521 }
1522 workend = workstart + width + EXTSIZ;
1523 }
1524 }
1525 if (*f == L_('$'))
1526 /* Oh, oh. The argument comes from a positional parameter. */
1527 goto do_positional;
1528 JUMP (*f, step1_jumps);
1529
1530 LABEL (precision):
1531 ++f;
1532 if (*f == L_('*'))
1533 {
1534 const UCHAR_T *tmp; /* Temporary value. */
1535
1536 tmp = ++f;
1537 if (ISDIGIT (*tmp))
1538 {
1539 int pos = read_int (&tmp);
1540
1541 if (pos == -1)
1542 {
1543 __set_errno (EOVERFLOW);
1544 done = -1;
1545 goto all_done;
1546 }
1547
1548 if (pos && *tmp == L_('$'))
1549 /* The precision comes from a positional parameter. */
1550 goto do_positional;
1551 }
1552 prec = va_arg (ap, int);
1553
1554 /* If the precision is negative the precision is omitted. */
1555 if (prec < 0)
1556 prec = -1;
1557 }
1558 else if (ISDIGIT (*f))
1559 {
1560 prec = read_int (&f);
1561
1562 /* The precision was specified in this case as an extremely
1563 large positive value. */
1564 if (prec == -1)
1565 {
1566 __set_errno (EOVERFLOW);
1567 done = -1;
1568 goto all_done;
1569 }
1570 }
1571 else
1572 prec = 0;
1573 if (prec > width && prec > WORK_BUFFER_SIZE - EXTSIZ)
1574 {
1575 /* Deallocate any previously allocated buffer because it is
1576 too small. */
1577 if (__glibc_unlikely (workstart != NULL))
1578 free (workstart);
1579 workstart = NULL;
1580 if (__glibc_unlikely (prec >= INT_MAX / sizeof (CHAR_T) - EXTSIZ))
1581 {
1582 __set_errno (EOVERFLOW);
1583 done = -1;
1584 goto all_done;
1585 }
1586 size_t needed = ((size_t) prec + EXTSIZ) * sizeof (CHAR_T);
1587
1588 if (__libc_use_alloca (needed))
1589 workend = (CHAR_T *) alloca (needed) + prec + EXTSIZ;
1590 else
1591 {
1592 workstart = (CHAR_T *) malloc (needed);
1593 if (workstart == NULL)
1594 {
1595 done = -1;
1596 goto all_done;
1597 }
1598 workend = workstart + prec + EXTSIZ;
1599 }
1600 }
1601 JUMP (*f, step2_jumps);
1602
1603 /* Process 'h' modifier. There might another 'h' following. */
1604 LABEL (mod_half):
1605 is_short = 1;
1606 JUMP (*++f, step3a_jumps);
1607
1608 /* Process 'hh' modifier. */
1609 LABEL (mod_halfhalf):
1610 is_short = 0;
1611 is_char = 1;
1612 JUMP (*++f, step4_jumps);
1613
1614 /* Process 'l' modifier. There might another 'l' following. */
1615 LABEL (mod_long):
1616 is_long = 1;
1617 JUMP (*++f, step3b_jumps);
1618
1619 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1620 allowed to follow. */
1621 LABEL (mod_longlong):
1622 is_long_double = 1;
1623 is_long = 1;
1624 JUMP (*++f, step4_jumps);
1625
1626 LABEL (mod_size_t):
1627 is_long_double = sizeof (size_t) > sizeof (unsigned long int);
1628 is_long = sizeof (size_t) > sizeof (unsigned int);
1629 JUMP (*++f, step4_jumps);
1630
1631 LABEL (mod_ptrdiff_t):
1632 is_long_double = sizeof (ptrdiff_t) > sizeof (unsigned long int);
1633 is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
1634 JUMP (*++f, step4_jumps);
1635
1636 LABEL (mod_intmax_t):
1637 is_long_double = sizeof (intmax_t) > sizeof (unsigned long int);
1638 is_long = sizeof (intmax_t) > sizeof (unsigned int);
1639 JUMP (*++f, step4_jumps);
1640
1641 /* Process current format. */
1642 while (1)
1643 {
1644 process_arg (((struct printf_spec *) NULL));
1645 process_string_arg (((struct printf_spec *) NULL));
1646
1647 LABEL (form_unknown):
1648 if (spec == L_('\0'))
1649 {
1650 /* The format string ended before the specifier is complete. */
1651 __set_errno (EINVAL);
1652 done = -1;
1653 goto all_done;
1654 }
1655
1656 /* If we are in the fast loop force entering the complicated
1657 one. */
1658 goto do_positional;
1659 }
1660
1661 /* The format is correctly handled. */
1662 ++nspecs_done;
1663
1664 if (__glibc_unlikely (workstart != NULL))
1665 free (workstart);
1666 workstart = NULL;
1667
1668 /* Look for next format specifier. */
1669 #ifdef COMPILE_WPRINTF
1670 f = __find_specwc ((end_of_spec = ++f));
1671 #else
1672 f = __find_specmb ((end_of_spec = ++f));
1673 #endif
1674
1675 /* Write the following constant string. */
1676 outstring (end_of_spec, f - end_of_spec);
1677 }
1678 while (*f != L_('\0'));
1679
1680 /* Unlock stream and return. */
1681 goto all_done;
1682
1683 /* Hand off processing for positional parameters. */
1684 do_positional:
1685 if (__glibc_unlikely (workstart != NULL))
1686 {
1687 free (workstart);
1688 workstart = NULL;
1689 }
1690 done = printf_positional (s, format, readonly_format, ap, &ap_save,
1691 done, nspecs_done, lead_str_end, work_buffer,
1692 save_errno, grouping, thousands_sep, mode_flags);
1693
1694 all_done:
1695 if (__glibc_unlikely (workstart != NULL))
1696 free (workstart);
1697 /* Unlock the stream. */
1698 _IO_funlockfile (s);
1699 _IO_cleanup_region_end (0);
1700
1701 return done;
1702 }
1703 \f
1704 static int
1705 printf_positional (FILE *s, const CHAR_T *format, int readonly_format,
1706 va_list ap, va_list *ap_savep, int done, int nspecs_done,
1707 const UCHAR_T *lead_str_end,
1708 CHAR_T *work_buffer, int save_errno,
1709 const char *grouping, THOUSANDS_SEP_T thousands_sep,
1710 unsigned int mode_flags)
1711 {
1712 /* For positional argument handling. */
1713 struct scratch_buffer specsbuf;
1714 scratch_buffer_init (&specsbuf);
1715 struct printf_spec *specs = specsbuf.data;
1716 size_t specs_limit = specsbuf.length / sizeof (specs[0]);
1717
1718 /* Used as a backing store for args_value, args_size, args_type
1719 below. */
1720 struct scratch_buffer argsbuf;
1721 scratch_buffer_init (&argsbuf);
1722
1723 /* Array with information about the needed arguments. This has to
1724 be dynamically extensible. */
1725 size_t nspecs = 0;
1726
1727 /* The number of arguments the format string requests. This will
1728 determine the size of the array needed to store the argument
1729 attributes. */
1730 size_t nargs = 0;
1731
1732 /* Positional parameters refer to arguments directly. This could
1733 also determine the maximum number of arguments. Track the
1734 maximum number. */
1735 size_t max_ref_arg = 0;
1736
1737 /* Just a counter. */
1738 size_t cnt;
1739
1740 CHAR_T *workstart = NULL;
1741
1742 if (grouping == (const char *) -1)
1743 {
1744 #ifdef COMPILE_WPRINTF
1745 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1746 _NL_NUMERIC_THOUSANDS_SEP_WC);
1747 #else
1748 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1749 #endif
1750
1751 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1752 if (*grouping == '\0' || *grouping == CHAR_MAX)
1753 grouping = NULL;
1754 }
1755
1756 for (const UCHAR_T *f = lead_str_end; *f != L_('\0');
1757 f = specs[nspecs++].next_fmt)
1758 {
1759 if (nspecs == specs_limit)
1760 {
1761 if (!scratch_buffer_grow_preserve (&specsbuf))
1762 {
1763 done = -1;
1764 goto all_done;
1765 }
1766 specs = specsbuf.data;
1767 specs_limit = specsbuf.length / sizeof (specs[0]);
1768 }
1769
1770 /* Parse the format specifier. */
1771 #ifdef COMPILE_WPRINTF
1772 nargs += __parse_one_specwc (f, nargs, &specs[nspecs], &max_ref_arg);
1773 #else
1774 nargs += __parse_one_specmb (f, nargs, &specs[nspecs], &max_ref_arg);
1775 #endif
1776 }
1777
1778 /* Determine the number of arguments the format string consumes. */
1779 nargs = MAX (nargs, max_ref_arg);
1780
1781 union printf_arg *args_value;
1782 int *args_size;
1783 int *args_type;
1784 {
1785 /* Calculate total size needed to represent a single argument
1786 across all three argument-related arrays. */
1787 size_t bytes_per_arg
1788 = sizeof (*args_value) + sizeof (*args_size) + sizeof (*args_type);
1789 if (!scratch_buffer_set_array_size (&argsbuf, nargs, bytes_per_arg))
1790 {
1791 done = -1;
1792 goto all_done;
1793 }
1794 args_value = argsbuf.data;
1795 /* Set up the remaining two arrays to each point past the end of
1796 the prior array, since space for all three has been allocated
1797 now. */
1798 args_size = &args_value[nargs].pa_int;
1799 args_type = &args_size[nargs];
1800 memset (args_type, (mode_flags & PRINTF_FORTIFY) != 0 ? '\xff' : '\0',
1801 nargs * sizeof (*args_type));
1802 }
1803
1804 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1805 still zero after this loop, format is invalid. For now we
1806 simply use 0 as the value. */
1807
1808 /* Fill in the types of all the arguments. */
1809 for (cnt = 0; cnt < nspecs; ++cnt)
1810 {
1811 /* If the width is determined by an argument this is an int. */
1812 if (specs[cnt].width_arg != -1)
1813 args_type[specs[cnt].width_arg] = PA_INT;
1814
1815 /* If the precision is determined by an argument this is an int. */
1816 if (specs[cnt].prec_arg != -1)
1817 args_type[specs[cnt].prec_arg] = PA_INT;
1818
1819 switch (specs[cnt].ndata_args)
1820 {
1821 case 0: /* No arguments. */
1822 break;
1823 case 1: /* One argument; we already have the
1824 type and size. */
1825 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1826 args_size[specs[cnt].data_arg] = specs[cnt].size;
1827 break;
1828 default:
1829 /* We have more than one argument for this format spec.
1830 We must call the arginfo function again to determine
1831 all the types. */
1832 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1833 (&specs[cnt].info,
1834 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg],
1835 &args_size[specs[cnt].data_arg]);
1836 break;
1837 }
1838 }
1839
1840 /* Now we know all the types and the order. Fill in the argument
1841 values. */
1842 for (cnt = 0; cnt < nargs; ++cnt)
1843 switch (args_type[cnt])
1844 {
1845 #define T(tag, mem, type) \
1846 case tag: \
1847 args_value[cnt].mem = va_arg (*ap_savep, type); \
1848 break
1849
1850 T (PA_WCHAR, pa_wchar, wint_t);
1851 case PA_CHAR: /* Promoted. */
1852 case PA_INT|PA_FLAG_SHORT: /* Promoted. */
1853 #if LONG_MAX == INT_MAX
1854 case PA_INT|PA_FLAG_LONG:
1855 #endif
1856 T (PA_INT, pa_int, int);
1857 #if LONG_MAX == LONG_LONG_MAX
1858 case PA_INT|PA_FLAG_LONG:
1859 #endif
1860 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1861 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1862 # error "he?"
1863 #endif
1864 case PA_FLOAT: /* Promoted. */
1865 T (PA_DOUBLE, pa_double, double);
1866 case PA_DOUBLE|PA_FLAG_LONG_DOUBLE:
1867 if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0))
1868 {
1869 args_value[cnt].pa_double = va_arg (*ap_savep, double);
1870 args_type[cnt] &= ~PA_FLAG_LONG_DOUBLE;
1871 }
1872 else
1873 args_value[cnt].pa_long_double = va_arg (*ap_savep, long double);
1874 break;
1875 case PA_STRING: /* All pointers are the same */
1876 case PA_WSTRING: /* All pointers are the same */
1877 T (PA_POINTER, pa_pointer, void *);
1878 #undef T
1879 default:
1880 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1881 args_value[cnt].pa_pointer = va_arg (*ap_savep, void *);
1882 else if (__glibc_unlikely (__printf_va_arg_table != NULL)
1883 && __printf_va_arg_table[args_type[cnt] - PA_LAST] != NULL)
1884 {
1885 args_value[cnt].pa_user = alloca (args_size[cnt]);
1886 (*__printf_va_arg_table[args_type[cnt] - PA_LAST])
1887 (args_value[cnt].pa_user, ap_savep);
1888 }
1889 else
1890 args_value[cnt].pa_long_double = 0.0;
1891 break;
1892 case -1:
1893 /* Error case. Not all parameters appear in N$ format
1894 strings. We have no way to determine their type. */
1895 assert ((mode_flags & PRINTF_FORTIFY) != 0);
1896 __libc_fatal ("*** invalid %N$ use detected ***\n");
1897 }
1898
1899 /* Now walk through all format specifiers and process them. */
1900 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1901 {
1902 STEP4_TABLE;
1903
1904 int is_negative;
1905 union
1906 {
1907 unsigned long long int longlong;
1908 unsigned long int word;
1909 } number;
1910 int base;
1911 union printf_arg the_arg;
1912 CHAR_T *string; /* Pointer to argument string. */
1913
1914 /* Fill variables from values in struct. */
1915 int alt = specs[nspecs_done].info.alt;
1916 int space = specs[nspecs_done].info.space;
1917 int left = specs[nspecs_done].info.left;
1918 int showsign = specs[nspecs_done].info.showsign;
1919 int group = specs[nspecs_done].info.group;
1920 int is_long_double = specs[nspecs_done].info.is_long_double;
1921 int is_short = specs[nspecs_done].info.is_short;
1922 int is_char = specs[nspecs_done].info.is_char;
1923 int is_long = specs[nspecs_done].info.is_long;
1924 int width = specs[nspecs_done].info.width;
1925 int prec = specs[nspecs_done].info.prec;
1926 int use_outdigits = specs[nspecs_done].info.i18n;
1927 char pad = specs[nspecs_done].info.pad;
1928 CHAR_T spec = specs[nspecs_done].info.spec;
1929
1930 workstart = NULL;
1931 CHAR_T *workend = work_buffer + WORK_BUFFER_SIZE;
1932
1933 /* Fill in last information. */
1934 if (specs[nspecs_done].width_arg != -1)
1935 {
1936 /* Extract the field width from an argument. */
1937 specs[nspecs_done].info.width =
1938 args_value[specs[nspecs_done].width_arg].pa_int;
1939
1940 if (specs[nspecs_done].info.width < 0)
1941 /* If the width value is negative left justification is
1942 selected and the value is taken as being positive. */
1943 {
1944 specs[nspecs_done].info.width *= -1;
1945 left = specs[nspecs_done].info.left = 1;
1946 }
1947 width = specs[nspecs_done].info.width;
1948 }
1949
1950 if (specs[nspecs_done].prec_arg != -1)
1951 {
1952 /* Extract the precision from an argument. */
1953 specs[nspecs_done].info.prec =
1954 args_value[specs[nspecs_done].prec_arg].pa_int;
1955
1956 if (specs[nspecs_done].info.prec < 0)
1957 /* If the precision is negative the precision is
1958 omitted. */
1959 specs[nspecs_done].info.prec = -1;
1960
1961 prec = specs[nspecs_done].info.prec;
1962 }
1963
1964 /* Maybe the buffer is too small. */
1965 if (MAX (prec, width) + EXTSIZ > WORK_BUFFER_SIZE)
1966 {
1967 if (__libc_use_alloca ((MAX (prec, width) + EXTSIZ)
1968 * sizeof (CHAR_T)))
1969 workend = ((CHAR_T *) alloca ((MAX (prec, width) + EXTSIZ)
1970 * sizeof (CHAR_T))
1971 + (MAX (prec, width) + EXTSIZ));
1972 else
1973 {
1974 workstart = (CHAR_T *) malloc ((MAX (prec, width) + EXTSIZ)
1975 * sizeof (CHAR_T));
1976 if (workstart == NULL)
1977 {
1978 done = -1;
1979 goto all_done;
1980 }
1981 workend = workstart + (MAX (prec, width) + EXTSIZ);
1982 }
1983 }
1984
1985 /* Process format specifiers. */
1986 while (1)
1987 {
1988 extern printf_function **__printf_function_table;
1989 int function_done;
1990
1991 if (spec <= UCHAR_MAX
1992 && __printf_function_table != NULL
1993 && __printf_function_table[(size_t) spec] != NULL)
1994 {
1995 const void **ptr = alloca (specs[nspecs_done].ndata_args
1996 * sizeof (const void *));
1997
1998 /* Fill in an array of pointers to the argument values. */
1999 for (unsigned int i = 0; i < specs[nspecs_done].ndata_args;
2000 ++i)
2001 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
2002
2003 /* Call the function. */
2004 function_done = __printf_function_table[(size_t) spec]
2005 (s, &specs[nspecs_done].info, ptr);
2006
2007 if (function_done != -2)
2008 {
2009 /* If an error occurred we don't have information
2010 about # of chars. */
2011 if (function_done < 0)
2012 {
2013 /* Function has set errno. */
2014 done = -1;
2015 goto all_done;
2016 }
2017
2018 done_add (function_done);
2019 break;
2020 }
2021 }
2022
2023 JUMP (spec, step4_jumps);
2024
2025 process_arg ((&specs[nspecs_done]));
2026 process_string_arg ((&specs[nspecs_done]));
2027
2028 LABEL (form_unknown):
2029 {
2030 unsigned int i;
2031 const void **ptr;
2032
2033 ptr = alloca (specs[nspecs_done].ndata_args
2034 * sizeof (const void *));
2035
2036 /* Fill in an array of pointers to the argument values. */
2037 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
2038 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
2039
2040 /* Call the function. */
2041 function_done = printf_unknown (s, &specs[nspecs_done].info,
2042 ptr);
2043
2044 /* If an error occurred we don't have information about #
2045 of chars. */
2046 if (function_done < 0)
2047 {
2048 /* Function has set errno. */
2049 done = -1;
2050 goto all_done;
2051 }
2052
2053 done_add (function_done);
2054 }
2055 break;
2056 }
2057
2058 if (__glibc_unlikely (workstart != NULL))
2059 free (workstart);
2060 workstart = NULL;
2061
2062 /* Write the following constant string. */
2063 outstring (specs[nspecs_done].end_of_fmt,
2064 specs[nspecs_done].next_fmt
2065 - specs[nspecs_done].end_of_fmt);
2066 }
2067 all_done:
2068 if (__glibc_unlikely (workstart != NULL))
2069 free (workstart);
2070 scratch_buffer_free (&argsbuf);
2071 scratch_buffer_free (&specsbuf);
2072 return done;
2073 }
2074 \f
2075 /* Handle an unknown format specifier. This prints out a canonicalized
2076 representation of the format spec itself. */
2077 static int
2078 printf_unknown (FILE *s, const struct printf_info *info,
2079 const void *const *args)
2080
2081 {
2082 int done = 0;
2083 CHAR_T work_buffer[MAX (sizeof (info->width), sizeof (info->prec)) * 3];
2084 CHAR_T *const workend
2085 = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
2086 CHAR_T *w;
2087
2088 outchar (L_('%'));
2089
2090 if (info->alt)
2091 outchar (L_('#'));
2092 if (info->group)
2093 outchar (L_('\''));
2094 if (info->showsign)
2095 outchar (L_('+'));
2096 else if (info->space)
2097 outchar (L_(' '));
2098 if (info->left)
2099 outchar (L_('-'));
2100 if (info->pad == L_('0'))
2101 outchar (L_('0'));
2102 if (info->i18n)
2103 outchar (L_('I'));
2104
2105 if (info->width != 0)
2106 {
2107 w = _itoa_word (info->width, workend, 10, 0);
2108 while (w < workend)
2109 outchar (*w++);
2110 }
2111
2112 if (info->prec != -1)
2113 {
2114 outchar (L_('.'));
2115 w = _itoa_word (info->prec, workend, 10, 0);
2116 while (w < workend)
2117 outchar (*w++);
2118 }
2119
2120 if (info->spec != L_('\0'))
2121 outchar (info->spec);
2122
2123 all_done:
2124 return done;
2125 }
2126 \f
2127 /* Group the digits from W to REAR_PTR according to the grouping rules
2128 of the current locale. The interpretation of GROUPING is as in
2129 `struct lconv' from <locale.h>. The grouped number extends from
2130 the returned pointer until REAR_PTR. FRONT_PTR to W is used as a
2131 scratch area. */
2132 static CHAR_T *
2133 group_number (CHAR_T *front_ptr, CHAR_T *w, CHAR_T *rear_ptr,
2134 const char *grouping, THOUSANDS_SEP_T thousands_sep)
2135 {
2136 /* Length of the current group. */
2137 int len;
2138 #ifndef COMPILE_WPRINTF
2139 /* Length of the separator (in wide mode, the separator is always a
2140 single wide character). */
2141 int tlen = strlen (thousands_sep);
2142 #endif
2143
2144 /* We treat all negative values like CHAR_MAX. */
2145
2146 if (*grouping == CHAR_MAX || *grouping <= 0)
2147 /* No grouping should be done. */
2148 return w;
2149
2150 len = *grouping++;
2151
2152 /* Copy existing string so that nothing gets overwritten. */
2153 memmove (front_ptr, w, (rear_ptr - w) * sizeof (CHAR_T));
2154 CHAR_T *s = front_ptr + (rear_ptr - w);
2155
2156 w = rear_ptr;
2157
2158 /* Process all characters in the string. */
2159 while (s > front_ptr)
2160 {
2161 *--w = *--s;
2162
2163 if (--len == 0 && s > front_ptr)
2164 {
2165 /* A new group begins. */
2166 #ifdef COMPILE_WPRINTF
2167 if (w != s)
2168 *--w = thousands_sep;
2169 else
2170 /* Not enough room for the separator. */
2171 goto copy_rest;
2172 #else
2173 int cnt = tlen;
2174 if (tlen < w - s)
2175 do
2176 *--w = thousands_sep[--cnt];
2177 while (cnt > 0);
2178 else
2179 /* Not enough room for the separator. */
2180 goto copy_rest;
2181 #endif
2182
2183 if (*grouping == CHAR_MAX
2184 #if CHAR_MIN < 0
2185 || *grouping < 0
2186 #endif
2187 )
2188 {
2189 copy_rest:
2190 /* No further grouping to be done. Copy the rest of the
2191 number. */
2192 memmove (w, s, (front_ptr -s) * sizeof (CHAR_T));
2193 break;
2194 }
2195 else if (*grouping != '\0')
2196 len = *grouping++;
2197 else
2198 /* The previous grouping repeats ad infinitum. */
2199 len = grouping[-1];
2200 }
2201 }
2202 return w;
2203 }
2204 \f
2205 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2206 struct helper_file
2207 {
2208 struct _IO_FILE_plus _f;
2209 #ifdef COMPILE_WPRINTF
2210 struct _IO_wide_data _wide_data;
2211 #endif
2212 FILE *_put_stream;
2213 #ifdef _IO_MTSAFE_IO
2214 _IO_lock_t lock;
2215 #endif
2216 };
2217
2218 static int
2219 _IO_helper_overflow (FILE *s, int c)
2220 {
2221 FILE *target = ((struct helper_file*) s)->_put_stream;
2222 #ifdef COMPILE_WPRINTF
2223 int used = s->_wide_data->_IO_write_ptr - s->_wide_data->_IO_write_base;
2224 if (used)
2225 {
2226 size_t written = _IO_sputn (target, s->_wide_data->_IO_write_base, used);
2227 if (written == 0 || written == WEOF)
2228 return WEOF;
2229 __wmemmove (s->_wide_data->_IO_write_base,
2230 s->_wide_data->_IO_write_base + written,
2231 used - written);
2232 s->_wide_data->_IO_write_ptr -= written;
2233 }
2234 #else
2235 int used = s->_IO_write_ptr - s->_IO_write_base;
2236 if (used)
2237 {
2238 size_t written = _IO_sputn (target, s->_IO_write_base, used);
2239 if (written == 0 || written == EOF)
2240 return EOF;
2241 memmove (s->_IO_write_base, s->_IO_write_base + written,
2242 used - written);
2243 s->_IO_write_ptr -= written;
2244 }
2245 #endif
2246 return PUTC (c, s);
2247 }
2248
2249 #ifdef COMPILE_WPRINTF
2250 static const struct _IO_jump_t _IO_helper_jumps libio_vtable =
2251 {
2252 JUMP_INIT_DUMMY,
2253 JUMP_INIT (finish, _IO_wdefault_finish),
2254 JUMP_INIT (overflow, _IO_helper_overflow),
2255 JUMP_INIT (underflow, _IO_default_underflow),
2256 JUMP_INIT (uflow, _IO_default_uflow),
2257 JUMP_INIT (pbackfail, (_IO_pbackfail_t) _IO_wdefault_pbackfail),
2258 JUMP_INIT (xsputn, _IO_wdefault_xsputn),
2259 JUMP_INIT (xsgetn, _IO_wdefault_xsgetn),
2260 JUMP_INIT (seekoff, _IO_default_seekoff),
2261 JUMP_INIT (seekpos, _IO_default_seekpos),
2262 JUMP_INIT (setbuf, _IO_default_setbuf),
2263 JUMP_INIT (sync, _IO_default_sync),
2264 JUMP_INIT (doallocate, _IO_wdefault_doallocate),
2265 JUMP_INIT (read, _IO_default_read),
2266 JUMP_INIT (write, _IO_default_write),
2267 JUMP_INIT (seek, _IO_default_seek),
2268 JUMP_INIT (close, _IO_default_close),
2269 JUMP_INIT (stat, _IO_default_stat)
2270 };
2271 #else
2272 static const struct _IO_jump_t _IO_helper_jumps libio_vtable =
2273 {
2274 JUMP_INIT_DUMMY,
2275 JUMP_INIT (finish, _IO_default_finish),
2276 JUMP_INIT (overflow, _IO_helper_overflow),
2277 JUMP_INIT (underflow, _IO_default_underflow),
2278 JUMP_INIT (uflow, _IO_default_uflow),
2279 JUMP_INIT (pbackfail, _IO_default_pbackfail),
2280 JUMP_INIT (xsputn, _IO_default_xsputn),
2281 JUMP_INIT (xsgetn, _IO_default_xsgetn),
2282 JUMP_INIT (seekoff, _IO_default_seekoff),
2283 JUMP_INIT (seekpos, _IO_default_seekpos),
2284 JUMP_INIT (setbuf, _IO_default_setbuf),
2285 JUMP_INIT (sync, _IO_default_sync),
2286 JUMP_INIT (doallocate, _IO_default_doallocate),
2287 JUMP_INIT (read, _IO_default_read),
2288 JUMP_INIT (write, _IO_default_write),
2289 JUMP_INIT (seek, _IO_default_seek),
2290 JUMP_INIT (close, _IO_default_close),
2291 JUMP_INIT (stat, _IO_default_stat)
2292 };
2293 #endif
2294
2295 static int
2296 buffered_vfprintf (FILE *s, const CHAR_T *format, va_list args,
2297 unsigned int mode_flags)
2298 {
2299 CHAR_T buf[BUFSIZ];
2300 struct helper_file helper;
2301 FILE *hp = (FILE *) &helper._f;
2302 int result, to_flush;
2303
2304 /* Orient the stream. */
2305 #ifdef ORIENT
2306 ORIENT;
2307 #endif
2308
2309 /* Initialize helper. */
2310 helper._put_stream = s;
2311 #ifdef COMPILE_WPRINTF
2312 hp->_wide_data = &helper._wide_data;
2313 _IO_wsetp (hp, buf, buf + sizeof buf / sizeof (CHAR_T));
2314 hp->_mode = 1;
2315 #else
2316 _IO_setp (hp, buf, buf + sizeof buf);
2317 hp->_mode = -1;
2318 #endif
2319 hp->_flags = _IO_MAGIC|_IO_NO_READS|_IO_USER_LOCK;
2320 #if _IO_JUMPS_OFFSET
2321 hp->_vtable_offset = 0;
2322 #endif
2323 #ifdef _IO_MTSAFE_IO
2324 hp->_lock = NULL;
2325 #endif
2326 hp->_flags2 = s->_flags2;
2327 _IO_JUMPS (&helper._f) = (struct _IO_jump_t *) &_IO_helper_jumps;
2328
2329 /* Now print to helper instead. */
2330 result = vfprintf (hp, format, args, mode_flags);
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 }