Home | History | Annotate | Line # | Download | only in calendar
calendar.c revision 1.3
      1 /*
      2  * Copyright (c) 1989 The Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 char copyright[] =
     36 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
     37  All rights reserved.\n";
     38 #endif /* not lint */
     39 
     40 #ifndef lint
     41 /*static char sccsid[] = "from: @(#)calendar.c	4.11 (Berkeley) 10/12/90";*/
     42 static char rcsid[] = "$Id: calendar.c,v 1.3 1994/01/05 13:13:00 cgd Exp $";
     43 #endif /* not lint */
     44 
     45 #include <sys/param.h>
     46 #include <sys/time.h>
     47 #include <sys/stat.h>
     48 #include <sys/file.h>
     49 #include <sys/uio.h>
     50 #include <pwd.h>
     51 #include <errno.h>
     52 #include <tzfile.h>
     53 #include <stdio.h>
     54 #include <ctype.h>
     55 #include <unistd.h>
     56 #include <string.h>
     57 #include "pathnames.h"
     58 
     59 extern int errno;
     60 struct passwd *pw;
     61 int doall;
     62 
     63 main(argc, argv)
     64 	int argc;
     65 	char **argv;
     66 {
     67 	extern int optind;
     68 	int ch;
     69 
     70 	while ((ch = getopt(argc, argv, "-a")) != EOF)
     71 		switch(ch) {
     72 		case '-':		/* backward contemptible */
     73 		case 'a':
     74 			if (getuid()) {
     75 				(void)fprintf(stderr,
     76 				    "calendar: %s\n", strerror(EPERM));
     77 				exit(1);
     78 			}
     79 			doall = 1;
     80 			break;
     81 		case '?':
     82 		default:
     83 			usage();
     84 		}
     85 	argc -= optind;
     86 	argv += optind;
     87 
     88 	if (argc)
     89 		usage();
     90 
     91 	settime();
     92 	if (doall)
     93 		while (pw = getpwent()) {
     94 			(void)setegid(pw->pw_gid);
     95 			(void)seteuid(pw->pw_uid);
     96 			if (!chdir(pw->pw_dir))
     97 				cal();
     98 			(void)seteuid(0);
     99 		}
    100 	else
    101 		cal();
    102 	exit(0);
    103 }
    104 
    105 cal()
    106 {
    107 	register int printing;
    108 	register char *p;
    109 	FILE *fp, *opencal();
    110 	int ch;
    111 	char buf[2048 + 1];
    112 
    113 	if (!(fp = opencal()))
    114 		return;
    115 	for (printing = 0; fgets(buf, sizeof(buf), stdin);) {
    116 		if (p = index(buf, '\n'))
    117 			*p = '\0';
    118 		else
    119 			while ((ch = getchar()) != '\n' && ch != EOF);
    120 		if (buf[0] == '\0')
    121 			continue;
    122 		if (buf[0] != '\t')
    123 			printing = isnow(buf) ? 1 : 0;
    124 		if (printing)
    125 			(void)fprintf(fp, "%s\n", buf);
    126 	}
    127 	closecal(fp);
    128 }
    129 
    130 struct iovec header[] = {
    131 	"From: ", 6,
    132 	NULL, 0,
    133 	" (Reminder Service)\nTo: ", 24,
    134 	NULL, 0,
    135 	"\nSubject: ", 10,
    136 	NULL, 0,
    137 	"'s Calendar\nPrecedence: bulk\n\n",  30,
    138 };
    139 
    140 /* 1-based month, 0-based days, cumulative */
    141 int daytab[][14] = {
    142 	0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364,
    143 	0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365,
    144 };
    145 struct tm *tp;
    146 int *cumdays, offset, yrdays;
    147 char dayname[10];
    148 
    149 settime()
    150 {
    151 	time_t now, time();
    152 
    153 	(void)time(&now);
    154 	tp = localtime(&now);
    155 	if (isleap(tp->tm_year + 1900)) {
    156 		yrdays = DAYSPERLYEAR;
    157 		cumdays = daytab[1];
    158 	} else {
    159 		yrdays = DAYSPERNYEAR;
    160 		cumdays = daytab[0];
    161 	}
    162 	/* Friday displays Monday's events */
    163 	offset = tp->tm_wday == 5 ? 3 : 1;
    164 	header[5].iov_base = dayname;
    165 	header[5].iov_len = strftime(dayname, sizeof(dayname), "%A", tp);
    166 }
    167 
    168 /*
    169  * Possible date formats include any combination of:
    170  *	3-charmonth			(January, Jan, Jan)
    171  *	3-charweekday			(Friday, Monday, mon.)
    172  *	numeric month or day		(1, 2, 04)
    173  *
    174  * Any character may separate them, or they may not be separated.  Any line,
    175  * following a line that is matched, that starts with "whitespace", is shown
    176  * along with the matched line.
    177  */
    178 isnow(endp)
    179 	char *endp;
    180 {
    181 	int day, flags, month, v1, v2;
    182 
    183 #define	F_ISMONTH	0x01
    184 #define	F_ISDAY		0x02
    185 	flags = 0;
    186 	/* didn't recognize anything, skip it */
    187 	if (!(v1 = getfield(endp, &endp, &flags)))
    188 		return(0);
    189 	if (flags&F_ISDAY || v1 > 12) {
    190 		/* found a day */
    191 		day = v1;
    192 		/* if no recognizable month, assume just a day alone */
    193 		if (!(month = getfield(endp, &endp, &flags)))
    194 			month = tp->tm_mon + 1;
    195 	} else if (flags&F_ISMONTH) {
    196 		month = v1;
    197 		/* if no recognizable day, assume the first */
    198 		if (!(day = getfield(endp, &endp, &flags)))
    199 			day = 1;
    200 	} else {
    201 		v2 = getfield(endp, &endp, &flags);
    202 		if (flags&F_ISMONTH) {
    203 			day = v1;
    204 			month = v2;
    205 		} else {
    206 			/* F_ISDAY set, v2 > 12, or no way to tell */
    207 			month = v1;
    208 			/* if no recognizable day, assume the first */
    209 			day = v2 ? v2 : 1;
    210 		}
    211 	}
    212 	day = cumdays[month] + day;
    213 
    214 	/* if today or today + offset days */
    215 	if (day >= tp->tm_yday && day <= tp->tm_yday + offset)
    216 		return(1);
    217 	/* if number of days left in this year + days to event in next year */
    218 	if (yrdays - tp->tm_yday + day <= offset)
    219 		return(1);
    220 	return(0);
    221 }
    222 
    223 getfield(p, endp, flags)
    224 	char *p, **endp;
    225 	int *flags;
    226 {
    227 	int val;
    228 	char *start, savech;
    229 
    230 	for (; !isdigit(*p) && !isalpha(*p) && *p != '*'; ++p)
    231 		;
    232 	if (*p == '*') {			/* `*' is current month */
    233 		*flags |= F_ISMONTH;
    234 		*endp = p+1;
    235 		return(tp->tm_mon + 1);
    236 	}
    237 	if (isdigit(*p)) {
    238 		val = strtol(p, &p, 10);	/* if 0, it's failure */
    239 		for (; !isdigit(*p) && !isalpha(*p) && *p != '*'; ++p)
    240 			;
    241 		*endp = p;
    242 		return(val);
    243 	}
    244 	for (start = p; isalpha(*++p););
    245 	savech = *p;
    246 	*p = '\0';
    247 	if (val = getmonth(start))
    248 		*flags |= F_ISMONTH;
    249 	else if (val = getday(start))
    250 		*flags |= F_ISDAY;
    251 	else
    252 		return(0);
    253 	for (*p = savech; !isdigit(*p) && !isalpha(*p) && *p != '*'; ++p)
    254 		;
    255 	*endp = p;
    256 	return(val);
    257 }
    258 
    259 char path[MAXPATHLEN + 1];
    260 
    261 FILE *
    262 opencal()
    263 {
    264 	int fd, pdes[2];
    265 	char *mktemp();
    266 
    267 	/* open up calendar file as stdin */
    268 	if (!freopen("calendar", "r", stdin)) {
    269 		if (doall)
    270 			return((FILE *)NULL);
    271 		(void)fprintf(stderr, "calendar: no calendar file.\n");
    272 		exit(1);
    273 	}
    274 	if (pipe(pdes) < 0)
    275 		return(NULL);
    276 	switch (vfork()) {
    277 	case -1:			/* error */
    278 		(void)close(pdes[0]);
    279 		(void)close(pdes[1]);
    280 		return(NULL);
    281 	case 0:
    282 		/* child -- stdin already setup, set stdout to pipe input */
    283 		if (pdes[1] != STDOUT_FILENO) {
    284 			(void)dup2(pdes[1], STDOUT_FILENO);
    285 			(void)close(pdes[1]);
    286 		}
    287 		(void)close(pdes[0]);
    288 		execl(_PATH_CPP, "cpp", "-I.", _PATH_INCLUDE, NULL);
    289 		_exit(1);
    290 	}
    291 	/* parent -- set stdin to pipe output */
    292 	(void)dup2(pdes[0], STDIN_FILENO);
    293 	(void)close(pdes[0]);
    294 	(void)close(pdes[1]);
    295 
    296 	/* not reading all calendar files, just set output to stdout */
    297 	if (!doall)
    298 		return(stdout);
    299 
    300 	/* set output to a temporary file, so if no output don't send mail */
    301 	(void)sprintf(path, "%s/_calXXXXXX", _PATH_TMP);
    302 	if ((fd = mkstemp(path)) < 0)
    303 		return(NULL);
    304 	return(fdopen(fd, "w+"));
    305 }
    306 
    307 closecal(fp)
    308 	FILE *fp;
    309 {
    310 	struct stat sbuf;
    311 	int nread, pdes[2], status;
    312 	char buf[1024], *mktemp();
    313 
    314 	if (!doall)
    315 		return;
    316 
    317 	(void)rewind(fp);
    318 	if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
    319 		goto done;
    320 	if (pipe(pdes) < 0)
    321 		goto done;
    322 	switch (vfork()) {
    323 	case -1:			/* error */
    324 		(void)close(pdes[0]);
    325 		(void)close(pdes[1]);
    326 		goto done;
    327 	case 0:
    328 		/* child -- set stdin to pipe output */
    329 		if (pdes[0] != STDIN_FILENO) {
    330 			(void)dup2(pdes[0], STDIN_FILENO);
    331 			(void)close(pdes[0]);
    332 		}
    333 		(void)close(pdes[1]);
    334 		execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
    335 		    "\"Reminder Service\"", "-f", "root", NULL);
    336 		(void)fprintf(stderr, "calendar: %s: %s.\n",
    337 		    _PATH_SENDMAIL, strerror(errno));
    338 		_exit(1);
    339 	}
    340 	/* parent -- write to pipe input */
    341 	(void)close(pdes[0]);
    342 
    343 	header[1].iov_base = header[3].iov_base = pw->pw_name;
    344 	header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
    345 	writev(pdes[1], header, 7);
    346 	while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
    347 		(void)write(pdes[1], buf, nread);
    348 	(void)close(pdes[1]);
    349 done:	(void)fclose(fp);
    350 	(void)unlink(path);
    351 	while (wait(&status) >= 0);
    352 }
    353 
    354 static char *months[] = {
    355 	"jan", "feb", "mar", "apr", "may", "jun",
    356 	"jul", "aug", "sep", "oct", "nov", "dec", NULL,
    357 };
    358 getmonth(s)
    359 	register char *s;
    360 {
    361 	register char **p;
    362 
    363 	for (p = months; *p; ++p)
    364 		if (!strncasecmp(s, *p, 3))
    365 			return((p - months) + 1);
    366 	return(0);
    367 }
    368 
    369 static char *days[] = {
    370 	"sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL,
    371 };
    372 getday(s)
    373 	register char *s;
    374 {
    375 	register char **p;
    376 
    377 	for (p = days; *p; ++p)
    378 		if (!strncasecmp(s, *p, 3))
    379 			return((p - days) + 1);
    380 	return(0);
    381 }
    382 
    383 usage()
    384 {
    385 	(void)fprintf(stderr, "usage: calendar [-a]\n");
    386 	exit(1);
    387 }
    388