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 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. 20 * 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. 32 */ 33 34 #ifndef lint 35 static char copyright[] = 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 41 static char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94"; 42 #endif /* not lint */ 43 44 #include <sys/param.h> 45 #include <stdio.h> 46 #include <ctype.h> 47 #include <sys/types.h> 48 #include <sys/stat.h> 49 #include <unistd.h> 50 #include <err.h> 51 #include <errno.h> 52 #include <string.h> 53 #include <sys/uio.h> 54 #include <sys/time.h> 55 #include <stdlib.h> 56 #include <pwd.h> 57 #include <sys/wait.h> 58 59 #include "pathnames.h" 60 #include "calendar.h" 61 62 63 char *calendarFile = "calendar"; /* default calendar file */ 64 char *calendarHome = ".calendar"; /* HOME */ 65 char *calendarNoMail = "nomail"; /* don't sent mail if this file exist */ 66 67 struct iovec header[] = { 68 "From: ", 6, 69 NULL, 0, 70 " (Reminder Service)\nTo: ", 24, 71 NULL, 0, 72 "\nSubject: ", 10, 73 NULL, 0, 74 "'s Calendar\nPrecedence: bulk\n\n", 30, 75 }; 76 77 78 void 79 cal() 80 { 81 register int printing; 82 register char *p; 83 FILE *fp; 84 int ch; 85 char buf[2048 + 1]; 86 87 if ((fp = opencal()) == NULL) 88 return; 89 for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) { 90 if ((p = strchr(buf, '\n')) != NULL) 91 *p = '\0'; 92 else 93 while ((ch = getchar()) != '\n' && ch != EOF); 94 if (buf[0] == '\0') 95 continue; 96 if (buf[0] != '\t') 97 printing = isnow(buf) ? 1 : 0; 98 if (printing) 99 (void)fprintf(fp, "%s\n", buf); 100 } 101 closecal(fp); 102 } 103 104 int 105 getfield(p, endp, flags) 106 char *p, **endp; 107 int *flags; 108 { 109 int val, var; 110 char *start, savech; 111 112 for (; !isdigit(*p) && !isalpha(*p) && *p != '*'; ++p); 113 if (*p == '*') { /* `*' is current month */ 114 *flags |= F_ISMONTH; 115 *endp = p+1; 116 return (tp->tm_mon + 1); 117 } 118 if (isdigit(*p)) { 119 val = strtol(p, &p, 10); /* if 0, it's failure */ 120 for (; !isdigit(*p) && !isalpha(*p) && *p != '*'; ++p); 121 *endp = p; 122 return (val); 123 } 124 for (start = p; isalpha(*++p);); 125 126 /* Sunday-1 */ 127 if (*p == '+' || *p == '-') 128 for(; isdigit(*++p);); 129 130 savech = *p; 131 *p = '\0'; 132 133 /* Month */ 134 if ((val = getmonth(start)) != 0) 135 *flags |= F_ISMONTH; 136 137 /* Day */ 138 else if ((val = getday(start)) != 0) { 139 *flags |= F_ISDAY; 140 141 /* variable weekday */ 142 if ((var = getdayvar(start)) != 0) { 143 if (var <=5 && var >= -4) 144 val += var * 10; 145 #ifdef DEBUG 146 printf("var: %d\n", var); 147 #endif 148 } 149 } 150 151 /* Easter */ 152 else if ((val = geteaster(start, tp->tm_year + 1900)) != 0) 153 *flags |= F_EASTER; 154 155 /* undefined rest */ 156 else { 157 *p = savech; 158 return (0); 159 } 160 for (*p = savech; !isdigit(*p) && !isalpha(*p) && *p != '*'; ++p); 161 *endp = p; 162 return (val); 163 } 164 165 char path[MAXPATHLEN + 1]; 166 167 FILE * 168 opencal() 169 { 170 int fd, pdes[2]; 171 struct stat sbuf; 172 173 /* open up calendar file as stdin */ 174 if (!freopen(calendarFile, "r", stdin)) { 175 if (doall) { 176 if (chdir(calendarHome) != 0) 177 return (NULL); 178 if (stat(calendarNoMail, &sbuf) == 0) 179 return (NULL); 180 if (!freopen(calendarFile, "r", stdin)) 181 return (NULL); 182 } else { 183 chdir(getenv("HOME")); 184 if (!(chdir(calendarHome) == 0 && 185 freopen(calendarFile, "r", stdin))) 186 errx(1, "no calendar file: ``%s'' or ``~/%s/%s\n", calendarFile, calendarHome, calendarFile); 187 } 188 } 189 if (pipe(pdes) < 0) 190 return (NULL); 191 switch (vfork()) { 192 case -1: /* error */ 193 (void)close(pdes[0]); 194 (void)close(pdes[1]); 195 return (NULL); 196 case 0: 197 /* child -- stdin already setup, set stdout to pipe input */ 198 if (pdes[1] != STDOUT_FILENO) { 199 (void)dup2(pdes[1], STDOUT_FILENO); 200 (void)close(pdes[1]); 201 } 202 (void)close(pdes[0]); 203 execl(_PATH_CPP, "cpp", "-P", "-I.", _PATH_INCLUDE, NULL); 204 (void)fprintf(stderr, 205 "calendar: execl: %s: %s.\n", _PATH_CPP, strerror(errno)); 206 _exit(1); 207 } 208 /* parent -- set stdin to pipe output */ 209 (void)dup2(pdes[0], STDIN_FILENO); 210 (void)close(pdes[0]); 211 (void)close(pdes[1]); 212 213 /* not reading all calendar files, just set output to stdout */ 214 if (!doall) 215 return (stdout); 216 217 /* set output to a temporary file, so if no output don't send mail */ 218 (void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP); 219 if ((fd = mkstemp(path)) < 0) 220 return (NULL); 221 return (fdopen(fd, "w+")); 222 } 223 224 void 225 closecal(fp) 226 FILE *fp; 227 { 228 struct stat sbuf; 229 int nread, pdes[2], status; 230 char buf[1024]; 231 232 if (!doall) 233 return; 234 235 (void)rewind(fp); 236 if (fstat(fileno(fp), &sbuf) || !sbuf.st_size) 237 goto done; 238 if (pipe(pdes) < 0) 239 goto done; 240 switch (vfork()) { 241 case -1: /* error */ 242 (void)close(pdes[0]); 243 (void)close(pdes[1]); 244 goto done; 245 case 0: 246 /* child -- set stdin to pipe output */ 247 if (pdes[0] != STDIN_FILENO) { 248 (void)dup2(pdes[0], STDIN_FILENO); 249 (void)close(pdes[0]); 250 } 251 (void)close(pdes[1]); 252 execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F", 253 "\"Reminder Service\"", "-f", "root", NULL); 254 (void)fprintf(stderr, 255 "calendar: %s: %s.\n", _PATH_SENDMAIL, strerror(errno)); 256 _exit(1); 257 } 258 /* parent -- write to pipe input */ 259 (void)close(pdes[0]); 260 261 header[1].iov_base = header[3].iov_base = pw->pw_name; 262 header[1].iov_len = header[3].iov_len = strlen(pw->pw_name); 263 writev(pdes[1], header, 7); 264 while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0) 265 (void)write(pdes[1], buf, nread); 266 (void)close(pdes[1]); 267 done: (void)fclose(fp); 268 (void)unlink(path); 269 while (wait(&status) >= 0); 270 } 271 272