GETGRENT(3)

๐Ÿ“ฆ
version 15.1 ยท 2 versions
, in freebsd 15.1

๐Ÿ’– 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() and getgrgid() โ€” search the group database for the group name pointed to by name or the group id pointed to by gid, 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(), and getgrgid_r() โ€” 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. 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 to setgroupent() 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(), 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. So donโ€™t save that pointer like itโ€™s gonna stay cute forever. ๐Ÿ˜ญ
  • 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).

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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms