xref: /minix3/lib/libc/stdio/vsnprintf_ss.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: vsnprintf_ss.c,v 1.13 2014/09/29 14:58:33 christos Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras  * Copyright (c) 1990, 1993
52fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * This code is derived from software contributed to Berkeley by
82fe8fb19SBen Gras  * Chris Torek.
92fe8fb19SBen Gras  *
102fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras  * are met:
132fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
192fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
202fe8fb19SBen Gras  *    without specific prior written permission.
212fe8fb19SBen Gras  *
222fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322fe8fb19SBen Gras  * SUCH DAMAGE.
332fe8fb19SBen Gras  */
342fe8fb19SBen Gras 
352fe8fb19SBen Gras #include <sys/cdefs.h>
362fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
372fe8fb19SBen Gras #if 0
382fe8fb19SBen Gras static char sccsid[] = "@(#)vsnprintf.c	8.1 (Berkeley) 6/4/93";
392fe8fb19SBen Gras #else
40*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: vsnprintf_ss.c,v 1.13 2014/09/29 14:58:33 christos Exp $");
412fe8fb19SBen Gras #endif
422fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
432fe8fb19SBen Gras 
442fe8fb19SBen Gras #include "namespace.h"
452fe8fb19SBen Gras 
462fe8fb19SBen Gras #include <sys/types.h>
472fe8fb19SBen Gras #include <inttypes.h>
482fe8fb19SBen Gras #include <assert.h>
492fe8fb19SBen Gras #include <stdio.h>
502fe8fb19SBen Gras #include <errno.h>
512fe8fb19SBen Gras #include <stdarg.h>
522fe8fb19SBen Gras #include <string.h>
532fe8fb19SBen Gras #include "reentrant.h"
542fe8fb19SBen Gras #include "extern.h"
552fe8fb19SBen Gras #include "local.h"
562fe8fb19SBen Gras 
572fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(vsnprintf_ss,_vsnprintf_ss)582fe8fb19SBen Gras __weak_alias(vsnprintf_ss,_vsnprintf_ss)
592fe8fb19SBen Gras #endif
602fe8fb19SBen Gras 
612fe8fb19SBen Gras /*
622fe8fb19SBen Gras  * vsnprintf_ss: scaled down version of printf(3).
632fe8fb19SBen Gras  *
642fe8fb19SBen Gras  * this version based on vfprintf() from libc which was derived from
652fe8fb19SBen Gras  * software contributed to Berkeley by Chris Torek.
662fe8fb19SBen Gras  *
672fe8fb19SBen Gras  */
682fe8fb19SBen Gras 
692fe8fb19SBen Gras /*
702fe8fb19SBen Gras  * macros for converting digits to letters and vice versa
712fe8fb19SBen Gras  */
722fe8fb19SBen Gras #define	to_digit(c)	((c) - '0')
732fe8fb19SBen Gras #define is_digit(c)	((unsigned)to_digit(c) <= 9)
742fe8fb19SBen Gras #define	to_char(n)	(char)((n) + '0')
752fe8fb19SBen Gras 
762fe8fb19SBen Gras /*
772fe8fb19SBen Gras  * flags used during conversion.
782fe8fb19SBen Gras  */
792fe8fb19SBen Gras #define	ALT		0x001		/* alternate form */
802fe8fb19SBen Gras #define	HEXPREFIX	0x002		/* add 0x or 0X prefix */
812fe8fb19SBen Gras #define	LADJUST		0x004		/* left adjustment */
822fe8fb19SBen Gras #define	LONGDBL		0x008		/* long double; unimplemented */
832fe8fb19SBen Gras #define	LONGINT		0x010		/* long integer */
842fe8fb19SBen Gras #define	QUADINT		0x020		/* quad integer */
852fe8fb19SBen Gras #define	SHORTINT	0x040		/* short integer */
862fe8fb19SBen Gras #define	MAXINT		0x080		/* intmax_t */
872fe8fb19SBen Gras #define	PTRINT		0x100		/* intptr_t */
882fe8fb19SBen Gras #define	SIZEINT		0x200		/* size_t */
892fe8fb19SBen Gras #define	ZEROPAD		0x400		/* zero (as opposed to blank) pad */
902fe8fb19SBen Gras #define FPT		0x800		/* Floating point number */
912fe8fb19SBen Gras 
922fe8fb19SBen Gras 	/*
932fe8fb19SBen Gras 	 * To extend shorts properly, we need both signed and unsigned
942fe8fb19SBen Gras 	 * argument extraction methods.
952fe8fb19SBen Gras 	 */
962fe8fb19SBen Gras #define	SARG() \
972fe8fb19SBen Gras 	(flags&MAXINT ? va_arg(ap, intmax_t) : \
982fe8fb19SBen Gras 	    flags&PTRINT ? va_arg(ap, intptr_t) : \
992fe8fb19SBen Gras 	    flags&SIZEINT ? va_arg(ap, ssize_t) : /* XXX */ \
1002fe8fb19SBen Gras 	    flags&QUADINT ? va_arg(ap, quad_t) : \
1012fe8fb19SBen Gras 	    flags&LONGINT ? va_arg(ap, long) : \
1022fe8fb19SBen Gras 	    flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
1032fe8fb19SBen Gras 	    (long)va_arg(ap, int))
1042fe8fb19SBen Gras #define	UARG() \
1052fe8fb19SBen Gras 	(flags&MAXINT ? va_arg(ap, uintmax_t) : \
1062fe8fb19SBen Gras 	    flags&PTRINT ? va_arg(ap, uintptr_t) : \
1072fe8fb19SBen Gras 	    flags&SIZEINT ? va_arg(ap, size_t) : \
1082fe8fb19SBen Gras 	    flags&QUADINT ? va_arg(ap, u_quad_t) : \
1092fe8fb19SBen Gras 	    flags&LONGINT ? va_arg(ap, u_long) : \
1102fe8fb19SBen Gras 	    flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
1112fe8fb19SBen Gras 	    (u_long)va_arg(ap, u_int))
1122fe8fb19SBen Gras 
1132fe8fb19SBen Gras #define PUTCHAR(C) do {					\
1142fe8fb19SBen Gras 	if (sbuf < tailp)				\
1152fe8fb19SBen Gras 		*sbuf++ = (C);				\
1162fe8fb19SBen Gras } while (/*CONSTCOND*/0)
1172fe8fb19SBen Gras 
1182fe8fb19SBen Gras int
119f14fb602SLionel Sambuc vsnprintf_ss(char *sbuf, size_t slen, const char *fmt0, va_list ap)
1202fe8fb19SBen Gras {
1212fe8fb19SBen Gras 	const char *fmt;	/* format string */
1222fe8fb19SBen Gras 	int ch;			/* character from fmt */
1232fe8fb19SBen Gras 	int n;			/* handy integer (short term usage) */
1242fe8fb19SBen Gras 	char *cp;		/* handy char pointer (short term usage) */
1252fe8fb19SBen Gras 	int flags;		/* flags as above */
1262fe8fb19SBen Gras 	int ret;		/* return value accumulator */
1272fe8fb19SBen Gras 	int width;		/* width from format (%8d), or 0 */
1282fe8fb19SBen Gras 	int prec;		/* precision from format (%.3d), or -1 */
1292fe8fb19SBen Gras 	char sign;		/* sign prefix (' ', '+', '-', or \0) */
1302fe8fb19SBen Gras 
1312fe8fb19SBen Gras 	u_quad_t _uquad;	/* integer arguments %[diouxX] */
1322fe8fb19SBen Gras 	enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
1332fe8fb19SBen Gras 	int dprec;		/* a copy of prec if [diouxX], 0 otherwise */
1342fe8fb19SBen Gras 	int realsz;		/* field size expanded by dprec */
1352fe8fb19SBen Gras 	int size;		/* size of converted field or string */
1362fe8fb19SBen Gras 	const char *xdigs;	/* digits for [xX] conversion */
1372fe8fb19SBen Gras 	char bf[128]; 		/* space for %c, %[diouxX] */
1382fe8fb19SBen Gras 	char *tailp;		/* tail pointer for snprintf */
139f14fb602SLionel Sambuc 	size_t len;
1402fe8fb19SBen Gras 
1412fe8fb19SBen Gras 	static const char xdigs_lower[16] = "0123456789abcdef";
1422fe8fb19SBen Gras 	static const char xdigs_upper[16] = "0123456789ABCDEF";
1432fe8fb19SBen Gras 
1442fe8fb19SBen Gras 	_DIAGASSERT(slen == 0 || sbuf != NULL);
1452fe8fb19SBen Gras 	_DIAGASSERT(fmt0 != NULL);
1462fe8fb19SBen Gras 
147*0a6a1f1dSLionel Sambuc 	if (slen > INT_MAX) {
148*0a6a1f1dSLionel Sambuc 		errno = EOVERFLOW;
149f14fb602SLionel Sambuc 		return -1;
1502fe8fb19SBen Gras 	}
1512fe8fb19SBen Gras 
1522fe8fb19SBen Gras 	tailp = sbuf + slen;
1532fe8fb19SBen Gras 
1542fe8fb19SBen Gras 	cp = NULL;	/* XXX: shutup gcc */
1552fe8fb19SBen Gras 	size = 0;	/* XXX: shutup gcc */
1562fe8fb19SBen Gras 
1572fe8fb19SBen Gras 	fmt = fmt0;
1582fe8fb19SBen Gras 	ret = 0;
1592fe8fb19SBen Gras 
1602fe8fb19SBen Gras 	xdigs = NULL;		/* XXX: shut up gcc warning */
1612fe8fb19SBen Gras 
1622fe8fb19SBen Gras 	/*
1632fe8fb19SBen Gras 	 * Scan the format for conversions (`%' character).
1642fe8fb19SBen Gras 	 */
1652fe8fb19SBen Gras 	for (;;) {
1662fe8fb19SBen Gras 		while (*fmt != '%' && *fmt) {
1672fe8fb19SBen Gras 			ret++;
1682fe8fb19SBen Gras 			PUTCHAR(*fmt);
1692fe8fb19SBen Gras 			fmt++;
1702fe8fb19SBen Gras 		}
1712fe8fb19SBen Gras 		if (*fmt == 0)
1722fe8fb19SBen Gras 			goto done;
1732fe8fb19SBen Gras 
1742fe8fb19SBen Gras 		fmt++;		/* skip over '%' */
1752fe8fb19SBen Gras 
1762fe8fb19SBen Gras 		flags = 0;
1772fe8fb19SBen Gras 		dprec = 0;
1782fe8fb19SBen Gras 		width = 0;
1792fe8fb19SBen Gras 		prec = -1;
1802fe8fb19SBen Gras 		sign = '\0';
1812fe8fb19SBen Gras 
1822fe8fb19SBen Gras rflag:		ch = *fmt++;
1832fe8fb19SBen Gras reswitch:	switch (ch) {
1842fe8fb19SBen Gras 		case ' ':
1852fe8fb19SBen Gras 			/*
1862fe8fb19SBen Gras 			 * ``If the space and + flags both appear, the space
1872fe8fb19SBen Gras 			 * flag will be ignored.''
1882fe8fb19SBen Gras 			 *	-- ANSI X3J11
1892fe8fb19SBen Gras 			 */
1902fe8fb19SBen Gras 			if (!sign)
1912fe8fb19SBen Gras 				sign = ' ';
1922fe8fb19SBen Gras 			goto rflag;
1932fe8fb19SBen Gras 		case '#':
1942fe8fb19SBen Gras 			flags |= ALT;
1952fe8fb19SBen Gras 			goto rflag;
1962fe8fb19SBen Gras 		case '*':
1972fe8fb19SBen Gras 			/*
1982fe8fb19SBen Gras 			 * ``A negative field width argument is taken as a
1992fe8fb19SBen Gras 			 * - flag followed by a positive field width.''
2002fe8fb19SBen Gras 			 *	-- ANSI X3J11
2012fe8fb19SBen Gras 			 * They don't exclude field widths read from args.
2022fe8fb19SBen Gras 			 */
2032fe8fb19SBen Gras 			if ((width = va_arg(ap, int)) >= 0)
2042fe8fb19SBen Gras 				goto rflag;
2052fe8fb19SBen Gras 			width = -width;
2062fe8fb19SBen Gras 			/* FALLTHROUGH */
2072fe8fb19SBen Gras 		case '-':
2082fe8fb19SBen Gras 			flags |= LADJUST;
2092fe8fb19SBen Gras 			goto rflag;
2102fe8fb19SBen Gras 		case '+':
2112fe8fb19SBen Gras 			sign = '+';
2122fe8fb19SBen Gras 			goto rflag;
2132fe8fb19SBen Gras 		case '.':
2142fe8fb19SBen Gras 			if ((ch = *fmt++) == '*') {
2152fe8fb19SBen Gras 				n = va_arg(ap, int);
2162fe8fb19SBen Gras 				prec = n < 0 ? -1 : n;
2172fe8fb19SBen Gras 				goto rflag;
2182fe8fb19SBen Gras 			}
2192fe8fb19SBen Gras 			n = 0;
2202fe8fb19SBen Gras 			while (is_digit(ch)) {
2212fe8fb19SBen Gras 				n = 10 * n + to_digit(ch);
2222fe8fb19SBen Gras 				ch = *fmt++;
2232fe8fb19SBen Gras 			}
2242fe8fb19SBen Gras 			prec = n < 0 ? -1 : n;
2252fe8fb19SBen Gras 			goto reswitch;
2262fe8fb19SBen Gras 		case '0':
2272fe8fb19SBen Gras 			/*
2282fe8fb19SBen Gras 			 * ``Note that 0 is taken as a flag, not as the
2292fe8fb19SBen Gras 			 * beginning of a field width.''
2302fe8fb19SBen Gras 			 *	-- ANSI X3J11
2312fe8fb19SBen Gras 			 */
2322fe8fb19SBen Gras 			flags |= ZEROPAD;
2332fe8fb19SBen Gras 			goto rflag;
2342fe8fb19SBen Gras 		case '1': case '2': case '3': case '4':
2352fe8fb19SBen Gras 		case '5': case '6': case '7': case '8': case '9':
2362fe8fb19SBen Gras 			n = 0;
2372fe8fb19SBen Gras 			do {
2382fe8fb19SBen Gras 				n = 10 * n + to_digit(ch);
2392fe8fb19SBen Gras 				ch = *fmt++;
2402fe8fb19SBen Gras 			} while (is_digit(ch));
2412fe8fb19SBen Gras 			width = n;
2422fe8fb19SBen Gras 			goto reswitch;
2432fe8fb19SBen Gras 		case 'h':
2442fe8fb19SBen Gras 			flags |= SHORTINT;
2452fe8fb19SBen Gras 			goto rflag;
2462fe8fb19SBen Gras 		case 'j':
2472fe8fb19SBen Gras 			flags |= MAXINT;
2482fe8fb19SBen Gras 			goto rflag;
2492fe8fb19SBen Gras 		case 'l':
2502fe8fb19SBen Gras 			if (*fmt == 'l') {
2512fe8fb19SBen Gras 				fmt++;
2522fe8fb19SBen Gras 				flags |= QUADINT;
2532fe8fb19SBen Gras 			} else {
2542fe8fb19SBen Gras 				flags |= LONGINT;
2552fe8fb19SBen Gras 			}
2562fe8fb19SBen Gras 			goto rflag;
2572fe8fb19SBen Gras 		case 'q':
2582fe8fb19SBen Gras 			flags |= QUADINT;
2592fe8fb19SBen Gras 			goto rflag;
2602fe8fb19SBen Gras 		case 't':
2612fe8fb19SBen Gras 			flags |= PTRINT;
2622fe8fb19SBen Gras 			goto rflag;
2632fe8fb19SBen Gras 		case 'z':
2642fe8fb19SBen Gras 			flags |= SIZEINT;
2652fe8fb19SBen Gras 			goto rflag;
2662fe8fb19SBen Gras 		case 'c':
2672fe8fb19SBen Gras 			*(cp = bf) = va_arg(ap, int);
2682fe8fb19SBen Gras 			size = 1;
2692fe8fb19SBen Gras 			sign = '\0';
2702fe8fb19SBen Gras 			break;
2712fe8fb19SBen Gras 		case 'D':
2722fe8fb19SBen Gras 			flags |= LONGINT;
2732fe8fb19SBen Gras 			/*FALLTHROUGH*/
2742fe8fb19SBen Gras 		case 'd':
2752fe8fb19SBen Gras 		case 'i':
2762fe8fb19SBen Gras 			_uquad = SARG();
2772fe8fb19SBen Gras 			if ((quad_t)_uquad < 0) {
2782fe8fb19SBen Gras 				_uquad = -_uquad;
2792fe8fb19SBen Gras 				sign = '-';
2802fe8fb19SBen Gras 			}
2812fe8fb19SBen Gras 			base = DEC;
2822fe8fb19SBen Gras 			goto number;
2832fe8fb19SBen Gras 		case 'n':
2842fe8fb19SBen Gras 			if (flags & MAXINT)
2852fe8fb19SBen Gras 				*va_arg(ap, intmax_t *) = ret;
2862fe8fb19SBen Gras 			else if (flags & PTRINT)
2872fe8fb19SBen Gras 				*va_arg(ap, intptr_t *) = ret;
2882fe8fb19SBen Gras 			else if (flags & SIZEINT)
2892fe8fb19SBen Gras 				*va_arg(ap, ssize_t *) = ret;
2902fe8fb19SBen Gras 			else if (flags & QUADINT)
2912fe8fb19SBen Gras 				*va_arg(ap, quad_t *) = ret;
2922fe8fb19SBen Gras 			else if (flags & LONGINT)
2932fe8fb19SBen Gras 				*va_arg(ap, long *) = ret;
2942fe8fb19SBen Gras 			else if (flags & SHORTINT)
2952fe8fb19SBen Gras 				*va_arg(ap, short *) = ret;
2962fe8fb19SBen Gras 			else
2972fe8fb19SBen Gras 				*va_arg(ap, int *) = ret;
2982fe8fb19SBen Gras 			continue;	/* no output */
2992fe8fb19SBen Gras 		case 'O':
3002fe8fb19SBen Gras 			flags |= LONGINT;
3012fe8fb19SBen Gras 			/*FALLTHROUGH*/
3022fe8fb19SBen Gras 		case 'o':
3032fe8fb19SBen Gras 			_uquad = UARG();
3042fe8fb19SBen Gras 			base = OCT;
3052fe8fb19SBen Gras 			goto nosign;
3062fe8fb19SBen Gras 		case 'p':
3072fe8fb19SBen Gras 			/*
3082fe8fb19SBen Gras 			 * ``The argument shall be a pointer to void.  The
3092fe8fb19SBen Gras 			 * value of the pointer is converted to a sequence
3102fe8fb19SBen Gras 			 * of printable characters, in an implementation-
3112fe8fb19SBen Gras 			 * defined manner.''
3122fe8fb19SBen Gras 			 *	-- ANSI X3J11
3132fe8fb19SBen Gras 			 */
3142fe8fb19SBen Gras 			/* NOSTRICT */
3152fe8fb19SBen Gras 			_uquad = (u_long)va_arg(ap, void *);
3162fe8fb19SBen Gras 			base = HEX;
3172fe8fb19SBen Gras 			xdigs = xdigs_lower;
3182fe8fb19SBen Gras 			flags |= HEXPREFIX;
3192fe8fb19SBen Gras 			ch = 'x';
3202fe8fb19SBen Gras 			goto nosign;
3212fe8fb19SBen Gras 		case 's':
3222fe8fb19SBen Gras 			if ((cp = va_arg(ap, char *)) == NULL)
3232fe8fb19SBen Gras 				/*XXXUNCONST*/
3242fe8fb19SBen Gras 				cp = __UNCONST("(null)");
3252fe8fb19SBen Gras 			if (prec >= 0) {
3262fe8fb19SBen Gras 				/*
3272fe8fb19SBen Gras 				 * can't use strlen; can only look for the
3282fe8fb19SBen Gras 				 * NUL in the first `prec' characters, and
3292fe8fb19SBen Gras 				 * strlen() will go further.
3302fe8fb19SBen Gras 				 */
3312fe8fb19SBen Gras 				char *p = memchr(cp, 0, (size_t)prec);
3322fe8fb19SBen Gras 
3332fe8fb19SBen Gras 				if (p != NULL) {
334f14fb602SLionel Sambuc 					_DIAGASSERT(__type_fit(int, p - cp));
335f14fb602SLionel Sambuc 					size = (int)(p - cp);
3362fe8fb19SBen Gras 					if (size > prec)
3372fe8fb19SBen Gras 						size = prec;
3382fe8fb19SBen Gras 				} else
3392fe8fb19SBen Gras 					size = prec;
340f14fb602SLionel Sambuc 			} else {
341f14fb602SLionel Sambuc 				len = strlen(cp);
342f14fb602SLionel Sambuc 				_DIAGASSERT(__type_fit(int, len));
343f14fb602SLionel Sambuc 				size = (int)len;
344f14fb602SLionel Sambuc 			}
3452fe8fb19SBen Gras 			sign = '\0';
3462fe8fb19SBen Gras 			break;
3472fe8fb19SBen Gras 		case 'U':
3482fe8fb19SBen Gras 			flags |= LONGINT;
3492fe8fb19SBen Gras 			/*FALLTHROUGH*/
3502fe8fb19SBen Gras 		case 'u':
3512fe8fb19SBen Gras 			_uquad = UARG();
3522fe8fb19SBen Gras 			base = DEC;
3532fe8fb19SBen Gras 			goto nosign;
3542fe8fb19SBen Gras 		case 'X':
3552fe8fb19SBen Gras 			xdigs = xdigs_upper;
3562fe8fb19SBen Gras 			goto hex;
3572fe8fb19SBen Gras 		case 'x':
3582fe8fb19SBen Gras 			xdigs = xdigs_lower;
3592fe8fb19SBen Gras hex:			_uquad = UARG();
3602fe8fb19SBen Gras 			base = HEX;
3612fe8fb19SBen Gras 			/* leading 0x/X only if non-zero */
3622fe8fb19SBen Gras 			if (flags & ALT && _uquad != 0)
3632fe8fb19SBen Gras 				flags |= HEXPREFIX;
3642fe8fb19SBen Gras 
3652fe8fb19SBen Gras 			/* unsigned conversions */
3662fe8fb19SBen Gras nosign:			sign = '\0';
3672fe8fb19SBen Gras 			/*
3682fe8fb19SBen Gras 			 * ``... diouXx conversions ... if a precision is
3692fe8fb19SBen Gras 			 * specified, the 0 flag will be ignored.''
3702fe8fb19SBen Gras 			 *	-- ANSI X3J11
3712fe8fb19SBen Gras 			 */
3722fe8fb19SBen Gras number:			if ((dprec = prec) >= 0)
3732fe8fb19SBen Gras 				flags &= ~ZEROPAD;
3742fe8fb19SBen Gras 
3752fe8fb19SBen Gras 			/*
3762fe8fb19SBen Gras 			 * ``The result of converting a zero value with an
3772fe8fb19SBen Gras 			 * explicit precision of zero is no characters.''
3782fe8fb19SBen Gras 			 *	-- ANSI X3J11
3792fe8fb19SBen Gras 			 */
3802fe8fb19SBen Gras 			cp = bf + sizeof(bf);
3812fe8fb19SBen Gras 			if (_uquad != 0 || prec != 0) {
3822fe8fb19SBen Gras 				/*
3832fe8fb19SBen Gras 				 * Unsigned mod is hard, and unsigned mod
3842fe8fb19SBen Gras 				 * by a constant is easier than that by
3852fe8fb19SBen Gras 				 * a variable; hence this switch.
3862fe8fb19SBen Gras 				 */
3872fe8fb19SBen Gras 				switch (base) {
3882fe8fb19SBen Gras 				case OCT:
3892fe8fb19SBen Gras 					do {
3902fe8fb19SBen Gras 						*--cp = to_char(_uquad & 7);
3912fe8fb19SBen Gras 						_uquad >>= 3;
3922fe8fb19SBen Gras 					} while (_uquad);
3932fe8fb19SBen Gras 					/* handle octal leading 0 */
3942fe8fb19SBen Gras 					if (flags & ALT && *cp != '0')
3952fe8fb19SBen Gras 						*--cp = '0';
3962fe8fb19SBen Gras 					break;
3972fe8fb19SBen Gras 
3982fe8fb19SBen Gras 				case DEC:
3992fe8fb19SBen Gras 					/* many numbers are 1 digit */
4002fe8fb19SBen Gras 					while (_uquad >= 10) {
4012fe8fb19SBen Gras 						*--cp = to_char(_uquad % 10);
4022fe8fb19SBen Gras 						_uquad /= 10;
4032fe8fb19SBen Gras 					}
4042fe8fb19SBen Gras 					*--cp = to_char(_uquad);
4052fe8fb19SBen Gras 					break;
4062fe8fb19SBen Gras 
4072fe8fb19SBen Gras 				case HEX:
4082fe8fb19SBen Gras 					do {
4092fe8fb19SBen Gras 						*--cp = xdigs[(size_t)_uquad & 15];
4102fe8fb19SBen Gras 						_uquad >>= 4;
4112fe8fb19SBen Gras 					} while (_uquad);
4122fe8fb19SBen Gras 					break;
4132fe8fb19SBen Gras 
4142fe8fb19SBen Gras 				default:
4152fe8fb19SBen Gras 					/*XXXUNCONST*/
4162fe8fb19SBen Gras 					cp = __UNCONST("bug bad base");
417f14fb602SLionel Sambuc 					len = strlen(cp);
418f14fb602SLionel Sambuc 					_DIAGASSERT(__type_fit(int, len));
419f14fb602SLionel Sambuc 					size = (int)len;
4202fe8fb19SBen Gras 					goto skipsize;
4212fe8fb19SBen Gras 				}
4222fe8fb19SBen Gras 			}
423f14fb602SLionel Sambuc 			_DIAGASSERT(__type_fit(int, bf + sizeof(bf) - cp));
424f14fb602SLionel Sambuc 			size = (int)(bf + sizeof(bf) - cp);
4252fe8fb19SBen Gras 		skipsize:
4262fe8fb19SBen Gras 			break;
4272fe8fb19SBen Gras 		default:	/* "%?" prints ?, unless ? is NUL */
4282fe8fb19SBen Gras 			if (ch == '\0')
4292fe8fb19SBen Gras 				goto done;
4302fe8fb19SBen Gras 			/* pretend it was %c with argument ch */
4312fe8fb19SBen Gras 			cp = bf;
4322fe8fb19SBen Gras 			*cp = ch;
4332fe8fb19SBen Gras 			size = 1;
4342fe8fb19SBen Gras 			sign = '\0';
4352fe8fb19SBen Gras 			break;
4362fe8fb19SBen Gras 		}
4372fe8fb19SBen Gras 
4382fe8fb19SBen Gras 		/*
4392fe8fb19SBen Gras 		 * All reasonable formats wind up here.  At this point, `cp'
4402fe8fb19SBen Gras 		 * points to a string which (if not flags&LADJUST) should be
4412fe8fb19SBen Gras 		 * padded out to `width' places.  If flags&ZEROPAD, it should
4422fe8fb19SBen Gras 		 * first be prefixed by any sign or other prefix; otherwise,
4432fe8fb19SBen Gras 		 * it should be blank padded before the prefix is emitted.
4442fe8fb19SBen Gras 		 * After any left-hand padding and prefixing, emit zeroes
4452fe8fb19SBen Gras 		 * required by a decimal [diouxX] precision, then print the
4462fe8fb19SBen Gras 		 * string proper, then emit zeroes required by any leftover
4472fe8fb19SBen Gras 		 * floating precision; finally, if LADJUST, pad with blanks.
4482fe8fb19SBen Gras 		 *
4492fe8fb19SBen Gras 		 * Compute actual size, so we know how much to pad.
4502fe8fb19SBen Gras 		 * size excludes decimal prec; realsz includes it.
4512fe8fb19SBen Gras 		 */
4522fe8fb19SBen Gras 		realsz = dprec > size ? dprec : size;
4532fe8fb19SBen Gras 		if (sign)
4542fe8fb19SBen Gras 			realsz++;
4552fe8fb19SBen Gras 		else if (flags & HEXPREFIX)
4562fe8fb19SBen Gras 			realsz+= 2;
4572fe8fb19SBen Gras 
4582fe8fb19SBen Gras 		/* adjust ret */
4592fe8fb19SBen Gras 		ret += width > realsz ? width : realsz;
4602fe8fb19SBen Gras 
4612fe8fb19SBen Gras 		/* right-adjusting blank padding */
4622fe8fb19SBen Gras 		if ((flags & (LADJUST|ZEROPAD)) == 0) {
4632fe8fb19SBen Gras 			n = width - realsz;
4642fe8fb19SBen Gras 			while (n-- > 0)
4652fe8fb19SBen Gras 				PUTCHAR(' ');
4662fe8fb19SBen Gras 		}
4672fe8fb19SBen Gras 
4682fe8fb19SBen Gras 		/* prefix */
4692fe8fb19SBen Gras 		if (sign) {
4702fe8fb19SBen Gras 			PUTCHAR(sign);
4712fe8fb19SBen Gras 		} else if (flags & HEXPREFIX) {
4722fe8fb19SBen Gras 			PUTCHAR('0');
4732fe8fb19SBen Gras 			PUTCHAR(ch);
4742fe8fb19SBen Gras 		}
4752fe8fb19SBen Gras 
4762fe8fb19SBen Gras 		/* right-adjusting zero padding */
4772fe8fb19SBen Gras 		if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
4782fe8fb19SBen Gras 			n = width - realsz;
4792fe8fb19SBen Gras 			while (n-- > 0)
4802fe8fb19SBen Gras 				PUTCHAR('0');
4812fe8fb19SBen Gras 		}
4822fe8fb19SBen Gras 
4832fe8fb19SBen Gras 		/* leading zeroes from decimal precision */
4842fe8fb19SBen Gras 		n = dprec - size;
4852fe8fb19SBen Gras 		while (n-- > 0)
4862fe8fb19SBen Gras 			PUTCHAR('0');
4872fe8fb19SBen Gras 
4882fe8fb19SBen Gras 		/* the string or number proper */
4892fe8fb19SBen Gras 		while (size--)
4902fe8fb19SBen Gras 			PUTCHAR(*cp++);
4912fe8fb19SBen Gras 		/* left-adjusting padding (always blank) */
4922fe8fb19SBen Gras 		if (flags & LADJUST) {
4932fe8fb19SBen Gras 			n = width - realsz;
4942fe8fb19SBen Gras 			while (n-- > 0)
4952fe8fb19SBen Gras 				PUTCHAR(' ');
4962fe8fb19SBen Gras 		}
4972fe8fb19SBen Gras 	}
4982fe8fb19SBen Gras 
4992fe8fb19SBen Gras done:
5002fe8fb19SBen Gras 	if (sbuf == tailp)
5012fe8fb19SBen Gras 		sbuf[-1] = '\0';
5022fe8fb19SBen Gras 	else
5032fe8fb19SBen Gras 		*sbuf = '\0';
504f14fb602SLionel Sambuc 	return ret;
5052fe8fb19SBen Gras 	/* NOTREACHED */
5062fe8fb19SBen Gras }
507