blob: e4bbbbae0019d3faeac3228daf60a06008dc2b40 [file] [log] [blame]
Elliott Hughes506c6de2016-01-15 16:30:18 -08001/* $OpenBSD: vfwprintf.c,v 1.15 2015/12/28 22:08:18 mmcc Exp $ */
Elliott Hughes94336d82014-04-29 17:39:29 -07002/*-
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 wchar_t
Elliott Hughes93a1f8b2017-11-07 22:52:29 -080035#define FUNCTION_NAME __vfwprintf
Elliott Hughesbc27bdc2017-11-10 15:25:49 -080036#define CHAR_TYPE_STRLEN wcslen
37#define CHAR_TYPE_STRNLEN wcsnlen
38#define CHAR_TYPE_INF L"INF"
39#define CHAR_TYPE_inf L"inf"
40#define CHAR_TYPE_NAN L"NAN"
41#define CHAR_TYPE_nan L"nan"
42#define CHAR_TYPE_ORIENTATION 1
Elliott Hughes1f493172017-11-08 16:13:18 -080043#include "printf_common.h"
Elliott Hughes94336d82014-04-29 17:39:29 -070044
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 wchar_t ch; /* character from fmt */
47 int n, n2, n3; /* handy integers (short term usage) */
Elliott Hughes93a1f8b2017-11-07 22:52:29 -080048 CHAR_TYPE* cp; /* handy char pointer (short term usage) */
49 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 */
68 char* decimal_point = NULL;
69 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 */
Elliott Hughesc8f2c522017-10-31 13:07:51 -070081 char* dtoaresult = NULL;
Elliott Hughes94336d82014-04-29 17:39:29 -070082
Elliott Hughesc8f2c522017-10-31 13:07:51 -070083 uintmax_t _umax; /* integer arguments %[diouxX] */
84 enum { OCT, DEC, HEX } base; /* base for %[diouxX] conversion */
85 int dprec; /* a copy of prec if %[diouxX], 0 otherwise */
86 int realsz; /* field size expanded by dprec */
87 int size; /* size of converted field or string */
88 const char* xdigs; /* digits for %[xX] conversion */
Elliott Hughes93a1f8b2017-11-07 22:52:29 -080089 CHAR_TYPE buf[BUF]; /* buffer with space for digits of uintmax_t */
90 CHAR_TYPE ox[2]; /* space for 0x; ox[1] is either x, X, or \0 */
Elliott Hughesc8f2c522017-10-31 13:07:51 -070091 union arg* argtable; /* args, built due to positional arg */
92 union arg statargtable[STATIC_ARG_TBL_SIZE];
93 size_t argtablesiz;
94 int nextarg; /* 1-based argument index */
95 va_list orgap; /* original argument pointer */
Elliott Hughes93a1f8b2017-11-07 22:52:29 -080096 CHAR_TYPE* convbuf; /* buffer for wide/multibyte conversion */
Elliott Hughes94336d82014-04-29 17:39:29 -070097
Elliott Hughesc8f2c522017-10-31 13:07:51 -070098 /*
99 * Choose PADSIZE to trade efficiency vs. size. If larger printf
100 * fields occur frequently, increase PADSIZE and make the initialisers
101 * below longer.
102 */
103#define PADSIZE 16 /* pad chunk size */
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700104 static CHAR_TYPE blanks[PADSIZE] = {
105 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '
106 };
107 static CHAR_TYPE zeroes[PADSIZE] = {
108 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'
109 };
Elliott Hughes94336d82014-04-29 17:39:29 -0700110
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700111 static const char xdigs_lower[] = "0123456789abcdef";
112 static const char xdigs_upper[] = "0123456789ABCDEF";
Elliott Hughes94336d82014-04-29 17:39:29 -0700113
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700114 /*
115 * BEWARE, these `goto error' on error, PRINT uses 'n3',
116 * PAD uses `n' and 'n3', and PRINTANDPAD uses 'n', 'n2', and 'n3'.
117 */
118#define PRINT(ptr, len) \
119 do { \
120 for (n3 = 0; n3 < (len); n3++) { \
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800121 if ((helpers::xfputwc((ptr)[n3], fp)) == WEOF) goto error; \
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700122 } \
123 } while (0)
Elliott Hughes94336d82014-04-29 17:39:29 -0700124
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800125 _SET_ORIENTATION(fp, CHAR_TYPE_ORIENTATION);
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700126
127 // Writing "" to a read only file returns EOF, not 0.
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700128 if (cantwrite(fp)) {
129 errno = EBADF;
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700130 return EOF;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700131 }
Elliott Hughes94336d82014-04-29 17:39:29 -0700132
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700133 // Optimize writes to stderr and other unbuffered files).
134 if ((fp->_flags & (__SNBF | __SWR | __SRW)) == (__SNBF | __SWR) && fp->_file >= 0) {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700135 return (__sbprintf(fp, fmt0, ap));
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700136 }
Elliott Hughes94336d82014-04-29 17:39:29 -0700137
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700138 CHAR_TYPE* fmt = const_cast<CHAR_TYPE*>(fmt0);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700139 argtable = NULL;
140 nextarg = 1;
141 va_copy(orgap, ap);
142 ret = 0;
143 convbuf = NULL;
Elliott Hughes94336d82014-04-29 17:39:29 -0700144
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700145 /*
146 * Scan the format for conversions (`%' character).
147 */
148 for (;;) {
149 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) continue;
150 if (fmt != cp) {
151 ptrdiff_t m = fmt - cp;
152 if (m < 0 || m > INT_MAX - ret) goto overflow;
153 PRINT(cp, m);
154 ret += m;
155 }
156 if (ch == '\0') goto done;
157 fmt++; /* skip over '%' */
Elliott Hughes94336d82014-04-29 17:39:29 -0700158
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700159 flags = 0;
160 dprec = 0;
161 width = 0;
162 prec = -1;
163 sign = '\0';
164 ox[1] = '\0';
Elliott Hughes94336d82014-04-29 17:39:29 -0700165
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700166 rflag:
167 ch = *fmt++;
168 reswitch:
169 switch (ch) {
170 case ' ':
171 /*
172 * ``If the space and + flags both appear, the space
173 * flag will be ignored.''
174 * -- ANSI X3J11
175 */
176 if (!sign) sign = ' ';
177 goto rflag;
178 case '#':
179 flags |= ALT;
180 goto rflag;
181 case '\'':
182 /* grouping not implemented */
183 goto rflag;
184 case '*':
185 /*
186 * ``A negative field width argument is taken as a
187 * - flag followed by a positive field width.''
188 * -- ANSI X3J11
189 * They don't exclude field widths read from args.
190 */
191 GETASTER(width);
192 if (width >= 0) goto rflag;
193 if (width == INT_MIN) goto overflow;
194 width = -width;
195 /* FALLTHROUGH */
196 case '-':
197 flags |= LADJUST;
198 goto rflag;
199 case '+':
200 sign = '+';
201 goto rflag;
202 case '.':
203 if ((ch = *fmt++) == '*') {
204 GETASTER(n);
205 prec = n < 0 ? -1 : n;
206 goto rflag;
207 }
208 n = 0;
209 while (is_digit(ch)) {
210 APPEND_DIGIT(n, ch);
211 ch = *fmt++;
212 }
213 if (ch == '$') {
214 nextarg = n;
215 if (argtable == NULL) {
216 argtable = statargtable;
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700217 if (__find_arguments(fmt0, orgap, &argtable, &argtablesiz) == -1) {
218 ret = -1;
219 goto error;
220 }
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700221 }
222 goto rflag;
223 }
224 prec = n;
225 goto reswitch;
226 case '0':
227 /*
228 * ``Note that 0 is taken as a flag, not as the
229 * beginning of a field width.''
230 * -- ANSI X3J11
231 */
232 flags |= ZEROPAD;
233 goto rflag;
234 case '1':
235 case '2':
236 case '3':
237 case '4':
238 case '5':
239 case '6':
240 case '7':
241 case '8':
242 case '9':
243 n = 0;
244 do {
245 APPEND_DIGIT(n, ch);
246 ch = *fmt++;
247 } while (is_digit(ch));
248 if (ch == '$') {
249 nextarg = n;
250 if (argtable == NULL) {
251 argtable = statargtable;
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700252 if (__find_arguments(fmt0, orgap, &argtable, &argtablesiz) == -1) {
253 ret = -1;
254 goto error;
255 }
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700256 }
257 goto rflag;
258 }
259 width = n;
260 goto reswitch;
261 case 'L':
262 flags |= LONGDBL;
263 goto rflag;
264 case 'h':
265 if (*fmt == 'h') {
266 fmt++;
267 flags |= CHARINT;
268 } else {
269 flags |= SHORTINT;
270 }
271 goto rflag;
272 case 'j':
273 flags |= MAXINT;
274 goto rflag;
275 case 'l':
276 if (*fmt == 'l') {
277 fmt++;
278 flags |= LLONGINT;
279 } else {
280 flags |= LONGINT;
281 }
282 goto rflag;
283 case 'q':
284 flags |= LLONGINT;
285 goto rflag;
286 case 't':
287 flags |= PTRINT;
288 goto rflag;
289 case 'z':
290 flags |= SIZEINT;
291 goto rflag;
292 case 'C':
293 flags |= LONGINT;
294 /*FALLTHROUGH*/
295 case 'c':
296 if (flags & LONGINT)
297 *(cp = buf) = (wchar_t)GETARG(wint_t);
298 else
299 *(cp = buf) = (wchar_t)btowc(GETARG(int));
300 size = 1;
301 sign = '\0';
302 break;
303 case 'D':
304 flags |= LONGINT;
305 /*FALLTHROUGH*/
306 case 'd':
307 case 'i':
308 _umax = SARG();
309 if ((intmax_t)_umax < 0) {
310 _umax = -_umax;
311 sign = '-';
312 }
313 base = DEC;
314 goto number;
315 case 'a':
316 case 'A':
317 if (ch == 'a') {
318 ox[1] = 'x';
319 xdigs = xdigs_lower;
320 expchar = 'p';
321 } else {
322 ox[1] = 'X';
323 xdigs = xdigs_upper;
324 expchar = 'P';
325 }
326 if (prec >= 0) prec++;
327 if (dtoaresult) __freedtoa(dtoaresult);
328 if (flags & LONGDBL) {
329 fparg.ldbl = GETARG(long double);
330 dtoaresult = __hldtoa(fparg.ldbl, xdigs, prec, &expt, &signflag, &dtoaend);
331 if (dtoaresult == NULL) {
332 errno = ENOMEM;
333 goto error;
334 }
335 } else {
336 fparg.dbl = GETARG(double);
337 dtoaresult = __hdtoa(fparg.dbl, xdigs, prec, &expt, &signflag, &dtoaend);
338 if (dtoaresult == NULL) {
339 errno = ENOMEM;
340 goto error;
341 }
342 }
343 if (prec < 0) prec = dtoaend - dtoaresult;
344 if (expt == INT_MAX) ox[1] = '\0';
345 free(convbuf);
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800346 cp = convbuf = helpers::mbsconv(dtoaresult, -1);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700347 if (cp == NULL) goto error;
348 ndig = dtoaend - dtoaresult;
349 goto fp_common;
350 case 'e':
351 case 'E':
352 expchar = ch;
353 if (prec < 0) /* account for digit before decpt */
354 prec = DEFPREC + 1;
355 else
356 prec++;
357 goto fp_begin;
358 case 'f':
359 case 'F':
360 expchar = '\0';
361 goto fp_begin;
362 case 'g':
363 case 'G':
364 expchar = ch - ('g' - 'e');
365 if (prec == 0) prec = 1;
366 fp_begin:
367 if (prec < 0) prec = DEFPREC;
368 if (dtoaresult) __freedtoa(dtoaresult);
369 if (flags & LONGDBL) {
370 fparg.ldbl = GETARG(long double);
371 dtoaresult = __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec, &expt, &signflag, &dtoaend);
372 if (dtoaresult == NULL) {
373 errno = ENOMEM;
374 goto error;
375 }
376 } else {
377 fparg.dbl = GETARG(double);
378 dtoaresult = __dtoa(fparg.dbl, expchar ? 2 : 3, prec, &expt, &signflag, &dtoaend);
379 if (dtoaresult == NULL) {
380 errno = ENOMEM;
381 goto error;
382 }
383 if (expt == 9999) expt = INT_MAX;
384 }
385 free(convbuf);
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800386 cp = convbuf = helpers::mbsconv(dtoaresult, -1);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700387 if (cp == NULL) goto error;
388 ndig = dtoaend - dtoaresult;
389 fp_common:
390 if (signflag) sign = '-';
391 if (expt == INT_MAX) { /* inf or nan */
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700392 if (*cp == 'N') {
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800393 cp = const_cast<CHAR_TYPE*>((ch >= 'a') ? CHAR_TYPE_nan : CHAR_TYPE_NAN);
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700394 } else {
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800395 cp = const_cast<CHAR_TYPE*>((ch >= 'a') ? CHAR_TYPE_inf : CHAR_TYPE_INF);
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700396 }
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700397 size = 3;
398 flags &= ~ZEROPAD;
399 break;
400 }
401 flags |= FPT;
402 if (ch == 'g' || ch == 'G') {
403 if (expt > -4 && expt <= prec) {
404 /* Make %[gG] smell like %[fF] */
405 expchar = '\0';
406 if (flags & ALT)
407 prec -= expt;
408 else
409 prec = ndig - expt;
410 if (prec < 0) prec = 0;
411 } else {
412 /*
413 * Make %[gG] smell like %[eE], but
414 * trim trailing zeroes if no # flag.
415 */
416 if (!(flags & ALT)) prec = ndig;
417 }
418 }
419 if (expchar) {
420 expsize = exponent(expstr, expt - 1, expchar);
421 size = expsize + prec;
422 if (prec > 1 || flags & ALT) ++size;
423 } else {
424 /* space for digits before decimal point */
425 if (expt > 0)
426 size = expt;
427 else /* "0" */
428 size = 1;
429 /* space for decimal pt and following digits */
430 if (prec || flags & ALT) size += prec + 1;
431 lead = expt;
432 }
433 break;
Elliott Hughese2341d02014-05-02 18:16:32 -0700434#ifndef NO_PRINTF_PERCENT_N
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700435 case 'n':
436 if (flags & LLONGINT)
437 *GETARG(long long*) = ret;
438 else if (flags & LONGINT)
439 *GETARG(long*) = ret;
440 else if (flags & SHORTINT)
441 *GETARG(short*) = ret;
442 else if (flags & CHARINT)
443 *GETARG(signed char*) = ret;
444 else if (flags & PTRINT)
445 *GETARG(ptrdiff_t*) = ret;
446 else if (flags & SIZEINT)
447 *GETARG(ssize_t*) = ret;
448 else if (flags & MAXINT)
449 *GETARG(intmax_t*) = ret;
450 else
451 *GETARG(int*) = ret;
452 continue; /* no output */
453#endif /* NO_PRINTF_PERCENT_N */
454 case 'O':
455 flags |= LONGINT;
456 /*FALLTHROUGH*/
457 case 'o':
458 _umax = UARG();
459 base = OCT;
460 goto nosign;
461 case 'p':
462 /*
463 * ``The argument shall be a pointer to void. The
464 * value of the pointer is converted to a sequence
465 * of printable characters, in an implementation-
466 * defined manner.''
467 * -- ANSI X3J11
468 */
469 _umax = (u_long)GETARG(void*);
470 base = HEX;
471 xdigs = xdigs_lower;
472 ox[1] = 'x';
473 goto nosign;
474 case 'S':
475 flags |= LONGINT;
476 /*FALLTHROUGH*/
477 case 's':
478 if (flags & LONGINT) {
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700479 if ((cp = GETARG(wchar_t*)) == NULL) cp = const_cast<wchar_t*>(L"(null)");
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700480 } else {
481 char* mbsarg;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700482 if ((mbsarg = GETARG(char*)) == NULL) mbsarg = const_cast<char*>("(null)");
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700483 free(convbuf);
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800484 convbuf = helpers::mbsconv(mbsarg, prec);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700485 if (convbuf == NULL) {
486 fp->_flags |= __SERR;
487 goto error;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700488 } else {
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700489 cp = convbuf;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700490 }
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700491 }
492 if (prec >= 0) {
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800493 size = CHAR_TYPE_STRNLEN(cp, prec);
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700494 } else {
495 size_t len;
Elliott Hughes94336d82014-04-29 17:39:29 -0700496
Elliott Hughesbc27bdc2017-11-10 15:25:49 -0800497 if ((len = CHAR_TYPE_STRLEN(cp)) > INT_MAX) goto overflow;
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700498 size = (int)len;
499 }
500 sign = '\0';
501 break;
502 case 'U':
503 flags |= LONGINT;
504 /*FALLTHROUGH*/
505 case 'u':
506 _umax = UARG();
507 base = DEC;
508 goto nosign;
509 case 'X':
510 xdigs = xdigs_upper;
511 goto hex;
512 case 'x':
513 xdigs = xdigs_lower;
514 hex:
515 _umax = UARG();
516 base = HEX;
517 /* leading 0x/X only if non-zero */
518 if (flags & ALT && _umax != 0) ox[1] = ch;
Elliott Hughes94336d82014-04-29 17:39:29 -0700519
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700520 /* unsigned conversions */
521 nosign:
522 sign = '\0';
523 /*
524 * ``... diouXx conversions ... if a precision is
525 * specified, the 0 flag will be ignored.''
526 * -- ANSI X3J11
527 */
528 number:
529 if ((dprec = prec) >= 0) flags &= ~ZEROPAD;
Elliott Hughes94336d82014-04-29 17:39:29 -0700530
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700531 /*
532 * ``The result of converting a zero value with an
533 * explicit precision of zero is no characters.''
534 * -- ANSI X3J11
535 */
536 cp = buf + BUF;
537 if (_umax != 0 || prec != 0) {
538 /*
539 * Unsigned mod is hard, and unsigned mod
540 * by a constant is easier than that by
541 * a variable; hence this switch.
542 */
543 switch (base) {
544 case OCT:
545 do {
546 *--cp = to_char(_umax & 7);
547 _umax >>= 3;
548 } while (_umax);
549 /* handle octal leading 0 */
550 if (flags & ALT && *cp != '0') *--cp = '0';
551 break;
Elliott Hughes94336d82014-04-29 17:39:29 -0700552
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700553 case DEC:
554 /* many numbers are 1 digit */
555 while (_umax >= 10) {
556 *--cp = to_char(_umax % 10);
557 _umax /= 10;
558 }
559 *--cp = to_char(_umax);
560 break;
Elliott Hughes94336d82014-04-29 17:39:29 -0700561
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700562 case HEX:
563 do {
564 *--cp = xdigs[_umax & 15];
565 _umax >>= 4;
566 } while (_umax);
567 break;
Elliott Hughes94336d82014-04-29 17:39:29 -0700568
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700569 default:
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700570 abort();
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700571 }
572 }
573 size = buf + BUF - cp;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700574 if (size > BUF) abort(); /* should never happen */
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700575 break;
576 default: /* "%?" prints ?, unless ? is NUL */
577 if (ch == '\0') goto done;
578 /* pretend it was %c with argument ch */
579 cp = buf;
580 *cp = ch;
581 size = 1;
582 sign = '\0';
583 break;
584 }
Elliott Hughes94336d82014-04-29 17:39:29 -0700585
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700586 /*
587 * All reasonable formats wind up here. At this point, `cp'
588 * points to a string which (if not flags&LADJUST) should be
589 * padded out to `width' places. If flags&ZEROPAD, it should
590 * first be prefixed by any sign or other prefix; otherwise,
591 * it should be blank padded before the prefix is emitted.
592 * After any left-hand padding and prefixing, emit zeroes
593 * required by a decimal %[diouxX] precision, then print the
594 * string proper, then emit zeroes required by any leftover
595 * floating precision; finally, if LADJUST, pad with blanks.
596 *
597 * Compute actual size, so we know how much to pad.
598 * size excludes decimal prec; realsz includes it.
599 */
600 realsz = dprec > size ? dprec : size;
601 if (sign) realsz++;
602 if (ox[1]) realsz += 2;
Elliott Hughes94336d82014-04-29 17:39:29 -0700603
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700604 /* right-adjusting blank padding */
605 if ((flags & (LADJUST | ZEROPAD)) == 0) PAD(width - realsz, blanks);
Elliott Hughes94336d82014-04-29 17:39:29 -0700606
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700607 /* prefix */
608 if (sign) PRINT(&sign, 1);
609 if (ox[1]) { /* ox[1] is either x, X, or \0 */
610 ox[0] = '0';
611 PRINT(ox, 2);
612 }
Elliott Hughes94336d82014-04-29 17:39:29 -0700613
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700614 /* right-adjusting zero padding */
615 if ((flags & (LADJUST | ZEROPAD)) == ZEROPAD) PAD(width - realsz, zeroes);
Elliott Hughes94336d82014-04-29 17:39:29 -0700616
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700617 /* leading zeroes from decimal precision */
618 PAD(dprec - size, zeroes);
Elliott Hughes94336d82014-04-29 17:39:29 -0700619
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700620 /* the string or number proper */
621 if ((flags & FPT) == 0) {
622 PRINT(cp, size);
623 } else { /* glue together f_p fragments */
624 if (decimal_point == NULL) decimal_point = nl_langinfo(RADIXCHAR);
625 if (!expchar) { /* %[fF] or sufficiently short %[gG] */
626 if (expt <= 0) {
627 PRINT(zeroes, 1);
628 if (prec || flags & ALT) PRINT(decimal_point, 1);
629 PAD(-expt, zeroes);
630 /* already handled initial 0's */
631 prec += expt;
632 } else {
633 PRINTANDPAD(cp, convbuf + ndig, lead, zeroes);
634 cp += lead;
635 if (prec || flags & ALT) PRINT(decimal_point, 1);
636 }
637 PRINTANDPAD(cp, convbuf + ndig, prec, zeroes);
638 } else { /* %[eE] or sufficiently long %[gG] */
639 if (prec > 1 || flags & ALT) {
640 buf[0] = *cp++;
641 buf[1] = *decimal_point;
642 PRINT(buf, 2);
643 PRINT(cp, ndig - 1);
644 PAD(prec - ndig, zeroes);
645 } else { /* XeYYY */
646 PRINT(cp, 1);
647 }
648 PRINT(expstr, expsize);
649 }
650 }
651 /* left-adjusting padding (always blank) */
652 if (flags & LADJUST) PAD(width - realsz, blanks);
Elliott Hughes94336d82014-04-29 17:39:29 -0700653
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700654 /* finally, adjust ret */
655 if (width < realsz) width = realsz;
656 if (width > INT_MAX - ret) goto overflow;
657 ret += width;
658 }
Elliott Hughes94336d82014-04-29 17:39:29 -0700659done:
660error:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700661 va_end(orgap);
662 if (__sferror(fp)) ret = -1;
663 goto finish;
Elliott Hughes94336d82014-04-29 17:39:29 -0700664
665overflow:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700666 errno = ENOMEM;
667 ret = -1;
Elliott Hughes94336d82014-04-29 17:39:29 -0700668
669finish:
Elliott Hughesc8f2c522017-10-31 13:07:51 -0700670 free(convbuf);
671 if (dtoaresult) __freedtoa(dtoaresult);
672 if (argtable != NULL && argtable != statargtable) {
673 munmap(argtable, argtablesiz);
674 argtable = NULL;
675 }
676 return (ret);
Elliott Hughes94336d82014-04-29 17:39:29 -0700677}