Documentation Archive Developer
ADC Home > Reference Library > Reference > Mac OS X > Mac OS X Man Pages


This document is a Mac OS X manual page. Manual pages are a command-line technology for providing documentation. You can view these manual pages locally using the man(1) command. These manual pages come from many different sources, and thus, have a variety of writing styles.

For more information about the manual page format, see the manual page for manpages(5).

GETGRENT(3)              BSD Library Functions Manual              GETGRENT(3)

     endgrent, getgrent, getgrgid, getgrgid_r, getgrnam, getgrnam_r, setgrent,
     setgroupent -- group database operations

     Standard C Library (libc, -lc)

     #include <grp.h>


     struct group *

     struct group *
     getgrgid(gid_t gid);

     getgrgid_r(gid_t gid, struct group *grp, char *buffer, size_t bufsize,
         struct group **result);

     struct group *
     getgrnam(const char *name);

     getgrnam_r(const char *name, struct group *grp, char *buffer,
         size_t bufsize, struct group **result);


     setgroupent(int stayopen);

     These functions operate on the group database file /etc/group, which is
     described in group(5).  Each line of the database is defined by the
     structure group, found in the include file <grp.h>:

           struct group {
                   char    *gr_name;       /* group name */
                   char    *gr_passwd;     /* group password */
                   int     gr_gid;         /* group id */
                   char    **gr_mem;       /* group members */

     The functions getgrnam() and getgrgid() search the group database for the
     given group name (pointed to by name) or the group id (pointed to by
     gid), respectively, returning the first one encountered.  Identical group
     names or group ids may result in undefined behavior.

     All of these routines are thread-safe.  The getgrent(), getgrgid(), and
     getgrnam() routines return a pointer to a result managed by the system
     library in a thread-specific data structure.  Every thread has space for
     a pointer to a struct group and allocates its own storage for the result.
     Neither previously returned values in memory nor a previously returned
     pointer value should be used by a thread after calling any one of these
     three routines.  Memory allocated by a thread is automatically released
     on subsequent calls by the same thread to any of these three routines,
     and when the thread exits.

     The functions getgrgid_r() and getgrnam_r() take additional arguments
     which supply storage space for the returned result.  The grp parameter is
     a pointer to a struct group, which must be allocated by the caller.  The
     buffer parameter is a pointer to a block of memory with a size specified
     by bufsize.  This buffer is used to hold the values which are pointed to
     by values filled in the grp structure.  Zero is returned on success.  If
     insufficient memory is supplied, these routines return ERANGE.

     The getgrent() function sequentially reads the group database and is
     intended for programs that wish to step through the complete list of

     All three routines will open the group file for reading, if necessary.

     The setgroupent() function opens the file, or rewinds it if it is already
     open.  If stayopen is non-zero, file descriptors are left open, signifi-cantly significantly
     cantly speeding functions' subsequent calls.  This functionality is
     unnecessary for getgrent(), as it doesn't close its file descriptors by
     default.  It should also be noted that it is dangerous for long-running
     programs to use this functionality, as the group file may be updated.

     The setgrent() function is identical to setgroupent() with an argument of

     The endgrent() function closes any open files.

     The functions getgrent(), getgrgid(), and getgrnam() each return a
     pointer to the group entry if successful; if end-of-file is reached or an
     error occurs, a null pointer is returned.  The function setgroupent()
     returns the value 1 if successful; otherwise, the value 0 is returned.
     The functions endgrent(), setgrent(), and setgrfile() have no return

     /etc/group  group database file

     #include <grp.h>


     The function setgrent() returns the value 1 if successful; otherwise, the
     value 0 is returned.

     getpwent(3), yp(8), group(5)

     The functions endgrent(), getgrent(), getgrnam(), getgrgid(), and
     setgrent() appeared in Version 7 AT&T UNIX.  The functions setgrfile()
     and setgroupent() appeared in 4.3BSD-Reno.

     The historic function setgrfile(), which allowed the specification of
     alternate password databases, has been deprecated and is no longer avail-able. available.

     The functions getgrent(), getgrnam(), and getgrgid(), leave their results
     in internal thread-specific memory and return a pointer to that object.
     Subsequent calls to any of these three routines by the same thread will
     release the object and return a new pointer value.

BSD                           September 29, 1994                           BSD