[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.27

1.27    ! millert     1: /*     $OpenBSD: cal.c,v 1.26 2013/11/20 21:33:17 deraadt 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: #include <sys/types.h>
                     37:
                     38: #include <ctype.h>
                     39: #include <err.h>
                     40: #include <stdio.h>
                     41: #include <stdlib.h>
                     42: #include <string.h>
                     43: #include <time.h>
                     44: #include <unistd.h>
                     45:
                     46: #define        THURSDAY                4               /* for reformation */
1.14      deraadt    47: #define        SATURDAY                6               /* 1 Jan 1 was a Saturday */
1.1       deraadt    48:
1.14      deraadt    49: #define        FIRST_MISSING_DAY       639799          /* 3 Sep 1752 */
                     50: #define        NUMBER_MISSING_DAYS     11              /* 11 day correction */
1.1       deraadt    51:
                     52: #define        MAXDAYS                 42              /* max slots in a month array */
                     53: #define        SPACE                   -1              /* used in day array */
                     54:
1.13      tedu       55: static const int days_in_month[2][13] = {
1.1       deraadt    56:        {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
                     57:        {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
                     58: };
                     59:
1.22      pyr        60: const int sep1752s[MAXDAYS] = {
1.1       deraadt    61:        SPACE,  SPACE,  1,      2,      14,     15,     16,
                     62:        17,     18,     19,     20,     21,     22,     23,
                     63:        24,     25,     26,     27,     28,     29,     30,
                     64:        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
                     65:        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
                     66:        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
1.22      pyr        67: }, sep1752m[MAXDAYS] = {
                     68:        SPACE,  1,      2,      14,     15,     16,     17,
                     69:        18,     19,     20,     21,     22,     23,     24,
                     70:        25,     26,     27,     28,     29,     30,     SPACE,
                     71:        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
                     72:        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
                     73:        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
                     74: }, sep1752js[MAXDAYS] = {
1.1       deraadt    75:        SPACE,  SPACE,  245,    246,    258,    259,    260,
                     76:        261,    262,    263,    264,    265,    266,    267,
                     77:        268,    269,    270,    271,    272,    273,    274,
                     78:        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
                     79:        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
                     80:        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
1.22      pyr        81: }, sep1752jm[MAXDAYS] = {
                     82:        SPACE,  245,    246,    258,    259,    260,    261,
                     83:        262,    263,    264,    265,    266,    267,    268,
                     84:        269,    270,    271,    272,    273,    274,    SPACE,
                     85:        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
                     86:        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
                     87:        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
1.1       deraadt    88: }, empty[MAXDAYS] = {
                     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:        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
                     94:        SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,  SPACE,
                     95: };
                     96:
1.13      tedu       97: const char *month_names[12] = {
1.1       deraadt    98:        "January", "February", "March", "April", "May", "June",
                     99:        "July", "August", "September", "October", "November", "December",
                    100: };
                    101:
1.22      pyr       102: #define        DAY_HEADINGS_S  "Su Mo Tu We Th Fr Sa"
                    103: #define        DAY_HEADINGS_M  "Mo Tu We Th Fr Sa Su"
                    104: #define        DAY_HEADINGS_JS " Su  Mo  Tu  We  Th  Fr  Sa"
                    105: #define        DAY_HEADINGS_JM " Mo  Tu  We  Th  Fr  Sa  Su"
                    106:
                    107: const int      *sep1752 = NULL;
                    108: const char     *day_headings = NULL;
1.1       deraadt   109:
                    110: /* leap year -- account for gregorian reformation in 1752 */
                    111: #define        leap_year(yr) \
                    112:        ((yr) <= 1752 ? !((yr) % 4) : \
1.5       deraadt   113:        (!((yr) % 4) && ((yr) % 100)) || !((yr) % 400))
1.1       deraadt   114:
                    115: /* number of centuries since 1700, not inclusive */
                    116: #define        centuries_since_1700(yr) \
                    117:        ((yr) > 1700 ? (yr) / 100 - 17 : 0)
                    118:
                    119: /* number of centuries since 1700 whose modulo of 400 is 0 */
                    120: #define        quad_centuries_since_1700(yr) \
                    121:        ((yr) > 1600 ? ((yr) - 1600) / 400 : 0)
                    122:
                    123: /* number of leap years between year 1 and this year, not inclusive */
                    124: #define        leap_years_since_year_1(yr) \
                    125:        ((yr) / 4 - centuries_since_1700(yr) + quad_centuries_since_1700(yr))
                    126:
                    127: int julian;
1.22      pyr       128: int mflag = 0;
1.23      pyr       129: int wflag = 0;
1.1       deraadt   130:
1.7       millert   131: void   ascii_day(char *, int);
1.13      tedu      132: void   center(const char *, int, int);
1.7       millert   133: void   day_array(int, int, int *);
                    134: int    day_in_week(int, int, int);
                    135: int    day_in_year(int, int, int);
1.23      pyr       136: int    week(int, int, int);
1.24      otto      137: int    isoweek(int, int, int);
1.7       millert   138: void   j_yearly(int);
                    139: void   monthly(int, int);
                    140: void   trim_trailing_spaces(char *);
                    141: void   usage(void);
                    142: void   yearly(int);
1.13      tedu      143: int    parsemonth(const char *);
1.1       deraadt   144:
                    145: int
1.10      deraadt   146: main(int argc, char *argv[])
1.1       deraadt   147: {
                    148:        struct tm *local_time;
                    149:        time_t now;
                    150:        int ch, month, year, yflag;
1.20      tedu      151:        const char *errstr;
1.1       deraadt   152:
1.6       deraadt   153:        yflag = year = 0;
1.23      pyr       154:        while ((ch = getopt(argc, argv, "jmwy")) != -1)
1.1       deraadt   155:                switch(ch) {
                    156:                case 'j':
                    157:                        julian = 1;
                    158:                        break;
1.22      pyr       159:                case 'm':
                    160:                        mflag = 1;
                    161:                        break;
1.23      pyr       162:                case 'w':
                    163:                        wflag = 1;
                    164:                        break;
1.1       deraadt   165:                case 'y':
                    166:                        yflag = 1;
                    167:                        break;
                    168:                case '?':
                    169:                default:
                    170:                        usage();
                    171:                }
                    172:        argc -= optind;
                    173:        argv += optind;
                    174:
1.23      pyr       175:        if (julian && wflag)
                    176:                usage();
                    177:
1.22      pyr       178:        day_headings = DAY_HEADINGS_S;
                    179:        sep1752 = sep1752s;
                    180:        if (mflag && julian) {
                    181:                sep1752 = sep1752jm;
                    182:                day_headings = DAY_HEADINGS_JM;
                    183:        } else if (mflag) {
                    184:                sep1752 = sep1752m;
                    185:                day_headings = DAY_HEADINGS_M;
                    186:        } else if (julian) {
                    187:                sep1752 = sep1752js;
                    188:                day_headings = DAY_HEADINGS_JS;
                    189:        }
                    190:
1.1       deraadt   191:        month = 0;
                    192:        switch(argc) {
                    193:        case 2:
1.12      tedu      194:                month = parsemonth(*argv++);
                    195:                if (!month)
                    196:                        errx(1, "Unable to parse month");
1.1       deraadt   197:                /* FALLTHROUGH */
                    198:        case 1:
1.26      deraadt   199:                if (argc == 1 && !isdigit((unsigned char)*argv[0])) {
1.12      tedu      200:                        month = parsemonth(*argv);
                    201:                        if (!month)
                    202:                                errx(1, "illegal year value: use 1-9999");
                    203:                        (void)time(&now);
                    204:                        local_time = localtime(&now);
                    205:                        year = local_time->tm_year + TM_YEAR_BASE;
                    206:                } else {
1.20      tedu      207:                        year = strtonum(*argv, 1, 9999, &errstr);
                    208:                        if (errstr)
1.12      tedu      209:                                errx(1, "illegal year value: use 1-9999");
                    210:                }
1.1       deraadt   211:                break;
                    212:        case 0:
                    213:                (void)time(&now);
                    214:                local_time = localtime(&now);
1.5       deraadt   215:                year = local_time->tm_year + TM_YEAR_BASE;
1.1       deraadt   216:                if (!yflag)
                    217:                        month = local_time->tm_mon + 1;
                    218:                break;
                    219:        default:
                    220:                usage();
                    221:        }
                    222:
                    223:        if (month)
                    224:                monthly(month, year);
                    225:        else if (julian)
                    226:                j_yearly(year);
                    227:        else
                    228:                yearly(year);
                    229:        exit(0);
                    230: }
                    231:
                    232: #define        DAY_LEN         3               /* 3 spaces per day */
                    233: #define        J_DAY_LEN       4               /* 4 spaces per day */
                    234: #define        WEEK_LEN        20              /* 7 * 3 - one space at the end */
1.23      pyr       235: #define WEEKNUMBER_LEN 5               /* 5 spaces per week number */
1.1       deraadt   236: #define        J_WEEK_LEN      27              /* 7 * 4 - one space at the end */
                    237: #define        HEAD_SEP        2               /* spaces between day headings */
                    238: #define        J_HEAD_SEP      2
                    239:
1.23      pyr       240: int
                    241: week(int day, int month, int year)
                    242: {
                    243:        int     yearday;
                    244:        int     firstweekday;
                    245:        int     weekday;
                    246:        int     firstday;
                    247:        int     firstsunday;
                    248:        int     shift;
1.24      otto      249:
                    250:        if (mflag)
                    251:                return isoweek(day, month, year);
1.23      pyr       252:
                    253:        yearday = day_in_year(day, month, year);
                    254:        firstweekday = day_in_week(1, 1, year) + 1;
                    255:        weekday = day_in_week(day, month, year) + 1;
1.24      otto      256:        firstday = day_in_year(1, 1, year);
1.23      pyr       257:        firstsunday = firstday + (8 - firstweekday);
                    258:
                    259:        shift = 1;
                    260:        if (yearday < firstsunday)
                    261:                return (1);
                    262:        if (firstweekday > THURSDAY - 1)
                    263:                shift = 2;
                    264:        return ((((yearday + 1) - (weekday - 1)) / 7) + shift);
                    265: }
                    266:
1.24      otto      267: int
                    268: isoweek(int day, int month, int year)
                    269: {
                    270:        /* http://www.tondering.dk/claus/cal/node8.html */
                    271:        int a, b, c, s, e, f, g, d, n;
                    272:
                    273:        a = month <= 2 ? year - 1 : year;
                    274:        b = a/4 - a/100 + a/400;
                    275:        c = (a-1)/4 - (a-1)/100 + (a-1)/400;
                    276:        s = b - c;
                    277:        if (month <= 2) {
                    278:                e = 0;
                    279:                f = day - 1 + 31 * (month-1);
                    280:        } else {
                    281:                e = s + 1;
                    282:                f = day + ((153 * (month-3) + 2) / 5) + 58 + s;
                    283:        }
                    284:        g = (a + b) % 7;
                    285:        d = (f + g - e) % 7;
                    286:        n = f + 3 - d;
                    287:
                    288:        if (n < 0)
                    289:                return 53 - (g - s) / 5;
                    290:        else if (n > 364 + s)
                    291:                return 1;
                    292:        else
                    293:                return n/7 + 1;
                    294: }
                    295:
1.1       deraadt   296: void
1.10      deraadt   297: monthly(int month, int year)
1.1       deraadt   298: {
1.23      pyr       299:        int col, row, len, days[MAXDAYS], firstday;
1.1       deraadt   300:        char *p, lineout[30];
                    301:
                    302:        day_array(month, year, days);
1.19      ray       303:        (void)snprintf(lineout, sizeof(lineout), "%s %d",
1.8       deraadt   304:            month_names[month - 1], year);
1.11      deraadt   305:        len = strlen(lineout);
1.1       deraadt   306:        (void)printf("%*s%s\n%s\n",
                    307:            ((julian ? J_WEEK_LEN : WEEK_LEN) - len) / 2, "",
1.22      pyr       308:            lineout, day_headings);
1.1       deraadt   309:        for (row = 0; row < 6; row++) {
1.23      pyr       310:                firstday = SPACE;
1.1       deraadt   311:                for (col = 0, p = lineout; col < 7; col++,
1.23      pyr       312:                    p += julian ? J_DAY_LEN : DAY_LEN) {
                    313:                        if (firstday == SPACE && days[row * 7 + col] != SPACE)
                    314:                                firstday = days[row * 7 + col];
1.1       deraadt   315:                        ascii_day(p, days[row * 7 + col]);
1.23      pyr       316:                }
1.1       deraadt   317:                *p = '\0';
                    318:                trim_trailing_spaces(lineout);
1.23      pyr       319:                (void)printf("%-20s", lineout);
                    320:                if (wflag && firstday != SPACE)
1.24      otto      321:                        printf(" [%2d]", week(firstday, month, year));
1.23      pyr       322:                printf("\n");
1.1       deraadt   323:        }
                    324: }
                    325:
                    326: void
1.10      deraadt   327: j_yearly(int year)
1.1       deraadt   328: {
                    329:        int col, *dp, i, month, row, which_cal;
                    330:        int days[12][MAXDAYS];
                    331:        char *p, lineout[80];
                    332:
1.19      ray       333:        (void)snprintf(lineout, sizeof(lineout), "%d", year);
1.1       deraadt   334:        center(lineout, J_WEEK_LEN * 2 + J_HEAD_SEP, 0);
                    335:        (void)printf("\n\n");
                    336:        for (i = 0; i < 12; i++)
                    337:                day_array(i + 1, year, days[i]);
                    338:        (void)memset(lineout, ' ', sizeof(lineout) - 1);
                    339:        lineout[sizeof(lineout) - 1] = '\0';
                    340:        for (month = 0; month < 12; month += 2) {
                    341:                center(month_names[month], J_WEEK_LEN, J_HEAD_SEP);
                    342:                center(month_names[month + 1], J_WEEK_LEN, 0);
1.22      pyr       343:                (void)printf("\n%s%*s%s\n", day_headings,
                    344:                    J_HEAD_SEP, "", day_headings);
                    345:
1.1       deraadt   346:                for (row = 0; row < 6; row++) {
                    347:                        for (which_cal = 0; which_cal < 2; which_cal++) {
                    348:                                p = lineout + which_cal * (J_WEEK_LEN + 2);
                    349:                                dp = &days[month + which_cal][row * 7];
                    350:                                for (col = 0; col < 7; col++, p += J_DAY_LEN)
                    351:                                        ascii_day(p, *dp++);
                    352:                        }
                    353:                        *p = '\0';
                    354:                        trim_trailing_spaces(lineout);
                    355:                        (void)printf("%s\n", lineout);
                    356:                }
                    357:        }
                    358:        (void)printf("\n");
                    359: }
                    360:
                    361: void
1.10      deraadt   362: yearly(int year)
1.1       deraadt   363: {
1.23      pyr       364:        int col, *dp, i, month, row, which_cal, week_len, wn, firstday;
1.1       deraadt   365:        int days[12][MAXDAYS];
1.23      pyr       366:        char *p, lineout[81];
1.1       deraadt   367:
1.23      pyr       368:        week_len = WEEK_LEN;
                    369:        if (wflag)
                    370:                week_len += WEEKNUMBER_LEN;
1.19      ray       371:        (void)snprintf(lineout, sizeof(lineout), "%d", year);
1.23      pyr       372:        center(lineout, week_len * 3 + HEAD_SEP * 2, 0);
1.1       deraadt   373:        (void)printf("\n\n");
                    374:        for (i = 0; i < 12; i++)
                    375:                day_array(i + 1, year, days[i]);
                    376:        (void)memset(lineout, ' ', sizeof(lineout) - 1);
                    377:        lineout[sizeof(lineout) - 1] = '\0';
                    378:        for (month = 0; month < 12; month += 3) {
1.23      pyr       379:                center(month_names[month], week_len, HEAD_SEP);
                    380:                center(month_names[month + 1], week_len, HEAD_SEP);
                    381:                center(month_names[month + 2], week_len, 0);
1.22      pyr       382:                (void)printf("\n%s%*s%s%*s%s\n", day_headings,
1.23      pyr       383:                    HEAD_SEP + (wflag ? WEEKNUMBER_LEN : 0), "", day_headings,
                    384:                    HEAD_SEP + (wflag ? WEEKNUMBER_LEN : 0), "", day_headings);
1.22      pyr       385:
1.1       deraadt   386:                for (row = 0; row < 6; row++) {
                    387:                        for (which_cal = 0; which_cal < 3; which_cal++) {
1.23      pyr       388:                                p = lineout + which_cal * (week_len + 2);
                    389:
1.1       deraadt   390:                                dp = &days[month + which_cal][row * 7];
1.23      pyr       391:                                firstday = SPACE;
                    392:                                for (col = 0; col < 7; col++, p += DAY_LEN) {
                    393:                                        if (firstday == SPACE && *dp != SPACE)
                    394:                                                firstday = *dp;
1.1       deraadt   395:                                        ascii_day(p, *dp++);
1.23      pyr       396:                                }
                    397:                                if (wflag && firstday != SPACE) {
1.24      otto      398:                                        wn = week(firstday,
1.23      pyr       399:                                            month + which_cal + 1, year);
                    400:                                        (void)snprintf(p, 5, "[%2d]", wn);
                    401:                                        p += strlen(p);
                    402:                                        *p = ' ';
                    403:                                } else
                    404:                                        memset(p, ' ', 4);
1.1       deraadt   405:                        }
                    406:                        *p = '\0';
                    407:                        trim_trailing_spaces(lineout);
                    408:                        (void)printf("%s\n", lineout);
                    409:                }
                    410:        }
                    411:        (void)printf("\n");
                    412: }
                    413:
                    414: /*
                    415:  * day_array --
                    416:  *     Fill in an array of 42 integers with a calendar.  Assume for a moment
                    417:  *     that you took the (maximum) 6 rows in a calendar and stretched them
                    418:  *     out end to end.  You would have 42 numbers or spaces.  This routine
                    419:  *     builds that array for any month from Jan. 1 through Dec. 9999.
                    420:  */
                    421: void
1.10      deraadt   422: day_array(int month, int year, int *days)
1.1       deraadt   423: {
                    424:        int day, dw, dm;
                    425:
                    426:        if (month == 9 && year == 1752) {
1.22      pyr       427:                memmove(days, sep1752, MAXDAYS * sizeof(int));
1.1       deraadt   428:                return;
                    429:        }
                    430:        memmove(days, empty, MAXDAYS * sizeof(int));
                    431:        dm = days_in_month[leap_year(year)][month];
1.22      pyr       432:        dw = day_in_week(mflag?0:1, month, year);
1.1       deraadt   433:        day = julian ? day_in_year(1, month, year) : 1;
                    434:        while (dm--)
                    435:                days[dw++] = day++;
                    436: }
                    437:
                    438: /*
                    439:  * day_in_year --
                    440:  *     return the 1 based day number within the year
                    441:  */
                    442: int
1.10      deraadt   443: day_in_year(int day, int month, int year)
1.1       deraadt   444: {
                    445:        int i, leap;
                    446:
                    447:        leap = leap_year(year);
                    448:        for (i = 1; i < month; i++)
                    449:                day += days_in_month[leap][i];
                    450:        return (day);
                    451: }
                    452:
                    453: /*
                    454:  * day_in_week
                    455:  *     return the 0 based day number for any date from 1 Jan. 1 to
                    456:  *     31 Dec. 9999.  Assumes the Gregorian reformation eliminates
                    457:  *     3 Sep. 1752 through 13 Sep. 1752.  Returns Thursday for all
                    458:  *     missing days.
                    459:  */
                    460: int
1.10      deraadt   461: day_in_week(int day, int month, int year)
1.1       deraadt   462: {
                    463:        long temp;
                    464:
                    465:        temp = (long)(year - 1) * 365 + leap_years_since_year_1(year - 1)
                    466:            + day_in_year(day, month, year);
                    467:        if (temp < FIRST_MISSING_DAY)
                    468:                return ((temp - 1 + SATURDAY) % 7);
                    469:        if (temp >= (FIRST_MISSING_DAY + NUMBER_MISSING_DAYS))
                    470:                return (((temp - 1 + SATURDAY) - NUMBER_MISSING_DAYS) % 7);
                    471:        return (THURSDAY);
                    472: }
                    473:
                    474: void
1.10      deraadt   475: ascii_day(char *p, int day)
1.1       deraadt   476: {
                    477:        int display, val;
1.13      tedu      478:        static const char *aday[] = {
1.1       deraadt   479:                "",
                    480:                " 1", " 2", " 3", " 4", " 5", " 6", " 7",
                    481:                " 8", " 9", "10", "11", "12", "13", "14",
                    482:                "15", "16", "17", "18", "19", "20", "21",
                    483:                "22", "23", "24", "25", "26", "27", "28",
                    484:                "29", "30", "31",
                    485:        };
                    486:
                    487:        if (day == SPACE) {
                    488:                memset(p, ' ', julian ? J_DAY_LEN : DAY_LEN);
                    489:                return;
                    490:        }
                    491:        if (julian) {
1.5       deraadt   492:                val = day / 100;
                    493:                if (val) {
1.1       deraadt   494:                        day %= 100;
                    495:                        *p++ = val + '0';
                    496:                        display = 1;
                    497:                } else {
                    498:                        *p++ = ' ';
                    499:                        display = 0;
                    500:                }
                    501:                val = day / 10;
                    502:                if (val || display)
                    503:                        *p++ = val + '0';
                    504:                else
                    505:                        *p++ = ' ';
                    506:                *p++ = day % 10 + '0';
                    507:        } else {
                    508:                *p++ = aday[day][0];
                    509:                *p++ = aday[day][1];
                    510:        }
                    511:        *p = ' ';
                    512: }
                    513:
                    514: void
1.10      deraadt   515: trim_trailing_spaces(char *s)
1.1       deraadt   516: {
                    517:        char *p;
                    518:
                    519:        for (p = s; *p; ++p)
                    520:                continue;
1.26      deraadt   521:        while (p > s && isspace((unsigned char)*--p))
1.1       deraadt   522:                continue;
                    523:        if (p > s)
                    524:                ++p;
                    525:        *p = '\0';
                    526: }
                    527:
                    528: void
1.13      tedu      529: center(const char *str, int len, int separate)
1.1       deraadt   530: {
                    531:
                    532:        len -= strlen(str);
1.21      tom       533:        (void)printf("%*s%s%*s", len / 2, "", str,
                    534:            len / 2 + len % 2 + separate, "");
1.1       deraadt   535: }
                    536:
                    537: void
1.10      deraadt   538: usage(void)
1.1       deraadt   539: {
                    540:
1.23      pyr       541:        (void)fprintf(stderr, "usage: cal [-jmwy] [month] [year]\n");
1.1       deraadt   542:        exit(1);
1.12      tedu      543: }
                    544:
                    545: int
                    546: parsemonth(const char *s)
                    547: {
1.15      deraadt   548:        struct tm tm;
                    549:        char *cp;
1.12      tedu      550:        int v;
                    551:
                    552:        v = (int)strtol(s, &cp, 10);
1.17      tom       553:        if (*cp != '\0') {              /* s wasn't purely numeric */
                    554:                v = 0;
                    555:                if ((cp = strptime(s, "%b", &tm)) != NULL && *cp == '\0')
                    556:                        v = tm.tm_mon + 1;
                    557:        }
1.15      deraadt   558:        if (v <= 0 || v > 12)
1.17      tom       559:                errx(1, "invalid month: use 1-12 or a name");
1.15      deraadt   560:        return (v);
1.1       deraadt   561: }