diff options
author | dds <dds@FreeBSD.org> | 2004-09-13 21:04:30 +0000 |
---|---|---|
committer | dds <dds@FreeBSD.org> | 2004-09-13 21:04:30 +0000 |
commit | 24d594eda771f1858e326345856afecfe73af001 (patch) | |
tree | afd33151a6d18d3771bbcfc7972ecb06277f2408 /usr.sbin/cron | |
parent | eb087a4ece0c8c616c45b975b0f206dff8cce308 (diff) | |
download | FreeBSD-src-24d594eda771f1858e326345856afecfe73af001.zip FreeBSD-src-24d594eda771f1858e326345856afecfe73af001.tar.gz |
Refactoring: move two similar code blocks into a seprate function.
Diffstat (limited to 'usr.sbin/cron')
-rw-r--r-- | usr.sbin/cron/crontab/crontab.c | 78 |
1 files changed, 31 insertions, 47 deletions
diff --git a/usr.sbin/cron/crontab/crontab.c b/usr.sbin/cron/crontab/crontab.c index 9cda6f6..50268d6 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); |