OSDN Git Service

48cd7d8a08bb6bc94284af1a6bceec1fd7f210ca
[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 .\" %%%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 2010\-06\-12 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 バイトは含まれない。 この値によって、読み込んだ行に含まれる NULL バイトを操作することができる。
79
80 どちらの関数も、行の読み込みに失敗した場合には \-1 を返す (ファイルの終端に達した場合にも \-1 を返す)。
81 .SH エラー
82 .TP 
83 \fBEINVAL\fP
84 引き数が不正である (\fIn\fP または \fIlineptr\fP が NULL である。 もしくは \fIstream\fP が有効でない)。
85 .SH バージョン
86 これらの関数は libc 4.6.27 以降で利用可能である。
87 .SH 準拠
88 \fBgetline\fP()  と \fBgetdelim\fP()  は、どちらも元は GNU による拡張であったが、 POSIX.1\-2008
89 で標準化された。
90 .SH 例
91 .nf
92 #define _GNU_SOURCE
93 #include <stdio.h>
94 #include <stdlib.h>
95
96 int
97 main(void)
98 {
99     FILE *fp;
100     char *line = NULL;
101     size_t len = 0;
102     ssize_t read;
103
104     fp = fopen("/etc/motd", "r");
105     if (fp == NULL)
106         exit(EXIT_FAILURE);
107
108     while ((read = getline(&line, &len, fp)) != \-1) {
109         printf("Retrieved line of length %zu :\en", read);
110         printf("%s", line);
111     }
112
113     free(line);
114     exit(EXIT_SUCCESS);
115 }
116 .fi
117 .SH 関連項目
118 \fBread\fP(2), \fBfgets\fP(3), \fBfopen\fP(3), \fBfread\fP(3), \fBgets\fP(3), \fBscanf\fP(3)
119 .SH この文書について
120 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.51 の一部
121 である。プロジェクトの説明とバグ報告に関する情報は
122 http://www.kernel.org/doc/man\-pages/ に書かれている。