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.7 2007/09/24 20:49:09 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
[] = {
80 static FILE *opencal(void);
81 static void closecal(FILE *);
93 static int d_first
= -1;
95 struct event
*events
= NULL
;
97 if ((fp
= opencal()) == NULL
)
99 for (printing
= 0; fgets(buf
, sizeof(buf
), stdin
) != NULL
;) {
100 if ((p
= strchr(buf
, '\n')) != NULL
)
103 while ((ch
= getchar()) != '\n' && ch
!= EOF
);
104 for (l
= strlen(buf
);
105 l
> 0 && isspace((unsigned char)buf
[l
- 1]);
111 if (strncmp(buf
, "LANG=", 5) == 0) {
112 setlocale(LC_ALL
, buf
+ 5);
113 d_first
= (*nl_langinfo(D_MD_ORDER
) == 'd');
117 if (strncasecmp(buf
, "Easter=", 7) == 0 && buf
[7]) {
118 if (neaster
.name
!= NULL
)
120 if ((neaster
.name
= strdup(buf
+ 7)) == NULL
)
121 errx(EXIT_FAILURE
, "cannot allocate memory");
122 neaster
.len
= strlen(buf
+ 7);
125 if (strncasecmp(buf
, "Paskha=", 7) == 0 && buf
[7]) {
126 if (npaskha
.name
!= NULL
)
128 if ((npaskha
.name
= strdup(buf
+ 7)) == NULL
)
129 errx(EXIT_FAILURE
, "cannot allocate memory");
130 npaskha
.len
= strlen(buf
+ 7);
133 if (buf
[0] != '\t') {
134 printing
= isnow(buf
, &month
, &day
, &var
) ? 1 : 0;
135 if ((p
= strchr(buf
, '\t')) == NULL
)
137 if (p
> buf
&& p
[-1] == '*')
144 d_first
= (*nl_langinfo(D_MD_ORDER
) == 'd');
145 tm
.tm_sec
= 0; /* unused */
146 tm
.tm_min
= 0; /* unused */
147 tm
.tm_hour
= 0; /* unused */
148 tm
.tm_wday
= 0; /* unused */
149 tm
.tm_mon
= month
- 1;
151 tm
.tm_year
= tp
->tm_year
; /* unused */
152 strftime(dbuf
, sizeof(dbuf
),
153 d_first
? "%e %b" : "%b %e",
155 events
= event_add(events
, month
, day
, dbuf
, var
, p
);
159 event_continue(events
, buf
);
162 event_print_all(fp
, events
);
167 * Functions to handle buffered calendar events.
170 event_add(struct event
*events
, int month
, int day
, char *date
, int var
, char *txt
)
174 e
= (struct event
*)calloc(1, sizeof(struct event
));
176 errx(EXIT_FAILURE
, "event_add: cannot allocate memory");
180 e
->date
= strdup(date
);
182 errx(EXIT_FAILURE
, "event_add: cannot allocate memory");
183 e
->text
= strdup(txt
);
185 errx(EXIT_FAILURE
, "event_add: cannot allocate memory");
192 event_continue(struct event
*e
, char *txt
)
196 text
= strdup(e
->text
);
198 errx(EXIT_FAILURE
, "event_continue: cannot allocate memory");
201 e
->text
= (char *)malloc(strlen(text
) + strlen(txt
) + 3);
203 errx(EXIT_FAILURE
, "event_continue: cannot allocate memory");
204 strcpy(e
->text
, text
);
205 strcat(e
->text
, "\n");
206 strcat(e
->text
, txt
);
213 event_print_all(FILE *fp
, struct event
*events
)
215 struct event
*e
, *e_next
;
216 int daycount
= f_dayAfter
+ f_dayBefore
;
220 for (daycounter
= 0; daycounter
<= daycount
; daycounter
++) {
221 day
= tp
->tm_yday
- f_dayBefore
+ daycounter
;
222 if (day
< 0) day
+= yrdays
;
223 if (day
>= yrdays
) day
-= yrdays
;
226 while (month
<= 12) {
227 if (day
<= cumdays
[month
])
232 day
-= cumdays
[month
];
235 fprintf(stderr
,"event_print_allmonth: %d, day: %d\n",month
,day
);
238 for (e
= events
; e
!= NULL
; e
= e_next
) {
241 if (month
!= e
->month
|| day
!= e
->day
)
244 fprintf(fp
, "%s%c%s\n", e
->date
,
245 e
->var
? '*' : ' ', e
->text
);
251 getfield(char *p
, char **endp
, int *flags
)
256 for (; !isdigit((unsigned char)*p
) && !isalpha((unsigned char)*p
)
258 if (*p
== '*') { /* `*' is current month */
261 return(tp
->tm_mon
+ 1);
263 if (isdigit((unsigned char)*p
)) {
264 val
= strtol(p
, &p
, 10); /* if 0, it's failure */
265 for (; !isdigit((unsigned char)*p
)
266 && !isalpha((unsigned char)*p
) && *p
!= '*'; ++p
);
270 for (start
= p
; isalpha((unsigned char)*++p
););
273 if (*p
== '+' || *p
== '-')
274 for(; isdigit((unsigned char)*++p
););
280 if ((val
= getmonth(start
)) != 0)
284 else if ((val
= getday(start
)) != 0) {
287 /* variable weekday */
288 if ((var
= getdayvar(start
)) != 0) {
289 if (var
<=5 && var
>= -4)
292 printf("var: %d\n", var
);
298 else if ((val
= geteaster(start
, tp
->tm_year
+ 1900)) != 0)
302 else if ((val
= getpaskha(start
, tp
->tm_year
+ 1900)) != 0)
310 for (*p
= savech
; !isdigit((unsigned char)*p
)
311 && !isalpha((unsigned char)*p
) && *p
!= '*'; ++p
);
316 static char path
[MAXPATHLEN
];
323 int fd
, found
, pdes
[2];
326 /* open up calendar file as stdin */
327 if (!freopen(calendarFile
, "r", stdin
)) {
329 if (chdir(calendarHomes
[0]) != 0)
331 if (stat(calendarNoMail
, &sbuf
) == 0)
333 if (!freopen(calendarFile
, "r", stdin
))
336 chdir(getenv("HOME"));
337 for (found
= i
= 0; i
< sizeof(calendarHomes
) /
338 sizeof(calendarHomes
[0]); i
++)
339 if (chdir(calendarHomes
[i
]) == 0 &&
340 freopen(calendarFile
, "r", stdin
)) {
345 errx(EXIT_FAILURE
, "no calendar file: ``%s''", calendarFile
);
356 /* child -- stdin already setup, set stdout to pipe input */
357 if (pdes
[1] != STDOUT_FILENO
) {
358 dup2(pdes
[1], STDOUT_FILENO
);
363 if (setuid(getuid()) < 0) {
364 warnx("first setuid failed");
367 if (setgid(getegid()) < 0) {
368 warnx("setgid failed");
371 if (setuid(uid
) < 0) {
372 warnx("setuid failed");
375 execl(_PATH_CPP
, "cpp", "-P",
376 "-traditional", "-nostdinc", /* GCC specific opts */
377 "-I.", "-I", _PATH_INCLUDE
, (char *)NULL
);
381 /* parent -- set stdin to pipe output */
382 dup2(pdes
[0], STDIN_FILENO
);
386 /* not reading all calendar files, just set output to stdout */
390 /* set output to a temporary file, so if no output don't send mail */
391 snprintf(path
, sizeof(path
), "%s/_calXXXXXX", _PATH_TMP
);
392 if ((fd
= mkstemp(path
)) < 0)
394 return(fdopen(fd
, "w+"));
402 int nread
, pdes
[2], status
;
409 if (fstat(fileno(fp
), &sbuf
) || !sbuf
.st_size
)
419 /* child -- set stdin to pipe output */
420 if (pdes
[0] != STDIN_FILENO
) {
421 dup2(pdes
[0], STDIN_FILENO
);
426 if (setuid(getuid()) < 0) {
427 warnx("setuid failed");
430 if (setgid(getegid()) < 0) {
431 warnx("setgid failed");
434 if (setuid(uid
) < 0) {
435 warnx("setuid failed");
438 execl(_PATH_SENDMAIL
, "sendmail", "-i", "-t", "-F",
439 "\"Reminder Service\"", (char *)NULL
);
440 warn(_PATH_SENDMAIL
);
443 /* parent -- write to pipe input */
446 header
[1].iov_base
= header
[3].iov_base
= pw
->pw_name
;
447 header
[1].iov_len
= header
[3].iov_len
= strlen(pw
->pw_name
);
448 writev(pdes
[1], header
, 7);
449 while ((nread
= read(fileno(fp
), buf
, sizeof(buf
))) > 0)
450 write(pdes
[1], buf
, nread
);
454 while (wait(&status
) >= 0);