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

1.31    ! espie       1: /*     $OpenBSD: day.c,v 1.30 2015/10/23 11:43:16 zhuk 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';
                    101:                if (ndays[i].name != NULL)
                    102:                        free(ndays[i].name);
                    103:                if ((ndays[i].name = strdup(buf)) == NULL)
1.11      pjanzen   104:                        err(1, NULL);
1.1       millert   105:                ndays[i].len = strlen(buf);
                    106:
1.3       millert   107:                l = strftime(buf, sizeof(buf), "%A", &tm);
1.27      deraadt   108:                for (; l > 0 && isspace((unsigned char)buf[l - 1]); l--)
1.1       millert   109:                        ;
                    110:                buf[l] = '\0';
                    111:                if (fndays[i].name != NULL)
                    112:                        free(fndays[i].name);
                    113:                if ((fndays[i].name = strdup(buf)) == NULL)
1.11      pjanzen   114:                        err(1, NULL);
1.1       millert   115:                fndays[i].len = strlen(buf);
                    116:        }
                    117:
                    118:        for (i = 0; i < 12; i++) {
                    119:                tm.tm_mon = i;
1.3       millert   120:                l = strftime(buf, sizeof(buf), "%b", &tm);
1.27      deraadt   121:                for (; l > 0 && isspace((unsigned char)buf[l - 1]); l--)
1.1       millert   122:                        ;
                    123:                buf[l] = '\0';
                    124:                if (nmonths[i].name != NULL)
                    125:                        free(nmonths[i].name);
                    126:                if ((nmonths[i].name = strdup(buf)) == NULL)
1.11      pjanzen   127:                        err(1, NULL);
1.1       millert   128:                nmonths[i].len = strlen(buf);
                    129:
1.3       millert   130:                l = strftime(buf, sizeof(buf), "%B", &tm);
1.27      deraadt   131:                for (; l > 0 && isspace((unsigned char)buf[l - 1]); l--)
1.1       millert   132:                        ;
                    133:                buf[l] = '\0';
                    134:                if (fnmonths[i].name != NULL)
                    135:                        free(fnmonths[i].name);
                    136:                if ((fnmonths[i].name = strdup(buf)) == NULL)
1.11      pjanzen   137:                        err(1, NULL);
1.1       millert   138:                fnmonths[i].len = strlen(buf);
                    139:        }
1.7       pjanzen   140:        /* Hardwired special events */
1.18      mickey    141:        spev[0].name = strdup(PESACH);
                    142:        spev[0].nlen = PESACHLEN;
                    143:        spev[0].getev = pesach;
                    144:        spev[1].name = strdup(EASTER);
                    145:        spev[1].nlen = EASTERNAMELEN;
                    146:        spev[1].getev = easter;
                    147:        spev[2].name = strdup(PASKHA);
                    148:        spev[2].nlen = PASKHALEN;
                    149:        spev[2].getev = paskha;
1.7       pjanzen   150:        for (i = 0; i < NUMEV; i++) {
                    151:                if (spev[i].name == NULL)
1.11      pjanzen   152:                        err(1, NULL);
1.7       pjanzen   153:                spev[i].uname = NULL;
                    154:        }
1.1       millert   155: }
                    156:
                    157: void
1.20      deraadt   158: settime(time_t *now)
1.1       millert   159: {
1.7       pjanzen   160:        tp = localtime(now);
                    161:        tp->tm_sec = 0;
                    162:        tp->tm_min = 0;
                    163:        /* Avoid getting caught by a timezone shift; set time to noon */
                    164:        tp->tm_isdst = 0;
                    165:        tp->tm_hour = 12;
                    166:        *now = mktime(tp);
1.29      millert   167:        if (isleap(tp->tm_year + 1900))
1.1       millert   168:                cumdays = daytab[1];
1.7       pjanzen   169:        else
1.1       millert   170:                cumdays = daytab[0];
                    171:        /* Friday displays Monday's events */
                    172:        offset = tp->tm_wday == 5 ? 3 : 1;
1.19      mickey    173:        if (f_SetdayAfter)
1.7       pjanzen   174:                offset = 0;     /* Except not when range is set explicitly */
1.1       millert   175:        header[5].iov_base = dayname;
                    176:
                    177:        (void) setlocale(LC_TIME, "C");
                    178:        header[5].iov_len = strftime(dayname, sizeof(dayname), "%A", tp);
                    179:        (void) setlocale(LC_TIME, "");
                    180:
                    181:        setnnames();
                    182: }
                    183:
1.2       millert   184: /* convert [Year][Month]Day into unix time (since 1970)
                    185:  * Year: two or four digits, Month: two digits, Day: two digits
1.1       millert   186:  */
1.20      deraadt   187: time_t
                    188: Mktime(char *date)
1.1       millert   189: {
1.20      deraadt   190:        time_t t;
                    191:        int len;
                    192:        struct tm tm;
1.1       millert   193:
1.20      deraadt   194:        (void)time(&t);
                    195:        tp = localtime(&t);
                    196:
                    197:        len = strlen(date);
                    198:        if (len < 2)
                    199:                return((time_t)-1);
1.25      deraadt   200:        bzero(&tm, sizeof tm);
1.20      deraadt   201:        tm.tm_sec = 0;
                    202:        tm.tm_min = 0;
                    203:        /* Avoid getting caught by a timezone shift; set time to noon */
                    204:        tm.tm_isdst = 0;
                    205:        tm.tm_hour = 12;
                    206:        tm.tm_wday = 0;
                    207:        tm.tm_mday = tp->tm_mday;
                    208:        tm.tm_mon = tp->tm_mon;
                    209:        tm.tm_year = tp->tm_year;
                    210:
                    211:        /* Day */
                    212:        tm.tm_mday = atoi(date + len - 2);
                    213:
                    214:        /* Month */
                    215:        if (len >= 4) {
                    216:                *(date + len - 2) = '\0';
                    217:                tm.tm_mon = atoi(date + len - 4) - 1;
                    218:        }
                    219:
                    220:        /* Year */
                    221:        if (len >= 6) {
1.7       pjanzen   222:                *(date + len - 4) = '\0';
                    223:                tm.tm_year = atoi(date);
1.1       millert   224:
1.20      deraadt   225:                if (tm.tm_year < 69)            /* Y2K */
1.29      millert   226:                        tm.tm_year += 100;
                    227:                else if (tm.tm_year > 1900)
                    228:                        tm.tm_year -= 1900;
1.20      deraadt   229:        }
1.1       millert   230:
                    231: #if DEBUG
1.24      deraadt   232:        printf("Mktime: %d %lld %d %s\n", (int)mktime(&tm), (long long)t, len,
1.20      deraadt   233:            asctime(&tm));
1.1       millert   234: #endif
1.20      deraadt   235:        return(mktime(&tm));
1.1       millert   236: }
                    237:
1.26      deraadt   238: static void
1.16      mickey    239: adjust_calendar(int *day, int *month)
                    240: {
                    241:        switch (calendar) {
                    242:        case GREGORIAN:
                    243:                break;
                    244:
                    245:        case JULIAN:
                    246:                *day += julian;
                    247:                if (*day > (cumdays[*month + 1] - cumdays[*month])) {
                    248:                        *day -= (cumdays[*month + 1] - cumdays[*month]);
                    249:                        if (++*month > 12)
                    250:                                *month = 1;
                    251:                }
                    252:                break;
                    253:        case LUNAR:
                    254:                break;
                    255:        }
                    256: }
                    257:
1.1       millert   258: /*
                    259:  * Possible date formats include any combination of:
                    260:  *     3-charmonth                     (January, Jan, Jan)
                    261:  *     3-charweekday                   (Friday, Monday, mon.)
                    262:  *     numeric month or day            (1, 2, 04)
                    263:  *
1.10      pjanzen   264:  * Any character except \t or '*' may separate them, or they may not be
                    265:  * separated.  Any line following a line that is matched, that starts
                    266:  * with \t, is shown along with the matched line.
1.1       millert   267:  */
1.6       pjanzen   268: struct match *
1.20      deraadt   269: isnow(char *endp, int bodun)
1.1       millert   270: {
1.7       pjanzen   271:        int day = 0, flags = 0, month = 0, v1, v2, i;
                    272:        int monthp, dayp, varp = 0;
                    273:        struct match *matches = NULL, *tmp, *tmp2;
                    274:        int interval = YEARLY;  /* how frequently the event repeats. */
                    275:        int vwd = 0;    /* Variable weekday */
                    276:        time_t tdiff, ttmp;
                    277:        struct tm tmtmp;
1.1       millert   278:
                    279:        /*
                    280:         * CONVENTION
                    281:         *
                    282:         * Month:     1-12
                    283:         * Monthname: Jan .. Dec
                    284:         * Day:       1-31
                    285:         * Weekday:   Mon-Sun
                    286:         *
                    287:         */
                    288:
                    289:        /* read first field */
                    290:        /* didn't recognize anything, skip it */
                    291:        if (!(v1 = getfield(endp, &endp, &flags)))
1.6       pjanzen   292:                return (NULL);
1.1       millert   293:
1.12      mickey    294:        /* adjust bodun rate */
                    295:        if (bodun && !bodun_always)
1.21      djm       296:                bodun = !arc4random_uniform(3);
1.30      zhuk      297:
1.1       millert   298:        /* Easter or Easter depending days */
1.7       pjanzen   299:        if (flags & F_SPECIAL)
                    300:                vwd = v1;
1.1       millert   301:
                    302:         /*
                    303:          * 1. {Weekday,Day} XYZ ...
                    304:          *
                    305:          *    where Day is > 12
                    306:          */
                    307:        else if (flags & F_ISDAY || v1 > 12) {
                    308:
1.7       pjanzen   309:                /* found a day; day: 13-31 or weekday: 1-7 */
1.1       millert   310:                day = v1;
                    311:
                    312:                /* {Day,Weekday} {Month,Monthname} ... */
1.7       pjanzen   313:                /* if no recognizable month, assume just a day alone -- this is
                    314:                 * very unlikely and can only happen after the first 12 days.
                    315:                 * --find month or use current month */
                    316:                if (!(month = getfield(endp, &endp, &flags))) {
1.1       millert   317:                        month = tp->tm_mon + 1;
1.7       pjanzen   318:                        /* F_ISDAY is set only if a weekday was spelled out */
                    319:                        /* F_ISDAY must be set if 0 < day < 8 */
                    320:                        if ((day <= 7) && (day >= 1))
                    321:                                interval = WEEKLY;
                    322:                        else
                    323:                                interval = MONTHLY;
                    324:                } else if ((day <= 7) && (day >= 1))
                    325:                        day += 10;
                    326:                        /* it's a weekday; make it the first one of the month */
                    327:                if (month == -1) {
                    328:                        month = tp->tm_mon + 1;
                    329:                        interval = MONTHLY;
1.16      mickey    330:                } else if (calendar)
                    331:                        adjust_calendar(&day, &month);
1.7       pjanzen   332:                if ((month > 12) || (month < 1))
                    333:                        return (NULL);
1.1       millert   334:        }
                    335:
                    336:        /* 2. {Monthname} XYZ ... */
                    337:        else if (flags & F_ISMONTH) {
                    338:                month = v1;
1.7       pjanzen   339:                if (month == -1) {
                    340:                        month = tp->tm_mon + 1;
                    341:                        interval = MONTHLY;
                    342:                }
1.1       millert   343:                /* Monthname {day,weekday} */
                    344:                /* if no recognizable day, assume the first day in month */
                    345:                if (!(day = getfield(endp, &endp, &flags)))
                    346:                        day = 1;
1.7       pjanzen   347:                /* If a weekday was spelled out without an ordering,
                    348:                 * assume the first of that day in the month */
1.16      mickey    349:                if ((flags & F_ISDAY)) {
                    350:                        if ((day >= 1) && (day <=7))
                    351:                                day += 10;
                    352:                } else if (calendar)
                    353:                        adjust_calendar(&day, &month);
1.1       millert   354:        }
                    355:
                    356:        /* Hm ... */
                    357:        else {
                    358:                v2 = getfield(endp, &endp, &flags);
                    359:
                    360:                /*
                    361:                 * {Day} {Monthname} ...
                    362:                 * where Day <= 12
                    363:                 */
                    364:                if (flags & F_ISMONTH) {
                    365:                        day = v1;
                    366:                        month = v2;
1.7       pjanzen   367:                        if (month == -1) {
                    368:                                month = tp->tm_mon + 1;
                    369:                                interval = MONTHLY;
1.16      mickey    370:                        } else if (calendar)
                    371:                                adjust_calendar(&day, &month);
1.1       millert   372:                }
                    373:
                    374:                /* {Month} {Weekday,Day} ...  */
                    375:                else {
                    376:                        /* F_ISDAY set, v2 > 12, or no way to tell */
                    377:                        month = v1;
                    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)) {
                    550:                                        if (tdiff >=  0 ||
                    551:                                            (bodun && tdiff == -1)) {
1.7       pjanzen   552:                                        if ((tmp = malloc(sizeof(struct match))) == NULL)
1.11      pjanzen   553:                                                err(1, NULL);
1.7       pjanzen   554:                                        tmp->when = ttmp;
1.31    ! espie     555:                                        fill_print_date(tmp, &tmtmp);
1.13      mickey    556:                                        tmp->bodun = bodun && tdiff == -1;
1.7       pjanzen   557:                                        tmp->var   = varp;
                    558:                                        tmp->next  = NULL;
                    559:                                        if (tmp2)
                    560:                                                tmp2->next = tmp;
                    561:                                        else
                    562:                                                matches = tmp;
                    563:                                        tmp2 = tmp;
                    564:                                        v2 = (i == 1) ? 1 : 0;
                    565:                                        }
                    566:                                } else
                    567:                                        i = 2; /* No point checking in the future */
                    568:                        }
                    569:                } while (v2 != 0);
1.6       pjanzen   570:        }
1.7       pjanzen   571:        return (matches);
1.1       millert   572: }
                    573:
                    574:
                    575: int
1.20      deraadt   576: getmonth(char *s)
1.1       millert   577: {
1.14      mpech     578:        char **p;
1.1       millert   579:        struct fixs *n;
                    580:
                    581:        for (n = fnmonths; n->name; ++n)
                    582:                if (!strncasecmp(s, n->name, n->len))
                    583:                        return ((n - fnmonths) + 1);
                    584:        for (n = nmonths; n->name; ++n)
                    585:                if (!strncasecmp(s, n->name, n->len))
                    586:                        return ((n - nmonths) + 1);
                    587:        for (p = months; *p; ++p)
                    588:                if (!strncasecmp(s, *p, 3))
                    589:                        return ((p - months) + 1);
                    590:        return (0);
                    591: }
                    592:
                    593:
                    594: int
1.20      deraadt   595: getday(char *s)
1.1       millert   596: {
1.14      mpech     597:        char **p;
1.1       millert   598:        struct fixs *n;
                    599:
                    600:        for (n = fndays; n->name; ++n)
                    601:                if (!strncasecmp(s, n->name, n->len))
                    602:                        return ((n - fndays) + 1);
                    603:        for (n = ndays; n->name; ++n)
                    604:                if (!strncasecmp(s, n->name, n->len))
                    605:                        return ((n - ndays) + 1);
                    606:        for (p = days; *p; ++p)
                    607:                if (!strncasecmp(s, *p, 3))
                    608:                        return ((p - days) + 1);
                    609:        return (0);
                    610: }
                    611:
                    612: /* return offset for variable weekdays
                    613:  * -1 -> last weekday in month
                    614:  * +1 -> first weekday in month
                    615:  * ... etc ...
                    616:  */
                    617: int
1.20      deraadt   618: getdayvar(char *s)
1.1       millert   619: {
1.14      mpech     620:        int offset;
1.1       millert   621:
                    622:
                    623:        offset = strlen(s);
                    624:
                    625:        /* Sun+1 or Wednesday-2
                    626:         *    ^              ^   */
                    627:
                    628:        /* printf ("x: %s %s %d\n", s, s + offset - 2, offset); */
                    629:        switch(*(s + offset - 2)) {
                    630:        case '-':
                    631:        case '+':
1.7       pjanzen   632:            return(atoi(s + offset - 2));
1.1       millert   633:            break;
                    634:        }
                    635:
                    636:        /*
                    637:         * some aliases: last, first, second, third, fourth
                    638:         */
                    639:
                    640:        /* last */
                    641:        if      (offset > 4 && !strcasecmp(s + offset - 4, "last"))
                    642:            return(-1);
                    643:        else if (offset > 5 && !strcasecmp(s + offset - 5, "first"))
                    644:            return(+1);
                    645:        else if (offset > 6 && !strcasecmp(s + offset - 6, "second"))
                    646:            return(+2);
                    647:        else if (offset > 5 && !strcasecmp(s + offset - 5, "third"))
                    648:            return(+3);
                    649:        else if (offset > 6 && !strcasecmp(s + offset - 6, "fourth"))
                    650:            return(+4);
                    651:
                    652:        /* no offset detected */
                    653:        return(0);
1.7       pjanzen   654: }
                    655:
                    656:
                    657: int
1.20      deraadt   658: foy(int year)
1.7       pjanzen   659: {
                    660:        /* 0-6; what weekday Jan 1 is */
                    661:        year--;
                    662:        return ((1 - year/100 + year/400 + (int)(365.25 * year)) % 7);
                    663: }
                    664:
                    665:
                    666:
                    667: void
1.20      deraadt   668: variable_weekday(int *day, int month, int year)
1.7       pjanzen   669: {
                    670:        int v1, v2;
                    671:        int *cumdays;
                    672:        int day1;
                    673:
                    674:        if (isleap(year))
                    675:                cumdays = daytab[1];
                    676:        else
                    677:                cumdays = daytab[0];
                    678:        day1 = foy(year);
                    679:        /* negative offset; last, -4 .. -1 */
                    680:        if (*day < 0) {
                    681:                v1 = *day/10 - 1;          /* offset -4 ... -1 */
                    682:                *day = 10 + (*day % 10);    /* day 1 ... 7 */
                    683:
                    684:                /* which weekday the end of the month is (1-7) */
                    685:                v2 = (cumdays[month + 1] + day1) % 7 + 1;
                    686:
                    687:                /* and subtract enough days */
                    688:                *day = cumdays[month + 1] - cumdays[month] +
                    689:                    (v1 + 1) * 7 - (v2 - *day + 7) % 7;
                    690: #if DEBUG
                    691:                fprintf(stderr, "\nMonth %d ends on weekday %d\n", month, v2);
                    692: #endif
                    693:        }
                    694:
                    695:        /* first, second ... +1 ... +5 */
                    696:        else {
                    697:                v1 = *day/10;        /* offset */
                    698:                *day = *day % 10;
                    699:
                    700:                /* which weekday the first of the month is (1-7) */
                    701:                v2 = (cumdays[month] + 1 + day1) % 7 + 1;
                    702:
                    703:                /* and add enough days */
                    704:                *day = 1 + (v1 - 1) * 7 + (*day - v2 + 7) % 7;
                    705: #if DEBUG
                    706:                fprintf(stderr, "\nMonth %d starts on weekday %d\n", month, v2);
                    707: #endif
                    708:        }
1.1       millert   709: }