logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git
commit: 1713d5da0213c18a0da4bea461274864ea87006a
parent 3ecb327180f95b6142c30996e2cf4e7e93000c19
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sun,  6 Oct 2024 09:19:14 +0200

cmd/sha*sum: explicitly handle : and ? getopt errors

Diffstat:

Mcmd/sha1sum.c52+++++++++++++++++++++++++++++++++++-----------------
Mcmd/sha256sum.c46++++++++++++++++++++++++++++++----------------
Mcmd/sha512sum.c46++++++++++++++++++++++++++++++----------------
3 files changed, 95 insertions(+), 49 deletions(-)

diff --git a/cmd/sha1sum.c b/cmd/sha1sum.c @@ -18,6 +18,8 @@ #define SHA1SUM_LEN SHA1_DIGEST_LENGTH * 2 + 1 +const char *argv0 = "sha1sum"; + static int sha1sum(int fd, const char *fdname, char sum[SHA1SUM_LEN]) { @@ -34,7 +36,8 @@ sha1sum(int fd, const char *fdname, char sum[SHA1SUM_LEN]) if(nread < 0) { fprintf(stderr, - "sha1sum: error: Failed reading file '%s': %s\n", + "%s: error: Failed reading file '%s': %s\n", + argv0, fdname ? fdname : "<stdin>", strerror(errno)); return -1; @@ -77,7 +80,8 @@ check(FILE *file, const char *filename) } fprintf(stderr, - "sha1sum: error: Invalid character '%c' while reading hash in line: %s\n", + "%s: error: Invalid character '%c' while reading hash in line: %s\n", + argv0, line[i], line); if(len > 0) free(line); @@ -85,7 +89,7 @@ check(FILE *file, const char *filename) } if(line[i++] != '\0') { - fprintf(stderr, "sha1sum: error: Invalid line: %s\n", line); + fprintf(stderr, "%s: error: Invalid line: %s\n", argv0, line); if(len > 0) free(line); return -1; } @@ -93,7 +97,8 @@ check(FILE *file, const char *filename) if(i != SHA1SUM_LEN) { fprintf(stderr, - "sha1sum: error: Got %zd hexadecimal digits while expected %d for a SHA1\n", + "%s: error: Got %zd hexadecimal digits while expected %d for a SHA1\n", + argv0, i, SHA1SUM_LEN); if(len > 0) free(line); @@ -110,7 +115,7 @@ check(FILE *file, const char *filename) int fd = open(target, O_RDONLY | O_NOCTTY); if(fd < 0) { - fprintf(stderr, "sha1sum: error: Failed opening file '%s': %s\n", target, strerror(errno)); + fprintf(stderr, "%s: error: Failed opening file '%s': %s\n", argv0, target, strerror(errno)); if(len > 0) free(line); return -1; @@ -119,7 +124,8 @@ check(FILE *file, const char *filename) int ret = posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL); if(ret != 0) fprintf(stderr, - "sha1sum: warning: posix_fadvise failed on file '%s': %s\n", + "%s: warning: posix_fadvise failed on file '%s': %s\n", + argv0, target, strerror(ret)); @@ -137,7 +143,8 @@ check(FILE *file, const char *filename) if(close(fd) < 0) { - fprintf(stderr, "sha1sum: error: Failed closing file '%s': %s\n", filename, strerror(errno)); + fprintf( + stderr, "%s: error: Failed closing file '%s': %s\n", argv0, filename, strerror(errno)); if(len > 0) free(line); return -1; @@ -147,7 +154,8 @@ check(FILE *file, const char *filename) { err = 1; fprintf(stderr, - "sha1sum: error: Failed reading line from file '%s': %s\n", + "%s: error: Failed reading line from file '%s': %s\n", + argv0, filename, strerror(errno)); } @@ -162,16 +170,21 @@ main(int argc, char *argv[]) bool opt_c = false; int c = -1; - while((c = getopt(argc, argv, "c")) != -1) + while((c = getopt(argc, argv, ":c")) != -1) { switch(c) { case 'c': opt_c = true; break; - default: - fprintf(stderr, "sha1sum: error: Unhandled option '-%c'\n", c); + case ':': + fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); return 1; + case '?': + fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); + return 1; + default: + abort(); } } @@ -204,8 +217,11 @@ main(int argc, char *argv[]) file = fopen(filename, "rb"); if(file == NULL) { - fprintf( - stderr, "sha1sum: error: Failed opening file '%s': %s\n", filename, strerror(errno)); + fprintf(stderr, + "%s: error: Failed opening file '%s': %s\n", + argv0, + filename, + strerror(errno)); return 1; } } @@ -215,7 +231,7 @@ main(int argc, char *argv[]) if(fclose(file) < 0) { fprintf( - stderr, "sha1sum: error: Failed closing file '%s': %s\n", filename, strerror(errno)); + stderr, "%s: error: Failed closing file '%s': %s\n", argv0, filename, strerror(errno)); return 1; } } @@ -249,14 +265,15 @@ main(int argc, char *argv[]) if(fd < 0) { fprintf( - stderr, "sha1sum: error: Failed opening file '%s': %s\n", filename, strerror(errno)); + stderr, "%s: error: Failed opening file '%s': %s\n", argv0, filename, strerror(errno)); return 1; } int ret = posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL); if(ret != 0) fprintf(stderr, - "sha1sum: warning: posix_fadvise failed on file '%s': %s\n", + "%s: warning: posix_fadvise failed on file '%s': %s\n", + argv0, filename, strerror(ret)); } @@ -269,7 +286,8 @@ main(int argc, char *argv[]) if(close(fd) < 0) { - fprintf(stderr, "sha1sum: error: Failed closing file '%s': %s\n", filename, strerror(errno)); + fprintf( + stderr, "%s: error: Failed closing file '%s': %s\n", argv0, filename, strerror(errno)); return 1; } diff --git a/cmd/sha256sum.c b/cmd/sha256sum.c @@ -18,6 +18,8 @@ #define SHA256SUM_LEN SHA256_DIGEST_LENGTH * 2 + 1 +const char *argv0 = "sha256sum"; + static int sha256sum(int fd, const char *fdname, char sum[SHA256SUM_LEN]) { @@ -34,7 +36,8 @@ sha256sum(int fd, const char *fdname, char sum[SHA256SUM_LEN]) if(nread < 0) { fprintf(stderr, - "sha256sum: error: Failed reading file '%s': %s\n", + "%s: error: Failed reading file '%s': %s\n", + argv0, fdname ? fdname : "<stdin>", strerror(errno)); return -1; @@ -77,7 +80,8 @@ check(FILE *file, const char *filename) } fprintf(stderr, - "sha256sum: error: Invalid character '%c' while reading hash in line: %s\n", + "%s: error: Invalid character '%c' while reading hash in line: %s\n", + argv0, line[i], line); if(len > 0) free(line); @@ -85,7 +89,7 @@ check(FILE *file, const char *filename) } if(line[i++] != '\0') { - fprintf(stderr, "sha256sum: error: Invalid line: %s\n", line); + fprintf(stderr, "%s: error: Invalid line: %s\n", argv0, line); if(len > 0) free(line); return -1; } @@ -93,7 +97,8 @@ check(FILE *file, const char *filename) if(i != SHA256SUM_LEN) { fprintf(stderr, - "sha256sum: error: Got %zd hexadecimal digits while expected %d for a SHA256\n", + "%s: error: Got %zd hexadecimal digits while expected %d for a SHA256\n", + argv0, i, SHA256SUM_LEN); if(len > 0) free(line); @@ -110,7 +115,7 @@ check(FILE *file, const char *filename) int fd = open(target, O_RDONLY | O_NOCTTY); if(fd < 0) { - fprintf(stderr, "sha256sum: error: Failed opening file '%s': %s\n", target, strerror(errno)); + fprintf(stderr, "%s: error: Failed opening file '%s': %s\n", argv0, target, strerror(errno)); if(len > 0) free(line); return -1; @@ -119,7 +124,8 @@ check(FILE *file, const char *filename) int ret = posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL); if(ret != 0) fprintf(stderr, - "sha256sum: warning: posix_fadvise failed on file '%s': %s\n", + "%s: warning: posix_fadvise failed on file '%s': %s\n", + argv0, target, strerror(ret)); @@ -138,7 +144,7 @@ check(FILE *file, const char *filename) if(close(fd) < 0) { fprintf( - stderr, "sha256sum: error: Failed closing file '%s': %s\n", filename, strerror(errno)); + stderr, "%s: error: Failed closing file '%s': %s\n", argv0, filename, strerror(errno)); if(len > 0) free(line); return -1; @@ -148,7 +154,8 @@ check(FILE *file, const char *filename) { err = 1; fprintf(stderr, - "sha256sum: error: Failed reading line from file '%s': %s\n", + "%s: error: Failed reading line from file '%s': %s\n", + argv0, filename, strerror(errno)); } @@ -163,16 +170,21 @@ main(int argc, char *argv[]) bool opt_c = false; int c = -1; - while((c = getopt(argc, argv, "c")) != -1) + while((c = getopt(argc, argv, ":c")) != -1) { switch(c) { case 'c': opt_c = true; break; - default: - fprintf(stderr, "sha256sum: error: Unhandled option '-%c'\n", c); + case ':': + fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); return 1; + case '?': + fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); + return 1; + default: + abort(); } } @@ -206,7 +218,8 @@ main(int argc, char *argv[]) if(file == NULL) { fprintf(stderr, - "sha256sum: error: Failed opening file '%s': %s\n", + "%s: error: Failed opening file '%s': %s\n", + argv0, filename, strerror(errno)); return 1; @@ -218,7 +231,7 @@ main(int argc, char *argv[]) if(fclose(file) < 0) { fprintf( - stderr, "sha256sum: error: Failed closing file '%s': %s\n", filename, strerror(errno)); + stderr, "%s: error: Failed closing file '%s': %s\n", argv0, filename, strerror(errno)); return 1; } } @@ -252,14 +265,15 @@ main(int argc, char *argv[]) if(fd < 0) { fprintf( - stderr, "sha256sum: error: Failed opening file '%s': %s\n", filename, strerror(errno)); + stderr, "%s: error: Failed opening file '%s': %s\n", argv0, filename, strerror(errno)); return 1; } int ret = posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL); if(ret != 0) fprintf(stderr, - "sha256sum: warning: posix_fadvise failed on file '%s': %s\n", + "%s: warning: posix_fadvise failed on file '%s': %s\n", + argv0, filename, strerror(ret)); } @@ -273,7 +287,7 @@ main(int argc, char *argv[]) if(close(fd) < 0) { fprintf( - stderr, "sha256sum: error: Failed closing file '%s': %s\n", filename, strerror(errno)); + stderr, "%s: error: Failed closing file '%s': %s\n", argv0, filename, strerror(errno)); return 1; } diff --git a/cmd/sha512sum.c b/cmd/sha512sum.c @@ -18,6 +18,8 @@ #define SHA512SUM_LEN SHA512_DIGEST_LENGTH * 2 + 1 +const char *argv0 = "sha512sum"; + static int sha512sum(int fd, const char *fdname, char sum[SHA512SUM_LEN]) { @@ -34,7 +36,8 @@ sha512sum(int fd, const char *fdname, char sum[SHA512SUM_LEN]) if(nread < 0) { fprintf(stderr, - "sha512sum: error: Failed reading file '%s': %s\n", + "%s: error: Failed reading file '%s': %s\n", + argv0, fdname ? fdname : "<stdin>", strerror(errno)); return -1; @@ -77,7 +80,8 @@ check(FILE *file, const char *filename) } fprintf(stderr, - "sha512sum: error: Invalid character '%c' while reading hash in line: %s\n", + "%s: error: Invalid character '%c' while reading hash in line: %s\n", + argv0, line[i], line); if(len > 0) free(line); @@ -85,7 +89,7 @@ check(FILE *file, const char *filename) } if(line[i++] != '\0') { - fprintf(stderr, "sha512sum: error: Invalid line: %s\n", line); + fprintf(stderr, "%s: error: Invalid line: %s\n", argv0, line); if(len > 0) free(line); return -1; } @@ -93,7 +97,8 @@ check(FILE *file, const char *filename) if(i != SHA512SUM_LEN) { fprintf(stderr, - "sha512sum: error: Got %zd hexadecimal digits while expected %d for a SHA512\n", + "%s: error: Got %zd hexadecimal digits while expected %d for a SHA512\n", + argv0, i, SHA512SUM_LEN); if(len > 0) free(line); @@ -110,7 +115,7 @@ check(FILE *file, const char *filename) int fd = open(target, O_RDONLY | O_NOCTTY); if(fd < 0) { - fprintf(stderr, "sha512sum: error: Failed opening file '%s': %s\n", target, strerror(errno)); + fprintf(stderr, "%s: error: Failed opening file '%s': %s\n", argv0, target, strerror(errno)); if(len > 0) free(line); return -1; @@ -119,7 +124,8 @@ check(FILE *file, const char *filename) int ret = posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL); if(ret != 0) fprintf(stderr, - "sha512sum: warning: posix_fadvise failed on file '%s': %s\n", + "%s: warning: posix_fadvise failed on file '%s': %s\n", + argv0, target, strerror(ret)); @@ -138,7 +144,7 @@ check(FILE *file, const char *filename) if(close(fd) < 0) { fprintf( - stderr, "sha512sum: error: Failed closing file '%s': %s\n", filename, strerror(errno)); + stderr, "%s: error: Failed closing file '%s': %s\n", argv0, filename, strerror(errno)); if(len > 0) free(line); return -1; @@ -148,7 +154,8 @@ check(FILE *file, const char *filename) { err = 1; fprintf(stderr, - "sha512sum: error: Failed reading line from file '%s': %s\n", + "%s: error: Failed reading line from file '%s': %s\n", + argv0, filename, strerror(errno)); } @@ -163,16 +170,21 @@ main(int argc, char *argv[]) bool opt_c = false; int c = -1; - while((c = getopt(argc, argv, "c")) != -1) + while((c = getopt(argc, argv, ":c")) != -1) { switch(c) { case 'c': opt_c = true; break; - default: - fprintf(stderr, "sha512sum: error: Unhandled option '-%c'\n", c); + case ':': + fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); return 1; + case '?': + fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); + return 1; + default: + abort(); } } @@ -206,7 +218,8 @@ main(int argc, char *argv[]) if(file == NULL) { fprintf(stderr, - "sha512sum: error: Failed opening file '%s': %s\n", + "%s: error: Failed opening file '%s': %s\n", + argv0, filename, strerror(errno)); return 1; @@ -218,7 +231,7 @@ main(int argc, char *argv[]) if(fclose(file) < 0) { fprintf( - stderr, "sha512sum: error: Failed closing file '%s': %s\n", filename, strerror(errno)); + stderr, "%s: error: Failed closing file '%s': %s\n", argv0, filename, strerror(errno)); return 1; } } @@ -252,14 +265,15 @@ main(int argc, char *argv[]) if(fd < 0) { fprintf( - stderr, "sha512sum: error: Failed opening file '%s': %s\n", filename, strerror(errno)); + stderr, "%s: error: Failed opening file '%s': %s\n", argv0, filename, strerror(errno)); return 1; } int ret = posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL); if(ret != 0) fprintf(stderr, - "sha512sum: warning: posix_fadvise failed on file '%s': %s\n", + "%s: warning: posix_fadvise failed on file '%s': %s\n", + argv0, filename, strerror(ret)); } @@ -273,7 +287,7 @@ main(int argc, char *argv[]) if(close(fd) < 0) { fprintf( - stderr, "sha512sum: error: Failed closing file '%s': %s\n", filename, strerror(errno)); + stderr, "%s: error: Failed closing file '%s': %s\n", argv0, filename, strerror(errno)); return 1; }