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