[BACK]Return to cal.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cal

Annotation of src/usr.bin/cal/cal.c, Revision 1.19

1.19    ! ray         1: /*     $OpenBSD: cal.c,v 1.18 2005/12/08 14:54:30 jmc Exp $    */
1.1       deraadt     2: /*     $NetBSD: cal.c,v 1.6 1995/03/26 03:10:24 glass Exp $    */
                      3:
                      4: /*
                      5:  * Copyright (c) 1989, 1993, 1994
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * This code is derived from software contributed to Berkeley by
                      9:  * Kim Letkeman.
                     10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
1.9       millert    19:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
                     36: #ifndef lint
1.13      tedu       37: static const char copyright[] =
1.1       deraadt    38: "@(#) Copyright (c) 1989, 1993, 1994\n\
                     39:        The Regents of the University of California.  All rights reserved.\n";
                     40: #if 0
                     41: static char sccsid[] = "@(#)cal.c      8.4 (Berkeley) 4/2/94";
                     42: #else
1.19    ! ray        43: static const char rcsid[] = "$OpenBSD: cal.c,v 1.18 2005/12/08 14:54:30 jmc Exp $";
1.1       deraadt    44: #endif
                     45: #endif /* not lint */
                     46:
                     47: #include <sys/types.h>
                     48:
                     49: #include <ctype.h>
                     50: #include <err.h>
                     51: #include <stdio.h>
                     52: #include <stdlib.h>
                     53: #include <string.h>
                     54: #include <time.h>
1.5       deraadt    55: #include <tzfile.h>
1.1       deraadt    56: #include <unistd.h>
                     57:
                     58: #define        THURSDAY                4               /* for reformation */
1.14      deraadt    59: #define        SATURDAY                6               /* 1 Jan 1 was a Saturday */
1.1       deraadt    60:
1.14      deraadt    61: #define        FIRST_MISSING_DAY       639799          /* 3 Sep 1752 */
                     62: #define        NUMBER_MISSING_DAYS     11              /* 11 day correction */
1.1       deraadt    63:
                     64: #define        MAXDAYS                 42              /* max slots in a month array */
                     65: #define        SPACE                   -1              /* used in day array */
                     66:
1.13      tedu       67: static const int days_in_month[2][13] = {
1.1       deraadt    68:        {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
                     69:        {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
                     70: };
                     71:
1.13      tedu       72: const int sep1752[MAXDAYS] = {
1.1       deraadt    73:        SPACE,  SPACE,  1,      2,      14,     15,     16,
                     74:        17,     18,     19,     20,     21,     22,     23,
                     75:        24,     25,     26,     27,     28,     29,     30,
                     76:        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
                     77:        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
                     78:        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
                     79: }, j_sep1752[MAXDAYS] = {
                     80:        SPACE,  SPACE,  245,    246,    258,    259,    260,
                     81:        261,    262,    263,    264,    265,    266,    267,
                     82:        268,    269,    270,    271,    272,    273,    274,
                     83:        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
                     84:        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
                     85:        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
                     86: }, empty[MAXDAYS] = {
                     87:        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
                     88:        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
                     89:        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
                     90:        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
                     91:        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
                     92:        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
                     93: };
                     94:
1.13      tedu       95: const char *month_names[12] = {
1.1       deraadt    96:        "January", "February", "March", "April", "May", "June",
                     97:        "July", "August", "September", "October", "November", "December",
                     98: };
                     99:
1.13      tedu      100: const char *day_headings = "Su Mo Tu We Th Fr Sa";
                    101: const char *j_day_headings = " Su  Mo  Tu  We  Th  Fr  Sa";
1.1       deraadt   102:
                    103: /* leap year -- account for gregorian reformation in 1752 */
                    104: #define        leap_year(yr) \
                    105:        ((yr) <= 1752 ? !((yr) % 4) : \
1.5       deraadt   106:        (!((yr) % 4) && ((yr) % 100)) || !((yr) % 400))
1.1       deraadt   107:
                    108: /* number of centuries since 1700, not inclusive */
                    109: #define        centuries_since_1700(yr) \
                    110:        ((yr) > 1700 ? (yr) / 100 - 17 : 0)
                    111:
                    112: /* number of centuries since 1700 whose modulo of 400 is 0 */
                    113: #define        quad_centuries_since_1700(yr) \
                    114:        ((yr) > 1600 ? ((yr) - 1600) / 400 : 0)
                    115:
                    116: /* number of leap years between year 1 and this year, not inclusive */
                    117: #define        leap_years_since_year_1(yr) \
                    118:        ((yr) / 4 - centuries_since_1700(yr) + quad_centuries_since_1700(yr))
                    119:
                    120: int julian;
                    121:
1.7       millert   122: void   ascii_day(char *, int);
1.13      tedu      123: void   center(const char *, int, int);
1.7       millert   124: void   day_array(int, int, int *);
                    125: int    day_in_week(int, int, int);
                    126: int    day_in_year(int, int, int);
                    127: void   j_yearly(int);
                    128: void   monthly(int, int);
                    129: void   trim_trailing_spaces(char *);
                    130: void   usage(void);
                    131: void   yearly(int);
1.13      tedu      132: int    parsemonth(const char *);
1.1       deraadt   133:
                    134: int
1.10      deraadt   135: main(int argc, char *argv[])
1.1       deraadt   136: {
                    137:        struct tm *local_time;
                    138:        time_t now;
                    139:        int ch, month, year, yflag;
                    140:
1.6       deraadt   141:        yflag = year = 0;
1.4       millert   142:        while ((ch = getopt(argc, argv, "jy")) != -1)
1.1       deraadt   143:                switch(ch) {
                    144:                case 'j':
                    145:                        julian = 1;
                    146:                        break;
                    147:                case 'y':
                    148:                        yflag = 1;
                    149:                        break;
                    150:                case '?':
                    151:                default:
                    152:                        usage();
                    153:                }
                    154:        argc -= optind;
                    155:        argv += optind;
                    156:
                    157:        month = 0;
                    158:        switch(argc) {
                    159:        case 2:
1.12      tedu      160:                month = parsemonth(*argv++);
                    161:                if (!month)
                    162:                        errx(1, "Unable to parse month");
1.1       deraadt   163:                /* FALLTHROUGH */
                    164:        case 1:
1.12      tedu      165:                if (argc == 1 && !isdigit(*argv[0])) {
                    166:                        month = parsemonth(*argv);
                    167:                        if (!month)
                    168:                                errx(1, "illegal year value: use 1-9999");
                    169:                        (void)time(&now);
                    170:                        local_time = localtime(&now);
                    171:                        year = local_time->tm_year + TM_YEAR_BASE;
                    172:                } else {
                    173:                        if ((year = atoi(*argv)) < 1 || year > 9999)
                    174:                                errx(1, "illegal year value: use 1-9999");
                    175:                }
1.1       deraadt   176:                break;
                    177:        case 0:
                    178:                (void)time(&now);
                    179:                local_time = localtime(&now);
1.5       deraadt   180:                year = local_time->tm_year + TM_YEAR_BASE;
1.1       deraadt   181:                if (!yflag)
                    182:                        month = local_time->tm_mon + 1;
                    183:                break;
                    184:        default:
                    185:                usage();
                    186:        }
                    187:
                    188:        if (month)
                    189:                monthly(month, year);
                    190:        else if (julian)
                    191:                j_yearly(year);
                    192:        else
                    193:                yearly(year);
                    194:        exit(0);
                    195: }
                    196:
                    197: #define        DAY_LEN         3               /* 3 spaces per day */
                    198: #define        J_DAY_LEN       4               /* 4 spaces per day */
                    199: #define        WEEK_LEN        20              /* 7 * 3 - one space at the end */
                    200: #define        J_WEEK_LEN      27              /* 7 * 4 - one space at the end */
                    201: #define        HEAD_SEP        2               /* spaces between day headings */
                    202: #define        J_HEAD_SEP      2
                    203:
                    204: void
1.10      deraadt   205: monthly(int month, int year)
1.1       deraadt   206: {
                    207:        int col, row, len, days[MAXDAYS];
                    208:        char *p, lineout[30];
                    209:
                    210:        day_array(month, year, days);
1.19    ! ray       211:        (void)snprintf(lineout, sizeof(lineout), "%s %d",
1.8       deraadt   212:            month_names[month - 1], year);
1.11      deraadt   213:        len = strlen(lineout);
1.1       deraadt   214:        (void)printf("%*s%s\n%s\n",
                    215:            ((julian ? J_WEEK_LEN : WEEK_LEN) - len) / 2, "",
                    216:            lineout, julian ? j_day_headings : day_headings);
                    217:        for (row = 0; row < 6; row++) {
                    218:                for (col = 0, p = lineout; col < 7; col++,
                    219:                    p += julian ? J_DAY_LEN : DAY_LEN)
                    220:                        ascii_day(p, days[row * 7 + col]);
                    221:                *p = '\0';
                    222:                trim_trailing_spaces(lineout);
                    223:                (void)printf("%s\n", lineout);
                    224:        }
                    225: }
                    226:
                    227: void
1.10      deraadt   228: j_yearly(int year)
1.1       deraadt   229: {
                    230:        int col, *dp, i, month, row, which_cal;
                    231:        int days[12][MAXDAYS];
                    232:        char *p, lineout[80];
                    233:
1.19    ! ray       234:        (void)snprintf(lineout, sizeof(lineout), "%d", year);
1.1       deraadt   235:        center(lineout, J_WEEK_LEN * 2 + J_HEAD_SEP, 0);
                    236:        (void)printf("\n\n");
                    237:        for (i = 0; i < 12; i++)
                    238:                day_array(i + 1, year, days[i]);
                    239:        (void)memset(lineout, ' ', sizeof(lineout) - 1);
                    240:        lineout[sizeof(lineout) - 1] = '\0';
                    241:        for (month = 0; month < 12; month += 2) {
                    242:                center(month_names[month], J_WEEK_LEN, J_HEAD_SEP);
                    243:                center(month_names[month + 1], J_WEEK_LEN, 0);
                    244:                (void)printf("\n%s%*s%s\n", j_day_headings, J_HEAD_SEP, "",
                    245:                    j_day_headings);
                    246:                for (row = 0; row < 6; row++) {
                    247:                        for (which_cal = 0; which_cal < 2; which_cal++) {
                    248:                                p = lineout + which_cal * (J_WEEK_LEN + 2);
                    249:                                dp = &days[month + which_cal][row * 7];
                    250:                                for (col = 0; col < 7; col++, p += J_DAY_LEN)
                    251:                                        ascii_day(p, *dp++);
                    252:                        }
                    253:                        *p = '\0';
                    254:                        trim_trailing_spaces(lineout);
                    255:                        (void)printf("%s\n", lineout);
                    256:                }
                    257:        }
                    258:        (void)printf("\n");
                    259: }
                    260:
                    261: void
1.10      deraadt   262: yearly(int year)
1.1       deraadt   263: {
                    264:        int col, *dp, i, month, row, which_cal;
                    265:        int days[12][MAXDAYS];
                    266:        char *p, lineout[80];
                    267:
1.19    ! ray       268:        (void)snprintf(lineout, sizeof(lineout), "%d", year);
1.1       deraadt   269:        center(lineout, WEEK_LEN * 3 + HEAD_SEP * 2, 0);
                    270:        (void)printf("\n\n");
                    271:        for (i = 0; i < 12; i++)
                    272:                day_array(i + 1, year, days[i]);
                    273:        (void)memset(lineout, ' ', sizeof(lineout) - 1);
                    274:        lineout[sizeof(lineout) - 1] = '\0';
                    275:        for (month = 0; month < 12; month += 3) {
                    276:                center(month_names[month], WEEK_LEN, HEAD_SEP);
                    277:                center(month_names[month + 1], WEEK_LEN, HEAD_SEP);
                    278:                center(month_names[month + 2], WEEK_LEN, 0);
                    279:                (void)printf("\n%s%*s%s%*s%s\n", day_headings, HEAD_SEP,
                    280:                    "", day_headings, HEAD_SEP, "", day_headings);
                    281:                for (row = 0; row < 6; row++) {
                    282:                        for (which_cal = 0; which_cal < 3; which_cal++) {
                    283:                                p = lineout + which_cal * (WEEK_LEN + 2);
                    284:                                dp = &days[month + which_cal][row * 7];
                    285:                                for (col = 0; col < 7; col++, p += DAY_LEN)
                    286:                                        ascii_day(p, *dp++);
                    287:                        }
                    288:                        *p = '\0';
                    289:                        trim_trailing_spaces(lineout);
                    290:                        (void)printf("%s\n", lineout);
                    291:                }
                    292:        }
                    293:        (void)printf("\n");
                    294: }
                    295:
                    296: /*
                    297:  * day_array --
                    298:  *     Fill in an array of 42 integers with a calendar.  Assume for a moment
                    299:  *     that you took the (maximum) 6 rows in a calendar and stretched them
                    300:  *     out end to end.  You would have 42 numbers or spaces.  This routine
                    301:  *     builds that array for any month from Jan. 1 through Dec. 9999.
                    302:  */
                    303: void
1.10      deraadt   304: day_array(int month, int year, int *days)
1.1       deraadt   305: {
                    306:        int day, dw, dm;
                    307:
                    308:        if (month == 9 && year == 1752) {
                    309:                memmove(days,
1.13      tedu      310:                    julian ? j_sep1752 : sep1752, MAXDAYS * sizeof(int));
1.1       deraadt   311:                return;
                    312:        }
                    313:        memmove(days, empty, MAXDAYS * sizeof(int));
                    314:        dm = days_in_month[leap_year(year)][month];
                    315:        dw = day_in_week(1, month, year);
                    316:        day = julian ? day_in_year(1, month, year) : 1;
                    317:        while (dm--)
                    318:                days[dw++] = day++;
                    319: }
                    320:
                    321: /*
                    322:  * day_in_year --
                    323:  *     return the 1 based day number within the year
                    324:  */
                    325: int
1.10      deraadt   326: day_in_year(int day, int month, int year)
1.1       deraadt   327: {
                    328:        int i, leap;
                    329:
                    330:        leap = leap_year(year);
                    331:        for (i = 1; i < month; i++)
                    332:                day += days_in_month[leap][i];
                    333:        return (day);
                    334: }
                    335:
                    336: /*
                    337:  * day_in_week
                    338:  *     return the 0 based day number for any date from 1 Jan. 1 to
                    339:  *     31 Dec. 9999.  Assumes the Gregorian reformation eliminates
                    340:  *     3 Sep. 1752 through 13 Sep. 1752.  Returns Thursday for all
                    341:  *     missing days.
                    342:  */
                    343: int
1.10      deraadt   344: day_in_week(int day, int month, int year)
1.1       deraadt   345: {
                    346:        long temp;
                    347:
                    348:        temp = (long)(year - 1) * 365 + leap_years_since_year_1(year - 1)
                    349:            + day_in_year(day, month, year);
                    350:        if (temp < FIRST_MISSING_DAY)
                    351:                return ((temp - 1 + SATURDAY) % 7);
                    352:        if (temp >= (FIRST_MISSING_DAY + NUMBER_MISSING_DAYS))
                    353:                return (((temp - 1 + SATURDAY) - NUMBER_MISSING_DAYS) % 7);
                    354:        return (THURSDAY);
                    355: }
                    356:
                    357: void
1.10      deraadt   358: ascii_day(char *p, int day)
1.1       deraadt   359: {
                    360:        int display, val;
1.13      tedu      361:        static const char *aday[] = {
1.1       deraadt   362:                "",
                    363:                " 1", " 2", " 3", " 4", " 5", " 6", " 7",
                    364:                " 8", " 9", "10", "11", "12", "13", "14",
                    365:                "15", "16", "17", "18", "19", "20", "21",
                    366:                "22", "23", "24", "25", "26", "27", "28",
                    367:                "29", "30", "31",
                    368:        };
                    369:
                    370:        if (day == SPACE) {
                    371:                memset(p, ' ', julian ? J_DAY_LEN : DAY_LEN);
                    372:                return;
                    373:        }
                    374:        if (julian) {
1.5       deraadt   375:                val = day / 100;
                    376:                if (val) {
1.1       deraadt   377:                        day %= 100;
                    378:                        *p++ = val + '0';
                    379:                        display = 1;
                    380:                } else {
                    381:                        *p++ = ' ';
                    382:                        display = 0;
                    383:                }
                    384:                val = day / 10;
                    385:                if (val || display)
                    386:                        *p++ = val + '0';
                    387:                else
                    388:                        *p++ = ' ';
                    389:                *p++ = day % 10 + '0';
                    390:        } else {
                    391:                *p++ = aday[day][0];
                    392:                *p++ = aday[day][1];
                    393:        }
                    394:        *p = ' ';
                    395: }
                    396:
                    397: void
1.10      deraadt   398: trim_trailing_spaces(char *s)
1.1       deraadt   399: {
                    400:        char *p;
                    401:
                    402:        for (p = s; *p; ++p)
                    403:                continue;
                    404:        while (p > s && isspace(*--p))
                    405:                continue;
                    406:        if (p > s)
                    407:                ++p;
                    408:        *p = '\0';
                    409: }
                    410:
                    411: void
1.13      tedu      412: center(const char *str, int len, int separate)
1.1       deraadt   413: {
                    414:
                    415:        len -= strlen(str);
                    416:        (void)printf("%*s%s%*s", len / 2, "", str, len / 2 + len % 2, "");
                    417:        if (separate)
                    418:                (void)printf("%*s", separate, "");
                    419: }
                    420:
                    421: void
1.10      deraadt   422: usage(void)
1.1       deraadt   423: {
                    424:
1.18      jmc       425:        (void)fprintf(stderr, "usage: cal [-jy] [month] [year]\n");
1.1       deraadt   426:        exit(1);
1.12      tedu      427: }
                    428:
                    429: int
                    430: parsemonth(const char *s)
                    431: {
1.15      deraadt   432:        struct tm tm;
                    433:        char *cp;
1.12      tedu      434:        int v;
                    435:
                    436:        v = (int)strtol(s, &cp, 10);
1.17      tom       437:        if (*cp != '\0') {              /* s wasn't purely numeric */
                    438:                v = 0;
                    439:                if ((cp = strptime(s, "%b", &tm)) != NULL && *cp == '\0')
                    440:                        v = tm.tm_mon + 1;
                    441:        }
1.15      deraadt   442:        if (v <= 0 || v > 12)
1.17      tom       443:                errx(1, "invalid month: use 1-12 or a name");
1.15      deraadt   444:        return (v);
1.1       deraadt   445: }