Subject: bin/10786: shouldn't last(1) behave like w(1) and resolve IPs?
To: None <gnats-bugs@gnats.netbsd.org>
From: None <kalt@taranis.org>
List: netbsd-bugs
Date: 08/08/2000 14:29:16
>Number:         10786
>Category:       bin
>Synopsis:       shouldn't last(1) behave like w(1) and resolve IPs?
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Tue Aug 08 14:30:00 PDT 2000
>Closed-Date:
>Last-Modified:
>Originator:     Christophe Kalt
>Release:        1.5ALPHA snapshot from 6/20 or so
>Organization:
>Environment:
	
System: NetBSD bzz.taranis.org 1.5_ALPHA NetBSD 1.5_ALPHA (bzz) #0: Fri Aug 4 17:10:25 EDT 2000 kalt@bzz.taranis.org:syssrc-1.5/sys/arch/sparc/compile/bzz sparc


>Description:
i'm not sure why, but on this system, all last ever gives me as origin
is IP addresses.  Has something changed with regard to this?

anyhow, i ended up noticing that w(1) will look up IP addresses for you,
and as i have been getting furstrated seeing IPs in last(1)'s output that
don't look familiar, i have stolen the code from w(1) and below is a
diff for last(1).

i'm not sure that doing lookups by default is the right default,
but that's easy to change.
>How-To-Repeat:
	
>Fix:

Index: last.1
===================================================================
RCS file: /cvsroot/basesrc/usr.bin/last/last.1,v
retrieving revision 1.7
diff -u -r1.7 last.1
--- last.1	2000/06/25 13:44:42	1.7
+++ last.1	2000/08/08 21:04:56
@@ -41,6 +41,7 @@
 .Nd indicate last logins of users and ttys
 .Sh SYNOPSIS
 .Nm
+.Op Fl n
 .Op Fl Ns Ar n
 .Op Fl f Ar file
 .Op Fl h Ar host
@@ -77,14 +78,18 @@
 .Ar tty .
 Tty names may be given fully or abbreviated, for example,
 .Dq Li "last -t 03"
-is
-equivalent to
+is equivalent to
 .Dq Li "last -t tty03" .
 .It Fl h Ar host
 .Ar Host
 names may be names or internet numbers.
 .It Fl T
 Display better time information, including the year and seconds.
+.It Fl n
+Show network addresses as numbers (normally
+.Nm
+interprets addresses
+and attempts to display them symbolically).
 .El
 .Pp
 If
Index: last.c
===================================================================
RCS file: /cvsroot/basesrc/usr.bin/last/last.c,v
retrieving revision 1.15
diff -u -r1.15 last.c
--- last.c	2000/06/30 06:19:58	1.15
+++ last.c	2000/08/08 21:04:56
@@ -49,9 +49,14 @@
 
 #include <sys/param.h>
 #include <sys/stat.h>
+#include <sys/socket.h>
 
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
 #include <err.h>
 #include <fcntl.h>
+#include <netdb.h>
 #include <paths.h>
 #include <signal.h>
 #include <stdio.h>
@@ -99,6 +104,8 @@
 static long	maxrec;			/* records to display */
 static char	*file = _PATH_WTMP;	/* wtmp file */
 static int	fulltime = 0;		/* Display seconds? */
+int		nflag = 0;	/* true if -n flag: don't convert addrs */
+char		domain[MAXHOSTNAMELEN + 1];
 
 int	 main __P((int, char *[]));
 void	 addarg __P((int, char *));
@@ -119,7 +126,7 @@
 	char *p;
 
 	maxrec = -1;
-	while ((ch = getopt(argc, argv, "0123456789f:h:t:T")) != -1)
+	while ((ch = getopt(argc, argv, "0123456789f:h:t:Tn")) != -1)
 		switch (ch) {
 		case '0': case '1': case '2': case '3': case '4':
 		case '5': case '6': case '7': case '8': case '9':
@@ -150,10 +157,13 @@
 		case 'T':
 			fulltime = 1;
 			break;
+		case 'n':
+			nflag = 1;
+			break;
 		case '?':
 		default:
 			(void)fprintf(stderr,
-	"usage: last [-#] [-f file] [-t tty] [-h hostname] [-T] [user ...]\n");
+	"usage: last [-n] [-#] [-f file] [-t tty] [-h hostname] [-T] [user ...]\n");
 			exit(1);
 		}
 
@@ -168,6 +178,18 @@
 			addarg(USER_TYPE, *argv);
 		}
 	}
+
+	if (!nflag) {
+		int	rv;
+
+		rv = gethostname(domain, sizeof(domain));
+		domain[sizeof(domain) - 1] = '\0';
+		if (rv < 0 || (p = strchr(domain, '.')) == 0)
+			domain[0] = '\0';
+		else
+			memmove(domain, p, strlen(p) + 1);
+	}
+
 	wtmp();
 	exit(0);
 }
@@ -258,11 +280,29 @@
 					break;
 			}
 			if (bp->ut_name[0] && want(bp, YES)) {
+				char *p;
+				struct in_addr l;
+				struct hostent *hp;
+
+				p = bp->ut_host;
+				if (!nflag && inet_aton(p, &l) &&
+				    (hp = gethostbyaddr((char *)&l, sizeof(l), AF_INET))) {
+					if (domain[0] != '\0') {
+						p = hp->h_name;
+						p +=strlen(hp->h_name);
+						p -= strlen(domain);
+						if (p > hp->h_name &&
+						    strcmp(p, domain) == 0)
+							*p = '\0';
+					}
+					p = hp->h_name;
+				}
+
 				ct = fmttime(bp->ut_time, fulltime);
 				printf("%-*.*s  %-*.*s %-*.*s %s ",
 				(int)UT_NAMESIZE, (int)UT_NAMESIZE, bp->ut_name,
 				(int)UT_LINESIZE, (int)UT_LINESIZE, bp->ut_line,
-				(int)UT_HOSTSIZE, (int)UT_HOSTSIZE, bp->ut_host,
+				(int)UT_HOSTSIZE, (int)UT_HOSTSIZE, p,
 				ct);
 				if (!T->logout)
 					puts("  still logged in");
>Release-Note:
>Audit-Trail:
>Unformatted: