]> diplodocus.org Git - nmh/blobdiff - sbr/snprintb.c
Reworked attach to add charset to Content-Type string for
[nmh] / sbr / snprintb.c
index 06140c8fb53c5b02dd7b2594ea913e60e23d3529..629da4b7dbf0e367bf3a03ddefae7a4eb6414c06 100644 (file)
@@ -2,7 +2,9 @@
 /*
  * snprintb.c -- snprintf a %b string
  *
- * $Id$
+ * This code is Copyright (c) 2002, by the authors of nmh.  See the
+ * COPYRIGHT file in the root directory of the nmh distribution for
+ * complete copyright information.
  */
 
 #include <h/mh.h>
@@ -16,21 +18,28 @@ snprintb (char *buffer, size_t n, unsigned v, char *bits)
 
     snprintf (buffer, n, bits && *bits == 010 ? "0%o" : "0x%x", v);
     bp = buffer + strlen(buffer);
+    n -= strlen(buffer);
 
     if (bits && *++bits) {
        j = 0;
        *bp++ = '<';
-       while ((i = *bits++))
+       while ((i = *bits++) && n > 1)
            if (v & (1 << (i - 1))) {
-               if (j++)
+               if (j++ && n > 1) {
                    *bp++ = ',';
-               for (; (c = *bits) > 32; bits++)
+                   n--;
+               }
+               for (; (c = *bits) > 32 && n > 1; bits++) {
                    *bp++ = c;
+                   n--;
+               }
            }
            else
                for (; *bits > 32; bits++)
                    continue;
-       *bp++ = '>';
+       if (n > 1)
+           *bp++ = '>';
+
        *bp = 0;
     }