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 * 4. Neither the name of the University nor the names of its contributors 1758f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 1858f0484fSRodney W. Grimes * without specific prior written permission. 1958f0484fSRodney W. Grimes * 2058f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2158f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2258f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2358f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2458f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2558f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2658f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2758f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2858f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2958f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3058f0484fSRodney W. Grimes * SUCH DAMAGE. 3158f0484fSRodney W. Grimes */ 3258f0484fSRodney W. Grimes 3358f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 3458f0484fSRodney W. Grimes static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93"; 3558f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 36333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 37333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 3858f0484fSRodney W. Grimes 3958f0484fSRodney W. Grimes /* 4058f0484fSRodney W. Grimes * Actual printf innards. 4158f0484fSRodney W. Grimes * 4258f0484fSRodney W. Grimes * This code is large and complicated... 4358f0484fSRodney W. Grimes */ 4458f0484fSRodney W. Grimes 45d201fe46SDaniel Eischen #include "namespace.h" 4658f0484fSRodney W. Grimes #include <sys/types.h> 4758f0484fSRodney W. Grimes 487735bb0fSBill Fenner #include <ctype.h> 4958f0484fSRodney W. Grimes #include <limits.h> 507ae5c679SAlexey Zelkin #include <locale.h> 517735bb0fSBill Fenner #include <stddef.h> 527735bb0fSBill Fenner #include <stdint.h> 5358f0484fSRodney W. Grimes #include <stdio.h> 5458f0484fSRodney W. Grimes #include <stdlib.h> 5558f0484fSRodney W. Grimes #include <string.h> 56b9aac308STim J. Robbins #include <wchar.h> 5775067f4fSPoul-Henning Kamp #include <printf.h> 5858f0484fSRodney W. Grimes 5958f0484fSRodney W. Grimes #include <stdarg.h> 60d201fe46SDaniel Eischen #include "un-namespace.h" 6158f0484fSRodney W. Grimes 62d201fe46SDaniel Eischen #include "libc_private.h" 6358f0484fSRodney W. Grimes #include "local.h" 6458f0484fSRodney W. Grimes #include "fvwrite.h" 652591efccSDavid Schultz #include "printflocal.h" 66e5abb5e6SDavid Schultz 67c05ac53bSDavid E. O'Brien static int __sprint(FILE *, struct __suio *); 681372519bSDavid E. O'Brien static int __sbprintf(FILE *, const char *, va_list) __printflike(2, 0); 69ebbad5ecSDavid Schultz static char *__ujtoa(uintmax_t, char *, int, int, const char *, int, char, 701372519bSDavid E. O'Brien const char *); 71ebbad5ecSDavid Schultz static char *__ultoa(u_long, char *, int, int, const char *, int, char, 721372519bSDavid E. O'Brien const char *); 73b9aac308STim J. Robbins static char *__wcsconv(wchar_t *, int); 74ce51cf03SJames Raynard 7558f0484fSRodney W. Grimes /* 7658f0484fSRodney W. Grimes * Flush out all the vectors defined by the given uio, 7758f0484fSRodney W. Grimes * then reset it so that it can be reused. 7858f0484fSRodney W. Grimes */ 7958f0484fSRodney W. Grimes static int 80d201fe46SDaniel Eischen __sprint(FILE *fp, struct __suio *uio) 8158f0484fSRodney W. Grimes { 82d201fe46SDaniel Eischen int err; 8358f0484fSRodney W. Grimes 8458f0484fSRodney W. Grimes if (uio->uio_resid == 0) { 8558f0484fSRodney W. Grimes uio->uio_iovcnt = 0; 8658f0484fSRodney W. Grimes return (0); 8758f0484fSRodney W. Grimes } 8858f0484fSRodney W. Grimes err = __sfvwrite(fp, uio); 8958f0484fSRodney W. Grimes uio->uio_resid = 0; 9058f0484fSRodney W. Grimes uio->uio_iovcnt = 0; 9158f0484fSRodney W. Grimes return (err); 9258f0484fSRodney W. Grimes } 9358f0484fSRodney W. Grimes 9458f0484fSRodney W. Grimes /* 9558f0484fSRodney W. Grimes * Helper function for `fprintf to unbuffered unix file': creates a 9658f0484fSRodney W. Grimes * temporary buffer. We only work on write-only files; this avoids 9758f0484fSRodney W. Grimes * worries about ungetc buffers and so forth. 9858f0484fSRodney W. Grimes */ 9958f0484fSRodney W. Grimes static int 100d201fe46SDaniel Eischen __sbprintf(FILE *fp, const char *fmt, va_list ap) 10158f0484fSRodney W. Grimes { 10258f0484fSRodney W. Grimes int ret; 10358f0484fSRodney W. Grimes FILE fake; 10458f0484fSRodney W. Grimes unsigned char buf[BUFSIZ]; 10558f0484fSRodney W. Grimes 10658f0484fSRodney W. Grimes /* copy the important variables */ 10758f0484fSRodney W. Grimes fake._flags = fp->_flags & ~__SNBF; 10858f0484fSRodney W. Grimes fake._file = fp->_file; 10958f0484fSRodney W. Grimes fake._cookie = fp->_cookie; 11058f0484fSRodney W. Grimes fake._write = fp->_write; 1111e98f887SJohn Baldwin fake._orientation = fp->_orientation; 1121e98f887SJohn Baldwin fake._mbstate = fp->_mbstate; 11358f0484fSRodney W. Grimes 11458f0484fSRodney W. Grimes /* set up the buffer */ 11558f0484fSRodney W. Grimes fake._bf._base = fake._p = buf; 11658f0484fSRodney W. Grimes fake._bf._size = fake._w = sizeof(buf); 11758f0484fSRodney W. Grimes fake._lbfsize = 0; /* not actually used, but Just In Case */ 11858f0484fSRodney W. Grimes 11958f0484fSRodney W. Grimes /* do the work, then copy any error status */ 120d201fe46SDaniel Eischen ret = __vfprintf(&fake, fmt, ap); 121d201fe46SDaniel Eischen if (ret >= 0 && __fflush(&fake)) 12258f0484fSRodney W. Grimes ret = EOF; 12358f0484fSRodney W. Grimes if (fake._flags & __SERR) 12458f0484fSRodney W. Grimes fp->_flags |= __SERR; 12558f0484fSRodney W. Grimes return (ret); 12658f0484fSRodney W. Grimes } 12758f0484fSRodney W. Grimes 12858f0484fSRodney W. Grimes /* 12958f0484fSRodney W. Grimes * Convert an unsigned long to ASCII for printf purposes, returning 13058f0484fSRodney W. Grimes * a pointer to the first character of the string representation. 13158f0484fSRodney W. Grimes * Octal numbers can be forced to have a leading zero; hex numbers 13258f0484fSRodney W. Grimes * use the given digits. 13358f0484fSRodney W. Grimes */ 13458f0484fSRodney W. Grimes static char * 135ebbad5ecSDavid Schultz __ultoa(u_long val, char *endp, int base, int octzero, const char *xdigs, 13698ee7635SAlexey Zelkin int needgrp, char thousep, const char *grp) 13758f0484fSRodney W. Grimes { 1388fb3f3f6SDavid E. O'Brien char *cp = endp; 1398fb3f3f6SDavid E. O'Brien long sval; 1407735bb0fSBill Fenner int ndig; 14158f0484fSRodney W. Grimes 14258f0484fSRodney W. Grimes /* 14358f0484fSRodney W. Grimes * Handle the three cases separately, in the hope of getting 14458f0484fSRodney W. Grimes * better/faster code. 14558f0484fSRodney W. Grimes */ 14658f0484fSRodney W. Grimes switch (base) { 14758f0484fSRodney W. Grimes case 10: 14858f0484fSRodney W. Grimes if (val < 10) { /* many numbers are 1 digit */ 14958f0484fSRodney W. Grimes *--cp = to_char(val); 15058f0484fSRodney W. Grimes return (cp); 15158f0484fSRodney W. Grimes } 1527735bb0fSBill Fenner ndig = 0; 15358f0484fSRodney W. Grimes /* 15458f0484fSRodney W. Grimes * On many machines, unsigned arithmetic is harder than 15558f0484fSRodney W. Grimes * signed arithmetic, so we do at most one unsigned mod and 15658f0484fSRodney W. Grimes * divide; this is sufficient to reduce the range of 15758f0484fSRodney W. Grimes * the incoming value to where signed arithmetic works. 15858f0484fSRodney W. Grimes */ 15958f0484fSRodney W. Grimes if (val > LONG_MAX) { 16058f0484fSRodney W. Grimes *--cp = to_char(val % 10); 1617735bb0fSBill Fenner ndig++; 16258f0484fSRodney W. Grimes sval = val / 10; 16358f0484fSRodney W. Grimes } else 16458f0484fSRodney W. Grimes sval = val; 16558f0484fSRodney W. Grimes do { 16658f0484fSRodney W. Grimes *--cp = to_char(sval % 10); 16798ee7635SAlexey Zelkin ndig++; 16898ee7635SAlexey Zelkin /* 16998ee7635SAlexey Zelkin * If (*grp == CHAR_MAX) then no more grouping 17098ee7635SAlexey Zelkin * should be performed. 17198ee7635SAlexey Zelkin */ 172243e90d6SAlexey Zelkin if (needgrp && ndig == *grp && *grp != CHAR_MAX 173243e90d6SAlexey Zelkin && sval > 9) { 17498ee7635SAlexey Zelkin *--cp = thousep; 1757735bb0fSBill Fenner ndig = 0; 17698ee7635SAlexey Zelkin /* 17798ee7635SAlexey Zelkin * If (*(grp+1) == '\0') then we have to 17898ee7635SAlexey Zelkin * use *grp character (last grouping rule) 17998ee7635SAlexey Zelkin * for all next cases 18098ee7635SAlexey Zelkin */ 1812e394b2fSAlexey Zelkin if (*(grp+1) != '\0') 1822e394b2fSAlexey Zelkin grp++; 1837735bb0fSBill Fenner } 18458f0484fSRodney W. Grimes sval /= 10; 18558f0484fSRodney W. Grimes } while (sval != 0); 18658f0484fSRodney W. Grimes break; 18758f0484fSRodney W. Grimes 18858f0484fSRodney W. Grimes case 8: 18958f0484fSRodney W. Grimes do { 19058f0484fSRodney W. Grimes *--cp = to_char(val & 7); 19158f0484fSRodney W. Grimes val >>= 3; 19258f0484fSRodney W. Grimes } while (val); 19358f0484fSRodney W. Grimes if (octzero && *cp != '0') 19458f0484fSRodney W. Grimes *--cp = '0'; 19558f0484fSRodney W. Grimes break; 19658f0484fSRodney W. Grimes 19758f0484fSRodney W. Grimes case 16: 19858f0484fSRodney W. Grimes do { 19958f0484fSRodney W. Grimes *--cp = xdigs[val & 15]; 20058f0484fSRodney W. Grimes val >>= 4; 20158f0484fSRodney W. Grimes } while (val); 20258f0484fSRodney W. Grimes break; 20358f0484fSRodney W. Grimes 20458f0484fSRodney W. Grimes default: /* oops */ 20558f0484fSRodney W. Grimes abort(); 20658f0484fSRodney W. Grimes } 20758f0484fSRodney W. Grimes return (cp); 20858f0484fSRodney W. Grimes } 20958f0484fSRodney W. Grimes 2107735bb0fSBill Fenner /* Identical to __ultoa, but for intmax_t. */ 21158f0484fSRodney W. Grimes static char * 212ebbad5ecSDavid Schultz __ujtoa(uintmax_t val, char *endp, int base, int octzero, const char *xdigs, 21398ee7635SAlexey Zelkin int needgrp, char thousep, const char *grp) 21458f0484fSRodney W. Grimes { 215d201fe46SDaniel Eischen char *cp = endp; 2167735bb0fSBill Fenner intmax_t sval; 2177735bb0fSBill Fenner int ndig; 21858f0484fSRodney W. Grimes 21958f0484fSRodney W. Grimes /* quick test for small values; __ultoa is typically much faster */ 22058f0484fSRodney W. Grimes /* (perhaps instead we should run until small, then call __ultoa?) */ 22158f0484fSRodney W. Grimes if (val <= ULONG_MAX) 2227735bb0fSBill Fenner return (__ultoa((u_long)val, endp, base, octzero, xdigs, 22398ee7635SAlexey Zelkin needgrp, thousep, grp)); 22458f0484fSRodney W. Grimes switch (base) { 22558f0484fSRodney W. Grimes case 10: 22658f0484fSRodney W. Grimes if (val < 10) { 22758f0484fSRodney W. Grimes *--cp = to_char(val % 10); 22858f0484fSRodney W. Grimes return (cp); 22958f0484fSRodney W. Grimes } 2307735bb0fSBill Fenner ndig = 0; 2317735bb0fSBill Fenner if (val > INTMAX_MAX) { 23258f0484fSRodney W. Grimes *--cp = to_char(val % 10); 2337735bb0fSBill Fenner ndig++; 23458f0484fSRodney W. Grimes sval = val / 10; 23558f0484fSRodney W. Grimes } else 23658f0484fSRodney W. Grimes sval = val; 23758f0484fSRodney W. Grimes do { 23858f0484fSRodney W. Grimes *--cp = to_char(sval % 10); 23998ee7635SAlexey Zelkin ndig++; 24098ee7635SAlexey Zelkin /* 24198ee7635SAlexey Zelkin * If (*grp == CHAR_MAX) then no more grouping 24298ee7635SAlexey Zelkin * should be performed. 24398ee7635SAlexey Zelkin */ 244243e90d6SAlexey Zelkin if (needgrp && *grp != CHAR_MAX && ndig == *grp 245243e90d6SAlexey Zelkin && sval > 9) { 24698ee7635SAlexey Zelkin *--cp = thousep; 2477735bb0fSBill Fenner ndig = 0; 24898ee7635SAlexey Zelkin /* 24998ee7635SAlexey Zelkin * If (*(grp+1) == '\0') then we have to 25098ee7635SAlexey Zelkin * use *grp character (last grouping rule) 25198ee7635SAlexey Zelkin * for all next cases 25298ee7635SAlexey Zelkin */ 2532e394b2fSAlexey Zelkin if (*(grp+1) != '\0') 2542e394b2fSAlexey Zelkin grp++; 2557735bb0fSBill Fenner } 25658f0484fSRodney W. Grimes sval /= 10; 25758f0484fSRodney W. Grimes } while (sval != 0); 25858f0484fSRodney W. Grimes break; 25958f0484fSRodney W. Grimes 26058f0484fSRodney W. Grimes case 8: 26158f0484fSRodney W. Grimes do { 26258f0484fSRodney W. Grimes *--cp = to_char(val & 7); 26358f0484fSRodney W. Grimes val >>= 3; 26458f0484fSRodney W. Grimes } while (val); 26558f0484fSRodney W. Grimes if (octzero && *cp != '0') 26658f0484fSRodney W. Grimes *--cp = '0'; 26758f0484fSRodney W. Grimes break; 26858f0484fSRodney W. Grimes 26958f0484fSRodney W. Grimes case 16: 27058f0484fSRodney W. Grimes do { 27158f0484fSRodney W. Grimes *--cp = xdigs[val & 15]; 27258f0484fSRodney W. Grimes val >>= 4; 27358f0484fSRodney W. Grimes } while (val); 27458f0484fSRodney W. Grimes break; 27558f0484fSRodney W. Grimes 27658f0484fSRodney W. Grimes default: 27758f0484fSRodney W. Grimes abort(); 27858f0484fSRodney W. Grimes } 27958f0484fSRodney W. Grimes return (cp); 28058f0484fSRodney W. Grimes } 28158f0484fSRodney W. Grimes 282d201fe46SDaniel Eischen /* 283b9aac308STim J. Robbins * Convert a wide character string argument for the %ls format to a multibyte 284d48c77b5STim J. Robbins * string representation. If not -1, prec specifies the maximum number of 285d48c77b5STim J. Robbins * bytes to output, and also means that we can't assume that the wide char. 286d48c77b5STim J. Robbins * string ends is null-terminated. 287b9aac308STim J. Robbins */ 288b9aac308STim J. Robbins static char * 289b9aac308STim J. Robbins __wcsconv(wchar_t *wcsarg, int prec) 290b9aac308STim J. Robbins { 29193996f6dSTim J. Robbins static const mbstate_t initial; 29293996f6dSTim J. Robbins mbstate_t mbs; 293b9aac308STim J. Robbins char buf[MB_LEN_MAX]; 294b9aac308STim J. Robbins wchar_t *p; 295d48c77b5STim J. Robbins char *convbuf; 296b9aac308STim J. Robbins size_t clen, nbytes; 297b9aac308STim J. Robbins 298d48c77b5STim J. Robbins /* Allocate space for the maximum number of bytes we could output. */ 299d48c77b5STim J. Robbins if (prec < 0) { 300d48c77b5STim J. Robbins p = wcsarg; 301d48c77b5STim J. Robbins mbs = initial; 302d48c77b5STim J. Robbins nbytes = wcsrtombs(NULL, (const wchar_t **)&p, 0, &mbs); 303d48c77b5STim J. Robbins if (nbytes == (size_t)-1) 304d48c77b5STim J. Robbins return (NULL); 305d48c77b5STim J. Robbins } else { 306b9aac308STim J. Robbins /* 307d48c77b5STim J. Robbins * Optimisation: if the output precision is small enough, 308d48c77b5STim J. Robbins * just allocate enough memory for the maximum instead of 309d48c77b5STim J. Robbins * scanning the string. 310b9aac308STim J. Robbins */ 311d48c77b5STim J. Robbins if (prec < 128) 312d48c77b5STim J. Robbins nbytes = prec; 313d48c77b5STim J. Robbins else { 314b9aac308STim J. Robbins nbytes = 0; 315b9aac308STim J. Robbins p = wcsarg; 31693996f6dSTim J. Robbins mbs = initial; 317b9aac308STim J. Robbins for (;;) { 31893996f6dSTim J. Robbins clen = wcrtomb(buf, *p++, &mbs); 319b9aac308STim J. Robbins if (clen == 0 || clen == (size_t)-1 || 320b9aac308STim J. Robbins nbytes + clen > prec) 321b9aac308STim J. Robbins break; 322b9aac308STim J. Robbins nbytes += clen; 323b9aac308STim J. Robbins } 324d48c77b5STim J. Robbins } 325b9aac308STim J. Robbins } 326b9aac308STim J. Robbins if ((convbuf = malloc(nbytes + 1)) == NULL) 327b9aac308STim J. Robbins return (NULL); 328b9aac308STim J. Robbins 329d48c77b5STim J. Robbins /* Fill the output buffer. */ 330b9aac308STim J. Robbins p = wcsarg; 33193996f6dSTim J. Robbins mbs = initial; 332d48c77b5STim J. Robbins if ((nbytes = wcsrtombs(convbuf, (const wchar_t **)&p, 333d48c77b5STim J. Robbins nbytes, &mbs)) == (size_t)-1) { 3346f098a48SAndrey A. Chernov free(convbuf); 335b9aac308STim J. Robbins return (NULL); 3366f098a48SAndrey A. Chernov } 337d48c77b5STim J. Robbins convbuf[nbytes] = '\0'; 338b9aac308STim J. Robbins return (convbuf); 339b9aac308STim J. Robbins } 340b9aac308STim J. Robbins 341b9aac308STim J. Robbins /* 342d201fe46SDaniel Eischen * MT-safe version 343d201fe46SDaniel Eischen */ 344d201fe46SDaniel Eischen int 345f8418db7SRobert Drehmel vfprintf(FILE * __restrict fp, const char * __restrict fmt0, va_list ap) 346f8418db7SRobert Drehmel 347d201fe46SDaniel Eischen { 348d201fe46SDaniel Eischen int ret; 349d201fe46SDaniel Eischen 350d201fe46SDaniel Eischen FLOCKFILE(fp); 351d201fe46SDaniel Eischen ret = __vfprintf(fp, fmt0, ap); 352d201fe46SDaniel Eischen FUNLOCKFILE(fp); 353d201fe46SDaniel Eischen return (ret); 354d201fe46SDaniel Eischen } 355d201fe46SDaniel Eischen 3568de9e897SDavid Schultz #ifndef NO_FLOATING_POINT 357ebbad5ecSDavid Schultz 358ebbad5ecSDavid Schultz #define dtoa __dtoa 359ebbad5ecSDavid Schultz #define freedtoa __freedtoa 360ebbad5ecSDavid Schultz 361ebbad5ecSDavid Schultz #include <float.h> 36258f0484fSRodney W. Grimes #include <math.h> 36358f0484fSRodney W. Grimes #include "floatio.h" 364ebbad5ecSDavid Schultz #include "gdtoa.h" 36558f0484fSRodney W. Grimes 36658f0484fSRodney W. Grimes #define DEFPREC 6 36758f0484fSRodney W. Grimes 368c05ac53bSDavid E. O'Brien static int exponent(char *, int, int); 36958f0484fSRodney W. Grimes 3708de9e897SDavid Schultz #endif /* !NO_FLOATING_POINT */ 37158f0484fSRodney W. Grimes 37238cac8f8SDavid Schultz /* 37338cac8f8SDavid Schultz * The size of the buffer we use as scratch space for integer 37438cac8f8SDavid Schultz * conversions, among other things. Technically, we would need the 37538cac8f8SDavid Schultz * most space for base 10 conversions with thousands' grouping 37638cac8f8SDavid Schultz * characters between each pair of digits. 100 bytes is a 37738cac8f8SDavid Schultz * conservative overestimate even for a 128-bit uintmax_t. 37838cac8f8SDavid Schultz */ 37938cac8f8SDavid Schultz #define BUF 100 38038cac8f8SDavid Schultz 38158f0484fSRodney W. Grimes /* 382d201fe46SDaniel Eischen * Non-MT-safe version 383d201fe46SDaniel Eischen */ 38458f0484fSRodney W. Grimes int 385d201fe46SDaniel Eischen __vfprintf(FILE *fp, const char *fmt0, va_list ap) 38658f0484fSRodney W. Grimes { 387d201fe46SDaniel Eischen char *fmt; /* format string */ 388d201fe46SDaniel Eischen int ch; /* character from fmt */ 389d201fe46SDaniel Eischen int n, n2; /* handy integer (short term usage) */ 390d201fe46SDaniel Eischen char *cp; /* handy char pointer (short term usage) */ 391d201fe46SDaniel Eischen struct __siov *iovp; /* for PRINT macro */ 392d201fe46SDaniel Eischen int flags; /* flags as above */ 39358f0484fSRodney W. Grimes int ret; /* return value accumulator */ 39458f0484fSRodney W. Grimes int width; /* width from format (%8d), or 0 */ 395ebbad5ecSDavid Schultz int prec; /* precision from format; <0 for N/A */ 39658f0484fSRodney W. Grimes char sign; /* sign prefix (' ', '+', '-', or \0) */ 39798ee7635SAlexey Zelkin char thousands_sep; /* locale specific thousands separator */ 39898ee7635SAlexey Zelkin const char *grouping; /* locale specific numeric grouping rules */ 39975067f4fSPoul-Henning Kamp 40075067f4fSPoul-Henning Kamp if (__use_xprintf == 0 && getenv("USE_XPRINTF")) 40175067f4fSPoul-Henning Kamp __use_xprintf = 1; 40275067f4fSPoul-Henning Kamp if (__use_xprintf > 0) 40375067f4fSPoul-Henning Kamp return (__xvprintf(fp, fmt0, ap)); 40475067f4fSPoul-Henning Kamp 4058de9e897SDavid Schultz #ifndef NO_FLOATING_POINT 406ebbad5ecSDavid Schultz /* 407ebbad5ecSDavid Schultz * We can decompose the printed representation of floating 408ebbad5ecSDavid Schultz * point numbers into several parts, some of which may be empty: 409ebbad5ecSDavid Schultz * 410ebbad5ecSDavid Schultz * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ 411ebbad5ecSDavid Schultz * A B ---C--- D E F 412ebbad5ecSDavid Schultz * 413ebbad5ecSDavid Schultz * A: 'sign' holds this value if present; '\0' otherwise 414ebbad5ecSDavid Schultz * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal 415ebbad5ecSDavid Schultz * C: cp points to the string MMMNNN. Leading and trailing 416ebbad5ecSDavid Schultz * zeros are not in the string and must be added. 417ebbad5ecSDavid Schultz * D: expchar holds this character; '\0' if no exponent, e.g. %f 418ebbad5ecSDavid Schultz * F: at least two digits for decimal, at least one digit for hex 419ebbad5ecSDavid Schultz */ 4207ae5c679SAlexey Zelkin char *decimal_point; /* locale specific decimal point */ 421ebbad5ecSDavid Schultz int signflag; /* true if float is negative */ 422ebbad5ecSDavid Schultz union { /* floating point arguments %[aAeEfFgG] */ 423ebbad5ecSDavid Schultz double dbl; 424ebbad5ecSDavid Schultz long double ldbl; 425ebbad5ecSDavid Schultz } fparg; 42658f0484fSRodney W. Grimes int expt; /* integer value of exponent */ 427ebbad5ecSDavid Schultz char expchar; /* exponent character: [eEpP\0] */ 428ebbad5ecSDavid Schultz char *dtoaend; /* pointer to end of converted digits */ 42958f0484fSRodney W. Grimes int expsize; /* character count for expstr */ 430ebbad5ecSDavid Schultz int lead; /* sig figs before decimal or group sep */ 431ebbad5ecSDavid Schultz int ndig; /* actual number of digits returned by dtoa */ 432ebbad5ecSDavid Schultz char expstr[MAXEXPDIG+2]; /* buffer for exponent string: e+ZZZ */ 4332ffc61baSTor Egge char *dtoaresult; /* buffer allocated by dtoa */ 434ebbad5ecSDavid Schultz int nseps; /* number of group separators with ' */ 435ebbad5ecSDavid Schultz int nrepeats; /* number of repeats of the last group */ 43658f0484fSRodney W. Grimes #endif 43758f0484fSRodney W. Grimes u_long ulval; /* integer arguments %[diouxX] */ 4387735bb0fSBill Fenner uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */ 43958f0484fSRodney W. Grimes int base; /* base for [diouxX] conversion */ 44058f0484fSRodney W. Grimes int dprec; /* a copy of prec if [diouxX], 0 otherwise */ 441261a532aSBill Fenner int realsz; /* field size expanded by dprec, sign, etc */ 44258f0484fSRodney W. Grimes int size; /* size of converted field or string */ 44392e88f87SAndrey A. Chernov int prsize; /* max size of printed field */ 444ebbad5ecSDavid Schultz const char *xdigs; /* digits for %[xX] conversion */ 44558f0484fSRodney W. Grimes #define NIOV 8 44658f0484fSRodney W. Grimes struct __suio uio; /* output information: summary */ 44758f0484fSRodney W. Grimes struct __siov iov[NIOV];/* ... and individual io vectors */ 44838cac8f8SDavid Schultz char buf[BUF]; /* buffer with space for digits of uintmax_t */ 449ebbad5ecSDavid Schultz char ox[2]; /* space for 0x; ox[1] is either x, X, or \0 */ 450a387081cSDoug Rabson union arg *argtable; /* args, built due to positional arg */ 451a387081cSDoug Rabson union arg statargtable [STATIC_ARG_TBL_SIZE]; 452efb7e53dSJordan K. Hubbard int nextarg; /* 1-based argument index */ 453efb7e53dSJordan K. Hubbard va_list orgap; /* original argument pointer */ 454b9aac308STim J. Robbins char *convbuf; /* wide to multibyte conversion result */ 45558f0484fSRodney W. Grimes 45658f0484fSRodney W. Grimes /* 45758f0484fSRodney W. Grimes * Choose PADSIZE to trade efficiency vs. size. If larger printf 45858f0484fSRodney W. Grimes * fields occur frequently, increase PADSIZE and make the initialisers 45958f0484fSRodney W. Grimes * below longer. 46058f0484fSRodney W. Grimes */ 46158f0484fSRodney W. Grimes #define PADSIZE 16 /* pad chunk size */ 46258f0484fSRodney W. Grimes static char blanks[PADSIZE] = 46358f0484fSRodney W. Grimes {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; 46458f0484fSRodney W. Grimes static char zeroes[PADSIZE] = 46558f0484fSRodney W. Grimes {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'}; 46658f0484fSRodney W. Grimes 467ac9913a7SDavid Schultz static const char xdigs_lower[16] = "0123456789abcdef"; 468ac9913a7SDavid Schultz static const char xdigs_upper[16] = "0123456789ABCDEF"; 469ebbad5ecSDavid Schultz 47058f0484fSRodney W. Grimes /* 47158f0484fSRodney W. Grimes * BEWARE, these `goto error' on error, and PAD uses `n'. 47258f0484fSRodney W. Grimes */ 47358f0484fSRodney W. Grimes #define PRINT(ptr, len) { \ 47458f0484fSRodney W. Grimes iovp->iov_base = (ptr); \ 47558f0484fSRodney W. Grimes iovp->iov_len = (len); \ 47658f0484fSRodney W. Grimes uio.uio_resid += (len); \ 47758f0484fSRodney W. Grimes iovp++; \ 47858f0484fSRodney W. Grimes if (++uio.uio_iovcnt >= NIOV) { \ 47958f0484fSRodney W. Grimes if (__sprint(fp, &uio)) \ 48058f0484fSRodney W. Grimes goto error; \ 48158f0484fSRodney W. Grimes iovp = iov; \ 48258f0484fSRodney W. Grimes } \ 48358f0484fSRodney W. Grimes } 48458f0484fSRodney W. Grimes #define PAD(howmany, with) { \ 48558f0484fSRodney W. Grimes if ((n = (howmany)) > 0) { \ 48658f0484fSRodney W. Grimes while (n > PADSIZE) { \ 48758f0484fSRodney W. Grimes PRINT(with, PADSIZE); \ 48858f0484fSRodney W. Grimes n -= PADSIZE; \ 48958f0484fSRodney W. Grimes } \ 49058f0484fSRodney W. Grimes PRINT(with, n); \ 49158f0484fSRodney W. Grimes } \ 49258f0484fSRodney W. Grimes } 4933b204b7dSDavid Schultz #define PRINTANDPAD(p, ep, len, with) do { \ 4943b204b7dSDavid Schultz n2 = (ep) - (p); \ 4953b204b7dSDavid Schultz if (n2 > (len)) \ 4963b204b7dSDavid Schultz n2 = (len); \ 4973b204b7dSDavid Schultz if (n2 > 0) \ 4983b204b7dSDavid Schultz PRINT((p), n2); \ 4993b204b7dSDavid Schultz PAD((len) - (n2 > 0 ? n2 : 0), (with)); \ 5003b204b7dSDavid Schultz } while(0) 50158f0484fSRodney W. Grimes #define FLUSH() { \ 50258f0484fSRodney W. Grimes if (uio.uio_resid && __sprint(fp, &uio)) \ 50358f0484fSRodney W. Grimes goto error; \ 50458f0484fSRodney W. Grimes uio.uio_iovcnt = 0; \ 50558f0484fSRodney W. Grimes iovp = iov; \ 50658f0484fSRodney W. Grimes } 50758f0484fSRodney W. Grimes 50858f0484fSRodney W. Grimes /* 509efb7e53dSJordan K. Hubbard * Get the argument indexed by nextarg. If the argument table is 510efb7e53dSJordan K. Hubbard * built, use it to get the argument. If its not, get the next 511efb7e53dSJordan K. Hubbard * argument (and arguments must be gotten sequentially). 512efb7e53dSJordan K. Hubbard */ 513efb7e53dSJordan K. Hubbard #define GETARG(type) \ 514a387081cSDoug Rabson ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \ 515efb7e53dSJordan K. Hubbard (nextarg++, va_arg(ap, type))) 516efb7e53dSJordan K. Hubbard 517efb7e53dSJordan K. Hubbard /* 51858f0484fSRodney W. Grimes * To extend shorts properly, we need both signed and unsigned 51958f0484fSRodney W. Grimes * argument extraction methods. 52058f0484fSRodney W. Grimes */ 52158f0484fSRodney W. Grimes #define SARG() \ 522efb7e53dSJordan K. Hubbard (flags&LONGINT ? GETARG(long) : \ 523efb7e53dSJordan K. Hubbard flags&SHORTINT ? (long)(short)GETARG(int) : \ 5247735bb0fSBill Fenner flags&CHARINT ? (long)(signed char)GETARG(int) : \ 525efb7e53dSJordan K. Hubbard (long)GETARG(int)) 52658f0484fSRodney W. Grimes #define UARG() \ 527efb7e53dSJordan K. Hubbard (flags&LONGINT ? GETARG(u_long) : \ 528efb7e53dSJordan K. Hubbard flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \ 5297735bb0fSBill Fenner flags&CHARINT ? (u_long)(u_char)GETARG(int) : \ 530efb7e53dSJordan K. Hubbard (u_long)GETARG(u_int)) 5317735bb0fSBill Fenner #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT) 5327735bb0fSBill Fenner #define SJARG() \ 5337735bb0fSBill Fenner (flags&INTMAXT ? GETARG(intmax_t) : \ 5347735bb0fSBill Fenner flags&SIZET ? (intmax_t)GETARG(size_t) : \ 5357735bb0fSBill Fenner flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \ 5367735bb0fSBill Fenner (intmax_t)GETARG(long long)) 5377735bb0fSBill Fenner #define UJARG() \ 5387735bb0fSBill Fenner (flags&INTMAXT ? GETARG(uintmax_t) : \ 5397735bb0fSBill Fenner flags&SIZET ? (uintmax_t)GETARG(size_t) : \ 5407735bb0fSBill Fenner flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \ 5417735bb0fSBill Fenner (uintmax_t)GETARG(unsigned long long)) 542efb7e53dSJordan K. Hubbard 543efb7e53dSJordan K. Hubbard /* 544efb7e53dSJordan K. Hubbard * Get * arguments, including the form *nn$. Preserve the nextarg 545efb7e53dSJordan K. Hubbard * that the argument can be gotten once the type is determined. 546efb7e53dSJordan K. Hubbard */ 547efb7e53dSJordan K. Hubbard #define GETASTER(val) \ 548efb7e53dSJordan K. Hubbard n2 = 0; \ 549efb7e53dSJordan K. Hubbard cp = fmt; \ 550efb7e53dSJordan K. Hubbard while (is_digit(*cp)) { \ 551efb7e53dSJordan K. Hubbard n2 = 10 * n2 + to_digit(*cp); \ 552efb7e53dSJordan K. Hubbard cp++; \ 553efb7e53dSJordan K. Hubbard } \ 554efb7e53dSJordan K. Hubbard if (*cp == '$') { \ 555efb7e53dSJordan K. Hubbard int hold = nextarg; \ 556efb7e53dSJordan K. Hubbard if (argtable == NULL) { \ 557efb7e53dSJordan K. Hubbard argtable = statargtable; \ 558efb7e53dSJordan K. Hubbard __find_arguments (fmt0, orgap, &argtable); \ 559efb7e53dSJordan K. Hubbard } \ 560efb7e53dSJordan K. Hubbard nextarg = n2; \ 561efb7e53dSJordan K. Hubbard val = GETARG (int); \ 562efb7e53dSJordan K. Hubbard nextarg = hold; \ 563efb7e53dSJordan K. Hubbard fmt = ++cp; \ 564efb7e53dSJordan K. Hubbard } else { \ 565efb7e53dSJordan K. Hubbard val = GETARG (int); \ 566efb7e53dSJordan K. Hubbard } 567efb7e53dSJordan K. Hubbard 56858f0484fSRodney W. Grimes 56998ee7635SAlexey Zelkin thousands_sep = '\0'; 57098ee7635SAlexey Zelkin grouping = NULL; 571b9aac308STim J. Robbins convbuf = NULL; 5728de9e897SDavid Schultz #ifndef NO_FLOATING_POINT 5732ffc61baSTor Egge dtoaresult = NULL; 5747735bb0fSBill Fenner decimal_point = localeconv()->decimal_point; 5752ffc61baSTor Egge #endif 57658f0484fSRodney W. Grimes /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */ 57752183d46SDavid Schultz if (prepwrite(fp) != 0) 57858f0484fSRodney W. Grimes return (EOF); 57958f0484fSRodney W. Grimes 58058f0484fSRodney W. Grimes /* optimise fprintf(stderr) (and other unbuffered Unix files) */ 58158f0484fSRodney W. Grimes if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) && 582d201fe46SDaniel Eischen fp->_file >= 0) 58358f0484fSRodney W. Grimes return (__sbprintf(fp, fmt0, ap)); 58458f0484fSRodney W. Grimes 58558f0484fSRodney W. Grimes fmt = (char *)fmt0; 586efb7e53dSJordan K. Hubbard argtable = NULL; 587efb7e53dSJordan K. Hubbard nextarg = 1; 588d07090a8STim J. Robbins va_copy(orgap, ap); 58958f0484fSRodney W. Grimes uio.uio_iov = iovp = iov; 59058f0484fSRodney W. Grimes uio.uio_resid = 0; 59158f0484fSRodney W. Grimes uio.uio_iovcnt = 0; 59258f0484fSRodney W. Grimes ret = 0; 59358f0484fSRodney W. Grimes 59458f0484fSRodney W. Grimes /* 59558f0484fSRodney W. Grimes * Scan the format for conversions (`%' character). 59658f0484fSRodney W. Grimes */ 59758f0484fSRodney W. Grimes for (;;) { 59858f0484fSRodney W. Grimes for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) 59958f0484fSRodney W. Grimes /* void */; 60058f0484fSRodney W. Grimes if ((n = fmt - cp) != 0) { 601b250f248SAndrey A. Chernov if ((unsigned)ret + n > INT_MAX) { 60292e88f87SAndrey A. Chernov ret = EOF; 60392e88f87SAndrey A. Chernov goto error; 60492e88f87SAndrey A. Chernov } 60558f0484fSRodney W. Grimes PRINT(cp, n); 60658f0484fSRodney W. Grimes ret += n; 60758f0484fSRodney W. Grimes } 60858f0484fSRodney W. Grimes if (ch == '\0') 60958f0484fSRodney W. Grimes goto done; 61058f0484fSRodney W. Grimes fmt++; /* skip over '%' */ 61158f0484fSRodney W. Grimes 61258f0484fSRodney W. Grimes flags = 0; 61358f0484fSRodney W. Grimes dprec = 0; 61458f0484fSRodney W. Grimes width = 0; 61558f0484fSRodney W. Grimes prec = -1; 61658f0484fSRodney W. Grimes sign = '\0'; 617ebbad5ecSDavid Schultz ox[1] = '\0'; 61858f0484fSRodney W. Grimes 61958f0484fSRodney W. Grimes rflag: ch = *fmt++; 62058f0484fSRodney W. Grimes reswitch: switch (ch) { 62158f0484fSRodney W. Grimes case ' ': 6222e394b2fSAlexey Zelkin /*- 62358f0484fSRodney W. Grimes * ``If the space and + flags both appear, the space 62458f0484fSRodney W. Grimes * flag will be ignored.'' 62558f0484fSRodney W. Grimes * -- ANSI X3J11 62658f0484fSRodney W. Grimes */ 62758f0484fSRodney W. Grimes if (!sign) 62858f0484fSRodney W. Grimes sign = ' '; 62958f0484fSRodney W. Grimes goto rflag; 63058f0484fSRodney W. Grimes case '#': 63158f0484fSRodney W. Grimes flags |= ALT; 63258f0484fSRodney W. Grimes goto rflag; 63358f0484fSRodney W. Grimes case '*': 6342e394b2fSAlexey Zelkin /*- 63558f0484fSRodney W. Grimes * ``A negative field width argument is taken as a 63658f0484fSRodney W. Grimes * - flag followed by a positive field width.'' 63758f0484fSRodney W. Grimes * -- ANSI X3J11 63858f0484fSRodney W. Grimes * They don't exclude field widths read from args. 63958f0484fSRodney W. Grimes */ 640efb7e53dSJordan K. Hubbard GETASTER (width); 641efb7e53dSJordan K. Hubbard if (width >= 0) 64258f0484fSRodney W. Grimes goto rflag; 64358f0484fSRodney W. Grimes width = -width; 64458f0484fSRodney W. Grimes /* FALLTHROUGH */ 64558f0484fSRodney W. Grimes case '-': 64658f0484fSRodney W. Grimes flags |= LADJUST; 64758f0484fSRodney W. Grimes goto rflag; 64858f0484fSRodney W. Grimes case '+': 64958f0484fSRodney W. Grimes sign = '+'; 65058f0484fSRodney W. Grimes goto rflag; 6517735bb0fSBill Fenner case '\'': 65298ee7635SAlexey Zelkin flags |= GROUPING; 65398ee7635SAlexey Zelkin thousands_sep = *(localeconv()->thousands_sep); 65498ee7635SAlexey Zelkin grouping = localeconv()->grouping; 6557735bb0fSBill Fenner goto rflag; 65658f0484fSRodney W. Grimes case '.': 65758f0484fSRodney W. Grimes if ((ch = *fmt++) == '*') { 6583b204b7dSDavid Schultz GETASTER (prec); 65958f0484fSRodney W. Grimes goto rflag; 66058f0484fSRodney W. Grimes } 6613b204b7dSDavid Schultz prec = 0; 66258f0484fSRodney W. Grimes while (is_digit(ch)) { 6633b204b7dSDavid Schultz prec = 10 * prec + to_digit(ch); 66458f0484fSRodney W. Grimes ch = *fmt++; 66558f0484fSRodney W. Grimes } 66658f0484fSRodney W. Grimes goto reswitch; 66758f0484fSRodney W. Grimes case '0': 6682e394b2fSAlexey Zelkin /*- 66958f0484fSRodney W. Grimes * ``Note that 0 is taken as a flag, not as the 67058f0484fSRodney W. Grimes * beginning of a field width.'' 67158f0484fSRodney W. Grimes * -- ANSI X3J11 67258f0484fSRodney W. Grimes */ 67358f0484fSRodney W. Grimes flags |= ZEROPAD; 67458f0484fSRodney W. Grimes goto rflag; 67558f0484fSRodney W. Grimes case '1': case '2': case '3': case '4': 67658f0484fSRodney W. Grimes case '5': case '6': case '7': case '8': case '9': 67758f0484fSRodney W. Grimes n = 0; 67858f0484fSRodney W. Grimes do { 67958f0484fSRodney W. Grimes n = 10 * n + to_digit(ch); 68058f0484fSRodney W. Grimes ch = *fmt++; 68158f0484fSRodney W. Grimes } while (is_digit(ch)); 682efb7e53dSJordan K. Hubbard if (ch == '$') { 683efb7e53dSJordan K. Hubbard nextarg = n; 684efb7e53dSJordan K. Hubbard if (argtable == NULL) { 685efb7e53dSJordan K. Hubbard argtable = statargtable; 686efb7e53dSJordan K. Hubbard __find_arguments (fmt0, orgap, 687efb7e53dSJordan K. Hubbard &argtable); 688efb7e53dSJordan K. Hubbard } 689efb7e53dSJordan K. Hubbard goto rflag; 690efb7e53dSJordan K. Hubbard } 69158f0484fSRodney W. Grimes width = n; 69258f0484fSRodney W. Grimes goto reswitch; 6938de9e897SDavid Schultz #ifndef NO_FLOATING_POINT 69458f0484fSRodney W. Grimes case 'L': 69558f0484fSRodney W. Grimes flags |= LONGDBL; 69658f0484fSRodney W. Grimes goto rflag; 69758f0484fSRodney W. Grimes #endif 69858f0484fSRodney W. Grimes case 'h': 6997735bb0fSBill Fenner if (flags & SHORTINT) { 7007735bb0fSBill Fenner flags &= ~SHORTINT; 7017735bb0fSBill Fenner flags |= CHARINT; 7027735bb0fSBill Fenner } else 70358f0484fSRodney W. Grimes flags |= SHORTINT; 70458f0484fSRodney W. Grimes goto rflag; 7057735bb0fSBill Fenner case 'j': 7067735bb0fSBill Fenner flags |= INTMAXT; 7077735bb0fSBill Fenner goto rflag; 70858f0484fSRodney W. Grimes case 'l': 7097735bb0fSBill Fenner if (flags & LONGINT) { 7107735bb0fSBill Fenner flags &= ~LONGINT; 7117735bb0fSBill Fenner flags |= LLONGINT; 7127735bb0fSBill Fenner } else 71358f0484fSRodney W. Grimes flags |= LONGINT; 71458f0484fSRodney W. Grimes goto rflag; 71558f0484fSRodney W. Grimes case 'q': 7167735bb0fSBill Fenner flags |= LLONGINT; /* not necessarily */ 7177735bb0fSBill Fenner goto rflag; 7187735bb0fSBill Fenner case 't': 7197735bb0fSBill Fenner flags |= PTRDIFFT; 7207735bb0fSBill Fenner goto rflag; 7217735bb0fSBill Fenner case 'z': 7227735bb0fSBill Fenner flags |= SIZET; 72358f0484fSRodney W. Grimes goto rflag; 724927ecbf3STim J. Robbins case 'C': 725927ecbf3STim J. Robbins flags |= LONGINT; 726927ecbf3STim J. Robbins /*FALLTHROUGH*/ 72758f0484fSRodney W. Grimes case 'c': 728b9aac308STim J. Robbins if (flags & LONGINT) { 72993996f6dSTim J. Robbins static const mbstate_t initial; 73093996f6dSTim J. Robbins mbstate_t mbs; 731b9aac308STim J. Robbins size_t mbseqlen; 732b9aac308STim J. Robbins 73393996f6dSTim J. Robbins mbs = initial; 734b9aac308STim J. Robbins mbseqlen = wcrtomb(cp = buf, 73593996f6dSTim J. Robbins (wchar_t)GETARG(wint_t), &mbs); 7366180233fSTim J. Robbins if (mbseqlen == (size_t)-1) { 7376180233fSTim J. Robbins fp->_flags |= __SERR; 738b9aac308STim J. Robbins goto error; 7396180233fSTim J. Robbins } 740b9aac308STim J. Robbins size = (int)mbseqlen; 741b9aac308STim J. Robbins } else { 742efb7e53dSJordan K. Hubbard *(cp = buf) = GETARG(int); 74358f0484fSRodney W. Grimes size = 1; 744b9aac308STim J. Robbins } 74558f0484fSRodney W. Grimes sign = '\0'; 74658f0484fSRodney W. Grimes break; 74758f0484fSRodney W. Grimes case 'D': 74858f0484fSRodney W. Grimes flags |= LONGINT; 74958f0484fSRodney W. Grimes /*FALLTHROUGH*/ 75058f0484fSRodney W. Grimes case 'd': 75158f0484fSRodney W. Grimes case 'i': 7527735bb0fSBill Fenner if (flags & INTMAX_SIZE) { 7537735bb0fSBill Fenner ujval = SJARG(); 7547735bb0fSBill Fenner if ((intmax_t)ujval < 0) { 7557735bb0fSBill Fenner ujval = -ujval; 75658f0484fSRodney W. Grimes sign = '-'; 75758f0484fSRodney W. Grimes } 75858f0484fSRodney W. Grimes } else { 75958f0484fSRodney W. Grimes ulval = SARG(); 76058f0484fSRodney W. Grimes if ((long)ulval < 0) { 76158f0484fSRodney W. Grimes ulval = -ulval; 76258f0484fSRodney W. Grimes sign = '-'; 76358f0484fSRodney W. Grimes } 76458f0484fSRodney W. Grimes } 76558f0484fSRodney W. Grimes base = 10; 76658f0484fSRodney W. Grimes goto number; 7678de9e897SDavid Schultz #ifndef NO_FLOATING_POINT 7687735bb0fSBill Fenner case 'a': 7697735bb0fSBill Fenner case 'A': 770ebbad5ecSDavid Schultz if (ch == 'a') { 771ebbad5ecSDavid Schultz ox[1] = 'x'; 772ebbad5ecSDavid Schultz xdigs = xdigs_lower; 773ebbad5ecSDavid Schultz expchar = 'p'; 774ebbad5ecSDavid Schultz } else { 775ebbad5ecSDavid Schultz ox[1] = 'X'; 776ebbad5ecSDavid Schultz xdigs = xdigs_upper; 777ebbad5ecSDavid Schultz expchar = 'P'; 778ebbad5ecSDavid Schultz } 779904322a5SDavid Schultz if (prec >= 0) 780904322a5SDavid Schultz prec++; 781904322a5SDavid Schultz if (dtoaresult != NULL) 782904322a5SDavid Schultz freedtoa(dtoaresult); 783ebbad5ecSDavid Schultz if (flags & LONGDBL) { 784904322a5SDavid Schultz fparg.ldbl = GETARG(long double); 785ebbad5ecSDavid Schultz dtoaresult = cp = 786ebbad5ecSDavid Schultz __hldtoa(fparg.ldbl, xdigs, prec, 787ebbad5ecSDavid Schultz &expt, &signflag, &dtoaend); 788ebbad5ecSDavid Schultz } else { 789ebbad5ecSDavid Schultz fparg.dbl = GETARG(double); 790ebbad5ecSDavid Schultz dtoaresult = cp = 791ebbad5ecSDavid Schultz __hdtoa(fparg.dbl, xdigs, prec, 792ebbad5ecSDavid Schultz &expt, &signflag, &dtoaend); 793ebbad5ecSDavid Schultz } 794904322a5SDavid Schultz if (prec < 0) 795904322a5SDavid Schultz prec = dtoaend - cp; 796904322a5SDavid Schultz if (expt == INT_MAX) 797904322a5SDavid Schultz ox[1] = '\0'; 798904322a5SDavid Schultz goto fp_common; 799d26be6f0SBruce Evans case 'e': 80058f0484fSRodney W. Grimes case 'E': 801ebbad5ecSDavid Schultz expchar = ch; 802ebbad5ecSDavid Schultz if (prec < 0) /* account for digit before decpt */ 803ebbad5ecSDavid Schultz prec = DEFPREC + 1; 804ebbad5ecSDavid Schultz else 805ebbad5ecSDavid Schultz prec++; 806ebbad5ecSDavid Schultz goto fp_begin; 807d26be6f0SBruce Evans case 'f': 8087735bb0fSBill Fenner case 'F': 809ebbad5ecSDavid Schultz expchar = '\0'; 810d26be6f0SBruce Evans goto fp_begin; 81158f0484fSRodney W. Grimes case 'g': 81258f0484fSRodney W. Grimes case 'G': 813ebbad5ecSDavid Schultz expchar = ch - ('g' - 'e'); 814d26be6f0SBruce Evans if (prec == 0) 815d26be6f0SBruce Evans prec = 1; 816ebbad5ecSDavid Schultz fp_begin: 817ebbad5ecSDavid Schultz if (prec < 0) 81858f0484fSRodney W. Grimes prec = DEFPREC; 819ebbad5ecSDavid Schultz if (dtoaresult != NULL) 820ebbad5ecSDavid Schultz freedtoa(dtoaresult); 821ebbad5ecSDavid Schultz if (flags & LONGDBL) { 822ebbad5ecSDavid Schultz fparg.ldbl = GETARG(long double); 823ebbad5ecSDavid Schultz dtoaresult = cp = 824ebbad5ecSDavid Schultz __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec, 825ebbad5ecSDavid Schultz &expt, &signflag, &dtoaend); 826ebbad5ecSDavid Schultz } else { 827ebbad5ecSDavid Schultz fparg.dbl = GETARG(double); 828ebbad5ecSDavid Schultz dtoaresult = cp = 829ebbad5ecSDavid Schultz dtoa(fparg.dbl, expchar ? 2 : 3, prec, 830ebbad5ecSDavid Schultz &expt, &signflag, &dtoaend); 831ebbad5ecSDavid Schultz if (expt == 9999) 832ebbad5ecSDavid Schultz expt = INT_MAX; 83358f0484fSRodney W. Grimes } 834904322a5SDavid Schultz fp_common: 835ebbad5ecSDavid Schultz if (signflag) 836ebbad5ecSDavid Schultz sign = '-'; 837ebbad5ecSDavid Schultz if (expt == INT_MAX) { /* inf or nan */ 838ebbad5ecSDavid Schultz if (*cp == 'N') { 839ebbad5ecSDavid Schultz cp = (ch >= 'a') ? "nan" : "NAN"; 840ebbad5ecSDavid Schultz sign = '\0'; 841ebbad5ecSDavid Schultz } else 842ebbad5ecSDavid Schultz cp = (ch >= 'a') ? "inf" : "INF"; 84358f0484fSRodney W. Grimes size = 3; 844970a466cSDavid Schultz flags &= ~ZEROPAD; 84558f0484fSRodney W. Grimes break; 84658f0484fSRodney W. Grimes } 84758f0484fSRodney W. Grimes flags |= FPT; 848ebbad5ecSDavid Schultz ndig = dtoaend - cp; 84958f0484fSRodney W. Grimes if (ch == 'g' || ch == 'G') { 850ebbad5ecSDavid Schultz if (expt > -4 && expt <= prec) { 851ebbad5ecSDavid Schultz /* Make %[gG] smell like %[fF] */ 852ebbad5ecSDavid Schultz expchar = '\0'; 853ebbad5ecSDavid Schultz if (flags & ALT) 854ebbad5ecSDavid Schultz prec -= expt; 85558f0484fSRodney W. Grimes else 856ebbad5ecSDavid Schultz prec = ndig - expt; 857ebbad5ecSDavid Schultz if (prec < 0) 858ebbad5ecSDavid Schultz prec = 0; 8591f2a0cdfSDavid Schultz } else { 8601f2a0cdfSDavid Schultz /* 8611f2a0cdfSDavid Schultz * Make %[gG] smell like %[eE], but 8621f2a0cdfSDavid Schultz * trim trailing zeroes if no # flag. 8631f2a0cdfSDavid Schultz */ 8641f2a0cdfSDavid Schultz if (!(flags & ALT)) 8651f2a0cdfSDavid Schultz prec = ndig; 86658f0484fSRodney W. Grimes } 867ebbad5ecSDavid Schultz } 868ebbad5ecSDavid Schultz if (expchar) { 869ebbad5ecSDavid Schultz expsize = exponent(expstr, expt - 1, expchar); 870ebbad5ecSDavid Schultz size = expsize + prec; 8713b204b7dSDavid Schultz if (prec > 1 || flags & ALT) 87258f0484fSRodney W. Grimes ++size; 873ebbad5ecSDavid Schultz } else { 87481ae2e9aSDavid Schultz /* space for digits before decimal point */ 87581ae2e9aSDavid Schultz if (expt > 0) 87658f0484fSRodney W. Grimes size = expt; 87781ae2e9aSDavid Schultz else /* "0" */ 87881ae2e9aSDavid Schultz size = 1; 87981ae2e9aSDavid Schultz /* space for decimal pt and following digits */ 88058f0484fSRodney W. Grimes if (prec || flags & ALT) 88158f0484fSRodney W. Grimes size += prec + 1; 882ebbad5ecSDavid Schultz if (grouping && expt > 0) { 883ebbad5ecSDavid Schultz /* space for thousands' grouping */ 884ebbad5ecSDavid Schultz nseps = nrepeats = 0; 885ebbad5ecSDavid Schultz lead = expt; 886ebbad5ecSDavid Schultz while (*grouping != CHAR_MAX) { 887ebbad5ecSDavid Schultz if (lead <= *grouping) 888ebbad5ecSDavid Schultz break; 889ebbad5ecSDavid Schultz lead -= *grouping; 890ebbad5ecSDavid Schultz if (*(grouping+1)) { 891ebbad5ecSDavid Schultz nseps++; 892ebbad5ecSDavid Schultz grouping++; 89358f0484fSRodney W. Grimes } else 894ebbad5ecSDavid Schultz nrepeats++; 895ebbad5ecSDavid Schultz } 896ebbad5ecSDavid Schultz size += nseps + nrepeats; 897ebbad5ecSDavid Schultz } else 898d890afb8SDavid Schultz lead = expt; 899ebbad5ecSDavid Schultz } 90058f0484fSRodney W. Grimes break; 9018de9e897SDavid Schultz #endif /* !NO_FLOATING_POINT */ 90258f0484fSRodney W. Grimes case 'n': 9037735bb0fSBill Fenner /* 9047735bb0fSBill Fenner * Assignment-like behavior is specified if the 9057735bb0fSBill Fenner * value overflows or is otherwise unrepresentable. 9067735bb0fSBill Fenner * C99 says to use `signed char' for %hhn conversions. 9077735bb0fSBill Fenner */ 9087735bb0fSBill Fenner if (flags & LLONGINT) 9097735bb0fSBill Fenner *GETARG(long long *) = ret; 9107735bb0fSBill Fenner else if (flags & SIZET) 9117735bb0fSBill Fenner *GETARG(ssize_t *) = (ssize_t)ret; 9127735bb0fSBill Fenner else if (flags & PTRDIFFT) 9137735bb0fSBill Fenner *GETARG(ptrdiff_t *) = ret; 9147735bb0fSBill Fenner else if (flags & INTMAXT) 9157735bb0fSBill Fenner *GETARG(intmax_t *) = ret; 91658f0484fSRodney W. Grimes else if (flags & LONGINT) 9176e690ad4SAndrey A. Chernov *GETARG(long *) = ret; 91858f0484fSRodney W. Grimes else if (flags & SHORTINT) 9196e690ad4SAndrey A. Chernov *GETARG(short *) = ret; 9207735bb0fSBill Fenner else if (flags & CHARINT) 9217735bb0fSBill Fenner *GETARG(signed char *) = ret; 92258f0484fSRodney W. Grimes else 9236e690ad4SAndrey A. Chernov *GETARG(int *) = ret; 92458f0484fSRodney W. Grimes continue; /* no output */ 92558f0484fSRodney W. Grimes case 'O': 92658f0484fSRodney W. Grimes flags |= LONGINT; 92758f0484fSRodney W. Grimes /*FALLTHROUGH*/ 92858f0484fSRodney W. Grimes case 'o': 9297735bb0fSBill Fenner if (flags & INTMAX_SIZE) 9307735bb0fSBill Fenner ujval = UJARG(); 93158f0484fSRodney W. Grimes else 93258f0484fSRodney W. Grimes ulval = UARG(); 93358f0484fSRodney W. Grimes base = 8; 93458f0484fSRodney W. Grimes goto nosign; 93558f0484fSRodney W. Grimes case 'p': 9362e394b2fSAlexey Zelkin /*- 93758f0484fSRodney W. Grimes * ``The argument shall be a pointer to void. The 93858f0484fSRodney W. Grimes * value of the pointer is converted to a sequence 93958f0484fSRodney W. Grimes * of printable characters, in an implementation- 94058f0484fSRodney W. Grimes * defined manner.'' 94158f0484fSRodney W. Grimes * -- ANSI X3J11 94258f0484fSRodney W. Grimes */ 9437735bb0fSBill Fenner ujval = (uintmax_t)(uintptr_t)GETARG(void *); 94458f0484fSRodney W. Grimes base = 16; 945ebbad5ecSDavid Schultz xdigs = xdigs_lower; 946ebbad5ecSDavid Schultz flags = flags | INTMAXT; 947ebbad5ecSDavid Schultz ox[1] = 'x'; 94858f0484fSRodney W. Grimes goto nosign; 949927ecbf3STim J. Robbins case 'S': 950927ecbf3STim J. Robbins flags |= LONGINT; 951927ecbf3STim J. Robbins /*FALLTHROUGH*/ 95258f0484fSRodney W. Grimes case 's': 953b9aac308STim J. Robbins if (flags & LONGINT) { 954b9aac308STim J. Robbins wchar_t *wcp; 955b9aac308STim J. Robbins 956b9aac308STim J. Robbins if (convbuf != NULL) 957b9aac308STim J. Robbins free(convbuf); 958b9aac308STim J. Robbins if ((wcp = GETARG(wchar_t *)) == NULL) 959b9aac308STim J. Robbins cp = "(null)"; 960b9aac308STim J. Robbins else { 961b9aac308STim J. Robbins convbuf = __wcsconv(wcp, prec); 9626180233fSTim J. Robbins if (convbuf == NULL) { 9636180233fSTim J. Robbins fp->_flags |= __SERR; 964b9aac308STim J. Robbins goto error; 9656180233fSTim J. Robbins } 966b9aac308STim J. Robbins cp = convbuf; 967b9aac308STim J. Robbins } 968b9aac308STim J. Robbins } else if ((cp = GETARG(char *)) == NULL) 96958f0484fSRodney W. Grimes cp = "(null)"; 97058f0484fSRodney W. Grimes if (prec >= 0) { 97158f0484fSRodney W. Grimes /* 97258f0484fSRodney W. Grimes * can't use strlen; can only look for the 97358f0484fSRodney W. Grimes * NUL in the first `prec' characters, and 97458f0484fSRodney W. Grimes * strlen() will go further. 97558f0484fSRodney W. Grimes */ 976ce51cf03SJames Raynard char *p = memchr(cp, 0, (size_t)prec); 97758f0484fSRodney W. Grimes 97858f0484fSRodney W. Grimes if (p != NULL) { 97958f0484fSRodney W. Grimes size = p - cp; 98058f0484fSRodney W. Grimes if (size > prec) 98158f0484fSRodney W. Grimes size = prec; 98258f0484fSRodney W. Grimes } else 98358f0484fSRodney W. Grimes size = prec; 98458f0484fSRodney W. Grimes } else 98558f0484fSRodney W. Grimes size = strlen(cp); 98658f0484fSRodney W. Grimes sign = '\0'; 98758f0484fSRodney W. Grimes break; 98858f0484fSRodney W. Grimes case 'U': 98958f0484fSRodney W. Grimes flags |= LONGINT; 99058f0484fSRodney W. Grimes /*FALLTHROUGH*/ 99158f0484fSRodney W. Grimes case 'u': 9927735bb0fSBill Fenner if (flags & INTMAX_SIZE) 9937735bb0fSBill Fenner ujval = UJARG(); 99458f0484fSRodney W. Grimes else 99558f0484fSRodney W. Grimes ulval = UARG(); 99658f0484fSRodney W. Grimes base = 10; 99758f0484fSRodney W. Grimes goto nosign; 99858f0484fSRodney W. Grimes case 'X': 999ebbad5ecSDavid Schultz xdigs = xdigs_upper; 100058f0484fSRodney W. Grimes goto hex; 100158f0484fSRodney W. Grimes case 'x': 1002ebbad5ecSDavid Schultz xdigs = xdigs_lower; 10037735bb0fSBill Fenner hex: 10047735bb0fSBill Fenner if (flags & INTMAX_SIZE) 10057735bb0fSBill Fenner ujval = UJARG(); 100658f0484fSRodney W. Grimes else 100758f0484fSRodney W. Grimes ulval = UARG(); 100858f0484fSRodney W. Grimes base = 16; 100958f0484fSRodney W. Grimes /* leading 0x/X only if non-zero */ 101058f0484fSRodney W. Grimes if (flags & ALT && 10117735bb0fSBill Fenner (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0)) 1012ebbad5ecSDavid Schultz ox[1] = ch; 101358f0484fSRodney W. Grimes 101498ee7635SAlexey Zelkin flags &= ~GROUPING; 101558f0484fSRodney W. Grimes /* unsigned conversions */ 101658f0484fSRodney W. Grimes nosign: sign = '\0'; 10172e394b2fSAlexey Zelkin /*- 101858f0484fSRodney W. Grimes * ``... diouXx conversions ... if a precision is 101958f0484fSRodney W. Grimes * specified, the 0 flag will be ignored.'' 102058f0484fSRodney W. Grimes * -- ANSI X3J11 102158f0484fSRodney W. Grimes */ 102258f0484fSRodney W. Grimes number: if ((dprec = prec) >= 0) 102358f0484fSRodney W. Grimes flags &= ~ZEROPAD; 102458f0484fSRodney W. Grimes 10252e394b2fSAlexey Zelkin /*- 102658f0484fSRodney W. Grimes * ``The result of converting a zero value with an 102758f0484fSRodney W. Grimes * explicit precision of zero is no characters.'' 102858f0484fSRodney W. Grimes * -- ANSI X3J11 10291be5319aSDavid Schultz * 10301be5319aSDavid Schultz * ``The C Standard is clear enough as is. The call 10311be5319aSDavid Schultz * printf("%#.0o", 0) should print 0.'' 10321be5319aSDavid Schultz * -- Defect Report #151 103358f0484fSRodney W. Grimes */ 103458f0484fSRodney W. Grimes cp = buf + BUF; 10357735bb0fSBill Fenner if (flags & INTMAX_SIZE) { 10361be5319aSDavid Schultz if (ujval != 0 || prec != 0 || 10371be5319aSDavid Schultz (flags & ALT && base == 8)) 10387735bb0fSBill Fenner cp = __ujtoa(ujval, cp, base, 103998ee7635SAlexey Zelkin flags & ALT, xdigs, 104098ee7635SAlexey Zelkin flags & GROUPING, thousands_sep, 104198ee7635SAlexey Zelkin grouping); 104258f0484fSRodney W. Grimes } else { 10431be5319aSDavid Schultz if (ulval != 0 || prec != 0 || 10441be5319aSDavid Schultz (flags & ALT && base == 8)) 104558f0484fSRodney W. Grimes cp = __ultoa(ulval, cp, base, 104698ee7635SAlexey Zelkin flags & ALT, xdigs, 104798ee7635SAlexey Zelkin flags & GROUPING, thousands_sep, 104898ee7635SAlexey Zelkin grouping); 104958f0484fSRodney W. Grimes } 105058f0484fSRodney W. Grimes size = buf + BUF - cp; 105138cac8f8SDavid Schultz if (size > BUF) /* should never happen */ 105238cac8f8SDavid Schultz abort(); 105358f0484fSRodney W. Grimes break; 105458f0484fSRodney W. Grimes default: /* "%?" prints ?, unless ? is NUL */ 105558f0484fSRodney W. Grimes if (ch == '\0') 105658f0484fSRodney W. Grimes goto done; 105758f0484fSRodney W. Grimes /* pretend it was %c with argument ch */ 105858f0484fSRodney W. Grimes cp = buf; 105958f0484fSRodney W. Grimes *cp = ch; 106058f0484fSRodney W. Grimes size = 1; 106158f0484fSRodney W. Grimes sign = '\0'; 106258f0484fSRodney W. Grimes break; 106358f0484fSRodney W. Grimes } 106458f0484fSRodney W. Grimes 106558f0484fSRodney W. Grimes /* 106658f0484fSRodney W. Grimes * All reasonable formats wind up here. At this point, `cp' 106758f0484fSRodney W. Grimes * points to a string which (if not flags&LADJUST) should be 106858f0484fSRodney W. Grimes * padded out to `width' places. If flags&ZEROPAD, it should 106958f0484fSRodney W. Grimes * first be prefixed by any sign or other prefix; otherwise, 107058f0484fSRodney W. Grimes * it should be blank padded before the prefix is emitted. 107158f0484fSRodney W. Grimes * After any left-hand padding and prefixing, emit zeroes 107258f0484fSRodney W. Grimes * required by a decimal [diouxX] precision, then print the 107358f0484fSRodney W. Grimes * string proper, then emit zeroes required by any leftover 107458f0484fSRodney W. Grimes * floating precision; finally, if LADJUST, pad with blanks. 107558f0484fSRodney W. Grimes * 107658f0484fSRodney W. Grimes * Compute actual size, so we know how much to pad. 1077261a532aSBill Fenner * size excludes decimal prec; realsz includes it. 107858f0484fSRodney W. Grimes */ 1079261a532aSBill Fenner realsz = dprec > size ? dprec : size; 108058f0484fSRodney W. Grimes if (sign) 1081261a532aSBill Fenner realsz++; 1082904322a5SDavid Schultz if (ox[1]) 1083261a532aSBill Fenner realsz += 2; 108458f0484fSRodney W. Grimes 108592e88f87SAndrey A. Chernov prsize = width > realsz ? width : realsz; 1086b250f248SAndrey A. Chernov if ((unsigned)ret + prsize > INT_MAX) { 108792e88f87SAndrey A. Chernov ret = EOF; 108892e88f87SAndrey A. Chernov goto error; 108992e88f87SAndrey A. Chernov } 109092e88f87SAndrey A. Chernov 109158f0484fSRodney W. Grimes /* right-adjusting blank padding */ 109258f0484fSRodney W. Grimes if ((flags & (LADJUST|ZEROPAD)) == 0) 109358f0484fSRodney W. Grimes PAD(width - realsz, blanks); 109458f0484fSRodney W. Grimes 109558f0484fSRodney W. Grimes /* prefix */ 1096904322a5SDavid Schultz if (sign) 109758f0484fSRodney W. Grimes PRINT(&sign, 1); 1098904322a5SDavid Schultz 1099904322a5SDavid Schultz if (ox[1]) { /* ox[1] is either x, X, or \0 */ 110058f0484fSRodney W. Grimes ox[0] = '0'; 110158f0484fSRodney W. Grimes PRINT(ox, 2); 110258f0484fSRodney W. Grimes } 110358f0484fSRodney W. Grimes 110458f0484fSRodney W. Grimes /* right-adjusting zero padding */ 110558f0484fSRodney W. Grimes if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) 110658f0484fSRodney W. Grimes PAD(width - realsz, zeroes); 110758f0484fSRodney W. Grimes 110858f0484fSRodney W. Grimes /* leading zeroes from decimal precision */ 1109261a532aSBill Fenner PAD(dprec - size, zeroes); 111058f0484fSRodney W. Grimes 111158f0484fSRodney W. Grimes /* the string or number proper */ 11128de9e897SDavid Schultz #ifndef NO_FLOATING_POINT 111358f0484fSRodney W. Grimes if ((flags & FPT) == 0) { 111458f0484fSRodney W. Grimes PRINT(cp, size); 111558f0484fSRodney W. Grimes } else { /* glue together f_p fragments */ 1116ebbad5ecSDavid Schultz if (!expchar) { /* %[fF] or sufficiently short %[gG] */ 1117ebbad5ecSDavid Schultz if (expt <= 0) { 111881ae2e9aSDavid Schultz PRINT(zeroes, 1); 111981ae2e9aSDavid Schultz if (prec || flags & ALT) 112081ae2e9aSDavid Schultz PRINT(decimal_point, 1); 112158f0484fSRodney W. Grimes PAD(-expt, zeroes); 11223b204b7dSDavid Schultz /* already handled initial 0's */ 11233b204b7dSDavid Schultz prec += expt; 112458f0484fSRodney W. Grimes } else { 11253b204b7dSDavid Schultz PRINTANDPAD(cp, dtoaend, lead, zeroes); 1126ebbad5ecSDavid Schultz cp += lead; 1127ebbad5ecSDavid Schultz if (grouping) { 1128ebbad5ecSDavid Schultz while (nseps>0 || nrepeats>0) { 1129ebbad5ecSDavid Schultz if (nrepeats > 0) 1130ebbad5ecSDavid Schultz nrepeats--; 1131ebbad5ecSDavid Schultz else { 1132ebbad5ecSDavid Schultz grouping--; 1133ebbad5ecSDavid Schultz nseps--; 113458f0484fSRodney W. Grimes } 1135ebbad5ecSDavid Schultz PRINT(&thousands_sep, 1136ebbad5ecSDavid Schultz 1); 11373b204b7dSDavid Schultz PRINTANDPAD(cp,dtoaend, 11383b204b7dSDavid Schultz *grouping, zeroes); 1139ebbad5ecSDavid Schultz cp += *grouping; 1140ebbad5ecSDavid Schultz } 11413b204b7dSDavid Schultz if (cp > dtoaend) 11423b204b7dSDavid Schultz cp = dtoaend; 1143ebbad5ecSDavid Schultz } 1144ebbad5ecSDavid Schultz if (prec || flags & ALT) 1145ebbad5ecSDavid Schultz PRINT(decimal_point,1); 1146ebbad5ecSDavid Schultz } 11473b204b7dSDavid Schultz PRINTANDPAD(cp, dtoaend, prec, zeroes); 1148ebbad5ecSDavid Schultz } else { /* %[eE] or sufficiently long %[gG] */ 11493b204b7dSDavid Schultz if (prec > 1 || flags & ALT) { 1150ebbad5ecSDavid Schultz buf[0] = *cp++; 1151ebbad5ecSDavid Schultz buf[1] = *decimal_point; 1152ebbad5ecSDavid Schultz PRINT(buf, 2); 115358f0484fSRodney W. Grimes PRINT(cp, ndig-1); 1154ebbad5ecSDavid Schultz PAD(prec - ndig, zeroes); 115558f0484fSRodney W. Grimes } else /* XeYYY */ 115658f0484fSRodney W. Grimes PRINT(cp, 1); 115758f0484fSRodney W. Grimes PRINT(expstr, expsize); 115858f0484fSRodney W. Grimes } 115958f0484fSRodney W. Grimes } 116058f0484fSRodney W. Grimes #else 116158f0484fSRodney W. Grimes PRINT(cp, size); 116258f0484fSRodney W. Grimes #endif 116358f0484fSRodney W. Grimes /* left-adjusting padding (always blank) */ 116458f0484fSRodney W. Grimes if (flags & LADJUST) 116558f0484fSRodney W. Grimes PAD(width - realsz, blanks); 116658f0484fSRodney W. Grimes 116758f0484fSRodney W. Grimes /* finally, adjust ret */ 116892e88f87SAndrey A. Chernov ret += prsize; 116958f0484fSRodney W. Grimes 117058f0484fSRodney W. Grimes FLUSH(); /* copy out the I/O vectors */ 117158f0484fSRodney W. Grimes } 117258f0484fSRodney W. Grimes done: 117358f0484fSRodney W. Grimes FLUSH(); 117458f0484fSRodney W. Grimes error: 1175096ad104SDag-Erling Smørgrav va_end(orgap); 11768de9e897SDavid Schultz #ifndef NO_FLOATING_POINT 11772ffc61baSTor Egge if (dtoaresult != NULL) 1178ebbad5ecSDavid Schultz freedtoa(dtoaresult); 11792ffc61baSTor Egge #endif 1180b9aac308STim J. Robbins if (convbuf != NULL) 1181b9aac308STim J. Robbins free(convbuf); 1182f70177e7SJulian Elischer if (__sferror(fp)) 1183f70177e7SJulian Elischer ret = EOF; 1184efb7e53dSJordan K. Hubbard if ((argtable != NULL) && (argtable != statargtable)) 1185efb7e53dSJordan K. Hubbard free (argtable); 1186f70177e7SJulian Elischer return (ret); 118758f0484fSRodney W. Grimes /* NOTREACHED */ 118858f0484fSRodney W. Grimes } 118958f0484fSRodney W. Grimes 1190efb7e53dSJordan K. Hubbard 11918de9e897SDavid Schultz #ifndef NO_FLOATING_POINT 119258f0484fSRodney W. Grimes 119358f0484fSRodney W. Grimes static int 1194d201fe46SDaniel Eischen exponent(char *p0, int exp, int fmtch) 119558f0484fSRodney W. Grimes { 1196d201fe46SDaniel Eischen char *p, *t; 119738cac8f8SDavid Schultz char expbuf[MAXEXPDIG]; 119858f0484fSRodney W. Grimes 119958f0484fSRodney W. Grimes p = p0; 120058f0484fSRodney W. Grimes *p++ = fmtch; 120158f0484fSRodney W. Grimes if (exp < 0) { 120258f0484fSRodney W. Grimes exp = -exp; 120358f0484fSRodney W. Grimes *p++ = '-'; 120458f0484fSRodney W. Grimes } 120558f0484fSRodney W. Grimes else 120658f0484fSRodney W. Grimes *p++ = '+'; 120738cac8f8SDavid Schultz t = expbuf + MAXEXPDIG; 120858f0484fSRodney W. Grimes if (exp > 9) { 120958f0484fSRodney W. Grimes do { 121058f0484fSRodney W. Grimes *--t = to_char(exp % 10); 121158f0484fSRodney W. Grimes } while ((exp /= 10) > 9); 121258f0484fSRodney W. Grimes *--t = to_char(exp); 121338cac8f8SDavid Schultz for (; t < expbuf + MAXEXPDIG; *p++ = *t++); 121458f0484fSRodney W. Grimes } 121558f0484fSRodney W. Grimes else { 1216ebbad5ecSDavid Schultz /* 1217ebbad5ecSDavid Schultz * Exponents for decimal floating point conversions 1218ebbad5ecSDavid Schultz * (%[eEgG]) must be at least two characters long, 1219ebbad5ecSDavid Schultz * whereas exponents for hexadecimal conversions can 1220ebbad5ecSDavid Schultz * be only one character long. 1221ebbad5ecSDavid Schultz */ 1222ebbad5ecSDavid Schultz if (fmtch == 'e' || fmtch == 'E') 122358f0484fSRodney W. Grimes *p++ = '0'; 122458f0484fSRodney W. Grimes *p++ = to_char(exp); 122558f0484fSRodney W. Grimes } 122658f0484fSRodney W. Grimes return (p - p0); 122758f0484fSRodney W. Grimes } 12288de9e897SDavid Schultz #endif /* !NO_FLOATING_POINT */ 1229