3 .\" This file and its contents are supplied under the terms of the
4 .\" Common Development and Distribution License ("CDDL"), version 1.0.
5 .\" You may only use this file in accordance with the terms of version
8 .\" A full copy of the text of the CDDL should have accompanied this
9 .\" source. A copy of the CDDL is also available via the Internet at
10 .\" http://www.illumos.org/license/CDDL.
13 .\" Copyright (c) 2013, Joyent, Inc. All rights reserved.
15 .TH GETLINE 3C "April 9, 2016"
17 getline, getdelim \- read delimited input from streams
26 \fBssize_t\fR \fBgetline\fR(\fBchar **restrict\fR \fIptr\fR, \
27 \fBsize_t *restrict\fR \fIcap\fR,
28 \fBFILE *restrict\fR \fIstream\fR);
33 \fBssize_t\fR \fBgetdelim\fR(\fBchar **restrict\fR \fIptr\fR, \
34 \fBsize_t *restrict\fR \fIcap\fR,
35 \fBint\fR \fIdelimiter\fR, \fBFILE *restrict\fR \fIstream\fR);
39 The \fBgetdelim\fR() function reads bytes from the \fIstream\fR into the
40 array pointed to by \fIptr\fR, until the \fIdelimiter\fR byte or an end-of-file
41 condition is encountered. The \fBgetline\fR() function is identical in
42 behaviour, but uses the newline character as the delimiter. The delimiter
43 character is included in the string (unless end-of-file was reached first) and
44 the string is terminated with a null byte.
46 The caller may pass a pre-allocated \fBmalloc\fR(3C) buffer as \fI*ptr\fR,
47 along with the capacity of that buffer as \fI*cap\fR. It is also valid to pass
48 \fBNULL\fR for \fI*ptr\fR and \fB0\fR for \fI*cap\fR, at which point memory
49 will be allocated automatically. If the buffer provided is not large enough to
50 hold the string it will be expanded, as if via \fBrealloc(3C)\fR. The caller
51 must \fBfree(3C)\fR the buffer when it is no longer required.
55 If successful, \fBgetdelim\fR() and \fBgetline\fR() return the number of bytes
56 written into the buffer, excluding the terminating null byte. If an error
57 occurs, or if end-of-file is reached prior to reading any bytes, the value
58 \fB\(mi1\fR is returned and \fIerrno\fR is set to indicate the error.
62 The \fBgetline\fR() and \fBgetdelim\fR() functions may fail due to the
71 Either \fIptr\fR or \fIcap\fR are \fBNULL\fR, or the \fIdelimiter\fR is
72 not a valid character.
81 More than \fBSSIZE_MAX\fR characters were read from the stream without
82 encountering the \fIdelimiter\fR.
87 The \fBgetline\fR() and \fBgetdelim\fR() functions may also fail and set
88 \fIerrno\fR for any of the errors specified for the library routines
89 \fBrealloc\fR(3C) or \fBfgetc\fR(3C).
93 \fBExample 1\fR Read a line from \fBstdin\fR.
96 The following example uses \fBgetline\fR to read a line from stdin.
106 if (getline(&ptr, &cap, stdin) == -1) {
110 fprintf(stdout, "input line: %s", ptr);
122 ATTRIBUTE TYPE ATTRIBUTE VALUE
124 Interface Stability Committed
131 \fBfgetc\fR(3C), \fBfgets\fR(3C), \fBfree\fR(3C), \fBmalloc\fR(3C),
132 \fBrealloc\fR(3C), \fBattributes\fR(5)