add manual pages; don't produce .o's unless necessary; add new labels;
[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 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22e155fc
DF
16 */
17
18#ifndef lint
19char copyright[] =
38c299b1 20"@(#) Copyright (c) 1983 The Regents of the University of California.\n\
22e155fc 21 All rights reserved.\n";
38c299b1 22#endif /* not lint */
22e155fc 23
2b2bf619 24#ifndef lint
38c299b1
KB
25static char sccsid[] = "@(#)rwho.c 5.3 (Berkeley) %G%";
26#endif /* not lint */
2b2bf619 27
ed0072ae 28#include <sys/param.h>
7123e2dc 29#include <sys/dir.h>
38c299b1 30#include <sys/file.h>
5a7285fa 31#include <protocols/rwhod.h>
38c299b1 32#include <stdio.h>
2b2bf619 33
49722e0d 34DIR *dirp;
2b2bf619
BJ
35
36struct whod wd;
37int utmpcmp();
38#define NUSERS 1000
39struct myutmp {
38c299b1 40 char myhost[MAXHOSTNAMELEN];
2b2bf619 41 int myidle;
26bcbdcb 42 struct outmp myutmp;
2b2bf619
BJ
43} myutmp[NUSERS];
44int nusers;
45
46#define WHDRSIZE (sizeof (wd) - sizeof (wd.wd_we))
fe77431f 47#define RWHODIR "/usr/spool/rwho"
5b93ecb3
MK
48/*
49 * this macro should be shared with ruptime.
50 */
51#define down(w,now) ((now) - (w)->wd_recvtime > 11 * 60)
2b2bf619
BJ
52
53char *ctime(), *strcpy();
38c299b1 54time_t now;
2b2bf619
BJ
55int aflg;
56
57main(argc, argv)
58 int argc;
59 char **argv;
60{
38c299b1
KB
61 extern char *optarg;
62 extern int optind;
63 int ch;
2b2bf619
BJ
64 struct direct *dp;
65 int cc, width;
66 register struct whod *w = &wd;
67 register struct whoent *we;
68 register struct myutmp *mp;
69 int f, n, i;
38c299b1 70 time_t time();
2b2bf619 71
38c299b1
KB
72 while ((ch = getopt(argc, argv, "a")) != EOF)
73 switch((char)ch) {
74 case 'a':
75 aflg = 1;
76 break;
77 case '?':
78 default:
79 fprintf(stderr, "usage: rwho [-a]\n");
80 exit(1);
81 }
82 if (chdir(RWHODIR) || (dirp = opendir(".")) == NULL) {
49722e0d 83 perror(RWHODIR);
2b2bf619
BJ
84 exit(1);
85 }
86 mp = myutmp;
38c299b1 87 (void)time(&now);
49722e0d 88 while (dp = readdir(dirp)) {
38c299b1 89 if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
2b2bf619 90 continue;
38c299b1 91 f = open(dp->d_name, O_RDONLY);
2b2bf619
BJ
92 if (f < 0)
93 continue;
94 cc = read(f, (char *)&wd, sizeof (struct whod));
95 if (cc < WHDRSIZE) {
96 (void) close(f);
97 continue;
98 }
5b93ecb3 99 if (down(w,now)) {
2b2bf619
BJ
100 (void) close(f);
101 continue;
102 }
103 cc -= WHDRSIZE;
104 we = w->wd_we;
105 for (n = cc / sizeof (struct whoent); n > 0; n--) {
106 if (aflg == 0 && we->we_idle >= 60*60) {
107 we++;
108 continue;
109 }
110 if (nusers >= NUSERS) {
111 printf("too many users\n");
112 exit(1);
113 }
114 mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
115 (void) strcpy(mp->myhost, w->wd_hostname);
116 nusers++; we++; mp++;
117 }
118 (void) close(f);
119 }
120 qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp);
121 mp = myutmp;
122 width = 0;
123 for (i = 0; i < nusers; i++) {
26bcbdcb 124 int j = strlen(mp->myhost) + 1 + strlen(mp->myutmp.out_line);
2b2bf619
BJ
125 if (j > width)
126 width = j;
127 mp++;
128 }
129 mp = myutmp;
130 for (i = 0; i < nusers; i++) {
2269668b 131 char buf[BUFSIZ];
5b93ecb3 132 (void)sprintf(buf, "%s:%s", mp->myhost, mp->myutmp.out_line);
2b2bf619 133 printf("%-8.8s %-*s %.12s",
26bcbdcb 134 mp->myutmp.out_name,
2b2bf619
BJ
135 width,
136 buf,
26bcbdcb 137 ctime((time_t *)&mp->myutmp.out_time)+4);
2b2bf619
BJ
138 mp->myidle /= 60;
139 if (mp->myidle) {
140 if (aflg) {
141 if (mp->myidle >= 100*60)
142 mp->myidle = 100*60 - 1;
143 if (mp->myidle >= 60)
144 printf(" %2d", mp->myidle / 60);
145 else
146 printf(" ");
147 } else
148 printf(" ");
149 printf(":%02d", mp->myidle % 60);
150 }
151 printf("\n");
152 mp++;
153 }
154 exit(0);
155}
156
157utmpcmp(u1, u2)
158 struct myutmp *u1, *u2;
159{
160 int rc;
161
26bcbdcb 162 rc = strncmp(u1->myutmp.out_name, u2->myutmp.out_name, 8);
2b2bf619
BJ
163 if (rc)
164 return (rc);
165 rc = strncmp(u1->myhost, u2->myhost, 8);
166 if (rc)
167 return (rc);
26bcbdcb 168 return (strncmp(u1->myutmp.out_line, u2->myutmp.out_line, 8));
2b2bf619 169}