summaryrefslogtreecommitdiffstats
path: root/misc-utils/cal.c
diff options
context:
space:
mode:
authorKarel Zak2018-08-07 10:52:33 +0200
committerKarel Zak2018-08-07 10:52:33 +0200
commit2395f93c8ca0ba6ff04c0099d5d428c50f66d643 (patch)
tree20dc1dac6f99e2b65381c045a10165b379b03e18 /misc-utils/cal.c
parentcal: use snprintf everywhere (diff)
downloadkernel-qcow2-util-linux-2395f93c8ca0ba6ff04c0099d5d428c50f66d643.tar.gz
kernel-qcow2-util-linux-2395f93c8ca0ba6ff04c0099d5d428c50f66d643.tar.xz
kernel-qcow2-util-linux-2395f93c8ca0ba6ff04c0099d5d428c50f66d643.zip
cal: fix --span for large numbers of months
The need to calculate with whole years when go back for --span. Addresses: https://github.com/karelzak/util-linux/issues/677 Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'misc-utils/cal.c')
-rw-r--r--misc-utils/cal.c15
1 files changed, 9 insertions, 6 deletions
diff --git a/misc-utils/cal.c b/misc-utils/cal.c
index acbcf5005..4dff491fb 100644
--- a/misc-utils/cal.c
+++ b/misc-utils/cal.c
@@ -840,17 +840,20 @@ static void cal_output_months(struct cal_month *month, const struct cal_control
static void monthly(const struct cal_control *ctl)
{
struct cal_month m1,m2,m3, *m;
- int i, rows, new_month, month = ctl->req.start_month ? ctl->req.start_month : ctl->req.month;
+ int i, rows, month = ctl->req.start_month ? ctl->req.start_month : ctl->req.month;
int32_t year = ctl->req.year;
/* cal -3, cal -Y --span, etc. */
if (ctl->span_months) {
- new_month = month - ctl->num_months / 2;
+ int new_month = month - ctl->num_months / 2;
if (new_month < 1) {
- month = new_month + MONTHS_IN_YEAR;
- year--;
- }
- else
+ new_month *= -1;
+ year -= (new_month / MONTHS_IN_YEAR) + 1;
+
+ if (new_month > MONTHS_IN_YEAR)
+ new_month %= MONTHS_IN_YEAR;
+ month = MONTHS_IN_YEAR - new_month;
+ } else
month = new_month;
}