>From 8eb8cc0df3e77ac0d39a0090e925ac4491e6abf5 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Tue, 28 Jul 2015 12:04:36 -0700
Subject: [PROPOSED PATCH 1/2] Remove obsolete utmp.h code from 'date'

The old utmp.h-related code predates POSIX, which standardized on
utmpx.h in the 1990s.  utmpx.h is more portable nowadays, so assume it.
Problem reported by Kees Dekker in:
http://mm.icann.org/pipermail/tz/2015-July/022528.html
* Makefile, private.h (HAVE_UTMP_H): Default to 1.
* NEWS: Document this.
* date.c: Do not include utmp.h.
(reset): Do not update utmp; utmpx suffices nowadays.
---
 Makefile  |  2 +-
 NEWS      |  6 ++++++
 date.c    | 31 +++++--------------------------
 private.h |  2 +-
 4 files changed, 13 insertions(+), 28 deletions(-)

diff --git a/Makefile b/Makefile
index 371a823..8702119 100644
--- a/Makefile
+++ b/Makefile
@@ -126,7 +126,7 @@ LDLIBS=
 #  -DHAVE_SYS_WAIT_H=0 if your compiler lacks a "sys/wait.h"
 #  -DHAVE_TZSET=0 if your system lacks a tzset function
 #  -DHAVE_UNISTD_H=0 if your compiler lacks a "unistd.h" (Microsoft C++ 7?)
-#  -DHAVE_UTMPX_H=1 if your compiler has a "utmpx.h"
+#  -DHAVE_UTMPX_H=0 if your compiler lacks a "utmpx.h"
 #  -DNO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU=1
 #	if you do not want run time warnings about formats that may cause
 #	year 2000 grief
diff --git a/NEWS b/NEWS
index 3ab0602..45065e4 100644
--- a/NEWS
+++ b/NEWS
@@ -53,6 +53,12 @@ Unreleased, experimental changes
     is no longer documented.  (Thanks to Kees Dekker for pointing out
     the problem.)
 
+    For recording clock changes date.c by default now includes and
+    uses <utmpx.h>, which has long been part of POSIX, and no longer
+    uses the long-obsolescent <utmp.h> API.  Compile with
+    -DHAVE_UTMPX_H=0 to suppress the use of <utmpx.h>.
+    (Thanks to Kees Dekker for pointing out the problem.)
+
   Changes affecting documentation
 
     tz-link.htm mentions Time Zone Database Parser (thanks to Howard Hinnant).
diff --git a/date.c b/date.c
index cb01bc3..2a55505 100644
--- a/date.c
+++ b/date.c
@@ -20,7 +20,6 @@
 #include "sys/time.h"	/* for struct timeval, struct timezone */
 #endif /* HAVE_ADJTIME || HAVE_SETTIMEOFDAY */
 #include "locale.h"
-#include "utmp.h"
 #if HAVE_UTMPX_H
 #include "utmpx.h"
 #endif
@@ -321,7 +320,7 @@ dogmt(void)
 
 /*
 ** We assume we're on a POSIX-based system,
-** should use stime, should write utmp entries,
+** should use stime, and should write utmp entries if HAVE_UTMPX_H,
 ** and don't have network notification to worry about.
 */
 
@@ -331,13 +330,11 @@ dogmt(void)
 static void
 reset(time_t newt)
 {
-	register int		fid;
-	time_t			oldt;
-	static struct {
-		struct utmp	before;
-		struct utmp	after;
-	} s;
 #if HAVE_UTMPX_H
+# if defined WTMPX_FILE && !SUPPRESS_WTMPX_FILE_UPDATE
+	register int		fid;
+# endif
+	time_t oldt = time(NULL);
 	static struct {
 		struct utmpx	before;
 		struct utmpx	after;
@@ -347,26 +344,8 @@ reset(time_t newt)
 	/*
 	** Wouldn't it be great if stime returned the old time?
 	*/
-	oldt = time(NULL);
 	if (stime(&newt) != 0)
 		oops("stime");
-	s.before.ut_type = OLD_TIME;
-	s.before.ut_time = oldt;
-	strcpy(s.before.ut_line, OTIME_MSG);
-	s.after.ut_type = NEW_TIME;
-	s.after.ut_time = newt;
-	strcpy(s.after.ut_line, NTIME_MSG);
-	fid = open(WTMP_FILE, O_WRONLY | O_APPEND);
-	if (fid < 0)
-		oops(_("log file open"));
-	if (write(fid, (char *) &s, sizeof s) != sizeof s)
-		oops(_("log file write"));
-	if (close(fid) != 0)
-		oops(_("log file close"));
-#if !HAVE_UTMPX_H
-	pututline(&s.before);
-	pututline(&s.after);
-#endif /* !HAVE_UTMPX_H */
 #if HAVE_UTMPX_H
 	sx.before.ut_type = OLD_TIME;
 	sx.before.ut_tv.tv_sec = oldt;
diff --git a/private.h b/private.h
index c6a2fcb..71a02d8 100644
--- a/private.h
+++ b/private.h
@@ -63,7 +63,7 @@
 #endif /* !defined HAVE_UNISTD_H */
 
 #ifndef HAVE_UTMPX_H
-#define HAVE_UTMPX_H		0
+#define HAVE_UTMPX_H		1
 #endif /* !defined HAVE_UTMPX_H */
 
 #ifndef NETBSD_INSPIRED
-- 
2.1.4

