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;
99 struct event *events = NULL;
101 if ((fp = opencal()) == NULL)
103 for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) {
104 if ((p = strchr(buf, '\n')) != NULL)
107 while ((ch = getchar()) != '\n' && ch != EOF);
108 for (l = strlen(buf);
109 l > 0 && isspace((unsigned char)buf[l - 1]);
115 if (strncmp(buf, "LANG=", 5) == 0) {
116 (void) setlocale(LC_ALL, buf + 5);
117 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
121 if (strncasecmp(buf, "Easter=", 7) == 0 && buf[7]) {
122 if (neaster.name != NULL)
124 if ((neaster.name = strdup(buf + 7)) == NULL)
125 errx(1, "cannot allocate memory");
126 neaster.len = strlen(buf + 7);
129 if (strncasecmp(buf, "Paskha=", 7) == 0 && buf[7]) {
130 if (npaskha.name != NULL)
132 if ((npaskha.name = strdup(buf + 7)) == NULL)
133 errx(1, "cannot allocate memory");
134 npaskha.len = strlen(buf + 7);
137 if (buf[0] != '\t') {
138 printing = isnow(buf, &month, &day, &var) ? 1 : 0;
139 if ((p = strchr(buf, '\t')) == NULL)
141 if (p > buf && p[-1] == '*')
148 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
149 tm.tm_sec = 0; /* unused */
150 tm.tm_min = 0; /* unused */
151 tm.tm_hour = 0; /* unused */
152 tm.tm_wday = 0; /* unused */
153 tm.tm_mon = month - 1;
155 tm.tm_year = tp->tm_year; /* unused */
156 (void)strftime(dbuf, sizeof(dbuf),
157 d_first ? "%e %b" : "%b %e",
159 events = event_add(events, month, day, dbuf, var, p);
163 event_continue(events, buf);
166 event_print_all(fp, events);
171 * Functions to handle buffered calendar events.
174 event_add(struct event *events, int month, int day, char *date, int var, char *txt)
178 e = (struct event *)calloc(1, sizeof(struct event));
180 errx(1, "event_add: cannot allocate memory");
184 e->date = strdup(date);
186 errx(1, "event_add: cannot allocate memory");
187 e->text = strdup(txt);
189 errx(1, "event_add: cannot allocate memory");
196 event_continue(struct event *e, char *txt)
200 text = strdup(e->text);
202 errx(1, "event_continue: cannot allocate memory");
205 e->text = (char *)malloc(strlen(text) + strlen(txt) + 3);
207 errx(1, "event_continue: cannot allocate memory");
208 strcpy(e->text, text);
209 strcat(e->text, "\n");
210 strcat(e->text, txt);
217 event_print_all(FILE *fp, struct event *events)
219 struct event *e, *e_next;
220 int daycount = f_dayAfter + f_dayBefore;
224 for (daycounter = 0; daycounter <= daycount; daycounter++) {
225 day = tp->tm_yday - f_dayBefore + daycounter;
226 if (day < 0) day += yrdays;
227 if (day >= yrdays) day -= yrdays;
230 while (month <= 12) {
231 if (day <= cumdays[month])
236 day -= cumdays[month];
239 fprintf(stderr,"event_print_allmonth: %d, day: %d\n",month,day);
242 for (e = events; e != NULL; e = e_next ) {
245 if (month != e->month || day != e->day)
248 (void)fprintf(fp, "%s%c%s\n", e->date,
249 e->var ? '*' : ' ', e->text);
255 getfield(char *p, char **endp, int *flags)
260 for (; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p)
262 if (*p == '*') { /* `*' is current month */
265 return (tp->tm_mon + 1);
267 if (isdigit((unsigned char)*p)) {
268 val = strtol(p, &p, 10); /* if 0, it's failure */
269 for (; !isdigit((unsigned char)*p)
270 && !isalpha((unsigned char)*p) && *p != '*'; ++p);
274 for (start = p; isalpha((unsigned char)*++p););
277 if (*p == '+' || *p == '-')
278 for(; isdigit((unsigned char)*++p););
284 if ((val = getmonth(start)) != 0)
288 else if ((val = getday(start)) != 0) {
291 /* variable weekday */
292 if ((var = getdayvar(start)) != 0) {
293 if (var <=5 && var >= -4)
296 printf("var: %d\n", var);
302 else if ((val = geteaster(start, tp->tm_year + 1900)) != 0)
306 else if ((val = getpaskha(start, tp->tm_year + 1900)) != 0)
314 for (*p = savech; !isdigit((unsigned char)*p)
315 && !isalpha((unsigned char)*p) && *p != '*'; ++p);
320 char path[MAXPATHLEN];
327 int fd, found, pdes[2];
330 /* open up calendar file as stdin */
331 if (!freopen(calendarFile, "r", stdin)) {
333 if (chdir(calendarHomes[0]) != 0)
335 if (stat(calendarNoMail, &sbuf) == 0)
337 if (!freopen(calendarFile, "r", stdin))
340 chdir(getenv("HOME"));
341 for (found = i = 0; i < sizeof(calendarHomes) /
342 sizeof(calendarHomes[0]); i++)
343 if (chdir(calendarHomes[i]) == 0 &&
344 freopen(calendarFile, "r", stdin)) {
349 errx(1, "no calendar file: ``%s''", calendarFile);
356 (void)close(pdes[0]);
357 (void)close(pdes[1]);
360 /* child -- stdin already setup, set stdout to pipe input */
361 if (pdes[1] != STDOUT_FILENO) {
362 (void)dup2(pdes[1], STDOUT_FILENO);
363 (void)close(pdes[1]);
365 (void)close(pdes[0]);
367 if (setuid(getuid()) < 0) {
368 warnx("first setuid failed");
371 if (setgid(getegid()) < 0) {
372 warnx("setgid failed");
375 if (setuid(uid) < 0) {
376 warnx("setuid failed");
379 execl(_PATH_CPP, "cpp", "-P",
380 "-traditional", "-nostdinc", /* GCC specific opts */
381 "-I.", "-I", _PATH_INCLUDE, (char *)NULL);
385 /* parent -- set stdin to pipe output */
386 (void)dup2(pdes[0], STDIN_FILENO);
387 (void)close(pdes[0]);
388 (void)close(pdes[1]);
390 /* not reading all calendar files, just set output to stdout */
394 /* set output to a temporary file, so if no output don't send mail */
395 (void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
396 if ((fd = mkstemp(path)) < 0)
398 return (fdopen(fd, "w+"));
406 int nread, pdes[2], status;
413 if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
419 (void)close(pdes[0]);
420 (void)close(pdes[1]);
423 /* child -- set stdin to pipe output */
424 if (pdes[0] != STDIN_FILENO) {
425 (void)dup2(pdes[0], STDIN_FILENO);
426 (void)close(pdes[0]);
428 (void)close(pdes[1]);
430 if (setuid(getuid()) < 0) {
431 warnx("setuid failed");
434 if (setgid(getegid()) < 0) {
435 warnx("setgid failed");
438 if (setuid(uid) < 0) {
439 warnx("setuid failed");
442 execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
443 "\"Reminder Service\"", (char *)NULL);
444 warn(_PATH_SENDMAIL);
447 /* parent -- write to pipe input */
448 (void)close(pdes[0]);
450 header[1].iov_base = header[3].iov_base = pw->pw_name;
451 header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
452 writev(pdes[1], header, 7);
453 while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
454 (void)write(pdes[1], buf, nread);
455 (void)close(pdes[1]);
456 done: (void)fclose(fp);
458 while (wait(&status) >= 0);