Changeset View
Changeset View
Standalone View
Standalone View
lib/libc/stdio/fgetln.3
Show All 22 Lines | |||||
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||||
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||||
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||||
.\" SUCH DAMAGE. | .\" SUCH DAMAGE. | ||||
.\" | .\" | ||||
.\" @(#)fgetln.3 8.3 (Berkeley) 4/19/94 | .\" @(#)fgetln.3 8.3 (Berkeley) 4/19/94 | ||||
.\" $FreeBSD$ | .\" $FreeBSD$ | ||||
.\" | .\" | ||||
.Dd February 15, 2016 | .Dd June 11, 2020 | ||||
.Dt FGETLN 3 | .Dt FGETLN 3 | ||||
.Os | .Os | ||||
.Sh NAME | .Sh NAME | ||||
.Nm fgetln | .Nm fgetln | ||||
.Nd get a line from a stream | .Nd get a line from a stream | ||||
.Sh LIBRARY | .Sh LIBRARY | ||||
.Lb libc | .Lb libc | ||||
.Sh SYNOPSIS | .Sh SYNOPSIS | ||||
▲ Show 20 Lines • Show All 81 Lines • ▼ Show 20 Lines | |||||
.Xr fopen 3 , | .Xr fopen 3 , | ||||
.Xr getline 3 , | .Xr getline 3 , | ||||
.Xr putc 3 | .Xr putc 3 | ||||
.Sh HISTORY | .Sh HISTORY | ||||
The | The | ||||
.Fn fgetln | .Fn fgetln | ||||
function first appeared in | function first appeared in | ||||
.Bx 4.4 . | .Bx 4.4 . | ||||
.Sh CAVEATS | |||||
Since the returned buffer is not a C string (it is not NUL terminated), a | |||||
common practice is to replace the newline character with | |||||
.Sq \e0 . | |||||
However, if the last line in a file does not contain a newline, | |||||
the returned text won't contain a newline either. | |||||
The following code demonstrates how to deal with this problem by allocating a | |||||
temporary buffer: | |||||
.Bd -literal | |||||
char *buf, *lbuf; | |||||
size_t len; | |||||
lbuf = NULL; | |||||
while ((buf = fgetln(fp, &len)) != NULL) { | |||||
yuripv: Nitpicking: `!= NULL`, per style(9), "Test pointers against NULL" | |||||
Done Inline ActionsDid you mean something like the following for the while condition? ((buf = fgetln(fp, &len)) != null) gbe: Did you mean something like the following for the while condition?
((buf = fgetln(fp, &len)) ! | |||||
Not Done Inline ActionsExactly, but as it's really nitpicking, feel free to ignore this comment. yuripv: Exactly, but as it's really nitpicking, feel free to ignore this comment. | |||||
if (buf[len - 1] == '\en') | |||||
buf[len - 1] = '\e0'; | |||||
else { | |||||
/* EOF without EOL, copy and add the NUL */ | |||||
if ((lbuf = malloc(len + 1)) == NULL) | |||||
err(1, NULL); | |||||
memcpy(lbuf, buf, len); | |||||
lbuf[len] = '\e0'; | |||||
buf = lbuf; | |||||
} | |||||
printf("%s\en", buf); | |||||
} | |||||
free(lbuf); | |||||
if (ferror(fp)) | |||||
err(1, "fgetln"); | |||||
.Ed |
Nitpicking: != NULL, per style(9), "Test pointers against NULL"