Bug in users command

Larry Wall lwall at jpl-devvax.JPL.NASA.GOV
Sat Jan 19 04:58:55 AEST 1991


In article <1126:Jan1811:17:4091 at kramden.acf.nyu.edu> brnstnd at kramden.acf.nyu.edu (Dan Bernstein) writes:
: In article <18947 at rpp386.cactus.org> jfh at rpp386.cactus.org (John F Haugh II) writes:
: > I think there is a bug in the code for "users" in the 4.3-reno
: > source.  The problem is that the first user name is always printed
: > as is the last user name.  If there is exactly one user on the
: > system, and the user is logged on more than once, you get the
: > user name twice.
: 
: Two other obvious bugs: first, the number of users is limited to 200;
: second, scmp() appears to take the wrong argument types. Here is a
: public-domain version that fixes these problems. This is derived from
: the public-domain u.c published on comp.sources.unix as part of the pty
: package; neither program was derived from Berkeley source.

And here's one in Perl.  (I recognize that hardcoding the template
for the utmp structure is a weakness.  On the other hand, it's short
and to the point.  I refer you to the definition of "Minimalism" in
the Perl book.)

#!/usr/bin/perl
$UTMP = 'A8 A8 A16 l';	# Your machine may be different.
$utlen = length(pack($UTMP,0));
open(UTMP,'/etc/utmp') || die "utmp unreadable: $!\n";;
while (read(UTMP,$utmp,$utlen)) {
    ($line,$name,$host,$time) = unpack($UTMP,$utmp);
    $seen{$name}++ if $name;
}   
print join(' ', sort keys(%seen)), "\n";

Larry Wall
lwall at jpl-devvax.jpl.nasa.gov



More information about the Comp.bugs.4bsd.ucb-fixes mailing list