xref: /netbsd-src/lib/libc/gen/getgrent.3 (revision cda4f8f6ee55684e8d311b86c99ea59191e6b74f)
1.\" Copyright (c) 1989, 1991 The Regents of the University of California.
2.\"  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	6.8 (Berkeley) 4/20/91
33.\"	$Id: getgrent.3,v 1.3 1993/07/30 08:36:31 mycroft Exp $
34.\"
35.Dd April 20, 1991
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 necesssary.
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 RETURN VALUES
126The functions
127.Fn getgrent ,
128.Fn getgrnam ,
129and
130.Fn getgrgid ,
131return a pointer to the group entry if successful; if end-of-file
132is reached or an error occurs a null pointer is returned.
133The functions
134.Fn setgroupent
135and
136.Fn setgrent
137return the value 1 if successful, otherwise the value
1380 is returned.
139The functions
140.Fn endgrent
141and
142.Fn setgrfile
143have 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.Fn getpwent 3 ,
151.Fn 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