xref: /freebsd/usr.bin/w/w.c (revision 255318a894151d22949adb1424bdb28f251d8d02)
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
359b50d902SRodney W. Grimes static 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
419b50d902SRodney W. Grimes static char sccsid[] = "@(#)w.c	8.4 (Berkeley) 4/16/94";
429b50d902SRodney W. Grimes #endif /* not lint */
439b50d902SRodney W. Grimes 
449b50d902SRodney W. Grimes /*
459b50d902SRodney W. Grimes  * w - print system status (who and what)
469b50d902SRodney W. Grimes  *
479b50d902SRodney W. Grimes  * This program is similar to the systat command on Tenex/Tops 10/20
489b50d902SRodney W. Grimes  *
499b50d902SRodney W. Grimes  */
509b50d902SRodney W. Grimes #include <sys/param.h>
519b50d902SRodney W. Grimes #include <sys/time.h>
529b50d902SRodney W. Grimes #include <sys/stat.h>
539b50d902SRodney W. Grimes #include <sys/sysctl.h>
549b50d902SRodney W. Grimes #include <sys/proc.h>
559b50d902SRodney W. Grimes #include <sys/user.h>
569b50d902SRodney W. Grimes #include <sys/ioctl.h>
579b50d902SRodney W. Grimes #include <sys/socket.h>
589b50d902SRodney W. Grimes #include <sys/tty.h>
599b50d902SRodney W. Grimes 
609b50d902SRodney W. Grimes #include <machine/cpu.h>
619b50d902SRodney W. Grimes #include <netinet/in.h>
629b50d902SRodney W. Grimes #include <arpa/inet.h>
639b50d902SRodney W. Grimes 
649b50d902SRodney W. Grimes #include <ctype.h>
659b50d902SRodney W. Grimes #include <err.h>
669b50d902SRodney W. Grimes #include <errno.h>
679b50d902SRodney W. Grimes #include <fcntl.h>
689b50d902SRodney W. Grimes #include <kvm.h>
699b50d902SRodney W. Grimes #include <netdb.h>
709b50d902SRodney W. Grimes #include <nlist.h>
719b50d902SRodney W. Grimes #include <paths.h>
729b50d902SRodney W. Grimes #include <stdio.h>
739b50d902SRodney W. Grimes #include <stdlib.h>
749b50d902SRodney W. Grimes #include <string.h>
759b50d902SRodney W. Grimes #include <tzfile.h>
769b50d902SRodney W. Grimes #include <unistd.h>
779b50d902SRodney W. Grimes #include <utmp.h>
789b50d902SRodney W. Grimes #include <vis.h>
799b50d902SRodney W. Grimes 
809b50d902SRodney W. Grimes #include "extern.h"
819b50d902SRodney W. Grimes 
829b50d902SRodney W. Grimes struct timeval	boottime;
839b50d902SRodney W. Grimes struct utmp	utmp;
849b50d902SRodney W. Grimes struct winsize	ws;
859b50d902SRodney W. Grimes kvm_t	       *kd;
869b50d902SRodney W. Grimes time_t		now;		/* the current time of day */
879b50d902SRodney W. Grimes time_t		uptime;		/* time of last reboot & elapsed time since */
889b50d902SRodney W. Grimes int		ttywidth;	/* width of tty */
899b50d902SRodney W. Grimes int		argwidth;	/* width of tty */
909b50d902SRodney W. Grimes int		header = 1;	/* true if -h flag: don't print heading */
919b50d902SRodney W. Grimes int		nflag;		/* true if -n flag: don't convert addrs */
929b50d902SRodney W. Grimes int		sortidle;	/* sort bu idle time */
939b50d902SRodney W. Grimes char	       *sel_user;	/* login of particular user selected */
949b50d902SRodney W. Grimes char		domain[MAXHOSTNAMELEN];
959b50d902SRodney W. Grimes 
969b50d902SRodney W. Grimes /*
979b50d902SRodney W. Grimes  * One of these per active utmp entry.
989b50d902SRodney W. Grimes  */
999b50d902SRodney W. Grimes struct	entry {
1009b50d902SRodney W. Grimes 	struct	entry *next;
1019b50d902SRodney W. Grimes 	struct	utmp utmp;
1029b50d902SRodney W. Grimes 	dev_t	tdev;		/* dev_t of terminal */
1039b50d902SRodney W. Grimes 	time_t	idle;		/* idle time of terminal in seconds */
1049b50d902SRodney W. Grimes 	struct	kinfo_proc *kp;	/* `most interesting' proc */
1059b50d902SRodney W. Grimes 	char	*args;		/* arg list of interesting process */
1069b50d902SRodney W. Grimes } *ep, *ehead = NULL, **nextp = &ehead;
1079b50d902SRodney W. Grimes 
1089b50d902SRodney W. Grimes static void	 pr_header __P((time_t *, int));
1099b50d902SRodney W. Grimes static struct stat
1109b50d902SRodney W. Grimes 		*ttystat __P((char *));
1119b50d902SRodney W. Grimes static void	 usage __P((int));
1129b50d902SRodney W. Grimes 
1139b50d902SRodney W. Grimes char *fmt_argv __P((char **, char *, int));	/* ../../bin/ps/fmt.c */
1149b50d902SRodney W. Grimes 
1159b50d902SRodney W. Grimes int
1169b50d902SRodney W. Grimes main(argc, argv)
1179b50d902SRodney W. Grimes 	int argc;
1189b50d902SRodney W. Grimes 	char **argv;
1199b50d902SRodney W. Grimes {
1209b50d902SRodney W. Grimes 	extern char *__progname;
1219b50d902SRodney W. Grimes 	struct kinfo_proc *kp;
1229b50d902SRodney W. Grimes 	struct hostent *hp;
1239b50d902SRodney W. Grimes 	struct stat *stp;
1249b50d902SRodney W. Grimes 	FILE *ut;
1259b50d902SRodney W. Grimes 	u_long l;
1269b50d902SRodney W. Grimes 	size_t arglen;
1279b50d902SRodney W. Grimes 	int ch, i, nentries, nusers, wcmd;
1289b50d902SRodney W. Grimes 	char *memf, *nlistf, *p, *vis_args, *x;
1299b50d902SRodney W. Grimes 	char buf[MAXHOSTNAMELEN], errbuf[256];
1309b50d902SRodney W. Grimes 
1319b50d902SRodney W. Grimes 	/* Are we w(1) or uptime(1)? */
1329b50d902SRodney W. Grimes 	p = __progname;
1339b50d902SRodney W. Grimes 	if (*p == '-')
1349b50d902SRodney W. Grimes 		p++;
1359b50d902SRodney W. Grimes 	if (*p == 'u') {
1369b50d902SRodney W. Grimes 		wcmd = 0;
1379b50d902SRodney W. Grimes 		p = "";
1389b50d902SRodney W. Grimes 	} else {
1399b50d902SRodney W. Grimes 		wcmd = 1;
1409b50d902SRodney W. Grimes 		p = "hiflM:N:nsuw";
1419b50d902SRodney W. Grimes 	}
1429b50d902SRodney W. Grimes 
1439b50d902SRodney W. Grimes 	memf = nlistf = NULL;
1449b50d902SRodney W. Grimes 	while ((ch = getopt(argc, argv, p)) != EOF)
1459b50d902SRodney W. Grimes 		switch (ch) {
1469b50d902SRodney W. Grimes 		case 'h':
1479b50d902SRodney W. Grimes 			header = 0;
1489b50d902SRodney W. Grimes 			break;
1499b50d902SRodney W. Grimes 		case 'i':
1509b50d902SRodney W. Grimes 			sortidle = 1;
1519b50d902SRodney W. Grimes 			break;
1529b50d902SRodney W. Grimes 		case 'M':
1539b50d902SRodney W. Grimes 			header = 0;
1549b50d902SRodney W. Grimes 			memf = optarg;
1559b50d902SRodney W. Grimes 			break;
1569b50d902SRodney W. Grimes 		case 'N':
1579b50d902SRodney W. Grimes 			nlistf = optarg;
1589b50d902SRodney W. Grimes 			break;
1599b50d902SRodney W. Grimes 		case 'n':
1609b50d902SRodney W. Grimes 			nflag = 1;
1619b50d902SRodney W. Grimes 			break;
1629b50d902SRodney W. Grimes 		case 'f': case 'l': case 's': case 'u': case 'w':
1639b50d902SRodney W. Grimes 			warnx("[-flsuw] no longer supported");
1649b50d902SRodney W. Grimes 			/* FALLTHROUGH */
1659b50d902SRodney W. Grimes 		case '?':
1669b50d902SRodney W. Grimes 		default:
1679b50d902SRodney W. Grimes 			usage(wcmd);
1689b50d902SRodney W. Grimes 		}
1699b50d902SRodney W. Grimes 	argc -= optind;
1709b50d902SRodney W. Grimes 	argv += optind;
1719b50d902SRodney W. Grimes 
1729b50d902SRodney W. Grimes 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL)
1739b50d902SRodney W. Grimes 		errx(1, "%s", errbuf);
1749b50d902SRodney W. Grimes 
1759b50d902SRodney W. Grimes 	(void)time(&now);
1769b50d902SRodney W. Grimes 	if ((ut = fopen(_PATH_UTMP, "r")) == NULL)
1779b50d902SRodney W. Grimes 		err(1, "%s", _PATH_UTMP);
1789b50d902SRodney W. Grimes 
1799b50d902SRodney W. Grimes 	if (*argv)
1809b50d902SRodney W. Grimes 		sel_user = *argv;
1819b50d902SRodney W. Grimes 
1829b50d902SRodney W. Grimes 	for (nusers = 0; fread(&utmp, sizeof(utmp), 1, ut);) {
1839b50d902SRodney W. Grimes 		if (utmp.ut_name[0] == '\0')
1849b50d902SRodney W. Grimes 			continue;
1859b50d902SRodney W. Grimes 		++nusers;
1869b50d902SRodney W. Grimes 		if (wcmd == 0 || (sel_user &&
1879b50d902SRodney W. Grimes 		    strncmp(utmp.ut_name, sel_user, UT_NAMESIZE) != 0))
1889b50d902SRodney W. Grimes 			continue;
1899b50d902SRodney W. Grimes 		if ((ep = calloc(1, sizeof(struct entry))) == NULL)
1909b50d902SRodney W. Grimes 			err(1, NULL);
1919b50d902SRodney W. Grimes 		*nextp = ep;
1929b50d902SRodney W. Grimes 		nextp = &(ep->next);
1939b50d902SRodney W. Grimes 		memmove(&(ep->utmp), &utmp, sizeof(struct utmp));
1949b50d902SRodney W. Grimes 		stp = ttystat(ep->utmp.ut_line);
1959b50d902SRodney W. Grimes 		ep->tdev = stp->st_rdev;
1969b50d902SRodney W. Grimes #ifdef CPU_CONSDEV
1979b50d902SRodney W. Grimes 		/*
1989b50d902SRodney W. Grimes 		 * If this is the console device, attempt to ascertain
1999b50d902SRodney W. Grimes 		 * the true console device dev_t.
2009b50d902SRodney W. Grimes 		 */
2019b50d902SRodney W. Grimes 		if (ep->tdev == 0) {
2029b50d902SRodney W. Grimes 			int mib[2];
2039b50d902SRodney W. Grimes 			size_t size;
2049b50d902SRodney W. Grimes 
2059b50d902SRodney W. Grimes 			mib[0] = CTL_MACHDEP;
2069b50d902SRodney W. Grimes 			mib[1] = CPU_CONSDEV;
2079b50d902SRodney W. Grimes 			size = sizeof(dev_t);
2089b50d902SRodney W. Grimes 			(void) sysctl(mib, 2, &ep->tdev, &size, NULL, 0);
2099b50d902SRodney W. Grimes 		}
2109b50d902SRodney W. Grimes #endif
2119b50d902SRodney W. Grimes 		if ((ep->idle = now - stp->st_atime) < 0)
2129b50d902SRodney W. Grimes 			ep->idle = 0;
2139b50d902SRodney W. Grimes 	}
2149b50d902SRodney W. Grimes 	(void)fclose(ut);
2159b50d902SRodney W. Grimes 
2169b50d902SRodney W. Grimes 	if (header || wcmd == 0) {
2179b50d902SRodney W. Grimes 		pr_header(&now, nusers);
2189b50d902SRodney W. Grimes 		if (wcmd == 0)
2199b50d902SRodney W. Grimes 			exit (0);
2209b50d902SRodney W. Grimes 	}
2219b50d902SRodney W. Grimes 
2229b50d902SRodney W. Grimes #define HEADER	"USER    TTY FROM              LOGIN@  IDLE WHAT\n"
2239b50d902SRodney W. Grimes #define WUSED	(sizeof (HEADER) - sizeof ("WHAT\n"))
2249b50d902SRodney W. Grimes 	(void)printf(HEADER);
2259b50d902SRodney W. Grimes 
2269b50d902SRodney W. Grimes 	if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL)
2279b50d902SRodney W. Grimes 		err(1, "%s", kvm_geterr(kd));
2289b50d902SRodney W. Grimes 	for (i = 0; i < nentries; i++, kp++) {
2299b50d902SRodney W. Grimes 		struct proc *p = &kp->kp_proc;
2309b50d902SRodney W. Grimes 		struct eproc *e;
2319b50d902SRodney W. Grimes 
2329b50d902SRodney W. Grimes 		if (p->p_stat == SIDL || p->p_stat == SZOMB)
2339b50d902SRodney W. Grimes 			continue;
2349b50d902SRodney W. Grimes 		e = &kp->kp_eproc;
2359b50d902SRodney W. Grimes 		for (ep = ehead; ep != NULL; ep = ep->next) {
2369b50d902SRodney W. Grimes 			if (ep->tdev == e->e_tdev && e->e_pgid == e->e_tpgid) {
2379b50d902SRodney W. Grimes 				/*
2389b50d902SRodney W. Grimes 				 * Proc is in foreground of this terminal
2399b50d902SRodney W. Grimes 				 */
2409b50d902SRodney W. Grimes 				if (proc_compare(&ep->kp->kp_proc, p))
2419b50d902SRodney W. Grimes 					ep->kp = kp;
2429b50d902SRodney W. Grimes 				break;
2439b50d902SRodney W. Grimes 			}
2449b50d902SRodney W. Grimes 		}
2459b50d902SRodney W. Grimes 	}
2469b50d902SRodney W. Grimes 	if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&
2479b50d902SRodney W. Grimes 	     ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 &&
2489b50d902SRodney W. Grimes 	     ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0)
2499b50d902SRodney W. Grimes 	       ttywidth = 79;
2509b50d902SRodney W. Grimes         else
2519b50d902SRodney W. Grimes 	       ttywidth = ws.ws_col - 1;
2529b50d902SRodney W. Grimes 	argwidth = ttywidth - WUSED;
2539b50d902SRodney W. Grimes 	if (argwidth < 4)
2549b50d902SRodney W. Grimes 		argwidth = 8;
2559b50d902SRodney W. Grimes 	for (ep = ehead; ep != NULL; ep = ep->next) {
2569b50d902SRodney W. Grimes 		if (ep->kp == NULL) {
2579b50d902SRodney W. Grimes 			ep->args = "-";
2589b50d902SRodney W. Grimes 			continue;
2599b50d902SRodney W. Grimes 		}
2609b50d902SRodney W. Grimes 		ep->args = fmt_argv(kvm_getargv(kd, ep->kp, argwidth),
2619b50d902SRodney W. Grimes 		    ep->kp->kp_proc.p_comm, MAXCOMLEN);
2629b50d902SRodney W. Grimes 		if (ep->args == NULL)
2639b50d902SRodney W. Grimes 			err(1, NULL);
2649b50d902SRodney W. Grimes 	}
2659b50d902SRodney W. Grimes 	/* sort by idle time */
2669b50d902SRodney W. Grimes 	if (sortidle && ehead != NULL) {
2679b50d902SRodney W. Grimes 		struct entry *from = ehead, *save;
2689b50d902SRodney W. Grimes 
2699b50d902SRodney W. Grimes 		ehead = NULL;
2709b50d902SRodney W. Grimes 		while (from != NULL) {
2719b50d902SRodney W. Grimes 			for (nextp = &ehead;
2729b50d902SRodney W. Grimes 			    (*nextp) && from->idle >= (*nextp)->idle;
2739b50d902SRodney W. Grimes 			    nextp = &(*nextp)->next)
2749b50d902SRodney W. Grimes 				continue;
2759b50d902SRodney W. Grimes 			save = from;
2769b50d902SRodney W. Grimes 			from = from->next;
2779b50d902SRodney W. Grimes 			save->next = *nextp;
2789b50d902SRodney W. Grimes 			*nextp = save;
2799b50d902SRodney W. Grimes 		}
2809b50d902SRodney W. Grimes 	}
2819b50d902SRodney W. Grimes 
2829b50d902SRodney W. Grimes 	if (!nflag)
2839b50d902SRodney W. Grimes 		if (gethostname(domain, sizeof(domain) - 1) < 0 ||
2849b50d902SRodney W. Grimes 		    (p = strchr(domain, '.')) == 0)
2859b50d902SRodney W. Grimes 			domain[0] = '\0';
2869b50d902SRodney W. Grimes 		else {
2879b50d902SRodney W. Grimes 			domain[sizeof(domain) - 1] = '\0';
2889b50d902SRodney W. Grimes 			memmove(domain, p, strlen(p) + 1);
2899b50d902SRodney W. Grimes 		}
2909b50d902SRodney W. Grimes 
2919b50d902SRodney W. Grimes 	if ((vis_args = malloc(argwidth * 4 + 1)) == NULL)
2929b50d902SRodney W. Grimes 		err(1, NULL);
2939b50d902SRodney W. Grimes 	for (ep = ehead; ep != NULL; ep = ep->next) {
2949b50d902SRodney W. Grimes 		p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-";
2959b50d902SRodney W. Grimes 		if ((x = strchr(p, ':')) != NULL)
2969b50d902SRodney W. Grimes 			*x++ = '\0';
2979b50d902SRodney W. Grimes 		if (!nflag && isdigit(*p) &&
2989b50d902SRodney W. Grimes 		    (long)(l = inet_addr(p)) != -1 &&
2999b50d902SRodney W. Grimes 		    (hp = gethostbyaddr((char *)&l, sizeof(l), AF_INET))) {
3009b50d902SRodney W. Grimes 			if (domain[0] != '\0') {
3019b50d902SRodney W. Grimes 				p = hp->h_name;
3029b50d902SRodney W. Grimes 				p += strlen(hp->h_name);
3039b50d902SRodney W. Grimes 				p -= strlen(domain);
3049b50d902SRodney W. Grimes 				if (p > hp->h_name && strcmp(p, domain) == 0)
3059b50d902SRodney W. Grimes 					*p = '\0';
3069b50d902SRodney W. Grimes 			}
3079b50d902SRodney W. Grimes 			p = hp->h_name;
3089b50d902SRodney W. Grimes 		}
3099b50d902SRodney W. Grimes 		if (x) {
3109b50d902SRodney W. Grimes 			(void)snprintf(buf, sizeof(buf), "%s:%s", p, x);
3119b50d902SRodney W. Grimes 			p = buf;
3129b50d902SRodney W. Grimes 		}
3139b50d902SRodney W. Grimes 		(void)printf("%-*.*s %-2.2s %-*.*s ",
3149b50d902SRodney W. Grimes 		    UT_NAMESIZE, UT_NAMESIZE, ep->utmp.ut_name,
315255318a8SAndrey A. Chernov 		    strncmp(ep->utmp.ut_line, "tty", 3) &&
316255318a8SAndrey A. Chernov 		    strncmp(ep->utmp.ut_line, "cua", 3) ?
3179b50d902SRodney W. Grimes 		    ep->utmp.ut_line : ep->utmp.ut_line + 3,
3189b50d902SRodney W. Grimes 		    UT_HOSTSIZE, UT_HOSTSIZE, *p ? p : "-");
3199b50d902SRodney W. Grimes 		pr_attime(&ep->utmp.ut_time, &now);
3209b50d902SRodney W. Grimes 		pr_idle(ep->idle);
3219b50d902SRodney W. Grimes 		if (ep->args != NULL) {
3229b50d902SRodney W. Grimes 			arglen = strlen(ep->args);
3239b50d902SRodney W. Grimes 			strvisx(vis_args, ep->args,
3249b50d902SRodney W. Grimes 			    arglen > argwidth ? argwidth : arglen,
3259b50d902SRodney W. Grimes 			    VIS_TAB | VIS_NL | VIS_NOSLASH);
3269b50d902SRodney W. Grimes 		}
3279b50d902SRodney W. Grimes 		(void)printf("%.*s\n", argwidth, ep->args);
3289b50d902SRodney W. Grimes 	}
3299b50d902SRodney W. Grimes 	exit(0);
3309b50d902SRodney W. Grimes }
3319b50d902SRodney W. Grimes 
3329b50d902SRodney W. Grimes static void
3339b50d902SRodney W. Grimes pr_header(nowp, nusers)
3349b50d902SRodney W. Grimes 	time_t *nowp;
3359b50d902SRodney W. Grimes 	int nusers;
3369b50d902SRodney W. Grimes {
3379b50d902SRodney W. Grimes 	double avenrun[3];
3389b50d902SRodney W. Grimes 	time_t uptime;
3399b50d902SRodney W. Grimes 	int days, hrs, i, mins;
3409b50d902SRodney W. Grimes 	int mib[2];
3419b50d902SRodney W. Grimes 	size_t size;
3429b50d902SRodney W. Grimes 	char buf[256];
3439b50d902SRodney W. Grimes 
3449b50d902SRodney W. Grimes 	/*
3459b50d902SRodney W. Grimes 	 * Print time of day.
3469b50d902SRodney W. Grimes 	 *
3479b50d902SRodney W. Grimes 	 * SCCS forces the string manipulation below, as it replaces
3489b50d902SRodney W. Grimes 	 * %, M, and % in a character string with the file name.
3499b50d902SRodney W. Grimes 	 */
3509b50d902SRodney W. Grimes 	(void)strftime(buf, sizeof(buf),
3519b50d902SRodney W. Grimes 	    __CONCAT("%l:%","M%p"), localtime(nowp));
3529b50d902SRodney W. Grimes 	(void)printf("%s ", buf);
3539b50d902SRodney W. Grimes 
3549b50d902SRodney W. Grimes 	/*
3559b50d902SRodney W. Grimes 	 * Print how long system has been up.
3569b50d902SRodney W. Grimes 	 * (Found by looking getting "boottime" from the kernel)
3579b50d902SRodney W. Grimes 	 */
3589b50d902SRodney W. Grimes 	mib[0] = CTL_KERN;
3599b50d902SRodney W. Grimes 	mib[1] = KERN_BOOTTIME;
3609b50d902SRodney W. Grimes 	size = sizeof(boottime);
3619b50d902SRodney W. Grimes 	if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
3629b50d902SRodney W. Grimes 	    boottime.tv_sec != 0) {
3639b50d902SRodney W. Grimes 		uptime = now - boottime.tv_sec;
3649b50d902SRodney W. Grimes 		uptime += 30;
3659b50d902SRodney W. Grimes 		days = uptime / SECSPERDAY;
3669b50d902SRodney W. Grimes 		uptime %= SECSPERDAY;
3679b50d902SRodney W. Grimes 		hrs = uptime / SECSPERHOUR;
3689b50d902SRodney W. Grimes 		uptime %= SECSPERHOUR;
3699b50d902SRodney W. Grimes 		mins = uptime / SECSPERMIN;
3709b50d902SRodney W. Grimes 		(void)printf(" up");
3719b50d902SRodney W. Grimes 		if (days > 0)
3729b50d902SRodney W. Grimes 			(void)printf(" %d day%s,", days, days > 1 ? "s" : "");
3739b50d902SRodney W. Grimes 		if (hrs > 0 && mins > 0)
3749b50d902SRodney W. Grimes 			(void)printf(" %2d:%02d,", hrs, mins);
3759b50d902SRodney W. Grimes 		else {
3769b50d902SRodney W. Grimes 			if (hrs > 0)
3779b50d902SRodney W. Grimes 				(void)printf(" %d hr%s,",
3789b50d902SRodney W. Grimes 				    hrs, hrs > 1 ? "s" : "");
3799b50d902SRodney W. Grimes 			if (mins > 0)
3809b50d902SRodney W. Grimes 				(void)printf(" %d min%s,",
3819b50d902SRodney W. Grimes 				    mins, mins > 1 ? "s" : "");
3829b50d902SRodney W. Grimes 		}
3839b50d902SRodney W. Grimes 	}
3849b50d902SRodney W. Grimes 
3859b50d902SRodney W. Grimes 	/* Print number of users logged in to system */
3869b50d902SRodney W. Grimes 	(void)printf(" %d user%s", nusers, nusers > 1 ? "s" : "");
3879b50d902SRodney W. Grimes 
3889b50d902SRodney W. Grimes 	/*
3899b50d902SRodney W. Grimes 	 * Print 1, 5, and 15 minute load averages.
3909b50d902SRodney W. Grimes 	 */
3919b50d902SRodney W. Grimes 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1)
3929b50d902SRodney W. Grimes 		(void)printf(", no load average information available\n");
3939b50d902SRodney W. Grimes 	else {
3949b50d902SRodney W. Grimes 		(void)printf(", load averages:");
3959b50d902SRodney W. Grimes 		for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) {
3969b50d902SRodney W. Grimes 			if (i > 0)
3979b50d902SRodney W. Grimes 				(void)printf(",");
3989b50d902SRodney W. Grimes 			(void)printf(" %.2f", avenrun[i]);
3999b50d902SRodney W. Grimes 		}
4009b50d902SRodney W. Grimes 		(void)printf("\n");
4019b50d902SRodney W. Grimes 	}
4029b50d902SRodney W. Grimes }
4039b50d902SRodney W. Grimes 
4049b50d902SRodney W. Grimes static struct stat *
4059b50d902SRodney W. Grimes ttystat(line)
4069b50d902SRodney W. Grimes 	char *line;
4079b50d902SRodney W. Grimes {
4089b50d902SRodney W. Grimes 	static struct stat sb;
4099b50d902SRodney W. Grimes 	char ttybuf[MAXPATHLEN];
4109b50d902SRodney W. Grimes 
4119b50d902SRodney W. Grimes 	(void)snprintf(ttybuf, sizeof(ttybuf), "%s/%s", _PATH_DEV, line);
4129b50d902SRodney W. Grimes 	if (stat(ttybuf, &sb))
4139b50d902SRodney W. Grimes 		err(1, "%s", ttybuf);
4149b50d902SRodney W. Grimes 	return (&sb);
4159b50d902SRodney W. Grimes }
4169b50d902SRodney W. Grimes 
4179b50d902SRodney W. Grimes static void
4189b50d902SRodney W. Grimes usage(wcmd)
4199b50d902SRodney W. Grimes 	int wcmd;
4209b50d902SRodney W. Grimes {
4219b50d902SRodney W. Grimes 	if (wcmd)
4229b50d902SRodney W. Grimes 		(void)fprintf(stderr,
4239b50d902SRodney W. Grimes 		    "usage: w: [-hin] [-M core] [-N system] [user]\n");
4249b50d902SRodney W. Grimes 	else
4259b50d902SRodney W. Grimes 		(void)fprintf(stderr, "uptime\n");
4269b50d902SRodney W. Grimes 	exit (1);
4279b50d902SRodney W. Grimes }
428