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

349 lines
11 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of GETPROTOENT_R</TITLE>
</HEAD><BODY>
<H1>GETPROTOENT_R</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<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>
getprotoent_r, getprotobyname_r, getprotobynumber_r - get
protocol entry (reentrant)
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/netdb.h">netdb.h</A>&gt;</B>
<B>int getprotoent_r(struct protoent *</B><I>result_buf</I><B>, char *</B><I>buf</I><B>,</B>
<B> size_t </B><I>buflen</I><B>, struct protoent **</B><I>result</I><B>);</B>
<B>int getprotobyname_r(const char *</B><I>name</I><B>,</B>
<B> struct protoent *</B><I>result_buf</I><B>, char *</B><I>buf</I><B>,</B>
<B> size_t </B><I>buflen</I><B>, struct protoent **</B><I>result</I><B>);</B>
<B>int getprotobynumber_r(int </B><I>proto</I><B>,</B>
<B> struct protoent *</B><I>result_buf</I><B>, char *</B><I>buf</I><B>,</B>
<B> size_t </B><I>buflen</I><B>, struct protoent **</B><I>result</I><B>);</B>
</PRE>
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>getprotoent_r</B>(),
<B>getprotobyname_r</B>(),
<B>getprotobynumber_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
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>getprotoent_r</B>(),
<B>getprotobyname_r</B>(),
and
<B>getprotobynumber_r</B>()
functions are the reentrant equivalents of, respectively,
<B><A HREF="/cgi-bin/man/man2html?3+getprotoent">getprotoent</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+getprotobyname">getprotobyname</A></B>(3),
and
<B><A HREF="/cgi-bin/man/man2html?3+getprotobynumber">getprotobynumber</A></B>(3).
They differ in the way that the
<I>protoent</I>
structure is returned,
and in the function calling signature and return value.
This manual page describes just the differences from
the nonreentrant functions.
<P>
Instead of returning a pointer to a statically allocated
<I>protoent</I>
structure as the function result,
these functions copy the structure into the location pointed to by
<I>result_buf</I>.
<P>
The
<I>buf</I>
array is used to store the string fields pointed to by the returned
<I>protoent</I>
structure.
(The nonreentrant functions allocate these strings in static storage.)
The size of this array is specified in
<I>buflen</I>.
If
<I>buf</I>
is too small, the call fails with the error
<B>ERANGE</B>,
and the caller must try again with a larger buffer.
(A buffer of length 1024 bytes should be sufficient for most applications.)
<P>
If the function call successfully obtains a protocol record, then
<I>*result</I>
is set pointing to
<I>result_buf</I>;
otherwise,
<I>*result</I>
is set to NULL.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success, these functions return 0.
On error, they return one of the positive error numbers listed in ERRORS.
<P>
On error, record not found
(<B>getprotobyname_r</B>(),
<B>getprotobynumber_r</B>()),
or end of input
(<B>getprotoent_r</B>())
<I>result</I>
is set to NULL.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="1"><B>ENOENT</B>
<DD>
(<B>getprotoent_r</B>())
No more records in database.
<DT id="2"><B>ERANGE</B>
<DD>
<I>buf</I>
is too small.
Try again with a larger buffer
(and increased
<I>buflen</I>).
</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>getprotoent_r</B>(),
<BR>
<B>getprotobyname_r</B>(),
<BR>
<B>getprotobynumber_r</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe locale<BR></TD></TR>
</TABLE>
<P>
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
These functions are GNU extensions.
Functions with similar names exist on some other systems,
though typically with different calling signatures.
<A NAME="lbAI">&nbsp;</A>
<H2>EXAMPLE</H2>
The program below uses
<B>getprotobyname_r</B>()
to retrieve the protocol record for the protocol named
in its first command-line argument.
If a second (integer) command-line argument is supplied,
it is used as the initial value for
<I>buflen</I>;
if
<B>getprotobyname_r</B>()
fails with the error
<B>ERANGE</B>,
the program retries with larger buffer sizes.
The following shell session shows a couple of sample runs:
<P>
$<B> ./a.out tcp 1</B>
ERANGE! Retrying with larger buffer
getprotobyname_r() returned: 0 (success) (buflen=78)
p_name=tcp; p_proto=6; aliases=TCP
$<B> ./a.out xxx 1</B>
ERANGE! Retrying with larger buffer
getprotobyname_r() returned: 0 (success) (buflen=100)
Call failed/record not found
<A NAME="lbAJ">&nbsp;</A>
<H3>Program source</H3>
#define _GNU_SOURCE
#include &lt;<A HREF="file:///usr/include/ctype.h">ctype.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/netdb.h">netdb.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/errno.h">errno.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/string.h">string.h</A>&gt;
<P>
#define MAX_BUF 10000
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;buflen,&nbsp;erange_cnt,&nbsp;s;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;protoent&nbsp;result_buf;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;protoent&nbsp;*result;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;buf[MAX_BUF];
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;**p;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(argc&nbsp;&lt;&nbsp;2)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Usage:&nbsp;%s&nbsp;proto-name&nbsp;[buflen]\n&quot;,&nbsp;argv[0]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;buflen&nbsp;=&nbsp;1024;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(argc&nbsp;&gt;&nbsp;2)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buflen&nbsp;=&nbsp;atoi(argv[2]);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(buflen&nbsp;&gt;&nbsp;MAX_BUF)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Exceeded&nbsp;buffer&nbsp;limit&nbsp;(%d)\n&quot;,&nbsp;MAX_BUF);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp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;:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strerror(s),&nbsp;buflen);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;!=&nbsp;0&nbsp;||&nbsp;result&nbsp;==&nbsp;NULL)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Call&nbsp;failed/record&nbsp;not&nbsp;found\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;p_name=%s;&nbsp;p_proto=%d;&nbsp;aliases=&quot;,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result_buf.p_name,&nbsp;result_buf.p_proto);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(p&nbsp;=&nbsp;result_buf.p_aliases;&nbsp;*p&nbsp;!=&nbsp;NULL;&nbsp;p++)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;%s&nbsp;&quot;,&nbsp;*p);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n&quot;);
<P>
<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+getprotoent">getprotoent</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?5+protocols">protocols</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">EXAMPLE</A><DD>
<DL>
<DT id="11"><A HREF="#lbAJ">Program source</A><DD>
</DL>
<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>