1getgrnam(3)                Library Functions Manual                getgrnam(3)
2
3
4

NAME

6       getgrnam, getgrnam_r, getgrgid, getgrgid_r - get group file entry
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <sys/types.h>
13       #include <grp.h>
14
15       struct group *getgrnam(const char *name);
16       struct group *getgrgid(gid_t gid);
17
18       int getgrnam_r(const char *restrict name, struct group *restrict grp,
19                      char buf[restrict .buflen], size_t buflen,
20                      struct group **restrict result);
21       int getgrgid_r(gid_t gid, struct group *restrict grp,
22                      char buf[restrict .buflen], size_t buflen,
23                      struct group **restrict result);
24
25   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
26
27       getgrnam_r(), getgrgid_r():
28           _POSIX_C_SOURCE
29               || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
30

DESCRIPTION

32       The getgrnam() function returns a pointer to a structure containing the
33       broken-out fields of the record in the group database (e.g., the  local
34       group file /etc/group, NIS, and LDAP) that matches the group name name.
35
36       The getgrgid() function returns a pointer to a structure containing the
37       broken-out fields of the record in the group database that matches  the
38       group ID gid.
39
40       The group structure is defined in <grp.h> as follows:
41
42           struct group {
43               char   *gr_name;        /* group name */
44               char   *gr_passwd;      /* group password */
45               gid_t   gr_gid;         /* group ID */
46               char  **gr_mem;         /* NULL-terminated array of pointers
47                                          to names of group members */
48           };
49
50       For more information about the fields of this structure, see group(5).
51
52       The getgrnam_r() and getgrgid_r() functions obtain the same information
53       as getgrnam() and getgrgid(), but store the retrieved  group  structure
54       in  the  space  pointed to by grp.  The string fields pointed to by the
55       members of the group structure are stored in the buffer buf of size bu‐
56       flen.  A pointer to the result (in case of success) or NULL (in case no
57       entry was found or an error occurred) is stored in *result.
58
59       The call
60
61           sysconf(_SC_GETGR_R_SIZE_MAX)
62
63       returns either -1, without changing errno, or an initial suggested size
64       for  buf.   (If  this size is too small, the call fails with ERANGE, in
65       which case the caller can retry with a larger buffer.)
66

RETURN VALUE

68       The getgrnam() and getgrgid() functions return a  pointer  to  a  group
69       structure,  or  NULL if the matching entry is not found or an error oc‐
70       curs.  If an error occurs, errno is set to indicate the error.  If  one
71       wants  to  check  errno after the call, it should be set to zero before
72       the call.
73
74       The return value may point to a static area, and may be overwritten  by
75       subsequent  calls  to  getgrent(3), getgrgid(), or getgrnam().  (Do not
76       pass the returned pointer to free(3).)
77
78       On success, getgrnam_r() and getgrgid_r() return zero, and set  *result
79       to  grp.  If no matching group record was found, these functions return
80       0 and store NULL in *result.  In case of error, an error number is  re‐
81       turned, and NULL is stored in *result.
82

ERRORS

84       0 or ENOENT or ESRCH or EBADF or EPERM or ...
85              The given name or gid was not found.
86
87       EINTR  A signal was caught; see signal(7).
88
89       EIO    I/O error.
90
91       EMFILE The per-process limit on the number of open file descriptors has
92              been reached.
93
94       ENFILE The system-wide limit on the total number of open files has been
95              reached.
96
97       ENOMEM Insufficient memory to allocate group structure.
98
99       ERANGE Insufficient buffer space supplied.
100

FILES

102       /etc/group
103              local group database file
104

ATTRIBUTES

106       For  an  explanation  of  the  terms  used  in  this  section,  see at‐
107       tributes(7).
108
109       ┌──────────────┬───────────────┬───────────────────────────────────────┐
110Interface     Attribute     Value                                 
111       ├──────────────┼───────────────┼───────────────────────────────────────┤
112getgrnam()    │ Thread safety │ MT-Unsafe race:grnam locale           │
113       ├──────────────┼───────────────┼───────────────────────────────────────┤
114getgrgid()    │ Thread safety │ MT-Unsafe race:grgid locale           │
115       ├──────────────┼───────────────┼───────────────────────────────────────┤
116getgrnam_r(), │ Thread safety │ MT-Safe locale                        │
117getgrgid_r()  │               │                                       │
118       └──────────────┴───────────────┴───────────────────────────────────────┘
119

VERSIONS

121       The  formulation  given above under "RETURN VALUE" is from POSIX.1.  It
122       does not call "not found" an error, hence does not specify  what  value
123       errno  might  have  in this situation.  But that makes it impossible to
124       recognize errors.  One might argue that according to POSIX errno should
125       be  left  unchanged  if  an entry is not found.  Experiments on various
126       UNIX-like systems show that lots of  different  values  occur  in  this
127       situation:  0,  ENOENT,  EBADF, ESRCH, EWOULDBLOCK, EPERM, and probably
128       others.
129

STANDARDS

131       POSIX.1-2008.
132

HISTORY

134       POSIX.1-2001, SVr4, 4.3BSD.
135

SEE ALSO

137       endgrent(3),  fgetgrent(3),  getgrent(3),   getpwnam(3),   setgrent(3),
138       group(5)
139
140
141
142Linux man-pages 6.05              2023-07-20                       getgrnam(3)
Impressum