1 /* $NetBSD: rwho.c,v 1.18 2008/07/21 14:19:25 lukem Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
35 The Regents of the University of California. All rights reserved.");
39 /*static char sccsid[] = "from: @(#)rwho.c 8.1 (Berkeley) 6/6/93";*/
40 __RCSID("$NetBSD: rwho.c,v 1.18 2008/07/21 14:19:25 lukem Exp $");
43 #include <sys/param.h>
46 #include <protocols/rwhod.h>
60 static void usage(void) __dead
;
61 static int utmpcmp(const void *, const void *);
64 * this macro should be shared with ruptime.
66 #define DOWN(w,now) ((now) - (w).wd_recvtime > 11 * 60)
67 #define WHDRSIZE (sizeof(struct whod) - \
68 sizeof (((struct whod *)NULL)->wd_we))
71 char myhost
[MAXHOSTNAMELEN
];
77 main(int argc
, char **argv
)
83 struct my_utmp
*mp
, *start
;
86 size_t width
, n
, i
, nhosts
, nusers
, namount
;
90 (void)setlocale(LC_TIME
, "");
91 aflg
= nusers
= nhosts
= qflg
= hflg
= 0;
96 while ((ch
= getopt(argc
, argv
, "aHq")) != -1)
119 if (chdir(_PATH_RWHODIR
) || (dirp
= opendir(".")) == NULL
)
120 err(EXIT_FAILURE
, "Cannot access `%s'", _PATH_RWHODIR
);
124 if ((start
= malloc(sizeof(*start
) * namount
)) == NULL
)
125 err(EXIT_FAILURE
, "malloc");
127 while ((dp
= readdir(dirp
)) != NULL
) {
131 if (dp
->d_ino
== 0 || strncmp(dp
->d_name
, "whod.", 5))
134 f
= open(dp
->d_name
, O_RDONLY
);
137 cc
= read(f
, &wd
, sizeof (wd
));
138 if (cc
< (ssize_t
)WHDRSIZE
) {
149 for (n
= cc
/ sizeof (struct whoent
); n
> 0; n
--) {
150 if (aflg
== 0 && we
->we_idle
>= 60*60) {
155 if (nusers
== namount
) {
156 namount
= namount
+ 40;
157 if ((start
= realloc(start
,
158 sizeof(struct my_utmp
) * namount
)) == NULL
)
164 mp
->myutmp
= we
->we_utmp
;
165 mp
->myidle
= we
->we_idle
;
166 (void)strcpy(mp
->myhost
, wd
.wd_hostname
);
167 nusers
++; we
++; mp
++;
173 errx(EX_OK
, "No hosts in `%s'.", _PATH_RWHODIR
);
176 qsort(start
, nusers
, sizeof(*start
), utmpcmp
);
178 for (i
= 0; i
< nusers
; i
++) {
179 size_t j
= strlen(mp
->myhost
) + 1 + strlen(mp
->myutmp
.out_line
);
186 (void)printf("%-*.*s %-*s %-12s %-*s\n", UT_NAMESIZE
,
187 UT_NAMESIZE
, "USER", (int)width
, "LINE", "WHEN",
190 for (i
= 0; i
< nusers
; i
++) {
191 char buf
[BUFSIZ
], cbuf
[80];
195 (void)printf("%-*.*s\n", UT_NAMESIZE
, UT_NAMESIZE
,
196 mp
->myutmp
.out_name
);
200 t
= mp
->myutmp
.out_time
;
201 (void)strftime(cbuf
, sizeof(cbuf
), "%c", localtime(&t
));
202 (void)snprintf(buf
, sizeof(buf
), "%s:%s", mp
->myhost
,
203 mp
->myutmp
.out_line
);
204 (void)printf("%-*.*s %-*s %.12s", UT_NAMESIZE
, UT_NAMESIZE
,
205 mp
->myutmp
.out_name
, (int)width
, buf
, cbuf
+ 4);
209 if (mp
->myidle
>= 100 * 60)
210 mp
->myidle
= 100 * 60 - 1;
211 if (mp
->myidle
>= 60)
212 (void)printf(" %2d", mp
->myidle
/ 60);
217 (void)printf(":%02d", mp
->myidle
% 60);
224 (void)printf("# users = %zd\n", nusers
);
230 utmpcmp(const void *v1
, const void *v2
)
232 const struct my_utmp
*u1
, *u2
;
237 rc
= strncmp(u1
->myutmp
.out_name
, u2
->myutmp
.out_name
, 8);
240 rc
= strcmp(u1
->myhost
, u2
->myhost
);
243 return strncmp(u1
->myutmp
.out_line
, u2
->myutmp
.out_line
, 8);
249 (void)fprintf(stderr
, "Usage: %s [-aHq]\n", getprogname());