]> diplodocus.org Git - nmh/blob - sbr/snprintb.c
Just reworded the bit about '%s' being safe not to quote (it's only safe not to
[nmh] / sbr / snprintb.c
1
2 /*
3 * snprintb.c -- snprintf a %b string
4 *
5 * $Id$
6 */
7
8 #include <h/mh.h>
9
10
11 char *
12 snprintb (char *buffer, size_t n, unsigned v, char *bits)
13 {
14 register int i, j;
15 register char c, *bp;
16
17 snprintf (buffer, n, bits && *bits == 010 ? "0%o" : "0x%x", v);
18 bp = buffer + strlen(buffer);
19
20 if (bits && *++bits) {
21 j = 0;
22 *bp++ = '<';
23 while ((i = *bits++))
24 if (v & (1 << (i - 1))) {
25 if (j++)
26 *bp++ = ',';
27 for (; (c = *bits) > 32; bits++)
28 *bp++ = c;
29 }
30 else
31 for (; *bits > 32; bits++)
32 continue;
33 *bp++ = '>';
34 *bp = 0;
35 }
36
37 return buffer;
38 }