1.\" $OpenBSD: fgetln.3,v 1.15 2007/06/21 13:47:07 moritz 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: June 21 2007 $ 31.Dt FGETLN 3 32.Os 33.Sh NAME 34.Nm fgetln 35.Nd get a line from a stream 36.Sh SYNOPSIS 37.Fd #include <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 48.Tn NUL 49character. 50The length of the line, including the final newline, 51is stored in the memory location to which 52.Fa len 53points and is guaranteed to be greater than 0 upon successful completion. 54(Note, however, that if the last line in the stream does not end in a newline, 55the returned text will not contain a newline.) 56.Sh RETURN VALUES 57Upon successful completion a pointer is returned; 58this pointer becomes invalid after the next 59.Tn I/O 60operation on 61.Fa stream 62(whether successful or not) 63or as soon as the stream is closed. 64Otherwise, 65.Dv NULL 66is returned. 67.Pp 68The 69.Fn fgetln 70function does not distinguish between end-of-file and error; the routines 71.Xr feof 3 72and 73.Xr ferror 3 74must be used 75to determine which occurred. 76If an error occurs, the global variable 77.Va errno 78is set to indicate the error. 79The end-of-file condition is remembered, even on a terminal, and all 80subsequent attempts to read will return 81.Dv NULL 82until the condition is 83cleared with 84.Xr clearerr 3 . 85.Pp 86The text to which the returned pointer points may be modified, 87provided that no changes are made beyond the returned size. 88These changes are lost as soon as the pointer becomes invalid. 89.Sh ERRORS 90.Bl -tag -width [EBADF] 91.It Bq Er EBADF 92The argument 93.Fa stream 94is not a stream open for reading. 95.El 96.Pp 97The 98.Fn fgetln 99function may also fail and set 100.Va errno 101for any of the errors specified for the routines 102.Xr fflush 3 , 103.Xr malloc 3 , 104.Xr read 2 , 105.Xr stat 2 , 106or 107.Xr realloc 3 . 108.Sh SEE ALSO 109.Xr ferror 3 , 110.Xr fgets 3 , 111.Xr fopen 3 , 112.Xr fparseln 3 , 113.Xr putc 3 114.Sh HISTORY 115The 116.Fn fgetln 117function first appeared in 118.Bx 4.4 . 119.Sh CAVEATS 120Since the returned buffer is not a C string (it is not NUL terminated), a 121common practice is to replace the newline character with 122.Sq \e0 . 123However, if the last line in a file does not contain a newline, 124the returned text won't contain a newline either. 125The following code demonstrates how to deal with this problem by allocating a 126temporary buffer: 127.Bd -literal 128 char *buf, *lbuf; 129 size_t len; 130 131 lbuf = NULL; 132 while ((buf = fgetln(fp, &len))) { 133 if (buf[len - 1] == '\en') 134 buf[len - 1] = '\e0'; 135 else { 136 /* EOF without EOL, copy and add the NUL */ 137 if ((lbuf = malloc(len + 1)) == NULL) 138 err(1, NULL); 139 memcpy(lbuf, buf, len); 140 lbuf[len] = '\e0'; 141 buf = lbuf; 142 } 143 printf("%s\en", buf); 144 } 145 free(lbuf); 146.Ed 147