OSDN Git Service

(split) LDP: Release pages for LDP v3.39.
[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 .\"
26 .\" This file was generated with po4a. Translate the source file.
27 .\"
28 .\"*******************************************************************
29 .TH GETLINE 3 2010\-06\-12 GNU "Linux Programmer's Manual"
30 .SH 名前
31 getline, getdelim \- 区切り文字までの文字列入力を読み込む
32 .SH 書式
33 .nf
34 \fB#include <stdio.h>\fP
35 .sp
36 \fBssize_t getline(char **\fP\fIlineptr\fP\fB, size_t *\fP\fIn\fP\fB, FILE *\fP\fIstream\fP\fB);\fP
37
38 \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
39 .fi
40 .sp
41 .in -4n
42 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7)  参照):
43 .in
44 .sp
45 .ad l
46 \fBgetline\fP(), \fBgetdelim\fP():
47 .PD 0
48 .RS 4
49 .TP  4
50 glibc 2.10 以降:
51 _POSIX_C_SOURCE\ >=\ 200809L || _XOPEN_SOURCE\ >=\ 700
52 .TP 
53 glibc 2.10 より前:
54 _GNU_SOURCE
55 .RE
56 .PD
57 .ad
58 .SH 説明
59 \fBgetline\fP()  は \fIstream\fP から 1 行全てを読み込み、テキストが含まれているバッファのアドレスを \fI*lineptr\fP
60 に格納する。 バッファはヌル文字 (\e0) で終端される。 改行文字が見つかった場合は、改行文字もバッファに格納される。
61
62 \fI*lineptr\fP が NULL の場合、 \fBgetline\fP()  は行の内容を格納するためのバッファを確保する。
63 このバッファはユーザーのプログラムで解放すべきである (この場合、 \fI*n\fP の値は無視される)。
64
65 別の方法として、 \fBgetline\fP()  を呼び出す際に、 \fI*lineptr\fP に \fBmalloc\fP(3)  で確保した大きさ \fI*n\fP
66 バイトのバッファへのポインタを入れて渡すこともできる。 読み込んだ行を保持するのに十分なバッファがない場合、 \fBgetline\fP()  は
67 \fBrealloc\fP(3)  を使ってバッファのサイズを変更し、必要に応じて \fI*lineptr\fP と \fI*n\fP を更新する。
68
69 どちらの場合でも、呼び出しに成功したときには、 \fI*lineptr\fP と \fI*n\fP がバッファのアドレスと割り当てたサイズを反映した値に更新される。
70
71 \fBgetdelim\fP()  は \fBgetline\fP()  と同じように動作するが、改行文字以外の区切り文字を引き数 \fIdelim\fP
72 に指定することができる。 \fBgetline\fP()  と同様に、ファイル終端に達するまでに入力行に区切り文字が見付からない場合は、
73 区切り文字をバッファに追加しない。
74 .SH 返り値
75 成功した場合、 \fBgetline\fP()  と \fBgetdelim\fP()  は読み込んだ文字数を返す。 文字数には区切り文字は含まれるが、終端に使う
76 NULL バイトは含まれない。 この値によって、読み込んだ行に含まれる NULL バイトを操作することができる。
77
78 どちらの関数も、行の読み込みに失敗した場合には \-1 を返す (ファイルの終端に達した場合にも \-1 を返す)。
79 .SH エラー
80 .TP 
81 \fBEINVAL\fP
82 引き数が不正である (\fIn\fP または \fIlineptr\fP が NULL である。 もしくは \fIstream\fP が有効でない)。
83 .SH バージョン
84 これらの関数は libc 4.6.27 以降で利用可能である。
85 .SH 準拠
86 \fBgetline\fP()  と \fBgetdelim\fP()  は、どちらも元は GNU による拡張であったが、 POSIX.1\-2008
87 で標準化された。
88 .SH 例
89 .nf
90 #define _GNU_SOURCE
91 #include <stdio.h>
92 #include <stdlib.h>
93
94 int
95 main(void)
96 {
97     FILE *fp;
98     char *line = NULL;
99     size_t len = 0;
100     ssize_t read;
101
102     fp = fopen("/etc/motd", "r");
103     if (fp == NULL)
104         exit(EXIT_FAILURE);
105
106     while ((read = getline(&line, &len, fp)) != \-1) {
107         printf("Retrieved line of length %zu :\en", read);
108         printf("%s", line);
109     }
110
111     free(line);
112     exit(EXIT_SUCCESS);
113 }
114 .fi
115 .SH 関連項目
116 \fBread\fP(2), \fBfgets\fP(3), \fBfopen\fP(3), \fBfread\fP(3), \fBgets\fP(3), \fBscanf\fP(3)