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