logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git
commit: 0e6c90b249b0f5d37f67d20cbc8d20d026280f4b
parent 2c92c7256ec7d7ca0b31c118588f4d7a2272293f
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Fri, 20 Sep 2024 04:16:34 +0200

cmd/strings: unify error message formatting

Diffstat:

Mcmd/strings.c20++++++++++----------
Mtest-cmd/strings.sh10+++++-----
2 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/cmd/strings.c b/cmd/strings.c @@ -33,7 +33,7 @@ print_string(char *buffer, size_t offset) if(ret < 0) { - fprintf(stderr, "strings: Error writing: %s\n", strerror(errno)); + fprintf(stderr, "strings: error: Failed writing: %s\n", strerror(errno)); errno = 0; return 1; } @@ -89,7 +89,7 @@ concat(int fd, const char *fdname) if(c < 0) { - fprintf(stderr, "strings: Error reading ‘%s’: %s\n", fdname, strerror(errno)); + fprintf(stderr, "strings: error: Failed reading ‘%s’: %s\n", fdname, strerror(errno)); errno = 0; return 1; } @@ -125,19 +125,19 @@ main(int argc, char *argv[]) } if(errno != 0) { - fprintf(stderr, "strings: Option `-n %s`: %s\n", optarg, strerror(errno)); + fprintf(stderr, "strings: error: Option `-n %s`: %s\n", optarg, strerror(errno)); usage(); return 1; } if(opt_min_strlen == LONG_MIN || opt_min_strlen < 1) { - fprintf(stderr, "strings: Option `-n %s` is too small\n", optarg); + fprintf(stderr, "strings: error: Option `-n %s` is too small\n", optarg); usage(); return 1; } if(opt_min_strlen == LONG_MAX || opt_min_strlen > 4096) { - fprintf(stderr, "strings: Option `-n %s` is too large\n", optarg); + fprintf(stderr, "strings: error: Option `-n %s` is too large\n", optarg); usage(); return 1; } @@ -161,7 +161,7 @@ main(int argc, char *argv[]) opt_offset_format = "%zd %s%c"; break; default: - fprintf(stderr, "strings: Unknown format: %s\n", optarg); + fprintf(stderr, "strings: error: Unknown format: %s\n", optarg); usage(); return 1; } @@ -170,11 +170,11 @@ main(int argc, char *argv[]) delim = '\0'; break; case ':': - fprintf(stderr, "strings: Error: Missing operand for option: '-%c'\n", optopt); + fprintf(stderr, "strings: error: Missing operand for option: '-%c'\n", optopt); usage(); return 1; case '?': - fprintf(stderr, "strings: Error: Unrecognised option: '-%c'\n", optopt); + fprintf(stderr, "strings: error: Unrecognised option: '-%c'\n", optopt); usage(); return 1; default: @@ -205,7 +205,7 @@ main(int argc, char *argv[]) int fd = open(argv[argi], O_RDONLY); if(fd <= 0) { - fprintf(stderr, "strings: Error opening ‘%s’: %s\n", argv[argi], strerror(errno)); + fprintf(stderr, "strings: error: Failed opening ‘%s’: %s\n", argv[argi], strerror(errno)); return 1; } @@ -217,7 +217,7 @@ main(int argc, char *argv[]) assert(errno == 0); if(close(fd) < 0) { - fprintf(stderr, "strings: Error closing ‘%s’: %s\n", argv[argi], strerror(errno)); + fprintf(stderr, "strings: error: Failed closing ‘%s’: %s\n", argv[argi], strerror(errno)); return 1; } } diff --git a/test-cmd/strings.sh b/test-cmd/strings.sh @@ -57,18 +57,18 @@ t_file dec:all_bytes "$WD/outputs/strings/all_bytes_td" -td "$WD/inputs/all_byte t_file dec:lengthelf "$WD/outputs/strings/length_td" -td "$WD/inputs/strings/length" t_file dec:length8elf "$WD/outputs/strings/length_8_td" -td -n 8 "$WD/inputs/strings/length" -fmt_t="strings: Unknown format: t +fmt_t="strings: error: Unknown format: t ${usage}" t --exit=1 fmt_t:devnull '-tt /dev/null' "${fmt_t}" t --exit=1 fmt_t:all_bytes "-tt $WD/inputs/all_bytes" "${fmt_t}" t --exit=1 fmt_t:lengthelf "-tt $WD/inputs/strings/length" "${fmt_t}" t --exit=1 fmt_t:length8elf "-tt -n 8 $WD/inputs/strings/length" "${fmt_t}" -t --exit=1 erange_n:0 "-n 0 $WD/inputs/all_bytes" "strings: Option \`-n 0\` is too small +t --exit=1 erange_n:0 "-n 0 $WD/inputs/all_bytes" "strings: error: Option \`-n 0\` is too small ${usage}" -t --exit=1 erange_n:4097 "-n 4097 $WD/inputs/all_bytes" "strings: Option \`-n 4097\` is too large +t --exit=1 erange_n:4097 "-n 4097 $WD/inputs/all_bytes" "strings: error: Option \`-n 4097\` is too large ${usage}" -t --exit=1 erange_n:f "-n f $WD/inputs/all_bytes" "strings: Option \`-n f\`: Invalid argument +t --exit=1 erange_n:f "-n f $WD/inputs/all_bytes" "strings: error: Option \`-n f\`: Invalid argument ${usage}" -t --exit=1 erange_n:42f "-n 42f $WD/inputs/all_bytes" "strings: Option \`-n 42f\`: Invalid argument +t --exit=1 erange_n:42f "-n 42f $WD/inputs/all_bytes" "strings: error: Option \`-n 42f\`: Invalid argument ${usage}"