Elliott Hughes | 506c6de | 2016-01-15 16:30:18 -0800 | [diff] [blame] | 1 | /* $OpenBSD: vfwprintf.c,v 1.15 2015/12/28 22:08:18 mmcc Exp $ */ |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 2 | /*- |
| 3 | * Copyright (c) 1990 The Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This code is derived from software contributed to Berkeley by |
| 7 | * Chris Torek. |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * 3. Neither the name of the University nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this software |
| 19 | * without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. |
| 32 | */ |
| 33 | |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 34 | #define CHAR_TYPE wchar_t |
Elliott Hughes | 93a1f8b | 2017-11-07 22:52:29 -0800 | [diff] [blame] | 35 | #define FUNCTION_NAME __vfwprintf |
Elliott Hughes | bc27bdc | 2017-11-10 15:25:49 -0800 | [diff] [blame] | 36 | #define CHAR_TYPE_STRLEN wcslen |
| 37 | #define CHAR_TYPE_STRNLEN wcsnlen |
| 38 | #define CHAR_TYPE_INF L"INF" |
| 39 | #define CHAR_TYPE_inf L"inf" |
| 40 | #define CHAR_TYPE_NAN L"NAN" |
| 41 | #define CHAR_TYPE_nan L"nan" |
| 42 | #define CHAR_TYPE_ORIENTATION 1 |
Elliott Hughes | 1f49317 | 2017-11-08 16:13:18 -0800 | [diff] [blame] | 43 | #include "printf_common.h" |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 44 | |
Elliott Hughes | 1f49317 | 2017-11-08 16:13:18 -0800 | [diff] [blame] | 45 | int FUNCTION_NAME(FILE* fp, const CHAR_TYPE* fmt0, va_list ap) { |
Elliott Hughes | 654cd83 | 2018-08-30 16:00:42 -0700 | [diff] [blame^] | 46 | int caller_errno = errno; |
Elliott Hughes | b70576b | 2017-11-13 11:10:05 -0800 | [diff] [blame] | 47 | int n, n2; |
Elliott Hughes | 93a1f8b | 2017-11-07 22:52:29 -0800 | [diff] [blame] | 48 | CHAR_TYPE* cp; /* handy char pointer (short term usage) */ |
| 49 | CHAR_TYPE sign; /* sign prefix (' ', '+', '-', or \0) */ |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 50 | int flags; /* flags as above */ |
| 51 | int ret; /* return value accumulator */ |
| 52 | int width; /* width from format (%8d), or 0 */ |
| 53 | int prec; /* precision from format; <0 for N/A */ |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 54 | /* |
| 55 | * We can decompose the printed representation of floating |
| 56 | * point numbers into several parts, some of which may be empty: |
| 57 | * |
| 58 | * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ |
| 59 | * A B ---C--- D E F |
| 60 | * |
| 61 | * A: 'sign' holds this value if present; '\0' otherwise |
| 62 | * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal |
| 63 | * C: cp points to the string MMMNNN. Leading and trailing |
| 64 | * zeros are not in the string and must be added. |
| 65 | * D: expchar holds this character; '\0' if no exponent, e.g. %f |
| 66 | * F: at least two digits for decimal, at least one digit for hex |
| 67 | */ |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 68 | char* decimal_point = nullptr; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 69 | int signflag; /* true if float is negative */ |
| 70 | union { /* floating point arguments %[aAeEfFgG] */ |
| 71 | double dbl; |
| 72 | long double ldbl; |
| 73 | } fparg; |
| 74 | int expt; /* integer value of exponent */ |
| 75 | char expchar; /* exponent character: [eEpP\0] */ |
| 76 | char* dtoaend; /* pointer to end of converted digits */ |
| 77 | int expsize; /* character count for expstr */ |
| 78 | int lead; /* sig figs before decimal or group sep */ |
| 79 | int ndig; /* actual number of digits returned by dtoa */ |
Elliott Hughes | 93a1f8b | 2017-11-07 22:52:29 -0800 | [diff] [blame] | 80 | CHAR_TYPE expstr[MAXEXPDIG + 2]; /* buffer for exponent string: e+ZZZ */ |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 81 | char* dtoaresult = nullptr; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 82 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 83 | uintmax_t _umax; /* integer arguments %[diouxX] */ |
| 84 | enum { OCT, DEC, HEX } base; /* base for %[diouxX] conversion */ |
| 85 | int dprec; /* a copy of prec if %[diouxX], 0 otherwise */ |
| 86 | int realsz; /* field size expanded by dprec */ |
| 87 | int size; /* size of converted field or string */ |
| 88 | const char* xdigs; /* digits for %[xX] conversion */ |
Elliott Hughes | b70576b | 2017-11-13 11:10:05 -0800 | [diff] [blame] | 89 | #define NIOV 8 |
| 90 | struct __suio uio; /* output information: summary */ |
| 91 | struct __siov iov[NIOV]; /* ... and individual io vectors */ |
| 92 | struct __siov* iovp; /* for PRINT macro */ |
Elliott Hughes | 93a1f8b | 2017-11-07 22:52:29 -0800 | [diff] [blame] | 93 | CHAR_TYPE buf[BUF]; /* buffer with space for digits of uintmax_t */ |
| 94 | CHAR_TYPE ox[2]; /* space for 0x; ox[1] is either x, X, or \0 */ |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 95 | union arg* argtable; /* args, built due to positional arg */ |
| 96 | union arg statargtable[STATIC_ARG_TBL_SIZE]; |
| 97 | size_t argtablesiz; |
| 98 | int nextarg; /* 1-based argument index */ |
| 99 | va_list orgap; /* original argument pointer */ |
Elliott Hughes | 93a1f8b | 2017-11-07 22:52:29 -0800 | [diff] [blame] | 100 | CHAR_TYPE* convbuf; /* buffer for wide/multibyte conversion */ |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 101 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 102 | /* |
| 103 | * Choose PADSIZE to trade efficiency vs. size. If larger printf |
| 104 | * fields occur frequently, increase PADSIZE and make the initialisers |
| 105 | * below longer. |
| 106 | */ |
| 107 | #define PADSIZE 16 /* pad chunk size */ |
Elliott Hughes | 5305a4d | 2017-11-03 14:00:37 -0700 | [diff] [blame] | 108 | static CHAR_TYPE blanks[PADSIZE] = { |
| 109 | ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' |
| 110 | }; |
| 111 | static CHAR_TYPE zeroes[PADSIZE] = { |
| 112 | '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' |
| 113 | }; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 114 | |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 115 | static const char xdigs_lower[] = "0123456789abcdef"; |
| 116 | static const char xdigs_upper[] = "0123456789ABCDEF"; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 117 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 118 | #define PRINT(ptr, len) \ |
| 119 | do { \ |
Elliott Hughes | b70576b | 2017-11-13 11:10:05 -0800 | [diff] [blame] | 120 | for (int n3 = 0; n3 < (len); n3++) { \ |
Elliott Hughes | bc27bdc | 2017-11-10 15:25:49 -0800 | [diff] [blame] | 121 | if ((helpers::xfputwc((ptr)[n3], fp)) == WEOF) goto error; \ |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 122 | } \ |
| 123 | } while (0) |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 124 | |
Elliott Hughes | bc27bdc | 2017-11-10 15:25:49 -0800 | [diff] [blame] | 125 | _SET_ORIENTATION(fp, CHAR_TYPE_ORIENTATION); |
Elliott Hughes | 5305a4d | 2017-11-03 14:00:37 -0700 | [diff] [blame] | 126 | |
| 127 | // Writing "" to a read only file returns EOF, not 0. |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 128 | if (cantwrite(fp)) { |
| 129 | errno = EBADF; |
Elliott Hughes | 5305a4d | 2017-11-03 14:00:37 -0700 | [diff] [blame] | 130 | return EOF; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 131 | } |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 132 | |
Elliott Hughes | 5305a4d | 2017-11-03 14:00:37 -0700 | [diff] [blame] | 133 | // Optimize writes to stderr and other unbuffered files). |
| 134 | if ((fp->_flags & (__SNBF | __SWR | __SRW)) == (__SNBF | __SWR) && fp->_file >= 0) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 135 | return (__sbprintf(fp, fmt0, ap)); |
Elliott Hughes | 5305a4d | 2017-11-03 14:00:37 -0700 | [diff] [blame] | 136 | } |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 137 | |
Elliott Hughes | 5305a4d | 2017-11-03 14:00:37 -0700 | [diff] [blame] | 138 | CHAR_TYPE* fmt = const_cast<CHAR_TYPE*>(fmt0); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 139 | argtable = nullptr; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 140 | nextarg = 1; |
| 141 | va_copy(orgap, ap); |
Elliott Hughes | b70576b | 2017-11-13 11:10:05 -0800 | [diff] [blame] | 142 | uio.uio_iov = iovp = iov; |
| 143 | uio.uio_resid = 0; |
| 144 | uio.uio_iovcnt = 0; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 145 | ret = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 146 | convbuf = nullptr; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 147 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 148 | /* |
| 149 | * Scan the format for conversions (`%' character). |
| 150 | */ |
| 151 | for (;;) { |
Elliott Hughes | b70576b | 2017-11-13 11:10:05 -0800 | [diff] [blame] | 152 | int ch; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 153 | for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) continue; |
| 154 | if (fmt != cp) { |
| 155 | ptrdiff_t m = fmt - cp; |
| 156 | if (m < 0 || m > INT_MAX - ret) goto overflow; |
| 157 | PRINT(cp, m); |
| 158 | ret += m; |
| 159 | } |
| 160 | if (ch == '\0') goto done; |
| 161 | fmt++; /* skip over '%' */ |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 162 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 163 | flags = 0; |
| 164 | dprec = 0; |
| 165 | width = 0; |
| 166 | prec = -1; |
| 167 | sign = '\0'; |
| 168 | ox[1] = '\0'; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 169 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 170 | rflag: |
| 171 | ch = *fmt++; |
| 172 | reswitch: |
| 173 | switch (ch) { |
| 174 | case ' ': |
| 175 | /* |
| 176 | * ``If the space and + flags both appear, the space |
| 177 | * flag will be ignored.'' |
| 178 | * -- ANSI X3J11 |
| 179 | */ |
| 180 | if (!sign) sign = ' '; |
| 181 | goto rflag; |
| 182 | case '#': |
| 183 | flags |= ALT; |
| 184 | goto rflag; |
| 185 | case '\'': |
| 186 | /* grouping not implemented */ |
| 187 | goto rflag; |
| 188 | case '*': |
| 189 | /* |
| 190 | * ``A negative field width argument is taken as a |
| 191 | * - flag followed by a positive field width.'' |
| 192 | * -- ANSI X3J11 |
| 193 | * They don't exclude field widths read from args. |
| 194 | */ |
| 195 | GETASTER(width); |
| 196 | if (width >= 0) goto rflag; |
| 197 | if (width == INT_MIN) goto overflow; |
| 198 | width = -width; |
George Burgess IV | fa5410f | 2018-08-13 17:44:06 -0700 | [diff] [blame] | 199 | __BIONIC_FALLTHROUGH; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 200 | case '-': |
| 201 | flags |= LADJUST; |
| 202 | goto rflag; |
| 203 | case '+': |
| 204 | sign = '+'; |
| 205 | goto rflag; |
| 206 | case '.': |
| 207 | if ((ch = *fmt++) == '*') { |
| 208 | GETASTER(n); |
| 209 | prec = n < 0 ? -1 : n; |
| 210 | goto rflag; |
| 211 | } |
| 212 | n = 0; |
| 213 | while (is_digit(ch)) { |
| 214 | APPEND_DIGIT(n, ch); |
| 215 | ch = *fmt++; |
| 216 | } |
| 217 | if (ch == '$') { |
| 218 | nextarg = n; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 219 | if (argtable == nullptr) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 220 | argtable = statargtable; |
Elliott Hughes | 5305a4d | 2017-11-03 14:00:37 -0700 | [diff] [blame] | 221 | if (__find_arguments(fmt0, orgap, &argtable, &argtablesiz) == -1) { |
| 222 | ret = -1; |
| 223 | goto error; |
| 224 | } |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 225 | } |
| 226 | goto rflag; |
| 227 | } |
| 228 | prec = n; |
| 229 | goto reswitch; |
| 230 | case '0': |
| 231 | /* |
| 232 | * ``Note that 0 is taken as a flag, not as the |
| 233 | * beginning of a field width.'' |
| 234 | * -- ANSI X3J11 |
| 235 | */ |
| 236 | flags |= ZEROPAD; |
| 237 | goto rflag; |
| 238 | case '1': |
| 239 | case '2': |
| 240 | case '3': |
| 241 | case '4': |
| 242 | case '5': |
| 243 | case '6': |
| 244 | case '7': |
| 245 | case '8': |
| 246 | case '9': |
| 247 | n = 0; |
| 248 | do { |
| 249 | APPEND_DIGIT(n, ch); |
| 250 | ch = *fmt++; |
| 251 | } while (is_digit(ch)); |
| 252 | if (ch == '$') { |
| 253 | nextarg = n; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 254 | if (argtable == nullptr) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 255 | argtable = statargtable; |
Elliott Hughes | 5305a4d | 2017-11-03 14:00:37 -0700 | [diff] [blame] | 256 | if (__find_arguments(fmt0, orgap, &argtable, &argtablesiz) == -1) { |
| 257 | ret = -1; |
| 258 | goto error; |
| 259 | } |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 260 | } |
| 261 | goto rflag; |
| 262 | } |
| 263 | width = n; |
| 264 | goto reswitch; |
| 265 | case 'L': |
| 266 | flags |= LONGDBL; |
| 267 | goto rflag; |
| 268 | case 'h': |
| 269 | if (*fmt == 'h') { |
| 270 | fmt++; |
| 271 | flags |= CHARINT; |
| 272 | } else { |
| 273 | flags |= SHORTINT; |
| 274 | } |
| 275 | goto rflag; |
| 276 | case 'j': |
| 277 | flags |= MAXINT; |
| 278 | goto rflag; |
| 279 | case 'l': |
| 280 | if (*fmt == 'l') { |
| 281 | fmt++; |
| 282 | flags |= LLONGINT; |
| 283 | } else { |
| 284 | flags |= LONGINT; |
| 285 | } |
| 286 | goto rflag; |
| 287 | case 'q': |
| 288 | flags |= LLONGINT; |
| 289 | goto rflag; |
| 290 | case 't': |
| 291 | flags |= PTRINT; |
| 292 | goto rflag; |
| 293 | case 'z': |
| 294 | flags |= SIZEINT; |
| 295 | goto rflag; |
| 296 | case 'C': |
| 297 | flags |= LONGINT; |
George Burgess IV | fa5410f | 2018-08-13 17:44:06 -0700 | [diff] [blame] | 298 | __BIONIC_FALLTHROUGH; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 299 | case 'c': |
| 300 | if (flags & LONGINT) |
| 301 | *(cp = buf) = (wchar_t)GETARG(wint_t); |
| 302 | else |
| 303 | *(cp = buf) = (wchar_t)btowc(GETARG(int)); |
| 304 | size = 1; |
| 305 | sign = '\0'; |
| 306 | break; |
| 307 | case 'D': |
| 308 | flags |= LONGINT; |
George Burgess IV | fa5410f | 2018-08-13 17:44:06 -0700 | [diff] [blame] | 309 | __BIONIC_FALLTHROUGH; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 310 | case 'd': |
| 311 | case 'i': |
| 312 | _umax = SARG(); |
| 313 | if ((intmax_t)_umax < 0) { |
| 314 | _umax = -_umax; |
| 315 | sign = '-'; |
| 316 | } |
| 317 | base = DEC; |
| 318 | goto number; |
| 319 | case 'a': |
| 320 | case 'A': |
| 321 | if (ch == 'a') { |
| 322 | ox[1] = 'x'; |
| 323 | xdigs = xdigs_lower; |
| 324 | expchar = 'p'; |
| 325 | } else { |
| 326 | ox[1] = 'X'; |
| 327 | xdigs = xdigs_upper; |
| 328 | expchar = 'P'; |
| 329 | } |
| 330 | if (prec >= 0) prec++; |
| 331 | if (dtoaresult) __freedtoa(dtoaresult); |
| 332 | if (flags & LONGDBL) { |
| 333 | fparg.ldbl = GETARG(long double); |
| 334 | dtoaresult = __hldtoa(fparg.ldbl, xdigs, prec, &expt, &signflag, &dtoaend); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 335 | if (dtoaresult == nullptr) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 336 | errno = ENOMEM; |
| 337 | goto error; |
| 338 | } |
| 339 | } else { |
| 340 | fparg.dbl = GETARG(double); |
| 341 | dtoaresult = __hdtoa(fparg.dbl, xdigs, prec, &expt, &signflag, &dtoaend); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 342 | if (dtoaresult == nullptr) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 343 | errno = ENOMEM; |
| 344 | goto error; |
| 345 | } |
| 346 | } |
| 347 | if (prec < 0) prec = dtoaend - dtoaresult; |
| 348 | if (expt == INT_MAX) ox[1] = '\0'; |
| 349 | free(convbuf); |
Elliott Hughes | bc27bdc | 2017-11-10 15:25:49 -0800 | [diff] [blame] | 350 | cp = convbuf = helpers::mbsconv(dtoaresult, -1); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 351 | if (cp == nullptr) goto error; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 352 | ndig = dtoaend - dtoaresult; |
| 353 | goto fp_common; |
| 354 | case 'e': |
| 355 | case 'E': |
| 356 | expchar = ch; |
| 357 | if (prec < 0) /* account for digit before decpt */ |
| 358 | prec = DEFPREC + 1; |
| 359 | else |
| 360 | prec++; |
| 361 | goto fp_begin; |
| 362 | case 'f': |
| 363 | case 'F': |
| 364 | expchar = '\0'; |
| 365 | goto fp_begin; |
| 366 | case 'g': |
| 367 | case 'G': |
| 368 | expchar = ch - ('g' - 'e'); |
| 369 | if (prec == 0) prec = 1; |
| 370 | fp_begin: |
| 371 | if (prec < 0) prec = DEFPREC; |
| 372 | if (dtoaresult) __freedtoa(dtoaresult); |
| 373 | if (flags & LONGDBL) { |
| 374 | fparg.ldbl = GETARG(long double); |
| 375 | dtoaresult = __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec, &expt, &signflag, &dtoaend); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 376 | if (dtoaresult == nullptr) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 377 | errno = ENOMEM; |
| 378 | goto error; |
| 379 | } |
| 380 | } else { |
| 381 | fparg.dbl = GETARG(double); |
| 382 | dtoaresult = __dtoa(fparg.dbl, expchar ? 2 : 3, prec, &expt, &signflag, &dtoaend); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 383 | if (dtoaresult == nullptr) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 384 | errno = ENOMEM; |
| 385 | goto error; |
| 386 | } |
| 387 | if (expt == 9999) expt = INT_MAX; |
| 388 | } |
| 389 | free(convbuf); |
Elliott Hughes | bc27bdc | 2017-11-10 15:25:49 -0800 | [diff] [blame] | 390 | cp = convbuf = helpers::mbsconv(dtoaresult, -1); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 391 | if (cp == nullptr) goto error; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 392 | ndig = dtoaend - dtoaresult; |
| 393 | fp_common: |
| 394 | if (signflag) sign = '-'; |
| 395 | if (expt == INT_MAX) { /* inf or nan */ |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 396 | if (*cp == 'N') { |
Elliott Hughes | bc27bdc | 2017-11-10 15:25:49 -0800 | [diff] [blame] | 397 | cp = const_cast<CHAR_TYPE*>((ch >= 'a') ? CHAR_TYPE_nan : CHAR_TYPE_NAN); |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 398 | } else { |
Elliott Hughes | bc27bdc | 2017-11-10 15:25:49 -0800 | [diff] [blame] | 399 | cp = const_cast<CHAR_TYPE*>((ch >= 'a') ? CHAR_TYPE_inf : CHAR_TYPE_INF); |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 400 | } |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 401 | size = 3; |
| 402 | flags &= ~ZEROPAD; |
| 403 | break; |
| 404 | } |
| 405 | flags |= FPT; |
| 406 | if (ch == 'g' || ch == 'G') { |
| 407 | if (expt > -4 && expt <= prec) { |
| 408 | /* Make %[gG] smell like %[fF] */ |
| 409 | expchar = '\0'; |
| 410 | if (flags & ALT) |
| 411 | prec -= expt; |
| 412 | else |
| 413 | prec = ndig - expt; |
| 414 | if (prec < 0) prec = 0; |
| 415 | } else { |
| 416 | /* |
| 417 | * Make %[gG] smell like %[eE], but |
| 418 | * trim trailing zeroes if no # flag. |
| 419 | */ |
| 420 | if (!(flags & ALT)) prec = ndig; |
| 421 | } |
| 422 | } |
| 423 | if (expchar) { |
| 424 | expsize = exponent(expstr, expt - 1, expchar); |
| 425 | size = expsize + prec; |
| 426 | if (prec > 1 || flags & ALT) ++size; |
| 427 | } else { |
| 428 | /* space for digits before decimal point */ |
| 429 | if (expt > 0) |
| 430 | size = expt; |
| 431 | else /* "0" */ |
| 432 | size = 1; |
| 433 | /* space for decimal pt and following digits */ |
| 434 | if (prec || flags & ALT) size += prec + 1; |
| 435 | lead = expt; |
| 436 | } |
| 437 | break; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 438 | case 'n': |
Elliott Hughes | 41398d0 | 2018-03-07 13:32:58 -0800 | [diff] [blame] | 439 | __fortify_fatal("%%n not allowed on Android"); |
Elliott Hughes | 654cd83 | 2018-08-30 16:00:42 -0700 | [diff] [blame^] | 440 | case 'm': |
| 441 | free(convbuf); |
| 442 | convbuf = helpers::mbsconv(strerror(caller_errno), prec); |
| 443 | if (convbuf == nullptr) { |
| 444 | fp->_flags |= __SERR; |
| 445 | goto error; |
| 446 | } else { |
| 447 | cp = convbuf; |
| 448 | } |
| 449 | goto string; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 450 | case 'O': |
| 451 | flags |= LONGINT; |
George Burgess IV | fa5410f | 2018-08-13 17:44:06 -0700 | [diff] [blame] | 452 | __BIONIC_FALLTHROUGH; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 453 | case 'o': |
| 454 | _umax = UARG(); |
| 455 | base = OCT; |
| 456 | goto nosign; |
| 457 | case 'p': |
| 458 | /* |
| 459 | * ``The argument shall be a pointer to void. The |
| 460 | * value of the pointer is converted to a sequence |
| 461 | * of printable characters, in an implementation- |
| 462 | * defined manner.'' |
| 463 | * -- ANSI X3J11 |
| 464 | */ |
| 465 | _umax = (u_long)GETARG(void*); |
| 466 | base = HEX; |
| 467 | xdigs = xdigs_lower; |
| 468 | ox[1] = 'x'; |
| 469 | goto nosign; |
| 470 | case 'S': |
| 471 | flags |= LONGINT; |
George Burgess IV | fa5410f | 2018-08-13 17:44:06 -0700 | [diff] [blame] | 472 | __BIONIC_FALLTHROUGH; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 473 | case 's': |
| 474 | if (flags & LONGINT) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 475 | if ((cp = GETARG(wchar_t*)) == nullptr) cp = const_cast<wchar_t*>(L"(null)"); |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 476 | } else { |
| 477 | char* mbsarg; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 478 | if ((mbsarg = GETARG(char*)) == nullptr) mbsarg = const_cast<char*>("(null)"); |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 479 | free(convbuf); |
Elliott Hughes | bc27bdc | 2017-11-10 15:25:49 -0800 | [diff] [blame] | 480 | convbuf = helpers::mbsconv(mbsarg, prec); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 481 | if (convbuf == nullptr) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 482 | fp->_flags |= __SERR; |
| 483 | goto error; |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 484 | } else { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 485 | cp = convbuf; |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 486 | } |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 487 | } |
Elliott Hughes | 654cd83 | 2018-08-30 16:00:42 -0700 | [diff] [blame^] | 488 | string: |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 489 | if (prec >= 0) { |
Elliott Hughes | bc27bdc | 2017-11-10 15:25:49 -0800 | [diff] [blame] | 490 | size = CHAR_TYPE_STRNLEN(cp, prec); |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 491 | } else { |
| 492 | size_t len; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 493 | |
Elliott Hughes | bc27bdc | 2017-11-10 15:25:49 -0800 | [diff] [blame] | 494 | if ((len = CHAR_TYPE_STRLEN(cp)) > INT_MAX) goto overflow; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 495 | size = (int)len; |
| 496 | } |
| 497 | sign = '\0'; |
| 498 | break; |
| 499 | case 'U': |
| 500 | flags |= LONGINT; |
George Burgess IV | fa5410f | 2018-08-13 17:44:06 -0700 | [diff] [blame] | 501 | __BIONIC_FALLTHROUGH; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 502 | case 'u': |
| 503 | _umax = UARG(); |
| 504 | base = DEC; |
| 505 | goto nosign; |
| 506 | case 'X': |
| 507 | xdigs = xdigs_upper; |
| 508 | goto hex; |
| 509 | case 'x': |
| 510 | xdigs = xdigs_lower; |
| 511 | hex: |
| 512 | _umax = UARG(); |
| 513 | base = HEX; |
| 514 | /* leading 0x/X only if non-zero */ |
| 515 | if (flags & ALT && _umax != 0) ox[1] = ch; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 516 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 517 | /* unsigned conversions */ |
| 518 | nosign: |
| 519 | sign = '\0'; |
| 520 | /* |
| 521 | * ``... diouXx conversions ... if a precision is |
| 522 | * specified, the 0 flag will be ignored.'' |
| 523 | * -- ANSI X3J11 |
| 524 | */ |
| 525 | number: |
| 526 | if ((dprec = prec) >= 0) flags &= ~ZEROPAD; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 527 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 528 | /* |
| 529 | * ``The result of converting a zero value with an |
| 530 | * explicit precision of zero is no characters.'' |
| 531 | * -- ANSI X3J11 |
| 532 | */ |
| 533 | cp = buf + BUF; |
| 534 | if (_umax != 0 || prec != 0) { |
| 535 | /* |
| 536 | * Unsigned mod is hard, and unsigned mod |
| 537 | * by a constant is easier than that by |
| 538 | * a variable; hence this switch. |
| 539 | */ |
| 540 | switch (base) { |
| 541 | case OCT: |
| 542 | do { |
| 543 | *--cp = to_char(_umax & 7); |
| 544 | _umax >>= 3; |
| 545 | } while (_umax); |
| 546 | /* handle octal leading 0 */ |
| 547 | if (flags & ALT && *cp != '0') *--cp = '0'; |
| 548 | break; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 549 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 550 | case DEC: |
| 551 | /* many numbers are 1 digit */ |
| 552 | while (_umax >= 10) { |
| 553 | *--cp = to_char(_umax % 10); |
| 554 | _umax /= 10; |
| 555 | } |
| 556 | *--cp = to_char(_umax); |
| 557 | break; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 558 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 559 | case HEX: |
| 560 | do { |
| 561 | *--cp = xdigs[_umax & 15]; |
| 562 | _umax >>= 4; |
| 563 | } while (_umax); |
| 564 | break; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 565 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 566 | default: |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 567 | abort(); |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | size = buf + BUF - cp; |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 571 | if (size > BUF) abort(); /* should never happen */ |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 572 | break; |
| 573 | default: /* "%?" prints ?, unless ? is NUL */ |
| 574 | if (ch == '\0') goto done; |
| 575 | /* pretend it was %c with argument ch */ |
| 576 | cp = buf; |
| 577 | *cp = ch; |
| 578 | size = 1; |
| 579 | sign = '\0'; |
| 580 | break; |
| 581 | } |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 582 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 583 | /* |
| 584 | * All reasonable formats wind up here. At this point, `cp' |
| 585 | * points to a string which (if not flags&LADJUST) should be |
| 586 | * padded out to `width' places. If flags&ZEROPAD, it should |
| 587 | * first be prefixed by any sign or other prefix; otherwise, |
| 588 | * it should be blank padded before the prefix is emitted. |
| 589 | * After any left-hand padding and prefixing, emit zeroes |
| 590 | * required by a decimal %[diouxX] precision, then print the |
| 591 | * string proper, then emit zeroes required by any leftover |
| 592 | * floating precision; finally, if LADJUST, pad with blanks. |
| 593 | * |
| 594 | * Compute actual size, so we know how much to pad. |
| 595 | * size excludes decimal prec; realsz includes it. |
| 596 | */ |
| 597 | realsz = dprec > size ? dprec : size; |
| 598 | if (sign) realsz++; |
| 599 | if (ox[1]) realsz += 2; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 600 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 601 | /* right-adjusting blank padding */ |
| 602 | if ((flags & (LADJUST | ZEROPAD)) == 0) PAD(width - realsz, blanks); |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 603 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 604 | /* prefix */ |
| 605 | if (sign) PRINT(&sign, 1); |
| 606 | if (ox[1]) { /* ox[1] is either x, X, or \0 */ |
| 607 | ox[0] = '0'; |
| 608 | PRINT(ox, 2); |
| 609 | } |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 610 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 611 | /* right-adjusting zero padding */ |
| 612 | if ((flags & (LADJUST | ZEROPAD)) == ZEROPAD) PAD(width - realsz, zeroes); |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 613 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 614 | /* leading zeroes from decimal precision */ |
| 615 | PAD(dprec - size, zeroes); |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 616 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 617 | /* the string or number proper */ |
| 618 | if ((flags & FPT) == 0) { |
| 619 | PRINT(cp, size); |
| 620 | } else { /* glue together f_p fragments */ |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 621 | if (decimal_point == nullptr) decimal_point = nl_langinfo(RADIXCHAR); |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 622 | if (!expchar) { /* %[fF] or sufficiently short %[gG] */ |
| 623 | if (expt <= 0) { |
| 624 | PRINT(zeroes, 1); |
| 625 | if (prec || flags & ALT) PRINT(decimal_point, 1); |
| 626 | PAD(-expt, zeroes); |
| 627 | /* already handled initial 0's */ |
| 628 | prec += expt; |
| 629 | } else { |
| 630 | PRINTANDPAD(cp, convbuf + ndig, lead, zeroes); |
| 631 | cp += lead; |
| 632 | if (prec || flags & ALT) PRINT(decimal_point, 1); |
| 633 | } |
| 634 | PRINTANDPAD(cp, convbuf + ndig, prec, zeroes); |
| 635 | } else { /* %[eE] or sufficiently long %[gG] */ |
| 636 | if (prec > 1 || flags & ALT) { |
| 637 | buf[0] = *cp++; |
| 638 | buf[1] = *decimal_point; |
| 639 | PRINT(buf, 2); |
| 640 | PRINT(cp, ndig - 1); |
| 641 | PAD(prec - ndig, zeroes); |
| 642 | } else { /* XeYYY */ |
| 643 | PRINT(cp, 1); |
| 644 | } |
| 645 | PRINT(expstr, expsize); |
| 646 | } |
| 647 | } |
| 648 | /* left-adjusting padding (always blank) */ |
| 649 | if (flags & LADJUST) PAD(width - realsz, blanks); |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 650 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 651 | /* finally, adjust ret */ |
| 652 | if (width < realsz) width = realsz; |
| 653 | if (width > INT_MAX - ret) goto overflow; |
| 654 | ret += width; |
| 655 | } |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 656 | done: |
| 657 | error: |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 658 | va_end(orgap); |
| 659 | if (__sferror(fp)) ret = -1; |
| 660 | goto finish; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 661 | |
| 662 | overflow: |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 663 | errno = ENOMEM; |
| 664 | ret = -1; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 665 | |
| 666 | finish: |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 667 | free(convbuf); |
| 668 | if (dtoaresult) __freedtoa(dtoaresult); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 669 | if (argtable != nullptr && argtable != statargtable) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 670 | munmap(argtable, argtablesiz); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 671 | argtable = nullptr; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 672 | } |
| 673 | return (ret); |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 674 | } |