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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)calendar.c 8.3 (Berkeley) 3/25/94
31 * $FreeBSD: src/usr.bin/calendar/io.c,v 1.21 2007/06/09 05:54:13 grog Exp $
34 #include <sys/types.h>
51 #include "pathnames.h"
54 const char *calendarFile
= "calendar.all"; /* default calendar file */
55 const char *calendarHomes
[] = { ".calendar", _PATH_INCLUDE
}; /* HOME */
56 const char *calendarNoMail
= "nomail"; /* don't send mail if this file exist */
58 struct fixs neaster
, npaskha
;
60 char hdr_from
[] = "From: ";
61 char hdr_to
[] = " (Reminder Service)\nTo: ";
62 char hdr_subj
[] = "\nSubject: ";
63 char hdr_prec
[] = "'s Calendar\nPrecedence: bulk\n\n";
65 struct iovec header
[] = {
75 static FILE *opencal(void);
76 static void closecal(FILE *);
88 static int d_first
= -1;
90 struct event
*events
= NULL
;
92 if ((fp
= opencal()) == NULL
)
94 for (printing
= 0; fgets(buf
, sizeof(buf
), stdin
) != NULL
;) {
95 if ((p
= strchr(buf
, '\n')) != NULL
)
98 while ((ch
= getchar()) != '\n' && ch
!= EOF
);
100 l
> 0 && isspace((unsigned char)buf
[l
- 1]);
106 if (strncmp(buf
, "LANG=", 5) == 0) {
107 setlocale(LC_ALL
, buf
+ 5);
108 d_first
= (*nl_langinfo(D_MD_ORDER
) == 'd');
112 if (strncasecmp(buf
, "Easter=", 7) == 0 && buf
[7]) {
113 if (neaster
.name
!= NULL
)
115 if ((neaster
.name
= strdup(buf
+ 7)) == NULL
)
116 errx(EXIT_FAILURE
, "cannot allocate memory");
117 neaster
.len
= strlen(buf
+ 7);
120 if (strncasecmp(buf
, "Paskha=", 7) == 0 && buf
[7]) {
121 if (npaskha
.name
!= NULL
)
123 if ((npaskha
.name
= strdup(buf
+ 7)) == NULL
)
124 errx(EXIT_FAILURE
, "cannot allocate memory");
125 npaskha
.len
= strlen(buf
+ 7);
128 if (buf
[0] != '\t') {
129 printing
= isnow(buf
, &month
, &day
, &var
) ? 1 : 0;
130 if ((p
= strchr(buf
, '\t')) == NULL
)
132 if (p
> buf
&& p
[-1] == '*')
139 d_first
= (*nl_langinfo(D_MD_ORDER
) == 'd');
140 tm
.tm_sec
= 0; /* unused */
141 tm
.tm_min
= 0; /* unused */
142 tm
.tm_hour
= 0; /* unused */
143 tm
.tm_wday
= 0; /* unused */
144 tm
.tm_mon
= month
- 1;
146 tm
.tm_year
= tp
->tm_year
; /* unused */
147 strftime(dbuf
, sizeof(dbuf
),
148 d_first
? "%e %b" : "%b %e",
150 events
= event_add(events
, month
, day
, dbuf
, var
, p
);
154 event_continue(events
, buf
);
157 event_print_all(fp
, events
);
162 * Functions to handle buffered calendar events.
165 event_add(struct event
*events
, int month
, int day
, char *date
, int var
, char *txt
)
169 e
= (struct event
*)calloc(1, sizeof(struct event
));
171 errx(EXIT_FAILURE
, "event_add: cannot allocate memory");
175 e
->date
= strdup(date
);
177 errx(EXIT_FAILURE
, "event_add: cannot allocate memory");
178 e
->text
= strdup(txt
);
180 errx(EXIT_FAILURE
, "event_add: cannot allocate memory");
187 event_continue(struct event
*e
, char *txt
)
191 text
= strdup(e
->text
);
193 errx(EXIT_FAILURE
, "event_continue: cannot allocate memory");
196 e
->text
= (char *)malloc(strlen(text
) + strlen(txt
) + 3);
198 errx(EXIT_FAILURE
, "event_continue: cannot allocate memory");
199 strcpy(e
->text
, text
);
200 strcat(e
->text
, "\n");
201 strcat(e
->text
, txt
);
208 event_print_all(FILE *fp
, struct event
*events
)
210 struct event
*e
, *e_next
;
211 int daycount
= f_dayAfter
+ f_dayBefore
;
215 for (daycounter
= 0; daycounter
<= daycount
; daycounter
++) {
216 day
= tp
->tm_yday
- f_dayBefore
+ daycounter
;
217 if (day
< 0) day
+= yrdays
;
218 if (day
>= yrdays
) day
-= yrdays
;
221 while (month
<= 12) {
222 if (day
<= cumdays
[month
])
227 day
-= cumdays
[month
];
230 fprintf(stderr
,"event_print_allmonth: %d, day: %d\n",month
,day
);
233 for (e
= events
; e
!= NULL
; e
= e_next
) {
236 if (month
!= e
->month
|| day
!= e
->day
)
239 fprintf(fp
, "%s%c%s\n", e
->date
,
240 e
->var
? '*' : ' ', e
->text
);
246 getfield(char *p
, char **endp
, int *flags
)
251 for (; !isdigit((unsigned char)*p
) && !isalpha((unsigned char)*p
)
253 if (*p
== '*') { /* `*' is current month */
256 return(tp
->tm_mon
+ 1);
258 if (isdigit((unsigned char)*p
)) {
259 val
= strtol(p
, &p
, 10); /* if 0, it's failure */
260 for (; !isdigit((unsigned char)*p
)
261 && !isalpha((unsigned char)*p
) && *p
!= '*'; ++p
);
265 for (start
= p
; isalpha((unsigned char)*++p
););
268 if (*p
== '+' || *p
== '-')
269 for(; isdigit((unsigned char)*++p
););
275 if ((val
= getmonth(start
)) != 0)
279 else if ((val
= getday(start
)) != 0) {
282 /* variable weekday */
283 if ((var
= getdayvar(start
)) != 0) {
284 if (var
<=5 && var
>= -4)
287 printf("var: %d\n", var
);
293 else if ((val
= geteaster(start
, tp
->tm_year
+ 1900)) != 0)
297 else if ((val
= getpaskha(start
, tp
->tm_year
+ 1900)) != 0)
305 for (*p
= savech
; !isdigit((unsigned char)*p
)
306 && !isalpha((unsigned char)*p
) && *p
!= '*'; ++p
);
311 static char path
[MAXPATHLEN
];
318 int fd
, found
, pdes
[2];
321 /* open up calendar file as stdin */
322 if (!freopen(calendarFile
, "r", stdin
)) {
324 if (chdir(calendarHomes
[0]) != 0)
326 if (stat(calendarNoMail
, &sbuf
) == 0)
328 if (!freopen(calendarFile
, "r", stdin
))
331 chdir(getenv("HOME"));
332 for (found
= i
= 0; i
< sizeof(calendarHomes
) /
333 sizeof(calendarHomes
[0]); i
++)
334 if (chdir(calendarHomes
[i
]) == 0 &&
335 freopen(calendarFile
, "r", stdin
)) {
340 errx(EXIT_FAILURE
, "no calendar file: ``%s''", calendarFile
);
351 /* child -- stdin already setup, set stdout to pipe input */
352 if (pdes
[1] != STDOUT_FILENO
) {
353 dup2(pdes
[1], STDOUT_FILENO
);
358 if (setuid(getuid()) < 0) {
359 warnx("first setuid failed");
362 if (setgid(getegid()) < 0) {
363 warnx("setgid failed");
366 if (setuid(uid
) < 0) {
367 warnx("setuid failed");
370 execl(_PATH_CPP
, "cpp", "-P",
371 "-traditional", "-nostdinc", /* GCC specific opts */
372 "-I.", "-I", _PATH_INCLUDE
, NULL
);
376 /* parent -- set stdin to pipe output */
377 dup2(pdes
[0], STDIN_FILENO
);
381 /* not reading all calendar files, just set output to stdout */
385 /* set output to a temporary file, so if no output don't send mail */
386 snprintf(path
, sizeof(path
), "%s/_calXXXXXX", _PATH_TMP
);
387 if ((fd
= mkstemp(path
)) < 0)
389 return(fdopen(fd
, "w+"));
397 int nread
, pdes
[2], status
;
404 if (fstat(fileno(fp
), &sbuf
) || !sbuf
.st_size
)
414 /* child -- set stdin to pipe output */
415 if (pdes
[0] != STDIN_FILENO
) {
416 dup2(pdes
[0], STDIN_FILENO
);
421 if (setuid(getuid()) < 0) {
422 warnx("setuid failed");
425 if (setgid(getegid()) < 0) {
426 warnx("setgid failed");
429 if (setuid(uid
) < 0) {
430 warnx("setuid failed");
433 execl(_PATH_SENDMAIL
, "sendmail", "-i", "-t", "-F",
434 "\"Reminder Service\"", NULL
);
435 warn(_PATH_SENDMAIL
);
438 /* parent -- write to pipe input */
441 header
[1].iov_base
= header
[3].iov_base
= pw
->pw_name
;
442 header
[1].iov_len
= header
[3].iov_len
= strlen(pw
->pw_name
);
443 writev(pdes
[1], header
, 7);
444 while ((nread
= read(fileno(fp
), buf
, sizeof(buf
))) > 0)
445 write(pdes
[1], buf
, nread
);
449 while (wait(&status
) >= 0);