#include <sys/param.h>
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <time.h>
#include <unistd.h>
#include <protocols/rwhod.h>
static DIR *dirp;
static struct whod wd;
#define NUSERS 1000
static struct myutmp {
char myhost[32];
int myidle;
struct outmp myutmp;
} myutmp[NUSERS];
static int utmpcmp(const void *, const void *);
static int nusers;
#define WHDRSIZE (sizeof (wd) - sizeof (wd.wd_we))
#define RWHODIR "/var/spool/rwho"
#define down(w, now) ((now) - (w)->wd_recvtime > 11 * 60)
static time_t now;
static int aflg = 0;
int
main(int argc, char **argv)
{
struct dirent *dp;
int cc, width;
register struct whod *w = &wd;
register struct whoent *we;
register struct myutmp *mp;
int f, n, i;
argc--, argv++;
again:
if (argc > 0 && strcmp(argv[0], "-a") == 0) {
argc--, argv++;
aflg++;
goto again;
}
(void) time(&now);
if (chdir(RWHODIR) < 0) {
perror(RWHODIR);
return (EXIT_FAILURE);
}
dirp = opendir(".");
if (dirp == NULL) {
perror(RWHODIR);
return (EXIT_FAILURE);
}
mp = myutmp;
while (dp = readdir(dirp)) {
if (dp->d_ino == 0)
continue;
if (strncmp(dp->d_name, "whod.", 5))
continue;
f = open(dp->d_name, 0);
if (f < 0)
continue;
cc = read(f, (char *)&wd, sizeof (struct whod));
if (cc < WHDRSIZE) {
(void) close(f);
continue;
}
if (down(w, now)) {
(void) close(f);
continue;
}
cc -= WHDRSIZE;
we = w->wd_we;
for (n = cc / sizeof (struct whoent); n > 0; n--) {
if (aflg == 0 && we->we_idle >= 60*60) {
we++;
continue;
}
if (nusers >= NUSERS) {
(void) printf("too many users\n");
return (EXIT_FAILURE);
}
mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
(void) strncpy(mp->myhost, w->wd_hostname,
sizeof (mp->myhost));
nusers++; we++; mp++;
}
(void) close(f);
}
qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp);
mp = myutmp;
width = 0;
for (i = 0; i < nusers; i++) {
int j = strlen(mp->myhost) + 1 + strlen(mp->myutmp.out_line);
if (j > width)
width = j;
mp++;
}
mp = myutmp;
for (i = 0; i < nusers; i++) {
char buf[BUFSIZ];
(void) snprintf(buf, BUFSIZ, "%.*s:%.*s",
sizeof (mp->myhost), mp->myhost,
sizeof (mp->myutmp.out_line), mp->myutmp.out_line);
(void) printf("%-8.*s %-*s %.12s",
sizeof (mp->myutmp.out_name), mp->myutmp.out_name,
width, buf,
ctime((time_t *)&mp->myutmp.out_time) + 4);
mp->myidle /= 60;
if (mp->myidle) {
if (aflg) {
if (mp->myidle >= 100*60)
mp->myidle = 100*60 - 1;
if (mp->myidle >= 60)
(void) printf(" %2d", mp->myidle / 60);
else
(void) fputs(" ", stdout);
} else
(void) printf(" ");
(void) printf(":%02d", mp->myidle % 60);
}
(void) puts("");
mp++;
}
return (EXIT_SUCCESS);
}
static int
utmpcmp(const void *p1, const void *p2)
{
const struct myutmp *u1 = p1, *u2 = p2;
int rc;
rc = strncmp(u1->myutmp.out_name, u2->myutmp.out_name,
sizeof (u1->myutmp.out_name));
if (rc != 0)
return (rc);
rc = strncmp(u1->myhost, u2->myhost, sizeof (u1->myhost));
if (rc != 0)
return (rc);
return (strncmp(u1->myutmp.out_line, u2->myutmp.out_line,
sizeof (u1->myutmp.out_line)));
}