From owner-svn-src-all@freebsd.org Thu Jun 18 13:13:05 2020 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 85AF235053A; Thu, 18 Jun 2020 13:13:05 +0000 (UTC) (envelope-from gbe@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 49nj6T330gz4h25; Thu, 18 Jun 2020 13:13:05 +0000 (UTC) (envelope-from gbe@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 63A58A314; Thu, 18 Jun 2020 13:13:05 +0000 (UTC) (envelope-from gbe@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 05IDD56u074036; Thu, 18 Jun 2020 13:13:05 GMT (envelope-from gbe@FreeBSD.org) Received: (from gbe@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 05IDD5ME074035; Thu, 18 Jun 2020 13:13:05 GMT (envelope-from gbe@FreeBSD.org) Message-Id: <202006181313.05IDD5ME074035@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: gbe set sender to gbe@FreeBSD.org using -f From: Gordon Bergling Date: Thu, 18 Jun 2020 13:13:05 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r362326 - head/lib/libc/stdio X-SVN-Group: head X-SVN-Commit-Author: gbe X-SVN-Commit-Paths: head/lib/libc/stdio X-SVN-Commit-Revision: 362326 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 18 Jun 2020 13:13:05 -0000 Author: gbe (doc committer) Date: Thu Jun 18 13:13:04 2020 New Revision: 362326 URL: https://svnweb.freebsd.org/changeset/base/362326 Log: fgetln(3): Add a Caveats Section Reviewed by: yuripv, bcr (mentor) Approved by: bcr (mentror) Obtained from: OpenBSD MFC after: 7 days Differential Revision: https://reviews.freebsd.org/D24916 Modified: head/lib/libc/stdio/fgetln.3 Modified: head/lib/libc/stdio/fgetln.3 ============================================================================== --- head/lib/libc/stdio/fgetln.3 Thu Jun 18 12:29:24 2020 (r362325) +++ head/lib/libc/stdio/fgetln.3 Thu Jun 18 13:13:04 2020 (r362326) @@ -28,7 +28,7 @@ .\" @(#)fgetln.3 8.3 (Berkeley) 4/19/94 .\" $FreeBSD$ .\" -.Dd February 15, 2016 +.Dd June 11, 2020 .Dt FGETLN 3 .Os .Sh NAME @@ -126,3 +126,33 @@ The .Fn fgetln function first appeared in .Bx 4.4 . +.Sh CAVEATS +Since the returned buffer is not a C string (it is not NUL terminated), a +common practice is to replace the newline character with +.Sq \e0 . +However, if the last line in a file does not contain a newline, +the returned text won't contain a newline either. +The following code demonstrates how to deal with this problem by allocating a +temporary buffer: +.Bd -literal + char *buf, *lbuf; + size_t len; + + lbuf = NULL; + while ((buf = fgetln(fp, &len)) != NULL) { + if (buf[len - 1] == '\en') + buf[len - 1] = '\e0'; + else { + /* EOF without EOL, copy and add the NUL */ + if ((lbuf = malloc(len + 1)) == NULL) + err(1, NULL); + memcpy(lbuf, buf, len); + lbuf[len] = '\e0'; + buf = lbuf; + } + printf("%s\en", buf); + } + free(lbuf); + if (ferror(fp)) + err(1, "fgetln"); +.Ed