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