summaryrefslogtreecommitdiff
path: root/sum.c
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2022-04-17 21:51:40 -0400
committerJakob Kaivo <jkk@ung.org>2022-04-17 21:51:40 -0400
commit4096678eb5943b9e21f7e7a283270f95bbfe5d86 (patch)
treeeb44759a52cdd7ba3d6971c23cad0c67ac7aabbc /sum.c
parent73d6489ee4eb973f95dbe0f941ef89668a21d882 (diff)
remove more cruft
Diffstat (limited to 'sum.c')
-rw-r--r--sum.c59
1 files changed, 23 insertions, 36 deletions
diff --git a/sum.c b/sum.c
index 3b5c57c..631167a 100644
--- a/sum.c
+++ b/sum.c
@@ -29,57 +29,44 @@
#define UINT16_BIT (16)
#define BLOCK_SIZE (512)
-struct sum {
- uintmax_t size;
- uintmax_t sum;
-};
-
-static struct sum sum_obsolete(FILE *f, int alt)
+static int sum(const char *path, int alt)
{
- struct sum sum = { 0 };
+ FILE *f = stdin;
+ if (path && strcmp(path, "-")) {
+ f = fopen(path, "rb");
+ }
+
+ if (f == NULL) {
+ fprintf(stderr, "sum: %s: %s\n", path, strerror(errno));
+ return 1;
+ }
int c;
+ uintmax_t size = 0;
+ uintmax_t sum = 0;
+
while ((c = fgetc(f)) != EOF) {
- sum.size++;
+ size++;
if (alt) {
- sum.sum = (sum.sum >> 1) +
- ((sum.sum & 1) << (UINT16_BIT - 1));
+ sum = (sum >> 1) + ((sum & 1) << (UINT16_BIT - 1));
}
- sum.sum += c;
+ sum += c;
if (alt) {
- sum.sum &= UINT16_MAX;
+ sum &= UINT16_MAX;
}
}
- sum.sum = (sum.sum & UINT16_MAX) + (sum.sum >> UINT16_BIT);
+ sum = (sum & UINT16_MAX) + (sum >> UINT16_BIT);
- /* obsolete sum program prints number of 512 byte blocks */
- if (sum.size % BLOCK_SIZE != 0) {
- sum.size += BLOCK_SIZE;
+ if (size % BLOCK_SIZE != 0) {
+ size += BLOCK_SIZE;
}
- sum.size /= BLOCK_SIZE;
-
- return sum;
-}
-
-static int cksum(const char *path, int alt)
-{
- FILE *f = stdin;
- if (path && strcmp(path, "-")) {
- f = fopen(path, "rb");
- }
-
- if (f == NULL) {
- fprintf(stderr, "sum: %s: %s\n", path, strerror(errno));
- return 1;
- }
-
- struct sum sum = sum_obsolete(f, alt);
+ size /= BLOCK_SIZE;
- printf("%"PRIuMAX" %"PRIuMAX"", sum.sum, sum.size);
+ printf("%ju %ju", sum, size);
if (f != stdin) {
printf(" %s", path);
@@ -112,7 +99,7 @@ int main(int argc, char *argv[])
int r = 0;
do {
- r |= cksum(argv[optind++], alt);
+ r |= sum(argv[optind++], alt);
} while (optind < argc);
return r;
}