logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git
commit: 2ccd22098e8af5a19f408abf62d0936e5b493f91
parent 96e7a2dd8352d81f735bd2fe4f06fbbd69bf716a
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Thu, 19 Sep 2024 18:32:48 +0200

cmd/cat: unify error message formatting

Diffstat:

Mcmd/cat.c30+++++++++++++++++++++---------
Mtest-cmd/cat.sh12++++++------
2 files changed, 27 insertions(+), 15 deletions(-)

diff --git a/cmd/cat.c b/cmd/cat.c @@ -13,6 +13,8 @@ #include <string.h> // strerror, strncmp #include <unistd.h> // read, write, close, getopt +const char *argv0 = "cat"; + static int concat(int fd, const char *fdname) { @@ -25,7 +27,7 @@ concat(int fd, const char *fdname) assert(errno == 0); if(write(STDOUT_FILENO, buf, (size_t)c) < 0) { - fprintf(stderr, "cat: Error writing: %s\n", strerror(errno)); + fprintf(stderr, "%s: error: Failed writing: %s\n", argv0, strerror(errno)); errno = 0; return 1; } @@ -33,7 +35,8 @@ concat(int fd, const char *fdname) if(c < 0) { - fprintf(stderr, "cat: Error reading ‘%s’: %s\n", fdname, strerror(errno)); + fprintf( + stderr, "%s: error: Failed reading from file '%s': %s\n", argv0, fdname, strerror(errno)); errno = 0; return 1; } @@ -64,7 +67,8 @@ fd_copy(int fd, const char *fdname) errno = 0; continue; default: - fprintf(stderr, "cat: Error copying ‘%s’: %s\n", fdname, strerror(errno)); + fprintf( + stderr, "%s: error: Failed copying file '%s': %s\n", argv0, fdname, strerror(errno)); errno = 0; return 1; } @@ -95,11 +99,11 @@ main(int argc, char *argv[]) // POSIX: Ignored, buffered streams aren't used break; case ':': - fprintf(stderr, "cat: Error: Missing operand for option: '-%c'\n", optopt); + fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); usage(); return 1; case '?': - fprintf(stderr, "cat: Error: Unrecognised option: '-%c'\n", optopt); + fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); usage(); return 1; default: @@ -134,7 +138,11 @@ main(int argc, char *argv[]) int fd = open(argv[argi], O_RDONLY); if(fd < 0) { - fprintf(stderr, "cat: Error opening ‘%s’: %s\n", argv[argi], strerror(errno)); + fprintf(stderr, + "%s: error: Failed opening file '%s': %s\n", + argv0, + argv[argi], + strerror(errno)); errno = 0; return 1; } @@ -147,7 +155,11 @@ main(int argc, char *argv[]) assert(errno == 0); if(close(fd) < 0) { - fprintf(stderr, "cat: Error closing ‘%s’: %s\n", argv[argi], strerror(errno)); + fprintf(stderr, + "%s: error: Failed closing file '%s': %s\n", + argv0, + argv[argi], + strerror(errno)); errno = 0; return 1; } @@ -157,13 +169,13 @@ main(int argc, char *argv[]) if(close(STDIN_FILENO) != 0) { - fprintf(stderr, "cat: Error closing <stdin>: %s\n", strerror(errno)); + fprintf(stderr, "%s: error: Failed closing file <stdin>: %s\n", argv0, strerror(errno)); return 1; } if(close(STDOUT_FILENO) != 0) { - fprintf(stderr, "cat: Error closing <stdout>: %s\n", strerror(errno)); + fprintf(stderr, "%s: error: Failed closing file <stdout>: %s\n", argv0, strerror(errno)); return 1; } diff --git a/test-cmd/cat.sh b/test-cmd/cat.sh @@ -22,7 +22,7 @@ then else touch "$WD/inputs/chmod_000" chmod 0000 "$WD/inputs/chmod_000" - t --exit=1 'noperm' "$WD/inputs/chmod_000" "cat: Error opening ‘$WD/inputs/chmod_000’: Permission denied + t --exit=1 'noperm' "$WD/inputs/chmod_000" "cat: error: Failed opening file '$WD/inputs/chmod_000': Permission denied " chmod 0600 "$WD/inputs/chmod_000" rm "$WD/inputs/chmod_000" @@ -38,15 +38,15 @@ fi if [ "$(uname -s)" = "NetBSD" ]; then skip readslash "NetBSD allows to read directories" else - t --exit=1 readslash / 'cat: Error reading ‘/’: Is a directory -' + t --exit=1 readslash / "cat: error: Failed reading from file '/': Is a directory +" fi -t --exit=1 enoent /var/empty/e/no/ent 'cat: Error opening ‘/var/empty/e/no/ent’: No such file or directory -' +t --exit=1 enoent /var/empty/e/no/ent "cat: error: Failed opening file '/var/empty/e/no/ent': No such file or directory +" t_file doubledash "$WD/inputs/all_bytes" -- "$WD/inputs/all_bytes" -t --exit=1 tripledash "--- $WD/inputs/all_bytes" "cat: Error: Unrecognised option: '--' +t --exit=1 tripledash "--- $WD/inputs/all_bytes" "cat: error: Unrecognised option: '--' Usage: cat [-u] [files ...] "