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