OSDN Git Service

d8d120a4304b425d5aa278d94730824ce2cfd35b
[linuxjm/LDP_man-pages.git] / draft / man3 / getline.3
1 .\" Copyright (c) 2001 John Levon <moz@compsoc.man.ac.uk>
2 .\" Based in part on GNU libc documentation
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 .\" License.
24 .\"
25 .\" Japanese Version Copyright (c) 2001 Yuichi SATO
26 .\"         all rights reserved.
27 .\" Translated 2001-11-09, Yuichi SATO <ysato@h4.dion.ne.jp>
28 .\" Updated 2006-07-20, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.34
29 .\" Updated 2010-04-18, Akihiro MOTOKI, LDP v3.24
30 .\"
31 .\"WORD:    delimiter    区切り文字
32 .\"
33 .TH GETLINE 3  2010-06-12 "GNU" "Linux Programmer's Manual"
34 .\"O .SH NAME
35 .SH 名前
36 .\"O getline, getdelim \- delimited string input
37 getline, getdelim \- 区切り文字までの文字列入力を読み込む
38 .\"O .SH SYNOPSIS
39 .SH 書式
40 .nf
41 .B #include <stdio.h>
42 .sp
43 .BI "ssize_t getline(char **" lineptr ", size_t *" n ", FILE *" stream );
44
45 .BI "ssize_t getdelim(char **" lineptr ", size_t *" n ", int " delim \
46 ", FILE *" stream );
47 .fi
48 .sp
49 .in -4n
50 .\"O Feature Test Macro Requirements for glibc (see
51 .\"O .BR feature_test_macros (7)):
52 glibc 向けの機能検査マクロの要件
53 .RB ( feature_test_macros (7)
54 参照):
55 .in
56 .sp
57 .ad l
58 .BR getline (),
59 .BR getdelim ():
60 .PD 0
61 .RS 4
62 .TP 4
63 .\"O Since glibc 2.10:
64 glibc 2.10 以降:
65 _POSIX_C_SOURCE\ >=\ 200809L || _XOPEN_SOURCE\ >=\ 700
66 .TP
67 .\"O Before glibc 2.10:
68 glibc 2.10 より前:
69 _GNU_SOURCE
70 .RE
71 .PD
72 .ad
73 .\"O .SH DESCRIPTION
74 .SH 説明
75 .\"O .BR getline ()
76 .\"O reads an entire line from \fIstream\fP,
77 .\"O storing the address of the buffer containing the text into
78 .\"O .IR "*lineptr" .
79 .BR getline ()
80 は \fIstream\fP から 1 行全てを読み込み、テキストが含まれているバッファのアドレスを
81 .I "*lineptr"
82 に格納する。
83 .\"O The buffer is null-terminated and includes the newline character, if
84 .\"O one was found.
85 バッファはヌル文字 (\e0) で終端される。
86 改行文字が見つかった場合は、改行文字もバッファに格納される。
87
88 .\"O If
89 .\"O .I "*lineptr"
90 .\"O is NULL, then
91 .\"O .BR getline ()
92 .\"O will allocate a buffer for storing the line,
93 .\"O which should be freed by the user program.
94 .\"O (In this case, the value in
95 .\"O .I *n
96 .\"O is ignored.)
97 .I "*lineptr"
98 が NULL の場合、
99 .BR getline ()
100 は行の内容を格納するためのバッファを確保する。
101 このバッファはユーザーのプログラムで解放すべきである
102 (この場合、
103 .I *n
104 の値は無視される)。
105
106 .\"O Alternatively, before calling
107 .\"O .BR getline (),
108 .\"O .I "*lineptr"
109 .\"O can contain a pointer to a
110 .\"O .BR malloc (3)\-allocated
111 .\"O buffer
112 .\"O .I "*n"
113 .\"O bytes in size.
114 別の方法として、
115 .BR getline ()
116 を呼び出す際に、
117 .I "*lineptr"
118
119 .BR malloc (3)
120 で確保した大きさ
121 .I "*n"
122 バイトのバッファへのポインタを入れて渡すこともできる。
123 .\"O If the buffer is not large enough to hold the line,
124 .\"O .BR getline ()
125 .\"O resizes it with
126 .\"O .BR "realloc(3)" ,
127 .\"O updating
128 .\"O .I "*lineptr"
129 .\"O and
130 .\"O .I "*n"
131 .\"O as necessary.
132 読み込んだ行を保持するのに十分なバッファがない場合、
133 .BR getline ()
134
135 .BR realloc (3)
136 を使ってバッファのサイズを変更し、必要に応じて
137 .I "*lineptr"
138
139 .I "*n"
140 を更新する。
141
142 .\"O In either case, on a successful call,
143 .\"O .I "*lineptr"
144 .\"O and
145 .\"O .I "*n"
146 .\"O will be updated to reflect the buffer address and allocated size respectively.
147 どちらの場合でも、呼び出しに成功したときには、
148 .I "*lineptr"
149
150 .I "*n"
151 がバッファのアドレスと割り当てたサイズを反映した値に更新される。
152
153 .\"O .BR getdelim ()
154 .\"O works like
155 .\"O .BR getline (),
156 .\"O except that a line delimiter other than newline can be specified as the
157 .\"O .I delimiter
158 .\"O argument.
159 .BR getdelim ()
160
161 .BR getline ()
162 と同じように動作するが、改行文字以外の区切り文字を引き数
163 .I delim
164 に指定することができる。
165 .\"O As with
166 .\"O .BR getline (),
167 .\"O a delimiter character is not added if one was not present
168 .\"O in the input before end of file was reached.
169 .BR getline ()
170 と同様に、ファイル終端に達するまでに入力行に区切り文字が見付からない場合は、
171 区切り文字をバッファに追加しない。
172 .\"O .SH "RETURN VALUE"
173 .SH 返り値
174 .\"O On success,
175 .\"O .BR getline ()
176 .\"O and
177 .\"O .BR getdelim ()
178 .\"O return the number of characters read, including the delimiter character,
179 .\"O but not including the terminating null byte.
180 .\"O This value can be used
181 .\"O to handle embedded null bytes in the line read.
182 成功した場合、
183 .BR getline ()
184
185 .BR getdelim ()
186 は読み込んだ文字数を返す。
187 文字数には区切り文字は含まれるが、終端に使う NULL バイトは含まれない。
188 この値によって、読み込んだ行に含まれる NULL バイトを操作することができる。
189
190 .\"O Both functions return \-1 on failure to read a line (including end-of-file
191 .\"O condition).
192 どちらの関数も、行の読み込みに失敗した場合には \-1 を返す
193 (ファイルの終端に達した場合にも \-1 を返す)。
194 .\"O .SH ERRORS
195 .SH エラー
196 .TP
197 .B EINVAL
198 .\"O Bad arguments
199 .\"O .RI ( n
200 .\"O or
201 .\"O .I lineptr
202 .\"O is NULL, or
203 .\"O .I stream
204 .\"O is not valid).
205 引き数が不正である
206 .RI ( n
207 または
208 .I lineptr
209 が NULL である。
210 もしくは
211 .I stream
212 が有効でない)。
213 .\"O .SH VERSIONS
214 .SH バージョン
215 .\"O These functions are available since libc 4.6.27.
216 これらの関数は libc 4.6.27 以降で利用可能である。
217 .\"O .SH "CONFORMING TO"
218 .SH 準拠
219 .\"O Both
220 .\"O .BR getline ()
221 .\"O and
222 .\"O .BR getdelim ()
223 .\"O were originally GNU extensions.
224 .\"O They were standardized in POSIX.1-2008.
225 .BR getline ()
226
227 .BR getdelim ()
228 は、どちらも元は GNU による拡張であったが、
229 POSIX.1-2008 で標準化された。
230 .\"O .SH "EXAMPLE"
231 .SH 例
232 .nf
233 #define _GNU_SOURCE
234 #include <stdio.h>
235 #include <stdlib.h>
236
237 int
238 main(void)
239 {
240     FILE *fp;
241     char *line = NULL;
242     size_t len = 0;
243     ssize_t read;
244
245     fp = fopen("/etc/motd", "r");
246     if (fp == NULL)
247         exit(EXIT_FAILURE);
248
249     while ((read = getline(&line, &len, fp)) != \-1) {
250         printf("Retrieved line of length %zu :\en", read);
251         printf("%s", line);
252     }
253
254     free(line);
255     exit(EXIT_SUCCESS);
256 }
257 .fi
258 .\"O .SH "SEE ALSO"
259 .SH 関連項目
260 .BR read (2),
261 .BR fgets (3),
262 .BR fopen (3),
263 .BR fread (3),
264 .BR gets (3),
265 .BR scanf (3)