xref: /netbsd-src/lib/libc/gen/getgrent.3 (revision 9fbd88883c38d0c0fbfcbe66d76fe6b0fab3f9de)
1.\"	$NetBSD: getgrent.3,v 1.14 2001/09/16 02:30:24 wiz Exp $
2.\"
3.\" Copyright (c) 1989, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. All advertising materials mentioning features or use of this software
15.\"    must display the following acknowledgement:
16.\"	This product includes software developed by the University of
17.\"	California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\"     @(#)getgrent.3	8.2 (Berkeley) 4/19/94
35.\"
36.Dd April 25, 1999
37.Dt GETGRENT 3
38.Os
39.Sh NAME
40.Nm getgrent ,
41.Nm getgrnam ,
42.Nm getgrgid ,
43.Nm setgroupent ,
44.\" .Nm setgrfile ,
45.Nm setgrent ,
46.Nm endgrent
47.Nd group database operations
48.Sh LIBRARY
49.Lb libc
50.Sh SYNOPSIS
51.Fd #include <sys/types.h>
52.Fd #include <grp.h>
53.Ft struct group *
54.Fn getgrent void
55.Ft struct group *
56.Fn getgrnam "const char *name"
57.Ft struct group *
58.Fn getgrgid "gid_t gid"
59.Ft int
60.Fn setgroupent "int stayopen"
61.\" .Ft void
62.\" .Fn setgrfile "const char *name"
63.Ft void
64.Fn setgrent void
65.Ft void
66.Fn endgrent void
67.Sh DESCRIPTION
68These functions operate on the group database file which is described in
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	gid_t	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 functions 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 equivalent to
119.Fn setgroupent
120with an argument of zero.
121.Pp
122The
123.Fn endgrent
124function
125closes any open files.
126.Sh RETURN VALUES
127The functions
128.Fn getgrent ,
129.Fn getgrnam ,
130and
131.Fn getgrgid ,
132return a pointer to the group entry if successful; if end-of-file
133is reached or an error occurs a null pointer is returned.
134The
135.Fn setgroupent
136function returns the value 1 if successful, otherwise the value
1370 is returned.
138The
139.Fn endgrent
140and
141.Fn setgrent
142functions have no return value.
143.Sh FILES
144.Bl -tag -width /etc/group -compact
145.It Pa /etc/group
146group database file
147.El
148.Sh SEE ALSO
149.Xr getpwent 3 ,
150.Xr group 5 ,
151.Xr nsswitch.conf 5
152.Sh STANDARDS
153The
154.Fn getgrnam
155and
156.Fn getgrgid
157functions conform to
158.St -p1003.1-90 .
159.Sh HISTORY
160The functions
161.Fn endgrent ,
162.Fn getgrent ,
163.Fn getgrnam ,
164.Fn getgrgid ,
165and
166.Fn setgrent
167appeared in
168.At v7 .
169The functions
170.Fn setgrfile
171and
172.Fn setgroupent
173appeared in
174.Bx 4.3 Reno .
175.Sh COMPATIBILITY
176The historic function
177.Fn setgrfile ,
178which allowed the specification of alternative password databases, has
179been deprecated and is no longer available.
180.Sh BUGS
181The functions
182.Fn getgrent ,
183.Fn getgrnam ,
184.Fn getgrgid ,
185.Fn setgroupent
186and
187.Fn setgrent
188leave their results in an internal static object and return
189a pointer to that object. Subsequent calls to
190the same function
191will modify the same object.
192.Pp
193The functions
194.Fn getgrent ,
195.Fn endgrent ,
196.Fn setgroupent ,
197and
198.Fn setgrent
199are fairly useless in a networked environment and should be
200avoided, if possible.
201.Fn getgrent
202makes no attempt to suppress duplicate information if multiple
203sources are specified in
204.Xr nsswitch.conf 5
205