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.21 2007/06/09 05:54:13 grog Exp $
38 #include <sys/types.h>
55 #include "pathnames.h"
58 const char *calendarFile = "calendar.all"; /* default calendar file */
59 const char *calendarHomes[] = { ".calendar", _PATH_INCLUDE }; /* HOME */
60 const char *calendarNoMail = "nomail"; /* don't send mail if this file exist */
62 struct fixs neaster, npaskha;
64 char hdr_from[] = "From: ";
65 char hdr_to[] = " (Reminder Service)\nTo: ";
66 char hdr_subj[] = "\nSubject: ";
67 char hdr_prec[] = "'s Calendar\nPrecedence: bulk\n\n";
69 struct iovec header[] = {
79 static FILE *opencal(void);
80 static void closecal(FILE *);
92 static int d_first = -1;
94 struct event *events = NULL;
96 if ((fp = opencal()) == NULL)
98 for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) {
99 if ((p = strchr(buf, '\n')) != NULL)
102 while ((ch = getchar()) != '\n' && ch != EOF);
103 for (l = strlen(buf);
104 l > 0 && isspace((unsigned char)buf[l - 1]);
110 if (strncmp(buf, "LANG=", 5) == 0) {
111 setlocale(LC_ALL, buf + 5);
112 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
116 if (strncasecmp(buf, "Easter=", 7) == 0 && buf[7]) {
117 if (neaster.name != NULL)
119 if ((neaster.name = strdup(buf + 7)) == NULL)
120 errx(EXIT_FAILURE, "cannot allocate memory");
121 neaster.len = strlen(buf + 7);
124 if (strncasecmp(buf, "Paskha=", 7) == 0 && buf[7]) {
125 if (npaskha.name != NULL)
127 if ((npaskha.name = strdup(buf + 7)) == NULL)
128 errx(EXIT_FAILURE, "cannot allocate memory");
129 npaskha.len = strlen(buf + 7);
132 if (buf[0] != '\t') {
133 printing = isnow(buf, &month, &day, &var) ? 1 : 0;
134 if ((p = strchr(buf, '\t')) == NULL)
136 if (p > buf && p[-1] == '*')
143 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
144 tm.tm_sec = 0; /* unused */
145 tm.tm_min = 0; /* unused */
146 tm.tm_hour = 0; /* unused */
147 tm.tm_wday = 0; /* unused */
148 tm.tm_mon = month - 1;
150 tm.tm_year = tp->tm_year; /* unused */
151 strftime(dbuf, sizeof(dbuf),
152 d_first ? "%e %b" : "%b %e",
154 events = event_add(events, month, day, dbuf, var, p);
158 event_continue(events, buf);
161 event_print_all(fp, events);
166 * Functions to handle buffered calendar events.
169 event_add(struct event *events, int month, int day, char *date, int var, char *txt)
173 e = (struct event *)calloc(1, sizeof(struct event));
175 errx(EXIT_FAILURE, "event_add: cannot allocate memory");
179 e->date = strdup(date);
181 errx(EXIT_FAILURE, "event_add: cannot allocate memory");
182 e->text = strdup(txt);
184 errx(EXIT_FAILURE, "event_add: cannot allocate memory");
191 event_continue(struct event *e, char *txt)
195 text = strdup(e->text);
197 errx(EXIT_FAILURE, "event_continue: cannot allocate memory");
200 e->text = (char *)malloc(strlen(text) + strlen(txt) + 3);
202 errx(EXIT_FAILURE, "event_continue: cannot allocate memory");
203 strcpy(e->text, text);
204 strcat(e->text, "\n");
205 strcat(e->text, txt);
212 event_print_all(FILE *fp, struct event *events)
214 struct event *e, *e_next;
215 int daycount = f_dayAfter + f_dayBefore;
219 for (daycounter = 0; daycounter <= daycount; daycounter++) {
220 day = tp->tm_yday - f_dayBefore + daycounter;
221 if (day < 0) day += yrdays;
222 if (day >= yrdays) day -= yrdays;
225 while (month <= 12) {
226 if (day <= cumdays[month])
231 day -= cumdays[month];
234 fprintf(stderr,"event_print_allmonth: %d, day: %d\n",month,day);
237 for (e = events; e != NULL; e = e_next ) {
240 if (month != e->month || day != e->day)
243 fprintf(fp, "%s%c%s\n", e->date,
244 e->var ? '*' : ' ', e->text);
250 getfield(char *p, char **endp, int *flags)
255 for (; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p)
257 if (*p == '*') { /* `*' is current month */
260 return(tp->tm_mon + 1);
262 if (isdigit((unsigned char)*p)) {
263 val = strtol(p, &p, 10); /* if 0, it's failure */
264 for (; !isdigit((unsigned char)*p)
265 && !isalpha((unsigned char)*p) && *p != '*'; ++p);
269 for (start = p; isalpha((unsigned char)*++p););
272 if (*p == '+' || *p == '-')
273 for(; isdigit((unsigned char)*++p););
279 if ((val = getmonth(start)) != 0)
283 else if ((val = getday(start)) != 0) {
286 /* variable weekday */
287 if ((var = getdayvar(start)) != 0) {
288 if (var <=5 && var >= -4)
291 printf("var: %d\n", var);
297 else if ((val = geteaster(start, tp->tm_year + 1900)) != 0)
301 else if ((val = getpaskha(start, tp->tm_year + 1900)) != 0)
309 for (*p = savech; !isdigit((unsigned char)*p)
310 && !isalpha((unsigned char)*p) && *p != '*'; ++p);
315 static char path[MAXPATHLEN];
322 int fd, found, pdes[2];
325 /* open up calendar file as stdin */
326 if (!freopen(calendarFile, "r", stdin)) {
328 if (chdir(calendarHomes[0]) != 0)
330 if (stat(calendarNoMail, &sbuf) == 0)
332 if (!freopen(calendarFile, "r", stdin))
335 chdir(getenv("HOME"));
336 for (found = i = 0; i < sizeof(calendarHomes) /
337 sizeof(calendarHomes[0]); i++)
338 if (chdir(calendarHomes[i]) == 0 &&
339 freopen(calendarFile, "r", stdin)) {
344 errx(EXIT_FAILURE, "no calendar file: ``%s''", calendarFile);
355 /* child -- stdin already setup, set stdout to pipe input */
356 if (pdes[1] != STDOUT_FILENO) {
357 dup2(pdes[1], STDOUT_FILENO);
362 if (setuid(getuid()) < 0) {
363 warnx("first setuid failed");
366 if (setgid(getegid()) < 0) {
367 warnx("setgid failed");
370 if (setuid(uid) < 0) {
371 warnx("setuid failed");
374 execl(_PATH_CPP, "cpp", "-P",
375 "-traditional", "-nostdinc", /* GCC specific opts */
376 "-I.", "-I", _PATH_INCLUDE, NULL);
380 /* parent -- set stdin to pipe output */
381 dup2(pdes[0], STDIN_FILENO);
385 /* not reading all calendar files, just set output to stdout */
389 /* set output to a temporary file, so if no output don't send mail */
390 snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
391 if ((fd = mkstemp(path)) < 0)
393 return(fdopen(fd, "w+"));
401 int nread, pdes[2], status;
408 if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
418 /* child -- set stdin to pipe output */
419 if (pdes[0] != STDIN_FILENO) {
420 dup2(pdes[0], STDIN_FILENO);
425 if (setuid(getuid()) < 0) {
426 warnx("setuid failed");
429 if (setgid(getegid()) < 0) {
430 warnx("setgid failed");
433 if (setuid(uid) < 0) {
434 warnx("setuid failed");
437 execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
438 "\"Reminder Service\"", NULL);
439 warn(_PATH_SENDMAIL);
442 /* parent -- write to pipe input */
445 header[1].iov_base = header[3].iov_base = pw->pw_name;
446 header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
447 writev(pdes[1], header, 7);
448 while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
449 write(pdes[1], buf, nread);
453 while (wait(&status) >= 0);