gno/usr.man/man3/fgets.3
1997-09-16 00:11:30 +00:00

153 lines
4.2 KiB
Groff

.\" Copyright (c) 1990, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" Chris Torek and the American National Standards Committee X3,
.\" on Information Processing Systems.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" 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
.\" SUCH DAMAGE.
.\"
.\" @(#)fgets.3 8.1 (Berkeley) 6/4/93
.\"
.TH FGETS 3 "15 September 1997" GNO "Library Routines"
.SH NAME
.BR fgets ,
.BR gets
\- get a line from a stream
.SH SYNOPSIS
#include <stdio.h>
.sp 1
char *\fBfgets\fR (char *\fIstr\fR, size_t \fIsize\fR, FILE *\fIstream\fR);
.br
char *\fBgets\fR (char *\fIstr\fR);
.SH DESCRIPTION
The
.BR fgets
function
reads at most one less than the number of characters specified by
.I size
from the given
.I stream
and stores them in the string
.IR str .
Reading stops when a newline character is found,
at end-of-file or error.
The newline, if any, is retained.
If any characters are read and there is no error, a
.BR \e0
character is appended to end the string.
.LP
The
.BR gets
function
is equivalent to
.BR fgets
with an infinite
.I size
and a
.I stream
of
.IR stdin ,
except that the newline character (if any) is not stored in the string.
It is the caller's responsibility to ensure that the input line,
if any, is sufficiently short to fit in the string.
.SH RETURN VALUES
.LP
Upon successful completion,
.BR fgets
and
.BR gets
return
a pointer to the string.
If end-of-file occurs before any characters are read,
they return
.BR NULL
and the buffer contents is unchanged.
If an error occurs,
they return
.BR NULL
and the buffer contents is indeterminate.
The
.BR fgets
and
.BR gets
functions
do not distinguish between end-of-file and error, and callers must use
.BR feof (3)
and
.BR ferror (3)
to determine which occurred.
.SH ERRORS
.RS
.IP \fBEBADF\fR
The given
.I stream
is not a readable stream.
.RE
.LP
The function
.BR fgets
may also fail and set
.IR errno
for any of the errors specified for the routines
.BR fflush (3),
.BR fstat (2),
.BR read (2),
or
.BR malloc (3).
.LP
The function
.BR gets
may also fail and set
.IR errno
for any of the errors specified for the routine
.BR getchar (3).
.SH SEE ALSO
.BR feof (3),
.BR ferror (3),
.BR fgetln (3)
.SH STANDARDS
The functions
.BR fgets
and
.BR gets
conform to ANSI/C.
.SH BUGS
Since it is usually impossible to ensure that the next input line
is less than some arbitrary length, and because overflowing the
input buffer is almost invariably a security violation, programs
should
.IR NEVER
use
.BR gets .
The
.BR gets
function
exists purely to conform to ANSI/C.