1.\" $NetBSD: fgets.3,v 1.19 2003/12/09 21:40:54 grant 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 June 4, 1993 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 . 137.Sh CAVEATS 138The following bit of code illustrates a case where the programmer assumes a 139string is too long if it does not contain a newline: 140.Bd -literal 141 char buf[1024], *p; 142 143 while (fgets(buf, sizeof(buf), fp) != NULL) { 144 if ((p = strchr(buf, '\en')) == NULL) { 145 fprintf(stderr, "input line too long.\en"); 146 exit(1); 147 } 148 *p = '\e0'; 149 printf("%s\en", buf); 150 } 151.Ed 152.Pp 153While the error would be true if a line > 1023 characters were read, it would 154be false in two other cases: 155.Bl -enum -offset indent 156.It 157If the last line in a file does not contain a newline, the string returned by 158.Fn fgets 159will not contain a newline either. 160Thus 161.Fn strchr 162will return 163.Dv NULL 164and the program will terminate, even if the line was valid. 165.It 166All C string functions, including 167.Fn strchr , 168correctly assume the end of the string is represented by a null 169.Pq Sq \e0 170character. 171If the first character of a line returned by 172.Fn fgets 173were null, 174.Fn strchr 175would immediately return without considering the rest of the returned text 176which may indeed include a newline. 177.El 178.Pp 179Consider using 180.Xr fgetln 3 181instead when dealing with untrusted input. 182.Sh SECURITY CONSIDERATIONS 183Since it is usually impossible to ensure that the next input line 184is less than some arbitrary length, and because overflowing the 185input buffer is almost invariably a security violation, programs 186should 187.Em NEVER 188use 189.Fn gets . 190The 191.Fn gets 192function 193exists purely to conform to 194.St -ansiC . 195