]> diplodocus.org Git - nmh/blobdiff - sbr/snprintb.c
new.c: Order two return statements to match comment.
[nmh] / sbr / snprintb.c
index 558c2cf8f591d047e04288f76d2919c57663a4bc..47aea95ff971b5bf3f1548bcf6c35e9c678939eb 100644 (file)
@@ -1,8 +1,4 @@
-
-/*
- * snprintb.c -- snprintf a %b string
- *
- * $Id$
+/* snprintb.c -- snprintf a %b string
  *
  * This code is Copyright (c) 2002, by the authors of nmh.  See the
  * COPYRIGHT file in the root directory of the nmh distribution for
 char *
 snprintb (char *buffer, size_t n, unsigned v, char *bits)
 {
-    register int i, j;
-    register char c, *bp;
+    size_t len;
+    int i, j;
+    char c, *bp;
 
     snprintf (buffer, n, bits && *bits == 010 ? "0%o" : "0x%x", v);
-    bp = buffer + strlen(buffer);
+    len = strlen(buffer);
+    bp = buffer + len;
+    n -= len;
 
     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;
     }