]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - misc-utils/cal.c
docs: update year in libs docs
[thirdparty/util-linux.git] / misc-utils / cal.c
index acbcf5005e8b56ef1087842ae0669159305eecd3..ca8e42e504626e9203afcb7cbe64f62dbf9b152c 100644 (file)
@@ -332,7 +332,7 @@ int main(int argc, char **argv)
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
-       atexit(close_stdout);
+       close_stdout_atexit();
 
        term = getenv("TERM");
        if (term) {
@@ -435,8 +435,7 @@ int main(int argc, char **argv)
                        ctl.reform_year = ISO;
                        break;
                case 'V':
-                       printf(UTIL_LINUX_VERSION);
-                       return EXIT_SUCCESS;
+                       print_version(EXIT_SUCCESS);
                case 'h':
                        usage();
                default:
@@ -550,9 +549,21 @@ int main(int argc, char **argv)
 
        headers_init(&ctl);
 
-       if (!colors_init(ctl.colormode, "cal")) {
-               ctl.req.day = 0;
-               ctl.weektype &= ~WEEK_NUM_MASK;
+       if (colors_init(ctl.colormode, "cal") == 0) {
+               /*
+                * If standout mode available (Senter and Sexit are set) and
+                * user or terminal-colors.d do not disable colors than
+                * ignore colors_init().
+                */
+               if (*Senter && *Sexit && colors_mode() != UL_COLORMODE_NEVER) {
+                       /* let use standout mode */
+                       ;
+               } else {
+                       /* disable */
+                       Senter = ""; Sexit = "";
+                       ctl.req.day = 0;
+                       ctl.weektype &= ~WEEK_NUM_MASK;
+               }
        }
 
        if (yflag || Yflag) {
@@ -569,13 +580,19 @@ int main(int argc, char **argv)
                ctl.months_in_row = MONTHS_IN_YEAR_ROW;         /* default */
 
                if (isatty(STDOUT_FILENO)) {
-                       int w = get_terminal_width(STDOUT_FILENO);
-                       int mw = ctl.julian ? DOY_MONTH_WIDTH : DOM_MONTH_WIDTH;
-                       int extra = ((w / mw) - 1) * ctl.gutter_width;
-                       int new_n = (w - extra) / mw;
+                       int w, mw, extra, new_n;
+
+                       w = get_terminal_width(80);
+                       mw = ctl.julian ? DOY_MONTH_WIDTH : DOM_MONTH_WIDTH;
+
+                       if (w < mw)
+                               w = mw;
+
+                       extra = ((w / mw) - 1) * ctl.gutter_width;
+                       new_n = (w - extra) / mw;
 
                        if (new_n < MONTHS_IN_YEAR_ROW)
-                               ctl.months_in_row = new_n;
+                               ctl.months_in_row = new_n > 0 ? new_n : 1;
                }
        } else if (!ctl.months_in_row)
                ctl.months_in_row = 1;
@@ -840,17 +857,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;
        }
 
@@ -1113,7 +1133,8 @@ static void __attribute__((__noreturn__)) usage(void)
        fputs(_(" -y, --year            show the whole year\n"), out);
        fputs(_(" -Y, --twelve          show the next twelve months\n"), out);
        fputs(_(" -w, --week[=<num>]    show US or ISO-8601 week numbers\n"), out);
-       fputs(_("     --color[=<when>]  colorize messages (auto, always or never)\n"), out);
+       fprintf(out,
+             _("     --color[=<when>]  colorize messages (%s, %s or %s)\n"), "auto", "always", "never");
        fprintf(out,
                "                         %s\n", USAGE_COLORS_DEFAULT);