Open System Services Programmer's Guide

Example 58 Using OSS Security Functions
/* Using OSS Security functions (without ACLs) */
#include <stdio.h>
#include <limits.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <grp.h>
#include <pwd.h>
/*
* Print out the group number in decimal followed by (groupname)
*/
void printgroup(gid_t groupid)
{
unsigned long lt; /* temp */
struct group *grpptr; /* pointer to group info */
lt = (unsigned long)groupid; /* make the gid a long */
printf(" %lu(",lt); /* print it */
grpptr = getgrgid(groupid); /* get group structure */
/* print question marks if group name is unknown */
if(grpptr == NULL) {
printf("??????)");
return;
}
printf("%s)",grpptr->gr_name); /* print group name */
return;
}
/*
* Print out the user ID in decimal followed by (username)
*/
void printuser(uid_t userid)
{
unsigned long lt; /* temp */
struct passwd *pwptr; /* pointer to user info */
lt = (unsigned long)userid; /* make the uid a long */
printf(" %lu(",lt); /* print the number */
pwptr = getpwuid(userid); /* get the information */
/* print question marks if user ID is not known */
if(pwptr == NULL) {
printf("??????)");
return;
}
printf("%s)",pwptr->pw_name);
return;
}
void printsupgroups()
{
int ngroups; /* number of supplemental groups */
gid_t grouplist[NGROUPS_MAX]; /* list of group IDs */
int i;
gid_t gid;
/* Get the number of supplementary group IDs in use */
ngroups = getgroups(NGROUPS_MAX, grouplist);
if(ngroups == -1) {
perror("getgroups() failed");
return;
}
Example of Managing file Ownership 257