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