commit: d9da7cbaab068ecbb0149db9ed44b19d3810d840
parent cb6d3e8769b8bc5d19e0bc6b5f7f9daf23094821
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Thu, 19 Sep 2024 19:20:21 +0200
cmd/cut: unify error message formatting
Diffstat:
2 files changed, 34 insertions(+), 26 deletions(-)
diff --git a/cmd/cut.c b/cmd/cut.c
@@ -34,6 +34,7 @@ enum cut_mode mode = CUT_MODE_NONE;
bool *list = NULL;
size_t list_len = 0;
bool nostop = false;
+const char *argv0 = "cut";
static ssize_t
parse_list_num(char **s)
@@ -43,19 +44,19 @@ parse_list_num(char **s)
size_t n = strtoul(*s, &endptr, 10);
if(errno != 0)
{
- fprintf(stderr, "cut: Error while parsing '%s' as a number: %s\n", *s, strerror(errno));
+ fprintf(stderr, "%s: error: Failed parsing '%s' as a number: %s\n", argv0, *s, strerror(errno));
return -1;
}
if(n < 1)
{
- fprintf(stderr, "cut: Invalid number in list: %zu\n", n);
+ fprintf(stderr, "%s: error: Invalid number in list: %zu\n", argv0, n);
return -1;
}
if(endptr != NULL && strchr(",-", *endptr) == NULL)
{
- fprintf(stderr, "cut: Invalid character in list: %c\n", *endptr);
+ fprintf(stderr, "%s: error: Invalid character in list: %c\n", argv0, *endptr);
return -1;
}
@@ -73,7 +74,7 @@ parse_list(char *s)
if(*s == ',')
{
- fprintf(stderr, "cut: Error: empty list element\n");
+ fprintf(stderr, "%s: error: Empty list element\n", argv0);
return -1;
}
@@ -102,7 +103,7 @@ parse_list(char *s)
if(max < min)
{
- fprintf(stderr, "cut: Error: decreasing range: %zu-%zu\n", min, max);
+ fprintf(stderr, "%s: error: Decreasing range: %zu-%zu\n", argv0, min, max);
return -1;
}
}
@@ -116,7 +117,7 @@ parse_list(char *s)
list = reallocarray(list, (size_t)max, sizeof(*list));
if(list == NULL)
{
- fprintf(stderr, "cut: Memory allocation error: %s\n", strerror(errno));
+ fprintf(stderr, "%s: error: Failed memory allocation: %s\n", argv0, strerror(errno));
return -1;
}
@@ -133,7 +134,7 @@ parse_list(char *s)
if(list_len == 0)
{
- fprintf(stderr, "cut: Error: empty list\n");
+ fprintf(stderr, "%s: error: Empty list\n", argv0);
return -1;
}
@@ -156,7 +157,8 @@ cut_b(FILE *in, const char *filename)
{
if(errno != 0)
{
- fprintf(stderr, "cut: Error while reading file '%s': %s\n", filename, strerror(errno));
+ fprintf(
+ stderr, "%s: error: Failed reading file '%s': %s\n", argv0, filename, strerror(errno));
err = 1;
}
break;
@@ -201,7 +203,8 @@ cut_c(FILE *in, const char *filename)
{
if(errno != 0)
{
- fprintf(stderr, "cut: Error while reading file '%s': %s\n", filename, strerror(errno));
+ fprintf(
+ stderr, "%s: error: Failed reading file '%s': %s\n", argv0, filename, strerror(errno));
err = 1;
}
break;
@@ -220,7 +223,7 @@ cut_c(FILE *in, const char *filename)
line_w = reallocarray(line_w, nread, sizeof(*line_w));
if(line_w == NULL)
{
- fprintf(stderr, "cut: Memory allocation error: %s\n", strerror(errno));
+ fprintf(stderr, "%s: error: Failed memory allocation: %s\n", argv0, strerror(errno));
return -1;
}
line_wsz = nread;
@@ -232,7 +235,8 @@ cut_c(FILE *in, const char *filename)
if(wcread == (size_t)-1)
{
fprintf(stderr,
- "cut: Error while parsing characters in file '%s': %s\n",
+ "%s: error: Failed parsing characters in file '%s': %s\n",
+ argv0,
filename,
strerror(errno));
err = 1;
@@ -275,7 +279,8 @@ cut_f(FILE *in, const char *filename)
{
if(errno != 0)
{
- fprintf(stderr, "cut: Error while reading file '%s': %s\n", filename, strerror(errno));
+ fprintf(
+ stderr, "%s: error: Failed reading file '%s': %s\n", argv0, filename, strerror(errno));
err = 1;
}
break;
@@ -343,7 +348,7 @@ cut(FILE *in, const char *filename)
switch(mode)
{
case CUT_MODE_NONE:
- fprintf(stderr, "cut: No action (-b, -c, -f) specified\n");
+ fprintf(stderr, "%s: error: No action (-b, -c, -f) specified\n", argv0);
return 1;
case CUT_MODE_B:
return cut_b(in, filename);
@@ -365,7 +370,7 @@ main(int argc, char *argv[])
setlocale(LC_ALL, "");
if(errno != 0)
{
- fprintf(stderr, "cut: Warning: Failed to initialize locales: %s\n", strerror(errno));
+ fprintf(stderr, "%s: warning: Failed to initialize locales: %s\n", argv0, strerror(errno));
errno = 0;
}
@@ -377,7 +382,7 @@ main(int argc, char *argv[])
case 'b':
if(opt_list != NULL)
{
- fprintf(stderr, "cut: Only one list may be specified\n");
+ fprintf(stderr, "%s: error: Only one list may be specified\n", argv0);
return 1;
}
mode = CUT_MODE_B;
@@ -386,7 +391,7 @@ main(int argc, char *argv[])
case 'c':
if(opt_list != NULL)
{
- fprintf(stderr, "cut: Only one list may be specified\n");
+ fprintf(stderr, "%s: error: Only one list may be specified\n", argv0);
return 1;
}
mode = CUT_MODE_C;
@@ -395,7 +400,7 @@ main(int argc, char *argv[])
case 'f':
if(opt_list != NULL)
{
- fprintf(stderr, "cut: Only one list may be specified\n");
+ fprintf(stderr, "%s: error: Only one list may be specified\n", argv0);
return 1;
}
mode = CUT_MODE_F;
@@ -404,7 +409,10 @@ main(int argc, char *argv[])
case 'd':
if(optarg[0] != '\0' && optarg[1] != '\0')
{
- fprintf(stderr, "cut: Option '-d' only accepts single characters, got \"%s\"\n", optarg);
+ fprintf(stderr,
+ "%s: error: Option '-d' only accepts single characters, got \"%s\"\n",
+ argv0,
+ optarg);
return 1;
}
delim = optarg[0];
@@ -416,10 +424,10 @@ main(int argc, char *argv[])
opt_s = true;
break;
case ':':
- fprintf(stderr, "cut: Option '-%c' requires an operand\n", optopt);
+ fprintf(stderr, "%s: error: Option '-%c' requires an operand\n", argv0, optopt);
return 1;
default:
- fprintf(stderr, "cut: Unhandled option '-%c'\n", optopt);
+ fprintf(stderr, "%s: error: Unhandled option '-%c'\n", argv0, optopt);
return 1;
}
}
@@ -429,7 +437,7 @@ main(int argc, char *argv[])
if(mode == CUT_MODE_NONE)
{
- fprintf(stderr, "cut: No action (-b, -c, -f) specified\n");
+ fprintf(stderr, "%s: error: No action (-b, -c, -f) specified\n", argv0);
return 1;
}
@@ -451,7 +459,7 @@ main(int argc, char *argv[])
FILE *in = fopen(argv[i], "r");
if(in == NULL)
{
- fprintf(stderr, "cut: Failed opening file '%s': %s\n", argv[i], strerror(errno));
+ fprintf(stderr, "%s: error: Failed opening file '%s': %s\n", argv0, argv[i], strerror(errno));
return 1;
}
@@ -459,7 +467,7 @@ main(int argc, char *argv[])
if(fclose(in) < 0)
{
- fprintf(stderr, "cut: Failed closing file '%s': %s\n", argv[i], strerror(errno));
+ fprintf(stderr, "%s: error: Failed closing file '%s': %s\n", argv0, argv[i], strerror(errno));
return 1;
}
@@ -468,13 +476,13 @@ main(int argc, char *argv[])
if(fclose(stdin) != 0)
{
- fprintf(stderr, "cut: Error closing <stdin>: %s\n", strerror(errno));
+ fprintf(stderr, "%s: error: Failed closing <stdin>: %s\n", argv0, strerror(errno));
return 1;
}
if(fclose(stdout) != 0)
{
- fprintf(stderr, "cut: Error closing <stdout>: %s\n", strerror(errno));
+ fprintf(stderr, "%s: error: Failed closing <stdout>: %s\n", argv0, strerror(errno));
return 1;
}
diff --git a/test-cmd/cut.sh b/test-cmd/cut.sh
@@ -31,7 +31,7 @@ t 'bytes:11-' "-b 11- ${WD}/test-cmd/inputs/alnum" 'ABCDEFGHIJKLMNOPQRSTUVWXYZab
'
t 'bytes:-10' "-b -10 ${WD}/test-cmd/inputs/alnum" '0123456789
'
-t --exit=1 'bytes:,' "-b , ${WD}/test-cmd/inputs/alnum" 'cut: Error: empty list element
+t --exit=1 'bytes:,' "-b , ${WD}/test-cmd/inputs/alnum" 'cut: error: Empty list element
'
# Example taken from POSIX cut(1)
t --input='abcdefghi' 'chars:6,2,4-7,1' '-c 6,2,4-7,1' 'abdefg