memcpy(&(ep->utmp), &utmp, sizeof(struct utmp));
memcpy(&(ep->utmp), &utmp, sizeof(utmp));
		*ttystat __P((char *));static void	 usage __P((int));intmain(argc, argv)	int argc;	char **argv;{	extern char *__progname;	struct kinfo_proc *kp;	struct hostent *hp;	struct stat *stp;	FILE *ut;	struct in_addr addr;	int ch, i, nentries, nusers, wcmd;	char *memf, *nlistf, *p, *x;	char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX];	/* Are we w(1) or uptime(1)? */	p = __progname;	if (*p == '-')		p++;	if (p[0] == 'w' && p[1] == '\0') {		wcmd = 1;		p = "hiflM:N:asuw";	} else if (!strcmp(p, "uptime")) {		wcmd = 0;		p = "M:N:";	} else		errx(1,		 "this program should be invoked only as \"w\" or \"uptime\"");	memf = nlistf = NULL;	while ((ch = getopt(argc, argv, p)) != -1)		switch (ch) {		case 'h':			header = 0;			break;		case 'i':			sortidle = 1;			break;		case 'M':			header = 0;			memf = optarg;			break;		case 'N':			nlistf = optarg;			break;		case 'a':			nflag = 0;			break;		case 'f': case 'l': case 's': case 'u': case 'w':			warnx("[-flsuw] no longer supported");			/* FALLTHROUGH */		case '?':		default:			usage(wcmd);		}	argc -= optind;	argv += optind;	/*	 * Discard setgid privileges if not the running kernel so that bad	 * guys can't print interesting stuff from kernel memory.	 */	if (nlistf != NULL || memf != NULL) {		setegid(getgid());		setgid(getgid());	}	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL)		errx(1, "%s", errbuf);	setegid(getgid());	setgid(getgid());	(void)time(&now);	if ((ut = fopen(_PATH_UTMP, "r")) == NULL)		err(1, "%s", _PATH_UTMP);	if (*argv)		sel_user = *argv;	for (nusers = 0; fread(&utmp, sizeof(utmp), 1, ut);) {		if (utmp.ut_name[0] == '\0')			continue;		++nusers;		if (wcmd == 0 || (sel_user &&		    strncmp(utmp.ut_name, sel_user, UT_NAMESIZE) != 0))			continue;		if ((ep = calloc(1, sizeof(struct entry))) == NULL)			err(1, NULL);		*nextp = ep;		nextp = &(ep->next);		memcpy(&(ep->utmp), &utmp, sizeof(struct utmp));		if (!(stp = ttystat(ep->utmp.ut_line)))			continue;		ep->tdev = stp->st_rdev;#ifdef CPU_CONSDEV		/*		 * If this is the console device, attempt to ascertain		 * the true console device dev_t.		 */		if (ep->tdev == 0) {			int mib[2];			size_t size;			mib[0] = CTL_MACHDEP;			mib[1] = CPU_CONSDEV;			size = sizeof(dev_t);			(void) sysctl(mib, 2, &ep->tdev, &size, NULL, 0);		}#endif		if ((ep->idle = now - stp->st_atime) < 0)			ep->idle = 0;	}	(void)fclose(ut);	if (header || wcmd == 0) {		pr_header(&now, nusers);		if (wcmd == 0)			exit (0);	}#define HEADER	"USER    TTY FROM              LOGIN@  IDLE WHAT"#define WUSED	(sizeof (HEADER) - sizeof ("WHAT"))	(void)puts(HEADER);	if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL)		errx(1, "%s", kvm_geterr(kd));	for (i = 0; i < nentries; i++, kp++) {		struct proc *p = &kp->kp_proc;		struct eproc *e;		if (p->p_stat == SIDL || p->p_stat == SZOMB)			continue;		e = &kp->kp_eproc;		for (ep = ehead; ep != NULL; ep = ep->next) {			/* ftp is a special case. */			if (strncmp(ep->utmp.ut_line, "ftp", 3) == 0) {				char pidstr[UT_LINESIZE-2];				pid_t fp;				(void)strncpy(pidstr, &ep->utmp.ut_line[3],				    sizeof(pidstr) - 1);				pidstr[sizeof(pidstr) - 1] = '\0';				fp = (pid_t)strtol(pidstr, NULL, 10);				if (p->p_pid == fp) {					ep->kp = kp;					break;				}			} else if (ep->tdev == e->e_tdev				   && e->e_pgid == e->e_tpgid) {				/*				 * Proc is in foreground of this terminal				 */				if (proc_compare(&ep->kp->kp_proc, p))					ep->kp = kp;				break;			}		}	}	if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&	     ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 &&	     ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0)	       ttywidth = 79;        else	       ttywidth = ws.ws_col - 1;	argwidth = ttywidth - WUSED;	if (argwidth < 4)		argwidth = 8;	/* sort by idle time */	if (sortidle && ehead != NULL) {		struct entry *from = ehead, *save;				ehead = NULL;		while (from != NULL) {			for (nextp = &ehead;			    (*nextp) && from->idle >= (*nextp)->idle;			    nextp = &(*nextp)->next)				continue;			save = from;			from = from->next;			save->next = *nextp;			*nextp = save;		}	}				if (!nflag)		if (gethostname(domain, sizeof(domain) - 1) < 0 ||		    (p = strchr(domain, '.')) == 0)			domain[0] = '\0';		else {			domain[sizeof(domain) - 1] = '\0';			memmove(domain, p, strlen(p) + 1);		}	for (ep = ehead; ep != NULL; ep = ep->next) {		p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-";		for (x = NULL, i = 0; p[i] != '\0' && i < UT_HOSTSIZE; i++)			if (p[i] == ':') {				x = &p[i];				*x++ = '\0';				break;			}		if (!nflag && inet_aton(p, &addr) &&		    (hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET))) {			if (domain[0] != '\0') {				p = hp->h_name;				p += strlen(hp->h_name);				p -= strlen(domain);				if (p > hp->h_name &&				    strcasecmp(p, domain) == 0)					*p = '\0';			}			p = hp->h_name;		}		if (x) {			(void)snprintf(buf, sizeof(buf), "%s:%.*s", p,			    ep->utmp.ut_host + UT_HOSTSIZE - x, x);			p = buf;		}		(void)printf("%-*.*s %-2.2s %-*.*s ",		    NAME_WIDTH, UT_NAMESIZE, ep->utmp.ut_name,		    strncmp(ep->utmp.ut_line, "tty", 3) ?		    ep->utmp.ut_line : ep->utmp.ut_line + 3,		    HOST_WIDTH, HOST_WIDTH, *p ? p : "-");		pr_attime(&ep->utmp.ut_time, &now);		pr_idle(ep->idle);		pr_args(ep->kp);		printf("\n");	}	exit(0);}static voidpr_args(kp)	struct kinfo_proc *kp;{	char **argv, *str;	int left;	if (kp == NULL)		goto nothing;	left = argwidth;	argv = kvm_getargv(kd, kp, argwidth+60);  /* +60 for ftpd snip */	if (argv == NULL)		goto nothing;	if (*argv == NULL || **argv == '\0') {		/* Process has zeroed argv[0], display executable name. */		fmt_putc('(', &left);		fmt_puts(kp->kp_proc.p_comm, &left);		fmt_putc(')', &left);	}	while (*argv) {		/* ftp is a special case... */		if (strncmp(*argv, "ftpd:", 5) == 0) {			str = strrchr(*argv, ':');			if (str != (char *)NULL) {				if ((str[0] == ':') && isspace(str[1]))					str += 2;				fmt_puts(str, &left);			} else				fmt_puts(*argv, &left);		} else			fmt_puts(*argv, &left);		argv++;		fmt_putc(' ', &left);	}	return;nothing:	putchar('-');}static voidpr_header(nowp, nusers)	time_t *nowp;	int nusers;{	double avenrun[3];	time_t uptime;	int days, hrs, i, mins;	int mib[2];	size_t size;	char buf[256];	/*	 * Print time of day.	 *	 * SCCS forces the string manipulation below, as it replaces	 * %, M, and % in a character string with the file name.	 */	(void)strftime(buf, sizeof(buf) - 1,	    __CONCAT("%l:%","M%p"), localtime(nowp));	buf[sizeof buf -1] = '\0';	(void)printf("%s ", buf);	/*	 * Print how long system has been up.	 * (Found by looking getting "boottime" from the kernel)	 */	mib[0] = CTL_KERN;	mib[1] = KERN_BOOTTIME;	size = sizeof(boottime);	if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1) {		uptime = now - boottime.tv_sec;		if (uptime > 59) {			uptime += 30;			days = uptime / SECSPERDAY;			uptime %= SECSPERDAY;			hrs = uptime / SECSPERHOUR;			uptime %= SECSPERHOUR;			mins = uptime / SECSPERMIN;			(void)printf(" up");			if (days > 0)				(void)printf(" %d day%s,", days,				    days > 1 ? "s" : "");			if (hrs > 0 && mins > 0)				(void)printf(" %2d:%02d,", hrs, mins);			else {				if (hrs > 0)					(void)printf(" %d hr%s,",					    hrs, hrs > 1 ? "s" : "");				if (mins > 0 || (days == 0 && hrs == 0))					(void)printf(" %d min%s,",					    mins, mins != 1 ? "s" : "");			}		} else			printf(" %d secs,", uptime);	}	/* Print number of users logged in to system */	(void)printf(" %d user%s", nusers, nusers != 1 ? "s" : "");	/*	 * Print 1, 5, and 15 minute load averages.	 */	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1)		(void)printf(", no load average information available\n");	else {		(void)printf(", load averages:");		for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) {			if (i > 0)				(void)printf(",");			(void)printf(" %.2f", avenrun[i]);		}		(void)printf("\n");	}}static struct stat *ttystat(line)	char *line;{	static struct stat sb;	char ttybuf[sizeof(_PATH_DEV) + UT_LINESIZE];	/* Note, line may not be NUL-terminated */	(void)strcpy(ttybuf, _PATH_DEV);	(void)strncpy(ttybuf + sizeof(_PATH_DEV) - 1, line, UT_LINESIZE);	ttybuf[sizeof(ttybuf) - 1] = '\0';	if (stat(ttybuf, &sb))		return (NULL);	return (&sb);}