GETGRENT(3) BSD Programmer's Manual GETGRENT(3)NAME
getgrent, getgrnam, getgrgid, setgroupent, setgrent, endgrent,
getgrent_r, getgrnam_r, getgrgid_r, setgrent_r, endgrent_r - group
database operations
SYNOPSIS
#include <sys/types.h>
#include <grp.h>
struct group *
getgrent(void);
struct group *
getgrnam(const char *name);
struct group *
getgrgid(gid_t gid);
int
setgroupent(int stayopen);
int
setgrent(void);
void
endgrent(void);
struct group *
getgrent_r(struct group *space, char *buf, int buflen);
int
getgrnam_r(const char *name, struct group *space, char *buf,
size_t bufsize, struct group **result);
int
getgrgid_r(gid_t gid, struct group *space, char *buf, size_t bufsize,
struct group **result);
void
setgrent_r(void);
void
endgrent_r(void);
DESCRIPTION
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 */
gid_t 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 gids may result in undefined behavior.
The getgrent() function sequentially reads the group database and is in-
tended for programs that wish to step through the complete list of
groups.
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 speeding functions subsequent calls. This functionality is unnec-
essary for getgrent() as it doesn't close its file descriptors by de-
fault. 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
zero.
The endgrent() function closes any open files.
The ``r-suffixed'' functions getgrent_r(), getgrnam_r(), getgrgid_r(),
setgrent_r(), and endgrent_r() functions are reentrant versions of their
corresponding non-reentrant functions. Those that build a group struc-
ture must be provided a pointer space to a structure that will hold the
result, as well as a buffer and its size (buflen or bufsize) for memory
to which members of space may be pointed. The built-up structure will
either be returned directly (getgrent_r()), or stored via the result pa-
rameter. This will be NULL if the given group name or ID does not exist.
For example:
struct group space, *gp;
int error;
char buf[SIZE]; /* where SIZE >= sysconf(_SC_GETGR_R_SIZE_MAX) */
error = getgrnam_r(groupname, &space, buf, sizeof buf, &gp);
if (gp != NULL) {
/* group exists; gp == &space */
printf("group %s, gid %ld\n", gp->gr_name, (long)gp->gr_uid);
} else
... group does not exist, or an error occurred ...
RETURN VALUES
The functions getgrent(), getgrent_r(), getgrnam(), and getgrgid() 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 functions getgrnam_r()
and getgrgid_r() return 0 on success, or an error number on failure. The
functions setgroupent() and setgrent() return the value 1 if successful,
otherwise the value 0 is returned. The functions endgrent(),
setgrent_r(), and endgrent_r() have no return value.
If the buffer supplied to the reentrant functions is too small, they will
fail with an ERANGE error. The POSIX functions getgrnam_r() and
getgruid_r() will return ERANGE, while getgrent_r() will set the global
variable errno instead.
FILES
/etc/group group database file
SEE ALSOgetpwent(3), group(5)HISTORY
The functions endgrent(), getgrent(), getgrnam(), getgrgid(), and
setgrent() appeared in Version 7 AT&T UNIX. The function setgroupent()
appeared in 4.3BSD-Reno.
BUGS
The functions getgrent(), getgrnam(), getgrgid(), setgroupent() and
setgrent() leave their results in an internal static object and return a
pointer to that object. Subsequent calls to the same function will modify
the same object.
The functions getgrent(), endgrent(), setgroupent(), and setgrent() are
fairly useless in a networked environment and should be avoided, if pos-
sible.
The getgrent(), endgrent(), setgroupent(), and setgrent() functions may
not be safely called concurrently from multiple threads, e.g., the inter-
faces described by pthreads(3).
The non-standard functions setgrent(), setgrent_r(), getgrent_r(), and
endgrent_r() are known to have different interfaces on different systems,
and should probably be avoided.
The ``interesting'' calling sequences for the reentrant functions are
courtesy of ongoing standardization efforts.
COMPATIBILITY
The historic function setgrfile(), which allowed the specification of al-
ternate password databases, has been deprecated and is no longer avail-
able.
The getgrnam(), getgrgid(), getgrnam_r(), and getgrgid_r() functions con-
form to IEEE Std1003.1-1996 (``POSIX'').
BSDI BSD/OS April 27, 2001 3