Home | History | Annotate | Line # | Download | only in w
w.c revision 1.46
      1  1.46       cgd /*	$NetBSD: w.c,v 1.46 2001/02/19 23:03:52 cgd 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.46       cgd __RCSID("$NetBSD: w.c,v 1.46 2001/02/19 23:03:52 cgd 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.42  jdolecek #include <sys/types.h>
     58   1.1       cgd #include <sys/time.h>
     59   1.1       cgd #include <sys/stat.h>
     60  1.11       cgd #include <sys/sysctl.h>
     61   1.1       cgd #include <sys/proc.h>
     62   1.1       cgd #include <sys/user.h>
     63   1.1       cgd #include <sys/ioctl.h>
     64  1.11       cgd #include <sys/socket.h>
     65  1.11       cgd 
     66  1.11       cgd #include <netinet/in.h>
     67  1.11       cgd #include <arpa/inet.h>
     68  1.11       cgd 
     69  1.11       cgd #include <ctype.h>
     70  1.11       cgd #include <err.h>
     71  1.11       cgd #include <errno.h>
     72  1.11       cgd #include <fcntl.h>
     73  1.11       cgd #include <kvm.h>
     74  1.20  explorer #include <limits.h>
     75  1.11       cgd #include <netdb.h>
     76   1.1       cgd #include <nlist.h>
     77   1.1       cgd #include <paths.h>
     78  1.11       cgd #include <stdio.h>
     79  1.11       cgd #include <stdlib.h>
     80   1.1       cgd #include <string.h>
     81  1.28    kleink #include <time.h>
     82  1.11       cgd #include <tzfile.h>
     83  1.11       cgd #include <unistd.h>
     84  1.11       cgd #include <utmp.h>
     85   1.7       cgd #include <vis.h>
     86   1.1       cgd 
     87  1.11       cgd #include "extern.h"
     88   1.1       cgd 
     89  1.37    simonb #define	max(a,b)	(((a)>(b))?(a):(b))
     90  1.37    simonb 
     91  1.11       cgd struct timeval	boottime;
     92  1.11       cgd struct utmp	utmp;
     93  1.11       cgd struct winsize	ws;
     94  1.11       cgd kvm_t	       *kd;
     95  1.11       cgd time_t		now;		/* the current time of day */
     96  1.11       cgd time_t		uptime;		/* time of last reboot & elapsed time since */
     97  1.11       cgd int		ttywidth;	/* width of tty */
     98  1.39     enami int		argwidth;	/* width of tty left to print process args */
     99  1.11       cgd int		header = 1;	/* true if -h flag: don't print heading */
    100  1.11       cgd int		nflag;		/* true if -n flag: don't convert addrs */
    101  1.11       cgd int		sortidle;	/* sort bu idle time */
    102  1.11       cgd char	       *sel_user;	/* login of particular user selected */
    103  1.29       mrg char		domain[MAXHOSTNAMELEN + 1];
    104   1.1       cgd 
    105   1.1       cgd /*
    106  1.11       cgd  * One of these per active utmp entry.
    107   1.1       cgd  */
    108   1.1       cgd struct	entry {
    109   1.1       cgd 	struct	entry *next;
    110   1.1       cgd 	struct	utmp utmp;
    111  1.35    simonb 	dev_t	tdev;			/* dev_t of terminal */
    112  1.35    simonb 	time_t	idle;			/* idle time of terminal in seconds */
    113  1.35    simonb 	struct	kinfo_proc2 *kp;	/* `most interesting' proc */
    114  1.42  jdolecek #ifdef SUPPORT_FTPD_UTMP
    115  1.42  jdolecek 	pid_t	ftpd_pid;		/* pid as extracted from ftpd's entry */
    116  1.42  jdolecek #endif
    117   1.1       cgd } *ep, *ehead = NULL, **nextp = &ehead;
    118   1.1       cgd 
    119  1.45       mjl static void	 pr_args(struct kinfo_proc2 *);
    120  1.45       mjl static void	 pr_header(time_t *, int);
    121  1.45       mjl static struct stat *ttystat(char *, size_t);
    122  1.45       mjl static void	 usage(int);
    123  1.45       mjl int	main(int, char **);
    124   1.1       cgd 
    125  1.11       cgd int
    126  1.45       mjl main(int argc, char **argv)
    127   1.1       cgd {
    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.44       mjl 	time_t touched;
    134  1.37    simonb 	int ch, i, nentries, nusers, wcmd, lognamelen;
    135  1.15   mycroft 	char *memf, *nlistf, *p, *x;
    136  1.46       cgd 	const char *progname;
    137  1.20  explorer 	char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX];
    138  1.11       cgd 
    139  1.11       cgd 	/* Are we w(1) or uptime(1)? */
    140  1.46       cgd 	progname = getprogname();
    141  1.46       cgd 	if (*progname == '-')
    142  1.46       cgd 		progname++;
    143  1.46       cgd 	if (*progname == 'u') {
    144   1.1       cgd 		wcmd = 0;
    145  1.11       cgd 		p = "";
    146  1.11       cgd 	} else {
    147  1.11       cgd 		wcmd = 1;
    148  1.11       cgd 		p = "hiflM:N:nsuw";
    149  1.11       cgd 	}
    150   1.1       cgd 
    151  1.11       cgd 	memf = nlistf = NULL;
    152  1.27     lukem 	while ((ch = getopt(argc, argv, p)) != -1)
    153  1.11       cgd 		switch (ch) {
    154   1.1       cgd 		case 'h':
    155   1.1       cgd 			header = 0;
    156   1.1       cgd 			break;
    157   1.1       cgd 		case 'i':
    158  1.11       cgd 			sortidle = 1;
    159  1.11       cgd 			break;
    160  1.11       cgd 		case 'M':
    161  1.11       cgd 			header = 0;
    162  1.11       cgd 			memf = optarg;
    163  1.11       cgd 			break;
    164  1.11       cgd 		case 'N':
    165  1.11       cgd 			nlistf = optarg;
    166  1.11       cgd 			break;
    167  1.11       cgd 		case 'n':
    168  1.11       cgd 			nflag = 1;
    169   1.1       cgd 			break;
    170   1.1       cgd 		case 'f': case 'l': case 's': case 'u': case 'w':
    171  1.11       cgd 			warnx("[-flsuw] no longer supported");
    172  1.11       cgd 			/* FALLTHROUGH */
    173   1.1       cgd 		case '?':
    174   1.1       cgd 		default:
    175  1.11       cgd 			usage(wcmd);
    176   1.1       cgd 		}
    177   1.1       cgd 	argc -= optind;
    178   1.1       cgd 	argv += optind;
    179  1.18   thorpej 
    180  1.35    simonb 	if ((kd = kvm_openfiles(nlistf, memf, NULL,
    181  1.35    simonb 	    memf == NULL ? KVM_NO_FILES : O_RDONLY, errbuf)) == NULL)
    182  1.11       cgd 		errx(1, "%s", errbuf);
    183  1.30       mrg 
    184  1.11       cgd 	(void)time(&now);
    185  1.41  christos 	if ((ut = fopen(_PATH_UTMP, "r")) == NULL && wcmd)
    186  1.11       cgd 		err(1, "%s", _PATH_UTMP);
    187  1.11       cgd 
    188   1.1       cgd 	if (*argv)
    189   1.1       cgd 		sel_user = *argv;
    190   1.1       cgd 
    191  1.41  christos 	for (nusers = 0; ut && fread(&utmp, sizeof(utmp), 1, ut);) {
    192   1.1       cgd 		if (utmp.ut_name[0] == '\0')
    193   1.1       cgd 			continue;
    194  1.11       cgd 		++nusers;
    195  1.11       cgd 		if (wcmd == 0 || (sel_user &&
    196   1.1       cgd 		    strncmp(utmp.ut_name, sel_user, UT_NAMESIZE) != 0))
    197   1.1       cgd 			continue;
    198  1.11       cgd 		if ((ep = calloc(1, sizeof(struct entry))) == NULL)
    199  1.33  drochner 			err(1, NULL);
    200   1.1       cgd 		*nextp = ep;
    201   1.1       cgd 		nextp = &(ep->next);
    202  1.11       cgd 		memmove(&(ep->utmp), &utmp, sizeof(struct utmp));
    203  1.44       mjl 		if (!(stp = ttystat(ep->utmp.ut_line, UT_LINESIZE))) {
    204  1.42  jdolecek #ifdef SUPPORT_FTPD_UTMP
    205  1.42  jdolecek 			/*
    206  1.42  jdolecek 			 * Hack to recognize and correctly parse
    207  1.42  jdolecek 			 * utmp entry made by ftpd. The "tty" used
    208  1.42  jdolecek 			 * by ftpd is not a real tty, just identifier in
    209  1.42  jdolecek 			 * form ftpPROCESS_ID. Pid parsed from the "tty name"
    210  1.42  jdolecek 			 * is used later to match corresponding process.
    211  1.42  jdolecek 			 */
    212  1.42  jdolecek 			if (strncmp(ep->utmp.ut_line, "ftp", 3) == 0)
    213  1.42  jdolecek 				ep->ftpd_pid =
    214  1.42  jdolecek 					strtol(ep->utmp.ut_line + 3, NULL, 10);
    215  1.42  jdolecek #endif /* SUPPORT_FTPD_UTMP */
    216  1.42  jdolecek 
    217  1.13       cgd 			continue;
    218  1.42  jdolecek 		}
    219   1.1       cgd 		ep->tdev = stp->st_rdev;
    220  1.11       cgd 		/*
    221  1.11       cgd 		 * If this is the console device, attempt to ascertain
    222  1.11       cgd 		 * the true console device dev_t.
    223  1.11       cgd 		 */
    224  1.11       cgd 		if (ep->tdev == 0) {
    225  1.11       cgd 			int mib[2];
    226  1.11       cgd 			size_t size;
    227  1.11       cgd 
    228  1.38    simonb 			mib[0] = CTL_KERN;
    229  1.38    simonb 			mib[1] = KERN_CONSDEV;
    230  1.11       cgd 			size = sizeof(dev_t);
    231  1.11       cgd 			(void) sysctl(mib, 2, &ep->tdev, &size, NULL, 0);
    232  1.11       cgd 		}
    233  1.44       mjl 
    234  1.44       mjl 		touched = stp->st_atime;
    235  1.44       mjl 		if (touched < ep->utmp.ut_time) {
    236  1.44       mjl 				/* tty untouched since before login */
    237  1.44       mjl 				touched = ep->utmp.ut_time;
    238  1.44       mjl 			}
    239  1.44       mjl 	if ((ep->idle = now - touched) < 0)
    240   1.1       cgd 			ep->idle = 0;
    241   1.1       cgd 	}
    242  1.41  christos 	if (ut)
    243  1.41  christos 		(void)fclose(ut);
    244  1.41  christos 	else
    245  1.41  christos 		nusers = 1;
    246   1.1       cgd 
    247   1.1       cgd 	if (header || wcmd == 0) {
    248  1.11       cgd 		pr_header(&now, nusers);
    249  1.11       cgd 		if (wcmd == 0)
    250  1.11       cgd 			exit (0);
    251  1.11       cgd 	}
    252   1.1       cgd 
    253  1.36     enami 	if ((kp = kvm_getproc2(kd, KERN_PROC_ALL, 0,
    254  1.36     enami 	    sizeof(struct kinfo_proc2), &nentries)) == NULL)
    255  1.17       jtc 		errx(1, "%s", kvm_geterr(kd));
    256  1.40    simonb 
    257  1.40    simonb 	/* Include trailing space because TTY header starts one column early. */
    258  1.40    simonb 	lognamelen = sizeof("USER ") - 1 /* NUL */;
    259  1.11       cgd 	for (i = 0; i < nentries; i++, kp++) {
    260   1.1       cgd 
    261  1.35    simonb 		if (kp->p_stat == SIDL || kp->p_stat == SZOMB)
    262   1.1       cgd 			continue;
    263  1.42  jdolecek 
    264   1.1       cgd 		for (ep = ehead; ep != NULL; ep = ep->next) {
    265  1.42  jdolecek 			if (ep->tdev == kp->p_tdev
    266  1.42  jdolecek 				&& kp->p__pgid == kp->p_tpgid) {
    267   1.1       cgd 				/*
    268   1.1       cgd 				 * Proc is in foreground of this terminal
    269   1.1       cgd 				 */
    270  1.37    simonb 				if (proc_compare(ep->kp, kp)) {
    271  1.11       cgd 					ep->kp = kp;
    272  1.37    simonb 					lognamelen = max(lognamelen,
    273  1.37    simonb 					    strlen(kp->p_login));
    274  1.37    simonb 				}
    275   1.1       cgd 				break;
    276   1.1       cgd 			}
    277  1.42  jdolecek #ifdef SUPPORT_FTPD_UTMP
    278  1.42  jdolecek 			/*
    279  1.42  jdolecek 			 * Hack to match process to ftp utmp entry.
    280  1.42  jdolecek 			 */
    281  1.42  jdolecek 			else if (ep->tdev == 0 && kp->p_tdev == NODEV
    282  1.42  jdolecek 				 && ep->ftpd_pid == kp->p_pid) {
    283  1.42  jdolecek 				ep->kp = kp;
    284  1.42  jdolecek 				lognamelen = max(lognamelen,
    285  1.42  jdolecek 					    strlen(kp->p_login));
    286  1.42  jdolecek 			}
    287  1.42  jdolecek #endif /* SUPPORT_FTPD_UTMP */
    288   1.1       cgd 		}
    289   1.1       cgd 	}
    290  1.37    simonb 
    291  1.40    simonb 	argwidth = printf("%-*sTTY %-*s %*s  IDLE WHAT\n",
    292  1.37    simonb 	    lognamelen, "USER", UT_HOSTSIZE, "FROM",
    293  1.37    simonb 	    7 /* "dddhhXm" */, "LOGIN@");
    294  1.39     enami 	argwidth -= sizeof("WHAT\n") - 1 /* NUL */;
    295  1.37    simonb 
    296  1.11       cgd 	if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&
    297  1.11       cgd 	     ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 &&
    298  1.11       cgd 	     ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0)
    299   1.1       cgd 	       ttywidth = 79;
    300   1.1       cgd         else
    301   1.1       cgd 	       ttywidth = ws.ws_col - 1;
    302  1.37    simonb 	argwidth = ttywidth - argwidth;
    303   1.1       cgd 	if (argwidth < 4)
    304   1.1       cgd 		argwidth = 8;
    305   1.1       cgd 	/* sort by idle time */
    306   1.1       cgd 	if (sortidle && ehead != NULL) {
    307   1.1       cgd 		struct entry *from = ehead, *save;
    308   1.1       cgd 
    309   1.1       cgd 		ehead = NULL;
    310   1.1       cgd 		while (from != NULL) {
    311  1.11       cgd 			for (nextp = &ehead;
    312   1.1       cgd 			    (*nextp) && from->idle >= (*nextp)->idle;
    313   1.1       cgd 			    nextp = &(*nextp)->next)
    314  1.11       cgd 				continue;
    315   1.1       cgd 			save = from;
    316   1.1       cgd 			from = from->next;
    317   1.1       cgd 			save->next = *nextp;
    318   1.1       cgd 			*nextp = save;
    319   1.1       cgd 		}
    320   1.1       cgd 	}
    321  1.11       cgd 
    322  1.29       mrg 	if (!nflag) {
    323  1.29       mrg 		int	rv;
    324  1.29       mrg 
    325  1.29       mrg 		rv = gethostname(domain, sizeof(domain));
    326  1.29       mrg 		domain[sizeof(domain) - 1] = '\0';
    327  1.29       mrg 		if (rv < 0 || (p = strchr(domain, '.')) == 0)
    328  1.11       cgd 			domain[0] = '\0';
    329  1.29       mrg 		else
    330  1.11       cgd 			memmove(domain, p, strlen(p) + 1);
    331  1.29       mrg 	}
    332  1.11       cgd 
    333   1.1       cgd 	for (ep = ehead; ep != NULL; ep = ep->next) {
    334  1.44       mjl 		char host_buf[UT_HOSTSIZE + 1];
    335  1.44       mjl 
    336  1.44       mjl 		host_buf[UT_HOSTSIZE] = '\0';
    337  1.44       mjl 		strncpy(host_buf, ep->utmp.ut_host, UT_HOSTSIZE);
    338  1.44       mjl 		p = *host_buf ? host_buf : "-";
    339  1.44       mjl 
    340  1.21  christos 		for (x = p; x < p + UT_HOSTSIZE; x++)
    341  1.22  christos 			if (*x == '\0' || *x == ':')
    342  1.21  christos 				break;
    343  1.22  christos 		if (x == p + UT_HOSTSIZE || *x != ':')
    344  1.21  christos 			x = NULL;
    345  1.22  christos 		else
    346  1.22  christos 			*x++ = '\0';
    347  1.22  christos 
    348  1.24   mycroft 		if (!nflag && inet_aton(p, &l) &&
    349  1.11       cgd 		    (hp = gethostbyaddr((char *)&l, sizeof(l), AF_INET))) {
    350  1.11       cgd 			if (domain[0] != '\0') {
    351  1.11       cgd 				p = hp->h_name;
    352  1.11       cgd 				p += strlen(hp->h_name);
    353  1.11       cgd 				p -= strlen(domain);
    354  1.44       mjl 				if (p > hp->h_name && strcasecmp(p, domain) == 0)
    355  1.11       cgd 					*p = '\0';
    356  1.11       cgd 			}
    357  1.11       cgd 			p = hp->h_name;
    358  1.11       cgd 		}
    359  1.11       cgd 		if (x) {
    360  1.44       mjl 			(void)snprintf(buf, sizeof(buf), "%s:%s", p, x);
    361  1.11       cgd 			p = buf;
    362  1.41  christos 		}
    363  1.41  christos 		if (ep->kp == NULL) {
    364  1.41  christos 		    warnx("Stale utmp entry: %.*s %.*s %.*s",
    365  1.41  christos 			UT_NAMESIZE, ep->utmp.ut_name,
    366  1.41  christos 			UT_LINESIZE, ep->utmp.ut_line,
    367  1.41  christos 			UT_HOSTSIZE, ep->utmp.ut_host);
    368  1.41  christos 		    continue;
    369  1.11       cgd 		}
    370  1.40    simonb 		(void)printf("%-*s %-2.2s %-*.*s ",
    371  1.37    simonb 		    lognamelen, ep->kp->p_login,
    372  1.32       mrg 		    (strncmp(ep->utmp.ut_line, "tty", 3) &&
    373  1.32       mrg 		    strncmp(ep->utmp.ut_line, "dty", 3)) ?
    374  1.11       cgd 		    ep->utmp.ut_line : ep->utmp.ut_line + 3,
    375  1.11       cgd 		    UT_HOSTSIZE, UT_HOSTSIZE, *p ? p : "-");
    376  1.11       cgd 		pr_attime(&ep->utmp.ut_time, &now);
    377  1.11       cgd 		pr_idle(ep->idle);
    378  1.15   mycroft 		pr_args(ep->kp);
    379  1.37    simonb 		(void)printf("\n");
    380  1.15   mycroft 	}
    381  1.15   mycroft 	exit(0);
    382  1.15   mycroft }
    383  1.15   mycroft 
    384  1.15   mycroft static void
    385  1.45       mjl pr_args(struct kinfo_proc2 *kp)
    386  1.15   mycroft {
    387  1.15   mycroft 	char **argv;
    388  1.15   mycroft 	int left;
    389  1.15   mycroft 
    390  1.16   mycroft 	if (kp == 0)
    391  1.16   mycroft 		goto nothing;
    392  1.15   mycroft 	left = argwidth;
    393  1.35    simonb 	argv = kvm_getargv2(kd, kp, argwidth);
    394  1.16   mycroft 	if (argv == 0)
    395  1.16   mycroft 		goto nothing;
    396  1.16   mycroft 	while (*argv) {
    397  1.16   mycroft 		fmt_puts(*argv, &left);
    398  1.16   mycroft 		argv++;
    399  1.16   mycroft 		fmt_putc(' ', &left);
    400   1.1       cgd 	}
    401  1.16   mycroft 	return;
    402  1.16   mycroft nothing:
    403  1.16   mycroft 	putchar('-');
    404   1.1       cgd }
    405   1.1       cgd 
    406  1.11       cgd static void
    407  1.45       mjl pr_header(time_t *nowp, int nusers)
    408   1.1       cgd {
    409  1.11       cgd 	double avenrun[3];
    410  1.11       cgd 	time_t uptime;
    411  1.11       cgd 	int days, hrs, i, mins;
    412  1.11       cgd 	int mib[2];
    413  1.11       cgd 	size_t size;
    414  1.11       cgd 	char buf[256];
    415   1.1       cgd 
    416  1.11       cgd 	/*
    417  1.11       cgd 	 * Print time of day.
    418  1.11       cgd 	 *
    419  1.11       cgd 	 * SCCS forces the string manipulation below, as it replaces
    420  1.11       cgd 	 * %, M, and % in a character string with the file name.
    421  1.11       cgd 	 */
    422  1.43       cgd 	(void)strftime(buf, sizeof(buf), "%l:%" "M%p", localtime(nowp));
    423  1.23       mrg 	buf[sizeof(buf) - 1] = '\0';
    424  1.11       cgd 	(void)printf("%s ", buf);
    425   1.1       cgd 
    426  1.11       cgd 	/*
    427  1.11       cgd 	 * Print how long system has been up.
    428  1.11       cgd 	 * (Found by looking getting "boottime" from the kernel)
    429  1.11       cgd 	 */
    430  1.11       cgd 	mib[0] = CTL_KERN;
    431  1.11       cgd 	mib[1] = KERN_BOOTTIME;
    432  1.11       cgd 	size = sizeof(boottime);
    433  1.11       cgd 	if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
    434  1.11       cgd 	    boottime.tv_sec != 0) {
    435  1.11       cgd 		uptime = now - boottime.tv_sec;
    436  1.11       cgd 		uptime += 30;
    437  1.25      phil 		if (uptime > SECSPERMIN) {
    438  1.25      phil 			days = uptime / SECSPERDAY;
    439  1.25      phil 			uptime %= SECSPERDAY;
    440  1.25      phil 			hrs = uptime / SECSPERHOUR;
    441  1.25      phil 			uptime %= SECSPERHOUR;
    442  1.25      phil 			mins = uptime / SECSPERMIN;
    443  1.25      phil 			(void)printf(" up");
    444  1.25      phil 			if (days > 0)
    445  1.25      phil 				(void)printf(" %d day%s,", days,
    446  1.25      phil 				    days > 1 ? "s" : "");
    447  1.25      phil 			if (hrs > 0 && mins > 0)
    448  1.25      phil 				(void)printf(" %2d:%02d,", hrs, mins);
    449  1.25      phil 			else {
    450  1.25      phil 				if (hrs > 0)
    451  1.25      phil 					(void)printf(" %d hr%s,",
    452  1.25      phil 					    hrs, hrs > 1 ? "s" : "");
    453  1.25      phil 				if (mins > 0)
    454  1.25      phil 					(void)printf(" %d min%s,",
    455  1.25      phil 					    mins, mins > 1 ? "s" : "");
    456  1.25      phil 			}
    457  1.11       cgd 		}
    458  1.11       cgd 	}
    459  1.11       cgd 
    460  1.11       cgd 	/* Print number of users logged in to system */
    461  1.14       cgd 	(void)printf(" %d user%s", nusers, nusers != 1 ? "s" : "");
    462  1.11       cgd 
    463  1.11       cgd 	/*
    464  1.11       cgd 	 * Print 1, 5, and 15 minute load averages.
    465  1.11       cgd 	 */
    466  1.11       cgd 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1)
    467  1.11       cgd 		(void)printf(", no load average information available\n");
    468  1.11       cgd 	else {
    469  1.11       cgd 		(void)printf(", load averages:");
    470  1.11       cgd 		for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) {
    471  1.11       cgd 			if (i > 0)
    472  1.11       cgd 				(void)printf(",");
    473  1.11       cgd 			(void)printf(" %.2f", avenrun[i]);
    474  1.11       cgd 		}
    475  1.11       cgd 		(void)printf("\n");
    476  1.11       cgd 	}
    477   1.1       cgd }
    478   1.1       cgd 
    479  1.11       cgd static struct stat *
    480  1.45       mjl ttystat(char *line, size_t sz)
    481   1.1       cgd {
    482  1.11       cgd 	static struct stat sb;
    483  1.11       cgd 	char ttybuf[MAXPATHLEN];
    484   1.1       cgd 
    485  1.44       mjl 	(void)snprintf(ttybuf, sizeof(ttybuf), "%s%.*s", _PATH_DEV, (int) sz, line);
    486  1.11       cgd 	if (stat(ttybuf, &sb))
    487  1.13       cgd 		return (NULL);
    488  1.11       cgd 	return (&sb);
    489   1.1       cgd }
    490   1.1       cgd 
    491  1.11       cgd static void
    492  1.45       mjl usage(int wcmd)
    493   1.1       cgd {
    494  1.11       cgd 	if (wcmd)
    495  1.11       cgd 		(void)fprintf(stderr,
    496  1.11       cgd 		    "usage: w: [-hin] [-M core] [-N system] [user]\n");
    497  1.11       cgd 	else
    498  1.11       cgd 		(void)fprintf(stderr, "uptime\n");
    499  1.29       mrg 	exit(1);
    500   1.1       cgd }
    501