summaryrefslogtreecommitdiff
path: root/sys/kern
diff options
context:
space:
mode:
authorkstailey <kstailey@cvs.openbsd.org>1996-11-24 00:42:05 +0000
committerkstailey <kstailey@cvs.openbsd.org>1996-11-24 00:42:05 +0000
commitd79a6195c38f323b6fa18f9c2732ffc350576686 (patch)
tree420c5c8f7eb20a087a6db47163e863d513a4de4e /sys/kern
parentae35e52e08ef100ae7e301bdb1977638baa8888a (diff)
added bitmap_snprintf
Diffstat (limited to 'sys/kern')
-rw-r--r--sys/kern/subr_prf.c79
1 files changed, 78 insertions, 1 deletions
diff --git a/sys/kern/subr_prf.c b/sys/kern/subr_prf.c
index 70ef3d1b944..2cd80520d0f 100644
--- a/sys/kern/subr_prf.c
+++ b/sys/kern/subr_prf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: subr_prf.c,v 1.11 1996/10/19 10:02:49 niklas Exp $ */
+/* $OpenBSD: subr_prf.c,v 1.12 1996/11/24 00:42:04 kstailey Exp $ */
/* $NetBSD: subr_prf.c,v 1.25 1996/04/22 01:38:46 christos Exp $ */
/*-
@@ -75,6 +75,12 @@
#define TOTTY 0x02
#define TOLOG 0x04
+/*
+ * This is the size of the buffer that should be passed to ksnprintn().
+ * It's the length of a long in base 8, plus NULL.
+ */
+#define KSNPRINTN_BUFSIZE (sizeof(long) * NBBY / 3 + 2)
+
struct tty *constty; /* pointer to console "window" tty */
void (*v_putc) __P((int)) = cnputc; /* routine to putc on virtual console */
@@ -693,3 +699,74 @@ ksprintn(ul, base, lenp)
*lenp = p - buf;
return (p);
}
+
+
+/*
+ * Print a bitmask into the provided buffer, and return a pointer
+ * to that buffer.
+ */
+char *
+bitmask_snprintf(ul, p, buf, buflen)
+ u_long ul;
+ const char *p;
+ char *buf;
+ size_t buflen;
+{
+ char *bp, *q;
+ size_t left;
+ register int n;
+ int ch, tmp;
+ char snbuf[KSNPRINTN_BUFSIZE];
+
+ bp = buf;
+ bzero(buf, buflen);
+
+ /*
+ * Always leave room for the trailing NULL.
+ */
+ left = buflen - 1;
+
+ /*
+ * Print the value into the buffer. Abort if there's not
+ * enough room.
+ */
+ if (buflen < KSNPRINTN_BUFSIZE)
+ return (buf);
+
+ for (q = ksprintn(ul, *p++, NULL); /* , snbuf, sizeof(snbuf)); */
+ (ch = *q--) != 0;) {
+ *bp++ = ch;
+ left--;
+ }
+
+ /*
+ * If the value we printed was 0, or if we don't have room for
+ * "<x>", we're done.
+ */
+ if (ul == 0 || left < 3)
+ return (buf);
+
+#define PUTBYTE(b, c, l) \
+ *(b)++ = (c); \
+ if (--(l) == 0) \
+ goto out;
+
+ for (tmp = 0; (n = *p++) != 0;) {
+ if (ul & (1 << (n - 1))) {
+ PUTBYTE(bp, tmp ? ',' : '<', left);
+ for (; (n = *p) > ' '; ++p) {
+ PUTBYTE(bp, n, left);
+ }
+ tmp = 1;
+ } else
+ for (; *p > ' '; ++p)
+ continue;
+ }
+ if (tmp)
+ *bp = '>';
+
+#undef PUTBYTE
+
+ out:
+ return (buf);
+}