Home | History | Annotate | Line # | Download | only in stdio
vfprintf.c revision 1.35.2.2
      1  1.35.2.2   nathanw /*	$NetBSD: vfprintf.c,v 1.35.2.2 2001/11/14 19:32:02 nathanw Exp $	*/
      2      1.18    kleink 
      3       1.1       cgd /*-
      4       1.1       cgd  * Copyright (c) 1990 The Regents of the University of California.
      5       1.1       cgd  * All rights reserved.
      6       1.1       cgd  *
      7       1.1       cgd  * This code is derived from software contributed to Berkeley by
      8       1.1       cgd  * Chris Torek.
      9       1.1       cgd  *
     10       1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11       1.1       cgd  * modification, are permitted provided that the following conditions
     12       1.1       cgd  * are met:
     13       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18       1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     19       1.1       cgd  *    must display the following acknowledgement:
     20       1.1       cgd  *	This product includes software developed by the University of
     21       1.1       cgd  *	California, Berkeley and its contributors.
     22       1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     23       1.1       cgd  *    may be used to endorse or promote products derived from this software
     24       1.1       cgd  *    without specific prior written permission.
     25       1.1       cgd  *
     26       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36       1.1       cgd  * SUCH DAMAGE.
     37       1.1       cgd  */
     38       1.1       cgd 
     39      1.20  christos #include <sys/cdefs.h>
     40       1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
     41      1.20  christos #if 0
     42      1.20  christos static char *sccsid = "@(#)vfprintf.c	5.50 (Berkeley) 12/16/92";
     43      1.20  christos #else
     44  1.35.2.2   nathanw __RCSID("$NetBSD: vfprintf.c,v 1.35.2.2 2001/11/14 19:32:02 nathanw Exp $");
     45      1.20  christos #endif
     46       1.1       cgd #endif /* LIBC_SCCS and not lint */
     47       1.1       cgd 
     48       1.1       cgd /*
     49       1.1       cgd  * Actual printf innards.
     50       1.1       cgd  *
     51       1.1       cgd  * This code is large and complicated...
     52       1.1       cgd  */
     53       1.1       cgd 
     54      1.29    kleink #include "namespace.h"
     55       1.1       cgd #include <sys/types.h>
     56      1.31     lukem 
     57      1.31     lukem #include <assert.h>
     58      1.19    kleink #include <errno.h>
     59  1.35.2.1   nathanw #include <stddef.h>
     60  1.35.2.1   nathanw #include <stdint.h>
     61       1.1       cgd #include <stdio.h>
     62       1.4       cgd #include <stdlib.h>
     63       1.1       cgd #include <string.h>
     64      1.34    itojun #include <wchar.h>
     65       1.4       cgd 
     66       1.1       cgd #if __STDC__
     67       1.1       cgd #include <stdarg.h>
     68       1.1       cgd #else
     69       1.1       cgd #include <varargs.h>
     70       1.1       cgd #endif
     71       1.4       cgd 
     72       1.1       cgd #include "local.h"
     73       1.1       cgd #include "fvwrite.h"
     74      1.20  christos #include "extern.h"
     75      1.28    kleink #include "reentrant.h"
     76      1.20  christos 
     77      1.20  christos static int __sprint __P((FILE *, struct __suio *));
     78      1.33  sommerfe static int __sbprintf __P((FILE *, const char *, va_list))
     79      1.33  sommerfe      __attribute__((__format__(__printf__, 2, 0)));
     80       1.1       cgd 
     81       1.1       cgd /*
     82       1.1       cgd  * Flush out all the vectors defined by the given uio,
     83       1.1       cgd  * then reset it so that it can be reused.
     84       1.1       cgd  */
     85       1.1       cgd static int
     86       1.1       cgd __sprint(fp, uio)
     87       1.1       cgd 	FILE *fp;
     88      1.23     perry 	struct __suio *uio;
     89       1.1       cgd {
     90      1.23     perry 	int err;
     91       1.1       cgd 
     92      1.31     lukem 	_DIAGASSERT(fp != NULL);
     93      1.31     lukem 	_DIAGASSERT(uio != NULL);
     94      1.31     lukem 
     95       1.1       cgd 	if (uio->uio_resid == 0) {
     96       1.1       cgd 		uio->uio_iovcnt = 0;
     97       1.1       cgd 		return (0);
     98       1.1       cgd 	}
     99       1.1       cgd 	err = __sfvwrite(fp, uio);
    100       1.1       cgd 	uio->uio_resid = 0;
    101       1.1       cgd 	uio->uio_iovcnt = 0;
    102       1.1       cgd 	return (err);
    103       1.1       cgd }
    104       1.1       cgd 
    105       1.1       cgd /*
    106       1.1       cgd  * Helper function for `fprintf to unbuffered unix file': creates a
    107       1.1       cgd  * temporary buffer.  We only work on write-only files; this avoids
    108       1.1       cgd  * worries about ungetc buffers and so forth.
    109       1.1       cgd  */
    110       1.1       cgd static int
    111       1.1       cgd __sbprintf(fp, fmt, ap)
    112      1.23     perry 	FILE *fp;
    113       1.1       cgd 	const char *fmt;
    114       1.1       cgd 	va_list ap;
    115       1.1       cgd {
    116       1.1       cgd 	int ret;
    117       1.1       cgd 	FILE fake;
    118       1.1       cgd 	unsigned char buf[BUFSIZ];
    119       1.1       cgd 
    120      1.31     lukem 	_DIAGASSERT(fp != NULL);
    121      1.31     lukem 	_DIAGASSERT(fmt != NULL);
    122      1.31     lukem 
    123       1.1       cgd 	/* copy the important variables */
    124       1.1       cgd 	fake._flags = fp->_flags & ~__SNBF;
    125       1.1       cgd 	fake._file = fp->_file;
    126       1.1       cgd 	fake._cookie = fp->_cookie;
    127       1.1       cgd 	fake._write = fp->_write;
    128       1.1       cgd 
    129       1.1       cgd 	/* set up the buffer */
    130       1.1       cgd 	fake._bf._base = fake._p = buf;
    131       1.1       cgd 	fake._bf._size = fake._w = sizeof(buf);
    132       1.1       cgd 	fake._lbfsize = 0;	/* not actually used, but Just In Case */
    133       1.1       cgd 
    134       1.1       cgd 	/* do the work, then copy any error status */
    135       1.1       cgd 	ret = vfprintf(&fake, fmt, ap);
    136       1.1       cgd 	if (ret >= 0 && fflush(&fake))
    137      1.22    kleink 		ret = -1;
    138       1.1       cgd 	if (fake._flags & __SERR)
    139       1.1       cgd 		fp->_flags |= __SERR;
    140       1.1       cgd 	return (ret);
    141       1.1       cgd }
    142       1.1       cgd 
    143       1.1       cgd 
    144       1.1       cgd #ifdef FLOATING_POINT
    145       1.9       jtc #include <locale.h>
    146       1.4       cgd #include <math.h>
    147       1.1       cgd #include "floatio.h"
    148       1.1       cgd 
    149       1.1       cgd #define	BUF		(MAXEXP+MAXFRACT+1)	/* + decimal point */
    150       1.1       cgd #define	DEFPREC		6
    151       1.1       cgd 
    152       1.4       cgd static char *cvt __P((double, int, int, char *, int *, int, int *));
    153       1.4       cgd static int exponent __P((char *, int, int));
    154       1.1       cgd 
    155       1.1       cgd #else /* no FLOATING_POINT */
    156       1.1       cgd 
    157       1.1       cgd #define	BUF		40
    158       1.1       cgd 
    159       1.1       cgd #endif /* FLOATING_POINT */
    160       1.1       cgd 
    161      1.30  christos #ifdef lint
    162      1.30  christos static __inline void *__UNCONST __P((const void *));
    163      1.30  christos static __inline void *
    164      1.30  christos __UNCONST(v)
    165      1.30  christos 	const void *v;
    166      1.30  christos {
    167      1.30  christos 	/* LINTED */
    168      1.30  christos 	return (void *) v;
    169      1.30  christos }
    170      1.30  christos #else
    171      1.30  christos #define __UNCONST(v)	(void *)(v)
    172      1.30  christos #endif
    173       1.1       cgd 
    174       1.1       cgd /*
    175       1.1       cgd  * Macros for converting digits to letters and vice versa
    176       1.1       cgd  */
    177       1.1       cgd #define	to_digit(c)	((c) - '0')
    178       1.1       cgd #define is_digit(c)	((unsigned)to_digit(c) <= 9)
    179      1.30  christos #define	to_char(n)	((char)((n) + '0'))
    180       1.1       cgd 
    181       1.1       cgd /*
    182       1.1       cgd  * Flags used during conversion.
    183       1.1       cgd  */
    184       1.4       cgd #define	ALT		0x001		/* alternate form */
    185       1.4       cgd #define	HEXPREFIX	0x002		/* add 0x or 0X prefix */
    186       1.4       cgd #define	LADJUST		0x004		/* left adjustment */
    187       1.4       cgd #define	LONGDBL		0x008		/* long double; unimplemented */
    188       1.4       cgd #define	LONGINT		0x010		/* long integer */
    189       1.4       cgd #define	QUADINT		0x020		/* quad integer */
    190       1.4       cgd #define	SHORTINT	0x040		/* short integer */
    191  1.35.2.1   nathanw #define	MAXINT		0x080		/* (unsigned) intmax_t */
    192  1.35.2.1   nathanw #define	PTRINT		0x100		/* (unsigned) ptrdiff_t */
    193  1.35.2.1   nathanw #define	SIZEINT		0x200		/* (signed) size_t */
    194  1.35.2.1   nathanw #define	ZEROPAD		0x400		/* zero (as opposed to blank) pad */
    195  1.35.2.1   nathanw #define FPT		0x800		/* Floating point number */
    196       1.1       cgd int
    197       1.1       cgd vfprintf(fp, fmt0, ap)
    198       1.1       cgd 	FILE *fp;
    199       1.1       cgd 	const char *fmt0;
    200      1.10       cgd 	_BSD_VA_LIST_ ap;
    201       1.1       cgd {
    202      1.27   mycroft 	const char *fmt;/* format string */
    203      1.23     perry 	int ch;	/* character from fmt */
    204      1.23     perry 	int n, m;	/* handy integers (short term usage) */
    205      1.27   mycroft 	const char *cp;	/* handy char pointer (short term usage) */
    206      1.27   mycroft 	char *bp;	/* handy char pointer (short term usage) */
    207      1.23     perry 	struct __siov *iovp;/* for PRINT macro */
    208      1.23     perry 	int flags;	/* flags as above */
    209       1.1       cgd 	int ret;		/* return value accumulator */
    210       1.1       cgd 	int width;		/* width from format (%8d), or 0 */
    211       1.1       cgd 	int prec;		/* precision from format (%.3d), or -1 */
    212       1.1       cgd 	char sign;		/* sign prefix (' ', '+', '-', or \0) */
    213      1.17       jtc 	wchar_t wc;
    214      1.34    itojun 	mbstate_t ps;
    215       1.1       cgd #ifdef FLOATING_POINT
    216      1.12       jtc 	char *decimal_point = localeconv()->decimal_point;
    217       1.1       cgd 	char softsign;		/* temporary negative sign for floats */
    218      1.20  christos 	double _double = 0;	/* double precision arguments %[eEfgG] */
    219       1.4       cgd 	int expt;		/* integer value of exponent */
    220      1.20  christos 	int expsize = 0;	/* character count for expstr */
    221       1.4       cgd 	int ndig;		/* actual number of digits returned by cvt */
    222       1.4       cgd 	char expstr[7];		/* buffer for exponent string */
    223       1.1       cgd #endif
    224       1.4       cgd 
    225       1.4       cgd #ifdef __GNUC__			/* gcc has builtin quad type (long long) SOS */
    226       1.4       cgd #define	quad_t	  long long
    227       1.4       cgd #define	u_quad_t  unsigned long long
    228       1.4       cgd #endif
    229       1.4       cgd 
    230  1.35.2.1   nathanw #define	INTMAX_T	intmax_t
    231  1.35.2.1   nathanw #define	UINTMAX_T	uintmax_t
    232  1.35.2.1   nathanw 
    233  1.35.2.1   nathanw 	UINTMAX_T _uintmax;	/* integer arguments %[diouxX] */
    234       1.1       cgd 	enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
    235       1.1       cgd 	int dprec;		/* a copy of prec if [diouxX], 0 otherwise */
    236       1.1       cgd 	int realsz;		/* field size expanded by dprec */
    237       1.1       cgd 	int size;		/* size of converted field or string */
    238      1.20  christos 	char *xdigs = NULL;	/* digits for [xX] conversion */
    239       1.1       cgd #define NIOV 8
    240       1.1       cgd 	struct __suio uio;	/* output information: summary */
    241       1.1       cgd 	struct __siov iov[NIOV];/* ... and individual io vectors */
    242       1.1       cgd 	char buf[BUF];		/* space for %c, %[diouxX], %[eEfgG] */
    243       1.1       cgd 	char ox[2];		/* space for 0x hex-prefix */
    244       1.1       cgd 
    245       1.1       cgd 	/*
    246       1.4       cgd 	 * Choose PADSIZE to trade efficiency vs. size.  If larger printf
    247       1.4       cgd 	 * fields occur frequently, increase PADSIZE and make the initialisers
    248       1.4       cgd 	 * below longer.
    249       1.1       cgd 	 */
    250       1.1       cgd #define	PADSIZE	16		/* pad chunk size */
    251      1.24   mycroft 	static const char blanks[PADSIZE] =
    252       1.1       cgd 	 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
    253      1.24   mycroft 	static const char zeroes[PADSIZE] =
    254       1.1       cgd 	 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
    255       1.2   mycroft 
    256       1.1       cgd 	/*
    257       1.1       cgd 	 * BEWARE, these `goto error' on error, and PAD uses `n'.
    258       1.1       cgd 	 */
    259       1.1       cgd #define	PRINT(ptr, len) { \
    260      1.30  christos 	iovp->iov_base = __UNCONST(ptr); \
    261       1.1       cgd 	iovp->iov_len = (len); \
    262       1.1       cgd 	uio.uio_resid += (len); \
    263       1.1       cgd 	iovp++; \
    264       1.1       cgd 	if (++uio.uio_iovcnt >= NIOV) { \
    265       1.1       cgd 		if (__sprint(fp, &uio)) \
    266       1.1       cgd 			goto error; \
    267       1.1       cgd 		iovp = iov; \
    268       1.1       cgd 	} \
    269       1.1       cgd }
    270       1.1       cgd #define	PAD(howmany, with) { \
    271       1.1       cgd 	if ((n = (howmany)) > 0) { \
    272       1.1       cgd 		while (n > PADSIZE) { \
    273       1.1       cgd 			PRINT(with, PADSIZE); \
    274       1.1       cgd 			n -= PADSIZE; \
    275       1.1       cgd 		} \
    276       1.1       cgd 		PRINT(with, n); \
    277       1.1       cgd 	} \
    278       1.1       cgd }
    279       1.1       cgd #define	FLUSH() { \
    280       1.1       cgd 	if (uio.uio_resid && __sprint(fp, &uio)) \
    281       1.1       cgd 		goto error; \
    282       1.1       cgd 	uio.uio_iovcnt = 0; \
    283       1.1       cgd 	iovp = iov; \
    284       1.1       cgd }
    285       1.1       cgd 
    286       1.1       cgd 	/*
    287       1.1       cgd 	 * To extend shorts properly, we need both signed and unsigned
    288       1.1       cgd 	 * argument extraction methods.
    289       1.1       cgd 	 */
    290       1.1       cgd #define	SARG() \
    291  1.35.2.1   nathanw 	(flags&MAXINT ? va_arg(ap, intmax_t) : \
    292  1.35.2.1   nathanw 	    flags&PTRINT ? va_arg(ap, ptrdiff_t) : \
    293  1.35.2.1   nathanw 	    flags&SIZEINT ? va_arg(ap, ssize_t) : /* XXX */ \
    294  1.35.2.1   nathanw 	    flags&QUADINT ? va_arg(ap, quad_t) : \
    295       1.4       cgd 	    flags&LONGINT ? va_arg(ap, long) : \
    296       1.1       cgd 	    flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
    297       1.1       cgd 	    (long)va_arg(ap, int))
    298       1.1       cgd #define	UARG() \
    299  1.35.2.1   nathanw 	(flags&MAXINT ? va_arg(ap, uintmax_t) : \
    300  1.35.2.1   nathanw 	    flags&PTRINT ? va_arg(ap, uintptr_t) : /* XXX */ \
    301  1.35.2.1   nathanw 	    flags&SIZEINT ? va_arg(ap, size_t) : \
    302  1.35.2.1   nathanw 	    flags&QUADINT ? va_arg(ap, u_quad_t) : \
    303       1.4       cgd 	    flags&LONGINT ? va_arg(ap, u_long) : \
    304       1.1       cgd 	    flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
    305       1.1       cgd 	    (u_long)va_arg(ap, u_int))
    306       1.1       cgd 
    307      1.31     lukem 	_DIAGASSERT(fp != NULL);
    308      1.31     lukem 	_DIAGASSERT(fmt0 != NULL);
    309      1.31     lukem 
    310      1.28    kleink 	FLOCKFILE(fp);
    311      1.28    kleink 
    312      1.22    kleink 	/* sorry, fprintf(read_only_file, "") returns -1, not 0 */
    313      1.19    kleink 	if (cantwrite(fp)) {
    314      1.19    kleink 		errno = EBADF;
    315      1.28    kleink 		FUNLOCKFILE(fp);
    316      1.22    kleink 		return (-1);
    317      1.19    kleink 	}
    318       1.1       cgd 
    319       1.1       cgd 	/* optimise fprintf(stderr) (and other unbuffered Unix files) */
    320       1.1       cgd 	if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
    321      1.28    kleink 	    fp->_file >= 0) {
    322      1.28    kleink 		ret = __sbprintf(fp, fmt0, ap);
    323      1.28    kleink 		FUNLOCKFILE(fp);
    324      1.28    kleink 		return (ret);
    325      1.28    kleink 	}
    326       1.1       cgd 
    327      1.27   mycroft 	fmt = fmt0;
    328       1.1       cgd 	uio.uio_iov = iovp = iov;
    329       1.1       cgd 	uio.uio_resid = 0;
    330       1.1       cgd 	uio.uio_iovcnt = 0;
    331       1.1       cgd 	ret = 0;
    332       1.1       cgd 
    333      1.35    itojun 	memset(&ps, 0, sizeof(ps));
    334      1.34    itojun 
    335       1.1       cgd 	/*
    336       1.1       cgd 	 * Scan the format for conversions (`%' character).
    337       1.1       cgd 	 */
    338       1.1       cgd 	for (;;) {
    339      1.17       jtc 		cp = fmt;
    340      1.34    itojun 		while ((n = mbrtowc(&wc, fmt, MB_CUR_MAX, &ps)) > 0) {
    341      1.17       jtc 			fmt += n;
    342      1.17       jtc 			if (wc == '%') {
    343      1.17       jtc 				fmt--;
    344      1.17       jtc 				break;
    345      1.17       jtc 			}
    346       1.1       cgd 		}
    347      1.17       jtc 		if ((m = fmt - cp) != 0) {
    348      1.17       jtc 			PRINT(cp, m);
    349      1.17       jtc 			ret += m;
    350      1.17       jtc 		}
    351      1.17       jtc 		if (n <= 0)
    352       1.1       cgd 			goto done;
    353       1.1       cgd 		fmt++;		/* skip over '%' */
    354       1.1       cgd 
    355       1.1       cgd 		flags = 0;
    356       1.1       cgd 		dprec = 0;
    357       1.1       cgd 		width = 0;
    358       1.1       cgd 		prec = -1;
    359       1.1       cgd 		sign = '\0';
    360       1.1       cgd 
    361       1.1       cgd rflag:		ch = *fmt++;
    362       1.1       cgd reswitch:	switch (ch) {
    363       1.1       cgd 		case ' ':
    364       1.1       cgd 			/*
    365       1.1       cgd 			 * ``If the space and + flags both appear, the space
    366       1.1       cgd 			 * flag will be ignored.''
    367       1.1       cgd 			 *	-- ANSI X3J11
    368       1.1       cgd 			 */
    369       1.1       cgd 			if (!sign)
    370       1.1       cgd 				sign = ' ';
    371       1.1       cgd 			goto rflag;
    372       1.1       cgd 		case '#':
    373       1.1       cgd 			flags |= ALT;
    374       1.1       cgd 			goto rflag;
    375       1.1       cgd 		case '*':
    376       1.1       cgd 			/*
    377       1.1       cgd 			 * ``A negative field width argument is taken as a
    378       1.1       cgd 			 * - flag followed by a positive field width.''
    379       1.1       cgd 			 *	-- ANSI X3J11
    380       1.1       cgd 			 * They don't exclude field widths read from args.
    381       1.1       cgd 			 */
    382       1.1       cgd 			if ((width = va_arg(ap, int)) >= 0)
    383       1.1       cgd 				goto rflag;
    384       1.1       cgd 			width = -width;
    385       1.1       cgd 			/* FALLTHROUGH */
    386       1.1       cgd 		case '-':
    387       1.1       cgd 			flags |= LADJUST;
    388       1.1       cgd 			goto rflag;
    389       1.1       cgd 		case '+':
    390       1.1       cgd 			sign = '+';
    391       1.1       cgd 			goto rflag;
    392       1.1       cgd 		case '.':
    393       1.1       cgd 			if ((ch = *fmt++) == '*') {
    394       1.1       cgd 				n = va_arg(ap, int);
    395       1.1       cgd 				prec = n < 0 ? -1 : n;
    396       1.1       cgd 				goto rflag;
    397       1.1       cgd 			}
    398       1.1       cgd 			n = 0;
    399       1.1       cgd 			while (is_digit(ch)) {
    400       1.1       cgd 				n = 10 * n + to_digit(ch);
    401       1.1       cgd 				ch = *fmt++;
    402       1.1       cgd 			}
    403       1.1       cgd 			prec = n < 0 ? -1 : n;
    404       1.1       cgd 			goto reswitch;
    405       1.1       cgd 		case '0':
    406       1.1       cgd 			/*
    407       1.1       cgd 			 * ``Note that 0 is taken as a flag, not as the
    408       1.1       cgd 			 * beginning of a field width.''
    409       1.1       cgd 			 *	-- ANSI X3J11
    410       1.1       cgd 			 */
    411       1.1       cgd 			flags |= ZEROPAD;
    412       1.1       cgd 			goto rflag;
    413       1.1       cgd 		case '1': case '2': case '3': case '4':
    414       1.1       cgd 		case '5': case '6': case '7': case '8': case '9':
    415       1.1       cgd 			n = 0;
    416       1.1       cgd 			do {
    417       1.1       cgd 				n = 10 * n + to_digit(ch);
    418       1.1       cgd 				ch = *fmt++;
    419       1.1       cgd 			} while (is_digit(ch));
    420       1.1       cgd 			width = n;
    421       1.1       cgd 			goto reswitch;
    422       1.1       cgd #ifdef FLOATING_POINT
    423       1.1       cgd 		case 'L':
    424       1.1       cgd 			flags |= LONGDBL;
    425       1.1       cgd 			goto rflag;
    426       1.1       cgd #endif
    427       1.1       cgd 		case 'h':
    428       1.1       cgd 			flags |= SHORTINT;
    429       1.1       cgd 			goto rflag;
    430  1.35.2.1   nathanw 		case 'j':
    431  1.35.2.1   nathanw 			flags |= MAXINT;
    432  1.35.2.1   nathanw 			goto rflag;
    433       1.1       cgd 		case 'l':
    434      1.16       jtc 			if (*fmt == 'l') {
    435      1.16       jtc 				fmt++;
    436      1.16       jtc 				flags |= QUADINT;
    437      1.16       jtc 			} else {
    438      1.16       jtc 				flags |= LONGINT;
    439      1.16       jtc 			}
    440       1.1       cgd 			goto rflag;
    441       1.4       cgd 		case 'q':
    442       1.4       cgd 			flags |= QUADINT;
    443       1.4       cgd 			goto rflag;
    444  1.35.2.1   nathanw 		case 't':
    445  1.35.2.1   nathanw 			flags |= PTRINT;
    446  1.35.2.1   nathanw 			goto rflag;
    447  1.35.2.1   nathanw 		case 'z':
    448  1.35.2.1   nathanw 			flags |= SIZEINT;
    449  1.35.2.1   nathanw 			goto rflag;
    450       1.1       cgd 		case 'c':
    451      1.27   mycroft 			*buf = va_arg(ap, int);
    452      1.27   mycroft 			cp = buf;
    453       1.1       cgd 			size = 1;
    454       1.1       cgd 			sign = '\0';
    455       1.1       cgd 			break;
    456       1.1       cgd 		case 'D':
    457       1.1       cgd 			flags |= LONGINT;
    458       1.1       cgd 			/*FALLTHROUGH*/
    459       1.1       cgd 		case 'd':
    460       1.1       cgd 		case 'i':
    461  1.35.2.1   nathanw 			_uintmax = SARG();
    462  1.35.2.1   nathanw 			if ((intmax_t)_uintmax < 0) {
    463  1.35.2.1   nathanw 				_uintmax = -_uintmax;
    464       1.1       cgd 				sign = '-';
    465       1.1       cgd 			}
    466       1.1       cgd 			base = DEC;
    467       1.1       cgd 			goto number;
    468       1.1       cgd #ifdef FLOATING_POINT
    469       1.9       jtc 		case 'e':
    470       1.1       cgd 		case 'E':
    471       1.9       jtc 		case 'f':
    472       1.1       cgd 		case 'g':
    473       1.1       cgd 		case 'G':
    474       1.9       jtc 			if (prec == -1) {
    475       1.4       cgd 				prec = DEFPREC;
    476       1.9       jtc 			} else if ((ch == 'g' || ch == 'G') && prec == 0) {
    477       1.9       jtc 				prec = 1;
    478       1.9       jtc 			}
    479       1.9       jtc 
    480       1.9       jtc 			if (flags & LONGDBL) {
    481       1.9       jtc 				_double = (double) va_arg(ap, long double);
    482       1.9       jtc 			} else {
    483       1.9       jtc 				_double = va_arg(ap, double);
    484       1.9       jtc 			}
    485       1.9       jtc 
    486       1.1       cgd 			/* do this before tricky precision changes */
    487       1.1       cgd 			if (isinf(_double)) {
    488       1.1       cgd 				if (_double < 0)
    489       1.1       cgd 					sign = '-';
    490       1.1       cgd 				cp = "Inf";
    491       1.1       cgd 				size = 3;
    492       1.1       cgd 				break;
    493       1.1       cgd 			}
    494       1.1       cgd 			if (isnan(_double)) {
    495       1.1       cgd 				cp = "NaN";
    496       1.1       cgd 				size = 3;
    497       1.1       cgd 				break;
    498       1.1       cgd 			}
    499       1.9       jtc 
    500       1.4       cgd 			flags |= FPT;
    501       1.4       cgd 			cp = cvt(_double, prec, flags, &softsign,
    502       1.4       cgd 				&expt, ch, &ndig);
    503       1.4       cgd 			if (ch == 'g' || ch == 'G') {
    504       1.4       cgd 				if (expt <= -4 || expt > prec)
    505       1.4       cgd 					ch = (ch == 'g') ? 'e' : 'E';
    506       1.4       cgd 				else
    507       1.4       cgd 					ch = 'g';
    508       1.4       cgd 			}
    509       1.4       cgd 			if (ch <= 'e') {	/* 'e' or 'E' fmt */
    510       1.4       cgd 				--expt;
    511       1.4       cgd 				expsize = exponent(expstr, expt, ch);
    512       1.4       cgd 				size = expsize + ndig;
    513       1.4       cgd 				if (ndig > 1 || flags & ALT)
    514       1.4       cgd 					++size;
    515       1.4       cgd 			} else if (ch == 'f') {		/* f fmt */
    516       1.4       cgd 				if (expt > 0) {
    517       1.4       cgd 					size = expt;
    518       1.4       cgd 					if (prec || flags & ALT)
    519       1.4       cgd 						size += prec + 1;
    520       1.4       cgd 				} else	/* "0.X" */
    521       1.4       cgd 					size = prec + 2;
    522       1.4       cgd 			} else if (expt >= ndig) {	/* fixed g fmt */
    523       1.4       cgd 				size = expt;
    524       1.4       cgd 				if (flags & ALT)
    525       1.4       cgd 					++size;
    526       1.4       cgd 			} else
    527       1.4       cgd 				size = ndig + (expt > 0 ?
    528       1.4       cgd 					1 : 2 - expt);
    529       1.4       cgd 
    530       1.1       cgd 			if (softsign)
    531       1.1       cgd 				sign = '-';
    532       1.1       cgd 			break;
    533       1.1       cgd #endif /* FLOATING_POINT */
    534       1.1       cgd 		case 'n':
    535  1.35.2.1   nathanw 			if (flags & MAXINT)
    536  1.35.2.1   nathanw 				*va_arg(ap, intmax_t *) = ret;
    537  1.35.2.1   nathanw 			else if (flags & PTRINT)
    538  1.35.2.1   nathanw 				*va_arg(ap, ptrdiff_t *) = ret;
    539  1.35.2.1   nathanw 			else if (flags & SIZEINT)
    540  1.35.2.1   nathanw 				*va_arg(ap, ssize_t *) = ret;	/* XXX */
    541  1.35.2.1   nathanw 			else if (flags & QUADINT)
    542       1.4       cgd 				*va_arg(ap, quad_t *) = ret;
    543       1.4       cgd 			else if (flags & LONGINT)
    544       1.1       cgd 				*va_arg(ap, long *) = ret;
    545       1.1       cgd 			else if (flags & SHORTINT)
    546       1.1       cgd 				*va_arg(ap, short *) = ret;
    547       1.1       cgd 			else
    548       1.1       cgd 				*va_arg(ap, int *) = ret;
    549       1.1       cgd 			continue;	/* no output */
    550       1.1       cgd 		case 'O':
    551       1.1       cgd 			flags |= LONGINT;
    552       1.1       cgd 			/*FALLTHROUGH*/
    553       1.1       cgd 		case 'o':
    554  1.35.2.1   nathanw 			_uintmax = UARG();
    555       1.1       cgd 			base = OCT;
    556       1.1       cgd 			goto nosign;
    557       1.1       cgd 		case 'p':
    558       1.1       cgd 			/*
    559       1.1       cgd 			 * ``The argument shall be a pointer to void.  The
    560       1.1       cgd 			 * value of the pointer is converted to a sequence
    561       1.1       cgd 			 * of printable characters, in an implementation-
    562       1.1       cgd 			 * defined manner.''
    563       1.1       cgd 			 *	-- ANSI X3J11
    564       1.1       cgd 			 */
    565       1.1       cgd 			/* NOSTRICT */
    566  1.35.2.1   nathanw 			_uintmax = (u_long)va_arg(ap, void *);
    567       1.1       cgd 			base = HEX;
    568       1.1       cgd 			xdigs = "0123456789abcdef";
    569       1.1       cgd 			flags |= HEXPREFIX;
    570       1.1       cgd 			ch = 'x';
    571       1.1       cgd 			goto nosign;
    572       1.1       cgd 		case 's':
    573       1.1       cgd 			if ((cp = va_arg(ap, char *)) == NULL)
    574       1.1       cgd 				cp = "(null)";
    575       1.1       cgd 			if (prec >= 0) {
    576       1.1       cgd 				/*
    577       1.1       cgd 				 * can't use strlen; can only look for the
    578       1.1       cgd 				 * NUL in the first `prec' characters, and
    579       1.1       cgd 				 * strlen() will go further.
    580       1.1       cgd 				 */
    581      1.30  christos 				char *p = memchr(cp, 0, (size_t)prec);
    582       1.1       cgd 
    583       1.1       cgd 				if (p != NULL) {
    584       1.1       cgd 					size = p - cp;
    585       1.1       cgd 					if (size > prec)
    586       1.1       cgd 						size = prec;
    587       1.1       cgd 				} else
    588       1.1       cgd 					size = prec;
    589       1.1       cgd 			} else
    590       1.1       cgd 				size = strlen(cp);
    591       1.1       cgd 			sign = '\0';
    592       1.1       cgd 			break;
    593       1.1       cgd 		case 'U':
    594       1.1       cgd 			flags |= LONGINT;
    595       1.1       cgd 			/*FALLTHROUGH*/
    596       1.1       cgd 		case 'u':
    597  1.35.2.1   nathanw 			_uintmax = UARG();
    598       1.1       cgd 			base = DEC;
    599       1.1       cgd 			goto nosign;
    600       1.1       cgd 		case 'X':
    601       1.1       cgd 			xdigs = "0123456789ABCDEF";
    602       1.1       cgd 			goto hex;
    603       1.1       cgd 		case 'x':
    604       1.1       cgd 			xdigs = "0123456789abcdef";
    605  1.35.2.1   nathanw hex:			_uintmax = UARG();
    606       1.1       cgd 			base = HEX;
    607       1.1       cgd 			/* leading 0x/X only if non-zero */
    608  1.35.2.1   nathanw 			if (flags & ALT && _uintmax != 0)
    609       1.1       cgd 				flags |= HEXPREFIX;
    610       1.1       cgd 
    611       1.1       cgd 			/* unsigned conversions */
    612       1.1       cgd nosign:			sign = '\0';
    613       1.1       cgd 			/*
    614       1.1       cgd 			 * ``... diouXx conversions ... if a precision is
    615       1.1       cgd 			 * specified, the 0 flag will be ignored.''
    616       1.1       cgd 			 *	-- ANSI X3J11
    617       1.1       cgd 			 */
    618       1.1       cgd number:			if ((dprec = prec) >= 0)
    619       1.1       cgd 				flags &= ~ZEROPAD;
    620       1.1       cgd 
    621       1.1       cgd 			/*
    622       1.1       cgd 			 * ``The result of converting a zero value with an
    623       1.1       cgd 			 * explicit precision of zero is no characters.''
    624       1.1       cgd 			 *	-- ANSI X3J11
    625       1.1       cgd 			 */
    626      1.27   mycroft 			bp = buf + BUF;
    627  1.35.2.1   nathanw 			if (_uintmax != 0 || prec != 0) {
    628       1.1       cgd 				/*
    629       1.4       cgd 				 * Unsigned mod is hard, and unsigned mod
    630       1.1       cgd 				 * by a constant is easier than that by
    631       1.1       cgd 				 * a variable; hence this switch.
    632       1.1       cgd 				 */
    633       1.1       cgd 				switch (base) {
    634       1.1       cgd 				case OCT:
    635       1.1       cgd 					do {
    636  1.35.2.1   nathanw 						*--bp = to_char(_uintmax & 7);
    637  1.35.2.1   nathanw 						_uintmax >>= 3;
    638  1.35.2.1   nathanw 					} while (_uintmax);
    639       1.1       cgd 					/* handle octal leading 0 */
    640      1.27   mycroft 					if (flags & ALT && *bp != '0')
    641      1.27   mycroft 						*--bp = '0';
    642       1.1       cgd 					break;
    643       1.1       cgd 
    644       1.1       cgd 				case DEC:
    645       1.1       cgd 					/* many numbers are 1 digit */
    646  1.35.2.1   nathanw 					while (_uintmax >= 10) {
    647  1.35.2.1   nathanw 						*--bp = to_char(_uintmax % 10);
    648  1.35.2.1   nathanw 						_uintmax /= 10;
    649       1.1       cgd 					}
    650  1.35.2.1   nathanw 					*--bp = to_char(_uintmax);
    651       1.1       cgd 					break;
    652       1.1       cgd 
    653       1.1       cgd 				case HEX:
    654       1.1       cgd 					do {
    655      1.30  christos 						*--bp = xdigs[(size_t)
    656  1.35.2.1   nathanw 						    (_uintmax & 15)];
    657  1.35.2.1   nathanw 						_uintmax >>= 4;
    658  1.35.2.1   nathanw 					} while (_uintmax);
    659       1.1       cgd 					break;
    660       1.1       cgd 
    661       1.1       cgd 				default:
    662       1.1       cgd 					cp = "bug in vfprintf: bad base";
    663       1.1       cgd 					size = strlen(cp);
    664       1.1       cgd 					goto skipsize;
    665       1.1       cgd 				}
    666       1.1       cgd 			}
    667      1.27   mycroft 			cp = bp;
    668      1.27   mycroft 			size = buf + BUF - bp;
    669       1.1       cgd 		skipsize:
    670       1.1       cgd 			break;
    671       1.1       cgd 		default:	/* "%?" prints ?, unless ? is NUL */
    672       1.1       cgd 			if (ch == '\0')
    673       1.1       cgd 				goto done;
    674       1.1       cgd 			/* pretend it was %c with argument ch */
    675      1.27   mycroft 			*buf = ch;
    676       1.1       cgd 			cp = buf;
    677       1.1       cgd 			size = 1;
    678       1.1       cgd 			sign = '\0';
    679       1.1       cgd 			break;
    680       1.1       cgd 		}
    681       1.1       cgd 
    682       1.1       cgd 		/*
    683       1.4       cgd 		 * All reasonable formats wind up here.  At this point, `cp'
    684       1.4       cgd 		 * points to a string which (if not flags&LADJUST) should be
    685       1.4       cgd 		 * padded out to `width' places.  If flags&ZEROPAD, it should
    686       1.4       cgd 		 * first be prefixed by any sign or other prefix; otherwise,
    687       1.4       cgd 		 * it should be blank padded before the prefix is emitted.
    688       1.4       cgd 		 * After any left-hand padding and prefixing, emit zeroes
    689       1.4       cgd 		 * required by a decimal [diouxX] precision, then print the
    690       1.4       cgd 		 * string proper, then emit zeroes required by any leftover
    691       1.4       cgd 		 * floating precision; finally, if LADJUST, pad with blanks.
    692       1.4       cgd 		 *
    693       1.4       cgd 		 * Compute actual size, so we know how much to pad.
    694      1.14       jtc 		 * size excludes decimal prec; realsz includes it.
    695       1.1       cgd 		 */
    696      1.14       jtc 		realsz = dprec > size ? dprec : size;
    697       1.1       cgd 		if (sign)
    698      1.14       jtc 			realsz++;
    699       1.1       cgd 		else if (flags & HEXPREFIX)
    700      1.14       jtc 			realsz+= 2;
    701       1.1       cgd 
    702       1.1       cgd 		/* right-adjusting blank padding */
    703       1.1       cgd 		if ((flags & (LADJUST|ZEROPAD)) == 0)
    704       1.1       cgd 			PAD(width - realsz, blanks);
    705       1.1       cgd 
    706       1.1       cgd 		/* prefix */
    707       1.1       cgd 		if (sign) {
    708       1.1       cgd 			PRINT(&sign, 1);
    709       1.1       cgd 		} else if (flags & HEXPREFIX) {
    710       1.1       cgd 			ox[0] = '0';
    711       1.1       cgd 			ox[1] = ch;
    712       1.1       cgd 			PRINT(ox, 2);
    713       1.1       cgd 		}
    714       1.1       cgd 
    715       1.1       cgd 		/* right-adjusting zero padding */
    716       1.1       cgd 		if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
    717       1.1       cgd 			PAD(width - realsz, zeroes);
    718       1.1       cgd 
    719       1.1       cgd 		/* leading zeroes from decimal precision */
    720      1.13       jtc 		PAD(dprec - size, zeroes);
    721       1.1       cgd 
    722       1.1       cgd 		/* the string or number proper */
    723       1.4       cgd #ifdef FLOATING_POINT
    724       1.4       cgd 		if ((flags & FPT) == 0) {
    725       1.4       cgd 			PRINT(cp, size);
    726       1.4       cgd 		} else {	/* glue together f_p fragments */
    727       1.4       cgd 			if (ch >= 'f') {	/* 'f' or 'g' */
    728       1.4       cgd 				if (_double == 0) {
    729       1.9       jtc 					/* kludge for __dtoa irregularity */
    730      1.12       jtc 					PRINT("0", 1);
    731      1.12       jtc 					if (expt < ndig || (flags & ALT) != 0) {
    732      1.12       jtc 						PRINT(decimal_point, 1);
    733       1.4       cgd 						PAD(ndig - 1, zeroes);
    734       1.4       cgd 					}
    735       1.4       cgd 				} else if (expt <= 0) {
    736      1.12       jtc 					PRINT("0", 1);
    737      1.12       jtc 					PRINT(decimal_point, 1);
    738       1.4       cgd 					PAD(-expt, zeroes);
    739       1.4       cgd 					PRINT(cp, ndig);
    740       1.4       cgd 				} else if (expt >= ndig) {
    741       1.4       cgd 					PRINT(cp, ndig);
    742       1.4       cgd 					PAD(expt - ndig, zeroes);
    743       1.4       cgd 					if (flags & ALT)
    744       1.4       cgd 						PRINT(".", 1);
    745       1.4       cgd 				} else {
    746       1.4       cgd 					PRINT(cp, expt);
    747       1.4       cgd 					cp += expt;
    748       1.4       cgd 					PRINT(".", 1);
    749       1.4       cgd 					PRINT(cp, ndig-expt);
    750       1.4       cgd 				}
    751       1.4       cgd 			} else {	/* 'e' or 'E' */
    752       1.4       cgd 				if (ndig > 1 || flags & ALT) {
    753       1.4       cgd 					ox[0] = *cp++;
    754       1.4       cgd 					ox[1] = '.';
    755       1.4       cgd 					PRINT(ox, 2);
    756      1.21      phil 					if (_double) {
    757       1.4       cgd 						PRINT(cp, ndig-1);
    758       1.4       cgd 					} else	/* 0.[0..] */
    759       1.4       cgd 						/* __dtoa irregularity */
    760       1.4       cgd 						PAD(ndig - 1, zeroes);
    761       1.4       cgd 				} else	/* XeYYY */
    762       1.4       cgd 					PRINT(cp, 1);
    763       1.4       cgd 				PRINT(expstr, expsize);
    764       1.4       cgd 			}
    765       1.4       cgd 		}
    766       1.4       cgd #else
    767       1.1       cgd 		PRINT(cp, size);
    768       1.1       cgd #endif
    769       1.1       cgd 		/* left-adjusting padding (always blank) */
    770       1.1       cgd 		if (flags & LADJUST)
    771       1.1       cgd 			PAD(width - realsz, blanks);
    772       1.1       cgd 
    773       1.1       cgd 		/* finally, adjust ret */
    774       1.1       cgd 		ret += width > realsz ? width : realsz;
    775       1.1       cgd 
    776       1.1       cgd 		FLUSH();	/* copy out the I/O vectors */
    777       1.1       cgd 	}
    778       1.1       cgd done:
    779       1.1       cgd 	FLUSH();
    780       1.1       cgd error:
    781      1.28    kleink 	if (__sferror(fp))
    782      1.28    kleink 		ret = -1;
    783      1.28    kleink 	FUNLOCKFILE(fp);
    784      1.28    kleink 	return (ret);
    785       1.1       cgd }
    786       1.1       cgd 
    787       1.1       cgd #ifdef FLOATING_POINT
    788       1.1       cgd 
    789       1.4       cgd static char *
    790       1.4       cgd cvt(value, ndigits, flags, sign, decpt, ch, length)
    791       1.4       cgd 	double value;
    792       1.4       cgd 	int ndigits, flags, *decpt, ch, *length;
    793       1.4       cgd 	char *sign;
    794       1.1       cgd {
    795       1.4       cgd 	int mode, dsgn;
    796       1.4       cgd 	char *digits, *bp, *rve;
    797       1.1       cgd 
    798      1.31     lukem 	_DIAGASSERT(decpt != NULL);
    799      1.31     lukem 	_DIAGASSERT(length != NULL);
    800      1.31     lukem 	_DIAGASSERT(sign != NULL);
    801      1.31     lukem 
    802       1.9       jtc 	if (ch == 'f') {
    803       1.9       jtc 		mode = 3;		/* ndigits after the decimal point */
    804       1.9       jtc 	} else {
    805       1.9       jtc 		/* To obtain ndigits after the decimal point for the 'e'
    806       1.9       jtc 		 * and 'E' formats, round to ndigits + 1 significant
    807       1.9       jtc 		 * figures.
    808       1.9       jtc 		 */
    809       1.9       jtc 		if (ch == 'e' || ch == 'E') {
    810       1.9       jtc 			ndigits++;
    811       1.9       jtc 		}
    812       1.9       jtc 		mode = 2;		/* ndigits significant digits */
    813       1.1       cgd 	}
    814       1.9       jtc 
    815       1.4       cgd 	if (value < 0) {
    816       1.4       cgd 		value = -value;
    817       1.4       cgd 		*sign = '-';
    818       1.4       cgd 	} else
    819       1.4       cgd 		*sign = '\000';
    820       1.4       cgd 	digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve);
    821       1.9       jtc 	if ((ch != 'g' && ch != 'G') || flags & ALT) {	/* Print trailing zeros */
    822       1.4       cgd 		bp = digits + ndigits;
    823       1.4       cgd 		if (ch == 'f') {
    824       1.4       cgd 			if (*digits == '0' && value)
    825       1.4       cgd 				*decpt = -ndigits + 1;
    826       1.4       cgd 			bp += *decpt;
    827       1.4       cgd 		}
    828       1.4       cgd 		if (value == 0)	/* kludge for __dtoa irregularity */
    829       1.4       cgd 			rve = bp;
    830       1.4       cgd 		while (rve < bp)
    831       1.4       cgd 			*rve++ = '0';
    832       1.1       cgd 	}
    833       1.4       cgd 	*length = rve - digits;
    834       1.4       cgd 	return (digits);
    835       1.1       cgd }
    836       1.1       cgd 
    837       1.4       cgd static int
    838  1.35.2.2   nathanw exponent(p0, expon, fmtch)
    839       1.4       cgd 	char *p0;
    840  1.35.2.2   nathanw 	int expon, fmtch;
    841       1.1       cgd {
    842      1.23     perry 	char *p, *t;
    843       1.1       cgd 	char expbuf[MAXEXP];
    844      1.31     lukem 
    845      1.31     lukem 	_DIAGASSERT(p0 != NULL);
    846       1.1       cgd 
    847       1.4       cgd 	p = p0;
    848       1.1       cgd 	*p++ = fmtch;
    849  1.35.2.2   nathanw 	if (expon < 0) {
    850  1.35.2.2   nathanw 		expon = -expon;
    851       1.1       cgd 		*p++ = '-';
    852       1.1       cgd 	}
    853       1.1       cgd 	else
    854       1.1       cgd 		*p++ = '+';
    855       1.1       cgd 	t = expbuf + MAXEXP;
    856  1.35.2.2   nathanw 	if (expon > 9) {
    857       1.1       cgd 		do {
    858  1.35.2.2   nathanw 			*--t = to_char(expon % 10);
    859  1.35.2.2   nathanw 		} while ((expon /= 10) > 9);
    860  1.35.2.2   nathanw 		*--t = to_char(expon);
    861       1.1       cgd 		for (; t < expbuf + MAXEXP; *p++ = *t++);
    862       1.1       cgd 	}
    863       1.1       cgd 	else {
    864       1.1       cgd 		*p++ = '0';
    865  1.35.2.2   nathanw 		*p++ = to_char(expon);
    866       1.1       cgd 	}
    867       1.4       cgd 	return (p - p0);
    868       1.1       cgd }
    869       1.1       cgd #endif /* FLOATING_POINT */
    870