GETGRNAM(3P)
π GETGRNAM(3P) β the POSIX Programmer's Manual glow-up π
okay bestie, before we even start β this page is literally part of the POSIX Programmer's Manual. that means the Linux version of this interface might lowkey behave differently (so go peek at the matching Linux man page for the tea β), or it might just... not be implemented on Linux at all. no cap. π
β¨ NAME β¨
getgrnam, getgrnam_r β searching the group database for a name, babes π
πΈ SYNOPSIS πΈ
#include <grp.h>
struct group *getgrnam(const char *name);
int getgrnam_r(const char *name, struct group *grp, char *buffer,
size_t bufsize, struct group **result);
π¦ DESCRIPTION π¦
so getgrnam() shall search the group database for an entry with a matching name. that's the whole mission. slay. π
β οΈ real talk: getgrnam() need not be thread-safe. it's giving "one person at a time, bestie".
if you're an application that wants to actually check for error situations, set errno to 0 before calling getgrnam(). then if getgrnam() returns a null pointer and errno is set to non-zero β yeah, an error happened. that's your sign. π¨
now getgrnam_r() β the responsible sibling β shall update the group structure pointed to by grp and store a pointer to that structure at the location pointed to by result. the structure shall contain an entry from the group database with a matching name. the storage that the group structure references is allocated from the memory you hand over with the buffer parameter, which is bufsize bytes in size. and get this: a call to sysconf(_SC_GETGR_R_SIZE_MAX) returns either -1 without changing errno, or an initial value suggested for the size of this buffer. if there's an error, or the entry you asked for just isn't there, a null pointer gets returned at the location pointed to by result. π
π RETURN VALUE π
getgrnam() shall return a pointer to a struct group β with the structure defined in <grp.h> β with a matching entry, if one is found. if the requested entry wasn't found, or an error occurred, it shall return a null pointer. important distinction, so listen up: if the entry simply wasn't found, errno shall not be changed. but on error, errno shall be set to indicate the error. periodt. π―
π ββοΈ do NOT modify the structure that the return value points to, and don't touch any storage areas pointed to by pointers inside that structure either. the returned pointer, and the pointers within the structure, might get invalidated β or the structure or those storage areas might get straight up overwritten β by a subsequent call togetgrent(),getgrgid(), orgetgrnam(). also? the returned pointer and the pointers within the structure might get invalidated if the calling thread is terminated. it's giving fragile, handle with care. π
getgrnam_r() shall return zero on success, or if the requested entry wasn't found and no error occurred. if any error happened, an error number shall be returned to indicate the error. simple. iconic. β¨
π ERRORS π
the getgrnam() and getgrnam_r() functions may fail if:
EIOβ an I/O error has occurred. π΅EINTRβ a signal was caught duringgetgrnam().EMFILEβ all file descriptors available to the process are currently open. every single one. no room.ENFILEβ the maximum allowable number of files is currently open in the system.
and the getgrnam_r() function may fail if:
ERANGEβ insufficient storage was supplied viabufferandbufsizeto contain the data to be referenced by the resulting group structure. your buffer was too smol, bestie. grow it. π
psst β the following sections are informative.
π EXAMPLES π
heads up: sysconf(_SC_GETGR_R_SIZE_MAX) may return -1 if there's no hard limit on the size of the buffer needed to store all the groups returned. this example shows how an application can allocate a buffer of sufficient size to work with getgrnam_r(). it's the loop-to-victory arc, we love to see it πͺ
long int initlen = sysconf(_SC_GETGR_R_SIZE_MAX);
size_t len;
if (initlen == -1)
/* Default initial length. */
len = 1024;
else
len = (size_t) initlen;
struct group result;
struct group *resultp;
char *buffer = malloc(len);
if (buffer == NULL)
...handle error...
int e;
while ((e = getgrnam_r("somegroup", &result, buffer, len, &resultp))
== ERANGE)
{
size_t newlen = 2 * len;
if (newlen < len)
...handle error...
len = newlen;
char *newbuffer = realloc(buffer, len);
if (newbuffer == NULL)
...handle error...
buffer = newbuffer;
}
if (e != 0)
...handle error...
free (buffer);
π«Ά APPLICATION USAGE π«Ά
the getgrnam_r() function is thread-safe and shall return values in a user-supplied buffer instead of possibly using a static data area that may be overwritten by each call. that's the whole point of the _r. main character energy, honestly. π
portable applications should take into account that it's usual for an implementation to return -1 from sysconf(), indicating that there is no maximum for _SC_GETGR_R_SIZE_MAX. so don't panic when you see that -1. π§
β RATIONALE β
None.
(yeah. that's it. nothing. moving on. π)
π FUTURE DIRECTIONS π
None.
(also nothing. we love a chill future. π)
π₯ SEE ALSO π₯
endgrent()getgrgid()sysconf()
and from the Base Definitions volume of POSIX.1β2017: <grp.h>, <sys_types.h>
π COPYRIGHT π
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
okay that's the whole page, bestie!! this one's brought to you by IEEE/The Open Group, dated 2017, and it's page GETGRNAM(3P) π«Άβ¨ go forth and look up your groups, you got this ππΈπ¦
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms