Bug in users command
Dan Bernstein
brnstnd at kramden.acf.nyu.edu
Fri Jan 18 22:17:40 AEST 1991
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.
---Dan
/* Public domain. */
#include <stdio.h>
#include <utmp.h>
#include <strings.h>
extern char *malloc();
#define PTYUTMP_FILE "/etc/utmp"
int compar(s,t)
char (**s)[8];
char (**t)[8];
{
return -strncmp(&(**s)[0],&(**t)[0],8);
}
main()
{
register FILE *fi;
struct utmp ut;
char (*us)[8];
char (**up)[8];
int lines = 0;
int i = 0;
if (!(fi = fopen(PTYUTMP_FILE,"r")))
{
fprintf(stderr,"users: cannot open %s\n",PTYUTMP_FILE);
exit(1);
}
while (fread((char *) &ut,sizeof(ut),1,fi))
if (ut.ut_name[0])
lines++;
(void) fclose(fi);
us = malloc(sizeof(*us) * (lines + 50));
up = malloc(sizeof(*up) * (lines + 50));
if ((!us) || (!up))
{
fprintf(stderr,"users: cannot allocate space for user list\n");
exit(2);
}
if (!(fi = fopen(PTYUTMP_FILE,"r")))
{
fprintf(stderr,"users: cannot open %s\n",PTYUTMP_FILE);
exit(1);
}
while (fread((char *) &ut,sizeof(ut),1,fi))
if ((ut.ut_name[0]) && (i < lines + 50))
{
(void) strncpy(us[i],ut.ut_name,8);
up[i] = us + i;
i++;
}
(void) fclose(fi);
(void) qsort(up,i,sizeof(*up),compar);
while (i-- > 0)
(void) printf((i ? "%.8s " : "%.8s\n"),up[i]);
(void) exit(0);
}
More information about the Comp.bugs.4bsd.ucb-fixes
mailing list