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.13.2.3 2002/08/26 00:32:46 jmallett Exp $
36 * $DragonFly: src/usr.bin/calendar/io.c,v 1.4 2004/01/06 15:29:09 eirikn Exp $
39 #include <sys/types.h>
40 #include <sys/param.h>
56 #include "pathnames.h"
60 const char *calendarFile
= "calendar.all"; /* default calendar file */
61 const char *calendarHomes
[] = { ".calendar", _PATH_INCLUDE
}; /* HOME */
62 const char *calendarNoMail
= "nomail"; /* don't sent mail if this file exist */
64 struct fixs neaster
, npaskha
;
66 struct iovec header
[] = {
69 {" (Reminder Service)\nTo: ", 24},
73 {"'s Calendar\nPrecedence: bulk\n\n", 30},
87 static int d_first
= -1;
90 if ((fp
= opencal()) == NULL
)
92 for (printing
= 0; fgets(buf
, sizeof(buf
), stdin
) != NULL
;) {
93 if ((p
= strchr(buf
, '\n')) != NULL
)
96 while ((ch
= getchar()) != '\n' && ch
!= EOF
);
98 l
> 0 && isspace((unsigned char)buf
[l
- 1]);
104 if (strncmp(buf
, "LANG=", 5) == 0) {
105 (void) setlocale(LC_ALL
, buf
+ 5);
106 d_first
= (*nl_langinfo(D_MD_ORDER
) == 'd');
110 if (strncasecmp(buf
, "Easter=", 7) == 0 && buf
[7]) {
111 if (neaster
.name
!= NULL
)
113 if ((neaster
.name
= strdup(buf
+ 7)) == NULL
)
114 errx(1, "cannot allocate memory");
115 neaster
.len
= strlen(buf
+ 7);
118 if (strncasecmp(buf
, "Paskha=", 7) == 0 && buf
[7]) {
119 if (npaskha
.name
!= NULL
)
121 if ((npaskha
.name
= strdup(buf
+ 7)) == NULL
)
122 errx(1, "cannot allocate memory");
123 npaskha
.len
= strlen(buf
+ 7);
126 if (buf
[0] != '\t') {
127 printing
= isnow(buf
, &month
, &day
, &var
) ? 1 : 0;
128 if ((p
= strchr(buf
, '\t')) == NULL
)
130 if (p
> buf
&& p
[-1] == '*')
137 d_first
= (*nl_langinfo(D_MD_ORDER
) == 'd');
138 tm
.tm_sec
= 0; /* unused */
139 tm
.tm_min
= 0; /* unused */
140 tm
.tm_hour
= 0; /* unused */
141 tm
.tm_wday
= 0; /* unused */
142 tm
.tm_mon
= month
- 1;
144 tm
.tm_year
= tp
->tm_year
; /* unused */
145 (void)strftime(dbuf
, sizeof(dbuf
),
146 d_first
? "%e %b" : "%b %e",
148 (void)fprintf(fp
, "%s%c%s\n", dbuf
,
153 fprintf(fp
, "%s\n", buf
);
159 getfield(char *p
, char **endp
, int *flags
)
164 for (; !isdigit((unsigned char)*p
) && !isalpha((unsigned char)*p
) && *p
!= '*'; ++p
);
165 if (*p
== '*') { /* `*' is current month */
168 return (tp
->tm_mon
+ 1);
170 if (isdigit((unsigned char)*p
)) {
171 val
= strtol(p
, &p
, 10); /* if 0, it's failure */
172 for (; !isdigit((unsigned char)*p
) && !isalpha((unsigned char)*p
) && *p
!= '*'; ++p
);
176 for (start
= p
; isalpha((unsigned char)*++p
););
179 if (*p
== '+' || *p
== '-')
180 for(; isdigit((unsigned char)*++p
););
186 if ((val
= getmonth(start
)) != 0)
190 else if ((val
= getday(start
)) != 0) {
193 /* variable weekday */
194 if ((var
= getdayvar(start
)) != 0) {
195 if (var
<=5 && var
>= -4)
198 printf("var: %d\n", var
);
204 else if ((val
= geteaster(start
, tp
->tm_year
+ 1900)) != 0)
208 else if ((val
= getpaskha(start
, tp
->tm_year
+ 1900)) != 0)
216 for (*p
= savech
; !isdigit((unsigned char)*p
) && !isalpha((unsigned char)*p
) && *p
!= '*'; ++p
);
221 char path
[MAXPATHLEN
];
228 int fd
, found
, pdes
[2];
231 /* open up calendar file as stdin */
232 if (!freopen(calendarFile
, "r", stdin
)) {
234 if (chdir(calendarHomes
[0]) != 0)
236 if (stat(calendarNoMail
, &sbuf
) == 0)
238 if (!freopen(calendarFile
, "r", stdin
))
241 chdir(getenv("HOME"));
242 for (found
= i
= 0; i
< sizeof(calendarHomes
) /
243 sizeof(calendarHomes
[0]); i
++)
244 if (chdir(calendarHomes
[i
]) == 0 &&
245 freopen(calendarFile
, "r", stdin
)) {
250 errx(1, "no calendar file: ``%s''", calendarFile
);
257 (void)close(pdes
[0]);
258 (void)close(pdes
[1]);
261 /* child -- stdin already setup, set stdout to pipe input */
262 if (pdes
[1] != STDOUT_FILENO
) {
263 (void)dup2(pdes
[1], STDOUT_FILENO
);
264 (void)close(pdes
[1]);
266 (void)close(pdes
[0]);
268 if (setuid(getuid()) < 0) {
269 warnx("first setuid failed");
272 if (setgid(getegid()) < 0) {
273 warnx("setgid failed");
276 if (setuid(uid
) < 0) {
277 warnx("setuid failed");
280 execl(_PATH_CPP
, "cpp", "-P",
281 "-traditional", "-nostdinc", /* GCC specific opts */
282 "-I.", "-I", _PATH_INCLUDE
, (char *)NULL
);
286 /* parent -- set stdin to pipe output */
287 (void)dup2(pdes
[0], STDIN_FILENO
);
288 (void)close(pdes
[0]);
289 (void)close(pdes
[1]);
291 /* not reading all calendar files, just set output to stdout */
295 /* set output to a temporary file, so if no output don't send mail */
296 (void)snprintf(path
, sizeof(path
), "%s/_calXXXXXX", _PATH_TMP
);
297 if ((fd
= mkstemp(path
)) < 0)
299 return (fdopen(fd
, "w+"));
307 int nread
, pdes
[2], status
;
314 if (fstat(fileno(fp
), &sbuf
) || !sbuf
.st_size
)
320 (void)close(pdes
[0]);
321 (void)close(pdes
[1]);
324 /* child -- set stdin to pipe output */
325 if (pdes
[0] != STDIN_FILENO
) {
326 (void)dup2(pdes
[0], STDIN_FILENO
);
327 (void)close(pdes
[0]);
329 (void)close(pdes
[1]);
331 if (setuid(getuid()) < 0) {
332 warnx("setuid failed");
335 if (setgid(getegid()) < 0) {
336 warnx("setgid failed");
339 if (setuid(uid
) < 0) {
340 warnx("setuid failed");
343 execl(_PATH_SENDMAIL
, "sendmail", "-i", "-t", "-F",
344 "\"Reminder Service\"", (char *)NULL
);
345 warn(_PATH_SENDMAIL
);
348 /* parent -- write to pipe input */
349 (void)close(pdes
[0]);
351 header
[1].iov_base
= header
[3].iov_base
= pw
->pw_name
;
352 header
[1].iov_len
= header
[3].iov_len
= strlen(pw
->pw_name
);
353 writev(pdes
[1], header
, 7);
354 while ((nread
= read(fileno(fp
), buf
, sizeof(buf
))) > 0)
355 (void)write(pdes
[1], buf
, nread
);
356 (void)close(pdes
[1]);
357 done
: (void)fclose(fp
);
359 while (wait(&status
) >= 0);