summaryrefslogtreecommitdiffstats
path: root/upstream/fedora-40/man3/getline.3
blob: 863860f8e4b270961648ca52d19bc39c0d253c00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
'\" t
.\" Copyright (c) 2001 John Levon <moz@compsoc.man.ac.uk>
.\" Based in part on GNU libc documentation
.\"
.\" SPDX-License-Identifier: Linux-man-pages-copyleft
.\"
.TH getline 3 2023-10-31 "Linux man-pages 6.06"
.SH NAME
getline, getdelim \- delimited string input
.SH LIBRARY
Standard C library
.RI ( libc ", " \-lc )
.SH SYNOPSIS
.nf
.B #include <stdio.h>
.P
.BI "ssize_t getline(char **restrict " lineptr ", size_t *restrict " n ,
.BI "                FILE *restrict " stream );
.BI "ssize_t getdelim(char **restrict " lineptr ", size_t *restrict " n ,
.BI "                int " delim ", FILE *restrict " stream );
.fi
.P
.RS -4
Feature Test Macro Requirements for glibc (see
.BR feature_test_macros (7)):
.RE
.P
.BR getline (),
.BR getdelim ():
.nf
    Since glibc 2.10:
        _POSIX_C_SOURCE >= 200809L
    Before glibc 2.10:
        _GNU_SOURCE
.fi
.SH DESCRIPTION
.BR getline ()
reads an entire line from \fIstream\fP,
storing the address of the buffer containing the text into
.IR *lineptr .
The buffer is null-terminated and includes the newline character, if
one was found.
.P
If
.I *lineptr
is set to NULL before the call, then
.BR getline ()
will allocate a buffer for storing the line.
This buffer should be freed by the user program
even if
.BR getline ()
failed.
.P
Alternatively, before calling
.BR getline (),
.I *lineptr
can contain a pointer to a
.BR malloc (3)\-allocated
buffer
.I *n
bytes in size.
If the buffer is not large enough to hold the line,
.BR getline ()
resizes it with
.BR realloc (3),
updating
.I *lineptr
and
.I *n
as necessary.
.P
In either case, on a successful call,
.I *lineptr
and
.I *n
will be updated to reflect the buffer address and allocated size respectively.
.P
.BR getdelim ()
works like
.BR getline (),
except that a line delimiter other than newline can be specified as the
.I delimiter
argument.
As with
.BR getline (),
a delimiter character is not added if one was not present
in the input before end of file was reached.
.SH RETURN VALUE
On success,
.BR getline ()
and
.BR getdelim ()
return the number of characters read, including the delimiter character,
but not including the terminating null byte (\[aq]\e0\[aq]).
This value can be used
to handle embedded null bytes in the line read.
.P
Both functions return \-1 on failure to read a line (including end-of-file
condition).
In the event of a failure,
.I errno
is set to indicate the error.
.P
If
.I *lineptr
was set to NULL before the call, then the buffer should be freed by the
user program even on failure.
.SH ERRORS
.TP
.B EINVAL
Bad arguments
.RI ( n
or
.I lineptr
is NULL, or
.I stream
is not valid).
.TP
.B ENOMEM
Allocation or reallocation of the line buffer failed.
.SH ATTRIBUTES
For an explanation of the terms used in this section, see
.BR attributes (7).
.TS
allbox;
lbx lb lb
l l l.
Interface	Attribute	Value
T{
.na
.nh
.BR getline (),
.BR getdelim ()
T}	Thread safety	MT-Safe
.TE
.SH STANDARDS
POSIX.1-2008.
.SH HISTORY
GNU, POSIX.1-2008.
.SH EXAMPLES
.\" SRC BEGIN (getline.c)
.EX
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
\&
int
main(int argc, char *argv[])
{
    FILE *stream;
    char *line = NULL;
    size_t len = 0;
    ssize_t nread;
\&
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <file>\en", argv[0]);
        exit(EXIT_FAILURE);
    }
\&
    stream = fopen(argv[1], "r");
    if (stream == NULL) {
        perror("fopen");
        exit(EXIT_FAILURE);
    }
\&
    while ((nread = getline(&line, &len, stream)) != \-1) {
        printf("Retrieved line of length %zd:\en", nread);
        fwrite(line, nread, 1, stdout);
    }
\&
    free(line);
    fclose(stream);
    exit(EXIT_SUCCESS);
}
.EE
.\" SRC END
.SH SEE ALSO
.BR read (2),
.BR fgets (3),
.BR fopen (3),
.BR fread (3),
.BR scanf (3)