17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7257d1b4Sraf * Common Development and Distribution License (the "License").
6*7257d1b4Sraf * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21e8031f0aSraf
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate * The Regents of the University of California
337c478bd9Sstevel@tonic-gate * All Rights Reserved
347c478bd9Sstevel@tonic-gate *
357c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate * contributors.
387c478bd9Sstevel@tonic-gate */
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate * _doprnt: common code for printf, fprintf, sprintf
447c478bd9Sstevel@tonic-gate */
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate #include <sys/types.h>
477c478bd9Sstevel@tonic-gate #include "file64.h"
487c478bd9Sstevel@tonic-gate #include <stdio.h>
497c478bd9Sstevel@tonic-gate #include <stdlib.h>
507c478bd9Sstevel@tonic-gate #include <ctype.h>
517c478bd9Sstevel@tonic-gate #include <stdarg.h>
527c478bd9Sstevel@tonic-gate #include <values.h>
537c478bd9Sstevel@tonic-gate #include <nan.h>
547c478bd9Sstevel@tonic-gate #include <memory.h>
557c478bd9Sstevel@tonic-gate #include <string.h>
567c478bd9Sstevel@tonic-gate #include "print.h" /* parameters & macros for doprnt */
577c478bd9Sstevel@tonic-gate #include "stdiom.h"
587c478bd9Sstevel@tonic-gate #include <locale.h>
597c478bd9Sstevel@tonic-gate #include <stddef.h>
607c478bd9Sstevel@tonic-gate #include "_locale.h"
617c478bd9Sstevel@tonic-gate #include "libc.h"
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate #define PUT(p, n) { unsigned char *newbufptr; \
647c478bd9Sstevel@tonic-gate if ((newbufptr = bufptr + (n)) > bufferend) { \
657c478bd9Sstevel@tonic-gate _dowrite((p), (n), iop, &bufptr); \
667c478bd9Sstevel@tonic-gate } else { \
677c478bd9Sstevel@tonic-gate (void) memcpy(bufptr, (p), (n)); \
687c478bd9Sstevel@tonic-gate bufptr = newbufptr; \
697c478bd9Sstevel@tonic-gate } \
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate #define PAD(s, n) { int nn; \
727c478bd9Sstevel@tonic-gate for (nn = (n); nn > 20; nn -= 20) \
737c478bd9Sstevel@tonic-gate _dowrite((s), 20, iop, &bufptr); \
747c478bd9Sstevel@tonic-gate PUT((s), nn); \
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate #define SNLEN 5 /* Length of string used when printing a NaN */
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate /* bit positions for flags used in doprnt */
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate #define LENGTH 1 /* l */
827c478bd9Sstevel@tonic-gate #define FPLUS 2 /* + */
837c478bd9Sstevel@tonic-gate #define FMINUS 4 /* - */
847c478bd9Sstevel@tonic-gate #define FBLANK 8 /* blank */
857c478bd9Sstevel@tonic-gate #define FSHARP 16 /* # */
867c478bd9Sstevel@tonic-gate #define PADZERO 32 /* padding zeroes requested via '0' */
877c478bd9Sstevel@tonic-gate #define DOTSEEN 64 /* dot appeared in format specification */
887c478bd9Sstevel@tonic-gate #define SUFFIX 128 /* a suffix is to appear in the output */
897c478bd9Sstevel@tonic-gate #define RZERO 256 /* there will be trailing zeros in output */
907c478bd9Sstevel@tonic-gate #define LZERO 512 /* there will be leading zeroes in output */
917c478bd9Sstevel@tonic-gate #define SHORT 1024 /* h */
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate * Positional Parameter information
957c478bd9Sstevel@tonic-gate */
967c478bd9Sstevel@tonic-gate #define MAXARGS 30 /* max. number of args for fast positional paramters */
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate * stva_list is used to subvert C's restriction that a variable with an
1007c478bd9Sstevel@tonic-gate * array type can not appear on the left hand side of an assignment operator.
1017c478bd9Sstevel@tonic-gate * By putting the array inside a structure, the functionality of assigning to
1027c478bd9Sstevel@tonic-gate * the whole array through a simple assignment is achieved..
1037c478bd9Sstevel@tonic-gate */
1047c478bd9Sstevel@tonic-gate typedef struct stva_list {
1057c478bd9Sstevel@tonic-gate va_list ap;
1067c478bd9Sstevel@tonic-gate } stva_list;
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate static char _blanks[] = " ";
1097c478bd9Sstevel@tonic-gate static char _zeroes[] = "00000000000000000000";
1107c478bd9Sstevel@tonic-gate static char uc_digs[] = "0123456789ABCDEF";
1117c478bd9Sstevel@tonic-gate static char lc_digs[] = "0123456789abcdef";
1127c478bd9Sstevel@tonic-gate static char lc_nan[] = "nan0x";
1137c478bd9Sstevel@tonic-gate static char uc_nan[] = "NAN0X";
1147c478bd9Sstevel@tonic-gate static char lc_inf[] = "inf";
1157c478bd9Sstevel@tonic-gate static char uc_inf[] = "INF";
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate /*
1187c478bd9Sstevel@tonic-gate * forward declarations
1197c478bd9Sstevel@tonic-gate */
1207c478bd9Sstevel@tonic-gate void _mkarglst(char *, stva_list, stva_list []);
1217c478bd9Sstevel@tonic-gate void _getarg(char *, stva_list *, int);
1227c478bd9Sstevel@tonic-gate static int _lowdigit(long *);
1237c478bd9Sstevel@tonic-gate static void _dowrite(char *, ssize_t, FILE *, unsigned char **);
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate static int
_lowdigit(long * valptr)1267c478bd9Sstevel@tonic-gate _lowdigit(long *valptr)
1277c478bd9Sstevel@tonic-gate { /* This function computes the decimal low-order digit of the number */
1287c478bd9Sstevel@tonic-gate /* pointed to by valptr, and returns this digit after dividing */
1297c478bd9Sstevel@tonic-gate /* *valptr by ten. This function is called ONLY to compute the */
1307c478bd9Sstevel@tonic-gate /* low-order digit of a long whose high-order bit is set. */
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate int lowbit = (int)(*valptr & 1);
1337c478bd9Sstevel@tonic-gate long value = (*valptr >> 1) & ~HIBITL;
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate *valptr = value / 5;
1367c478bd9Sstevel@tonic-gate return ((int)(value % 5 * 2 + lowbit + '0'));
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate /* The function _dowrite carries out buffer pointer bookkeeping surrounding */
1407c478bd9Sstevel@tonic-gate /* a call to fwrite. It is called only when the end of the file output */
1417c478bd9Sstevel@tonic-gate /* buffer is approached or in other unusual situations. */
1427c478bd9Sstevel@tonic-gate static void
_dowrite(char * p,ssize_t n,FILE * iop,unsigned char ** ptrptr)1437c478bd9Sstevel@tonic-gate _dowrite(char *p, ssize_t n, FILE *iop, unsigned char **ptrptr)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate if (!(iop->_flag & _IOREAD)) {
1467c478bd9Sstevel@tonic-gate iop->_cnt -= (*ptrptr - iop->_ptr);
1477c478bd9Sstevel@tonic-gate iop->_ptr = *ptrptr;
1487c478bd9Sstevel@tonic-gate _bufsync(iop, _bufend(iop));
1497c478bd9Sstevel@tonic-gate (void) fwrite(p, 1, n, iop);
1507c478bd9Sstevel@tonic-gate *ptrptr = iop->_ptr;
1517c478bd9Sstevel@tonic-gate } else
1527c478bd9Sstevel@tonic-gate *ptrptr = (unsigned char *) memcpy(*ptrptr, p, n) + n;
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate int
_doprnt(char * format,va_list in_args,FILE * iop)1567c478bd9Sstevel@tonic-gate _doprnt(char *format, va_list in_args, FILE *iop)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate /* bufptr is used inside of doprnt instead of iop->_ptr; */
1607c478bd9Sstevel@tonic-gate /* bufferend is a copy of _bufend(iop), if it exists. For */
1617c478bd9Sstevel@tonic-gate /* dummy file descriptors (iop->_flag & _IOREAD), bufferend */
1627c478bd9Sstevel@tonic-gate /* may be meaningless. Dummy file descriptors are used so that */
1637c478bd9Sstevel@tonic-gate /* sprintf and vsprintf may share the _doprnt routine with the */
1647c478bd9Sstevel@tonic-gate /* rest of the printf family. */
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate unsigned char *bufptr;
1677c478bd9Sstevel@tonic-gate unsigned char *bufferend;
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate /* This variable counts output characters. */
1707c478bd9Sstevel@tonic-gate int count = 0;
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate /* Starting and ending points for value to be printed */
1737c478bd9Sstevel@tonic-gate char *bp;
1747c478bd9Sstevel@tonic-gate char *p;
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate /* Field width and precision */
1777c478bd9Sstevel@tonic-gate int width, prec;
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate /* Format code */
1807c478bd9Sstevel@tonic-gate int fcode;
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate /* Number of padding zeroes required on the left and right */
1837c478bd9Sstevel@tonic-gate int lzero, rzero;
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate /* Flags - bit positions defined by LENGTH, FPLUS, FMINUS, FBLANK, */
1867c478bd9Sstevel@tonic-gate /* and FSHARP are set if corresponding character is in format */
1877c478bd9Sstevel@tonic-gate /* Bit position defined by PADZERO means extra space in the field */
1887c478bd9Sstevel@tonic-gate /* should be padded with leading zeroes rather than with blanks */
1897c478bd9Sstevel@tonic-gate int flagword;
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate /* Values are developed in this buffer */
1927c478bd9Sstevel@tonic-gate char buf[max(MAXDIGS, 1+max(MAXFCVT+MAXEXP, MAXECVT))];
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate /* Pointer to sign, "0x", "0X", or empty */
1957c478bd9Sstevel@tonic-gate char *prefix;
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate /* Exponent or empty */
1987c478bd9Sstevel@tonic-gate char *suffix;
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate /* Buffer to create exponent */
2017c478bd9Sstevel@tonic-gate char expbuf[MAXESIZ + 1];
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate /* Length of prefix and of suffix */
2047c478bd9Sstevel@tonic-gate int prefixlength, suffixlength;
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate /* Combined length of leading zeroes, trailing zeroes, and suffix */
2077c478bd9Sstevel@tonic-gate int otherlength;
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate /* The value being converted, if integer */
2107c478bd9Sstevel@tonic-gate long val;
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate /* The value being converted, if real */
2137c478bd9Sstevel@tonic-gate double dval;
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate /* Output values from fcvt and ecvt */
2167c478bd9Sstevel@tonic-gate int decpt, sign;
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate /* Pointer to a translate table for digits of whatever radix */
2197c478bd9Sstevel@tonic-gate char *tab;
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate /* Work variables */
2227c478bd9Sstevel@tonic-gate int k, lradix, mradix;
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate /* Variables used to flag an infinities and nans, resp. */
2257c478bd9Sstevel@tonic-gate /* Nan_flg is used with two purposes: to flag a NaN and */
2267c478bd9Sstevel@tonic-gate /* as the length of the string ``NAN0X'' (``nan0x'') */
2277c478bd9Sstevel@tonic-gate int inf_nan = 0, NaN_flg = 0;
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate /* Pointer to string "NAN0X" or "nan0x" */
2307c478bd9Sstevel@tonic-gate char *SNAN;
2317c478bd9Sstevel@tonic-gate
2327c478bd9Sstevel@tonic-gate /* Flag for negative infinity or NaN */
2337c478bd9Sstevel@tonic-gate int neg_in = 0;
2347c478bd9Sstevel@tonic-gate
2357c478bd9Sstevel@tonic-gate /* variables for positional parameters */
2367c478bd9Sstevel@tonic-gate char *sformat = format; /* save the beginning of the format */
2377c478bd9Sstevel@tonic-gate int fpos = 1; /* 1 if first positional parameter */
2387c478bd9Sstevel@tonic-gate stva_list args; /* used to step through the argument list */
2397c478bd9Sstevel@tonic-gate stva_list sargs;
2407c478bd9Sstevel@tonic-gate /* used to save the start of the argument list */
2417c478bd9Sstevel@tonic-gate stva_list bargs;
2427c478bd9Sstevel@tonic-gate /* used to restore args if positional width or precision */
2437c478bd9Sstevel@tonic-gate stva_list arglst[MAXARGS];
2447c478bd9Sstevel@tonic-gate /*
2457c478bd9Sstevel@tonic-gate * array giving the appropriate values for va_arg() to
2467c478bd9Sstevel@tonic-gate * retrieve the corresponding argument:
2477c478bd9Sstevel@tonic-gate * arglst[0] is the first argument,
2487c478bd9Sstevel@tonic-gate * arglst[1] is the second argument, etc.
2497c478bd9Sstevel@tonic-gate */
2507c478bd9Sstevel@tonic-gate int starflg = 0; /* set to 1 if * format specifier seen */
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate * Initialize args and sargs to the start of the argument list.
2537c478bd9Sstevel@tonic-gate * Note that ANSI guarantees that the address of the first member of
2547c478bd9Sstevel@tonic-gate * a structure will be the same as the address of the structure.
2557c478bd9Sstevel@tonic-gate * See equivalent code in libc doprnt.c
2567c478bd9Sstevel@tonic-gate */
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate #if !(defined(__amd64) && defined(__GNUC__)) /* XX64 - fix me */
2597c478bd9Sstevel@tonic-gate va_copy(args.ap, in_args);
2607c478bd9Sstevel@tonic-gate #endif
2617c478bd9Sstevel@tonic-gate sargs = args;
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate /* if first I/O to the stream get a buffer */
2647c478bd9Sstevel@tonic-gate /* Note that iop->_base should not equal 0 for sprintf and vsprintf */
2657c478bd9Sstevel@tonic-gate if (iop->_base == 0 && _findbuf(iop) == 0)
2667c478bd9Sstevel@tonic-gate return (EOF);
2677c478bd9Sstevel@tonic-gate
2687c478bd9Sstevel@tonic-gate /* initialize buffer pointer and buffer end pointer */
2697c478bd9Sstevel@tonic-gate bufptr = iop->_ptr;
2707c478bd9Sstevel@tonic-gate bufferend = (iop->_flag & _IOREAD) ?
2717c478bd9Sstevel@tonic-gate (unsigned char *)((long)bufptr | (-1L & ~HIBITL))
2727c478bd9Sstevel@tonic-gate : _bufend(iop);
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate /*
2757c478bd9Sstevel@tonic-gate * The main loop -- this loop goes through one iteration
2767c478bd9Sstevel@tonic-gate * for each string of ordinary characters or format specification.
2777c478bd9Sstevel@tonic-gate */
2787c478bd9Sstevel@tonic-gate for (;;) {
2797c478bd9Sstevel@tonic-gate ptrdiff_t pdiff;
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate if ((fcode = *format) != '\0' && fcode != '%') {
2827c478bd9Sstevel@tonic-gate bp = format;
2837c478bd9Sstevel@tonic-gate do {
2847c478bd9Sstevel@tonic-gate format++;
2857c478bd9Sstevel@tonic-gate } while ((fcode = *format) != '\0' && fcode != '%');
2867c478bd9Sstevel@tonic-gate
2877c478bd9Sstevel@tonic-gate pdiff = format - bp;
2887c478bd9Sstevel@tonic-gate /* pdiff = no. of non-% chars */
2897c478bd9Sstevel@tonic-gate count += pdiff;
2907c478bd9Sstevel@tonic-gate PUT(bp, pdiff);
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate if (fcode == '\0') { /* end of format; return */
2937c478bd9Sstevel@tonic-gate ptrdiff_t d = bufptr - iop->_ptr;
2947c478bd9Sstevel@tonic-gate iop->_cnt -= d;
2957c478bd9Sstevel@tonic-gate iop->_ptr = bufptr;
2967c478bd9Sstevel@tonic-gate if (bufptr + iop->_cnt > bufferend &&
2977c478bd9Sstevel@tonic-gate !(iop->_flag & _IOREAD))
2987c478bd9Sstevel@tonic-gate _bufsync(iop, bufferend);
2997c478bd9Sstevel@tonic-gate /*
3007c478bd9Sstevel@tonic-gate * in case of interrupt during last
3017c478bd9Sstevel@tonic-gate * several lines
3027c478bd9Sstevel@tonic-gate */
3037c478bd9Sstevel@tonic-gate if (iop->_flag & (_IONBF | _IOLBF) &&
3047c478bd9Sstevel@tonic-gate (iop->_flag & _IONBF ||
3057c478bd9Sstevel@tonic-gate memchr((char *)(bufptr-count), '\n', count) !=
3067c478bd9Sstevel@tonic-gate NULL))
3077c478bd9Sstevel@tonic-gate (void) _xflsbuf(iop);
3087c478bd9Sstevel@tonic-gate return (ferror(iop) ? EOF : count);
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate
3117c478bd9Sstevel@tonic-gate /*
3127c478bd9Sstevel@tonic-gate * % has been found.
3137c478bd9Sstevel@tonic-gate * The following switch is used to parse the format
3147c478bd9Sstevel@tonic-gate * specification and to perform the operation specified
3157c478bd9Sstevel@tonic-gate * by the format letter. The program repeatedly goes
3167c478bd9Sstevel@tonic-gate * back to this switch until the format letter is
3177c478bd9Sstevel@tonic-gate * encountered.
3187c478bd9Sstevel@tonic-gate */
3197c478bd9Sstevel@tonic-gate width = prefixlength = otherlength = flagword =
3207c478bd9Sstevel@tonic-gate suffixlength = 0;
3217c478bd9Sstevel@tonic-gate format++;
3227c478bd9Sstevel@tonic-gate
3237c478bd9Sstevel@tonic-gate charswitch:
3247c478bd9Sstevel@tonic-gate
3257c478bd9Sstevel@tonic-gate switch (fcode = *format++) {
3267c478bd9Sstevel@tonic-gate
3277c478bd9Sstevel@tonic-gate case '+':
3287c478bd9Sstevel@tonic-gate flagword |= FPLUS;
3297c478bd9Sstevel@tonic-gate goto charswitch;
3307c478bd9Sstevel@tonic-gate case '-':
3317c478bd9Sstevel@tonic-gate flagword |= FMINUS;
3327c478bd9Sstevel@tonic-gate flagword &= ~PADZERO; /* ignore 0 flag */
3337c478bd9Sstevel@tonic-gate goto charswitch;
3347c478bd9Sstevel@tonic-gate case ' ':
3357c478bd9Sstevel@tonic-gate flagword |= FBLANK;
3367c478bd9Sstevel@tonic-gate goto charswitch;
3377c478bd9Sstevel@tonic-gate case '#':
3387c478bd9Sstevel@tonic-gate flagword |= FSHARP;
3397c478bd9Sstevel@tonic-gate goto charswitch;
3407c478bd9Sstevel@tonic-gate
3417c478bd9Sstevel@tonic-gate /* Scan the field width and precision */
3427c478bd9Sstevel@tonic-gate case '.':
3437c478bd9Sstevel@tonic-gate flagword |= DOTSEEN;
3447c478bd9Sstevel@tonic-gate prec = 0;
3457c478bd9Sstevel@tonic-gate goto charswitch;
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate case '*':
3487c478bd9Sstevel@tonic-gate if (isdigit(*format)) {
3497c478bd9Sstevel@tonic-gate starflg = 1;
3507c478bd9Sstevel@tonic-gate bargs = args;
3517c478bd9Sstevel@tonic-gate goto charswitch;
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate if (!(flagword & DOTSEEN)) {
3547c478bd9Sstevel@tonic-gate width = va_arg(args.ap, int);
3557c478bd9Sstevel@tonic-gate if (width < 0) {
3567c478bd9Sstevel@tonic-gate width = -width;
3577c478bd9Sstevel@tonic-gate flagword ^= FMINUS;
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate } else {
3607c478bd9Sstevel@tonic-gate prec = va_arg(args.ap, int);
3617c478bd9Sstevel@tonic-gate if (prec < 0)
3627c478bd9Sstevel@tonic-gate prec = 0;
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate goto charswitch;
3657c478bd9Sstevel@tonic-gate
3667c478bd9Sstevel@tonic-gate case '$':
3677c478bd9Sstevel@tonic-gate {
3687c478bd9Sstevel@tonic-gate int position;
3697c478bd9Sstevel@tonic-gate stva_list targs;
3707c478bd9Sstevel@tonic-gate if (fpos) {
3717c478bd9Sstevel@tonic-gate _mkarglst(sformat, sargs, arglst);
3727c478bd9Sstevel@tonic-gate fpos = 0;
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate if (flagword & DOTSEEN) {
3757c478bd9Sstevel@tonic-gate position = prec;
3767c478bd9Sstevel@tonic-gate prec = 0;
3777c478bd9Sstevel@tonic-gate } else {
3787c478bd9Sstevel@tonic-gate position = width;
3797c478bd9Sstevel@tonic-gate width = 0;
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate if (position <= 0) {
3827c478bd9Sstevel@tonic-gate /* illegal position */
3837c478bd9Sstevel@tonic-gate format--;
3847c478bd9Sstevel@tonic-gate continue;
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate if (position <= MAXARGS) {
3877c478bd9Sstevel@tonic-gate targs = arglst[position - 1];
3887c478bd9Sstevel@tonic-gate } else {
3897c478bd9Sstevel@tonic-gate targs = arglst[MAXARGS - 1];
3907c478bd9Sstevel@tonic-gate _getarg(sformat, &targs, position);
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate if (!starflg)
3937c478bd9Sstevel@tonic-gate args = targs;
3947c478bd9Sstevel@tonic-gate else {
3957c478bd9Sstevel@tonic-gate starflg = 0;
3967c478bd9Sstevel@tonic-gate args = bargs;
3977c478bd9Sstevel@tonic-gate if (flagword & DOTSEEN)
3987c478bd9Sstevel@tonic-gate prec = va_arg(targs.ap, int);
3997c478bd9Sstevel@tonic-gate else
4007c478bd9Sstevel@tonic-gate width = va_arg(targs.ap, int);
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate goto charswitch;
4037c478bd9Sstevel@tonic-gate }
4047c478bd9Sstevel@tonic-gate
4057c478bd9Sstevel@tonic-gate case '0': /* obsolescent spec: leading zero in width */
4067c478bd9Sstevel@tonic-gate /* means pad with leading zeros */
4077c478bd9Sstevel@tonic-gate if (!(flagword & (DOTSEEN | FMINUS)))
4087c478bd9Sstevel@tonic-gate flagword |= PADZERO;
4097c478bd9Sstevel@tonic-gate /* FALLTHROUGH */
4107c478bd9Sstevel@tonic-gate case '1':
4117c478bd9Sstevel@tonic-gate case '2':
4127c478bd9Sstevel@tonic-gate case '3':
4137c478bd9Sstevel@tonic-gate case '4':
4147c478bd9Sstevel@tonic-gate case '5':
4157c478bd9Sstevel@tonic-gate case '6':
4167c478bd9Sstevel@tonic-gate case '7':
4177c478bd9Sstevel@tonic-gate case '8':
4187c478bd9Sstevel@tonic-gate case '9':
419*7257d1b4Sraf {
420*7257d1b4Sraf int num = fcode - '0';
4217c478bd9Sstevel@tonic-gate while (isdigit(fcode = *format)) {
4227c478bd9Sstevel@tonic-gate num = num * 10 + fcode - '0';
4237c478bd9Sstevel@tonic-gate format++;
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate if (flagword & DOTSEEN)
4267c478bd9Sstevel@tonic-gate prec = num;
4277c478bd9Sstevel@tonic-gate else
4287c478bd9Sstevel@tonic-gate width = num;
4297c478bd9Sstevel@tonic-gate goto charswitch;
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate
4327c478bd9Sstevel@tonic-gate /* Scan the length modifier */
4337c478bd9Sstevel@tonic-gate case 'l':
4347c478bd9Sstevel@tonic-gate flagword |= LENGTH;
4357c478bd9Sstevel@tonic-gate goto charswitch;
4367c478bd9Sstevel@tonic-gate case 'h':
4377c478bd9Sstevel@tonic-gate flagword |= SHORT;
4387c478bd9Sstevel@tonic-gate goto charswitch;
4397c478bd9Sstevel@tonic-gate case 'L':
4407c478bd9Sstevel@tonic-gate goto charswitch;
4417c478bd9Sstevel@tonic-gate
4427c478bd9Sstevel@tonic-gate /*
4437c478bd9Sstevel@tonic-gate * The character addressed by format must be
4447c478bd9Sstevel@tonic-gate * the format letter -- there is nothing
4457c478bd9Sstevel@tonic-gate * left for it to be.
4467c478bd9Sstevel@tonic-gate *
4477c478bd9Sstevel@tonic-gate * The status of the +, -, #, and blank
4487c478bd9Sstevel@tonic-gate * flags are reflected in the variable
4497c478bd9Sstevel@tonic-gate * "flagword". "width" and "prec" contain
4507c478bd9Sstevel@tonic-gate * numbers corresponding to the digit
4517c478bd9Sstevel@tonic-gate * strings before and after the decimal
4527c478bd9Sstevel@tonic-gate * point, respectively. If there was no
4537c478bd9Sstevel@tonic-gate * decimal point, then flagword & DOTSEEN
4547c478bd9Sstevel@tonic-gate * is false and the value of prec is meaningless.
4557c478bd9Sstevel@tonic-gate *
4567c478bd9Sstevel@tonic-gate * The following switch cases set things up
4577c478bd9Sstevel@tonic-gate * for printing. What ultimately gets
4587c478bd9Sstevel@tonic-gate * printed will be padding blanks, a
4597c478bd9Sstevel@tonic-gate * prefix, left padding zeroes, a value,
4607c478bd9Sstevel@tonic-gate * right padding zeroes, a suffix, and
4617c478bd9Sstevel@tonic-gate * more padding blanks. Padding blanks
4627c478bd9Sstevel@tonic-gate * will not appear simultaneously on both
4637c478bd9Sstevel@tonic-gate * the left and the right. Each case in
4647c478bd9Sstevel@tonic-gate * this switch will compute the value, and
4657c478bd9Sstevel@tonic-gate * leave in several variables the informa-
4667c478bd9Sstevel@tonic-gate * tion necessary to construct what is to
4677c478bd9Sstevel@tonic-gate * be printed.
4687c478bd9Sstevel@tonic-gate *
4697c478bd9Sstevel@tonic-gate * The prefix is a sign, a blank, "0x",
4707c478bd9Sstevel@tonic-gate * "0X", or null, and is addressed by
4717c478bd9Sstevel@tonic-gate * "prefix".
4727c478bd9Sstevel@tonic-gate *
4737c478bd9Sstevel@tonic-gate * The suffix is either null or an
4747c478bd9Sstevel@tonic-gate * exponent, and is addressed by "suffix".
4757c478bd9Sstevel@tonic-gate * If there is a suffix, the flagword bit
4767c478bd9Sstevel@tonic-gate * SUFFIX will be set.
4777c478bd9Sstevel@tonic-gate *
4787c478bd9Sstevel@tonic-gate * The value to be printed starts at "bp"
4797c478bd9Sstevel@tonic-gate * and continues up to and not including
4807c478bd9Sstevel@tonic-gate * "p".
4817c478bd9Sstevel@tonic-gate *
4827c478bd9Sstevel@tonic-gate * "lzero" and "rzero" will contain the
4837c478bd9Sstevel@tonic-gate * number of padding zeroes required on
4847c478bd9Sstevel@tonic-gate * the left and right, respectively.
4857c478bd9Sstevel@tonic-gate * The flagword bits LZERO and RZERO tell
4867c478bd9Sstevel@tonic-gate * whether padding zeros are required.
4877c478bd9Sstevel@tonic-gate *
4887c478bd9Sstevel@tonic-gate * The number of padding blanks, and
4897c478bd9Sstevel@tonic-gate * whether they go on the left or the
4907c478bd9Sstevel@tonic-gate * right, will be computed on exit from
4917c478bd9Sstevel@tonic-gate * the switch.
4927c478bd9Sstevel@tonic-gate */
4937c478bd9Sstevel@tonic-gate
4947c478bd9Sstevel@tonic-gate
4957c478bd9Sstevel@tonic-gate
4967c478bd9Sstevel@tonic-gate
4977c478bd9Sstevel@tonic-gate /*
4987c478bd9Sstevel@tonic-gate * decimal fixed point representations
4997c478bd9Sstevel@tonic-gate *
5007c478bd9Sstevel@tonic-gate * HIBITL is 100...000
5017c478bd9Sstevel@tonic-gate * binary, and is equal to the maximum
5027c478bd9Sstevel@tonic-gate * negative number.
5037c478bd9Sstevel@tonic-gate * We assume a 2's complement machine
5047c478bd9Sstevel@tonic-gate */
5057c478bd9Sstevel@tonic-gate
5067c478bd9Sstevel@tonic-gate case 'i':
5077c478bd9Sstevel@tonic-gate case 'd':
5087c478bd9Sstevel@tonic-gate /* Fetch the argument to be printed */
5097c478bd9Sstevel@tonic-gate if (flagword & LENGTH)
5107c478bd9Sstevel@tonic-gate val = va_arg(args.ap, long);
5117c478bd9Sstevel@tonic-gate else
5127c478bd9Sstevel@tonic-gate val = va_arg(args.ap, int);
5137c478bd9Sstevel@tonic-gate
5147c478bd9Sstevel@tonic-gate if (flagword & SHORT)
5157c478bd9Sstevel@tonic-gate val = (short)val;
5167c478bd9Sstevel@tonic-gate
5177c478bd9Sstevel@tonic-gate /* Set buffer pointer to last digit */
5187c478bd9Sstevel@tonic-gate p = bp = buf + MAXDIGS;
5197c478bd9Sstevel@tonic-gate
5207c478bd9Sstevel@tonic-gate /* If signed conversion, make sign */
5217c478bd9Sstevel@tonic-gate if (val < 0) {
5227c478bd9Sstevel@tonic-gate prefix = "-";
5237c478bd9Sstevel@tonic-gate prefixlength = 1;
5247c478bd9Sstevel@tonic-gate /*
5257c478bd9Sstevel@tonic-gate * Negate, checking in
5267c478bd9Sstevel@tonic-gate * advance for possible
5277c478bd9Sstevel@tonic-gate * overflow.
5287c478bd9Sstevel@tonic-gate */
5297c478bd9Sstevel@tonic-gate if (val != HIBITL)
5307c478bd9Sstevel@tonic-gate val = -val;
5317c478bd9Sstevel@tonic-gate else /* number is -HIBITL; convert last */
5327c478bd9Sstevel@tonic-gate /* digit now and get positive number */
5337c478bd9Sstevel@tonic-gate *--bp = _lowdigit(&val);
5347c478bd9Sstevel@tonic-gate } else if (flagword & FPLUS) {
5357c478bd9Sstevel@tonic-gate prefix = "+";
5367c478bd9Sstevel@tonic-gate prefixlength = 1;
5377c478bd9Sstevel@tonic-gate } else if (flagword & FBLANK) {
5387c478bd9Sstevel@tonic-gate prefix = " ";
5397c478bd9Sstevel@tonic-gate prefixlength = 1;
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate
5427c478bd9Sstevel@tonic-gate decimal:
543*7257d1b4Sraf {
544*7257d1b4Sraf long qval = val;
5457c478bd9Sstevel@tonic-gate long saveq;
5467c478bd9Sstevel@tonic-gate
5477c478bd9Sstevel@tonic-gate if (qval <= 9) {
5487c478bd9Sstevel@tonic-gate if (qval != 0 || !(flagword & DOTSEEN))
5497c478bd9Sstevel@tonic-gate *--bp = (char)(qval + '0');
5507c478bd9Sstevel@tonic-gate } else {
5517c478bd9Sstevel@tonic-gate do {
5527c478bd9Sstevel@tonic-gate saveq = qval;
5537c478bd9Sstevel@tonic-gate qval /= 10;
5547c478bd9Sstevel@tonic-gate *--bp = (char)(saveq -
5557c478bd9Sstevel@tonic-gate qval * 10 + '0');
5567c478bd9Sstevel@tonic-gate } while (qval > 9);
5577c478bd9Sstevel@tonic-gate *--bp = (char)(qval + '0');
5587c478bd9Sstevel@tonic-gate pdiff = (ptrdiff_t)saveq;
5597c478bd9Sstevel@tonic-gate }
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate
5627c478bd9Sstevel@tonic-gate /* Calculate minimum padding zero requirement */
5637c478bd9Sstevel@tonic-gate if (flagword & DOTSEEN) {
5647c478bd9Sstevel@tonic-gate int leadzeroes = prec - (int)(p - bp);
5657c478bd9Sstevel@tonic-gate if (leadzeroes > 0) {
5667c478bd9Sstevel@tonic-gate otherlength = lzero = leadzeroes;
5677c478bd9Sstevel@tonic-gate flagword |= LZERO;
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate
5717c478bd9Sstevel@tonic-gate break;
5727c478bd9Sstevel@tonic-gate
5737c478bd9Sstevel@tonic-gate case 'u':
5747c478bd9Sstevel@tonic-gate /* Fetch the argument to be printed */
5757c478bd9Sstevel@tonic-gate if (flagword & LENGTH)
5767c478bd9Sstevel@tonic-gate val = va_arg(args.ap, long);
5777c478bd9Sstevel@tonic-gate else
5787c478bd9Sstevel@tonic-gate val = va_arg(args.ap, unsigned);
5797c478bd9Sstevel@tonic-gate
5807c478bd9Sstevel@tonic-gate if (flagword & SHORT)
5817c478bd9Sstevel@tonic-gate val = (unsigned short)val;
5827c478bd9Sstevel@tonic-gate
5837c478bd9Sstevel@tonic-gate p = bp = buf + MAXDIGS;
5847c478bd9Sstevel@tonic-gate
5857c478bd9Sstevel@tonic-gate if (val & HIBITL)
5867c478bd9Sstevel@tonic-gate *--bp = _lowdigit(&val);
5877c478bd9Sstevel@tonic-gate
5887c478bd9Sstevel@tonic-gate goto decimal;
5897c478bd9Sstevel@tonic-gate
5907c478bd9Sstevel@tonic-gate /*
5917c478bd9Sstevel@tonic-gate * non-decimal fixed point representations
5927c478bd9Sstevel@tonic-gate * for radix equal to a power of two
5937c478bd9Sstevel@tonic-gate *
5947c478bd9Sstevel@tonic-gate * "mradix" is one less than the radix for the conversion.
5957c478bd9Sstevel@tonic-gate * "lradix" is one less than the base 2 log
5967c478bd9Sstevel@tonic-gate * of the radix for the conversion. Conversion is unsigned.
5977c478bd9Sstevel@tonic-gate * HIBITL is 100...000
5987c478bd9Sstevel@tonic-gate * binary, and is equal to the maximum
5997c478bd9Sstevel@tonic-gate * negative number.
6007c478bd9Sstevel@tonic-gate * We assume a 2's complement machine
6017c478bd9Sstevel@tonic-gate */
6027c478bd9Sstevel@tonic-gate
6037c478bd9Sstevel@tonic-gate case 'o':
6047c478bd9Sstevel@tonic-gate mradix = 7;
6057c478bd9Sstevel@tonic-gate lradix = 2;
6067c478bd9Sstevel@tonic-gate goto fixed;
6077c478bd9Sstevel@tonic-gate
6087c478bd9Sstevel@tonic-gate case 'X':
6097c478bd9Sstevel@tonic-gate case 'x':
6107c478bd9Sstevel@tonic-gate case 'p':
6117c478bd9Sstevel@tonic-gate mradix = 15;
6127c478bd9Sstevel@tonic-gate lradix = 3;
6137c478bd9Sstevel@tonic-gate
6147c478bd9Sstevel@tonic-gate fixed:
6157c478bd9Sstevel@tonic-gate /* Fetch the argument to be printed */
6167c478bd9Sstevel@tonic-gate if (flagword & LENGTH)
6177c478bd9Sstevel@tonic-gate val = va_arg(args.ap, long);
6187c478bd9Sstevel@tonic-gate else
6197c478bd9Sstevel@tonic-gate val = va_arg(args.ap, unsigned);
6207c478bd9Sstevel@tonic-gate
6217c478bd9Sstevel@tonic-gate if (flagword & SHORT)
6227c478bd9Sstevel@tonic-gate val = (unsigned short)val;
6237c478bd9Sstevel@tonic-gate
6247c478bd9Sstevel@tonic-gate /* Set translate table for digits */
6257c478bd9Sstevel@tonic-gate tab = (fcode == 'X') ? uc_digs : lc_digs;
6267c478bd9Sstevel@tonic-gate
6277c478bd9Sstevel@tonic-gate /* Entry point when printing a double which is a NaN */
6287c478bd9Sstevel@tonic-gate put_pc:
6297c478bd9Sstevel@tonic-gate /* Develop the digits of the value */
6307c478bd9Sstevel@tonic-gate p = bp = buf + MAXDIGS;
631*7257d1b4Sraf {
632*7257d1b4Sraf long qval = val;
6337c478bd9Sstevel@tonic-gate if (qval == 0) {
6347c478bd9Sstevel@tonic-gate if (!(flagword & DOTSEEN)) {
6357c478bd9Sstevel@tonic-gate otherlength = lzero = 1;
6367c478bd9Sstevel@tonic-gate flagword |= LZERO;
6377c478bd9Sstevel@tonic-gate }
6387c478bd9Sstevel@tonic-gate } else
6397c478bd9Sstevel@tonic-gate do {
6407c478bd9Sstevel@tonic-gate *--bp = tab[qval & mradix];
6417c478bd9Sstevel@tonic-gate qval = ((qval >> 1) & ~HIBITL)
6427c478bd9Sstevel@tonic-gate >> lradix;
6437c478bd9Sstevel@tonic-gate } while (qval != 0);
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate
6467c478bd9Sstevel@tonic-gate /* Calculate minimum padding zero requirement */
6477c478bd9Sstevel@tonic-gate if (flagword & DOTSEEN) {
6487c478bd9Sstevel@tonic-gate int leadzeroes = prec - (int)(p - bp);
6497c478bd9Sstevel@tonic-gate if (leadzeroes > 0) {
6507c478bd9Sstevel@tonic-gate otherlength = lzero = leadzeroes;
6517c478bd9Sstevel@tonic-gate flagword |= LZERO;
6527c478bd9Sstevel@tonic-gate }
6537c478bd9Sstevel@tonic-gate }
6547c478bd9Sstevel@tonic-gate
6557c478bd9Sstevel@tonic-gate /* Handle the # flag */
6567c478bd9Sstevel@tonic-gate if (flagword & FSHARP && val != 0)
6577c478bd9Sstevel@tonic-gate switch (fcode) {
6587c478bd9Sstevel@tonic-gate case 'o':
6597c478bd9Sstevel@tonic-gate if (!(flagword & LZERO)) {
6607c478bd9Sstevel@tonic-gate otherlength = lzero = 1;
6617c478bd9Sstevel@tonic-gate flagword |= LZERO;
6627c478bd9Sstevel@tonic-gate }
6637c478bd9Sstevel@tonic-gate break;
6647c478bd9Sstevel@tonic-gate case 'x':
6657c478bd9Sstevel@tonic-gate prefix = "0x";
6667c478bd9Sstevel@tonic-gate prefixlength = 2;
6677c478bd9Sstevel@tonic-gate break;
6687c478bd9Sstevel@tonic-gate case 'X':
6697c478bd9Sstevel@tonic-gate prefix = "0X";
6707c478bd9Sstevel@tonic-gate prefixlength = 2;
6717c478bd9Sstevel@tonic-gate break;
6727c478bd9Sstevel@tonic-gate }
6737c478bd9Sstevel@tonic-gate
6747c478bd9Sstevel@tonic-gate break;
6757c478bd9Sstevel@tonic-gate
6767c478bd9Sstevel@tonic-gate case 'E':
6777c478bd9Sstevel@tonic-gate case 'e':
6787c478bd9Sstevel@tonic-gate /*
6797c478bd9Sstevel@tonic-gate * E-format. The general strategy
6807c478bd9Sstevel@tonic-gate * here is fairly easy: we take
6817c478bd9Sstevel@tonic-gate * what ecvt gives us and re-format it.
6827c478bd9Sstevel@tonic-gate */
6837c478bd9Sstevel@tonic-gate
6847c478bd9Sstevel@tonic-gate /* Establish default precision */
6857c478bd9Sstevel@tonic-gate if (!(flagword & DOTSEEN))
6867c478bd9Sstevel@tonic-gate prec = 6;
6877c478bd9Sstevel@tonic-gate
6887c478bd9Sstevel@tonic-gate /* Fetch the value */
6897c478bd9Sstevel@tonic-gate dval = va_arg(args.ap, double);
6907c478bd9Sstevel@tonic-gate
6917c478bd9Sstevel@tonic-gate /* Check for NaNs and Infinities */
6927c478bd9Sstevel@tonic-gate if (IsNANorINF(dval)) {
6937c478bd9Sstevel@tonic-gate if (IsINF(dval)) {
6947c478bd9Sstevel@tonic-gate if (IsNegNAN(dval))
6957c478bd9Sstevel@tonic-gate neg_in = 1;
6967c478bd9Sstevel@tonic-gate inf_nan = 1;
6977c478bd9Sstevel@tonic-gate bp = (fcode == 'E')? uc_inf: lc_inf;
6987c478bd9Sstevel@tonic-gate p = bp + 3;
6997c478bd9Sstevel@tonic-gate break;
7007c478bd9Sstevel@tonic-gate } else {
7017c478bd9Sstevel@tonic-gate if (IsNegNAN(dval))
7027c478bd9Sstevel@tonic-gate neg_in = 1;
7037c478bd9Sstevel@tonic-gate inf_nan = 1;
7047c478bd9Sstevel@tonic-gate val = GETNaNPC(dval);
7057c478bd9Sstevel@tonic-gate NaN_flg = SNLEN;
7067c478bd9Sstevel@tonic-gate mradix = 15;
7077c478bd9Sstevel@tonic-gate lradix = 3;
7087c478bd9Sstevel@tonic-gate if (fcode == 'E') {
7097c478bd9Sstevel@tonic-gate SNAN = uc_nan;
7107c478bd9Sstevel@tonic-gate tab = uc_digs;
7117c478bd9Sstevel@tonic-gate } else {
7127c478bd9Sstevel@tonic-gate SNAN = lc_nan;
7137c478bd9Sstevel@tonic-gate tab = lc_digs;
7147c478bd9Sstevel@tonic-gate }
7157c478bd9Sstevel@tonic-gate goto put_pc;
7167c478bd9Sstevel@tonic-gate }
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate /* Develop the mantissa */
7197c478bd9Sstevel@tonic-gate bp = ecvt(dval, min(prec + 1, MAXECVT), &decpt, &sign);
7207c478bd9Sstevel@tonic-gate
7217c478bd9Sstevel@tonic-gate /* Determine the prefix */
7227c478bd9Sstevel@tonic-gate e_merge:
7237c478bd9Sstevel@tonic-gate if (sign) {
7247c478bd9Sstevel@tonic-gate prefix = "-";
7257c478bd9Sstevel@tonic-gate prefixlength = 1;
7267c478bd9Sstevel@tonic-gate } else if (flagword & FPLUS) {
7277c478bd9Sstevel@tonic-gate prefix = "+";
7287c478bd9Sstevel@tonic-gate prefixlength = 1;
7297c478bd9Sstevel@tonic-gate } else if (flagword & FBLANK) {
7307c478bd9Sstevel@tonic-gate prefix = " ";
7317c478bd9Sstevel@tonic-gate prefixlength = 1;
7327c478bd9Sstevel@tonic-gate }
7337c478bd9Sstevel@tonic-gate
7347c478bd9Sstevel@tonic-gate /* Place the first digit in the buffer */
7357c478bd9Sstevel@tonic-gate p = &buf[0];
7367c478bd9Sstevel@tonic-gate *p++ = (*bp != '\0') ? *bp++ : '0';
7377c478bd9Sstevel@tonic-gate
7387c478bd9Sstevel@tonic-gate /* Put in a decimal point if needed */
7397c478bd9Sstevel@tonic-gate if (prec != 0 || (flagword & FSHARP))
7407c478bd9Sstevel@tonic-gate *p++ = _numeric[0];
7417c478bd9Sstevel@tonic-gate
7427c478bd9Sstevel@tonic-gate /* Create the rest of the mantissa */
743*7257d1b4Sraf {
744*7257d1b4Sraf int rz = prec;
7457c478bd9Sstevel@tonic-gate for (; rz > 0 && *bp != '\0'; --rz)
7467c478bd9Sstevel@tonic-gate *p++ = *bp++;
7477c478bd9Sstevel@tonic-gate if (rz > 0) {
7487c478bd9Sstevel@tonic-gate otherlength = rzero = rz;
7497c478bd9Sstevel@tonic-gate flagword |= RZERO;
7507c478bd9Sstevel@tonic-gate }
7517c478bd9Sstevel@tonic-gate }
7527c478bd9Sstevel@tonic-gate
7537c478bd9Sstevel@tonic-gate bp = &buf[0];
7547c478bd9Sstevel@tonic-gate
7557c478bd9Sstevel@tonic-gate /* Create the exponent */
7567c478bd9Sstevel@tonic-gate *(suffix = &expbuf[MAXESIZ]) = '\0';
7577c478bd9Sstevel@tonic-gate if (dval != 0) {
7587c478bd9Sstevel@tonic-gate int nn = decpt - 1;
7597c478bd9Sstevel@tonic-gate if (nn < 0)
7607c478bd9Sstevel@tonic-gate nn = -nn;
7617c478bd9Sstevel@tonic-gate for (; nn > 9; nn /= 10)
7627c478bd9Sstevel@tonic-gate *--suffix = todigit(nn % 10);
7637c478bd9Sstevel@tonic-gate *--suffix = todigit(nn);
7647c478bd9Sstevel@tonic-gate }
7657c478bd9Sstevel@tonic-gate
7667c478bd9Sstevel@tonic-gate /* Prepend leading zeroes to the exponent */
7677c478bd9Sstevel@tonic-gate while (suffix > &expbuf[MAXESIZ - 2])
7687c478bd9Sstevel@tonic-gate *--suffix = '0';
7697c478bd9Sstevel@tonic-gate
7707c478bd9Sstevel@tonic-gate /* Put in the exponent sign */
7717c478bd9Sstevel@tonic-gate *--suffix = (decpt > 0 || dval == 0) ? '+' : '-';
7727c478bd9Sstevel@tonic-gate
7737c478bd9Sstevel@tonic-gate /* Put in the e */
7747c478bd9Sstevel@tonic-gate *--suffix = isupper(fcode) ? 'E' : 'e';
7757c478bd9Sstevel@tonic-gate
7767c478bd9Sstevel@tonic-gate /* compute size of suffix */
7777c478bd9Sstevel@tonic-gate otherlength += (suffixlength =
7787c478bd9Sstevel@tonic-gate (int)(&expbuf[MAXESIZ] - suffix));
7797c478bd9Sstevel@tonic-gate flagword |= SUFFIX;
7807c478bd9Sstevel@tonic-gate
7817c478bd9Sstevel@tonic-gate break;
7827c478bd9Sstevel@tonic-gate
7837c478bd9Sstevel@tonic-gate case 'f':
7847c478bd9Sstevel@tonic-gate /*
7857c478bd9Sstevel@tonic-gate * F-format floating point. This is a
7867c478bd9Sstevel@tonic-gate * good deal less simple than E-format.
7877c478bd9Sstevel@tonic-gate * The overall strategy will be to call
7887c478bd9Sstevel@tonic-gate * fcvt, reformat its result into buf,
7897c478bd9Sstevel@tonic-gate * and calculate how many trailing
7907c478bd9Sstevel@tonic-gate * zeroes will be required. There will
7917c478bd9Sstevel@tonic-gate * never be any leading zeroes needed.
7927c478bd9Sstevel@tonic-gate */
7937c478bd9Sstevel@tonic-gate
7947c478bd9Sstevel@tonic-gate /* Establish default precision */
7957c478bd9Sstevel@tonic-gate if (!(flagword & DOTSEEN))
7967c478bd9Sstevel@tonic-gate prec = 6;
7977c478bd9Sstevel@tonic-gate
7987c478bd9Sstevel@tonic-gate /* Fetch the value */
7997c478bd9Sstevel@tonic-gate dval = va_arg(args.ap, double);
8007c478bd9Sstevel@tonic-gate
8017c478bd9Sstevel@tonic-gate /* Check for NaNs and Infinities */
8027c478bd9Sstevel@tonic-gate if (IsNANorINF(dval)) {
8037c478bd9Sstevel@tonic-gate if (IsINF(dval)) {
8047c478bd9Sstevel@tonic-gate if (IsNegNAN(dval))
8057c478bd9Sstevel@tonic-gate neg_in = 1;
8067c478bd9Sstevel@tonic-gate inf_nan = 1;
8077c478bd9Sstevel@tonic-gate bp = lc_inf;
8087c478bd9Sstevel@tonic-gate p = bp + 3;
8097c478bd9Sstevel@tonic-gate break;
8107c478bd9Sstevel@tonic-gate } else {
8117c478bd9Sstevel@tonic-gate if (IsNegNAN(dval))
8127c478bd9Sstevel@tonic-gate neg_in = 1;
8137c478bd9Sstevel@tonic-gate inf_nan = 1;
8147c478bd9Sstevel@tonic-gate val = GETNaNPC(dval);
8157c478bd9Sstevel@tonic-gate NaN_flg = SNLEN;
8167c478bd9Sstevel@tonic-gate mradix = 15;
8177c478bd9Sstevel@tonic-gate lradix = 3;
8187c478bd9Sstevel@tonic-gate tab = lc_digs;
8197c478bd9Sstevel@tonic-gate SNAN = lc_nan;
8207c478bd9Sstevel@tonic-gate goto put_pc;
8217c478bd9Sstevel@tonic-gate }
8227c478bd9Sstevel@tonic-gate }
8237c478bd9Sstevel@tonic-gate /* Do the conversion */
8247c478bd9Sstevel@tonic-gate bp = fcvt(dval, min(prec, MAXFCVT), &decpt, &sign);
8257c478bd9Sstevel@tonic-gate
8267c478bd9Sstevel@tonic-gate /* Determine the prefix */
8277c478bd9Sstevel@tonic-gate f_merge:
8287c478bd9Sstevel@tonic-gate if (sign) {
8297c478bd9Sstevel@tonic-gate prefix = "-";
8307c478bd9Sstevel@tonic-gate prefixlength = 1;
8317c478bd9Sstevel@tonic-gate } else if (flagword & FPLUS) {
8327c478bd9Sstevel@tonic-gate prefix = "+";
8337c478bd9Sstevel@tonic-gate prefixlength = 1;
8347c478bd9Sstevel@tonic-gate } else if (flagword & FBLANK) {
8357c478bd9Sstevel@tonic-gate prefix = " ";
8367c478bd9Sstevel@tonic-gate prefixlength = 1;
8377c478bd9Sstevel@tonic-gate }
8387c478bd9Sstevel@tonic-gate
8397c478bd9Sstevel@tonic-gate /* Initialize buffer pointer */
8407c478bd9Sstevel@tonic-gate p = &buf[0];
841*7257d1b4Sraf {
842*7257d1b4Sraf int nn = decpt;
8437c478bd9Sstevel@tonic-gate
8447c478bd9Sstevel@tonic-gate /* Emit the digits before the decimal point */
8457c478bd9Sstevel@tonic-gate k = 0;
8467c478bd9Sstevel@tonic-gate do {
8477c478bd9Sstevel@tonic-gate *p++ = (nn <= 0 || *bp == '\0' ||
8487c478bd9Sstevel@tonic-gate k >= MAXFSIG) ?
8497c478bd9Sstevel@tonic-gate '0' : (k++, *bp++);
8507c478bd9Sstevel@tonic-gate } while (--nn > 0);
8517c478bd9Sstevel@tonic-gate
8527c478bd9Sstevel@tonic-gate /* Decide whether we need a decimal point */
8537c478bd9Sstevel@tonic-gate if ((flagword & FSHARP) || prec > 0)
8547c478bd9Sstevel@tonic-gate *p++ = _numeric[0];
8557c478bd9Sstevel@tonic-gate
8567c478bd9Sstevel@tonic-gate /* Digits (if any) after the decimal point */
8577c478bd9Sstevel@tonic-gate nn = min(prec, MAXFCVT);
8587c478bd9Sstevel@tonic-gate if (prec > nn) {
8597c478bd9Sstevel@tonic-gate flagword |= RZERO;
8607c478bd9Sstevel@tonic-gate otherlength = rzero = prec - nn;
8617c478bd9Sstevel@tonic-gate }
8627c478bd9Sstevel@tonic-gate while (--nn >= 0)
8637c478bd9Sstevel@tonic-gate *p++ = (++decpt <= 0 || *bp == '\0' ||
8647c478bd9Sstevel@tonic-gate k >= MAXFSIG) ?
8657c478bd9Sstevel@tonic-gate '0' : (k++, *bp++);
8667c478bd9Sstevel@tonic-gate }
8677c478bd9Sstevel@tonic-gate
8687c478bd9Sstevel@tonic-gate bp = &buf[0];
8697c478bd9Sstevel@tonic-gate
8707c478bd9Sstevel@tonic-gate break;
8717c478bd9Sstevel@tonic-gate
8727c478bd9Sstevel@tonic-gate case 'G':
8737c478bd9Sstevel@tonic-gate case 'g':
8747c478bd9Sstevel@tonic-gate /*
8757c478bd9Sstevel@tonic-gate * g-format. We play around a bit
8767c478bd9Sstevel@tonic-gate * and then jump into e or f, as needed.
8777c478bd9Sstevel@tonic-gate */
8787c478bd9Sstevel@tonic-gate
8797c478bd9Sstevel@tonic-gate /* Establish default precision */
8807c478bd9Sstevel@tonic-gate if (!(flagword & DOTSEEN))
8817c478bd9Sstevel@tonic-gate prec = 6;
8827c478bd9Sstevel@tonic-gate else if (prec == 0)
8837c478bd9Sstevel@tonic-gate prec = 1;
8847c478bd9Sstevel@tonic-gate
8857c478bd9Sstevel@tonic-gate /* Fetch the value */
8867c478bd9Sstevel@tonic-gate dval = va_arg(args.ap, double);
8877c478bd9Sstevel@tonic-gate
8887c478bd9Sstevel@tonic-gate /* Check for NaN and Infinities */
8897c478bd9Sstevel@tonic-gate if (IsNANorINF(dval)) {
8907c478bd9Sstevel@tonic-gate if (IsINF(dval)) {
8917c478bd9Sstevel@tonic-gate if (IsNegNAN(dval))
8927c478bd9Sstevel@tonic-gate neg_in = 1;
8937c478bd9Sstevel@tonic-gate bp = (fcode == 'G') ? uc_inf : lc_inf;
8947c478bd9Sstevel@tonic-gate p = bp + 3;
8957c478bd9Sstevel@tonic-gate inf_nan = 1;
8967c478bd9Sstevel@tonic-gate break;
8977c478bd9Sstevel@tonic-gate } else {
8987c478bd9Sstevel@tonic-gate if (IsNegNAN(dval))
8997c478bd9Sstevel@tonic-gate neg_in = 1;
9007c478bd9Sstevel@tonic-gate inf_nan = 1;
9017c478bd9Sstevel@tonic-gate val = GETNaNPC(dval);
9027c478bd9Sstevel@tonic-gate NaN_flg = SNLEN;
9037c478bd9Sstevel@tonic-gate mradix = 15;
9047c478bd9Sstevel@tonic-gate lradix = 3;
9057c478bd9Sstevel@tonic-gate if (fcode == 'G') {
9067c478bd9Sstevel@tonic-gate SNAN = uc_nan;
9077c478bd9Sstevel@tonic-gate tab = uc_digs;
9087c478bd9Sstevel@tonic-gate } else {
9097c478bd9Sstevel@tonic-gate SNAN = lc_nan;
9107c478bd9Sstevel@tonic-gate tab = lc_digs;
9117c478bd9Sstevel@tonic-gate }
9127c478bd9Sstevel@tonic-gate goto put_pc;
9137c478bd9Sstevel@tonic-gate }
9147c478bd9Sstevel@tonic-gate }
9157c478bd9Sstevel@tonic-gate
9167c478bd9Sstevel@tonic-gate /* Do the conversion */
9177c478bd9Sstevel@tonic-gate bp = ecvt(dval, min(prec, MAXECVT), &decpt, &sign);
9187c478bd9Sstevel@tonic-gate if (dval == 0)
9197c478bd9Sstevel@tonic-gate decpt = 1;
920*7257d1b4Sraf {
921*7257d1b4Sraf int kk = prec;
9227c478bd9Sstevel@tonic-gate size_t sz;
9237c478bd9Sstevel@tonic-gate
9247c478bd9Sstevel@tonic-gate if (!(flagword & FSHARP)) {
9257c478bd9Sstevel@tonic-gate sz = strlen(bp);
9267c478bd9Sstevel@tonic-gate if (sz < kk)
9277c478bd9Sstevel@tonic-gate kk = (int)sz;
9287c478bd9Sstevel@tonic-gate while (kk >= 1 && bp[kk-1] == '0')
9297c478bd9Sstevel@tonic-gate --kk;
9307c478bd9Sstevel@tonic-gate }
9317c478bd9Sstevel@tonic-gate
9327c478bd9Sstevel@tonic-gate if (decpt < -3 || decpt > prec) {
9337c478bd9Sstevel@tonic-gate prec = kk - 1;
9347c478bd9Sstevel@tonic-gate goto e_merge;
9357c478bd9Sstevel@tonic-gate }
9367c478bd9Sstevel@tonic-gate prec = kk - decpt;
9377c478bd9Sstevel@tonic-gate goto f_merge;
9387c478bd9Sstevel@tonic-gate }
9397c478bd9Sstevel@tonic-gate
9407c478bd9Sstevel@tonic-gate case '%':
9417c478bd9Sstevel@tonic-gate buf[0] = (char)fcode;
9427c478bd9Sstevel@tonic-gate goto c_merge;
9437c478bd9Sstevel@tonic-gate
9447c478bd9Sstevel@tonic-gate case 'c':
9457c478bd9Sstevel@tonic-gate buf[0] = va_arg(args.ap, int);
9467c478bd9Sstevel@tonic-gate c_merge:
9477c478bd9Sstevel@tonic-gate p = (bp = &buf[0]) + 1;
9487c478bd9Sstevel@tonic-gate break;
9497c478bd9Sstevel@tonic-gate
9507c478bd9Sstevel@tonic-gate case 's':
9517c478bd9Sstevel@tonic-gate bp = va_arg(args.ap, char *);
9527c478bd9Sstevel@tonic-gate if (!(flagword & DOTSEEN))
9537c478bd9Sstevel@tonic-gate p = bp + strlen(bp);
9547c478bd9Sstevel@tonic-gate else { /* a strnlen function would be useful here! */
9557c478bd9Sstevel@tonic-gate char *qp = bp;
9567c478bd9Sstevel@tonic-gate while (*qp++ != '\0' && --prec >= 0)
9577c478bd9Sstevel@tonic-gate ;
9587c478bd9Sstevel@tonic-gate p = qp - 1;
9597c478bd9Sstevel@tonic-gate }
9607c478bd9Sstevel@tonic-gate break;
9617c478bd9Sstevel@tonic-gate
9627c478bd9Sstevel@tonic-gate case 'n':
9637c478bd9Sstevel@tonic-gate {
9647c478bd9Sstevel@tonic-gate if (flagword & LENGTH) {
9657c478bd9Sstevel@tonic-gate long *svcount;
9667c478bd9Sstevel@tonic-gate svcount = va_arg(args.ap, long *);
9677c478bd9Sstevel@tonic-gate *svcount = count;
9687c478bd9Sstevel@tonic-gate } else if (flagword & SHORT) {
9697c478bd9Sstevel@tonic-gate short *svcount;
9707c478bd9Sstevel@tonic-gate svcount = va_arg(args.ap, short *);
9717c478bd9Sstevel@tonic-gate *svcount = (short)count;
9727c478bd9Sstevel@tonic-gate } else {
9737c478bd9Sstevel@tonic-gate int *svcount;
9747c478bd9Sstevel@tonic-gate svcount = va_arg(args.ap, int *);
9757c478bd9Sstevel@tonic-gate *svcount = count;
9767c478bd9Sstevel@tonic-gate }
9777c478bd9Sstevel@tonic-gate continue;
9787c478bd9Sstevel@tonic-gate }
9797c478bd9Sstevel@tonic-gate
9807c478bd9Sstevel@tonic-gate default: /* this is technically an error; what we do is to */
9817c478bd9Sstevel@tonic-gate /* back up the format pointer to the offending char */
9827c478bd9Sstevel@tonic-gate /* and continue with the format scan */
9837c478bd9Sstevel@tonic-gate format--;
9847c478bd9Sstevel@tonic-gate continue;
9857c478bd9Sstevel@tonic-gate
9867c478bd9Sstevel@tonic-gate }
9877c478bd9Sstevel@tonic-gate
9887c478bd9Sstevel@tonic-gate if (inf_nan) {
9897c478bd9Sstevel@tonic-gate if (neg_in) {
9907c478bd9Sstevel@tonic-gate prefix = "-";
9917c478bd9Sstevel@tonic-gate prefixlength = 1;
9927c478bd9Sstevel@tonic-gate neg_in = 0;
9937c478bd9Sstevel@tonic-gate } else if (flagword & FPLUS) {
9947c478bd9Sstevel@tonic-gate prefix = "+";
9957c478bd9Sstevel@tonic-gate prefixlength = 1;
9967c478bd9Sstevel@tonic-gate } else if (flagword & FBLANK) {
9977c478bd9Sstevel@tonic-gate prefix = " ";
9987c478bd9Sstevel@tonic-gate prefixlength = 1;
9997c478bd9Sstevel@tonic-gate }
10007c478bd9Sstevel@tonic-gate inf_nan = 0;
10017c478bd9Sstevel@tonic-gate }
10027c478bd9Sstevel@tonic-gate
10037c478bd9Sstevel@tonic-gate /* Calculate number of padding blanks */
10047c478bd9Sstevel@tonic-gate k = (int)(pdiff = p - bp) + prefixlength + otherlength +
10057c478bd9Sstevel@tonic-gate NaN_flg;
10067c478bd9Sstevel@tonic-gate if (width <= k)
10077c478bd9Sstevel@tonic-gate count += k;
10087c478bd9Sstevel@tonic-gate else {
10097c478bd9Sstevel@tonic-gate count += width;
10107c478bd9Sstevel@tonic-gate
10117c478bd9Sstevel@tonic-gate /* Set up for padding zeroes if requested */
10127c478bd9Sstevel@tonic-gate /* Otherwise emit padding blanks unless output is */
10137c478bd9Sstevel@tonic-gate /* to be left-justified. */
10147c478bd9Sstevel@tonic-gate
10157c478bd9Sstevel@tonic-gate if (flagword & PADZERO) {
10167c478bd9Sstevel@tonic-gate if (!(flagword & LZERO)) {
10177c478bd9Sstevel@tonic-gate flagword |= LZERO;
10187c478bd9Sstevel@tonic-gate lzero = width - k;
10197c478bd9Sstevel@tonic-gate }
10207c478bd9Sstevel@tonic-gate else
10217c478bd9Sstevel@tonic-gate lzero += width - k;
10227c478bd9Sstevel@tonic-gate k = width; /* cancel padding blanks */
10237c478bd9Sstevel@tonic-gate } else
10247c478bd9Sstevel@tonic-gate /* Blanks on left if required */
10257c478bd9Sstevel@tonic-gate if (!(flagword & FMINUS))
10267c478bd9Sstevel@tonic-gate PAD(_blanks, width - k);
10277c478bd9Sstevel@tonic-gate }
10287c478bd9Sstevel@tonic-gate
10297c478bd9Sstevel@tonic-gate /* Prefix, if any */
10307c478bd9Sstevel@tonic-gate if (prefixlength != 0)
10317c478bd9Sstevel@tonic-gate PUT(prefix, prefixlength);
10327c478bd9Sstevel@tonic-gate
10337c478bd9Sstevel@tonic-gate /* If value is NaN, put string NaN */
10347c478bd9Sstevel@tonic-gate if (NaN_flg) {
10357c478bd9Sstevel@tonic-gate PUT(SNAN, SNLEN);
10367c478bd9Sstevel@tonic-gate NaN_flg = 0;
10377c478bd9Sstevel@tonic-gate }
10387c478bd9Sstevel@tonic-gate
10397c478bd9Sstevel@tonic-gate /* Zeroes on the left */
10407c478bd9Sstevel@tonic-gate if (flagword & LZERO)
10417c478bd9Sstevel@tonic-gate PAD(_zeroes, lzero);
10427c478bd9Sstevel@tonic-gate
10437c478bd9Sstevel@tonic-gate /* The value itself */
10447c478bd9Sstevel@tonic-gate if (pdiff > 0)
10457c478bd9Sstevel@tonic-gate PUT(bp, pdiff);
10467c478bd9Sstevel@tonic-gate
10477c478bd9Sstevel@tonic-gate if (flagword & (RZERO | SUFFIX | FMINUS)) {
10487c478bd9Sstevel@tonic-gate /* Zeroes on the right */
10497c478bd9Sstevel@tonic-gate if (flagword & RZERO)
10507c478bd9Sstevel@tonic-gate PAD(_zeroes, rzero);
10517c478bd9Sstevel@tonic-gate
10527c478bd9Sstevel@tonic-gate /* The suffix */
10537c478bd9Sstevel@tonic-gate if (flagword & SUFFIX)
10547c478bd9Sstevel@tonic-gate PUT(suffix, suffixlength);
10557c478bd9Sstevel@tonic-gate
10567c478bd9Sstevel@tonic-gate /* Blanks on the right if required */
10577c478bd9Sstevel@tonic-gate if (flagword & FMINUS && width > k)
10587c478bd9Sstevel@tonic-gate PAD(_blanks, width - k);
10597c478bd9Sstevel@tonic-gate }
10607c478bd9Sstevel@tonic-gate }
10617c478bd9Sstevel@tonic-gate }
10627c478bd9Sstevel@tonic-gate
10637c478bd9Sstevel@tonic-gate /*
10647c478bd9Sstevel@tonic-gate * This function initializes arglst, to contain the appropriate va_list values
10657c478bd9Sstevel@tonic-gate * for the first MAXARGS arguments.
10667c478bd9Sstevel@tonic-gate */
10677c478bd9Sstevel@tonic-gate void
_mkarglst(char * fmt,stva_list args,stva_list arglst[])10687c478bd9Sstevel@tonic-gate _mkarglst(char *fmt, stva_list args, stva_list arglst[])
10697c478bd9Sstevel@tonic-gate {
10707c478bd9Sstevel@tonic-gate static char digits[] = "01234567890", skips[] = "# +-.0123456789hL$";
10717c478bd9Sstevel@tonic-gate
10727c478bd9Sstevel@tonic-gate enum types {INT = 1, LONG, CHAR_PTR, DOUBLE, LONG_DOUBLE, VOID_PTR,
10737c478bd9Sstevel@tonic-gate LONG_PTR, INT_PTR};
10747c478bd9Sstevel@tonic-gate enum types typelst[MAXARGS], curtype;
10757c478bd9Sstevel@tonic-gate int maxnum, n, curargno, flags;
10767c478bd9Sstevel@tonic-gate
10777c478bd9Sstevel@tonic-gate /*
10787c478bd9Sstevel@tonic-gate * Algorithm 1. set all argument types to zero.
10797c478bd9Sstevel@tonic-gate * 2. walk through fmt putting arg types in typelst[].
10807c478bd9Sstevel@tonic-gate * 3. walk through args using va_arg(args.ap, typelst[n])
10817c478bd9Sstevel@tonic-gate * and set arglst[] to the appropriate values.
10827c478bd9Sstevel@tonic-gate * Assumptions: Cannot use %*$... to specify variable position.
10837c478bd9Sstevel@tonic-gate */
10847c478bd9Sstevel@tonic-gate
10857c478bd9Sstevel@tonic-gate (void) memset((void *)typelst, 0, sizeof (typelst));
10867c478bd9Sstevel@tonic-gate maxnum = -1;
10877c478bd9Sstevel@tonic-gate curargno = 0;
10887c478bd9Sstevel@tonic-gate while ((fmt = strchr(fmt, '%')) != 0) {
10897c478bd9Sstevel@tonic-gate size_t sz;
10907c478bd9Sstevel@tonic-gate
10917c478bd9Sstevel@tonic-gate fmt++; /* skip % */
10927c478bd9Sstevel@tonic-gate if (fmt[sz = strspn(fmt, digits)] == '$') {
10937c478bd9Sstevel@tonic-gate curargno = atoi(fmt) - 1;
10947c478bd9Sstevel@tonic-gate /* convert to zero base */
10957c478bd9Sstevel@tonic-gate if (curargno < 0)
10967c478bd9Sstevel@tonic-gate continue;
10977c478bd9Sstevel@tonic-gate fmt += sz + 1;
10987c478bd9Sstevel@tonic-gate }
10997c478bd9Sstevel@tonic-gate flags = 0;
11007c478bd9Sstevel@tonic-gate again:;
11017c478bd9Sstevel@tonic-gate fmt += strspn(fmt, skips);
11027c478bd9Sstevel@tonic-gate switch (*fmt++) {
11037c478bd9Sstevel@tonic-gate case '%': /* there is no argument! */
11047c478bd9Sstevel@tonic-gate continue;
11057c478bd9Sstevel@tonic-gate case 'l':
11067c478bd9Sstevel@tonic-gate flags |= 0x1;
11077c478bd9Sstevel@tonic-gate goto again;
11087c478bd9Sstevel@tonic-gate case '*': /* int argument used for value */
11097c478bd9Sstevel@tonic-gate /* check if there is a positional parameter */
11107c478bd9Sstevel@tonic-gate if (isdigit(*fmt)) {
11117c478bd9Sstevel@tonic-gate int targno;
11127c478bd9Sstevel@tonic-gate targno = atoi(fmt) - 1;
11137c478bd9Sstevel@tonic-gate fmt += strspn(fmt, digits);
11147c478bd9Sstevel@tonic-gate if (*fmt == '$')
11157c478bd9Sstevel@tonic-gate fmt++; /* skip '$' */
11167c478bd9Sstevel@tonic-gate if (targno >= 0 && targno < MAXARGS) {
11177c478bd9Sstevel@tonic-gate typelst[targno] = INT;
11187c478bd9Sstevel@tonic-gate if (maxnum < targno)
11197c478bd9Sstevel@tonic-gate maxnum = targno;
11207c478bd9Sstevel@tonic-gate }
11217c478bd9Sstevel@tonic-gate goto again;
11227c478bd9Sstevel@tonic-gate }
11237c478bd9Sstevel@tonic-gate flags |= 0x2;
11247c478bd9Sstevel@tonic-gate curtype = INT;
11257c478bd9Sstevel@tonic-gate break;
11267c478bd9Sstevel@tonic-gate case 'e':
11277c478bd9Sstevel@tonic-gate case 'E':
11287c478bd9Sstevel@tonic-gate case 'f':
11297c478bd9Sstevel@tonic-gate case 'g':
11307c478bd9Sstevel@tonic-gate case 'G':
11317c478bd9Sstevel@tonic-gate curtype = DOUBLE;
11327c478bd9Sstevel@tonic-gate break;
11337c478bd9Sstevel@tonic-gate case 's':
11347c478bd9Sstevel@tonic-gate curtype = CHAR_PTR;
11357c478bd9Sstevel@tonic-gate break;
11367c478bd9Sstevel@tonic-gate case 'p':
11377c478bd9Sstevel@tonic-gate curtype = VOID_PTR;
11387c478bd9Sstevel@tonic-gate break;
11397c478bd9Sstevel@tonic-gate case 'n':
11407c478bd9Sstevel@tonic-gate if (flags & 0x1)
11417c478bd9Sstevel@tonic-gate curtype = LONG_PTR;
11427c478bd9Sstevel@tonic-gate else
11437c478bd9Sstevel@tonic-gate curtype = INT_PTR;
11447c478bd9Sstevel@tonic-gate break;
11457c478bd9Sstevel@tonic-gate default:
11467c478bd9Sstevel@tonic-gate if (flags & 0x1)
11477c478bd9Sstevel@tonic-gate curtype = LONG;
11487c478bd9Sstevel@tonic-gate else
11497c478bd9Sstevel@tonic-gate curtype = INT;
11507c478bd9Sstevel@tonic-gate break;
11517c478bd9Sstevel@tonic-gate }
11527c478bd9Sstevel@tonic-gate if (curargno >= 0 && curargno < MAXARGS) {
11537c478bd9Sstevel@tonic-gate typelst[curargno] = curtype;
11547c478bd9Sstevel@tonic-gate if (maxnum < curargno)
11557c478bd9Sstevel@tonic-gate maxnum = curargno;
11567c478bd9Sstevel@tonic-gate }
11577c478bd9Sstevel@tonic-gate curargno++; /* default to next in list */
11587c478bd9Sstevel@tonic-gate if (flags & 0x2) /* took care of *, keep going */
11597c478bd9Sstevel@tonic-gate {
11607c478bd9Sstevel@tonic-gate flags ^= 0x2;
11617c478bd9Sstevel@tonic-gate goto again;
11627c478bd9Sstevel@tonic-gate }
11637c478bd9Sstevel@tonic-gate }
11647c478bd9Sstevel@tonic-gate for (n = 0; n <= maxnum; n++) {
11657c478bd9Sstevel@tonic-gate arglst[n] = args;
11667c478bd9Sstevel@tonic-gate if (typelst[n] == 0)
11677c478bd9Sstevel@tonic-gate typelst[n] = INT;
11687c478bd9Sstevel@tonic-gate
11697c478bd9Sstevel@tonic-gate switch (typelst[n]) {
11707c478bd9Sstevel@tonic-gate case INT:
11717c478bd9Sstevel@tonic-gate (void) va_arg(args.ap, int);
11727c478bd9Sstevel@tonic-gate break;
11737c478bd9Sstevel@tonic-gate case LONG:
11747c478bd9Sstevel@tonic-gate (void) va_arg(args.ap, long);
11757c478bd9Sstevel@tonic-gate break;
11767c478bd9Sstevel@tonic-gate case CHAR_PTR:
11777c478bd9Sstevel@tonic-gate (void) va_arg(args.ap, char *);
11787c478bd9Sstevel@tonic-gate break;
11797c478bd9Sstevel@tonic-gate case DOUBLE:
11807c478bd9Sstevel@tonic-gate (void) va_arg(args.ap, double);
11817c478bd9Sstevel@tonic-gate break;
11827c478bd9Sstevel@tonic-gate case LONG_DOUBLE:
11837c478bd9Sstevel@tonic-gate (void) va_arg(args.ap, double);
11847c478bd9Sstevel@tonic-gate break;
11857c478bd9Sstevel@tonic-gate case VOID_PTR:
11867c478bd9Sstevel@tonic-gate (void) va_arg(args.ap, void *);
11877c478bd9Sstevel@tonic-gate break;
11887c478bd9Sstevel@tonic-gate case LONG_PTR:
11897c478bd9Sstevel@tonic-gate (void) va_arg(args.ap, long *);
11907c478bd9Sstevel@tonic-gate break;
11917c478bd9Sstevel@tonic-gate case INT_PTR:
11927c478bd9Sstevel@tonic-gate (void) va_arg(args.ap, int *);
11937c478bd9Sstevel@tonic-gate break;
11947c478bd9Sstevel@tonic-gate }
11957c478bd9Sstevel@tonic-gate }
11967c478bd9Sstevel@tonic-gate }
11977c478bd9Sstevel@tonic-gate
11987c478bd9Sstevel@tonic-gate /*
11997c478bd9Sstevel@tonic-gate * This function is used to find the va_list value for arguments whose
12007c478bd9Sstevel@tonic-gate * position is greater than MAXARGS. This function is slow, so hopefully
12017c478bd9Sstevel@tonic-gate * MAXARGS will be big enough so that this function need only be called in
12027c478bd9Sstevel@tonic-gate * unusual circumstances.
12037c478bd9Sstevel@tonic-gate * pargs is assumed to contain the value of arglst[MAXARGS - 1].
12047c478bd9Sstevel@tonic-gate */
12057c478bd9Sstevel@tonic-gate void
_getarg(char * fmt,stva_list * pargs,int argno)12067c478bd9Sstevel@tonic-gate _getarg(char *fmt, stva_list *pargs, int argno)
12077c478bd9Sstevel@tonic-gate {
12087c478bd9Sstevel@tonic-gate static char digits[] = "01234567890", skips[] = "# +-.0123456789h$";
12097c478bd9Sstevel@tonic-gate int i, curargno, flags;
12107c478bd9Sstevel@tonic-gate size_t n;
12117c478bd9Sstevel@tonic-gate char *sfmt = fmt;
12127c478bd9Sstevel@tonic-gate int found = 1;
12137c478bd9Sstevel@tonic-gate
12147c478bd9Sstevel@tonic-gate i = MAXARGS;
12157c478bd9Sstevel@tonic-gate curargno = 1;
12167c478bd9Sstevel@tonic-gate while (found) {
12177c478bd9Sstevel@tonic-gate fmt = sfmt;
12187c478bd9Sstevel@tonic-gate found = 0;
12197c478bd9Sstevel@tonic-gate while ((i != argno) && (fmt = strchr(fmt, '%')) != 0) {
12207c478bd9Sstevel@tonic-gate fmt++; /* skip % */
12217c478bd9Sstevel@tonic-gate if (fmt[n = strspn(fmt, digits)] == '$') {
12227c478bd9Sstevel@tonic-gate curargno = atoi(fmt);
12237c478bd9Sstevel@tonic-gate if (curargno <= 0)
12247c478bd9Sstevel@tonic-gate continue;
12257c478bd9Sstevel@tonic-gate fmt += n + 1;
12267c478bd9Sstevel@tonic-gate }
12277c478bd9Sstevel@tonic-gate
12287c478bd9Sstevel@tonic-gate /* find conversion specifier for next argument */
12297c478bd9Sstevel@tonic-gate if (i != curargno) {
12307c478bd9Sstevel@tonic-gate curargno++;
12317c478bd9Sstevel@tonic-gate continue;
12327c478bd9Sstevel@tonic-gate } else
12337c478bd9Sstevel@tonic-gate found = 1;
12347c478bd9Sstevel@tonic-gate flags = 0;
12357c478bd9Sstevel@tonic-gate again:;
12367c478bd9Sstevel@tonic-gate fmt += strspn(fmt, skips);
12377c478bd9Sstevel@tonic-gate switch (*fmt++) {
12387c478bd9Sstevel@tonic-gate case '%': /* there is no argument! */
12397c478bd9Sstevel@tonic-gate continue;
12407c478bd9Sstevel@tonic-gate case 'l':
12417c478bd9Sstevel@tonic-gate flags |= 0x1;
12427c478bd9Sstevel@tonic-gate goto again;
12437c478bd9Sstevel@tonic-gate case '*': /* int argument used for value */
12447c478bd9Sstevel@tonic-gate /*
12457c478bd9Sstevel@tonic-gate * check if there is a positional parameter;
12467c478bd9Sstevel@tonic-gate * if so, just skip it; its size will be
12477c478bd9Sstevel@tonic-gate * correctly determined by default
12487c478bd9Sstevel@tonic-gate */
12497c478bd9Sstevel@tonic-gate if (isdigit(*fmt)) {
12507c478bd9Sstevel@tonic-gate fmt += strspn(fmt, digits);
12517c478bd9Sstevel@tonic-gate if (*fmt == '$')
12527c478bd9Sstevel@tonic-gate fmt++; /* skip '$' */
12537c478bd9Sstevel@tonic-gate goto again;
12547c478bd9Sstevel@tonic-gate }
12557c478bd9Sstevel@tonic-gate flags |= 0x2;
12567c478bd9Sstevel@tonic-gate (void) va_arg((*pargs).ap, int);
12577c478bd9Sstevel@tonic-gate break;
12587c478bd9Sstevel@tonic-gate case 'e':
12597c478bd9Sstevel@tonic-gate case 'E':
12607c478bd9Sstevel@tonic-gate case 'f':
12617c478bd9Sstevel@tonic-gate case 'g':
12627c478bd9Sstevel@tonic-gate case 'G':
12637c478bd9Sstevel@tonic-gate if (flags & 0x1)
12647c478bd9Sstevel@tonic-gate (void) va_arg((*pargs).ap, double);
12657c478bd9Sstevel@tonic-gate else
12667c478bd9Sstevel@tonic-gate (void) va_arg((*pargs).ap, double);
12677c478bd9Sstevel@tonic-gate break;
12687c478bd9Sstevel@tonic-gate case 's':
12697c478bd9Sstevel@tonic-gate (void) va_arg((*pargs).ap, char *);
12707c478bd9Sstevel@tonic-gate break;
12717c478bd9Sstevel@tonic-gate case 'p':
12727c478bd9Sstevel@tonic-gate (void) va_arg((*pargs).ap, void *);
12737c478bd9Sstevel@tonic-gate break;
12747c478bd9Sstevel@tonic-gate case 'n':
12757c478bd9Sstevel@tonic-gate if (flags & 0x1)
12767c478bd9Sstevel@tonic-gate (void) va_arg((*pargs).ap, long *);
12777c478bd9Sstevel@tonic-gate else
12787c478bd9Sstevel@tonic-gate (void) va_arg((*pargs).ap, int *);
12797c478bd9Sstevel@tonic-gate break;
12807c478bd9Sstevel@tonic-gate default:
12817c478bd9Sstevel@tonic-gate if (flags & 0x1)
12827c478bd9Sstevel@tonic-gate (void) va_arg((*pargs).ap, long int);
12837c478bd9Sstevel@tonic-gate else
12847c478bd9Sstevel@tonic-gate (void) va_arg((*pargs).ap, int);
12857c478bd9Sstevel@tonic-gate break;
12867c478bd9Sstevel@tonic-gate }
12877c478bd9Sstevel@tonic-gate i++;
12887c478bd9Sstevel@tonic-gate curargno++; /* default to next in list */
12897c478bd9Sstevel@tonic-gate if (flags & 0x2) /* took care of *, keep going */
12907c478bd9Sstevel@tonic-gate {
12917c478bd9Sstevel@tonic-gate flags ^= 0x2;
12927c478bd9Sstevel@tonic-gate goto again;
12937c478bd9Sstevel@tonic-gate }
12947c478bd9Sstevel@tonic-gate }
12957c478bd9Sstevel@tonic-gate
12967c478bd9Sstevel@tonic-gate /*
12977c478bd9Sstevel@tonic-gate * missing specifier for parameter, assume parameter is an int
12987c478bd9Sstevel@tonic-gate */
12997c478bd9Sstevel@tonic-gate if (!found && i != argno) {
13007c478bd9Sstevel@tonic-gate (void) va_arg((*pargs).ap, int);
13017c478bd9Sstevel@tonic-gate i++;
13027c478bd9Sstevel@tonic-gate curargno = i;
13037c478bd9Sstevel@tonic-gate found = 1;
13047c478bd9Sstevel@tonic-gate }
13057c478bd9Sstevel@tonic-gate }
13067c478bd9Sstevel@tonic-gate }
1307