Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!zaphod.mps.ohio-state.edu!rpi!dali.cs.montana.edu!caen!ox.com!yale!cmcl2!kramden.acf.nyu.edu!brnstnd From: brnstnd@kramden.acf.nyu.edu (Dan Bernstein) Newsgroups: comp.bugs.4bsd Subject: Re: Bug in users command Message-ID: <1126:Jan1811:17:4091@kramden.acf.nyu.edu> Date: 18 Jan 91 11:17:40 GMT References: <18947@rpp386.cactus.org> Organization: IR Lines: 74 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. ---Dan /* Public domain. */ #include #include #include 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); }