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

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