Module Name:    src
Committed By:   dholland
Date:           Thu Mar 15 02:55:02 UTC 2012

Modified Files:
        src/usr.bin/last: want.c

Log Message:
Keep track of the timestamp of the last (thus oldest) record seen and use
it to print "wtmp[x] begins" at the end, instead of knowing where to look
in the final utmp buffer to get a final timestamp out. This is both tidier
and fixes a problem with wtmpx files, which is that if the header record
is missing (which it seems to be on my machines) it would fetch the wrong
time out, and if you happened to have a one-record wtmp file it would use
the current time instead. This change restores the traditional behavior
of printing the time of the oldest record in the file, and if no records
are present to use the current time.

It might be a bug that wtmpx files don't seem to have the header
record they supposedly ought to.


To generate a diff of this commit:
cvs rdiff -u -r1.14 -r1.15 src/usr.bin/last/want.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/last/want.c
diff -u src/usr.bin/last/want.c:1.14 src/usr.bin/last/want.c:1.15
--- src/usr.bin/last/want.c:1.14	Fri Sep 16 15:39:27 2011
+++ src/usr.bin/last/want.c	Thu Mar 15 02:55:02 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: want.c,v 1.14 2011/09/16 15:39:27 joerg Exp $	*/
+/*	$NetBSD: want.c,v 1.15 2012/03/15 02:55:02 dholland Exp $	*/
 
 /*
  * Copyright (c) 1987, 1993, 1994
@@ -29,6 +29,7 @@
  * SUCH DAMAGE.
  */
 static struct utmp *buf;
+static time_t seentime;
 
 static void onintr(int);
 static int want(struct utmp *, int);
@@ -130,7 +131,7 @@ wtmp(const char *file, int namesz, int l
 	if (!S_ISREG(stb.st_mode))
 		errx(EXIT_FAILURE, "%s: Not a regular file", file);
 
-	buf[FIRSTVALID].ut_timefld = time(NULL);
+	seentime = time(NULL);
 	(void)signal(SIGINT, onintr);
 	(void)signal(SIGQUIT, onintr);
 
@@ -157,6 +158,9 @@ wtmp(const char *file, int namesz, int l
 			NULTERM(name);
 			NULTERM(line);
 			NULTERM(host);
+
+			seentime = bp->ut_timefld;
+
 			/*
 			 * if the terminal line is '~', the machine stopped.
 			 * see utmp(5) for more info.
@@ -250,7 +254,7 @@ wtmp(const char *file, int namesz, int l
 		}
 	}
 	fulltime = 1;	/* show full time */
-	crmsg = fmttime(buf[FIRSTVALID].ut_timefld, FULLTIME);
+	crmsg = fmttime(seentime, FULLTIME);
 	if ((ct = strrchr(file, '/')) != NULL)
 		ct++;
 	printf("\n%s begins %s\n", ct ? ct : file, crmsg);
@@ -305,8 +309,7 @@ static void
 onintr(int signo)
 {
 	/* FIXME: None of this is allowed in a signal handler */
-	printf("\ninterrupted %s\n", fmttime(buf[FIRSTVALID].ut_timefld,
-	    FULLTIME));
+	printf("\ninterrupted %s\n", fmttime(seentime, FULLTIME));
 	if (signo == SIGINT) {
 		(void)raise_default_signal(signo);
 		exit(EXIT_FAILURE);

Reply via email to