From nobody Fri Apr 19 21:52:42 2024 X-Original-To: dev-commits-src-all@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 4VLpKQ4Z3Vz5Hpwr; Fri, 19 Apr 2024 21:52:42 +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 4VLpKQ3vRvz49MP; Fri, 19 Apr 2024 21:52:42 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1713563562; 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=b+lluVUVZZKbn+wKF6LuLYvyjQyXn/Ta1q9XgjHVjTw=; b=w7ZTfAsXqURly36uC/3hvY12R5AZ9fJnTZOZ6Y0mhJyMvSUULf6A/WulKeSQxwI1reJNv1 wcuzxuVIvjXoUrEe+pOZSQSNSC5v5hNDxiNQT4CA6mY3YtdjE37X5XIXCkWk5y3OmES2CU 395bwswi6bT5si+armEoUVbqsc9yTcLL2jiWekv1CwtfRHRKDNBXK7fkCfvj4t+Bi4BINs KClxVG6XbBv74rcyUt6dt2BJgzE0JKi9xEvEgP9YhIzoC01CgGwDS899aacNlymjZjlx7N wfw2pDdLBWQfJwb4nigGrgIMxu8NbxtTHUFyp5s8V/upekEuj4ix/2DxCiN6gg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1713563562; a=rsa-sha256; cv=none; b=q8r99H5Zrjjn9cogRFb6q2wVtExtk7O/4uU5yxZTWltOWB7oGCdBMylAoHMrXeuMxXO5g3 f8H1/kQkIxA8I24N1ADV7KeivKOhCgWBAQMctPZFgeW/tY9AaYUkgjZn/CMRt0EqAdTceS zErFN/YdmyVsoomiJqxnNb096G3zq5yIlvFl274dQUJUhKEHy5kgJ3bAfYYTaftjbiGcuc 1BLBNEyoxq7UivRU2yVWaQ+1KYv7ZPthZq793yjm0MoKXjYUcgUBHFvIloSUEfSAeV6q/T tUzq8hojwlN6sjUlmZi0tJ7Lxv0nMzrJU9A8o9tHEIAJZvWBQcah4YJyvWnkMw== 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=1713563562; 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=b+lluVUVZZKbn+wKF6LuLYvyjQyXn/Ta1q9XgjHVjTw=; b=CYb7Yqh5l4lhWunYCK8xxSFMsqlPetRNW9t/PE7eLIw4/nvTcMvmyNl6uzGZa/ACM8XTRA P7g+a5DA/VD7mdUyzg9kE13QhGst4k1sK/DpHekkam8sxnksOYsKNae7krIxNUco5hlTjz df4+05e55tAkgPgBxdDpxZG2rYfBm2XRgCHuCJzc/jyapDv21ETccJZ7/M0bntJBnUp9sA PABelZdhM9j8IiM5sWx15BOzDzaVHca3yJWUkuOkeFUdGyzrw7/8islqW/9DimT+/4qhjd F7bmeqqPkqFyfRZmQkcUPyioogmZVcUB108dk+kOF0c4/6a8ZwHYaAKXzlDLgg== 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 4VLpKQ3WcSz18cW; Fri, 19 Apr 2024 21:52:42 +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 43JLqgcY023837; Fri, 19 Apr 2024 21:52:42 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 43JLqgL7023834; Fri, 19 Apr 2024 21:52:42 GMT (envelope-from git) Date: Fri, 19 Apr 2024 21:52:42 GMT Message-Id: <202404192152.43JLqgL7023834@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: 3abcc79c6a25 - main - head: use getline() instead of fgetln() List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@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: 3abcc79c6a25fe9846be5a036d9a44086fb00163 Auto-Submitted: auto-generated The branch main has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=3abcc79c6a25fe9846be5a036d9a44086fb00163 commit 3abcc79c6a25fe9846be5a036d9a44086fb00163 Author: Martin Tournoij AuthorDate: 2024-04-19 21:11:30 +0000 Commit: Warner Losh CommitDate: 2024-04-19 21:52:22 +0000 head: 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/head/head.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/usr.bin/head/head.c b/usr.bin/head/head.c index 598d54b1c1ed..62796ae144a1 100644 --- a/usr.bin/head/head.c +++ b/usr.bin/head/head.c @@ -156,12 +156,13 @@ main(int argc, char *argv[]) static void head(FILE *fp, intmax_t cnt) { - char *cp; - size_t error, readlen; + char *cp = NULL; + size_t error, bufsize = 0; + ssize_t readlen; - while (cnt != 0 && (cp = fgetln(fp, &readlen)) != NULL) { + while (cnt != 0 && (readlen = getline(&cp, &bufsize, fp)) >= 0) { error = fwrite(cp, sizeof(char), readlen, stdout); - if (error != readlen) + if ((ssize_t)error != readlen) err(1, "stdout"); cnt--; }