From nobody Fri Apr 19 21:52:43 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 4VLpKS1ZYvz5Hpsn; Fri, 19 Apr 2024 21:52:44 +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 4VLpKR4fRLz49Q6; Fri, 19 Apr 2024 21:52:43 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1713563563; 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=w8/iqOfCfr4dRarpWDiE3B9mixU3BjGOzMqq8bSgrSY=; b=X7vBM3wT2EN+ZIWAOai6IUQ5dTn1uVi6rv2D/XAXjqV4hf7fDs2rX5PCSH4sy9oBDVNMu4 mKl5jTZJwRxOMU2jFG+/AnWOyVdWmuGg4fvhsRs1mduuZE2U+Lq4b73d/a0MEWo/UU8kvl 7e1oewsPYINvo3si+gOUxwuANGsZrR28kEGBBLik2uRbhUqURazEoO5Fo8ryLMoNgmbUwe ODspUey/oku38KLuW9m/IQUyI3rTcLBPh/GA8cRdJmk/aOHJK+RSpwlMp+WJpF2YLgEFF6 wVSEwRpfd/CWPYdwWFmiOmnSNwLApELBp/g8loYohJaqIi739MmNtAdaKDumUQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1713563563; a=rsa-sha256; cv=none; b=hwaFcXYx+7b5RzO5KNJ/ZHXyNX6r3CpS7gfgDo7Dn2bd6XjltTu3g7gYjOEnuy64GZFXsi Jikh3CwKo3w2gwPuK55wj8qnJWx+1JbF8FpPsr7yYpjpVW69xnOb/chYPGyizdqiu+484V 3qc+8oCor6gKam/5Z7nbCwT/4tFx9WB9G2P+CSIgnjejWo+Ag/W2HZJG11wUHUS+TC4nmF fckvZtTx90Oyl6+agsfJYSPCipOdMtIgZu0CGEpyDRefyiZiRsXei/NS3sgPH32lm0vn4q y/nLY7lK44fd/Gd6anQtZjsa9XzOgnXTrqdfJjFwHvJpd9C9dAMx7B5/jSfJpQ== 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=1713563563; 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=w8/iqOfCfr4dRarpWDiE3B9mixU3BjGOzMqq8bSgrSY=; b=D7RbW7FjplOdCY2Hx/VhliorXvVi6oh0xNgcwiFIWqDRGLGQ3SOEIJ3DWa16EtsP09mXnI IBf9DbE+4i11QF4/QWGl7QKq7hi+Dj+6KGRjrgC8hap4Eu+nMSJlm3J/5p/oaEY+Ub8wdI z0KnNUMZG1WclbjrfWLk71yiYscz2uIqgl1P0jfRBdxcEYXjzuSkRhX4VRFTi88p5rpAlR RHPIenv/CVAOcOTmu2nJxEm2MdNlewEXVG7Ly/TMkUZBw4LbBfHLYlHsRZhlbe7XqvmCYZ kSHbL/sIufP1R7h+x8pnCRqokaBDpjAn3NBpSZrdlgLAJijTE4GnCOIo3NZL5g== 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 4VLpKR4Cylz18cX; Fri, 19 Apr 2024 21:52:43 +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 43JLqhHQ023882; Fri, 19 Apr 2024 21:52:43 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 43JLqhSN023879; Fri, 19 Apr 2024 21:52:43 GMT (envelope-from git) Date: Fri, 19 Apr 2024 21:52:43 GMT Message-Id: <202404192152.43JLqhSN023879@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: d3643c9efe78 - main - join: 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: d3643c9efe7806c17c11251acd299f70b112c596 Auto-Submitted: auto-generated The branch main has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=d3643c9efe7806c17c11251acd299f70b112c596 commit d3643c9efe7806c17c11251acd299f70b112c596 Author: Martin Tournoij AuthorDate: 2024-04-19 21:11:30 +0000 Commit: Warner Losh CommitDate: 2024-04-19 21:52:22 +0000 join: 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/join/join.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/usr.bin/join/join.c b/usr.bin/join/join.c index b1be8cd81690..79469cdc52db 100644 --- a/usr.bin/join/join.c +++ b/usr.bin/join/join.c @@ -262,9 +262,10 @@ static void slurp(INPUT *F) { LINE *lp, *lastlp, tmp; - size_t len; + size_t blen = 0; + ssize_t len; int cnt; - char *bp, *fieldp; + char *bp, *buf = NULL, *fieldp; /* * Read all of the lines from an input file that have the same @@ -307,21 +308,21 @@ slurp(INPUT *F) F->pushbool = 0; continue; } - if ((bp = fgetln(F->fp, &len)) == NULL) + if ((len = getline(&buf, &blen, F->fp)) < 0) { + free(buf); return; - if (lp->linealloc <= len + 1) { + } + if (lp->linealloc <= (size_t)(len + 1)) { lp->linealloc += MAX(100, len + 1 - lp->linealloc); if ((lp->line = realloc(lp->line, lp->linealloc)) == NULL) err(1, NULL); } - memmove(lp->line, bp, len); + memmove(lp->line, buf, len); /* Replace trailing newline, if it exists. */ - if (bp[len - 1] == '\n') + if (buf[len - 1] == '\n') lp->line[len - 1] = '\0'; - else - lp->line[len] = '\0'; bp = lp->line; /* Split the line into fields, allocate space as necessary. */ @@ -345,6 +346,7 @@ slurp(INPUT *F) break; } } + free(buf); } static char *