commit: 2c92c7256ec7d7ca0b31c118588f4d7a2272293f
parent 9f7d2a185929826faf9a8c9df3d5af15ce971572
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 20 Sep 2024 04:13:01 +0200
cmd/split: unify error message formatting
Diffstat:
M | cmd/split.c | 86 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------- |
1 file changed, 61 insertions(+), 25 deletions(-)
diff --git a/cmd/split.c b/cmd/split.c
@@ -39,7 +39,11 @@ base26(int id, char *str)
if(id_p <= name_len)
{
- fprintf(stderr, "split: Failed representing %d into suffix of length %zu\n", id, suffix_len);
+ fprintf(stderr,
+ "%s: error: Failed representing %d into suffix of length %zu\n",
+ argv0,
+ id,
+ suffix_len);
return -1;
}
@@ -55,7 +59,7 @@ split_bytes(void)
fd_in = open(name_in, O_RDONLY | O_NOCTTY);
if(fd_in < 0)
{
- fprintf(stderr, "split: Failed opening '%s' file: %s\n", name_in, strerror(errno));
+ fprintf(stderr, "%s: error: Failed opening '%s' file: %s\n", argv0, name_in, strerror(errno));
return 1;
}
}
@@ -63,7 +67,11 @@ split_bytes(void)
struct stat fd_in_stat;
if(fstat(fd_in, &fd_in_stat) != 0)
{
- fprintf(stderr, "split: Failed getting status from file '%s': %s", name_in, strerror(errno));
+ fprintf(stderr,
+ "%s: error: Failed getting status from file '%s': %s",
+ argv0,
+ name_in,
+ strerror(errno));
close(fd_in);
return 1;
}
@@ -79,7 +87,8 @@ split_bytes(void)
int fd_out = open(name_out, O_WRONLY | O_NOCTTY | O_CREAT, 0644);
if(fd_out < 0)
{
- fprintf(stderr, "split: Failed opening '%s' file: %s\n", name_out, strerror(errno));
+ fprintf(
+ stderr, "%s: error: Failed opening '%s' file: %s\n", argv0, name_out, strerror(errno));
err = 1;
break;
}
@@ -88,7 +97,8 @@ split_bytes(void)
if(ret < 0)
{
fprintf(stderr,
- "split: Error while copying from file '%s' to file '%s': %s\n",
+ "%s: error: Failed copying from file '%s' to file '%s': %s\n",
+ argv0,
name_in ? name_in : "<stdin>",
name_out,
strerror(errno));
@@ -101,7 +111,8 @@ split_bytes(void)
if(close(fd_out) < 0)
{
- fprintf(stderr, "split: Failing closing file '%s': %s\n", name_out, strerror(errno));
+ fprintf(
+ stderr, "%s: error: Failed closing file '%s': %s\n", argv0, name_out, strerror(errno));
err = 1;
break;
}
@@ -121,7 +132,7 @@ split_lines(void)
in = fopen(name_in, "r");
if(in == NULL)
{
- fprintf(stderr, "split: Failed opening '%s' file: %s\n", name_in, strerror(errno));
+ fprintf(stderr, "%s: error: Failed opening '%s' file: %s\n", argv0, name_in, strerror(errno));
return 1;
}
}
@@ -135,7 +146,11 @@ split_lines(void)
if(feof(in)) break;
if(ferror(in))
{
- fprintf(stderr, "split: Failed reading line from file '%s': %s\n", name_in, strerror(errno));
+ fprintf(stderr,
+ "%s: error: Failed reading line from file '%s': %s\n",
+ argv0,
+ name_in,
+ strerror(errno));
err = 1;
break;
}
@@ -155,8 +170,11 @@ split_lines(void)
{
if(errno != 0)
{
- fprintf(
- stderr, "split: Failed reading line from file '%s': %s\n", name_in, strerror(errno));
+ fprintf(stderr,
+ "%s: error: Failed reading line from file '%s': %s\n",
+ argv0,
+ name_in,
+ strerror(errno));
err = 1;
}
break;
@@ -167,7 +185,11 @@ split_lines(void)
out = fopen(name_out, "w");
if(out == NULL)
{
- fprintf(stderr, "split: Failed opening '%s' file: %s\n", name_out, strerror(errno));
+ fprintf(stderr,
+ "%s: error: Failed opening '%s' file: %s\n",
+ argv0,
+ name_out,
+ strerror(errno));
err = 1;
break;
}
@@ -175,7 +197,11 @@ split_lines(void)
if(fwrite(line, nread, 1, out) < 0)
{
- fprintf(stderr, "split: Failed writing line to file '%s': %s\n", name_out, strerror(errno));
+ fprintf(stderr,
+ "%s: error: Failed writing line to file '%s': %s\n",
+ argv0,
+ name_out,
+ strerror(errno));
err = 1;
break;
}
@@ -185,7 +211,8 @@ split_lines(void)
{
if(fclose(out) < 0)
{
- fprintf(stderr, "split: Failing closing file '%s': %s\n", name_out, strerror(errno));
+ fprintf(
+ stderr, "%s: error: Failed closing file '%s': %s\n", argv0, name_out, strerror(errno));
err = 1;
break;
}
@@ -201,7 +228,7 @@ split_lines(void)
return err;
}
-static const char *error_opt_b_l = "split: Options -b and -l are mutually exclusive\n";
+static const char *error_opt_b_l = "%s: error: Options -b and -l are mutually exclusive\n";
int
main(int argc, char *argv[])
@@ -217,12 +244,16 @@ main(int argc, char *argv[])
suffix_len = strtoul(optarg, &endptr, 0);
if(suffix_len == 0)
{
- fprintf(stderr, "split: Error while parsing '-a %s': %s\n", optarg, strerror(errno));
+ fprintf(stderr, "%s: error: Failed parsing '-a %s': %s\n", argv0, optarg, strerror(errno));
return 1;
}
if(endptr != NULL && *endptr != '\0')
{
- fprintf(stderr, "split: Invalid trailing characters in '-a %s': %s\n", optarg, endptr);
+ fprintf(stderr,
+ "%s: error: Invalid trailing characters in '-a %s': %s\n",
+ argv0,
+ optarg,
+ endptr);
return 1;
}
break;
@@ -230,14 +261,14 @@ main(int argc, char *argv[])
{
if(lines != 0)
{
- fputs(error_opt_b_l, stderr);
+ fprintf(stderr, error_opt_b_l, argv0);
return 1;
}
unsigned long opt_b = strtoul(optarg, &endptr, 0);
if(opt_b == 0)
{
- fprintf(stderr, "split: Error while parsing '-b %s': %s\n", optarg, strerror(errno));
+ fprintf(stderr, "%s: error: Failed parsing '-b %s': %s\n", argv0, optarg, strerror(errno));
return 1;
}
@@ -251,27 +282,31 @@ main(int argc, char *argv[])
case 'l':
if(bytes != 0)
{
- fputs(error_opt_b_l, stderr);
+ fprintf(stderr, error_opt_b_l, argv0);
return 1;
}
lines = strtoul(optarg, &endptr, 0);
if(lines == 0)
{
- fprintf(stderr, "split: Error while parsing '-l %s': %s\n", optarg, strerror(errno));
+ fprintf(stderr, "%s: error: Failed parsing '-l %s': %s\n", argv0, optarg, strerror(errno));
return 1;
}
if(endptr != NULL && *endptr != '\0')
{
- fprintf(stderr, "split: Invalid trailing characters in '-l %s': %s\n", optarg, endptr);
+ fprintf(stderr,
+ "%s: error: Invalid trailing characters in '-l %s': %s\n",
+ argv0,
+ optarg,
+ endptr);
return 1;
}
break;
case ':':
- fprintf(stderr, "split: Option '-%c' requires an operand\n", optopt);
+ fprintf(stderr, "%s: error: Option '-%c' requires an operand\n", argv0, optopt);
return 1;
default:
- fprintf(stderr, "split: Unhandled option '-%c'\n", optopt);
+ fprintf(stderr, "%s: error: Unhandled option '-%c'\n", argv0, optopt);
return 1;
}
}
@@ -283,7 +318,7 @@ main(int argc, char *argv[])
if(argc > 2 || argc < 0)
{
- fprintf(stderr, "split: Expected 0, 1, or 2 arguments, got %d\n", argc);
+ fprintf(stderr, "%s: error: Expected 0, 1, or 2 arguments, got %d\n", argv0, argc);
return 1;
}
else if(argc >= 1)
@@ -297,7 +332,8 @@ main(int argc, char *argv[])
if(name_len + suffix_len > NAME_MAX)
{
fprintf(stderr,
- "split: Error: name(%zd bytes) + suffix_length(%zd bytes) > NAME_MAX(%d bytes)\n",
+ "%s: error: name(%zd bytes) + suffix_length(%zd bytes) > NAME_MAX(%d bytes)\n",
+ argv0,
name_len,
suffix_len,
NAME_MAX);