]> thyrsus.com Git - ascii.git/commitdiff
Clean up C to C99 level.
authorEric S. Raymond <esr@thyrsus.com>
Tue, 19 Oct 2010 05:09:50 +0000 (01:09 -0400)
committerEric S. Raymond <esr@thyrsus.com>
Tue, 19 Oct 2010 05:09:50 +0000 (01:09 -0400)
ascii.c

diff --git a/ascii.c b/ascii.c
index 32c77521518356e7041ba5fea9ab087e667dda53..1c621cda9f74b38a45929bb2b644df6d27bb5642 100644 (file)
--- a/ascii.c
+++ b/ascii.c
 #include <ctype.h>
 #include <string.h>
 #include <stdlib.h>
+#include <stdbool.h>
 
 typedef char   *string;
 
-static unsigned char terse = 0;
-static unsigned char help = 0;
-static unsigned char line = 0;
+static bool terse = false;
+static bool help = false;
+static bool line = false;
 
 enum { decimal, hex, octal } mode;
 
@@ -35,6 +36,7 @@ static string cnames[128][16] =
 #include "nametable.h"
     }
 };
+
 /******************************************************************/
 static int atob(str)
 /* ASCII-to-binary conversion */
@@ -42,7 +44,7 @@ char  *str;
 {
     int        val;
 
-    for (val = 0; *str; str++)
+    for (val = 0; *str != '\0'; str++)
     {
        val *= 2;
 
@@ -59,7 +61,7 @@ void
 showHelp(FILE *out, char *progname) 
 {
   fprintf(out,"Usage: %s [-dxohv] [-t] [char-alias...]\n", progname); 
-#define P(s)   fputs(s "\n", out);
+#define P(s)   (void)fputs(s "\n", out);
 #include "splashscreen.h"
 #undef P
 
@@ -76,11 +78,11 @@ process_options(int argc, char *argv[], char * opt_string)
 
        switch (op) {
        case 't':
-           terse = 1; 
+           terse = true; 
            break;
        case 's':
-           terse=1; 
-           line=1; 
+           terse = true; 
+           line = true; 
            break;
        case 'd':
             print_table(decimal);
@@ -93,20 +95,20 @@ process_options(int argc, char *argv[], char * opt_string)
             exit(0);
        case '?':
        case 'h':
-           help = 1;
+           help = true;
            break;
        case 'v':
-           printf("ascii %2.1f\n",REVISION); exit(0) ;
-           break;
+           printf("ascii %2.1f\n",REVISION); 
+           exit(0);
        default :
-           help = 1;
+           help = true;
            break;
         } /*switch*/
     }/*while*/
 }
 
 /******************************************************************/
-static char *btoa(val)
+static char *btoa(unsigned int val)
 /* binary-to-ASCII conversion */
 {
 #define BITSPERCHAR    8
@@ -116,10 +118,10 @@ static char *btoa(val)
     /* write out external representation at least one char */
     *(rp = rep + BITSPERCHAR) = '\0';
     do {
-       *--rp = (val & 1) + '0'; /* Is '1' == '0' + 1 in EBCDIC? */
+       *--rp = (char)('0' + (val & 1)); /* Is '1' == '0' + 1 in EBCDIC? */
        val /= 2;
     } while
-       (val && rp > rep);
+       (val != 0 && rp > rep);
 
 #ifndef SHORT_BINARY_REPRESENTATION
     while (rp > rep)
@@ -130,20 +132,19 @@ static char *btoa(val)
 }
 
 /******************************************************************/
-static void speak(ch)
+static void speak(unsigned int ch)
 /* list all the names for a given character */
-int    ch;
 {
     char       **ptr = &cnames[ch][0];
 
     if (terse) {
-        (void) printf("%d/%d   %d   0x%02X   0o%o   %s\n", 
+        (void) printf("%u/%u   %u   0x%02X   0o%o   %s\n", 
                      ch / 16, ch % 16, ch, ch, ch, btoa(ch));
         return;
     }
 
     (void) printf(
-       "ASCII %d/%d is decimal %03d, hex %02x, octal %03o, bits %s: ",
+       "ASCII %u/%u is decimal %03u, hex %02x, octal %03o, bits %s: ",
        ch / 16, ch % 16, ch, ch, ch, btoa(ch));
 
     /* display high-half characters */
@@ -152,16 +153,16 @@ int       ch;
        ch &=~ 0x80;
        if (ch == 0x7f)
            (void) printf("meta-^?\n");
-       else if (isprint(ch))
-           (void) printf("meta-%c\n", ch);
+       else if (isprint((int)ch))
+           (void) printf("meta-%c\n", (char)ch);
        else
            (void) printf("meta-^%c\n", '@' + (ch & 0x1f));
        return;
     }
 
-    if (isprint(ch))
+if (isprint((int)ch))
        (void) printf("prints as `%s'\n", *ptr++);
-    else if (iscntrl(ch) || ch == 0x7F)
+    else if (iscntrl((char)ch) || ch == 0x7F)
     {
        if (ch == 0x7f)
            (void) printf("called ^?");
@@ -190,7 +191,7 @@ int ch;
                commentary = *ptr;
            else
                (void) printf("%s%s ", *ptr, 
-                             (ptr[1] && *ptr[1] != '#') ? "," : "");
+                             (ptr[1]!=NULL && *ptr[1] != '#') ? "," : "");
        (void) putchar('\n');
        if (commentary)
            (void) printf("Note: %s\n", commentary+2);
@@ -198,18 +199,18 @@ int       ch;
 
     (void) putchar('\n');
 }
+
 /******************************************************************/
-static int stricmp(s, t)
+static int stricmp(char        *s, char *t)
 /* case-blind string compare */
-register char  *s, *t;
 {
     while (*s && tolower(*s) == tolower(*t))
        s++, t++;
     return(*t - *s);
 }
+
 /******************************************************************/
-static void ascii(str)
-char *str;
+static void ascii(char *str)
 {
     int        ch, hi, lo;
     char **ptr;
@@ -353,6 +354,7 @@ char *str;
                    speak(ch);
     } /* outer if */
 }
+
 /******************************************************************/
 void
 print_table(unsigned short delivery)
@@ -381,10 +383,9 @@ print_table(unsigned short delivery)
    }
        
 }
+
 /*----------------------------------------- M A I N --------------*/
-int main(argc, argv)
-int argc;
-char **argv;
+int main(int argc, char **argv)
 {
     char      *optstring="tshvxod" ;