logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git
commit: 41049d427e924f99cd0f2ff9f67baafafa78fd02
parent 39d74bbc14ae39af717ea7c886bdb30f1796a2b1
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Fri, 20 Sep 2024 04:53:31 +0200

cmd/wc: unify error message formatting

Diffstat:

Mcmd/wc.c23+++++++++++++----------
Mtest-cmd/wc.sh2+-
2 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/cmd/wc.c b/cmd/wc.c @@ -70,7 +70,7 @@ wc_file_bytes(FILE *file, char *filename) if(errno != 0) { fprintf(stderr, - "%s: Failed reading from file '%s': %s\n", + "%s: error: Failed reading from file '%s': %s\n", argv0, filename != NULL ? filename : "<stdin>", strerror(errno)); @@ -119,7 +119,7 @@ wc_file_chars(FILE *file, char *filename) if(errno != 0) { fprintf(stderr, - "%s: Failed reading from file '%s': %s\n", + "%s: error: Failed reading from file '%s': %s\n", argv0, filename != NULL ? filename : "<stdin>", strerror(errno)); @@ -165,7 +165,7 @@ main(int argc, char *argv[]) setlocale(LC_ALL, ""); if(errno != 0) { - fprintf(stderr, "%s: Warning: Failed to initialize locales: %s\n", argv0, strerror(errno)); + fprintf(stderr, "%s: warning: Failed to initialize locales: %s\n", argv0, strerror(errno)); errno = 0; } int (*wc_file)(FILE *, char *) = &wc_file_bytes; @@ -206,11 +206,11 @@ main(int argc, char *argv[]) wc_opts |= WC_OPT_W; break; case ':': - fprintf(stderr, "%s: Error: Missing operand for option: '-%c'\n", argv0, optopt); + fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); usage(); return 1; case '?': - fprintf(stderr, "%s: Error: Unrecognised option: '-%c'\n", argv0, optopt); + fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); usage(); return 1; default: @@ -243,7 +243,7 @@ main(int argc, char *argv[]) if(strchr(path, '\n') != NULL) fprintf( stderr, - "%s: Warning: Filename '%s' contains a newline while wc(1) uses newlines as separators\n", + "%s: warning: Filename '%s' contains a newline while wc(1) uses newlines as separators\n", argv0, path); @@ -252,8 +252,11 @@ main(int argc, char *argv[]) struct stat status; if(stat(path, &status) < 0) { - fprintf( - stderr, "%s: Failed getting status for file '%s': %s\n", argv0, path, strerror(errno)); + fprintf(stderr, + "%s: error: Failed getting status for file '%s': %s\n", + argv0, + path, + strerror(errno)); return 1; } @@ -265,7 +268,7 @@ main(int argc, char *argv[]) FILE *file = fopen(path, "r"); if(file == NULL) { - fprintf(stderr, "%s: Failed opening file '%s': %s\n", argv0, path, strerror(errno)); + fprintf(stderr, "%s: error: Failed opening file '%s': %s\n", argv0, path, strerror(errno)); return 1; } @@ -273,7 +276,7 @@ main(int argc, char *argv[]) if(fclose(file) < 0) { - fprintf(stderr, "%s: Failed closing file '%s': %s\n", argv0, path, strerror(errno)); + fprintf(stderr, "%s: error: Failed closing file '%s': %s\n", argv0, path, strerror(errno)); return 1; } } diff --git a/test-cmd/wc.sh b/test-cmd/wc.sh @@ -48,7 +48,7 @@ t --input='' 'empty_stdin' '' '0 0 0 t 'empty_file' "${WD}/test-cmd/inputs/empty" "0 0 0 ${WD}/test-cmd/inputs/empty " -t --exit=1 'enoent' '/var/empty/e/no/ent' "wc: Failed opening file '/var/empty/e/no/ent': No such file or directory +t --exit=1 'enoent' '/var/empty/e/no/ent' "wc: error: Failed opening file '/var/empty/e/no/ent': No such file or directory " # Formatting with reduced counts