Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 10 Jan 2010 17:45:47 +0000 (UTC)
From:      Ed Schouten <ed@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-user@freebsd.org
Subject:   svn commit: r202020 - user/ed/utmpx/usr.bin/last
Message-ID:  <201001101745.o0AHjlqC095649@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: ed
Date: Sun Jan 10 17:45:47 2010
New Revision: 202020
URL: http://svn.freebsd.org/changeset/base/202020

Log:
  Somewhat make last(1) work.
  
  Right now it's still quite broken. It doesn't properly detect logouts,
  because it doesn't honour ut_id and ut_type yet.

Modified:
  user/ed/utmpx/usr.bin/last/last.c

Modified: user/ed/utmpx/usr.bin/last/last.c
==============================================================================
--- user/ed/utmpx/usr.bin/last/last.c	Sun Jan 10 17:44:22 2010	(r202019)
+++ user/ed/utmpx/usr.bin/last/last.c	Sun Jan 10 17:45:47 2010	(r202020)
@@ -66,8 +66,6 @@ __FBSDID("$FreeBSD$");
 #define	YES	1				/* true/yes */
 #define	ATOI2(ar)	((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
 
-static struct utmpx	buf[1024];		/* utmp read buffer */
-
 typedef struct arg {
 	char	*name;				/* argument */
 #define	HOST_TYPE	-2
@@ -104,7 +102,6 @@ void	 addarg(int, char *);
 time_t	 dateconv(char *);
 void	 doentry(struct utmpx *);
 void	 hostconv(char *);
-void	 onintr(int);
 void	 printentry(struct utmpx *, struct ttytab *);
 char	*ttyconv(char *);
 int	 want(struct utmpx *);
@@ -199,6 +196,8 @@ main(int argc, char *argv[])
 	exit(0);
 }
 
+#define	MAXUTXENTRIES	1024
+
 /*
  * wtmp --
  *	read through the wtmp file
@@ -206,36 +205,37 @@ main(int argc, char *argv[])
 void
 wtmp(void)
 {
-#if 0
-	struct utmpx	*bp;			/* current structure */
-	int	bytes, wfd;
+	struct utmpx buf[MAXUTXENTRIES];
+	struct utmpx *ut;
+	static unsigned int first = 0, amount = 0;
+	time_t t;
 	char ct[80];
 	struct tm *tm;
-	time_t	t;
 
 	LIST_INIT(&ttylist);
+	(void)time(&t);
 
+	/* Load the last entries from the file. */
 	if (setutxdb(UTXDB_LOG, file) != 0)
 		err(1, "%s", file);
-	bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf);
+	while ((ut = getutxent()) != NULL) {
+		memcpy(&buf[(first + amount) % MAXUTXENTRIES], ut, sizeof *ut);
+		if (amount == MAXUTXENTRIES)
+			first++;
+		else
+			amount++;
+		if (t > ut->ut_tv.tv_sec)
+			t = ut->ut_tv.tv_sec;
+	}
+	endutxent();
+
+	/* Display them in reverse order. */
+	while (amount > 0)
+		doentry(&buf[(first + amount--) % MAXUTXENTRIES]);
 
-	(void)time(&t);
-	buf[0].ut_tv.tv_sec = t;
-	(void)signal(SIGINT, onintr);
-	(void)signal(SIGQUIT, onintr);
-
-	while (--bl >= 0) {
-		if (lseek(wfd, (off_t)(bl * sizeof(buf)), L_SET) == -1 ||
-		    (bytes = read(wfd, buf, sizeof(buf))) == -1)
-			err(1, "%s", file);
-		for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp)
-			doentry(bp);
-	}
-	t = buf[0].ut_tv.tv_sec;
 	tm = localtime(&t);
 	(void) strftime(ct, sizeof(ct), "\nwtmp begins %+\n", tm);
 	printf("%s", ct);
-#endif
 }
 
 /*
@@ -550,25 +550,3 @@ terr:           errx(1,
         "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
         return timet;
 }
-
-
-/*
- * onintr --
- *	on interrupt, we inform the user how far we've gotten
- */
-void
-onintr(int signo)
-{
-	char ct[80];
-	struct tm *tm;
-	time_t t = buf[0].ut_tv.tv_sec;
-
-	tm = localtime(&t);
-	(void) strftime(ct, sizeof(ct),
-			d_first ? "%a %e %b %R" : "%a %b %e %R",
-			tm);
-	printf("\ninterrupted %s\n", ct);
-	if (signo == SIGINT)
-		exit(1);
-	(void)fflush(stdout);			/* fix required for rsh */
-}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201001101745.o0AHjlqC095649>