OSDN Git Service

(split) LDP: Restore and add Copyrights for 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 .\"
32 .\" Japanese Version Copyright (c) 2001 Yuichi SATO
33 .\"         all rights reserved.
34 .\" Translated 2001-11-09, Yuichi SATO <ysato@h4.dion.ne.jp>
35 .\" Updated 2006-07-20, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.34
36 .\" Updated 2010-04-18, Akihiro MOTOKI, LDP v3.24
37 .\" Updated 2013-07-22, Akihiro MOTOKI <amotoki@gmail.com>
38 .\"
39 .TH GETLINE 3 2013\-04\-19 GNU "Linux Programmer's Manual"
40 .SH 名前
41 getline, getdelim \- 区切り文字までの文字列入力を読み込む
42 .SH 書式
43 .nf
44 \fB#include <stdio.h>\fP
45 .sp
46 \fBssize_t getline(char **\fP\fIlineptr\fP\fB, size_t *\fP\fIn\fP\fB, FILE *\fP\fIstream\fP\fB);\fP
47
48 \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
49 .fi
50 .sp
51 .in -4n
52 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7)  参照):
53 .in
54 .sp
55 .ad l
56 \fBgetline\fP(), \fBgetdelim\fP():
57 .PD 0
58 .RS 4
59 .TP  4
60 glibc 2.10 以降:
61 _POSIX_C_SOURCE\ >=\ 200809L || _XOPEN_SOURCE\ >=\ 700
62 .TP 
63 glibc 2.10 より前:
64 _GNU_SOURCE
65 .RE
66 .PD
67 .ad
68 .SH 説明
69 \fBgetline\fP()  は \fIstream\fP から 1 行全てを読み込み、テキストが含まれているバッファのアドレスを \fI*lineptr\fP
70 に格納する。 バッファはヌル文字 (\e0) で終端される。 改行文字が見つかった場合は、改行文字もバッファに格納される。
71
72 \fI*lineptr\fP が NULL の場合、 \fBgetline\fP()  は行の内容を格納するためのバッファを確保する。
73 このバッファはユーザーのプログラムで解放すべきである (この場合、 \fI*n\fP の値は無視される)。
74
75 別の方法として、 \fBgetline\fP()  を呼び出す際に、 \fI*lineptr\fP に \fBmalloc\fP(3)  で確保した大きさ \fI*n\fP
76 バイトのバッファへのポインタを入れて渡すこともできる。 読み込んだ行を保持するのに十分なバッファがない場合、 \fBgetline\fP()  は
77 \fBrealloc\fP(3)  を使ってバッファのサイズを変更し、必要に応じて \fI*lineptr\fP と \fI*n\fP を更新する。
78
79 どちらの場合でも、呼び出しに成功したときには、 \fI*lineptr\fP と \fI*n\fP がバッファのアドレスと割り当てたサイズを反映した値に更新される。
80
81 \fBgetdelim\fP()  は \fBgetline\fP()  と同じように動作するが、改行文字以外の区切り文字を引き数 \fIdelim\fP
82 に指定することができる。 \fBgetline\fP()  と同様に、ファイル終端に達するまでに入力行に区切り文字が見付からない場合は、
83 区切り文字をバッファに追加しない。
84 .SH 返り値
85 成功した場合、 \fBgetline\fP()  と \fBgetdelim\fP()  は読み込んだ文字数を返す。 文字数には区切り文字は含まれるが、終端に使う
86 NULL バイト (\(aq\e0\(aq) は含まれない。 この値によって、読み込んだ行に含まれる NULL バイトを操作することができる。
87
88 どちらの関数も、行の読み込みに失敗した場合には \-1 を返す (ファイルの終端に達した場合にも \-1 を返す)。 エラーが発生した場合には、
89 \fIerrno\fP にエラーの原因を示す値が設定される。
90 .SH エラー
91 .TP 
92 \fBEINVAL\fP
93 引き数が不正である (\fIn\fP または \fIlineptr\fP が NULL である。 もしくは \fIstream\fP が有効でない)。
94 .SH バージョン
95 これらの関数は libc 4.6.27 以降で利用可能である。
96 .SH 準拠
97 \fBgetline\fP()  と \fBgetdelim\fP()  は、どちらも元は GNU による拡張であったが、 POSIX.1\-2008
98 で標準化された。
99 .SH 例
100 .nf
101 #define _GNU_SOURCE
102 #include <stdio.h>
103 #include <stdlib.h>
104
105 int
106 main(void)
107 {
108     FILE *fp;
109     char *line = NULL;
110     size_t len = 0;
111     ssize_t read;
112
113     fp = fopen("/etc/motd", "r");
114     if (fp == NULL)
115         exit(EXIT_FAILURE);
116
117     while ((read = getline(&line, &len, fp)) != \-1) {
118         printf("Retrieved line of length %zu :\en", read);
119         printf("%s", line);
120     }
121
122     free(line);
123     exit(EXIT_SUCCESS);
124 }
125 .fi
126 .SH 関連項目
127 \fBread\fP(2), \fBfgets\fP(3), \fBfopen\fP(3), \fBfread\fP(3), \fBgets\fP(3), \fBscanf\fP(3)
128 .SH この文書について
129 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.53 の一部
130 である。プロジェクトの説明とバグ報告に関する情報は
131 http://www.kernel.org/doc/man\-pages/ に書かれている。