]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/getline.3
intro.1, _exit.2, access.2, alarm.2, alloc_hugepages.2, arch_prctl.2, bind.2, chdir...
[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.\"
7a4a8221 26.TH GETLINE 3 2010-06-12 "GNU" "Linux Programmer's Manual"
fea681da
MK
27.SH NAME
28getline, getdelim \- delimited string input
29.SH SYNOPSIS
30.nf
fea681da
MK
31.B #include <stdio.h>
32.sp
33.BI "ssize_t getline(char **" lineptr ", size_t *" n ", FILE *" stream );
5895e7eb 34
c8250206
MK
35.BI "ssize_t getdelim(char **" lineptr ", size_t *" n ", int " delim \
36", FILE *" stream );
37.fi
6deb7a03
MK
38.sp
39.in -4n
40Feature Test Macro Requirements for glibc (see
41.BR feature_test_macros (7)):
42.in
cfb08d1e 43.sp
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:
6f1b61a1 51_POSIX_C_SOURCE\ >=\ 200809L || _XOPEN_SOURCE\ >=\ 700
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.
fea681da 65
fea681da 66If
0daa9e92 67.I "*lineptr"
2df47dc8 68is NULL, then
63aa9df0 69.BR getline ()
865be3cc
MK
70will allocate a buffer for storing the line,
71which should be freed by the user program.
9f864ad2 72(In this case, the value in
865be3cc
MK
73.I *n
74is ignored.)
75
fea681da 76Alternatively, before calling
e1d6264d 77.BR getline (),
0daa9e92 78.I "*lineptr"
fea681da 79can contain a pointer to a
fb186734 80.BR malloc (3)\-allocated
fea681da 81buffer
0daa9e92 82.I "*n"
c13182ef
MK
83bytes in size.
84If the buffer is not large enough to hold the line,
63aa9df0 85.BR getline ()
2df47dc8 86resizes it with
fb186734 87.BR realloc (3),
fea681da 88updating
0daa9e92 89.I "*lineptr"
fea681da 90and
0daa9e92 91.I "*n"
c13182ef 92as necessary.
865be3cc 93
c13182ef 94In either case, on a successful call,
0daa9e92 95.I "*lineptr"
fea681da 96and
0daa9e92 97.I "*n"
2df47dc8 98will be updated to reflect the buffer address and allocated size respectively.
fea681da 99
63aa9df0 100.BR getdelim ()
fea681da 101works like
e1d6264d 102.BR getline (),
6f1b61a1 103except that a line delimiter other than newline can be specified as the
0daa9e92 104.I delimiter
c13182ef
MK
105argument.
106As with
e1d6264d 107.BR getline (),
fea681da
MK
108a delimiter character is not added if one was not present
109in the input before end of file was reached.
47297adb 110.SH RETURN VALUE
fea681da 111On success,
e1d6264d 112.BR getline ()
fea681da 113and
f87925c6 114.BR getdelim ()
fea681da 115return the number of characters read, including the delimiter character,
c13182ef
MK
116but not including the terminating null byte.
117This value can be used
28d88c17 118to handle embedded null bytes in the line read.
fea681da 119
1c1a8c02 120Both functions return \-1 on failure to read a line (including end-of-file
fea681da 121condition).
fea681da
MK
122.SH ERRORS
123.TP
124.B EINVAL
c4bb193f 125Bad arguments
fea681da
MK
126.RI ( n
127or
128.I lineptr
129is NULL, or
130.I stream
131is not valid).
6deb7a03
MK
132.SH VERSIONS
133These functions are available since libc 4.6.27.
47297adb 134.SH CONFORMING TO
2b2581ee
MK
135Both
136.BR getline ()
137and
138.BR getdelim ()
6deb7a03
MK
139were originally GNU extensions.
140They were standardized in POSIX.1-2008.
47297adb 141.SH EXAMPLE
fea681da
MK
142.nf
143#define _GNU_SOURCE
144#include <stdio.h>
145#include <stdlib.h>
146
c13182ef 147int
cf0a9ace 148main(void)
fea681da 149{
9b50d251
MK
150 FILE *fp;
151 char *line = NULL;
cf0a9ace
MK
152 size_t len = 0;
153 ssize_t read;
e77234bb 154
cf0a9ace
MK
155 fp = fopen("/etc/motd", "r");
156 if (fp == NULL)
157 exit(EXIT_FAILURE);
e77234bb 158
cf0a9ace 159 while ((read = getline(&line, &len, fp)) != \-1) {
31a6818e 160 printf("Retrieved line of length %zu :\en", read);
cf0a9ace
MK
161 printf("%s", line);
162 }
e77234bb 163
7a4a8221 164 free(line);
e77234bb 165 exit(EXIT_SUCCESS);
fea681da
MK
166}
167.fi
47297adb 168.SH SEE ALSO
fea681da
MK
169.BR read (2),
170.BR fgets (3),
171.BR fopen (3),
172.BR fread (3),
173.BR gets (3),
0a4f8b7b 174.BR scanf (3)