[alt.sources] w command w/ whence field

tchrist@convexe.uucp (Tom Christiansen) (12/18/89)

here's a script that merges w output with that of who.  it's a good
demo of handling pipes and dynamic code generation.  here's some
output:

  3:51pm  up 2 days, 5 mins,  7 users,  load average: 2.02, 1.79, 1.55
User     tty     whence     login@  idle   JCPU   PCPU  what
cfarmer  ttyd2              2:41pm           39     38  convexs
tchrist  ttyp0   concave    3:31pm           40      9  /usr/ucb/w
tweton   ttyp1   arrow      1:26pm    21     12     12  rlogin floyd
herod    ttyp3   ramses     2:37pm  1:10      5      1  -ksh
tchrist  ttyq0   cthulhu    3:48pm 23:24     17     16  monthd -i5
brandon  ttyqe   oblio     12:47pm 26:04     17      3  mail -f

this and the other two scripts i'm posting now will work with
both perl2 and perl3.

#!/usr/local/bin/perl

$cols = ($ENV{"TERMCAP"} =~ /:co#(\d+):/) ? $1 : 80;

$pipe = "/bin/who|";
open pipe || die "$0: can't open $pipe: $!\n";

while (<pipe>) {
    chop; split;
    $users{$_[1]} = $_[0];
    next unless $#_ > 4;
    ( $whence = $_[5] ) =~ s/^\((.*)\)$/\1/;
    $whence =~ s/(.*):.*/$1/;
    $whence =~ s/(................).*/$1/;
    $whence{$_[1]} = $whence;
} 

$pipe = "/usr/ucb/w|"; open pipe || die "$0: can't open $pipe: $!\n";
$_ = <pipe>; print;

$_ = <pipe>; 
s/^(.................).(.*)$/$1whence    $2/;
print;

while (<pipe>) {
    chop; split;
    $whence = ($users{$_[1]} eq $_[0]) ? $whence{$_[1]} : "(new login)";
    /(.................).(.*)/;
    $line = sprintf ("%s%-10s%s", $1, $whence, $2);
    eval '$line =~ s/^(' . ('.' x $cols) . ').*/$1/';
    print $line,"\n";
} 

    Tom Christiansen                       {uunet,uiucdcs,sun}!convex!tchrist 
    Convex Computer Corporation                            tchrist@convex.COM
		 "EMACS belongs in <sys/errno.h>: Editor too big!"