blob: 54ca7a73c0b9bb97297280bd91052207f9f0f018 [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 Hughes26a0ebd2023-05-09 11:52:16 -070043
44#define PRINT(ptr, len) \
45 do { \
46 iovp->iov_base = (ptr); \
47 iovp->iov_len = (len); \
48 uio.uio_resid += (len); \
49 iovp++; \
50 if (++uio.uio_iovcnt >= NIOV) { \
51 if (helpers::sprint(fp, &uio)) goto error; \
52 iovp = iov; \
53 } \
54 } while (0)
55
56#define FLUSH() \
57 do { \
58 if (uio.uio_resid && helpers::sprint(fp, &uio)) goto error; \
59 uio.uio_iovcnt = 0; \
60 iovp = iov; \
61 } while (0)
62
Elliott Hughes1f493172017-11-08 16:13:18 -080063#include "printf_common.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080064
Elliott Hughes1f493172017-11-08 16:13:18 -080065int FUNCTION_NAME(FILE* fp, const CHAR_TYPE* fmt0, va_list ap) {
Elliott Hughes654cd832018-08-30 16:00:42 -070066 int caller_errno = errno;
Elliott Hughesb70576b2017-11-13 11:10:05 -080067 int n, n2;
Elliott Hughes93a1f8b2017-11-07 22:52:29 -080068 CHAR_TYPE* cp; /* handy char pointer (short term usage) */
Elliott Hughes93a1f8b2017-11-07 22:52:29 -080069 CHAR_TYPE sign; /* sign prefix (' ', '+', '-', or \0) */
Elliott Hughesc8f2c522017-10-31 13:07:51 -070070 int flags; /* flags as above */
71 int ret; /* return value accumulator */
72 int width; /* width from format (%8d), or 0 */
73 int prec; /* precision from format; <0 for N/A */
Elliott Hughesc8f2c522017-10-31 13:07:51 -070074 /*
75 * We can decompose the printed representation of floating
76 * point numbers into several parts, some of which may be empty:
77 *
78 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
79 * A B ---C--- D E F
80 *
81 * A: 'sign' holds this value if present; '\0' otherwise
82 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
83 * C: cp points to the string MMMNNN. Leading and trailing
84 * zeros are not in the string and must be added.
85 * D: expchar holds this character; '\0' if no exponent, e.g. %f
86 * F: at least two digits for decimal, at least one digit for hex
87 */
Yi Kong32bc0fc2018-08-02 17:31:13 -070088 char* decimal_point = nullptr;
Elliott Hughesc8f2c522017-10-31 13:07:51 -070089 int signflag; /* true if float is negative */
90 union { /* floating point arguments %[aAeEfFgG] */
91 double dbl;
92 long double ldbl;
93 } fparg;
94 int expt; /* integer value of exponent */
95 char expchar; /* exponent character: [eEpP\0] */
96 char* dtoaend; /* pointer to end of converted digits */
97 int expsize; /* character count for expstr */
98 int lead; /* sig figs before decimal or group sep */
99 int ndig; /* actual number of digits returned by dtoa */
Elliott Hughes93a1f8b2017-11-07 22:52:29 -0800100 CHAR_TYPE expstr[MAXEXPDIG + 2]; /* buffer for exponent string: e+ZZZ */
Yi Kong32bc0fc2018-08-02 17:31:13 -0700101 char* dtoaresult = nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800102
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700103 uintmax_t _umax; /* integer arguments %[diouxX] */
Elliott Hughesb813a6a2022-08-01 22:18:40 +0000104 enum { BIN, OCT, DEC, HEX } base; /* base for %[bBdiouxX] conversion */
105 int dprec; /* a copy of prec if %[bBdiouxX], 0 otherwise */
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700106 int realsz; /* field size expanded by dprec */
107 int size; /* size of converted field or string */
108 const char* xdigs; /* digits for %[xX] conversion */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800109#define NIOV 8
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700110 struct __suio uio; /* output information: summary */
111 struct __siov iov[NIOV]; /* ... and individual io vectors */
Elliott Hughesb70576b2017-11-13 11:10:05 -0800112 struct __siov* iovp; /* for PRINT macro */
Elliott Hughes93a1f8b2017-11-07 22:52:29 -0800113 CHAR_TYPE buf[BUF]; /* buffer with space for digits of uintmax_t */
114 CHAR_TYPE ox[2]; /* space for 0x; ox[1] is either x, X, or \0 */
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700115 union arg* argtable; /* args, built due to positional arg */
116 union arg statargtable[STATIC_ARG_TBL_SIZE];
117 size_t argtablesiz;
118 int nextarg; /* 1-based argument index */
119 va_list orgap; /* original argument pointer */
Elliott Hughes93a1f8b2017-11-07 22:52:29 -0800120 CHAR_TYPE* convbuf; /* buffer for wide/multibyte conversion */
Elliott Hughes05493712014-04-17 17:30:03 -0700121
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700122 /*
123 * Choose PADSIZE to trade efficiency vs. size. If larger printf
124 * fields occur frequently, increase PADSIZE and make the initialisers
125 * below longer.
126 */
127#define PADSIZE 16 /* pad chunk size */
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700128 static CHAR_TYPE blanks[PADSIZE] = {
129 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '
130 };
131 static CHAR_TYPE zeroes[PADSIZE] = {
132 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'
133 };
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800134
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700135 static const char xdigs_lower[] = "0123456789abcdef";
136 static const char xdigs_upper[] = "0123456789ABCDEF";
Elliott Hughes05493712014-04-17 17:30:03 -0700137
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800138 _SET_ORIENTATION(fp, CHAR_TYPE_ORIENTATION);
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700139
140 // Writing "" to a read only file returns EOF, not 0.
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700141 if (cantwrite(fp)) {
142 errno = EBADF;
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700143 return EOF;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700144 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800145
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700146 // Optimize writes to stderr and other unbuffered files).
147 if ((fp->_flags & (__SNBF | __SWR | __SRW)) == (__SNBF | __SWR) && fp->_file >= 0) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700148 return (__sbprintf(fp, fmt0, ap));
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700149 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800150
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700151 CHAR_TYPE* fmt = const_cast<CHAR_TYPE*>(fmt0);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700152 argtable = nullptr;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700153 nextarg = 1;
154 va_copy(orgap, ap);
155 uio.uio_iov = iovp = iov;
156 uio.uio_resid = 0;
157 uio.uio_iovcnt = 0;
158 ret = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700159 convbuf = nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800160
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700161 /*
162 * Scan the format for conversions (`%' character).
163 */
164 for (;;) {
Elliott Hughesb70576b2017-11-13 11:10:05 -0800165 int ch;
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;
George Burgess IVfa5410f2018-08-13 17:44:06 -0700212 __BIONIC_FALLTHROUGH;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700213 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;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700232 if (argtable == nullptr) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700233 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;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700267 if (argtable == nullptr) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700268 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 Hughesb813a6a2022-08-01 22:18:40 +0000309 case 'B':
310 case 'b':
311 _umax = UARG();
312 base = BIN;
313 if (flags & ALT && _umax != 0) ox[1] = ch;
314 goto nosign;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700315 case 'C':
316 flags |= LONGINT;
George Burgess IVfa5410f2018-08-13 17:44:06 -0700317 __BIONIC_FALLTHROUGH;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700318 case 'c':
319 if (flags & LONGINT) {
320 mbstate_t mbs;
321 size_t mbseqlen;
Elliott Hughes05493712014-04-17 17:30:03 -0700322
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700323 memset(&mbs, 0, sizeof(mbs));
324 mbseqlen = wcrtomb(buf, (wchar_t)GETARG(wint_t), &mbs);
325 if (mbseqlen == (size_t)-1) {
326 ret = -1;
327 goto error;
328 }
329 cp = buf;
330 size = (int)mbseqlen;
331 } else {
332 *(cp = buf) = GETARG(int);
333 size = 1;
334 }
335 sign = '\0';
336 break;
337 case 'D':
338 flags |= LONGINT;
George Burgess IVfa5410f2018-08-13 17:44:06 -0700339 __BIONIC_FALLTHROUGH;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700340 case 'd':
341 case 'i':
342 _umax = SARG();
343 if ((intmax_t)_umax < 0) {
344 _umax = -_umax;
345 sign = '-';
346 }
347 base = DEC;
348 goto number;
349 case 'a':
350 case 'A':
351 if (ch == 'a') {
352 ox[1] = 'x';
353 xdigs = xdigs_lower;
354 expchar = 'p';
355 } else {
356 ox[1] = 'X';
357 xdigs = xdigs_upper;
358 expchar = 'P';
359 }
360 if (prec >= 0) prec++;
361 if (dtoaresult) __freedtoa(dtoaresult);
362 if (flags & LONGDBL) {
363 fparg.ldbl = GETARG(long double);
364 dtoaresult = cp = __hldtoa(fparg.ldbl, xdigs, prec, &expt, &signflag, &dtoaend);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700365 if (dtoaresult == nullptr) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700366 errno = ENOMEM;
367 goto error;
368 }
369 } else {
370 fparg.dbl = GETARG(double);
371 dtoaresult = cp = __hdtoa(fparg.dbl, xdigs, prec, &expt, &signflag, &dtoaend);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700372 if (dtoaresult == nullptr) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700373 errno = ENOMEM;
374 goto error;
375 }
376 }
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800377 if (prec < 0) prec = dtoaend - dtoaresult;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700378 if (expt == INT_MAX) ox[1] = '\0';
379 goto fp_common;
380 case 'e':
381 case 'E':
382 expchar = ch;
383 if (prec < 0) /* account for digit before decpt */
384 prec = DEFPREC + 1;
385 else
386 prec++;
387 goto fp_begin;
388 case 'f':
389 case 'F':
390 expchar = '\0';
391 goto fp_begin;
392 case 'g':
393 case 'G':
394 expchar = ch - ('g' - 'e');
395 if (prec == 0) prec = 1;
396 fp_begin:
397 if (prec < 0) prec = DEFPREC;
398 if (dtoaresult) __freedtoa(dtoaresult);
399 if (flags & LONGDBL) {
400 fparg.ldbl = GETARG(long double);
401 dtoaresult = cp = __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec, &expt, &signflag, &dtoaend);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700402 if (dtoaresult == nullptr) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700403 errno = ENOMEM;
404 goto error;
405 }
406 } else {
407 fparg.dbl = GETARG(double);
408 dtoaresult = cp = __dtoa(fparg.dbl, expchar ? 2 : 3, prec, &expt, &signflag, &dtoaend);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700409 if (dtoaresult == nullptr) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700410 errno = ENOMEM;
411 goto error;
412 }
413 if (expt == 9999) expt = INT_MAX;
414 }
415 fp_common:
416 if (signflag) sign = '-';
417 if (expt == INT_MAX) { /* inf or nan */
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700418 if (*cp == 'N') {
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800419 cp = const_cast<CHAR_TYPE*>((ch >= 'a') ? CHAR_TYPE_nan : CHAR_TYPE_NAN);
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700420 } else {
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800421 cp = const_cast<CHAR_TYPE*>((ch >= 'a') ? CHAR_TYPE_inf : CHAR_TYPE_INF);
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700422 }
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700423 size = 3;
424 flags &= ~ZEROPAD;
425 break;
426 }
427 flags |= FPT;
428 ndig = dtoaend - cp;
429 if (ch == 'g' || ch == 'G') {
430 if (expt > -4 && expt <= prec) {
431 /* Make %[gG] smell like %[fF] */
432 expchar = '\0';
433 if (flags & ALT)
434 prec -= expt;
435 else
436 prec = ndig - expt;
437 if (prec < 0) prec = 0;
438 } else {
439 /*
440 * Make %[gG] smell like %[eE], but
441 * trim trailing zeroes if no # flag.
442 */
443 if (!(flags & ALT)) prec = ndig;
444 }
445 }
446 if (expchar) {
447 expsize = exponent(expstr, expt - 1, expchar);
448 size = expsize + prec;
449 if (prec > 1 || flags & ALT) ++size;
450 } else {
451 /* space for digits before decimal point */
452 if (expt > 0)
453 size = expt;
454 else /* "0" */
455 size = 1;
456 /* space for decimal pt and following digits */
457 if (prec || flags & ALT) size += prec + 1;
458 lead = expt;
459 }
460 break;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700461 case 'n':
Elliott Hughes41398d02018-03-07 13:32:58 -0800462 __fortify_fatal("%%n not allowed on Android");
Elliott Hughes654cd832018-08-30 16:00:42 -0700463 case 'm':
Elliott Hughesf340a562018-09-06 10:42:40 -0700464 cp = strerror_r(caller_errno, buf, sizeof(buf));
Elliott Hughes654cd832018-08-30 16:00:42 -0700465 goto string;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700466 case 'O':
467 flags |= LONGINT;
George Burgess IVfa5410f2018-08-13 17:44:06 -0700468 __BIONIC_FALLTHROUGH;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700469 case 'o':
470 _umax = UARG();
471 base = OCT;
472 goto nosign;
473 case 'p':
474 /*
475 * ``The argument shall be a pointer to void. The
476 * value of the pointer is converted to a sequence
477 * of printable characters, in an implementation-
478 * defined manner.''
479 * -- ANSI X3J11
480 */
481 _umax = (u_long)GETARG(void*);
482 base = HEX;
483 xdigs = xdigs_lower;
484 ox[1] = 'x';
485 goto nosign;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700486 case 'S':
487 flags |= LONGINT;
George Burgess IVfa5410f2018-08-13 17:44:06 -0700488 __BIONIC_FALLTHROUGH;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700489 case 's':
490 if (flags & LONGINT) {
491 wchar_t* wcp;
Elliott Hughes05493712014-04-17 17:30:03 -0700492
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700493 free(convbuf);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700494 convbuf = nullptr;
495 if ((wcp = GETARG(wchar_t*)) == nullptr) {
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700496 cp = const_cast<char*>("(null)");
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700497 } else {
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800498 convbuf = helpers::wcsconv(wcp, prec);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700499 if (convbuf == nullptr) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700500 ret = -1;
501 goto error;
502 }
503 cp = convbuf;
504 }
Yi Kong32bc0fc2018-08-02 17:31:13 -0700505 } else if ((cp = GETARG(char*)) == nullptr) {
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700506 cp = const_cast<char*>("(null)");
507 }
Elliott Hughes654cd832018-08-30 16:00:42 -0700508 string:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700509 if (prec >= 0) {
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800510 size = CHAR_TYPE_STRNLEN(cp, prec);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700511 } else {
512 size_t len;
Elliott Hughes05493712014-04-17 17:30:03 -0700513
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800514 if ((len = CHAR_TYPE_STRLEN(cp)) > INT_MAX) goto overflow;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700515 size = (int)len;
516 }
517 sign = '\0';
518 break;
519 case 'U':
520 flags |= LONGINT;
George Burgess IVfa5410f2018-08-13 17:44:06 -0700521 __BIONIC_FALLTHROUGH;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700522 case 'u':
523 _umax = UARG();
524 base = DEC;
525 goto nosign;
zijunzhao1fdece92023-04-25 17:37:19 +0000526 case 'w': {
zijunzhao3b846ea2023-04-11 20:53:39 +0000527 n = 0;
zijunzhao1fdece92023-04-25 17:37:19 +0000528 bool fast = false;
zijunzhao3b846ea2023-04-11 20:53:39 +0000529 ch = *fmt++;
zijunzhao1fdece92023-04-25 17:37:19 +0000530 if (ch == 'f') {
531 fast = true;
532 ch = *fmt++;
533 }
zijunzhao3b846ea2023-04-11 20:53:39 +0000534 while (is_digit(ch)) {
535 APPEND_DIGIT(n, ch);
536 ch = *fmt++;
537 }
zijunzhao1fdece92023-04-25 17:37:19 +0000538 flags |= helpers::w_to_flag(n, fast);
539 goto reswitch;
540 }
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700541 case 'X':
542 xdigs = xdigs_upper;
543 goto hex;
544 case 'x':
545 xdigs = xdigs_lower;
546 hex:
547 _umax = UARG();
548 base = HEX;
549 /* leading 0x/X only if non-zero */
550 if (flags & ALT && _umax != 0) ox[1] = ch;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800551
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700552 /* unsigned conversions */
553 nosign:
554 sign = '\0';
555 /*
556 * ``... diouXx conversions ... if a precision is
557 * specified, the 0 flag will be ignored.''
558 * -- ANSI X3J11
559 */
560 number:
561 if ((dprec = prec) >= 0) flags &= ~ZEROPAD;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800562
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700563 /*
564 * ``The result of converting a zero value with an
565 * explicit precision of zero is no characters.''
566 * -- ANSI X3J11
567 */
568 cp = buf + BUF;
569 if (_umax != 0 || prec != 0) {
570 /*
571 * Unsigned mod is hard, and unsigned mod
572 * by a constant is easier than that by
573 * a variable; hence this switch.
574 */
575 switch (base) {
Elliott Hughesb813a6a2022-08-01 22:18:40 +0000576 case BIN:
577 do {
578 *--cp = to_char(_umax & 1);
579 _umax >>= 1;
580 } while (_umax);
581 break;
582
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700583 case OCT:
584 do {
585 *--cp = to_char(_umax & 7);
586 _umax >>= 3;
587 } while (_umax);
588 /* handle octal leading 0 */
589 if (flags & ALT && *cp != '0') *--cp = '0';
590 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800591
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700592 case DEC:
593 /* many numbers are 1 digit */
594 while (_umax >= 10) {
595 *--cp = to_char(_umax % 10);
596 _umax /= 10;
597 }
598 *--cp = to_char(_umax);
599 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800600
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700601 case HEX:
602 do {
603 *--cp = xdigs[_umax & 15];
604 _umax >>= 4;
605 } while (_umax);
606 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800607
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700608 default:
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700609 abort();
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700610 }
611 }
612 size = buf + BUF - cp;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700613 if (size > BUF) abort(); /* should never happen */
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700614 break;
615 default: /* "%?" prints ?, unless ? is NUL */
616 if (ch == '\0') goto done;
617 /* pretend it was %c with argument ch */
618 cp = buf;
619 *cp = ch;
620 size = 1;
621 sign = '\0';
622 break;
623 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800624
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700625 /*
626 * All reasonable formats wind up here. At this point, `cp'
627 * points to a string which (if not flags&LADJUST) should be
628 * padded out to `width' places. If flags&ZEROPAD, it should
629 * first be prefixed by any sign or other prefix; otherwise,
630 * it should be blank padded before the prefix is emitted.
631 * After any left-hand padding and prefixing, emit zeroes
Elliott Hughesb813a6a2022-08-01 22:18:40 +0000632 * required by a decimal %[bBdiouxX] precision, then print the
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700633 * string proper, then emit zeroes required by any leftover
634 * floating precision; finally, if LADJUST, pad with blanks.
635 *
636 * Compute actual size, so we know how much to pad.
637 * size excludes decimal prec; realsz includes it.
638 */
639 realsz = dprec > size ? dprec : size;
640 if (sign) realsz++;
641 if (ox[1]) realsz += 2;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800642
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700643 /* right-adjusting blank padding */
644 if ((flags & (LADJUST | ZEROPAD)) == 0) PAD(width - realsz, blanks);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800645
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700646 /* prefix */
647 if (sign) PRINT(&sign, 1);
648 if (ox[1]) { /* ox[1] is either x, X, or \0 */
649 ox[0] = '0';
650 PRINT(ox, 2);
651 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800652
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700653 /* right-adjusting zero padding */
654 if ((flags & (LADJUST | ZEROPAD)) == ZEROPAD) PAD(width - realsz, zeroes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800655
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700656 /* leading zeroes from decimal precision */
657 PAD(dprec - size, zeroes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800658
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700659 /* the string or number proper */
660 if ((flags & FPT) == 0) {
661 PRINT(cp, size);
662 } else { /* glue together f_p fragments */
Yi Kong32bc0fc2018-08-02 17:31:13 -0700663 if (decimal_point == nullptr) decimal_point = nl_langinfo(RADIXCHAR);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700664 if (!expchar) { /* %[fF] or sufficiently short %[gG] */
665 if (expt <= 0) {
666 PRINT(zeroes, 1);
667 if (prec || flags & ALT) PRINT(decimal_point, 1);
668 PAD(-expt, zeroes);
669 /* already handled initial 0's */
670 prec += expt;
671 } else {
672 PRINTANDPAD(cp, dtoaend, lead, zeroes);
673 cp += lead;
674 if (prec || flags & ALT) PRINT(decimal_point, 1);
675 }
676 PRINTANDPAD(cp, dtoaend, prec, zeroes);
677 } else { /* %[eE] or sufficiently long %[gG] */
678 if (prec > 1 || flags & ALT) {
679 buf[0] = *cp++;
680 buf[1] = *decimal_point;
681 PRINT(buf, 2);
682 PRINT(cp, ndig - 1);
683 PAD(prec - ndig, zeroes);
684 } else { /* XeYYY */
685 PRINT(cp, 1);
686 }
687 PRINT(expstr, expsize);
688 }
689 }
690 /* left-adjusting padding (always blank) */
691 if (flags & LADJUST) PAD(width - realsz, blanks);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800692
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700693 /* finally, adjust ret */
694 if (width < realsz) width = realsz;
695 if (width > INT_MAX - ret) goto overflow;
696 ret += width;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800697
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700698 FLUSH(); /* copy out the I/O vectors */
699 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800700done:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700701 FLUSH();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800702error:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700703 va_end(orgap);
704 if (__sferror(fp)) ret = -1;
705 goto finish;
Elliott Hughes05493712014-04-17 17:30:03 -0700706
707overflow:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700708 errno = ENOMEM;
709 ret = -1;
Elliott Hughes05493712014-04-17 17:30:03 -0700710
711finish:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700712 free(convbuf);
713 if (dtoaresult) __freedtoa(dtoaresult);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700714 if (argtable != nullptr && argtable != statargtable) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700715 munmap(argtable, argtablesiz);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700716 argtable = nullptr;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700717 }
718 return (ret);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800719}