xref: /freebsd/usr.bin/w/w.c (revision 000346454c6dc7c62495204afd8d6e11a1578ad9)
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 
349b50d902SRodney W. Grimes #ifndef lint
3590389da9SPhilippe Charnier static const char copyright[] =
369b50d902SRodney W. Grimes "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
379b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
389b50d902SRodney W. Grimes #endif /* not lint */
399b50d902SRodney W. Grimes 
409b50d902SRodney W. Grimes #ifndef lint
4190389da9SPhilippe Charnier #if 0
429b50d902SRodney W. Grimes static char sccsid[] = "@(#)w.c	8.4 (Berkeley) 4/16/94";
4390389da9SPhilippe Charnier #endif
4490389da9SPhilippe Charnier static const char rcsid[] =
45c3aac50fSPeter Wemm   "$FreeBSD$";
469b50d902SRodney W. Grimes #endif /* not lint */
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>
679b50d902SRodney W. Grimes 
689b50d902SRodney W. Grimes #include <ctype.h>
699b50d902SRodney W. Grimes #include <err.h>
709b50d902SRodney W. Grimes #include <errno.h>
719b50d902SRodney W. Grimes #include <fcntl.h>
729b50d902SRodney W. Grimes #include <kvm.h>
7300034645SAndrey A. Chernov #include <langinfo.h>
7400034645SAndrey A. Chernov #include <locale.h>
759b50d902SRodney W. Grimes #include <netdb.h>
769b50d902SRodney W. Grimes #include <nlist.h>
779b50d902SRodney W. Grimes #include <paths.h>
789b50d902SRodney W. Grimes #include <stdio.h>
799b50d902SRodney W. Grimes #include <stdlib.h>
809b50d902SRodney W. Grimes #include <string.h>
819b50d902SRodney W. Grimes #include <unistd.h>
829b50d902SRodney W. Grimes #include <utmp.h>
839b50d902SRodney W. Grimes #include <vis.h>
849b50d902SRodney W. Grimes 
856367cd09SPeter Wemm #include <arpa/nameser.h>
866367cd09SPeter Wemm #include <resolv.h>
876367cd09SPeter Wemm 
889b50d902SRodney W. Grimes #include "extern.h"
899b50d902SRodney W. Grimes 
909b50d902SRodney W. Grimes struct timeval	boottime;
919b50d902SRodney W. Grimes struct utmp	utmp;
929b50d902SRodney W. Grimes struct winsize	ws;
939b50d902SRodney W. Grimes kvm_t	       *kd;
949b50d902SRodney W. Grimes time_t		now;		/* the current time of day */
959b50d902SRodney W. Grimes time_t		uptime;		/* time of last reboot & elapsed time since */
969b50d902SRodney W. Grimes int		ttywidth;	/* width of tty */
979b50d902SRodney W. Grimes int		argwidth;	/* width of tty */
989b50d902SRodney W. Grimes int		header = 1;	/* true if -h flag: don't print heading */
999b50d902SRodney W. Grimes int		nflag;		/* true if -n flag: don't convert addrs */
1001bb32253SAndrey A. Chernov int		dflag;		/* true if -d flag: output debug info */
10169b41093SPeter Wemm int		sortidle;	/* sort by idle time */
10200034645SAndrey A. Chernov int		use_ampm;	/* use AM/PM time */
103ac3cd520SBrian Feldman char	      **sel_users;	/* login array of particular users selected */
1049b50d902SRodney W. Grimes char		domain[MAXHOSTNAMELEN];
1059b50d902SRodney W. Grimes 
1069b50d902SRodney W. Grimes /*
1079b50d902SRodney W. Grimes  * One of these per active utmp entry.
1089b50d902SRodney W. Grimes  */
1099b50d902SRodney W. Grimes struct	entry {
1109b50d902SRodney W. Grimes 	struct	entry *next;
1119b50d902SRodney W. Grimes 	struct	utmp utmp;
1129b50d902SRodney W. Grimes 	dev_t	tdev;			/* dev_t of terminal */
1139b50d902SRodney W. Grimes 	time_t	idle;			/* idle time of terminal in seconds */
1149b50d902SRodney W. Grimes 	struct	kinfo_proc *kp;		/* `most interesting' proc */
1159b50d902SRodney W. Grimes 	char	*args;			/* arg list of interesting process */
1161bb32253SAndrey A. Chernov 	struct	kinfo_proc *dkp;	/* debug option proc list */
1179b50d902SRodney W. Grimes } *ep, *ehead = NULL, **nextp = &ehead;
1189b50d902SRodney W. Grimes 
1191f7d2501SKirk McKusick #define debugproc(p) *((struct kinfo_proc **)&(p)->ki_spare[0])
120ac3cd520SBrian Feldman 
1219b50d902SRodney W. Grimes static void		 pr_header __P((time_t *, int));
122ac3cd520SBrian Feldman static struct stat	*ttystat __P((char *, int));
1239b50d902SRodney W. Grimes static void		 usage __P((int));
124a4f33f43SMatthew Dillon static int		 this_is_uptime __P((const char *s));
1259b50d902SRodney W. Grimes 
1269b50d902SRodney W. Grimes char *fmt_argv __P((char **, char *, int));	/* ../../bin/ps/fmt.c */
1279b50d902SRodney W. Grimes 
1289b50d902SRodney W. Grimes int
1299b50d902SRodney W. Grimes main(argc, argv)
1309b50d902SRodney W. Grimes 	int argc;
1319b50d902SRodney W. Grimes 	char **argv;
1329b50d902SRodney W. Grimes {
1339b50d902SRodney W. Grimes 	struct kinfo_proc *kp;
1341bb32253SAndrey A. Chernov 	struct kinfo_proc *dkp;
1359b50d902SRodney W. Grimes 	struct hostent *hp;
1369b50d902SRodney W. Grimes 	struct stat *stp;
1379b50d902SRodney W. Grimes 	FILE *ut;
1389b50d902SRodney W. Grimes 	u_long l;
1393bebe991SBrian Somers 	time_t touched;
1406ff4ec18SPeter Wemm 	int ch, i, nentries, nusers, wcmd, longidle, dropgid;
141fd67e2c5SWarner Losh 	char *memf, *nlistf, *p, *x;
1429b50d902SRodney W. Grimes 	char buf[MAXHOSTNAMELEN], errbuf[256];
1439b50d902SRodney W. Grimes 
144d1b2ad1aSAndrey A. Chernov 	(void)setlocale(LC_ALL, "");
14500034645SAndrey A. Chernov 	use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
146baf72ec1SAndrey A. Chernov 
1479b50d902SRodney W. Grimes 	/* Are we w(1) or uptime(1)? */
148a4f33f43SMatthew Dillon 	if (this_is_uptime(argv[0]) == 0) {
1499b50d902SRodney W. Grimes 		wcmd = 0;
1509b50d902SRodney W. Grimes 		p = "";
1519b50d902SRodney W. Grimes 	} else {
1529b50d902SRodney W. Grimes 		wcmd = 1;
1531bb32253SAndrey A. Chernov 		p = "dhiflM:N:nsuw";
1549b50d902SRodney W. Grimes 	}
1559b50d902SRodney W. Grimes 
1566ff4ec18SPeter Wemm 	dropgid = 0;
1576ff4ec18SPeter Wemm 	memf = nlistf = _PATH_DEVNULL;
1581c8af878SWarner Losh 	while ((ch = getopt(argc, argv, p)) != -1)
1599b50d902SRodney W. Grimes 		switch (ch) {
1601bb32253SAndrey A. Chernov 		case 'd':
1611bb32253SAndrey A. Chernov 			dflag = 1;
1621bb32253SAndrey A. Chernov 			break;
1639b50d902SRodney W. Grimes 		case 'h':
1649b50d902SRodney W. Grimes 			header = 0;
1659b50d902SRodney W. Grimes 			break;
1669b50d902SRodney W. Grimes 		case 'i':
1679b50d902SRodney W. Grimes 			sortidle = 1;
1689b50d902SRodney W. Grimes 			break;
1699b50d902SRodney W. Grimes 		case 'M':
1709b50d902SRodney W. Grimes 			header = 0;
1719b50d902SRodney W. Grimes 			memf = optarg;
1726ff4ec18SPeter Wemm 			dropgid = 1;
1739b50d902SRodney W. Grimes 			break;
1749b50d902SRodney W. Grimes 		case 'N':
1759b50d902SRodney W. Grimes 			nlistf = optarg;
1766ff4ec18SPeter Wemm 			dropgid = 1;
1779b50d902SRodney W. Grimes 			break;
1789b50d902SRodney W. Grimes 		case 'n':
1799b50d902SRodney W. Grimes 			nflag = 1;
1809b50d902SRodney W. Grimes 			break;
1819b50d902SRodney W. Grimes 		case 'f': case 'l': case 's': case 'u': case 'w':
1829b50d902SRodney W. Grimes 			warnx("[-flsuw] no longer supported");
1839b50d902SRodney W. Grimes 			/* FALLTHROUGH */
1849b50d902SRodney W. Grimes 		case '?':
1859b50d902SRodney W. Grimes 		default:
1869b50d902SRodney W. Grimes 			usage(wcmd);
1879b50d902SRodney W. Grimes 		}
1889b50d902SRodney W. Grimes 	argc -= optind;
1899b50d902SRodney W. Grimes 	argv += optind;
1909b50d902SRodney W. Grimes 
1916367cd09SPeter Wemm 	if (!(_res.options & RES_INIT))
1926367cd09SPeter Wemm 		res_init();
1936367cd09SPeter Wemm 	_res.retrans = 2;	/* resolver timeout to 2 seconds per try */
1946367cd09SPeter Wemm 	_res.retry = 1;		/* only try once.. */
1956367cd09SPeter Wemm 
19666e5b18fSPaul Traina 	/*
19766e5b18fSPaul Traina 	 * Discard setgid privileges if not the running kernel so that bad
19866e5b18fSPaul Traina 	 * guys can't print interesting stuff from kernel memory.
19966e5b18fSPaul Traina 	 */
2006ff4ec18SPeter Wemm 	if (dropgid)
20166e5b18fSPaul Traina 		setgid(getgid());
20266e5b18fSPaul Traina 
2039b50d902SRodney W. Grimes 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL)
2049b50d902SRodney W. Grimes 		errx(1, "%s", errbuf);
2059b50d902SRodney W. Grimes 
2069b50d902SRodney W. Grimes 	(void)time(&now);
2079b50d902SRodney W. Grimes 	if ((ut = fopen(_PATH_UTMP, "r")) == NULL)
2089b50d902SRodney W. Grimes 		err(1, "%s", _PATH_UTMP);
2099b50d902SRodney W. Grimes 
2109b50d902SRodney W. Grimes 	if (*argv)
211ac3cd520SBrian Feldman 		sel_users = argv;
2129b50d902SRodney W. Grimes 
2139b50d902SRodney W. Grimes 	for (nusers = 0; fread(&utmp, sizeof(utmp), 1, ut);) {
2149b50d902SRodney W. Grimes 		if (utmp.ut_name[0] == '\0')
2159b50d902SRodney W. Grimes 			continue;
2160cadb9caSBrian Somers 		if (!(stp = ttystat(utmp.ut_line, UT_LINESIZE)))
2170cadb9caSBrian Somers 			continue;	/* corrupted record */
2189b50d902SRodney W. Grimes 		++nusers;
219ac3cd520SBrian Feldman 		if (wcmd == 0)
2209b50d902SRodney W. Grimes 			continue;
221ac3cd520SBrian Feldman 		if (sel_users) {
222ac3cd520SBrian Feldman 			int usermatch;
223ac3cd520SBrian Feldman 			char **user;
224ac3cd520SBrian Feldman 
225ac3cd520SBrian Feldman 			usermatch = 0;
226ac3cd520SBrian Feldman 			for (user = sel_users; !usermatch && *user; user++)
227ac3cd520SBrian Feldman 				if (!strncmp(utmp.ut_name, *user, UT_NAMESIZE))
228ac3cd520SBrian Feldman 					usermatch = 1;
229ac3cd520SBrian Feldman 			if (!usermatch)
230ac3cd520SBrian Feldman 				continue;
231ac3cd520SBrian Feldman 		}
2329b50d902SRodney W. Grimes 		if ((ep = calloc(1, sizeof(struct entry))) == NULL)
23390389da9SPhilippe Charnier 			errx(1, "calloc");
2349b50d902SRodney W. Grimes 		*nextp = ep;
235ac3cd520SBrian Feldman 		nextp = &ep->next;
236ac3cd520SBrian Feldman 		memmove(&ep->utmp, &utmp, sizeof(struct utmp));
2379b50d902SRodney W. Grimes 		ep->tdev = stp->st_rdev;
2389b50d902SRodney W. Grimes #ifdef CPU_CONSDEV
2399b50d902SRodney W. Grimes 		/*
2409b50d902SRodney W. Grimes 		 * If this is the console device, attempt to ascertain
2419b50d902SRodney W. Grimes 		 * the true console device dev_t.
2429b50d902SRodney W. Grimes 		 */
2439b50d902SRodney W. Grimes 		if (ep->tdev == 0) {
2449b50d902SRodney W. Grimes 			int mib[2];
2459b50d902SRodney W. Grimes 			size_t size;
2469b50d902SRodney W. Grimes 
2479b50d902SRodney W. Grimes 			mib[0] = CTL_MACHDEP;
2489b50d902SRodney W. Grimes 			mib[1] = CPU_CONSDEV;
2499b50d902SRodney W. Grimes 			size = sizeof(dev_t);
2509b50d902SRodney W. Grimes 			(void)sysctl(mib, 2, &ep->tdev, &size, NULL, 0);
2519b50d902SRodney W. Grimes 		}
2529b50d902SRodney W. Grimes #endif
2533bebe991SBrian Somers 		touched = stp->st_atime;
2543bebe991SBrian Somers 		if (touched < ep->utmp.ut_time) {
2553bebe991SBrian Somers 			/* tty untouched since before login */
2563bebe991SBrian Somers 			touched = ep->utmp.ut_time;
2573bebe991SBrian Somers 		}
2583bebe991SBrian Somers 		if ((ep->idle = now - touched) < 0)
2599b50d902SRodney W. Grimes 			ep->idle = 0;
2609b50d902SRodney W. Grimes 	}
2619b50d902SRodney W. Grimes 	(void)fclose(ut);
2629b50d902SRodney W. Grimes 
2639b50d902SRodney W. Grimes 	if (header || wcmd == 0) {
2649b50d902SRodney W. Grimes 		pr_header(&now, nusers);
2659b50d902SRodney W. Grimes 		if (wcmd == 0)
2669b50d902SRodney W. Grimes 			exit(0);
2679b50d902SRodney W. Grimes 
268a88d7a82SJoseph Koshy #define HEADER_USER		"USER"
269a88d7a82SJoseph Koshy #define HEADER_TTY		"TTY"
270a88d7a82SJoseph Koshy #define HEADER_FROM		"FROM"
271a88d7a82SJoseph Koshy #define HEADER_LOGIN_IDLE	"LOGIN@  IDLE "
272a88d7a82SJoseph Koshy #define HEADER_WHAT		"WHAT\n"
273a88d7a82SJoseph Koshy #define WUSED  (UT_NAMESIZE + UT_LINESIZE + UT_HOSTSIZE + \
274a88d7a82SJoseph Koshy 		sizeof(HEADER_LOGIN_IDLE) + 3)	/* header width incl. spaces */
275a88d7a82SJoseph Koshy 		(void)printf("%-*.*s %-*.*s %-*.*s  %s",
276a88d7a82SJoseph Koshy 				UT_NAMESIZE, UT_NAMESIZE, HEADER_USER,
277a88d7a82SJoseph Koshy 				UT_LINESIZE, UT_LINESIZE, HEADER_TTY,
278a88d7a82SJoseph Koshy 				UT_HOSTSIZE, UT_HOSTSIZE, HEADER_FROM,
279a88d7a82SJoseph Koshy 				HEADER_LOGIN_IDLE HEADER_WHAT);
2800812a2b4SPeter Wemm 	}
2819b50d902SRodney W. Grimes 
2829b50d902SRodney W. Grimes 	if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL)
2839b50d902SRodney W. Grimes 		err(1, "%s", kvm_geterr(kd));
2849b50d902SRodney W. Grimes 	for (i = 0; i < nentries; i++, kp++) {
2851f7d2501SKirk McKusick 		if (kp->ki_stat == SIDL || kp->ki_stat == SZOMB)
2869b50d902SRodney W. Grimes 			continue;
2879b50d902SRodney W. Grimes 		for (ep = ehead; ep != NULL; ep = ep->next) {
2881f7d2501SKirk McKusick 			if (ep->tdev == kp->ki_tdev) {
2899b50d902SRodney W. Grimes 				/*
2901bb32253SAndrey A. Chernov 				 * proc is associated with this terminal
2911bb32253SAndrey A. Chernov 				 */
2921f7d2501SKirk McKusick 				if (ep->kp == NULL && kp->ki_pgid == kp->ki_tpgid) {
2931bb32253SAndrey A. Chernov 					/*
2941bb32253SAndrey A. Chernov 					 * Proc is 'most interesting'
2959b50d902SRodney W. Grimes 					 */
2961f7d2501SKirk McKusick 					if (proc_compare(ep->kp, kp))
2979b50d902SRodney W. Grimes 						ep->kp = kp;
2981bb32253SAndrey A. Chernov 				}
2991bb32253SAndrey A. Chernov 				/*
3001bb32253SAndrey A. Chernov 				 * Proc debug option info; add to debug
3011f7d2501SKirk McKusick 				 * list using kinfo_proc ki_spare[0]
3021bb32253SAndrey A. Chernov 				 * as next pointer; ptr to ptr avoids the
3031bb32253SAndrey A. Chernov 				 * ptr = long assumption.
3041bb32253SAndrey A. Chernov 				 */
3051bb32253SAndrey A. Chernov 				dkp = ep->dkp;
3061bb32253SAndrey A. Chernov 				ep->dkp = kp;
307ac3cd520SBrian Feldman 				debugproc(kp) = dkp;
3089b50d902SRodney W. Grimes 			}
3099b50d902SRodney W. Grimes 		}
3109b50d902SRodney W. Grimes 	}
3119b50d902SRodney W. Grimes 	if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&
3129b50d902SRodney W. Grimes 	     ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 &&
3139b50d902SRodney W. Grimes 	     ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0)
3149b50d902SRodney W. Grimes 	       ttywidth = 79;
3159b50d902SRodney W. Grimes         else
3169b50d902SRodney W. Grimes 	       ttywidth = ws.ws_col - 1;
3179b50d902SRodney W. Grimes 	argwidth = ttywidth - WUSED;
3189b50d902SRodney W. Grimes 	if (argwidth < 4)
3199b50d902SRodney W. Grimes 		argwidth = 8;
3209b50d902SRodney W. Grimes 	for (ep = ehead; ep != NULL; ep = ep->next) {
3219b50d902SRodney W. Grimes 		if (ep->kp == NULL) {
3229b50d902SRodney W. Grimes 			ep->args = "-";
3239b50d902SRodney W. Grimes 			continue;
3249b50d902SRodney W. Grimes 		}
3259b50d902SRodney W. Grimes 		ep->args = fmt_argv(kvm_getargv(kd, ep->kp, argwidth),
3261f7d2501SKirk McKusick 		    ep->kp->ki_comm, MAXCOMLEN);
3279b50d902SRodney W. Grimes 		if (ep->args == NULL)
3289b50d902SRodney W. Grimes 			err(1, NULL);
3299b50d902SRodney W. Grimes 	}
3309b50d902SRodney W. Grimes 	/* sort by idle time */
3319b50d902SRodney W. Grimes 	if (sortidle && ehead != NULL) {
332ac3cd520SBrian Feldman 		struct entry *from, *save;
3339b50d902SRodney W. Grimes 
334ac3cd520SBrian Feldman 		from = ehead;
3359b50d902SRodney W. Grimes 		ehead = NULL;
3369b50d902SRodney W. Grimes 		while (from != NULL) {
3379b50d902SRodney W. Grimes 			for (nextp = &ehead;
3389b50d902SRodney W. Grimes 			    (*nextp) && from->idle >= (*nextp)->idle;
3399b50d902SRodney W. Grimes 			    nextp = &(*nextp)->next)
3409b50d902SRodney W. Grimes 				continue;
3419b50d902SRodney W. Grimes 			save = from;
3429b50d902SRodney W. Grimes 			from = from->next;
3439b50d902SRodney W. Grimes 			save->next = *nextp;
3449b50d902SRodney W. Grimes 			*nextp = save;
3459b50d902SRodney W. Grimes 		}
3469b50d902SRodney W. Grimes 	}
3479b50d902SRodney W. Grimes 
3489ef5c48bSBill Fumerola 	if (!nflag) {
3499b50d902SRodney W. Grimes 		if (gethostname(domain, sizeof(domain) - 1) < 0 ||
350ac3cd520SBrian Feldman 		    (p = strchr(domain, '.')) == NULL)
3519b50d902SRodney W. Grimes 			domain[0] = '\0';
3529b50d902SRodney W. Grimes 		else {
3539b50d902SRodney W. Grimes 			domain[sizeof(domain) - 1] = '\0';
3549b50d902SRodney W. Grimes 			memmove(domain, p, strlen(p) + 1);
3559b50d902SRodney W. Grimes 		}
3569ef5c48bSBill Fumerola 	}
3579b50d902SRodney W. Grimes 
3589b50d902SRodney W. Grimes 	for (ep = ehead; ep != NULL; ep = ep->next) {
3599587fac0SAndrey A. Chernov 		char host_buf[UT_HOSTSIZE + 1];
3609587fac0SAndrey A. Chernov 
3619587fac0SAndrey A. Chernov 		host_buf[UT_HOSTSIZE] = '\0';
3629587fac0SAndrey A. Chernov 		strncpy(host_buf, ep->utmp.ut_host, UT_HOSTSIZE);
3639587fac0SAndrey A. Chernov 		p = *host_buf ? host_buf : "-";
3649b50d902SRodney W. Grimes 		if ((x = strchr(p, ':')) != NULL)
3659b50d902SRodney W. Grimes 			*x++ = '\0';
366ac3cd520SBrian Feldman 		if (!nflag && isdigit(*p) && (l = inet_addr(p)) != -1 &&
3679b50d902SRodney W. Grimes 		    (hp = gethostbyaddr((char *)&l, sizeof(l), AF_INET))) {
3689b50d902SRodney W. Grimes 			if (domain[0] != '\0') {
3699b50d902SRodney W. Grimes 				p = hp->h_name;
3709b50d902SRodney W. Grimes 				p += strlen(hp->h_name);
3719b50d902SRodney W. Grimes 				p -= strlen(domain);
3723b93c743SBrian Somers 				if (p > hp->h_name &&
3733b93c743SBrian Somers 				    strcasecmp(p, domain) == 0)
3749b50d902SRodney W. Grimes 					*p = '\0';
3759b50d902SRodney W. Grimes 			}
3769b50d902SRodney W. Grimes 			p = hp->h_name;
3779b50d902SRodney W. Grimes 		}
37869b41093SPeter Wemm 		if (nflag && *p && strcmp(p, "-") &&
37969b41093SPeter Wemm 		    inet_addr(p) == INADDR_NONE) {
38066b12529SAndrey A. Chernov 			hp = gethostbyname(p);
38166b12529SAndrey A. Chernov 
38266b12529SAndrey A. Chernov 			if (hp != NULL) {
38366b12529SAndrey A. Chernov 				struct in_addr in;
38466b12529SAndrey A. Chernov 
38566b12529SAndrey A. Chernov 				memmove(&in, hp->h_addr, sizeof(in));
38666b12529SAndrey A. Chernov 				p = inet_ntoa(in);
387c54481c1SAndrey A. Chernov 			}
38866b12529SAndrey A. Chernov 		}
3899b50d902SRodney W. Grimes 		if (x) {
3909587fac0SAndrey A. Chernov 			(void)snprintf(buf, sizeof(buf), "%s:%s", p, x);
3919b50d902SRodney W. Grimes 			p = buf;
3929b50d902SRodney W. Grimes 		}
3931bb32253SAndrey A. Chernov 		if (dflag) {
394ac3cd520SBrian Feldman 			for (dkp = ep->dkp; dkp != NULL; dkp = debugproc(dkp)) {
3950cadb9caSBrian Somers 				char *ptr;
396ac3cd520SBrian Feldman 
3970cadb9caSBrian Somers 				ptr = fmt_argv(kvm_getargv(kd, dkp, argwidth),
3981f7d2501SKirk McKusick 				    dkp->ki_comm, MAXCOMLEN);
3990cadb9caSBrian Somers 				if (ptr == NULL)
4000cadb9caSBrian Somers 					ptr = "-";
401ac3cd520SBrian Feldman 				(void)printf("\t\t%-9d %s\n",
4021f7d2501SKirk McKusick 				    dkp->ki_pid, ptr);
4031bb32253SAndrey A. Chernov 			}
4041bb32253SAndrey A. Chernov 		}
4053d65e4d1SPoul-Henning Kamp 		(void)printf("%-*.*s %-*.*s %-*.*s ",
4069b50d902SRodney W. Grimes 		    UT_NAMESIZE, UT_NAMESIZE, ep->utmp.ut_name,
4073d65e4d1SPoul-Henning Kamp 		    UT_LINESIZE, UT_LINESIZE,
408255318a8SAndrey A. Chernov 		    strncmp(ep->utmp.ut_line, "tty", 3) &&
409255318a8SAndrey A. Chernov 		    strncmp(ep->utmp.ut_line, "cua", 3) ?
4109b50d902SRodney W. Grimes 		    ep->utmp.ut_line : ep->utmp.ut_line + 3,
4119b50d902SRodney W. Grimes 		    UT_HOSTSIZE, UT_HOSTSIZE, *p ? p : "-");
4129b50d902SRodney W. Grimes 		pr_attime(&ep->utmp.ut_time, &now);
41370498d5aSDaniel O'Callaghan 		longidle = pr_idle(ep->idle);
41469b41093SPeter Wemm 		(void)printf("%.*s\n", argwidth - longidle, ep->args);
4159b50d902SRodney W. Grimes 	}
4169b50d902SRodney W. Grimes 	exit(0);
4179b50d902SRodney W. Grimes }
4189b50d902SRodney W. Grimes 
4199b50d902SRodney W. Grimes static void
4209b50d902SRodney W. Grimes pr_header(nowp, nusers)
4219b50d902SRodney W. Grimes 	time_t *nowp;
4229b50d902SRodney W. Grimes 	int nusers;
4239b50d902SRodney W. Grimes {
4249b50d902SRodney W. Grimes 	double avenrun[3];
4259b50d902SRodney W. Grimes 	time_t uptime;
4269d8c91b7SAndrey A. Chernov 	int days, hrs, i, mins, secs;
4279b50d902SRodney W. Grimes 	int mib[2];
4289b50d902SRodney W. Grimes 	size_t size;
4299b50d902SRodney W. Grimes 	char buf[256];
4309b50d902SRodney W. Grimes 
4319b50d902SRodney W. Grimes 	/*
4329b50d902SRodney W. Grimes 	 * Print time of day.
4339b50d902SRodney W. Grimes 	 */
43400034645SAndrey A. Chernov 	(void)strftime(buf, sizeof(buf)	- 1,
43500034645SAndrey A. Chernov 		       use_ampm	? "%l:%M%p" : "%k:%M", localtime(nowp));
4366d7977a7SKazutaka YOKOTA 	buf[sizeof(buf) - 1] = '\0';
4379b50d902SRodney W. Grimes 	(void)printf("%s ", buf);
4389b50d902SRodney W. Grimes 
4399b50d902SRodney W. Grimes 	/*
4409b50d902SRodney W. Grimes 	 * Print how long system has been up.
4419b50d902SRodney W. Grimes 	 * (Found by looking getting "boottime" from the kernel)
4429b50d902SRodney W. Grimes 	 */
4439b50d902SRodney W. Grimes 	mib[0] = CTL_KERN;
4449b50d902SRodney W. Grimes 	mib[1] = KERN_BOOTTIME;
4459b50d902SRodney W. Grimes 	size = sizeof(boottime);
4469b50d902SRodney W. Grimes 	if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
4479b50d902SRodney W. Grimes 	    boottime.tv_sec != 0) {
4489b50d902SRodney W. Grimes 		uptime = now - boottime.tv_sec;
4499b50d902SRodney W. Grimes 		uptime += 30;
450656dcd43SGarrett Wollman 		days = uptime / 86400;
451656dcd43SGarrett Wollman 		uptime %= 86400;
452656dcd43SGarrett Wollman 		hrs = uptime / 3600;
453656dcd43SGarrett Wollman 		uptime %= 3600;
454656dcd43SGarrett Wollman 		mins = uptime / 60;
4559d8c91b7SAndrey A. Chernov 		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:");
479ac3cd520SBrian Feldman 		for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++)
480ac3cd520SBrian Feldman 			(void)printf("%s %.2f", i > 0 ? "," : "", avenrun[i]);
4819b50d902SRodney W. Grimes 		(void)printf("\n");
4829b50d902SRodney W. Grimes 	}
4839b50d902SRodney W. Grimes }
4849b50d902SRodney W. Grimes 
4859b50d902SRodney W. Grimes static struct stat *
4860cadb9caSBrian Somers ttystat(line, sz)
4879b50d902SRodney W. Grimes 	char *line;
4880cadb9caSBrian Somers 	int sz;
4899b50d902SRodney W. Grimes {
4909b50d902SRodney W. Grimes 	static struct stat sb;
4919b50d902SRodney W. Grimes 	char ttybuf[MAXPATHLEN];
4929b50d902SRodney W. Grimes 
4930cadb9caSBrian Somers 	(void)snprintf(ttybuf, sizeof(ttybuf), "%s%.*s", _PATH_DEV, sz, line);
494bc53c0a6SJoseph Koshy 	if (stat(ttybuf, &sb)) {
495bc53c0a6SJoseph Koshy 		warn("%s", ttybuf);
496ac3cd520SBrian Feldman 		return (NULL);
497bc53c0a6SJoseph Koshy 	}
4989b50d902SRodney W. Grimes 	return (&sb);
4999b50d902SRodney W. Grimes }
5009b50d902SRodney W. Grimes 
5019b50d902SRodney W. Grimes static void
5029b50d902SRodney W. Grimes usage(wcmd)
5039b50d902SRodney W. Grimes 	int wcmd;
5049b50d902SRodney W. Grimes {
5059b50d902SRodney W. Grimes 	if (wcmd)
5069b50d902SRodney W. Grimes 		(void)fprintf(stderr,
507ac3cd520SBrian Feldman 		    "usage: w [-dhin] [-M core] [-N system] [user ...]\n");
5089b50d902SRodney W. Grimes 	else
509ac3cd520SBrian Feldman 		(void)fprintf(stderr, "usage: uptime\n");
5109b50d902SRodney W. Grimes 	exit(1);
5119b50d902SRodney W. Grimes }
512a4f33f43SMatthew Dillon 
513a4f33f43SMatthew Dillon static int
514a4f33f43SMatthew Dillon this_is_uptime(s)
515a4f33f43SMatthew Dillon 	const char *s;
516a4f33f43SMatthew Dillon {
517a4f33f43SMatthew Dillon 	const char *u;
518a4f33f43SMatthew Dillon 
519a4f33f43SMatthew Dillon 	if ((u = strrchr(s, '/')) != NULL)
520a4f33f43SMatthew Dillon 		++u;
521a4f33f43SMatthew Dillon 	else
522a4f33f43SMatthew Dillon 		u = s;
523a4f33f43SMatthew Dillon 	if (strcmp(u, "uptime") == 0)
524a4f33f43SMatthew Dillon 		return (0);
525a4f33f43SMatthew Dillon 	return (-1);
526a4f33f43SMatthew Dillon }
527