xref: /csrg-svn/sys/kern/subr_prf.c (revision 52392)
149062Sbostic /*-
249062Sbostic  * Copyright (c) 1986, 1988, 1991 The Regents of the University of California.
349062Sbostic  * All rights reserved.
423381Smckusick  *
549062Sbostic  * %sccs.include.redist.c%
649062Sbostic  *
7*52392Smarc  *	@(#)subr_prf.c	7.34 (Berkeley) 02/05/92
823381Smckusick  */
931Sbill 
1051768Smarc #include <sys/param.h>
1151768Smarc #include <sys/systm.h>
1251768Smarc #include <sys/buf.h>
1351768Smarc #include <sys/conf.h>
1451768Smarc #include <sys/reboot.h>
1551768Smarc #include <sys/msgbuf.h>
1651768Smarc #include <sys/proc.h>
1751768Smarc #include <sys/ioctl.h>
1851768Smarc #include <sys/vnode.h>
1951768Smarc #include <sys/file.h>
2051768Smarc #include <sys/tty.h>
2151768Smarc #include <sys/tprintf.h>
2251768Smarc #include <sys/syslog.h>
2351768Smarc #include <sys/malloc.h>
2431Sbill 
2549062Sbostic /*
2649062Sbostic  * Note that stdarg.h and the ANSI style va_start macro is used for both
2749062Sbostic  * ANSI and traditional C compilers.
2849062Sbostic  */
2949062Sbostic #include <machine/stdarg.h>
3049062Sbostic 
3135282Skarels #ifdef KADB
3237496Smckusick #include "machine/kdbparam.h"
3330625Skarels #endif
3430625Skarels 
3549062Sbostic #define TOCONS	0x01
3649062Sbostic #define TOTTY	0x02
3749062Sbostic #define TOLOG	0x04
3816724Sralph 
3930549Skarels struct	tty *constty;			/* pointer to console "window" tty */
4030549Skarels 
4140808Smarc #ifdef KADB
4240808Smarc extern	cngetc();			/* standard console getc */
4349062Sbostic int	(*v_getc)() = cngetc;		/* "" getc from virtual console */
4440808Smarc extern	cnpoll();
4540808Smarc int	(*v_poll)() = cnpoll;		/* kdb hook to enable input polling */
4640808Smarc #endif
4749062Sbostic extern	cnputc();			/* standard console putc */
4849953Sbostic int	(*v_putc)() = cnputc;		/* routine to putc on virtual console */
4940808Smarc 
5051768Smarc void  logpri __P((int level));
5149908Sbostic static void  putchar __P((int ch, int flags, struct tty *tp));
5249908Sbostic static char *ksprintn __P((u_long num, int base, int *len));
53*52392Smarc void kprintf __P((const char *fmt, int flags, struct tty *tp, va_list ap));
5449062Sbostic 
5551768Smarc int consintr = 1;			/* Ok to handle console interrupts? */
5651768Smarc 
5731Sbill /*
5851768Smarc  * Variable panicstr contains argument to first call to panic; used as flag
5951768Smarc  * to indicate that the kernel has already called panic.
6031Sbill  */
6151768Smarc const char *panicstr;
6229946Skarels 
6349062Sbostic /*
6449062Sbostic  * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
6549062Sbostic  * and then reboots.  If we are called twice, then we avoid trying to sync
6649062Sbostic  * the disks as this often leads to recursive panics.
6749062Sbostic  */
68*52392Smarc #ifdef __GNUC__
69*52392Smarc volatile			/* panic() does not return */
70*52392Smarc #endif
7149062Sbostic void
7251768Smarc #ifdef __STDC__
7351768Smarc panic(const char *fmt, ...)
7451768Smarc #else
7551768Smarc panic(fmt /*, va_alist */)
7651768Smarc 	char *fmt;
7751768Smarc #endif
7849062Sbostic {
79*52392Smarc 	int bootopt;
8051768Smarc 	va_list ap;
8148426Skarels 
8251768Smarc 	bootopt = RB_AUTOBOOT | RB_DUMP;
8349062Sbostic 	if (panicstr)
8449062Sbostic 		bootopt |= RB_NOSYNC;
8549062Sbostic 	else
8651768Smarc 		panicstr = fmt;
8751768Smarc 
8851768Smarc 	va_start(ap, fmt);
89*52392Smarc 	printf("panic: %r\n", fmt, ap);
9051768Smarc 	va_end(ap);
9151768Smarc 
9249062Sbostic #ifdef KGDB
9349062Sbostic 	kgdb_panic();
9449062Sbostic #endif
9549062Sbostic #ifdef KADB
9649062Sbostic 	if (boothowto & RB_KDB) {
9750268Sbostic 		int s;
9850268Sbostic 
9950268Sbostic 		s = splnet();	/* below kdb pri */
10049062Sbostic 		setsoftkdb();
10149062Sbostic 		splx(s);
10249062Sbostic 	}
10349062Sbostic #endif
10449062Sbostic 	boot(bootopt);
10549062Sbostic }
10649062Sbostic 
10749062Sbostic /*
10849062Sbostic  * Warn that a system table is full.
10949062Sbostic  */
11049062Sbostic void
11149062Sbostic tablefull(tab)
11249062Sbostic 	char *tab;
11331Sbill {
114285Sbill 
11549062Sbostic 	log(LOG_ERR, "%s: table is full\n", tab);
116285Sbill }
117285Sbill 
1182377Swnj /*
11939560Smarc  * Uprintf prints to the controlling terminal for the current process.
12049062Sbostic  * It may block if the tty queue is overfull.  No message is printed if
12149062Sbostic  * the queue does not clear in a reasonable time.
1222377Swnj  */
12349062Sbostic void
12449062Sbostic #ifdef __STDC__
12549062Sbostic uprintf(const char *fmt, ...)
12649062Sbostic #else
12751767Smarc uprintf(fmt, va_alist)
1282781Swnj 	char *fmt;
12949062Sbostic #endif
130285Sbill {
13147540Skarels 	register struct proc *p = curproc;
13249062Sbostic 	va_list ap;
133285Sbill 
13449953Sbostic 	if (p->p_flag & SCTTY && p->p_session->s_ttyvp) {
13549953Sbostic 		va_start(ap, fmt);
13649062Sbostic 		kprintf(fmt, TOTTY, p->p_session->s_ttyp, ap);
13749953Sbostic 		va_end(ap);
13849953Sbostic 	}
139285Sbill }
140285Sbill 
14144386Smarc tpr_t
14248426Skarels tprintf_open(p)
14348426Skarels 	register struct proc *p;
14444386Smarc {
14549953Sbostic 
14644386Smarc 	if (p->p_flag & SCTTY && p->p_session->s_ttyvp) {
14744386Smarc 		SESSHOLD(p->p_session);
14848426Skarels 		return ((tpr_t) p->p_session);
14949062Sbostic 	}
15049062Sbostic 	return ((tpr_t) NULL);
15144386Smarc }
15244386Smarc 
15348426Skarels void
15444386Smarc tprintf_close(sess)
15544386Smarc 	tpr_t sess;
15644386Smarc {
15749953Sbostic 
15844386Smarc 	if (sess)
15948426Skarels 		SESSRELE((struct session *) sess);
16044386Smarc }
16144386Smarc 
16218364Skarels /*
16344386Smarc  * tprintf prints on the controlling terminal associated
16449062Sbostic  * with the given session.
16518364Skarels  */
16649062Sbostic void
16749062Sbostic #ifdef __STDC__
16849062Sbostic tprintf(tpr_t tpr, const char *fmt, ...)
16949062Sbostic #else
17051767Smarc tprintf(tpr, fmt, va_alist)
17148426Skarels 	tpr_t tpr;
17216724Sralph 	char *fmt;
17349062Sbostic #endif
17416724Sralph {
17548426Skarels 	register struct session *sess = (struct session *)tpr;
17648426Skarels 	struct tty *tp = NULL;
17744386Smarc 	int flags = TOLOG;
17849062Sbostic 	va_list ap;
17916724Sralph 
18025389Skarels 	logpri(LOG_INFO);
18148426Skarels 	if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
18244386Smarc 		flags |= TOTTY;
18348426Skarels 		tp = sess->s_ttyp;
18448426Skarels 	}
18549062Sbostic 	va_start(ap, fmt);
18649062Sbostic 	kprintf(fmt, flags, tp, ap);
18749062Sbostic 	va_end(ap);
18825389Skarels 	logwakeup();
18916724Sralph }
19016724Sralph 
19149062Sbostic /*
19249062Sbostic  * Ttyprintf displays a message on a tty; it should be used only by
19349062Sbostic  * the tty driver, or anything that knows the underlying tty will not
19449062Sbostic  * be revoke(2)'d away.  Other callers should use tprintf.
19549062Sbostic  */
19649062Sbostic void
19749062Sbostic #ifdef __STDC__
19849062Sbostic ttyprintf(struct tty *tp, const char *fmt, ...)
19949062Sbostic #else
20051767Smarc ttyprintf(tp, fmt, va_alist)
20149062Sbostic 	struct tty *tp;
20249062Sbostic 	char *fmt;
20349062Sbostic #endif
20449062Sbostic {
20549062Sbostic 	va_list ap;
20649062Sbostic 
20749062Sbostic 	va_start(ap, fmt);
20849062Sbostic 	kprintf(fmt, TOTTY, tp, ap);
20949062Sbostic 	va_end(ap);
21049062Sbostic }
21149062Sbostic 
21248426Skarels extern	int log_open;
21344386Smarc 
21416724Sralph /*
21549062Sbostic  * Log writes to the log buffer, and guarantees not to sleep (so can be
21649062Sbostic  * called by interrupt routines).  If there is no process reading the
21749062Sbostic  * log yet, it writes to the console also.
21816724Sralph  */
21949062Sbostic void
22049062Sbostic #ifdef __STDC__
22149062Sbostic log(int level, const char *fmt, ...)
22249062Sbostic #else
22351767Smarc log(level, fmt, va_alist)
22449062Sbostic 	int level;
22516724Sralph 	char *fmt;
22649062Sbostic #endif
22716724Sralph {
22850268Sbostic 	register int s;
22949062Sbostic 	va_list ap;
23016724Sralph 
23150268Sbostic 	s = splhigh();
23225389Skarels 	logpri(level);
23349062Sbostic 	va_start(ap, fmt);
23449062Sbostic 	kprintf(fmt, TOLOG, NULL, ap);
23516724Sralph 	splx(s);
23649953Sbostic 	va_end(ap);
23749953Sbostic 	if (!log_open) {
23849953Sbostic 		va_start(ap, fmt);
23949062Sbostic 		kprintf(fmt, TOCONS, NULL, ap);
24049953Sbostic 		va_end(ap);
24149953Sbostic 	}
24216724Sralph 	logwakeup();
24316724Sralph }
24416724Sralph 
24551768Smarc void
24625389Skarels logpri(level)
24725389Skarels 	int level;
24825389Skarels {
24949908Sbostic 	register int ch;
25049908Sbostic 	register char *p;
25125389Skarels 
25249062Sbostic 	putchar('<', TOLOG, NULL);
25349908Sbostic 	for (p = ksprintn((u_long)level, 10, NULL); ch = *p--;)
25449908Sbostic 		putchar(ch, TOLOG, NULL);
25549062Sbostic 	putchar('>', TOLOG, NULL);
25625389Skarels }
25725389Skarels 
25849062Sbostic void
25949062Sbostic #ifdef __STDC__
26049062Sbostic addlog(const char *fmt, ...)
26149062Sbostic #else
26251767Smarc addlog(fmt, va_alist)
26333479Skarels 	char *fmt;
26449062Sbostic #endif
26533479Skarels {
26650268Sbostic 	register int s;
26749062Sbostic 	va_list ap;
26833479Skarels 
26950268Sbostic 	s = splhigh();
27049062Sbostic 	va_start(ap, fmt);
27149062Sbostic 	kprintf(fmt, TOLOG, NULL, ap);
27233479Skarels 	splx(s);
27349953Sbostic 	va_end(ap);
27449953Sbostic 	if (!log_open) {
27549953Sbostic 		va_start(ap, fmt);
27649062Sbostic 		kprintf(fmt, TOCONS, NULL, ap);
27749953Sbostic 		va_end(ap);
27849953Sbostic 	}
27933479Skarels 	logwakeup();
28033479Skarels }
28133479Skarels 
28249062Sbostic void
28349062Sbostic #ifdef __STDC__
28449062Sbostic printf(const char *fmt, ...)
28549062Sbostic #else
28651767Smarc printf(fmt, va_alist)
28749062Sbostic 	char *fmt;
28829946Skarels #endif
28949062Sbostic {
29049953Sbostic 	va_list ap;
29149062Sbostic 	register int savintr;
2922678Swnj 
29349062Sbostic 	savintr = consintr;		/* disable interrupts */
29449062Sbostic 	consintr = 0;
29549062Sbostic 	va_start(ap, fmt);
29649062Sbostic 	kprintf(fmt, TOCONS | TOLOG, NULL, ap);
29749062Sbostic 	va_end(ap);
29849062Sbostic 	if (!panicstr)
29949062Sbostic 		logwakeup();
30049062Sbostic 	consintr = savintr;		/* reenable interrupts */
30131Sbill }
30231Sbill 
3032781Swnj /*
30449062Sbostic  * Scaled down version of printf(3).
30549062Sbostic  *
30649062Sbostic  * Two additional formats:
30749062Sbostic  *
30849062Sbostic  * The format %b is supported to decode error registers.
30949062Sbostic  * Its usage is:
31049062Sbostic  *
311*52392Smarc  *	printf("reg=%b\n", regval, "<base><arg>*");
31249062Sbostic  *
31349062Sbostic  * where <base> is the output base expressed as a control character, e.g.
31449062Sbostic  * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
31549062Sbostic  * the first of which gives the bit number to be inspected (origin 1), and
31649062Sbostic  * the next characters (up to a control character, i.e. a character <= 32),
31749062Sbostic  * give the name of the register.  Thus:
31849062Sbostic  *
31951768Smarc  *	kprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
32049062Sbostic  *
32149062Sbostic  * would produce output:
32249062Sbostic  *
32349062Sbostic  *	reg=3<BITTWO,BITONE>
32449062Sbostic  *
32551768Smarc  * The format %r passes an additional format string and argument list
32651768Smarc  * recursively.  Its usage is:
32749062Sbostic  *
32851768Smarc  * fn(char *fmt, ...)
32949953Sbostic  * {
33049953Sbostic  *	va_list ap;
33149953Sbostic  *	va_start(ap, fmt);
332*52392Smarc  *	printf("prefix: %r: suffix\n", fmt, ap);
33349953Sbostic  *	va_end(ap);
33451768Smarc  * }
33549908Sbostic  *
33649908Sbostic  * Space or zero padding and a field width are supported for the numeric
33749908Sbostic  * formats only.
3382781Swnj  */
33949909Sbostic void
340*52392Smarc kprintf(fmt, flags, tp, ap)
34149094Sbostic 	register const char *fmt;
34249062Sbostic 	int flags;
34349094Sbostic 	struct tty *tp;
34449062Sbostic 	va_list ap;
34531Sbill {
34649062Sbostic 	register char *p;
34749062Sbostic 	register int ch, n;
34849908Sbostic 	u_long ul;
34949908Sbostic 	int base, lflag, tmp, width;
35049908Sbostic 	char padc;
35131Sbill 
35249062Sbostic 	for (;;) {
35349908Sbostic 		padc = ' ';
35449908Sbostic 		width = 0;
35549919Skarels 		while ((ch = *(u_char *)fmt++) != '%') {
35649062Sbostic 			if (ch == '\0')
35749062Sbostic 				return;
35849094Sbostic 			putchar(ch, flags, tp);
35929946Skarels 		}
36049062Sbostic 		lflag = 0;
36149919Skarels reswitch:	switch (ch = *(u_char *)fmt++) {
36249908Sbostic 		case '0':
36349908Sbostic 			padc = '0';
36449908Sbostic 			goto reswitch;
36549908Sbostic 		case '1': case '2': case '3': case '4':
36649908Sbostic 		case '5': case '6': case '7': case '8': case '9':
36749908Sbostic 			for (width = 0;; ++fmt) {
36849908Sbostic 				width = width * 10 + ch - '0';
36949908Sbostic 				ch = *fmt;
37049908Sbostic 				if (ch < '0' || ch > '9')
37149908Sbostic 					break;
37249908Sbostic 			}
37349908Sbostic 			goto reswitch;
37449062Sbostic 		case 'l':
37549062Sbostic 			lflag = 1;
37649062Sbostic 			goto reswitch;
37749062Sbostic 		case 'b':
37849062Sbostic 			ul = va_arg(ap, int);
37949062Sbostic 			p = va_arg(ap, char *);
38049908Sbostic 			for (p = ksprintn(ul, *p++, NULL); ch = *p--;)
38149908Sbostic 				putchar(ch, flags, tp);
38231Sbill 
38349062Sbostic 			if (!ul)
38449062Sbostic 				break;
3852377Swnj 
38649908Sbostic 			for (tmp = 0; n = *p++;) {
38749062Sbostic 				if (ul & (1 << (n - 1))) {
38849908Sbostic 					putchar(tmp ? ',' : '<', flags, tp);
38949062Sbostic 					for (; (n = *p) > ' '; ++p)
39049094Sbostic 						putchar(n, flags, tp);
39149908Sbostic 					tmp = 1;
39249062Sbostic 				} else
39349062Sbostic 					for (; *p > ' '; ++p);
39449062Sbostic 			}
39549908Sbostic 			if (tmp)
39649094Sbostic 				putchar('>', flags, tp);
39749062Sbostic 			break;
39849062Sbostic 		case 'c':
39949094Sbostic 			putchar(va_arg(ap, int), flags, tp);
40049062Sbostic 			break;
40149062Sbostic 		case 'r':
40249062Sbostic 			p = va_arg(ap, char *);
40349094Sbostic 			kprintf(p, flags, tp, va_arg(ap, va_list));
40449062Sbostic 			break;
40549062Sbostic 		case 's':
40649062Sbostic 			p = va_arg(ap, char *);
40749062Sbostic 			while (ch = *p++)
40849094Sbostic 				putchar(ch, flags, tp);
40949062Sbostic 			break;
41049062Sbostic 		case 'd':
41149908Sbostic 			ul = lflag ? va_arg(ap, long) : va_arg(ap, int);
41249062Sbostic 			if ((long)ul < 0) {
41349094Sbostic 				putchar('-', flags, tp);
41449062Sbostic 				ul = -(long)ul;
41549062Sbostic 			}
41649908Sbostic 			base = 10;
41749908Sbostic 			goto number;
41849062Sbostic 		case 'o':
41949908Sbostic 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
42049908Sbostic 			base = 8;
42149953Sbostic 			goto number;
42249062Sbostic 		case 'u':
42349908Sbostic 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
42449908Sbostic 			base = 10;
42549908Sbostic 			goto number;
42649062Sbostic 		case 'x':
42749908Sbostic 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
42849908Sbostic 			base = 16;
42949908Sbostic number:			p = ksprintn(ul, base, &tmp);
43049908Sbostic 			if (width && (width -= tmp) > 0)
43149908Sbostic 				while (width--)
43249908Sbostic 					putchar(padc, flags, tp);
43349908Sbostic 			while (ch = *p--)
43449908Sbostic 				putchar(ch, flags, tp);
43549062Sbostic 			break;
43649062Sbostic 		default:
43749094Sbostic 			putchar('%', flags, tp);
43849062Sbostic 			if (lflag)
43949094Sbostic 				putchar('l', flags, tp);
44049908Sbostic 			/* FALLTHROUGH */
44149908Sbostic 		case '%':
44249094Sbostic 			putchar(ch, flags, tp);
44349062Sbostic 		}
44430625Skarels 	}
44531Sbill }
44631Sbill 
4472941Swnj /*
44849062Sbostic  * Print a character on console or users terminal.  If destination is
44949062Sbostic  * the console then the last MSGBUFS characters are saved in msgbuf for
45049062Sbostic  * inspection later.
451285Sbill  */
45249062Sbostic static void
45349094Sbostic putchar(c, flags, tp)
4542377Swnj 	register int c;
45549062Sbostic 	int flags;
45649094Sbostic 	struct tty *tp;
457285Sbill {
45833479Skarels 	extern int msgbufmapped;
45950268Sbostic 	register struct msgbuf *mbp;
460285Sbill 
46130549Skarels 	if (panicstr)
46249062Sbostic 		constty = NULL;
46349094Sbostic 	if ((flags & TOCONS) && tp == NULL && constty) {
46449094Sbostic 		tp = constty;
46530549Skarels 		flags |= TOTTY;
46630549Skarels 	}
46749094Sbostic 	if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
46849094Sbostic 	    (flags & TOCONS) && tp == constty)
46949062Sbostic 		constty = NULL;
47049062Sbostic 	if ((flags & TOLOG) &&
47149062Sbostic 	    c != '\0' && c != '\r' && c != 0177 && msgbufmapped) {
47250268Sbostic 		mbp = msgbufp;
47345733Smckusick 		if (mbp->msg_magic != MSG_MAGIC) {
47449953Sbostic 			bzero((caddr_t)mbp, sizeof(*mbp));
47545733Smckusick 			mbp->msg_magic = MSG_MAGIC;
4762172Swnj 		}
47745733Smckusick 		mbp->msg_bufc[mbp->msg_bufx++] = c;
47845733Smckusick 		if (mbp->msg_bufx < 0 || mbp->msg_bufx >= MSG_BSIZE)
47945733Smckusick 			mbp->msg_bufx = 0;
480285Sbill 	}
48149062Sbostic 	if ((flags & TOCONS) && constty == NULL && c != '\0')
48230549Skarels 		(*v_putc)(c);
483285Sbill }
48449908Sbostic 
48549908Sbostic /*
48649908Sbostic  * Scaled down version of sprintf(3).
48749908Sbostic  */
48849908Sbostic #ifdef __STDC__
48949919Skarels sprintf(char *buf, const char *cfmt, ...)
49049908Sbostic #else
49151767Smarc sprintf(buf, cfmt, va_alist)
49249919Skarels 	char *buf, *cfmt;
49349908Sbostic #endif
49449908Sbostic {
49549919Skarels 	register const char *fmt = cfmt;
49649908Sbostic 	register char *p, *bp;
49749908Sbostic 	register int ch, base;
49849908Sbostic 	u_long ul;
49949908Sbostic 	int lflag;
50049908Sbostic 	va_list ap;
50149908Sbostic 
50249919Skarels 	va_start(ap, cfmt);
50349919Skarels 	for (bp = buf; ; ) {
50449919Skarels 		while ((ch = *(u_char *)fmt++) != '%')
50549908Sbostic 			if ((*bp++ = ch) == '\0')
50649919Skarels 				return ((bp - buf) - 1);
50749919Skarels 
50849908Sbostic 		lflag = 0;
50949919Skarels reswitch:	switch (ch = *(u_char *)fmt++) {
51049908Sbostic 		case 'l':
51149908Sbostic 			lflag = 1;
51249908Sbostic 			goto reswitch;
51349908Sbostic 		case 'c':
51449908Sbostic 			*bp++ = va_arg(ap, int);
51549908Sbostic 			break;
51649908Sbostic 		case 's':
51749908Sbostic 			p = va_arg(ap, char *);
51849919Skarels 			while (*bp++ = *p++)
519*52392Smarc 				continue;
52049908Sbostic 			--bp;
52149908Sbostic 			break;
52249908Sbostic 		case 'd':
52349908Sbostic 			ul = lflag ? va_arg(ap, long) : va_arg(ap, int);
52449908Sbostic 			if ((long)ul < 0) {
52549908Sbostic 				*bp++ = '-';
52649908Sbostic 				ul = -(long)ul;
52749908Sbostic 			}
52849908Sbostic 			base = 10;
52949908Sbostic 			goto number;
53049908Sbostic 			break;
53149908Sbostic 		case 'o':
53249908Sbostic 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
53349908Sbostic 			base = 8;
53449908Sbostic 			goto number;
53549908Sbostic 			break;
53649908Sbostic 		case 'u':
53749908Sbostic 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
53849908Sbostic 			base = 10;
53949908Sbostic 			goto number;
54049908Sbostic 			break;
54149908Sbostic 		case 'x':
54249908Sbostic 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
54349908Sbostic 			base = 16;
54449908Sbostic number:			for (p = ksprintn(ul, base, NULL); ch = *p--;)
54549908Sbostic 				*bp++ = ch;
54649908Sbostic 			break;
54749908Sbostic 		default:
54849908Sbostic 			*bp++ = '%';
54949908Sbostic 			if (lflag)
55049908Sbostic 				*bp++ = 'l';
55149908Sbostic 			/* FALLTHROUGH */
55249908Sbostic 		case '%':
55349908Sbostic 			*bp++ = ch;
55449908Sbostic 		}
55549908Sbostic 	}
55649908Sbostic 	va_end(ap);
55749908Sbostic }
55849908Sbostic 
55949908Sbostic /*
56049908Sbostic  * Put a number (base <= 16) in a buffer in reverse order; return an
56149908Sbostic  * optional length and a pointer to the NULL terminated (preceded?)
56249908Sbostic  * buffer.
56349908Sbostic  */
56449908Sbostic static char *
56549908Sbostic ksprintn(ul, base, lenp)
56649908Sbostic 	register u_long ul;
56749908Sbostic 	register int base, *lenp;
56849908Sbostic {					/* A long in base 8, plus NULL. */
56949908Sbostic 	static char buf[sizeof(long) * NBBY / 3 + 2];
57049908Sbostic 	register char *p;
57149908Sbostic 
57249908Sbostic 	p = buf;
57349908Sbostic 	do {
57449908Sbostic 		*++p = "0123456789abcdef"[ul % base];
57549908Sbostic 	} while (ul /= base);
57649908Sbostic 	if (lenp)
57749908Sbostic 		*lenp = p - buf;
57849919Skarels 	return (p);
57949908Sbostic }
580