Wait 11 minutes to decide a machine is down (same as ruptime).
[unix-history] / usr / src / usr.bin / rwho / rwho.c
CommitLineData
2b2bf619 1#ifndef lint
36b77661 2static char sccsid[] = "@(#)rwho.c 4.9 (Berkeley) 85/05/07";
2b2bf619
BJ
3#endif
4
ed0072ae 5#include <sys/param.h>
2b2bf619 6#include <stdio.h>
7123e2dc
SL
7#include <sys/dir.h>
8#include "../etc/rwhod/rwhod.h"
2b2bf619 9
49722e0d 10DIR *dirp;
2b2bf619
BJ
11
12struct whod wd;
13int utmpcmp();
14#define NUSERS 1000
15struct myutmp {
16 char myhost[32];
17 int myidle;
26bcbdcb 18 struct outmp myutmp;
2b2bf619
BJ
19} myutmp[NUSERS];
20int nusers;
21
22#define WHDRSIZE (sizeof (wd) - sizeof (wd.wd_we))
fe77431f 23#define RWHODIR "/usr/spool/rwho"
2b2bf619
BJ
24
25char *ctime(), *strcpy();
26int now;
27int aflg;
28
29main(argc, argv)
30 int argc;
31 char **argv;
32{
33 struct direct *dp;
34 int cc, width;
35 register struct whod *w = &wd;
36 register struct whoent *we;
37 register struct myutmp *mp;
38 int f, n, i;
39
40 argc--, argv++;
41again:
42 if (argc > 0 && !strcmp(argv[0], "-a")) {
43 argc--, argv++;
44 aflg++;
45 goto again;
46 }
47 (void) time(&now);
a82aa56a
SL
48 if (chdir(RWHODIR) < 0) {
49 perror(RWHODIR);
2b2bf619
BJ
50 exit(1);
51 }
49722e0d
CL
52 dirp = opendir(".");
53 if (dirp == NULL) {
54 perror(RWHODIR);
2b2bf619
BJ
55 exit(1);
56 }
57 mp = myutmp;
49722e0d 58 while (dp = readdir(dirp)) {
2b2bf619
BJ
59 if (dp->d_ino == 0)
60 continue;
61 if (strncmp(dp->d_name, "whod.", 5))
62 continue;
63 f = open(dp->d_name, 0);
64 if (f < 0)
65 continue;
66 cc = read(f, (char *)&wd, sizeof (struct whod));
67 if (cc < WHDRSIZE) {
68 (void) close(f);
69 continue;
70 }
71 if (now - w->wd_recvtime > 5 * 60) {
72 (void) close(f);
73 continue;
74 }
75 cc -= WHDRSIZE;
76 we = w->wd_we;
77 for (n = cc / sizeof (struct whoent); n > 0; n--) {
78 if (aflg == 0 && we->we_idle >= 60*60) {
79 we++;
80 continue;
81 }
82 if (nusers >= NUSERS) {
83 printf("too many users\n");
84 exit(1);
85 }
86 mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
87 (void) strcpy(mp->myhost, w->wd_hostname);
88 nusers++; we++; mp++;
89 }
90 (void) close(f);
91 }
92 qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp);
93 mp = myutmp;
94 width = 0;
95 for (i = 0; i < nusers; i++) {
26bcbdcb 96 int j = strlen(mp->myhost) + 1 + strlen(mp->myutmp.out_line);
2b2bf619
BJ
97 if (j > width)
98 width = j;
99 mp++;
100 }
101 mp = myutmp;
102 for (i = 0; i < nusers; i++) {
103 char buf[22];
26bcbdcb 104 sprintf(buf, "%s:%s", mp->myhost, mp->myutmp.out_line);
2b2bf619 105 printf("%-8.8s %-*s %.12s",
26bcbdcb 106 mp->myutmp.out_name,
2b2bf619
BJ
107 width,
108 buf,
26bcbdcb 109 ctime((time_t *)&mp->myutmp.out_time)+4);
2b2bf619
BJ
110 mp->myidle /= 60;
111 if (mp->myidle) {
112 if (aflg) {
113 if (mp->myidle >= 100*60)
114 mp->myidle = 100*60 - 1;
115 if (mp->myidle >= 60)
116 printf(" %2d", mp->myidle / 60);
117 else
118 printf(" ");
119 } else
120 printf(" ");
121 printf(":%02d", mp->myidle % 60);
122 }
123 printf("\n");
124 mp++;
125 }
126 exit(0);
127}
128
129utmpcmp(u1, u2)
130 struct myutmp *u1, *u2;
131{
132 int rc;
133
26bcbdcb 134 rc = strncmp(u1->myutmp.out_name, u2->myutmp.out_name, 8);
2b2bf619
BJ
135 if (rc)
136 return (rc);
137 rc = strncmp(u1->myhost, u2->myhost, 8);
138 if (rc)
139 return (rc);
26bcbdcb 140 return (strncmp(u1->myutmp.out_line, u2->myutmp.out_line, 8));
2b2bf619 141}