.
[glibc.git] / stdio-common / vfprintf.c
blobbf5227873b575c7a8ba4fe9401cbf967596642e4
1 /* Copyright (C) 1991-2002, 2003, 2004, 2005, 2006, 2007
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <ctype.h>
21 #include <limits.h>
22 #include <printf.h>
23 #include <stdarg.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <wchar.h>
29 #include <bits/libc-lock.h>
30 #include <sys/param.h>
31 #include "_itoa.h"
32 #include <locale/localeinfo.h>
33 #include <stdio.h>
35 /* This code is shared between the standard stdio implementation found
36 in GNU C library and the libio implementation originally found in
37 GNU libg++.
39 Beside this it is also shared between the normal and wide character
40 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
43 #include <libioP.h>
44 #define FILE _IO_FILE
45 #undef va_list
46 #define va_list _IO_va_list
47 #undef BUFSIZ
48 #define BUFSIZ _IO_BUFSIZ
49 #define ARGCHECK(S, Format) \
50 do \
51 { \
52 /* Check file argument for consistence. */ \
53 CHECK_FILE (S, -1); \
54 if (S->_flags & _IO_NO_WRITES) \
55 { \
56 __set_errno (EBADF); \
57 return -1; \
58 } \
59 if (Format == NULL) \
60 { \
61 MAYBE_SET_EINVAL; \
62 return -1; \
63 } \
64 } while (0)
65 #define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
67 #ifndef COMPILE_WPRINTF
68 # define vfprintf _IO_vfprintf_internal
69 # define CHAR_T char
70 # define UCHAR_T unsigned char
71 # define INT_T int
72 # define L_(Str) Str
73 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
74 # define STR_LEN(Str) strlen (Str)
76 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
77 # define PAD(Padchar) \
78 if (width > 0) \
79 done += INTUSE(_IO_padn) (s, (Padchar), width)
80 # define PUTC(C, F) _IO_putc_unlocked (C, F)
81 # define ORIENT if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
82 return -1
83 #else
84 # define vfprintf _IO_vfwprintf
85 # define CHAR_T wchar_t
86 /* This is a hack!!! There should be a type uwchar_t. */
87 # define UCHAR_T unsigned int /* uwchar_t */
88 # define INT_T wint_t
89 # define L_(Str) L##Str
90 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
91 # define STR_LEN(Str) __wcslen (Str)
93 # include "_itowa.h"
95 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
96 # define PAD(Padchar) \
97 if (width > 0) \
98 done += _IO_wpadn (s, (Padchar), width)
99 # define PUTC(C, F) _IO_putwc_unlocked (C, F)
100 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
102 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
103 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
104 # undef EOF
105 # define EOF WEOF
106 #endif
108 #include "_i18n_number.h"
110 /* Include the shared code for parsing the format string. */
111 #include "printf-parse.h"
114 #define outchar(Ch) \
115 do \
117 register const INT_T outc = (Ch); \
118 if (PUTC (outc, s) == EOF) \
120 done = -1; \
121 goto all_done; \
123 else \
124 ++done; \
126 while (0)
128 #define outstring(String, Len) \
129 do \
131 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
133 done = -1; \
134 goto all_done; \
136 done += (Len); \
138 while (0)
140 /* For handling long_double and longlong we use the same flag. If
141 `long' and `long long' are effectively the same type define it to
142 zero. */
143 #if LONG_MAX == LONG_LONG_MAX
144 # define is_longlong 0
145 #else
146 # define is_longlong is_long_double
147 #endif
149 /* If `long' and `int' is effectively the same type we don't have to
150 handle `long separately. */
151 #if INT_MAX == LONG_MAX
152 # define is_long_num 0
153 #else
154 # define is_long_num is_long
155 #endif
158 /* Global variables. */
159 static const CHAR_T null[] = L_("(null)");
162 /* Helper function to provide temporary buffering for unbuffered streams. */
163 static int buffered_vfprintf (FILE *stream, const CHAR_T *fmt, va_list)
164 __THROW __attribute__ ((noinline)) internal_function;
166 /* Handle unknown format specifier. */
167 static int printf_unknown (FILE *, const struct printf_info *,
168 const void *const *) __THROW;
170 /* Group digits of number string. */
171 #ifdef COMPILE_WPRINTF
172 static CHAR_T *group_number (CHAR_T *, CHAR_T *, const char *, wchar_t)
173 __THROW internal_function;
174 #else
175 static CHAR_T *group_number (CHAR_T *, CHAR_T *, const char *, const char *)
176 __THROW internal_function;
177 #endif
180 /* The function itself. */
182 vfprintf (FILE *s, const CHAR_T *format, va_list ap)
184 /* The character used as thousands separator. */
185 #ifdef COMPILE_WPRINTF
186 wchar_t thousands_sep = L'\0';
187 #else
188 const char *thousands_sep = NULL;
189 #endif
191 /* The string describing the size of groups of digits. */
192 const char *grouping;
194 /* Place to accumulate the result. */
195 int done;
197 /* Current character in format string. */
198 const UCHAR_T *f;
200 /* End of leading constant string. */
201 const UCHAR_T *lead_str_end;
203 /* Points to next format specifier. */
204 const UCHAR_T *end_of_spec;
206 /* Buffer intermediate results. */
207 CHAR_T work_buffer[1000];
208 CHAR_T *workstart = NULL;
209 CHAR_T *workend;
211 /* State for restartable multibyte character handling functions. */
212 #ifndef COMPILE_WPRINTF
213 mbstate_t mbstate;
214 #endif
216 /* We have to save the original argument pointer. */
217 va_list ap_save;
219 /* Count number of specifiers we already processed. */
220 int nspecs_done;
222 /* For the %m format we may need the current `errno' value. */
223 int save_errno = errno;
225 /* 1 if format is in read-only memory, -1 if it is in writable memory,
226 0 if unknown. */
227 int readonly_format = 0;
229 /* This table maps a character into a number representing a
230 class. In each step there is a destination label for each
231 class. */
232 static const int jump_table[] =
234 /* ' ' */ 1, 0, 0, /* '#' */ 4,
235 0, /* '%' */ 14, 0, /* '\''*/ 6,
236 0, 0, /* '*' */ 7, /* '+' */ 2,
237 0, /* '-' */ 3, /* '.' */ 9, 0,
238 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
239 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
240 /* '8' */ 8, /* '9' */ 8, 0, 0,
241 0, 0, 0, 0,
242 0, /* 'A' */ 26, 0, /* 'C' */ 25,
243 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
244 0, /* 'I' */ 29, 0, 0,
245 /* 'L' */ 12, 0, 0, 0,
246 0, 0, 0, /* 'S' */ 21,
247 0, 0, 0, 0,
248 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
249 0, 0, 0, 0,
250 0, /* 'a' */ 26, 0, /* 'c' */ 20,
251 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
252 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
253 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
254 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
255 /* 't' */ 27, /* 'u' */ 16, 0, 0,
256 /* 'x' */ 18, 0, /* 'z' */ 13
259 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
260 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
261 #ifdef SHARED
262 /* 'int' is enough and it saves some space on 64 bit systems. */
263 # define JUMP_TABLE_TYPE const int
264 # define JUMP(ChExpr, table) \
265 do \
267 int offset; \
268 void *__unbounded ptr; \
269 spec = (ChExpr); \
270 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
271 : table[CHAR_CLASS (spec)]; \
272 ptr = &&do_form_unknown + offset; \
273 goto *ptr; \
275 while (0)
276 #else
277 # define JUMP_TABLE_TYPE const void *const
278 # define JUMP(ChExpr, table) \
279 do \
281 const void *__unbounded ptr; \
282 spec = (ChExpr); \
283 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
284 : table[CHAR_CLASS (spec)]; \
285 goto *ptr; \
287 while (0)
288 #endif
290 #define STEP0_3_TABLE \
291 /* Step 0: at the beginning. */ \
292 static JUMP_TABLE_TYPE step0_jumps[30] = \
294 REF (form_unknown), \
295 REF (flag_space), /* for ' ' */ \
296 REF (flag_plus), /* for '+' */ \
297 REF (flag_minus), /* for '-' */ \
298 REF (flag_hash), /* for '<hash>' */ \
299 REF (flag_zero), /* for '0' */ \
300 REF (flag_quote), /* for '\'' */ \
301 REF (width_asterics), /* for '*' */ \
302 REF (width), /* for '1'...'9' */ \
303 REF (precision), /* for '.' */ \
304 REF (mod_half), /* for 'h' */ \
305 REF (mod_long), /* for 'l' */ \
306 REF (mod_longlong), /* for 'L', 'q' */ \
307 REF (mod_size_t), /* for 'z', 'Z' */ \
308 REF (form_percent), /* for '%' */ \
309 REF (form_integer), /* for 'd', 'i' */ \
310 REF (form_unsigned), /* for 'u' */ \
311 REF (form_octal), /* for 'o' */ \
312 REF (form_hexa), /* for 'X', 'x' */ \
313 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
314 REF (form_character), /* for 'c' */ \
315 REF (form_string), /* for 's', 'S' */ \
316 REF (form_pointer), /* for 'p' */ \
317 REF (form_number), /* for 'n' */ \
318 REF (form_strerror), /* for 'm' */ \
319 REF (form_wcharacter), /* for 'C' */ \
320 REF (form_floathex), /* for 'A', 'a' */ \
321 REF (mod_ptrdiff_t), /* for 't' */ \
322 REF (mod_intmax_t), /* for 'j' */ \
323 REF (flag_i18n), /* for 'I' */ \
324 }; \
325 /* Step 1: after processing width. */ \
326 static JUMP_TABLE_TYPE step1_jumps[30] = \
328 REF (form_unknown), \
329 REF (form_unknown), /* for ' ' */ \
330 REF (form_unknown), /* for '+' */ \
331 REF (form_unknown), /* for '-' */ \
332 REF (form_unknown), /* for '<hash>' */ \
333 REF (form_unknown), /* for '0' */ \
334 REF (form_unknown), /* for '\'' */ \
335 REF (form_unknown), /* for '*' */ \
336 REF (form_unknown), /* for '1'...'9' */ \
337 REF (precision), /* for '.' */ \
338 REF (mod_half), /* for 'h' */ \
339 REF (mod_long), /* for 'l' */ \
340 REF (mod_longlong), /* for 'L', 'q' */ \
341 REF (mod_size_t), /* for 'z', 'Z' */ \
342 REF (form_percent), /* for '%' */ \
343 REF (form_integer), /* for 'd', 'i' */ \
344 REF (form_unsigned), /* for 'u' */ \
345 REF (form_octal), /* for 'o' */ \
346 REF (form_hexa), /* for 'X', 'x' */ \
347 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
348 REF (form_character), /* for 'c' */ \
349 REF (form_string), /* for 's', 'S' */ \
350 REF (form_pointer), /* for 'p' */ \
351 REF (form_number), /* for 'n' */ \
352 REF (form_strerror), /* for 'm' */ \
353 REF (form_wcharacter), /* for 'C' */ \
354 REF (form_floathex), /* for 'A', 'a' */ \
355 REF (mod_ptrdiff_t), /* for 't' */ \
356 REF (mod_intmax_t), /* for 'j' */ \
357 REF (form_unknown) /* for 'I' */ \
358 }; \
359 /* Step 2: after processing precision. */ \
360 static JUMP_TABLE_TYPE step2_jumps[30] = \
362 REF (form_unknown), \
363 REF (form_unknown), /* for ' ' */ \
364 REF (form_unknown), /* for '+' */ \
365 REF (form_unknown), /* for '-' */ \
366 REF (form_unknown), /* for '<hash>' */ \
367 REF (form_unknown), /* for '0' */ \
368 REF (form_unknown), /* for '\'' */ \
369 REF (form_unknown), /* for '*' */ \
370 REF (form_unknown), /* for '1'...'9' */ \
371 REF (form_unknown), /* for '.' */ \
372 REF (mod_half), /* for 'h' */ \
373 REF (mod_long), /* for 'l' */ \
374 REF (mod_longlong), /* for 'L', 'q' */ \
375 REF (mod_size_t), /* for 'z', 'Z' */ \
376 REF (form_percent), /* for '%' */ \
377 REF (form_integer), /* for 'd', 'i' */ \
378 REF (form_unsigned), /* for 'u' */ \
379 REF (form_octal), /* for 'o' */ \
380 REF (form_hexa), /* for 'X', 'x' */ \
381 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
382 REF (form_character), /* for 'c' */ \
383 REF (form_string), /* for 's', 'S' */ \
384 REF (form_pointer), /* for 'p' */ \
385 REF (form_number), /* for 'n' */ \
386 REF (form_strerror), /* for 'm' */ \
387 REF (form_wcharacter), /* for 'C' */ \
388 REF (form_floathex), /* for 'A', 'a' */ \
389 REF (mod_ptrdiff_t), /* for 't' */ \
390 REF (mod_intmax_t), /* for 'j' */ \
391 REF (form_unknown) /* for 'I' */ \
392 }; \
393 /* Step 3a: after processing first 'h' modifier. */ \
394 static JUMP_TABLE_TYPE step3a_jumps[30] = \
396 REF (form_unknown), \
397 REF (form_unknown), /* for ' ' */ \
398 REF (form_unknown), /* for '+' */ \
399 REF (form_unknown), /* for '-' */ \
400 REF (form_unknown), /* for '<hash>' */ \
401 REF (form_unknown), /* for '0' */ \
402 REF (form_unknown), /* for '\'' */ \
403 REF (form_unknown), /* for '*' */ \
404 REF (form_unknown), /* for '1'...'9' */ \
405 REF (form_unknown), /* for '.' */ \
406 REF (mod_halfhalf), /* for 'h' */ \
407 REF (form_unknown), /* for 'l' */ \
408 REF (form_unknown), /* for 'L', 'q' */ \
409 REF (form_unknown), /* for 'z', 'Z' */ \
410 REF (form_percent), /* for '%' */ \
411 REF (form_integer), /* for 'd', 'i' */ \
412 REF (form_unsigned), /* for 'u' */ \
413 REF (form_octal), /* for 'o' */ \
414 REF (form_hexa), /* for 'X', 'x' */ \
415 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
416 REF (form_unknown), /* for 'c' */ \
417 REF (form_unknown), /* for 's', 'S' */ \
418 REF (form_unknown), /* for 'p' */ \
419 REF (form_number), /* for 'n' */ \
420 REF (form_unknown), /* for 'm' */ \
421 REF (form_unknown), /* for 'C' */ \
422 REF (form_unknown), /* for 'A', 'a' */ \
423 REF (form_unknown), /* for 't' */ \
424 REF (form_unknown), /* for 'j' */ \
425 REF (form_unknown) /* for 'I' */ \
426 }; \
427 /* Step 3b: after processing first 'l' modifier. */ \
428 static JUMP_TABLE_TYPE step3b_jumps[30] = \
430 REF (form_unknown), \
431 REF (form_unknown), /* for ' ' */ \
432 REF (form_unknown), /* for '+' */ \
433 REF (form_unknown), /* for '-' */ \
434 REF (form_unknown), /* for '<hash>' */ \
435 REF (form_unknown), /* for '0' */ \
436 REF (form_unknown), /* for '\'' */ \
437 REF (form_unknown), /* for '*' */ \
438 REF (form_unknown), /* for '1'...'9' */ \
439 REF (form_unknown), /* for '.' */ \
440 REF (form_unknown), /* for 'h' */ \
441 REF (mod_longlong), /* for 'l' */ \
442 REF (form_unknown), /* for 'L', 'q' */ \
443 REF (form_unknown), /* for 'z', 'Z' */ \
444 REF (form_percent), /* for '%' */ \
445 REF (form_integer), /* for 'd', 'i' */ \
446 REF (form_unsigned), /* for 'u' */ \
447 REF (form_octal), /* for 'o' */ \
448 REF (form_hexa), /* for 'X', 'x' */ \
449 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
450 REF (form_character), /* for 'c' */ \
451 REF (form_string), /* for 's', 'S' */ \
452 REF (form_pointer), /* for 'p' */ \
453 REF (form_number), /* for 'n' */ \
454 REF (form_strerror), /* for 'm' */ \
455 REF (form_wcharacter), /* for 'C' */ \
456 REF (form_floathex), /* for 'A', 'a' */ \
457 REF (form_unknown), /* for 't' */ \
458 REF (form_unknown), /* for 'j' */ \
459 REF (form_unknown) /* for 'I' */ \
462 #define STEP4_TABLE \
463 /* Step 4: processing format specifier. */ \
464 static JUMP_TABLE_TYPE step4_jumps[30] = \
466 REF (form_unknown), \
467 REF (form_unknown), /* for ' ' */ \
468 REF (form_unknown), /* for '+' */ \
469 REF (form_unknown), /* for '-' */ \
470 REF (form_unknown), /* for '<hash>' */ \
471 REF (form_unknown), /* for '0' */ \
472 REF (form_unknown), /* for '\'' */ \
473 REF (form_unknown), /* for '*' */ \
474 REF (form_unknown), /* for '1'...'9' */ \
475 REF (form_unknown), /* for '.' */ \
476 REF (form_unknown), /* for 'h' */ \
477 REF (form_unknown), /* for 'l' */ \
478 REF (form_unknown), /* for 'L', 'q' */ \
479 REF (form_unknown), /* for 'z', 'Z' */ \
480 REF (form_percent), /* for '%' */ \
481 REF (form_integer), /* for 'd', 'i' */ \
482 REF (form_unsigned), /* for 'u' */ \
483 REF (form_octal), /* for 'o' */ \
484 REF (form_hexa), /* for 'X', 'x' */ \
485 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
486 REF (form_character), /* for 'c' */ \
487 REF (form_string), /* for 's', 'S' */ \
488 REF (form_pointer), /* for 'p' */ \
489 REF (form_number), /* for 'n' */ \
490 REF (form_strerror), /* for 'm' */ \
491 REF (form_wcharacter), /* for 'C' */ \
492 REF (form_floathex), /* for 'A', 'a' */ \
493 REF (form_unknown), /* for 't' */ \
494 REF (form_unknown), /* for 'j' */ \
495 REF (form_unknown) /* for 'I' */ \
499 #define process_arg(fspec) \
500 /* Start real work. We know about all flags and modifiers and \
501 now process the wanted format specifier. */ \
502 LABEL (form_percent): \
503 /* Write a literal "%". */ \
504 outchar (L_('%')); \
505 break; \
507 LABEL (form_integer): \
508 /* Signed decimal integer. */ \
509 base = 10; \
511 if (is_longlong) \
513 long long int signed_number; \
515 if (fspec == NULL) \
516 signed_number = va_arg (ap, long long int); \
517 else \
518 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
520 is_negative = signed_number < 0; \
521 number.longlong = is_negative ? (- signed_number) : signed_number; \
523 goto LABEL (longlong_number); \
525 else \
527 long int signed_number; \
529 if (fspec == NULL) \
531 if (is_long_num) \
532 signed_number = va_arg (ap, long int); \
533 else if (is_char) \
534 signed_number = (signed char) va_arg (ap, unsigned int); \
535 else if (!is_short) \
536 signed_number = va_arg (ap, int); \
537 else \
538 signed_number = (short int) va_arg (ap, unsigned int); \
540 else \
541 if (is_long_num) \
542 signed_number = args_value[fspec->data_arg].pa_long_int; \
543 else if (is_char) \
544 signed_number = (signed char) \
545 args_value[fspec->data_arg].pa_u_int; \
546 else if (!is_short) \
547 signed_number = args_value[fspec->data_arg].pa_int; \
548 else \
549 signed_number = (short int) \
550 args_value[fspec->data_arg].pa_u_int; \
552 is_negative = signed_number < 0; \
553 number.word = is_negative ? (- signed_number) : signed_number; \
555 goto LABEL (number); \
557 /* NOTREACHED */ \
559 LABEL (form_unsigned): \
560 /* Unsigned decimal integer. */ \
561 base = 10; \
562 goto LABEL (unsigned_number); \
563 /* NOTREACHED */ \
565 LABEL (form_octal): \
566 /* Unsigned octal integer. */ \
567 base = 8; \
568 goto LABEL (unsigned_number); \
569 /* NOTREACHED */ \
571 LABEL (form_hexa): \
572 /* Unsigned hexadecimal integer. */ \
573 base = 16; \
575 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
577 /* ISO specifies the `+' and ` ' flags only for signed \
578 conversions. */ \
579 is_negative = 0; \
580 showsign = 0; \
581 space = 0; \
583 if (is_longlong) \
585 if (fspec == NULL) \
586 number.longlong = va_arg (ap, unsigned long long int); \
587 else \
588 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
590 LABEL (longlong_number): \
591 if (prec < 0) \
592 /* Supply a default precision if none was given. */ \
593 prec = 1; \
594 else \
595 /* We have to take care for the '0' flag. If a precision \
596 is given it must be ignored. */ \
597 pad = L_(' '); \
599 /* If the precision is 0 and the number is 0 nothing has to \
600 be written for the number, except for the 'o' format in \
601 alternate form. */ \
602 if (prec == 0 && number.longlong == 0) \
604 string = workend; \
605 if (base == 8 && alt) \
606 *--string = L_('0'); \
608 else \
610 /* Put the number in WORK. */ \
611 string = _itoa (number.longlong, workend, base, \
612 spec == L_('X')); \
613 if (group && grouping) \
614 string = group_number (string, workend, grouping, \
615 thousands_sep); \
617 if (use_outdigits && base == 10) \
618 string = _i18n_number_rewrite (string, workend); \
620 /* Simplify further test for num != 0. */ \
621 number.word = number.longlong != 0; \
623 else \
625 if (fspec == NULL) \
627 if (is_long_num) \
628 number.word = va_arg (ap, unsigned long int); \
629 else if (is_char) \
630 number.word = (unsigned char) va_arg (ap, unsigned int); \
631 else if (!is_short) \
632 number.word = va_arg (ap, unsigned int); \
633 else \
634 number.word = (unsigned short int) va_arg (ap, unsigned int); \
636 else \
637 if (is_long_num) \
638 number.word = args_value[fspec->data_arg].pa_u_long_int; \
639 else if (is_char) \
640 number.word = (unsigned char) \
641 args_value[fspec->data_arg].pa_u_int; \
642 else if (!is_short) \
643 number.word = args_value[fspec->data_arg].pa_u_int; \
644 else \
645 number.word = (unsigned short int) \
646 args_value[fspec->data_arg].pa_u_int; \
648 LABEL (number): \
649 if (prec < 0) \
650 /* Supply a default precision if none was given. */ \
651 prec = 1; \
652 else \
653 /* We have to take care for the '0' flag. If a precision \
654 is given it must be ignored. */ \
655 pad = L_(' '); \
657 /* If the precision is 0 and the number is 0 nothing has to \
658 be written for the number, except for the 'o' format in \
659 alternate form. */ \
660 if (prec == 0 && number.word == 0) \
662 string = workend; \
663 if (base == 8 && alt) \
664 *--string = L_('0'); \
666 else \
668 /* Put the number in WORK. */ \
669 string = _itoa_word (number.word, workend, base, \
670 spec == L_('X')); \
671 if (group && grouping) \
672 string = group_number (string, workend, grouping, \
673 thousands_sep); \
675 if (use_outdigits && base == 10) \
676 string = _i18n_number_rewrite (string, workend); \
680 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
681 /* Add octal marker. */ \
682 *--string = L_('0'); \
684 prec = MAX (0, prec - (workend - string)); \
686 if (!left) \
688 width -= workend - string + prec; \
690 if (number.word != 0 && alt && base == 16) \
691 /* Account for 0X hex marker. */ \
692 width -= 2; \
694 if (is_negative || showsign || space) \
695 --width; \
697 if (pad == L_(' ')) \
699 PAD (L_(' ')); \
700 width = 0; \
703 if (is_negative) \
704 outchar (L_('-')); \
705 else if (showsign) \
706 outchar (L_('+')); \
707 else if (space) \
708 outchar (L_(' ')); \
710 if (number.word != 0 && alt && base == 16) \
712 outchar (L_('0')); \
713 outchar (spec); \
716 width += prec; \
717 PAD (L_('0')); \
719 outstring (string, workend - string); \
721 break; \
723 else \
725 if (is_negative) \
727 outchar (L_('-')); \
728 --width; \
730 else if (showsign) \
732 outchar (L_('+')); \
733 --width; \
735 else if (space) \
737 outchar (L_(' ')); \
738 --width; \
741 if (number.word != 0 && alt && base == 16) \
743 outchar (L_('0')); \
744 outchar (spec); \
745 width -= 2; \
748 width -= workend - string + prec; \
750 if (prec > 0) \
752 int temp = width; \
753 width = prec; \
754 PAD (L_('0'));; \
755 width = temp; \
758 outstring (string, workend - string); \
760 PAD (L_(' ')); \
761 break; \
764 LABEL (form_float): \
766 /* Floating-point number. This is handled by printf_fp.c. */ \
767 const void *ptr; \
768 int function_done; \
770 if (fspec == NULL) \
772 if (__ldbl_is_dbl) \
773 is_long_double = 0; \
775 struct printf_info info = { .prec = prec, \
776 .width = width, \
777 .spec = spec, \
778 .is_long_double = is_long_double, \
779 .is_short = is_short, \
780 .is_long = is_long, \
781 .alt = alt, \
782 .space = space, \
783 .left = left, \
784 .showsign = showsign, \
785 .group = group, \
786 .pad = pad, \
787 .extra = 0, \
788 .i18n = use_outdigits, \
789 .wide = sizeof (CHAR_T) != 1 }; \
791 if (is_long_double) \
792 the_arg.pa_long_double = va_arg (ap, long double); \
793 else \
794 the_arg.pa_double = va_arg (ap, double); \
795 ptr = (const void *) &the_arg; \
797 function_done = __printf_fp (s, &info, &ptr); \
799 else \
801 ptr = (const void *) &args_value[fspec->data_arg]; \
802 if (__ldbl_is_dbl) \
804 fspec->data_arg_type = PA_DOUBLE; \
805 fspec->info.is_long_double = 0; \
808 function_done = __printf_fp (s, &fspec->info, &ptr); \
811 if (function_done < 0) \
813 /* Error in print handler. */ \
814 done = -1; \
815 goto all_done; \
818 done += function_done; \
820 break; \
822 LABEL (form_floathex): \
824 /* Floating point number printed as hexadecimal number. */ \
825 const void *ptr; \
826 int function_done; \
828 if (fspec == NULL) \
830 if (__ldbl_is_dbl) \
831 is_long_double = 0; \
833 struct printf_info info = { .prec = prec, \
834 .width = width, \
835 .spec = spec, \
836 .is_long_double = is_long_double, \
837 .is_short = is_short, \
838 .is_long = is_long, \
839 .alt = alt, \
840 .space = space, \
841 .left = left, \
842 .showsign = showsign, \
843 .group = group, \
844 .pad = pad, \
845 .extra = 0, \
846 .wide = sizeof (CHAR_T) != 1 }; \
848 if (is_long_double) \
849 the_arg.pa_long_double = va_arg (ap, long double); \
850 else \
851 the_arg.pa_double = va_arg (ap, double); \
852 ptr = (const void *) &the_arg; \
854 function_done = __printf_fphex (s, &info, &ptr); \
856 else \
858 ptr = (const void *) &args_value[fspec->data_arg]; \
859 if (__ldbl_is_dbl) \
860 fspec->info.is_long_double = 0; \
862 function_done = __printf_fphex (s, &fspec->info, &ptr); \
865 if (function_done < 0) \
867 /* Error in print handler. */ \
868 done = -1; \
869 goto all_done; \
872 done += function_done; \
874 break; \
876 LABEL (form_pointer): \
877 /* Generic pointer. */ \
879 const void *ptr; \
880 if (fspec == NULL) \
881 ptr = va_arg (ap, void *); \
882 else \
883 ptr = args_value[fspec->data_arg].pa_pointer; \
884 if (ptr != NULL) \
886 /* If the pointer is not NULL, write it as a %#x spec. */ \
887 base = 16; \
888 number.word = (unsigned long int) ptr; \
889 is_negative = 0; \
890 alt = 1; \
891 group = 0; \
892 spec = L_('x'); \
893 goto LABEL (number); \
895 else \
897 /* Write "(nil)" for a nil pointer. */ \
898 string = (CHAR_T *) L_("(nil)"); \
899 /* Make sure the full string "(nil)" is printed. */ \
900 if (prec < 5) \
901 prec = 5; \
902 is_long = 0; /* This is no wide-char string. */ \
903 goto LABEL (print_string); \
906 /* NOTREACHED */ \
908 LABEL (form_number): \
909 if (s->_flags2 & _IO_FLAGS2_FORTIFY) \
911 if (! readonly_format) \
913 extern int __readonly_area (const void *, size_t) \
914 attribute_hidden; \
915 readonly_format \
916 = __readonly_area (format, ((STR_LEN (format) + 1) \
917 * sizeof (CHAR_T))); \
919 if (readonly_format < 0) \
920 __libc_fatal ("*** %n in writable segment detected ***\n"); \
922 /* Answer the count of characters written. */ \
923 if (fspec == NULL) \
925 if (is_longlong) \
926 *(long long int *) va_arg (ap, void *) = done; \
927 else if (is_long_num) \
928 *(long int *) va_arg (ap, void *) = done; \
929 else if (is_char) \
930 *(char *) va_arg (ap, void *) = done; \
931 else if (!is_short) \
932 *(int *) va_arg (ap, void *) = done; \
933 else \
934 *(short int *) va_arg (ap, void *) = done; \
936 else \
937 if (is_longlong) \
938 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
939 else if (is_long_num) \
940 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
941 else if (is_char) \
942 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
943 else if (!is_short) \
944 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
945 else \
946 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
947 break; \
949 LABEL (form_strerror): \
950 /* Print description of error ERRNO. */ \
951 string = \
952 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
953 sizeof work_buffer); \
954 is_long = 0; /* This is no wide-char string. */ \
955 goto LABEL (print_string)
957 #ifdef COMPILE_WPRINTF
958 # define process_string_arg(fspec) \
959 LABEL (form_character): \
960 /* Character. */ \
961 if (is_long) \
962 goto LABEL (form_wcharacter); \
963 --width; /* Account for the character itself. */ \
964 if (!left) \
965 PAD (L' '); \
966 if (fspec == NULL) \
967 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
968 else \
969 outchar (__btowc ((unsigned char) \
970 args_value[fspec->data_arg].pa_int)); \
971 if (left) \
972 PAD (L' '); \
973 break; \
975 LABEL (form_wcharacter): \
977 /* Wide character. */ \
978 --width; \
979 if (!left) \
980 PAD (L' '); \
981 if (fspec == NULL) \
982 outchar (va_arg (ap, wchar_t)); \
983 else \
984 outchar (args_value[fspec->data_arg].pa_wchar); \
985 if (left) \
986 PAD (L' '); \
988 break; \
990 LABEL (form_string): \
992 size_t len; \
993 int string_malloced; \
995 /* The string argument could in fact be `char *' or `wchar_t *'. \
996 But this should not make a difference here. */ \
997 if (fspec == NULL) \
998 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
999 else \
1000 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
1002 /* Entry point for printing other strings. */ \
1003 LABEL (print_string): \
1005 string_malloced = 0; \
1006 if (string == NULL) \
1008 /* Write "(null)" if there's space. */ \
1009 if (prec == -1 \
1010 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
1012 string = (CHAR_T *) null; \
1013 len = (sizeof (null) / sizeof (null[0])) - 1; \
1015 else \
1017 string = (CHAR_T *) L""; \
1018 len = 0; \
1021 else if (!is_long && spec != L_('S')) \
1023 /* This is complicated. We have to transform the multibyte \
1024 string into a wide character string. */ \
1025 const char *mbs = (const char *) string; \
1026 mbstate_t mbstate; \
1028 len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
1030 /* Allocate dynamically an array which definitely is long \
1031 enough for the wide character version. Each byte in the \
1032 multi-byte string can produce at most one wide character. */ \
1033 if (__libc_use_alloca (len * sizeof (wchar_t))) \
1034 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1035 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1036 == NULL) \
1038 done = -1; \
1039 goto all_done; \
1041 else \
1042 string_malloced = 1; \
1044 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1045 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1046 if (len == (size_t) -1) \
1048 /* Illegal multibyte character. */ \
1049 done = -1; \
1050 goto all_done; \
1053 else \
1055 if (prec != -1) \
1056 /* Search for the end of the string, but don't search past \
1057 the length specified by the precision. */ \
1058 len = __wcsnlen (string, prec); \
1059 else \
1060 len = __wcslen (string); \
1063 if ((width -= len) < 0) \
1065 outstring (string, len); \
1066 break; \
1069 if (!left) \
1070 PAD (L' '); \
1071 outstring (string, len); \
1072 if (left) \
1073 PAD (L' '); \
1074 if (__builtin_expect (string_malloced, 0)) \
1075 free (string); \
1077 break;
1078 #else
1079 # define process_string_arg(fspec) \
1080 LABEL (form_character): \
1081 /* Character. */ \
1082 if (is_long) \
1083 goto LABEL (form_wcharacter); \
1084 --width; /* Account for the character itself. */ \
1085 if (!left) \
1086 PAD (' '); \
1087 if (fspec == NULL) \
1088 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1089 else \
1090 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1091 if (left) \
1092 PAD (' '); \
1093 break; \
1095 LABEL (form_wcharacter): \
1097 /* Wide character. */ \
1098 char buf[MB_CUR_MAX]; \
1099 mbstate_t mbstate; \
1100 size_t len; \
1102 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1103 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1104 : args_value[fspec->data_arg].pa_wchar), \
1105 &mbstate); \
1106 if (len == (size_t) -1) \
1108 /* Something went wron gduring the conversion. Bail out. */ \
1109 done = -1; \
1110 goto all_done; \
1112 width -= len; \
1113 if (!left) \
1114 PAD (' '); \
1115 outstring (buf, len); \
1116 if (left) \
1117 PAD (' '); \
1119 break; \
1121 LABEL (form_string): \
1123 size_t len; \
1124 int string_malloced; \
1126 /* The string argument could in fact be `char *' or `wchar_t *'. \
1127 But this should not make a difference here. */ \
1128 if (fspec == NULL) \
1129 string = (char *) va_arg (ap, const char *); \
1130 else \
1131 string = (char *) args_value[fspec->data_arg].pa_string; \
1133 /* Entry point for printing other strings. */ \
1134 LABEL (print_string): \
1136 string_malloced = 0; \
1137 if (string == NULL) \
1139 /* Write "(null)" if there's space. */ \
1140 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1142 string = (char *) null; \
1143 len = sizeof (null) - 1; \
1145 else \
1147 string = (char *) ""; \
1148 len = 0; \
1151 else if (!is_long && spec != L_('S')) \
1153 if (prec != -1) \
1155 /* Search for the end of the string, but don't search past \
1156 the length (in bytes) specified by the precision. Also \
1157 don't use incomplete characters. */ \
1158 if (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MB_CUR_MAX) == 1) \
1159 len = __strnlen (string, prec); \
1160 else \
1162 /* In case we have a multibyte character set the \
1163 situation is more complicated. We must not copy \
1164 bytes at the end which form an incomplete character. */\
1165 size_t ignore_size = (unsigned) prec > 1024 ? 1024 : prec;\
1166 wchar_t ignore[ignore_size]; \
1167 const char *str2 = string; \
1168 const char *strend = string + prec; \
1169 if (strend < string) \
1170 strend = (const char *) UINTPTR_MAX; \
1172 mbstate_t ps; \
1173 memset (&ps, '\0', sizeof (ps)); \
1175 while (str2 != NULL && str2 < strend) \
1176 if (__mbsnrtowcs (ignore, &str2, strend - str2, \
1177 ignore_size, &ps) == (size_t) -1) \
1179 done = -1; \
1180 goto all_done; \
1183 if (str2 == NULL) \
1184 len = strlen (string); \
1185 else \
1186 len = str2 - string - (ps.__count & 7); \
1189 else \
1190 len = strlen (string); \
1192 else \
1194 const wchar_t *s2 = (const wchar_t *) string; \
1195 mbstate_t mbstate; \
1197 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1199 if (prec >= 0) \
1201 /* The string `s2' might not be NUL terminated. */ \
1202 if (__libc_use_alloca (prec)) \
1203 string = (char *) alloca (prec); \
1204 else if ((string = (char *) malloc (prec)) == NULL) \
1206 done = -1; \
1207 goto all_done; \
1209 else \
1210 string_malloced = 1; \
1211 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1213 else \
1215 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1216 if (len != (size_t) -1) \
1218 assert (__mbsinit (&mbstate)); \
1219 s2 = (const wchar_t *) string; \
1220 if (__libc_use_alloca (len + 1)) \
1221 string = (char *) alloca (len + 1); \
1222 else if ((string = (char *) malloc (len + 1)) == NULL) \
1224 done = -1; \
1225 goto all_done; \
1227 else \
1228 string_malloced = 1; \
1229 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1233 if (len == (size_t) -1) \
1235 /* Illegal wide-character string. */ \
1236 done = -1; \
1237 goto all_done; \
1241 if ((width -= len) < 0) \
1243 outstring (string, len); \
1244 break; \
1247 if (!left) \
1248 PAD (' '); \
1249 outstring (string, len); \
1250 if (left) \
1251 PAD (' '); \
1252 if (__builtin_expect (string_malloced, 0)) \
1253 free (string); \
1255 break;
1256 #endif
1258 /* Orient the stream. */
1259 #ifdef ORIENT
1260 ORIENT;
1261 #endif
1263 /* Sanity check of arguments. */
1264 ARGCHECK (s, format);
1266 #ifdef ORIENT
1267 /* Check for correct orientation. */
1268 if (_IO_vtable_offset (s) == 0 &&
1269 _IO_fwide (s, sizeof (CHAR_T) == 1 ? -1 : 1)
1270 != (sizeof (CHAR_T) == 1 ? -1 : 1))
1271 /* The stream is already oriented otherwise. */
1272 return EOF;
1273 #endif
1275 if (UNBUFFERED_P (s))
1276 /* Use a helper function which will allocate a local temporary buffer
1277 for the stream and then call us again. */
1278 return buffered_vfprintf (s, format, ap);
1280 /* Initialize local variables. */
1281 done = 0;
1282 grouping = (const char *) -1;
1283 #ifdef __va_copy
1284 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1285 since on some systems `va_list' is not an integral type. */
1286 __va_copy (ap_save, ap);
1287 #else
1288 ap_save = ap;
1289 #endif
1290 nspecs_done = 0;
1292 #ifdef COMPILE_WPRINTF
1293 /* Find the first format specifier. */
1294 f = lead_str_end = __find_specwc ((const UCHAR_T *) format);
1295 #else
1296 /* Put state for processing format string in initial state. */
1297 memset (&mbstate, '\0', sizeof (mbstate_t));
1299 /* Find the first format specifier. */
1300 f = lead_str_end = __find_specmb (format, &mbstate);
1301 #endif
1303 /* Lock stream. */
1304 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
1305 _IO_flockfile (s);
1307 /* Write the literal text before the first format. */
1308 outstring ((const UCHAR_T *) format,
1309 lead_str_end - (const UCHAR_T *) format);
1311 /* If we only have to print a simple string, return now. */
1312 if (*f == L_('\0'))
1313 goto all_done;
1315 /* Process whole format string. */
1318 #ifdef SHARED
1319 # define REF(Name) &&do_##Name - &&do_form_unknown
1320 #else
1321 # define REF(Name) &&do_##Name
1322 #endif
1323 #define LABEL(Name) do_##Name
1324 STEP0_3_TABLE;
1325 STEP4_TABLE;
1327 union printf_arg *args_value; /* This is not used here but ... */
1328 int is_negative; /* Flag for negative number. */
1329 union
1331 unsigned long long int longlong;
1332 unsigned long int word;
1333 } number;
1334 int base;
1335 union printf_arg the_arg;
1336 CHAR_T *string; /* Pointer to argument string. */
1337 int alt = 0; /* Alternate format. */
1338 int space = 0; /* Use space prefix if no sign is needed. */
1339 int left = 0; /* Left-justify output. */
1340 int showsign = 0; /* Always begin with plus or minus sign. */
1341 int group = 0; /* Print numbers according grouping rules. */
1342 int is_long_double = 0; /* Argument is long double/ long long int. */
1343 int is_short = 0; /* Argument is short int. */
1344 int is_long = 0; /* Argument is long int. */
1345 int is_char = 0; /* Argument is promoted (unsigned) char. */
1346 int width = 0; /* Width of output; 0 means none specified. */
1347 int prec = -1; /* Precision of output; -1 means none specified. */
1348 /* This flag is set by the 'I' modifier and selects the use of the
1349 `outdigits' as determined by the current locale. */
1350 int use_outdigits = 0;
1351 UCHAR_T pad = L_(' ');/* Padding character. */
1352 CHAR_T spec;
1354 workstart = NULL;
1355 workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1357 /* Get current character in format string. */
1358 JUMP (*++f, step0_jumps);
1360 /* ' ' flag. */
1361 LABEL (flag_space):
1362 space = 1;
1363 JUMP (*++f, step0_jumps);
1365 /* '+' flag. */
1366 LABEL (flag_plus):
1367 showsign = 1;
1368 JUMP (*++f, step0_jumps);
1370 /* The '-' flag. */
1371 LABEL (flag_minus):
1372 left = 1;
1373 pad = L_(' ');
1374 JUMP (*++f, step0_jumps);
1376 /* The '#' flag. */
1377 LABEL (flag_hash):
1378 alt = 1;
1379 JUMP (*++f, step0_jumps);
1381 /* The '0' flag. */
1382 LABEL (flag_zero):
1383 if (!left)
1384 pad = L_('0');
1385 JUMP (*++f, step0_jumps);
1387 /* The '\'' flag. */
1388 LABEL (flag_quote):
1389 group = 1;
1391 if (grouping == (const char *) -1)
1393 #ifdef COMPILE_WPRINTF
1394 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1395 _NL_NUMERIC_THOUSANDS_SEP_WC);
1396 #else
1397 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1398 #endif
1400 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1401 if (*grouping == '\0' || *grouping == CHAR_MAX
1402 #ifdef COMPILE_WPRINTF
1403 || thousands_sep == L'\0'
1404 #else
1405 || *thousands_sep == '\0'
1406 #endif
1408 grouping = NULL;
1410 JUMP (*++f, step0_jumps);
1412 LABEL (flag_i18n):
1413 use_outdigits = 1;
1414 JUMP (*++f, step0_jumps);
1416 /* Get width from argument. */
1417 LABEL (width_asterics):
1419 const UCHAR_T *tmp; /* Temporary value. */
1421 tmp = ++f;
1422 if (ISDIGIT (*tmp) && read_int (&tmp) && *tmp == L_('$'))
1423 /* The width comes from a positional parameter. */
1424 goto do_positional;
1426 width = va_arg (ap, int);
1428 /* Negative width means left justified. */
1429 if (width < 0)
1431 width = -width;
1432 pad = L_(' ');
1433 left = 1;
1436 if (width + 32 >= (int) (sizeof (work_buffer)
1437 / sizeof (work_buffer[0])))
1439 /* We have to use a special buffer. The "32" is just a safe
1440 bet for all the output which is not counted in the width. */
1441 if (__libc_use_alloca ((width + 32) * sizeof (CHAR_T)))
1442 workend = ((CHAR_T *) alloca ((width + 32) * sizeof (CHAR_T))
1443 + (width + 32));
1444 else
1446 workstart = (CHAR_T *) malloc ((width + 32) * sizeof (CHAR_T));
1447 if (workstart == NULL)
1449 done = -1;
1450 goto all_done;
1452 workend = workstart + (width + 32);
1456 JUMP (*f, step1_jumps);
1458 /* Given width in format string. */
1459 LABEL (width):
1460 width = read_int (&f);
1462 if (width + 32 >= (int) (sizeof (work_buffer) / sizeof (work_buffer[0])))
1464 /* We have to use a special buffer. The "32" is just a safe
1465 bet for all the output which is not counted in the width. */
1466 if (__libc_use_alloca ((width + 32) * sizeof (CHAR_T)))
1467 workend = ((CHAR_T *) alloca ((width + 32) * sizeof (CHAR_T))
1468 + (width + 32));
1469 else
1471 workstart = (CHAR_T *) malloc ((width + 32) * sizeof (CHAR_T));
1472 if (workstart == NULL)
1474 done = -1;
1475 goto all_done;
1477 workend = workstart + (width + 32);
1480 if (*f == L_('$'))
1481 /* Oh, oh. The argument comes from a positional parameter. */
1482 goto do_positional;
1483 JUMP (*f, step1_jumps);
1485 LABEL (precision):
1486 ++f;
1487 if (*f == L_('*'))
1489 const UCHAR_T *tmp; /* Temporary value. */
1491 tmp = ++f;
1492 if (ISDIGIT (*tmp) && read_int (&tmp) > 0 && *tmp == L_('$'))
1493 /* The precision comes from a positional parameter. */
1494 goto do_positional;
1496 prec = va_arg (ap, int);
1498 /* If the precision is negative the precision is omitted. */
1499 if (prec < 0)
1500 prec = -1;
1502 else if (ISDIGIT (*f))
1503 prec = read_int (&f);
1504 else
1505 prec = 0;
1506 if (prec > width
1507 && prec + 32 > (int)(sizeof (work_buffer) / sizeof (work_buffer[0])))
1509 if (__libc_use_alloca ((prec + 32) * sizeof (CHAR_T)))
1510 workend = ((CHAR_T *) alloca ((prec + 32) * sizeof (CHAR_T)))
1511 + (prec + 32);
1512 else
1514 workstart = (CHAR_T *) malloc ((prec + 32) * sizeof (CHAR_T));
1515 if (workstart == NULL)
1517 done = -1;
1518 goto all_done;
1520 workend = workstart + (prec + 32);
1523 JUMP (*f, step2_jumps);
1525 /* Process 'h' modifier. There might another 'h' following. */
1526 LABEL (mod_half):
1527 is_short = 1;
1528 JUMP (*++f, step3a_jumps);
1530 /* Process 'hh' modifier. */
1531 LABEL (mod_halfhalf):
1532 is_short = 0;
1533 is_char = 1;
1534 JUMP (*++f, step4_jumps);
1536 /* Process 'l' modifier. There might another 'l' following. */
1537 LABEL (mod_long):
1538 is_long = 1;
1539 JUMP (*++f, step3b_jumps);
1541 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1542 allowed to follow. */
1543 LABEL (mod_longlong):
1544 is_long_double = 1;
1545 is_long = 1;
1546 JUMP (*++f, step4_jumps);
1548 LABEL (mod_size_t):
1549 is_long_double = sizeof (size_t) > sizeof (unsigned long int);
1550 is_long = sizeof (size_t) > sizeof (unsigned int);
1551 JUMP (*++f, step4_jumps);
1553 LABEL (mod_ptrdiff_t):
1554 is_long_double = sizeof (ptrdiff_t) > sizeof (unsigned long int);
1555 is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
1556 JUMP (*++f, step4_jumps);
1558 LABEL (mod_intmax_t):
1559 is_long_double = sizeof (intmax_t) > sizeof (unsigned long int);
1560 is_long = sizeof (intmax_t) > sizeof (unsigned int);
1561 JUMP (*++f, step4_jumps);
1563 /* Process current format. */
1564 while (1)
1566 process_arg (((struct printf_spec *) NULL));
1567 process_string_arg (((struct printf_spec *) NULL));
1569 LABEL (form_unknown):
1570 if (spec == L_('\0'))
1572 /* The format string ended before the specifier is complete. */
1573 done = -1;
1574 goto all_done;
1577 /* If we are in the fast loop force entering the complicated
1578 one. */
1579 goto do_positional;
1582 /* The format is correctly handled. */
1583 ++nspecs_done;
1585 if (__builtin_expect (workstart != NULL, 0))
1586 free (workstart);
1587 workstart = NULL;
1589 /* Look for next format specifier. */
1590 #ifdef COMPILE_WPRINTF
1591 f = __find_specwc ((end_of_spec = ++f));
1592 #else
1593 f = __find_specmb ((end_of_spec = ++f), &mbstate);
1594 #endif
1596 /* Write the following constant string. */
1597 outstring (end_of_spec, f - end_of_spec);
1599 while (*f != L_('\0'));
1601 /* Unlock stream and return. */
1602 goto all_done;
1604 /* Here starts the more complex loop to handle positional parameters. */
1605 do_positional:
1607 /* Array with information about the needed arguments. This has to
1608 be dynamically extensible. */
1609 size_t nspecs = 0;
1610 size_t nspecs_max = 32; /* A more or less arbitrary start value. */
1611 struct printf_spec *specs
1612 = alloca (nspecs_max * sizeof (struct printf_spec));
1614 /* The number of arguments the format string requests. This will
1615 determine the size of the array needed to store the argument
1616 attributes. */
1617 size_t nargs = 0;
1618 int *args_type;
1619 union printf_arg *args_value = NULL;
1621 /* Positional parameters refer to arguments directly. This could
1622 also determine the maximum number of arguments. Track the
1623 maximum number. */
1624 size_t max_ref_arg = 0;
1626 /* Just a counter. */
1627 size_t cnt;
1629 free (workstart);
1630 workstart = NULL;
1632 if (grouping == (const char *) -1)
1634 #ifdef COMPILE_WPRINTF
1635 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1636 _NL_NUMERIC_THOUSANDS_SEP_WC);
1637 #else
1638 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1639 #endif
1641 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1642 if (*grouping == '\0' || *grouping == CHAR_MAX)
1643 grouping = NULL;
1646 for (f = lead_str_end; *f != L_('\0'); f = specs[nspecs++].next_fmt)
1648 if (nspecs >= nspecs_max)
1650 /* Extend the array of format specifiers. */
1651 struct printf_spec *old = specs;
1653 nspecs_max *= 2;
1654 specs = alloca (nspecs_max * sizeof (struct printf_spec));
1656 if (specs == &old[nspecs])
1657 /* Stack grows up, OLD was the last thing allocated;
1658 extend it. */
1659 nspecs_max += nspecs_max / 2;
1660 else
1662 /* Copy the old array's elements to the new space. */
1663 memcpy (specs, old, nspecs * sizeof (struct printf_spec));
1664 if (old == &specs[nspecs])
1665 /* Stack grows down, OLD was just below the new
1666 SPECS. We can use that space when the new space
1667 runs out. */
1668 nspecs_max += nspecs_max / 2;
1672 /* Parse the format specifier. */
1673 #ifdef COMPILE_WPRINTF
1674 nargs += __parse_one_specwc (f, nargs, &specs[nspecs], &max_ref_arg);
1675 #else
1676 nargs += __parse_one_specmb (f, nargs, &specs[nspecs], &max_ref_arg,
1677 &mbstate);
1678 #endif
1681 /* Determine the number of arguments the format string consumes. */
1682 nargs = MAX (nargs, max_ref_arg);
1684 /* Allocate memory for the argument descriptions. */
1685 args_type = alloca (nargs * sizeof (int));
1686 memset (args_type, s->_flags2 & _IO_FLAGS2_FORTIFY ? '\xff' : '\0',
1687 nargs * sizeof (int));
1688 args_value = alloca (nargs * sizeof (union printf_arg));
1690 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1691 still zero after this loop, format is invalid. For now we
1692 simply use 0 as the value. */
1694 /* Fill in the types of all the arguments. */
1695 for (cnt = 0; cnt < nspecs; ++cnt)
1697 /* If the width is determined by an argument this is an int. */
1698 if (specs[cnt].width_arg != -1)
1699 args_type[specs[cnt].width_arg] = PA_INT;
1701 /* If the precision is determined by an argument this is an int. */
1702 if (specs[cnt].prec_arg != -1)
1703 args_type[specs[cnt].prec_arg] = PA_INT;
1705 switch (specs[cnt].ndata_args)
1707 case 0: /* No arguments. */
1708 break;
1709 case 1: /* One argument; we already have the type. */
1710 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1711 break;
1712 default:
1713 /* We have more than one argument for this format spec.
1714 We must call the arginfo function again to determine
1715 all the types. */
1716 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1717 (&specs[cnt].info,
1718 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg]);
1719 break;
1723 /* Now we know all the types and the order. Fill in the argument
1724 values. */
1725 for (cnt = 0; cnt < nargs; ++cnt)
1726 switch (args_type[cnt])
1728 #define T(tag, mem, type) \
1729 case tag: \
1730 args_value[cnt].mem = va_arg (ap_save, type); \
1731 break
1733 T (PA_CHAR, pa_int, int); /* Promoted. */
1734 T (PA_WCHAR, pa_wchar, wint_t);
1735 T (PA_INT|PA_FLAG_SHORT, pa_int, int); /* Promoted. */
1736 T (PA_INT, pa_int, int);
1737 T (PA_INT|PA_FLAG_LONG, pa_long_int, long int);
1738 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1739 T (PA_FLOAT, pa_double, double); /* Promoted. */
1740 T (PA_DOUBLE, pa_double, double);
1741 case PA_DOUBLE|PA_FLAG_LONG_DOUBLE:
1742 if (__ldbl_is_dbl)
1744 args_value[cnt].pa_double = va_arg (ap_save, double);
1745 args_type[cnt] &= ~PA_FLAG_LONG_DOUBLE;
1747 else
1748 args_value[cnt].pa_long_double = va_arg (ap_save, long double);
1749 break;
1750 T (PA_STRING, pa_string, const char *);
1751 T (PA_WSTRING, pa_wstring, const wchar_t *);
1752 T (PA_POINTER, pa_pointer, void *);
1753 #undef T
1754 default:
1755 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1756 args_value[cnt].pa_pointer = va_arg (ap_save, void *);
1757 else
1758 args_value[cnt].pa_long_double = 0.0;
1759 break;
1760 case -1:
1761 /* Error case. Not all parameters appear in N$ format
1762 strings. We have no way to determine their type. */
1763 assert (s->_flags2 & _IO_FLAGS2_FORTIFY);
1764 __libc_fatal ("*** invalid %N$ use detected ***\n");
1767 /* Now walk through all format specifiers and process them. */
1768 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1770 #undef REF
1771 #ifdef SHARED
1772 # define REF(Name) &&do2_##Name - &&do_form_unknown
1773 #else
1774 # define REF(Name) &&do2_##Name
1775 #endif
1776 #undef LABEL
1777 #define LABEL(Name) do2_##Name
1778 STEP4_TABLE;
1780 int is_negative;
1781 union
1783 unsigned long long int longlong;
1784 unsigned long int word;
1785 } number;
1786 int base;
1787 union printf_arg the_arg;
1788 CHAR_T *string; /* Pointer to argument string. */
1790 /* Fill variables from values in struct. */
1791 int alt = specs[nspecs_done].info.alt;
1792 int space = specs[nspecs_done].info.space;
1793 int left = specs[nspecs_done].info.left;
1794 int showsign = specs[nspecs_done].info.showsign;
1795 int group = specs[nspecs_done].info.group;
1796 int is_long_double = specs[nspecs_done].info.is_long_double;
1797 int is_short = specs[nspecs_done].info.is_short;
1798 int is_char = specs[nspecs_done].info.is_char;
1799 int is_long = specs[nspecs_done].info.is_long;
1800 int width = specs[nspecs_done].info.width;
1801 int prec = specs[nspecs_done].info.prec;
1802 int use_outdigits = specs[nspecs_done].info.i18n;
1803 char pad = specs[nspecs_done].info.pad;
1804 CHAR_T spec = specs[nspecs_done].info.spec;
1806 workstart = NULL;
1807 workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1809 /* Fill in last information. */
1810 if (specs[nspecs_done].width_arg != -1)
1812 /* Extract the field width from an argument. */
1813 specs[nspecs_done].info.width =
1814 args_value[specs[nspecs_done].width_arg].pa_int;
1816 if (specs[nspecs_done].info.width < 0)
1817 /* If the width value is negative left justification is
1818 selected and the value is taken as being positive. */
1820 specs[nspecs_done].info.width *= -1;
1821 left = specs[nspecs_done].info.left = 1;
1823 width = specs[nspecs_done].info.width;
1826 if (specs[nspecs_done].prec_arg != -1)
1828 /* Extract the precision from an argument. */
1829 specs[nspecs_done].info.prec =
1830 args_value[specs[nspecs_done].prec_arg].pa_int;
1832 if (specs[nspecs_done].info.prec < 0)
1833 /* If the precision is negative the precision is
1834 omitted. */
1835 specs[nspecs_done].info.prec = -1;
1837 prec = specs[nspecs_done].info.prec;
1840 /* Maybe the buffer is too small. */
1841 if (MAX (prec, width) + 32 > (int) (sizeof (work_buffer)
1842 / sizeof (CHAR_T)))
1844 if (__libc_use_alloca ((MAX (prec, width) + 32)
1845 * sizeof (CHAR_T)))
1846 workend = ((CHAR_T *) alloca ((MAX (prec, width) + 32)
1847 * sizeof (CHAR_T))
1848 + (MAX (prec, width) + 32));
1849 else
1851 workstart = (CHAR_T *) malloc ((MAX (prec, width) + 32)
1852 * sizeof (CHAR_T));
1853 workend = workstart + (MAX (prec, width) + 32);
1857 /* Process format specifiers. */
1858 while (1)
1860 JUMP (spec, step4_jumps);
1862 process_arg ((&specs[nspecs_done]));
1863 process_string_arg ((&specs[nspecs_done]));
1865 LABEL (form_unknown):
1867 extern printf_function **__printf_function_table;
1868 int function_done;
1869 printf_function *function;
1870 unsigned int i;
1871 const void **ptr;
1873 function =
1874 (__printf_function_table == NULL ? NULL :
1875 __printf_function_table[specs[nspecs_done].info.spec]);
1877 if (function == NULL)
1878 function = &printf_unknown;
1880 ptr = alloca (specs[nspecs_done].ndata_args
1881 * sizeof (const void *));
1883 /* Fill in an array of pointers to the argument values. */
1884 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
1885 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1887 /* Call the function. */
1888 function_done = (*function) (s, &specs[nspecs_done].info, ptr);
1890 /* If an error occurred we don't have information about #
1891 of chars. */
1892 if (function_done < 0)
1894 done = -1;
1895 goto all_done;
1898 done += function_done;
1900 break;
1903 free (workstart);
1904 workstart = NULL;
1906 /* Write the following constant string. */
1907 outstring (specs[nspecs_done].end_of_fmt,
1908 specs[nspecs_done].next_fmt
1909 - specs[nspecs_done].end_of_fmt);
1913 all_done:
1914 if (__builtin_expect (workstart != NULL, 0))
1915 free (workstart);
1916 /* Unlock the stream. */
1917 _IO_funlockfile (s);
1918 _IO_cleanup_region_end (0);
1920 return done;
1923 /* Handle an unknown format specifier. This prints out a canonicalized
1924 representation of the format spec itself. */
1925 static int
1926 printf_unknown (FILE *s, const struct printf_info *info,
1927 const void *const *args)
1930 int done = 0;
1931 CHAR_T work_buffer[MAX (sizeof (info->width), sizeof (info->prec)) * 3];
1932 CHAR_T *const workend
1933 = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1934 register CHAR_T *w;
1936 outchar (L_('%'));
1938 if (info->alt)
1939 outchar (L_('#'));
1940 if (info->group)
1941 outchar (L_('\''));
1942 if (info->showsign)
1943 outchar (L_('+'));
1944 else if (info->space)
1945 outchar (L_(' '));
1946 if (info->left)
1947 outchar (L_('-'));
1948 if (info->pad == L_('0'))
1949 outchar (L_('0'));
1950 if (info->i18n)
1951 outchar (L_('I'));
1953 if (info->width != 0)
1955 w = _itoa_word (info->width, workend, 10, 0);
1956 while (w < workend)
1957 outchar (*w++);
1960 if (info->prec != -1)
1962 outchar (L_('.'));
1963 w = _itoa_word (info->prec, workend, 10, 0);
1964 while (w < workend)
1965 outchar (*w++);
1968 if (info->spec != L_('\0'))
1969 outchar (info->spec);
1971 all_done:
1972 return done;
1975 /* Group the digits according to the grouping rules of the current locale.
1976 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1977 static CHAR_T *
1978 internal_function
1979 group_number (CHAR_T *w, CHAR_T *rear_ptr, const char *grouping,
1980 #ifdef COMPILE_WPRINTF
1981 wchar_t thousands_sep
1982 #else
1983 const char *thousands_sep
1984 #endif
1987 int len;
1988 CHAR_T *src, *s;
1989 #ifndef COMPILE_WPRINTF
1990 int tlen = strlen (thousands_sep);
1991 #endif
1993 /* We treat all negative values like CHAR_MAX. */
1995 if (*grouping == CHAR_MAX || *grouping <= 0)
1996 /* No grouping should be done. */
1997 return w;
1999 len = *grouping++;
2001 /* Copy existing string so that nothing gets overwritten. */
2002 src = (CHAR_T *) alloca ((rear_ptr - w) * sizeof (CHAR_T));
2003 s = (CHAR_T *) __mempcpy (src, w,
2004 (rear_ptr - w) * sizeof (CHAR_T));
2005 w = rear_ptr;
2007 /* Process all characters in the string. */
2008 while (s > src)
2010 *--w = *--s;
2012 if (--len == 0 && s > src)
2014 /* A new group begins. */
2015 #ifdef COMPILE_WPRINTF
2016 *--w = thousands_sep;
2017 #else
2018 int cnt = tlen;
2020 *--w = thousands_sep[--cnt];
2021 while (cnt > 0);
2022 #endif
2024 if (*grouping == CHAR_MAX
2025 #if CHAR_MIN < 0
2026 || *grouping < 0
2027 #endif
2030 /* No further grouping to be done.
2031 Copy the rest of the number. */
2033 *--w = *--s;
2034 while (s > src);
2035 break;
2037 else if (*grouping != '\0')
2038 /* The previous grouping repeats ad infinitum. */
2039 len = *grouping++;
2040 else
2041 len = grouping[-1];
2044 return w;
2047 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2048 struct helper_file
2050 struct _IO_FILE_plus _f;
2051 #ifdef COMPILE_WPRINTF
2052 struct _IO_wide_data _wide_data;
2053 #endif
2054 _IO_FILE *_put_stream;
2055 #ifdef _IO_MTSAFE_IO
2056 _IO_lock_t lock;
2057 #endif
2060 static int
2061 _IO_helper_overflow (_IO_FILE *s, int c)
2063 _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
2064 #ifdef COMPILE_WPRINTF
2065 int used = s->_wide_data->_IO_write_ptr - s->_wide_data->_IO_write_base;
2066 if (used)
2068 _IO_size_t written = _IO_sputn (target, s->_wide_data->_IO_write_base,
2069 used);
2070 s->_wide_data->_IO_write_ptr -= written;
2072 #else
2073 int used = s->_IO_write_ptr - s->_IO_write_base;
2074 if (used)
2076 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
2077 s->_IO_write_ptr -= written;
2079 #endif
2080 return PUTC (c, s);
2083 #ifdef COMPILE_WPRINTF
2084 static const struct _IO_jump_t _IO_helper_jumps =
2086 JUMP_INIT_DUMMY,
2087 JUMP_INIT (finish, INTUSE(_IO_wdefault_finish)),
2088 JUMP_INIT (overflow, _IO_helper_overflow),
2089 JUMP_INIT (underflow, _IO_default_underflow),
2090 JUMP_INIT (uflow, INTUSE(_IO_default_uflow)),
2091 JUMP_INIT (pbackfail, (_IO_pbackfail_t) INTUSE(_IO_wdefault_pbackfail)),
2092 JUMP_INIT (xsputn, INTUSE(_IO_wdefault_xsputn)),
2093 JUMP_INIT (xsgetn, INTUSE(_IO_wdefault_xsgetn)),
2094 JUMP_INIT (seekoff, _IO_default_seekoff),
2095 JUMP_INIT (seekpos, _IO_default_seekpos),
2096 JUMP_INIT (setbuf, _IO_default_setbuf),
2097 JUMP_INIT (sync, _IO_default_sync),
2098 JUMP_INIT (doallocate, INTUSE(_IO_wdefault_doallocate)),
2099 JUMP_INIT (read, _IO_default_read),
2100 JUMP_INIT (write, _IO_default_write),
2101 JUMP_INIT (seek, _IO_default_seek),
2102 JUMP_INIT (close, _IO_default_close),
2103 JUMP_INIT (stat, _IO_default_stat)
2105 #else
2106 static const struct _IO_jump_t _IO_helper_jumps =
2108 JUMP_INIT_DUMMY,
2109 JUMP_INIT (finish, INTUSE(_IO_default_finish)),
2110 JUMP_INIT (overflow, _IO_helper_overflow),
2111 JUMP_INIT (underflow, _IO_default_underflow),
2112 JUMP_INIT (uflow, INTUSE(_IO_default_uflow)),
2113 JUMP_INIT (pbackfail, INTUSE(_IO_default_pbackfail)),
2114 JUMP_INIT (xsputn, INTUSE(_IO_default_xsputn)),
2115 JUMP_INIT (xsgetn, INTUSE(_IO_default_xsgetn)),
2116 JUMP_INIT (seekoff, _IO_default_seekoff),
2117 JUMP_INIT (seekpos, _IO_default_seekpos),
2118 JUMP_INIT (setbuf, _IO_default_setbuf),
2119 JUMP_INIT (sync, _IO_default_sync),
2120 JUMP_INIT (doallocate, INTUSE(_IO_default_doallocate)),
2121 JUMP_INIT (read, _IO_default_read),
2122 JUMP_INIT (write, _IO_default_write),
2123 JUMP_INIT (seek, _IO_default_seek),
2124 JUMP_INIT (close, _IO_default_close),
2125 JUMP_INIT (stat, _IO_default_stat)
2127 #endif
2129 static int
2130 internal_function
2131 buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format,
2132 _IO_va_list args)
2134 CHAR_T buf[_IO_BUFSIZ];
2135 struct helper_file helper;
2136 register _IO_FILE *hp = (_IO_FILE *) &helper._f;
2137 int result, to_flush;
2139 /* Orient the stream. */
2140 #ifdef ORIENT
2141 ORIENT;
2142 #endif
2144 /* Initialize helper. */
2145 helper._put_stream = s;
2146 #ifdef COMPILE_WPRINTF
2147 hp->_wide_data = &helper._wide_data;
2148 _IO_wsetp (hp, buf, buf + sizeof buf / sizeof (CHAR_T));
2149 hp->_mode = 1;
2150 #else
2151 _IO_setp (hp, buf, buf + sizeof buf);
2152 hp->_mode = -1;
2153 #endif
2154 hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS|_IO_USER_LOCK;
2155 #if _IO_JUMPS_OFFSET
2156 hp->_vtable_offset = 0;
2157 #endif
2158 #ifdef _IO_MTSAFE_IO
2159 hp->_lock = NULL;
2160 #endif
2161 hp->_flags2 = s->_flags2;
2162 _IO_JUMPS (&helper._f) = (struct _IO_jump_t *) &_IO_helper_jumps;
2164 /* Now print to helper instead. */
2165 #ifndef COMPILE_WPRINTF
2166 result = INTUSE(_IO_vfprintf) (hp, format, args);
2167 #else
2168 result = vfprintf (hp, format, args);
2169 #endif
2171 /* Lock stream. */
2172 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, s);
2173 _IO_flockfile (s);
2175 /* Now flush anything from the helper to the S. */
2176 #ifdef COMPILE_WPRINTF
2177 if ((to_flush = (hp->_wide_data->_IO_write_ptr
2178 - hp->_wide_data->_IO_write_base)) > 0)
2180 if ((int) _IO_sputn (s, hp->_wide_data->_IO_write_base, to_flush)
2181 != to_flush)
2182 result = -1;
2184 #else
2185 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
2187 if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
2188 result = -1;
2190 #endif
2192 /* Unlock the stream. */
2193 _IO_funlockfile (s);
2194 __libc_cleanup_region_end (0);
2196 return result;
2199 #undef vfprintf
2200 #ifdef COMPILE_WPRINTF
2201 strong_alias (_IO_vfwprintf, __vfwprintf);
2202 ldbl_weak_alias (_IO_vfwprintf, vfwprintf);
2203 #else
2204 ldbl_strong_alias (_IO_vfprintf_internal, vfprintf);
2205 ldbl_hidden_def (_IO_vfprintf_internal, vfprintf)
2206 ldbl_strong_alias (_IO_vfprintf_internal, _IO_vfprintf);
2207 #endif