OSDN Git Service

(split) Convert release and draft pages to UTF-8.
[linuxjm/LDP_man-pages.git] / release / 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 .SH 名前
35 getline, getdelim \- 区切り文字までの文字列入力を読み込む
36 .SH 書式
37 .nf
38 .B #include <stdio.h>
39 .sp
40 .BI "ssize_t getline(char **" lineptr ", size_t *" n ", FILE *" stream );
41
42 .BI "ssize_t getdelim(char **" lineptr ", size_t *" n ", int " delim \
43 ", FILE *" stream );
44 .fi
45 .sp
46 .in -4n
47 glibc 向けの機能検査マクロの要件
48 .RB ( feature_test_macros (7)
49 参照):
50 .in
51 .sp
52 .ad l
53 .BR getline (),
54 .BR getdelim ():
55 .PD 0
56 .RS 4
57 .TP 4
58 glibc 2.10 以降:
59 _POSIX_C_SOURCE\ >=\ 200809L || _XOPEN_SOURCE\ >=\ 700
60 .TP
61 glibc 2.10 より前:
62 _GNU_SOURCE
63 .RE
64 .PD
65 .ad
66 .SH 説明
67 .BR getline ()
68 は \fIstream\fP から 1 行全てを読み込み、テキストが含まれているバッファのアドレスを
69 .I "*lineptr"
70 に格納する。
71 バッファはヌル文字 (\e0) で終端される。
72 改行文字が見つかった場合は、改行文字もバッファに格納される。
73
74 .I "*lineptr"
75 が NULL の場合、
76 .BR getline ()
77 は行の内容を格納するためのバッファを確保する。
78 このバッファはユーザーのプログラムで解放すべきである
79 (この場合、
80 .I *n
81 の値は無視される)。
82
83 別の方法として、
84 .BR getline ()
85 を呼び出す際に、
86 .I "*lineptr"
87
88 .BR malloc (3)
89 で確保した大きさ
90 .I "*n"
91 バイトのバッファへのポインタを入れて渡すこともできる。
92 読み込んだ行を保持するのに十分なバッファがない場合、
93 .BR getline ()
94
95 .BR realloc (3)
96 を使ってバッファのサイズを変更し、必要に応じて
97 .I "*lineptr"
98
99 .I "*n"
100 を更新する。
101
102 どちらの場合でも、呼び出しに成功したときには、
103 .I "*lineptr"
104
105 .I "*n"
106 がバッファのアドレスと割り当てたサイズを反映した値に更新される。
107
108 .BR getdelim ()
109
110 .BR getline ()
111 と同じように動作するが、改行文字以外の区切り文字を引き数
112 .I delim
113 に指定することができる。
114 .BR getline ()
115 と同様に、ファイル終端に達するまでに入力行に区切り文字が見付からない場合は、
116 区切り文字をバッファに追加しない。
117 .SH 返り値
118 成功した場合、
119 .BR getline ()
120
121 .BR getdelim ()
122 は読み込んだ文字数を返す。
123 文字数には区切り文字は含まれるが、終端に使う NULL バイトは含まれない。
124 この値によって、読み込んだ行に含まれる NULL バイトを操作することができる。
125
126 どちらの関数も、行の読み込みに失敗した場合には \-1 を返す
127 (ファイルの終端に達した場合にも \-1 を返す)。
128 .SH エラー
129 .TP
130 .B EINVAL
131 引き数が不正である
132 .RI ( n
133 または
134 .I lineptr
135 が NULL である。
136 もしくは
137 .I stream
138 が有効でない)。
139 .SH バージョン
140 これらの関数は libc 4.6.27 以降で利用可能である。
141 .SH 準拠
142 .BR getline ()
143
144 .BR getdelim ()
145 は、どちらも元は GNU による拡張であったが、
146 POSIX.1-2008 で標準化された。
147 .SH 例
148 .nf
149 #define _GNU_SOURCE
150 #include <stdio.h>
151 #include <stdlib.h>
152
153 int
154 main(void)
155 {
156     FILE *fp;
157     char *line = NULL;
158     size_t len = 0;
159     ssize_t read;
160
161     fp = fopen("/etc/motd", "r");
162     if (fp == NULL)
163         exit(EXIT_FAILURE);
164
165     while ((read = getline(&line, &len, fp)) != \-1) {
166         printf("Retrieved line of length %zu :\en", read);
167         printf("%s", line);
168     }
169
170     free(line);
171     exit(EXIT_SUCCESS);
172 }
173 .fi
174 .SH 関連項目
175 .BR read (2),
176 .BR fgets (3),
177 .BR fopen (3),
178 .BR fread (3),
179 .BR gets (3),
180 .BR scanf (3)