OSDN Git Service

Import translated manuals from JM CVS Repository.
[linuxjm/jm.git] / manual / LDP_man-pages / original / 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 .TH GETLINE 3  2009-12-05 "GNU" "Linux Programmer's Manual"
25 .SH NAME
26 getline, getdelim \- delimited string input
27 .SH SYNOPSIS
28 .nf
29 .B #include <stdio.h>
30 .sp
31 .BI "ssize_t getline(char **" lineptr ", size_t *" n ", FILE *" stream );
32
33 .BI "ssize_t getdelim(char **" lineptr ", size_t *" n ", int " delim \
34 ", FILE *" stream );
35 .fi
36 .sp
37 .in -4n
38 Feature Test Macro Requirements for glibc (see
39 .BR feature_test_macros (7)):
40 .in
41
42 Before glibc 2.10:
43 .br
44 .BR getline (),
45 .BR getdelim ():
46 _GNU_SOURCE
47
48 Since glibc 2.10:
49 .br
50 .BR getline (),
51 .BR getdelim ():
52 _POSIX_C_SOURCE >= 200809 || _XOPEN_SOURCE >= 700
53 .SH DESCRIPTION
54 .BR getline ()
55 reads an entire line from \fIstream\fP,
56 storing the address of the buffer containing the text into
57 .IR "*lineptr" .
58 The buffer is null-terminated and includes the newline character, if
59 one was found.
60
61 If
62 .I "*lineptr"
63 is NULL, then
64 .BR getline ()
65 will allocate a buffer for storing the line,
66 which should be freed by the user program.
67 (The value in
68 .I *n
69 is ignored.)
70
71 Alternatively, before calling
72 .BR getline (),
73 .I "*lineptr"
74 can contain a pointer to a
75 .BR malloc (3)\-allocated
76 buffer
77 .I "*n"
78 bytes in size.
79 If the buffer is not large enough to hold the line,
80 .BR getline ()
81 resizes it with
82 .BR realloc (3),
83 updating
84 .I "*lineptr"
85 and
86 .I "*n"
87 as necessary.
88
89 In either case, on a successful call,
90 .I "*lineptr"
91 and
92 .I "*n"
93 will be updated to reflect the buffer address and allocated size respectively.
94
95 .BR getdelim ()
96 works like
97 .BR getline (),
98 except a line delimiter other than newline can be specified as the
99 .I delimiter
100 argument.
101 As with
102 .BR getline (),
103 a delimiter character is not added if one was not present
104 in the input before end of file was reached.
105 .SH "RETURN VALUE"
106 On success,
107 .BR getline ()
108 and
109 .BR getdelim ()
110 return the number of characters read, including the delimiter character,
111 but not including the terminating null byte.
112 This value can be used
113 to handle embedded null bytes in the line read.
114
115 Both functions return \-1  on failure to read a line (including end of file
116 condition).
117 .SH ERRORS
118 .TP
119 .B EINVAL
120 Bad arguments
121 .RI ( n
122 or
123 .I lineptr
124 is NULL, or
125 .I stream
126 is not valid).
127 .SH VERSIONS
128 These functions are available since libc 4.6.27.
129 .SH "CONFORMING TO"
130 Both
131 .BR getline ()
132 and
133 .BR getdelim ()
134 were originally GNU extensions.
135 They were standardized in POSIX.1-2008.
136 .SH "EXAMPLE"
137 .nf
138 #define _GNU_SOURCE
139 #include <stdio.h>
140 #include <stdlib.h>
141
142 int
143 main(void)
144 {
145     FILE * fp;
146     char * line = NULL;
147     size_t len = 0;
148     ssize_t read;
149
150     fp = fopen("/etc/motd", "r");
151     if (fp == NULL)
152         exit(EXIT_FAILURE);
153
154     while ((read = getline(&line, &len, fp)) != \-1) {
155         printf("Retrieved line of length %zu :\en", read);
156         printf("%s", line);
157     }
158
159     if (line)
160         free(line);
161     exit(EXIT_SUCCESS);
162 }
163 .fi
164 .SH "SEE ALSO"
165 .BR read (2),
166 .BR fgets (3),
167 .BR fopen (3),
168 .BR fread (3),
169 .BR gets (3),
170 .BR scanf (3),
171 .BR feature_test_macros (7)