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