diff options
-rw-r--r-- | cksum.c | 102 |
1 files changed, 5 insertions, 97 deletions
@@ -1,7 +1,7 @@ /* * UNG's Not GNU * - * Copyright (c) 2011-2017, Jakob Kaivo <jkk@ung.org> + * Copyright (c) 2011-2022, Jakob Kaivo <jkk@ung.org> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -32,15 +32,11 @@ #define UINT16_BIT (16) #define BLOCK_SIZE (512) -enum algorithm { UNSPECIFIED, ALTERNATIVE, CRC32 }; - struct sum { uintmax_t size; uintmax_t sum; }; -static char *progname = NULL; - static uint32_t reverse(uint32_t n, int width) { uint32_t r = 0; @@ -75,48 +71,7 @@ static struct sum sum_crc32(FILE *f) return sum; } -static struct sum sum_obsolete(FILE *f, int alt) -{ - struct sum sum = { 0 }; - - int c; - while ((c = fgetc(f)) != EOF) { - sum.size++; - - if (alt) { - sum.sum = (sum.sum >> 1) + - ((sum.sum & 1) << (UINT16_BIT - 1)); - } - - sum.sum += c; - - if (alt) { - sum.sum &= UINT16_MAX; - } - } - - sum.sum = (sum.sum & UINT16_MAX) + (sum.sum >> UINT16_BIT); - - /* obsolete sum program prints number of 512 byte blocks */ - if (sum.size % BLOCK_SIZE != 0) { - sum.size += BLOCK_SIZE; - } - sum.size /= BLOCK_SIZE; - - return sum; -} - -static struct sum sum_unspecified(FILE *f) -{ - return sum_obsolete(f, 0); -} - -static struct sum sum_alternative(FILE *f) -{ - return sum_obsolete(f, 1); -} - -int cksum(const char *path, enum algorithm alg) +int cksum(const char *path) { FILE *f = stdin; if (path && strcmp(path, "-")) { @@ -124,27 +79,11 @@ int cksum(const char *path, enum algorithm alg) } if (f == NULL) { - fprintf(stderr, "%s: %s: %s\n", progname, path, strerror(errno)); + fprintf(stderr, "cksum: %s: %s\n", path, strerror(errno)); return 1; } - struct sum sum; - switch (alg) { - case UNSPECIFIED: - sum = sum_unspecified(f); - break; - - case ALTERNATIVE: - sum = sum_alternative(f); - break; - - case CRC32: - sum = sum_crc32(f); - break; - - default: - break; - } + struct sum sum = sum_crc32(f); printf("%"PRIuMAX" %"PRIuMAX"", sum.sum, sum.size); @@ -157,48 +96,17 @@ int cksum(const char *path, enum algorithm alg) return 0; } -int sum_main(int argc, char *argv[]) -{ - fprintf(stderr, "sum: utility is obsolete; use cksum\n"); - - enum algorithm alg = UNSPECIFIED; - - int c; - while ((c = getopt(argc, argv, "r")) != -1) { - switch (c) { - case 'r': - alg = ALTERNATIVE; - break; - - default: - return 1; - } - } - - int r = 0; - do { - r |= cksum(argv[optind++], alg); - } while (optind < argc); - return r; -} - int main(int argc, char *argv[]) { setlocale(LC_ALL, ""); - progname = basename(argv[0]); - if (!strcmp(progname, "sum")) { - return sum_main(argc, argv); - } - while (getopt(argc, argv, "") != -1) { return 1; } int r = 0; do { - r |= cksum(argv[optind++], CRC32); + r |= cksum(argv[optind++]); } while (optind < argc); return r; } - |