]> diplodocus.org Git - nmh/blob - sbr/snprintb.c
Fix a segfault that happens when using the -file option.
[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
22 if (bits && *++bits) {
23 j = 0;
24 *bp++ = '<';
25 while ((i = *bits++))
26 if (v & (1 << (i - 1))) {
27 if (j++)
28 *bp++ = ',';
29 for (; (c = *bits) > 32; bits++)
30 *bp++ = c;
31 }
32 else
33 for (; *bits > 32; bits++)
34 continue;
35 *bp++ = '>';
36 *bp = 0;
37 }
38
39 return buffer;
40 }