- option = REPLACE;
- /* fall through */
- case REPLACE:
- {
- char buf[1024];
- char path[1024];
- int fd;
- int n;
-
- snprintf(path, sizeof(path), "%s.new", pas->pw_name);
- if ((fd = open(path, O_CREAT|O_TRUNC|O_APPEND|O_WRONLY, 0600)) >= 0) {
- while ((n = read(repFd, buf, sizeof(buf))) > 0) {
- write(fd, buf, n);
+
+ /*
+ * Change directory to our crontab directory
+ */
+
+ xchdir(CDir);
+
+ /*
+ * Handle options as appropriate
+ */
+
+ switch (option) {
+ case LIST:
+ {
+ FILE *fi;
+
+ fi = fopen(pas->pw_name, "r");
+ if (fi) {
+ while (fgets(buf, sizeof(buf), fi) != NULL)
+ fputs(buf, stdout);
+ fclose(fi);
+ } else {
+ bb_error_msg("no crontab for %s", pas->pw_name);
+ }
+ }
+ break;
+ case EDIT:
+ {
+ FILE *fi;
+ int fd;
+ int n;
+ char tmp[128];
+
+ snprintf(tmp, sizeof(tmp), TMPDIR "/crontab.%d", getpid());
+ fd = xopen3(tmp, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0600);
+ chown(tmp, getuid(), getgid());
+ fi = fopen(pas->pw_name, "r");
+ if (fi) {
+ while ((n = fread(buf, 1, sizeof(buf), fi)) > 0)
+ write(fd, buf, n);
+ }
+ EditFile(caller, tmp);
+ remove(tmp);
+ lseek(fd, 0L, SEEK_SET);
+ repFd = fd;
+ }
+ option = REPLACE;
+ /* fall through */
+ case REPLACE:
+ {
+ char path[1024];
+ int fd;
+ int n;
+
+ snprintf(path, sizeof(path), "%s.new", pas->pw_name);
+ fd = open(path, O_CREAT|O_TRUNC|O_APPEND|O_WRONLY, 0600);
+ if (fd >= 0) {
+ while ((n = read(repFd, buf, sizeof(buf))) > 0) {
+ write(fd, buf, n);
+ }
+ close(fd);
+ rename(path, pas->pw_name);
+ } else {
+ bb_error_msg("cannot create %s/%s", CDir, path);
+ }
+ close(repFd);