crontab.c (30cfb241294100ece14c530fa5b432011772a014) crontab.c (25e9ca2b190c310e2b4be9437950cce6e76420c5)
1/* Copyright 1988,1990,1993,1994 by Paul Vixie
2 * All rights reserved
3 *
4 * Distribute freely, except: don't remove my name from the source or
5 * documentation (don't take credit for my work), mark your changes (don't
6 * get me blamed for your possible bugs), don't alter or remove this
7 * notice. May be sold if buildable source is provided to buyer. No
8 * warrantee of any kind, express or implied, is included with this

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

280}
281
282
283static void
284edit_cmd() {
285 char n[MAX_FNAME], q[MAX_TEMPSTR], *editor;
286 FILE *f;
287 int ch, t, x;
1/* Copyright 1988,1990,1993,1994 by Paul Vixie
2 * All rights reserved
3 *
4 * Distribute freely, except: don't remove my name from the source or
5 * documentation (don't take credit for my work), mark your changes (don't
6 * get me blamed for your possible bugs), don't alter or remove this
7 * notice. May be sold if buildable source is provided to buyer. No
8 * warrantee of any kind, express or implied, is included with this

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

280}
281
282
283static void
284edit_cmd() {
285 char n[MAX_FNAME], q[MAX_TEMPSTR], *editor;
286 FILE *f;
287 int ch, t, x;
288 struct stat statbuf;
288 struct stat statbuf, fsbuf;
289 time_t mtime;
290 WAIT_T waiter;
291 PID_T pid, xpid;
292 mode_t um;
293
294 log_it(RealUser, Pid, "BEGIN EDIT", User);
295 (void) sprintf(n, CRON_TAB(User));
296 if (!(f = fopen(n, "r"))) {

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

312#ifdef HAS_FCHOWN
313 if (fchown(t, getuid(), getgid()) < 0) {
314#else
315 if (chown(Filename, getuid(), getgid()) < 0) {
316#endif
317 warn("fchown");
318 goto fatal;
319 }
289 time_t mtime;
290 WAIT_T waiter;
291 PID_T pid, xpid;
292 mode_t um;
293
294 log_it(RealUser, Pid, "BEGIN EDIT", User);
295 (void) sprintf(n, CRON_TAB(User));
296 if (!(f = fopen(n, "r"))) {

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

312#ifdef HAS_FCHOWN
313 if (fchown(t, getuid(), getgid()) < 0) {
314#else
315 if (chown(Filename, getuid(), getgid()) < 0) {
316#endif
317 warn("fchown");
318 goto fatal;
319 }
320 if (!(NewCrontab = fdopen(t, "w"))) {
320 if (!(NewCrontab = fdopen(t, "r+"))) {
321 warn("fdopen");
322 goto fatal;
323 }
324
325 Set_LineNum(1)
326
327 /* ignore the top few comments since we probably put them there.
328 */

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

342 }
343
344 /* copy the rest of the crontab (if any) to the temp file.
345 */
346 if (EOF != ch)
347 while (EOF != (ch = get_char(f)))
348 putc(ch, NewCrontab);
349 fclose(f);
321 warn("fdopen");
322 goto fatal;
323 }
324
325 Set_LineNum(1)
326
327 /* ignore the top few comments since we probably put them there.
328 */

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

342 }
343
344 /* copy the rest of the crontab (if any) to the temp file.
345 */
346 if (EOF != ch)
347 while (EOF != (ch = get_char(f)))
348 putc(ch, NewCrontab);
349 fclose(f);
350 if (fclose(NewCrontab))
350 if (fflush(NewCrontab))
351 err(ERROR_EXIT, "%s", Filename);
351 err(ERROR_EXIT, "%s", Filename);
352 if (fstat(t, &fsbuf) < 0) {
353 warn("unable to fstat temp file");
354 goto fatal;
355 }
352 again:
353 if (stat(Filename, &statbuf) < 0) {
354 warn("stat");
355 fatal: unlink(Filename);
356 exit(ERROR_EXIT);
357 }
356 again:
357 if (stat(Filename, &statbuf) < 0) {
358 warn("stat");
359 fatal: unlink(Filename);
360 exit(ERROR_EXIT);
361 }
362 if (statbuf.st_dev != fsbuf.st_dev || statbuf.st_ino != fsbuf.st_ino)
363 errx(ERROR_EXIT, "temp file must be edited in place");
358 mtime = statbuf.st_mtime;
359
360 if ((!(editor = getenv("VISUAL")))
361 && (!(editor = getenv("EDITOR")))
362 ) {
363 editor = EDITOR;
364 }
365

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

414 warnx("\"%s\" killed; signal %d (%score dumped)",
415 editor, WTERMSIG(waiter), WCOREDUMP(waiter) ?"" :"no ");
416 goto fatal;
417 }
418 if (stat(Filename, &statbuf) < 0) {
419 warn("stat");
420 goto fatal;
421 }
364 mtime = statbuf.st_mtime;
365
366 if ((!(editor = getenv("VISUAL")))
367 && (!(editor = getenv("EDITOR")))
368 ) {
369 editor = EDITOR;
370 }
371

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

420 warnx("\"%s\" killed; signal %d (%score dumped)",
421 editor, WTERMSIG(waiter), WCOREDUMP(waiter) ?"" :"no ");
422 goto fatal;
423 }
424 if (stat(Filename, &statbuf) < 0) {
425 warn("stat");
426 goto fatal;
427 }
428 if (statbuf.st_dev != fsbuf.st_dev || statbuf.st_ino != fsbuf.st_ino)
429 errx(ERROR_EXIT, "temp file must be edited in place");
422 if (mtime == statbuf.st_mtime) {
423 warnx("no changes made to crontab");
424 goto remove;
425 }
426 warnx("installing new crontab");
430 if (mtime == statbuf.st_mtime) {
431 warnx("no changes made to crontab");
432 goto remove;
433 }
434 warnx("installing new crontab");
427 if (!(NewCrontab = fopen(Filename, "r"))) {
428 warn("%s", Filename);
429 goto fatal;
430 }
431 switch (replace_cmd()) {
432 case 0:
433 break;
434 case -1:
435 for (;;) {
436 printf("Do you want to retry the same edit? ");
437 fflush(stdout);
438 q[0] = '\0';

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

492 * VERY IMPORTANT: make sure NHEADER_LINES agrees with this code.
493 */
494 fprintf(tmp, "# DO NOT EDIT THIS FILE - edit the master and reinstall.\n");
495 fprintf(tmp, "# (%s installed on %-24.24s)\n", Filename, ctime(&now));
496 fprintf(tmp, "# (Cron version -- %s)\n", rcsid);
497
498 /* copy the crontab to the tmp
499 */
435 switch (replace_cmd()) {
436 case 0:
437 break;
438 case -1:
439 for (;;) {
440 printf("Do you want to retry the same edit? ");
441 fflush(stdout);
442 q[0] = '\0';

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

496 * VERY IMPORTANT: make sure NHEADER_LINES agrees with this code.
497 */
498 fprintf(tmp, "# DO NOT EDIT THIS FILE - edit the master and reinstall.\n");
499 fprintf(tmp, "# (%s installed on %-24.24s)\n", Filename, ctime(&now));
500 fprintf(tmp, "# (Cron version -- %s)\n", rcsid);
501
502 /* copy the crontab to the tmp
503 */
504 rewind(NewCrontab);
500 Set_LineNum(1)
501 while (EOF != (ch = get_char(NewCrontab)))
502 putc(ch, tmp);
505 Set_LineNum(1)
506 while (EOF != (ch = get_char(NewCrontab)))
507 putc(ch, tmp);
503 fclose(NewCrontab);
504 ftruncate(fileno(tmp), ftell(tmp));
505 fflush(tmp); rewind(tmp);
506
507 if (ferror(tmp)) {
508 warnx("error while writing new crontab to %s", tn);
509 fclose(tmp); unlink(tn);
510 return (-2);
511 }

--- 92 unchanged lines hidden ---
508 ftruncate(fileno(tmp), ftell(tmp));
509 fflush(tmp); rewind(tmp);
510
511 if (ferror(tmp)) {
512 warnx("error while writing new crontab to %s", tn);
513 fclose(tmp); unlink(tn);
514 return (-2);
515 }

--- 92 unchanged lines hidden ---