1 .\" $NetBSD: getdelim.3,v 1.8 2010/06/30 13:38:10 jruoho 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.
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
55 .Dv NUL Ns No -terminated
56 and includes the delimiter.
59 character must be representable as an unsigned char.
65 must be pre-allocated to at least
68 The buffer should be allocated dynamically;
69 it must be possible to
75 is large enough to hold the input, updating
77 to reflect the new size.
81 function is equivalent to
85 set to the newline character.
91 functions return the number of characters read, including the delimiter.
92 If no characters were read and the stream is at end-of-file, the functions
94 If an error occurs, the functions return \-1 and the global variable
96 is set to indicate the error.
98 The functions do not distinguish between end-of-file and error,
103 to determine which occurred.
105 The following code fragment reads lines from a file and writes them to
107 .Bd -literal -offset indent
111 while ((linelen = getline(\*[Am]line, \*[Am]linesize, fp)) != -1)
112 fwrite(line, linelen, 1, stdout);
118 .Bl -tag -width [EOVERFLOW]
127 More than SSIZE_MAX characters were read without encountering the delimiter.
134 functions may also fail and set
136 for any of the errors specified in the routines