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

309 lines
8.4 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of GETPWENT_R</TITLE>
</HEAD><BODY>
<H1>GETPWENT_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>
getpwent_r, fgetpwent_r - get passwd file entry reentrantly
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/pwd.h">pwd.h</A>&gt;</B>
<B>int getpwent_r(struct passwd *</B><I>pwbuf</I><B>, char *</B><I>buf</I><B>,</B>
<B> size_t </B><I>buflen</I><B>, struct passwd **</B><I>pwbufp</I><B>);</B>
<B>int fgetpwent_r(FILE *</B><I>stream</I><B>, struct passwd *</B><I>pwbuf</I><B>, char *</B><I>buf</I><B>,</B>
<B> size_t </B><I>buflen</I><B>, struct passwd **</B><I>pwbufp</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>getpwent_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;_BSD_SOURCE&nbsp;||&nbsp;_SVID_SOURCE
<BR>
<B>fgetpwent_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>getpwent_r</B>()
and
<B>fgetpwent_r</B>()
are the reentrant versions of
<B><A HREF="/cgi-bin/man/man2html?3+getpwent">getpwent</A></B>(3)
and
<B><A HREF="/cgi-bin/man/man2html?3+fgetpwent">fgetpwent</A></B>(3).
The former reads the next passwd entry from the stream initialized by
<B><A HREF="/cgi-bin/man/man2html?3+setpwent">setpwent</A></B>(3).
The latter reads the next passwd entry from
<I>stream</I>.
<P>
The <I>passwd</I> structure is defined in
<I>&lt;<A HREF="file:///usr/include/pwd.h">pwd.h</A>&gt;</I>
as follows:
<P>
struct passwd {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;&nbsp;&nbsp;&nbsp;*pw_name;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;username&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;&nbsp;&nbsp;&nbsp;*pw_passwd;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;user&nbsp;password&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;uid_t&nbsp;&nbsp;&nbsp;&nbsp;pw_uid;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;user&nbsp;ID&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;gid_t&nbsp;&nbsp;&nbsp;&nbsp;pw_gid;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;group&nbsp;ID&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;&nbsp;&nbsp;&nbsp;*pw_gecos;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;user&nbsp;information&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;&nbsp;&nbsp;&nbsp;*pw_dir;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;home&nbsp;directory&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;&nbsp;&nbsp;&nbsp;*pw_shell;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;shell&nbsp;program&nbsp;*/
};
<P>
For more information about the fields of this structure, see
<B><A HREF="/cgi-bin/man/man2html?5+passwd">passwd</A></B>(5).
<P>
The nonreentrant functions return a pointer to static storage,
where this static storage contains further pointers to user
name, password, gecos field, home directory and shell.
The reentrant functions described here return all of that in
caller-provided buffers.
First of all there is the buffer
<I>pwbuf</I>
that can hold a <I>struct passwd</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 passwd</I> read from the stream,
is stored in the provided buffer
<I>*pwbuf</I>,
and a pointer to this <I>struct passwd</I> is returned in
<I>*pwbufp</I>.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success, these functions return 0 and
<I>*pwbufp</I>
is a pointer to the <I>struct passwd</I>.
On error, these functions return an error value and
<I>*pwbufp</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>getpwent_r</B>()
</TD><TD>Thread safety</TD><TD>MT-Unsafe race:pwent locale<BR></TD></TR>
<TR VALIGN=top><TD>
<B>fgetpwent_r</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<P>
In the above table,
<I>pwent</I>
in
<I>race:pwent</I>
signifies that if any of the functions
<B>setpwent</B>(),
<B>getpwent</B>(),
<B>endpwent</B>(),
or
<B>getpwent_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 passwd *
getpwent_r(struct passwd *pwd, char *buf, int buflen);
<P>
or, better,
<P>
int
getpwent_r(struct passwd *pwd, char *buf, int buflen,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FILE&nbsp;**pw_fp);
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
The function
<B>getpwent_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/pwd.h">pwd.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#define BUFLEN 4096
<P>
int
main(void)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;passwd&nbsp;pw,&nbsp;*pwp;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;buf[BUFLEN];
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;i;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;setpwent();
<BR>&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(1)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;=&nbsp;getpwent_r(&amp;pw,&nbsp;buf,&nbsp;BUFLEN,&nbsp;&amp;pwp);
<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)\tHOME&nbsp;%s\tSHELL&nbsp;%s\n&quot;,&nbsp;pwp-&gt;pw_name,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pwp-&gt;pw_uid,&nbsp;pwp-&gt;pw_dir,&nbsp;pwp-&gt;pw_shell);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<BR>&nbsp;&nbsp;&nbsp;&nbsp;endpwent();
<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+fgetpwent">fgetpwent</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+getpw">getpw</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+getpwent">getpwent</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+getpwnam">getpwnam</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+getpwuid">getpwuid</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+putpwent">putpwent</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?5+passwd">passwd</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>