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