Home | History | Annotate | Line # | Download | only in stdio
vsnprintf_ss.c revision 1.9
      1  1.9  christos /*	$NetBSD: vsnprintf_ss.c,v 1.9 2009/12/17 15:19:48 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 1990, 1993
      5  1.1  christos  *	The Regents of the University of California.  All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * This code is derived from software contributed to Berkeley by
      8  1.1  christos  * Chris Torek.
      9  1.1  christos  *
     10  1.1  christos  * Redistribution and use in source and binary forms, with or without
     11  1.1  christos  * modification, are permitted provided that the following conditions
     12  1.1  christos  * are met:
     13  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  christos  *    documentation and/or other materials provided with the distribution.
     18  1.1  christos  * 3. Neither the name of the University nor the names of its contributors
     19  1.1  christos  *    may be used to endorse or promote products derived from this software
     20  1.1  christos  *    without specific prior written permission.
     21  1.1  christos  *
     22  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.1  christos  * SUCH DAMAGE.
     33  1.1  christos  */
     34  1.1  christos 
     35  1.1  christos #include <sys/cdefs.h>
     36  1.1  christos #if defined(LIBC_SCCS) && !defined(lint)
     37  1.1  christos #if 0
     38  1.1  christos static char sccsid[] = "@(#)vsnprintf.c	8.1 (Berkeley) 6/4/93";
     39  1.1  christos #else
     40  1.9  christos __RCSID("$NetBSD: vsnprintf_ss.c,v 1.9 2009/12/17 15:19:48 christos Exp $");
     41  1.1  christos #endif
     42  1.1  christos #endif /* LIBC_SCCS and not lint */
     43  1.1  christos 
     44  1.1  christos #include "namespace.h"
     45  1.1  christos 
     46  1.3  christos #include <sys/types.h>
     47  1.3  christos #include <inttypes.h>
     48  1.1  christos #include <assert.h>
     49  1.3  christos #include <stdio.h>
     50  1.1  christos #include <errno.h>
     51  1.3  christos #include <stdarg.h>
     52  1.3  christos #include <string.h>
     53  1.1  christos #include "reentrant.h"
     54  1.2  christos #include "extern.h"
     55  1.1  christos #include "local.h"
     56  1.1  christos 
     57  1.1  christos #ifdef __weak_alias
     58  1.1  christos __weak_alias(vsnprintf_ss,_vsnprintf_ss)
     59  1.1  christos #endif
     60  1.1  christos 
     61  1.3  christos /*
     62  1.3  christos  * vsnprintf_ss: scaled down version of printf(3).
     63  1.3  christos  *
     64  1.3  christos  * this version based on vfprintf() from libc which was derived from
     65  1.3  christos  * software contributed to Berkeley by Chris Torek.
     66  1.3  christos  *
     67  1.3  christos  */
     68  1.3  christos 
     69  1.3  christos /*
     70  1.3  christos  * macros for converting digits to letters and vice versa
     71  1.3  christos  */
     72  1.3  christos #define	to_digit(c)	((c) - '0')
     73  1.3  christos #define is_digit(c)	((unsigned)to_digit(c) <= 9)
     74  1.3  christos #define	to_char(n)	(char)((n) + '0')
     75  1.3  christos 
     76  1.3  christos /*
     77  1.3  christos  * flags used during conversion.
     78  1.3  christos  */
     79  1.3  christos #define	ALT		0x001		/* alternate form */
     80  1.3  christos #define	HEXPREFIX	0x002		/* add 0x or 0X prefix */
     81  1.3  christos #define	LADJUST		0x004		/* left adjustment */
     82  1.3  christos #define	LONGDBL		0x008		/* long double; unimplemented */
     83  1.3  christos #define	LONGINT		0x010		/* long integer */
     84  1.3  christos #define	QUADINT		0x020		/* quad integer */
     85  1.3  christos #define	SHORTINT	0x040		/* short integer */
     86  1.3  christos #define	MAXINT		0x080		/* intmax_t */
     87  1.3  christos #define	PTRINT		0x100		/* intptr_t */
     88  1.3  christos #define	SIZEINT		0x200		/* size_t */
     89  1.3  christos #define	ZEROPAD		0x400		/* zero (as opposed to blank) pad */
     90  1.3  christos #define FPT		0x800		/* Floating point number */
     91  1.3  christos 
     92  1.3  christos 	/*
     93  1.3  christos 	 * To extend shorts properly, we need both signed and unsigned
     94  1.3  christos 	 * argument extraction methods.
     95  1.3  christos 	 */
     96  1.3  christos #define	SARG() \
     97  1.3  christos 	(flags&MAXINT ? va_arg(ap, intmax_t) : \
     98  1.3  christos 	    flags&PTRINT ? va_arg(ap, intptr_t) : \
     99  1.3  christos 	    flags&SIZEINT ? va_arg(ap, ssize_t) : /* XXX */ \
    100  1.3  christos 	    flags&QUADINT ? va_arg(ap, quad_t) : \
    101  1.3  christos 	    flags&LONGINT ? va_arg(ap, long) : \
    102  1.3  christos 	    flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
    103  1.3  christos 	    (long)va_arg(ap, int))
    104  1.3  christos #define	UARG() \
    105  1.3  christos 	(flags&MAXINT ? va_arg(ap, uintmax_t) : \
    106  1.3  christos 	    flags&PTRINT ? va_arg(ap, uintptr_t) : \
    107  1.3  christos 	    flags&SIZEINT ? va_arg(ap, size_t) : \
    108  1.3  christos 	    flags&QUADINT ? va_arg(ap, u_quad_t) : \
    109  1.3  christos 	    flags&LONGINT ? va_arg(ap, u_long) : \
    110  1.3  christos 	    flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
    111  1.3  christos 	    (u_long)va_arg(ap, u_int))
    112  1.3  christos 
    113  1.3  christos #define PUTCHAR(C) do {					\
    114  1.3  christos 	if (sbuf < tailp)				\
    115  1.3  christos 		*sbuf++ = (C);				\
    116  1.3  christos } while (/*CONSTCOND*/0)
    117  1.3  christos 
    118  1.1  christos int
    119  1.3  christos vsnprintf_ss(char *sbuf, size_t slen, const char *fmt0, _BSD_VA_LIST_ ap)
    120  1.1  christos {
    121  1.3  christos 	const char *fmt;	/* format string */
    122  1.3  christos 	int ch;			/* character from fmt */
    123  1.3  christos 	int n;			/* handy integer (short term usage) */
    124  1.3  christos 	char *cp;		/* handy char pointer (short term usage) */
    125  1.3  christos 	int flags;		/* flags as above */
    126  1.3  christos 	int ret;		/* return value accumulator */
    127  1.3  christos 	int width;		/* width from format (%8d), or 0 */
    128  1.3  christos 	int prec;		/* precision from format (%.3d), or -1 */
    129  1.3  christos 	char sign;		/* sign prefix (' ', '+', '-', or \0) */
    130  1.3  christos 
    131  1.3  christos 	u_quad_t _uquad;	/* integer arguments %[diouxX] */
    132  1.3  christos 	enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
    133  1.3  christos 	int dprec;		/* a copy of prec if [diouxX], 0 otherwise */
    134  1.3  christos 	int realsz;		/* field size expanded by dprec */
    135  1.3  christos 	int size;		/* size of converted field or string */
    136  1.3  christos 	const char *xdigs;	/* digits for [xX] conversion */
    137  1.3  christos 	char bf[128]; 		/* space for %c, %[diouxX] */
    138  1.3  christos 	char *tailp;		/* tail pointer for snprintf */
    139  1.3  christos 
    140  1.3  christos 	static const char xdigs_lower[16] = "0123456789abcdef";
    141  1.3  christos 	static const char xdigs_upper[16] = "0123456789ABCDEF";
    142  1.1  christos 
    143  1.5  christos 	_DIAGASSERT(slen == 0 || sbuf != NULL);
    144  1.6  christos 	_DIAGASSERT(fmt0 != NULL);
    145  1.1  christos 
    146  1.5  christos 	if ((int)slen < 0) {
    147  1.4  christos 		errno = EINVAL;
    148  1.4  christos 		return (-1);
    149  1.4  christos 	}
    150  1.4  christos 
    151  1.3  christos 	tailp = sbuf + slen;
    152  1.3  christos 
    153  1.3  christos 	cp = NULL;	/* XXX: shutup gcc */
    154  1.3  christos 	size = 0;	/* XXX: shutup gcc */
    155  1.3  christos 
    156  1.3  christos 	fmt = fmt0;
    157  1.3  christos 	ret = 0;
    158  1.3  christos 
    159  1.3  christos 	xdigs = NULL;		/* XXX: shut up gcc warning */
    160  1.3  christos 
    161  1.3  christos 	/*
    162  1.3  christos 	 * Scan the format for conversions (`%' character).
    163  1.3  christos 	 */
    164  1.3  christos 	for (;;) {
    165  1.3  christos 		while (*fmt != '%' && *fmt) {
    166  1.3  christos 			ret++;
    167  1.9  christos 			PUTCHAR(*fmt);
    168  1.9  christos 			fmt++;
    169  1.3  christos 		}
    170  1.3  christos 		if (*fmt == 0)
    171  1.3  christos 			goto done;
    172  1.3  christos 
    173  1.3  christos 		fmt++;		/* skip over '%' */
    174  1.3  christos 
    175  1.3  christos 		flags = 0;
    176  1.3  christos 		dprec = 0;
    177  1.3  christos 		width = 0;
    178  1.3  christos 		prec = -1;
    179  1.3  christos 		sign = '\0';
    180  1.3  christos 
    181  1.3  christos rflag:		ch = *fmt++;
    182  1.3  christos reswitch:	switch (ch) {
    183  1.3  christos 		case ' ':
    184  1.3  christos 			/*
    185  1.3  christos 			 * ``If the space and + flags both appear, the space
    186  1.3  christos 			 * flag will be ignored.''
    187  1.3  christos 			 *	-- ANSI X3J11
    188  1.3  christos 			 */
    189  1.3  christos 			if (!sign)
    190  1.3  christos 				sign = ' ';
    191  1.3  christos 			goto rflag;
    192  1.3  christos 		case '#':
    193  1.3  christos 			flags |= ALT;
    194  1.3  christos 			goto rflag;
    195  1.3  christos 		case '*':
    196  1.3  christos 			/*
    197  1.3  christos 			 * ``A negative field width argument is taken as a
    198  1.3  christos 			 * - flag followed by a positive field width.''
    199  1.3  christos 			 *	-- ANSI X3J11
    200  1.3  christos 			 * They don't exclude field widths read from args.
    201  1.3  christos 			 */
    202  1.3  christos 			if ((width = va_arg(ap, int)) >= 0)
    203  1.3  christos 				goto rflag;
    204  1.3  christos 			width = -width;
    205  1.3  christos 			/* FALLTHROUGH */
    206  1.3  christos 		case '-':
    207  1.3  christos 			flags |= LADJUST;
    208  1.3  christos 			goto rflag;
    209  1.3  christos 		case '+':
    210  1.3  christos 			sign = '+';
    211  1.3  christos 			goto rflag;
    212  1.3  christos 		case '.':
    213  1.3  christos 			if ((ch = *fmt++) == '*') {
    214  1.3  christos 				n = va_arg(ap, int);
    215  1.3  christos 				prec = n < 0 ? -1 : n;
    216  1.3  christos 				goto rflag;
    217  1.3  christos 			}
    218  1.3  christos 			n = 0;
    219  1.3  christos 			while (is_digit(ch)) {
    220  1.3  christos 				n = 10 * n + to_digit(ch);
    221  1.3  christos 				ch = *fmt++;
    222  1.3  christos 			}
    223  1.3  christos 			prec = n < 0 ? -1 : n;
    224  1.3  christos 			goto reswitch;
    225  1.3  christos 		case '0':
    226  1.3  christos 			/*
    227  1.3  christos 			 * ``Note that 0 is taken as a flag, not as the
    228  1.3  christos 			 * beginning of a field width.''
    229  1.3  christos 			 *	-- ANSI X3J11
    230  1.3  christos 			 */
    231  1.3  christos 			flags |= ZEROPAD;
    232  1.3  christos 			goto rflag;
    233  1.3  christos 		case '1': case '2': case '3': case '4':
    234  1.3  christos 		case '5': case '6': case '7': case '8': case '9':
    235  1.3  christos 			n = 0;
    236  1.3  christos 			do {
    237  1.3  christos 				n = 10 * n + to_digit(ch);
    238  1.3  christos 				ch = *fmt++;
    239  1.3  christos 			} while (is_digit(ch));
    240  1.3  christos 			width = n;
    241  1.3  christos 			goto reswitch;
    242  1.3  christos 		case 'h':
    243  1.3  christos 			flags |= SHORTINT;
    244  1.3  christos 			goto rflag;
    245  1.3  christos 		case 'j':
    246  1.3  christos 			flags |= MAXINT;
    247  1.3  christos 			goto rflag;
    248  1.3  christos 		case 'l':
    249  1.3  christos 			if (*fmt == 'l') {
    250  1.3  christos 				fmt++;
    251  1.3  christos 				flags |= QUADINT;
    252  1.3  christos 			} else {
    253  1.3  christos 				flags |= LONGINT;
    254  1.3  christos 			}
    255  1.3  christos 			goto rflag;
    256  1.3  christos 		case 'q':
    257  1.3  christos 			flags |= QUADINT;
    258  1.3  christos 			goto rflag;
    259  1.3  christos 		case 't':
    260  1.3  christos 			flags |= PTRINT;
    261  1.3  christos 			goto rflag;
    262  1.3  christos 		case 'z':
    263  1.3  christos 			flags |= SIZEINT;
    264  1.3  christos 			goto rflag;
    265  1.3  christos 		case 'c':
    266  1.3  christos 			*(cp = bf) = va_arg(ap, int);
    267  1.3  christos 			size = 1;
    268  1.3  christos 			sign = '\0';
    269  1.3  christos 			break;
    270  1.3  christos 		case 'D':
    271  1.3  christos 			flags |= LONGINT;
    272  1.3  christos 			/*FALLTHROUGH*/
    273  1.3  christos 		case 'd':
    274  1.3  christos 		case 'i':
    275  1.3  christos 			_uquad = SARG();
    276  1.3  christos 			if ((quad_t)_uquad < 0) {
    277  1.3  christos 				_uquad = -_uquad;
    278  1.3  christos 				sign = '-';
    279  1.3  christos 			}
    280  1.3  christos 			base = DEC;
    281  1.3  christos 			goto number;
    282  1.3  christos 		case 'n':
    283  1.3  christos 			if (flags & MAXINT)
    284  1.3  christos 				*va_arg(ap, intmax_t *) = ret;
    285  1.3  christos 			else if (flags & PTRINT)
    286  1.3  christos 				*va_arg(ap, intptr_t *) = ret;
    287  1.3  christos 			else if (flags & SIZEINT)
    288  1.3  christos 				*va_arg(ap, ssize_t *) = ret;
    289  1.3  christos 			else if (flags & QUADINT)
    290  1.3  christos 				*va_arg(ap, quad_t *) = ret;
    291  1.3  christos 			else if (flags & LONGINT)
    292  1.3  christos 				*va_arg(ap, long *) = ret;
    293  1.3  christos 			else if (flags & SHORTINT)
    294  1.3  christos 				*va_arg(ap, short *) = ret;
    295  1.3  christos 			else
    296  1.3  christos 				*va_arg(ap, int *) = ret;
    297  1.3  christos 			continue;	/* no output */
    298  1.3  christos 		case 'O':
    299  1.3  christos 			flags |= LONGINT;
    300  1.3  christos 			/*FALLTHROUGH*/
    301  1.3  christos 		case 'o':
    302  1.3  christos 			_uquad = UARG();
    303  1.3  christos 			base = OCT;
    304  1.3  christos 			goto nosign;
    305  1.3  christos 		case 'p':
    306  1.3  christos 			/*
    307  1.3  christos 			 * ``The argument shall be a pointer to void.  The
    308  1.3  christos 			 * value of the pointer is converted to a sequence
    309  1.3  christos 			 * of printable characters, in an implementation-
    310  1.3  christos 			 * defined manner.''
    311  1.3  christos 			 *	-- ANSI X3J11
    312  1.3  christos 			 */
    313  1.3  christos 			/* NOSTRICT */
    314  1.3  christos 			_uquad = (u_long)va_arg(ap, void *);
    315  1.3  christos 			base = HEX;
    316  1.3  christos 			xdigs = xdigs_lower;
    317  1.3  christos 			flags |= HEXPREFIX;
    318  1.3  christos 			ch = 'x';
    319  1.3  christos 			goto nosign;
    320  1.3  christos 		case 's':
    321  1.3  christos 			if ((cp = va_arg(ap, char *)) == NULL)
    322  1.3  christos 				/*XXXUNCONST*/
    323  1.3  christos 				cp = __UNCONST("(null)");
    324  1.3  christos 			if (prec >= 0) {
    325  1.3  christos 				/*
    326  1.3  christos 				 * can't use strlen; can only look for the
    327  1.3  christos 				 * NUL in the first `prec' characters, and
    328  1.3  christos 				 * strlen() will go further.
    329  1.3  christos 				 */
    330  1.8  christos 				char *p = memchr(cp, 0, (size_t)prec);
    331  1.3  christos 
    332  1.3  christos 				if (p != NULL) {
    333  1.3  christos 					size = p - cp;
    334  1.3  christos 					if (size > prec)
    335  1.3  christos 						size = prec;
    336  1.3  christos 				} else
    337  1.3  christos 					size = prec;
    338  1.3  christos 			} else
    339  1.3  christos 				size = strlen(cp);
    340  1.3  christos 			sign = '\0';
    341  1.3  christos 			break;
    342  1.3  christos 		case 'U':
    343  1.3  christos 			flags |= LONGINT;
    344  1.3  christos 			/*FALLTHROUGH*/
    345  1.3  christos 		case 'u':
    346  1.3  christos 			_uquad = UARG();
    347  1.3  christos 			base = DEC;
    348  1.3  christos 			goto nosign;
    349  1.3  christos 		case 'X':
    350  1.3  christos 			xdigs = xdigs_upper;
    351  1.3  christos 			goto hex;
    352  1.3  christos 		case 'x':
    353  1.3  christos 			xdigs = xdigs_lower;
    354  1.3  christos hex:			_uquad = UARG();
    355  1.3  christos 			base = HEX;
    356  1.3  christos 			/* leading 0x/X only if non-zero */
    357  1.3  christos 			if (flags & ALT && _uquad != 0)
    358  1.3  christos 				flags |= HEXPREFIX;
    359  1.3  christos 
    360  1.3  christos 			/* unsigned conversions */
    361  1.3  christos nosign:			sign = '\0';
    362  1.3  christos 			/*
    363  1.3  christos 			 * ``... diouXx conversions ... if a precision is
    364  1.3  christos 			 * specified, the 0 flag will be ignored.''
    365  1.3  christos 			 *	-- ANSI X3J11
    366  1.3  christos 			 */
    367  1.3  christos number:			if ((dprec = prec) >= 0)
    368  1.3  christos 				flags &= ~ZEROPAD;
    369  1.3  christos 
    370  1.3  christos 			/*
    371  1.3  christos 			 * ``The result of converting a zero value with an
    372  1.3  christos 			 * explicit precision of zero is no characters.''
    373  1.3  christos 			 *	-- ANSI X3J11
    374  1.3  christos 			 */
    375  1.3  christos 			cp = bf + sizeof(bf);
    376  1.3  christos 			if (_uquad != 0 || prec != 0) {
    377  1.3  christos 				/*
    378  1.3  christos 				 * Unsigned mod is hard, and unsigned mod
    379  1.3  christos 				 * by a constant is easier than that by
    380  1.3  christos 				 * a variable; hence this switch.
    381  1.3  christos 				 */
    382  1.3  christos 				switch (base) {
    383  1.3  christos 				case OCT:
    384  1.3  christos 					do {
    385  1.3  christos 						*--cp = to_char(_uquad & 7);
    386  1.3  christos 						_uquad >>= 3;
    387  1.3  christos 					} while (_uquad);
    388  1.3  christos 					/* handle octal leading 0 */
    389  1.3  christos 					if (flags & ALT && *cp != '0')
    390  1.3  christos 						*--cp = '0';
    391  1.3  christos 					break;
    392  1.3  christos 
    393  1.3  christos 				case DEC:
    394  1.3  christos 					/* many numbers are 1 digit */
    395  1.3  christos 					while (_uquad >= 10) {
    396  1.3  christos 						*--cp = to_char(_uquad % 10);
    397  1.3  christos 						_uquad /= 10;
    398  1.3  christos 					}
    399  1.3  christos 					*--cp = to_char(_uquad);
    400  1.3  christos 					break;
    401  1.3  christos 
    402  1.3  christos 				case HEX:
    403  1.3  christos 					do {
    404  1.8  christos 						*--cp = xdigs[(size_t)_uquad & 15];
    405  1.3  christos 						_uquad >>= 4;
    406  1.3  christos 					} while (_uquad);
    407  1.3  christos 					break;
    408  1.3  christos 
    409  1.3  christos 				default:
    410  1.3  christos 					/*XXXUNCONST*/
    411  1.3  christos 					cp = __UNCONST("bug bad base");
    412  1.3  christos 					size = strlen(cp);
    413  1.3  christos 					goto skipsize;
    414  1.3  christos 				}
    415  1.3  christos 			}
    416  1.3  christos 			size = bf + sizeof(bf) - cp;
    417  1.3  christos 		skipsize:
    418  1.3  christos 			break;
    419  1.3  christos 		default:	/* "%?" prints ?, unless ? is NUL */
    420  1.3  christos 			if (ch == '\0')
    421  1.3  christos 				goto done;
    422  1.3  christos 			/* pretend it was %c with argument ch */
    423  1.3  christos 			cp = bf;
    424  1.3  christos 			*cp = ch;
    425  1.3  christos 			size = 1;
    426  1.3  christos 			sign = '\0';
    427  1.3  christos 			break;
    428  1.3  christos 		}
    429  1.3  christos 
    430  1.3  christos 		/*
    431  1.3  christos 		 * All reasonable formats wind up here.  At this point, `cp'
    432  1.3  christos 		 * points to a string which (if not flags&LADJUST) should be
    433  1.3  christos 		 * padded out to `width' places.  If flags&ZEROPAD, it should
    434  1.3  christos 		 * first be prefixed by any sign or other prefix; otherwise,
    435  1.3  christos 		 * it should be blank padded before the prefix is emitted.
    436  1.3  christos 		 * After any left-hand padding and prefixing, emit zeroes
    437  1.3  christos 		 * required by a decimal [diouxX] precision, then print the
    438  1.3  christos 		 * string proper, then emit zeroes required by any leftover
    439  1.3  christos 		 * floating precision; finally, if LADJUST, pad with blanks.
    440  1.3  christos 		 *
    441  1.3  christos 		 * Compute actual size, so we know how much to pad.
    442  1.3  christos 		 * size excludes decimal prec; realsz includes it.
    443  1.3  christos 		 */
    444  1.3  christos 		realsz = dprec > size ? dprec : size;
    445  1.3  christos 		if (sign)
    446  1.3  christos 			realsz++;
    447  1.3  christos 		else if (flags & HEXPREFIX)
    448  1.3  christos 			realsz+= 2;
    449  1.3  christos 
    450  1.3  christos 		/* adjust ret */
    451  1.3  christos 		ret += width > realsz ? width : realsz;
    452  1.3  christos 
    453  1.3  christos 		/* right-adjusting blank padding */
    454  1.3  christos 		if ((flags & (LADJUST|ZEROPAD)) == 0) {
    455  1.3  christos 			n = width - realsz;
    456  1.3  christos 			while (n-- > 0)
    457  1.3  christos 				PUTCHAR(' ');
    458  1.3  christos 		}
    459  1.3  christos 
    460  1.3  christos 		/* prefix */
    461  1.3  christos 		if (sign) {
    462  1.3  christos 			PUTCHAR(sign);
    463  1.3  christos 		} else if (flags & HEXPREFIX) {
    464  1.3  christos 			PUTCHAR('0');
    465  1.3  christos 			PUTCHAR(ch);
    466  1.3  christos 		}
    467  1.3  christos 
    468  1.3  christos 		/* right-adjusting zero padding */
    469  1.3  christos 		if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
    470  1.3  christos 			n = width - realsz;
    471  1.3  christos 			while (n-- > 0)
    472  1.3  christos 				PUTCHAR('0');
    473  1.3  christos 		}
    474  1.3  christos 
    475  1.3  christos 		/* leading zeroes from decimal precision */
    476  1.3  christos 		n = dprec - size;
    477  1.3  christos 		while (n-- > 0)
    478  1.3  christos 			PUTCHAR('0');
    479  1.3  christos 
    480  1.3  christos 		/* the string or number proper */
    481  1.3  christos 		while (size--)
    482  1.3  christos 			PUTCHAR(*cp++);
    483  1.3  christos 		/* left-adjusting padding (always blank) */
    484  1.3  christos 		if (flags & LADJUST) {
    485  1.3  christos 			n = width - realsz;
    486  1.3  christos 			while (n-- > 0)
    487  1.3  christos 				PUTCHAR(' ');
    488  1.3  christos 		}
    489  1.1  christos 	}
    490  1.1  christos 
    491  1.3  christos done:
    492  1.3  christos 	if (sbuf == tailp)
    493  1.3  christos 		sbuf[-1] = '\0';
    494  1.3  christos 	else
    495  1.3  christos 		*sbuf = '\0';
    496  1.1  christos 	return (ret);
    497  1.3  christos 	/* NOTREACHED */
    498  1.1  christos }
    499