From owner-freebsd-current@FreeBSD.ORG Tue Mar 31 07:10:47 2009 Return-Path: Delivered-To: current@FreeBSD.ORG Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 96DD71065670; Tue, 31 Mar 2009 07:10:47 +0000 (UTC) (envelope-from das@FreeBSD.ORG) Received: from zim.MIT.EDU (ZIM.MIT.EDU [18.95.3.101]) by mx1.freebsd.org (Postfix) with ESMTP id 575E48FC12; Tue, 31 Mar 2009 07:10:47 +0000 (UTC) (envelope-from das@FreeBSD.ORG) Received: from zim.MIT.EDU (localhost [127.0.0.1]) by zim.MIT.EDU (8.14.3/8.14.2) with ESMTP id n2V6oO63009937; Tue, 31 Mar 2009 02:50:24 -0400 (EDT) (envelope-from das@FreeBSD.ORG) Received: (from das@localhost) by zim.MIT.EDU (8.14.3/8.14.2/Submit) id n2V6oOHH009936; Tue, 31 Mar 2009 02:50:24 -0400 (EDT) (envelope-from das@FreeBSD.ORG) Date: Tue, 31 Mar 2009 02:50:24 -0400 From: David Schultz To: Joe Marcus Clarke Message-ID: <20090331065024.GA9671@zim.MIT.EDU> Mail-Followup-To: Joe Marcus Clarke , current References: <1238362728.73736.165.camel@shumai.marcuscom.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1238362728.73736.165.camel@shumai.marcuscom.com> Cc: current Subject: Re: getline incompatibility with Linux X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 31 Mar 2009 07:10:47 -0000 On Sun, Mar 29, 2009, Joe Marcus Clarke wrote: > The new getline() function in FreeBSD is not completely compatible with > Linux's implementation. The result is that programs which assume Linux > getline may enter a tight infinite loop. > > According to the Linux getline(3) manpage, getline(3) returns -1 on > error (including EOF). Our implementation returns 0 on EOF. Would it > be possible to return -1 on EOF in our implementation? Good catch; even POSIX says (in its usual roundabout way) that getline() is supposed to return -1 on both error and EOF, and never return 0. Of course POSIX merely inherited this braindeadness from glibc... The following patch should fix it. I'll commit this soonish, but ENOTIME right now. Thanks for pointing this out. Index: getline.3 =================================================================== --- getline.3 (revision 190425) +++ getline.3 (working copy) @@ -24,7 +24,7 @@ .\" .\" $FreeBSD$ .\" -.Dd February 28, 2009 +.Dd March 29, 2009 .Dt GETLINE 3 .Os .Sh NAME @@ -79,7 +79,7 @@ functions return the number of characters written, excluding the terminating .Dv NUL . -The value \-1 is returned if an error occurs. +The value \-1 is returned if an error occurs, or if end-of-file is reached. .Sh EXAMPLES The following code fragment reads lines from a file and writes them to standard output. Index: getdelim.c =================================================================== --- getdelim.c (revision 190425) +++ getdelim.c (working copy) @@ -120,7 +120,6 @@ goto error; } - linelen = 0; if (*linecapp == 0) *linep = NULL; @@ -128,9 +127,12 @@ /* If fp is at EOF already, we just need space for the NUL. */ if (__sferror(fp) || expandtofit(linep, 1, linecapp)) goto error; - goto done; + FUNLOCKFILE(fp); + (*linep)[0] = '\0'; + return (-1); } + linelen = 0; while ((endp = memchr(fp->_p, delim, fp->_r)) == NULL) { if (sappend(linep, &linelen, linecapp, fp->_p, fp->_r)) goto error;