io.c (251647) | io.c (255715) |
---|---|
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 --- 26 unchanged lines hidden (view full) --- 35 36#if 0 37#ifndef lint 38static char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94"; 39#endif 40#endif 41 42#include <sys/cdefs.h> | 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 --- 26 unchanged lines hidden (view full) --- 35 36#if 0 37#ifndef lint 38static char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94"; 39#endif 40#endif 41 42#include <sys/cdefs.h> |
43__FBSDID("$FreeBSD: head/usr.bin/calendar/io.c 251647 2013-06-12 07:52:49Z grog $"); | 43__FBSDID("$FreeBSD: head/usr.bin/calendar/io.c 255715 2013-09-19 20:17:50Z db $"); |
44 45#include <sys/param.h> 46#include <sys/stat.h> 47#include <sys/wait.h> 48#include <ctype.h> 49#include <err.h> 50#include <errno.h> 51#include <langinfo.h> --- 24 unchanged lines hidden (view full) --- 76 errx(1, "cannot allocate memory"); \ 77 struct_.len = strlen(buf + (slen)); \ 78 continue; \ 79 } 80void 81cal(void) 82{ 83 char *pp, p; | 44 45#include <sys/param.h> 46#include <sys/stat.h> 47#include <sys/wait.h> 48#include <ctype.h> 49#include <err.h> 50#include <errno.h> 51#include <langinfo.h> --- 24 unchanged lines hidden (view full) --- 76 errx(1, "cannot allocate memory"); \ 77 struct_.len = strlen(buf + (slen)); \ 78 continue; \ 79 } 80void 81cal(void) 82{ 83 char *pp, p; |
84 FILE *fp; 85 int ch, l; | 84 FILE *fpin; 85 FILE *fpout; 86 int l; |
86 int count, i; 87 int month[MAXCOUNT]; 88 int day[MAXCOUNT]; 89 int year[MAXCOUNT]; 90 char **extradata; /* strings of 20 length */ 91 int flags; 92 static int d_first = -1; 93 char buf[2048 + 1]; 94 struct event *events[MAXCOUNT]; 95 struct tm tm; 96 char dbuf[80]; 97 | 87 int count, i; 88 int month[MAXCOUNT]; 89 int day[MAXCOUNT]; 90 int year[MAXCOUNT]; 91 char **extradata; /* strings of 20 length */ 92 int flags; 93 static int d_first = -1; 94 char buf[2048 + 1]; 95 struct event *events[MAXCOUNT]; 96 struct tm tm; 97 char dbuf[80]; 98 |
99 initcpp(); |
|
98 extradata = (char **)calloc(MAXCOUNT, sizeof(char *)); 99 for (i = 0; i < MAXCOUNT; i++) { 100 extradata[i] = (char *)calloc(1, 20); 101 } 102 103 /* Unused */ 104 tm.tm_sec = 0; 105 tm.tm_min = 0; 106 tm.tm_hour = 0; 107 tm.tm_wday = 0; 108 109 count = 0; | 100 extradata = (char **)calloc(MAXCOUNT, sizeof(char *)); 101 for (i = 0; i < MAXCOUNT; i++) { 102 extradata[i] = (char *)calloc(1, 20); 103 } 104 105 /* Unused */ 106 tm.tm_sec = 0; 107 tm.tm_min = 0; 108 tm.tm_hour = 0; 109 tm.tm_wday = 0; 110 111 count = 0; |
110 if ((fp = opencal()) == NULL) { | 112 if ((fpin = opencalin()) == NULL) { |
111 free(extradata); 112 return; 113 } | 113 free(extradata); 114 return; 115 } |
114 while (fgets(buf, sizeof(buf), stdin) != NULL) { 115 if ((pp = strchr(buf, '\n')) != NULL) 116 *pp = '\0'; 117 else 118 /* Flush this line */ 119 while ((ch = getchar()) != '\n' && ch != EOF); | 116 if ((fpout = opencalout()) == NULL) { 117 fclose(fpin); 118 free(extradata); 119 return; 120 } 121 while ((fpin = fincludegets(buf, sizeof(buf), fpin)) != NULL) { 122 if (*buf == '\0') 123 continue; |
120 for (l = strlen(buf); 121 l > 0 && isspace((unsigned char)buf[l - 1]); 122 l--) 123 ; 124 buf[l] = '\0'; 125 if (buf[0] == '\0') 126 continue; 127 --- 71 unchanged lines hidden (view full) --- 199 if (debug) 200 fprintf(stderr, "got %s\n", pp); 201 events[i] = event_add(year[i], month[i], day[i], dbuf, 202 ((flags &= F_VARIABLE) != 0) ? 1 : 0, pp, 203 extradata[i]); 204 } 205 } 206 | 124 for (l = strlen(buf); 125 l > 0 && isspace((unsigned char)buf[l - 1]); 126 l--) 127 ; 128 buf[l] = '\0'; 129 if (buf[0] == '\0') 130 continue; 131 --- 71 unchanged lines hidden (view full) --- 203 if (debug) 204 fprintf(stderr, "got %s\n", pp); 205 events[i] = event_add(year[i], month[i], day[i], dbuf, 206 ((flags &= F_VARIABLE) != 0) ? 1 : 0, pp, 207 extradata[i]); 208 } 209 } 210 |
207 event_print_all(fp); 208 closecal(fp); | 211 event_print_all(fpout); 212 closecal(fpout); |
209 free(extradata); 210} 211 212FILE * | 213 free(extradata); 214} 215 216FILE * |
213opencal(void) | 217opencalin(void) |
214{ | 218{ |
215 uid_t uid; | |
216 size_t i; | 219 size_t i; |
217 int fd, found, pdes[2]; | 220 int found; |
218 struct stat sbuf; | 221 struct stat sbuf; |
222 FILE *fpin; |
|
219 | 223 |
220 /* open up calendar file as stdin */ 221 if (!freopen(calendarFile, "r", stdin)) { | 224 /* open up calendar file */ 225 if ((fpin = fopen(calendarFile, "r")) == NULL) { |
222 if (doall) { 223 if (chdir(calendarHomes[0]) != 0) 224 return (NULL); 225 if (stat(calendarNoMail, &sbuf) == 0) 226 return (NULL); | 226 if (doall) { 227 if (chdir(calendarHomes[0]) != 0) 228 return (NULL); 229 if (stat(calendarNoMail, &sbuf) == 0) 230 return (NULL); |
227 if (!freopen(calendarFile, "r", stdin)) | 231 if ((fpin = fopen(calendarFile, "r")) == NULL) |
228 return (NULL); 229 } else { 230 char *home = getenv("HOME"); 231 if (home == NULL || *home == '\0') 232 errx(1, "cannot get home directory"); 233 if (chdir(home) != 0) 234 errx(1, "cannot enter home directory"); 235 for (found = i = 0; i < sizeof(calendarHomes) / 236 sizeof(calendarHomes[0]); i++) 237 if (chdir(calendarHomes[i]) == 0 && | 232 return (NULL); 233 } else { 234 char *home = getenv("HOME"); 235 if (home == NULL || *home == '\0') 236 errx(1, "cannot get home directory"); 237 if (chdir(home) != 0) 238 errx(1, "cannot enter home directory"); 239 for (found = i = 0; i < sizeof(calendarHomes) / 240 sizeof(calendarHomes[0]); i++) 241 if (chdir(calendarHomes[i]) == 0 && |
238 freopen(calendarFile, "r", stdin)) { | 242 (fpin = fopen(calendarFile, "r")) != NULL) { |
239 found = 1; 240 break; 241 } 242 if (!found) 243 errx(1, 244 "can't open calendar file \"%s\": %s (%d)", 245 calendarFile, strerror(errno), errno); 246 } 247 } | 243 found = 1; 244 break; 245 } 246 if (!found) 247 errx(1, 248 "can't open calendar file \"%s\": %s (%d)", 249 calendarFile, strerror(errno), errno); 250 } 251 } |
248 if (pipe(pdes) < 0) 249 return (NULL); 250 switch (fork()) { 251 case -1: /* error */ 252 (void)close(pdes[0]); 253 (void)close(pdes[1]); 254 return (NULL); 255 case 0: 256 /* child -- stdin already setup, set stdout to pipe input */ 257 if (pdes[1] != STDOUT_FILENO) { 258 (void)dup2(pdes[1], STDOUT_FILENO); 259 (void)close(pdes[1]); 260 } 261 (void)close(pdes[0]); 262 uid = geteuid(); 263 if (setuid(getuid()) < 0) { 264 warnx("first setuid failed"); 265 _exit(1); 266 }; 267 if (setgid(getegid()) < 0) { 268 warnx("setgid failed"); 269 _exit(1); 270 } 271 if (setuid(uid) < 0) { 272 warnx("setuid failed"); 273 _exit(1); 274 } 275 execl(_PATH_CPP, "cpp", "-P", 276 "-traditional-cpp", "-nostdinc", /* GCC specific opts */ 277 "-I.", "-I", _PATH_INCLUDE, (char *)NULL); 278 warn(_PATH_CPP); 279 _exit(1); 280 } 281 /* parent -- set stdin to pipe output */ 282 (void)dup2(pdes[0], STDIN_FILENO); 283 (void)close(pdes[0]); 284 (void)close(pdes[1]); | 252 return (fpin); 253} |
285 | 254 |
255FILE * 256opencalout(void) 257{ 258 int fd; 259 |
|
286 /* not reading all calendar files, just set output to stdout */ 287 if (!doall) 288 return (stdout); 289 290 /* set output to a temporary file, so if no output don't send mail */ | 260 /* not reading all calendar files, just set output to stdout */ 261 if (!doall) 262 return (stdout); 263 264 /* set output to a temporary file, so if no output don't send mail */ |
291 (void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP); | 265 snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP); |
292 if ((fd = mkstemp(path)) < 0) 293 return (NULL); 294 return (fdopen(fd, "w+")); 295} 296 297void 298closecal(FILE *fp) 299{ --- 61 unchanged lines hidden --- | 266 if ((fd = mkstemp(path)) < 0) 267 return (NULL); 268 return (fdopen(fd, "w+")); 269} 270 271void 272closecal(FILE *fp) 273{ --- 61 unchanged lines hidden --- |