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 December 6, 2024 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.Fd #define __STDC_WANT_LIB_EXT1__ 1 46.Ft char * 47.Fn gets_s "char *str" "rsize_t size" 48.Sh DESCRIPTION 49The 50.Fn fgets 51function 52reads at most one less than the number of characters specified by 53.Fa size 54from the given 55.Fa stream 56and stores them in the string 57.Fa str . 58Reading stops when a newline character is found, 59at end-of-file or error. 60The newline, if any, is retained. 61If any characters are read and there is no error, a 62.Ql \e0 63character is appended to end the string. 64.Pp 65The 66.Fn gets_s 67function 68is equivalent to 69.Fn fgets 70with a 71.Fa stream 72of 73.Dv stdin , 74except that the newline character (if any) is not stored in the string. 75.Pp 76The 77.Fn gets 78function 79was unsafe and is no longer available. 80.Sh RETURN VALUES 81Upon successful completion, 82.Fn fgets 83and 84.Fn gets_s 85return 86a pointer to the string. 87If end-of-file occurs before any characters are read, 88they return 89.Dv NULL 90and the buffer contents remain unchanged. 91If an error occurs, 92they return 93.Dv NULL 94and the buffer contents are indeterminate. 95The 96.Fn fgets 97and 98.Fn gets_s 99functions 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_s 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.Xr fgetws 3 , 135.Xr getline 3 136.Sh STANDARDS 137The 138.Fn fgets 139function conforms to 140.St -isoC-99 . 141.Fn gets_s 142conforms to 143.St -isoC-2011 144K.3.7.4.1. 145.Fn gets 146has been removed from 147.St -isoC-2011 . 148.Sh HISTORY 149The functions 150.Fn fgets 151and 152.Fn gets 153first appeared in 154.At v7 . 155