gtpc2m3tC/C++ Language Support User's Guide

getgrgid-Access the Group Database by ID

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);

gid
A group ID value.

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:

gr_name
Pointer to the group name character string.

gr_gid
The numeric GID.

gr_mem
A NULL-terminated array of pointers to the individual member names in this group.

Error Return

If unsuccessful, the getgrgid function returns a NULL pointer and sets errno to one of the following:

EIO
An input/output (I/O) error occurred.

EMFILE
The process has already reached its maximum number of open file descriptors. This limit is given by OPEN_MAX, which is defined in the limits.h header file.

ENFILE
The TPF system has already reached its maximum number of open files.

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