OSDN Git Service

(split) Apply minor changes from v3.32 to v3.35 in the upstream.
[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 .\" 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 .\"O .SH NAME
35 .SH Ì¾Á°
36 .\"O getline, getdelim \- delimited string input
37 getline, getdelim \- ¶èÀÚ¤êʸ»ú¤Þ¤Ç¤Îʸ»úÎóÆþÎϤòÆɤ߹þ¤à
38 .\"O .SH SYNOPSIS
39 .SH ½ñ¼°
40 .nf
41 .B #include <stdio.h>
42 .sp
43 .BI "ssize_t getline(char **" lineptr ", size_t *" n ", FILE *" stream );
44
45 .BI "ssize_t getdelim(char **" lineptr ", size_t *" n ", int " delim \
46 ", FILE *" stream );
47 .fi
48 .sp
49 .in -4n
50 .\"O Feature Test Macro Requirements for glibc (see
51 .\"O .BR feature_test_macros (7)):
52 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
53 .RB ( feature_test_macros (7)
54 »²¾È):
55 .in
56 .sp
57 .ad l
58 .BR getline (),
59 .BR getdelim ():
60 .PD 0
61 .RS 4
62 .TP 4
63 .\"O Since glibc 2.10:
64 glibc 2.10 °Ê¹ß:
65 _POSIX_C_SOURCE\ >=\ 200809L || _XOPEN_SOURCE\ >=\ 700
66 .TP
67 .\"O Before glibc 2.10:
68 glibc 2.10 ¤è¤êÁ°:
69 _GNU_SOURCE
70 .RE
71 .PD
72 .ad
73 .\"O .SH DESCRIPTION
74 .SH ÀâÌÀ
75 .\"O .BR getline ()
76 .\"O reads an entire line from \fIstream\fP,
77 .\"O storing the address of the buffer containing the text into
78 .\"O .IR "*lineptr" .
79 .BR getline ()
80 ¤Ï \fIstream\fP ¤«¤é 1 ¹ÔÁ´¤Æ¤òÆɤ߹þ¤ß¡¢¥Æ¥­¥¹¥È¤¬´Þ¤Þ¤ì¤Æ¤¤¤ë¥Ð¥Ã¥Õ¥¡¤Î¥¢¥É¥ì¥¹¤ò
81 .I "*lineptr"
82 ¤Ë³ÊǼ¤¹¤ë¡£
83 .\"O The buffer is null-terminated and includes the newline character, if
84 .\"O one was found.
85 ¥Ð¥Ã¥Õ¥¡¤Ï¥Ì¥ëʸ»ú (\e0) ¤Ç½ªÃ¼¤µ¤ì¤ë¡£
86 ²þ¹Ôʸ»ú¤¬¸«¤Ä¤«¤Ã¤¿¾ì¹ç¤Ï¡¢²þ¹Ôʸ»ú¤â¥Ð¥Ã¥Õ¥¡¤Ë³ÊǼ¤µ¤ì¤ë¡£
87
88 .\"O If
89 .\"O .I "*lineptr"
90 .\"O is NULL, then
91 .\"O .BR getline ()
92 .\"O will allocate a buffer for storing the line,
93 .\"O which should be freed by the user program.
94 .\"O (In this case, the value in
95 .\"O .I *n
96 .\"O is ignored.)
97 .I "*lineptr"
98 ¤¬ NULL ¤Î¾ì¹ç¡¢
99 .BR getline ()
100 ¤Ï¹Ô¤ÎÆâÍƤò³ÊǼ¤¹¤ë¤¿¤á¤Î¥Ð¥Ã¥Õ¥¡¤ò³ÎÊݤ¹¤ë¡£
101 ¤³¤Î¥Ð¥Ã¥Õ¥¡¤Ï¥æ¡¼¥¶¡¼¤Î¥×¥í¥°¥é¥à¤Ç²òÊü¤¹¤Ù¤­¤Ç¤¢¤ë
102 (¤³¤Î¾ì¹ç¡¢
103 .I *n
104 ¤ÎÃͤÏ̵»ë¤µ¤ì¤ë)¡£
105
106 .\"O Alternatively, before calling
107 .\"O .BR getline (),
108 .\"O .I "*lineptr"
109 .\"O can contain a pointer to a
110 .\"O .BR malloc (3)\-allocated
111 .\"O buffer
112 .\"O .I "*n"
113 .\"O bytes in size.
114 Ê̤ÎÊýË¡¤È¤·¤Æ¡¢
115 .BR getline ()
116 ¤ò¸Æ¤Ó½Ð¤¹ºÝ¤Ë¡¢
117 .I "*lineptr"
118 ¤Ë
119 .BR malloc (3)
120 ¤Ç³ÎÊݤ·¤¿Â礭¤µ
121 .I "*n"
122 ¥Ð¥¤¥È¤Î¥Ð¥Ã¥Õ¥¡¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÆþ¤ì¤ÆÅϤ¹¤³¤È¤â¤Ç¤­¤ë¡£
123 .\"O If the buffer is not large enough to hold the line,
124 .\"O .BR getline ()
125 .\"O resizes it with
126 .\"O .BR "realloc(3)" ,
127 .\"O updating
128 .\"O .I "*lineptr"
129 .\"O and
130 .\"O .I "*n"
131 .\"O as necessary.
132 Æɤ߹þ¤ó¤À¹Ô¤òÊÝ»ý¤¹¤ë¤Î¤Ë½½Ê¬¤Ê¥Ð¥Ã¥Õ¥¡¤¬¤Ê¤¤¾ì¹ç¡¢
133 .BR getline ()
134 ¤Ï
135 .BR realloc (3)
136 ¤ò»È¤Ã¤Æ¥Ð¥Ã¥Õ¥¡¤Î¥µ¥¤¥º¤òÊѹ¹¤·¡¢É¬Íפ˱þ¤¸¤Æ
137 .I "*lineptr"
138 ¤È
139 .I "*n"
140 ¤ò¹¹¿·¤¹¤ë¡£
141
142 .\"O In either case, on a successful call,
143 .\"O .I "*lineptr"
144 .\"O and
145 .\"O .I "*n"
146 .\"O will be updated to reflect the buffer address and allocated size respectively.
147 ¤É¤Á¤é¤Î¾ì¹ç¤Ç¤â¡¢¸Æ¤Ó½Ð¤·¤ËÀ®¸ù¤·¤¿¤È¤­¤Ë¤Ï¡¢
148 .I "*lineptr"
149 ¤È
150 .I "*n"
151 ¤¬¥Ð¥Ã¥Õ¥¡¤Î¥¢¥É¥ì¥¹¤È³ä¤êÅö¤Æ¤¿¥µ¥¤¥º¤òÈ¿±Ç¤·¤¿Ãͤ˹¹¿·¤µ¤ì¤ë¡£
152
153 .\"O .BR getdelim ()
154 .\"O works like
155 .\"O .BR getline (),
156 .\"O except that a line delimiter other than newline can be specified as the
157 .\"O .I delimiter
158 .\"O argument.
159 .BR getdelim ()
160 ¤Ï
161 .BR getline ()
162 ¤ÈƱ¤¸¤è¤¦¤ËÆ°ºî¤¹¤ë¤¬¡¢²þ¹Ôʸ»ú°Ê³°¤Î¶èÀÚ¤êʸ»ú¤ò°ú¤­¿ô
163 .I delim
164 ¤Ë»ØÄꤹ¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
165 .\"O As with
166 .\"O .BR getline (),
167 .\"O a delimiter character is not added if one was not present
168 .\"O in the input before end of file was reached.
169 .BR getline ()
170 ¤ÈƱÍͤˡ¢¥Õ¥¡¥¤¥ë½ªÃ¼¤Ë㤹¤ë¤Þ¤Ç¤ËÆþÎϹԤ˶èÀÚ¤êʸ»ú¤¬¸«ÉÕ¤«¤é¤Ê¤¤¾ì¹ç¤Ï¡¢
171 ¶èÀÚ¤êʸ»ú¤ò¥Ð¥Ã¥Õ¥¡¤ËÄɲ䷤ʤ¤¡£
172 .\"O .SH "RETURN VALUE"
173 .SH ÊÖ¤êÃÍ
174 .\"O On success,
175 .\"O .BR getline ()
176 .\"O and
177 .\"O .BR getdelim ()
178 .\"O return the number of characters read, including the delimiter character,
179 .\"O but not including the terminating null byte.
180 .\"O This value can be used
181 .\"O to handle embedded null bytes in the line read.
182 À®¸ù¤·¤¿¾ì¹ç¡¢
183 .BR getline ()
184 ¤È
185 .BR getdelim ()
186 ¤ÏÆɤ߹þ¤ó¤Àʸ»ú¿ô¤òÊÖ¤¹¡£
187 ʸ»ú¿ô¤Ë¤Ï¶èÀÚ¤êʸ»ú¤Ï´Þ¤Þ¤ì¤ë¤¬¡¢½ªÃ¼¤Ë»È¤¦ NULL ¥Ð¥¤¥È¤Ï´Þ¤Þ¤ì¤Ê¤¤¡£
188 ¤³¤ÎÃͤˤè¤Ã¤Æ¡¢Æɤ߹þ¤ó¤À¹Ô¤Ë´Þ¤Þ¤ì¤ë NULL ¥Ð¥¤¥È¤òÁàºî¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
189
190 .\"O Both functions return \-1 on failure to read a line (including end-of-file
191 .\"O condition).
192 ¤É¤Á¤é¤Î´Ø¿ô¤â¡¢¹Ô¤ÎÆɤ߹þ¤ß¤Ë¼ºÇÔ¤·¤¿¾ì¹ç¤Ë¤Ï \-1 ¤òÊÖ¤¹
193 (¥Õ¥¡¥¤¥ë¤Î½ªÃ¼¤Ë㤷¤¿¾ì¹ç¤Ë¤â \-1 ¤òÊÖ¤¹)¡£
194 .\"O .SH ERRORS
195 .SH ¥¨¥é¡¼
196 .TP
197 .B EINVAL
198 .\"O Bad arguments
199 .\"O .RI ( n
200 .\"O or
201 .\"O .I lineptr
202 .\"O is NULL, or
203 .\"O .I stream
204 .\"O is not valid).
205 °ú¤­¿ô¤¬ÉÔÀµ¤Ç¤¢¤ë
206 .RI ( n
207 ¤Þ¤¿¤Ï
208 .I lineptr
209 ¤¬ NULL ¤Ç¤¢¤ë¡£
210 ¤â¤·¤¯¤Ï
211 .I stream
212 ¤¬Í­¸ú¤Ç¤Ê¤¤)¡£
213 .\"O .SH VERSIONS
214 .SH ¥Ð¡¼¥¸¥ç¥ó
215 .\"O These functions are available since libc 4.6.27.
216 ¤³¤ì¤é¤Î´Ø¿ô¤Ï libc 4.6.27 °Ê¹ß¤ÇÍøÍѲÄǽ¤Ç¤¢¤ë¡£
217 .\"O .SH "CONFORMING TO"
218 .SH ½àµò
219 .\"O Both
220 .\"O .BR getline ()
221 .\"O and
222 .\"O .BR getdelim ()
223 .\"O were originally GNU extensions.
224 .\"O They were standardized in POSIX.1-2008.
225 .BR getline ()
226 ¤È
227 .BR getdelim ()
228 ¤Ï¡¢¤É¤Á¤é¤â¸µ¤Ï GNU ¤Ë¤è¤ë³ÈÄ¥¤Ç¤¢¤Ã¤¿¤¬¡¢
229 POSIX.1-2008 ¤Çɸ½à²½¤µ¤ì¤¿¡£
230 .\"O .SH "EXAMPLE"
231 .SH Îã
232 .nf
233 #define _GNU_SOURCE
234 #include <stdio.h>
235 #include <stdlib.h>
236
237 int
238 main(void)
239 {
240     FILE *fp;
241     char *line = NULL;
242     size_t len = 0;
243     ssize_t read;
244
245     fp = fopen("/etc/motd", "r");
246     if (fp == NULL)
247         exit(EXIT_FAILURE);
248
249     while ((read = getline(&line, &len, fp)) != \-1) {
250         printf("Retrieved line of length %zu :\en", read);
251         printf("%s", line);
252     }
253
254     free(line);
255     exit(EXIT_SUCCESS);
256 }
257 .fi
258 .\"O .SH "SEE ALSO"
259 .SH ´ØÏ¢¹àÌÜ
260 .BR read (2),
261 .BR fgets (3),
262 .BR fopen (3),
263 .BR fread (3),
264 .BR gets (3),
265 .BR scanf (3)