Add copyright
[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"
5b93ecb3
MK
24/*
25 * this macro should be shared with ruptime.
26 */
27#define down(w,now) ((now) - (w)->wd_recvtime > 11 * 60)
2b2bf619
BJ
28
29char *ctime(), *strcpy();
30int now;
31int aflg;
32
33main(argc, argv)
34 int argc;
35 char **argv;
36{
37 struct direct *dp;
38 int cc, width;
39 register struct whod *w = &wd;
40 register struct whoent *we;
41 register struct myutmp *mp;
42 int f, n, i;
43
44 argc--, argv++;
45again:
46 if (argc > 0 && !strcmp(argv[0], "-a")) {
47 argc--, argv++;
48 aflg++;
49 goto again;
50 }
51 (void) time(&now);
a82aa56a
SL
52 if (chdir(RWHODIR) < 0) {
53 perror(RWHODIR);
2b2bf619
BJ
54 exit(1);
55 }
49722e0d
CL
56 dirp = opendir(".");
57 if (dirp == NULL) {
58 perror(RWHODIR);
2b2bf619
BJ
59 exit(1);
60 }
61 mp = myutmp;
49722e0d 62 while (dp = readdir(dirp)) {
2b2bf619
BJ
63 if (dp->d_ino == 0)
64 continue;
65 if (strncmp(dp->d_name, "whod.", 5))
66 continue;
67 f = open(dp->d_name, 0);
68 if (f < 0)
69 continue;
70 cc = read(f, (char *)&wd, sizeof (struct whod));
71 if (cc < WHDRSIZE) {
72 (void) close(f);
73 continue;
74 }
5b93ecb3 75 if (down(w,now)) {
2b2bf619
BJ
76 (void) close(f);
77 continue;
78 }
79 cc -= WHDRSIZE;
80 we = w->wd_we;
81 for (n = cc / sizeof (struct whoent); n > 0; n--) {
82 if (aflg == 0 && we->we_idle >= 60*60) {
83 we++;
84 continue;
85 }
86 if (nusers >= NUSERS) {
87 printf("too many users\n");
88 exit(1);
89 }
90 mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
91 (void) strcpy(mp->myhost, w->wd_hostname);
92 nusers++; we++; mp++;
93 }
94 (void) close(f);
95 }
96 qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp);
97 mp = myutmp;
98 width = 0;
99 for (i = 0; i < nusers; i++) {
26bcbdcb 100 int j = strlen(mp->myhost) + 1 + strlen(mp->myutmp.out_line);
2b2bf619
BJ
101 if (j > width)
102 width = j;
103 mp++;
104 }
105 mp = myutmp;
106 for (i = 0; i < nusers; i++) {
107 char buf[22];
5b93ecb3 108 (void)sprintf(buf, "%s:%s", mp->myhost, mp->myutmp.out_line);
2b2bf619 109 printf("%-8.8s %-*s %.12s",
26bcbdcb 110 mp->myutmp.out_name,
2b2bf619
BJ
111 width,
112 buf,
26bcbdcb 113 ctime((time_t *)&mp->myutmp.out_time)+4);
2b2bf619
BJ
114 mp->myidle /= 60;
115 if (mp->myidle) {
116 if (aflg) {
117 if (mp->myidle >= 100*60)
118 mp->myidle = 100*60 - 1;
119 if (mp->myidle >= 60)
120 printf(" %2d", mp->myidle / 60);
121 else
122 printf(" ");
123 } else
124 printf(" ");
125 printf(":%02d", mp->myidle % 60);
126 }
127 printf("\n");
128 mp++;
129 }
130 exit(0);
131}
132
133utmpcmp(u1, u2)
134 struct myutmp *u1, *u2;
135{
136 int rc;
137
26bcbdcb 138 rc = strncmp(u1->myutmp.out_name, u2->myutmp.out_name, 8);
2b2bf619
BJ
139 if (rc)
140 return (rc);
141 rc = strncmp(u1->myhost, u2->myhost, 8);
142 if (rc)
143 return (rc);
26bcbdcb 144 return (strncmp(u1->myutmp.out_line, u2->myutmp.out_line, 8));
2b2bf619 145}