]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/getprotoent_r.3
man*/: srcfix (Use .P instead of .PP or .LP)
[thirdparty/man-pages.git] / man3 / getprotoent_r.3
CommitLineData
a1eaacb1 1'\" t
ca1a4bba
MK
2.\" Copyright 2008, Linux Foundation, written by Michael Kerrisk
3.\" <mtk.manpages@gmail.com>
4.\"
5fbde956 5.\" SPDX-License-Identifier: Linux-man-pages-copyleft
ca1a4bba 6.\"
4c1c5274 7.TH getprotoent_r 3 (date) "Linux man-pages (unreleased)"
ca1a4bba
MK
8.SH NAME
9getprotoent_r, getprotobyname_r, getprotobynumber_r \- get
10protocol entry (reentrant)
42009080
AC
11.SH LIBRARY
12Standard C library
13.RI ( libc ", " \-lc )
ca1a4bba
MK
14.SH SYNOPSIS
15.nf
16.B #include <netdb.h>
c6d039a3 17.P
698bf790 18.BI "int getprotoent_r(struct protoent *restrict " result_buf ,
1eed67e7 19.BI " char " buf "[restrict ." buflen "], size_t " buflen ,
698bf790
AC
20.BI " struct protoent **restrict " result );
21.BI "int getprotobyname_r(const char *restrict " name ,
22.BI " struct protoent *restrict " result_buf ,
1eed67e7 23.BI " char " buf "[restrict ." buflen "], size_t " buflen ,
698bf790 24.BI " struct protoent **restrict " result );
ca1a4bba 25.BI "int getprotobynumber_r(int " proto ,
698bf790 26.BI " struct protoent *restrict " result_buf ,
1eed67e7 27.BI " char " buf "[restrict ." buflen "], size_t " buflen ,
698bf790 28.BI " struct protoent **restrict " result );
c6d039a3 29.P
ca1a4bba 30.fi
d39ad78f 31.RS -4
ca1a4bba
MK
32Feature Test Macro Requirements for glibc (see
33.BR feature_test_macros (7)):
d39ad78f 34.RE
c6d039a3 35.P
ca1a4bba
MK
36.BR getprotoent_r (),
37.BR getprotobyname_r (),
38.BR getprotobynumber_r ():
9d2adbae 39.nf
51c612fb
MK
40 Since glibc 2.19:
41 _DEFAULT_SOURCE
75c018a1 42 glibc 2.19 and earlier:
51c612fb 43 _BSD_SOURCE || _SVID_SOURCE
9d2adbae 44.fi
ca1a4bba
MK
45.SH DESCRIPTION
46The
47.BR getprotoent_r (),
48.BR getprotobyname_r (),
49and
50.BR getprotobynumber_r ()
51functions are the reentrant equivalents of, respectively,
52.BR getprotoent (3),
53.BR getprotobyname (3),
54and
55.BR getprotobynumber (3).
56They differ in the way that the
57.I protoent
58structure is returned,
59and in the function calling signature and return value.
60This manual page describes just the differences from
54d75d6c 61the nonreentrant functions.
c6d039a3 62.P
ca1a4bba
MK
63Instead of returning a pointer to a statically allocated
64.I protoent
65structure as the function result,
66these functions copy the structure into the location pointed to by
67.IR result_buf .
c6d039a3 68.P
ca1a4bba
MK
69The
70.I buf
71array is used to store the string fields pointed to by the returned
72.I protoent
73structure.
54d75d6c 74(The nonreentrant functions allocate these strings in static storage.)
ca1a4bba
MK
75The size of this array is specified in
76.IR buflen .
77If
78.I buf
79is too small, the call fails with the error
80.BR ERANGE ,
81and the caller must try again with a larger buffer.
82(A buffer of length 1024 bytes should be sufficient for most applications.)
83.\" I can find no information on the required/recommended buffer size;
54d75d6c 84.\" the nonreentrant functions use a 1024 byte buffer.
ca1a4bba 85.\" The 1024 byte value is also what the Solaris man page suggests. -- mtk
c6d039a3 86.P
ca1a4bba
MK
87If the function call successfully obtains a protocol record, then
88.I *result
89is set pointing to
90.IR result_buf ;
91otherwise,
92.I *result
93is set to NULL.
47297adb 94.SH RETURN VALUE
ca1a4bba 95On success, these functions return 0.
535f0df5 96On error, they return one of the positive error numbers listed in ERRORS.
c6d039a3 97.P
ca1a4bba
MK
98On error, record not found
99.RB ( getprotobyname_r (),
100.BR getprotobynumber_r ()),
101or end of input
102.RB ( getprotoent_r ())
103.I result
104is set to NULL.
105.SH ERRORS
106.TP
107.B ENOENT
108.RB ( getprotoent_r ())
109No more records in database.
110.TP
111.B ERANGE
112.I buf
113is too small.
114Try again with a larger buffer
115(and increased
116.IR buflen ).
252c5d6b
ZL
117.SH ATTRIBUTES
118For an explanation of the terms used in this section, see
119.BR attributes (7).
120.TS
121allbox;
c466875e 122lbx lb lb
252c5d6b
ZL
123l l l.
124Interface Attribute Value
125T{
9e54434e
BR
126.na
127.nh
252c5d6b 128.BR getprotoent_r (),
252c5d6b 129.BR getprotobyname_r (),
252c5d6b
ZL
130.BR getprotobynumber_r ()
131T} Thread safety MT-Safe locale
132.TE
4131356c 133.SH VERSIONS
ca1a4bba
MK
134Functions with similar names exist on some other systems,
135though typically with different calling signatures.
4131356c
AC
136.SH STANDARDS
137GNU.
a14af333 138.SH EXAMPLES
ca1a4bba
MK
139The program below uses
140.BR getprotobyname_r ()
141to retrieve the protocol record for the protocol named
142in its first command-line argument.
143If a second (integer) command-line argument is supplied,
144it is used as the initial value for
145.IR buflen ;
146if
147.BR getprotobyname_r ()
148fails with the error
149.BR ERANGE ,
150the program retries with larger buffer sizes.
151The following shell session shows a couple of sample runs:
c6d039a3 152.P
ca1a4bba 153.in +4n
e646a1ba 154.EX
b43a3b30 155.RB "$" " ./a.out tcp 1"
ca1a4bba
MK
156ERANGE! Retrying with larger buffer
157getprotobyname_r() returned: 0 (success) (buflen=78)
158p_name=tcp; p_proto=6; aliases=TCP
b43a3b30 159.RB "$" " ./a.out xxx 1"
ca1a4bba
MK
160ERANGE! Retrying with larger buffer
161getprotobyname_r() returned: 0 (success) (buflen=100)
162Call failed/record not found
b8302363 163.EE
ca1a4bba 164.in
9c330504 165.SS Program source
d84d0300 166\&
b0b6ab4e 167.\" SRC BEGIN (getprotoent_r.c)
e7d0bb47 168.EX
ca1a4bba
MK
169#define _GNU_SOURCE
170#include <ctype.h>
ad3868f0 171#include <errno.h>
ca1a4bba 172#include <netdb.h>
ca1a4bba 173#include <stdio.h>
ad3868f0 174#include <stdlib.h>
ca1a4bba 175#include <string.h>
fe5dba13 176\&
ca1a4bba 177#define MAX_BUF 10000
fe5dba13 178\&
ca1a4bba
MK
179int
180main(int argc, char *argv[])
181{
182 int buflen, erange_cnt, s;
183 struct protoent result_buf;
184 struct protoent *result;
185 char buf[MAX_BUF];
fe5dba13 186\&
ca1a4bba 187 if (argc < 2) {
d1a71985 188 printf("Usage: %s proto\-name [buflen]\en", argv[0]);
ca1a4bba
MK
189 exit(EXIT_FAILURE);
190 }
fe5dba13 191\&
ca1a4bba
MK
192 buflen = 1024;
193 if (argc > 2)
194 buflen = atoi(argv[2]);
fe5dba13 195\&
ca1a4bba 196 if (buflen > MAX_BUF) {
d1a71985 197 printf("Exceeded buffer limit (%d)\en", MAX_BUF);
ca1a4bba
MK
198 exit(EXIT_FAILURE);
199 }
fe5dba13 200\&
ca1a4bba
MK
201 erange_cnt = 0;
202 do {
203 s = getprotobyname_r(argv[1], &result_buf,
d917c31d 204 buf, buflen, &result);
ca1a4bba
MK
205 if (s == ERANGE) {
206 if (erange_cnt == 0)
d1a71985 207 printf("ERANGE! Retrying with larger buffer\en");
ca1a4bba 208 erange_cnt++;
fe5dba13 209\&
ca1a4bba 210 /* Increment a byte at a time so we can see exactly
46b20ca1 211 what size buffer was required. */
fe5dba13 212\&
ca1a4bba 213 buflen++;
fe5dba13 214\&
ca1a4bba 215 if (buflen > MAX_BUF) {
d1a71985 216 printf("Exceeded buffer limit (%d)\en", MAX_BUF);
ca1a4bba
MK
217 exit(EXIT_FAILURE);
218 }
219 }
220 } while (s == ERANGE);
fe5dba13 221\&
d1a71985 222 printf("getprotobyname_r() returned: %s (buflen=%d)\en",
d917c31d
AC
223 (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" :
224 strerror(s), buflen);
fe5dba13 225\&
ca1a4bba 226 if (s != 0 || result == NULL) {
d1a71985 227 printf("Call failed/record not found\en");
ca1a4bba
MK
228 exit(EXIT_FAILURE);
229 }
fe5dba13 230\&
ca1a4bba 231 printf("p_name=%s; p_proto=%d; aliases=",
d917c31d 232 result_buf.p_name, result_buf.p_proto);
88893a77 233 for (char **p = result_buf.p_aliases; *p != NULL; p++)
ca1a4bba 234 printf("%s ", *p);
d1a71985 235 printf("\en");
fe5dba13 236\&
ca1a4bba
MK
237 exit(EXIT_SUCCESS);
238}
e7d0bb47 239.EE
b0b6ab4e 240.\" SRC END
47297adb 241.SH SEE ALSO
ca1a4bba 242.BR getprotoent (3),
ca1a4bba 243.BR protocols (5)