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[] = 451bb32253SAndrey A. Chernov "$Id: w.c,v 1.21 1997/08/25 06:42:19 charnier Exp $"; 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> 739b50d902SRodney W. Grimes #include <netdb.h> 749b50d902SRodney W. Grimes #include <nlist.h> 759b50d902SRodney W. Grimes #include <paths.h> 769b50d902SRodney W. Grimes #include <stdio.h> 779b50d902SRodney W. Grimes #include <stdlib.h> 789b50d902SRodney W. Grimes #include <string.h> 799b50d902SRodney W. Grimes #include <unistd.h> 809b50d902SRodney W. Grimes #include <utmp.h> 819b50d902SRodney W. Grimes #include <vis.h> 82baf72ec1SAndrey A. Chernov #include <locale.h> 839b50d902SRodney W. Grimes 846367cd09SPeter Wemm #include <arpa/nameser.h> 856367cd09SPeter Wemm #include <resolv.h> 866367cd09SPeter Wemm 879b50d902SRodney W. Grimes #include "extern.h" 889b50d902SRodney W. Grimes 899b50d902SRodney W. Grimes struct timeval boottime; 909b50d902SRodney W. Grimes struct utmp utmp; 919b50d902SRodney W. Grimes struct winsize ws; 929b50d902SRodney W. Grimes kvm_t *kd; 939b50d902SRodney W. Grimes time_t now; /* the current time of day */ 949b50d902SRodney W. Grimes time_t uptime; /* time of last reboot & elapsed time since */ 959b50d902SRodney W. Grimes int ttywidth; /* width of tty */ 969b50d902SRodney W. Grimes int argwidth; /* width of tty */ 979b50d902SRodney W. Grimes int header = 1; /* true if -h flag: don't print heading */ 989b50d902SRodney W. Grimes int nflag; /* true if -n flag: don't convert addrs */ 991bb32253SAndrey A. Chernov int dflag; /* true if -d flag: output debug info */ 1009b50d902SRodney W. Grimes int sortidle; /* sort bu idle time */ 1019b50d902SRodney W. Grimes char *sel_user; /* login of particular user selected */ 1029b50d902SRodney W. Grimes char domain[MAXHOSTNAMELEN]; 1039b50d902SRodney W. Grimes 1049b50d902SRodney W. Grimes /* 1059b50d902SRodney W. Grimes * One of these per active utmp entry. 1069b50d902SRodney W. Grimes */ 1079b50d902SRodney W. Grimes struct entry { 1089b50d902SRodney W. Grimes struct entry *next; 1099b50d902SRodney W. Grimes struct utmp utmp; 1109b50d902SRodney W. Grimes dev_t tdev; /* dev_t of terminal */ 1119b50d902SRodney W. Grimes time_t idle; /* idle time of terminal in seconds */ 1129b50d902SRodney W. Grimes struct kinfo_proc *kp; /* `most interesting' proc */ 1139b50d902SRodney W. Grimes char *args; /* arg list of interesting process */ 1141bb32253SAndrey A. Chernov struct kinfo_proc *dkp; /* debug option proc list */ 1159b50d902SRodney W. Grimes } *ep, *ehead = NULL, **nextp = &ehead; 1169b50d902SRodney W. Grimes 1179b50d902SRodney W. Grimes static void pr_header __P((time_t *, int)); 1189b50d902SRodney W. Grimes static struct stat 1199b50d902SRodney W. Grimes *ttystat __P((char *)); 1209b50d902SRodney W. Grimes static void usage __P((int)); 1219b50d902SRodney W. Grimes 1229b50d902SRodney W. Grimes char *fmt_argv __P((char **, char *, int)); /* ../../bin/ps/fmt.c */ 1239b50d902SRodney W. Grimes 1249b50d902SRodney W. Grimes int 1259b50d902SRodney W. Grimes main(argc, argv) 1269b50d902SRodney W. Grimes int argc; 1279b50d902SRodney W. Grimes char **argv; 1289b50d902SRodney W. Grimes { 1299b50d902SRodney W. Grimes extern char *__progname; 1309b50d902SRodney W. Grimes struct kinfo_proc *kp; 1311bb32253SAndrey A. Chernov struct kinfo_proc *dkp; 1329b50d902SRodney W. Grimes struct hostent *hp; 1339b50d902SRodney W. Grimes struct stat *stp; 1349b50d902SRodney W. Grimes FILE *ut; 1359b50d902SRodney W. Grimes u_long l; 1369b50d902SRodney W. Grimes size_t arglen; 13770498d5aSDaniel O'Callaghan int ch, i, nentries, nusers, wcmd, longidle; 1389b50d902SRodney W. Grimes char *memf, *nlistf, *p, *vis_args, *x; 1399b50d902SRodney W. Grimes char buf[MAXHOSTNAMELEN], errbuf[256]; 1409b50d902SRodney W. Grimes 141d1b2ad1aSAndrey A. Chernov (void) setlocale(LC_ALL, ""); 142baf72ec1SAndrey A. Chernov 1439b50d902SRodney W. Grimes /* Are we w(1) or uptime(1)? */ 1449b50d902SRodney W. Grimes p = __progname; 1459b50d902SRodney W. Grimes if (*p == '-') 1469b50d902SRodney W. Grimes p++; 1479b50d902SRodney W. Grimes if (*p == 'u') { 1489b50d902SRodney W. Grimes wcmd = 0; 1499b50d902SRodney W. Grimes p = ""; 1509b50d902SRodney W. Grimes } else { 1519b50d902SRodney W. Grimes wcmd = 1; 1521bb32253SAndrey A. Chernov p = "dhiflM:N:nsuw"; 1539b50d902SRodney W. Grimes } 1549b50d902SRodney W. Grimes 1559b50d902SRodney W. Grimes memf = nlistf = NULL; 1561c8af878SWarner Losh while ((ch = getopt(argc, argv, p)) != -1) 1579b50d902SRodney W. Grimes switch (ch) { 1581bb32253SAndrey A. Chernov case 'd': 1591bb32253SAndrey A. Chernov dflag = 1; 1601bb32253SAndrey A. Chernov break; 1619b50d902SRodney W. Grimes case 'h': 1629b50d902SRodney W. Grimes header = 0; 1639b50d902SRodney W. Grimes break; 1649b50d902SRodney W. Grimes case 'i': 1659b50d902SRodney W. Grimes sortidle = 1; 1669b50d902SRodney W. Grimes break; 1679b50d902SRodney W. Grimes case 'M': 1689b50d902SRodney W. Grimes header = 0; 1699b50d902SRodney W. Grimes memf = optarg; 1709b50d902SRodney W. Grimes break; 1719b50d902SRodney W. Grimes case 'N': 1729b50d902SRodney W. Grimes nlistf = optarg; 1739b50d902SRodney W. Grimes break; 1749b50d902SRodney W. Grimes case 'n': 1759b50d902SRodney W. Grimes nflag = 1; 1769b50d902SRodney W. Grimes break; 1779b50d902SRodney W. Grimes case 'f': case 'l': case 's': case 'u': case 'w': 1789b50d902SRodney W. Grimes warnx("[-flsuw] no longer supported"); 1799b50d902SRodney W. Grimes /* FALLTHROUGH */ 1809b50d902SRodney W. Grimes case '?': 1819b50d902SRodney W. Grimes default: 1829b50d902SRodney W. Grimes usage(wcmd); 1839b50d902SRodney W. Grimes } 1849b50d902SRodney W. Grimes argc -= optind; 1859b50d902SRodney W. Grimes argv += optind; 1869b50d902SRodney W. Grimes 1876367cd09SPeter Wemm if (!(_res.options & RES_INIT)) 1886367cd09SPeter Wemm res_init(); 1896367cd09SPeter Wemm _res.retrans = 2; /* resolver timeout to 2 seconds per try */ 1906367cd09SPeter Wemm _res.retry = 1; /* only try once.. */ 1916367cd09SPeter Wemm 19266e5b18fSPaul Traina /* 19366e5b18fSPaul Traina * Discard setgid privileges if not the running kernel so that bad 19466e5b18fSPaul Traina * guys can't print interesting stuff from kernel memory. 19566e5b18fSPaul Traina */ 19666e5b18fSPaul Traina if (nlistf != NULL || memf != NULL) 19766e5b18fSPaul Traina setgid(getgid()); 19866e5b18fSPaul Traina 1999b50d902SRodney W. Grimes if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL) 2009b50d902SRodney W. Grimes errx(1, "%s", errbuf); 2019b50d902SRodney W. Grimes 2029b50d902SRodney W. Grimes (void)time(&now); 2039b50d902SRodney W. Grimes if ((ut = fopen(_PATH_UTMP, "r")) == NULL) 2049b50d902SRodney W. Grimes err(1, "%s", _PATH_UTMP); 2059b50d902SRodney W. Grimes 2069b50d902SRodney W. Grimes if (*argv) 2079b50d902SRodney W. Grimes sel_user = *argv; 2089b50d902SRodney W. Grimes 2099b50d902SRodney W. Grimes for (nusers = 0; fread(&utmp, sizeof(utmp), 1, ut);) { 2109b50d902SRodney W. Grimes if (utmp.ut_name[0] == '\0') 2119b50d902SRodney W. Grimes continue; 2129b50d902SRodney W. Grimes ++nusers; 2139b50d902SRodney W. Grimes if (wcmd == 0 || (sel_user && 2149b50d902SRodney W. Grimes strncmp(utmp.ut_name, sel_user, UT_NAMESIZE) != 0)) 2159b50d902SRodney W. Grimes continue; 2169b50d902SRodney W. Grimes if ((ep = calloc(1, sizeof(struct entry))) == NULL) 21790389da9SPhilippe Charnier errx(1, "calloc"); 2189b50d902SRodney W. Grimes *nextp = ep; 2199b50d902SRodney W. Grimes nextp = &(ep->next); 2209b50d902SRodney W. Grimes memmove(&(ep->utmp), &utmp, sizeof(struct utmp)); 2219b50d902SRodney W. Grimes stp = ttystat(ep->utmp.ut_line); 2229b50d902SRodney W. Grimes ep->tdev = stp->st_rdev; 2239b50d902SRodney W. Grimes #ifdef CPU_CONSDEV 2249b50d902SRodney W. Grimes /* 2259b50d902SRodney W. Grimes * If this is the console device, attempt to ascertain 2269b50d902SRodney W. Grimes * the true console device dev_t. 2279b50d902SRodney W. Grimes */ 2289b50d902SRodney W. Grimes if (ep->tdev == 0) { 2299b50d902SRodney W. Grimes int mib[2]; 2309b50d902SRodney W. Grimes size_t size; 2319b50d902SRodney W. Grimes 2329b50d902SRodney W. Grimes mib[0] = CTL_MACHDEP; 2339b50d902SRodney W. Grimes mib[1] = CPU_CONSDEV; 2349b50d902SRodney W. Grimes size = sizeof(dev_t); 2359b50d902SRodney W. Grimes (void) sysctl(mib, 2, &ep->tdev, &size, NULL, 0); 2369b50d902SRodney W. Grimes } 2379b50d902SRodney W. Grimes #endif 2389b50d902SRodney W. Grimes if ((ep->idle = now - stp->st_atime) < 0) 2399b50d902SRodney W. Grimes ep->idle = 0; 2409b50d902SRodney W. Grimes } 2419b50d902SRodney W. Grimes (void)fclose(ut); 2429b50d902SRodney W. Grimes 2439b50d902SRodney W. Grimes if (header || wcmd == 0) { 2449b50d902SRodney W. Grimes pr_header(&now, nusers); 2459b50d902SRodney W. Grimes if (wcmd == 0) 2469b50d902SRodney W. Grimes exit (0); 2479b50d902SRodney W. Grimes 2489b50d902SRodney W. Grimes #define HEADER "USER TTY FROM LOGIN@ IDLE WHAT\n" 2499b50d902SRodney W. Grimes #define WUSED (sizeof (HEADER) - sizeof ("WHAT\n")) 2509b50d902SRodney W. Grimes (void)printf(HEADER); 2510812a2b4SPeter Wemm } 2529b50d902SRodney W. Grimes 2539b50d902SRodney W. Grimes if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL) 2549b50d902SRodney W. Grimes err(1, "%s", kvm_geterr(kd)); 2559b50d902SRodney W. Grimes for (i = 0; i < nentries; i++, kp++) { 2569b50d902SRodney W. Grimes struct proc *p = &kp->kp_proc; 2579b50d902SRodney W. Grimes struct eproc *e; 2589b50d902SRodney W. Grimes 2599b50d902SRodney W. Grimes if (p->p_stat == SIDL || p->p_stat == SZOMB) 2609b50d902SRodney W. Grimes continue; 2619b50d902SRodney W. Grimes e = &kp->kp_eproc; 2629b50d902SRodney W. Grimes for (ep = ehead; ep != NULL; ep = ep->next) { 2631bb32253SAndrey A. Chernov if (ep->tdev == e->e_tdev) { 2649b50d902SRodney W. Grimes /* 2651bb32253SAndrey A. Chernov * proc is associated with this terminal 2661bb32253SAndrey A. Chernov */ 2671bb32253SAndrey A. Chernov if (ep->kp == NULL && e->e_pgid == e->e_tpgid) { 2681bb32253SAndrey A. Chernov /* 2691bb32253SAndrey A. Chernov * Proc is 'most interesting' 2709b50d902SRodney W. Grimes */ 2719b50d902SRodney W. Grimes if (proc_compare(&ep->kp->kp_proc, p)) 2729b50d902SRodney W. Grimes ep->kp = kp; 2731bb32253SAndrey A. Chernov } 2741bb32253SAndrey A. Chernov /* 2751bb32253SAndrey A. Chernov * Proc debug option info; add to debug 2761bb32253SAndrey A. Chernov * list using kinfo_proc kp_eproc.e_spare 2771bb32253SAndrey A. Chernov * as next pointer; ptr to ptr avoids the 2781bb32253SAndrey A. Chernov * ptr = long assumption. 2791bb32253SAndrey A. Chernov */ 2801bb32253SAndrey A. Chernov dkp = ep->dkp; 2811bb32253SAndrey A. Chernov ep->dkp = kp; 2821bb32253SAndrey A. Chernov *((struct kinfo_proc **)(&kp->kp_eproc.e_spare[ 0])) = dkp; 2839b50d902SRodney W. Grimes } 2849b50d902SRodney W. Grimes } 2859b50d902SRodney W. Grimes } 2869b50d902SRodney W. Grimes if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 && 2879b50d902SRodney W. Grimes ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 && 2889b50d902SRodney W. Grimes ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0) 2899b50d902SRodney W. Grimes ttywidth = 79; 2909b50d902SRodney W. Grimes else 2919b50d902SRodney W. Grimes ttywidth = ws.ws_col - 1; 2929b50d902SRodney W. Grimes argwidth = ttywidth - WUSED; 2939b50d902SRodney W. Grimes if (argwidth < 4) 2949b50d902SRodney W. Grimes argwidth = 8; 2959b50d902SRodney W. Grimes for (ep = ehead; ep != NULL; ep = ep->next) { 2969b50d902SRodney W. Grimes if (ep->kp == NULL) { 2979b50d902SRodney W. Grimes ep->args = "-"; 2989b50d902SRodney W. Grimes continue; 2999b50d902SRodney W. Grimes } 3009b50d902SRodney W. Grimes ep->args = fmt_argv(kvm_getargv(kd, ep->kp, argwidth), 3019b50d902SRodney W. Grimes ep->kp->kp_proc.p_comm, MAXCOMLEN); 3029b50d902SRodney W. Grimes if (ep->args == NULL) 3039b50d902SRodney W. Grimes err(1, NULL); 3049b50d902SRodney W. Grimes } 3059b50d902SRodney W. Grimes /* sort by idle time */ 3069b50d902SRodney W. Grimes if (sortidle && ehead != NULL) { 3079b50d902SRodney W. Grimes struct entry *from = ehead, *save; 3089b50d902SRodney W. Grimes 3099b50d902SRodney W. Grimes ehead = NULL; 3109b50d902SRodney W. Grimes while (from != NULL) { 3119b50d902SRodney W. Grimes for (nextp = &ehead; 3129b50d902SRodney W. Grimes (*nextp) && from->idle >= (*nextp)->idle; 3139b50d902SRodney W. Grimes nextp = &(*nextp)->next) 3149b50d902SRodney W. Grimes continue; 3159b50d902SRodney W. Grimes save = from; 3169b50d902SRodney W. Grimes from = from->next; 3179b50d902SRodney W. Grimes save->next = *nextp; 3189b50d902SRodney W. Grimes *nextp = save; 3199b50d902SRodney W. Grimes } 3209b50d902SRodney W. Grimes } 3219b50d902SRodney W. Grimes 3229b50d902SRodney W. Grimes if (!nflag) 3239b50d902SRodney W. Grimes if (gethostname(domain, sizeof(domain) - 1) < 0 || 3249b50d902SRodney W. Grimes (p = strchr(domain, '.')) == 0) 3259b50d902SRodney W. Grimes domain[0] = '\0'; 3269b50d902SRodney W. Grimes else { 3279b50d902SRodney W. Grimes domain[sizeof(domain) - 1] = '\0'; 3289b50d902SRodney W. Grimes memmove(domain, p, strlen(p) + 1); 3299b50d902SRodney W. Grimes } 3309b50d902SRodney W. Grimes 3319b50d902SRodney W. Grimes if ((vis_args = malloc(argwidth * 4 + 1)) == NULL) 33290389da9SPhilippe Charnier errx(1, "malloc"); 3339b50d902SRodney W. Grimes for (ep = ehead; ep != NULL; ep = ep->next) { 3349b50d902SRodney W. Grimes p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-"; 3359b50d902SRodney W. Grimes if ((x = strchr(p, ':')) != NULL) 3369b50d902SRodney W. Grimes *x++ = '\0'; 3379b50d902SRodney W. Grimes if (!nflag && isdigit(*p) && 3389b50d902SRodney W. Grimes (long)(l = inet_addr(p)) != -1 && 3399b50d902SRodney W. Grimes (hp = gethostbyaddr((char *)&l, sizeof(l), AF_INET))) { 3409b50d902SRodney W. Grimes if (domain[0] != '\0') { 3419b50d902SRodney W. Grimes p = hp->h_name; 3429b50d902SRodney W. Grimes p += strlen(hp->h_name); 3439b50d902SRodney W. Grimes p -= strlen(domain); 3449b50d902SRodney W. Grimes if (p > hp->h_name && strcmp(p, domain) == 0) 3459b50d902SRodney W. Grimes *p = '\0'; 3469b50d902SRodney W. Grimes } 3479b50d902SRodney W. Grimes p = hp->h_name; 3489b50d902SRodney W. Grimes } 3491694c5cbSAndrey A. Chernov if (nflag && *p && strcmp(p, "-") && inet_addr(p) == INADDR_NONE) { 35066b12529SAndrey A. Chernov hp = gethostbyname(p); 35166b12529SAndrey A. Chernov 35266b12529SAndrey A. Chernov if (hp != NULL) { 35366b12529SAndrey A. Chernov struct in_addr in; 35466b12529SAndrey A. Chernov 35566b12529SAndrey A. Chernov memmove(&in, hp->h_addr, sizeof(in)); 35666b12529SAndrey A. Chernov p = inet_ntoa(in); 357c54481c1SAndrey A. Chernov } 35866b12529SAndrey A. Chernov } 3599b50d902SRodney W. Grimes if (x) { 36092172c87SDavid Greenman (void)snprintf(buf, sizeof(buf), "%s:%.*s", p, 36192172c87SDavid Greenman ep->utmp.ut_host + UT_HOSTSIZE - x, x); 3629b50d902SRodney W. Grimes p = buf; 3639b50d902SRodney W. Grimes } 3641bb32253SAndrey A. Chernov if( dflag) { 3651bb32253SAndrey A. Chernov for( dkp = ep->dkp; dkp != NULL; dkp = *((struct kinfo_proc **)(&dkp->kp_eproc.e_spare[ 0]))) { 3661bb32253SAndrey A. Chernov char *p; 3671bb32253SAndrey A. Chernov p = fmt_argv(kvm_getargv(kd, dkp, argwidth), 3681bb32253SAndrey A. Chernov dkp->kp_proc.p_comm, MAXCOMLEN); 3691bb32253SAndrey A. Chernov if (p == NULL) 3701bb32253SAndrey A. Chernov p = "-"; 3711bb32253SAndrey A. Chernov (void)printf( "\t\t%-9d %s\n", dkp->kp_proc.p_pid, p); 3721bb32253SAndrey A. Chernov } 3731bb32253SAndrey A. Chernov } 37469267920SPeter Wemm (void)printf("%-*.*s %-3.3s %-*.*s ", 3759b50d902SRodney W. Grimes UT_NAMESIZE, UT_NAMESIZE, ep->utmp.ut_name, 376255318a8SAndrey A. Chernov strncmp(ep->utmp.ut_line, "tty", 3) && 377255318a8SAndrey A. Chernov strncmp(ep->utmp.ut_line, "cua", 3) ? 3789b50d902SRodney W. Grimes ep->utmp.ut_line : ep->utmp.ut_line + 3, 3799b50d902SRodney W. Grimes UT_HOSTSIZE, UT_HOSTSIZE, *p ? p : "-"); 3809b50d902SRodney W. Grimes pr_attime(&ep->utmp.ut_time, &now); 38170498d5aSDaniel O'Callaghan longidle=pr_idle(ep->idle); 38270498d5aSDaniel O'Callaghan if (longidle) 38370498d5aSDaniel O'Callaghan argwidth--; 3849b50d902SRodney W. Grimes if (ep->args != NULL) { 3859b50d902SRodney W. Grimes arglen = strlen(ep->args); 3869b50d902SRodney W. Grimes strvisx(vis_args, ep->args, 3879b50d902SRodney W. Grimes arglen > argwidth ? argwidth : arglen, 3889b50d902SRodney W. Grimes VIS_TAB | VIS_NL | VIS_NOSLASH); 3899b50d902SRodney W. Grimes } 3909b50d902SRodney W. Grimes (void)printf("%.*s\n", argwidth, ep->args); 3919b50d902SRodney W. Grimes } 3929b50d902SRodney W. Grimes exit(0); 3939b50d902SRodney W. Grimes } 3949b50d902SRodney W. Grimes 3959b50d902SRodney W. Grimes static void 3969b50d902SRodney W. Grimes pr_header(nowp, nusers) 3979b50d902SRodney W. Grimes time_t *nowp; 3989b50d902SRodney W. Grimes int nusers; 3999b50d902SRodney W. Grimes { 4009b50d902SRodney W. Grimes double avenrun[3]; 4019b50d902SRodney W. Grimes time_t uptime; 4029b50d902SRodney W. Grimes int days, hrs, i, mins; 4039b50d902SRodney W. Grimes int mib[2]; 4049b50d902SRodney W. Grimes size_t size; 4059b50d902SRodney W. Grimes char buf[256]; 4069b50d902SRodney W. Grimes 4079b50d902SRodney W. Grimes /* 4089b50d902SRodney W. Grimes * Print time of day. 4099b50d902SRodney W. Grimes * 4109b50d902SRodney W. Grimes * SCCS forces the string manipulation below, as it replaces 4119b50d902SRodney W. Grimes * %, M, and % in a character string with the file name. 4129b50d902SRodney W. Grimes */ 4136d7977a7SKazutaka YOKOTA (void)strftime(buf, sizeof(buf) - 1, 4149b50d902SRodney W. Grimes __CONCAT("%l:%","M%p"), localtime(nowp)); 4156d7977a7SKazutaka YOKOTA buf[sizeof(buf) - 1] = '\0'; 4169b50d902SRodney W. Grimes (void)printf("%s ", buf); 4179b50d902SRodney W. Grimes 4189b50d902SRodney W. Grimes /* 4199b50d902SRodney W. Grimes * Print how long system has been up. 4209b50d902SRodney W. Grimes * (Found by looking getting "boottime" from the kernel) 4219b50d902SRodney W. Grimes */ 4229b50d902SRodney W. Grimes mib[0] = CTL_KERN; 4239b50d902SRodney W. Grimes mib[1] = KERN_BOOTTIME; 4249b50d902SRodney W. Grimes size = sizeof(boottime); 4259b50d902SRodney W. Grimes if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && 4269b50d902SRodney W. Grimes boottime.tv_sec != 0) { 4279b50d902SRodney W. Grimes uptime = now - boottime.tv_sec; 4289b50d902SRodney W. Grimes uptime += 30; 429656dcd43SGarrett Wollman days = uptime / 86400; 430656dcd43SGarrett Wollman uptime %= 86400; 431656dcd43SGarrett Wollman hrs = uptime / 3600; 432656dcd43SGarrett Wollman uptime %= 3600; 433656dcd43SGarrett Wollman mins = uptime / 60; 4349b50d902SRodney W. Grimes (void)printf(" up"); 4359b50d902SRodney W. Grimes if (days > 0) 4369b50d902SRodney W. Grimes (void)printf(" %d day%s,", days, days > 1 ? "s" : ""); 4379b50d902SRodney W. Grimes if (hrs > 0 && mins > 0) 4389b50d902SRodney W. Grimes (void)printf(" %2d:%02d,", hrs, mins); 4399b50d902SRodney W. Grimes else { 4409b50d902SRodney W. Grimes if (hrs > 0) 4419b50d902SRodney W. Grimes (void)printf(" %d hr%s,", 4429b50d902SRodney W. Grimes hrs, hrs > 1 ? "s" : ""); 4439b50d902SRodney W. Grimes if (mins > 0) 4449b50d902SRodney W. Grimes (void)printf(" %d min%s,", 4459b50d902SRodney W. Grimes mins, mins > 1 ? "s" : ""); 4469b50d902SRodney W. Grimes } 4479b50d902SRodney W. Grimes } 4489b50d902SRodney W. Grimes 4499b50d902SRodney W. Grimes /* Print number of users logged in to system */ 4501b0c06d9SScott Mace (void)printf(" %d user%s", nusers, nusers == 1 ? "" : "s"); 4519b50d902SRodney W. Grimes 4529b50d902SRodney W. Grimes /* 4539b50d902SRodney W. Grimes * Print 1, 5, and 15 minute load averages. 4549b50d902SRodney W. Grimes */ 4559b50d902SRodney W. Grimes if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1) 4569b50d902SRodney W. Grimes (void)printf(", no load average information available\n"); 4579b50d902SRodney W. Grimes else { 4589b50d902SRodney W. Grimes (void)printf(", load averages:"); 4599b50d902SRodney W. Grimes for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) { 4609b50d902SRodney W. Grimes if (i > 0) 4619b50d902SRodney W. Grimes (void)printf(","); 4629b50d902SRodney W. Grimes (void)printf(" %.2f", avenrun[i]); 4639b50d902SRodney W. Grimes } 4649b50d902SRodney W. Grimes (void)printf("\n"); 4659b50d902SRodney W. Grimes } 4669b50d902SRodney W. Grimes } 4679b50d902SRodney W. Grimes 4689b50d902SRodney W. Grimes static struct stat * 4699b50d902SRodney W. Grimes ttystat(line) 4709b50d902SRodney W. Grimes char *line; 4719b50d902SRodney W. Grimes { 4729b50d902SRodney W. Grimes static struct stat sb; 4739b50d902SRodney W. Grimes char ttybuf[MAXPATHLEN]; 4749b50d902SRodney W. Grimes 4759b50d902SRodney W. Grimes (void)snprintf(ttybuf, sizeof(ttybuf), "%s/%s", _PATH_DEV, line); 4769b50d902SRodney W. Grimes if (stat(ttybuf, &sb)) 4779b50d902SRodney W. Grimes err(1, "%s", ttybuf); 4789b50d902SRodney W. Grimes return (&sb); 4799b50d902SRodney W. Grimes } 4809b50d902SRodney W. Grimes 4819b50d902SRodney W. Grimes static void 4829b50d902SRodney W. Grimes usage(wcmd) 4839b50d902SRodney W. Grimes int wcmd; 4849b50d902SRodney W. Grimes { 4859b50d902SRodney W. Grimes if (wcmd) 4869b50d902SRodney W. Grimes (void)fprintf(stderr, 48790389da9SPhilippe Charnier "usage: w [-hin] [-M core] [-N system] [user]\n"); 4889b50d902SRodney W. Grimes else 48990389da9SPhilippe Charnier (void)fprintf(stderr, 49090389da9SPhilippe Charnier "usage: uptime\n"); 4919b50d902SRodney W. Grimes exit (1); 4929b50d902SRodney W. Grimes } 493