OSDN Git Service

(split) LDP: Release pages for LDP v3.39.
[linuxjm/LDP_man-pages.git] / release / 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 \- get service entry
32 (reentrant)
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 The \fBgetservent_r\fP(), \fBgetservbyname_r\fP(), and \fBgetservbyport_r\fP()
61 functions are the reentrant equivalents of, respectively, \fBgetservent\fP(3),
62 \fBgetservbyname\fP(3), and \fBgetservbyport\fP(3).  They differ in the way that
63 the \fIservent\fP structure is returned, and in the function calling signature
64 and return value.  This manual page describes just the differences from the
65 nonreentrant functions.
66
67 Instead of returning a pointer to a statically allocated \fIservent\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 -- mtk.
73 The \fIbuf\fP array is used to store the string fields pointed to by the
74 returned \fIservent\fP structure.  (The nonreentrant functions allocate these
75 strings in static storage.)  The size of this array is specified in
76 \fIbuflen\fP.  If \fIbuf\fP is too small, the call fails with the error \fBERANGE\fP,
77 and the caller must try again with a larger buffer.  (A buffer of length
78 1024 bytes should be sufficient for most applications.)
79
80 If the function call successfully obtains a service record, then \fI*result\fP
81 is set pointing to \fIresult_buf\fP; otherwise, \fI*result\fP is set to NULL.
82 .SH 返り値
83 On success, these functions return 0.  On error, they return one of the
84 positive error numbers listed in errors.
85
86 On error, record not found (\fBgetservbyname_r\fP(), \fBgetservbyport_r\fP()), or
87 end of input (\fBgetservent_r\fP())  \fIresult\fP is set to NULL.
88 .SH エラー
89 .TP 
90 \fBENOENT\fP
91 (\fBgetservent_r\fP())  No more records in database.
92 .TP 
93 \fBERANGE\fP
94 \fIbuf\fP is too small.  Try again with a larger buffer (and increased
95 \fIbuflen\fP).
96 .SH 準拠
97 These functions are GNU extensions.  Functions with similar names exist on
98 some other systems, though typically with different calling signatures.
99 .SH 例
100 The program below uses \fBgetservbyport_r\fP()  to retrieve the service record
101 for the port and protocol named in its first command\-line argument.  If a
102 third (integer) command\-line argument is supplied, it is used as the initial
103 value for \fIbuflen\fP; if \fBgetservbyport_r\fP()  fails with the error
104 \fBERANGE\fP, the program retries with larger buffer sizes.  The following
105 shell session shows a couple of sample runs:
106 .in +4n
107 .nf
108
109 $\fB ./a.out 7 tcp 1\fP
110 ERANGE! Retrying with larger buffer
111 getservbyport_r() returned: 0 (success)  (buflen=87)
112 s_name=echo; s_proto=tcp; s_port=7; aliases=
113 $\fB ./a.out 77777 tcp\fP
114 getservbyport_r() returned: 0 (success)  (buflen=1024)
115 Call failed/record not found
116 .fi
117 .in
118 .SS プログラムのソース
119 \&
120 .nf
121 #define _GNU_SOURCE
122 #include <ctype.h>
123 #include <netdb.h>
124 #include <stdlib.h>
125 #include <stdio.h>
126 #include <errno.h>
127 #include <string.h>
128
129 #define MAX_BUF 10000
130
131 int
132 main(int argc, char *argv[])
133 {
134     int buflen, erange_cnt, port, s;
135     struct servent result_buf;
136     struct servent *result;
137     char buf[MAX_BUF];
138     char *protop;
139     char **p;
140
141     if (argc < 3) {
142         printf("Usage: %s port\-num proto\-name [buflen]\en", argv[0]);
143         exit(EXIT_FAILURE);
144     }
145
146     port = htons(atoi(argv[1]));
147     protop = (strcmp(argv[2], "null") == 0 ||
148               strcmp(argv[2], "NULL") == 0) ?  NULL : argv[2];
149
150     buflen = 1024;
151     if (argc > 3)
152         buflen = atoi(argv[3]);
153
154     if (buflen > MAX_BUF) {
155         printf("Exceeded buffer limit (%d)\en", MAX_BUF);
156         exit(EXIT_FAILURE);
157     }
158
159     erange_cnt = 0;
160     do {
161         s = getservbyport_r(port, protop, &result_buf,
162                      buf, buflen, &result);
163         if (s == ERANGE) {
164             if (erange_cnt == 0)
165                 printf("ERANGE! Retrying with larger buffer\en");
166             erange_cnt++;
167
168             /* Increment a byte at a time so we can see exactly
169                what size buffer was required */
170
171             buflen++;
172
173             if (buflen > MAX_BUF) {
174                 printf("Exceeded buffer limit (%d)\en", MAX_BUF);
175                 exit(EXIT_FAILURE);
176             }
177         }
178     } while (s == ERANGE);
179
180     printf("getservbyport_r() returned: %s  (buflen=%d)\en",
181             (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" :
182             strerror(s), buflen);
183
184     if (s != 0 || result == NULL) {
185         printf("Call failed/record not found\en");
186         exit(EXIT_FAILURE);
187     }
188
189     printf("s_name=%s; s_proto=%s; s_port=%d; aliases=",
190                 result_buf.s_name, result_buf.s_proto,
191                 ntohs(result_buf.s_port));
192     for (p = result_buf.s_aliases; *p != NULL; p++)
193         printf("%s ", *p);
194     printf("\en");
195
196     exit(EXIT_SUCCESS);
197 }
198 .fi
199 .SH 関連項目
200 \fBgetservent\fP(3), \fBservices\fP(5)