1.\" $OpenBSD: fgets.3,v 1.19 2003/06/02 20:18:37 millert 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.Dd June 4, 1993 35.Dt FGETS 3 36.Os 37.Sh NAME 38.Nm fgets , 39.Nm gets 40.Nd get a line from a stream 41.Sh SYNOPSIS 42.Fd #include <stdio.h> 43.Ft char * 44.Fn fgets "char *str" "int size" "FILE *stream" 45.Ft char * 46.Fn gets "char *str" 47.Sh DESCRIPTION 48The 49.Fn fgets 50function reads at most one less than the number of characters specified by 51.Ar size 52from the given 53.Fa stream 54and stores them in the string 55.Fa str . 56Reading stops when a newline character is found, 57at end-of-file, or on error. 58The newline, if any, is retained. 59In any case, a 60.Ql \e0 61character is appended to end the string. 62.Pp 63The 64.Fn gets 65function is equivalent to 66.Fn fgets 67with an infinite 68.Ar size 69and a 70.Fa stream 71of 72.Em stdin , 73except that the newline character (if any) is not stored in the string. 74It is the caller's responsibility to ensure that the input line, 75if any, is sufficiently short to fit in the string. 76.Sh RETURN VALUES 77Upon successful completion, 78.Fn fgets 79and 80.Fn gets 81return 82a pointer to the string. 83If end-of-file or an error occurs before any characters are read, 84they return 85.Dv NULL . 86The 87.Fn fgets 88and functions 89.Fn gets 90do not distinguish between end-of-file and error, and callers must use 91.Xr feof 3 92and 93.Xr ferror 3 94to determine which occurred. 95Whether 96.Fn fgets 97can possibly fail with a 98.Ar size 99argument of 1 is implementation-dependent. 100On 101.Ox , 102.Fn fgets 103will never return 104.Dv NULL 105when 106.Ar size 107is 1. 108.Sh ERRORS 109.Bl -tag -width Er 110.It Bq Er EBADF 111The given 112.Fa stream 113is not a readable stream. 114.El 115.Pp 116The function 117.Fn fgets 118may also fail and set 119.Va errno 120for any of the errors specified for the routines 121.Xr fflush 3 , 122.Xr fstat 2 , 123.Xr read 2 , 124or 125.Xr malloc 3 . 126.Pp 127The function 128.Fn gets 129may also fail and set 130.Va errno 131for any of the errors specified for the routine 132.Xr getchar 3 . 133.Sh SEE ALSO 134.Xr feof 3 , 135.Xr ferror 3 , 136.Xr fgetln 3 137.Sh STANDARDS 138The functions 139.Fn fgets 140and 141.Fn gets 142conform to 143.St -ansiC . 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 > 1023 characters were read, it would 161be 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 BUGS 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 exists purely to conform to 200.St -ansiC . 201