Open System Services Programmer's Guide

Example 1 Using Some OSS Group Information Functions
#include <limits.h>
#include <ctype.h>
#include <sys/types.h>
#include <grp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
struct group *gnam = NULL;
gid_t grouplist[NGROUPS_MAX];
int gcnt;
char groupname[32];
int main(int argc, char *argv[])
{
int i;
char **ptr;
/*
Get the group number or name from the command line.
If no argument is provided, use the group number for the user.
*/
if(argc > 1) {
if(isdigit(argv[1][0])) /* check if numeric value */
gnam = getgrgid(atoi(argv[1]));
else /* else assume group name */
gnam = getgrnam(argv[1]);
} else
gnam = getgrgid(getgid());
if(gnam == NULL) {
fprintf(stderr,"Can't get group entry for %s\n", argv[1]);
exit(1);
}
/* Print group name entry information. */
printf("Group name entry -> = %s\n", gnam->gr_name);
printf("Group number entry -> = %d\n", gnam->gr_gid);
/* Print names of other members of this group using getgrent(). */
strcpy(groupname, gnam->gr_name); /* save name of group name */
setgrent(); /* reset to beginning */
while((gnam = getgrent()) != NULL) {
if(strcmp(gnam->gr_name, groupname) == 0)
break;
}
endgrent(); /* close the file */
ptr = gnam->gr_mem;
printf("All members of group (by name) are:\n");
i = 0;
while(*ptr)
printf("%d: %s\n", i++, *ptr++);
/* Print supplementary group IDs for user. */
if((gcnt = getgroups(NGROUPS_MAX, grouplist)) < 0) {
fprintf(stderr, "Can't get list of group membership\n");
exit(1);
}
printf("\nNumber of supplementary group IDs for user = %d\n", gcnt);
for(i = 0; i < gcnt; i++)
printf("\tGroup ID #%d:\t%d\n", i, grouplist[i]);
return(0);
}
Two Process Types 35