man page pass, minor cleanups
[unix-history] / usr / src / lib / libc / stdio / fgets.3
CommitLineData
639aed4c 1.\" Copyright (c) 1990 The Regents of the University of California.
952018c5 2.\" All rights reserved.
2e146d0b 3.\"
639aed4c
KB
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek.
6.\"
91cff1e1 7.\" %sccs.include.redist.man%
952018c5 8.\"
1382c0b2 9.\" @(#)fgets.3 6.7 (Berkeley) %G%
952018c5
KB
10.\"
11.TH FGETS 3 ""
639aed4c 12.UC 7
2e146d0b 13.SH NAME
639aed4c 14fgets, gets \- get a line from a stream
2e146d0b 15.SH SYNOPSIS
952018c5 16.nf
639aed4c
KB
17.ft B
18#include <stdio.h>
19
20char *
21fgets(char *str, size_t size, FILE *stream);
952018c5 22
639aed4c
KB
23char *
24gets(char *str);
25.ft R
952018c5 26.fi
2e146d0b 27.SH DESCRIPTION
639aed4c
KB
28.I Fgets
29reads at most one less than the number of characters specified by
30.IR size
31from the given
2e146d0b 32.I stream
639aed4c
KB
33into the string
34.IR str .
35Reading stops when a newline character is found,
36at end-of-file or error.
37The newline, if any, is retained.
38In any case a '\e0' character is appended to end the string.
952018c5 39.PP
639aed4c
KB
40.I Gets
41is equivalent to
952018c5 42.I fgets
639aed4c
KB
43with an infinite
44.I size
45and a
46.I stream
47of
48.BR stdin ,
49except that the newline character (if any) is not stored in the string.
50It is the caller's responsibility to ensure that the input line,
51if any, is sufficiently short to fit in the string.
639aed4c
KB
52.SH "RETURN VALUE"
53.PP
54Upon successful completion,
2e146d0b 55.I fgets
639aed4c
KB
56and
57.I gets
58return
59.IR s .
60If end-of-file or an error occurs before any characters are read,
61they return NULL.
1382c0b2
KB
62.I Fgets
63and
64.I gets
65do not distinguish between end-of-file and error, and callers must use
66.IR feof (3)
639aed4c 67and
1382c0b2 68.I ferror (3)
639aed4c
KB
69to determine which occurred.
70.SH ERRORS
71.TP 15
72[EBADF]
73.I Stream
74is not a readable stream.
75.PP
76.I Fgets
77may also fail and set
78.I errno
79for any of the errors specified for the routines
80.IR fflush (3),
81.IR fstat (2),
82.IR read (2),
83or
84.IR malloc (3).
85.PP
86.I Gets
87may also fail and set
88.I errno
89for any of the errors specified for the routine
90.IR getchar (3).
78a22176
KB
91.SH "SEE ALSO"
92feof(3), ferror(3), fgetline(3)
93.SH STANDARDS
94.I Fgets
95and
96.I gets
97conform to ANSI X3.159-1989 (``ANSI C'').
639aed4c
KB
98.SH BUGS
99Since it is usually impossible to ensure that the next input line
100is less than some arbitrary length, and because overflowing the
101input buffer is almost invariably a security violation, programs
102should
103.B NEVER
104use
105.IR gets .
106.I Gets
107exists purely to conform to ANSI X3.159-1989.