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