blob: 91828801d4e79a6030920ad64d52b701d41e342c [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 Hughese2341d02014-05-02 18:16:32 -0700452#ifndef NO_PRINTF_PERCENT_N
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700453 case 'n':
454 if (flags & LLONGINT)
455 *GETARG(long long*) = ret;
456 else if (flags & LONGINT)
457 *GETARG(long*) = ret;
458 else if (flags & SHORTINT)
459 *GETARG(short*) = ret;
460 else if (flags & CHARINT)
461 *GETARG(signed char*) = ret;
462 else if (flags & PTRINT)
463 *GETARG(ptrdiff_t*) = ret;
464 else if (flags & SIZEINT)
465 *GETARG(ssize_t*) = ret;
466 else if (flags & MAXINT)
467 *GETARG(intmax_t*) = ret;
468 else
469 *GETARG(int*) = ret;
470 continue; /* no output */
471#endif /* NO_PRINTF_PERCENT_N */
472 case 'O':
473 flags |= LONGINT;
474 /*FALLTHROUGH*/
475 case 'o':
476 _umax = UARG();
477 base = OCT;
478 goto nosign;
479 case 'p':
480 /*
481 * ``The argument shall be a pointer to void. The
482 * value of the pointer is converted to a sequence
483 * of printable characters, in an implementation-
484 * defined manner.''
485 * -- ANSI X3J11
486 */
487 _umax = (u_long)GETARG(void*);
488 base = HEX;
489 xdigs = xdigs_lower;
490 ox[1] = 'x';
491 goto nosign;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700492 case 'S':
493 flags |= LONGINT;
494 /*FALLTHROUGH*/
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700495 case 's':
496 if (flags & LONGINT) {
497 wchar_t* wcp;
Elliott Hughes05493712014-04-17 17:30:03 -0700498
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700499 free(convbuf);
500 convbuf = NULL;
501 if ((wcp = GETARG(wchar_t*)) == NULL) {
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700502 cp = const_cast<char*>("(null)");
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700503 } else {
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800504 convbuf = helpers::wcsconv(wcp, prec);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700505 if (convbuf == NULL) {
506 ret = -1;
507 goto error;
508 }
509 cp = convbuf;
510 }
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700511 } else if ((cp = GETARG(char*)) == NULL) {
512 cp = const_cast<char*>("(null)");
513 }
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700514 if (prec >= 0) {
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800515 size = CHAR_TYPE_STRNLEN(cp, prec);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700516 } else {
517 size_t len;
Elliott Hughes05493712014-04-17 17:30:03 -0700518
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800519 if ((len = CHAR_TYPE_STRLEN(cp)) > INT_MAX) goto overflow;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700520 size = (int)len;
521 }
522 sign = '\0';
523 break;
524 case 'U':
525 flags |= LONGINT;
526 /*FALLTHROUGH*/
527 case 'u':
528 _umax = UARG();
529 base = DEC;
530 goto nosign;
531 case 'X':
532 xdigs = xdigs_upper;
533 goto hex;
534 case 'x':
535 xdigs = xdigs_lower;
536 hex:
537 _umax = UARG();
538 base = HEX;
539 /* leading 0x/X only if non-zero */
540 if (flags & ALT && _umax != 0) ox[1] = ch;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800541
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700542 /* unsigned conversions */
543 nosign:
544 sign = '\0';
545 /*
546 * ``... diouXx conversions ... if a precision is
547 * specified, the 0 flag will be ignored.''
548 * -- ANSI X3J11
549 */
550 number:
551 if ((dprec = prec) >= 0) flags &= ~ZEROPAD;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800552
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700553 /*
554 * ``The result of converting a zero value with an
555 * explicit precision of zero is no characters.''
556 * -- ANSI X3J11
557 */
558 cp = buf + BUF;
559 if (_umax != 0 || prec != 0) {
560 /*
561 * Unsigned mod is hard, and unsigned mod
562 * by a constant is easier than that by
563 * a variable; hence this switch.
564 */
565 switch (base) {
566 case OCT:
567 do {
568 *--cp = to_char(_umax & 7);
569 _umax >>= 3;
570 } while (_umax);
571 /* handle octal leading 0 */
572 if (flags & ALT && *cp != '0') *--cp = '0';
573 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800574
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700575 case DEC:
576 /* many numbers are 1 digit */
577 while (_umax >= 10) {
578 *--cp = to_char(_umax % 10);
579 _umax /= 10;
580 }
581 *--cp = to_char(_umax);
582 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800583
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700584 case HEX:
585 do {
586 *--cp = xdigs[_umax & 15];
587 _umax >>= 4;
588 } while (_umax);
589 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800590
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700591 default:
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700592 abort();
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700593 }
594 }
595 size = buf + BUF - cp;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700596 if (size > BUF) abort(); /* should never happen */
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700597 break;
598 default: /* "%?" prints ?, unless ? is NUL */
599 if (ch == '\0') goto done;
600 /* pretend it was %c with argument ch */
601 cp = buf;
602 *cp = ch;
603 size = 1;
604 sign = '\0';
605 break;
606 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800607
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700608 /*
609 * All reasonable formats wind up here. At this point, `cp'
610 * points to a string which (if not flags&LADJUST) should be
611 * padded out to `width' places. If flags&ZEROPAD, it should
612 * first be prefixed by any sign or other prefix; otherwise,
613 * it should be blank padded before the prefix is emitted.
614 * After any left-hand padding and prefixing, emit zeroes
615 * required by a decimal %[diouxX] precision, then print the
616 * string proper, then emit zeroes required by any leftover
617 * floating precision; finally, if LADJUST, pad with blanks.
618 *
619 * Compute actual size, so we know how much to pad.
620 * size excludes decimal prec; realsz includes it.
621 */
622 realsz = dprec > size ? dprec : size;
623 if (sign) realsz++;
624 if (ox[1]) realsz += 2;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800625
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700626 /* right-adjusting blank padding */
627 if ((flags & (LADJUST | ZEROPAD)) == 0) PAD(width - realsz, blanks);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800628
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700629 /* prefix */
630 if (sign) PRINT(&sign, 1);
631 if (ox[1]) { /* ox[1] is either x, X, or \0 */
632 ox[0] = '0';
633 PRINT(ox, 2);
634 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800635
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700636 /* right-adjusting zero padding */
637 if ((flags & (LADJUST | ZEROPAD)) == ZEROPAD) PAD(width - realsz, zeroes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800638
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700639 /* leading zeroes from decimal precision */
640 PAD(dprec - size, zeroes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800641
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700642 /* the string or number proper */
643 if ((flags & FPT) == 0) {
644 PRINT(cp, size);
645 } else { /* glue together f_p fragments */
646 if (decimal_point == NULL) decimal_point = nl_langinfo(RADIXCHAR);
647 if (!expchar) { /* %[fF] or sufficiently short %[gG] */
648 if (expt <= 0) {
649 PRINT(zeroes, 1);
650 if (prec || flags & ALT) PRINT(decimal_point, 1);
651 PAD(-expt, zeroes);
652 /* already handled initial 0's */
653 prec += expt;
654 } else {
655 PRINTANDPAD(cp, dtoaend, lead, zeroes);
656 cp += lead;
657 if (prec || flags & ALT) PRINT(decimal_point, 1);
658 }
659 PRINTANDPAD(cp, dtoaend, prec, zeroes);
660 } else { /* %[eE] or sufficiently long %[gG] */
661 if (prec > 1 || flags & ALT) {
662 buf[0] = *cp++;
663 buf[1] = *decimal_point;
664 PRINT(buf, 2);
665 PRINT(cp, ndig - 1);
666 PAD(prec - ndig, zeroes);
667 } else { /* XeYYY */
668 PRINT(cp, 1);
669 }
670 PRINT(expstr, expsize);
671 }
672 }
673 /* left-adjusting padding (always blank) */
674 if (flags & LADJUST) PAD(width - realsz, blanks);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800675
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700676 /* finally, adjust ret */
677 if (width < realsz) width = realsz;
678 if (width > INT_MAX - ret) goto overflow;
679 ret += width;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800680
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700681 FLUSH(); /* copy out the I/O vectors */
682 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800683done:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700684 FLUSH();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800685error:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700686 va_end(orgap);
687 if (__sferror(fp)) ret = -1;
688 goto finish;
Elliott Hughes05493712014-04-17 17:30:03 -0700689
690overflow:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700691 errno = ENOMEM;
692 ret = -1;
Elliott Hughes05493712014-04-17 17:30:03 -0700693
694finish:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700695 free(convbuf);
696 if (dtoaresult) __freedtoa(dtoaresult);
697 if (argtable != NULL && argtable != statargtable) {
698 munmap(argtable, argtablesiz);
699 argtable = NULL;
700 }
701 return (ret);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800702}