gtpc2m3t | C/C++ Language Support User's Guide |
This function provides information about the group specified by the group ID (GID) and its members.
Format
#include <grp.h> struct group *getgrgid(gid_t gid);
Normal Return
If successful, the getgrgid function returns a pointer to a group structure containing an entry from the group database with the specified gid. The return value is a pointer to static data that is overwritten by each call.
This group structure, defined in the grp.h header file, contains the following members:
Error Return
If unsuccessful, the getgrgid function returns a NULL pointer and sets errno to one of the following:
Programming Considerations
Examples
The following example provides the root GID and group name.
#include <grp.h> #include <stdio.h> #include <sys/stat.h> int main(void) { struct group *grp; struct stat info; if ( stat("/", &info ) < 0 ) perror ( "stat() error" ); else { printf( "The root is owned by gid %d\n", info.st_gid ); if ( ( grp = getgrgid ( info.st_gid ) ) == NULL ) perror ( "getgrgid() error" ); else printf ( "This group name is %s\n", grp->gr_name ); } return 0; } /* end of main */
Output
The root is owned by GID 0 This group name is root
Related Information