Home | History | Annotate | Line # | Download | only in w
w.c revision 1.38
      1  1.38    simonb /*	$NetBSD: w.c,v 1.38 2000/07/13 14:28:07 simonb Exp $	*/
      2  1.18   thorpej 
      3   1.1       cgd /*-
      4  1.11       cgd  * Copyright (c) 1980, 1991, 1993, 1994
      5  1.11       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36  1.26       mrg #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38  1.26       mrg __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
     39  1.26       mrg 	The Regents of the University of California.  All rights reserved.\n");
     40   1.1       cgd #endif /* not lint */
     41   1.1       cgd 
     42   1.1       cgd #ifndef lint
     43  1.18   thorpej #if 0
     44  1.19       tls static char sccsid[] = "@(#)w.c	8.6 (Berkeley) 6/30/94";
     45  1.18   thorpej #else
     46  1.38    simonb __RCSID("$NetBSD: w.c,v 1.38 2000/07/13 14:28:07 simonb Exp $");
     47  1.18   thorpej #endif
     48   1.1       cgd #endif /* not lint */
     49   1.1       cgd 
     50   1.1       cgd /*
     51   1.1       cgd  * w - print system status (who and what)
     52   1.1       cgd  *
     53   1.1       cgd  * This program is similar to the systat command on Tenex/Tops 10/20
     54   1.1       cgd  *
     55   1.1       cgd  */
     56   1.1       cgd #include <sys/param.h>
     57   1.1       cgd #include <sys/time.h>
     58   1.1       cgd #include <sys/stat.h>
     59  1.11       cgd #include <sys/sysctl.h>
     60   1.1       cgd #include <sys/proc.h>
     61   1.1       cgd #include <sys/user.h>
     62   1.1       cgd #include <sys/ioctl.h>
     63  1.11       cgd #include <sys/socket.h>
     64  1.11       cgd 
     65  1.11       cgd #include <netinet/in.h>
     66  1.11       cgd #include <arpa/inet.h>
     67  1.11       cgd 
     68  1.11       cgd #include <ctype.h>
     69  1.11       cgd #include <err.h>
     70  1.11       cgd #include <errno.h>
     71  1.11       cgd #include <fcntl.h>
     72  1.11       cgd #include <kvm.h>
     73  1.20  explorer #include <limits.h>
     74  1.11       cgd #include <netdb.h>
     75   1.1       cgd #include <nlist.h>
     76   1.1       cgd #include <paths.h>
     77  1.11       cgd #include <stdio.h>
     78  1.11       cgd #include <stdlib.h>
     79   1.1       cgd #include <string.h>
     80  1.28    kleink #include <time.h>
     81  1.11       cgd #include <tzfile.h>
     82  1.11       cgd #include <unistd.h>
     83  1.11       cgd #include <utmp.h>
     84   1.7       cgd #include <vis.h>
     85   1.1       cgd 
     86  1.11       cgd #include "extern.h"
     87   1.1       cgd 
     88  1.37    simonb #define	max(a,b)	(((a)>(b))?(a):(b))
     89  1.37    simonb 
     90  1.11       cgd struct timeval	boottime;
     91  1.11       cgd struct utmp	utmp;
     92  1.11       cgd struct winsize	ws;
     93  1.11       cgd kvm_t	       *kd;
     94  1.11       cgd time_t		now;		/* the current time of day */
     95  1.11       cgd time_t		uptime;		/* time of last reboot & elapsed time since */
     96  1.11       cgd int		ttywidth;	/* width of tty */
     97  1.11       cgd int		argwidth;	/* width of tty */
     98  1.11       cgd int		header = 1;	/* true if -h flag: don't print heading */
     99  1.11       cgd int		nflag;		/* true if -n flag: don't convert addrs */
    100  1.11       cgd int		sortidle;	/* sort bu idle time */
    101  1.11       cgd char	       *sel_user;	/* login of particular user selected */
    102  1.29       mrg char		domain[MAXHOSTNAMELEN + 1];
    103   1.1       cgd 
    104   1.1       cgd /*
    105  1.11       cgd  * One of these per active utmp entry.
    106   1.1       cgd  */
    107   1.1       cgd struct	entry {
    108   1.1       cgd 	struct	entry *next;
    109   1.1       cgd 	struct	utmp utmp;
    110  1.35    simonb 	dev_t	tdev;			/* dev_t of terminal */
    111  1.35    simonb 	time_t	idle;			/* idle time of terminal in seconds */
    112  1.35    simonb 	struct	kinfo_proc2 *kp;	/* `most interesting' proc */
    113   1.1       cgd } *ep, *ehead = NULL, **nextp = &ehead;
    114   1.1       cgd 
    115  1.35    simonb static void	 pr_args __P((struct kinfo_proc2 *));
    116  1.11       cgd static void	 pr_header __P((time_t *, int));
    117  1.11       cgd static struct stat
    118  1.11       cgd 		*ttystat __P((char *));
    119  1.11       cgd static void	 usage __P((int));
    120  1.26       mrg int	main __P((int, char **));
    121   1.1       cgd 
    122  1.11       cgd int
    123   1.1       cgd main(argc, argv)
    124   1.1       cgd 	int argc;
    125   1.1       cgd 	char **argv;
    126   1.1       cgd {
    127  1.11       cgd 	extern char *__progname;
    128  1.35    simonb 	struct kinfo_proc2 *kp;
    129  1.11       cgd 	struct hostent *hp;
    130  1.11       cgd 	struct stat *stp;
    131   1.1       cgd 	FILE *ut;
    132  1.24   mycroft 	struct in_addr l;
    133  1.37    simonb 	int ch, i, nentries, nusers, wcmd, lognamelen;
    134  1.15   mycroft 	char *memf, *nlistf, *p, *x;
    135  1.20  explorer 	char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX];
    136  1.11       cgd 
    137  1.11       cgd 	/* Are we w(1) or uptime(1)? */
    138  1.11       cgd 	p = __progname;
    139  1.11       cgd 	if (*p == '-')
    140  1.11       cgd 		p++;
    141  1.11       cgd 	if (*p == 'u') {
    142   1.1       cgd 		wcmd = 0;
    143  1.11       cgd 		p = "";
    144  1.11       cgd 	} else {
    145  1.11       cgd 		wcmd = 1;
    146  1.11       cgd 		p = "hiflM:N:nsuw";
    147  1.11       cgd 	}
    148   1.1       cgd 
    149  1.11       cgd 	memf = nlistf = NULL;
    150  1.27     lukem 	while ((ch = getopt(argc, argv, p)) != -1)
    151  1.11       cgd 		switch (ch) {
    152   1.1       cgd 		case 'h':
    153   1.1       cgd 			header = 0;
    154   1.1       cgd 			break;
    155   1.1       cgd 		case 'i':
    156  1.11       cgd 			sortidle = 1;
    157  1.11       cgd 			break;
    158  1.11       cgd 		case 'M':
    159  1.11       cgd 			header = 0;
    160  1.11       cgd 			memf = optarg;
    161  1.11       cgd 			break;
    162  1.11       cgd 		case 'N':
    163  1.11       cgd 			nlistf = optarg;
    164  1.11       cgd 			break;
    165  1.11       cgd 		case 'n':
    166  1.11       cgd 			nflag = 1;
    167   1.1       cgd 			break;
    168   1.1       cgd 		case 'f': case 'l': case 's': case 'u': case 'w':
    169  1.11       cgd 			warnx("[-flsuw] no longer supported");
    170  1.11       cgd 			/* FALLTHROUGH */
    171   1.1       cgd 		case '?':
    172   1.1       cgd 		default:
    173  1.11       cgd 			usage(wcmd);
    174   1.1       cgd 		}
    175   1.1       cgd 	argc -= optind;
    176   1.1       cgd 	argv += optind;
    177  1.18   thorpej 
    178  1.35    simonb 	if ((kd = kvm_openfiles(nlistf, memf, NULL,
    179  1.35    simonb 	    memf == NULL ? KVM_NO_FILES : O_RDONLY, errbuf)) == NULL)
    180  1.11       cgd 		errx(1, "%s", errbuf);
    181  1.30       mrg 
    182  1.11       cgd 	(void)time(&now);
    183  1.11       cgd 	if ((ut = fopen(_PATH_UTMP, "r")) == NULL)
    184  1.11       cgd 		err(1, "%s", _PATH_UTMP);
    185  1.11       cgd 
    186   1.1       cgd 	if (*argv)
    187   1.1       cgd 		sel_user = *argv;
    188   1.1       cgd 
    189  1.11       cgd 	for (nusers = 0; fread(&utmp, sizeof(utmp), 1, ut);) {
    190   1.1       cgd 		if (utmp.ut_name[0] == '\0')
    191   1.1       cgd 			continue;
    192  1.11       cgd 		++nusers;
    193  1.11       cgd 		if (wcmd == 0 || (sel_user &&
    194   1.1       cgd 		    strncmp(utmp.ut_name, sel_user, UT_NAMESIZE) != 0))
    195   1.1       cgd 			continue;
    196  1.11       cgd 		if ((ep = calloc(1, sizeof(struct entry))) == NULL)
    197  1.33  drochner 			err(1, NULL);
    198   1.1       cgd 		*nextp = ep;
    199   1.1       cgd 		nextp = &(ep->next);
    200  1.11       cgd 		memmove(&(ep->utmp), &utmp, sizeof(struct utmp));
    201  1.13       cgd 		if (!(stp = ttystat(ep->utmp.ut_line)))
    202  1.13       cgd 			continue;
    203   1.1       cgd 		ep->tdev = stp->st_rdev;
    204  1.11       cgd 		/*
    205  1.11       cgd 		 * If this is the console device, attempt to ascertain
    206  1.11       cgd 		 * the true console device dev_t.
    207  1.11       cgd 		 */
    208  1.11       cgd 		if (ep->tdev == 0) {
    209  1.11       cgd 			int mib[2];
    210  1.11       cgd 			size_t size;
    211  1.11       cgd 
    212  1.38    simonb 			mib[0] = CTL_KERN;
    213  1.38    simonb 			mib[1] = KERN_CONSDEV;
    214  1.11       cgd 			size = sizeof(dev_t);
    215  1.11       cgd 			(void) sysctl(mib, 2, &ep->tdev, &size, NULL, 0);
    216  1.11       cgd 		}
    217  1.11       cgd 		if ((ep->idle = now - stp->st_atime) < 0)
    218   1.1       cgd 			ep->idle = 0;
    219   1.1       cgd 	}
    220  1.11       cgd 	(void)fclose(ut);
    221   1.1       cgd 
    222   1.1       cgd 	if (header || wcmd == 0) {
    223  1.11       cgd 		pr_header(&now, nusers);
    224  1.11       cgd 		if (wcmd == 0)
    225  1.11       cgd 			exit (0);
    226  1.11       cgd 	}
    227   1.1       cgd 
    228  1.36     enami 	if ((kp = kvm_getproc2(kd, KERN_PROC_ALL, 0,
    229  1.36     enami 	    sizeof(struct kinfo_proc2), &nentries)) == NULL)
    230  1.17       jtc 		errx(1, "%s", kvm_geterr(kd));
    231  1.37    simonb 	lognamelen = 0;
    232  1.11       cgd 	for (i = 0; i < nentries; i++, kp++) {
    233   1.1       cgd 
    234  1.35    simonb 		if (kp->p_stat == SIDL || kp->p_stat == SZOMB)
    235   1.1       cgd 			continue;
    236   1.1       cgd 		for (ep = ehead; ep != NULL; ep = ep->next) {
    237  1.35    simonb 			if (ep->tdev == kp->p_tdev &&
    238  1.35    simonb 			    kp->p__pgid == kp->p_tpgid) {
    239   1.1       cgd 				/*
    240   1.1       cgd 				 * Proc is in foreground of this terminal
    241   1.1       cgd 				 */
    242  1.37    simonb 				if (proc_compare(ep->kp, kp)) {
    243  1.11       cgd 					ep->kp = kp;
    244  1.37    simonb 					lognamelen = max(lognamelen,
    245  1.37    simonb 					    strlen(kp->p_login));
    246  1.37    simonb 				}
    247   1.1       cgd 				break;
    248   1.1       cgd 			}
    249   1.1       cgd 		}
    250   1.1       cgd 	}
    251  1.37    simonb 
    252  1.37    simonb 	argwidth = printf("%-*sTTY %-*s %*s  IDLE WHAT\n",
    253  1.37    simonb 	    lognamelen, "USER", UT_HOSTSIZE, "FROM",
    254  1.37    simonb 	    7 /* "dddhhXm" */, "LOGIN@");
    255  1.37    simonb 	argwidth -= sizeof("WHAT\n");
    256  1.37    simonb 
    257  1.11       cgd 	if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&
    258  1.11       cgd 	     ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 &&
    259  1.11       cgd 	     ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0)
    260   1.1       cgd 	       ttywidth = 79;
    261   1.1       cgd         else
    262   1.1       cgd 	       ttywidth = ws.ws_col - 1;
    263  1.37    simonb 	argwidth = ttywidth - argwidth;
    264   1.1       cgd 	if (argwidth < 4)
    265   1.1       cgd 		argwidth = 8;
    266   1.1       cgd 	/* sort by idle time */
    267   1.1       cgd 	if (sortidle && ehead != NULL) {
    268   1.1       cgd 		struct entry *from = ehead, *save;
    269   1.1       cgd 
    270   1.1       cgd 		ehead = NULL;
    271   1.1       cgd 		while (from != NULL) {
    272  1.11       cgd 			for (nextp = &ehead;
    273   1.1       cgd 			    (*nextp) && from->idle >= (*nextp)->idle;
    274   1.1       cgd 			    nextp = &(*nextp)->next)
    275  1.11       cgd 				continue;
    276   1.1       cgd 			save = from;
    277   1.1       cgd 			from = from->next;
    278   1.1       cgd 			save->next = *nextp;
    279   1.1       cgd 			*nextp = save;
    280   1.1       cgd 		}
    281   1.1       cgd 	}
    282  1.11       cgd 
    283  1.29       mrg 	if (!nflag) {
    284  1.29       mrg 		int	rv;
    285  1.29       mrg 
    286  1.29       mrg 		rv = gethostname(domain, sizeof(domain));
    287  1.29       mrg 		domain[sizeof(domain) - 1] = '\0';
    288  1.29       mrg 		if (rv < 0 || (p = strchr(domain, '.')) == 0)
    289  1.11       cgd 			domain[0] = '\0';
    290  1.29       mrg 		else
    291  1.11       cgd 			memmove(domain, p, strlen(p) + 1);
    292  1.29       mrg 	}
    293  1.11       cgd 
    294   1.1       cgd 	for (ep = ehead; ep != NULL; ep = ep->next) {
    295  1.11       cgd 		p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-";
    296  1.21  christos 		for (x = p; x < p + UT_HOSTSIZE; x++)
    297  1.22  christos 			if (*x == '\0' || *x == ':')
    298  1.21  christos 				break;
    299  1.22  christos 		if (x == p + UT_HOSTSIZE || *x != ':')
    300  1.21  christos 			x = NULL;
    301  1.22  christos 		else
    302  1.22  christos 			*x++ = '\0';
    303  1.22  christos 
    304  1.24   mycroft 		if (!nflag && inet_aton(p, &l) &&
    305  1.11       cgd 		    (hp = gethostbyaddr((char *)&l, sizeof(l), AF_INET))) {
    306  1.11       cgd 			if (domain[0] != '\0') {
    307  1.11       cgd 				p = hp->h_name;
    308  1.11       cgd 				p += strlen(hp->h_name);
    309  1.11       cgd 				p -= strlen(domain);
    310  1.11       cgd 				if (p > hp->h_name && strcmp(p, domain) == 0)
    311  1.11       cgd 					*p = '\0';
    312  1.11       cgd 			}
    313  1.11       cgd 			p = hp->h_name;
    314  1.11       cgd 		}
    315  1.11       cgd 		if (x) {
    316  1.12       cgd 			(void)snprintf(buf, sizeof(buf), "%s:%.*s", p,
    317  1.26       mrg 			    (int)(ep->utmp.ut_host + UT_HOSTSIZE - x), x);
    318  1.11       cgd 			p = buf;
    319  1.11       cgd 		}
    320  1.37    simonb 		(void)printf("%-*s %-2.2s %-*.*s ",
    321  1.37    simonb 		    lognamelen, ep->kp->p_login,
    322  1.32       mrg 		    (strncmp(ep->utmp.ut_line, "tty", 3) &&
    323  1.32       mrg 		    strncmp(ep->utmp.ut_line, "dty", 3)) ?
    324  1.11       cgd 		    ep->utmp.ut_line : ep->utmp.ut_line + 3,
    325  1.11       cgd 		    UT_HOSTSIZE, UT_HOSTSIZE, *p ? p : "-");
    326  1.11       cgd 		pr_attime(&ep->utmp.ut_time, &now);
    327  1.11       cgd 		pr_idle(ep->idle);
    328  1.15   mycroft 		pr_args(ep->kp);
    329  1.37    simonb 		(void)printf("\n");
    330  1.15   mycroft 	}
    331  1.15   mycroft 	exit(0);
    332  1.15   mycroft }
    333  1.15   mycroft 
    334  1.15   mycroft static void
    335  1.15   mycroft pr_args(kp)
    336  1.35    simonb 	struct kinfo_proc2 *kp;
    337  1.15   mycroft {
    338  1.15   mycroft 	char **argv;
    339  1.15   mycroft 	int left;
    340  1.15   mycroft 
    341  1.16   mycroft 	if (kp == 0)
    342  1.16   mycroft 		goto nothing;
    343  1.15   mycroft 	left = argwidth;
    344  1.35    simonb 	argv = kvm_getargv2(kd, kp, argwidth);
    345  1.16   mycroft 	if (argv == 0)
    346  1.16   mycroft 		goto nothing;
    347  1.16   mycroft 	while (*argv) {
    348  1.16   mycroft 		fmt_puts(*argv, &left);
    349  1.16   mycroft 		argv++;
    350  1.16   mycroft 		fmt_putc(' ', &left);
    351   1.1       cgd 	}
    352  1.16   mycroft 	return;
    353  1.16   mycroft nothing:
    354  1.16   mycroft 	putchar('-');
    355   1.1       cgd }
    356   1.1       cgd 
    357  1.11       cgd static void
    358  1.11       cgd pr_header(nowp, nusers)
    359  1.11       cgd 	time_t *nowp;
    360  1.11       cgd 	int nusers;
    361   1.1       cgd {
    362  1.11       cgd 	double avenrun[3];
    363  1.11       cgd 	time_t uptime;
    364  1.11       cgd 	int days, hrs, i, mins;
    365  1.11       cgd 	int mib[2];
    366  1.11       cgd 	size_t size;
    367  1.11       cgd 	char buf[256];
    368   1.1       cgd 
    369  1.11       cgd 	/*
    370  1.11       cgd 	 * Print time of day.
    371  1.11       cgd 	 *
    372  1.11       cgd 	 * SCCS forces the string manipulation below, as it replaces
    373  1.11       cgd 	 * %, M, and % in a character string with the file name.
    374  1.11       cgd 	 */
    375  1.11       cgd 	(void)strftime(buf, sizeof(buf),
    376  1.11       cgd 	    __CONCAT("%l:%","M%p"), localtime(nowp));
    377  1.23       mrg 	buf[sizeof(buf) - 1] = '\0';
    378  1.11       cgd 	(void)printf("%s ", buf);
    379   1.1       cgd 
    380  1.11       cgd 	/*
    381  1.11       cgd 	 * Print how long system has been up.
    382  1.11       cgd 	 * (Found by looking getting "boottime" from the kernel)
    383  1.11       cgd 	 */
    384  1.11       cgd 	mib[0] = CTL_KERN;
    385  1.11       cgd 	mib[1] = KERN_BOOTTIME;
    386  1.11       cgd 	size = sizeof(boottime);
    387  1.11       cgd 	if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
    388  1.11       cgd 	    boottime.tv_sec != 0) {
    389  1.11       cgd 		uptime = now - boottime.tv_sec;
    390  1.11       cgd 		uptime += 30;
    391  1.25      phil 		if (uptime > SECSPERMIN) {
    392  1.25      phil 			days = uptime / SECSPERDAY;
    393  1.25      phil 			uptime %= SECSPERDAY;
    394  1.25      phil 			hrs = uptime / SECSPERHOUR;
    395  1.25      phil 			uptime %= SECSPERHOUR;
    396  1.25      phil 			mins = uptime / SECSPERMIN;
    397  1.25      phil 			(void)printf(" up");
    398  1.25      phil 			if (days > 0)
    399  1.25      phil 				(void)printf(" %d day%s,", days,
    400  1.25      phil 				    days > 1 ? "s" : "");
    401  1.25      phil 			if (hrs > 0 && mins > 0)
    402  1.25      phil 				(void)printf(" %2d:%02d,", hrs, mins);
    403  1.25      phil 			else {
    404  1.25      phil 				if (hrs > 0)
    405  1.25      phil 					(void)printf(" %d hr%s,",
    406  1.25      phil 					    hrs, hrs > 1 ? "s" : "");
    407  1.25      phil 				if (mins > 0)
    408  1.25      phil 					(void)printf(" %d min%s,",
    409  1.25      phil 					    mins, mins > 1 ? "s" : "");
    410  1.25      phil 			}
    411  1.11       cgd 		}
    412  1.11       cgd 	}
    413  1.11       cgd 
    414  1.11       cgd 	/* Print number of users logged in to system */
    415  1.14       cgd 	(void)printf(" %d user%s", nusers, nusers != 1 ? "s" : "");
    416  1.11       cgd 
    417  1.11       cgd 	/*
    418  1.11       cgd 	 * Print 1, 5, and 15 minute load averages.
    419  1.11       cgd 	 */
    420  1.11       cgd 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1)
    421  1.11       cgd 		(void)printf(", no load average information available\n");
    422  1.11       cgd 	else {
    423  1.11       cgd 		(void)printf(", load averages:");
    424  1.11       cgd 		for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) {
    425  1.11       cgd 			if (i > 0)
    426  1.11       cgd 				(void)printf(",");
    427  1.11       cgd 			(void)printf(" %.2f", avenrun[i]);
    428  1.11       cgd 		}
    429  1.11       cgd 		(void)printf("\n");
    430  1.11       cgd 	}
    431   1.1       cgd }
    432   1.1       cgd 
    433  1.11       cgd static struct stat *
    434  1.11       cgd ttystat(line)
    435  1.11       cgd 	char *line;
    436   1.1       cgd {
    437  1.11       cgd 	static struct stat sb;
    438  1.11       cgd 	char ttybuf[MAXPATHLEN];
    439   1.1       cgd 
    440  1.11       cgd 	(void)snprintf(ttybuf, sizeof(ttybuf), "%s/%s", _PATH_DEV, line);
    441  1.11       cgd 	if (stat(ttybuf, &sb))
    442  1.13       cgd 		return (NULL);
    443  1.11       cgd 	return (&sb);
    444   1.1       cgd }
    445   1.1       cgd 
    446  1.11       cgd static void
    447  1.11       cgd usage(wcmd)
    448  1.11       cgd 	int wcmd;
    449   1.1       cgd {
    450  1.11       cgd 	if (wcmd)
    451  1.11       cgd 		(void)fprintf(stderr,
    452  1.11       cgd 		    "usage: w: [-hin] [-M core] [-N system] [user]\n");
    453  1.11       cgd 	else
    454  1.11       cgd 		(void)fprintf(stderr, "uptime\n");
    455  1.29       mrg 	exit(1);
    456   1.1       cgd }
    457