Home | History | Annotate | Line # | Download | only in w
w.c revision 1.85
      1 /*	$NetBSD: w.c,v 1.85 2020/06/30 14:57:25 kim Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1980, 1991, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\
     35  The Regents of the University of California.  All rights reserved.");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)w.c	8.6 (Berkeley) 6/30/94";
     41 #else
     42 __RCSID("$NetBSD: w.c,v 1.85 2020/06/30 14:57:25 kim Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 /*
     47  * w - print system status (who and what)
     48  *
     49  * This program is similar to the systat command on Tenex/Tops 10/20
     50  *
     51  */
     52 #include <sys/param.h>
     53 #include <sys/types.h>
     54 #include <sys/time.h>
     55 #include <sys/stat.h>
     56 #include <sys/sysctl.h>
     57 #include <sys/proc.h>
     58 #include <sys/ioctl.h>
     59 #include <sys/socket.h>
     60 
     61 #include <netinet/in.h>
     62 #include <arpa/inet.h>
     63 
     64 #include <ctype.h>
     65 #include <err.h>
     66 #include <errno.h>
     67 #include <fcntl.h>
     68 #include <kvm.h>
     69 #include <limits.h>
     70 #include <netdb.h>
     71 #include <nlist.h>
     72 #include <paths.h>
     73 #include <stdio.h>
     74 #include <stdlib.h>
     75 #include <string.h>
     76 #include <time.h>
     77 #include <tzfile.h>
     78 #include <unistd.h>
     79 #ifdef SUPPORT_UTMP
     80 #include <utmp.h>
     81 #endif
     82 #ifdef SUPPORT_UTMPX
     83 #include <utmpx.h>
     84 #endif
     85 #include <vis.h>
     86 
     87 #include "extern.h"
     88 
     89 struct timespec	boottime;
     90 struct winsize	ws;
     91 kvm_t	       *kd;
     92 time_t		now;		/* the current time of day */
     93 int		ttywidth;	/* width of tty */
     94 int		argwidth;	/* width of tty left to print process args */
     95 int		header = 1;	/* true if -h flag: don't print heading */
     96 int		nflag;		/* true if -n flag: don't convert addrs */
     97 int		wflag;		/* true if -w flag: wide printout */
     98 int		sortidle;	/* sort bu idle time */
     99 char	       *sel_user;	/* login of particular user selected */
    100 char		domain[MAXHOSTNAMELEN + 1];
    101 int maxname = 8, maxline = 3, maxhost = 16;
    102 
    103 /*
    104  * One of these per active utmp entry.
    105  */
    106 struct	entry {
    107 	struct	entry *next;
    108 	char name[UTX_USERSIZE + 1];
    109 	char line[UTX_LINESIZE + 1];
    110 	char host[UTX_HOSTSIZE + 1];
    111 	char type[2];
    112 	struct timeval tv;
    113 	dev_t	tdev;			/* dev_t of terminal */
    114 	time_t	idle;			/* idle time of terminal in seconds */
    115 	struct	kinfo_proc2 *tp;	/* `most interesting' tty proc */
    116 	struct	kinfo_proc2 *pp;	/* pid proc */
    117 	pid_t	pid;			/* pid or ~0 if not known */
    118 } *ehead = NULL, **nextp = &ehead;
    119 
    120 static void	pr_args(struct kinfo_proc2 *);
    121 static void	pr_header(time_t *, int);
    122 static int	proc_compare_wrapper(const struct kinfo_proc2 *,
    123     const struct kinfo_proc2 *);
    124 #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
    125 static int	ttystat(const char *, struct stat *);
    126 static void	process(struct entry *);
    127 #endif
    128 static void	fixhost(struct entry *ep);
    129 __dead static void	usage(int);
    130 
    131 int
    132 main(int argc, char **argv)
    133 {
    134 	struct kinfo_proc2 *kp;
    135 	struct entry *ep;
    136 	int ch, i, nentries, nusers, wcmd, curtain, use_sysctl;
    137 	char *memf, *nlistf, *usrnp;
    138 	const char *options;
    139 	time_t then;
    140 	size_t len;
    141 #ifdef SUPPORT_UTMP
    142 	struct utmp *ut;
    143 #endif
    144 #ifdef SUPPORT_UTMPX
    145 	struct utmpx *utx;
    146 #endif
    147 	const char *progname;
    148 	char errbuf[_POSIX2_LINE_MAX];
    149 
    150 	setprogname(argv[0]);
    151 
    152 	/* Are we w(1) or uptime(1)? */
    153 	progname = getprogname();
    154 	if (*progname == '-')
    155 		progname++;
    156 	if (*progname == 'u') {
    157 		wcmd = 0;
    158 		options = "";
    159 	} else {
    160 		wcmd = 1;
    161 		options = "hiM:N:nw";
    162 	}
    163 
    164 	memf = nlistf = NULL;
    165 	while ((ch = getopt(argc, argv, options)) != -1)
    166 		switch (ch) {
    167 		case 'h':
    168 			header = 0;
    169 			break;
    170 		case 'i':
    171 			sortidle = 1;
    172 			break;
    173 		case 'M':
    174 			header = 0;
    175 			memf = optarg;
    176 			break;
    177 		case 'N':
    178 			nlistf = optarg;
    179 			break;
    180 		case 'n':
    181 			nflag = 1;
    182 			break;
    183 		case 'w':
    184 			wflag = 1;
    185 			break;
    186 		case '?':
    187 		default:
    188 			usage(wcmd);
    189 		}
    190 	argc -= optind;
    191 	argv += optind;
    192 
    193 	use_sysctl = (memf == NULL && nlistf == NULL);
    194 
    195 	if ((kd = kvm_openfiles(nlistf, memf, NULL,
    196 	    memf == NULL ? KVM_NO_FILES : O_RDONLY, errbuf)) == NULL)
    197 		errx(1, "%s", errbuf);
    198 
    199 	(void)time(&now);
    200 
    201 	if (use_sysctl) {
    202 		len = sizeof(curtain);
    203 		if (sysctlbyname("security.curtain", &curtain, &len,
    204 		    NULL, 0) == -1)
    205 			curtain = 0;
    206 	}
    207 
    208 	if (!nflag) {
    209 		int	rv;
    210 		char	*p;
    211 
    212 		rv = gethostname(domain, sizeof(domain));
    213 		domain[sizeof(domain) - 1] = '\0';
    214 		if (rv < 0 || (p = strchr(domain, '.')) == 0)
    215 			domain[0] = '\0';
    216 		else
    217 			memmove(domain, p, strlen(p) + 1);
    218 	}
    219 
    220 #ifdef SUPPORT_UTMPX
    221 	setutxent();
    222 #endif
    223 #ifdef SUPPORT_UTMP
    224 	setutent();
    225 #endif
    226 
    227 	if (*argv)
    228 		sel_user = *argv;
    229 
    230 	nusers = 0;
    231 #ifdef SUPPORT_UTMPX
    232 	while ((utx = getutxent()) != NULL) {
    233 		if (utx->ut_type != USER_PROCESS)
    234 			continue;
    235 		++nusers;
    236 
    237 #ifndef SUPPORT_UTMP
    238 		if (wcmd == 0)
    239 			continue;
    240 #endif	/* !SUPPORT_UTMP */
    241 
    242 		if (sel_user &&
    243 		    strncmp(utx->ut_name, sel_user, sizeof(utx->ut_name)) != 0)
    244 			continue;
    245 		if ((ep = calloc(1, sizeof(struct entry))) == NULL)
    246 			err(1, NULL);
    247 		(void)memcpy(ep->line, utx->ut_line, sizeof(utx->ut_line));
    248 		ep->line[sizeof(utx->ut_line)] = '\0';
    249 		*nextp = ep;
    250 		nextp = &(ep->next);
    251 
    252 		if (wcmd == 0)
    253 			continue;
    254 
    255 		(void)memcpy(ep->name, utx->ut_name, sizeof(utx->ut_name));
    256 		ep->name[sizeof(utx->ut_name)] = '\0';
    257 		if (!nflag || getnameinfo((struct sockaddr *)&utx->ut_ss,
    258 		    utx->ut_ss.ss_len, ep->host, sizeof(ep->host), NULL, 0,
    259 		    NI_NUMERICHOST) != 0) {
    260 			(void)memcpy(ep->host, utx->ut_host,
    261 			    sizeof(utx->ut_host));
    262 			ep->host[sizeof(utx->ut_host)] = '\0';
    263 		}
    264 		fixhost(ep);
    265 		ep->type[0] = 'x';
    266 		ep->tv = utx->ut_tv;
    267 		ep->pid = utx->ut_pid;
    268 		process(ep);
    269 	}
    270 #endif
    271 
    272 #ifdef SUPPORT_UTMP
    273 	while ((ut = getutent()) != NULL) {
    274 		if (ut->ut_name[0] == '\0')
    275 			continue;
    276 
    277 		if (sel_user &&
    278 		    strncmp(ut->ut_name, sel_user, sizeof(ut->ut_name)) != 0)
    279 			continue;
    280 
    281 		/* Don't process entries that we have utmpx for */
    282 		for (ep = ehead; ep != NULL; ep = ep->next) {
    283 			if (strncmp(ep->line, ut->ut_line,
    284 			    sizeof(ut->ut_line)) == 0)
    285 				break;
    286 		}
    287 		if (ep != NULL)
    288 			continue;
    289 
    290 		++nusers;
    291 
    292 		if (wcmd == 0)
    293 			continue;
    294 
    295 		if ((ep = calloc(1, sizeof(struct entry))) == NULL)
    296 			err(1, NULL);
    297 		(void)memcpy(ep->name, ut->ut_name, sizeof(ut->ut_name));
    298 		(void)memcpy(ep->line, ut->ut_line, sizeof(ut->ut_line));
    299 		(void)memcpy(ep->host, ut->ut_host, sizeof(ut->ut_host));
    300 		ep->name[sizeof(ut->ut_name)] = '\0';
    301 		ep->line[sizeof(ut->ut_line)] = '\0';
    302 		ep->host[sizeof(ut->ut_host)] = '\0';
    303 		fixhost(ep);
    304 		ep->tv.tv_sec = ut->ut_time;
    305 		*nextp = ep;
    306 		nextp = &(ep->next);
    307 		process(ep);
    308 	}
    309 #endif
    310 
    311 #ifdef SUPPORT_UTMPX
    312 	endutxent();
    313 #endif
    314 #ifdef SUPPORT_UTMP
    315 	endutent();
    316 #endif
    317 
    318 	if (header || wcmd == 0) {
    319 		pr_header(&now, nusers);
    320 		if (wcmd == 0)
    321 			exit (0);
    322 	}
    323 
    324 	if ((kp = kvm_getproc2(kd, KERN_PROC_ALL, 0,
    325 	    sizeof(struct kinfo_proc2), &nentries)) == NULL)
    326 		errx(1, "%s", kvm_geterr(kd));
    327 
    328 	/* Include trailing space because TTY header starts one column early. */
    329 	for (i = 0; i < nentries; i++, kp++) {
    330 
    331 		for (ep = ehead; ep != NULL; ep = ep->next) {
    332 			if (ep->tdev != 0 && ep->tdev == kp->p_tdev &&
    333 			    kp->p__pgid == kp->p_tpgid) {
    334 				/*
    335 				 * Proc is in foreground of this
    336 				 * terminal
    337 				 */
    338 				if (proc_compare_wrapper(ep->tp, kp))
    339 					ep->tp = kp;
    340 				break;
    341 			}
    342 			if (ep->pid != 0 && ep->pid == kp->p_pid) {
    343 				ep->pp = kp;
    344 				break;
    345 			}
    346 		}
    347 	}
    348 
    349 	if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&
    350 	    ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 &&
    351 	    ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0)
    352 		ttywidth = 79;
    353 	else
    354 		ttywidth = ws.ws_col - 1;
    355 
    356 	if (!wflag && maxhost > (ttywidth / 3))
    357 		maxhost = ttywidth / 3;
    358 
    359 	argwidth = printf("%-*s TTY     %-*s %*s  IDLE WHAT\n",
    360 	    maxname, "USER", maxhost, "FROM",
    361 	    7 /* "dddhhXm" */, "LOGIN@");
    362 	argwidth -= sizeof("WHAT\n") - 1 /* NUL */;
    363 	argwidth = ttywidth - argwidth;
    364 	if (argwidth < 4)
    365 		argwidth = 8;
    366 	if (wflag)
    367 		argwidth = -1;
    368 
    369 	/* sort by idle time */
    370 	if (sortidle && ehead != NULL) {
    371 		struct entry *from = ehead, *save;
    372 
    373 		ehead = NULL;
    374 		while (from != NULL) {
    375 			for (nextp = &ehead;
    376 			    (*nextp) && from->idle >= (*nextp)->idle;
    377 			    nextp = &(*nextp)->next)
    378 				continue;
    379 			save = from;
    380 			from = from->next;
    381 			save->next = *nextp;
    382 			*nextp = save;
    383 		}
    384 	}
    385 #if defined(SUPPORT_UTMP) && defined(SUPPORT_UTMPX)
    386 	else if (ehead != NULL) {
    387 		struct entry *from = ehead, *save;
    388 
    389 		ehead = NULL;
    390 		while (from != NULL) {
    391 			for (nextp = &ehead;
    392 			    (*nextp) && strcmp(from->line, (*nextp)->line) > 0;
    393 			    nextp = &(*nextp)->next)
    394 				continue;
    395 			save = from;
    396 			from = from->next;
    397 			save->next = *nextp;
    398 			*nextp = save;
    399 		}
    400 	}
    401 #endif
    402 
    403 	for (ep = ehead; ep != NULL; ep = ep->next) {
    404 		if (ep->tp != NULL)
    405 			kp = ep->tp;
    406 		else if (ep->pp != NULL)
    407 			kp = ep->pp;
    408 		else if (ep->pid != 0) {
    409 			if (curtain)
    410 				kp = NULL;
    411 			else {
    412 				warnx("Stale utmp%s entry: %s %s %s",
    413 				    ep->type, ep->name, ep->line, ep->host);
    414 				continue;
    415 			}
    416 		}
    417 		usrnp = (kp == NULL) ? ep->name : kp->p_login;
    418 		(void)printf("%-*s %-7.7s %-*.*s ",
    419 		    maxname, usrnp, ep->line,
    420 		    maxhost, maxhost, ep->host);
    421 		then = (time_t)ep->tv.tv_sec;
    422 		pr_attime(&then, &now);
    423 		pr_idle(ep->idle);
    424 		pr_args(kp);
    425 		(void)printf("\n");
    426 	}
    427 	exit(0);
    428 }
    429 
    430 static void
    431 pr_args(struct kinfo_proc2 *kp)
    432 {
    433 	char **argv;
    434 	int left;
    435 
    436 	if (kp == 0)
    437 		goto nothing;
    438 	left = argwidth;
    439 	argv = kvm_getargv2(kd, kp, (argwidth < 0) ? 0 : argwidth);
    440 	if (argv == 0) {
    441 		fmt_putc('(', &left);
    442 		fmt_puts((char *)kp->p_comm, &left);
    443 		fmt_putc(')', &left);
    444 		return;
    445 	}
    446 	while (*argv) {
    447 		fmt_puts(*argv, &left);
    448 		argv++;
    449 		fmt_putc(' ', &left);
    450 	}
    451 	return;
    452 nothing:
    453 	putchar('-');
    454 }
    455 
    456 static void
    457 pr_header(time_t *nowp, int nusers)
    458 {
    459 	double avenrun[3];
    460 	time_t uptime;
    461 	int days, hrs, mins;
    462 	int mib[2];
    463 	size_t size, i;
    464 	char buf[256];
    465 
    466 	/*
    467 	 * Print time of day.
    468 	 *
    469 	 * SCCS forces the string manipulation below, as it replaces
    470 	 * %, M, and % in a character string with the file name.
    471 	 */
    472 	(void)strftime(buf, sizeof(buf), "%l:%" "M%p", localtime(nowp));
    473 	buf[sizeof(buf) - 1] = '\0';
    474 	(void)printf("%s ", buf);
    475 
    476 	/*
    477 	 * Print how long system has been up.
    478 	 * (Found by looking getting "boottime" from the kernel)
    479 	 */
    480 	mib[0] = CTL_KERN;
    481 	mib[1] = KERN_BOOTTIME;
    482 	size = sizeof(boottime);
    483 	if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
    484 	    boottime.tv_sec != 0) {
    485 		uptime = now - boottime.tv_sec;
    486 		uptime += 30;
    487 		if (uptime > SECSPERMIN) {
    488 			days = uptime / SECSPERDAY;
    489 			uptime %= SECSPERDAY;
    490 			hrs = uptime / SECSPERHOUR;
    491 			uptime %= SECSPERHOUR;
    492 			mins = uptime / SECSPERMIN;
    493 			(void)printf(" up");
    494 			if (days > 0)
    495 				(void)printf(" %d day%s,", days,
    496 				    days > 1 ? "s" : "");
    497 			if (hrs > 0 && mins > 0)
    498 				(void)printf(" %2d:%02d,", hrs, mins);
    499 			else {
    500 				if (hrs > 0)
    501 					(void)printf(" %d hr%s,",
    502 					    hrs, hrs > 1 ? "s" : "");
    503 				if (mins > 0)
    504 					(void)printf(" %d min%s,",
    505 					    mins, mins > 1 ? "s" : "");
    506 			}
    507 		}
    508 	}
    509 
    510 	/* Print number of users logged in to system */
    511 	(void)printf(" %d user%s", nusers, nusers != 1 ? "s" : "");
    512 
    513 	/*
    514 	 * Print 1, 5, and 15 minute load averages.
    515 	 */
    516 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1)
    517 		(void)printf(", no load average information available\n");
    518 	else {
    519 		(void)printf(", load averages:");
    520 		for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) {
    521 			if (i > 0)
    522 				(void)printf(",");
    523 			(void)printf(" %.2f", avenrun[i]);
    524 		}
    525 		(void)printf("\n");
    526 	}
    527 }
    528 
    529 #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
    530 static int
    531 ttystat(const char *line, struct stat *st)
    532 {
    533 	char ttybuf[MAXPATHLEN];
    534 
    535 	(void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line);
    536 	return stat(ttybuf, st);
    537 }
    538 
    539 static void
    540 process(struct entry *ep)
    541 {
    542 	struct stat st;
    543 	time_t touched;
    544 	int max;
    545 
    546 	if ((max = strlen(ep->name)) > maxname)
    547 		maxname = max;
    548 	if ((max = strlen(ep->line)) > maxline)
    549 		maxline = max;
    550 	if ((max = strlen(ep->host)) > maxhost)
    551 		maxhost = max;
    552 
    553 	ep->tdev = 0;
    554 	ep->idle = (time_t)-1;
    555 
    556 #ifdef SUPPORT_UTMP
    557 	/*
    558 	 * Hack to recognize and correctly parse
    559 	 * ut entry made by ftpd. The "tty" used
    560 	 * by ftpd is not a real tty, just identifier in
    561 	 * form ftpPID. Pid parsed from the "tty name"
    562 	 * is used later to match corresponding process.
    563 	 * NB: This is only used for utmp entries. For utmpx,
    564 	 * we already have the pid.
    565 	 */
    566 	if (ep->pid == 0 && strncmp(ep->line, "ftp", 3) == 0) {
    567 		ep->pid = strtol(ep->line + 3, NULL, 10);
    568 		return;
    569 	}
    570 #endif
    571 	if (ttystat(ep->line, &st) == -1)
    572 		return;
    573 
    574 	ep->tdev = st.st_rdev;
    575 	/*
    576 	 * If this is the console device, attempt to ascertain
    577 	 * the true console device dev_t.
    578 	 */
    579 	if (ep->tdev == 0) {
    580 		int mib[2];
    581 		size_t size;
    582 
    583 		mib[0] = CTL_KERN;
    584 		mib[1] = KERN_CONSDEV;
    585 		size = sizeof(dev_t);
    586 		(void) sysctl(mib, 2, &ep->tdev, &size, NULL, 0);
    587 	}
    588 
    589 	touched = st.st_atime;
    590 	if (touched < ep->tv.tv_sec) {
    591 		/* tty untouched since before login */
    592 		touched = ep->tv.tv_sec;
    593 	}
    594 	if ((ep->idle = now - touched) < 0)
    595 		ep->idle = 0;
    596 }
    597 #endif
    598 
    599 static int
    600 proc_compare_wrapper(const struct kinfo_proc2 *p1,
    601     const struct kinfo_proc2 *p2)
    602 {
    603 	struct kinfo_lwp *l1, *l2;
    604 	int cnt;
    605 
    606 	if (p1 == NULL)
    607 		return 1;
    608 
    609 	l1 = kvm_getlwps(kd, p1->p_pid, 0, sizeof(*l1), &cnt);
    610 	if (l1 == NULL || cnt == 0)
    611 		return 1;
    612 
    613 	l2 = kvm_getlwps(kd, p2->p_pid, 0, sizeof(*l1), &cnt);
    614 	if (l2 == NULL || cnt == 0)
    615 		return 0;
    616 
    617 	return proc_compare(p1, l1, p2, l2);
    618 }
    619 
    620 static void
    621 fixhost(struct entry *ep)
    622 {
    623 	char host_buf[sizeof(ep->host)];
    624 	char *p, *x, *m;
    625 	struct hostent *hp;
    626 	union {
    627 		struct in_addr l4;
    628 		struct in6_addr l6;
    629 	} l;
    630 
    631 	strlcpy(host_buf, *ep->host ? ep->host : "-", sizeof(host_buf));
    632 	p = host_buf;
    633 
    634 	/*
    635 	 * One ':' in hostname means X display number, more is IPv6.
    636 	 */
    637 	for (x = p; x < &host_buf[sizeof(host_buf)]; x++)
    638 		if (*x == '\0' || *x == ':')
    639 			break;
    640 	if (x == p + sizeof(host_buf) || *x != ':')
    641 		m = x = NULL;
    642 	else {
    643 		for (m = x + 1; m < &host_buf[sizeof(host_buf)]; m++)
    644 			if (*m == '\0' || *m == ':')
    645 				break;
    646 		if (m == p + sizeof(host_buf) || *m != ':') {
    647 			*x++ = '\0';
    648 			m = NULL;
    649 		} else
    650 			x = NULL;
    651 	}
    652 	int af = m ? AF_INET6 : AF_INET;
    653 	size_t alen = m ? sizeof(l.l6) : sizeof(l.l4);
    654 	if (!nflag && inet_pton(af, p, &l) &&
    655 	    (hp = gethostbyaddr((char *)&l, alen, af))) {
    656 		if (domain[0] != '\0') {
    657 			p = hp->h_name;
    658 			p += strlen(hp->h_name);
    659 			p -= strlen(domain);
    660 			if (p > hp->h_name &&
    661 			    strcasecmp(p, domain) == 0)
    662 				*p = '\0';
    663 		}
    664 		p = hp->h_name;
    665 	}
    666 
    667 	if (x)
    668 		(void)snprintf(ep->host, sizeof(ep->host), "%s:%s", p, x);
    669 	else
    670 
    671 		strlcpy(ep->host, p, sizeof(ep->host));
    672 }
    673 
    674 static void
    675 usage(int wcmd)
    676 {
    677 
    678 	if (wcmd)
    679 		(void)fprintf(stderr,
    680 		    "Usage: %s [-hinw] [-M core] [-N system] [user]\n",
    681 		    getprogname());
    682 	else
    683 		(void)fprintf(stderr, "Usage: %s\n", getprogname());
    684 	exit(1);
    685 }
    686