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 | |
| 34 | /* |
| 35 | * Actual wprintf innards. |
| 36 | * |
| 37 | * This code is large and complicated... |
| 38 | */ |
| 39 | |
| 40 | #include <sys/types.h> |
| 41 | #include <sys/mman.h> |
| 42 | |
| 43 | #include <errno.h> |
| 44 | #include <langinfo.h> |
| 45 | #include <limits.h> |
| 46 | #include <stdarg.h> |
| 47 | #include <stddef.h> |
| 48 | #include <stdio.h> |
| 49 | #include <stdint.h> |
| 50 | #include <stdlib.h> |
| 51 | #include <string.h> |
| 52 | #include <unistd.h> |
| 53 | |
| 54 | #include "local.h" |
| 55 | #include "fvwrite.h" |
| 56 | |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 57 | union arg { |
| 58 | int intarg; |
| 59 | unsigned int uintarg; |
| 60 | long longarg; |
| 61 | unsigned long ulongarg; |
| 62 | long long longlongarg; |
| 63 | unsigned long long ulonglongarg; |
| 64 | ptrdiff_t ptrdiffarg; |
| 65 | size_t sizearg; |
| 66 | ssize_t ssizearg; |
| 67 | intmax_t intmaxarg; |
| 68 | uintmax_t uintmaxarg; |
| 69 | void *pvoidarg; |
| 70 | char *pchararg; |
| 71 | signed char *pschararg; |
| 72 | short *pshortarg; |
| 73 | int *pintarg; |
| 74 | long *plongarg; |
| 75 | long long *plonglongarg; |
| 76 | ptrdiff_t *pptrdiffarg; |
| 77 | ssize_t *pssizearg; |
| 78 | intmax_t *pintmaxarg; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 79 | double doublearg; |
| 80 | long double longdoublearg; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 81 | wint_t wintarg; |
| 82 | wchar_t *pwchararg; |
| 83 | }; |
| 84 | |
| 85 | static int __find_arguments(const wchar_t *fmt0, va_list ap, union arg **argtable, |
| 86 | size_t *argtablesiz); |
| 87 | static int __grow_type_table(unsigned char **typetable, int *tablesize); |
| 88 | |
| 89 | /* |
| 90 | * Helper function for `fprintf to unbuffered unix file': creates a |
| 91 | * temporary buffer. We only work on write-only files; this avoids |
| 92 | * worries about ungetc buffers and so forth. |
| 93 | */ |
| 94 | static int |
| 95 | __sbprintf(FILE *fp, const wchar_t *fmt, va_list ap) |
| 96 | { |
| 97 | int ret; |
| 98 | FILE fake; |
| 99 | struct __sfileext fakeext; |
| 100 | unsigned char buf[BUFSIZ]; |
| 101 | |
| 102 | _FILEEXT_SETUP(&fake, &fakeext); |
| 103 | /* copy the important variables */ |
| 104 | fake._flags = fp->_flags & ~__SNBF; |
| 105 | fake._file = fp->_file; |
| 106 | fake._cookie = fp->_cookie; |
| 107 | fake._write = fp->_write; |
| 108 | |
| 109 | /* set up the buffer */ |
| 110 | fake._bf._base = fake._p = buf; |
| 111 | fake._bf._size = fake._w = sizeof(buf); |
| 112 | fake._lbfsize = 0; /* not actually used, but Just In Case */ |
| 113 | |
| 114 | /* do the work, then copy any error status */ |
| 115 | ret = __vfwprintf(&fake, fmt, ap); |
| 116 | if (ret >= 0 && __sflush(&fake)) |
| 117 | ret = EOF; |
| 118 | if (fake._flags & __SERR) |
| 119 | fp->_flags |= __SERR; |
| 120 | return (ret); |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * Like __fputwc_unlock, but handles fake string (__SSTR) files properly. |
| 125 | * File must already be locked. |
| 126 | */ |
| 127 | static wint_t |
| 128 | __xfputwc(wchar_t wc, FILE *fp) |
| 129 | { |
| 130 | mbstate_t mbs; |
| 131 | char buf[MB_LEN_MAX]; |
| 132 | struct __suio uio; |
| 133 | struct __siov iov; |
| 134 | size_t len; |
| 135 | |
| 136 | if ((fp->_flags & __SSTR) == 0) |
| 137 | return (__fputwc_unlock(wc, fp)); |
| 138 | |
| 139 | bzero(&mbs, sizeof(mbs)); |
| 140 | len = wcrtomb(buf, wc, &mbs); |
| 141 | if (len == (size_t)-1) { |
| 142 | fp->_flags |= __SERR; |
| 143 | errno = EILSEQ; |
| 144 | return (WEOF); |
| 145 | } |
| 146 | uio.uio_iov = &iov; |
| 147 | uio.uio_resid = len; |
| 148 | uio.uio_iovcnt = 1; |
| 149 | iov.iov_base = buf; |
| 150 | iov.iov_len = len; |
| 151 | return (__sfvwrite(fp, &uio) != EOF ? (wint_t)wc : WEOF); |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Convert a multibyte character string argument for the %s format to a wide |
| 156 | * string representation. ``prec'' specifies the maximum number of bytes |
| 157 | * to output. If ``prec'' is greater than or equal to zero, we can't assume |
| 158 | * that the multibyte character string ends in a null character. |
| 159 | * |
| 160 | * Returns NULL on failure. |
| 161 | * To find out what happened check errno for ENOMEM, EILSEQ and EINVAL. |
| 162 | */ |
| 163 | static wchar_t * |
| 164 | __mbsconv(char *mbsarg, int prec) |
| 165 | { |
| 166 | mbstate_t mbs; |
| 167 | wchar_t *convbuf, *wcp; |
| 168 | const char *p; |
| 169 | size_t insize, nchars, nconv; |
| 170 | |
| 171 | if (mbsarg == NULL) |
| 172 | return (NULL); |
| 173 | |
| 174 | /* |
| 175 | * Supplied argument is a multibyte string; convert it to wide |
| 176 | * characters first. |
| 177 | */ |
| 178 | if (prec >= 0) { |
| 179 | /* |
| 180 | * String is not guaranteed to be NUL-terminated. Find the |
| 181 | * number of characters to print. |
| 182 | */ |
| 183 | p = mbsarg; |
| 184 | insize = nchars = nconv = 0; |
| 185 | bzero(&mbs, sizeof(mbs)); |
| 186 | while (nchars != (size_t)prec) { |
| 187 | nconv = mbrlen(p, MB_CUR_MAX, &mbs); |
| 188 | if (nconv == (size_t)0 || nconv == (size_t)-1 || |
| 189 | nconv == (size_t)-2) |
| 190 | break; |
| 191 | p += nconv; |
| 192 | nchars++; |
| 193 | insize += nconv; |
| 194 | } |
| 195 | if (nconv == (size_t)-1 || nconv == (size_t)-2) |
| 196 | return (NULL); |
| 197 | } else |
| 198 | insize = strlen(mbsarg); |
| 199 | |
| 200 | /* |
| 201 | * Allocate buffer for the result and perform the conversion, |
| 202 | * converting at most `size' bytes of the input multibyte string to |
| 203 | * wide characters for printing. |
| 204 | */ |
| 205 | convbuf = calloc(insize + 1, sizeof(*convbuf)); |
| 206 | if (convbuf == NULL) |
| 207 | return (NULL); |
| 208 | wcp = convbuf; |
| 209 | p = mbsarg; |
| 210 | bzero(&mbs, sizeof(mbs)); |
| 211 | nconv = 0; |
| 212 | while (insize != 0) { |
| 213 | nconv = mbrtowc(wcp, p, insize, &mbs); |
| 214 | if (nconv == 0 || nconv == (size_t)-1 || nconv == (size_t)-2) |
| 215 | break; |
| 216 | wcp++; |
| 217 | p += nconv; |
| 218 | insize -= nconv; |
| 219 | } |
| 220 | if (nconv == (size_t)-1 || nconv == (size_t)-2) { |
| 221 | free(convbuf); |
| 222 | return (NULL); |
| 223 | } |
| 224 | *wcp = '\0'; |
| 225 | |
| 226 | return (convbuf); |
| 227 | } |
| 228 | |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 229 | #include <float.h> |
| 230 | #include <locale.h> |
| 231 | #include <math.h> |
| 232 | #include "floatio.h" |
Elliott Hughes | f1ada79 | 2014-05-02 17:56:56 -0700 | [diff] [blame] | 233 | #include "gdtoa.h" |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 234 | |
| 235 | #define DEFPREC 6 |
| 236 | |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 237 | static int exponent(wchar_t *, int, int); |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 238 | |
| 239 | /* |
| 240 | * The size of the buffer we use as scratch space for integer |
| 241 | * conversions, among other things. Technically, we would need the |
| 242 | * most space for base 10 conversions with thousands' grouping |
| 243 | * characters between each pair of digits. 100 bytes is a |
| 244 | * conservative overestimate even for a 128-bit uintmax_t. |
| 245 | */ |
| 246 | #define BUF 100 |
| 247 | |
| 248 | #define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */ |
| 249 | |
| 250 | |
| 251 | /* |
| 252 | * Macros for converting digits to letters and vice versa |
| 253 | */ |
| 254 | #define to_digit(c) ((c) - '0') |
| 255 | #define is_digit(c) ((unsigned)to_digit(c) <= 9) |
| 256 | #define to_char(n) ((wchar_t)((n) + '0')) |
| 257 | |
| 258 | /* |
| 259 | * Flags used during conversion. |
| 260 | */ |
| 261 | #define ALT 0x0001 /* alternate form */ |
| 262 | #define LADJUST 0x0004 /* left adjustment */ |
| 263 | #define LONGDBL 0x0008 /* long double */ |
| 264 | #define LONGINT 0x0010 /* long integer */ |
| 265 | #define LLONGINT 0x0020 /* long long integer */ |
| 266 | #define SHORTINT 0x0040 /* short integer */ |
| 267 | #define ZEROPAD 0x0080 /* zero (as opposed to blank) pad */ |
| 268 | #define FPT 0x0100 /* Floating point number */ |
| 269 | #define PTRINT 0x0200 /* (unsigned) ptrdiff_t */ |
| 270 | #define SIZEINT 0x0400 /* (signed) size_t */ |
| 271 | #define CHARINT 0x0800 /* 8 bit integer */ |
| 272 | #define MAXINT 0x1000 /* largest integer size (intmax_t) */ |
| 273 | |
| 274 | int |
| 275 | __vfwprintf(FILE * __restrict fp, const wchar_t * __restrict fmt0, __va_list ap) |
| 276 | { |
| 277 | wchar_t *fmt; /* format string */ |
| 278 | wchar_t ch; /* character from fmt */ |
| 279 | int n, n2, n3; /* handy integers (short term usage) */ |
| 280 | wchar_t *cp; /* handy char pointer (short term usage) */ |
| 281 | int flags; /* flags as above */ |
| 282 | int ret; /* return value accumulator */ |
| 283 | int width; /* width from format (%8d), or 0 */ |
| 284 | int prec; /* precision from format; <0 for N/A */ |
| 285 | wchar_t sign; /* sign prefix (' ', '+', '-', or \0) */ |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 286 | /* |
| 287 | * We can decompose the printed representation of floating |
| 288 | * point numbers into several parts, some of which may be empty: |
| 289 | * |
| 290 | * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ |
| 291 | * A B ---C--- D E F |
| 292 | * |
| 293 | * A: 'sign' holds this value if present; '\0' otherwise |
| 294 | * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal |
| 295 | * C: cp points to the string MMMNNN. Leading and trailing |
| 296 | * zeros are not in the string and must be added. |
| 297 | * D: expchar holds this character; '\0' if no exponent, e.g. %f |
| 298 | * F: at least two digits for decimal, at least one digit for hex |
| 299 | */ |
| 300 | char *decimal_point = NULL; |
| 301 | int signflag; /* true if float is negative */ |
| 302 | union { /* floating point arguments %[aAeEfFgG] */ |
| 303 | double dbl; |
| 304 | long double ldbl; |
| 305 | } fparg; |
| 306 | int expt; /* integer value of exponent */ |
| 307 | char expchar; /* exponent character: [eEpP\0] */ |
| 308 | char *dtoaend; /* pointer to end of converted digits */ |
| 309 | int expsize; /* character count for expstr */ |
| 310 | int lead; /* sig figs before decimal or group sep */ |
| 311 | int ndig; /* actual number of digits returned by dtoa */ |
| 312 | wchar_t expstr[MAXEXPDIG+2]; /* buffer for exponent string: e+ZZZ */ |
| 313 | char *dtoaresult = NULL; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 314 | |
| 315 | uintmax_t _umax; /* integer arguments %[diouxX] */ |
| 316 | enum { OCT, DEC, HEX } base; /* base for %[diouxX] conversion */ |
| 317 | int dprec; /* a copy of prec if %[diouxX], 0 otherwise */ |
| 318 | int realsz; /* field size expanded by dprec */ |
| 319 | int size; /* size of converted field or string */ |
| 320 | const char *xdigs; /* digits for %[xX] conversion */ |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 321 | wchar_t buf[BUF]; /* buffer with space for digits of uintmax_t */ |
| 322 | wchar_t ox[2]; /* space for 0x; ox[1] is either x, X, or \0 */ |
| 323 | union arg *argtable; /* args, built due to positional arg */ |
| 324 | union arg statargtable[STATIC_ARG_TBL_SIZE]; |
| 325 | size_t argtablesiz; |
| 326 | int nextarg; /* 1-based argument index */ |
| 327 | va_list orgap; /* original argument pointer */ |
| 328 | wchar_t *convbuf; /* buffer for multibyte to wide conversion */ |
| 329 | |
| 330 | /* |
| 331 | * Choose PADSIZE to trade efficiency vs. size. If larger printf |
| 332 | * fields occur frequently, increase PADSIZE and make the initialisers |
| 333 | * below longer. |
| 334 | */ |
| 335 | #define PADSIZE 16 /* pad chunk size */ |
| 336 | static wchar_t blanks[PADSIZE] = |
| 337 | {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; |
| 338 | static wchar_t zeroes[PADSIZE] = |
| 339 | {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'}; |
| 340 | |
| 341 | static const char xdigs_lower[16] = "0123456789abcdef"; |
| 342 | static const char xdigs_upper[16] = "0123456789ABCDEF"; |
| 343 | |
| 344 | /* |
| 345 | * BEWARE, these `goto error' on error, PRINT uses 'n3', |
| 346 | * PAD uses `n' and 'n3', and PRINTANDPAD uses 'n', 'n2', and 'n3'. |
| 347 | */ |
| 348 | #define PRINT(ptr, len) do { \ |
| 349 | for (n3 = 0; n3 < (len); n3++) { \ |
| 350 | if ((__xfputwc((ptr)[n3], fp)) == WEOF) \ |
| 351 | goto error; \ |
| 352 | } \ |
| 353 | } while (0) |
| 354 | #define PAD(howmany, with) do { \ |
| 355 | if ((n = (howmany)) > 0) { \ |
| 356 | while (n > PADSIZE) { \ |
| 357 | PRINT(with, PADSIZE); \ |
| 358 | n -= PADSIZE; \ |
| 359 | } \ |
| 360 | PRINT(with, n); \ |
| 361 | } \ |
| 362 | } while (0) |
| 363 | #define PRINTANDPAD(p, ep, len, with) do { \ |
| 364 | n2 = (ep) - (p); \ |
| 365 | if (n2 > (len)) \ |
| 366 | n2 = (len); \ |
| 367 | if (n2 > 0) \ |
| 368 | PRINT((p), n2); \ |
| 369 | PAD((len) - (n2 > 0 ? n2 : 0), (with)); \ |
| 370 | } while(0) |
| 371 | |
| 372 | /* |
| 373 | * To extend shorts properly, we need both signed and unsigned |
| 374 | * argument extraction methods. |
| 375 | */ |
| 376 | #define SARG() \ |
| 377 | ((intmax_t)(flags&MAXINT ? GETARG(intmax_t) : \ |
| 378 | flags&LLONGINT ? GETARG(long long) : \ |
| 379 | flags&LONGINT ? GETARG(long) : \ |
| 380 | flags&PTRINT ? GETARG(ptrdiff_t) : \ |
| 381 | flags&SIZEINT ? GETARG(ssize_t) : \ |
| 382 | flags&SHORTINT ? (short)GETARG(int) : \ |
Elliott Hughes | f1ada79 | 2014-05-02 17:56:56 -0700 | [diff] [blame] | 383 | flags&CHARINT ? (signed char)GETARG(int) : \ |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 384 | GETARG(int))) |
| 385 | #define UARG() \ |
| 386 | ((uintmax_t)(flags&MAXINT ? GETARG(uintmax_t) : \ |
| 387 | flags&LLONGINT ? GETARG(unsigned long long) : \ |
| 388 | flags&LONGINT ? GETARG(unsigned long) : \ |
| 389 | flags&PTRINT ? (uintptr_t)GETARG(ptrdiff_t) : /* XXX */ \ |
| 390 | flags&SIZEINT ? GETARG(size_t) : \ |
| 391 | flags&SHORTINT ? (unsigned short)GETARG(int) : \ |
| 392 | flags&CHARINT ? (unsigned char)GETARG(int) : \ |
| 393 | GETARG(unsigned int))) |
| 394 | |
| 395 | /* |
| 396 | * Append a digit to a value and check for overflow. |
| 397 | */ |
| 398 | #define APPEND_DIGIT(val, dig) do { \ |
| 399 | if ((val) > INT_MAX / 10) \ |
| 400 | goto overflow; \ |
| 401 | (val) *= 10; \ |
| 402 | if ((val) > INT_MAX - to_digit((dig))) \ |
| 403 | goto overflow; \ |
| 404 | (val) += to_digit((dig)); \ |
| 405 | } while (0) |
| 406 | |
| 407 | /* |
| 408 | * Get * arguments, including the form *nn$. Preserve the nextarg |
| 409 | * that the argument can be gotten once the type is determined. |
| 410 | */ |
| 411 | #define GETASTER(val) \ |
| 412 | n2 = 0; \ |
| 413 | cp = fmt; \ |
| 414 | while (is_digit(*cp)) { \ |
| 415 | APPEND_DIGIT(n2, *cp); \ |
| 416 | cp++; \ |
| 417 | } \ |
| 418 | if (*cp == '$') { \ |
| 419 | int hold = nextarg; \ |
| 420 | if (argtable == NULL) { \ |
| 421 | argtable = statargtable; \ |
| 422 | __find_arguments(fmt0, orgap, &argtable, &argtablesiz); \ |
| 423 | } \ |
| 424 | nextarg = n2; \ |
| 425 | val = GETARG(int); \ |
| 426 | nextarg = hold; \ |
| 427 | fmt = ++cp; \ |
| 428 | } else { \ |
| 429 | val = GETARG(int); \ |
| 430 | } |
| 431 | |
| 432 | /* |
| 433 | * Get the argument indexed by nextarg. If the argument table is |
| 434 | * built, use it to get the argument. If its not, get the next |
| 435 | * argument (and arguments must be gotten sequentially). |
| 436 | */ |
| 437 | #define GETARG(type) \ |
| 438 | ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \ |
| 439 | (nextarg++, va_arg(ap, type))) |
| 440 | |
| 441 | _SET_ORIENTATION(fp, 1); |
| 442 | /* sorry, fwprintf(read_only_file, "") returns EOF, not 0 */ |
| 443 | if (cantwrite(fp)) { |
| 444 | errno = EBADF; |
| 445 | return (EOF); |
| 446 | } |
| 447 | |
| 448 | /* optimise fwprintf(stderr) (and other unbuffered Unix files) */ |
| 449 | if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) && |
| 450 | fp->_file >= 0) |
| 451 | return (__sbprintf(fp, fmt0, ap)); |
| 452 | |
| 453 | fmt = (wchar_t *)fmt0; |
| 454 | argtable = NULL; |
| 455 | nextarg = 1; |
| 456 | va_copy(orgap, ap); |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 457 | ret = 0; |
| 458 | convbuf = NULL; |
| 459 | |
| 460 | /* |
| 461 | * Scan the format for conversions (`%' character). |
| 462 | */ |
| 463 | for (;;) { |
| 464 | for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) |
| 465 | continue; |
| 466 | if (fmt != cp) { |
| 467 | ptrdiff_t m = fmt - cp; |
| 468 | if (m < 0 || m > INT_MAX - ret) |
| 469 | goto overflow; |
| 470 | PRINT(cp, m); |
| 471 | ret += m; |
| 472 | } |
| 473 | if (ch == '\0') |
| 474 | goto done; |
| 475 | fmt++; /* skip over '%' */ |
| 476 | |
| 477 | flags = 0; |
| 478 | dprec = 0; |
| 479 | width = 0; |
| 480 | prec = -1; |
| 481 | sign = '\0'; |
| 482 | ox[1] = '\0'; |
| 483 | |
| 484 | rflag: ch = *fmt++; |
| 485 | reswitch: switch (ch) { |
| 486 | case ' ': |
| 487 | /* |
| 488 | * ``If the space and + flags both appear, the space |
| 489 | * flag will be ignored.'' |
| 490 | * -- ANSI X3J11 |
| 491 | */ |
| 492 | if (!sign) |
| 493 | sign = ' '; |
| 494 | goto rflag; |
| 495 | case '#': |
| 496 | flags |= ALT; |
| 497 | goto rflag; |
| 498 | case '\'': |
| 499 | /* grouping not implemented */ |
| 500 | goto rflag; |
| 501 | case '*': |
| 502 | /* |
| 503 | * ``A negative field width argument is taken as a |
| 504 | * - flag followed by a positive field width.'' |
| 505 | * -- ANSI X3J11 |
| 506 | * They don't exclude field widths read from args. |
| 507 | */ |
| 508 | GETASTER(width); |
| 509 | if (width >= 0) |
| 510 | goto rflag; |
| 511 | if (width == INT_MIN) |
| 512 | goto overflow; |
| 513 | width = -width; |
| 514 | /* FALLTHROUGH */ |
| 515 | case '-': |
| 516 | flags |= LADJUST; |
| 517 | goto rflag; |
| 518 | case '+': |
| 519 | sign = '+'; |
| 520 | goto rflag; |
| 521 | case '.': |
| 522 | if ((ch = *fmt++) == '*') { |
| 523 | GETASTER(n); |
| 524 | prec = n < 0 ? -1 : n; |
| 525 | goto rflag; |
| 526 | } |
| 527 | n = 0; |
| 528 | while (is_digit(ch)) { |
| 529 | APPEND_DIGIT(n, ch); |
| 530 | ch = *fmt++; |
| 531 | } |
| 532 | if (ch == '$') { |
| 533 | nextarg = n; |
| 534 | if (argtable == NULL) { |
| 535 | argtable = statargtable; |
| 536 | __find_arguments(fmt0, orgap, |
| 537 | &argtable, &argtablesiz); |
| 538 | } |
| 539 | goto rflag; |
| 540 | } |
| 541 | prec = n; |
| 542 | goto reswitch; |
| 543 | case '0': |
| 544 | /* |
| 545 | * ``Note that 0 is taken as a flag, not as the |
| 546 | * beginning of a field width.'' |
| 547 | * -- ANSI X3J11 |
| 548 | */ |
| 549 | flags |= ZEROPAD; |
| 550 | goto rflag; |
| 551 | case '1': case '2': case '3': case '4': |
| 552 | case '5': case '6': case '7': case '8': case '9': |
| 553 | n = 0; |
| 554 | do { |
| 555 | APPEND_DIGIT(n, ch); |
| 556 | ch = *fmt++; |
| 557 | } while (is_digit(ch)); |
| 558 | if (ch == '$') { |
| 559 | nextarg = n; |
| 560 | if (argtable == NULL) { |
| 561 | argtable = statargtable; |
| 562 | __find_arguments(fmt0, orgap, |
| 563 | &argtable, &argtablesiz); |
| 564 | } |
| 565 | goto rflag; |
| 566 | } |
| 567 | width = n; |
| 568 | goto reswitch; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 569 | case 'L': |
| 570 | flags |= LONGDBL; |
| 571 | goto rflag; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 572 | case 'h': |
| 573 | if (*fmt == 'h') { |
| 574 | fmt++; |
| 575 | flags |= CHARINT; |
| 576 | } else { |
| 577 | flags |= SHORTINT; |
| 578 | } |
| 579 | goto rflag; |
| 580 | case 'j': |
| 581 | flags |= MAXINT; |
| 582 | goto rflag; |
| 583 | case 'l': |
| 584 | if (*fmt == 'l') { |
| 585 | fmt++; |
| 586 | flags |= LLONGINT; |
| 587 | } else { |
| 588 | flags |= LONGINT; |
| 589 | } |
| 590 | goto rflag; |
| 591 | case 'q': |
| 592 | flags |= LLONGINT; |
| 593 | goto rflag; |
| 594 | case 't': |
| 595 | flags |= PTRINT; |
| 596 | goto rflag; |
| 597 | case 'z': |
| 598 | flags |= SIZEINT; |
| 599 | goto rflag; |
| 600 | case 'C': |
| 601 | flags |= LONGINT; |
| 602 | /*FALLTHROUGH*/ |
| 603 | case 'c': |
| 604 | if (flags & LONGINT) |
| 605 | *(cp = buf) = (wchar_t)GETARG(wint_t); |
| 606 | else |
| 607 | *(cp = buf) = (wchar_t)btowc(GETARG(int)); |
| 608 | size = 1; |
| 609 | sign = '\0'; |
| 610 | break; |
| 611 | case 'D': |
| 612 | flags |= LONGINT; |
| 613 | /*FALLTHROUGH*/ |
| 614 | case 'd': |
| 615 | case 'i': |
| 616 | _umax = SARG(); |
| 617 | if ((intmax_t)_umax < 0) { |
| 618 | _umax = -_umax; |
| 619 | sign = '-'; |
| 620 | } |
| 621 | base = DEC; |
| 622 | goto number; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 623 | case 'a': |
| 624 | case 'A': |
| 625 | if (ch == 'a') { |
| 626 | ox[1] = 'x'; |
| 627 | xdigs = xdigs_lower; |
| 628 | expchar = 'p'; |
| 629 | } else { |
| 630 | ox[1] = 'X'; |
| 631 | xdigs = xdigs_upper; |
| 632 | expchar = 'P'; |
| 633 | } |
| 634 | if (prec >= 0) |
| 635 | prec++; |
| 636 | if (dtoaresult) |
| 637 | __freedtoa(dtoaresult); |
| 638 | if (flags & LONGDBL) { |
| 639 | fparg.ldbl = GETARG(long double); |
| 640 | dtoaresult = |
| 641 | __hldtoa(fparg.ldbl, xdigs, prec, |
| 642 | &expt, &signflag, &dtoaend); |
| 643 | if (dtoaresult == NULL) { |
| 644 | errno = ENOMEM; |
| 645 | goto error; |
| 646 | } |
| 647 | } else { |
| 648 | fparg.dbl = GETARG(double); |
| 649 | dtoaresult = |
| 650 | __hdtoa(fparg.dbl, xdigs, prec, |
| 651 | &expt, &signflag, &dtoaend); |
| 652 | if (dtoaresult == NULL) { |
| 653 | errno = ENOMEM; |
| 654 | goto error; |
| 655 | } |
| 656 | } |
| 657 | if (prec < 0) |
| 658 | prec = dtoaend - dtoaresult; |
| 659 | if (expt == INT_MAX) |
| 660 | ox[1] = '\0'; |
Elliott Hughes | 506c6de | 2016-01-15 16:30:18 -0800 | [diff] [blame] | 661 | free(convbuf); |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 662 | cp = convbuf = __mbsconv(dtoaresult, -1); |
| 663 | if (cp == NULL) |
| 664 | goto error; |
| 665 | ndig = dtoaend - dtoaresult; |
| 666 | goto fp_common; |
| 667 | case 'e': |
| 668 | case 'E': |
| 669 | expchar = ch; |
| 670 | if (prec < 0) /* account for digit before decpt */ |
| 671 | prec = DEFPREC + 1; |
| 672 | else |
| 673 | prec++; |
| 674 | goto fp_begin; |
| 675 | case 'f': |
| 676 | case 'F': |
| 677 | expchar = '\0'; |
| 678 | goto fp_begin; |
| 679 | case 'g': |
| 680 | case 'G': |
| 681 | expchar = ch - ('g' - 'e'); |
| 682 | if (prec == 0) |
| 683 | prec = 1; |
| 684 | fp_begin: |
| 685 | if (prec < 0) |
| 686 | prec = DEFPREC; |
| 687 | if (dtoaresult) |
| 688 | __freedtoa(dtoaresult); |
| 689 | if (flags & LONGDBL) { |
| 690 | fparg.ldbl = GETARG(long double); |
| 691 | dtoaresult = |
| 692 | __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec, |
| 693 | &expt, &signflag, &dtoaend); |
| 694 | if (dtoaresult == NULL) { |
| 695 | errno = ENOMEM; |
| 696 | goto error; |
| 697 | } |
| 698 | } else { |
| 699 | fparg.dbl = GETARG(double); |
| 700 | dtoaresult = |
| 701 | __dtoa(fparg.dbl, expchar ? 2 : 3, prec, |
| 702 | &expt, &signflag, &dtoaend); |
| 703 | if (dtoaresult == NULL) { |
| 704 | errno = ENOMEM; |
| 705 | goto error; |
| 706 | } |
| 707 | if (expt == 9999) |
| 708 | expt = INT_MAX; |
| 709 | } |
Elliott Hughes | 506c6de | 2016-01-15 16:30:18 -0800 | [diff] [blame] | 710 | free(convbuf); |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 711 | cp = convbuf = __mbsconv(dtoaresult, -1); |
| 712 | if (cp == NULL) |
| 713 | goto error; |
| 714 | ndig = dtoaend - dtoaresult; |
| 715 | fp_common: |
| 716 | if (signflag) |
| 717 | sign = '-'; |
| 718 | if (expt == INT_MAX) { /* inf or nan */ |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 719 | if (*cp == 'N') |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 720 | cp = (ch >= 'a') ? L"nan" : L"NAN"; |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 721 | else |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 722 | cp = (ch >= 'a') ? L"inf" : L"INF"; |
| 723 | size = 3; |
| 724 | flags &= ~ZEROPAD; |
| 725 | break; |
| 726 | } |
| 727 | flags |= FPT; |
| 728 | if (ch == 'g' || ch == 'G') { |
| 729 | if (expt > -4 && expt <= prec) { |
| 730 | /* Make %[gG] smell like %[fF] */ |
| 731 | expchar = '\0'; |
| 732 | if (flags & ALT) |
| 733 | prec -= expt; |
| 734 | else |
| 735 | prec = ndig - expt; |
| 736 | if (prec < 0) |
| 737 | prec = 0; |
| 738 | } else { |
| 739 | /* |
| 740 | * Make %[gG] smell like %[eE], but |
| 741 | * trim trailing zeroes if no # flag. |
| 742 | */ |
| 743 | if (!(flags & ALT)) |
| 744 | prec = ndig; |
| 745 | } |
| 746 | } |
| 747 | if (expchar) { |
| 748 | expsize = exponent(expstr, expt - 1, expchar); |
| 749 | size = expsize + prec; |
| 750 | if (prec > 1 || flags & ALT) |
| 751 | ++size; |
| 752 | } else { |
| 753 | /* space for digits before decimal point */ |
| 754 | if (expt > 0) |
| 755 | size = expt; |
| 756 | else /* "0" */ |
| 757 | size = 1; |
| 758 | /* space for decimal pt and following digits */ |
| 759 | if (prec || flags & ALT) |
| 760 | size += prec + 1; |
| 761 | lead = expt; |
| 762 | } |
| 763 | break; |
Elliott Hughes | e2341d0 | 2014-05-02 18:16:32 -0700 | [diff] [blame] | 764 | #ifndef NO_PRINTF_PERCENT_N |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 765 | case 'n': |
| 766 | if (flags & LLONGINT) |
| 767 | *GETARG(long long *) = ret; |
| 768 | else if (flags & LONGINT) |
| 769 | *GETARG(long *) = ret; |
| 770 | else if (flags & SHORTINT) |
| 771 | *GETARG(short *) = ret; |
| 772 | else if (flags & CHARINT) |
Elliott Hughes | f1ada79 | 2014-05-02 17:56:56 -0700 | [diff] [blame] | 773 | *GETARG(signed char *) = ret; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 774 | else if (flags & PTRINT) |
| 775 | *GETARG(ptrdiff_t *) = ret; |
| 776 | else if (flags & SIZEINT) |
| 777 | *GETARG(ssize_t *) = ret; |
| 778 | else if (flags & MAXINT) |
| 779 | *GETARG(intmax_t *) = ret; |
| 780 | else |
| 781 | *GETARG(int *) = ret; |
| 782 | continue; /* no output */ |
Elliott Hughes | e2341d0 | 2014-05-02 18:16:32 -0700 | [diff] [blame] | 783 | #endif /* NO_PRINTF_PERCENT_N */ |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 784 | case 'O': |
| 785 | flags |= LONGINT; |
| 786 | /*FALLTHROUGH*/ |
| 787 | case 'o': |
| 788 | _umax = UARG(); |
| 789 | base = OCT; |
| 790 | goto nosign; |
| 791 | case 'p': |
| 792 | /* |
| 793 | * ``The argument shall be a pointer to void. The |
| 794 | * value of the pointer is converted to a sequence |
| 795 | * of printable characters, in an implementation- |
| 796 | * defined manner.'' |
| 797 | * -- ANSI X3J11 |
| 798 | */ |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 799 | _umax = (u_long)GETARG(void *); |
| 800 | base = HEX; |
| 801 | xdigs = xdigs_lower; |
| 802 | ox[1] = 'x'; |
| 803 | goto nosign; |
| 804 | case 'S': |
| 805 | flags |= LONGINT; |
| 806 | /*FALLTHROUGH*/ |
| 807 | case 's': |
| 808 | if (flags & LONGINT) { |
| 809 | if ((cp = GETARG(wchar_t *)) == NULL) |
| 810 | cp = L"(null)"; |
| 811 | } else { |
| 812 | char *mbsarg; |
| 813 | if ((mbsarg = GETARG(char *)) == NULL) |
| 814 | mbsarg = "(null)"; |
Elliott Hughes | 506c6de | 2016-01-15 16:30:18 -0800 | [diff] [blame] | 815 | free(convbuf); |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 816 | convbuf = __mbsconv(mbsarg, prec); |
| 817 | if (convbuf == NULL) { |
| 818 | fp->_flags |= __SERR; |
| 819 | goto error; |
| 820 | } else |
| 821 | cp = convbuf; |
| 822 | } |
| 823 | if (prec >= 0) { |
| 824 | /* |
| 825 | * can't use wcslen; can only look for the |
| 826 | * NUL in the first `prec' characters, and |
| 827 | * wcslen() will go further. |
| 828 | */ |
| 829 | wchar_t *p = wmemchr(cp, 0, prec); |
| 830 | |
| 831 | size = p ? (p - cp) : prec; |
| 832 | } else { |
| 833 | size_t len; |
| 834 | |
| 835 | if ((len = wcslen(cp)) > INT_MAX) |
| 836 | goto overflow; |
| 837 | size = (int)len; |
| 838 | } |
| 839 | sign = '\0'; |
| 840 | break; |
| 841 | case 'U': |
| 842 | flags |= LONGINT; |
| 843 | /*FALLTHROUGH*/ |
| 844 | case 'u': |
| 845 | _umax = UARG(); |
| 846 | base = DEC; |
| 847 | goto nosign; |
| 848 | case 'X': |
| 849 | xdigs = xdigs_upper; |
| 850 | goto hex; |
| 851 | case 'x': |
| 852 | xdigs = xdigs_lower; |
| 853 | hex: _umax = UARG(); |
| 854 | base = HEX; |
| 855 | /* leading 0x/X only if non-zero */ |
| 856 | if (flags & ALT && _umax != 0) |
| 857 | ox[1] = ch; |
| 858 | |
| 859 | /* unsigned conversions */ |
| 860 | nosign: sign = '\0'; |
| 861 | /* |
| 862 | * ``... diouXx conversions ... if a precision is |
| 863 | * specified, the 0 flag will be ignored.'' |
| 864 | * -- ANSI X3J11 |
| 865 | */ |
| 866 | number: if ((dprec = prec) >= 0) |
| 867 | flags &= ~ZEROPAD; |
| 868 | |
| 869 | /* |
| 870 | * ``The result of converting a zero value with an |
| 871 | * explicit precision of zero is no characters.'' |
| 872 | * -- ANSI X3J11 |
| 873 | */ |
| 874 | cp = buf + BUF; |
| 875 | if (_umax != 0 || prec != 0) { |
| 876 | /* |
| 877 | * Unsigned mod is hard, and unsigned mod |
| 878 | * by a constant is easier than that by |
| 879 | * a variable; hence this switch. |
| 880 | */ |
| 881 | switch (base) { |
| 882 | case OCT: |
| 883 | do { |
| 884 | *--cp = to_char(_umax & 7); |
| 885 | _umax >>= 3; |
| 886 | } while (_umax); |
| 887 | /* handle octal leading 0 */ |
| 888 | if (flags & ALT && *cp != '0') |
| 889 | *--cp = '0'; |
| 890 | break; |
| 891 | |
| 892 | case DEC: |
| 893 | /* many numbers are 1 digit */ |
| 894 | while (_umax >= 10) { |
| 895 | *--cp = to_char(_umax % 10); |
| 896 | _umax /= 10; |
| 897 | } |
| 898 | *--cp = to_char(_umax); |
| 899 | break; |
| 900 | |
| 901 | case HEX: |
| 902 | do { |
| 903 | *--cp = xdigs[_umax & 15]; |
| 904 | _umax >>= 4; |
| 905 | } while (_umax); |
| 906 | break; |
| 907 | |
| 908 | default: |
| 909 | cp = L"bug in vfwprintf: bad base"; |
| 910 | size = wcslen(cp); |
| 911 | goto skipsize; |
| 912 | } |
| 913 | } |
| 914 | size = buf + BUF - cp; |
| 915 | if (size > BUF) /* should never happen */ |
| 916 | abort(); |
| 917 | skipsize: |
| 918 | break; |
| 919 | default: /* "%?" prints ?, unless ? is NUL */ |
| 920 | if (ch == '\0') |
| 921 | goto done; |
| 922 | /* pretend it was %c with argument ch */ |
| 923 | cp = buf; |
| 924 | *cp = ch; |
| 925 | size = 1; |
| 926 | sign = '\0'; |
| 927 | break; |
| 928 | } |
| 929 | |
| 930 | /* |
| 931 | * All reasonable formats wind up here. At this point, `cp' |
| 932 | * points to a string which (if not flags&LADJUST) should be |
| 933 | * padded out to `width' places. If flags&ZEROPAD, it should |
| 934 | * first be prefixed by any sign or other prefix; otherwise, |
| 935 | * it should be blank padded before the prefix is emitted. |
| 936 | * After any left-hand padding and prefixing, emit zeroes |
| 937 | * required by a decimal %[diouxX] precision, then print the |
| 938 | * string proper, then emit zeroes required by any leftover |
| 939 | * floating precision; finally, if LADJUST, pad with blanks. |
| 940 | * |
| 941 | * Compute actual size, so we know how much to pad. |
| 942 | * size excludes decimal prec; realsz includes it. |
| 943 | */ |
| 944 | realsz = dprec > size ? dprec : size; |
| 945 | if (sign) |
| 946 | realsz++; |
| 947 | if (ox[1]) |
| 948 | realsz+= 2; |
| 949 | |
| 950 | /* right-adjusting blank padding */ |
| 951 | if ((flags & (LADJUST|ZEROPAD)) == 0) |
| 952 | PAD(width - realsz, blanks); |
| 953 | |
| 954 | /* prefix */ |
| 955 | if (sign) |
| 956 | PRINT(&sign, 1); |
| 957 | if (ox[1]) { /* ox[1] is either x, X, or \0 */ |
| 958 | ox[0] = '0'; |
| 959 | PRINT(ox, 2); |
| 960 | } |
| 961 | |
| 962 | /* right-adjusting zero padding */ |
| 963 | if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) |
| 964 | PAD(width - realsz, zeroes); |
| 965 | |
| 966 | /* leading zeroes from decimal precision */ |
| 967 | PAD(dprec - size, zeroes); |
| 968 | |
| 969 | /* the string or number proper */ |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 970 | if ((flags & FPT) == 0) { |
| 971 | PRINT(cp, size); |
| 972 | } else { /* glue together f_p fragments */ |
| 973 | if (decimal_point == NULL) |
| 974 | decimal_point = nl_langinfo(RADIXCHAR); |
| 975 | if (!expchar) { /* %[fF] or sufficiently short %[gG] */ |
| 976 | if (expt <= 0) { |
| 977 | PRINT(zeroes, 1); |
| 978 | if (prec || flags & ALT) |
| 979 | PRINT(decimal_point, 1); |
| 980 | PAD(-expt, zeroes); |
| 981 | /* already handled initial 0's */ |
| 982 | prec += expt; |
| 983 | } else { |
| 984 | PRINTANDPAD(cp, convbuf + ndig, |
| 985 | lead, zeroes); |
| 986 | cp += lead; |
| 987 | if (prec || flags & ALT) |
| 988 | PRINT(decimal_point, 1); |
| 989 | } |
| 990 | PRINTANDPAD(cp, convbuf + ndig, prec, zeroes); |
| 991 | } else { /* %[eE] or sufficiently long %[gG] */ |
| 992 | if (prec > 1 || flags & ALT) { |
| 993 | buf[0] = *cp++; |
| 994 | buf[1] = *decimal_point; |
| 995 | PRINT(buf, 2); |
| 996 | PRINT(cp, ndig-1); |
| 997 | PAD(prec - ndig, zeroes); |
| 998 | } else { /* XeYYY */ |
| 999 | PRINT(cp, 1); |
| 1000 | } |
| 1001 | PRINT(expstr, expsize); |
| 1002 | } |
| 1003 | } |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 1004 | /* left-adjusting padding (always blank) */ |
| 1005 | if (flags & LADJUST) |
| 1006 | PAD(width - realsz, blanks); |
| 1007 | |
| 1008 | /* finally, adjust ret */ |
| 1009 | if (width < realsz) |
| 1010 | width = realsz; |
| 1011 | if (width > INT_MAX - ret) |
| 1012 | goto overflow; |
| 1013 | ret += width; |
| 1014 | } |
| 1015 | done: |
| 1016 | error: |
| 1017 | va_end(orgap); |
| 1018 | if (__sferror(fp)) |
| 1019 | ret = -1; |
| 1020 | goto finish; |
| 1021 | |
| 1022 | overflow: |
| 1023 | errno = ENOMEM; |
| 1024 | ret = -1; |
| 1025 | |
| 1026 | finish: |
Elliott Hughes | 506c6de | 2016-01-15 16:30:18 -0800 | [diff] [blame] | 1027 | free(convbuf); |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 1028 | if (dtoaresult) |
| 1029 | __freedtoa(dtoaresult); |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 1030 | if (argtable != NULL && argtable != statargtable) { |
| 1031 | munmap(argtable, argtablesiz); |
| 1032 | argtable = NULL; |
| 1033 | } |
| 1034 | return (ret); |
| 1035 | } |
| 1036 | |
| 1037 | int |
| 1038 | vfwprintf(FILE * __restrict fp, const wchar_t * __restrict fmt0, __va_list ap) |
| 1039 | { |
| 1040 | int r; |
| 1041 | |
| 1042 | FLOCKFILE(fp); |
| 1043 | r = __vfwprintf(fp, fmt0, ap); |
| 1044 | FUNLOCKFILE(fp); |
| 1045 | |
| 1046 | return (r); |
| 1047 | } |
Elliott Hughes | 506c6de | 2016-01-15 16:30:18 -0800 | [diff] [blame] | 1048 | DEF_STRONG(vfwprintf); |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 1049 | |
| 1050 | /* |
| 1051 | * Type ids for argument type table. |
| 1052 | */ |
| 1053 | #define T_UNUSED 0 |
| 1054 | #define T_SHORT 1 |
| 1055 | #define T_U_SHORT 2 |
| 1056 | #define TP_SHORT 3 |
| 1057 | #define T_INT 4 |
| 1058 | #define T_U_INT 5 |
| 1059 | #define TP_INT 6 |
| 1060 | #define T_LONG 7 |
| 1061 | #define T_U_LONG 8 |
| 1062 | #define TP_LONG 9 |
| 1063 | #define T_LLONG 10 |
| 1064 | #define T_U_LLONG 11 |
| 1065 | #define TP_LLONG 12 |
| 1066 | #define T_DOUBLE 13 |
| 1067 | #define T_LONG_DOUBLE 14 |
| 1068 | #define TP_CHAR 15 |
| 1069 | #define TP_VOID 16 |
| 1070 | #define T_PTRINT 17 |
| 1071 | #define TP_PTRINT 18 |
| 1072 | #define T_SIZEINT 19 |
| 1073 | #define T_SSIZEINT 20 |
| 1074 | #define TP_SSIZEINT 21 |
| 1075 | #define T_MAXINT 22 |
| 1076 | #define T_MAXUINT 23 |
| 1077 | #define TP_MAXINT 24 |
| 1078 | #define T_CHAR 25 |
| 1079 | #define T_U_CHAR 26 |
| 1080 | #define T_WINT 27 |
| 1081 | #define TP_WCHAR 28 |
| 1082 | |
| 1083 | /* |
| 1084 | * Find all arguments when a positional parameter is encountered. Returns a |
| 1085 | * table, indexed by argument number, of pointers to each arguments. The |
| 1086 | * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries. |
| 1087 | * It will be replaced with a mmap-ed one if it overflows (malloc cannot be |
| 1088 | * used since we are attempting to make snprintf thread safe, and alloca is |
| 1089 | * problematic since we have nested functions..) |
| 1090 | */ |
| 1091 | static int |
| 1092 | __find_arguments(const wchar_t *fmt0, va_list ap, union arg **argtable, |
| 1093 | size_t *argtablesiz) |
| 1094 | { |
| 1095 | wchar_t *fmt; /* format string */ |
| 1096 | int ch; /* character from fmt */ |
| 1097 | int n, n2; /* handy integer (short term usage) */ |
| 1098 | wchar_t *cp; /* handy char pointer (short term usage) */ |
| 1099 | int flags; /* flags as above */ |
| 1100 | unsigned char *typetable; /* table of types */ |
| 1101 | unsigned char stattypetable[STATIC_ARG_TBL_SIZE]; |
| 1102 | int tablesize; /* current size of type table */ |
| 1103 | int tablemax; /* largest used index in table */ |
| 1104 | int nextarg; /* 1-based argument index */ |
| 1105 | int ret = 0; /* return value */ |
| 1106 | |
| 1107 | /* |
| 1108 | * Add an argument type to the table, expanding if necessary. |
| 1109 | */ |
| 1110 | #define ADDTYPE(type) \ |
| 1111 | ((nextarg >= tablesize) ? \ |
| 1112 | __grow_type_table(&typetable, &tablesize) : 0, \ |
| 1113 | (nextarg > tablemax) ? tablemax = nextarg : 0, \ |
| 1114 | typetable[nextarg++] = type) |
| 1115 | |
| 1116 | #define ADDSARG() \ |
| 1117 | ((flags&MAXINT) ? ADDTYPE(T_MAXINT) : \ |
| 1118 | ((flags&PTRINT) ? ADDTYPE(T_PTRINT) : \ |
| 1119 | ((flags&SIZEINT) ? ADDTYPE(T_SSIZEINT) : \ |
| 1120 | ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \ |
| 1121 | ((flags&LONGINT) ? ADDTYPE(T_LONG) : \ |
| 1122 | ((flags&SHORTINT) ? ADDTYPE(T_SHORT) : \ |
| 1123 | ((flags&CHARINT) ? ADDTYPE(T_CHAR) : ADDTYPE(T_INT)))))))) |
| 1124 | |
| 1125 | #define ADDUARG() \ |
| 1126 | ((flags&MAXINT) ? ADDTYPE(T_MAXUINT) : \ |
| 1127 | ((flags&PTRINT) ? ADDTYPE(T_PTRINT) : \ |
| 1128 | ((flags&SIZEINT) ? ADDTYPE(T_SIZEINT) : \ |
| 1129 | ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \ |
| 1130 | ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : \ |
| 1131 | ((flags&SHORTINT) ? ADDTYPE(T_U_SHORT) : \ |
| 1132 | ((flags&CHARINT) ? ADDTYPE(T_U_CHAR) : ADDTYPE(T_U_INT)))))))) |
| 1133 | |
| 1134 | /* |
| 1135 | * Add * arguments to the type array. |
| 1136 | */ |
| 1137 | #define ADDASTER() \ |
| 1138 | n2 = 0; \ |
| 1139 | cp = fmt; \ |
| 1140 | while (is_digit(*cp)) { \ |
| 1141 | APPEND_DIGIT(n2, *cp); \ |
| 1142 | cp++; \ |
| 1143 | } \ |
| 1144 | if (*cp == '$') { \ |
| 1145 | int hold = nextarg; \ |
| 1146 | nextarg = n2; \ |
| 1147 | ADDTYPE(T_INT); \ |
| 1148 | nextarg = hold; \ |
| 1149 | fmt = ++cp; \ |
| 1150 | } else { \ |
| 1151 | ADDTYPE(T_INT); \ |
| 1152 | } |
| 1153 | fmt = (wchar_t *)fmt0; |
| 1154 | typetable = stattypetable; |
| 1155 | tablesize = STATIC_ARG_TBL_SIZE; |
| 1156 | tablemax = 0; |
| 1157 | nextarg = 1; |
| 1158 | memset(typetable, T_UNUSED, STATIC_ARG_TBL_SIZE); |
| 1159 | |
| 1160 | /* |
| 1161 | * Scan the format for conversions (`%' character). |
| 1162 | */ |
| 1163 | for (;;) { |
| 1164 | for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) |
| 1165 | continue; |
| 1166 | if (ch == '\0') |
| 1167 | goto done; |
| 1168 | fmt++; /* skip over '%' */ |
| 1169 | |
| 1170 | flags = 0; |
| 1171 | |
| 1172 | rflag: ch = *fmt++; |
| 1173 | reswitch: switch (ch) { |
| 1174 | case ' ': |
| 1175 | case '#': |
| 1176 | case '\'': |
| 1177 | goto rflag; |
| 1178 | case '*': |
| 1179 | ADDASTER(); |
| 1180 | goto rflag; |
| 1181 | case '-': |
| 1182 | case '+': |
| 1183 | goto rflag; |
| 1184 | case '.': |
| 1185 | if ((ch = *fmt++) == '*') { |
| 1186 | ADDASTER(); |
| 1187 | goto rflag; |
| 1188 | } |
| 1189 | while (is_digit(ch)) { |
| 1190 | ch = *fmt++; |
| 1191 | } |
| 1192 | goto reswitch; |
| 1193 | case '0': |
| 1194 | goto rflag; |
| 1195 | case '1': case '2': case '3': case '4': |
| 1196 | case '5': case '6': case '7': case '8': case '9': |
| 1197 | n = 0; |
| 1198 | do { |
| 1199 | APPEND_DIGIT(n ,ch); |
| 1200 | ch = *fmt++; |
| 1201 | } while (is_digit(ch)); |
| 1202 | if (ch == '$') { |
| 1203 | nextarg = n; |
| 1204 | goto rflag; |
| 1205 | } |
| 1206 | goto reswitch; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 1207 | case 'L': |
| 1208 | flags |= LONGDBL; |
| 1209 | goto rflag; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 1210 | case 'h': |
| 1211 | if (*fmt == 'h') { |
| 1212 | fmt++; |
| 1213 | flags |= CHARINT; |
| 1214 | } else { |
| 1215 | flags |= SHORTINT; |
| 1216 | } |
| 1217 | goto rflag; |
| 1218 | case 'l': |
| 1219 | if (*fmt == 'l') { |
| 1220 | fmt++; |
| 1221 | flags |= LLONGINT; |
| 1222 | } else { |
| 1223 | flags |= LONGINT; |
| 1224 | } |
| 1225 | goto rflag; |
| 1226 | case 'q': |
| 1227 | flags |= LLONGINT; |
| 1228 | goto rflag; |
| 1229 | case 't': |
| 1230 | flags |= PTRINT; |
| 1231 | goto rflag; |
| 1232 | case 'z': |
| 1233 | flags |= SIZEINT; |
| 1234 | goto rflag; |
| 1235 | case 'C': |
| 1236 | flags |= LONGINT; |
| 1237 | /*FALLTHROUGH*/ |
| 1238 | case 'c': |
| 1239 | if (flags & LONGINT) |
| 1240 | ADDTYPE(T_WINT); |
| 1241 | else |
| 1242 | ADDTYPE(T_INT); |
| 1243 | break; |
| 1244 | case 'D': |
| 1245 | flags |= LONGINT; |
| 1246 | /*FALLTHROUGH*/ |
| 1247 | case 'd': |
| 1248 | case 'i': |
| 1249 | ADDSARG(); |
| 1250 | break; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 1251 | case 'a': |
| 1252 | case 'A': |
| 1253 | case 'e': |
| 1254 | case 'E': |
| 1255 | case 'f': |
| 1256 | case 'F': |
| 1257 | case 'g': |
| 1258 | case 'G': |
| 1259 | if (flags & LONGDBL) |
| 1260 | ADDTYPE(T_LONG_DOUBLE); |
| 1261 | else |
| 1262 | ADDTYPE(T_DOUBLE); |
| 1263 | break; |
Elliott Hughes | e2341d0 | 2014-05-02 18:16:32 -0700 | [diff] [blame] | 1264 | #ifndef NO_PRINTF_PERCENT_N |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 1265 | case 'n': |
| 1266 | if (flags & LLONGINT) |
| 1267 | ADDTYPE(TP_LLONG); |
| 1268 | else if (flags & LONGINT) |
| 1269 | ADDTYPE(TP_LONG); |
| 1270 | else if (flags & SHORTINT) |
| 1271 | ADDTYPE(TP_SHORT); |
| 1272 | else if (flags & PTRINT) |
| 1273 | ADDTYPE(TP_PTRINT); |
| 1274 | else if (flags & SIZEINT) |
| 1275 | ADDTYPE(TP_SSIZEINT); |
| 1276 | else if (flags & MAXINT) |
| 1277 | ADDTYPE(TP_MAXINT); |
| 1278 | else |
| 1279 | ADDTYPE(TP_INT); |
| 1280 | continue; /* no output */ |
Elliott Hughes | e2341d0 | 2014-05-02 18:16:32 -0700 | [diff] [blame] | 1281 | #endif /* NO_PRINTF_PERCENT_N */ |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 1282 | case 'O': |
| 1283 | flags |= LONGINT; |
| 1284 | /*FALLTHROUGH*/ |
| 1285 | case 'o': |
| 1286 | ADDUARG(); |
| 1287 | break; |
| 1288 | case 'p': |
| 1289 | ADDTYPE(TP_VOID); |
| 1290 | break; |
| 1291 | case 'S': |
| 1292 | flags |= LONGINT; |
| 1293 | /*FALLTHROUGH*/ |
| 1294 | case 's': |
| 1295 | if (flags & LONGINT) |
| 1296 | ADDTYPE(TP_CHAR); |
| 1297 | else |
| 1298 | ADDTYPE(TP_WCHAR); |
| 1299 | break; |
| 1300 | case 'U': |
| 1301 | flags |= LONGINT; |
| 1302 | /*FALLTHROUGH*/ |
| 1303 | case 'u': |
| 1304 | case 'X': |
| 1305 | case 'x': |
| 1306 | ADDUARG(); |
| 1307 | break; |
| 1308 | default: /* "%?" prints ?, unless ? is NUL */ |
| 1309 | if (ch == '\0') |
| 1310 | goto done; |
| 1311 | break; |
| 1312 | } |
| 1313 | } |
| 1314 | done: |
| 1315 | /* |
| 1316 | * Build the argument table. |
| 1317 | */ |
| 1318 | if (tablemax >= STATIC_ARG_TBL_SIZE) { |
| 1319 | *argtablesiz = sizeof(union arg) * (tablemax + 1); |
| 1320 | *argtable = mmap(NULL, *argtablesiz, |
| 1321 | PROT_WRITE|PROT_READ, MAP_ANON|MAP_PRIVATE, -1, 0); |
| 1322 | if (*argtable == MAP_FAILED) |
| 1323 | return (-1); |
| 1324 | } |
| 1325 | |
| 1326 | #if 0 |
| 1327 | /* XXX is this required? */ |
| 1328 | (*argtable)[0].intarg = 0; |
| 1329 | #endif |
| 1330 | for (n = 1; n <= tablemax; n++) { |
| 1331 | switch (typetable[n]) { |
| 1332 | case T_UNUSED: |
| 1333 | case T_CHAR: |
| 1334 | case T_U_CHAR: |
| 1335 | case T_SHORT: |
| 1336 | case T_U_SHORT: |
| 1337 | case T_INT: |
| 1338 | (*argtable)[n].intarg = va_arg(ap, int); |
| 1339 | break; |
| 1340 | case TP_SHORT: |
| 1341 | (*argtable)[n].pshortarg = va_arg(ap, short *); |
| 1342 | break; |
| 1343 | case T_U_INT: |
| 1344 | (*argtable)[n].uintarg = va_arg(ap, unsigned int); |
| 1345 | break; |
| 1346 | case TP_INT: |
| 1347 | (*argtable)[n].pintarg = va_arg(ap, int *); |
| 1348 | break; |
| 1349 | case T_LONG: |
| 1350 | (*argtable)[n].longarg = va_arg(ap, long); |
| 1351 | break; |
| 1352 | case T_U_LONG: |
| 1353 | (*argtable)[n].ulongarg = va_arg(ap, unsigned long); |
| 1354 | break; |
| 1355 | case TP_LONG: |
| 1356 | (*argtable)[n].plongarg = va_arg(ap, long *); |
| 1357 | break; |
| 1358 | case T_LLONG: |
| 1359 | (*argtable)[n].longlongarg = va_arg(ap, long long); |
| 1360 | break; |
| 1361 | case T_U_LLONG: |
| 1362 | (*argtable)[n].ulonglongarg = va_arg(ap, unsigned long long); |
| 1363 | break; |
| 1364 | case TP_LLONG: |
| 1365 | (*argtable)[n].plonglongarg = va_arg(ap, long long *); |
| 1366 | break; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 1367 | case T_DOUBLE: |
| 1368 | (*argtable)[n].doublearg = va_arg(ap, double); |
| 1369 | break; |
| 1370 | case T_LONG_DOUBLE: |
| 1371 | (*argtable)[n].longdoublearg = va_arg(ap, long double); |
| 1372 | break; |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 1373 | case TP_CHAR: |
| 1374 | (*argtable)[n].pchararg = va_arg(ap, char *); |
| 1375 | break; |
| 1376 | case TP_VOID: |
| 1377 | (*argtable)[n].pvoidarg = va_arg(ap, void *); |
| 1378 | break; |
| 1379 | case T_PTRINT: |
| 1380 | (*argtable)[n].ptrdiffarg = va_arg(ap, ptrdiff_t); |
| 1381 | break; |
| 1382 | case TP_PTRINT: |
| 1383 | (*argtable)[n].pptrdiffarg = va_arg(ap, ptrdiff_t *); |
| 1384 | break; |
| 1385 | case T_SIZEINT: |
| 1386 | (*argtable)[n].sizearg = va_arg(ap, size_t); |
| 1387 | break; |
| 1388 | case T_SSIZEINT: |
| 1389 | (*argtable)[n].ssizearg = va_arg(ap, ssize_t); |
| 1390 | break; |
| 1391 | case TP_SSIZEINT: |
| 1392 | (*argtable)[n].pssizearg = va_arg(ap, ssize_t *); |
| 1393 | break; |
| 1394 | case TP_MAXINT: |
| 1395 | (*argtable)[n].intmaxarg = va_arg(ap, intmax_t); |
| 1396 | break; |
| 1397 | case T_WINT: |
| 1398 | (*argtable)[n].wintarg = va_arg(ap, wint_t); |
| 1399 | break; |
| 1400 | case TP_WCHAR: |
| 1401 | (*argtable)[n].pwchararg = va_arg(ap, wchar_t *); |
| 1402 | break; |
| 1403 | } |
| 1404 | } |
| 1405 | goto finish; |
| 1406 | |
| 1407 | overflow: |
| 1408 | errno = ENOMEM; |
| 1409 | ret = -1; |
| 1410 | |
| 1411 | finish: |
| 1412 | if (typetable != NULL && typetable != stattypetable) { |
| 1413 | munmap(typetable, *argtablesiz); |
| 1414 | typetable = NULL; |
| 1415 | } |
| 1416 | return (ret); |
| 1417 | } |
| 1418 | |
| 1419 | /* |
| 1420 | * Increase the size of the type table. |
| 1421 | */ |
| 1422 | static int |
| 1423 | __grow_type_table(unsigned char **typetable, int *tablesize) |
| 1424 | { |
| 1425 | unsigned char *oldtable = *typetable; |
| 1426 | int newsize = *tablesize * 2; |
| 1427 | |
| 1428 | if (newsize < getpagesize()) |
| 1429 | newsize = getpagesize(); |
| 1430 | |
| 1431 | if (*tablesize == STATIC_ARG_TBL_SIZE) { |
| 1432 | *typetable = mmap(NULL, newsize, PROT_WRITE|PROT_READ, |
| 1433 | MAP_ANON|MAP_PRIVATE, -1, 0); |
| 1434 | if (*typetable == MAP_FAILED) |
| 1435 | return (-1); |
| 1436 | bcopy(oldtable, *typetable, *tablesize); |
| 1437 | } else { |
| 1438 | unsigned char *new = mmap(NULL, newsize, PROT_WRITE|PROT_READ, |
| 1439 | MAP_ANON|MAP_PRIVATE, -1, 0); |
| 1440 | if (new == MAP_FAILED) |
| 1441 | return (-1); |
| 1442 | memmove(new, *typetable, *tablesize); |
| 1443 | munmap(*typetable, *tablesize); |
| 1444 | *typetable = new; |
| 1445 | } |
| 1446 | memset(*typetable + *tablesize, T_UNUSED, (newsize - *tablesize)); |
| 1447 | |
| 1448 | *tablesize = newsize; |
| 1449 | return (0); |
| 1450 | } |
| 1451 | |
| 1452 | |
Elliott Hughes | 94336d8 | 2014-04-29 17:39:29 -0700 | [diff] [blame] | 1453 | static int |
| 1454 | exponent(wchar_t *p0, int exp, int fmtch) |
| 1455 | { |
| 1456 | wchar_t *p, *t; |
| 1457 | wchar_t expbuf[MAXEXPDIG]; |
| 1458 | |
| 1459 | p = p0; |
| 1460 | *p++ = fmtch; |
| 1461 | if (exp < 0) { |
| 1462 | exp = -exp; |
| 1463 | *p++ = '-'; |
| 1464 | } else |
| 1465 | *p++ = '+'; |
| 1466 | t = expbuf + MAXEXPDIG; |
| 1467 | if (exp > 9) { |
| 1468 | do { |
| 1469 | *--t = to_char(exp % 10); |
| 1470 | } while ((exp /= 10) > 9); |
| 1471 | *--t = to_char(exp); |
| 1472 | for (; t < expbuf + MAXEXPDIG; *p++ = *t++) |
| 1473 | /* nothing */; |
| 1474 | } else { |
| 1475 | /* |
| 1476 | * Exponents for decimal floating point conversions |
| 1477 | * (%[eEgG]) must be at least two characters long, |
| 1478 | * whereas exponents for hexadecimal conversions can |
| 1479 | * be only one character long. |
| 1480 | */ |
| 1481 | if (fmtch == 'e' || fmtch == 'E') |
| 1482 | *p++ = '0'; |
| 1483 | *p++ = to_char(exp); |
| 1484 | } |
| 1485 | return (p - p0); |
| 1486 | } |