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; 1309b50d902SRodney W. Grimes int ch, i, nentries, nusers, wcmd; 1319b50d902SRodney W. Grimes char *memf, *nlistf, *p, *vis_args, *x; 1329b50d902SRodney W. Grimes char buf[MAXHOSTNAMELEN], errbuf[256]; 1339b50d902SRodney W. Grimes 134baf72ec1SAndrey A. Chernov (void) setlocale(LC_TIME|LC_CTYPE, ""); 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 1829b50d902SRodney W. Grimes if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL) 1839b50d902SRodney W. Grimes errx(1, "%s", errbuf); 1849b50d902SRodney W. Grimes 1859b50d902SRodney W. Grimes (void)time(&now); 1869b50d902SRodney W. Grimes if ((ut = fopen(_PATH_UTMP, "r")) == NULL) 1879b50d902SRodney W. Grimes err(1, "%s", _PATH_UTMP); 1889b50d902SRodney W. Grimes 1899b50d902SRodney W. Grimes if (*argv) 1909b50d902SRodney W. Grimes sel_user = *argv; 1919b50d902SRodney W. Grimes 1929b50d902SRodney W. Grimes for (nusers = 0; fread(&utmp, sizeof(utmp), 1, ut);) { 1939b50d902SRodney W. Grimes if (utmp.ut_name[0] == '\0') 1949b50d902SRodney W. Grimes continue; 1959b50d902SRodney W. Grimes ++nusers; 1969b50d902SRodney W. Grimes if (wcmd == 0 || (sel_user && 1979b50d902SRodney W. Grimes strncmp(utmp.ut_name, sel_user, UT_NAMESIZE) != 0)) 1989b50d902SRodney W. Grimes continue; 1999b50d902SRodney W. Grimes if ((ep = calloc(1, sizeof(struct entry))) == NULL) 2009b50d902SRodney W. Grimes err(1, NULL); 2019b50d902SRodney W. Grimes *nextp = ep; 2029b50d902SRodney W. Grimes nextp = &(ep->next); 2039b50d902SRodney W. Grimes memmove(&(ep->utmp), &utmp, sizeof(struct utmp)); 2049b50d902SRodney W. Grimes stp = ttystat(ep->utmp.ut_line); 2059b50d902SRodney W. Grimes ep->tdev = stp->st_rdev; 2069b50d902SRodney W. Grimes #ifdef CPU_CONSDEV 2079b50d902SRodney W. Grimes /* 2089b50d902SRodney W. Grimes * If this is the console device, attempt to ascertain 2099b50d902SRodney W. Grimes * the true console device dev_t. 2109b50d902SRodney W. Grimes */ 2119b50d902SRodney W. Grimes if (ep->tdev == 0) { 2129b50d902SRodney W. Grimes int mib[2]; 2139b50d902SRodney W. Grimes size_t size; 2149b50d902SRodney W. Grimes 2159b50d902SRodney W. Grimes mib[0] = CTL_MACHDEP; 2169b50d902SRodney W. Grimes mib[1] = CPU_CONSDEV; 2179b50d902SRodney W. Grimes size = sizeof(dev_t); 2189b50d902SRodney W. Grimes (void) sysctl(mib, 2, &ep->tdev, &size, NULL, 0); 2199b50d902SRodney W. Grimes } 2209b50d902SRodney W. Grimes #endif 2219b50d902SRodney W. Grimes if ((ep->idle = now - stp->st_atime) < 0) 2229b50d902SRodney W. Grimes ep->idle = 0; 2239b50d902SRodney W. Grimes } 2249b50d902SRodney W. Grimes (void)fclose(ut); 2259b50d902SRodney W. Grimes 2269b50d902SRodney W. Grimes if (header || wcmd == 0) { 2279b50d902SRodney W. Grimes pr_header(&now, nusers); 2289b50d902SRodney W. Grimes if (wcmd == 0) 2299b50d902SRodney W. Grimes exit (0); 2309b50d902SRodney W. Grimes 2319b50d902SRodney W. Grimes #define HEADER "USER TTY FROM LOGIN@ IDLE WHAT\n" 2329b50d902SRodney W. Grimes #define WUSED (sizeof (HEADER) - sizeof ("WHAT\n")) 2339b50d902SRodney W. Grimes (void)printf(HEADER); 2340812a2b4SPeter Wemm } 2359b50d902SRodney W. Grimes 2369b50d902SRodney W. Grimes if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL) 2379b50d902SRodney W. Grimes err(1, "%s", kvm_geterr(kd)); 2389b50d902SRodney W. Grimes for (i = 0; i < nentries; i++, kp++) { 2399b50d902SRodney W. Grimes struct proc *p = &kp->kp_proc; 2409b50d902SRodney W. Grimes struct eproc *e; 2419b50d902SRodney W. Grimes 2429b50d902SRodney W. Grimes if (p->p_stat == SIDL || p->p_stat == SZOMB) 2439b50d902SRodney W. Grimes continue; 2449b50d902SRodney W. Grimes e = &kp->kp_eproc; 2459b50d902SRodney W. Grimes for (ep = ehead; ep != NULL; ep = ep->next) { 2469b50d902SRodney W. Grimes if (ep->tdev == e->e_tdev && e->e_pgid == e->e_tpgid) { 2479b50d902SRodney W. Grimes /* 2489b50d902SRodney W. Grimes * Proc is in foreground of this terminal 2499b50d902SRodney W. Grimes */ 2509b50d902SRodney W. Grimes if (proc_compare(&ep->kp->kp_proc, p)) 2519b50d902SRodney W. Grimes ep->kp = kp; 2529b50d902SRodney W. Grimes break; 2539b50d902SRodney W. Grimes } 2549b50d902SRodney W. Grimes } 2559b50d902SRodney W. Grimes } 2569b50d902SRodney W. Grimes if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 && 2579b50d902SRodney W. Grimes ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 && 2589b50d902SRodney W. Grimes ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0) 2599b50d902SRodney W. Grimes ttywidth = 79; 2609b50d902SRodney W. Grimes else 2619b50d902SRodney W. Grimes ttywidth = ws.ws_col - 1; 2629b50d902SRodney W. Grimes argwidth = ttywidth - WUSED; 2639b50d902SRodney W. Grimes if (argwidth < 4) 2649b50d902SRodney W. Grimes argwidth = 8; 2659b50d902SRodney W. Grimes for (ep = ehead; ep != NULL; ep = ep->next) { 2669b50d902SRodney W. Grimes if (ep->kp == NULL) { 2679b50d902SRodney W. Grimes ep->args = "-"; 2689b50d902SRodney W. Grimes continue; 2699b50d902SRodney W. Grimes } 2709b50d902SRodney W. Grimes ep->args = fmt_argv(kvm_getargv(kd, ep->kp, argwidth), 2719b50d902SRodney W. Grimes ep->kp->kp_proc.p_comm, MAXCOMLEN); 2729b50d902SRodney W. Grimes if (ep->args == NULL) 2739b50d902SRodney W. Grimes err(1, NULL); 2749b50d902SRodney W. Grimes } 2759b50d902SRodney W. Grimes /* sort by idle time */ 2769b50d902SRodney W. Grimes if (sortidle && ehead != NULL) { 2779b50d902SRodney W. Grimes struct entry *from = ehead, *save; 2789b50d902SRodney W. Grimes 2799b50d902SRodney W. Grimes ehead = NULL; 2809b50d902SRodney W. Grimes while (from != NULL) { 2819b50d902SRodney W. Grimes for (nextp = &ehead; 2829b50d902SRodney W. Grimes (*nextp) && from->idle >= (*nextp)->idle; 2839b50d902SRodney W. Grimes nextp = &(*nextp)->next) 2849b50d902SRodney W. Grimes continue; 2859b50d902SRodney W. Grimes save = from; 2869b50d902SRodney W. Grimes from = from->next; 2879b50d902SRodney W. Grimes save->next = *nextp; 2889b50d902SRodney W. Grimes *nextp = save; 2899b50d902SRodney W. Grimes } 2909b50d902SRodney W. Grimes } 2919b50d902SRodney W. Grimes 2929b50d902SRodney W. Grimes if (!nflag) 2939b50d902SRodney W. Grimes if (gethostname(domain, sizeof(domain) - 1) < 0 || 2949b50d902SRodney W. Grimes (p = strchr(domain, '.')) == 0) 2959b50d902SRodney W. Grimes domain[0] = '\0'; 2969b50d902SRodney W. Grimes else { 2979b50d902SRodney W. Grimes domain[sizeof(domain) - 1] = '\0'; 2989b50d902SRodney W. Grimes memmove(domain, p, strlen(p) + 1); 2999b50d902SRodney W. Grimes } 3009b50d902SRodney W. Grimes 3019b50d902SRodney W. Grimes if ((vis_args = malloc(argwidth * 4 + 1)) == NULL) 3029b50d902SRodney W. Grimes err(1, NULL); 3039b50d902SRodney W. Grimes for (ep = ehead; ep != NULL; ep = ep->next) { 3049b50d902SRodney W. Grimes p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-"; 3059b50d902SRodney W. Grimes if ((x = strchr(p, ':')) != NULL) 3069b50d902SRodney W. Grimes *x++ = '\0'; 3079b50d902SRodney W. Grimes if (!nflag && isdigit(*p) && 3089b50d902SRodney W. Grimes (long)(l = inet_addr(p)) != -1 && 3099b50d902SRodney W. Grimes (hp = gethostbyaddr((char *)&l, sizeof(l), AF_INET))) { 3109b50d902SRodney W. Grimes if (domain[0] != '\0') { 3119b50d902SRodney W. Grimes p = hp->h_name; 3129b50d902SRodney W. Grimes p += strlen(hp->h_name); 3139b50d902SRodney W. Grimes p -= strlen(domain); 3149b50d902SRodney W. Grimes if (p > hp->h_name && strcmp(p, domain) == 0) 3159b50d902SRodney W. Grimes *p = '\0'; 3169b50d902SRodney W. Grimes } 3179b50d902SRodney W. Grimes p = hp->h_name; 3189b50d902SRodney W. Grimes } 3199b50d902SRodney W. Grimes if (x) { 32092172c87SDavid Greenman (void)snprintf(buf, sizeof(buf), "%s:%.*s", p, 32192172c87SDavid Greenman ep->utmp.ut_host + UT_HOSTSIZE - x, x); 3229b50d902SRodney W. Grimes p = buf; 3239b50d902SRodney W. Grimes } 32469267920SPeter Wemm (void)printf("%-*.*s %-3.3s %-*.*s ", 3259b50d902SRodney W. Grimes UT_NAMESIZE, UT_NAMESIZE, ep->utmp.ut_name, 326255318a8SAndrey A. Chernov strncmp(ep->utmp.ut_line, "tty", 3) && 327255318a8SAndrey A. Chernov strncmp(ep->utmp.ut_line, "cua", 3) ? 3289b50d902SRodney W. Grimes ep->utmp.ut_line : ep->utmp.ut_line + 3, 3299b50d902SRodney W. Grimes UT_HOSTSIZE, UT_HOSTSIZE, *p ? p : "-"); 3309b50d902SRodney W. Grimes pr_attime(&ep->utmp.ut_time, &now); 3319b50d902SRodney W. Grimes pr_idle(ep->idle); 3329b50d902SRodney W. Grimes if (ep->args != NULL) { 3339b50d902SRodney W. Grimes arglen = strlen(ep->args); 3349b50d902SRodney W. Grimes strvisx(vis_args, ep->args, 3359b50d902SRodney W. Grimes arglen > argwidth ? argwidth : arglen, 3369b50d902SRodney W. Grimes VIS_TAB | VIS_NL | VIS_NOSLASH); 3379b50d902SRodney W. Grimes } 3389b50d902SRodney W. Grimes (void)printf("%.*s\n", argwidth, ep->args); 3399b50d902SRodney W. Grimes } 3409b50d902SRodney W. Grimes exit(0); 3419b50d902SRodney W. Grimes } 3429b50d902SRodney W. Grimes 3439b50d902SRodney W. Grimes static void 3449b50d902SRodney W. Grimes pr_header(nowp, nusers) 3459b50d902SRodney W. Grimes time_t *nowp; 3469b50d902SRodney W. Grimes int nusers; 3479b50d902SRodney W. Grimes { 3489b50d902SRodney W. Grimes double avenrun[3]; 3499b50d902SRodney W. Grimes time_t uptime; 3509b50d902SRodney W. Grimes int days, hrs, i, mins; 3519b50d902SRodney W. Grimes int mib[2]; 3529b50d902SRodney W. Grimes size_t size; 3539b50d902SRodney W. Grimes char buf[256]; 3549b50d902SRodney W. Grimes 3559b50d902SRodney W. Grimes /* 3569b50d902SRodney W. Grimes * Print time of day. 3579b50d902SRodney W. Grimes * 3589b50d902SRodney W. Grimes * SCCS forces the string manipulation below, as it replaces 3599b50d902SRodney W. Grimes * %, M, and % in a character string with the file name. 3609b50d902SRodney W. Grimes */ 3619b50d902SRodney W. Grimes (void)strftime(buf, sizeof(buf), 3629b50d902SRodney W. Grimes __CONCAT("%l:%","M%p"), localtime(nowp)); 3639b50d902SRodney W. Grimes (void)printf("%s ", buf); 3649b50d902SRodney W. Grimes 3659b50d902SRodney W. Grimes /* 3669b50d902SRodney W. Grimes * Print how long system has been up. 3679b50d902SRodney W. Grimes * (Found by looking getting "boottime" from the kernel) 3689b50d902SRodney W. Grimes */ 3699b50d902SRodney W. Grimes mib[0] = CTL_KERN; 3709b50d902SRodney W. Grimes mib[1] = KERN_BOOTTIME; 3719b50d902SRodney W. Grimes size = sizeof(boottime); 3729b50d902SRodney W. Grimes if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && 3739b50d902SRodney W. Grimes boottime.tv_sec != 0) { 3749b50d902SRodney W. Grimes uptime = now - boottime.tv_sec; 3759b50d902SRodney W. Grimes uptime += 30; 376656dcd43SGarrett Wollman days = uptime / 86400; 377656dcd43SGarrett Wollman uptime %= 86400; 378656dcd43SGarrett Wollman hrs = uptime / 3600; 379656dcd43SGarrett Wollman uptime %= 3600; 380656dcd43SGarrett Wollman mins = uptime / 60; 3819b50d902SRodney W. Grimes (void)printf(" up"); 3829b50d902SRodney W. Grimes if (days > 0) 3839b50d902SRodney W. Grimes (void)printf(" %d day%s,", days, days > 1 ? "s" : ""); 3849b50d902SRodney W. Grimes if (hrs > 0 && mins > 0) 3859b50d902SRodney W. Grimes (void)printf(" %2d:%02d,", hrs, mins); 3869b50d902SRodney W. Grimes else { 3879b50d902SRodney W. Grimes if (hrs > 0) 3889b50d902SRodney W. Grimes (void)printf(" %d hr%s,", 3899b50d902SRodney W. Grimes hrs, hrs > 1 ? "s" : ""); 3909b50d902SRodney W. Grimes if (mins > 0) 3919b50d902SRodney W. Grimes (void)printf(" %d min%s,", 3929b50d902SRodney W. Grimes mins, mins > 1 ? "s" : ""); 3939b50d902SRodney W. Grimes } 3949b50d902SRodney W. Grimes } 3959b50d902SRodney W. Grimes 3969b50d902SRodney W. Grimes /* Print number of users logged in to system */ 3971b0c06d9SScott Mace (void)printf(" %d user%s", nusers, nusers == 1 ? "" : "s"); 3989b50d902SRodney W. Grimes 3999b50d902SRodney W. Grimes /* 4009b50d902SRodney W. Grimes * Print 1, 5, and 15 minute load averages. 4019b50d902SRodney W. Grimes */ 4029b50d902SRodney W. Grimes if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1) 4039b50d902SRodney W. Grimes (void)printf(", no load average information available\n"); 4049b50d902SRodney W. Grimes else { 4059b50d902SRodney W. Grimes (void)printf(", load averages:"); 4069b50d902SRodney W. Grimes for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) { 4079b50d902SRodney W. Grimes if (i > 0) 4089b50d902SRodney W. Grimes (void)printf(","); 4099b50d902SRodney W. Grimes (void)printf(" %.2f", avenrun[i]); 4109b50d902SRodney W. Grimes } 4119b50d902SRodney W. Grimes (void)printf("\n"); 4129b50d902SRodney W. Grimes } 4139b50d902SRodney W. Grimes } 4149b50d902SRodney W. Grimes 4159b50d902SRodney W. Grimes static struct stat * 4169b50d902SRodney W. Grimes ttystat(line) 4179b50d902SRodney W. Grimes char *line; 4189b50d902SRodney W. Grimes { 4199b50d902SRodney W. Grimes static struct stat sb; 4209b50d902SRodney W. Grimes char ttybuf[MAXPATHLEN]; 4219b50d902SRodney W. Grimes 4229b50d902SRodney W. Grimes (void)snprintf(ttybuf, sizeof(ttybuf), "%s/%s", _PATH_DEV, line); 4239b50d902SRodney W. Grimes if (stat(ttybuf, &sb)) 4249b50d902SRodney W. Grimes err(1, "%s", ttybuf); 4259b50d902SRodney W. Grimes return (&sb); 4269b50d902SRodney W. Grimes } 4279b50d902SRodney W. Grimes 4289b50d902SRodney W. Grimes static void 4299b50d902SRodney W. Grimes usage(wcmd) 4309b50d902SRodney W. Grimes int wcmd; 4319b50d902SRodney W. Grimes { 4329b50d902SRodney W. Grimes if (wcmd) 4339b50d902SRodney W. Grimes (void)fprintf(stderr, 4349b50d902SRodney W. Grimes "usage: w: [-hin] [-M core] [-N system] [user]\n"); 4359b50d902SRodney W. Grimes else 4369b50d902SRodney W. Grimes (void)fprintf(stderr, "uptime\n"); 4379b50d902SRodney W. Grimes exit (1); 4389b50d902SRodney W. Grimes } 439