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