calendar.c revision 1.8 1 1.8 jtc /* $NetBSD: calendar.c,v 1.8 1995/09/02 05:38:38 jtc Exp $ */
2 1.7 glass
3 1.1 cgd /*
4 1.7 glass * Copyright (c) 1989, 1993, 1994
5 1.7 glass * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.1 cgd #ifndef lint
37 1.7 glass static char copyright[] =
38 1.7 glass "@(#) Copyright (c) 1989, 1993\n\
39 1.7 glass The Regents of the University of California. All rights reserved.\n";
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #ifndef lint
43 1.7 glass #if 0
44 1.8 jtc static char sccsid[] = "@(#)calendar.c 8.4 (Berkeley) 1/7/95";
45 1.7 glass #endif
46 1.8 jtc static char rcsid[] = "$NetBSD: calendar.c,v 1.8 1995/09/02 05:38:38 jtc Exp $";
47 1.1 cgd #endif /* not lint */
48 1.1 cgd
49 1.1 cgd #include <sys/param.h>
50 1.1 cgd #include <sys/time.h>
51 1.1 cgd #include <sys/stat.h>
52 1.1 cgd #include <sys/uio.h>
53 1.7 glass #include <sys/wait.h>
54 1.7 glass
55 1.7 glass #include <ctype.h>
56 1.7 glass #include <err.h>
57 1.7 glass #include <errno.h>
58 1.7 glass #include <fcntl.h>
59 1.1 cgd #include <pwd.h>
60 1.7 glass #include <stdio.h>
61 1.7 glass #include <stdlib.h>
62 1.7 glass #include <string.h>
63 1.1 cgd #include <tzfile.h>
64 1.1 cgd #include <unistd.h>
65 1.7 glass
66 1.1 cgd #include "pathnames.h"
67 1.1 cgd
68 1.1 cgd struct passwd *pw;
69 1.1 cgd int doall;
70 1.1 cgd
71 1.7 glass void cal __P((void));
72 1.7 glass void closecal __P((FILE *));
73 1.7 glass int getday __P((char *));
74 1.7 glass int getfield __P((char *, char **, int *));
75 1.7 glass int getmonth __P((char *));
76 1.7 glass int isnow __P((char *));
77 1.7 glass FILE *opencal __P((void));
78 1.7 glass void settime __P((void));
79 1.7 glass void usage __P((void));
80 1.7 glass
81 1.7 glass int
82 1.1 cgd main(argc, argv)
83 1.1 cgd int argc;
84 1.7 glass char *argv[];
85 1.1 cgd {
86 1.1 cgd extern int optind;
87 1.1 cgd int ch;
88 1.6 hpeyerl char *caldir;
89 1.1 cgd
90 1.1 cgd while ((ch = getopt(argc, argv, "-a")) != EOF)
91 1.7 glass switch (ch) {
92 1.1 cgd case '-': /* backward contemptible */
93 1.1 cgd case 'a':
94 1.1 cgd if (getuid()) {
95 1.7 glass errno = EPERM;
96 1.7 glass err(1, NULL);
97 1.1 cgd }
98 1.1 cgd doall = 1;
99 1.1 cgd break;
100 1.1 cgd case '?':
101 1.1 cgd default:
102 1.1 cgd usage();
103 1.1 cgd }
104 1.1 cgd argc -= optind;
105 1.1 cgd argv += optind;
106 1.1 cgd
107 1.1 cgd if (argc)
108 1.1 cgd usage();
109 1.1 cgd
110 1.1 cgd settime();
111 1.1 cgd if (doall)
112 1.7 glass while ((pw = getpwent()) != NULL) {
113 1.1 cgd (void)setegid(pw->pw_gid);
114 1.1 cgd (void)seteuid(pw->pw_uid);
115 1.1 cgd if (!chdir(pw->pw_dir))
116 1.1 cgd cal();
117 1.1 cgd (void)seteuid(0);
118 1.1 cgd }
119 1.6 hpeyerl else if ((caldir = getenv("CALENDAR_DIR")) != NULL) {
120 1.6 hpeyerl if(!chdir(caldir))
121 1.6 hpeyerl cal();
122 1.6 hpeyerl } else
123 1.1 cgd cal();
124 1.1 cgd exit(0);
125 1.1 cgd }
126 1.1 cgd
127 1.7 glass void
128 1.1 cgd cal()
129 1.1 cgd {
130 1.1 cgd register int printing;
131 1.1 cgd register char *p;
132 1.7 glass FILE *fp;
133 1.1 cgd int ch;
134 1.1 cgd char buf[2048 + 1];
135 1.1 cgd
136 1.7 glass if ((fp = opencal()) == NULL)
137 1.1 cgd return;
138 1.7 glass for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) {
139 1.7 glass if ((p = strchr(buf, '\n')) != NULL)
140 1.1 cgd *p = '\0';
141 1.1 cgd else
142 1.1 cgd while ((ch = getchar()) != '\n' && ch != EOF);
143 1.1 cgd if (buf[0] == '\0')
144 1.1 cgd continue;
145 1.1 cgd if (buf[0] != '\t')
146 1.1 cgd printing = isnow(buf) ? 1 : 0;
147 1.1 cgd if (printing)
148 1.1 cgd (void)fprintf(fp, "%s\n", buf);
149 1.1 cgd }
150 1.1 cgd closecal(fp);
151 1.1 cgd }
152 1.1 cgd
153 1.1 cgd struct iovec header[] = {
154 1.1 cgd "From: ", 6,
155 1.1 cgd NULL, 0,
156 1.1 cgd " (Reminder Service)\nTo: ", 24,
157 1.1 cgd NULL, 0,
158 1.1 cgd "\nSubject: ", 10,
159 1.1 cgd NULL, 0,
160 1.1 cgd "'s Calendar\nPrecedence: bulk\n\n", 30,
161 1.1 cgd };
162 1.1 cgd
163 1.1 cgd /* 1-based month, 0-based days, cumulative */
164 1.1 cgd int daytab[][14] = {
165 1.3 cgd 0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364,
166 1.3 cgd 0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365,
167 1.1 cgd };
168 1.1 cgd struct tm *tp;
169 1.1 cgd int *cumdays, offset, yrdays;
170 1.1 cgd char dayname[10];
171 1.1 cgd
172 1.7 glass void
173 1.1 cgd settime()
174 1.1 cgd {
175 1.7 glass time_t now;
176 1.1 cgd
177 1.1 cgd (void)time(&now);
178 1.1 cgd tp = localtime(&now);
179 1.1 cgd if (isleap(tp->tm_year + 1900)) {
180 1.1 cgd yrdays = DAYSPERLYEAR;
181 1.1 cgd cumdays = daytab[1];
182 1.1 cgd } else {
183 1.1 cgd yrdays = DAYSPERNYEAR;
184 1.1 cgd cumdays = daytab[0];
185 1.1 cgd }
186 1.1 cgd /* Friday displays Monday's events */
187 1.1 cgd offset = tp->tm_wday == 5 ? 3 : 1;
188 1.1 cgd header[5].iov_base = dayname;
189 1.1 cgd header[5].iov_len = strftime(dayname, sizeof(dayname), "%A", tp);
190 1.1 cgd }
191 1.1 cgd
192 1.1 cgd /*
193 1.1 cgd * Possible date formats include any combination of:
194 1.1 cgd * 3-charmonth (January, Jan, Jan)
195 1.1 cgd * 3-charweekday (Friday, Monday, mon.)
196 1.1 cgd * numeric month or day (1, 2, 04)
197 1.1 cgd *
198 1.1 cgd * Any character may separate them, or they may not be separated. Any line,
199 1.1 cgd * following a line that is matched, that starts with "whitespace", is shown
200 1.1 cgd * along with the matched line.
201 1.1 cgd */
202 1.7 glass int
203 1.1 cgd isnow(endp)
204 1.1 cgd char *endp;
205 1.1 cgd {
206 1.1 cgd int day, flags, month, v1, v2;
207 1.1 cgd
208 1.1 cgd #define F_ISMONTH 0x01
209 1.1 cgd #define F_ISDAY 0x02
210 1.1 cgd flags = 0;
211 1.1 cgd /* didn't recognize anything, skip it */
212 1.1 cgd if (!(v1 = getfield(endp, &endp, &flags)))
213 1.7 glass return (0);
214 1.7 glass if (flags & F_ISDAY || v1 > 12) {
215 1.1 cgd /* found a day */
216 1.1 cgd day = v1;
217 1.6 hpeyerl month = tp->tm_mon + 1;
218 1.7 glass } else if (flags & F_ISMONTH) {
219 1.1 cgd month = v1;
220 1.1 cgd /* if no recognizable day, assume the first */
221 1.1 cgd if (!(day = getfield(endp, &endp, &flags)))
222 1.1 cgd day = 1;
223 1.1 cgd } else {
224 1.1 cgd v2 = getfield(endp, &endp, &flags);
225 1.7 glass if (flags & F_ISMONTH) {
226 1.1 cgd day = v1;
227 1.1 cgd month = v2;
228 1.1 cgd } else {
229 1.1 cgd /* F_ISDAY set, v2 > 12, or no way to tell */
230 1.1 cgd month = v1;
231 1.1 cgd /* if no recognizable day, assume the first */
232 1.1 cgd day = v2 ? v2 : 1;
233 1.1 cgd }
234 1.1 cgd }
235 1.7 glass if (flags & F_ISDAY)
236 1.4 cgd day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
237 1.1 cgd day = cumdays[month] + day;
238 1.1 cgd
239 1.1 cgd /* if today or today + offset days */
240 1.1 cgd if (day >= tp->tm_yday && day <= tp->tm_yday + offset)
241 1.7 glass return (1);
242 1.1 cgd /* if number of days left in this year + days to event in next year */
243 1.1 cgd if (yrdays - tp->tm_yday + day <= offset)
244 1.7 glass return (1);
245 1.7 glass return (0);
246 1.1 cgd }
247 1.1 cgd
248 1.7 glass int
249 1.1 cgd getfield(p, endp, flags)
250 1.1 cgd char *p, **endp;
251 1.1 cgd int *flags;
252 1.1 cgd {
253 1.1 cgd int val;
254 1.1 cgd char *start, savech;
255 1.1 cgd
256 1.7 glass for (; !isdigit(*p) && !isalpha(*p) && *p != '*'; ++p);
257 1.1 cgd if (*p == '*') { /* `*' is current month */
258 1.1 cgd *flags |= F_ISMONTH;
259 1.3 cgd *endp = p+1;
260 1.7 glass return (tp->tm_mon + 1);
261 1.1 cgd }
262 1.1 cgd if (isdigit(*p)) {
263 1.1 cgd val = strtol(p, &p, 10); /* if 0, it's failure */
264 1.7 glass for (; !isdigit(*p) && !isalpha(*p) && *p != '*'; ++p);
265 1.1 cgd *endp = p;
266 1.7 glass return (val);
267 1.1 cgd }
268 1.1 cgd for (start = p; isalpha(*++p););
269 1.1 cgd savech = *p;
270 1.1 cgd *p = '\0';
271 1.7 glass if ((val = getmonth(start)) != 0)
272 1.1 cgd *flags |= F_ISMONTH;
273 1.7 glass else if ((val = getday(start)) != 0)
274 1.1 cgd *flags |= F_ISDAY;
275 1.4 cgd else {
276 1.4 cgd *p = savech;
277 1.7 glass return (0);
278 1.4 cgd }
279 1.7 glass for (*p = savech; !isdigit(*p) && !isalpha(*p) && *p != '*'; ++p);
280 1.1 cgd *endp = p;
281 1.7 glass return (val);
282 1.1 cgd }
283 1.1 cgd
284 1.1 cgd char path[MAXPATHLEN + 1];
285 1.1 cgd
286 1.1 cgd FILE *
287 1.1 cgd opencal()
288 1.1 cgd {
289 1.1 cgd int fd, pdes[2];
290 1.1 cgd
291 1.1 cgd /* open up calendar file as stdin */
292 1.1 cgd if (!freopen("calendar", "r", stdin)) {
293 1.1 cgd if (doall)
294 1.7 glass return (NULL);
295 1.7 glass errx(1, "no calendar file.");
296 1.1 cgd }
297 1.1 cgd if (pipe(pdes) < 0)
298 1.7 glass return (NULL);
299 1.1 cgd switch (vfork()) {
300 1.1 cgd case -1: /* error */
301 1.1 cgd (void)close(pdes[0]);
302 1.1 cgd (void)close(pdes[1]);
303 1.7 glass return (NULL);
304 1.1 cgd case 0:
305 1.1 cgd /* child -- stdin already setup, set stdout to pipe input */
306 1.1 cgd if (pdes[1] != STDOUT_FILENO) {
307 1.1 cgd (void)dup2(pdes[1], STDOUT_FILENO);
308 1.1 cgd (void)close(pdes[1]);
309 1.1 cgd }
310 1.1 cgd (void)close(pdes[0]);
311 1.5 cgd execl(_PATH_CPP, "cpp", "-P", "-I.", _PATH_INCLUDE, NULL);
312 1.7 glass warn("execl: %s", _PATH_CPP);
313 1.1 cgd _exit(1);
314 1.1 cgd }
315 1.1 cgd /* parent -- set stdin to pipe output */
316 1.1 cgd (void)dup2(pdes[0], STDIN_FILENO);
317 1.1 cgd (void)close(pdes[0]);
318 1.1 cgd (void)close(pdes[1]);
319 1.1 cgd
320 1.1 cgd /* not reading all calendar files, just set output to stdout */
321 1.1 cgd if (!doall)
322 1.7 glass return (stdout);
323 1.1 cgd
324 1.1 cgd /* set output to a temporary file, so if no output don't send mail */
325 1.7 glass (void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
326 1.1 cgd if ((fd = mkstemp(path)) < 0)
327 1.7 glass return (NULL);
328 1.7 glass return (fdopen(fd, "w+"));
329 1.1 cgd }
330 1.1 cgd
331 1.7 glass void
332 1.1 cgd closecal(fp)
333 1.1 cgd FILE *fp;
334 1.1 cgd {
335 1.1 cgd struct stat sbuf;
336 1.1 cgd int nread, pdes[2], status;
337 1.7 glass char buf[1024];
338 1.1 cgd
339 1.1 cgd if (!doall)
340 1.1 cgd return;
341 1.1 cgd
342 1.1 cgd (void)rewind(fp);
343 1.1 cgd if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
344 1.1 cgd goto done;
345 1.1 cgd if (pipe(pdes) < 0)
346 1.1 cgd goto done;
347 1.1 cgd switch (vfork()) {
348 1.1 cgd case -1: /* error */
349 1.1 cgd (void)close(pdes[0]);
350 1.1 cgd (void)close(pdes[1]);
351 1.1 cgd goto done;
352 1.1 cgd case 0:
353 1.1 cgd /* child -- set stdin to pipe output */
354 1.1 cgd if (pdes[0] != STDIN_FILENO) {
355 1.1 cgd (void)dup2(pdes[0], STDIN_FILENO);
356 1.1 cgd (void)close(pdes[0]);
357 1.1 cgd }
358 1.1 cgd (void)close(pdes[1]);
359 1.1 cgd execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
360 1.1 cgd "\"Reminder Service\"", "-f", "root", NULL);
361 1.7 glass warn("execl: %s", _PATH_SENDMAIL);
362 1.1 cgd _exit(1);
363 1.1 cgd }
364 1.1 cgd /* parent -- write to pipe input */
365 1.1 cgd (void)close(pdes[0]);
366 1.1 cgd
367 1.1 cgd header[1].iov_base = header[3].iov_base = pw->pw_name;
368 1.1 cgd header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
369 1.1 cgd writev(pdes[1], header, 7);
370 1.1 cgd while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
371 1.1 cgd (void)write(pdes[1], buf, nread);
372 1.1 cgd (void)close(pdes[1]);
373 1.1 cgd done: (void)fclose(fp);
374 1.1 cgd (void)unlink(path);
375 1.1 cgd while (wait(&status) >= 0);
376 1.1 cgd }
377 1.1 cgd
378 1.1 cgd static char *months[] = {
379 1.1 cgd "jan", "feb", "mar", "apr", "may", "jun",
380 1.1 cgd "jul", "aug", "sep", "oct", "nov", "dec", NULL,
381 1.1 cgd };
382 1.7 glass
383 1.7 glass int
384 1.1 cgd getmonth(s)
385 1.1 cgd register char *s;
386 1.1 cgd {
387 1.1 cgd register char **p;
388 1.1 cgd
389 1.1 cgd for (p = months; *p; ++p)
390 1.1 cgd if (!strncasecmp(s, *p, 3))
391 1.7 glass return ((p - months) + 1);
392 1.7 glass return (0);
393 1.1 cgd }
394 1.1 cgd
395 1.1 cgd static char *days[] = {
396 1.1 cgd "sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL,
397 1.1 cgd };
398 1.7 glass
399 1.7 glass int
400 1.1 cgd getday(s)
401 1.1 cgd register char *s;
402 1.1 cgd {
403 1.1 cgd register char **p;
404 1.1 cgd
405 1.1 cgd for (p = days; *p; ++p)
406 1.1 cgd if (!strncasecmp(s, *p, 3))
407 1.7 glass return ((p - days) + 1);
408 1.7 glass return (0);
409 1.1 cgd }
410 1.1 cgd
411 1.7 glass void
412 1.1 cgd usage()
413 1.1 cgd {
414 1.1 cgd (void)fprintf(stderr, "usage: calendar [-a]\n");
415 1.1 cgd exit(1);
416 1.1 cgd }
417