OSDN Git Service

c41952565f7d8d74eb2c2a012247044fd24fd0fe
[linuxjm/LDP_man-pages.git] / draft / 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 .\"*******************************************************************
25 .\"
26 .\" This file was generated with po4a. Translate the source file.
27 .\"
28 .\"*******************************************************************
29 .TH GETPROTOENT_R 3 2010\-09\-10 GNU "Linux Programmer's Manual"
30 .SH 名前
31 getprotoent_r, getprotobyname_r, getprotobynumber_r \- get protocol entry
32 (reentrant)
33 .SH 書式
34 .nf
35 \fB#include <netdb.h>\fP
36 .sp
37 \fBint getprotoent_r(struct protoent *\fP\fIresult_buf\fP\fB, char *\fP\fIbuf\fP\fB,\fP
38 \fB                size_t \fP\fIbuflen\fP\fB, struct protoent **\fP\fIresult\fP\fB);\fP
39 .sp
40 \fBint getprotobyname_r(const char *\fP\fIname\fP\fB,\fP
41 \fB                struct protoent *\fP\fIresult_buf\fP\fB, char *\fP\fIbuf\fP\fB,\fP
42 \fB                size_t \fP\fIbuflen\fP\fB, struct protoent **\fP\fIresult\fP\fB);\fP
43 .sp
44 \fBint getprotobynumber_r(int \fP\fIproto\fP\fB,\fP
45 \fB                struct protoent *\fP\fIresult_buf\fP\fB, char *\fP\fIbuf\fP\fB,\fP
46 \fB                size_t \fP\fIbuflen\fP\fB, struct protoent **\fP\fIresult\fP\fB);\fP
47 .sp
48 .fi
49 .in -4n
50 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7)  参照):
51 .ad l
52 .in
53 .sp
54 \fBgetprotoent_r\fP(), \fBgetprotobyname_r\fP(), \fBgetprotobynumber_r\fP():
55 .RS 4
56 _BSD_SOURCE || _SVID_SOURCE
57 .RE
58 .ad b
59 .SH 説明
60 The \fBgetprotoent_r\fP(), \fBgetprotobyname_r\fP(), and \fBgetprotobynumber_r\fP()
61 functions are the reentrant equivalents of, respectively, \fBgetprotoent\fP(3),
62 \fBgetprotobyname\fP(3), and \fBgetprotobynumber\fP(3).  They differ in the way
63 that the \fIprotoent\fP structure is returned, and in the function calling
64 signature and return value.  This manual page describes just the differences
65 from the nonreentrant functions.
66
67 Instead of returning a pointer to a statically allocated \fIprotoent\fP
68 structure as the function result, these functions copy the structure into
69 the location pointed to by \fIresult_buf\fP.
70
71 .\" I can find no information on the required/recommended buffer size;
72 .\" the nonreentrant functions use a 1024 byte buffer.
73 .\" The 1024 byte value is also what the Solaris man page suggests. -- mtk
74 The \fIbuf\fP array is used to store the string fields pointed to by the
75 returned \fIprotoent\fP structure.  (The nonreentrant functions allocate these
76 strings in static storage.)  The size of this array is specified in
77 \fIbuflen\fP.  If \fIbuf\fP is too small, the call fails with the error \fBERANGE\fP,
78 and the caller must try again with a larger buffer.  (A buffer of length
79 1024 bytes should be sufficient for most applications.)
80
81 If the function call successfully obtains a protocol record, then \fI*result\fP
82 is set pointing to \fIresult_buf\fP; otherwise, \fI*result\fP is set to NULL.
83 .SH 返り値
84 On success, these functions return 0.  On error, they return one of the
85 positive error numbers listed in ERRORS.
86
87 On error, record not found (\fBgetprotobyname_r\fP(), \fBgetprotobynumber_r\fP()),
88 or end of input (\fBgetprotoent_r\fP())  \fIresult\fP is set to NULL.
89 .SH エラー
90 .TP 
91 \fBENOENT\fP
92 (\fBgetprotoent_r\fP())  No more records in database.
93 .TP 
94 \fBERANGE\fP
95 \fIbuf\fP is too small.  Try again with a larger buffer (and increased
96 \fIbuflen\fP).
97 .SH 準拠
98 These functions are GNU extensions.  Functions with similar names exist on
99 some other systems, though typically with different calling signatures.
100 .SH 例
101 The program below uses \fBgetprotobyname_r\fP()  to retrieve the protocol
102 record for the protocol named in its first command\-line argument.  If a
103 second (integer) command\-line argument is supplied, it is used as the
104 initial value for \fIbuflen\fP; if \fBgetprotobyname_r\fP()  fails with the error
105 \fBERANGE\fP, the program retries with larger buffer sizes.  The following
106 shell session shows a couple of sample runs:
107 .in +4n
108 .nf
109
110 $\fB ./a.out tcp 1\fP
111 ERANGE! Retrying with larger buffer
112 getprotobyname_r() returned: 0 (success)  (buflen=78)
113 p_name=tcp; p_proto=6; aliases=TCP
114 $\fB ./a.out xxx 1\fP
115 ERANGE! Retrying with larger buffer
116 getprotobyname_r() returned: 0 (success)  (buflen=100)
117 Call failed/record not found
118 .fi
119 .in
120 .SS プログラムのソース
121 \&
122 .nf
123 #define _GNU_SOURCE
124 #include <ctype.h>
125 #include <netdb.h>
126 #include <stdlib.h>
127 #include <stdio.h>
128 #include <errno.h>
129 #include <string.h>
130
131 #define MAX_BUF 10000
132
133 int
134 main(int argc, char *argv[])
135 {
136     int buflen, erange_cnt, s;
137     struct protoent result_buf;
138     struct protoent *result;
139     char buf[MAX_BUF];
140     char **p;
141
142     if (argc < 2) {
143         printf("Usage: %s proto\-name [buflen]\en", argv[0]);
144         exit(EXIT_FAILURE);
145     }
146
147     buflen = 1024;
148     if (argc > 2)
149         buflen = atoi(argv[2]);
150
151     if (buflen > MAX_BUF) {
152         printf("Exceeded buffer limit (%d)\en", MAX_BUF);
153         exit(EXIT_FAILURE);
154     }
155
156     erange_cnt = 0;
157     do {
158         s = getprotobyname_r(argv[1], &result_buf,
159                      buf, buflen, &result);
160         if (s == ERANGE) {
161             if (erange_cnt == 0)
162                 printf("ERANGE! Retrying with larger buffer\en");
163             erange_cnt++;
164
165             /* Increment a byte at a time so we can see exactly
166                what size buffer was required */
167
168             buflen++;
169
170             if (buflen > MAX_BUF) {
171                 printf("Exceeded buffer limit (%d)\en", MAX_BUF);
172                 exit(EXIT_FAILURE);
173             }
174         }
175     } while (s == ERANGE);
176
177     printf("getprotobyname_r() returned: %s  (buflen=%d)\en",
178             (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" :
179             strerror(s), buflen);
180
181     if (s != 0 || result == NULL) {
182         printf("Call failed/record not found\en");
183         exit(EXIT_FAILURE);
184     }
185
186     printf("p_name=%s; p_proto=%d; aliases=",
187                 result_buf.p_name, result_buf.p_proto);
188     for (p = result_buf.p_aliases; *p != NULL; p++)
189         printf("%s ", *p);
190     printf("\en");
191
192     exit(EXIT_SUCCESS);
193 }
194 .fi
195 .SH 関連項目
196 \fBgetprotoent\fP(3), \fBprotocols\fP(5)