]> diplodocus.org Git - nmh/blob - sbr/snprintb.c
Escape literal leading full stop in man/new.man.
[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 size_t len;
17 int i, j;
18 char c, *bp;
19
20 snprintf (buffer, n, bits && *bits == 010 ? "0%o" : "0x%x", v);
21 len = strlen(buffer);
22 bp = buffer + len;
23 n -= len;
24
25 if (bits && *++bits) {
26 j = 0;
27 *bp++ = '<';
28 while ((i = *bits++) && n > 1)
29 if (v & (1 << (i - 1))) {
30 if (j++ && n > 1) {
31 *bp++ = ',';
32 n--;
33 }
34 for (; (c = *bits) > 32 && n > 1; bits++) {
35 *bp++ = c;
36 n--;
37 }
38 }
39 else
40 for (; *bits > 32; bits++)
41 continue;
42 if (n > 1)
43 *bp++ = '>';
44
45 *bp = 0;
46 }
47
48 return buffer;
49 }