Make 'line' writable (and save 4 bytes).
[unix-history] / usr / src / lib / libc / stdio / fgets.3
.\" Copyright (c) 1990 The Regents of the University of California.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" Chris Torek.
.\"
.\" %sccs.include.redist.man%
.\"
.\" @(#)fgets.3 6.6 (Berkeley) %G%
.\"
.TH FGETS 3 ""
.UC 7
.SH NAME
fgets, gets \- get a line from a stream
.SH SYNOPSIS
.nf
.ft B
#include <stdio.h>
char *
fgets(char *str, size_t size, FILE *stream);
char *
gets(char *str);
.ft R
.fi
.SH DESCRIPTION
.I Fgets
reads at most one less than the number of characters specified by
.IR size
from the given
.I stream
into the string
.IR str .
Reading stops when a newline character is found,
at end-of-file or error.
The newline, if any, is retained.
In any case a '\e0' character is appended to end the string.
.PP
.I Gets
is equivalent to
.I fgets
with an infinite
.I size
and a
.I stream
of
.BR 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 VALUE"
.PP
Upon successful completion,
.I fgets
and
.I gets
return
.IR s .
If end-of-file or an error occurs before any characters are read,
they return NULL.
Callers must use
.I feof
and
.I ferror
to determine which occurred.
.SH ERRORS
.TP 15
[EBADF]
.I Stream
is not a readable stream.
.PP
.I Fgets
may also fail and set
.I errno
for any of the errors specified for the routines
.IR fflush (3),
.IR fstat (2),
.IR read (2),
or
.IR malloc (3).
.PP
.I Gets
may also fail and set
.I errno
for any of the errors specified for the routine
.IR getchar (3).
.SH "SEE ALSO"
feof(3), ferror(3), fgetline(3)
.SH STANDARDS
.I Fgets
and
.I gets
conform to ANSI X3.159-1989 (``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
.B NEVER
use
.IR gets .
.I Gets
exists purely to conform to ANSI X3.159-1989.