1.\" $NetBSD: fgets.3,v 1.22 2010/05/13 18:38:24 jruoho 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.\" @(#)fgets.3 8.1 (Berkeley) 6/4/93 35.\" 36.Dd May 13, 2010 37.Dt FGETS 3 38.Os 39.Sh NAME 40.Nm fgets , 41.Nm gets 42.Nd get a line from a stream 43.Sh LIBRARY 44.Lb libc 45.Sh SYNOPSIS 46.In stdio.h 47.Ft char * 48.Fn fgets "char * restrict str" "int size" "FILE * restrict stream" 49.Ft char * 50.Fn gets "char *str" 51.Sh DESCRIPTION 52The 53.Fn fgets 54function 55reads at most one less than the number of characters specified by 56.Fa size 57from the given 58.Fa stream 59and stores them in the string 60.Fa str . 61Reading stops when a newline character is found, 62at end-of-file or error. 63The newline, if any, is retained, and a 64.Ql \e0 65character is appended to end the string. 66.Pp 67The 68.Fn gets 69function 70is equivalent to 71.Fn fgets 72with an infinite 73.Fa size 74and a 75.Fa stream 76of 77.Em stdin , 78except that the newline character (if any) is not stored in the string. 79It is the caller's responsibility to ensure that the input line, 80if any, is sufficiently short to fit in the string. 81.Sh RETURN VALUES 82Upon successful completion, 83.Fn fgets 84and 85.Fn gets 86return 87a pointer to the string. 88If end-of-file or an error occurs before any characters are read, 89they return 90.Dv NULL . 91The 92.Fn fgets 93and 94.Fn gets 95functions 96do not distinguish between end-of-file and error, and callers must use 97.Xr feof 3 98and 99.Xr ferror 3 100to determine which occurred. 101.Sh ERRORS 102.Bl -tag -width Er 103.It Bq Er EBADF 104The given 105.Fa stream 106is not a readable stream. 107.El 108.Pp 109The function 110.Fn fgets 111may also fail and set 112.Va errno 113for any of the errors specified for the routines 114.Xr fflush 3 , 115.Xr fstat 2 , 116.Xr read 2 , 117or 118.Xr malloc 3 . 119.Pp 120The function 121.Fn gets 122may also fail and set 123.Va errno 124for any of the errors specified for the routine 125.Xr getchar 3 . 126.Sh SEE ALSO 127.Xr feof 3 , 128.Xr ferror 3 , 129.Xr fgetln 3 130.Sh STANDARDS 131The functions 132.Fn fgets 133and 134.Fn gets 135conform to 136.St -ansiC 137and 138.St -p1003.1-2001 . 139The 140.St -p1003.1-2008 141revision marked 142.Fn gets 143as obsolescent. 144.Sh CAVEATS 145The following bit of code illustrates a case where the programmer assumes a 146string is too long if it does not contain a newline: 147.Bd -literal 148 char buf[1024], *p; 149 150 while (fgets(buf, sizeof(buf), fp) != NULL) { 151 if ((p = strchr(buf, '\en')) == NULL) { 152 fprintf(stderr, "input line too long.\en"); 153 exit(1); 154 } 155 *p = '\e0'; 156 printf("%s\en", buf); 157 } 158.Ed 159.Pp 160While the error would be true if a line longer than 1023 characters 161were read, it would be false in two other cases: 162.Bl -enum -offset indent 163.It 164If the last line in a file does not contain a newline, the string returned by 165.Fn fgets 166will not contain a newline either. 167Thus 168.Fn strchr 169will return 170.Dv NULL 171and the program will terminate, even if the line was valid. 172.It 173All C string functions, including 174.Fn strchr , 175correctly assume the end of the string is represented by a null 176.Pq Sq \e0 177character. 178If the first character of a line returned by 179.Fn fgets 180were null, 181.Fn strchr 182would immediately return without considering the rest of the returned text 183which may indeed include a newline. 184.El 185.Pp 186Consider using 187.Xr fgetln 3 188instead when dealing with untrusted input. 189.Sh SECURITY CONSIDERATIONS 190Since it is usually impossible to ensure that the next input line 191is less than some arbitrary length, and because overflowing the 192input buffer is almost invariably a security violation, programs 193should 194.Em NEVER 195use 196.Fn gets . 197The 198.Fn gets 199function 200exists purely to conform to 201.St -ansiC . 202