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; 614efb7e53dSJordan K. Hubbard 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; 75158f0484fSRodney W. Grimes case 'c': 752b9aac308STim J. Robbins if (flags & LONGINT) { 753b9aac308STim J. Robbins mbstate_t mbs; 754b9aac308STim J. Robbins size_t mbseqlen; 755b9aac308STim J. Robbins 756b9aac308STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 757b9aac308STim J. Robbins mbseqlen = wcrtomb(cp = buf, 758b9aac308STim J. Robbins (wchar_t)GETARG(wint_t), &mbs); 759b9aac308STim J. Robbins if (mbseqlen == (size_t)-1) 760b9aac308STim J. Robbins goto error; 761b9aac308STim J. Robbins size = (int)mbseqlen; 762b9aac308STim J. Robbins } else { 763efb7e53dSJordan K. Hubbard *(cp = buf) = GETARG(int); 76458f0484fSRodney W. Grimes size = 1; 765b9aac308STim J. Robbins } 76658f0484fSRodney W. Grimes sign = '\0'; 76758f0484fSRodney W. Grimes break; 76858f0484fSRodney W. Grimes case 'D': 76958f0484fSRodney W. Grimes flags |= LONGINT; 77058f0484fSRodney W. Grimes /*FALLTHROUGH*/ 77158f0484fSRodney W. Grimes case 'd': 77258f0484fSRodney W. Grimes case 'i': 7737735bb0fSBill Fenner if (flags & INTMAX_SIZE) { 7747735bb0fSBill Fenner ujval = SJARG(); 7757735bb0fSBill Fenner if ((intmax_t)ujval < 0) { 7767735bb0fSBill Fenner ujval = -ujval; 77758f0484fSRodney W. Grimes sign = '-'; 77858f0484fSRodney W. Grimes } 77958f0484fSRodney W. Grimes } else { 78058f0484fSRodney W. Grimes ulval = SARG(); 78158f0484fSRodney W. Grimes if ((long)ulval < 0) { 78258f0484fSRodney W. Grimes ulval = -ulval; 78358f0484fSRodney W. Grimes sign = '-'; 78458f0484fSRodney W. Grimes } 78558f0484fSRodney W. Grimes } 78658f0484fSRodney W. Grimes base = 10; 78758f0484fSRodney W. Grimes goto number; 78858f0484fSRodney W. Grimes #ifdef FLOATING_POINT 7897735bb0fSBill Fenner #ifdef HEXFLOAT 7907735bb0fSBill Fenner case 'a': 7917735bb0fSBill Fenner case 'A': 7927735bb0fSBill Fenner #endif 793d26be6f0SBruce Evans case 'e': 79458f0484fSRodney W. Grimes case 'E': 7952e394b2fSAlexey Zelkin /*- 79698ee7635SAlexey Zelkin * Grouping apply to %i, %d, %u, %f, %F, %g, %G 79798ee7635SAlexey Zelkin * conversion specifiers only. For other conversions 79898ee7635SAlexey Zelkin * behavior is undefined. 79998ee7635SAlexey Zelkin * -- POSIX 80098ee7635SAlexey Zelkin */ 80198ee7635SAlexey Zelkin flags &= ~GROUPING; 80298ee7635SAlexey Zelkin /*FALLTHROUGH*/ 803d26be6f0SBruce Evans case 'f': 8047735bb0fSBill Fenner case 'F': 805d26be6f0SBruce Evans goto fp_begin; 80658f0484fSRodney W. Grimes case 'g': 80758f0484fSRodney W. Grimes case 'G': 808d26be6f0SBruce Evans if (prec == 0) 809d26be6f0SBruce Evans prec = 1; 810d26be6f0SBruce Evans fp_begin: if (prec == -1) 81158f0484fSRodney W. Grimes prec = DEFPREC; 812d26be6f0SBruce Evans if (flags & LONGDBL) 8136a93659fSBruce Evans /* XXX this loses precision. */ 814efb7e53dSJordan K. Hubbard _double = (double)GETARG(long double); 815d26be6f0SBruce Evans else 816efb7e53dSJordan K. Hubbard _double = GETARG(double); 81758f0484fSRodney W. Grimes /* do this before tricky precision changes */ 81858f0484fSRodney W. Grimes if (isinf(_double)) { 81958f0484fSRodney W. Grimes if (_double < 0) 82058f0484fSRodney W. Grimes sign = '-'; 8217735bb0fSBill Fenner if (isupper(ch)) 8227735bb0fSBill Fenner cp = "INF"; 8237735bb0fSBill Fenner else 8247735bb0fSBill Fenner cp = "inf"; 82558f0484fSRodney W. Grimes size = 3; 82658f0484fSRodney W. Grimes break; 82758f0484fSRodney W. Grimes } 82858f0484fSRodney W. Grimes if (isnan(_double)) { 8297735bb0fSBill Fenner if (isupper(ch)) 8307735bb0fSBill Fenner cp = "NAN"; 8317735bb0fSBill Fenner else 8327735bb0fSBill Fenner cp = "nan"; 83358f0484fSRodney W. Grimes size = 3; 83458f0484fSRodney W. Grimes break; 83558f0484fSRodney W. Grimes } 83658f0484fSRodney W. Grimes flags |= FPT; 8372ffc61baSTor Egge if (dtoaresult != NULL) { 8382ffc61baSTor Egge free(dtoaresult); 8392ffc61baSTor Egge dtoaresult = NULL; 8402ffc61baSTor Egge } 84158f0484fSRodney W. Grimes cp = cvt(_double, prec, flags, &softsign, 8422ffc61baSTor Egge &expt, ch, &ndig, &dtoaresult); 84358f0484fSRodney W. Grimes if (ch == 'g' || ch == 'G') { 84458f0484fSRodney W. Grimes if (expt <= -4 || expt > prec) 84558f0484fSRodney W. Grimes ch = (ch == 'g') ? 'e' : 'E'; 84658f0484fSRodney W. Grimes else 84758f0484fSRodney W. Grimes ch = 'g'; 84858f0484fSRodney W. Grimes } 8497735bb0fSBill Fenner if (ch == 'e' || ch == 'E') { 85058f0484fSRodney W. Grimes --expt; 85158f0484fSRodney W. Grimes expsize = exponent(expstr, expt, ch); 85258f0484fSRodney W. Grimes size = expsize + ndig; 85358f0484fSRodney W. Grimes if (ndig > 1 || flags & ALT) 85458f0484fSRodney W. Grimes ++size; 8557735bb0fSBill Fenner } else if (ch == 'f' || ch == 'F') { 85658f0484fSRodney W. Grimes if (expt > 0) { 85758f0484fSRodney W. Grimes size = expt; 85858f0484fSRodney W. Grimes if (prec || flags & ALT) 85958f0484fSRodney W. Grimes size += prec + 1; 86058f0484fSRodney W. Grimes } else /* "0.X" */ 86158f0484fSRodney W. Grimes size = prec + 2; 86258f0484fSRodney W. Grimes } else if (expt >= ndig) { /* fixed g fmt */ 86358f0484fSRodney W. Grimes size = expt; 86458f0484fSRodney W. Grimes if (flags & ALT) 86558f0484fSRodney W. Grimes ++size; 86658f0484fSRodney W. Grimes } else 86758f0484fSRodney W. Grimes size = ndig + (expt > 0 ? 86858f0484fSRodney W. Grimes 1 : 2 - expt); 86958f0484fSRodney W. Grimes 87058f0484fSRodney W. Grimes if (softsign) 87158f0484fSRodney W. Grimes sign = '-'; 87258f0484fSRodney W. Grimes break; 87358f0484fSRodney W. Grimes #endif /* FLOATING_POINT */ 87458f0484fSRodney W. Grimes case 'n': 8757735bb0fSBill Fenner /* 8767735bb0fSBill Fenner * Assignment-like behavior is specified if the 8777735bb0fSBill Fenner * value overflows or is otherwise unrepresentable. 8787735bb0fSBill Fenner * C99 says to use `signed char' for %hhn conversions. 8797735bb0fSBill Fenner */ 8807735bb0fSBill Fenner if (flags & LLONGINT) 8817735bb0fSBill Fenner *GETARG(long long *) = ret; 8827735bb0fSBill Fenner else if (flags & SIZET) 8837735bb0fSBill Fenner *GETARG(ssize_t *) = (ssize_t)ret; 8847735bb0fSBill Fenner else if (flags & PTRDIFFT) 8857735bb0fSBill Fenner *GETARG(ptrdiff_t *) = ret; 8867735bb0fSBill Fenner else if (flags & INTMAXT) 8877735bb0fSBill Fenner *GETARG(intmax_t *) = ret; 88858f0484fSRodney W. Grimes else if (flags & LONGINT) 8896e690ad4SAndrey A. Chernov *GETARG(long *) = ret; 89058f0484fSRodney W. Grimes else if (flags & SHORTINT) 8916e690ad4SAndrey A. Chernov *GETARG(short *) = ret; 8927735bb0fSBill Fenner else if (flags & CHARINT) 8937735bb0fSBill Fenner *GETARG(signed char *) = ret; 89458f0484fSRodney W. Grimes else 8956e690ad4SAndrey A. Chernov *GETARG(int *) = ret; 89658f0484fSRodney W. Grimes continue; /* no output */ 89758f0484fSRodney W. Grimes case 'O': 89858f0484fSRodney W. Grimes flags |= LONGINT; 89958f0484fSRodney W. Grimes /*FALLTHROUGH*/ 90058f0484fSRodney W. Grimes case 'o': 9017735bb0fSBill Fenner if (flags & INTMAX_SIZE) 9027735bb0fSBill Fenner ujval = UJARG(); 90358f0484fSRodney W. Grimes else 90458f0484fSRodney W. Grimes ulval = UARG(); 90558f0484fSRodney W. Grimes base = 8; 90658f0484fSRodney W. Grimes goto nosign; 90758f0484fSRodney W. Grimes case 'p': 9082e394b2fSAlexey Zelkin /*- 90958f0484fSRodney W. Grimes * ``The argument shall be a pointer to void. The 91058f0484fSRodney W. Grimes * value of the pointer is converted to a sequence 91158f0484fSRodney W. Grimes * of printable characters, in an implementation- 91258f0484fSRodney W. Grimes * defined manner.'' 91358f0484fSRodney W. Grimes * -- ANSI X3J11 91458f0484fSRodney W. Grimes */ 9157735bb0fSBill Fenner ujval = (uintmax_t)(uintptr_t)GETARG(void *); 91658f0484fSRodney W. Grimes base = 16; 91758f0484fSRodney W. Grimes xdigs = "0123456789abcdef"; 9187735bb0fSBill Fenner flags = flags | INTMAXT | HEXPREFIX; 91958f0484fSRodney W. Grimes ch = 'x'; 92058f0484fSRodney W. Grimes goto nosign; 92158f0484fSRodney W. Grimes case 's': 922b9aac308STim J. Robbins if (flags & LONGINT) { 923b9aac308STim J. Robbins wchar_t *wcp; 924b9aac308STim J. Robbins 925b9aac308STim J. Robbins if (convbuf != NULL) 926b9aac308STim J. Robbins free(convbuf); 927b9aac308STim J. Robbins if ((wcp = GETARG(wchar_t *)) == NULL) 928b9aac308STim J. Robbins cp = "(null)"; 929b9aac308STim J. Robbins else { 930b9aac308STim J. Robbins convbuf = __wcsconv(wcp, prec); 931b9aac308STim J. Robbins if (convbuf == NULL) 932b9aac308STim J. Robbins goto error; 933b9aac308STim J. Robbins cp = convbuf; 934b9aac308STim J. Robbins } 935b9aac308STim J. Robbins } else if ((cp = GETARG(char *)) == NULL) 93658f0484fSRodney W. Grimes cp = "(null)"; 93758f0484fSRodney W. Grimes if (prec >= 0) { 93858f0484fSRodney W. Grimes /* 93958f0484fSRodney W. Grimes * can't use strlen; can only look for the 94058f0484fSRodney W. Grimes * NUL in the first `prec' characters, and 94158f0484fSRodney W. Grimes * strlen() will go further. 94258f0484fSRodney W. Grimes */ 943ce51cf03SJames Raynard char *p = memchr(cp, 0, (size_t)prec); 94458f0484fSRodney W. Grimes 94558f0484fSRodney W. Grimes if (p != NULL) { 94658f0484fSRodney W. Grimes size = p - cp; 94758f0484fSRodney W. Grimes if (size > prec) 94858f0484fSRodney W. Grimes size = prec; 94958f0484fSRodney W. Grimes } else 95058f0484fSRodney W. Grimes size = prec; 95158f0484fSRodney W. Grimes } else 95258f0484fSRodney W. Grimes size = strlen(cp); 95358f0484fSRodney W. Grimes sign = '\0'; 95458f0484fSRodney W. Grimes break; 95558f0484fSRodney W. Grimes case 'U': 95658f0484fSRodney W. Grimes flags |= LONGINT; 95758f0484fSRodney W. Grimes /*FALLTHROUGH*/ 95858f0484fSRodney W. Grimes case 'u': 9597735bb0fSBill Fenner if (flags & INTMAX_SIZE) 9607735bb0fSBill Fenner ujval = UJARG(); 96158f0484fSRodney W. Grimes else 96258f0484fSRodney W. Grimes ulval = UARG(); 96358f0484fSRodney W. Grimes base = 10; 96458f0484fSRodney W. Grimes goto nosign; 96558f0484fSRodney W. Grimes case 'X': 96658f0484fSRodney W. Grimes xdigs = "0123456789ABCDEF"; 96758f0484fSRodney W. Grimes goto hex; 96858f0484fSRodney W. Grimes case 'x': 96958f0484fSRodney W. Grimes xdigs = "0123456789abcdef"; 9707735bb0fSBill Fenner hex: 9717735bb0fSBill Fenner if (flags & INTMAX_SIZE) 9727735bb0fSBill Fenner ujval = UJARG(); 97358f0484fSRodney W. Grimes else 97458f0484fSRodney W. Grimes ulval = UARG(); 97558f0484fSRodney W. Grimes base = 16; 97658f0484fSRodney W. Grimes /* leading 0x/X only if non-zero */ 97758f0484fSRodney W. Grimes if (flags & ALT && 9787735bb0fSBill Fenner (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0)) 97958f0484fSRodney W. Grimes flags |= HEXPREFIX; 98058f0484fSRodney W. Grimes 98198ee7635SAlexey Zelkin flags &= ~GROUPING; 98258f0484fSRodney W. Grimes /* unsigned conversions */ 98358f0484fSRodney W. Grimes nosign: sign = '\0'; 9842e394b2fSAlexey Zelkin /*- 98558f0484fSRodney W. Grimes * ``... diouXx conversions ... if a precision is 98658f0484fSRodney W. Grimes * specified, the 0 flag will be ignored.'' 98758f0484fSRodney W. Grimes * -- ANSI X3J11 98858f0484fSRodney W. Grimes */ 98958f0484fSRodney W. Grimes number: if ((dprec = prec) >= 0) 99058f0484fSRodney W. Grimes flags &= ~ZEROPAD; 99158f0484fSRodney W. Grimes 9922e394b2fSAlexey Zelkin /*- 99358f0484fSRodney W. Grimes * ``The result of converting a zero value with an 99458f0484fSRodney W. Grimes * explicit precision of zero is no characters.'' 99558f0484fSRodney W. Grimes * -- ANSI X3J11 99658f0484fSRodney W. Grimes */ 99758f0484fSRodney W. Grimes cp = buf + BUF; 9987735bb0fSBill Fenner if (flags & INTMAX_SIZE) { 9997735bb0fSBill Fenner if (ujval != 0 || prec != 0) 10007735bb0fSBill Fenner cp = __ujtoa(ujval, cp, base, 100198ee7635SAlexey Zelkin flags & ALT, xdigs, 100298ee7635SAlexey Zelkin flags & GROUPING, thousands_sep, 100398ee7635SAlexey Zelkin grouping); 100458f0484fSRodney W. Grimes } else { 100558f0484fSRodney W. Grimes if (ulval != 0 || prec != 0) 100658f0484fSRodney W. Grimes cp = __ultoa(ulval, cp, base, 100798ee7635SAlexey Zelkin flags & ALT, xdigs, 100898ee7635SAlexey Zelkin flags & GROUPING, thousands_sep, 100998ee7635SAlexey Zelkin grouping); 101058f0484fSRodney W. Grimes } 101158f0484fSRodney W. Grimes size = buf + BUF - cp; 101258f0484fSRodney W. Grimes break; 101358f0484fSRodney W. Grimes default: /* "%?" prints ?, unless ? is NUL */ 101458f0484fSRodney W. Grimes if (ch == '\0') 101558f0484fSRodney W. Grimes goto done; 101658f0484fSRodney W. Grimes /* pretend it was %c with argument ch */ 101758f0484fSRodney W. Grimes cp = buf; 101858f0484fSRodney W. Grimes *cp = ch; 101958f0484fSRodney W. Grimes size = 1; 102058f0484fSRodney W. Grimes sign = '\0'; 102158f0484fSRodney W. Grimes break; 102258f0484fSRodney W. Grimes } 102358f0484fSRodney W. Grimes 102458f0484fSRodney W. Grimes /* 102558f0484fSRodney W. Grimes * All reasonable formats wind up here. At this point, `cp' 102658f0484fSRodney W. Grimes * points to a string which (if not flags&LADJUST) should be 102758f0484fSRodney W. Grimes * padded out to `width' places. If flags&ZEROPAD, it should 102858f0484fSRodney W. Grimes * first be prefixed by any sign or other prefix; otherwise, 102958f0484fSRodney W. Grimes * it should be blank padded before the prefix is emitted. 103058f0484fSRodney W. Grimes * After any left-hand padding and prefixing, emit zeroes 103158f0484fSRodney W. Grimes * required by a decimal [diouxX] precision, then print the 103258f0484fSRodney W. Grimes * string proper, then emit zeroes required by any leftover 103358f0484fSRodney W. Grimes * floating precision; finally, if LADJUST, pad with blanks. 103458f0484fSRodney W. Grimes * 103558f0484fSRodney W. Grimes * Compute actual size, so we know how much to pad. 1036261a532aSBill Fenner * size excludes decimal prec; realsz includes it. 103758f0484fSRodney W. Grimes */ 1038261a532aSBill Fenner realsz = dprec > size ? dprec : size; 103958f0484fSRodney W. Grimes if (sign) 1040261a532aSBill Fenner realsz++; 104158f0484fSRodney W. Grimes else if (flags & HEXPREFIX) 1042261a532aSBill Fenner realsz += 2; 104358f0484fSRodney W. Grimes 104492e88f87SAndrey A. Chernov prsize = width > realsz ? width : realsz; 1045b250f248SAndrey A. Chernov if ((unsigned)ret + prsize > INT_MAX) { 104692e88f87SAndrey A. Chernov ret = EOF; 104792e88f87SAndrey A. Chernov goto error; 104892e88f87SAndrey A. Chernov } 104992e88f87SAndrey A. Chernov 105058f0484fSRodney W. Grimes /* right-adjusting blank padding */ 105158f0484fSRodney W. Grimes if ((flags & (LADJUST|ZEROPAD)) == 0) 105258f0484fSRodney W. Grimes PAD(width - realsz, blanks); 105358f0484fSRodney W. Grimes 105458f0484fSRodney W. Grimes /* prefix */ 105558f0484fSRodney W. Grimes if (sign) { 105658f0484fSRodney W. Grimes PRINT(&sign, 1); 105758f0484fSRodney W. Grimes } else if (flags & HEXPREFIX) { 105858f0484fSRodney W. Grimes ox[0] = '0'; 105958f0484fSRodney W. Grimes ox[1] = ch; 106058f0484fSRodney W. Grimes PRINT(ox, 2); 106158f0484fSRodney W. Grimes } 106258f0484fSRodney W. Grimes 106358f0484fSRodney W. Grimes /* right-adjusting zero padding */ 106458f0484fSRodney W. Grimes if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) 106558f0484fSRodney W. Grimes PAD(width - realsz, zeroes); 106658f0484fSRodney W. Grimes 106758f0484fSRodney W. Grimes /* leading zeroes from decimal precision */ 1068261a532aSBill Fenner PAD(dprec - size, zeroes); 106958f0484fSRodney W. Grimes 107058f0484fSRodney W. Grimes /* the string or number proper */ 107158f0484fSRodney W. Grimes #ifdef FLOATING_POINT 107258f0484fSRodney W. Grimes if ((flags & FPT) == 0) { 107358f0484fSRodney W. Grimes PRINT(cp, size); 107458f0484fSRodney W. Grimes } else { /* glue together f_p fragments */ 107558f0484fSRodney W. Grimes if (ch >= 'f') { /* 'f' or 'g' */ 107658f0484fSRodney W. Grimes if (_double == 0) { 107758f0484fSRodney W. Grimes /* kludge for __dtoa irregularity */ 107858f0484fSRodney W. Grimes PRINT("0", 1); 10799ad80ab5SAndrey A. Chernov if (expt < ndig || (flags & ALT) != 0) { 10809ad80ab5SAndrey A. Chernov PRINT(decimal_point, 1); 108158f0484fSRodney W. Grimes PAD(ndig - 1, zeroes); 108258f0484fSRodney W. Grimes } 108358f0484fSRodney W. Grimes } else if (expt <= 0) { 10849ad80ab5SAndrey A. Chernov PRINT("0", 1); 10859ad80ab5SAndrey A. Chernov PRINT(decimal_point, 1); 108658f0484fSRodney W. Grimes PAD(-expt, zeroes); 108758f0484fSRodney W. Grimes PRINT(cp, ndig); 108858f0484fSRodney W. Grimes } else if (expt >= ndig) { 108958f0484fSRodney W. Grimes PRINT(cp, ndig); 109058f0484fSRodney W. Grimes PAD(expt - ndig, zeroes); 109158f0484fSRodney W. Grimes if (flags & ALT) 10929ad80ab5SAndrey A. Chernov PRINT(decimal_point, 1); 109358f0484fSRodney W. Grimes } else { 109458f0484fSRodney W. Grimes PRINT(cp, expt); 109558f0484fSRodney W. Grimes cp += expt; 10969ad80ab5SAndrey A. Chernov PRINT(decimal_point, 1); 109758f0484fSRodney W. Grimes PRINT(cp, ndig-expt); 109858f0484fSRodney W. Grimes } 109958f0484fSRodney W. Grimes } else { /* 'e' or 'E' */ 110058f0484fSRodney W. Grimes if (ndig > 1 || flags & ALT) { 110158f0484fSRodney W. Grimes ox[0] = *cp++; 11029ad80ab5SAndrey A. Chernov ox[1] = *decimal_point; 110358f0484fSRodney W. Grimes PRINT(ox, 2); 1104918bed75SBruce Evans if (_double) { 110558f0484fSRodney W. Grimes PRINT(cp, ndig-1); 110658f0484fSRodney W. Grimes } else /* 0.[0..] */ 110758f0484fSRodney W. Grimes /* __dtoa irregularity */ 110858f0484fSRodney W. Grimes PAD(ndig - 1, zeroes); 110958f0484fSRodney W. Grimes } else /* XeYYY */ 111058f0484fSRodney W. Grimes PRINT(cp, 1); 111158f0484fSRodney W. Grimes PRINT(expstr, expsize); 111258f0484fSRodney W. Grimes } 111358f0484fSRodney W. Grimes } 111458f0484fSRodney W. Grimes #else 111558f0484fSRodney W. Grimes PRINT(cp, size); 111658f0484fSRodney W. Grimes #endif 111758f0484fSRodney W. Grimes /* left-adjusting padding (always blank) */ 111858f0484fSRodney W. Grimes if (flags & LADJUST) 111958f0484fSRodney W. Grimes PAD(width - realsz, blanks); 112058f0484fSRodney W. Grimes 112158f0484fSRodney W. Grimes /* finally, adjust ret */ 112292e88f87SAndrey A. Chernov ret += prsize; 112358f0484fSRodney W. Grimes 112458f0484fSRodney W. Grimes FLUSH(); /* copy out the I/O vectors */ 112558f0484fSRodney W. Grimes } 112658f0484fSRodney W. Grimes done: 112758f0484fSRodney W. Grimes FLUSH(); 112858f0484fSRodney W. Grimes error: 11292ffc61baSTor Egge #ifdef FLOATING_POINT 11302ffc61baSTor Egge if (dtoaresult != NULL) 11312ffc61baSTor Egge free(dtoaresult); 11322ffc61baSTor Egge #endif 1133b9aac308STim J. Robbins if (convbuf != NULL) 1134b9aac308STim J. Robbins free(convbuf); 1135f70177e7SJulian Elischer if (__sferror(fp)) 1136f70177e7SJulian Elischer ret = EOF; 1137efb7e53dSJordan K. Hubbard if ((argtable != NULL) && (argtable != statargtable)) 1138efb7e53dSJordan K. Hubbard free (argtable); 1139f70177e7SJulian Elischer return (ret); 114058f0484fSRodney W. Grimes /* NOTREACHED */ 114158f0484fSRodney W. Grimes } 114258f0484fSRodney W. Grimes 1143efb7e53dSJordan K. Hubbard /* 1144efb7e53dSJordan K. Hubbard * Find all arguments when a positional parameter is encountered. Returns a 1145efb7e53dSJordan K. Hubbard * table, indexed by argument number, of pointers to each arguments. The 1146efb7e53dSJordan K. Hubbard * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries. 114742cebaa5SArchie Cobbs * It will be replaces with a malloc-ed one if it overflows. 1148efb7e53dSJordan K. Hubbard */ 1149efb7e53dSJordan K. Hubbard static void 1150a387081cSDoug Rabson __find_arguments (const char *fmt0, va_list ap, union arg **argtable) 1151efb7e53dSJordan K. Hubbard { 1152d201fe46SDaniel Eischen char *fmt; /* format string */ 1153d201fe46SDaniel Eischen int ch; /* character from fmt */ 1154d201fe46SDaniel Eischen int n, n2; /* handy integer (short term usage) */ 1155d201fe46SDaniel Eischen char *cp; /* handy char pointer (short term usage) */ 1156d201fe46SDaniel Eischen int flags; /* flags as above */ 1157efb7e53dSJordan K. Hubbard int width; /* width from format (%8d), or 0 */ 11587735bb0fSBill Fenner enum typeid *typetable; /* table of types */ 11597735bb0fSBill Fenner enum typeid stattypetable [STATIC_ARG_TBL_SIZE]; 1160efb7e53dSJordan K. Hubbard int tablesize; /* current size of type table */ 1161efb7e53dSJordan K. Hubbard int tablemax; /* largest used index in table */ 1162efb7e53dSJordan K. Hubbard int nextarg; /* 1-based argument index */ 1163efb7e53dSJordan K. Hubbard 1164efb7e53dSJordan K. Hubbard /* 1165efb7e53dSJordan K. Hubbard * Add an argument type to the table, expanding if necessary. 1166efb7e53dSJordan K. Hubbard */ 1167efb7e53dSJordan K. Hubbard #define ADDTYPE(type) \ 1168efb7e53dSJordan K. Hubbard ((nextarg >= tablesize) ? \ 1169efb7e53dSJordan K. Hubbard __grow_type_table(nextarg, &typetable, &tablesize) : 0, \ 117042cebaa5SArchie Cobbs (nextarg > tablemax) ? tablemax = nextarg : 0, \ 117142cebaa5SArchie Cobbs typetable[nextarg++] = type) 1172efb7e53dSJordan K. Hubbard 1173efb7e53dSJordan K. Hubbard #define ADDSARG() \ 11747735bb0fSBill Fenner ((flags&INTMAXT) ? ADDTYPE(T_INTMAXT) : \ 11757735bb0fSBill Fenner ((flags&SIZET) ? ADDTYPE(T_SIZET) : \ 11767735bb0fSBill Fenner ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \ 11777735bb0fSBill Fenner ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \ 11787735bb0fSBill Fenner ((flags&LONGINT) ? ADDTYPE(T_LONG) : ADDTYPE(T_INT)))))) 1179efb7e53dSJordan K. Hubbard 1180efb7e53dSJordan K. Hubbard #define ADDUARG() \ 11817735bb0fSBill Fenner ((flags&INTMAXT) ? ADDTYPE(T_UINTMAXT) : \ 11827735bb0fSBill Fenner ((flags&SIZET) ? ADDTYPE(T_SIZET) : \ 11837735bb0fSBill Fenner ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \ 11847735bb0fSBill Fenner ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \ 11857735bb0fSBill Fenner ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : ADDTYPE(T_U_INT)))))) 1186efb7e53dSJordan K. Hubbard 1187efb7e53dSJordan K. Hubbard /* 1188efb7e53dSJordan K. Hubbard * Add * arguments to the type array. 1189efb7e53dSJordan K. Hubbard */ 1190efb7e53dSJordan K. Hubbard #define ADDASTER() \ 1191efb7e53dSJordan K. Hubbard n2 = 0; \ 1192efb7e53dSJordan K. Hubbard cp = fmt; \ 1193efb7e53dSJordan K. Hubbard while (is_digit(*cp)) { \ 1194efb7e53dSJordan K. Hubbard n2 = 10 * n2 + to_digit(*cp); \ 1195efb7e53dSJordan K. Hubbard cp++; \ 1196efb7e53dSJordan K. Hubbard } \ 1197efb7e53dSJordan K. Hubbard if (*cp == '$') { \ 1198efb7e53dSJordan K. Hubbard int hold = nextarg; \ 1199efb7e53dSJordan K. Hubbard nextarg = n2; \ 1200efb7e53dSJordan K. Hubbard ADDTYPE (T_INT); \ 1201efb7e53dSJordan K. Hubbard nextarg = hold; \ 1202efb7e53dSJordan K. Hubbard fmt = ++cp; \ 1203efb7e53dSJordan K. Hubbard } else { \ 1204efb7e53dSJordan K. Hubbard ADDTYPE (T_INT); \ 1205efb7e53dSJordan K. Hubbard } 1206efb7e53dSJordan K. Hubbard fmt = (char *)fmt0; 1207efb7e53dSJordan K. Hubbard typetable = stattypetable; 1208efb7e53dSJordan K. Hubbard tablesize = STATIC_ARG_TBL_SIZE; 1209efb7e53dSJordan K. Hubbard tablemax = 0; 1210efb7e53dSJordan K. Hubbard nextarg = 1; 1211efb7e53dSJordan K. Hubbard memset (typetable, T_UNUSED, STATIC_ARG_TBL_SIZE); 1212efb7e53dSJordan K. Hubbard 1213efb7e53dSJordan K. Hubbard /* 1214efb7e53dSJordan K. Hubbard * Scan the format for conversions (`%' character). 1215efb7e53dSJordan K. Hubbard */ 1216efb7e53dSJordan K. Hubbard for (;;) { 1217efb7e53dSJordan K. Hubbard for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) 1218efb7e53dSJordan K. Hubbard /* void */; 1219efb7e53dSJordan K. Hubbard if (ch == '\0') 1220efb7e53dSJordan K. Hubbard goto done; 1221efb7e53dSJordan K. Hubbard fmt++; /* skip over '%' */ 1222efb7e53dSJordan K. Hubbard 1223efb7e53dSJordan K. Hubbard flags = 0; 1224efb7e53dSJordan K. Hubbard width = 0; 1225efb7e53dSJordan K. Hubbard 1226efb7e53dSJordan K. Hubbard rflag: ch = *fmt++; 1227efb7e53dSJordan K. Hubbard reswitch: switch (ch) { 1228efb7e53dSJordan K. Hubbard case ' ': 1229efb7e53dSJordan K. Hubbard case '#': 1230efb7e53dSJordan K. Hubbard goto rflag; 1231efb7e53dSJordan K. Hubbard case '*': 1232efb7e53dSJordan K. Hubbard ADDASTER (); 1233efb7e53dSJordan K. Hubbard goto rflag; 1234efb7e53dSJordan K. Hubbard case '-': 1235efb7e53dSJordan K. Hubbard case '+': 12367735bb0fSBill Fenner case '\'': 1237efb7e53dSJordan K. Hubbard goto rflag; 1238efb7e53dSJordan K. Hubbard case '.': 1239efb7e53dSJordan K. Hubbard if ((ch = *fmt++) == '*') { 1240efb7e53dSJordan K. Hubbard ADDASTER (); 1241efb7e53dSJordan K. Hubbard goto rflag; 1242efb7e53dSJordan K. Hubbard } 1243efb7e53dSJordan K. Hubbard while (is_digit(ch)) { 1244efb7e53dSJordan K. Hubbard ch = *fmt++; 1245efb7e53dSJordan K. Hubbard } 1246efb7e53dSJordan K. Hubbard goto reswitch; 1247efb7e53dSJordan K. Hubbard case '0': 1248efb7e53dSJordan K. Hubbard goto rflag; 1249efb7e53dSJordan K. Hubbard case '1': case '2': case '3': case '4': 1250efb7e53dSJordan K. Hubbard case '5': case '6': case '7': case '8': case '9': 1251efb7e53dSJordan K. Hubbard n = 0; 1252efb7e53dSJordan K. Hubbard do { 1253efb7e53dSJordan K. Hubbard n = 10 * n + to_digit(ch); 1254efb7e53dSJordan K. Hubbard ch = *fmt++; 1255efb7e53dSJordan K. Hubbard } while (is_digit(ch)); 1256efb7e53dSJordan K. Hubbard if (ch == '$') { 1257efb7e53dSJordan K. Hubbard nextarg = n; 1258efb7e53dSJordan K. Hubbard goto rflag; 1259efb7e53dSJordan K. Hubbard } 1260efb7e53dSJordan K. Hubbard width = n; 1261efb7e53dSJordan K. Hubbard goto reswitch; 1262efb7e53dSJordan K. Hubbard #ifdef FLOATING_POINT 1263efb7e53dSJordan K. Hubbard case 'L': 1264efb7e53dSJordan K. Hubbard flags |= LONGDBL; 1265efb7e53dSJordan K. Hubbard goto rflag; 1266efb7e53dSJordan K. Hubbard #endif 1267efb7e53dSJordan K. Hubbard case 'h': 12687735bb0fSBill Fenner if (flags & SHORTINT) { 12697735bb0fSBill Fenner flags &= ~SHORTINT; 12707735bb0fSBill Fenner flags |= CHARINT; 12717735bb0fSBill Fenner } else 1272efb7e53dSJordan K. Hubbard flags |= SHORTINT; 1273efb7e53dSJordan K. Hubbard goto rflag; 12747735bb0fSBill Fenner case 'j': 12757735bb0fSBill Fenner flags |= INTMAXT; 12767735bb0fSBill Fenner goto rflag; 1277efb7e53dSJordan K. Hubbard case 'l': 12787735bb0fSBill Fenner if (flags & LONGINT) { 12797735bb0fSBill Fenner flags &= ~LONGINT; 12807735bb0fSBill Fenner flags |= LLONGINT; 12817735bb0fSBill Fenner } else 1282efb7e53dSJordan K. Hubbard flags |= LONGINT; 1283efb7e53dSJordan K. Hubbard goto rflag; 1284efb7e53dSJordan K. Hubbard case 'q': 12857735bb0fSBill Fenner flags |= LLONGINT; /* not necessarily */ 12867735bb0fSBill Fenner goto rflag; 12877735bb0fSBill Fenner case 't': 12887735bb0fSBill Fenner flags |= PTRDIFFT; 12897735bb0fSBill Fenner goto rflag; 12907735bb0fSBill Fenner case 'z': 12917735bb0fSBill Fenner flags |= SIZET; 1292efb7e53dSJordan K. Hubbard goto rflag; 1293efb7e53dSJordan K. Hubbard case 'c': 1294b9aac308STim J. Robbins if (flags & LONGINT) 1295b9aac308STim J. Robbins ADDTYPE(T_WINT); 1296b9aac308STim J. Robbins else 1297efb7e53dSJordan K. Hubbard ADDTYPE(T_INT); 1298efb7e53dSJordan K. Hubbard break; 1299efb7e53dSJordan K. Hubbard case 'D': 1300efb7e53dSJordan K. Hubbard flags |= LONGINT; 1301efb7e53dSJordan K. Hubbard /*FALLTHROUGH*/ 1302efb7e53dSJordan K. Hubbard case 'd': 1303efb7e53dSJordan K. Hubbard case 'i': 1304efb7e53dSJordan K. Hubbard ADDSARG(); 1305efb7e53dSJordan K. Hubbard break; 1306efb7e53dSJordan K. Hubbard #ifdef FLOATING_POINT 13077735bb0fSBill Fenner #ifdef HEXFLOAT 13087735bb0fSBill Fenner case 'a': 13097735bb0fSBill Fenner case 'A': 13107735bb0fSBill Fenner #endif 1311efb7e53dSJordan K. Hubbard case 'e': 1312efb7e53dSJordan K. Hubbard case 'E': 1313efb7e53dSJordan K. Hubbard case 'f': 1314efb7e53dSJordan K. Hubbard case 'g': 1315efb7e53dSJordan K. Hubbard case 'G': 1316efb7e53dSJordan K. Hubbard if (flags & LONGDBL) 1317efb7e53dSJordan K. Hubbard ADDTYPE(T_LONG_DOUBLE); 1318efb7e53dSJordan K. Hubbard else 1319efb7e53dSJordan K. Hubbard ADDTYPE(T_DOUBLE); 1320efb7e53dSJordan K. Hubbard break; 1321efb7e53dSJordan K. Hubbard #endif /* FLOATING_POINT */ 1322efb7e53dSJordan K. Hubbard case 'n': 13237735bb0fSBill Fenner if (flags & INTMAXT) 13247735bb0fSBill Fenner ADDTYPE(TP_INTMAXT); 13257735bb0fSBill Fenner else if (flags & PTRDIFFT) 13267735bb0fSBill Fenner ADDTYPE(TP_PTRDIFFT); 13277735bb0fSBill Fenner else if (flags & SIZET) 13287735bb0fSBill Fenner ADDTYPE(TP_SIZET); 13297735bb0fSBill Fenner else if (flags & LLONGINT) 13307735bb0fSBill Fenner ADDTYPE(TP_LLONG); 1331efb7e53dSJordan K. Hubbard else if (flags & LONGINT) 1332efb7e53dSJordan K. Hubbard ADDTYPE(TP_LONG); 1333efb7e53dSJordan K. Hubbard else if (flags & SHORTINT) 1334efb7e53dSJordan K. Hubbard ADDTYPE(TP_SHORT); 13357735bb0fSBill Fenner else if (flags & CHARINT) 13367735bb0fSBill Fenner ADDTYPE(TP_SCHAR); 1337efb7e53dSJordan K. Hubbard else 1338efb7e53dSJordan K. Hubbard ADDTYPE(TP_INT); 1339efb7e53dSJordan K. Hubbard continue; /* no output */ 1340efb7e53dSJordan K. Hubbard case 'O': 1341efb7e53dSJordan K. Hubbard flags |= LONGINT; 1342efb7e53dSJordan K. Hubbard /*FALLTHROUGH*/ 1343efb7e53dSJordan K. Hubbard case 'o': 1344efb7e53dSJordan K. Hubbard ADDUARG(); 1345efb7e53dSJordan K. Hubbard break; 1346efb7e53dSJordan K. Hubbard case 'p': 1347efb7e53dSJordan K. Hubbard ADDTYPE(TP_VOID); 1348efb7e53dSJordan K. Hubbard break; 1349efb7e53dSJordan K. Hubbard case 's': 1350b9aac308STim J. Robbins if (flags & LONGINT) 1351b9aac308STim J. Robbins ADDTYPE(TP_WCHAR); 1352b9aac308STim J. Robbins else 1353efb7e53dSJordan K. Hubbard ADDTYPE(TP_CHAR); 1354efb7e53dSJordan K. Hubbard break; 1355efb7e53dSJordan K. Hubbard case 'U': 1356efb7e53dSJordan K. Hubbard flags |= LONGINT; 1357efb7e53dSJordan K. Hubbard /*FALLTHROUGH*/ 1358efb7e53dSJordan K. Hubbard case 'u': 1359efb7e53dSJordan K. Hubbard case 'X': 1360efb7e53dSJordan K. Hubbard case 'x': 1361efb7e53dSJordan K. Hubbard ADDUARG(); 1362efb7e53dSJordan K. Hubbard break; 1363efb7e53dSJordan K. Hubbard default: /* "%?" prints ?, unless ? is NUL */ 1364efb7e53dSJordan K. Hubbard if (ch == '\0') 1365efb7e53dSJordan K. Hubbard goto done; 1366efb7e53dSJordan K. Hubbard break; 1367efb7e53dSJordan K. Hubbard } 1368efb7e53dSJordan K. Hubbard } 1369efb7e53dSJordan K. Hubbard done: 1370efb7e53dSJordan K. Hubbard /* 1371efb7e53dSJordan K. Hubbard * Build the argument table. 1372efb7e53dSJordan K. Hubbard */ 1373efb7e53dSJordan K. Hubbard if (tablemax >= STATIC_ARG_TBL_SIZE) { 1374a387081cSDoug Rabson *argtable = (union arg *) 1375a387081cSDoug Rabson malloc (sizeof (union arg) * (tablemax + 1)); 1376efb7e53dSJordan K. Hubbard } 1377efb7e53dSJordan K. Hubbard 1378a387081cSDoug Rabson (*argtable) [0].intarg = 0; 1379efb7e53dSJordan K. Hubbard for (n = 1; n <= tablemax; n++) { 1380efb7e53dSJordan K. Hubbard switch (typetable [n]) { 13817735bb0fSBill Fenner case T_UNUSED: /* whoops! */ 1382a387081cSDoug Rabson (*argtable) [n].intarg = va_arg (ap, int); 1383efb7e53dSJordan K. Hubbard break; 13847735bb0fSBill Fenner case TP_SCHAR: 13857735bb0fSBill Fenner (*argtable) [n].pschararg = va_arg (ap, signed char *); 1386efb7e53dSJordan K. Hubbard break; 1387efb7e53dSJordan K. Hubbard case TP_SHORT: 1388a387081cSDoug Rabson (*argtable) [n].pshortarg = va_arg (ap, short *); 1389efb7e53dSJordan K. Hubbard break; 1390efb7e53dSJordan K. Hubbard case T_INT: 1391a387081cSDoug Rabson (*argtable) [n].intarg = va_arg (ap, int); 1392efb7e53dSJordan K. Hubbard break; 1393efb7e53dSJordan K. Hubbard case T_U_INT: 1394a387081cSDoug Rabson (*argtable) [n].uintarg = va_arg (ap, unsigned int); 1395efb7e53dSJordan K. Hubbard break; 1396efb7e53dSJordan K. Hubbard case TP_INT: 1397a387081cSDoug Rabson (*argtable) [n].pintarg = va_arg (ap, int *); 1398efb7e53dSJordan K. Hubbard break; 1399efb7e53dSJordan K. Hubbard case T_LONG: 1400a387081cSDoug Rabson (*argtable) [n].longarg = va_arg (ap, long); 1401efb7e53dSJordan K. Hubbard break; 1402efb7e53dSJordan K. Hubbard case T_U_LONG: 1403a387081cSDoug Rabson (*argtable) [n].ulongarg = va_arg (ap, unsigned long); 1404efb7e53dSJordan K. Hubbard break; 1405efb7e53dSJordan K. Hubbard case TP_LONG: 1406a387081cSDoug Rabson (*argtable) [n].plongarg = va_arg (ap, long *); 1407efb7e53dSJordan K. Hubbard break; 14087735bb0fSBill Fenner case T_LLONG: 14097735bb0fSBill Fenner (*argtable) [n].longlongarg = va_arg (ap, long long); 1410efb7e53dSJordan K. Hubbard break; 14117735bb0fSBill Fenner case T_U_LLONG: 14127735bb0fSBill Fenner (*argtable) [n].ulonglongarg = va_arg (ap, unsigned long long); 1413efb7e53dSJordan K. Hubbard break; 14147735bb0fSBill Fenner case TP_LLONG: 14157735bb0fSBill Fenner (*argtable) [n].plonglongarg = va_arg (ap, long long *); 14167735bb0fSBill Fenner break; 14177735bb0fSBill Fenner case T_PTRDIFFT: 14187735bb0fSBill Fenner (*argtable) [n].ptrdiffarg = va_arg (ap, ptrdiff_t); 14197735bb0fSBill Fenner break; 14207735bb0fSBill Fenner case TP_PTRDIFFT: 14217735bb0fSBill Fenner (*argtable) [n].pptrdiffarg = va_arg (ap, ptrdiff_t *); 14227735bb0fSBill Fenner break; 14237735bb0fSBill Fenner case T_SIZET: 14247735bb0fSBill Fenner (*argtable) [n].sizearg = va_arg (ap, size_t); 14257735bb0fSBill Fenner break; 14267735bb0fSBill Fenner case TP_SIZET: 14277735bb0fSBill Fenner (*argtable) [n].psizearg = va_arg (ap, ssize_t *); 14287735bb0fSBill Fenner break; 14297735bb0fSBill Fenner case T_INTMAXT: 14307735bb0fSBill Fenner (*argtable) [n].intmaxarg = va_arg (ap, intmax_t); 14317735bb0fSBill Fenner break; 14327735bb0fSBill Fenner case T_UINTMAXT: 14337735bb0fSBill Fenner (*argtable) [n].uintmaxarg = va_arg (ap, uintmax_t); 14347735bb0fSBill Fenner break; 14357735bb0fSBill Fenner case TP_INTMAXT: 14367735bb0fSBill Fenner (*argtable) [n].pintmaxarg = va_arg (ap, intmax_t *); 1437efb7e53dSJordan K. Hubbard break; 1438a387081cSDoug Rabson #ifdef FLOATING_POINT 1439efb7e53dSJordan K. Hubbard case T_DOUBLE: 1440a387081cSDoug Rabson (*argtable) [n].doublearg = va_arg (ap, double); 1441efb7e53dSJordan K. Hubbard break; 1442efb7e53dSJordan K. Hubbard case T_LONG_DOUBLE: 1443a387081cSDoug Rabson (*argtable) [n].longdoublearg = va_arg (ap, long double); 1444efb7e53dSJordan K. Hubbard break; 1445a387081cSDoug Rabson #endif 1446efb7e53dSJordan K. Hubbard case TP_CHAR: 1447a387081cSDoug Rabson (*argtable) [n].pchararg = va_arg (ap, char *); 1448efb7e53dSJordan K. Hubbard break; 1449efb7e53dSJordan K. Hubbard case TP_VOID: 1450a387081cSDoug Rabson (*argtable) [n].pvoidarg = va_arg (ap, void *); 1451efb7e53dSJordan K. Hubbard break; 1452b9aac308STim J. Robbins case T_WINT: 1453b9aac308STim J. Robbins (*argtable) [n].wintarg = va_arg (ap, wint_t); 1454b9aac308STim J. Robbins break; 1455b9aac308STim J. Robbins case TP_WCHAR: 1456b9aac308STim J. Robbins (*argtable) [n].pwchararg = va_arg (ap, wchar_t *); 1457b9aac308STim J. Robbins break; 1458efb7e53dSJordan K. Hubbard } 1459efb7e53dSJordan K. Hubbard } 1460efb7e53dSJordan K. Hubbard 1461efb7e53dSJordan K. Hubbard if ((typetable != NULL) && (typetable != stattypetable)) 1462efb7e53dSJordan K. Hubbard free (typetable); 1463efb7e53dSJordan K. Hubbard } 1464efb7e53dSJordan K. Hubbard 1465efb7e53dSJordan K. Hubbard /* 1466efb7e53dSJordan K. Hubbard * Increase the size of the type table. 1467efb7e53dSJordan K. Hubbard */ 1468efb7e53dSJordan K. Hubbard static void 14697735bb0fSBill Fenner __grow_type_table (int nextarg, enum typeid **typetable, int *tablesize) 1470efb7e53dSJordan K. Hubbard { 14717735bb0fSBill Fenner enum typeid *const oldtable = *typetable; 147242cebaa5SArchie Cobbs const int oldsize = *tablesize; 14737735bb0fSBill Fenner enum typeid *newtable; 147442cebaa5SArchie Cobbs int newsize = oldsize * 2; 1475efb7e53dSJordan K. Hubbard 147642cebaa5SArchie Cobbs if (newsize < nextarg + 1) 147742cebaa5SArchie Cobbs newsize = nextarg + 1; 147842cebaa5SArchie Cobbs if (oldsize == STATIC_ARG_TBL_SIZE) { 147942cebaa5SArchie Cobbs if ((newtable = malloc(newsize)) == NULL) 148042cebaa5SArchie Cobbs abort(); /* XXX handle better */ 148142cebaa5SArchie Cobbs bcopy(oldtable, newtable, oldsize); 1482efb7e53dSJordan K. Hubbard } else { 148342cebaa5SArchie Cobbs if ((newtable = reallocf(oldtable, newsize)) == NULL) 148442cebaa5SArchie Cobbs abort(); /* XXX handle better */ 1485efb7e53dSJordan K. Hubbard } 148642cebaa5SArchie Cobbs memset(&newtable[oldsize], T_UNUSED, newsize - oldsize); 1487efb7e53dSJordan K. Hubbard 148842cebaa5SArchie Cobbs *typetable = newtable; 1489efb7e53dSJordan K. Hubbard *tablesize = newsize; 1490efb7e53dSJordan K. Hubbard } 1491efb7e53dSJordan K. Hubbard 1492efb7e53dSJordan K. Hubbard 149358f0484fSRodney W. Grimes #ifdef FLOATING_POINT 149458f0484fSRodney W. Grimes 1495c05ac53bSDavid E. O'Brien extern char *__dtoa(double, int, int, int *, int *, char **, char **); 149658f0484fSRodney W. Grimes 149758f0484fSRodney W. Grimes static char * 1498d201fe46SDaniel Eischen cvt(double value, int ndigits, int flags, char *sign, int *decpt, 14992ffc61baSTor Egge int ch, int *length, char **dtoaresultp) 150058f0484fSRodney W. Grimes { 150158f0484fSRodney W. Grimes int mode, dsgn; 150258f0484fSRodney W. Grimes char *digits, *bp, *rve; 150358f0484fSRodney W. Grimes 150458f0484fSRodney W. Grimes if (ch == 'f') 1505d26be6f0SBruce Evans mode = 3; /* ndigits after the decimal point */ 150658f0484fSRodney W. Grimes else { 1507d26be6f0SBruce Evans /* 1508d26be6f0SBruce Evans * To obtain ndigits after the decimal point for the 'e' 1509d26be6f0SBruce Evans * and 'E' formats, round to ndigits + 1 significant 1510d26be6f0SBruce Evans * figures. 1511d26be6f0SBruce Evans */ 1512d26be6f0SBruce Evans if (ch == 'e' || ch == 'E') 1513d26be6f0SBruce Evans ndigits++; 1514d26be6f0SBruce Evans mode = 2; /* ndigits significant digits */ 151558f0484fSRodney W. Grimes } 15163dd65760SJens Schweikhardt digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve, dtoaresultp); 15173dd65760SJens Schweikhardt *sign = dsgn != 0; 1518d26be6f0SBruce Evans if ((ch != 'g' && ch != 'G') || flags & ALT) { 1519d26be6f0SBruce Evans /* print trailing zeros */ 152058f0484fSRodney W. Grimes bp = digits + ndigits; 152158f0484fSRodney W. Grimes if (ch == 'f') { 152258f0484fSRodney W. Grimes if (*digits == '0' && value) 152358f0484fSRodney W. Grimes *decpt = -ndigits + 1; 152458f0484fSRodney W. Grimes bp += *decpt; 152558f0484fSRodney W. Grimes } 152658f0484fSRodney W. Grimes if (value == 0) /* kludge for __dtoa irregularity */ 152758f0484fSRodney W. Grimes rve = bp; 152858f0484fSRodney W. Grimes while (rve < bp) 152958f0484fSRodney W. Grimes *rve++ = '0'; 153058f0484fSRodney W. Grimes } 153158f0484fSRodney W. Grimes *length = rve - digits; 153258f0484fSRodney W. Grimes return (digits); 153358f0484fSRodney W. Grimes } 153458f0484fSRodney W. Grimes 153558f0484fSRodney W. Grimes static int 1536d201fe46SDaniel Eischen exponent(char *p0, int exp, int fmtch) 153758f0484fSRodney W. Grimes { 1538d201fe46SDaniel Eischen char *p, *t; 153958f0484fSRodney W. Grimes char expbuf[MAXEXP]; 154058f0484fSRodney W. Grimes 154158f0484fSRodney W. Grimes p = p0; 154258f0484fSRodney W. Grimes *p++ = fmtch; 154358f0484fSRodney W. Grimes if (exp < 0) { 154458f0484fSRodney W. Grimes exp = -exp; 154558f0484fSRodney W. Grimes *p++ = '-'; 154658f0484fSRodney W. Grimes } 154758f0484fSRodney W. Grimes else 154858f0484fSRodney W. Grimes *p++ = '+'; 154958f0484fSRodney W. Grimes t = expbuf + MAXEXP; 155058f0484fSRodney W. Grimes if (exp > 9) { 155158f0484fSRodney W. Grimes do { 155258f0484fSRodney W. Grimes *--t = to_char(exp % 10); 155358f0484fSRodney W. Grimes } while ((exp /= 10) > 9); 155458f0484fSRodney W. Grimes *--t = to_char(exp); 155558f0484fSRodney W. Grimes for (; t < expbuf + MAXEXP; *p++ = *t++); 155658f0484fSRodney W. Grimes } 155758f0484fSRodney W. Grimes else { 155858f0484fSRodney W. Grimes *p++ = '0'; 155958f0484fSRodney W. Grimes *p++ = to_char(exp); 156058f0484fSRodney W. Grimes } 156158f0484fSRodney W. Grimes return (p - p0); 156258f0484fSRodney W. Grimes } 156358f0484fSRodney W. Grimes #endif /* FLOATING_POINT */ 1564