Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/w Expand ep->host to contain the final string that w...



details:   https://anonhg.NetBSD.org/src/rev/2c49e2301552
branches:  trunk
changeset: 789832:2c49e2301552
user:      christos <christos%NetBSD.org@localhost>
date:      Mon Sep 09 19:20:38 2013 +0000

description:
Expand ep->host to contain the final string that we are going to print
before computing the width of the host column.

diffstat:

 usr.bin/w/w.c |  89 ++++++++++++++++++++++++++++++++++------------------------
 1 files changed, 52 insertions(+), 37 deletions(-)

diffs (167 lines):

diff -r 07b63346293a -r 2c49e2301552 usr.bin/w/w.c
--- a/usr.bin/w/w.c     Mon Sep 09 19:18:08 2013 +0000
+++ b/usr.bin/w/w.c     Mon Sep 09 19:20:38 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: w.c,v 1.76 2011/10/21 02:26:09 christos Exp $  */
+/*     $NetBSD: w.c,v 1.77 2013/09/09 19:20:38 christos Exp $  */
 
 /*-
  * Copyright (c) 1980, 1991, 1993, 1994
@@ -39,7 +39,7 @@
 #if 0
 static char sccsid[] = "@(#)w.c        8.6 (Berkeley) 6/30/94";
 #else
-__RCSID("$NetBSD: w.c,v 1.76 2011/10/21 02:26:09 christos Exp $");
+__RCSID("$NetBSD: w.c,v 1.77 2013/09/09 19:20:38 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -126,17 +126,16 @@
 static int     ttystat(const char *, struct stat *);
 static void    process(struct entry *);
 #endif
+static void    fixhost(struct entry *ep);
 __dead static void     usage(int);
 
 int
 main(int argc, char **argv)
 {
        struct kinfo_proc2 *kp;
-       struct hostent *hp;
-       struct in_addr l;
        struct entry *ep;
        int ch, i, nentries, nusers, wcmd, curtain, use_sysctl;
-       char *memf, *nlistf, *p, *x, *usrnp;
+       char *memf, *nlistf, *usrnp;
        const char *options;
        time_t then;
        size_t len;
@@ -147,7 +146,7 @@
        struct utmpx *utx;
 #endif
        const char *progname;
-       char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX];
+       char errbuf[_POSIX2_LINE_MAX];
 
        setprogname(argv[0]);
 
@@ -239,6 +238,7 @@
                            sizeof(utx->ut_host));
                        ep->host[sizeof(utx->ut_host)] = '\0';
                }
+               fixhost(ep);
                ep->type[0] = 'x';
                ep->tv = utx->ut_tv;
                ep->pid = utx->ut_pid;
@@ -276,6 +276,7 @@
                ep->name[sizeof(ut->ut_name)] = '\0';
                ep->line[sizeof(ut->ut_line)] = '\0';
                ep->host[sizeof(ut->ut_host)] = '\0';
+               fixhost(ep);
                ep->tv.tv_sec = ut->ut_time;
                *nextp = ep;
                nextp = &(ep->next);
@@ -378,6 +379,7 @@
 
        if (!nflag) {
                int     rv;
+               char    *p;
 
                rv = gethostname(domain, sizeof(domain));
                domain[sizeof(domain) - 1] = '\0';
@@ -388,36 +390,6 @@
        }
 
        for (ep = ehead; ep != NULL; ep = ep->next) {
-               char host_buf[MAXHOSTNAMELEN + 1];
-
-               strlcpy(host_buf, *ep->host ? ep->host : "-", sizeof(host_buf));
-               p = host_buf;
-
-               for (x = p; x < p + MAXHOSTNAMELEN; x++)
-                       if (*x == '\0' || *x == ':')
-                               break;
-               if (x == p + MAXHOSTNAMELEN || *x != ':')
-                       x = NULL;
-               else
-                       *x++ = '\0';
-
-               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 &&
-                                   strcasecmp(p, domain) == 0)
-                                       *p = '\0';
-                       }
-                       p = hp->h_name;
-               }
-               if (x) {
-                       (void)snprintf(buf, sizeof(buf), "%s:%s", p, x);
-                       p = buf;
-               }
-
                if (ep->tp != NULL)
                        kp = ep->tp;
                else if (ep->pp != NULL)
@@ -434,7 +406,7 @@
                usrnp = (kp == NULL) ? ep->name : kp->p_login;
                (void)printf("%-*s %-7.7s %-*.*s ",
                    maxname, usrnp, ep->line,
-                   maxhost, maxhost, *p ? p : "-");
+                   maxhost, maxhost, ep->host);
                then = (time_t)ep->tv.tv_sec;
                pr_attime(&then, &now);
                pr_idle(ep->idle);
@@ -639,6 +611,49 @@
 }
 
 static void
+fixhost(struct entry *ep)
+{
+       char host_buf[sizeof(ep->host)];
+       char *p, *x;
+       struct hostent *hp;
+       struct in_addr l;
+
+       strlcpy(host_buf, *ep->host ? ep->host : "-", sizeof(host_buf));
+       p = host_buf;
+
+       /*
+        * XXX: Historical behavior, ':' in hostname means X display number,
+        * IPv6 not handled.
+        */
+       for (x = p; x < &host_buf[sizeof(host_buf)]; x++)
+               if (*x == '\0' || *x == ':')
+                       break;
+       if (x == p + sizeof(host_buf) || *x != ':')
+               x = NULL;
+       else
+               *x++ = '\0';
+
+       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 &&
+                           strcasecmp(p, domain) == 0)
+                               *p = '\0';
+               }
+               p = hp->h_name;
+       }
+
+       if (x)
+               (void)snprintf(ep->host, sizeof(ep->host), "%s:%s", p, x);
+       else
+
+               strlcpy(ep->host, p, sizeof(ep->host));
+}
+
+static void
 usage(int wcmd)
 {
 



Home | Main Index | Thread Index | Old Index