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