OSDN Git Service

(split) LDP: draft snapshot from latest ja.po files.
[linuxjm/LDP_man-pages.git] / draft / man3 / getservent_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 GETSERVENT_R 3 2010\-09\-10 GNU "Linux Programmer's Manual"
30 .SH 名前
31 getservent_r, getservbyname_r, getservbyport_r \- サービスエントリを
32 取得する (リエントラント版)
33 .SH 書式
34 .nf
35 \fB#include <netdb.h>\fP
36 .sp
37 \fBint getservent_r(struct servent *\fP\fIresult_buf\fP\fB, char *\fP\fIbuf\fP\fB,\fP
38 \fB                size_t \fP\fIbuflen\fP\fB, struct servent **\fP\fIresult\fP\fB);\fP
39 .sp
40 \fBint getservbyname_r(const char *\fP\fIname\fP\fB, const char *\fP\fIproto\fP\fB,\fP
41 \fB                struct servent *\fP\fIresult_buf\fP\fB, char *\fP\fIbuf\fP\fB,\fP
42 \fB                size_t \fP\fIbuflen\fP\fB, struct servent **\fP\fIresult\fP\fB);\fP
43 .sp
44 \fBint getservbyport_r(int \fP\fIport\fP\fB, const char *\fP\fIproto\fP\fB,\fP
45 \fB                struct servent *\fP\fIresult_buf\fP\fB, char *\fP\fIbuf\fP\fB,\fP
46 \fB                size_t \fP\fIbuflen\fP\fB, struct servent **\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 \fBgetservent_r\fP(), \fBgetservbyname_r\fP(), \fBgetservbyport_r\fP():
55 .RS 4
56 _BSD_SOURCE || _SVID_SOURCE
57 .RE
58 .ad b
59 .SH 説明
60 関数 \fBgetservent_r\fP(), \fBgetservbyname_r\fP(), \fBgetservbyport_r\fP() は、
61 それぞれ \fBgetservent\fP(3), \fBgetservbyname\fP(3), \fBgetservbyport\fP(3) の
62 リエントラント版である。
63 \fIservent\fP 構造体の返し方と、関数呼び出し時の引き数と返り値が異なる。
64 このマニュアルページでは、リエントラントでない関数との違いだけを
65 説明する。
66
67 これらの関数は、関数の結果として静的に割り当てられた \fIservent\fP 構造体
68 へのポインタを返すのではなく、 \fIservent\fP 構造体を \fIresult_buf\fP が
69 指す場所にコピーする。
70
71 .\" I can find no information on the required/recommended buffer size;
72 .\" the nonreentrant functions use a 1024 byte buffer -- mtk.
73 配列 \fIbuf\fP は、返される \fIservent\fP 構造体が指す文字列フィールドを
74 格納するのに使用される (リエントラントでない関数の場合は、
75 これらの文字列は静的な領域に格納される)。
76 この配列の大きさは \fIbuflen\fP で指定される。
77 \fIbuf\fP が小さすぎる場合、関数呼び出しはエラー \fBERANGE\fP で失敗し、
78 呼び出し側ではもっと大きなバッファで再度呼び出す必要がある (ほとんどの
79 アプリケーションでは、長さ 1024 バイトのバッファで十分なはずである)。
80
81 関数呼び出しでサービスレコードの取得に成功すると、
82 \fI*result\fP は \fIresult_buf\fP を指すように設定される。
83 それ以外の場合は \fI*result\fP に NULL が設定される。
84 .SH 返り値
85 成功すると、これらの関数は 0 を返す。エラーの場合、「エラー」の節の
86 リストにある正のエラー番号のいずれかを返す。
87
88 エラーの場合、レコードが見つからなかった場合 (\fBgetservbyname_r\fP(),
89 \fBgetservbyaddr_r\fP()) やこれ以上レコードがない場合 (\fBgetservent_r\fP())、
90 \fIresult\fP には NULL が設定される。
91 .SH エラー
92 .TP 
93 \fBENOENT\fP
94 (\fBgetservent_r\fP())  データベースにこれ以上レコードがない。
95 .TP 
96 \fBERANGE\fP
97 \fIbuf\fP が小さすぎる。もっと大きなバッファにして
98 (または \fIbuflen\fP を増やして) 再度呼び出すこと。
99 .SH 準拠
100 これらの関数は GNU による拡張である。
101 他のシステムにも同様の名前の関数が存在する場合があるが、
102 通常は関数の引き数が異なる。
103 .SH 例
104 以下のプログラムは、 \fBgetservbyport_r\fP() を使って、コマンド
105 ライン引き数で指定されたポート番号とプロトコル名を持つ
106 サービスレコードを取得する。
107 三番目のコマンドライン引き数 (整数値) が指定された場合は、
108 その値が \fIbuflen\fP の初期値として使用される。
109 \fBgetservbyport_r\fP() がエラー \fBERANGE\fP で失敗すると、プログラムは
110 より大きなバッファサイズで再度 \fBgetservbyport_r\fP を呼び出す。
111 下記のシェルのセッションは実行例を示している。
112 .in +4n
113 .nf
114
115 $\fB ./a.out 7 tcp 1\fP
116 ERANGE! Retrying with larger buffer
117 getservbyport_r() returned: 0 (success)  (buflen=87)
118 s_name=echo; s_proto=tcp; s_port=7; aliases=
119 $\fB ./a.out 77777 tcp\fP
120 getservbyport_r() returned: 0 (success)  (buflen=1024)
121 Call failed/record not found
122 .fi
123 .in
124 .SS プログラムのソース
125 \&
126 .nf
127 #define _GNU_SOURCE
128 #include <ctype.h>
129 #include <netdb.h>
130 #include <stdlib.h>
131 #include <stdio.h>
132 #include <errno.h>
133 #include <string.h>
134
135 #define MAX_BUF 10000
136
137 int
138 main(int argc, char *argv[])
139 {
140     int buflen, erange_cnt, port, s;
141     struct servent result_buf;
142     struct servent *result;
143     char buf[MAX_BUF];
144     char *protop;
145     char **p;
146
147     if (argc < 3) {
148         printf("Usage: %s port\-num proto\-name [buflen]\en", argv[0]);
149         exit(EXIT_FAILURE);
150     }
151
152     port = htons(atoi(argv[1]));
153     protop = (strcmp(argv[2], "null") == 0 ||
154               strcmp(argv[2], "NULL") == 0) ?  NULL : argv[2];
155
156     buflen = 1024;
157     if (argc > 3)
158         buflen = atoi(argv[3]);
159
160     if (buflen > MAX_BUF) {
161         printf("Exceeded buffer limit (%d)\en", MAX_BUF);
162         exit(EXIT_FAILURE);
163     }
164
165     erange_cnt = 0;
166     do {
167         s = getservbyport_r(port, protop, &result_buf,
168                      buf, buflen, &result);
169         if (s == ERANGE) {
170             if (erange_cnt == 0)
171                 printf("ERANGE! Retrying with larger buffer\en");
172             erange_cnt++;
173
174             /* Increment a byte at a time so we can see exactly
175                what size buffer was required */
176
177             buflen++;
178
179             if (buflen > MAX_BUF) {
180                 printf("Exceeded buffer limit (%d)\en", MAX_BUF);
181                 exit(EXIT_FAILURE);
182             }
183         }
184     } while (s == ERANGE);
185
186     printf("getservbyport_r() returned: %s  (buflen=%d)\en",
187             (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" :
188             strerror(s), buflen);
189
190     if (s != 0 || result == NULL) {
191         printf("Call failed/record not found\en");
192         exit(EXIT_FAILURE);
193     }
194
195     printf("s_name=%s; s_proto=%s; s_port=%d; aliases=",
196                 result_buf.s_name, result_buf.s_proto,
197                 ntohs(result_buf.s_port));
198     for (p = result_buf.s_aliases; *p != NULL; p++)
199         printf("%s ", *p);
200     printf("\en");
201
202     exit(EXIT_SUCCESS);
203 }
204 .fi
205 .SH 関連項目
206 \fBgetservent\fP(3), \fBservices\fP(5)
207 .SH この文書について
208 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.40 の一部
209 である。プロジェクトの説明とバグ報告に関する情報は
210 http://www.kernel.org/doc/man\-pages/ に書かれている。