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 2021-03-22 "GNU" "Linux Programmer's Manual"
28 getline, getdelim \- delimited string input
33 .BI "ssize_t getline(char **restrict " lineptr ", size_t *restrict " n ,
34 .BI " FILE *restrict " stream );
35 .BI "ssize_t getdelim(char **restrict " lineptr ", size_t *restrict " n ,
36 .BI " int " delim ", FILE *restrict " stream );
40 Feature Test Macro Requirements for glibc (see
41 .BR feature_test_macros (7)):
48 _POSIX_C_SOURCE >= 200809L
54 reads an entire line from \fIstream\fP,
55 storing the address of the buffer containing the text into
57 The buffer is null-terminated and includes the newline character, if
62 is set to NULL before the call, then
64 will allocate a buffer for storing the line.
65 This buffer should be freed by the user program
70 Alternatively, before calling
73 can contain a pointer to a
74 .BR malloc (3)\-allocated
78 If the buffer is not large enough to hold the line,
88 In either case, on a successful call,
92 will be updated to reflect the buffer address and allocated size respectively.
97 except that a line delimiter other than newline can be specified as the
102 a delimiter character is not added if one was not present
103 in the input before end of file was reached.
109 return the number of characters read, including the delimiter character,
110 but not including the terminating null byte (\(aq\e0\(aq).
111 This value can be used
112 to handle embedded null bytes in the line read.
114 Both functions return \-1 on failure to read a line (including end-of-file
116 In the event of a failure,
118 is set to indicate the error.
131 Allocation or reallocation of the line buffer failed.
133 For an explanation of the terms used in this section, see
141 Interface Attribute Value
145 T} Thread safety MT-Safe
155 were originally GNU extensions.
156 They were standardized in POSIX.1-2008.
164 main(int argc, char *argv[])
172 fprintf(stderr, "Usage: %s <file>\en", argv[0]);
176 stream = fopen(argv[1], "r");
177 if (stream == NULL) {
182 while ((nread = getline(&line, &len, stream)) != \-1) {
183 printf("Retrieved line of length %zd:\en", nread);
184 fwrite(line, nread, 1, stdout);