randy@uokmax.UUCP (Longshot) (10/28/88)
Posting-number: Volume 5, Issue 8 Submitted-by: "Longshot" <randy@uokmax.UUCP> Archive-name: whosin [Methinks this is a job for perl or awk, not C.... ++bsa] A little program I wrote to display the members of specified groups. Should work on any UNIX with grp.h and group data in /etc/group. Randy J. Ray ---- Cut Here and unpack ---- #!/bin/sh # shar: Shell Archiver (v1.22) # Packed Sun Oct 23 21:00:34 CDT 1988 by uokmax!randy # from directory /uokmax/staff/randy/src/C # # Run the following text with /bin/sh to create: # README # whosin.L # whosin.c # if test -f README; then echo "File README exists"; else echo "x - extracting README (Text)" sed 's/^X//' << 'SHAR_EOF' > README && XThis is a little program I tossed together because I occassionally wanted Xto know who all were included in a specific group. This is handy if you need Xto get in touch with someone who has specific access. It accepts as arguments Xgroup names or numbers as listed in the file /etc/group. It prints the name, Xnumber, and number of listed users for the group, followed by an alphabetized Xlist of usernames, printed 8 to a line. To create it, just type: X X cc -O -o whosin whosin.c # No need for a makefile X XAs /etc/group is world readable, and no writing is done, no special permissions Xare needed. Be warned, however. On our system, many student account groups are Xnot entered with complete listings, as they change from semester to semester. XRather, the group entry is made, and the user accounts bestowed with the Xnumber. In these cases, the group appears legal, but is reported with X"Users: 0" and no names following. This is not really a bug, since all the Xprogram does is read the file. But it is handy for smaller groups (like finding Xthe nearest person with root access, by typing "whosin wheel"). X XRandy J. Ray X-- XRandy J. Ray University of Oklahoma, Norman, Oklahoma Xrandy@uokmax.uucp "...and he who made kittens put snakes XRJRAY@aardvark.ucs.uoknor.edu in the grass..." -Jethro Tull X-=*-=*-=*-=*-=*-=*-=*-=*-=*-=*-=*-=*-=*-=*-=*-=*-=*-=*-=*-=*-=*-=*-=*-=*-=*-=*- SHAR_EOF chmod 0644 README || echo "restore of README fails" set `wc -c README`;Sum=$1 if test "$Sum" != "1384" then echo original size 1384, current size $Sum;fi fi if test -f whosin.L; then echo "File whosin.L exists"; else echo "x - extracting whosin.L (Text)" sed 's/^X//' << 'SHAR_EOF' > whosin.L && X.TH WHOSIN L LOCAL X.SH NAME Xwhosin \- diplays users in specified groups X.SH SYNOPSIS Xwhosin [ X.B name X| X.B number X] . . . X.SH DESCRIPTION X.B Whosin Xtakes a list of arguments being either names of active groups Xor groups numbers, and displays the users listed with that Xgroup in the file X.I /etc/group . X.SH FILES X/etc/group X.SH "SEE ALSO" Xgroups(1), gid(L), uid(L), id(L) X.SH BUGS XIf the group's entry in /etc/group is quite long (as is the case for Xgroups such as eecsugrad, the undergraduate accounts in EECS), all Xindividual names are not listed with the regular entry. In these cases, Xno names are listed, and number of users is reported as 0. X.SH AUTHOR XRandy J. Ray SHAR_EOF chmod 0644 whosin.L || echo "restore of whosin.L fails" set `wc -c whosin.L`;Sum=$1 if test "$Sum" != "672" then echo original size 672, current size $Sum;fi fi if test -f whosin.c; then echo "File whosin.c exists"; else echo "x - extracting whosin.c (Text)" sed 's/^X//' << 'SHAR_EOF' > whosin.c && X/* X whosin - show the members of a specified group X*/ X X/* X $Header: whosin.c,v 1.2 88/10/19 19:50:14 randy Exp $ X X $Log: whosin.c,v $ X * Revision 1.2 88/10/19 19:50:14 randy X * Changed string.h header file to strings.h to increase portability. X * X * Revision 1.1 88/10/17 14:35:51 randy X * Initial revision X * X*/ X X#include <stdio.h> X#include <grp.h> X#include <strings.h> X Xmain(argc,argv) Xint argc; Xchar **argv; X{ X int strkmp(),grnum,nnum; X char **names,**argx,*grp; X struct group *gr; X X if (--argc == 0) { X fprintf(stderr,"Usage: whosin [group name | group num] . . .\n"); X exit(1); X } X while (argc--) { X grnum=atoi(*++argv); X if ((grnum == 0) && (strcmp(*argv,"0"))) gr=getgrnam(*argv); X else gr=getgrgid(grnum); X if ((names=gr->gr_mem) == NULL) { X fprintf(stderr,"Unknown group: %s\n",*argv); X exit(1); X } X argx=names; X nnum=0; X while (*argx++) nnum++; X qsort(names,nnum,(sizeof(char *)),strkmp); X printf("\nGroup: %s\t\tId: %d\t\tUsers: %d\n",gr->gr_name,gr->gr_gid,nnum); X grnum=0; X while (grnum++ != nnum) { X printf("%-9s",*names++); X if ((grnum % 8) == 0) putchar('\n'); X } X if ((nnum % 8) != 0) putchar('\n'); X } X} X Xint strkmp(s1,s2) Xchar **s1,**s2; X{ X return(strcmp(*s1,*s2)); X} SHAR_EOF chmod 0444 whosin.c || echo "restore of whosin.c fails" set `wc -c whosin.c`;Sum=$1 if test "$Sum" != "1220" then echo original size 1220, current size $Sum;fi fi exit 0