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 | 1f49317 | 2017-11-08 16:13:18 -0800 | [diff] [blame] | 36 | #include "printf_common.h" |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | * Like __fputwc_unlock, but handles fake string (__SSTR) files properly. |
| 40 | * File must already be locked. |
| 41 | */ |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 42 | static wint_t __xfputwc(wchar_t wc, FILE* fp) { |
| 43 | mbstate_t mbs; |
| 44 | char buf[MB_LEN_MAX]; |
| 45 | struct __suio uio; |
| 46 | struct __siov iov; |
| 47 | size_t len; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 48 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 49 | if ((fp->_flags & __SSTR) == 0) return (__fputwc_unlock(wc, fp)); |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 50 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 51 | bzero(&mbs, sizeof(mbs)); |
| 52 | len = wcrtomb(buf, wc, &mbs); |
| 53 | if (len == (size_t)-1) { |
| 54 | fp->_flags |= __SERR; |
| 55 | errno = EILSEQ; |
| 56 | return (WEOF); |
| 57 | } |
| 58 | uio.uio_iov = &iov; |
| 59 | uio.uio_resid = len; |
| 60 | uio.uio_iovcnt = 1; |
| 61 | iov.iov_base = buf; |
| 62 | iov.iov_len = len; |
| 63 | return (__sfvwrite(fp, &uio) != EOF ? (wint_t)wc : WEOF); |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Convert a multibyte character string argument for the %s format to a wide |
| 68 | * string representation. ``prec'' specifies the maximum number of bytes |
| 69 | * to output. If ``prec'' is greater than or equal to zero, we can't assume |
| 70 | * that the multibyte character string ends in a null character. |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 71 | * |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 72 | * Returns NULL on failure. |
| 73 | * To find out what happened check errno for ENOMEM, EILSEQ and EINVAL. |
| 74 | */ |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 75 | static wchar_t* __mbsconv(char* mbsarg, int prec) { |
| 76 | mbstate_t mbs; |
| 77 | wchar_t *convbuf, *wcp; |
| 78 | const char* p; |
| 79 | size_t insize, nchars, nconv; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 80 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 81 | if (mbsarg == NULL) return (NULL); |
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 | /* |
| 84 | * Supplied argument is a multibyte string; convert it to wide |
| 85 | * characters first. |
| 86 | */ |
| 87 | if (prec >= 0) { |
| 88 | /* |
| 89 | * String is not guaranteed to be NUL-terminated. Find the |
| 90 | * number of characters to print. |
| 91 | */ |
| 92 | p = mbsarg; |
| 93 | insize = nchars = nconv = 0; |
| 94 | bzero(&mbs, sizeof(mbs)); |
| 95 | while (nchars != (size_t)prec) { |
| 96 | nconv = mbrlen(p, MB_CUR_MAX, &mbs); |
| 97 | if (nconv == (size_t)0 || nconv == (size_t)-1 || nconv == (size_t)-2) break; |
| 98 | p += nconv; |
| 99 | nchars++; |
| 100 | insize += nconv; |
| 101 | } |
| 102 | if (nconv == (size_t)-1 || nconv == (size_t)-2) return (NULL); |
| 103 | } else |
| 104 | insize = strlen(mbsarg); |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 105 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 106 | /* |
| 107 | * Allocate buffer for the result and perform the conversion, |
| 108 | * converting at most `size' bytes of the input multibyte string to |
| 109 | * wide characters for printing. |
| 110 | */ |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 111 | convbuf = static_cast<wchar_t*>(calloc(insize + 1, sizeof(*convbuf))); |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 112 | if (convbuf == NULL) return (NULL); |
| 113 | wcp = convbuf; |
| 114 | p = mbsarg; |
| 115 | bzero(&mbs, sizeof(mbs)); |
| 116 | nconv = 0; |
| 117 | while (insize != 0) { |
| 118 | nconv = mbrtowc(wcp, p, insize, &mbs); |
| 119 | if (nconv == 0 || nconv == (size_t)-1 || nconv == (size_t)-2) break; |
| 120 | wcp++; |
| 121 | p += nconv; |
| 122 | insize -= nconv; |
| 123 | } |
| 124 | if (nconv == (size_t)-1 || nconv == (size_t)-2) { |
| 125 | free(convbuf); |
| 126 | return (NULL); |
| 127 | } |
| 128 | *wcp = '\0'; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 129 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 130 | return (convbuf); |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Elliott Hughes | 1f49317 | 2017-11-08 16:13:18 -0800 | [diff] [blame] | 133 | int FUNCTION_NAME(FILE* fp, const CHAR_TYPE* fmt0, va_list ap) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 134 | wchar_t ch; /* character from fmt */ |
| 135 | int n, n2, n3; /* handy integers (short term usage) */ |
Elliott Hughes | 93a1f8b | 2017-11-07 22:52:29 -0800 | [diff] [blame] | 136 | CHAR_TYPE* cp; /* handy char pointer (short term usage) */ |
| 137 | CHAR_TYPE sign; /* sign prefix (' ', '+', '-', or \0) */ |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 138 | int flags; /* flags as above */ |
| 139 | int ret; /* return value accumulator */ |
| 140 | int width; /* width from format (%8d), or 0 */ |
| 141 | int prec; /* precision from format; <0 for N/A */ |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 142 | /* |
| 143 | * We can decompose the printed representation of floating |
| 144 | * point numbers into several parts, some of which may be empty: |
| 145 | * |
| 146 | * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ |
| 147 | * A B ---C--- D E F |
| 148 | * |
| 149 | * A: 'sign' holds this value if present; '\0' otherwise |
| 150 | * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal |
| 151 | * C: cp points to the string MMMNNN. Leading and trailing |
| 152 | * zeros are not in the string and must be added. |
| 153 | * D: expchar holds this character; '\0' if no exponent, e.g. %f |
| 154 | * F: at least two digits for decimal, at least one digit for hex |
| 155 | */ |
| 156 | char* decimal_point = NULL; |
| 157 | int signflag; /* true if float is negative */ |
| 158 | union { /* floating point arguments %[aAeEfFgG] */ |
| 159 | double dbl; |
| 160 | long double ldbl; |
| 161 | } fparg; |
| 162 | int expt; /* integer value of exponent */ |
| 163 | char expchar; /* exponent character: [eEpP\0] */ |
| 164 | char* dtoaend; /* pointer to end of converted digits */ |
| 165 | int expsize; /* character count for expstr */ |
| 166 | int lead; /* sig figs before decimal or group sep */ |
| 167 | int ndig; /* actual number of digits returned by dtoa */ |
Elliott Hughes | 93a1f8b | 2017-11-07 22:52:29 -0800 | [diff] [blame] | 168 | CHAR_TYPE expstr[MAXEXPDIG + 2]; /* buffer for exponent string: e+ZZZ */ |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 169 | char* dtoaresult = NULL; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 170 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 171 | uintmax_t _umax; /* integer arguments %[diouxX] */ |
| 172 | enum { OCT, DEC, HEX } base; /* base for %[diouxX] conversion */ |
| 173 | int dprec; /* a copy of prec if %[diouxX], 0 otherwise */ |
| 174 | int realsz; /* field size expanded by dprec */ |
| 175 | int size; /* size of converted field or string */ |
| 176 | const char* xdigs; /* digits for %[xX] conversion */ |
Elliott Hughes | 93a1f8b | 2017-11-07 22:52:29 -0800 | [diff] [blame] | 177 | CHAR_TYPE buf[BUF]; /* buffer with space for digits of uintmax_t */ |
| 178 | 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] | 179 | union arg* argtable; /* args, built due to positional arg */ |
| 180 | union arg statargtable[STATIC_ARG_TBL_SIZE]; |
| 181 | size_t argtablesiz; |
| 182 | int nextarg; /* 1-based argument index */ |
| 183 | va_list orgap; /* original argument pointer */ |
Elliott Hughes | 93a1f8b | 2017-11-07 22:52:29 -0800 | [diff] [blame] | 184 | CHAR_TYPE* convbuf; /* buffer for wide/multibyte conversion */ |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 185 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 186 | /* |
| 187 | * Choose PADSIZE to trade efficiency vs. size. If larger printf |
| 188 | * fields occur frequently, increase PADSIZE and make the initialisers |
| 189 | * below longer. |
| 190 | */ |
| 191 | #define PADSIZE 16 /* pad chunk size */ |
Elliott Hughes | 5305a4d | 2017-11-03 14:00:37 -0700 | [diff] [blame] | 192 | static CHAR_TYPE blanks[PADSIZE] = { |
| 193 | ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' |
| 194 | }; |
| 195 | static CHAR_TYPE zeroes[PADSIZE] = { |
| 196 | '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' |
| 197 | }; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 198 | |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 199 | static const char xdigs_lower[] = "0123456789abcdef"; |
| 200 | static const char xdigs_upper[] = "0123456789ABCDEF"; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 201 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 202 | /* |
| 203 | * BEWARE, these `goto error' on error, PRINT uses 'n3', |
| 204 | * PAD uses `n' and 'n3', and PRINTANDPAD uses 'n', 'n2', and 'n3'. |
| 205 | */ |
| 206 | #define PRINT(ptr, len) \ |
| 207 | do { \ |
| 208 | for (n3 = 0; n3 < (len); n3++) { \ |
| 209 | if ((__xfputwc((ptr)[n3], fp)) == WEOF) goto error; \ |
| 210 | } \ |
| 211 | } while (0) |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 212 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 213 | _SET_ORIENTATION(fp, 1); |
Elliott Hughes | 5305a4d | 2017-11-03 14:00:37 -0700 | [diff] [blame] | 214 | |
| 215 | // Writing "" to a read only file returns EOF, not 0. |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 216 | if (cantwrite(fp)) { |
| 217 | errno = EBADF; |
Elliott Hughes | 5305a4d | 2017-11-03 14:00:37 -0700 | [diff] [blame] | 218 | return EOF; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 219 | } |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 220 | |
Elliott Hughes | 5305a4d | 2017-11-03 14:00:37 -0700 | [diff] [blame] | 221 | // Optimize writes to stderr and other unbuffered files). |
| 222 | if ((fp->_flags & (__SNBF | __SWR | __SRW)) == (__SNBF | __SWR) && fp->_file >= 0) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 223 | return (__sbprintf(fp, fmt0, ap)); |
Elliott Hughes | 5305a4d | 2017-11-03 14:00:37 -0700 | [diff] [blame] | 224 | } |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 225 | |
Elliott Hughes | 5305a4d | 2017-11-03 14:00:37 -0700 | [diff] [blame] | 226 | CHAR_TYPE* fmt = const_cast<CHAR_TYPE*>(fmt0); |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 227 | argtable = NULL; |
| 228 | nextarg = 1; |
| 229 | va_copy(orgap, ap); |
| 230 | ret = 0; |
| 231 | convbuf = NULL; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 232 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 233 | /* |
| 234 | * Scan the format for conversions (`%' character). |
| 235 | */ |
| 236 | for (;;) { |
| 237 | for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) continue; |
| 238 | if (fmt != cp) { |
| 239 | ptrdiff_t m = fmt - cp; |
| 240 | if (m < 0 || m > INT_MAX - ret) goto overflow; |
| 241 | PRINT(cp, m); |
| 242 | ret += m; |
| 243 | } |
| 244 | if (ch == '\0') goto done; |
| 245 | fmt++; /* skip over '%' */ |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 246 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 247 | flags = 0; |
| 248 | dprec = 0; |
| 249 | width = 0; |
| 250 | prec = -1; |
| 251 | sign = '\0'; |
| 252 | ox[1] = '\0'; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 253 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 254 | rflag: |
| 255 | ch = *fmt++; |
| 256 | reswitch: |
| 257 | switch (ch) { |
| 258 | case ' ': |
| 259 | /* |
| 260 | * ``If the space and + flags both appear, the space |
| 261 | * flag will be ignored.'' |
| 262 | * -- ANSI X3J11 |
| 263 | */ |
| 264 | if (!sign) sign = ' '; |
| 265 | goto rflag; |
| 266 | case '#': |
| 267 | flags |= ALT; |
| 268 | goto rflag; |
| 269 | case '\'': |
| 270 | /* grouping not implemented */ |
| 271 | goto rflag; |
| 272 | case '*': |
| 273 | /* |
| 274 | * ``A negative field width argument is taken as a |
| 275 | * - flag followed by a positive field width.'' |
| 276 | * -- ANSI X3J11 |
| 277 | * They don't exclude field widths read from args. |
| 278 | */ |
| 279 | GETASTER(width); |
| 280 | if (width >= 0) goto rflag; |
| 281 | if (width == INT_MIN) goto overflow; |
| 282 | width = -width; |
| 283 | /* FALLTHROUGH */ |
| 284 | case '-': |
| 285 | flags |= LADJUST; |
| 286 | goto rflag; |
| 287 | case '+': |
| 288 | sign = '+'; |
| 289 | goto rflag; |
| 290 | case '.': |
| 291 | if ((ch = *fmt++) == '*') { |
| 292 | GETASTER(n); |
| 293 | prec = n < 0 ? -1 : n; |
| 294 | goto rflag; |
| 295 | } |
| 296 | n = 0; |
| 297 | while (is_digit(ch)) { |
| 298 | APPEND_DIGIT(n, ch); |
| 299 | ch = *fmt++; |
| 300 | } |
| 301 | if (ch == '$') { |
| 302 | nextarg = n; |
| 303 | if (argtable == NULL) { |
| 304 | argtable = statargtable; |
Elliott Hughes | 5305a4d | 2017-11-03 14:00:37 -0700 | [diff] [blame] | 305 | if (__find_arguments(fmt0, orgap, &argtable, &argtablesiz) == -1) { |
| 306 | ret = -1; |
| 307 | goto error; |
| 308 | } |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 309 | } |
| 310 | goto rflag; |
| 311 | } |
| 312 | prec = n; |
| 313 | goto reswitch; |
| 314 | case '0': |
| 315 | /* |
| 316 | * ``Note that 0 is taken as a flag, not as the |
| 317 | * beginning of a field width.'' |
| 318 | * -- ANSI X3J11 |
| 319 | */ |
| 320 | flags |= ZEROPAD; |
| 321 | goto rflag; |
| 322 | case '1': |
| 323 | case '2': |
| 324 | case '3': |
| 325 | case '4': |
| 326 | case '5': |
| 327 | case '6': |
| 328 | case '7': |
| 329 | case '8': |
| 330 | case '9': |
| 331 | n = 0; |
| 332 | do { |
| 333 | APPEND_DIGIT(n, ch); |
| 334 | ch = *fmt++; |
| 335 | } while (is_digit(ch)); |
| 336 | if (ch == '$') { |
| 337 | nextarg = n; |
| 338 | if (argtable == NULL) { |
| 339 | argtable = statargtable; |
Elliott Hughes | 5305a4d | 2017-11-03 14:00:37 -0700 | [diff] [blame] | 340 | if (__find_arguments(fmt0, orgap, &argtable, &argtablesiz) == -1) { |
| 341 | ret = -1; |
| 342 | goto error; |
| 343 | } |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 344 | } |
| 345 | goto rflag; |
| 346 | } |
| 347 | width = n; |
| 348 | goto reswitch; |
| 349 | case 'L': |
| 350 | flags |= LONGDBL; |
| 351 | goto rflag; |
| 352 | case 'h': |
| 353 | if (*fmt == 'h') { |
| 354 | fmt++; |
| 355 | flags |= CHARINT; |
| 356 | } else { |
| 357 | flags |= SHORTINT; |
| 358 | } |
| 359 | goto rflag; |
| 360 | case 'j': |
| 361 | flags |= MAXINT; |
| 362 | goto rflag; |
| 363 | case 'l': |
| 364 | if (*fmt == 'l') { |
| 365 | fmt++; |
| 366 | flags |= LLONGINT; |
| 367 | } else { |
| 368 | flags |= LONGINT; |
| 369 | } |
| 370 | goto rflag; |
| 371 | case 'q': |
| 372 | flags |= LLONGINT; |
| 373 | goto rflag; |
| 374 | case 't': |
| 375 | flags |= PTRINT; |
| 376 | goto rflag; |
| 377 | case 'z': |
| 378 | flags |= SIZEINT; |
| 379 | goto rflag; |
| 380 | case 'C': |
| 381 | flags |= LONGINT; |
| 382 | /*FALLTHROUGH*/ |
| 383 | case 'c': |
| 384 | if (flags & LONGINT) |
| 385 | *(cp = buf) = (wchar_t)GETARG(wint_t); |
| 386 | else |
| 387 | *(cp = buf) = (wchar_t)btowc(GETARG(int)); |
| 388 | size = 1; |
| 389 | sign = '\0'; |
| 390 | break; |
| 391 | case 'D': |
| 392 | flags |= LONGINT; |
| 393 | /*FALLTHROUGH*/ |
| 394 | case 'd': |
| 395 | case 'i': |
| 396 | _umax = SARG(); |
| 397 | if ((intmax_t)_umax < 0) { |
| 398 | _umax = -_umax; |
| 399 | sign = '-'; |
| 400 | } |
| 401 | base = DEC; |
| 402 | goto number; |
| 403 | case 'a': |
| 404 | case 'A': |
| 405 | if (ch == 'a') { |
| 406 | ox[1] = 'x'; |
| 407 | xdigs = xdigs_lower; |
| 408 | expchar = 'p'; |
| 409 | } else { |
| 410 | ox[1] = 'X'; |
| 411 | xdigs = xdigs_upper; |
| 412 | expchar = 'P'; |
| 413 | } |
| 414 | if (prec >= 0) prec++; |
| 415 | if (dtoaresult) __freedtoa(dtoaresult); |
| 416 | if (flags & LONGDBL) { |
| 417 | fparg.ldbl = GETARG(long double); |
| 418 | dtoaresult = __hldtoa(fparg.ldbl, xdigs, prec, &expt, &signflag, &dtoaend); |
| 419 | if (dtoaresult == NULL) { |
| 420 | errno = ENOMEM; |
| 421 | goto error; |
| 422 | } |
| 423 | } else { |
| 424 | fparg.dbl = GETARG(double); |
| 425 | dtoaresult = __hdtoa(fparg.dbl, xdigs, prec, &expt, &signflag, &dtoaend); |
| 426 | if (dtoaresult == NULL) { |
| 427 | errno = ENOMEM; |
| 428 | goto error; |
| 429 | } |
| 430 | } |
| 431 | if (prec < 0) prec = dtoaend - dtoaresult; |
| 432 | if (expt == INT_MAX) ox[1] = '\0'; |
| 433 | free(convbuf); |
| 434 | cp = convbuf = __mbsconv(dtoaresult, -1); |
| 435 | if (cp == NULL) goto error; |
| 436 | ndig = dtoaend - dtoaresult; |
| 437 | goto fp_common; |
| 438 | case 'e': |
| 439 | case 'E': |
| 440 | expchar = ch; |
| 441 | if (prec < 0) /* account for digit before decpt */ |
| 442 | prec = DEFPREC + 1; |
| 443 | else |
| 444 | prec++; |
| 445 | goto fp_begin; |
| 446 | case 'f': |
| 447 | case 'F': |
| 448 | expchar = '\0'; |
| 449 | goto fp_begin; |
| 450 | case 'g': |
| 451 | case 'G': |
| 452 | expchar = ch - ('g' - 'e'); |
| 453 | if (prec == 0) prec = 1; |
| 454 | fp_begin: |
| 455 | if (prec < 0) prec = DEFPREC; |
| 456 | if (dtoaresult) __freedtoa(dtoaresult); |
| 457 | if (flags & LONGDBL) { |
| 458 | fparg.ldbl = GETARG(long double); |
| 459 | dtoaresult = __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec, &expt, &signflag, &dtoaend); |
| 460 | if (dtoaresult == NULL) { |
| 461 | errno = ENOMEM; |
| 462 | goto error; |
| 463 | } |
| 464 | } else { |
| 465 | fparg.dbl = GETARG(double); |
| 466 | dtoaresult = __dtoa(fparg.dbl, expchar ? 2 : 3, prec, &expt, &signflag, &dtoaend); |
| 467 | if (dtoaresult == NULL) { |
| 468 | errno = ENOMEM; |
| 469 | goto error; |
| 470 | } |
| 471 | if (expt == 9999) expt = INT_MAX; |
| 472 | } |
| 473 | free(convbuf); |
| 474 | cp = convbuf = __mbsconv(dtoaresult, -1); |
| 475 | if (cp == NULL) goto error; |
| 476 | ndig = dtoaend - dtoaresult; |
| 477 | fp_common: |
| 478 | if (signflag) sign = '-'; |
| 479 | if (expt == INT_MAX) { /* inf or nan */ |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 480 | if (*cp == 'N') { |
| 481 | cp = const_cast<wchar_t*>((ch >= 'a') ? L"nan" : L"NAN"); |
| 482 | } else { |
| 483 | cp = const_cast<wchar_t*>((ch >= 'a') ? L"inf" : L"INF"); |
| 484 | } |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 485 | size = 3; |
| 486 | flags &= ~ZEROPAD; |
| 487 | break; |
| 488 | } |
| 489 | flags |= FPT; |
| 490 | if (ch == 'g' || ch == 'G') { |
| 491 | if (expt > -4 && expt <= prec) { |
| 492 | /* Make %[gG] smell like %[fF] */ |
| 493 | expchar = '\0'; |
| 494 | if (flags & ALT) |
| 495 | prec -= expt; |
| 496 | else |
| 497 | prec = ndig - expt; |
| 498 | if (prec < 0) prec = 0; |
| 499 | } else { |
| 500 | /* |
| 501 | * Make %[gG] smell like %[eE], but |
| 502 | * trim trailing zeroes if no # flag. |
| 503 | */ |
| 504 | if (!(flags & ALT)) prec = ndig; |
| 505 | } |
| 506 | } |
| 507 | if (expchar) { |
| 508 | expsize = exponent(expstr, expt - 1, expchar); |
| 509 | size = expsize + prec; |
| 510 | if (prec > 1 || flags & ALT) ++size; |
| 511 | } else { |
| 512 | /* space for digits before decimal point */ |
| 513 | if (expt > 0) |
| 514 | size = expt; |
| 515 | else /* "0" */ |
| 516 | size = 1; |
| 517 | /* space for decimal pt and following digits */ |
| 518 | if (prec || flags & ALT) size += prec + 1; |
| 519 | lead = expt; |
| 520 | } |
| 521 | break; |
Elliott Hughes | e2341d0 | 2014-05-02 18:16:32 -0700 | [diff] [blame] | 522 | #ifndef NO_PRINTF_PERCENT_N |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 523 | case 'n': |
| 524 | if (flags & LLONGINT) |
| 525 | *GETARG(long long*) = ret; |
| 526 | else if (flags & LONGINT) |
| 527 | *GETARG(long*) = ret; |
| 528 | else if (flags & SHORTINT) |
| 529 | *GETARG(short*) = ret; |
| 530 | else if (flags & CHARINT) |
| 531 | *GETARG(signed char*) = ret; |
| 532 | else if (flags & PTRINT) |
| 533 | *GETARG(ptrdiff_t*) = ret; |
| 534 | else if (flags & SIZEINT) |
| 535 | *GETARG(ssize_t*) = ret; |
| 536 | else if (flags & MAXINT) |
| 537 | *GETARG(intmax_t*) = ret; |
| 538 | else |
| 539 | *GETARG(int*) = ret; |
| 540 | continue; /* no output */ |
| 541 | #endif /* NO_PRINTF_PERCENT_N */ |
| 542 | case 'O': |
| 543 | flags |= LONGINT; |
| 544 | /*FALLTHROUGH*/ |
| 545 | case 'o': |
| 546 | _umax = UARG(); |
| 547 | base = OCT; |
| 548 | goto nosign; |
| 549 | case 'p': |
| 550 | /* |
| 551 | * ``The argument shall be a pointer to void. The |
| 552 | * value of the pointer is converted to a sequence |
| 553 | * of printable characters, in an implementation- |
| 554 | * defined manner.'' |
| 555 | * -- ANSI X3J11 |
| 556 | */ |
| 557 | _umax = (u_long)GETARG(void*); |
| 558 | base = HEX; |
| 559 | xdigs = xdigs_lower; |
| 560 | ox[1] = 'x'; |
| 561 | goto nosign; |
| 562 | case 'S': |
| 563 | flags |= LONGINT; |
| 564 | /*FALLTHROUGH*/ |
| 565 | case 's': |
| 566 | if (flags & LONGINT) { |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 567 | if ((cp = GETARG(wchar_t*)) == NULL) cp = const_cast<wchar_t*>(L"(null)"); |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 568 | } else { |
| 569 | char* mbsarg; |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 570 | if ((mbsarg = GETARG(char*)) == NULL) mbsarg = const_cast<char*>("(null)"); |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 571 | free(convbuf); |
| 572 | convbuf = __mbsconv(mbsarg, prec); |
| 573 | if (convbuf == NULL) { |
| 574 | fp->_flags |= __SERR; |
| 575 | goto error; |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 576 | } else { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 577 | cp = convbuf; |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 578 | } |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 579 | } |
| 580 | if (prec >= 0) { |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 581 | size = wcsnlen(cp, prec); |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 582 | } else { |
| 583 | size_t len; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 584 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 585 | if ((len = wcslen(cp)) > INT_MAX) goto overflow; |
| 586 | size = (int)len; |
| 587 | } |
| 588 | sign = '\0'; |
| 589 | break; |
| 590 | case 'U': |
| 591 | flags |= LONGINT; |
| 592 | /*FALLTHROUGH*/ |
| 593 | case 'u': |
| 594 | _umax = UARG(); |
| 595 | base = DEC; |
| 596 | goto nosign; |
| 597 | case 'X': |
| 598 | xdigs = xdigs_upper; |
| 599 | goto hex; |
| 600 | case 'x': |
| 601 | xdigs = xdigs_lower; |
| 602 | hex: |
| 603 | _umax = UARG(); |
| 604 | base = HEX; |
| 605 | /* leading 0x/X only if non-zero */ |
| 606 | if (flags & ALT && _umax != 0) ox[1] = ch; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 607 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 608 | /* unsigned conversions */ |
| 609 | nosign: |
| 610 | sign = '\0'; |
| 611 | /* |
| 612 | * ``... diouXx conversions ... if a precision is |
| 613 | * specified, the 0 flag will be ignored.'' |
| 614 | * -- ANSI X3J11 |
| 615 | */ |
| 616 | number: |
| 617 | if ((dprec = prec) >= 0) flags &= ~ZEROPAD; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 618 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 619 | /* |
| 620 | * ``The result of converting a zero value with an |
| 621 | * explicit precision of zero is no characters.'' |
| 622 | * -- ANSI X3J11 |
| 623 | */ |
| 624 | cp = buf + BUF; |
| 625 | if (_umax != 0 || prec != 0) { |
| 626 | /* |
| 627 | * Unsigned mod is hard, and unsigned mod |
| 628 | * by a constant is easier than that by |
| 629 | * a variable; hence this switch. |
| 630 | */ |
| 631 | switch (base) { |
| 632 | case OCT: |
| 633 | do { |
| 634 | *--cp = to_char(_umax & 7); |
| 635 | _umax >>= 3; |
| 636 | } while (_umax); |
| 637 | /* handle octal leading 0 */ |
| 638 | if (flags & ALT && *cp != '0') *--cp = '0'; |
| 639 | break; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 640 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 641 | case DEC: |
| 642 | /* many numbers are 1 digit */ |
| 643 | while (_umax >= 10) { |
| 644 | *--cp = to_char(_umax % 10); |
| 645 | _umax /= 10; |
| 646 | } |
| 647 | *--cp = to_char(_umax); |
| 648 | break; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 649 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 650 | case HEX: |
| 651 | do { |
| 652 | *--cp = xdigs[_umax & 15]; |
| 653 | _umax >>= 4; |
| 654 | } while (_umax); |
| 655 | break; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 656 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 657 | default: |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 658 | abort(); |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | size = buf + BUF - cp; |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 662 | if (size > BUF) abort(); /* should never happen */ |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 663 | break; |
| 664 | default: /* "%?" prints ?, unless ? is NUL */ |
| 665 | if (ch == '\0') goto done; |
| 666 | /* pretend it was %c with argument ch */ |
| 667 | cp = buf; |
| 668 | *cp = ch; |
| 669 | size = 1; |
| 670 | sign = '\0'; |
| 671 | break; |
| 672 | } |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 673 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 674 | /* |
| 675 | * All reasonable formats wind up here. At this point, `cp' |
| 676 | * points to a string which (if not flags&LADJUST) should be |
| 677 | * padded out to `width' places. If flags&ZEROPAD, it should |
| 678 | * first be prefixed by any sign or other prefix; otherwise, |
| 679 | * it should be blank padded before the prefix is emitted. |
| 680 | * After any left-hand padding and prefixing, emit zeroes |
| 681 | * required by a decimal %[diouxX] precision, then print the |
| 682 | * string proper, then emit zeroes required by any leftover |
| 683 | * floating precision; finally, if LADJUST, pad with blanks. |
| 684 | * |
| 685 | * Compute actual size, so we know how much to pad. |
| 686 | * size excludes decimal prec; realsz includes it. |
| 687 | */ |
| 688 | realsz = dprec > size ? dprec : size; |
| 689 | if (sign) realsz++; |
| 690 | if (ox[1]) realsz += 2; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 691 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 692 | /* right-adjusting blank padding */ |
| 693 | if ((flags & (LADJUST | ZEROPAD)) == 0) PAD(width - realsz, blanks); |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 694 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 695 | /* prefix */ |
| 696 | if (sign) PRINT(&sign, 1); |
| 697 | if (ox[1]) { /* ox[1] is either x, X, or \0 */ |
| 698 | ox[0] = '0'; |
| 699 | PRINT(ox, 2); |
| 700 | } |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 701 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 702 | /* right-adjusting zero padding */ |
| 703 | if ((flags & (LADJUST | ZEROPAD)) == ZEROPAD) PAD(width - realsz, zeroes); |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 704 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 705 | /* leading zeroes from decimal precision */ |
| 706 | PAD(dprec - size, zeroes); |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 707 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 708 | /* the string or number proper */ |
| 709 | if ((flags & FPT) == 0) { |
| 710 | PRINT(cp, size); |
| 711 | } else { /* glue together f_p fragments */ |
| 712 | if (decimal_point == NULL) decimal_point = nl_langinfo(RADIXCHAR); |
| 713 | if (!expchar) { /* %[fF] or sufficiently short %[gG] */ |
| 714 | if (expt <= 0) { |
| 715 | PRINT(zeroes, 1); |
| 716 | if (prec || flags & ALT) PRINT(decimal_point, 1); |
| 717 | PAD(-expt, zeroes); |
| 718 | /* already handled initial 0's */ |
| 719 | prec += expt; |
| 720 | } else { |
| 721 | PRINTANDPAD(cp, convbuf + ndig, lead, zeroes); |
| 722 | cp += lead; |
| 723 | if (prec || flags & ALT) PRINT(decimal_point, 1); |
| 724 | } |
| 725 | PRINTANDPAD(cp, convbuf + ndig, prec, zeroes); |
| 726 | } else { /* %[eE] or sufficiently long %[gG] */ |
| 727 | if (prec > 1 || flags & ALT) { |
| 728 | buf[0] = *cp++; |
| 729 | buf[1] = *decimal_point; |
| 730 | PRINT(buf, 2); |
| 731 | PRINT(cp, ndig - 1); |
| 732 | PAD(prec - ndig, zeroes); |
| 733 | } else { /* XeYYY */ |
| 734 | PRINT(cp, 1); |
| 735 | } |
| 736 | PRINT(expstr, expsize); |
| 737 | } |
| 738 | } |
| 739 | /* left-adjusting padding (always blank) */ |
| 740 | if (flags & LADJUST) PAD(width - realsz, blanks); |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 741 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 742 | /* finally, adjust ret */ |
| 743 | if (width < realsz) width = realsz; |
| 744 | if (width > INT_MAX - ret) goto overflow; |
| 745 | ret += width; |
| 746 | } |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 747 | done: |
| 748 | error: |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 749 | va_end(orgap); |
| 750 | if (__sferror(fp)) ret = -1; |
| 751 | goto finish; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 752 | |
| 753 | overflow: |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 754 | errno = ENOMEM; |
| 755 | ret = -1; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 756 | |
| 757 | finish: |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 758 | free(convbuf); |
| 759 | if (dtoaresult) __freedtoa(dtoaresult); |
| 760 | if (argtable != NULL && argtable != statargtable) { |
| 761 | munmap(argtable, argtablesiz); |
| 762 | argtable = NULL; |
| 763 | } |
| 764 | return (ret); |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 765 | } |