xref: /illumos-gate/usr/src/boot/libsa/printf.c (revision 22028508)
14c528395SToomas Soome /*
2199767f8SToomas Soome  * Copyright (c) 1986, 1988, 1991, 1993
3199767f8SToomas Soome  *	The Regents of the University of California.  All rights reserved.
4199767f8SToomas Soome  * (c) UNIX System Laboratories, Inc.
5199767f8SToomas Soome  * All or some portions of this file are derived from material licensed
6199767f8SToomas Soome  * to the University of California by American Telephone and Telegraph
7199767f8SToomas Soome  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8199767f8SToomas Soome  * the permission of UNIX System Laboratories, Inc.
9199767f8SToomas Soome  *
10199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
11199767f8SToomas Soome  * modification, are permitted provided that the following conditions
12199767f8SToomas Soome  * are met:
13199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
14199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
15199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
16199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
17199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
181974da4bSToomas Soome  * 3. Neither the name of the University nor the names of its contributors
19199767f8SToomas Soome  *    may be used to endorse or promote products derived from this software
20199767f8SToomas Soome  *    without specific prior written permission.
21199767f8SToomas Soome  *
22199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23199767f8SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26199767f8SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27199767f8SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28199767f8SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29199767f8SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30199767f8SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31199767f8SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32199767f8SToomas Soome  * SUCH DAMAGE.
33199767f8SToomas Soome  *
34199767f8SToomas Soome  *	@(#)subr_prf.c	8.3 (Berkeley) 1/21/94
35199767f8SToomas Soome  */
36199767f8SToomas Soome 
37199767f8SToomas Soome #include <sys/cdefs.h>
38199767f8SToomas Soome 
39199767f8SToomas Soome /*
40199767f8SToomas Soome  * Standaloneified version of the FreeBSD kernel printf family.
41199767f8SToomas Soome  */
42199767f8SToomas Soome 
43199767f8SToomas Soome #include <sys/types.h>
44199767f8SToomas Soome #include <sys/stddef.h>
45199767f8SToomas Soome #include <sys/stdint.h>
46199767f8SToomas Soome #include <limits.h>
47199767f8SToomas Soome #include <string.h>
48199767f8SToomas Soome #include "stand.h"
49199767f8SToomas Soome 
50199767f8SToomas Soome /*
51199767f8SToomas Soome  * Note that stdarg.h and the ANSI style va_start macro is used for both
52199767f8SToomas Soome  * ANSI and traditional C compilers.
53199767f8SToomas Soome  */
54199767f8SToomas Soome #include <machine/stdarg.h>
55199767f8SToomas Soome 
564c528395SToomas Soome #define	MAXNBUF (sizeof (intmax_t) * CHAR_BIT + 1)
57199767f8SToomas Soome 
58199767f8SToomas Soome typedef void (kvprintf_fn_t)(int, void *);
59199767f8SToomas Soome 
604c528395SToomas Soome static char *ksprintn(char *, uintmax_t, int, int *, int);
614c528395SToomas Soome static int kvprintf(char const *, kvprintf_fn_t *, void *, int, va_list);
62199767f8SToomas Soome 
63199767f8SToomas Soome static void
putchar_wrapper(int cc,void * arg __unused)648eef2ab6SToomas Soome putchar_wrapper(int cc, void *arg __unused)
65199767f8SToomas Soome {
66199767f8SToomas Soome 
67199767f8SToomas Soome 	putchar(cc);
68199767f8SToomas Soome }
69199767f8SToomas Soome 
70199767f8SToomas Soome int
printf(const char * fmt,...)71199767f8SToomas Soome printf(const char *fmt, ...)
72199767f8SToomas Soome {
73199767f8SToomas Soome 	va_list ap;
74199767f8SToomas Soome 	int retval;
75199767f8SToomas Soome 
76199767f8SToomas Soome 	va_start(ap, fmt);
77199767f8SToomas Soome 	retval = kvprintf(fmt, putchar_wrapper, NULL, 10, ap);
78199767f8SToomas Soome 	va_end(ap);
794c528395SToomas Soome 	return (retval);
80199767f8SToomas Soome }
81199767f8SToomas Soome 
82199767f8SToomas Soome void
vprintf(const char * fmt,va_list ap)83199767f8SToomas Soome vprintf(const char *fmt, va_list ap)
84199767f8SToomas Soome {
85199767f8SToomas Soome 
86199767f8SToomas Soome 	kvprintf(fmt, putchar_wrapper, NULL, 10, ap);
87199767f8SToomas Soome }
88199767f8SToomas Soome 
89199767f8SToomas Soome int
sprintf(char * buf,const char * cfmt,...)90199767f8SToomas Soome sprintf(char *buf, const char *cfmt, ...)
91199767f8SToomas Soome {
92199767f8SToomas Soome 	int retval;
93199767f8SToomas Soome 	va_list ap;
94199767f8SToomas Soome 
95199767f8SToomas Soome 	va_start(ap, cfmt);
96199767f8SToomas Soome 	retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
97199767f8SToomas Soome 	buf[retval] = '\0';
98199767f8SToomas Soome 	va_end(ap);
994c528395SToomas Soome 	return (retval);
100199767f8SToomas Soome }
101199767f8SToomas Soome 
102199767f8SToomas Soome struct print_buf {
103199767f8SToomas Soome 	char *buf;
104199767f8SToomas Soome 	size_t size;
105199767f8SToomas Soome };
106199767f8SToomas Soome 
107199767f8SToomas Soome static void
snprint_func(int ch,void * arg)108199767f8SToomas Soome snprint_func(int ch, void *arg)
109199767f8SToomas Soome {
110199767f8SToomas Soome 	struct print_buf *pbuf = arg;
111199767f8SToomas Soome 
112199767f8SToomas Soome 	if (pbuf->size < 2) {
113199767f8SToomas Soome 		/*
114199767f8SToomas Soome 		 * Reserve last buffer position for the terminating
115199767f8SToomas Soome 		 * character:
116199767f8SToomas Soome 		 */
117199767f8SToomas Soome 		return;
118199767f8SToomas Soome 	}
119199767f8SToomas Soome 	*(pbuf->buf)++ = ch;
120199767f8SToomas Soome 	pbuf->size--;
121199767f8SToomas Soome }
122199767f8SToomas Soome 
1236ea3a31bSToomas Soome int
asprintf(char ** buf,const char * cfmt,...)1246ea3a31bSToomas Soome asprintf(char **buf, const char *cfmt, ...)
1256ea3a31bSToomas Soome {
1266ea3a31bSToomas Soome 	int retval;
1276ea3a31bSToomas Soome 	struct print_buf arg;
1286ea3a31bSToomas Soome 	va_list ap;
1296ea3a31bSToomas Soome 
1306ea3a31bSToomas Soome 	*buf = NULL;
1316ea3a31bSToomas Soome 	va_start(ap, cfmt);
1326ea3a31bSToomas Soome 	retval = kvprintf(cfmt, NULL, NULL, 10, ap);
1336ea3a31bSToomas Soome 	va_end(ap);
1346ea3a31bSToomas Soome 	if (retval <= 0)
1356ea3a31bSToomas Soome 		return (-1);
1366ea3a31bSToomas Soome 
1376ea3a31bSToomas Soome 	arg.size = retval + 1;
1386ea3a31bSToomas Soome 	arg.buf = *buf = malloc(arg.size);
1396ea3a31bSToomas Soome 	if (*buf == NULL)
1406ea3a31bSToomas Soome 		return (-1);
1416ea3a31bSToomas Soome 
1426ea3a31bSToomas Soome 	va_start(ap, cfmt);
1436ea3a31bSToomas Soome 	retval = kvprintf(cfmt, &snprint_func, &arg, 10, ap);
1446ea3a31bSToomas Soome 	va_end(ap);
1456ea3a31bSToomas Soome 
1466ea3a31bSToomas Soome 	if (arg.size >= 1)
1476ea3a31bSToomas Soome 		*(arg.buf)++ = 0;
1486ea3a31bSToomas Soome 	return (retval);
1496ea3a31bSToomas Soome }
1506ea3a31bSToomas Soome 
151199767f8SToomas Soome int
snprintf(char * buf,size_t size,const char * cfmt,...)152199767f8SToomas Soome snprintf(char *buf, size_t size, const char *cfmt, ...)
153199767f8SToomas Soome {
154199767f8SToomas Soome 	int retval;
155199767f8SToomas Soome 	va_list ap;
156199767f8SToomas Soome 	struct print_buf arg;
157199767f8SToomas Soome 
158199767f8SToomas Soome 	arg.buf = buf;
159199767f8SToomas Soome 	arg.size = size;
160199767f8SToomas Soome 
161199767f8SToomas Soome 	va_start(ap, cfmt);
162199767f8SToomas Soome 	retval = kvprintf(cfmt, &snprint_func, &arg, 10, ap);
163199767f8SToomas Soome 	va_end(ap);
164199767f8SToomas Soome 
165199767f8SToomas Soome 	if (arg.size >= 1)
166199767f8SToomas Soome 		*(arg.buf)++ = 0;
1674c528395SToomas Soome 	return (retval);
168199767f8SToomas Soome }
169199767f8SToomas Soome 
170199767f8SToomas Soome void
vsprintf(char * buf,const char * cfmt,va_list ap)171199767f8SToomas Soome vsprintf(char *buf, const char *cfmt, va_list ap)
172199767f8SToomas Soome {
173199767f8SToomas Soome 	int	retval;
1744c528395SToomas Soome 
175199767f8SToomas Soome 	retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
176199767f8SToomas Soome 	buf[retval] = '\0';
177199767f8SToomas Soome }
178199767f8SToomas Soome 
179199767f8SToomas Soome void
vsnprintf(char * buf,size_t size,const char * cfmt,va_list ap)180199767f8SToomas Soome vsnprintf(char *buf, size_t size, const char *cfmt, va_list ap)
181199767f8SToomas Soome {
182199767f8SToomas Soome 	int	retval;
183199767f8SToomas Soome 	struct print_buf arg;
184199767f8SToomas Soome 
185199767f8SToomas Soome 	arg.buf = buf;
186199767f8SToomas Soome 	arg.size = size;
187199767f8SToomas Soome 
188199767f8SToomas Soome 	retval = kvprintf(cfmt, &snprint_func, &arg, 10, ap);
189199767f8SToomas Soome 	buf[retval] = '\0';
190199767f8SToomas Soome }
191199767f8SToomas Soome 
192199767f8SToomas Soome /*
193199767f8SToomas Soome  * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
194199767f8SToomas Soome  * order; return an optional length and a pointer to the last character
195199767f8SToomas Soome  * written in the buffer (i.e., the first character of the string).
196199767f8SToomas Soome  * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
197199767f8SToomas Soome  */
198199767f8SToomas Soome static char *
ksprintn(char * nbuf,uintmax_t num,int base,int * lenp,int upper)199199767f8SToomas Soome ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
200199767f8SToomas Soome {
201199767f8SToomas Soome 	char *p, c;
202199767f8SToomas Soome 
203199767f8SToomas Soome 	p = nbuf;
204199767f8SToomas Soome 	*p = '\0';
205199767f8SToomas Soome 	do {
206199767f8SToomas Soome 		c = hex2ascii(num % base);
207199767f8SToomas Soome 		*++p = upper ? toupper(c) : c;
208199767f8SToomas Soome 	} while (num /= base);
209199767f8SToomas Soome 	if (lenp)
210199767f8SToomas Soome 		*lenp = p - nbuf;
211199767f8SToomas Soome 	return (p);
212199767f8SToomas Soome }
213199767f8SToomas Soome 
214199767f8SToomas Soome /*
215199767f8SToomas Soome  * Scaled down version of printf(3).
216199767f8SToomas Soome  *
217199767f8SToomas Soome  * Two additional formats:
218199767f8SToomas Soome  *
219199767f8SToomas Soome  * The format %b is supported to decode error registers.
220199767f8SToomas Soome  * Its usage is:
221199767f8SToomas Soome  *
222199767f8SToomas Soome  *	printf("reg=%b\n", regval, "<base><arg>*");
223199767f8SToomas Soome  *
224199767f8SToomas Soome  * where <base> is the output base expressed as a control character, e.g.
225199767f8SToomas Soome  * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
226199767f8SToomas Soome  * the first of which gives the bit number to be inspected (origin 1), and
227199767f8SToomas Soome  * the next characters (up to a control character, i.e. a character <= 32),
228199767f8SToomas Soome  * give the name of the register.  Thus:
229199767f8SToomas Soome  *
230199767f8SToomas Soome  *	kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE");
231199767f8SToomas Soome  *
232199767f8SToomas Soome  * would produce output:
233199767f8SToomas Soome  *
234199767f8SToomas Soome  *	reg=3<BITTWO,BITONE>
235199767f8SToomas Soome  *
236199767f8SToomas Soome  * XXX:  %D  -- Hexdump, takes pointer and separator string:
237199767f8SToomas Soome  *		("%6D", ptr, ":")   -> XX:XX:XX:XX:XX:XX
238199767f8SToomas Soome  *		("%*D", len, ptr, " " -> XX XX XX XX ...
239199767f8SToomas Soome  */
240199767f8SToomas Soome static int
kvprintf(char const * fmt,kvprintf_fn_t * func,void * arg,int radix,va_list ap)241199767f8SToomas Soome kvprintf(char const *fmt, kvprintf_fn_t *func, void *arg, int radix, va_list ap)
242199767f8SToomas Soome {
2436ea3a31bSToomas Soome #define	PCHAR(c) { \
2446ea3a31bSToomas Soome 	int cc = (c);				\
2456ea3a31bSToomas Soome 						\
2466ea3a31bSToomas Soome 	if (func) {				\
2476ea3a31bSToomas Soome 		(*func)(cc, arg);		\
2486ea3a31bSToomas Soome 	} else if (d != NULL) {			\
2496ea3a31bSToomas Soome 		*d++ = cc;			\
2506ea3a31bSToomas Soome 	}					\
2516ea3a31bSToomas Soome 	retval++;				\
2526ea3a31bSToomas Soome 	}
2536ea3a31bSToomas Soome 
254199767f8SToomas Soome 	char nbuf[MAXNBUF];
255199767f8SToomas Soome 	char *d;
256199767f8SToomas Soome 	const char *p, *percent, *q;
257cfba4bc6SToomas Soome 	uint16_t *S;
2584c528395SToomas Soome 	uchar_t *up;
259199767f8SToomas Soome 	int ch, n;
260199767f8SToomas Soome 	uintmax_t num;
261199767f8SToomas Soome 	int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
262199767f8SToomas Soome 	int cflag, hflag, jflag, tflag, zflag;
263199767f8SToomas Soome 	int dwidth, upper;
264199767f8SToomas Soome 	char padc;
265199767f8SToomas Soome 	int stop = 0, retval = 0;
266199767f8SToomas Soome 
267199767f8SToomas Soome 	num = 0;
268199767f8SToomas Soome 	if (!func)
2694c528395SToomas Soome 		d = (char *)arg;
270199767f8SToomas Soome 	else
271199767f8SToomas Soome 		d = NULL;
272199767f8SToomas Soome 
273199767f8SToomas Soome 	if (fmt == NULL)
274199767f8SToomas Soome 		fmt = "(fmt null)\n";
275199767f8SToomas Soome 
276199767f8SToomas Soome 	if (radix < 2 || radix > 36)
277199767f8SToomas Soome 		radix = 10;
278199767f8SToomas Soome 
279199767f8SToomas Soome 	for (;;) {
280199767f8SToomas Soome 		padc = ' ';
281199767f8SToomas Soome 		width = 0;
2824c528395SToomas Soome 		while ((ch = (uchar_t)*fmt++) != '%' || stop) {
283199767f8SToomas Soome 			if (ch == '\0')
284199767f8SToomas Soome 				return (retval);
285199767f8SToomas Soome 			PCHAR(ch);
286199767f8SToomas Soome 		}
287199767f8SToomas Soome 		percent = fmt - 1;
288199767f8SToomas Soome 		qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
289199767f8SToomas Soome 		sign = 0; dot = 0; dwidth = 0; upper = 0;
290199767f8SToomas Soome 		cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
2914c528395SToomas Soome reswitch:	switch (ch = (uchar_t)*fmt++) {
292199767f8SToomas Soome 		case '.':
293199767f8SToomas Soome 			dot = 1;
294199767f8SToomas Soome 			goto reswitch;
295199767f8SToomas Soome 		case '#':
296199767f8SToomas Soome 			sharpflag = 1;
297199767f8SToomas Soome 			goto reswitch;
298199767f8SToomas Soome 		case '+':
299199767f8SToomas Soome 			sign = 1;
300199767f8SToomas Soome 			goto reswitch;
301199767f8SToomas Soome 		case '-':
302199767f8SToomas Soome 			ladjust = 1;
303199767f8SToomas Soome 			goto reswitch;
304199767f8SToomas Soome 		case '%':
305199767f8SToomas Soome 			PCHAR(ch);
306199767f8SToomas Soome 			break;
307199767f8SToomas Soome 		case '*':
308199767f8SToomas Soome 			if (!dot) {
309199767f8SToomas Soome 				width = va_arg(ap, int);
310199767f8SToomas Soome 				if (width < 0) {
311199767f8SToomas Soome 					ladjust = !ladjust;
312199767f8SToomas Soome 					width = -width;
313199767f8SToomas Soome 				}
314199767f8SToomas Soome 			} else {
315199767f8SToomas Soome 				dwidth = va_arg(ap, int);
316199767f8SToomas Soome 			}
317199767f8SToomas Soome 			goto reswitch;
318199767f8SToomas Soome 		case '0':
319199767f8SToomas Soome 			if (!dot) {
320199767f8SToomas Soome 				padc = '0';
321199767f8SToomas Soome 				goto reswitch;
322199767f8SToomas Soome 			}
3234c528395SToomas Soome 			/* FALLTHROUGH */
324199767f8SToomas Soome 		case '1': case '2': case '3': case '4':
325199767f8SToomas Soome 		case '5': case '6': case '7': case '8': case '9':
3264c528395SToomas Soome 				for (n = 0; ; ++fmt) {
327199767f8SToomas Soome 					n = n * 10 + ch - '0';
328199767f8SToomas Soome 					ch = *fmt;
329199767f8SToomas Soome 					if (ch < '0' || ch > '9')
330199767f8SToomas Soome 						break;
331199767f8SToomas Soome 				}
332199767f8SToomas Soome 			if (dot)
333199767f8SToomas Soome 				dwidth = n;
334199767f8SToomas Soome 			else
335199767f8SToomas Soome 				width = n;
336199767f8SToomas Soome 			goto reswitch;
337199767f8SToomas Soome 		case 'b':
3384c528395SToomas Soome 			num = (uint_t)va_arg(ap, int);
339199767f8SToomas Soome 			p = va_arg(ap, char *);
3404c528395SToomas Soome 			for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q; )
341199767f8SToomas Soome 				PCHAR(*q--);
342199767f8SToomas Soome 
343199767f8SToomas Soome 			if (num == 0)
344199767f8SToomas Soome 				break;
345199767f8SToomas Soome 
3464c528395SToomas Soome 			for (tmp = 0; *p; ) {
347199767f8SToomas Soome 				n = *p++;
348199767f8SToomas Soome 				if (num & (1 << (n - 1))) {
349199767f8SToomas Soome 					PCHAR(tmp ? ',' : '<');
350199767f8SToomas Soome 					for (; (n = *p) > ' '; ++p)
351199767f8SToomas Soome 						PCHAR(n);
352199767f8SToomas Soome 					tmp = 1;
353199767f8SToomas Soome 				} else
354199767f8SToomas Soome 					for (; *p > ' '; ++p)
355199767f8SToomas Soome 						continue;
356199767f8SToomas Soome 			}
357199767f8SToomas Soome 			if (tmp)
358199767f8SToomas Soome 				PCHAR('>');
359199767f8SToomas Soome 			break;
360199767f8SToomas Soome 		case 'c':
361199767f8SToomas Soome 			PCHAR(va_arg(ap, int));
362199767f8SToomas Soome 			break;
363199767f8SToomas Soome 		case 'D':
3644c528395SToomas Soome 			up = va_arg(ap, uchar_t *);
365199767f8SToomas Soome 			p = va_arg(ap, char *);
366199767f8SToomas Soome 			if (!width)
367199767f8SToomas Soome 				width = 16;
3684c528395SToomas Soome 			while (width--) {
369199767f8SToomas Soome 				PCHAR(hex2ascii(*up >> 4));
370199767f8SToomas Soome 				PCHAR(hex2ascii(*up & 0x0f));
371199767f8SToomas Soome 				up++;
372199767f8SToomas Soome 				if (width)
3734c528395SToomas Soome 					for (q = p; *q; q++)
374199767f8SToomas Soome 						PCHAR(*q);
375199767f8SToomas Soome 			}
376199767f8SToomas Soome 			break;
377199767f8SToomas Soome 		case 'd':
378199767f8SToomas Soome 		case 'i':
379199767f8SToomas Soome 			base = 10;
380199767f8SToomas Soome 			sign = 1;
381199767f8SToomas Soome 			goto handle_sign;
382199767f8SToomas Soome 		case 'h':
383199767f8SToomas Soome 			if (hflag) {
384199767f8SToomas Soome 				hflag = 0;
385199767f8SToomas Soome 				cflag = 1;
386199767f8SToomas Soome 			} else
387199767f8SToomas Soome 				hflag = 1;
388199767f8SToomas Soome 			goto reswitch;
389199767f8SToomas Soome 		case 'j':
390199767f8SToomas Soome 			jflag = 1;
391199767f8SToomas Soome 			goto reswitch;
392199767f8SToomas Soome 		case 'l':
393199767f8SToomas Soome 			if (lflag) {
394199767f8SToomas Soome 				lflag = 0;
395199767f8SToomas Soome 				qflag = 1;
396199767f8SToomas Soome 			} else
397199767f8SToomas Soome 				lflag = 1;
398199767f8SToomas Soome 			goto reswitch;
399199767f8SToomas Soome 		case 'n':
400199767f8SToomas Soome 			if (jflag)
401199767f8SToomas Soome 				*(va_arg(ap, intmax_t *)) = retval;
402199767f8SToomas Soome 			else if (qflag)
403199767f8SToomas Soome 				*(va_arg(ap, quad_t *)) = retval;
404199767f8SToomas Soome 			else if (lflag)
405199767f8SToomas Soome 				*(va_arg(ap, long *)) = retval;
406199767f8SToomas Soome 			else if (zflag)
407199767f8SToomas Soome 				*(va_arg(ap, size_t *)) = retval;
408199767f8SToomas Soome 			else if (hflag)
409199767f8SToomas Soome 				*(va_arg(ap, short *)) = retval;
410199767f8SToomas Soome 			else if (cflag)
411199767f8SToomas Soome 				*(va_arg(ap, char *)) = retval;
412199767f8SToomas Soome 			else
413199767f8SToomas Soome 				*(va_arg(ap, int *)) = retval;
414199767f8SToomas Soome 			break;
415199767f8SToomas Soome 		case 'o':
416199767f8SToomas Soome 			base = 8;
417199767f8SToomas Soome 			goto handle_nosign;
418199767f8SToomas Soome 		case 'p':
419199767f8SToomas Soome 			base = 16;
420199767f8SToomas Soome 			sharpflag = (width == 0);
421199767f8SToomas Soome 			sign = 0;
422199767f8SToomas Soome 			num = (uintptr_t)va_arg(ap, void *);
423199767f8SToomas Soome 			goto number;
424199767f8SToomas Soome 		case 'q':
425199767f8SToomas Soome 			qflag = 1;
426199767f8SToomas Soome 			goto reswitch;
427199767f8SToomas Soome 		case 'r':
428199767f8SToomas Soome 			base = radix;
429199767f8SToomas Soome 			if (sign)
430199767f8SToomas Soome 				goto handle_sign;
431199767f8SToomas Soome 			goto handle_nosign;
432199767f8SToomas Soome 		case 's':
433199767f8SToomas Soome 			p = va_arg(ap, char *);
434199767f8SToomas Soome 			if (p == NULL)
435199767f8SToomas Soome 				p = "(null)";
436199767f8SToomas Soome 			if (!dot)
4374c528395SToomas Soome 				n = strlen(p);
438199767f8SToomas Soome 			else
439199767f8SToomas Soome 				for (n = 0; n < dwidth && p[n]; n++)
440199767f8SToomas Soome 					continue;
441199767f8SToomas Soome 
442199767f8SToomas Soome 			width -= n;
443199767f8SToomas Soome 
444199767f8SToomas Soome 			if (!ladjust && width > 0)
445199767f8SToomas Soome 				while (width--)
446199767f8SToomas Soome 					PCHAR(padc);
447199767f8SToomas Soome 			while (n--)
448199767f8SToomas Soome 				PCHAR(*p++);
449199767f8SToomas Soome 			if (ladjust && width > 0)
450199767f8SToomas Soome 				while (width--)
451199767f8SToomas Soome 					PCHAR(padc);
452199767f8SToomas Soome 			break;
453cfba4bc6SToomas Soome 		case 'S':	/* Assume console can cope with wide chars */
454cfba4bc6SToomas Soome 			S = va_arg(ap, uint16_t *);
455cfba4bc6SToomas Soome 			if (S == NULL)
456cfba4bc6SToomas Soome 				S = (uint16_t *)L"(null)";
457cfba4bc6SToomas Soome 			if (!dot) {
458cfba4bc6SToomas Soome 				for (n = 0; S[n] != 0; n++)
459cfba4bc6SToomas Soome 					continue;
460cfba4bc6SToomas Soome 			} else {
461cfba4bc6SToomas Soome 				for (n = 0; n < dwidth && S[n]; n++)
462cfba4bc6SToomas Soome 					continue;
463cfba4bc6SToomas Soome 			}
464cfba4bc6SToomas Soome 
465cfba4bc6SToomas Soome 			width -= n;
466cfba4bc6SToomas Soome 
467cfba4bc6SToomas Soome 			if (!ladjust && width > 0)
468cfba4bc6SToomas Soome 				while (width--)
469cfba4bc6SToomas Soome 					PCHAR(padc);
470cfba4bc6SToomas Soome 			while (n--)
471cfba4bc6SToomas Soome 				PCHAR(*S++);
472cfba4bc6SToomas Soome 			if (ladjust && width > 0)
473cfba4bc6SToomas Soome 				while (width--)
474cfba4bc6SToomas Soome 					PCHAR(padc);
475cfba4bc6SToomas Soome 			break;
476199767f8SToomas Soome 		case 't':
477199767f8SToomas Soome 			tflag = 1;
478199767f8SToomas Soome 			goto reswitch;
479199767f8SToomas Soome 		case 'u':
480199767f8SToomas Soome 			base = 10;
481199767f8SToomas Soome 			goto handle_nosign;
482199767f8SToomas Soome 		case 'X':
483199767f8SToomas Soome 			upper = 1;
4844c528395SToomas Soome 			/* FALLTHROUGH */
485199767f8SToomas Soome 		case 'x':
486199767f8SToomas Soome 			base = 16;
487199767f8SToomas Soome 			goto handle_nosign;
488199767f8SToomas Soome 		case 'y':
489199767f8SToomas Soome 			base = 16;
490199767f8SToomas Soome 			sign = 1;
491199767f8SToomas Soome 			goto handle_sign;
492199767f8SToomas Soome 		case 'z':
493199767f8SToomas Soome 			zflag = 1;
494199767f8SToomas Soome 			goto reswitch;
495199767f8SToomas Soome handle_nosign:
496199767f8SToomas Soome 			sign = 0;
497199767f8SToomas Soome 			if (jflag)
498199767f8SToomas Soome 				num = va_arg(ap, uintmax_t);
499199767f8SToomas Soome 			else if (qflag)
5004c528395SToomas Soome 				num = va_arg(ap, uint64_t);
501199767f8SToomas Soome 			else if (tflag)
502199767f8SToomas Soome 				num = va_arg(ap, ptrdiff_t);
503199767f8SToomas Soome 			else if (lflag)
5044c528395SToomas Soome 				num = va_arg(ap, ulong_t);
505199767f8SToomas Soome 			else if (zflag)
506199767f8SToomas Soome 				num = va_arg(ap, size_t);
507199767f8SToomas Soome 			else if (hflag)
5084c528395SToomas Soome 				num = (ushort_t)va_arg(ap, int);
509199767f8SToomas Soome 			else if (cflag)
5104c528395SToomas Soome 				num = (uchar_t)va_arg(ap, int);
511199767f8SToomas Soome 			else
5124c528395SToomas Soome 				num = va_arg(ap, uint_t);
513199767f8SToomas Soome 			goto number;
514199767f8SToomas Soome handle_sign:
515199767f8SToomas Soome 			if (jflag)
516199767f8SToomas Soome 				num = va_arg(ap, intmax_t);
517199767f8SToomas Soome 			else if (qflag)
518199767f8SToomas Soome 				num = va_arg(ap, quad_t);
519199767f8SToomas Soome 			else if (tflag)
520199767f8SToomas Soome 				num = va_arg(ap, ptrdiff_t);
521199767f8SToomas Soome 			else if (lflag)
522199767f8SToomas Soome 				num = va_arg(ap, long);
523199767f8SToomas Soome 			else if (zflag)
524199767f8SToomas Soome 				num = va_arg(ap, ssize_t);
525199767f8SToomas Soome 			else if (hflag)
526199767f8SToomas Soome 				num = (short)va_arg(ap, int);
527199767f8SToomas Soome 			else if (cflag)
528199767f8SToomas Soome 				num = (char)va_arg(ap, int);
529199767f8SToomas Soome 			else
530199767f8SToomas Soome 				num = va_arg(ap, int);
531199767f8SToomas Soome number:
532199767f8SToomas Soome 			if (sign && (intmax_t)num < 0) {
533199767f8SToomas Soome 				neg = 1;
534199767f8SToomas Soome 				num = -(intmax_t)num;
535199767f8SToomas Soome 			}
536199767f8SToomas Soome 			p = ksprintn(nbuf, num, base, &n, upper);
537199767f8SToomas Soome 			tmp = 0;
538199767f8SToomas Soome 			if (sharpflag && num != 0) {
539199767f8SToomas Soome 				if (base == 8)
540199767f8SToomas Soome 					tmp++;
541199767f8SToomas Soome 				else if (base == 16)
542199767f8SToomas Soome 					tmp += 2;
543199767f8SToomas Soome 			}
544199767f8SToomas Soome 			if (neg)
545199767f8SToomas Soome 				tmp++;
546199767f8SToomas Soome 
547199767f8SToomas Soome 			if (!ladjust && padc == '0')
548199767f8SToomas Soome 				dwidth = width - tmp;
549199767f8SToomas Soome 			width -= tmp + imax(dwidth, n);
550199767f8SToomas Soome 			dwidth -= n;
551199767f8SToomas Soome 			if (!ladjust)
552199767f8SToomas Soome 				while (width-- > 0)
553199767f8SToomas Soome 					PCHAR(' ');
554199767f8SToomas Soome 			if (neg)
555199767f8SToomas Soome 				PCHAR('-');
556199767f8SToomas Soome 			if (sharpflag && num != 0) {
557199767f8SToomas Soome 				if (base == 8) {
558199767f8SToomas Soome 					PCHAR('0');
559199767f8SToomas Soome 				} else if (base == 16) {
560199767f8SToomas Soome 					PCHAR('0');
561199767f8SToomas Soome 					PCHAR('x');
562199767f8SToomas Soome 				}
563199767f8SToomas Soome 			}
564199767f8SToomas Soome 			while (dwidth-- > 0)
565199767f8SToomas Soome 				PCHAR('0');
566199767f8SToomas Soome 
567199767f8SToomas Soome 			while (*p)
568199767f8SToomas Soome 				PCHAR(*p--);
569199767f8SToomas Soome 
570199767f8SToomas Soome 			if (ladjust)
571199767f8SToomas Soome 				while (width-- > 0)
572199767f8SToomas Soome 					PCHAR(' ');
573199767f8SToomas Soome 
574199767f8SToomas Soome 			break;
575199767f8SToomas Soome 		default:
576199767f8SToomas Soome 			while (percent < fmt)
577199767f8SToomas Soome 				PCHAR(*percent++);
578199767f8SToomas Soome 			/*
579199767f8SToomas Soome 			 * Since we ignore a formatting argument it is no
580199767f8SToomas Soome 			 * longer safe to obey the remaining formatting
581199767f8SToomas Soome 			 * arguments as the arguments will no longer match
582199767f8SToomas Soome 			 * the format specs.
583199767f8SToomas Soome 			 */
584199767f8SToomas Soome 			stop = 1;
585199767f8SToomas Soome 			break;
586199767f8SToomas Soome 		}
587199767f8SToomas Soome 	}
588199767f8SToomas Soome #undef PCHAR
589199767f8SToomas Soome }
590