xref: /openbsd/lib/libc/stdio/fgets.3 (revision 133306f0)
1.\"	$OpenBSD: fgets.3,v 1.11 2000/12/24 00:30:58 aaron Exp $
2.\"
3.\" Copyright (c) 1990, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
9.\"
10.\" Redistribution and use in source and binary forms, with or without
11.\" modification, are permitted provided that the following conditions
12.\" are met:
13.\" 1. Redistributions of source code must retain the above copyright
14.\"    notice, this list of conditions and the following disclaimer.
15.\" 2. Redistributions in binary form must reproduce the above copyright
16.\"    notice, this list of conditions and the following disclaimer in the
17.\"    documentation and/or other materials provided with the distribution.
18.\" 3. All advertising materials mentioning features or use of this software
19.\"    must display the following acknowledgement:
20.\"	This product includes software developed by the University of
21.\"	California, Berkeley and its contributors.
22.\" 4. Neither the name of the University nor the names of its contributors
23.\"    may be used to endorse or promote products derived from this software
24.\"    without specific prior written permission.
25.\"
26.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36.\" SUCH DAMAGE.
37.\"
38.Dd June 4, 1993
39.Dt FGETS 3
40.Os
41.Sh NAME
42.Nm fgets ,
43.Nm gets
44.Nd get a line from a stream
45.Sh SYNOPSIS
46.Fd #include <stdio.h>
47.Ft char *
48.Fn fgets "char *str" "int size" "FILE *stream"
49.Ft char *
50.Fn gets "char *str"
51.Sh DESCRIPTION
52The
53.Fn fgets
54function reads at most one less than the number of characters specified by
55.Xr size
56from the given
57.Fa stream
58and stores them in the string
59.Fa str .
60Reading stops when a newline character is found,
61at end-of-file or error.
62The newline, if any, is retained.
63In any case a
64.Ql \e0
65character is appended to end the string.
66.Pp
67The
68.Fn gets
69function is equivalent to
70.Fn fgets
71with an infinite
72.Xr size
73and a
74.Fa stream
75of
76.Em stdin ,
77except that the newline character (if any) is not stored in the string.
78It is the caller's responsibility to ensure that the input line,
79if any, is sufficiently short to fit in the string.
80.Sh RETURN VALUES
81Upon successful completion,
82.Fn fgets
83and
84.Fn gets
85return
86a pointer to the string.
87If end-of-file or an error occurs before any characters are read,
88they return
89.Dv NULL .
90The
91.Fn fgets
92and functions
93.Fn gets
94do not distinguish between end-of-file and error, and callers must use
95.Xr feof 3
96and
97.Xr ferror 3
98to determine which occurred.
99.Sh ERRORS
100.Bl -tag -width Er
101.It Bq Er EBADF
102The given
103.Fa stream
104is not a readable stream.
105.El
106.Pp
107The function
108.Fn fgets
109may also fail and set
110.Va errno
111for any of the errors specified for the routines
112.Xr fflush 3 ,
113.Xr fstat 2 ,
114.Xr read 2 ,
115or
116.Xr malloc 3 .
117.Pp
118The function
119.Fn gets
120may also fail and set
121.Va errno
122for any of the errors specified for the routine
123.Xr getchar 3 .
124.Sh CAVEATS
125The following bit of code illustrates a case where the programmer assumes a
126string is too long if it does not contain a newline:
127.Bd -literal
128	char buf[1024], *p;
129
130	while (fgets(buf, sizeof(buf), fp)) {
131		if (!(p = strchr(buf, '\en'))) {
132			fprintf(stderr, "input line too long.\en");
133			exit(1);
134		}
135		*p = '\e0';
136		printf("%s\en", p);
137	}
138.Ed
139.Pp
140While the error would be true if a line > 1023 characters were read, it would
141be false in two other cases:
142.Bl -enum -offset indent
143.It
144If the last line in a file does not contain a newline, the string returned by
145.Fn fgets
146will not contain a newline either.
147Thus
148.Fn strchr
149will return
150.Dv NULL
151and the program will terminate, even if the line was valid.
152.It
153All C string functions, including
154.Fn strchr ,
155correctly assume the end of the string is represented by a null
156.Pq Sq \e0
157character.
158If the first character of a line returned by
159.Fn fgets
160were null,
161.Fn strchr
162would immediately return without considering the rest of the returned text
163which may indeed include a newline.
164.El
165.Pp
166Consider using
167.Xr fgetln 3
168instead when dealing with untrusted input.
169.Sh SEE ALSO
170.Xr feof 3 ,
171.Xr ferror 3 ,
172.Xr fgetln 3
173.Sh STANDARDS
174The functions
175.Fn fgets
176and
177.Fn gets
178conform to
179.St -ansiC .
180.Sh BUGS
181Since it is usually impossible to ensure that the next input line
182is less than some arbitrary length, and because overflowing the
183input buffer is almost invariably a security violation, programs
184should
185.Em NEVER
186use
187.Fn gets .
188The
189.Fn gets
190function exists purely to conform to
191.St -ansiC .
192