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