b8c87a539fe52ab1c6f872f754f8eb31d46c5acb
[glibc.git] / stdio-common / vfprintf.c
blobb8c87a539fe52ab1c6f872f754f8eb31d46c5acb
1 /* Copyright (C) 1991-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
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>
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++.
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. */
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)
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)
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
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)
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)) \
102 done = -1; \
103 goto all_done; \
105 done_add (written); \
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
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)
122 # include <_itowa.h>
124 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
125 # define PAD(Padchar) \
126 do { \
127 if (width > 0) \
129 _IO_ssize_t written = _IO_wpadn (s, (Padchar), width); \
130 if (__glibc_unlikely (written != width)) \
132 done = -1; \
133 goto all_done; \
135 done_add (written); \
137 } while (0)
138 # define PUTC(C, F) _IO_putwc_unlocked (C, F)
139 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
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
148 #include "_i18n_number.h"
150 /* Include the shared code for parsing the format string. */
151 #include "printf-parse.h"
154 #define outchar(Ch) \
155 do \
157 const INT_T outc = (Ch); \
158 if (PUTC (outc, s) == EOF || done == INT_MAX) \
160 done = -1; \
161 goto all_done; \
163 ++done; \
165 while (0)
167 #define outstring(String, Len) \
168 do \
170 assert ((size_t) done <= (size_t) INT_MAX); \
171 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
173 done = -1; \
174 goto all_done; \
176 if (__glibc_unlikely (INT_MAX - done < (Len))) \
178 done = -1; \
179 __set_errno (EOVERFLOW); \
180 goto all_done; \
182 done += (Len); \
184 while (0)
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
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
204 /* Global constants. */
205 static const CHAR_T null[] = L_("(null)");
207 /* Size of the work_buffer variable (in characters, not bytes. */
208 enum { WORK_BUFFER_SIZE = 1000 };
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[] =
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
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 \
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; \
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 \
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; \
271 while (0)
272 #endif
274 #define STEP0_3_TABLE \
275 /* Step 0: at the beginning. */ \
276 static JUMP_TABLE_TYPE step0_jumps[30] = \
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] = \
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] = \
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] = \
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] = \
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' */ \
446 #define STEP4_TABLE \
447 /* Step 4: processing format specifier. */ \
448 static JUMP_TABLE_TYPE step4_jumps[30] = \
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' */ \
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; \
491 LABEL (form_integer): \
492 /* Signed decimal integer. */ \
493 base = 10; \
495 if (is_longlong) \
497 long long int signed_number; \
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; \
504 is_negative = signed_number < 0; \
505 number.longlong = is_negative ? (- signed_number) : signed_number; \
507 goto LABEL (longlong_number); \
509 else \
511 long int signed_number; \
513 if (fspec == NULL) \
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); \
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; \
536 is_negative = signed_number < 0; \
537 number.word = is_negative ? (- signed_number) : signed_number; \
539 goto LABEL (number); \
541 /* NOTREACHED */ \
543 LABEL (form_unsigned): \
544 /* Unsigned decimal integer. */ \
545 base = 10; \
546 goto LABEL (unsigned_number); \
547 /* NOTREACHED */ \
549 LABEL (form_octal): \
550 /* Unsigned octal integer. */ \
551 base = 8; \
552 goto LABEL (unsigned_number); \
553 /* NOTREACHED */ \
555 LABEL (form_hexa): \
556 /* Unsigned hexadecimal integer. */ \
557 base = 16; \
559 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
561 /* ISO specifies the `+' and ` ' flags only for signed \
562 conversions. */ \
563 is_negative = 0; \
564 showsign = 0; \
565 space = 0; \
567 if (is_longlong) \
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; \
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_(' '); \
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) \
588 string = workend; \
589 if (base == 8 && alt) \
590 *--string = L_('0'); \
592 else \
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); \
601 if (use_outdigits && base == 10) \
602 string = _i18n_number_rewrite (string, workend, workend); \
604 /* Simplify further test for num != 0. */ \
605 number.word = number.longlong != 0; \
607 else \
609 if (fspec == NULL) \
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); \
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; \
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_(' '); \
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) \
646 string = workend; \
647 if (base == 8 && alt) \
648 *--string = L_('0'); \
650 else \
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); \
659 if (use_outdigits && base == 10) \
660 string = _i18n_number_rewrite (string, workend, workend); \
664 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
665 /* Add octal marker. */ \
666 *--string = L_('0'); \
668 prec = MAX (0, prec - (workend - string)); \
670 if (!left) \
672 width -= workend - string + prec; \
674 if (number.word != 0 && alt && base == 16) \
675 /* Account for 0X hex marker. */ \
676 width -= 2; \
678 if (is_negative || showsign || space) \
679 --width; \
681 if (pad == L_(' ')) \
683 PAD (L_(' ')); \
684 width = 0; \
687 if (is_negative) \
688 outchar (L_('-')); \
689 else if (showsign) \
690 outchar (L_('+')); \
691 else if (space) \
692 outchar (L_(' ')); \
694 if (number.word != 0 && alt && base == 16) \
696 outchar (L_('0')); \
697 outchar (spec); \
700 width += prec; \
701 PAD (L_('0')); \
703 outstring (string, workend - string); \
705 break; \
707 else \
709 if (is_negative) \
711 outchar (L_('-')); \
712 --width; \
714 else if (showsign) \
716 outchar (L_('+')); \
717 --width; \
719 else if (space) \
721 outchar (L_(' ')); \
722 --width; \
725 if (number.word != 0 && alt && base == 16) \
727 outchar (L_('0')); \
728 outchar (spec); \
729 width -= 2; \
732 width -= workend - string + prec; \
734 if (prec > 0) \
736 int temp = width; \
737 width = prec; \
738 PAD (L_('0')); \
739 width = temp; \
742 outstring (string, workend - string); \
744 PAD (L_(' ')); \
745 break; \
748 LABEL (form_float): \
750 /* Floating-point number. This is handled by printf_fp.c. */ \
751 const void *ptr; \
752 int function_done; \
754 if (fspec == NULL) \
756 if (__ldbl_is_dbl) \
757 is_long_double = 0; \
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 .is_binary128 = 0}; \
776 if (is_long_double) \
777 the_arg.pa_long_double = va_arg (ap, long double); \
778 else \
779 the_arg.pa_double = va_arg (ap, double); \
780 ptr = (const void *) &the_arg; \
782 function_done = __printf_fp (s, &info, &ptr); \
784 else \
786 ptr = (const void *) &args_value[fspec->data_arg]; \
787 if (__ldbl_is_dbl) \
789 fspec->data_arg_type = PA_DOUBLE; \
790 fspec->info.is_long_double = 0; \
792 /* Not supported by *printf functions. */ \
793 fspec->info.is_binary128 = 0; \
795 function_done = __printf_fp (s, &fspec->info, &ptr); \
798 if (function_done < 0) \
800 /* Error in print handler; up to handler to set errno. */ \
801 done = -1; \
802 goto all_done; \
805 done_add (function_done); \
807 break; \
809 LABEL (form_floathex): \
811 /* Floating point number printed as hexadecimal number. */ \
812 const void *ptr; \
813 int function_done; \
815 if (fspec == NULL) \
817 if (__ldbl_is_dbl) \
818 is_long_double = 0; \
820 struct printf_info info = { .prec = prec, \
821 .width = width, \
822 .spec = spec, \
823 .is_long_double = is_long_double, \
824 .is_short = is_short, \
825 .is_long = is_long, \
826 .alt = alt, \
827 .space = space, \
828 .left = left, \
829 .showsign = showsign, \
830 .group = group, \
831 .pad = pad, \
832 .extra = 0, \
833 .wide = sizeof (CHAR_T) != 1, \
834 .is_binary128 = 0}; \
836 if (is_long_double) \
837 the_arg.pa_long_double = va_arg (ap, long double); \
838 else \
839 the_arg.pa_double = va_arg (ap, double); \
840 ptr = (const void *) &the_arg; \
842 function_done = __printf_fphex (s, &info, &ptr); \
844 else \
846 ptr = (const void *) &args_value[fspec->data_arg]; \
847 if (__ldbl_is_dbl) \
848 fspec->info.is_long_double = 0; \
849 /* Not supported by *printf functions. */ \
850 fspec->info.is_binary128 = 0; \
852 function_done = __printf_fphex (s, &fspec->info, &ptr); \
855 if (function_done < 0) \
857 /* Error in print handler; up to handler to set errno. */ \
858 done = -1; \
859 goto all_done; \
862 done_add (function_done); \
864 break; \
866 LABEL (form_pointer): \
867 /* Generic pointer. */ \
869 const void *ptr; \
870 if (fspec == NULL) \
871 ptr = va_arg (ap, void *); \
872 else \
873 ptr = args_value[fspec->data_arg].pa_pointer; \
874 if (ptr != NULL) \
876 /* If the pointer is not NULL, write it as a %#x spec. */ \
877 base = 16; \
878 number.word = (unsigned long int) ptr; \
879 is_negative = 0; \
880 alt = 1; \
881 group = 0; \
882 spec = L_('x'); \
883 goto LABEL (number); \
885 else \
887 /* Write "(nil)" for a nil pointer. */ \
888 string = (CHAR_T *) L_("(nil)"); \
889 /* Make sure the full string "(nil)" is printed. */ \
890 if (prec < 5) \
891 prec = 5; \
892 /* This is a wide string iff compiling wprintf. */ \
893 is_long = sizeof (CHAR_T) > 1; \
894 goto LABEL (print_string); \
897 /* NOTREACHED */ \
899 LABEL (form_number): \
900 if (s->_flags2 & _IO_FLAGS2_FORTIFY) \
902 if (! readonly_format) \
904 extern int __readonly_area (const void *, size_t) \
905 attribute_hidden; \
906 readonly_format \
907 = __readonly_area (format, ((STR_LEN (format) + 1) \
908 * sizeof (CHAR_T))); \
910 if (readonly_format < 0) \
911 __libc_fatal ("*** %n in writable segment detected ***\n"); \
913 /* Answer the count of characters written. */ \
914 if (fspec == NULL) \
916 if (is_longlong) \
917 *(long long int *) va_arg (ap, void *) = done; \
918 else if (is_long_num) \
919 *(long int *) va_arg (ap, void *) = done; \
920 else if (is_char) \
921 *(char *) va_arg (ap, void *) = done; \
922 else if (!is_short) \
923 *(int *) va_arg (ap, void *) = done; \
924 else \
925 *(short int *) va_arg (ap, void *) = done; \
927 else \
928 if (is_longlong) \
929 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
930 else if (is_long_num) \
931 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
932 else if (is_char) \
933 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
934 else if (!is_short) \
935 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
936 else \
937 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
938 break; \
940 LABEL (form_strerror): \
941 /* Print description of error ERRNO. */ \
942 string = \
943 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
944 WORK_BUFFER_SIZE * sizeof (CHAR_T)); \
945 is_long = 0; /* This is no wide-char string. */ \
946 goto LABEL (print_string)
948 #ifdef COMPILE_WPRINTF
949 # define process_string_arg(fspec) \
950 LABEL (form_character): \
951 /* Character. */ \
952 if (is_long) \
953 goto LABEL (form_wcharacter); \
954 --width; /* Account for the character itself. */ \
955 if (!left) \
956 PAD (L' '); \
957 if (fspec == NULL) \
958 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
959 else \
960 outchar (__btowc ((unsigned char) \
961 args_value[fspec->data_arg].pa_int)); \
962 if (left) \
963 PAD (L' '); \
964 break; \
966 LABEL (form_wcharacter): \
968 /* Wide character. */ \
969 --width; \
970 if (!left) \
971 PAD (L' '); \
972 if (fspec == NULL) \
973 outchar (va_arg (ap, wchar_t)); \
974 else \
975 outchar (args_value[fspec->data_arg].pa_wchar); \
976 if (left) \
977 PAD (L' '); \
979 break; \
981 LABEL (form_string): \
983 size_t len; \
984 int string_malloced; \
986 /* The string argument could in fact be `char *' or `wchar_t *'. \
987 But this should not make a difference here. */ \
988 if (fspec == NULL) \
989 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
990 else \
991 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
993 /* Entry point for printing other strings. */ \
994 LABEL (print_string): \
996 string_malloced = 0; \
997 if (string == NULL) \
999 /* Write "(null)" if there's space. */ \
1000 if (prec == -1 \
1001 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
1003 string = (CHAR_T *) null; \
1004 len = (sizeof (null) / sizeof (null[0])) - 1; \
1006 else \
1008 string = (CHAR_T *) L""; \
1009 len = 0; \
1012 else if (!is_long && spec != L_('S')) \
1014 /* This is complicated. We have to transform the multibyte \
1015 string into a wide character string. */ \
1016 const char *mbs = (const char *) string; \
1017 mbstate_t mbstate; \
1019 len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
1021 /* Allocate dynamically an array which definitely is long \
1022 enough for the wide character version. Each byte in the \
1023 multi-byte string can produce at most one wide character. */ \
1024 if (__glibc_unlikely (len > SIZE_MAX / sizeof (wchar_t))) \
1026 __set_errno (EOVERFLOW); \
1027 done = -1; \
1028 goto all_done; \
1030 else if (__libc_use_alloca (len * sizeof (wchar_t))) \
1031 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1032 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1033 == NULL) \
1035 done = -1; \
1036 goto all_done; \
1038 else \
1039 string_malloced = 1; \
1041 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1042 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1043 if (len == (size_t) -1) \
1045 /* Illegal multibyte character. */ \
1046 done = -1; \
1047 goto all_done; \
1050 else \
1052 if (prec != -1) \
1053 /* Search for the end of the string, but don't search past \
1054 the length specified by the precision. */ \
1055 len = __wcsnlen (string, prec); \
1056 else \
1057 len = __wcslen (string); \
1060 if ((width -= len) < 0) \
1062 outstring (string, len); \
1063 break; \
1066 if (!left) \
1067 PAD (L' '); \
1068 outstring (string, len); \
1069 if (left) \
1070 PAD (L' '); \
1071 if (__glibc_unlikely (string_malloced)) \
1072 free (string); \
1074 break;
1075 #else
1076 # define process_string_arg(fspec) \
1077 LABEL (form_character): \
1078 /* Character. */ \
1079 if (is_long) \
1080 goto LABEL (form_wcharacter); \
1081 --width; /* Account for the character itself. */ \
1082 if (!left) \
1083 PAD (' '); \
1084 if (fspec == NULL) \
1085 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1086 else \
1087 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1088 if (left) \
1089 PAD (' '); \
1090 break; \
1092 LABEL (form_wcharacter): \
1094 /* Wide character. */ \
1095 char buf[MB_LEN_MAX]; \
1096 mbstate_t mbstate; \
1097 size_t len; \
1099 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1100 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1101 : args_value[fspec->data_arg].pa_wchar), \
1102 &mbstate); \
1103 if (len == (size_t) -1) \
1105 /* Something went wrong during the conversion. Bail out. */ \
1106 done = -1; \
1107 goto all_done; \
1109 width -= len; \
1110 if (!left) \
1111 PAD (' '); \
1112 outstring (buf, len); \
1113 if (left) \
1114 PAD (' '); \
1116 break; \
1118 LABEL (form_string): \
1120 size_t len; \
1121 int string_malloced; \
1123 /* The string argument could in fact be `char *' or `wchar_t *'. \
1124 But this should not make a difference here. */ \
1125 if (fspec == NULL) \
1126 string = (char *) va_arg (ap, const char *); \
1127 else \
1128 string = (char *) args_value[fspec->data_arg].pa_string; \
1130 /* Entry point for printing other strings. */ \
1131 LABEL (print_string): \
1133 string_malloced = 0; \
1134 if (string == NULL) \
1136 /* Write "(null)" if there's space. */ \
1137 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1139 string = (char *) null; \
1140 len = sizeof (null) - 1; \
1142 else \
1144 string = (char *) ""; \
1145 len = 0; \
1148 else if (!is_long && spec != L_('S')) \
1150 if (prec != -1) \
1151 /* Search for the end of the string, but don't search past \
1152 the length (in bytes) specified by the precision. */ \
1153 len = __strnlen (string, prec); \
1154 else \
1155 len = strlen (string); \
1157 else \
1159 const wchar_t *s2 = (const wchar_t *) string; \
1160 mbstate_t mbstate; \
1162 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1164 if (prec >= 0) \
1166 /* The string `s2' might not be NUL terminated. */ \
1167 if (__libc_use_alloca (prec)) \
1168 string = (char *) alloca (prec); \
1169 else if ((string = (char *) malloc (prec)) == NULL) \
1171 done = -1; \
1172 goto all_done; \
1174 else \
1175 string_malloced = 1; \
1176 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1178 else \
1180 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1181 if (len != (size_t) -1) \
1183 assert (__mbsinit (&mbstate)); \
1184 s2 = (const wchar_t *) string; \
1185 if (__libc_use_alloca (len + 1)) \
1186 string = (char *) alloca (len + 1); \
1187 else if ((string = (char *) malloc (len + 1)) == NULL) \
1189 done = -1; \
1190 goto all_done; \
1192 else \
1193 string_malloced = 1; \
1194 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1198 if (len == (size_t) -1) \
1200 /* Illegal wide-character string. */ \
1201 done = -1; \
1202 goto all_done; \
1206 if ((width -= len) < 0) \
1208 outstring (string, len); \
1209 break; \
1212 if (!left) \
1213 PAD (' '); \
1214 outstring (string, len); \
1215 if (left) \
1216 PAD (' '); \
1217 if (__glibc_unlikely (string_malloced)) \
1218 free (string); \
1220 break;
1221 #endif
1223 /* Helper function to provide temporary buffering for unbuffered streams. */
1224 static int buffered_vfprintf (FILE *stream, const CHAR_T *fmt, va_list)
1225 __THROW __attribute__ ((noinline)) internal_function;
1227 /* Handle positional format specifiers. */
1228 static int printf_positional (_IO_FILE *s,
1229 const CHAR_T *format, int readonly_format,
1230 va_list ap, va_list *ap_savep, int done,
1231 int nspecs_done, const UCHAR_T *lead_str_end,
1232 CHAR_T *work_buffer, int save_errno,
1233 const char *grouping, THOUSANDS_SEP_T);
1235 /* Handle unknown format specifier. */
1236 static int printf_unknown (FILE *, const struct printf_info *,
1237 const void *const *) __THROW;
1239 /* Group digits of number string. */
1240 static CHAR_T *group_number (CHAR_T *, CHAR_T *, const char *, THOUSANDS_SEP_T)
1241 __THROW internal_function;
1243 /* The function itself. */
1245 vfprintf (FILE *s, const CHAR_T *format, va_list ap)
1247 /* The character used as thousands separator. */
1248 THOUSANDS_SEP_T thousands_sep = 0;
1250 /* The string describing the size of groups of digits. */
1251 const char *grouping;
1253 /* Place to accumulate the result. */
1254 int done;
1256 /* Current character in format string. */
1257 const UCHAR_T *f;
1259 /* End of leading constant string. */
1260 const UCHAR_T *lead_str_end;
1262 /* Points to next format specifier. */
1263 const UCHAR_T *end_of_spec;
1265 /* Buffer intermediate results. */
1266 CHAR_T work_buffer[WORK_BUFFER_SIZE];
1267 CHAR_T *workstart = NULL;
1268 CHAR_T *workend;
1270 /* We have to save the original argument pointer. */
1271 va_list ap_save;
1273 /* Count number of specifiers we already processed. */
1274 int nspecs_done;
1276 /* For the %m format we may need the current `errno' value. */
1277 int save_errno = errno;
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;
1283 /* Orient the stream. */
1284 #ifdef ORIENT
1285 ORIENT;
1286 #endif
1288 /* Sanity check of arguments. */
1289 ARGCHECK (s, format);
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
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);
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;
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
1325 /* Lock stream. */
1326 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
1327 _IO_flockfile (s);
1329 /* Write the literal text before the first format. */
1330 outstring ((const UCHAR_T *) format,
1331 lead_str_end - (const UCHAR_T *) format);
1333 /* If we only have to print a simple string, return now. */
1334 if (*f == L_('\0'))
1335 goto all_done;
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;
1343 /* Process whole format string. */
1346 STEP0_3_TABLE;
1347 STEP4_TABLE;
1349 union printf_arg *args_value; /* This is not used here but ... */
1350 int is_negative; /* Flag for negative number. */
1351 union
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;
1376 workstart = NULL;
1377 workend = work_buffer + WORK_BUFFER_SIZE;
1379 /* Get current character in format string. */
1380 JUMP (*++f, step0_jumps);
1382 /* ' ' flag. */
1383 LABEL (flag_space):
1384 space = 1;
1385 JUMP (*++f, step0_jumps);
1387 /* '+' flag. */
1388 LABEL (flag_plus):
1389 showsign = 1;
1390 JUMP (*++f, step0_jumps);
1392 /* The '-' flag. */
1393 LABEL (flag_minus):
1394 left = 1;
1395 pad = L_(' ');
1396 JUMP (*++f, step0_jumps);
1398 /* The '#' flag. */
1399 LABEL (flag_hash):
1400 alt = 1;
1401 JUMP (*++f, step0_jumps);
1403 /* The '0' flag. */
1404 LABEL (flag_zero):
1405 if (!left)
1406 pad = L_('0');
1407 JUMP (*++f, step0_jumps);
1409 /* The '\'' flag. */
1410 LABEL (flag_quote):
1411 group = 1;
1413 if (grouping == (const char *) -1)
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
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
1430 grouping = NULL;
1432 JUMP (*++f, step0_jumps);
1434 LABEL (flag_i18n):
1435 use_outdigits = 1;
1436 JUMP (*++f, step0_jumps);
1438 /* Get width from argument. */
1439 LABEL (width_asterics):
1441 const UCHAR_T *tmp; /* Temporary value. */
1443 tmp = ++f;
1444 if (ISDIGIT (*tmp))
1446 int pos = read_int (&tmp);
1448 if (pos == -1)
1450 __set_errno (EOVERFLOW);
1451 done = -1;
1452 goto all_done;
1455 if (pos && *tmp == L_('$'))
1456 /* The width comes from a positional parameter. */
1457 goto do_positional;
1459 width = va_arg (ap, int);
1461 /* Negative width means left justified. */
1462 if (width < 0)
1464 width = -width;
1465 pad = L_(' ');
1466 left = 1;
1469 if (__glibc_unlikely (width >= INT_MAX / sizeof (CHAR_T) - EXTSIZ))
1471 __set_errno (EOVERFLOW);
1472 done = -1;
1473 goto all_done;
1476 if (width >= WORK_BUFFER_SIZE - EXTSIZ)
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
1484 workstart = (CHAR_T *) malloc (needed);
1485 if (workstart == NULL)
1487 done = -1;
1488 goto all_done;
1490 workend = workstart + width + EXTSIZ;
1494 JUMP (*f, step1_jumps);
1496 /* Given width in format string. */
1497 LABEL (width):
1498 width = read_int (&f);
1500 if (__glibc_unlikely (width == -1
1501 || width >= INT_MAX / sizeof (CHAR_T) - EXTSIZ))
1503 __set_errno (EOVERFLOW);
1504 done = -1;
1505 goto all_done;
1508 if (width >= WORK_BUFFER_SIZE - EXTSIZ)
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
1516 workstart = (CHAR_T *) malloc (needed);
1517 if (workstart == NULL)
1519 done = -1;
1520 goto all_done;
1522 workend = workstart + width + EXTSIZ;
1525 if (*f == L_('$'))
1526 /* Oh, oh. The argument comes from a positional parameter. */
1527 goto do_positional;
1528 JUMP (*f, step1_jumps);
1530 LABEL (precision):
1531 ++f;
1532 if (*f == L_('*'))
1534 const UCHAR_T *tmp; /* Temporary value. */
1536 tmp = ++f;
1537 if (ISDIGIT (*tmp))
1539 int pos = read_int (&tmp);
1541 if (pos == -1)
1543 __set_errno (EOVERFLOW);
1544 done = -1;
1545 goto all_done;
1548 if (pos && *tmp == L_('$'))
1549 /* The precision comes from a positional parameter. */
1550 goto do_positional;
1552 prec = va_arg (ap, int);
1554 /* If the precision is negative the precision is omitted. */
1555 if (prec < 0)
1556 prec = -1;
1558 else if (ISDIGIT (*f))
1560 prec = read_int (&f);
1562 /* The precision was specified in this case as an extremely
1563 large positive value. */
1564 if (prec == -1)
1566 __set_errno (EOVERFLOW);
1567 done = -1;
1568 goto all_done;
1571 else
1572 prec = 0;
1573 if (prec > width && prec > WORK_BUFFER_SIZE - EXTSIZ)
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))
1582 __set_errno (EOVERFLOW);
1583 done = -1;
1584 goto all_done;
1586 size_t needed = ((size_t) prec + EXTSIZ) * sizeof (CHAR_T);
1588 if (__libc_use_alloca (needed))
1589 workend = (CHAR_T *) alloca (needed) + prec + EXTSIZ;
1590 else
1592 workstart = (CHAR_T *) malloc (needed);
1593 if (workstart == NULL)
1595 done = -1;
1596 goto all_done;
1598 workend = workstart + prec + EXTSIZ;
1601 JUMP (*f, step2_jumps);
1603 /* Process 'h' modifier. There might another 'h' following. */
1604 LABEL (mod_half):
1605 is_short = 1;
1606 JUMP (*++f, step3a_jumps);
1608 /* Process 'hh' modifier. */
1609 LABEL (mod_halfhalf):
1610 is_short = 0;
1611 is_char = 1;
1612 JUMP (*++f, step4_jumps);
1614 /* Process 'l' modifier. There might another 'l' following. */
1615 LABEL (mod_long):
1616 is_long = 1;
1617 JUMP (*++f, step3b_jumps);
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);
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);
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);
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);
1641 /* Process current format. */
1642 while (1)
1644 process_arg (((struct printf_spec *) NULL));
1645 process_string_arg (((struct printf_spec *) NULL));
1647 LABEL (form_unknown):
1648 if (spec == L_('\0'))
1650 /* The format string ended before the specifier is complete. */
1651 __set_errno (EINVAL);
1652 done = -1;
1653 goto all_done;
1656 /* If we are in the fast loop force entering the complicated
1657 one. */
1658 goto do_positional;
1661 /* The format is correctly handled. */
1662 ++nspecs_done;
1664 if (__glibc_unlikely (workstart != NULL))
1665 free (workstart);
1666 workstart = NULL;
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
1675 /* Write the following constant string. */
1676 outstring (end_of_spec, f - end_of_spec);
1678 while (*f != L_('\0'));
1680 /* Unlock stream and return. */
1681 goto all_done;
1683 /* Hand off processing for positional parameters. */
1684 do_positional:
1685 if (__glibc_unlikely (workstart != NULL))
1687 free (workstart);
1688 workstart = NULL;
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);
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);
1701 return done;
1704 static int
1705 printf_positional (_IO_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)
1711 /* For the argument descriptions, which may be allocated on the heap. */
1712 void *args_malloced = NULL;
1714 /* For positional argument handling. */
1715 struct scratch_buffer specsbuf;
1716 scratch_buffer_init (&specsbuf);
1717 struct printf_spec *specs = specsbuf.data;
1718 size_t specs_limit = specsbuf.length / sizeof (specs[0]);
1720 /* Array with information about the needed arguments. This has to
1721 be dynamically extensible. */
1722 size_t nspecs = 0;
1724 /* The number of arguments the format string requests. This will
1725 determine the size of the array needed to store the argument
1726 attributes. */
1727 size_t nargs = 0;
1728 size_t bytes_per_arg;
1729 union printf_arg *args_value;
1730 int *args_size;
1731 int *args_type;
1733 /* Positional parameters refer to arguments directly. This could
1734 also determine the maximum number of arguments. Track the
1735 maximum number. */
1736 size_t max_ref_arg = 0;
1738 /* Just a counter. */
1739 size_t cnt;
1741 CHAR_T *workstart = NULL;
1743 if (grouping == (const char *) -1)
1745 #ifdef COMPILE_WPRINTF
1746 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1747 _NL_NUMERIC_THOUSANDS_SEP_WC);
1748 #else
1749 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1750 #endif
1752 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1753 if (*grouping == '\0' || *grouping == CHAR_MAX)
1754 grouping = NULL;
1757 for (const UCHAR_T *f = lead_str_end; *f != L_('\0');
1758 f = specs[nspecs++].next_fmt)
1760 if (nspecs == specs_limit)
1762 if (!scratch_buffer_grow_preserve (&specsbuf))
1764 done = -1;
1765 goto all_done;
1767 specs = specsbuf.data;
1768 specs_limit = specsbuf.length / sizeof (specs[0]);
1771 /* Parse the format specifier. */
1772 #ifdef COMPILE_WPRINTF
1773 nargs += __parse_one_specwc (f, nargs, &specs[nspecs], &max_ref_arg);
1774 #else
1775 nargs += __parse_one_specmb (f, nargs, &specs[nspecs], &max_ref_arg);
1776 #endif
1779 /* Determine the number of arguments the format string consumes. */
1780 nargs = MAX (nargs, max_ref_arg);
1781 /* Calculate total size needed to represent a single argument across
1782 all three argument-related arrays. */
1783 bytes_per_arg = (sizeof (*args_value) + sizeof (*args_size)
1784 + sizeof (*args_type));
1786 /* Check for potential integer overflow. */
1787 if (__glibc_unlikely (nargs > INT_MAX / bytes_per_arg))
1789 __set_errno (EOVERFLOW);
1790 done = -1;
1791 goto all_done;
1794 /* Allocate memory for all three argument arrays. */
1795 if (__libc_use_alloca (nargs * bytes_per_arg))
1796 args_value = alloca (nargs * bytes_per_arg);
1797 else
1799 args_value = args_malloced = malloc (nargs * bytes_per_arg);
1800 if (args_value == NULL)
1802 done = -1;
1803 goto all_done;
1807 /* Set up the remaining two arrays to each point past the end of the
1808 prior array, since space for all three has been allocated now. */
1809 args_size = &args_value[nargs].pa_int;
1810 args_type = &args_size[nargs];
1811 memset (args_type, s->_flags2 & _IO_FLAGS2_FORTIFY ? '\xff' : '\0',
1812 nargs * sizeof (*args_type));
1814 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1815 still zero after this loop, format is invalid. For now we
1816 simply use 0 as the value. */
1818 /* Fill in the types of all the arguments. */
1819 for (cnt = 0; cnt < nspecs; ++cnt)
1821 /* If the width is determined by an argument this is an int. */
1822 if (specs[cnt].width_arg != -1)
1823 args_type[specs[cnt].width_arg] = PA_INT;
1825 /* If the precision is determined by an argument this is an int. */
1826 if (specs[cnt].prec_arg != -1)
1827 args_type[specs[cnt].prec_arg] = PA_INT;
1829 switch (specs[cnt].ndata_args)
1831 case 0: /* No arguments. */
1832 break;
1833 case 1: /* One argument; we already have the
1834 type and size. */
1835 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1836 args_size[specs[cnt].data_arg] = specs[cnt].size;
1837 break;
1838 default:
1839 /* We have more than one argument for this format spec.
1840 We must call the arginfo function again to determine
1841 all the types. */
1842 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1843 (&specs[cnt].info,
1844 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg],
1845 &args_size[specs[cnt].data_arg]);
1846 break;
1850 /* Now we know all the types and the order. Fill in the argument
1851 values. */
1852 for (cnt = 0; cnt < nargs; ++cnt)
1853 switch (args_type[cnt])
1855 #define T(tag, mem, type) \
1856 case tag: \
1857 args_value[cnt].mem = va_arg (*ap_savep, type); \
1858 break
1860 T (PA_WCHAR, pa_wchar, wint_t);
1861 case PA_CHAR: /* Promoted. */
1862 case PA_INT|PA_FLAG_SHORT: /* Promoted. */
1863 #if LONG_MAX == INT_MAX
1864 case PA_INT|PA_FLAG_LONG:
1865 #endif
1866 T (PA_INT, pa_int, int);
1867 #if LONG_MAX == LONG_LONG_MAX
1868 case PA_INT|PA_FLAG_LONG:
1869 #endif
1870 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1871 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1872 # error "he?"
1873 #endif
1874 case PA_FLOAT: /* Promoted. */
1875 T (PA_DOUBLE, pa_double, double);
1876 case PA_DOUBLE|PA_FLAG_LONG_DOUBLE:
1877 if (__ldbl_is_dbl)
1879 args_value[cnt].pa_double = va_arg (*ap_savep, double);
1880 args_type[cnt] &= ~PA_FLAG_LONG_DOUBLE;
1882 else
1883 args_value[cnt].pa_long_double = va_arg (*ap_savep, long double);
1884 break;
1885 case PA_STRING: /* All pointers are the same */
1886 case PA_WSTRING: /* All pointers are the same */
1887 T (PA_POINTER, pa_pointer, void *);
1888 #undef T
1889 default:
1890 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1891 args_value[cnt].pa_pointer = va_arg (*ap_savep, void *);
1892 else if (__glibc_unlikely (__printf_va_arg_table != NULL)
1893 && __printf_va_arg_table[args_type[cnt] - PA_LAST] != NULL)
1895 args_value[cnt].pa_user = alloca (args_size[cnt]);
1896 (*__printf_va_arg_table[args_type[cnt] - PA_LAST])
1897 (args_value[cnt].pa_user, ap_savep);
1899 else
1900 args_value[cnt].pa_long_double = 0.0;
1901 break;
1902 case -1:
1903 /* Error case. Not all parameters appear in N$ format
1904 strings. We have no way to determine their type. */
1905 assert (s->_flags2 & _IO_FLAGS2_FORTIFY);
1906 __libc_fatal ("*** invalid %N$ use detected ***\n");
1909 /* Now walk through all format specifiers and process them. */
1910 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1912 STEP4_TABLE;
1914 int is_negative;
1915 union
1917 unsigned long long int longlong;
1918 unsigned long int word;
1919 } number;
1920 int base;
1921 union printf_arg the_arg;
1922 CHAR_T *string; /* Pointer to argument string. */
1924 /* Fill variables from values in struct. */
1925 int alt = specs[nspecs_done].info.alt;
1926 int space = specs[nspecs_done].info.space;
1927 int left = specs[nspecs_done].info.left;
1928 int showsign = specs[nspecs_done].info.showsign;
1929 int group = specs[nspecs_done].info.group;
1930 int is_long_double = specs[nspecs_done].info.is_long_double;
1931 int is_short = specs[nspecs_done].info.is_short;
1932 int is_char = specs[nspecs_done].info.is_char;
1933 int is_long = specs[nspecs_done].info.is_long;
1934 int width = specs[nspecs_done].info.width;
1935 int prec = specs[nspecs_done].info.prec;
1936 int use_outdigits = specs[nspecs_done].info.i18n;
1937 char pad = specs[nspecs_done].info.pad;
1938 CHAR_T spec = specs[nspecs_done].info.spec;
1940 workstart = NULL;
1941 CHAR_T *workend = work_buffer + WORK_BUFFER_SIZE;
1943 /* Fill in last information. */
1944 if (specs[nspecs_done].width_arg != -1)
1946 /* Extract the field width from an argument. */
1947 specs[nspecs_done].info.width =
1948 args_value[specs[nspecs_done].width_arg].pa_int;
1950 if (specs[nspecs_done].info.width < 0)
1951 /* If the width value is negative left justification is
1952 selected and the value is taken as being positive. */
1954 specs[nspecs_done].info.width *= -1;
1955 left = specs[nspecs_done].info.left = 1;
1957 width = specs[nspecs_done].info.width;
1960 if (specs[nspecs_done].prec_arg != -1)
1962 /* Extract the precision from an argument. */
1963 specs[nspecs_done].info.prec =
1964 args_value[specs[nspecs_done].prec_arg].pa_int;
1966 if (specs[nspecs_done].info.prec < 0)
1967 /* If the precision is negative the precision is
1968 omitted. */
1969 specs[nspecs_done].info.prec = -1;
1971 prec = specs[nspecs_done].info.prec;
1974 /* Maybe the buffer is too small. */
1975 if (MAX (prec, width) + EXTSIZ > WORK_BUFFER_SIZE)
1977 if (__libc_use_alloca ((MAX (prec, width) + EXTSIZ)
1978 * sizeof (CHAR_T)))
1979 workend = ((CHAR_T *) alloca ((MAX (prec, width) + EXTSIZ)
1980 * sizeof (CHAR_T))
1981 + (MAX (prec, width) + EXTSIZ));
1982 else
1984 workstart = (CHAR_T *) malloc ((MAX (prec, width) + EXTSIZ)
1985 * sizeof (CHAR_T));
1986 if (workstart == NULL)
1988 done = -1;
1989 goto all_done;
1991 workend = workstart + (MAX (prec, width) + EXTSIZ);
1995 /* Process format specifiers. */
1996 while (1)
1998 extern printf_function **__printf_function_table;
1999 int function_done;
2001 if (spec <= UCHAR_MAX
2002 && __printf_function_table != NULL
2003 && __printf_function_table[(size_t) spec] != NULL)
2005 const void **ptr = alloca (specs[nspecs_done].ndata_args
2006 * sizeof (const void *));
2008 /* Fill in an array of pointers to the argument values. */
2009 for (unsigned int i = 0; i < specs[nspecs_done].ndata_args;
2010 ++i)
2011 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
2013 /* Call the function. */
2014 function_done = __printf_function_table[(size_t) spec]
2015 (s, &specs[nspecs_done].info, ptr);
2017 if (function_done != -2)
2019 /* If an error occurred we don't have information
2020 about # of chars. */
2021 if (function_done < 0)
2023 /* Function has set errno. */
2024 done = -1;
2025 goto all_done;
2028 done_add (function_done);
2029 break;
2033 JUMP (spec, step4_jumps);
2035 process_arg ((&specs[nspecs_done]));
2036 process_string_arg ((&specs[nspecs_done]));
2038 LABEL (form_unknown):
2040 unsigned int i;
2041 const void **ptr;
2043 ptr = alloca (specs[nspecs_done].ndata_args
2044 * sizeof (const void *));
2046 /* Fill in an array of pointers to the argument values. */
2047 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
2048 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
2050 /* Call the function. */
2051 function_done = printf_unknown (s, &specs[nspecs_done].info,
2052 ptr);
2054 /* If an error occurred we don't have information about #
2055 of chars. */
2056 if (function_done < 0)
2058 /* Function has set errno. */
2059 done = -1;
2060 goto all_done;
2063 done_add (function_done);
2065 break;
2068 if (__glibc_unlikely (workstart != NULL))
2069 free (workstart);
2070 workstart = NULL;
2072 /* Write the following constant string. */
2073 outstring (specs[nspecs_done].end_of_fmt,
2074 specs[nspecs_done].next_fmt
2075 - specs[nspecs_done].end_of_fmt);
2077 all_done:
2078 if (__glibc_unlikely (args_malloced != NULL))
2079 free (args_malloced);
2080 if (__glibc_unlikely (workstart != NULL))
2081 free (workstart);
2082 scratch_buffer_free (&specsbuf);
2083 return done;
2086 /* Handle an unknown format specifier. This prints out a canonicalized
2087 representation of the format spec itself. */
2088 static int
2089 printf_unknown (FILE *s, const struct printf_info *info,
2090 const void *const *args)
2093 int done = 0;
2094 CHAR_T work_buffer[MAX (sizeof (info->width), sizeof (info->prec)) * 3];
2095 CHAR_T *const workend
2096 = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
2097 CHAR_T *w;
2099 outchar (L_('%'));
2101 if (info->alt)
2102 outchar (L_('#'));
2103 if (info->group)
2104 outchar (L_('\''));
2105 if (info->showsign)
2106 outchar (L_('+'));
2107 else if (info->space)
2108 outchar (L_(' '));
2109 if (info->left)
2110 outchar (L_('-'));
2111 if (info->pad == L_('0'))
2112 outchar (L_('0'));
2113 if (info->i18n)
2114 outchar (L_('I'));
2116 if (info->width != 0)
2118 w = _itoa_word (info->width, workend, 10, 0);
2119 while (w < workend)
2120 outchar (*w++);
2123 if (info->prec != -1)
2125 outchar (L_('.'));
2126 w = _itoa_word (info->prec, workend, 10, 0);
2127 while (w < workend)
2128 outchar (*w++);
2131 if (info->spec != L_('\0'))
2132 outchar (info->spec);
2134 all_done:
2135 return done;
2138 /* Group the digits according to the grouping rules of the current locale.
2139 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
2140 static CHAR_T *
2141 internal_function
2142 group_number (CHAR_T *w, CHAR_T *rear_ptr, const char *grouping,
2143 THOUSANDS_SEP_T thousands_sep)
2145 int len;
2146 CHAR_T *src, *s;
2147 #ifndef COMPILE_WPRINTF
2148 int tlen = strlen (thousands_sep);
2149 #endif
2151 /* We treat all negative values like CHAR_MAX. */
2153 if (*grouping == CHAR_MAX || *grouping <= 0)
2154 /* No grouping should be done. */
2155 return w;
2157 len = *grouping++;
2159 /* Copy existing string so that nothing gets overwritten. */
2160 src = (CHAR_T *) alloca ((rear_ptr - w) * sizeof (CHAR_T));
2161 s = (CHAR_T *) __mempcpy (src, w,
2162 (rear_ptr - w) * sizeof (CHAR_T));
2163 w = rear_ptr;
2165 /* Process all characters in the string. */
2166 while (s > src)
2168 *--w = *--s;
2170 if (--len == 0 && s > src)
2172 /* A new group begins. */
2173 #ifdef COMPILE_WPRINTF
2174 *--w = thousands_sep;
2175 #else
2176 int cnt = tlen;
2178 *--w = thousands_sep[--cnt];
2179 while (cnt > 0);
2180 #endif
2182 if (*grouping == CHAR_MAX
2183 #if CHAR_MIN < 0
2184 || *grouping < 0
2185 #endif
2188 /* No further grouping to be done.
2189 Copy the rest of the number. */
2191 *--w = *--s;
2192 while (s > src);
2193 break;
2195 else if (*grouping != '\0')
2196 /* The previous grouping repeats ad infinitum. */
2197 len = *grouping++;
2198 else
2199 len = grouping[-1];
2202 return w;
2205 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2206 struct helper_file
2208 struct _IO_FILE_plus _f;
2209 #ifdef COMPILE_WPRINTF
2210 struct _IO_wide_data _wide_data;
2211 #endif
2212 _IO_FILE *_put_stream;
2213 #ifdef _IO_MTSAFE_IO
2214 _IO_lock_t lock;
2215 #endif
2218 static int
2219 _IO_helper_overflow (_IO_FILE *s, int c)
2221 _IO_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)
2226 _IO_size_t written = _IO_sputn (target, s->_wide_data->_IO_write_base,
2227 used);
2228 if (written == 0 || written == WEOF)
2229 return WEOF;
2230 __wmemmove (s->_wide_data->_IO_write_base,
2231 s->_wide_data->_IO_write_base + written,
2232 used - written);
2233 s->_wide_data->_IO_write_ptr -= written;
2235 #else
2236 int used = s->_IO_write_ptr - s->_IO_write_base;
2237 if (used)
2239 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
2240 if (written == 0 || written == EOF)
2241 return EOF;
2242 memmove (s->_IO_write_base, s->_IO_write_base + written,
2243 used - written);
2244 s->_IO_write_ptr -= written;
2246 #endif
2247 return PUTC (c, s);
2250 #ifdef COMPILE_WPRINTF
2251 static const struct _IO_jump_t _IO_helper_jumps libio_vtable =
2253 JUMP_INIT_DUMMY,
2254 JUMP_INIT (finish, _IO_wdefault_finish),
2255 JUMP_INIT (overflow, _IO_helper_overflow),
2256 JUMP_INIT (underflow, _IO_default_underflow),
2257 JUMP_INIT (uflow, _IO_default_uflow),
2258 JUMP_INIT (pbackfail, (_IO_pbackfail_t) _IO_wdefault_pbackfail),
2259 JUMP_INIT (xsputn, _IO_wdefault_xsputn),
2260 JUMP_INIT (xsgetn, _IO_wdefault_xsgetn),
2261 JUMP_INIT (seekoff, _IO_default_seekoff),
2262 JUMP_INIT (seekpos, _IO_default_seekpos),
2263 JUMP_INIT (setbuf, _IO_default_setbuf),
2264 JUMP_INIT (sync, _IO_default_sync),
2265 JUMP_INIT (doallocate, _IO_wdefault_doallocate),
2266 JUMP_INIT (read, _IO_default_read),
2267 JUMP_INIT (write, _IO_default_write),
2268 JUMP_INIT (seek, _IO_default_seek),
2269 JUMP_INIT (close, _IO_default_close),
2270 JUMP_INIT (stat, _IO_default_stat)
2272 #else
2273 static const struct _IO_jump_t _IO_helper_jumps libio_vtable =
2275 JUMP_INIT_DUMMY,
2276 JUMP_INIT (finish, _IO_default_finish),
2277 JUMP_INIT (overflow, _IO_helper_overflow),
2278 JUMP_INIT (underflow, _IO_default_underflow),
2279 JUMP_INIT (uflow, _IO_default_uflow),
2280 JUMP_INIT (pbackfail, _IO_default_pbackfail),
2281 JUMP_INIT (xsputn, _IO_default_xsputn),
2282 JUMP_INIT (xsgetn, _IO_default_xsgetn),
2283 JUMP_INIT (seekoff, _IO_default_seekoff),
2284 JUMP_INIT (seekpos, _IO_default_seekpos),
2285 JUMP_INIT (setbuf, _IO_default_setbuf),
2286 JUMP_INIT (sync, _IO_default_sync),
2287 JUMP_INIT (doallocate, _IO_default_doallocate),
2288 JUMP_INIT (read, _IO_default_read),
2289 JUMP_INIT (write, _IO_default_write),
2290 JUMP_INIT (seek, _IO_default_seek),
2291 JUMP_INIT (close, _IO_default_close),
2292 JUMP_INIT (stat, _IO_default_stat)
2294 #endif
2296 static int
2297 internal_function
2298 buffered_vfprintf (_IO_FILE *s, const CHAR_T *format,
2299 _IO_va_list args)
2301 CHAR_T buf[_IO_BUFSIZ];
2302 struct helper_file helper;
2303 _IO_FILE *hp = (_IO_FILE *) &helper._f;
2304 int result, to_flush;
2306 /* Orient the stream. */
2307 #ifdef ORIENT
2308 ORIENT;
2309 #endif
2311 /* Initialize helper. */
2312 helper._put_stream = s;
2313 #ifdef COMPILE_WPRINTF
2314 hp->_wide_data = &helper._wide_data;
2315 _IO_wsetp (hp, buf, buf + sizeof buf / sizeof (CHAR_T));
2316 hp->_mode = 1;
2317 #else
2318 _IO_setp (hp, buf, buf + sizeof buf);
2319 hp->_mode = -1;
2320 #endif
2321 hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS|_IO_USER_LOCK;
2322 #if _IO_JUMPS_OFFSET
2323 hp->_vtable_offset = 0;
2324 #endif
2325 #ifdef _IO_MTSAFE_IO
2326 hp->_lock = NULL;
2327 #endif
2328 hp->_flags2 = s->_flags2;
2329 _IO_JUMPS (&helper._f) = (struct _IO_jump_t *) &_IO_helper_jumps;
2331 /* Now print to helper instead. */
2332 #ifndef COMPILE_WPRINTF
2333 result = _IO_vfprintf (hp, format, args);
2334 #else
2335 result = vfprintf (hp, format, args);
2336 #endif
2338 /* Lock stream. */
2339 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, s);
2340 _IO_flockfile (s);
2342 /* Now flush anything from the helper to the S. */
2343 #ifdef COMPILE_WPRINTF
2344 if ((to_flush = (hp->_wide_data->_IO_write_ptr
2345 - hp->_wide_data->_IO_write_base)) > 0)
2347 if ((int) _IO_sputn (s, hp->_wide_data->_IO_write_base, to_flush)
2348 != to_flush)
2349 result = -1;
2351 #else
2352 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
2354 if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
2355 result = -1;
2357 #endif
2359 /* Unlock the stream. */
2360 _IO_funlockfile (s);
2361 __libc_cleanup_region_end (0);
2363 return result;
2366 #undef vfprintf
2367 #ifdef COMPILE_WPRINTF
2368 strong_alias (_IO_vfwprintf, __vfwprintf);
2369 ldbl_weak_alias (_IO_vfwprintf, vfwprintf);
2370 #else
2371 ldbl_strong_alias (_IO_vfprintf_internal, vfprintf);
2372 ldbl_hidden_def (_IO_vfprintf_internal, vfprintf)
2373 ldbl_strong_alias (_IO_vfprintf_internal, _IO_vfprintf);
2374 ldbl_hidden_def (_IO_vfprintf_internal, _IO_vfprintf)
2375 #endif