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.
This commit is contained in:
parent
3c33abc520
commit
d6818a3c5f
2
README
2
README
@ -16,7 +16,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
|
|||||||
=*| chmod yes none
|
=*| chmod yes none
|
||||||
=*| chown yes none
|
=*| chown yes none
|
||||||
=*| chroot non-posix none
|
=*| chroot non-posix none
|
||||||
=* cksum yes none
|
=*| cksum yes none
|
||||||
=* cmp yes none
|
=* cmp yes none
|
||||||
#*| cols non-posix none
|
#*| cols non-posix none
|
||||||
col yes none
|
col yes none
|
||||||
|
34
cksum.c
34
cksum.c
@ -61,32 +61,33 @@ static const unsigned long crctab[] = { 0x00000000,
|
|||||||
static void
|
static void
|
||||||
cksum(FILE *fp, const char *s)
|
cksum(FILE *fp, const char *s)
|
||||||
{
|
{
|
||||||
unsigned char buf[BUFSIZ];
|
size_t len = 0, i, n;
|
||||||
uint32_t ck = 0;
|
uint32_t ck = 0;
|
||||||
size_t len = 0;
|
unsigned char buf[BUFSIZ];
|
||||||
size_t i, n;
|
|
||||||
|
|
||||||
while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) {
|
while ((n = fread(buf, 1, sizeof(buf), fp))) {
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
ck = (ck << 8) ^ crctab[(ck >> 24) ^ buf[i]];
|
ck = (ck << 8) ^ crctab[(ck >> 24) ^ buf[i]];
|
||||||
len += n;
|
len += n;
|
||||||
}
|
}
|
||||||
if (ferror(fp))
|
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)];
|
ck = (ck << 8) ^ crctab[(ck >> 24) ^ (i & 0xFF)];
|
||||||
|
|
||||||
printf("%"PRIu32" %lu", ~ck, (unsigned long)len);
|
printf("%"PRIu32" %zu", ~ck, len);
|
||||||
if (s)
|
if (s) {
|
||||||
printf(" %s", s);
|
putchar(' ');
|
||||||
|
fputs(s, stdout);
|
||||||
|
}
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
usage(void)
|
usage(void)
|
||||||
{
|
{
|
||||||
eprintf("usage: %s [files ...]\n", argv0);
|
eprintf("usage: %s [file ...]\n", argv0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
@ -99,17 +100,18 @@ main(int argc, char *argv[])
|
|||||||
usage();
|
usage();
|
||||||
} ARGEND;
|
} ARGEND;
|
||||||
|
|
||||||
if (argc == 0)
|
if (!argc) {
|
||||||
cksum(stdin, NULL);
|
cksum(stdin, NULL);
|
||||||
else {
|
} else {
|
||||||
for (; argc > 0; argc--, argv++) {
|
for (; *argv; argc--, argv++) {
|
||||||
if (!(fp = fopen(argv[0], "r"))) {
|
if (!(fp = fopen(*argv, "r"))) {
|
||||||
weprintf("fopen %s:", argv[0]);
|
weprintf("fopen %s:", *argv);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
cksum(fp, argv[0]);
|
cksum(fp, *argv);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user