xref: /freebsd/lib/libc/gen/getgrent.3 (revision b7a1a0a5ff8895e6a5c9404d02357ac9e2eb8091)
1.\" Copyright (c) 1989, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     From: @(#)getgrent.3	8.2 (Berkeley) 4/19/94
33.\"	$Id$
34.\"
35.Dd September 29, 1994
36.Dt GETGRENT 3
37.Os
38.Sh NAME
39.Nm getgrent ,
40.Nm getgrnam ,
41.Nm getgrgid ,
42.Nm setgroupent ,
43.\" .Nm setgrfile ,
44.Nm setgrent ,
45.Nm endgrent
46.Nd group database operations
47.Sh SYNOPSIS
48.Fd #include <grp.h>
49.Ft struct group *
50.Fn getgrent void
51.Ft struct group *
52.Fn getgrnam "const char *name"
53.Ft struct group *
54.Fn getgrgid "gid_t gid"
55.Ft struct group *
56.Fn setgroupent "int stayopen"
57.\" .Ft void
58.\" .Fn setgrfile "const char *name"
59.Ft int
60.Fn setgrent void
61.Ft void
62.Fn endgrent void
63.Sh DESCRIPTION
64These functions operate on the group database file
65.Pa /etc/group
66which is described
67in
68.Xr group 5 .
69Each line of the database is defined by the structure
70.Ar group
71found in the include
72file
73.Aq Pa grp.h :
74.Bd -literal -offset indent
75struct group {
76	char	*gr_name;	/* group name */
77	char	*gr_passwd;	/* group password */
78	gid_t	gr_gid;		/* group id */
79	char	**gr_mem;	/* group members */
80};
81.Ed
82.Pp
83The functions
84.Fn getgrnam
85and
86.Fn getgrgid
87search the group database for the given group name pointed to by
88.Ar name
89or the group id pointed to by
90.Ar gid ,
91respectively, returning the first one encountered.  Identical group
92names or group gids may result in undefined behavior.
93.Pp
94The
95.Fn getgrent
96function
97sequentially reads the group database and is intended for programs
98that wish to step through the complete list of groups.
99.Pp
100All three routines will open the group file for reading, if necessary.
101.Pp
102The
103.Fn setgroupent
104function
105opens the file, or rewinds it if it is already open.  If
106.Fa stayopen
107is non-zero, file descriptors are left open, significantly speeding
108functions subsequent calls.  This functionality is unnecessary for
109.Fn getgrent
110as it doesn't close its file descriptors by default.  It should also
111be noted that it is dangerous for long-running programs to use this
112functionality as the group file may be updated.
113.Pp
114The
115.Fn setgrent
116function
117is identical to
118.Fn setgroupent
119with an argument of zero.
120.Pp
121The
122.Fn endgrent
123function
124closes any open files.
125.Sh YP/NIS INTERACTION
126When the
127.Xr yp 4
128group database is enabled, the
129.Fn getgrnam
130and
131.Fn getgrgid
132functions use the YP maps
133.Dq Li group.byname
134and
135.Dq Li group.bygid ,
136respectively, if the requested group is not found in the local
137.Pa /etc/group
138file.  The
139.Fn getgrent
140function will step through the YP map
141.Dq Li group.byname
142if the entire map is enabled as described in
143.Xr group 5 .
144.Sh RETURN VALUES
145The functions
146.Fn getgrent ,
147.Fn getgrnam ,
148and
149.Fn getgrgid ,
150return a pointer to the group entry if successful; if end-of-file
151is reached or an error occurs a null pointer is returned.
152The functions
153.Fn setgroupent
154and
155.Fn setgrent
156return the value 1 if successful, otherwise the value
1570 is returned.
158The functions
159.Fn endgrent
160and
161.Fn setgrfile
162have no return value.
163.Sh FILES
164.Bl -tag -width /etc/group -compact
165.It Pa /etc/group
166group database file
167.El
168.Sh SEE ALSO
169.Fn getpwent 3 ,
170.Fn group 5 ,
171.Fn yp 4
172.Sh HISTORY
173The functions
174.Fn endgrent ,
175.Fn getgrent ,
176.Fn getgrnam ,
177.Fn getgrgid ,
178and
179.Fn setgrent
180appeared in
181.At v7 .
182The functions
183.Fn setgrfile
184and
185.Fn setgroupent
186appeared in
187.Bx 4.3 Reno .
188.Sh COMPATIBILITY
189The historic function
190.Fn setgrfile ,
191which allowed the specification of alternate password databases, has
192been deprecated and is no longer available.
193.Sh BUGS
194The functions
195.Fn getgrent ,
196.Fn getgrnam ,
197.Fn getgrgid ,
198.Fn setgroupent
199and
200.Fn setgrent
201leave their results in an internal static object and return
202a pointer to that object. Subsequent calls to
203the same function
204will modify the same object.
205