Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

getgrent(3) [minix man page]

GETGRENT(3)						     Library Functions Manual						       GETGRENT(3)

getgrent, getgrnam, getgrgid, setgrent, endgrent, setgrfile - group file routines SYNOPSIS
#include <grp.h> struct group *getgrent(void) struct group *getgrnam(const char *name) struct group *getgrgid(gid_t gid) int setgrent(void) void endgrent(void) void setgrfile(const char *file) DESCRIPTION
These functions are used to obtain information from the group file. They return this information in a struct group as defined by <grp.h>: struct group { char *gr_name; /* login name */ char *gr_passwd; /* encrypted password */ gid_t gr_gid; /* numeric group id */ char **gr_mem; /* null-terminated list of group members */ }; Getgrent() reads the group file entry by entry. Getgrnam() scans the entire group file for the group with the given name. Getgrgid() looks for the first group with the given gid. The setgrent() and endgrent() functions are used to open and later close the group file. With setgrfile() one can specify the file to read other than the normal group file. This only sets the name, the next setgrent() call will open the file. Do not touch the file name while it is active. Use setgrfile(NULL) to revert back to the normal group file. The usual way to scan the group file is (error checking omitted): setgrent(); while ((gr = getgrent()) != NULL) if (appropriate_test(gr)) break; endgrent(); The gr variable contains the entry that is wanted if non-NULL. The getgrnam() and getgrgid() functions are implemented as in this example, with error checking of course. Getgrent() calls setgrent() if this has not yet been done. Setgrent() first calls endgrent() if the group file is still open. (Other implementations may simply rewind the file.) FILES
/etc/group The group file database. SEE ALSO
getgroups(2), initgroups(3), getpwent(3), passwd(5). DIAGNOSTICS
Setgrent() has the same return value and error codes as the open(2) call it uses to open the group file. The getxxx() functions return NULL on end of file, entry not found, or error. You can set errno to zero before the call and check it after. NOTES
All getxxx() routines return a pointer to static storage that is overwritten in each call. Only getgrnam() and getgrgid() are defined by POSIX. The _MINIX_SOURCE macro must be defined before including <grp.h> to make the other functions visible. The gr_passwd field is also not defined by POSIX, but is always visible. Portable code cannot reliably detect errors by setting errno to zero. Under Minix it is better to make a getgrent() scan if you need to look up several group-id's or names, but por- table code had better use several getgrgid() or getgrnam() calls. The getgrent() is usually available on other systems, but may be very expensive. See initgroups(3) if you are after supplementary group id's. AUTHOR
Kees J. Bot ( GETGRENT(3)

Check Out this Related Man Page

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

getgrent, getgrent_r, getgrnam, getgrnam_r, getgrgid, getgrgid_r, setgroupent, setgrent, endgrent -- group database operations LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <grp.h> struct group * getgrent(void); int getgrent_r(struct group *grp, char *buffer, size_t bufsize, struct group **result); struct group * getgrnam(const char *name); int getgrnam_r(const char *name, struct group *grp, char *buffer, size_t bufsize, struct group **result); struct group * getgrgid(gid_t gid); int getgrgid_r(gid_t gid, struct group *grp, char *buffer, size_t bufsize, struct group **result); int setgroupent(int stayopen); int setgrent(void); void endgrent(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 intended for programs that wish to step through the complete list of groups. The functions getgrent_r(), getgrnam_r(), and getgrgid_r() are thread-safe versions of getgrent(), getgrnam(), and getgrgid(), respectively. The caller must provide storage for the results of the search in the grp, buffer, bufsize, and result arguments. When these functions are successful, the grp argument will be filled-in, and a pointer to that argument will be stored in result. If an entry is not found or an error occurs, result will be set to NULL. These functions 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, significantly speeding functions subsequent calls. This functionality is unnecessary for getgrent() as it does not close its file descrip- tors 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 zero. The endgrent() function closes any open files. RETURN VALUES
The functions getgrent(), getgrnam(), and getgrgid(), return a pointer to a group structure on success or NULL if the entry is not found or if an error occurs. If an error does occur, errno will be set. Note that programs must explicitly set errno to zero before calling any of these functions if they need to distinguish between a non-existent entry and an error. The functions getgrent_r(), getgrnam_r(), and getgrgid_r() return 0 if no error occurred, or an error number to indicate failure. It is not an error if a matching entry is not found. (Thus, if result is set to NULL and the return value is 0, no matching entry exists.) The functions setgroupent() and setgrent() return the value 1 if successful, otherwise the value 0 is returned. The functions endgrent() and setgrfile() have no return value. FILES
/etc/group group database file COMPATIBILITY
The historic function setgrfile(), which allowed the specification of alternate password databases, has been deprecated and is no longer available. SEE ALSO
getpwent(3), group(5), nsswitch.conf(5), yp(8) STANDARDS
The getgrent(), getgrnam(), getgrnam_r(), getgrgid(), getgrgid_r() and endgrent() functions conform to ISO/IEC 9945-1:1996 (``POSIX.1''). The setgrent() function differs from that standard in that its return type is int rather than void. HISTORY
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 functions getgrent_r(), getgrnam_r(), and getgrgid_r() appeared in FreeBSD 5.1. 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(), getgrent_r(), endgrent(), setgroupent(), and setgrent() are fairly useless in a networked environment and should be avoided, if possible. The getgrent() and getgrent_r() functions make no attempt to suppress duplicate information if multiple sources are specified in nsswitch.conf(5). BSD
April 16, 2003 BSD
Man Page