blob: da7473564d099f6c7d55a1cf4e146fd7fdb0b8a8 [file] [log] [blame]
Elliott Hughes506c6de2016-01-15 16:30:18 -08001/* $OpenBSD: vfprintf.c,v 1.71 2016/01/04 15:47:47 schwarze Exp $ */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002/*-
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>
Elliott Hughes05493712014-04-17 17:30:03 -070044#include <langinfo.h>
45#include <limits.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080046#include <stdarg.h>
47#include <stddef.h>
48#include <stdio.h>
49#include <stdint.h>
50#include <stdlib.h>
51#include <string.h>
Elliott Hughes05493712014-04-17 17:30:03 -070052#include <unistd.h>
53#include <wchar.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080054
55#include "local.h"
56#include "fvwrite.h"
57
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +040058union arg {
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;
Elliott Hughes05493712014-04-17 17:30:03 -070067 ssize_t ssizearg;
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +040068 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;
Elliott Hughes05493712014-04-17 17:30:03 -070078 ssize_t *pssizearg;
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +040079 intmax_t *pintmaxarg;
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +040080 double doublearg;
81 long double longdoublearg;
Elliott Hughes05493712014-04-17 17:30:03 -070082 wint_t wintarg;
83 wchar_t *pwchararg;
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +040084};
85
86static int __find_arguments(const char *fmt0, va_list ap, union arg **argtable,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080087 size_t *argtablesiz);
88static int __grow_type_table(unsigned char **typetable, int *tablesize);
89
90/*
91 * Flush out all the vectors defined by the given uio,
92 * then reset it so that it can be reused.
93 */
94static int
95__sprint(FILE *fp, struct __suio *uio)
96{
97 int err;
98
99 if (uio->uio_resid == 0) {
100 uio->uio_iovcnt = 0;
101 return (0);
102 }
103 err = __sfvwrite(fp, uio);
104 uio->uio_resid = 0;
105 uio->uio_iovcnt = 0;
106 return (err);
107}
108
109/*
110 * Helper function for `fprintf to unbuffered unix file': creates a
111 * temporary buffer. We only work on write-only files; this avoids
112 * worries about ungetc buffers and so forth.
113 */
114static int
115__sbprintf(FILE *fp, const char *fmt, va_list ap)
116{
117 int ret;
118 FILE fake;
119 struct __sfileext fakeext;
120 unsigned char buf[BUFSIZ];
121
122 _FILEEXT_SETUP(&fake, &fakeext);
123 /* copy the important variables */
124 fake._flags = fp->_flags & ~__SNBF;
125 fake._file = fp->_file;
126 fake._cookie = fp->_cookie;
127 fake._write = fp->_write;
128
129 /* set up the buffer */
130 fake._bf._base = fake._p = buf;
131 fake._bf._size = fake._w = sizeof(buf);
132 fake._lbfsize = 0; /* not actually used, but Just In Case */
133
134 /* do the work, then copy any error status */
Kenny Rootf5823402011-02-12 07:13:44 -0800135 ret = __vfprintf(&fake, fmt, ap);
136 if (ret >= 0 && __sflush(&fake))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800137 ret = EOF;
138 if (fake._flags & __SERR)
139 fp->_flags |= __SERR;
140 return (ret);
141}
142
Elliott Hughes05493712014-04-17 17:30:03 -0700143/*
144 * Convert a wide character string argument for the %ls format to a multibyte
145 * string representation. If not -1, prec specifies the maximum number of
146 * bytes to output, and also means that we can't assume that the wide char
147 * string is null-terminated.
148 */
149static char *
150__wcsconv(wchar_t *wcsarg, int prec)
151{
152 mbstate_t mbs;
153 char buf[MB_LEN_MAX];
154 wchar_t *p;
155 char *convbuf;
156 size_t clen, nbytes;
157
158 /* Allocate space for the maximum number of bytes we could output. */
159 if (prec < 0) {
160 memset(&mbs, 0, sizeof(mbs));
161 p = wcsarg;
162 nbytes = wcsrtombs(NULL, (const wchar_t **)&p, 0, &mbs);
Elliott Hughes506c6de2016-01-15 16:30:18 -0800163 if (nbytes == (size_t)-1)
Elliott Hughes05493712014-04-17 17:30:03 -0700164 return (NULL);
Elliott Hughes05493712014-04-17 17:30:03 -0700165 } else {
166 /*
167 * Optimisation: if the output precision is small enough,
168 * just allocate enough memory for the maximum instead of
169 * scanning the string.
170 */
171 if (prec < 128)
172 nbytes = prec;
173 else {
174 nbytes = 0;
175 p = wcsarg;
176 memset(&mbs, 0, sizeof(mbs));
177 for (;;) {
178 clen = wcrtomb(buf, *p++, &mbs);
179 if (clen == 0 || clen == (size_t)-1 ||
180 nbytes + clen > (size_t)prec)
181 break;
182 nbytes += clen;
183 }
Elliott Hughes506c6de2016-01-15 16:30:18 -0800184 if (clen == (size_t)-1)
Elliott Hughes05493712014-04-17 17:30:03 -0700185 return (NULL);
Elliott Hughes05493712014-04-17 17:30:03 -0700186 }
187 }
188 if ((convbuf = malloc(nbytes + 1)) == NULL)
189 return (NULL);
190
191 /* Fill the output buffer. */
192 p = wcsarg;
193 memset(&mbs, 0, sizeof(mbs));
194 if ((nbytes = wcsrtombs(convbuf, (const wchar_t **)&p,
195 nbytes, &mbs)) == (size_t)-1) {
196 free(convbuf);
Elliott Hughes05493712014-04-17 17:30:03 -0700197 return (NULL);
198 }
199 convbuf[nbytes] = '\0';
200 return (convbuf);
201}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800202
Elliott Hughes05493712014-04-17 17:30:03 -0700203#include <float.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800204#include <locale.h>
205#include <math.h>
206#include "floatio.h"
Elliott Hughesf1ada792014-05-02 17:56:56 -0700207#include "gdtoa.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800208
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800209#define DEFPREC 6
210
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800211static int exponent(char *, int, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800212
Elliott Hughes05493712014-04-17 17:30:03 -0700213/*
214 * The size of the buffer we use as scratch space for integer
215 * conversions, among other things. Technically, we would need the
216 * most space for base 10 conversions with thousands' grouping
217 * characters between each pair of digits. 100 bytes is a
218 * conservative overestimate even for a 128-bit uintmax_t.
219 */
220#define BUF 100
221
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800222#define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */
223
Elliott Hughes05493712014-04-17 17:30:03 -0700224
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800225/*
226 * Macros for converting digits to letters and vice versa
227 */
228#define to_digit(c) ((c) - '0')
229#define is_digit(c) ((unsigned)to_digit(c) <= 9)
230#define to_char(n) ((n) + '0')
231
232/*
233 * Flags used during conversion.
234 */
235#define ALT 0x0001 /* alternate form */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800236#define LADJUST 0x0004 /* left adjustment */
Elliott Hughes05493712014-04-17 17:30:03 -0700237#define LONGDBL 0x0008 /* long double */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800238#define LONGINT 0x0010 /* long integer */
239#define LLONGINT 0x0020 /* long long integer */
240#define SHORTINT 0x0040 /* short integer */
241#define ZEROPAD 0x0080 /* zero (as opposed to blank) pad */
242#define FPT 0x0100 /* Floating point number */
243#define PTRINT 0x0200 /* (unsigned) ptrdiff_t */
244#define SIZEINT 0x0400 /* (signed) size_t */
245#define CHARINT 0x0800 /* 8 bit integer */
246#define MAXINT 0x1000 /* largest integer size (intmax_t) */
247
248int
249vfprintf(FILE *fp, const char *fmt0, __va_list ap)
250{
Kenny Rootf5823402011-02-12 07:13:44 -0800251 int ret;
252
253 FLOCKFILE(fp);
254 ret = __vfprintf(fp, fmt0, ap);
255 FUNLOCKFILE(fp);
256 return (ret);
257}
Elliott Hughes506c6de2016-01-15 16:30:18 -0800258DEF_STRONG(vfprintf);
Kenny Rootf5823402011-02-12 07:13:44 -0800259
260int
261__vfprintf(FILE *fp, const char *fmt0, __va_list ap)
262{
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400263 char *fmt; /* format string */
264 int ch; /* character from fmt */
Elliott Hughes05493712014-04-17 17:30:03 -0700265 int n, n2; /* handy integers (short term usage) */
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400266 char *cp; /* handy char pointer (short term usage) */
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400267 struct __siov *iovp; /* for PRINT macro */
268 int flags; /* flags as above */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800269 int ret; /* return value accumulator */
270 int width; /* width from format (%8d), or 0 */
Elliott Hughes05493712014-04-17 17:30:03 -0700271 int prec; /* precision from format; <0 for N/A */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800272 char sign; /* sign prefix (' ', '+', '-', or \0) */
273 wchar_t wc;
Elliott Hughes05493712014-04-17 17:30:03 -0700274 mbstate_t ps;
Elliott Hughes05493712014-04-17 17:30:03 -0700275 /*
276 * We can decompose the printed representation of floating
277 * point numbers into several parts, some of which may be empty:
278 *
279 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
280 * A B ---C--- D E F
281 *
282 * A: 'sign' holds this value if present; '\0' otherwise
283 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
284 * C: cp points to the string MMMNNN. Leading and trailing
285 * zeros are not in the string and must be added.
286 * D: expchar holds this character; '\0' if no exponent, e.g. %f
287 * F: at least two digits for decimal, at least one digit for hex
288 */
289 char *decimal_point = NULL;
290 int signflag; /* true if float is negative */
291 union { /* floating point arguments %[aAeEfFgG] */
292 double dbl;
293 long double ldbl;
294 } fparg;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800295 int expt; /* integer value of exponent */
Elliott Hughes05493712014-04-17 17:30:03 -0700296 char expchar; /* exponent character: [eEpP\0] */
297 char *dtoaend; /* pointer to end of converted digits */
298 int expsize; /* character count for expstr */
299 int lead; /* sig figs before decimal or group sep */
300 int ndig; /* actual number of digits returned by dtoa */
301 char expstr[MAXEXPDIG+2]; /* buffer for exponent string: e+ZZZ */
Elliott Hughes5eb67042014-04-11 18:00:37 -0700302 char *dtoaresult = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800303
304 uintmax_t _umax; /* integer arguments %[diouxX] */
Elliott Hughes05493712014-04-17 17:30:03 -0700305 enum { OCT, DEC, HEX } base; /* base for %[diouxX] conversion */
306 int dprec; /* a copy of prec if %[diouxX], 0 otherwise */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800307 int realsz; /* field size expanded by dprec */
308 int size; /* size of converted field or string */
Elliott Hughes05493712014-04-17 17:30:03 -0700309 const char *xdigs; /* digits for %[xX] conversion */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800310#define NIOV 8
311 struct __suio uio; /* output information: summary */
312 struct __siov iov[NIOV];/* ... and individual io vectors */
Elliott Hughes05493712014-04-17 17:30:03 -0700313 char buf[BUF]; /* buffer with space for digits of uintmax_t */
314 char ox[2]; /* space for 0x; ox[1] is either x, X, or \0 */
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400315 union arg *argtable; /* args, built due to positional arg */
316 union arg statargtable[STATIC_ARG_TBL_SIZE];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800317 size_t argtablesiz;
318 int nextarg; /* 1-based argument index */
319 va_list orgap; /* original argument pointer */
Elliott Hughes05493712014-04-17 17:30:03 -0700320 char *convbuf; /* buffer for wide to multi-byte conversion */
Elliott Hughes05493712014-04-17 17:30:03 -0700321
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800322 /*
323 * Choose PADSIZE to trade efficiency vs. size. If larger printf
324 * fields occur frequently, increase PADSIZE and make the initialisers
325 * below longer.
326 */
327#define PADSIZE 16 /* pad chunk size */
Elliott Hughes05493712014-04-17 17:30:03 -0700328 static char blanks[PADSIZE] =
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800329 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
Elliott Hughes05493712014-04-17 17:30:03 -0700330 static char zeroes[PADSIZE] =
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800331 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
332
Elliott Hughes05493712014-04-17 17:30:03 -0700333 static const char xdigs_lower[16] = "0123456789abcdef";
334 static const char xdigs_upper[16] = "0123456789ABCDEF";
335
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800336 /*
337 * BEWARE, these `goto error' on error, and PAD uses `n'.
338 */
339#define PRINT(ptr, len) do { \
340 iovp->iov_base = (ptr); \
341 iovp->iov_len = (len); \
342 uio.uio_resid += (len); \
343 iovp++; \
344 if (++uio.uio_iovcnt >= NIOV) { \
345 if (__sprint(fp, &uio)) \
346 goto error; \
347 iovp = iov; \
348 } \
349} while (0)
350#define PAD(howmany, with) do { \
351 if ((n = (howmany)) > 0) { \
352 while (n > PADSIZE) { \
353 PRINT(with, PADSIZE); \
354 n -= PADSIZE; \
355 } \
356 PRINT(with, n); \
357 } \
358} while (0)
Elliott Hughes05493712014-04-17 17:30:03 -0700359#define PRINTANDPAD(p, ep, len, with) do { \
360 n2 = (ep) - (p); \
361 if (n2 > (len)) \
362 n2 = (len); \
363 if (n2 > 0) \
364 PRINT((p), n2); \
365 PAD((len) - (n2 > 0 ? n2 : 0), (with)); \
366} while(0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800367#define FLUSH() do { \
368 if (uio.uio_resid && __sprint(fp, &uio)) \
369 goto error; \
370 uio.uio_iovcnt = 0; \
371 iovp = iov; \
372} while (0)
373
374 /*
375 * To extend shorts properly, we need both signed and unsigned
376 * argument extraction methods.
377 */
378#define SARG() \
379 ((intmax_t)(flags&MAXINT ? GETARG(intmax_t) : \
380 flags&LLONGINT ? GETARG(long long) : \
381 flags&LONGINT ? GETARG(long) : \
382 flags&PTRINT ? GETARG(ptrdiff_t) : \
383 flags&SIZEINT ? GETARG(ssize_t) : \
384 flags&SHORTINT ? (short)GETARG(int) : \
Elliott Hughesf1ada792014-05-02 17:56:56 -0700385 flags&CHARINT ? (signed char)GETARG(int) : \
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800386 GETARG(int)))
387#define UARG() \
388 ((uintmax_t)(flags&MAXINT ? GETARG(uintmax_t) : \
389 flags&LLONGINT ? GETARG(unsigned long long) : \
390 flags&LONGINT ? GETARG(unsigned long) : \
391 flags&PTRINT ? (uintptr_t)GETARG(ptrdiff_t) : /* XXX */ \
392 flags&SIZEINT ? GETARG(size_t) : \
393 flags&SHORTINT ? (unsigned short)GETARG(int) : \
394 flags&CHARINT ? (unsigned char)GETARG(int) : \
395 GETARG(unsigned int)))
396
Elliott Hughes05493712014-04-17 17:30:03 -0700397 /*
398 * Append a digit to a value and check for overflow.
399 */
400#define APPEND_DIGIT(val, dig) do { \
401 if ((val) > INT_MAX / 10) \
402 goto overflow; \
403 (val) *= 10; \
404 if ((val) > INT_MAX - to_digit((dig))) \
405 goto overflow; \
406 (val) += to_digit((dig)); \
407} while (0)
408
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800409 /*
410 * Get * arguments, including the form *nn$. Preserve the nextarg
411 * that the argument can be gotten once the type is determined.
412 */
413#define GETASTER(val) \
414 n2 = 0; \
415 cp = fmt; \
416 while (is_digit(*cp)) { \
Elliott Hughes05493712014-04-17 17:30:03 -0700417 APPEND_DIGIT(n2, *cp); \
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800418 cp++; \
419 } \
420 if (*cp == '$') { \
421 int hold = nextarg; \
422 if (argtable == NULL) { \
423 argtable = statargtable; \
Elliott Hughes506c6de2016-01-15 16:30:18 -0800424 if (__find_arguments(fmt0, orgap, &argtable, \
425 &argtablesiz) == -1) { \
426 ret = -1; \
427 goto error; \
428 } \
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800429 } \
430 nextarg = n2; \
431 val = GETARG(int); \
432 nextarg = hold; \
433 fmt = ++cp; \
434 } else { \
435 val = GETARG(int); \
436 }
437
438/*
439* Get the argument indexed by nextarg. If the argument table is
440* built, use it to get the argument. If its not, get the next
441* argument (and arguments must be gotten sequentially).
442*/
443#define GETARG(type) \
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400444 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
445 (nextarg++, va_arg(ap, type)))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800446
447 _SET_ORIENTATION(fp, -1);
448 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
449 if (cantwrite(fp)) {
450 errno = EBADF;
451 return (EOF);
452 }
453
454 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
455 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
456 fp->_file >= 0)
457 return (__sbprintf(fp, fmt0, ap));
458
459 fmt = (char *)fmt0;
460 argtable = NULL;
461 nextarg = 1;
462 va_copy(orgap, ap);
463 uio.uio_iov = iovp = iov;
464 uio.uio_resid = 0;
465 uio.uio_iovcnt = 0;
466 ret = 0;
Elliott Hughes05493712014-04-17 17:30:03 -0700467 convbuf = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800468
469 memset(&ps, 0, sizeof(ps));
470 /*
471 * Scan the format for conversions (`%' character).
472 */
473 for (;;) {
474 cp = fmt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800475 while ((n = mbrtowc(&wc, fmt, MB_CUR_MAX, &ps)) > 0) {
476 fmt += n;
477 if (wc == '%') {
478 fmt--;
479 break;
480 }
481 }
Elliott Hughes506c6de2016-01-15 16:30:18 -0800482 if (n < 0) {
483 ret = -1;
484 goto error;
485 }
Elliott Hughes05493712014-04-17 17:30:03 -0700486 if (fmt != cp) {
487 ptrdiff_t m = fmt - cp;
488 if (m < 0 || m > INT_MAX - ret)
489 goto overflow;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800490 PRINT(cp, m);
491 ret += m;
492 }
Elliott Hughes506c6de2016-01-15 16:30:18 -0800493 if (n == 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800494 goto done;
495 fmt++; /* skip over '%' */
496
497 flags = 0;
498 dprec = 0;
499 width = 0;
500 prec = -1;
501 sign = '\0';
Elliott Hughes05493712014-04-17 17:30:03 -0700502 ox[1] = '\0';
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800503
504rflag: ch = *fmt++;
505reswitch: switch (ch) {
506 case ' ':
507 /*
508 * ``If the space and + flags both appear, the space
509 * flag will be ignored.''
510 * -- ANSI X3J11
511 */
512 if (!sign)
513 sign = ' ';
514 goto rflag;
515 case '#':
516 flags |= ALT;
517 goto rflag;
Elliott Hughes05493712014-04-17 17:30:03 -0700518 case '\'':
519 /* grouping not implemented */
520 goto rflag;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800521 case '*':
522 /*
523 * ``A negative field width argument is taken as a
524 * - flag followed by a positive field width.''
525 * -- ANSI X3J11
526 * They don't exclude field widths read from args.
527 */
528 GETASTER(width);
529 if (width >= 0)
530 goto rflag;
Elliott Hughes05493712014-04-17 17:30:03 -0700531 if (width == INT_MIN)
532 goto overflow;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800533 width = -width;
534 /* FALLTHROUGH */
535 case '-':
536 flags |= LADJUST;
537 goto rflag;
538 case '+':
539 sign = '+';
540 goto rflag;
541 case '.':
542 if ((ch = *fmt++) == '*') {
543 GETASTER(n);
544 prec = n < 0 ? -1 : n;
545 goto rflag;
546 }
547 n = 0;
548 while (is_digit(ch)) {
Elliott Hughes05493712014-04-17 17:30:03 -0700549 APPEND_DIGIT(n, ch);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800550 ch = *fmt++;
551 }
552 if (ch == '$') {
553 nextarg = n;
554 if (argtable == NULL) {
555 argtable = statargtable;
Elliott Hughes506c6de2016-01-15 16:30:18 -0800556 if (__find_arguments(fmt0, orgap,
557 &argtable, &argtablesiz) == -1) {
558 ret = -1;
559 goto error;
560 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800561 }
562 goto rflag;
563 }
Elliott Hughes05493712014-04-17 17:30:03 -0700564 prec = n;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800565 goto reswitch;
566 case '0':
567 /*
568 * ``Note that 0 is taken as a flag, not as the
569 * beginning of a field width.''
570 * -- ANSI X3J11
571 */
572 flags |= ZEROPAD;
573 goto rflag;
574 case '1': case '2': case '3': case '4':
575 case '5': case '6': case '7': case '8': case '9':
576 n = 0;
577 do {
Elliott Hughes05493712014-04-17 17:30:03 -0700578 APPEND_DIGIT(n, ch);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800579 ch = *fmt++;
580 } while (is_digit(ch));
581 if (ch == '$') {
582 nextarg = n;
583 if (argtable == NULL) {
584 argtable = statargtable;
Elliott Hughes506c6de2016-01-15 16:30:18 -0800585 if (__find_arguments(fmt0, orgap,
586 &argtable, &argtablesiz) == -1) {
587 ret = -1;
588 goto error;
589 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800590 }
591 goto rflag;
592 }
593 width = n;
594 goto reswitch;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800595 case 'L':
596 flags |= LONGDBL;
597 goto rflag;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800598 case 'h':
Elliott Hughes1d13c642013-09-23 16:02:39 -0700599 if (*fmt == 'h') {
600 fmt++;
601 flags |= CHARINT;
602 } else {
603 flags |= SHORTINT;
604 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800605 goto rflag;
606 case 'j':
607 flags |= MAXINT;
608 goto rflag;
609 case 'l':
610 if (*fmt == 'l') {
611 fmt++;
612 flags |= LLONGINT;
613 } else {
614 flags |= LONGINT;
615 }
616 goto rflag;
617 case 'q':
618 flags |= LLONGINT;
619 goto rflag;
620 case 't':
621 flags |= PTRINT;
622 goto rflag;
623 case 'z':
624 flags |= SIZEINT;
625 goto rflag;
626 case 'c':
Elliott Hughes05493712014-04-17 17:30:03 -0700627 if (flags & LONGINT) {
628 mbstate_t mbs;
629 size_t mbseqlen;
630
631 memset(&mbs, 0, sizeof(mbs));
632 mbseqlen = wcrtomb(buf,
633 (wchar_t)GETARG(wint_t), &mbs);
634 if (mbseqlen == (size_t)-1) {
Elliott Hughes506c6de2016-01-15 16:30:18 -0800635 ret = -1;
Elliott Hughes05493712014-04-17 17:30:03 -0700636 goto error;
637 }
638 cp = buf;
639 size = (int)mbseqlen;
640 } else {
Elliott Hughes05493712014-04-17 17:30:03 -0700641 *(cp = buf) = GETARG(int);
642 size = 1;
Elliott Hughes05493712014-04-17 17:30:03 -0700643 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800644 sign = '\0';
645 break;
646 case 'D':
647 flags |= LONGINT;
648 /*FALLTHROUGH*/
649 case 'd':
650 case 'i':
651 _umax = SARG();
652 if ((intmax_t)_umax < 0) {
653 _umax = -_umax;
654 sign = '-';
655 }
656 base = DEC;
657 goto number;
Elliott Hughes05493712014-04-17 17:30:03 -0700658 case 'a':
659 case 'A':
660 if (ch == 'a') {
661 ox[1] = 'x';
662 xdigs = xdigs_lower;
663 expchar = 'p';
664 } else {
665 ox[1] = 'X';
666 xdigs = xdigs_upper;
667 expchar = 'P';
668 }
669 if (prec >= 0)
670 prec++;
671 if (dtoaresult)
672 __freedtoa(dtoaresult);
673 if (flags & LONGDBL) {
674 fparg.ldbl = GETARG(long double);
675 dtoaresult = cp =
676 __hldtoa(fparg.ldbl, xdigs, prec,
677 &expt, &signflag, &dtoaend);
678 if (dtoaresult == NULL) {
679 errno = ENOMEM;
680 goto error;
681 }
682 } else {
683 fparg.dbl = GETARG(double);
684 dtoaresult = cp =
685 __hdtoa(fparg.dbl, xdigs, prec,
686 &expt, &signflag, &dtoaend);
687 if (dtoaresult == NULL) {
688 errno = ENOMEM;
689 goto error;
690 }
691 }
692 if (prec < 0)
693 prec = dtoaend - cp;
694 if (expt == INT_MAX)
695 ox[1] = '\0';
696 goto fp_common;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800697 case 'e':
698 case 'E':
Elliott Hughes05493712014-04-17 17:30:03 -0700699 expchar = ch;
700 if (prec < 0) /* account for digit before decpt */
701 prec = DEFPREC + 1;
702 else
703 prec++;
704 goto fp_begin;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800705 case 'f':
Elliott Hughes05493712014-04-17 17:30:03 -0700706 case 'F':
707 expchar = '\0';
708 goto fp_begin;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800709 case 'g':
710 case 'G':
Elliott Hughes05493712014-04-17 17:30:03 -0700711 expchar = ch - ('g' - 'e');
712 if (prec == 0)
713 prec = 1;
714fp_begin:
715 if (prec < 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800716 prec = DEFPREC;
Elliott Hughes05493712014-04-17 17:30:03 -0700717 if (dtoaresult)
718 __freedtoa(dtoaresult);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800719 if (flags & LONGDBL) {
Elliott Hughes05493712014-04-17 17:30:03 -0700720 fparg.ldbl = GETARG(long double);
721 dtoaresult = cp =
722 __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec,
723 &expt, &signflag, &dtoaend);
724 if (dtoaresult == NULL) {
725 errno = ENOMEM;
726 goto error;
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400727 }
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400728 } else {
Elliott Hughes05493712014-04-17 17:30:03 -0700729 fparg.dbl = GETARG(double);
730 dtoaresult = cp =
731 __dtoa(fparg.dbl, expchar ? 2 : 3, prec,
732 &expt, &signflag, &dtoaend);
733 if (dtoaresult == NULL) {
734 errno = ENOMEM;
735 goto error;
736 }
737 if (expt == 9999)
738 expt = INT_MAX;
739 }
740fp_common:
741 if (signflag)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800742 sign = '-';
Elliott Hughes05493712014-04-17 17:30:03 -0700743 if (expt == INT_MAX) { /* inf or nan */
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800744 if (*cp == 'N')
Elliott Hughes05493712014-04-17 17:30:03 -0700745 cp = (ch >= 'a') ? "nan" : "NAN";
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800746 else
Elliott Hughes05493712014-04-17 17:30:03 -0700747 cp = (ch >= 'a') ? "inf" : "INF";
748 size = 3;
749 flags &= ~ZEROPAD;
750 break;
751 }
752 flags |= FPT;
753 ndig = dtoaend - cp;
754 if (ch == 'g' || ch == 'G') {
755 if (expt > -4 && expt <= prec) {
756 /* Make %[gG] smell like %[fF] */
757 expchar = '\0';
758 if (flags & ALT)
759 prec -= expt;
760 else
761 prec = ndig - expt;
762 if (prec < 0)
763 prec = 0;
764 } else {
765 /*
766 * Make %[gG] smell like %[eE], but
767 * trim trailing zeroes if no # flag.
768 */
769 if (!(flags & ALT))
770 prec = ndig;
771 }
772 }
773 if (expchar) {
774 expsize = exponent(expstr, expt - 1, expchar);
775 size = expsize + prec;
776 if (prec > 1 || flags & ALT)
777 ++size;
778 } else {
779 /* space for digits before decimal point */
780 if (expt > 0)
781 size = expt;
782 else /* "0" */
783 size = 1;
784 /* space for decimal pt and following digits */
785 if (prec || flags & ALT)
786 size += prec + 1;
787 lead = expt;
788 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800789 break;
Elliott Hughese2341d02014-05-02 18:16:32 -0700790#ifndef NO_PRINTF_PERCENT_N
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800791 case 'n':
792 if (flags & LLONGINT)
793 *GETARG(long long *) = ret;
794 else if (flags & LONGINT)
795 *GETARG(long *) = ret;
796 else if (flags & SHORTINT)
797 *GETARG(short *) = ret;
798 else if (flags & CHARINT)
Elliott Hughesf1ada792014-05-02 17:56:56 -0700799 *GETARG(signed char *) = ret;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800800 else if (flags & PTRINT)
801 *GETARG(ptrdiff_t *) = ret;
802 else if (flags & SIZEINT)
803 *GETARG(ssize_t *) = ret;
804 else if (flags & MAXINT)
805 *GETARG(intmax_t *) = ret;
806 else
807 *GETARG(int *) = ret;
808 continue; /* no output */
Elliott Hughese2341d02014-05-02 18:16:32 -0700809#endif /* NO_PRINTF_PERCENT_N */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800810 case 'O':
811 flags |= LONGINT;
812 /*FALLTHROUGH*/
813 case 'o':
814 _umax = UARG();
815 base = OCT;
816 goto nosign;
817 case 'p':
818 /*
819 * ``The argument shall be a pointer to void. The
820 * value of the pointer is converted to a sequence
821 * of printable characters, in an implementation-
822 * defined manner.''
823 * -- ANSI X3J11
824 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800825 _umax = (u_long)GETARG(void *);
826 base = HEX;
Elliott Hughes05493712014-04-17 17:30:03 -0700827 xdigs = xdigs_lower;
828 ox[1] = 'x';
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800829 goto nosign;
830 case 's':
Elliott Hughes05493712014-04-17 17:30:03 -0700831 if (flags & LONGINT) {
832 wchar_t *wcp;
833
Elliott Hughes506c6de2016-01-15 16:30:18 -0800834 free(convbuf);
835 convbuf = NULL;
Elliott Hughes05493712014-04-17 17:30:03 -0700836 if ((wcp = GETARG(wchar_t *)) == NULL) {
837 cp = "(null)";
838 } else {
839 convbuf = __wcsconv(wcp, prec);
840 if (convbuf == NULL) {
Elliott Hughes506c6de2016-01-15 16:30:18 -0800841 ret = -1;
Elliott Hughes05493712014-04-17 17:30:03 -0700842 goto error;
843 }
844 cp = convbuf;
845 }
846 } else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800847 if ((cp = GETARG(char *)) == NULL)
848 cp = "(null)";
849 if (prec >= 0) {
850 /*
851 * can't use strlen; can only look for the
852 * NUL in the first `prec' characters, and
853 * strlen() will go further.
854 */
855 char *p = memchr(cp, 0, prec);
856
Elliott Hughes05493712014-04-17 17:30:03 -0700857 size = p ? (p - cp) : prec;
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400858 } else {
Elliott Hughes05493712014-04-17 17:30:03 -0700859 size_t len;
860
861 if ((len = strlen(cp)) > INT_MAX)
862 goto overflow;
863 size = (int)len;
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400864 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800865 sign = '\0';
866 break;
867 case 'U':
868 flags |= LONGINT;
869 /*FALLTHROUGH*/
870 case 'u':
871 _umax = UARG();
872 base = DEC;
873 goto nosign;
874 case 'X':
Elliott Hughes05493712014-04-17 17:30:03 -0700875 xdigs = xdigs_upper;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800876 goto hex;
877 case 'x':
Elliott Hughes05493712014-04-17 17:30:03 -0700878 xdigs = xdigs_lower;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800879hex: _umax = UARG();
880 base = HEX;
881 /* leading 0x/X only if non-zero */
882 if (flags & ALT && _umax != 0)
Elliott Hughes05493712014-04-17 17:30:03 -0700883 ox[1] = ch;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800884
885 /* unsigned conversions */
886nosign: sign = '\0';
887 /*
888 * ``... diouXx conversions ... if a precision is
889 * specified, the 0 flag will be ignored.''
890 * -- ANSI X3J11
891 */
892number: if ((dprec = prec) >= 0)
893 flags &= ~ZEROPAD;
894
895 /*
896 * ``The result of converting a zero value with an
897 * explicit precision of zero is no characters.''
898 * -- ANSI X3J11
899 */
900 cp = buf + BUF;
901 if (_umax != 0 || prec != 0) {
902 /*
903 * Unsigned mod is hard, and unsigned mod
904 * by a constant is easier than that by
905 * a variable; hence this switch.
906 */
907 switch (base) {
908 case OCT:
909 do {
910 *--cp = to_char(_umax & 7);
911 _umax >>= 3;
912 } while (_umax);
913 /* handle octal leading 0 */
914 if (flags & ALT && *cp != '0')
915 *--cp = '0';
916 break;
917
918 case DEC:
919 /* many numbers are 1 digit */
920 while (_umax >= 10) {
921 *--cp = to_char(_umax % 10);
922 _umax /= 10;
923 }
924 *--cp = to_char(_umax);
925 break;
926
927 case HEX:
928 do {
929 *--cp = xdigs[_umax & 15];
930 _umax >>= 4;
931 } while (_umax);
932 break;
933
934 default:
935 cp = "bug in vfprintf: bad base";
936 size = strlen(cp);
937 goto skipsize;
938 }
939 }
940 size = buf + BUF - cp;
Elliott Hughes05493712014-04-17 17:30:03 -0700941 if (size > BUF) /* should never happen */
942 abort();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800943 skipsize:
944 break;
945 default: /* "%?" prints ?, unless ? is NUL */
946 if (ch == '\0')
947 goto done;
948 /* pretend it was %c with argument ch */
949 cp = buf;
950 *cp = ch;
951 size = 1;
952 sign = '\0';
953 break;
954 }
955
956 /*
957 * All reasonable formats wind up here. At this point, `cp'
958 * points to a string which (if not flags&LADJUST) should be
959 * padded out to `width' places. If flags&ZEROPAD, it should
960 * first be prefixed by any sign or other prefix; otherwise,
961 * it should be blank padded before the prefix is emitted.
962 * After any left-hand padding and prefixing, emit zeroes
Elliott Hughes05493712014-04-17 17:30:03 -0700963 * required by a decimal %[diouxX] precision, then print the
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800964 * string proper, then emit zeroes required by any leftover
965 * floating precision; finally, if LADJUST, pad with blanks.
966 *
967 * Compute actual size, so we know how much to pad.
968 * size excludes decimal prec; realsz includes it.
969 */
970 realsz = dprec > size ? dprec : size;
971 if (sign)
972 realsz++;
Elliott Hughes05493712014-04-17 17:30:03 -0700973 if (ox[1])
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800974 realsz+= 2;
975
976 /* right-adjusting blank padding */
977 if ((flags & (LADJUST|ZEROPAD)) == 0)
978 PAD(width - realsz, blanks);
979
980 /* prefix */
Elliott Hughes05493712014-04-17 17:30:03 -0700981 if (sign)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800982 PRINT(&sign, 1);
Elliott Hughes05493712014-04-17 17:30:03 -0700983 if (ox[1]) { /* ox[1] is either x, X, or \0 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800984 ox[0] = '0';
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800985 PRINT(ox, 2);
986 }
987
988 /* right-adjusting zero padding */
989 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
990 PAD(width - realsz, zeroes);
991
992 /* leading zeroes from decimal precision */
993 PAD(dprec - size, zeroes);
994
995 /* the string or number proper */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800996 if ((flags & FPT) == 0) {
997 PRINT(cp, size);
998 } else { /* glue together f_p fragments */
Elliott Hughes05493712014-04-17 17:30:03 -0700999 if (decimal_point == NULL)
1000 decimal_point = nl_langinfo(RADIXCHAR);
1001 if (!expchar) { /* %[fF] or sufficiently short %[gG] */
1002 if (expt <= 0) {
1003 PRINT(zeroes, 1);
1004 if (prec || flags & ALT)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001005 PRINT(decimal_point, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001006 PAD(-expt, zeroes);
Elliott Hughes05493712014-04-17 17:30:03 -07001007 /* already handled initial 0's */
1008 prec += expt;
1009 } else {
1010 PRINTANDPAD(cp, dtoaend, lead, zeroes);
1011 cp += lead;
1012 if (prec || flags & ALT)
1013 PRINT(decimal_point, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001014 }
Elliott Hughes05493712014-04-17 17:30:03 -07001015 PRINTANDPAD(cp, dtoaend, prec, zeroes);
1016 } else { /* %[eE] or sufficiently long %[gG] */
1017 if (prec > 1 || flags & ALT) {
1018 buf[0] = *cp++;
1019 buf[1] = *decimal_point;
1020 PRINT(buf, 2);
1021 PRINT(cp, ndig-1);
1022 PAD(prec - ndig, zeroes);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001023 } else { /* XeYYY */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001024 PRINT(cp, 1);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001025 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001026 PRINT(expstr, expsize);
1027 }
1028 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001029 /* left-adjusting padding (always blank) */
1030 if (flags & LADJUST)
1031 PAD(width - realsz, blanks);
1032
1033 /* finally, adjust ret */
Elliott Hughes05493712014-04-17 17:30:03 -07001034 if (width < realsz)
1035 width = realsz;
1036 if (width > INT_MAX - ret)
1037 goto overflow;
1038 ret += width;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001039
1040 FLUSH(); /* copy out the I/O vectors */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001041 }
1042done:
1043 FLUSH();
1044error:
Elliott Hughes05493712014-04-17 17:30:03 -07001045 va_end(orgap);
1046 if (__sferror(fp))
1047 ret = -1;
1048 goto finish;
1049
1050overflow:
1051 errno = ENOMEM;
1052 ret = -1;
1053
1054finish:
Elliott Hughes506c6de2016-01-15 16:30:18 -08001055 free(convbuf);
Elliott Hughes05493712014-04-17 17:30:03 -07001056 if (dtoaresult)
Elliott Hughes4bd97ce2014-04-10 17:48:14 -07001057 __freedtoa(dtoaresult);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001058 if (argtable != NULL && argtable != statargtable) {
1059 munmap(argtable, argtablesiz);
1060 argtable = NULL;
1061 }
Elliott Hughes05493712014-04-17 17:30:03 -07001062 return (ret);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001063}
1064
1065/*
1066 * Type ids for argument type table.
1067 */
1068#define T_UNUSED 0
1069#define T_SHORT 1
1070#define T_U_SHORT 2
1071#define TP_SHORT 3
1072#define T_INT 4
1073#define T_U_INT 5
1074#define TP_INT 6
1075#define T_LONG 7
1076#define T_U_LONG 8
1077#define TP_LONG 9
1078#define T_LLONG 10
1079#define T_U_LLONG 11
1080#define TP_LLONG 12
1081#define T_DOUBLE 13
1082#define T_LONG_DOUBLE 14
1083#define TP_CHAR 15
1084#define TP_VOID 16
1085#define T_PTRINT 17
1086#define TP_PTRINT 18
1087#define T_SIZEINT 19
1088#define T_SSIZEINT 20
1089#define TP_SSIZEINT 21
1090#define T_MAXINT 22
1091#define T_MAXUINT 23
1092#define TP_MAXINT 24
Elliott Hughes05493712014-04-17 17:30:03 -07001093#define T_CHAR 25
1094#define T_U_CHAR 26
1095#define T_WINT 27
1096#define TP_WCHAR 28
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001097
1098/*
1099 * Find all arguments when a positional parameter is encountered. Returns a
1100 * table, indexed by argument number, of pointers to each arguments. The
1101 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
1102 * It will be replaced with a mmap-ed one if it overflows (malloc cannot be
1103 * used since we are attempting to make snprintf thread safe, and alloca is
1104 * problematic since we have nested functions..)
1105 */
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001106static int
1107__find_arguments(const char *fmt0, va_list ap, union arg **argtable,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001108 size_t *argtablesiz)
1109{
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001110 char *fmt; /* format string */
1111 int ch; /* character from fmt */
1112 int n, n2; /* handy integer (short term usage) */
1113 char *cp; /* handy char pointer (short term usage) */
1114 int flags; /* flags as above */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001115 unsigned char *typetable; /* table of types */
1116 unsigned char stattypetable[STATIC_ARG_TBL_SIZE];
1117 int tablesize; /* current size of type table */
1118 int tablemax; /* largest used index in table */
1119 int nextarg; /* 1-based argument index */
Elliott Hughes05493712014-04-17 17:30:03 -07001120 int ret = 0; /* return value */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001121 wchar_t wc;
Elliott Hughes05493712014-04-17 17:30:03 -07001122 mbstate_t ps;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001123
1124 /*
1125 * Add an argument type to the table, expanding if necessary.
1126 */
1127#define ADDTYPE(type) \
1128 ((nextarg >= tablesize) ? \
1129 __grow_type_table(&typetable, &tablesize) : 0, \
1130 (nextarg > tablemax) ? tablemax = nextarg : 0, \
1131 typetable[nextarg++] = type)
1132
1133#define ADDSARG() \
1134 ((flags&MAXINT) ? ADDTYPE(T_MAXINT) : \
1135 ((flags&PTRINT) ? ADDTYPE(T_PTRINT) : \
1136 ((flags&SIZEINT) ? ADDTYPE(T_SSIZEINT) : \
1137 ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \
1138 ((flags&LONGINT) ? ADDTYPE(T_LONG) : \
Elliott Hughes05493712014-04-17 17:30:03 -07001139 ((flags&SHORTINT) ? ADDTYPE(T_SHORT) : \
1140 ((flags&CHARINT) ? ADDTYPE(T_CHAR) : ADDTYPE(T_INT))))))))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001141
1142#define ADDUARG() \
1143 ((flags&MAXINT) ? ADDTYPE(T_MAXUINT) : \
1144 ((flags&PTRINT) ? ADDTYPE(T_PTRINT) : \
1145 ((flags&SIZEINT) ? ADDTYPE(T_SIZEINT) : \
1146 ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \
1147 ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : \
Elliott Hughes05493712014-04-17 17:30:03 -07001148 ((flags&SHORTINT) ? ADDTYPE(T_U_SHORT) : \
1149 ((flags&CHARINT) ? ADDTYPE(T_U_CHAR) : ADDTYPE(T_U_INT))))))))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001150
1151 /*
1152 * Add * arguments to the type array.
1153 */
1154#define ADDASTER() \
1155 n2 = 0; \
1156 cp = fmt; \
1157 while (is_digit(*cp)) { \
Elliott Hughes05493712014-04-17 17:30:03 -07001158 APPEND_DIGIT(n2, *cp); \
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001159 cp++; \
1160 } \
1161 if (*cp == '$') { \
1162 int hold = nextarg; \
1163 nextarg = n2; \
1164 ADDTYPE(T_INT); \
1165 nextarg = hold; \
1166 fmt = ++cp; \
1167 } else { \
1168 ADDTYPE(T_INT); \
1169 }
1170 fmt = (char *)fmt0;
1171 typetable = stattypetable;
1172 tablesize = STATIC_ARG_TBL_SIZE;
1173 tablemax = 0;
1174 nextarg = 1;
1175 memset(typetable, T_UNUSED, STATIC_ARG_TBL_SIZE);
1176 memset(&ps, 0, sizeof(ps));
1177
1178 /*
1179 * Scan the format for conversions (`%' character).
1180 */
1181 for (;;) {
1182 cp = fmt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001183 while ((n = mbrtowc(&wc, fmt, MB_CUR_MAX, &ps)) > 0) {
1184 fmt += n;
1185 if (wc == '%') {
1186 fmt--;
1187 break;
1188 }
1189 }
Elliott Hughes506c6de2016-01-15 16:30:18 -08001190 if (n < 0)
1191 return (-1);
1192 if (n == 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001193 goto done;
1194 fmt++; /* skip over '%' */
1195
1196 flags = 0;
1197
1198rflag: ch = *fmt++;
1199reswitch: switch (ch) {
1200 case ' ':
1201 case '#':
Elliott Hughes05493712014-04-17 17:30:03 -07001202 case '\'':
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001203 goto rflag;
1204 case '*':
1205 ADDASTER();
1206 goto rflag;
1207 case '-':
1208 case '+':
1209 goto rflag;
1210 case '.':
1211 if ((ch = *fmt++) == '*') {
1212 ADDASTER();
1213 goto rflag;
1214 }
1215 while (is_digit(ch)) {
1216 ch = *fmt++;
1217 }
1218 goto reswitch;
1219 case '0':
1220 goto rflag;
1221 case '1': case '2': case '3': case '4':
1222 case '5': case '6': case '7': case '8': case '9':
1223 n = 0;
1224 do {
Elliott Hughes05493712014-04-17 17:30:03 -07001225 APPEND_DIGIT(n ,ch);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001226 ch = *fmt++;
1227 } while (is_digit(ch));
1228 if (ch == '$') {
1229 nextarg = n;
1230 goto rflag;
1231 }
1232 goto reswitch;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001233 case 'L':
1234 flags |= LONGDBL;
1235 goto rflag;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001236 case 'h':
1237 if (*fmt == 'h') {
1238 fmt++;
1239 flags |= CHARINT;
1240 } else {
1241 flags |= SHORTINT;
1242 }
1243 goto rflag;
Elliott Hughes05493712014-04-17 17:30:03 -07001244 case 'j':
1245 flags |= MAXINT;
1246 goto rflag;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001247 case 'l':
1248 if (*fmt == 'l') {
1249 fmt++;
1250 flags |= LLONGINT;
1251 } else {
1252 flags |= LONGINT;
1253 }
1254 goto rflag;
1255 case 'q':
1256 flags |= LLONGINT;
1257 goto rflag;
1258 case 't':
1259 flags |= PTRINT;
1260 goto rflag;
1261 case 'z':
1262 flags |= SIZEINT;
1263 goto rflag;
1264 case 'c':
Elliott Hughes05493712014-04-17 17:30:03 -07001265 if (flags & LONGINT)
1266 ADDTYPE(T_WINT);
1267 else
Elliott Hughes05493712014-04-17 17:30:03 -07001268 ADDTYPE(T_INT);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001269 break;
1270 case 'D':
1271 flags |= LONGINT;
1272 /*FALLTHROUGH*/
1273 case 'd':
1274 case 'i':
1275 ADDSARG();
1276 break;
Elliott Hughes05493712014-04-17 17:30:03 -07001277 case 'a':
1278 case 'A':
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001279 case 'e':
1280 case 'E':
1281 case 'f':
Elliott Hughes05493712014-04-17 17:30:03 -07001282 case 'F':
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001283 case 'g':
1284 case 'G':
1285 if (flags & LONGDBL)
1286 ADDTYPE(T_LONG_DOUBLE);
1287 else
1288 ADDTYPE(T_DOUBLE);
1289 break;
Elliott Hughese2341d02014-05-02 18:16:32 -07001290#ifndef NO_PRINTF_PERCENT_N
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001291 case 'n':
1292 if (flags & LLONGINT)
1293 ADDTYPE(TP_LLONG);
1294 else if (flags & LONGINT)
1295 ADDTYPE(TP_LONG);
1296 else if (flags & SHORTINT)
1297 ADDTYPE(TP_SHORT);
1298 else if (flags & PTRINT)
1299 ADDTYPE(TP_PTRINT);
1300 else if (flags & SIZEINT)
1301 ADDTYPE(TP_SSIZEINT);
1302 else if (flags & MAXINT)
1303 ADDTYPE(TP_MAXINT);
1304 else
1305 ADDTYPE(TP_INT);
1306 continue; /* no output */
Elliott Hughese2341d02014-05-02 18:16:32 -07001307#endif /* NO_PRINTF_PERCENT_N */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001308 case 'O':
1309 flags |= LONGINT;
1310 /*FALLTHROUGH*/
1311 case 'o':
1312 ADDUARG();
1313 break;
1314 case 'p':
1315 ADDTYPE(TP_VOID);
1316 break;
1317 case 's':
Elliott Hughes05493712014-04-17 17:30:03 -07001318 if (flags & LONGINT)
1319 ADDTYPE(TP_WCHAR);
1320 else
Elliott Hughes05493712014-04-17 17:30:03 -07001321 ADDTYPE(TP_CHAR);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001322 break;
1323 case 'U':
1324 flags |= LONGINT;
1325 /*FALLTHROUGH*/
1326 case 'u':
1327 case 'X':
1328 case 'x':
1329 ADDUARG();
1330 break;
1331 default: /* "%?" prints ?, unless ? is NUL */
1332 if (ch == '\0')
1333 goto done;
1334 break;
1335 }
1336 }
1337done:
1338 /*
1339 * Build the argument table.
1340 */
1341 if (tablemax >= STATIC_ARG_TBL_SIZE) {
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001342 *argtablesiz = sizeof(union arg) * (tablemax + 1);
1343 *argtable = mmap(NULL, *argtablesiz,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001344 PROT_WRITE|PROT_READ, MAP_ANON|MAP_PRIVATE, -1, 0);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001345 if (*argtable == MAP_FAILED)
1346 return (-1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001347 }
1348
1349#if 0
1350 /* XXX is this required? */
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001351 (*argtable)[0].intarg = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001352#endif
1353 for (n = 1; n <= tablemax; n++) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001354 switch (typetable[n]) {
1355 case T_UNUSED:
Elliott Hughes05493712014-04-17 17:30:03 -07001356 case T_CHAR:
1357 case T_U_CHAR:
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001358 case T_SHORT:
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001359 case T_U_SHORT:
Elliott Hughes05493712014-04-17 17:30:03 -07001360 case T_INT:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001361 (*argtable)[n].intarg = va_arg(ap, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001362 break;
1363 case TP_SHORT:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001364 (*argtable)[n].pshortarg = va_arg(ap, short *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001365 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001366 case T_U_INT:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001367 (*argtable)[n].uintarg = va_arg(ap, unsigned int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001368 break;
1369 case TP_INT:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001370 (*argtable)[n].pintarg = va_arg(ap, int *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001371 break;
1372 case T_LONG:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001373 (*argtable)[n].longarg = va_arg(ap, long);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001374 break;
1375 case T_U_LONG:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001376 (*argtable)[n].ulongarg = va_arg(ap, unsigned long);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001377 break;
1378 case TP_LONG:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001379 (*argtable)[n].plongarg = va_arg(ap, long *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001380 break;
1381 case T_LLONG:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001382 (*argtable)[n].longlongarg = va_arg(ap, long long);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001383 break;
1384 case T_U_LLONG:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001385 (*argtable)[n].ulonglongarg = va_arg(ap, unsigned long long);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001386 break;
1387 case TP_LLONG:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001388 (*argtable)[n].plonglongarg = va_arg(ap, long long *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001389 break;
1390 case T_DOUBLE:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001391 (*argtable)[n].doublearg = va_arg(ap, double);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001392 break;
1393 case T_LONG_DOUBLE:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001394 (*argtable)[n].longdoublearg = va_arg(ap, long double);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001395 break;
1396 case TP_CHAR:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001397 (*argtable)[n].pchararg = va_arg(ap, char *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001398 break;
1399 case TP_VOID:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001400 (*argtable)[n].pvoidarg = va_arg(ap, void *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001401 break;
1402 case T_PTRINT:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001403 (*argtable)[n].ptrdiffarg = va_arg(ap, ptrdiff_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001404 break;
1405 case TP_PTRINT:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001406 (*argtable)[n].pptrdiffarg = va_arg(ap, ptrdiff_t *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001407 break;
1408 case T_SIZEINT:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001409 (*argtable)[n].sizearg = va_arg(ap, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001410 break;
1411 case T_SSIZEINT:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001412 (*argtable)[n].ssizearg = va_arg(ap, ssize_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001413 break;
1414 case TP_SSIZEINT:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001415 (*argtable)[n].pssizearg = va_arg(ap, ssize_t *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001416 break;
Elliott Hughes05493712014-04-17 17:30:03 -07001417 case T_MAXINT:
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001418 (*argtable)[n].intmaxarg = va_arg(ap, intmax_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001419 break;
Elliott Hughes05493712014-04-17 17:30:03 -07001420 case T_MAXUINT:
1421 (*argtable)[n].uintmaxarg = va_arg(ap, uintmax_t);
1422 break;
1423 case TP_MAXINT:
1424 (*argtable)[n].pintmaxarg = va_arg(ap, intmax_t *);
1425 break;
Elliott Hughes05493712014-04-17 17:30:03 -07001426 case T_WINT:
1427 (*argtable)[n].wintarg = va_arg(ap, wint_t);
1428 break;
1429 case TP_WCHAR:
1430 (*argtable)[n].pwchararg = va_arg(ap, wchar_t *);
1431 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001432 }
1433 }
Elliott Hughes05493712014-04-17 17:30:03 -07001434 goto finish;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001435
Elliott Hughes05493712014-04-17 17:30:03 -07001436overflow:
1437 errno = ENOMEM;
1438 ret = -1;
1439
1440finish:
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001441 if (typetable != NULL && typetable != stattypetable) {
1442 munmap(typetable, *argtablesiz);
1443 typetable = NULL;
1444 }
Elliott Hughes05493712014-04-17 17:30:03 -07001445 return (ret);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001446}
1447
1448/*
1449 * Increase the size of the type table.
1450 */
1451static int
1452__grow_type_table(unsigned char **typetable, int *tablesize)
1453{
1454 unsigned char *oldtable = *typetable;
1455 int newsize = *tablesize * 2;
1456
Elliott Hughes05493712014-04-17 17:30:03 -07001457 if (newsize < getpagesize())
1458 newsize = getpagesize();
1459
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001460 if (*tablesize == STATIC_ARG_TBL_SIZE) {
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001461 *typetable = mmap(NULL, newsize, PROT_WRITE|PROT_READ,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001462 MAP_ANON|MAP_PRIVATE, -1, 0);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001463 if (*typetable == MAP_FAILED)
1464 return (-1);
Elliott Hughes05493712014-04-17 17:30:03 -07001465 bcopy(oldtable, *typetable, *tablesize);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001466 } else {
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001467 unsigned char *new = mmap(NULL, newsize, PROT_WRITE|PROT_READ,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001468 MAP_ANON|MAP_PRIVATE, -1, 0);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001469 if (new == MAP_FAILED)
1470 return (-1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001471 memmove(new, *typetable, *tablesize);
1472 munmap(*typetable, *tablesize);
1473 *typetable = new;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001474 }
1475 memset(*typetable + *tablesize, T_UNUSED, (newsize - *tablesize));
1476
1477 *tablesize = newsize;
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001478 return (0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001479}
1480
Elliott Hughes05493712014-04-17 17:30:03 -07001481
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001482static int
1483exponent(char *p0, int exp, int fmtch)
1484{
1485 char *p, *t;
Elliott Hughes05493712014-04-17 17:30:03 -07001486 char expbuf[MAXEXPDIG];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001487
1488 p = p0;
1489 *p++ = fmtch;
1490 if (exp < 0) {
1491 exp = -exp;
1492 *p++ = '-';
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001493 } else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001494 *p++ = '+';
Elliott Hughes05493712014-04-17 17:30:03 -07001495 t = expbuf + MAXEXPDIG;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001496 if (exp > 9) {
1497 do {
1498 *--t = to_char(exp % 10);
1499 } while ((exp /= 10) > 9);
1500 *--t = to_char(exp);
Elliott Hughes05493712014-04-17 17:30:03 -07001501 for (; t < expbuf + MAXEXPDIG; *p++ = *t++)
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +04001502 /* nothing */;
1503 } else {
Elliott Hughes05493712014-04-17 17:30:03 -07001504 /*
1505 * Exponents for decimal floating point conversions
1506 * (%[eEgG]) must be at least two characters long,
1507 * whereas exponents for hexadecimal conversions can
1508 * be only one character long.
1509 */
1510 if (fmtch == 'e' || fmtch == 'E')
1511 *p++ = '0';
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001512 *p++ = to_char(exp);
1513 }
1514 return (p - p0);
1515}