* bits/types.h: Don't include stddef.h, don't define __need_size_t.
[glibc.git] / stdio-common / vfprintf.c
blob25edde751113741701b74f00b83717974e35a0a3
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 # undef _itoa
103 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
104 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
105 # undef EOF
106 # define EOF WEOF
107 #endif
109 #include "_i18n_number.h"
111 /* Include the shared code for parsing the format string. */
112 #include "printf-parse.h"
115 #define outchar(Ch) \
116 do \
118 register const INT_T outc = (Ch); \
119 if (PUTC (outc, s) == EOF) \
121 done = -1; \
122 goto all_done; \
124 else \
125 ++done; \
127 while (0)
129 #define outstring(String, Len) \
130 do \
132 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
134 done = -1; \
135 goto all_done; \
137 done += (Len); \
139 while (0)
141 /* For handling long_double and longlong we use the same flag. If
142 `long' and `long long' are effectively the same type define it to
143 zero. */
144 #if LONG_MAX == LONG_LONG_MAX
145 # define is_longlong 0
146 #else
147 # define is_longlong is_long_double
148 #endif
150 /* If `long' and `int' is effectively the same type we don't have to
151 handle `long separately. */
152 #if INT_MAX == LONG_MAX
153 # define is_long_num 0
154 #else
155 # define is_long_num is_long
156 #endif
159 /* Global variables. */
160 static const CHAR_T null[] = L_("(null)");
163 /* Helper function to provide temporary buffering for unbuffered streams. */
164 static int buffered_vfprintf (FILE *stream, const CHAR_T *fmt, va_list)
165 __THROW __attribute__ ((noinline)) internal_function;
167 /* Handle unknown format specifier. */
168 static int printf_unknown (FILE *, const struct printf_info *,
169 const void *const *) __THROW;
171 /* Group digits of number string. */
172 #ifdef COMPILE_WPRINTF
173 static CHAR_T *group_number (CHAR_T *, CHAR_T *, const char *, wchar_t)
174 __THROW internal_function;
175 #else
176 static CHAR_T *group_number (CHAR_T *, CHAR_T *, const char *, const char *)
177 __THROW internal_function;
178 #endif
181 /* The function itself. */
183 vfprintf (FILE *s, const CHAR_T *format, va_list ap)
185 /* The character used as thousands separator. */
186 #ifdef COMPILE_WPRINTF
187 wchar_t thousands_sep = L'\0';
188 #else
189 const char *thousands_sep = NULL;
190 #endif
192 /* The string describing the size of groups of digits. */
193 const char *grouping;
195 /* Place to accumulate the result. */
196 int done;
198 /* Current character in format string. */
199 const UCHAR_T *f;
201 /* End of leading constant string. */
202 const UCHAR_T *lead_str_end;
204 /* Points to next format specifier. */
205 const UCHAR_T *end_of_spec;
207 /* Buffer intermediate results. */
208 CHAR_T work_buffer[1000];
209 CHAR_T *workstart = NULL;
210 CHAR_T *workend;
212 /* State for restartable multibyte character handling functions. */
213 #ifndef COMPILE_WPRINTF
214 mbstate_t mbstate;
215 #endif
217 /* We have to save the original argument pointer. */
218 va_list ap_save;
220 /* Count number of specifiers we already processed. */
221 int nspecs_done;
223 /* For the %m format we may need the current `errno' value. */
224 int save_errno = errno;
226 /* 1 if format is in read-only memory, -1 if it is in writable memory,
227 0 if unknown. */
228 int readonly_format = 0;
230 /* This table maps a character into a number representing a
231 class. In each step there is a destination label for each
232 class. */
233 static const int jump_table[] =
235 /* ' ' */ 1, 0, 0, /* '#' */ 4,
236 0, /* '%' */ 14, 0, /* '\''*/ 6,
237 0, 0, /* '*' */ 7, /* '+' */ 2,
238 0, /* '-' */ 3, /* '.' */ 9, 0,
239 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
240 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
241 /* '8' */ 8, /* '9' */ 8, 0, 0,
242 0, 0, 0, 0,
243 0, /* 'A' */ 26, 0, /* 'C' */ 25,
244 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
245 0, /* 'I' */ 29, 0, 0,
246 /* 'L' */ 12, 0, 0, 0,
247 0, 0, 0, /* 'S' */ 21,
248 0, 0, 0, 0,
249 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
250 0, 0, 0, 0,
251 0, /* 'a' */ 26, 0, /* 'c' */ 20,
252 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
253 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
254 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
255 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
256 /* 't' */ 27, /* 'u' */ 16, 0, 0,
257 /* 'x' */ 18, 0, /* 'z' */ 13
260 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
261 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
262 #ifdef SHARED
263 /* 'int' is enough and it saves some space on 64 bit systems. */
264 # define JUMP_TABLE_TYPE const int
265 # define JUMP(ChExpr, table) \
266 do \
268 int offset; \
269 void *__unbounded ptr; \
270 spec = (ChExpr); \
271 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
272 : table[CHAR_CLASS (spec)]; \
273 ptr = &&do_form_unknown + offset; \
274 goto *ptr; \
276 while (0)
277 #else
278 # define JUMP_TABLE_TYPE const void *const
279 # define JUMP(ChExpr, table) \
280 do \
282 const void *__unbounded ptr; \
283 spec = (ChExpr); \
284 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
285 : table[CHAR_CLASS (spec)]; \
286 goto *ptr; \
288 while (0)
289 #endif
291 #define STEP0_3_TABLE \
292 /* Step 0: at the beginning. */ \
293 static JUMP_TABLE_TYPE step0_jumps[30] = \
295 REF (form_unknown), \
296 REF (flag_space), /* for ' ' */ \
297 REF (flag_plus), /* for '+' */ \
298 REF (flag_minus), /* for '-' */ \
299 REF (flag_hash), /* for '<hash>' */ \
300 REF (flag_zero), /* for '0' */ \
301 REF (flag_quote), /* for '\'' */ \
302 REF (width_asterics), /* for '*' */ \
303 REF (width), /* for '1'...'9' */ \
304 REF (precision), /* for '.' */ \
305 REF (mod_half), /* for 'h' */ \
306 REF (mod_long), /* for 'l' */ \
307 REF (mod_longlong), /* for 'L', 'q' */ \
308 REF (mod_size_t), /* for 'z', 'Z' */ \
309 REF (form_percent), /* for '%' */ \
310 REF (form_integer), /* for 'd', 'i' */ \
311 REF (form_unsigned), /* for 'u' */ \
312 REF (form_octal), /* for 'o' */ \
313 REF (form_hexa), /* for 'X', 'x' */ \
314 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
315 REF (form_character), /* for 'c' */ \
316 REF (form_string), /* for 's', 'S' */ \
317 REF (form_pointer), /* for 'p' */ \
318 REF (form_number), /* for 'n' */ \
319 REF (form_strerror), /* for 'm' */ \
320 REF (form_wcharacter), /* for 'C' */ \
321 REF (form_floathex), /* for 'A', 'a' */ \
322 REF (mod_ptrdiff_t), /* for 't' */ \
323 REF (mod_intmax_t), /* for 'j' */ \
324 REF (flag_i18n), /* for 'I' */ \
325 }; \
326 /* Step 1: after processing width. */ \
327 static JUMP_TABLE_TYPE step1_jumps[30] = \
329 REF (form_unknown), \
330 REF (form_unknown), /* for ' ' */ \
331 REF (form_unknown), /* for '+' */ \
332 REF (form_unknown), /* for '-' */ \
333 REF (form_unknown), /* for '<hash>' */ \
334 REF (form_unknown), /* for '0' */ \
335 REF (form_unknown), /* for '\'' */ \
336 REF (form_unknown), /* for '*' */ \
337 REF (form_unknown), /* for '1'...'9' */ \
338 REF (precision), /* for '.' */ \
339 REF (mod_half), /* for 'h' */ \
340 REF (mod_long), /* for 'l' */ \
341 REF (mod_longlong), /* for 'L', 'q' */ \
342 REF (mod_size_t), /* for 'z', 'Z' */ \
343 REF (form_percent), /* for '%' */ \
344 REF (form_integer), /* for 'd', 'i' */ \
345 REF (form_unsigned), /* for 'u' */ \
346 REF (form_octal), /* for 'o' */ \
347 REF (form_hexa), /* for 'X', 'x' */ \
348 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
349 REF (form_character), /* for 'c' */ \
350 REF (form_string), /* for 's', 'S' */ \
351 REF (form_pointer), /* for 'p' */ \
352 REF (form_number), /* for 'n' */ \
353 REF (form_strerror), /* for 'm' */ \
354 REF (form_wcharacter), /* for 'C' */ \
355 REF (form_floathex), /* for 'A', 'a' */ \
356 REF (mod_ptrdiff_t), /* for 't' */ \
357 REF (mod_intmax_t), /* for 'j' */ \
358 REF (form_unknown) /* for 'I' */ \
359 }; \
360 /* Step 2: after processing precision. */ \
361 static JUMP_TABLE_TYPE step2_jumps[30] = \
363 REF (form_unknown), \
364 REF (form_unknown), /* for ' ' */ \
365 REF (form_unknown), /* for '+' */ \
366 REF (form_unknown), /* for '-' */ \
367 REF (form_unknown), /* for '<hash>' */ \
368 REF (form_unknown), /* for '0' */ \
369 REF (form_unknown), /* for '\'' */ \
370 REF (form_unknown), /* for '*' */ \
371 REF (form_unknown), /* for '1'...'9' */ \
372 REF (form_unknown), /* for '.' */ \
373 REF (mod_half), /* for 'h' */ \
374 REF (mod_long), /* for 'l' */ \
375 REF (mod_longlong), /* for 'L', 'q' */ \
376 REF (mod_size_t), /* for 'z', 'Z' */ \
377 REF (form_percent), /* for '%' */ \
378 REF (form_integer), /* for 'd', 'i' */ \
379 REF (form_unsigned), /* for 'u' */ \
380 REF (form_octal), /* for 'o' */ \
381 REF (form_hexa), /* for 'X', 'x' */ \
382 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
383 REF (form_character), /* for 'c' */ \
384 REF (form_string), /* for 's', 'S' */ \
385 REF (form_pointer), /* for 'p' */ \
386 REF (form_number), /* for 'n' */ \
387 REF (form_strerror), /* for 'm' */ \
388 REF (form_wcharacter), /* for 'C' */ \
389 REF (form_floathex), /* for 'A', 'a' */ \
390 REF (mod_ptrdiff_t), /* for 't' */ \
391 REF (mod_intmax_t), /* for 'j' */ \
392 REF (form_unknown) /* for 'I' */ \
393 }; \
394 /* Step 3a: after processing first 'h' modifier. */ \
395 static JUMP_TABLE_TYPE step3a_jumps[30] = \
397 REF (form_unknown), \
398 REF (form_unknown), /* for ' ' */ \
399 REF (form_unknown), /* for '+' */ \
400 REF (form_unknown), /* for '-' */ \
401 REF (form_unknown), /* for '<hash>' */ \
402 REF (form_unknown), /* for '0' */ \
403 REF (form_unknown), /* for '\'' */ \
404 REF (form_unknown), /* for '*' */ \
405 REF (form_unknown), /* for '1'...'9' */ \
406 REF (form_unknown), /* for '.' */ \
407 REF (mod_halfhalf), /* for 'h' */ \
408 REF (form_unknown), /* for 'l' */ \
409 REF (form_unknown), /* for 'L', 'q' */ \
410 REF (form_unknown), /* for 'z', 'Z' */ \
411 REF (form_percent), /* for '%' */ \
412 REF (form_integer), /* for 'd', 'i' */ \
413 REF (form_unsigned), /* for 'u' */ \
414 REF (form_octal), /* for 'o' */ \
415 REF (form_hexa), /* for 'X', 'x' */ \
416 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
417 REF (form_unknown), /* for 'c' */ \
418 REF (form_unknown), /* for 's', 'S' */ \
419 REF (form_unknown), /* for 'p' */ \
420 REF (form_number), /* for 'n' */ \
421 REF (form_unknown), /* for 'm' */ \
422 REF (form_unknown), /* for 'C' */ \
423 REF (form_unknown), /* for 'A', 'a' */ \
424 REF (form_unknown), /* for 't' */ \
425 REF (form_unknown), /* for 'j' */ \
426 REF (form_unknown) /* for 'I' */ \
427 }; \
428 /* Step 3b: after processing first 'l' modifier. */ \
429 static JUMP_TABLE_TYPE step3b_jumps[30] = \
431 REF (form_unknown), \
432 REF (form_unknown), /* for ' ' */ \
433 REF (form_unknown), /* for '+' */ \
434 REF (form_unknown), /* for '-' */ \
435 REF (form_unknown), /* for '<hash>' */ \
436 REF (form_unknown), /* for '0' */ \
437 REF (form_unknown), /* for '\'' */ \
438 REF (form_unknown), /* for '*' */ \
439 REF (form_unknown), /* for '1'...'9' */ \
440 REF (form_unknown), /* for '.' */ \
441 REF (form_unknown), /* for 'h' */ \
442 REF (mod_longlong), /* for 'l' */ \
443 REF (form_unknown), /* for 'L', 'q' */ \
444 REF (form_unknown), /* for 'z', 'Z' */ \
445 REF (form_percent), /* for '%' */ \
446 REF (form_integer), /* for 'd', 'i' */ \
447 REF (form_unsigned), /* for 'u' */ \
448 REF (form_octal), /* for 'o' */ \
449 REF (form_hexa), /* for 'X', 'x' */ \
450 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
451 REF (form_character), /* for 'c' */ \
452 REF (form_string), /* for 's', 'S' */ \
453 REF (form_pointer), /* for 'p' */ \
454 REF (form_number), /* for 'n' */ \
455 REF (form_strerror), /* for 'm' */ \
456 REF (form_wcharacter), /* for 'C' */ \
457 REF (form_floathex), /* for 'A', 'a' */ \
458 REF (form_unknown), /* for 't' */ \
459 REF (form_unknown), /* for 'j' */ \
460 REF (form_unknown) /* for 'I' */ \
463 #define STEP4_TABLE \
464 /* Step 4: processing format specifier. */ \
465 static JUMP_TABLE_TYPE step4_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 (form_unknown), /* 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' */ \
500 #define process_arg(fspec) \
501 /* Start real work. We know about all flags and modifiers and \
502 now process the wanted format specifier. */ \
503 LABEL (form_percent): \
504 /* Write a literal "%". */ \
505 outchar (L_('%')); \
506 break; \
508 LABEL (form_integer): \
509 /* Signed decimal integer. */ \
510 base = 10; \
512 if (is_longlong) \
514 long long int signed_number; \
516 if (fspec == NULL) \
517 signed_number = va_arg (ap, long long int); \
518 else \
519 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
521 is_negative = signed_number < 0; \
522 number.longlong = is_negative ? (- signed_number) : signed_number; \
524 goto LABEL (longlong_number); \
526 else \
528 long int signed_number; \
530 if (fspec == NULL) \
532 if (is_long_num) \
533 signed_number = va_arg (ap, long int); \
534 else if (is_char) \
535 signed_number = (signed char) va_arg (ap, unsigned int); \
536 else if (!is_short) \
537 signed_number = va_arg (ap, int); \
538 else \
539 signed_number = (short int) va_arg (ap, unsigned int); \
541 else \
542 if (is_long_num) \
543 signed_number = args_value[fspec->data_arg].pa_long_int; \
544 else if (is_char) \
545 signed_number = (signed char) \
546 args_value[fspec->data_arg].pa_u_int; \
547 else if (!is_short) \
548 signed_number = args_value[fspec->data_arg].pa_int; \
549 else \
550 signed_number = (short int) \
551 args_value[fspec->data_arg].pa_u_int; \
553 is_negative = signed_number < 0; \
554 number.word = is_negative ? (- signed_number) : signed_number; \
556 goto LABEL (number); \
558 /* NOTREACHED */ \
560 LABEL (form_unsigned): \
561 /* Unsigned decimal integer. */ \
562 base = 10; \
563 goto LABEL (unsigned_number); \
564 /* NOTREACHED */ \
566 LABEL (form_octal): \
567 /* Unsigned octal integer. */ \
568 base = 8; \
569 goto LABEL (unsigned_number); \
570 /* NOTREACHED */ \
572 LABEL (form_hexa): \
573 /* Unsigned hexadecimal integer. */ \
574 base = 16; \
576 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
578 /* ISO specifies the `+' and ` ' flags only for signed \
579 conversions. */ \
580 is_negative = 0; \
581 showsign = 0; \
582 space = 0; \
584 if (is_longlong) \
586 if (fspec == NULL) \
587 number.longlong = va_arg (ap, unsigned long long int); \
588 else \
589 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
591 LABEL (longlong_number): \
592 if (prec < 0) \
593 /* Supply a default precision if none was given. */ \
594 prec = 1; \
595 else \
596 /* We have to take care for the '0' flag. If a precision \
597 is given it must be ignored. */ \
598 pad = L_(' '); \
600 /* If the precision is 0 and the number is 0 nothing has to \
601 be written for the number, except for the 'o' format in \
602 alternate form. */ \
603 if (prec == 0 && number.longlong == 0) \
605 string = workend; \
606 if (base == 8 && alt) \
607 *--string = L_('0'); \
609 else \
611 /* Put the number in WORK. */ \
612 string = _itoa (number.longlong, workend, base, \
613 spec == L_('X')); \
614 if (group && grouping) \
615 string = group_number (string, workend, grouping, \
616 thousands_sep); \
618 if (use_outdigits && base == 10) \
619 string = _i18n_number_rewrite (string, workend); \
621 /* Simplify further test for num != 0. */ \
622 number.word = number.longlong != 0; \
624 else \
626 if (fspec == NULL) \
628 if (is_long_num) \
629 number.word = va_arg (ap, unsigned long int); \
630 else if (is_char) \
631 number.word = (unsigned char) va_arg (ap, unsigned int); \
632 else if (!is_short) \
633 number.word = va_arg (ap, unsigned int); \
634 else \
635 number.word = (unsigned short int) va_arg (ap, unsigned int); \
637 else \
638 if (is_long_num) \
639 number.word = args_value[fspec->data_arg].pa_u_long_int; \
640 else if (is_char) \
641 number.word = (unsigned char) \
642 args_value[fspec->data_arg].pa_u_int; \
643 else if (!is_short) \
644 number.word = args_value[fspec->data_arg].pa_u_int; \
645 else \
646 number.word = (unsigned short int) \
647 args_value[fspec->data_arg].pa_u_int; \
649 LABEL (number): \
650 if (prec < 0) \
651 /* Supply a default precision if none was given. */ \
652 prec = 1; \
653 else \
654 /* We have to take care for the '0' flag. If a precision \
655 is given it must be ignored. */ \
656 pad = L_(' '); \
658 /* If the precision is 0 and the number is 0 nothing has to \
659 be written for the number, except for the 'o' format in \
660 alternate form. */ \
661 if (prec == 0 && number.word == 0) \
663 string = workend; \
664 if (base == 8 && alt) \
665 *--string = L_('0'); \
667 else \
669 /* Put the number in WORK. */ \
670 string = _itoa_word (number.word, workend, base, \
671 spec == L_('X')); \
672 if (group && grouping) \
673 string = group_number (string, workend, grouping, \
674 thousands_sep); \
676 if (use_outdigits && base == 10) \
677 string = _i18n_number_rewrite (string, workend); \
681 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
682 /* Add octal marker. */ \
683 *--string = L_('0'); \
685 prec = MAX (0, prec - (workend - string)); \
687 if (!left) \
689 width -= workend - string + prec; \
691 if (number.word != 0 && alt && base == 16) \
692 /* Account for 0X hex marker. */ \
693 width -= 2; \
695 if (is_negative || showsign || space) \
696 --width; \
698 if (pad == L_(' ')) \
700 PAD (L_(' ')); \
701 width = 0; \
704 if (is_negative) \
705 outchar (L_('-')); \
706 else if (showsign) \
707 outchar (L_('+')); \
708 else if (space) \
709 outchar (L_(' ')); \
711 if (number.word != 0 && alt && base == 16) \
713 outchar (L_('0')); \
714 outchar (spec); \
717 width += prec; \
718 PAD (L_('0')); \
720 outstring (string, workend - string); \
722 break; \
724 else \
726 if (is_negative) \
728 outchar (L_('-')); \
729 --width; \
731 else if (showsign) \
733 outchar (L_('+')); \
734 --width; \
736 else if (space) \
738 outchar (L_(' ')); \
739 --width; \
742 if (number.word != 0 && alt && base == 16) \
744 outchar (L_('0')); \
745 outchar (spec); \
746 width -= 2; \
749 width -= workend - string + prec; \
751 if (prec > 0) \
753 int temp = width; \
754 width = prec; \
755 PAD (L_('0'));; \
756 width = temp; \
759 outstring (string, workend - string); \
761 PAD (L_(' ')); \
762 break; \
765 LABEL (form_float): \
767 /* Floating-point number. This is handled by printf_fp.c. */ \
768 const void *ptr; \
769 int function_done; \
771 if (fspec == NULL) \
773 if (__ldbl_is_dbl) \
774 is_long_double = 0; \
776 struct printf_info info = { .prec = prec, \
777 .width = width, \
778 .spec = spec, \
779 .is_long_double = is_long_double, \
780 .is_short = is_short, \
781 .is_long = is_long, \
782 .alt = alt, \
783 .space = space, \
784 .left = left, \
785 .showsign = showsign, \
786 .group = group, \
787 .pad = pad, \
788 .extra = 0, \
789 .i18n = use_outdigits, \
790 .wide = sizeof (CHAR_T) != 1 }; \
792 if (is_long_double) \
793 the_arg.pa_long_double = va_arg (ap, long double); \
794 else \
795 the_arg.pa_double = va_arg (ap, double); \
796 ptr = (const void *) &the_arg; \
798 function_done = __printf_fp (s, &info, &ptr); \
800 else \
802 ptr = (const void *) &args_value[fspec->data_arg]; \
803 if (__ldbl_is_dbl) \
805 fspec->data_arg_type = PA_DOUBLE; \
806 fspec->info.is_long_double = 0; \
809 function_done = __printf_fp (s, &fspec->info, &ptr); \
812 if (function_done < 0) \
814 /* Error in print handler. */ \
815 done = -1; \
816 goto all_done; \
819 done += function_done; \
821 break; \
823 LABEL (form_floathex): \
825 /* Floating point number printed as hexadecimal number. */ \
826 const void *ptr; \
827 int function_done; \
829 if (fspec == NULL) \
831 if (__ldbl_is_dbl) \
832 is_long_double = 0; \
834 struct printf_info info = { .prec = prec, \
835 .width = width, \
836 .spec = spec, \
837 .is_long_double = is_long_double, \
838 .is_short = is_short, \
839 .is_long = is_long, \
840 .alt = alt, \
841 .space = space, \
842 .left = left, \
843 .showsign = showsign, \
844 .group = group, \
845 .pad = pad, \
846 .extra = 0, \
847 .wide = sizeof (CHAR_T) != 1 }; \
849 if (is_long_double) \
850 the_arg.pa_long_double = va_arg (ap, long double); \
851 else \
852 the_arg.pa_double = va_arg (ap, double); \
853 ptr = (const void *) &the_arg; \
855 function_done = __printf_fphex (s, &info, &ptr); \
857 else \
859 ptr = (const void *) &args_value[fspec->data_arg]; \
860 if (__ldbl_is_dbl) \
861 fspec->info.is_long_double = 0; \
863 function_done = __printf_fphex (s, &fspec->info, &ptr); \
866 if (function_done < 0) \
868 /* Error in print handler. */ \
869 done = -1; \
870 goto all_done; \
873 done += function_done; \
875 break; \
877 LABEL (form_pointer): \
878 /* Generic pointer. */ \
880 const void *ptr; \
881 if (fspec == NULL) \
882 ptr = va_arg (ap, void *); \
883 else \
884 ptr = args_value[fspec->data_arg].pa_pointer; \
885 if (ptr != NULL) \
887 /* If the pointer is not NULL, write it as a %#x spec. */ \
888 base = 16; \
889 number.word = (unsigned long int) ptr; \
890 is_negative = 0; \
891 alt = 1; \
892 group = 0; \
893 spec = L_('x'); \
894 goto LABEL (number); \
896 else \
898 /* Write "(nil)" for a nil pointer. */ \
899 string = (CHAR_T *) L_("(nil)"); \
900 /* Make sure the full string "(nil)" is printed. */ \
901 if (prec < 5) \
902 prec = 5; \
903 is_long = 0; /* This is no wide-char string. */ \
904 goto LABEL (print_string); \
907 /* NOTREACHED */ \
909 LABEL (form_number): \
910 if (s->_flags2 & _IO_FLAGS2_FORTIFY) \
912 if (! readonly_format) \
914 extern int __readonly_area (const void *, size_t) \
915 attribute_hidden; \
916 readonly_format \
917 = __readonly_area (format, ((STR_LEN (format) + 1) \
918 * sizeof (CHAR_T))); \
920 if (readonly_format < 0) \
921 __libc_fatal ("*** %n in writable segment detected ***\n"); \
923 /* Answer the count of characters written. */ \
924 if (fspec == NULL) \
926 if (is_longlong) \
927 *(long long int *) va_arg (ap, void *) = done; \
928 else if (is_long_num) \
929 *(long int *) va_arg (ap, void *) = done; \
930 else if (is_char) \
931 *(char *) va_arg (ap, void *) = done; \
932 else if (!is_short) \
933 *(int *) va_arg (ap, void *) = done; \
934 else \
935 *(short int *) va_arg (ap, void *) = done; \
937 else \
938 if (is_longlong) \
939 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
940 else if (is_long_num) \
941 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
942 else if (is_char) \
943 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
944 else if (!is_short) \
945 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
946 else \
947 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
948 break; \
950 LABEL (form_strerror): \
951 /* Print description of error ERRNO. */ \
952 string = \
953 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
954 sizeof work_buffer); \
955 is_long = 0; /* This is no wide-char string. */ \
956 goto LABEL (print_string)
958 #ifdef COMPILE_WPRINTF
959 # define process_string_arg(fspec) \
960 LABEL (form_character): \
961 /* Character. */ \
962 if (is_long) \
963 goto LABEL (form_wcharacter); \
964 --width; /* Account for the character itself. */ \
965 if (!left) \
966 PAD (L' '); \
967 if (fspec == NULL) \
968 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
969 else \
970 outchar (__btowc ((unsigned char) \
971 args_value[fspec->data_arg].pa_int)); \
972 if (left) \
973 PAD (L' '); \
974 break; \
976 LABEL (form_wcharacter): \
978 /* Wide character. */ \
979 --width; \
980 if (!left) \
981 PAD (L' '); \
982 if (fspec == NULL) \
983 outchar (va_arg (ap, wchar_t)); \
984 else \
985 outchar (args_value[fspec->data_arg].pa_wchar); \
986 if (left) \
987 PAD (L' '); \
989 break; \
991 LABEL (form_string): \
993 size_t len; \
994 int string_malloced; \
996 /* The string argument could in fact be `char *' or `wchar_t *'. \
997 But this should not make a difference here. */ \
998 if (fspec == NULL) \
999 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
1000 else \
1001 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
1003 /* Entry point for printing other strings. */ \
1004 LABEL (print_string): \
1006 string_malloced = 0; \
1007 if (string == NULL) \
1009 /* Write "(null)" if there's space. */ \
1010 if (prec == -1 \
1011 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
1013 string = (CHAR_T *) null; \
1014 len = (sizeof (null) / sizeof (null[0])) - 1; \
1016 else \
1018 string = (CHAR_T *) L""; \
1019 len = 0; \
1022 else if (!is_long && spec != L_('S')) \
1024 /* This is complicated. We have to transform the multibyte \
1025 string into a wide character string. */ \
1026 const char *mbs = (const char *) string; \
1027 mbstate_t mbstate; \
1029 len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
1031 /* Allocate dynamically an array which definitely is long \
1032 enough for the wide character version. Each byte in the \
1033 multi-byte string can produce at most one wide character. */ \
1034 if (__libc_use_alloca (len * sizeof (wchar_t))) \
1035 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1036 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1037 == NULL) \
1039 done = -1; \
1040 goto all_done; \
1042 else \
1043 string_malloced = 1; \
1045 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1046 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1047 if (len == (size_t) -1) \
1049 /* Illegal multibyte character. */ \
1050 done = -1; \
1051 goto all_done; \
1054 else \
1056 if (prec != -1) \
1057 /* Search for the end of the string, but don't search past \
1058 the length specified by the precision. */ \
1059 len = __wcsnlen (string, prec); \
1060 else \
1061 len = __wcslen (string); \
1064 if ((width -= len) < 0) \
1066 outstring (string, len); \
1067 break; \
1070 if (!left) \
1071 PAD (L' '); \
1072 outstring (string, len); \
1073 if (left) \
1074 PAD (L' '); \
1075 if (__builtin_expect (string_malloced, 0)) \
1076 free (string); \
1078 break;
1079 #else
1080 # define process_string_arg(fspec) \
1081 LABEL (form_character): \
1082 /* Character. */ \
1083 if (is_long) \
1084 goto LABEL (form_wcharacter); \
1085 --width; /* Account for the character itself. */ \
1086 if (!left) \
1087 PAD (' '); \
1088 if (fspec == NULL) \
1089 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1090 else \
1091 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1092 if (left) \
1093 PAD (' '); \
1094 break; \
1096 LABEL (form_wcharacter): \
1098 /* Wide character. */ \
1099 char buf[MB_CUR_MAX]; \
1100 mbstate_t mbstate; \
1101 size_t len; \
1103 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1104 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1105 : args_value[fspec->data_arg].pa_wchar), \
1106 &mbstate); \
1107 if (len == (size_t) -1) \
1109 /* Something went wron gduring the conversion. Bail out. */ \
1110 done = -1; \
1111 goto all_done; \
1113 width -= len; \
1114 if (!left) \
1115 PAD (' '); \
1116 outstring (buf, len); \
1117 if (left) \
1118 PAD (' '); \
1120 break; \
1122 LABEL (form_string): \
1124 size_t len; \
1125 int string_malloced; \
1127 /* The string argument could in fact be `char *' or `wchar_t *'. \
1128 But this should not make a difference here. */ \
1129 if (fspec == NULL) \
1130 string = (char *) va_arg (ap, const char *); \
1131 else \
1132 string = (char *) args_value[fspec->data_arg].pa_string; \
1134 /* Entry point for printing other strings. */ \
1135 LABEL (print_string): \
1137 string_malloced = 0; \
1138 if (string == NULL) \
1140 /* Write "(null)" if there's space. */ \
1141 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1143 string = (char *) null; \
1144 len = sizeof (null) - 1; \
1146 else \
1148 string = (char *) ""; \
1149 len = 0; \
1152 else if (!is_long && spec != L_('S')) \
1154 if (prec != -1) \
1156 /* Search for the end of the string, but don't search past \
1157 the length (in bytes) specified by the precision. Also \
1158 don't use incomplete characters. */ \
1159 if (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MB_CUR_MAX) == 1) \
1160 len = __strnlen (string, prec); \
1161 else \
1163 /* In case we have a multibyte character set the \
1164 situation is more complicated. We must not copy \
1165 bytes at the end which form an incomplete character. */\
1166 size_t ignore_size = (unsigned) prec > 1024 ? 1024 : prec;\
1167 wchar_t ignore[ignore_size]; \
1168 const char *str2 = string; \
1169 const char *strend = string + prec; \
1170 if (strend < string) \
1171 strend = (const char *) UINTPTR_MAX; \
1173 mbstate_t ps; \
1174 memset (&ps, '\0', sizeof (ps)); \
1176 while (str2 != NULL && str2 < strend) \
1177 if (__mbsnrtowcs (ignore, &str2, strend - str2, \
1178 ignore_size, &ps) == (size_t) -1) \
1180 done = -1; \
1181 goto all_done; \
1184 if (str2 == NULL) \
1185 len = strlen (string); \
1186 else \
1187 len = str2 - string - (ps.__count & 7); \
1190 else \
1191 len = strlen (string); \
1193 else \
1195 const wchar_t *s2 = (const wchar_t *) string; \
1196 mbstate_t mbstate; \
1198 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1200 if (prec >= 0) \
1202 /* The string `s2' might not be NUL terminated. */ \
1203 if (__libc_use_alloca (prec)) \
1204 string = (char *) alloca (prec); \
1205 else if ((string = (char *) malloc (prec)) == NULL) \
1207 done = -1; \
1208 goto all_done; \
1210 else \
1211 string_malloced = 1; \
1212 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1214 else \
1216 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1217 if (len != (size_t) -1) \
1219 assert (__mbsinit (&mbstate)); \
1220 s2 = (const wchar_t *) string; \
1221 if (__libc_use_alloca (len + 1)) \
1222 string = (char *) alloca (len + 1); \
1223 else if ((string = (char *) malloc (len + 1)) == NULL) \
1225 done = -1; \
1226 goto all_done; \
1228 else \
1229 string_malloced = 1; \
1230 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1234 if (len == (size_t) -1) \
1236 /* Illegal wide-character string. */ \
1237 done = -1; \
1238 goto all_done; \
1242 if ((width -= len) < 0) \
1244 outstring (string, len); \
1245 break; \
1248 if (!left) \
1249 PAD (' '); \
1250 outstring (string, len); \
1251 if (left) \
1252 PAD (' '); \
1253 if (__builtin_expect (string_malloced, 0)) \
1254 free (string); \
1256 break;
1257 #endif
1259 /* Orient the stream. */
1260 #ifdef ORIENT
1261 ORIENT;
1262 #endif
1264 /* Sanity check of arguments. */
1265 ARGCHECK (s, format);
1267 #ifdef ORIENT
1268 /* Check for correct orientation. */
1269 if (_IO_vtable_offset (s) == 0 &&
1270 _IO_fwide (s, sizeof (CHAR_T) == 1 ? -1 : 1)
1271 != (sizeof (CHAR_T) == 1 ? -1 : 1))
1272 /* The stream is already oriented otherwise. */
1273 return EOF;
1274 #endif
1276 if (UNBUFFERED_P (s))
1277 /* Use a helper function which will allocate a local temporary buffer
1278 for the stream and then call us again. */
1279 return buffered_vfprintf (s, format, ap);
1281 /* Initialize local variables. */
1282 done = 0;
1283 grouping = (const char *) -1;
1284 #ifdef __va_copy
1285 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1286 since on some systems `va_list' is not an integral type. */
1287 __va_copy (ap_save, ap);
1288 #else
1289 ap_save = ap;
1290 #endif
1291 nspecs_done = 0;
1293 #ifdef COMPILE_WPRINTF
1294 /* Find the first format specifier. */
1295 f = lead_str_end = __find_specwc ((const UCHAR_T *) format);
1296 #else
1297 /* Put state for processing format string in initial state. */
1298 memset (&mbstate, '\0', sizeof (mbstate_t));
1300 /* Find the first format specifier. */
1301 f = lead_str_end = __find_specmb (format, &mbstate);
1302 #endif
1304 /* Lock stream. */
1305 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
1306 _IO_flockfile (s);
1308 /* Write the literal text before the first format. */
1309 outstring ((const UCHAR_T *) format,
1310 lead_str_end - (const UCHAR_T *) format);
1312 /* If we only have to print a simple string, return now. */
1313 if (*f == L_('\0'))
1314 goto all_done;
1316 /* Process whole format string. */
1319 #ifdef SHARED
1320 # define REF(Name) &&do_##Name - &&do_form_unknown
1321 #else
1322 # define REF(Name) &&do_##Name
1323 #endif
1324 #define LABEL(Name) do_##Name
1325 STEP0_3_TABLE;
1326 STEP4_TABLE;
1328 union printf_arg *args_value; /* This is not used here but ... */
1329 int is_negative; /* Flag for negative number. */
1330 union
1332 unsigned long long int longlong;
1333 unsigned long int word;
1334 } number;
1335 int base;
1336 union printf_arg the_arg;
1337 CHAR_T *string; /* Pointer to argument string. */
1338 int alt = 0; /* Alternate format. */
1339 int space = 0; /* Use space prefix if no sign is needed. */
1340 int left = 0; /* Left-justify output. */
1341 int showsign = 0; /* Always begin with plus or minus sign. */
1342 int group = 0; /* Print numbers according grouping rules. */
1343 int is_long_double = 0; /* Argument is long double/ long long int. */
1344 int is_short = 0; /* Argument is short int. */
1345 int is_long = 0; /* Argument is long int. */
1346 int is_char = 0; /* Argument is promoted (unsigned) char. */
1347 int width = 0; /* Width of output; 0 means none specified. */
1348 int prec = -1; /* Precision of output; -1 means none specified. */
1349 /* This flag is set by the 'I' modifier and selects the use of the
1350 `outdigits' as determined by the current locale. */
1351 int use_outdigits = 0;
1352 UCHAR_T pad = L_(' ');/* Padding character. */
1353 CHAR_T spec;
1355 workstart = NULL;
1356 workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1358 /* Get current character in format string. */
1359 JUMP (*++f, step0_jumps);
1361 /* ' ' flag. */
1362 LABEL (flag_space):
1363 space = 1;
1364 JUMP (*++f, step0_jumps);
1366 /* '+' flag. */
1367 LABEL (flag_plus):
1368 showsign = 1;
1369 JUMP (*++f, step0_jumps);
1371 /* The '-' flag. */
1372 LABEL (flag_minus):
1373 left = 1;
1374 pad = L_(' ');
1375 JUMP (*++f, step0_jumps);
1377 /* The '#' flag. */
1378 LABEL (flag_hash):
1379 alt = 1;
1380 JUMP (*++f, step0_jumps);
1382 /* The '0' flag. */
1383 LABEL (flag_zero):
1384 if (!left)
1385 pad = L_('0');
1386 JUMP (*++f, step0_jumps);
1388 /* The '\'' flag. */
1389 LABEL (flag_quote):
1390 group = 1;
1392 if (grouping == (const char *) -1)
1394 #ifdef COMPILE_WPRINTF
1395 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1396 _NL_NUMERIC_THOUSANDS_SEP_WC);
1397 #else
1398 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1399 #endif
1401 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1402 if (*grouping == '\0' || *grouping == CHAR_MAX
1403 #ifdef COMPILE_WPRINTF
1404 || thousands_sep == L'\0'
1405 #else
1406 || *thousands_sep == '\0'
1407 #endif
1409 grouping = NULL;
1411 JUMP (*++f, step0_jumps);
1413 LABEL (flag_i18n):
1414 use_outdigits = 1;
1415 JUMP (*++f, step0_jumps);
1417 /* Get width from argument. */
1418 LABEL (width_asterics):
1420 const UCHAR_T *tmp; /* Temporary value. */
1422 tmp = ++f;
1423 if (ISDIGIT (*tmp) && read_int (&tmp) && *tmp == L_('$'))
1424 /* The width comes from a positional parameter. */
1425 goto do_positional;
1427 width = va_arg (ap, int);
1429 /* Negative width means left justified. */
1430 if (width < 0)
1432 width = -width;
1433 pad = L_(' ');
1434 left = 1;
1437 if (width + 32 >= (int) (sizeof (work_buffer)
1438 / sizeof (work_buffer[0])))
1440 /* We have to use a special buffer. The "32" is just a safe
1441 bet for all the output which is not counted in the width. */
1442 if (__libc_use_alloca ((width + 32) * sizeof (CHAR_T)))
1443 workend = ((CHAR_T *) alloca ((width + 32) * sizeof (CHAR_T))
1444 + (width + 32));
1445 else
1447 workstart = (CHAR_T *) malloc ((width + 32) * sizeof (CHAR_T));
1448 if (workstart == NULL)
1450 done = -1;
1451 goto all_done;
1453 workend = workstart + (width + 32);
1457 JUMP (*f, step1_jumps);
1459 /* Given width in format string. */
1460 LABEL (width):
1461 width = read_int (&f);
1463 if (width + 32 >= (int) (sizeof (work_buffer) / sizeof (work_buffer[0])))
1465 /* We have to use a special buffer. The "32" is just a safe
1466 bet for all the output which is not counted in the width. */
1467 if (__libc_use_alloca ((width + 32) * sizeof (CHAR_T)))
1468 workend = ((CHAR_T *) alloca ((width + 32) * sizeof (CHAR_T))
1469 + (width + 32));
1470 else
1472 workstart = (CHAR_T *) malloc ((width + 32) * sizeof (CHAR_T));
1473 if (workstart == NULL)
1475 done = -1;
1476 goto all_done;
1478 workend = workstart + (width + 32);
1481 if (*f == L_('$'))
1482 /* Oh, oh. The argument comes from a positional parameter. */
1483 goto do_positional;
1484 JUMP (*f, step1_jumps);
1486 LABEL (precision):
1487 ++f;
1488 if (*f == L_('*'))
1490 const UCHAR_T *tmp; /* Temporary value. */
1492 tmp = ++f;
1493 if (ISDIGIT (*tmp) && read_int (&tmp) > 0 && *tmp == L_('$'))
1494 /* The precision comes from a positional parameter. */
1495 goto do_positional;
1497 prec = va_arg (ap, int);
1499 /* If the precision is negative the precision is omitted. */
1500 if (prec < 0)
1501 prec = -1;
1503 else if (ISDIGIT (*f))
1504 prec = read_int (&f);
1505 else
1506 prec = 0;
1507 if (prec > width
1508 && prec + 32 > (int)(sizeof (work_buffer) / sizeof (work_buffer[0])))
1510 if (__libc_use_alloca ((prec + 32) * sizeof (CHAR_T)))
1511 workend = ((CHAR_T *) alloca ((prec + 32) * sizeof (CHAR_T)))
1512 + (prec + 32);
1513 else
1515 workstart = (CHAR_T *) malloc ((prec + 32) * sizeof (CHAR_T));
1516 if (workstart == NULL)
1518 done = -1;
1519 goto all_done;
1521 workend = workstart + (prec + 32);
1524 JUMP (*f, step2_jumps);
1526 /* Process 'h' modifier. There might another 'h' following. */
1527 LABEL (mod_half):
1528 is_short = 1;
1529 JUMP (*++f, step3a_jumps);
1531 /* Process 'hh' modifier. */
1532 LABEL (mod_halfhalf):
1533 is_short = 0;
1534 is_char = 1;
1535 JUMP (*++f, step4_jumps);
1537 /* Process 'l' modifier. There might another 'l' following. */
1538 LABEL (mod_long):
1539 is_long = 1;
1540 JUMP (*++f, step3b_jumps);
1542 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1543 allowed to follow. */
1544 LABEL (mod_longlong):
1545 is_long_double = 1;
1546 is_long = 1;
1547 JUMP (*++f, step4_jumps);
1549 LABEL (mod_size_t):
1550 is_long_double = sizeof (size_t) > sizeof (unsigned long int);
1551 is_long = sizeof (size_t) > sizeof (unsigned int);
1552 JUMP (*++f, step4_jumps);
1554 LABEL (mod_ptrdiff_t):
1555 is_long_double = sizeof (ptrdiff_t) > sizeof (unsigned long int);
1556 is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
1557 JUMP (*++f, step4_jumps);
1559 LABEL (mod_intmax_t):
1560 is_long_double = sizeof (intmax_t) > sizeof (unsigned long int);
1561 is_long = sizeof (intmax_t) > sizeof (unsigned int);
1562 JUMP (*++f, step4_jumps);
1564 /* Process current format. */
1565 while (1)
1567 process_arg (((struct printf_spec *) NULL));
1568 process_string_arg (((struct printf_spec *) NULL));
1570 LABEL (form_unknown):
1571 if (spec == L_('\0'))
1573 /* The format string ended before the specifier is complete. */
1574 done = -1;
1575 goto all_done;
1578 /* If we are in the fast loop force entering the complicated
1579 one. */
1580 goto do_positional;
1583 /* The format is correctly handled. */
1584 ++nspecs_done;
1586 if (__builtin_expect (workstart != NULL, 0))
1587 free (workstart);
1588 workstart = NULL;
1590 /* Look for next format specifier. */
1591 #ifdef COMPILE_WPRINTF
1592 f = __find_specwc ((end_of_spec = ++f));
1593 #else
1594 f = __find_specmb ((end_of_spec = ++f), &mbstate);
1595 #endif
1597 /* Write the following constant string. */
1598 outstring (end_of_spec, f - end_of_spec);
1600 while (*f != L_('\0'));
1602 /* Unlock stream and return. */
1603 goto all_done;
1605 /* Here starts the more complex loop to handle positional parameters. */
1606 do_positional:
1608 /* Array with information about the needed arguments. This has to
1609 be dynamically extensible. */
1610 size_t nspecs = 0;
1611 size_t nspecs_max = 32; /* A more or less arbitrary start value. */
1612 struct printf_spec *specs
1613 = alloca (nspecs_max * sizeof (struct printf_spec));
1615 /* The number of arguments the format string requests. This will
1616 determine the size of the array needed to store the argument
1617 attributes. */
1618 size_t nargs = 0;
1619 int *args_type;
1620 union printf_arg *args_value = NULL;
1622 /* Positional parameters refer to arguments directly. This could
1623 also determine the maximum number of arguments. Track the
1624 maximum number. */
1625 size_t max_ref_arg = 0;
1627 /* Just a counter. */
1628 size_t cnt;
1630 free (workstart);
1631 workstart = NULL;
1633 if (grouping == (const char *) -1)
1635 #ifdef COMPILE_WPRINTF
1636 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1637 _NL_NUMERIC_THOUSANDS_SEP_WC);
1638 #else
1639 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1640 #endif
1642 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1643 if (*grouping == '\0' || *grouping == CHAR_MAX)
1644 grouping = NULL;
1647 for (f = lead_str_end; *f != L_('\0'); f = specs[nspecs++].next_fmt)
1649 if (nspecs >= nspecs_max)
1651 /* Extend the array of format specifiers. */
1652 struct printf_spec *old = specs;
1654 nspecs_max *= 2;
1655 specs = alloca (nspecs_max * sizeof (struct printf_spec));
1657 if (specs == &old[nspecs])
1658 /* Stack grows up, OLD was the last thing allocated;
1659 extend it. */
1660 nspecs_max += nspecs_max / 2;
1661 else
1663 /* Copy the old array's elements to the new space. */
1664 memcpy (specs, old, nspecs * sizeof (struct printf_spec));
1665 if (old == &specs[nspecs])
1666 /* Stack grows down, OLD was just below the new
1667 SPECS. We can use that space when the new space
1668 runs out. */
1669 nspecs_max += nspecs_max / 2;
1673 /* Parse the format specifier. */
1674 #ifdef COMPILE_WPRINTF
1675 nargs += __parse_one_specwc (f, nargs, &specs[nspecs], &max_ref_arg);
1676 #else
1677 nargs += __parse_one_specmb (f, nargs, &specs[nspecs], &max_ref_arg,
1678 &mbstate);
1679 #endif
1682 /* Determine the number of arguments the format string consumes. */
1683 nargs = MAX (nargs, max_ref_arg);
1685 /* Allocate memory for the argument descriptions. */
1686 args_type = alloca (nargs * sizeof (int));
1687 memset (args_type, s->_flags2 & _IO_FLAGS2_FORTIFY ? '\xff' : '\0',
1688 nargs * sizeof (int));
1689 args_value = alloca (nargs * sizeof (union printf_arg));
1691 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1692 still zero after this loop, format is invalid. For now we
1693 simply use 0 as the value. */
1695 /* Fill in the types of all the arguments. */
1696 for (cnt = 0; cnt < nspecs; ++cnt)
1698 /* If the width is determined by an argument this is an int. */
1699 if (specs[cnt].width_arg != -1)
1700 args_type[specs[cnt].width_arg] = PA_INT;
1702 /* If the precision is determined by an argument this is an int. */
1703 if (specs[cnt].prec_arg != -1)
1704 args_type[specs[cnt].prec_arg] = PA_INT;
1706 switch (specs[cnt].ndata_args)
1708 case 0: /* No arguments. */
1709 break;
1710 case 1: /* One argument; we already have the type. */
1711 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1712 break;
1713 default:
1714 /* We have more than one argument for this format spec.
1715 We must call the arginfo function again to determine
1716 all the types. */
1717 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1718 (&specs[cnt].info,
1719 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg]);
1720 break;
1724 /* Now we know all the types and the order. Fill in the argument
1725 values. */
1726 for (cnt = 0; cnt < nargs; ++cnt)
1727 switch (args_type[cnt])
1729 #define T(tag, mem, type) \
1730 case tag: \
1731 args_value[cnt].mem = va_arg (ap_save, type); \
1732 break
1734 T (PA_CHAR, pa_int, int); /* Promoted. */
1735 T (PA_WCHAR, pa_wchar, wint_t);
1736 T (PA_INT|PA_FLAG_SHORT, pa_int, int); /* Promoted. */
1737 T (PA_INT, pa_int, int);
1738 T (PA_INT|PA_FLAG_LONG, pa_long_int, long int);
1739 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1740 T (PA_FLOAT, pa_double, double); /* Promoted. */
1741 T (PA_DOUBLE, pa_double, double);
1742 case PA_DOUBLE|PA_FLAG_LONG_DOUBLE:
1743 if (__ldbl_is_dbl)
1745 args_value[cnt].pa_double = va_arg (ap_save, double);
1746 args_type[cnt] &= ~PA_FLAG_LONG_DOUBLE;
1748 else
1749 args_value[cnt].pa_long_double = va_arg (ap_save, long double);
1750 break;
1751 T (PA_STRING, pa_string, const char *);
1752 T (PA_WSTRING, pa_wstring, const wchar_t *);
1753 T (PA_POINTER, pa_pointer, void *);
1754 #undef T
1755 default:
1756 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1757 args_value[cnt].pa_pointer = va_arg (ap_save, void *);
1758 else
1759 args_value[cnt].pa_long_double = 0.0;
1760 break;
1761 case -1:
1762 /* Error case. Not all parameters appear in N$ format
1763 strings. We have no way to determine their type. */
1764 assert (s->_flags2 & _IO_FLAGS2_FORTIFY);
1765 __libc_fatal ("*** invalid %N$ use detected ***\n");
1768 /* Now walk through all format specifiers and process them. */
1769 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1771 #undef REF
1772 #ifdef SHARED
1773 # define REF(Name) &&do2_##Name - &&do_form_unknown
1774 #else
1775 # define REF(Name) &&do2_##Name
1776 #endif
1777 #undef LABEL
1778 #define LABEL(Name) do2_##Name
1779 STEP4_TABLE;
1781 int is_negative;
1782 union
1784 unsigned long long int longlong;
1785 unsigned long int word;
1786 } number;
1787 int base;
1788 union printf_arg the_arg;
1789 CHAR_T *string; /* Pointer to argument string. */
1791 /* Fill variables from values in struct. */
1792 int alt = specs[nspecs_done].info.alt;
1793 int space = specs[nspecs_done].info.space;
1794 int left = specs[nspecs_done].info.left;
1795 int showsign = specs[nspecs_done].info.showsign;
1796 int group = specs[nspecs_done].info.group;
1797 int is_long_double = specs[nspecs_done].info.is_long_double;
1798 int is_short = specs[nspecs_done].info.is_short;
1799 int is_char = specs[nspecs_done].info.is_char;
1800 int is_long = specs[nspecs_done].info.is_long;
1801 int width = specs[nspecs_done].info.width;
1802 int prec = specs[nspecs_done].info.prec;
1803 int use_outdigits = specs[nspecs_done].info.i18n;
1804 char pad = specs[nspecs_done].info.pad;
1805 CHAR_T spec = specs[nspecs_done].info.spec;
1807 workstart = NULL;
1808 workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1810 /* Fill in last information. */
1811 if (specs[nspecs_done].width_arg != -1)
1813 /* Extract the field width from an argument. */
1814 specs[nspecs_done].info.width =
1815 args_value[specs[nspecs_done].width_arg].pa_int;
1817 if (specs[nspecs_done].info.width < 0)
1818 /* If the width value is negative left justification is
1819 selected and the value is taken as being positive. */
1821 specs[nspecs_done].info.width *= -1;
1822 left = specs[nspecs_done].info.left = 1;
1824 width = specs[nspecs_done].info.width;
1827 if (specs[nspecs_done].prec_arg != -1)
1829 /* Extract the precision from an argument. */
1830 specs[nspecs_done].info.prec =
1831 args_value[specs[nspecs_done].prec_arg].pa_int;
1833 if (specs[nspecs_done].info.prec < 0)
1834 /* If the precision is negative the precision is
1835 omitted. */
1836 specs[nspecs_done].info.prec = -1;
1838 prec = specs[nspecs_done].info.prec;
1841 /* Maybe the buffer is too small. */
1842 if (MAX (prec, width) + 32 > (int) (sizeof (work_buffer)
1843 / sizeof (CHAR_T)))
1845 if (__libc_use_alloca ((MAX (prec, width) + 32)
1846 * sizeof (CHAR_T)))
1847 workend = ((CHAR_T *) alloca ((MAX (prec, width) + 32)
1848 * sizeof (CHAR_T))
1849 + (MAX (prec, width) + 32));
1850 else
1852 workstart = (CHAR_T *) malloc ((MAX (prec, width) + 32)
1853 * sizeof (CHAR_T));
1854 workend = workstart + (MAX (prec, width) + 32);
1858 /* Process format specifiers. */
1859 while (1)
1861 JUMP (spec, step4_jumps);
1863 process_arg ((&specs[nspecs_done]));
1864 process_string_arg ((&specs[nspecs_done]));
1866 LABEL (form_unknown):
1868 extern printf_function **__printf_function_table;
1869 int function_done;
1870 printf_function *function;
1871 unsigned int i;
1872 const void **ptr;
1874 function =
1875 (__printf_function_table == NULL ? NULL :
1876 __printf_function_table[specs[nspecs_done].info.spec]);
1878 if (function == NULL)
1879 function = &printf_unknown;
1881 ptr = alloca (specs[nspecs_done].ndata_args
1882 * sizeof (const void *));
1884 /* Fill in an array of pointers to the argument values. */
1885 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
1886 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1888 /* Call the function. */
1889 function_done = (*function) (s, &specs[nspecs_done].info, ptr);
1891 /* If an error occurred we don't have information about #
1892 of chars. */
1893 if (function_done < 0)
1895 done = -1;
1896 goto all_done;
1899 done += function_done;
1901 break;
1904 free (workstart);
1905 workstart = NULL;
1907 /* Write the following constant string. */
1908 outstring (specs[nspecs_done].end_of_fmt,
1909 specs[nspecs_done].next_fmt
1910 - specs[nspecs_done].end_of_fmt);
1914 all_done:
1915 if (__builtin_expect (workstart != NULL, 0))
1916 free (workstart);
1917 /* Unlock the stream. */
1918 _IO_funlockfile (s);
1919 _IO_cleanup_region_end (0);
1921 return done;
1924 /* Handle an unknown format specifier. This prints out a canonicalized
1925 representation of the format spec itself. */
1926 static int
1927 printf_unknown (FILE *s, const struct printf_info *info,
1928 const void *const *args)
1931 int done = 0;
1932 CHAR_T work_buffer[MAX (sizeof (info->width), sizeof (info->prec)) * 3];
1933 CHAR_T *const workend
1934 = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1935 register CHAR_T *w;
1937 outchar (L_('%'));
1939 if (info->alt)
1940 outchar (L_('#'));
1941 if (info->group)
1942 outchar (L_('\''));
1943 if (info->showsign)
1944 outchar (L_('+'));
1945 else if (info->space)
1946 outchar (L_(' '));
1947 if (info->left)
1948 outchar (L_('-'));
1949 if (info->pad == L_('0'))
1950 outchar (L_('0'));
1951 if (info->i18n)
1952 outchar (L_('I'));
1954 if (info->width != 0)
1956 w = _itoa_word (info->width, workend, 10, 0);
1957 while (w < workend)
1958 outchar (*w++);
1961 if (info->prec != -1)
1963 outchar (L_('.'));
1964 w = _itoa_word (info->prec, workend, 10, 0);
1965 while (w < workend)
1966 outchar (*w++);
1969 if (info->spec != L_('\0'))
1970 outchar (info->spec);
1972 all_done:
1973 return done;
1976 /* Group the digits according to the grouping rules of the current locale.
1977 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1978 static CHAR_T *
1979 internal_function
1980 group_number (CHAR_T *w, CHAR_T *rear_ptr, const char *grouping,
1981 #ifdef COMPILE_WPRINTF
1982 wchar_t thousands_sep
1983 #else
1984 const char *thousands_sep
1985 #endif
1988 int len;
1989 CHAR_T *src, *s;
1990 #ifndef COMPILE_WPRINTF
1991 int tlen = strlen (thousands_sep);
1992 #endif
1994 /* We treat all negative values like CHAR_MAX. */
1996 if (*grouping == CHAR_MAX || *grouping <= 0)
1997 /* No grouping should be done. */
1998 return w;
2000 len = *grouping++;
2002 /* Copy existing string so that nothing gets overwritten. */
2003 src = (CHAR_T *) alloca ((rear_ptr - w) * sizeof (CHAR_T));
2004 s = (CHAR_T *) __mempcpy (src, w,
2005 (rear_ptr - w) * sizeof (CHAR_T));
2006 w = rear_ptr;
2008 /* Process all characters in the string. */
2009 while (s > src)
2011 *--w = *--s;
2013 if (--len == 0 && s > src)
2015 /* A new group begins. */
2016 #ifdef COMPILE_WPRINTF
2017 *--w = thousands_sep;
2018 #else
2019 int cnt = tlen;
2021 *--w = thousands_sep[--cnt];
2022 while (cnt > 0);
2023 #endif
2025 if (*grouping == CHAR_MAX
2026 #if CHAR_MIN < 0
2027 || *grouping < 0
2028 #endif
2031 /* No further grouping to be done.
2032 Copy the rest of the number. */
2034 *--w = *--s;
2035 while (s > src);
2036 break;
2038 else if (*grouping != '\0')
2039 /* The previous grouping repeats ad infinitum. */
2040 len = *grouping++;
2041 else
2042 len = grouping[-1];
2045 return w;
2048 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2049 struct helper_file
2051 struct _IO_FILE_plus _f;
2052 #ifdef COMPILE_WPRINTF
2053 struct _IO_wide_data _wide_data;
2054 #endif
2055 _IO_FILE *_put_stream;
2056 #ifdef _IO_MTSAFE_IO
2057 _IO_lock_t lock;
2058 #endif
2061 static int
2062 _IO_helper_overflow (_IO_FILE *s, int c)
2064 _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
2065 #ifdef COMPILE_WPRINTF
2066 int used = s->_wide_data->_IO_write_ptr - s->_wide_data->_IO_write_base;
2067 if (used)
2069 _IO_size_t written = _IO_sputn (target, s->_wide_data->_IO_write_base,
2070 used);
2071 s->_wide_data->_IO_write_ptr -= written;
2073 #else
2074 int used = s->_IO_write_ptr - s->_IO_write_base;
2075 if (used)
2077 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
2078 s->_IO_write_ptr -= written;
2080 #endif
2081 return PUTC (c, s);
2084 #ifdef COMPILE_WPRINTF
2085 static const struct _IO_jump_t _IO_helper_jumps =
2087 JUMP_INIT_DUMMY,
2088 JUMP_INIT (finish, INTUSE(_IO_wdefault_finish)),
2089 JUMP_INIT (overflow, _IO_helper_overflow),
2090 JUMP_INIT (underflow, _IO_default_underflow),
2091 JUMP_INIT (uflow, INTUSE(_IO_default_uflow)),
2092 JUMP_INIT (pbackfail, (_IO_pbackfail_t) INTUSE(_IO_wdefault_pbackfail)),
2093 JUMP_INIT (xsputn, INTUSE(_IO_wdefault_xsputn)),
2094 JUMP_INIT (xsgetn, INTUSE(_IO_wdefault_xsgetn)),
2095 JUMP_INIT (seekoff, _IO_default_seekoff),
2096 JUMP_INIT (seekpos, _IO_default_seekpos),
2097 JUMP_INIT (setbuf, _IO_default_setbuf),
2098 JUMP_INIT (sync, _IO_default_sync),
2099 JUMP_INIT (doallocate, INTUSE(_IO_wdefault_doallocate)),
2100 JUMP_INIT (read, _IO_default_read),
2101 JUMP_INIT (write, _IO_default_write),
2102 JUMP_INIT (seek, _IO_default_seek),
2103 JUMP_INIT (close, _IO_default_close),
2104 JUMP_INIT (stat, _IO_default_stat)
2106 #else
2107 static const struct _IO_jump_t _IO_helper_jumps =
2109 JUMP_INIT_DUMMY,
2110 JUMP_INIT (finish, INTUSE(_IO_default_finish)),
2111 JUMP_INIT (overflow, _IO_helper_overflow),
2112 JUMP_INIT (underflow, _IO_default_underflow),
2113 JUMP_INIT (uflow, INTUSE(_IO_default_uflow)),
2114 JUMP_INIT (pbackfail, INTUSE(_IO_default_pbackfail)),
2115 JUMP_INIT (xsputn, INTUSE(_IO_default_xsputn)),
2116 JUMP_INIT (xsgetn, INTUSE(_IO_default_xsgetn)),
2117 JUMP_INIT (seekoff, _IO_default_seekoff),
2118 JUMP_INIT (seekpos, _IO_default_seekpos),
2119 JUMP_INIT (setbuf, _IO_default_setbuf),
2120 JUMP_INIT (sync, _IO_default_sync),
2121 JUMP_INIT (doallocate, INTUSE(_IO_default_doallocate)),
2122 JUMP_INIT (read, _IO_default_read),
2123 JUMP_INIT (write, _IO_default_write),
2124 JUMP_INIT (seek, _IO_default_seek),
2125 JUMP_INIT (close, _IO_default_close),
2126 JUMP_INIT (stat, _IO_default_stat)
2128 #endif
2130 static int
2131 internal_function
2132 buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format,
2133 _IO_va_list args)
2135 CHAR_T buf[_IO_BUFSIZ];
2136 struct helper_file helper;
2137 register _IO_FILE *hp = (_IO_FILE *) &helper._f;
2138 int result, to_flush;
2140 /* Orient the stream. */
2141 #ifdef ORIENT
2142 ORIENT;
2143 #endif
2145 /* Initialize helper. */
2146 helper._put_stream = s;
2147 #ifdef COMPILE_WPRINTF
2148 hp->_wide_data = &helper._wide_data;
2149 _IO_wsetp (hp, buf, buf + sizeof buf / sizeof (CHAR_T));
2150 hp->_mode = 1;
2151 #else
2152 _IO_setp (hp, buf, buf + sizeof buf);
2153 hp->_mode = -1;
2154 #endif
2155 hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS|_IO_USER_LOCK;
2156 #if _IO_JUMPS_OFFSET
2157 hp->_vtable_offset = 0;
2158 #endif
2159 #ifdef _IO_MTSAFE_IO
2160 hp->_lock = NULL;
2161 #endif
2162 hp->_flags2 = s->_flags2;
2163 _IO_JUMPS (&helper._f) = (struct _IO_jump_t *) &_IO_helper_jumps;
2165 /* Now print to helper instead. */
2166 #ifndef COMPILE_WPRINTF
2167 result = INTUSE(_IO_vfprintf) (hp, format, args);
2168 #else
2169 result = vfprintf (hp, format, args);
2170 #endif
2172 /* Lock stream. */
2173 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, s);
2174 _IO_flockfile (s);
2176 /* Now flush anything from the helper to the S. */
2177 #ifdef COMPILE_WPRINTF
2178 if ((to_flush = (hp->_wide_data->_IO_write_ptr
2179 - hp->_wide_data->_IO_write_base)) > 0)
2181 if ((int) _IO_sputn (s, hp->_wide_data->_IO_write_base, to_flush)
2182 != to_flush)
2183 result = -1;
2185 #else
2186 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
2188 if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
2189 result = -1;
2191 #endif
2193 /* Unlock the stream. */
2194 _IO_funlockfile (s);
2195 __libc_cleanup_region_end (0);
2197 return result;
2200 #undef vfprintf
2201 #ifdef COMPILE_WPRINTF
2202 strong_alias (_IO_vfwprintf, __vfwprintf);
2203 ldbl_weak_alias (_IO_vfwprintf, vfwprintf);
2204 #else
2205 ldbl_strong_alias (_IO_vfprintf_internal, vfprintf);
2206 ldbl_hidden_def (_IO_vfprintf_internal, vfprintf)
2207 ldbl_strong_alias (_IO_vfprintf_internal, _IO_vfprintf);
2208 #endif