]> git.saurik.com Git - apple/network_cmds.git/blame - rwho.tproj/rwho.c
network_cmds-201.tar.gz
[apple/network_cmds.git] / rwho.tproj / rwho.c
CommitLineData
b7080c8e
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
07f47057
A
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
b7080c8e
A
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
07f47057
A
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
b7080c8e
A
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25/*
26 * Copyright (c) 1983, 1993
27 * The Regents of the University of California. All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 */
57
58
59#ifndef lint
60static char copyright[] =
61"@(#) Copyright (c) 1983, 1993\n\
62 The Regents of the University of California. All rights reserved.\n";
63#endif /* not lint */
64
65#include <sys/param.h>
66#include <sys/dir.h>
67#include <sys/file.h>
68#include <protocols/rwhod.h>
69#include <stdio.h>
70
71DIR *dirp;
72
73struct whod wd;
74int utmpcmp();
75#define NUSERS 1000
76struct myutmp {
77 char myhost[MAXHOSTNAMELEN];
78 int myidle;
79 struct outmp myutmp;
80} myutmp[NUSERS];
81int nusers;
82
83#define WHDRSIZE (sizeof (wd) - sizeof (wd.wd_we))
84/*
85 * this macro should be shared with ruptime.
86 */
87#define down(w,now) ((now) - (w)->wd_recvtime > 11 * 60)
88
89char *ctime(), *strcpy();
90time_t now;
91int aflg;
92
93main(argc, argv)
94 int argc;
95 char **argv;
96{
97 extern char *optarg;
98 extern int optind;
99 int ch;
100 struct direct *dp;
101 int cc, width;
102 register struct whod *w = &wd;
103 register struct whoent *we;
104 register struct myutmp *mp;
105 int f, n, i;
106 time_t time();
107
108 while ((ch = getopt(argc, argv, "a")) != EOF)
109 switch((char)ch) {
110 case 'a':
111 aflg = 1;
112 break;
113 case '?':
114 default:
115 fprintf(stderr, "usage: rwho [-a]\n");
116 exit(1);
117 }
118 if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL) {
119 perror(_PATH_RWHODIR);
120 exit(1);
121 }
122 mp = myutmp;
123 (void)time(&now);
124 while (dp = readdir(dirp)) {
125 if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
126 continue;
127 f = open(dp->d_name, O_RDONLY);
128 if (f < 0)
129 continue;
130 cc = read(f, (char *)&wd, sizeof (struct whod));
131 if (cc < WHDRSIZE) {
132 (void) close(f);
133 continue;
134 }
135 if (down(w,now)) {
136 (void) close(f);
137 continue;
138 }
139 cc -= WHDRSIZE;
140 we = w->wd_we;
141 for (n = cc / sizeof (struct whoent); n > 0; n--) {
142 if (aflg == 0 && we->we_idle >= 60*60) {
143 we++;
144 continue;
145 }
146 if (nusers >= NUSERS) {
147 printf("too many users\n");
148 exit(1);
149 }
150 mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
151 (void) strcpy(mp->myhost, w->wd_hostname);
152 nusers++; we++; mp++;
153 }
154 (void) close(f);
155 }
156 qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp);
157 mp = myutmp;
158 width = 0;
159 for (i = 0; i < nusers; i++) {
160 int j = strlen(mp->myhost) + 1 + strlen(mp->myutmp.out_line);
161 if (j > width)
162 width = j;
163 mp++;
164 }
165 mp = myutmp;
166 for (i = 0; i < nusers; i++) {
167 char buf[BUFSIZ];
168 (void)sprintf(buf, "%s:%s", mp->myhost, mp->myutmp.out_line);
169 printf("%-8.8s %-*s %.12s",
170 mp->myutmp.out_name,
171 width,
172 buf,
173 ctime((time_t *)&mp->myutmp.out_time)+4);
174 mp->myidle /= 60;
175 if (mp->myidle) {
176 if (aflg) {
177 if (mp->myidle >= 100*60)
178 mp->myidle = 100*60 - 1;
179 if (mp->myidle >= 60)
180 printf(" %2d", mp->myidle / 60);
181 else
182 printf(" ");
183 } else
184 printf(" ");
185 printf(":%02d", mp->myidle % 60);
186 }
187 printf("\n");
188 mp++;
189 }
190 exit(0);
191}
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, 8);
199 if (rc)
200 return (rc);
201 rc = strncmp(u1->myhost, u2->myhost, 8);
202 if (rc)
203 return (rc);
204 return (strncmp(u1->myutmp.out_line, u2->myutmp.out_line, 8));
205}