1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
From: Christian Kastner <ckk@kvr.at>
Date: Fri, 15 Jan 2016 20:35:09 +0100
Subject: Suppress crontab header by default
Change the default behaviour including a warning header when editing a crontab
to not doing so. This makes `crontab -l | crontab -` idempotent.
Adding the header can be fored by exporting CRONTAB_NOHEADER=N.
Contributed by Steve Greenland <stevegr@debian.org>.
Forwarded: no
Last-Update: 2016-01-15
---
crontab.1 | 25 ++++++++++++++++++++++++-
crontab.c | 29 +++++++++++++++++++++++++++++
2 files changed, 53 insertions(+), 1 deletion(-)
diff --git a/crontab.1 b/crontab.1
index b349099..f432097 100644
--- a/crontab.1
+++ b/crontab.1
@@ -82,7 +82,10 @@ named file or standard input if the pseudo-filename ``-'' is given.
.PP
The
.I \-l
-option causes the current crontab to be displayed on standard output.
+option causes the current crontab to be displayed on standard output. See
+the note under
+.B DEBIAN SPECIFIC
+below.
.PP
The
.I \-r
@@ -97,6 +100,26 @@ from the editor, the modified crontab will be installed automatically. If
neither of the environment variables is defined, then the
default editor /usr/bin/editor is used.
.PP
+.SH DEBIAN SPECIFIC
+The "out-of-the-box" behaviour for
+.I crontab \-l
+is to display the three line "DO NOT EDIT THIS FILE" header
+that is placed at the
+beginning of the crontab when it is installed. The problem is that
+it makes the sequence
+.PP
+crontab \-l | crontab \-
+.PP
+non-idempotent \(em you keep adding copies of the header. This causes
+pain to scripts that use sed to edit a crontab. Therefore, the default
+behaviour of the
+.B \-l
+option has been changed to not output such header. You may obtain the
+original behaviour by setting the environment variable
+.B CRONTAB_NOHEADER
+to 'N', which will cause the
+.I crontab \-l
+command to emit the extraneous header.
.SH "SEE ALSO"
crontab(5), cron(8)
.SH FILES
diff --git a/crontab.c b/crontab.c
index c81bc87..033d536 100644
--- a/crontab.c
+++ b/crontab.c
@@ -292,6 +292,8 @@ list_cmd() {
char n[MAX_FNAME];
FILE *f;
int ch;
+ int x;
+ char *ctnh;
log_it(RealUser, Pid, "LIST", User);
(void) snprintf(n, MAX_FNAME, CRON_TAB(User));
@@ -307,6 +309,33 @@ list_cmd() {
/* file is open. copy to stdout, close.
*/
Set_LineNum(1)
+
+ /* Don't list header lines unless CRONTAB_NOHEADER is 'N'.
+ *
+ * ignore the top few comments since we probably put them there.
+ */
+ if (!(ctnh = getenv("CRONTAB_NOHEADER")) || toupper(*ctnh) != 'N') {
+ for (x = 0; x < NHEADER_LINES; x++) {
+ ch = get_char(f);
+ if (EOF == ch) {
+ break;
+ }
+
+ if ('#' != ch) {
+ putchar(ch);
+ break;
+ }
+ while (EOF != (ch = get_char(f))) {
+ if (ch == '\n') {
+ break;
+ }
+ }
+ if (EOF == ch) {
+ break;
+ }
+ }
+ }
+
while (EOF != (ch = get_char(f)))
putchar(ch);
fclose(f);
|