1 /* $NetBSD: calendar.c,v 1.50 2013/11/09 15:57:15 christos Exp $ */
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)calendar.c 8.4 (Berkeley) 1/7/95";
42 __RCSID("$NetBSD: calendar.c,v 1.50 2013/11/09 15:57:15 christos Exp $");
45 #include <sys/param.h>
64 #include "pathnames.h"
66 /* flags used by calendar file parser */
67 #define F_ISMONTH 0x01
70 #define F_WILDMONTH 0x10
71 #define F_WILDDAY 0x20
73 static unsigned short lookahead
= 1;
74 static unsigned short weekend
= 2;
75 static char *fname
= NULL
;
76 static char *datestr
= NULL
;
77 static const char *defaultnames
[] = {"calendar", ".calendar", _PATH_SYSTEM_CALENDAR
, NULL
};
78 static struct passwd
*pw
;
79 static char path
[MAXPATHLEN
+ 1];
80 static bool doall
= false;
81 static bool cpp_restricted
= false;
83 /* 1-based month, 0-based days, cumulative */
84 static const int daytab
[][14] = {
85 { 0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 },
86 { 0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
89 static const int *cumdays
;
90 static int offset
, yrdays
;
91 static char dayname
[10];
93 static struct iovec header
[] = {
94 { __UNCONST("From: "), 6 },
96 { __UNCONST(" (Reminder Service)\nTo: "), 24 },
98 { __UNCONST("\nSubject: "), 10 },
100 { __UNCONST("'s Calendar\nPrecedence: bulk\n\n"), 30 },
103 static const char *days
[] = {
104 "sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL
,
107 static const char *months
[] = {
108 "jan", "feb", "mar", "apr", "may", "jun",
109 "jul", "aug", "sep", "oct", "nov", "dec", NULL
,
112 static void atodays(int, char *, unsigned short *);
113 static void cal(void);
114 static void closecal(FILE *);
115 static int getday(char *);
116 static int getfield(char *, char **, int *);
117 static void getmmdd(struct tm
*, char *);
118 static int getmonth(char *);
119 static bool isnow(char *);
120 static FILE *opencal(FILE **);
121 static void settime(void);
122 static void usage(void) __dead
;
125 main(int argc
, char **argv
)
130 (void)setprogname(argv
[0]); /* for portability */
132 while ((ch
= getopt(argc
, argv
, "-ad:f:l:w:x")) != -1) {
134 case '-': /* backward contemptible */
138 err(EXIT_FAILURE
, NULL
);
149 atodays(ch
, optarg
, &lookahead
);
152 atodays(ch
, optarg
, &weekend
);
155 cpp_restricted
= true;
171 * XXX - This ignores the user's CALENDAR_DIR variable.
172 * Run under user's login shell?
174 while ((pw
= getpwent()) != NULL
) {
175 (void)setegid(pw
->pw_gid
);
176 (void)seteuid(pw
->pw_uid
);
177 if (chdir(pw
->pw_dir
) != -1)
181 } else if ((caldir
= getenv("CALENDAR_DIR")) != NULL
) {
182 if (chdir(caldir
) != -1)
184 } else if ((pw
= getpwuid(geteuid())) != NULL
) {
185 if (chdir(pw
->pw_dir
) != -1)
195 FILE *fp
, *in
= NULL
;
198 if ((fp
= opencal(&in
)) == NULL
|| in
== NULL
)
201 while ((line
= fparseln(in
,
202 NULL
, NULL
, NULL
, FPARSELN_UNESCCOMM
)) != NULL
) {
206 printing
= isnow(line
);
208 (void)fprintf(fp
, "%s\n", line
);
221 tp
= localtime(&now
);
223 getmmdd(tp
, datestr
);
225 if (isleap(tp
->tm_year
+ TM_YEAR_BASE
)) {
226 yrdays
= DAYSPERLYEAR
;
229 yrdays
= DAYSPERNYEAR
;
232 /* Friday displays Monday's events */
233 offset
= tp
->tm_wday
== 5 ? lookahead
+ weekend
: lookahead
;
234 header
[5].iov_base
= dayname
;
235 header
[5].iov_len
= strftime(dayname
, sizeof(dayname
), "%A", tp
);
239 * Possible date formats include any combination of:
240 * 3-charmonth (January, Jan, Jan)
241 * 3-charweekday (Friday, Monday, mon.)
242 * numeric month or day (1, 2, 04)
244 * Any character may separate them, or they may not be separated. Any line,
245 * following a line that is matched, that starts with "whitespace", is shown
246 * along with the matched line.
259 /* didn't recognize anything, skip it */
260 if (!(v1
= getfield(endp
, &endp
, &flags
)))
263 if ((flags
& (F_ISDAY
|F_ISDOW
)) || v1
> 12) {
266 /* if no recognizable month, assume wildcard ('*') month */
267 if ((month
= getfield(endp
, &endp
, &flags
)) == 0) {
268 flags
|= F_ISMONTH
| F_WILDMONTH
;
269 month
= tp
->tm_mon
+ 1;
271 } else if (flags
& F_ISMONTH
) {
273 /* if no recognizable day, assume the first */
274 if ((day
= getfield(endp
, &endp
, &flags
)) == 0)
277 v2
= getfield(endp
, &endp
, &flags
);
278 if (flags
& F_ISMONTH
) {
282 /* F_ISDAY set, v2 > 12, or no way to tell */
284 /* if no recognizable day, assume the first */
288 /* if month is out of range, treat it as '*' */
289 if (month
< 1 || month
> 12) {
290 flags
|= F_ISMONTH
| F_WILDMONTH
;
291 month
= tp
->tm_mon
+ 1;
294 if (flags
& F_WILDMONTH
&& flags
& F_WILDDAY
)
297 if (flags
& F_WILDMONTH
&& flags
& F_ISDAY
&& day
== tp
->tm_mday
)
300 if (flags
& F_WILDMONTH
&& flags
& F_ISDOW
&& day
== tp
->tm_wday
+ 1)
303 if (flags
& F_ISMONTH
&& flags
& F_WILDDAY
&& month
== tp
->tm_mon
+ 1)
306 if (flags
& F_ISMONTH
&& flags
& F_ISDOW
&& month
== tp
->tm_mon
+ 1 &&
307 day
== tp
->tm_wday
+ 1)
311 day
= tp
->tm_mday
+ (((day
- 1) - tp
->tm_wday
+ 7) % 7);
312 day
= cumdays
[month
] + day
;
314 /* if today or today + offset days */
315 if (day
>= tp
->tm_yday
&& day
<= tp
->tm_yday
+ offset
)
318 /* if number of days left in this year + days to event in next year */
319 if (yrdays
- tp
->tm_yday
+ day
<= offset
)
326 getfield(char *p
, char **endp
, int *flags
)
333 * note this macro has an arg that isn't used ... it is retained
334 * (it is believed) to make the macro call look more "natural"
335 * and suggest at the call site what is happening.
337 #define FLDCHAR(a) (*p != '\0' && !isdigit((unsigned char)*p) && \
338 !isalpha((unsigned char)*p) && *p != '*')
341 for (/*EMPTY*/; FLDCHAR(*p
); ++p
)
343 if (*p
== '*') { /* `*' is current month */
344 if (!(*flags
& F_ISMONTH
)) {
345 *flags
|= F_ISMONTH
| F_WILDMONTH
;
347 return tp
->tm_mon
+ 1;
349 *flags
|= F_ISDAY
| F_WILDDAY
;
354 if (isdigit((unsigned char)*p
)) {
355 val
= (int)strtol(p
, &p
, 10); /* if 0, it's failure */
356 for (/*EMPTY*/; FLDCHAR(*p
); ++p
)
361 for (start
= p
; *p
!= '\0' && isalpha((unsigned char)*p
); p
++)
367 if ((val
= getmonth(start
)) != 0)
369 else if ((val
= getday(start
)) != 0)
377 for (*p
= savech
; FLDCHAR(*p
); ++p
)
389 /* open up calendar file as stdin */
391 for (const char **name
= defaultnames
; *name
!= NULL
; name
++) {
392 if ((fd
= open(*name
, O_RDONLY
)) == -1)
400 err(EXIT_FAILURE
, "Cannot open calendar file");
402 } else if ((fd
= open(fname
, O_RDONLY
)) == -1) {
405 err(EXIT_FAILURE
, "Cannot open `%s'", fname
);
408 if (pipe(pdes
) == -1) {
409 warn("Cannot open pipe");
416 (void)close(pdes
[0]);
417 (void)close(pdes
[1]);
421 /* set stdin to calendar file */
422 if (fd
!= STDIN_FILENO
) {
423 (void)dup2(fd
, STDIN_FILENO
);
426 /* set stdout to pipe input */
427 if (pdes
[1] != STDOUT_FILENO
) {
428 (void)dup2(pdes
[1], STDOUT_FILENO
);
429 (void)close(pdes
[1]);
431 (void)close(pdes
[0]);
432 /* tell CPP to only open regular files */
433 if(!cpp_restricted
&& setenv("CPP_RESTRICTED", "", 1) == -1)
434 err(EXIT_FAILURE
, "Cannot restrict cpp");
435 cpp_restricted
= true;
437 (void)execl(_PATH_CPP
, "cpp", "-traditional", "-P", "-I.",
438 "-I" _PATH_CALENDARS
, NULL
);
439 err(EXIT_FAILURE
, "Cannot exec `%s'", _PATH_CPP
);
442 /* parent -- fdopen *in to pipe output */
443 *in
= fdopen(pdes
[0], "r");
444 (void)close(pdes
[1]);
446 /* close calendar file */
449 /* not reading all calendar files, just set output to stdout */
454 * Set output to a temporary file, so if no output
457 (void)snprintf(path
, sizeof(path
), "%s/_calXXXXXX", _PATH_TMP
);
458 if ((fd
= mkstemp(path
)) == -1) {
459 warn("Cannot create temporary file");
462 return fdopen(fd
, "w+");
480 if (fstat(fileno(fp
), &sbuf
) == -1 || sbuf
.st_size
== 0)
482 if (pipe(pdes
) == -1)
488 (void)close(pdes
[0]);
489 (void)close(pdes
[1]);
492 /* child -- set stdin to pipe output */
493 if (pdes
[0] != STDIN_FILENO
) {
494 (void)dup2(pdes
[0], STDIN_FILENO
);
495 (void)close(pdes
[0]);
497 (void)close(pdes
[1]);
498 (void)execl(_PATH_SENDMAIL
, "sendmail", "-i", "-t", "-F",
499 "\"Reminder Service\"", "-f", "root", NULL
);
500 err(EXIT_FAILURE
, "Cannot exec `%s'", _PATH_SENDMAIL
);
503 /* parent -- write to pipe input */
504 (void)close(pdes
[0]);
506 header
[1].iov_base
= header
[3].iov_base
= (void *)pw
->pw_name
;
507 header
[1].iov_len
= header
[3].iov_len
= strlen(pw
->pw_name
);
508 (void)writev(pdes
[1], header
, 7);
509 while ((nread
= read(fileno(fp
), buf
, sizeof(buf
))) > 0)
510 (void)write(pdes
[1], buf
, (size_t)nread
);
511 (void)close(pdes
[1]);
515 done
: (void)fclose(fp
);
517 while (wait(&status
) != -1)
526 for (p
= months
; *p
; ++p
)
527 if (strncasecmp(s
, *p
, 3) == 0)
528 return (int)(p
- months
) + 1;
537 for (p
= days
; *p
; ++p
)
538 if (strncasecmp(s
, *p
, 3) == 0)
539 return (int)(p
- days
) + 1;
544 atodays(int ch
, char *arg
, unsigned short *rvp
)
549 if (u
< 0 || u
> 366)
550 warnx("-%c %d out of range 0-366, ignored.", ch
, u
);
555 #define todigit(x) ((x) - '0')
556 #define ATOI2(x) (todigit((x)[0]) * 10 + todigit((x)[1]))
557 #define ISDIG2(x) (isdigit((unsigned char)(x)[0]) && isdigit((unsigned char)(x)[1]))
560 getmmdd(struct tm
*ptm
, char *ds
)
569 ttm
.tm_mon
= ATOI2(ds
) - 1;
573 ttm
.tm_mday
= ATOI2(ds
);
578 if (ISDIG2(ds
) && ISDIG2(ds
+ 2)) {
579 ttm
.tm_year
= ATOI2(ds
) * 100 - TM_YEAR_BASE
;
581 ttm
.tm_year
+= ATOI2(ds
);
582 } else if (ISDIG2(ds
)) {
583 ttm
.tm_year
= ATOI2(ds
);
584 if (ttm
.tm_year
< 69)
585 ttm
.tm_year
+= 2000 - TM_YEAR_BASE
;
587 ttm
.tm_year
+= 1900 - TM_YEAR_BASE
;
590 if (ok
&& mktime(&ttm
) == -1)
596 warnx("Can't convert `%s' to date, ignored.", ds
);
605 (void)fprintf(stderr
, "usage: %s [-ax] [-d MMDD[[YY]YY]"
606 " [-f fname] [-l days] [-w days]\n", getprogname());