summaryrefslogtreecommitdiffstats
path: root/misc-utils/cal.c
diff options
context:
space:
mode:
authorKarel Zak2018-02-02 10:05:15 +0100
committerKarel Zak2018-02-02 10:05:15 +0100
commitb9d9ab7dae14815cc5f40aae0170cd8ed7d342eb (patch)
treea5ba6880a65b62537dcf2e2323c72e18a0e4a912 /misc-utils/cal.c
parenttests: update setarch test (diff)
downloadkernel-qcow2-util-linux-b9d9ab7dae14815cc5f40aae0170cd8ed7d342eb.tar.gz
kernel-qcow2-util-linux-b9d9ab7dae14815cc5f40aae0170cd8ed7d342eb.tar.xz
kernel-qcow2-util-linux-b9d9ab7dae14815cc5f40aae0170cd8ed7d342eb.zip
cal: add ifdef TEST_CAL
The test program follows CAL_TEST_TIME=<sec> rather than libc time(). It allows to use cal(1) in regression tests in cases where output depends on the current time. (We already use the same for example for logger.) Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'misc-utils/cal.c')
-rw-r--r--misc-utils/cal.c22
1 files changed, 20 insertions, 2 deletions
diff --git a/misc-utils/cal.c b/misc-utils/cal.c
index a110b1b8f..266f77bdc 100644
--- a/misc-utils/cal.c
+++ b/misc-utils/cal.c
@@ -261,6 +261,24 @@ static void center(const char *str, size_t len, int separate);
static int parse_reform_year(const char *reform_year);
static void __attribute__((__noreturn__)) usage(void);
+#ifdef TEST_CAL
+static time_t cal_time(time_t *t)
+{
+ char *str = getenv("CAL_TEST_TIME");
+
+ if (str) {
+ uint64_t x = strtou64_or_err(str, "failed to parse CAL_TEST_TIME");
+
+ *t = x;
+ return *t;
+ }
+
+ return time(t);
+}
+#else
+# define cal_time(t) time(t)
+#endif
+
int main(int argc, char **argv)
{
struct tm *local_time;
@@ -443,12 +461,12 @@ int main(int argc, char **argv)
now = (time_t) (x / 1000000);
/* cal <monthname> */
else if ((ctl.req.month = monthname_to_number(&ctl, *argv)) > 0)
- time(&now); /* this year */
+ cal_time(&now); /* this year */
else
errx(EXIT_FAILURE, _("failed to parse timestamp or unknown month name: %s"), *argv);
argc = 0;
} else
- time(&now);
+ cal_time(&now);
local_time = localtime(&now);