]>
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 <libiosexec.h>
68 #include "pathnames.h"
72 const char *calendarFile
= "calendar"; /* default calendar file */
73 const char *calendarHomes
[] = { ".calendar", _PATH_INCLUDE
}; /* HOME */
74 const char *calendarNoMail
= "nomail"; /* don't sent mail if this file exist */
76 struct fixs neaster
, npaskha
;
78 struct iovec header
[] = {
81 {" (Reminder Service)\nTo: ", 24},
85 {"'s Calendar\nPrecedence: bulk\n\n", 30},
99 static int d_first
= -1;
101 struct event
*events
= NULL
;
103 if ((fp
= opencal()) == NULL
)
105 for (printing
= 0; fgets(buf
, sizeof(buf
), stdin
) != NULL
;) {
106 if ((p
= strchr(buf
, '\n')) != NULL
)
109 while ((ch
= getchar()) != '\n' && ch
!= EOF
);
110 for (l
= strlen(buf
);
111 l
> 0 && isspace((unsigned char)buf
[l
- 1]);
117 if (strncmp(buf
, "LANG=", 5) == 0) {
118 (void) setlocale(LC_ALL
, buf
+ 5);
119 d_first
= (*nl_langinfo(D_MD_ORDER
) == 'd');
123 if (strncasecmp(buf
, "Easter=", 7) == 0 && buf
[7]) {
124 if (neaster
.name
!= NULL
)
126 if ((neaster
.name
= strdup(buf
+ 7)) == NULL
)
127 errx(1, "cannot allocate memory");
128 neaster
.len
= strlen(buf
+ 7);
131 if (strncasecmp(buf
, "Paskha=", 7) == 0 && buf
[7]) {
132 if (npaskha
.name
!= NULL
)
134 if ((npaskha
.name
= strdup(buf
+ 7)) == NULL
)
135 errx(1, "cannot allocate memory");
136 npaskha
.len
= strlen(buf
+ 7);
139 if (buf
[0] != '\t') {
140 printing
= isnow(buf
, &month
, &day
, &var
) ? 1 : 0;
141 if ((p
= strchr(buf
, '\t')) == NULL
) {
143 fprintf(stderr
, "[continue <%s>]\n", buf
);
147 if (p
> buf
&& p
[-1] == '*')
151 fprintf(stderr
, "[Add event]\n");
157 d_first
= (*nl_langinfo(D_MD_ORDER
) == 'd');
158 tm
.tm_sec
= 0; /* unused */
159 tm
.tm_min
= 0; /* unused */
160 tm
.tm_hour
= 0; /* unused */
161 tm
.tm_wday
= 0; /* unused */
162 tm
.tm_mon
= month
- 1;
164 tm
.tm_year
= tp
->tm_year
; /* unused */
165 (void)strftime(dbuf
, sizeof(dbuf
),
166 d_first
? "%e %b" : "%b %e",
168 events
= event_add(events
, month
, day
, dbuf
, var
, p
);
172 event_continue(events
, buf
);
175 event_print_all(fp
, events
);
180 * Functions to handle buffered calendar events.
183 event_add(struct event
*events
, int month
, int day
, char *date
, int var
, char *txt
)
187 e
= (struct event
*)calloc(1, sizeof(struct event
));
189 errx(1, "event_add: cannot allocate memory");
193 e
->date
= strdup(date
);
195 errx(1, "event_add: cannot allocate memory");
196 e
->text
= strdup(txt
);
198 errx(1, "event_add: cannot allocate memory");
205 event_continue(struct event
*e
, char *txt
)
209 text
= strdup(e
->text
);
211 errx(1, "event_continue: cannot allocate memory");
214 e
->text
= (char *)malloc(strlen(text
) + strlen(txt
) + 3);
216 errx(1, "event_continue: cannot allocate memory");
217 strcpy(e
->text
, text
);
218 strcat(e
->text
, "\n");
219 strcat(e
->text
, txt
);
226 event_print_all(FILE *fp
, struct event
*events
)
228 struct event
*e
, *e_next
;
229 int daycount
= f_dayAfter
+ f_dayBefore
;
233 for (daycounter
= 0; daycounter
<= daycount
; daycounter
++) {
234 day
= tp
->tm_yday
- f_dayBefore
+ daycounter
;
235 if (day
< 0) day
+= yrdays
;
236 if (day
>= yrdays
) day
-= yrdays
;
239 while (month
<= 12) {
240 if (day
<= cumdays
[month
])
245 day
-= cumdays
[month
];
248 fprintf(stderr
,"event_print_allmonth: %d, day: %d\n",month
,day
);
251 for (e
= events
; e
!= NULL
; e
= e_next
) {
254 if (month
!= e
->month
|| day
!= e
->day
)
257 (void)fprintf(fp
, "%s%c%s\n", e
->date
,
258 e
->var
? '*' : ' ', e
->text
);
264 getfield(char *p
, char **endp
, int *flags
)
269 for (; !isdigit((unsigned char)*p
) && !isalpha((unsigned char)*p
)
271 if (*p
== '*') { /* `*' is current month */
274 return (tp
->tm_mon
+ 1);
276 if (isdigit((unsigned char)*p
)) {
277 val
= strtol(p
, &p
, 10); /* if 0, it's failure */
278 for (; !isdigit((unsigned char)*p
)
279 && !isalpha((unsigned char)*p
) && *p
!= '*'; ++p
);
283 for (start
= p
; isalpha((unsigned char)*++p
););
286 if (*p
== '+' || *p
== '-')
287 for(; isdigit((unsigned char)*++p
););
293 if ((val
= getmonth(start
)) != 0)
297 else if ((val
= getday(start
)) != 0) {
300 /* variable weekday */
301 if ((var
= getdayvar(start
)) != 0) {
302 if (var
<=5 && var
>= -4)
305 printf("var: %d\n", var
);
311 else if ((val
= geteaster(start
, tp
->tm_year
+ 1900)) != 0)
315 else if ((val
= getpaskha(start
, tp
->tm_year
+ 1900)) != 0)
323 for (*p
= savech
; !isdigit((unsigned char)*p
)
324 && !isalpha((unsigned char)*p
) && *p
!= '*'; ++p
);
329 char path
[MAXPATHLEN
];
336 int fd
, found
, pdes
[2];
339 /* open up calendar file as stdin */
340 if (!freopen(calendarFile
, "r", stdin
)) {
342 if (chdir(calendarHomes
[0]) != 0)
344 if (stat(calendarNoMail
, &sbuf
) == 0)
346 if (!freopen(calendarFile
, "r", stdin
))
349 char *home
= getenv("HOME");
350 if (home
== NULL
|| *home
== '\0')
351 errx(1, "cannot get home directory");
353 for (found
= i
= 0; i
< sizeof(calendarHomes
) /
354 sizeof(calendarHomes
[0]); i
++)
355 if (chdir(calendarHomes
[i
]) == 0 &&
356 freopen(calendarFile
, "r", stdin
)) {
361 errx(1, "can't open calendar file \"%s\": %s (%d)",
362 calendarFile
, strerror(errno
), errno
);
369 (void)close(pdes
[0]);
370 (void)close(pdes
[1]);
373 /* child -- stdin already setup, set stdout to pipe input */
374 if (pdes
[1] != STDOUT_FILENO
) {
375 (void)dup2(pdes
[1], STDOUT_FILENO
);
376 (void)close(pdes
[1]);
378 (void)close(pdes
[0]);
380 if (setuid(getuid()) < 0) {
381 warnx("first setuid failed");
384 if (setgid(getegid()) < 0) {
385 warnx("setgid failed");
388 if (setuid(uid
) < 0) {
389 warnx("setuid failed");
392 execl(_PATH_CPP
, "cpp", "-w", "-P",
393 "-traditional", "-nostdinc", /* GCC specific opts */
394 "-I.", "-I" _PATH_INCLUDE
, (char *)NULL
);
398 /* parent -- set stdin to pipe output */
399 (void)dup2(pdes
[0], STDIN_FILENO
);
400 (void)close(pdes
[0]);
401 (void)close(pdes
[1]);
403 /* not reading all calendar files, just set output to stdout */
407 /* set output to a temporary file, so if no output don't send mail */
408 (void)snprintf(path
, sizeof(path
), "%s/_calXXXXXX", _PATH_TMP
);
409 if ((fd
= mkstemp(path
)) < 0)
411 return (fdopen(fd
, "w+"));
419 int nread
, pdes
[2], status
;
426 if (fstat(fileno(fp
), &sbuf
) || !sbuf
.st_size
)
432 (void)close(pdes
[0]);
433 (void)close(pdes
[1]);
436 /* child -- set stdin to pipe output */
437 if (pdes
[0] != STDIN_FILENO
) {
438 (void)dup2(pdes
[0], STDIN_FILENO
);
439 (void)close(pdes
[0]);
441 (void)close(pdes
[1]);
443 if (setuid(getuid()) < 0) {
444 warnx("setuid failed");
447 if (setgid(getegid()) < 0) {
448 warnx("setgid failed");
451 if (setuid(uid
) < 0) {
452 warnx("setuid failed");
455 execl(_PATH_SENDMAIL
, "sendmail", "-i", "-t", "-F",
456 "\"Reminder Service\"", (char *)NULL
);
457 warn(_PATH_SENDMAIL
);
460 /* parent -- write to pipe input */
461 (void)close(pdes
[0]);
463 header
[1].iov_base
= header
[3].iov_base
= pw
->pw_name
;
464 header
[1].iov_len
= header
[3].iov_len
= strlen(pw
->pw_name
);
465 writev(pdes
[1], header
, 7);
466 while ((nread
= read(fileno(fp
), buf
, sizeof(buf
))) > 0)
467 (void)write(pdes
[1], buf
, nread
);
468 (void)close(pdes
[1]);
469 done
: (void)fclose(fp
);
471 while (wait(&status
) >= 0);