1 .\" Copyright 2008, Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
24 .TH GETPROTOENT_R 3 2008-08-19 "GNU" "Linux Programmer's Manual"
26 getprotoent_r, getprotobyname_r, getprotobynumber_r \- get
27 protocol entry (reentrant)
32 .BI "int getprotoent_r(struct protoent *" result_buf ", char *" buf ,
33 .BI " size_t " buflen ", struct protoent **" result );
35 .BI "int getprotobyname_r(const char *" name ,
36 .BI " struct protoent *" result_buf ", char *" buf ,
37 .BI " size_t " buflen ", struct protoent **" result );
39 .BI "int getprotobynumber_r(int " proto ,
40 .BI " struct protoent *" result_buf ", char *" buf ,
41 .BI " size_t " buflen ", struct protoent **" result );
45 Feature Test Macro Requirements for glibc (see
46 .BR feature_test_macros (7)):
51 .BR getprotobyname_r (),
52 .BR getprotobynumber_r ():
53 _BSD_SOURCE || _SVID_SOURCE
58 .BR getprotobyname_r (),
60 .BR getprotobynumber_r ()
61 functions are the reentrant equivalents of, respectively,
63 .BR getprotobyname (3),
65 .BR getprotobynumber (3).
66 They differ in the way that the
68 structure is returned,
69 and in the function calling signature and return value.
70 This manual page describes just the differences from
71 the nonreentrant functions.
73 Instead of returning a pointer to a statically allocated
75 structure as the function result,
76 these functions copy the structure into the location pointed to by
81 array is used to store the string fields pointed to by the returned
84 (The nonreentrant functions allocate these strings in static storage.)
85 The size of this array is specified in
89 is too small, the call fails with the error
91 and the caller must try again with a larger buffer.
92 (A buffer of length 1024 bytes should be sufficient for most applications.)
93 .\" I can find no information on the required/recommended buffer size;
94 .\" the nonreentrant functions use a 1024 byte buffer.
95 .\" The 1024 byte value is also what the Solaris man page suggests. -- mtk
97 If the function call successfully obtains a protocol record, then
105 On success, these functions return 0.
106 On error, a positive error number is returned.
108 On error, record not found
109 .RB ( getprotobyname_r (),
110 .BR getprotobynumber_r ()),
112 .RB ( getprotoent_r ())
118 .RB ( getprotoent_r ())
119 No more records in database.
124 Try again with a larger buffer
128 These functions are GNU extensions.
129 Functions with similar names exist on some other systems,
130 though typically with different calling signatures.
132 The program below uses
133 .BR getprotobyname_r ()
134 to retrieve the protocol record for the protocol named
135 in its first command-line argument.
136 If a second (integer) command-line argument is supplied,
137 it is used as the initial value for
140 .BR getprotobyname_r ()
143 the program retries with larger buffer sizes.
144 The following shell session shows a couple of sample runs:
148 .RB "$" " ./a.out tcp 1"
149 ERANGE! Retrying with larger buffer
150 getprotobyname_r() returned: 0 (success) (buflen=78)
151 p_name=tcp; p_proto=6; aliases=TCP
152 .RB "$" " ./a.out xxx 1"
153 ERANGE! Retrying with larger buffer
154 getprotobyname_r() returned: 0 (success) (buflen=100)
155 Call failed/record not found
169 #define MAX_BUF 10000
172 main(int argc, char *argv[])
174 int buflen, erange_cnt, s;
175 struct protoent result_buf;
176 struct protoent *result;
181 printf("Usage: %s proto\-name [buflen]\\n", argv[0]);
187 buflen = atoi(argv[2]);
189 if (buflen > MAX_BUF) {
190 printf("Exceeded buffer limit (%d)\\n", MAX_BUF);
196 s = getprotobyname_r(argv[1], &result_buf,
197 buf, buflen, &result);
200 printf("ERANGE! Retrying with larger buffer\\n");
203 /* Increment a byte at a time so we can see exactly
204 what size buffer was required */
208 if (buflen > MAX_BUF) {
209 printf("Exceeded buffer limit (%d)\\n", MAX_BUF);
213 } while (s == ERANGE);
215 printf("getprotobyname_r() returned: %s (buflen=%d)\\n",
216 (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" :
217 strerror(s), buflen);
219 if (s != 0 || result == NULL) {
220 printf("Call failed/record not found\\n");
224 printf("p_name=%s; p_proto=%d; aliases=",
225 result_buf.p_name, result_buf.p_proto);
226 for (p = result_buf.p_aliases; *p != NULL; p++)