getgrnam() — Access the group database by name

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 *getgrnam(const char *name);

General description

Accesses the group structure containing an entry from the group database with the specified name.

Returned value

If successful, getgrnam() returns a pointer to a group structure. The return value may point to static data that is overwritten by each call.

The 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 or if the requested entry is not found, getgrnam() returns a NULL pointer.

There are no documented errno values.

Example

CELEBG09
⁄* CELEBG09

   This example provides the members of a group.

 *⁄
#define _POSIX_SOURCE
#include <sys⁄types.h>
#include <grp.h>
#include <stdio.h>

main() {
  struct group *grp;
  char   grpname[]="USERS", **curr;

  if ((grp = getgrnam(grpname)) == NULL)
    perror("getgrnam() error");
  else {
    printf("The following are members of group %s:\n", grpname);
    for (curr=grp->gr_mem; (*curr) != NULL; curr++)
      printf("  %s\n", *curr);
  }
}
Output
The following are members of group USERS:
  MVSUSR1
  MVSUSR2
  MVSUSR3
  MVSUSR4
  MVSUSR5
  MVSUSR6
  MVSUSR7
  MVSUSR8
  MVSUSR9

Related information