GETGRENT(3)
๐ getgrent(3) โ group database operations ๐
Okay bestie, this page is from the Library Functions Manual, and itโs literally all about the group database. Group chat but make it Unix. ๐
๐ธ NAME ๐ธ
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);
void
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 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 */
};
getgrnam()andgetgrgid()โ search the group database for the group name pointed to bynameor the group id pointed to bygid, respectively, and return the first one encountered. Heads up: identical group names or group gids may result in undefined behavior โ thatโs a no from the vibe check, bestie. ๐ซgetgrent()โ sequentially reads the group database. Itโs intended for programs that wanna step through the complete list of groups. Slay.getgrent_r(),getgrnam_r(), andgetgrgid_r()โ thread-safe versions ofgetgrent(),getgrnam(), andgetgrgid(), respectively. The caller must provide storage for the results of the search in thegrp,buffer,bufsize, andresultarguments. When these functions are successful, thegrpargument will be filled in, and a pointer to that argument will be stored inresult. If an entry is not found or an error occurs,resultwill be set toNULL.
These functions will open the group file for reading, if necessary. Lowkey doing the prep work for you. ๐
setgroupent() opens the file, or rewinds it if it is already open. If stayopen is non-zero, file descriptors are left open, which significantly speeds up subsequent calls. This functionality is unnecessary for getgrent() because it does not 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()โ identical tosetgroupent()with an argument of zero.endgrent()โ closes any open files.
๐ RETURN VALUES ๐
On success, getgrent(), getgrnam(), and getgrgid() return a pointer to a group structure. They return NULL if the entry is not found or if an error occurs. If an error does occur, errno will be set. Important caveat: 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. Periodt. ๐
The getgrent_r(), getgrnam_r(), and getgrgid_r() functions 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 setgroupent() function returns the value 1 if successful; otherwise, the value 0 is returned. The functions endgrent(), setgrent(), 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. rip to that era. ๐
๐ฅ SEE ALSO ๐ฅ
๐ 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. Yes, the page literally says that โ Iโm just reporting the lore. ๐
๐ง 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. History lesson, bestie. ๐
๐ BUGS ๐
- The functions
getgrent(),getgrnam(),getgrgid(),setgroupent(), andsetgrent()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. So donโt save that pointer like itโs gonna stay cute forever. ๐ญ - The functions
getgrent(),getgrent_r(),endgrent(),setgroupent(), andsetgrent()are fairly useless in a networked environment and should be avoided, if possible. Thegetgrent()andgetgrent_r()functions make no attempt to suppress duplicate information if multiple sources are specified innsswitch.conf(5).
xoxo, your Unix bestie ๐ โ this oneโs from Debian, dated July 31, 2016. Now go slay those group lookups, bestie! โจ
๐ฆ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (c) 1989, 1991, 1993 The Regents of the University of California. All rights reserved
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms