Remove ENABLE_SSSE3_ON_ATOM.
[glibc.git] / stdio-common / vfprintf.c
blob38ba8ffdcd6f6566e4a0a15510754ce8fbaddce5
1 /* Copyright (C) 1991-2008, 2009 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <ctype.h>
20 #include <limits.h>
21 #include <printf.h>
22 #include <stdarg.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <wchar.h>
28 #include <bits/libc-lock.h>
29 #include <sys/param.h>
30 #include "_itoa.h"
31 #include <locale/localeinfo.h>
32 #include <stdio.h>
34 /* This code is shared between the standard stdio implementation found
35 in GNU C library and the libio implementation originally found in
36 GNU libg++.
38 Beside this it is also shared between the normal and wide character
39 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
42 #include <libioP.h>
43 #define FILE _IO_FILE
44 #undef va_list
45 #define va_list _IO_va_list
46 #undef BUFSIZ
47 #define BUFSIZ _IO_BUFSIZ
48 #define ARGCHECK(S, Format) \
49 do \
50 { \
51 /* Check file argument for consistence. */ \
52 CHECK_FILE (S, -1); \
53 if (S->_flags & _IO_NO_WRITES) \
54 { \
55 __set_errno (EBADF); \
56 return -1; \
57 } \
58 if (Format == NULL) \
59 { \
60 MAYBE_SET_EINVAL; \
61 return -1; \
62 } \
63 } while (0)
64 #define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
66 #define done_add(val) \
67 do { \
68 unsigned int _val = val; \
69 assert ((unsigned int) done < (unsigned int) INT_MAX); \
70 if (__builtin_expect ((unsigned int) INT_MAX - (unsigned int) done \
71 < _val, 0)) \
72 { \
73 done = -1; \
74 goto all_done; \
75 } \
76 done += _val; \
77 } while (0)
79 #ifndef COMPILE_WPRINTF
80 # define vfprintf _IO_vfprintf_internal
81 # define CHAR_T char
82 # define UCHAR_T unsigned char
83 # define INT_T int
84 # define L_(Str) Str
85 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
86 # define STR_LEN(Str) strlen (Str)
88 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
89 # define PAD(Padchar) \
90 if (width > 0) \
91 done_add (INTUSE(_IO_padn) (s, (Padchar), width))
92 # define PUTC(C, F) _IO_putc_unlocked (C, F)
93 # define ORIENT if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
94 return -1
95 #else
96 # define vfprintf _IO_vfwprintf
97 # define CHAR_T wchar_t
98 /* This is a hack!!! There should be a type uwchar_t. */
99 # define UCHAR_T unsigned int /* uwchar_t */
100 # define INT_T wint_t
101 # define L_(Str) L##Str
102 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
103 # define STR_LEN(Str) __wcslen (Str)
105 # include "_itowa.h"
107 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
108 # define PAD(Padchar) \
109 if (width > 0) \
110 done_add (_IO_wpadn (s, (Padchar), width))
111 # define PUTC(C, F) _IO_putwc_unlocked (C, F)
112 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
114 # undef _itoa
115 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
116 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
117 # undef EOF
118 # define EOF WEOF
119 #endif
121 #include "_i18n_number.h"
123 /* Include the shared code for parsing the format string. */
124 #include "printf-parse.h"
127 #define outchar(Ch) \
128 do \
130 register const INT_T outc = (Ch); \
131 if (PUTC (outc, s) == EOF || done == INT_MAX) \
133 done = -1; \
134 goto all_done; \
136 ++done; \
138 while (0)
140 #define outstring(String, Len) \
141 do \
143 assert ((size_t) done <= (size_t) INT_MAX); \
144 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len) \
145 || (size_t) INT_MAX - (size_t) done < (size_t) (Len)) \
147 done = -1; \
148 goto all_done; \
150 done += (Len); \
152 while (0)
154 /* For handling long_double and longlong we use the same flag. If
155 `long' and `long long' are effectively the same type define it to
156 zero. */
157 #if LONG_MAX == LONG_LONG_MAX
158 # define is_longlong 0
159 #else
160 # define is_longlong is_long_double
161 #endif
163 /* If `long' and `int' is effectively the same type we don't have to
164 handle `long separately. */
165 #if INT_MAX == LONG_MAX
166 # define is_long_num 0
167 #else
168 # define is_long_num is_long
169 #endif
172 /* Global variables. */
173 static const CHAR_T null[] = L_("(null)");
176 /* Helper function to provide temporary buffering for unbuffered streams. */
177 static int buffered_vfprintf (FILE *stream, const CHAR_T *fmt, va_list)
178 __THROW __attribute__ ((noinline)) internal_function;
180 /* Handle unknown format specifier. */
181 static int printf_unknown (FILE *, const struct printf_info *,
182 const void *const *) __THROW;
184 /* Group digits of number string. */
185 #ifdef COMPILE_WPRINTF
186 static CHAR_T *group_number (CHAR_T *, CHAR_T *, const char *, wchar_t)
187 __THROW internal_function;
188 #else
189 static CHAR_T *group_number (CHAR_T *, CHAR_T *, const char *, const char *)
190 __THROW internal_function;
191 #endif
194 /* The function itself. */
196 vfprintf (FILE *s, const CHAR_T *format, va_list ap)
198 /* The character used as thousands separator. */
199 #ifdef COMPILE_WPRINTF
200 wchar_t thousands_sep = L'\0';
201 #else
202 const char *thousands_sep = NULL;
203 #endif
205 /* The string describing the size of groups of digits. */
206 const char *grouping;
208 /* Place to accumulate the result. */
209 int done;
211 /* Current character in format string. */
212 const UCHAR_T *f;
214 /* End of leading constant string. */
215 const UCHAR_T *lead_str_end;
217 /* Points to next format specifier. */
218 const UCHAR_T *end_of_spec;
220 /* Buffer intermediate results. */
221 CHAR_T work_buffer[1000];
222 CHAR_T *workstart = NULL;
223 CHAR_T *workend;
225 /* We have to save the original argument pointer. */
226 va_list ap_save;
228 /* Count number of specifiers we already processed. */
229 int nspecs_done;
231 /* For the %m format we may need the current `errno' value. */
232 int save_errno = errno;
234 /* 1 if format is in read-only memory, -1 if it is in writable memory,
235 0 if unknown. */
236 int readonly_format = 0;
238 /* This table maps a character into a number representing a
239 class. In each step there is a destination label for each
240 class. */
241 static const uint8_t jump_table[] =
243 /* ' ' */ 1, 0, 0, /* '#' */ 4,
244 0, /* '%' */ 14, 0, /* '\''*/ 6,
245 0, 0, /* '*' */ 7, /* '+' */ 2,
246 0, /* '-' */ 3, /* '.' */ 9, 0,
247 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
248 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
249 /* '8' */ 8, /* '9' */ 8, 0, 0,
250 0, 0, 0, 0,
251 0, /* 'A' */ 26, 0, /* 'C' */ 25,
252 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
253 0, /* 'I' */ 29, 0, 0,
254 /* 'L' */ 12, 0, 0, 0,
255 0, 0, 0, /* 'S' */ 21,
256 0, 0, 0, 0,
257 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
258 0, 0, 0, 0,
259 0, /* 'a' */ 26, 0, /* 'c' */ 20,
260 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
261 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
262 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
263 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
264 /* 't' */ 27, /* 'u' */ 16, 0, 0,
265 /* 'x' */ 18, 0, /* 'z' */ 13
268 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
269 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
270 #ifdef SHARED
271 /* 'int' is enough and it saves some space on 64 bit systems. */
272 # define JUMP_TABLE_TYPE const int
273 # define JUMP(ChExpr, table) \
274 do \
276 int offset; \
277 void *__unbounded ptr; \
278 spec = (ChExpr); \
279 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
280 : table[CHAR_CLASS (spec)]; \
281 ptr = &&do_form_unknown + offset; \
282 goto *ptr; \
284 while (0)
285 #else
286 # define JUMP_TABLE_TYPE const void *const
287 # define JUMP(ChExpr, table) \
288 do \
290 const void *__unbounded ptr; \
291 spec = (ChExpr); \
292 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
293 : table[CHAR_CLASS (spec)]; \
294 goto *ptr; \
296 while (0)
297 #endif
299 #define STEP0_3_TABLE \
300 /* Step 0: at the beginning. */ \
301 static JUMP_TABLE_TYPE step0_jumps[30] = \
303 REF (form_unknown), \
304 REF (flag_space), /* for ' ' */ \
305 REF (flag_plus), /* for '+' */ \
306 REF (flag_minus), /* for '-' */ \
307 REF (flag_hash), /* for '<hash>' */ \
308 REF (flag_zero), /* for '0' */ \
309 REF (flag_quote), /* for '\'' */ \
310 REF (width_asterics), /* for '*' */ \
311 REF (width), /* for '1'...'9' */ \
312 REF (precision), /* for '.' */ \
313 REF (mod_half), /* for 'h' */ \
314 REF (mod_long), /* for 'l' */ \
315 REF (mod_longlong), /* for 'L', 'q' */ \
316 REF (mod_size_t), /* for 'z', 'Z' */ \
317 REF (form_percent), /* for '%' */ \
318 REF (form_integer), /* for 'd', 'i' */ \
319 REF (form_unsigned), /* for 'u' */ \
320 REF (form_octal), /* for 'o' */ \
321 REF (form_hexa), /* for 'X', 'x' */ \
322 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
323 REF (form_character), /* for 'c' */ \
324 REF (form_string), /* for 's', 'S' */ \
325 REF (form_pointer), /* for 'p' */ \
326 REF (form_number), /* for 'n' */ \
327 REF (form_strerror), /* for 'm' */ \
328 REF (form_wcharacter), /* for 'C' */ \
329 REF (form_floathex), /* for 'A', 'a' */ \
330 REF (mod_ptrdiff_t), /* for 't' */ \
331 REF (mod_intmax_t), /* for 'j' */ \
332 REF (flag_i18n), /* for 'I' */ \
333 }; \
334 /* Step 1: after processing width. */ \
335 static JUMP_TABLE_TYPE step1_jumps[30] = \
337 REF (form_unknown), \
338 REF (form_unknown), /* for ' ' */ \
339 REF (form_unknown), /* for '+' */ \
340 REF (form_unknown), /* for '-' */ \
341 REF (form_unknown), /* for '<hash>' */ \
342 REF (form_unknown), /* for '0' */ \
343 REF (form_unknown), /* for '\'' */ \
344 REF (form_unknown), /* for '*' */ \
345 REF (form_unknown), /* for '1'...'9' */ \
346 REF (precision), /* for '.' */ \
347 REF (mod_half), /* for 'h' */ \
348 REF (mod_long), /* for 'l' */ \
349 REF (mod_longlong), /* for 'L', 'q' */ \
350 REF (mod_size_t), /* for 'z', 'Z' */ \
351 REF (form_percent), /* for '%' */ \
352 REF (form_integer), /* for 'd', 'i' */ \
353 REF (form_unsigned), /* for 'u' */ \
354 REF (form_octal), /* for 'o' */ \
355 REF (form_hexa), /* for 'X', 'x' */ \
356 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
357 REF (form_character), /* for 'c' */ \
358 REF (form_string), /* for 's', 'S' */ \
359 REF (form_pointer), /* for 'p' */ \
360 REF (form_number), /* for 'n' */ \
361 REF (form_strerror), /* for 'm' */ \
362 REF (form_wcharacter), /* for 'C' */ \
363 REF (form_floathex), /* for 'A', 'a' */ \
364 REF (mod_ptrdiff_t), /* for 't' */ \
365 REF (mod_intmax_t), /* for 'j' */ \
366 REF (form_unknown) /* for 'I' */ \
367 }; \
368 /* Step 2: after processing precision. */ \
369 static JUMP_TABLE_TYPE step2_jumps[30] = \
371 REF (form_unknown), \
372 REF (form_unknown), /* for ' ' */ \
373 REF (form_unknown), /* for '+' */ \
374 REF (form_unknown), /* for '-' */ \
375 REF (form_unknown), /* for '<hash>' */ \
376 REF (form_unknown), /* for '0' */ \
377 REF (form_unknown), /* for '\'' */ \
378 REF (form_unknown), /* for '*' */ \
379 REF (form_unknown), /* for '1'...'9' */ \
380 REF (form_unknown), /* for '.' */ \
381 REF (mod_half), /* for 'h' */ \
382 REF (mod_long), /* for 'l' */ \
383 REF (mod_longlong), /* for 'L', 'q' */ \
384 REF (mod_size_t), /* for 'z', 'Z' */ \
385 REF (form_percent), /* for '%' */ \
386 REF (form_integer), /* for 'd', 'i' */ \
387 REF (form_unsigned), /* for 'u' */ \
388 REF (form_octal), /* for 'o' */ \
389 REF (form_hexa), /* for 'X', 'x' */ \
390 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
391 REF (form_character), /* for 'c' */ \
392 REF (form_string), /* for 's', 'S' */ \
393 REF (form_pointer), /* for 'p' */ \
394 REF (form_number), /* for 'n' */ \
395 REF (form_strerror), /* for 'm' */ \
396 REF (form_wcharacter), /* for 'C' */ \
397 REF (form_floathex), /* for 'A', 'a' */ \
398 REF (mod_ptrdiff_t), /* for 't' */ \
399 REF (mod_intmax_t), /* for 'j' */ \
400 REF (form_unknown) /* for 'I' */ \
401 }; \
402 /* Step 3a: after processing first 'h' modifier. */ \
403 static JUMP_TABLE_TYPE step3a_jumps[30] = \
405 REF (form_unknown), \
406 REF (form_unknown), /* for ' ' */ \
407 REF (form_unknown), /* for '+' */ \
408 REF (form_unknown), /* for '-' */ \
409 REF (form_unknown), /* for '<hash>' */ \
410 REF (form_unknown), /* for '0' */ \
411 REF (form_unknown), /* for '\'' */ \
412 REF (form_unknown), /* for '*' */ \
413 REF (form_unknown), /* for '1'...'9' */ \
414 REF (form_unknown), /* for '.' */ \
415 REF (mod_halfhalf), /* for 'h' */ \
416 REF (form_unknown), /* for 'l' */ \
417 REF (form_unknown), /* for 'L', 'q' */ \
418 REF (form_unknown), /* for 'z', 'Z' */ \
419 REF (form_percent), /* for '%' */ \
420 REF (form_integer), /* for 'd', 'i' */ \
421 REF (form_unsigned), /* for 'u' */ \
422 REF (form_octal), /* for 'o' */ \
423 REF (form_hexa), /* for 'X', 'x' */ \
424 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
425 REF (form_unknown), /* for 'c' */ \
426 REF (form_unknown), /* for 's', 'S' */ \
427 REF (form_unknown), /* for 'p' */ \
428 REF (form_number), /* for 'n' */ \
429 REF (form_unknown), /* for 'm' */ \
430 REF (form_unknown), /* for 'C' */ \
431 REF (form_unknown), /* for 'A', 'a' */ \
432 REF (form_unknown), /* for 't' */ \
433 REF (form_unknown), /* for 'j' */ \
434 REF (form_unknown) /* for 'I' */ \
435 }; \
436 /* Step 3b: after processing first 'l' modifier. */ \
437 static JUMP_TABLE_TYPE step3b_jumps[30] = \
439 REF (form_unknown), \
440 REF (form_unknown), /* for ' ' */ \
441 REF (form_unknown), /* for '+' */ \
442 REF (form_unknown), /* for '-' */ \
443 REF (form_unknown), /* for '<hash>' */ \
444 REF (form_unknown), /* for '0' */ \
445 REF (form_unknown), /* for '\'' */ \
446 REF (form_unknown), /* for '*' */ \
447 REF (form_unknown), /* for '1'...'9' */ \
448 REF (form_unknown), /* for '.' */ \
449 REF (form_unknown), /* for 'h' */ \
450 REF (mod_longlong), /* for 'l' */ \
451 REF (form_unknown), /* for 'L', 'q' */ \
452 REF (form_unknown), /* for 'z', 'Z' */ \
453 REF (form_percent), /* for '%' */ \
454 REF (form_integer), /* for 'd', 'i' */ \
455 REF (form_unsigned), /* for 'u' */ \
456 REF (form_octal), /* for 'o' */ \
457 REF (form_hexa), /* for 'X', 'x' */ \
458 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
459 REF (form_character), /* for 'c' */ \
460 REF (form_string), /* for 's', 'S' */ \
461 REF (form_pointer), /* for 'p' */ \
462 REF (form_number), /* for 'n' */ \
463 REF (form_strerror), /* for 'm' */ \
464 REF (form_wcharacter), /* for 'C' */ \
465 REF (form_floathex), /* for 'A', 'a' */ \
466 REF (form_unknown), /* for 't' */ \
467 REF (form_unknown), /* for 'j' */ \
468 REF (form_unknown) /* for 'I' */ \
471 #define STEP4_TABLE \
472 /* Step 4: processing format specifier. */ \
473 static JUMP_TABLE_TYPE step4_jumps[30] = \
475 REF (form_unknown), \
476 REF (form_unknown), /* for ' ' */ \
477 REF (form_unknown), /* for '+' */ \
478 REF (form_unknown), /* for '-' */ \
479 REF (form_unknown), /* for '<hash>' */ \
480 REF (form_unknown), /* for '0' */ \
481 REF (form_unknown), /* for '\'' */ \
482 REF (form_unknown), /* for '*' */ \
483 REF (form_unknown), /* for '1'...'9' */ \
484 REF (form_unknown), /* for '.' */ \
485 REF (form_unknown), /* for 'h' */ \
486 REF (form_unknown), /* for 'l' */ \
487 REF (form_unknown), /* for 'L', 'q' */ \
488 REF (form_unknown), /* for 'z', 'Z' */ \
489 REF (form_percent), /* for '%' */ \
490 REF (form_integer), /* for 'd', 'i' */ \
491 REF (form_unsigned), /* for 'u' */ \
492 REF (form_octal), /* for 'o' */ \
493 REF (form_hexa), /* for 'X', 'x' */ \
494 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
495 REF (form_character), /* for 'c' */ \
496 REF (form_string), /* for 's', 'S' */ \
497 REF (form_pointer), /* for 'p' */ \
498 REF (form_number), /* for 'n' */ \
499 REF (form_strerror), /* for 'm' */ \
500 REF (form_wcharacter), /* for 'C' */ \
501 REF (form_floathex), /* for 'A', 'a' */ \
502 REF (form_unknown), /* for 't' */ \
503 REF (form_unknown), /* for 'j' */ \
504 REF (form_unknown) /* for 'I' */ \
508 #define process_arg(fspec) \
509 /* Start real work. We know about all flags and modifiers and \
510 now process the wanted format specifier. */ \
511 LABEL (form_percent): \
512 /* Write a literal "%". */ \
513 outchar (L_('%')); \
514 break; \
516 LABEL (form_integer): \
517 /* Signed decimal integer. */ \
518 base = 10; \
520 if (is_longlong) \
522 long long int signed_number; \
524 if (fspec == NULL) \
525 signed_number = va_arg (ap, long long int); \
526 else \
527 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
529 is_negative = signed_number < 0; \
530 number.longlong = is_negative ? (- signed_number) : signed_number; \
532 goto LABEL (longlong_number); \
534 else \
536 long int signed_number; \
538 if (fspec == NULL) \
540 if (is_long_num) \
541 signed_number = va_arg (ap, long int); \
542 else if (is_char) \
543 signed_number = (signed char) va_arg (ap, unsigned int); \
544 else if (!is_short) \
545 signed_number = va_arg (ap, int); \
546 else \
547 signed_number = (short int) va_arg (ap, unsigned int); \
549 else \
550 if (is_long_num) \
551 signed_number = args_value[fspec->data_arg].pa_long_int; \
552 else if (is_char) \
553 signed_number = (signed char) \
554 args_value[fspec->data_arg].pa_u_int; \
555 else if (!is_short) \
556 signed_number = args_value[fspec->data_arg].pa_int; \
557 else \
558 signed_number = (short int) \
559 args_value[fspec->data_arg].pa_u_int; \
561 is_negative = signed_number < 0; \
562 number.word = is_negative ? (- signed_number) : signed_number; \
564 goto LABEL (number); \
566 /* NOTREACHED */ \
568 LABEL (form_unsigned): \
569 /* Unsigned decimal integer. */ \
570 base = 10; \
571 goto LABEL (unsigned_number); \
572 /* NOTREACHED */ \
574 LABEL (form_octal): \
575 /* Unsigned octal integer. */ \
576 base = 8; \
577 goto LABEL (unsigned_number); \
578 /* NOTREACHED */ \
580 LABEL (form_hexa): \
581 /* Unsigned hexadecimal integer. */ \
582 base = 16; \
584 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
586 /* ISO specifies the `+' and ` ' flags only for signed \
587 conversions. */ \
588 is_negative = 0; \
589 showsign = 0; \
590 space = 0; \
592 if (is_longlong) \
594 if (fspec == NULL) \
595 number.longlong = va_arg (ap, unsigned long long int); \
596 else \
597 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
599 LABEL (longlong_number): \
600 if (prec < 0) \
601 /* Supply a default precision if none was given. */ \
602 prec = 1; \
603 else \
604 /* We have to take care for the '0' flag. If a precision \
605 is given it must be ignored. */ \
606 pad = L_(' '); \
608 /* If the precision is 0 and the number is 0 nothing has to \
609 be written for the number, except for the 'o' format in \
610 alternate form. */ \
611 if (prec == 0 && number.longlong == 0) \
613 string = workend; \
614 if (base == 8 && alt) \
615 *--string = L_('0'); \
617 else \
619 /* Put the number in WORK. */ \
620 string = _itoa (number.longlong, workend, base, \
621 spec == L_('X')); \
622 if (group && grouping) \
623 string = group_number (string, workend, grouping, \
624 thousands_sep); \
626 if (use_outdigits && base == 10) \
627 string = _i18n_number_rewrite (string, workend, workend); \
629 /* Simplify further test for num != 0. */ \
630 number.word = number.longlong != 0; \
632 else \
634 if (fspec == NULL) \
636 if (is_long_num) \
637 number.word = va_arg (ap, unsigned long int); \
638 else if (is_char) \
639 number.word = (unsigned char) va_arg (ap, unsigned int); \
640 else if (!is_short) \
641 number.word = va_arg (ap, unsigned int); \
642 else \
643 number.word = (unsigned short int) va_arg (ap, unsigned int); \
645 else \
646 if (is_long_num) \
647 number.word = args_value[fspec->data_arg].pa_u_long_int; \
648 else if (is_char) \
649 number.word = (unsigned char) \
650 args_value[fspec->data_arg].pa_u_int; \
651 else if (!is_short) \
652 number.word = args_value[fspec->data_arg].pa_u_int; \
653 else \
654 number.word = (unsigned short int) \
655 args_value[fspec->data_arg].pa_u_int; \
657 LABEL (number): \
658 if (prec < 0) \
659 /* Supply a default precision if none was given. */ \
660 prec = 1; \
661 else \
662 /* We have to take care for the '0' flag. If a precision \
663 is given it must be ignored. */ \
664 pad = L_(' '); \
666 /* If the precision is 0 and the number is 0 nothing has to \
667 be written for the number, except for the 'o' format in \
668 alternate form. */ \
669 if (prec == 0 && number.word == 0) \
671 string = workend; \
672 if (base == 8 && alt) \
673 *--string = L_('0'); \
675 else \
677 /* Put the number in WORK. */ \
678 string = _itoa_word (number.word, workend, base, \
679 spec == L_('X')); \
680 if (group && grouping) \
681 string = group_number (string, workend, grouping, \
682 thousands_sep); \
684 if (use_outdigits && base == 10) \
685 string = _i18n_number_rewrite (string, workend, workend); \
689 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
690 /* Add octal marker. */ \
691 *--string = L_('0'); \
693 prec = MAX (0, prec - (workend - string)); \
695 if (!left) \
697 width -= workend - string + prec; \
699 if (number.word != 0 && alt && base == 16) \
700 /* Account for 0X hex marker. */ \
701 width -= 2; \
703 if (is_negative || showsign || space) \
704 --width; \
706 if (pad == L_(' ')) \
708 PAD (L_(' ')); \
709 width = 0; \
712 if (is_negative) \
713 outchar (L_('-')); \
714 else if (showsign) \
715 outchar (L_('+')); \
716 else if (space) \
717 outchar (L_(' ')); \
719 if (number.word != 0 && alt && base == 16) \
721 outchar (L_('0')); \
722 outchar (spec); \
725 width += prec; \
726 PAD (L_('0')); \
728 outstring (string, workend - string); \
730 break; \
732 else \
734 if (is_negative) \
736 outchar (L_('-')); \
737 --width; \
739 else if (showsign) \
741 outchar (L_('+')); \
742 --width; \
744 else if (space) \
746 outchar (L_(' ')); \
747 --width; \
750 if (number.word != 0 && alt && base == 16) \
752 outchar (L_('0')); \
753 outchar (spec); \
754 width -= 2; \
757 width -= workend - string + prec; \
759 if (prec > 0) \
761 int temp = width; \
762 width = prec; \
763 PAD (L_('0')); \
764 width = temp; \
767 outstring (string, workend - string); \
769 PAD (L_(' ')); \
770 break; \
773 LABEL (form_float): \
775 /* Floating-point number. This is handled by printf_fp.c. */ \
776 const void *ptr; \
777 int function_done; \
779 if (fspec == NULL) \
781 if (__ldbl_is_dbl) \
782 is_long_double = 0; \
784 struct printf_info info = { .prec = prec, \
785 .width = width, \
786 .spec = spec, \
787 .is_long_double = is_long_double, \
788 .is_short = is_short, \
789 .is_long = is_long, \
790 .alt = alt, \
791 .space = space, \
792 .left = left, \
793 .showsign = showsign, \
794 .group = group, \
795 .pad = pad, \
796 .extra = 0, \
797 .i18n = use_outdigits, \
798 .wide = sizeof (CHAR_T) != 1 }; \
800 if (is_long_double) \
801 the_arg.pa_long_double = va_arg (ap, long double); \
802 else \
803 the_arg.pa_double = va_arg (ap, double); \
804 ptr = (const void *) &the_arg; \
806 function_done = __printf_fp (s, &info, &ptr); \
808 else \
810 ptr = (const void *) &args_value[fspec->data_arg]; \
811 if (__ldbl_is_dbl) \
813 fspec->data_arg_type = PA_DOUBLE; \
814 fspec->info.is_long_double = 0; \
817 function_done = __printf_fp (s, &fspec->info, &ptr); \
820 if (function_done < 0) \
822 /* Error in print handler. */ \
823 done = -1; \
824 goto all_done; \
827 done_add (function_done); \
829 break; \
831 LABEL (form_floathex): \
833 /* Floating point number printed as hexadecimal number. */ \
834 const void *ptr; \
835 int function_done; \
837 if (fspec == NULL) \
839 if (__ldbl_is_dbl) \
840 is_long_double = 0; \
842 struct printf_info info = { .prec = prec, \
843 .width = width, \
844 .spec = spec, \
845 .is_long_double = is_long_double, \
846 .is_short = is_short, \
847 .is_long = is_long, \
848 .alt = alt, \
849 .space = space, \
850 .left = left, \
851 .showsign = showsign, \
852 .group = group, \
853 .pad = pad, \
854 .extra = 0, \
855 .wide = sizeof (CHAR_T) != 1 }; \
857 if (is_long_double) \
858 the_arg.pa_long_double = va_arg (ap, long double); \
859 else \
860 the_arg.pa_double = va_arg (ap, double); \
861 ptr = (const void *) &the_arg; \
863 function_done = __printf_fphex (s, &info, &ptr); \
865 else \
867 ptr = (const void *) &args_value[fspec->data_arg]; \
868 if (__ldbl_is_dbl) \
869 fspec->info.is_long_double = 0; \
871 function_done = __printf_fphex (s, &fspec->info, &ptr); \
874 if (function_done < 0) \
876 /* Error in print handler. */ \
877 done = -1; \
878 goto all_done; \
881 done_add (function_done); \
883 break; \
885 LABEL (form_pointer): \
886 /* Generic pointer. */ \
888 const void *ptr; \
889 if (fspec == NULL) \
890 ptr = va_arg (ap, void *); \
891 else \
892 ptr = args_value[fspec->data_arg].pa_pointer; \
893 if (ptr != NULL) \
895 /* If the pointer is not NULL, write it as a %#x spec. */ \
896 base = 16; \
897 number.word = (unsigned long int) ptr; \
898 is_negative = 0; \
899 alt = 1; \
900 group = 0; \
901 spec = L_('x'); \
902 goto LABEL (number); \
904 else \
906 /* Write "(nil)" for a nil pointer. */ \
907 string = (CHAR_T *) L_("(nil)"); \
908 /* Make sure the full string "(nil)" is printed. */ \
909 if (prec < 5) \
910 prec = 5; \
911 is_long = 0; /* This is no wide-char string. */ \
912 goto LABEL (print_string); \
915 /* NOTREACHED */ \
917 LABEL (form_number): \
918 if (s->_flags2 & _IO_FLAGS2_FORTIFY) \
920 if (! readonly_format) \
922 extern int __readonly_area (const void *, size_t) \
923 attribute_hidden; \
924 readonly_format \
925 = __readonly_area (format, ((STR_LEN (format) + 1) \
926 * sizeof (CHAR_T))); \
928 if (readonly_format < 0) \
929 __libc_fatal ("*** %n in writable segment detected ***\n"); \
931 /* Answer the count of characters written. */ \
932 if (fspec == NULL) \
934 if (is_longlong) \
935 *(long long int *) va_arg (ap, void *) = done; \
936 else if (is_long_num) \
937 *(long int *) va_arg (ap, void *) = done; \
938 else if (is_char) \
939 *(char *) va_arg (ap, void *) = done; \
940 else if (!is_short) \
941 *(int *) va_arg (ap, void *) = done; \
942 else \
943 *(short int *) va_arg (ap, void *) = done; \
945 else \
946 if (is_longlong) \
947 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
948 else if (is_long_num) \
949 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
950 else if (is_char) \
951 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
952 else if (!is_short) \
953 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
954 else \
955 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
956 break; \
958 LABEL (form_strerror): \
959 /* Print description of error ERRNO. */ \
960 string = \
961 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
962 sizeof work_buffer); \
963 is_long = 0; /* This is no wide-char string. */ \
964 goto LABEL (print_string)
966 #ifdef COMPILE_WPRINTF
967 # define process_string_arg(fspec) \
968 LABEL (form_character): \
969 /* Character. */ \
970 if (is_long) \
971 goto LABEL (form_wcharacter); \
972 --width; /* Account for the character itself. */ \
973 if (!left) \
974 PAD (L' '); \
975 if (fspec == NULL) \
976 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
977 else \
978 outchar (__btowc ((unsigned char) \
979 args_value[fspec->data_arg].pa_int)); \
980 if (left) \
981 PAD (L' '); \
982 break; \
984 LABEL (form_wcharacter): \
986 /* Wide character. */ \
987 --width; \
988 if (!left) \
989 PAD (L' '); \
990 if (fspec == NULL) \
991 outchar (va_arg (ap, wchar_t)); \
992 else \
993 outchar (args_value[fspec->data_arg].pa_wchar); \
994 if (left) \
995 PAD (L' '); \
997 break; \
999 LABEL (form_string): \
1001 size_t len; \
1002 int string_malloced; \
1004 /* The string argument could in fact be `char *' or `wchar_t *'. \
1005 But this should not make a difference here. */ \
1006 if (fspec == NULL) \
1007 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
1008 else \
1009 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
1011 /* Entry point for printing other strings. */ \
1012 LABEL (print_string): \
1014 string_malloced = 0; \
1015 if (string == NULL) \
1017 /* Write "(null)" if there's space. */ \
1018 if (prec == -1 \
1019 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
1021 string = (CHAR_T *) null; \
1022 len = (sizeof (null) / sizeof (null[0])) - 1; \
1024 else \
1026 string = (CHAR_T *) L""; \
1027 len = 0; \
1030 else if (!is_long && spec != L_('S')) \
1032 /* This is complicated. We have to transform the multibyte \
1033 string into a wide character string. */ \
1034 const char *mbs = (const char *) string; \
1035 mbstate_t mbstate; \
1037 len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
1039 /* Allocate dynamically an array which definitely is long \
1040 enough for the wide character version. Each byte in the \
1041 multi-byte string can produce at most one wide character. */ \
1042 if (__libc_use_alloca (len * sizeof (wchar_t))) \
1043 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1044 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1045 == NULL) \
1047 done = -1; \
1048 goto all_done; \
1050 else \
1051 string_malloced = 1; \
1053 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1054 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1055 if (len == (size_t) -1) \
1057 /* Illegal multibyte character. */ \
1058 done = -1; \
1059 goto all_done; \
1062 else \
1064 if (prec != -1) \
1065 /* Search for the end of the string, but don't search past \
1066 the length specified by the precision. */ \
1067 len = __wcsnlen (string, prec); \
1068 else \
1069 len = __wcslen (string); \
1072 if ((width -= len) < 0) \
1074 outstring (string, len); \
1075 break; \
1078 if (!left) \
1079 PAD (L' '); \
1080 outstring (string, len); \
1081 if (left) \
1082 PAD (L' '); \
1083 if (__builtin_expect (string_malloced, 0)) \
1084 free (string); \
1086 break;
1087 #else
1088 # define process_string_arg(fspec) \
1089 LABEL (form_character): \
1090 /* Character. */ \
1091 if (is_long) \
1092 goto LABEL (form_wcharacter); \
1093 --width; /* Account for the character itself. */ \
1094 if (!left) \
1095 PAD (' '); \
1096 if (fspec == NULL) \
1097 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1098 else \
1099 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1100 if (left) \
1101 PAD (' '); \
1102 break; \
1104 LABEL (form_wcharacter): \
1106 /* Wide character. */ \
1107 char buf[MB_CUR_MAX]; \
1108 mbstate_t mbstate; \
1109 size_t len; \
1111 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1112 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1113 : args_value[fspec->data_arg].pa_wchar), \
1114 &mbstate); \
1115 if (len == (size_t) -1) \
1117 /* Something went wron gduring the conversion. Bail out. */ \
1118 done = -1; \
1119 goto all_done; \
1121 width -= len; \
1122 if (!left) \
1123 PAD (' '); \
1124 outstring (buf, len); \
1125 if (left) \
1126 PAD (' '); \
1128 break; \
1130 LABEL (form_string): \
1132 size_t len; \
1133 int string_malloced; \
1135 /* The string argument could in fact be `char *' or `wchar_t *'. \
1136 But this should not make a difference here. */ \
1137 if (fspec == NULL) \
1138 string = (char *) va_arg (ap, const char *); \
1139 else \
1140 string = (char *) args_value[fspec->data_arg].pa_string; \
1142 /* Entry point for printing other strings. */ \
1143 LABEL (print_string): \
1145 string_malloced = 0; \
1146 if (string == NULL) \
1148 /* Write "(null)" if there's space. */ \
1149 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1151 string = (char *) null; \
1152 len = sizeof (null) - 1; \
1154 else \
1156 string = (char *) ""; \
1157 len = 0; \
1160 else if (!is_long && spec != L_('S')) \
1162 if (prec != -1) \
1164 /* Search for the end of the string, but don't search past \
1165 the length (in bytes) specified by the precision. Also \
1166 don't use incomplete characters. */ \
1167 if (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MB_CUR_MAX) == 1) \
1168 len = __strnlen (string, prec); \
1169 else \
1171 /* In case we have a multibyte character set the \
1172 situation is more complicated. We must not copy \
1173 bytes at the end which form an incomplete character. */\
1174 size_t ignore_size = (unsigned) prec > 1024 ? 1024 : prec;\
1175 wchar_t ignore[ignore_size]; \
1176 const char *str2 = string; \
1177 const char *strend = string + prec; \
1178 if (strend < string) \
1179 strend = (const char *) UINTPTR_MAX; \
1181 mbstate_t ps; \
1182 memset (&ps, '\0', sizeof (ps)); \
1184 while (str2 != NULL && str2 < strend) \
1185 if (__mbsnrtowcs (ignore, &str2, strend - str2, \
1186 ignore_size, &ps) == (size_t) -1) \
1188 done = -1; \
1189 goto all_done; \
1192 if (str2 == NULL) \
1193 len = strlen (string); \
1194 else \
1195 len = str2 - string - (ps.__count & 7); \
1198 else \
1199 len = strlen (string); \
1201 else \
1203 const wchar_t *s2 = (const wchar_t *) string; \
1204 mbstate_t mbstate; \
1206 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1208 if (prec >= 0) \
1210 /* The string `s2' might not be NUL terminated. */ \
1211 if (__libc_use_alloca (prec)) \
1212 string = (char *) alloca (prec); \
1213 else if ((string = (char *) malloc (prec)) == NULL) \
1215 done = -1; \
1216 goto all_done; \
1218 else \
1219 string_malloced = 1; \
1220 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1222 else \
1224 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1225 if (len != (size_t) -1) \
1227 assert (__mbsinit (&mbstate)); \
1228 s2 = (const wchar_t *) string; \
1229 if (__libc_use_alloca (len + 1)) \
1230 string = (char *) alloca (len + 1); \
1231 else if ((string = (char *) malloc (len + 1)) == NULL) \
1233 done = -1; \
1234 goto all_done; \
1236 else \
1237 string_malloced = 1; \
1238 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1242 if (len == (size_t) -1) \
1244 /* Illegal wide-character string. */ \
1245 done = -1; \
1246 goto all_done; \
1250 if ((width -= len) < 0) \
1252 outstring (string, len); \
1253 break; \
1256 if (!left) \
1257 PAD (' '); \
1258 outstring (string, len); \
1259 if (left) \
1260 PAD (' '); \
1261 if (__builtin_expect (string_malloced, 0)) \
1262 free (string); \
1264 break;
1265 #endif
1267 /* Orient the stream. */
1268 #ifdef ORIENT
1269 ORIENT;
1270 #endif
1272 /* Sanity check of arguments. */
1273 ARGCHECK (s, format);
1275 #ifdef ORIENT
1276 /* Check for correct orientation. */
1277 if (_IO_vtable_offset (s) == 0 &&
1278 _IO_fwide (s, sizeof (CHAR_T) == 1 ? -1 : 1)
1279 != (sizeof (CHAR_T) == 1 ? -1 : 1))
1280 /* The stream is already oriented otherwise. */
1281 return EOF;
1282 #endif
1284 if (UNBUFFERED_P (s))
1285 /* Use a helper function which will allocate a local temporary buffer
1286 for the stream and then call us again. */
1287 return buffered_vfprintf (s, format, ap);
1289 /* Initialize local variables. */
1290 done = 0;
1291 grouping = (const char *) -1;
1292 #ifdef __va_copy
1293 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1294 since on some systems `va_list' is not an integral type. */
1295 __va_copy (ap_save, ap);
1296 #else
1297 ap_save = ap;
1298 #endif
1299 nspecs_done = 0;
1301 #ifdef COMPILE_WPRINTF
1302 /* Find the first format specifier. */
1303 f = lead_str_end = __find_specwc ((const UCHAR_T *) format);
1304 #else
1305 /* Find the first format specifier. */
1306 f = lead_str_end = __find_specmb ((const UCHAR_T *) format);
1307 #endif
1309 /* Lock stream. */
1310 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
1311 _IO_flockfile (s);
1313 /* Write the literal text before the first format. */
1314 outstring ((const UCHAR_T *) format,
1315 lead_str_end - (const UCHAR_T *) format);
1317 /* If we only have to print a simple string, return now. */
1318 if (*f == L_('\0'))
1319 goto all_done;
1321 /* Process whole format string. */
1324 #ifdef SHARED
1325 # define REF(Name) &&do_##Name - &&do_form_unknown
1326 #else
1327 # define REF(Name) &&do_##Name
1328 #endif
1329 #define LABEL(Name) do_##Name
1330 STEP0_3_TABLE;
1331 STEP4_TABLE;
1333 union printf_arg *args_value; /* This is not used here but ... */
1334 int is_negative; /* Flag for negative number. */
1335 union
1337 unsigned long long int longlong;
1338 unsigned long int word;
1339 } number;
1340 int base;
1341 union printf_arg the_arg;
1342 CHAR_T *string; /* Pointer to argument string. */
1343 int alt = 0; /* Alternate format. */
1344 int space = 0; /* Use space prefix if no sign is needed. */
1345 int left = 0; /* Left-justify output. */
1346 int showsign = 0; /* Always begin with plus or minus sign. */
1347 int group = 0; /* Print numbers according grouping rules. */
1348 int is_long_double = 0; /* Argument is long double/ long long int. */
1349 int is_short = 0; /* Argument is short int. */
1350 int is_long = 0; /* Argument is long int. */
1351 int is_char = 0; /* Argument is promoted (unsigned) char. */
1352 int width = 0; /* Width of output; 0 means none specified. */
1353 int prec = -1; /* Precision of output; -1 means none specified. */
1354 /* This flag is set by the 'I' modifier and selects the use of the
1355 `outdigits' as determined by the current locale. */
1356 int use_outdigits = 0;
1357 UCHAR_T pad = L_(' ');/* Padding character. */
1358 CHAR_T spec;
1360 workstart = NULL;
1361 workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1363 /* Get current character in format string. */
1364 JUMP (*++f, step0_jumps);
1366 /* ' ' flag. */
1367 LABEL (flag_space):
1368 space = 1;
1369 JUMP (*++f, step0_jumps);
1371 /* '+' flag. */
1372 LABEL (flag_plus):
1373 showsign = 1;
1374 JUMP (*++f, step0_jumps);
1376 /* The '-' flag. */
1377 LABEL (flag_minus):
1378 left = 1;
1379 pad = L_(' ');
1380 JUMP (*++f, step0_jumps);
1382 /* The '#' flag. */
1383 LABEL (flag_hash):
1384 alt = 1;
1385 JUMP (*++f, step0_jumps);
1387 /* The '0' flag. */
1388 LABEL (flag_zero):
1389 if (!left)
1390 pad = L_('0');
1391 JUMP (*++f, step0_jumps);
1393 /* The '\'' flag. */
1394 LABEL (flag_quote):
1395 group = 1;
1397 if (grouping == (const char *) -1)
1399 #ifdef COMPILE_WPRINTF
1400 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1401 _NL_NUMERIC_THOUSANDS_SEP_WC);
1402 #else
1403 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1404 #endif
1406 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1407 if (*grouping == '\0' || *grouping == CHAR_MAX
1408 #ifdef COMPILE_WPRINTF
1409 || thousands_sep == L'\0'
1410 #else
1411 || *thousands_sep == '\0'
1412 #endif
1414 grouping = NULL;
1416 JUMP (*++f, step0_jumps);
1418 LABEL (flag_i18n):
1419 use_outdigits = 1;
1420 JUMP (*++f, step0_jumps);
1422 /* Get width from argument. */
1423 LABEL (width_asterics):
1425 const UCHAR_T *tmp; /* Temporary value. */
1427 tmp = ++f;
1428 if (ISDIGIT (*tmp) && read_int (&tmp) && *tmp == L_('$'))
1429 /* The width comes from a positional parameter. */
1430 goto do_positional;
1432 width = va_arg (ap, int);
1434 /* Negative width means left justified. */
1435 if (width < 0)
1437 width = -width;
1438 pad = L_(' ');
1439 left = 1;
1442 if (width + 32 >= (int) (sizeof (work_buffer)
1443 / sizeof (work_buffer[0])))
1445 /* We have to use a special buffer. The "32" is just a safe
1446 bet for all the output which is not counted in the width. */
1447 if (__libc_use_alloca ((width + 32) * sizeof (CHAR_T)))
1448 workend = ((CHAR_T *) alloca ((width + 32) * sizeof (CHAR_T))
1449 + (width + 32));
1450 else
1452 workstart = (CHAR_T *) malloc ((width + 32) * sizeof (CHAR_T));
1453 if (workstart == NULL)
1455 done = -1;
1456 goto all_done;
1458 workend = workstart + (width + 32);
1462 JUMP (*f, step1_jumps);
1464 /* Given width in format string. */
1465 LABEL (width):
1466 width = read_int (&f);
1468 if (width + 32 >= (int) (sizeof (work_buffer) / sizeof (work_buffer[0])))
1470 /* We have to use a special buffer. The "32" is just a safe
1471 bet for all the output which is not counted in the width. */
1472 if (__libc_use_alloca ((width + 32) * sizeof (CHAR_T)))
1473 workend = ((CHAR_T *) alloca ((width + 32) * sizeof (CHAR_T))
1474 + (width + 32));
1475 else
1477 workstart = (CHAR_T *) malloc ((width + 32) * sizeof (CHAR_T));
1478 if (workstart == NULL)
1480 done = -1;
1481 goto all_done;
1483 workend = workstart + (width + 32);
1486 if (*f == L_('$'))
1487 /* Oh, oh. The argument comes from a positional parameter. */
1488 goto do_positional;
1489 JUMP (*f, step1_jumps);
1491 LABEL (precision):
1492 ++f;
1493 if (*f == L_('*'))
1495 const UCHAR_T *tmp; /* Temporary value. */
1497 tmp = ++f;
1498 if (ISDIGIT (*tmp) && read_int (&tmp) > 0 && *tmp == L_('$'))
1499 /* The precision comes from a positional parameter. */
1500 goto do_positional;
1502 prec = va_arg (ap, int);
1504 /* If the precision is negative the precision is omitted. */
1505 if (prec < 0)
1506 prec = -1;
1508 else if (ISDIGIT (*f))
1509 prec = read_int (&f);
1510 else
1511 prec = 0;
1512 if (prec > width
1513 && prec + 32 > (int)(sizeof (work_buffer) / sizeof (work_buffer[0])))
1515 if (__builtin_expect (prec > ~((size_t) 0) / sizeof (CHAR_T) - 31,
1518 done = -1;
1519 goto all_done;
1521 size_t needed = ((size_t) prec + 32) * sizeof (CHAR_T);
1523 if (__libc_use_alloca (needed))
1524 workend = (((CHAR_T *) alloca (needed)) + ((size_t) prec + 32));
1525 else
1527 workstart = (CHAR_T *) malloc (needed);
1528 if (workstart == NULL)
1530 done = -1;
1531 goto all_done;
1533 workend = workstart + ((size_t) prec + 32);
1536 JUMP (*f, step2_jumps);
1538 /* Process 'h' modifier. There might another 'h' following. */
1539 LABEL (mod_half):
1540 is_short = 1;
1541 JUMP (*++f, step3a_jumps);
1543 /* Process 'hh' modifier. */
1544 LABEL (mod_halfhalf):
1545 is_short = 0;
1546 is_char = 1;
1547 JUMP (*++f, step4_jumps);
1549 /* Process 'l' modifier. There might another 'l' following. */
1550 LABEL (mod_long):
1551 is_long = 1;
1552 JUMP (*++f, step3b_jumps);
1554 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1555 allowed to follow. */
1556 LABEL (mod_longlong):
1557 is_long_double = 1;
1558 is_long = 1;
1559 JUMP (*++f, step4_jumps);
1561 LABEL (mod_size_t):
1562 is_long_double = sizeof (size_t) > sizeof (unsigned long int);
1563 is_long = sizeof (size_t) > sizeof (unsigned int);
1564 JUMP (*++f, step4_jumps);
1566 LABEL (mod_ptrdiff_t):
1567 is_long_double = sizeof (ptrdiff_t) > sizeof (unsigned long int);
1568 is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
1569 JUMP (*++f, step4_jumps);
1571 LABEL (mod_intmax_t):
1572 is_long_double = sizeof (intmax_t) > sizeof (unsigned long int);
1573 is_long = sizeof (intmax_t) > sizeof (unsigned int);
1574 JUMP (*++f, step4_jumps);
1576 /* Process current format. */
1577 while (1)
1579 process_arg (((struct printf_spec *) NULL));
1580 process_string_arg (((struct printf_spec *) NULL));
1582 LABEL (form_unknown):
1583 if (spec == L_('\0'))
1585 /* The format string ended before the specifier is complete. */
1586 done = -1;
1587 goto all_done;
1590 /* If we are in the fast loop force entering the complicated
1591 one. */
1592 goto do_positional;
1595 /* The format is correctly handled. */
1596 ++nspecs_done;
1598 if (__builtin_expect (workstart != NULL, 0))
1599 free (workstart);
1600 workstart = NULL;
1602 /* Look for next format specifier. */
1603 #ifdef COMPILE_WPRINTF
1604 f = __find_specwc ((end_of_spec = ++f));
1605 #else
1606 f = __find_specmb ((end_of_spec = ++f));
1607 #endif
1609 /* Write the following constant string. */
1610 outstring (end_of_spec, f - end_of_spec);
1612 while (*f != L_('\0'));
1614 /* Unlock stream and return. */
1615 goto all_done;
1617 /* Here starts the more complex loop to handle positional parameters. */
1618 do_positional:
1620 /* Array with information about the needed arguments. This has to
1621 be dynamically extensible. */
1622 size_t nspecs = 0;
1623 size_t nspecs_max = 32; /* A more or less arbitrary start value. */
1624 struct printf_spec *specs
1625 = alloca (nspecs_max * sizeof (struct printf_spec));
1627 /* The number of arguments the format string requests. This will
1628 determine the size of the array needed to store the argument
1629 attributes. */
1630 size_t nargs = 0;
1631 int *args_type;
1632 union printf_arg *args_value = NULL;
1633 int *args_size;
1635 /* Positional parameters refer to arguments directly. This could
1636 also determine the maximum number of arguments. Track the
1637 maximum number. */
1638 size_t max_ref_arg = 0;
1640 /* Just a counter. */
1641 size_t cnt;
1643 free (workstart);
1644 workstart = NULL;
1646 if (grouping == (const char *) -1)
1648 #ifdef COMPILE_WPRINTF
1649 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1650 _NL_NUMERIC_THOUSANDS_SEP_WC);
1651 #else
1652 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1653 #endif
1655 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1656 if (*grouping == '\0' || *grouping == CHAR_MAX)
1657 grouping = NULL;
1660 for (f = lead_str_end; *f != L_('\0'); f = specs[nspecs++].next_fmt)
1662 if (nspecs >= nspecs_max)
1664 /* Extend the array of format specifiers. */
1665 struct printf_spec *old = specs;
1666 specs = extend_alloca (specs, nspecs_max, 2 * nspecs_max);
1668 /* Copy the old array's elements to the new space. */
1669 memmove (specs, old, nspecs * sizeof (struct printf_spec));
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 #endif
1680 /* Determine the number of arguments the format string consumes. */
1681 nargs = MAX (nargs, max_ref_arg);
1683 /* Allocate memory for the argument descriptions. */
1684 args_type = alloca (nargs * sizeof (int));
1685 memset (args_type, s->_flags2 & _IO_FLAGS2_FORTIFY ? '\xff' : '\0',
1686 nargs * sizeof (int));
1687 args_value = alloca (nargs * sizeof (union printf_arg));
1688 args_size = alloca (nargs * sizeof (int));
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
1710 type and size. */
1711 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1712 args_size[specs[cnt].data_arg] = specs[cnt].size;
1713 break;
1714 default:
1715 /* We have more than one argument for this format spec.
1716 We must call the arginfo function again to determine
1717 all the types. */
1718 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1719 (&specs[cnt].info,
1720 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg],
1721 &args_size[specs[cnt].data_arg]);
1722 break;
1726 /* Now we know all the types and the order. Fill in the argument
1727 values. */
1728 for (cnt = 0; cnt < nargs; ++cnt)
1729 switch (args_type[cnt])
1731 #define T(tag, mem, type) \
1732 case tag: \
1733 args_value[cnt].mem = va_arg (ap_save, type); \
1734 break
1736 T (PA_WCHAR, pa_wchar, wint_t);
1737 case PA_CHAR: /* Promoted. */
1738 case PA_INT|PA_FLAG_SHORT: /* Promoted. */
1739 #if LONG_MAX == INT_MAX
1740 case PA_INT|PA_FLAG_LONG:
1741 #endif
1742 T (PA_INT, pa_int, int);
1743 #if LONG_MAX == LONG_LONG_MAX
1744 case PA_INT|PA_FLAG_LONG:
1745 #endif
1746 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1747 #if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
1748 # error "he?"
1749 #endif
1750 case PA_FLOAT: /* Promoted. */
1751 T (PA_DOUBLE, pa_double, double);
1752 case PA_DOUBLE|PA_FLAG_LONG_DOUBLE:
1753 if (__ldbl_is_dbl)
1755 args_value[cnt].pa_double = va_arg (ap_save, double);
1756 args_type[cnt] &= ~PA_FLAG_LONG_DOUBLE;
1758 else
1759 args_value[cnt].pa_long_double = va_arg (ap_save, long double);
1760 break;
1761 case PA_STRING: /* All pointers are the same */
1762 case PA_WSTRING: /* All pointers are the same */
1763 T (PA_POINTER, pa_pointer, void *);
1764 #undef T
1765 default:
1766 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1767 args_value[cnt].pa_pointer = va_arg (ap_save, void *);
1768 else if (__builtin_expect (__printf_va_arg_table != NULL, 0)
1769 && __printf_va_arg_table[args_type[cnt] - PA_LAST] != NULL)
1771 args_value[cnt].pa_user = alloca (args_size[cnt]);
1772 (*__printf_va_arg_table[args_type[cnt] - PA_LAST])
1773 (args_value[cnt].pa_user, &ap_save);
1775 else
1776 args_value[cnt].pa_long_double = 0.0;
1777 break;
1778 case -1:
1779 /* Error case. Not all parameters appear in N$ format
1780 strings. We have no way to determine their type. */
1781 assert (s->_flags2 & _IO_FLAGS2_FORTIFY);
1782 __libc_fatal ("*** invalid %N$ use detected ***\n");
1785 /* Now walk through all format specifiers and process them. */
1786 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1788 #undef REF
1789 #ifdef SHARED
1790 # define REF(Name) &&do2_##Name - &&do_form_unknown
1791 #else
1792 # define REF(Name) &&do2_##Name
1793 #endif
1794 #undef LABEL
1795 #define LABEL(Name) do2_##Name
1796 STEP4_TABLE;
1798 int is_negative;
1799 union
1801 unsigned long long int longlong;
1802 unsigned long int word;
1803 } number;
1804 int base;
1805 union printf_arg the_arg;
1806 CHAR_T *string; /* Pointer to argument string. */
1808 /* Fill variables from values in struct. */
1809 int alt = specs[nspecs_done].info.alt;
1810 int space = specs[nspecs_done].info.space;
1811 int left = specs[nspecs_done].info.left;
1812 int showsign = specs[nspecs_done].info.showsign;
1813 int group = specs[nspecs_done].info.group;
1814 int is_long_double = specs[nspecs_done].info.is_long_double;
1815 int is_short = specs[nspecs_done].info.is_short;
1816 int is_char = specs[nspecs_done].info.is_char;
1817 int is_long = specs[nspecs_done].info.is_long;
1818 int width = specs[nspecs_done].info.width;
1819 int prec = specs[nspecs_done].info.prec;
1820 int use_outdigits = specs[nspecs_done].info.i18n;
1821 char pad = specs[nspecs_done].info.pad;
1822 CHAR_T spec = specs[nspecs_done].info.spec;
1824 workstart = NULL;
1825 workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1827 /* Fill in last information. */
1828 if (specs[nspecs_done].width_arg != -1)
1830 /* Extract the field width from an argument. */
1831 specs[nspecs_done].info.width =
1832 args_value[specs[nspecs_done].width_arg].pa_int;
1834 if (specs[nspecs_done].info.width < 0)
1835 /* If the width value is negative left justification is
1836 selected and the value is taken as being positive. */
1838 specs[nspecs_done].info.width *= -1;
1839 left = specs[nspecs_done].info.left = 1;
1841 width = specs[nspecs_done].info.width;
1844 if (specs[nspecs_done].prec_arg != -1)
1846 /* Extract the precision from an argument. */
1847 specs[nspecs_done].info.prec =
1848 args_value[specs[nspecs_done].prec_arg].pa_int;
1850 if (specs[nspecs_done].info.prec < 0)
1851 /* If the precision is negative the precision is
1852 omitted. */
1853 specs[nspecs_done].info.prec = -1;
1855 prec = specs[nspecs_done].info.prec;
1858 /* Maybe the buffer is too small. */
1859 if (MAX (prec, width) + 32 > (int) (sizeof (work_buffer)
1860 / sizeof (CHAR_T)))
1862 if (__libc_use_alloca ((MAX (prec, width) + 32)
1863 * sizeof (CHAR_T)))
1864 workend = ((CHAR_T *) alloca ((MAX (prec, width) + 32)
1865 * sizeof (CHAR_T))
1866 + (MAX (prec, width) + 32));
1867 else
1869 workstart = (CHAR_T *) malloc ((MAX (prec, width) + 32)
1870 * sizeof (CHAR_T));
1871 workend = workstart + (MAX (prec, width) + 32);
1875 /* Process format specifiers. */
1876 while (1)
1878 extern printf_function **__printf_function_table;
1879 int function_done;
1881 if (spec <= UCHAR_MAX
1882 && __printf_function_table != NULL
1883 && __printf_function_table[(size_t) spec] != NULL)
1885 const void **ptr = alloca (specs[nspecs_done].ndata_args
1886 * sizeof (const void *));
1888 /* Fill in an array of pointers to the argument values. */
1889 for (unsigned int i = 0; i < specs[nspecs_done].ndata_args;
1890 ++i)
1891 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1893 /* Call the function. */
1894 function_done = __printf_function_table[(size_t) spec]
1895 (s, &specs[nspecs_done].info, ptr);
1897 if (function_done != -2)
1899 /* If an error occurred we don't have information
1900 about # of chars. */
1901 if (function_done < 0)
1903 done = -1;
1904 goto all_done;
1907 done_add (function_done);
1908 break;
1912 JUMP (spec, step4_jumps);
1914 process_arg ((&specs[nspecs_done]));
1915 process_string_arg ((&specs[nspecs_done]));
1917 LABEL (form_unknown):
1919 unsigned int i;
1920 const void **ptr;
1922 ptr = alloca (specs[nspecs_done].ndata_args
1923 * sizeof (const void *));
1925 /* Fill in an array of pointers to the argument values. */
1926 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
1927 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1929 /* Call the function. */
1930 function_done = printf_unknown (s, &specs[nspecs_done].info,
1931 ptr);
1933 /* If an error occurred we don't have information about #
1934 of chars. */
1935 if (function_done < 0)
1937 done = -1;
1938 goto all_done;
1941 done_add (function_done);
1943 break;
1946 free (workstart);
1947 workstart = NULL;
1949 /* Write the following constant string. */
1950 outstring (specs[nspecs_done].end_of_fmt,
1951 specs[nspecs_done].next_fmt
1952 - specs[nspecs_done].end_of_fmt);
1956 all_done:
1957 if (__builtin_expect (workstart != NULL, 0))
1958 free (workstart);
1959 /* Unlock the stream. */
1960 _IO_funlockfile (s);
1961 _IO_cleanup_region_end (0);
1963 return done;
1966 /* Handle an unknown format specifier. This prints out a canonicalized
1967 representation of the format spec itself. */
1968 static int
1969 printf_unknown (FILE *s, const struct printf_info *info,
1970 const void *const *args)
1973 int done = 0;
1974 CHAR_T work_buffer[MAX (sizeof (info->width), sizeof (info->prec)) * 3];
1975 CHAR_T *const workend
1976 = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1977 register CHAR_T *w;
1979 outchar (L_('%'));
1981 if (info->alt)
1982 outchar (L_('#'));
1983 if (info->group)
1984 outchar (L_('\''));
1985 if (info->showsign)
1986 outchar (L_('+'));
1987 else if (info->space)
1988 outchar (L_(' '));
1989 if (info->left)
1990 outchar (L_('-'));
1991 if (info->pad == L_('0'))
1992 outchar (L_('0'));
1993 if (info->i18n)
1994 outchar (L_('I'));
1996 if (info->width != 0)
1998 w = _itoa_word (info->width, workend, 10, 0);
1999 while (w < workend)
2000 outchar (*w++);
2003 if (info->prec != -1)
2005 outchar (L_('.'));
2006 w = _itoa_word (info->prec, workend, 10, 0);
2007 while (w < workend)
2008 outchar (*w++);
2011 if (info->spec != L_('\0'))
2012 outchar (info->spec);
2014 all_done:
2015 return done;
2018 /* Group the digits according to the grouping rules of the current locale.
2019 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
2020 static CHAR_T *
2021 internal_function
2022 group_number (CHAR_T *w, CHAR_T *rear_ptr, const char *grouping,
2023 #ifdef COMPILE_WPRINTF
2024 wchar_t thousands_sep
2025 #else
2026 const char *thousands_sep
2027 #endif
2030 int len;
2031 CHAR_T *src, *s;
2032 #ifndef COMPILE_WPRINTF
2033 int tlen = strlen (thousands_sep);
2034 #endif
2036 /* We treat all negative values like CHAR_MAX. */
2038 if (*grouping == CHAR_MAX || *grouping <= 0)
2039 /* No grouping should be done. */
2040 return w;
2042 len = *grouping++;
2044 /* Copy existing string so that nothing gets overwritten. */
2045 src = (CHAR_T *) alloca ((rear_ptr - w) * sizeof (CHAR_T));
2046 s = (CHAR_T *) __mempcpy (src, w,
2047 (rear_ptr - w) * sizeof (CHAR_T));
2048 w = rear_ptr;
2050 /* Process all characters in the string. */
2051 while (s > src)
2053 *--w = *--s;
2055 if (--len == 0 && s > src)
2057 /* A new group begins. */
2058 #ifdef COMPILE_WPRINTF
2059 *--w = thousands_sep;
2060 #else
2061 int cnt = tlen;
2063 *--w = thousands_sep[--cnt];
2064 while (cnt > 0);
2065 #endif
2067 if (*grouping == CHAR_MAX
2068 #if CHAR_MIN < 0
2069 || *grouping < 0
2070 #endif
2073 /* No further grouping to be done.
2074 Copy the rest of the number. */
2076 *--w = *--s;
2077 while (s > src);
2078 break;
2080 else if (*grouping != '\0')
2081 /* The previous grouping repeats ad infinitum. */
2082 len = *grouping++;
2083 else
2084 len = grouping[-1];
2087 return w;
2090 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
2091 struct helper_file
2093 struct _IO_FILE_plus _f;
2094 #ifdef COMPILE_WPRINTF
2095 struct _IO_wide_data _wide_data;
2096 #endif
2097 _IO_FILE *_put_stream;
2098 #ifdef _IO_MTSAFE_IO
2099 _IO_lock_t lock;
2100 #endif
2103 static int
2104 _IO_helper_overflow (_IO_FILE *s, int c)
2106 _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
2107 #ifdef COMPILE_WPRINTF
2108 int used = s->_wide_data->_IO_write_ptr - s->_wide_data->_IO_write_base;
2109 if (used)
2111 _IO_size_t written = _IO_sputn (target, s->_wide_data->_IO_write_base,
2112 used);
2113 if (written == 0 || written == WEOF)
2114 return WEOF;
2115 __wmemmove (s->_wide_data->_IO_write_base,
2116 s->_wide_data->_IO_write_base + written,
2117 used - written);
2118 s->_wide_data->_IO_write_ptr -= written;
2120 #else
2121 int used = s->_IO_write_ptr - s->_IO_write_base;
2122 if (used)
2124 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
2125 if (written == 0 || written == EOF)
2126 return EOF;
2127 memmove (s->_IO_write_base, s->_IO_write_base + written,
2128 used - written);
2129 s->_IO_write_ptr -= written;
2131 #endif
2132 return PUTC (c, s);
2135 #ifdef COMPILE_WPRINTF
2136 static const struct _IO_jump_t _IO_helper_jumps =
2138 JUMP_INIT_DUMMY,
2139 JUMP_INIT (finish, INTUSE(_IO_wdefault_finish)),
2140 JUMP_INIT (overflow, _IO_helper_overflow),
2141 JUMP_INIT (underflow, _IO_default_underflow),
2142 JUMP_INIT (uflow, INTUSE(_IO_default_uflow)),
2143 JUMP_INIT (pbackfail, (_IO_pbackfail_t) INTUSE(_IO_wdefault_pbackfail)),
2144 JUMP_INIT (xsputn, INTUSE(_IO_wdefault_xsputn)),
2145 JUMP_INIT (xsgetn, INTUSE(_IO_wdefault_xsgetn)),
2146 JUMP_INIT (seekoff, _IO_default_seekoff),
2147 JUMP_INIT (seekpos, _IO_default_seekpos),
2148 JUMP_INIT (setbuf, _IO_default_setbuf),
2149 JUMP_INIT (sync, _IO_default_sync),
2150 JUMP_INIT (doallocate, INTUSE(_IO_wdefault_doallocate)),
2151 JUMP_INIT (read, _IO_default_read),
2152 JUMP_INIT (write, _IO_default_write),
2153 JUMP_INIT (seek, _IO_default_seek),
2154 JUMP_INIT (close, _IO_default_close),
2155 JUMP_INIT (stat, _IO_default_stat)
2157 #else
2158 static const struct _IO_jump_t _IO_helper_jumps =
2160 JUMP_INIT_DUMMY,
2161 JUMP_INIT (finish, INTUSE(_IO_default_finish)),
2162 JUMP_INIT (overflow, _IO_helper_overflow),
2163 JUMP_INIT (underflow, _IO_default_underflow),
2164 JUMP_INIT (uflow, INTUSE(_IO_default_uflow)),
2165 JUMP_INIT (pbackfail, INTUSE(_IO_default_pbackfail)),
2166 JUMP_INIT (xsputn, INTUSE(_IO_default_xsputn)),
2167 JUMP_INIT (xsgetn, INTUSE(_IO_default_xsgetn)),
2168 JUMP_INIT (seekoff, _IO_default_seekoff),
2169 JUMP_INIT (seekpos, _IO_default_seekpos),
2170 JUMP_INIT (setbuf, _IO_default_setbuf),
2171 JUMP_INIT (sync, _IO_default_sync),
2172 JUMP_INIT (doallocate, INTUSE(_IO_default_doallocate)),
2173 JUMP_INIT (read, _IO_default_read),
2174 JUMP_INIT (write, _IO_default_write),
2175 JUMP_INIT (seek, _IO_default_seek),
2176 JUMP_INIT (close, _IO_default_close),
2177 JUMP_INIT (stat, _IO_default_stat)
2179 #endif
2181 static int
2182 internal_function
2183 buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format,
2184 _IO_va_list args)
2186 CHAR_T buf[_IO_BUFSIZ];
2187 struct helper_file helper;
2188 register _IO_FILE *hp = (_IO_FILE *) &helper._f;
2189 int result, to_flush;
2191 /* Orient the stream. */
2192 #ifdef ORIENT
2193 ORIENT;
2194 #endif
2196 /* Initialize helper. */
2197 helper._put_stream = s;
2198 #ifdef COMPILE_WPRINTF
2199 hp->_wide_data = &helper._wide_data;
2200 _IO_wsetp (hp, buf, buf + sizeof buf / sizeof (CHAR_T));
2201 hp->_mode = 1;
2202 #else
2203 _IO_setp (hp, buf, buf + sizeof buf);
2204 hp->_mode = -1;
2205 #endif
2206 hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS|_IO_USER_LOCK;
2207 #if _IO_JUMPS_OFFSET
2208 hp->_vtable_offset = 0;
2209 #endif
2210 #ifdef _IO_MTSAFE_IO
2211 hp->_lock = NULL;
2212 #endif
2213 hp->_flags2 = s->_flags2;
2214 _IO_JUMPS (&helper._f) = (struct _IO_jump_t *) &_IO_helper_jumps;
2216 /* Now print to helper instead. */
2217 #ifndef COMPILE_WPRINTF
2218 result = INTUSE(_IO_vfprintf) (hp, format, args);
2219 #else
2220 result = vfprintf (hp, format, args);
2221 #endif
2223 /* Lock stream. */
2224 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, s);
2225 _IO_flockfile (s);
2227 /* Now flush anything from the helper to the S. */
2228 #ifdef COMPILE_WPRINTF
2229 if ((to_flush = (hp->_wide_data->_IO_write_ptr
2230 - hp->_wide_data->_IO_write_base)) > 0)
2232 if ((int) _IO_sputn (s, hp->_wide_data->_IO_write_base, to_flush)
2233 != to_flush)
2234 result = -1;
2236 #else
2237 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
2239 if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
2240 result = -1;
2242 #endif
2244 /* Unlock the stream. */
2245 _IO_funlockfile (s);
2246 __libc_cleanup_region_end (0);
2248 return result;
2251 #undef vfprintf
2252 #ifdef COMPILE_WPRINTF
2253 strong_alias (_IO_vfwprintf, __vfwprintf);
2254 ldbl_weak_alias (_IO_vfwprintf, vfwprintf);
2255 #else
2256 ldbl_strong_alias (_IO_vfprintf_internal, vfprintf);
2257 ldbl_hidden_def (_IO_vfprintf_internal, vfprintf)
2258 ldbl_strong_alias (_IO_vfprintf_internal, _IO_vfprintf);
2259 #endif