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