xref: /freebsd/usr.bin/w/w.c (revision 69fe77ce99d22158d0d9c6f7dd69faffb049b8d0)
19b50d902SRodney W. Grimes /*-
29b50d902SRodney W. Grimes  * Copyright (c) 1980, 1991, 1993, 1994
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
348b56c58bSMark Murray #include <sys/cdefs.h>
358b56c58bSMark Murray 
368b56c58bSMark Murray __FBSDID("$FreeBSD$");
378b56c58bSMark Murray 
389b50d902SRodney W. Grimes #ifndef lint
3990389da9SPhilippe Charnier static const char copyright[] =
409b50d902SRodney W. Grimes "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
419b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
428b56c58bSMark Murray #endif
439b50d902SRodney W. Grimes 
449b50d902SRodney W. Grimes #ifndef lint
458b56c58bSMark Murray static const char sccsid[] = "@(#)w.c	8.4 (Berkeley) 4/16/94";
4690389da9SPhilippe Charnier #endif
479b50d902SRodney W. Grimes 
489b50d902SRodney W. Grimes /*
499b50d902SRodney W. Grimes  * w - print system status (who and what)
509b50d902SRodney W. Grimes  *
519b50d902SRodney W. Grimes  * This program is similar to the systat command on Tenex/Tops 10/20
529b50d902SRodney W. Grimes  *
539b50d902SRodney W. Grimes  */
549b50d902SRodney W. Grimes #include <sys/param.h>
559b50d902SRodney W. Grimes #include <sys/time.h>
569b50d902SRodney W. Grimes #include <sys/stat.h>
579b50d902SRodney W. Grimes #include <sys/sysctl.h>
589b50d902SRodney W. Grimes #include <sys/proc.h>
599b50d902SRodney W. Grimes #include <sys/user.h>
609b50d902SRodney W. Grimes #include <sys/ioctl.h>
619b50d902SRodney W. Grimes #include <sys/socket.h>
629b50d902SRodney W. Grimes #include <sys/tty.h>
639b50d902SRodney W. Grimes 
649b50d902SRodney W. Grimes #include <machine/cpu.h>
659b50d902SRodney W. Grimes #include <netinet/in.h>
669b50d902SRodney W. Grimes #include <arpa/inet.h>
678b56c58bSMark Murray #include <arpa/nameser.h>
689b50d902SRodney W. Grimes 
699b50d902SRodney W. Grimes #include <ctype.h>
709b50d902SRodney W. Grimes #include <err.h>
719b50d902SRodney W. Grimes #include <errno.h>
729b50d902SRodney W. Grimes #include <fcntl.h>
739b50d902SRodney W. Grimes #include <kvm.h>
7400034645SAndrey A. Chernov #include <langinfo.h>
755b718312SBrian Somers #include <libutil.h>
76af943f55SThomas Moestl #include <limits.h>
7700034645SAndrey A. Chernov #include <locale.h>
789b50d902SRodney W. Grimes #include <netdb.h>
799b50d902SRodney W. Grimes #include <nlist.h>
809b50d902SRodney W. Grimes #include <paths.h>
818b56c58bSMark Murray #include <resolv.h>
829b50d902SRodney W. Grimes #include <stdio.h>
839b50d902SRodney W. Grimes #include <stdlib.h>
849b50d902SRodney W. Grimes #include <string.h>
85c76b41b7SPeter Wemm #include <timeconv.h>
869b50d902SRodney W. Grimes #include <unistd.h>
879b50d902SRodney W. Grimes #include <utmp.h>
889b50d902SRodney W. Grimes #include <vis.h>
899b50d902SRodney W. Grimes 
909b50d902SRodney W. Grimes #include "extern.h"
919b50d902SRodney W. Grimes 
929b50d902SRodney W. Grimes struct timeval	boottime;
939b50d902SRodney W. Grimes struct utmp	utmp;
949b50d902SRodney W. Grimes struct winsize	ws;
959b50d902SRodney W. Grimes kvm_t	       *kd;
969b50d902SRodney W. Grimes time_t		now;		/* the current time of day */
979b50d902SRodney W. Grimes int		ttywidth;	/* width of tty */
989b50d902SRodney W. Grimes int		argwidth;	/* width of tty */
999b50d902SRodney W. Grimes int		header = 1;	/* true if -h flag: don't print heading */
1009b50d902SRodney W. Grimes int		nflag;		/* true if -n flag: don't convert addrs */
1011bb32253SAndrey A. Chernov int		dflag;		/* true if -d flag: output debug info */
10269b41093SPeter Wemm int		sortidle;	/* sort by idle time */
10300034645SAndrey A. Chernov int		use_ampm;	/* use AM/PM time */
1042742fc8eSAndrey A. Chernov int             use_comma;      /* use comma as floats separator */
105ac3cd520SBrian Feldman char	      **sel_users;	/* login array of particular users selected */
1069b50d902SRodney W. Grimes 
1079b50d902SRodney W. Grimes /*
1089b50d902SRodney W. Grimes  * One of these per active utmp entry.
1099b50d902SRodney W. Grimes  */
1109b50d902SRodney W. Grimes struct	entry {
1119b50d902SRodney W. Grimes 	struct	entry *next;
1129b50d902SRodney W. Grimes 	struct	utmp utmp;
1139b50d902SRodney W. Grimes 	dev_t	tdev;			/* dev_t of terminal */
1149b50d902SRodney W. Grimes 	time_t	idle;			/* idle time of terminal in seconds */
1159b50d902SRodney W. Grimes 	struct	kinfo_proc *kp;		/* `most interesting' proc */
1169b50d902SRodney W. Grimes 	char	*args;			/* arg list of interesting process */
1171bb32253SAndrey A. Chernov 	struct	kinfo_proc *dkp;	/* debug option proc list */
1189b50d902SRodney W. Grimes } *ep, *ehead = NULL, **nextp = &ehead;
1199b50d902SRodney W. Grimes 
1201f7d2501SKirk McKusick #define debugproc(p) *((struct kinfo_proc **)&(p)->ki_spare[0])
121ac3cd520SBrian Feldman 
1225b718312SBrian Somers /* W_DISPHOSTSIZE should not be greater than UT_HOSTSIZE */
1235b718312SBrian Somers #define	W_DISPHOSTSIZE	16
1245b718312SBrian Somers 
1253f330d7dSWarner Losh static void		 pr_header(time_t *, int);
1263f330d7dSWarner Losh static struct stat	*ttystat(char *, int);
1273f330d7dSWarner Losh static void		 usage(int);
1283f330d7dSWarner Losh static int		 this_is_uptime(const char *s);
1299b50d902SRodney W. Grimes 
1303f330d7dSWarner Losh char *fmt_argv(char **, char *, int);	/* ../../bin/ps/fmt.c */
1319b50d902SRodney W. Grimes 
1329b50d902SRodney W. Grimes int
133e8e649ccSJuli Mallett main(int argc, char *argv[])
1349b50d902SRodney W. Grimes {
1359b50d902SRodney W. Grimes 	struct kinfo_proc *kp;
1361bb32253SAndrey A. Chernov 	struct kinfo_proc *dkp;
1379b50d902SRodney W. Grimes 	struct stat *stp;
1389b50d902SRodney W. Grimes 	FILE *ut;
1393bebe991SBrian Somers 	time_t touched;
1406ff4ec18SPeter Wemm 	int ch, i, nentries, nusers, wcmd, longidle, dropgid;
1418b56c58bSMark Murray 	const char *memf, *nlistf, *p;
1428b56c58bSMark Murray 	char *x_suffix;
143af943f55SThomas Moestl 	char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX];
1445b718312SBrian Somers 	char fn[MAXHOSTNAMELEN];
1455b718312SBrian Somers 	char *dot;
1469b50d902SRodney W. Grimes 
147d1b2ad1aSAndrey A. Chernov 	(void)setlocale(LC_ALL, "");
14800034645SAndrey A. Chernov 	use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
1492742fc8eSAndrey A. Chernov 	use_comma = (*nl_langinfo(RADIXCHAR) != ',');
150baf72ec1SAndrey A. Chernov 
1519b50d902SRodney W. Grimes 	/* Are we w(1) or uptime(1)? */
152a4f33f43SMatthew Dillon 	if (this_is_uptime(argv[0]) == 0) {
1539b50d902SRodney W. Grimes 		wcmd = 0;
1549b50d902SRodney W. Grimes 		p = "";
1559b50d902SRodney W. Grimes 	} else {
1569b50d902SRodney W. Grimes 		wcmd = 1;
1571bb32253SAndrey A. Chernov 		p = "dhiflM:N:nsuw";
1589b50d902SRodney W. Grimes 	}
1599b50d902SRodney W. Grimes 
1606ff4ec18SPeter Wemm 	dropgid = 0;
1616ff4ec18SPeter Wemm 	memf = nlistf = _PATH_DEVNULL;
1621c8af878SWarner Losh 	while ((ch = getopt(argc, argv, p)) != -1)
1639b50d902SRodney W. Grimes 		switch (ch) {
1641bb32253SAndrey A. Chernov 		case 'd':
1651bb32253SAndrey A. Chernov 			dflag = 1;
1661bb32253SAndrey A. Chernov 			break;
1679b50d902SRodney W. Grimes 		case 'h':
1689b50d902SRodney W. Grimes 			header = 0;
1699b50d902SRodney W. Grimes 			break;
1709b50d902SRodney W. Grimes 		case 'i':
1719b50d902SRodney W. Grimes 			sortidle = 1;
1729b50d902SRodney W. Grimes 			break;
1739b50d902SRodney W. Grimes 		case 'M':
1749b50d902SRodney W. Grimes 			header = 0;
1759b50d902SRodney W. Grimes 			memf = optarg;
1766ff4ec18SPeter Wemm 			dropgid = 1;
1779b50d902SRodney W. Grimes 			break;
1789b50d902SRodney W. Grimes 		case 'N':
1799b50d902SRodney W. Grimes 			nlistf = optarg;
1806ff4ec18SPeter Wemm 			dropgid = 1;
1819b50d902SRodney W. Grimes 			break;
1829b50d902SRodney W. Grimes 		case 'n':
1839b50d902SRodney W. Grimes 			nflag = 1;
1849b50d902SRodney W. Grimes 			break;
1859b50d902SRodney W. Grimes 		case 'f': case 'l': case 's': case 'u': case 'w':
1869b50d902SRodney W. Grimes 			warnx("[-flsuw] no longer supported");
1879b50d902SRodney W. Grimes 			/* FALLTHROUGH */
1889b50d902SRodney W. Grimes 		case '?':
1899b50d902SRodney W. Grimes 		default:
1909b50d902SRodney W. Grimes 			usage(wcmd);
1919b50d902SRodney W. Grimes 		}
1929b50d902SRodney W. Grimes 	argc -= optind;
1939b50d902SRodney W. Grimes 	argv += optind;
1949b50d902SRodney W. Grimes 
1956367cd09SPeter Wemm 	if (!(_res.options & RES_INIT))
1966367cd09SPeter Wemm 		res_init();
1976367cd09SPeter Wemm 	_res.retrans = 2;	/* resolver timeout to 2 seconds per try */
1986367cd09SPeter Wemm 	_res.retry = 1;		/* only try once.. */
1996367cd09SPeter Wemm 
20066e5b18fSPaul Traina 	/*
20166e5b18fSPaul Traina 	 * Discard setgid privileges if not the running kernel so that bad
20266e5b18fSPaul Traina 	 * guys can't print interesting stuff from kernel memory.
20366e5b18fSPaul Traina 	 */
2046ff4ec18SPeter Wemm 	if (dropgid)
20566e5b18fSPaul Traina 		setgid(getgid());
20666e5b18fSPaul Traina 
2079b50d902SRodney W. Grimes 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL)
2089b50d902SRodney W. Grimes 		errx(1, "%s", errbuf);
2099b50d902SRodney W. Grimes 
2109b50d902SRodney W. Grimes 	(void)time(&now);
2119b50d902SRodney W. Grimes 	if ((ut = fopen(_PATH_UTMP, "r")) == NULL)
2129b50d902SRodney W. Grimes 		err(1, "%s", _PATH_UTMP);
2139b50d902SRodney W. Grimes 
2149b50d902SRodney W. Grimes 	if (*argv)
215ac3cd520SBrian Feldman 		sel_users = argv;
2169b50d902SRodney W. Grimes 
2179b50d902SRodney W. Grimes 	for (nusers = 0; fread(&utmp, sizeof(utmp), 1, ut);) {
2189b50d902SRodney W. Grimes 		if (utmp.ut_name[0] == '\0')
2199b50d902SRodney W. Grimes 			continue;
2200cadb9caSBrian Somers 		if (!(stp = ttystat(utmp.ut_line, UT_LINESIZE)))
2210cadb9caSBrian Somers 			continue;	/* corrupted record */
2229b50d902SRodney W. Grimes 		++nusers;
223ac3cd520SBrian Feldman 		if (wcmd == 0)
2249b50d902SRodney W. Grimes 			continue;
225ac3cd520SBrian Feldman 		if (sel_users) {
226ac3cd520SBrian Feldman 			int usermatch;
227ac3cd520SBrian Feldman 			char **user;
228ac3cd520SBrian Feldman 
229ac3cd520SBrian Feldman 			usermatch = 0;
230ac3cd520SBrian Feldman 			for (user = sel_users; !usermatch && *user; user++)
231ac3cd520SBrian Feldman 				if (!strncmp(utmp.ut_name, *user, UT_NAMESIZE))
232ac3cd520SBrian Feldman 					usermatch = 1;
233ac3cd520SBrian Feldman 			if (!usermatch)
234ac3cd520SBrian Feldman 				continue;
235ac3cd520SBrian Feldman 		}
2369b50d902SRodney W. Grimes 		if ((ep = calloc(1, sizeof(struct entry))) == NULL)
23790389da9SPhilippe Charnier 			errx(1, "calloc");
2389b50d902SRodney W. Grimes 		*nextp = ep;
239ac3cd520SBrian Feldman 		nextp = &ep->next;
240ac3cd520SBrian Feldman 		memmove(&ep->utmp, &utmp, sizeof(struct utmp));
2419b50d902SRodney W. Grimes 		ep->tdev = stp->st_rdev;
2429b50d902SRodney W. Grimes #ifdef CPU_CONSDEV
2439b50d902SRodney W. Grimes 		/*
2449b50d902SRodney W. Grimes 		 * If this is the console device, attempt to ascertain
2459b50d902SRodney W. Grimes 		 * the true console device dev_t.
2469b50d902SRodney W. Grimes 		 */
2479b50d902SRodney W. Grimes 		if (ep->tdev == 0) {
2489b50d902SRodney W. Grimes 			int mib[2];
2499b50d902SRodney W. Grimes 			size_t size;
2509b50d902SRodney W. Grimes 
2519b50d902SRodney W. Grimes 			mib[0] = CTL_MACHDEP;
2529b50d902SRodney W. Grimes 			mib[1] = CPU_CONSDEV;
2539b50d902SRodney W. Grimes 			size = sizeof(dev_t);
2549b50d902SRodney W. Grimes 			(void)sysctl(mib, 2, &ep->tdev, &size, NULL, 0);
2559b50d902SRodney W. Grimes 		}
2569b50d902SRodney W. Grimes #endif
2573bebe991SBrian Somers 		touched = stp->st_atime;
2583bebe991SBrian Somers 		if (touched < ep->utmp.ut_time) {
2593bebe991SBrian Somers 			/* tty untouched since before login */
2603bebe991SBrian Somers 			touched = ep->utmp.ut_time;
2613bebe991SBrian Somers 		}
2623bebe991SBrian Somers 		if ((ep->idle = now - touched) < 0)
2639b50d902SRodney W. Grimes 			ep->idle = 0;
2649b50d902SRodney W. Grimes 	}
2659b50d902SRodney W. Grimes 	(void)fclose(ut);
2669b50d902SRodney W. Grimes 
2679b50d902SRodney W. Grimes 	if (header || wcmd == 0) {
2689b50d902SRodney W. Grimes 		pr_header(&now, nusers);
269c59c7f97SPoul-Henning Kamp 		if (wcmd == 0) {
270c59c7f97SPoul-Henning Kamp 			(void)kvm_close(kd);
2719b50d902SRodney W. Grimes 			exit(0);
272c59c7f97SPoul-Henning Kamp 		}
2739b50d902SRodney W. Grimes 
274a88d7a82SJoseph Koshy #define HEADER_USER		"USER"
275a88d7a82SJoseph Koshy #define HEADER_TTY		"TTY"
276a88d7a82SJoseph Koshy #define HEADER_FROM		"FROM"
277a88d7a82SJoseph Koshy #define HEADER_LOGIN_IDLE	"LOGIN@  IDLE "
278a88d7a82SJoseph Koshy #define HEADER_WHAT		"WHAT\n"
2795b718312SBrian Somers #define WUSED  (UT_NAMESIZE + UT_LINESIZE + W_DISPHOSTSIZE + \
280a88d7a82SJoseph Koshy 		sizeof(HEADER_LOGIN_IDLE) + 3)	/* header width incl. spaces */
281a88d7a82SJoseph Koshy 		(void)printf("%-*.*s %-*.*s %-*.*s  %s",
282a88d7a82SJoseph Koshy 				UT_NAMESIZE, UT_NAMESIZE, HEADER_USER,
283a88d7a82SJoseph Koshy 				UT_LINESIZE, UT_LINESIZE, HEADER_TTY,
2845b718312SBrian Somers 				W_DISPHOSTSIZE, W_DISPHOSTSIZE, HEADER_FROM,
285a88d7a82SJoseph Koshy 				HEADER_LOGIN_IDLE HEADER_WHAT);
2860812a2b4SPeter Wemm 	}
2879b50d902SRodney W. Grimes 
2889b50d902SRodney W. Grimes 	if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL)
2899b50d902SRodney W. Grimes 		err(1, "%s", kvm_geterr(kd));
2909b50d902SRodney W. Grimes 	for (i = 0; i < nentries; i++, kp++) {
2911f7d2501SKirk McKusick 		if (kp->ki_stat == SIDL || kp->ki_stat == SZOMB)
2929b50d902SRodney W. Grimes 			continue;
2939b50d902SRodney W. Grimes 		for (ep = ehead; ep != NULL; ep = ep->next) {
2941f7d2501SKirk McKusick 			if (ep->tdev == kp->ki_tdev) {
2959b50d902SRodney W. Grimes 				/*
2961bb32253SAndrey A. Chernov 				 * proc is associated with this terminal
2971bb32253SAndrey A. Chernov 				 */
2981f7d2501SKirk McKusick 				if (ep->kp == NULL && kp->ki_pgid == kp->ki_tpgid) {
2991bb32253SAndrey A. Chernov 					/*
3001bb32253SAndrey A. Chernov 					 * Proc is 'most interesting'
3019b50d902SRodney W. Grimes 					 */
3021f7d2501SKirk McKusick 					if (proc_compare(ep->kp, kp))
3039b50d902SRodney W. Grimes 						ep->kp = kp;
3041bb32253SAndrey A. Chernov 				}
3051bb32253SAndrey A. Chernov 				/*
3061bb32253SAndrey A. Chernov 				 * Proc debug option info; add to debug
3071f7d2501SKirk McKusick 				 * list using kinfo_proc ki_spare[0]
3081bb32253SAndrey A. Chernov 				 * as next pointer; ptr to ptr avoids the
3091bb32253SAndrey A. Chernov 				 * ptr = long assumption.
3101bb32253SAndrey A. Chernov 				 */
3111bb32253SAndrey A. Chernov 				dkp = ep->dkp;
3121bb32253SAndrey A. Chernov 				ep->dkp = kp;
313ac3cd520SBrian Feldman 				debugproc(kp) = dkp;
3149b50d902SRodney W. Grimes 			}
3159b50d902SRodney W. Grimes 		}
3169b50d902SRodney W. Grimes 	}
3179b50d902SRodney W. Grimes 	if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&
3189b50d902SRodney W. Grimes 	     ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 &&
3199b50d902SRodney W. Grimes 	     ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0)
3209b50d902SRodney W. Grimes 	       ttywidth = 79;
3219b50d902SRodney W. Grimes         else
3229b50d902SRodney W. Grimes 	       ttywidth = ws.ws_col - 1;
3239b50d902SRodney W. Grimes 	argwidth = ttywidth - WUSED;
3249b50d902SRodney W. Grimes 	if (argwidth < 4)
3259b50d902SRodney W. Grimes 		argwidth = 8;
3269b50d902SRodney W. Grimes 	for (ep = ehead; ep != NULL; ep = ep->next) {
3279b50d902SRodney W. Grimes 		if (ep->kp == NULL) {
3288b56c58bSMark Murray 			ep->args = strdup("-");
3299b50d902SRodney W. Grimes 			continue;
3309b50d902SRodney W. Grimes 		}
3319b50d902SRodney W. Grimes 		ep->args = fmt_argv(kvm_getargv(kd, ep->kp, argwidth),
3321f7d2501SKirk McKusick 		    ep->kp->ki_comm, MAXCOMLEN);
3339b50d902SRodney W. Grimes 		if (ep->args == NULL)
3349b50d902SRodney W. Grimes 			err(1, NULL);
3359b50d902SRodney W. Grimes 	}
3369b50d902SRodney W. Grimes 	/* sort by idle time */
3379b50d902SRodney W. Grimes 	if (sortidle && ehead != NULL) {
338ac3cd520SBrian Feldman 		struct entry *from, *save;
3399b50d902SRodney W. Grimes 
340ac3cd520SBrian Feldman 		from = ehead;
3419b50d902SRodney W. Grimes 		ehead = NULL;
3429b50d902SRodney W. Grimes 		while (from != NULL) {
3439b50d902SRodney W. Grimes 			for (nextp = &ehead;
3449b50d902SRodney W. Grimes 			    (*nextp) && from->idle >= (*nextp)->idle;
3459b50d902SRodney W. Grimes 			    nextp = &(*nextp)->next)
3469b50d902SRodney W. Grimes 				continue;
3479b50d902SRodney W. Grimes 			save = from;
3489b50d902SRodney W. Grimes 			from = from->next;
3499b50d902SRodney W. Grimes 			save->next = *nextp;
3509b50d902SRodney W. Grimes 			*nextp = save;
3519b50d902SRodney W. Grimes 		}
3529b50d902SRodney W. Grimes 	}
3539b50d902SRodney W. Grimes 
3549b50d902SRodney W. Grimes 	for (ep = ehead; ep != NULL; ep = ep->next) {
3559587fac0SAndrey A. Chernov 		char host_buf[UT_HOSTSIZE + 1];
3565b718312SBrian Somers 		struct sockaddr_storage ss;
3575b718312SBrian Somers 		struct sockaddr *sa = (struct sockaddr *)&ss;
3588b56c58bSMark Murray 		struct sockaddr_in *lsin = (struct sockaddr_in *)&ss;
3598b56c58bSMark Murray 		struct sockaddr_in6 *lsin6 = (struct sockaddr_in6 *)&ss;
360c76b41b7SPeter Wemm 		time_t t;
3615b718312SBrian Somers 		int isaddr;
3629587fac0SAndrey A. Chernov 
3639587fac0SAndrey A. Chernov 		host_buf[UT_HOSTSIZE] = '\0';
3649587fac0SAndrey A. Chernov 		strncpy(host_buf, ep->utmp.ut_host, UT_HOSTSIZE);
3659587fac0SAndrey A. Chernov 		p = *host_buf ? host_buf : "-";
3665b718312SBrian Somers 		if ((x_suffix = strrchr(p, ':')) != NULL) {
3675b718312SBrian Somers 			if ((dot = strchr(x_suffix, '.')) != NULL &&
3685b718312SBrian Somers 			    strchr(dot+1, '.') == NULL)
3695b718312SBrian Somers 				*x_suffix++ = '\0';
3705b718312SBrian Somers 			else
3715b718312SBrian Somers 				x_suffix = NULL;
3729b50d902SRodney W. Grimes 		}
3735b718312SBrian Somers 		if (!nflag) {
3745b718312SBrian Somers 			/* Attempt to change an IP address into a name */
3755b718312SBrian Somers 			isaddr = 0;
3765b718312SBrian Somers 			memset(&ss, '\0', sizeof(ss));
3778b56c58bSMark Murray 			if (inet_pton(AF_INET6, p, &lsin6->sin6_addr) == 1) {
3788b56c58bSMark Murray 				lsin6->sin6_len = sizeof(*lsin6);
3798b56c58bSMark Murray 				lsin6->sin6_family = AF_INET6;
3805b718312SBrian Somers 				isaddr = 1;
3818b56c58bSMark Murray 			} else if (inet_pton(AF_INET, p, &lsin->sin_addr) == 1) {
3828b56c58bSMark Murray 				lsin->sin_len = sizeof(*lsin);
3838b56c58bSMark Murray 				lsin->sin_family = AF_INET;
3845b718312SBrian Somers 				isaddr = 1;
3859b50d902SRodney W. Grimes 			}
3865b718312SBrian Somers 			if (isaddr && realhostname_sa(fn, sizeof(fn), sa,
3875b718312SBrian Somers 			    sa->sa_len) == HOSTNAME_FOUND)
3885b718312SBrian Somers 				p = fn;
389c54481c1SAndrey A. Chernov 		}
3905b718312SBrian Somers 		if (x_suffix) {
3915b718312SBrian Somers 			(void)snprintf(buf, sizeof(buf), "%s:%s", p, x_suffix);
3929b50d902SRodney W. Grimes 			p = buf;
3939b50d902SRodney W. Grimes 		}
3941bb32253SAndrey A. Chernov 		if (dflag) {
395ac3cd520SBrian Feldman 			for (dkp = ep->dkp; dkp != NULL; dkp = debugproc(dkp)) {
3968b56c58bSMark Murray 				const char *ptr;
397ac3cd520SBrian Feldman 
3980cadb9caSBrian Somers 				ptr = fmt_argv(kvm_getargv(kd, dkp, argwidth),
3991f7d2501SKirk McKusick 				    dkp->ki_comm, MAXCOMLEN);
4000cadb9caSBrian Somers 				if (ptr == NULL)
4010cadb9caSBrian Somers 					ptr = "-";
402ac3cd520SBrian Feldman 				(void)printf("\t\t%-9d %s\n",
4031f7d2501SKirk McKusick 				    dkp->ki_pid, ptr);
4041bb32253SAndrey A. Chernov 			}
4051bb32253SAndrey A. Chernov 		}
4063d65e4d1SPoul-Henning Kamp 		(void)printf("%-*.*s %-*.*s %-*.*s ",
4079b50d902SRodney W. Grimes 		    UT_NAMESIZE, UT_NAMESIZE, ep->utmp.ut_name,
4083d65e4d1SPoul-Henning Kamp 		    UT_LINESIZE, UT_LINESIZE,
409255318a8SAndrey A. Chernov 		    strncmp(ep->utmp.ut_line, "tty", 3) &&
410255318a8SAndrey A. Chernov 		    strncmp(ep->utmp.ut_line, "cua", 3) ?
4119b50d902SRodney W. Grimes 		    ep->utmp.ut_line : ep->utmp.ut_line + 3,
4125b718312SBrian Somers 		    W_DISPHOSTSIZE, W_DISPHOSTSIZE, *p ? p : "-");
413c76b41b7SPeter Wemm 		t = _time_to_time32(ep->utmp.ut_time);
414c76b41b7SPeter Wemm 		pr_attime(&t, &now);
41570498d5aSDaniel O'Callaghan 		longidle = pr_idle(ep->idle);
41669b41093SPeter Wemm 		(void)printf("%.*s\n", argwidth - longidle, ep->args);
4179b50d902SRodney W. Grimes 	}
418c59c7f97SPoul-Henning Kamp 	(void)kvm_close(kd);
4199b50d902SRodney W. Grimes 	exit(0);
4209b50d902SRodney W. Grimes }
4219b50d902SRodney W. Grimes 
4229b50d902SRodney W. Grimes static void
423e8e649ccSJuli Mallett pr_header(time_t *nowp, int nusers)
4249b50d902SRodney W. Grimes {
4259b50d902SRodney W. Grimes 	double avenrun[3];
426716ced0bSDavid E. O'Brien 	time_t uptime;
4279d8c91b7SAndrey A. Chernov 	int days, hrs, i, mins, secs;
4289b50d902SRodney W. Grimes 	int mib[2];
4299b50d902SRodney W. Grimes 	size_t size;
4309b50d902SRodney W. Grimes 	char buf[256];
4319b50d902SRodney W. Grimes 
4329b50d902SRodney W. Grimes 	/*
4339b50d902SRodney W. Grimes 	 * Print time of day.
4349b50d902SRodney W. Grimes 	 */
43569fe77ceSPhilippe Charnier 	if (strftime(buf, sizeof(buf),
43669fe77ceSPhilippe Charnier 	    use_ampm ? "%l:%M%p" : "%k:%M", localtime(nowp)) != 0)
4379b50d902SRodney W. Grimes 		(void)printf("%s ", buf);
4389b50d902SRodney W. Grimes 	/*
4399b50d902SRodney W. Grimes 	 * Print how long system has been up.
4409b50d902SRodney W. Grimes 	 * (Found by looking getting "boottime" from the kernel)
4419b50d902SRodney W. Grimes 	 */
4429b50d902SRodney W. Grimes 	mib[0] = CTL_KERN;
4439b50d902SRodney W. Grimes 	mib[1] = KERN_BOOTTIME;
4449b50d902SRodney W. Grimes 	size = sizeof(boottime);
4459b50d902SRodney W. Grimes 	if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
4469b50d902SRodney W. Grimes 	    boottime.tv_sec != 0) {
447716ced0bSDavid E. O'Brien 		uptime = now - boottime.tv_sec;
448716ced0bSDavid E. O'Brien 		if (uptime > 60)
449716ced0bSDavid E. O'Brien 			uptime += 30;
450716ced0bSDavid E. O'Brien 		days = uptime / 86400;
451716ced0bSDavid E. O'Brien 		uptime %= 86400;
452716ced0bSDavid E. O'Brien 		hrs = uptime / 3600;
453716ced0bSDavid E. O'Brien 		uptime %= 3600;
454716ced0bSDavid E. O'Brien 		mins = uptime / 60;
455716ced0bSDavid E. O'Brien 		secs = uptime % 60;
4569b50d902SRodney W. Grimes 		(void)printf(" up");
4579b50d902SRodney W. Grimes 		if (days > 0)
4589b50d902SRodney W. Grimes 			(void)printf(" %d day%s,", days, days > 1 ? "s" : "");
4599b50d902SRodney W. Grimes 		if (hrs > 0 && mins > 0)
4609b50d902SRodney W. Grimes 			(void)printf(" %2d:%02d,", hrs, mins);
4619d8c91b7SAndrey A. Chernov 		else if (hrs > 0)
462ac3cd520SBrian Feldman 			(void)printf(" %d hr%s,", hrs, hrs > 1 ? "s" : "");
4639d8c91b7SAndrey A. Chernov 		else if (mins > 0)
464ac3cd520SBrian Feldman 			(void)printf(" %d min%s,", mins, mins > 1 ? "s" : "");
4659d8c91b7SAndrey A. Chernov 		else
466ac3cd520SBrian Feldman 			(void)printf(" %d sec%s,", secs, secs > 1 ? "s" : "");
4679b50d902SRodney W. Grimes 	}
4689b50d902SRodney W. Grimes 
4699b50d902SRodney W. Grimes 	/* Print number of users logged in to system */
4701b0c06d9SScott Mace 	(void)printf(" %d user%s", nusers, nusers == 1 ? "" : "s");
4719b50d902SRodney W. Grimes 
4729b50d902SRodney W. Grimes 	/*
4739b50d902SRodney W. Grimes 	 * Print 1, 5, and 15 minute load averages.
4749b50d902SRodney W. Grimes 	 */
4759b50d902SRodney W. Grimes 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1)
4769b50d902SRodney W. Grimes 		(void)printf(", no load average information available\n");
4779b50d902SRodney W. Grimes 	else {
4789b50d902SRodney W. Grimes 		(void)printf(", load averages:");
4798b56c58bSMark Murray 		for (i = 0; i < (int)(sizeof(avenrun) / sizeof(avenrun[0])); i++) {
4802742fc8eSAndrey A. Chernov 			if (use_comma && i > 0)
4812742fc8eSAndrey A. Chernov 				(void)printf(",");
482e25fd27cSAndrey A. Chernov 			(void)printf(" %.2f", avenrun[i]);
4832742fc8eSAndrey A. Chernov 		}
4849b50d902SRodney W. Grimes 		(void)printf("\n");
4859b50d902SRodney W. Grimes 	}
4869b50d902SRodney W. Grimes }
4879b50d902SRodney W. Grimes 
4889b50d902SRodney W. Grimes static struct stat *
489e8e649ccSJuli Mallett ttystat(char *line, int sz)
4909b50d902SRodney W. Grimes {
4919b50d902SRodney W. Grimes 	static struct stat sb;
4929b50d902SRodney W. Grimes 	char ttybuf[MAXPATHLEN];
4939b50d902SRodney W. Grimes 
4940cadb9caSBrian Somers 	(void)snprintf(ttybuf, sizeof(ttybuf), "%s%.*s", _PATH_DEV, sz, line);
495d0d0355eSSean Chittenden 	if (stat(ttybuf, &sb) == 0) {
4969b50d902SRodney W. Grimes 		return (&sb);
497d0d0355eSSean Chittenden 	} else
498d0d0355eSSean Chittenden 		return (NULL);
4999b50d902SRodney W. Grimes }
5009b50d902SRodney W. Grimes 
5019b50d902SRodney W. Grimes static void
502e8e649ccSJuli Mallett usage(int wcmd)
5039b50d902SRodney W. Grimes {
5049b50d902SRodney W. Grimes 	if (wcmd)
5059b50d902SRodney W. Grimes 		(void)fprintf(stderr,
506ac3cd520SBrian Feldman 		    "usage: w [-dhin] [-M core] [-N system] [user ...]\n");
5079b50d902SRodney W. Grimes 	else
508ac3cd520SBrian Feldman 		(void)fprintf(stderr, "usage: uptime\n");
5099b50d902SRodney W. Grimes 	exit(1);
5109b50d902SRodney W. Grimes }
511a4f33f43SMatthew Dillon 
512a4f33f43SMatthew Dillon static int
513e8e649ccSJuli Mallett this_is_uptime(const char *s)
514a4f33f43SMatthew Dillon {
515a4f33f43SMatthew Dillon 	const char *u;
516a4f33f43SMatthew Dillon 
517a4f33f43SMatthew Dillon 	if ((u = strrchr(s, '/')) != NULL)
518a4f33f43SMatthew Dillon 		++u;
519a4f33f43SMatthew Dillon 	else
520a4f33f43SMatthew Dillon 		u = s;
521a4f33f43SMatthew Dillon 	if (strcmp(u, "uptime") == 0)
522a4f33f43SMatthew Dillon 		return (0);
523a4f33f43SMatthew Dillon 	return (-1);
524a4f33f43SMatthew Dillon }
525