Nuke unused macro and comment
[dragonfly.git] / usr.bin / calendar / io.c
blob2fd2c6b08028bec45c4163c1a5d22e2ea1558d55
1 /*
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
7 * are met:
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
31 * SUCH DAMAGE.
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.21 2007/06/09 05:54:13 grog Exp $
36 * $DragonFly: src/usr.bin/calendar/io.c,v 1.7 2007/09/24 20:49:09 pavalos Exp $
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <sys/time.h>
42 #include <sys/uio.h>
43 #include <sys/wait.h>
45 #include <ctype.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <langinfo.h>
49 #include <locale.h>
50 #include <pwd.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
56 #include "pathnames.h"
57 #include "calendar.h"
59 const char *calendarFile = "calendar.all"; /* default calendar file */
60 const char *calendarHomes[] = { ".calendar", _PATH_INCLUDE }; /* HOME */
61 const char *calendarNoMail = "nomail"; /* don't send mail if this file exist */
63 struct fixs neaster, npaskha;
65 char hdr_from[] = "From: ";
66 char hdr_to[] = " (Reminder Service)\nTo: ";
67 char hdr_subj[] = "\nSubject: ";
68 char hdr_prec[] = "'s Calendar\nPrecedence: bulk\n\n";
70 struct iovec header[] = {
71 {hdr_from, 6},
72 {NULL, 0},
73 {hdr_to, 24},
74 {NULL, 0},
75 {hdr_subj, 10},
76 {NULL, 0},
77 {hdr_prec, 30},
80 static FILE *opencal(void);
81 static void closecal(FILE *);
83 void
84 cal(void)
86 int printing;
87 char *p;
88 FILE *fp;
89 int ch, l;
90 int month;
91 int day;
92 int var;
93 static int d_first = -1;
94 char buf[2048 + 1];
95 struct event *events = NULL;
97 if ((fp = opencal()) == NULL)
98 return;
99 for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) {
100 if ((p = strchr(buf, '\n')) != NULL)
101 *p = '\0';
102 else
103 while ((ch = getchar()) != '\n' && ch != EOF);
104 for (l = strlen(buf);
105 l > 0 && isspace((unsigned char)buf[l - 1]);
106 l--)
108 buf[l] = '\0';
109 if (buf[0] == '\0')
110 continue;
111 if (strncmp(buf, "LANG=", 5) == 0) {
112 setlocale(LC_ALL, buf + 5);
113 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
114 setnnames();
115 continue;
117 if (strncasecmp(buf, "Easter=", 7) == 0 && buf[7]) {
118 if (neaster.name != NULL)
119 free(neaster.name);
120 if ((neaster.name = strdup(buf + 7)) == NULL)
121 errx(EXIT_FAILURE, "cannot allocate memory");
122 neaster.len = strlen(buf + 7);
123 continue;
125 if (strncasecmp(buf, "Paskha=", 7) == 0 && buf[7]) {
126 if (npaskha.name != NULL)
127 free(npaskha.name);
128 if ((npaskha.name = strdup(buf + 7)) == NULL)
129 errx(EXIT_FAILURE, "cannot allocate memory");
130 npaskha.len = strlen(buf + 7);
131 continue;
133 if (buf[0] != '\t') {
134 printing = isnow(buf, &month, &day, &var) ? 1 : 0;
135 if ((p = strchr(buf, '\t')) == NULL)
136 continue;
137 if (p > buf && p[-1] == '*')
138 var = 1;
139 if (printing) {
140 struct tm tm;
141 char dbuf[80];
143 if (d_first < 0)
144 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
145 tm.tm_sec = 0; /* unused */
146 tm.tm_min = 0; /* unused */
147 tm.tm_hour = 0; /* unused */
148 tm.tm_wday = 0; /* unused */
149 tm.tm_mon = month - 1;
150 tm.tm_mday = day;
151 tm.tm_year = tp->tm_year; /* unused */
152 strftime(dbuf, sizeof(dbuf),
153 d_first ? "%e %b" : "%b %e",
154 &tm);
155 events = event_add(events, month, day, dbuf, var, p);
158 else if (printing)
159 event_continue(events, buf);
162 event_print_all(fp, events);
163 closecal(fp);
167 * Functions to handle buffered calendar events.
169 struct event *
170 event_add(struct event *events, int month, int day, char *date, int var, char *txt)
172 struct event *e;
174 e = (struct event *)calloc(1, sizeof(struct event));
175 if (e == NULL)
176 errx(EXIT_FAILURE, "event_add: cannot allocate memory");
177 e->month = month;
178 e->day = day;
179 e->var = var;
180 e->date = strdup(date);
181 if (e->date == NULL)
182 errx(EXIT_FAILURE, "event_add: cannot allocate memory");
183 e->text = strdup(txt);
184 if (e->text == NULL)
185 errx(EXIT_FAILURE, "event_add: cannot allocate memory");
186 e->next = events;
188 return e;
191 void
192 event_continue(struct event *e, char *txt)
194 char *text;
196 text = strdup(e->text);
197 if (text == NULL)
198 errx(EXIT_FAILURE, "event_continue: cannot allocate memory");
200 free(e->text);
201 e->text = (char *)malloc(strlen(text) + strlen(txt) + 3);
202 if (e->text == NULL)
203 errx(EXIT_FAILURE, "event_continue: cannot allocate memory");
204 strcpy(e->text, text);
205 strcat(e->text, "\n");
206 strcat(e->text, txt);
207 free(text);
209 return;
212 void
213 event_print_all(FILE *fp, struct event *events)
215 struct event *e, *e_next;
216 int daycount = f_dayAfter + f_dayBefore;
217 int daycounter;
218 int day, month;
220 for (daycounter = 0; daycounter <= daycount; daycounter++) {
221 day = tp->tm_yday - f_dayBefore + daycounter;
222 if (day < 0) day += yrdays;
223 if (day >= yrdays) day -= yrdays;
225 month = 1;
226 while (month <= 12) {
227 if (day <= cumdays[month])
228 break;
229 month++;
231 month--;
232 day -= cumdays[month];
234 #ifdef DEBUG
235 fprintf(stderr,"event_print_allmonth: %d, day: %d\n",month,day);
236 #endif
238 for (e = events; e != NULL; e = e_next ) {
239 e_next = e->next;
241 if (month != e->month || day != e->day)
242 continue;
244 fprintf(fp, "%s%c%s\n", e->date,
245 e->var ? '*' : ' ', e->text);
251 getfield(char *p, char **endp, int *flags)
253 int val, var;
254 char *start, savech;
256 for (; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p)
257 && *p != '*'; ++p);
258 if (*p == '*') { /* `*' is current month */
259 *flags |= F_ISMONTH;
260 *endp = p+1;
261 return(tp->tm_mon + 1);
263 if (isdigit((unsigned char)*p)) {
264 val = strtol(p, &p, 10); /* if 0, it's failure */
265 for (; !isdigit((unsigned char)*p)
266 && !isalpha((unsigned char)*p) && *p != '*'; ++p);
267 *endp = p;
268 return(val);
270 for (start = p; isalpha((unsigned char)*++p););
272 /* Sunday-1 */
273 if (*p == '+' || *p == '-')
274 for(; isdigit((unsigned char)*++p););
276 savech = *p;
277 *p = '\0';
279 /* Month */
280 if ((val = getmonth(start)) != 0)
281 *flags |= F_ISMONTH;
283 /* Day */
284 else if ((val = getday(start)) != 0) {
285 *flags |= F_ISDAY;
287 /* variable weekday */
288 if ((var = getdayvar(start)) != 0) {
289 if (var <=5 && var >= -4)
290 val += var * 10;
291 #ifdef DEBUG
292 printf("var: %d\n", var);
293 #endif
297 /* Easter */
298 else if ((val = geteaster(start, tp->tm_year + 1900)) != 0)
299 *flags |= F_EASTER;
301 /* Paskha */
302 else if ((val = getpaskha(start, tp->tm_year + 1900)) != 0)
303 *flags |= F_EASTER;
305 /* undefined rest */
306 else {
307 *p = savech;
308 return (0);
310 for (*p = savech; !isdigit((unsigned char)*p)
311 && !isalpha((unsigned char)*p) && *p != '*'; ++p);
312 *endp = p;
313 return(val);
316 static char path[MAXPATHLEN];
318 static FILE *
319 opencal(void)
321 uid_t uid;
322 size_t i;
323 int fd, found, pdes[2];
324 struct stat sbuf;
326 /* open up calendar file as stdin */
327 if (!freopen(calendarFile, "r", stdin)) {
328 if (doall) {
329 if (chdir(calendarHomes[0]) != 0)
330 return(NULL);
331 if (stat(calendarNoMail, &sbuf) == 0)
332 return(NULL);
333 if (!freopen(calendarFile, "r", stdin))
334 return(NULL);
335 } else {
336 chdir(getenv("HOME"));
337 for (found = i = 0; i < sizeof(calendarHomes) /
338 sizeof(calendarHomes[0]); i++)
339 if (chdir(calendarHomes[i]) == 0 &&
340 freopen(calendarFile, "r", stdin)) {
341 found = 1;
342 break;
344 if (!found)
345 errx(EXIT_FAILURE, "no calendar file: ``%s''", calendarFile);
348 if (pipe(pdes) < 0)
349 return(NULL);
350 switch (fork()) {
351 case -1: /* error */
352 close(pdes[0]);
353 close(pdes[1]);
354 return(NULL);
355 case 0:
356 /* child -- stdin already setup, set stdout to pipe input */
357 if (pdes[1] != STDOUT_FILENO) {
358 dup2(pdes[1], STDOUT_FILENO);
359 close(pdes[1]);
361 close(pdes[0]);
362 uid = geteuid();
363 if (setuid(getuid()) < 0) {
364 warnx("first setuid failed");
365 _exit(EXIT_FAILURE);
367 if (setgid(getegid()) < 0) {
368 warnx("setgid failed");
369 _exit(EXIT_FAILURE);
371 if (setuid(uid) < 0) {
372 warnx("setuid failed");
373 _exit(EXIT_FAILURE);
375 execl(_PATH_CPP, "cpp", "-P",
376 "-traditional", "-nostdinc", /* GCC specific opts */
377 "-I.", "-I", _PATH_INCLUDE, (char *)NULL);
378 warn(_PATH_CPP);
379 _exit(EXIT_FAILURE);
381 /* parent -- set stdin to pipe output */
382 dup2(pdes[0], STDIN_FILENO);
383 close(pdes[0]);
384 close(pdes[1]);
386 /* not reading all calendar files, just set output to stdout */
387 if (!doall)
388 return(stdout);
390 /* set output to a temporary file, so if no output don't send mail */
391 snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
392 if ((fd = mkstemp(path)) < 0)
393 return(NULL);
394 return(fdopen(fd, "w+"));
397 static void
398 closecal(FILE *fp)
400 uid_t uid;
401 struct stat sbuf;
402 int nread, pdes[2], status;
403 char buf[1024];
405 if (!doall)
406 return;
408 rewind(fp);
409 if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
410 goto done;
411 if (pipe(pdes) < 0)
412 goto done;
413 switch (fork()) {
414 case -1: /* error */
415 close(pdes[0]);
416 close(pdes[1]);
417 goto done;
418 case 0:
419 /* child -- set stdin to pipe output */
420 if (pdes[0] != STDIN_FILENO) {
421 dup2(pdes[0], STDIN_FILENO);
422 close(pdes[0]);
424 close(pdes[1]);
425 uid = geteuid();
426 if (setuid(getuid()) < 0) {
427 warnx("setuid failed");
428 _exit(EXIT_FAILURE);
430 if (setgid(getegid()) < 0) {
431 warnx("setgid failed");
432 _exit(EXIT_FAILURE);
434 if (setuid(uid) < 0) {
435 warnx("setuid failed");
436 _exit(EXIT_FAILURE);
438 execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
439 "\"Reminder Service\"", (char *)NULL);
440 warn(_PATH_SENDMAIL);
441 _exit(EXIT_FAILURE);
443 /* parent -- write to pipe input */
444 close(pdes[0]);
446 header[1].iov_base = header[3].iov_base = pw->pw_name;
447 header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
448 writev(pdes[1], header, 7);
449 while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
450 write(pdes[1], buf, nread);
451 close(pdes[1]);
452 done: fclose(fp);
453 unlink(path);
454 while (wait(&status) >= 0);