]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/getline.3
Removed trailing white space at end of lines
[thirdparty/man-pages.git] / man3 / getline.3
CommitLineData
fea681da
MK
1.\" Copyright (c) 2001 John Levon <moz@compsoc.man.ac.uk>
2.\" Based in part on GNU libc documentation
3.\"
93015253 4.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
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.
8.\"
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.
c13182ef 13.\"
fea681da
MK
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
20.\" professionally.
c13182ef 21.\"
fea681da
MK
22.\" Formatted or processed versions of this manual, if unaccompanied by
23.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 24.\" %%%LICENSE_END
c08df37a 25.\"
9ba01802 26.TH GETLINE 3 2019-03-06 "GNU" "Linux Programmer's Manual"
fea681da
MK
27.SH NAME
28getline, getdelim \- delimited string input
29.SH SYNOPSIS
30.nf
fea681da 31.B #include <stdio.h>
68e4db0a 32.PP
fea681da 33.BI "ssize_t getline(char **" lineptr ", size_t *" n ", FILE *" stream );
dbfe9c70 34.PP
c8250206
MK
35.BI "ssize_t getdelim(char **" lineptr ", size_t *" n ", int " delim \
36", FILE *" stream );
37.fi
68e4db0a 38.PP
6deb7a03
MK
39.in -4n
40Feature Test Macro Requirements for glibc (see
41.BR feature_test_macros (7)):
42.in
68e4db0a 43.PP
6f1b61a1 44.ad l
6deb7a03
MK
45.BR getline (),
46.BR getdelim ():
cfb08d1e
MK
47.PD 0
48.RS 4
49.TP 4
50Since glibc 2.10:
b0da7b8b 51_POSIX_C_SOURCE\ >=\ 200809L
cfb08d1e
MK
52.TP
53Before glibc 2.10:
54_GNU_SOURCE
55.RE
56.PD
6f1b61a1 57.ad
fea681da 58.SH DESCRIPTION
63aa9df0 59.BR getline ()
c13182ef 60reads an entire line from \fIstream\fP,
2df47dc8 61storing the address of the buffer containing the text into
a5e0a0e4 62.IR "*lineptr" .
2df47dc8
MK
63The buffer is null-terminated and includes the newline character, if
64one was found.
847e0d88 65.PP
fea681da 66If
0daa9e92 67.I "*lineptr"
ac16fdff
MK
68is set to NULL and
69.I *n
70is set 0 before the call, then
63aa9df0 71.BR getline ()
cc82e457
AS
72will allocate a buffer for storing the line.
73This buffer should be freed by the user program
74even if
75.BR getline ()
76failed.
847e0d88 77.PP
fea681da 78Alternatively, before calling
e1d6264d 79.BR getline (),
0daa9e92 80.I "*lineptr"
fea681da 81can contain a pointer to a
fb186734 82.BR malloc (3)\-allocated
fea681da 83buffer
0daa9e92 84.I "*n"
c13182ef
MK
85bytes in size.
86If the buffer is not large enough to hold the line,
63aa9df0 87.BR getline ()
2df47dc8 88resizes it with
fb186734 89.BR realloc (3),
fea681da 90updating
0daa9e92 91.I "*lineptr"
fea681da 92and
0daa9e92 93.I "*n"
c13182ef 94as necessary.
847e0d88 95.PP
c13182ef 96In either case, on a successful call,
0daa9e92 97.I "*lineptr"
fea681da 98and
0daa9e92 99.I "*n"
2df47dc8 100will be updated to reflect the buffer address and allocated size respectively.
847e0d88 101.PP
63aa9df0 102.BR getdelim ()
fea681da 103works like
e1d6264d 104.BR getline (),
6f1b61a1 105except that a line delimiter other than newline can be specified as the
0daa9e92 106.I delimiter
c13182ef
MK
107argument.
108As with
e1d6264d 109.BR getline (),
fea681da
MK
110a delimiter character is not added if one was not present
111in the input before end of file was reached.
47297adb 112.SH RETURN VALUE
fea681da 113On success,
e1d6264d 114.BR getline ()
fea681da 115and
f87925c6 116.BR getdelim ()
fea681da 117return the number of characters read, including the delimiter character,
d1a71985 118but not including the terminating null byte (\(aq\e0\(aq).
c13182ef 119This value can be used
28d88c17 120to handle embedded null bytes in the line read.
847e0d88 121.PP
1c1a8c02 122Both functions return \-1 on failure to read a line (including end-of-file
fea681da 123condition).
1790ea44
MK
124In the event of an error,
125.I errno
126is set to indicate the cause.
fea681da
MK
127.SH ERRORS
128.TP
129.B EINVAL
c4bb193f 130Bad arguments
fea681da
MK
131.RI ( n
132or
133.I lineptr
134is NULL, or
135.I stream
136is not valid).
a2db5b9d
JH
137.TP
138.B ENOMEM
139Allocation or reallocation of the line buffer failed.
65317e78
MS
140.SH ATTRIBUTES
141For an explanation of the terms used in this section, see
142.BR attributes (7).
143.TS
144allbox;
145lbw21 lb lb
146l l l.
147Interface Attribute Value
148T{
149.BR getline (),
150.BR getdelim ()
151T} Thread safety MT-Safe
152.TE
847e0d88 153.sp 1
47297adb 154.SH CONFORMING TO
2b2581ee
MK
155Both
156.BR getline ()
157and
158.BR getdelim ()
6deb7a03
MK
159were originally GNU extensions.
160They were standardized in POSIX.1-2008.
47297adb 161.SH EXAMPLE
207050fa 162.EX
fea681da
MK
163#define _GNU_SOURCE
164#include <stdio.h>
165#include <stdlib.h>
166
c13182ef 167int
cbc616e3 168main(int argc, char *argv[])
fea681da 169{
363b9dce 170 FILE *stream;
9b50d251 171 char *line = NULL;
cf0a9ace 172 size_t len = 0;
809b6c47 173 ssize_t nread;
e77234bb 174
cbc616e3 175 if (argc != 2) {
f0e956ca 176 fprintf(stderr, "Usage: %s <file>\en", argv[0]);
cbc616e3
MK
177 exit(EXIT_FAILURE);
178 }
179
180 stream = fopen(argv[1], "r");
0885f950
MK
181 if (stream == NULL) {
182 perror("fopen");
cf0a9ace 183 exit(EXIT_FAILURE);
0885f950 184 }
e77234bb 185
809b6c47 186 while ((nread = getline(&line, &len, stream)) != \-1) {
71fd04a3 187 printf("Retrieved line of length %zu:\en", nread);
1b1069ee 188 fwrite(line, nread, 1, stdout);
cf0a9ace 189 }
e77234bb 190
7a4a8221 191 free(line);
363b9dce 192 fclose(stream);
e77234bb 193 exit(EXIT_SUCCESS);
fea681da 194}
207050fa 195.EE
47297adb 196.SH SEE ALSO
fea681da
MK
197.BR read (2),
198.BR fgets (3),
199.BR fopen (3),
200.BR fread (3),
0a4f8b7b 201.BR scanf (3)