xref: /onnv-gate/usr/src/ucblib/libucb/port/stdio/doprnt.c (revision 1219:f89f56c2d9ac)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*1219Sraf 
230Sstevel@tonic-gate /*
24*1219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
290Sstevel@tonic-gate /*	  All Rights Reserved  	*/
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
330Sstevel@tonic-gate  * The Regents of the University of California
340Sstevel@tonic-gate  * All Rights Reserved
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
370Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
380Sstevel@tonic-gate  * contributors.
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /*LINTLIBRARY*/
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate  *	_doprnt: common code for printf, fprintf, sprintf
470Sstevel@tonic-gate  */
480Sstevel@tonic-gate 
49*1219Sraf #include "../../../lib/common/inc/c_synonyms.h"
500Sstevel@tonic-gate #include <sys/types.h>
510Sstevel@tonic-gate #include "file64.h"
520Sstevel@tonic-gate #include <stdio.h>
530Sstevel@tonic-gate #include <stdlib.h>
540Sstevel@tonic-gate #include <ctype.h>
550Sstevel@tonic-gate #include <stdarg.h>
560Sstevel@tonic-gate #include <values.h>
570Sstevel@tonic-gate #include <nan.h>
580Sstevel@tonic-gate #include <memory.h>
590Sstevel@tonic-gate #include <string.h>
600Sstevel@tonic-gate #include "print.h"	/* parameters & macros for doprnt */
610Sstevel@tonic-gate #include "stdiom.h"
620Sstevel@tonic-gate #include <locale.h>
630Sstevel@tonic-gate #include <stddef.h>
640Sstevel@tonic-gate #include "_locale.h"
650Sstevel@tonic-gate #include "libc.h"
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #define	PUT(p, n)	{ unsigned char *newbufptr; \
680Sstevel@tonic-gate 			if ((newbufptr = bufptr + (n)) > bufferend) { \
690Sstevel@tonic-gate 				_dowrite((p), (n), iop, &bufptr); \
700Sstevel@tonic-gate 			} else { \
710Sstevel@tonic-gate 				(void) memcpy(bufptr, (p), (n)); \
720Sstevel@tonic-gate 				bufptr = newbufptr; \
730Sstevel@tonic-gate 			} \
740Sstevel@tonic-gate 			}
750Sstevel@tonic-gate #define	PAD(s, n)	{ int nn; \
760Sstevel@tonic-gate 			for (nn = (n); nn > 20; nn -= 20) \
770Sstevel@tonic-gate 				_dowrite((s), 20, iop, &bufptr); \
780Sstevel@tonic-gate 			PUT((s), nn); \
790Sstevel@tonic-gate 			}
800Sstevel@tonic-gate 
810Sstevel@tonic-gate #define	SNLEN	5	/* Length of string used when printing a NaN */
820Sstevel@tonic-gate 
830Sstevel@tonic-gate /* bit positions for flags used in doprnt */
840Sstevel@tonic-gate 
850Sstevel@tonic-gate #define	LENGTH	1	/* l */
860Sstevel@tonic-gate #define	FPLUS	2	/* + */
870Sstevel@tonic-gate #define	FMINUS	  4	/* - */
880Sstevel@tonic-gate #define	FBLANK	  8	/* blank */
890Sstevel@tonic-gate #define	FSHARP	 16	/* # */
900Sstevel@tonic-gate #define	PADZERO  32	/* padding zeroes requested via '0' */
910Sstevel@tonic-gate #define	DOTSEEN  64	/* dot appeared in format specification */
920Sstevel@tonic-gate #define	SUFFIX	128	/* a suffix is to appear in the output */
930Sstevel@tonic-gate #define	RZERO	256	/* there will be trailing zeros in output */
940Sstevel@tonic-gate #define	LZERO	512	/* there will be leading zeroes in output */
950Sstevel@tonic-gate #define	SHORT  1024	/* h */
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate  *	Positional Parameter information
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate #define	MAXARGS	30	/* max. number of args for fast positional paramters */
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate  * stva_list is used to subvert C's restriction that a variable with an
1040Sstevel@tonic-gate  * array type can not appear on the left hand side of an assignment operator.
1050Sstevel@tonic-gate  * By putting the array inside a structure, the functionality of assigning to
1060Sstevel@tonic-gate  * the whole array through a simple assignment is achieved..
1070Sstevel@tonic-gate  */
1080Sstevel@tonic-gate typedef struct stva_list {
1090Sstevel@tonic-gate 	va_list	ap;
1100Sstevel@tonic-gate } stva_list;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate static char _blanks[] = "                    ";
1130Sstevel@tonic-gate static char _zeroes[] = "00000000000000000000";
1140Sstevel@tonic-gate static char uc_digs[] = "0123456789ABCDEF";
1150Sstevel@tonic-gate static char lc_digs[] = "0123456789abcdef";
1160Sstevel@tonic-gate static char  lc_nan[] = "nan0x";
1170Sstevel@tonic-gate static char  uc_nan[] = "NAN0X";
1180Sstevel@tonic-gate static char  lc_inf[] = "inf";
1190Sstevel@tonic-gate static char  uc_inf[] = "INF";
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate /*
1220Sstevel@tonic-gate  * forward declarations
1230Sstevel@tonic-gate  */
1240Sstevel@tonic-gate void _mkarglst(char *, stva_list, stva_list []);
1250Sstevel@tonic-gate void _getarg(char *, stva_list *, int);
1260Sstevel@tonic-gate static int _lowdigit(long *);
1270Sstevel@tonic-gate static void _dowrite(char *, ssize_t, FILE *, unsigned char **);
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate static int
1300Sstevel@tonic-gate _lowdigit(long *valptr)
1310Sstevel@tonic-gate {	/* This function computes the decimal low-order digit of the number */
1320Sstevel@tonic-gate 	/* pointed to by valptr, and returns this digit after dividing   */
1330Sstevel@tonic-gate 	/* *valptr by ten.  This function is called ONLY to compute the */
1340Sstevel@tonic-gate 	/* low-order digit of a long whose high-order bit is set. */
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	int lowbit = (int)(*valptr & 1);
1370Sstevel@tonic-gate 	long value = (*valptr >> 1) & ~HIBITL;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	*valptr = value / 5;
1400Sstevel@tonic-gate 	return ((int)(value % 5 * 2 + lowbit + '0'));
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate /* The function _dowrite carries out buffer pointer bookkeeping surrounding */
1440Sstevel@tonic-gate /* a call to fwrite.  It is called only when the end of the file output */
1450Sstevel@tonic-gate /* buffer is approached or in other unusual situations. */
1460Sstevel@tonic-gate static void
1470Sstevel@tonic-gate _dowrite(char *p, ssize_t n, FILE *iop, unsigned char **ptrptr)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate 	if (!(iop->_flag & _IOREAD)) {
1500Sstevel@tonic-gate 		iop->_cnt -= (*ptrptr - iop->_ptr);
1510Sstevel@tonic-gate 		iop->_ptr = *ptrptr;
1520Sstevel@tonic-gate 		_bufsync(iop, _bufend(iop));
1530Sstevel@tonic-gate 		(void) fwrite(p, 1, n, iop);
1540Sstevel@tonic-gate 		*ptrptr = iop->_ptr;
1550Sstevel@tonic-gate 	} else
1560Sstevel@tonic-gate 		*ptrptr = (unsigned char *) memcpy(*ptrptr, p, n) + n;
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate int
1600Sstevel@tonic-gate _doprnt(char *format, va_list in_args, FILE *iop)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	/* bufptr is used inside of doprnt instead of iop->_ptr; */
1640Sstevel@tonic-gate 	/* bufferend is a copy of _bufend(iop), if it exists.  For */
1650Sstevel@tonic-gate 	/* dummy file descriptors (iop->_flag & _IOREAD), bufferend */
1660Sstevel@tonic-gate 	/* may be meaningless. Dummy file descriptors are used so that */
1670Sstevel@tonic-gate 	/* sprintf and vsprintf may share the _doprnt routine with the */
1680Sstevel@tonic-gate 	/* rest of the printf family. */
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	unsigned char *bufptr;
1710Sstevel@tonic-gate 	unsigned char *bufferend;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	/* This variable counts output characters. */
1740Sstevel@tonic-gate 	int	count = 0;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	/* Starting and ending points for value to be printed */
1770Sstevel@tonic-gate 	char	*bp;
1780Sstevel@tonic-gate 	char	*p;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	/* Field width and precision */
1810Sstevel@tonic-gate 	int	width, prec;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	/* Format code */
1840Sstevel@tonic-gate 	int	fcode;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	/* Number of padding zeroes required on the left and right */
1870Sstevel@tonic-gate 	int	lzero, rzero;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	/* Flags - bit positions defined by LENGTH, FPLUS, FMINUS, FBLANK, */
1900Sstevel@tonic-gate 	/* and FSHARP are set if corresponding character is in format */
1910Sstevel@tonic-gate 	/* Bit position defined by PADZERO means extra space in the field */
1920Sstevel@tonic-gate 	/* should be padded with leading zeroes rather than with blanks */
1930Sstevel@tonic-gate 	int	flagword;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	/* Values are developed in this buffer */
1960Sstevel@tonic-gate 	char	buf[max(MAXDIGS, 1+max(MAXFCVT+MAXEXP, MAXECVT))];
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	/* Pointer to sign, "0x", "0X", or empty */
1990Sstevel@tonic-gate 	char	*prefix;
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	/* Exponent or empty */
2020Sstevel@tonic-gate 	char	*suffix;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	/* Buffer to create exponent */
2050Sstevel@tonic-gate 	char	expbuf[MAXESIZ + 1];
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	/* Length of prefix and of suffix */
2080Sstevel@tonic-gate 	int	prefixlength, suffixlength;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	/* Combined length of leading zeroes, trailing zeroes, and suffix */
2110Sstevel@tonic-gate 	int 	otherlength;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	/* The value being converted, if integer */
2140Sstevel@tonic-gate 	long	val;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	/* The value being converted, if real */
2170Sstevel@tonic-gate 	double	dval;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	/* Output values from fcvt and ecvt */
2200Sstevel@tonic-gate 	int	decpt, sign;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	/* Pointer to a translate table for digits of whatever radix */
2230Sstevel@tonic-gate 	char	*tab;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	/* Work variables */
2260Sstevel@tonic-gate 	int	k, lradix, mradix;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	/* Variables used to flag an infinities and nans, resp. */
2290Sstevel@tonic-gate 	/* Nan_flg is used with two purposes: to flag a NaN and */
2300Sstevel@tonic-gate 	/* as the length of the string ``NAN0X'' (``nan0x'') */
2310Sstevel@tonic-gate 	int	 inf_nan = 0, NaN_flg = 0;
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	/* Pointer to string "NAN0X" or "nan0x" */
2340Sstevel@tonic-gate 	char	 *SNAN;
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	/* Flag for negative infinity or NaN */
2370Sstevel@tonic-gate 	int neg_in = 0;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	/* variables for positional parameters */
2400Sstevel@tonic-gate 	char	*sformat = format;	/* save the beginning of the format */
2410Sstevel@tonic-gate 	int	fpos = 1;		/* 1 if first positional parameter */
2420Sstevel@tonic-gate 	stva_list args;		/* used to step through the argument list */
2430Sstevel@tonic-gate 	stva_list sargs;
2440Sstevel@tonic-gate 		/* used to save the start of the argument list */
2450Sstevel@tonic-gate 	stva_list bargs;
2460Sstevel@tonic-gate 		/* used to restore args if positional width or precision */
2470Sstevel@tonic-gate 	stva_list arglst[MAXARGS];
2480Sstevel@tonic-gate 		/*
2490Sstevel@tonic-gate 		 * array giving the appropriate values for va_arg() to
2500Sstevel@tonic-gate 		 * retrieve the corresponding argument:
2510Sstevel@tonic-gate 		 * arglst[0] is the first argument,
2520Sstevel@tonic-gate 		 * arglst[1] is the second argument, etc.
2530Sstevel@tonic-gate 		 */
2540Sstevel@tonic-gate 	int	starflg = 0;	/* set to 1 if * format specifier seen */
2550Sstevel@tonic-gate 	/*
2560Sstevel@tonic-gate 	 * Initialize args and sargs to the start of the argument list.
2570Sstevel@tonic-gate 	 * Note that ANSI guarantees that the address of the first member of
2580Sstevel@tonic-gate 	 * a structure will be the same as the address of the structure.
2590Sstevel@tonic-gate 	 * See equivalent code in libc doprnt.c
2600Sstevel@tonic-gate 	 */
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate #if !(defined(__amd64) && defined(__GNUC__))	/* XX64 - fix me */
2630Sstevel@tonic-gate 	va_copy(args.ap, in_args);
2640Sstevel@tonic-gate #endif
2650Sstevel@tonic-gate 	sargs = args;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	/* if first I/O to the stream get a buffer */
2680Sstevel@tonic-gate 	/* Note that iop->_base should not equal 0 for sprintf and vsprintf */
2690Sstevel@tonic-gate 	if (iop->_base == 0 && _findbuf(iop) == 0)
2700Sstevel@tonic-gate 		return (EOF);
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	/* initialize buffer pointer and buffer end pointer */
2730Sstevel@tonic-gate 	bufptr = iop->_ptr;
2740Sstevel@tonic-gate 	bufferend = (iop->_flag & _IOREAD) ?
2750Sstevel@tonic-gate 		(unsigned char *)((long)bufptr | (-1L & ~HIBITL))
2760Sstevel@tonic-gate 		: _bufend(iop);
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	/*
2790Sstevel@tonic-gate 	 *	The main loop -- this loop goes through one iteration
2800Sstevel@tonic-gate 	 *	for each string of ordinary characters or format specification.
2810Sstevel@tonic-gate 	 */
2820Sstevel@tonic-gate 	for (;;) {
2830Sstevel@tonic-gate 		ptrdiff_t pdiff;
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 		if ((fcode = *format) != '\0' && fcode != '%') {
2860Sstevel@tonic-gate 			bp = format;
2870Sstevel@tonic-gate 			do {
2880Sstevel@tonic-gate 				format++;
2890Sstevel@tonic-gate 			} while ((fcode = *format) != '\0' && fcode != '%');
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 			pdiff = format - bp;
2920Sstevel@tonic-gate 				/* pdiff = no. of non-% chars */
2930Sstevel@tonic-gate 			count += pdiff;
2940Sstevel@tonic-gate 			PUT(bp, pdiff);
2950Sstevel@tonic-gate 		}
2960Sstevel@tonic-gate 		if (fcode == '\0') {  /* end of format; return */
2970Sstevel@tonic-gate 			ptrdiff_t d = bufptr - iop->_ptr;
2980Sstevel@tonic-gate 			iop->_cnt -= d;
2990Sstevel@tonic-gate 			iop->_ptr = bufptr;
3000Sstevel@tonic-gate 			if (bufptr + iop->_cnt > bufferend &&
3010Sstevel@tonic-gate 			    !(iop->_flag & _IOREAD))
3020Sstevel@tonic-gate 				_bufsync(iop, bufferend);
3030Sstevel@tonic-gate 				/*
3040Sstevel@tonic-gate 				 * in case of interrupt during last
3050Sstevel@tonic-gate 				 * several lines
3060Sstevel@tonic-gate 				 */
3070Sstevel@tonic-gate 			if (iop->_flag & (_IONBF | _IOLBF) &&
3080Sstevel@tonic-gate 			    (iop->_flag & _IONBF ||
3090Sstevel@tonic-gate 			    memchr((char *)(bufptr-count), '\n', count) !=
3100Sstevel@tonic-gate 			    NULL))
3110Sstevel@tonic-gate 				(void) _xflsbuf(iop);
3120Sstevel@tonic-gate 			return (ferror(iop) ? EOF : count);
3130Sstevel@tonic-gate 		}
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 		/*
3160Sstevel@tonic-gate 		 *	% has been found.
3170Sstevel@tonic-gate 		 *	The following switch is used to parse the format
3180Sstevel@tonic-gate 		 *	specification and to perform the operation specified
3190Sstevel@tonic-gate 		 *	by the format letter.  The program repeatedly goes
3200Sstevel@tonic-gate 		 *	back to this switch until the format letter is
3210Sstevel@tonic-gate 		 *	encountered.
3220Sstevel@tonic-gate 		 */
3230Sstevel@tonic-gate 		width = prefixlength = otherlength = flagword =
3240Sstevel@tonic-gate 			suffixlength = 0;
3250Sstevel@tonic-gate 		format++;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	charswitch:
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 		switch (fcode = *format++) {
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 		case '+':
3320Sstevel@tonic-gate 			flagword |= FPLUS;
3330Sstevel@tonic-gate 			goto charswitch;
3340Sstevel@tonic-gate 		case '-':
3350Sstevel@tonic-gate 			flagword |= FMINUS;
3360Sstevel@tonic-gate 			flagword &= ~PADZERO; /* ignore 0 flag */
3370Sstevel@tonic-gate 			goto charswitch;
3380Sstevel@tonic-gate 		case ' ':
3390Sstevel@tonic-gate 			flagword |= FBLANK;
3400Sstevel@tonic-gate 			goto charswitch;
3410Sstevel@tonic-gate 		case '#':
3420Sstevel@tonic-gate 			flagword |= FSHARP;
3430Sstevel@tonic-gate 			goto charswitch;
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 		/* Scan the field width and precision */
3460Sstevel@tonic-gate 		case '.':
3470Sstevel@tonic-gate 			flagword |= DOTSEEN;
3480Sstevel@tonic-gate 			prec = 0;
3490Sstevel@tonic-gate 			goto charswitch;
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 		case '*':
3520Sstevel@tonic-gate 			if (isdigit(*format)) {
3530Sstevel@tonic-gate 				starflg = 1;
3540Sstevel@tonic-gate 				bargs = args;
3550Sstevel@tonic-gate 				goto charswitch;
3560Sstevel@tonic-gate 			}
3570Sstevel@tonic-gate 			if (!(flagword & DOTSEEN)) {
3580Sstevel@tonic-gate 				width = va_arg(args.ap, int);
3590Sstevel@tonic-gate 				if (width < 0) {
3600Sstevel@tonic-gate 					width = -width;
3610Sstevel@tonic-gate 					flagword ^= FMINUS;
3620Sstevel@tonic-gate 				}
3630Sstevel@tonic-gate 			} else {
3640Sstevel@tonic-gate 				prec = va_arg(args.ap, int);
3650Sstevel@tonic-gate 				if (prec < 0)
3660Sstevel@tonic-gate 					prec = 0;
3670Sstevel@tonic-gate 			}
3680Sstevel@tonic-gate 			goto charswitch;
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 		case '$':
3710Sstevel@tonic-gate 			{
3720Sstevel@tonic-gate 			int		position;
3730Sstevel@tonic-gate 			stva_list	targs;
3740Sstevel@tonic-gate 			if (fpos) {
3750Sstevel@tonic-gate 				_mkarglst(sformat, sargs, arglst);
3760Sstevel@tonic-gate 				fpos = 0;
3770Sstevel@tonic-gate 			}
3780Sstevel@tonic-gate 			if (flagword & DOTSEEN) {
3790Sstevel@tonic-gate 				position = prec;
3800Sstevel@tonic-gate 				prec = 0;
3810Sstevel@tonic-gate 			} else {
3820Sstevel@tonic-gate 				position = width;
3830Sstevel@tonic-gate 				width = 0;
3840Sstevel@tonic-gate 			}
3850Sstevel@tonic-gate 			if (position <= 0) {
3860Sstevel@tonic-gate 				/* illegal position */
3870Sstevel@tonic-gate 				format--;
3880Sstevel@tonic-gate 				continue;
3890Sstevel@tonic-gate 			}
3900Sstevel@tonic-gate 			if (position <= MAXARGS) {
3910Sstevel@tonic-gate 				targs = arglst[position - 1];
3920Sstevel@tonic-gate 			} else {
3930Sstevel@tonic-gate 				targs = arglst[MAXARGS - 1];
3940Sstevel@tonic-gate 				_getarg(sformat, &targs, position);
3950Sstevel@tonic-gate 			}
3960Sstevel@tonic-gate 			if (!starflg)
3970Sstevel@tonic-gate 				args = targs;
3980Sstevel@tonic-gate 			else {
3990Sstevel@tonic-gate 				starflg = 0;
4000Sstevel@tonic-gate 				args = bargs;
4010Sstevel@tonic-gate 				if (flagword & DOTSEEN)
4020Sstevel@tonic-gate 					prec = va_arg(targs.ap, int);
4030Sstevel@tonic-gate 				else
4040Sstevel@tonic-gate 					width = va_arg(targs.ap, int);
4050Sstevel@tonic-gate 			}
4060Sstevel@tonic-gate 			goto charswitch;
4070Sstevel@tonic-gate 			}
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 		case '0':	/* obsolescent spec:  leading zero in width */
4100Sstevel@tonic-gate 				/* means pad with leading zeros */
4110Sstevel@tonic-gate 			if (!(flagword & (DOTSEEN | FMINUS)))
4120Sstevel@tonic-gate 				flagword |= PADZERO;
4130Sstevel@tonic-gate 			/* FALLTHROUGH */
4140Sstevel@tonic-gate 		case '1':
4150Sstevel@tonic-gate 		case '2':
4160Sstevel@tonic-gate 		case '3':
4170Sstevel@tonic-gate 		case '4':
4180Sstevel@tonic-gate 		case '5':
4190Sstevel@tonic-gate 		case '6':
4200Sstevel@tonic-gate 		case '7':
4210Sstevel@tonic-gate 		case '8':
4220Sstevel@tonic-gate 		case '9':
4230Sstevel@tonic-gate 			{ 	int num = fcode - '0';
4240Sstevel@tonic-gate 				while (isdigit(fcode = *format)) {
4250Sstevel@tonic-gate 					num = num * 10 + fcode - '0';
4260Sstevel@tonic-gate 					format++;
4270Sstevel@tonic-gate 				}
4280Sstevel@tonic-gate 				if (flagword & DOTSEEN)
4290Sstevel@tonic-gate 					prec = num;
4300Sstevel@tonic-gate 				else
4310Sstevel@tonic-gate 					width = num;
4320Sstevel@tonic-gate 				goto charswitch;
4330Sstevel@tonic-gate 			}
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 		/* Scan the length modifier */
4360Sstevel@tonic-gate 		case 'l':
4370Sstevel@tonic-gate 			flagword |= LENGTH;
4380Sstevel@tonic-gate 			goto charswitch;
4390Sstevel@tonic-gate 		case 'h':
4400Sstevel@tonic-gate 			flagword |= SHORT;
4410Sstevel@tonic-gate 			goto charswitch;
4420Sstevel@tonic-gate 		case 'L':
4430Sstevel@tonic-gate 			goto charswitch;
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 		/*
4460Sstevel@tonic-gate 		 *	The character addressed by format must be
4470Sstevel@tonic-gate 		 *	the format letter -- there is nothing
4480Sstevel@tonic-gate 		 *	left for it to be.
4490Sstevel@tonic-gate 		 *
4500Sstevel@tonic-gate 		 *	The status of the +, -, #, and blank
4510Sstevel@tonic-gate 		 *	flags are reflected in the variable
4520Sstevel@tonic-gate 		 *	"flagword".  "width" and "prec" contain
4530Sstevel@tonic-gate 		 *	numbers corresponding to the digit
4540Sstevel@tonic-gate 		 *	strings before and after the decimal
4550Sstevel@tonic-gate 		 *	point, respectively. If there was no
4560Sstevel@tonic-gate 		 *	decimal point, then flagword & DOTSEEN
4570Sstevel@tonic-gate 		 *	is false and the value of prec is meaningless.
4580Sstevel@tonic-gate 		 *
4590Sstevel@tonic-gate 		 *	The following switch cases set things up
4600Sstevel@tonic-gate 		 *	for printing.  What ultimately gets
4610Sstevel@tonic-gate 		 *	printed will be padding blanks, a
4620Sstevel@tonic-gate 		 *	prefix, left padding zeroes, a value,
4630Sstevel@tonic-gate 		 *	right padding zeroes, a suffix, and
4640Sstevel@tonic-gate 		 *	more padding blanks.  Padding blanks
4650Sstevel@tonic-gate 		 *	will not appear simultaneously on both
4660Sstevel@tonic-gate 		 *	the left and the right.  Each case in
4670Sstevel@tonic-gate 		 *	this switch will compute the value, and
4680Sstevel@tonic-gate 		 *	leave in several variables the informa-
4690Sstevel@tonic-gate 		 *	tion necessary to construct what is to
4700Sstevel@tonic-gate 		 *	be printed.
4710Sstevel@tonic-gate 		 *
4720Sstevel@tonic-gate 		 *	The prefix is a sign, a blank, "0x",
4730Sstevel@tonic-gate 		 *	"0X", or null, and is addressed by
4740Sstevel@tonic-gate 		 *	"prefix".
4750Sstevel@tonic-gate 		 *
4760Sstevel@tonic-gate 		 *	The suffix is either null or an
4770Sstevel@tonic-gate 		 *	exponent, and is addressed by "suffix".
4780Sstevel@tonic-gate 		 *	If there is a suffix, the flagword bit
4790Sstevel@tonic-gate 		 *	SUFFIX will be set.
4800Sstevel@tonic-gate 		 *
4810Sstevel@tonic-gate 		 *	The value to be printed starts at "bp"
4820Sstevel@tonic-gate 		 *	and continues up to and not including
4830Sstevel@tonic-gate 		 *	"p".
4840Sstevel@tonic-gate 		 *
4850Sstevel@tonic-gate 		 *	"lzero" and "rzero" will contain the
4860Sstevel@tonic-gate 		 *	number of padding zeroes required on
4870Sstevel@tonic-gate 		 *	the left and right, respectively.
4880Sstevel@tonic-gate 		 *	The flagword bits LZERO and RZERO tell
4890Sstevel@tonic-gate 		 *	whether padding zeros are required.
4900Sstevel@tonic-gate 		 *
4910Sstevel@tonic-gate 		 *	The number of padding blanks, and
4920Sstevel@tonic-gate 		 *	whether they go on the left or the
4930Sstevel@tonic-gate 		 *	right, will be computed on exit from
4940Sstevel@tonic-gate 		 *	the switch.
4950Sstevel@tonic-gate 		 */
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 		/*
5010Sstevel@tonic-gate 		 *	decimal fixed point representations
5020Sstevel@tonic-gate 		 *
5030Sstevel@tonic-gate 		 *	HIBITL is 100...000
5040Sstevel@tonic-gate 		 *	binary, and is equal to	the maximum
5050Sstevel@tonic-gate 		 *	negative number.
5060Sstevel@tonic-gate 		 *	We assume a 2's complement machine
5070Sstevel@tonic-gate 		 */
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 		case 'i':
5100Sstevel@tonic-gate 		case 'd':
5110Sstevel@tonic-gate 			/* Fetch the argument to be printed */
5120Sstevel@tonic-gate 			if (flagword & LENGTH)
5130Sstevel@tonic-gate 				val = va_arg(args.ap, long);
5140Sstevel@tonic-gate 			else
5150Sstevel@tonic-gate 				val = va_arg(args.ap, int);
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 			if (flagword & SHORT)
5180Sstevel@tonic-gate 				val = (short)val;
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 			/* Set buffer pointer to last digit */
5210Sstevel@tonic-gate 			p = bp = buf + MAXDIGS;
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 			/* If signed conversion, make sign */
5240Sstevel@tonic-gate 			if (val < 0) {
5250Sstevel@tonic-gate 				prefix = "-";
5260Sstevel@tonic-gate 				prefixlength = 1;
5270Sstevel@tonic-gate 				/*
5280Sstevel@tonic-gate 				 * Negate, checking in
5290Sstevel@tonic-gate 				 * advance for possible
5300Sstevel@tonic-gate 				 * overflow.
5310Sstevel@tonic-gate 				 */
5320Sstevel@tonic-gate 				if (val != HIBITL)
5330Sstevel@tonic-gate 					val = -val;
5340Sstevel@tonic-gate 				else	/* number is -HIBITL; convert last */
5350Sstevel@tonic-gate 					/* digit now and get positive number */
5360Sstevel@tonic-gate 					*--bp = _lowdigit(&val);
5370Sstevel@tonic-gate 			} else if (flagword & FPLUS) {
5380Sstevel@tonic-gate 				prefix = "+";
5390Sstevel@tonic-gate 				prefixlength = 1;
5400Sstevel@tonic-gate 			} else if (flagword & FBLANK) {
5410Sstevel@tonic-gate 				prefix = " ";
5420Sstevel@tonic-gate 				prefixlength = 1;
5430Sstevel@tonic-gate 			}
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 		decimal:
5460Sstevel@tonic-gate 			{	long qval = val;
5470Sstevel@tonic-gate 				long saveq;
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 				if (qval <= 9) {
5500Sstevel@tonic-gate 					if (qval != 0 || !(flagword & DOTSEEN))
5510Sstevel@tonic-gate 						*--bp = (char)(qval + '0');
5520Sstevel@tonic-gate 				} else {
5530Sstevel@tonic-gate 					do {
5540Sstevel@tonic-gate 						saveq = qval;
5550Sstevel@tonic-gate 						qval /= 10;
5560Sstevel@tonic-gate 						*--bp = (char)(saveq -
5570Sstevel@tonic-gate 							qval * 10 + '0');
5580Sstevel@tonic-gate 					} while (qval > 9);
5590Sstevel@tonic-gate 					*--bp = (char)(qval + '0');
5600Sstevel@tonic-gate 					pdiff = (ptrdiff_t)saveq;
5610Sstevel@tonic-gate 				}
5620Sstevel@tonic-gate 			}
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 			/* Calculate minimum padding zero requirement */
5650Sstevel@tonic-gate 			if (flagword & DOTSEEN) {
5660Sstevel@tonic-gate 				int leadzeroes = prec - (int)(p - bp);
5670Sstevel@tonic-gate 				if (leadzeroes > 0) {
5680Sstevel@tonic-gate 					otherlength = lzero = leadzeroes;
5690Sstevel@tonic-gate 					flagword |= LZERO;
5700Sstevel@tonic-gate 				}
5710Sstevel@tonic-gate 			}
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 			break;
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 		case 'u':
5760Sstevel@tonic-gate 			/* Fetch the argument to be printed */
5770Sstevel@tonic-gate 			if (flagword & LENGTH)
5780Sstevel@tonic-gate 				val = va_arg(args.ap, long);
5790Sstevel@tonic-gate 			else
5800Sstevel@tonic-gate 				val = va_arg(args.ap, unsigned);
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 			if (flagword & SHORT)
5830Sstevel@tonic-gate 				val = (unsigned short)val;
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 			p = bp = buf + MAXDIGS;
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 			if (val & HIBITL)
5880Sstevel@tonic-gate 				*--bp = _lowdigit(&val);
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 			goto decimal;
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 		/*
5930Sstevel@tonic-gate 		 *	non-decimal fixed point representations
5940Sstevel@tonic-gate 		 *	for radix equal to a power of two
5950Sstevel@tonic-gate 		 *
5960Sstevel@tonic-gate 		 *	"mradix" is one less than the radix for the conversion.
5970Sstevel@tonic-gate 		 *	"lradix" is one less than the base 2 log
5980Sstevel@tonic-gate 		 *	of the radix for the conversion. Conversion is unsigned.
5990Sstevel@tonic-gate 		 *	HIBITL is 100...000
6000Sstevel@tonic-gate 		 *	binary, and is equal to	the maximum
6010Sstevel@tonic-gate 		 *	negative number.
6020Sstevel@tonic-gate 		 *	We assume a 2's complement machine
6030Sstevel@tonic-gate 		 */
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 		case 'o':
6060Sstevel@tonic-gate 			mradix = 7;
6070Sstevel@tonic-gate 			lradix = 2;
6080Sstevel@tonic-gate 			goto fixed;
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 		case 'X':
6110Sstevel@tonic-gate 		case 'x':
6120Sstevel@tonic-gate 		case 'p':
6130Sstevel@tonic-gate 			mradix = 15;
6140Sstevel@tonic-gate 			lradix = 3;
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 		fixed:
6170Sstevel@tonic-gate 			/* Fetch the argument to be printed */
6180Sstevel@tonic-gate 			if (flagword & LENGTH)
6190Sstevel@tonic-gate 				val = va_arg(args.ap, long);
6200Sstevel@tonic-gate 			else
6210Sstevel@tonic-gate 				val = va_arg(args.ap, unsigned);
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 			if (flagword & SHORT)
6240Sstevel@tonic-gate 				val = (unsigned short)val;
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 			/* Set translate table for digits */
6270Sstevel@tonic-gate 			tab = (fcode == 'X') ? uc_digs : lc_digs;
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 			/* Entry point when printing a double which is a NaN */
6300Sstevel@tonic-gate 		put_pc:
6310Sstevel@tonic-gate 			/* Develop the digits of the value */
6320Sstevel@tonic-gate 			p = bp = buf + MAXDIGS;
6330Sstevel@tonic-gate 			{	long qval = val;
6340Sstevel@tonic-gate 				if (qval == 0) {
6350Sstevel@tonic-gate 					if (!(flagword & DOTSEEN)) {
6360Sstevel@tonic-gate 						otherlength = lzero = 1;
6370Sstevel@tonic-gate 						flagword |= LZERO;
6380Sstevel@tonic-gate 					}
6390Sstevel@tonic-gate 				} else
6400Sstevel@tonic-gate 					do {
6410Sstevel@tonic-gate 						*--bp = tab[qval & mradix];
6420Sstevel@tonic-gate 						qval = ((qval >> 1) & ~HIBITL)
6430Sstevel@tonic-gate 								>> lradix;
6440Sstevel@tonic-gate 					} while (qval != 0);
6450Sstevel@tonic-gate 			}
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 			/* Calculate minimum padding zero requirement */
6480Sstevel@tonic-gate 			if (flagword & DOTSEEN) {
6490Sstevel@tonic-gate 				int leadzeroes = prec - (int)(p - bp);
6500Sstevel@tonic-gate 				if (leadzeroes > 0) {
6510Sstevel@tonic-gate 					otherlength = lzero = leadzeroes;
6520Sstevel@tonic-gate 					flagword |= LZERO;
6530Sstevel@tonic-gate 				}
6540Sstevel@tonic-gate 			}
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 			/* Handle the # flag */
6570Sstevel@tonic-gate 			if (flagword & FSHARP && val != 0)
6580Sstevel@tonic-gate 				switch (fcode) {
6590Sstevel@tonic-gate 				case 'o':
6600Sstevel@tonic-gate 					if (!(flagword & LZERO)) {
6610Sstevel@tonic-gate 						otherlength = lzero = 1;
6620Sstevel@tonic-gate 						flagword |= LZERO;
6630Sstevel@tonic-gate 					}
6640Sstevel@tonic-gate 					break;
6650Sstevel@tonic-gate 				case 'x':
6660Sstevel@tonic-gate 					prefix = "0x";
6670Sstevel@tonic-gate 					prefixlength = 2;
6680Sstevel@tonic-gate 					break;
6690Sstevel@tonic-gate 				case 'X':
6700Sstevel@tonic-gate 					prefix = "0X";
6710Sstevel@tonic-gate 					prefixlength = 2;
6720Sstevel@tonic-gate 					break;
6730Sstevel@tonic-gate 				}
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 			break;
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 		case 'E':
6780Sstevel@tonic-gate 		case 'e':
6790Sstevel@tonic-gate 			/*
6800Sstevel@tonic-gate 			 * E-format.  The general strategy
6810Sstevel@tonic-gate 			 * here is fairly easy: we take
6820Sstevel@tonic-gate 			 * what ecvt gives us and re-format it.
6830Sstevel@tonic-gate 			 */
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 			/* Establish default precision */
6860Sstevel@tonic-gate 			if (!(flagword & DOTSEEN))
6870Sstevel@tonic-gate 				prec = 6;
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 			/* Fetch the value */
6900Sstevel@tonic-gate 			dval = va_arg(args.ap, double);
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 			/* Check for NaNs and Infinities */
6930Sstevel@tonic-gate 			if (IsNANorINF(dval)) {
6940Sstevel@tonic-gate 				if (IsINF(dval)) {
6950Sstevel@tonic-gate 					if (IsNegNAN(dval))
6960Sstevel@tonic-gate 						neg_in = 1;
6970Sstevel@tonic-gate 					inf_nan = 1;
6980Sstevel@tonic-gate 					bp = (fcode == 'E')? uc_inf: lc_inf;
6990Sstevel@tonic-gate 					p = bp + 3;
7000Sstevel@tonic-gate 					break;
7010Sstevel@tonic-gate 				} else {
7020Sstevel@tonic-gate 					if (IsNegNAN(dval))
7030Sstevel@tonic-gate 						neg_in = 1;
7040Sstevel@tonic-gate 					inf_nan = 1;
7050Sstevel@tonic-gate 					val = GETNaNPC(dval);
7060Sstevel@tonic-gate 					NaN_flg = SNLEN;
7070Sstevel@tonic-gate 					mradix = 15;
7080Sstevel@tonic-gate 					lradix = 3;
7090Sstevel@tonic-gate 					if (fcode == 'E') {
7100Sstevel@tonic-gate 						SNAN = uc_nan;
7110Sstevel@tonic-gate 						tab =  uc_digs;
7120Sstevel@tonic-gate 					} else {
7130Sstevel@tonic-gate 						SNAN =  lc_nan;
7140Sstevel@tonic-gate 						tab =  lc_digs;
7150Sstevel@tonic-gate 					}
7160Sstevel@tonic-gate 					goto put_pc;
7170Sstevel@tonic-gate 				}
7180Sstevel@tonic-gate 			}
7190Sstevel@tonic-gate 			/* Develop the mantissa */
7200Sstevel@tonic-gate 			bp = ecvt(dval, min(prec + 1, MAXECVT), &decpt, &sign);
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 			/* Determine the prefix */
7230Sstevel@tonic-gate 		e_merge:
7240Sstevel@tonic-gate 			if (sign) {
7250Sstevel@tonic-gate 				prefix = "-";
7260Sstevel@tonic-gate 				prefixlength = 1;
7270Sstevel@tonic-gate 			} else if (flagword & FPLUS) {
7280Sstevel@tonic-gate 				prefix = "+";
7290Sstevel@tonic-gate 				prefixlength = 1;
7300Sstevel@tonic-gate 			} else if (flagword & FBLANK) {
7310Sstevel@tonic-gate 				prefix = " ";
7320Sstevel@tonic-gate 				prefixlength = 1;
7330Sstevel@tonic-gate 			}
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 			/* Place the first digit in the buffer */
7360Sstevel@tonic-gate 			p = &buf[0];
7370Sstevel@tonic-gate 			*p++ = (*bp != '\0') ? *bp++ : '0';
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 			/* Put in a decimal point if needed */
7400Sstevel@tonic-gate 			if (prec != 0 || (flagword & FSHARP))
7410Sstevel@tonic-gate 				*p++ = _numeric[0];
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 			/* Create the rest of the mantissa */
7440Sstevel@tonic-gate 			{	int rz = prec;
7450Sstevel@tonic-gate 				for (; rz > 0 && *bp != '\0'; --rz)
7460Sstevel@tonic-gate 					*p++ = *bp++;
7470Sstevel@tonic-gate 				if (rz > 0) {
7480Sstevel@tonic-gate 					otherlength = rzero = rz;
7490Sstevel@tonic-gate 					flagword |= RZERO;
7500Sstevel@tonic-gate 				}
7510Sstevel@tonic-gate 			}
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 			bp = &buf[0];
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 			/* Create the exponent */
7560Sstevel@tonic-gate 			*(suffix = &expbuf[MAXESIZ]) = '\0';
7570Sstevel@tonic-gate 			if (dval != 0) {
7580Sstevel@tonic-gate 				int nn = decpt - 1;
7590Sstevel@tonic-gate 				if (nn < 0)
7600Sstevel@tonic-gate 				    nn = -nn;
7610Sstevel@tonic-gate 				for (; nn > 9; nn /= 10)
7620Sstevel@tonic-gate 					*--suffix = todigit(nn % 10);
7630Sstevel@tonic-gate 				*--suffix = todigit(nn);
7640Sstevel@tonic-gate 			}
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 			/* Prepend leading zeroes to the exponent */
7670Sstevel@tonic-gate 			while (suffix > &expbuf[MAXESIZ - 2])
7680Sstevel@tonic-gate 				*--suffix = '0';
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 			/* Put in the exponent sign */
7710Sstevel@tonic-gate 			*--suffix = (decpt > 0 || dval == 0) ? '+' : '-';
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 			/* Put in the e */
7740Sstevel@tonic-gate 			*--suffix = isupper(fcode) ? 'E'  : 'e';
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 			/* compute size of suffix */
7770Sstevel@tonic-gate 			otherlength += (suffixlength =
7780Sstevel@tonic-gate 				(int)(&expbuf[MAXESIZ] - suffix));
7790Sstevel@tonic-gate 			flagword |= SUFFIX;
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 			break;
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 		case 'f':
7840Sstevel@tonic-gate 			/*
7850Sstevel@tonic-gate 			 * F-format floating point.  This is a
7860Sstevel@tonic-gate 			 * good deal less simple than E-format.
7870Sstevel@tonic-gate 			 * The overall strategy will be to call
7880Sstevel@tonic-gate 			 * fcvt, reformat its result into buf,
7890Sstevel@tonic-gate 			 * and calculate how many trailing
7900Sstevel@tonic-gate 			 * zeroes will be required.  There will
7910Sstevel@tonic-gate 			 * never be any leading zeroes needed.
7920Sstevel@tonic-gate 			 */
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 			/* Establish default precision */
7950Sstevel@tonic-gate 			if (!(flagword & DOTSEEN))
7960Sstevel@tonic-gate 				prec = 6;
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 			/* Fetch the value */
7990Sstevel@tonic-gate 			dval = va_arg(args.ap, double);
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 			/* Check for NaNs and Infinities  */
8020Sstevel@tonic-gate 			if (IsNANorINF(dval)) {
8030Sstevel@tonic-gate 				if (IsINF(dval)) {
8040Sstevel@tonic-gate 					if (IsNegNAN(dval))
8050Sstevel@tonic-gate 						neg_in = 1;
8060Sstevel@tonic-gate 					inf_nan = 1;
8070Sstevel@tonic-gate 					bp = lc_inf;
8080Sstevel@tonic-gate 					p = bp + 3;
8090Sstevel@tonic-gate 					break;
8100Sstevel@tonic-gate 				} else {
8110Sstevel@tonic-gate 					if (IsNegNAN(dval))
8120Sstevel@tonic-gate 						neg_in = 1;
8130Sstevel@tonic-gate 					inf_nan = 1;
8140Sstevel@tonic-gate 					val  = GETNaNPC(dval);
8150Sstevel@tonic-gate 					NaN_flg = SNLEN;
8160Sstevel@tonic-gate 					mradix = 15;
8170Sstevel@tonic-gate 					lradix = 3;
8180Sstevel@tonic-gate 					tab =  lc_digs;
8190Sstevel@tonic-gate 					SNAN = lc_nan;
8200Sstevel@tonic-gate 					goto put_pc;
8210Sstevel@tonic-gate 				}
8220Sstevel@tonic-gate 			}
8230Sstevel@tonic-gate 			/* Do the conversion */
8240Sstevel@tonic-gate 			bp = fcvt(dval, min(prec, MAXFCVT), &decpt, &sign);
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 			/* Determine the prefix */
8270Sstevel@tonic-gate 		f_merge:
8280Sstevel@tonic-gate 			if (sign) {
8290Sstevel@tonic-gate 				prefix = "-";
8300Sstevel@tonic-gate 				prefixlength = 1;
8310Sstevel@tonic-gate 			} else if (flagword & FPLUS) {
8320Sstevel@tonic-gate 				prefix = "+";
8330Sstevel@tonic-gate 				prefixlength = 1;
8340Sstevel@tonic-gate 			} else if (flagword & FBLANK) {
8350Sstevel@tonic-gate 				prefix = " ";
8360Sstevel@tonic-gate 				prefixlength = 1;
8370Sstevel@tonic-gate 			}
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 			/* Initialize buffer pointer */
8400Sstevel@tonic-gate 			p = &buf[0];
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 			{	int nn = decpt;
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 				/* Emit the digits before the decimal point */
8450Sstevel@tonic-gate 				k = 0;
8460Sstevel@tonic-gate 				do {
8470Sstevel@tonic-gate 					*p++ = (nn <= 0 || *bp == '\0' ||
8480Sstevel@tonic-gate 						k >= MAXFSIG) ?
8490Sstevel@tonic-gate 						'0' : (k++, *bp++);
8500Sstevel@tonic-gate 				} while (--nn > 0);
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 				/* Decide whether we need a decimal point */
8530Sstevel@tonic-gate 				if ((flagword & FSHARP) || prec > 0)
8540Sstevel@tonic-gate 					*p++ = _numeric[0];
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 				/* Digits (if any) after the decimal point */
8570Sstevel@tonic-gate 				nn = min(prec, MAXFCVT);
8580Sstevel@tonic-gate 				if (prec > nn) {
8590Sstevel@tonic-gate 					flagword |= RZERO;
8600Sstevel@tonic-gate 					otherlength = rzero = prec - nn;
8610Sstevel@tonic-gate 				}
8620Sstevel@tonic-gate 				while (--nn >= 0)
8630Sstevel@tonic-gate 					*p++ = (++decpt <= 0 || *bp == '\0' ||
8640Sstevel@tonic-gate 						k >= MAXFSIG) ?
8650Sstevel@tonic-gate 						'0' : (k++, *bp++);
8660Sstevel@tonic-gate 			}
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 			bp = &buf[0];
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 			break;
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 		case 'G':
8730Sstevel@tonic-gate 		case 'g':
8740Sstevel@tonic-gate 			/*
8750Sstevel@tonic-gate 			 * g-format.  We play around a bit
8760Sstevel@tonic-gate 			 * and then jump into e or f, as needed.
8770Sstevel@tonic-gate 			 */
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 			/* Establish default precision */
8800Sstevel@tonic-gate 			if (!(flagword & DOTSEEN))
8810Sstevel@tonic-gate 				prec = 6;
8820Sstevel@tonic-gate 			else if (prec == 0)
8830Sstevel@tonic-gate 				prec = 1;
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 			/* Fetch the value */
8860Sstevel@tonic-gate 			dval = va_arg(args.ap, double);
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 			/* Check for NaN and Infinities  */
8890Sstevel@tonic-gate 			if (IsNANorINF(dval)) {
8900Sstevel@tonic-gate 				if (IsINF(dval)) {
8910Sstevel@tonic-gate 					if (IsNegNAN(dval))
8920Sstevel@tonic-gate 						neg_in = 1;
8930Sstevel@tonic-gate 					bp = (fcode == 'G') ? uc_inf : lc_inf;
8940Sstevel@tonic-gate 					p = bp + 3;
8950Sstevel@tonic-gate 					inf_nan = 1;
8960Sstevel@tonic-gate 					break;
8970Sstevel@tonic-gate 				} else {
8980Sstevel@tonic-gate 					if (IsNegNAN(dval))
8990Sstevel@tonic-gate 						neg_in = 1;
9000Sstevel@tonic-gate 					inf_nan = 1;
9010Sstevel@tonic-gate 					val  = GETNaNPC(dval);
9020Sstevel@tonic-gate 					NaN_flg = SNLEN;
9030Sstevel@tonic-gate 					mradix = 15;
9040Sstevel@tonic-gate 					lradix = 3;
9050Sstevel@tonic-gate 					if (fcode == 'G') {
9060Sstevel@tonic-gate 						SNAN = uc_nan;
9070Sstevel@tonic-gate 						tab = uc_digs;
9080Sstevel@tonic-gate 					} else {
9090Sstevel@tonic-gate 						SNAN = lc_nan;
9100Sstevel@tonic-gate 						tab =  lc_digs;
9110Sstevel@tonic-gate 					}
9120Sstevel@tonic-gate 					goto put_pc;
9130Sstevel@tonic-gate 				}
9140Sstevel@tonic-gate 			}
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 			/* Do the conversion */
9170Sstevel@tonic-gate 			bp = ecvt(dval, min(prec, MAXECVT), &decpt, &sign);
9180Sstevel@tonic-gate 			if (dval == 0)
9190Sstevel@tonic-gate 				decpt = 1;
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 			{	int kk = prec;
9220Sstevel@tonic-gate 				size_t sz;
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 				if (!(flagword & FSHARP)) {
9250Sstevel@tonic-gate 					sz = strlen(bp);
9260Sstevel@tonic-gate 					if (sz < kk)
9270Sstevel@tonic-gate 						kk = (int)sz;
9280Sstevel@tonic-gate 					while (kk >= 1 && bp[kk-1] == '0')
9290Sstevel@tonic-gate 						--kk;
9300Sstevel@tonic-gate 				}
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 				if (decpt < -3 || decpt > prec) {
9330Sstevel@tonic-gate 					prec = kk - 1;
9340Sstevel@tonic-gate 					goto e_merge;
9350Sstevel@tonic-gate 				}
9360Sstevel@tonic-gate 				prec = kk - decpt;
9370Sstevel@tonic-gate 				goto f_merge;
9380Sstevel@tonic-gate 			}
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 		case '%':
9410Sstevel@tonic-gate 			buf[0] = (char)fcode;
9420Sstevel@tonic-gate 			goto c_merge;
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 		case 'c':
9450Sstevel@tonic-gate 			buf[0] = va_arg(args.ap, int);
9460Sstevel@tonic-gate 		c_merge:
9470Sstevel@tonic-gate 			p = (bp = &buf[0]) + 1;
9480Sstevel@tonic-gate 			break;
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 		case 's':
9510Sstevel@tonic-gate 			bp = va_arg(args.ap, char *);
9520Sstevel@tonic-gate 			if (!(flagword & DOTSEEN))
9530Sstevel@tonic-gate 				p = bp + strlen(bp);
9540Sstevel@tonic-gate 			else { /* a strnlen function would  be useful here! */
9550Sstevel@tonic-gate 				char *qp = bp;
9560Sstevel@tonic-gate 				while (*qp++ != '\0' && --prec >= 0)
9570Sstevel@tonic-gate 					;
9580Sstevel@tonic-gate 				p = qp - 1;
9590Sstevel@tonic-gate 			}
9600Sstevel@tonic-gate 			break;
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 		case 'n':
9630Sstevel@tonic-gate 			{
9640Sstevel@tonic-gate 				if (flagword & LENGTH) {
9650Sstevel@tonic-gate 					long *svcount;
9660Sstevel@tonic-gate 					svcount = va_arg(args.ap, long *);
9670Sstevel@tonic-gate 					*svcount = count;
9680Sstevel@tonic-gate 				} else if (flagword & SHORT) {
9690Sstevel@tonic-gate 					short *svcount;
9700Sstevel@tonic-gate 					svcount = va_arg(args.ap, short *);
9710Sstevel@tonic-gate 					*svcount = (short)count;
9720Sstevel@tonic-gate 				} else {
9730Sstevel@tonic-gate 					int *svcount;
9740Sstevel@tonic-gate 					svcount = va_arg(args.ap, int *);
9750Sstevel@tonic-gate 					*svcount = count;
9760Sstevel@tonic-gate 				}
9770Sstevel@tonic-gate 				continue;
9780Sstevel@tonic-gate 			}
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 		default: /* this is technically an error; what we do is to */
9810Sstevel@tonic-gate 			/* back up the format pointer to the offending char */
9820Sstevel@tonic-gate 			/* and continue with the format scan */
9830Sstevel@tonic-gate 			format--;
9840Sstevel@tonic-gate 			continue;
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 		}
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 		if (inf_nan) {
9890Sstevel@tonic-gate 			if (neg_in) {
9900Sstevel@tonic-gate 				prefix = "-";
9910Sstevel@tonic-gate 				prefixlength = 1;
9920Sstevel@tonic-gate 				neg_in = 0;
9930Sstevel@tonic-gate 			} else if (flagword & FPLUS) {
9940Sstevel@tonic-gate 				prefix = "+";
9950Sstevel@tonic-gate 				prefixlength = 1;
9960Sstevel@tonic-gate 			} else if (flagword & FBLANK) {
9970Sstevel@tonic-gate 				prefix = " ";
9980Sstevel@tonic-gate 				prefixlength = 1;
9990Sstevel@tonic-gate 			}
10000Sstevel@tonic-gate 			inf_nan = 0;
10010Sstevel@tonic-gate 		}
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 		/* Calculate number of padding blanks */
10040Sstevel@tonic-gate 		k = (int)(pdiff = p - bp) + prefixlength + otherlength +
10050Sstevel@tonic-gate 			NaN_flg;
10060Sstevel@tonic-gate 		if (width <= k)
10070Sstevel@tonic-gate 			count += k;
10080Sstevel@tonic-gate 		else {
10090Sstevel@tonic-gate 			count += width;
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 			/* Set up for padding zeroes if requested */
10120Sstevel@tonic-gate 			/* Otherwise emit padding blanks unless output is */
10130Sstevel@tonic-gate 			/* to be left-justified.  */
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate 			if (flagword & PADZERO) {
10160Sstevel@tonic-gate 				if (!(flagword & LZERO)) {
10170Sstevel@tonic-gate 					flagword |= LZERO;
10180Sstevel@tonic-gate 					lzero = width - k;
10190Sstevel@tonic-gate 				}
10200Sstevel@tonic-gate 				else
10210Sstevel@tonic-gate 					lzero += width - k;
10220Sstevel@tonic-gate 				k = width; /* cancel padding blanks */
10230Sstevel@tonic-gate 			} else
10240Sstevel@tonic-gate 				/* Blanks on left if required */
10250Sstevel@tonic-gate 				if (!(flagword & FMINUS))
10260Sstevel@tonic-gate 					PAD(_blanks, width - k);
10270Sstevel@tonic-gate 		}
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 		/* Prefix, if any */
10300Sstevel@tonic-gate 		if (prefixlength != 0)
10310Sstevel@tonic-gate 			PUT(prefix, prefixlength);
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 		/* If value is NaN, put string NaN */
10340Sstevel@tonic-gate 		if (NaN_flg) {
10350Sstevel@tonic-gate 			PUT(SNAN, SNLEN);
10360Sstevel@tonic-gate 			NaN_flg = 0;
10370Sstevel@tonic-gate 		}
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 		/* Zeroes on the left */
10400Sstevel@tonic-gate 		if (flagword & LZERO)
10410Sstevel@tonic-gate 			PAD(_zeroes, lzero);
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 		/* The value itself */
10440Sstevel@tonic-gate 		if (pdiff > 0)
10450Sstevel@tonic-gate 			PUT(bp, pdiff);
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 		if (flagword & (RZERO | SUFFIX | FMINUS)) {
10480Sstevel@tonic-gate 			/* Zeroes on the right */
10490Sstevel@tonic-gate 			if (flagword & RZERO)
10500Sstevel@tonic-gate 				PAD(_zeroes, rzero);
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 			/* The suffix */
10530Sstevel@tonic-gate 			if (flagword & SUFFIX)
10540Sstevel@tonic-gate 				PUT(suffix, suffixlength);
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 			/* Blanks on the right if required */
10570Sstevel@tonic-gate 			if (flagword & FMINUS && width > k)
10580Sstevel@tonic-gate 				PAD(_blanks, width - k);
10590Sstevel@tonic-gate 		}
10600Sstevel@tonic-gate 	}
10610Sstevel@tonic-gate }
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate /*
10640Sstevel@tonic-gate  * This function initializes arglst, to contain the appropriate va_list values
10650Sstevel@tonic-gate  * for the first MAXARGS arguments.
10660Sstevel@tonic-gate  */
10670Sstevel@tonic-gate void
10680Sstevel@tonic-gate _mkarglst(char *fmt, stva_list args, stva_list arglst[])
10690Sstevel@tonic-gate {
10700Sstevel@tonic-gate 	static char digits[] = "01234567890", skips[] = "# +-.0123456789hL$";
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	enum types {INT = 1, LONG, CHAR_PTR, DOUBLE, LONG_DOUBLE, VOID_PTR,
10730Sstevel@tonic-gate 		LONG_PTR, INT_PTR};
10740Sstevel@tonic-gate 	enum types typelst[MAXARGS], curtype;
10750Sstevel@tonic-gate 	int maxnum, n, curargno, flags;
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	/*
10780Sstevel@tonic-gate 	 * Algorithm	1. set all argument types to zero.
10790Sstevel@tonic-gate 	 *		2. walk through fmt putting arg types in typelst[].
10800Sstevel@tonic-gate 	 *		3. walk through args using va_arg(args.ap, typelst[n])
10810Sstevel@tonic-gate 	 *		   and set arglst[] to the appropriate values.
10820Sstevel@tonic-gate 	 * Assumptions:	Cannot use %*$... to specify variable position.
10830Sstevel@tonic-gate 	 */
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	(void) memset((void *)typelst, 0, sizeof (typelst));
10860Sstevel@tonic-gate 	maxnum = -1;
10870Sstevel@tonic-gate 	curargno = 0;
10880Sstevel@tonic-gate 	while ((fmt = strchr(fmt, '%')) != 0) {
10890Sstevel@tonic-gate 		size_t sz;
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 		fmt++;	/* skip % */
10920Sstevel@tonic-gate 		if (fmt[sz = strspn(fmt, digits)] == '$') {
10930Sstevel@tonic-gate 			curargno = atoi(fmt) - 1;
10940Sstevel@tonic-gate 				/* convert to zero base */
10950Sstevel@tonic-gate 			if (curargno < 0)
10960Sstevel@tonic-gate 				continue;
10970Sstevel@tonic-gate 			fmt += sz + 1;
10980Sstevel@tonic-gate 		}
10990Sstevel@tonic-gate 		flags = 0;
11000Sstevel@tonic-gate 	again:;
11010Sstevel@tonic-gate 		fmt += strspn(fmt, skips);
11020Sstevel@tonic-gate 		switch (*fmt++) {
11030Sstevel@tonic-gate 		case '%':	/* there is no argument! */
11040Sstevel@tonic-gate 			continue;
11050Sstevel@tonic-gate 		case 'l':
11060Sstevel@tonic-gate 			flags |= 0x1;
11070Sstevel@tonic-gate 			goto again;
11080Sstevel@tonic-gate 		case '*':	/* int argument used for value */
11090Sstevel@tonic-gate 			/* check if there is a positional parameter */
11100Sstevel@tonic-gate 			if (isdigit(*fmt)) {
11110Sstevel@tonic-gate 				int	targno;
11120Sstevel@tonic-gate 				targno = atoi(fmt) - 1;
11130Sstevel@tonic-gate 				fmt += strspn(fmt, digits);
11140Sstevel@tonic-gate 				if (*fmt == '$')
11150Sstevel@tonic-gate 					fmt++; /* skip '$' */
11160Sstevel@tonic-gate 				if (targno >= 0 && targno < MAXARGS) {
11170Sstevel@tonic-gate 					typelst[targno] = INT;
11180Sstevel@tonic-gate 					if (maxnum < targno)
11190Sstevel@tonic-gate 						maxnum = targno;
11200Sstevel@tonic-gate 				}
11210Sstevel@tonic-gate 				goto again;
11220Sstevel@tonic-gate 			}
11230Sstevel@tonic-gate 			flags |= 0x2;
11240Sstevel@tonic-gate 			curtype = INT;
11250Sstevel@tonic-gate 			break;
11260Sstevel@tonic-gate 		case 'e':
11270Sstevel@tonic-gate 		case 'E':
11280Sstevel@tonic-gate 		case 'f':
11290Sstevel@tonic-gate 		case 'g':
11300Sstevel@tonic-gate 		case 'G':
11310Sstevel@tonic-gate 			curtype = DOUBLE;
11320Sstevel@tonic-gate 			break;
11330Sstevel@tonic-gate 		case 's':
11340Sstevel@tonic-gate 			curtype = CHAR_PTR;
11350Sstevel@tonic-gate 			break;
11360Sstevel@tonic-gate 		case 'p':
11370Sstevel@tonic-gate 			curtype = VOID_PTR;
11380Sstevel@tonic-gate 			break;
11390Sstevel@tonic-gate 		case 'n':
11400Sstevel@tonic-gate 			if (flags & 0x1)
11410Sstevel@tonic-gate 				curtype = LONG_PTR;
11420Sstevel@tonic-gate 			else
11430Sstevel@tonic-gate 				curtype = INT_PTR;
11440Sstevel@tonic-gate 			break;
11450Sstevel@tonic-gate 		default:
11460Sstevel@tonic-gate 			if (flags & 0x1)
11470Sstevel@tonic-gate 				curtype = LONG;
11480Sstevel@tonic-gate 			else
11490Sstevel@tonic-gate 				curtype = INT;
11500Sstevel@tonic-gate 			break;
11510Sstevel@tonic-gate 		}
11520Sstevel@tonic-gate 		if (curargno >= 0 && curargno < MAXARGS) {
11530Sstevel@tonic-gate 			typelst[curargno] = curtype;
11540Sstevel@tonic-gate 			if (maxnum < curargno)
11550Sstevel@tonic-gate 				maxnum = curargno;
11560Sstevel@tonic-gate 		}
11570Sstevel@tonic-gate 		curargno++;	/* default to next in list */
11580Sstevel@tonic-gate 		if (flags & 0x2)	/* took care of *, keep going */
11590Sstevel@tonic-gate 		{
11600Sstevel@tonic-gate 			flags ^= 0x2;
11610Sstevel@tonic-gate 			goto again;
11620Sstevel@tonic-gate 		}
11630Sstevel@tonic-gate 	}
11640Sstevel@tonic-gate 	for (n = 0; n <= maxnum; n++) {
11650Sstevel@tonic-gate 		arglst[n] = args;
11660Sstevel@tonic-gate 		if (typelst[n] == 0)
11670Sstevel@tonic-gate 			typelst[n] = INT;
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 		switch (typelst[n]) {
11700Sstevel@tonic-gate 		case INT:
11710Sstevel@tonic-gate 			(void) va_arg(args.ap, int);
11720Sstevel@tonic-gate 			break;
11730Sstevel@tonic-gate 		case LONG:
11740Sstevel@tonic-gate 			(void) va_arg(args.ap, long);
11750Sstevel@tonic-gate 			break;
11760Sstevel@tonic-gate 		case CHAR_PTR:
11770Sstevel@tonic-gate 			(void) va_arg(args.ap, char *);
11780Sstevel@tonic-gate 			break;
11790Sstevel@tonic-gate 		case DOUBLE:
11800Sstevel@tonic-gate 			(void) va_arg(args.ap, double);
11810Sstevel@tonic-gate 			break;
11820Sstevel@tonic-gate 		case LONG_DOUBLE:
11830Sstevel@tonic-gate 			(void) va_arg(args.ap, double);
11840Sstevel@tonic-gate 			break;
11850Sstevel@tonic-gate 		case VOID_PTR:
11860Sstevel@tonic-gate 			(void) va_arg(args.ap, void *);
11870Sstevel@tonic-gate 			break;
11880Sstevel@tonic-gate 		case LONG_PTR:
11890Sstevel@tonic-gate 			(void) va_arg(args.ap, long *);
11900Sstevel@tonic-gate 			break;
11910Sstevel@tonic-gate 		case INT_PTR:
11920Sstevel@tonic-gate 			(void) va_arg(args.ap, int *);
11930Sstevel@tonic-gate 			break;
11940Sstevel@tonic-gate 		}
11950Sstevel@tonic-gate 	}
11960Sstevel@tonic-gate }
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate /*
11990Sstevel@tonic-gate  * This function is used to find the va_list value for arguments whose
12000Sstevel@tonic-gate  * position is greater than MAXARGS.  This function is slow, so hopefully
12010Sstevel@tonic-gate  * MAXARGS will be big enough so that this function need only be called in
12020Sstevel@tonic-gate  * unusual circumstances.
12030Sstevel@tonic-gate  * pargs is assumed to contain the value of arglst[MAXARGS - 1].
12040Sstevel@tonic-gate  */
12050Sstevel@tonic-gate void
12060Sstevel@tonic-gate _getarg(char *fmt, stva_list *pargs, int argno)
12070Sstevel@tonic-gate {
12080Sstevel@tonic-gate 	static char digits[] = "01234567890", skips[] = "# +-.0123456789h$";
12090Sstevel@tonic-gate 	int i, curargno, flags;
12100Sstevel@tonic-gate 	size_t n;
12110Sstevel@tonic-gate 	char	*sfmt = fmt;
12120Sstevel@tonic-gate 	int	found = 1;
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 	i = MAXARGS;
12150Sstevel@tonic-gate 	curargno = 1;
12160Sstevel@tonic-gate 	while (found) {
12170Sstevel@tonic-gate 		fmt = sfmt;
12180Sstevel@tonic-gate 		found = 0;
12190Sstevel@tonic-gate 		while ((i != argno) && (fmt = strchr(fmt, '%')) != 0) {
12200Sstevel@tonic-gate 			fmt++;	/* skip % */
12210Sstevel@tonic-gate 			if (fmt[n = strspn(fmt, digits)] == '$') {
12220Sstevel@tonic-gate 				curargno = atoi(fmt);
12230Sstevel@tonic-gate 				if (curargno <= 0)
12240Sstevel@tonic-gate 					continue;
12250Sstevel@tonic-gate 				fmt += n + 1;
12260Sstevel@tonic-gate 			}
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 			/* find conversion specifier for next argument */
12290Sstevel@tonic-gate 			if (i != curargno) {
12300Sstevel@tonic-gate 				curargno++;
12310Sstevel@tonic-gate 				continue;
12320Sstevel@tonic-gate 			} else
12330Sstevel@tonic-gate 				found = 1;
12340Sstevel@tonic-gate 			flags = 0;
12350Sstevel@tonic-gate 		again:;
12360Sstevel@tonic-gate 			fmt += strspn(fmt, skips);
12370Sstevel@tonic-gate 			switch (*fmt++) {
12380Sstevel@tonic-gate 			case '%':	/* there is no argument! */
12390Sstevel@tonic-gate 				continue;
12400Sstevel@tonic-gate 			case 'l':
12410Sstevel@tonic-gate 				flags |= 0x1;
12420Sstevel@tonic-gate 				goto again;
12430Sstevel@tonic-gate 			case '*':	/* int argument used for value */
12440Sstevel@tonic-gate 				/*
12450Sstevel@tonic-gate 				 * check if there is a positional parameter;
12460Sstevel@tonic-gate 				 * if so, just skip it; its size will be
12470Sstevel@tonic-gate 				 * correctly determined by default
12480Sstevel@tonic-gate 				 */
12490Sstevel@tonic-gate 				if (isdigit(*fmt)) {
12500Sstevel@tonic-gate 					fmt += strspn(fmt, digits);
12510Sstevel@tonic-gate 					if (*fmt == '$')
12520Sstevel@tonic-gate 						fmt++; /* skip '$' */
12530Sstevel@tonic-gate 					goto again;
12540Sstevel@tonic-gate 				}
12550Sstevel@tonic-gate 				flags |= 0x2;
12560Sstevel@tonic-gate 				(void) va_arg((*pargs).ap, int);
12570Sstevel@tonic-gate 				break;
12580Sstevel@tonic-gate 			case 'e':
12590Sstevel@tonic-gate 			case 'E':
12600Sstevel@tonic-gate 			case 'f':
12610Sstevel@tonic-gate 			case 'g':
12620Sstevel@tonic-gate 			case 'G':
12630Sstevel@tonic-gate 				if (flags & 0x1)
12640Sstevel@tonic-gate 					(void) va_arg((*pargs).ap, double);
12650Sstevel@tonic-gate 				else
12660Sstevel@tonic-gate 					(void) va_arg((*pargs).ap, double);
12670Sstevel@tonic-gate 				break;
12680Sstevel@tonic-gate 			case 's':
12690Sstevel@tonic-gate 				(void) va_arg((*pargs).ap, char *);
12700Sstevel@tonic-gate 				break;
12710Sstevel@tonic-gate 			case 'p':
12720Sstevel@tonic-gate 				(void) va_arg((*pargs).ap, void *);
12730Sstevel@tonic-gate 				break;
12740Sstevel@tonic-gate 			case 'n':
12750Sstevel@tonic-gate 				if (flags & 0x1)
12760Sstevel@tonic-gate 					(void) va_arg((*pargs).ap, long *);
12770Sstevel@tonic-gate 				else
12780Sstevel@tonic-gate 					(void) va_arg((*pargs).ap, int *);
12790Sstevel@tonic-gate 				break;
12800Sstevel@tonic-gate 			default:
12810Sstevel@tonic-gate 				if (flags & 0x1)
12820Sstevel@tonic-gate 					(void) va_arg((*pargs).ap, long int);
12830Sstevel@tonic-gate 				else
12840Sstevel@tonic-gate 					(void) va_arg((*pargs).ap, int);
12850Sstevel@tonic-gate 				break;
12860Sstevel@tonic-gate 			}
12870Sstevel@tonic-gate 			i++;
12880Sstevel@tonic-gate 			curargno++;	/* default to next in list */
12890Sstevel@tonic-gate 			if (flags & 0x2)	/* took care of *, keep going */
12900Sstevel@tonic-gate 			{
12910Sstevel@tonic-gate 				flags ^= 0x2;
12920Sstevel@tonic-gate 				goto again;
12930Sstevel@tonic-gate 			}
12940Sstevel@tonic-gate 		}
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 		/*
12970Sstevel@tonic-gate 		 * missing specifier for parameter, assume parameter is an int
12980Sstevel@tonic-gate 		 */
12990Sstevel@tonic-gate 		if (!found && i != argno) {
13000Sstevel@tonic-gate 			(void) va_arg((*pargs).ap, int);
13010Sstevel@tonic-gate 			i++;
13020Sstevel@tonic-gate 			curargno = i;
13030Sstevel@tonic-gate 			found = 1;
13040Sstevel@tonic-gate 		}
13050Sstevel@tonic-gate 	}
13060Sstevel@tonic-gate }
1307