2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1989, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
49 #include <sys/types.h>
50 #include <sys/param.h>
66 #include "pathnames.h"
70 const char *calendarFile = "calendar"; /* default calendar file */
71 const char *calendarHomes[] = { ".calendar", _PATH_INCLUDE }; /* HOME */
72 const char *calendarNoMail = "nomail"; /* don't sent mail if this file exist */
74 struct fixs neaster, npaskha;
76 struct iovec header[] = {
79 {" (Reminder Service)\nTo: ", 24},
83 {"'s Calendar\nPrecedence: bulk\n\n", 30},
97 static int d_first = -1;
100 if ((fp = opencal()) == NULL)
102 for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) {
103 if ((p = strchr(buf, '\n')) != NULL)
106 while ((ch = getchar()) != '\n' && ch != EOF);
107 for (l = strlen(buf);
108 l > 0 && isspace((unsigned char)buf[l - 1]);
114 if (strncmp(buf, "LANG=", 5) == 0) {
115 (void) setlocale(LC_ALL, buf + 5);
116 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
120 if (strncasecmp(buf, "Easter=", 7) == 0 && buf[7]) {
121 if (neaster.name != NULL)
123 if ((neaster.name = strdup(buf + 7)) == NULL)
124 errx(1, "cannot allocate memory");
125 neaster.len = strlen(buf + 7);
128 if (strncasecmp(buf, "Paskha=", 7) == 0 && buf[7]) {
129 if (npaskha.name != NULL)
131 if ((npaskha.name = strdup(buf + 7)) == NULL)
132 errx(1, "cannot allocate memory");
133 npaskha.len = strlen(buf + 7);
136 if (buf[0] != '\t') {
137 printing = isnow(buf, &month, &day, &var) ? 1 : 0;
138 if ((p = strchr(buf, '\t')) == NULL)
140 if (p > buf && p[-1] == '*')
147 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
148 tm.tm_sec = 0; /* unused */
149 tm.tm_min = 0; /* unused */
150 tm.tm_hour = 0; /* unused */
151 tm.tm_wday = 0; /* unused */
152 tm.tm_mon = month - 1;
154 tm.tm_year = tp->tm_year; /* unused */
155 (void)strftime(dbuf, sizeof(dbuf),
156 d_first ? "%e %b" : "%b %e",
158 (void)fprintf(fp, "%s%c%s\n", dbuf,
163 fprintf(fp, "%s\n", buf);
169 getfield(p, endp, flags)
176 for (; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p) && *p != '*'; ++p);
177 if (*p == '*') { /* `*' is current month */
180 return (tp->tm_mon + 1);
182 if (isdigit((unsigned char)*p)) {
183 val = strtol(p, &p, 10); /* if 0, it's failure */
184 for (; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p) && *p != '*'; ++p);
188 for (start = p; isalpha((unsigned char)*++p););
191 if (*p == '+' || *p == '-')
192 for(; isdigit((unsigned char)*++p););
198 if ((val = getmonth(start)) != 0)
202 else if ((val = getday(start)) != 0) {
205 /* variable weekday */
206 if ((var = getdayvar(start)) != 0) {
207 if (var <=5 && var >= -4)
210 printf("var: %d\n", var);
216 else if ((val = geteaster(start, tp->tm_year + 1900)) != 0)
220 else if ((val = getpaskha(start, tp->tm_year + 1900)) != 0)
228 for (*p = savech; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p) && *p != '*'; ++p);
233 char path[MAXPATHLEN];
240 int fd, found, pdes[2];
243 /* open up calendar file as stdin */
244 if (!freopen(calendarFile, "r", stdin)) {
246 if (chdir(calendarHomes[0]) != 0)
248 if (stat(calendarNoMail, &sbuf) == 0)
250 if (!freopen(calendarFile, "r", stdin))
253 chdir(getenv("HOME"));
254 for (found = i = 0; i < sizeof(calendarHomes) /
255 sizeof(calendarHomes[0]); i++)
256 if (chdir(calendarHomes[i]) == 0 &&
257 freopen(calendarFile, "r", stdin)) {
262 errx(1, "no calendar file: ``%s''", calendarFile);
269 (void)close(pdes[0]);
270 (void)close(pdes[1]);
273 /* child -- stdin already setup, set stdout to pipe input */
274 if (pdes[1] != STDOUT_FILENO) {
275 (void)dup2(pdes[1], STDOUT_FILENO);
276 (void)close(pdes[1]);
278 (void)close(pdes[0]);
280 if (setuid(getuid()) < 0) {
281 warnx("first setuid failed");
284 if (setgid(getegid()) < 0) {
285 warnx("setgid failed");
288 if (setuid(uid) < 0) {
289 warnx("setuid failed");
292 execl(_PATH_CPP, "cpp", "-P",
293 "-traditional", "-nostdinc", /* GCC specific opts */
294 "-I.", "-I", _PATH_INCLUDE, (char *)NULL);
298 /* parent -- set stdin to pipe output */
299 (void)dup2(pdes[0], STDIN_FILENO);
300 (void)close(pdes[0]);
301 (void)close(pdes[1]);
303 /* not reading all calendar files, just set output to stdout */
307 /* set output to a temporary file, so if no output don't send mail */
308 (void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
309 if ((fd = mkstemp(path)) < 0)
311 return (fdopen(fd, "w+"));
320 int nread, pdes[2], status;
327 if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
333 (void)close(pdes[0]);
334 (void)close(pdes[1]);
337 /* child -- set stdin to pipe output */
338 if (pdes[0] != STDIN_FILENO) {
339 (void)dup2(pdes[0], STDIN_FILENO);
340 (void)close(pdes[0]);
342 (void)close(pdes[1]);
344 if (setuid(getuid()) < 0) {
345 warnx("setuid failed");
348 if (setgid(getegid()) < 0) {
349 warnx("setgid failed");
352 if (setuid(uid) < 0) {
353 warnx("setuid failed");
356 execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
357 "\"Reminder Service\"", (char *)NULL);
358 warn(_PATH_SENDMAIL);
361 /* parent -- write to pipe input */
362 (void)close(pdes[0]);
364 header[1].iov_base = header[3].iov_base = pw->pw_name;
365 header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
366 writev(pdes[1], header, 7);
367 while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
368 (void)write(pdes[1], buf, nread);
369 (void)close(pdes[1]);
370 done: (void)fclose(fp);
372 while (wait(&status) >= 0);