xref: /openbsd-src/lib/libc/stdio/fgets.3 (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1.\"	$OpenBSD: fgets.3,v 1.28 2007/09/07 05:17:59 cloder 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. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.Dd $Mdocdate: September 7 2007 $
35.Dt FGETS 3
36.Os
37.Sh NAME
38.Nm fgets ,
39.Nm gets
40.Nd get a line from a stream
41.Sh SYNOPSIS
42.Fd #include <stdio.h>
43.Ft char *
44.Fn fgets "char *str" "int size" "FILE *stream"
45.Ft char *
46.Fn gets "char *str"
47.Sh DESCRIPTION
48The
49.Fn fgets
50function reads at most
51.Ar size Ns \-1
52characters from the given
53.Fa stream
54and stores them in the string
55.Fa str .
56Reading stops when a newline character is found,
57at end-of-file, or on error.
58The newline, if any, is retained.
59The string will be NUL-terminated if
60.Fn fgets
61succeeds; otherwise the contents of
62.Fa str
63are undefined.
64.Pp
65The
66.Fn gets
67function is equivalent to
68.Fn fgets
69with an infinite
70.Ar size
71and a
72.Fa stream
73of
74.Em stdin ,
75except that the newline character (if any) is not stored in the string.
76It is the caller's responsibility to ensure that the input line,
77if any, is sufficiently short to fit in the string.
78.Sh RETURN VALUES
79Upon successful completion,
80.Fn fgets
81and
82.Fn gets
83return
84a pointer to the string.
85If end-of-file or an error occurs before any characters are read,
86they return
87.Dv NULL .
88The
89.Fn fgets
90and
91.Fn gets
92functions
93do not distinguish between end-of-file and error, and callers must use
94.Xr feof 3
95and
96.Xr ferror 3
97to determine which occurred.
98Whether
99.Fn fgets
100can possibly fail with a
101.Ar size
102argument of 1 is implementation-dependent.
103On
104.Ox ,
105.Fn fgets
106will never return
107.Dv NULL
108when
109.Ar size
110is 1.
111.Sh ERRORS
112.Bl -tag -width Er
113.It Bq Er EBADF
114The given
115.Fa stream
116is not a readable stream.
117.El
118.Pp
119The function
120.Fn fgets
121may also fail and set
122.Va errno
123for any of the errors specified for the routines
124.Xr fflush 3 ,
125.Xr fstat 2 ,
126.Xr read 2 ,
127or
128.Xr malloc 3 .
129.Pp
130The function
131.Fn gets
132may also fail and set
133.Va errno
134for any of the errors specified for the routine
135.Xr getchar 3 .
136.Sh SEE ALSO
137.Xr feof 3 ,
138.Xr ferror 3 ,
139.Xr fgetln 3
140.Sh STANDARDS
141The functions
142.Fn fgets
143and
144.Fn gets
145conform to
146.St -ansiC .
147.Sh CAVEATS
148The following bit of code illustrates a case where the programmer assumes a
149string is too long if it does not contain a newline:
150.Bd -literal -offset indent
151char buf[1024], *p;
152
153while (fgets(buf, sizeof(buf), fp) != NULL) {
154	if ((p = strchr(buf, '\en')) == NULL) {
155		fprintf(stderr, "input line too long.\en");
156		exit(1);
157	}
158	*p = '\e0';
159	printf("%s\en", buf);
160}
161.Ed
162.Pp
163While the error would be true if a line \*(Gt 1023 characters were read,
164it would be false in two other cases:
165.Bl -enum -offset indent
166.It
167If the last line in a file does not contain a newline, the string returned by
168.Fn fgets
169will not contain a newline either.
170Thus
171.Fn strchr
172will return
173.Dv NULL
174and the program will terminate, even if the line was valid.
175.It
176All C string functions, including
177.Fn strchr ,
178correctly assume the end of the string is represented by a NUL
179.Pq Sq \e0
180character.
181If the first character of a line returned by
182.Fn fgets
183were NUL,
184.Fn strchr
185would immediately return without considering the rest of the returned text
186which may indeed include a newline.
187.El
188.Pp
189Consider using
190.Xr fgetln 3
191instead when dealing with untrusted input.
192.Pp
193It is erroneous to assume that
194.Fn fgets
195never returns an empty string when successful.
196If a line starts with the NUL character, fgets will store the NUL and
197continue reading until it encounters a newline or end-of-file.
198This will result in an empty string being returned.
199The following bit of code illustrates a case where the programmer assumes
200the string cannot be zero length.
201.Bd -literal -offset indent
202char buf[1024];
203
204if (fgets(buf, sizeof(buf), fp) != NULL) {
205	/* WRONG */
206	if (buf[strlen(buf) - 1] == '\en')
207		buf[strlen(buf) - 1] = '\e0';
208}
209.Ed
210.Pp
211If
212.Fn strlen
213returns 0, the index into the buffer becomes \-1.
214One way to concisely and correctly trim a newline is shown below.
215.Bd -literal -offset indent
216char buf[1024];
217
218if (fgets(buf, sizeof(buf), fp) != NULL)
219	buf[strcspn(buf, "\en")] = '\e0';
220.Ed
221.Sh BUGS
222Since it is usually impossible to ensure that the next input line
223is less than some arbitrary length, and because overflowing the
224input buffer is almost invariably a security violation, programs
225should
226.Em NEVER
227use
228.Fn gets .
229The
230.Fn gets
231function exists purely to conform to
232.St -ansiC .
233