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 char path[MAXPATHLEN];
76 struct fixs neaster, npaskha;
78 struct iovec header[] = {
81 {" (Reminder Service)\nTo: ", 24},
85 {"'s Calendar\nPrecedence: bulk\n\n", 30},
98 static int d_first = -1;
100 struct event *events = NULL;
102 if ((fp = opencal()) == NULL)
104 for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) {
105 if ((p = strchr(buf, '\n')) != NULL)
108 while ((ch = getchar()) != '\n' && ch != EOF);
109 for (l = strlen(buf);
110 l > 0 && isspace((unsigned char)buf[l - 1]);
116 if (strncmp(buf, "LANG=", 5) == 0) {
117 (void)setlocale(LC_ALL, buf + 5);
118 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
122 if (strncasecmp(buf, "Easter=", 7) == 0 && buf[7]) {
123 if (neaster.name != NULL)
125 if ((neaster.name = strdup(buf + 7)) == NULL)
126 errx(1, "cannot allocate memory");
127 neaster.len = strlen(buf + 7);
130 if (strncasecmp(buf, "Paskha=", 7) == 0 && buf[7]) {
131 if (npaskha.name != NULL)
133 if ((npaskha.name = strdup(buf + 7)) == NULL)
134 errx(1, "cannot allocate memory");
135 npaskha.len = strlen(buf + 7);
138 if (buf[0] != '\t') {
139 printing = isnow(buf, &month, &day, &var) ? 1 : 0;
140 if ((p = strchr(buf, '\t')) == NULL)
142 if (p > buf && p[-1] == '*')
150 (*nl_langinfo(D_MD_ORDER) == 'd');
151 tm.tm_sec = 0; /* unused */
152 tm.tm_min = 0; /* unused */
153 tm.tm_hour = 0; /* unused */
154 tm.tm_wday = 0; /* unused */
155 tm.tm_mon = month - 1;
157 tm.tm_year = tp->tm_year; /* unused */
158 (void)strftime(dbuf, sizeof(dbuf),
159 d_first ? "%e %b" : "%b %e", &tm);
160 events = event_add(events, month, day, dbuf,
165 event_continue(events, buf);
169 event_print_all(fp, events);
174 event_add(struct event *events, int month, int day,
175 char *date, int var, char *txt)
180 * Creating a new event:
181 * - Create a new event
182 * - Copy the machine readable day and month
183 * - Copy the human readable and language specific date
184 * - Copy the text of the event
186 e = (struct event *)calloc(1, sizeof(struct event));
188 errx(1, "event_add: cannot allocate memory");
192 e->date = strdup(date);
194 errx(1, "event_add: cannot allocate memory");
195 e->text = strdup(txt);
197 errx(1, "event_add: cannot allocate memory");
204 event_continue(struct event *e, char *txt)
209 * Adding text to the event:
210 * - Save a copy of the old text (unknown length, so strdup())
211 * - Allocate enough space for old text + \n + new text + 0
212 * - Store the old text + \n + new text
213 * - Destroy the saved copy.
215 text = strdup(e->text);
217 errx(1, "event_continue: cannot allocate memory");
220 e->text = (char *)malloc(strlen(text) + strlen(txt) + 3);
222 errx(1, "event_continue: cannot allocate memory");
223 strcpy(e->text, text);
224 strcat(e->text, "\n");
225 strcat(e->text, txt);
232 event_print_all(FILE *fp, struct event *events)
234 struct event *e, *e_next;
240 * - We know the number of days to be counted (f_dayAfter + f_dayBefore)
241 * - We know the current day of the year ("now" - f_dayBefore + counter)
242 * - We know the number of days in the year (yrdays, set in settime())
243 * - So we know the date on which the current daycounter is on the
244 * calendar in days and months.
245 * - Go through the list of events, and print all matching dates
247 for (daycounter = 0; daycounter <= f_dayAfter + f_dayBefore;
249 day = tp->tm_yday - f_dayBefore + daycounter;
256 * When we know the day of the year, we can determine the day
257 * of the month and the month.
260 while (month <= 12) {
261 if (day <= cumdays[month])
266 day -= cumdays[month];
269 fprintf(stderr, "event_print_allmonth: %d, day: %d\n",
274 * Go through all events and print the text of the matching
277 for (e = events; e != NULL; e = e_next) {
280 if (month != e->month || day != e->day)
283 (void)fprintf(fp, "%s%c%s\n", e->date,
284 e->var ? '*' : ' ', e->text);
290 getfield(char *p, char **endp, int *flags)
295 for (; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p)
298 if (*p == '*') { /* `*' is current month */
301 return (tp->tm_mon + 1);
303 if (isdigit((unsigned char)*p)) {
304 val = strtol(p, &p, 10); /* if 0, it's failure */
305 for (; !isdigit((unsigned char)*p)
306 && !isalpha((unsigned char)*p) && *p != '*'; ++p);
310 for (start = p; isalpha((unsigned char)*++p););
313 if (*p == '+' || *p == '-')
314 for(; isdigit((unsigned char)*++p);)
321 if ((val = getmonth(start)) != 0)
325 else if ((val = getday(start)) != 0) {
328 /* variable weekday */
329 if ((var = getdayvar(start)) != 0) {
330 if (var <= 5 && var >= -4)
333 printf("var: %d\n", var);
339 else if ((val = geteaster(start, tp->tm_year + 1900)) != 0)
343 else if ((val = getpaskha(start, tp->tm_year + 1900)) != 0)
351 for (*p = savech; !isdigit((unsigned char)*p)
352 && !isalpha((unsigned char)*p) && *p != '*'; ++p)
363 int fd, found, pdes[2];
366 /* open up calendar file as stdin */
367 if (!freopen(calendarFile, "r", stdin)) {
369 if (chdir(calendarHomes[0]) != 0)
371 if (stat(calendarNoMail, &sbuf) == 0)
373 if (!freopen(calendarFile, "r", stdin))
376 char *home = getenv("HOME");
377 if (home == NULL || *home == '\0')
378 errx(1, "cannot get home directory");
380 for (found = i = 0; i < sizeof(calendarHomes) /
381 sizeof(calendarHomes[0]); i++)
382 if (chdir(calendarHomes[i]) == 0 &&
383 freopen(calendarFile, "r", stdin)) {
389 "can't open calendar file \"%s\": %s (%d)",
390 calendarFile, strerror(errno), errno);
397 (void)close(pdes[0]);
398 (void)close(pdes[1]);
401 /* child -- stdin already setup, set stdout to pipe input */
402 if (pdes[1] != STDOUT_FILENO) {
403 (void)dup2(pdes[1], STDOUT_FILENO);
404 (void)close(pdes[1]);
406 (void)close(pdes[0]);
408 if (setuid(getuid()) < 0) {
409 warnx("first setuid failed");
412 if (setgid(getegid()) < 0) {
413 warnx("setgid failed");
416 if (setuid(uid) < 0) {
417 warnx("setuid failed");
420 execl(_PATH_CPP, "cpp", "-P",
421 "-traditional", "-nostdinc", /* GCC specific opts */
422 "-I.", "-I", _PATH_INCLUDE, (char *)NULL);
426 /* parent -- set stdin to pipe output */
427 (void)dup2(pdes[0], STDIN_FILENO);
428 (void)close(pdes[0]);
429 (void)close(pdes[1]);
431 /* not reading all calendar files, just set output to stdout */
435 /* set output to a temporary file, so if no output don't send mail */
436 (void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
437 if ((fd = mkstemp(path)) < 0)
439 return (fdopen(fd, "w+"));
447 int nread, pdes[2], status;
454 if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
460 (void)close(pdes[0]);
461 (void)close(pdes[1]);
464 /* child -- set stdin to pipe output */
465 if (pdes[0] != STDIN_FILENO) {
466 (void)dup2(pdes[0], STDIN_FILENO);
467 (void)close(pdes[0]);
469 (void)close(pdes[1]);
471 if (setuid(getuid()) < 0) {
472 warnx("setuid failed");
475 if (setgid(getegid()) < 0) {
476 warnx("setgid failed");
479 if (setuid(uid) < 0) {
480 warnx("setuid failed");
483 execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
484 "\"Reminder Service\"", (char *)NULL);
485 warn(_PATH_SENDMAIL);
488 /* parent -- write to pipe input */
489 (void)close(pdes[0]);
491 header[1].iov_base = header[3].iov_base = pw->pw_name;
492 header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
493 writev(pdes[1], header, 7);
494 while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
495 (void)write(pdes[1], buf, nread);
496 (void)close(pdes[1]);
497 done: (void)fclose(fp);
499 while (wait(&status) >= 0);