blob: a60b862ba87fe59be4bd0630907009f15285e86a [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
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -070034#define CHAR_TYPE char
Elliott Hughes93a1f8b2017-11-07 22:52:29 -080035#define FUNCTION_NAME __vfprintf
Elliott Hughes1f493172017-11-08 16:13:18 -080036#include "printf_common.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080037
38/*
39 * Flush out all the vectors defined by the given uio,
40 * then reset it so that it can be reused.
41 */
Elliott Hughesc8f2c522017-10-31 13:07:51 -070042static int __sprint(FILE* fp, struct __suio* uio) {
43 int err;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080044
Elliott Hughesc8f2c522017-10-31 13:07:51 -070045 if (uio->uio_resid == 0) {
46 uio->uio_iovcnt = 0;
47 return (0);
48 }
49 err = __sfvwrite(fp, uio);
50 uio->uio_resid = 0;
51 uio->uio_iovcnt = 0;
52 return (err);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080053}
54
55/*
Elliott Hughes05493712014-04-17 17:30:03 -070056 * Convert a wide character string argument for the %ls format to a multibyte
57 * string representation. If not -1, prec specifies the maximum number of
58 * bytes to output, and also means that we can't assume that the wide char
59 * string is null-terminated.
60 */
Elliott Hughesc8f2c522017-10-31 13:07:51 -070061static char* __wcsconv(wchar_t* wcsarg, int prec) {
62 mbstate_t mbs;
63 char buf[MB_LEN_MAX];
64 wchar_t* p;
65 char* convbuf;
66 size_t clen, nbytes;
Elliott Hughes05493712014-04-17 17:30:03 -070067
Elliott Hughesc8f2c522017-10-31 13:07:51 -070068 /* Allocate space for the maximum number of bytes we could output. */
69 if (prec < 0) {
70 memset(&mbs, 0, sizeof(mbs));
71 p = wcsarg;
72 nbytes = wcsrtombs(NULL, (const wchar_t**)&p, 0, &mbs);
73 if (nbytes == (size_t)-1) return (NULL);
74 } else {
75 /*
76 * Optimisation: if the output precision is small enough,
77 * just allocate enough memory for the maximum instead of
78 * scanning the string.
79 */
80 if (prec < 128)
81 nbytes = prec;
82 else {
83 nbytes = 0;
84 p = wcsarg;
85 memset(&mbs, 0, sizeof(mbs));
86 for (;;) {
87 clen = wcrtomb(buf, *p++, &mbs);
88 if (clen == 0 || clen == (size_t)-1 || nbytes + clen > (size_t)prec) break;
89 nbytes += clen;
90 }
91 if (clen == (size_t)-1) return (NULL);
92 }
93 }
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -070094 if ((convbuf = static_cast<char*>(malloc(nbytes + 1))) == NULL) return NULL;
Elliott Hughes05493712014-04-17 17:30:03 -070095
Elliott Hughesc8f2c522017-10-31 13:07:51 -070096 /* Fill the output buffer. */
97 p = wcsarg;
98 memset(&mbs, 0, sizeof(mbs));
99 if ((nbytes = wcsrtombs(convbuf, (const wchar_t**)&p, nbytes, &mbs)) == (size_t)-1) {
100 free(convbuf);
101 return (NULL);
102 }
103 convbuf[nbytes] = '\0';
104 return (convbuf);
Elliott Hughes05493712014-04-17 17:30:03 -0700105}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800106
Elliott Hughes1f493172017-11-08 16:13:18 -0800107int FUNCTION_NAME(FILE* fp, const CHAR_TYPE* fmt0, va_list ap) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700108 int ch; /* character from fmt */
109 int n, n2; /* handy integers (short term usage) */
Elliott Hughes93a1f8b2017-11-07 22:52:29 -0800110 CHAR_TYPE* cp; /* handy char pointer (short term usage) */
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700111 struct __siov* iovp; /* for PRINT macro */
Elliott Hughes93a1f8b2017-11-07 22:52:29 -0800112 CHAR_TYPE sign; /* sign prefix (' ', '+', '-', or \0) */
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700113 int flags; /* flags as above */
114 int ret; /* return value accumulator */
115 int width; /* width from format (%8d), or 0 */
116 int prec; /* precision from format; <0 for N/A */
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700117 /*
118 * We can decompose the printed representation of floating
119 * point numbers into several parts, some of which may be empty:
120 *
121 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
122 * A B ---C--- D E F
123 *
124 * A: 'sign' holds this value if present; '\0' otherwise
125 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
126 * C: cp points to the string MMMNNN. Leading and trailing
127 * zeros are not in the string and must be added.
128 * D: expchar holds this character; '\0' if no exponent, e.g. %f
129 * F: at least two digits for decimal, at least one digit for hex
130 */
131 char* decimal_point = NULL;
132 int signflag; /* true if float is negative */
133 union { /* floating point arguments %[aAeEfFgG] */
134 double dbl;
135 long double ldbl;
136 } fparg;
137 int expt; /* integer value of exponent */
138 char expchar; /* exponent character: [eEpP\0] */
139 char* dtoaend; /* pointer to end of converted digits */
140 int expsize; /* character count for expstr */
141 int lead; /* sig figs before decimal or group sep */
142 int ndig; /* actual number of digits returned by dtoa */
Elliott Hughes93a1f8b2017-11-07 22:52:29 -0800143 CHAR_TYPE expstr[MAXEXPDIG + 2]; /* buffer for exponent string: e+ZZZ */
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700144 char* dtoaresult = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800145
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700146 uintmax_t _umax; /* integer arguments %[diouxX] */
147 enum { OCT, DEC, HEX } base; /* base for %[diouxX] conversion */
148 int dprec; /* a copy of prec if %[diouxX], 0 otherwise */
149 int realsz; /* field size expanded by dprec */
150 int size; /* size of converted field or string */
151 const char* xdigs; /* digits for %[xX] conversion */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800152#define NIOV 8
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700153 struct __suio uio; /* output information: summary */
154 struct __siov iov[NIOV]; /* ... and individual io vectors */
Elliott Hughes93a1f8b2017-11-07 22:52:29 -0800155 CHAR_TYPE buf[BUF]; /* buffer with space for digits of uintmax_t */
156 CHAR_TYPE ox[2]; /* space for 0x; ox[1] is either x, X, or \0 */
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700157 union arg* argtable; /* args, built due to positional arg */
158 union arg statargtable[STATIC_ARG_TBL_SIZE];
159 size_t argtablesiz;
160 int nextarg; /* 1-based argument index */
161 va_list orgap; /* original argument pointer */
Elliott Hughes93a1f8b2017-11-07 22:52:29 -0800162 CHAR_TYPE* convbuf; /* buffer for wide/multibyte conversion */
Elliott Hughes05493712014-04-17 17:30:03 -0700163
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700164 /*
165 * Choose PADSIZE to trade efficiency vs. size. If larger printf
166 * fields occur frequently, increase PADSIZE and make the initialisers
167 * below longer.
168 */
169#define PADSIZE 16 /* pad chunk size */
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700170 static CHAR_TYPE blanks[PADSIZE] = {
171 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '
172 };
173 static CHAR_TYPE zeroes[PADSIZE] = {
174 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'
175 };
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800176
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700177 static const char xdigs_lower[] = "0123456789abcdef";
178 static const char xdigs_upper[] = "0123456789ABCDEF";
Elliott Hughes05493712014-04-17 17:30:03 -0700179
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700180 /*
181 * BEWARE, these `goto error' on error, and PAD uses `n'.
182 */
183#define PRINT(ptr, len) \
184 do { \
185 iovp->iov_base = (ptr); \
186 iovp->iov_len = (len); \
187 uio.uio_resid += (len); \
188 iovp++; \
189 if (++uio.uio_iovcnt >= NIOV) { \
190 if (__sprint(fp, &uio)) goto error; \
191 iovp = iov; \
192 } \
193 } while (0)
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700194#define FLUSH() \
195 do { \
196 if (uio.uio_resid && __sprint(fp, &uio)) goto error; \
197 uio.uio_iovcnt = 0; \
198 iovp = iov; \
199 } while (0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800200
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700201 _SET_ORIENTATION(fp, -1);
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700202
203 // Writing "" to a read only file returns EOF, not 0.
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700204 if (cantwrite(fp)) {
205 errno = EBADF;
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700206 return EOF;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700207 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800208
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700209 // Optimize writes to stderr and other unbuffered files).
210 if ((fp->_flags & (__SNBF | __SWR | __SRW)) == (__SNBF | __SWR) && fp->_file >= 0) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700211 return (__sbprintf(fp, fmt0, ap));
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700212 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800213
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700214 CHAR_TYPE* fmt = const_cast<CHAR_TYPE*>(fmt0);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700215 argtable = NULL;
216 nextarg = 1;
217 va_copy(orgap, ap);
218 uio.uio_iov = iovp = iov;
219 uio.uio_resid = 0;
220 uio.uio_iovcnt = 0;
221 ret = 0;
222 convbuf = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800223
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700224 /*
225 * Scan the format for conversions (`%' character).
226 */
227 for (;;) {
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700228 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) continue;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700229 if (fmt != cp) {
230 ptrdiff_t m = fmt - cp;
231 if (m < 0 || m > INT_MAX - ret) goto overflow;
232 PRINT(cp, m);
233 ret += m;
234 }
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700235 if (ch == '\0') goto done;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700236 fmt++; /* skip over '%' */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800237
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700238 flags = 0;
239 dprec = 0;
240 width = 0;
241 prec = -1;
242 sign = '\0';
243 ox[1] = '\0';
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800244
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700245 rflag:
246 ch = *fmt++;
247 reswitch:
248 switch (ch) {
249 case ' ':
250 /*
251 * ``If the space and + flags both appear, the space
252 * flag will be ignored.''
253 * -- ANSI X3J11
254 */
255 if (!sign) sign = ' ';
256 goto rflag;
257 case '#':
258 flags |= ALT;
259 goto rflag;
260 case '\'':
261 /* grouping not implemented */
262 goto rflag;
263 case '*':
264 /*
265 * ``A negative field width argument is taken as a
266 * - flag followed by a positive field width.''
267 * -- ANSI X3J11
268 * They don't exclude field widths read from args.
269 */
270 GETASTER(width);
271 if (width >= 0) goto rflag;
272 if (width == INT_MIN) goto overflow;
273 width = -width;
274 /* FALLTHROUGH */
275 case '-':
276 flags |= LADJUST;
277 goto rflag;
278 case '+':
279 sign = '+';
280 goto rflag;
281 case '.':
282 if ((ch = *fmt++) == '*') {
283 GETASTER(n);
284 prec = n < 0 ? -1 : n;
285 goto rflag;
286 }
287 n = 0;
288 while (is_digit(ch)) {
289 APPEND_DIGIT(n, ch);
290 ch = *fmt++;
291 }
292 if (ch == '$') {
293 nextarg = n;
294 if (argtable == NULL) {
295 argtable = statargtable;
296 if (__find_arguments(fmt0, orgap, &argtable, &argtablesiz) == -1) {
297 ret = -1;
298 goto error;
299 }
300 }
301 goto rflag;
302 }
303 prec = n;
304 goto reswitch;
305 case '0':
306 /*
307 * ``Note that 0 is taken as a flag, not as the
308 * beginning of a field width.''
309 * -- ANSI X3J11
310 */
311 flags |= ZEROPAD;
312 goto rflag;
313 case '1':
314 case '2':
315 case '3':
316 case '4':
317 case '5':
318 case '6':
319 case '7':
320 case '8':
321 case '9':
322 n = 0;
323 do {
324 APPEND_DIGIT(n, ch);
325 ch = *fmt++;
326 } while (is_digit(ch));
327 if (ch == '$') {
328 nextarg = n;
329 if (argtable == NULL) {
330 argtable = statargtable;
331 if (__find_arguments(fmt0, orgap, &argtable, &argtablesiz) == -1) {
332 ret = -1;
333 goto error;
334 }
335 }
336 goto rflag;
337 }
338 width = n;
339 goto reswitch;
340 case 'L':
341 flags |= LONGDBL;
342 goto rflag;
343 case 'h':
344 if (*fmt == 'h') {
345 fmt++;
346 flags |= CHARINT;
347 } else {
348 flags |= SHORTINT;
349 }
350 goto rflag;
351 case 'j':
352 flags |= MAXINT;
353 goto rflag;
354 case 'l':
355 if (*fmt == 'l') {
356 fmt++;
357 flags |= LLONGINT;
358 } else {
359 flags |= LONGINT;
360 }
361 goto rflag;
362 case 'q':
363 flags |= LLONGINT;
364 goto rflag;
365 case 't':
366 flags |= PTRINT;
367 goto rflag;
368 case 'z':
369 flags |= SIZEINT;
370 goto rflag;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700371 case 'C':
372 flags |= LONGINT;
373 /*FALLTHROUGH*/
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700374 case 'c':
375 if (flags & LONGINT) {
376 mbstate_t mbs;
377 size_t mbseqlen;
Elliott Hughes05493712014-04-17 17:30:03 -0700378
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700379 memset(&mbs, 0, sizeof(mbs));
380 mbseqlen = wcrtomb(buf, (wchar_t)GETARG(wint_t), &mbs);
381 if (mbseqlen == (size_t)-1) {
382 ret = -1;
383 goto error;
384 }
385 cp = buf;
386 size = (int)mbseqlen;
387 } else {
388 *(cp = buf) = GETARG(int);
389 size = 1;
390 }
391 sign = '\0';
392 break;
393 case 'D':
394 flags |= LONGINT;
395 /*FALLTHROUGH*/
396 case 'd':
397 case 'i':
398 _umax = SARG();
399 if ((intmax_t)_umax < 0) {
400 _umax = -_umax;
401 sign = '-';
402 }
403 base = DEC;
404 goto number;
405 case 'a':
406 case 'A':
407 if (ch == 'a') {
408 ox[1] = 'x';
409 xdigs = xdigs_lower;
410 expchar = 'p';
411 } else {
412 ox[1] = 'X';
413 xdigs = xdigs_upper;
414 expchar = 'P';
415 }
416 if (prec >= 0) prec++;
417 if (dtoaresult) __freedtoa(dtoaresult);
418 if (flags & LONGDBL) {
419 fparg.ldbl = GETARG(long double);
420 dtoaresult = cp = __hldtoa(fparg.ldbl, xdigs, prec, &expt, &signflag, &dtoaend);
421 if (dtoaresult == NULL) {
422 errno = ENOMEM;
423 goto error;
424 }
425 } else {
426 fparg.dbl = GETARG(double);
427 dtoaresult = cp = __hdtoa(fparg.dbl, xdigs, prec, &expt, &signflag, &dtoaend);
428 if (dtoaresult == NULL) {
429 errno = ENOMEM;
430 goto error;
431 }
432 }
433 if (prec < 0) prec = dtoaend - cp;
434 if (expt == INT_MAX) ox[1] = '\0';
435 goto fp_common;
436 case 'e':
437 case 'E':
438 expchar = ch;
439 if (prec < 0) /* account for digit before decpt */
440 prec = DEFPREC + 1;
441 else
442 prec++;
443 goto fp_begin;
444 case 'f':
445 case 'F':
446 expchar = '\0';
447 goto fp_begin;
448 case 'g':
449 case 'G':
450 expchar = ch - ('g' - 'e');
451 if (prec == 0) prec = 1;
452 fp_begin:
453 if (prec < 0) prec = DEFPREC;
454 if (dtoaresult) __freedtoa(dtoaresult);
455 if (flags & LONGDBL) {
456 fparg.ldbl = GETARG(long double);
457 dtoaresult = cp = __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec, &expt, &signflag, &dtoaend);
458 if (dtoaresult == NULL) {
459 errno = ENOMEM;
460 goto error;
461 }
462 } else {
463 fparg.dbl = GETARG(double);
464 dtoaresult = cp = __dtoa(fparg.dbl, expchar ? 2 : 3, prec, &expt, &signflag, &dtoaend);
465 if (dtoaresult == NULL) {
466 errno = ENOMEM;
467 goto error;
468 }
469 if (expt == 9999) expt = INT_MAX;
470 }
471 fp_common:
472 if (signflag) sign = '-';
473 if (expt == INT_MAX) { /* inf or nan */
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700474 if (*cp == 'N') {
475 cp = const_cast<char*>((ch >= 'a') ? "nan" : "NAN");
476 } else {
477 cp = const_cast<char*>((ch >= 'a') ? "inf" : "INF");
478 }
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700479 size = 3;
480 flags &= ~ZEROPAD;
481 break;
482 }
483 flags |= FPT;
484 ndig = dtoaend - cp;
485 if (ch == 'g' || ch == 'G') {
486 if (expt > -4 && expt <= prec) {
487 /* Make %[gG] smell like %[fF] */
488 expchar = '\0';
489 if (flags & ALT)
490 prec -= expt;
491 else
492 prec = ndig - expt;
493 if (prec < 0) prec = 0;
494 } else {
495 /*
496 * Make %[gG] smell like %[eE], but
497 * trim trailing zeroes if no # flag.
498 */
499 if (!(flags & ALT)) prec = ndig;
500 }
501 }
502 if (expchar) {
503 expsize = exponent(expstr, expt - 1, expchar);
504 size = expsize + prec;
505 if (prec > 1 || flags & ALT) ++size;
506 } else {
507 /* space for digits before decimal point */
508 if (expt > 0)
509 size = expt;
510 else /* "0" */
511 size = 1;
512 /* space for decimal pt and following digits */
513 if (prec || flags & ALT) size += prec + 1;
514 lead = expt;
515 }
516 break;
Elliott Hughese2341d02014-05-02 18:16:32 -0700517#ifndef NO_PRINTF_PERCENT_N
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700518 case 'n':
519 if (flags & LLONGINT)
520 *GETARG(long long*) = ret;
521 else if (flags & LONGINT)
522 *GETARG(long*) = ret;
523 else if (flags & SHORTINT)
524 *GETARG(short*) = ret;
525 else if (flags & CHARINT)
526 *GETARG(signed char*) = ret;
527 else if (flags & PTRINT)
528 *GETARG(ptrdiff_t*) = ret;
529 else if (flags & SIZEINT)
530 *GETARG(ssize_t*) = ret;
531 else if (flags & MAXINT)
532 *GETARG(intmax_t*) = ret;
533 else
534 *GETARG(int*) = ret;
535 continue; /* no output */
536#endif /* NO_PRINTF_PERCENT_N */
537 case 'O':
538 flags |= LONGINT;
539 /*FALLTHROUGH*/
540 case 'o':
541 _umax = UARG();
542 base = OCT;
543 goto nosign;
544 case 'p':
545 /*
546 * ``The argument shall be a pointer to void. The
547 * value of the pointer is converted to a sequence
548 * of printable characters, in an implementation-
549 * defined manner.''
550 * -- ANSI X3J11
551 */
552 _umax = (u_long)GETARG(void*);
553 base = HEX;
554 xdigs = xdigs_lower;
555 ox[1] = 'x';
556 goto nosign;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700557 case 'S':
558 flags |= LONGINT;
559 /*FALLTHROUGH*/
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700560 case 's':
561 if (flags & LONGINT) {
562 wchar_t* wcp;
Elliott Hughes05493712014-04-17 17:30:03 -0700563
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700564 free(convbuf);
565 convbuf = NULL;
566 if ((wcp = GETARG(wchar_t*)) == NULL) {
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700567 cp = const_cast<char*>("(null)");
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700568 } else {
569 convbuf = __wcsconv(wcp, prec);
570 if (convbuf == NULL) {
571 ret = -1;
572 goto error;
573 }
574 cp = convbuf;
575 }
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700576 } else if ((cp = GETARG(char*)) == NULL) {
577 cp = const_cast<char*>("(null)");
578 }
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700579 if (prec >= 0) {
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700580 size = strnlen(cp, prec);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700581 } else {
582 size_t len;
Elliott Hughes05493712014-04-17 17:30:03 -0700583
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700584 if ((len = strlen(cp)) > INT_MAX) goto overflow;
585 size = (int)len;
586 }
587 sign = '\0';
588 break;
589 case 'U':
590 flags |= LONGINT;
591 /*FALLTHROUGH*/
592 case 'u':
593 _umax = UARG();
594 base = DEC;
595 goto nosign;
596 case 'X':
597 xdigs = xdigs_upper;
598 goto hex;
599 case 'x':
600 xdigs = xdigs_lower;
601 hex:
602 _umax = UARG();
603 base = HEX;
604 /* leading 0x/X only if non-zero */
605 if (flags & ALT && _umax != 0) ox[1] = ch;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800606
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700607 /* unsigned conversions */
608 nosign:
609 sign = '\0';
610 /*
611 * ``... diouXx conversions ... if a precision is
612 * specified, the 0 flag will be ignored.''
613 * -- ANSI X3J11
614 */
615 number:
616 if ((dprec = prec) >= 0) flags &= ~ZEROPAD;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800617
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700618 /*
619 * ``The result of converting a zero value with an
620 * explicit precision of zero is no characters.''
621 * -- ANSI X3J11
622 */
623 cp = buf + BUF;
624 if (_umax != 0 || prec != 0) {
625 /*
626 * Unsigned mod is hard, and unsigned mod
627 * by a constant is easier than that by
628 * a variable; hence this switch.
629 */
630 switch (base) {
631 case OCT:
632 do {
633 *--cp = to_char(_umax & 7);
634 _umax >>= 3;
635 } while (_umax);
636 /* handle octal leading 0 */
637 if (flags & ALT && *cp != '0') *--cp = '0';
638 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800639
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700640 case DEC:
641 /* many numbers are 1 digit */
642 while (_umax >= 10) {
643 *--cp = to_char(_umax % 10);
644 _umax /= 10;
645 }
646 *--cp = to_char(_umax);
647 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800648
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700649 case HEX:
650 do {
651 *--cp = xdigs[_umax & 15];
652 _umax >>= 4;
653 } while (_umax);
654 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800655
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700656 default:
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700657 abort();
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700658 }
659 }
660 size = buf + BUF - cp;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700661 if (size > BUF) abort(); /* should never happen */
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700662 break;
663 default: /* "%?" prints ?, unless ? is NUL */
664 if (ch == '\0') goto done;
665 /* pretend it was %c with argument ch */
666 cp = buf;
667 *cp = ch;
668 size = 1;
669 sign = '\0';
670 break;
671 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800672
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700673 /*
674 * All reasonable formats wind up here. At this point, `cp'
675 * points to a string which (if not flags&LADJUST) should be
676 * padded out to `width' places. If flags&ZEROPAD, it should
677 * first be prefixed by any sign or other prefix; otherwise,
678 * it should be blank padded before the prefix is emitted.
679 * After any left-hand padding and prefixing, emit zeroes
680 * required by a decimal %[diouxX] precision, then print the
681 * string proper, then emit zeroes required by any leftover
682 * floating precision; finally, if LADJUST, pad with blanks.
683 *
684 * Compute actual size, so we know how much to pad.
685 * size excludes decimal prec; realsz includes it.
686 */
687 realsz = dprec > size ? dprec : size;
688 if (sign) realsz++;
689 if (ox[1]) realsz += 2;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800690
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700691 /* right-adjusting blank padding */
692 if ((flags & (LADJUST | ZEROPAD)) == 0) PAD(width - realsz, blanks);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800693
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700694 /* prefix */
695 if (sign) PRINT(&sign, 1);
696 if (ox[1]) { /* ox[1] is either x, X, or \0 */
697 ox[0] = '0';
698 PRINT(ox, 2);
699 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800700
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700701 /* right-adjusting zero padding */
702 if ((flags & (LADJUST | ZEROPAD)) == ZEROPAD) PAD(width - realsz, zeroes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800703
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700704 /* leading zeroes from decimal precision */
705 PAD(dprec - size, zeroes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800706
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700707 /* the string or number proper */
708 if ((flags & FPT) == 0) {
709 PRINT(cp, size);
710 } else { /* glue together f_p fragments */
711 if (decimal_point == NULL) decimal_point = nl_langinfo(RADIXCHAR);
712 if (!expchar) { /* %[fF] or sufficiently short %[gG] */
713 if (expt <= 0) {
714 PRINT(zeroes, 1);
715 if (prec || flags & ALT) PRINT(decimal_point, 1);
716 PAD(-expt, zeroes);
717 /* already handled initial 0's */
718 prec += expt;
719 } else {
720 PRINTANDPAD(cp, dtoaend, lead, zeroes);
721 cp += lead;
722 if (prec || flags & ALT) PRINT(decimal_point, 1);
723 }
724 PRINTANDPAD(cp, dtoaend, prec, zeroes);
725 } else { /* %[eE] or sufficiently long %[gG] */
726 if (prec > 1 || flags & ALT) {
727 buf[0] = *cp++;
728 buf[1] = *decimal_point;
729 PRINT(buf, 2);
730 PRINT(cp, ndig - 1);
731 PAD(prec - ndig, zeroes);
732 } else { /* XeYYY */
733 PRINT(cp, 1);
734 }
735 PRINT(expstr, expsize);
736 }
737 }
738 /* left-adjusting padding (always blank) */
739 if (flags & LADJUST) PAD(width - realsz, blanks);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800740
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700741 /* finally, adjust ret */
742 if (width < realsz) width = realsz;
743 if (width > INT_MAX - ret) goto overflow;
744 ret += width;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800745
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700746 FLUSH(); /* copy out the I/O vectors */
747 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800748done:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700749 FLUSH();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800750error:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700751 va_end(orgap);
752 if (__sferror(fp)) ret = -1;
753 goto finish;
Elliott Hughes05493712014-04-17 17:30:03 -0700754
755overflow:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700756 errno = ENOMEM;
757 ret = -1;
Elliott Hughes05493712014-04-17 17:30:03 -0700758
759finish:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700760 free(convbuf);
761 if (dtoaresult) __freedtoa(dtoaresult);
762 if (argtable != NULL && argtable != statargtable) {
763 munmap(argtable, argtablesiz);
764 argtable = NULL;
765 }
766 return (ret);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800767}