OSDN Git Service

Import translated manuals from JM CVS Repository.
[linuxjm/jm.git] / manual / LDP_man-pages / original / man3 / getprotoent_r.3
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 .\"
24 .TH GETPROTOENT_R 3  2008-08-19 "GNU" "Linux Programmer's Manual"
25 .SH NAME
26 getprotoent_r, getprotobyname_r, getprotobynumber_r \- get
27 protocol 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
45 Feature 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 ():
53 _BSD_SOURCE || _SVID_SOURCE
54 .ad b
55 .SH DESCRIPTION
56 The
57 .BR getprotoent_r (),
58 .BR getprotobyname_r (),
59 and
60 .BR getprotobynumber_r ()
61 functions are the reentrant equivalents of, respectively,
62 .BR getprotoent (3),
63 .BR getprotobyname (3),
64 and
65 .BR getprotobynumber (3).
66 They differ in the way that the
67 .I protoent
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.
72
73 Instead of returning a pointer to a statically allocated
74 .I protoent
75 structure as the function result,
76 these functions copy the structure into the location pointed to by
77 .IR result_buf .
78
79 The
80 .I buf
81 array is used to store the string fields pointed to by the returned
82 .I protoent
83 structure.
84 (The nonreentrant functions allocate these strings in static storage.)
85 The size of this array is specified in
86 .IR buflen .
87 If
88 .I buf
89 is too small, the call fails with the error
90 .BR ERANGE ,
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
96
97 If the function call successfully obtains a protocol record, then
98 .I *result
99 is set pointing to
100 .IR result_buf ;
101 otherwise,
102 .I *result
103 is set to NULL.
104 .SH "RETURN VALUE"
105 On success, these functions return 0.
106 On error, a positive error number is returned.
107
108 On error, record not found
109 .RB ( getprotobyname_r (),
110 .BR getprotobynumber_r ()),
111 or end of input
112 .RB ( getprotoent_r ())
113 .I result
114 is set to NULL.
115 .SH ERRORS
116 .TP
117 .B ENOENT
118 .RB ( getprotoent_r ())
119 No more records in database.
120 .TP
121 .B ERANGE
122 .I buf
123 is too small.
124 Try again with a larger buffer
125 (and increased
126 .IR buflen ).
127 .SH "CONFORMING TO"
128 These functions are GNU extensions.
129 Functions with similar names exist on some other systems,
130 though typically with different calling signatures.
131 .SH EXAMPLE
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
138 .IR buflen ;
139 if
140 .BR getprotobyname_r ()
141 fails with the error
142 .BR ERANGE ,
143 the program retries with larger buffer sizes.
144 The following shell session shows a couple of sample runs:
145 .in +4n
146 .nf
147
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
156 .fi
157 .in
158 .SS Program source
159 \&
160 .nf
161 #define _GNU_SOURCE
162 #include <ctype.h>
163 #include <netdb.h>
164 #include <stdlib.h>
165 #include <stdio.h>
166 #include <errno.h>
167 #include <string.h>
168
169 #define MAX_BUF 10000
170
171 int
172 main(int argc, char *argv[])
173 {
174     int buflen, erange_cnt, s;
175     struct protoent result_buf;
176     struct protoent *result;
177     char buf[MAX_BUF];
178     char **p;
179
180     if (argc < 2) {
181         printf("Usage: %s proto\-name [buflen]\\n", argv[0]);
182         exit(EXIT_FAILURE);
183     }
184
185     buflen = 1024;
186     if (argc > 2)
187         buflen = atoi(argv[2]);
188
189     if (buflen > MAX_BUF) {
190         printf("Exceeded buffer limit (%d)\\n", MAX_BUF);
191         exit(EXIT_FAILURE);
192     }
193
194     erange_cnt = 0;
195     do {
196         s = getprotobyname_r(argv[1], &result_buf,
197                      buf, buflen, &result);
198         if (s == ERANGE) {
199             if (erange_cnt == 0)
200                 printf("ERANGE! Retrying with larger buffer\\n");
201             erange_cnt++;
202
203             /* Increment a byte at a time so we can see exactly
204                what size buffer was required */
205
206             buflen++;
207
208             if (buflen > MAX_BUF) {
209                 printf("Exceeded buffer limit (%d)\\n", MAX_BUF);
210                 exit(EXIT_FAILURE);
211             }
212         }
213     } while (s == ERANGE);
214
215     printf("getprotobyname_r() returned: %s  (buflen=%d)\\n",
216             (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" :
217             strerror(s), buflen);
218
219     if (s != 0 || result == NULL) {
220         printf("Call failed/record not found\\n");
221         exit(EXIT_FAILURE);
222     }
223
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++)
227         printf("%s ", *p);
228     printf("\\n");
229
230     exit(EXIT_SUCCESS);
231 }
232 .fi
233 .SH "SEE ALSO"
234 .BR getprotoent (3),
235 .BR protocols (5)