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