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