xref: /onnv-gate/usr/src/ucblib/libucb/port/stdio/doprnt.c (revision 6812:febeba71273d)
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
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211219Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate  * The Regents of the University of California
330Sstevel@tonic-gate  * All Rights Reserved
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate  * contributors.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  *	_doprnt: common code for printf, fprintf, sprintf
440Sstevel@tonic-gate  */
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include <sys/types.h>
470Sstevel@tonic-gate #include "file64.h"
480Sstevel@tonic-gate #include <stdio.h>
490Sstevel@tonic-gate #include <stdlib.h>
500Sstevel@tonic-gate #include <ctype.h>
510Sstevel@tonic-gate #include <stdarg.h>
520Sstevel@tonic-gate #include <values.h>
530Sstevel@tonic-gate #include <nan.h>
540Sstevel@tonic-gate #include <memory.h>
550Sstevel@tonic-gate #include <string.h>
560Sstevel@tonic-gate #include "print.h"	/* parameters & macros for doprnt */
570Sstevel@tonic-gate #include "stdiom.h"
580Sstevel@tonic-gate #include <locale.h>
590Sstevel@tonic-gate #include <stddef.h>
600Sstevel@tonic-gate #include "_locale.h"
610Sstevel@tonic-gate #include "libc.h"
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #define	PUT(p, n)	{ unsigned char *newbufptr; \
640Sstevel@tonic-gate 			if ((newbufptr = bufptr + (n)) > bufferend) { \
650Sstevel@tonic-gate 				_dowrite((p), (n), iop, &bufptr); \
660Sstevel@tonic-gate 			} else { \
670Sstevel@tonic-gate 				(void) memcpy(bufptr, (p), (n)); \
680Sstevel@tonic-gate 				bufptr = newbufptr; \
690Sstevel@tonic-gate 			} \
700Sstevel@tonic-gate 			}
710Sstevel@tonic-gate #define	PAD(s, n)	{ int nn; \
720Sstevel@tonic-gate 			for (nn = (n); nn > 20; nn -= 20) \
730Sstevel@tonic-gate 				_dowrite((s), 20, iop, &bufptr); \
740Sstevel@tonic-gate 			PUT((s), nn); \
750Sstevel@tonic-gate 			}
760Sstevel@tonic-gate 
770Sstevel@tonic-gate #define	SNLEN	5	/* Length of string used when printing a NaN */
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /* bit positions for flags used in doprnt */
800Sstevel@tonic-gate 
810Sstevel@tonic-gate #define	LENGTH	1	/* l */
820Sstevel@tonic-gate #define	FPLUS	2	/* + */
830Sstevel@tonic-gate #define	FMINUS	  4	/* - */
840Sstevel@tonic-gate #define	FBLANK	  8	/* blank */
850Sstevel@tonic-gate #define	FSHARP	 16	/* # */
860Sstevel@tonic-gate #define	PADZERO  32	/* padding zeroes requested via '0' */
870Sstevel@tonic-gate #define	DOTSEEN  64	/* dot appeared in format specification */
880Sstevel@tonic-gate #define	SUFFIX	128	/* a suffix is to appear in the output */
890Sstevel@tonic-gate #define	RZERO	256	/* there will be trailing zeros in output */
900Sstevel@tonic-gate #define	LZERO	512	/* there will be leading zeroes in output */
910Sstevel@tonic-gate #define	SHORT  1024	/* h */
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  *	Positional Parameter information
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate #define	MAXARGS	30	/* max. number of args for fast positional paramters */
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate  * stva_list is used to subvert C's restriction that a variable with an
1000Sstevel@tonic-gate  * array type can not appear on the left hand side of an assignment operator.
1010Sstevel@tonic-gate  * By putting the array inside a structure, the functionality of assigning to
1020Sstevel@tonic-gate  * the whole array through a simple assignment is achieved..
1030Sstevel@tonic-gate  */
1040Sstevel@tonic-gate typedef struct stva_list {
1050Sstevel@tonic-gate 	va_list	ap;
1060Sstevel@tonic-gate } stva_list;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate static char _blanks[] = "                    ";
1090Sstevel@tonic-gate static char _zeroes[] = "00000000000000000000";
1100Sstevel@tonic-gate static char uc_digs[] = "0123456789ABCDEF";
1110Sstevel@tonic-gate static char lc_digs[] = "0123456789abcdef";
1120Sstevel@tonic-gate static char  lc_nan[] = "nan0x";
1130Sstevel@tonic-gate static char  uc_nan[] = "NAN0X";
1140Sstevel@tonic-gate static char  lc_inf[] = "inf";
1150Sstevel@tonic-gate static char  uc_inf[] = "INF";
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate /*
1180Sstevel@tonic-gate  * forward declarations
1190Sstevel@tonic-gate  */
1200Sstevel@tonic-gate void _mkarglst(char *, stva_list, stva_list []);
1210Sstevel@tonic-gate void _getarg(char *, stva_list *, int);
1220Sstevel@tonic-gate static int _lowdigit(long *);
1230Sstevel@tonic-gate static void _dowrite(char *, ssize_t, FILE *, unsigned char **);
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate static int
_lowdigit(long * valptr)1260Sstevel@tonic-gate _lowdigit(long *valptr)
1270Sstevel@tonic-gate {	/* This function computes the decimal low-order digit of the number */
1280Sstevel@tonic-gate 	/* pointed to by valptr, and returns this digit after dividing   */
1290Sstevel@tonic-gate 	/* *valptr by ten.  This function is called ONLY to compute the */
1300Sstevel@tonic-gate 	/* low-order digit of a long whose high-order bit is set. */
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	int lowbit = (int)(*valptr & 1);
1330Sstevel@tonic-gate 	long value = (*valptr >> 1) & ~HIBITL;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	*valptr = value / 5;
1360Sstevel@tonic-gate 	return ((int)(value % 5 * 2 + lowbit + '0'));
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate /* The function _dowrite carries out buffer pointer bookkeeping surrounding */
1400Sstevel@tonic-gate /* a call to fwrite.  It is called only when the end of the file output */
1410Sstevel@tonic-gate /* buffer is approached or in other unusual situations. */
1420Sstevel@tonic-gate static void
_dowrite(char * p,ssize_t n,FILE * iop,unsigned char ** ptrptr)1430Sstevel@tonic-gate _dowrite(char *p, ssize_t n, FILE *iop, unsigned char **ptrptr)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate 	if (!(iop->_flag & _IOREAD)) {
1460Sstevel@tonic-gate 		iop->_cnt -= (*ptrptr - iop->_ptr);
1470Sstevel@tonic-gate 		iop->_ptr = *ptrptr;
1480Sstevel@tonic-gate 		_bufsync(iop, _bufend(iop));
1490Sstevel@tonic-gate 		(void) fwrite(p, 1, n, iop);
1500Sstevel@tonic-gate 		*ptrptr = iop->_ptr;
1510Sstevel@tonic-gate 	} else
1520Sstevel@tonic-gate 		*ptrptr = (unsigned char *) memcpy(*ptrptr, p, n) + n;
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate int
_doprnt(char * format,va_list in_args,FILE * iop)1560Sstevel@tonic-gate _doprnt(char *format, va_list in_args, FILE *iop)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	/* bufptr is used inside of doprnt instead of iop->_ptr; */
1600Sstevel@tonic-gate 	/* bufferend is a copy of _bufend(iop), if it exists.  For */
1610Sstevel@tonic-gate 	/* dummy file descriptors (iop->_flag & _IOREAD), bufferend */
1620Sstevel@tonic-gate 	/* may be meaningless. Dummy file descriptors are used so that */
1630Sstevel@tonic-gate 	/* sprintf and vsprintf may share the _doprnt routine with the */
1640Sstevel@tonic-gate 	/* rest of the printf family. */
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	unsigned char *bufptr;
1670Sstevel@tonic-gate 	unsigned char *bufferend;
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	/* This variable counts output characters. */
1700Sstevel@tonic-gate 	int	count = 0;
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	/* Starting and ending points for value to be printed */
1730Sstevel@tonic-gate 	char	*bp;
1740Sstevel@tonic-gate 	char	*p;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	/* Field width and precision */
1770Sstevel@tonic-gate 	int	width, prec;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	/* Format code */
1800Sstevel@tonic-gate 	int	fcode;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	/* Number of padding zeroes required on the left and right */
1830Sstevel@tonic-gate 	int	lzero, rzero;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	/* Flags - bit positions defined by LENGTH, FPLUS, FMINUS, FBLANK, */
1860Sstevel@tonic-gate 	/* and FSHARP are set if corresponding character is in format */
1870Sstevel@tonic-gate 	/* Bit position defined by PADZERO means extra space in the field */
1880Sstevel@tonic-gate 	/* should be padded with leading zeroes rather than with blanks */
1890Sstevel@tonic-gate 	int	flagword;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	/* Values are developed in this buffer */
1920Sstevel@tonic-gate 	char	buf[max(MAXDIGS, 1+max(MAXFCVT+MAXEXP, MAXECVT))];
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	/* Pointer to sign, "0x", "0X", or empty */
1950Sstevel@tonic-gate 	char	*prefix;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	/* Exponent or empty */
1980Sstevel@tonic-gate 	char	*suffix;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	/* Buffer to create exponent */
2010Sstevel@tonic-gate 	char	expbuf[MAXESIZ + 1];
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	/* Length of prefix and of suffix */
2040Sstevel@tonic-gate 	int	prefixlength, suffixlength;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	/* Combined length of leading zeroes, trailing zeroes, and suffix */
2070Sstevel@tonic-gate 	int 	otherlength;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	/* The value being converted, if integer */
2100Sstevel@tonic-gate 	long	val;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	/* The value being converted, if real */
2130Sstevel@tonic-gate 	double	dval;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	/* Output values from fcvt and ecvt */
2160Sstevel@tonic-gate 	int	decpt, sign;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	/* Pointer to a translate table for digits of whatever radix */
2190Sstevel@tonic-gate 	char	*tab;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	/* Work variables */
2220Sstevel@tonic-gate 	int	k, lradix, mradix;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	/* Variables used to flag an infinities and nans, resp. */
2250Sstevel@tonic-gate 	/* Nan_flg is used with two purposes: to flag a NaN and */
2260Sstevel@tonic-gate 	/* as the length of the string ``NAN0X'' (``nan0x'') */
2270Sstevel@tonic-gate 	int	 inf_nan = 0, NaN_flg = 0;
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	/* Pointer to string "NAN0X" or "nan0x" */
2300Sstevel@tonic-gate 	char	 *SNAN;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	/* Flag for negative infinity or NaN */
2330Sstevel@tonic-gate 	int neg_in = 0;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	/* variables for positional parameters */
2360Sstevel@tonic-gate 	char	*sformat = format;	/* save the beginning of the format */
2370Sstevel@tonic-gate 	int	fpos = 1;		/* 1 if first positional parameter */
2380Sstevel@tonic-gate 	stva_list args;		/* used to step through the argument list */
2390Sstevel@tonic-gate 	stva_list sargs;
2400Sstevel@tonic-gate 		/* used to save the start of the argument list */
2410Sstevel@tonic-gate 	stva_list bargs;
2420Sstevel@tonic-gate 		/* used to restore args if positional width or precision */
2430Sstevel@tonic-gate 	stva_list arglst[MAXARGS];
2440Sstevel@tonic-gate 		/*
2450Sstevel@tonic-gate 		 * array giving the appropriate values for va_arg() to
2460Sstevel@tonic-gate 		 * retrieve the corresponding argument:
2470Sstevel@tonic-gate 		 * arglst[0] is the first argument,
2480Sstevel@tonic-gate 		 * arglst[1] is the second argument, etc.
2490Sstevel@tonic-gate 		 */
2500Sstevel@tonic-gate 	int	starflg = 0;	/* set to 1 if * format specifier seen */
2510Sstevel@tonic-gate 	/*
2520Sstevel@tonic-gate 	 * Initialize args and sargs to the start of the argument list.
2530Sstevel@tonic-gate 	 * Note that ANSI guarantees that the address of the first member of
2540Sstevel@tonic-gate 	 * a structure will be the same as the address of the structure.
2550Sstevel@tonic-gate 	 * See equivalent code in libc doprnt.c
2560Sstevel@tonic-gate 	 */
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate #if !(defined(__amd64) && defined(__GNUC__))	/* XX64 - fix me */
2590Sstevel@tonic-gate 	va_copy(args.ap, in_args);
2600Sstevel@tonic-gate #endif
2610Sstevel@tonic-gate 	sargs = args;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	/* if first I/O to the stream get a buffer */
2640Sstevel@tonic-gate 	/* Note that iop->_base should not equal 0 for sprintf and vsprintf */
2650Sstevel@tonic-gate 	if (iop->_base == 0 && _findbuf(iop) == 0)
2660Sstevel@tonic-gate 		return (EOF);
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	/* initialize buffer pointer and buffer end pointer */
2690Sstevel@tonic-gate 	bufptr = iop->_ptr;
2700Sstevel@tonic-gate 	bufferend = (iop->_flag & _IOREAD) ?
271*6812Sraf 	    (unsigned char *)((long)bufptr | (-1L & ~HIBITL))
272*6812Sraf 	    : _bufend(iop);
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	/*
2750Sstevel@tonic-gate 	 *	The main loop -- this loop goes through one iteration
2760Sstevel@tonic-gate 	 *	for each string of ordinary characters or format specification.
2770Sstevel@tonic-gate 	 */
2780Sstevel@tonic-gate 	for (;;) {
2790Sstevel@tonic-gate 		ptrdiff_t pdiff;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 		if ((fcode = *format) != '\0' && fcode != '%') {
2820Sstevel@tonic-gate 			bp = format;
2830Sstevel@tonic-gate 			do {
2840Sstevel@tonic-gate 				format++;
2850Sstevel@tonic-gate 			} while ((fcode = *format) != '\0' && fcode != '%');
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 			pdiff = format - bp;
2880Sstevel@tonic-gate 				/* pdiff = no. of non-% chars */
2890Sstevel@tonic-gate 			count += pdiff;
2900Sstevel@tonic-gate 			PUT(bp, pdiff);
2910Sstevel@tonic-gate 		}
2920Sstevel@tonic-gate 		if (fcode == '\0') {  /* end of format; return */
2930Sstevel@tonic-gate 			ptrdiff_t d = bufptr - iop->_ptr;
2940Sstevel@tonic-gate 			iop->_cnt -= d;
2950Sstevel@tonic-gate 			iop->_ptr = bufptr;
2960Sstevel@tonic-gate 			if (bufptr + iop->_cnt > bufferend &&
2970Sstevel@tonic-gate 			    !(iop->_flag & _IOREAD))
2980Sstevel@tonic-gate 				_bufsync(iop, bufferend);
2990Sstevel@tonic-gate 				/*
3000Sstevel@tonic-gate 				 * in case of interrupt during last
3010Sstevel@tonic-gate 				 * several lines
3020Sstevel@tonic-gate 				 */
3030Sstevel@tonic-gate 			if (iop->_flag & (_IONBF | _IOLBF) &&
3040Sstevel@tonic-gate 			    (iop->_flag & _IONBF ||
3050Sstevel@tonic-gate 			    memchr((char *)(bufptr-count), '\n', count) !=
3060Sstevel@tonic-gate 			    NULL))
3070Sstevel@tonic-gate 				(void) _xflsbuf(iop);
3080Sstevel@tonic-gate 			return (ferror(iop) ? EOF : count);
3090Sstevel@tonic-gate 		}
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 		/*
3120Sstevel@tonic-gate 		 *	% has been found.
3130Sstevel@tonic-gate 		 *	The following switch is used to parse the format
3140Sstevel@tonic-gate 		 *	specification and to perform the operation specified
3150Sstevel@tonic-gate 		 *	by the format letter.  The program repeatedly goes
3160Sstevel@tonic-gate 		 *	back to this switch until the format letter is
3170Sstevel@tonic-gate 		 *	encountered.
3180Sstevel@tonic-gate 		 */
3190Sstevel@tonic-gate 		width = prefixlength = otherlength = flagword =
320*6812Sraf 		    suffixlength = 0;
3210Sstevel@tonic-gate 		format++;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	charswitch:
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 		switch (fcode = *format++) {
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 		case '+':
3280Sstevel@tonic-gate 			flagword |= FPLUS;
3290Sstevel@tonic-gate 			goto charswitch;
3300Sstevel@tonic-gate 		case '-':
3310Sstevel@tonic-gate 			flagword |= FMINUS;
3320Sstevel@tonic-gate 			flagword &= ~PADZERO; /* ignore 0 flag */
3330Sstevel@tonic-gate 			goto charswitch;
3340Sstevel@tonic-gate 		case ' ':
3350Sstevel@tonic-gate 			flagword |= FBLANK;
3360Sstevel@tonic-gate 			goto charswitch;
3370Sstevel@tonic-gate 		case '#':
3380Sstevel@tonic-gate 			flagword |= FSHARP;
3390Sstevel@tonic-gate 			goto charswitch;
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 		/* Scan the field width and precision */
3420Sstevel@tonic-gate 		case '.':
3430Sstevel@tonic-gate 			flagword |= DOTSEEN;
3440Sstevel@tonic-gate 			prec = 0;
3450Sstevel@tonic-gate 			goto charswitch;
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 		case '*':
3480Sstevel@tonic-gate 			if (isdigit(*format)) {
3490Sstevel@tonic-gate 				starflg = 1;
3500Sstevel@tonic-gate 				bargs = args;
3510Sstevel@tonic-gate 				goto charswitch;
3520Sstevel@tonic-gate 			}
3530Sstevel@tonic-gate 			if (!(flagword & DOTSEEN)) {
3540Sstevel@tonic-gate 				width = va_arg(args.ap, int);
3550Sstevel@tonic-gate 				if (width < 0) {
3560Sstevel@tonic-gate 					width = -width;
3570Sstevel@tonic-gate 					flagword ^= FMINUS;
3580Sstevel@tonic-gate 				}
3590Sstevel@tonic-gate 			} else {
3600Sstevel@tonic-gate 				prec = va_arg(args.ap, int);
3610Sstevel@tonic-gate 				if (prec < 0)
3620Sstevel@tonic-gate 					prec = 0;
3630Sstevel@tonic-gate 			}
3640Sstevel@tonic-gate 			goto charswitch;
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 		case '$':
3670Sstevel@tonic-gate 			{
3680Sstevel@tonic-gate 			int		position;
3690Sstevel@tonic-gate 			stva_list	targs;
3700Sstevel@tonic-gate 			if (fpos) {
3710Sstevel@tonic-gate 				_mkarglst(sformat, sargs, arglst);
3720Sstevel@tonic-gate 				fpos = 0;
3730Sstevel@tonic-gate 			}
3740Sstevel@tonic-gate 			if (flagword & DOTSEEN) {
3750Sstevel@tonic-gate 				position = prec;
3760Sstevel@tonic-gate 				prec = 0;
3770Sstevel@tonic-gate 			} else {
3780Sstevel@tonic-gate 				position = width;
3790Sstevel@tonic-gate 				width = 0;
3800Sstevel@tonic-gate 			}
3810Sstevel@tonic-gate 			if (position <= 0) {
3820Sstevel@tonic-gate 				/* illegal position */
3830Sstevel@tonic-gate 				format--;
3840Sstevel@tonic-gate 				continue;
3850Sstevel@tonic-gate 			}
3860Sstevel@tonic-gate 			if (position <= MAXARGS) {
3870Sstevel@tonic-gate 				targs = arglst[position - 1];
3880Sstevel@tonic-gate 			} else {
3890Sstevel@tonic-gate 				targs = arglst[MAXARGS - 1];
3900Sstevel@tonic-gate 				_getarg(sformat, &targs, position);
3910Sstevel@tonic-gate 			}
3920Sstevel@tonic-gate 			if (!starflg)
3930Sstevel@tonic-gate 				args = targs;
3940Sstevel@tonic-gate 			else {
3950Sstevel@tonic-gate 				starflg = 0;
3960Sstevel@tonic-gate 				args = bargs;
3970Sstevel@tonic-gate 				if (flagword & DOTSEEN)
3980Sstevel@tonic-gate 					prec = va_arg(targs.ap, int);
3990Sstevel@tonic-gate 				else
4000Sstevel@tonic-gate 					width = va_arg(targs.ap, int);
4010Sstevel@tonic-gate 			}
4020Sstevel@tonic-gate 			goto charswitch;
4030Sstevel@tonic-gate 			}
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 		case '0':	/* obsolescent spec:  leading zero in width */
4060Sstevel@tonic-gate 				/* means pad with leading zeros */
4070Sstevel@tonic-gate 			if (!(flagword & (DOTSEEN | FMINUS)))
4080Sstevel@tonic-gate 				flagword |= PADZERO;
4090Sstevel@tonic-gate 			/* FALLTHROUGH */
4100Sstevel@tonic-gate 		case '1':
4110Sstevel@tonic-gate 		case '2':
4120Sstevel@tonic-gate 		case '3':
4130Sstevel@tonic-gate 		case '4':
4140Sstevel@tonic-gate 		case '5':
4150Sstevel@tonic-gate 		case '6':
4160Sstevel@tonic-gate 		case '7':
4170Sstevel@tonic-gate 		case '8':
4180Sstevel@tonic-gate 		case '9':
419*6812Sraf 			{
420*6812Sraf 				int num = fcode - '0';
4210Sstevel@tonic-gate 				while (isdigit(fcode = *format)) {
4220Sstevel@tonic-gate 					num = num * 10 + fcode - '0';
4230Sstevel@tonic-gate 					format++;
4240Sstevel@tonic-gate 				}
4250Sstevel@tonic-gate 				if (flagword & DOTSEEN)
4260Sstevel@tonic-gate 					prec = num;
4270Sstevel@tonic-gate 				else
4280Sstevel@tonic-gate 					width = num;
4290Sstevel@tonic-gate 				goto charswitch;
4300Sstevel@tonic-gate 			}
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 		/* Scan the length modifier */
4330Sstevel@tonic-gate 		case 'l':
4340Sstevel@tonic-gate 			flagword |= LENGTH;
4350Sstevel@tonic-gate 			goto charswitch;
4360Sstevel@tonic-gate 		case 'h':
4370Sstevel@tonic-gate 			flagword |= SHORT;
4380Sstevel@tonic-gate 			goto charswitch;
4390Sstevel@tonic-gate 		case 'L':
4400Sstevel@tonic-gate 			goto charswitch;
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 		/*
4430Sstevel@tonic-gate 		 *	The character addressed by format must be
4440Sstevel@tonic-gate 		 *	the format letter -- there is nothing
4450Sstevel@tonic-gate 		 *	left for it to be.
4460Sstevel@tonic-gate 		 *
4470Sstevel@tonic-gate 		 *	The status of the +, -, #, and blank
4480Sstevel@tonic-gate 		 *	flags are reflected in the variable
4490Sstevel@tonic-gate 		 *	"flagword".  "width" and "prec" contain
4500Sstevel@tonic-gate 		 *	numbers corresponding to the digit
4510Sstevel@tonic-gate 		 *	strings before and after the decimal
4520Sstevel@tonic-gate 		 *	point, respectively. If there was no
4530Sstevel@tonic-gate 		 *	decimal point, then flagword & DOTSEEN
4540Sstevel@tonic-gate 		 *	is false and the value of prec is meaningless.
4550Sstevel@tonic-gate 		 *
4560Sstevel@tonic-gate 		 *	The following switch cases set things up
4570Sstevel@tonic-gate 		 *	for printing.  What ultimately gets
4580Sstevel@tonic-gate 		 *	printed will be padding blanks, a
4590Sstevel@tonic-gate 		 *	prefix, left padding zeroes, a value,
4600Sstevel@tonic-gate 		 *	right padding zeroes, a suffix, and
4610Sstevel@tonic-gate 		 *	more padding blanks.  Padding blanks
4620Sstevel@tonic-gate 		 *	will not appear simultaneously on both
4630Sstevel@tonic-gate 		 *	the left and the right.  Each case in
4640Sstevel@tonic-gate 		 *	this switch will compute the value, and
4650Sstevel@tonic-gate 		 *	leave in several variables the informa-
4660Sstevel@tonic-gate 		 *	tion necessary to construct what is to
4670Sstevel@tonic-gate 		 *	be printed.
4680Sstevel@tonic-gate 		 *
4690Sstevel@tonic-gate 		 *	The prefix is a sign, a blank, "0x",
4700Sstevel@tonic-gate 		 *	"0X", or null, and is addressed by
4710Sstevel@tonic-gate 		 *	"prefix".
4720Sstevel@tonic-gate 		 *
4730Sstevel@tonic-gate 		 *	The suffix is either null or an
4740Sstevel@tonic-gate 		 *	exponent, and is addressed by "suffix".
4750Sstevel@tonic-gate 		 *	If there is a suffix, the flagword bit
4760Sstevel@tonic-gate 		 *	SUFFIX will be set.
4770Sstevel@tonic-gate 		 *
4780Sstevel@tonic-gate 		 *	The value to be printed starts at "bp"
4790Sstevel@tonic-gate 		 *	and continues up to and not including
4800Sstevel@tonic-gate 		 *	"p".
4810Sstevel@tonic-gate 		 *
4820Sstevel@tonic-gate 		 *	"lzero" and "rzero" will contain the
4830Sstevel@tonic-gate 		 *	number of padding zeroes required on
4840Sstevel@tonic-gate 		 *	the left and right, respectively.
4850Sstevel@tonic-gate 		 *	The flagword bits LZERO and RZERO tell
4860Sstevel@tonic-gate 		 *	whether padding zeros are required.
4870Sstevel@tonic-gate 		 *
4880Sstevel@tonic-gate 		 *	The number of padding blanks, and
4890Sstevel@tonic-gate 		 *	whether they go on the left or the
4900Sstevel@tonic-gate 		 *	right, will be computed on exit from
4910Sstevel@tonic-gate 		 *	the switch.
4920Sstevel@tonic-gate 		 */
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 		/*
4980Sstevel@tonic-gate 		 *	decimal fixed point representations
4990Sstevel@tonic-gate 		 *
5000Sstevel@tonic-gate 		 *	HIBITL is 100...000
5010Sstevel@tonic-gate 		 *	binary, and is equal to	the maximum
5020Sstevel@tonic-gate 		 *	negative number.
5030Sstevel@tonic-gate 		 *	We assume a 2's complement machine
5040Sstevel@tonic-gate 		 */
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 		case 'i':
5070Sstevel@tonic-gate 		case 'd':
5080Sstevel@tonic-gate 			/* Fetch the argument to be printed */
5090Sstevel@tonic-gate 			if (flagword & LENGTH)
5100Sstevel@tonic-gate 				val = va_arg(args.ap, long);
5110Sstevel@tonic-gate 			else
5120Sstevel@tonic-gate 				val = va_arg(args.ap, int);
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 			if (flagword & SHORT)
5150Sstevel@tonic-gate 				val = (short)val;
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 			/* Set buffer pointer to last digit */
5180Sstevel@tonic-gate 			p = bp = buf + MAXDIGS;
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 			/* If signed conversion, make sign */
5210Sstevel@tonic-gate 			if (val < 0) {
5220Sstevel@tonic-gate 				prefix = "-";
5230Sstevel@tonic-gate 				prefixlength = 1;
5240Sstevel@tonic-gate 				/*
5250Sstevel@tonic-gate 				 * Negate, checking in
5260Sstevel@tonic-gate 				 * advance for possible
5270Sstevel@tonic-gate 				 * overflow.
5280Sstevel@tonic-gate 				 */
5290Sstevel@tonic-gate 				if (val != HIBITL)
5300Sstevel@tonic-gate 					val = -val;
5310Sstevel@tonic-gate 				else	/* number is -HIBITL; convert last */
5320Sstevel@tonic-gate 					/* digit now and get positive number */
5330Sstevel@tonic-gate 					*--bp = _lowdigit(&val);
5340Sstevel@tonic-gate 			} else if (flagword & FPLUS) {
5350Sstevel@tonic-gate 				prefix = "+";
5360Sstevel@tonic-gate 				prefixlength = 1;
5370Sstevel@tonic-gate 			} else if (flagword & FBLANK) {
5380Sstevel@tonic-gate 				prefix = " ";
5390Sstevel@tonic-gate 				prefixlength = 1;
5400Sstevel@tonic-gate 			}
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 		decimal:
543*6812Sraf 			{
544*6812Sraf 				long qval = val;
5450Sstevel@tonic-gate 				long saveq;
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 				if (qval <= 9) {
5480Sstevel@tonic-gate 					if (qval != 0 || !(flagword & DOTSEEN))
5490Sstevel@tonic-gate 						*--bp = (char)(qval + '0');
5500Sstevel@tonic-gate 				} else {
5510Sstevel@tonic-gate 					do {
5520Sstevel@tonic-gate 						saveq = qval;
5530Sstevel@tonic-gate 						qval /= 10;
5540Sstevel@tonic-gate 						*--bp = (char)(saveq -
555*6812Sraf 						    qval * 10 + '0');
5560Sstevel@tonic-gate 					} while (qval > 9);
5570Sstevel@tonic-gate 					*--bp = (char)(qval + '0');
5580Sstevel@tonic-gate 					pdiff = (ptrdiff_t)saveq;
5590Sstevel@tonic-gate 				}
5600Sstevel@tonic-gate 			}
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 			/* Calculate minimum padding zero requirement */
5630Sstevel@tonic-gate 			if (flagword & DOTSEEN) {
5640Sstevel@tonic-gate 				int leadzeroes = prec - (int)(p - bp);
5650Sstevel@tonic-gate 				if (leadzeroes > 0) {
5660Sstevel@tonic-gate 					otherlength = lzero = leadzeroes;
5670Sstevel@tonic-gate 					flagword |= LZERO;
5680Sstevel@tonic-gate 				}
5690Sstevel@tonic-gate 			}
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 			break;
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 		case 'u':
5740Sstevel@tonic-gate 			/* Fetch the argument to be printed */
5750Sstevel@tonic-gate 			if (flagword & LENGTH)
5760Sstevel@tonic-gate 				val = va_arg(args.ap, long);
5770Sstevel@tonic-gate 			else
5780Sstevel@tonic-gate 				val = va_arg(args.ap, unsigned);
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 			if (flagword & SHORT)
5810Sstevel@tonic-gate 				val = (unsigned short)val;
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 			p = bp = buf + MAXDIGS;
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 			if (val & HIBITL)
5860Sstevel@tonic-gate 				*--bp = _lowdigit(&val);
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 			goto decimal;
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 		/*
5910Sstevel@tonic-gate 		 *	non-decimal fixed point representations
5920Sstevel@tonic-gate 		 *	for radix equal to a power of two
5930Sstevel@tonic-gate 		 *
5940Sstevel@tonic-gate 		 *	"mradix" is one less than the radix for the conversion.
5950Sstevel@tonic-gate 		 *	"lradix" is one less than the base 2 log
5960Sstevel@tonic-gate 		 *	of the radix for the conversion. Conversion is unsigned.
5970Sstevel@tonic-gate 		 *	HIBITL is 100...000
5980Sstevel@tonic-gate 		 *	binary, and is equal to	the maximum
5990Sstevel@tonic-gate 		 *	negative number.
6000Sstevel@tonic-gate 		 *	We assume a 2's complement machine
6010Sstevel@tonic-gate 		 */
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 		case 'o':
6040Sstevel@tonic-gate 			mradix = 7;
6050Sstevel@tonic-gate 			lradix = 2;
6060Sstevel@tonic-gate 			goto fixed;
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 		case 'X':
6090Sstevel@tonic-gate 		case 'x':
6100Sstevel@tonic-gate 		case 'p':
6110Sstevel@tonic-gate 			mradix = 15;
6120Sstevel@tonic-gate 			lradix = 3;
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 		fixed:
6150Sstevel@tonic-gate 			/* Fetch the argument to be printed */
6160Sstevel@tonic-gate 			if (flagword & LENGTH)
6170Sstevel@tonic-gate 				val = va_arg(args.ap, long);
6180Sstevel@tonic-gate 			else
6190Sstevel@tonic-gate 				val = va_arg(args.ap, unsigned);
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 			if (flagword & SHORT)
6220Sstevel@tonic-gate 				val = (unsigned short)val;
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 			/* Set translate table for digits */
6250Sstevel@tonic-gate 			tab = (fcode == 'X') ? uc_digs : lc_digs;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 			/* Entry point when printing a double which is a NaN */
6280Sstevel@tonic-gate 		put_pc:
6290Sstevel@tonic-gate 			/* Develop the digits of the value */
6300Sstevel@tonic-gate 			p = bp = buf + MAXDIGS;
631*6812Sraf 			{
632*6812Sraf 				long qval = val;
6330Sstevel@tonic-gate 				if (qval == 0) {
6340Sstevel@tonic-gate 					if (!(flagword & DOTSEEN)) {
6350Sstevel@tonic-gate 						otherlength = lzero = 1;
6360Sstevel@tonic-gate 						flagword |= LZERO;
6370Sstevel@tonic-gate 					}
6380Sstevel@tonic-gate 				} else
6390Sstevel@tonic-gate 					do {
6400Sstevel@tonic-gate 						*--bp = tab[qval & mradix];
6410Sstevel@tonic-gate 						qval = ((qval >> 1) & ~HIBITL)
642*6812Sraf 						    >> lradix;
6430Sstevel@tonic-gate 					} while (qval != 0);
6440Sstevel@tonic-gate 			}
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 			/* Calculate minimum padding zero requirement */
6470Sstevel@tonic-gate 			if (flagword & DOTSEEN) {
6480Sstevel@tonic-gate 				int leadzeroes = prec - (int)(p - bp);
6490Sstevel@tonic-gate 				if (leadzeroes > 0) {
6500Sstevel@tonic-gate 					otherlength = lzero = leadzeroes;
6510Sstevel@tonic-gate 					flagword |= LZERO;
6520Sstevel@tonic-gate 				}
6530Sstevel@tonic-gate 			}
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 			/* Handle the # flag */
6560Sstevel@tonic-gate 			if (flagword & FSHARP && val != 0)
6570Sstevel@tonic-gate 				switch (fcode) {
6580Sstevel@tonic-gate 				case 'o':
6590Sstevel@tonic-gate 					if (!(flagword & LZERO)) {
6600Sstevel@tonic-gate 						otherlength = lzero = 1;
6610Sstevel@tonic-gate 						flagword |= LZERO;
6620Sstevel@tonic-gate 					}
6630Sstevel@tonic-gate 					break;
6640Sstevel@tonic-gate 				case 'x':
6650Sstevel@tonic-gate 					prefix = "0x";
6660Sstevel@tonic-gate 					prefixlength = 2;
6670Sstevel@tonic-gate 					break;
6680Sstevel@tonic-gate 				case 'X':
6690Sstevel@tonic-gate 					prefix = "0X";
6700Sstevel@tonic-gate 					prefixlength = 2;
6710Sstevel@tonic-gate 					break;
6720Sstevel@tonic-gate 				}
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 			break;
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 		case 'E':
6770Sstevel@tonic-gate 		case 'e':
6780Sstevel@tonic-gate 			/*
6790Sstevel@tonic-gate 			 * E-format.  The general strategy
6800Sstevel@tonic-gate 			 * here is fairly easy: we take
6810Sstevel@tonic-gate 			 * what ecvt gives us and re-format it.
6820Sstevel@tonic-gate 			 */
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 			/* Establish default precision */
6850Sstevel@tonic-gate 			if (!(flagword & DOTSEEN))
6860Sstevel@tonic-gate 				prec = 6;
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 			/* Fetch the value */
6890Sstevel@tonic-gate 			dval = va_arg(args.ap, double);
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 			/* Check for NaNs and Infinities */
6920Sstevel@tonic-gate 			if (IsNANorINF(dval)) {
6930Sstevel@tonic-gate 				if (IsINF(dval)) {
6940Sstevel@tonic-gate 					if (IsNegNAN(dval))
6950Sstevel@tonic-gate 						neg_in = 1;
6960Sstevel@tonic-gate 					inf_nan = 1;
6970Sstevel@tonic-gate 					bp = (fcode == 'E')? uc_inf: lc_inf;
6980Sstevel@tonic-gate 					p = bp + 3;
6990Sstevel@tonic-gate 					break;
7000Sstevel@tonic-gate 				} else {
7010Sstevel@tonic-gate 					if (IsNegNAN(dval))
7020Sstevel@tonic-gate 						neg_in = 1;
7030Sstevel@tonic-gate 					inf_nan = 1;
7040Sstevel@tonic-gate 					val = GETNaNPC(dval);
7050Sstevel@tonic-gate 					NaN_flg = SNLEN;
7060Sstevel@tonic-gate 					mradix = 15;
7070Sstevel@tonic-gate 					lradix = 3;
7080Sstevel@tonic-gate 					if (fcode == 'E') {
7090Sstevel@tonic-gate 						SNAN = uc_nan;
7100Sstevel@tonic-gate 						tab =  uc_digs;
7110Sstevel@tonic-gate 					} else {
7120Sstevel@tonic-gate 						SNAN =  lc_nan;
7130Sstevel@tonic-gate 						tab =  lc_digs;
7140Sstevel@tonic-gate 					}
7150Sstevel@tonic-gate 					goto put_pc;
7160Sstevel@tonic-gate 				}
7170Sstevel@tonic-gate 			}
7180Sstevel@tonic-gate 			/* Develop the mantissa */
7190Sstevel@tonic-gate 			bp = ecvt(dval, min(prec + 1, MAXECVT), &decpt, &sign);
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 			/* Determine the prefix */
7220Sstevel@tonic-gate 		e_merge:
7230Sstevel@tonic-gate 			if (sign) {
7240Sstevel@tonic-gate 				prefix = "-";
7250Sstevel@tonic-gate 				prefixlength = 1;
7260Sstevel@tonic-gate 			} else if (flagword & FPLUS) {
7270Sstevel@tonic-gate 				prefix = "+";
7280Sstevel@tonic-gate 				prefixlength = 1;
7290Sstevel@tonic-gate 			} else if (flagword & FBLANK) {
7300Sstevel@tonic-gate 				prefix = " ";
7310Sstevel@tonic-gate 				prefixlength = 1;
7320Sstevel@tonic-gate 			}
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 			/* Place the first digit in the buffer */
7350Sstevel@tonic-gate 			p = &buf[0];
7360Sstevel@tonic-gate 			*p++ = (*bp != '\0') ? *bp++ : '0';
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 			/* Put in a decimal point if needed */
7390Sstevel@tonic-gate 			if (prec != 0 || (flagword & FSHARP))
7400Sstevel@tonic-gate 				*p++ = _numeric[0];
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 			/* Create the rest of the mantissa */
743*6812Sraf 			{
744*6812Sraf 				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)
760*6812Sraf 					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 =
778*6812Sraf 			    (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];
841*6812Sraf 			{
842*6812Sraf 				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' ||
848*6812Sraf 					    k >= MAXFSIG) ?
849*6812Sraf 					    '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' ||
864*6812Sraf 					    k >= MAXFSIG) ?
865*6812Sraf 					    '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;
920*6812Sraf 			{
921*6812Sraf 				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 +
1005*6812Sraf 		    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
_mkarglst(char * fmt,stva_list args,stva_list arglst[])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
_getarg(char * fmt,stva_list * pargs,int argno)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