[hackers] [sbase] Audit cksum(1) || FRIGN

From: <git_AT_suckless.org>
Date: Wed, 11 Mar 2015 13:42:33 +0100 (CET)

commit 468fa6db5186bdb4be3d6c899f1ffcb0bd0421ee
Author: FRIGN <dev_AT_frign.de>
Date: Wed Mar 11 00:13:48 2015 +0100

    Audit cksum(1)
    
    1) Reorder local variables.
    2) Cleanup error messages, use %zu for size_t.
    3) combine putchar(' ') and fputs to substitute printf(" %s", s).
    4) Fix usage().
    5) argv-argc-usage-fix.
    6) Add empty line before return.

diff --git a/README b/README
index 2df3f42..f5a17bb 100644
--- a/README
+++ b/README
_AT_@ -16,7 +16,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
 =*| chmod yes none
 =*| chown yes none
 =*| chroot non-posix none
-=* cksum yes none
+=*| cksum yes none
 =* cmp yes none
 #*| cols non-posix none
     col yes none
diff --git a/cksum.c b/cksum.c
index 6f099fa..59a4f88 100644
--- a/cksum.c
+++ b/cksum.c
_AT_@ -61,32 +61,33 @@ static const unsigned long crctab[] = { 0x00000000,
 static void
 cksum(FILE *fp, const char *s)
 {
- unsigned char buf[BUFSIZ];
+ size_t len = 0, i, n;
         uint32_t ck = 0;
- size_t len = 0;
- size_t i, n;
+ unsigned char buf[BUFSIZ];
 
- while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) {
+ while ((n = fread(buf, 1, sizeof(buf), fp))) {
                 for (i = 0; i < n; i++)
                         ck = (ck << 8) ^ crctab[(ck >> 24) ^ buf[i]];
                 len += n;
         }
         if (ferror(fp))
- eprintf("%s: read error:", s ? s : "<stdin>");
+ eprintf("fread %s:", s ? s : "<stdin>");
 
- for (i = len; i > 0; i >>= 8)
+ for (i = len; i; i >>= 8)
                 ck = (ck << 8) ^ crctab[(ck >> 24) ^ (i & 0xFF)];
 
- printf("%"PRIu32" %lu", ~ck, (unsigned long)len);
- if (s)
- printf(" %s", s);
+ printf("%"PRIu32" %zu", ~ck, len);
+ if (s) {
+ putchar(' ');
+ fputs(s, stdout);
+ }
         putchar('\n');
 }
 
 static void
 usage(void)
 {
- eprintf("usage: %s [files ...]\n", argv0);
+ eprintf("usage: %s [file ...]\n", argv0);
 }
 
 int
_AT_@ -99,17 +100,18 @@ main(int argc, char *argv[])
                 usage();
         } ARGEND;
 
- if (argc == 0)
+ if (!argc) {
                 cksum(stdin, NULL);
- else {
- for (; argc > 0; argc--, argv++) {
- if (!(fp = fopen(argv[0], "r"))) {
- weprintf("fopen %s:", argv[0]);
+ } else {
+ for (; *argv; argc--, argv++) {
+ if (!(fp = fopen(*argv, "r"))) {
+ weprintf("fopen %s:", *argv);
                                 continue;
                         }
- cksum(fp, argv[0]);
+ cksum(fp, *argv);
                         fclose(fp);
                 }
         }
+
         return 0;
 }
Received on Wed Mar 11 2015 - 13:42:33 CET

This archive was generated by hypermail 2.3.0 : Wed Mar 11 2015 - 13:48:14 CET