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