]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
cal: honor num_months when only a year argument
authorJ William Piggott <elseifthen@gmx.com>
Mon, 22 Jan 2018 20:05:08 +0000 (15:05 -0500)
committerKarel Zak <kzak@redhat.com>
Wed, 24 Jan 2018 12:24:33 +0000 (13:24 +0100)
I don't know if this was an oversight or an overzealous
interpretation of POSIX. Just in case, I'll address the
POSIX possibility. POSIX description for cal(1) says:

  If only the year operand is given, cal shall produce a
  calendar for all twelve months in the given calendar year.

It also says that cal(1) has no options, so in that context
if an option is given then it should be expected to override
POSIX behavior.

Before patched all of these command displayed a full year:
cal -1 2020
cal -3 2020
cal -n6 2020

Patched the number of months options are honored.

This patch also fixes the -1 option which was a no-op.

Signed-off-by: J William Piggott <elseifthen@gmx.com>
misc-utils/cal.c

index 438e7f09b939f68cccef4fc36e5964a02083a5f4..8d681f0cabf374fe97750e61a509f380135e9edb 100644 (file)
@@ -267,7 +267,6 @@ int main(int argc, char **argv)
        static struct cal_control ctl = {
                .reform_year = DEFAULT_REFORM_YEAR,
                .weekstart = SUNDAY,
-               .num_months = 1,                /* default is "cal -1" */
                .span_months = 0,
                .colormode = UL_COLORMODE_UNDEF,
                .weektype = WEEK_NUM_DISABLED,
@@ -363,7 +362,7 @@ int main(int argc, char **argv)
 
                switch(ch) {
                case '1':
-                       /* default */
+                       ctl.num_months = 1;
                        break;
                case '3':
                        ctl.num_months = 3;
@@ -536,7 +535,8 @@ int main(int argc, char **argv)
 
        if (yflag || Yflag) {
                ctl.gutter_width = 3;
-               ctl.num_months = MONTHS_IN_YEAR;
+               if (!ctl.num_months)
+                       ctl.num_months = MONTHS_IN_YEAR;
                if (yflag) {
                        ctl.req.start_month = 1;        /* start from Jan */
                        ctl.header_year = 1;            /* print year number */
@@ -549,6 +549,9 @@ int main(int argc, char **argv)
        else if (!ctl.months_in_row)
                ctl.months_in_row = 1;
 
+       if (!ctl.num_months)
+               ctl.num_months = 1;             /* display at least one month */
+
        if (yflag || Yflag)
                yearly(&ctl);
        else