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