xref: /freebsd/lib/libc/stdio/fgets.3 (revision 7c43148a974877188a930e4078a164f83da8e652)
1.\" Copyright (c) 1990, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.Dd April 2, 2022
33.Dt FGETS 3
34.Os
35.Sh NAME
36.Nm fgets ,
37.Nm gets_s
38.Nd get a line from a stream
39.Sh LIBRARY
40.Lb libc
41.Sh SYNOPSIS
42.In stdio.h
43.Ft char *
44.Fn fgets "char * restrict str" "int size" "FILE * restrict stream"
45.Ft char *
46.Fn gets_s "char *str" "rsize_t size"
47.Sh DESCRIPTION
48The
49.Fn fgets
50function
51reads at most one less than the number of characters specified by
52.Fa size
53from the given
54.Fa stream
55and stores them in the string
56.Fa str .
57Reading stops when a newline character is found,
58at end-of-file or error.
59The newline, if any, is retained.
60If any characters are read and there is no error, a
61.Ql \e0
62character is appended to end the string.
63.Pp
64The
65.Fn gets_s
66function
67is equivalent to
68.Fn fgets
69with a
70.Fa stream
71of
72.Dv stdin ,
73except that the newline character (if any) is not stored in the string.
74.Pp
75The
76.Fn gets
77function
78was unsafe and is no longer available.
79.Sh RETURN VALUES
80Upon successful completion,
81.Fn fgets
82and
83.Fn gets_s
84return
85a pointer to the string.
86If end-of-file occurs before any characters are read,
87they return
88.Dv NULL
89and the buffer contents remain unchanged.
90If an error occurs,
91they return
92.Dv NULL
93and the buffer contents are indeterminate.
94The
95.Fn fgets
96and
97.Fn gets_s
98functions
99do not distinguish between end-of-file and error, and callers must use
100.Xr feof 3
101and
102.Xr ferror 3
103to determine which occurred.
104.Sh ERRORS
105.Bl -tag -width Er
106.It Bq Er EBADF
107The given
108.Fa stream
109is not a readable stream.
110.El
111.Pp
112The function
113.Fn fgets
114may also fail and set
115.Va errno
116for any of the errors specified for the routines
117.Xr fflush 3 ,
118.Xr fstat 2 ,
119.Xr read 2 ,
120or
121.Xr malloc 3 .
122.Pp
123The function
124.Fn gets_s
125may also fail and set
126.Va errno
127for any of the errors specified for the routine
128.Xr getchar 3 .
129.Sh SEE ALSO
130.Xr feof 3 ,
131.Xr ferror 3 ,
132.Xr fgetln 3 ,
133.Xr fgetws 3 ,
134.Xr getline 3
135.Sh STANDARDS
136The
137.Fn fgets
138function conforms to
139.St -isoC-99 .
140.Fn gets_s
141conforms to
142.St -isoC-2011
143K.3.7.4.1.
144.Fn gets
145has been removed from
146.St -isoC-2011 .
147.Sh HISTORY
148The functions
149.Fn fgets
150and
151.Fn gets
152first appeared in
153.At v7 .
154