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