]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/getline.3
_syscall.2, add_key.2, epoll_create.2, epoll_ctl.2, epoll_wait.2, getxattr.2, inotify...
[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.\"
4.\" Permission is granted to make and distribute verbatim copies of this
5.\" manual provided the copyright notice and this permission notice are
6.\" preserved on all copies.
7.\"
8.\" Permission is granted to copy and distribute modified versions of this
9.\" manual under the conditions for verbatim copying, provided that the
10.\" entire resulting derived work is distributed under the terms of a
11.\" permission notice identical to this one.
c13182ef 12.\"
fea681da
MK
13.\" Since the Linux kernel and libraries are constantly changing, this
14.\" manual page may be incorrect or out-of-date. The author(s) assume no
15.\" responsibility for errors or omissions, or for damages resulting from
16.\" the use of the information contained herein. The author(s) may not
17.\" have taken the same level of care in the production of this manual,
18.\" which is licensed free of charge, as they might when working
19.\" professionally.
c13182ef 20.\"
fea681da
MK
21.\" Formatted or processed versions of this manual, if unaccompanied by
22.\" the source, must acknowledge the copyright and authors of this work.
23.\" License.
7a4a8221 24.TH GETLINE 3 2010-06-12 "GNU" "Linux Programmer's Manual"
fea681da
MK
25.SH NAME
26getline, getdelim \- delimited string input
27.SH SYNOPSIS
28.nf
fea681da
MK
29.B #include <stdio.h>
30.sp
31.BI "ssize_t getline(char **" lineptr ", size_t *" n ", FILE *" stream );
5895e7eb 32
c8250206
MK
33.BI "ssize_t getdelim(char **" lineptr ", size_t *" n ", int " delim \
34", FILE *" stream );
35.fi
6deb7a03
MK
36.sp
37.in -4n
38Feature Test Macro Requirements for glibc (see
39.BR feature_test_macros (7)):
40.in
cfb08d1e 41.sp
6f1b61a1 42.ad l
6deb7a03
MK
43.BR getline (),
44.BR getdelim ():
cfb08d1e
MK
45.PD 0
46.RS 4
47.TP 4
48Since glibc 2.10:
6f1b61a1 49_POSIX_C_SOURCE\ >=\ 200809L || _XOPEN_SOURCE\ >=\ 700
cfb08d1e
MK
50.TP
51Before glibc 2.10:
52_GNU_SOURCE
53.RE
54.PD
6f1b61a1 55.ad
fea681da 56.SH DESCRIPTION
63aa9df0 57.BR getline ()
c13182ef 58reads an entire line from \fIstream\fP,
2df47dc8 59storing the address of the buffer containing the text into
a5e0a0e4 60.IR "*lineptr" .
2df47dc8
MK
61The buffer is null-terminated and includes the newline character, if
62one was found.
fea681da 63
fea681da 64If
0daa9e92 65.I "*lineptr"
2df47dc8 66is NULL, then
63aa9df0 67.BR getline ()
865be3cc
MK
68will allocate a buffer for storing the line,
69which should be freed by the user program.
9f864ad2 70(In this case, the value in
865be3cc
MK
71.I *n
72is ignored.)
73
fea681da 74Alternatively, before calling
e1d6264d 75.BR getline (),
0daa9e92 76.I "*lineptr"
fea681da 77can contain a pointer to a
fb186734 78.BR malloc (3)\-allocated
fea681da 79buffer
0daa9e92 80.I "*n"
c13182ef
MK
81bytes in size.
82If the buffer is not large enough to hold the line,
63aa9df0 83.BR getline ()
2df47dc8 84resizes it with
fb186734 85.BR realloc (3),
fea681da 86updating
0daa9e92 87.I "*lineptr"
fea681da 88and
0daa9e92 89.I "*n"
c13182ef 90as necessary.
865be3cc 91
c13182ef 92In either case, on a successful call,
0daa9e92 93.I "*lineptr"
fea681da 94and
0daa9e92 95.I "*n"
2df47dc8 96will be updated to reflect the buffer address and allocated size respectively.
fea681da 97
63aa9df0 98.BR getdelim ()
fea681da 99works like
e1d6264d 100.BR getline (),
6f1b61a1 101except that a line delimiter other than newline can be specified as the
0daa9e92 102.I delimiter
c13182ef
MK
103argument.
104As with
e1d6264d 105.BR getline (),
fea681da
MK
106a delimiter character is not added if one was not present
107in the input before end of file was reached.
47297adb 108.SH RETURN VALUE
fea681da 109On success,
e1d6264d 110.BR getline ()
fea681da 111and
f87925c6 112.BR getdelim ()
fea681da 113return the number of characters read, including the delimiter character,
c13182ef
MK
114but not including the terminating null byte.
115This value can be used
28d88c17 116to handle embedded null bytes in the line read.
fea681da 117
1c1a8c02 118Both functions return \-1 on failure to read a line (including end-of-file
fea681da 119condition).
fea681da
MK
120.SH ERRORS
121.TP
122.B EINVAL
c4bb193f 123Bad arguments
fea681da
MK
124.RI ( n
125or
126.I lineptr
127is NULL, or
128.I stream
129is not valid).
6deb7a03
MK
130.SH VERSIONS
131These functions are available since libc 4.6.27.
47297adb 132.SH CONFORMING TO
2b2581ee
MK
133Both
134.BR getline ()
135and
136.BR getdelim ()
6deb7a03
MK
137were originally GNU extensions.
138They were standardized in POSIX.1-2008.
47297adb 139.SH EXAMPLE
fea681da
MK
140.nf
141#define _GNU_SOURCE
142#include <stdio.h>
143#include <stdlib.h>
144
c13182ef 145int
cf0a9ace 146main(void)
fea681da 147{
9b50d251
MK
148 FILE *fp;
149 char *line = NULL;
cf0a9ace
MK
150 size_t len = 0;
151 ssize_t read;
e77234bb 152
cf0a9ace
MK
153 fp = fopen("/etc/motd", "r");
154 if (fp == NULL)
155 exit(EXIT_FAILURE);
e77234bb 156
cf0a9ace 157 while ((read = getline(&line, &len, fp)) != \-1) {
31a6818e 158 printf("Retrieved line of length %zu :\en", read);
cf0a9ace
MK
159 printf("%s", line);
160 }
e77234bb 161
7a4a8221 162 free(line);
e77234bb 163 exit(EXIT_SUCCESS);
fea681da
MK
164}
165.fi
47297adb 166.SH SEE ALSO
fea681da
MK
167.BR read (2),
168.BR fgets (3),
169.BR fopen (3),
170.BR fread (3),
171.BR gets (3),
0a4f8b7b 172.BR scanf (3)