commit: e11265ba5e52324b47fba5a2b1dcea8df50969f2
parent 320e446f1ef258dc7295ac149ee56c0845978ba7
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 20 Sep 2024 02:40:46 +0200
cmd/head: unify error message formatting
Diffstat:
M | cmd/head.c | 61 | +++++++++++++++++++++++++++++++++++++++++++------------------ |
1 file changed, 43 insertions(+), 18 deletions(-)
diff --git a/cmd/head.c b/cmd/head.c
@@ -39,7 +39,7 @@ copy_bytes(const char *filename)
if(buf == NULL)
{
- fprintf(stderr, "head: Failed to allocate buffer: %s\n", strerror(errno));
+ fprintf(stderr, "%s: error: Failed to allocate buffer: %s\n", argv0, strerror(errno));
return 1;
}
@@ -56,7 +56,8 @@ copy_bytes(const char *filename)
fd = open(filename, O_RDONLY | O_CLOEXEC | O_NOCTTY);
if(fd < 0)
{
- fprintf(stderr, "head: Failed opening file '%s': %s\n", filename, strerror(errno));
+ fprintf(
+ stderr, "%s: error: Failed opening file '%s': %s\n", argv0, filename, strerror(errno));
return 1;
}
}
@@ -68,7 +69,8 @@ copy_bytes(const char *filename)
ssize_t nread = read(fd, buf, MIN(buflen, bytes - i));
if(nread < 0)
{
- fprintf(stderr, "head: Failed 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;
}
@@ -77,8 +79,11 @@ copy_bytes(const char *filename)
if(write(STDOUT_FILENO, buf, nread) < 0)
{
- fprintf(
- stderr, "head: Failed writing line from '%s' to stdout: %s\n", filename, strerror(errno));
+ fprintf(stderr,
+ "%s: error: Failed writing line from '%s' to stdout: %s\n",
+ argv0,
+ filename,
+ strerror(errno));
err = 1;
break;
}
@@ -89,7 +94,8 @@ copy_bytes(const char *filename)
if(fd != STDIN_FILENO)
if(close(fd) != 0)
{
- fprintf(stderr, "head: Failed closing file '%s': %s\n", filename, strerror(errno));
+ fprintf(
+ stderr, "%s: error: Failed closing file '%s': %s\n", argv0, filename, strerror(errno));
return 1;
}
@@ -111,7 +117,8 @@ copy_lines(const char *filename)
in = fopen(filename, "r");
if(in == NULL)
{
- fprintf(stderr, "head: Failed opening file '%s': %s\n", filename, strerror(errno));
+ fprintf(
+ stderr, "%s: error: Failed opening file '%s': %s\n", argv0, filename, strerror(errno));
return 1;
}
}
@@ -124,15 +131,22 @@ copy_lines(const char *filename)
{
if(errno == 0) break;
- fprintf(stderr, "head: Failed reading line from '%s': %s\n", filename, strerror(errno));
+ fprintf(stderr,
+ "%s: error: Failed reading line from '%s': %s\n",
+ argv0,
+ filename,
+ strerror(errno));
err = 1;
break;
}
if(write(STDOUT_FILENO, buf, nread) < 0)
{
- fprintf(
- stderr, "head: Failed writing line from '%s' to stdout: %s\n", filename, strerror(errno));
+ fprintf(stderr,
+ "%s: error: Failed writing line from '%s' to stdout: %s\n",
+ argv0,
+ filename,
+ strerror(errno));
err = 1;
break;
}
@@ -141,7 +155,8 @@ copy_lines(const char *filename)
if(in != stdin)
if(fclose(in) != 0)
{
- fprintf(stderr, "head: Failed closing file '%s': %s\n", filename, strerror(errno));
+ fprintf(
+ stderr, "%s: error: Failed closing file '%s': %s\n", argv0, filename, strerror(errno));
return 1;
}
@@ -177,7 +192,11 @@ main(int argc, char *argv[])
lines = strtoul(arg, &endptr, 0);
if(!(errno == 0 && endptr != NULL && *endptr == '\0'))
{
- fprintf(stderr, "head: Error while parsing number for `-%s`: %s\n", arg, strerror(errno));
+ fprintf(stderr,
+ "%s: error: Failed parsing number for `-%s`: %s\n",
+ argv0,
+ arg,
+ strerror(errno));
return 1;
}
@@ -203,8 +222,11 @@ main(int argc, char *argv[])
unsigned long size = strtoul(optarg, &endptr, 0);
if(errno != 0)
{
- fprintf(
- stderr, "head: Error while parsing number for `-n %s`: %s\n", optarg, strerror(errno));
+ fprintf(stderr,
+ "%s: error: Failed parsing number for `-n %s`: %s\n",
+ argv0,
+ optarg,
+ strerror(errno));
return 1;
}
@@ -222,8 +244,11 @@ main(int argc, char *argv[])
lines = strtoul(optarg, &endptr, 0);
if(!(errno == 0 && endptr != NULL && *endptr == '\0'))
{
- fprintf(
- stderr, "head: Error while parsing number for `-n %s`: %s\n", optarg, strerror(errno));
+ fprintf(stderr,
+ "%s: error: Failed parsing number for `-n %s`: %s\n",
+ argv0,
+ optarg,
+ strerror(errno));
return 1;
}
break;
@@ -232,11 +257,11 @@ main(int argc, char *argv[])
delim = '\0';
break;
case ':':
- fprintf(stderr, "head: 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, "head: Error: Unrecognised option: '-%c'\n", optopt);
+ fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt);
usage();
return 1;
default: