Path: utzoo!utgpu!watserv1!watmath!att!pacbell.com!ucsd!sdd.hp.com!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.bugs.4bsd Subject: Re: Bug in users command Message-ID: <11104@jpl-devvax.JPL.NASA.GOV> Date: 18 Jan 91 17:58:55 GMT References: <18947@rpp386.cactus.org> <1126:Jan1811:17:4091@kramden.acf.nyu.edu> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 31 In article <1126:Jan1811:17:4091@kramden.acf.nyu.edu> brnstnd@kramden.acf.nyu.edu (Dan Bernstein) writes: : In article <18947@rpp386.cactus.org> jfh@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@jpl-devvax.jpl.nasa.gov