Home | History | Annotate | Line # | Download | only in calendar
calendar.c revision 1.13
      1  1.13    kleink /*	$NetBSD: calendar.c,v 1.13 1998/04/01 20:50:17 kleink 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.13    kleink __RCSID("$NetBSD: calendar.c,v 1.13 1998/04/01 20:50:17 kleink 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.7     glass 
     67   1.1       cgd #include "pathnames.h"
     68   1.1       cgd 
     69  1.10   thorpej #ifndef TRUE
     70  1.10   thorpej #define TRUE 1
     71  1.10   thorpej #endif
     72  1.10   thorpej #ifndef FALSE
     73  1.10   thorpej #define FALSE 0
     74  1.10   thorpej #endif
     75  1.10   thorpej 
     76  1.10   thorpej unsigned short lookahead = 1, weekend = 2;
     77  1.10   thorpej char *fname = "calendar", *datestr = NULL;
     78   1.1       cgd struct passwd *pw;
     79   1.1       cgd int doall;
     80   1.1       cgd 
     81  1.12  christos extern char *__progname;
     82  1.12  christos 
     83  1.10   thorpej void	 atodays __P((char, char *, unsigned short *));
     84   1.7     glass void	 cal __P((void));
     85   1.7     glass void	 closecal __P((FILE *));
     86   1.7     glass int	 getday __P((char *));
     87   1.7     glass int	 getfield __P((char *, char **, int *));
     88  1.10   thorpej void	 getmmdd(struct tm *tp, char *ds);
     89   1.7     glass int	 getmonth __P((char *));
     90   1.7     glass int	 isnow __P((char *));
     91  1.11     lukem int	 main __P((int, char **));
     92   1.7     glass FILE	*opencal __P((void));
     93   1.7     glass void	 settime __P((void));
     94   1.7     glass void	 usage __P((void));
     95   1.7     glass 
     96   1.7     glass int
     97   1.1       cgd main(argc, argv)
     98   1.1       cgd 	int argc;
     99   1.7     glass 	char *argv[];
    100   1.1       cgd {
    101   1.1       cgd 	extern int optind;
    102   1.1       cgd 	int ch;
    103   1.6   hpeyerl 	char *caldir;
    104   1.1       cgd 
    105  1.11     lukem 	while ((ch = getopt(argc, argv, "-ad:f:l:w:")) != -1)
    106   1.7     glass 		switch (ch) {
    107   1.1       cgd 		case '-':		/* backward contemptible */
    108   1.1       cgd 		case 'a':
    109   1.1       cgd 			if (getuid()) {
    110   1.7     glass 				errno = EPERM;
    111  1.11     lukem 				err(1, "%s", "");
    112   1.1       cgd 			}
    113   1.1       cgd 			doall = 1;
    114   1.1       cgd 			break;
    115  1.10   thorpej 		case 'd':
    116  1.10   thorpej 			datestr = optarg;
    117  1.10   thorpej 			break;
    118  1.10   thorpej 		case 'f':
    119  1.10   thorpej 			fname = optarg;
    120  1.10   thorpej 			break;
    121  1.10   thorpej 		case 'l':
    122  1.10   thorpej 			atodays(ch, optarg, &lookahead);
    123  1.10   thorpej 			break;
    124  1.10   thorpej 		case 'w':
    125  1.10   thorpej 			atodays(ch, optarg, &weekend);
    126  1.10   thorpej 			break;
    127   1.1       cgd 		case '?':
    128   1.1       cgd 		default:
    129   1.1       cgd 			usage();
    130   1.1       cgd 		}
    131   1.1       cgd 	argc -= optind;
    132   1.1       cgd 	argv += optind;
    133   1.1       cgd 
    134   1.1       cgd 	if (argc)
    135   1.1       cgd 		usage();
    136   1.1       cgd 
    137   1.1       cgd 	settime();
    138   1.1       cgd 	if (doall)
    139   1.7     glass 		while ((pw = getpwent()) != NULL) {
    140   1.1       cgd 			(void)setegid(pw->pw_gid);
    141   1.1       cgd 			(void)seteuid(pw->pw_uid);
    142   1.1       cgd 			if (!chdir(pw->pw_dir))
    143   1.1       cgd 				cal();
    144   1.1       cgd 			(void)seteuid(0);
    145   1.1       cgd 		}
    146   1.6   hpeyerl 	else if ((caldir = getenv("CALENDAR_DIR")) != NULL) {
    147   1.6   hpeyerl 			if(!chdir(caldir))
    148   1.6   hpeyerl 				cal();
    149   1.6   hpeyerl 	} else
    150   1.1       cgd 		cal();
    151   1.1       cgd 	exit(0);
    152   1.1       cgd }
    153   1.1       cgd 
    154   1.7     glass void
    155   1.1       cgd cal()
    156   1.1       cgd {
    157  1.11     lukem 	int printing;
    158  1.11     lukem 	char *p;
    159   1.7     glass 	FILE *fp;
    160   1.1       cgd 	int ch;
    161   1.1       cgd 	char buf[2048 + 1];
    162   1.1       cgd 
    163   1.7     glass 	if ((fp = opencal()) == NULL)
    164   1.1       cgd 		return;
    165   1.7     glass 	for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) {
    166   1.7     glass 		if ((p = strchr(buf, '\n')) != NULL)
    167   1.1       cgd 			*p = '\0';
    168   1.1       cgd 		else
    169   1.1       cgd 			while ((ch = getchar()) != '\n' && ch != EOF);
    170   1.1       cgd 		if (buf[0] == '\0')
    171   1.1       cgd 			continue;
    172   1.1       cgd 		if (buf[0] != '\t')
    173   1.1       cgd 			printing = isnow(buf) ? 1 : 0;
    174   1.1       cgd 		if (printing)
    175   1.1       cgd 			(void)fprintf(fp, "%s\n", buf);
    176   1.1       cgd 	}
    177   1.1       cgd 	closecal(fp);
    178   1.1       cgd }
    179   1.1       cgd 
    180   1.1       cgd struct iovec header[] = {
    181  1.11     lukem 	{ "From: ", 6 },
    182  1.11     lukem 	{ NULL, 0 },
    183  1.11     lukem 	{ " (Reminder Service)\nTo: ", 24 },
    184  1.11     lukem 	{ NULL, 0 },
    185  1.11     lukem 	{ "\nSubject: ", 10 },
    186  1.11     lukem 	{ NULL, 0 },
    187  1.11     lukem 	{ "'s Calendar\nPrecedence: bulk\n\n",  30 },
    188   1.1       cgd };
    189   1.1       cgd 
    190   1.1       cgd /* 1-based month, 0-based days, cumulative */
    191   1.1       cgd int daytab[][14] = {
    192  1.11     lukem 	{ 0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 },
    193  1.11     lukem 	{ 0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
    194   1.1       cgd };
    195   1.1       cgd struct tm *tp;
    196   1.1       cgd int *cumdays, offset, yrdays;
    197   1.1       cgd char dayname[10];
    198   1.1       cgd 
    199   1.7     glass void
    200   1.1       cgd settime()
    201   1.1       cgd {
    202   1.7     glass 	time_t now;
    203   1.1       cgd 
    204   1.1       cgd 	(void)time(&now);
    205   1.1       cgd 	tp = localtime(&now);
    206  1.10   thorpej 	if (datestr) {
    207  1.10   thorpej 		getmmdd(tp, datestr);
    208  1.10   thorpej 	}
    209  1.12  christos 	if (isleap(tp->tm_year + TM_YEAR_BASE)) {
    210   1.1       cgd 		yrdays = DAYSPERLYEAR;
    211   1.1       cgd 		cumdays = daytab[1];
    212   1.1       cgd 	} else {
    213   1.1       cgd 		yrdays = DAYSPERNYEAR;
    214   1.1       cgd 		cumdays = daytab[0];
    215   1.1       cgd 	}
    216   1.1       cgd 	/* Friday displays Monday's events */
    217  1.10   thorpej 	offset = tp->tm_wday == 5 ? lookahead + weekend : lookahead;
    218   1.1       cgd 	header[5].iov_base = dayname;
    219   1.1       cgd 	header[5].iov_len = strftime(dayname, sizeof(dayname), "%A", tp);
    220   1.1       cgd }
    221   1.1       cgd 
    222   1.1       cgd /*
    223   1.1       cgd  * Possible date formats include any combination of:
    224   1.1       cgd  *	3-charmonth			(January, Jan, Jan)
    225   1.1       cgd  *	3-charweekday			(Friday, Monday, mon.)
    226   1.1       cgd  *	numeric month or day		(1, 2, 04)
    227   1.1       cgd  *
    228   1.1       cgd  * Any character may separate them, or they may not be separated.  Any line,
    229   1.1       cgd  * following a line that is matched, that starts with "whitespace", is shown
    230   1.1       cgd  * along with the matched line.
    231   1.1       cgd  */
    232   1.7     glass int
    233   1.1       cgd isnow(endp)
    234   1.1       cgd 	char *endp;
    235   1.1       cgd {
    236   1.1       cgd 	int day, flags, month, v1, v2;
    237   1.1       cgd 
    238   1.1       cgd #define	F_ISMONTH	0x01
    239   1.1       cgd #define	F_ISDAY		0x02
    240   1.1       cgd 	flags = 0;
    241   1.1       cgd 	/* didn't recognize anything, skip it */
    242   1.1       cgd 	if (!(v1 = getfield(endp, &endp, &flags)))
    243   1.7     glass 		return (0);
    244   1.7     glass 	if (flags & F_ISDAY || v1 > 12) {
    245   1.1       cgd 		/* found a day */
    246   1.1       cgd 		day = v1;
    247   1.6   hpeyerl 		month = tp->tm_mon + 1;
    248   1.7     glass 	} else if (flags & F_ISMONTH) {
    249   1.1       cgd 		month = v1;
    250   1.1       cgd 		/* if no recognizable day, assume the first */
    251   1.1       cgd 		if (!(day = getfield(endp, &endp, &flags)))
    252   1.1       cgd 			day = 1;
    253   1.1       cgd 	} else {
    254   1.1       cgd 		v2 = getfield(endp, &endp, &flags);
    255   1.7     glass 		if (flags & F_ISMONTH) {
    256   1.1       cgd 			day = v1;
    257   1.1       cgd 			month = v2;
    258   1.1       cgd 		} else {
    259   1.1       cgd 			/* F_ISDAY set, v2 > 12, or no way to tell */
    260   1.1       cgd 			month = v1;
    261   1.1       cgd 			/* if no recognizable day, assume the first */
    262   1.1       cgd 			day = v2 ? v2 : 1;
    263   1.1       cgd 		}
    264   1.1       cgd 	}
    265   1.7     glass 	if (flags & F_ISDAY)
    266   1.4       cgd 		day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
    267   1.1       cgd 	day = cumdays[month] + day;
    268   1.1       cgd 
    269   1.1       cgd 	/* if today or today + offset days */
    270   1.1       cgd 	if (day >= tp->tm_yday && day <= tp->tm_yday + offset)
    271   1.7     glass 		return (1);
    272   1.1       cgd 	/* if number of days left in this year + days to event in next year */
    273   1.1       cgd 	if (yrdays - tp->tm_yday + day <= offset)
    274   1.7     glass 		return (1);
    275   1.7     glass 	return (0);
    276   1.1       cgd }
    277   1.1       cgd 
    278   1.7     glass int
    279   1.1       cgd getfield(p, endp, flags)
    280   1.1       cgd 	char *p, **endp;
    281   1.1       cgd 	int *flags;
    282   1.1       cgd {
    283   1.1       cgd 	int val;
    284   1.1       cgd 	char *start, savech;
    285   1.1       cgd 
    286   1.9     lukem 	for (; *p != '\0' && !isdigit(*p) && !isalpha(*p) && *p != '*'; ++p)
    287   1.9     lukem 		;
    288   1.1       cgd 	if (*p == '*') {			/* `*' is current month */
    289   1.1       cgd 		*flags |= F_ISMONTH;
    290   1.3       cgd 		*endp = p+1;
    291   1.7     glass 		return (tp->tm_mon + 1);
    292   1.1       cgd 	}
    293   1.1       cgd 	if (isdigit(*p)) {
    294   1.1       cgd 		val = strtol(p, &p, 10);	/* if 0, it's failure */
    295   1.9     lukem 		for (; *p != '\0' && !isdigit(*p) && !isalpha(*p) && *p != '*';
    296   1.9     lukem 		    ++p)
    297   1.9     lukem 			;
    298   1.1       cgd 		*endp = p;
    299   1.7     glass 		return (val);
    300   1.1       cgd 	}
    301   1.9     lukem 	for (start = p; *p != '\0' && isalpha(*++p);)
    302   1.9     lukem 		;
    303   1.1       cgd 	savech = *p;
    304   1.1       cgd 	*p = '\0';
    305   1.7     glass 	if ((val = getmonth(start)) != 0)
    306   1.1       cgd 		*flags |= F_ISMONTH;
    307   1.7     glass 	else if ((val = getday(start)) != 0)
    308   1.1       cgd 		*flags |= F_ISDAY;
    309   1.4       cgd 	else {
    310   1.4       cgd 		*p = savech;
    311   1.7     glass 		return (0);
    312   1.4       cgd 	}
    313   1.9     lukem 	for (*p = savech;
    314   1.9     lukem 	    *p != '\0' && !isdigit(*p) && !isalpha(*p) && *p != '*';
    315   1.9     lukem 	    ++p)
    316   1.9     lukem 		;
    317   1.1       cgd 	*endp = p;
    318   1.7     glass 	return (val);
    319   1.1       cgd }
    320   1.1       cgd 
    321   1.1       cgd char path[MAXPATHLEN + 1];
    322   1.1       cgd 
    323   1.1       cgd FILE *
    324   1.1       cgd opencal()
    325   1.1       cgd {
    326   1.1       cgd 	int fd, pdes[2];
    327   1.1       cgd 
    328   1.1       cgd 	/* open up calendar file as stdin */
    329  1.10   thorpej 	if (!freopen(fname, "r", stdin)) {
    330   1.1       cgd 		if (doall)
    331   1.7     glass 			return (NULL);
    332   1.7     glass 		errx(1, "no calendar file.");
    333   1.1       cgd 	}
    334  1.10   thorpej 	if (pipe(pdes) < 0)
    335   1.7     glass 		return (NULL);
    336   1.1       cgd 	switch (vfork()) {
    337   1.1       cgd 	case -1:			/* error */
    338   1.1       cgd 		(void)close(pdes[0]);
    339   1.1       cgd 		(void)close(pdes[1]);
    340   1.7     glass 		return (NULL);
    341   1.1       cgd 	case 0:
    342   1.1       cgd 		/* child -- stdin already setup, set stdout to pipe input */
    343   1.1       cgd 		if (pdes[1] != STDOUT_FILENO) {
    344   1.1       cgd 			(void)dup2(pdes[1], STDOUT_FILENO);
    345   1.1       cgd 			(void)close(pdes[1]);
    346   1.1       cgd 		}
    347   1.1       cgd 		(void)close(pdes[0]);
    348   1.5       cgd 		execl(_PATH_CPP, "cpp", "-P", "-I.", _PATH_INCLUDE, NULL);
    349   1.7     glass 		warn("execl: %s", _PATH_CPP);
    350   1.1       cgd 		_exit(1);
    351   1.1       cgd 	}
    352   1.1       cgd 	/* parent -- set stdin to pipe output */
    353   1.1       cgd 	(void)dup2(pdes[0], STDIN_FILENO);
    354   1.1       cgd 	(void)close(pdes[0]);
    355   1.1       cgd 	(void)close(pdes[1]);
    356   1.1       cgd 
    357   1.1       cgd 	/* not reading all calendar files, just set output to stdout */
    358   1.1       cgd 	if (!doall)
    359   1.7     glass 		return (stdout);
    360   1.1       cgd 
    361   1.1       cgd 	/* set output to a temporary file, so if no output don't send mail */
    362   1.7     glass 	(void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
    363   1.1       cgd 	if ((fd = mkstemp(path)) < 0)
    364   1.7     glass 		return (NULL);
    365   1.7     glass 	return (fdopen(fd, "w+"));
    366   1.1       cgd }
    367   1.1       cgd 
    368   1.7     glass void
    369   1.1       cgd closecal(fp)
    370   1.1       cgd 	FILE *fp;
    371   1.1       cgd {
    372   1.1       cgd 	struct stat sbuf;
    373   1.1       cgd 	int nread, pdes[2], status;
    374   1.7     glass 	char buf[1024];
    375   1.1       cgd 
    376   1.1       cgd 	if (!doall)
    377   1.1       cgd 		return;
    378   1.1       cgd 
    379   1.1       cgd 	(void)rewind(fp);
    380   1.1       cgd 	if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
    381   1.1       cgd 		goto done;
    382  1.10   thorpej 	if (pipe(pdes) < 0)
    383   1.1       cgd 		goto done;
    384   1.1       cgd 	switch (vfork()) {
    385   1.1       cgd 	case -1:			/* error */
    386   1.1       cgd 		(void)close(pdes[0]);
    387   1.1       cgd 		(void)close(pdes[1]);
    388   1.1       cgd 		goto done;
    389  1.10   thorpej 	case 0:
    390   1.1       cgd 		/* child -- set stdin to pipe output */
    391   1.1       cgd 		if (pdes[0] != STDIN_FILENO) {
    392   1.1       cgd 			(void)dup2(pdes[0], STDIN_FILENO);
    393   1.1       cgd 			(void)close(pdes[0]);
    394   1.1       cgd 		}
    395   1.1       cgd 		(void)close(pdes[1]);
    396   1.1       cgd 		execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
    397   1.1       cgd 		    "\"Reminder Service\"", "-f", "root", NULL);
    398   1.7     glass 		warn("execl: %s", _PATH_SENDMAIL);
    399   1.1       cgd 		_exit(1);
    400   1.1       cgd 	}
    401   1.1       cgd 	/* parent -- write to pipe input */
    402   1.1       cgd 	(void)close(pdes[0]);
    403   1.1       cgd 
    404   1.1       cgd 	header[1].iov_base = header[3].iov_base = pw->pw_name;
    405   1.1       cgd 	header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
    406   1.1       cgd 	writev(pdes[1], header, 7);
    407   1.1       cgd 	while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
    408   1.1       cgd 		(void)write(pdes[1], buf, nread);
    409   1.1       cgd 	(void)close(pdes[1]);
    410   1.1       cgd done:	(void)fclose(fp);
    411   1.1       cgd 	(void)unlink(path);
    412   1.1       cgd 	while (wait(&status) >= 0);
    413   1.1       cgd }
    414   1.1       cgd 
    415   1.1       cgd static char *months[] = {
    416   1.1       cgd 	"jan", "feb", "mar", "apr", "may", "jun",
    417   1.1       cgd 	"jul", "aug", "sep", "oct", "nov", "dec", NULL,
    418   1.1       cgd };
    419   1.7     glass 
    420   1.7     glass int
    421   1.1       cgd getmonth(s)
    422  1.11     lukem 	char *s;
    423   1.1       cgd {
    424  1.11     lukem 	char **p;
    425   1.1       cgd 
    426   1.1       cgd 	for (p = months; *p; ++p)
    427   1.1       cgd 		if (!strncasecmp(s, *p, 3))
    428   1.7     glass 			return ((p - months) + 1);
    429   1.7     glass 	return (0);
    430   1.1       cgd }
    431   1.1       cgd 
    432   1.1       cgd static char *days[] = {
    433   1.1       cgd 	"sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL,
    434   1.1       cgd };
    435   1.7     glass 
    436   1.7     glass int
    437   1.1       cgd getday(s)
    438  1.11     lukem 	char *s;
    439   1.1       cgd {
    440  1.11     lukem 	char **p;
    441   1.1       cgd 
    442   1.1       cgd 	for (p = days; *p; ++p)
    443   1.1       cgd 		if (!strncasecmp(s, *p, 3))
    444   1.7     glass 			return ((p - days) + 1);
    445   1.7     glass 	return (0);
    446   1.1       cgd }
    447   1.1       cgd 
    448   1.7     glass void
    449  1.10   thorpej atodays(char ch, char *optarg, unsigned short *days)
    450  1.10   thorpej {
    451  1.10   thorpej 	int u;
    452  1.10   thorpej 
    453  1.10   thorpej 	u = atoi(optarg);
    454  1.10   thorpej 	if ((u < 0) || (u > 366)) {
    455  1.10   thorpej 		fprintf(stderr,
    456  1.10   thorpej 			"%s: warning: -%c %d out of range 0-366, ignored.\n",
    457  1.10   thorpej 			__progname, ch, u);
    458  1.10   thorpej 	} else {
    459  1.10   thorpej 		*days = u;
    460  1.10   thorpej 	}
    461  1.10   thorpej }
    462  1.10   thorpej 
    463  1.10   thorpej #define todigit(x) ((x) - '0')
    464  1.10   thorpej #define ATOI2(x) (todigit((x)[0]) * 10 + todigit((x)[1]))
    465  1.10   thorpej #define ISDIG2(x) (isdigit((x)[0]) && isdigit((x)[1]))
    466  1.10   thorpej 
    467  1.10   thorpej void
    468  1.10   thorpej getmmdd(struct tm *tp, char *ds)
    469  1.10   thorpej {
    470  1.10   thorpej 	extern char *__progname;
    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.10   thorpej 		fprintf(stderr,
    511  1.10   thorpej 			"%s: warning: can't convert %s to date, ignored.\n",
    512  1.10   thorpej 			__progname, ds);
    513  1.10   thorpej 		usage();
    514  1.10   thorpej 	}
    515  1.10   thorpej }
    516  1.10   thorpej 
    517  1.10   thorpej void
    518   1.1       cgd usage()
    519   1.1       cgd {
    520  1.12  christos 	(void)fprintf(stderr, "Usage: %s [-a] [-d MMDD[[YY]YY]" \
    521  1.12  christos 		" [-f fname] [-l days] [-w days]\n", __progname);
    522   1.1       cgd 	exit(1);
    523   1.1       cgd }
    524