]>
git.cameronkatri.com Git - apple_cmds.git/blob - misc_cmds/calendar/io.c
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: src/usr.bin/calendar/io.c,v 1.24 2007/12/30 22:04:04 grog Exp $");
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 struct fixs neaster
, npaskha
;
76 struct iovec header
[] = {
79 {" (Reminder Service)\nTo: ", 24},
83 {"'s Calendar\nPrecedence: bulk\n\n", 30},
97 static int d_first
= -1;
99 struct event
*events
= NULL
;
101 if ((fp
= opencal()) == NULL
)
103 for (printing
= 0; fgets(buf
, sizeof(buf
), stdin
) != NULL
;) {
104 if ((p
= strchr(buf
, '\n')) != NULL
)
107 while ((ch
= getchar()) != '\n' && ch
!= EOF
);
108 for (l
= strlen(buf
);
109 l
> 0 && isspace((unsigned char)buf
[l
- 1]);
115 if (strncmp(buf
, "LANG=", 5) == 0) {
116 (void) setlocale(LC_ALL
, buf
+ 5);
117 d_first
= (*nl_langinfo(D_MD_ORDER
) == 'd');
121 if (strncasecmp(buf
, "Easter=", 7) == 0 && buf
[7]) {
122 if (neaster
.name
!= NULL
)
124 if ((neaster
.name
= strdup(buf
+ 7)) == NULL
)
125 errx(1, "cannot allocate memory");
126 neaster
.len
= strlen(buf
+ 7);
129 if (strncasecmp(buf
, "Paskha=", 7) == 0 && buf
[7]) {
130 if (npaskha
.name
!= NULL
)
132 if ((npaskha
.name
= strdup(buf
+ 7)) == NULL
)
133 errx(1, "cannot allocate memory");
134 npaskha
.len
= strlen(buf
+ 7);
137 if (buf
[0] != '\t') {
138 printing
= isnow(buf
, &month
, &day
, &var
) ? 1 : 0;
139 if ((p
= strchr(buf
, '\t')) == NULL
) {
141 fprintf(stderr
, "[continue <%s>]\n", buf
);
145 if (p
> buf
&& p
[-1] == '*')
149 fprintf(stderr
, "[Add event]\n");
155 d_first
= (*nl_langinfo(D_MD_ORDER
) == 'd');
156 tm
.tm_sec
= 0; /* unused */
157 tm
.tm_min
= 0; /* unused */
158 tm
.tm_hour
= 0; /* unused */
159 tm
.tm_wday
= 0; /* unused */
160 tm
.tm_mon
= month
- 1;
162 tm
.tm_year
= tp
->tm_year
; /* unused */
163 (void)strftime(dbuf
, sizeof(dbuf
),
164 d_first
? "%e %b" : "%b %e",
166 events
= event_add(events
, month
, day
, dbuf
, var
, p
);
170 event_continue(events
, buf
);
173 event_print_all(fp
, events
);
178 * Functions to handle buffered calendar events.
181 event_add(struct event
*events
, int month
, int day
, char *date
, int var
, char *txt
)
185 e
= (struct event
*)calloc(1, sizeof(struct event
));
187 errx(1, "event_add: cannot allocate memory");
191 e
->date
= strdup(date
);
193 errx(1, "event_add: cannot allocate memory");
194 e
->text
= strdup(txt
);
196 errx(1, "event_add: cannot allocate memory");
203 event_continue(struct event
*e
, char *txt
)
207 text
= strdup(e
->text
);
209 errx(1, "event_continue: cannot allocate memory");
212 e
->text
= (char *)malloc(strlen(text
) + strlen(txt
) + 3);
214 errx(1, "event_continue: cannot allocate memory");
215 strcpy(e
->text
, text
);
216 strcat(e
->text
, "\n");
217 strcat(e
->text
, txt
);
224 event_print_all(FILE *fp
, struct event
*events
)
226 struct event
*e
, *e_next
;
227 int daycount
= f_dayAfter
+ f_dayBefore
;
231 for (daycounter
= 0; daycounter
<= daycount
; daycounter
++) {
232 day
= tp
->tm_yday
- f_dayBefore
+ daycounter
;
233 if (day
< 0) day
+= yrdays
;
234 if (day
>= yrdays
) day
-= yrdays
;
237 while (month
<= 12) {
238 if (day
<= cumdays
[month
])
243 day
-= cumdays
[month
];
246 fprintf(stderr
,"event_print_allmonth: %d, day: %d\n",month
,day
);
249 for (e
= events
; e
!= NULL
; e
= e_next
) {
252 if (month
!= e
->month
|| day
!= e
->day
)
255 (void)fprintf(fp
, "%s%c%s\n", e
->date
,
256 e
->var
? '*' : ' ', e
->text
);
262 getfield(char *p
, char **endp
, int *flags
)
267 for (; !isdigit((unsigned char)*p
) && !isalpha((unsigned char)*p
)
269 if (*p
== '*') { /* `*' is current month */
272 return (tp
->tm_mon
+ 1);
274 if (isdigit((unsigned char)*p
)) {
275 val
= strtol(p
, &p
, 10); /* if 0, it's failure */
276 for (; !isdigit((unsigned char)*p
)
277 && !isalpha((unsigned char)*p
) && *p
!= '*'; ++p
);
281 for (start
= p
; isalpha((unsigned char)*++p
););
284 if (*p
== '+' || *p
== '-')
285 for(; isdigit((unsigned char)*++p
););
291 if ((val
= getmonth(start
)) != 0)
295 else if ((val
= getday(start
)) != 0) {
298 /* variable weekday */
299 if ((var
= getdayvar(start
)) != 0) {
300 if (var
<=5 && var
>= -4)
303 printf("var: %d\n", var
);
309 else if ((val
= geteaster(start
, tp
->tm_year
+ 1900)) != 0)
313 else if ((val
= getpaskha(start
, tp
->tm_year
+ 1900)) != 0)
321 for (*p
= savech
; !isdigit((unsigned char)*p
)
322 && !isalpha((unsigned char)*p
) && *p
!= '*'; ++p
);
327 char path
[MAXPATHLEN
];
334 int fd
, found
, pdes
[2];
337 /* open up calendar file as stdin */
338 if (!freopen(calendarFile
, "r", stdin
)) {
340 if (chdir(calendarHomes
[0]) != 0)
342 if (stat(calendarNoMail
, &sbuf
) == 0)
344 if (!freopen(calendarFile
, "r", stdin
))
347 char *home
= getenv("HOME");
348 if (home
== NULL
|| *home
== '\0')
349 errx(1, "cannot get home directory");
351 for (found
= i
= 0; i
< sizeof(calendarHomes
) /
352 sizeof(calendarHomes
[0]); i
++)
353 if (chdir(calendarHomes
[i
]) == 0 &&
354 freopen(calendarFile
, "r", stdin
)) {
359 errx(1, "can't open calendar file \"%s\": %s (%d)",
360 calendarFile
, strerror(errno
), errno
);
367 (void)close(pdes
[0]);
368 (void)close(pdes
[1]);
371 /* child -- stdin already setup, set stdout to pipe input */
372 if (pdes
[1] != STDOUT_FILENO
) {
373 (void)dup2(pdes
[1], STDOUT_FILENO
);
374 (void)close(pdes
[1]);
376 (void)close(pdes
[0]);
378 if (setuid(getuid()) < 0) {
379 warnx("first setuid failed");
382 if (setgid(getegid()) < 0) {
383 warnx("setgid failed");
386 if (setuid(uid
) < 0) {
387 warnx("setuid failed");
390 execl(_PATH_CPP
, "cpp", "-w", "-P",
391 "-traditional", "-nostdinc", /* GCC specific opts */
392 "-I.", "-I" _PATH_INCLUDE
, (char *)NULL
);
396 /* parent -- set stdin to pipe output */
397 (void)dup2(pdes
[0], STDIN_FILENO
);
398 (void)close(pdes
[0]);
399 (void)close(pdes
[1]);
401 /* not reading all calendar files, just set output to stdout */
405 /* set output to a temporary file, so if no output don't send mail */
406 (void)snprintf(path
, sizeof(path
), "%s/_calXXXXXX", _PATH_TMP
);
407 if ((fd
= mkstemp(path
)) < 0)
409 return (fdopen(fd
, "w+"));
417 int nread
, pdes
[2], status
;
424 if (fstat(fileno(fp
), &sbuf
) || !sbuf
.st_size
)
430 (void)close(pdes
[0]);
431 (void)close(pdes
[1]);
434 /* child -- set stdin to pipe output */
435 if (pdes
[0] != STDIN_FILENO
) {
436 (void)dup2(pdes
[0], STDIN_FILENO
);
437 (void)close(pdes
[0]);
439 (void)close(pdes
[1]);
441 if (setuid(getuid()) < 0) {
442 warnx("setuid failed");
445 if (setgid(getegid()) < 0) {
446 warnx("setgid failed");
449 if (setuid(uid
) < 0) {
450 warnx("setuid failed");
453 execl(_PATH_SENDMAIL
, "sendmail", "-i", "-t", "-F",
454 "\"Reminder Service\"", (char *)NULL
);
455 warn(_PATH_SENDMAIL
);
458 /* parent -- write to pipe input */
459 (void)close(pdes
[0]);
461 header
[1].iov_base
= header
[3].iov_base
= pw
->pw_name
;
462 header
[1].iov_len
= header
[3].iov_len
= strlen(pw
->pw_name
);
463 writev(pdes
[1], header
, 7);
464 while ((nread
= read(fileno(fp
), buf
, sizeof(buf
))) > 0)
465 (void)write(pdes
[1], buf
, nread
);
466 (void)close(pdes
[1]);
467 done
: (void)fclose(fp
);
469 while (wait(&status
) >= 0);