--- newsyslog.c.orig	Fri May 19 18:21:30 2000
+++ newsyslog.c	Fri Jan 31 18:15:13 2003
@@ -81,6 +81,11 @@
 	struct conf_entry *next;/* Linked list pointer */
 };
 
+struct compress_entry {
+	char		target[MAXPATHLEN + 1];		/* file name */
+	struct compress_entry	*next;			/* next target */
+};
+
 int archtodir = 0;		/* Archive old logfiles to other directory */
 int verbose = 0;		/* Print out what's going on */
 int needroot = 1;		/* Root privs are necessary */
@@ -94,6 +99,8 @@
 #define MAX_PID		99999	/* was lower, see /usr/include/sys/proc.h */
 char hostname[MAXHOSTNAMELEN + 1];	/* hostname */
 char *daytime;			/* timenow in human readable form */
+char curdate[9];		/* current date string (%Y%m%d) */
+struct compress_entry	*need_compress = NULL;	/* defferd compress target */
 
 static struct conf_entry *parse_file(char **files);
 static char *sob(char *p);
@@ -106,7 +113,8 @@
 static int log_trim(char *log);
 static void compress_log(char *log);
 static int sizefile(char *file);
-static int age_old_log(char *file);
+static int age_old_log(char *file, int numlogs);
+static int create_stamp_filename(char *dst, char *log);
 static pid_t get_pid(char *pid_file);
 static time_t parse8601(char *s);
 static void movefile(char *from, char *to, int perm, int owner_uid, int group_gid);
@@ -145,7 +153,7 @@
 			printf("%s <%d>: ", ent->log, ent->numlogs);
 	}
 	size = sizefile(ent->log);
-	modtime = age_old_log(ent->log);
+	modtime = age_old_log(ent->log, ent->numlogs);
 	if (size < 0) {
 		if (verbose)
 			printf("does not exist.\n");
@@ -202,10 +210,13 @@
 {
 	int c;
 	char *p;
+	struct tm *lt;
 
 	timenow = time((time_t *) 0);
 	daytime = ctime(&timenow) + 4;
 	daytime[15] = '\0';
+	lt  = localtime(&timenow);
+	strftime(curdate, 9, "%Y%m%d", lt);
 
 	/* Let's get our hostname */
 	(void) gethostname(hostname, sizeof(hostname));
@@ -430,6 +441,8 @@
 		if (q && *q) {
 			if (*q == '/')
 				working->pid_file = strdup(q);
+			else if (*q == '-')
+				working->pid_file = strdup("-");
 			else if (isdigit(*q))
 				goto got_sig;
 			else
@@ -478,8 +491,11 @@
 	char file1[MAXPATHLEN + 1], file2[MAXPATHLEN + 1];
 	char zfile1[MAXPATHLEN + 1], zfile2[MAXPATHLEN + 1];
 	int notified, need_notification, fd, _numdays;
+	int deffered_compress;
 	struct stat st;
 	pid_t pid;
+	int count;
+	char suffix[12];
 
 #ifdef _IBMR2
 	/*
@@ -538,7 +554,7 @@
 
 	/* Move down log files */
 	_numdays = numdays;	/* preserve */
-	while (numdays--) {
+	while (numdays-- > 0) {
 
 		(void) strcpy(file2, file1);
 
@@ -575,6 +591,26 @@
 		else
 			(void) unlink(log);
 	} else {
+		/* if negative value specified as numdays.
+		    then use date as suffix */
+		if (_numdays < 0) {
+			sprintf(file1, "%s.%s", log, curdate);
+			(void) strcpy(zfile1, file1);
+			(void) strcat(zfile1, COMPRESS_POSTFIX);
+			strcpy(suffix, curdate);
+			count = 0;
+			while (!lstat(file1, &st) || !stat(zfile1, &st)) {
+				if (count == 100) {
+					errx(1, "cannot create route file for %s", log);
+				}
+				sprintf(file1, "%s.%s-%02d", log, curdate, count);
+				(void) strcpy(zfile1, file1);
+				(void) strcat(zfile1, COMPRESS_POSTFIX);
+				sprintf(suffix, "%s-%02d", curdate, count++);
+			}
+		} else {
+			suffix[0] = '\0';
+		}
 		if (noaction)
 			printf("mv %s to %s\n", log, file1);
 		else {
@@ -583,6 +619,22 @@
 			else
 				(void) rename(log, file1);
 		}
+		if (_numdays < 0) {
+			struct stat sb;
+			int fd;
+			char ts[MAXPATHLEN + sizeof(".") + 1];
+			(void) create_stamp_filename(ts, log);
+
+			if (noaction) {
+				printf("touch %s\n", ts);
+			} else {
+				if ((fd = open(ts, O_WRONLY | O_CREAT, 0644)) < 0) {
+					errx(1, "cannot open time stamp file:\n%s", log);
+				}
+				write(fd, "\n", 1);
+				close(fd);
+			}
+		}
 	}
 
 	if (noaction)
@@ -605,9 +657,14 @@
 
 	pid = 0;
 	need_notification = notified = 0;
+	deffered_compress = 0;
 	if (pid_file != NULL) {
 		need_notification = 1;
-		pid = get_pid(pid_file);
+		if (*pid_file == '-') {
+			deffered_compress = 1;
+		} else {
+			pid = get_pid(pid_file);
+		}
 	}
 	if (pid) {
 		if (noaction) {
@@ -622,10 +679,23 @@
 		}
 	}
 	if ((flags & CE_COMPACT)) {
-		if (need_notification && !notified)
+		char	target[MAXPATHLEN + 1];
+		if (suffix[0] == '\0') {
+			(void) sprintf(target, "%s.0", log);
+		} else {
+			(void) sprintf(target, "%s.%s", log, suffix);
+		}
+
+		if (deffered_compress) {
+			struct compress_entry *t;
+			t = (struct compress_entry *) malloc(sizeof(struct compress_entry));
+			strncpy(t->target, target, MAXPATHLEN);
+			t->next = need_compress;
+			need_compress = t;
+		} else if (need_notification && !notified)
 			warnx("log not compressed because daemon not notified");
 		else if (noaction)
-			printf("Compress %s.0\n", log);
+			printf("Compress %s\n", target);
 		else {
 			if (notified) {
 				if (verbose)
@@ -636,10 +706,25 @@
 				(void) sprintf(file1, "%s/%s", dirpart, namepart);
 				compress_log(file1);
 			} else {
-				compress_log(log);
+				compress_log(target);
 			}
 		}
 	}
+
+	if (pid && need_compress) {
+		while (need_compress != NULL) {
+			struct compress_entry *t;
+			t = need_compress;
+			if (noaction) {
+				printf("Compress %s\n", t->target);
+			} else {
+				compress_log(t->target);
+			}
+
+			need_compress = t->next;
+			free((void*)t);
+		}
+	}
 }
 
 /* Log the fact that the logs were turned over */
@@ -662,14 +747,12 @@
 compress_log(char *log)
 {
 	pid_t pid;
-	char tmp[MAXPATHLEN + 1];
 
-	(void) sprintf(tmp, "%s.0", log);
 	pid = fork();
 	if (pid < 0)
 		err(1, "fork");
 	else if (!pid) {
-		(void) execl(_PATH_GZIP, _PATH_GZIP, "-f", tmp, 0);
+		(void) execl(_PATH_GZIP, _PATH_GZIP, "-f", log, 0);
 		err(1, _PATH_GZIP);
 	}
 }
@@ -687,7 +770,7 @@
 
 /* Return the age of old log file (file.0) */
 static int
-age_old_log(char *file)
+age_old_log(char *file, int numlogs)
 {
 	struct stat sb;
 	char tmp[MAXPATHLEN + sizeof(".0") + sizeof(COMPRESS_POSTFIX) + 1];
@@ -719,12 +802,42 @@
 		(void) strcpy(tmp, file);
 	}
 
-	if (stat(strcat(tmp, ".0"), &sb) < 0)
-		if (stat(strcat(tmp, COMPRESS_POSTFIX), &sb) < 0)
+	if (numlogs >= 0) {
+		(void) strcpy(tmp,file);
+		if (stat(strcat(tmp, ".0"), &sb) < 0)
+			if (stat(strcat(tmp, COMPRESS_POSTFIX), &sb) < 0)
+				return (-1);
+	} else {
+		(void) create_stamp_filename(tmp, file);
+		if (stat(tmp, &sb) < 0)
 			return (-1);
+	}
+
 	return ((int) (timenow - sb.st_mtime + 1800) / 3600);
 }
 
+static int
+create_stamp_filename(char *dst, char *log) {
+
+	char	*base;
+	char	saved = '.';
+	strcpy(dst, log);
+	if ((base = strrchr(dst, '/')) == NULL) {
+		errx(1, "at least include one slash in a log file name");
+	}
+
+	base++;
+	while (*base != '\0') {
+		char previous = saved;
+		saved = *base;
+		*base++ = previous;
+	}
+	*base++ = saved;
+	*base   = '\0';
+
+	return 0;
+}
+
 static pid_t
 get_pid(char *pid_file)
 {
@@ -782,6 +895,7 @@
 	char *t;
 	struct tm tm, *tmp;
 	u_long ul;
+	time_t	tmp_ti;
 
 	tmp = localtime(&timenow);
 	tm = *tmp;
@@ -810,6 +924,15 @@
 		ul = ul % 100;
 	case 2:
 		tm.tm_mday = ul;
+		break;
+	case 1:
+		ul %= 7;
+		if (tm.tm_wday == ul) break;
+		tmp_ti = timenow + (((ul + 7) - tm.tm_wday) % 7) * 60 * 60 * 24;
+		tmp = localtime(&tmp_ti);
+		tm = *tmp;
+		tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
+		break;
 	case 0:
 		break;
 	default:
