158f0484fSRodney W. Grimes /*- 258f0484fSRodney W. Grimes * Copyright (c) 1990, 1993 358f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 458f0484fSRodney W. Grimes * 558f0484fSRodney W. Grimes * This code is derived from software contributed to Berkeley by 658f0484fSRodney W. Grimes * Chris Torek. 758f0484fSRodney W. Grimes * 858f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 958f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 1058f0484fSRodney W. Grimes * are met: 1158f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 1258f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1358f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1458f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1558f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1658f0484fSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 1758f0484fSRodney W. Grimes * must display the following acknowledgement: 1858f0484fSRodney W. Grimes * This product includes software developed by the University of 1958f0484fSRodney W. Grimes * California, Berkeley and its contributors. 2058f0484fSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 2158f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 2258f0484fSRodney W. Grimes * without specific prior written permission. 2358f0484fSRodney W. Grimes * 2458f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2558f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2658f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2758f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2858f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2958f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3058f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3158f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3258f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3358f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3458f0484fSRodney W. Grimes * SUCH DAMAGE. 3558f0484fSRodney W. Grimes */ 3658f0484fSRodney W. Grimes 3758f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 3858f0484fSRodney W. Grimes static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93"; 3958f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 40333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 41333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 4258f0484fSRodney W. Grimes 4358f0484fSRodney W. Grimes /* 4458f0484fSRodney W. Grimes * Actual printf innards. 4558f0484fSRodney W. Grimes * 4658f0484fSRodney W. Grimes * This code is large and complicated... 4758f0484fSRodney W. Grimes */ 4858f0484fSRodney W. Grimes 49d201fe46SDaniel Eischen #include "namespace.h" 5058f0484fSRodney W. Grimes #include <sys/types.h> 5158f0484fSRodney W. Grimes 527735bb0fSBill Fenner #include <ctype.h> 5358f0484fSRodney W. Grimes #include <limits.h> 547ae5c679SAlexey Zelkin #include <locale.h> 557735bb0fSBill Fenner #include <stddef.h> 567735bb0fSBill Fenner #include <stdint.h> 5758f0484fSRodney W. Grimes #include <stdio.h> 5858f0484fSRodney W. Grimes #include <stdlib.h> 5958f0484fSRodney W. Grimes #include <string.h> 60b9aac308STim J. Robbins #include <wchar.h> 6158f0484fSRodney W. Grimes 6258f0484fSRodney W. Grimes #include <stdarg.h> 63d201fe46SDaniel Eischen #include "un-namespace.h" 6458f0484fSRodney W. Grimes 65d201fe46SDaniel Eischen #include "libc_private.h" 6658f0484fSRodney W. Grimes #include "local.h" 6758f0484fSRodney W. Grimes #include "fvwrite.h" 6858f0484fSRodney W. Grimes 6958f0484fSRodney W. Grimes /* Define FLOATING_POINT to get floating point. */ 7058f0484fSRodney W. Grimes #define FLOATING_POINT 7158f0484fSRodney W. Grimes 72a387081cSDoug Rabson union arg { 73a387081cSDoug Rabson int intarg; 7418ca70d1SBruce Evans u_int uintarg; 75a387081cSDoug Rabson long longarg; 7618ca70d1SBruce Evans u_long ulongarg; 777735bb0fSBill Fenner long long longlongarg; 787735bb0fSBill Fenner unsigned long long ulonglongarg; 797735bb0fSBill Fenner ptrdiff_t ptrdiffarg; 807735bb0fSBill Fenner size_t sizearg; 817735bb0fSBill Fenner intmax_t intmaxarg; 827735bb0fSBill Fenner uintmax_t uintmaxarg; 83a387081cSDoug Rabson void *pvoidarg; 84a387081cSDoug Rabson char *pchararg; 857735bb0fSBill Fenner signed char *pschararg; 86a387081cSDoug Rabson short *pshortarg; 87a387081cSDoug Rabson int *pintarg; 88a387081cSDoug Rabson long *plongarg; 897735bb0fSBill Fenner long long *plonglongarg; 907735bb0fSBill Fenner ptrdiff_t *pptrdiffarg; 917735bb0fSBill Fenner size_t *psizearg; 927735bb0fSBill Fenner intmax_t *pintmaxarg; 93a387081cSDoug Rabson #ifdef FLOATING_POINT 94a387081cSDoug Rabson double doublearg; 95a387081cSDoug Rabson long double longdoublearg; 96a387081cSDoug Rabson #endif 97b9aac308STim J. Robbins wint_t wintarg; 98b9aac308STim J. Robbins wchar_t *pwchararg; 99a387081cSDoug Rabson }; 100a387081cSDoug Rabson 1017735bb0fSBill Fenner /* 1027735bb0fSBill Fenner * Type ids for argument type table. 1037735bb0fSBill Fenner */ 1047735bb0fSBill Fenner enum typeid { 1057735bb0fSBill Fenner T_UNUSED, TP_SHORT, T_INT, T_U_INT, TP_INT, 1067735bb0fSBill Fenner T_LONG, T_U_LONG, TP_LONG, T_LLONG, T_U_LLONG, TP_LLONG, 1077735bb0fSBill Fenner T_PTRDIFFT, TP_PTRDIFFT, T_SIZET, TP_SIZET, 1087735bb0fSBill Fenner T_INTMAXT, T_UINTMAXT, TP_INTMAXT, TP_VOID, TP_CHAR, TP_SCHAR, 109b9aac308STim J. Robbins T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR 1107735bb0fSBill Fenner }; 1117735bb0fSBill Fenner 112c05ac53bSDavid E. O'Brien static int __sprint(FILE *, struct __suio *); 1131372519bSDavid E. O'Brien static int __sbprintf(FILE *, const char *, va_list) __printflike(2, 0); 1141372519bSDavid E. O'Brien static char *__ujtoa(uintmax_t, char *, int, int, char *, int, char, 1151372519bSDavid E. O'Brien const char *); 1161372519bSDavid E. O'Brien static char *__ultoa(u_long, char *, int, int, char *, int, char, 1171372519bSDavid E. O'Brien const char *); 118b9aac308STim J. Robbins static char *__wcsconv(wchar_t *, int); 119c05ac53bSDavid E. O'Brien static void __find_arguments(const char *, va_list, union arg **); 120c05ac53bSDavid E. O'Brien static void __grow_type_table(int, enum typeid **, int *); 121ce51cf03SJames Raynard 12258f0484fSRodney W. Grimes /* 12358f0484fSRodney W. Grimes * Flush out all the vectors defined by the given uio, 12458f0484fSRodney W. Grimes * then reset it so that it can be reused. 12558f0484fSRodney W. Grimes */ 12658f0484fSRodney W. Grimes static int 127d201fe46SDaniel Eischen __sprint(FILE *fp, struct __suio *uio) 12858f0484fSRodney W. Grimes { 129d201fe46SDaniel Eischen int err; 13058f0484fSRodney W. Grimes 13158f0484fSRodney W. Grimes if (uio->uio_resid == 0) { 13258f0484fSRodney W. Grimes uio->uio_iovcnt = 0; 13358f0484fSRodney W. Grimes return (0); 13458f0484fSRodney W. Grimes } 13558f0484fSRodney W. Grimes err = __sfvwrite(fp, uio); 13658f0484fSRodney W. Grimes uio->uio_resid = 0; 13758f0484fSRodney W. Grimes uio->uio_iovcnt = 0; 13858f0484fSRodney W. Grimes return (err); 13958f0484fSRodney W. Grimes } 14058f0484fSRodney W. Grimes 14158f0484fSRodney W. Grimes /* 14258f0484fSRodney W. Grimes * Helper function for `fprintf to unbuffered unix file': creates a 14358f0484fSRodney W. Grimes * temporary buffer. We only work on write-only files; this avoids 14458f0484fSRodney W. Grimes * worries about ungetc buffers and so forth. 14558f0484fSRodney W. Grimes */ 14658f0484fSRodney W. Grimes static int 147d201fe46SDaniel Eischen __sbprintf(FILE *fp, const char *fmt, va_list ap) 14858f0484fSRodney W. Grimes { 14958f0484fSRodney W. Grimes int ret; 15058f0484fSRodney W. Grimes FILE fake; 15158f0484fSRodney W. Grimes unsigned char buf[BUFSIZ]; 15258f0484fSRodney W. Grimes 15358f0484fSRodney W. Grimes /* copy the important variables */ 15458f0484fSRodney W. Grimes fake._flags = fp->_flags & ~__SNBF; 15558f0484fSRodney W. Grimes fake._file = fp->_file; 15658f0484fSRodney W. Grimes fake._cookie = fp->_cookie; 15758f0484fSRodney W. Grimes fake._write = fp->_write; 158e74101e4STim J. Robbins fake._extra = fp->_extra; 15958f0484fSRodney W. Grimes 16058f0484fSRodney W. Grimes /* set up the buffer */ 16158f0484fSRodney W. Grimes fake._bf._base = fake._p = buf; 16258f0484fSRodney W. Grimes fake._bf._size = fake._w = sizeof(buf); 16358f0484fSRodney W. Grimes fake._lbfsize = 0; /* not actually used, but Just In Case */ 16458f0484fSRodney W. Grimes 16558f0484fSRodney W. Grimes /* do the work, then copy any error status */ 166d201fe46SDaniel Eischen ret = __vfprintf(&fake, fmt, ap); 167d201fe46SDaniel Eischen if (ret >= 0 && __fflush(&fake)) 16858f0484fSRodney W. Grimes ret = EOF; 16958f0484fSRodney W. Grimes if (fake._flags & __SERR) 17058f0484fSRodney W. Grimes fp->_flags |= __SERR; 17158f0484fSRodney W. Grimes return (ret); 17258f0484fSRodney W. Grimes } 17358f0484fSRodney W. Grimes 17458f0484fSRodney W. Grimes /* 17558f0484fSRodney W. Grimes * Macros for converting digits to letters and vice versa 17658f0484fSRodney W. Grimes */ 17758f0484fSRodney W. Grimes #define to_digit(c) ((c) - '0') 17858f0484fSRodney W. Grimes #define is_digit(c) ((unsigned)to_digit(c) <= 9) 17958f0484fSRodney W. Grimes #define to_char(n) ((n) + '0') 18058f0484fSRodney W. Grimes 18158f0484fSRodney W. Grimes /* 18258f0484fSRodney W. Grimes * Convert an unsigned long to ASCII for printf purposes, returning 18358f0484fSRodney W. Grimes * a pointer to the first character of the string representation. 18458f0484fSRodney W. Grimes * Octal numbers can be forced to have a leading zero; hex numbers 18558f0484fSRodney W. Grimes * use the given digits. 18658f0484fSRodney W. Grimes */ 18758f0484fSRodney W. Grimes static char * 1887735bb0fSBill Fenner __ultoa(u_long val, char *endp, int base, int octzero, char *xdigs, 18998ee7635SAlexey Zelkin int needgrp, char thousep, const char *grp) 19058f0484fSRodney W. Grimes { 1918fb3f3f6SDavid E. O'Brien char *cp = endp; 1928fb3f3f6SDavid E. O'Brien long sval; 1937735bb0fSBill Fenner int ndig; 19458f0484fSRodney W. Grimes 19558f0484fSRodney W. Grimes /* 19658f0484fSRodney W. Grimes * Handle the three cases separately, in the hope of getting 19758f0484fSRodney W. Grimes * better/faster code. 19858f0484fSRodney W. Grimes */ 19958f0484fSRodney W. Grimes switch (base) { 20058f0484fSRodney W. Grimes case 10: 20158f0484fSRodney W. Grimes if (val < 10) { /* many numbers are 1 digit */ 20258f0484fSRodney W. Grimes *--cp = to_char(val); 20358f0484fSRodney W. Grimes return (cp); 20458f0484fSRodney W. Grimes } 2057735bb0fSBill Fenner ndig = 0; 20658f0484fSRodney W. Grimes /* 20758f0484fSRodney W. Grimes * On many machines, unsigned arithmetic is harder than 20858f0484fSRodney W. Grimes * signed arithmetic, so we do at most one unsigned mod and 20958f0484fSRodney W. Grimes * divide; this is sufficient to reduce the range of 21058f0484fSRodney W. Grimes * the incoming value to where signed arithmetic works. 21158f0484fSRodney W. Grimes */ 21258f0484fSRodney W. Grimes if (val > LONG_MAX) { 21358f0484fSRodney W. Grimes *--cp = to_char(val % 10); 2147735bb0fSBill Fenner ndig++; 21558f0484fSRodney W. Grimes sval = val / 10; 21658f0484fSRodney W. Grimes } else 21758f0484fSRodney W. Grimes sval = val; 21858f0484fSRodney W. Grimes do { 21958f0484fSRodney W. Grimes *--cp = to_char(sval % 10); 22098ee7635SAlexey Zelkin ndig++; 22198ee7635SAlexey Zelkin /* 22298ee7635SAlexey Zelkin * If (*grp == CHAR_MAX) then no more grouping 22398ee7635SAlexey Zelkin * should be performed. 22498ee7635SAlexey Zelkin */ 225243e90d6SAlexey Zelkin if (needgrp && ndig == *grp && *grp != CHAR_MAX 226243e90d6SAlexey Zelkin && sval > 9) { 22798ee7635SAlexey Zelkin *--cp = thousep; 2287735bb0fSBill Fenner ndig = 0; 22998ee7635SAlexey Zelkin /* 23098ee7635SAlexey Zelkin * If (*(grp+1) == '\0') then we have to 23198ee7635SAlexey Zelkin * use *grp character (last grouping rule) 23298ee7635SAlexey Zelkin * for all next cases 23398ee7635SAlexey Zelkin */ 2342e394b2fSAlexey Zelkin if (*(grp+1) != '\0') 2352e394b2fSAlexey Zelkin grp++; 2367735bb0fSBill Fenner } 23758f0484fSRodney W. Grimes sval /= 10; 23858f0484fSRodney W. Grimes } while (sval != 0); 23958f0484fSRodney W. Grimes break; 24058f0484fSRodney W. Grimes 24158f0484fSRodney W. Grimes case 8: 24258f0484fSRodney W. Grimes do { 24358f0484fSRodney W. Grimes *--cp = to_char(val & 7); 24458f0484fSRodney W. Grimes val >>= 3; 24558f0484fSRodney W. Grimes } while (val); 24658f0484fSRodney W. Grimes if (octzero && *cp != '0') 24758f0484fSRodney W. Grimes *--cp = '0'; 24858f0484fSRodney W. Grimes break; 24958f0484fSRodney W. Grimes 25058f0484fSRodney W. Grimes case 16: 25158f0484fSRodney W. Grimes do { 25258f0484fSRodney W. Grimes *--cp = xdigs[val & 15]; 25358f0484fSRodney W. Grimes val >>= 4; 25458f0484fSRodney W. Grimes } while (val); 25558f0484fSRodney W. Grimes break; 25658f0484fSRodney W. Grimes 25758f0484fSRodney W. Grimes default: /* oops */ 25858f0484fSRodney W. Grimes abort(); 25958f0484fSRodney W. Grimes } 26058f0484fSRodney W. Grimes return (cp); 26158f0484fSRodney W. Grimes } 26258f0484fSRodney W. Grimes 2637735bb0fSBill Fenner /* Identical to __ultoa, but for intmax_t. */ 26458f0484fSRodney W. Grimes static char * 2657735bb0fSBill Fenner __ujtoa(uintmax_t val, char *endp, int base, int octzero, char *xdigs, 26698ee7635SAlexey Zelkin int needgrp, char thousep, const char *grp) 26758f0484fSRodney W. Grimes { 268d201fe46SDaniel Eischen char *cp = endp; 2697735bb0fSBill Fenner intmax_t sval; 2707735bb0fSBill Fenner int ndig; 27158f0484fSRodney W. Grimes 27258f0484fSRodney W. Grimes /* quick test for small values; __ultoa is typically much faster */ 27358f0484fSRodney W. Grimes /* (perhaps instead we should run until small, then call __ultoa?) */ 27458f0484fSRodney W. Grimes if (val <= ULONG_MAX) 2757735bb0fSBill Fenner return (__ultoa((u_long)val, endp, base, octzero, xdigs, 27698ee7635SAlexey Zelkin needgrp, thousep, grp)); 27758f0484fSRodney W. Grimes switch (base) { 27858f0484fSRodney W. Grimes case 10: 27958f0484fSRodney W. Grimes if (val < 10) { 28058f0484fSRodney W. Grimes *--cp = to_char(val % 10); 28158f0484fSRodney W. Grimes return (cp); 28258f0484fSRodney W. Grimes } 2837735bb0fSBill Fenner ndig = 0; 2847735bb0fSBill Fenner if (val > INTMAX_MAX) { 28558f0484fSRodney W. Grimes *--cp = to_char(val % 10); 2867735bb0fSBill Fenner ndig++; 28758f0484fSRodney W. Grimes sval = val / 10; 28858f0484fSRodney W. Grimes } else 28958f0484fSRodney W. Grimes sval = val; 29058f0484fSRodney W. Grimes do { 29158f0484fSRodney W. Grimes *--cp = to_char(sval % 10); 29298ee7635SAlexey Zelkin ndig++; 29398ee7635SAlexey Zelkin /* 29498ee7635SAlexey Zelkin * If (*grp == CHAR_MAX) then no more grouping 29598ee7635SAlexey Zelkin * should be performed. 29698ee7635SAlexey Zelkin */ 297243e90d6SAlexey Zelkin if (needgrp && *grp != CHAR_MAX && ndig == *grp 298243e90d6SAlexey Zelkin && sval > 9) { 29998ee7635SAlexey Zelkin *--cp = thousep; 3007735bb0fSBill Fenner ndig = 0; 30198ee7635SAlexey Zelkin /* 30298ee7635SAlexey Zelkin * If (*(grp+1) == '\0') then we have to 30398ee7635SAlexey Zelkin * use *grp character (last grouping rule) 30498ee7635SAlexey Zelkin * for all next cases 30598ee7635SAlexey Zelkin */ 3062e394b2fSAlexey Zelkin if (*(grp+1) != '\0') 3072e394b2fSAlexey Zelkin grp++; 3087735bb0fSBill Fenner } 30958f0484fSRodney W. Grimes sval /= 10; 31058f0484fSRodney W. Grimes } while (sval != 0); 31158f0484fSRodney W. Grimes break; 31258f0484fSRodney W. Grimes 31358f0484fSRodney W. Grimes case 8: 31458f0484fSRodney W. Grimes do { 31558f0484fSRodney W. Grimes *--cp = to_char(val & 7); 31658f0484fSRodney W. Grimes val >>= 3; 31758f0484fSRodney W. Grimes } while (val); 31858f0484fSRodney W. Grimes if (octzero && *cp != '0') 31958f0484fSRodney W. Grimes *--cp = '0'; 32058f0484fSRodney W. Grimes break; 32158f0484fSRodney W. Grimes 32258f0484fSRodney W. Grimes case 16: 32358f0484fSRodney W. Grimes do { 32458f0484fSRodney W. Grimes *--cp = xdigs[val & 15]; 32558f0484fSRodney W. Grimes val >>= 4; 32658f0484fSRodney W. Grimes } while (val); 32758f0484fSRodney W. Grimes break; 32858f0484fSRodney W. Grimes 32958f0484fSRodney W. Grimes default: 33058f0484fSRodney W. Grimes abort(); 33158f0484fSRodney W. Grimes } 33258f0484fSRodney W. Grimes return (cp); 33358f0484fSRodney W. Grimes } 33458f0484fSRodney W. Grimes 335d201fe46SDaniel Eischen /* 336b9aac308STim J. Robbins * Convert a wide character string argument for the %ls format to a multibyte 337b9aac308STim J. Robbins * string representation. ``prec'' specifies the maximum number of bytes 338b9aac308STim J. Robbins * to output. If ``prec'' is greater than or equal to zero, we can't assume 339b9aac308STim J. Robbins * that the wide char. string ends in a null character. 340b9aac308STim J. Robbins */ 341b9aac308STim J. Robbins static char * 342b9aac308STim J. Robbins __wcsconv(wchar_t *wcsarg, int prec) 343b9aac308STim J. Robbins { 344b9aac308STim J. Robbins char buf[MB_LEN_MAX]; 345b9aac308STim J. Robbins wchar_t *p; 346b9aac308STim J. Robbins char *convbuf, *mbp; 347b9aac308STim J. Robbins size_t clen, nbytes; 348b9aac308STim J. Robbins mbstate_t mbs; 349b9aac308STim J. Robbins 350b9aac308STim J. Robbins /* 351b9aac308STim J. Robbins * Determine the number of bytes to output and allocate space for 352b9aac308STim J. Robbins * the output. 353b9aac308STim J. Robbins */ 354b9aac308STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 355b9aac308STim J. Robbins if (prec >= 0) { 356b9aac308STim J. Robbins nbytes = 0; 357b9aac308STim J. Robbins p = wcsarg; 358b9aac308STim J. Robbins for (;;) { 359b9aac308STim J. Robbins clen = wcrtomb(buf, *p++, &mbs); 360b9aac308STim J. Robbins if (clen == 0 || clen == (size_t)-1 || 361b9aac308STim J. Robbins nbytes + clen > prec) 362b9aac308STim J. Robbins break; 363b9aac308STim J. Robbins nbytes += clen; 364b9aac308STim J. Robbins } 365b9aac308STim J. Robbins } else { 366b9aac308STim J. Robbins p = wcsarg; 367b9aac308STim J. Robbins nbytes = wcsrtombs(NULL, (const wchar_t **)&p, 0, &mbs); 368b9aac308STim J. Robbins if (nbytes == (size_t)-1) 369b9aac308STim J. Robbins return (NULL); 370b9aac308STim J. Robbins } 371b9aac308STim J. Robbins if ((convbuf = malloc(nbytes + 1)) == NULL) 372b9aac308STim J. Robbins return (NULL); 373b9aac308STim J. Robbins 374b9aac308STim J. Robbins /* 375b9aac308STim J. Robbins * Fill the output buffer with the multibyte representations of as 376b9aac308STim J. Robbins * many wide characters as will fit. 377b9aac308STim J. Robbins */ 378b9aac308STim J. Robbins mbp = convbuf; 379b9aac308STim J. Robbins p = wcsarg; 380b9aac308STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 381b9aac308STim J. Robbins while (mbp - convbuf < nbytes) { 382b9aac308STim J. Robbins clen = wcrtomb(mbp, *p++, &mbs); 383b9aac308STim J. Robbins if (clen == 0 || clen == (size_t)-1) 384b9aac308STim J. Robbins break; 385b9aac308STim J. Robbins mbp += clen; 386b9aac308STim J. Robbins } 387b9aac308STim J. Robbins *mbp = '\0'; 388b9aac308STim J. Robbins if (clen == (size_t)-1) 389b9aac308STim J. Robbins return (NULL); 390b9aac308STim J. Robbins 391b9aac308STim J. Robbins return (convbuf); 392b9aac308STim J. Robbins } 393b9aac308STim J. Robbins 394b9aac308STim J. Robbins /* 395d201fe46SDaniel Eischen * MT-safe version 396d201fe46SDaniel Eischen */ 397d201fe46SDaniel Eischen int 398f8418db7SRobert Drehmel vfprintf(FILE * __restrict fp, const char * __restrict fmt0, va_list ap) 399f8418db7SRobert Drehmel 400d201fe46SDaniel Eischen { 401d201fe46SDaniel Eischen int ret; 402d201fe46SDaniel Eischen 403d201fe46SDaniel Eischen FLOCKFILE(fp); 404d201fe46SDaniel Eischen ret = __vfprintf(fp, fmt0, ap); 405d201fe46SDaniel Eischen FUNLOCKFILE(fp); 406d201fe46SDaniel Eischen return (ret); 407d201fe46SDaniel Eischen } 408d201fe46SDaniel Eischen 40958f0484fSRodney W. Grimes #ifdef FLOATING_POINT 41058f0484fSRodney W. Grimes #include <math.h> 41158f0484fSRodney W. Grimes #include "floatio.h" 41258f0484fSRodney W. Grimes 41398ee7635SAlexey Zelkin #define BUF ((MAXEXP*2)+MAXFRACT+1) /* + decimal point */ 41458f0484fSRodney W. Grimes #define DEFPREC 6 41558f0484fSRodney W. Grimes 416c05ac53bSDavid E. O'Brien static char *cvt(double, int, int, char *, int *, int, int *, char **); 417c05ac53bSDavid E. O'Brien static int exponent(char *, int, int); 41858f0484fSRodney W. Grimes 41958f0484fSRodney W. Grimes #else /* no FLOATING_POINT */ 42058f0484fSRodney W. Grimes 42198ee7635SAlexey Zelkin #define BUF 136 42258f0484fSRodney W. Grimes 42358f0484fSRodney W. Grimes #endif /* FLOATING_POINT */ 42458f0484fSRodney W. Grimes 425efb7e53dSJordan K. Hubbard #define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */ 42658f0484fSRodney W. Grimes 42758f0484fSRodney W. Grimes /* 42858f0484fSRodney W. Grimes * Flags used during conversion. 42958f0484fSRodney W. Grimes */ 43058f0484fSRodney W. Grimes #define ALT 0x001 /* alternate form */ 43158f0484fSRodney W. Grimes #define HEXPREFIX 0x002 /* add 0x or 0X prefix */ 43258f0484fSRodney W. Grimes #define LADJUST 0x004 /* left adjustment */ 4336a93659fSBruce Evans #define LONGDBL 0x008 /* long double */ 43458f0484fSRodney W. Grimes #define LONGINT 0x010 /* long integer */ 4357735bb0fSBill Fenner #define LLONGINT 0x020 /* long long integer */ 43658f0484fSRodney W. Grimes #define SHORTINT 0x040 /* short integer */ 43758f0484fSRodney W. Grimes #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */ 43858f0484fSRodney W. Grimes #define FPT 0x100 /* Floating point number */ 43998ee7635SAlexey Zelkin #define GROUPING 0x200 /* use grouping ("'" flag) */ 4407735bb0fSBill Fenner /* C99 additional size modifiers: */ 44198ee7635SAlexey Zelkin #define SIZET 0x400 /* size_t */ 44298ee7635SAlexey Zelkin #define PTRDIFFT 0x800 /* ptrdiff_t */ 44398ee7635SAlexey Zelkin #define INTMAXT 0x1000 /* intmax_t */ 44498ee7635SAlexey Zelkin #define CHARINT 0x2000 /* print char using int format */ 4457735bb0fSBill Fenner 446d201fe46SDaniel Eischen /* 447d201fe46SDaniel Eischen * Non-MT-safe version 448d201fe46SDaniel Eischen */ 44958f0484fSRodney W. Grimes int 450d201fe46SDaniel Eischen __vfprintf(FILE *fp, const char *fmt0, va_list ap) 45158f0484fSRodney W. Grimes { 452d201fe46SDaniel Eischen char *fmt; /* format string */ 453d201fe46SDaniel Eischen int ch; /* character from fmt */ 454d201fe46SDaniel Eischen int n, n2; /* handy integer (short term usage) */ 455d201fe46SDaniel Eischen char *cp; /* handy char pointer (short term usage) */ 456d201fe46SDaniel Eischen struct __siov *iovp; /* for PRINT macro */ 457d201fe46SDaniel Eischen int flags; /* flags as above */ 45858f0484fSRodney W. Grimes int ret; /* return value accumulator */ 45958f0484fSRodney W. Grimes int width; /* width from format (%8d), or 0 */ 46058f0484fSRodney W. Grimes int prec; /* precision from format (%.3d), or -1 */ 46158f0484fSRodney W. Grimes char sign; /* sign prefix (' ', '+', '-', or \0) */ 46298ee7635SAlexey Zelkin char thousands_sep; /* locale specific thousands separator */ 46398ee7635SAlexey Zelkin const char *grouping; /* locale specific numeric grouping rules */ 46458f0484fSRodney W. Grimes #ifdef FLOATING_POINT 4657ae5c679SAlexey Zelkin char *decimal_point; /* locale specific decimal point */ 46658f0484fSRodney W. Grimes char softsign; /* temporary negative sign for floats */ 46758f0484fSRodney W. Grimes double _double; /* double precision arguments %[eEfgG] */ 46858f0484fSRodney W. Grimes int expt; /* integer value of exponent */ 46958f0484fSRodney W. Grimes int expsize; /* character count for expstr */ 47058f0484fSRodney W. Grimes int ndig; /* actual number of digits returned by cvt */ 47158f0484fSRodney W. Grimes char expstr[7]; /* buffer for exponent string */ 4722ffc61baSTor Egge char *dtoaresult; /* buffer allocated by dtoa */ 47358f0484fSRodney W. Grimes #endif 47458f0484fSRodney W. Grimes u_long ulval; /* integer arguments %[diouxX] */ 4757735bb0fSBill Fenner uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */ 47658f0484fSRodney W. Grimes int base; /* base for [diouxX] conversion */ 47758f0484fSRodney W. Grimes int dprec; /* a copy of prec if [diouxX], 0 otherwise */ 478261a532aSBill Fenner int realsz; /* field size expanded by dprec, sign, etc */ 47958f0484fSRodney W. Grimes int size; /* size of converted field or string */ 48092e88f87SAndrey A. Chernov int prsize; /* max size of printed field */ 48158f0484fSRodney W. Grimes char *xdigs; /* digits for [xX] conversion */ 48258f0484fSRodney W. Grimes #define NIOV 8 48358f0484fSRodney W. Grimes struct __suio uio; /* output information: summary */ 48458f0484fSRodney W. Grimes struct __siov iov[NIOV];/* ... and individual io vectors */ 4857735bb0fSBill Fenner char buf[BUF]; /* space for %c, %[diouxX], %[eEfFgG] */ 48658f0484fSRodney W. Grimes char ox[2]; /* space for 0x hex-prefix */ 487a387081cSDoug Rabson union arg *argtable; /* args, built due to positional arg */ 488a387081cSDoug Rabson union arg statargtable [STATIC_ARG_TBL_SIZE]; 489efb7e53dSJordan K. Hubbard int nextarg; /* 1-based argument index */ 490efb7e53dSJordan K. Hubbard va_list orgap; /* original argument pointer */ 491b9aac308STim J. Robbins char *convbuf; /* wide to multibyte conversion result */ 49258f0484fSRodney W. Grimes 49358f0484fSRodney W. Grimes /* 49458f0484fSRodney W. Grimes * Choose PADSIZE to trade efficiency vs. size. If larger printf 49558f0484fSRodney W. Grimes * fields occur frequently, increase PADSIZE and make the initialisers 49658f0484fSRodney W. Grimes * below longer. 49758f0484fSRodney W. Grimes */ 49858f0484fSRodney W. Grimes #define PADSIZE 16 /* pad chunk size */ 49958f0484fSRodney W. Grimes static char blanks[PADSIZE] = 50058f0484fSRodney W. Grimes {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; 50158f0484fSRodney W. Grimes static char zeroes[PADSIZE] = 50258f0484fSRodney W. Grimes {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'}; 50358f0484fSRodney W. Grimes 50458f0484fSRodney W. Grimes /* 50558f0484fSRodney W. Grimes * BEWARE, these `goto error' on error, and PAD uses `n'. 50658f0484fSRodney W. Grimes */ 50758f0484fSRodney W. Grimes #define PRINT(ptr, len) { \ 50858f0484fSRodney W. Grimes iovp->iov_base = (ptr); \ 50958f0484fSRodney W. Grimes iovp->iov_len = (len); \ 51058f0484fSRodney W. Grimes uio.uio_resid += (len); \ 51158f0484fSRodney W. Grimes iovp++; \ 51258f0484fSRodney W. Grimes if (++uio.uio_iovcnt >= NIOV) { \ 51358f0484fSRodney W. Grimes if (__sprint(fp, &uio)) \ 51458f0484fSRodney W. Grimes goto error; \ 51558f0484fSRodney W. Grimes iovp = iov; \ 51658f0484fSRodney W. Grimes } \ 51758f0484fSRodney W. Grimes } 51858f0484fSRodney W. Grimes #define PAD(howmany, with) { \ 51958f0484fSRodney W. Grimes if ((n = (howmany)) > 0) { \ 52058f0484fSRodney W. Grimes while (n > PADSIZE) { \ 52158f0484fSRodney W. Grimes PRINT(with, PADSIZE); \ 52258f0484fSRodney W. Grimes n -= PADSIZE; \ 52358f0484fSRodney W. Grimes } \ 52458f0484fSRodney W. Grimes PRINT(with, n); \ 52558f0484fSRodney W. Grimes } \ 52658f0484fSRodney W. Grimes } 52758f0484fSRodney W. Grimes #define FLUSH() { \ 52858f0484fSRodney W. Grimes if (uio.uio_resid && __sprint(fp, &uio)) \ 52958f0484fSRodney W. Grimes goto error; \ 53058f0484fSRodney W. Grimes uio.uio_iovcnt = 0; \ 53158f0484fSRodney W. Grimes iovp = iov; \ 53258f0484fSRodney W. Grimes } 53358f0484fSRodney W. Grimes 53458f0484fSRodney W. Grimes /* 535efb7e53dSJordan K. Hubbard * Get the argument indexed by nextarg. If the argument table is 536efb7e53dSJordan K. Hubbard * built, use it to get the argument. If its not, get the next 537efb7e53dSJordan K. Hubbard * argument (and arguments must be gotten sequentially). 538efb7e53dSJordan K. Hubbard */ 539efb7e53dSJordan K. Hubbard #define GETARG(type) \ 540a387081cSDoug Rabson ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \ 541efb7e53dSJordan K. Hubbard (nextarg++, va_arg(ap, type))) 542efb7e53dSJordan K. Hubbard 543efb7e53dSJordan K. Hubbard /* 54458f0484fSRodney W. Grimes * To extend shorts properly, we need both signed and unsigned 54558f0484fSRodney W. Grimes * argument extraction methods. 54658f0484fSRodney W. Grimes */ 54758f0484fSRodney W. Grimes #define SARG() \ 548efb7e53dSJordan K. Hubbard (flags&LONGINT ? GETARG(long) : \ 549efb7e53dSJordan K. Hubbard flags&SHORTINT ? (long)(short)GETARG(int) : \ 5507735bb0fSBill Fenner flags&CHARINT ? (long)(signed char)GETARG(int) : \ 551efb7e53dSJordan K. Hubbard (long)GETARG(int)) 55258f0484fSRodney W. Grimes #define UARG() \ 553efb7e53dSJordan K. Hubbard (flags&LONGINT ? GETARG(u_long) : \ 554efb7e53dSJordan K. Hubbard flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \ 5557735bb0fSBill Fenner flags&CHARINT ? (u_long)(u_char)GETARG(int) : \ 556efb7e53dSJordan K. Hubbard (u_long)GETARG(u_int)) 5577735bb0fSBill Fenner #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT) 5587735bb0fSBill Fenner #define SJARG() \ 5597735bb0fSBill Fenner (flags&INTMAXT ? GETARG(intmax_t) : \ 5607735bb0fSBill Fenner flags&SIZET ? (intmax_t)GETARG(size_t) : \ 5617735bb0fSBill Fenner flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \ 5627735bb0fSBill Fenner (intmax_t)GETARG(long long)) 5637735bb0fSBill Fenner #define UJARG() \ 5647735bb0fSBill Fenner (flags&INTMAXT ? GETARG(uintmax_t) : \ 5657735bb0fSBill Fenner flags&SIZET ? (uintmax_t)GETARG(size_t) : \ 5667735bb0fSBill Fenner flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \ 5677735bb0fSBill Fenner (uintmax_t)GETARG(unsigned long long)) 568efb7e53dSJordan K. Hubbard 569efb7e53dSJordan K. Hubbard /* 570efb7e53dSJordan K. Hubbard * Get * arguments, including the form *nn$. Preserve the nextarg 571efb7e53dSJordan K. Hubbard * that the argument can be gotten once the type is determined. 572efb7e53dSJordan K. Hubbard */ 573efb7e53dSJordan K. Hubbard #define GETASTER(val) \ 574efb7e53dSJordan K. Hubbard n2 = 0; \ 575efb7e53dSJordan K. Hubbard cp = fmt; \ 576efb7e53dSJordan K. Hubbard while (is_digit(*cp)) { \ 577efb7e53dSJordan K. Hubbard n2 = 10 * n2 + to_digit(*cp); \ 578efb7e53dSJordan K. Hubbard cp++; \ 579efb7e53dSJordan K. Hubbard } \ 580efb7e53dSJordan K. Hubbard if (*cp == '$') { \ 581efb7e53dSJordan K. Hubbard int hold = nextarg; \ 582efb7e53dSJordan K. Hubbard if (argtable == NULL) { \ 583efb7e53dSJordan K. Hubbard argtable = statargtable; \ 584efb7e53dSJordan K. Hubbard __find_arguments (fmt0, orgap, &argtable); \ 585efb7e53dSJordan K. Hubbard } \ 586efb7e53dSJordan K. Hubbard nextarg = n2; \ 587efb7e53dSJordan K. Hubbard val = GETARG (int); \ 588efb7e53dSJordan K. Hubbard nextarg = hold; \ 589efb7e53dSJordan K. Hubbard fmt = ++cp; \ 590efb7e53dSJordan K. Hubbard } else { \ 591efb7e53dSJordan K. Hubbard val = GETARG (int); \ 592efb7e53dSJordan K. Hubbard } 593efb7e53dSJordan K. Hubbard 59458f0484fSRodney W. Grimes 59598ee7635SAlexey Zelkin thousands_sep = '\0'; 59698ee7635SAlexey Zelkin grouping = NULL; 597b9aac308STim J. Robbins convbuf = NULL; 5982ffc61baSTor Egge #ifdef FLOATING_POINT 5992ffc61baSTor Egge dtoaresult = NULL; 6007735bb0fSBill Fenner decimal_point = localeconv()->decimal_point; 6012ffc61baSTor Egge #endif 60258f0484fSRodney W. Grimes /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */ 603d201fe46SDaniel Eischen if (cantwrite(fp)) 60458f0484fSRodney W. Grimes return (EOF); 60558f0484fSRodney W. Grimes 60658f0484fSRodney W. Grimes /* optimise fprintf(stderr) (and other unbuffered Unix files) */ 60758f0484fSRodney W. Grimes if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) && 608d201fe46SDaniel Eischen fp->_file >= 0) 60958f0484fSRodney W. Grimes return (__sbprintf(fp, fmt0, ap)); 61058f0484fSRodney W. Grimes 61158f0484fSRodney W. Grimes fmt = (char *)fmt0; 612efb7e53dSJordan K. Hubbard argtable = NULL; 613efb7e53dSJordan K. Hubbard nextarg = 1; 614d07090a8STim J. Robbins va_copy(orgap, ap); 61558f0484fSRodney W. Grimes uio.uio_iov = iovp = iov; 61658f0484fSRodney W. Grimes uio.uio_resid = 0; 61758f0484fSRodney W. Grimes uio.uio_iovcnt = 0; 61858f0484fSRodney W. Grimes ret = 0; 61958f0484fSRodney W. Grimes 62058f0484fSRodney W. Grimes /* 62158f0484fSRodney W. Grimes * Scan the format for conversions (`%' character). 62258f0484fSRodney W. Grimes */ 62358f0484fSRodney W. Grimes for (;;) { 62458f0484fSRodney W. Grimes for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) 62558f0484fSRodney W. Grimes /* void */; 62658f0484fSRodney W. Grimes if ((n = fmt - cp) != 0) { 627b250f248SAndrey A. Chernov if ((unsigned)ret + n > INT_MAX) { 62892e88f87SAndrey A. Chernov ret = EOF; 62992e88f87SAndrey A. Chernov goto error; 63092e88f87SAndrey A. Chernov } 63158f0484fSRodney W. Grimes PRINT(cp, n); 63258f0484fSRodney W. Grimes ret += n; 63358f0484fSRodney W. Grimes } 63458f0484fSRodney W. Grimes if (ch == '\0') 63558f0484fSRodney W. Grimes goto done; 63658f0484fSRodney W. Grimes fmt++; /* skip over '%' */ 63758f0484fSRodney W. Grimes 63858f0484fSRodney W. Grimes flags = 0; 63958f0484fSRodney W. Grimes dprec = 0; 64058f0484fSRodney W. Grimes width = 0; 64158f0484fSRodney W. Grimes prec = -1; 64258f0484fSRodney W. Grimes sign = '\0'; 64358f0484fSRodney W. Grimes 64458f0484fSRodney W. Grimes rflag: ch = *fmt++; 64558f0484fSRodney W. Grimes reswitch: switch (ch) { 64658f0484fSRodney W. Grimes case ' ': 6472e394b2fSAlexey Zelkin /*- 64858f0484fSRodney W. Grimes * ``If the space and + flags both appear, the space 64958f0484fSRodney W. Grimes * flag will be ignored.'' 65058f0484fSRodney W. Grimes * -- ANSI X3J11 65158f0484fSRodney W. Grimes */ 65258f0484fSRodney W. Grimes if (!sign) 65358f0484fSRodney W. Grimes sign = ' '; 65458f0484fSRodney W. Grimes goto rflag; 65558f0484fSRodney W. Grimes case '#': 65658f0484fSRodney W. Grimes flags |= ALT; 65758f0484fSRodney W. Grimes goto rflag; 65858f0484fSRodney W. Grimes case '*': 6592e394b2fSAlexey Zelkin /*- 66058f0484fSRodney W. Grimes * ``A negative field width argument is taken as a 66158f0484fSRodney W. Grimes * - flag followed by a positive field width.'' 66258f0484fSRodney W. Grimes * -- ANSI X3J11 66358f0484fSRodney W. Grimes * They don't exclude field widths read from args. 66458f0484fSRodney W. Grimes */ 665efb7e53dSJordan K. Hubbard GETASTER (width); 666efb7e53dSJordan K. Hubbard if (width >= 0) 66758f0484fSRodney W. Grimes goto rflag; 66858f0484fSRodney W. Grimes width = -width; 66958f0484fSRodney W. Grimes /* FALLTHROUGH */ 67058f0484fSRodney W. Grimes case '-': 67158f0484fSRodney W. Grimes flags |= LADJUST; 67258f0484fSRodney W. Grimes goto rflag; 67358f0484fSRodney W. Grimes case '+': 67458f0484fSRodney W. Grimes sign = '+'; 67558f0484fSRodney W. Grimes goto rflag; 6767735bb0fSBill Fenner case '\'': 67798ee7635SAlexey Zelkin flags |= GROUPING; 67898ee7635SAlexey Zelkin thousands_sep = *(localeconv()->thousands_sep); 67998ee7635SAlexey Zelkin grouping = localeconv()->grouping; 6807735bb0fSBill Fenner goto rflag; 68158f0484fSRodney W. Grimes case '.': 68258f0484fSRodney W. Grimes if ((ch = *fmt++) == '*') { 683efb7e53dSJordan K. Hubbard GETASTER (n); 68458f0484fSRodney W. Grimes prec = n < 0 ? -1 : n; 68558f0484fSRodney W. Grimes goto rflag; 68658f0484fSRodney W. Grimes } 68758f0484fSRodney W. Grimes n = 0; 68858f0484fSRodney W. Grimes while (is_digit(ch)) { 68958f0484fSRodney W. Grimes n = 10 * n + to_digit(ch); 69058f0484fSRodney W. Grimes ch = *fmt++; 69158f0484fSRodney W. Grimes } 69258f0484fSRodney W. Grimes prec = n < 0 ? -1 : n; 69358f0484fSRodney W. Grimes goto reswitch; 69458f0484fSRodney W. Grimes case '0': 6952e394b2fSAlexey Zelkin /*- 69658f0484fSRodney W. Grimes * ``Note that 0 is taken as a flag, not as the 69758f0484fSRodney W. Grimes * beginning of a field width.'' 69858f0484fSRodney W. Grimes * -- ANSI X3J11 69958f0484fSRodney W. Grimes */ 70058f0484fSRodney W. Grimes flags |= ZEROPAD; 70158f0484fSRodney W. Grimes goto rflag; 70258f0484fSRodney W. Grimes case '1': case '2': case '3': case '4': 70358f0484fSRodney W. Grimes case '5': case '6': case '7': case '8': case '9': 70458f0484fSRodney W. Grimes n = 0; 70558f0484fSRodney W. Grimes do { 70658f0484fSRodney W. Grimes n = 10 * n + to_digit(ch); 70758f0484fSRodney W. Grimes ch = *fmt++; 70858f0484fSRodney W. Grimes } while (is_digit(ch)); 709efb7e53dSJordan K. Hubbard if (ch == '$') { 710efb7e53dSJordan K. Hubbard nextarg = n; 711efb7e53dSJordan K. Hubbard if (argtable == NULL) { 712efb7e53dSJordan K. Hubbard argtable = statargtable; 713efb7e53dSJordan K. Hubbard __find_arguments (fmt0, orgap, 714efb7e53dSJordan K. Hubbard &argtable); 715efb7e53dSJordan K. Hubbard } 716efb7e53dSJordan K. Hubbard goto rflag; 717efb7e53dSJordan K. Hubbard } 71858f0484fSRodney W. Grimes width = n; 71958f0484fSRodney W. Grimes goto reswitch; 72058f0484fSRodney W. Grimes #ifdef FLOATING_POINT 72158f0484fSRodney W. Grimes case 'L': 72258f0484fSRodney W. Grimes flags |= LONGDBL; 72358f0484fSRodney W. Grimes goto rflag; 72458f0484fSRodney W. Grimes #endif 72558f0484fSRodney W. Grimes case 'h': 7267735bb0fSBill Fenner if (flags & SHORTINT) { 7277735bb0fSBill Fenner flags &= ~SHORTINT; 7287735bb0fSBill Fenner flags |= CHARINT; 7297735bb0fSBill Fenner } else 73058f0484fSRodney W. Grimes flags |= SHORTINT; 73158f0484fSRodney W. Grimes goto rflag; 7327735bb0fSBill Fenner case 'j': 7337735bb0fSBill Fenner flags |= INTMAXT; 7347735bb0fSBill Fenner goto rflag; 73558f0484fSRodney W. Grimes case 'l': 7367735bb0fSBill Fenner if (flags & LONGINT) { 7377735bb0fSBill Fenner flags &= ~LONGINT; 7387735bb0fSBill Fenner flags |= LLONGINT; 7397735bb0fSBill Fenner } else 74058f0484fSRodney W. Grimes flags |= LONGINT; 74158f0484fSRodney W. Grimes goto rflag; 74258f0484fSRodney W. Grimes case 'q': 7437735bb0fSBill Fenner flags |= LLONGINT; /* not necessarily */ 7447735bb0fSBill Fenner goto rflag; 7457735bb0fSBill Fenner case 't': 7467735bb0fSBill Fenner flags |= PTRDIFFT; 7477735bb0fSBill Fenner goto rflag; 7487735bb0fSBill Fenner case 'z': 7497735bb0fSBill Fenner flags |= SIZET; 75058f0484fSRodney W. Grimes goto rflag; 751927ecbf3STim J. Robbins case 'C': 752927ecbf3STim J. Robbins flags |= LONGINT; 753927ecbf3STim J. Robbins /*FALLTHROUGH*/ 75458f0484fSRodney W. Grimes case 'c': 755b9aac308STim J. Robbins if (flags & LONGINT) { 756b9aac308STim J. Robbins mbstate_t mbs; 757b9aac308STim J. Robbins size_t mbseqlen; 758b9aac308STim J. Robbins 759b9aac308STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 760b9aac308STim J. Robbins mbseqlen = wcrtomb(cp = buf, 761b9aac308STim J. Robbins (wchar_t)GETARG(wint_t), &mbs); 7626180233fSTim J. Robbins if (mbseqlen == (size_t)-1) { 7636180233fSTim J. Robbins fp->_flags |= __SERR; 764b9aac308STim J. Robbins goto error; 7656180233fSTim J. Robbins } 766b9aac308STim J. Robbins size = (int)mbseqlen; 767b9aac308STim J. Robbins } else { 768efb7e53dSJordan K. Hubbard *(cp = buf) = GETARG(int); 76958f0484fSRodney W. Grimes size = 1; 770b9aac308STim J. Robbins } 77158f0484fSRodney W. Grimes sign = '\0'; 77258f0484fSRodney W. Grimes break; 77358f0484fSRodney W. Grimes case 'D': 77458f0484fSRodney W. Grimes flags |= LONGINT; 77558f0484fSRodney W. Grimes /*FALLTHROUGH*/ 77658f0484fSRodney W. Grimes case 'd': 77758f0484fSRodney W. Grimes case 'i': 7787735bb0fSBill Fenner if (flags & INTMAX_SIZE) { 7797735bb0fSBill Fenner ujval = SJARG(); 7807735bb0fSBill Fenner if ((intmax_t)ujval < 0) { 7817735bb0fSBill Fenner ujval = -ujval; 78258f0484fSRodney W. Grimes sign = '-'; 78358f0484fSRodney W. Grimes } 78458f0484fSRodney W. Grimes } else { 78558f0484fSRodney W. Grimes ulval = SARG(); 78658f0484fSRodney W. Grimes if ((long)ulval < 0) { 78758f0484fSRodney W. Grimes ulval = -ulval; 78858f0484fSRodney W. Grimes sign = '-'; 78958f0484fSRodney W. Grimes } 79058f0484fSRodney W. Grimes } 79158f0484fSRodney W. Grimes base = 10; 79258f0484fSRodney W. Grimes goto number; 79358f0484fSRodney W. Grimes #ifdef FLOATING_POINT 7947735bb0fSBill Fenner #ifdef HEXFLOAT 7957735bb0fSBill Fenner case 'a': 7967735bb0fSBill Fenner case 'A': 7977735bb0fSBill Fenner #endif 798d26be6f0SBruce Evans case 'e': 79958f0484fSRodney W. Grimes case 'E': 8002e394b2fSAlexey Zelkin /*- 80198ee7635SAlexey Zelkin * Grouping apply to %i, %d, %u, %f, %F, %g, %G 80298ee7635SAlexey Zelkin * conversion specifiers only. For other conversions 80398ee7635SAlexey Zelkin * behavior is undefined. 80498ee7635SAlexey Zelkin * -- POSIX 80598ee7635SAlexey Zelkin */ 80698ee7635SAlexey Zelkin flags &= ~GROUPING; 80798ee7635SAlexey Zelkin /*FALLTHROUGH*/ 808d26be6f0SBruce Evans case 'f': 8097735bb0fSBill Fenner case 'F': 810d26be6f0SBruce Evans goto fp_begin; 81158f0484fSRodney W. Grimes case 'g': 81258f0484fSRodney W. Grimes case 'G': 813d26be6f0SBruce Evans if (prec == 0) 814d26be6f0SBruce Evans prec = 1; 815d26be6f0SBruce Evans fp_begin: if (prec == -1) 81658f0484fSRodney W. Grimes prec = DEFPREC; 817d26be6f0SBruce Evans if (flags & LONGDBL) 8186a93659fSBruce Evans /* XXX this loses precision. */ 819efb7e53dSJordan K. Hubbard _double = (double)GETARG(long double); 820d26be6f0SBruce Evans else 821efb7e53dSJordan K. Hubbard _double = GETARG(double); 82258f0484fSRodney W. Grimes /* do this before tricky precision changes */ 82358f0484fSRodney W. Grimes if (isinf(_double)) { 82458f0484fSRodney W. Grimes if (_double < 0) 82558f0484fSRodney W. Grimes sign = '-'; 8267735bb0fSBill Fenner if (isupper(ch)) 8277735bb0fSBill Fenner cp = "INF"; 8287735bb0fSBill Fenner else 8297735bb0fSBill Fenner cp = "inf"; 83058f0484fSRodney W. Grimes size = 3; 83158f0484fSRodney W. Grimes break; 83258f0484fSRodney W. Grimes } 83358f0484fSRodney W. Grimes if (isnan(_double)) { 8347735bb0fSBill Fenner if (isupper(ch)) 8357735bb0fSBill Fenner cp = "NAN"; 8367735bb0fSBill Fenner else 8377735bb0fSBill Fenner cp = "nan"; 83858f0484fSRodney W. Grimes size = 3; 83958f0484fSRodney W. Grimes break; 84058f0484fSRodney W. Grimes } 84158f0484fSRodney W. Grimes flags |= FPT; 8422ffc61baSTor Egge if (dtoaresult != NULL) { 8432ffc61baSTor Egge free(dtoaresult); 8442ffc61baSTor Egge dtoaresult = NULL; 8452ffc61baSTor Egge } 84658f0484fSRodney W. Grimes cp = cvt(_double, prec, flags, &softsign, 8472ffc61baSTor Egge &expt, ch, &ndig, &dtoaresult); 84858f0484fSRodney W. Grimes if (ch == 'g' || ch == 'G') { 84958f0484fSRodney W. Grimes if (expt <= -4 || expt > prec) 85058f0484fSRodney W. Grimes ch = (ch == 'g') ? 'e' : 'E'; 85158f0484fSRodney W. Grimes else 85258f0484fSRodney W. Grimes ch = 'g'; 85358f0484fSRodney W. Grimes } 8547735bb0fSBill Fenner if (ch == 'e' || ch == 'E') { 85558f0484fSRodney W. Grimes --expt; 85658f0484fSRodney W. Grimes expsize = exponent(expstr, expt, ch); 85758f0484fSRodney W. Grimes size = expsize + ndig; 85858f0484fSRodney W. Grimes if (ndig > 1 || flags & ALT) 85958f0484fSRodney W. Grimes ++size; 8607735bb0fSBill Fenner } else if (ch == 'f' || ch == 'F') { 86158f0484fSRodney W. Grimes if (expt > 0) { 86258f0484fSRodney W. Grimes size = expt; 86358f0484fSRodney W. Grimes if (prec || flags & ALT) 86458f0484fSRodney W. Grimes size += prec + 1; 86558f0484fSRodney W. Grimes } else /* "0.X" */ 86658f0484fSRodney W. Grimes size = prec + 2; 86758f0484fSRodney W. Grimes } else if (expt >= ndig) { /* fixed g fmt */ 86858f0484fSRodney W. Grimes size = expt; 86958f0484fSRodney W. Grimes if (flags & ALT) 87058f0484fSRodney W. Grimes ++size; 87158f0484fSRodney W. Grimes } else 87258f0484fSRodney W. Grimes size = ndig + (expt > 0 ? 87358f0484fSRodney W. Grimes 1 : 2 - expt); 87458f0484fSRodney W. Grimes 87558f0484fSRodney W. Grimes if (softsign) 87658f0484fSRodney W. Grimes sign = '-'; 87758f0484fSRodney W. Grimes break; 87858f0484fSRodney W. Grimes #endif /* FLOATING_POINT */ 87958f0484fSRodney W. Grimes case 'n': 8807735bb0fSBill Fenner /* 8817735bb0fSBill Fenner * Assignment-like behavior is specified if the 8827735bb0fSBill Fenner * value overflows or is otherwise unrepresentable. 8837735bb0fSBill Fenner * C99 says to use `signed char' for %hhn conversions. 8847735bb0fSBill Fenner */ 8857735bb0fSBill Fenner if (flags & LLONGINT) 8867735bb0fSBill Fenner *GETARG(long long *) = ret; 8877735bb0fSBill Fenner else if (flags & SIZET) 8887735bb0fSBill Fenner *GETARG(ssize_t *) = (ssize_t)ret; 8897735bb0fSBill Fenner else if (flags & PTRDIFFT) 8907735bb0fSBill Fenner *GETARG(ptrdiff_t *) = ret; 8917735bb0fSBill Fenner else if (flags & INTMAXT) 8927735bb0fSBill Fenner *GETARG(intmax_t *) = ret; 89358f0484fSRodney W. Grimes else if (flags & LONGINT) 8946e690ad4SAndrey A. Chernov *GETARG(long *) = ret; 89558f0484fSRodney W. Grimes else if (flags & SHORTINT) 8966e690ad4SAndrey A. Chernov *GETARG(short *) = ret; 8977735bb0fSBill Fenner else if (flags & CHARINT) 8987735bb0fSBill Fenner *GETARG(signed char *) = ret; 89958f0484fSRodney W. Grimes else 9006e690ad4SAndrey A. Chernov *GETARG(int *) = ret; 90158f0484fSRodney W. Grimes continue; /* no output */ 90258f0484fSRodney W. Grimes case 'O': 90358f0484fSRodney W. Grimes flags |= LONGINT; 90458f0484fSRodney W. Grimes /*FALLTHROUGH*/ 90558f0484fSRodney W. Grimes case 'o': 9067735bb0fSBill Fenner if (flags & INTMAX_SIZE) 9077735bb0fSBill Fenner ujval = UJARG(); 90858f0484fSRodney W. Grimes else 90958f0484fSRodney W. Grimes ulval = UARG(); 91058f0484fSRodney W. Grimes base = 8; 91158f0484fSRodney W. Grimes goto nosign; 91258f0484fSRodney W. Grimes case 'p': 9132e394b2fSAlexey Zelkin /*- 91458f0484fSRodney W. Grimes * ``The argument shall be a pointer to void. The 91558f0484fSRodney W. Grimes * value of the pointer is converted to a sequence 91658f0484fSRodney W. Grimes * of printable characters, in an implementation- 91758f0484fSRodney W. Grimes * defined manner.'' 91858f0484fSRodney W. Grimes * -- ANSI X3J11 91958f0484fSRodney W. Grimes */ 9207735bb0fSBill Fenner ujval = (uintmax_t)(uintptr_t)GETARG(void *); 92158f0484fSRodney W. Grimes base = 16; 92258f0484fSRodney W. Grimes xdigs = "0123456789abcdef"; 9237735bb0fSBill Fenner flags = flags | INTMAXT | HEXPREFIX; 92458f0484fSRodney W. Grimes ch = 'x'; 92558f0484fSRodney W. Grimes goto nosign; 926927ecbf3STim J. Robbins case 'S': 927927ecbf3STim J. Robbins flags |= LONGINT; 928927ecbf3STim J. Robbins /*FALLTHROUGH*/ 92958f0484fSRodney W. Grimes case 's': 930b9aac308STim J. Robbins if (flags & LONGINT) { 931b9aac308STim J. Robbins wchar_t *wcp; 932b9aac308STim J. Robbins 933b9aac308STim J. Robbins if (convbuf != NULL) 934b9aac308STim J. Robbins free(convbuf); 935b9aac308STim J. Robbins if ((wcp = GETARG(wchar_t *)) == NULL) 936b9aac308STim J. Robbins cp = "(null)"; 937b9aac308STim J. Robbins else { 938b9aac308STim J. Robbins convbuf = __wcsconv(wcp, prec); 9396180233fSTim J. Robbins if (convbuf == NULL) { 9406180233fSTim J. Robbins fp->_flags |= __SERR; 941b9aac308STim J. Robbins goto error; 9426180233fSTim J. Robbins } 943b9aac308STim J. Robbins cp = convbuf; 944b9aac308STim J. Robbins } 945b9aac308STim J. Robbins } else if ((cp = GETARG(char *)) == NULL) 94658f0484fSRodney W. Grimes cp = "(null)"; 94758f0484fSRodney W. Grimes if (prec >= 0) { 94858f0484fSRodney W. Grimes /* 94958f0484fSRodney W. Grimes * can't use strlen; can only look for the 95058f0484fSRodney W. Grimes * NUL in the first `prec' characters, and 95158f0484fSRodney W. Grimes * strlen() will go further. 95258f0484fSRodney W. Grimes */ 953ce51cf03SJames Raynard char *p = memchr(cp, 0, (size_t)prec); 95458f0484fSRodney W. Grimes 95558f0484fSRodney W. Grimes if (p != NULL) { 95658f0484fSRodney W. Grimes size = p - cp; 95758f0484fSRodney W. Grimes if (size > prec) 95858f0484fSRodney W. Grimes size = prec; 95958f0484fSRodney W. Grimes } else 96058f0484fSRodney W. Grimes size = prec; 96158f0484fSRodney W. Grimes } else 96258f0484fSRodney W. Grimes size = strlen(cp); 96358f0484fSRodney W. Grimes sign = '\0'; 96458f0484fSRodney W. Grimes break; 96558f0484fSRodney W. Grimes case 'U': 96658f0484fSRodney W. Grimes flags |= LONGINT; 96758f0484fSRodney W. Grimes /*FALLTHROUGH*/ 96858f0484fSRodney W. Grimes case 'u': 9697735bb0fSBill Fenner if (flags & INTMAX_SIZE) 9707735bb0fSBill Fenner ujval = UJARG(); 97158f0484fSRodney W. Grimes else 97258f0484fSRodney W. Grimes ulval = UARG(); 97358f0484fSRodney W. Grimes base = 10; 97458f0484fSRodney W. Grimes goto nosign; 97558f0484fSRodney W. Grimes case 'X': 97658f0484fSRodney W. Grimes xdigs = "0123456789ABCDEF"; 97758f0484fSRodney W. Grimes goto hex; 97858f0484fSRodney W. Grimes case 'x': 97958f0484fSRodney W. Grimes xdigs = "0123456789abcdef"; 9807735bb0fSBill Fenner hex: 9817735bb0fSBill Fenner if (flags & INTMAX_SIZE) 9827735bb0fSBill Fenner ujval = UJARG(); 98358f0484fSRodney W. Grimes else 98458f0484fSRodney W. Grimes ulval = UARG(); 98558f0484fSRodney W. Grimes base = 16; 98658f0484fSRodney W. Grimes /* leading 0x/X only if non-zero */ 98758f0484fSRodney W. Grimes if (flags & ALT && 9887735bb0fSBill Fenner (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0)) 98958f0484fSRodney W. Grimes flags |= HEXPREFIX; 99058f0484fSRodney W. Grimes 99198ee7635SAlexey Zelkin flags &= ~GROUPING; 99258f0484fSRodney W. Grimes /* unsigned conversions */ 99358f0484fSRodney W. Grimes nosign: sign = '\0'; 9942e394b2fSAlexey Zelkin /*- 99558f0484fSRodney W. Grimes * ``... diouXx conversions ... if a precision is 99658f0484fSRodney W. Grimes * specified, the 0 flag will be ignored.'' 99758f0484fSRodney W. Grimes * -- ANSI X3J11 99858f0484fSRodney W. Grimes */ 99958f0484fSRodney W. Grimes number: if ((dprec = prec) >= 0) 100058f0484fSRodney W. Grimes flags &= ~ZEROPAD; 100158f0484fSRodney W. Grimes 10022e394b2fSAlexey Zelkin /*- 100358f0484fSRodney W. Grimes * ``The result of converting a zero value with an 100458f0484fSRodney W. Grimes * explicit precision of zero is no characters.'' 100558f0484fSRodney W. Grimes * -- ANSI X3J11 100658f0484fSRodney W. Grimes */ 100758f0484fSRodney W. Grimes cp = buf + BUF; 10087735bb0fSBill Fenner if (flags & INTMAX_SIZE) { 10097735bb0fSBill Fenner if (ujval != 0 || prec != 0) 10107735bb0fSBill Fenner cp = __ujtoa(ujval, cp, base, 101198ee7635SAlexey Zelkin flags & ALT, xdigs, 101298ee7635SAlexey Zelkin flags & GROUPING, thousands_sep, 101398ee7635SAlexey Zelkin grouping); 101458f0484fSRodney W. Grimes } else { 101558f0484fSRodney W. Grimes if (ulval != 0 || prec != 0) 101658f0484fSRodney W. Grimes cp = __ultoa(ulval, cp, base, 101798ee7635SAlexey Zelkin flags & ALT, xdigs, 101898ee7635SAlexey Zelkin flags & GROUPING, thousands_sep, 101998ee7635SAlexey Zelkin grouping); 102058f0484fSRodney W. Grimes } 102158f0484fSRodney W. Grimes size = buf + BUF - cp; 102258f0484fSRodney W. Grimes break; 102358f0484fSRodney W. Grimes default: /* "%?" prints ?, unless ? is NUL */ 102458f0484fSRodney W. Grimes if (ch == '\0') 102558f0484fSRodney W. Grimes goto done; 102658f0484fSRodney W. Grimes /* pretend it was %c with argument ch */ 102758f0484fSRodney W. Grimes cp = buf; 102858f0484fSRodney W. Grimes *cp = ch; 102958f0484fSRodney W. Grimes size = 1; 103058f0484fSRodney W. Grimes sign = '\0'; 103158f0484fSRodney W. Grimes break; 103258f0484fSRodney W. Grimes } 103358f0484fSRodney W. Grimes 103458f0484fSRodney W. Grimes /* 103558f0484fSRodney W. Grimes * All reasonable formats wind up here. At this point, `cp' 103658f0484fSRodney W. Grimes * points to a string which (if not flags&LADJUST) should be 103758f0484fSRodney W. Grimes * padded out to `width' places. If flags&ZEROPAD, it should 103858f0484fSRodney W. Grimes * first be prefixed by any sign or other prefix; otherwise, 103958f0484fSRodney W. Grimes * it should be blank padded before the prefix is emitted. 104058f0484fSRodney W. Grimes * After any left-hand padding and prefixing, emit zeroes 104158f0484fSRodney W. Grimes * required by a decimal [diouxX] precision, then print the 104258f0484fSRodney W. Grimes * string proper, then emit zeroes required by any leftover 104358f0484fSRodney W. Grimes * floating precision; finally, if LADJUST, pad with blanks. 104458f0484fSRodney W. Grimes * 104558f0484fSRodney W. Grimes * Compute actual size, so we know how much to pad. 1046261a532aSBill Fenner * size excludes decimal prec; realsz includes it. 104758f0484fSRodney W. Grimes */ 1048261a532aSBill Fenner realsz = dprec > size ? dprec : size; 104958f0484fSRodney W. Grimes if (sign) 1050261a532aSBill Fenner realsz++; 105158f0484fSRodney W. Grimes else if (flags & HEXPREFIX) 1052261a532aSBill Fenner realsz += 2; 105358f0484fSRodney W. Grimes 105492e88f87SAndrey A. Chernov prsize = width > realsz ? width : realsz; 1055b250f248SAndrey A. Chernov if ((unsigned)ret + prsize > INT_MAX) { 105692e88f87SAndrey A. Chernov ret = EOF; 105792e88f87SAndrey A. Chernov goto error; 105892e88f87SAndrey A. Chernov } 105992e88f87SAndrey A. Chernov 106058f0484fSRodney W. Grimes /* right-adjusting blank padding */ 106158f0484fSRodney W. Grimes if ((flags & (LADJUST|ZEROPAD)) == 0) 106258f0484fSRodney W. Grimes PAD(width - realsz, blanks); 106358f0484fSRodney W. Grimes 106458f0484fSRodney W. Grimes /* prefix */ 106558f0484fSRodney W. Grimes if (sign) { 106658f0484fSRodney W. Grimes PRINT(&sign, 1); 106758f0484fSRodney W. Grimes } else if (flags & HEXPREFIX) { 106858f0484fSRodney W. Grimes ox[0] = '0'; 106958f0484fSRodney W. Grimes ox[1] = ch; 107058f0484fSRodney W. Grimes PRINT(ox, 2); 107158f0484fSRodney W. Grimes } 107258f0484fSRodney W. Grimes 107358f0484fSRodney W. Grimes /* right-adjusting zero padding */ 107458f0484fSRodney W. Grimes if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) 107558f0484fSRodney W. Grimes PAD(width - realsz, zeroes); 107658f0484fSRodney W. Grimes 107758f0484fSRodney W. Grimes /* leading zeroes from decimal precision */ 1078261a532aSBill Fenner PAD(dprec - size, zeroes); 107958f0484fSRodney W. Grimes 108058f0484fSRodney W. Grimes /* the string or number proper */ 108158f0484fSRodney W. Grimes #ifdef FLOATING_POINT 108258f0484fSRodney W. Grimes if ((flags & FPT) == 0) { 108358f0484fSRodney W. Grimes PRINT(cp, size); 108458f0484fSRodney W. Grimes } else { /* glue together f_p fragments */ 108558f0484fSRodney W. Grimes if (ch >= 'f') { /* 'f' or 'g' */ 108658f0484fSRodney W. Grimes if (_double == 0) { 108758f0484fSRodney W. Grimes /* kludge for __dtoa irregularity */ 108858f0484fSRodney W. Grimes PRINT("0", 1); 10899ad80ab5SAndrey A. Chernov if (expt < ndig || (flags & ALT) != 0) { 10909ad80ab5SAndrey A. Chernov PRINT(decimal_point, 1); 109158f0484fSRodney W. Grimes PAD(ndig - 1, zeroes); 109258f0484fSRodney W. Grimes } 109358f0484fSRodney W. Grimes } else if (expt <= 0) { 10949ad80ab5SAndrey A. Chernov PRINT("0", 1); 10959ad80ab5SAndrey A. Chernov PRINT(decimal_point, 1); 109658f0484fSRodney W. Grimes PAD(-expt, zeroes); 109758f0484fSRodney W. Grimes PRINT(cp, ndig); 109858f0484fSRodney W. Grimes } else if (expt >= ndig) { 109958f0484fSRodney W. Grimes PRINT(cp, ndig); 110058f0484fSRodney W. Grimes PAD(expt - ndig, zeroes); 110158f0484fSRodney W. Grimes if (flags & ALT) 11029ad80ab5SAndrey A. Chernov PRINT(decimal_point, 1); 110358f0484fSRodney W. Grimes } else { 110458f0484fSRodney W. Grimes PRINT(cp, expt); 110558f0484fSRodney W. Grimes cp += expt; 11069ad80ab5SAndrey A. Chernov PRINT(decimal_point, 1); 110758f0484fSRodney W. Grimes PRINT(cp, ndig-expt); 110858f0484fSRodney W. Grimes } 110958f0484fSRodney W. Grimes } else { /* 'e' or 'E' */ 111058f0484fSRodney W. Grimes if (ndig > 1 || flags & ALT) { 111158f0484fSRodney W. Grimes ox[0] = *cp++; 11129ad80ab5SAndrey A. Chernov ox[1] = *decimal_point; 111358f0484fSRodney W. Grimes PRINT(ox, 2); 1114918bed75SBruce Evans if (_double) { 111558f0484fSRodney W. Grimes PRINT(cp, ndig-1); 111658f0484fSRodney W. Grimes } else /* 0.[0..] */ 111758f0484fSRodney W. Grimes /* __dtoa irregularity */ 111858f0484fSRodney W. Grimes PAD(ndig - 1, zeroes); 111958f0484fSRodney W. Grimes } else /* XeYYY */ 112058f0484fSRodney W. Grimes PRINT(cp, 1); 112158f0484fSRodney W. Grimes PRINT(expstr, expsize); 112258f0484fSRodney W. Grimes } 112358f0484fSRodney W. Grimes } 112458f0484fSRodney W. Grimes #else 112558f0484fSRodney W. Grimes PRINT(cp, size); 112658f0484fSRodney W. Grimes #endif 112758f0484fSRodney W. Grimes /* left-adjusting padding (always blank) */ 112858f0484fSRodney W. Grimes if (flags & LADJUST) 112958f0484fSRodney W. Grimes PAD(width - realsz, blanks); 113058f0484fSRodney W. Grimes 113158f0484fSRodney W. Grimes /* finally, adjust ret */ 113292e88f87SAndrey A. Chernov ret += prsize; 113358f0484fSRodney W. Grimes 113458f0484fSRodney W. Grimes FLUSH(); /* copy out the I/O vectors */ 113558f0484fSRodney W. Grimes } 113658f0484fSRodney W. Grimes done: 113758f0484fSRodney W. Grimes FLUSH(); 113858f0484fSRodney W. Grimes error: 11392ffc61baSTor Egge #ifdef FLOATING_POINT 11402ffc61baSTor Egge if (dtoaresult != NULL) 11412ffc61baSTor Egge free(dtoaresult); 11422ffc61baSTor Egge #endif 1143b9aac308STim J. Robbins if (convbuf != NULL) 1144b9aac308STim J. Robbins free(convbuf); 1145f70177e7SJulian Elischer if (__sferror(fp)) 1146f70177e7SJulian Elischer ret = EOF; 1147efb7e53dSJordan K. Hubbard if ((argtable != NULL) && (argtable != statargtable)) 1148efb7e53dSJordan K. Hubbard free (argtable); 1149f70177e7SJulian Elischer return (ret); 115058f0484fSRodney W. Grimes /* NOTREACHED */ 115158f0484fSRodney W. Grimes } 115258f0484fSRodney W. Grimes 1153efb7e53dSJordan K. Hubbard /* 1154efb7e53dSJordan K. Hubbard * Find all arguments when a positional parameter is encountered. Returns a 1155efb7e53dSJordan K. Hubbard * table, indexed by argument number, of pointers to each arguments. The 1156efb7e53dSJordan K. Hubbard * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries. 115742cebaa5SArchie Cobbs * It will be replaces with a malloc-ed one if it overflows. 1158efb7e53dSJordan K. Hubbard */ 1159efb7e53dSJordan K. Hubbard static void 1160a387081cSDoug Rabson __find_arguments (const char *fmt0, va_list ap, union arg **argtable) 1161efb7e53dSJordan K. Hubbard { 1162d201fe46SDaniel Eischen char *fmt; /* format string */ 1163d201fe46SDaniel Eischen int ch; /* character from fmt */ 1164d201fe46SDaniel Eischen int n, n2; /* handy integer (short term usage) */ 1165d201fe46SDaniel Eischen char *cp; /* handy char pointer (short term usage) */ 1166d201fe46SDaniel Eischen int flags; /* flags as above */ 1167efb7e53dSJordan K. Hubbard int width; /* width from format (%8d), or 0 */ 11687735bb0fSBill Fenner enum typeid *typetable; /* table of types */ 11697735bb0fSBill Fenner enum typeid stattypetable [STATIC_ARG_TBL_SIZE]; 1170efb7e53dSJordan K. Hubbard int tablesize; /* current size of type table */ 1171efb7e53dSJordan K. Hubbard int tablemax; /* largest used index in table */ 1172efb7e53dSJordan K. Hubbard int nextarg; /* 1-based argument index */ 1173efb7e53dSJordan K. Hubbard 1174efb7e53dSJordan K. Hubbard /* 1175efb7e53dSJordan K. Hubbard * Add an argument type to the table, expanding if necessary. 1176efb7e53dSJordan K. Hubbard */ 1177efb7e53dSJordan K. Hubbard #define ADDTYPE(type) \ 1178efb7e53dSJordan K. Hubbard ((nextarg >= tablesize) ? \ 1179efb7e53dSJordan K. Hubbard __grow_type_table(nextarg, &typetable, &tablesize) : 0, \ 118042cebaa5SArchie Cobbs (nextarg > tablemax) ? tablemax = nextarg : 0, \ 118142cebaa5SArchie Cobbs typetable[nextarg++] = type) 1182efb7e53dSJordan K. Hubbard 1183efb7e53dSJordan K. Hubbard #define ADDSARG() \ 11847735bb0fSBill Fenner ((flags&INTMAXT) ? ADDTYPE(T_INTMAXT) : \ 11857735bb0fSBill Fenner ((flags&SIZET) ? ADDTYPE(T_SIZET) : \ 11867735bb0fSBill Fenner ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \ 11877735bb0fSBill Fenner ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \ 11887735bb0fSBill Fenner ((flags&LONGINT) ? ADDTYPE(T_LONG) : ADDTYPE(T_INT)))))) 1189efb7e53dSJordan K. Hubbard 1190efb7e53dSJordan K. Hubbard #define ADDUARG() \ 11917735bb0fSBill Fenner ((flags&INTMAXT) ? ADDTYPE(T_UINTMAXT) : \ 11927735bb0fSBill Fenner ((flags&SIZET) ? ADDTYPE(T_SIZET) : \ 11937735bb0fSBill Fenner ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \ 11947735bb0fSBill Fenner ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \ 11957735bb0fSBill Fenner ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : ADDTYPE(T_U_INT)))))) 1196efb7e53dSJordan K. Hubbard 1197efb7e53dSJordan K. Hubbard /* 1198efb7e53dSJordan K. Hubbard * Add * arguments to the type array. 1199efb7e53dSJordan K. Hubbard */ 1200efb7e53dSJordan K. Hubbard #define ADDASTER() \ 1201efb7e53dSJordan K. Hubbard n2 = 0; \ 1202efb7e53dSJordan K. Hubbard cp = fmt; \ 1203efb7e53dSJordan K. Hubbard while (is_digit(*cp)) { \ 1204efb7e53dSJordan K. Hubbard n2 = 10 * n2 + to_digit(*cp); \ 1205efb7e53dSJordan K. Hubbard cp++; \ 1206efb7e53dSJordan K. Hubbard } \ 1207efb7e53dSJordan K. Hubbard if (*cp == '$') { \ 1208efb7e53dSJordan K. Hubbard int hold = nextarg; \ 1209efb7e53dSJordan K. Hubbard nextarg = n2; \ 1210efb7e53dSJordan K. Hubbard ADDTYPE (T_INT); \ 1211efb7e53dSJordan K. Hubbard nextarg = hold; \ 1212efb7e53dSJordan K. Hubbard fmt = ++cp; \ 1213efb7e53dSJordan K. Hubbard } else { \ 1214efb7e53dSJordan K. Hubbard ADDTYPE (T_INT); \ 1215efb7e53dSJordan K. Hubbard } 1216efb7e53dSJordan K. Hubbard fmt = (char *)fmt0; 1217efb7e53dSJordan K. Hubbard typetable = stattypetable; 1218efb7e53dSJordan K. Hubbard tablesize = STATIC_ARG_TBL_SIZE; 1219efb7e53dSJordan K. Hubbard tablemax = 0; 1220efb7e53dSJordan K. Hubbard nextarg = 1; 1221efb7e53dSJordan K. Hubbard memset (typetable, T_UNUSED, STATIC_ARG_TBL_SIZE); 1222efb7e53dSJordan K. Hubbard 1223efb7e53dSJordan K. Hubbard /* 1224efb7e53dSJordan K. Hubbard * Scan the format for conversions (`%' character). 1225efb7e53dSJordan K. Hubbard */ 1226efb7e53dSJordan K. Hubbard for (;;) { 1227efb7e53dSJordan K. Hubbard for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) 1228efb7e53dSJordan K. Hubbard /* void */; 1229efb7e53dSJordan K. Hubbard if (ch == '\0') 1230efb7e53dSJordan K. Hubbard goto done; 1231efb7e53dSJordan K. Hubbard fmt++; /* skip over '%' */ 1232efb7e53dSJordan K. Hubbard 1233efb7e53dSJordan K. Hubbard flags = 0; 1234efb7e53dSJordan K. Hubbard width = 0; 1235efb7e53dSJordan K. Hubbard 1236efb7e53dSJordan K. Hubbard rflag: ch = *fmt++; 1237efb7e53dSJordan K. Hubbard reswitch: switch (ch) { 1238efb7e53dSJordan K. Hubbard case ' ': 1239efb7e53dSJordan K. Hubbard case '#': 1240efb7e53dSJordan K. Hubbard goto rflag; 1241efb7e53dSJordan K. Hubbard case '*': 1242efb7e53dSJordan K. Hubbard ADDASTER (); 1243efb7e53dSJordan K. Hubbard goto rflag; 1244efb7e53dSJordan K. Hubbard case '-': 1245efb7e53dSJordan K. Hubbard case '+': 12467735bb0fSBill Fenner case '\'': 1247efb7e53dSJordan K. Hubbard goto rflag; 1248efb7e53dSJordan K. Hubbard case '.': 1249efb7e53dSJordan K. Hubbard if ((ch = *fmt++) == '*') { 1250efb7e53dSJordan K. Hubbard ADDASTER (); 1251efb7e53dSJordan K. Hubbard goto rflag; 1252efb7e53dSJordan K. Hubbard } 1253efb7e53dSJordan K. Hubbard while (is_digit(ch)) { 1254efb7e53dSJordan K. Hubbard ch = *fmt++; 1255efb7e53dSJordan K. Hubbard } 1256efb7e53dSJordan K. Hubbard goto reswitch; 1257efb7e53dSJordan K. Hubbard case '0': 1258efb7e53dSJordan K. Hubbard goto rflag; 1259efb7e53dSJordan K. Hubbard case '1': case '2': case '3': case '4': 1260efb7e53dSJordan K. Hubbard case '5': case '6': case '7': case '8': case '9': 1261efb7e53dSJordan K. Hubbard n = 0; 1262efb7e53dSJordan K. Hubbard do { 1263efb7e53dSJordan K. Hubbard n = 10 * n + to_digit(ch); 1264efb7e53dSJordan K. Hubbard ch = *fmt++; 1265efb7e53dSJordan K. Hubbard } while (is_digit(ch)); 1266efb7e53dSJordan K. Hubbard if (ch == '$') { 1267efb7e53dSJordan K. Hubbard nextarg = n; 1268efb7e53dSJordan K. Hubbard goto rflag; 1269efb7e53dSJordan K. Hubbard } 1270efb7e53dSJordan K. Hubbard width = n; 1271efb7e53dSJordan K. Hubbard goto reswitch; 1272efb7e53dSJordan K. Hubbard #ifdef FLOATING_POINT 1273efb7e53dSJordan K. Hubbard case 'L': 1274efb7e53dSJordan K. Hubbard flags |= LONGDBL; 1275efb7e53dSJordan K. Hubbard goto rflag; 1276efb7e53dSJordan K. Hubbard #endif 1277efb7e53dSJordan K. Hubbard case 'h': 12787735bb0fSBill Fenner if (flags & SHORTINT) { 12797735bb0fSBill Fenner flags &= ~SHORTINT; 12807735bb0fSBill Fenner flags |= CHARINT; 12817735bb0fSBill Fenner } else 1282efb7e53dSJordan K. Hubbard flags |= SHORTINT; 1283efb7e53dSJordan K. Hubbard goto rflag; 12847735bb0fSBill Fenner case 'j': 12857735bb0fSBill Fenner flags |= INTMAXT; 12867735bb0fSBill Fenner goto rflag; 1287efb7e53dSJordan K. Hubbard case 'l': 12887735bb0fSBill Fenner if (flags & LONGINT) { 12897735bb0fSBill Fenner flags &= ~LONGINT; 12907735bb0fSBill Fenner flags |= LLONGINT; 12917735bb0fSBill Fenner } else 1292efb7e53dSJordan K. Hubbard flags |= LONGINT; 1293efb7e53dSJordan K. Hubbard goto rflag; 1294efb7e53dSJordan K. Hubbard case 'q': 12957735bb0fSBill Fenner flags |= LLONGINT; /* not necessarily */ 12967735bb0fSBill Fenner goto rflag; 12977735bb0fSBill Fenner case 't': 12987735bb0fSBill Fenner flags |= PTRDIFFT; 12997735bb0fSBill Fenner goto rflag; 13007735bb0fSBill Fenner case 'z': 13017735bb0fSBill Fenner flags |= SIZET; 1302efb7e53dSJordan K. Hubbard goto rflag; 1303927ecbf3STim J. Robbins case 'C': 1304927ecbf3STim J. Robbins flags |= LONGINT; 1305927ecbf3STim J. Robbins /*FALLTHROUGH*/ 1306efb7e53dSJordan K. Hubbard case 'c': 1307b9aac308STim J. Robbins if (flags & LONGINT) 1308b9aac308STim J. Robbins ADDTYPE(T_WINT); 1309b9aac308STim J. Robbins else 1310efb7e53dSJordan K. Hubbard ADDTYPE(T_INT); 1311efb7e53dSJordan K. Hubbard break; 1312efb7e53dSJordan K. Hubbard case 'D': 1313efb7e53dSJordan K. Hubbard flags |= LONGINT; 1314efb7e53dSJordan K. Hubbard /*FALLTHROUGH*/ 1315efb7e53dSJordan K. Hubbard case 'd': 1316efb7e53dSJordan K. Hubbard case 'i': 1317efb7e53dSJordan K. Hubbard ADDSARG(); 1318efb7e53dSJordan K. Hubbard break; 1319efb7e53dSJordan K. Hubbard #ifdef FLOATING_POINT 13207735bb0fSBill Fenner #ifdef HEXFLOAT 13217735bb0fSBill Fenner case 'a': 13227735bb0fSBill Fenner case 'A': 13237735bb0fSBill Fenner #endif 1324efb7e53dSJordan K. Hubbard case 'e': 1325efb7e53dSJordan K. Hubbard case 'E': 1326efb7e53dSJordan K. Hubbard case 'f': 1327efb7e53dSJordan K. Hubbard case 'g': 1328efb7e53dSJordan K. Hubbard case 'G': 1329efb7e53dSJordan K. Hubbard if (flags & LONGDBL) 1330efb7e53dSJordan K. Hubbard ADDTYPE(T_LONG_DOUBLE); 1331efb7e53dSJordan K. Hubbard else 1332efb7e53dSJordan K. Hubbard ADDTYPE(T_DOUBLE); 1333efb7e53dSJordan K. Hubbard break; 1334efb7e53dSJordan K. Hubbard #endif /* FLOATING_POINT */ 1335efb7e53dSJordan K. Hubbard case 'n': 13367735bb0fSBill Fenner if (flags & INTMAXT) 13377735bb0fSBill Fenner ADDTYPE(TP_INTMAXT); 13387735bb0fSBill Fenner else if (flags & PTRDIFFT) 13397735bb0fSBill Fenner ADDTYPE(TP_PTRDIFFT); 13407735bb0fSBill Fenner else if (flags & SIZET) 13417735bb0fSBill Fenner ADDTYPE(TP_SIZET); 13427735bb0fSBill Fenner else if (flags & LLONGINT) 13437735bb0fSBill Fenner ADDTYPE(TP_LLONG); 1344efb7e53dSJordan K. Hubbard else if (flags & LONGINT) 1345efb7e53dSJordan K. Hubbard ADDTYPE(TP_LONG); 1346efb7e53dSJordan K. Hubbard else if (flags & SHORTINT) 1347efb7e53dSJordan K. Hubbard ADDTYPE(TP_SHORT); 13487735bb0fSBill Fenner else if (flags & CHARINT) 13497735bb0fSBill Fenner ADDTYPE(TP_SCHAR); 1350efb7e53dSJordan K. Hubbard else 1351efb7e53dSJordan K. Hubbard ADDTYPE(TP_INT); 1352efb7e53dSJordan K. Hubbard continue; /* no output */ 1353efb7e53dSJordan K. Hubbard case 'O': 1354efb7e53dSJordan K. Hubbard flags |= LONGINT; 1355efb7e53dSJordan K. Hubbard /*FALLTHROUGH*/ 1356efb7e53dSJordan K. Hubbard case 'o': 1357efb7e53dSJordan K. Hubbard ADDUARG(); 1358efb7e53dSJordan K. Hubbard break; 1359efb7e53dSJordan K. Hubbard case 'p': 1360efb7e53dSJordan K. Hubbard ADDTYPE(TP_VOID); 1361efb7e53dSJordan K. Hubbard break; 1362927ecbf3STim J. Robbins case 'S': 1363927ecbf3STim J. Robbins flags |= LONGINT; 1364927ecbf3STim J. Robbins /*FALLTHROUGH*/ 1365efb7e53dSJordan K. Hubbard case 's': 1366b9aac308STim J. Robbins if (flags & LONGINT) 1367b9aac308STim J. Robbins ADDTYPE(TP_WCHAR); 1368b9aac308STim J. Robbins else 1369efb7e53dSJordan K. Hubbard ADDTYPE(TP_CHAR); 1370efb7e53dSJordan K. Hubbard break; 1371efb7e53dSJordan K. Hubbard case 'U': 1372efb7e53dSJordan K. Hubbard flags |= LONGINT; 1373efb7e53dSJordan K. Hubbard /*FALLTHROUGH*/ 1374efb7e53dSJordan K. Hubbard case 'u': 1375efb7e53dSJordan K. Hubbard case 'X': 1376efb7e53dSJordan K. Hubbard case 'x': 1377efb7e53dSJordan K. Hubbard ADDUARG(); 1378efb7e53dSJordan K. Hubbard break; 1379efb7e53dSJordan K. Hubbard default: /* "%?" prints ?, unless ? is NUL */ 1380efb7e53dSJordan K. Hubbard if (ch == '\0') 1381efb7e53dSJordan K. Hubbard goto done; 1382efb7e53dSJordan K. Hubbard break; 1383efb7e53dSJordan K. Hubbard } 1384efb7e53dSJordan K. Hubbard } 1385efb7e53dSJordan K. Hubbard done: 1386efb7e53dSJordan K. Hubbard /* 1387efb7e53dSJordan K. Hubbard * Build the argument table. 1388efb7e53dSJordan K. Hubbard */ 1389efb7e53dSJordan K. Hubbard if (tablemax >= STATIC_ARG_TBL_SIZE) { 1390a387081cSDoug Rabson *argtable = (union arg *) 1391a387081cSDoug Rabson malloc (sizeof (union arg) * (tablemax + 1)); 1392efb7e53dSJordan K. Hubbard } 1393efb7e53dSJordan K. Hubbard 1394a387081cSDoug Rabson (*argtable) [0].intarg = 0; 1395efb7e53dSJordan K. Hubbard for (n = 1; n <= tablemax; n++) { 1396efb7e53dSJordan K. Hubbard switch (typetable [n]) { 13977735bb0fSBill Fenner case T_UNUSED: /* whoops! */ 1398a387081cSDoug Rabson (*argtable) [n].intarg = va_arg (ap, int); 1399efb7e53dSJordan K. Hubbard break; 14007735bb0fSBill Fenner case TP_SCHAR: 14017735bb0fSBill Fenner (*argtable) [n].pschararg = va_arg (ap, signed char *); 1402efb7e53dSJordan K. Hubbard break; 1403efb7e53dSJordan K. Hubbard case TP_SHORT: 1404a387081cSDoug Rabson (*argtable) [n].pshortarg = va_arg (ap, short *); 1405efb7e53dSJordan K. Hubbard break; 1406efb7e53dSJordan K. Hubbard case T_INT: 1407a387081cSDoug Rabson (*argtable) [n].intarg = va_arg (ap, int); 1408efb7e53dSJordan K. Hubbard break; 1409efb7e53dSJordan K. Hubbard case T_U_INT: 1410a387081cSDoug Rabson (*argtable) [n].uintarg = va_arg (ap, unsigned int); 1411efb7e53dSJordan K. Hubbard break; 1412efb7e53dSJordan K. Hubbard case TP_INT: 1413a387081cSDoug Rabson (*argtable) [n].pintarg = va_arg (ap, int *); 1414efb7e53dSJordan K. Hubbard break; 1415efb7e53dSJordan K. Hubbard case T_LONG: 1416a387081cSDoug Rabson (*argtable) [n].longarg = va_arg (ap, long); 1417efb7e53dSJordan K. Hubbard break; 1418efb7e53dSJordan K. Hubbard case T_U_LONG: 1419a387081cSDoug Rabson (*argtable) [n].ulongarg = va_arg (ap, unsigned long); 1420efb7e53dSJordan K. Hubbard break; 1421efb7e53dSJordan K. Hubbard case TP_LONG: 1422a387081cSDoug Rabson (*argtable) [n].plongarg = va_arg (ap, long *); 1423efb7e53dSJordan K. Hubbard break; 14247735bb0fSBill Fenner case T_LLONG: 14257735bb0fSBill Fenner (*argtable) [n].longlongarg = va_arg (ap, long long); 1426efb7e53dSJordan K. Hubbard break; 14277735bb0fSBill Fenner case T_U_LLONG: 14287735bb0fSBill Fenner (*argtable) [n].ulonglongarg = va_arg (ap, unsigned long long); 1429efb7e53dSJordan K. Hubbard break; 14307735bb0fSBill Fenner case TP_LLONG: 14317735bb0fSBill Fenner (*argtable) [n].plonglongarg = va_arg (ap, long long *); 14327735bb0fSBill Fenner break; 14337735bb0fSBill Fenner case T_PTRDIFFT: 14347735bb0fSBill Fenner (*argtable) [n].ptrdiffarg = va_arg (ap, ptrdiff_t); 14357735bb0fSBill Fenner break; 14367735bb0fSBill Fenner case TP_PTRDIFFT: 14377735bb0fSBill Fenner (*argtable) [n].pptrdiffarg = va_arg (ap, ptrdiff_t *); 14387735bb0fSBill Fenner break; 14397735bb0fSBill Fenner case T_SIZET: 14407735bb0fSBill Fenner (*argtable) [n].sizearg = va_arg (ap, size_t); 14417735bb0fSBill Fenner break; 14427735bb0fSBill Fenner case TP_SIZET: 14437735bb0fSBill Fenner (*argtable) [n].psizearg = va_arg (ap, ssize_t *); 14447735bb0fSBill Fenner break; 14457735bb0fSBill Fenner case T_INTMAXT: 14467735bb0fSBill Fenner (*argtable) [n].intmaxarg = va_arg (ap, intmax_t); 14477735bb0fSBill Fenner break; 14487735bb0fSBill Fenner case T_UINTMAXT: 14497735bb0fSBill Fenner (*argtable) [n].uintmaxarg = va_arg (ap, uintmax_t); 14507735bb0fSBill Fenner break; 14517735bb0fSBill Fenner case TP_INTMAXT: 14527735bb0fSBill Fenner (*argtable) [n].pintmaxarg = va_arg (ap, intmax_t *); 1453efb7e53dSJordan K. Hubbard break; 1454a387081cSDoug Rabson #ifdef FLOATING_POINT 1455efb7e53dSJordan K. Hubbard case T_DOUBLE: 1456a387081cSDoug Rabson (*argtable) [n].doublearg = va_arg (ap, double); 1457efb7e53dSJordan K. Hubbard break; 1458efb7e53dSJordan K. Hubbard case T_LONG_DOUBLE: 1459a387081cSDoug Rabson (*argtable) [n].longdoublearg = va_arg (ap, long double); 1460efb7e53dSJordan K. Hubbard break; 1461a387081cSDoug Rabson #endif 1462efb7e53dSJordan K. Hubbard case TP_CHAR: 1463a387081cSDoug Rabson (*argtable) [n].pchararg = va_arg (ap, char *); 1464efb7e53dSJordan K. Hubbard break; 1465efb7e53dSJordan K. Hubbard case TP_VOID: 1466a387081cSDoug Rabson (*argtable) [n].pvoidarg = va_arg (ap, void *); 1467efb7e53dSJordan K. Hubbard break; 1468b9aac308STim J. Robbins case T_WINT: 1469b9aac308STim J. Robbins (*argtable) [n].wintarg = va_arg (ap, wint_t); 1470b9aac308STim J. Robbins break; 1471b9aac308STim J. Robbins case TP_WCHAR: 1472b9aac308STim J. Robbins (*argtable) [n].pwchararg = va_arg (ap, wchar_t *); 1473b9aac308STim J. Robbins break; 1474efb7e53dSJordan K. Hubbard } 1475efb7e53dSJordan K. Hubbard } 1476efb7e53dSJordan K. Hubbard 1477efb7e53dSJordan K. Hubbard if ((typetable != NULL) && (typetable != stattypetable)) 1478efb7e53dSJordan K. Hubbard free (typetable); 1479efb7e53dSJordan K. Hubbard } 1480efb7e53dSJordan K. Hubbard 1481efb7e53dSJordan K. Hubbard /* 1482efb7e53dSJordan K. Hubbard * Increase the size of the type table. 1483efb7e53dSJordan K. Hubbard */ 1484efb7e53dSJordan K. Hubbard static void 14857735bb0fSBill Fenner __grow_type_table (int nextarg, enum typeid **typetable, int *tablesize) 1486efb7e53dSJordan K. Hubbard { 14877735bb0fSBill Fenner enum typeid *const oldtable = *typetable; 148842cebaa5SArchie Cobbs const int oldsize = *tablesize; 14897735bb0fSBill Fenner enum typeid *newtable; 149042cebaa5SArchie Cobbs int newsize = oldsize * 2; 1491efb7e53dSJordan K. Hubbard 149242cebaa5SArchie Cobbs if (newsize < nextarg + 1) 149342cebaa5SArchie Cobbs newsize = nextarg + 1; 149442cebaa5SArchie Cobbs if (oldsize == STATIC_ARG_TBL_SIZE) { 149542cebaa5SArchie Cobbs if ((newtable = malloc(newsize)) == NULL) 149642cebaa5SArchie Cobbs abort(); /* XXX handle better */ 149742cebaa5SArchie Cobbs bcopy(oldtable, newtable, oldsize); 1498efb7e53dSJordan K. Hubbard } else { 149942cebaa5SArchie Cobbs if ((newtable = reallocf(oldtable, newsize)) == NULL) 150042cebaa5SArchie Cobbs abort(); /* XXX handle better */ 1501efb7e53dSJordan K. Hubbard } 150242cebaa5SArchie Cobbs memset(&newtable[oldsize], T_UNUSED, newsize - oldsize); 1503efb7e53dSJordan K. Hubbard 150442cebaa5SArchie Cobbs *typetable = newtable; 1505efb7e53dSJordan K. Hubbard *tablesize = newsize; 1506efb7e53dSJordan K. Hubbard } 1507efb7e53dSJordan K. Hubbard 1508efb7e53dSJordan K. Hubbard 150958f0484fSRodney W. Grimes #ifdef FLOATING_POINT 151058f0484fSRodney W. Grimes 1511c05ac53bSDavid E. O'Brien extern char *__dtoa(double, int, int, int *, int *, char **, char **); 151258f0484fSRodney W. Grimes 151358f0484fSRodney W. Grimes static char * 1514d201fe46SDaniel Eischen cvt(double value, int ndigits, int flags, char *sign, int *decpt, 15152ffc61baSTor Egge int ch, int *length, char **dtoaresultp) 151658f0484fSRodney W. Grimes { 151758f0484fSRodney W. Grimes int mode, dsgn; 151858f0484fSRodney W. Grimes char *digits, *bp, *rve; 151958f0484fSRodney W. Grimes 152058f0484fSRodney W. Grimes if (ch == 'f') 1521d26be6f0SBruce Evans mode = 3; /* ndigits after the decimal point */ 152258f0484fSRodney W. Grimes else { 1523d26be6f0SBruce Evans /* 1524d26be6f0SBruce Evans * To obtain ndigits after the decimal point for the 'e' 1525d26be6f0SBruce Evans * and 'E' formats, round to ndigits + 1 significant 1526d26be6f0SBruce Evans * figures. 1527d26be6f0SBruce Evans */ 1528d26be6f0SBruce Evans if (ch == 'e' || ch == 'E') 1529d26be6f0SBruce Evans ndigits++; 1530d26be6f0SBruce Evans mode = 2; /* ndigits significant digits */ 153158f0484fSRodney W. Grimes } 15323dd65760SJens Schweikhardt digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve, dtoaresultp); 15333dd65760SJens Schweikhardt *sign = dsgn != 0; 1534d26be6f0SBruce Evans if ((ch != 'g' && ch != 'G') || flags & ALT) { 1535d26be6f0SBruce Evans /* print trailing zeros */ 153658f0484fSRodney W. Grimes bp = digits + ndigits; 153758f0484fSRodney W. Grimes if (ch == 'f') { 153858f0484fSRodney W. Grimes if (*digits == '0' && value) 153958f0484fSRodney W. Grimes *decpt = -ndigits + 1; 154058f0484fSRodney W. Grimes bp += *decpt; 154158f0484fSRodney W. Grimes } 154258f0484fSRodney W. Grimes if (value == 0) /* kludge for __dtoa irregularity */ 154358f0484fSRodney W. Grimes rve = bp; 154458f0484fSRodney W. Grimes while (rve < bp) 154558f0484fSRodney W. Grimes *rve++ = '0'; 154658f0484fSRodney W. Grimes } 154758f0484fSRodney W. Grimes *length = rve - digits; 154858f0484fSRodney W. Grimes return (digits); 154958f0484fSRodney W. Grimes } 155058f0484fSRodney W. Grimes 155158f0484fSRodney W. Grimes static int 1552d201fe46SDaniel Eischen exponent(char *p0, int exp, int fmtch) 155358f0484fSRodney W. Grimes { 1554d201fe46SDaniel Eischen char *p, *t; 155558f0484fSRodney W. Grimes char expbuf[MAXEXP]; 155658f0484fSRodney W. Grimes 155758f0484fSRodney W. Grimes p = p0; 155858f0484fSRodney W. Grimes *p++ = fmtch; 155958f0484fSRodney W. Grimes if (exp < 0) { 156058f0484fSRodney W. Grimes exp = -exp; 156158f0484fSRodney W. Grimes *p++ = '-'; 156258f0484fSRodney W. Grimes } 156358f0484fSRodney W. Grimes else 156458f0484fSRodney W. Grimes *p++ = '+'; 156558f0484fSRodney W. Grimes t = expbuf + MAXEXP; 156658f0484fSRodney W. Grimes if (exp > 9) { 156758f0484fSRodney W. Grimes do { 156858f0484fSRodney W. Grimes *--t = to_char(exp % 10); 156958f0484fSRodney W. Grimes } while ((exp /= 10) > 9); 157058f0484fSRodney W. Grimes *--t = to_char(exp); 157158f0484fSRodney W. Grimes for (; t < expbuf + MAXEXP; *p++ = *t++); 157258f0484fSRodney W. Grimes } 157358f0484fSRodney W. Grimes else { 157458f0484fSRodney W. Grimes *p++ = '0'; 157558f0484fSRodney W. Grimes *p++ = to_char(exp); 157658f0484fSRodney W. Grimes } 157758f0484fSRodney W. Grimes return (p - p0); 157858f0484fSRodney W. Grimes } 157958f0484fSRodney W. Grimes #endif /* FLOATING_POINT */ 1580