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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1989, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
59 #include "pathnames.h"
62 const char *calendarFile = "calendar"; /* default calendar file */
63 const char *calendarHomes[] = {".calendar", _PATH_INCLUDE}; /* HOME */
64 const char *calendarNoMail = "nomail"; /* don't sent mail if this file exist */
66 char path[MAXPATHLEN];
68 struct fixs neaster, npaskha, ncny, nfullmoon, nnewmoon;
69 struct fixs nmarequinox, nsepequinox, njunsolstice, ndecsolstice;
71 #define REPLACE(string, slen, struct_) \
72 if (strncasecmp(buf, (string), (slen)) == 0 && buf[(slen)]) { \
73 if (struct_.name != NULL) \
75 if ((struct_.name = strdup(buf + (slen))) == NULL) \
76 errx(1, "cannot allocate memory"); \
77 struct_.len = strlen(buf + (slen)); \
90 char **extradata; /* strings of 20 length */
92 static int d_first = -1;
94 struct event *events[MAXCOUNT];
98 extradata = (char **)calloc(MAXCOUNT, sizeof(char *));
99 for (i = 0; i < MAXCOUNT; i++) {
100 extradata[i] = (char *)calloc(1, 20);
110 if ((fp = opencal()) == NULL) {
114 while (fgets(buf, sizeof(buf), stdin) != NULL) {
115 if ((pp = strchr(buf, '\n')) != NULL)
118 /* Flush this line */
119 while ((ch = getchar()) != '\n' && ch != EOF);
120 for (l = strlen(buf);
121 l > 0 && isspace((unsigned char)buf[l - 1]);
128 /* Parse special definitions: LANG, Easter, Paskha etc */
129 if (strncmp(buf, "LANG=", 5) == 0) {
130 (void)setlocale(LC_ALL, buf + 5);
131 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
135 REPLACE("Easter=", 7, neaster);
136 REPLACE("Paskha=", 7, npaskha);
137 REPLACE("ChineseNewYear=", 15, ncny);
138 REPLACE("NewMoon=", 8, nnewmoon);
139 REPLACE("FullMoon=", 9, nfullmoon);
140 REPLACE("MarEquinox=", 11, nmarequinox);
141 REPLACE("SepEquinox=", 11, nsepequinox);
142 REPLACE("JunSolstice=", 12, njunsolstice);
143 REPLACE("DecSolstice=", 12, ndecsolstice);
144 if (strncmp(buf, "SEQUENCE=", 9) == 0) {
145 setnsequences(buf + 9);
150 * If the line starts with a tab, the data has to be
151 * added to the previous line
153 if (buf[0] == '\t') {
154 for (i = 0; i < count; i++)
155 event_continue(events[i], buf);
159 /* Get rid of leading spaces (non-standard) */
160 while (isspace((unsigned char)buf[0]))
161 memcpy(buf, buf + 1, strlen(buf));
163 /* No tab in the line, then not a valid line */
164 if ((pp = strchr(buf, '\t')) == NULL)
167 /* Trim spaces in front of the tab */
168 while (isspace((unsigned char)pp[-1]))
173 if ((count = parsedaymonth(buf, year, month, day, &flags,
178 /* Show error status based on return value */
180 fprintf(stderr, "Ignored: %s\n", buf);
186 /* Find the last tab */
187 while (pp[1] == '\t')
191 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
193 for (i = 0; i < count; i++) {
194 tm.tm_mon = month[i] - 1;
196 tm.tm_year = year[i] - 1900;
197 (void)strftime(dbuf, sizeof(dbuf),
198 d_first ? "%e %b" : "%b %e", &tm);
200 fprintf(stderr, "got %s\n", pp);
201 events[i] = event_add(year[i], month[i], day[i], dbuf,
202 ((flags &= F_VARIABLE) != 0) ? 1 : 0, pp,
217 int fd, found, pdes[2];
220 /* open up calendar file as stdin */
221 if (!freopen(calendarFile, "r", stdin)) {
223 if (chdir(calendarHomes[0]) != 0)
225 if (stat(calendarNoMail, &sbuf) == 0)
227 if (!freopen(calendarFile, "r", stdin))
230 char *home = getenv("HOME");
231 if (home == NULL || *home == '\0')
232 errx(1, "cannot get home directory");
233 if (chdir(home) != 0)
234 errx(1, "cannot enter home directory");
235 for (found = i = 0; i < sizeof(calendarHomes) /
236 sizeof(calendarHomes[0]); i++)
237 if (chdir(calendarHomes[i]) == 0 &&
238 freopen(calendarFile, "r", stdin)) {
244 "can't open calendar file \"%s\": %s (%d)",
245 calendarFile, strerror(errno), errno);
252 (void)close(pdes[0]);
253 (void)close(pdes[1]);
256 /* child -- stdin already setup, set stdout to pipe input */
257 if (pdes[1] != STDOUT_FILENO) {
258 (void)dup2(pdes[1], STDOUT_FILENO);
259 (void)close(pdes[1]);
261 (void)close(pdes[0]);
263 if (setuid(getuid()) < 0) {
264 warnx("first setuid failed");
267 if (setgid(getegid()) < 0) {
268 warnx("setgid failed");
271 if (setuid(uid) < 0) {
272 warnx("setuid failed");
275 execl(_PATH_CPP, "cpp", "-P",
276 "-traditional", "-nostdinc", /* GCC specific opts */
277 "-I.", "-I", _PATH_INCLUDE, (char *)NULL);
281 /* parent -- set stdin to pipe output */
282 (void)dup2(pdes[0], STDIN_FILENO);
283 (void)close(pdes[0]);
284 (void)close(pdes[1]);
286 /* not reading all calendar files, just set output to stdout */
290 /* set output to a temporary file, so if no output don't send mail */
291 (void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
292 if ((fd = mkstemp(path)) < 0)
294 return (fdopen(fd, "w+"));
302 int nread, pdes[2], status;
309 if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
315 (void)close(pdes[0]);
316 (void)close(pdes[1]);
319 /* child -- set stdin to pipe output */
320 if (pdes[0] != STDIN_FILENO) {
321 (void)dup2(pdes[0], STDIN_FILENO);
322 (void)close(pdes[0]);
324 (void)close(pdes[1]);
326 if (setuid(getuid()) < 0) {
327 warnx("setuid failed");
330 if (setgid(getegid()) < 0) {
331 warnx("setgid failed");
334 if (setuid(uid) < 0) {
335 warnx("setuid failed");
338 execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
339 "\"Reminder Service\"", (char *)NULL);
340 warn(_PATH_SENDMAIL);
343 /* parent -- write to pipe input */
344 (void)close(pdes[0]);
346 write(pdes[1], "From: \"Reminder Service\" <", 26);
347 write(pdes[1], pw->pw_name, strlen(pw->pw_name));
348 write(pdes[1], ">\nTo: <", 7);
349 write(pdes[1], pw->pw_name, strlen(pw->pw_name));
350 write(pdes[1], ">\nSubject: ", 11);
351 write(pdes[1], dayname, strlen(dayname));
352 write(pdes[1], "'s Calendar\nPrecedence: bulk\n\n", 30);
354 while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
355 (void)write(pdes[1], buf, nread);
356 (void)close(pdes[1]);
357 done: (void)fclose(fp);
359 while (wait(&status) >= 0);