1.\" $NetBSD: fgets.3,v 1.14 2002/07/21 19:04:03 mjl 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. All advertising materials mentioning features or use of this software 19.\" must display the following acknowledgement: 20.\" This product includes software developed by the University of 21.\" California, Berkeley and its contributors. 22.\" 4. Neither the name of the University nor the names of its contributors 23.\" may be used to endorse or promote products derived from this software 24.\" without specific prior written permission. 25.\" 26.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36.\" SUCH DAMAGE. 37.\" 38.\" @(#)fgets.3 8.1 (Berkeley) 6/4/93 39.\" 40.Dd June 4, 1993 41.Dt FGETS 3 42.Os 43.Sh NAME 44.Nm fgets , 45.Nm gets 46.Nd get a line from a stream 47.Sh LIBRARY 48.Lb libc 49.Sh SYNOPSIS 50.Fd #include \*[Lt]stdio.h\*[Gt] 51.Ft char * 52.Fn fgets "char * restrict str" "int size" "FILE * restrict stream" 53.Ft char * 54.Fn gets "char *str" 55.Sh DESCRIPTION 56The 57.Fn fgets 58function 59reads at most one less than the number of characters specified by 60.Fa size 61from the given 62.Fa stream 63and stores them in the string 64.Fa str . 65Reading stops when a newline character is found, 66at end-of-file or error. 67The newline, if any, is retained. 68In any case a 69.Ql \e0 70character is appended to end the string. 71.Pp 72The 73.Fn gets 74function 75is equivalent to 76.Fn fgets 77with an infinite 78.Fa size 79and a 80.Fa stream 81of 82.Em stdin , 83except that the newline character (if any) is not stored in the string. 84It is the caller's responsibility to ensure that the input line, 85if any, is sufficiently short to fit in the string. 86.Sh RETURN VALUES 87Upon successful completion, 88.Fn fgets 89and 90.Fn gets 91return 92a pointer to the string. 93If end-of-file or an error occurs before any characters are read, 94they return 95.Dv NULL . 96The 97.Fn fgets 98and 99functions 100.Fn gets 101do not distinguish between end-of-file and error, and callers must use 102.Xr feof 3 103and 104.Xr ferror 3 105to determine which occurred. 106.Sh ERRORS 107.Bl -tag -width Er 108.It Bq Er EBADF 109The given 110.Fa stream 111is not a readable stream. 112.El 113.Pp 114The function 115.Fn fgets 116may also fail and set 117.Va errno 118for any of the errors specified for the routines 119.Xr fflush 3 , 120.Xr fstat 2 , 121.Xr read 2 , 122or 123.Xr malloc 3 . 124.Pp 125The function 126.Fn gets 127may also fail and set 128.Va errno 129for any of the errors specified for the routine 130.Xr getchar 3 . 131.Sh SEE ALSO 132.Xr feof 3 , 133.Xr ferror 3 , 134.Xr fgetln 3 135.Sh STANDARDS 136The functions 137.Fn fgets 138and 139.Fn gets 140conform to 141.St -ansiC . 142.Sh CAVEATS 143The following bit of code illustrates a case where the programmer assumes a 144string is too long if it does not contain a newline: 145.Bd -literal 146 char buf[1024], *p; 147 148 while (fgets(buf, sizeof(buf), fp) != NULL) { 149 if ((p = strchr(buf, '\en')) == NULL) { 150 fprintf(stderr, "input line too long.\en"); 151 exit(1); 152 } 153 *p = '\e0'; 154 printf("%s\en", buf); 155 } 156.Ed 157.Pp 158While the error would be true if a line > 1023 characters were read, it would 159be false in two other cases: 160.Bl -enum -offset indent 161.It 162If the last line in a file does not contain a newline, the string returned by 163.Fn fgets 164will not contain a newline either. 165Thus 166.Fn strchr 167will return 168.Dv NULL 169and the program will terminate, even if the line was valid. 170.It 171All C string functions, including 172.Fn strchr , 173correctly assume the end of the string is represented by a null 174.Pq Sq \e0 175character. 176If the first character of a line returned by 177.Fn fgets 178were null, 179.Fn strchr 180would immediately return without considering the rest of the returned text 181which may indeed include a newline. 182.El 183.Pp 184Consider using 185.Xr fgetln 3 186instead when dealing with untrusted input. 187.Sh SECURITY CONSIDERATIONS 188Since it is usually impossible to ensure that the next input line 189is less than some arbitrary length, and because overflowing the 190input buffer is almost invariably a security violation, programs 191should 192.Em NEVER 193use 194.Fn gets . 195The 196.Fn gets 197function 198exists purely to conform to 199.St -ansiC . 200