Author: karels
Date: Thu Aug 22 03:28:31 2019
New Revision: 351379
URL: https://svnweb.freebsd.org/changeset/base/351379
Log:
Change w(1) to compute FROM (host) field size dynamically
It's nice to be able to display a full IPv6 host address if
needed, but it's also nice to display more than 3 characters of a command
line. Compute the needed size for the FROM column in an earlier pass,
and determine the maximum, then print what fits for the command.
Reviewed by: marcel@ (markm@ previous revision)
MFC after:1 week
Differential Revision:https://reviews.freebsd.org/D21211
Modified:
head/usr.bin/w/w.c
Modified: head/usr.bin/w/w.c
==
--- head/usr.bin/w/w.c Thu Aug 22 03:00:55 2019(r351378)
+++ head/usr.bin/w/w.c Thu Aug 22 03:28:31 2019(r351379)
@@ -96,7 +96,8 @@ static struct winsize ws;
static kvm_t *kd;
static time_t now;/* the current time of day */
static int ttywidth; /* width of tty */
-static int argwidth; /* width of tty */
+static int fromwidth = 0; /* max width of "from" field */
+static int argwidth; /* width of arguments */
static int header = 1; /* true if -h flag: don't print heading */
static int nflag; /* true if -n flag: don't convert addrs */
static int dflag; /* true if -d flag: output debug info */
@@ -116,13 +117,14 @@ static struct entry {
struct kinfo_proc *kp; /* `most interesting' proc */
char*args; /* arg list of interesting process */
struct kinfo_proc *dkp;/* debug option proc list */
+ char*from; /* "from": name or addr */
} *ep, *ehead = NULL, **nextp = &ehead;
#definedebugproc(p) *(&((struct kinfo_proc *)p)->ki_udata)
#defineW_DISPUSERSIZE 10
#defineW_DISPLINESIZE 8
-#defineW_DISPHOSTSIZE 40
+#defineW_MAXHOSTSIZE 40
static void pr_header(time_t *, int);
static struct stat *ttystat(char *);
@@ -209,6 +211,13 @@ main(int argc, char *argv[])
setutxent();
for (nusers = 0; (utmp = getutxent()) != NULL;) {
+ struct addrinfo hints, *res;
+ struct sockaddr_storage ss;
+ struct sockaddr *sa = (struct sockaddr *)&ss;
+ struct sockaddr_in *lsin = (struct sockaddr_in *)&ss;
+ struct sockaddr_in6 *lsin6 = (struct sockaddr_in6 *)&ss;
+ int isaddr;
+
if (utmp->ut_type != USER_PROCESS)
continue;
if (!(stp = ttystat(utmp->ut_line)))
@@ -250,9 +259,76 @@ main(int argc, char *argv[])
}
if ((ep->idle = now - touched) < 0)
ep->idle = 0;
+
+ save_p = p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-";
+ if ((x_suffix = strrchr(p, ':')) != NULL) {
+ if ((dot = strchr(x_suffix, '.')) != NULL &&
+ strchr(dot+1, '.') == NULL)
+ *x_suffix++ = '\0';
+ else
+ x_suffix = NULL;
+ }
+
+ isaddr = 0;
+ memset(&ss, '\0', sizeof(ss));
+ if (inet_pton(AF_INET6, p, &lsin6->sin6_addr) == 1) {
+ lsin6->sin6_len = sizeof(*lsin6);
+ lsin6->sin6_family = AF_INET6;
+ isaddr = 1;
+ } else if (inet_pton(AF_INET, p, &lsin->sin_addr) == 1) {
+ lsin->sin_len = sizeof(*lsin);
+ lsin->sin_family = AF_INET;
+ isaddr = 1;
+ }
+ if (nflag == 0) {
+ /* Attempt to change an IP address into a name */
+ if (isaddr && realhostname_sa(fn, sizeof(fn), sa,
+ sa->sa_len) == HOSTNAME_FOUND)
+ p = fn;
+ } else if (!isaddr && nflag > 1) {
+ /*
+* If a host has only one A/ RR, change a
+* name into an IP address
+*/
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_flags = AI_PASSIVE;
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+ if (getaddrinfo(p, NULL, &hints, &res) == 0) {
+ if (res->ai_next == NULL &&
+ getnameinfo(res->ai_addr, res->ai_addrlen,
+ fn, sizeof(fn), NULL, 0,
+ NI_NUMERICHOST) == 0)
+ p = fn;
+ freeaddrinfo(res);
+