]> git.saurik.com Git - apple/libc.git/blame - stdio/FreeBSD/vfprintf.c
Libc-1272.200.26.tar.gz
[apple/libc.git] / stdio / FreeBSD / vfprintf.c
CommitLineData
9385eb3d 1/*-
e9ce8d39
A
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
e9ce8d39
A
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
70ad1dc8
A
33#pragma clang diagnostic push
34#pragma clang diagnostic ignored "-Wint-conversion"
35
e07eda1a
A
36#define OS_CRASH_ENABLE_EXPERIMENTAL_LIBTRACE 1
37
9385eb3d
A
38#if defined(LIBC_SCCS) && !defined(lint)
39static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93";
40#endif /* LIBC_SCCS and not lint */
41#include <sys/cdefs.h>
1f2f436a 42__FBSDID("$FreeBSD: src/lib/libc/stdio/vfprintf.c,v 1.90 2009/02/28 06:06:57 das Exp $");
9385eb3d 43
ad3c9f2a
A
44#include "xlocale_private.h"
45
e9ce8d39
A
46/*
47 * Actual printf innards.
48 *
49 * This code is large and complicated...
50 */
51
9385eb3d 52#include "namespace.h"
e9ce8d39
A
53#include <sys/types.h>
54
9385eb3d 55#include <ctype.h>
e9ce8d39 56#include <limits.h>
9385eb3d
A
57#include <locale.h>
58#include <stddef.h>
59#include <stdint.h>
e9ce8d39
A
60#include <stdio.h>
61#include <stdlib.h>
62#include <string.h>
9385eb3d 63#include <wchar.h>
ad3c9f2a 64#if 0 // xprintf pending API review
1f2f436a 65#include <printf.h>
ad3c9f2a
A
66#endif
67#include <errno.h>
e9ce8d39 68
e9ce8d39 69#include <stdarg.h>
9385eb3d 70#include "un-namespace.h"
e9ce8d39 71
b061a43b
A
72#include <os/assumes.h>
73#include <mach-o/dyld_priv.h>
74#include <mach/vm_region.h>
75
9385eb3d 76#include "libc_private.h"
e9ce8d39
A
77#include "local.h"
78#include "fvwrite.h"
1f2f436a 79#include "printflocal.h"
e9ce8d39 80
ad3c9f2a 81static int __sprint(FILE *, locale_t, struct __suio *);
6465356a 82#if 0
ad3c9f2a 83static int __sbprintf(FILE *, locale_t, const char *, va_list) __printflike(3, 0);
6465356a 84#endif
ad3c9f2a
A
85static char *__wcsconv(wchar_t *, int, locale_t);
86
87__private_extern__ const char *__fix_nogrouping(const char *);
1f2f436a
A
88
89#define CHAR char
90#include "printfcommon.h"
91
92struct grouping_state {
93 char *thousands_sep; /* locale-specific thousands separator */
94 int thousep_len; /* length of thousands_sep */
95 const char *grouping; /* locale-specific numeric grouping rules */
96 int lead; /* sig figs before decimal or group sep */
97 int nseps; /* number of group separators with ' */
98 int nrepeats; /* number of repeats of the last group */
5b2abdfb 99};
e9ce8d39 100
9385eb3d 101/*
1f2f436a
A
102 * Initialize the thousands' grouping state in preparation to print a
103 * number with ndigits digits. This routine returns the total number
104 * of bytes that will be needed.
9385eb3d 105 */
1f2f436a 106static int
ad3c9f2a 107grouping_init(struct grouping_state *gs, int ndigits, locale_t loc)
1f2f436a
A
108{
109 struct lconv *locale;
110
ad3c9f2a
A
111 locale = localeconv_l(loc);
112 gs->grouping = __fix_nogrouping(locale->grouping);
1f2f436a
A
113 gs->thousands_sep = locale->thousands_sep;
114 gs->thousep_len = strlen(gs->thousands_sep);
115
116 gs->nseps = gs->nrepeats = 0;
117 gs->lead = ndigits;
118 while (*gs->grouping != CHAR_MAX) {
119 if (gs->lead <= *gs->grouping)
120 break;
121 gs->lead -= *gs->grouping;
122 if (*(gs->grouping+1)) {
123 gs->nseps++;
124 gs->grouping++;
125 } else
126 gs->nrepeats++;
127 }
128 return ((gs->nseps + gs->nrepeats) * gs->thousep_len);
129}
5b2abdfb 130
1f2f436a
A
131/*
132 * Print a number with thousands' separators.
133 */
134static int
135grouping_print(struct grouping_state *gs, struct io_state *iop,
ad3c9f2a 136 const CHAR *cp, const CHAR *ep, locale_t loc)
1f2f436a
A
137{
138 const CHAR *cp0 = cp;
139
ad3c9f2a 140 if (io_printandpad(iop, cp, ep, gs->lead, zeroes, loc))
1f2f436a
A
141 return (-1);
142 cp += gs->lead;
143 while (gs->nseps > 0 || gs->nrepeats > 0) {
144 if (gs->nrepeats > 0)
145 gs->nrepeats--;
146 else {
147 gs->grouping--;
148 gs->nseps--;
149 }
ad3c9f2a 150 if (io_print(iop, gs->thousands_sep, gs->thousep_len, loc))
1f2f436a 151 return (-1);
ad3c9f2a 152 if (io_printandpad(iop, cp, ep, *gs->grouping, zeroes, loc))
1f2f436a
A
153 return (-1);
154 cp += *gs->grouping;
155 }
156 if (cp > ep)
157 cp = ep;
158 return (cp - cp0);
159}
5b2abdfb 160
e9ce8d39
A
161/*
162 * Flush out all the vectors defined by the given uio,
163 * then reset it so that it can be reused.
164 */
165static int
ad3c9f2a 166__sprint(FILE *fp, locale_t loc __unused, struct __suio *uio)
e9ce8d39 167{
9385eb3d 168 int err;
e9ce8d39
A
169
170 if (uio->uio_resid == 0) {
171 uio->uio_iovcnt = 0;
172 return (0);
173 }
174 err = __sfvwrite(fp, uio);
175 uio->uio_resid = 0;
176 uio->uio_iovcnt = 0;
177 return (err);
178}
179
6465356a 180#if 0
e9ce8d39
A
181/*
182 * Helper function for `fprintf to unbuffered unix file': creates a
183 * temporary buffer. We only work on write-only files; this avoids
184 * worries about ungetc buffers and so forth.
185 */
186static int
ad3c9f2a 187__sbprintf(FILE *fp, locale_t loc, const char *fmt, va_list ap)
e9ce8d39
A
188{
189 int ret;
190 FILE fake;
191 unsigned char buf[BUFSIZ];
ad3c9f2a
A
192 struct __sFILEX ext;
193 fake._extra = &ext;
194 INITEXTRA(&fake);
e9ce8d39 195
1f2f436a
A
196 /* XXX This is probably not needed. */
197 if (prepwrite(fp) != 0)
198 return (EOF);
199
e9ce8d39
A
200 /* copy the important variables */
201 fake._flags = fp->_flags & ~__SNBF;
202 fake._file = fp->_file;
203 fake._cookie = fp->_cookie;
204 fake._write = fp->_write;
1f2f436a
A
205 fake._orientation = fp->_orientation;
206 fake._mbstate = fp->_mbstate;
e9ce8d39
A
207
208 /* set up the buffer */
209 fake._bf._base = fake._p = buf;
210 fake._bf._size = fake._w = sizeof(buf);
211 fake._lbfsize = 0; /* not actually used, but Just In Case */
212
213 /* do the work, then copy any error status */
ad3c9f2a 214 ret = __vfprintf(&fake, loc, fmt, ap);
9385eb3d 215 if (ret >= 0 && __fflush(&fake))
e9ce8d39
A
216 ret = EOF;
217 if (fake._flags & __SERR)
218 fp->_flags |= __SERR;
219 return (ret);
220}
6465356a 221#endif
e9ce8d39 222
9385eb3d
A
223/*
224 * Convert a wide character string argument for the %ls format to a multibyte
1f2f436a
A
225 * string representation. If not -1, prec specifies the maximum number of
226 * bytes to output, and also means that we can't assume that the wide char.
227 * string ends is null-terminated.
9385eb3d
A
228 */
229static char *
ad3c9f2a 230__wcsconv(wchar_t *wcsarg, int prec, locale_t loc)
9385eb3d 231{
59e0d9fe
A
232 static const mbstate_t initial;
233 mbstate_t mbs;
9385eb3d
A
234 char buf[MB_LEN_MAX];
235 wchar_t *p;
1f2f436a 236 char *convbuf;
9385eb3d 237 size_t clen, nbytes;
e9ce8d39 238
1f2f436a
A
239 /* Allocate space for the maximum number of bytes we could output. */
240 if (prec < 0) {
9385eb3d 241 p = wcsarg;
59e0d9fe 242 mbs = initial;
ad3c9f2a 243 nbytes = wcsrtombs_l(NULL, (const wchar_t **)&p, 0, &mbs, loc);
9385eb3d
A
244 if (nbytes == (size_t)-1)
245 return (NULL);
1f2f436a
A
246 } else {
247 /*
248 * Optimisation: if the output precision is small enough,
249 * just allocate enough memory for the maximum instead of
250 * scanning the string.
251 */
252 if (prec < 128)
253 nbytes = prec;
254 else {
255 nbytes = 0;
256 p = wcsarg;
257 mbs = initial;
258 for (;;) {
ad3c9f2a 259 clen = wcrtomb_l(buf, *p++, &mbs, loc);
1f2f436a
A
260 if (clen == 0 || clen == (size_t)-1 ||
261 nbytes + clen > prec)
262 break;
263 nbytes += clen;
264 }
265 }
9385eb3d
A
266 }
267 if ((convbuf = malloc(nbytes + 1)) == NULL)
268 return (NULL);
e9ce8d39 269
1f2f436a 270 /* Fill the output buffer. */
9385eb3d 271 p = wcsarg;
59e0d9fe 272 mbs = initial;
ad3c9f2a
A
273 if ((nbytes = wcsrtombs_l(convbuf, (const wchar_t **)&p,
274 nbytes, &mbs, loc)) == (size_t)-1) {
9385eb3d
A
275 free(convbuf);
276 return (NULL);
277 }
1f2f436a 278 convbuf[nbytes] = '\0';
9385eb3d
A
279 return (convbuf);
280}
e9ce8d39 281
5b2abdfb 282/*
9385eb3d 283 * MT-safe version
5b2abdfb 284 */
9385eb3d 285int
ad3c9f2a 286vfprintf_l(FILE * __restrict fp, locale_t loc, const char * __restrict fmt0, va_list ap)
5b2abdfb 287
9385eb3d
A
288{
289 int ret;
5b2abdfb 290
9385eb3d 291 FLOCKFILE(fp);
6465356a 292 ret = __xvprintf(XPRINTF_PLAIN, NULL, fp, loc, fmt0, ap);
9385eb3d
A
293 FUNLOCKFILE(fp);
294 return (ret);
295}
5b2abdfb 296
ad3c9f2a
A
297int
298vfprintf(FILE * __restrict fp, const char * __restrict fmt0, va_list ap)
299
300{
6465356a
A
301 int ret;
302
303 FLOCKFILE(fp);
304 ret = __xvprintf(XPRINTF_PLAIN, NULL, fp, __current_locale(), fmt0, ap);
305 FUNLOCKFILE(fp);
306 return ret;
ad3c9f2a
A
307}
308
9385eb3d
A
309/*
310 * The size of the buffer we use as scratch space for integer
1f2f436a
A
311 * conversions, among other things. We need enough space to
312 * write a uintmax_t in octal (plus one byte).
9385eb3d 313 */
1f2f436a
A
314#if UINTMAX_MAX <= UINT64_MAX
315#define BUF 32
316#else
317#error "BUF must be large enough to format a uintmax_t"
318#endif
9385eb3d 319
b061a43b
A
320__private_extern__ bool
321__printf_is_memory_read_only(void *addr, size_t __unused size)
322{
323 vm_address_t address = addr;
324 vm_size_t vmsize = 0;
325 vm_region_basic_info_data_64_t info;
326 mach_msg_type_number_t info_cnt = VM_REGION_BASIC_INFO_COUNT_64;
327 memory_object_name_t object = MACH_PORT_NULL;
328 kern_return_t kr = KERN_SUCCESS;
329
330 kr = vm_region_64(mach_task_self(),
331 &address,
332 &vmsize,
333 VM_REGION_BASIC_INFO_64,
334 (vm_region_info_t) &info,
335 &info_cnt,
336 &object);
337 return (kr == KERN_SUCCESS) && !(info.protection & VM_PROT_WRITE);
338}
339
9385eb3d
A
340/*
341 * Non-MT-safe version
342 */
ad3c9f2a
A
343__private_extern__ int
344__vfprintf(FILE *fp, locale_t loc, const char *fmt0, va_list ap)
e9ce8d39 345{
9385eb3d
A
346 char *fmt; /* format string */
347 int ch; /* character from fmt */
b061a43b 348 ssize_t n, n2; /* handy integer (short term usage) */
9385eb3d 349 char *cp; /* handy char pointer (short term usage) */
9385eb3d 350 int flags; /* flags as above */
b061a43b
A
351 ssize_t ret; /* return value accumulator */
352 ssize_t width; /* width from format (%8d), or 0 */
353 ssize_t prec; /* precision from format; <0 for N/A */
e9ce8d39 354 char sign; /* sign prefix (' ', '+', '-', or \0) */
1f2f436a
A
355 struct grouping_state gs; /* thousands' grouping info */
356
b061a43b
A
357#ifndef ALLOW_DYNAMIC_PERCENT_N
358 bool static_format_checked = false;
359#endif // ALLOW_DYNAMIC_PERCENT_N
360
59e0d9fe 361#ifndef NO_FLOATING_POINT
9385eb3d
A
362 /*
363 * We can decompose the printed representation of floating
364 * point numbers into several parts, some of which may be empty:
365 *
366 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
367 * A B ---C--- D E F
368 *
369 * A: 'sign' holds this value if present; '\0' otherwise
370 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
371 * C: cp points to the string MMMNNN. Leading and trailing
372 * zeros are not in the string and must be added.
373 * D: expchar holds this character; '\0' if no exponent, e.g. %f
374 * F: at least two digits for decimal, at least one digit for hex
375 */
376 char *decimal_point; /* locale specific decimal point */
1f2f436a 377 int decpt_len; /* length of decimal_point */
9385eb3d
A
378 int signflag; /* true if float is negative */
379 union { /* floating point arguments %[aAeEfFgG] */
380 double dbl;
381 long double ldbl;
382 } fparg;
e9ce8d39 383 int expt; /* integer value of exponent */
9385eb3d
A
384 char expchar; /* exponent character: [eEpP\0] */
385 char *dtoaend; /* pointer to end of converted digits */
386 int expsize; /* character count for expstr */
9385eb3d
A
387 int ndig; /* actual number of digits returned by dtoa */
388 char expstr[MAXEXPDIG+2]; /* buffer for exponent string: e+ZZZ */
3b2a1fe8 389 char *dtoaresult; /* buffer allocated by dtoa */
ad3c9f2a
A
390#endif
391#ifdef VECTORS
392 union arg vval; /* Vector argument. */
393 char *pct; /* Pointer to '%' at beginning of specifier. */
394 char vsep; /* Vector separator character. */
5b2abdfb 395#endif
9385eb3d
A
396 u_long ulval; /* integer arguments %[diouxX] */
397 uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */
e9ce8d39
A
398 int base; /* base for [diouxX] conversion */
399 int dprec; /* a copy of prec if [diouxX], 0 otherwise */
b061a43b
A
400 ssize_t realsz; /* field size expanded by dprec, sign, etc */
401 ssize_t size; /* size of converted field or string */
402 ssize_t prsize; /* max size of printed field */
9385eb3d 403 const char *xdigs; /* digits for %[xX] conversion */
1f2f436a 404 struct io_state io; /* I/O buffering state */
9385eb3d
A
405 char buf[BUF]; /* buffer with space for digits of uintmax_t */
406 char ox[2]; /* space for 0x; ox[1] is either x, X, or \0 */
407 union arg *argtable; /* args, built due to positional arg */
408 union arg statargtable [STATIC_ARG_TBL_SIZE];
409 int nextarg; /* 1-based argument index */
410 va_list orgap; /* original argument pointer */
411 char *convbuf; /* wide to multibyte conversion result */
e9ce8d39 412
9385eb3d
A
413 static const char xdigs_lower[16] = "0123456789abcdef";
414 static const char xdigs_upper[16] = "0123456789ABCDEF";
415
1f2f436a 416 /* BEWARE, these `goto error' on error. */
e9ce8d39 417#define PRINT(ptr, len) { \
ad3c9f2a 418 if (io_print(&io, (ptr), (len), loc)) \
1f2f436a 419 goto error; \
e9ce8d39
A
420}
421#define PAD(howmany, with) { \
ad3c9f2a 422 if (io_pad(&io, (howmany), (with), loc)) \
1f2f436a
A
423 goto error; \
424}
425#define PRINTANDPAD(p, ep, len, with) { \
ad3c9f2a 426 if (io_printandpad(&io, (p), (ep), (len), (with), loc)) \
1f2f436a 427 goto error; \
e9ce8d39
A
428}
429#define FLUSH() { \
ad3c9f2a 430 if (io_flush(&io, loc)) \
e9ce8d39 431 goto error; \
e9ce8d39
A
432}
433
9385eb3d
A
434 /*
435 * Get the argument indexed by nextarg. If the argument table is
436 * built, use it to get the argument. If its not, get the next
437 * argument (and arguments must be gotten sequentially).
438 */
439#define GETARG(type) \
440 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
441 (nextarg++, va_arg(ap, type)))
e9ce8d39
A
442
443 /*
444 * To extend shorts properly, we need both signed and unsigned
445 * argument extraction methods.
446 */
447#define SARG() \
448 (flags&LONGINT ? GETARG(long) : \
449 flags&SHORTINT ? (long)(short)GETARG(int) : \
9385eb3d 450 flags&CHARINT ? (long)(signed char)GETARG(int) : \
e9ce8d39
A
451 (long)GETARG(int))
452#define UARG() \
453 (flags&LONGINT ? GETARG(u_long) : \
454 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
9385eb3d 455 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
e9ce8d39 456 (u_long)GETARG(u_int))
9385eb3d
A
457#define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
458#define SJARG() \
459 (flags&INTMAXT ? GETARG(intmax_t) : \
1f2f436a 460 flags&SIZET ? (intmax_t)GETARG(ssize_t) : \
9385eb3d
A
461 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
462 (intmax_t)GETARG(long long))
463#define UJARG() \
464 (flags&INTMAXT ? GETARG(uintmax_t) : \
465 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
ad3c9f2a 466 flags&PTRDIFFT ? (uintmax_t)(unsigned long)GETARG(ptrdiff_t) : \
9385eb3d 467 (uintmax_t)GETARG(unsigned long long))
e9ce8d39 468
9385eb3d
A
469 /*
470 * Get * arguments, including the form *nn$. Preserve the nextarg
471 * that the argument can be gotten once the type is determined.
472 */
e9ce8d39 473#define GETASTER(val) \
9385eb3d
A
474 n2 = 0; \
475 cp = fmt; \
476 while (is_digit(*cp)) { \
477 n2 = 10 * n2 + to_digit(*cp); \
478 cp++; \
479 } \
480 if (*cp == '$') { \
481 int hold = nextarg; \
482 if (argtable == NULL) { \
483 argtable = statargtable; \
1f2f436a
A
484 if (__find_arguments (fmt0, orgap, &argtable)) { \
485 ret = EOF; \
486 goto error; \
487 } \
9385eb3d
A
488 } \
489 nextarg = n2; \
e9ce8d39 490 val = GETARG (int); \
9385eb3d
A
491 nextarg = hold; \
492 fmt = ++cp; \
493 } else { \
494 val = GETARG (int); \
495 }
496
6465356a
A
497 /* The following has been moved to __v2printf() */
498#if 0
e9ce8d39 499 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
ad3c9f2a
A
500 if (prepwrite(fp) != 0) {
501 errno = EBADF;
e9ce8d39 502 return (EOF);
ad3c9f2a
A
503 }
504 ORIENT(fp, -1);
6465356a 505#endif
e9ce8d39 506
1f2f436a 507 convbuf = NULL;
e9ce8d39 508 fmt = (char *)fmt0;
9385eb3d
A
509 argtable = NULL;
510 nextarg = 1;
511 va_copy(orgap, ap);
1f2f436a 512 io_init(&io, fp);
e9ce8d39 513 ret = 0;
1f2f436a
A
514#ifndef NO_FLOATING_POINT
515 dtoaresult = NULL;
ad3c9f2a 516 decimal_point = localeconv_l(loc)->decimal_point;
1f2f436a
A
517 /* The overwhelmingly common case is decpt_len == 1. */
518 decpt_len = (decimal_point[1] == '\0' ? 1 : strlen(decimal_point));
519#endif
e9ce8d39
A
520
521 /*
522 * Scan the format for conversions (`%' character).
523 */
524 for (;;) {
525 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
526 /* void */;
527 if ((n = fmt - cp) != 0) {
b061a43b 528 if (ret + n >= INT_MAX) {
e9ce8d39 529 ret = EOF;
b061a43b 530 errno = EOVERFLOW;
e9ce8d39
A
531 goto error;
532 }
533 PRINT(cp, n);
534 ret += n;
535 }
536 if (ch == '\0')
537 goto done;
ad3c9f2a
A
538#ifdef VECTORS
539 pct = fmt;
540#endif /* VECTORS */
e9ce8d39
A
541 fmt++; /* skip over '%' */
542
543 flags = 0;
544 dprec = 0;
545 width = 0;
546 prec = -1;
1f2f436a 547 gs.grouping = NULL;
e9ce8d39 548 sign = '\0';
9385eb3d 549 ox[1] = '\0';
ad3c9f2a
A
550#ifdef VECTORS
551 vsep = 'X'; /* Illegal value, changed to defaults later. */
552#endif /* VECTORS */
e9ce8d39
A
553
554rflag: ch = *fmt++;
555reswitch: switch (ch) {
556 case ' ':
9385eb3d 557 /*-
e9ce8d39
A
558 * ``If the space and + flags both appear, the space
559 * flag will be ignored.''
560 * -- ANSI X3J11
561 */
562 if (!sign)
563 sign = ' ';
564 goto rflag;
565 case '#':
566 flags |= ALT;
567 goto rflag;
ad3c9f2a
A
568#ifdef VECTORS
569 case ',': case ';': case ':': case '_':
570 vsep = ch;
571 goto rflag;
572#endif /* VECTORS */
e9ce8d39 573 case '*':
9385eb3d 574 /*-
e9ce8d39
A
575 * ``A negative field width argument is taken as a
576 * - flag followed by a positive field width.''
577 * -- ANSI X3J11
578 * They don't exclude field widths read from args.
579 */
580 GETASTER (width);
581 if (width >= 0)
582 goto rflag;
583 width = -width;
584 /* FALLTHROUGH */
585 case '-':
586 flags |= LADJUST;
587 goto rflag;
588 case '+':
589 sign = '+';
590 goto rflag;
9385eb3d
A
591 case '\'':
592 flags |= GROUPING;
9385eb3d 593 goto rflag;
e9ce8d39
A
594 case '.':
595 if ((ch = *fmt++) == '*') {
9385eb3d 596 GETASTER (prec);
e9ce8d39
A
597 goto rflag;
598 }
9385eb3d 599 prec = 0;
e9ce8d39 600 while (is_digit(ch)) {
9385eb3d 601 prec = 10 * prec + to_digit(ch);
e9ce8d39
A
602 ch = *fmt++;
603 }
e9ce8d39
A
604 goto reswitch;
605 case '0':
9385eb3d 606 /*-
e9ce8d39
A
607 * ``Note that 0 is taken as a flag, not as the
608 * beginning of a field width.''
609 * -- ANSI X3J11
610 */
611 flags |= ZEROPAD;
612 goto rflag;
613 case '1': case '2': case '3': case '4':
614 case '5': case '6': case '7': case '8': case '9':
615 n = 0;
616 do {
617 n = 10 * n + to_digit(ch);
618 ch = *fmt++;
619 } while (is_digit(ch));
620 if (ch == '$') {
621 nextarg = n;
9385eb3d
A
622 if (argtable == NULL) {
623 argtable = statargtable;
1f2f436a
A
624 if (__find_arguments (fmt0, orgap,
625 &argtable)) {
626 ret = EOF;
627 goto error;
628 }
e9ce8d39
A
629 }
630 goto rflag;
9385eb3d 631 }
e9ce8d39
A
632 width = n;
633 goto reswitch;
59e0d9fe 634#ifndef NO_FLOATING_POINT
e9ce8d39
A
635 case 'L':
636 flags |= LONGDBL;
637 goto rflag;
638#endif
639 case 'h':
9385eb3d
A
640 if (flags & SHORTINT) {
641 flags &= ~SHORTINT;
642 flags |= CHARINT;
643 } else
644 flags |= SHORTINT;
645 goto rflag;
646 case 'j':
647 flags |= INTMAXT;
e9ce8d39
A
648 goto rflag;
649 case 'l':
9385eb3d
A
650 if (flags & LONGINT) {
651 flags &= ~LONGINT;
652 flags |= LLONGINT;
653 } else
e9ce8d39
A
654 flags |= LONGINT;
655 goto rflag;
656 case 'q':
9385eb3d
A
657 flags |= LLONGINT; /* not necessarily */
658 goto rflag;
659 case 't':
660 flags |= PTRDIFFT;
e9ce8d39 661 goto rflag;
5b2abdfb 662 case 'z':
9385eb3d 663 flags |= SIZET;
5b2abdfb 664 goto rflag;
9385eb3d
A
665 case 'C':
666 flags |= LONGINT;
667 /*FALLTHROUGH*/
e9ce8d39 668 case 'c':
ad3c9f2a
A
669#ifdef VECTORS
670 if (flags & VECTOR)
671 break;
672#endif /* VECTORS */
9385eb3d 673 if (flags & LONGINT) {
59e0d9fe 674 static const mbstate_t initial;
9385eb3d
A
675 mbstate_t mbs;
676 size_t mbseqlen;
677
59e0d9fe 678 mbs = initial;
ad3c9f2a
A
679 mbseqlen = wcrtomb_l(cp = buf,
680 (wchar_t)GETARG(wint_t), &mbs, loc);
9385eb3d
A
681 if (mbseqlen == (size_t)-1) {
682 fp->_flags |= __SERR;
683 goto error;
684 }
685 size = (int)mbseqlen;
686 } else {
687 *(cp = buf) = GETARG(int);
688 size = 1;
5b2abdfb 689 }
e9ce8d39
A
690 sign = '\0';
691 break;
692 case 'D':
693 flags |= LONGINT;
694 /*FALLTHROUGH*/
695 case 'd':
696 case 'i':
ad3c9f2a
A
697#ifdef VECTORS
698 if (flags & VECTOR)
699 break;
700#endif /* VECTORS */
9385eb3d
A
701 if (flags & INTMAX_SIZE) {
702 ujval = SJARG();
703 if ((intmax_t)ujval < 0) {
704 ujval = -ujval;
e9ce8d39
A
705 sign = '-';
706 }
707 } else {
708 ulval = SARG();
709 if ((long)ulval < 0) {
710 ulval = -ulval;
711 sign = '-';
712 }
713 }
714 base = 10;
715 goto number;
59e0d9fe 716#ifndef NO_FLOATING_POINT
9385eb3d
A
717 case 'a':
718 case 'A':
ad3c9f2a
A
719#ifdef VECTORS
720 if (flags & VECTOR) {
721 flags |= FPT;
722 break;
723 }
724#endif /* VECTORS */
9385eb3d
A
725 if (ch == 'a') {
726 ox[1] = 'x';
727 xdigs = xdigs_lower;
728 expchar = 'p';
729 } else {
730 ox[1] = 'X';
731 xdigs = xdigs_upper;
732 expchar = 'P';
733 }
59e0d9fe
A
734 if (prec >= 0)
735 prec++;
736 if (dtoaresult != NULL)
737 freedtoa(dtoaresult);
9385eb3d 738 if (flags & LONGDBL) {
59e0d9fe 739 fparg.ldbl = GETARG(long double);
9385eb3d
A
740 dtoaresult = cp =
741 __hldtoa(fparg.ldbl, xdigs, prec,
742 &expt, &signflag, &dtoaend);
743 } else {
744 fparg.dbl = GETARG(double);
745 dtoaresult = cp =
746 __hdtoa(fparg.dbl, xdigs, prec,
747 &expt, &signflag, &dtoaend);
748 }
59e0d9fe
A
749 if (prec < 0)
750 prec = dtoaend - cp;
751 if (expt == INT_MAX)
752 ox[1] = '\0';
753 goto fp_common;
e9ce8d39
A
754 case 'e':
755 case 'E':
ad3c9f2a
A
756#ifdef VECTORS
757 if (flags & VECTOR) {
758 flags |= FPT;
759 break;
760 }
761#endif /* VECTORS */
9385eb3d
A
762 expchar = ch;
763 if (prec < 0) /* account for digit before decpt */
764 prec = DEFPREC + 1;
765 else
766 prec++;
767 goto fp_begin;
e9ce8d39 768 case 'f':
9385eb3d 769 case 'F':
ad3c9f2a
A
770#ifdef VECTORS
771 if (flags & VECTOR) {
772 flags |= FPT;
773 break;
774 }
775#endif /* VECTORS */
9385eb3d 776 expchar = '\0';
e9ce8d39
A
777 goto fp_begin;
778 case 'g':
779 case 'G':
ad3c9f2a
A
780#ifdef VECTORS
781 if (flags & VECTOR) {
782 flags |= FPT;
783 break;
784 }
785#endif /* VECTORS */
9385eb3d 786 expchar = ch - ('g' - 'e');
e9ce8d39
A
787 if (prec == 0)
788 prec = 1;
9385eb3d
A
789fp_begin:
790 if (prec < 0)
e9ce8d39 791 prec = DEFPREC;
9385eb3d
A
792 if (dtoaresult != NULL)
793 freedtoa(dtoaresult);
794 if (flags & LONGDBL) {
795 fparg.ldbl = GETARG(long double);
796 dtoaresult = cp =
797 __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec,
798 &expt, &signflag, &dtoaend);
799 } else {
800 fparg.dbl = GETARG(double);
801 dtoaresult = cp =
802 dtoa(fparg.dbl, expchar ? 2 : 3, prec,
803 &expt, &signflag, &dtoaend);
804 if (expt == 9999)
805 expt = INT_MAX;
e9ce8d39 806 }
59e0d9fe 807fp_common:
9385eb3d
A
808 if (signflag)
809 sign = '-';
810 if (expt == INT_MAX) { /* inf or nan */
811 if (*cp == 'N') {
812 cp = (ch >= 'a') ? "nan" : "NAN";
813 sign = '\0';
814 } else
815 cp = (ch >= 'a') ? "inf" : "INF";
e9ce8d39 816 size = 3;
1f2f436a 817 flags &= ~ZEROPAD;
e9ce8d39
A
818 break;
819 }
820 flags |= FPT;
9385eb3d 821 ndig = dtoaend - cp;
e9ce8d39 822 if (ch == 'g' || ch == 'G') {
9385eb3d
A
823 if (expt > -4 && expt <= prec) {
824 /* Make %[gG] smell like %[fF] */
825 expchar = '\0';
826 if (flags & ALT)
827 prec -= expt;
828 else
829 prec = ndig - expt;
830 if (prec < 0)
831 prec = 0;
832 } else {
833 /*
834 * Make %[gG] smell like %[eE], but
835 * trim trailing zeroes if no # flag.
836 */
837 if (!(flags & ALT))
838 prec = ndig;
839 }
e9ce8d39 840 }
9385eb3d
A
841 if (expchar) {
842 expsize = exponent(expstr, expt - 1, expchar);
843 size = expsize + prec;
844 if (prec > 1 || flags & ALT)
1f2f436a 845 size += decpt_len;
9385eb3d
A
846 } else {
847 /* space for digits before decimal point */
848 if (expt > 0)
e9ce8d39 849 size = expt;
9385eb3d
A
850 else /* "0" */
851 size = 1;
852 /* space for decimal pt and following digits */
853 if (prec || flags & ALT)
1f2f436a
A
854 size += prec + decpt_len;
855 if ((flags & GROUPING) && expt > 0)
ad3c9f2a 856 size += grouping_init(&gs, expt, loc);
9385eb3d 857 }
e9ce8d39 858 break;
59e0d9fe 859#endif /* !NO_FLOATING_POINT */
e9ce8d39 860 case 'n':
ad3c9f2a 861 {
9385eb3d
A
862 /*
863 * Assignment-like behavior is specified if the
864 * value overflows or is otherwise unrepresentable.
865 * C99 says to use `signed char' for %hhn conversions.
866 */
ad3c9f2a
A
867 void *ptr = GETARG(void *);
868 if (ptr == NULL)
869 continue;
b061a43b
A
870
871#ifndef ALLOW_DYNAMIC_PERCENT_N
872 if (!static_format_checked) {
873 static_format_checked = __printf_is_memory_read_only((void*)fmt0, strlen(fmt0));
874 }
875 if (!static_format_checked) {
e07eda1a 876 os_crash("%%n used in a non-immutable format string: %s", fmt0);
b061a43b
A
877 }
878#endif // ALLOW_DYNAMIC_PERCENT_N
879
880 if (flags & LLONGINT)
ad3c9f2a 881 *(long long *)ptr = ret;
9385eb3d 882 else if (flags & SIZET)
ad3c9f2a 883 *(ssize_t *)ptr = (ssize_t)ret;
9385eb3d 884 else if (flags & PTRDIFFT)
ad3c9f2a 885 *(ptrdiff_t *)ptr = ret;
9385eb3d 886 else if (flags & INTMAXT)
ad3c9f2a 887 *(intmax_t *)ptr = ret;
e9ce8d39 888 else if (flags & LONGINT)
ad3c9f2a 889 *(long *)ptr = ret;
e9ce8d39 890 else if (flags & SHORTINT)
ad3c9f2a 891 *(short *)ptr = ret;
9385eb3d 892 else if (flags & CHARINT)
ad3c9f2a 893 *(signed char *)ptr = ret;
e9ce8d39 894 else
ad3c9f2a 895 *(int *)ptr = ret;
e9ce8d39 896 continue; /* no output */
ad3c9f2a 897 }
e9ce8d39
A
898 case 'O':
899 flags |= LONGINT;
900 /*FALLTHROUGH*/
901 case 'o':
ad3c9f2a
A
902#ifdef VECTORS
903 if (flags & VECTOR)
904 break;
905#endif /* VECTORS */
9385eb3d
A
906 if (flags & INTMAX_SIZE)
907 ujval = UJARG();
e9ce8d39
A
908 else
909 ulval = UARG();
910 base = 8;
911 goto nosign;
912 case 'p':
9385eb3d 913 /*-
e9ce8d39
A
914 * ``The argument shall be a pointer to void. The
915 * value of the pointer is converted to a sequence
916 * of printable characters, in an implementation-
917 * defined manner.''
918 * -- ANSI X3J11
919 */
ad3c9f2a
A
920#ifdef VECTORS
921 if (flags & VECTOR)
922 break;
923#endif /* VECTORS */
9385eb3d 924 ujval = (uintmax_t)(uintptr_t)GETARG(void *);
e9ce8d39 925 base = 16;
9385eb3d
A
926 xdigs = xdigs_lower;
927 flags = flags | INTMAXT;
928 ox[1] = 'x';
e9ce8d39 929 goto nosign;
9385eb3d
A
930 case 'S':
931 flags |= LONGINT;
932 /*FALLTHROUGH*/
e9ce8d39 933 case 's':
9385eb3d
A
934 if (flags & LONGINT) {
935 wchar_t *wcp;
936
974e3884
A
937 free(convbuf);
938 if ((wcp = GETARG(wchar_t *)) == NULL) {
939 convbuf = NULL;
9385eb3d 940 cp = "(null)";
974e3884 941 } else {
ad3c9f2a 942 convbuf = __wcsconv(wcp, prec, loc);
9385eb3d
A
943 if (convbuf == NULL) {
944 fp->_flags |= __SERR;
945 goto error;
946 }
947 cp = convbuf;
948 }
949 } else if ((cp = GETARG(char *)) == NULL)
e9ce8d39 950 cp = "(null)";
b061a43b
A
951 {
952 size_t cp_len = (prec >= 0) ? strnlen(cp, prec) : strlen(cp);
953 if (cp_len < INT_MAX) {
954 size = cp_len;
955 } else {
956 ret = EOF;
957 goto error;
958 }
959 }
e9ce8d39
A
960 sign = '\0';
961 break;
962 case 'U':
963 flags |= LONGINT;
964 /*FALLTHROUGH*/
965 case 'u':
ad3c9f2a
A
966#ifdef VECTORS
967 if (flags & VECTOR)
968 break;
969#endif /* VECTORS */
9385eb3d
A
970 if (flags & INTMAX_SIZE)
971 ujval = UJARG();
e9ce8d39
A
972 else
973 ulval = UARG();
974 base = 10;
975 goto nosign;
976 case 'X':
9385eb3d 977 xdigs = xdigs_upper;
e9ce8d39
A
978 goto hex;
979 case 'x':
9385eb3d 980 xdigs = xdigs_lower;
5b2abdfb 981hex:
ad3c9f2a
A
982#ifdef VECTORS
983 if (flags & VECTOR)
984 break;
985#endif /* VECTORS */
9385eb3d
A
986 if (flags & INTMAX_SIZE)
987 ujval = UJARG();
e9ce8d39
A
988 else
989 ulval = UARG();
990 base = 16;
991 /* leading 0x/X only if non-zero */
992 if (flags & ALT &&
9385eb3d
A
993 (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0))
994 ox[1] = ch;
e9ce8d39 995
9385eb3d 996 flags &= ~GROUPING;
e9ce8d39
A
997 /* unsigned conversions */
998nosign: sign = '\0';
9385eb3d 999 /*-
e9ce8d39
A
1000 * ``... diouXx conversions ... if a precision is
1001 * specified, the 0 flag will be ignored.''
1002 * -- ANSI X3J11
ad3c9f2a 1003 * except for %#.0o and zero value
e9ce8d39
A
1004 */
1005number: if ((dprec = prec) >= 0)
1006 flags &= ~ZEROPAD;
1007
9385eb3d 1008 /*-
e9ce8d39
A
1009 * ``The result of converting a zero value with an
1010 * explicit precision of zero is no characters.''
1011 * -- ANSI X3J11
1f2f436a
A
1012 *
1013 * ``The C Standard is clear enough as is. The call
1014 * printf("%#.0o", 0) should print 0.''
1015 * -- Defect Report #151
e9ce8d39
A
1016 */
1017 cp = buf + BUF;
9385eb3d 1018 if (flags & INTMAX_SIZE) {
1f2f436a
A
1019 if (ujval != 0 || prec != 0 ||
1020 (flags & ALT && base == 8))
9385eb3d 1021 cp = __ujtoa(ujval, cp, base,
1f2f436a 1022 flags & ALT, xdigs);
e9ce8d39 1023 } else {
1f2f436a
A
1024 if (ulval != 0 || prec != 0 ||
1025 (flags & ALT && base == 8))
e9ce8d39 1026 cp = __ultoa(ulval, cp, base,
1f2f436a 1027 flags & ALT, xdigs);
e9ce8d39
A
1028 }
1029 size = buf + BUF - cp;
9385eb3d 1030 if (size > BUF) /* should never happen */
b061a43b 1031 LIBC_ABORT("size (%zd) > BUF (%d)", size, BUF);
1f2f436a 1032 if ((flags & GROUPING) && size != 0)
ad3c9f2a 1033 size += grouping_init(&gs, size, loc);
e9ce8d39 1034 break;
ad3c9f2a
A
1035#ifdef VECTORS
1036 case 'v':
1037 flags |= VECTOR;
1038 goto rflag;
1039#endif /* VECTORS */
e9ce8d39
A
1040 default: /* "%?" prints ?, unless ? is NUL */
1041 if (ch == '\0')
1042 goto done;
1043 /* pretend it was %c with argument ch */
1044 cp = buf;
1045 *cp = ch;
1046 size = 1;
1047 sign = '\0';
1048 break;
1049 }
1050
ad3c9f2a
A
1051#ifdef VECTORS
1052 if (flags & VECTOR) {
1053 /*
1054 * Do the minimum amount of work necessary to construct
1055 * a format specifier that can be used to recursively
1056 * call vfprintf() for each element in the vector.
1057 */
1058 int i, j; /* Counter. */
1059 int vcnt; /* Number of elements in vector. */
1060 char *vfmt; /* Pointer to format specifier. */
1061#define EXTRAHH 2
1062 char vfmt_buf[32 + EXTRAHH]; /* Static buffer for format spec. */
1063 int vwidth = 0; /* Width specified via '*'. */
1064 int vprec = 0; /* Precision specified via '*'. */
1065 char *vstr; /* Used for asprintf(). */
1066 int vlen; /* Length returned by asprintf(). */
1067 enum {
1068 V_CHAR, V_SHORT, V_INT,
1069 V_PCHAR, V_PSHORT, V_PINT,
1070 V_FLOAT,
1071#ifdef V64TYPE
1072 V_LONGLONG, V_PLONGLONG,
1073 V_DOUBLE,
1074#endif /* V64TYPE */
1075 } vtype;
1076
1077 vval.vectorarg = GETARG(VECTORTYPE);
1078 /*
1079 * Set vfmt. If vfmt_buf may not be big enough,
1080 * malloc() space, taking care to free it later.
1081 * (EXTRAHH is for possible extra "hh")
1082 */
1083 if (&fmt[-1] - pct + EXTRAHH < sizeof(vfmt_buf))
1084 vfmt = vfmt_buf;
1085 else
1086 vfmt = (char *)malloc(&fmt[-1] - pct + EXTRAHH + 1);
1087
1088 /* Set the separator character, if not specified. */
1089 if (vsep == 'X') {
1090 if (ch == 'c')
1091 vsep = '\0';
1092 else
1093 vsep = ' ';
1094 }
1095
1096 /* Create the format specifier. */
1097 for (i = j = 0; i < &fmt[-1] - pct; i++) {
1098 switch (pct[i]) {
1099 case ',': case ';': case ':': case '_':
1100 case 'v': case 'h': case 'l':
1101 /* Ignore. */
1102 break;
1103 case '*':
1104 if (pct[i - 1] != '.')
1105 vwidth = 1;
1106 else
1107 vprec = 1;
1108 /* FALLTHROUGH */
1109 default:
1110 vfmt[j++] = pct[i];
1111 }
1112 }
1113
1114 /*
1115 * Determine the number of elements in the vector and
1116 * finish up the format specifier.
1117 */
1118 if (flags & SHORTINT) {
1119 switch (ch) {
1120 case 'c':
1121 vtype = V_SHORT;
1122 break;
1123 case 'p':
1124 vtype = V_PSHORT;
1125 break;
1126 default:
1127 vfmt[j++] = 'h';
1128 vtype = V_SHORT;
1129 break;
1130 }
1131 vcnt = 8;
1132 } else if (flags & LONGINT) {
1133 vcnt = 4;
1134 vtype = (ch == 'p') ? V_PINT : V_INT;
1135#ifdef V64TYPE
1136 } else if (flags & LLONGINT) {
1137 switch (ch) {
1138 case 'a':
1139 case 'A':
1140 case 'e':
1141 case 'E':
1142 case 'f':
1143 case 'g':
1144 case 'G':
1145 vcnt = 2;
1146 vtype = V_DOUBLE;
1147 break;
1148 case 'd':
1149 case 'i':
1150 case 'u':
1151 case 'o':
1152 case 'p':
1153 case 'x':
1154 case 'X':
1155 vfmt[j++] = 'l';
1156 vfmt[j++] = 'l';
1157 vcnt = 2;
1158 vtype = (ch == 'p') ? V_PLONGLONG : V_LONGLONG;
1159 break;
1160 default:
1161 /*
1162 * The default case should never
1163 * happen.
1164 */
1165 case 'c':
1166 vcnt = 16;
1167 vtype = V_CHAR;
1168 }
1169#endif /* V64TYPE */
1170 } else {
1171 switch (ch) {
1172 case 'a':
1173 case 'A':
1174 case 'e':
1175 case 'E':
1176 case 'f':
1177 case 'g':
1178 case 'G':
1179 vcnt = 4;
1180 vtype = V_FLOAT;
1181 break;
1182 default:
1183 /*
1184 * The default case should never
1185 * happen.
1186 */
1187 case 'd':
1188 case 'i':
1189 case 'u':
1190 case 'o':
1191 case 'x':
1192 case 'X':
1193 vfmt[j++] = 'h';
1194 vfmt[j++] = 'h';
1195 /* drop through */
1196 case 'p':
1197 case 'c':
1198 vcnt = 16;
1199 vtype = (ch == 'p') ? V_PCHAR : V_CHAR;
1200 }
1201 }
1202 vfmt[j++] = ch;
1203 vfmt[j++] = '\0';
1204
1205/* Get a vector element. */
1206#ifdef V64TYPE
1207#define VPRINT(type, ind, args...) do { \
1208 switch (type) { \
1209 case V_CHAR: \
1210 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
1211 break; \
1212 case V_PCHAR: \
1213 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \
1214 break; \
1215 case V_SHORT: \
1216 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
1217 break; \
1218 case V_PSHORT: \
1219 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \
1220 break; \
1221 case V_INT: \
1222 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
1223 break; \
1224 case V_PINT: \
1225 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \
1226 break; \
1227 case V_LONGLONG: \
1228 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vulonglongarg[ind]); \
1229 break; \
1230 case V_PLONGLONG: \
1231 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vulonglongarg[ind]); \
1232 break; \
1233 case V_FLOAT: \
1234 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
1235 break; \
1236 case V_DOUBLE: \
1237 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vdoublearg[ind]); \
1238 break; \
1239 } \
1240 ret += vlen; \
1241 PRINT(vstr, vlen); \
1242 FLUSH(); \
1243 free(vstr); \
1244} while (0)
1245#else /* !V64TYPE */
1246#define VPRINT(type, ind, args...) do { \
1247 switch (type) { \
1248 case V_CHAR: \
1249 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
1250 break; \
1251 case V_PCHAR: \
1252 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \
1253 break; \
1254 case V_SHORT: \
1255 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
1256 break; \
1257 case V_PSHORT: \
1258 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \
1259 break; \
1260 case V_INT: \
1261 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
1262 break; \
1263 case V_PINT: \
1264 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \
1265 break; \
1266 case V_FLOAT: \
1267 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
1268 break; \
1269 } \
1270 ret += vlen; \
1271 PRINT(vstr, vlen); \
1272 FLUSH(); \
1273 free(vstr); \
1274} while (0)
1275#endif /* V64TYPE */
1276
1277 /* Actually print. */
1278 if (vwidth == 0) {
1279 if (vprec == 0) {
1280 /* First element. */
1281 VPRINT(vtype, 0);
1282 for (i = 1; i < vcnt; i++) {
1283 /* Separator. */
1284 if(vsep)
1285 PRINT(&vsep, 1);
1286
1287 /* Element. */
1288 VPRINT(vtype, i);
1289 }
1290 } else {
1291 /* First element. */
1292 VPRINT(vtype, 0, prec);
1293 for (i = 1; i < vcnt; i++) {
1294 /* Separator. */
1295 if(vsep)
1296 PRINT(&vsep, 1);
1297
1298 /* Element. */
1299 VPRINT(vtype, i, prec);
1300 }
1301 }
1302 } else {
1303 if (vprec == 0) {
1304 /* First element. */
1305 VPRINT(vtype, 0, width);
1306 for (i = 1; i < vcnt; i++) {
1307 /* Separator. */
1308 if(vsep)
1309 PRINT(&vsep, 1);
1310
1311 /* Element. */
1312 VPRINT(vtype, i, width);
1313 }
1314 } else {
1315 /* First element. */
1316 VPRINT(vtype, 0, width, prec);
1317 for (i = 1; i < vcnt; i++) {
1318 /* Separator. */
1319 if(vsep)
1320 PRINT(&vsep, 1);
1321
1322 /* Element. */
1323 VPRINT(vtype, i, width, prec);
1324 }
1325 }
1326 }
1327#undef VPRINT
1328
1329 if (vfmt != vfmt_buf)
1330 free(vfmt);
1331
1332 continue;
1333 }
1334#endif /* VECTORS */
e9ce8d39
A
1335 /*
1336 * All reasonable formats wind up here. At this point, `cp'
1337 * points to a string which (if not flags&LADJUST) should be
1338 * padded out to `width' places. If flags&ZEROPAD, it should
1339 * first be prefixed by any sign or other prefix; otherwise,
1340 * it should be blank padded before the prefix is emitted.
1341 * After any left-hand padding and prefixing, emit zeroes
1342 * required by a decimal [diouxX] precision, then print the
1343 * string proper, then emit zeroes required by any leftover
1344 * floating precision; finally, if LADJUST, pad with blanks.
1345 *
1346 * Compute actual size, so we know how much to pad.
1347 * size excludes decimal prec; realsz includes it.
1348 */
1349 realsz = dprec > size ? dprec : size;
1350 if (sign)
1351 realsz++;
59e0d9fe 1352 if (ox[1])
e9ce8d39
A
1353 realsz += 2;
1354
1355 prsize = width > realsz ? width : realsz;
b061a43b 1356 if (ret + prsize >= INT_MAX) {
e9ce8d39 1357 ret = EOF;
b061a43b 1358 errno = EOVERFLOW;
e9ce8d39
A
1359 goto error;
1360 }
1361
1362 /* right-adjusting blank padding */
1363 if ((flags & (LADJUST|ZEROPAD)) == 0)
1364 PAD(width - realsz, blanks);
1365
1366 /* prefix */
59e0d9fe 1367 if (sign)
e9ce8d39 1368 PRINT(&sign, 1);
59e0d9fe
A
1369
1370 if (ox[1]) { /* ox[1] is either x, X, or \0 */
e9ce8d39 1371 ox[0] = '0';
e9ce8d39
A
1372 PRINT(ox, 2);
1373 }
1374
1375 /* right-adjusting zero padding */
1376 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
1377 PAD(width - realsz, zeroes);
1378
e9ce8d39 1379 /* the string or number proper */
59e0d9fe 1380#ifndef NO_FLOATING_POINT
e9ce8d39 1381 if ((flags & FPT) == 0) {
1f2f436a
A
1382#endif
1383 /* leading zeroes from decimal precision */
1384 PAD(dprec - size, zeroes);
1385 if (gs.grouping) {
ad3c9f2a 1386 if (grouping_print(&gs, &io, cp, buf+BUF, loc) < 0)
1f2f436a
A
1387 goto error;
1388 } else {
1389 PRINT(cp, size);
1390 }
1391#ifndef NO_FLOATING_POINT
e9ce8d39 1392 } else { /* glue together f_p fragments */
9385eb3d
A
1393 if (!expchar) { /* %[fF] or sufficiently short %[gG] */
1394 if (expt <= 0) {
1395 PRINT(zeroes, 1);
1396 if (prec || flags & ALT)
1f2f436a 1397 PRINT(decimal_point,decpt_len);
e9ce8d39 1398 PAD(-expt, zeroes);
9385eb3d
A
1399 /* already handled initial 0's */
1400 prec += expt;
e9ce8d39 1401 } else {
1f2f436a
A
1402 if (gs.grouping) {
1403 n = grouping_print(&gs, &io,
ad3c9f2a 1404 cp, dtoaend, loc);
1f2f436a
A
1405 if (n < 0)
1406 goto error;
1407 cp += n;
1408 } else {
1409 PRINTANDPAD(cp, dtoaend,
1410 expt, zeroes);
1411 cp += expt;
9385eb3d
A
1412 }
1413 if (prec || flags & ALT)
1f2f436a 1414 PRINT(decimal_point,decpt_len);
e9ce8d39 1415 }
9385eb3d
A
1416 PRINTANDPAD(cp, dtoaend, prec, zeroes);
1417 } else { /* %[eE] or sufficiently long %[gG] */
1418 if (prec > 1 || flags & ALT) {
1f2f436a
A
1419 PRINT(cp++, 1);
1420 PRINT(decimal_point, decpt_len);
9385eb3d
A
1421 PRINT(cp, ndig-1);
1422 PAD(prec - ndig, zeroes);
e9ce8d39
A
1423 } else /* XeYYY */
1424 PRINT(cp, 1);
1425 PRINT(expstr, expsize);
1426 }
1427 }
e9ce8d39
A
1428#endif
1429 /* left-adjusting padding (always blank) */
1430 if (flags & LADJUST)
1431 PAD(width - realsz, blanks);
1432
1433 /* finally, adjust ret */
1434 ret += prsize;
1435
1436 FLUSH(); /* copy out the I/O vectors */
1437 }
1438done:
1439 FLUSH();
1440error:
3d9156a7 1441 va_end(orgap);
59e0d9fe 1442#ifndef NO_FLOATING_POINT
3b2a1fe8 1443 if (dtoaresult != NULL)
9385eb3d 1444 freedtoa(dtoaresult);
3b2a1fe8 1445#endif
974e3884 1446 free(convbuf);
e9ce8d39
A
1447 if (__sferror(fp))
1448 ret = EOF;
9385eb3d
A
1449 if ((argtable != NULL) && (argtable != statargtable))
1450 free (argtable);
b061a43b 1451 return (ret < 0 || ret >= INT_MAX) ? -1 : (int)ret;
e9ce8d39
A
1452 /* NOTREACHED */
1453}
70ad1dc8 1454#pragma clang diagnostic pop