2 .\" Copyright (c) 2001 John Levon <moz@compsoc.man.ac.uk>
3 .\" Based in part on GNU libc documentation
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .TH getline 3 (date) "Linux man-pages (unreleased)"
9 getline, getdelim \- delimited string input
12 .RI ( libc ", " \-lc )
17 .BI "ssize_t getline(char **restrict " lineptr ", size_t *restrict " n ,
18 .BI " FILE *restrict " stream );
19 .BI "ssize_t getdelim(char **restrict " lineptr ", size_t *restrict " n ,
20 .BI " int " delim ", FILE *restrict " stream );
24 Feature Test Macro Requirements for glibc (see
25 .BR feature_test_macros (7)):
32 _POSIX_C_SOURCE >= 200809L
38 reads an entire line from \fIstream\fP,
39 storing the address of the buffer containing the text into
41 The buffer is null-terminated and includes the newline character, if
46 is set to NULL before the call, then
48 will allocate a buffer for storing the line.
49 This buffer should be freed by the user program
54 Alternatively, before calling
57 can contain a pointer to a
58 .BR malloc (3)\-allocated
62 If the buffer is not large enough to hold the line,
72 In either case, on a successful call,
76 will be updated to reflect the buffer address and allocated size respectively.
81 except that a line delimiter other than newline can be specified as the
86 a delimiter character is not added if one was not present
87 in the input before end of file was reached.
93 return the number of characters read, including the delimiter character,
94 but not including the terminating null byte (\[aq]\e0\[aq]).
95 This value can be used
96 to handle embedded null bytes in the line read.
98 Both functions return \-1 on failure to read a line (including end-of-file
100 In the event of a failure,
102 is set to indicate the error.
106 was set to NULL before the call, then the buffer should be freed by the
107 user program even on failure.
120 Allocation or reallocation of the line buffer failed.
122 For an explanation of the terms used in this section, see
130 Interface Attribute Value
134 T} Thread safety MT-Safe
144 .\" SRC BEGIN (getline.c)
151 main(int argc, char *argv[])
159 fprintf(stderr, "Usage: %s <file>\en", argv[0]);
163 stream = fopen(argv[1], "r");
164 if (stream == NULL) {
169 while ((nread = getline(&line, &len, stream)) != \-1) {
170 printf("Retrieved line of length %zd:\en", nread);
171 fwrite(line, nread, 1, stdout);