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