aboutsummaryrefslogtreecommitdiff
path: root/usr.sbin/cron/crontab
diff options
context:
space:
mode:
authorDiomidis Spinellis <dds@FreeBSD.org>2004-09-13 21:04:30 +0000
committerDiomidis Spinellis <dds@FreeBSD.org>2004-09-13 21:04:30 +0000
commit9a2ef7d17247f84530974d8cc52c5350ae583758 (patch)
treeafd33151a6d18d3771bbcfc7972ecb06277f2408 /usr.sbin/cron/crontab
parent12653dec9dd530a86aa829857a3f25887282a0be (diff)
downloadsrc-9a2ef7d17247f84530974d8cc52c5350ae583758.tar.gz
src-9a2ef7d17247f84530974d8cc52c5350ae583758.zip
Refactoring: move two similar code blocks into a seprate function.
Notes
Notes: svn path=/head/; revision=135174
Diffstat (limited to 'usr.sbin/cron/crontab')
-rw-r--r--usr.sbin/cron/crontab/crontab.c78
1 files changed, 31 insertions, 47 deletions
diff --git a/usr.sbin/cron/crontab/crontab.c b/usr.sbin/cron/crontab/crontab.c
index 9cda6f6d94b4..50268d6f12c0 100644
--- a/usr.sbin/cron/crontab/crontab.c
+++ b/usr.sbin/cron/crontab/crontab.c
@@ -221,45 +221,52 @@ parse_args(argc, argv)
User, Filename, Options[(int)Option]))
}
-
static void
-list_cmd() {
- char n[MAX_FNAME];
- FILE *f;
- int ch, x;
-
- log_it(RealUser, Pid, "LIST", User);
- (void) sprintf(n, CRON_TAB(User));
- if (!(f = fopen(n, "r"))) {
- if (errno == ENOENT)
- errx(ERROR_EXIT, "no crontab for %s", User);
- else
- err(ERROR_EXIT, "%s", n);
- }
+copy_file(FILE *in, FILE *out) {
+ int x, ch;
- /* file is open. copy to stdout, close.
- */
Set_LineNum(1)
-
/* ignore the top few comments since we probably put them there.
*/
for (x = 0; x < NHEADER_LINES; x++) {
- ch = get_char(f);
+ ch = get_char(in);
if (EOF == ch)
break;
if ('#' != ch) {
- putchar(ch);
+ putc(ch, out);
break;
}
- while (EOF != (ch = get_char(f)))
+ while (EOF != (ch = get_char(in)))
if (ch == '\n')
break;
if (EOF == ch)
break;
}
- while (EOF != (ch = get_char(f)))
- putchar(ch);
+ /* copy the rest of the crontab (if any) to the output file.
+ */
+ if (EOF != ch)
+ while (EOF != (ch = get_char(in)))
+ putc(ch, out);
+}
+
+static void
+list_cmd() {
+ char n[MAX_FNAME];
+ FILE *f;
+
+ log_it(RealUser, Pid, "LIST", User);
+ (void) sprintf(n, CRON_TAB(User));
+ if (!(f = fopen(n, "r"))) {
+ if (errno == ENOENT)
+ errx(ERROR_EXIT, "no crontab for %s", User);
+ else
+ err(ERROR_EXIT, "%s", n);
+ }
+
+ /* file is open. copy to stdout, close.
+ */
+ copy_file(f, stdout);
fclose(f);
}
@@ -303,7 +310,7 @@ static void
edit_cmd() {
char n[MAX_FNAME], q[MAX_TEMPSTR], *editor;
FILE *f;
- int ch, t, x;
+ int t;
struct stat statbuf, fsbuf;
time_t mtime;
WAIT_T waiter;
@@ -342,30 +349,7 @@ edit_cmd() {
goto fatal;
}
- Set_LineNum(1)
-
- /* ignore the top few comments since we probably put them there.
- */
- for (x = 0; x < NHEADER_LINES; x++) {
- ch = get_char(f);
- if (EOF == ch)
- break;
- if ('#' != ch) {
- putc(ch, NewCrontab);
- break;
- }
- while (EOF != (ch = get_char(f)))
- if (ch == '\n')
- break;
- if (EOF == ch)
- break;
- }
-
- /* copy the rest of the crontab (if any) to the temp file.
- */
- if (EOF != ch)
- while (EOF != (ch = get_char(f)))
- putc(ch, NewCrontab);
+ copy_file(f, NewCrontab);
fclose(f);
if (fflush(NewCrontab))
err(ERROR_EXIT, "%s", Filename);