1 |
|
|
/* $OpenBSD: w.c,v 1.63 2017/07/27 14:17:34 jca Exp $ */ |
2 |
|
|
|
3 |
|
|
/*- |
4 |
|
|
* Copyright (c) 1980, 1991, 1993, 1994 |
5 |
|
|
* The Regents of the University of California. All rights reserved. |
6 |
|
|
* |
7 |
|
|
* Redistribution and use in source and binary forms, with or without |
8 |
|
|
* modification, are permitted provided that the following conditions |
9 |
|
|
* are met: |
10 |
|
|
* 1. Redistributions of source code must retain the above copyright |
11 |
|
|
* notice, this list of conditions and the following disclaimer. |
12 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
|
|
* notice, this list of conditions and the following disclaimer in the |
14 |
|
|
* documentation and/or other materials provided with the distribution. |
15 |
|
|
* 3. Neither the name of the University nor the names of its contributors |
16 |
|
|
* may be used to endorse or promote products derived from this software |
17 |
|
|
* without specific prior written permission. |
18 |
|
|
* |
19 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
20 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
22 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
23 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
24 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
25 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
26 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
27 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
28 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
29 |
|
|
* SUCH DAMAGE. |
30 |
|
|
*/ |
31 |
|
|
|
32 |
|
|
/* |
33 |
|
|
* w - print system status (who and what) |
34 |
|
|
* |
35 |
|
|
* This program is similar to the systat command on Tenex/Tops 10/20 |
36 |
|
|
* |
37 |
|
|
*/ |
38 |
|
|
#include <sys/param.h> /* MAXCOMLEN */ |
39 |
|
|
#include <sys/time.h> |
40 |
|
|
#include <sys/stat.h> |
41 |
|
|
#include <sys/sysctl.h> |
42 |
|
|
#include <sys/signal.h> |
43 |
|
|
#include <sys/proc.h> |
44 |
|
|
#include <sys/ioctl.h> |
45 |
|
|
#include <sys/socket.h> |
46 |
|
|
#include <sys/tty.h> |
47 |
|
|
|
48 |
|
|
#include <netinet/in.h> |
49 |
|
|
#include <arpa/inet.h> |
50 |
|
|
|
51 |
|
|
#include <ctype.h> |
52 |
|
|
#include <err.h> |
53 |
|
|
#include <errno.h> |
54 |
|
|
#include <fcntl.h> |
55 |
|
|
#include <kvm.h> |
56 |
|
|
#include <netdb.h> |
57 |
|
|
#include <nlist.h> |
58 |
|
|
#include <paths.h> |
59 |
|
|
#include <stdio.h> |
60 |
|
|
#include <stdlib.h> |
61 |
|
|
#include <string.h> |
62 |
|
|
#include <unistd.h> |
63 |
|
|
#include <limits.h> |
64 |
|
|
#include <utmp.h> |
65 |
|
|
#include <vis.h> |
66 |
|
|
|
67 |
|
|
#include "extern.h" |
68 |
|
|
|
69 |
|
|
struct timeval boottime; |
70 |
|
|
struct utmp utmp; |
71 |
|
|
struct winsize ws; |
72 |
|
|
kvm_t *kd; |
73 |
|
|
time_t now; /* the current time of day */ |
74 |
|
|
int ttywidth; /* width of tty */ |
75 |
|
|
int argwidth; /* width of tty */ |
76 |
|
|
int header = 1; /* true if -h flag: don't print heading */ |
77 |
|
|
int nflag = 1; /* true if -n flag: don't convert addrs */ |
78 |
|
|
int sortidle; /* sort by idle time */ |
79 |
|
|
char *sel_user; /* login of particular user selected */ |
80 |
|
|
char domain[HOST_NAME_MAX+1]; |
81 |
|
|
|
82 |
|
|
#define NAME_WIDTH 8 |
83 |
|
|
#define HOST_WIDTH 16 |
84 |
|
|
|
85 |
|
|
/* |
86 |
|
|
* One of these per active utmp entry. |
87 |
|
|
*/ |
88 |
|
|
struct entry { |
89 |
|
|
struct entry *next; |
90 |
|
|
struct utmp utmp; |
91 |
|
|
dev_t tdev; /* dev_t of terminal */ |
92 |
|
|
time_t idle; /* idle time of terminal in seconds */ |
93 |
|
|
struct kinfo_proc *kp; /* `most interesting' proc */ |
94 |
|
|
} *ep, *ehead = NULL, **nextp = &ehead; |
95 |
|
|
|
96 |
|
|
static void fmt_putc(int, int *); |
97 |
|
|
static void fmt_puts(const char *, int *); |
98 |
|
|
static void pr_args(struct kinfo_proc *); |
99 |
|
|
static void pr_header(time_t *, int); |
100 |
|
|
static struct stat |
101 |
|
|
*ttystat(char *); |
102 |
|
|
static void usage(int); |
103 |
|
|
|
104 |
|
|
int |
105 |
|
|
main(int argc, char *argv[]) |
106 |
|
|
{ |
107 |
|
|
extern char *__progname; |
108 |
|
|
struct kinfo_proc *kp; |
109 |
|
|
struct hostent *hp; |
110 |
|
|
struct stat *stp; |
111 |
|
|
FILE *ut; |
112 |
|
2 |
struct in_addr addr; |
113 |
|
1 |
int ch, i, nentries, nusers, wcmd; |
114 |
|
|
char *memf, *nlistf, *p, *x; |
115 |
|
1 |
char buf[HOST_NAME_MAX+1], errbuf[_POSIX2_LINE_MAX]; |
116 |
|
|
|
117 |
|
|
/* Are we w(1) or uptime(1)? */ |
118 |
|
1 |
p = __progname; |
119 |
✗✓ |
1 |
if (*p == '-') |
120 |
|
|
p++; |
121 |
✗✓✗✗
|
1 |
if (p[0] == 'w' && p[1] == '\0') { |
122 |
|
|
wcmd = 1; |
123 |
|
|
p = "hiflM:N:asuw"; |
124 |
✓✗ |
1 |
} else if (!strcmp(p, "uptime")) { |
125 |
|
|
wcmd = 0; |
126 |
|
|
p = ""; |
127 |
|
|
} else |
128 |
|
|
errx(1, |
129 |
|
|
"this program should be invoked only as \"w\" or \"uptime\""); |
130 |
|
|
|
131 |
|
|
memf = nlistf = NULL; |
132 |
✗✓ |
2 |
while ((ch = getopt(argc, argv, p)) != -1) |
133 |
|
|
switch (ch) { |
134 |
|
|
case 'h': |
135 |
|
|
header = 0; |
136 |
|
|
break; |
137 |
|
|
case 'i': |
138 |
|
|
sortidle = 1; |
139 |
|
|
break; |
140 |
|
|
case 'M': |
141 |
|
|
header = 0; |
142 |
|
|
memf = optarg; |
143 |
|
|
break; |
144 |
|
|
case 'N': |
145 |
|
|
nlistf = optarg; |
146 |
|
|
break; |
147 |
|
|
case 'a': |
148 |
|
|
nflag = 0; |
149 |
|
|
break; |
150 |
|
|
case 'f': case 'l': case 's': case 'u': case 'w': |
151 |
|
|
warnx("[-flsuw] no longer supported"); |
152 |
|
|
/* FALLTHROUGH */ |
153 |
|
|
case '?': |
154 |
|
|
default: |
155 |
|
|
usage(wcmd); |
156 |
|
|
} |
157 |
|
1 |
argc -= optind; |
158 |
|
1 |
argv += optind; |
159 |
|
|
|
160 |
✗✓ |
1 |
if (nflag == 0) { |
161 |
|
|
if (pledge("stdio tty rpath dns ps vminfo flock cpath wpath", NULL) == -1) |
162 |
|
|
err(1, "pledge"); |
163 |
|
|
} else { |
164 |
✗✓ |
1 |
if (pledge("stdio tty rpath ps vminfo flock cpath wpath", NULL) == -1) |
165 |
|
|
err(1, "pledge"); |
166 |
|
|
} |
167 |
|
|
|
168 |
✓✗ |
1 |
if (nlistf == NULL && memf == NULL) { |
169 |
✗✓ |
2 |
if ((kd = kvm_openfiles(nlistf, memf, NULL, KVM_NO_FILES, |
170 |
|
1 |
errbuf)) == NULL) |
171 |
|
|
errx(1, "%s", errbuf); |
172 |
|
|
} else { |
173 |
|
|
if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL) |
174 |
|
|
errx(1, "%s", errbuf); |
175 |
|
|
} |
176 |
|
|
|
177 |
|
1 |
(void)time(&now); |
178 |
✗✓ |
1 |
if ((ut = fopen(_PATH_UTMP, "r")) == NULL) |
179 |
|
|
err(1, "%s", _PATH_UTMP); |
180 |
|
|
|
181 |
✗✓ |
1 |
if (*argv) |
182 |
|
|
sel_user = *argv; |
183 |
|
|
|
184 |
✓✓ |
31 |
for (nusers = 0; fread(&utmp, sizeof(utmp), 1, ut);) { |
185 |
✓✓ |
29 |
if (utmp.ut_name[0] == '\0') |
186 |
|
|
continue; |
187 |
|
2 |
++nusers; |
188 |
✓✗✗✗ ✗✗ |
2 |
if (wcmd == 0 || (sel_user && |
189 |
|
|
strncmp(utmp.ut_name, sel_user, UT_NAMESIZE) != 0)) |
190 |
|
|
continue; |
191 |
|
|
if ((ep = calloc(1, sizeof(*ep))) == NULL) |
192 |
|
|
err(1, NULL); |
193 |
|
|
*nextp = ep; |
194 |
|
|
nextp = &(ep->next); |
195 |
|
|
memcpy(&(ep->utmp), &utmp, sizeof(utmp)); |
196 |
|
|
if (!(stp = ttystat(ep->utmp.ut_line))) |
197 |
|
|
continue; |
198 |
|
|
ep->tdev = stp->st_rdev; |
199 |
|
|
|
200 |
|
|
/* |
201 |
|
|
* If this is the console device, attempt to ascertain |
202 |
|
|
* the true console device dev_t. |
203 |
|
|
*/ |
204 |
|
|
if (ep->tdev == 0) { |
205 |
|
|
int mib[2]; |
206 |
|
|
size_t size; |
207 |
|
|
|
208 |
|
|
mib[0] = CTL_KERN; |
209 |
|
|
mib[1] = KERN_CONSDEV; |
210 |
|
|
size = sizeof(dev_t); |
211 |
|
|
(void) sysctl(mib, 2, &ep->tdev, &size, NULL, 0); |
212 |
|
|
} |
213 |
|
|
|
214 |
|
|
if ((ep->idle = now - stp->st_atime) < 0) |
215 |
|
|
ep->idle = 0; |
216 |
|
|
} |
217 |
|
1 |
(void)fclose(ut); |
218 |
|
|
|
219 |
✓✗ |
1 |
if (header || wcmd == 0) { |
220 |
|
1 |
pr_header(&now, nusers); |
221 |
✓✗ |
1 |
if (wcmd == 0) |
222 |
|
|
exit (0); |
223 |
|
|
} |
224 |
|
|
|
225 |
|
|
#define HEADER "USER TTY FROM LOGIN@ IDLE WHAT" |
226 |
|
|
#define WUSED (sizeof(HEADER) - sizeof("WHAT")) |
227 |
|
|
(void)puts(HEADER); |
228 |
|
|
|
229 |
|
|
kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(*kp), &nentries); |
230 |
|
|
if (kp == NULL) |
231 |
|
|
errx(1, "%s", kvm_geterr(kd)); |
232 |
|
|
|
233 |
|
|
if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 && |
234 |
|
|
ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 && |
235 |
|
|
ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0) |
236 |
|
|
ttywidth = 79; |
237 |
|
|
else |
238 |
|
|
ttywidth = ws.ws_col - 1; |
239 |
|
|
argwidth = ttywidth - WUSED; |
240 |
|
|
if (argwidth < 4) |
241 |
|
|
argwidth = 8; |
242 |
|
|
|
243 |
|
|
for (i = 0; i < nentries; i++, kp++) { |
244 |
|
|
if (kp->p_psflags & (PS_EMBRYO | PS_ZOMBIE)) |
245 |
|
|
continue; |
246 |
|
|
for (ep = ehead; ep != NULL; ep = ep->next) { |
247 |
|
|
/* ftp is a special case. */ |
248 |
|
|
if (strncmp(ep->utmp.ut_line, "ftp", 3) == 0) { |
249 |
|
|
char pidstr[UT_LINESIZE-2]; |
250 |
|
|
pid_t fp; |
251 |
|
|
|
252 |
|
|
(void)strncpy(pidstr, &ep->utmp.ut_line[3], |
253 |
|
|
sizeof(pidstr) - 1); |
254 |
|
|
pidstr[sizeof(pidstr) - 1] = '\0'; |
255 |
|
|
fp = (pid_t)strtol(pidstr, NULL, 10); |
256 |
|
|
if (kp->p_pid == fp) { |
257 |
|
|
ep->kp = kp; |
258 |
|
|
break; |
259 |
|
|
} |
260 |
|
|
} else if (ep->tdev == kp->p_tdev && |
261 |
|
|
kp->p__pgid == kp->p_tpgid) { |
262 |
|
|
/* |
263 |
|
|
* Proc is in foreground of this terminal |
264 |
|
|
*/ |
265 |
|
|
if (proc_compare(ep->kp, kp)) |
266 |
|
|
ep->kp = kp; |
267 |
|
|
break; |
268 |
|
|
} |
269 |
|
|
} |
270 |
|
|
} |
271 |
|
|
/* sort by idle time */ |
272 |
|
|
if (sortidle && ehead != NULL) { |
273 |
|
|
struct entry *from = ehead, *save; |
274 |
|
|
|
275 |
|
|
ehead = NULL; |
276 |
|
|
while (from != NULL) { |
277 |
|
|
for (nextp = &ehead; |
278 |
|
|
(*nextp) && from->idle >= (*nextp)->idle; |
279 |
|
|
nextp = &(*nextp)->next) |
280 |
|
|
continue; |
281 |
|
|
save = from; |
282 |
|
|
from = from->next; |
283 |
|
|
save->next = *nextp; |
284 |
|
|
*nextp = save; |
285 |
|
|
} |
286 |
|
|
} |
287 |
|
|
|
288 |
|
|
if (!nflag) { |
289 |
|
|
if (gethostname(domain, sizeof(domain)) < 0 || |
290 |
|
|
(p = strchr(domain, '.')) == 0) |
291 |
|
|
domain[0] = '\0'; |
292 |
|
|
else { |
293 |
|
|
domain[sizeof(domain) - 1] = '\0'; |
294 |
|
|
memmove(domain, p, strlen(p) + 1); |
295 |
|
|
} |
296 |
|
|
} |
297 |
|
|
|
298 |
|
|
for (ep = ehead; ep != NULL; ep = ep->next) { |
299 |
|
|
p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-"; |
300 |
|
|
for (x = NULL, i = 0; p[i] != '\0' && i < UT_HOSTSIZE; i++) |
301 |
|
|
if (p[i] == ':') { |
302 |
|
|
x = &p[i]; |
303 |
|
|
*x++ = '\0'; |
304 |
|
|
break; |
305 |
|
|
} |
306 |
|
|
if (!nflag && inet_aton(p, &addr) && |
307 |
|
|
(hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET))) { |
308 |
|
|
if (domain[0] != '\0') { |
309 |
|
|
p = hp->h_name; |
310 |
|
|
p += strlen(hp->h_name); |
311 |
|
|
p -= strlen(domain); |
312 |
|
|
if (p > hp->h_name && |
313 |
|
|
strcasecmp(p, domain) == 0) |
314 |
|
|
*p = '\0'; |
315 |
|
|
} |
316 |
|
|
p = hp->h_name; |
317 |
|
|
} |
318 |
|
|
if (x) { |
319 |
|
|
(void)snprintf(buf, sizeof(buf), "%s:%.*s", p, |
320 |
|
|
(int)(ep->utmp.ut_host + UT_HOSTSIZE - x), x); |
321 |
|
|
p = buf; |
322 |
|
|
} |
323 |
|
|
(void)printf("%-*.*s %-2.2s %-*.*s ", |
324 |
|
|
NAME_WIDTH, UT_NAMESIZE, ep->utmp.ut_name, |
325 |
|
|
strncmp(ep->utmp.ut_line, "tty", 3) ? |
326 |
|
|
ep->utmp.ut_line : ep->utmp.ut_line + 3, |
327 |
|
|
HOST_WIDTH, HOST_WIDTH, *p ? p : "-"); |
328 |
|
|
pr_attime(&ep->utmp.ut_time, &now); |
329 |
|
|
pr_idle(ep->idle); |
330 |
|
|
pr_args(ep->kp); |
331 |
|
|
printf("\n"); |
332 |
|
|
} |
333 |
|
|
exit(0); |
334 |
|
|
} |
335 |
|
|
|
336 |
|
|
static void |
337 |
|
|
fmt_putc(int c, int *leftp) |
338 |
|
|
{ |
339 |
|
|
|
340 |
|
|
if (*leftp == 0) |
341 |
|
|
return; |
342 |
|
|
if (*leftp != -1) |
343 |
|
|
*leftp -= 1; |
344 |
|
|
putchar(c); |
345 |
|
|
} |
346 |
|
|
|
347 |
|
|
static void |
348 |
|
|
fmt_puts(const char *s, int *leftp) |
349 |
|
|
{ |
350 |
|
|
static char *v = NULL; |
351 |
|
|
static size_t maxlen = 0; |
352 |
|
|
size_t len; |
353 |
|
|
|
354 |
|
|
if (*leftp == 0) |
355 |
|
|
return; |
356 |
|
|
len = strlen(s) * 4 + 1; |
357 |
|
|
if (len > maxlen) { |
358 |
|
|
free(v); |
359 |
|
|
maxlen = 0; |
360 |
|
|
if (len < getpagesize()) |
361 |
|
|
len = getpagesize(); |
362 |
|
|
v = malloc(len); |
363 |
|
|
if (v == NULL) |
364 |
|
|
return; |
365 |
|
|
maxlen = len; |
366 |
|
|
} |
367 |
|
|
strvis(v, s, VIS_TAB | VIS_NL | VIS_CSTYLE); |
368 |
|
|
if (*leftp != -1) { |
369 |
|
|
len = strlen(v); |
370 |
|
|
if (len > *leftp) { |
371 |
|
|
v[*leftp] = '\0'; |
372 |
|
|
*leftp = 0; |
373 |
|
|
} else |
374 |
|
|
*leftp -= len; |
375 |
|
|
} |
376 |
|
|
printf("%s", v); |
377 |
|
|
} |
378 |
|
|
|
379 |
|
|
|
380 |
|
|
static void |
381 |
|
|
pr_args(struct kinfo_proc *kp) |
382 |
|
|
{ |
383 |
|
|
char **argv, *str; |
384 |
|
|
int left; |
385 |
|
|
|
386 |
|
|
if (kp == NULL) |
387 |
|
|
goto nothing; /* no matching process found */ |
388 |
|
|
left = argwidth; |
389 |
|
|
argv = kvm_getargv(kd, kp, argwidth+60); /* +60 for ftpd snip */ |
390 |
|
|
if (argv == NULL) |
391 |
|
|
goto nothing; |
392 |
|
|
|
393 |
|
|
if (*argv == NULL || **argv == '\0') { |
394 |
|
|
/* Process has zeroed argv[0], display executable name. */ |
395 |
|
|
fmt_putc('(', &left); |
396 |
|
|
fmt_puts(kp->p_comm, &left); |
397 |
|
|
fmt_putc(')', &left); |
398 |
|
|
} |
399 |
|
|
while (*argv) { |
400 |
|
|
/* |
401 |
|
|
* ftp argv[0] is in the following format: |
402 |
|
|
* ftpd: HOSTNAME: [USER/PASS: ]CMD args (ftpd) |
403 |
|
|
*/ |
404 |
|
|
if (strncmp(*argv, "ftpd:", 5) == 0) { |
405 |
|
|
if ((str = strchr(*argv + 5, ':')) != NULL) |
406 |
|
|
str = strchr(str + 1, ':'); |
407 |
|
|
if (str != NULL) { |
408 |
|
|
if ((str[0] == ':') && |
409 |
|
|
isspace((unsigned char)str[1])) |
410 |
|
|
str += 2; |
411 |
|
|
fmt_puts(str, &left); |
412 |
|
|
} else |
413 |
|
|
fmt_puts(*argv, &left); |
414 |
|
|
} else |
415 |
|
|
fmt_puts(*argv, &left); |
416 |
|
|
argv++; |
417 |
|
|
fmt_putc(' ', &left); |
418 |
|
|
} |
419 |
|
|
return; |
420 |
|
|
nothing: |
421 |
|
|
putchar('-'); |
422 |
|
|
} |
423 |
|
|
|
424 |
|
|
static void |
425 |
|
|
pr_header(time_t *nowp, int nusers) |
426 |
|
|
{ |
427 |
|
2 |
double avenrun[3]; |
428 |
|
|
time_t uptime; |
429 |
|
|
int days, hrs, i, mins; |
430 |
|
1 |
int mib[2]; |
431 |
|
1 |
size_t size; |
432 |
|
1 |
char buf[256]; |
433 |
|
|
|
434 |
|
|
/* |
435 |
|
|
* Print time of day. |
436 |
|
|
*/ |
437 |
|
1 |
(void)strftime(buf, sizeof(buf) - 1, "%l:%M%p", localtime(nowp)); |
438 |
|
1 |
buf[sizeof(buf) - 1] = '\0'; |
439 |
|
1 |
(void)printf("%s ", buf); |
440 |
|
|
|
441 |
|
|
/* |
442 |
|
|
* Print how long system has been up. |
443 |
|
|
* (Found by getting "boottime" from the kernel) |
444 |
|
|
*/ |
445 |
|
1 |
mib[0] = CTL_KERN; |
446 |
|
1 |
mib[1] = KERN_BOOTTIME; |
447 |
|
1 |
size = sizeof(boottime); |
448 |
✓✗ |
1 |
if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1) { |
449 |
|
1 |
uptime = now - boottime.tv_sec; |
450 |
✓✗ |
1 |
if (uptime > 59) { |
451 |
|
1 |
uptime += 30; |
452 |
|
1 |
days = uptime / SECSPERDAY; |
453 |
|
1 |
uptime %= SECSPERDAY; |
454 |
|
1 |
hrs = uptime / SECSPERHOUR; |
455 |
|
1 |
uptime %= SECSPERHOUR; |
456 |
|
1 |
mins = uptime / 60; |
457 |
|
1 |
(void)printf(" up"); |
458 |
✗✓ |
1 |
if (days > 0) |
459 |
|
|
(void)printf(" %d day%s,", days, |
460 |
|
|
days > 1 ? "s" : ""); |
461 |
✓✗ |
1 |
if (hrs > 0 && mins > 0) |
462 |
|
1 |
(void)printf(" %2d:%02d,", hrs, mins); |
463 |
|
|
else { |
464 |
|
|
if (hrs > 0) |
465 |
|
|
(void)printf(" %d hr%s,", |
466 |
|
|
hrs, hrs > 1 ? "s" : ""); |
467 |
|
|
if (mins > 0 || (days == 0 && hrs == 0)) |
468 |
|
|
(void)printf(" %d min%s,", |
469 |
|
|
mins, mins != 1 ? "s" : ""); |
470 |
|
|
} |
471 |
|
|
} else |
472 |
|
|
printf(" %d secs,", (int)uptime); |
473 |
|
|
} |
474 |
|
|
|
475 |
|
|
/* Print number of users logged in to system */ |
476 |
|
1 |
(void)printf(" %d user%s", nusers, nusers != 1 ? "s" : ""); |
477 |
|
|
|
478 |
|
|
/* |
479 |
|
|
* Print 1, 5, and 15 minute load averages. |
480 |
|
|
*/ |
481 |
✗✓ |
1 |
if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1) |
482 |
|
|
(void)printf(", no load average information available\n"); |
483 |
|
|
else { |
484 |
|
1 |
(void)printf(", load averages:"); |
485 |
✓✓ |
8 |
for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) { |
486 |
✓✓ |
3 |
if (i > 0) |
487 |
|
2 |
(void)printf(","); |
488 |
|
3 |
(void)printf(" %.2f", avenrun[i]); |
489 |
|
|
} |
490 |
|
1 |
(void)printf("\n"); |
491 |
|
|
} |
492 |
|
1 |
} |
493 |
|
|
|
494 |
|
|
static struct stat * |
495 |
|
|
ttystat(char *line) |
496 |
|
|
{ |
497 |
|
|
static struct stat sb; |
498 |
|
|
char ttybuf[sizeof(_PATH_DEV) + UT_LINESIZE]; |
499 |
|
|
|
500 |
|
|
/* Note, line may not be NUL-terminated */ |
501 |
|
|
(void)strlcpy(ttybuf, _PATH_DEV, sizeof(ttybuf)); |
502 |
|
|
(void)strncat(ttybuf, line, sizeof(ttybuf) - 1 - strlen(ttybuf)); |
503 |
|
|
if (stat(ttybuf, &sb)) |
504 |
|
|
return (NULL); |
505 |
|
|
return (&sb); |
506 |
|
|
} |
507 |
|
|
|
508 |
|
|
static void |
509 |
|
|
usage(int wcmd) |
510 |
|
|
{ |
511 |
|
|
if (wcmd) |
512 |
|
|
(void)fprintf(stderr, |
513 |
|
|
"usage: w [-ahi] [-M core] [-N system] [user]\n"); |
514 |
|
|
else |
515 |
|
|
(void)fprintf(stderr, |
516 |
|
|
"usage: uptime\n"); |
517 |
|
|
exit (1); |
518 |
|
|
} |