1.\" $NetBSD: fgets.3,v 1.17 2003/08/07 16:43:23 agc 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. 64In any case a 65.Ql \e0 66character is appended to end the string. 67.Pp 68The 69.Fn gets 70function 71is equivalent to 72.Fn fgets 73with an infinite 74.Fa size 75and a 76.Fa stream 77of 78.Em stdin , 79except that the newline character (if any) is not stored in the string. 80It is the caller's responsibility to ensure that the input line, 81if any, is sufficiently short to fit in the string. 82.Sh RETURN VALUES 83Upon successful completion, 84.Fn fgets 85and 86.Fn gets 87return 88a pointer to the string. 89If end-of-file or an error occurs before any characters are read, 90they return 91.Dv NULL . 92The 93.Fn fgets 94and 95functions 96.Fn gets 97do not distinguish between end-of-file and error, and callers must use 98.Xr feof 3 99and 100.Xr ferror 3 101to determine which occurred. 102.Sh ERRORS 103.Bl -tag -width Er 104.It Bq Er EBADF 105The given 106.Fa stream 107is not a readable stream. 108.El 109.Pp 110The function 111.Fn fgets 112may also fail and set 113.Va errno 114for any of the errors specified for the routines 115.Xr fflush 3 , 116.Xr fstat 2 , 117.Xr read 2 , 118or 119.Xr malloc 3 . 120.Pp 121The function 122.Fn gets 123may also fail and set 124.Va errno 125for any of the errors specified for the routine 126.Xr getchar 3 . 127.Sh SEE ALSO 128.Xr feof 3 , 129.Xr ferror 3 , 130.Xr fgetln 3 131.Sh STANDARDS 132The functions 133.Fn fgets 134and 135.Fn gets 136conform to 137.St -ansiC . 138.Sh CAVEATS 139The following bit of code illustrates a case where the programmer assumes a 140string is too long if it does not contain a newline: 141.Bd -literal 142 char buf[1024], *p; 143 144 while (fgets(buf, sizeof(buf), fp) != NULL) { 145 if ((p = strchr(buf, '\en')) == NULL) { 146 fprintf(stderr, "input line too long.\en"); 147 exit(1); 148 } 149 *p = '\e0'; 150 printf("%s\en", buf); 151 } 152.Ed 153.Pp 154While the error would be true if a line > 1023 characters were read, it would 155be false in two other cases: 156.Bl -enum -offset indent 157.It 158If the last line in a file does not contain a newline, the string returned by 159.Fn fgets 160will not contain a newline either. 161Thus 162.Fn strchr 163will return 164.Dv NULL 165and the program will terminate, even if the line was valid. 166.It 167All C string functions, including 168.Fn strchr , 169correctly assume the end of the string is represented by a null 170.Pq Sq \e0 171character. 172If the first character of a line returned by 173.Fn fgets 174were null, 175.Fn strchr 176would immediately return without considering the rest of the returned text 177which may indeed include a newline. 178.El 179.Pp 180Consider using 181.Xr fgetln 3 182instead when dealing with untrusted input. 183.Sh SECURITY CONSIDERATIONS 184Since it is usually impossible to ensure that the next input line 185is less than some arbitrary length, and because overflowing the 186input buffer is almost invariably a security violation, programs 187should 188.Em NEVER 189use 190.Fn gets . 191The 192.Fn gets 193function 194exists purely to conform to 195.St -ansiC . 196