snprintb.c revision 1.3 1 1.3 pooka /* $NetBSD: snprintb.c,v 1.3 2009/01/14 21:33:22 pooka Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * Redistribution and use in source and binary forms, with or without
8 1.1 christos * modification, are permitted provided that the following conditions
9 1.1 christos * are met:
10 1.1 christos * 1. Redistributions of source code must retain the above copyright
11 1.1 christos * notice, this list of conditions and the following disclaimer.
12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer in the
14 1.1 christos * documentation and/or other materials provided with the distribution.
15 1.1 christos *
16 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
27 1.1 christos */
28 1.1 christos
29 1.1 christos /*
30 1.1 christos * snprintb: print an interpreted bitmask to a buffer
31 1.1 christos *
32 1.2 christos * => returns the length of the buffer that would be required to print the
33 1.2 christos * string minus the terminating NUL.
34 1.1 christos */
35 1.2 christos #ifndef _STANDALONE
36 1.2 christos # ifndef _KERNEL
37 1.1 christos
38 1.2 christos # if HAVE_NBTOOL_CONFIG_H
39 1.2 christos # include "nbtool_config.h"
40 1.2 christos # endif
41 1.2 christos
42 1.2 christos # include <sys/cdefs.h>
43 1.2 christos # if defined(LIBC_SCCS) && !defined(lint)
44 1.3 pooka __RCSID("$NetBSD: snprintb.c,v 1.3 2009/01/14 21:33:22 pooka Exp $");
45 1.2 christos # endif
46 1.2 christos
47 1.2 christos # include <sys/types.h>
48 1.2 christos # include <sys/inttypes.h>
49 1.2 christos # include <stdio.h>
50 1.2 christos # include <util.h>
51 1.2 christos # include <errno.h>
52 1.1 christos # else
53 1.2 christos # include <sys/cdefs.h>
54 1.3 pooka __KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.3 2009/01/14 21:33:22 pooka Exp $");
55 1.3 pooka # include <sys/param.h>
56 1.2 christos # include <sys/inttypes.h>
57 1.2 christos # include <sys/systm.h>
58 1.2 christos # include <lib/libkern/libkern.h>
59 1.2 christos # endif
60 1.1 christos
61 1.1 christos int
62 1.1 christos snprintb(char *buf, size_t buflen, const char *bitfmt, uint64_t val)
63 1.1 christos {
64 1.1 christos char *bp = buf;
65 1.1 christos const char *sbase;
66 1.1 christos int bit, ch, len, sep, flen;
67 1.1 christos uint64_t field;
68 1.1 christos
69 1.1 christos #ifdef _KERNEL
70 1.1 christos /*
71 1.1 christos * For safety; no other *s*printf() do this, but in the kernel
72 1.1 christos * we don't usually check the return value
73 1.1 christos */
74 1.1 christos (void)memset(buf, 0, buflen);
75 1.1 christos #endif /* _KERNEL */
76 1.1 christos
77 1.1 christos ch = *bitfmt++;
78 1.1 christos switch (ch != '\177' ? ch : *bitfmt++) {
79 1.1 christos case 8:
80 1.1 christos sbase = "0%" PRIo64;
81 1.1 christos break;
82 1.1 christos case 10:
83 1.1 christos sbase = "%" PRId64;
84 1.1 christos break;
85 1.1 christos case 16:
86 1.1 christos sbase = "0x%" PRIx64;
87 1.1 christos break;
88 1.1 christos default:
89 1.1 christos goto internal;
90 1.1 christos }
91 1.1 christos
92 1.1 christos len = snprintf(bp, buflen, sbase, val);
93 1.1 christos if (len < 0)
94 1.1 christos goto internal;
95 1.1 christos
96 1.1 christos if (len < buflen)
97 1.1 christos bp += len;
98 1.1 christos else
99 1.1 christos bp += buflen - 1;
100 1.1 christos
101 1.1 christos /*
102 1.1 christos * If the value we printed was 0 and we're using the old-style format,
103 1.1 christos * we're done.
104 1.1 christos */
105 1.1 christos if ((val == 0) && (ch != '\177'))
106 1.1 christos goto terminate;
107 1.1 christos
108 1.1 christos #define PUTC(c) if (++len < buflen) *bp++ = (c)
109 1.1 christos #define PUTS(s) while ((ch = *(s)++) != 0) PUTC(ch)
110 1.1 christos
111 1.1 christos /*
112 1.1 christos * Chris Torek's new bitmask format is identified by a leading \177
113 1.1 christos */
114 1.1 christos sep = '<';
115 1.1 christos if (ch != '\177') {
116 1.1 christos /* old (standard) format. */
117 1.1 christos for (;(bit = *bitfmt++) != 0;) {
118 1.1 christos if (val & (1 << (bit - 1))) {
119 1.1 christos PUTC(sep);
120 1.1 christos for (; (ch = *bitfmt) > ' '; ++bitfmt)
121 1.1 christos PUTC(ch);
122 1.1 christos sep = ',';
123 1.1 christos } else
124 1.1 christos for (; *bitfmt > ' '; ++bitfmt)
125 1.1 christos continue;
126 1.1 christos }
127 1.1 christos } else {
128 1.1 christos /* new quad-capable format; also does fields. */
129 1.1 christos field = val;
130 1.1 christos while ((ch = *bitfmt++) != '\0') {
131 1.1 christos bit = *bitfmt++; /* now 0-origin */
132 1.1 christos switch (ch) {
133 1.1 christos case 'b':
134 1.1 christos if (((u_int)(val >> bit) & 1) == 0)
135 1.1 christos goto skip;
136 1.1 christos PUTC(sep);
137 1.1 christos PUTS(bitfmt);
138 1.1 christos sep = ',';
139 1.1 christos break;
140 1.1 christos case 'f':
141 1.1 christos case 'F':
142 1.1 christos flen = *bitfmt++; /* field length */
143 1.1 christos field = (val >> bit) &
144 1.1 christos (((uint64_t)1 << flen) - 1);
145 1.1 christos if (ch == 'F') /* just extract */
146 1.1 christos break;
147 1.1 christos PUTC(sep);
148 1.1 christos sep = ',';
149 1.1 christos PUTS(bitfmt);
150 1.1 christos PUTC('=');
151 1.1 christos flen = snprintf(bp, buflen - len, sbase, field);
152 1.1 christos if (flen < 0)
153 1.1 christos goto internal;
154 1.1 christos len += flen;
155 1.1 christos if (len < buflen)
156 1.1 christos bp += flen;
157 1.1 christos break;
158 1.1 christos case '=':
159 1.1 christos case ':':
160 1.1 christos /*
161 1.1 christos * Here "bit" is actually a value instead,
162 1.1 christos * to be compared against the last field.
163 1.1 christos * This only works for values in [0..255],
164 1.1 christos * of course.
165 1.1 christos */
166 1.1 christos if ((int)field != bit)
167 1.1 christos goto skip;
168 1.1 christos if (ch == '=') {
169 1.1 christos PUTC('=');
170 1.1 christos }
171 1.1 christos PUTS(bitfmt);
172 1.1 christos break;
173 1.1 christos default:
174 1.1 christos skip:
175 1.1 christos while (*bitfmt++ != '\0')
176 1.1 christos continue;
177 1.1 christos break;
178 1.1 christos }
179 1.1 christos }
180 1.1 christos }
181 1.1 christos if (sep != '<') {
182 1.1 christos PUTC('>');
183 1.1 christos }
184 1.1 christos terminate:
185 1.1 christos *bp = '\0';
186 1.1 christos return len;
187 1.1 christos internal:
188 1.1 christos #ifndef _KERNEL
189 1.1 christos errno = EINVAL;
190 1.1 christos #endif
191 1.1 christos return -1;
192 1.1 christos }
193 1.2 christos #endif
194