getgrgid() — Access the group database by ID

Standards

Standards / Extensions C or C++ Dependencies

POSIX.1
XPG4
XPG4.2
Single UNIX Specification, Version 3

both  

Format

#define _POSIX_SOURCE
#include <grp.h>

struct group *getgrgid(gid_t gid);

General description

Provides information about the group specified by gid and its members.

Returned value

If successful, getgrgid() returns a pointer to a group structure containing an entry from the group database with the specified gid. The return value may point 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
The name of the group
gr_gid
The numerical group ID (GID)
gr_mem
A NULL-terminated vector of pointers to the individual member names

If unsuccessful, getgrgid() returns a NULL pointer.

There are no documented errno values.

Example

CELEBG08
⁄* CELEBG08

   This example provides the root GID and group name.

 *⁄
#define _POSIX_SOURCE
#include <sys⁄types.h>
#include <grp.h>
#include <stdio.h>
#include <sys⁄stat.h>   ⁄*FIX: used to be <stat.h>*⁄

main() {
  struct stat info;
  struct group *grp;

  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);
  }
}
Output
The root is owned by gid 500
This group name is SYS1

Related information