From nobody Fri Apr 19 21:52:40 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4VLpKN2SCkz5HpnN; Fri, 19 Apr 2024 21:52:40 +0000 (UTC) (envelope-from git@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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4VLpKN1tvRz493w; Fri, 19 Apr 2024 21:52:40 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1713563560; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=8xg3THCzT0lWBbPOrMPz6JjITQCmczKiEToMFb4UWsI=; b=p5jQx6D0or0UEfmtZanSMyqmuWcNq4f3jNhFJrrzE6uIsrFyp2tOGPYF9QurhQTMffASob JUmjhTGLiwbbd6j4jfPqwIAbBbOephhIZfWmCLEjuAcOmwThKcuHhe8k5nunMXyANLtwI9 n+qH0gi8je7r5aX+4zGIgTtGaKwGooiMGIlqb6E0FrnfVZhLjNVNTQbFnvY3GafcpanCQS v7emgKWN7HJQBn90btE698y49I3GIeKA7LOBmdM+RU19+YrlQ46juZnbFS35wrFvwGFHJf 4kVFcmEW0euMVtnHzLkeUWRoviY2oTS4W8E5ko1WyddIzU+hI1zv7bnQD5W3OA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1713563560; a=rsa-sha256; cv=none; b=bYZFrR+CknGENbTW4TbISpjX/T8p1BdX4Ir2C3ndIpLn/9xWFGhf++/fB1hCULcmP7CCJi Rb0Ak2gyTTQinLP5wNXjtwpTLMiLUjGW8O+HNZY+tHZyGMQWhTeUfPU4x2YDT5PS12enBY hXSgzu73iM9EfpaAWs3M0fmKFY6NKBFwMuH0SlmGT7YCBR78gR8YEcyTYxNFxTJJwYV1Lh PKl2rYhtaBCcWdFSSxB4qAT5j6u1LNDY6Phx5GYZaENGEZNE6FFYjvJ0rxYoMflqPlaPhm tkYffRRoFhyFRbgfLQOdKOBN9LUlwEnUA4Blag6lb8XQpo6+VLaGqTioVFlsvw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1713563560; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=8xg3THCzT0lWBbPOrMPz6JjITQCmczKiEToMFb4UWsI=; b=E6D5HQyFFfLIPfr7iawM/RIkH0zOz7wjX9oWid8xGxoOm0Qhf79kfrMhwwcjs0ikF6xpa6 WIev+OQS7dtP4/pQcx/hBXRlRjwYfFw/clKt3q9cSWnLg/C2Jp7BpU+PL5rZvl9OiyNiGu +Kj3l956hReV30o9m6fHRivZqgv52PbOkr1lo4MSY+Qx1bphuhO4Xhq3fA0XyAiXAbs8cx R91KVvElC4vpwPzGc9MTLHGEGgN0pCNfaBxp6slDX6rG6zmwHI07TOmQtJUENM6ZcbxIRL 48uEa+YGNRO1pCREJPuQh7KrEwcPixXb6OWLLYMcI0/tXUDeQM5/G/Vsa7HwEw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4VLpKN1VdKz19C3; Fri, 19 Apr 2024 21:52:40 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 43JLqeJt023747; Fri, 19 Apr 2024 21:52:40 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 43JLqeWL023744; Fri, 19 Apr 2024 21:52:40 GMT (envelope-from git) Date: Fri, 19 Apr 2024 21:52:40 GMT Message-Id: <202404192152.43JLqeWL023744@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Warner Losh Subject: git: b05785953e55 - main - cut: use getline() instead of fgetln() List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: imp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: b05785953e555e3dd13a162a8854076212026ca5 Auto-Submitted: auto-generated The branch main has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=b05785953e555e3dd13a162a8854076212026ca5 commit b05785953e555e3dd13a162a8854076212026ca5 Author: Martin Tournoij AuthorDate: 2024-04-19 21:11:30 +0000 Commit: Warner Losh CommitDate: 2024-04-19 21:52:21 +0000 cut: use getline() instead of fgetln() This replaces fgetln() with getline(). The main reason for this is portability, making things easier for people who want to compile these tools on non-FreeBSD systems. I appreciate that's probably not the top concern for FreeBSD base tools, but fgetln() is impossible to port to most platforms, as concurrent access is essentially impossible to implement fully correct without the line buffer on the FILE struct. Other than this, many generic FreeBSD tools compile fairly cleanly on Linux with a few small changes. Most uses of fgetln() pre-date getline() support (added in 2009 with 69099ba2ec8b), and there's been some previous patches (ee3ca711a898 8c98e6b1a7f3 1a2a4fc8ce1b) for other tools. Obtained from: https://github.com/dcantrell/bsdutils and https://github.com/chimera-linux/chimerautils Signed-off-by: Martin Tournoij Reviewed by: imp Pull Request: https://github.com/freebsd/freebsd-src/pull/893 --- usr.bin/cut/cut.c | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/usr.bin/cut/cut.c b/usr.bin/cut/cut.c index df4a2d7eb132..60ff5a31062a 100644 --- a/usr.bin/cut/cut.c +++ b/usr.bin/cut/cut.c @@ -268,14 +268,15 @@ b_cut(FILE *fp, const char *fname __unused) static int b_n_cut(FILE *fp, const char *fname) { - size_t col, i, lbuflen; - char *lbuf; + size_t col, i, bufsize = 0; + ssize_t lbuflen; + char *lbuf = NULL; int canwrite, clen, warned; mbstate_t mbs; memset(&mbs, 0, sizeof(mbs)); warned = 0; - while ((lbuf = fgetln(fp, &lbuflen)) != NULL) { + while ((lbuflen = getline(&lbuf, &bufsize, fp)) >= 0) { for (col = 0; lbuflen > 0; col += clen) { if ((clen = mbrlen(lbuf, lbuflen, &mbs)) < 0) { if (!warned) { @@ -324,6 +325,7 @@ b_n_cut(FILE *fp, const char *fname) if (lbuflen > 0) putchar('\n'); } + free(lbuf); return (warned); } @@ -382,21 +384,22 @@ f_cut(FILE *fp, const char *fname) int field, i, isdelim; char *pos, *p; int output; - char *lbuf, *mlbuf; - size_t clen, lbuflen, reallen; + char *lbuf = NULL; + size_t clen, bufsize = 0, reallen; + ssize_t lbuflen; - mlbuf = NULL; - while ((lbuf = fgetln(fp, &lbuflen)) != NULL) { + while ((lbuflen = getline(&lbuf, &bufsize, fp)) >= 0) { reallen = lbuflen; /* Assert EOL has a newline. */ - if (*(lbuf + lbuflen - 1) != '\n') { + if (lbuflen > 0 && *(lbuf + lbuflen - 1) != '\n') { /* Can't have > 1 line with no trailing newline. */ - mlbuf = malloc(lbuflen + 1); - if (mlbuf == NULL) - err(1, "malloc"); - memcpy(mlbuf, lbuf, lbuflen); - *(mlbuf + lbuflen) = '\n'; - lbuf = mlbuf; + if ((ssize_t)bufsize < (lbuflen + 1)) { + bufsize = lbuflen + 1; + lbuf = realloc(lbuf, bufsize); + } + if (lbuf == NULL) + err(1, "realloc"); + lbuf[lbuflen] = '\n'; reallen++; } output = 0; @@ -404,7 +407,7 @@ f_cut(FILE *fp, const char *fname) clen = mbrtowc(&ch, p, lbuf + reallen - p, NULL); if (clen == (size_t)-1 || clen == (size_t)-2) { warnc(EILSEQ, "%s", fname); - free(mlbuf); + free(lbuf); return (1); } if (clen == 0) @@ -431,7 +434,7 @@ f_cut(FILE *fp, const char *fname) NULL); if (clen == (size_t)-1 || clen == (size_t)-2) { warnc(EILSEQ, "%s", fname); - free(mlbuf); + free(lbuf); return (1); } if (clen == 0) @@ -463,7 +466,7 @@ f_cut(FILE *fp, const char *fname) } (void)putchar('\n'); } - free(mlbuf); + free(lbuf); return (0); }