No need to use __THROW in this header.
[glibc.git] / stdio-common / vfprintf.c
blobee0326e43b7dee03edb15768f966248dc6dfc9d4
1 /* Copyright (C) 1991-2002, 2003, 2004 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 #ifndef COMPILE_WPRINTF
67 # define vfprintf _IO_vfprintf
68 # define CHAR_T char
69 # define UCHAR_T unsigned char
70 # define INT_T int
71 # define L_(Str) Str
72 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
74 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
75 # define PAD(Padchar) \
76 if (width > 0) \
77 done += INTUSE(_IO_padn) (s, (Padchar), width)
78 # define PUTC(C, F) _IO_putc_unlocked (C, F)
79 # define ORIENT if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
80 return -1
81 #else
82 # define vfprintf _IO_vfwprintf
83 # define CHAR_T wchar_t
84 /* This is a hack!!! There should be a type uwchar_t. */
85 # define UCHAR_T unsigned int /* uwchar_t */
86 # define INT_T wint_t
87 # define L_(Str) L##Str
88 # define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
90 # include "_itowa.h"
92 # define PUT(F, S, N) _IO_sputn ((F), (S), (N))
93 # define PAD(Padchar) \
94 if (width > 0) \
95 done += _IO_wpadn (s, (Padchar), width)
96 # define PUTC(C, F) _IO_putwc_unlocked (C, F)
97 # define ORIENT if (_IO_fwide (s, 1) != 1) return -1
99 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
100 # define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
101 # undef EOF
102 # define EOF WEOF
103 #endif
105 #include "_i18n_number.h"
107 /* Include the shared code for parsing the format string. */
108 #include "printf-parse.h"
111 #define outchar(Ch) \
112 do \
114 register const INT_T outc = (Ch); \
115 if (PUTC (outc, s) == EOF) \
117 done = -1; \
118 goto all_done; \
120 else \
121 ++done; \
123 while (0)
125 #define outstring(String, Len) \
126 do \
128 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
130 done = -1; \
131 goto all_done; \
133 done += (Len); \
135 while (0)
137 /* For handling long_double and longlong we use the same flag. If
138 `long' and `long long' are effectively the same type define it to
139 zero. */
140 #if LONG_MAX == LONG_LONG_MAX
141 # define is_longlong 0
142 #else
143 # define is_longlong is_long_double
144 #endif
146 /* If `long' and `int' is effectively the same type we don't have to
147 handle `long separately. */
148 #if INT_MAX == LONG_MAX
149 # define is_long_num 0
150 #else
151 # define is_long_num is_long
152 #endif
155 /* Global variables. */
156 static const CHAR_T null[] = L_("(null)");
159 /* Helper function to provide temporary buffering for unbuffered streams. */
160 static int buffered_vfprintf __P ((FILE *stream, const CHAR_T *fmt, va_list))
161 __attribute__ ((noinline)) internal_function;
163 /* Handle unknown format specifier. */
164 static int printf_unknown __P ((FILE *, const struct printf_info *,
165 const void *const *));
167 /* Group digits of number string. */
168 #ifdef COMPILE_WPRINTF
169 static CHAR_T *group_number __P ((CHAR_T *, CHAR_T *, const char *, wchar_t))
170 internal_function;
171 #else
172 static CHAR_T *group_number __P ((CHAR_T *, CHAR_T *, const char *,
173 const char *)) internal_function;
174 #endif
177 /* The function itself. */
179 vfprintf (FILE *s, const CHAR_T *format, va_list ap)
181 /* The character used as thousands separator. */
182 #ifdef COMPILE_WPRINTF
183 wchar_t thousands_sep = L'\0';
184 #else
185 const char *thousands_sep = NULL;
186 #endif
188 /* The string describing the size of groups of digits. */
189 const char *grouping;
191 /* Place to accumulate the result. */
192 int done;
194 /* Current character in format string. */
195 const UCHAR_T *f;
197 /* End of leading constant string. */
198 const UCHAR_T *lead_str_end;
200 /* Points to next format specifier. */
201 const UCHAR_T *end_of_spec;
203 /* Buffer intermediate results. */
204 CHAR_T work_buffer[1000];
205 CHAR_T *workstart = NULL;
206 CHAR_T *workend;
208 /* State for restartable multibyte character handling functions. */
209 #ifndef COMPILE_WPRINTF
210 mbstate_t mbstate;
211 #endif
213 /* We have to save the original argument pointer. */
214 va_list ap_save;
216 /* Count number of specifiers we already processed. */
217 int nspecs_done;
219 /* For the %m format we may need the current `errno' value. */
220 int save_errno = errno;
223 /* This table maps a character into a number representing a
224 class. In each step there is a destination label for each
225 class. */
226 static const int jump_table[] =
228 /* ' ' */ 1, 0, 0, /* '#' */ 4,
229 0, /* '%' */ 14, 0, /* '\''*/ 6,
230 0, 0, /* '*' */ 7, /* '+' */ 2,
231 0, /* '-' */ 3, /* '.' */ 9, 0,
232 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
233 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
234 /* '8' */ 8, /* '9' */ 8, 0, 0,
235 0, 0, 0, 0,
236 0, /* 'A' */ 26, 0, /* 'C' */ 25,
237 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
238 0, /* 'I' */ 29, 0, 0,
239 /* 'L' */ 12, 0, 0, 0,
240 0, 0, 0, /* 'S' */ 21,
241 0, 0, 0, 0,
242 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
243 0, 0, 0, 0,
244 0, /* 'a' */ 26, 0, /* 'c' */ 20,
245 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
246 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
247 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
248 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
249 /* 't' */ 27, /* 'u' */ 16, 0, 0,
250 /* 'x' */ 18, 0, /* 'z' */ 13
253 #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
254 #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
255 #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
256 /* 'int' is enough and it saves some space on 64 bit systems. */
257 # define JUMP_TABLE_TYPE const int
258 # define JUMP(ChExpr, table) \
259 do \
261 int offset; \
262 void *__unbounded ptr; \
263 spec = (ChExpr); \
264 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
265 : table[CHAR_CLASS (spec)]; \
266 ptr = &&do_form_unknown + offset; \
267 goto *ptr; \
269 while (0)
270 #else
271 # define JUMP_TABLE_TYPE const void *const
272 # define JUMP(ChExpr, table) \
273 do \
275 const void *__unbounded ptr; \
276 spec = (ChExpr); \
277 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
278 : table[CHAR_CLASS (spec)]; \
279 goto *ptr; \
281 while (0)
282 #endif
284 #define STEP0_3_TABLE \
285 /* Step 0: at the beginning. */ \
286 static JUMP_TABLE_TYPE step0_jumps[30] = \
288 REF (form_unknown), \
289 REF (flag_space), /* for ' ' */ \
290 REF (flag_plus), /* for '+' */ \
291 REF (flag_minus), /* for '-' */ \
292 REF (flag_hash), /* for '<hash>' */ \
293 REF (flag_zero), /* for '0' */ \
294 REF (flag_quote), /* for '\'' */ \
295 REF (width_asterics), /* for '*' */ \
296 REF (width), /* for '1'...'9' */ \
297 REF (precision), /* for '.' */ \
298 REF (mod_half), /* for 'h' */ \
299 REF (mod_long), /* for 'l' */ \
300 REF (mod_longlong), /* for 'L', 'q' */ \
301 REF (mod_size_t), /* for 'z', 'Z' */ \
302 REF (form_percent), /* for '%' */ \
303 REF (form_integer), /* for 'd', 'i' */ \
304 REF (form_unsigned), /* for 'u' */ \
305 REF (form_octal), /* for 'o' */ \
306 REF (form_hexa), /* for 'X', 'x' */ \
307 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
308 REF (form_character), /* for 'c' */ \
309 REF (form_string), /* for 's', 'S' */ \
310 REF (form_pointer), /* for 'p' */ \
311 REF (form_number), /* for 'n' */ \
312 REF (form_strerror), /* for 'm' */ \
313 REF (form_wcharacter), /* for 'C' */ \
314 REF (form_floathex), /* for 'A', 'a' */ \
315 REF (mod_ptrdiff_t), /* for 't' */ \
316 REF (mod_intmax_t), /* for 'j' */ \
317 REF (flag_i18n), /* for 'I' */ \
318 }; \
319 /* Step 1: after processing width. */ \
320 static JUMP_TABLE_TYPE step1_jumps[30] = \
322 REF (form_unknown), \
323 REF (form_unknown), /* for ' ' */ \
324 REF (form_unknown), /* for '+' */ \
325 REF (form_unknown), /* for '-' */ \
326 REF (form_unknown), /* for '<hash>' */ \
327 REF (form_unknown), /* for '0' */ \
328 REF (form_unknown), /* for '\'' */ \
329 REF (form_unknown), /* for '*' */ \
330 REF (form_unknown), /* for '1'...'9' */ \
331 REF (precision), /* for '.' */ \
332 REF (mod_half), /* for 'h' */ \
333 REF (mod_long), /* for 'l' */ \
334 REF (mod_longlong), /* for 'L', 'q' */ \
335 REF (mod_size_t), /* for 'z', 'Z' */ \
336 REF (form_percent), /* for '%' */ \
337 REF (form_integer), /* for 'd', 'i' */ \
338 REF (form_unsigned), /* for 'u' */ \
339 REF (form_octal), /* for 'o' */ \
340 REF (form_hexa), /* for 'X', 'x' */ \
341 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
342 REF (form_character), /* for 'c' */ \
343 REF (form_string), /* for 's', 'S' */ \
344 REF (form_pointer), /* for 'p' */ \
345 REF (form_number), /* for 'n' */ \
346 REF (form_strerror), /* for 'm' */ \
347 REF (form_wcharacter), /* for 'C' */ \
348 REF (form_floathex), /* for 'A', 'a' */ \
349 REF (mod_ptrdiff_t), /* for 't' */ \
350 REF (mod_intmax_t), /* for 'j' */ \
351 REF (form_unknown) /* for 'I' */ \
352 }; \
353 /* Step 2: after processing precision. */ \
354 static JUMP_TABLE_TYPE step2_jumps[30] = \
356 REF (form_unknown), \
357 REF (form_unknown), /* for ' ' */ \
358 REF (form_unknown), /* for '+' */ \
359 REF (form_unknown), /* for '-' */ \
360 REF (form_unknown), /* for '<hash>' */ \
361 REF (form_unknown), /* for '0' */ \
362 REF (form_unknown), /* for '\'' */ \
363 REF (form_unknown), /* for '*' */ \
364 REF (form_unknown), /* for '1'...'9' */ \
365 REF (form_unknown), /* for '.' */ \
366 REF (mod_half), /* for 'h' */ \
367 REF (mod_long), /* for 'l' */ \
368 REF (mod_longlong), /* for 'L', 'q' */ \
369 REF (mod_size_t), /* for 'z', 'Z' */ \
370 REF (form_percent), /* for '%' */ \
371 REF (form_integer), /* for 'd', 'i' */ \
372 REF (form_unsigned), /* for 'u' */ \
373 REF (form_octal), /* for 'o' */ \
374 REF (form_hexa), /* for 'X', 'x' */ \
375 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
376 REF (form_character), /* for 'c' */ \
377 REF (form_string), /* for 's', 'S' */ \
378 REF (form_pointer), /* for 'p' */ \
379 REF (form_number), /* for 'n' */ \
380 REF (form_strerror), /* for 'm' */ \
381 REF (form_wcharacter), /* for 'C' */ \
382 REF (form_floathex), /* for 'A', 'a' */ \
383 REF (mod_ptrdiff_t), /* for 't' */ \
384 REF (mod_intmax_t), /* for 'j' */ \
385 REF (form_unknown) /* for 'I' */ \
386 }; \
387 /* Step 3a: after processing first 'h' modifier. */ \
388 static JUMP_TABLE_TYPE step3a_jumps[30] = \
390 REF (form_unknown), \
391 REF (form_unknown), /* for ' ' */ \
392 REF (form_unknown), /* for '+' */ \
393 REF (form_unknown), /* for '-' */ \
394 REF (form_unknown), /* for '<hash>' */ \
395 REF (form_unknown), /* for '0' */ \
396 REF (form_unknown), /* for '\'' */ \
397 REF (form_unknown), /* for '*' */ \
398 REF (form_unknown), /* for '1'...'9' */ \
399 REF (form_unknown), /* for '.' */ \
400 REF (mod_halfhalf), /* for 'h' */ \
401 REF (form_unknown), /* for 'l' */ \
402 REF (form_unknown), /* for 'L', 'q' */ \
403 REF (form_unknown), /* for 'z', 'Z' */ \
404 REF (form_percent), /* for '%' */ \
405 REF (form_integer), /* for 'd', 'i' */ \
406 REF (form_unsigned), /* for 'u' */ \
407 REF (form_octal), /* for 'o' */ \
408 REF (form_hexa), /* for 'X', 'x' */ \
409 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
410 REF (form_unknown), /* for 'c' */ \
411 REF (form_unknown), /* for 's', 'S' */ \
412 REF (form_unknown), /* for 'p' */ \
413 REF (form_number), /* for 'n' */ \
414 REF (form_unknown), /* for 'm' */ \
415 REF (form_unknown), /* for 'C' */ \
416 REF (form_unknown), /* for 'A', 'a' */ \
417 REF (form_unknown), /* for 't' */ \
418 REF (form_unknown), /* for 'j' */ \
419 REF (form_unknown) /* for 'I' */ \
420 }; \
421 /* Step 3b: after processing first 'l' modifier. */ \
422 static JUMP_TABLE_TYPE step3b_jumps[30] = \
424 REF (form_unknown), \
425 REF (form_unknown), /* for ' ' */ \
426 REF (form_unknown), /* for '+' */ \
427 REF (form_unknown), /* for '-' */ \
428 REF (form_unknown), /* for '<hash>' */ \
429 REF (form_unknown), /* for '0' */ \
430 REF (form_unknown), /* for '\'' */ \
431 REF (form_unknown), /* for '*' */ \
432 REF (form_unknown), /* for '1'...'9' */ \
433 REF (form_unknown), /* for '.' */ \
434 REF (form_unknown), /* for 'h' */ \
435 REF (mod_longlong), /* for 'l' */ \
436 REF (form_unknown), /* for 'L', 'q' */ \
437 REF (form_unknown), /* for 'z', 'Z' */ \
438 REF (form_percent), /* for '%' */ \
439 REF (form_integer), /* for 'd', 'i' */ \
440 REF (form_unsigned), /* for 'u' */ \
441 REF (form_octal), /* for 'o' */ \
442 REF (form_hexa), /* for 'X', 'x' */ \
443 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
444 REF (form_character), /* for 'c' */ \
445 REF (form_string), /* for 's', 'S' */ \
446 REF (form_pointer), /* for 'p' */ \
447 REF (form_number), /* for 'n' */ \
448 REF (form_strerror), /* for 'm' */ \
449 REF (form_wcharacter), /* for 'C' */ \
450 REF (form_floathex), /* for 'A', 'a' */ \
451 REF (form_unknown), /* for 't' */ \
452 REF (form_unknown), /* for 'j' */ \
453 REF (form_unknown) /* for 'I' */ \
456 #define STEP4_TABLE \
457 /* Step 4: processing format specifier. */ \
458 static JUMP_TABLE_TYPE step4_jumps[30] = \
460 REF (form_unknown), \
461 REF (form_unknown), /* for ' ' */ \
462 REF (form_unknown), /* for '+' */ \
463 REF (form_unknown), /* for '-' */ \
464 REF (form_unknown), /* for '<hash>' */ \
465 REF (form_unknown), /* for '0' */ \
466 REF (form_unknown), /* for '\'' */ \
467 REF (form_unknown), /* for '*' */ \
468 REF (form_unknown), /* for '1'...'9' */ \
469 REF (form_unknown), /* for '.' */ \
470 REF (form_unknown), /* for 'h' */ \
471 REF (form_unknown), /* for 'l' */ \
472 REF (form_unknown), /* for 'L', 'q' */ \
473 REF (form_unknown), /* for 'z', 'Z' */ \
474 REF (form_percent), /* for '%' */ \
475 REF (form_integer), /* for 'd', 'i' */ \
476 REF (form_unsigned), /* for 'u' */ \
477 REF (form_octal), /* for 'o' */ \
478 REF (form_hexa), /* for 'X', 'x' */ \
479 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
480 REF (form_character), /* for 'c' */ \
481 REF (form_string), /* for 's', 'S' */ \
482 REF (form_pointer), /* for 'p' */ \
483 REF (form_number), /* for 'n' */ \
484 REF (form_strerror), /* for 'm' */ \
485 REF (form_wcharacter), /* for 'C' */ \
486 REF (form_floathex), /* for 'A', 'a' */ \
487 REF (form_unknown), /* for 't' */ \
488 REF (form_unknown), /* for 'j' */ \
489 REF (form_unknown) /* for 'I' */ \
493 #define process_arg(fspec) \
494 /* Start real work. We know about all flags and modifiers and \
495 now process the wanted format specifier. */ \
496 LABEL (form_percent): \
497 /* Write a literal "%". */ \
498 outchar (L_('%')); \
499 break; \
501 LABEL (form_integer): \
502 /* Signed decimal integer. */ \
503 base = 10; \
505 if (is_longlong) \
507 long long int signed_number; \
509 if (fspec == NULL) \
510 signed_number = va_arg (ap, long long int); \
511 else \
512 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
514 is_negative = signed_number < 0; \
515 number.longlong = is_negative ? (- signed_number) : signed_number; \
517 goto LABEL (longlong_number); \
519 else \
521 long int signed_number; \
523 if (fspec == NULL) \
525 if (is_long_num) \
526 signed_number = va_arg (ap, long int); \
527 else /* `char' and `short int' will be promoted to `int'. */ \
528 signed_number = va_arg (ap, int); \
530 else \
531 if (is_long_num) \
532 signed_number = args_value[fspec->data_arg].pa_long_int; \
533 else /* `char' and `short int' will be promoted to `int'. */ \
534 signed_number = args_value[fspec->data_arg].pa_int; \
536 is_negative = signed_number < 0; \
537 number.word = is_negative ? (- signed_number) : signed_number; \
539 goto LABEL (number); \
541 /* NOTREACHED */ \
543 LABEL (form_unsigned): \
544 /* Unsigned decimal integer. */ \
545 base = 10; \
546 goto LABEL (unsigned_number); \
547 /* NOTREACHED */ \
549 LABEL (form_octal): \
550 /* Unsigned octal integer. */ \
551 base = 8; \
552 goto LABEL (unsigned_number); \
553 /* NOTREACHED */ \
555 LABEL (form_hexa): \
556 /* Unsigned hexadecimal integer. */ \
557 base = 16; \
559 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
561 /* ISO specifies the `+' and ` ' flags only for signed \
562 conversions. */ \
563 is_negative = 0; \
564 showsign = 0; \
565 space = 0; \
567 if (is_longlong) \
569 if (fspec == NULL) \
570 number.longlong = va_arg (ap, unsigned long long int); \
571 else \
572 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
574 LABEL (longlong_number): \
575 if (prec < 0) \
576 /* Supply a default precision if none was given. */ \
577 prec = 1; \
578 else \
579 /* We have to take care for the '0' flag. If a precision \
580 is given it must be ignored. */ \
581 pad = L_(' '); \
583 /* If the precision is 0 and the number is 0 nothing has to \
584 be written for the number, except for the 'o' format in \
585 alternate form. */ \
586 if (prec == 0 && number.longlong == 0) \
588 string = workend; \
589 if (base == 8 && alt) \
590 *--string = L_('0'); \
592 else \
594 /* Put the number in WORK. */ \
595 string = _itoa (number.longlong, workend, base, \
596 spec == L_('X')); \
597 if (group && grouping) \
598 string = group_number (string, workend, grouping, \
599 thousands_sep); \
601 if (use_outdigits && base == 10) \
602 string = _i18n_number_rewrite (string, workend); \
604 /* Simplify further test for num != 0. */ \
605 number.word = number.longlong != 0; \
607 else \
609 if (fspec == NULL) \
611 if (is_long_num) \
612 number.word = va_arg (ap, unsigned long int); \
613 else if (is_char) \
614 number.word = (unsigned char) va_arg (ap, unsigned int); \
615 else if (!is_short) \
616 number.word = va_arg (ap, unsigned int); \
617 else \
618 number.word = (unsigned short int) va_arg (ap, unsigned int); \
620 else \
621 if (is_long_num) \
622 number.word = args_value[fspec->data_arg].pa_u_long_int; \
623 else if (is_char) \
624 number.word = (unsigned char) \
625 args_value[fspec->data_arg].pa_u_int; \
626 else if (!is_short) \
627 number.word = args_value[fspec->data_arg].pa_u_int; \
628 else \
629 number.word = (unsigned short int) \
630 args_value[fspec->data_arg].pa_u_int; \
632 LABEL (number): \
633 if (prec < 0) \
634 /* Supply a default precision if none was given. */ \
635 prec = 1; \
636 else \
637 /* We have to take care for the '0' flag. If a precision \
638 is given it must be ignored. */ \
639 pad = L_(' '); \
641 /* If the precision is 0 and the number is 0 nothing has to \
642 be written for the number, except for the 'o' format in \
643 alternate form. */ \
644 if (prec == 0 && number.word == 0) \
646 string = workend; \
647 if (base == 8 && alt) \
648 *--string = L_('0'); \
650 else \
652 /* Put the number in WORK. */ \
653 string = _itoa_word (number.word, workend, base, \
654 spec == L_('X')); \
655 if (group && grouping) \
656 string = group_number (string, workend, grouping, \
657 thousands_sep); \
659 if (use_outdigits && base == 10) \
660 string = _i18n_number_rewrite (string, workend); \
664 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
665 /* Add octal marker. */ \
666 *--string = L_('0'); \
668 prec = MAX (0, prec - (workend - string)); \
670 if (!left) \
672 width -= workend - string + prec; \
674 if (number.word != 0 && alt && base == 16) \
675 /* Account for 0X hex marker. */ \
676 width -= 2; \
678 if (is_negative || showsign || space) \
679 --width; \
681 if (pad == L_(' ')) \
683 PAD (L_(' ')); \
684 width = 0; \
687 if (is_negative) \
688 outchar (L_('-')); \
689 else if (showsign) \
690 outchar (L_('+')); \
691 else if (space) \
692 outchar (L_(' ')); \
694 if (number.word != 0 && alt && base == 16) \
696 outchar (L_('0')); \
697 outchar (spec); \
700 width += prec; \
701 PAD (L_('0')); \
703 outstring (string, workend - string); \
705 break; \
707 else \
709 if (is_negative) \
711 outchar (L_('-')); \
712 --width; \
714 else if (showsign) \
716 outchar (L_('+')); \
717 --width; \
719 else if (space) \
721 outchar (L_(' ')); \
722 --width; \
725 if (number.word != 0 && alt && base == 16) \
727 outchar (L_('0')); \
728 outchar (spec); \
729 width -= 2; \
732 width -= workend - string + prec; \
734 if (prec > 0) \
736 int temp = width; \
737 width = prec; \
738 PAD (L_('0'));; \
739 width = temp; \
742 outstring (string, workend - string); \
744 PAD (L_(' ')); \
745 break; \
748 LABEL (form_float): \
750 /* Floating-point number. This is handled by printf_fp.c. */ \
751 const void *ptr; \
752 int function_done; \
754 if (fspec == NULL) \
756 struct printf_info info = { .prec = prec, \
757 .width = width, \
758 .spec = spec, \
759 .is_long_double = is_long_double, \
760 .is_short = is_short, \
761 .is_long = is_long, \
762 .alt = alt, \
763 .space = space, \
764 .left = left, \
765 .showsign = showsign, \
766 .group = group, \
767 .pad = pad, \
768 .extra = 0, \
769 .i18n = use_outdigits, \
770 .wide = sizeof (CHAR_T) != 1 }; \
772 if (is_long_double) \
773 the_arg.pa_long_double = va_arg (ap, long double); \
774 else \
775 the_arg.pa_double = va_arg (ap, double); \
776 ptr = (const void *) &the_arg; \
778 function_done = __printf_fp (s, &info, &ptr); \
780 else \
782 ptr = (const void *) &args_value[fspec->data_arg]; \
784 function_done = __printf_fp (s, &fspec->info, &ptr); \
787 if (function_done < 0) \
789 /* Error in print handler. */ \
790 done = -1; \
791 goto all_done; \
794 done += function_done; \
796 break; \
798 LABEL (form_floathex): \
800 /* Floating point number printed as hexadecimal number. */ \
801 const void *ptr; \
802 int function_done; \
804 if (fspec == NULL) \
806 struct printf_info info = { .prec = prec, \
807 .width = width, \
808 .spec = spec, \
809 .is_long_double = is_long_double, \
810 .is_short = is_short, \
811 .is_long = is_long, \
812 .alt = alt, \
813 .space = space, \
814 .left = left, \
815 .showsign = showsign, \
816 .group = group, \
817 .pad = pad, \
818 .extra = 0, \
819 .wide = sizeof (CHAR_T) != 1 }; \
821 if (is_long_double) \
822 the_arg.pa_long_double = va_arg (ap, long double); \
823 else \
824 the_arg.pa_double = va_arg (ap, double); \
825 ptr = (const void *) &the_arg; \
827 function_done = __printf_fphex (s, &info, &ptr); \
829 else \
831 ptr = (const void *) &args_value[fspec->data_arg]; \
833 function_done = __printf_fphex (s, &fspec->info, &ptr); \
836 if (function_done < 0) \
838 /* Error in print handler. */ \
839 done = -1; \
840 goto all_done; \
843 done += function_done; \
845 break; \
847 LABEL (form_pointer): \
848 /* Generic pointer. */ \
850 const void *ptr; \
851 if (fspec == NULL) \
852 ptr = va_arg (ap, void *); \
853 else \
854 ptr = args_value[fspec->data_arg].pa_pointer; \
855 if (ptr != NULL) \
857 /* If the pointer is not NULL, write it as a %#x spec. */ \
858 base = 16; \
859 number.word = (unsigned long int) ptr; \
860 is_negative = 0; \
861 alt = 1; \
862 group = 0; \
863 spec = L_('x'); \
864 goto LABEL (number); \
866 else \
868 /* Write "(nil)" for a nil pointer. */ \
869 string = (CHAR_T *) L_("(nil)"); \
870 /* Make sure the full string "(nil)" is printed. */ \
871 if (prec < 5) \
872 prec = 5; \
873 is_long = 0; /* This is no wide-char string. */ \
874 goto LABEL (print_string); \
877 /* NOTREACHED */ \
879 LABEL (form_number): \
880 /* Answer the count of characters written. */ \
881 if (fspec == NULL) \
883 if (is_longlong) \
884 *(long long int *) va_arg (ap, void *) = done; \
885 else if (is_long_num) \
886 *(long int *) va_arg (ap, void *) = done; \
887 else if (is_char) \
888 *(char *) va_arg (ap, void *) = done; \
889 else if (!is_short) \
890 *(int *) va_arg (ap, void *) = done; \
891 else \
892 *(short int *) va_arg (ap, void *) = done; \
894 else \
895 if (is_longlong) \
896 *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \
897 else if (is_long_num) \
898 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
899 else if (is_char) \
900 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
901 else if (!is_short) \
902 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
903 else \
904 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
905 break; \
907 LABEL (form_strerror): \
908 /* Print description of error ERRNO. */ \
909 string = \
910 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
911 sizeof work_buffer); \
912 is_long = 0; /* This is no wide-char string. */ \
913 goto LABEL (print_string)
915 #ifdef COMPILE_WPRINTF
916 # define process_string_arg(fspec) \
917 LABEL (form_character): \
918 /* Character. */ \
919 if (is_long) \
920 goto LABEL (form_wcharacter); \
921 --width; /* Account for the character itself. */ \
922 if (!left) \
923 PAD (L' '); \
924 if (fspec == NULL) \
925 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
926 else \
927 outchar (__btowc ((unsigned char) \
928 args_value[fspec->data_arg].pa_int)); \
929 if (left) \
930 PAD (L' '); \
931 break; \
933 LABEL (form_wcharacter): \
935 /* Wide character. */ \
936 --width; \
937 if (!left) \
938 PAD (L' '); \
939 if (fspec == NULL) \
940 outchar (va_arg (ap, wchar_t)); \
941 else \
942 outchar (args_value[fspec->data_arg].pa_wchar); \
943 if (left) \
944 PAD (L' '); \
946 break; \
948 LABEL (form_string): \
950 size_t len; \
951 int string_malloced; \
953 /* The string argument could in fact be `char *' or `wchar_t *'. \
954 But this should not make a difference here. */ \
955 if (fspec == NULL) \
956 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
957 else \
958 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
960 /* Entry point for printing other strings. */ \
961 LABEL (print_string): \
963 string_malloced = 0; \
964 if (string == NULL) \
966 /* Write "(null)" if there's space. */ \
967 if (prec == -1 \
968 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
970 string = (CHAR_T *) null; \
971 len = (sizeof (null) / sizeof (null[0])) - 1; \
973 else \
975 string = (CHAR_T *) L""; \
976 len = 0; \
979 else if (!is_long && spec != L_('S')) \
981 /* This is complicated. We have to transform the multibyte \
982 string into a wide character string. */ \
983 const char *mbs = (const char *) string; \
984 mbstate_t mbstate; \
986 len = prec != -1 ? (size_t) prec : strlen (mbs); \
988 /* Allocate dynamically an array which definitely is long \
989 enough for the wide character version. */ \
990 if (__libc_use_alloca (len * sizeof (wchar_t))) \
991 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
992 else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
993 == NULL) \
995 done = -1; \
996 goto all_done; \
998 else \
999 string_malloced = 1; \
1001 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1002 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
1003 if (len == (size_t) -1) \
1005 /* Illegal multibyte character. */ \
1006 done = -1; \
1007 goto all_done; \
1010 else \
1012 if (prec != -1) \
1013 /* Search for the end of the string, but don't search past \
1014 the length specified by the precision. */ \
1015 len = __wcsnlen (string, prec); \
1016 else \
1017 len = __wcslen (string); \
1020 if ((width -= len) < 0) \
1022 outstring (string, len); \
1023 break; \
1026 if (!left) \
1027 PAD (L' '); \
1028 outstring (string, len); \
1029 if (left) \
1030 PAD (L' '); \
1031 if (__builtin_expect (string_malloced, 0)) \
1032 free (string); \
1034 break;
1035 #else
1036 # define process_string_arg(fspec) \
1037 LABEL (form_character): \
1038 /* Character. */ \
1039 if (is_long) \
1040 goto LABEL (form_wcharacter); \
1041 --width; /* Account for the character itself. */ \
1042 if (!left) \
1043 PAD (' '); \
1044 if (fspec == NULL) \
1045 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
1046 else \
1047 outchar ((unsigned char) args_value[fspec->data_arg].pa_int); \
1048 if (left) \
1049 PAD (' '); \
1050 break; \
1052 LABEL (form_wcharacter): \
1054 /* Wide character. */ \
1055 char buf[MB_CUR_MAX]; \
1056 mbstate_t mbstate; \
1057 size_t len; \
1059 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1060 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t) \
1061 : args_value[fspec->data_arg].pa_wchar), \
1062 &mbstate); \
1063 if (len == (size_t) -1) \
1065 /* Something went wron gduring the conversion. Bail out. */ \
1066 done = -1; \
1067 goto all_done; \
1069 width -= len; \
1070 if (!left) \
1071 PAD (' '); \
1072 outstring (buf, len); \
1073 if (left) \
1074 PAD (' '); \
1076 break; \
1078 LABEL (form_string): \
1080 size_t len; \
1081 int string_malloced; \
1083 /* The string argument could in fact be `char *' or `wchar_t *'. \
1084 But this should not make a difference here. */ \
1085 if (fspec == NULL) \
1086 string = (char *) va_arg (ap, const char *); \
1087 else \
1088 string = (char *) args_value[fspec->data_arg].pa_string; \
1090 /* Entry point for printing other strings. */ \
1091 LABEL (print_string): \
1093 string_malloced = 0; \
1094 if (string == NULL) \
1096 /* Write "(null)" if there's space. */ \
1097 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1099 string = (char *) null; \
1100 len = sizeof (null) - 1; \
1102 else \
1104 string = (char *) ""; \
1105 len = 0; \
1108 else if (!is_long && spec != L_('S')) \
1110 if (prec != -1) \
1112 /* Search for the end of the string, but don't search past \
1113 the length (in bytes) specified by the precision. Also \
1114 don't use incomplete characters. */ \
1115 if (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MB_CUR_MAX) == 1) \
1116 len = __strnlen (string, prec); \
1117 else \
1119 /* In case we have a multibyte character set the \
1120 situation is more compilcated. We must not copy \
1121 bytes at the end which form an incomplete character. */\
1122 wchar_t ignore[prec]; \
1123 const char *str2 = string; \
1124 mbstate_t ps; \
1126 memset (&ps, '\0', sizeof (ps)); \
1127 if (__mbsnrtowcs (ignore, &str2, prec, prec, &ps) \
1128 == (size_t) -1) \
1130 done = -1; \
1131 goto all_done; \
1133 if (str2 == NULL) \
1134 len = strlen (string); \
1135 else \
1136 len = str2 - string - (ps.__count & 7); \
1139 else \
1140 len = strlen (string); \
1142 else \
1144 const wchar_t *s2 = (const wchar_t *) string; \
1145 mbstate_t mbstate; \
1147 memset (&mbstate, '\0', sizeof (mbstate_t)); \
1149 if (prec >= 0) \
1151 /* The string `s2' might not be NUL terminated. */ \
1152 if (__libc_use_alloca (prec)) \
1153 string = (char *) alloca (prec); \
1154 else if ((string = (char *) malloc (prec)) == NULL) \
1156 done = -1; \
1157 goto all_done; \
1159 else \
1160 string_malloced = 1; \
1161 len = __wcsrtombs (string, &s2, prec, &mbstate); \
1163 else \
1165 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1166 if (len != (size_t) -1) \
1168 assert (__mbsinit (&mbstate)); \
1169 s2 = (const wchar_t *) string; \
1170 if (__libc_use_alloca (len + 1)) \
1171 string = (char *) alloca (len + 1); \
1172 else if ((string = (char *) malloc (len + 1)) == NULL) \
1174 done = -1; \
1175 goto all_done; \
1177 else \
1178 string_malloced = 1; \
1179 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1183 if (len == (size_t) -1) \
1185 /* Illegal wide-character string. */ \
1186 done = -1; \
1187 goto all_done; \
1191 if ((width -= len) < 0) \
1193 outstring (string, len); \
1194 break; \
1197 if (!left) \
1198 PAD (' '); \
1199 outstring (string, len); \
1200 if (left) \
1201 PAD (' '); \
1202 if (__builtin_expect (string_malloced, 0)) \
1203 free (string); \
1205 break;
1206 #endif
1208 /* Orient the stream. */
1209 #ifdef ORIENT
1210 ORIENT;
1211 #endif
1213 /* Sanity check of arguments. */
1214 ARGCHECK (s, format);
1216 #ifdef ORIENT
1217 /* Check for correct orientation. */
1218 if (_IO_vtable_offset (s) == 0 &&
1219 _IO_fwide (s, sizeof (CHAR_T) == 1 ? -1 : 1)
1220 != (sizeof (CHAR_T) == 1 ? -1 : 1))
1221 /* The stream is already oriented otherwise. */
1222 return EOF;
1223 #endif
1225 if (UNBUFFERED_P (s))
1226 /* Use a helper function which will allocate a local temporary buffer
1227 for the stream and then call us again. */
1228 return buffered_vfprintf (s, format, ap);
1230 /* Initialize local variables. */
1231 done = 0;
1232 grouping = (const char *) -1;
1233 #ifdef __va_copy
1234 /* This macro will be available soon in gcc's <stdarg.h>. We need it
1235 since on some systems `va_list' is not an integral type. */
1236 __va_copy (ap_save, ap);
1237 #else
1238 ap_save = ap;
1239 #endif
1240 nspecs_done = 0;
1242 #ifdef COMPILE_WPRINTF
1243 /* Find the first format specifier. */
1244 f = lead_str_end = __find_specwc ((const UCHAR_T *) format);
1245 #else
1246 /* Put state for processing format string in initial state. */
1247 memset (&mbstate, '\0', sizeof (mbstate_t));
1249 /* Find the first format specifier. */
1250 f = lead_str_end = __find_specmb (format, &mbstate);
1251 #endif
1253 /* Lock stream. */
1254 _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
1255 _IO_flockfile (s);
1257 /* Write the literal text before the first format. */
1258 outstring ((const UCHAR_T *) format,
1259 lead_str_end - (const UCHAR_T *) format);
1261 /* If we only have to print a simple string, return now. */
1262 if (*f == L_('\0'))
1263 goto all_done;
1265 /* Process whole format string. */
1268 #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
1269 # define REF(Name) &&do_##Name - &&do_form_unknown
1270 #else
1271 # define REF(Name) &&do_##Name
1272 #endif
1273 #define LABEL(Name) do_##Name
1274 STEP0_3_TABLE;
1275 STEP4_TABLE;
1277 union printf_arg *args_value; /* This is not used here but ... */
1278 int is_negative; /* Flag for negative number. */
1279 union
1281 unsigned long long int longlong;
1282 unsigned long int word;
1283 } number;
1284 int base;
1285 union printf_arg the_arg;
1286 CHAR_T *string; /* Pointer to argument string. */
1287 int alt = 0; /* Alternate format. */
1288 int space = 0; /* Use space prefix if no sign is needed. */
1289 int left = 0; /* Left-justify output. */
1290 int showsign = 0; /* Always begin with plus or minus sign. */
1291 int group = 0; /* Print numbers according grouping rules. */
1292 int is_long_double = 0; /* Argument is long double/ long long int. */
1293 int is_short = 0; /* Argument is short int. */
1294 int is_long = 0; /* Argument is long int. */
1295 int is_char = 0; /* Argument is promoted (unsigned) char. */
1296 int width = 0; /* Width of output; 0 means none specified. */
1297 int prec = -1; /* Precision of output; -1 means none specified. */
1298 /* This flag is set by the 'I' modifier and selects the use of the
1299 `outdigits' as determined by the current locale. */
1300 int use_outdigits = 0;
1301 UCHAR_T pad = L_(' ');/* Padding character. */
1302 CHAR_T spec;
1304 workstart = NULL;
1305 workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1307 /* Get current character in format string. */
1308 JUMP (*++f, step0_jumps);
1310 /* ' ' flag. */
1311 LABEL (flag_space):
1312 space = 1;
1313 JUMP (*++f, step0_jumps);
1315 /* '+' flag. */
1316 LABEL (flag_plus):
1317 showsign = 1;
1318 JUMP (*++f, step0_jumps);
1320 /* The '-' flag. */
1321 LABEL (flag_minus):
1322 left = 1;
1323 pad = L_(' ');
1324 JUMP (*++f, step0_jumps);
1326 /* The '#' flag. */
1327 LABEL (flag_hash):
1328 alt = 1;
1329 JUMP (*++f, step0_jumps);
1331 /* The '0' flag. */
1332 LABEL (flag_zero):
1333 if (!left)
1334 pad = L_('0');
1335 JUMP (*++f, step0_jumps);
1337 /* The '\'' flag. */
1338 LABEL (flag_quote):
1339 group = 1;
1341 if (grouping == (const char *) -1)
1343 #ifdef COMPILE_WPRINTF
1344 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1345 _NL_NUMERIC_THOUSANDS_SEP_WC);
1346 #else
1347 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1348 #endif
1350 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1351 if (*grouping == '\0' || *grouping == CHAR_MAX
1352 #ifdef COMPILE_WPRINTF
1353 || thousands_sep == L'\0'
1354 #else
1355 || *thousands_sep == '\0'
1356 #endif
1358 grouping = NULL;
1360 JUMP (*++f, step0_jumps);
1362 LABEL (flag_i18n):
1363 use_outdigits = 1;
1364 JUMP (*++f, step0_jumps);
1366 /* Get width from argument. */
1367 LABEL (width_asterics):
1369 const UCHAR_T *tmp; /* Temporary value. */
1371 tmp = ++f;
1372 if (ISDIGIT (*tmp) && read_int (&tmp) && *tmp == L_('$'))
1373 /* The width comes from a positional parameter. */
1374 goto do_positional;
1376 width = va_arg (ap, int);
1378 /* Negative width means left justified. */
1379 if (width < 0)
1381 width = -width;
1382 pad = L_(' ');
1383 left = 1;
1386 if (width + 32 >= (int) (sizeof (work_buffer)
1387 / sizeof (work_buffer[0])))
1389 /* We have to use a special buffer. The "32" is just a safe
1390 bet for all the output which is not counted in the width. */
1391 if (__libc_use_alloca ((width + 32) * sizeof (CHAR_T)))
1392 workend = ((CHAR_T *) alloca ((width + 32) * sizeof (CHAR_T))
1393 + (width + 32));
1394 else
1396 workstart = (CHAR_T *) malloc ((width + 32) * sizeof (CHAR_T));
1397 if (workstart == NULL)
1399 done = -1;
1400 goto all_done;
1402 workend = workstart + (width + 32);
1406 JUMP (*f, step1_jumps);
1408 /* Given width in format string. */
1409 LABEL (width):
1410 width = read_int (&f);
1412 if (width + 32 >= (int) (sizeof (work_buffer) / sizeof (work_buffer[0])))
1414 /* We have to use a special buffer. The "32" is just a safe
1415 bet for all the output which is not counted in the width. */
1416 if (__libc_use_alloca ((width + 32) * sizeof (CHAR_T)))
1417 workend = ((CHAR_T *) alloca ((width + 32) * sizeof (CHAR_T))
1418 + (width + 32));
1419 else
1421 workstart = (CHAR_T *) malloc ((width + 32) * sizeof (CHAR_T));
1422 if (workstart == NULL)
1424 done = -1;
1425 goto all_done;
1427 workend = workstart + (width + 32);
1430 if (*f == L_('$'))
1431 /* Oh, oh. The argument comes from a positional parameter. */
1432 goto do_positional;
1433 JUMP (*f, step1_jumps);
1435 LABEL (precision):
1436 ++f;
1437 if (*f == L_('*'))
1439 const UCHAR_T *tmp; /* Temporary value. */
1441 tmp = ++f;
1442 if (ISDIGIT (*tmp) && read_int (&tmp) > 0 && *tmp == L_('$'))
1443 /* The precision comes from a positional parameter. */
1444 goto do_positional;
1446 prec = va_arg (ap, int);
1448 /* If the precision is negative the precision is omitted. */
1449 if (prec < 0)
1450 prec = -1;
1452 else if (ISDIGIT (*f))
1453 prec = read_int (&f);
1454 else
1455 prec = 0;
1456 if (prec > width
1457 && prec + 32 > (int)(sizeof (work_buffer) / sizeof (work_buffer[0])))
1459 if (__libc_use_alloca ((prec + 32) * sizeof (CHAR_T)))
1460 workend = ((CHAR_T *) alloca ((prec + 32) * sizeof (CHAR_T)))
1461 + (prec + 32);
1462 else
1464 workstart = (CHAR_T *) malloc ((prec + 32) * sizeof (CHAR_T));
1465 if (workstart == NULL)
1467 done = -1;
1468 goto all_done;
1470 workend = workstart + (prec + 32);
1473 JUMP (*f, step2_jumps);
1475 /* Process 'h' modifier. There might another 'h' following. */
1476 LABEL (mod_half):
1477 is_short = 1;
1478 JUMP (*++f, step3a_jumps);
1480 /* Process 'hh' modifier. */
1481 LABEL (mod_halfhalf):
1482 is_short = 0;
1483 is_char = 1;
1484 JUMP (*++f, step4_jumps);
1486 /* Process 'l' modifier. There might another 'l' following. */
1487 LABEL (mod_long):
1488 is_long = 1;
1489 JUMP (*++f, step3b_jumps);
1491 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1492 allowed to follow. */
1493 LABEL (mod_longlong):
1494 is_long_double = 1;
1495 is_long = 1;
1496 JUMP (*++f, step4_jumps);
1498 LABEL (mod_size_t):
1499 is_long_double = sizeof (size_t) > sizeof (unsigned long int);
1500 is_long = sizeof (size_t) > sizeof (unsigned int);
1501 JUMP (*++f, step4_jumps);
1503 LABEL (mod_ptrdiff_t):
1504 is_long_double = sizeof (ptrdiff_t) > sizeof (unsigned long int);
1505 is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
1506 JUMP (*++f, step4_jumps);
1508 LABEL (mod_intmax_t):
1509 is_long_double = sizeof (intmax_t) > sizeof (unsigned long int);
1510 is_long = sizeof (intmax_t) > sizeof (unsigned int);
1511 JUMP (*++f, step4_jumps);
1513 /* Process current format. */
1514 while (1)
1516 process_arg (((struct printf_spec *) NULL));
1517 process_string_arg (((struct printf_spec *) NULL));
1519 LABEL (form_unknown):
1520 if (spec == L_('\0'))
1522 /* The format string ended before the specifier is complete. */
1523 done = -1;
1524 goto all_done;
1527 /* If we are in the fast loop force entering the complicated
1528 one. */
1529 goto do_positional;
1532 /* The format is correctly handled. */
1533 ++nspecs_done;
1535 if (__builtin_expect (workstart != NULL, 0))
1536 free (workstart);
1537 workstart = NULL;
1539 /* Look for next format specifier. */
1540 #ifdef COMPILE_WPRINTF
1541 f = __find_specwc ((end_of_spec = ++f));
1542 #else
1543 f = __find_specmb ((end_of_spec = ++f), &mbstate);
1544 #endif
1546 /* Write the following constant string. */
1547 outstring (end_of_spec, f - end_of_spec);
1549 while (*f != L_('\0'));
1551 /* Unlock stream and return. */
1552 goto all_done;
1554 /* Here starts the more complex loop to handle positional parameters. */
1555 do_positional:
1557 /* Array with information about the needed arguments. This has to
1558 be dynamically extensible. */
1559 size_t nspecs = 0;
1560 size_t nspecs_max = 32; /* A more or less arbitrary start value. */
1561 struct printf_spec *specs
1562 = alloca (nspecs_max * sizeof (struct printf_spec));
1564 /* The number of arguments the format string requests. This will
1565 determine the size of the array needed to store the argument
1566 attributes. */
1567 size_t nargs = 0;
1568 int *args_type;
1569 union printf_arg *args_value = NULL;
1571 /* Positional parameters refer to arguments directly. This could
1572 also determine the maximum number of arguments. Track the
1573 maximum number. */
1574 size_t max_ref_arg = 0;
1576 /* Just a counter. */
1577 size_t cnt;
1580 if (grouping == (const char *) -1)
1582 #ifdef COMPILE_WPRINTF
1583 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1584 _NL_NUMERIC_THOUSANDS_SEP_WC);
1585 #else
1586 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1587 #endif
1589 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1590 if (*grouping == '\0' || *grouping == CHAR_MAX)
1591 grouping = NULL;
1594 for (f = lead_str_end; *f != L_('\0'); f = specs[nspecs++].next_fmt)
1596 if (nspecs >= nspecs_max)
1598 /* Extend the array of format specifiers. */
1599 struct printf_spec *old = specs;
1601 nspecs_max *= 2;
1602 specs = alloca (nspecs_max * sizeof (struct printf_spec));
1604 if (specs == &old[nspecs])
1605 /* Stack grows up, OLD was the last thing allocated;
1606 extend it. */
1607 nspecs_max += nspecs_max / 2;
1608 else
1610 /* Copy the old array's elements to the new space. */
1611 memcpy (specs, old, nspecs * sizeof (struct printf_spec));
1612 if (old == &specs[nspecs])
1613 /* Stack grows down, OLD was just below the new
1614 SPECS. We can use that space when the new space
1615 runs out. */
1616 nspecs_max += nspecs_max / 2;
1620 /* Parse the format specifier. */
1621 #ifdef COMPILE_WPRINTF
1622 nargs += __parse_one_specwc (f, nargs, &specs[nspecs], &max_ref_arg);
1623 #else
1624 nargs += __parse_one_specmb (f, nargs, &specs[nspecs], &max_ref_arg,
1625 &mbstate);
1626 #endif
1629 /* Determine the number of arguments the format string consumes. */
1630 nargs = MAX (nargs, max_ref_arg);
1632 /* Allocate memory for the argument descriptions. */
1633 args_type = alloca (nargs * sizeof (int));
1634 memset (args_type, 0, nargs * sizeof (int));
1635 args_value = alloca (nargs * sizeof (union printf_arg));
1637 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1638 still zero after this loop, format is invalid. For now we
1639 simply use 0 as the value. */
1641 /* Fill in the types of all the arguments. */
1642 for (cnt = 0; cnt < nspecs; ++cnt)
1644 /* If the width is determined by an argument this is an int. */
1645 if (specs[cnt].width_arg != -1)
1646 args_type[specs[cnt].width_arg] = PA_INT;
1648 /* If the precision is determined by an argument this is an int. */
1649 if (specs[cnt].prec_arg != -1)
1650 args_type[specs[cnt].prec_arg] = PA_INT;
1652 switch (specs[cnt].ndata_args)
1654 case 0: /* No arguments. */
1655 break;
1656 case 1: /* One argument; we already have the type. */
1657 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1658 break;
1659 default:
1660 /* We have more than one argument for this format spec.
1661 We must call the arginfo function again to determine
1662 all the types. */
1663 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1664 (&specs[cnt].info,
1665 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg]);
1666 break;
1670 /* Now we know all the types and the order. Fill in the argument
1671 values. */
1672 for (cnt = 0; cnt < nargs; ++cnt)
1673 switch (args_type[cnt])
1675 #define T(tag, mem, type) \
1676 case tag: \
1677 args_value[cnt].mem = va_arg (ap_save, type); \
1678 break
1680 T (PA_CHAR, pa_int, int); /* Promoted. */
1681 T (PA_WCHAR, pa_wchar, wint_t);
1682 T (PA_INT|PA_FLAG_SHORT, pa_int, int); /* Promoted. */
1683 T (PA_INT, pa_int, int);
1684 T (PA_INT|PA_FLAG_LONG, pa_long_int, long int);
1685 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1686 T (PA_FLOAT, pa_double, double); /* Promoted. */
1687 T (PA_DOUBLE, pa_double, double);
1688 T (PA_DOUBLE|PA_FLAG_LONG_DOUBLE, pa_long_double, long double);
1689 T (PA_STRING, pa_string, const char *);
1690 T (PA_WSTRING, pa_wstring, const wchar_t *);
1691 T (PA_POINTER, pa_pointer, void *);
1692 #undef T
1693 default:
1694 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
1695 args_value[cnt].pa_pointer = va_arg (ap_save, void *);
1696 else
1697 args_value[cnt].pa_long_double = 0.0;
1698 break;
1701 /* Now walk through all format specifiers and process them. */
1702 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
1704 #undef REF
1705 #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
1706 # define REF(Name) &&do2_##Name - &&do_form_unknown
1707 #else
1708 # define REF(Name) &&do2_##Name
1709 #endif
1710 #undef LABEL
1711 #define LABEL(Name) do2_##Name
1712 STEP4_TABLE;
1714 int is_negative;
1715 union
1717 unsigned long long int longlong;
1718 unsigned long int word;
1719 } number;
1720 int base;
1721 union printf_arg the_arg;
1722 CHAR_T *string; /* Pointer to argument string. */
1724 /* Fill variables from values in struct. */
1725 int alt = specs[nspecs_done].info.alt;
1726 int space = specs[nspecs_done].info.space;
1727 int left = specs[nspecs_done].info.left;
1728 int showsign = specs[nspecs_done].info.showsign;
1729 int group = specs[nspecs_done].info.group;
1730 int is_long_double = specs[nspecs_done].info.is_long_double;
1731 int is_short = specs[nspecs_done].info.is_short;
1732 int is_char = specs[nspecs_done].info.is_char;
1733 int is_long = specs[nspecs_done].info.is_long;
1734 int width = specs[nspecs_done].info.width;
1735 int prec = specs[nspecs_done].info.prec;
1736 int use_outdigits = specs[nspecs_done].info.i18n;
1737 char pad = specs[nspecs_done].info.pad;
1738 CHAR_T spec = specs[nspecs_done].info.spec;
1739 CHAR_T *workstart = NULL;
1741 /* Fill in last information. */
1742 if (specs[nspecs_done].width_arg != -1)
1744 /* Extract the field width from an argument. */
1745 specs[nspecs_done].info.width =
1746 args_value[specs[nspecs_done].width_arg].pa_int;
1748 if (specs[nspecs_done].info.width < 0)
1749 /* If the width value is negative left justification is
1750 selected and the value is taken as being positive. */
1752 specs[nspecs_done].info.width *= -1;
1753 left = specs[nspecs_done].info.left = 1;
1755 width = specs[nspecs_done].info.width;
1758 if (specs[nspecs_done].prec_arg != -1)
1760 /* Extract the precision from an argument. */
1761 specs[nspecs_done].info.prec =
1762 args_value[specs[nspecs_done].prec_arg].pa_int;
1764 if (specs[nspecs_done].info.prec < 0)
1765 /* If the precision is negative the precision is
1766 omitted. */
1767 specs[nspecs_done].info.prec = -1;
1769 prec = specs[nspecs_done].info.prec;
1772 /* Maybe the buffer is too small. */
1773 if (MAX (prec, width) + 32 > (int) (sizeof (work_buffer)
1774 / sizeof (CHAR_T)))
1776 if (__libc_use_alloca ((MAX (prec, width) + 32)
1777 * sizeof (CHAR_T)))
1778 workend = ((CHAR_T *) alloca ((MAX (prec, width) + 32)
1779 * sizeof (CHAR_T))
1780 + (MAX (prec, width) + 32));
1781 else
1783 workstart = (CHAR_T *) malloc ((MAX (prec, width) + 32)
1784 * sizeof (CHAR_T));
1785 workend = workstart + (MAX (prec, width) + 32);
1789 /* Process format specifiers. */
1790 while (1)
1792 JUMP (spec, step4_jumps);
1794 process_arg ((&specs[nspecs_done]));
1795 process_string_arg ((&specs[nspecs_done]));
1797 LABEL (form_unknown):
1799 extern printf_function **__printf_function_table;
1800 int function_done;
1801 printf_function *function;
1802 unsigned int i;
1803 const void **ptr;
1805 function =
1806 (__printf_function_table == NULL ? NULL :
1807 __printf_function_table[specs[nspecs_done].info.spec]);
1809 if (function == NULL)
1810 function = &printf_unknown;
1812 ptr = alloca (specs[nspecs_done].ndata_args
1813 * sizeof (const void *));
1815 /* Fill in an array of pointers to the argument values. */
1816 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
1817 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
1819 /* Call the function. */
1820 function_done = (*function) (s, &specs[nspecs_done].info, ptr);
1822 /* If an error occurred we don't have information about #
1823 of chars. */
1824 if (function_done < 0)
1826 done = -1;
1827 goto all_done;
1830 done += function_done;
1832 break;
1835 if (__builtin_expect (workstart != NULL, 0))
1836 free (workstart);
1837 workstart = NULL;
1839 /* Write the following constant string. */
1840 outstring (specs[nspecs_done].end_of_fmt,
1841 specs[nspecs_done].next_fmt
1842 - specs[nspecs_done].end_of_fmt);
1846 all_done:
1847 if (__builtin_expect (workstart != NULL, 0))
1848 free (workstart);
1849 /* Unlock the stream. */
1850 _IO_funlockfile (s);
1851 _IO_cleanup_region_end (0);
1853 return done;
1856 /* Handle an unknown format specifier. This prints out a canonicalized
1857 representation of the format spec itself. */
1858 static int
1859 printf_unknown (FILE *s, const struct printf_info *info,
1860 const void *const *args)
1863 int done = 0;
1864 CHAR_T work_buffer[MAX (info->width, info->spec) + 32];
1865 CHAR_T *const workend
1866 = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
1867 register CHAR_T *w;
1869 outchar (L_('%'));
1871 if (info->alt)
1872 outchar (L_('#'));
1873 if (info->group)
1874 outchar (L_('\''));
1875 if (info->showsign)
1876 outchar (L_('+'));
1877 else if (info->space)
1878 outchar (L_(' '));
1879 if (info->left)
1880 outchar (L_('-'));
1881 if (info->pad == L_('0'))
1882 outchar (L_('0'));
1883 if (info->i18n)
1884 outchar (L_('I'));
1886 if (info->width != 0)
1888 w = _itoa_word (info->width, workend, 10, 0);
1889 while (w < workend)
1890 outchar (*w++);
1893 if (info->prec != -1)
1895 outchar (L_('.'));
1896 w = _itoa_word (info->prec, workend, 10, 0);
1897 while (w < workend)
1898 outchar (*w++);
1901 if (info->spec != L_('\0'))
1902 outchar (info->spec);
1904 all_done:
1905 return done;
1908 /* Group the digits according to the grouping rules of the current locale.
1909 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
1910 static CHAR_T *
1911 internal_function
1912 group_number (CHAR_T *w, CHAR_T *rear_ptr, const char *grouping,
1913 #ifdef COMPILE_WPRINTF
1914 wchar_t thousands_sep
1915 #else
1916 const char *thousands_sep
1917 #endif
1920 int len;
1921 CHAR_T *src, *s;
1922 #ifndef COMPILE_WPRINTF
1923 int tlen = strlen (thousands_sep);
1924 #endif
1926 /* We treat all negative values like CHAR_MAX. */
1928 if (*grouping == CHAR_MAX || *grouping <= 0)
1929 /* No grouping should be done. */
1930 return w;
1932 len = *grouping++;
1934 /* Copy existing string so that nothing gets overwritten. */
1935 src = (CHAR_T *) alloca ((rear_ptr - w) * sizeof (CHAR_T));
1936 s = (CHAR_T *) __mempcpy (src, w,
1937 (rear_ptr - w) * sizeof (CHAR_T));
1938 w = rear_ptr;
1940 /* Process all characters in the string. */
1941 while (s > src)
1943 *--w = *--s;
1945 if (--len == 0 && s > src)
1947 /* A new group begins. */
1948 #ifdef COMPILE_WPRINTF
1949 *--w = thousands_sep;
1950 #else
1951 int cnt = tlen;
1953 *--w = thousands_sep[--cnt];
1954 while (cnt > 0);
1955 #endif
1957 if (*grouping == CHAR_MAX
1958 #if CHAR_MIN < 0
1959 || *grouping < 0
1960 #endif
1963 /* No further grouping to be done.
1964 Copy the rest of the number. */
1966 *--w = *--s;
1967 while (s > src);
1968 break;
1970 else if (*grouping != '\0')
1971 /* The previous grouping repeats ad infinitum. */
1972 len = *grouping++;
1973 else
1974 len = grouping[-1];
1977 return w;
1980 /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
1981 struct helper_file
1983 struct _IO_FILE_plus _f;
1984 #ifdef COMPILE_WPRINTF
1985 struct _IO_wide_data _wide_data;
1986 #endif
1987 _IO_FILE *_put_stream;
1988 #ifdef _IO_MTSAFE_IO
1989 _IO_lock_t lock;
1990 #endif
1993 static int
1994 _IO_helper_overflow (_IO_FILE *s, int c)
1996 _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
1997 #ifdef COMPILE_WPRINTF
1998 int used = s->_wide_data->_IO_write_ptr - s->_wide_data->_IO_write_base;
1999 if (used)
2001 _IO_size_t written = _IO_sputn (target, s->_wide_data->_IO_write_base,
2002 used);
2003 s->_wide_data->_IO_write_ptr -= written;
2005 #else
2006 int used = s->_IO_write_ptr - s->_IO_write_base;
2007 if (used)
2009 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
2010 s->_IO_write_ptr -= written;
2012 #endif
2013 return PUTC (c, s);
2016 #ifdef COMPILE_WPRINTF
2017 static const struct _IO_jump_t _IO_helper_jumps =
2019 JUMP_INIT_DUMMY,
2020 JUMP_INIT (finish, INTUSE(_IO_wdefault_finish)),
2021 JUMP_INIT (overflow, _IO_helper_overflow),
2022 JUMP_INIT (underflow, _IO_default_underflow),
2023 JUMP_INIT (uflow, INTUSE(_IO_default_uflow)),
2024 JUMP_INIT (pbackfail, (_IO_pbackfail_t) INTUSE(_IO_wdefault_pbackfail)),
2025 JUMP_INIT (xsputn, INTUSE(_IO_wdefault_xsputn)),
2026 JUMP_INIT (xsgetn, INTUSE(_IO_wdefault_xsgetn)),
2027 JUMP_INIT (seekoff, _IO_default_seekoff),
2028 JUMP_INIT (seekpos, _IO_default_seekpos),
2029 JUMP_INIT (setbuf, _IO_default_setbuf),
2030 JUMP_INIT (sync, _IO_default_sync),
2031 JUMP_INIT (doallocate, INTUSE(_IO_wdefault_doallocate)),
2032 JUMP_INIT (read, _IO_default_read),
2033 JUMP_INIT (write, _IO_default_write),
2034 JUMP_INIT (seek, _IO_default_seek),
2035 JUMP_INIT (close, _IO_default_close),
2036 JUMP_INIT (stat, _IO_default_stat)
2038 #else
2039 static const struct _IO_jump_t _IO_helper_jumps =
2041 JUMP_INIT_DUMMY,
2042 JUMP_INIT (finish, INTUSE(_IO_default_finish)),
2043 JUMP_INIT (overflow, _IO_helper_overflow),
2044 JUMP_INIT (underflow, _IO_default_underflow),
2045 JUMP_INIT (uflow, INTUSE(_IO_default_uflow)),
2046 JUMP_INIT (pbackfail, INTUSE(_IO_default_pbackfail)),
2047 JUMP_INIT (xsputn, INTUSE(_IO_default_xsputn)),
2048 JUMP_INIT (xsgetn, INTUSE(_IO_default_xsgetn)),
2049 JUMP_INIT (seekoff, _IO_default_seekoff),
2050 JUMP_INIT (seekpos, _IO_default_seekpos),
2051 JUMP_INIT (setbuf, _IO_default_setbuf),
2052 JUMP_INIT (sync, _IO_default_sync),
2053 JUMP_INIT (doallocate, INTUSE(_IO_default_doallocate)),
2054 JUMP_INIT (read, _IO_default_read),
2055 JUMP_INIT (write, _IO_default_write),
2056 JUMP_INIT (seek, _IO_default_seek),
2057 JUMP_INIT (close, _IO_default_close),
2058 JUMP_INIT (stat, _IO_default_stat)
2060 #endif
2062 static int
2063 internal_function
2064 buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format,
2065 _IO_va_list args)
2067 CHAR_T buf[_IO_BUFSIZ];
2068 struct helper_file helper;
2069 register _IO_FILE *hp = (_IO_FILE *) &helper._f;
2070 int result, to_flush;
2072 /* Orient the stream. */
2073 #ifdef ORIENT
2074 ORIENT;
2075 #endif
2077 /* Initialize helper. */
2078 helper._put_stream = s;
2079 #ifdef COMPILE_WPRINTF
2080 hp->_wide_data = &helper._wide_data;
2081 _IO_wsetp (hp, buf, buf + sizeof buf / sizeof (CHAR_T));
2082 hp->_mode = 1;
2083 #else
2084 _IO_setp (hp, buf, buf + sizeof buf);
2085 hp->_mode = -1;
2086 #endif
2087 hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS|_IO_USER_LOCK;
2088 #if _IO_JUMPS_OFFSET
2089 hp->_vtable_offset = 0;
2090 #endif
2091 #ifdef _IO_MTSAFE_IO
2092 hp->_lock = NULL;
2093 #endif
2094 _IO_JUMPS (&helper._f) = (struct _IO_jump_t *) &_IO_helper_jumps;
2096 /* Now print to helper instead. */
2097 #ifndef COMPILE_WPRINTF
2098 result = INTUSE(_IO_vfprintf) (hp, format, args);
2099 #else
2100 result = vfprintf (hp, format, args);
2101 #endif
2103 /* Lock stream. */
2104 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, s);
2105 _IO_flockfile (s);
2107 /* Now flush anything from the helper to the S. */
2108 #ifdef COMPILE_WPRINTF
2109 if ((to_flush = (hp->_wide_data->_IO_write_ptr
2110 - hp->_wide_data->_IO_write_base)) > 0)
2112 if ((int) _IO_sputn (s, hp->_wide_data->_IO_write_base, to_flush)
2113 != to_flush)
2114 result = -1;
2116 #else
2117 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
2119 if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
2120 result = -1;
2122 #endif
2124 /* Unlock the stream. */
2125 _IO_funlockfile (s);
2126 __libc_cleanup_region_end (0);
2128 return result;
2131 #undef vfprintf
2132 #ifdef strong_alias
2133 /* This is for glibc. */
2134 # ifdef COMPILE_WPRINTF
2135 strong_alias (_IO_vfwprintf, __vfwprintf);
2136 weak_alias (_IO_vfwprintf, vfwprintf);
2137 # else
2138 strong_alias (_IO_vfprintf, vfprintf);
2139 libc_hidden_def (vfprintf)
2140 INTDEF(_IO_vfprintf)
2141 # endif
2142 #else
2143 # if defined __ELF__ || defined __GNU_LIBRARY__
2144 # include <gnu-stabs.h>
2145 # ifdef weak_alias
2146 # ifdef COMPILE_WPRINTF
2147 weak_alias (_IO_vfwprintf, vfwprintf);
2148 # else
2149 weak_alias (_IO_vfprintf, vfprintf);
2150 # endif
2151 # endif
2152 # endif
2153 #endif