file reorg, pathnames.h, paths.h
[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
435e8dff 25static char sccsid[] = "@(#)rwho.c 5.4 (Berkeley) %G%";
38c299b1 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))
5b93ecb3
MK
47/*
48 * this macro should be shared with ruptime.
49 */
50#define down(w,now) ((now) - (w)->wd_recvtime > 11 * 60)
2b2bf619
BJ
51
52char *ctime(), *strcpy();
38c299b1 53time_t now;
2b2bf619
BJ
54int aflg;
55
56main(argc, argv)
57 int argc;
58 char **argv;
59{
38c299b1
KB
60 extern char *optarg;
61 extern int optind;
62 int ch;
2b2bf619
BJ
63 struct direct *dp;
64 int cc, width;
65 register struct whod *w = &wd;
66 register struct whoent *we;
67 register struct myutmp *mp;
68 int f, n, i;
38c299b1 69 time_t time();
2b2bf619 70
38c299b1
KB
71 while ((ch = getopt(argc, argv, "a")) != EOF)
72 switch((char)ch) {
73 case 'a':
74 aflg = 1;
75 break;
76 case '?':
77 default:
78 fprintf(stderr, "usage: rwho [-a]\n");
79 exit(1);
80 }
435e8dff
KB
81 if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL) {
82 perror(_PATH_RWHODIR);
2b2bf619
BJ
83 exit(1);
84 }
85 mp = myutmp;
38c299b1 86 (void)time(&now);
49722e0d 87 while (dp = readdir(dirp)) {
38c299b1 88 if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
2b2bf619 89 continue;
38c299b1 90 f = open(dp->d_name, O_RDONLY);
2b2bf619
BJ
91 if (f < 0)
92 continue;
93 cc = read(f, (char *)&wd, sizeof (struct whod));
94 if (cc < WHDRSIZE) {
95 (void) close(f);
96 continue;
97 }
5b93ecb3 98 if (down(w,now)) {
2b2bf619
BJ
99 (void) close(f);
100 continue;
101 }
102 cc -= WHDRSIZE;
103 we = w->wd_we;
104 for (n = cc / sizeof (struct whoent); n > 0; n--) {
105 if (aflg == 0 && we->we_idle >= 60*60) {
106 we++;
107 continue;
108 }
109 if (nusers >= NUSERS) {
110 printf("too many users\n");
111 exit(1);
112 }
113 mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
114 (void) strcpy(mp->myhost, w->wd_hostname);
115 nusers++; we++; mp++;
116 }
117 (void) close(f);
118 }
119 qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp);
120 mp = myutmp;
121 width = 0;
122 for (i = 0; i < nusers; i++) {
26bcbdcb 123 int j = strlen(mp->myhost) + 1 + strlen(mp->myutmp.out_line);
2b2bf619
BJ
124 if (j > width)
125 width = j;
126 mp++;
127 }
128 mp = myutmp;
129 for (i = 0; i < nusers; i++) {
2269668b 130 char buf[BUFSIZ];
5b93ecb3 131 (void)sprintf(buf, "%s:%s", mp->myhost, mp->myutmp.out_line);
2b2bf619 132 printf("%-8.8s %-*s %.12s",
26bcbdcb 133 mp->myutmp.out_name,
2b2bf619
BJ
134 width,
135 buf,
26bcbdcb 136 ctime((time_t *)&mp->myutmp.out_time)+4);
2b2bf619
BJ
137 mp->myidle /= 60;
138 if (mp->myidle) {
139 if (aflg) {
140 if (mp->myidle >= 100*60)
141 mp->myidle = 100*60 - 1;
142 if (mp->myidle >= 60)
143 printf(" %2d", mp->myidle / 60);
144 else
145 printf(" ");
146 } else
147 printf(" ");
148 printf(":%02d", mp->myidle % 60);
149 }
150 printf("\n");
151 mp++;
152 }
153 exit(0);
154}
155
156utmpcmp(u1, u2)
157 struct myutmp *u1, *u2;
158{
159 int rc;
160
26bcbdcb 161 rc = strncmp(u1->myutmp.out_name, u2->myutmp.out_name, 8);
2b2bf619
BJ
162 if (rc)
163 return (rc);
164 rc = strncmp(u1->myhost, u2->myhost, 8);
165 if (rc)
166 return (rc);
26bcbdcb 167 return (strncmp(u1->myutmp.out_line, u2->myutmp.out_line, 8));
2b2bf619 168}