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