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