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

Annotation of src/usr.bin/calendar/day.c, Revision 1.37

1.37    ! millert     1: /*     $OpenBSD: day.c,v 1.36 2019/02/01 16:22:53 millert Exp $        */
1.1       millert     2:
                      3: /*
                      4:  * Copyright (c) 1989, 1993, 1994
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
1.15      millert    15:  * 3. Neither the name of the University nor the names of its contributors
1.1       millert    16:  *    may be used to endorse or promote products derived from this software
                     17:  *    without specific prior written permission.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     29:  * SUCH DAMAGE.
                     30:  */
                     31:
                     32: #include <sys/types.h>
                     33: #include <sys/uio.h>
                     34:
                     35: #include <ctype.h>
                     36: #include <err.h>
                     37: #include <locale.h>
                     38: #include <stdio.h>
                     39: #include <stdlib.h>
                     40: #include <string.h>
                     41: #include <time.h>
                     42:
                     43: #include "pathnames.h"
                     44: #include "calendar.h"
1.23      jsg        45:
                     46: extern struct iovec header[];
1.1       millert    47:
1.7       pjanzen    48: #define WEEKLY 1
                     49: #define MONTHLY 2
                     50: #define YEARLY 3
                     51:
1.1       millert    52: struct tm *tp;
1.7       pjanzen    53: int *cumdays, offset;
1.1       millert    54: char dayname[10];
1.16      mickey     55: enum calendars calendar;
                     56: u_long julian;
1.1       millert    57:
                     58:
                     59: /* 1-based month, 0-based days, cumulative */
                     60: int daytab[][14] = {
                     61:        { 0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 },
                     62:        { 0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
                     63: };
                     64:
                     65: static char *days[] = {
                     66:        "sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL,
                     67: };
                     68:
                     69: static char *months[] = {
                     70:        "jan", "feb", "mar", "apr", "may", "jun",
                     71:        "jul", "aug", "sep", "oct", "nov", "dec", NULL,
                     72: };
                     73:
                     74: static struct fixs fndays[8];         /* full national days names */
                     75: static struct fixs ndays[8];          /* short national days names */
                     76:
                     77: static struct fixs fnmonths[13];      /* full national months names */
                     78: static struct fixs nmonths[13];       /* short national month names */
                     79:
1.20      deraadt    80: void
1.31      espie      81: fill_print_date(struct match *m, struct tm *tm)
                     82: {
                     83:        if (strftime(m->print_date, sizeof(m->print_date),
                     84:            daynames ? "%a %b %d" : "%b %d", tm) == 0)
                     85:                m->print_date[sizeof(m->print_date) - 1] = '\0';
                     86: }
                     87:
                     88: void
1.20      deraadt    89: setnnames(void)
1.1       millert    90: {
                     91:        char buf[80];
                     92:        int i, l;
                     93:        struct tm tm;
                     94:
                     95:        for (i = 0; i < 7; i++) {
                     96:                tm.tm_wday = i;
1.3       millert    97:                l = strftime(buf, sizeof(buf), "%a", &tm);
1.27      deraadt    98:                for (; l > 0 && isspace((unsigned char)buf[l - 1]); l--)
1.1       millert    99:                        ;
                    100:                buf[l] = '\0';
1.32      mmcc      101:                free(ndays[i].name);
1.1       millert   102:                if ((ndays[i].name = strdup(buf)) == NULL)
1.11      pjanzen   103:                        err(1, NULL);
1.1       millert   104:                ndays[i].len = strlen(buf);
                    105:
1.3       millert   106:                l = strftime(buf, sizeof(buf), "%A", &tm);
1.27      deraadt   107:                for (; l > 0 && isspace((unsigned char)buf[l - 1]); l--)
1.1       millert   108:                        ;
                    109:                buf[l] = '\0';
1.32      mmcc      110:                free(fndays[i].name);
1.1       millert   111:                if ((fndays[i].name = strdup(buf)) == NULL)
1.11      pjanzen   112:                        err(1, NULL);
1.1       millert   113:                fndays[i].len = strlen(buf);
                    114:        }
                    115:
                    116:        for (i = 0; i < 12; i++) {
                    117:                tm.tm_mon = i;
1.3       millert   118:                l = strftime(buf, sizeof(buf), "%b", &tm);
1.27      deraadt   119:                for (; l > 0 && isspace((unsigned char)buf[l - 1]); l--)
1.1       millert   120:                        ;
                    121:                buf[l] = '\0';
1.32      mmcc      122:                free(nmonths[i].name);
1.1       millert   123:                if ((nmonths[i].name = strdup(buf)) == NULL)
1.11      pjanzen   124:                        err(1, NULL);
1.1       millert   125:                nmonths[i].len = strlen(buf);
                    126:
1.3       millert   127:                l = strftime(buf, sizeof(buf), "%B", &tm);
1.27      deraadt   128:                for (; l > 0 && isspace((unsigned char)buf[l - 1]); l--)
1.1       millert   129:                        ;
                    130:                buf[l] = '\0';
1.32      mmcc      131:                free(fnmonths[i].name);
1.1       millert   132:                if ((fnmonths[i].name = strdup(buf)) == NULL)
1.11      pjanzen   133:                        err(1, NULL);
1.1       millert   134:                fnmonths[i].len = strlen(buf);
                    135:        }
1.7       pjanzen   136:        /* Hardwired special events */
1.18      mickey    137:        spev[0].name = strdup(PESACH);
                    138:        spev[0].nlen = PESACHLEN;
                    139:        spev[0].getev = pesach;
                    140:        spev[1].name = strdup(EASTER);
                    141:        spev[1].nlen = EASTERNAMELEN;
                    142:        spev[1].getev = easter;
                    143:        spev[2].name = strdup(PASKHA);
                    144:        spev[2].nlen = PASKHALEN;
                    145:        spev[2].getev = paskha;
1.7       pjanzen   146:        for (i = 0; i < NUMEV; i++) {
                    147:                if (spev[i].name == NULL)
1.11      pjanzen   148:                        err(1, NULL);
1.7       pjanzen   149:                spev[i].uname = NULL;
                    150:        }
1.1       millert   151: }
                    152:
                    153: void
1.20      deraadt   154: settime(time_t *now)
1.1       millert   155: {
1.7       pjanzen   156:        tp = localtime(now);
                    157:        tp->tm_sec = 0;
                    158:        tp->tm_min = 0;
                    159:        /* Avoid getting caught by a timezone shift; set time to noon */
                    160:        tp->tm_isdst = 0;
                    161:        tp->tm_hour = 12;
                    162:        *now = mktime(tp);
1.29      millert   163:        if (isleap(tp->tm_year + 1900))
1.1       millert   164:                cumdays = daytab[1];
1.7       pjanzen   165:        else
1.1       millert   166:                cumdays = daytab[0];
                    167:        /* Friday displays Monday's events */
                    168:        offset = tp->tm_wday == 5 ? 3 : 1;
1.36      millert   169:        if (f_Setday)
1.7       pjanzen   170:                offset = 0;     /* Except not when range is set explicitly */
1.1       millert   171:        header[5].iov_base = dayname;
                    172:
                    173:        (void) setlocale(LC_TIME, "C");
                    174:        header[5].iov_len = strftime(dayname, sizeof(dayname), "%A", tp);
                    175:        (void) setlocale(LC_TIME, "");
                    176:
                    177:        setnnames();
                    178: }
                    179:
1.2       millert   180: /* convert [Year][Month]Day into unix time (since 1970)
                    181:  * Year: two or four digits, Month: two digits, Day: two digits
1.1       millert   182:  */
1.20      deraadt   183: time_t
                    184: Mktime(char *date)
1.1       millert   185: {
1.20      deraadt   186:        time_t t;
                    187:        int len;
                    188:        struct tm tm;
1.1       millert   189:
1.20      deraadt   190:        (void)time(&t);
                    191:        tp = localtime(&t);
                    192:
                    193:        len = strlen(date);
                    194:        if (len < 2)
                    195:                return((time_t)-1);
1.25      deraadt   196:        bzero(&tm, sizeof tm);
1.20      deraadt   197:        tm.tm_sec = 0;
                    198:        tm.tm_min = 0;
                    199:        /* Avoid getting caught by a timezone shift; set time to noon */
                    200:        tm.tm_isdst = 0;
                    201:        tm.tm_hour = 12;
                    202:        tm.tm_wday = 0;
                    203:        tm.tm_mday = tp->tm_mday;
                    204:        tm.tm_mon = tp->tm_mon;
                    205:        tm.tm_year = tp->tm_year;
                    206:
                    207:        /* Day */
                    208:        tm.tm_mday = atoi(date + len - 2);
                    209:
                    210:        /* Month */
                    211:        if (len >= 4) {
                    212:                *(date + len - 2) = '\0';
                    213:                tm.tm_mon = atoi(date + len - 4) - 1;
                    214:        }
                    215:
                    216:        /* Year */
                    217:        if (len >= 6) {
1.7       pjanzen   218:                *(date + len - 4) = '\0';
                    219:                tm.tm_year = atoi(date);
1.1       millert   220:
1.20      deraadt   221:                if (tm.tm_year < 69)            /* Y2K */
1.29      millert   222:                        tm.tm_year += 100;
                    223:                else if (tm.tm_year > 1900)
                    224:                        tm.tm_year -= 1900;
1.20      deraadt   225:        }
1.1       millert   226:
                    227: #if DEBUG
1.24      deraadt   228:        printf("Mktime: %d %lld %d %s\n", (int)mktime(&tm), (long long)t, len,
1.20      deraadt   229:            asctime(&tm));
1.1       millert   230: #endif
1.20      deraadt   231:        return(mktime(&tm));
1.1       millert   232: }
                    233:
1.26      deraadt   234: static void
1.16      mickey    235: adjust_calendar(int *day, int *month)
                    236: {
                    237:        switch (calendar) {
                    238:        case GREGORIAN:
                    239:                break;
                    240:
                    241:        case JULIAN:
                    242:                *day += julian;
                    243:                if (*day > (cumdays[*month + 1] - cumdays[*month])) {
                    244:                        *day -= (cumdays[*month + 1] - cumdays[*month]);
                    245:                        if (++*month > 12)
                    246:                                *month = 1;
                    247:                }
                    248:                break;
                    249:        case LUNAR:
                    250:                break;
                    251:        }
                    252: }
                    253:
1.1       millert   254: /*
                    255:  * Possible date formats include any combination of:
                    256:  *     3-charmonth                     (January, Jan, Jan)
                    257:  *     3-charweekday                   (Friday, Monday, mon.)
                    258:  *     numeric month or day            (1, 2, 04)
                    259:  *
1.10      pjanzen   260:  * Any character except \t or '*' may separate them, or they may not be
                    261:  * separated.  Any line following a line that is matched, that starts
                    262:  * with \t, is shown along with the matched line.
1.1       millert   263:  */
1.6       pjanzen   264: struct match *
1.20      deraadt   265: isnow(char *endp, int bodun)
1.1       millert   266: {
1.7       pjanzen   267:        int day = 0, flags = 0, month = 0, v1, v2, i;
                    268:        int monthp, dayp, varp = 0;
                    269:        struct match *matches = NULL, *tmp, *tmp2;
                    270:        int interval = YEARLY;  /* how frequently the event repeats. */
                    271:        int vwd = 0;    /* Variable weekday */
                    272:        time_t tdiff, ttmp;
                    273:        struct tm tmtmp;
1.1       millert   274:
                    275:        /*
                    276:         * CONVENTION
                    277:         *
                    278:         * Month:     1-12
                    279:         * Monthname: Jan .. Dec
                    280:         * Day:       1-31
                    281:         * Weekday:   Mon-Sun
                    282:         *
                    283:         */
                    284:
                    285:        /* read first field */
                    286:        /* didn't recognize anything, skip it */
                    287:        if (!(v1 = getfield(endp, &endp, &flags)))
1.6       pjanzen   288:                return (NULL);
1.1       millert   289:
1.12      mickey    290:        /* adjust bodun rate */
                    291:        if (bodun && !bodun_always)
1.21      djm       292:                bodun = !arc4random_uniform(3);
1.30      zhuk      293:
1.1       millert   294:        /* Easter or Easter depending days */
1.7       pjanzen   295:        if (flags & F_SPECIAL)
                    296:                vwd = v1;
1.1       millert   297:
                    298:         /*
                    299:          * 1. {Weekday,Day} XYZ ...
                    300:          *
                    301:          *    where Day is > 12
                    302:          */
                    303:        else if (flags & F_ISDAY || v1 > 12) {
                    304:
1.7       pjanzen   305:                /* found a day; day: 13-31 or weekday: 1-7 */
1.1       millert   306:                day = v1;
                    307:
                    308:                /* {Day,Weekday} {Month,Monthname} ... */
1.7       pjanzen   309:                /* if no recognizable month, assume just a day alone -- this is
                    310:                 * very unlikely and can only happen after the first 12 days.
                    311:                 * --find month or use current month */
                    312:                if (!(month = getfield(endp, &endp, &flags))) {
1.1       millert   313:                        month = tp->tm_mon + 1;
1.7       pjanzen   314:                        /* F_ISDAY is set only if a weekday was spelled out */
                    315:                        /* F_ISDAY must be set if 0 < day < 8 */
                    316:                        if ((day <= 7) && (day >= 1))
                    317:                                interval = WEEKLY;
                    318:                        else
                    319:                                interval = MONTHLY;
                    320:                } else if ((day <= 7) && (day >= 1))
                    321:                        day += 10;
                    322:                        /* it's a weekday; make it the first one of the month */
                    323:                if (month == -1) {
                    324:                        month = tp->tm_mon + 1;
                    325:                        interval = MONTHLY;
1.37    ! millert   326:                } else {
        !           327:                        if ((month > 12) || (month < 1))
        !           328:                                return (NULL);
        !           329:                        if (calendar)
        !           330:                                adjust_calendar(&day, &month);
        !           331:                }
1.1       millert   332:        }
                    333:
                    334:        /* 2. {Monthname} XYZ ... */
                    335:        else if (flags & F_ISMONTH) {
                    336:                month = v1;
1.7       pjanzen   337:                if (month == -1) {
                    338:                        month = tp->tm_mon + 1;
                    339:                        interval = MONTHLY;
                    340:                }
1.1       millert   341:                /* Monthname {day,weekday} */
                    342:                /* if no recognizable day, assume the first day in month */
                    343:                if (!(day = getfield(endp, &endp, &flags)))
                    344:                        day = 1;
1.7       pjanzen   345:                /* If a weekday was spelled out without an ordering,
                    346:                 * assume the first of that day in the month */
1.16      mickey    347:                if ((flags & F_ISDAY)) {
                    348:                        if ((day >= 1) && (day <=7))
                    349:                                day += 10;
                    350:                } else if (calendar)
                    351:                        adjust_calendar(&day, &month);
1.1       millert   352:        }
                    353:
                    354:        /* Hm ... */
                    355:        else {
                    356:                v2 = getfield(endp, &endp, &flags);
                    357:
                    358:                /*
                    359:                 * {Day} {Monthname} ...
                    360:                 * where Day <= 12
                    361:                 */
                    362:                if (flags & F_ISMONTH) {
                    363:                        day = v1;
                    364:                        month = v2;
1.7       pjanzen   365:                        if (month == -1) {
                    366:                                month = tp->tm_mon + 1;
                    367:                                interval = MONTHLY;
1.16      mickey    368:                        } else if (calendar)
                    369:                                adjust_calendar(&day, &month);
1.1       millert   370:                }
                    371:
                    372:                /* {Month} {Weekday,Day} ...  */
                    373:                else {
                    374:                        /* F_ISDAY set, v2 > 12, or no way to tell */
                    375:                        month = v1;
1.37    ! millert   376:                        if ((month > 12) || (month < 1))
        !           377:                                return (NULL);
1.1       millert   378:                        /* if no recognizable day, assume the first */
                    379:                        day = v2 ? v2 : 1;
1.16      mickey    380:                        if ((flags & F_ISDAY)) {
                    381:                                if ((day >= 1) && (day <= 7))
                    382:                                        day += 10;
                    383:                        } else
                    384:                                adjust_calendar(&day, &month);
1.1       millert   385:                }
                    386:        }
                    387:
                    388:        /* convert Weekday into *next*  Day,
                    389:         * e.g.: 'Sunday' -> 22
1.5       pjanzen   390:         *       'SundayLast' -> ??
1.1       millert   391:         */
                    392:        if (flags & F_ISDAY) {
                    393: #if DEBUG
1.7       pjanzen   394:                fprintf(stderr, "\nday: %d %s month %d\n", day, endp, month);
1.1       millert   395: #endif
                    396:
1.7       pjanzen   397:                varp = 1;
                    398:                /* variable weekday, SundayLast, MondayFirst ... */
                    399:                if (day < 0 || day >= 10)
                    400:                        vwd = day;
                    401:                else {
                    402:                        day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
                    403:                        interval = WEEKLY;
                    404:                }
                    405:        } else
                    406:        /* Check for silliness.  Note we still catch Feb 29 */
                    407:                if (!(flags & F_SPECIAL) &&
                    408:                    (day > (cumdays[month + 1] - cumdays[month]) || day < 1)) {
                    409:                        if (!((month == 2 && day == 29) ||
                    410:                            (interval == MONTHLY && day <= 31)))
                    411:                                return (NULL);
1.1       millert   412:                }
                    413:
1.7       pjanzen   414:        if (!(flags & F_SPECIAL)) {
                    415:                monthp = month;
                    416:                dayp = day;
                    417:                day = cumdays[month] + day;
1.5       pjanzen   418: #if DEBUG
1.7       pjanzen   419:                fprintf(stderr, "day2: day %d(%d) yday %d\n", dayp, day, tp->tm_yday);
1.5       pjanzen   420: #endif
1.7       pjanzen   421:        /* Speed up processing for the most common situation:  yearly events
                    422:         * when the interval being checked is less than a month or so (this
                    423:         * could be less than a year, but then we have to start worrying about
                    424:         * leap years).  Only one event can match, and it's easy to find.
                    425:         * Note we can't check special events, because they can wander widely.
                    426:         */
                    427:                if (((v1 = offset + f_dayAfter) < 50) && (interval == YEARLY)) {
                    428:                        memcpy(&tmtmp, tp, sizeof(struct tm));
                    429:                        tmtmp.tm_mday = dayp;
                    430:                        tmtmp.tm_mon = monthp - 1;
1.8       pjanzen   431:                        if (vwd) {
                    432:                        /* We want the event next year if it's late now
                    433:                         * this year.  The 50-day limit means we don't have to
                    434:                         * worry if next year is or isn't a leap year.
                    435:                         */
                    436:                                if (tp->tm_yday > 300 && tmtmp.tm_mon <= 1)
                    437:                                        variable_weekday(&vwd, tmtmp.tm_mon + 1,
1.29      millert   438:                                            tmtmp.tm_year + 1900 + 1);
1.8       pjanzen   439:                                else
                    440:                                        variable_weekday(&vwd, tmtmp.tm_mon + 1,
1.29      millert   441:                                            tmtmp.tm_year + 1900);
1.8       pjanzen   442:                                day = cumdays[tmtmp.tm_mon + 1] + vwd;
                    443:                                tmtmp.tm_mday = vwd;
                    444:                        }
1.7       pjanzen   445:                        v2 = day - tp->tm_yday;
                    446:                        if ((v2 > v1) || (v2 < 0)) {
1.29      millert   447:                                if ((v2 += isleap(tp->tm_year + 1900) ? 366 : 365)
1.7       pjanzen   448:                                    <= v1)
                    449:                                        tmtmp.tm_year++;
1.12      mickey    450:                                else if(!bodun || (day - tp->tm_yday) != -1)
1.7       pjanzen   451:                                        return(NULL);
                    452:                        }
                    453:                        if ((tmp = malloc(sizeof(struct match))) == NULL)
1.11      pjanzen   454:                                err(1, NULL);
1.13      mickey    455:
                    456:                        if (bodun && (day - tp->tm_yday) == -1) {
                    457:                                tmp->when = f_time - 1 * SECSPERDAY;
                    458:                                tmtmp.tm_mday++;
                    459:                                tmp->bodun = 1;
                    460:                        } else {
                    461:                                tmp->when = f_time + v2 * SECSPERDAY;
                    462:                                tmp->bodun = 0;
                    463:                        }
                    464:
1.7       pjanzen   465:                        (void)mktime(&tmtmp);
1.31      espie     466:                        fill_print_date(tmp, &tmtmp);
1.7       pjanzen   467:                        tmp->var   = varp;
                    468:                        tmp->next  = NULL;
                    469:                        return(tmp);
                    470:                }
1.16      mickey    471:        } else {
1.7       pjanzen   472:                varp = 1;
                    473:                /* Set up v1 to the event number and ... */
                    474:                v1 = vwd % (NUMEV + 1) - 1;
                    475:                vwd /= (NUMEV + 1);
                    476:                if (v1 < 0) {
                    477:                        v1 += NUMEV + 1;
                    478:                        vwd--;
                    479:                }
                    480:                dayp = monthp = 1;      /* Why not */
1.1       millert   481:        }
                    482:
1.7       pjanzen   483:        /* Compare to past and coming instances of the event.  The i == 0 part
                    484:         * of the loop corresponds to this specific instance.  Note that we
                    485:         * can leave things sort of higgledy-piggledy since a mktime() happens
                    486:         * on this before anything gets printed.  Also note that even though
                    487:         * we've effectively gotten rid of f_dayBefore, we still have to check
                    488:         * the one prior event for situations like "the 31st of every month"
                    489:         * and "yearly" events which could happen twice in one year but not in
                    490:         * the next */
                    491:        tmp2 = matches;
                    492:        for (i = -1; i < 2; i++) {
                    493:                memcpy(&tmtmp, tp, sizeof(struct tm));
                    494:                tmtmp.tm_mday = dayp;
                    495:                tmtmp.tm_mon = month = monthp - 1;
                    496:                do {
                    497:                        v2 = 0;
                    498:                        switch (interval) {
                    499:                        case WEEKLY:
                    500:                                tmtmp.tm_mday += 7 * i;
                    501:                                break;
                    502:                        case MONTHLY:
                    503:                                month += i;
                    504:                                tmtmp.tm_mon = month;
                    505:                                switch(tmtmp.tm_mon) {
                    506:                                case -1:
                    507:                                        tmtmp.tm_mon = month = 11;
                    508:                                        tmtmp.tm_year--;
                    509:                                        break;
                    510:                                case 12:
                    511:                                        tmtmp.tm_mon = month = 0;
                    512:                                        tmtmp.tm_year++;
                    513:                                        break;
                    514:                                }
                    515:                                if (vwd) {
                    516:                                        v1 = vwd;
                    517:                                        variable_weekday(&v1, tmtmp.tm_mon + 1,
1.29      millert   518:                                            tmtmp.tm_year + 1900);
1.7       pjanzen   519:                                        tmtmp.tm_mday = v1;
                    520:                                } else
                    521:                                        tmtmp.tm_mday = dayp;
                    522:                                break;
                    523:                        case YEARLY:
                    524:                        default:
                    525:                                tmtmp.tm_year += i;
                    526:                                if (flags & F_SPECIAL) {
                    527:                                        tmtmp.tm_mon = 0;       /* Gee, mktime() is nice */
                    528:                                        tmtmp.tm_mday = spev[v1].getev(tmtmp.tm_year +
1.29      millert   529:                                            1900) + vwd;
1.7       pjanzen   530:                                } else if (vwd) {
                    531:                                        v1 = vwd;
                    532:                                        variable_weekday(&v1, tmtmp.tm_mon + 1,
1.29      millert   533:                                            tmtmp.tm_year + 1900);
1.7       pjanzen   534:                                        tmtmp.tm_mday = v1;
                    535:                                } else {
                    536:                                /* Need the following to keep Feb 29 from
                    537:                                 * becoming Mar 1 */
                    538:                                tmtmp.tm_mday = dayp;
                    539:                                tmtmp.tm_mon = monthp - 1;
                    540:                                }
                    541:                                break;
                    542:                        }
                    543:                        /* How many days apart are we */
                    544:                        if ((ttmp = mktime(&tmtmp)) == -1)
                    545:                                warnx("time out of range: %s", endp);
                    546:                        else {
                    547:                                tdiff = difftime(ttmp, f_time)/ SECSPERDAY;
1.12      mickey    548:                                if (tdiff <= offset + f_dayAfter ||
                    549:                                    (bodun && tdiff == -1)) {
1.34      millert   550:                                        if (((tmtmp.tm_mon == month) ||
                    551:                                             (flags & F_SPECIAL) ||
                    552:                                             (interval == WEEKLY)) &&
1.33      millert   553:                                            (tdiff >=  0 ||
                    554:                                            (bodun && tdiff == -1))) {
1.7       pjanzen   555:                                        if ((tmp = malloc(sizeof(struct match))) == NULL)
1.11      pjanzen   556:                                                err(1, NULL);
1.7       pjanzen   557:                                        tmp->when = ttmp;
1.31      espie     558:                                        fill_print_date(tmp, &tmtmp);
1.13      mickey    559:                                        tmp->bodun = bodun && tdiff == -1;
1.7       pjanzen   560:                                        tmp->var   = varp;
                    561:                                        tmp->next  = NULL;
                    562:                                        if (tmp2)
                    563:                                                tmp2->next = tmp;
                    564:                                        else
                    565:                                                matches = tmp;
                    566:                                        tmp2 = tmp;
                    567:                                        v2 = (i == 1) ? 1 : 0;
                    568:                                        }
                    569:                                } else
                    570:                                        i = 2; /* No point checking in the future */
                    571:                        }
                    572:                } while (v2 != 0);
1.6       pjanzen   573:        }
1.7       pjanzen   574:        return (matches);
1.1       millert   575: }
                    576:
                    577:
                    578: int
1.20      deraadt   579: getmonth(char *s)
1.1       millert   580: {
1.14      mpech     581:        char **p;
1.1       millert   582:        struct fixs *n;
                    583:
                    584:        for (n = fnmonths; n->name; ++n)
                    585:                if (!strncasecmp(s, n->name, n->len))
                    586:                        return ((n - fnmonths) + 1);
                    587:        for (n = nmonths; n->name; ++n)
                    588:                if (!strncasecmp(s, n->name, n->len))
                    589:                        return ((n - nmonths) + 1);
                    590:        for (p = months; *p; ++p)
                    591:                if (!strncasecmp(s, *p, 3))
                    592:                        return ((p - months) + 1);
                    593:        return (0);
                    594: }
                    595:
                    596:
                    597: int
1.20      deraadt   598: getday(char *s)
1.1       millert   599: {
1.14      mpech     600:        char **p;
1.1       millert   601:        struct fixs *n;
                    602:
                    603:        for (n = fndays; n->name; ++n)
                    604:                if (!strncasecmp(s, n->name, n->len))
                    605:                        return ((n - fndays) + 1);
                    606:        for (n = ndays; n->name; ++n)
                    607:                if (!strncasecmp(s, n->name, n->len))
                    608:                        return ((n - ndays) + 1);
                    609:        for (p = days; *p; ++p)
                    610:                if (!strncasecmp(s, *p, 3))
                    611:                        return ((p - days) + 1);
                    612:        return (0);
                    613: }
                    614:
                    615: /* return offset for variable weekdays
                    616:  * -1 -> last weekday in month
                    617:  * +1 -> first weekday in month
                    618:  * ... etc ...
                    619:  */
                    620: int
1.20      deraadt   621: getdayvar(char *s)
1.1       millert   622: {
1.14      mpech     623:        int offset;
1.1       millert   624:
                    625:
                    626:        offset = strlen(s);
                    627:
                    628:        /* Sun+1 or Wednesday-2
                    629:         *    ^              ^   */
                    630:
                    631:        /* printf ("x: %s %s %d\n", s, s + offset - 2, offset); */
                    632:        switch(*(s + offset - 2)) {
                    633:        case '-':
                    634:        case '+':
1.7       pjanzen   635:            return(atoi(s + offset - 2));
1.1       millert   636:            break;
                    637:        }
                    638:
                    639:        /*
                    640:         * some aliases: last, first, second, third, fourth
                    641:         */
                    642:
                    643:        /* last */
                    644:        if      (offset > 4 && !strcasecmp(s + offset - 4, "last"))
                    645:            return(-1);
                    646:        else if (offset > 5 && !strcasecmp(s + offset - 5, "first"))
                    647:            return(+1);
                    648:        else if (offset > 6 && !strcasecmp(s + offset - 6, "second"))
                    649:            return(+2);
                    650:        else if (offset > 5 && !strcasecmp(s + offset - 5, "third"))
                    651:            return(+3);
                    652:        else if (offset > 6 && !strcasecmp(s + offset - 6, "fourth"))
                    653:            return(+4);
                    654:
                    655:        /* no offset detected */
                    656:        return(0);
1.7       pjanzen   657: }
                    658:
                    659:
                    660: int
1.20      deraadt   661: foy(int year)
1.7       pjanzen   662: {
                    663:        /* 0-6; what weekday Jan 1 is */
                    664:        year--;
                    665:        return ((1 - year/100 + year/400 + (int)(365.25 * year)) % 7);
                    666: }
                    667:
                    668:
                    669:
                    670: void
1.20      deraadt   671: variable_weekday(int *day, int month, int year)
1.7       pjanzen   672: {
                    673:        int v1, v2;
                    674:        int *cumdays;
                    675:        int day1;
                    676:
                    677:        if (isleap(year))
                    678:                cumdays = daytab[1];
                    679:        else
                    680:                cumdays = daytab[0];
                    681:        day1 = foy(year);
                    682:        /* negative offset; last, -4 .. -1 */
                    683:        if (*day < 0) {
                    684:                v1 = *day/10 - 1;          /* offset -4 ... -1 */
                    685:                *day = 10 + (*day % 10);    /* day 1 ... 7 */
                    686:
                    687:                /* which weekday the end of the month is (1-7) */
                    688:                v2 = (cumdays[month + 1] + day1) % 7 + 1;
                    689:
                    690:                /* and subtract enough days */
                    691:                *day = cumdays[month + 1] - cumdays[month] +
                    692:                    (v1 + 1) * 7 - (v2 - *day + 7) % 7;
                    693: #if DEBUG
                    694:                fprintf(stderr, "\nMonth %d ends on weekday %d\n", month, v2);
                    695: #endif
                    696:        }
                    697:
                    698:        /* first, second ... +1 ... +5 */
                    699:        else {
                    700:                v1 = *day/10;        /* offset */
                    701:                *day = *day % 10;
                    702:
                    703:                /* which weekday the first of the month is (1-7) */
                    704:                v2 = (cumdays[month] + 1 + day1) % 7 + 1;
                    705:
                    706:                /* and add enough days */
                    707:                *day = 1 + (v1 - 1) * 7 + (*day - v2 + 7) % 7;
                    708: #if DEBUG
                    709:                fprintf(stderr, "\nMonth %d starts on weekday %d\n", month, v2);
                    710: #endif
                    711:        }
1.1       millert   712: }