xref: /netbsd-src/lib/libc/stdio/fgets.3 (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1.\"	$NetBSD: fgets.3,v 1.6 1997/01/17 02:38:19 perry 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.\"     @(#)fgets.3	8.1 (Berkeley) 6/4/93
39.\"
40.Dd June 4, 1993
41.Dt FGETS 3
42.Os
43.Sh NAME
44.Nm fgets ,
45.Nm gets
46.Nd get a line from a stream
47.Sh SYNOPSIS
48.Fd #include <stdio.h>
49.Ft char *
50.Fn fgets "char *str" "int size" "FILE *stream"
51.Ft char *
52.Fn gets "char *str"
53.Sh DESCRIPTION
54The
55.Fn fgets
56function
57reads at most one less than the number of characters specified by
58.Xr size
59from the given
60.Fa stream
61and stores them in the string
62.Fa str .
63Reading stops when a newline character is found,
64at end-of-file or error.
65The newline, if any, is retained.
66In any case a
67.Ql \e0
68character is appended to end the string.
69.Pp
70The
71.Fn gets
72function
73is equivalent to
74.Fn fgets
75with an infinite
76.Xr size
77and a
78.Fa stream
79of
80.Em stdin ,
81except that the newline character (if any) is not stored in the string.
82It is the caller's responsibility to ensure that the input line,
83if any, is sufficiently short to fit in the string.
84.Sh RETURN VALUES
85.Pp
86Upon successful completion,
87.Fn fgets
88and
89.Fn gets
90return
91a pointer to the string.
92If end-of-file or an error occurs before any characters are read,
93they return
94.Dv NULL.
95The
96.Fn fgets
97and
98functions
99.Fn gets
100do not distinguish between end-of-file and error, and callers must use
101.Xr feof 3
102and
103.Xr ferror 3
104to determine which occurred.
105.Sh ERRORS
106.Bl -tag -width Er
107.It Bq Er EBADF
108The given
109.Fa stream
110is not a readable stream.
111.El
112.Pp
113The function
114.Fn fgets
115may also fail and set
116.Va errno
117for any of the errors specified for the routines
118.Xr fflush 3 ,
119.Xr fstat 2 ,
120.Xr read 2 ,
121or
122.Xr malloc 3 .
123.Pp
124The function
125.Fn gets
126may also fail and set
127.Va errno
128for any of the errors specified for the routine
129.Xr getchar 3 .
130.Sh SEE ALSO
131.Xr feof 3 ,
132.Xr ferror 3 ,
133.Xr fgetln 3
134.Sh STANDARDS
135The functions
136.Fn fgets
137and
138.Fn gets
139conform to
140.St -ansiC .
141.Sh BUGS
142Since it is usually impossible to ensure that the next input line
143is less than some arbitrary length, and because overflowing the
144input buffer is almost invariably a security violation, programs
145should
146.Em NEVER
147use
148.Fn gets .
149The
150.Fn gets
151function
152exists purely to conform to
153.St -ansiC .
154