logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git
commit: fdb4d4697d53e777ec7a20fce4b6e139bda22c69
parent 1ddfde2bc7463991444614e7668a20f48ac3492d
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Thu, 19 Sep 2024 18:56:02 +0200

cmd/cksum: unify error message formatting

Diffstat:

Mcmd/cksum.c14++++++++++----
Mtest-cmd/cksum.sh2+-
2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/cmd/cksum.c b/cmd/cksum.c @@ -10,6 +10,8 @@ #include <string.h> // strerror #include <unistd.h> // read, write, close, getopt +const char *argv0 = "cksum"; + // Table taken from POSIX.1-2024 cksum(1) // https://pubs.opengroup.org/onlinepubs/9799919799/utilities/cksum.html // clang-format off @@ -89,7 +91,8 @@ cksum(int fd, const char *fdname) if(nread < 0) { fprintf(stderr, - "cksum: I/O Error while reading file '%s': %s\n", + "%s: error: Failed reading from file '%s': %s\n", + argv0, fdname ? fdname : "<stdin>", strerror(errno)); return -1; @@ -138,14 +141,16 @@ main(int argc, char *argv[]) fd = open(filename, O_RDONLY | O_NOCTTY); if(fd < 0) { - fprintf(stderr, "cksum: 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; } int ret = posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL); if(ret != 0) fprintf(stderr, - "cksum: Warning: posix_fadvise failed on file '%s': %s\n", + "%s: warning: posix_fadvise failed on file '%s': %s\n", + argv0, filename, strerror(ret)); } @@ -155,7 +160,8 @@ main(int argc, char *argv[]) if(close(fd) < 0) { - fprintf(stderr, "cksum: 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/test-cmd/cksum.sh b/test-cmd/cksum.sh @@ -22,5 +22,5 @@ t --input='' 'empty_stdin' '' '4294967295 0 t all_bytes "$WD/inputs/all_bytes" "1313719201 256 $WD/inputs/all_bytes " -t --exit=1 'enoent' '/var/empty/e/no/ent' "cksum: Error: Failed opening file '/var/empty/e/no/ent': No such file or directory +t --exit=1 'enoent' '/var/empty/e/no/ent' "cksum: error: Failed opening file '/var/empty/e/no/ent': No such file or directory "