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 $
36 * $DragonFly: src/usr.bin/calendar/io.c,v 1.6 2007/09/24 20:31:44 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;
92 struct event *events = NULL;
94 if ((fp = opencal()) == NULL)
96 for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) {
97 if ((p = strchr(buf, '\n')) != NULL)
100 while ((ch = getchar()) != '\n' && ch != EOF);
101 for (l = strlen(buf);
102 l > 0 && isspace((unsigned char)buf[l - 1]);
108 if (strncmp(buf, "LANG=", 5) == 0) {
109 setlocale(LC_ALL, buf + 5);
110 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
114 if (strncasecmp(buf, "Easter=", 7) == 0 && buf[7]) {
115 if (neaster.name != NULL)
117 if ((neaster.name = strdup(buf + 7)) == NULL)
118 errx(EXIT_FAILURE, "cannot allocate memory");
119 neaster.len = strlen(buf + 7);
122 if (strncasecmp(buf, "Paskha=", 7) == 0 && buf[7]) {
123 if (npaskha.name != NULL)
125 if ((npaskha.name = strdup(buf + 7)) == NULL)
126 errx(EXIT_FAILURE, "cannot allocate memory");
127 npaskha.len = strlen(buf + 7);
130 if (buf[0] != '\t') {
131 printing = isnow(buf, &month, &day, &var) ? 1 : 0;
132 if ((p = strchr(buf, '\t')) == NULL)
134 if (p > buf && p[-1] == '*')
141 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
142 tm.tm_sec = 0; /* unused */
143 tm.tm_min = 0; /* unused */
144 tm.tm_hour = 0; /* unused */
145 tm.tm_wday = 0; /* unused */
146 tm.tm_mon = month - 1;
148 tm.tm_year = tp->tm_year; /* unused */
149 strftime(dbuf, sizeof(dbuf),
150 d_first ? "%e %b" : "%b %e",
152 events = event_add(events, month, day, dbuf, var, p);
156 event_continue(events, buf);
159 event_print_all(fp, events);
164 * Functions to handle buffered calendar events.
167 event_add(struct event *events, int month, int day, char *date, int var, char *txt)
171 e = (struct event *)calloc(1, sizeof(struct event));
173 errx(EXIT_FAILURE, "event_add: cannot allocate memory");
177 e->date = strdup(date);
179 errx(EXIT_FAILURE, "event_add: cannot allocate memory");
180 e->text = strdup(txt);
182 errx(EXIT_FAILURE, "event_add: cannot allocate memory");
189 event_continue(struct event *e, char *txt)
193 text = strdup(e->text);
195 errx(EXIT_FAILURE, "event_continue: cannot allocate memory");
198 e->text = (char *)malloc(strlen(text) + strlen(txt) + 3);
200 errx(EXIT_FAILURE, "event_continue: cannot allocate memory");
201 strcpy(e->text, text);
202 strcat(e->text, "\n");
203 strcat(e->text, txt);
210 event_print_all(FILE *fp, struct event *events)
212 struct event *e, *e_next;
213 int daycount = f_dayAfter + f_dayBefore;
217 for (daycounter = 0; daycounter <= daycount; daycounter++) {
218 day = tp->tm_yday - f_dayBefore + daycounter;
219 if (day < 0) day += yrdays;
220 if (day >= yrdays) day -= yrdays;
223 while (month <= 12) {
224 if (day <= cumdays[month])
229 day -= cumdays[month];
232 fprintf(stderr,"event_print_allmonth: %d, day: %d\n",month,day);
235 for (e = events; e != NULL; e = e_next ) {
238 if (month != e->month || day != e->day)
241 fprintf(fp, "%s%c%s\n", e->date,
242 e->var ? '*' : ' ', e->text);
248 getfield(char *p, char **endp, int *flags)
253 for (; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p)
255 if (*p == '*') { /* `*' is current month */
258 return(tp->tm_mon + 1);
260 if (isdigit((unsigned char)*p)) {
261 val = strtol(p, &p, 10); /* if 0, it's failure */
262 for (; !isdigit((unsigned char)*p)
263 && !isalpha((unsigned char)*p) && *p != '*'; ++p);
267 for (start = p; isalpha((unsigned char)*++p););
270 if (*p == '+' || *p == '-')
271 for(; isdigit((unsigned char)*++p););
277 if ((val = getmonth(start)) != 0)
281 else if ((val = getday(start)) != 0) {
284 /* variable weekday */
285 if ((var = getdayvar(start)) != 0) {
286 if (var <=5 && var >= -4)
289 printf("var: %d\n", var);
295 else if ((val = geteaster(start, tp->tm_year + 1900)) != 0)
299 else if ((val = getpaskha(start, tp->tm_year + 1900)) != 0)
307 for (*p = savech; !isdigit((unsigned char)*p)
308 && !isalpha((unsigned char)*p) && *p != '*'; ++p);
313 static char path[MAXPATHLEN];
320 int fd, found, pdes[2];
323 /* open up calendar file as stdin */
324 if (!freopen(calendarFile, "r", stdin)) {
326 if (chdir(calendarHomes[0]) != 0)
328 if (stat(calendarNoMail, &sbuf) == 0)
330 if (!freopen(calendarFile, "r", stdin))
333 chdir(getenv("HOME"));
334 for (found = i = 0; i < sizeof(calendarHomes) /
335 sizeof(calendarHomes[0]); i++)
336 if (chdir(calendarHomes[i]) == 0 &&
337 freopen(calendarFile, "r", stdin)) {
342 errx(EXIT_FAILURE, "no calendar file: ``%s''", calendarFile);
353 /* child -- stdin already setup, set stdout to pipe input */
354 if (pdes[1] != STDOUT_FILENO) {
355 dup2(pdes[1], STDOUT_FILENO);
360 if (setuid(getuid()) < 0) {
361 warnx("first setuid failed");
364 if (setgid(getegid()) < 0) {
365 warnx("setgid failed");
368 if (setuid(uid) < 0) {
369 warnx("setuid failed");
372 execl(_PATH_CPP, "cpp", "-P",
373 "-traditional", "-nostdinc", /* GCC specific opts */
374 "-I.", "-I", _PATH_INCLUDE, (char *)NULL);
378 /* parent -- set stdin to pipe output */
379 dup2(pdes[0], STDIN_FILENO);
383 /* not reading all calendar files, just set output to stdout */
387 /* set output to a temporary file, so if no output don't send mail */
388 snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
389 if ((fd = mkstemp(path)) < 0)
391 return(fdopen(fd, "w+"));
399 int nread, pdes[2], status;
406 if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
416 /* child -- set stdin to pipe output */
417 if (pdes[0] != STDIN_FILENO) {
418 dup2(pdes[0], STDIN_FILENO);
423 if (setuid(getuid()) < 0) {
424 warnx("setuid failed");
427 if (setgid(getegid()) < 0) {
428 warnx("setgid failed");
431 if (setuid(uid) < 0) {
432 warnx("setuid failed");
435 execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
436 "\"Reminder Service\"", (char *)NULL);
437 warn(_PATH_SENDMAIL);
440 /* parent -- write to pipe input */
443 header[1].iov_base = header[3].iov_base = pw->pw_name;
444 header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
445 writev(pdes[1], header, 7);
446 while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
447 write(pdes[1], buf, nread);
451 while (wait(&status) >= 0);