]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdio-common/vfprintf.c
Update.
[thirdparty/glibc.git] / stdio-common / vfprintf.c
CommitLineData
77fe0b9c 1/* Copyright (C) 1991-1999, 2000, 2001, 2002 Free Software Foundation, Inc.
2c6fe0bd 2 This file is part of the GNU C Library.
28f540f4 3
2c6fe0bd 4 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
28f540f4 8
2c6fe0bd
UD
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
41bdb6e2 12 Lesser General Public License for more details.
28f540f4 13
41bdb6e2
AJ
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. */
28f540f4 18
28f540f4 19#include <ctype.h>
28f540f4 20#include <limits.h>
a04e7405 21#include <printf.h>
28f540f4 22#include <stdarg.h>
e852e889 23#include <stdint.h>
28f540f4 24#include <stdlib.h>
69c69fe1 25#include <string.h>
66aeca9c 26#include <errno.h>
299a95b9 27#include <wchar.h>
5107cf1d 28#include <bits/libc-lock.h>
01c901a5 29#include <sys/param.h>
28f540f4 30#include "_itoa.h"
cc3fa755 31#include <locale/localeinfo.h>
a04e7405 32
299a95b9
RM
33/* This code is shared between the standard stdio implementation found
34 in GNU C library and the libio implementation originally found in
35 GNU libg++.
36
37 Beside this it is also shared between the normal and wide character
38 implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */
39
28f540f4 40
28f540f4
RM
41#ifdef USE_IN_LIBIO
42/* This code is for use in libio. */
299a95b9 43# include <libioP.h>
299a95b9 44# define FILE _IO_FILE
a68b0d31 45# undef va_list
299a95b9 46# define va_list _IO_va_list
d64b6ad0 47# undef BUFSIZ
299a95b9 48# define BUFSIZ _IO_BUFSIZ
d64b6ad0 49# define ARGCHECK(S, Format) \
28f540f4
RM
50 do \
51 { \
52 /* Check file argument for consistence. */ \
299a95b9 53 CHECK_FILE (S, -1); \
68dbb3a6
UD
54 if (S->_flags & _IO_NO_WRITES) \
55 { \
56 __set_errno (EBADF); \
57 return -1; \
58 } \
59 if (Format == NULL) \
28f540f4
RM
60 { \
61 MAYBE_SET_EINVAL; \
62 return -1; \
63 } \
64 } while (0)
299a95b9 65# define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
d64b6ad0
UD
66
67# ifndef COMPILE_WPRINTF
68# define vfprintf _IO_vfprintf
69# define CHAR_T char
70# define UCHAR_T unsigned char
71# define INT_T int
72# define L_(Str) Str
69c69fe1 73# define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10)
d64b6ad0
UD
74
75# define PUT(F, S, N) _IO_sputn ((F), (S), (N))
76# define PAD(Padchar) \
77 if (width > 0) \
77fe0b9c 78 done += INTUSE(_IO_padn) (s, (Padchar), width)
d64b6ad0 79# define PUTC(C, F) _IO_putc_unlocked (C, F)
c9eaa8b9
UD
80# define ORIENT if (s->_vtable_offset == 0 && _IO_fwide (s, -1) != -1)\
81 return -1
d64b6ad0 82# else
d64b6ad0
UD
83# define vfprintf _IO_vfwprintf
84# define CHAR_T wchar_t
85/* This is a hack!!! There should be a type uwchar_t. */
86# define UCHAR_T unsigned int /* uwchar_t */
87# define INT_T wint_t
88# define L_(Str) L##Str
69c69fe1
UD
89# define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10)
90
91# include "_itowa.h"
d64b6ad0
UD
92
93# define PUT(F, S, N) _IO_sputn ((F), (S), (N))
94# define PAD(Padchar) \
95 if (width > 0) \
96 done += _IO_wpadn (s, (Padchar), width)
97# define PUTC(C, F) _IO_putwc_unlocked (C, F)
98# define ORIENT if (_IO_fwide (s, 1) != 1) return -1
99
655c0697
UD
100# define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
101# define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
d64b6ad0
UD
102# undef EOF
103# define EOF WEOF
104# endif
28f540f4
RM
105#else /* ! USE_IN_LIBIO */
106/* This code is for use in the GNU C library. */
299a95b9 107# include <stdio.h>
d64b6ad0 108# define ARGCHECK(S, Format) \
28f540f4
RM
109 do \
110 { \
111 /* Check file argument for consistence. */ \
68dbb3a6
UD
112 if (!__validfp (S) || !S->__mode.__write) \
113 { \
114 __set_errno (EBADF); \
115 return -1; \
116 } \
117 if (Format == NULL) \
28f540f4 118 { \
c4029823 119 __set_errno (EINVAL); \
28f540f4
RM
120 return -1; \
121 } \
299a95b9 122 if (!S->__seen) \
28f540f4 123 { \
299a95b9 124 if (__flshfp (S, EOF) == EOF) \
28f540f4
RM
125 return -1; \
126 } \
299a95b9
RM
127 } \
128 while (0)
129# define UNBUFFERED_P(s) ((s)->__buffer == NULL)
46ec036d 130
ab58d620
UD
131# define CHAR_T char
132# define UCHAR_T unsigned char
133# define INT_T int
134# define L_(Str) Str
135# define ISDIGIT(Ch) isdigit (Ch)
136
137# define PUT(F, S, N) fwrite (S, 1, N, F)
138ssize_t __printf_pad __P ((FILE *, char pad, size_t n));
139# define PAD(Padchar) \
140 if (width > 0) \
141 { ssize_t __res = __printf_pad (s, (Padchar), width); \
142 if (__res == -1) \
143 { \
144 done = -1; \
145 goto all_done; \
146 } \
147 done += __res; }
148# define PUTC(C, F) putc (C, F)
149
46ec036d
UD
150/* XXX These declarations should go as soon as the stdio header files
151 have these prototypes. */
152extern void __flockfile (FILE *);
153extern void __funlockfile (FILE *);
28f540f4
RM
154#endif /* USE_IN_LIBIO */
155
a9706118
UD
156#include "_i18n_number.h"
157
d64b6ad0
UD
158/* Include the shared code for parsing the format string. */
159#include "printf-parse.h"
160
28f540f4 161
299a95b9 162#define outchar(Ch) \
28f540f4
RM
163 do \
164 { \
d64b6ad0 165 register const INT_T outc = (Ch); \
77a58cad 166 if (PUTC (outc, s) == EOF) \
b0882748
UD
167 { \
168 done = -1; \
169 goto all_done; \
170 } \
28f540f4
RM
171 else \
172 ++done; \
299a95b9
RM
173 } \
174 while (0)
28f540f4 175
299a95b9 176#define outstring(String, Len) \
28f540f4
RM
177 do \
178 { \
ba1ffaa1 179 if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \
b0882748
UD
180 { \
181 done = -1; \
182 goto all_done; \
183 } \
ba1ffaa1 184 done += (Len); \
299a95b9
RM
185 } \
186 while (0)
28f540f4 187
0ae97dea
UD
188/* For handling long_double and longlong we use the same flag. If
189 `long' and `long long' are effectively the same type define it to
190 zero. */
191#if LONG_MAX == LONG_LONG_MAX
192# define is_longlong 0
193#else
299a95b9
RM
194# define is_longlong is_long_double
195#endif
196
859a09cf
UD
197/* If `long' and `int' is effectively the same type we don't have to
198 handle `long separately. */
199#if INT_MAX == LONG_MAX
200# define is_long_num 0
201#else
202# define is_long_num is_long
203#endif
204
fafaa44e 205
299a95b9 206/* Global variables. */
d64b6ad0 207static const CHAR_T null[] = L_("(null)");
28f540f4 208
28f540f4 209
299a95b9 210/* Helper function to provide temporary buffering for unbuffered streams. */
dfd2257a
UD
211static int buffered_vfprintf __P ((FILE *stream, const CHAR_T *fmt, va_list))
212 internal_function;
299a95b9
RM
213
214/* Handle unknown format specifier. */
215static int printf_unknown __P ((FILE *, const struct printf_info *,
216 const void *const *));
28f540f4 217
299a95b9 218/* Group digits of number string. */
4295702f 219#ifdef COMPILE_WPRINTF
655c0697 220static CHAR_T *group_number __P ((CHAR_T *, CHAR_T *, const char *, wchar_t))
dfd2257a 221 internal_function;
4295702f
UD
222#else
223static CHAR_T *group_number __P ((CHAR_T *, CHAR_T *, const char *,
224 const char *)) internal_function;
225#endif
28f540f4 226
a04e7405 227
299a95b9 228/* The function itself. */
28f540f4 229int
299a95b9 230vfprintf (FILE *s, const CHAR_T *format, va_list ap)
28f540f4
RM
231{
232 /* The character used as thousands separator. */
4295702f 233#ifdef COMPILE_WPRINTF
e115dbd7 234 wchar_t thousands_sep = L'\0';
4295702f 235#else
e115dbd7 236 const char *thousands_sep = NULL;
4295702f 237#endif
28f540f4
RM
238
239 /* The string describing the size of groups of digits. */
a04e7405
RM
240 const char *grouping;
241
299a95b9
RM
242 /* Place to accumulate the result. */
243 int done;
a04e7405 244
299a95b9
RM
245 /* Current character in format string. */
246 const UCHAR_T *f;
28f540f4 247
a04e7405 248 /* End of leading constant string. */
299a95b9
RM
249 const UCHAR_T *lead_str_end;
250
251 /* Points to next format specifier. */
252 const UCHAR_T *end_of_spec;
253
254 /* Buffer intermediate results. */
655c0697
UD
255 CHAR_T work_buffer[1000];
256 CHAR_T *workend;
299a95b9
RM
257
258 /* State for restartable multibyte character handling functions. */
d64b6ad0 259#ifndef COMPILE_WPRINTF
299a95b9 260 mbstate_t mbstate;
d64b6ad0 261#endif
299a95b9
RM
262
263 /* We have to save the original argument pointer. */
264 va_list ap_save;
28f540f4 265
299a95b9
RM
266 /* Count number of specifiers we already processed. */
267 int nspecs_done;
268
47707456
UD
269 /* For the %m format we may need the current `errno' value. */
270 int save_errno = errno;
271
299a95b9
RM
272
273 /* This table maps a character into a number representing a
274 class. In each step there is a destination label for each
275 class. */
276 static const int jump_table[] =
277 {
278 /* ' ' */ 1, 0, 0, /* '#' */ 4,
279 0, /* '%' */ 14, 0, /* '\''*/ 6,
280 0, 0, /* '*' */ 7, /* '+' */ 2,
281 0, /* '-' */ 3, /* '.' */ 9, 0,
282 /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8,
283 /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8,
284 /* '8' */ 8, /* '9' */ 8, 0, 0,
285 0, 0, 0, 0,
2f6d1f1b 286 0, /* 'A' */ 26, 0, /* 'C' */ 25,
6c46718f 287 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19,
4295702f 288 0, /* 'I' */ 29, 0, 0,
299a95b9 289 /* 'L' */ 12, 0, 0, 0,
2c6fe0bd 290 0, 0, 0, /* 'S' */ 21,
299a95b9
RM
291 0, 0, 0, 0,
292 /* 'X' */ 18, 0, /* 'Z' */ 13, 0,
293 0, 0, 0, 0,
2f6d1f1b 294 0, /* 'a' */ 26, 0, /* 'c' */ 20,
299a95b9 295 /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
e852e889 296 /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0,
299a95b9
RM
297 /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
298 /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21,
e852e889
UD
299 /* 't' */ 27, /* 'u' */ 16, 0, 0,
300 /* 'x' */ 18, 0, /* 'z' */ 13
299a95b9
RM
301 };
302
655c0697
UD
303#define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
304#define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
b5567b2a 305#if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
3d558f4e
UD
306 /* 'int' is enough and it saves some space on 64 bit systems. */
307# define JUMP_TABLE_TYPE const int
308# define JUMP(ChExpr, table) \
309 do \
310 { \
311 int offset; \
2ca8b1ee 312 void *__unbounded ptr; \
3d558f4e
UD
313 spec = (ChExpr); \
314 offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
315 : table[CHAR_CLASS (spec)]; \
316 ptr = &&do_form_unknown + offset; \
317 goto *ptr; \
318 } \
319 while (0)
320#else
321# define JUMP_TABLE_TYPE const void *const
322# define JUMP(ChExpr, table) \
299a95b9
RM
323 do \
324 { \
2ca8b1ee 325 const void *__unbounded ptr; \
299a95b9
RM
326 spec = (ChExpr); \
327 ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \
328 : table[CHAR_CLASS (spec)]; \
329 goto *ptr; \
330 } \
331 while (0)
3d558f4e 332#endif
299a95b9
RM
333
334#define STEP0_3_TABLE \
335 /* Step 0: at the beginning. */ \
4295702f 336 static JUMP_TABLE_TYPE step0_jumps[30] = \
299a95b9
RM
337 { \
338 REF (form_unknown), \
339 REF (flag_space), /* for ' ' */ \
340 REF (flag_plus), /* for '+' */ \
341 REF (flag_minus), /* for '-' */ \
342 REF (flag_hash), /* for '<hash>' */ \
343 REF (flag_zero), /* for '0' */ \
344 REF (flag_quote), /* for '\'' */ \
345 REF (width_asterics), /* for '*' */ \
346 REF (width), /* for '1'...'9' */ \
347 REF (precision), /* for '.' */ \
348 REF (mod_half), /* for 'h' */ \
349 REF (mod_long), /* for 'l' */ \
350 REF (mod_longlong), /* for 'L', 'q' */ \
e852e889 351 REF (mod_size_t), /* for 'z', 'Z' */ \
299a95b9
RM
352 REF (form_percent), /* for '%' */ \
353 REF (form_integer), /* for 'd', 'i' */ \
354 REF (form_unsigned), /* for 'u' */ \
355 REF (form_octal), /* for 'o' */ \
356 REF (form_hexa), /* for 'X', 'x' */ \
6c46718f 357 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
299a95b9 358 REF (form_character), /* for 'c' */ \
2c6fe0bd 359 REF (form_string), /* for 's', 'S' */ \
299a95b9
RM
360 REF (form_pointer), /* for 'p' */ \
361 REF (form_number), /* for 'n' */ \
2c6fe0bd 362 REF (form_strerror), /* for 'm' */ \
2f6d1f1b 363 REF (form_wcharacter), /* for 'C' */ \
e852e889
UD
364 REF (form_floathex), /* for 'A', 'a' */ \
365 REF (mod_ptrdiff_t), /* for 't' */ \
366 REF (mod_intmax_t), /* for 'j' */ \
4295702f 367 REF (flag_i18n), /* for 'I' */ \
299a95b9
RM
368 }; \
369 /* Step 1: after processing width. */ \
4295702f 370 static JUMP_TABLE_TYPE step1_jumps[30] = \
299a95b9
RM
371 { \
372 REF (form_unknown), \
373 REF (form_unknown), /* for ' ' */ \
374 REF (form_unknown), /* for '+' */ \
375 REF (form_unknown), /* for '-' */ \
376 REF (form_unknown), /* for '<hash>' */ \
377 REF (form_unknown), /* for '0' */ \
378 REF (form_unknown), /* for '\'' */ \
379 REF (form_unknown), /* for '*' */ \
380 REF (form_unknown), /* for '1'...'9' */ \
381 REF (precision), /* for '.' */ \
382 REF (mod_half), /* for 'h' */ \
383 REF (mod_long), /* for 'l' */ \
384 REF (mod_longlong), /* for 'L', 'q' */ \
e852e889 385 REF (mod_size_t), /* for 'z', 'Z' */ \
299a95b9
RM
386 REF (form_percent), /* for '%' */ \
387 REF (form_integer), /* for 'd', 'i' */ \
388 REF (form_unsigned), /* for 'u' */ \
389 REF (form_octal), /* for 'o' */ \
390 REF (form_hexa), /* for 'X', 'x' */ \
6c46718f 391 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
299a95b9 392 REF (form_character), /* for 'c' */ \
2c6fe0bd 393 REF (form_string), /* for 's', 'S' */ \
299a95b9
RM
394 REF (form_pointer), /* for 'p' */ \
395 REF (form_number), /* for 'n' */ \
2c6fe0bd 396 REF (form_strerror), /* for 'm' */ \
2f6d1f1b 397 REF (form_wcharacter), /* for 'C' */ \
e852e889
UD
398 REF (form_floathex), /* for 'A', 'a' */ \
399 REF (mod_ptrdiff_t), /* for 't' */ \
4295702f
UD
400 REF (mod_intmax_t), /* for 'j' */ \
401 REF (flag_i18n) /* for 'I' */ \
299a95b9
RM
402 }; \
403 /* Step 2: after processing precision. */ \
4295702f 404 static JUMP_TABLE_TYPE step2_jumps[30] = \
299a95b9
RM
405 { \
406 REF (form_unknown), \
407 REF (form_unknown), /* for ' ' */ \
408 REF (form_unknown), /* for '+' */ \
409 REF (form_unknown), /* for '-' */ \
410 REF (form_unknown), /* for '<hash>' */ \
411 REF (form_unknown), /* for '0' */ \
412 REF (form_unknown), /* for '\'' */ \
413 REF (form_unknown), /* for '*' */ \
414 REF (form_unknown), /* for '1'...'9' */ \
415 REF (form_unknown), /* for '.' */ \
416 REF (mod_half), /* for 'h' */ \
417 REF (mod_long), /* for 'l' */ \
418 REF (mod_longlong), /* for 'L', 'q' */ \
e852e889 419 REF (mod_size_t), /* for 'z', 'Z' */ \
299a95b9
RM
420 REF (form_percent), /* for '%' */ \
421 REF (form_integer), /* for 'd', 'i' */ \
422 REF (form_unsigned), /* for 'u' */ \
423 REF (form_octal), /* for 'o' */ \
424 REF (form_hexa), /* for 'X', 'x' */ \
6c46718f 425 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
299a95b9 426 REF (form_character), /* for 'c' */ \
2c6fe0bd 427 REF (form_string), /* for 's', 'S' */ \
299a95b9
RM
428 REF (form_pointer), /* for 'p' */ \
429 REF (form_number), /* for 'n' */ \
2c6fe0bd 430 REF (form_strerror), /* for 'm' */ \
2f6d1f1b 431 REF (form_wcharacter), /* for 'C' */ \
e852e889
UD
432 REF (form_floathex), /* for 'A', 'a' */ \
433 REF (mod_ptrdiff_t), /* for 't' */ \
4295702f
UD
434 REF (mod_intmax_t), /* for 'j' */ \
435 REF (flag_i18n) /* for 'I' */ \
299a95b9 436 }; \
cc3fa755 437 /* Step 3a: after processing first 'h' modifier. */ \
4295702f 438 static JUMP_TABLE_TYPE step3a_jumps[30] = \
cc3fa755
UD
439 { \
440 REF (form_unknown), \
441 REF (form_unknown), /* for ' ' */ \
442 REF (form_unknown), /* for '+' */ \
443 REF (form_unknown), /* for '-' */ \
444 REF (form_unknown), /* for '<hash>' */ \
445 REF (form_unknown), /* for '0' */ \
446 REF (form_unknown), /* for '\'' */ \
447 REF (form_unknown), /* for '*' */ \
448 REF (form_unknown), /* for '1'...'9' */ \
449 REF (form_unknown), /* for '.' */ \
450 REF (mod_halfhalf), /* for 'h' */ \
451 REF (form_unknown), /* for 'l' */ \
452 REF (form_unknown), /* for 'L', 'q' */ \
e852e889 453 REF (form_unknown), /* for 'z', 'Z' */ \
cc3fa755
UD
454 REF (form_percent), /* for '%' */ \
455 REF (form_integer), /* for 'd', 'i' */ \
456 REF (form_unsigned), /* for 'u' */ \
457 REF (form_octal), /* for 'o' */ \
458 REF (form_hexa), /* for 'X', 'x' */ \
6c46718f 459 REF (form_unknown), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
cc3fa755
UD
460 REF (form_unknown), /* for 'c' */ \
461 REF (form_unknown), /* for 's', 'S' */ \
462 REF (form_unknown), /* for 'p' */ \
463 REF (form_number), /* for 'n' */ \
464 REF (form_unknown), /* for 'm' */ \
465 REF (form_unknown), /* for 'C' */ \
e852e889
UD
466 REF (form_unknown), /* for 'A', 'a' */ \
467 REF (form_unknown), /* for 't' */ \
4295702f
UD
468 REF (form_unknown), /* for 'j' */ \
469 REF (form_unknown) /* for 'I' */ \
cc3fa755
UD
470 }; \
471 /* Step 3b: after processing first 'l' modifier. */ \
4295702f 472 static JUMP_TABLE_TYPE step3b_jumps[30] = \
299a95b9
RM
473 { \
474 REF (form_unknown), \
475 REF (form_unknown), /* for ' ' */ \
476 REF (form_unknown), /* for '+' */ \
477 REF (form_unknown), /* for '-' */ \
478 REF (form_unknown), /* for '<hash>' */ \
479 REF (form_unknown), /* for '0' */ \
480 REF (form_unknown), /* for '\'' */ \
481 REF (form_unknown), /* for '*' */ \
482 REF (form_unknown), /* for '1'...'9' */ \
483 REF (form_unknown), /* for '.' */ \
484 REF (form_unknown), /* for 'h' */ \
485 REF (mod_longlong), /* for 'l' */ \
486 REF (form_unknown), /* for 'L', 'q' */ \
e852e889 487 REF (form_unknown), /* for 'z', 'Z' */ \
299a95b9
RM
488 REF (form_percent), /* for '%' */ \
489 REF (form_integer), /* for 'd', 'i' */ \
490 REF (form_unsigned), /* for 'u' */ \
491 REF (form_octal), /* for 'o' */ \
492 REF (form_hexa), /* for 'X', 'x' */ \
6c46718f 493 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
299a95b9 494 REF (form_character), /* for 'c' */ \
2c6fe0bd 495 REF (form_string), /* for 's', 'S' */ \
299a95b9
RM
496 REF (form_pointer), /* for 'p' */ \
497 REF (form_number), /* for 'n' */ \
2c6fe0bd 498 REF (form_strerror), /* for 'm' */ \
2f6d1f1b 499 REF (form_wcharacter), /* for 'C' */ \
e852e889
UD
500 REF (form_floathex), /* for 'A', 'a' */ \
501 REF (form_unknown), /* for 't' */ \
4295702f
UD
502 REF (form_unknown), /* for 'j' */ \
503 REF (form_unknown) /* for 'I' */ \
299a95b9 504 }
28f540f4 505
299a95b9
RM
506#define STEP4_TABLE \
507 /* Step 4: processing format specifier. */ \
4295702f 508 static JUMP_TABLE_TYPE step4_jumps[30] = \
299a95b9
RM
509 { \
510 REF (form_unknown), \
511 REF (form_unknown), /* for ' ' */ \
512 REF (form_unknown), /* for '+' */ \
513 REF (form_unknown), /* for '-' */ \
514 REF (form_unknown), /* for '<hash>' */ \
515 REF (form_unknown), /* for '0' */ \
516 REF (form_unknown), /* for '\'' */ \
517 REF (form_unknown), /* for '*' */ \
518 REF (form_unknown), /* for '1'...'9' */ \
519 REF (form_unknown), /* for '.' */ \
520 REF (form_unknown), /* for 'h' */ \
521 REF (form_unknown), /* for 'l' */ \
522 REF (form_unknown), /* for 'L', 'q' */ \
e852e889 523 REF (form_unknown), /* for 'z', 'Z' */ \
299a95b9
RM
524 REF (form_percent), /* for '%' */ \
525 REF (form_integer), /* for 'd', 'i' */ \
526 REF (form_unsigned), /* for 'u' */ \
527 REF (form_octal), /* for 'o' */ \
528 REF (form_hexa), /* for 'X', 'x' */ \
6c46718f 529 REF (form_float), /* for 'E', 'e', 'F', 'f', 'G', 'g' */ \
299a95b9 530 REF (form_character), /* for 'c' */ \
2c6fe0bd 531 REF (form_string), /* for 's', 'S' */ \
299a95b9
RM
532 REF (form_pointer), /* for 'p' */ \
533 REF (form_number), /* for 'n' */ \
2c6fe0bd 534 REF (form_strerror), /* for 'm' */ \
2f6d1f1b 535 REF (form_wcharacter), /* for 'C' */ \
e852e889
UD
536 REF (form_floathex), /* for 'A', 'a' */ \
537 REF (form_unknown), /* for 't' */ \
4295702f
UD
538 REF (form_unknown), /* for 'j' */ \
539 REF (form_unknown) /* for 'I' */ \
299a95b9 540 }
a04e7405 541
a04e7405 542
299a95b9 543#define process_arg(fspec) \
edf5b2d7 544 /* Start real work. We know about all flags and modifiers and \
299a95b9
RM
545 now process the wanted format specifier. */ \
546 LABEL (form_percent): \
547 /* Write a literal "%". */ \
d64b6ad0 548 outchar (L_('%')); \
299a95b9
RM
549 break; \
550 \
551 LABEL (form_integer): \
552 /* Signed decimal integer. */ \
553 base = 10; \
554 \
555 if (is_longlong) \
556 { \
557 long long int signed_number; \
558 \
b8fe19fa
RM
559 if (fspec == NULL) \
560 signed_number = va_arg (ap, long long int); \
561 else \
562 signed_number = args_value[fspec->data_arg].pa_long_long_int; \
299a95b9
RM
563 \
564 is_negative = signed_number < 0; \
565 number.longlong = is_negative ? (- signed_number) : signed_number; \
566 \
567 goto LABEL (longlong_number); \
568 } \
569 else \
570 { \
571 long int signed_number; \
572 \
b8fe19fa 573 if (fspec == NULL) \
6591c335 574 { \
859a09cf 575 if (is_long_num) \
6591c335
UD
576 signed_number = va_arg (ap, long int); \
577 else /* `char' and `short int' will be promoted to `int'. */ \
578 signed_number = va_arg (ap, int); \
579 } \
b8fe19fa 580 else \
859a09cf 581 if (is_long_num) \
b8fe19fa
RM
582 signed_number = args_value[fspec->data_arg].pa_long_int; \
583 else \
584 signed_number = args_value[fspec->data_arg].pa_int; \
299a95b9
RM
585 \
586 is_negative = signed_number < 0; \
587 number.word = is_negative ? (- signed_number) : signed_number; \
588 \
589 goto LABEL (number); \
590 } \
591 /* NOTREACHED */ \
592 \
593 LABEL (form_unsigned): \
594 /* Unsigned decimal integer. */ \
595 base = 10; \
596 goto LABEL (unsigned_number); \
597 /* NOTREACHED */ \
598 \
599 LABEL (form_octal): \
600 /* Unsigned octal integer. */ \
601 base = 8; \
602 goto LABEL (unsigned_number); \
603 /* NOTREACHED */ \
604 \
605 LABEL (form_hexa): \
606 /* Unsigned hexadecimal integer. */ \
607 base = 16; \
608 \
609 LABEL (unsigned_number): /* Unsigned number of base BASE. */ \
610 \
cc3fa755 611 /* ISO specifies the `+' and ` ' flags only for signed \
299a95b9
RM
612 conversions. */ \
613 is_negative = 0; \
614 showsign = 0; \
615 space = 0; \
616 \
617 if (is_longlong) \
618 { \
b8fe19fa
RM
619 if (fspec == NULL) \
620 number.longlong = va_arg (ap, unsigned long long int); \
621 else \
622 number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
299a95b9
RM
623 \
624 LABEL (longlong_number): \
625 if (prec < 0) \
626 /* Supply a default precision if none was given. */ \
627 prec = 1; \
628 else \
629 /* We have to take care for the '0' flag. If a precision \
630 is given it must be ignored. */ \
d64b6ad0 631 pad = L_(' '); \
299a95b9
RM
632 \
633 /* If the precision is 0 and the number is 0 nothing has to \
6591c335
UD
634 be written for the number, except for the 'o' format in \
635 alternate form. */ \
299a95b9 636 if (prec == 0 && number.longlong == 0) \
6591c335
UD
637 { \
638 string = workend; \
639 if (base == 8 && alt) \
69c69fe1 640 *--string = L_('0'); \
6591c335 641 } \
299a95b9
RM
642 else \
643 { \
644 /* Put the number in WORK. */ \
69c69fe1
UD
645 string = _itoa (number.longlong, workend, base, \
646 spec == L_('X')); \
299a95b9
RM
647 if (group && grouping) \
648 string = group_number (string, workend, grouping, \
649 thousands_sep); \
69c69fe1
UD
650 \
651 if (use_outdigits && base == 10) \
652 string = _i18n_number_rewrite (string, workend); \
299a95b9 653 } \
31c46cf5 654 /* Simplify further test for num != 0. */ \
299a95b9
RM
655 number.word = number.longlong != 0; \
656 } \
657 else \
658 { \
b8fe19fa 659 if (fspec == NULL) \
6591c335 660 { \
859a09cf 661 if (is_long_num) \
6591c335 662 number.word = va_arg (ap, unsigned long int); \
b3f7c665
UD
663 else if (is_char) \
664 number.word = (unsigned char) va_arg (ap, unsigned int); \
6591c335
UD
665 else if (!is_short) \
666 number.word = va_arg (ap, unsigned int); \
667 else \
668 number.word = (unsigned short int) va_arg (ap, unsigned int); \
669 } \
299a95b9 670 else \
859a09cf 671 if (is_long_num) \
b8fe19fa 672 number.word = args_value[fspec->data_arg].pa_u_long_int; \
cc3fa755
UD
673 else if (is_char) \
674 number.word = (unsigned char) \
675 args_value[fspec->data_arg].pa_char; \
b8fe19fa
RM
676 else if (!is_short) \
677 number.word = args_value[fspec->data_arg].pa_u_int; \
678 else \
679 number.word = (unsigned short int) \
680 args_value[fspec->data_arg].pa_u_short_int; \
299a95b9
RM
681 \
682 LABEL (number): \
683 if (prec < 0) \
684 /* Supply a default precision if none was given. */ \
685 prec = 1; \
686 else \
687 /* We have to take care for the '0' flag. If a precision \
688 is given it must be ignored. */ \
d64b6ad0 689 pad = L_(' '); \
299a95b9
RM
690 \
691 /* If the precision is 0 and the number is 0 nothing has to \
6591c335
UD
692 be written for the number, except for the 'o' format in \
693 alternate form. */ \
299a95b9 694 if (prec == 0 && number.word == 0) \
6591c335
UD
695 { \
696 string = workend; \
697 if (base == 8 && alt) \
69c69fe1 698 *--string = L_('0'); \
6591c335 699 } \
299a95b9
RM
700 else \
701 { \
702 /* Put the number in WORK. */ \
69c69fe1
UD
703 string = _itoa_word (number.word, workend, base, \
704 spec == L_('X')); \
299a95b9
RM
705 if (group && grouping) \
706 string = group_number (string, workend, grouping, \
707 thousands_sep); \
69c69fe1
UD
708 \
709 if (use_outdigits && base == 10) \
710 string = _i18n_number_rewrite (string, workend); \
299a95b9
RM
711 } \
712 } \
713 \
3e95f660 714 if (prec <= workend - string && number.word != 0 && alt && base == 8) \
299a95b9 715 /* Add octal marker. */ \
69c69fe1 716 *--string = L_('0'); \
299a95b9 717 \
3e95f660
UD
718 prec = MAX (0, prec - (workend - string)); \
719 \
299a95b9
RM
720 if (!left) \
721 { \
3e95f660 722 width -= workend - string + prec; \
299a95b9
RM
723 \
724 if (number.word != 0 && alt && base == 16) \
725 /* Account for 0X hex marker. */ \
726 width -= 2; \
727 \
728 if (is_negative || showsign || space) \
729 --width; \
730 \
3e95f660 731 if (pad == L_(' ')) \
299a95b9 732 { \
3e95f660
UD
733 PAD (L_(' ')); \
734 width = 0; \
299a95b9 735 } \
3e95f660
UD
736 \
737 if (is_negative) \
e41db95b 738 outchar (L_('-')); \
3e95f660 739 else if (showsign) \
e41db95b 740 outchar (L_('+')); \
3e95f660 741 else if (space) \
e41db95b 742 outchar (L_(' ')); \
3e95f660
UD
743 \
744 if (number.word != 0 && alt && base == 16) \
299a95b9 745 { \
e41db95b
UD
746 outchar (L_('0')); \
747 outchar (spec); \
299a95b9
RM
748 } \
749 \
3e95f660
UD
750 width += prec; \
751 PAD (L_('0')); \
752 \
69c69fe1 753 outstring (string, workend - string); \
299a95b9
RM
754 \
755 break; \
756 } \
757 else \
758 { \
3e95f660 759 if (is_negative) \
299a95b9 760 { \
e41db95b 761 outchar (L_('-')); \
3e95f660 762 --width; \
299a95b9 763 } \
299a95b9 764 else if (showsign) \
3e95f660 765 { \
e41db95b 766 outchar (L_('+')); \
3e95f660
UD
767 --width; \
768 } \
299a95b9 769 else if (space) \
3e95f660 770 { \
e41db95b 771 outchar (L_(' ')); \
3e95f660
UD
772 --width; \
773 } \
774 \
775 if (number.word != 0 && alt && base == 16) \
776 { \
e41db95b
UD
777 outchar (L_('0')); \
778 outchar (spec); \
3e95f660
UD
779 width -= 2; \
780 } \
781 \
782 width -= workend - string + prec; \
783 \
784 if (prec > 0) \
785 { \
786 int temp = width; \
787 width = prec; \
788 PAD (L_('0'));; \
789 width = temp; \
790 } \
299a95b9 791 \
69c69fe1 792 outstring (string, workend - string); \
299a95b9 793 \
d64b6ad0 794 PAD (L_(' ')); \
299a95b9
RM
795 break; \
796 } \
797 \
798 LABEL (form_float): \
799 { \
800 /* Floating-point number. This is handled by printf_fp.c. */ \
299a95b9
RM
801 const void *ptr; \
802 int function_done; \
803 \
299a95b9
RM
804 if (fspec == NULL) \
805 { \
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, \
b8fe19fa 817 pad: pad, \
d64b6ad0
UD
818 extra: 0, \
819 wide: sizeof (CHAR_T) != 1 }; \
b8fe19fa
RM
820 \
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; \
299a95b9
RM
826 \
827 function_done = __printf_fp (s, &info, &ptr); \
828 } \
829 else \
b8fe19fa
RM
830 { \
831 ptr = (const void *) &args_value[fspec->data_arg]; \
832 \
833 function_done = __printf_fp (s, &fspec->info, &ptr); \
834 } \
299a95b9
RM
835 \
836 if (function_done < 0) \
b0882748
UD
837 { \
838 /* Error in print handler. */ \
839 done = -1; \
840 goto all_done; \
841 } \
299a95b9
RM
842 \
843 done += function_done; \
844 } \
845 break; \
846 \
2f6d1f1b
UD
847 LABEL (form_floathex): \
848 { \
03bac9ac 849 /* Floating point number printed as hexadecimal number. */ \
2f6d1f1b
UD
850 const void *ptr; \
851 int function_done; \
852 \
853 if (fspec == NULL) \
854 { \
855 struct printf_info info = { prec: prec, \
856 width: width, \
857 spec: spec, \
858 is_long_double: is_long_double, \
859 is_short: is_short, \
860 is_long: is_long, \
861 alt: alt, \
862 space: space, \
863 left: left, \
864 showsign: showsign, \
865 group: group, \
866 pad: pad, \
d64b6ad0
UD
867 extra: 0, \
868 wide: sizeof (CHAR_T) != 1 }; \
2f6d1f1b
UD
869 \
870 if (is_long_double) \
871 the_arg.pa_long_double = va_arg (ap, long double); \
872 else \
873 the_arg.pa_double = va_arg (ap, double); \
874 ptr = (const void *) &the_arg; \
875 \
876 function_done = __printf_fphex (s, &info, &ptr); \
877 } \
878 else \
879 { \
880 ptr = (const void *) &args_value[fspec->data_arg]; \
881 \
882 function_done = __printf_fphex (s, &fspec->info, &ptr); \
883 } \
884 \
885 if (function_done < 0) \
b0882748
UD
886 { \
887 /* Error in print handler. */ \
888 done = -1; \
889 goto all_done; \
890 } \
2f6d1f1b
UD
891 \
892 done += function_done; \
893 } \
894 break; \
895 \
d64b6ad0
UD
896 LABEL (form_pointer): \
897 /* Generic pointer. */ \
898 { \
899 const void *ptr; \
900 if (fspec == NULL) \
901 ptr = va_arg (ap, void *); \
902 else \
903 ptr = args_value[fspec->data_arg].pa_pointer; \
904 if (ptr != NULL) \
905 { \
906 /* If the pointer is not NULL, write it as a %#x spec. */ \
907 base = 16; \
908 number.word = (unsigned long int) ptr; \
909 is_negative = 0; \
910 alt = 1; \
911 group = 0; \
655c0697 912 spec = L_('x'); \
d64b6ad0
UD
913 goto LABEL (number); \
914 } \
915 else \
916 { \
917 /* Write "(nil)" for a nil pointer. */ \
03bac9ac 918 string = (CHAR_T *) L_("(nil)"); \
d64b6ad0
UD
919 /* Make sure the full string "(nil)" is printed. */ \
920 if (prec < 5) \
921 prec = 5; \
922 is_long = 0; /* This is no wide-char string. */ \
923 goto LABEL (print_string); \
924 } \
925 } \
926 /* NOTREACHED */ \
927 \
928 LABEL (form_number): \
929 /* Answer the count of characters written. */ \
930 if (fspec == NULL) \
931 { \
932 if (is_longlong) \
933 *(long long int *) va_arg (ap, void *) = done; \
934 else if (is_long_num) \
935 *(long int *) va_arg (ap, void *) = done; \
b3f7c665
UD
936 else if (is_char) \
937 *(char *) va_arg (ap, void *) = done; \
938 else if (is_long_num) \
939 *(long int *) va_arg (ap, void *) = done; \
d64b6ad0
UD
940 else if (!is_short) \
941 *(int *) va_arg (ap, void *) = done; \
942 else \
943 *(short int *) va_arg (ap, void *) = done; \
944 } \
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; \
b3f7c665
UD
950 else if (is_long_num) \
951 *(long int *) args_value[fspec->data_arg].pa_pointer = done; \
952 else if (is_char) \
953 *(char *) args_value[fspec->data_arg].pa_pointer = done; \
d64b6ad0
UD
954 else if (!is_short) \
955 *(int *) args_value[fspec->data_arg].pa_pointer = done; \
956 else \
957 *(short int *) args_value[fspec->data_arg].pa_pointer = done; \
958 break; \
959 \
960 LABEL (form_strerror): \
961 /* Print description of error ERRNO. */ \
962 string = \
655c0697
UD
963 (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \
964 sizeof work_buffer); \
d64b6ad0
UD
965 is_long = 0; /* This is no wide-char string. */ \
966 goto LABEL (print_string)
967
968#ifdef COMPILE_WPRINTF
969# define process_string_arg(fspec) \
970 LABEL (form_character): \
971 /* Character. */ \
972 if (is_long) \
973 goto LABEL (form_wcharacter); \
974 --width; /* Account for the character itself. */ \
975 if (!left) \
976 PAD (L' '); \
977 if (fspec == NULL) \
4aebaa6b 978 outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
d64b6ad0 979 else \
4aebaa6b
UD
980 outchar (__btowc ((unsigned char) \
981 args_value[fspec->data_arg].pa_char)); \
d64b6ad0
UD
982 if (left) \
983 PAD (L' '); \
984 break; \
985 \
986 LABEL (form_wcharacter): \
987 { \
988 /* Wide character. */ \
989 --width; \
990 if (!left) \
991 PAD (L' '); \
992 if (fspec == NULL) \
993 outchar (va_arg (ap, wint_t)); \
994 else \
995 outchar (args_value[fspec->data_arg].pa_wchar); \
996 if (left) \
997 PAD (L' '); \
998 } \
999 break; \
1000 \
1001 LABEL (form_string): \
1002 { \
1003 size_t len; \
74ac2cec 1004 int string_malloced; \
d64b6ad0
UD
1005 \
1006 /* The string argument could in fact be `char *' or `wchar_t *'. \
1007 But this should not make a difference here. */ \
1008 if (fspec == NULL) \
655c0697 1009 string = (CHAR_T *) va_arg (ap, const wchar_t *); \
d64b6ad0 1010 else \
655c0697 1011 string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \
d64b6ad0
UD
1012 \
1013 /* Entry point for printing other strings. */ \
1014 LABEL (print_string): \
1015 \
74ac2cec 1016 string_malloced = 0; \
d64b6ad0
UD
1017 if (string == NULL) \
1018 { \
1019 /* Write "(null)" if there's space. */ \
1020 if (prec == -1 \
1021 || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \
1022 { \
655c0697 1023 string = (CHAR_T *) null; \
d64b6ad0
UD
1024 len = (sizeof (null) / sizeof (null[0])) - 1; \
1025 } \
1026 else \
1027 { \
655c0697 1028 string = (CHAR_T *) L""; \
d64b6ad0
UD
1029 len = 0; \
1030 } \
1031 } \
1032 else if (!is_long && spec != L_('S')) \
1033 { \
1034 /* This is complicated. We have to transform the multibyte \
1035 string into a wide character string. */ \
1036 const char *mbs = (const char *) string; \
1037 mbstate_t mbstate; \
1038 \
74ac2cec 1039 len = prec != -1 ? prec : strlen (mbs); \
d64b6ad0
UD
1040 \
1041 /* Allocate dynamically an array which definitely is long \
1042 enough for the wide character version. */ \
74ac2cec
UD
1043 if (len < 8192 \
1044 || ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \
1045 == NULL)) \
1046 string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \
1047 else \
1048 string_malloced = 1; \
d64b6ad0
UD
1049 \
1050 memset (&mbstate, '\0', sizeof (mbstate_t)); \
74ac2cec 1051 len = __mbsrtowcs (string, &mbs, len, &mbstate); \
d64b6ad0
UD
1052 if (len == (size_t) -1) \
1053 { \
1054 /* Illegal multibyte character. */ \
1055 done = -1; \
1056 goto all_done; \
1057 } \
1058 } \
1059 else \
1060 { \
1061 if (prec != -1) \
1062 /* Search for the end of the string, but don't search past \
1063 the length specified by the precision. */ \
655c0697 1064 len = __wcsnlen (string, prec); \
d64b6ad0 1065 else \
655c0697 1066 len = __wcslen (string); \
d64b6ad0
UD
1067 } \
1068 \
1069 if ((width -= len) < 0) \
1070 { \
1071 outstring (string, len); \
1072 break; \
1073 } \
1074 \
1075 if (!left) \
1076 PAD (L' '); \
1077 outstring (string, len); \
1078 if (left) \
1079 PAD (L' '); \
74ac2cec
UD
1080 if (string_malloced) \
1081 free (string); \
d64b6ad0
UD
1082 } \
1083 break;
1084#else
1085# define process_string_arg(fspec) \
299a95b9
RM
1086 LABEL (form_character): \
1087 /* Character. */ \
2c6fe0bd
UD
1088 if (is_long) \
1089 goto LABEL (form_wcharacter); \
299a95b9
RM
1090 --width; /* Account for the character itself. */ \
1091 if (!left) \
1092 PAD (' '); \
b8fe19fa 1093 if (fspec == NULL) \
2c6fe0bd 1094 outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \
b8fe19fa
RM
1095 else \
1096 outchar ((unsigned char) args_value[fspec->data_arg].pa_char); \
299a95b9
RM
1097 if (left) \
1098 PAD (' '); \
1099 break; \
1100 \
2c6fe0bd
UD
1101 LABEL (form_wcharacter): \
1102 { \
1103 /* Wide character. */ \
1104 char buf[MB_CUR_MAX]; \
1105 mbstate_t mbstate; \
1106 size_t len; \
1107 \
3c720987 1108 memset (&mbstate, '\0', sizeof (mbstate_t)); \
2c6fe0bd
UD
1109 len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wint_t) \
1110 : args_value[fspec->data_arg].pa_wchar), \
1111 &mbstate); \
ca4447d6
UD
1112 if (len == (size_t) -1) \
1113 { \
1114 /* Something went wron gduring the conversion. Bail out. */ \
1115 done = -1; \
1116 goto all_done; \
1117 } \
2c6fe0bd
UD
1118 width -= len; \
1119 if (!left) \
1120 PAD (' '); \
1121 outstring (buf, len); \
1122 if (left) \
1123 PAD (' '); \
1124 } \
1125 break; \
1126 \
299a95b9
RM
1127 LABEL (form_string): \
1128 { \
1129 size_t len; \
74ac2cec 1130 int string_malloced; \
299a95b9
RM
1131 \
1132 /* The string argument could in fact be `char *' or `wchar_t *'. \
1133 But this should not make a difference here. */ \
b8fe19fa
RM
1134 if (fspec == NULL) \
1135 string = (char *) va_arg (ap, const char *); \
1136 else \
1137 string = (char *) args_value[fspec->data_arg].pa_string; \
299a95b9
RM
1138 \
1139 /* Entry point for printing other strings. */ \
1140 LABEL (print_string): \
1141 \
74ac2cec 1142 string_malloced = 0; \
299a95b9
RM
1143 if (string == NULL) \
1144 { \
1145 /* Write "(null)" if there's space. */ \
1146 if (prec == -1 || prec >= (int) sizeof (null) - 1) \
1147 { \
1148 string = (char *) null; \
1149 len = sizeof (null) - 1; \
1150 } \
1151 else \
1152 { \
1153 string = (char *) ""; \
1154 len = 0; \
1155 } \
1156 } \
2c6fe0bd 1157 else if (!is_long && spec != L_('S')) \
299a95b9
RM
1158 { \
1159 if (prec != -1) \
40c014b3
UD
1160 { \
1161 /* Search for the end of the string, but don't search past \
1162 the length (in bytes) specified by the precision. Also \
1163 don't use incomplete characters. */ \
1164 if (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MB_CUR_MAX) == 1) \
1165 len = __strnlen (string, prec); \
1166 else \
1167 { \
1168 /* In case we have a multibyte character set the \
1169 situation is more compilcated. We must not copy \
1170 bytes at the end which form an incomplete character. */\
1171 wchar_t ignore[prec]; \
1172 const char *str2 = string; \
1173 mbstate_t ps; \
1174 \
1175 memset (&ps, '\0', sizeof (ps)); \
1176 if (__mbsnrtowcs (ignore, &str2, prec, prec, &ps) \
1177 == (size_t) -1) \
1178 { \
1179 done = -1; \
1180 goto all_done; \
1181 } \
1182 if (str2 == NULL) \
1183 len = strlen (string); \
1184 else \
1185 len = str2 - string - (ps.__count); \
1186 } \
1187 } \
299a95b9
RM
1188 else \
1189 len = strlen (string); \
1190 } \
1191 else \
1192 { \
1193 const wchar_t *s2 = (const wchar_t *) string; \
07a4742f 1194 mbstate_t mbstate; \
299a95b9 1195 \
3c720987 1196 memset (&mbstate, '\0', sizeof (mbstate_t)); \
22318b7b 1197 \
13c5a442 1198 if (prec >= 0) \
22318b7b
UD
1199 { \
1200 /* The string `s2' might not be NUL terminated. */ \
74ac2cec
UD
1201 if (prec < 32768 \
1202 || (string = (char *) malloc (prec)) == NULL) \
1203 string = (char *) alloca (prec); \
1204 else \
1205 string_malloced = 1; \
d198009a 1206 len = __wcsrtombs (string, &s2, prec, &mbstate); \
22318b7b
UD
1207 } \
1208 else \
1209 { \
1210 len = __wcsrtombs (NULL, &s2, 0, &mbstate); \
1211 if (len != (size_t) -1) \
1212 { \
1213 assert (__mbsinit (&mbstate)); \
1214 s2 = (const wchar_t *) string; \
74ac2cec
UD
1215 if (len + 1 < 32768 \
1216 || (string = (char *) malloc (len + 1)) == NULL) \
1217 string = (char *) alloca (len + 1); \
1218 else \
1219 string_malloced = 1; \
22318b7b
UD
1220 (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \
1221 } \
1222 } \
1223 \
299a95b9 1224 if (len == (size_t) -1) \
b0882748
UD
1225 { \
1226 /* Illegal wide-character string. */ \
1227 done = -1; \
1228 goto all_done; \
1229 } \
299a95b9
RM
1230 } \
1231 \
1232 if ((width -= len) < 0) \
1233 { \
1234 outstring (string, len); \
1235 break; \
1236 } \
1237 \
1238 if (!left) \
1239 PAD (' '); \
1240 outstring (string, len); \
1241 if (left) \
1242 PAD (' '); \
74ac2cec
UD
1243 if (string_malloced) \
1244 free (string); \
299a95b9 1245 } \
d64b6ad0
UD
1246 break;
1247#endif
299a95b9 1248
d64b6ad0
UD
1249 /* Orient the stream. */
1250#ifdef ORIENT
1251 ORIENT;
1252#endif
299a95b9
RM
1253
1254 /* Sanity check of arguments. */
28f540f4
RM
1255 ARGCHECK (s, format);
1256
ab58d620 1257#ifdef ORIENT
d64b6ad0 1258 /* Check for correct orientation. */
c9eaa8b9 1259 if (
ab58d620 1260# ifdef USE_IN_LIBIO
c9eaa8b9 1261 s->_vtable_offset == 0 &&
ab58d620 1262# endif
c9eaa8b9 1263 _IO_fwide (s, sizeof (CHAR_T) == 1 ? -1 : 1)
d64b6ad0
UD
1264 != (sizeof (CHAR_T) == 1 ? -1 : 1))
1265 /* The stream is already oriented otherwise. */
1266 return EOF;
ab58d620 1267#endif
d64b6ad0 1268
28f540f4
RM
1269 if (UNBUFFERED_P (s))
1270 /* Use a helper function which will allocate a local temporary buffer
1271 for the stream and then call us again. */
a04e7405 1272 return buffered_vfprintf (s, format, ap);
28f540f4 1273
299a95b9
RM
1274 /* Initialize local variables. */
1275 done = 0;
1276 grouping = (const char *) -1;
7cc27f44 1277#ifdef __va_copy
2f6d1f1b 1278 /* This macro will be available soon in gcc's <stdarg.h>. We need it
7cc27f44
UD
1279 since on some systems `va_list' is not an integral type. */
1280 __va_copy (ap_save, ap);
1281#else
299a95b9 1282 ap_save = ap;
7cc27f44 1283#endif
299a95b9 1284 nspecs_done = 0;
28f540f4 1285
d64b6ad0
UD
1286#ifdef COMPILE_WPRINTF
1287 /* Find the first format specifier. */
1288 f = lead_str_end = find_spec ((const UCHAR_T *) format);
1289#else
3c720987
UD
1290 /* Put state for processing format string in initial state. */
1291 memset (&mbstate, '\0', sizeof (mbstate_t));
1292
299a95b9
RM
1293 /* Find the first format specifier. */
1294 f = lead_str_end = find_spec (format, &mbstate);
d64b6ad0 1295#endif
28f540f4 1296
7c713e28 1297 /* Lock stream. */
46ec036d 1298#ifdef USE_IN_LIBIO
0dce3d15 1299 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, s);
46ec036d
UD
1300 _IO_flockfile (s);
1301#else
0dce3d15 1302 __libc_cleanup_region_start (1, (void (*) (void *)) &__funlockfile, s);
d41c6f61 1303 __flockfile (s);
46ec036d 1304#endif
aa1075ea 1305
299a95b9
RM
1306 /* Write the literal text before the first format. */
1307 outstring ((const UCHAR_T *) format,
1308 lead_str_end - (const UCHAR_T *) format);
a04e7405 1309
299a95b9
RM
1310 /* If we only have to print a simple string, return now. */
1311 if (*f == L_('\0'))
edf5b2d7 1312 goto all_done;
a04e7405 1313
299a95b9
RM
1314 /* Process whole format string. */
1315 do
28f540f4 1316 {
b5567b2a 1317#if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
3d558f4e
UD
1318# define REF(Name) &&do_##Name - &&do_form_unknown
1319#else
1320# define REF(Name) &&do_##Name
1321#endif
299a95b9
RM
1322#define LABEL(Name) do_##Name
1323 STEP0_3_TABLE;
1324 STEP4_TABLE;
1325
b8fe19fa 1326 union printf_arg *args_value; /* This is not used here but ... */
299a95b9
RM
1327 int is_negative; /* Flag for negative number. */
1328 union
1329 {
1330 unsigned long long int longlong;
1331 unsigned long int word;
1332 } number;
1333 int base;
1334 union printf_arg the_arg;
655c0697 1335 CHAR_T *string; /* Pointer to argument string. */
299a95b9
RM
1336 int alt = 0; /* Alternate format. */
1337 int space = 0; /* Use space prefix if no sign is needed. */
1338 int left = 0; /* Left-justify output. */
1339 int showsign = 0; /* Always begin with plus or minus sign. */
1340 int group = 0; /* Print numbers according grouping rules. */
1341 int is_long_double = 0; /* Argument is long double/ long long int. */
9a8fcca0
UD
1342 int is_short = 0; /* Argument is short int. */
1343 int is_long = 0; /* Argument is long int. */
cc3fa755 1344 int is_char = 0; /* Argument is promoted (unsigned) char. */
299a95b9
RM
1345 int width = 0; /* Width of output; 0 means none specified. */
1346 int prec = -1; /* Precision of output; -1 means none specified. */
e115dbd7
UD
1347 /* This flag is set by the 'I' modifier and selects the use of the
1348 `outdigits' as determined by the current locale. */
1349 int use_outdigits = 0;
d64b6ad0 1350 UCHAR_T pad = L_(' ');/* Padding character. */
299a95b9
RM
1351 CHAR_T spec;
1352
69c69fe1 1353 workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
afe426a0 1354
299a95b9
RM
1355 /* Get current character in format string. */
1356 JUMP (*++f, step0_jumps);
1357
1358 /* ' ' flag. */
1359 LABEL (flag_space):
1360 space = 1;
1361 JUMP (*++f, step0_jumps);
1362
1363 /* '+' flag. */
1364 LABEL (flag_plus):
1365 showsign = 1;
1366 JUMP (*++f, step0_jumps);
1367
1368 /* The '-' flag. */
1369 LABEL (flag_minus):
1370 left = 1;
1371 pad = L_(' ');
1372 JUMP (*++f, step0_jumps);
1373
1374 /* The '#' flag. */
1375 LABEL (flag_hash):
1376 alt = 1;
1377 JUMP (*++f, step0_jumps);
1378
1379 /* The '0' flag. */
1380 LABEL (flag_zero):
1381 if (!left)
1382 pad = L_('0');
1383 JUMP (*++f, step0_jumps);
1384
1385 /* The '\'' flag. */
1386 LABEL (flag_quote):
1387 group = 1;
1388
299a95b9 1389 if (grouping == (const char *) -1)
28f540f4 1390 {
4295702f
UD
1391#ifdef COMPILE_WPRINTF
1392 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1393 _NL_NUMERIC_THOUSANDS_SEP_WC);
1394#else
1395 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1396#endif
1397
299a95b9
RM
1398 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
1399 if (*grouping == '\0' || *grouping == CHAR_MAX
4295702f
UD
1400#ifdef COMPILE_WPRINTF
1401 || thousands_sep == L'\0'
1402#else
1403 || *thousands_sep == '\0'
1404#endif
1405 )
299a95b9 1406 grouping = NULL;
28f540f4 1407 }
299a95b9 1408 JUMP (*++f, step0_jumps);
28f540f4 1409
4295702f
UD
1410 LABEL (flag_i18n):
1411 use_outdigits = 1;
9d54e984 1412 JUMP (*++f, step0_jumps);
4295702f 1413
299a95b9
RM
1414 /* Get width from argument. */
1415 LABEL (width_asterics):
1416 {
1417 const UCHAR_T *tmp; /* Temporary value. */
a04e7405 1418
299a95b9
RM
1419 tmp = ++f;
1420 if (ISDIGIT (*tmp) && read_int (&tmp) && *tmp == L_('$'))
a641835a 1421 /* The width comes from a positional parameter. */
299a95b9 1422 goto do_positional;
a04e7405 1423
299a95b9 1424 width = va_arg (ap, int);
a04e7405 1425
299a95b9
RM
1426 /* Negative width means left justified. */
1427 if (width < 0)
1428 {
1429 width = -width;
1430 pad = L_(' ');
1431 left = 1;
1432 }
afe426a0 1433
d25adda7 1434 if (width + 32 >= (int) (sizeof (work_buffer) / sizeof (work_buffer[0])))
afe426a0
UD
1435 /* We have to use a special buffer. The "32" is just a safe
1436 bet for all the output which is not counted in the width. */
655c0697 1437 workend = ((CHAR_T *) alloca ((width + 32) * sizeof (CHAR_T))
69c69fe1 1438 + (width + 32));
299a95b9
RM
1439 }
1440 JUMP (*f, step1_jumps);
1441
1442 /* Given width in format string. */
1443 LABEL (width):
1444 width = read_int (&f);
afe426a0 1445
d25adda7 1446 if (width + 32 >= (int) (sizeof (work_buffer) / sizeof (work_buffer[0])))
afe426a0
UD
1447 /* We have to use a special buffer. The "32" is just a safe
1448 bet for all the output which is not counted in the width. */
655c0697 1449 workend = ((CHAR_T *) alloca ((width + 32) * sizeof (CHAR_T))
69c69fe1 1450 + (width + 32));
299a95b9 1451 if (*f == L_('$'))
a641835a 1452 /* Oh, oh. The argument comes from a positional parameter. */
299a95b9
RM
1453 goto do_positional;
1454 JUMP (*f, step1_jumps);
1455
1456 LABEL (precision):
1457 ++f;
1458 if (*f == L_('*'))
1459 {
1460 const UCHAR_T *tmp; /* Temporary value. */
a04e7405 1461
299a95b9
RM
1462 tmp = ++f;
1463 if (ISDIGIT (*tmp) && read_int (&tmp) > 0 && *tmp == L_('$'))
a641835a 1464 /* The precision comes from a positional parameter. */
299a95b9 1465 goto do_positional;
28f540f4 1466
299a95b9 1467 prec = va_arg (ap, int);
28f540f4 1468
299a95b9
RM
1469 /* If the precision is negative the precision is omitted. */
1470 if (prec < 0)
1471 prec = -1;
1472 }
1473 else if (ISDIGIT (*f))
1474 prec = read_int (&f);
1475 else
1476 prec = 0;
d64b6ad0 1477 if (prec > width
d25adda7 1478 && prec + 32 > (int)(sizeof (work_buffer) / sizeof (work_buffer[0])))
69c69fe1 1479 workend = alloca (spec + 32) + (spec + 32);
299a95b9
RM
1480 JUMP (*f, step2_jumps);
1481
cc3fa755 1482 /* Process 'h' modifier. There might another 'h' following. */
299a95b9
RM
1483 LABEL (mod_half):
1484 is_short = 1;
cc3fa755
UD
1485 JUMP (*++f, step3a_jumps);
1486
1487 /* Process 'hh' modifier. */
1488 LABEL (mod_halfhalf):
1489 is_short = 0;
1490 is_char = 1;
299a95b9
RM
1491 JUMP (*++f, step4_jumps);
1492
cc3fa755 1493 /* Process 'l' modifier. There might another 'l' following. */
299a95b9
RM
1494 LABEL (mod_long):
1495 is_long = 1;
cc3fa755 1496 JUMP (*++f, step3b_jumps);
299a95b9
RM
1497
1498 /* Process 'L', 'q', or 'll' modifier. No other modifier is
1499 allowed to follow. */
1500 LABEL (mod_longlong):
1501 is_long_double = 1;
1000d1e5 1502 is_long = 1;
299a95b9
RM
1503 JUMP (*++f, step4_jumps);
1504
1505 LABEL (mod_size_t):
0ae97dea 1506 is_long_double = sizeof (size_t) > sizeof (unsigned long int);
299a95b9
RM
1507 is_long = sizeof (size_t) > sizeof (unsigned int);
1508 JUMP (*++f, step4_jumps);
1509
e852e889 1510 LABEL (mod_ptrdiff_t):
0ae97dea 1511 is_long_double = sizeof (ptrdiff_t) > sizeof (unsigned long int);
e852e889
UD
1512 is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
1513 JUMP (*++f, step4_jumps);
1514
1515 LABEL (mod_intmax_t):
0ae97dea 1516 is_long_double = sizeof (intmax_t) > sizeof (unsigned long int);
e852e889
UD
1517 is_long = sizeof (intmax_t) > sizeof (unsigned int);
1518 JUMP (*++f, step4_jumps);
1519
299a95b9
RM
1520 /* Process current format. */
1521 while (1)
28f540f4 1522 {
299a95b9 1523 process_arg (((struct printf_spec *) NULL));
d64b6ad0 1524 process_string_arg (((struct printf_spec *) NULL));
299a95b9
RM
1525
1526 LABEL (form_unknown):
1527 if (spec == L_('\0'))
7c713e28
RM
1528 {
1529 /* The format string ended before the specifier is complete. */
edf5b2d7
UD
1530 done = -1;
1531 goto all_done;
7c713e28 1532 }
299a95b9
RM
1533
1534 /* If we are in the fast loop force entering the complicated
1535 one. */
1536 goto do_positional;
28f540f4 1537 }
299a95b9 1538
3e5f5557
UD
1539 /* The format is correctly handled. */
1540 ++nspecs_done;
1541
299a95b9 1542 /* Look for next format specifier. */
d64b6ad0
UD
1543#ifdef COMPILE_WPRINTF
1544 f = find_spec ((end_of_spec = ++f));
1545#else
299a95b9 1546 f = find_spec ((end_of_spec = ++f), &mbstate);
d64b6ad0 1547#endif
299a95b9
RM
1548
1549 /* Write the following constant string. */
1550 outstring (end_of_spec, f - end_of_spec);
a04e7405 1551 }
299a95b9 1552 while (*f != L_('\0'));
28f540f4 1553
edf5b2d7
UD
1554 /* Unlock stream and return. */
1555 goto all_done;
299a95b9
RM
1556
1557 /* Here starts the more complex loop to handle positional parameters. */
1558do_positional:
1559 {
1560 /* Array with information about the needed arguments. This has to
6d52618b 1561 be dynamically extensible. */
299a95b9
RM
1562 size_t nspecs = 0;
1563 size_t nspecs_max = 32; /* A more or less arbitrary start value. */
1564 struct printf_spec *specs
1565 = alloca (nspecs_max * sizeof (struct printf_spec));
1566
1567 /* The number of arguments the format string requests. This will
1568 determine the size of the array needed to store the argument
1569 attributes. */
1570 size_t nargs = 0;
1571 int *args_type;
d64b6ad0 1572 union printf_arg *args_value = NULL;
299a95b9
RM
1573
1574 /* Positional parameters refer to arguments directly. This could
1575 also determine the maximum number of arguments. Track the
1576 maximum number. */
1577 size_t max_ref_arg = 0;
1578
1579 /* Just a counter. */
ba1ffaa1 1580 size_t cnt;
299a95b9
RM
1581
1582
1583 if (grouping == (const char *) -1)
a04e7405 1584 {
4295702f
UD
1585#ifdef COMPILE_WPRINTF
1586 thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
1587 _NL_NUMERIC_THOUSANDS_SEP_WC);
1588#else
1589 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
1590#endif
1591
299a95b9 1592 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
4295702f 1593 if (*grouping == '\0' || *grouping == CHAR_MAX)
299a95b9
RM
1594 grouping = NULL;
1595 }
1596
d64b6ad0 1597 for (f = lead_str_end; *f != L_('\0'); f = specs[nspecs++].next_fmt)
299a95b9
RM
1598 {
1599 if (nspecs >= nspecs_max)
1600 {
1601 /* Extend the array of format specifiers. */
1602 struct printf_spec *old = specs;
1603
1604 nspecs_max *= 2;
1605 specs = alloca (nspecs_max * sizeof (struct printf_spec));
1606
1607 if (specs == &old[nspecs])
1608 /* Stack grows up, OLD was the last thing allocated;
1609 extend it. */
1610 nspecs_max += nspecs_max / 2;
1611 else
1612 {
1613 /* Copy the old array's elements to the new space. */
1614 memcpy (specs, old, nspecs * sizeof (struct printf_spec));
1615 if (old == &specs[nspecs])
1616 /* Stack grows down, OLD was just below the new
1617 SPECS. We can use that space when the new space
1618 runs out. */
1619 nspecs_max += nspecs_max / 2;
1620 }
1621 }
1622
1623 /* Parse the format specifier. */
d64b6ad0
UD
1624#ifdef COMPILE_WPRINTF
1625 nargs += parse_one_spec (f, nargs, &specs[nspecs], &max_ref_arg);
1626#else
ec42724d
RM
1627 nargs += parse_one_spec (f, nargs, &specs[nspecs], &max_ref_arg,
1628 &mbstate);
d64b6ad0 1629#endif
299a95b9
RM
1630 }
1631
1632 /* Determine the number of arguments the format string consumes. */
1633 nargs = MAX (nargs, max_ref_arg);
1634
1635 /* Allocate memory for the argument descriptions. */
1636 args_type = alloca (nargs * sizeof (int));
1637 memset (args_type, 0, nargs * sizeof (int));
1638 args_value = alloca (nargs * sizeof (union printf_arg));
1639
1640 /* XXX Could do sanity check here: If any element in ARGS_TYPE is
1641 still zero after this loop, format is invalid. For now we
1642 simply use 0 as the value. */
1643
1644 /* Fill in the types of all the arguments. */
1645 for (cnt = 0; cnt < nspecs; ++cnt)
1646 {
1647 /* If the width is determined by an argument this is an int. */
1648 if (specs[cnt].width_arg != -1)
1649 args_type[specs[cnt].width_arg] = PA_INT;
1650
1651 /* If the precision is determined by an argument this is an int. */
1652 if (specs[cnt].prec_arg != -1)
1653 args_type[specs[cnt].prec_arg] = PA_INT;
1654
1655 switch (specs[cnt].ndata_args)
1656 {
1657 case 0: /* No arguments. */
1658 break;
1659 case 1: /* One argument; we already have the type. */
1660 args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
1661 break;
1662 default:
1663 /* We have more than one argument for this format spec.
1664 We must call the arginfo function again to determine
1665 all the types. */
1666 (void) (*__printf_arginfo_table[specs[cnt].info.spec])
1667 (&specs[cnt].info,
1668 specs[cnt].ndata_args, &args_type[specs[cnt].data_arg]);
1669 break;
1670 }
1671 }
1672
1673 /* Now we know all the types and the order. Fill in the argument
1674 values. */
7cc27f44 1675 for (cnt = 0; cnt < nargs; ++cnt)
299a95b9
RM
1676 switch (args_type[cnt])
1677 {
a04e7405 1678#define T(tag, mem, type) \
299a95b9 1679 case tag: \
7cc27f44 1680 args_value[cnt].mem = va_arg (ap_save, type); \
299a95b9 1681 break
a04e7405
RM
1682
1683 T (PA_CHAR, pa_char, int); /* Promoted. */
2c6fe0bd 1684 T (PA_WCHAR, pa_wchar, wint_t);
a04e7405
RM
1685 T (PA_INT|PA_FLAG_SHORT, pa_short_int, int); /* Promoted. */
1686 T (PA_INT, pa_int, int);
1687 T (PA_INT|PA_FLAG_LONG, pa_long_int, long int);
1688 T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
1689 T (PA_FLOAT, pa_float, double); /* Promoted. */
1690 T (PA_DOUBLE, pa_double, double);
1691 T (PA_DOUBLE|PA_FLAG_LONG_DOUBLE, pa_long_double, long double);
1692 T (PA_STRING, pa_string, const char *);
2c6fe0bd 1693 T (PA_WSTRING, pa_wstring, const wchar_t *);
a04e7405
RM
1694 T (PA_POINTER, pa_pointer, void *);
1695#undef T
299a95b9
RM
1696 default:
1697 if ((args_type[cnt] & PA_FLAG_PTR) != 0)
7cc27f44 1698 args_value[cnt].pa_pointer = va_arg (ap_save, void *);
299a95b9
RM
1699 else
1700 args_value[cnt].pa_long_double = 0.0;
1701 break;
1702 }
a04e7405 1703
299a95b9 1704 /* Now walk through all format specifiers and process them. */
ba1ffaa1 1705 for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
299a95b9
RM
1706 {
1707#undef REF
b5567b2a 1708#if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
3d558f4e
UD
1709# define REF(Name) &&do2_##Name - &&do_form_unknown
1710#else
1711# define REF(Name) &&do2_##Name
1712#endif
299a95b9
RM
1713#undef LABEL
1714#define LABEL(Name) do2_##Name
1715 STEP4_TABLE;
1716
1717 int is_negative;
1718 union
28f540f4 1719 {
299a95b9
RM
1720 unsigned long long int longlong;
1721 unsigned long int word;
1722 } number;
1723 int base;
1724 union printf_arg the_arg;
655c0697 1725 CHAR_T *string; /* Pointer to argument string. */
299a95b9
RM
1726
1727 /* Fill variables from values in struct. */
1728 int alt = specs[nspecs_done].info.alt;
1729 int space = specs[nspecs_done].info.space;
1730 int left = specs[nspecs_done].info.left;
1731 int showsign = specs[nspecs_done].info.showsign;
1732 int group = specs[nspecs_done].info.group;
1733 int is_long_double = specs[nspecs_done].info.is_long_double;
6591c335
UD
1734 int is_short = specs[nspecs_done].info.is_short;
1735 int is_char = specs[nspecs_done].info.is_char;
299a95b9
RM
1736 int is_long = specs[nspecs_done].info.is_long;
1737 int width = specs[nspecs_done].info.width;
1738 int prec = specs[nspecs_done].info.prec;
e115dbd7 1739 int use_outdigits = specs[nspecs_done].info.i18n;
299a95b9
RM
1740 char pad = specs[nspecs_done].info.pad;
1741 CHAR_T spec = specs[nspecs_done].info.spec;
1742
1743 /* Fill in last information. */
1744 if (specs[nspecs_done].width_arg != -1)
1745 {
1746 /* Extract the field width from an argument. */
1747 specs[nspecs_done].info.width =
1748 args_value[specs[nspecs_done].width_arg].pa_int;
1749
1750 if (specs[nspecs_done].info.width < 0)
1751 /* If the width value is negative left justification is
1752 selected and the value is taken as being positive. */
1753 {
1754 specs[nspecs_done].info.width *= -1;
1755 left = specs[nspecs_done].info.left = 1;
1756 }
1757 width = specs[nspecs_done].info.width;
1758 }
a04e7405 1759
299a95b9
RM
1760 if (specs[nspecs_done].prec_arg != -1)
1761 {
1762 /* Extract the precision from an argument. */
1763 specs[nspecs_done].info.prec =
1764 args_value[specs[nspecs_done].prec_arg].pa_int;
28f540f4 1765
299a95b9
RM
1766 if (specs[nspecs_done].info.prec < 0)
1767 /* If the precision is negative the precision is
1768 omitted. */
1769 specs[nspecs_done].info.prec = -1;
a04e7405 1770
299a95b9
RM
1771 prec = specs[nspecs_done].info.prec;
1772 }
28f540f4 1773
afe426a0 1774 /* Maybe the buffer is too small. */
d25adda7 1775 if (MAX (prec, width) + 32 > (int) (sizeof (work_buffer) / sizeof (CHAR_T)))
655c0697
UD
1776 workend = ((CHAR_T *) alloca ((MAX (prec, width) + 32)
1777 * sizeof (CHAR_T))
69c69fe1 1778 + (MAX (prec, width) + 32));
afe426a0 1779
299a95b9
RM
1780 /* Process format specifiers. */
1781 while (1)
1782 {
1783 JUMP (spec, step4_jumps);
28f540f4 1784
299a95b9 1785 process_arg ((&specs[nspecs_done]));
d64b6ad0 1786 process_string_arg ((&specs[nspecs_done]));
28f540f4 1787
299a95b9
RM
1788 LABEL (form_unknown):
1789 {
1790 extern printf_function **__printf_function_table;
1791 int function_done;
1792 printf_function *function;
1793 unsigned int i;
1794 const void **ptr;
28f540f4 1795
299a95b9
RM
1796 function =
1797 (__printf_function_table == NULL ? NULL :
1798 __printf_function_table[specs[nspecs_done].info.spec]);
28f540f4 1799
299a95b9
RM
1800 if (function == NULL)
1801 function = &printf_unknown;
a04e7405 1802
299a95b9
RM
1803 ptr = alloca (specs[nspecs_done].ndata_args
1804 * sizeof (const void *));
1805
1806 /* Fill in an array of pointers to the argument values. */
1807 for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
1808 ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
28f540f4 1809
299a95b9
RM
1810 /* Call the function. */
1811 function_done = (*function) (s, &specs[nspecs_done].info, ptr);
1812
6d52618b 1813 /* If an error occurred we don't have information about #
299a95b9
RM
1814 of chars. */
1815 if (function_done < 0)
7c713e28 1816 {
edf5b2d7
UD
1817 done = -1;
1818 goto all_done;
7c713e28 1819 }
299a95b9
RM
1820
1821 done += function_done;
1822 }
1823 break;
28f540f4 1824 }
28f540f4 1825
299a95b9
RM
1826 /* Write the following constant string. */
1827 outstring (specs[nspecs_done].end_of_fmt,
1828 specs[nspecs_done].next_fmt
1829 - specs[nspecs_done].end_of_fmt);
1830 }
1831 }
28f540f4 1832
edf5b2d7 1833all_done:
7c713e28 1834 /* Unlock the stream. */
c0fb8a56
UD
1835#ifdef USE_IN_LIBIO
1836 _IO_funlockfile (s);
1837#else
1838 __funlockfile (s);
1839#endif
1840 __libc_cleanup_region_end (0);
aa1075ea 1841
28f540f4
RM
1842 return done;
1843}
299a95b9 1844\f
a04e7405
RM
1845/* Handle an unknown format specifier. This prints out a canonicalized
1846 representation of the format spec itself. */
28f540f4 1847static int
299a95b9
RM
1848printf_unknown (FILE *s, const struct printf_info *info,
1849 const void *const *args)
1850
28f540f4
RM
1851{
1852 int done = 0;
d64b6ad0 1853 CHAR_T work_buffer[MAX (info->width, info->spec) + 32];
655c0697 1854 CHAR_T *const workend
69c69fe1 1855 = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
d64b6ad0 1856 register CHAR_T *w;
28f540f4 1857
d64b6ad0 1858 outchar (L_('%'));
28f540f4
RM
1859
1860 if (info->alt)
d64b6ad0 1861 outchar (L_('#'));
28f540f4 1862 if (info->group)
d64b6ad0 1863 outchar (L_('\''));
28f540f4 1864 if (info->showsign)
d64b6ad0 1865 outchar (L_('+'));
28f540f4 1866 else if (info->space)
d64b6ad0 1867 outchar (L_(' '));
28f540f4 1868 if (info->left)
d64b6ad0 1869 outchar (L_('-'));
655c0697 1870 if (info->pad == L_('0'))
d64b6ad0 1871 outchar (L_('0'));
4295702f
UD
1872 if (info->i18n)
1873 outchar (L_('I'));
28f540f4 1874
a04e7405 1875 if (info->width != 0)
28f540f4 1876 {
69c69fe1
UD
1877 w = _itoa_word (info->width, workend, 10, 0);
1878 while (w < workend)
2f6d1f1b 1879 outchar (*w++);
28f540f4 1880 }
28f540f4
RM
1881
1882 if (info->prec != -1)
1883 {
655c0697 1884 outchar (L_('.'));
69c69fe1
UD
1885 w = _itoa_word (info->prec, workend, 10, 0);
1886 while (w < workend)
2f6d1f1b 1887 outchar (*w++);
28f540f4
RM
1888 }
1889
d64b6ad0 1890 if (info->spec != L_('\0'))
a04e7405 1891 outchar (info->spec);
28f540f4 1892
d8334b9a 1893 all_done:
28f540f4
RM
1894 return done;
1895}
1896\f
1897/* Group the digits according to the grouping rules of the current locale.
1898 The interpretation of GROUPING is as in `struct lconv' from <locale.h>. */
655c0697 1899static CHAR_T *
dfd2257a 1900internal_function
655c0697 1901group_number (CHAR_T *w, CHAR_T *rear_ptr, const char *grouping,
4295702f
UD
1902#ifdef COMPILE_WPRINTF
1903 wchar_t thousands_sep
1904#else
1905 const char *thousands_sep
1906#endif
1907 )
28f540f4
RM
1908{
1909 int len;
655c0697 1910 CHAR_T *src, *s;
4295702f
UD
1911#ifndef COMPILE_WPRINTF
1912 int tlen = strlen (thousands_sep);
1913#endif
28f540f4
RM
1914
1915 /* We treat all negative values like CHAR_MAX. */
1916
feb3c934 1917 if (*grouping == CHAR_MAX || *grouping <= 0)
28f540f4
RM
1918 /* No grouping should be done. */
1919 return w;
1920
1921 len = *grouping;
1922
1923 /* Copy existing string so that nothing gets overwritten. */
655c0697 1924 src = (CHAR_T *) alloca ((rear_ptr - w) * sizeof (CHAR_T));
69c69fe1
UD
1925 s = (CHAR_T *) __mempcpy (src, w,
1926 (rear_ptr - w) * sizeof (CHAR_T));
299a95b9 1927 w = rear_ptr;
28f540f4
RM
1928
1929 /* Process all characters in the string. */
69c69fe1 1930 while (s > src)
28f540f4 1931 {
69c69fe1 1932 *--w = *--s;
28f540f4 1933
69c69fe1 1934 if (--len == 0 && s > src)
28f540f4
RM
1935 {
1936 /* A new group begins. */
4295702f 1937#ifdef COMPILE_WPRINTF
69c69fe1 1938 *--w = thousands_sep;
4295702f
UD
1939#else
1940 int cnt = tlen;
1941 do
69c69fe1 1942 *--w = thousands_sep[--cnt];
4295702f
UD
1943 while (cnt > 0);
1944#endif
28f540f4
RM
1945
1946 len = *grouping++;
1947 if (*grouping == '\0')
1948 /* The previous grouping repeats ad infinitum. */
1949 --grouping;
60c96635
UD
1950 else if (*grouping == CHAR_MAX
1951#if CHAR_MIN < 0
1952 || *grouping < 0
1953#endif
1954 )
28f540f4
RM
1955 {
1956 /* No further grouping to be done.
1957 Copy the rest of the number. */
1958 do
69c69fe1
UD
1959 *--w = *--s;
1960 while (s > src);
28f540f4
RM
1961 break;
1962 }
1963 }
1964 }
28f540f4
RM
1965 return w;
1966}
1967\f
1968#ifdef USE_IN_LIBIO
1969/* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */
1970struct helper_file
1971 {
1972 struct _IO_FILE_plus _f;
9c38a689
UD
1973#ifdef COMPILE_WPRINTF
1974 struct _IO_wide_data _wide_data;
1975#endif
28f540f4 1976 _IO_FILE *_put_stream;
c4029823
UD
1977#ifdef _IO_MTSAFE_IO
1978 _IO_lock_t lock;
1979#endif
28f540f4
RM
1980 };
1981
1982static int
522548fb 1983_IO_helper_overflow (_IO_FILE *s, int c)
28f540f4
RM
1984{
1985 _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
d64b6ad0
UD
1986#ifdef COMPILE_WPRINTF
1987 int used = s->_wide_data->_IO_write_ptr - s->_wide_data->_IO_write_base;
1988 if (used)
1989 {
1990 _IO_size_t written = _IO_sputn (target, s->_wide_data->_IO_write_base,
1991 used);
1992 s->_wide_data->_IO_write_ptr -= written;
1993 }
1994#else
28f540f4
RM
1995 int used = s->_IO_write_ptr - s->_IO_write_base;
1996 if (used)
1997 {
1998 _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
1999 s->_IO_write_ptr -= written;
2000 }
d64b6ad0 2001#endif
7c713e28 2002 return PUTC (c, s);
28f540f4
RM
2003}
2004
9c38a689
UD
2005#ifdef COMPILE_WPRINTF
2006static const struct _IO_jump_t _IO_helper_jumps =
2007{
2008 JUMP_INIT_DUMMY,
77fe0b9c 2009 JUMP_INIT (finish, INTUSE(_IO_wdefault_finish)),
9c38a689
UD
2010 JUMP_INIT (overflow, _IO_helper_overflow),
2011 JUMP_INIT (underflow, _IO_default_underflow),
77fe0b9c
UD
2012 JUMP_INIT (uflow, INTUSE(_IO_default_uflow)),
2013 JUMP_INIT (pbackfail, (_IO_pbackfail_t) INTUSE(_IO_wdefault_pbackfail)),
2014 JUMP_INIT (xsputn, INTUSE(_IO_wdefault_xsputn)),
2015 JUMP_INIT (xsgetn, INTUSE(_IO_wdefault_xsgetn)),
9c38a689
UD
2016 JUMP_INIT (seekoff, _IO_default_seekoff),
2017 JUMP_INIT (seekpos, _IO_default_seekpos),
77fe0b9c 2018 JUMP_INIT (setbuf,(_IO_setbuf_t) INTUSE(_IO_wdefault_setbuf)),
9c38a689 2019 JUMP_INIT (sync, _IO_default_sync),
77fe0b9c 2020 JUMP_INIT (doallocate, INTUSE(_IO_wdefault_doallocate)),
9c38a689
UD
2021 JUMP_INIT (read, _IO_default_read),
2022 JUMP_INIT (write, _IO_default_write),
2023 JUMP_INIT (seek, _IO_default_seek),
2024 JUMP_INIT (close, _IO_default_close),
2025 JUMP_INIT (stat, _IO_default_stat)
2026};
2027#else
28f540f4 2028static const struct _IO_jump_t _IO_helper_jumps =
299a95b9
RM
2029{
2030 JUMP_INIT_DUMMY,
77fe0b9c 2031 JUMP_INIT (finish, INTUSE(_IO_default_finish)),
299a95b9
RM
2032 JUMP_INIT (overflow, _IO_helper_overflow),
2033 JUMP_INIT (underflow, _IO_default_underflow),
77fe0b9c
UD
2034 JUMP_INIT (uflow, INTUSE(_IO_default_uflow)),
2035 JUMP_INIT (pbackfail, INTUSE(_IO_default_pbackfail)),
2036 JUMP_INIT (xsputn, INTUSE(_IO_default_xsputn)),
2037 JUMP_INIT (xsgetn, INTUSE(_IO_default_xsgetn)),
299a95b9
RM
2038 JUMP_INIT (seekoff, _IO_default_seekoff),
2039 JUMP_INIT (seekpos, _IO_default_seekpos),
2040 JUMP_INIT (setbuf, _IO_default_setbuf),
2041 JUMP_INIT (sync, _IO_default_sync),
77fe0b9c 2042 JUMP_INIT (doallocate, INTUSE(_IO_default_doallocate)),
299a95b9
RM
2043 JUMP_INIT (read, _IO_default_read),
2044 JUMP_INIT (write, _IO_default_write),
2045 JUMP_INIT (seek, _IO_default_seek),
2046 JUMP_INIT (close, _IO_default_close),
2047 JUMP_INIT (stat, _IO_default_stat)
2048};
9c38a689 2049#endif
28f540f4
RM
2050
2051static int
dfd2257a 2052internal_function
299a95b9
RM
2053buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format,
2054 _IO_va_list args)
28f540f4 2055{
d64b6ad0 2056 CHAR_T buf[_IO_BUFSIZ];
28f540f4 2057 struct helper_file helper;
2ca8b1ee 2058 register _IO_FILE *hp = (_IO_FILE *) &helper._f;
28f540f4
RM
2059 int result, to_flush;
2060
655de5fd
UD
2061 /* Orient the stream. */
2062#ifdef ORIENT
2063 ORIENT;
2064#endif
2065
28f540f4
RM
2066 /* Initialize helper. */
2067 helper._put_stream = s;
d64b6ad0 2068#ifdef COMPILE_WPRINTF
9c38a689 2069 hp->_wide_data = &helper._wide_data;
d64b6ad0 2070 _IO_wsetp (hp, buf, buf + sizeof buf / sizeof (CHAR_T));
110215a9 2071 hp->_mode = 1;
d64b6ad0
UD
2072#else
2073 _IO_setp (hp, buf, buf + sizeof buf);
110215a9 2074 hp->_mode = -1;
d64b6ad0 2075#endif
c020d48c 2076 hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS|_IO_USER_LOCK;
bd355af0
UD
2077#if _IO_JUMPS_OFFSET
2078 hp->_vtable_offset = 0;
2079#endif
c4029823 2080#ifdef _IO_MTSAFE_IO
c020d48c 2081 hp->_lock = NULL;
c4029823 2082#endif
2ca8b1ee 2083 _IO_JUMPS (&helper._f) = (struct _IO_jump_t *) &_IO_helper_jumps;
96aa2d94 2084
28f540f4 2085 /* Now print to helper instead. */
77fe0b9c
UD
2086#if defined USE_IN_LIBIO && !defined COMPILE_WPRINTF
2087 result = INTUSE(_IO_vfprintf) (hp, format, args);
2088#else
d64b6ad0 2089 result = vfprintf (hp, format, args);
77fe0b9c 2090#endif
28f540f4 2091
5ef2d37b 2092 /* Lock stream. */
0dce3d15 2093 __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, s);
5ef2d37b
UD
2094 _IO_flockfile (s);
2095
28f540f4 2096 /* Now flush anything from the helper to the S. */
d64b6ad0
UD
2097#ifdef COMPILE_WPRINTF
2098 if ((to_flush = (hp->_wide_data->_IO_write_ptr
2099 - hp->_wide_data->_IO_write_base)) > 0)
2100 {
2101 if ((int) _IO_sputn (s, hp->_wide_data->_IO_write_base, to_flush)
2102 != to_flush)
5ef2d37b 2103 result = -1;
d64b6ad0
UD
2104 }
2105#else
28f540f4
RM
2106 if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
2107 {
ba1ffaa1 2108 if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
5ef2d37b 2109 result = -1;
28f540f4 2110 }
d64b6ad0 2111#endif
28f540f4 2112
5ef2d37b
UD
2113 /* Unlock the stream. */
2114 _IO_funlockfile (s);
2115 __libc_cleanup_region_end (0);
2116
28f540f4
RM
2117 return result;
2118}
2119
2120#else /* !USE_IN_LIBIO */
2121
2122static int
dfd2257a 2123internal_function
299a95b9 2124buffered_vfprintf (register FILE *s, const CHAR_T *format, va_list args)
28f540f4
RM
2125{
2126 char buf[BUFSIZ];
2127 int result;
2128
655de5fd
UD
2129 /* Orient the stream. */
2130#ifdef ORIENT
2131 ORIENT;
2132#endif
2133
28f540f4
RM
2134 s->__bufp = s->__buffer = buf;
2135 s->__bufsize = sizeof buf;
2136 s->__put_limit = s->__buffer + s->__bufsize;
2137 s->__get_limit = s->__buffer;
2138
2139 /* Now use buffer to print. */
2140 result = vfprintf (s, format, args);
2141
2142 if (fflush (s) == EOF)
c21ec326 2143 result = -1;
28f540f4
RM
2144 s->__buffer = s->__bufp = s->__get_limit = s->__put_limit = NULL;
2145 s->__bufsize = 0;
2146
2147 return result;
2148}
299a95b9 2149\f
28f540f4
RM
2150/* Pads string with given number of a specified character.
2151 This code is taken from iopadn.c of the GNU I/O library. */
2152#define PADSIZE 16
299a95b9
RM
2153static const CHAR_T blanks[PADSIZE] =
2154{ L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '),
2155 L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' ') };
2156static const CHAR_T zeroes[PADSIZE] =
2157{ L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'),
2158 L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0') };
28f540f4
RM
2159
2160ssize_t
299a95b9
RM
2161#ifndef COMPILE_WPRINTF
2162__printf_pad (FILE *s, char pad, size_t count)
2163#else
2164__wprintf_pad (FILE *s, wchar_t pad, size_t count)
2165#endif
28f540f4 2166{
299a95b9 2167 const CHAR_T *padptr;
a04e7405 2168 register size_t i;
28f540f4 2169
299a95b9 2170 padptr = pad == L_(' ') ? blanks : zeroes;
28f540f4
RM
2171
2172 for (i = count; i >= PADSIZE; i -= PADSIZE)
a04e7405
RM
2173 if (PUT (s, padptr, PADSIZE) != PADSIZE)
2174 return -1;
28f540f4 2175 if (i > 0)
a04e7405
RM
2176 if (PUT (s, padptr, i) != i)
2177 return -1;
2178
2179 return count;
28f540f4
RM
2180}
2181#undef PADSIZE
2182#endif /* USE_IN_LIBIO */
d64b6ad0
UD
2183
2184#ifdef USE_IN_LIBIO
2185# undef vfprintf
2186# ifdef strong_alias
2187/* This is for glibc. */
2188# ifdef COMPILE_WPRINTF
51028f34
UD
2189strong_alias (_IO_vfwprintf, __vfwprintf);
2190weak_alias (_IO_vfwprintf, vfwprintf);
d64b6ad0
UD
2191# else
2192strong_alias (_IO_vfprintf, vfprintf);
77fe0b9c 2193INTDEF(_IO_vfprintf)
d64b6ad0
UD
2194# endif
2195# else
2196# if defined __ELF__ || defined __GNU_LIBRARY__
2197# include <gnu-stabs.h>
2198# ifdef weak_alias
2199# ifdef COMPILE_WPRINTF
2200weak_alias (_IO_vfwprintf, vfwprintf);
2201# else
2202weak_alias (_IO_vfprintf, vfprintf);
2203# endif
2204# endif
2205# endif
2206# endif
2207#endif