#!/afs/athena/contrib/perl/perl

require 'ctime.pl';

$sleeptime = $ARGV[0]; $sleeptime = 15 unless $sleeptime;

$machtype = $ENV{'HOSTTYPE'}; $machtype || chop ($machtype = `machtype`);

if ($machtype eq "rsaix") {
	$utmp_size = 64;
	$bsd = 0;
}
else {
	$utmp_size = 36;
	$bsd = 1;
}

%users = ();

$| = 1;

while (1) {
	open (UTMP,"/etc/utmp") || die $!;
	while (read (UTMP, $entry, $utmp_size)) {
		if (! $bsd) {
			die "non bsd system";
			@entries = unpack ("a8 a14 a12 s l s s l a16", $entry);
			print join('|',@entries), "\n" if $entries[0] !~ /^\0/ && 
				$entries[3] == 7; 
		}
		else {
			($line, $user, $host, $time) = unpack("a8 a8 a16 l", $entry);
			if ($line !~ /^\0/) {
				if (defined $users{$line}) {
					if ($user =~ /^\0/) {
						# user logged out
						print "$users{$line} logged out on $line on ", &ctime($time);
						delete $users{$line};
					}
					else {
						if ($user ne $users{$line}) {
							# replaced
							print "$user replaced $users{$line} on $line on ", &ctime($time);
							$users{$line} = $user;
						}
					}
				}
				else {
					if ($user !~ /^\0/) {
						# new login
						print "$user logged in on $line on ", &ctime($time);
						$users{$line} = $user;
					}
				}
			}
		}
	}
	close(UTMP);
	$otime = (stat ("/etc/utmp"))[9];
	do {
		sleep $sleeptime;
	} until $otime < (stat ("/etc/utmp"))[9];
}
