summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2022-04-17 21:42:31 -0400
committerJakob Kaivo <jkk@ung.org>2022-04-17 21:42:31 -0400
commit5ce418badefa9638b033217d73b96b7c619084c4 (patch)
treebe2e87e75c465221fc0e9f026be391a725f5e716
parent177b330ace2d96eca9ee16fde1ee92a323a23eb2 (diff)
clean upHEADmaster
-rw-r--r--cksum.c48
1 files changed, 16 insertions, 32 deletions
diff --git a/cksum.c b/cksum.c
index ccaff2c..a48e9f3 100644
--- a/cksum.c
+++ b/cksum.c
@@ -20,22 +20,15 @@
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <inttypes.h>
-#include <libgen.h>
#include <limits.h>
#include <locale.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
-#define MAX_SUM_WIDTH (32)
#define UINT32_BIT (32)
-#define UINT16_BIT (16)
-#define BLOCK_SIZE (512)
-struct sum {
- uintmax_t size;
- uintmax_t sum;
-};
+const uint32_t polynomial = 0x04c11db7;
static uint32_t reverse(uint32_t n, int width)
{
@@ -47,15 +40,24 @@ static uint32_t reverse(uint32_t n, int width)
return r;
}
-static struct sum sum_crc32(FILE *f)
+static int cksum(const char *path)
{
- const uint32_t polynomial = 0x04c11db7;
+ FILE *f = stdin;
+ if (path && strcmp(path, "-")) {
+ f = fopen(path, "rb");
+ }
+
+ if (f == NULL) {
+ fprintf(stderr, "cksum: %s: %s\n", path, strerror(errno));
+ return 1;
+ }
+
uint32_t crc = UINT32_MAX;
- struct sum sum = { 0 };
+ uintmax_t size = 0;
int c;
while ((c = fgetc(f)) != EOF) {
- sum.size++;
+ size++;
crc ^= reverse((uint8_t)c, CHAR_BIT) << (UINT32_BIT - CHAR_BIT);
for (int i = 0; i < CHAR_BIT; i++) {
if (crc & (1 << (UINT32_BIT - 1))) {
@@ -65,27 +67,9 @@ static struct sum sum_crc32(FILE *f)
}
}
}
- crc = reverse(crc, CHAR_BIT * sizeof(crc)) ^ UINT32_MAX;
-
- sum.sum = crc;
- return sum;
-}
-
-int cksum(const char *path)
-{
- FILE *f = stdin;
- if (path && strcmp(path, "-")) {
- f = fopen(path, "rb");
- }
-
- if (f == NULL) {
- fprintf(stderr, "cksum: %s: %s\n", path, strerror(errno));
- return 1;
- }
-
- struct sum sum = sum_crc32(f);
+ crc = reverse(crc, UINT32_BIT) ^ UINT32_MAX;
- printf("%"PRIuMAX" %"PRIuMAX"", sum.sum, sum.size);
+ printf("%"PRIu32" %ju", crc, size);
if (f != stdin) {
printf(" %s", path);