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

unshift(@INC, '/afs/athena/user/b/e/bert/PERL');

require 'utmp.ph';
require 'utmp.pl';

require 'ctime.pl';

sub usage {
    printf <<"END_of_usage", &UTMP_FILE, &WTMP_FILE;
usage: $0 [ -h ] [ -u | -w | -f filename ] [ display_toggles ]
	  -h	  show this help message.

        default	  use utmp file ( %s ).
	  -w	  use wtmp file ( %s ).
	  -f	  use specified file.

     diplay_toggles:
	  -o	  display offset into the file.
	  -u	  display username (default: on.)
	  -i	  display the inittab id.
	  -l	  display the terminal or line (default: on.)
	  -p	  display the process id.
	  -T	  display entry type. (default: on.)
	  -n	  display the entry type as a number.
	  -x	  display exit status of a process marked as DEAD_PROCESS.
	  -U	  display entry timestamp.
	  -t	  display the entry time, in a human-readable format.
END_of_usage
    exit(@_[0] || 1);
}

$print_user = $print_line = $print_type = 1;

ARG:
    while (@ARGV) {
	$arg = shift(@ARGV);
	($arg eq "-h") && &usage("0 but true");

	($arg eq "-u") && (&utmpname(&UTMP_FILE), next ARG);
	($arg eq "-w") && (&utmpname(&WTMP_FILE), next ARG);
	($arg eq "-f") && (&utmpname(shift(@ARGV)), next ARG);

	($arg eq "-o") && ($print_off =   !$print_off, next ARG);
	($arg eq "-u") && ($print_user =  !$print_user, next ARG);
	($arg eq "-i") && ($print_id =    !$print_id, next ARG);
	($arg eq "-l") && ($print_line =  !$print_line, next ARG);
	($arg eq "-p") && ($print_pid =   !$print_pid, next ARG);
	($arg eq "-T") && ($print_type =  !$print_type, next ARG);
	($arg eq "-n") && ($print_typeN = !$print_typeN, next ARG);
	($arg eq "-x") && ($print_exit  = !$print_exit, next ARG);
	($arg eq "-t") && ($print_time  = !$print_time, next ARG);
	($arg eq "-U") && ($print_timeU = !$print_timeU, next ARG);
	&usage;
    }

while ((@gue = &getutent) && ($#gue == 7)) {
    ($user,$id,$line,$pid,$type,$exit_trm,$exit_exit,$time) = @gue;
    printf "%5d ",   $off                     if $print_off;
    printf "%-8s ",  $user                    if $print_user;
    printf "%4s ",   $id                      if $print_id;
    printf "%-12s ", $line                    if $print_line;
    printf "%5d ",   $pid                     if $print_pid;
    printf "%-13s ", &type2str($type)         if $print_type;
    printf "%2d ",   $type                    if $print_typeN;
    printf "%5d %5d ",  $exit_trm, $exit_exit if $print_exit;
    printf "%9d ",   $time                    if $print_timeU;
    if ($print_time) {
	print " ",&ctime($time);
    } else {
	print "\n";
    }
    $off += $utmp'struct_length;
}

sub type2str {
    local($type) = @_;

    ($type == &EMPTY)         && return "EMPTY";
    ($type == &RUN_LVL)       && return "RUN_LVL";
    ($type == &BOOT_TIME)     && return "BOOT_TIME";
    ($type == &OLD_TIME)      && return "OLD_TIME";
    ($type == &NEW_TIME)      && return "NEW_TIME";
    ($type == &INIT_PROCESS)  && return "INIT_PROCESS";
    ($type == &LOGIN_PROCESS) && return "LOGIN_PROCESS";
    ($type == &USER_PROCESS)  && return "USER_PROCESS";
    ($type == &DEAD_PROCESS)  && return "DEAD_PROCESS";
    ($type == &ACCOUNTING)    && return "ACCOUNTING";
    return "???";
}
