commit: cb6d3e8769b8bc5d19e0bc6b5f7f9daf23094821
parent fdb4d4697d53e777ec7a20fce4b6e139bda22c69
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Thu, 19 Sep 2024 19:01:47 +0200
cmd/cmp: unify error message formatting
Diffstat:
2 files changed, 26 insertions(+), 14 deletions(-)
diff --git a/cmd/cmp.c b/cmd/cmp.c
@@ -13,6 +13,7 @@
static bool opt_s = false, opt_l = false;
static unsigned long max_bytes = 0;
+const char *argv0 = "cmp";
#undef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
@@ -31,8 +32,12 @@ do_cmp(FILE *file1, const char *name1, FILE *file2, const char *name2)
{
if(!ferror(file1)) return 0;
- fprintf(
- stderr, "cmp: Failed to read line %ld from file '%s': %s\n", ln, name1, strerror(errno));
+ fprintf(stderr,
+ "%s: error: Failed to read line %ld from file '%s': %s\n",
+ argv0,
+ ln,
+ name1,
+ strerror(errno));
return 1;
}
@@ -41,13 +46,17 @@ do_cmp(FILE *file1, const char *name1, FILE *file2, const char *name2)
{
if(!ferror(file2))
{
- if(!opt_s) fprintf(stderr, "cmp: EOF on %s line %ld\n", name2, ln);
+ if(!opt_s) fprintf(stderr, "%s: error: EOF on %s line %ld\n", argv0, name2, ln);
return 1;
}
- fprintf(
- stderr, "cmp: Failed to read line %ld from file '%s': %s\n", ln, name1, strerror(errno));
+ fprintf(stderr,
+ "%s: error: Failed to read line %ld from file '%s': %s\n",
+ argv0,
+ ln,
+ name1,
+ strerror(errno));
return 2;
}
@@ -104,22 +113,25 @@ main(int argc, char *argv[])
max_bytes = strtoul(optarg, &endptr, 0);
if(errno != 0)
{
- fprintf(stderr, "cmp: Error: Failed parsing '-n %s': %s\n", optarg, strerror(errno));
+ fprintf(stderr, "%s: error: Failed parsing '-n %s': %s\n", argv0, optarg, strerror(errno));
return 1;
}
if(endptr != NULL && endptr[0] != 0)
{
- fprintf(
- stderr, "cmp: Error: Non-numeric characters passed to '-n %s': %s\n", optarg, endptr);
+ fprintf(stderr,
+ "%s: error: Non-numeric characters passed to '-n %s': %s\n",
+ argv0,
+ optarg,
+ endptr);
return 1;
}
break;
case ':':
- fprintf(stderr, "cmp: 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, "cmp: Error: Unrecognised option: '-%c'\n", optopt);
+ fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt);
usage();
return 1;
default:
@@ -132,7 +144,7 @@ main(int argc, char *argv[])
if(argc != 2)
{
- fprintf(stderr, "cmp: Expected 2 arguments, got %d arguments\n", argc);
+ fprintf(stderr, "%s: error: Expected 2 arguments, got %d arguments\n", argv0, argc);
return 1;
}
@@ -148,7 +160,7 @@ main(int argc, char *argv[])
file1 = fopen(argv[0], "r");
if(file1 == NULL)
{
- fprintf(stderr, "cmp: Error opening ‘%s’: %s\n", argv[0], strerror(errno));
+ fprintf(stderr, "%s: error: Failed opening file '%s': %s\n", argv0, argv[0], strerror(errno));
return 1;
}
}
@@ -161,7 +173,7 @@ main(int argc, char *argv[])
file2 = fopen(argv[1], "r");
if(file2 == NULL)
{
- fprintf(stderr, "cmp: Error opening ‘%s’: %s\n", argv[1], strerror(errno));
+ fprintf(stderr, "%s: error: Failed opening file '%s': %s\n", argv0, argv[1], strerror(errno));
return 1;
}
}
diff --git a/test-cmd/cmp.sh b/test-cmd/cmp.sh
@@ -25,7 +25,7 @@ rm foo bar
seq 1 3 > seq_1_3
seq 1 2 > seq_1_2
-t --exit=1 seq_1_3-1_2 'seq_1_3 seq_1_2' 'cmp: EOF on seq_1_2 line 3
+t --exit=1 seq_1_3-1_2 'seq_1_3 seq_1_2' 'cmp: error: EOF on seq_1_2 line 3
'
t --exit=1 s_seq_1_3-1_2 '-s seq_1_3 seq_1_2'