]> git.saurik.com Git - apple/libc.git/blame - stdio/FreeBSD/getline.3
Libc-1439.100.3.tar.gz
[apple/libc.git] / stdio / FreeBSD / getline.3
CommitLineData
1f2f436a
A
1.\" Copyright (c) 2009 David Schultz <das@FreeBSD.org>
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\" notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\" notice, this list of conditions and the following disclaimer in the
11.\" documentation and/or other materials provided with the distribution.
12.\"
13.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23.\" SUCH DAMAGE.
24.\"
6465356a 25.\" $FreeBSD: src/lib/libc/stdio/getline.3,v 1.5 2012/03/29 05:02:12 eadler Exp $
1f2f436a 26.\"
6465356a 27.Dd November 30, 2010
1f2f436a
A
28.Dt GETLINE 3
29.Os
30.Sh NAME
31.Nm getdelim ,
32.Nm getline
33.Nd get a line from a stream
34.Sh LIBRARY
35.Lb libc
36.Sh SYNOPSIS
1f2f436a
A
37.In stdio.h
38.Ft ssize_t
39.Fn getdelim "char ** restrict linep" "size_t * restrict linecapp" "int delimiter" " FILE * restrict stream"
40.Ft ssize_t
41.Fn getline "char ** restrict linep" "size_t * restrict linecapp" " FILE * restrict stream"
42.Sh DESCRIPTION
43The
44.Fn getdelim
45function reads a line from
46.Fa stream ,
47delimited by the character
48.Fa delimiter .
49The
50.Fn getline
51function is equivalent to
52.Fn getdelim
53with the newline character as the delimiter.
54The delimiter character is included as part of the line, unless
55the end of the file is reached.
6465356a
A
56.Pp
57The caller may provide a pointer to a malloced buffer for the line in
1f2f436a
A
58.Fa *linep ,
59and the capacity of that buffer in
6465356a
A
60.Fa *linecapp .
61These functions expand the buffer as needed, as if via
62.Fn realloc .
63If
64.Fa linep
65points to a
66.Dv NULL
67pointer, a new buffer will be allocated.
68In either case,
1f2f436a
A
69.Fa *linep
70and
71.Fa *linecapp
6465356a 72will be updated accordingly.
1f2f436a
A
73.Sh RETURN VALUES
74The
75.Fn getdelim
76and
77.Fn getline
78functions return the number of characters written, excluding the
79terminating
6465356a
A
80.Dv NUL
81character.
1f2f436a
A
82The value \-1 is returned if an error occurs, or if end-of-file is reached.
83.Sh EXAMPLES
84The following code fragment reads lines from a file and
85writes them to standard output.
86The
87.Fn fwrite
88function is used in case the line contains embedded
89.Dv NUL
90characters.
91.Bd -literal -offset indent
92char *line = NULL;
93size_t linecap = 0;
94ssize_t linelen;
95while ((linelen = getline(&line, &linecap, fp)) > 0)
96 fwrite(line, linelen, 1, stdout);
97.Ed
1f2f436a
A
98.Sh ERRORS
99These functions may fail if:
100.Bl -tag -width Er
101.It Bq Er EINVAL
102Either
103.Fa linep
104or
105.Fa linecapp
106is
107.Dv NULL .
108.It Bq Er EOVERFLOW
109No delimiter was found in the first
110.Dv SSIZE_MAX
111characters.
112.El
113.Pp
6465356a 114These functions may also fail due to any of the errors specified for
1f2f436a
A
115.Fn fgets
116and
117.Fn malloc .
118.Sh SEE ALSO
119.Xr fgetln 3 ,
120.Xr fgets 3 ,
121.Xr malloc 3
122.Sh STANDARDS
123The
124.Fn getdelim
125and
126.Fn getline
127functions conform to
128.St -p1003.1-2008 .
129.Sh HISTORY
130These routines first appeared in
131.Fx 8.0 .
132.Sh BUGS
133There are no wide character versions of
134.Fn getdelim
135or
136.Fn getline .