man-pages/man3/getgrent_r.3.html
2021-03-31 01:06:50 +01:00

308 lines
8.6 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of GETGRENT_R</TITLE>
</HEAD><BODY>
<H1>GETGRENT_R</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2017-09-15<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
getgrent_r, fgetgrent_r - get group file entry reentrantly
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/grp.h">grp.h</A>&gt;</B>
<B>int getgrent_r(struct group *</B><I>gbuf</I><B>, char *</B><I>buf</I><B>,</B>
<B> size_t </B><I>buflen</I><B>, struct group **</B><I>gbufp</I><B>);</B>
<B>int fgetgrent_r(FILE *</B><I>stream</I><B>, struct group *</B><I>gbuf</I><B>, char *</B><I>buf</I><B>,</B>
<B> size_t </B><I>buflen</I><B>, struct group **</B><I>gbufp</I><B>);</B>
</PRE>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>getgrent_r</B>():
_GNU_SOURCE
<BR>
<B>fgetgrent_r</B>():
<BR>&nbsp;&nbsp;&nbsp;&nbsp;Since&nbsp;glibc&nbsp;2.19:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_DEFAULT_SOURCE
<BR>&nbsp;&nbsp;&nbsp;&nbsp;Glibc&nbsp;2.19&nbsp;and&nbsp;earlier:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_SVID_SOURCE
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The functions
<B>getgrent_r</B>()
and
<B>fgetgrent_r</B>()
are the reentrant versions of
<B><A HREF="/cgi-bin/man/man2html?3+getgrent">getgrent</A></B>(3)
and
<B><A HREF="/cgi-bin/man/man2html?3+fgetgrent">fgetgrent</A></B>(3).
The former reads the next group entry from the stream initialized by
<B><A HREF="/cgi-bin/man/man2html?3+setgrent">setgrent</A></B>(3).
The latter reads the next group entry from
<I>stream</I>.
<P>
The <I>group</I> structure is defined in
<I>&lt;<A HREF="file:///usr/include/grp.h">grp.h</A>&gt;</I>
as follows:
<P>
struct group {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;&nbsp;&nbsp;*gr_name;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;group&nbsp;name&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;&nbsp;&nbsp;*gr_passwd;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;group&nbsp;password&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;gid_t&nbsp;&nbsp;&nbsp;gr_gid;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;group&nbsp;ID&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;&nbsp;**gr_mem;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;NULL-terminated&nbsp;array&nbsp;of&nbsp;pointers
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;to&nbsp;names&nbsp;of&nbsp;group&nbsp;members&nbsp;*/
};
<P>
For more information about the fields of this structure, see
<B><A HREF="/cgi-bin/man/man2html?5+group">group</A></B>(5).
<P>
The nonreentrant functions return a pointer to static storage,
where this static storage contains further pointers to group
name, password and members.
The reentrant functions described here return all of that in
caller-provided buffers.
First of all there is the buffer
<I>gbuf</I>
that can hold a <I>struct group</I>.
And next the buffer
<I>buf</I>
of size
<I>buflen</I>
that can hold additional strings.
The result of these functions, the <I>struct group</I> read from the stream,
is stored in the provided buffer
<I>*gbuf</I>,
and a pointer to this <I>struct group</I> is returned in
<I>*gbufp</I>.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success, these functions return 0 and
<I>*gbufp</I>
is a pointer to the <I>struct group</I>.
On error, these functions return an error value and
<I>*gbufp</I>
is NULL.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="1"><B>ENOENT</B>
<DD>
No more entries.
<DT id="2"><B>ERANGE</B>
<DD>
Insufficient buffer space supplied.
Try again with larger buffer.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>getgrent_r</B>()
</TD><TD>Thread safety</TD><TD>MT-Unsafe race:grent locale<BR></TD></TR>
<TR VALIGN=top><TD>
<B>fgetgrent_r</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<P>
In the above table,
<I>grent</I>
in
<I>race:grent</I>
signifies that if any of the functions
<B>setgrent</B>(),
<B>getgrent</B>(),
<B>endgrent</B>(),
or
<B>getgrent_r</B>()
are used in parallel in different threads of a program,
then data races could occur.
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
These functions are GNU extensions, done in a style resembling
the POSIX version of functions like
<B><A HREF="/cgi-bin/man/man2html?3+getpwnam_r">getpwnam_r</A></B>(3).
Other systems use the prototype
<P>
struct group *getgrent_r(struct group *grp, char *buf,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;buflen);
<P>
or, better,
<P>
int getgrent_r(struct group *grp, char *buf, int buflen,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FILE&nbsp;**gr_fp);
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
The function
<B>getgrent_r</B>()
is not really reentrant since it shares the reading position
in the stream with all other threads.
<A NAME="lbAJ">&nbsp;</A>
<H2>EXAMPLE</H2>
#define _GNU_SOURCE
#include &lt;<A HREF="file:///usr/include/grp.h">grp.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#define BUFLEN 4096
<P>
int
main(void)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;group&nbsp;grp,&nbsp;*grpp;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;buf[BUFLEN];
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;i;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;setgrent();
<BR>&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(1)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;=&nbsp;getgrent_r(&amp;grp,&nbsp;buf,&nbsp;BUFLEN,&nbsp;&amp;grpp);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(i)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;%s&nbsp;(%d):&quot;,&nbsp;grpp-&gt;gr_name,&nbsp;grpp-&gt;gr_gid);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(i&nbsp;=&nbsp;0;&nbsp;;&nbsp;i++)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(grpp-&gt;gr_mem[i]&nbsp;==&nbsp;NULL)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;&nbsp;%s&quot;,&nbsp;grpp-&gt;gr_mem[i]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<BR>&nbsp;&nbsp;&nbsp;&nbsp;endgrent();
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAK">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?3+fgetgrent">fgetgrent</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+getgrent">getgrent</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+getgrgid">getgrgid</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+getgrnam">getgrnam</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+putgrent">putgrent</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?5+group">group</A></B>(5)
<A NAME="lbAL">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="3"><A HREF="#lbAB">NAME</A><DD>
<DT id="4"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="5"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="6"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="7"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="8"><A HREF="#lbAG">ATTRIBUTES</A><DD>
<DT id="9"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="10"><A HREF="#lbAI">NOTES</A><DD>
<DT id="11"><A HREF="#lbAJ">EXAMPLE</A><DD>
<DT id="12"><A HREF="#lbAK">SEE ALSO</A><DD>
<DT id="13"><A HREF="#lbAL">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:44 GMT, March 31, 2021
</BODY>
</HTML>