* `LICENSE' that comes with the fcron source distribution.
*/
- /* $Id: fcrontab.c,v 1.14 2000-09-12 19:53:03 thib Exp $ */
+ /* $Id: fcrontab.c,v 1.15 2000-09-15 19:49:24 thib Exp $ */
/*
* The goal of this program is simple : giving a user interface to fcron
#include "fcrontab.h"
-char rcs_info[] = "$Id: fcrontab.c,v 1.14 2000-09-12 19:53:03 thib Exp $";
+char rcs_info[] = "$Id: fcrontab.c,v 1.15 2000-09-15 19:49:24 thib Exp $";
void info(void);
void usage(void);
if ( (from = fopen(orig, "r")) == NULL
|| (to = fopen(dest, "w")) == NULL) {
- perror("copy");
+ error_e("copy");
return ;
}
if ( errno == ENOENT )
return ENOENT;
else
- perror(buf);
+ error_e("could not removing %s", buf);
}
}
sprintf(tmp, "/tmp/fcrontab.%d", getpid());
- if ( (file = open(tmp, O_CREAT|O_EXCL|O_WRONLY, 0600)) == -1 )
- die_e("could not create file %s", tmp);
- if ( (fi = fdopen(file, "w")) == NULL )
- die_e("could not fdopen");
+ if ( (file = open(tmp, O_CREAT|O_EXCL|O_WRONLY, 0600)) == -1 ) {
+ error_e("could not create file %s", tmp);
+ goto exiterr;
+ }
+ if ( (fi = fdopen(file, "w")) == NULL ) {
+ error_e("could not fdopen");
+ goto exiterr;
+ }
/* copy user's fcrontab (if any) to a temp file */
if ( (f = fopen(buf, "r")) == NULL ) {
- if ( errno != ENOENT )
- die_e("could not open file %s", buf);
+ if ( errno != ENOENT ) {
+ error_e("could not open file %s", buf);
+ goto exiterr;
+ }
else
fprintf(stderr, "no fcrontab for %s - using an empty one\n",
user);
fclose(f);
}
- if ( fchown(file, getuid(), getgid()) != 0 )
- die_e("could not chown %s", tmp);
+ if ( fchown(file, getuid(), getgid()) != 0 ) {
+ error_e("could not chown %s", tmp);
+ goto exiterr;
+ }
fclose(fi);
close(file);
if ( stat(tmp, &st) == 0 )
mtime = st.st_mtime;
- else
- die_e("could not stat '%s'", buf);
-
+ else {
+ error_e("could not stat '%s'", buf);
+ goto exiterr;
+ }
switch ( pid = fork() ) {
case 0:
/* child */
if (setuid(getuid()) < 0) {
- perror("setuid(getuid())");
- xexit(EXIT_ERR);
+ error_e("setuid(getuid())");
+ goto exiterr;
}
execlp(editor, editor, tmp, NULL);
- perror(editor);
- xexit(EXIT_ERR);
+ error_e(editor);
+ goto exiterr;
case -1:
- perror("fork");
- xexit(EXIT_ERR);
+ error_e("fork");
+ goto exiterr;
default:
/* parent */
if ( ! WIFEXITED(status) ) {
fprintf(stderr, "Editor exited abnormally:"
" fcrontab is unchanged.\n");
- xexit(EXIT_ERR);
+ goto exiterr;
}
/* check if file has been modified */
- if ( stat(tmp, &st) != 0 )
- die_e("could not stat %s", tmp);
+ if ( stat(tmp, &st) != 0 ) {
+ error_e("could not stat %s", tmp);
+ goto exiterr;
+ }
else if ( st.st_mtime > mtime || correction == 1) {
switch ( read_file(tmp, user) ) {
case ERR:
- if ( remove(tmp) != 0 )
- error("could not remove %s", tmp);
- xexit (EXIT_ERR);
+ goto exiterr;
case 2:
fprintf(stderr, "\nFile contains some errors. "
"Ignore [i] or Correct [c] ? ");
else {
fprintf(stderr, "Fcrontab is unchanged :"
" no need to install it.\n");
- xexit(EXIT_OK);
+ goto end;
}
} while ( correction == 1);
/* tell daemon to update the conf */
need_sig = 1;
+ end:
if ( remove(tmp) != 0 )
error("could not remove %s", tmp);
-
xexit (EXIT_OK);
+
+ exiterr:
+ if ( remove(tmp) != 0 )
+ error("could not remove %s", tmp);
+ xexit (EXIT_ERR);
+
}
fclose(tmp_file);
- if ( chown(tmp, getuid(), getgid()) != 0 )
- die_e("could not chown %s", tmp);
-
- if ( make_file(tmp) == ERR ) {
- remove(tmp);
- xexit ( EXIT_ERR );
+ if ( chown(tmp, getuid(), getgid()) != 0 ) {
+ error_e("could not chown %s", tmp);
+ goto exiterr;
}
+
+ if ( make_file(tmp) == ERR )
+ goto exiterr;
else {
remove(tmp);
xexit ( EXIT_OK );
}
+ exiterr:
+ remove(tmp);
+ xexit ( EXIT_ERR );
+
}
void
/* get current dir */
if ( (orig_dir = getcwd(NULL, 0)) == NULL )
- perror("getcwd");
+ error_e("getcwd");
/* change directory */
if (chdir(cdir) != 0) {
- perror("Could not chdir to " FCRONTABS );
+ error_e("Could not chdir to " FCRONTABS );
xexit (EXIT_ERR);
}