commit: 6462828c8a803e12e80be3706c577811fcca731c
parent d9da7cbaab068ecbb0149db9ed44b19d3810d840
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Thu, 19 Sep 2024 19:31:25 +0200
cmd/date: unify error message formatting
Diffstat:
M | cmd/date.c | 61 | +++++++++++++++++++++++++++++++++++-------------------------- |
1 file changed, 35 insertions(+), 26 deletions(-)
diff --git a/cmd/date.c b/cmd/date.c
@@ -12,12 +12,14 @@
#include <errno.h>
#include <locale.h> /* setlocale() */
#include <stdbool.h>
-#include <stdio.h> /* BUFSIZ, perror(), puts() */
+#include <stdio.h> /* BUFSIZ, fprintf(), puts() */
#include <stdlib.h> /* exit(), strtol() */
#include <string.h> /* strerror */
#include <time.h> /* time, localtime, tm, strftime, strptime, clock_settime */
#include <unistd.h> /* getopt(), optarg, optind */
+const char *argv0 = "date";
+
static void
usage(void)
{
@@ -60,14 +62,14 @@ main(int argc, char *argv[])
setlocale(LC_ALL, "");
if(errno != 0)
{
- fprintf(stderr, "date: Warning: Failed to initialize locales: %s\n", strerror(errno));
+ fprintf(stderr, "%s: warning: Failed to initialize locales: %s\n", argv0, strerror(errno));
errno = 0;
}
tp.tv_sec = time(NULL);
if(tp.tv_sec == (time_t)-1)
{
- perror("date: time");
+ fprintf(stderr, "%s: error: Failed getting current time: %s\n", argv0, strerror(errno));
exit(EXIT_FAILURE);
}
@@ -79,12 +81,12 @@ main(int argc, char *argv[])
case 'd': /* Custom datetime */
if(input_format != NULL)
{
- fprintf(stderr, "date: Cannot both use '-d' and '-f'\n");
+ fprintf(stderr, "%s: error: Cannot both use '-d' and '-f'\n", argv0);
exit(EXIT_FAILURE);
}
if(rflag == 1)
{
- fprintf(stderr, "date: Cannot both use '-d' and '-r'\n");
+ fprintf(stderr, "%s: error: Cannot both use '-d' and '-r'\n", argv0);
exit(EXIT_FAILURE);
}
@@ -92,7 +94,7 @@ main(int argc, char *argv[])
dflag = 1;
if(errstr != NULL)
{
- fprintf(stderr, "date: iso_parse(\"%s\", …): %s\n", optarg, errstr);
+ fprintf(stderr, "%s: error: iso_parse(\"%s\", …): %s\n", argv0, optarg, errstr);
exit(EXIT_FAILURE);
}
@@ -100,7 +102,7 @@ main(int argc, char *argv[])
tp.tv_sec = mktime_tz(&tm);
if(tp.tv_sec == (time_t)-1)
{
- fprintf(stderr, "date: mktime: %s\n", strerror(errno));
+ fprintf(stderr, "%s: error: mktime: %s\n", argv0, strerror(errno));
exit(EXIT_FAILURE);
}
errno = 0;
@@ -109,12 +111,12 @@ main(int argc, char *argv[])
case 'f': /* input datetime format */
if(dflag == 1)
{
- fprintf(stderr, "date: Cannot both use '-d' and '-f'\n");
+ fprintf(stderr, "%s: error: Cannot both use '-d' and '-f'\n", argv0);
exit(EXIT_FAILURE);
}
if(rflag == 1)
{
- fprintf(stderr, "date: Cannot both use '-f' and '-r'\n");
+ fprintf(stderr, "%s: error: Cannot both use '-f' and '-r'\n", argv0);
exit(EXIT_FAILURE);
}
@@ -125,12 +127,12 @@ main(int argc, char *argv[])
{
if(input_format != NULL)
{
- fprintf(stderr, "date: Cannot both use '-f' and '-r'\n");
+ fprintf(stderr, "%s: error: Cannot both use '-f' and '-r'\n", argv0);
exit(EXIT_FAILURE);
}
if(dflag == 1)
{
- fprintf(stderr, "date: Cannot both use '-d' and '-r'\n");
+ fprintf(stderr, "%s: error: Cannot both use '-d' and '-r'\n", argv0);
exit(EXIT_FAILURE);
}
@@ -139,12 +141,12 @@ main(int argc, char *argv[])
tp.tv_sec = strtol(optarg, &endptr, 10);
if(errno != 0)
{
- fprintf(stderr, "date: Failed parsing '-r %s'\n", optarg);
+ fprintf(stderr, "%s: error: Failed parsing '-r %s'\n", argv0, optarg);
exit(EXIT_FAILURE);
}
if(!(endptr == NULL || *endptr == '\0'))
{
- fprintf(stderr, "date: Error: Invalid characters in '-r %s': %s\n", optarg, endptr);
+ fprintf(stderr, "%s: error: Invalid characters in '-r %s': %s\n", argv0, optarg, endptr);
exit(EXIT_FAILURE);
}
break;
@@ -161,11 +163,11 @@ main(int argc, char *argv[])
jflag = true;
break;
case ':':
- fprintf(stderr, "date: 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, "date: Error: Unrecognised option: '-%c'\n", optopt);
+ fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt);
usage();
return 1;
}
@@ -178,7 +180,7 @@ main(int argc, char *argv[])
{
if(gmtime_r(&tp.tv_sec, &tm) == NULL)
{
- perror("date: gmtime_r");
+ fprintf(stderr, "%s: error: gmtime_r: %s\n", argv0, strerror(errno));
exit(EXIT_FAILURE);
}
}
@@ -186,7 +188,7 @@ main(int argc, char *argv[])
{
if(localtime_r(&tp.tv_sec, &tm) == NULL)
{
- perror("date: localtime_r");
+ fprintf(stderr, "%s: error: localtime_r: %s\n", argv0, strerror(errno));
exit(EXIT_FAILURE);
}
}
@@ -198,7 +200,8 @@ main(int argc, char *argv[])
if(res == NULL)
{
fprintf(stderr,
- "date: strptime(\"%s\", \"%s\", …) as passed by '-f' option failed\n",
+ "%s: error: strptime(\"%s\", \"%s\", …) as passed by '-f' option failed\n",
+ argv0,
argv[0],
input_format);
exit(EXIT_FAILURE);
@@ -209,7 +212,7 @@ main(int argc, char *argv[])
tp.tv_nsec = 0;
if(tp.tv_sec == (time_t)-1)
{
- fprintf(stderr, "date: mktime: %s\n", strerror(errno));
+ fprintf(stderr, "%s: error: mktime: %s\n", argv0, strerror(errno));
exit(EXIT_FAILURE);
}
errno = 0;
@@ -224,7 +227,7 @@ main(int argc, char *argv[])
char *s = strptime(argv[0], fmt, &tm);
if(s == NULL)
{
- fprintf(stderr, "date: strptime(\"%s\", \"%s\", …) returned NULL\n", *argv, fmt);
+ fprintf(stderr, "%s: error: strptime(\"%s\", \"%s\", …) returned NULL\n", argv0, *argv, fmt);
exit(EXIT_FAILURE);
}
@@ -240,14 +243,19 @@ main(int argc, char *argv[])
fmt = "%Y";
break;
default:
- fprintf(stderr, "date: Got %zu trailing characters after \"%s\" for mmddHHMM\n", rem, fmt);
+ fprintf(stderr,
+ "%s: error: Got %zu trailing characters after \"%s\" for mmddHHMM\n",
+ argv0,
+ rem,
+ fmt);
exit(EXIT_FAILURE);
}
s = strptime(s, fmt, &tm);
if(s == NULL)
{
- fprintf(stderr, "date: strptime(\"%s\", \"%s\", …) returned NULL\n", *argv, fmt);
+ fprintf(
+ stderr, "%s: error: strptime(\"%s\", \"%s\", …) returned NULL\n", argv0, *argv, fmt);
exit(EXIT_FAILURE);
}
}
@@ -257,7 +265,7 @@ main(int argc, char *argv[])
tp.tv_nsec = 0;
if(tp.tv_sec == (time_t)-1)
{
- fprintf(stderr, "date: mktime: %s\n", strerror(errno));
+ fprintf(stderr, "%s: error: mktime: %s\n", argv0, strerror(errno));
exit(EXIT_FAILURE);
}
errno = 0;
@@ -272,7 +280,8 @@ main(int argc, char *argv[])
if(clock_settime(CLOCK_REALTIME, &tp) != 0)
{
fprintf(stderr,
- "date: Error clock_settime(CLOCK_REALTIME, {%ld, %ld}): %s\n",
+ "%s: error: clock_settime(CLOCK_REALTIME, {%ld, %ld}): %s\n",
+ argv0,
tp.tv_sec,
tp.tv_nsec,
strerror(errno));
@@ -291,13 +300,13 @@ main(int argc, char *argv[])
errno = 0;
if(strftime(outstr, sizeof(outstr), format, &tm) == 0 && errno != 0)
{
- perror("date: strftime");
+ fprintf(stderr, "%s: error: Failed formatting time: %s\n", argv0, strerror(errno));
exit(EXIT_FAILURE);
}
if(puts(outstr) < 0)
{
- perror("date: puts");
+ fprintf(stderr, "%s: error: Failed writing time: %s\n", argv0, strerror(errno));
exit(EXIT_FAILURE);
}