]> diplodocus.org Git - nmh/blob - sbr/snprintb.c
pending-release-notes: add mhshow's "-prefer", and mh-format's %(kibi/kilo)
[nmh] / sbr / snprintb.c
1
2 /*
3 * snprintb.c -- snprintf a %b string
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11
12
13 char *
14 snprintb (char *buffer, size_t n, unsigned v, char *bits)
15 {
16 register int i, j;
17 register char c, *bp;
18
19 snprintf (buffer, n, bits && *bits == 010 ? "0%o" : "0x%x", v);
20 bp = buffer + strlen(buffer);
21 n -= strlen(buffer);
22
23 if (bits && *++bits) {
24 j = 0;
25 *bp++ = '<';
26 while ((i = *bits++) && n > 1)
27 if (v & (1 << (i - 1))) {
28 if (j++ && n > 1) {
29 *bp++ = ',';
30 n--;
31 }
32 for (; (c = *bits) > 32 && n > 1; bits++) {
33 *bp++ = c;
34 n--;
35 }
36 }
37 else
38 for (; *bits > 32; bits++)
39 continue;
40 if (n > 1)
41 *bp++ = '>';
42
43 *bp = 0;
44 }
45
46 return buffer;
47 }