xref: /freebsd/lib/libc/stdio/getline.3 (revision dd47921eac70fde581261a1890d7c1bc1cfbc4a4)
169099ba2SDavid Schultz.\" Copyright (c) 2009 David Schultz <das@FreeBSD.org>
269099ba2SDavid Schultz.\" All rights reserved.
369099ba2SDavid Schultz.\"
469099ba2SDavid Schultz.\" Redistribution and use in source and binary forms, with or without
569099ba2SDavid Schultz.\" modification, are permitted provided that the following conditions
669099ba2SDavid Schultz.\" are met:
769099ba2SDavid Schultz.\" 1. Redistributions of source code must retain the above copyright
869099ba2SDavid Schultz.\"    notice, this list of conditions and the following disclaimer.
969099ba2SDavid Schultz.\" 2. Redistributions in binary form must reproduce the above copyright
1069099ba2SDavid Schultz.\"    notice, this list of conditions and the following disclaimer in the
1169099ba2SDavid Schultz.\"    documentation and/or other materials provided with the distribution.
1269099ba2SDavid Schultz.\"
1369099ba2SDavid Schultz.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1469099ba2SDavid Schultz.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1569099ba2SDavid Schultz.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1669099ba2SDavid Schultz.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1769099ba2SDavid Schultz.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1869099ba2SDavid Schultz.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1969099ba2SDavid Schultz.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2069099ba2SDavid Schultz.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2169099ba2SDavid Schultz.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2269099ba2SDavid Schultz.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2369099ba2SDavid Schultz.\" SUCH DAMAGE.
2469099ba2SDavid Schultz.\"
2569099ba2SDavid Schultz.\" $FreeBSD$
2669099ba2SDavid Schultz.\"
27*dd47921eSBaptiste Daroussin.Dd July 30, 2016
2869099ba2SDavid Schultz.Dt GETLINE 3
2969099ba2SDavid Schultz.Os
3069099ba2SDavid Schultz.Sh NAME
3169099ba2SDavid Schultz.Nm getdelim ,
3269099ba2SDavid Schultz.Nm getline
3369099ba2SDavid Schultz.Nd get a line from a stream
3469099ba2SDavid Schultz.Sh LIBRARY
3569099ba2SDavid Schultz.Lb libc
3669099ba2SDavid Schultz.Sh SYNOPSIS
3769099ba2SDavid Schultz.In stdio.h
3869099ba2SDavid Schultz.Ft ssize_t
3969099ba2SDavid Schultz.Fn getdelim "char ** restrict linep" "size_t * restrict linecapp" "int delimiter" " FILE * restrict stream"
4069099ba2SDavid Schultz.Ft ssize_t
4169099ba2SDavid Schultz.Fn getline "char ** restrict linep" "size_t * restrict linecapp" " FILE * restrict stream"
4269099ba2SDavid Schultz.Sh DESCRIPTION
4369099ba2SDavid SchultzThe
4469099ba2SDavid Schultz.Fn getdelim
4569099ba2SDavid Schultzfunction reads a line from
4669099ba2SDavid Schultz.Fa stream ,
4769099ba2SDavid Schultzdelimited by the character
4869099ba2SDavid Schultz.Fa delimiter .
4969099ba2SDavid SchultzThe
5069099ba2SDavid Schultz.Fn getline
5169099ba2SDavid Schultzfunction is equivalent to
5269099ba2SDavid Schultz.Fn getdelim
5369099ba2SDavid Schultzwith the newline character as the delimiter.
5469099ba2SDavid SchultzThe delimiter character is included as part of the line, unless
5569099ba2SDavid Schultzthe end of the file is reached.
5606127c9cSDavid Schultz.Pp
5706127c9cSDavid SchultzThe caller may provide a pointer to a malloced buffer for the line in
5869099ba2SDavid Schultz.Fa *linep ,
5969099ba2SDavid Schultzand the capacity of that buffer in
6006127c9cSDavid Schultz.Fa *linecapp .
6106127c9cSDavid SchultzThese functions expand the buffer as needed, as if via
6206127c9cSDavid Schultz.Fn realloc .
6306127c9cSDavid SchultzIf
6406127c9cSDavid Schultz.Fa linep
6506127c9cSDavid Schultzpoints to a
6606127c9cSDavid Schultz.Dv NULL
6706127c9cSDavid Schultzpointer, a new buffer will be allocated.
6806127c9cSDavid SchultzIn either case,
6969099ba2SDavid Schultz.Fa *linep
7069099ba2SDavid Schultzand
7169099ba2SDavid Schultz.Fa *linecapp
7206127c9cSDavid Schultzwill be updated accordingly.
7369099ba2SDavid Schultz.Sh RETURN VALUES
7469099ba2SDavid SchultzThe
7569099ba2SDavid Schultz.Fn getdelim
7669099ba2SDavid Schultzand
7769099ba2SDavid Schultz.Fn getline
788c039ae1SEitan Adlerfunctions return the number of characters stored in the buffer, excluding the
7969099ba2SDavid Schultzterminating
805d26f10fSMatteo Riondato.Dv NUL
815d26f10fSMatteo Riondatocharacter.
826685ac34SDavid SchultzThe value \-1 is returned if an error occurs, or if end-of-file is reached.
8369099ba2SDavid Schultz.Sh EXAMPLES
8469099ba2SDavid SchultzThe following code fragment reads lines from a file and
8569099ba2SDavid Schultzwrites them to standard output.
8669099ba2SDavid SchultzThe
8769099ba2SDavid Schultz.Fn fwrite
8869099ba2SDavid Schultzfunction is used in case the line contains embedded
8969099ba2SDavid Schultz.Dv NUL
9069099ba2SDavid Schultzcharacters.
9169099ba2SDavid Schultz.Bd -literal -offset indent
9269099ba2SDavid Schultzchar *line = NULL;
9369099ba2SDavid Schultzsize_t linecap = 0;
9469099ba2SDavid Schultzssize_t linelen;
9569099ba2SDavid Schultzwhile ((linelen = getline(&line, &linecap, fp)) > 0)
9669099ba2SDavid Schultz	fwrite(line, linelen, 1, stdout);
97500e59c6SNick Hibmafree(line);
9869099ba2SDavid Schultz.Ed
9969099ba2SDavid Schultz.Sh ERRORS
10069099ba2SDavid SchultzThese functions may fail if:
10169099ba2SDavid Schultz.Bl -tag -width Er
10269099ba2SDavid Schultz.It Bq Er EINVAL
10369099ba2SDavid SchultzEither
10469099ba2SDavid Schultz.Fa linep
10569099ba2SDavid Schultzor
10669099ba2SDavid Schultz.Fa linecapp
10769099ba2SDavid Schultzis
10869099ba2SDavid Schultz.Dv NULL .
10969099ba2SDavid Schultz.It Bq Er EOVERFLOW
11069099ba2SDavid SchultzNo delimiter was found in the first
11169099ba2SDavid Schultz.Dv SSIZE_MAX
11269099ba2SDavid Schultzcharacters.
11369099ba2SDavid Schultz.El
11469099ba2SDavid Schultz.Pp
11506127c9cSDavid SchultzThese functions may also fail due to any of the errors specified for
11669099ba2SDavid Schultz.Fn fgets
11769099ba2SDavid Schultzand
11869099ba2SDavid Schultz.Fn malloc .
11969099ba2SDavid Schultz.Sh SEE ALSO
12069099ba2SDavid Schultz.Xr fgetln 3 ,
12169099ba2SDavid Schultz.Xr fgets 3 ,
12269099ba2SDavid Schultz.Xr malloc 3
12369099ba2SDavid Schultz.Sh STANDARDS
12469099ba2SDavid SchultzThe
12569099ba2SDavid Schultz.Fn getdelim
12669099ba2SDavid Schultzand
12769099ba2SDavid Schultz.Fn getline
12869099ba2SDavid Schultzfunctions conform to
12969099ba2SDavid Schultz.St -p1003.1-2008 .
13069099ba2SDavid Schultz.Sh HISTORY
13169099ba2SDavid SchultzThese routines first appeared in
13269099ba2SDavid Schultz.Fx 8.0 .
13369099ba2SDavid Schultz.Sh BUGS
13469099ba2SDavid SchultzThere are no wide character versions of
13569099ba2SDavid Schultz.Fn getdelim
13669099ba2SDavid Schultzor
13769099ba2SDavid Schultz.Fn getline .
138