1*0a6a1f1dSLionel Sambuc /* $NetBSD: vfwprintf.c,v 1.34 2014/01/20 14:11:03 yamt Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras * Copyright (c) 1990, 1993
52fe8fb19SBen Gras * The Regents of the University of California. All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * This code is derived from software contributed to Berkeley by
82fe8fb19SBen Gras * Chris Torek.
92fe8fb19SBen Gras *
102fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras * are met:
132fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras * 3. Neither the name of the University nor the names of its contributors
192fe8fb19SBen Gras * may be used to endorse or promote products derived from this software
202fe8fb19SBen Gras * without specific prior written permission.
212fe8fb19SBen Gras *
222fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322fe8fb19SBen Gras * SUCH DAMAGE.
332fe8fb19SBen Gras */
342fe8fb19SBen Gras
352fe8fb19SBen Gras #include <sys/cdefs.h>
362fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
372fe8fb19SBen Gras #if 0
382fe8fb19SBen Gras static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93";
392fe8fb19SBen Gras __FBSDID("$FreeBSD: src/lib/libc/stdio/vfwprintf.c,v 1.27 2007/01/09 00:28:08 imp Exp $");
402fe8fb19SBen Gras #else
41*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: vfwprintf.c,v 1.34 2014/01/20 14:11:03 yamt Exp $");
422fe8fb19SBen Gras #endif
432fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
442fe8fb19SBen Gras
452fe8fb19SBen Gras /*
462fe8fb19SBen Gras * Actual {w,}printf innards.
472fe8fb19SBen Gras */
482fe8fb19SBen Gras
492fe8fb19SBen Gras #include "namespace.h"
502fe8fb19SBen Gras #include <sys/types.h>
512fe8fb19SBen Gras
522fe8fb19SBen Gras #include <assert.h>
532fe8fb19SBen Gras #include <ctype.h>
542fe8fb19SBen Gras #include <limits.h>
552fe8fb19SBen Gras #include <locale.h>
562fe8fb19SBen Gras #include <stdarg.h>
572fe8fb19SBen Gras #include <stddef.h>
582fe8fb19SBen Gras #include <stdint.h>
592fe8fb19SBen Gras #include <stdio.h>
602fe8fb19SBen Gras #include <stdlib.h>
612fe8fb19SBen Gras #include <string.h>
622fe8fb19SBen Gras #include <errno.h>
632fe8fb19SBen Gras #include <wchar.h>
642fe8fb19SBen Gras #include <wctype.h>
652fe8fb19SBen Gras
662fe8fb19SBen Gras #include "reentrant.h"
6784d9c625SLionel Sambuc #include "setlocale_local.h"
682fe8fb19SBen Gras #include "local.h"
692fe8fb19SBen Gras #include "extern.h"
702fe8fb19SBen Gras #include "fvwrite.h"
712fe8fb19SBen Gras
722fe8fb19SBen Gras #ifndef NARROW
732fe8fb19SBen Gras #define MCHAR_T char
742fe8fb19SBen Gras #define CHAR_T wchar_t
752fe8fb19SBen Gras #define STRLEN(a) wcslen(a)
762fe8fb19SBen Gras #define MEMCHR(a, b, c) wmemchr(a, b, c)
7784d9c625SLionel Sambuc #define SCONV(a, b, loc) __mbsconv(a, b, loc)
782fe8fb19SBen Gras #define STRCONST(a) L ## a
792fe8fb19SBen Gras #define WDECL(a, b) a ## w ## b
802fe8fb19SBen Gras #define END_OF_FILE WEOF
812fe8fb19SBen Gras #define MULTI 0
822fe8fb19SBen Gras #else
832fe8fb19SBen Gras #define MCHAR_T wchar_t
842fe8fb19SBen Gras #define CHAR_T char
852fe8fb19SBen Gras #define STRLEN(a) strlen(a)
862fe8fb19SBen Gras #define MEMCHR(a, b, c) memchr(a, b, c)
8784d9c625SLionel Sambuc #define SCONV(a, b, loc) __wcsconv(a, b, loc)
882fe8fb19SBen Gras #define STRCONST(a) a
892fe8fb19SBen Gras #define WDECL(a, b) a ## b
902fe8fb19SBen Gras #define END_OF_FILE EOF
912fe8fb19SBen Gras #define MULTI LONGINT
922fe8fb19SBen Gras #endif
932fe8fb19SBen Gras
942fe8fb19SBen Gras union arg {
952fe8fb19SBen Gras int intarg;
962fe8fb19SBen Gras u_int uintarg;
972fe8fb19SBen Gras long longarg;
982fe8fb19SBen Gras u_long ulongarg;
992fe8fb19SBen Gras long long longlongarg;
1002fe8fb19SBen Gras unsigned long long ulonglongarg;
1012fe8fb19SBen Gras ptrdiff_t ptrdiffarg;
1022fe8fb19SBen Gras ssize_t ssizearg;
1032fe8fb19SBen Gras size_t sizearg;
1042fe8fb19SBen Gras intmax_t intmaxarg;
1052fe8fb19SBen Gras uintmax_t uintmaxarg;
1062fe8fb19SBen Gras void *pvoidarg;
1072fe8fb19SBen Gras char *pchararg;
1082fe8fb19SBen Gras signed char *pschararg;
1092fe8fb19SBen Gras short *pshortarg;
1102fe8fb19SBen Gras int *pintarg;
1112fe8fb19SBen Gras long *plongarg;
1122fe8fb19SBen Gras long long *plonglongarg;
1132fe8fb19SBen Gras ptrdiff_t *pptrdiffarg;
1142fe8fb19SBen Gras size_t *psizearg;
1152fe8fb19SBen Gras intmax_t *pintmaxarg;
1162fe8fb19SBen Gras #ifndef NO_FLOATING_POINT
1172fe8fb19SBen Gras double doublearg;
1182fe8fb19SBen Gras long double longdoublearg;
1192fe8fb19SBen Gras #endif
1202fe8fb19SBen Gras wint_t wintarg;
1212fe8fb19SBen Gras wchar_t *pwchararg;
1222fe8fb19SBen Gras };
1232fe8fb19SBen Gras
1242fe8fb19SBen Gras /*
1252fe8fb19SBen Gras * Type ids for argument type table.
1262fe8fb19SBen Gras */
1272fe8fb19SBen Gras enum typeid {
128f14fb602SLionel Sambuc T_UNUSED = 0, TP_SHORT, T_INT, T_U_INT, TP_INT,
1292fe8fb19SBen Gras T_LONG, T_U_LONG, TP_LONG, T_LLONG, T_U_LLONG, TP_LLONG,
1302fe8fb19SBen Gras T_PTRDIFFT, TP_PTRDIFFT, T_SSIZET, T_SIZET, TP_SIZET,
1312fe8fb19SBen Gras T_INTMAXT, T_UINTMAXT, TP_INTMAXT, TP_VOID, TP_CHAR, TP_SCHAR,
1322fe8fb19SBen Gras T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR
1332fe8fb19SBen Gras };
1342fe8fb19SBen Gras
13584d9c625SLionel Sambuc #ifdef NARROW
13684d9c625SLionel Sambuc __printflike(3, 0)
13784d9c625SLionel Sambuc #endif
13884d9c625SLionel Sambuc static int __sbprintf(FILE *, locale_t, const CHAR_T *, va_list);
13984d9c625SLionel Sambuc
1402fe8fb19SBen Gras static CHAR_T *__ujtoa(uintmax_t, CHAR_T *, int, int, const char *, int,
1412fe8fb19SBen Gras char, const char *);
1422fe8fb19SBen Gras static CHAR_T *__ultoa(u_long, CHAR_T *, int, int, const char *, int,
1432fe8fb19SBen Gras char, const char *);
1442fe8fb19SBen Gras #ifndef NARROW
14584d9c625SLionel Sambuc static CHAR_T *__mbsconv(char *, int, locale_t);
14684d9c625SLionel Sambuc static wint_t __xfputwc(CHAR_T, FILE *, locale_t);
1472fe8fb19SBen Gras #else
14884d9c625SLionel Sambuc static char *__wcsconv(wchar_t *, int, locale_t);
1492fe8fb19SBen Gras static int __sprint(FILE *, struct __suio *);
1502fe8fb19SBen Gras #endif
1512fe8fb19SBen Gras static int __find_arguments(const CHAR_T *, va_list, union arg **);
152f14fb602SLionel Sambuc static int __grow_type_table(size_t, enum typeid **, size_t *);
1532fe8fb19SBen Gras
1542fe8fb19SBen Gras /*
1552fe8fb19SBen Gras * Helper function for `fprintf to unbuffered unix file': creates a
1562fe8fb19SBen Gras * temporary buffer. We only work on write-only files; this avoids
1572fe8fb19SBen Gras * worries about ungetc buffers and so forth.
1582fe8fb19SBen Gras */
1592fe8fb19SBen Gras static int
__sbprintf(FILE * fp,locale_t loc,const CHAR_T * fmt,va_list ap)16084d9c625SLionel Sambuc __sbprintf(FILE *fp, locale_t loc, const CHAR_T *fmt, va_list ap)
1612fe8fb19SBen Gras {
1622fe8fb19SBen Gras int ret;
1632fe8fb19SBen Gras FILE fake;
1642fe8fb19SBen Gras struct __sfileext fakeext;
1652fe8fb19SBen Gras unsigned char buf[BUFSIZ];
1662fe8fb19SBen Gras
1672fe8fb19SBen Gras _DIAGASSERT(fp != NULL);
1682fe8fb19SBen Gras _DIAGASSERT(fmt != NULL);
1692fe8fb19SBen Gras
1702fe8fb19SBen Gras _FILEEXT_SETUP(&fake, &fakeext);
1712fe8fb19SBen Gras memset(WCIO_GET(&fake), 0, sizeof(struct wchar_io_data));
1722fe8fb19SBen Gras
1732fe8fb19SBen Gras /* copy the important variables */
1742fe8fb19SBen Gras fake._flags = fp->_flags & ~__SNBF;
1752fe8fb19SBen Gras fake._file = fp->_file;
1762fe8fb19SBen Gras fake._cookie = fp->_cookie;
1772fe8fb19SBen Gras fake._write = fp->_write;
178f14fb602SLionel Sambuc fake._flush = fp->_flush;
1792fe8fb19SBen Gras
1802fe8fb19SBen Gras /* set up the buffer */
1812fe8fb19SBen Gras fake._bf._base = fake._p = buf;
1822fe8fb19SBen Gras fake._bf._size = fake._w = sizeof(buf);
1832fe8fb19SBen Gras fake._lbfsize = 0; /* not actually used, but Just In Case */
1842fe8fb19SBen Gras
1852fe8fb19SBen Gras /* do the work, then copy any error status */
18684d9c625SLionel Sambuc ret = WDECL(__vf,printf_unlocked_l)(&fake, loc, fmt, ap);
1872fe8fb19SBen Gras if (ret >= 0 && fflush(&fake))
1882fe8fb19SBen Gras ret = END_OF_FILE;
1892fe8fb19SBen Gras if (fake._flags & __SERR)
1902fe8fb19SBen Gras fp->_flags |= __SERR;
191f14fb602SLionel Sambuc return ret;
1922fe8fb19SBen Gras }
1932fe8fb19SBen Gras
1942fe8fb19SBen Gras #ifndef NARROW
1952fe8fb19SBen Gras /*
1962fe8fb19SBen Gras * Like __fputwc, but handles fake string (__SSTR) files properly.
1972fe8fb19SBen Gras * File must already be locked.
1982fe8fb19SBen Gras */
1992fe8fb19SBen Gras static wint_t
__xfputwc(wchar_t wc,FILE * fp,locale_t loc)20084d9c625SLionel Sambuc __xfputwc(wchar_t wc, FILE *fp, locale_t loc)
2012fe8fb19SBen Gras {
2022fe8fb19SBen Gras static const mbstate_t initial;
2032fe8fb19SBen Gras mbstate_t mbs;
2042fe8fb19SBen Gras char buf[MB_LEN_MAX];
2052fe8fb19SBen Gras struct __suio uio;
2062fe8fb19SBen Gras struct __siov iov;
2072fe8fb19SBen Gras size_t len;
2082fe8fb19SBen Gras
2092fe8fb19SBen Gras if ((fp->_flags & __SSTR) == 0)
210f14fb602SLionel Sambuc return __fputwc_unlock(wc, fp);
2112fe8fb19SBen Gras
2122fe8fb19SBen Gras mbs = initial;
21384d9c625SLionel Sambuc if ((len = wcrtomb_l(buf, wc, &mbs, loc)) == (size_t)-1) {
2142fe8fb19SBen Gras fp->_flags |= __SERR;
215f14fb602SLionel Sambuc return END_OF_FILE;
2162fe8fb19SBen Gras }
2172fe8fb19SBen Gras uio.uio_iov = &iov;
2182fe8fb19SBen Gras uio.uio_resid = len;
2192fe8fb19SBen Gras uio.uio_iovcnt = 1;
2202fe8fb19SBen Gras iov.iov_base = buf;
2212fe8fb19SBen Gras iov.iov_len = len;
222f14fb602SLionel Sambuc return __sfvwrite(fp, &uio) != EOF ? (wint_t)wc : END_OF_FILE;
2232fe8fb19SBen Gras }
2242fe8fb19SBen Gras #else
2252fe8fb19SBen Gras /*
2262fe8fb19SBen Gras * Flush out all the vectors defined by the given uio,
2272fe8fb19SBen Gras * then reset it so that it can be reused.
2282fe8fb19SBen Gras */
2292fe8fb19SBen Gras static int
__sprint(FILE * fp,struct __suio * uio)2302fe8fb19SBen Gras __sprint(FILE *fp, struct __suio *uio)
2312fe8fb19SBen Gras {
2322fe8fb19SBen Gras int err;
2332fe8fb19SBen Gras
2342fe8fb19SBen Gras _DIAGASSERT(fp != NULL);
2352fe8fb19SBen Gras _DIAGASSERT(uio != NULL);
2362fe8fb19SBen Gras
2372fe8fb19SBen Gras if (uio->uio_resid == 0) {
2382fe8fb19SBen Gras uio->uio_iovcnt = 0;
239f14fb602SLionel Sambuc return 0;
2402fe8fb19SBen Gras }
2412fe8fb19SBen Gras err = __sfvwrite(fp, uio);
2422fe8fb19SBen Gras uio->uio_resid = 0;
2432fe8fb19SBen Gras uio->uio_iovcnt = 0;
244f14fb602SLionel Sambuc return err;
2452fe8fb19SBen Gras }
2462fe8fb19SBen Gras #endif
2472fe8fb19SBen Gras
2482fe8fb19SBen Gras /*
2492fe8fb19SBen Gras * Macros for converting digits to letters and vice versa
2502fe8fb19SBen Gras */
2512fe8fb19SBen Gras #define to_digit(c) ((c) - '0')
2522fe8fb19SBen Gras #define is_digit(c) ((unsigned)to_digit(c) <= 9)
2532fe8fb19SBen Gras #define to_char(n) (CHAR_T)((n) + '0')
2542fe8fb19SBen Gras
2552fe8fb19SBen Gras /*
2562fe8fb19SBen Gras * Convert an unsigned long to ASCII for printf purposes, returning
2572fe8fb19SBen Gras * a pointer to the first character of the string representation.
2582fe8fb19SBen Gras * Octal numbers can be forced to have a leading zero; hex numbers
2592fe8fb19SBen Gras * use the given digits.
2602fe8fb19SBen Gras */
2612fe8fb19SBen Gras static CHAR_T *
__ultoa(u_long val,CHAR_T * endp,int base,int octzero,const char * xdigs,int needgrp,char thousep,const char * grp)2622fe8fb19SBen Gras __ultoa(u_long val, CHAR_T *endp, int base, int octzero, const char *xdigs,
2632fe8fb19SBen Gras int needgrp, char thousep, const char *grp)
2642fe8fb19SBen Gras {
2652fe8fb19SBen Gras CHAR_T *cp = endp;
2662fe8fb19SBen Gras long sval;
2672fe8fb19SBen Gras int ndig;
2682fe8fb19SBen Gras
2692fe8fb19SBen Gras /*
2702fe8fb19SBen Gras * Handle the three cases separately, in the hope of getting
2712fe8fb19SBen Gras * better/faster code.
2722fe8fb19SBen Gras */
2732fe8fb19SBen Gras switch (base) {
2742fe8fb19SBen Gras case 10:
2752fe8fb19SBen Gras if (val < 10) { /* many numbers are 1 digit */
2762fe8fb19SBen Gras *--cp = to_char(val);
277f14fb602SLionel Sambuc return cp;
2782fe8fb19SBen Gras }
2792fe8fb19SBen Gras ndig = 0;
2802fe8fb19SBen Gras /*
2812fe8fb19SBen Gras * On many machines, unsigned arithmetic is harder than
2822fe8fb19SBen Gras * signed arithmetic, so we do at most one unsigned mod and
2832fe8fb19SBen Gras * divide; this is sufficient to reduce the range of
2842fe8fb19SBen Gras * the incoming value to where signed arithmetic works.
2852fe8fb19SBen Gras */
2862fe8fb19SBen Gras if (val > LONG_MAX) {
2872fe8fb19SBen Gras *--cp = to_char(val % 10);
2882fe8fb19SBen Gras ndig++;
2892fe8fb19SBen Gras sval = val / 10;
2902fe8fb19SBen Gras } else
2912fe8fb19SBen Gras sval = val;
2922fe8fb19SBen Gras do {
2932fe8fb19SBen Gras *--cp = to_char(sval % 10);
2942fe8fb19SBen Gras ndig++;
2952fe8fb19SBen Gras /*
2962fe8fb19SBen Gras * If (*grp == CHAR_MAX) then no more grouping
2972fe8fb19SBen Gras * should be performed.
2982fe8fb19SBen Gras */
299f14fb602SLionel Sambuc if (needgrp && ndig == *grp
300f14fb602SLionel Sambuc && (unsigned char)*grp != (unsigned char)CHAR_MAX
3012fe8fb19SBen Gras && sval > 9) {
3022fe8fb19SBen Gras *--cp = thousep;
3032fe8fb19SBen Gras ndig = 0;
3042fe8fb19SBen Gras /*
3052fe8fb19SBen Gras * If (*(grp+1) == '\0') then we have to
3062fe8fb19SBen Gras * use *grp character (last grouping rule)
3072fe8fb19SBen Gras * for all next cases
3082fe8fb19SBen Gras */
3092fe8fb19SBen Gras if (*(grp+1) != '\0')
3102fe8fb19SBen Gras grp++;
3112fe8fb19SBen Gras }
3122fe8fb19SBen Gras sval /= 10;
3132fe8fb19SBen Gras } while (sval != 0);
3142fe8fb19SBen Gras break;
3152fe8fb19SBen Gras
3162fe8fb19SBen Gras case 8:
3172fe8fb19SBen Gras do {
3182fe8fb19SBen Gras *--cp = to_char(val & 7);
3192fe8fb19SBen Gras val >>= 3;
3202fe8fb19SBen Gras } while (val);
3212fe8fb19SBen Gras if (octzero && *cp != '0')
3222fe8fb19SBen Gras *--cp = '0';
3232fe8fb19SBen Gras break;
3242fe8fb19SBen Gras
3252fe8fb19SBen Gras case 16:
3262fe8fb19SBen Gras do {
3272fe8fb19SBen Gras *--cp = xdigs[(size_t)val & 15];
3282fe8fb19SBen Gras val >>= 4;
3292fe8fb19SBen Gras } while (val);
3302fe8fb19SBen Gras break;
3312fe8fb19SBen Gras
3322fe8fb19SBen Gras default: /* oops */
3332fe8fb19SBen Gras abort();
3342fe8fb19SBen Gras }
335f14fb602SLionel Sambuc return cp;
3362fe8fb19SBen Gras }
3372fe8fb19SBen Gras
3382fe8fb19SBen Gras /* Identical to __ultoa, but for intmax_t. */
3392fe8fb19SBen Gras static CHAR_T *
__ujtoa(uintmax_t val,CHAR_T * endp,int base,int octzero,const char * xdigs,int needgrp,char thousep,const char * grp)3402fe8fb19SBen Gras __ujtoa(uintmax_t val, CHAR_T *endp, int base, int octzero,
3412fe8fb19SBen Gras const char *xdigs, int needgrp, char thousep, const char *grp)
3422fe8fb19SBen Gras {
3432fe8fb19SBen Gras CHAR_T *cp = endp;
3442fe8fb19SBen Gras intmax_t sval;
3452fe8fb19SBen Gras int ndig;
3462fe8fb19SBen Gras
3472fe8fb19SBen Gras /* quick test for small values; __ultoa is typically much faster */
3482fe8fb19SBen Gras /* (perhaps instead we should run until small, then call __ultoa?) */
3492fe8fb19SBen Gras if (val <= ULONG_MAX)
350f14fb602SLionel Sambuc return __ultoa((u_long)val, endp, base, octzero, xdigs,
351f14fb602SLionel Sambuc needgrp, thousep, grp);
3522fe8fb19SBen Gras switch (base) {
3532fe8fb19SBen Gras case 10:
3542fe8fb19SBen Gras if (val < 10) {
3552fe8fb19SBen Gras *--cp = to_char(val % 10);
356f14fb602SLionel Sambuc return cp;
3572fe8fb19SBen Gras }
3582fe8fb19SBen Gras ndig = 0;
3592fe8fb19SBen Gras if (val > INTMAX_MAX) {
3602fe8fb19SBen Gras *--cp = to_char(val % 10);
3612fe8fb19SBen Gras ndig++;
3622fe8fb19SBen Gras sval = val / 10;
3632fe8fb19SBen Gras } else
3642fe8fb19SBen Gras sval = val;
3652fe8fb19SBen Gras do {
3662fe8fb19SBen Gras *--cp = to_char(sval % 10);
3672fe8fb19SBen Gras ndig++;
3682fe8fb19SBen Gras /*
3692fe8fb19SBen Gras * If (*grp == CHAR_MAX) then no more grouping
3702fe8fb19SBen Gras * should be performed.
3712fe8fb19SBen Gras */
372f14fb602SLionel Sambuc if (needgrp
373f14fb602SLionel Sambuc && (unsigned char)*grp != (unsigned char)CHAR_MAX
374f14fb602SLionel Sambuc && ndig == *grp
3752fe8fb19SBen Gras && sval > 9) {
3762fe8fb19SBen Gras *--cp = thousep;
3772fe8fb19SBen Gras ndig = 0;
3782fe8fb19SBen Gras /*
3792fe8fb19SBen Gras * If (*(grp+1) == '\0') then we have to
3802fe8fb19SBen Gras * use *grp character (last grouping rule)
3812fe8fb19SBen Gras * for all next cases
3822fe8fb19SBen Gras */
3832fe8fb19SBen Gras if (*(grp+1) != '\0')
3842fe8fb19SBen Gras grp++;
3852fe8fb19SBen Gras }
3862fe8fb19SBen Gras sval /= 10;
3872fe8fb19SBen Gras } while (sval != 0);
3882fe8fb19SBen Gras break;
3892fe8fb19SBen Gras
3902fe8fb19SBen Gras case 8:
3912fe8fb19SBen Gras do {
3922fe8fb19SBen Gras *--cp = to_char(val & 7);
3932fe8fb19SBen Gras val >>= 3;
3942fe8fb19SBen Gras } while (val);
3952fe8fb19SBen Gras if (octzero && *cp != '0')
3962fe8fb19SBen Gras *--cp = '0';
3972fe8fb19SBen Gras break;
3982fe8fb19SBen Gras
3992fe8fb19SBen Gras case 16:
4002fe8fb19SBen Gras do {
4012fe8fb19SBen Gras *--cp = xdigs[(size_t)val & 15];
4022fe8fb19SBen Gras val >>= 4;
4032fe8fb19SBen Gras } while (val);
4042fe8fb19SBen Gras break;
4052fe8fb19SBen Gras
4062fe8fb19SBen Gras default:
4072fe8fb19SBen Gras abort();
4082fe8fb19SBen Gras }
409f14fb602SLionel Sambuc return cp;
4102fe8fb19SBen Gras }
4112fe8fb19SBen Gras
4122fe8fb19SBen Gras #ifndef NARROW
4132fe8fb19SBen Gras /*
4142fe8fb19SBen Gras * Convert a multibyte character string argument for the %s format to a wide
4152fe8fb19SBen Gras * string representation. ``prec'' specifies the maximum number of bytes
4162fe8fb19SBen Gras * to output. If ``prec'' is greater than or equal to zero, we can't assume
4172fe8fb19SBen Gras * that the multibyte char. string ends in a null character.
4182fe8fb19SBen Gras */
4192fe8fb19SBen Gras static wchar_t *
__mbsconv(char * mbsarg,int prec,locale_t loc)42084d9c625SLionel Sambuc __mbsconv(char *mbsarg, int prec, locale_t loc)
4212fe8fb19SBen Gras {
4222fe8fb19SBen Gras static const mbstate_t initial;
4232fe8fb19SBen Gras mbstate_t mbs;
4242fe8fb19SBen Gras wchar_t *convbuf, *wcp;
4252fe8fb19SBen Gras const char *p;
4262fe8fb19SBen Gras size_t insize, nchars, nconv;
4272fe8fb19SBen Gras
4282fe8fb19SBen Gras if (mbsarg == NULL)
429f14fb602SLionel Sambuc return NULL;
4302fe8fb19SBen Gras
4312fe8fb19SBen Gras /*
4322fe8fb19SBen Gras * Supplied argument is a multibyte string; convert it to wide
4332fe8fb19SBen Gras * characters first.
4342fe8fb19SBen Gras */
4352fe8fb19SBen Gras if (prec >= 0) {
4362fe8fb19SBen Gras /*
4372fe8fb19SBen Gras * String is not guaranteed to be NUL-terminated. Find the
4382fe8fb19SBen Gras * number of characters to print.
4392fe8fb19SBen Gras */
4402fe8fb19SBen Gras p = mbsarg;
4412fe8fb19SBen Gras insize = nchars = nconv = 0;
4422fe8fb19SBen Gras mbs = initial;
4432fe8fb19SBen Gras while (nchars != (size_t)prec) {
44484d9c625SLionel Sambuc nconv = mbrlen_l(p, MB_CUR_MAX_L(loc), &mbs, loc);
4452fe8fb19SBen Gras if (nconv == 0 || nconv == (size_t)-1 ||
4462fe8fb19SBen Gras nconv == (size_t)-2)
4472fe8fb19SBen Gras break;
4482fe8fb19SBen Gras p += nconv;
4492fe8fb19SBen Gras nchars++;
4502fe8fb19SBen Gras insize += nconv;
4512fe8fb19SBen Gras }
4522fe8fb19SBen Gras if (nconv == (size_t)-1 || nconv == (size_t)-2)
453f14fb602SLionel Sambuc return NULL;
4542fe8fb19SBen Gras } else
4552fe8fb19SBen Gras insize = strlen(mbsarg);
4562fe8fb19SBen Gras
4572fe8fb19SBen Gras /*
4582fe8fb19SBen Gras * Allocate buffer for the result and perform the conversion,
4592fe8fb19SBen Gras * converting at most `size' bytes of the input multibyte string to
4602fe8fb19SBen Gras * wide characters for printing.
4612fe8fb19SBen Gras */
4622fe8fb19SBen Gras convbuf = malloc((insize + 1) * sizeof(*convbuf));
4632fe8fb19SBen Gras if (convbuf == NULL)
464f14fb602SLionel Sambuc return NULL;
4652fe8fb19SBen Gras wcp = convbuf;
4662fe8fb19SBen Gras p = mbsarg;
4672fe8fb19SBen Gras mbs = initial;
4682fe8fb19SBen Gras nconv = 0;
4692fe8fb19SBen Gras while (insize != 0) {
47084d9c625SLionel Sambuc nconv = mbrtowc_l(wcp, p, insize, &mbs, loc);
4712fe8fb19SBen Gras if (nconv == 0 || nconv == (size_t)-1 || nconv == (size_t)-2)
4722fe8fb19SBen Gras break;
4732fe8fb19SBen Gras wcp++;
4742fe8fb19SBen Gras p += nconv;
4752fe8fb19SBen Gras insize -= nconv;
4762fe8fb19SBen Gras }
4772fe8fb19SBen Gras if (nconv == (size_t)-1 || nconv == (size_t)-2) {
4782fe8fb19SBen Gras free(convbuf);
479f14fb602SLionel Sambuc return NULL;
4802fe8fb19SBen Gras }
4812fe8fb19SBen Gras *wcp = L'\0';
4822fe8fb19SBen Gras
483f14fb602SLionel Sambuc return convbuf;
4842fe8fb19SBen Gras }
4852fe8fb19SBen Gras #else
4862fe8fb19SBen Gras /*
4872fe8fb19SBen Gras * Convert a wide-character string argument for the %ls format to a multibyte
4882fe8fb19SBen Gras * string representation. If not -1, prec specifies the maximum number of
4892fe8fb19SBen Gras * bytes to output, and also means that we can't assume that the wide-char.
4902fe8fb19SBen Gras * string ends is null-terminated.
4912fe8fb19SBen Gras */
4922fe8fb19SBen Gras static char *
__wcsconv(wchar_t * wcsarg,int prec,locale_t loc)49384d9c625SLionel Sambuc __wcsconv(wchar_t *wcsarg, int prec, locale_t loc)
4942fe8fb19SBen Gras {
4952fe8fb19SBen Gras static const mbstate_t initial;
4962fe8fb19SBen Gras mbstate_t mbs;
4972fe8fb19SBen Gras char buf[MB_LEN_MAX];
4982fe8fb19SBen Gras wchar_t *p;
4992fe8fb19SBen Gras char *convbuf;
5002fe8fb19SBen Gras size_t clen, nbytes;
5012fe8fb19SBen Gras
5022fe8fb19SBen Gras /* Allocate space for the maximum number of bytes we could output. */
5032fe8fb19SBen Gras if (prec < 0) {
5042fe8fb19SBen Gras p = wcsarg;
5052fe8fb19SBen Gras mbs = initial;
50684d9c625SLionel Sambuc nbytes = wcsrtombs_l(NULL, (void *)&p, 0, &mbs, loc);
5072fe8fb19SBen Gras if (nbytes == (size_t)-1)
508f14fb602SLionel Sambuc return NULL;
5092fe8fb19SBen Gras } else {
5102fe8fb19SBen Gras /*
5112fe8fb19SBen Gras * Optimisation: if the output precision is small enough,
5122fe8fb19SBen Gras * just allocate enough memory for the maximum instead of
5132fe8fb19SBen Gras * scanning the string.
5142fe8fb19SBen Gras */
5152fe8fb19SBen Gras if (prec < 128)
5162fe8fb19SBen Gras nbytes = prec;
5172fe8fb19SBen Gras else {
5182fe8fb19SBen Gras nbytes = 0;
5192fe8fb19SBen Gras p = wcsarg;
5202fe8fb19SBen Gras mbs = initial;
5212fe8fb19SBen Gras for (;;) {
52284d9c625SLionel Sambuc clen = wcrtomb_l(buf, *p++, &mbs, loc);
5232fe8fb19SBen Gras if (clen == 0 || clen == (size_t)-1 ||
5242fe8fb19SBen Gras nbytes + clen > (size_t)prec)
5252fe8fb19SBen Gras break;
5262fe8fb19SBen Gras nbytes += clen;
5272fe8fb19SBen Gras }
5282fe8fb19SBen Gras }
5292fe8fb19SBen Gras }
5302fe8fb19SBen Gras if ((convbuf = malloc(nbytes + 1)) == NULL)
531f14fb602SLionel Sambuc return NULL;
5322fe8fb19SBen Gras
5332fe8fb19SBen Gras /* Fill the output buffer. */
5342fe8fb19SBen Gras p = wcsarg;
5352fe8fb19SBen Gras mbs = initial;
53684d9c625SLionel Sambuc if ((nbytes = wcsrtombs_l(convbuf, (void *)&p,
53784d9c625SLionel Sambuc nbytes, &mbs, loc)) == (size_t)-1) {
5382fe8fb19SBen Gras free(convbuf);
539f14fb602SLionel Sambuc return NULL;
5402fe8fb19SBen Gras }
5412fe8fb19SBen Gras convbuf[nbytes] = '\0';
542f14fb602SLionel Sambuc return convbuf;
5432fe8fb19SBen Gras }
5442fe8fb19SBen Gras #endif
5452fe8fb19SBen Gras
5462fe8fb19SBen Gras /*
5472fe8fb19SBen Gras * MT-safe version
5482fe8fb19SBen Gras */
5492fe8fb19SBen Gras int
WDECL(vf,printf)5502fe8fb19SBen Gras WDECL(vf,printf)(FILE * __restrict fp, const CHAR_T * __restrict fmt0, va_list ap)
5512fe8fb19SBen Gras {
5522fe8fb19SBen Gras int ret;
5532fe8fb19SBen Gras
5542fe8fb19SBen Gras FLOCKFILE(fp);
55584d9c625SLionel Sambuc ret = WDECL(__vf,printf_unlocked_l)(fp, _current_locale(), fmt0, ap);
55684d9c625SLionel Sambuc FUNLOCKFILE(fp);
55784d9c625SLionel Sambuc return ret;
55884d9c625SLionel Sambuc }
55984d9c625SLionel Sambuc
56084d9c625SLionel Sambuc int
WDECL(vf,printf_l)56184d9c625SLionel Sambuc WDECL(vf,printf_l)(FILE * __restrict fp, locale_t loc, const CHAR_T * __restrict fmt0,
56284d9c625SLionel Sambuc va_list ap)
56384d9c625SLionel Sambuc {
56484d9c625SLionel Sambuc int ret;
56584d9c625SLionel Sambuc
56684d9c625SLionel Sambuc FLOCKFILE(fp);
56784d9c625SLionel Sambuc ret = WDECL(__vf,printf_unlocked_l)(fp, loc, fmt0, ap);
5682fe8fb19SBen Gras FUNLOCKFILE(fp);
569f14fb602SLionel Sambuc return ret;
5702fe8fb19SBen Gras }
5712fe8fb19SBen Gras
5722fe8fb19SBen Gras #ifndef NO_FLOATING_POINT
5732fe8fb19SBen Gras
5742fe8fb19SBen Gras #include <float.h>
5752fe8fb19SBen Gras #include <math.h>
5762fe8fb19SBen Gras #include "floatio.h"
5772fe8fb19SBen Gras
5782fe8fb19SBen Gras #define DEFPREC 6
5792fe8fb19SBen Gras
5802fe8fb19SBen Gras static int exponent(CHAR_T *, int, int);
5812fe8fb19SBen Gras #ifndef WIDE_DOUBLE
5822fe8fb19SBen Gras static char *cvt(double, int, int, char *, int *, int, int *);
5832fe8fb19SBen Gras #endif
5842fe8fb19SBen Gras
5852fe8fb19SBen Gras #endif /* !NO_FLOATING_POINT */
5862fe8fb19SBen Gras
5872fe8fb19SBen Gras /*
5882fe8fb19SBen Gras * The size of the buffer we use as scratch space for integer
5892fe8fb19SBen Gras * conversions, among other things. Technically, we would need the
5902fe8fb19SBen Gras * most space for base 10 conversions with thousands' grouping
5912fe8fb19SBen Gras * characters between each pair of digits. 100 bytes is a
5922fe8fb19SBen Gras * conservative overestimate even for a 128-bit uintmax_t.
5932fe8fb19SBen Gras */
5942fe8fb19SBen Gras #define BUF 100
5952fe8fb19SBen Gras
5962fe8fb19SBen Gras #define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */
5972fe8fb19SBen Gras
5982fe8fb19SBen Gras /*
5992fe8fb19SBen Gras * Flags used during conversion.
6002fe8fb19SBen Gras */
6012fe8fb19SBen Gras #define ALT 0x001 /* alternate form */
6022fe8fb19SBen Gras #define LADJUST 0x004 /* left adjustment */
6032fe8fb19SBen Gras #define LONGDBL 0x008 /* long double */
6042fe8fb19SBen Gras #define LONGINT 0x010 /* long integer */
6052fe8fb19SBen Gras #define LLONGINT 0x020 /* long long integer */
6062fe8fb19SBen Gras #define SHORTINT 0x040 /* short integer */
6072fe8fb19SBen Gras #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
6082fe8fb19SBen Gras #define FPT 0x100 /* Floating point number */
6092fe8fb19SBen Gras #define GROUPING 0x200 /* use grouping ("'" flag) */
6102fe8fb19SBen Gras /* C99 additional size modifiers: */
6112fe8fb19SBen Gras #define SIZET 0x400 /* size_t */
6122fe8fb19SBen Gras #define PTRDIFFT 0x800 /* ptrdiff_t */
6132fe8fb19SBen Gras #define INTMAXT 0x1000 /* intmax_t */
6142fe8fb19SBen Gras #define CHARINT 0x2000 /* print char using int format */
6152fe8fb19SBen Gras
6162fe8fb19SBen Gras /*
6172fe8fb19SBen Gras * Non-MT-safe version
6182fe8fb19SBen Gras */
6192fe8fb19SBen Gras int
WDECL(__vf,printf_unlocked_l)62084d9c625SLionel Sambuc WDECL(__vf,printf_unlocked_l)(FILE *fp, locale_t loc, const CHAR_T *fmt0, va_list ap)
6212fe8fb19SBen Gras {
6222fe8fb19SBen Gras CHAR_T *fmt; /* format string */
6232fe8fb19SBen Gras int ch; /* character from fmt */
6242fe8fb19SBen Gras int n, n2; /* handy integer (short term usage) */
6252fe8fb19SBen Gras CHAR_T *cp; /* handy char pointer (short term usage) */
6262fe8fb19SBen Gras int flags; /* flags as above */
6272fe8fb19SBen Gras int ret; /* return value accumulator */
6282fe8fb19SBen Gras int width; /* width from format (%8d), or 0 */
6292fe8fb19SBen Gras int prec; /* precision from format; <0 for N/A */
6302fe8fb19SBen Gras CHAR_T sign; /* sign prefix (' ', '+', '-', or \0) */
6312fe8fb19SBen Gras char thousands_sep; /* locale specific thousands separator */
6322fe8fb19SBen Gras const char *grouping; /* locale specific numeric grouping rules */
6332fe8fb19SBen Gras #ifndef NO_FLOATING_POINT
6342fe8fb19SBen Gras /*
6352fe8fb19SBen Gras * We can decompose the printed representation of floating
6362fe8fb19SBen Gras * point numbers into several parts, some of which may be empty:
6372fe8fb19SBen Gras *
6382fe8fb19SBen Gras * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
6392fe8fb19SBen Gras * A B ---C--- D E F
6402fe8fb19SBen Gras *
6412fe8fb19SBen Gras * A: 'sign' holds this value if present; '\0' otherwise
6422fe8fb19SBen Gras * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
6432fe8fb19SBen Gras * C: cp points to the string MMMNNN. Leading and trailing
6442fe8fb19SBen Gras * zeros are not in the string and must be added.
6452fe8fb19SBen Gras * D: expchar holds this character; '\0' if no exponent, e.g. %f
6462fe8fb19SBen Gras * F: at least two digits for decimal, at least one digit for hex
6472fe8fb19SBen Gras */
6482fe8fb19SBen Gras char *decimal_point; /* locale specific decimal point */
6492fe8fb19SBen Gras #ifdef WIDE_DOUBLE
6502fe8fb19SBen Gras int signflag; /* true if float is negative */
6512fe8fb19SBen Gras union { /* floating point arguments %[aAeEfFgG] */
6522fe8fb19SBen Gras double dbl;
6532fe8fb19SBen Gras long double ldbl;
6542fe8fb19SBen Gras } fparg;
6552fe8fb19SBen Gras char *dtoaend; /* pointer to end of converted digits */
6562fe8fb19SBen Gras #else
6572fe8fb19SBen Gras double _double; /* double precision arguments %[eEfgG] */
6582fe8fb19SBen Gras char softsign; /* temporary negative sign for floats */
6592fe8fb19SBen Gras #endif
6602fe8fb19SBen Gras char *dtoaresult; /* buffer allocated by dtoa */
6612fe8fb19SBen Gras int expt; /* integer value of exponent */
6622fe8fb19SBen Gras char expchar; /* exponent character: [eEpP\0] */
6632fe8fb19SBen Gras int expsize; /* character count for expstr */
6642fe8fb19SBen Gras int lead; /* sig figs before decimal or group sep */
6652fe8fb19SBen Gras int ndig; /* actual number of digits returned by dtoa */
6662fe8fb19SBen Gras CHAR_T expstr[MAXEXPDIG+2]; /* buffer for exponent string: e+ZZZ */
6672fe8fb19SBen Gras int nseps; /* number of group separators with ' */
6682fe8fb19SBen Gras int nrepeats; /* number of repeats of the last group */
6692fe8fb19SBen Gras #endif
6702fe8fb19SBen Gras u_long ulval; /* integer arguments %[diouxX] */
6712fe8fb19SBen Gras uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */
6722fe8fb19SBen Gras int base; /* base for [diouxX] conversion */
6732fe8fb19SBen Gras int dprec; /* a copy of prec if [diouxX], 0 otherwise */
6742fe8fb19SBen Gras int realsz; /* field size expanded by dprec, sign, etc */
6752fe8fb19SBen Gras int size; /* size of converted field or string */
6762fe8fb19SBen Gras int prsize; /* max size of printed field */
6772fe8fb19SBen Gras const char *xdigs; /* digits for %[xX] conversion */
6782fe8fb19SBen Gras #ifdef NARROW
6792fe8fb19SBen Gras #define NIOV 8
6802fe8fb19SBen Gras struct __siov *iovp; /* for PRINT macro */
6812fe8fb19SBen Gras struct __suio uio; /* output information: summary */
6822fe8fb19SBen Gras struct __siov iov[NIOV];/* ... and individual io vectors */
6832fe8fb19SBen Gras #else
6842fe8fb19SBen Gras int n3;
6852fe8fb19SBen Gras #endif
6862fe8fb19SBen Gras CHAR_T buf[BUF]; /* buffer with space for digits of uintmax_t */
6872fe8fb19SBen Gras CHAR_T ox[2]; /* space for 0x hex-prefix */
6882fe8fb19SBen Gras union arg *argtable; /* args, built due to positional arg */
6892fe8fb19SBen Gras union arg statargtable [STATIC_ARG_TBL_SIZE];
6902fe8fb19SBen Gras int nextarg; /* 1-based argument index */
6912fe8fb19SBen Gras va_list orgap; /* original argument pointer */
6922fe8fb19SBen Gras CHAR_T *convbuf; /* multibyte to wide conversion result */
6932fe8fb19SBen Gras
6942fe8fb19SBen Gras /*
6952fe8fb19SBen Gras * Choose PADSIZE to trade efficiency vs. size. If larger printf
6962fe8fb19SBen Gras * fields occur frequently, increase PADSIZE and make the initialisers
6972fe8fb19SBen Gras * below longer.
6982fe8fb19SBen Gras */
6992fe8fb19SBen Gras #define PADSIZE 16 /* pad chunk size */
7002fe8fb19SBen Gras static CHAR_T blanks[PADSIZE] =
7012fe8fb19SBen Gras {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
7022fe8fb19SBen Gras static CHAR_T zeroes[PADSIZE] =
7032fe8fb19SBen Gras {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
7042fe8fb19SBen Gras
7052fe8fb19SBen Gras static const char xdigs_lower[16] = "0123456789abcdef";
7062fe8fb19SBen Gras static const char xdigs_upper[16] = "0123456789ABCDEF";
7072fe8fb19SBen Gras
7082fe8fb19SBen Gras /*
7092fe8fb19SBen Gras * BEWARE, these `goto error' on error, PRINT uses `n2' and
7102fe8fb19SBen Gras * PAD uses `n'.
7112fe8fb19SBen Gras */
7122fe8fb19SBen Gras #ifndef NARROW
7132fe8fb19SBen Gras #define PRINT(ptr, len) do { \
7142fe8fb19SBen Gras for (n3 = 0; n3 < (len); n3++) \
715*0a6a1f1dSLionel Sambuc if (__xfputwc((ptr)[n3], fp, loc) == END_OF_FILE) { \
716*0a6a1f1dSLionel Sambuc fp->_flags |= __SERR; \
717*0a6a1f1dSLionel Sambuc goto error; \
718*0a6a1f1dSLionel Sambuc } \
7192fe8fb19SBen Gras } while (/*CONSTCOND*/0)
7202fe8fb19SBen Gras #define FLUSH()
7212fe8fb19SBen Gras #else
7222fe8fb19SBen Gras #define PRINT(ptr, len) do { \
7232fe8fb19SBen Gras iovp->iov_base = __UNCONST(ptr); \
7242fe8fb19SBen Gras iovp->iov_len = (len); \
7252fe8fb19SBen Gras uio.uio_resid += (len); \
7262fe8fb19SBen Gras iovp++; \
7272fe8fb19SBen Gras if (++uio.uio_iovcnt >= NIOV) { \
7282fe8fb19SBen Gras if (__sprint(fp, &uio)) \
7292fe8fb19SBen Gras goto error; \
7302fe8fb19SBen Gras iovp = iov; \
7312fe8fb19SBen Gras } \
7322fe8fb19SBen Gras } while (/*CONSTCOND*/0)
7332fe8fb19SBen Gras #define FLUSH() do { \
7342fe8fb19SBen Gras if (uio.uio_resid && __sprint(fp, &uio)) \
7352fe8fb19SBen Gras goto error; \
7362fe8fb19SBen Gras uio.uio_iovcnt = 0; \
7372fe8fb19SBen Gras iovp = iov; \
7382fe8fb19SBen Gras } while (/*CONSTCOND*/0)
7392fe8fb19SBen Gras #endif /* NARROW */
7402fe8fb19SBen Gras
7412fe8fb19SBen Gras #define PAD(howmany, with) do { \
7422fe8fb19SBen Gras if ((n = (howmany)) > 0) { \
7432fe8fb19SBen Gras while (n > PADSIZE) { \
7442fe8fb19SBen Gras PRINT(with, PADSIZE); \
7452fe8fb19SBen Gras n -= PADSIZE; \
7462fe8fb19SBen Gras } \
7472fe8fb19SBen Gras PRINT(with, n); \
7482fe8fb19SBen Gras } \
7492fe8fb19SBen Gras } while (/*CONSTCOND*/0)
7502fe8fb19SBen Gras #define PRINTANDPAD(p, ep, len, with) do { \
751f14fb602SLionel Sambuc ptrdiff_t td = (ep) - (p); \
752f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(int, td)); \
753f14fb602SLionel Sambuc n2 = (int)td; \
7542fe8fb19SBen Gras if (n2 > (len)) \
7552fe8fb19SBen Gras n2 = (len); \
7562fe8fb19SBen Gras if (n2 > 0) \
7572fe8fb19SBen Gras PRINT((p), n2); \
7582fe8fb19SBen Gras PAD((len) - (n2 > 0 ? n2 : 0), (with)); \
7592fe8fb19SBen Gras } while(/*CONSTCOND*/0)
7602fe8fb19SBen Gras
7612fe8fb19SBen Gras /*
7622fe8fb19SBen Gras * Get the argument indexed by nextarg. If the argument table is
7632fe8fb19SBen Gras * built, use it to get the argument. If its not, get the next
7642fe8fb19SBen Gras * argument (and arguments must be gotten sequentially).
7652fe8fb19SBen Gras */
7662fe8fb19SBen Gras #define GETARG(type) \
7672fe8fb19SBen Gras ((/*CONSTCOND*/argtable != NULL) ? *((type*)(void*)(&argtable[nextarg++])) : \
7682fe8fb19SBen Gras (nextarg++, va_arg(ap, type)))
7692fe8fb19SBen Gras
7702fe8fb19SBen Gras /*
7712fe8fb19SBen Gras * To extend shorts properly, we need both signed and unsigned
7722fe8fb19SBen Gras * argument extraction methods.
7732fe8fb19SBen Gras */
7742fe8fb19SBen Gras #define SARG() \
7752fe8fb19SBen Gras (flags&LONGINT ? GETARG(long) : \
7762fe8fb19SBen Gras flags&SHORTINT ? (long)(short)GETARG(int) : \
7772fe8fb19SBen Gras flags&CHARINT ? (long)(signed char)GETARG(int) : \
7782fe8fb19SBen Gras (long)GETARG(int))
7792fe8fb19SBen Gras #define UARG() \
7802fe8fb19SBen Gras (flags&LONGINT ? GETARG(u_long) : \
7812fe8fb19SBen Gras flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
7822fe8fb19SBen Gras flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
7832fe8fb19SBen Gras (u_long)GETARG(u_int))
7842fe8fb19SBen Gras #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
7852fe8fb19SBen Gras #define SJARG() \
7862fe8fb19SBen Gras (flags&INTMAXT ? GETARG(intmax_t) : \
7872fe8fb19SBen Gras flags&SIZET ? (intmax_t)GETARG(ssize_t) : \
7882fe8fb19SBen Gras flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
7892fe8fb19SBen Gras (intmax_t)GETARG(long long))
7902fe8fb19SBen Gras #define UJARG() \
7912fe8fb19SBen Gras (flags&INTMAXT ? GETARG(uintmax_t) : \
7922fe8fb19SBen Gras flags&SIZET ? (uintmax_t)GETARG(size_t) : \
7932fe8fb19SBen Gras flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \
7942fe8fb19SBen Gras (uintmax_t)GETARG(unsigned long long))
7952fe8fb19SBen Gras
7962fe8fb19SBen Gras /*
7972fe8fb19SBen Gras * Get * arguments, including the form *nn$. Preserve the nextarg
7982fe8fb19SBen Gras * that the argument can be gotten once the type is determined.
7992fe8fb19SBen Gras */
8002fe8fb19SBen Gras #define GETASTER(val) \
8012fe8fb19SBen Gras n2 = 0; \
8022fe8fb19SBen Gras cp = fmt; \
8032fe8fb19SBen Gras while (is_digit(*cp)) { \
8042fe8fb19SBen Gras n2 = 10 * n2 + to_digit(*cp); \
8052fe8fb19SBen Gras cp++; \
8062fe8fb19SBen Gras } \
8072fe8fb19SBen Gras if (*cp == '$') { \
8082fe8fb19SBen Gras int hold = nextarg; \
8092fe8fb19SBen Gras if (argtable == NULL) { \
8102fe8fb19SBen Gras argtable = statargtable; \
8112fe8fb19SBen Gras if (__find_arguments(fmt0, orgap, &argtable) == -1) \
8122fe8fb19SBen Gras goto oomem; \
8132fe8fb19SBen Gras } \
8142fe8fb19SBen Gras nextarg = n2; \
8152fe8fb19SBen Gras val = GETARG (int); \
8162fe8fb19SBen Gras nextarg = hold; \
8172fe8fb19SBen Gras fmt = ++cp; \
8182fe8fb19SBen Gras } else { \
8192fe8fb19SBen Gras val = GETARG (int); \
8202fe8fb19SBen Gras }
8212fe8fb19SBen Gras
8222fe8fb19SBen Gras _DIAGASSERT(fp != NULL);
8232fe8fb19SBen Gras _DIAGASSERT(fmt0 != NULL);
8242fe8fb19SBen Gras
8252fe8fb19SBen Gras _SET_ORIENTATION(fp, -1);
8262fe8fb19SBen Gras
8272fe8fb19SBen Gras thousands_sep = '\0';
8282fe8fb19SBen Gras grouping = NULL;
8292fe8fb19SBen Gras #ifndef NO_FLOATING_POINT
83084d9c625SLionel Sambuc decimal_point = localeconv_l(loc)->decimal_point;
8312fe8fb19SBen Gras expsize = 0; /* XXXGCC -Wuninitialized [sh3,m68000] */
83284d9c625SLionel Sambuc ndig = -1; /* XXX gcc */
8332fe8fb19SBen Gras #endif
8342fe8fb19SBen Gras convbuf = NULL;
8352fe8fb19SBen Gras /* sorry, f{w,}printf(read_only_file, L"") returns {W,}EOF, not 0 */
8362fe8fb19SBen Gras if (cantwrite(fp)) {
8372fe8fb19SBen Gras errno = EBADF;
838f14fb602SLionel Sambuc return END_OF_FILE;
8392fe8fb19SBen Gras }
8402fe8fb19SBen Gras
8412fe8fb19SBen Gras /* optimise fprintf(stderr) (and other unbuffered Unix files) */
8422fe8fb19SBen Gras if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
8432fe8fb19SBen Gras __sfileno(fp) != -1)
84484d9c625SLionel Sambuc return __sbprintf(fp, loc, fmt0, ap);
8452fe8fb19SBen Gras
8462fe8fb19SBen Gras fmt = (CHAR_T *)__UNCONST(fmt0);
8472fe8fb19SBen Gras argtable = NULL;
8482fe8fb19SBen Gras nextarg = 1;
8492fe8fb19SBen Gras va_copy(orgap, ap);
8502fe8fb19SBen Gras #ifdef NARROW
8512fe8fb19SBen Gras uio.uio_iov = iovp = iov;
8522fe8fb19SBen Gras uio.uio_resid = 0;
8532fe8fb19SBen Gras uio.uio_iovcnt = 0;
8542fe8fb19SBen Gras #endif
8552fe8fb19SBen Gras ret = 0;
8562fe8fb19SBen Gras
8572fe8fb19SBen Gras /*
8582fe8fb19SBen Gras * Scan the format for conversions (`%' character).
8592fe8fb19SBen Gras */
8602fe8fb19SBen Gras for (;;) {
8612fe8fb19SBen Gras const CHAR_T *result;
8622fe8fb19SBen Gras
8632fe8fb19SBen Gras for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
8642fe8fb19SBen Gras continue;
865f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(int, fmt - cp));
866f14fb602SLionel Sambuc if ((n = (int)(fmt - cp)) != 0) {
8672fe8fb19SBen Gras if ((unsigned)ret + n > INT_MAX) {
8682fe8fb19SBen Gras ret = END_OF_FILE;
8692fe8fb19SBen Gras goto error;
8702fe8fb19SBen Gras }
8712fe8fb19SBen Gras PRINT(cp, n);
8722fe8fb19SBen Gras ret += n;
8732fe8fb19SBen Gras }
8742fe8fb19SBen Gras if (ch == '\0')
8752fe8fb19SBen Gras goto done;
8762fe8fb19SBen Gras fmt++; /* skip over '%' */
8772fe8fb19SBen Gras
8782fe8fb19SBen Gras flags = 0;
8792fe8fb19SBen Gras dprec = 0;
8802fe8fb19SBen Gras width = 0;
8812fe8fb19SBen Gras prec = -1;
8822fe8fb19SBen Gras sign = '\0';
8832fe8fb19SBen Gras ox[1] = '\0';
88484d9c625SLionel Sambuc #ifndef NO_FLOATING_POINT
8852fe8fb19SBen Gras expchar = '\0';
8862fe8fb19SBen Gras lead = 0;
8872fe8fb19SBen Gras nseps = nrepeats = 0;
88884d9c625SLionel Sambuc #endif
8892fe8fb19SBen Gras ulval = 0;
8902fe8fb19SBen Gras ujval = 0;
8912fe8fb19SBen Gras xdigs = NULL;
8922fe8fb19SBen Gras
8932fe8fb19SBen Gras rflag: ch = *fmt++;
8942fe8fb19SBen Gras reswitch: switch (ch) {
8952fe8fb19SBen Gras case ' ':
8962fe8fb19SBen Gras /*-
8972fe8fb19SBen Gras * ``If the space and + flags both appear, the space
8982fe8fb19SBen Gras * flag will be ignored.''
8992fe8fb19SBen Gras * -- ANSI X3J11
9002fe8fb19SBen Gras */
9012fe8fb19SBen Gras if (!sign)
9022fe8fb19SBen Gras sign = ' ';
9032fe8fb19SBen Gras goto rflag;
9042fe8fb19SBen Gras case '#':
9052fe8fb19SBen Gras flags |= ALT;
9062fe8fb19SBen Gras goto rflag;
9072fe8fb19SBen Gras case '*':
9082fe8fb19SBen Gras /*-
9092fe8fb19SBen Gras * ``A negative field width argument is taken as a
9102fe8fb19SBen Gras * - flag followed by a positive field width.''
9112fe8fb19SBen Gras * -- ANSI X3J11
9122fe8fb19SBen Gras * They don't exclude field widths read from args.
9132fe8fb19SBen Gras */
9142fe8fb19SBen Gras GETASTER (width);
9152fe8fb19SBen Gras if (width >= 0)
9162fe8fb19SBen Gras goto rflag;
9172fe8fb19SBen Gras width = -width;
9182fe8fb19SBen Gras /* FALLTHROUGH */
9192fe8fb19SBen Gras case '-':
9202fe8fb19SBen Gras flags |= LADJUST;
9212fe8fb19SBen Gras goto rflag;
9222fe8fb19SBen Gras case '+':
9232fe8fb19SBen Gras sign = '+';
9242fe8fb19SBen Gras goto rflag;
9252fe8fb19SBen Gras case '\'':
9262fe8fb19SBen Gras flags |= GROUPING;
92784d9c625SLionel Sambuc thousands_sep = *(localeconv_l(loc)->thousands_sep);
92884d9c625SLionel Sambuc grouping = localeconv_l(loc)->grouping;
9292fe8fb19SBen Gras /* If the locale doesn't define the above, use sane
9302fe8fb19SBen Gras * defaults - otherwise silly things happen! */
9312fe8fb19SBen Gras if (thousands_sep == 0)
9322fe8fb19SBen Gras thousands_sep = ',';
9332fe8fb19SBen Gras if (!grouping || !*grouping)
9342fe8fb19SBen Gras grouping = "\3";
9352fe8fb19SBen Gras goto rflag;
9362fe8fb19SBen Gras case '.':
9372fe8fb19SBen Gras if ((ch = *fmt++) == '*') {
9382fe8fb19SBen Gras GETASTER (prec);
9392fe8fb19SBen Gras goto rflag;
9402fe8fb19SBen Gras }
9412fe8fb19SBen Gras prec = 0;
9422fe8fb19SBen Gras while (is_digit(ch)) {
9432fe8fb19SBen Gras prec = 10 * prec + to_digit(ch);
9442fe8fb19SBen Gras ch = *fmt++;
9452fe8fb19SBen Gras }
9462fe8fb19SBen Gras goto reswitch;
9472fe8fb19SBen Gras case '0':
9482fe8fb19SBen Gras /*-
9492fe8fb19SBen Gras * ``Note that 0 is taken as a flag, not as the
9502fe8fb19SBen Gras * beginning of a field width.''
9512fe8fb19SBen Gras * -- ANSI X3J11
9522fe8fb19SBen Gras */
9532fe8fb19SBen Gras flags |= ZEROPAD;
9542fe8fb19SBen Gras goto rflag;
9552fe8fb19SBen Gras case '1': case '2': case '3': case '4':
9562fe8fb19SBen Gras case '5': case '6': case '7': case '8': case '9':
9572fe8fb19SBen Gras n = 0;
9582fe8fb19SBen Gras do {
9592fe8fb19SBen Gras n = 10 * n + to_digit(ch);
9602fe8fb19SBen Gras ch = *fmt++;
9612fe8fb19SBen Gras } while (is_digit(ch));
9622fe8fb19SBen Gras if (ch == '$') {
9632fe8fb19SBen Gras nextarg = n;
9642fe8fb19SBen Gras if (argtable == NULL) {
9652fe8fb19SBen Gras argtable = statargtable;
9662fe8fb19SBen Gras if (__find_arguments(fmt0, orgap,
9672fe8fb19SBen Gras &argtable) == -1)
9682fe8fb19SBen Gras goto oomem;
9692fe8fb19SBen Gras }
9702fe8fb19SBen Gras goto rflag;
9712fe8fb19SBen Gras }
9722fe8fb19SBen Gras width = n;
9732fe8fb19SBen Gras goto reswitch;
9742fe8fb19SBen Gras #ifndef NO_FLOATING_POINT
9752fe8fb19SBen Gras case 'L':
9762fe8fb19SBen Gras flags |= LONGDBL;
9772fe8fb19SBen Gras goto rflag;
9782fe8fb19SBen Gras #endif
9792fe8fb19SBen Gras case 'h':
9802fe8fb19SBen Gras if (flags & SHORTINT) {
9812fe8fb19SBen Gras flags &= ~SHORTINT;
9822fe8fb19SBen Gras flags |= CHARINT;
9832fe8fb19SBen Gras } else
9842fe8fb19SBen Gras flags |= SHORTINT;
9852fe8fb19SBen Gras goto rflag;
9862fe8fb19SBen Gras case 'j':
9872fe8fb19SBen Gras flags |= INTMAXT;
9882fe8fb19SBen Gras goto rflag;
9892fe8fb19SBen Gras case 'l':
9902fe8fb19SBen Gras if (flags & LONGINT) {
9912fe8fb19SBen Gras flags &= ~LONGINT;
9922fe8fb19SBen Gras flags |= LLONGINT;
9932fe8fb19SBen Gras } else
9942fe8fb19SBen Gras flags |= LONGINT;
9952fe8fb19SBen Gras goto rflag;
9962fe8fb19SBen Gras case 'q':
9972fe8fb19SBen Gras flags |= LLONGINT; /* not necessarily */
9982fe8fb19SBen Gras goto rflag;
9992fe8fb19SBen Gras case 't':
10002fe8fb19SBen Gras flags |= PTRDIFFT;
10012fe8fb19SBen Gras goto rflag;
10022fe8fb19SBen Gras case 'z':
10032fe8fb19SBen Gras flags |= SIZET;
10042fe8fb19SBen Gras goto rflag;
10052fe8fb19SBen Gras case 'C':
10062fe8fb19SBen Gras flags |= LONGINT;
10072fe8fb19SBen Gras /*FALLTHROUGH*/
10082fe8fb19SBen Gras case 'c':
10092fe8fb19SBen Gras #ifdef NARROW
10102fe8fb19SBen Gras if (flags & LONGINT) {
10112fe8fb19SBen Gras static const mbstate_t initial;
10122fe8fb19SBen Gras mbstate_t mbs;
10132fe8fb19SBen Gras size_t mbseqlen;
10142fe8fb19SBen Gras
10152fe8fb19SBen Gras mbs = initial;
101684d9c625SLionel Sambuc mbseqlen = wcrtomb_l(buf,
101784d9c625SLionel Sambuc (wchar_t)GETARG(wint_t), &mbs, loc);
10182fe8fb19SBen Gras if (mbseqlen == (size_t)-1) {
10192fe8fb19SBen Gras fp->_flags |= __SERR;
10202fe8fb19SBen Gras goto error;
10212fe8fb19SBen Gras }
10222fe8fb19SBen Gras size = (int)mbseqlen;
10232fe8fb19SBen Gras } else {
10242fe8fb19SBen Gras *buf = GETARG(int);
10252fe8fb19SBen Gras size = 1;
10262fe8fb19SBen Gras }
10272fe8fb19SBen Gras #else
10282fe8fb19SBen Gras if (flags & LONGINT)
10292fe8fb19SBen Gras *buf = (wchar_t)GETARG(wint_t);
10302fe8fb19SBen Gras else
103184d9c625SLionel Sambuc *buf = (wchar_t)btowc_l(GETARG(int), loc);
10322fe8fb19SBen Gras size = 1;
10332fe8fb19SBen Gras #endif
10342fe8fb19SBen Gras result = buf;
10352fe8fb19SBen Gras sign = '\0';
10362fe8fb19SBen Gras break;
10372fe8fb19SBen Gras case 'D':
10382fe8fb19SBen Gras flags |= LONGINT;
10392fe8fb19SBen Gras /*FALLTHROUGH*/
10402fe8fb19SBen Gras case 'd':
10412fe8fb19SBen Gras case 'i':
10422fe8fb19SBen Gras if (flags & INTMAX_SIZE) {
10432fe8fb19SBen Gras ujval = SJARG();
10442fe8fb19SBen Gras if ((intmax_t)ujval < 0) {
10452fe8fb19SBen Gras ujval = -ujval;
10462fe8fb19SBen Gras sign = '-';
10472fe8fb19SBen Gras }
10482fe8fb19SBen Gras } else {
10492fe8fb19SBen Gras ulval = SARG();
10502fe8fb19SBen Gras if ((long)ulval < 0) {
10512fe8fb19SBen Gras ulval = -ulval;
10522fe8fb19SBen Gras sign = '-';
10532fe8fb19SBen Gras }
10542fe8fb19SBen Gras }
10552fe8fb19SBen Gras base = 10;
10562fe8fb19SBen Gras goto number;
10572fe8fb19SBen Gras #ifndef NO_FLOATING_POINT
10582fe8fb19SBen Gras #ifdef WIDE_DOUBLE
10592fe8fb19SBen Gras case 'a':
10602fe8fb19SBen Gras case 'A':
10612fe8fb19SBen Gras if (ch == 'a') {
10622fe8fb19SBen Gras ox[1] = 'x';
10632fe8fb19SBen Gras xdigs = xdigs_lower;
10642fe8fb19SBen Gras expchar = 'p';
10652fe8fb19SBen Gras } else {
10662fe8fb19SBen Gras ox[1] = 'X';
10672fe8fb19SBen Gras xdigs = xdigs_upper;
10682fe8fb19SBen Gras expchar = 'P';
10692fe8fb19SBen Gras }
10702fe8fb19SBen Gras if (prec >= 0)
10712fe8fb19SBen Gras prec++;
10722fe8fb19SBen Gras if (flags & LONGDBL) {
10732fe8fb19SBen Gras fparg.ldbl = GETARG(long double);
10742fe8fb19SBen Gras dtoaresult =
10752fe8fb19SBen Gras __hldtoa(fparg.ldbl, xdigs, prec,
10762fe8fb19SBen Gras &expt, &signflag, &dtoaend);
10772fe8fb19SBen Gras } else {
10782fe8fb19SBen Gras fparg.dbl = GETARG(double);
10792fe8fb19SBen Gras dtoaresult =
10802fe8fb19SBen Gras __hdtoa(fparg.dbl, xdigs, prec,
10812fe8fb19SBen Gras &expt, &signflag, &dtoaend);
10822fe8fb19SBen Gras }
10832fe8fb19SBen Gras if (dtoaresult == NULL)
10842fe8fb19SBen Gras goto oomem;
10852fe8fb19SBen Gras
1086f14fb602SLionel Sambuc if (prec < 0) {
1087f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(int,
1088f14fb602SLionel Sambuc dtoaend - dtoaresult));
1089f14fb602SLionel Sambuc prec = (int)(dtoaend - dtoaresult);
1090f14fb602SLionel Sambuc }
10912fe8fb19SBen Gras if (expt == INT_MAX)
10922fe8fb19SBen Gras ox[1] = '\0';
1093f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(int, dtoaend - dtoaresult));
1094f14fb602SLionel Sambuc ndig = (int)(dtoaend - dtoaresult);
10952fe8fb19SBen Gras if (convbuf != NULL)
10962fe8fb19SBen Gras free(convbuf);
10972fe8fb19SBen Gras #ifndef NARROW
109884d9c625SLionel Sambuc result = convbuf = __mbsconv(dtoaresult, -1, loc);
10992fe8fb19SBen Gras #else
11002fe8fb19SBen Gras /*XXX inefficient*/
11012fe8fb19SBen Gras result = convbuf = strdup(dtoaresult);
11022fe8fb19SBen Gras #endif
11032fe8fb19SBen Gras if (result == NULL)
11042fe8fb19SBen Gras goto oomem;
11052fe8fb19SBen Gras __freedtoa(dtoaresult);
11062fe8fb19SBen Gras goto fp_common;
11072fe8fb19SBen Gras case 'e':
11082fe8fb19SBen Gras case 'E':
11092fe8fb19SBen Gras expchar = ch;
11102fe8fb19SBen Gras if (prec < 0) /* account for digit before decpt */
11112fe8fb19SBen Gras prec = DEFPREC + 1;
11122fe8fb19SBen Gras else
11132fe8fb19SBen Gras prec++;
11142fe8fb19SBen Gras goto fp_begin;
11152fe8fb19SBen Gras case 'f':
11162fe8fb19SBen Gras case 'F':
11172fe8fb19SBen Gras expchar = '\0';
11182fe8fb19SBen Gras goto fp_begin;
11192fe8fb19SBen Gras case 'g':
11202fe8fb19SBen Gras case 'G':
11212fe8fb19SBen Gras expchar = ch - ('g' - 'e');
11222fe8fb19SBen Gras if (prec == 0)
11232fe8fb19SBen Gras prec = 1;
11242fe8fb19SBen Gras fp_begin:
11252fe8fb19SBen Gras if (prec < 0)
11262fe8fb19SBen Gras prec = DEFPREC;
11272fe8fb19SBen Gras if (flags & LONGDBL) {
11282fe8fb19SBen Gras fparg.ldbl = GETARG(long double);
11292fe8fb19SBen Gras dtoaresult =
11302fe8fb19SBen Gras __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec,
11312fe8fb19SBen Gras &expt, &signflag, &dtoaend);
11322fe8fb19SBen Gras } else {
11332fe8fb19SBen Gras fparg.dbl = GETARG(double);
11342fe8fb19SBen Gras dtoaresult =
11352fe8fb19SBen Gras __dtoa(fparg.dbl, expchar ? 2 : 3, prec,
11362fe8fb19SBen Gras &expt, &signflag, &dtoaend);
11372fe8fb19SBen Gras if (expt == 9999)
11382fe8fb19SBen Gras expt = INT_MAX;
11392fe8fb19SBen Gras }
11402fe8fb19SBen Gras if (dtoaresult == NULL)
11412fe8fb19SBen Gras goto oomem;
1142f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(int, dtoaend - dtoaresult));
1143f14fb602SLionel Sambuc ndig = (int)(dtoaend - dtoaresult);
11442fe8fb19SBen Gras if (convbuf != NULL)
11452fe8fb19SBen Gras free(convbuf);
11462fe8fb19SBen Gras #ifndef NARROW
114784d9c625SLionel Sambuc result = convbuf = __mbsconv(dtoaresult, -1, loc);
11482fe8fb19SBen Gras #else
11492fe8fb19SBen Gras /*XXX inefficient*/
11502fe8fb19SBen Gras result = convbuf = strdup(dtoaresult);
11512fe8fb19SBen Gras #endif
11522fe8fb19SBen Gras if (result == NULL)
11532fe8fb19SBen Gras goto oomem;
11542fe8fb19SBen Gras __freedtoa(dtoaresult);
11552fe8fb19SBen Gras fp_common:
11562fe8fb19SBen Gras if (signflag)
11572fe8fb19SBen Gras sign = '-';
11582fe8fb19SBen Gras if (expt == INT_MAX) { /* inf or nan */
11592fe8fb19SBen Gras if (*result == 'N') {
11602fe8fb19SBen Gras result = (ch >= 'a') ? STRCONST("nan") :
11612fe8fb19SBen Gras STRCONST("NAN");
11622fe8fb19SBen Gras sign = '\0';
11632fe8fb19SBen Gras } else
11642fe8fb19SBen Gras result = (ch >= 'a') ? STRCONST("inf") :
11652fe8fb19SBen Gras STRCONST("INF");
11662fe8fb19SBen Gras size = 3;
11672fe8fb19SBen Gras flags &= ~ZEROPAD;
11682fe8fb19SBen Gras break;
11692fe8fb19SBen Gras }
11702fe8fb19SBen Gras #else
11712fe8fb19SBen Gras case 'e':
11722fe8fb19SBen Gras case 'E':
11732fe8fb19SBen Gras case 'f':
11742fe8fb19SBen Gras case 'F':
11752fe8fb19SBen Gras case 'g':
11762fe8fb19SBen Gras case 'G':
11772fe8fb19SBen Gras if (prec == -1) {
11782fe8fb19SBen Gras prec = DEFPREC;
11792fe8fb19SBen Gras } else if ((ch == 'g' || ch == 'G') && prec == 0) {
11802fe8fb19SBen Gras prec = 1;
11812fe8fb19SBen Gras }
11822fe8fb19SBen Gras
11832fe8fb19SBen Gras if (flags & LONGDBL) {
11842fe8fb19SBen Gras _double = (double) GETARG(long double);
11852fe8fb19SBen Gras } else {
11862fe8fb19SBen Gras _double = GETARG(double);
11872fe8fb19SBen Gras }
11882fe8fb19SBen Gras
11892fe8fb19SBen Gras /* do this before tricky precision changes */
11902fe8fb19SBen Gras if (isinf(_double)) {
11912fe8fb19SBen Gras if (_double < 0)
11922fe8fb19SBen Gras sign = '-';
11932fe8fb19SBen Gras if (ch == 'E' || ch == 'F' || ch == 'G')
11942fe8fb19SBen Gras result = STRCONST("INF");
11952fe8fb19SBen Gras else
11962fe8fb19SBen Gras result = STRCONST("inf");
11972fe8fb19SBen Gras size = 3;
11982fe8fb19SBen Gras flags &= ~ZEROPAD;
11992fe8fb19SBen Gras break;
12002fe8fb19SBen Gras }
12012fe8fb19SBen Gras if (isnan(_double)) {
12022fe8fb19SBen Gras if (ch == 'E' || ch == 'F' || ch == 'G')
12032fe8fb19SBen Gras result = STRCONST("NAN");
12042fe8fb19SBen Gras else
12052fe8fb19SBen Gras result = STRCONST("nan");
12062fe8fb19SBen Gras size = 3;
12072fe8fb19SBen Gras flags &= ~ZEROPAD;
12082fe8fb19SBen Gras break;
12092fe8fb19SBen Gras }
12102fe8fb19SBen Gras
12112fe8fb19SBen Gras flags |= FPT;
12122fe8fb19SBen Gras dtoaresult = cvt(_double, prec, flags, &softsign,
12132fe8fb19SBen Gras &expt, ch, &ndig);
12142fe8fb19SBen Gras if (dtoaresult == NULL)
12152fe8fb19SBen Gras goto oomem;
12162fe8fb19SBen Gras if (convbuf != NULL)
12172fe8fb19SBen Gras free(convbuf);
12182fe8fb19SBen Gras #ifndef NARROW
121984d9c625SLionel Sambuc result = convbuf = __mbsconv(dtoaresult, -1, loc);
12202fe8fb19SBen Gras #else
12212fe8fb19SBen Gras /*XXX inefficient*/
12222fe8fb19SBen Gras result = convbuf = strdup(dtoaresult);
12232fe8fb19SBen Gras #endif
12242fe8fb19SBen Gras if (result == NULL)
12252fe8fb19SBen Gras goto oomem;
12262fe8fb19SBen Gras __freedtoa(dtoaresult);
12272fe8fb19SBen Gras if (softsign)
12282fe8fb19SBen Gras sign = '-';
12292fe8fb19SBen Gras #endif
12302fe8fb19SBen Gras flags |= FPT;
12312fe8fb19SBen Gras if (ch == 'g' || ch == 'G') {
12322fe8fb19SBen Gras if (expt > -4 && expt <= prec) {
12332fe8fb19SBen Gras /* Make %[gG] smell like %[fF] */
12342fe8fb19SBen Gras expchar = '\0';
12352fe8fb19SBen Gras if (flags & ALT)
12362fe8fb19SBen Gras prec -= expt;
12372fe8fb19SBen Gras else
12382fe8fb19SBen Gras prec = ndig - expt;
12392fe8fb19SBen Gras if (prec < 0)
12402fe8fb19SBen Gras prec = 0;
12412fe8fb19SBen Gras } else {
12422fe8fb19SBen Gras /*
12432fe8fb19SBen Gras * Make %[gG] smell like %[eE], but
12442fe8fb19SBen Gras * trim trailing zeroes if no # flag.
12452fe8fb19SBen Gras */
12462fe8fb19SBen Gras if (!(flags & ALT))
12472fe8fb19SBen Gras prec = ndig;
12482fe8fb19SBen Gras }
12492fe8fb19SBen Gras }
12502fe8fb19SBen Gras if (expchar) {
12512fe8fb19SBen Gras expsize = exponent(expstr, expt - 1, expchar);
12522fe8fb19SBen Gras size = expsize + prec;
12532fe8fb19SBen Gras if (prec > 1 || flags & ALT)
12542fe8fb19SBen Gras ++size;
12552fe8fb19SBen Gras } else {
12562fe8fb19SBen Gras /* space for digits before decimal point */
12572fe8fb19SBen Gras if (expt > 0)
12582fe8fb19SBen Gras size = expt;
12592fe8fb19SBen Gras else /* "0" */
12602fe8fb19SBen Gras size = 1;
12612fe8fb19SBen Gras /* space for decimal pt and following digits */
12622fe8fb19SBen Gras if (prec || flags & ALT)
12632fe8fb19SBen Gras size += prec + 1;
12642fe8fb19SBen Gras if (grouping && expt > 0) {
12652fe8fb19SBen Gras /* space for thousands' grouping */
12662fe8fb19SBen Gras nseps = nrepeats = 0;
12672fe8fb19SBen Gras lead = expt;
1268f14fb602SLionel Sambuc while ((unsigned char)*grouping
1269f14fb602SLionel Sambuc != (unsigned char)CHAR_MAX) {
12702fe8fb19SBen Gras if (lead <= *grouping)
12712fe8fb19SBen Gras break;
12722fe8fb19SBen Gras lead -= *grouping;
12732fe8fb19SBen Gras if (*(grouping+1)) {
12742fe8fb19SBen Gras nseps++;
12752fe8fb19SBen Gras grouping++;
12762fe8fb19SBen Gras } else
12772fe8fb19SBen Gras nrepeats++;
12782fe8fb19SBen Gras }
12792fe8fb19SBen Gras size += nseps + nrepeats;
12802fe8fb19SBen Gras } else
12812fe8fb19SBen Gras lead = expt;
12822fe8fb19SBen Gras }
12832fe8fb19SBen Gras break;
12842fe8fb19SBen Gras #endif /* !NO_FLOATING_POINT */
12852fe8fb19SBen Gras case 'n':
12862fe8fb19SBen Gras /*
12872fe8fb19SBen Gras * Assignment-like behavior is specified if the
12882fe8fb19SBen Gras * value overflows or is otherwise unrepresentable.
12892fe8fb19SBen Gras * C99 says to use `signed char' for %hhn conversions.
12902fe8fb19SBen Gras */
12912fe8fb19SBen Gras if (flags & LLONGINT)
12922fe8fb19SBen Gras *GETARG(long long *) = ret;
12932fe8fb19SBen Gras else if (flags & SIZET)
12942fe8fb19SBen Gras *GETARG(ssize_t *) = (ssize_t)ret;
12952fe8fb19SBen Gras else if (flags & PTRDIFFT)
12962fe8fb19SBen Gras *GETARG(ptrdiff_t *) = ret;
12972fe8fb19SBen Gras else if (flags & INTMAXT)
12982fe8fb19SBen Gras *GETARG(intmax_t *) = ret;
12992fe8fb19SBen Gras else if (flags & LONGINT)
13002fe8fb19SBen Gras *GETARG(long *) = ret;
13012fe8fb19SBen Gras else if (flags & SHORTINT)
13022fe8fb19SBen Gras *GETARG(short *) = ret;
13032fe8fb19SBen Gras else if (flags & CHARINT)
13042fe8fb19SBen Gras *GETARG(signed char *) = ret;
13052fe8fb19SBen Gras else
13062fe8fb19SBen Gras *GETARG(int *) = ret;
13072fe8fb19SBen Gras continue; /* no output */
13082fe8fb19SBen Gras case 'O':
13092fe8fb19SBen Gras flags |= LONGINT;
13102fe8fb19SBen Gras /*FALLTHROUGH*/
13112fe8fb19SBen Gras case 'o':
13122fe8fb19SBen Gras if (flags & INTMAX_SIZE)
13132fe8fb19SBen Gras ujval = UJARG();
13142fe8fb19SBen Gras else
13152fe8fb19SBen Gras ulval = UARG();
13162fe8fb19SBen Gras base = 8;
13172fe8fb19SBen Gras goto nosign;
13182fe8fb19SBen Gras case 'p':
13192fe8fb19SBen Gras /*-
13202fe8fb19SBen Gras * ``The argument shall be a pointer to void. The
13212fe8fb19SBen Gras * value of the pointer is converted to a sequence
13222fe8fb19SBen Gras * of printable characters, in an implementation-
13232fe8fb19SBen Gras * defined manner.''
13242fe8fb19SBen Gras * -- ANSI X3J11
13252fe8fb19SBen Gras */
13262fe8fb19SBen Gras ujval = (uintmax_t)(uintptr_t)GETARG(void *);
13272fe8fb19SBen Gras base = 16;
13282fe8fb19SBen Gras xdigs = xdigs_lower;
13292fe8fb19SBen Gras flags = flags | INTMAXT;
13302fe8fb19SBen Gras ox[1] = 'x';
13312fe8fb19SBen Gras goto nosign;
13322fe8fb19SBen Gras case 'S':
13332fe8fb19SBen Gras flags |= LONGINT;
13342fe8fb19SBen Gras /*FALLTHROUGH*/
13352fe8fb19SBen Gras case 's':
13362fe8fb19SBen Gras if ((flags & LONGINT) != MULTI) {
13372fe8fb19SBen Gras if ((result = GETARG(CHAR_T *)) == NULL)
13382fe8fb19SBen Gras result = STRCONST("(null)");
13392fe8fb19SBen Gras } else {
13402fe8fb19SBen Gras MCHAR_T *mc;
13412fe8fb19SBen Gras
13422fe8fb19SBen Gras if (convbuf != NULL)
13432fe8fb19SBen Gras free(convbuf);
13442fe8fb19SBen Gras if ((mc = GETARG(MCHAR_T *)) == NULL)
13452fe8fb19SBen Gras result = STRCONST("(null)");
13462fe8fb19SBen Gras else {
134784d9c625SLionel Sambuc convbuf = SCONV(mc, prec, loc);
13482fe8fb19SBen Gras if (convbuf == NULL) {
13492fe8fb19SBen Gras fp->_flags |= __SERR;
13502fe8fb19SBen Gras goto error;
13512fe8fb19SBen Gras }
13522fe8fb19SBen Gras result = convbuf;
13532fe8fb19SBen Gras }
13542fe8fb19SBen Gras }
13552fe8fb19SBen Gras
13562fe8fb19SBen Gras if (prec >= 0) {
13572fe8fb19SBen Gras /*
13582fe8fb19SBen Gras * can't use STRLEN; can only look for the
13592fe8fb19SBen Gras * NUL in the first `prec' characters, and
13602fe8fb19SBen Gras * STRLEN() will go further.
13612fe8fb19SBen Gras */
13622fe8fb19SBen Gras CHAR_T *p = MEMCHR(result, 0, (size_t)prec);
13632fe8fb19SBen Gras
13642fe8fb19SBen Gras if (p != NULL) {
1365f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(int,
1366f14fb602SLionel Sambuc p - result));
1367f14fb602SLionel Sambuc size = (int)(p - result);
13682fe8fb19SBen Gras if (size > prec)
13692fe8fb19SBen Gras size = prec;
13702fe8fb19SBen Gras } else
13712fe8fb19SBen Gras size = prec;
1372f14fb602SLionel Sambuc } else {
1373f14fb602SLionel Sambuc size_t rlen = STRLEN(result);
1374f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(int, rlen));
1375f14fb602SLionel Sambuc size = (int)rlen;
1376f14fb602SLionel Sambuc }
13772fe8fb19SBen Gras sign = '\0';
13782fe8fb19SBen Gras break;
13792fe8fb19SBen Gras case 'U':
13802fe8fb19SBen Gras flags |= LONGINT;
13812fe8fb19SBen Gras /*FALLTHROUGH*/
13822fe8fb19SBen Gras case 'u':
13832fe8fb19SBen Gras if (flags & INTMAX_SIZE)
13842fe8fb19SBen Gras ujval = UJARG();
13852fe8fb19SBen Gras else
13862fe8fb19SBen Gras ulval = UARG();
13872fe8fb19SBen Gras base = 10;
13882fe8fb19SBen Gras goto nosign;
13892fe8fb19SBen Gras case 'X':
13902fe8fb19SBen Gras xdigs = xdigs_upper;
13912fe8fb19SBen Gras goto hex;
13922fe8fb19SBen Gras case 'x':
13932fe8fb19SBen Gras xdigs = xdigs_lower;
13942fe8fb19SBen Gras hex:
13952fe8fb19SBen Gras if (flags & INTMAX_SIZE)
13962fe8fb19SBen Gras ujval = UJARG();
13972fe8fb19SBen Gras else
13982fe8fb19SBen Gras ulval = UARG();
13992fe8fb19SBen Gras base = 16;
14002fe8fb19SBen Gras /* leading 0x/X only if non-zero */
14012fe8fb19SBen Gras if (flags & ALT &&
14022fe8fb19SBen Gras (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0))
14032fe8fb19SBen Gras ox[1] = ch;
14042fe8fb19SBen Gras
14052fe8fb19SBen Gras flags &= ~GROUPING;
14062fe8fb19SBen Gras /* unsigned conversions */
14072fe8fb19SBen Gras nosign: sign = '\0';
14082fe8fb19SBen Gras /*-
14092fe8fb19SBen Gras * ``... diouXx conversions ... if a precision is
14102fe8fb19SBen Gras * specified, the 0 flag will be ignored.''
14112fe8fb19SBen Gras * -- ANSI X3J11
14122fe8fb19SBen Gras */
14132fe8fb19SBen Gras number: if ((dprec = prec) >= 0)
14142fe8fb19SBen Gras flags &= ~ZEROPAD;
14152fe8fb19SBen Gras
14162fe8fb19SBen Gras /*-
14172fe8fb19SBen Gras * ``The result of converting a zero value with an
14182fe8fb19SBen Gras * explicit precision of zero is no characters.''
14192fe8fb19SBen Gras * -- ANSI X3J11
14202fe8fb19SBen Gras *
14212fe8fb19SBen Gras * ``The C Standard is clear enough as is. The call
14222fe8fb19SBen Gras * printf("%#.0o", 0) should print 0.''
14232fe8fb19SBen Gras * -- Defect Report #151
14242fe8fb19SBen Gras */
14252fe8fb19SBen Gras result = cp = buf + BUF;
14262fe8fb19SBen Gras if (flags & INTMAX_SIZE) {
14272fe8fb19SBen Gras if (ujval != 0 || prec != 0 ||
14282fe8fb19SBen Gras (flags & ALT && base == 8))
14292fe8fb19SBen Gras result = __ujtoa(ujval, cp, base,
14302fe8fb19SBen Gras flags & ALT, xdigs,
14312fe8fb19SBen Gras flags & GROUPING, thousands_sep,
14322fe8fb19SBen Gras grouping);
14332fe8fb19SBen Gras } else {
14342fe8fb19SBen Gras if (ulval != 0 || prec != 0 ||
14352fe8fb19SBen Gras (flags & ALT && base == 8))
14362fe8fb19SBen Gras result = __ultoa(ulval, cp, base,
14372fe8fb19SBen Gras flags & ALT, xdigs,
14382fe8fb19SBen Gras flags & GROUPING, thousands_sep,
14392fe8fb19SBen Gras grouping);
14402fe8fb19SBen Gras }
1441f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(int, buf + BUF - result));
1442f14fb602SLionel Sambuc size = (int)(buf + BUF - result);
14432fe8fb19SBen Gras if (size > BUF) /* should never happen */
14442fe8fb19SBen Gras abort();
14452fe8fb19SBen Gras break;
14462fe8fb19SBen Gras default: /* "%?" prints ?, unless ? is NUL */
14472fe8fb19SBen Gras if (ch == '\0')
14482fe8fb19SBen Gras goto done;
14492fe8fb19SBen Gras /* pretend it was %c with argument ch */
14502fe8fb19SBen Gras *buf = ch;
14512fe8fb19SBen Gras result = buf;
14522fe8fb19SBen Gras size = 1;
14532fe8fb19SBen Gras sign = '\0';
14542fe8fb19SBen Gras break;
14552fe8fb19SBen Gras }
14562fe8fb19SBen Gras
14572fe8fb19SBen Gras /*
14582fe8fb19SBen Gras * All reasonable formats wind up here. At this point, `result'
14592fe8fb19SBen Gras * points to a string which (if not flags&LADJUST) should be
14602fe8fb19SBen Gras * padded out to `width' places. If flags&ZEROPAD, it should
14612fe8fb19SBen Gras * first be prefixed by any sign or other prefix; otherwise,
14622fe8fb19SBen Gras * it should be blank padded before the prefix is emitted.
14632fe8fb19SBen Gras * After any left-hand padding and prefixing, emit zeroes
14642fe8fb19SBen Gras * required by a decimal [diouxX] precision, then print the
14652fe8fb19SBen Gras * string proper, then emit zeroes required by any leftover
14662fe8fb19SBen Gras * floating precision; finally, if LADJUST, pad with blanks.
14672fe8fb19SBen Gras *
14682fe8fb19SBen Gras * Compute actual size, so we know how much to pad.
14692fe8fb19SBen Gras * size excludes decimal prec; realsz includes it.
14702fe8fb19SBen Gras */
14712fe8fb19SBen Gras realsz = dprec > size ? dprec : size;
14722fe8fb19SBen Gras if (sign)
14732fe8fb19SBen Gras realsz++;
14742fe8fb19SBen Gras if (ox[1])
14752fe8fb19SBen Gras realsz += 2;
14762fe8fb19SBen Gras
14772fe8fb19SBen Gras prsize = width > realsz ? width : realsz;
14782fe8fb19SBen Gras if ((unsigned)ret + prsize > INT_MAX) {
14792fe8fb19SBen Gras ret = END_OF_FILE;
14802fe8fb19SBen Gras goto error;
14812fe8fb19SBen Gras }
14822fe8fb19SBen Gras
14832fe8fb19SBen Gras /* right-adjusting blank padding */
14842fe8fb19SBen Gras if ((flags & (LADJUST|ZEROPAD)) == 0)
14852fe8fb19SBen Gras PAD(width - realsz, blanks);
14862fe8fb19SBen Gras
14872fe8fb19SBen Gras /* prefix */
14882fe8fb19SBen Gras if (sign)
14892fe8fb19SBen Gras PRINT(&sign, 1);
14902fe8fb19SBen Gras
14912fe8fb19SBen Gras if (ox[1]) { /* ox[1] is either x, X, or \0 */
14922fe8fb19SBen Gras ox[0] = '0';
14932fe8fb19SBen Gras PRINT(ox, 2);
14942fe8fb19SBen Gras }
14952fe8fb19SBen Gras
14962fe8fb19SBen Gras /* right-adjusting zero padding */
14972fe8fb19SBen Gras if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
14982fe8fb19SBen Gras PAD(width - realsz, zeroes);
14992fe8fb19SBen Gras
15002fe8fb19SBen Gras /* leading zeroes from decimal precision */
15012fe8fb19SBen Gras PAD(dprec - size, zeroes);
15022fe8fb19SBen Gras
15032fe8fb19SBen Gras /* the string or number proper */
15042fe8fb19SBen Gras #ifndef NO_FLOATING_POINT
15052fe8fb19SBen Gras if ((flags & FPT) == 0) {
15062fe8fb19SBen Gras PRINT(result, size);
15072fe8fb19SBen Gras } else { /* glue together f_p fragments */
15082fe8fb19SBen Gras if (!expchar) { /* %[fF] or sufficiently short %[gG] */
15092fe8fb19SBen Gras if (expt <= 0) {
15102fe8fb19SBen Gras PRINT(zeroes, 1);
15112fe8fb19SBen Gras if (prec || flags & ALT)
15122fe8fb19SBen Gras PRINT(decimal_point, 1);
15132fe8fb19SBen Gras PAD(-expt, zeroes);
15142fe8fb19SBen Gras /* already handled initial 0's */
15152fe8fb19SBen Gras prec += expt;
15162fe8fb19SBen Gras } else {
15172fe8fb19SBen Gras PRINTANDPAD(result, convbuf + ndig,
15182fe8fb19SBen Gras lead, zeroes);
15192fe8fb19SBen Gras result += lead;
15202fe8fb19SBen Gras if (grouping) {
15212fe8fb19SBen Gras while (nseps>0 || nrepeats>0) {
15222fe8fb19SBen Gras if (nrepeats > 0)
15232fe8fb19SBen Gras nrepeats--;
15242fe8fb19SBen Gras else {
15252fe8fb19SBen Gras grouping--;
15262fe8fb19SBen Gras nseps--;
15272fe8fb19SBen Gras }
15282fe8fb19SBen Gras PRINT(&thousands_sep,
15292fe8fb19SBen Gras 1);
15302fe8fb19SBen Gras PRINTANDPAD(result,
15312fe8fb19SBen Gras convbuf + ndig,
15322fe8fb19SBen Gras *grouping, zeroes);
15332fe8fb19SBen Gras result += *grouping;
15342fe8fb19SBen Gras }
15352fe8fb19SBen Gras if (result > convbuf + ndig)
15362fe8fb19SBen Gras result = convbuf + ndig;
15372fe8fb19SBen Gras }
15382fe8fb19SBen Gras if (prec || flags & ALT) {
15392fe8fb19SBen Gras buf[0] = *decimal_point;
15402fe8fb19SBen Gras PRINT(buf, 1);
15412fe8fb19SBen Gras }
15422fe8fb19SBen Gras }
15432fe8fb19SBen Gras PRINTANDPAD(result, convbuf + ndig, prec,
15442fe8fb19SBen Gras zeroes);
15452fe8fb19SBen Gras } else { /* %[eE] or sufficiently long %[gG] */
15462fe8fb19SBen Gras if (prec > 1 || flags & ALT) {
15472fe8fb19SBen Gras buf[0] = *result++;
15482fe8fb19SBen Gras buf[1] = *decimal_point;
15492fe8fb19SBen Gras PRINT(buf, 2);
15502fe8fb19SBen Gras PRINT(result, ndig-1);
15512fe8fb19SBen Gras PAD(prec - ndig, zeroes);
15522fe8fb19SBen Gras } else /* XeYYY */
15532fe8fb19SBen Gras PRINT(result, 1);
15542fe8fb19SBen Gras PRINT(expstr, expsize);
15552fe8fb19SBen Gras }
15562fe8fb19SBen Gras }
15572fe8fb19SBen Gras #else
15582fe8fb19SBen Gras PRINT(result, size);
15592fe8fb19SBen Gras #endif
15602fe8fb19SBen Gras /* left-adjusting padding (always blank) */
15612fe8fb19SBen Gras if (flags & LADJUST)
15622fe8fb19SBen Gras PAD(width - realsz, blanks);
15632fe8fb19SBen Gras
15642fe8fb19SBen Gras /* finally, adjust ret */
15652fe8fb19SBen Gras ret += prsize;
15662fe8fb19SBen Gras FLUSH();
15672fe8fb19SBen Gras }
15682fe8fb19SBen Gras done:
15692fe8fb19SBen Gras FLUSH();
15702fe8fb19SBen Gras error:
15712fe8fb19SBen Gras va_end(orgap);
15722fe8fb19SBen Gras if (convbuf != NULL)
15732fe8fb19SBen Gras free(convbuf);
15742fe8fb19SBen Gras if (__sferror(fp))
15752fe8fb19SBen Gras ret = END_OF_FILE;
15762fe8fb19SBen Gras if ((argtable != NULL) && (argtable != statargtable))
15772fe8fb19SBen Gras free (argtable);
1578f14fb602SLionel Sambuc return ret;
15792fe8fb19SBen Gras /* NOTREACHED */
15802fe8fb19SBen Gras oomem:
15812fe8fb19SBen Gras errno = ENOMEM;
15822fe8fb19SBen Gras ret = END_OF_FILE;
15832fe8fb19SBen Gras goto error;
15842fe8fb19SBen Gras }
15852fe8fb19SBen Gras
15862fe8fb19SBen Gras /*
15872fe8fb19SBen Gras * Find all arguments when a positional parameter is encountered. Returns a
15882fe8fb19SBen Gras * table, indexed by argument number, of pointers to each arguments. The
15892fe8fb19SBen Gras * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
15902fe8fb19SBen Gras * It will be replaces with a malloc-ed one if it overflows.
15912fe8fb19SBen Gras */
15922fe8fb19SBen Gras static int
__find_arguments(const CHAR_T * fmt0,va_list ap,union arg ** argtable)15932fe8fb19SBen Gras __find_arguments(const CHAR_T *fmt0, va_list ap, union arg **argtable)
15942fe8fb19SBen Gras {
15952fe8fb19SBen Gras CHAR_T *fmt; /* format string */
15962fe8fb19SBen Gras int ch; /* character from fmt */
1597f14fb602SLionel Sambuc size_t n, n2; /* handy index (short term usage) */
15982fe8fb19SBen Gras CHAR_T *cp; /* handy char pointer (short term usage) */
15992fe8fb19SBen Gras int flags; /* flags as above */
16002fe8fb19SBen Gras enum typeid *typetable; /* table of types */
16012fe8fb19SBen Gras enum typeid stattypetable [STATIC_ARG_TBL_SIZE];
1602f14fb602SLionel Sambuc size_t tablesize; /* current size of type table */
1603f14fb602SLionel Sambuc size_t tablemax; /* largest used index in table */
1604f14fb602SLionel Sambuc size_t nextarg; /* 1-based argument index */
1605f14fb602SLionel Sambuc size_t nitems; /* number of items we picked from the stack */
16062fe8fb19SBen Gras
16072fe8fb19SBen Gras /*
16082fe8fb19SBen Gras * Add an argument type to the table, expanding if necessary.
1609f14fb602SLionel Sambuc * Check for overflow.
16102fe8fb19SBen Gras */
16112fe8fb19SBen Gras #define ADDTYPE(type) \
16122fe8fb19SBen Gras do { \
1613f14fb602SLionel Sambuc if (nextarg > SIZE_MAX / sizeof(**argtable)) { \
1614f14fb602SLionel Sambuc if (typetable != stattypetable) \
1615f14fb602SLionel Sambuc free(typetable); \
1616f14fb602SLionel Sambuc return -1; \
1617f14fb602SLionel Sambuc } \
16182fe8fb19SBen Gras if (nextarg >= tablesize) \
16192fe8fb19SBen Gras if (__grow_type_table(nextarg, &typetable, \
16202fe8fb19SBen Gras &tablesize) == -1) \
16212fe8fb19SBen Gras return -1; \
16222fe8fb19SBen Gras if (nextarg > tablemax) \
16232fe8fb19SBen Gras tablemax = nextarg; \
16242fe8fb19SBen Gras typetable[nextarg++] = type; \
1625f14fb602SLionel Sambuc nitems++; \
16262fe8fb19SBen Gras } while (/*CONSTCOND*/0)
16272fe8fb19SBen Gras
16282fe8fb19SBen Gras #define ADDSARG() \
16292fe8fb19SBen Gras do { \
16302fe8fb19SBen Gras if (flags & INTMAXT) \
16312fe8fb19SBen Gras ADDTYPE(T_INTMAXT); \
16322fe8fb19SBen Gras else if (flags & SIZET) \
16332fe8fb19SBen Gras ADDTYPE(T_SSIZET); \
16342fe8fb19SBen Gras else if (flags & PTRDIFFT) \
16352fe8fb19SBen Gras ADDTYPE(T_PTRDIFFT); \
16362fe8fb19SBen Gras else if (flags & LLONGINT) \
16372fe8fb19SBen Gras ADDTYPE(T_LLONG); \
16382fe8fb19SBen Gras else if (flags & LONGINT) \
16392fe8fb19SBen Gras ADDTYPE(T_LONG); \
16402fe8fb19SBen Gras else \
16412fe8fb19SBen Gras ADDTYPE(T_INT); \
16422fe8fb19SBen Gras } while (/*CONSTCOND*/0)
16432fe8fb19SBen Gras
16442fe8fb19SBen Gras #define ADDUARG() \
16452fe8fb19SBen Gras do { \
16462fe8fb19SBen Gras if (flags & INTMAXT) \
16472fe8fb19SBen Gras ADDTYPE(T_UINTMAXT); \
16482fe8fb19SBen Gras else if (flags & SIZET) \
16492fe8fb19SBen Gras ADDTYPE(T_SIZET); \
16502fe8fb19SBen Gras else if (flags & PTRDIFFT) \
16512fe8fb19SBen Gras ADDTYPE(T_PTRDIFFT); \
16522fe8fb19SBen Gras else if (flags & LLONGINT) \
16532fe8fb19SBen Gras ADDTYPE(T_U_LLONG); \
16542fe8fb19SBen Gras else if (flags & LONGINT) \
16552fe8fb19SBen Gras ADDTYPE(T_U_LONG); \
16562fe8fb19SBen Gras else \
16572fe8fb19SBen Gras ADDTYPE(T_U_INT); \
16582fe8fb19SBen Gras } while (/*CONSTCOND*/0)
16592fe8fb19SBen Gras /*
16602fe8fb19SBen Gras * Add * arguments to the type array.
16612fe8fb19SBen Gras */
16622fe8fb19SBen Gras #define ADDASTER() \
16632fe8fb19SBen Gras n2 = 0; \
16642fe8fb19SBen Gras cp = fmt; \
16652fe8fb19SBen Gras while (is_digit(*cp)) { \
16662fe8fb19SBen Gras n2 = 10 * n2 + to_digit(*cp); \
16672fe8fb19SBen Gras cp++; \
16682fe8fb19SBen Gras } \
16692fe8fb19SBen Gras if (*cp == '$') { \
1670f14fb602SLionel Sambuc size_t hold = nextarg; \
16712fe8fb19SBen Gras nextarg = n2; \
16722fe8fb19SBen Gras ADDTYPE(T_INT); \
16732fe8fb19SBen Gras nextarg = hold; \
16742fe8fb19SBen Gras fmt = ++cp; \
16752fe8fb19SBen Gras } else { \
16762fe8fb19SBen Gras ADDTYPE(T_INT); \
16772fe8fb19SBen Gras }
16782fe8fb19SBen Gras fmt = (CHAR_T *)__UNCONST(fmt0);
1679f14fb602SLionel Sambuc memset(stattypetable, 0, sizeof(stattypetable));
16802fe8fb19SBen Gras typetable = stattypetable;
16812fe8fb19SBen Gras tablesize = STATIC_ARG_TBL_SIZE;
16822fe8fb19SBen Gras tablemax = 0;
16832fe8fb19SBen Gras nextarg = 1;
1684f14fb602SLionel Sambuc nitems = 1;
16852fe8fb19SBen Gras
16862fe8fb19SBen Gras /*
16872fe8fb19SBen Gras * Scan the format for conversions (`%' character).
16882fe8fb19SBen Gras */
16892fe8fb19SBen Gras for (;;) {
16902fe8fb19SBen Gras for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
16912fe8fb19SBen Gras /* void */;
16922fe8fb19SBen Gras if (ch == '\0')
16932fe8fb19SBen Gras goto done;
16942fe8fb19SBen Gras fmt++; /* skip over '%' */
16952fe8fb19SBen Gras
16962fe8fb19SBen Gras flags = 0;
16972fe8fb19SBen Gras
16982fe8fb19SBen Gras rflag: ch = *fmt++;
16992fe8fb19SBen Gras reswitch: switch (ch) {
17002fe8fb19SBen Gras case ' ':
17012fe8fb19SBen Gras case '#':
17022fe8fb19SBen Gras goto rflag;
17032fe8fb19SBen Gras case '*':
17042fe8fb19SBen Gras ADDASTER ();
17052fe8fb19SBen Gras goto rflag;
17062fe8fb19SBen Gras case '-':
17072fe8fb19SBen Gras case '+':
17082fe8fb19SBen Gras case '\'':
17092fe8fb19SBen Gras goto rflag;
17102fe8fb19SBen Gras case '.':
17112fe8fb19SBen Gras if ((ch = *fmt++) == '*') {
17122fe8fb19SBen Gras ADDASTER ();
17132fe8fb19SBen Gras goto rflag;
17142fe8fb19SBen Gras }
17152fe8fb19SBen Gras while (is_digit(ch)) {
17162fe8fb19SBen Gras ch = *fmt++;
17172fe8fb19SBen Gras }
17182fe8fb19SBen Gras goto reswitch;
17192fe8fb19SBen Gras case '0':
17202fe8fb19SBen Gras goto rflag;
17212fe8fb19SBen Gras case '1': case '2': case '3': case '4':
17222fe8fb19SBen Gras case '5': case '6': case '7': case '8': case '9':
17232fe8fb19SBen Gras n = 0;
17242fe8fb19SBen Gras do {
17252fe8fb19SBen Gras n = 10 * n + to_digit(ch);
17262fe8fb19SBen Gras ch = *fmt++;
17272fe8fb19SBen Gras } while (is_digit(ch));
17282fe8fb19SBen Gras if (ch == '$') {
17292fe8fb19SBen Gras nextarg = n;
17302fe8fb19SBen Gras goto rflag;
17312fe8fb19SBen Gras }
17322fe8fb19SBen Gras goto reswitch;
17332fe8fb19SBen Gras #ifndef NO_FLOATING_POINT
17342fe8fb19SBen Gras case 'L':
17352fe8fb19SBen Gras flags |= LONGDBL;
17362fe8fb19SBen Gras goto rflag;
17372fe8fb19SBen Gras #endif
17382fe8fb19SBen Gras case 'h':
17392fe8fb19SBen Gras if (flags & SHORTINT) {
17402fe8fb19SBen Gras flags &= ~SHORTINT;
17412fe8fb19SBen Gras flags |= CHARINT;
17422fe8fb19SBen Gras } else
17432fe8fb19SBen Gras flags |= SHORTINT;
17442fe8fb19SBen Gras goto rflag;
17452fe8fb19SBen Gras case 'j':
17462fe8fb19SBen Gras flags |= INTMAXT;
17472fe8fb19SBen Gras goto rflag;
17482fe8fb19SBen Gras case 'l':
17492fe8fb19SBen Gras if (flags & LONGINT) {
17502fe8fb19SBen Gras flags &= ~LONGINT;
17512fe8fb19SBen Gras flags |= LLONGINT;
17522fe8fb19SBen Gras } else
17532fe8fb19SBen Gras flags |= LONGINT;
17542fe8fb19SBen Gras goto rflag;
17552fe8fb19SBen Gras case 'q':
17562fe8fb19SBen Gras flags |= LLONGINT; /* not necessarily */
17572fe8fb19SBen Gras goto rflag;
17582fe8fb19SBen Gras case 't':
17592fe8fb19SBen Gras flags |= PTRDIFFT;
17602fe8fb19SBen Gras goto rflag;
17612fe8fb19SBen Gras case 'z':
17622fe8fb19SBen Gras flags |= SIZET;
17632fe8fb19SBen Gras goto rflag;
17642fe8fb19SBen Gras case 'C':
17652fe8fb19SBen Gras flags |= LONGINT;
17662fe8fb19SBen Gras /*FALLTHROUGH*/
17672fe8fb19SBen Gras case 'c':
17682fe8fb19SBen Gras if (flags & LONGINT)
17692fe8fb19SBen Gras ADDTYPE(T_WINT);
17702fe8fb19SBen Gras else
17712fe8fb19SBen Gras ADDTYPE(T_INT);
17722fe8fb19SBen Gras break;
17732fe8fb19SBen Gras case 'D':
17742fe8fb19SBen Gras flags |= LONGINT;
17752fe8fb19SBen Gras /*FALLTHROUGH*/
17762fe8fb19SBen Gras case 'd':
17772fe8fb19SBen Gras case 'i':
17782fe8fb19SBen Gras ADDSARG();
17792fe8fb19SBen Gras break;
17802fe8fb19SBen Gras #ifndef NO_FLOATING_POINT
17812fe8fb19SBen Gras case 'a':
17822fe8fb19SBen Gras case 'A':
17832fe8fb19SBen Gras case 'e':
17842fe8fb19SBen Gras case 'E':
17852fe8fb19SBen Gras case 'f':
17862fe8fb19SBen Gras case 'g':
17872fe8fb19SBen Gras case 'G':
17882fe8fb19SBen Gras if (flags & LONGDBL)
17892fe8fb19SBen Gras ADDTYPE(T_LONG_DOUBLE);
17902fe8fb19SBen Gras else
17912fe8fb19SBen Gras ADDTYPE(T_DOUBLE);
17922fe8fb19SBen Gras break;
17932fe8fb19SBen Gras #endif /* !NO_FLOATING_POINT */
17942fe8fb19SBen Gras case 'n':
17952fe8fb19SBen Gras if (flags & INTMAXT)
17962fe8fb19SBen Gras ADDTYPE(TP_INTMAXT);
17972fe8fb19SBen Gras else if (flags & PTRDIFFT)
17982fe8fb19SBen Gras ADDTYPE(TP_PTRDIFFT);
17992fe8fb19SBen Gras else if (flags & SIZET)
18002fe8fb19SBen Gras ADDTYPE(TP_SIZET);
18012fe8fb19SBen Gras else if (flags & LLONGINT)
18022fe8fb19SBen Gras ADDTYPE(TP_LLONG);
18032fe8fb19SBen Gras else if (flags & LONGINT)
18042fe8fb19SBen Gras ADDTYPE(TP_LONG);
18052fe8fb19SBen Gras else if (flags & SHORTINT)
18062fe8fb19SBen Gras ADDTYPE(TP_SHORT);
18072fe8fb19SBen Gras else if (flags & CHARINT)
18082fe8fb19SBen Gras ADDTYPE(TP_SCHAR);
18092fe8fb19SBen Gras else
18102fe8fb19SBen Gras ADDTYPE(TP_INT);
18112fe8fb19SBen Gras continue; /* no output */
18122fe8fb19SBen Gras case 'O':
18132fe8fb19SBen Gras flags |= LONGINT;
18142fe8fb19SBen Gras /*FALLTHROUGH*/
18152fe8fb19SBen Gras case 'o':
18162fe8fb19SBen Gras ADDUARG();
18172fe8fb19SBen Gras break;
18182fe8fb19SBen Gras case 'p':
18192fe8fb19SBen Gras ADDTYPE(TP_VOID);
18202fe8fb19SBen Gras break;
18212fe8fb19SBen Gras case 'S':
18222fe8fb19SBen Gras flags |= LONGINT;
18232fe8fb19SBen Gras /*FALLTHROUGH*/
18242fe8fb19SBen Gras case 's':
18252fe8fb19SBen Gras if (flags & LONGINT)
18262fe8fb19SBen Gras ADDTYPE(TP_WCHAR);
18272fe8fb19SBen Gras else
18282fe8fb19SBen Gras ADDTYPE(TP_CHAR);
18292fe8fb19SBen Gras break;
18302fe8fb19SBen Gras case 'U':
18312fe8fb19SBen Gras flags |= LONGINT;
18322fe8fb19SBen Gras /*FALLTHROUGH*/
18332fe8fb19SBen Gras case 'u':
18342fe8fb19SBen Gras case 'X':
18352fe8fb19SBen Gras case 'x':
18362fe8fb19SBen Gras ADDUARG();
18372fe8fb19SBen Gras break;
18382fe8fb19SBen Gras default: /* "%?" prints ?, unless ? is NUL */
18392fe8fb19SBen Gras if (ch == '\0')
18402fe8fb19SBen Gras goto done;
18412fe8fb19SBen Gras break;
18422fe8fb19SBen Gras }
18432fe8fb19SBen Gras }
18442fe8fb19SBen Gras done:
18452fe8fb19SBen Gras /*
1846f14fb602SLionel Sambuc * nitems contains the number of arguments we picked from the stack.
1847f14fb602SLionel Sambuc * If tablemax is larger, this means that some positional argument,
1848f14fb602SLionel Sambuc * tried to pick an argument the number of arguments possibly supplied.
1849f14fb602SLionel Sambuc * Since positional arguments are typically used to swap the order of
1850f14fb602SLionel Sambuc * the printf arguments and not to pick random arguments from strange
1851f14fb602SLionel Sambuc * positions in the stack, we assume that if the positional argument
1852f14fb602SLionel Sambuc * is trying to pick beyond the end of arguments, then this is wrong.
1853f14fb602SLionel Sambuc * Alternatively we could find a way to figure out when va_arg() runs
1854f14fb602SLionel Sambuc * out, but how to do that?
1855f14fb602SLionel Sambuc */
1856f14fb602SLionel Sambuc if (nitems < tablemax) {
1857f14fb602SLionel Sambuc if (typetable != stattypetable)
1858f14fb602SLionel Sambuc free(typetable);
1859f14fb602SLionel Sambuc return -1;
1860f14fb602SLionel Sambuc }
1861f14fb602SLionel Sambuc /*
18622fe8fb19SBen Gras * Build the argument table.
18632fe8fb19SBen Gras */
18642fe8fb19SBen Gras if (tablemax >= STATIC_ARG_TBL_SIZE) {
1865f14fb602SLionel Sambuc *argtable = malloc(sizeof(**argtable) * (tablemax + 1));
1866f14fb602SLionel Sambuc if (*argtable == NULL) {
1867f14fb602SLionel Sambuc free(typetable);
18682fe8fb19SBen Gras return -1;
18692fe8fb19SBen Gras }
1870f14fb602SLionel Sambuc }
18712fe8fb19SBen Gras
18722fe8fb19SBen Gras (*argtable) [0].intarg = 0;
18732fe8fb19SBen Gras for (n = 1; n <= tablemax; n++) {
18742fe8fb19SBen Gras switch (typetable [n]) {
18752fe8fb19SBen Gras case T_UNUSED: /* whoops! */
18762fe8fb19SBen Gras (*argtable) [n].intarg = va_arg (ap, int);
18772fe8fb19SBen Gras break;
18782fe8fb19SBen Gras case TP_SCHAR:
18792fe8fb19SBen Gras (*argtable) [n].pschararg = va_arg (ap, signed char *);
18802fe8fb19SBen Gras break;
18812fe8fb19SBen Gras case TP_SHORT:
18822fe8fb19SBen Gras (*argtable) [n].pshortarg = va_arg (ap, short *);
18832fe8fb19SBen Gras break;
18842fe8fb19SBen Gras case T_INT:
18852fe8fb19SBen Gras (*argtable) [n].intarg = va_arg (ap, int);
18862fe8fb19SBen Gras break;
18872fe8fb19SBen Gras case T_U_INT:
18882fe8fb19SBen Gras (*argtable) [n].uintarg = va_arg (ap, unsigned int);
18892fe8fb19SBen Gras break;
18902fe8fb19SBen Gras case TP_INT:
18912fe8fb19SBen Gras (*argtable) [n].pintarg = va_arg (ap, int *);
18922fe8fb19SBen Gras break;
18932fe8fb19SBen Gras case T_LONG:
18942fe8fb19SBen Gras (*argtable) [n].longarg = va_arg (ap, long);
18952fe8fb19SBen Gras break;
18962fe8fb19SBen Gras case T_U_LONG:
18972fe8fb19SBen Gras (*argtable) [n].ulongarg = va_arg (ap, unsigned long);
18982fe8fb19SBen Gras break;
18992fe8fb19SBen Gras case TP_LONG:
19002fe8fb19SBen Gras (*argtable) [n].plongarg = va_arg (ap, long *);
19012fe8fb19SBen Gras break;
19022fe8fb19SBen Gras case T_LLONG:
19032fe8fb19SBen Gras (*argtable) [n].longlongarg = va_arg (ap, long long);
19042fe8fb19SBen Gras break;
19052fe8fb19SBen Gras case T_U_LLONG:
19062fe8fb19SBen Gras (*argtable) [n].ulonglongarg = va_arg (ap, unsigned long long);
19072fe8fb19SBen Gras break;
19082fe8fb19SBen Gras case TP_LLONG:
19092fe8fb19SBen Gras (*argtable) [n].plonglongarg = va_arg (ap, long long *);
19102fe8fb19SBen Gras break;
19112fe8fb19SBen Gras case T_PTRDIFFT:
19122fe8fb19SBen Gras (*argtable) [n].ptrdiffarg = va_arg (ap, ptrdiff_t);
19132fe8fb19SBen Gras break;
19142fe8fb19SBen Gras case TP_PTRDIFFT:
19152fe8fb19SBen Gras (*argtable) [n].pptrdiffarg = va_arg (ap, ptrdiff_t *);
19162fe8fb19SBen Gras break;
19172fe8fb19SBen Gras case T_SSIZET:
19182fe8fb19SBen Gras (*argtable) [n].ssizearg = va_arg (ap, ssize_t);
19192fe8fb19SBen Gras break;
19202fe8fb19SBen Gras case T_SIZET:
19212fe8fb19SBen Gras (*argtable) [n].sizearg = va_arg (ap, size_t);
19222fe8fb19SBen Gras break;
19232fe8fb19SBen Gras case TP_SIZET:
19242fe8fb19SBen Gras (*argtable) [n].psizearg = va_arg (ap, size_t *);
19252fe8fb19SBen Gras break;
19262fe8fb19SBen Gras case T_INTMAXT:
19272fe8fb19SBen Gras (*argtable) [n].intmaxarg = va_arg (ap, intmax_t);
19282fe8fb19SBen Gras break;
19292fe8fb19SBen Gras case T_UINTMAXT:
19302fe8fb19SBen Gras (*argtable) [n].uintmaxarg = va_arg (ap, uintmax_t);
19312fe8fb19SBen Gras break;
19322fe8fb19SBen Gras case TP_INTMAXT:
19332fe8fb19SBen Gras (*argtable) [n].pintmaxarg = va_arg (ap, intmax_t *);
19342fe8fb19SBen Gras break;
19352fe8fb19SBen Gras case T_DOUBLE:
19362fe8fb19SBen Gras #ifndef NO_FLOATING_POINT
19372fe8fb19SBen Gras (*argtable) [n].doublearg = va_arg (ap, double);
19382fe8fb19SBen Gras #endif
19392fe8fb19SBen Gras break;
19402fe8fb19SBen Gras case T_LONG_DOUBLE:
19412fe8fb19SBen Gras #ifndef NO_FLOATING_POINT
19422fe8fb19SBen Gras (*argtable) [n].longdoublearg = va_arg (ap, long double);
19432fe8fb19SBen Gras #endif
19442fe8fb19SBen Gras break;
19452fe8fb19SBen Gras case TP_CHAR:
19462fe8fb19SBen Gras (*argtable) [n].pchararg = va_arg (ap, char *);
19472fe8fb19SBen Gras break;
19482fe8fb19SBen Gras case TP_VOID:
19492fe8fb19SBen Gras (*argtable) [n].pvoidarg = va_arg (ap, void *);
19502fe8fb19SBen Gras break;
19512fe8fb19SBen Gras case T_WINT:
19522fe8fb19SBen Gras (*argtable) [n].wintarg = va_arg (ap, wint_t);
19532fe8fb19SBen Gras break;
19542fe8fb19SBen Gras case TP_WCHAR:
19552fe8fb19SBen Gras (*argtable) [n].pwchararg = va_arg (ap, wchar_t *);
19562fe8fb19SBen Gras break;
19572fe8fb19SBen Gras }
19582fe8fb19SBen Gras }
19592fe8fb19SBen Gras
1960f14fb602SLionel Sambuc if (typetable != stattypetable)
19612fe8fb19SBen Gras free (typetable);
19622fe8fb19SBen Gras return 0;
19632fe8fb19SBen Gras }
19642fe8fb19SBen Gras
19652fe8fb19SBen Gras /*
19662fe8fb19SBen Gras * Increase the size of the type table.
19672fe8fb19SBen Gras */
19682fe8fb19SBen Gras static int
__grow_type_table(size_t nextarg,enum typeid ** typetable,size_t * tablesize)1969f14fb602SLionel Sambuc __grow_type_table (size_t nextarg, enum typeid **typetable, size_t *tablesize)
19702fe8fb19SBen Gras {
19712fe8fb19SBen Gras enum typeid *const oldtable = *typetable;
1972f14fb602SLionel Sambuc const size_t oldsize = *tablesize;
19732fe8fb19SBen Gras enum typeid *newtable;
1974f14fb602SLionel Sambuc size_t newsize = oldsize * 2;
19752fe8fb19SBen Gras
19762fe8fb19SBen Gras if (newsize < nextarg + 1)
19772fe8fb19SBen Gras newsize = nextarg + 1;
19782fe8fb19SBen Gras if (oldsize == STATIC_ARG_TBL_SIZE) {
1979f14fb602SLionel Sambuc if ((newtable = malloc(newsize * sizeof(*newtable))) == NULL)
19802fe8fb19SBen Gras return -1;
1981f14fb602SLionel Sambuc memcpy(newtable, oldtable, oldsize * sizeof(*newtable));
19822fe8fb19SBen Gras } else {
1983f14fb602SLionel Sambuc newtable = realloc(oldtable, newsize * sizeof(*newtable));
19842fe8fb19SBen Gras if (newtable == NULL) {
19852fe8fb19SBen Gras free(oldtable);
19862fe8fb19SBen Gras return -1;
19872fe8fb19SBen Gras }
19882fe8fb19SBen Gras }
1989f14fb602SLionel Sambuc memset(&newtable[oldsize], 0, (newsize - oldsize) * sizeof(*newtable));
19902fe8fb19SBen Gras
19912fe8fb19SBen Gras *typetable = newtable;
19922fe8fb19SBen Gras *tablesize = newsize;
19932fe8fb19SBen Gras return 0;
19942fe8fb19SBen Gras }
19952fe8fb19SBen Gras
19962fe8fb19SBen Gras
19972fe8fb19SBen Gras #ifndef NO_FLOATING_POINT
19982fe8fb19SBen Gras #ifndef WIDE_DOUBLE
19992fe8fb19SBen Gras static char *
cvt(double value,int ndigits,int flags,char * sign,int * decpt,int ch,int * length)20002fe8fb19SBen Gras cvt(double value, int ndigits, int flags, char *sign, int *decpt, int ch,
20012fe8fb19SBen Gras int *length)
20022fe8fb19SBen Gras {
20032fe8fb19SBen Gras int mode, dsgn;
20042fe8fb19SBen Gras char *digits, *bp, *rve;
20052fe8fb19SBen Gras
20062fe8fb19SBen Gras _DIAGASSERT(decpt != NULL);
20072fe8fb19SBen Gras _DIAGASSERT(length != NULL);
20082fe8fb19SBen Gras _DIAGASSERT(sign != NULL);
20092fe8fb19SBen Gras
20102fe8fb19SBen Gras if (ch == 'f') {
20112fe8fb19SBen Gras mode = 3; /* ndigits after the decimal point */
20122fe8fb19SBen Gras } else {
20132fe8fb19SBen Gras /* To obtain ndigits after the decimal point for the 'e'
20142fe8fb19SBen Gras * and 'E' formats, round to ndigits + 1 significant
20152fe8fb19SBen Gras * figures.
20162fe8fb19SBen Gras */
20172fe8fb19SBen Gras if (ch == 'e' || ch == 'E') {
20182fe8fb19SBen Gras ndigits++;
20192fe8fb19SBen Gras }
20202fe8fb19SBen Gras mode = 2; /* ndigits significant digits */
20212fe8fb19SBen Gras }
20222fe8fb19SBen Gras
20232fe8fb19SBen Gras digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve);
20242fe8fb19SBen Gras if (digits == NULL)
20252fe8fb19SBen Gras return NULL;
20262fe8fb19SBen Gras if (dsgn) {
20272fe8fb19SBen Gras value = -value;
20282fe8fb19SBen Gras *sign = '-';
20292fe8fb19SBen Gras } else
20302fe8fb19SBen Gras *sign = '\000';
20312fe8fb19SBen Gras if ((ch != 'g' && ch != 'G') || flags & ALT) { /* Print trailing zeros */
20322fe8fb19SBen Gras bp = digits + ndigits;
20332fe8fb19SBen Gras if (ch == 'f') {
20342fe8fb19SBen Gras if (*digits == '0' && value)
20352fe8fb19SBen Gras *decpt = -ndigits + 1;
20362fe8fb19SBen Gras bp += *decpt;
20372fe8fb19SBen Gras }
20382fe8fb19SBen Gras if (value == 0) /* kludge for __dtoa irregularity */
20392fe8fb19SBen Gras rve = bp;
20402fe8fb19SBen Gras while (rve < bp)
20412fe8fb19SBen Gras *rve++ = '0';
20422fe8fb19SBen Gras }
20432fe8fb19SBen Gras *length = rve - digits;
20442fe8fb19SBen Gras return digits;
20452fe8fb19SBen Gras }
20462fe8fb19SBen Gras #endif
20472fe8fb19SBen Gras
20482fe8fb19SBen Gras static int
exponent(CHAR_T * p0,int expo,int fmtch)20492fe8fb19SBen Gras exponent(CHAR_T *p0, int expo, int fmtch)
20502fe8fb19SBen Gras {
20512fe8fb19SBen Gras CHAR_T *p, *t;
20522fe8fb19SBen Gras CHAR_T expbuf[MAXEXPDIG];
20532fe8fb19SBen Gras
20542fe8fb19SBen Gras p = p0;
20552fe8fb19SBen Gras *p++ = fmtch;
20562fe8fb19SBen Gras if (expo < 0) {
20572fe8fb19SBen Gras expo = -expo;
20582fe8fb19SBen Gras *p++ = '-';
20592fe8fb19SBen Gras }
20602fe8fb19SBen Gras else
20612fe8fb19SBen Gras *p++ = '+';
20622fe8fb19SBen Gras t = expbuf + MAXEXPDIG;
20632fe8fb19SBen Gras if (expo > 9) {
20642fe8fb19SBen Gras do {
20652fe8fb19SBen Gras *--t = to_char(expo % 10);
20662fe8fb19SBen Gras } while ((expo /= 10) > 9);
20672fe8fb19SBen Gras *--t = to_char(expo);
20682fe8fb19SBen Gras for (; t < expbuf + MAXEXPDIG; *p++ = *t++);
20692fe8fb19SBen Gras }
20702fe8fb19SBen Gras else {
20712fe8fb19SBen Gras /*
20722fe8fb19SBen Gras * Exponents for decimal floating point conversions
20732fe8fb19SBen Gras * (%[eEgG]) must be at least two characters long,
20742fe8fb19SBen Gras * whereas exponents for hexadecimal conversions can
20752fe8fb19SBen Gras * be only one character long.
20762fe8fb19SBen Gras */
20772fe8fb19SBen Gras if (fmtch == 'e' || fmtch == 'E')
20782fe8fb19SBen Gras *p++ = '0';
20792fe8fb19SBen Gras *p++ = to_char(expo);
20802fe8fb19SBen Gras }
2081f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(int, p - p0));
2082f14fb602SLionel Sambuc return (int)(p - p0);
20832fe8fb19SBen Gras }
20842fe8fb19SBen Gras #endif /* !NO_FLOATING_POINT */
2085