OSDN Git Service

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