Home | History | Annotate | Line # | Download | only in calendar
calendar.c revision 1.26
      1  1.26  christos /*	$NetBSD: calendar.c,v 1.26 2001/12/04 15:55:32 christos Exp $	*/
      2   1.7     glass 
      3   1.1       cgd /*
      4   1.7     glass  * Copyright (c) 1989, 1993, 1994
      5   1.7     glass  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36  1.11     lukem #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38  1.11     lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
     39  1.11     lukem 	The Regents of the University of California.  All rights reserved.\n");
     40   1.1       cgd #endif /* not lint */
     41   1.1       cgd 
     42   1.1       cgd #ifndef lint
     43   1.7     glass #if 0
     44   1.8       jtc static char sccsid[] = "@(#)calendar.c	8.4 (Berkeley) 1/7/95";
     45   1.7     glass #endif
     46  1.26  christos __RCSID("$NetBSD: calendar.c,v 1.26 2001/12/04 15:55:32 christos Exp $");
     47   1.1       cgd #endif /* not lint */
     48   1.1       cgd 
     49   1.1       cgd #include <sys/param.h>
     50   1.1       cgd #include <sys/time.h>
     51   1.1       cgd #include <sys/stat.h>
     52   1.1       cgd #include <sys/uio.h>
     53   1.7     glass #include <sys/wait.h>
     54   1.7     glass 
     55   1.7     glass #include <ctype.h>
     56   1.7     glass #include <err.h>
     57   1.7     glass #include <errno.h>
     58   1.7     glass #include <fcntl.h>
     59   1.1       cgd #include <pwd.h>
     60   1.7     glass #include <stdio.h>
     61   1.7     glass #include <stdlib.h>
     62   1.7     glass #include <string.h>
     63  1.13    kleink #include <time.h>
     64   1.1       cgd #include <tzfile.h>
     65   1.1       cgd #include <unistd.h>
     66  1.26  christos #include <util.h>
     67   1.7     glass 
     68   1.1       cgd #include "pathnames.h"
     69   1.1       cgd 
     70  1.10   thorpej #ifndef TRUE
     71  1.10   thorpej #define TRUE 1
     72  1.10   thorpej #endif
     73  1.10   thorpej #ifndef FALSE
     74  1.10   thorpej #define FALSE 0
     75  1.10   thorpej #endif
     76  1.10   thorpej 
     77  1.26  christos static unsigned short lookahead = 1, weekend = 2;
     78  1.26  christos static char *fname = "calendar", *datestr = NULL;
     79  1.26  christos static struct passwd *pw;
     80  1.26  christos static int doall;
     81  1.26  christos static char path[MAXPATHLEN + 1];
     82  1.26  christos 
     83  1.26  christos /* 1-based month, 0-based days, cumulative */
     84  1.26  christos static int daytab[][14] = {
     85  1.26  christos 	{ 0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 },
     86  1.26  christos 	{ 0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
     87  1.26  christos };
     88  1.26  christos static struct tm *tp;
     89  1.26  christos static int *cumdays, offset, yrdays;
     90  1.26  christos static char dayname[10];
     91  1.26  christos 
     92  1.26  christos static struct iovec header[] = {
     93  1.26  christos 	{ "From: ", 6 },
     94  1.26  christos 	{ NULL, 0 },
     95  1.26  christos 	{ " (Reminder Service)\nTo: ", 24 },
     96  1.26  christos 	{ NULL, 0 },
     97  1.26  christos 	{ "\nSubject: ", 10 },
     98  1.26  christos 	{ NULL, 0 },
     99  1.26  christos 	{ "'s Calendar\nPrecedence: bulk\n\n",  30 },
    100  1.26  christos };
    101  1.26  christos 
    102  1.26  christos static char *days[] = {
    103  1.26  christos 	"sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL,
    104  1.26  christos };
    105  1.26  christos 
    106  1.26  christos static char *months[] = {
    107  1.26  christos 	"jan", "feb", "mar", "apr", "may", "jun",
    108  1.26  christos 	"jul", "aug", "sep", "oct", "nov", "dec", NULL,
    109  1.26  christos };
    110  1.26  christos 
    111  1.26  christos int	 main(int, char **);
    112  1.26  christos 
    113  1.26  christos static void	 atodays(int, char *, unsigned short *);
    114  1.26  christos static void	 cal(void);
    115  1.26  christos static void	 closecal(FILE *);
    116  1.26  christos static int	 getday(char *);
    117  1.26  christos static int	 getfield(char *, char **, int *);
    118  1.26  christos static void	 getmmdd(struct tm *, char *);
    119  1.26  christos static int	 getmonth(char *);
    120  1.26  christos static int	 isnow(char *);
    121  1.26  christos static FILE	*opencal(void);
    122  1.26  christos static void	 settime(void);
    123  1.26  christos static void	 usage(void) __attribute__((__noreturn__));
    124   1.7     glass 
    125   1.7     glass int
    126   1.1       cgd main(argc, argv)
    127   1.1       cgd 	int argc;
    128   1.7     glass 	char *argv[];
    129   1.1       cgd {
    130   1.1       cgd 	int ch;
    131  1.15   mycroft 	const char *caldir;
    132   1.1       cgd 
    133  1.20     lukem 	while ((ch = getopt(argc, argv, "-ad:f:l:w:")) != -1)
    134   1.7     glass 		switch (ch) {
    135   1.1       cgd 		case '-':		/* backward contemptible */
    136   1.1       cgd 		case 'a':
    137   1.1       cgd 			if (getuid()) {
    138   1.7     glass 				errno = EPERM;
    139  1.21  drochner 				err(1, NULL);
    140   1.1       cgd 			}
    141   1.1       cgd 			doall = 1;
    142   1.1       cgd 			break;
    143  1.10   thorpej 		case 'd':
    144  1.10   thorpej 			datestr = optarg;
    145  1.10   thorpej 			break;
    146  1.10   thorpej 		case 'f':
    147  1.10   thorpej 			fname = optarg;
    148  1.10   thorpej 			break;
    149  1.10   thorpej 		case 'l':
    150  1.10   thorpej 			atodays(ch, optarg, &lookahead);
    151  1.10   thorpej 			break;
    152  1.10   thorpej 		case 'w':
    153  1.10   thorpej 			atodays(ch, optarg, &weekend);
    154  1.10   thorpej 			break;
    155   1.1       cgd 		case '?':
    156   1.1       cgd 		default:
    157   1.1       cgd 			usage();
    158   1.1       cgd 		}
    159   1.1       cgd 	argc -= optind;
    160   1.1       cgd 	argv += optind;
    161   1.1       cgd 
    162   1.1       cgd 	if (argc)
    163   1.1       cgd 		usage();
    164   1.1       cgd 
    165   1.1       cgd 	settime();
    166  1.26  christos 	if (doall) {
    167   1.7     glass 		while ((pw = getpwent()) != NULL) {
    168   1.1       cgd 			(void)setegid(pw->pw_gid);
    169   1.1       cgd 			(void)seteuid(pw->pw_uid);
    170   1.1       cgd 			if (!chdir(pw->pw_dir))
    171   1.1       cgd 				cal();
    172   1.1       cgd 			(void)seteuid(0);
    173   1.1       cgd 		}
    174  1.26  christos 	} else if ((caldir = getenv("CALENDAR_DIR")) != NULL) {
    175  1.26  christos 		if(!chdir(caldir))
    176  1.26  christos 			cal();
    177  1.26  christos 	} else {
    178   1.1       cgd 		cal();
    179  1.26  christos 	}
    180   1.1       cgd 	exit(0);
    181   1.1       cgd }
    182   1.1       cgd 
    183  1.26  christos static void
    184  1.26  christos cal(void)
    185   1.1       cgd {
    186  1.11     lukem 	int printing;
    187   1.7     glass 	FILE *fp;
    188  1.26  christos 	char *line;
    189   1.1       cgd 
    190   1.7     glass 	if ((fp = opencal()) == NULL)
    191   1.1       cgd 		return;
    192  1.26  christos 	while ((line = fparseln(stdin, NULL, NULL, NULL, 0)) != NULL) {
    193  1.26  christos 		if (line[0] == '\0')
    194   1.1       cgd 			continue;
    195  1.26  christos 		if (line[0] != '\t')
    196  1.26  christos 			printing = isnow(line) ? 1 : 0;
    197   1.1       cgd 		if (printing)
    198  1.26  christos 			(void)fprintf(fp, "%s\n", line);
    199  1.26  christos 		free(line);
    200  1.26  christos 
    201   1.1       cgd 	}
    202   1.1       cgd 	closecal(fp);
    203   1.1       cgd }
    204   1.1       cgd 
    205   1.1       cgd 
    206  1.26  christos static void
    207  1.26  christos settime(void)
    208   1.1       cgd {
    209   1.7     glass 	time_t now;
    210   1.1       cgd 
    211   1.1       cgd 	(void)time(&now);
    212   1.1       cgd 	tp = localtime(&now);
    213  1.10   thorpej 	if (datestr) {
    214  1.10   thorpej 		getmmdd(tp, datestr);
    215  1.10   thorpej 	}
    216  1.12  christos 	if (isleap(tp->tm_year + TM_YEAR_BASE)) {
    217   1.1       cgd 		yrdays = DAYSPERLYEAR;
    218   1.1       cgd 		cumdays = daytab[1];
    219   1.1       cgd 	} else {
    220   1.1       cgd 		yrdays = DAYSPERNYEAR;
    221   1.1       cgd 		cumdays = daytab[0];
    222   1.1       cgd 	}
    223   1.1       cgd 	/* Friday displays Monday's events */
    224  1.10   thorpej 	offset = tp->tm_wday == 5 ? lookahead + weekend : lookahead;
    225   1.1       cgd 	header[5].iov_base = dayname;
    226   1.1       cgd 	header[5].iov_len = strftime(dayname, sizeof(dayname), "%A", tp);
    227   1.1       cgd }
    228   1.1       cgd 
    229   1.1       cgd /*
    230   1.1       cgd  * Possible date formats include any combination of:
    231   1.1       cgd  *	3-charmonth			(January, Jan, Jan)
    232   1.1       cgd  *	3-charweekday			(Friday, Monday, mon.)
    233   1.1       cgd  *	numeric month or day		(1, 2, 04)
    234   1.1       cgd  *
    235   1.1       cgd  * Any character may separate them, or they may not be separated.  Any line,
    236   1.1       cgd  * following a line that is matched, that starts with "whitespace", is shown
    237   1.1       cgd  * along with the matched line.
    238   1.1       cgd  */
    239  1.26  christos static int
    240   1.1       cgd isnow(endp)
    241   1.1       cgd 	char *endp;
    242   1.1       cgd {
    243   1.1       cgd 	int day, flags, month, v1, v2;
    244   1.1       cgd 
    245   1.1       cgd #define	F_ISMONTH	0x01
    246   1.1       cgd #define	F_ISDAY		0x02
    247   1.1       cgd 	flags = 0;
    248   1.1       cgd 	/* didn't recognize anything, skip it */
    249   1.1       cgd 	if (!(v1 = getfield(endp, &endp, &flags)))
    250   1.7     glass 		return (0);
    251   1.7     glass 	if (flags & F_ISDAY || v1 > 12) {
    252   1.1       cgd 		/* found a day */
    253   1.1       cgd 		day = v1;
    254   1.6   hpeyerl 		month = tp->tm_mon + 1;
    255   1.7     glass 	} else if (flags & F_ISMONTH) {
    256   1.1       cgd 		month = v1;
    257   1.1       cgd 		/* if no recognizable day, assume the first */
    258   1.1       cgd 		if (!(day = getfield(endp, &endp, &flags)))
    259   1.1       cgd 			day = 1;
    260   1.1       cgd 	} else {
    261   1.1       cgd 		v2 = getfield(endp, &endp, &flags);
    262   1.7     glass 		if (flags & F_ISMONTH) {
    263   1.1       cgd 			day = v1;
    264   1.1       cgd 			month = v2;
    265   1.1       cgd 		} else {
    266   1.1       cgd 			/* F_ISDAY set, v2 > 12, or no way to tell */
    267   1.1       cgd 			month = v1;
    268   1.1       cgd 			/* if no recognizable day, assume the first */
    269   1.1       cgd 			day = v2 ? v2 : 1;
    270   1.1       cgd 		}
    271   1.1       cgd 	}
    272   1.7     glass 	if (flags & F_ISDAY)
    273   1.4       cgd 		day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
    274   1.1       cgd 	day = cumdays[month] + day;
    275   1.1       cgd 
    276   1.1       cgd 	/* if today or today + offset days */
    277   1.1       cgd 	if (day >= tp->tm_yday && day <= tp->tm_yday + offset)
    278   1.7     glass 		return (1);
    279   1.1       cgd 	/* if number of days left in this year + days to event in next year */
    280   1.1       cgd 	if (yrdays - tp->tm_yday + day <= offset)
    281   1.7     glass 		return (1);
    282   1.7     glass 	return (0);
    283   1.1       cgd }
    284   1.1       cgd 
    285  1.26  christos static int
    286   1.1       cgd getfield(p, endp, flags)
    287   1.1       cgd 	char *p, **endp;
    288   1.1       cgd 	int *flags;
    289   1.1       cgd {
    290   1.1       cgd 	int val;
    291   1.1       cgd 	char *start, savech;
    292   1.1       cgd 
    293  1.18  christos #define FLDCHAR(a) (*p != '\0' && !isdigit((unsigned char)*p) && \
    294  1.18  christos     !isalpha((unsigned char)*p) && *p != '*')
    295  1.18  christos 
    296  1.18  christos 	for (; FLDCHAR(*p); ++p)
    297  1.26  christos 		continue;
    298   1.1       cgd 	if (*p == '*') {			/* `*' is current month */
    299   1.1       cgd 		*flags |= F_ISMONTH;
    300   1.3       cgd 		*endp = p+1;
    301   1.7     glass 		return (tp->tm_mon + 1);
    302   1.1       cgd 	}
    303  1.18  christos 	if (isdigit((unsigned char)*p)) {
    304   1.1       cgd 		val = strtol(p, &p, 10);	/* if 0, it's failure */
    305  1.18  christos 		for (; FLDCHAR(*p); ++p)
    306  1.26  christos 			continue;
    307   1.1       cgd 		*endp = p;
    308   1.7     glass 		return (val);
    309   1.1       cgd 	}
    310  1.18  christos 	for (start = p; *p != '\0' && isalpha((unsigned char)*++p);)
    311  1.26  christos 		continue;
    312   1.1       cgd 	savech = *p;
    313   1.1       cgd 	*p = '\0';
    314  1.26  christos 	if ((val = getmonth(start)) != 0) {
    315   1.1       cgd 		*flags |= F_ISMONTH;
    316  1.26  christos 	} else if ((val = getday(start)) != 0) {
    317   1.1       cgd 		*flags |= F_ISDAY;
    318  1.26  christos 	} else {
    319   1.4       cgd 		*p = savech;
    320   1.7     glass 		return (0);
    321   1.4       cgd 	}
    322  1.18  christos 	for (*p = savech; FLDCHAR(*p); ++p)
    323  1.26  christos 		continue;
    324   1.1       cgd 	*endp = p;
    325   1.7     glass 	return (val);
    326   1.1       cgd }
    327   1.1       cgd 
    328  1.26  christos static FILE *
    329  1.26  christos opencal(void)
    330   1.1       cgd {
    331   1.1       cgd 	int fd, pdes[2];
    332   1.1       cgd 
    333   1.1       cgd 	/* open up calendar file as stdin */
    334  1.23  christos 	if (!freopen(fname, "rf", stdin)) {
    335   1.1       cgd 		if (doall)
    336   1.7     glass 			return (NULL);
    337  1.23  christos 		err(1, "Cannot open `%s'", fname);
    338   1.1       cgd 	}
    339  1.26  christos 	if (pipe(pdes) < 0) {
    340  1.26  christos 		warn("Cannot open pipe");
    341   1.7     glass 		return (NULL);
    342  1.26  christos 	}
    343  1.26  christos 	switch (fork()) {
    344   1.1       cgd 	case -1:			/* error */
    345   1.1       cgd 		(void)close(pdes[0]);
    346   1.1       cgd 		(void)close(pdes[1]);
    347   1.7     glass 		return (NULL);
    348   1.1       cgd 	case 0:
    349   1.1       cgd 		/* child -- stdin already setup, set stdout to pipe input */
    350   1.1       cgd 		if (pdes[1] != STDOUT_FILENO) {
    351   1.1       cgd 			(void)dup2(pdes[1], STDOUT_FILENO);
    352   1.1       cgd 			(void)close(pdes[1]);
    353   1.1       cgd 		}
    354   1.1       cgd 		(void)close(pdes[0]);
    355  1.26  christos 		(void)execl(_PATH_CPP, "cpp", "-traditional", "-P", "-I.",
    356  1.26  christos 		    "-I" _PATH_CALENDARS, NULL);
    357  1.26  christos 		err(1, "Cannot exec `%s'", _PATH_CPP);
    358  1.26  christos 		/*NOTREACHED*/
    359   1.1       cgd 	}
    360   1.1       cgd 	/* parent -- set stdin to pipe output */
    361   1.1       cgd 	(void)dup2(pdes[0], STDIN_FILENO);
    362   1.1       cgd 	(void)close(pdes[0]);
    363   1.1       cgd 	(void)close(pdes[1]);
    364   1.1       cgd 
    365   1.1       cgd 	/* not reading all calendar files, just set output to stdout */
    366   1.1       cgd 	if (!doall)
    367   1.7     glass 		return (stdout);
    368   1.1       cgd 
    369   1.1       cgd 	/* set output to a temporary file, so if no output don't send mail */
    370   1.7     glass 	(void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
    371  1.26  christos 	if ((fd = mkstemp(path)) < 0) {
    372  1.26  christos 		warn("Cannot create temporary file");
    373   1.7     glass 		return (NULL);
    374  1.26  christos 	}
    375   1.7     glass 	return (fdopen(fd, "w+"));
    376   1.1       cgd }
    377   1.1       cgd 
    378  1.26  christos static void
    379   1.1       cgd closecal(fp)
    380   1.1       cgd 	FILE *fp;
    381   1.1       cgd {
    382   1.1       cgd 	struct stat sbuf;
    383   1.1       cgd 	int nread, pdes[2], status;
    384   1.7     glass 	char buf[1024];
    385   1.1       cgd 
    386   1.1       cgd 	if (!doall)
    387   1.1       cgd 		return;
    388   1.1       cgd 
    389   1.1       cgd 	(void)rewind(fp);
    390   1.1       cgd 	if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
    391   1.1       cgd 		goto done;
    392  1.10   thorpej 	if (pipe(pdes) < 0)
    393   1.1       cgd 		goto done;
    394  1.26  christos 	switch (fork()) {
    395   1.1       cgd 	case -1:			/* error */
    396   1.1       cgd 		(void)close(pdes[0]);
    397   1.1       cgd 		(void)close(pdes[1]);
    398   1.1       cgd 		goto done;
    399  1.10   thorpej 	case 0:
    400   1.1       cgd 		/* child -- set stdin to pipe output */
    401   1.1       cgd 		if (pdes[0] != STDIN_FILENO) {
    402   1.1       cgd 			(void)dup2(pdes[0], STDIN_FILENO);
    403   1.1       cgd 			(void)close(pdes[0]);
    404   1.1       cgd 		}
    405   1.1       cgd 		(void)close(pdes[1]);
    406  1.26  christos 		(void)execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
    407   1.1       cgd 		    "\"Reminder Service\"", "-f", "root", NULL);
    408  1.26  christos 		err(1, "Cannot exec `%s'", _PATH_SENDMAIL);
    409  1.26  christos 		/*NOTREACHED*/
    410   1.1       cgd 	}
    411   1.1       cgd 	/* parent -- write to pipe input */
    412   1.1       cgd 	(void)close(pdes[0]);
    413   1.1       cgd 
    414  1.14   mycroft 	header[1].iov_base = header[3].iov_base = (void *)pw->pw_name;
    415   1.1       cgd 	header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
    416   1.1       cgd 	writev(pdes[1], header, 7);
    417   1.1       cgd 	while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
    418   1.1       cgd 		(void)write(pdes[1], buf, nread);
    419   1.1       cgd 	(void)close(pdes[1]);
    420  1.26  christos 
    421   1.1       cgd done:	(void)fclose(fp);
    422   1.1       cgd 	(void)unlink(path);
    423  1.26  christos 	while (wait(&status) >= 0)
    424  1.26  christos 		continue;
    425   1.1       cgd }
    426   1.1       cgd 
    427  1.26  christos static int
    428   1.1       cgd getmonth(s)
    429  1.11     lukem 	char *s;
    430   1.1       cgd {
    431  1.11     lukem 	char **p;
    432   1.1       cgd 
    433   1.1       cgd 	for (p = months; *p; ++p)
    434   1.1       cgd 		if (!strncasecmp(s, *p, 3))
    435   1.7     glass 			return ((p - months) + 1);
    436   1.7     glass 	return (0);
    437   1.1       cgd }
    438   1.1       cgd 
    439  1.26  christos static int
    440   1.1       cgd getday(s)
    441  1.11     lukem 	char *s;
    442   1.1       cgd {
    443  1.11     lukem 	char **p;
    444   1.1       cgd 
    445   1.1       cgd 	for (p = days; *p; ++p)
    446   1.1       cgd 		if (!strncasecmp(s, *p, 3))
    447   1.7     glass 			return ((p - days) + 1);
    448   1.7     glass 	return (0);
    449   1.1       cgd }
    450   1.1       cgd 
    451  1.26  christos static void
    452  1.26  christos atodays(int ch, char *optarg, unsigned short *days)
    453  1.10   thorpej {
    454  1.10   thorpej 	int u;
    455  1.10   thorpej 
    456  1.10   thorpej 	u = atoi(optarg);
    457  1.10   thorpej 	if ((u < 0) || (u > 366)) {
    458  1.26  christos 		warnx("-%c %d out of range 0-366, ignored.", ch, u);
    459  1.10   thorpej 	} else {
    460  1.10   thorpej 		*days = u;
    461  1.10   thorpej 	}
    462  1.10   thorpej }
    463  1.10   thorpej 
    464  1.10   thorpej #define todigit(x) ((x) - '0')
    465  1.10   thorpej #define ATOI2(x) (todigit((x)[0]) * 10 + todigit((x)[1]))
    466  1.18  christos #define ISDIG2(x) (isdigit((unsigned char)(x)[0]) && isdigit((unsigned char)(x)[1]))
    467  1.10   thorpej 
    468  1.26  christos static void
    469  1.10   thorpej getmmdd(struct tm *tp, char *ds)
    470  1.10   thorpej {
    471  1.10   thorpej 	int ok = FALSE;
    472  1.10   thorpej 	struct tm ttm;
    473  1.10   thorpej 
    474  1.10   thorpej 	ttm = *tp;
    475  1.10   thorpej 	ttm.tm_isdst = -1;
    476  1.10   thorpej 
    477  1.10   thorpej 	if (ISDIG2(ds)) {
    478  1.10   thorpej 		ttm.tm_mon = ATOI2(ds) - 1;
    479  1.10   thorpej 		ds += 2;
    480  1.10   thorpej 	}
    481  1.10   thorpej 
    482  1.10   thorpej 	if (ISDIG2(ds)) {
    483  1.10   thorpej 		ttm.tm_mday = ATOI2(ds);
    484  1.10   thorpej 		ds += 2;
    485  1.10   thorpej 
    486  1.10   thorpej 		ok = TRUE;
    487  1.10   thorpej 	}
    488  1.10   thorpej 
    489  1.10   thorpej 	if (ok) {
    490  1.10   thorpej 		if (ISDIG2(ds) && ISDIG2(ds + 2)) {
    491  1.12  christos 			ttm.tm_year = ATOI2(ds) * 100 - TM_YEAR_BASE;
    492  1.10   thorpej 			ds += 2;
    493  1.10   thorpej 			ttm.tm_year += ATOI2(ds);
    494  1.10   thorpej 		} else if (ISDIG2(ds)) {
    495  1.10   thorpej 			ttm.tm_year = ATOI2(ds);
    496  1.12  christos 			if (ttm.tm_year < 69)
    497  1.12  christos 				ttm.tm_year += 2000 - TM_YEAR_BASE;
    498  1.12  christos 			else
    499  1.12  christos 				ttm.tm_year += 1900 - TM_YEAR_BASE;
    500  1.10   thorpej 		}
    501  1.10   thorpej 	}
    502  1.10   thorpej 
    503  1.10   thorpej 	if (ok && (mktime(&ttm) < 0)) {
    504  1.10   thorpej 		ok = FALSE;
    505  1.10   thorpej 	}
    506  1.10   thorpej 
    507  1.10   thorpej 	if (ok) {
    508  1.10   thorpej 		*tp = ttm;
    509  1.10   thorpej 	} else {
    510  1.26  christos 		warnx("Can't convert `%s' to date, ignored.", ds);
    511  1.10   thorpej 		usage();
    512  1.10   thorpej 	}
    513  1.10   thorpej }
    514  1.10   thorpej 
    515  1.26  christos static void
    516  1.26  christos usage(void)
    517   1.1       cgd {
    518  1.26  christos 	(void)fprintf(stderr, "Usage: %s [-a] [-d MMDD[[YY]YY]"
    519  1.26  christos 	    " [-f fname] [-l days] [-w days]\n", getprogname());
    520   1.1       cgd 	exit(1);
    521   1.1       cgd }
    522