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