From 1db2eb64a2715560a24d3b58f615969ad21cb4fa Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Mon, 29 Jan 2018 09:25:40 +0100
Subject: [PATCH] newsyslog(8): Further reduce differences with FreeBSD.

---
 usr.sbin/newsyslog/Makefile         |  3 +-
 usr.sbin/newsyslog/extern.h         |  5 ++--
 usr.sbin/newsyslog/newsyslog.8      | 11 ++++++--
 usr.sbin/newsyslog/newsyslog.c      | 56 ++++++++++++++++++-------------------
 usr.sbin/newsyslog/newsyslog.conf.5 | 46 +++++++++++++++++++-----------
 usr.sbin/newsyslog/pathnames.h      |  2 +-
 usr.sbin/newsyslog/ptimes.c         | 27 +++++++++---------
 7 files changed, 85 insertions(+), 65 deletions(-)

diff --git a/usr.sbin/newsyslog/Makefile b/usr.sbin/newsyslog/Makefile
index b1d761f609..8ae12c734f 100644
--- a/usr.sbin/newsyslog/Makefile
+++ b/usr.sbin/newsyslog/Makefile
@@ -1,5 +1,4 @@
-# $FreeBSD: src/usr.sbin/newsyslog/Makefile,v 1.7.2.2 2001/11/14 16:55:38 obrien Exp $
-# $DragonFly: src/usr.sbin/newsyslog/Makefile,v 1.4 2007/05/12 08:52:00 swildner Exp $
+# $FreeBSD: head/usr.sbin/newsyslog/Makefile 322515 2017-08-14 19:21:37Z ngie $
 
 PROG=	newsyslog
 MAN=	newsyslog.8 newsyslog.conf.5
diff --git a/usr.sbin/newsyslog/extern.h b/usr.sbin/newsyslog/extern.h
index f4ce3bc5f9..85273b88b6 100644
--- a/usr.sbin/newsyslog/extern.h
+++ b/usr.sbin/newsyslog/extern.h
@@ -1,4 +1,6 @@
 /*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
  * ------+---------+---------+---------+---------+---------+---------+---------*
  * Copyright (c) 2003  - Garance Alistair Drosehn <gad@FreeBSD.org>.
  * All rights reserved.
@@ -29,8 +31,7 @@
  * official policies, either expressed or implied, of the FreeBSD Project.
  *
  * ------+---------+---------+---------+---------+---------+---------+---------*
- * $FreeBSD: src/usr.sbin/newsyslog/extern.h,v 1.2 2003/09/23 00:00:26 gad Exp $
- * $DragonFly: src/usr.sbin/newsyslog/extern.h,v 1.1 2007/05/12 08:52:00 swildner Exp $
+ * $FreeBSD: head/usr.sbin/newsyslog/extern.h 326276 2017-11-27 15:37:16Z pfg $
  * ------+---------+---------+---------+---------+---------+---------+---------*
  */
 
diff --git a/usr.sbin/newsyslog/newsyslog.8 b/usr.sbin/newsyslog/newsyslog.8
index ca866052d3..babfea5f0d 100644
--- a/usr.sbin/newsyslog/newsyslog.8
+++ b/usr.sbin/newsyslog/newsyslog.8
@@ -1,7 +1,7 @@
 .\" This file contains changes from the Open Software Foundation.
 .\"
 .\"	from: @(#)newsyslog.8
-.\" $FreeBSD: src/usr.sbin/newsyslog/newsyslog.8,v 1.57 2011/01/31 11:30:26 mm Exp $
+.\" $FreeBSD: head/usr.sbin/newsyslog/newsyslog.8 318960 2017-05-26 16:36:30Z dab $
 .\"
 .\" Copyright 1988, 1989 by the Massachusetts Institute of Technology
 .\"
@@ -155,6 +155,7 @@ will create the
 .Dq rotated
 logfiles using the specified time format instead of the default
 sequential filenames.
+The filename used will be kept until it is deleted.
 The time format is described in the
 .Xr strftime 3
 manual page.
@@ -259,10 +260,16 @@ If additional command line arguments are given,
 will only examine log files that match those arguments; otherwise, it
 will examine all files listed in the configuration file.
 .Sh FILES
-.Bl -tag -width /etc/newsyslog.confxxxx -compact
+.Bl -tag -width ".Pa /usr/local/etc/newsyslog.conf.d" -compact
 .It Pa /etc/newsyslog.conf
 .Nm
 configuration file
+.It Pa /etc/newsyslog.conf.d
+Each file in this directory will be included by the default
+.Pa newsyslog.conf .
+.It Pa /usr/local/etc/newsyslog.conf.d
+Each file in this directory will be included by the default
+.Pa newsyslog.conf .
 .El
 .Sh COMPATIBILITY
 Previous versions of the
diff --git a/usr.sbin/newsyslog/newsyslog.c b/usr.sbin/newsyslog/newsyslog.c
index 761bdcfed2..a98af9cced 100644
--- a/usr.sbin/newsyslog/newsyslog.c
+++ b/usr.sbin/newsyslog/newsyslog.c
@@ -46,7 +46,7 @@
  * for any purpose.  It is provided "as is" without express or implied
  * warranty.
  *
- * $FreeBSD: src/usr.sbin/newsyslog/newsyslog.c,v 1.117 2011/01/31 10:57:54 mm Exp $
+ * $FreeBSD: head/usr.sbin/newsyslog/newsyslog.c 327451 2017-12-31 22:01:36Z eadler $
  */
 
 /*
@@ -151,7 +151,7 @@ struct compress_types {
 	const char *flag;	/* Flag in configuration file */
 	const char *suffix;	/* Compression suffix */
 	const char *path;	/* Path to compression program */
-	char **args;		/* Compression arguments */
+	char **args;		/* Compression program arguments */
 };
 
 static char f_arg[] = "-f";
@@ -225,49 +225,51 @@ typedef enum {
 }	fk_entry;
 
 STAILQ_HEAD(cflist, conf_entry);
-static SLIST_HEAD(swlisthead, sigwork_entry) swhead = SLIST_HEAD_INITIALIZER(swhead);
-static SLIST_HEAD(zwlisthead, zipwork_entry) zwhead = SLIST_HEAD_INITIALIZER(zwhead);
+static SLIST_HEAD(swlisthead, sigwork_entry) swhead =
+    SLIST_HEAD_INITIALIZER(swhead);
+static SLIST_HEAD(zwlisthead, zipwork_entry) zwhead =
+    SLIST_HEAD_INITIALIZER(zwhead);
 STAILQ_HEAD(ilist, include_entry);
 
 int dbg_at_times;		/* -D Show details of 'trim_at' code */
 
-static int archtodir = 0;		/* Archive old logfiles to other directory */
-static int createlogs;			/* Create (non-GLOB) logfiles which do not */
+static int archtodir = 0;	/* Archive old logfiles to other directory */
+static int createlogs;		/* Create (non-GLOB) logfiles which do not */
 				/*    already exist.  1=='for entries with */
 				/*    C flag', 2=='for all entries'. */
 int verbose = 0;		/* Print out what's going on */
-static int needroot = 1;		/* Root privs are necessary */
+static int needroot = 1;	/* Root privs are necessary */
 int noaction = 0;		/* Don't do anything, just show it */
-static int norotate = 0;		/* Don't rotate */
-static int nosignal;			/* Do not send any signals */
-static int enforcepid = 0;		/* If PID file does not exist or empty, do nothing */
-static int force = 0;			/* Force the trim no matter what */
-static int rotatereq = 0;		/* -R = Always rotate the file(s) as given */
+static int norotate = 0;	/* Don't rotate */
+static int nosignal;		/* Do not send any signals */
+static int enforcepid = 0;	/* If PID file does not exist or empty, do nothing */
+static int force = 0;		/* Force the trim no matter what */
+static int rotatereq = 0;	/* -R = Always rotate the file(s) as given */
 				/*    on the command (this also requires   */
 				/*    that a list of files *are* given on  */
 				/*    the run command). */
 static char *requestor;		/* The name given on a -R request */
-static char *timefnamefmt = NULL;	/* Use time based filenames instead of .0 etc */
-static char *archdirname;		/* Directory path to old logfiles archive */
-static char *destdir = NULL;		/* Directory to treat at root for logs */
-static const char *conf;		/* Configuration file to use */
+static char *timefnamefmt = NULL; /* Use time based filenames instead of .0 */
+static char *archdirname;	/* Directory path to old logfiles archive */
+static char *destdir = NULL;	/* Directory to treat at root for logs */
+static const char *conf;	/* Configuration file to use */
 
 struct ptime_data *dbg_timenow;	/* A "timenow" value set via -D option */
-static struct ptime_data *timenow;	/* The time to use for checking at-fields */
+static struct ptime_data *timenow; /* The time to use for checking at-fields */
 
 #define	DAYTIME_LEN	16
-static char daytime[DAYTIME_LEN];	/* The current time in human readable form,
-					* used for rotation-tracking messages. */
+static char daytime[DAYTIME_LEN]; /* The current time in human readable form,
+				   * used for rotation-tracking messages. */
 
 /* Another buffer to hold the current time in RFC5424 format. Fractional
  * seconds are allowed by the RFC, but are not included in the
  * rotation-tracking messages written by newsyslog and so are not accounted for
  * in the length below.
  */
-#define        DAYTIME_RFC5424_LEN     sizeof("YYYY-MM-DDTHH:MM:SS+00:00")
+#define	DAYTIME_RFC5424_LEN	sizeof("YYYY-MM-DDTHH:MM:SS+00:00")
 static char daytime_rfc5424[DAYTIME_RFC5424_LEN];
 
-static char hostname[MAXHOSTNAMELEN];	/* hostname */
+static char hostname[MAXHOSTNAMELEN]; /* hostname */
 
 static const char *path_syslogpid = _PATH_SYSLOGPID;
 
@@ -812,8 +814,8 @@ usage(void)
 {
 
 	fprintf(stderr,
-	    "usage: newsyslog [-CFNnrsv] [-a directory] [-d directory] [-f config-file]\n"
-	    "                 [-S pidfile] [-t timefmt ] [ [-R tagname] filename ... ]\n");
+	    "usage: newsyslog [-CFNPnrsv] [-a directory] [-d directory] [-f config_file]\n"
+	    "                 [-S pidfile] [-t timefmt] [[-R tagname] file ...]\n");
 	exit(1);
 }
 
@@ -1665,7 +1667,7 @@ gen_classiclog_fname(char *fname, size_t fname_sz, const char *archive_dir,
 }
 
 /*
- * Delete a rotated logfiles, when using classic filenames.
+ * Delete a rotated logfile, when using classic filenames.
  */
 static void
 delete_classiclog(const char *archive_dir, const char *namepart, int numlog_c)
@@ -1823,13 +1825,13 @@ do_rotate(const struct conf_entry *ent)
 	} else {
 		gen_classiclog_fname(file1, sizeof(file1), dirpart, namepart,
 		    ent->numlogs - 1);
-		numlogs_c = ent->numlogs - 2;           /* copy for countdown */
+		numlogs_c = ent->numlogs - 2;		/* copy for countdown */
 	}
 
 	/* Move down log files */
 	for (; numlogs_c >= 0; numlogs_c--) {
-
 		strlcpy(file2, file1, sizeof(file2));
+
 		gen_classiclog_fname(file1, sizeof(file1), dirpart, namepart,
 		    numlogs_c);
 
@@ -2060,7 +2062,6 @@ do_zipwork(struct zipwork_entry *zwork)
 	else
 		pgm_name++;
 
-
 	args[0] = strdup(pgm_name);
 	if (args[0] == NULL)
 		err(1, "strdup()");
@@ -2289,7 +2290,6 @@ set_swpid(struct sigwork_entry *swork, const struct conf_entry *ent)
 			warnx("pid/cmd file is empty: %s", ent->pid_cmd_file);
 		} else
 			warn("can't read from pid file: %s", ent->pid_cmd_file);
-
 		fclose(f);
 		return;
 	}
diff --git a/usr.sbin/newsyslog/newsyslog.conf.5 b/usr.sbin/newsyslog/newsyslog.conf.5
index d988b9d807..c047945f25 100644
--- a/usr.sbin/newsyslog/newsyslog.conf.5
+++ b/usr.sbin/newsyslog/newsyslog.conf.5
@@ -5,7 +5,7 @@
 .\" This file contains changes from the Open Software Foundation.
 .\"
 .\"     from: @(#)newsyslog.8
-.\" $FreeBSD: src/usr.sbin/newsyslog/newsyslog.conf.5,v 1.12 2011/01/31 11:30:26 mm Exp $
+.\" $FreeBSD: head/usr.sbin/newsyslog/newsyslog.conf.5 328035 2018-01-16 00:47:53Z jhb $
 .\"
 .\" Copyright 1988, 1989 by the Massachusetts Institute of Technology
 .\"
@@ -130,7 +130,7 @@ Additionally, the format may also be constructed with a
 sign along with a rotation time specification of once
 a day, once a week, or once a month.
 .Pp
-If a time is specified, the log file will only be trimmed if
+Time based trimming happens only if
 .Xr newsyslog 8
 is run within one hour of the specified time.
 If an interval is specified, the log file will be trimmed if that many
@@ -155,8 +155,17 @@ The particular format of the time in restricted
 .Tn ISO 8601
 is:
 .Sm off
-.Oo Oo Oo Oo Oo Va cc Oc Va yy Oc Va mm Oc Va dd Oc
-.Oo Li T Oo Va hh Oo Va mm Oo Va ss Oc Oc Oc Oc Oc .
+.Oo Oo Oo Oo Oo
+.Va cc Oc
+.Va yy Oc
+.Va mm Oc
+.Va dd Oc
+.Oo
+.Li T Oo
+.Va hh Oo
+.Va mm Oo
+.Va ss
+.Oc Oc Oc Oc Oc .
 .Sm on
 Optional date fields default to the appropriate component of the
 current date; optional time fields default to midnight; hence if today
@@ -288,18 +297,6 @@ indicates that
 should attempt to save disk space by compressing the rotated
 log file using
 .Xr bzip2 1 .
-.It Cm X
-indicates that
-.Xr newsyslog 8
-should attempt to save disk space by compressing the rotated
-log file using
-.Xr xz 1 .
-.It Cm Y
-indicates that
-.Xr newsyslog 8
-should attempt to save disk space by compressing the rotated
-log file using
-.Xr zstd 1 .
 .It Cm N
 indicates that there is no process which needs to be signaled
 when this log file is rotated.
@@ -323,6 +320,18 @@ indicates that the file specified by
 will contain the ID for a process group instead of a process.
 This option also requires that the first line in that file
 be a negative value to distinguish it from a process ID.
+.It Cm X
+indicates that
+.Xr newsyslog 8
+should attempt to save disk space by compressing the rotated
+log file using
+.Xr xz 1 .
+.It Cm Y
+indicates that
+.Xr newsyslog 8
+should attempt to save disk space by compressing the rotated
+log file using
+.Xr zstd 1 .
 .It Cm Z
 indicates that
 .Xr newsyslog 8
@@ -380,6 +389,11 @@ Alternatively,
 can be the signal number, e.g., 30 for
 .Dv SIGUSR1 .
 .El
+.Sh EXAMPLES
+The following is an example of the
+.Dq Aq Li include
+entry:
+.Dl "<include> /etc/newsyslog-local.conf"
 .Sh SEE ALSO
 .Xr bzip2 1 ,
 .Xr gzip 1 ,
diff --git a/usr.sbin/newsyslog/pathnames.h b/usr.sbin/newsyslog/pathnames.h
index 26a4c29556..bc69089e14 100644
--- a/usr.sbin/newsyslog/pathnames.h
+++ b/usr.sbin/newsyslog/pathnames.h
@@ -18,7 +18,7 @@ M.I.T. and the M.I.T. S.I.P.B. make no representations about
 the suitability of this software for any purpose.  It is
 provided "as is" without express or implied warranty.
 
- $FreeBSD: src/usr.sbin/newsyslog/pathnames.h,v 1.4 2011/01/31 10:57:54 mm Exp $
+ $FreeBSD: head/usr.sbin/newsyslog/pathnames.h 316981 2017-04-15 20:37:34Z bapt $
 
 */
 
diff --git a/usr.sbin/newsyslog/ptimes.c b/usr.sbin/newsyslog/ptimes.c
index 203e54baa0..cf2601a6ce 100644
--- a/usr.sbin/newsyslog/ptimes.c
+++ b/usr.sbin/newsyslog/ptimes.c
@@ -39,8 +39,7 @@
  * it works fine for newsyslog but might not work for other uses.
  * ------+---------+---------+---------+---------+---------+---------+---------*
  *
- * $FreeBSD: src/usr.sbin/newsyslog/ptimes.c,v 1.5 2004/06/07 21:53:27 gad Exp $
- * $DragonFly: src/usr.sbin/newsyslog/ptimes.c,v 1.1 2007/05/12 08:52:00 swildner Exp $
+ * $FreeBSD: head/usr.sbin/newsyslog/ptimes.c 318960 2017-05-26 16:36:30Z dab $
  */
 
 #include <ctype.h>
@@ -342,7 +341,7 @@ ptime_init(const struct ptime_data *optsrc)
 int
 ptime_adjust4dst(struct ptime_data *ptime, const struct ptime_data *dstsrc)
 {
-	struct ptime_data adjtim;
+	struct ptime_data adjtime;
 
 	if (ptime == NULL)
 		return (-1);
@@ -351,22 +350,22 @@ ptime_adjust4dst(struct ptime_data *ptime, const struct ptime_data *dstsrc)
 	 * Changes are not made to the given time until after all
 	 * of the calculations have been successful.
 	 */
-	adjtim = *ptime;
+	adjtime = *ptime;
 
 	/* Check to see if this adjustment was already made */
-	if ((adjtim.did_adj4dst != TNYET_ADJ4DST) &&
-	    (adjtim.did_adj4dst == dstsrc->tm.tm_isdst))
+	if ((adjtime.did_adj4dst != TNYET_ADJ4DST) &&
+	    (adjtime.did_adj4dst == dstsrc->tm.tm_isdst))
 		return (0);		/* yes, so don't make it twice */
 
 	/* See if daylight-saving has changed between the two times. */
-	if (dstsrc->tm.tm_isdst != adjtim.tm.tm_isdst) {
-		if (adjtim.tm.tm_isdst == 1)
-			adjtim.tsecs -= SECS_PER_HOUR;
-		else if (adjtim.tm.tm_isdst == 0)
-			adjtim.tsecs += SECS_PER_HOUR;
-		adjtim.tm = *(localtime(&adjtim.tsecs));
+	if (dstsrc->tm.tm_isdst != adjtime.tm.tm_isdst) {
+		if (adjtime.tm.tm_isdst == 1)
+			adjtime.tsecs -= SECS_PER_HOUR;
+		else if (adjtime.tm.tm_isdst == 0)
+			adjtime.tsecs += SECS_PER_HOUR;
+		adjtime.tm = *(localtime(&adjtime.tsecs));
 		/* Remember that this adjustment has been made */
-		adjtim.did_adj4dst = dstsrc->tm.tm_isdst;
+		adjtime.did_adj4dst = dstsrc->tm.tm_isdst;
 		/*
 		 * XXX - Should probably check to see if changing the
 		 *	hour also changed the value of is_dst.  What
@@ -374,7 +373,7 @@ ptime_adjust4dst(struct ptime_data *ptime, const struct ptime_data *dstsrc)
 		 */
 	}
 
-	*ptime = adjtim;
+	*ptime = adjtime;
 	return (0);
 }
 
-- 
2.15.1

