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 <unistd.h> 769b50d902SRodney W. Grimes #include <utmp.h> 779b50d902SRodney W. Grimes #include <vis.h> 78baf72ec1SAndrey A. Chernov #include <locale.h> 799b50d902SRodney W. Grimes 806367cd09SPeter Wemm #include <arpa/nameser.h> 816367cd09SPeter Wemm #include <resolv.h> 826367cd09SPeter Wemm 839b50d902SRodney W. Grimes #include "extern.h" 849b50d902SRodney W. Grimes 859b50d902SRodney W. Grimes struct timeval boottime; 869b50d902SRodney W. Grimes struct utmp utmp; 879b50d902SRodney W. Grimes struct winsize ws; 889b50d902SRodney W. Grimes kvm_t *kd; 899b50d902SRodney W. Grimes time_t now; /* the current time of day */ 909b50d902SRodney W. Grimes time_t uptime; /* time of last reboot & elapsed time since */ 919b50d902SRodney W. Grimes int ttywidth; /* width of tty */ 929b50d902SRodney W. Grimes int argwidth; /* width of tty */ 939b50d902SRodney W. Grimes int header = 1; /* true if -h flag: don't print heading */ 949b50d902SRodney W. Grimes int nflag; /* true if -n flag: don't convert addrs */ 959b50d902SRodney W. Grimes int sortidle; /* sort bu idle time */ 969b50d902SRodney W. Grimes char *sel_user; /* login of particular user selected */ 979b50d902SRodney W. Grimes char domain[MAXHOSTNAMELEN]; 989b50d902SRodney W. Grimes 999b50d902SRodney W. Grimes /* 1009b50d902SRodney W. Grimes * One of these per active utmp entry. 1019b50d902SRodney W. Grimes */ 1029b50d902SRodney W. Grimes struct entry { 1039b50d902SRodney W. Grimes struct entry *next; 1049b50d902SRodney W. Grimes struct utmp utmp; 1059b50d902SRodney W. Grimes dev_t tdev; /* dev_t of terminal */ 1069b50d902SRodney W. Grimes time_t idle; /* idle time of terminal in seconds */ 1079b50d902SRodney W. Grimes struct kinfo_proc *kp; /* `most interesting' proc */ 1089b50d902SRodney W. Grimes char *args; /* arg list of interesting process */ 1099b50d902SRodney W. Grimes } *ep, *ehead = NULL, **nextp = &ehead; 1109b50d902SRodney W. Grimes 1119b50d902SRodney W. Grimes static void pr_header __P((time_t *, int)); 1129b50d902SRodney W. Grimes static struct stat 1139b50d902SRodney W. Grimes *ttystat __P((char *)); 1149b50d902SRodney W. Grimes static void usage __P((int)); 1159b50d902SRodney W. Grimes 1169b50d902SRodney W. Grimes char *fmt_argv __P((char **, char *, int)); /* ../../bin/ps/fmt.c */ 1179b50d902SRodney W. Grimes 1189b50d902SRodney W. Grimes int 1199b50d902SRodney W. Grimes main(argc, argv) 1209b50d902SRodney W. Grimes int argc; 1219b50d902SRodney W. Grimes char **argv; 1229b50d902SRodney W. Grimes { 1239b50d902SRodney W. Grimes extern char *__progname; 1249b50d902SRodney W. Grimes struct kinfo_proc *kp; 1259b50d902SRodney W. Grimes struct hostent *hp; 1269b50d902SRodney W. Grimes struct stat *stp; 1279b50d902SRodney W. Grimes FILE *ut; 1289b50d902SRodney W. Grimes u_long l; 1299b50d902SRodney W. Grimes size_t arglen; 13070498d5aSDaniel O'Callaghan int ch, i, nentries, nusers, wcmd, longidle; 1319b50d902SRodney W. Grimes char *memf, *nlistf, *p, *vis_args, *x; 1329b50d902SRodney W. Grimes char buf[MAXHOSTNAMELEN], errbuf[256]; 1339b50d902SRodney W. Grimes 134d1b2ad1aSAndrey A. Chernov (void) setlocale(LC_ALL, ""); 135baf72ec1SAndrey A. Chernov 1369b50d902SRodney W. Grimes /* Are we w(1) or uptime(1)? */ 1379b50d902SRodney W. Grimes p = __progname; 1389b50d902SRodney W. Grimes if (*p == '-') 1399b50d902SRodney W. Grimes p++; 1409b50d902SRodney W. Grimes if (*p == 'u') { 1419b50d902SRodney W. Grimes wcmd = 0; 1429b50d902SRodney W. Grimes p = ""; 1439b50d902SRodney W. Grimes } else { 1449b50d902SRodney W. Grimes wcmd = 1; 1459b50d902SRodney W. Grimes p = "hiflM:N:nsuw"; 1469b50d902SRodney W. Grimes } 1479b50d902SRodney W. Grimes 1489b50d902SRodney W. Grimes memf = nlistf = NULL; 1499b50d902SRodney W. Grimes while ((ch = getopt(argc, argv, p)) != EOF) 1509b50d902SRodney W. Grimes switch (ch) { 1519b50d902SRodney W. Grimes case 'h': 1529b50d902SRodney W. Grimes header = 0; 1539b50d902SRodney W. Grimes break; 1549b50d902SRodney W. Grimes case 'i': 1559b50d902SRodney W. Grimes sortidle = 1; 1569b50d902SRodney W. Grimes break; 1579b50d902SRodney W. Grimes case 'M': 1589b50d902SRodney W. Grimes header = 0; 1599b50d902SRodney W. Grimes memf = optarg; 1609b50d902SRodney W. Grimes break; 1619b50d902SRodney W. Grimes case 'N': 1629b50d902SRodney W. Grimes nlistf = optarg; 1639b50d902SRodney W. Grimes break; 1649b50d902SRodney W. Grimes case 'n': 1659b50d902SRodney W. Grimes nflag = 1; 1669b50d902SRodney W. Grimes break; 1679b50d902SRodney W. Grimes case 'f': case 'l': case 's': case 'u': case 'w': 1689b50d902SRodney W. Grimes warnx("[-flsuw] no longer supported"); 1699b50d902SRodney W. Grimes /* FALLTHROUGH */ 1709b50d902SRodney W. Grimes case '?': 1719b50d902SRodney W. Grimes default: 1729b50d902SRodney W. Grimes usage(wcmd); 1739b50d902SRodney W. Grimes } 1749b50d902SRodney W. Grimes argc -= optind; 1759b50d902SRodney W. Grimes argv += optind; 1769b50d902SRodney W. Grimes 1776367cd09SPeter Wemm if (!(_res.options & RES_INIT)) 1786367cd09SPeter Wemm res_init(); 1796367cd09SPeter Wemm _res.retrans = 2; /* resolver timeout to 2 seconds per try */ 1806367cd09SPeter Wemm _res.retry = 1; /* only try once.. */ 1816367cd09SPeter Wemm 18266e5b18fSPaul Traina /* 18366e5b18fSPaul Traina * Discard setgid privileges if not the running kernel so that bad 18466e5b18fSPaul Traina * guys can't print interesting stuff from kernel memory. 18566e5b18fSPaul Traina */ 18666e5b18fSPaul Traina if (nlistf != NULL || memf != NULL) 18766e5b18fSPaul Traina setgid(getgid()); 18866e5b18fSPaul Traina 1899b50d902SRodney W. Grimes if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL) 1909b50d902SRodney W. Grimes errx(1, "%s", errbuf); 1919b50d902SRodney W. Grimes 1929b50d902SRodney W. Grimes (void)time(&now); 1939b50d902SRodney W. Grimes if ((ut = fopen(_PATH_UTMP, "r")) == NULL) 1949b50d902SRodney W. Grimes err(1, "%s", _PATH_UTMP); 1959b50d902SRodney W. Grimes 1969b50d902SRodney W. Grimes if (*argv) 1979b50d902SRodney W. Grimes sel_user = *argv; 1989b50d902SRodney W. Grimes 1999b50d902SRodney W. Grimes for (nusers = 0; fread(&utmp, sizeof(utmp), 1, ut);) { 2009b50d902SRodney W. Grimes if (utmp.ut_name[0] == '\0') 2019b50d902SRodney W. Grimes continue; 2029b50d902SRodney W. Grimes ++nusers; 2039b50d902SRodney W. Grimes if (wcmd == 0 || (sel_user && 2049b50d902SRodney W. Grimes strncmp(utmp.ut_name, sel_user, UT_NAMESIZE) != 0)) 2059b50d902SRodney W. Grimes continue; 2069b50d902SRodney W. Grimes if ((ep = calloc(1, sizeof(struct entry))) == NULL) 2079b50d902SRodney W. Grimes err(1, NULL); 2089b50d902SRodney W. Grimes *nextp = ep; 2099b50d902SRodney W. Grimes nextp = &(ep->next); 2109b50d902SRodney W. Grimes memmove(&(ep->utmp), &utmp, sizeof(struct utmp)); 2119b50d902SRodney W. Grimes stp = ttystat(ep->utmp.ut_line); 2129b50d902SRodney W. Grimes ep->tdev = stp->st_rdev; 2139b50d902SRodney W. Grimes #ifdef CPU_CONSDEV 2149b50d902SRodney W. Grimes /* 2159b50d902SRodney W. Grimes * If this is the console device, attempt to ascertain 2169b50d902SRodney W. Grimes * the true console device dev_t. 2179b50d902SRodney W. Grimes */ 2189b50d902SRodney W. Grimes if (ep->tdev == 0) { 2199b50d902SRodney W. Grimes int mib[2]; 2209b50d902SRodney W. Grimes size_t size; 2219b50d902SRodney W. Grimes 2229b50d902SRodney W. Grimes mib[0] = CTL_MACHDEP; 2239b50d902SRodney W. Grimes mib[1] = CPU_CONSDEV; 2249b50d902SRodney W. Grimes size = sizeof(dev_t); 2259b50d902SRodney W. Grimes (void) sysctl(mib, 2, &ep->tdev, &size, NULL, 0); 2269b50d902SRodney W. Grimes } 2279b50d902SRodney W. Grimes #endif 2289b50d902SRodney W. Grimes if ((ep->idle = now - stp->st_atime) < 0) 2299b50d902SRodney W. Grimes ep->idle = 0; 2309b50d902SRodney W. Grimes } 2319b50d902SRodney W. Grimes (void)fclose(ut); 2329b50d902SRodney W. Grimes 2339b50d902SRodney W. Grimes if (header || wcmd == 0) { 2349b50d902SRodney W. Grimes pr_header(&now, nusers); 2359b50d902SRodney W. Grimes if (wcmd == 0) 2369b50d902SRodney W. Grimes exit (0); 2379b50d902SRodney W. Grimes 2389b50d902SRodney W. Grimes #define HEADER "USER TTY FROM LOGIN@ IDLE WHAT\n" 2399b50d902SRodney W. Grimes #define WUSED (sizeof (HEADER) - sizeof ("WHAT\n")) 2409b50d902SRodney W. Grimes (void)printf(HEADER); 2410812a2b4SPeter Wemm } 2429b50d902SRodney W. Grimes 2439b50d902SRodney W. Grimes if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL) 2449b50d902SRodney W. Grimes err(1, "%s", kvm_geterr(kd)); 2459b50d902SRodney W. Grimes for (i = 0; i < nentries; i++, kp++) { 2469b50d902SRodney W. Grimes struct proc *p = &kp->kp_proc; 2479b50d902SRodney W. Grimes struct eproc *e; 2489b50d902SRodney W. Grimes 2499b50d902SRodney W. Grimes if (p->p_stat == SIDL || p->p_stat == SZOMB) 2509b50d902SRodney W. Grimes continue; 2519b50d902SRodney W. Grimes e = &kp->kp_eproc; 2529b50d902SRodney W. Grimes for (ep = ehead; ep != NULL; ep = ep->next) { 2539b50d902SRodney W. Grimes if (ep->tdev == e->e_tdev && e->e_pgid == e->e_tpgid) { 2549b50d902SRodney W. Grimes /* 2559b50d902SRodney W. Grimes * Proc is in foreground of this terminal 2569b50d902SRodney W. Grimes */ 2579b50d902SRodney W. Grimes if (proc_compare(&ep->kp->kp_proc, p)) 2589b50d902SRodney W. Grimes ep->kp = kp; 2599b50d902SRodney W. Grimes break; 2609b50d902SRodney W. Grimes } 2619b50d902SRodney W. Grimes } 2629b50d902SRodney W. Grimes } 2639b50d902SRodney W. Grimes if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 && 2649b50d902SRodney W. Grimes ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 && 2659b50d902SRodney W. Grimes ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0) 2669b50d902SRodney W. Grimes ttywidth = 79; 2679b50d902SRodney W. Grimes else 2689b50d902SRodney W. Grimes ttywidth = ws.ws_col - 1; 2699b50d902SRodney W. Grimes argwidth = ttywidth - WUSED; 2709b50d902SRodney W. Grimes if (argwidth < 4) 2719b50d902SRodney W. Grimes argwidth = 8; 2729b50d902SRodney W. Grimes for (ep = ehead; ep != NULL; ep = ep->next) { 2739b50d902SRodney W. Grimes if (ep->kp == NULL) { 2749b50d902SRodney W. Grimes ep->args = "-"; 2759b50d902SRodney W. Grimes continue; 2769b50d902SRodney W. Grimes } 2779b50d902SRodney W. Grimes ep->args = fmt_argv(kvm_getargv(kd, ep->kp, argwidth), 2789b50d902SRodney W. Grimes ep->kp->kp_proc.p_comm, MAXCOMLEN); 2799b50d902SRodney W. Grimes if (ep->args == NULL) 2809b50d902SRodney W. Grimes err(1, NULL); 2819b50d902SRodney W. Grimes } 2829b50d902SRodney W. Grimes /* sort by idle time */ 2839b50d902SRodney W. Grimes if (sortidle && ehead != NULL) { 2849b50d902SRodney W. Grimes struct entry *from = ehead, *save; 2859b50d902SRodney W. Grimes 2869b50d902SRodney W. Grimes ehead = NULL; 2879b50d902SRodney W. Grimes while (from != NULL) { 2889b50d902SRodney W. Grimes for (nextp = &ehead; 2899b50d902SRodney W. Grimes (*nextp) && from->idle >= (*nextp)->idle; 2909b50d902SRodney W. Grimes nextp = &(*nextp)->next) 2919b50d902SRodney W. Grimes continue; 2929b50d902SRodney W. Grimes save = from; 2939b50d902SRodney W. Grimes from = from->next; 2949b50d902SRodney W. Grimes save->next = *nextp; 2959b50d902SRodney W. Grimes *nextp = save; 2969b50d902SRodney W. Grimes } 2979b50d902SRodney W. Grimes } 2989b50d902SRodney W. Grimes 2999b50d902SRodney W. Grimes if (!nflag) 3009b50d902SRodney W. Grimes if (gethostname(domain, sizeof(domain) - 1) < 0 || 3019b50d902SRodney W. Grimes (p = strchr(domain, '.')) == 0) 3029b50d902SRodney W. Grimes domain[0] = '\0'; 3039b50d902SRodney W. Grimes else { 3049b50d902SRodney W. Grimes domain[sizeof(domain) - 1] = '\0'; 3059b50d902SRodney W. Grimes memmove(domain, p, strlen(p) + 1); 3069b50d902SRodney W. Grimes } 3079b50d902SRodney W. Grimes 3089b50d902SRodney W. Grimes if ((vis_args = malloc(argwidth * 4 + 1)) == NULL) 3099b50d902SRodney W. Grimes err(1, NULL); 3109b50d902SRodney W. Grimes for (ep = ehead; ep != NULL; ep = ep->next) { 3119b50d902SRodney W. Grimes p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-"; 3129b50d902SRodney W. Grimes if ((x = strchr(p, ':')) != NULL) 3139b50d902SRodney W. Grimes *x++ = '\0'; 3149b50d902SRodney W. Grimes if (!nflag && isdigit(*p) && 3159b50d902SRodney W. Grimes (long)(l = inet_addr(p)) != -1 && 3169b50d902SRodney W. Grimes (hp = gethostbyaddr((char *)&l, sizeof(l), AF_INET))) { 3179b50d902SRodney W. Grimes if (domain[0] != '\0') { 3189b50d902SRodney W. Grimes p = hp->h_name; 3199b50d902SRodney W. Grimes p += strlen(hp->h_name); 3209b50d902SRodney W. Grimes p -= strlen(domain); 3219b50d902SRodney W. Grimes if (p > hp->h_name && strcmp(p, domain) == 0) 3229b50d902SRodney W. Grimes *p = '\0'; 3239b50d902SRodney W. Grimes } 3249b50d902SRodney W. Grimes p = hp->h_name; 3259b50d902SRodney W. Grimes } 3261694c5cbSAndrey A. Chernov if (nflag && *p && strcmp(p, "-") && inet_addr(p) == INADDR_NONE) { 32766b12529SAndrey A. Chernov hp = gethostbyname(p); 32866b12529SAndrey A. Chernov 32966b12529SAndrey A. Chernov if (hp != NULL) { 33066b12529SAndrey A. Chernov struct in_addr in; 33166b12529SAndrey A. Chernov 33266b12529SAndrey A. Chernov memmove(&in, hp->h_addr, sizeof(in)); 33366b12529SAndrey A. Chernov p = inet_ntoa(in); 334c54481c1SAndrey A. Chernov } 33566b12529SAndrey A. Chernov } 3369b50d902SRodney W. Grimes if (x) { 33792172c87SDavid Greenman (void)snprintf(buf, sizeof(buf), "%s:%.*s", p, 33892172c87SDavid Greenman ep->utmp.ut_host + UT_HOSTSIZE - x, x); 3399b50d902SRodney W. Grimes p = buf; 3409b50d902SRodney W. Grimes } 34169267920SPeter Wemm (void)printf("%-*.*s %-3.3s %-*.*s ", 3429b50d902SRodney W. Grimes UT_NAMESIZE, UT_NAMESIZE, ep->utmp.ut_name, 343255318a8SAndrey A. Chernov strncmp(ep->utmp.ut_line, "tty", 3) && 344255318a8SAndrey A. Chernov strncmp(ep->utmp.ut_line, "cua", 3) ? 3459b50d902SRodney W. Grimes ep->utmp.ut_line : ep->utmp.ut_line + 3, 3469b50d902SRodney W. Grimes UT_HOSTSIZE, UT_HOSTSIZE, *p ? p : "-"); 3479b50d902SRodney W. Grimes pr_attime(&ep->utmp.ut_time, &now); 34870498d5aSDaniel O'Callaghan longidle=pr_idle(ep->idle); 34970498d5aSDaniel O'Callaghan if (longidle) 35070498d5aSDaniel O'Callaghan argwidth--; 3519b50d902SRodney W. Grimes if (ep->args != NULL) { 3529b50d902SRodney W. Grimes arglen = strlen(ep->args); 3539b50d902SRodney W. Grimes strvisx(vis_args, ep->args, 3549b50d902SRodney W. Grimes arglen > argwidth ? argwidth : arglen, 3559b50d902SRodney W. Grimes VIS_TAB | VIS_NL | VIS_NOSLASH); 3569b50d902SRodney W. Grimes } 3579b50d902SRodney W. Grimes (void)printf("%.*s\n", argwidth, ep->args); 3589b50d902SRodney W. Grimes } 3599b50d902SRodney W. Grimes exit(0); 3609b50d902SRodney W. Grimes } 3619b50d902SRodney W. Grimes 3629b50d902SRodney W. Grimes static void 3639b50d902SRodney W. Grimes pr_header(nowp, nusers) 3649b50d902SRodney W. Grimes time_t *nowp; 3659b50d902SRodney W. Grimes int nusers; 3669b50d902SRodney W. Grimes { 3679b50d902SRodney W. Grimes double avenrun[3]; 3689b50d902SRodney W. Grimes time_t uptime; 3699b50d902SRodney W. Grimes int days, hrs, i, mins; 3709b50d902SRodney W. Grimes int mib[2]; 3719b50d902SRodney W. Grimes size_t size; 3729b50d902SRodney W. Grimes char buf[256]; 3739b50d902SRodney W. Grimes 3749b50d902SRodney W. Grimes /* 3759b50d902SRodney W. Grimes * Print time of day. 3769b50d902SRodney W. Grimes * 3779b50d902SRodney W. Grimes * SCCS forces the string manipulation below, as it replaces 3789b50d902SRodney W. Grimes * %, M, and % in a character string with the file name. 3799b50d902SRodney W. Grimes */ 3809b50d902SRodney W. Grimes (void)strftime(buf, sizeof(buf), 3819b50d902SRodney W. Grimes __CONCAT("%l:%","M%p"), localtime(nowp)); 3829b50d902SRodney W. Grimes (void)printf("%s ", buf); 3839b50d902SRodney W. Grimes 3849b50d902SRodney W. Grimes /* 3859b50d902SRodney W. Grimes * Print how long system has been up. 3869b50d902SRodney W. Grimes * (Found by looking getting "boottime" from the kernel) 3879b50d902SRodney W. Grimes */ 3889b50d902SRodney W. Grimes mib[0] = CTL_KERN; 3899b50d902SRodney W. Grimes mib[1] = KERN_BOOTTIME; 3909b50d902SRodney W. Grimes size = sizeof(boottime); 3919b50d902SRodney W. Grimes if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && 3929b50d902SRodney W. Grimes boottime.tv_sec != 0) { 3939b50d902SRodney W. Grimes uptime = now - boottime.tv_sec; 3949b50d902SRodney W. Grimes uptime += 30; 395656dcd43SGarrett Wollman days = uptime / 86400; 396656dcd43SGarrett Wollman uptime %= 86400; 397656dcd43SGarrett Wollman hrs = uptime / 3600; 398656dcd43SGarrett Wollman uptime %= 3600; 399656dcd43SGarrett Wollman mins = uptime / 60; 4009b50d902SRodney W. Grimes (void)printf(" up"); 4019b50d902SRodney W. Grimes if (days > 0) 4029b50d902SRodney W. Grimes (void)printf(" %d day%s,", days, days > 1 ? "s" : ""); 4039b50d902SRodney W. Grimes if (hrs > 0 && mins > 0) 4049b50d902SRodney W. Grimes (void)printf(" %2d:%02d,", hrs, mins); 4059b50d902SRodney W. Grimes else { 4069b50d902SRodney W. Grimes if (hrs > 0) 4079b50d902SRodney W. Grimes (void)printf(" %d hr%s,", 4089b50d902SRodney W. Grimes hrs, hrs > 1 ? "s" : ""); 4099b50d902SRodney W. Grimes if (mins > 0) 4109b50d902SRodney W. Grimes (void)printf(" %d min%s,", 4119b50d902SRodney W. Grimes mins, mins > 1 ? "s" : ""); 4129b50d902SRodney W. Grimes } 4139b50d902SRodney W. Grimes } 4149b50d902SRodney W. Grimes 4159b50d902SRodney W. Grimes /* Print number of users logged in to system */ 4161b0c06d9SScott Mace (void)printf(" %d user%s", nusers, nusers == 1 ? "" : "s"); 4179b50d902SRodney W. Grimes 4189b50d902SRodney W. Grimes /* 4199b50d902SRodney W. Grimes * Print 1, 5, and 15 minute load averages. 4209b50d902SRodney W. Grimes */ 4219b50d902SRodney W. Grimes if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1) 4229b50d902SRodney W. Grimes (void)printf(", no load average information available\n"); 4239b50d902SRodney W. Grimes else { 4249b50d902SRodney W. Grimes (void)printf(", load averages:"); 4259b50d902SRodney W. Grimes for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) { 4269b50d902SRodney W. Grimes if (i > 0) 4279b50d902SRodney W. Grimes (void)printf(","); 4289b50d902SRodney W. Grimes (void)printf(" %.2f", avenrun[i]); 4299b50d902SRodney W. Grimes } 4309b50d902SRodney W. Grimes (void)printf("\n"); 4319b50d902SRodney W. Grimes } 4329b50d902SRodney W. Grimes } 4339b50d902SRodney W. Grimes 4349b50d902SRodney W. Grimes static struct stat * 4359b50d902SRodney W. Grimes ttystat(line) 4369b50d902SRodney W. Grimes char *line; 4379b50d902SRodney W. Grimes { 4389b50d902SRodney W. Grimes static struct stat sb; 4399b50d902SRodney W. Grimes char ttybuf[MAXPATHLEN]; 4409b50d902SRodney W. Grimes 4419b50d902SRodney W. Grimes (void)snprintf(ttybuf, sizeof(ttybuf), "%s/%s", _PATH_DEV, line); 4429b50d902SRodney W. Grimes if (stat(ttybuf, &sb)) 4439b50d902SRodney W. Grimes err(1, "%s", ttybuf); 4449b50d902SRodney W. Grimes return (&sb); 4459b50d902SRodney W. Grimes } 4469b50d902SRodney W. Grimes 4479b50d902SRodney W. Grimes static void 4489b50d902SRodney W. Grimes usage(wcmd) 4499b50d902SRodney W. Grimes int wcmd; 4509b50d902SRodney W. Grimes { 4519b50d902SRodney W. Grimes if (wcmd) 4529b50d902SRodney W. Grimes (void)fprintf(stderr, 4539b50d902SRodney W. Grimes "usage: w: [-hin] [-M core] [-N system] [user]\n"); 4549b50d902SRodney W. Grimes else 4559b50d902SRodney W. Grimes (void)fprintf(stderr, "uptime\n"); 4569b50d902SRodney W. Grimes exit (1); 4579b50d902SRodney W. Grimes } 458