blob: 12cceebc723ed133e2888cd2210929aa22192730 [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"
Elliott Hughes531199c2023-05-09 16:11:49 -070042#define CHAR_TYPE_ORIENTATION ORIENT_BYTES
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);
Elliott Hughes531199c2023-05-09 16:11:49 -0700364 dtoaresult = __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);
Elliott Hughes531199c2023-05-09 16:11:49 -0700371 dtoaresult = __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);
Elliott Hughes531199c2023-05-09 16:11:49 -0700401 dtoaresult = __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);
Elliott Hughes531199c2023-05-09 16:11:49 -0700408 dtoaresult = __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:
Elliott Hughes531199c2023-05-09 16:11:49 -0700416#if CHAR_TYPE_ORIENTATION == ORIENT_BYTES
417 cp = dtoaresult;
418#else
419 free(convbuf);
420 cp = convbuf = helpers::mbsconv(dtoaresult, -1);
421 if (cp == nullptr) goto error;
422#endif
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700423 if (signflag) sign = '-';
424 if (expt == INT_MAX) { /* inf or nan */
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700425 if (*cp == 'N') {
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800426 cp = const_cast<CHAR_TYPE*>((ch >= 'a') ? CHAR_TYPE_nan : CHAR_TYPE_NAN);
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700427 } else {
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800428 cp = const_cast<CHAR_TYPE*>((ch >= 'a') ? CHAR_TYPE_inf : CHAR_TYPE_INF);
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700429 }
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700430 size = 3;
431 flags &= ~ZEROPAD;
432 break;
433 }
434 flags |= FPT;
Elliott Hughes531199c2023-05-09 16:11:49 -0700435 ndig = dtoaend - dtoaresult;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700436 if (ch == 'g' || ch == 'G') {
437 if (expt > -4 && expt <= prec) {
438 /* Make %[gG] smell like %[fF] */
439 expchar = '\0';
440 if (flags & ALT)
441 prec -= expt;
442 else
443 prec = ndig - expt;
444 if (prec < 0) prec = 0;
445 } else {
446 /*
447 * Make %[gG] smell like %[eE], but
448 * trim trailing zeroes if no # flag.
449 */
450 if (!(flags & ALT)) prec = ndig;
451 }
452 }
453 if (expchar) {
454 expsize = exponent(expstr, expt - 1, expchar);
455 size = expsize + prec;
456 if (prec > 1 || flags & ALT) ++size;
457 } else {
458 /* space for digits before decimal point */
459 if (expt > 0)
460 size = expt;
461 else /* "0" */
462 size = 1;
463 /* space for decimal pt and following digits */
464 if (prec || flags & ALT) size += prec + 1;
465 lead = expt;
466 }
467 break;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700468 case 'n':
Elliott Hughes41398d02018-03-07 13:32:58 -0800469 __fortify_fatal("%%n not allowed on Android");
Elliott Hughes654cd832018-08-30 16:00:42 -0700470 case 'm':
Elliott Hughesf340a562018-09-06 10:42:40 -0700471 cp = strerror_r(caller_errno, buf, sizeof(buf));
Elliott Hughes654cd832018-08-30 16:00:42 -0700472 goto string;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700473 case 'O':
474 flags |= LONGINT;
George Burgess IVfa5410f2018-08-13 17:44:06 -0700475 __BIONIC_FALLTHROUGH;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700476 case 'o':
477 _umax = UARG();
478 base = OCT;
479 goto nosign;
480 case 'p':
481 /*
482 * ``The argument shall be a pointer to void. The
483 * value of the pointer is converted to a sequence
484 * of printable characters, in an implementation-
485 * defined manner.''
486 * -- ANSI X3J11
487 */
488 _umax = (u_long)GETARG(void*);
489 base = HEX;
490 xdigs = xdigs_lower;
491 ox[1] = 'x';
492 goto nosign;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700493 case 'S':
494 flags |= LONGINT;
George Burgess IVfa5410f2018-08-13 17:44:06 -0700495 __BIONIC_FALLTHROUGH;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700496 case 's':
497 if (flags & LONGINT) {
498 wchar_t* wcp;
Elliott Hughes05493712014-04-17 17:30:03 -0700499
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700500 free(convbuf);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700501 convbuf = nullptr;
502 if ((wcp = GETARG(wchar_t*)) == nullptr) {
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700503 cp = const_cast<char*>("(null)");
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700504 } else {
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800505 convbuf = helpers::wcsconv(wcp, prec);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700506 if (convbuf == nullptr) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700507 ret = -1;
508 goto error;
509 }
510 cp = convbuf;
511 }
Yi Kong32bc0fc2018-08-02 17:31:13 -0700512 } else if ((cp = GETARG(char*)) == nullptr) {
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700513 cp = const_cast<char*>("(null)");
514 }
Elliott Hughes654cd832018-08-30 16:00:42 -0700515 string:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700516 if (prec >= 0) {
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800517 size = CHAR_TYPE_STRNLEN(cp, prec);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700518 } else {
519 size_t len;
Elliott Hughes05493712014-04-17 17:30:03 -0700520
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800521 if ((len = CHAR_TYPE_STRLEN(cp)) > INT_MAX) goto overflow;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700522 size = (int)len;
523 }
524 sign = '\0';
525 break;
526 case 'U':
527 flags |= LONGINT;
George Burgess IVfa5410f2018-08-13 17:44:06 -0700528 __BIONIC_FALLTHROUGH;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700529 case 'u':
530 _umax = UARG();
531 base = DEC;
532 goto nosign;
zijunzhao1fdece92023-04-25 17:37:19 +0000533 case 'w': {
zijunzhao3b846ea2023-04-11 20:53:39 +0000534 n = 0;
zijunzhao1fdece92023-04-25 17:37:19 +0000535 bool fast = false;
zijunzhao3b846ea2023-04-11 20:53:39 +0000536 ch = *fmt++;
zijunzhao1fdece92023-04-25 17:37:19 +0000537 if (ch == 'f') {
538 fast = true;
539 ch = *fmt++;
540 }
zijunzhao3b846ea2023-04-11 20:53:39 +0000541 while (is_digit(ch)) {
542 APPEND_DIGIT(n, ch);
543 ch = *fmt++;
544 }
zijunzhao1fdece92023-04-25 17:37:19 +0000545 flags |= helpers::w_to_flag(n, fast);
546 goto reswitch;
547 }
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700548 case 'X':
549 xdigs = xdigs_upper;
550 goto hex;
551 case 'x':
552 xdigs = xdigs_lower;
553 hex:
554 _umax = UARG();
555 base = HEX;
556 /* leading 0x/X only if non-zero */
557 if (flags & ALT && _umax != 0) ox[1] = ch;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800558
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700559 /* unsigned conversions */
560 nosign:
561 sign = '\0';
562 /*
563 * ``... diouXx conversions ... if a precision is
564 * specified, the 0 flag will be ignored.''
565 * -- ANSI X3J11
566 */
567 number:
568 if ((dprec = prec) >= 0) flags &= ~ZEROPAD;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800569
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700570 /*
571 * ``The result of converting a zero value with an
572 * explicit precision of zero is no characters.''
573 * -- ANSI X3J11
574 */
575 cp = buf + BUF;
576 if (_umax != 0 || prec != 0) {
577 /*
578 * Unsigned mod is hard, and unsigned mod
579 * by a constant is easier than that by
580 * a variable; hence this switch.
581 */
582 switch (base) {
Elliott Hughesb813a6a2022-08-01 22:18:40 +0000583 case BIN:
584 do {
585 *--cp = to_char(_umax & 1);
586 _umax >>= 1;
587 } while (_umax);
588 break;
589
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700590 case OCT:
591 do {
592 *--cp = to_char(_umax & 7);
593 _umax >>= 3;
594 } while (_umax);
595 /* handle octal leading 0 */
596 if (flags & ALT && *cp != '0') *--cp = '0';
597 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800598
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700599 case DEC:
600 /* many numbers are 1 digit */
601 while (_umax >= 10) {
602 *--cp = to_char(_umax % 10);
603 _umax /= 10;
604 }
605 *--cp = to_char(_umax);
606 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800607
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700608 case HEX:
609 do {
610 *--cp = xdigs[_umax & 15];
611 _umax >>= 4;
612 } while (_umax);
613 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800614
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700615 default:
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700616 abort();
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700617 }
618 }
619 size = buf + BUF - cp;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700620 if (size > BUF) abort(); /* should never happen */
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700621 break;
622 default: /* "%?" prints ?, unless ? is NUL */
623 if (ch == '\0') goto done;
624 /* pretend it was %c with argument ch */
625 cp = buf;
626 *cp = ch;
627 size = 1;
628 sign = '\0';
629 break;
630 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800631
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700632 /*
633 * All reasonable formats wind up here. At this point, `cp'
634 * points to a string which (if not flags&LADJUST) should be
635 * padded out to `width' places. If flags&ZEROPAD, it should
636 * first be prefixed by any sign or other prefix; otherwise,
637 * it should be blank padded before the prefix is emitted.
638 * After any left-hand padding and prefixing, emit zeroes
Elliott Hughesb813a6a2022-08-01 22:18:40 +0000639 * required by a decimal %[bBdiouxX] precision, then print the
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700640 * string proper, then emit zeroes required by any leftover
641 * floating precision; finally, if LADJUST, pad with blanks.
642 *
643 * Compute actual size, so we know how much to pad.
644 * size excludes decimal prec; realsz includes it.
645 */
646 realsz = dprec > size ? dprec : size;
647 if (sign) realsz++;
648 if (ox[1]) realsz += 2;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800649
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700650 /* right-adjusting blank padding */
651 if ((flags & (LADJUST | ZEROPAD)) == 0) PAD(width - realsz, blanks);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800652
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700653 /* prefix */
654 if (sign) PRINT(&sign, 1);
655 if (ox[1]) { /* ox[1] is either x, X, or \0 */
656 ox[0] = '0';
657 PRINT(ox, 2);
658 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800659
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700660 /* right-adjusting zero padding */
661 if ((flags & (LADJUST | ZEROPAD)) == ZEROPAD) PAD(width - realsz, zeroes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800662
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700663 /* leading zeroes from decimal precision */
664 PAD(dprec - size, zeroes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800665
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700666 /* the string or number proper */
667 if ((flags & FPT) == 0) {
668 PRINT(cp, size);
669 } else { /* glue together f_p fragments */
Yi Kong32bc0fc2018-08-02 17:31:13 -0700670 if (decimal_point == nullptr) decimal_point = nl_langinfo(RADIXCHAR);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700671 if (!expchar) { /* %[fF] or sufficiently short %[gG] */
Elliott Hughes531199c2023-05-09 16:11:49 -0700672 CHAR_TYPE* end = cp + ndig;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700673 if (expt <= 0) {
674 PRINT(zeroes, 1);
675 if (prec || flags & ALT) PRINT(decimal_point, 1);
676 PAD(-expt, zeroes);
677 /* already handled initial 0's */
678 prec += expt;
679 } else {
Elliott Hughes531199c2023-05-09 16:11:49 -0700680 PRINTANDPAD(cp, end, lead, zeroes);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700681 cp += lead;
682 if (prec || flags & ALT) PRINT(decimal_point, 1);
683 }
Elliott Hughes531199c2023-05-09 16:11:49 -0700684 PRINTANDPAD(cp, end, prec, zeroes);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700685 } else { /* %[eE] or sufficiently long %[gG] */
686 if (prec > 1 || flags & ALT) {
687 buf[0] = *cp++;
688 buf[1] = *decimal_point;
689 PRINT(buf, 2);
690 PRINT(cp, ndig - 1);
691 PAD(prec - ndig, zeroes);
692 } else { /* XeYYY */
693 PRINT(cp, 1);
694 }
695 PRINT(expstr, expsize);
696 }
697 }
698 /* left-adjusting padding (always blank) */
699 if (flags & LADJUST) PAD(width - realsz, blanks);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800700
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700701 /* finally, adjust ret */
702 if (width < realsz) width = realsz;
703 if (width > INT_MAX - ret) goto overflow;
704 ret += width;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800705
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700706 FLUSH(); /* copy out the I/O vectors */
707 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800708done:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700709 FLUSH();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800710error:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700711 va_end(orgap);
712 if (__sferror(fp)) ret = -1;
713 goto finish;
Elliott Hughes05493712014-04-17 17:30:03 -0700714
715overflow:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700716 errno = ENOMEM;
717 ret = -1;
Elliott Hughes05493712014-04-17 17:30:03 -0700718
719finish:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700720 free(convbuf);
721 if (dtoaresult) __freedtoa(dtoaresult);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700722 if (argtable != nullptr && argtable != statargtable) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700723 munmap(argtable, argtablesiz);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700724 argtable = nullptr;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700725 }
726 return (ret);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800727}