Hi,
This is gonna be my first post in CUFP, so please don't be too hard on me. :-)
I'm just looking for ways to improve my coding, so perhaps you could look at this code and tell me what I could be better to improve it. Here goes:
#!/usr/local/bin/perl5 -w use strict; use Term::ReadKey; $| = 1; $0 = "whoson"; my $userdir = $ENV{HOME}; my $clearstring = `/usr/bin/clear`; my $dotusersfile = "$userdir/.users"; my $changed = 0; my $busy = 1; my $wait = 1; my %dotusers; sub read_userfile { open(U, "<$dotusersfile") || die "Can't open $dotusersfile! ($!)\n"; my @monitor = <U>; close(U); %dotusers = (); foreach (@monitor) { chomp; $dotusers{$_}{NAME} = $_; $dotusers{$_}{LAST} = 0; } } sub catch_alarm { $wait = 0; } $SIG{ALRM} = \&catch_alarm; $SIG{HUP} = \&read_userfile; read_userfile; ReadMode('cbreak'); do { $changed = 0; open(W, "/usr/bin/w -hi |") || die "Couldn't do `w`! ($!)\n"; my @w = <W>; close(W); my %users = (); foreach (@w) { chomp; if ($_ =~ / \- hi /x) { print "$_\n"; # this is our own process. since it will always have zero idlet +ime, # we'll skip it... next; } my $user = substr($_, 0, 8); $user =~ s/\s+$//; my $idle = substr($_, 50, 6); $idle =~ s/^\s+//; if (defined($users{$user})) { # `w` was sorted by idle time. later occurrences have larger # idle times. so we don't need these. } else { $users{$user} = $idle; } } print $clearstring; foreach (sort keys %dotusers) { if (defined ($users{$_})) { if ($dotusers{$_}{LAST} == 0) { $dotusers{$_}{LAST} = 1; $changed = 1; printf "\e[1m%-10s %7s\e[0m\n", $_, $users{$_}; } else { printf "%-10s %7s\n", $_, $users{$_}; } } else { $dotusers{$_}{LAST} = 0; } } if ($changed == 1) { print "\07"; } $wait = 1; alarm 3; while ($wait == 1) { select undef,undef,undef,0.1; if (defined (my $key = ReadKey(-1)) ) { if ($key =~ m/q/i) { $wait = 0; $busy = 0; } } else { ; } } } while ($busy == 1); ReadMode('normal'); exit(0);
I use this code on our FreeBSD server at work. Normally we have about 40 users logged in at the same time and only some of them are important to me. So, I like to be able to quickly see whether or not they are logged in, and what their idle time is. Users that have been idle for 2 days, are probably not going to respond to my writes...
Thanks,
Jink

All Camels are equal, but some Camels are more equal than others.