Home | History | Annotate | Line # | Download | only in w
w.c revision 1.84
      1 /*	$NetBSD: w.c,v 1.84 2018/10/30 21:15:09 kre 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.84 2018/10/30 21:15:09 kre 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 #ifdef SUPPORT_UTMPX
    209 	setutxent();
    210 #endif
    211 #ifdef SUPPORT_UTMP
    212 	setutent();
    213 #endif
    214 
    215 	if (*argv)
    216 		sel_user = *argv;
    217 
    218 	nusers = 0;
    219 #ifdef SUPPORT_UTMPX
    220 	while ((utx = getutxent()) != NULL) {
    221 		if (utx->ut_type != USER_PROCESS)
    222 			continue;
    223 		++nusers;
    224 
    225 #ifndef SUPPORT_UTMP
    226 		if (wcmd == 0)
    227 			continue;
    228 #endif	/* !SUPPORT_UTMP */
    229 
    230 		if (sel_user &&
    231 		    strncmp(utx->ut_name, sel_user, sizeof(utx->ut_name)) != 0)
    232 			continue;
    233 		if ((ep = calloc(1, sizeof(struct entry))) == NULL)
    234 			err(1, NULL);
    235 		(void)memcpy(ep->line, utx->ut_line, sizeof(utx->ut_line));
    236 		ep->line[sizeof(utx->ut_line)] = '\0';
    237 		*nextp = ep;
    238 		nextp = &(ep->next);
    239 
    240 		if (wcmd == 0)
    241 			continue;
    242 
    243 		(void)memcpy(ep->name, utx->ut_name, sizeof(utx->ut_name));
    244 		ep->name[sizeof(utx->ut_name)] = '\0';
    245 		if (!nflag || getnameinfo((struct sockaddr *)&utx->ut_ss,
    246 		    utx->ut_ss.ss_len, ep->host, sizeof(ep->host), NULL, 0,
    247 		    NI_NUMERICHOST) != 0) {
    248 			(void)memcpy(ep->host, utx->ut_host,
    249 			    sizeof(utx->ut_host));
    250 			ep->host[sizeof(utx->ut_host)] = '\0';
    251 		}
    252 		fixhost(ep);
    253 		ep->type[0] = 'x';
    254 		ep->tv = utx->ut_tv;
    255 		ep->pid = utx->ut_pid;
    256 		process(ep);
    257 	}
    258 #endif
    259 
    260 #ifdef SUPPORT_UTMP
    261 	while ((ut = getutent()) != NULL) {
    262 		if (ut->ut_name[0] == '\0')
    263 			continue;
    264 
    265 		if (sel_user &&
    266 		    strncmp(ut->ut_name, sel_user, sizeof(ut->ut_name)) != 0)
    267 			continue;
    268 
    269 		/* Don't process entries that we have utmpx for */
    270 		for (ep = ehead; ep != NULL; ep = ep->next) {
    271 			if (strncmp(ep->line, ut->ut_line,
    272 			    sizeof(ut->ut_line)) == 0)
    273 				break;
    274 		}
    275 		if (ep != NULL)
    276 			continue;
    277 
    278 		++nusers;
    279 
    280 		if (wcmd == 0)
    281 			continue;
    282 
    283 		if ((ep = calloc(1, sizeof(struct entry))) == NULL)
    284 			err(1, NULL);
    285 		(void)memcpy(ep->name, ut->ut_name, sizeof(ut->ut_name));
    286 		(void)memcpy(ep->line, ut->ut_line, sizeof(ut->ut_line));
    287 		(void)memcpy(ep->host, ut->ut_host, sizeof(ut->ut_host));
    288 		ep->name[sizeof(ut->ut_name)] = '\0';
    289 		ep->line[sizeof(ut->ut_line)] = '\0';
    290 		ep->host[sizeof(ut->ut_host)] = '\0';
    291 		fixhost(ep);
    292 		ep->tv.tv_sec = ut->ut_time;
    293 		*nextp = ep;
    294 		nextp = &(ep->next);
    295 		process(ep);
    296 	}
    297 #endif
    298 
    299 #ifdef SUPPORT_UTMPX
    300 	endutxent();
    301 #endif
    302 #ifdef SUPPORT_UTMP
    303 	endutent();
    304 #endif
    305 
    306 	if (header || wcmd == 0) {
    307 		pr_header(&now, nusers);
    308 		if (wcmd == 0)
    309 			exit (0);
    310 	}
    311 
    312 	if ((kp = kvm_getproc2(kd, KERN_PROC_ALL, 0,
    313 	    sizeof(struct kinfo_proc2), &nentries)) == NULL)
    314 		errx(1, "%s", kvm_geterr(kd));
    315 
    316 	/* Include trailing space because TTY header starts one column early. */
    317 	for (i = 0; i < nentries; i++, kp++) {
    318 
    319 		for (ep = ehead; ep != NULL; ep = ep->next) {
    320 			if (ep->tdev != 0 && ep->tdev == kp->p_tdev &&
    321 			    kp->p__pgid == kp->p_tpgid) {
    322 				/*
    323 				 * Proc is in foreground of this
    324 				 * terminal
    325 				 */
    326 				if (proc_compare_wrapper(ep->tp, kp))
    327 					ep->tp = kp;
    328 				break;
    329 			}
    330 			if (ep->pid != 0 && ep->pid == kp->p_pid) {
    331 				ep->pp = kp;
    332 				break;
    333 			}
    334 		}
    335 	}
    336 
    337 	if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&
    338 	    ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 &&
    339 	    ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0)
    340 		ttywidth = 79;
    341 	else
    342 		ttywidth = ws.ws_col - 1;
    343 
    344 	if (!wflag && maxhost > (ttywidth / 3))
    345 		maxhost = ttywidth / 3;
    346 
    347 	argwidth = printf("%-*s TTY     %-*s %*s  IDLE WHAT\n",
    348 	    maxname, "USER", maxhost, "FROM",
    349 	    7 /* "dddhhXm" */, "LOGIN@");
    350 	argwidth -= sizeof("WHAT\n") - 1 /* NUL */;
    351 	argwidth = ttywidth - argwidth;
    352 	if (argwidth < 4)
    353 		argwidth = 8;
    354 	if (wflag)
    355 		argwidth = -1;
    356 
    357 	/* sort by idle time */
    358 	if (sortidle && ehead != NULL) {
    359 		struct entry *from = ehead, *save;
    360 
    361 		ehead = NULL;
    362 		while (from != NULL) {
    363 			for (nextp = &ehead;
    364 			    (*nextp) && from->idle >= (*nextp)->idle;
    365 			    nextp = &(*nextp)->next)
    366 				continue;
    367 			save = from;
    368 			from = from->next;
    369 			save->next = *nextp;
    370 			*nextp = save;
    371 		}
    372 	}
    373 #if defined(SUPPORT_UTMP) && defined(SUPPORT_UTMPX)
    374 	else if (ehead != NULL) {
    375 		struct entry *from = ehead, *save;
    376 
    377 		ehead = NULL;
    378 		while (from != NULL) {
    379 			for (nextp = &ehead;
    380 			    (*nextp) && strcmp(from->line, (*nextp)->line) > 0;
    381 			    nextp = &(*nextp)->next)
    382 				continue;
    383 			save = from;
    384 			from = from->next;
    385 			save->next = *nextp;
    386 			*nextp = save;
    387 		}
    388 	}
    389 #endif
    390 
    391 	if (!nflag) {
    392 		int	rv;
    393 		char	*p;
    394 
    395 		rv = gethostname(domain, sizeof(domain));
    396 		domain[sizeof(domain) - 1] = '\0';
    397 		if (rv < 0 || (p = strchr(domain, '.')) == 0)
    398 			domain[0] = '\0';
    399 		else
    400 			memmove(domain, p, strlen(p) + 1);
    401 	}
    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