rwho.c (1c8af8787354e20c2b38cab5801698133ff8b403) rwho.c (157f1c6c00f74ba3919dbdc0d79ac77cd4a4297b)
1/*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 18 unchanged lines hidden (view full) ---

27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
1/*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 18 unchanged lines hidden (view full) ---

27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char copyright[] =
35static const char copyright[] =
36"@(#) Copyright (c) 1983, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
36"@(#) Copyright (c) 1983, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
41static char sccsid[] = "@(#)rwho.c 8.1 (Berkeley) 6/6/93";
42static char sccsid[] = "@(#)rwho.c 8.1 (Berkeley) 6/6/93";
43#endif
44static const char rcsid[] =
45 "$Id$";
42#endif /* not lint */
43
44#include <sys/param.h>
45#include <sys/file.h>
46#include <protocols/rwhod.h>
47#include <dirent.h>
46#endif /* not lint */
47
48#include <sys/param.h>
49#include <sys/file.h>
50#include <protocols/rwhod.h>
51#include <dirent.h>
52#include <err.h>
48#include <locale.h>
49#include <stdio.h>
53#include <locale.h>
54#include <stdio.h>
55#include <stdlib.h>
50#include <string.h>
51#include <time.h>
56#include <string.h>
57#include <time.h>
58#include <unistd.h>
52#include <utmp.h>
53
54DIR *dirp;
55
56struct whod wd;
57int utmpcmp();
58#define NUSERS 1000
59struct myutmp {

--- 7 unchanged lines hidden (view full) ---

67/*
68 * this macro should be shared with ruptime.
69 */
70#define down(w,now) ((now) - (w)->wd_recvtime > 11 * 60)
71
72time_t now;
73int aflg;
74
59#include <utmp.h>
60
61DIR *dirp;
62
63struct whod wd;
64int utmpcmp();
65#define NUSERS 1000
66struct myutmp {

--- 7 unchanged lines hidden (view full) ---

74/*
75 * this macro should be shared with ruptime.
76 */
77#define down(w,now) ((now) - (w)->wd_recvtime > 11 * 60)
78
79time_t now;
80int aflg;
81
82static void usage __P((void));
83int utmpcmp __P((struct myutmp *, struct myutmp *));
84
85int
75main(argc, argv)
76 int argc;
77 char **argv;
78{
86main(argc, argv)
87 int argc;
88 char **argv;
89{
79 extern char *optarg;
80 extern int optind;
81 int ch;
82 struct dirent *dp;
83 int cc, width;
84 register struct whod *w = &wd;
85 register struct whoent *we;
86 register struct myutmp *mp;
87 int f, n, i;
88
89 (void) setlocale(LC_TIME, "");
90
91 while ((ch = getopt(argc, argv, "a")) != -1)
92 switch((char)ch) {
93 case 'a':
94 aflg = 1;
95 break;
96 case '?':
97 default:
90 int ch;
91 struct dirent *dp;
92 int cc, width;
93 register struct whod *w = &wd;
94 register struct whoent *we;
95 register struct myutmp *mp;
96 int f, n, i;
97
98 (void) setlocale(LC_TIME, "");
99
100 while ((ch = getopt(argc, argv, "a")) != -1)
101 switch((char)ch) {
102 case 'a':
103 aflg = 1;
104 break;
105 case '?':
106 default:
98 fprintf(stderr, "usage: rwho [-a]\n");
99 exit(1);
107 usage();
100 }
108 }
101 if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL) {
102 perror(_PATH_RWHODIR);
103 exit(1);
104 }
109 if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL)
110 err(1, "%s", _PATH_RWHODIR);
105 mp = myutmp;
106 (void)time(&now);
111 mp = myutmp;
112 (void)time(&now);
107 while (dp = readdir(dirp)) {
113 while ((dp = readdir(dirp))) {
108 if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
109 continue;
110 f = open(dp->d_name, O_RDONLY);
111 if (f < 0)
112 continue;
113 cc = read(f, (char *)&wd, sizeof (struct whod));
114 if (cc < WHDRSIZE) {
115 (void) close(f);

--- 5 unchanged lines hidden (view full) ---

121 }
122 cc -= WHDRSIZE;
123 we = w->wd_we;
124 for (n = cc / sizeof (struct whoent); n > 0; n--) {
125 if (aflg == 0 && we->we_idle >= 60*60) {
126 we++;
127 continue;
128 }
114 if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
115 continue;
116 f = open(dp->d_name, O_RDONLY);
117 if (f < 0)
118 continue;
119 cc = read(f, (char *)&wd, sizeof (struct whod));
120 if (cc < WHDRSIZE) {
121 (void) close(f);

--- 5 unchanged lines hidden (view full) ---

127 }
128 cc -= WHDRSIZE;
129 we = w->wd_we;
130 for (n = cc / sizeof (struct whoent); n > 0; n--) {
131 if (aflg == 0 && we->we_idle >= 60*60) {
132 we++;
133 continue;
134 }
129 if (nusers >= NUSERS) {
130 printf("too many users\n");
131 exit(1);
132 }
135 if (nusers >= NUSERS)
136 errx(1, "too many users");
133 mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
134 (void) strcpy(mp->myhost, w->wd_hostname);
135 nusers++; we++; mp++;
136 }
137 (void) close(f);
138 }
139 qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp);
140 mp = myutmp;

--- 31 unchanged lines hidden (view full) ---

172 printf(":%02d", mp->myidle % 60);
173 }
174 printf("\n");
175 mp++;
176 }
177 exit(0);
178}
179
137 mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
138 (void) strcpy(mp->myhost, w->wd_hostname);
139 nusers++; we++; mp++;
140 }
141 (void) close(f);
142 }
143 qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp);
144 mp = myutmp;

--- 31 unchanged lines hidden (view full) ---

176 printf(":%02d", mp->myidle % 60);
177 }
178 printf("\n");
179 mp++;
180 }
181 exit(0);
182}
183
184
185static void
186usage()
187{
188 fprintf(stderr, "usage: rwho [-a]\n");
189 exit(1);
190}
191
192int
180utmpcmp(u1, u2)
181 struct myutmp *u1, *u2;
182{
183 int rc;
184
185 rc = strncmp(u1->myutmp.out_name, u2->myutmp.out_name, sizeof(u2->myutmp.out_name));
186 if (rc)
187 return (rc);
188 rc = strcmp(u1->myhost, u2->myhost);
189 if (rc)
190 return (rc);
191 return (strncmp(u1->myutmp.out_line, u2->myutmp.out_line, sizeof(u2->myutmp.out_line)));
192}
193utmpcmp(u1, u2)
194 struct myutmp *u1, *u2;
195{
196 int rc;
197
198 rc = strncmp(u1->myutmp.out_name, u2->myutmp.out_name, sizeof(u2->myutmp.out_name));
199 if (rc)
200 return (rc);
201 rc = strcmp(u1->myhost, u2->myhost);
202 if (rc)
203 return (rc);
204 return (strncmp(u1->myutmp.out_line, u2->myutmp.out_line, sizeof(u2->myutmp.out_line)));
205}