]> git.saurik.com Git - apple/shell_cmds.git/blame - w/w.c
shell_cmds-203.tar.gz
[apple/shell_cmds.git] / w / w.c
CommitLineData
44bd5ea7
A
1/*-
2 * Copyright (c) 1980, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
e1a085ba 4 * Portions copyright (c) 2007 Apple Inc. All rights reserved.
44bd5ea7
A
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
e1a085ba 14 * 3. Neither the name of the University nor the names of its contributors
44bd5ea7
A
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/cdefs.h>
9bafe280 32
e1a085ba
A
33#ifndef __APPLE__
34__FBSDID("$FreeBSD: src/usr.bin/w/w.c,v 1.58 2005/06/04 23:40:09 gad Exp $");
35#endif
36
44bd5ea7 37#ifndef lint
9bafe280
A
38static const char copyright[] =
39"@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
40 The Regents of the University of California. All rights reserved.\n";
41#endif
44bd5ea7
A
42
43#ifndef lint
9bafe280 44static const char sccsid[] = "@(#)w.c 8.4 (Berkeley) 4/16/94";
44bd5ea7 45#endif
44bd5ea7
A
46
47/*
48 * w - print system status (who and what)
49 *
50 * This program is similar to the systat command on Tenex/Tops 10/20
51 *
52 */
53#include <sys/param.h>
54#include <sys/time.h>
55#include <sys/stat.h>
56#include <sys/sysctl.h>
57#include <sys/proc.h>
58#include <sys/user.h>
59#include <sys/ioctl.h>
60#include <sys/socket.h>
9bafe280 61#include <sys/tty.h>
44bd5ea7 62
e1a085ba
A
63#ifndef __APPLE__
64#include <machine/cpu.h>
65#endif
44bd5ea7
A
66#include <netinet/in.h>
67#include <arpa/inet.h>
9bafe280 68#include <arpa/nameser.h>
44bd5ea7
A
69
70#include <ctype.h>
71#include <err.h>
72#include <errno.h>
73#include <fcntl.h>
e1a085ba 74#if HAVE_KVM
44bd5ea7 75#include <kvm.h>
e1a085ba
A
76#endif
77#include <langinfo.h>
78#include <libutil.h>
44bd5ea7 79#include <limits.h>
9bafe280 80#include <locale.h>
44bd5ea7
A
81#include <netdb.h>
82#include <nlist.h>
83#include <paths.h>
9bafe280 84#include <resolv.h>
44bd5ea7
A
85#include <stdio.h>
86#include <stdlib.h>
87#include <string.h>
e1a085ba 88#include <timeconv.h>
44bd5ea7 89#include <unistd.h>
e1a085ba
A
90#if HAVE_UTMPX
91#include <utmpx.h>
92/* use utmp values so formatting is the same */
93#define UT_NAMESIZE 8
94#define UT_LINESIZE 8
95#else /* HAVE_UTMPX */
44bd5ea7 96#include <utmp.h>
e1a085ba 97#endif /* HAVE_UTMPX */
44bd5ea7
A
98#include <vis.h>
99
41a2c555
A
100#include <TargetConditionals.h>
101
44bd5ea7
A
102#include "extern.h"
103
104struct timeval boottime;
e1a085ba 105#if !HAVE_UTMPX
44bd5ea7 106struct utmp utmp;
e1a085ba 107#endif
44bd5ea7 108struct winsize ws;
e1a085ba 109#if HAVE_KVM
44bd5ea7 110kvm_t *kd;
e1a085ba 111#endif
44bd5ea7 112time_t now; /* the current time of day */
44bd5ea7
A
113int ttywidth; /* width of tty */
114int argwidth; /* width of tty */
115int header = 1; /* true if -h flag: don't print heading */
e1a085ba 116#if !HAVE_UTMPX
44bd5ea7 117int nflag; /* true if -n flag: don't convert addrs */
e1a085ba
A
118#endif
119#ifndef __APPLE__
9bafe280 120int dflag; /* true if -d flag: output debug info */
e1a085ba 121#endif
9bafe280
A
122int sortidle; /* sort by idle time */
123int use_ampm; /* use AM/PM time */
124int use_comma; /* use comma as floats separator */
125char **sel_users; /* login array of particular users selected */
44bd5ea7
A
126
127/*
128 * One of these per active utmp entry.
129 */
130struct entry {
131 struct entry *next;
e1a085ba
A
132#if HAVE_UTMPX
133 struct utmpx utmp;
134#else
44bd5ea7 135 struct utmp utmp;
e1a085ba 136#endif
9bafe280
A
137 dev_t tdev; /* dev_t of terminal */
138 time_t idle; /* idle time of terminal in seconds */
139 struct kinfo_proc *kp; /* `most interesting' proc */
140 char *args; /* arg list of interesting process */
141 struct kinfo_proc *dkp; /* debug option proc list */
44bd5ea7
A
142} *ep, *ehead = NULL, **nextp = &ehead;
143
e1a085ba
A
144#ifndef __APPLE__
145#define debugproc(p) *((struct kinfo_proc **)&(p)->ki_udata)
146#else
9bafe280 147#define debugproc(p) *((struct kinfo_proc **)&(p)->ki_spare[0])
e1a085ba 148#endif
9bafe280
A
149
150/* W_DISPHOSTSIZE should not be greater than UT_HOSTSIZE */
151#define W_DISPHOSTSIZE 16
152
153static void pr_header(time_t *, int);
154static struct stat *ttystat(char *, int);
155static void usage(int);
156static int this_is_uptime(const char *s);
e1a085ba 157#if !HAVE_KVM
9bafe280 158static void w_getargv(void);
e1a085ba 159#endif
9bafe280
A
160
161char *fmt_argv(char **, char *, int); /* ../../bin/ps/fmt.c */
44bd5ea7
A
162
163int
e1a085ba 164main(int argc, char *argv[])
44bd5ea7 165{
e1a085ba
A
166 struct kinfo_proc *kp;
167 struct kinfo_proc *kprocbuf;
9bafe280 168 struct kinfo_proc *dkp;
44bd5ea7 169 struct stat *stp;
e1a085ba
A
170#if HAVE_UTMPX
171 struct utmpx *ux;
172#else
44bd5ea7 173 FILE *ut;
e1a085ba 174#endif
9bafe280 175 time_t touched;
e1a085ba 176#if HAVE_KVM
9bafe280
A
177 int ch, i, nentries, nusers, wcmd, longidle, dropgid;
178 const char *memf, *nlistf, *p;
e1a085ba
A
179#else
180 int ch, i, nentries, nusers, wcmd, longidle;
181 const char *p;
182#endif /* HAVE_KVM */
9bafe280 183 char *x_suffix;
e1a085ba
A
184#ifdef __APPLE__
185 char buf[MAXHOSTNAMELEN];
186#else
44bd5ea7 187 char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX];
9bafe280 188 char fn[MAXHOSTNAMELEN];
e1a085ba 189#endif /* __APPLE__ */
9bafe280 190 char *dot;
e1a085ba 191#if !HAVE_KVM
9bafe280
A
192 int local_error = 0, retry_count = 0;
193 size_t bufSize = 0;
194 size_t orig_bufSize = 0;
195 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
e1a085ba 196#endif
9bafe280
A
197
198 (void)setlocale(LC_ALL, "");
e1a085ba 199#ifndef __APPLE__
9bafe280
A
200 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
201 use_comma = (*nl_langinfo(RADIXCHAR) != ',');
e1a085ba 202#endif
44bd5ea7
A
203
204 /* Are we w(1) or uptime(1)? */
9bafe280 205 if (this_is_uptime(argv[0]) == 0) {
44bd5ea7
A
206 wcmd = 0;
207 p = "";
208 } else {
209 wcmd = 1;
9bafe280 210 p = "dhiflM:N:nsuw";
44bd5ea7
A
211 }
212
e1a085ba 213#if HAVE_KVM
9bafe280 214 dropgid = 0;
e1a085ba
A
215 memf = nlistf = _PATH_DEVNULL;
216#endif
44bd5ea7
A
217 while ((ch = getopt(argc, argv, p)) != -1)
218 switch (ch) {
e1a085ba 219#ifndef __APPLE__
9bafe280
A
220 case 'd':
221 dflag = 1;
222 break;
e1a085ba 223#endif
44bd5ea7
A
224 case 'h':
225 header = 0;
226 break;
227 case 'i':
228 sortidle = 1;
229 break;
e1a085ba 230#if HAVE_KVM
44bd5ea7
A
231 case 'M':
232 header = 0;
233 memf = optarg;
9bafe280 234 dropgid = 1;
44bd5ea7
A
235 break;
236 case 'N':
237 nlistf = optarg;
9bafe280 238 dropgid = 1;
44bd5ea7 239 break;
e1a085ba
A
240#endif /* HAVE_KVM */
241#if !HAVE_UTMPX
44bd5ea7
A
242 case 'n':
243 nflag = 1;
244 break;
e1a085ba
A
245#else /* !HAVE_UTMPX */
246 case 'n':
247#endif /* !HAVE_UTMPX */
44bd5ea7 248 case 'f': case 'l': case 's': case 'u': case 'w':
e1a085ba
A
249#if !HAVE_KVM
250 case 'M': case 'N':
251#endif
252#ifdef __APPLE__
253 case 'd':
254 warnx("[-MNdflnsuw] no longer supported");
255#else
44bd5ea7 256 warnx("[-flsuw] no longer supported");
e1a085ba 257#endif
44bd5ea7
A
258 /* FALLTHROUGH */
259 case '?':
260 default:
261 usage(wcmd);
262 }
263 argc -= optind;
264 argv += optind;
265
41a2c555 266#if !TARGET_OS_EMBEDDED
9bafe280
A
267 if (!(_res.options & RES_INIT))
268 res_init();
269 _res.retrans = 2; /* resolver timeout to 2 seconds per try */
270 _res.retry = 1; /* only try once.. */
41a2c555 271#endif
9bafe280 272
e1a085ba 273#if HAVE_KVM
44bd5ea7 274 /*
9bafe280
A
275 * Discard setgid privileges if not the running kernel so that bad
276 * guys can't print interesting stuff from kernel memory.
44bd5ea7 277 */
9bafe280
A
278 if (dropgid)
279 setgid(getgid());
44bd5ea7 280
c0fcf4e1 281 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL)
44bd5ea7 282 errx(1, "%s", errbuf);
9bafe280 283#endif
44bd5ea7
A
284
285 (void)time(&now);
e1a085ba
A
286#if HAVE_UTMPX
287 setutxent();
288#else
44bd5ea7
A
289 if ((ut = fopen(_PATH_UTMP, "r")) == NULL)
290 err(1, "%s", _PATH_UTMP);
e1a085ba 291#endif
44bd5ea7
A
292
293 if (*argv)
9bafe280 294 sel_users = argv;
44bd5ea7 295
e1a085ba
A
296#if HAVE_UTMPX
297 for (nusers = 0; (ux = getutxent());) {
298 if (ux->ut_user[0] == '\0' || ux->ut_type != USER_PROCESS)
299 continue;
300 if (!(stp = ttystat(ux->ut_line, sizeof(ux->ut_line))))
301 continue; /* corrupted record */
302#else
44bd5ea7
A
303 for (nusers = 0; fread(&utmp, sizeof(utmp), 1, ut);) {
304 if (utmp.ut_name[0] == '\0')
305 continue;
9bafe280
A
306 if (!(stp = ttystat(utmp.ut_line, UT_LINESIZE)))
307 continue; /* corrupted record */
e1a085ba 308#endif
44bd5ea7 309 ++nusers;
9bafe280 310 if (wcmd == 0)
44bd5ea7 311 continue;
9bafe280
A
312 if (sel_users) {
313 int usermatch;
314 char **user;
315
316 usermatch = 0;
317 for (user = sel_users; !usermatch && *user; user++)
e1a085ba
A
318#if HAVE_UTMPX
319 if (!strncmp(ux->ut_user, *user, sizeof(ux->ut_user)))
320#else
9bafe280 321 if (!strncmp(utmp.ut_name, *user, UT_NAMESIZE))
e1a085ba 322#endif
9bafe280
A
323 usermatch = 1;
324 if (!usermatch)
325 continue;
326 }
44bd5ea7 327 if ((ep = calloc(1, sizeof(struct entry))) == NULL)
9bafe280 328 errx(1, "calloc");
44bd5ea7 329 *nextp = ep;
9bafe280 330 nextp = &ep->next;
e1a085ba
A
331#if HAVE_UTMPX
332 memmove(&ep->utmp, ux, sizeof(*ux));
333#else
9bafe280 334 memmove(&ep->utmp, &utmp, sizeof(struct utmp));
e1a085ba 335#endif
44bd5ea7
A
336 ep->tdev = stp->st_rdev;
337#ifdef CPU_CONSDEV
338 /*
339 * If this is the console device, attempt to ascertain
340 * the true console device dev_t.
341 */
342 if (ep->tdev == 0) {
343 int mib[2];
344 size_t size;
345
346 mib[0] = CTL_MACHDEP;
347 mib[1] = CPU_CONSDEV;
348 size = sizeof(dev_t);
9bafe280 349 (void)sysctl(mib, 2, &ep->tdev, &size, NULL, 0);
44bd5ea7
A
350 }
351#endif
9bafe280 352 touched = stp->st_atime;
e1a085ba
A
353#ifdef __APPLE__
354 if (touched < ep->utmp.ut_tv.tv_sec) {
355 /* tty untouched since before login */
356 touched = ep->utmp.ut_tv.tv_sec;
357 }
358#else
9bafe280
A
359 if (touched < ep->utmp.ut_time) {
360 /* tty untouched since before login */
361 touched = ep->utmp.ut_time;
362 }
e1a085ba 363#endif
9bafe280 364 if ((ep->idle = now - touched) < 0)
44bd5ea7
A
365 ep->idle = 0;
366 }
e1a085ba
A
367#if HAVE_UTMPX
368 endutxent();
369#else
44bd5ea7 370 (void)fclose(ut);
e1a085ba 371#endif
44bd5ea7
A
372
373 if (header || wcmd == 0) {
374 pr_header(&now, nusers);
9bafe280 375 if (wcmd == 0) {
e1a085ba 376#if HAVE_KVM
9bafe280
A
377 (void)kvm_close(kd);
378#endif
379 exit(0);
380 }
44bd5ea7 381
9bafe280
A
382#define HEADER_USER "USER"
383#define HEADER_TTY "TTY"
384#define HEADER_FROM "FROM"
385#define HEADER_LOGIN_IDLE "LOGIN@ IDLE "
386#define HEADER_WHAT "WHAT\n"
387#define WUSED (UT_NAMESIZE + UT_LINESIZE + W_DISPHOSTSIZE + \
388 sizeof(HEADER_LOGIN_IDLE) + 3) /* header width incl. spaces */
389 (void)printf("%-*.*s %-*.*s %-*.*s %s",
390 UT_NAMESIZE, UT_NAMESIZE, HEADER_USER,
391 UT_LINESIZE, UT_LINESIZE, HEADER_TTY,
392 W_DISPHOSTSIZE, W_DISPHOSTSIZE, HEADER_FROM,
393 HEADER_LOGIN_IDLE HEADER_WHAT);
394 }
44bd5ea7 395
e1a085ba 396#if HAVE_KVM
44bd5ea7 397 if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL)
9bafe280 398 err(1, "%s", kvm_geterr(kd));
44bd5ea7 399#else
9bafe280
A
400 mib[0] = CTL_KERN;
401 mib[1] = KERN_PROC;
402 mib[2] = KERN_PROC_ALL;
403 mib[3] = 0;
404
405 if (sysctl(mib, 4, NULL, &bufSize, NULL, 0) < 0) {
406 perror("Failure calling sysctl");
407 exit(1);
408 }
44bd5ea7 409
9bafe280
A
410 kprocbuf = kp = (struct kinfo_proc *)malloc(bufSize);
411
412 retry_count = 0;
413 orig_bufSize = bufSize;
414 for (retry_count = 0; ; retry_count++) {
415 local_error = 0;
416 bufSize = orig_bufSize;
417 if ((local_error = sysctl(mib, 4, kp, &bufSize, NULL, 0)) < 0) {
418 if (retry_count < 1000) {
419 sleep(1);
420 continue;
421 }
422 perror("Failure calling sysctl");
423 exit(1);
424 } else if (local_error == 0) {
425 break;
426 }
427 sleep(1);
428 }
429 nentries = bufSize / sizeof(struct kinfo_proc);
e1a085ba
A
430#endif /* !HAVE_KVM */
431
432#if !HAVE_KVM
433#define ki_stat kp_proc.p_stat
434#define ki_pgid kp_eproc.e_pgid
435#define ki_tpgid kp_eproc.e_tpgid
436#define ki_tdev kp_eproc.e_tdev
437#endif /* !HAVE_KVM */
9bafe280 438 for (i = 0; i < nentries; i++, kp++) {
e1a085ba 439 if (kp->ki_stat == SIDL || kp->ki_stat == SZOMB)
44bd5ea7 440 continue;
44bd5ea7 441 for (ep = ehead; ep != NULL; ep = ep->next) {
e1a085ba 442 if (ep->tdev == kp->ki_tdev) {
44bd5ea7 443 /*
9bafe280 444 * proc is associated with this terminal
44bd5ea7 445 */
e1a085ba 446 if (ep->kp == NULL && kp->ki_pgid == kp->ki_tpgid) {
9bafe280
A
447 /*
448 * Proc is 'most interesting'
449 */
e1a085ba 450 if (proc_compare(ep->kp, kp))
9bafe280
A
451 ep->kp = kp;
452 }
453 /*
454 * Proc debug option info; add to debug
455 * list using kinfo_proc ki_spare[0]
456 * as next pointer; ptr to ptr avoids the
457 * ptr = long assumption.
458 */
459 dkp = ep->dkp;
460 ep->dkp = kp;
e1a085ba 461#ifndef __APPLE__
9bafe280 462 debugproc(kp) = dkp;
e1a085ba 463#endif
44bd5ea7
A
464 }
465 }
466 }
467 if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&
468 ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 &&
469 ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0)
470 ttywidth = 79;
471 else
472 ttywidth = ws.ws_col - 1;
473 argwidth = ttywidth - WUSED;
474 if (argwidth < 4)
475 argwidth = 8;
9bafe280
A
476 for (ep = ehead; ep != NULL; ep = ep->next) {
477 if (ep->kp == NULL) {
478 ep->args = strdup("-");
479 continue;
480 }
e1a085ba 481#if HAVE_KVM
9bafe280 482 ep->args = fmt_argv(kvm_getargv(kd, ep->kp, argwidth),
e1a085ba 483 ep->kp->ki_comm, MAXCOMLEN);
9bafe280
A
484#else
485 w_getargv();
e1a085ba 486#endif /* HAVE_KVM */
9bafe280
A
487 if (ep->args == NULL)
488 err(1, NULL);
489 }
44bd5ea7
A
490 /* sort by idle time */
491 if (sortidle && ehead != NULL) {
9bafe280
A
492 struct entry *from, *save;
493
494 from = ehead;
44bd5ea7
A
495 ehead = NULL;
496 while (from != NULL) {
497 for (nextp = &ehead;
498 (*nextp) && from->idle >= (*nextp)->idle;
499 nextp = &(*nextp)->next)
500 continue;
501 save = from;
502 from = from->next;
503 save->next = *nextp;
504 *nextp = save;
505 }
506 }
44bd5ea7
A
507
508 for (ep = ehead; ep != NULL; ep = ep->next) {
e1a085ba
A
509#if HAVE_UTMPX
510 char host_buf[sizeof(ep->utmp.ut_host) + 1];
511 strlcpy(host_buf, ep->utmp.ut_host, sizeof(host_buf));
512#else
9bafe280
A
513 char host_buf[UT_HOSTSIZE + 1];
514 struct sockaddr_storage ss;
515 struct sockaddr *sa = (struct sockaddr *)&ss;
516 struct sockaddr_in *lsin = (struct sockaddr_in *)&ss;
9bafe280 517 struct sockaddr_in6 *lsin6 = (struct sockaddr_in6 *)&ss;
e1a085ba 518 time_t t;
9bafe280
A
519 int isaddr;
520
521 host_buf[UT_HOSTSIZE] = '\0';
522 strncpy(host_buf, ep->utmp.ut_host, UT_HOSTSIZE);
e1a085ba 523#endif /* HAVE_UTMPX */
9bafe280
A
524 p = *host_buf ? host_buf : "-";
525 if ((x_suffix = strrchr(p, ':')) != NULL) {
526 if ((dot = strchr(x_suffix, '.')) != NULL &&
527 strchr(dot+1, '.') == NULL)
528 *x_suffix++ = '\0';
529 else
530 x_suffix = NULL;
531 }
e1a085ba 532#if !HAVE_UTMPX
9bafe280
A
533 if (!nflag) {
534 /* Attempt to change an IP address into a name */
535 isaddr = 0;
536 memset(&ss, '\0', sizeof(ss));
9bafe280
A
537 if (inet_pton(AF_INET6, p, &lsin6->sin6_addr) == 1) {
538 lsin6->sin6_len = sizeof(*lsin6);
539 lsin6->sin6_family = AF_INET6;
540 isaddr = 1;
541 } else if (inet_pton(AF_INET, p, &lsin->sin_addr) == 1) {
542 lsin->sin_len = sizeof(*lsin);
543 lsin->sin_family = AF_INET;
544 isaddr = 1;
545 }
546 if (isaddr && realhostname_sa(fn, sizeof(fn), sa,
547 sa->sa_len) == HOSTNAME_FOUND)
548 p = fn;
44bd5ea7 549 }
e1a085ba 550#endif /* !HAVE_UTMPX */
9bafe280
A
551 if (x_suffix) {
552 (void)snprintf(buf, sizeof(buf), "%s:%s", p, x_suffix);
44bd5ea7
A
553 p = buf;
554 }
e1a085ba 555#ifndef __APPLE__
9bafe280
A
556 if (dflag) {
557 for (dkp = ep->dkp; dkp != NULL; dkp = debugproc(dkp)) {
558 const char *ptr;
559
560 ptr = fmt_argv(kvm_getargv(kd, dkp, argwidth),
561 dkp->ki_comm, MAXCOMLEN);
562 if (ptr == NULL)
563 ptr = "-";
564 (void)printf("\t\t%-9d %s\n",
565 dkp->ki_pid, ptr);
566 }
567 }
e1a085ba 568#endif /* !__APPLE__ */
9bafe280 569 (void)printf("%-*.*s %-*.*s %-*.*s ",
e1a085ba
A
570#if HAVE_UTMPX
571 UT_NAMESIZE, (int)sizeof(ep->utmp.ut_user), ep->utmp.ut_user,
572 UT_LINESIZE, (int)sizeof(ep->utmp.ut_line),
573#else
44bd5ea7 574 UT_NAMESIZE, UT_NAMESIZE, ep->utmp.ut_name,
9bafe280 575 UT_LINESIZE, UT_LINESIZE,
e1a085ba 576#endif
9bafe280
A
577 strncmp(ep->utmp.ut_line, "tty", 3) &&
578 strncmp(ep->utmp.ut_line, "cua", 3) ?
44bd5ea7 579 ep->utmp.ut_line : ep->utmp.ut_line + 3,
9bafe280 580 W_DISPHOSTSIZE, W_DISPHOSTSIZE, *p ? p : "-");
e1a085ba
A
581#ifdef __APPLE__
582 pr_attime(&ep->utmp.ut_tv.tv_sec, &now);
583#else
584 t = _time_to_time32(ep->utmp.ut_time);
585 pr_attime(&t, &now);
586#endif
9bafe280
A
587 longidle = pr_idle(ep->idle);
588 (void)printf("%.*s\n", argwidth - longidle, ep->args);
e1a085ba 589#ifdef __APPLE__
9bafe280
A
590 free(ep->args);
591#endif
44bd5ea7 592 }
e1a085ba 593#if HAVE_KVM
9bafe280
A
594 (void)kvm_close(kd);
595#else
596 free(kprocbuf);
e1a085ba 597#endif /* HAVE_KVM */
44bd5ea7
A
598 exit(0);
599}
600
44bd5ea7 601static void
e1a085ba 602pr_header(time_t *nowp, int nusers)
44bd5ea7
A
603{
604 double avenrun[3];
605 time_t uptime;
9bafe280 606 int days, hrs, i, mins, secs;
44bd5ea7
A
607 int mib[2];
608 size_t size;
609 char buf[256];
610
611 /*
612 * Print time of day.
44bd5ea7 613 */
e1a085ba
A
614 if (strftime(buf, sizeof(buf),
615 use_ampm ? "%l:%M%p" : "%k:%M", localtime(nowp)) != 0)
616 (void)printf("%s ", buf);
44bd5ea7
A
617 /*
618 * Print how long system has been up.
619 * (Found by looking getting "boottime" from the kernel)
620 */
621 mib[0] = CTL_KERN;
622 mib[1] = KERN_BOOTTIME;
623 size = sizeof(boottime);
624 if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
625 boottime.tv_sec != 0) {
626 uptime = now - boottime.tv_sec;
9bafe280
A
627 if (uptime > 60)
628 uptime += 30;
629 days = uptime / 86400;
630 uptime %= 86400;
631 hrs = uptime / 3600;
632 uptime %= 3600;
633 mins = uptime / 60;
634 secs = uptime % 60;
635 (void)printf(" up");
636 if (days > 0)
637 (void)printf(" %d day%s,", days, days > 1 ? "s" : "");
638 if (hrs > 0 && mins > 0)
639 (void)printf(" %2d:%02d,", hrs, mins);
640 else if (hrs > 0)
641 (void)printf(" %d hr%s,", hrs, hrs > 1 ? "s" : "");
642 else if (mins > 0)
643 (void)printf(" %d min%s,", mins, mins > 1 ? "s" : "");
644 else
645 (void)printf(" %d sec%s,", secs, secs > 1 ? "s" : "");
44bd5ea7
A
646 }
647
648 /* Print number of users logged in to system */
9bafe280 649 (void)printf(" %d user%s", nusers, nusers == 1 ? "" : "s");
44bd5ea7
A
650
651 /*
652 * Print 1, 5, and 15 minute load averages.
653 */
654 if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1)
655 (void)printf(", no load average information available\n");
656 else {
657 (void)printf(", load averages:");
9bafe280
A
658 for (i = 0; i < (int)(sizeof(avenrun) / sizeof(avenrun[0])); i++) {
659 if (use_comma && i > 0)
44bd5ea7
A
660 (void)printf(",");
661 (void)printf(" %.2f", avenrun[i]);
662 }
663 (void)printf("\n");
664 }
665}
666
667static struct stat *
e1a085ba 668ttystat(char *line, int sz)
44bd5ea7
A
669{
670 static struct stat sb;
671 char ttybuf[MAXPATHLEN];
672
9bafe280 673 (void)snprintf(ttybuf, sizeof(ttybuf), "%s%.*s", _PATH_DEV, sz, line);
e1a085ba
A
674 if (stat(ttybuf, &sb) == 0) {
675 return (&sb);
676 } else {
9bafe280 677 warn("%s", ttybuf);
44bd5ea7 678 return (NULL);
9bafe280 679 }
44bd5ea7
A
680}
681
682static void
e1a085ba 683usage(int wcmd)
44bd5ea7
A
684{
685 if (wcmd)
686 (void)fprintf(stderr,
e1a085ba 687 "usage: w [hi] [user ...]\n");
44bd5ea7 688 else
9bafe280 689 (void)fprintf(stderr, "usage: uptime\n");
44bd5ea7
A
690 exit(1);
691}
9bafe280
A
692
693static int
e1a085ba 694this_is_uptime(const char *s)
9bafe280
A
695{
696 const char *u;
697
698 if ((u = strrchr(s, '/')) != NULL)
699 ++u;
700 else
701 u = s;
702 if (strcmp(u, "uptime") == 0)
703 return (0);
704 return (-1);
705}
706
e1a085ba 707#if !HAVE_KVM
9bafe280
A
708static void
709w_getargv(void)
710{
711 int mib[3], argmax;
712 size_t size;
713 char *procargs, *sp, *np, *cp;
714
715 mib[0] = CTL_KERN;
716 mib[1] = KERN_ARGMAX;
717
718 size = sizeof(argmax);
719 if (sysctl(mib, 2, &argmax, &size, NULL, 0) == -1) {
720 goto ERROR;
721 }
722
723 procargs = malloc(argmax);
724 if (procargs == NULL) {
725 goto ERROR;
726 }
727
728 mib[0] = CTL_KERN;
729 mib[1] = KERN_PROCARGS;
730 mib[2] = KI_PROC(ep)->p_pid;
731
732 size = (size_t)argmax;
733 if (sysctl(mib, 3, procargs, &size, NULL, 0) == -1) {
734 goto ERROR_FREE;
735 }
736
737 for (cp = procargs; cp < &procargs[size]; cp++) {
738 if (*cp == '\0') {
739 break;
740 }
741 }
742 if (cp == &procargs[size]) {
743 goto ERROR_FREE;
744 }
745
746 sp = cp;
747
748 for (np = NULL; cp < &procargs[size]; cp++) {
749 if (*cp == '\0') {
750 if (np != NULL) {
751 *np = ' ';
752 }
753 np = cp;
754 } else if (*cp == '=') {
755 break;
756 }
757 }
758
759 for (np = sp; (np < &procargs[size]) && (*np == ' '); np++);
760
761 ep->args = strdup(np);
762 free(procargs);
763 return;
764
765ERROR_FREE:
766 free(procargs);
767ERROR:
768/*
769 ep->args = malloc(2);
770 ep->args[0] = '-';
771 ep->args[1] = '\0';
772*/
773 asprintf(&ep->args, "%s", KI_PROC(ep)->p_comm);
774 return;
775}
e1a085ba 776#endif /* HAVE_KVM */