xref: /minix3/lib/libc/stdio/fgetln.3 (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc.\"	$NetBSD: fgetln.3,v 1.15 2014/06/19 14:27:50 christos Exp $
22fe8fb19SBen Gras.\"
32fe8fb19SBen Gras.\" Copyright (c) 1990, 1991, 1993
42fe8fb19SBen Gras.\"	The Regents of the University of California.  All rights reserved.
52fe8fb19SBen Gras.\"
62fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without
72fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions
82fe8fb19SBen Gras.\" are met:
92fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright
102fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer.
112fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright
122fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer in the
132fe8fb19SBen Gras.\"    documentation and/or other materials provided with the distribution.
142fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors
152fe8fb19SBen Gras.\"    may be used to endorse or promote products derived from this software
162fe8fb19SBen Gras.\"    without specific prior written permission.
172fe8fb19SBen Gras.\"
182fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
192fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
202fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
212fe8fb19SBen Gras.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
222fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
232fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
242fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
252fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
262fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
272fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
282fe8fb19SBen Gras.\" SUCH DAMAGE.
292fe8fb19SBen Gras.\"
302fe8fb19SBen Gras.\"     @(#)fgetln.3	8.3 (Berkeley) 4/19/94
312fe8fb19SBen Gras.\"
32*0a6a1f1dSLionel Sambuc.Dd June 19, 2014
332fe8fb19SBen Gras.Dt FGETLN 3
342fe8fb19SBen Gras.Os
352fe8fb19SBen Gras.Sh NAME
362fe8fb19SBen Gras.Nm fgetln
372fe8fb19SBen Gras.Nd get a line from a stream
382fe8fb19SBen Gras.Sh LIBRARY
392fe8fb19SBen Gras.Lb libc
402fe8fb19SBen Gras.Sh SYNOPSIS
412fe8fb19SBen Gras.In stdio.h
422fe8fb19SBen Gras.Ft char *
432fe8fb19SBen Gras.Fn fgetln "FILE * restrict stream" "size_t * restrict len"
442fe8fb19SBen Gras.Sh DESCRIPTION
452fe8fb19SBen GrasThe
462fe8fb19SBen Gras.Fn fgetln
472fe8fb19SBen Grasfunction
482fe8fb19SBen Grasreturns a pointer to the next line from the stream referenced by
492fe8fb19SBen Gras.Fa stream .
502fe8fb19SBen GrasThis line is
512fe8fb19SBen Gras.Em not
522fe8fb19SBen Grasa C string as it does not end with a terminating
532fe8fb19SBen Gras.Dv NUL
542fe8fb19SBen Grascharacter.
552fe8fb19SBen GrasThe length of the line, including the final newline,
562fe8fb19SBen Grasis stored in the memory location to which
572fe8fb19SBen Gras.Fa len
582fe8fb19SBen Graspoints.
592fe8fb19SBen Gras(Note, however, that if the line is the last
602fe8fb19SBen Grasin a file that does not end in a newline,
612fe8fb19SBen Grasthe returned text will not contain a newline.)
622fe8fb19SBen Gras.Sh RETURN VALUES
632fe8fb19SBen GrasUpon successful completion a pointer is returned;
642fe8fb19SBen Grasthis pointer becomes invalid after the next
652fe8fb19SBen Gras.Tn I/O
662fe8fb19SBen Grasoperation on
672fe8fb19SBen Gras.Fa stream
682fe8fb19SBen Gras(whether successful or not)
692fe8fb19SBen Grasor as soon as the stream is closed.
702fe8fb19SBen GrasOtherwise,
712fe8fb19SBen Gras.Dv NULL
722fe8fb19SBen Grasis returned.
732fe8fb19SBen GrasThe
742fe8fb19SBen Gras.Fn fgetln
752fe8fb19SBen Grasfunction
762fe8fb19SBen Grasdoes not distinguish between end-of-file and error; the routines
772fe8fb19SBen Gras.Xr feof 3
782fe8fb19SBen Grasand
792fe8fb19SBen Gras.Xr ferror 3
802fe8fb19SBen Grasmust be used
812fe8fb19SBen Grasto determine which occurred.
822fe8fb19SBen GrasIf an error occurs, the global variable
832fe8fb19SBen Gras.Va errno
842fe8fb19SBen Grasis set to indicate the error.
852fe8fb19SBen GrasThe end-of-file condition is remembered, even on a terminal, and all
862fe8fb19SBen Grassubsequent attempts to read will return
872fe8fb19SBen Gras.Dv NULL
882fe8fb19SBen Grasuntil the condition is
892fe8fb19SBen Grascleared with
902fe8fb19SBen Gras.Xr clearerr 3 .
912fe8fb19SBen Gras.Pp
922fe8fb19SBen GrasThe text to which the returned pointer points may be modified,
932fe8fb19SBen Grasprovided that no changes are made beyond the returned size.
942fe8fb19SBen GrasThese changes are lost as soon as the pointer becomes invalid.
952fe8fb19SBen Gras.Sh ERRORS
962fe8fb19SBen Gras.Bl -tag -width [EBADF]
972fe8fb19SBen Gras.It Bq Er EBADF
982fe8fb19SBen GrasThe argument
992fe8fb19SBen Gras.Fa stream
1002fe8fb19SBen Grasis not a stream open for reading.
1012fe8fb19SBen Gras.El
1022fe8fb19SBen Gras.Pp
1032fe8fb19SBen GrasThe
1042fe8fb19SBen Gras.Fn fgetln
1052fe8fb19SBen Grasfunction
1062fe8fb19SBen Grasmay also fail and set
1072fe8fb19SBen Gras.Va errno
1082fe8fb19SBen Grasfor any of the errors specified for the routines
1092fe8fb19SBen Gras.Xr fflush 3 ,
1102fe8fb19SBen Gras.Xr malloc 3 ,
1112fe8fb19SBen Gras.Xr read 2 ,
1122fe8fb19SBen Gras.Xr stat 2 ,
1132fe8fb19SBen Grasor
1142fe8fb19SBen Gras.Xr realloc 3 .
1152fe8fb19SBen Gras.Sh SEE ALSO
1162fe8fb19SBen Gras.Xr ferror 3 ,
1172fe8fb19SBen Gras.Xr fgets 3 ,
1182fe8fb19SBen Gras.Xr fopen 3 ,
1192fe8fb19SBen Gras.Xr putc 3
1202fe8fb19SBen Gras.Sh HISTORY
1212fe8fb19SBen GrasThe
1222fe8fb19SBen Gras.Fn fgetln
1232fe8fb19SBen Grasfunction first appeared in
1242fe8fb19SBen Gras.Bx 4.4 .
1252fe8fb19SBen Gras.Sh CAVEATS
1262fe8fb19SBen GrasSince the returned buffer is not a C string (it is not null terminated), a
1272fe8fb19SBen Grascommon practice is to replace the newline character with
1282fe8fb19SBen Gras.Sq \e0 .
1292fe8fb19SBen GrasHowever, if the last line in a file does not contain a newline,
1302fe8fb19SBen Grasthe returned text won't contain a newline either.
1312fe8fb19SBen GrasThe following code demonstrates how to deal with this problem by allocating a
1322fe8fb19SBen Grastemporary buffer:
1332fe8fb19SBen Gras.Bd -literal
1342fe8fb19SBen Gras	char *buf, *lbuf;
1352fe8fb19SBen Gras	size_t len;
1362fe8fb19SBen Gras
137*0a6a1f1dSLionel Sambuc	while ((lbuf = buf = fgetln(fp, &len)) != NULL) {
138*0a6a1f1dSLionel Sambuc		if (len > 0 && buf[len - 1] == '\en')
1392fe8fb19SBen Gras			buf[len - 1] = '\e0';
140*0a6a1f1dSLionel Sambuc		else if ((lbuf = strndup(buf, len + 1)) == NULL)
1412fe8fb19SBen Gras				err(1, NULL);
142*0a6a1f1dSLionel Sambuc		printf("%s\en", lbuf);
1432fe8fb19SBen Gras
144*0a6a1f1dSLionel Sambuc		if (lbuf != buf)
1452fe8fb19SBen Gras			free(lbuf);
1462fe8fb19SBen Gras	}
1472fe8fb19SBen Gras.Ed
148