1 .\" $NetBSD: getdelim.3,v 1.14 2014/09/16 08:52:02 wiz Exp $
3 .\" Copyright (c) 2009 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" This code is derived from software contributed to The NetBSD Foundation
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\" notice, this list of conditions and the following disclaimer in the
16 .\" documentation and/or other materials provided with the distribution.
18 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 .\" POSSIBILITY OF SUCH DAMAGE.
30 .Dd September 15, 2014
36 .Nd read a delimited record from a stream
42 .Fn getdelim "char ** restrict lineptr" "size_t * restrict n" "int delimiter" "FILE * restrict stream"
44 .Fn getline "char ** restrict lineptr" "size_t * restrict n" "FILE * restrict stream"
48 function reads from the
50 until it encounters a character matching
57 .Dv NUL Ns No -terminated
58 and includes the delimiter, if one was found.
61 character must be representable as an unsigned char.
67 must be pre-allocated to at least
70 The buffer should be allocated dynamically;
71 it must be possible to
77 is large enough to hold the input, updating
79 to reflect the new size.
83 function is equivalent to
87 set to the newline character.
93 functions return the number of characters read, including the delimiter if
95 If no characters were read and the stream is at end-of-file, the functions
97 If an error occurs, the functions return \-1 and the global variable
99 is set to indicate the error.
101 The functions do not distinguish between end-of-file and error,
106 to determine which occurred.
108 The following code fragment reads lines from a file and writes them to
110 .Bd -literal -offset indent
115 while ((linelen = getline(\*[Am]line, \*[Am]linesize, fp)) != -1)
116 fwrite(line, linelen, 1, stdout);
122 .Bl -tag -width [EOVERFLOW]
131 More than SSIZE_MAX characters were read without encountering the delimiter.
138 functions may also fail and set
140 for any of the errors specified in the routines
164 functions can return results that include
166 characters, which can cause the apparent length reported by
168 to be less than the true length reported by the
169 return values of the functions.