xref: /freebsd/lib/libc/gen/getgrent.3 (revision df7f5d4de4592a8948a25ce01e5bddfbb7ce39dc)
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: getgrent.3,v 1.9 1997/02/22 14:58:07 peter Exp $
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 <sys/types.h>
49.Fd #include <grp.h>
50.Ft struct group *
51.Fn getgrent void
52.Ft struct group *
53.Fn getgrnam "const char *name"
54.Ft struct group *
55.Fn getgrgid "gid_t gid"
56.Ft int
57.Fn setgroupent "int stayopen"
58.\" .Ft void
59.\" .Fn setgrfile "const char *name"
60.Ft int
61.Fn setgrent void
62.Ft void
63.Fn endgrent void
64.Sh DESCRIPTION
65These functions operate on the group database file
66.Pa /etc/group
67which is described
68in
69.Xr group 5 .
70Each line of the database is defined by the structure
71.Ar group
72found in the include
73file
74.Aq Pa grp.h :
75.Bd -literal -offset indent
76struct group {
77	char	*gr_name;	/* group name */
78	char	*gr_passwd;	/* group password */
79	int	gr_gid;		/* group id */
80	char	**gr_mem;	/* group members */
81};
82.Ed
83.Pp
84The functions
85.Fn getgrnam
86and
87.Fn getgrgid
88search the group database for the given group name pointed to by
89.Ar name
90or the group id pointed to by
91.Ar gid ,
92respectively, returning the first one encountered.  Identical group
93names or group gids may result in undefined behavior.
94.Pp
95The
96.Fn getgrent
97function
98sequentially reads the group database and is intended for programs
99that wish to step through the complete list of groups.
100.Pp
101All three routines will open the group file for reading, if necessary.
102.Pp
103The
104.Fn setgroupent
105function
106opens the file, or rewinds it if it is already open.  If
107.Fa stayopen
108is non-zero, file descriptors are left open, significantly speeding
109functions subsequent calls.  This functionality is unnecessary for
110.Fn getgrent
111as it doesn't close its file descriptors by default.  It should also
112be noted that it is dangerous for long-running programs to use this
113functionality as the group file may be updated.
114.Pp
115The
116.Fn setgrent
117function
118is identical to
119.Fn setgroupent
120with an argument of zero.
121.Pp
122The
123.Fn endgrent
124function
125closes any open files.
126.Sh YP/NIS INTERACTION
127When the
128.Xr yp 4
129group database is enabled, the
130.Fn getgrnam
131and
132.Fn getgrgid
133functions use the YP maps
134.Dq Li group.byname
135and
136.Dq Li group.bygid ,
137respectively, if the requested group is not found in the local
138.Pa /etc/group
139file.  The
140.Fn getgrent
141function will step through the YP map
142.Dq Li group.byname
143if the entire map is enabled as described in
144.Xr group 5 .
145.Sh RETURN VALUES
146The functions
147.Fn getgrent ,
148.Fn getgrnam ,
149and
150.Fn getgrgid ,
151return a pointer to the group entry if successful; if end-of-file
152is reached or an error occurs a null pointer is returned.
153The functions
154.Fn setgroupent
155and
156.Fn setgrent
157return the value 1 if successful, otherwise the value
1580 is returned.
159The functions
160.Fn endgrent
161and
162.Fn setgrfile
163have no return value.
164.Sh FILES
165.Bl -tag -width /etc/group -compact
166.It Pa /etc/group
167group database file
168.El
169.Sh SEE ALSO
170.Xr getpwent 3 ,
171.Xr yp 4 ,
172.Xr group 5
173.Sh HISTORY
174The functions
175.Fn endgrent ,
176.Fn getgrent ,
177.Fn getgrnam ,
178.Fn getgrgid ,
179and
180.Fn setgrent
181appeared in
182.At v7 .
183The functions
184.Fn setgrfile
185and
186.Fn setgroupent
187appeared in
188.Bx 4.3 Reno .
189.Sh COMPATIBILITY
190The historic function
191.Fn setgrfile ,
192which allowed the specification of alternate password databases, has
193been deprecated and is no longer available.
194.Sh BUGS
195The functions
196.Fn getgrent ,
197.Fn getgrnam ,
198.Fn getgrgid ,
199.Fn setgroupent
200and
201.Fn setgrent
202leave their results in an internal static object and return
203a pointer to that object. Subsequent calls to
204the same function
205will modify the same object.
206