Manual Page Search Parameters

GETGRENT(3) Library Functions Manual GETGRENT(3)


getgrent, getgrnam, getgrnam_r, getgrgid, getgrgid_r, setgroupent, setgrent, endgrentgroup database operations


#include <grp.h>
struct group *
struct group *
getgrnam(const char *name);
getgrnam_r(const char *name, struct group *grp, char *buf, size_t bufsize, struct group **result);
struct group *
getgrgid(gid_t gid);
getgrgid_r(gid_t gid, struct group *grp, char *buf, 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 struct 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.
getgrent() sequentially reads the group database and is intended for programs that wish to step through the complete list of groups.
All three routines will open the group file for reading, if necessary.
setgroupent() opens the file, or rewinds it if it is already open. If stayopen is non-zero, file descriptors are left open, significantly speeding subsequent function 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.
setgrent() is equivalent to setgroupent() with an argument of zero.
The endgrent() function closes any open files.
The getgrgid_r() and getgrnam_r() functions both update the group structure pointed to by grp and store a pointer to that structure at the location pointed to by result. The structure is filled with an entry from the group database with a matching gid or name. Storage referenced by the group structure will be allocated from the memory provided with the buf parameter, which is bufsiz characters in size. The maximum size needed for this buffer can be determined with sysconf(_SC_GETGR_R_SIZE_MAX).

YP support

If YP is active, the functions getgrent() and getgrnam() also use the group.byname YP map and the function getgrgid() also uses the group.bygid YP map in addition to the group file, respecting the order of normal and YP entries in the group file.


The functions getgrent(), 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 setgroupent() function returns the value 1 if successful, otherwise 0.
The endgrent() and setgrent() functions have no return value.
The functions getgrgid_r() and getgrnam_r() update result to point to grp if a match is found or set it to NULL if no match is found or an error occurs. They return 0 on success, even if no match is found, or an error number if an error occurs; see ERRORS.


group database file


The getgruid_r() and getgrnam_r() functions may fail if:
The storage supplied via buf and bufsize is too small and cannot contain all the strings to be returned in grp.
The getgrent(), getgrgid(), getgrgid_r(), getgrnam(), and getgrnam_r() functions may also fail for any of the errors specified for fgets(3), getc(3), and strtoul(3). If YP is active, they may also fail due to errors caused by the YP subsystem.
The setgroupent() function may fail for any of the errors specified for fopen(3).
The endgrent(), getgrgid_r(), getgrnam_r(), and setgrent() functions do not set errno.


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


The functions getgrgid(), getgrgid_r(), getgrnam(), and getgrnam_r() are compliant with the IEEE Std 1003.1-2008 (“POSIX.1”) specification.
The functions endgrent(), getgrent(), and setgrent() are compliant with the X/Open System Interfaces option of the IEEE Std 1003.1-2008 (“POSIX.1”) specification.
YP support and the setgroupent() function are extensions to that specification.


The functions endgrent(), getgrent(), getgrnam(), getgrgid(), and setgrent() appeared in Version 7 AT&T UNIX. The setgroupent() function appeared in 4.3BSD-Reno.
The historic function setgrfile(), which allowed the specification of alternate group databases, has been deprecated and is no longer available.


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 possible.
November 4, 2015 OpenBSD-current