blob: e1aecbc4d73b764668470794cdc2b4bbebcbbab1 [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 Hughesc8f2c522017-10-31 13:07:51 -070046 int ch; /* character from fmt */
47 int n, n2; /* handy integers (short term usage) */
Elliott Hughes93a1f8b2017-11-07 22:52:29 -080048 CHAR_TYPE* cp; /* handy char pointer (short term usage) */
Elliott Hughesc8f2c522017-10-31 13:07:51 -070049 struct __siov* iovp; /* for PRINT macro */
Elliott Hughes93a1f8b2017-11-07 22:52:29 -080050 CHAR_TYPE sign; /* sign prefix (' ', '+', '-', or \0) */
Elliott Hughesc8f2c522017-10-31 13:07:51 -070051 int flags; /* flags as above */
52 int ret; /* return value accumulator */
53 int width; /* width from format (%8d), or 0 */
54 int prec; /* precision from format; <0 for N/A */
Elliott Hughesc8f2c522017-10-31 13:07:51 -070055 /*
56 * We can decompose the printed representation of floating
57 * point numbers into several parts, some of which may be empty:
58 *
59 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
60 * A B ---C--- D E F
61 *
62 * A: 'sign' holds this value if present; '\0' otherwise
63 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
64 * C: cp points to the string MMMNNN. Leading and trailing
65 * zeros are not in the string and must be added.
66 * D: expchar holds this character; '\0' if no exponent, e.g. %f
67 * F: at least two digits for decimal, at least one digit for hex
68 */
69 char* decimal_point = NULL;
70 int signflag; /* true if float is negative */
71 union { /* floating point arguments %[aAeEfFgG] */
72 double dbl;
73 long double ldbl;
74 } fparg;
75 int expt; /* integer value of exponent */
76 char expchar; /* exponent character: [eEpP\0] */
77 char* dtoaend; /* pointer to end of converted digits */
78 int expsize; /* character count for expstr */
79 int lead; /* sig figs before decimal or group sep */
80 int ndig; /* actual number of digits returned by dtoa */
Elliott Hughes93a1f8b2017-11-07 22:52:29 -080081 CHAR_TYPE expstr[MAXEXPDIG + 2]; /* buffer for exponent string: e+ZZZ */
Elliott Hughesc8f2c522017-10-31 13:07:51 -070082 char* dtoaresult = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080083
Elliott Hughesc8f2c522017-10-31 13:07:51 -070084 uintmax_t _umax; /* integer arguments %[diouxX] */
85 enum { OCT, DEC, HEX } base; /* base for %[diouxX] conversion */
86 int dprec; /* a copy of prec if %[diouxX], 0 otherwise */
87 int realsz; /* field size expanded by dprec */
88 int size; /* size of converted field or string */
89 const char* xdigs; /* digits for %[xX] conversion */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080090#define NIOV 8
Elliott Hughesc8f2c522017-10-31 13:07:51 -070091 struct __suio uio; /* output information: summary */
92 struct __siov iov[NIOV]; /* ... and individual io vectors */
Elliott Hughes93a1f8b2017-11-07 22:52:29 -080093 CHAR_TYPE buf[BUF]; /* buffer with space for digits of uintmax_t */
94 CHAR_TYPE ox[2]; /* space for 0x; ox[1] is either x, X, or \0 */
Elliott Hughesc8f2c522017-10-31 13:07:51 -070095 union arg* argtable; /* args, built due to positional arg */
96 union arg statargtable[STATIC_ARG_TBL_SIZE];
97 size_t argtablesiz;
98 int nextarg; /* 1-based argument index */
99 va_list orgap; /* original argument pointer */
Elliott Hughes93a1f8b2017-11-07 22:52:29 -0800100 CHAR_TYPE* convbuf; /* buffer for wide/multibyte conversion */
Elliott Hughes05493712014-04-17 17:30:03 -0700101
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700102 /*
103 * Choose PADSIZE to trade efficiency vs. size. If larger printf
104 * fields occur frequently, increase PADSIZE and make the initialisers
105 * below longer.
106 */
107#define PADSIZE 16 /* pad chunk size */
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700108 static CHAR_TYPE blanks[PADSIZE] = {
109 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '
110 };
111 static CHAR_TYPE zeroes[PADSIZE] = {
112 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'
113 };
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800114
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700115 static const char xdigs_lower[] = "0123456789abcdef";
116 static const char xdigs_upper[] = "0123456789ABCDEF";
Elliott Hughes05493712014-04-17 17:30:03 -0700117
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700118 /*
119 * BEWARE, these `goto error' on error, and PAD uses `n'.
120 */
121#define PRINT(ptr, len) \
122 do { \
123 iovp->iov_base = (ptr); \
124 iovp->iov_len = (len); \
125 uio.uio_resid += (len); \
126 iovp++; \
127 if (++uio.uio_iovcnt >= NIOV) { \
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800128 if (helpers::sprint(fp, &uio)) goto error; \
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700129 iovp = iov; \
130 } \
131 } while (0)
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700132#define FLUSH() \
133 do { \
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800134 if (uio.uio_resid && helpers::sprint(fp, &uio)) goto error; \
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700135 uio.uio_iovcnt = 0; \
136 iovp = iov; \
137 } while (0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800138
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800139 _SET_ORIENTATION(fp, CHAR_TYPE_ORIENTATION);
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700140
141 // Writing "" to a read only file returns EOF, not 0.
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700142 if (cantwrite(fp)) {
143 errno = EBADF;
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700144 return EOF;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700145 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800146
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700147 // Optimize writes to stderr and other unbuffered files).
148 if ((fp->_flags & (__SNBF | __SWR | __SRW)) == (__SNBF | __SWR) && fp->_file >= 0) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700149 return (__sbprintf(fp, fmt0, ap));
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700150 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800151
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700152 CHAR_TYPE* fmt = const_cast<CHAR_TYPE*>(fmt0);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700153 argtable = NULL;
154 nextarg = 1;
155 va_copy(orgap, ap);
156 uio.uio_iov = iovp = iov;
157 uio.uio_resid = 0;
158 uio.uio_iovcnt = 0;
159 ret = 0;
160 convbuf = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800161
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700162 /*
163 * Scan the format for conversions (`%' character).
164 */
165 for (;;) {
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700166 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) continue;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700167 if (fmt != cp) {
168 ptrdiff_t m = fmt - cp;
169 if (m < 0 || m > INT_MAX - ret) goto overflow;
170 PRINT(cp, m);
171 ret += m;
172 }
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700173 if (ch == '\0') goto done;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700174 fmt++; /* skip over '%' */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800175
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700176 flags = 0;
177 dprec = 0;
178 width = 0;
179 prec = -1;
180 sign = '\0';
181 ox[1] = '\0';
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800182
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700183 rflag:
184 ch = *fmt++;
185 reswitch:
186 switch (ch) {
187 case ' ':
188 /*
189 * ``If the space and + flags both appear, the space
190 * flag will be ignored.''
191 * -- ANSI X3J11
192 */
193 if (!sign) sign = ' ';
194 goto rflag;
195 case '#':
196 flags |= ALT;
197 goto rflag;
198 case '\'':
199 /* grouping not implemented */
200 goto rflag;
201 case '*':
202 /*
203 * ``A negative field width argument is taken as a
204 * - flag followed by a positive field width.''
205 * -- ANSI X3J11
206 * They don't exclude field widths read from args.
207 */
208 GETASTER(width);
209 if (width >= 0) goto rflag;
210 if (width == INT_MIN) goto overflow;
211 width = -width;
212 /* FALLTHROUGH */
213 case '-':
214 flags |= LADJUST;
215 goto rflag;
216 case '+':
217 sign = '+';
218 goto rflag;
219 case '.':
220 if ((ch = *fmt++) == '*') {
221 GETASTER(n);
222 prec = n < 0 ? -1 : n;
223 goto rflag;
224 }
225 n = 0;
226 while (is_digit(ch)) {
227 APPEND_DIGIT(n, ch);
228 ch = *fmt++;
229 }
230 if (ch == '$') {
231 nextarg = n;
232 if (argtable == NULL) {
233 argtable = statargtable;
234 if (__find_arguments(fmt0, orgap, &argtable, &argtablesiz) == -1) {
235 ret = -1;
236 goto error;
237 }
238 }
239 goto rflag;
240 }
241 prec = n;
242 goto reswitch;
243 case '0':
244 /*
245 * ``Note that 0 is taken as a flag, not as the
246 * beginning of a field width.''
247 * -- ANSI X3J11
248 */
249 flags |= ZEROPAD;
250 goto rflag;
251 case '1':
252 case '2':
253 case '3':
254 case '4':
255 case '5':
256 case '6':
257 case '7':
258 case '8':
259 case '9':
260 n = 0;
261 do {
262 APPEND_DIGIT(n, ch);
263 ch = *fmt++;
264 } while (is_digit(ch));
265 if (ch == '$') {
266 nextarg = n;
267 if (argtable == NULL) {
268 argtable = statargtable;
269 if (__find_arguments(fmt0, orgap, &argtable, &argtablesiz) == -1) {
270 ret = -1;
271 goto error;
272 }
273 }
274 goto rflag;
275 }
276 width = n;
277 goto reswitch;
278 case 'L':
279 flags |= LONGDBL;
280 goto rflag;
281 case 'h':
282 if (*fmt == 'h') {
283 fmt++;
284 flags |= CHARINT;
285 } else {
286 flags |= SHORTINT;
287 }
288 goto rflag;
289 case 'j':
290 flags |= MAXINT;
291 goto rflag;
292 case 'l':
293 if (*fmt == 'l') {
294 fmt++;
295 flags |= LLONGINT;
296 } else {
297 flags |= LONGINT;
298 }
299 goto rflag;
300 case 'q':
301 flags |= LLONGINT;
302 goto rflag;
303 case 't':
304 flags |= PTRINT;
305 goto rflag;
306 case 'z':
307 flags |= SIZEINT;
308 goto rflag;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700309 case 'C':
310 flags |= LONGINT;
311 /*FALLTHROUGH*/
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700312 case 'c':
313 if (flags & LONGINT) {
314 mbstate_t mbs;
315 size_t mbseqlen;
Elliott Hughes05493712014-04-17 17:30:03 -0700316
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700317 memset(&mbs, 0, sizeof(mbs));
318 mbseqlen = wcrtomb(buf, (wchar_t)GETARG(wint_t), &mbs);
319 if (mbseqlen == (size_t)-1) {
320 ret = -1;
321 goto error;
322 }
323 cp = buf;
324 size = (int)mbseqlen;
325 } else {
326 *(cp = buf) = GETARG(int);
327 size = 1;
328 }
329 sign = '\0';
330 break;
331 case 'D':
332 flags |= LONGINT;
333 /*FALLTHROUGH*/
334 case 'd':
335 case 'i':
336 _umax = SARG();
337 if ((intmax_t)_umax < 0) {
338 _umax = -_umax;
339 sign = '-';
340 }
341 base = DEC;
342 goto number;
343 case 'a':
344 case 'A':
345 if (ch == 'a') {
346 ox[1] = 'x';
347 xdigs = xdigs_lower;
348 expchar = 'p';
349 } else {
350 ox[1] = 'X';
351 xdigs = xdigs_upper;
352 expchar = 'P';
353 }
354 if (prec >= 0) prec++;
355 if (dtoaresult) __freedtoa(dtoaresult);
356 if (flags & LONGDBL) {
357 fparg.ldbl = GETARG(long double);
358 dtoaresult = cp = __hldtoa(fparg.ldbl, xdigs, prec, &expt, &signflag, &dtoaend);
359 if (dtoaresult == NULL) {
360 errno = ENOMEM;
361 goto error;
362 }
363 } else {
364 fparg.dbl = GETARG(double);
365 dtoaresult = cp = __hdtoa(fparg.dbl, xdigs, prec, &expt, &signflag, &dtoaend);
366 if (dtoaresult == NULL) {
367 errno = ENOMEM;
368 goto error;
369 }
370 }
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800371 if (prec < 0) prec = dtoaend - dtoaresult;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700372 if (expt == INT_MAX) ox[1] = '\0';
373 goto fp_common;
374 case 'e':
375 case 'E':
376 expchar = ch;
377 if (prec < 0) /* account for digit before decpt */
378 prec = DEFPREC + 1;
379 else
380 prec++;
381 goto fp_begin;
382 case 'f':
383 case 'F':
384 expchar = '\0';
385 goto fp_begin;
386 case 'g':
387 case 'G':
388 expchar = ch - ('g' - 'e');
389 if (prec == 0) prec = 1;
390 fp_begin:
391 if (prec < 0) prec = DEFPREC;
392 if (dtoaresult) __freedtoa(dtoaresult);
393 if (flags & LONGDBL) {
394 fparg.ldbl = GETARG(long double);
395 dtoaresult = cp = __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec, &expt, &signflag, &dtoaend);
396 if (dtoaresult == NULL) {
397 errno = ENOMEM;
398 goto error;
399 }
400 } else {
401 fparg.dbl = GETARG(double);
402 dtoaresult = cp = __dtoa(fparg.dbl, expchar ? 2 : 3, prec, &expt, &signflag, &dtoaend);
403 if (dtoaresult == NULL) {
404 errno = ENOMEM;
405 goto error;
406 }
407 if (expt == 9999) expt = INT_MAX;
408 }
409 fp_common:
410 if (signflag) sign = '-';
411 if (expt == INT_MAX) { /* inf or nan */
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700412 if (*cp == 'N') {
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800413 cp = const_cast<CHAR_TYPE*>((ch >= 'a') ? CHAR_TYPE_nan : CHAR_TYPE_NAN);
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700414 } else {
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800415 cp = const_cast<CHAR_TYPE*>((ch >= 'a') ? CHAR_TYPE_inf : CHAR_TYPE_INF);
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700416 }
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700417 size = 3;
418 flags &= ~ZEROPAD;
419 break;
420 }
421 flags |= FPT;
422 ndig = dtoaend - cp;
423 if (ch == 'g' || ch == 'G') {
424 if (expt > -4 && expt <= prec) {
425 /* Make %[gG] smell like %[fF] */
426 expchar = '\0';
427 if (flags & ALT)
428 prec -= expt;
429 else
430 prec = ndig - expt;
431 if (prec < 0) prec = 0;
432 } else {
433 /*
434 * Make %[gG] smell like %[eE], but
435 * trim trailing zeroes if no # flag.
436 */
437 if (!(flags & ALT)) prec = ndig;
438 }
439 }
440 if (expchar) {
441 expsize = exponent(expstr, expt - 1, expchar);
442 size = expsize + prec;
443 if (prec > 1 || flags & ALT) ++size;
444 } else {
445 /* space for digits before decimal point */
446 if (expt > 0)
447 size = expt;
448 else /* "0" */
449 size = 1;
450 /* space for decimal pt and following digits */
451 if (prec || flags & ALT) size += prec + 1;
452 lead = expt;
453 }
454 break;
Elliott Hughese2341d02014-05-02 18:16:32 -0700455#ifndef NO_PRINTF_PERCENT_N
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700456 case 'n':
457 if (flags & LLONGINT)
458 *GETARG(long long*) = ret;
459 else if (flags & LONGINT)
460 *GETARG(long*) = ret;
461 else if (flags & SHORTINT)
462 *GETARG(short*) = ret;
463 else if (flags & CHARINT)
464 *GETARG(signed char*) = ret;
465 else if (flags & PTRINT)
466 *GETARG(ptrdiff_t*) = ret;
467 else if (flags & SIZEINT)
468 *GETARG(ssize_t*) = ret;
469 else if (flags & MAXINT)
470 *GETARG(intmax_t*) = ret;
471 else
472 *GETARG(int*) = ret;
473 continue; /* no output */
474#endif /* NO_PRINTF_PERCENT_N */
475 case 'O':
476 flags |= LONGINT;
477 /*FALLTHROUGH*/
478 case 'o':
479 _umax = UARG();
480 base = OCT;
481 goto nosign;
482 case 'p':
483 /*
484 * ``The argument shall be a pointer to void. The
485 * value of the pointer is converted to a sequence
486 * of printable characters, in an implementation-
487 * defined manner.''
488 * -- ANSI X3J11
489 */
490 _umax = (u_long)GETARG(void*);
491 base = HEX;
492 xdigs = xdigs_lower;
493 ox[1] = 'x';
494 goto nosign;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700495 case 'S':
496 flags |= LONGINT;
497 /*FALLTHROUGH*/
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700498 case 's':
499 if (flags & LONGINT) {
500 wchar_t* wcp;
Elliott Hughes05493712014-04-17 17:30:03 -0700501
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700502 free(convbuf);
503 convbuf = NULL;
504 if ((wcp = GETARG(wchar_t*)) == NULL) {
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700505 cp = const_cast<char*>("(null)");
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700506 } else {
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800507 convbuf = helpers::wcsconv(wcp, prec);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700508 if (convbuf == NULL) {
509 ret = -1;
510 goto error;
511 }
512 cp = convbuf;
513 }
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700514 } else if ((cp = GETARG(char*)) == NULL) {
515 cp = const_cast<char*>("(null)");
516 }
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700517 if (prec >= 0) {
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800518 size = CHAR_TYPE_STRNLEN(cp, prec);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700519 } else {
520 size_t len;
Elliott Hughes05493712014-04-17 17:30:03 -0700521
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800522 if ((len = CHAR_TYPE_STRLEN(cp)) > INT_MAX) goto overflow;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700523 size = (int)len;
524 }
525 sign = '\0';
526 break;
527 case 'U':
528 flags |= LONGINT;
529 /*FALLTHROUGH*/
530 case 'u':
531 _umax = UARG();
532 base = DEC;
533 goto nosign;
534 case 'X':
535 xdigs = xdigs_upper;
536 goto hex;
537 case 'x':
538 xdigs = xdigs_lower;
539 hex:
540 _umax = UARG();
541 base = HEX;
542 /* leading 0x/X only if non-zero */
543 if (flags & ALT && _umax != 0) ox[1] = ch;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800544
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700545 /* unsigned conversions */
546 nosign:
547 sign = '\0';
548 /*
549 * ``... diouXx conversions ... if a precision is
550 * specified, the 0 flag will be ignored.''
551 * -- ANSI X3J11
552 */
553 number:
554 if ((dprec = prec) >= 0) flags &= ~ZEROPAD;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800555
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700556 /*
557 * ``The result of converting a zero value with an
558 * explicit precision of zero is no characters.''
559 * -- ANSI X3J11
560 */
561 cp = buf + BUF;
562 if (_umax != 0 || prec != 0) {
563 /*
564 * Unsigned mod is hard, and unsigned mod
565 * by a constant is easier than that by
566 * a variable; hence this switch.
567 */
568 switch (base) {
569 case OCT:
570 do {
571 *--cp = to_char(_umax & 7);
572 _umax >>= 3;
573 } while (_umax);
574 /* handle octal leading 0 */
575 if (flags & ALT && *cp != '0') *--cp = '0';
576 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800577
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700578 case DEC:
579 /* many numbers are 1 digit */
580 while (_umax >= 10) {
581 *--cp = to_char(_umax % 10);
582 _umax /= 10;
583 }
584 *--cp = to_char(_umax);
585 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800586
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700587 case HEX:
588 do {
589 *--cp = xdigs[_umax & 15];
590 _umax >>= 4;
591 } while (_umax);
592 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800593
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700594 default:
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700595 abort();
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700596 }
597 }
598 size = buf + BUF - cp;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700599 if (size > BUF) abort(); /* should never happen */
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700600 break;
601 default: /* "%?" prints ?, unless ? is NUL */
602 if (ch == '\0') goto done;
603 /* pretend it was %c with argument ch */
604 cp = buf;
605 *cp = ch;
606 size = 1;
607 sign = '\0';
608 break;
609 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800610
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700611 /*
612 * All reasonable formats wind up here. At this point, `cp'
613 * points to a string which (if not flags&LADJUST) should be
614 * padded out to `width' places. If flags&ZEROPAD, it should
615 * first be prefixed by any sign or other prefix; otherwise,
616 * it should be blank padded before the prefix is emitted.
617 * After any left-hand padding and prefixing, emit zeroes
618 * required by a decimal %[diouxX] precision, then print the
619 * string proper, then emit zeroes required by any leftover
620 * floating precision; finally, if LADJUST, pad with blanks.
621 *
622 * Compute actual size, so we know how much to pad.
623 * size excludes decimal prec; realsz includes it.
624 */
625 realsz = dprec > size ? dprec : size;
626 if (sign) realsz++;
627 if (ox[1]) realsz += 2;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800628
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700629 /* right-adjusting blank padding */
630 if ((flags & (LADJUST | ZEROPAD)) == 0) PAD(width - realsz, blanks);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800631
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700632 /* prefix */
633 if (sign) PRINT(&sign, 1);
634 if (ox[1]) { /* ox[1] is either x, X, or \0 */
635 ox[0] = '0';
636 PRINT(ox, 2);
637 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800638
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700639 /* right-adjusting zero padding */
640 if ((flags & (LADJUST | ZEROPAD)) == ZEROPAD) PAD(width - realsz, zeroes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800641
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700642 /* leading zeroes from decimal precision */
643 PAD(dprec - size, zeroes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800644
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700645 /* the string or number proper */
646 if ((flags & FPT) == 0) {
647 PRINT(cp, size);
648 } else { /* glue together f_p fragments */
649 if (decimal_point == NULL) decimal_point = nl_langinfo(RADIXCHAR);
650 if (!expchar) { /* %[fF] or sufficiently short %[gG] */
651 if (expt <= 0) {
652 PRINT(zeroes, 1);
653 if (prec || flags & ALT) PRINT(decimal_point, 1);
654 PAD(-expt, zeroes);
655 /* already handled initial 0's */
656 prec += expt;
657 } else {
658 PRINTANDPAD(cp, dtoaend, lead, zeroes);
659 cp += lead;
660 if (prec || flags & ALT) PRINT(decimal_point, 1);
661 }
662 PRINTANDPAD(cp, dtoaend, prec, zeroes);
663 } else { /* %[eE] or sufficiently long %[gG] */
664 if (prec > 1 || flags & ALT) {
665 buf[0] = *cp++;
666 buf[1] = *decimal_point;
667 PRINT(buf, 2);
668 PRINT(cp, ndig - 1);
669 PAD(prec - ndig, zeroes);
670 } else { /* XeYYY */
671 PRINT(cp, 1);
672 }
673 PRINT(expstr, expsize);
674 }
675 }
676 /* left-adjusting padding (always blank) */
677 if (flags & LADJUST) PAD(width - realsz, blanks);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800678
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700679 /* finally, adjust ret */
680 if (width < realsz) width = realsz;
681 if (width > INT_MAX - ret) goto overflow;
682 ret += width;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800683
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700684 FLUSH(); /* copy out the I/O vectors */
685 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800686done:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700687 FLUSH();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800688error:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700689 va_end(orgap);
690 if (__sferror(fp)) ret = -1;
691 goto finish;
Elliott Hughes05493712014-04-17 17:30:03 -0700692
693overflow:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700694 errno = ENOMEM;
695 ret = -1;
Elliott Hughes05493712014-04-17 17:30:03 -0700696
697finish:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700698 free(convbuf);
699 if (dtoaresult) __freedtoa(dtoaresult);
700 if (argtable != NULL && argtable != statargtable) {
701 munmap(argtable, argtablesiz);
702 argtable = NULL;
703 }
704 return (ret);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800705}