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