blob: 994269b7e24d22cc90d2031bc4dcba0a263143d7 [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 Hughes654cd832018-08-30 16:00:42 -070046 int caller_errno = errno;
Elliott Hughesb70576b2017-11-13 11:10:05 -080047 int n, n2;
Elliott Hughes93a1f8b2017-11-07 22:52:29 -080048 CHAR_TYPE* cp; /* handy char pointer (short term usage) */
Elliott Hughes93a1f8b2017-11-07 22:52:29 -080049 CHAR_TYPE sign; /* sign prefix (' ', '+', '-', or \0) */
Elliott Hughesc8f2c522017-10-31 13:07:51 -070050 int flags; /* flags as above */
51 int ret; /* return value accumulator */
52 int width; /* width from format (%8d), or 0 */
53 int prec; /* precision from format; <0 for N/A */
Elliott Hughesc8f2c522017-10-31 13:07:51 -070054 /*
55 * We can decompose the printed representation of floating
56 * point numbers into several parts, some of which may be empty:
57 *
58 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
59 * A B ---C--- D E F
60 *
61 * A: 'sign' holds this value if present; '\0' otherwise
62 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
63 * C: cp points to the string MMMNNN. Leading and trailing
64 * zeros are not in the string and must be added.
65 * D: expchar holds this character; '\0' if no exponent, e.g. %f
66 * F: at least two digits for decimal, at least one digit for hex
67 */
Yi Kong32bc0fc2018-08-02 17:31:13 -070068 char* decimal_point = nullptr;
Elliott Hughesc8f2c522017-10-31 13:07:51 -070069 int signflag; /* true if float is negative */
70 union { /* floating point arguments %[aAeEfFgG] */
71 double dbl;
72 long double ldbl;
73 } fparg;
74 int expt; /* integer value of exponent */
75 char expchar; /* exponent character: [eEpP\0] */
76 char* dtoaend; /* pointer to end of converted digits */
77 int expsize; /* character count for expstr */
78 int lead; /* sig figs before decimal or group sep */
79 int ndig; /* actual number of digits returned by dtoa */
Elliott Hughes93a1f8b2017-11-07 22:52:29 -080080 CHAR_TYPE expstr[MAXEXPDIG + 2]; /* buffer for exponent string: e+ZZZ */
Yi Kong32bc0fc2018-08-02 17:31:13 -070081 char* dtoaresult = nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080082
Elliott Hughesc8f2c522017-10-31 13:07:51 -070083 uintmax_t _umax; /* integer arguments %[diouxX] */
Elliott Hughesb813a6a2022-08-01 22:18:40 +000084 enum { BIN, OCT, DEC, HEX } base; /* base for %[bBdiouxX] conversion */
85 int dprec; /* a copy of prec if %[bBdiouxX], 0 otherwise */
Elliott Hughesc8f2c522017-10-31 13:07:51 -070086 int realsz; /* field size expanded by dprec */
87 int size; /* size of converted field or string */
88 const char* xdigs; /* digits for %[xX] conversion */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080089#define NIOV 8
Elliott Hughesc8f2c522017-10-31 13:07:51 -070090 struct __suio uio; /* output information: summary */
91 struct __siov iov[NIOV]; /* ... and individual io vectors */
Elliott Hughesb70576b2017-11-13 11:10:05 -080092 struct __siov* iovp; /* for PRINT macro */
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#define PRINT(ptr, len) \
119 do { \
120 iovp->iov_base = (ptr); \
121 iovp->iov_len = (len); \
122 uio.uio_resid += (len); \
123 iovp++; \
124 if (++uio.uio_iovcnt >= NIOV) { \
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800125 if (helpers::sprint(fp, &uio)) goto error; \
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700126 iovp = iov; \
127 } \
128 } while (0)
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700129#define FLUSH() \
130 do { \
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800131 if (uio.uio_resid && helpers::sprint(fp, &uio)) goto error; \
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700132 uio.uio_iovcnt = 0; \
133 iovp = iov; \
134 } while (0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800135
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800136 _SET_ORIENTATION(fp, CHAR_TYPE_ORIENTATION);
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700137
138 // Writing "" to a read only file returns EOF, not 0.
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700139 if (cantwrite(fp)) {
140 errno = EBADF;
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700141 return EOF;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700142 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800143
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700144 // Optimize writes to stderr and other unbuffered files).
145 if ((fp->_flags & (__SNBF | __SWR | __SRW)) == (__SNBF | __SWR) && fp->_file >= 0) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700146 return (__sbprintf(fp, fmt0, ap));
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700147 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800148
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700149 CHAR_TYPE* fmt = const_cast<CHAR_TYPE*>(fmt0);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700150 argtable = nullptr;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700151 nextarg = 1;
152 va_copy(orgap, ap);
153 uio.uio_iov = iovp = iov;
154 uio.uio_resid = 0;
155 uio.uio_iovcnt = 0;
156 ret = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700157 convbuf = nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800158
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700159 /*
160 * Scan the format for conversions (`%' character).
161 */
162 for (;;) {
Elliott Hughesb70576b2017-11-13 11:10:05 -0800163 int ch;
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700164 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) continue;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700165 if (fmt != cp) {
166 ptrdiff_t m = fmt - cp;
167 if (m < 0 || m > INT_MAX - ret) goto overflow;
168 PRINT(cp, m);
169 ret += m;
170 }
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700171 if (ch == '\0') goto done;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700172 fmt++; /* skip over '%' */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800173
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700174 flags = 0;
175 dprec = 0;
176 width = 0;
177 prec = -1;
178 sign = '\0';
179 ox[1] = '\0';
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800180
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700181 rflag:
182 ch = *fmt++;
183 reswitch:
184 switch (ch) {
185 case ' ':
186 /*
187 * ``If the space and + flags both appear, the space
188 * flag will be ignored.''
189 * -- ANSI X3J11
190 */
191 if (!sign) sign = ' ';
192 goto rflag;
193 case '#':
194 flags |= ALT;
195 goto rflag;
196 case '\'':
197 /* grouping not implemented */
198 goto rflag;
199 case '*':
200 /*
201 * ``A negative field width argument is taken as a
202 * - flag followed by a positive field width.''
203 * -- ANSI X3J11
204 * They don't exclude field widths read from args.
205 */
206 GETASTER(width);
207 if (width >= 0) goto rflag;
208 if (width == INT_MIN) goto overflow;
209 width = -width;
George Burgess IVfa5410f2018-08-13 17:44:06 -0700210 __BIONIC_FALLTHROUGH;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700211 case '-':
212 flags |= LADJUST;
213 goto rflag;
214 case '+':
215 sign = '+';
216 goto rflag;
217 case '.':
218 if ((ch = *fmt++) == '*') {
219 GETASTER(n);
220 prec = n < 0 ? -1 : n;
221 goto rflag;
222 }
223 n = 0;
224 while (is_digit(ch)) {
225 APPEND_DIGIT(n, ch);
226 ch = *fmt++;
227 }
228 if (ch == '$') {
229 nextarg = n;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700230 if (argtable == nullptr) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700231 argtable = statargtable;
232 if (__find_arguments(fmt0, orgap, &argtable, &argtablesiz) == -1) {
233 ret = -1;
234 goto error;
235 }
236 }
237 goto rflag;
238 }
239 prec = n;
240 goto reswitch;
241 case '0':
242 /*
243 * ``Note that 0 is taken as a flag, not as the
244 * beginning of a field width.''
245 * -- ANSI X3J11
246 */
247 flags |= ZEROPAD;
248 goto rflag;
249 case '1':
250 case '2':
251 case '3':
252 case '4':
253 case '5':
254 case '6':
255 case '7':
256 case '8':
257 case '9':
258 n = 0;
259 do {
260 APPEND_DIGIT(n, ch);
261 ch = *fmt++;
262 } while (is_digit(ch));
263 if (ch == '$') {
264 nextarg = n;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700265 if (argtable == nullptr) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700266 argtable = statargtable;
267 if (__find_arguments(fmt0, orgap, &argtable, &argtablesiz) == -1) {
268 ret = -1;
269 goto error;
270 }
271 }
272 goto rflag;
273 }
274 width = n;
275 goto reswitch;
276 case 'L':
277 flags |= LONGDBL;
278 goto rflag;
279 case 'h':
280 if (*fmt == 'h') {
281 fmt++;
282 flags |= CHARINT;
283 } else {
284 flags |= SHORTINT;
285 }
286 goto rflag;
287 case 'j':
288 flags |= MAXINT;
289 goto rflag;
290 case 'l':
291 if (*fmt == 'l') {
292 fmt++;
293 flags |= LLONGINT;
294 } else {
295 flags |= LONGINT;
296 }
297 goto rflag;
298 case 'q':
299 flags |= LLONGINT;
300 goto rflag;
301 case 't':
302 flags |= PTRINT;
303 goto rflag;
304 case 'z':
305 flags |= SIZEINT;
306 goto rflag;
Elliott Hughesb813a6a2022-08-01 22:18:40 +0000307 case 'B':
308 case 'b':
309 _umax = UARG();
310 base = BIN;
311 if (flags & ALT && _umax != 0) ox[1] = ch;
312 goto nosign;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700313 case 'C':
314 flags |= LONGINT;
George Burgess IVfa5410f2018-08-13 17:44:06 -0700315 __BIONIC_FALLTHROUGH;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700316 case 'c':
317 if (flags & LONGINT) {
318 mbstate_t mbs;
319 size_t mbseqlen;
Elliott Hughes05493712014-04-17 17:30:03 -0700320
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700321 memset(&mbs, 0, sizeof(mbs));
322 mbseqlen = wcrtomb(buf, (wchar_t)GETARG(wint_t), &mbs);
323 if (mbseqlen == (size_t)-1) {
324 ret = -1;
325 goto error;
326 }
327 cp = buf;
328 size = (int)mbseqlen;
329 } else {
330 *(cp = buf) = GETARG(int);
331 size = 1;
332 }
333 sign = '\0';
334 break;
335 case 'D':
336 flags |= LONGINT;
George Burgess IVfa5410f2018-08-13 17:44:06 -0700337 __BIONIC_FALLTHROUGH;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700338 case 'd':
339 case 'i':
340 _umax = SARG();
341 if ((intmax_t)_umax < 0) {
342 _umax = -_umax;
343 sign = '-';
344 }
345 base = DEC;
346 goto number;
347 case 'a':
348 case 'A':
349 if (ch == 'a') {
350 ox[1] = 'x';
351 xdigs = xdigs_lower;
352 expchar = 'p';
353 } else {
354 ox[1] = 'X';
355 xdigs = xdigs_upper;
356 expchar = 'P';
357 }
358 if (prec >= 0) prec++;
359 if (dtoaresult) __freedtoa(dtoaresult);
360 if (flags & LONGDBL) {
361 fparg.ldbl = GETARG(long double);
362 dtoaresult = cp = __hldtoa(fparg.ldbl, xdigs, prec, &expt, &signflag, &dtoaend);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700363 if (dtoaresult == nullptr) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700364 errno = ENOMEM;
365 goto error;
366 }
367 } else {
368 fparg.dbl = GETARG(double);
369 dtoaresult = cp = __hdtoa(fparg.dbl, xdigs, prec, &expt, &signflag, &dtoaend);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700370 if (dtoaresult == nullptr) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700371 errno = ENOMEM;
372 goto error;
373 }
374 }
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800375 if (prec < 0) prec = dtoaend - dtoaresult;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700376 if (expt == INT_MAX) ox[1] = '\0';
377 goto fp_common;
378 case 'e':
379 case 'E':
380 expchar = ch;
381 if (prec < 0) /* account for digit before decpt */
382 prec = DEFPREC + 1;
383 else
384 prec++;
385 goto fp_begin;
386 case 'f':
387 case 'F':
388 expchar = '\0';
389 goto fp_begin;
390 case 'g':
391 case 'G':
392 expchar = ch - ('g' - 'e');
393 if (prec == 0) prec = 1;
394 fp_begin:
395 if (prec < 0) prec = DEFPREC;
396 if (dtoaresult) __freedtoa(dtoaresult);
397 if (flags & LONGDBL) {
398 fparg.ldbl = GETARG(long double);
399 dtoaresult = cp = __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec, &expt, &signflag, &dtoaend);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700400 if (dtoaresult == nullptr) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700401 errno = ENOMEM;
402 goto error;
403 }
404 } else {
405 fparg.dbl = GETARG(double);
406 dtoaresult = cp = __dtoa(fparg.dbl, expchar ? 2 : 3, prec, &expt, &signflag, &dtoaend);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700407 if (dtoaresult == nullptr) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700408 errno = ENOMEM;
409 goto error;
410 }
411 if (expt == 9999) expt = INT_MAX;
412 }
413 fp_common:
414 if (signflag) sign = '-';
415 if (expt == INT_MAX) { /* inf or nan */
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700416 if (*cp == 'N') {
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800417 cp = const_cast<CHAR_TYPE*>((ch >= 'a') ? CHAR_TYPE_nan : CHAR_TYPE_NAN);
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700418 } else {
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800419 cp = const_cast<CHAR_TYPE*>((ch >= 'a') ? CHAR_TYPE_inf : CHAR_TYPE_INF);
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700420 }
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700421 size = 3;
422 flags &= ~ZEROPAD;
423 break;
424 }
425 flags |= FPT;
426 ndig = dtoaend - cp;
427 if (ch == 'g' || ch == 'G') {
428 if (expt > -4 && expt <= prec) {
429 /* Make %[gG] smell like %[fF] */
430 expchar = '\0';
431 if (flags & ALT)
432 prec -= expt;
433 else
434 prec = ndig - expt;
435 if (prec < 0) prec = 0;
436 } else {
437 /*
438 * Make %[gG] smell like %[eE], but
439 * trim trailing zeroes if no # flag.
440 */
441 if (!(flags & ALT)) prec = ndig;
442 }
443 }
444 if (expchar) {
445 expsize = exponent(expstr, expt - 1, expchar);
446 size = expsize + prec;
447 if (prec > 1 || flags & ALT) ++size;
448 } else {
449 /* space for digits before decimal point */
450 if (expt > 0)
451 size = expt;
452 else /* "0" */
453 size = 1;
454 /* space for decimal pt and following digits */
455 if (prec || flags & ALT) size += prec + 1;
456 lead = expt;
457 }
458 break;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700459 case 'n':
Elliott Hughes41398d02018-03-07 13:32:58 -0800460 __fortify_fatal("%%n not allowed on Android");
Elliott Hughes654cd832018-08-30 16:00:42 -0700461 case 'm':
Elliott Hughesf340a562018-09-06 10:42:40 -0700462 cp = strerror_r(caller_errno, buf, sizeof(buf));
Elliott Hughes654cd832018-08-30 16:00:42 -0700463 goto string;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700464 case 'O':
465 flags |= LONGINT;
George Burgess IVfa5410f2018-08-13 17:44:06 -0700466 __BIONIC_FALLTHROUGH;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700467 case 'o':
468 _umax = UARG();
469 base = OCT;
470 goto nosign;
471 case 'p':
472 /*
473 * ``The argument shall be a pointer to void. The
474 * value of the pointer is converted to a sequence
475 * of printable characters, in an implementation-
476 * defined manner.''
477 * -- ANSI X3J11
478 */
479 _umax = (u_long)GETARG(void*);
480 base = HEX;
481 xdigs = xdigs_lower;
482 ox[1] = 'x';
483 goto nosign;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700484 case 'S':
485 flags |= LONGINT;
George Burgess IVfa5410f2018-08-13 17:44:06 -0700486 __BIONIC_FALLTHROUGH;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700487 case 's':
488 if (flags & LONGINT) {
489 wchar_t* wcp;
Elliott Hughes05493712014-04-17 17:30:03 -0700490
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700491 free(convbuf);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700492 convbuf = nullptr;
493 if ((wcp = GETARG(wchar_t*)) == nullptr) {
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700494 cp = const_cast<char*>("(null)");
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700495 } else {
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800496 convbuf = helpers::wcsconv(wcp, prec);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700497 if (convbuf == nullptr) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700498 ret = -1;
499 goto error;
500 }
501 cp = convbuf;
502 }
Yi Kong32bc0fc2018-08-02 17:31:13 -0700503 } else if ((cp = GETARG(char*)) == nullptr) {
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700504 cp = const_cast<char*>("(null)");
505 }
Elliott Hughes654cd832018-08-30 16:00:42 -0700506 string:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700507 if (prec >= 0) {
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800508 size = CHAR_TYPE_STRNLEN(cp, prec);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700509 } else {
510 size_t len;
Elliott Hughes05493712014-04-17 17:30:03 -0700511
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800512 if ((len = CHAR_TYPE_STRLEN(cp)) > INT_MAX) goto overflow;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700513 size = (int)len;
514 }
515 sign = '\0';
516 break;
517 case 'U':
518 flags |= LONGINT;
George Burgess IVfa5410f2018-08-13 17:44:06 -0700519 __BIONIC_FALLTHROUGH;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700520 case 'u':
521 _umax = UARG();
522 base = DEC;
523 goto nosign;
zijunzhao1fdece92023-04-25 17:37:19 +0000524 case 'w': {
zijunzhao3b846ea2023-04-11 20:53:39 +0000525 n = 0;
zijunzhao1fdece92023-04-25 17:37:19 +0000526 bool fast = false;
zijunzhao3b846ea2023-04-11 20:53:39 +0000527 ch = *fmt++;
zijunzhao1fdece92023-04-25 17:37:19 +0000528 if (ch == 'f') {
529 fast = true;
530 ch = *fmt++;
531 }
zijunzhao3b846ea2023-04-11 20:53:39 +0000532 while (is_digit(ch)) {
533 APPEND_DIGIT(n, ch);
534 ch = *fmt++;
535 }
zijunzhao1fdece92023-04-25 17:37:19 +0000536 flags |= helpers::w_to_flag(n, fast);
537 goto reswitch;
538 }
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700539 case 'X':
540 xdigs = xdigs_upper;
541 goto hex;
542 case 'x':
543 xdigs = xdigs_lower;
544 hex:
545 _umax = UARG();
546 base = HEX;
547 /* leading 0x/X only if non-zero */
548 if (flags & ALT && _umax != 0) ox[1] = ch;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800549
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700550 /* unsigned conversions */
551 nosign:
552 sign = '\0';
553 /*
554 * ``... diouXx conversions ... if a precision is
555 * specified, the 0 flag will be ignored.''
556 * -- ANSI X3J11
557 */
558 number:
559 if ((dprec = prec) >= 0) flags &= ~ZEROPAD;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800560
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700561 /*
562 * ``The result of converting a zero value with an
563 * explicit precision of zero is no characters.''
564 * -- ANSI X3J11
565 */
566 cp = buf + BUF;
567 if (_umax != 0 || prec != 0) {
568 /*
569 * Unsigned mod is hard, and unsigned mod
570 * by a constant is easier than that by
571 * a variable; hence this switch.
572 */
573 switch (base) {
Elliott Hughesb813a6a2022-08-01 22:18:40 +0000574 case BIN:
575 do {
576 *--cp = to_char(_umax & 1);
577 _umax >>= 1;
578 } while (_umax);
579 break;
580
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700581 case OCT:
582 do {
583 *--cp = to_char(_umax & 7);
584 _umax >>= 3;
585 } while (_umax);
586 /* handle octal leading 0 */
587 if (flags & ALT && *cp != '0') *--cp = '0';
588 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800589
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700590 case DEC:
591 /* many numbers are 1 digit */
592 while (_umax >= 10) {
593 *--cp = to_char(_umax % 10);
594 _umax /= 10;
595 }
596 *--cp = to_char(_umax);
597 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800598
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700599 case HEX:
600 do {
601 *--cp = xdigs[_umax & 15];
602 _umax >>= 4;
603 } while (_umax);
604 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800605
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700606 default:
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700607 abort();
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700608 }
609 }
610 size = buf + BUF - cp;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700611 if (size > BUF) abort(); /* should never happen */
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700612 break;
613 default: /* "%?" prints ?, unless ? is NUL */
614 if (ch == '\0') goto done;
615 /* pretend it was %c with argument ch */
616 cp = buf;
617 *cp = ch;
618 size = 1;
619 sign = '\0';
620 break;
621 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800622
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700623 /*
624 * All reasonable formats wind up here. At this point, `cp'
625 * points to a string which (if not flags&LADJUST) should be
626 * padded out to `width' places. If flags&ZEROPAD, it should
627 * first be prefixed by any sign or other prefix; otherwise,
628 * it should be blank padded before the prefix is emitted.
629 * After any left-hand padding and prefixing, emit zeroes
Elliott Hughesb813a6a2022-08-01 22:18:40 +0000630 * required by a decimal %[bBdiouxX] precision, then print the
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700631 * string proper, then emit zeroes required by any leftover
632 * floating precision; finally, if LADJUST, pad with blanks.
633 *
634 * Compute actual size, so we know how much to pad.
635 * size excludes decimal prec; realsz includes it.
636 */
637 realsz = dprec > size ? dprec : size;
638 if (sign) realsz++;
639 if (ox[1]) realsz += 2;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800640
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700641 /* right-adjusting blank padding */
642 if ((flags & (LADJUST | ZEROPAD)) == 0) PAD(width - realsz, blanks);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800643
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700644 /* prefix */
645 if (sign) PRINT(&sign, 1);
646 if (ox[1]) { /* ox[1] is either x, X, or \0 */
647 ox[0] = '0';
648 PRINT(ox, 2);
649 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800650
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700651 /* right-adjusting zero padding */
652 if ((flags & (LADJUST | ZEROPAD)) == ZEROPAD) PAD(width - realsz, zeroes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800653
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700654 /* leading zeroes from decimal precision */
655 PAD(dprec - size, zeroes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800656
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700657 /* the string or number proper */
658 if ((flags & FPT) == 0) {
659 PRINT(cp, size);
660 } else { /* glue together f_p fragments */
Yi Kong32bc0fc2018-08-02 17:31:13 -0700661 if (decimal_point == nullptr) decimal_point = nl_langinfo(RADIXCHAR);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700662 if (!expchar) { /* %[fF] or sufficiently short %[gG] */
663 if (expt <= 0) {
664 PRINT(zeroes, 1);
665 if (prec || flags & ALT) PRINT(decimal_point, 1);
666 PAD(-expt, zeroes);
667 /* already handled initial 0's */
668 prec += expt;
669 } else {
670 PRINTANDPAD(cp, dtoaend, lead, zeroes);
671 cp += lead;
672 if (prec || flags & ALT) PRINT(decimal_point, 1);
673 }
674 PRINTANDPAD(cp, dtoaend, prec, zeroes);
675 } else { /* %[eE] or sufficiently long %[gG] */
676 if (prec > 1 || flags & ALT) {
677 buf[0] = *cp++;
678 buf[1] = *decimal_point;
679 PRINT(buf, 2);
680 PRINT(cp, ndig - 1);
681 PAD(prec - ndig, zeroes);
682 } else { /* XeYYY */
683 PRINT(cp, 1);
684 }
685 PRINT(expstr, expsize);
686 }
687 }
688 /* left-adjusting padding (always blank) */
689 if (flags & LADJUST) PAD(width - realsz, blanks);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800690
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700691 /* finally, adjust ret */
692 if (width < realsz) width = realsz;
693 if (width > INT_MAX - ret) goto overflow;
694 ret += width;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800695
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700696 FLUSH(); /* copy out the I/O vectors */
697 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800698done:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700699 FLUSH();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800700error:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700701 va_end(orgap);
702 if (__sferror(fp)) ret = -1;
703 goto finish;
Elliott Hughes05493712014-04-17 17:30:03 -0700704
705overflow:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700706 errno = ENOMEM;
707 ret = -1;
Elliott Hughes05493712014-04-17 17:30:03 -0700708
709finish:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700710 free(convbuf);
711 if (dtoaresult) __freedtoa(dtoaresult);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700712 if (argtable != nullptr && argtable != statargtable) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700713 munmap(argtable, argtablesiz);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700714 argtable = nullptr;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700715 }
716 return (ret);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800717}