BSD 4_3_Net_2 release
[unix-history] / usr / src / usr.bin / cal / cal.c
index 3219577..6bc6a7b 100644 (file)
@@ -5,7 +5,33 @@
  * This code is derived from software contributed to Berkeley by
  * Kim Letkeman.
  *
  * This code is derived from software contributed to Berkeley by
  * Kim Letkeman.
  *
- * %sccs.include.redist.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
@@ -15,7 +41,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)cal.c      4.8 (Berkeley) %G%";
+static char sccsid[] = "@(#)cal.c      5.2 (Berkeley) 4/19/91";
 #endif /* not lint */
 
 #include <sys/types.h>
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -25,8 +51,11 @@ static char sccsid[] = "@(#)cal.c    4.8 (Berkeley) %G%";
 
 #define        THURSDAY                4               /* for reformation */
 #define        SATURDAY                6               /* 1 Jan 1 was a Saturday */
 
 #define        THURSDAY                4               /* for reformation */
 #define        SATURDAY                6               /* 1 Jan 1 was a Saturday */
+
 #define        FIRST_MISSING_DAY       639787          /* 3 Sep 1752 */
 #define        NUMBER_MISSING_DAYS     11              /* 11 day correction */
 #define        FIRST_MISSING_DAY       639787          /* 3 Sep 1752 */
 #define        NUMBER_MISSING_DAYS     11              /* 11 day correction */
+
+#define        MAXDAYS                 42              /* max slots in a month array */
 #define        SPACE                   -1              /* used in day array */
 
 static int days_in_month[2][13] = {
 #define        SPACE                   -1              /* used in day array */
 
 static int days_in_month[2][13] = {
@@ -34,29 +63,36 @@ static int days_in_month[2][13] = {
        {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
 };
 
        {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
 };
 
-static int sep1752[42] = {
+int sep1752[MAXDAYS] = {
        SPACE,  SPACE,  1,      2,      14,     15,     16,
        17,     18,     19,     20,     21,     22,     23,
        24,     25,     26,     27,     28,     29,     30,
        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
        SPACE,  SPACE,  1,      2,      14,     15,     16,
        17,     18,     19,     20,     21,     22,     23,
        24,     25,     26,     27,     28,     29,     30,
        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
-}, j_sep1752[42] = {
+}, j_sep1752[MAXDAYS] = {
        SPACE,  SPACE,  245,    246,    258,    259,    260,
        261,    262,    263,    264,    265,    266,    267,
        268,    269,    270,    271,    272,    273,    274,
        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
        SPACE,  SPACE,  245,    246,    258,    259,    260,
        261,    262,    263,    264,    265,    266,    267,
        268,    269,    270,    271,    272,    273,    274,
        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
+}, empty[MAXDAYS] = {
+       SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
+       SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
+       SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
+       SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
+       SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
+       SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
 };
 
 };
 
-static char *month_names[12] = {
+char *month_names[12] = {
        "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December",
 };
 
        "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December",
 };
 
-static char *day_headings = " S  M  Tu W  Th F  S";
-static char *j_day_headings = " S   M   Tu  W   Th  F   S";
+char *day_headings = " S  M Tu  W Th  F  S";
+char *j_day_headings = "  S   M  Tu   W  Th   F   S";
 
 /* leap year -- account for gregorian reformation in 1752 */
 #define        leap_year(yr) \
 
 /* leap year -- account for gregorian reformation in 1752 */
 #define        leap_year(yr) \
@@ -103,16 +139,19 @@ main(argc, argv)
        argc -= optind;
        argv += optind;
 
        argc -= optind;
        argv += optind;
 
+       month = 0;
        switch(argc) {
        case 2:
                if ((month = atoi(*argv++)) <= 0 || month > 12) {
        switch(argc) {
        case 2:
                if ((month = atoi(*argv++)) <= 0 || month > 12) {
-                       (void)fprintf(stderr, "cal: illegal month value.\n");
+                       (void)fprintf(stderr,
+                           "cal: illegal month value: use 0-12\n");
                        exit(1);
                }
                /* FALLTHROUGH */
        case 1:
                if ((year = atoi(*argv)) <= 0 || year > 9999) {
                        exit(1);
                }
                /* FALLTHROUGH */
        case 1:
                if ((year = atoi(*argv)) <= 0 || year > 9999) {
-                       (void)fprintf(stderr, "cal: illegal year value.\n");
+                       (void)fprintf(stderr,
+                           "cal: illegal year value: use 0-9999\n");
                        exit(1);
                }
                break;
                        exit(1);
                }
                break;
@@ -126,6 +165,7 @@ main(argc, argv)
        default:
                usage();
        }
        default:
                usage();
        }
+
        if (month)
                monthly(month, year);
        else if (julian)
        if (month)
                monthly(month, year);
        else if (julian)
@@ -140,14 +180,14 @@ main(argc, argv)
 #define        WEEK_LEN        20              /* 7 * 3 - one space at the end */
 #define        J_WEEK_LEN      27              /* 7 * 4 - one space at the end */
 #define        HEAD_SEP        2               /* spaces between day headings */
 #define        WEEK_LEN        20              /* 7 * 3 - one space at the end */
 #define        J_WEEK_LEN      27              /* 7 * 4 - one space at the end */
 #define        HEAD_SEP        2               /* spaces between day headings */
-#define        J_HEAD_SEP      3
+#define        J_HEAD_SEP      2
 
 monthly(month, year)
        int month, year;
 {
        register int col, row;
        register char *p;
 
 monthly(month, year)
        int month, year;
 {
        register int col, row;
        register char *p;
-       int len, days[42];
+       int len, days[MAXDAYS];
        char lineout[30];
 
        day_array(month, year, days);
        char lineout[30];
 
        day_array(month, year, days);
@@ -169,14 +209,14 @@ j_yearly(year)
 {
        register int col, *dp, i, month, row, which_cal;
        register char *p;
 {
        register int col, *dp, i, month, row, which_cal;
        register char *p;
-       int days[12][42];
+       int days[12][MAXDAYS];
        char lineout[80];
 
        (void)sprintf(lineout, "%d", year);
        center(lineout, J_WEEK_LEN * 2 + J_HEAD_SEP, 0);
        (void)printf("\n\n");
        for (i = 0; i < 12; i++)
        char lineout[80];
 
        (void)sprintf(lineout, "%d", year);
        center(lineout, J_WEEK_LEN * 2 + J_HEAD_SEP, 0);
        (void)printf("\n\n");
        for (i = 0; i < 12; i++)
-               day_array(i + 1, year, &days[i][0]);
+               day_array(i + 1, year, days[i]);
        (void)memset(lineout, ' ', sizeof(lineout) - 1);
        lineout[sizeof(lineout) - 1] = '\0';
        for (month = 0; month < 12; month += 2) {
        (void)memset(lineout, ' ', sizeof(lineout) - 1);
        lineout[sizeof(lineout) - 1] = '\0';
        for (month = 0; month < 12; month += 2) {
@@ -203,14 +243,14 @@ yearly(year)
 {
        register int col, *dp, i, month, row, which_cal;
        register char *p;
 {
        register int col, *dp, i, month, row, which_cal;
        register char *p;
-       int days[12][42];
+       int days[12][MAXDAYS];
        char lineout[80];
 
        (void)sprintf(lineout, "%d", year);
        center(lineout, WEEK_LEN * 3 + HEAD_SEP * 2, 0);
        (void)printf("\n\n");
        for (i = 0; i < 12; i++)
        char lineout[80];
 
        (void)sprintf(lineout, "%d", year);
        center(lineout, WEEK_LEN * 3 + HEAD_SEP * 2, 0);
        (void)printf("\n\n");
        for (i = 0; i < 12; i++)
-               day_array(i + 1, year, &days[i][0]);
+               day_array(i + 1, year, days[i]);
        (void)memset(lineout, ' ', sizeof(lineout) - 1);
        lineout[sizeof(lineout) - 1] = '\0';
        for (month = 0; month < 12; month += 3) {
        (void)memset(lineout, ' ', sizeof(lineout) - 1);
        lineout[sizeof(lineout) - 1] = '\0';
        for (month = 0; month < 12; month += 3) {
@@ -244,18 +284,16 @@ day_array(month, year, days)
        register int *days;
        int month, year;
 {
        register int *days;
        int month, year;
 {
-       register int i, day, dw, dm, *p;
+       register int i, day, dw, dm;
 
 
-       dm = days_in_month[leap_year(year)][month];
-       dw = day_in_week(1, month, year);
        if (month == 9 && year == 1752) {
        if (month == 9 && year == 1752) {
-               p = julian ? j_sep1752 : sep1752;
-               for (i = 0; i < 42; i++)
-                       *days++ = *p++;
+               bcopy(julian ? j_sep1752 : sep1752,
+                   days, MAXDAYS * sizeof(int));
                return;
        }
                return;
        }
-       for (i = 42, p = days; i--;)
-               *p++ = SPACE;
+       bcopy(empty, days, MAXDAYS * sizeof(int));
+       dm = days_in_month[leap_year(year)][month];
+       dw = day_in_week(1, month, year);
        day = julian ? day_in_year(1, month, year) : 1;
        while (dm--)
                days[dw++] = day++;
        day = julian ? day_in_year(1, month, year) : 1;
        while (dm--)
                days[dw++] = day++;
@@ -303,26 +341,38 @@ ascii_day(p, day)
        register int day;
 {
        register int display, val;
        register int day;
 {
        register int display, val;
+       static char *aday[] = {
+               "",
+               " 1", " 2", " 3", " 4", " 5", " 6", " 7",
+               " 8", " 9", "10", "11", "12", "13", "14",
+               "15", "16", "17", "18", "19", "20", "21",
+               "22", "23", "24", "25", "26", "27", "28",
+               "29", "30", "31",
+       };
 
        if (day == SPACE) {
 
        if (day == SPACE) {
-               memset(p, ' ', julian ? 4 : 3);
+               memset(p, ' ', julian ? J_DAY_LEN : DAY_LEN);
                return;
        }
                return;
        }
-       display = 0;
        if (julian) {
                if (val = day / 100) {
                        day %= 100;
                        *p++ = val + '0';
                        display = 1;
        if (julian) {
                if (val = day / 100) {
                        day %= 100;
                        *p++ = val + '0';
                        display = 1;
-               } else
+               } else {
                        *p++ = ' ';
                        *p++ = ' ';
+                       display = 0;
+               }
+               val = day / 10;
+               if (val || display)
+                       *p++ = val + '0';
+               else
+                       *p++ = ' ';
+               *p++ = day % 10 + '0';
+       } else {
+               *p++ = aday[day][0];
+               *p++ = aday[day][1];
        }
        }
-       val = day / 10;
-       if (val || display)
-               *p++ = val + '0';
-       else
-               *p++ = ' ';
-       *p++ = day % 10 + '0';
        *p = ' ';
 }
 
        *p = ' ';
 }