1.\" $OpenBSD: fgetln.3,v 1.17 2015/01/13 14:02:30 schwarze Exp $ 2.\" 3.\" Copyright (c) 1990, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.Dd $Mdocdate: January 13 2015 $ 31.Dt FGETLN 3 32.Os 33.Sh NAME 34.Nm fgetln 35.Nd get a line from a stream 36.Sh SYNOPSIS 37.In stdio.h 38.Ft char * 39.Fn fgetln "FILE *stream" "size_t *len" 40.Sh DESCRIPTION 41The 42.Fn fgetln 43function returns a pointer to the next line from the stream referenced by 44.Fa stream . 45This line is 46.Em not 47a C string as it does not end with a terminating NUL character. 48The length of the line, including the final newline, 49is stored in the memory location to which 50.Fa len 51points and is guaranteed to be greater than 0 upon successful completion. 52(Note, however, that if the last line in the stream does not end in a newline, 53the returned text will not contain a newline.) 54.Sh RETURN VALUES 55Upon successful completion a pointer is returned; 56this pointer becomes invalid after the next I/O operation on 57.Fa stream 58(whether successful or not) 59or as soon as the stream is closed. 60Otherwise, 61.Dv NULL 62is returned. 63.Pp 64The 65.Fn fgetln 66function does not distinguish between end-of-file and error; the routines 67.Xr feof 3 68and 69.Xr ferror 3 70must be used 71to determine which occurred. 72If an error occurs, the global variable 73.Va errno 74is set to indicate the error. 75The end-of-file condition is remembered, even on a terminal, and all 76subsequent attempts to read will return 77.Dv NULL 78until the condition is 79cleared with 80.Xr clearerr 3 . 81.Pp 82The text to which the returned pointer points may be modified, 83provided that no changes are made beyond the returned size. 84These changes are lost as soon as the pointer becomes invalid. 85.Sh ERRORS 86.Bl -tag -width [EBADF] 87.It Bq Er EBADF 88The argument 89.Fa stream 90is not a stream open for reading. 91.El 92.Pp 93The 94.Fn fgetln 95function may also fail and set 96.Va errno 97for any of the errors specified for the routines 98.Xr fflush 3 , 99.Xr malloc 3 , 100.Xr read 2 , 101.Xr stat 2 , 102or 103.Xr realloc 3 . 104.Sh SEE ALSO 105.Xr ferror 3 , 106.Xr fgets 3 , 107.Xr fopen 3 , 108.Xr fparseln 3 , 109.Xr putc 3 110.Sh HISTORY 111The 112.Fn fgetln 113function first appeared in 114.Bx 4.4 . 115.Sh CAVEATS 116Since the returned buffer is not a C string (it is not NUL terminated), a 117common practice is to replace the newline character with 118.Sq \e0 . 119However, if the last line in a file does not contain a newline, 120the returned text won't contain a newline either. 121The following code demonstrates how to deal with this problem by allocating a 122temporary buffer: 123.Bd -literal 124 char *buf, *lbuf; 125 size_t len; 126 127 lbuf = NULL; 128 while ((buf = fgetln(fp, &len))) { 129 if (buf[len - 1] == '\en') 130 buf[len - 1] = '\e0'; 131 else { 132 /* EOF without EOL, copy and add the NUL */ 133 if ((lbuf = malloc(len + 1)) == NULL) 134 err(1, NULL); 135 memcpy(lbuf, buf, len); 136 lbuf[len] = '\e0'; 137 buf = lbuf; 138 } 139 printf("%s\en", buf); 140 } 141 free(lbuf); 142.Ed 143