blob: 17e4372cdbaf2e74553923e583ba843692c5a59e [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 Hughesbc27bdc2017-11-10 15:25:49 -080036#define CHAR_TYPE_STRLEN strlen
37#define CHAR_TYPE_STRNLEN strnlen
38#define CHAR_TYPE_INF "INF"
39#define CHAR_TYPE_inf "inf"
40#define CHAR_TYPE_NAN "NAN"
41#define CHAR_TYPE_nan "nan"
42#define CHAR_TYPE_ORIENTATION -1
Elliott Hughes1f493172017-11-08 16:13:18 -080043#include "printf_common.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080044
Elliott Hughes1f493172017-11-08 16:13:18 -080045int FUNCTION_NAME(FILE* fp, const CHAR_TYPE* fmt0, va_list ap) {
Elliott Hughesb70576b2017-11-13 11:10:05 -080046 int n, n2;
Elliott Hughes93a1f8b2017-11-07 22:52:29 -080047 CHAR_TYPE* cp; /* handy char pointer (short term usage) */
Elliott Hughes93a1f8b2017-11-07 22:52:29 -080048 CHAR_TYPE sign; /* sign prefix (' ', '+', '-', or \0) */
Elliott Hughesc8f2c522017-10-31 13:07:51 -070049 int flags; /* flags as above */
50 int ret; /* return value accumulator */
51 int width; /* width from format (%8d), or 0 */
52 int prec; /* precision from format; <0 for N/A */
Elliott Hughesc8f2c522017-10-31 13:07:51 -070053 /*
54 * We can decompose the printed representation of floating
55 * point numbers into several parts, some of which may be empty:
56 *
57 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
58 * A B ---C--- D E F
59 *
60 * A: 'sign' holds this value if present; '\0' otherwise
61 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
62 * C: cp points to the string MMMNNN. Leading and trailing
63 * zeros are not in the string and must be added.
64 * D: expchar holds this character; '\0' if no exponent, e.g. %f
65 * F: at least two digits for decimal, at least one digit for hex
66 */
67 char* decimal_point = NULL;
68 int signflag; /* true if float is negative */
69 union { /* floating point arguments %[aAeEfFgG] */
70 double dbl;
71 long double ldbl;
72 } fparg;
73 int expt; /* integer value of exponent */
74 char expchar; /* exponent character: [eEpP\0] */
75 char* dtoaend; /* pointer to end of converted digits */
76 int expsize; /* character count for expstr */
77 int lead; /* sig figs before decimal or group sep */
78 int ndig; /* actual number of digits returned by dtoa */
Elliott Hughes93a1f8b2017-11-07 22:52:29 -080079 CHAR_TYPE expstr[MAXEXPDIG + 2]; /* buffer for exponent string: e+ZZZ */
Elliott Hughesc8f2c522017-10-31 13:07:51 -070080 char* dtoaresult = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080081
Elliott Hughesc8f2c522017-10-31 13:07:51 -070082 uintmax_t _umax; /* integer arguments %[diouxX] */
83 enum { OCT, DEC, HEX } base; /* base for %[diouxX] conversion */
84 int dprec; /* a copy of prec if %[diouxX], 0 otherwise */
85 int realsz; /* field size expanded by dprec */
86 int size; /* size of converted field or string */
87 const char* xdigs; /* digits for %[xX] conversion */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080088#define NIOV 8
Elliott Hughesc8f2c522017-10-31 13:07:51 -070089 struct __suio uio; /* output information: summary */
90 struct __siov iov[NIOV]; /* ... and individual io vectors */
Elliott Hughesb70576b2017-11-13 11:10:05 -080091 struct __siov* iovp; /* for PRINT macro */
Elliott Hughes93a1f8b2017-11-07 22:52:29 -080092 CHAR_TYPE buf[BUF]; /* buffer with space for digits of uintmax_t */
93 CHAR_TYPE ox[2]; /* space for 0x; ox[1] is either x, X, or \0 */
Elliott Hughesc8f2c522017-10-31 13:07:51 -070094 union arg* argtable; /* args, built due to positional arg */
95 union arg statargtable[STATIC_ARG_TBL_SIZE];
96 size_t argtablesiz;
97 int nextarg; /* 1-based argument index */
98 va_list orgap; /* original argument pointer */
Elliott Hughes93a1f8b2017-11-07 22:52:29 -080099 CHAR_TYPE* convbuf; /* buffer for wide/multibyte conversion */
Elliott Hughes05493712014-04-17 17:30:03 -0700100
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700101 /*
102 * Choose PADSIZE to trade efficiency vs. size. If larger printf
103 * fields occur frequently, increase PADSIZE and make the initialisers
104 * below longer.
105 */
106#define PADSIZE 16 /* pad chunk size */
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700107 static CHAR_TYPE blanks[PADSIZE] = {
108 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '
109 };
110 static CHAR_TYPE zeroes[PADSIZE] = {
111 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'
112 };
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800113
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700114 static const char xdigs_lower[] = "0123456789abcdef";
115 static const char xdigs_upper[] = "0123456789ABCDEF";
Elliott Hughes05493712014-04-17 17:30:03 -0700116
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700117#define PRINT(ptr, len) \
118 do { \
119 iovp->iov_base = (ptr); \
120 iovp->iov_len = (len); \
121 uio.uio_resid += (len); \
122 iovp++; \
123 if (++uio.uio_iovcnt >= NIOV) { \
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800124 if (helpers::sprint(fp, &uio)) goto error; \
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700125 iovp = iov; \
126 } \
127 } while (0)
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700128#define FLUSH() \
129 do { \
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800130 if (uio.uio_resid && helpers::sprint(fp, &uio)) goto error; \
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700131 uio.uio_iovcnt = 0; \
132 iovp = iov; \
133 } while (0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800134
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800135 _SET_ORIENTATION(fp, CHAR_TYPE_ORIENTATION);
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700136
137 // Writing "" to a read only file returns EOF, not 0.
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700138 if (cantwrite(fp)) {
139 errno = EBADF;
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700140 return EOF;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700141 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800142
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700143 // Optimize writes to stderr and other unbuffered files).
144 if ((fp->_flags & (__SNBF | __SWR | __SRW)) == (__SNBF | __SWR) && fp->_file >= 0) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700145 return (__sbprintf(fp, fmt0, ap));
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700146 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800147
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700148 CHAR_TYPE* fmt = const_cast<CHAR_TYPE*>(fmt0);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700149 argtable = NULL;
150 nextarg = 1;
151 va_copy(orgap, ap);
152 uio.uio_iov = iovp = iov;
153 uio.uio_resid = 0;
154 uio.uio_iovcnt = 0;
155 ret = 0;
156 convbuf = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800157
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700158 /*
159 * Scan the format for conversions (`%' character).
160 */
161 for (;;) {
Elliott Hughesb70576b2017-11-13 11:10:05 -0800162 int ch;
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700163 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) continue;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700164 if (fmt != cp) {
165 ptrdiff_t m = fmt - cp;
166 if (m < 0 || m > INT_MAX - ret) goto overflow;
167 PRINT(cp, m);
168 ret += m;
169 }
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700170 if (ch == '\0') goto done;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700171 fmt++; /* skip over '%' */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800172
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700173 flags = 0;
174 dprec = 0;
175 width = 0;
176 prec = -1;
177 sign = '\0';
178 ox[1] = '\0';
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800179
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700180 rflag:
181 ch = *fmt++;
182 reswitch:
183 switch (ch) {
184 case ' ':
185 /*
186 * ``If the space and + flags both appear, the space
187 * flag will be ignored.''
188 * -- ANSI X3J11
189 */
190 if (!sign) sign = ' ';
191 goto rflag;
192 case '#':
193 flags |= ALT;
194 goto rflag;
195 case '\'':
196 /* grouping not implemented */
197 goto rflag;
198 case '*':
199 /*
200 * ``A negative field width argument is taken as a
201 * - flag followed by a positive field width.''
202 * -- ANSI X3J11
203 * They don't exclude field widths read from args.
204 */
205 GETASTER(width);
206 if (width >= 0) goto rflag;
207 if (width == INT_MIN) goto overflow;
208 width = -width;
209 /* FALLTHROUGH */
210 case '-':
211 flags |= LADJUST;
212 goto rflag;
213 case '+':
214 sign = '+';
215 goto rflag;
216 case '.':
217 if ((ch = *fmt++) == '*') {
218 GETASTER(n);
219 prec = n < 0 ? -1 : n;
220 goto rflag;
221 }
222 n = 0;
223 while (is_digit(ch)) {
224 APPEND_DIGIT(n, ch);
225 ch = *fmt++;
226 }
227 if (ch == '$') {
228 nextarg = n;
229 if (argtable == NULL) {
230 argtable = statargtable;
231 if (__find_arguments(fmt0, orgap, &argtable, &argtablesiz) == -1) {
232 ret = -1;
233 goto error;
234 }
235 }
236 goto rflag;
237 }
238 prec = n;
239 goto reswitch;
240 case '0':
241 /*
242 * ``Note that 0 is taken as a flag, not as the
243 * beginning of a field width.''
244 * -- ANSI X3J11
245 */
246 flags |= ZEROPAD;
247 goto rflag;
248 case '1':
249 case '2':
250 case '3':
251 case '4':
252 case '5':
253 case '6':
254 case '7':
255 case '8':
256 case '9':
257 n = 0;
258 do {
259 APPEND_DIGIT(n, ch);
260 ch = *fmt++;
261 } while (is_digit(ch));
262 if (ch == '$') {
263 nextarg = n;
264 if (argtable == NULL) {
265 argtable = statargtable;
266 if (__find_arguments(fmt0, orgap, &argtable, &argtablesiz) == -1) {
267 ret = -1;
268 goto error;
269 }
270 }
271 goto rflag;
272 }
273 width = n;
274 goto reswitch;
275 case 'L':
276 flags |= LONGDBL;
277 goto rflag;
278 case 'h':
279 if (*fmt == 'h') {
280 fmt++;
281 flags |= CHARINT;
282 } else {
283 flags |= SHORTINT;
284 }
285 goto rflag;
286 case 'j':
287 flags |= MAXINT;
288 goto rflag;
289 case 'l':
290 if (*fmt == 'l') {
291 fmt++;
292 flags |= LLONGINT;
293 } else {
294 flags |= LONGINT;
295 }
296 goto rflag;
297 case 'q':
298 flags |= LLONGINT;
299 goto rflag;
300 case 't':
301 flags |= PTRINT;
302 goto rflag;
303 case 'z':
304 flags |= SIZEINT;
305 goto rflag;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700306 case 'C':
307 flags |= LONGINT;
308 /*FALLTHROUGH*/
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700309 case 'c':
310 if (flags & LONGINT) {
311 mbstate_t mbs;
312 size_t mbseqlen;
Elliott Hughes05493712014-04-17 17:30:03 -0700313
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700314 memset(&mbs, 0, sizeof(mbs));
315 mbseqlen = wcrtomb(buf, (wchar_t)GETARG(wint_t), &mbs);
316 if (mbseqlen == (size_t)-1) {
317 ret = -1;
318 goto error;
319 }
320 cp = buf;
321 size = (int)mbseqlen;
322 } else {
323 *(cp = buf) = GETARG(int);
324 size = 1;
325 }
326 sign = '\0';
327 break;
328 case 'D':
329 flags |= LONGINT;
330 /*FALLTHROUGH*/
331 case 'd':
332 case 'i':
333 _umax = SARG();
334 if ((intmax_t)_umax < 0) {
335 _umax = -_umax;
336 sign = '-';
337 }
338 base = DEC;
339 goto number;
340 case 'a':
341 case 'A':
342 if (ch == 'a') {
343 ox[1] = 'x';
344 xdigs = xdigs_lower;
345 expchar = 'p';
346 } else {
347 ox[1] = 'X';
348 xdigs = xdigs_upper;
349 expchar = 'P';
350 }
351 if (prec >= 0) prec++;
352 if (dtoaresult) __freedtoa(dtoaresult);
353 if (flags & LONGDBL) {
354 fparg.ldbl = GETARG(long double);
355 dtoaresult = cp = __hldtoa(fparg.ldbl, xdigs, prec, &expt, &signflag, &dtoaend);
356 if (dtoaresult == NULL) {
357 errno = ENOMEM;
358 goto error;
359 }
360 } else {
361 fparg.dbl = GETARG(double);
362 dtoaresult = cp = __hdtoa(fparg.dbl, xdigs, prec, &expt, &signflag, &dtoaend);
363 if (dtoaresult == NULL) {
364 errno = ENOMEM;
365 goto error;
366 }
367 }
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800368 if (prec < 0) prec = dtoaend - dtoaresult;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700369 if (expt == INT_MAX) ox[1] = '\0';
370 goto fp_common;
371 case 'e':
372 case 'E':
373 expchar = ch;
374 if (prec < 0) /* account for digit before decpt */
375 prec = DEFPREC + 1;
376 else
377 prec++;
378 goto fp_begin;
379 case 'f':
380 case 'F':
381 expchar = '\0';
382 goto fp_begin;
383 case 'g':
384 case 'G':
385 expchar = ch - ('g' - 'e');
386 if (prec == 0) prec = 1;
387 fp_begin:
388 if (prec < 0) prec = DEFPREC;
389 if (dtoaresult) __freedtoa(dtoaresult);
390 if (flags & LONGDBL) {
391 fparg.ldbl = GETARG(long double);
392 dtoaresult = cp = __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec, &expt, &signflag, &dtoaend);
393 if (dtoaresult == NULL) {
394 errno = ENOMEM;
395 goto error;
396 }
397 } else {
398 fparg.dbl = GETARG(double);
399 dtoaresult = cp = __dtoa(fparg.dbl, expchar ? 2 : 3, prec, &expt, &signflag, &dtoaend);
400 if (dtoaresult == NULL) {
401 errno = ENOMEM;
402 goto error;
403 }
404 if (expt == 9999) expt = INT_MAX;
405 }
406 fp_common:
407 if (signflag) sign = '-';
408 if (expt == INT_MAX) { /* inf or nan */
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700409 if (*cp == 'N') {
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800410 cp = const_cast<CHAR_TYPE*>((ch >= 'a') ? CHAR_TYPE_nan : CHAR_TYPE_NAN);
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700411 } else {
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800412 cp = const_cast<CHAR_TYPE*>((ch >= 'a') ? CHAR_TYPE_inf : CHAR_TYPE_INF);
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700413 }
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700414 size = 3;
415 flags &= ~ZEROPAD;
416 break;
417 }
418 flags |= FPT;
419 ndig = dtoaend - cp;
420 if (ch == 'g' || ch == 'G') {
421 if (expt > -4 && expt <= prec) {
422 /* Make %[gG] smell like %[fF] */
423 expchar = '\0';
424 if (flags & ALT)
425 prec -= expt;
426 else
427 prec = ndig - expt;
428 if (prec < 0) prec = 0;
429 } else {
430 /*
431 * Make %[gG] smell like %[eE], but
432 * trim trailing zeroes if no # flag.
433 */
434 if (!(flags & ALT)) prec = ndig;
435 }
436 }
437 if (expchar) {
438 expsize = exponent(expstr, expt - 1, expchar);
439 size = expsize + prec;
440 if (prec > 1 || flags & ALT) ++size;
441 } else {
442 /* space for digits before decimal point */
443 if (expt > 0)
444 size = expt;
445 else /* "0" */
446 size = 1;
447 /* space for decimal pt and following digits */
448 if (prec || flags & ALT) size += prec + 1;
449 lead = expt;
450 }
451 break;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700452 case 'n':
Elliott Hughes41398d02018-03-07 13:32:58 -0800453 __fortify_fatal("%%n not allowed on Android");
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700454 case 'O':
455 flags |= LONGINT;
456 /*FALLTHROUGH*/
457 case 'o':
458 _umax = UARG();
459 base = OCT;
460 goto nosign;
461 case 'p':
462 /*
463 * ``The argument shall be a pointer to void. The
464 * value of the pointer is converted to a sequence
465 * of printable characters, in an implementation-
466 * defined manner.''
467 * -- ANSI X3J11
468 */
469 _umax = (u_long)GETARG(void*);
470 base = HEX;
471 xdigs = xdigs_lower;
472 ox[1] = 'x';
473 goto nosign;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700474 case 'S':
475 flags |= LONGINT;
476 /*FALLTHROUGH*/
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700477 case 's':
478 if (flags & LONGINT) {
479 wchar_t* wcp;
Elliott Hughes05493712014-04-17 17:30:03 -0700480
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700481 free(convbuf);
482 convbuf = NULL;
483 if ((wcp = GETARG(wchar_t*)) == NULL) {
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700484 cp = const_cast<char*>("(null)");
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700485 } else {
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800486 convbuf = helpers::wcsconv(wcp, prec);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700487 if (convbuf == NULL) {
488 ret = -1;
489 goto error;
490 }
491 cp = convbuf;
492 }
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700493 } else if ((cp = GETARG(char*)) == NULL) {
494 cp = const_cast<char*>("(null)");
495 }
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700496 if (prec >= 0) {
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800497 size = CHAR_TYPE_STRNLEN(cp, prec);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700498 } else {
499 size_t len;
Elliott Hughes05493712014-04-17 17:30:03 -0700500
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800501 if ((len = CHAR_TYPE_STRLEN(cp)) > INT_MAX) goto overflow;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700502 size = (int)len;
503 }
504 sign = '\0';
505 break;
506 case 'U':
507 flags |= LONGINT;
508 /*FALLTHROUGH*/
509 case 'u':
510 _umax = UARG();
511 base = DEC;
512 goto nosign;
513 case 'X':
514 xdigs = xdigs_upper;
515 goto hex;
516 case 'x':
517 xdigs = xdigs_lower;
518 hex:
519 _umax = UARG();
520 base = HEX;
521 /* leading 0x/X only if non-zero */
522 if (flags & ALT && _umax != 0) ox[1] = ch;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800523
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700524 /* unsigned conversions */
525 nosign:
526 sign = '\0';
527 /*
528 * ``... diouXx conversions ... if a precision is
529 * specified, the 0 flag will be ignored.''
530 * -- ANSI X3J11
531 */
532 number:
533 if ((dprec = prec) >= 0) flags &= ~ZEROPAD;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800534
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700535 /*
536 * ``The result of converting a zero value with an
537 * explicit precision of zero is no characters.''
538 * -- ANSI X3J11
539 */
540 cp = buf + BUF;
541 if (_umax != 0 || prec != 0) {
542 /*
543 * Unsigned mod is hard, and unsigned mod
544 * by a constant is easier than that by
545 * a variable; hence this switch.
546 */
547 switch (base) {
548 case OCT:
549 do {
550 *--cp = to_char(_umax & 7);
551 _umax >>= 3;
552 } while (_umax);
553 /* handle octal leading 0 */
554 if (flags & ALT && *cp != '0') *--cp = '0';
555 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800556
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700557 case DEC:
558 /* many numbers are 1 digit */
559 while (_umax >= 10) {
560 *--cp = to_char(_umax % 10);
561 _umax /= 10;
562 }
563 *--cp = to_char(_umax);
564 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800565
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700566 case HEX:
567 do {
568 *--cp = xdigs[_umax & 15];
569 _umax >>= 4;
570 } while (_umax);
571 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800572
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700573 default:
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700574 abort();
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700575 }
576 }
577 size = buf + BUF - cp;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700578 if (size > BUF) abort(); /* should never happen */
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700579 break;
580 default: /* "%?" prints ?, unless ? is NUL */
581 if (ch == '\0') goto done;
582 /* pretend it was %c with argument ch */
583 cp = buf;
584 *cp = ch;
585 size = 1;
586 sign = '\0';
587 break;
588 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800589
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700590 /*
591 * All reasonable formats wind up here. At this point, `cp'
592 * points to a string which (if not flags&LADJUST) should be
593 * padded out to `width' places. If flags&ZEROPAD, it should
594 * first be prefixed by any sign or other prefix; otherwise,
595 * it should be blank padded before the prefix is emitted.
596 * After any left-hand padding and prefixing, emit zeroes
597 * required by a decimal %[diouxX] precision, then print the
598 * string proper, then emit zeroes required by any leftover
599 * floating precision; finally, if LADJUST, pad with blanks.
600 *
601 * Compute actual size, so we know how much to pad.
602 * size excludes decimal prec; realsz includes it.
603 */
604 realsz = dprec > size ? dprec : size;
605 if (sign) realsz++;
606 if (ox[1]) realsz += 2;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800607
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700608 /* right-adjusting blank padding */
609 if ((flags & (LADJUST | ZEROPAD)) == 0) PAD(width - realsz, blanks);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800610
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700611 /* prefix */
612 if (sign) PRINT(&sign, 1);
613 if (ox[1]) { /* ox[1] is either x, X, or \0 */
614 ox[0] = '0';
615 PRINT(ox, 2);
616 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800617
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700618 /* right-adjusting zero padding */
619 if ((flags & (LADJUST | ZEROPAD)) == ZEROPAD) PAD(width - realsz, zeroes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800620
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700621 /* leading zeroes from decimal precision */
622 PAD(dprec - size, zeroes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800623
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700624 /* the string or number proper */
625 if ((flags & FPT) == 0) {
626 PRINT(cp, size);
627 } else { /* glue together f_p fragments */
628 if (decimal_point == NULL) decimal_point = nl_langinfo(RADIXCHAR);
629 if (!expchar) { /* %[fF] or sufficiently short %[gG] */
630 if (expt <= 0) {
631 PRINT(zeroes, 1);
632 if (prec || flags & ALT) PRINT(decimal_point, 1);
633 PAD(-expt, zeroes);
634 /* already handled initial 0's */
635 prec += expt;
636 } else {
637 PRINTANDPAD(cp, dtoaend, lead, zeroes);
638 cp += lead;
639 if (prec || flags & ALT) PRINT(decimal_point, 1);
640 }
641 PRINTANDPAD(cp, dtoaend, prec, zeroes);
642 } else { /* %[eE] or sufficiently long %[gG] */
643 if (prec > 1 || flags & ALT) {
644 buf[0] = *cp++;
645 buf[1] = *decimal_point;
646 PRINT(buf, 2);
647 PRINT(cp, ndig - 1);
648 PAD(prec - ndig, zeroes);
649 } else { /* XeYYY */
650 PRINT(cp, 1);
651 }
652 PRINT(expstr, expsize);
653 }
654 }
655 /* left-adjusting padding (always blank) */
656 if (flags & LADJUST) PAD(width - realsz, blanks);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800657
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700658 /* finally, adjust ret */
659 if (width < realsz) width = realsz;
660 if (width > INT_MAX - ret) goto overflow;
661 ret += width;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800662
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700663 FLUSH(); /* copy out the I/O vectors */
664 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800665done:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700666 FLUSH();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800667error:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700668 va_end(orgap);
669 if (__sferror(fp)) ret = -1;
670 goto finish;
Elliott Hughes05493712014-04-17 17:30:03 -0700671
672overflow:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700673 errno = ENOMEM;
674 ret = -1;
Elliott Hughes05493712014-04-17 17:30:03 -0700675
676finish:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700677 free(convbuf);
678 if (dtoaresult) __freedtoa(dtoaresult);
679 if (argtable != NULL && argtable != statargtable) {
680 munmap(argtable, argtablesiz);
681 argtable = NULL;
682 }
683 return (ret);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800684}