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

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