Deleted Added
full compact
calendar.c (11335) calendar.c (13840)
1/*
2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
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

--- 27 unchanged lines hidden (view full) ---

36"@(#) Copyright (c) 1989, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94";
42#endif /* not lint */
43
1/*
2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
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

--- 27 unchanged lines hidden (view full) ---

36"@(#) Copyright (c) 1989, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94";
42#endif /* not lint */
43
44#include <sys/param.h>
45#include <sys/time.h>
46#include <sys/stat.h>
47#include <sys/uio.h>
48#include <sys/wait.h>
49
50#include <ctype.h>
51#include <err.h>
52#include <errno.h>
53#include <fcntl.h>
54#include <pwd.h>
55#include <stdio.h>
44#include <pwd.h>
45#include <stdio.h>
56#include <stdlib.h>
57#include <string.h>
58#include <unistd.h>
46#include <unistd.h>
47#include <errno.h>
48#include <err.h>
49#include <stdlib.h>
50#include <time.h>
59
60#include "pathnames.h"
51
52#include "pathnames.h"
53#include "calendar.h"
61
62struct passwd *pw;
54
55struct passwd *pw;
63int doall;
56int doall = 0;
57time_t f_time = 0;
64
58
65void cal __P((void));
66void closecal __P((FILE *));
67int getday __P((char *));
68int getfield __P((char *, char **, int *));
69int getmonth __P((char *));
70int isnow __P((char *));
71FILE *opencal __P((void));
72void settime __P((void));
73void usage __P((void));
74#define isleap(y) (((y) % 4) == 0 && ((y) % 100) != 0 || ((y) % 400) == 0)
59int f_dayAfter = 0; /* days after current date */
60int f_dayBefore = 0; /* days before current date */
75
76int
77main(argc, argv)
78 int argc;
79 char *argv[];
80{
81 extern int optind;
82 int ch;
83
61
62int
63main(argc, argv)
64 int argc;
65 char *argv[];
66{
67 extern int optind;
68 int ch;
69
84 while ((ch = getopt(argc, argv, "-a")) != EOF)
70 while ((ch = getopt(argc, argv, "?-af:t:A:B:")) != EOF)
85 switch (ch) {
86 case '-': /* backward contemptible */
87 case 'a':
88 if (getuid()) {
89 errno = EPERM;
90 err(1, NULL);
91 }
92 doall = 1;
93 break;
71 switch (ch) {
72 case '-': /* backward contemptible */
73 case 'a':
74 if (getuid()) {
75 errno = EPERM;
76 err(1, NULL);
77 }
78 doall = 1;
79 break;
80
81
82 case 'f': /* other calendar file */
83 calendarFile = optarg;
84 break;
85
86 case 't': /* other date, undocumented, for tests */
87 f_time = Mktime (optarg);
88 break;
89
90 case 'A': /* days after current date */
91 f_dayAfter = atoi(optarg);
92 break;
93
94 case 'B': /* days before current date */
95 f_dayBefore = atoi(optarg);
96 break;
97
94 case '?':
95 default:
96 usage();
97 }
98 argc -= optind;
99 argv += optind;
100
101 if (argc)
102 usage();
103
98 case '?':
99 default:
100 usage();
101 }
102 argc -= optind;
103 argv += optind;
104
105 if (argc)
106 usage();
107
104 settime();
108 /* use current time */
109 if (f_time <= 0)
110 (void)time(&f_time);
111
112 settime(f_time);
113
105 if (doall)
106 while ((pw = getpwent()) != NULL) {
107 (void)setegid(pw->pw_gid);
108 (void)seteuid(pw->pw_uid);
109 if (!chdir(pw->pw_dir))
110 cal();
111 (void)seteuid(0);
112 }
113 else
114 cal();
115 exit(0);
116}
117
114 if (doall)
115 while ((pw = getpwent()) != NULL) {
116 (void)setegid(pw->pw_gid);
117 (void)seteuid(pw->pw_uid);
118 if (!chdir(pw->pw_dir))
119 cal();
120 (void)seteuid(0);
121 }
122 else
123 cal();
124 exit(0);
125}
126
118void
119cal()
120{
121 register int printing;
122 register char *p;
123 FILE *fp;
124 int ch;
125 char buf[2048 + 1];
126
127
127 if ((fp = opencal()) == NULL)
128 return;
129 for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) {
130 if ((p = strchr(buf, '\n')) != NULL)
131 *p = '\0';
132 else
133 while ((ch = getchar()) != '\n' && ch != EOF);
134 if (buf[0] == '\0')
135 continue;
136 if (buf[0] != '\t')
137 printing = isnow(buf) ? 1 : 0;
138 if (printing)
139 (void)fprintf(fp, "%s\n", buf);
140 }
141 closecal(fp);
142}
143
144struct iovec header[] = {
145 "From: ", 6,
146 NULL, 0,
147 " (Reminder Service)\nTo: ", 24,
148 NULL, 0,
149 "\nSubject: ", 10,
150 NULL, 0,
151 "'s Calendar\nPrecedence: bulk\n\n", 30,
152};
153
154/* 1-based month, 0-based days, cumulative */
155int daytab[][14] = {
156 0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364,
157 0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365,
158};
159struct tm *tp;
160int *cumdays, offset, yrdays;
161char dayname[10];
162
163void
128void
164settime()
165{
166 time_t now;
167
168 (void)time(&now);
169 tp = localtime(&now);
170 if (isleap(tp->tm_year + 1900)) {
171 yrdays = 366;
172 cumdays = daytab[1];
173 } else {
174 yrdays = 365;
175 cumdays = daytab[0];
176 }
177 /* Friday displays Monday's events */
178 offset = tp->tm_wday == 5 ? 3 : 1;
179 header[5].iov_base = dayname;
180 header[5].iov_len = strftime(dayname, sizeof(dayname), "%A", tp);
181}
182
183/*
184 * Possible date formats include any combination of:
185 * 3-charmonth (January, Jan, Jan)
186 * 3-charweekday (Friday, Monday, mon.)
187 * numeric month or day (1, 2, 04)
188 *
189 * Any character may separate them, or they may not be separated. Any line,
190 * following a line that is matched, that starts with "whitespace", is shown
191 * along with the matched line.
192 */
193int
194isnow(endp)
195 char *endp;
196{
197 int day, flags, month, v1, v2;
198
199#define F_ISMONTH 0x01
200#define F_ISDAY 0x02
201 flags = 0;
202 /* didn't recognize anything, skip it */
203 if (!(v1 = getfield(endp, &endp, &flags)))
204 return (0);
205 if (flags & F_ISDAY || v1 > 12) {
206 /* found a day */
207 day = v1;
208 /* if no recognizable month, assume just a day alone */
209 if (!(month = getfield(endp, &endp, &flags)))
210 month = tp->tm_mon + 1;
211 } else if (flags & F_ISMONTH) {
212 month = v1;
213 /* if no recognizable day, assume the first */
214 if (!(day = getfield(endp, &endp, &flags)))
215 day = 1;
216 } else {
217 v2 = getfield(endp, &endp, &flags);
218 if (flags & F_ISMONTH) {
219 day = v1;
220 month = v2;
221 } else {
222 /* F_ISDAY set, v2 > 12, or no way to tell */
223 month = v1;
224 /* if no recognizable day, assume the first */
225 day = v2 ? v2 : 1;
226 }
227 }
228 if (flags & F_ISDAY)
229 day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
230 day = cumdays[month] + day;
231
232 /* if today or today + offset days */
233 if (day >= tp->tm_yday && day <= tp->tm_yday + offset)
234 return (1);
235 /* if number of days left in this year + days to event in next year */
236 if (yrdays - tp->tm_yday + day <= offset)
237 return (1);
238 return (0);
239}
240
241int
242getfield(p, endp, flags)
243 char *p, **endp;
244 int *flags;
245{
246 int val;
247 char *start, savech;
248
249 for (; !isdigit(*p) && !isalpha(*p) && *p != '*'; ++p);
250 if (*p == '*') { /* `*' is current month */
251 *flags |= F_ISMONTH;
252 *endp = p+1;
253 return (tp->tm_mon + 1);
254 }
255 if (isdigit(*p)) {
256 val = strtol(p, &p, 10); /* if 0, it's failure */
257 for (; !isdigit(*p) && !isalpha(*p) && *p != '*'; ++p);
258 *endp = p;
259 return (val);
260 }
261 for (start = p; isalpha(*++p););
262 savech = *p;
263 *p = '\0';
264 if ((val = getmonth(start)) != 0)
265 *flags |= F_ISMONTH;
266 else if ((val = getday(start)) != 0)
267 *flags |= F_ISDAY;
268 else {
269 *p = savech;
270 return (0);
271 }
272 for (*p = savech; !isdigit(*p) && !isalpha(*p) && *p != '*'; ++p);
273 *endp = p;
274 return (val);
275}
276
277char path[MAXPATHLEN + 1];
278
279FILE *
280opencal()
281{
282 int fd, pdes[2];
283
284 /* open up calendar file as stdin */
285 if (!freopen("calendar", "r", stdin)) {
286 if (doall)
287 return (NULL);
288 errx(1, "no calendar file in current directory.");
289 }
290 if (pipe(pdes) < 0)
291 return (NULL);
292 switch (vfork()) {
293 case -1: /* error */
294 (void)close(pdes[0]);
295 (void)close(pdes[1]);
296 return (NULL);
297 case 0:
298 /* child -- stdin already setup, set stdout to pipe input */
299 if (pdes[1] != STDOUT_FILENO) {
300 (void)dup2(pdes[1], STDOUT_FILENO);
301 (void)close(pdes[1]);
302 }
303 (void)close(pdes[0]);
304 execl(_PATH_CPP, "cpp", "-P", "-I.", _PATH_INCLUDE, NULL);
305 (void)fprintf(stderr,
306 "calendar: execl: %s: %s.\n", _PATH_CPP, strerror(errno));
307 _exit(1);
308 }
309 /* parent -- set stdin to pipe output */
310 (void)dup2(pdes[0], STDIN_FILENO);
311 (void)close(pdes[0]);
312 (void)close(pdes[1]);
313
314 /* not reading all calendar files, just set output to stdout */
315 if (!doall)
316 return (stdout);
317
318 /* set output to a temporary file, so if no output don't send mail */
319 (void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
320 if ((fd = mkstemp(path)) < 0)
321 return (NULL);
322 return (fdopen(fd, "w+"));
323}
324
325void
326closecal(fp)
327 FILE *fp;
328{
329 struct stat sbuf;
330 int nread, pdes[2], status;
331 char buf[1024];
332
333 if (!doall)
334 return;
335
336 (void)rewind(fp);
337 if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
338 goto done;
339 if (pipe(pdes) < 0)
340 goto done;
341 switch (vfork()) {
342 case -1: /* error */
343 (void)close(pdes[0]);
344 (void)close(pdes[1]);
345 goto done;
346 case 0:
347 /* child -- set stdin to pipe output */
348 if (pdes[0] != STDIN_FILENO) {
349 (void)dup2(pdes[0], STDIN_FILENO);
350 (void)close(pdes[0]);
351 }
352 (void)close(pdes[1]);
353 execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
354 "\"Reminder Service\"", "-f", "root", NULL);
355 (void)fprintf(stderr,
356 "calendar: %s: %s.\n", _PATH_SENDMAIL, strerror(errno));
357 _exit(1);
358 }
359 /* parent -- write to pipe input */
360 (void)close(pdes[0]);
361
362 header[1].iov_base = header[3].iov_base = pw->pw_name;
363 header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
364 writev(pdes[1], header, 7);
365 while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
366 (void)write(pdes[1], buf, nread);
367 (void)close(pdes[1]);
368done: (void)fclose(fp);
369 (void)unlink(path);
370 while (wait(&status) >= 0);
371}
372
373static char *months[] = {
374 "jan", "feb", "mar", "apr", "may", "jun",
375 "jul", "aug", "sep", "oct", "nov", "dec", NULL,
376};
377
378int
379getmonth(s)
380 register char *s;
381{
382 register char **p;
383
384 for (p = months; *p; ++p)
385 if (!strncasecmp(s, *p, 3))
386 return ((p - months) + 1);
387 return (0);
388}
389
390static char *days[] = {
391 "sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL,
392};
393
394int
395getday(s)
396 register char *s;
397{
398 register char **p;
399
400 for (p = days; *p; ++p)
401 if (!strncasecmp(s, *p, 3))
402 return ((p - days) + 1);
403 return (0);
404}
405
406void
407usage()
408{
129usage()
130{
409 (void)fprintf(stderr, "usage: calendar [-a]\n");
131 (void)fprintf(stderr,
132 "usage: calendar [-a] [-A days] [-B days] [-f calendarfile]\n");
410 exit(1);
411}
133 exit(1);
134}
135
136