Pashto yesstr/nostr locale are missing
[glibc.git] / stdio-common / vfprintf.c
blob089bcefaf9a2e461b772b5d06310730eff87e394
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 / sizeof (CHAR_T) };
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 (work_buffer, string, workend, \
599 grouping, thousands_sep); \
600 if (use_outdigits && base == 10) \
601 string = _i18n_number_rewrite (string, workend, workend); \
603 /* Simplify further test for num != 0. */ \
604 number.word = number.longlong != 0; \
606 else \
608 if (fspec == NULL) \
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); \
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; \
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_(' '); \
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) \
645 string = workend; \
646 if (base == 8 && alt) \
647 *--string = L_('0'); \
649 else \
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); \
662 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
663 /* Add octal marker. */ \
664 *--string = L_('0'); \
666 prec = MAX (0, prec - (workend - string)); \
668 if (!left) \
670 width -= workend - string + prec; \
672 if (number.word != 0 && alt && base == 16) \
673 /* Account for 0X hex marker. */ \
674 width -= 2; \
676 if (is_negative || showsign || space) \
677 --width; \
679 if (pad == L_(' ')) \
681 PAD (L_(' ')); \
682 width = 0; \
685 if (is_negative) \
686 outchar (L_('-')); \
687 else if (showsign) \
688 outchar (L_('+')); \
689 else if (space) \
690 outchar (L_(' ')); \
692 if (number.word != 0 && alt && base == 16) \
694 outchar (L_('0')); \
695 outchar (spec); \
698 width += prec; \
699 PAD (L_('0')); \
701 outstring (string, workend - string); \
703 break; \
705 else \
707 if (is_negative) \
709 outchar (L_('-')); \
710 --width; \
712 else if (showsign) \
714 outchar (L_('+')); \
715 --width; \
717 else if (space) \
719 outchar (L_(' ')); \
720 --width; \
723 if (number.word != 0 && alt && base == 16) \
725 outchar (L_('0')); \
726 outchar (spec); \
727 width -= 2; \
730 width -= workend - string + prec; \
732 if (prec > 0) \
734 int temp = width; \
735 width = prec; \
736 PAD (L_('0')); \
737 width = temp; \
740 outstring (string, workend - string); \
742 PAD (L_(' ')); \
743 break; \
746 LABEL (form_float): \
748 /* Floating-point number. This is handled by printf_fp.c. */ \
749 const void *ptr; \
750 int function_done; \
752 if (fspec == NULL) \
754 if (__ldbl_is_dbl) \
755 is_long_double = 0; \
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}; \
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; \
780 function_done = __printf_fp (s, &info, &ptr); \
782 else \
784 ptr = (const void *) &args_value[fspec->data_arg]; \
785 if (__ldbl_is_dbl) \
787 fspec->data_arg_type = PA_DOUBLE; \
788 fspec->info.is_long_double = 0; \
790 /* Not supported by *printf functions. */ \
791 fspec->info.is_binary128 = 0; \
793 function_done = __printf_fp (s, &fspec->info, &ptr); \
796 if (function_done < 0) \
798 /* Error in print handler; up to handler to set errno. */ \
799 done = -1; \
800 goto all_done; \
803 done_add (function_done); \
805 break; \
807 LABEL (form_floathex): \
809 /* Floating point number printed as hexadecimal number. */ \
810 const void *ptr; \
811 int function_done; \
813 if (fspec == NULL) \
815 if (__ldbl_is_dbl) \
816 is_long_double = 0; \
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}; \
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; \
840 function_done = __printf_fphex (s, &info, &ptr); \
842 else \
844 ptr = (const void *) &args_value[fspec->data_arg]; \
845 if (__ldbl_is_dbl) \
846 fspec->info.is_long_double = 0; \
847 /* Not supported by *printf functions. */ \
848 fspec->info.is_binary128 = 0; \
850 function_done = __printf_fphex (s, &fspec->info, &ptr); \
853 if (function_done < 0) \
855 /* Error in print handler; up to handler to set errno. */ \
856 done = -1; \
857 goto all_done; \
860 done_add (function_done); \
862 break; \
864 LABEL (form_pointer): \
865 /* Generic pointer. */ \
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) \
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); \
883 else \
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); \
895 /* NOTREACHED */ \
897 LABEL (form_number): \
898 if (s->_flags2 & _IO_FLAGS2_FORTIFY) \
900 if (! readonly_format) \
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))); \
908 if (readonly_format < 0) \
909 __libc_fatal ("*** %n in writable segment detected ***\n"); \
911 /* Answer the count of characters written. */ \
912 if (fspec == NULL) \
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; \
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; \
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)
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; \
964 LABEL (form_wcharacter): \
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' '); \
977 break; \
979 LABEL (form_string): \
981 size_t len; \
982 int string_malloced; \
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; \
991 /* Entry point for printing other strings. */ \
992 LABEL (print_string): \
994 string_malloced = 0; \
995 if (string == NULL) \
997 /* Write "(null)" if there's space. */ \
998 if (prec == -1 \
999 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
1001 string = (CHAR_T *) null; \
1002 len = (sizeof (null) / sizeof (null[0])) - 1; \
1004 else \
1006 string = (CHAR_T *) L""; \
1007 len = 0; \
1010 else if (!is_long && spec != L_('S')) \
1012 /* This is complicated. We have to transform the multibyte \
1013 string into a wide character string. */ \
1014 const char *mbs = (const char *) string; \
1015 mbstate_t mbstate; \
1017 len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
1019 /* Allocate dynamically an array which definitely is long \
1020 enough for the wide character version. Each byte in the \
1021 multi-byte string can produce at most one wide character. */ \
1022 if (__glibc_unlikely (len > SIZE_MAX / sizeof (wchar_t))) \
1024 __set_errno (EOVERFLOW); \
1025 done = -1; \
1026 goto all_done; \
1028 else if (__libc_use_alloca (len * sizeof (wchar_t))) \
1029 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1030 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1031 == NULL) \
1033 done = -1; \
1034 goto all_done; \
1036 else \
1037 string_malloced = 1; \
1039 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1040 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1041 if (len == (size_t) -1) \
1043 /* Illegal multibyte character. */ \
1044 done = -1; \
1045 goto all_done; \
1048 else \
1050 if (prec != -1) \
1051 /* Search for the end of the string, but don't search past \
1052 the length specified by the precision. */ \
1053 len = __wcsnlen (string, prec); \
1054 else \
1055 len = __wcslen (string); \
1058 if ((width -= len) < 0) \
1060 outstring (string, len); \
1061 break; \
1064 if (!left) \
1065 PAD (L' '); \
1066 outstring (string, len); \
1067 if (left) \
1068 PAD (L' '); \
1069 if (__glibc_unlikely (string_malloced)) \
1070 free (string); \
1072 break;
1073 #else
1074 # define process_string_arg(fspec) \
1075 LABEL (form_character): \
1076 /* Character. */ \
1077 if (is_long) \
1078 goto LABEL (form_wcharacter); \
1079 --width; /* Account for the character itself. */ \
1080 if (!left) \
1081 PAD (' '); \
1082 if (fspec == NULL) \
1083 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1084 else \
1085 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1086 if (left) \
1087 PAD (' '); \
1088 break; \
1090 LABEL (form_wcharacter): \
1092 /* Wide character. */ \
1093 char buf[MB_LEN_MAX]; \
1094 mbstate_t mbstate; \
1095 size_t len; \
1097 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1098 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1099 : args_value[fspec->data_arg].pa_wchar), \
1100 &mbstate); \
1101 if (len == (size_t) -1) \
1103 /* Something went wrong during the conversion. Bail out. */ \
1104 done = -1; \
1105 goto all_done; \
1107 width -= len; \
1108 if (!left) \
1109 PAD (' '); \
1110 outstring (buf, len); \
1111 if (left) \
1112 PAD (' '); \
1114 break; \
1116 LABEL (form_string): \
1118 size_t len; \
1119 int string_malloced; \
1121 /* The string argument could in fact be `char *' or `wchar_t *'. \
1122 But this should not make a difference here. */ \
1123 if (fspec == NULL) \
1124 string = (char *) va_arg (ap, const char *); \
1125 else \
1126 string = (char *) args_value[fspec->data_arg].pa_string; \
1128 /* Entry point for printing other strings. */ \
1129 LABEL (print_string): \
1131 string_malloced = 0; \
1132 if (string == NULL) \
1134 /* Write "(null)" if there's space. */ \
1135 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1137 string = (char *) null; \
1138 len = sizeof (null) - 1; \
1140 else \
1142 string = (char *) ""; \
1143 len = 0; \
1146 else if (!is_long && spec != L_('S')) \
1148 if (prec != -1) \
1149 /* Search for the end of the string, but don't search past \
1150 the length (in bytes) specified by the precision. */ \
1151 len = __strnlen (string, prec); \
1152 else \
1153 len = strlen (string); \
1155 else \
1157 const wchar_t *s2 = (const wchar_t *) string; \
1158 mbstate_t mbstate; \
1160 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1162 if (prec >= 0) \
1164 /* The string `s2' might not be NUL terminated. */ \
1165 if (__libc_use_alloca (prec)) \
1166 string = (char *) alloca (prec); \
1167 else if ((string = (char *) malloc (prec)) == NULL) \
1169 done = -1; \
1170 goto all_done; \
1172 else \
1173 string_malloced = 1; \
1174 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1176 else \
1178 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1179 if (len != (size_t) -1) \
1181 assert (__mbsinit (&mbstate)); \
1182 s2 = (const wchar_t *) string; \
1183 if (__libc_use_alloca (len + 1)) \
1184 string = (char *) alloca (len + 1); \
1185 else if ((string = (char *) malloc (len + 1)) == NULL) \
1187 done = -1; \
1188 goto all_done; \
1190 else \
1191 string_malloced = 1; \
1192 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1196 if (len == (size_t) -1) \
1198 /* Illegal wide-character string. */ \
1199 done = -1; \
1200 goto all_done; \
1204 if ((width -= len) < 0) \
1206 outstring (string, len); \
1207 break; \
1210 if (!left) \
1211 PAD (' '); \
1212 outstring (string, len); \
1213 if (left) \
1214 PAD (' '); \
1215 if (__glibc_unlikely (string_malloced)) \
1216 free (string); \
1218 break;
1219 #endif
1221 /* Helper function to provide temporary buffering for unbuffered streams. */
1222 static int buffered_vfprintf (FILE *stream, const CHAR_T *fmt, va_list)
1223 __THROW __attribute__ ((noinline)) internal_function;
1225 /* Handle positional format specifiers. */
1226 static int printf_positional (_IO_FILE *s,
1227 const CHAR_T *format, int readonly_format,
1228 va_list ap, va_list *ap_savep, int done,
1229 int nspecs_done, const UCHAR_T *lead_str_end,
1230 CHAR_T *work_buffer, int save_errno,
1231 const char *grouping, THOUSANDS_SEP_T);
1233 /* Handle unknown format specifier. */
1234 static int printf_unknown (FILE *, const struct printf_info *,
1235 const void *const *) __THROW;
1237 /* Group digits of number string. */
1238 static CHAR_T *group_number (CHAR_T *, CHAR_T *, CHAR_T *, const char *,
1239 THOUSANDS_SEP_T);
1241 /* The function itself. */
1243 vfprintf (FILE *s, const CHAR_T *format, va_list ap)
1245 /* The character used as thousands separator. */
1246 THOUSANDS_SEP_T thousands_sep = 0;
1248 /* The string describing the size of groups of digits. */
1249 const char *grouping;
1251 /* Place to accumulate the result. */
1252 int done;
1254 /* Current character in format string. */
1255 const UCHAR_T *f;
1257 /* End of leading constant string. */
1258 const UCHAR_T *lead_str_end;
1260 /* Points to next format specifier. */
1261 const UCHAR_T *end_of_spec;
1263 /* Buffer intermediate results. */
1264 CHAR_T work_buffer[WORK_BUFFER_SIZE];
1265 CHAR_T *workstart = NULL;
1266 CHAR_T *workend;
1268 /* We have to save the original argument pointer. */
1269 va_list ap_save;
1271 /* Count number of specifiers we already processed. */
1272 int nspecs_done;
1274 /* For the %m format we may need the current `errno' value. */
1275 int save_errno = errno;
1277 /* 1 if format is in read-only memory, -1 if it is in writable memory,
1278 0 if unknown. */
1279 int readonly_format = 0;
1281 /* Orient the stream. */
1282 #ifdef ORIENT
1283 ORIENT;
1284 #endif
1286 /* Sanity check of arguments. */
1287 ARGCHECK (s, format);
1289 #ifdef ORIENT
1290 /* Check for correct orientation. */
1291 if (_IO_vtable_offset (s) == 0 &&
1292 _IO_fwide (s, sizeof (CHAR_T) == 1 ? -1 : 1)
1293 != (sizeof (CHAR_T) == 1 ? -1 : 1))
1294 /* The stream is already oriented otherwise. */
1295 return EOF;
1296 #endif
1298 if (UNBUFFERED_P (s))
1299 /* Use a helper function which will allocate a local temporary buffer
1300 for the stream and then call us again. */
1301 return buffered_vfprintf (s, format, ap);
1303 /* Initialize local variables. */
1304 done = 0;
1305 grouping = (const char *) -1;
1306 #ifdef __va_copy
1307 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1308 since on some systems `va_list' is not an integral type. */
1309 __va_copy (ap_save, ap);
1310 #else
1311 ap_save = ap;
1312 #endif
1313 nspecs_done = 0;
1315 #ifdef COMPILE_WPRINTF
1316 /* Find the first format specifier. */
1317 f = lead_str_end = __find_specwc ((const UCHAR_T *) format);
1318 #else
1319 /* Find the first format specifier. */
1320 f = lead_str_end = __find_specmb ((const UCHAR_T *) format);
1321 #endif
1323 /* Lock stream. */
1324 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
1325 _IO_flockfile (s);
1327 /* Write the literal text before the first format. */
1328 outstring ((const UCHAR_T *) format,
1329 lead_str_end - (const UCHAR_T *) format);
1331 /* If we only have to print a simple string, return now. */
1332 if (*f == L_('\0'))
1333 goto all_done;
1335 /* Use the slow path in case any printf handler is registered. */
1336 if (__glibc_unlikely (__printf_function_table != NULL
1337 || __printf_modifier_table != NULL
1338 || __printf_va_arg_table != NULL))
1339 goto do_positional;
1341 /* Process whole format string. */
1344 STEP0_3_TABLE;
1345 STEP4_TABLE;
1347 union printf_arg *args_value; /* This is not used here but ... */
1348 int is_negative; /* Flag for negative number. */
1349 union
1351 unsigned long long int longlong;
1352 unsigned long int word;
1353 } number;
1354 int base;
1355 union printf_arg the_arg;
1356 CHAR_T *string; /* Pointer to argument string. */
1357 int alt = 0; /* Alternate format. */
1358 int space = 0; /* Use space prefix if no sign is needed. */
1359 int left = 0; /* Left-justify output. */
1360 int showsign = 0; /* Always begin with plus or minus sign. */
1361 int group = 0; /* Print numbers according grouping rules. */
1362 int is_long_double = 0; /* Argument is long double/ long long int. */
1363 int is_short = 0; /* Argument is short int. */
1364 int is_long = 0; /* Argument is long int. */
1365 int is_char = 0; /* Argument is promoted (unsigned) char. */
1366 int width = 0; /* Width of output; 0 means none specified. */
1367 int prec = -1; /* Precision of output; -1 means none specified. */
1368 /* This flag is set by the 'I' modifier and selects the use of the
1369 `outdigits' as determined by the current locale. */
1370 int use_outdigits = 0;
1371 UCHAR_T pad = L_(' ');/* Padding character. */
1372 CHAR_T spec;
1374 workstart = NULL;
1375 workend = work_buffer + WORK_BUFFER_SIZE;
1377 /* Get current character in format string. */
1378 JUMP (*++f, step0_jumps);
1380 /* ' ' flag. */
1381 LABEL (flag_space):
1382 space = 1;
1383 JUMP (*++f, step0_jumps);
1385 /* '+' flag. */
1386 LABEL (flag_plus):
1387 showsign = 1;
1388 JUMP (*++f, step0_jumps);
1390 /* The '-' flag. */
1391 LABEL (flag_minus):
1392 left = 1;
1393 pad = L_(' ');
1394 JUMP (*++f, step0_jumps);
1396 /* The '#' flag. */
1397 LABEL (flag_hash):
1398 alt = 1;
1399 JUMP (*++f, step0_jumps);
1401 /* The '0' flag. */
1402 LABEL (flag_zero):
1403 if (!left)
1404 pad = L_('0');
1405 JUMP (*++f, step0_jumps);
1407 /* The '\'' flag. */
1408 LABEL (flag_quote):
1409 group = 1;
1411 if (grouping == (const char *) -1)
1413 #ifdef COMPILE_WPRINTF
1414 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1415 _NL_NUMERIC_THOUSANDS_SEP_WC);
1416 #else
1417 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1418 #endif
1420 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1421 if (*grouping == '\0' || *grouping == CHAR_MAX
1422 #ifdef COMPILE_WPRINTF
1423 || thousands_sep == L'\0'
1424 #else
1425 || *thousands_sep == '\0'
1426 #endif
1428 grouping = NULL;
1430 JUMP (*++f, step0_jumps);
1432 LABEL (flag_i18n):
1433 use_outdigits = 1;
1434 JUMP (*++f, step0_jumps);
1436 /* Get width from argument. */
1437 LABEL (width_asterics):
1439 const UCHAR_T *tmp; /* Temporary value. */
1441 tmp = ++f;
1442 if (ISDIGIT (*tmp))
1444 int pos = read_int (&tmp);
1446 if (pos == -1)
1448 __set_errno (EOVERFLOW);
1449 done = -1;
1450 goto all_done;
1453 if (pos && *tmp == L_('$'))
1454 /* The width comes from a positional parameter. */
1455 goto do_positional;
1457 width = va_arg (ap, int);
1459 /* Negative width means left justified. */
1460 if (width < 0)
1462 width = -width;
1463 pad = L_(' ');
1464 left = 1;
1467 if (__glibc_unlikely (width >= INT_MAX / sizeof (CHAR_T) - EXTSIZ))
1469 __set_errno (EOVERFLOW);
1470 done = -1;
1471 goto all_done;
1474 if (width >= WORK_BUFFER_SIZE - EXTSIZ)
1476 /* We have to use a special buffer. */
1477 size_t needed = ((size_t) width + EXTSIZ) * sizeof (CHAR_T);
1478 if (__libc_use_alloca (needed))
1479 workend = (CHAR_T *) alloca (needed) + width + EXTSIZ;
1480 else
1482 workstart = (CHAR_T *) malloc (needed);
1483 if (workstart == NULL)
1485 done = -1;
1486 goto all_done;
1488 workend = workstart + width + EXTSIZ;
1492 JUMP (*f, step1_jumps);
1494 /* Given width in format string. */
1495 LABEL (width):
1496 width = read_int (&f);
1498 if (__glibc_unlikely (width == -1
1499 || width >= INT_MAX / sizeof (CHAR_T) - EXTSIZ))
1501 __set_errno (EOVERFLOW);
1502 done = -1;
1503 goto all_done;
1506 if (width >= WORK_BUFFER_SIZE - EXTSIZ)
1508 /* We have to use a special buffer. */
1509 size_t needed = ((size_t) width + EXTSIZ) * sizeof (CHAR_T);
1510 if (__libc_use_alloca (needed))
1511 workend = (CHAR_T *) alloca (needed) + width + EXTSIZ;
1512 else
1514 workstart = (CHAR_T *) malloc (needed);
1515 if (workstart == NULL)
1517 done = -1;
1518 goto all_done;
1520 workend = workstart + width + EXTSIZ;
1523 if (*f == L_('$'))
1524 /* Oh, oh. The argument comes from a positional parameter. */
1525 goto do_positional;
1526 JUMP (*f, step1_jumps);
1528 LABEL (precision):
1529 ++f;
1530 if (*f == L_('*'))
1532 const UCHAR_T *tmp; /* Temporary value. */
1534 tmp = ++f;
1535 if (ISDIGIT (*tmp))
1537 int pos = read_int (&tmp);
1539 if (pos == -1)
1541 __set_errno (EOVERFLOW);
1542 done = -1;
1543 goto all_done;
1546 if (pos && *tmp == L_('$'))
1547 /* The precision comes from a positional parameter. */
1548 goto do_positional;
1550 prec = va_arg (ap, int);
1552 /* If the precision is negative the precision is omitted. */
1553 if (prec < 0)
1554 prec = -1;
1556 else if (ISDIGIT (*f))
1558 prec = read_int (&f);
1560 /* The precision was specified in this case as an extremely
1561 large positive value. */
1562 if (prec == -1)
1564 __set_errno (EOVERFLOW);
1565 done = -1;
1566 goto all_done;
1569 else
1570 prec = 0;
1571 if (prec > width && prec > WORK_BUFFER_SIZE - EXTSIZ)
1573 /* Deallocate any previously allocated buffer because it is
1574 too small. */
1575 if (__glibc_unlikely (workstart != NULL))
1576 free (workstart);
1577 workstart = NULL;
1578 if (__glibc_unlikely (prec >= INT_MAX / sizeof (CHAR_T) - EXTSIZ))
1580 __set_errno (EOVERFLOW);
1581 done = -1;
1582 goto all_done;
1584 size_t needed = ((size_t) prec + EXTSIZ) * sizeof (CHAR_T);
1586 if (__libc_use_alloca (needed))
1587 workend = (CHAR_T *) alloca (needed) + prec + EXTSIZ;
1588 else
1590 workstart = (CHAR_T *) malloc (needed);
1591 if (workstart == NULL)
1593 done = -1;
1594 goto all_done;
1596 workend = workstart + prec + EXTSIZ;
1599 JUMP (*f, step2_jumps);
1601 /* Process 'h' modifier. There might another 'h' following. */
1602 LABEL (mod_half):
1603 is_short = 1;
1604 JUMP (*++f, step3a_jumps);
1606 /* Process 'hh' modifier. */
1607 LABEL (mod_halfhalf):
1608 is_short = 0;
1609 is_char = 1;
1610 JUMP (*++f, step4_jumps);
1612 /* Process 'l' modifier. There might another 'l' following. */
1613 LABEL (mod_long):
1614 is_long = 1;
1615 JUMP (*++f, step3b_jumps);
1617 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1618 allowed to follow. */
1619 LABEL (mod_longlong):
1620 is_long_double = 1;
1621 is_long = 1;
1622 JUMP (*++f, step4_jumps);
1624 LABEL (mod_size_t):
1625 is_long_double = sizeof (size_t) > sizeof (unsigned long int);
1626 is_long = sizeof (size_t) > sizeof (unsigned int);
1627 JUMP (*++f, step4_jumps);
1629 LABEL (mod_ptrdiff_t):
1630 is_long_double = sizeof (ptrdiff_t) > sizeof (unsigned long int);
1631 is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
1632 JUMP (*++f, step4_jumps);
1634 LABEL (mod_intmax_t):
1635 is_long_double = sizeof (intmax_t) > sizeof (unsigned long int);
1636 is_long = sizeof (intmax_t) > sizeof (unsigned int);
1637 JUMP (*++f, step4_jumps);
1639 /* Process current format. */
1640 while (1)
1642 process_arg (((struct printf_spec *) NULL));
1643 process_string_arg (((struct printf_spec *) NULL));
1645 LABEL (form_unknown):
1646 if (spec == L_('\0'))
1648 /* The format string ended before the specifier is complete. */
1649 __set_errno (EINVAL);
1650 done = -1;
1651 goto all_done;
1654 /* If we are in the fast loop force entering the complicated
1655 one. */
1656 goto do_positional;
1659 /* The format is correctly handled. */
1660 ++nspecs_done;
1662 if (__glibc_unlikely (workstart != NULL))
1663 free (workstart);
1664 workstart = NULL;
1666 /* Look for next format specifier. */
1667 #ifdef COMPILE_WPRINTF
1668 f = __find_specwc ((end_of_spec = ++f));
1669 #else
1670 f = __find_specmb ((end_of_spec = ++f));
1671 #endif
1673 /* Write the following constant string. */
1674 outstring (end_of_spec, f - end_of_spec);
1676 while (*f != L_('\0'));
1678 /* Unlock stream and return. */
1679 goto all_done;
1681 /* Hand off processing for positional parameters. */
1682 do_positional:
1683 if (__glibc_unlikely (workstart != NULL))
1685 free (workstart);
1686 workstart = NULL;
1688 done = printf_positional (s, format, readonly_format, ap, &ap_save,
1689 done, nspecs_done, lead_str_end, work_buffer,
1690 save_errno, grouping, thousands_sep);
1692 all_done:
1693 if (__glibc_unlikely (workstart != NULL))
1694 free (workstart);
1695 /* Unlock the stream. */
1696 _IO_funlockfile (s);
1697 _IO_cleanup_region_end (0);
1699 return done;
1702 static int
1703 printf_positional (_IO_FILE *s, const CHAR_T *format, int readonly_format,
1704 va_list ap, va_list *ap_savep, int done, int nspecs_done,
1705 const UCHAR_T *lead_str_end,
1706 CHAR_T *work_buffer, int save_errno,
1707 const char *grouping, THOUSANDS_SEP_T thousands_sep)
1709 /* For positional argument handling. */
1710 struct scratch_buffer specsbuf;
1711 scratch_buffer_init (&specsbuf);
1712 struct printf_spec *specs = specsbuf.data;
1713 size_t specs_limit = specsbuf.length / sizeof (specs[0]);
1715 /* Used as a backing store for args_value, args_size, args_type
1716 below. */
1717 struct scratch_buffer argsbuf;
1718 scratch_buffer_init (&argsbuf);
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;
1729 /* Positional parameters refer to arguments directly. This could
1730 also determine the maximum number of arguments. Track the
1731 maximum number. */
1732 size_t max_ref_arg = 0;
1734 /* Just a counter. */
1735 size_t cnt;
1737 CHAR_T *workstart = NULL;
1739 if (grouping == (const char *) -1)
1741 #ifdef COMPILE_WPRINTF
1742 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1743 _NL_NUMERIC_THOUSANDS_SEP_WC);
1744 #else
1745 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1746 #endif
1748 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1749 if (*grouping == '\0' || *grouping == CHAR_MAX)
1750 grouping = NULL;
1753 for (const UCHAR_T *f = lead_str_end; *f != L_('\0');
1754 f = specs[nspecs++].next_fmt)
1756 if (nspecs == specs_limit)
1758 if (!scratch_buffer_grow_preserve (&specsbuf))
1760 done = -1;
1761 goto all_done;
1763 specs = specsbuf.data;
1764 specs_limit = specsbuf.length / sizeof (specs[0]);
1767 /* Parse the format specifier. */
1768 #ifdef COMPILE_WPRINTF
1769 nargs += __parse_one_specwc (f, nargs, &specs[nspecs], &max_ref_arg);
1770 #else
1771 nargs += __parse_one_specmb (f, nargs, &specs[nspecs], &max_ref_arg);
1772 #endif
1775 /* Determine the number of arguments the format string consumes. */
1776 nargs = MAX (nargs, max_ref_arg);
1778 union printf_arg *args_value;
1779 int *args_size;
1780 int *args_type;
1782 /* Calculate total size needed to represent a single argument
1783 across all three argument-related arrays. */
1784 size_t bytes_per_arg
1785 = sizeof (*args_value) + sizeof (*args_size) + sizeof (*args_type);
1786 if (!scratch_buffer_set_array_size (&argsbuf, nargs, bytes_per_arg))
1788 done = -1;
1789 goto all_done;
1791 args_value = argsbuf.data;
1792 /* Set up the remaining two arrays to each point past the end of
1793 the prior array, since space for all three has been allocated
1794 now. */
1795 args_size = &args_value[nargs].pa_int;
1796 args_type = &args_size[nargs];
1797 memset (args_type, s->_flags2 & _IO_FLAGS2_FORTIFY ? '\xff' : '\0',
1798 nargs * sizeof (*args_type));
1801 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1802 still zero after this loop, format is invalid. For now we
1803 simply use 0 as the value. */
1805 /* Fill in the types of all the arguments. */
1806 for (cnt = 0; cnt < nspecs; ++cnt)
1808 /* If the width is determined by an argument this is an int. */
1809 if (specs[cnt].width_arg != -1)
1810 args_type[specs[cnt].width_arg] = PA_INT;
1812 /* If the precision is determined by an argument this is an int. */
1813 if (specs[cnt].prec_arg != -1)
1814 args_type[specs[cnt].prec_arg] = PA_INT;
1816 switch (specs[cnt].ndata_args)
1818 case 0: /* No arguments. */
1819 break;
1820 case 1: /* One argument; we already have the
1821 type and size. */
1822 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1823 args_size[specs[cnt].data_arg] = specs[cnt].size;
1824 break;
1825 default:
1826 /* We have more than one argument for this format spec.
1827 We must call the arginfo function again to determine
1828 all the types. */
1829 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1830 (&specs[cnt].info,
1831 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg],
1832 &args_size[specs[cnt].data_arg]);
1833 break;
1837 /* Now we know all the types and the order. Fill in the argument
1838 values. */
1839 for (cnt = 0; cnt < nargs; ++cnt)
1840 switch (args_type[cnt])
1842 #define T(tag, mem, type) \
1843 case tag: \
1844 args_value[cnt].mem = va_arg (*ap_savep, type); \
1845 break
1847 T (PA_WCHAR, pa_wchar, wint_t);
1848 case PA_CHAR: /* Promoted. */
1849 case PA_INT|PA_FLAG_SHORT: /* Promoted. */
1850 #if LONG_MAX == INT_MAX
1851 case PA_INT|PA_FLAG_LONG:
1852 #endif
1853 T (PA_INT, pa_int, int);
1854 #if LONG_MAX == LONG_LONG_MAX
1855 case PA_INT|PA_FLAG_LONG:
1856 #endif
1857 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1858 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1859 # error "he?"
1860 #endif
1861 case PA_FLOAT: /* Promoted. */
1862 T (PA_DOUBLE, pa_double, double);
1863 case PA_DOUBLE|PA_FLAG_LONG_DOUBLE:
1864 if (__ldbl_is_dbl)
1866 args_value[cnt].pa_double = va_arg (*ap_savep, double);
1867 args_type[cnt] &= ~PA_FLAG_LONG_DOUBLE;
1869 else
1870 args_value[cnt].pa_long_double = va_arg (*ap_savep, long double);
1871 break;
1872 case PA_STRING: /* All pointers are the same */
1873 case PA_WSTRING: /* All pointers are the same */
1874 T (PA_POINTER, pa_pointer, void *);
1875 #undef T
1876 default:
1877 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1878 args_value[cnt].pa_pointer = va_arg (*ap_savep, void *);
1879 else if (__glibc_unlikely (__printf_va_arg_table != NULL)
1880 && __printf_va_arg_table[args_type[cnt] - PA_LAST] != NULL)
1882 args_value[cnt].pa_user = alloca (args_size[cnt]);
1883 (*__printf_va_arg_table[args_type[cnt] - PA_LAST])
1884 (args_value[cnt].pa_user, ap_savep);
1886 else
1887 args_value[cnt].pa_long_double = 0.0;
1888 break;
1889 case -1:
1890 /* Error case. Not all parameters appear in N$ format
1891 strings. We have no way to determine their type. */
1892 assert (s->_flags2 & _IO_FLAGS2_FORTIFY);
1893 __libc_fatal ("*** invalid %N$ use detected ***\n");
1896 /* Now walk through all format specifiers and process them. */
1897 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1899 STEP4_TABLE;
1901 int is_negative;
1902 union
1904 unsigned long long int longlong;
1905 unsigned long int word;
1906 } number;
1907 int base;
1908 union printf_arg the_arg;
1909 CHAR_T *string; /* Pointer to argument string. */
1911 /* Fill variables from values in struct. */
1912 int alt = specs[nspecs_done].info.alt;
1913 int space = specs[nspecs_done].info.space;
1914 int left = specs[nspecs_done].info.left;
1915 int showsign = specs[nspecs_done].info.showsign;
1916 int group = specs[nspecs_done].info.group;
1917 int is_long_double = specs[nspecs_done].info.is_long_double;
1918 int is_short = specs[nspecs_done].info.is_short;
1919 int is_char = specs[nspecs_done].info.is_char;
1920 int is_long = specs[nspecs_done].info.is_long;
1921 int width = specs[nspecs_done].info.width;
1922 int prec = specs[nspecs_done].info.prec;
1923 int use_outdigits = specs[nspecs_done].info.i18n;
1924 char pad = specs[nspecs_done].info.pad;
1925 CHAR_T spec = specs[nspecs_done].info.spec;
1927 workstart = NULL;
1928 CHAR_T *workend = work_buffer + WORK_BUFFER_SIZE;
1930 /* Fill in last information. */
1931 if (specs[nspecs_done].width_arg != -1)
1933 /* Extract the field width from an argument. */
1934 specs[nspecs_done].info.width =
1935 args_value[specs[nspecs_done].width_arg].pa_int;
1937 if (specs[nspecs_done].info.width < 0)
1938 /* If the width value is negative left justification is
1939 selected and the value is taken as being positive. */
1941 specs[nspecs_done].info.width *= -1;
1942 left = specs[nspecs_done].info.left = 1;
1944 width = specs[nspecs_done].info.width;
1947 if (specs[nspecs_done].prec_arg != -1)
1949 /* Extract the precision from an argument. */
1950 specs[nspecs_done].info.prec =
1951 args_value[specs[nspecs_done].prec_arg].pa_int;
1953 if (specs[nspecs_done].info.prec < 0)
1954 /* If the precision is negative the precision is
1955 omitted. */
1956 specs[nspecs_done].info.prec = -1;
1958 prec = specs[nspecs_done].info.prec;
1961 /* Maybe the buffer is too small. */
1962 if (MAX (prec, width) + EXTSIZ > WORK_BUFFER_SIZE)
1964 if (__libc_use_alloca ((MAX (prec, width) + EXTSIZ)
1965 * sizeof (CHAR_T)))
1966 workend = ((CHAR_T *) alloca ((MAX (prec, width) + EXTSIZ)
1967 * sizeof (CHAR_T))
1968 + (MAX (prec, width) + EXTSIZ));
1969 else
1971 workstart = (CHAR_T *) malloc ((MAX (prec, width) + EXTSIZ)
1972 * sizeof (CHAR_T));
1973 if (workstart == NULL)
1975 done = -1;
1976 goto all_done;
1978 workend = workstart + (MAX (prec, width) + EXTSIZ);
1982 /* Process format specifiers. */
1983 while (1)
1985 extern printf_function **__printf_function_table;
1986 int function_done;
1988 if (spec <= UCHAR_MAX
1989 && __printf_function_table != NULL
1990 && __printf_function_table[(size_t) spec] != NULL)
1992 const void **ptr = alloca (specs[nspecs_done].ndata_args
1993 * sizeof (const void *));
1995 /* Fill in an array of pointers to the argument values. */
1996 for (unsigned int i = 0; i < specs[nspecs_done].ndata_args;
1997 ++i)
1998 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
2000 /* Call the function. */
2001 function_done = __printf_function_table[(size_t) spec]
2002 (s, &specs[nspecs_done].info, ptr);
2004 if (function_done != -2)
2006 /* If an error occurred we don't have information
2007 about # of chars. */
2008 if (function_done < 0)
2010 /* Function has set errno. */
2011 done = -1;
2012 goto all_done;
2015 done_add (function_done);
2016 break;
2020 JUMP (spec, step4_jumps);
2022 process_arg ((&specs[nspecs_done]));
2023 process_string_arg ((&specs[nspecs_done]));
2025 LABEL (form_unknown):
2027 unsigned int i;
2028 const void **ptr;
2030 ptr = alloca (specs[nspecs_done].ndata_args
2031 * sizeof (const void *));
2033 /* Fill in an array of pointers to the argument values. */
2034 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
2035 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
2037 /* Call the function. */
2038 function_done = printf_unknown (s, &specs[nspecs_done].info,
2039 ptr);
2041 /* If an error occurred we don't have information about #
2042 of chars. */
2043 if (function_done < 0)
2045 /* Function has set errno. */
2046 done = -1;
2047 goto all_done;
2050 done_add (function_done);
2052 break;
2055 if (__glibc_unlikely (workstart != NULL))
2056 free (workstart);
2057 workstart = NULL;
2059 /* Write the following constant string. */
2060 outstring (specs[nspecs_done].end_of_fmt,
2061 specs[nspecs_done].next_fmt
2062 - specs[nspecs_done].end_of_fmt);
2064 all_done:
2065 if (__glibc_unlikely (workstart != NULL))
2066 free (workstart);
2067 scratch_buffer_free (&argsbuf);
2068 scratch_buffer_free (&specsbuf);
2069 return done;
2072 /* Handle an unknown format specifier. This prints out a canonicalized
2073 representation of the format spec itself. */
2074 static int
2075 printf_unknown (FILE *s, const struct printf_info *info,
2076 const void *const *args)
2079 int done = 0;
2080 CHAR_T work_buffer[MAX (sizeof (info->width), sizeof (info->prec)) * 3];
2081 CHAR_T *const workend
2082 = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
2083 CHAR_T *w;
2085 outchar (L_('%'));
2087 if (info->alt)
2088 outchar (L_('#'));
2089 if (info->group)
2090 outchar (L_('\''));
2091 if (info->showsign)
2092 outchar (L_('+'));
2093 else if (info->space)
2094 outchar (L_(' '));
2095 if (info->left)
2096 outchar (L_('-'));
2097 if (info->pad == L_('0'))
2098 outchar (L_('0'));
2099 if (info->i18n)
2100 outchar (L_('I'));
2102 if (info->width != 0)
2104 w = _itoa_word (info->width, workend, 10, 0);
2105 while (w < workend)
2106 outchar (*w++);
2109 if (info->prec != -1)
2111 outchar (L_('.'));
2112 w = _itoa_word (info->prec, workend, 10, 0);
2113 while (w < workend)
2114 outchar (*w++);
2117 if (info->spec != L_('\0'))
2118 outchar (info->spec);
2120 all_done:
2121 return done;
2124 /* Group the digits from W to REAR_PTR according to the grouping rules
2125 of the current locale. The interpretation of GROUPING is as in
2126 `struct lconv' from <locale.h>. The grouped number extends from
2127 the returned pointer until REAR_PTR. FRONT_PTR to W is used as a
2128 scratch area. */
2129 static CHAR_T *
2130 group_number (CHAR_T *front_ptr, CHAR_T *w, CHAR_T *rear_ptr,
2131 const char *grouping, THOUSANDS_SEP_T thousands_sep)
2133 /* Length of the current group. */
2134 int len;
2135 #ifndef COMPILE_WPRINTF
2136 /* Length of the separator (in wide mode, the separator is always a
2137 single wide character). */
2138 int tlen = strlen (thousands_sep);
2139 #endif
2141 /* We treat all negative values like CHAR_MAX. */
2143 if (*grouping == CHAR_MAX || *grouping <= 0)
2144 /* No grouping should be done. */
2145 return w;
2147 len = *grouping++;
2149 /* Copy existing string so that nothing gets overwritten. */
2150 memmove (front_ptr, w, (rear_ptr - w) * sizeof (CHAR_T));
2151 CHAR_T *s = front_ptr + (rear_ptr - w);
2153 w = rear_ptr;
2155 /* Process all characters in the string. */
2156 while (s > front_ptr)
2158 *--w = *--s;
2160 if (--len == 0 && s > front_ptr)
2162 /* A new group begins. */
2163 #ifdef COMPILE_WPRINTF
2164 if (w != s)
2165 *--w = thousands_sep;
2166 else
2167 /* Not enough room for the separator. */
2168 goto copy_rest;
2169 #else
2170 int cnt = tlen;
2171 if (tlen < w - s)
2173 *--w = thousands_sep[--cnt];
2174 while (cnt > 0);
2175 else
2176 /* Not enough room for the separator. */
2177 goto copy_rest;
2178 #endif
2180 if (*grouping == CHAR_MAX
2181 #if CHAR_MIN < 0
2182 || *grouping < 0
2183 #endif
2186 copy_rest:
2187 /* No further grouping to be done. Copy the rest of the
2188 number. */
2189 memmove (w, s, (front_ptr -s) * sizeof (CHAR_T));
2190 break;
2192 else if (*grouping != '\0')
2193 len = *grouping++;
2194 else
2195 /* The previous grouping repeats ad infinitum. */
2196 len = grouping[-1];
2199 return w;
2202 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2203 struct helper_file
2205 struct _IO_FILE_plus _f;
2206 #ifdef COMPILE_WPRINTF
2207 struct _IO_wide_data _wide_data;
2208 #endif
2209 _IO_FILE *_put_stream;
2210 #ifdef _IO_MTSAFE_IO
2211 _IO_lock_t lock;
2212 #endif
2215 static int
2216 _IO_helper_overflow (_IO_FILE *s, int c)
2218 _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
2219 #ifdef COMPILE_WPRINTF
2220 int used = s->_wide_data->_IO_write_ptr - s->_wide_data->_IO_write_base;
2221 if (used)
2223 _IO_size_t written = _IO_sputn (target, s->_wide_data->_IO_write_base,
2224 used);
2225 if (written == 0 || written == WEOF)
2226 return WEOF;
2227 __wmemmove (s->_wide_data->_IO_write_base,
2228 s->_wide_data->_IO_write_base + written,
2229 used - written);
2230 s->_wide_data->_IO_write_ptr -= written;
2232 #else
2233 int used = s->_IO_write_ptr - s->_IO_write_base;
2234 if (used)
2236 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
2237 if (written == 0 || written == EOF)
2238 return EOF;
2239 memmove (s->_IO_write_base, s->_IO_write_base + written,
2240 used - written);
2241 s->_IO_write_ptr -= written;
2243 #endif
2244 return PUTC (c, s);
2247 #ifdef COMPILE_WPRINTF
2248 static const struct _IO_jump_t _IO_helper_jumps libio_vtable =
2250 JUMP_INIT_DUMMY,
2251 JUMP_INIT (finish, _IO_wdefault_finish),
2252 JUMP_INIT (overflow, _IO_helper_overflow),
2253 JUMP_INIT (underflow, _IO_default_underflow),
2254 JUMP_INIT (uflow, _IO_default_uflow),
2255 JUMP_INIT (pbackfail, (_IO_pbackfail_t) _IO_wdefault_pbackfail),
2256 JUMP_INIT (xsputn, _IO_wdefault_xsputn),
2257 JUMP_INIT (xsgetn, _IO_wdefault_xsgetn),
2258 JUMP_INIT (seekoff, _IO_default_seekoff),
2259 JUMP_INIT (seekpos, _IO_default_seekpos),
2260 JUMP_INIT (setbuf, _IO_default_setbuf),
2261 JUMP_INIT (sync, _IO_default_sync),
2262 JUMP_INIT (doallocate, _IO_wdefault_doallocate),
2263 JUMP_INIT (read, _IO_default_read),
2264 JUMP_INIT (write, _IO_default_write),
2265 JUMP_INIT (seek, _IO_default_seek),
2266 JUMP_INIT (close, _IO_default_close),
2267 JUMP_INIT (stat, _IO_default_stat)
2269 #else
2270 static const struct _IO_jump_t _IO_helper_jumps libio_vtable =
2272 JUMP_INIT_DUMMY,
2273 JUMP_INIT (finish, _IO_default_finish),
2274 JUMP_INIT (overflow, _IO_helper_overflow),
2275 JUMP_INIT (underflow, _IO_default_underflow),
2276 JUMP_INIT (uflow, _IO_default_uflow),
2277 JUMP_INIT (pbackfail, _IO_default_pbackfail),
2278 JUMP_INIT (xsputn, _IO_default_xsputn),
2279 JUMP_INIT (xsgetn, _IO_default_xsgetn),
2280 JUMP_INIT (seekoff, _IO_default_seekoff),
2281 JUMP_INIT (seekpos, _IO_default_seekpos),
2282 JUMP_INIT (setbuf, _IO_default_setbuf),
2283 JUMP_INIT (sync, _IO_default_sync),
2284 JUMP_INIT (doallocate, _IO_default_doallocate),
2285 JUMP_INIT (read, _IO_default_read),
2286 JUMP_INIT (write, _IO_default_write),
2287 JUMP_INIT (seek, _IO_default_seek),
2288 JUMP_INIT (close, _IO_default_close),
2289 JUMP_INIT (stat, _IO_default_stat)
2291 #endif
2293 static int
2294 internal_function
2295 buffered_vfprintf (_IO_FILE *s, const CHAR_T *format,
2296 _IO_va_list args)
2298 CHAR_T buf[_IO_BUFSIZ];
2299 struct helper_file helper;
2300 _IO_FILE *hp = (_IO_FILE *) &helper._f;
2301 int result, to_flush;
2303 /* Orient the stream. */
2304 #ifdef ORIENT
2305 ORIENT;
2306 #endif
2308 /* Initialize helper. */
2309 helper._put_stream = s;
2310 #ifdef COMPILE_WPRINTF
2311 hp->_wide_data = &helper._wide_data;
2312 _IO_wsetp (hp, buf, buf + sizeof buf / sizeof (CHAR_T));
2313 hp->_mode = 1;
2314 #else
2315 _IO_setp (hp, buf, buf + sizeof buf);
2316 hp->_mode = -1;
2317 #endif
2318 hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS|_IO_USER_LOCK;
2319 #if _IO_JUMPS_OFFSET
2320 hp->_vtable_offset = 0;
2321 #endif
2322 #ifdef _IO_MTSAFE_IO
2323 hp->_lock = NULL;
2324 #endif
2325 hp->_flags2 = s->_flags2;
2326 _IO_JUMPS (&helper._f) = (struct _IO_jump_t *) &_IO_helper_jumps;
2328 /* Now print to helper instead. */
2329 #ifndef COMPILE_WPRINTF
2330 result = _IO_vfprintf (hp, format, args);
2331 #else
2332 result = vfprintf (hp, format, args);
2333 #endif
2335 /* Lock stream. */
2336 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, s);
2337 _IO_flockfile (s);
2339 /* Now flush anything from the helper to the S. */
2340 #ifdef COMPILE_WPRINTF
2341 if ((to_flush = (hp->_wide_data->_IO_write_ptr
2342 - hp->_wide_data->_IO_write_base)) > 0)
2344 if ((int) _IO_sputn (s, hp->_wide_data->_IO_write_base, to_flush)
2345 != to_flush)
2346 result = -1;
2348 #else
2349 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
2351 if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
2352 result = -1;
2354 #endif
2356 /* Unlock the stream. */
2357 _IO_funlockfile (s);
2358 __libc_cleanup_region_end (0);
2360 return result;
2363 #undef vfprintf
2364 #ifdef COMPILE_WPRINTF
2365 strong_alias (_IO_vfwprintf, __vfwprintf);
2366 ldbl_weak_alias (_IO_vfwprintf, vfwprintf);
2367 #else
2368 ldbl_strong_alias (_IO_vfprintf_internal, vfprintf);
2369 ldbl_hidden_def (_IO_vfprintf_internal, vfprintf)
2370 ldbl_strong_alias (_IO_vfprintf_internal, _IO_vfprintf);
2371 ldbl_hidden_def (_IO_vfprintf_internal, _IO_vfprintf)
2372 #endif