OSDN Git Service

(split) LDP: Update draft pages
[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 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\"*******************************************************************
27 .\"
28 .\" This file was generated with po4a. Translate the source file.
29 .\"
30 .\"*******************************************************************
31 .TH GETLINE 3 2013\-04\-19 GNU "Linux Programmer's Manual"
32 .SH 名前
33 getline, getdelim \- 区切り文字までの文字列入力を読み込む
34 .SH 書式
35 .nf
36 \fB#include <stdio.h>\fP
37 .sp
38 \fBssize_t getline(char **\fP\fIlineptr\fP\fB, size_t *\fP\fIn\fP\fB, FILE *\fP\fIstream\fP\fB);\fP
39
40 \fBssize_t getdelim(char **\fP\fIlineptr\fP\fB, size_t *\fP\fIn\fP\fB, int \fP\fIdelim\fP\fB, FILE *\fP\fIstream\fP\fB);\fP
41 .fi
42 .sp
43 .in -4n
44 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7)  参照):
45 .in
46 .sp
47 .ad l
48 \fBgetline\fP(), \fBgetdelim\fP():
49 .PD 0
50 .RS 4
51 .TP  4
52 glibc 2.10 以降:
53 _POSIX_C_SOURCE\ >=\ 200809L || _XOPEN_SOURCE\ >=\ 700
54 .TP 
55 glibc 2.10 より前:
56 _GNU_SOURCE
57 .RE
58 .PD
59 .ad
60 .SH 説明
61 \fBgetline\fP()  は \fIstream\fP から 1 行全てを読み込み、テキストが含まれているバッファのアドレスを \fI*lineptr\fP
62 に格納する。 バッファはヌル文字 (\e0) で終端される。 改行文字が見つかった場合は、改行文字もバッファに格納される。
63
64 \fI*lineptr\fP が NULL の場合、 \fBgetline\fP()  は行の内容を格納するためのバッファを確保する。
65 このバッファはユーザーのプログラムで解放すべきである (この場合、 \fI*n\fP の値は無視される)。
66
67 別の方法として、 \fBgetline\fP()  を呼び出す際に、 \fI*lineptr\fP に \fBmalloc\fP(3)  で確保した大きさ \fI*n\fP
68 バイトのバッファへのポインタを入れて渡すこともできる。 読み込んだ行を保持するのに十分なバッファがない場合、 \fBgetline\fP()  は
69 \fBrealloc\fP(3)  を使ってバッファのサイズを変更し、必要に応じて \fI*lineptr\fP と \fI*n\fP を更新する。
70
71 どちらの場合でも、呼び出しに成功したときには、 \fI*lineptr\fP と \fI*n\fP がバッファのアドレスと割り当てたサイズを反映した値に更新される。
72
73 \fBgetdelim\fP()  は \fBgetline\fP()  と同じように動作するが、改行文字以外の区切り文字を引き数 \fIdelim\fP
74 に指定することができる。 \fBgetline\fP()  と同様に、ファイル終端に達するまでに入力行に区切り文字が見付からない場合は、
75 区切り文字をバッファに追加しない。
76 .SH 返り値
77 成功した場合、 \fBgetline\fP()  と \fBgetdelim\fP()  は読み込んだ文字数を返す。 文字数には区切り文字は含まれるが、終端に使う
78 NULL バイト (\(aq\e0\(aq) は含まれない。 この値によって、読み込んだ行に含まれる NULL バイトを操作することができる。
79
80 どちらの関数も、行の読み込みに失敗した場合には \-1 を返す (ファイルの終端に達した場合にも \-1 を返す)。 エラーが発生した場合には、
81 \fIerrno\fP にエラーの原因を示す値が設定される。
82 .SH エラー
83 .TP 
84 \fBEINVAL\fP
85 引き数が不正である (\fIn\fP または \fIlineptr\fP が NULL である。 もしくは \fIstream\fP が有効でない)。
86 .SH バージョン
87 これらの関数は libc 4.6.27 以降で利用可能である。
88 .SH 準拠
89 \fBgetline\fP()  と \fBgetdelim\fP()  は、どちらも元は GNU による拡張であったが、 POSIX.1\-2008
90 で標準化された。
91 .SH 例
92 .nf
93 #define _GNU_SOURCE
94 #include <stdio.h>
95 #include <stdlib.h>
96
97 int
98 main(void)
99 {
100     FILE *fp;
101     char *line = NULL;
102     size_t len = 0;
103     ssize_t read;
104
105     fp = fopen("/etc/motd", "r");
106     if (fp == NULL)
107         exit(EXIT_FAILURE);
108
109     while ((read = getline(&line, &len, fp)) != \-1) {
110         printf("Retrieved line of length %zu :\en", read);
111         printf("%s", line);
112     }
113
114     free(line);
115     exit(EXIT_SUCCESS);
116 }
117 .fi
118 .SH 関連項目
119 \fBread\fP(2), \fBfgets\fP(3), \fBfopen\fP(3), \fBfread\fP(3), \fBgets\fP(3), \fBscanf\fP(3)
120 .SH この文書について
121 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.52 の一部
122 である。プロジェクトの説明とバグ報告に関する情報は
123 http://www.kernel.org/doc/man\-pages/ に書かれている。