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 int month; 86 int day; 87 int var; 88 char buf[2048 + 1]; 89 90 if ((fp = opencal()) == NULL) 91 return; 92 for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) { 93 if ((p = strchr(buf, '\n')) != NULL) 94 *p = '\0'; 95 else 96 while ((ch = getchar()) != '\n' && ch != EOF); 97 if (buf[0] == '\0') 98 continue; 99 if (buf[0] != '\t') { 100 printing = isnow(buf, &month, &day, &var) ? 1 : 0; 101 if ((p = strchr(buf, '\t')) == NULL) 102 continue; 103 if (p > buf && p[-1] == '*') 104 var = 1; 105 if (printing) 106 (void)fprintf(fp, "%.2d/%.2d%c%s\n", month, 107 day, var ? '*' : ' ', p); 108 } 109 else if (printing) 110 fprintf(fp, "%s\n", buf); 111 } 112 closecal(fp); 113 } 114 115 int 116 getfield(p, endp, flags) 117 char *p, **endp; 118 int *flags; 119 { 120 int val, var; 121 char *start, savech; 122 123 for (; !isdigit(*p) && !isalpha(*p) && *p != '*'; ++p); 124 if (*p == '*') { /* `*' is current month */ 125 *flags |= F_ISMONTH; 126 *endp = p+1; 127 return (tp->tm_mon + 1); 128 } 129 if (isdigit(*p)) { 130 val = strtol(p, &p, 10); /* if 0, it's failure */ 131 for (; !isdigit(*p) && !isalpha(*p) && *p != '*'; ++p); 132 *endp = p; 133 return (val); 134 } 135 for (start = p; isalpha(*++p);); 136 137 /* Sunday-1 */ 138 if (*p == '+' || *p == '-') 139 for(; isdigit(*++p);); 140 141 savech = *p; 142 *p = '\0'; 143 144 /* Month */ 145 if ((val = getmonth(start)) != 0) 146 *flags |= F_ISMONTH; 147 148 /* Day */ 149 else if ((val = getday(start)) != 0) { 150 *flags |= F_ISDAY; 151 152 /* variable weekday */ 153 if ((var = getdayvar(start)) != 0) { 154 if (var <=5 && var >= -4) 155 val += var * 10; 156 #ifdef DEBUG 157 printf("var: %d\n", var); 158 #endif 159 } 160 } 161 162 /* Easter */ 163 else if ((val = geteaster(start, tp->tm_year + 1900)) != 0) 164 *flags |= F_EASTER; 165 166 /* undefined rest */ 167 else { 168 *p = savech; 169 return (0); 170 } 171 for (*p = savech; !isdigit(*p) && !isalpha(*p) && *p != '*'; ++p); 172 *endp = p; 173 return (val); 174 } 175 176 char path[MAXPATHLEN + 1]; 177 178 FILE * 179 opencal() 180 { 181 int fd, pdes[2]; 182 struct stat sbuf; 183 184 /* open up calendar file as stdin */ 185 if (!freopen(calendarFile, "r", stdin)) { 186 if (doall) { 187 if (chdir(calendarHome) != 0) 188 return (NULL); 189 if (stat(calendarNoMail, &sbuf) == 0) 190 return (NULL); 191 if (!freopen(calendarFile, "r", stdin)) 192 return (NULL); 193 } else { 194 chdir(getenv("HOME")); 195 if (!(chdir(calendarHome) == 0 && 196 freopen(calendarFile, "r", stdin))) 197 errx(1, "no calendar file: ``%s'' or ``~/%s/%s\n", calendarFile, calendarHome, calendarFile); 198 } 199 } 200 if (pipe(pdes) < 0) 201 return (NULL); 202 switch (vfork()) { 203 case -1: /* error */ 204 (void)close(pdes[0]); 205 (void)close(pdes[1]); 206 return (NULL); 207 case 0: 208 /* child -- stdin already setup, set stdout to pipe input */ 209 if (pdes[1] != STDOUT_FILENO) { 210 (void)dup2(pdes[1], STDOUT_FILENO); 211 (void)close(pdes[1]); 212 } 213 (void)close(pdes[0]); 214 execl(_PATH_CPP, "cpp", "-P", "-I.", _PATH_INCLUDE, NULL); 215 (void)fprintf(stderr, 216 "calendar: execl: %s: %s.\n", _PATH_CPP, strerror(errno)); 217 _exit(1); 218 } 219 /* parent -- set stdin to pipe output */ 220 (void)dup2(pdes[0], STDIN_FILENO); 221 (void)close(pdes[0]); 222 (void)close(pdes[1]); 223 224 /* not reading all calendar files, just set output to stdout */ 225 if (!doall) 226 return (stdout); 227 228 /* set output to a temporary file, so if no output don't send mail */ 229 (void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP); 230 if ((fd = mkstemp(path)) < 0) 231 return (NULL); 232 return (fdopen(fd, "w+")); 233 } 234 235 void 236 closecal(fp) 237 FILE *fp; 238 { 239 struct stat sbuf; 240 int nread, pdes[2], status; 241 char buf[1024]; 242 243 if (!doall) 244 return; 245 246 (void)rewind(fp); 247 if (fstat(fileno(fp), &sbuf) || !sbuf.st_size) 248 goto done; 249 if (pipe(pdes) < 0) 250 goto done; 251 switch (vfork()) { 252 case -1: /* error */ 253 (void)close(pdes[0]); 254 (void)close(pdes[1]); 255 goto done; 256 case 0: 257 /* child -- set stdin to pipe output */ 258 if (pdes[0] != STDIN_FILENO) { 259 (void)dup2(pdes[0], STDIN_FILENO); 260 (void)close(pdes[0]); 261 } 262 (void)close(pdes[1]); 263 execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F", 264 "\"Reminder Service\"", "-f", "root", NULL); 265 (void)fprintf(stderr, 266 "calendar: %s: %s.\n", _PATH_SENDMAIL, strerror(errno)); 267 _exit(1); 268 } 269 /* parent -- write to pipe input */ 270 (void)close(pdes[0]); 271 272 header[1].iov_base = header[3].iov_base = pw->pw_name; 273 header[1].iov_len = header[3].iov_len = strlen(pw->pw_name); 274 writev(pdes[1], header, 7); 275 while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0) 276 (void)write(pdes[1], buf, nread); 277 (void)close(pdes[1]); 278 done: (void)fclose(fp); 279 (void)unlink(path); 280 while (wait(&status) >= 0); 281 } 282 283