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
33 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)calendar.c 8.3 (Berkeley) 3/25/94
35 * $FreeBSD: src/usr.bin/calendar/io.c,v 1.13.2.3 2002/08/26 00:32:46 jmallett Exp $
36 * $DragonFly: src/usr.bin/calendar/io.c,v 1.5 2006/09/16 18:38:00 pavalos Exp $
39 #include <sys/types.h>
56 #include "pathnames.h"
59 const char *calendarFile = "calendar.all"; /* default calendar file */
60 const char *calendarHomes[] = { ".calendar", _PATH_INCLUDE }; /* HOME */
61 const char *calendarNoMail = "nomail"; /* don't send mail if this file exist */
63 struct fixs neaster, npaskha;
65 char hdr_from[] = "From: ";
66 char hdr_to[] = " (Reminder Service)\nTo: ";
67 char hdr_subj[] = "\nSubject: ";
68 char hdr_prec[] = "'s Calendar\nPrecedence: bulk\n\n";
70 struct iovec header[] = {
90 static int d_first = -1;
93 if ((fp = opencal()) == NULL)
95 for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) {
96 if ((p = strchr(buf, '\n')) != NULL)
99 while ((ch = getchar()) != '\n' && ch != EOF);
100 for (l = strlen(buf);
101 l > 0 && isspace((unsigned char)buf[l - 1]);
107 if (strncmp(buf, "LANG=", 5) == 0) {
108 setlocale(LC_ALL, buf + 5);
109 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
113 if (strncasecmp(buf, "Easter=", 7) == 0 && buf[7]) {
114 if (neaster.name != NULL)
116 if ((neaster.name = strdup(buf + 7)) == NULL)
117 errx(EXIT_FAILURE, "cannot allocate memory");
118 neaster.len = strlen(buf + 7);
121 if (strncasecmp(buf, "Paskha=", 7) == 0 && buf[7]) {
122 if (npaskha.name != NULL)
124 if ((npaskha.name = strdup(buf + 7)) == NULL)
125 errx(EXIT_FAILURE, "cannot allocate memory");
126 npaskha.len = strlen(buf + 7);
129 if (buf[0] != '\t') {
130 printing = isnow(buf, &month, &day, &var) ? 1 : 0;
131 if ((p = strchr(buf, '\t')) == NULL)
133 if (p > buf && p[-1] == '*')
140 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
141 tm.tm_sec = 0; /* unused */
142 tm.tm_min = 0; /* unused */
143 tm.tm_hour = 0; /* unused */
144 tm.tm_wday = 0; /* unused */
145 tm.tm_mon = month - 1;
147 tm.tm_year = tp->tm_year; /* unused */
148 strftime(dbuf, sizeof(dbuf),
149 d_first ? "%e %b" : "%b %e",
151 fprintf(fp, "%s%c%s\n", dbuf,
156 fprintf(fp, "%s\n", buf);
162 getfield(char *p, char **endp, int *flags)
167 for (; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p) && *p != '*'; ++p);
168 if (*p == '*') { /* `*' is current month */
171 return(tp->tm_mon + 1);
173 if (isdigit((unsigned char)*p)) {
174 val = strtol(p, &p, 10); /* if 0, it's failure */
175 for (; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p) && *p != '*'; ++p);
179 for (start = p; isalpha((unsigned char)*++p););
182 if (*p == '+' || *p == '-')
183 for(; isdigit((unsigned char)*++p););
189 if ((val = getmonth(start)) != 0)
193 else if ((val = getday(start)) != 0) {
196 /* variable weekday */
197 if ((var = getdayvar(start)) != 0) {
198 if (var <=5 && var >= -4)
201 printf("var: %d\n", var);
207 else if ((val = geteaster(start, tp->tm_year + 1900)) != 0)
211 else if ((val = getpaskha(start, tp->tm_year + 1900)) != 0)
219 for (*p = savech; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p) && *p != '*'; ++p);
224 static char path[MAXPATHLEN];
231 int fd, found, pdes[2];
234 /* open up calendar file as stdin */
235 if (!freopen(calendarFile, "r", stdin)) {
237 if (chdir(calendarHomes[0]) != 0)
239 if (stat(calendarNoMail, &sbuf) == 0)
241 if (!freopen(calendarFile, "r", stdin))
244 chdir(getenv("HOME"));
245 for (found = i = 0; i < sizeof(calendarHomes) /
246 sizeof(calendarHomes[0]); i++)
247 if (chdir(calendarHomes[i]) == 0 &&
248 freopen(calendarFile, "r", stdin)) {
253 errx(EXIT_FAILURE, "no calendar file: ``%s''", calendarFile);
264 /* child -- stdin already setup, set stdout to pipe input */
265 if (pdes[1] != STDOUT_FILENO) {
266 dup2(pdes[1], STDOUT_FILENO);
271 if (setuid(getuid()) < 0) {
272 warnx("first setuid failed");
275 if (setgid(getegid()) < 0) {
276 warnx("setgid failed");
279 if (setuid(uid) < 0) {
280 warnx("setuid failed");
283 execl(_PATH_CPP, "cpp", "-P",
284 "-traditional", "-nostdinc", /* GCC specific opts */
285 "-I.", "-I", _PATH_INCLUDE, (char *)NULL);
289 /* parent -- set stdin to pipe output */
290 dup2(pdes[0], STDIN_FILENO);
294 /* not reading all calendar files, just set output to stdout */
298 /* set output to a temporary file, so if no output don't send mail */
299 snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
300 if ((fd = mkstemp(path)) < 0)
302 return(fdopen(fd, "w+"));
310 int nread, pdes[2], status;
317 if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
327 /* child -- set stdin to pipe output */
328 if (pdes[0] != STDIN_FILENO) {
329 dup2(pdes[0], STDIN_FILENO);
334 if (setuid(getuid()) < 0) {
335 warnx("setuid failed");
338 if (setgid(getegid()) < 0) {
339 warnx("setgid failed");
342 if (setuid(uid) < 0) {
343 warnx("setuid failed");
346 execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
347 "\"Reminder Service\"", (char *)NULL);
348 warn(_PATH_SENDMAIL);
351 /* parent -- write to pipe input */
354 header[1].iov_base = header[3].iov_base = pw->pw_name;
355 header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
356 writev(pdes[1], header, 7);
357 while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
358 write(pdes[1], buf, nread);
362 while (wait(&status) >= 0);