1 .\" Copyright (c) 2001 John Levon <moz@compsoc.man.ac.uk>
2 .\" Based in part on GNU libc documentation
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.
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.
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
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH GETLINE 3 2019-03-06 "GNU" "Linux Programmer's Manual"
28 getline, getdelim \- delimited string input
33 .BI "ssize_t getline(char **" lineptr ", size_t *" n ", FILE *" stream );
35 .BI "ssize_t getdelim(char **" lineptr ", size_t *" n ", int " delim \
40 Feature Test Macro Requirements for glibc (see
41 .BR feature_test_macros (7)):
51 _POSIX_C_SOURCE\ >=\ 200809L
60 reads an entire line from \fIstream\fP,
61 storing the address of the buffer containing the text into
63 The buffer is null-terminated and includes the newline character, if
70 is set 0 before the call, then
72 will allocate a buffer for storing the line.
73 This buffer should be freed by the user program
78 Alternatively, before calling
81 can contain a pointer to a
82 .BR malloc (3)\-allocated
86 If the buffer is not large enough to hold the line,
96 In either case, on a successful call,
100 will be updated to reflect the buffer address and allocated size respectively.
105 except that a line delimiter other than newline can be specified as the
110 a delimiter character is not added if one was not present
111 in the input before end of file was reached.
117 return the number of characters read, including the delimiter character,
118 but not including the terminating null byte (\(aq\e0\(aq).
119 This value can be used
120 to handle embedded null bytes in the line read.
122 Both functions return \-1 on failure to read a line (including end-of-file
124 In the event of an error,
126 is set to indicate the cause.
139 Allocation or reallocation of the line buffer failed.
141 For an explanation of the terms used in this section, see
147 Interface Attribute Value
151 T} Thread safety MT-Safe
159 were originally GNU extensions.
160 They were standardized in POSIX.1-2008.
168 main(int argc, char *argv[])
176 fprintf(stderr, "Usage: %s <file>\en", argv[0]);
180 stream = fopen(argv[1], "r");
181 if (stream == NULL) {
186 while ((nread = getline(&line, &len, stream)) != \-1) {
187 printf("Retrieved line of length %zu:\en", nread);
188 fwrite(line, nread, 1, stdout);