Annotation of src/usr.bin/calendar/io.c, Revision 1.13
1.13 ! pjanzen 1: /* $OpenBSD: io.c,v 1.12 2001/07/09 07:04:48 deraadt Exp $ */
1.1 millert 2:
3: /*
4: * Copyright (c) 1989, 1993, 1994
5: * The Regents of the University of California. All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed by the University of
18: * California, Berkeley and its contributors.
19: * 4. Neither the name of the University nor the names of its contributors
20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
36: #ifndef lint
37: static const char copyright[] =
38: "@(#) Copyright (c) 1989, 1993\n\
39: The Regents of the University of California. All rights reserved.\n";
40: #endif /* not lint */
41:
42: #ifndef lint
43: #if 0
44: static const char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94";
45: #else
1.13 ! pjanzen 46: static char rcsid[] = "$OpenBSD: io.c,v 1.12 2001/07/09 07:04:48 deraadt Exp $";
1.1 millert 47: #endif
48: #endif /* not lint */
49:
50: #include <sys/param.h>
51: #include <sys/stat.h>
52: #include <sys/time.h>
53: #include <sys/types.h>
54: #include <sys/uio.h>
55: #include <sys/wait.h>
1.10 deraadt 56: #include <sys/file.h>
1.1 millert 57:
58: #include <ctype.h>
59: #include <err.h>
60: #include <errno.h>
61: #include <locale.h>
62: #include <pwd.h>
63: #include <stdio.h>
64: #include <stdlib.h>
65: #include <string.h>
1.4 deraadt 66: #include <tzfile.h>
1.1 millert 67: #include <unistd.h>
68:
69: #include "pathnames.h"
70: #include "calendar.h"
71:
72:
73: struct iovec header[] = {
74: {"From: ", 6},
75: {NULL, 0},
76: {" (Reminder Service)\nTo: ", 24},
77: {NULL, 0},
78: {"\nSubject: ", 10},
79: {NULL, 0},
80: {"'s Calendar\nPrecedence: bulk\n\n", 30},
81: };
82:
83:
1.10 deraadt 84: int openf(char *path);
85:
1.1 millert 86: void
87: cal()
88: {
89: register int printing;
90: register char *p;
91: FILE *fp;
1.6 pjanzen 92: int ch, l, i;
1.1 millert 93: int var;
94: char buf[2048 + 1];
1.6 pjanzen 95: struct event *events, *cur_evt, *ev1, *tmp;
1.5 pjanzen 96: struct match *m;
1.1 millert 97:
1.5 pjanzen 98: events = NULL;
99: cur_evt = NULL;
1.1 millert 100: if ((fp = opencal()) == NULL)
101: return;
102: for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) {
103: if ((p = strchr(buf, '\n')) != NULL)
104: *p = '\0';
105: else
106: while ((ch = getchar()) != '\n' && ch != EOF);
1.10 deraadt 107: for (l = strlen(buf); l > 0 && isspace(buf[l - 1]); l--)
1.1 millert 108: ;
109: buf[l] = '\0';
110: if (buf[0] == '\0')
111: continue;
112: if (strncmp(buf, "LANG=", 5) == 0) {
113: (void) setlocale(LC_ALL, buf + 5);
114: setnnames();
115: continue;
116: }
1.6 pjanzen 117: /* User defined names for special events */
118: if ((p = strchr(buf, '='))) {
119: for (i = 0; i < NUMEV; i++) {
120: if (strncasecmp(buf, spev[i].name, spev[i].nlen) == 0 &&
121: (p - buf == spev[i].nlen) && buf[spev[i].nlen + 1]) {
122: p++;
123: if (spev[i].uname != NULL)
124: free(spev[i].uname);
125: if ((spev[i].uname = strdup(p)) == NULL)
1.11 pjanzen 126: err(1, NULL);
1.6 pjanzen 127: spev[i].ulen = strlen(p);
128: i = NUMEV + 1;
129: }
130: }
131: if (i > NUMEV)
1.1 millert 132: continue;
133: }
134: if (buf[0] != '\t') {
1.5 pjanzen 135: printing = (m = isnow(buf)) ? 1 : 0;
1.6 pjanzen 136: if ((p = strchr(buf, '\t')) == NULL) {
137: printing = 0;
1.1 millert 138: continue;
1.6 pjanzen 139: }
1.5 pjanzen 140: /* Need the following to catch hardwired "variable"
141: * dates */
1.1 millert 142: if (p > buf && p[-1] == '*')
143: var = 1;
1.5 pjanzen 144: else
145: var = 0;
1.1 millert 146: if (printing) {
1.5 pjanzen 147: struct match *foo;
148:
1.6 pjanzen 149: ev1 = NULL;
1.5 pjanzen 150: while (m) {
151: cur_evt = (struct event *) malloc(sizeof(struct event));
152: if (cur_evt == NULL)
1.11 pjanzen 153: err(1, NULL);
1.1 millert 154:
1.6 pjanzen 155: cur_evt->when = m->when;
156: snprintf(cur_evt->print_date,
157: sizeof(cur_evt->print_date), "%s%c",
158: m->print_date, (var + m->var) ? '*' : ' ');
159: if (ev1) {
160: cur_evt->desc = ev1->desc;
161: cur_evt->ldesc = NULL;
162: } else {
163: if ((cur_evt->ldesc = strdup(p)) == NULL)
1.11 pjanzen 164: err(1, NULL);
1.6 pjanzen 165: cur_evt->desc = &(cur_evt->ldesc);
166: ev1 = cur_evt;
1.5 pjanzen 167: }
168: insert(&events, cur_evt);
169: foo = m;
170: m = m->next;
171: free(foo);
172: }
1.1 millert 173: }
174: }
1.5 pjanzen 175: else if (printing) {
1.6 pjanzen 176: if ((ev1->ldesc = realloc(ev1->ldesc,
177: (2 + strlen(ev1->ldesc) + strlen(buf)))) == NULL)
1.11 pjanzen 178: err(1, NULL);
1.6 pjanzen 179: strcat(ev1->ldesc, "\n");
180: strcat(ev1->ldesc, buf);
1.5 pjanzen 181: }
182: }
183: tmp = events;
184: while (tmp) {
1.6 pjanzen 185: (void)fprintf(fp, "%s%s\n", tmp->print_date, *(tmp->desc));
186: tmp = tmp->next;
187: }
188: tmp = events;
189: while (tmp) {
1.5 pjanzen 190: events = tmp;
1.6 pjanzen 191: if (tmp->ldesc)
192: free(tmp->ldesc);
1.5 pjanzen 193: tmp = tmp->next;
194: free(events);
1.1 millert 195: }
196: closecal(fp);
197: }
198:
199: int
200: getfield(p, endp, flags)
201: char *p, **endp;
202: int *flags;
203: {
1.6 pjanzen 204: int val, var, i;
1.1 millert 205: char *start, savech;
206:
1.8 pjanzen 207: for (; !isdigit(*p) && !isalpha(*p) && *p != '*' && *p != '\t'; ++p)
1.1 millert 208: ;
1.6 pjanzen 209: if (*p == '*') { /* `*' is every month */
1.1 millert 210: *flags |= F_ISMONTH;
211: *endp = p+1;
1.6 pjanzen 212: return (-1); /* means 'every month' */
1.1 millert 213: }
214: if (isdigit(*p)) {
215: val = strtol(p, &p, 10); /* if 0, it's failure */
216: for (; !isdigit(*p) && !isalpha(*p) && *p != '*'; ++p)
217: ;
218: *endp = p;
219: return (val);
220: }
221: for (start = p; isalpha(*++p);)
222: ;
223:
224: /* Sunday-1 */
225: if (*p == '+' || *p == '-')
226: for(; isdigit(*++p);)
227: ;
228:
229: savech = *p;
230: *p = '\0';
231:
232: /* Month */
233: if ((val = getmonth(start)) != 0)
234: *flags |= F_ISMONTH;
235:
236: /* Day */
237: else if ((val = getday(start)) != 0) {
238: *flags |= F_ISDAY;
239:
240: /* variable weekday */
241: if ((var = getdayvar(start)) != 0) {
1.6 pjanzen 242: if (var <= 5 && var >= -4)
1.1 millert 243: val += var * 10;
244: #ifdef DEBUG
245: printf("var: %d\n", var);
246: #endif
247: }
248: }
249:
1.6 pjanzen 250: /* Try specials (Easter, Paskha, ...) */
1.1 millert 251: else {
1.6 pjanzen 252: for (i = 0; i < NUMEV; i++) {
253: if (strncasecmp(start, spev[i].name, spev[i].nlen) == 0) {
254: start += spev[i].nlen;
255: val = i + 1;
256: i = NUMEV + 1;
257: } else if (spev[i].uname != NULL &&
258: strncasecmp(start, spev[i].uname, spev[i].ulen) == 0) {
259: start += spev[i].ulen;
260: val = i + 1;
261: i = NUMEV + 1;
262: }
263: }
264: if (i > NUMEV) {
265: switch(*start) {
266: case '-':
267: case '+':
268: var = atoi(start);
269: if (var > 365 || var < -365)
270: return (0); /* Someone is just being silly */
271: val += (NUMEV + 1) * var;
272: /* We add one to the matching event and multiply by
273: * (NUMEV + 1) so as not to return 0 if there's a match.
274: * val will overflow if there is an obscenely large
275: * number of special events. */
276: break;
277: }
278: *flags |= F_SPECIAL;
279: }
280: if (!(*flags & F_SPECIAL)) {
281: /* undefined rest */
282: *p = savech;
283: return (0);
284: }
1.1 millert 285: }
1.8 pjanzen 286: for (*p = savech; !isdigit(*p) && !isalpha(*p) && *p != '*' && *p != '\t'; ++p)
1.1 millert 287: ;
288: *endp = p;
289: return (val);
290: }
291:
292: char path[MAXPATHLEN];
293:
1.10 deraadt 294: int
295: openf(path)
296: char *path;
297: {
298: struct stat st;
299: int fd;
300:
301: fd = open(path, O_RDONLY|O_NONBLOCK);
302: if (fd == -1)
303: return (-1);
304: if (fstat(fd, &st) == -1) {
305: close(fd);
306: return (-1);
307: }
308: if ((st.st_mode & S_IFMT) != S_IFREG) {
309: close (fd);
310: return (-1);
311: }
312:
313: fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) &~ O_NONBLOCK);
314: return (fd);
315: }
316:
1.1 millert 317: FILE *
318: opencal()
319: {
1.13 ! pjanzen 320: int pdes[2];
1.10 deraadt 321: int fdin;
1.1 millert 322:
323: /* open up calendar file as stdin */
1.10 deraadt 324: if ((fdin = openf(calendarFile)) == -1) {
1.13 ! pjanzen 325: if (!doall) {
1.9 millert 326: char *home = getenv("HOME");
327: if (home == NULL || *home == '\0')
328: errx(1, "cannot get home directory");
1.10 deraadt 329: if (!(chdir(home) == 0 &&
330: chdir(calendarHome) == 0 &&
331: (fdin = openf(calendarFile)) != -1))
1.2 millert 332: errx(1, "no calendar file: ``%s'' or ``~/%s/%s",
333: calendarFile, calendarHome, calendarFile);
1.1 millert 334: }
335: }
336: if (pipe(pdes) < 0)
337: return (NULL);
338: switch (vfork()) {
339: case -1: /* error */
340: (void)close(pdes[0]);
341: (void)close(pdes[1]);
342: return (NULL);
343: case 0:
1.10 deraadt 344: dup2(fdin, STDIN_FILENO);
345: /* child -- set stdout to pipe input */
1.1 millert 346: if (pdes[1] != STDOUT_FILENO) {
347: (void)dup2(pdes[1], STDOUT_FILENO);
348: (void)close(pdes[1]);
349: }
350: (void)close(pdes[0]);
1.13 ! pjanzen 351: /* Set stderr to /dev/null. Necessary so that cron does not
! 352: * wait for cpp to finish if it's running calendar -a.
! 353: */
! 354: if (doall) {
! 355: int fderr;
! 356: fderr = open(_PATH_DEVNULL, O_WRONLY, 0);
! 357: if (fderr == -1)
! 358: _exit(0);
! 359: (void)dup2(fderr, STDERR_FILENO);
! 360: (void)close(fderr);
! 361: }
1.12 deraadt 362: execl(_PATH_CPP, "cpp", "-P", "-I.", _PATH_INCLUDE, (char *)NULL);
1.1 millert 363: warn(_PATH_CPP);
364: _exit(1);
365: }
366: /* parent -- set stdin to pipe output */
367: (void)dup2(pdes[0], STDIN_FILENO);
368: (void)close(pdes[0]);
369: (void)close(pdes[1]);
370:
371: /* not reading all calendar files, just set output to stdout */
372: if (!doall)
373: return (stdout);
374:
375: /* set output to a temporary file, so if no output don't send mail */
1.13 ! pjanzen 376: return(tmpfile());
1.1 millert 377: }
378:
379: void
380: closecal(fp)
381: FILE *fp;
382: {
383: struct stat sbuf;
384: int nread, pdes[2], status;
385: char buf[1024];
386:
387: if (!doall)
388: return;
389:
390: (void)rewind(fp);
391: if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
392: goto done;
393: if (pipe(pdes) < 0)
394: goto done;
395: switch (vfork()) {
396: case -1: /* error */
397: (void)close(pdes[0]);
398: (void)close(pdes[1]);
399: goto done;
400: case 0:
401: /* child -- set stdin to pipe output */
402: if (pdes[0] != STDIN_FILENO) {
403: (void)dup2(pdes[0], STDIN_FILENO);
404: (void)close(pdes[0]);
405: }
406: (void)close(pdes[1]);
407: execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
1.12 deraadt 408: "\"Reminder Service\"", (char *)NULL);
1.1 millert 409: warn(_PATH_SENDMAIL);
410: _exit(1);
411: }
412: /* parent -- write to pipe input */
413: (void)close(pdes[0]);
414:
415: header[1].iov_base = header[3].iov_base = pw->pw_name;
416: header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
417: writev(pdes[1], header, 7);
418: while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
419: (void)write(pdes[1], buf, nread);
420: (void)close(pdes[1]);
421: done: (void)fclose(fp);
1.10 deraadt 422: while (wait(&status) >= 0)
423: ;
1.5 pjanzen 424: }
425:
426:
427: void
428: insert(head, cur_evt)
429: struct event **head;
430: struct event *cur_evt;
431: {
432: struct event *tmp, *tmp2;
433:
434: if (*head) {
435: /* Insert this one in order */
436: tmp = *head;
437: tmp2 = NULL;
438: while (tmp->next &&
439: tmp->when <= cur_evt->when) {
440: tmp2 = tmp;
441: tmp = tmp->next;
442: }
443: if (tmp->when > cur_evt->when) {
444: cur_evt->next = tmp;
445: if (tmp2)
446: tmp2->next = cur_evt;
447: else
448: *head = cur_evt;
449: } else {
450: cur_evt->next = tmp->next;
451: tmp->next = cur_evt;
452: }
453: } else {
454: *head = cur_evt;
455: cur_evt->next = NULL;
456: }
1.1 millert 457: }