commit: 77351e346c00719d9f94cf5ab011aaf1c31ff0c6
parent 2f3808524d6309c5b48e9df77b75815265e24f57
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Thu, 19 Sep 2024 18:10:25 +0200
cmd/base64: unify error message formatting
Diffstat:
2 files changed, 32 insertions(+), 22 deletions(-)
diff --git a/cmd/base64.c b/cmd/base64.c
@@ -18,6 +18,7 @@ static const char *b64_encmap = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstu
static size_t c_out = 0;
// 76 is lowest of all base64 related RFCs
static long wrap_nl = 76;
+const char *argv0 = "base64";
static int
xputc(int c, FILE *stream)
@@ -25,7 +26,7 @@ xputc(int c, FILE *stream)
assert(errno == 0);
if(fputc(c, stream) == EOF)
{
- fprintf(stderr, "base64: Error writing: %s\n", strerror(errno));
+ fprintf(stderr, "%s: error: Failed writing: %s\n", argv0, strerror(errno));
errno = 0;
return 1;
}
@@ -34,7 +35,7 @@ xputc(int c, FILE *stream)
int err = ferror(stream);
if(err != 0)
{
- fprintf(stderr, "base64: Error writing: %s\n", strerror(err));
+ fprintf(stderr, "%s: error: Failed writing: %s\n", argv0, strerror(err));
errno = 0;
return 1;
}
@@ -84,7 +85,8 @@ encode(FILE *fin, const char *name)
assert(errno == 0);
if(ferror(fin))
{
- fprintf(stderr, "base64: Error reading ‘%s’: %s\n", name, strerror(errno));
+ fprintf(
+ stderr, "%s: error: Failed reading from file '%s': %s\n", argv0, name, strerror(errno));
errno = 0;
return 1;
}
@@ -104,7 +106,7 @@ encode(FILE *fin, const char *name)
if(fwrite((char *)obuf, 4, 1, stdout) <= 0)
{
- fprintf(stderr, "base64: Error writing: %s\n", strerror(errno));
+ fprintf(stderr, "%s: error: Failed writing: %s\n", argv0, strerror(errno));
errno = 0;
return 1;
}
@@ -122,14 +124,14 @@ encode(FILE *fin, const char *name)
assert(errno == 0);
if(fflush(stdout))
{
- fprintf(stderr, "base64: Error writing: %s\n", strerror(errno));
+ fprintf(stderr, "%s: error: Failed writing: %s\n", argv0, strerror(errno));
errno = 0;
return 1;
}
int err = ferror(stdout);
if(err != 0)
{
- fprintf(stderr, "base64: Error writing: %s\n", strerror(err));
+ fprintf(stderr, "%s: error: Failed writing: %s\n", argv0, strerror(errno));
return 1;
}
@@ -169,7 +171,7 @@ decode(FILE *fin, const char *name)
}
if(b > 64)
{
- fprintf(stderr, "base64: Invalid character %c\n", c);
+ fprintf(stderr, "%s: error: Invalid character '%c'\n", argv0, c);
return 1;
}
@@ -209,7 +211,7 @@ decode(FILE *fin, const char *name)
{
case 0:
case 1:
- fprintf(stderr, "base64: Invalid character %c (early '=')\n", c);
+ fprintf(stderr, "%s: error: Invalid character '%c' (early '=')\n", argv0, c);
return 1;
case 2:
while(isspace(c = getc(fin)))
@@ -217,7 +219,7 @@ decode(FILE *fin, const char *name)
if(c != '=')
{
- fprintf(stderr, "base64: Invalid character %c (not '=')\n", c);
+ fprintf(stderr, "%s: error: Invalid character '%c' (not '=')\n", argv0, c);
return 1;
}
/* fallthrough */
@@ -227,7 +229,7 @@ decode(FILE *fin, const char *name)
if(c != EOF)
{
- fprintf(stderr, "base64: Invalid character %c (not EOF)\n", c);
+ fprintf(stderr, "%s: error: Invalid character '%c' (not EOF)\n", argv0, c);
return 1;
}
return 0;
@@ -241,7 +243,7 @@ decode(FILE *fin, const char *name)
assert(errno == 0);
if(fflush(stdout))
{
- fprintf(stderr, "base64: Error writing: %s\n", strerror(errno));
+ fprintf(stderr, "%s: error: Failed writing: %s\n", argv0, strerror(errno));
errno = 0;
return 1;
}
@@ -249,7 +251,7 @@ decode(FILE *fin, const char *name)
int err = ferror(stdout);
if(err != 0)
{
- fprintf(stderr, "base64: Error writing: %s\n", strerror(err));
+ fprintf(stderr, "%s: error: Failed writing: %s\n", argv0, strerror(err));
errno = 0;
return 1;
}
@@ -281,15 +283,15 @@ main(int argc, char *argv[])
if(errno != 0)
{
- fprintf(stderr, "base64: Option `-w %s`: %s\n", optarg, strerror(errno));
+ fprintf(stderr, "%s: error: Option '-w %s': %s\n", argv0, optarg, strerror(errno));
return 1;
}
break;
case ':':
- fprintf(stderr, "base64: Error: Missing operand for option: ‘-%c’\n", optopt);
+ fprintf(stderr, "%s: error: Missing operand for option '-%c'\n", argv0, optopt);
return 1;
case '?':
- fprintf(stderr, "base64: Error: Unrecognised option: ‘-%c’\n", optopt);
+ fprintf(stderr, "%s: error: Unrecognised option '-%c'\n", argv0, optopt);
return 1;
}
}
@@ -322,7 +324,11 @@ main(int argc, char *argv[])
FILE *fin = fopen(argv[argi], "r");
if(fin == NULL || ferror(fin) != 0)
{
- fprintf(stderr, "base64: Error opening ‘%s’: %s\n", argv[argi], strerror(errno));
+ fprintf(stderr,
+ "%s: error: Failed opening file '%s': %s\n",
+ argv0,
+ argv[argi],
+ strerror(errno));
ret = 1;
goto end;
}
@@ -335,7 +341,11 @@ main(int argc, char *argv[])
if(fclose(fin) < 0)
{
- fprintf(stderr, "base64: Error closing ‘%s’: %s\n", argv[argi], strerror(errno));
+ fprintf(stderr,
+ "%s: error: Failed closing file '%s': %s\n",
+ argv0,
+ argv[argi],
+ strerror(errno));
ret = 1;
goto end;
}
@@ -350,13 +360,13 @@ end:
if(fclose(stdin) != 0)
{
- fprintf(stderr, "base64: Error closing <stdin>: %s\n", strerror(errno));
+ fprintf(stderr, "%s: error: Failed closing file <stdin>: %s\n", argv0, strerror(errno));
ret = 1;
}
if(fclose(stdout) != 0)
{
- fprintf(stderr, "base64: Error closing <stdout>: %s\n", strerror(errno));
+ fprintf(stderr, "%s: error: Failed closing file <stdout>: %s\n", argv0, strerror(errno));
ret = 1;
}
diff --git a/test-cmd/base64.sh b/test-cmd/base64.sh
@@ -46,7 +46,7 @@ then
else
touch "$WD/inputs/chmod_000"
chmod 0000 "$WD/inputs/chmod_000"
- t --exit=1 'noperm' "$WD/inputs/chmod_000" "base64: Error opening ‘$WD/inputs/chmod_000’: Permission denied
+ t --exit=1 'noperm' "$WD/inputs/chmod_000" "base64: error: Failed opening file '$WD/inputs/chmod_000': Permission denied
"
chmod 0600 "$WD/inputs/chmod_000"
rm "$WD/inputs/chmod_000"
@@ -66,5 +66,5 @@ fi
#t --exit=1 'readslash' '/' 'base64: Error reading ‘/’: Is a directory
#'
-t --exit=1 'enoent' '/var/empty/e/no/ent' 'base64: Error opening ‘/var/empty/e/no/ent’: No such file or directory
-'
+t --exit=1 'enoent' '/var/empty/e/no/ent' "base64: error: Failed opening file '/var/empty/e/no/ent': No such file or directory
+"