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