1 .\" Copyright (c) 2001 John Levon <moz@compsoc.man.ac.uk>
2 .\" Based in part on GNU libc documentation
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.
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.
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
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
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
31 .\"WORD: delimiter ¶èÀÚ¤êʸ»ú
33 .TH GETLINE 3 2010-06-12 "GNU" "Linux Programmer's Manual"
36 .\"O getline, getdelim \- delimited string input
37 getline, getdelim \- ¶èÀÚ¤êʸ»ú¤Þ¤Ç¤Îʸ»úÎóÆþÎϤòÆɤ߹þ¤à
43 .BI "ssize_t getline(char **" lineptr ", size_t *" n ", FILE *" stream );
45 .BI "ssize_t getdelim(char **" lineptr ", size_t *" n ", int " delim \
50 .\"O Feature Test Macro Requirements for glibc (see
51 .\"O .BR feature_test_macros (7)):
52 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
53 .RB ( feature_test_macros (7)
63 .\"O Since glibc 2.10:
65 _POSIX_C_SOURCE\ >=\ 200809L || _XOPEN_SOURCE\ >=\ 700
67 .\"O Before glibc 2.10:
76 .\"O reads an entire line from \fIstream\fP,
77 .\"O storing the address of the buffer containing the text into
80 ¤Ï \fIstream\fP ¤«¤é 1 ¹ÔÁ´¤Æ¤òÆɤ߹þ¤ß¡¢¥Æ¥¥¹¥È¤¬´Þ¤Þ¤ì¤Æ¤¤¤ë¥Ð¥Ã¥Õ¥¡¤Î¥¢¥É¥ì¥¹¤ò
83 .\"O The buffer is null-terminated and includes the newline character, if
85 ¥Ð¥Ã¥Õ¥¡¤Ï¥Ì¥ëʸ»ú (\e0) ¤Ç½ªÃ¼¤µ¤ì¤ë¡£
86 ²þ¹Ôʸ»ú¤¬¸«¤Ä¤«¤Ã¤¿¾ì¹ç¤Ï¡¢²þ¹Ôʸ»ú¤â¥Ð¥Ã¥Õ¥¡¤Ë³ÊǼ¤µ¤ì¤ë¡£
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
100 ¤Ï¹Ô¤ÎÆâÍƤò³ÊǼ¤¹¤ë¤¿¤á¤Î¥Ð¥Ã¥Õ¥¡¤ò³ÎÊݤ¹¤ë¡£
101 ¤³¤Î¥Ð¥Ã¥Õ¥¡¤Ï¥æ¡¼¥¶¡¼¤Î¥×¥í¥°¥é¥à¤Ç²òÊü¤¹¤Ù¤¤Ç¤¢¤ë
106 .\"O Alternatively, before calling
109 .\"O can contain a pointer to a
110 .\"O .BR malloc (3)\-allocated
122 ¥Ð¥¤¥È¤Î¥Ð¥Ã¥Õ¥¡¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÆþ¤ì¤ÆÅϤ¹¤³¤È¤â¤Ç¤¤ë¡£
123 .\"O If the buffer is not large enough to hold the line,
126 .\"O .BR "realloc(3)" ,
132 Æɤ߹þ¤ó¤À¹Ô¤òÊÝ»ý¤¹¤ë¤Î¤Ë½½Ê¬¤Ê¥Ð¥Ã¥Õ¥¡¤¬¤Ê¤¤¾ì¹ç¡¢
136 ¤ò»È¤Ã¤Æ¥Ð¥Ã¥Õ¥¡¤Î¥µ¥¤¥º¤òÊѹ¹¤·¡¢É¬Íפ˱þ¤¸¤Æ
142 .\"O In either case, on a successful call,
146 .\"O will be updated to reflect the buffer address and allocated size respectively.
147 ¤É¤Á¤é¤Î¾ì¹ç¤Ç¤â¡¢¸Æ¤Ó½Ð¤·¤ËÀ®¸ù¤·¤¿¤È¤¤Ë¤Ï¡¢
151 ¤¬¥Ð¥Ã¥Õ¥¡¤Î¥¢¥É¥ì¥¹¤È³ä¤êÅö¤Æ¤¿¥µ¥¤¥º¤òÈ¿±Ç¤·¤¿Ãͤ˹¹¿·¤µ¤ì¤ë¡£
156 .\"O except that a line delimiter other than newline can be specified as the
162 ¤ÈƱ¤¸¤è¤¦¤ËÆ°ºî¤¹¤ë¤¬¡¢²þ¹Ôʸ»ú°Ê³°¤Î¶èÀÚ¤êʸ»ú¤ò°ú¤¿ô
164 ¤Ë»ØÄꤹ¤ë¤³¤È¤¬¤Ç¤¤ë¡£
167 .\"O a delimiter character is not added if one was not present
168 .\"O in the input before end of file was reached.
170 ¤ÈƱÍͤˡ¢¥Õ¥¡¥¤¥ë½ªÃ¼¤Ë㤹¤ë¤Þ¤Ç¤ËÆþÎϹԤ˶èÀÚ¤êʸ»ú¤¬¸«ÉÕ¤«¤é¤Ê¤¤¾ì¹ç¤Ï¡¢
171 ¶èÀÚ¤êʸ»ú¤ò¥Ð¥Ã¥Õ¥¡¤ËÄɲ䷤ʤ¤¡£
172 .\"O .SH "RETURN VALUE"
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.
186 ¤ÏÆɤ߹þ¤ó¤Àʸ»ú¿ô¤òÊÖ¤¹¡£
187 ʸ»ú¿ô¤Ë¤Ï¶èÀÚ¤êʸ»ú¤Ï´Þ¤Þ¤ì¤ë¤¬¡¢½ªÃ¼¤Ë»È¤¦ NULL ¥Ð¥¤¥È¤Ï´Þ¤Þ¤ì¤Ê¤¤¡£
188 ¤³¤ÎÃͤˤè¤Ã¤Æ¡¢Æɤ߹þ¤ó¤À¹Ô¤Ë´Þ¤Þ¤ì¤ë NULL ¥Ð¥¤¥È¤òÁàºî¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£
190 .\"O Both functions return \-1 on failure to read a line (including end-of-file
192 ¤É¤Á¤é¤Î´Ø¿ô¤â¡¢¹Ô¤ÎÆɤ߹þ¤ß¤Ë¼ºÇÔ¤·¤¿¾ì¹ç¤Ë¤Ï \-1 ¤òÊÖ¤¹
193 (¥Õ¥¡¥¤¥ë¤Î½ªÃ¼¤Ë㤷¤¿¾ì¹ç¤Ë¤â \-1 ¤òÊÖ¤¹)¡£
215 .\"O These functions are available since libc 4.6.27.
216 ¤³¤ì¤é¤Î´Ø¿ô¤Ï libc 4.6.27 °Ê¹ß¤ÇÍøÍѲÄǽ¤Ç¤¢¤ë¡£
217 .\"O .SH "CONFORMING TO"
223 .\"O were originally GNU extensions.
224 .\"O They were standardized in POSIX.1-2008.
228 ¤Ï¡¢¤É¤Á¤é¤â¸µ¤Ï GNU ¤Ë¤è¤ë³ÈÄ¥¤Ç¤¢¤Ã¤¿¤¬¡¢
229 POSIX.1-2008 ¤Çɸ½à²½¤µ¤ì¤¿¡£
245 fp = fopen("/etc/motd", "r");
249 while ((read = getline(&line, &len, fp)) != \-1) {
250 printf("Retrieved line of length %zu :\en", read);