From nobody Wed Aug 6 20:43:42 2025 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 4by2M32S3bz64C73; Wed, 06 Aug 2025 20:43:43 +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 "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4by2M25YwQz48tj; Wed, 06 Aug 2025 20:43:42 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1754513022; 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=dgVoLFhIvUU9fB1ofnF2Z7PX1GPv6kssrU3NM5wu8JQ=; b=KkCrioMW2+/xoo3jd8SsrD5+RRXicOhQAq8wGF5wojXH+oLQSBXw4LRg0x/PDgz5hHL5tB V7Sda9PznwCJdirj2KF6gOkCS5sgdszVqMuOXS2+lV2bBqCRRhZ5eDK1RqVpDvM/J/G8gN FEPrp3o/KyYp8jQHoGrFjTROeaRFWZdgWtes8ESdlz9RrNmI/RK5WF5cS0DvrIJ+l2fZv7 35Mv07TZMCIbkseyVQu77qHom00iL/rExgWVITEiLNktTlIdQaFwIaRB7tgWV5uQ1FAsR9 zxuwB/T0Etl7AK9FqFnYxSmi9TVhKz1sA/VHFs+trSNl/sIskkPQUg16gUL6eA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1754513022; 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=dgVoLFhIvUU9fB1ofnF2Z7PX1GPv6kssrU3NM5wu8JQ=; b=hQie8LiZEYvtebCbsEyUgnN2yHXng12GCF8n6Z6B5EgX9JBuU6EkovZ5hVBOnnerJ0upUW Mjaz+s9an90TTjQTzVgK43GQoTGzLBuRjjuFJuxcvXMPv+bOS+UeEc7C68r4a6m5idYtpR 9jbuQWhKTRvUdJBy9fsQtnNvzrS5vzTByXsP6zD+zag6eDW7ovP8U4Cn1IVUrxRCJmiEB3 NMveNmwNrLPfFKNJAiBbQ4MozSpNPbyyPZPsXgN7rmBodSkVMehwRR3+vsufqpsCJQC/lF MxarYFOp7tKTn6jtmFiQau5Ko7dYbvpFJ391phH73F98n70/29b85+3O6qfnJw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1754513022; a=rsa-sha256; cv=none; b=vMzPAqjNr1zWr2GYrxRZJeJjhlGYeAls6+n4JoaQIYWtPtKa6ypzbE0DbJ294Zwb0fGs12 5PL+njDjZ5222BYcU1FCweX3oNZnULHmUTQ/Px5iCnM+hb2G/QG6c6kYYW+z2GO6HRJ48h JUtHsn5zmTVKuKKUpJBN6uVFo9BZ36d42PwsUO/nHZFAdIJ2S1lzvb1VthYA+YTREC949J Mi4o5+Elauu2fkmANZ0pyXFlfzdh7zf91JTNLf8GvAdH85hx+VKYJVTyOftMhYZvCS4yNN LFbc6wogGrGA/2gGbUi7hUkvpwX1PaCuwOHN08jUMKyTBhyqYxWbwW1sV/Cpgg== 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 4by2M259rXzd28; Wed, 06 Aug 2025 20:43:42 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 576Khg0h033696; Wed, 6 Aug 2025 20:43:42 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 576KhgTY033693; Wed, 6 Aug 2025 20:43:42 GMT (envelope-from git) Date: Wed, 6 Aug 2025 20:43:42 GMT Message-Id: <202508062043.576KhgTY033693@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= Subject: git: d81c64d165d5 - main - tail: Fix -b, -c, -n options 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: des X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: d81c64d165d52f8ebcafadda5012d3bb2bdd25a9 Auto-Submitted: auto-generated The branch main has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=d81c64d165d52f8ebcafadda5012d3bb2bdd25a9 commit d81c64d165d52f8ebcafadda5012d3bb2bdd25a9 Author: Dag-Erling Smørgrav AuthorDate: 2025-08-06 20:34:34 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2025-08-06 20:43:13 +0000 tail: Fix -b, -c, -n options Switching from strtoll() to expand_number() was improper at the time as it only accepted positive numbers. Now that it also accepts negative numbers, the -b option is still broken because the same commit that switched to expand_number() also dropped the multiplication by units. Fixes: 643ac419fafb Reviewed by: delphij Differential Revision: https://reviews.freebsd.org/D51757 --- usr.bin/tail/tail.c | 12 ++++++----- usr.bin/tail/tests/tail_test.sh | 48 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/usr.bin/tail/tail.c b/usr.bin/tail/tail.c index fc60a82287df..a92eee3881b4 100644 --- a/usr.bin/tail/tail.c +++ b/usr.bin/tail/tail.c @@ -95,15 +95,17 @@ main(int argc, char *argv[]) * -r is the entire file, not 10 lines. */ #define ARG(units, forward, backward) { \ + int64_t num; \ if (style) \ usage(); \ - if (expand_number(optarg, &off)) \ + if (expand_number(optarg, &num)) \ err(1, "illegal offset -- %s", optarg); \ - if (off > INT64_MAX / units || off < INT64_MIN / units ) \ + if (num > INT64_MAX / units || num < INT64_MIN / units) \ errx(1, "illegal offset -- %s", optarg); \ - switch(optarg[0]) { \ + off = num * units; \ + switch (optarg[0]) { \ case '+': \ - if (off) \ + if (off != 0) \ off -= (units); \ style = (forward); \ break; \ @@ -121,7 +123,7 @@ main(int argc, char *argv[]) off = 0; while ((ch = getopt_long(argc, argv, "+Fb:c:fn:qrv", long_opts, NULL)) != -1) - switch(ch) { + switch (ch) { case 'F': /* -F is superset of (and implies) -f */ Fflag = fflag = 1; break; diff --git a/usr.bin/tail/tests/tail_test.sh b/usr.bin/tail/tests/tail_test.sh index 9c941f8a2c2f..74d6908f7568 100755 --- a/usr.bin/tail/tests/tail_test.sh +++ b/usr.bin/tail/tests/tail_test.sh @@ -423,6 +423,51 @@ no_lf_at_eof_body() atf_check -o inline:"a\nb\nc" tail -4 infile } +atf_test_case tail_b +tail_b_head() +{ + atf_set "descr" "Test -b option" +} +tail_b_body() +{ + (jot -b a 256 ; jot -b b 256 ; jot -b c 256) >infile + (jot -b b 256 ; jot -b c 256) >outfile + # infile is 3 blocks long, outfile contains the last two + atf_check -o file:outfile tail -b +2 infile # start at the 2nd block + atf_check -o file:outfile tail -b -2 infile # 2 blocks from the end + atf_check -o file:outfile tail -b 2 infile # 2 blocks from the end +} + +atf_test_case tail_c +tail_c_head() +{ + atf_set "descr" "Test -c option" +} +tail_c_body() +{ + (jot -b a 256 ; jot -b b 256 ; jot -b c 256) >infile + (jot -b b 256 ; jot -b c 256) >outfile + # infile is 1536 bytes long, outfile contains the last 1024 + atf_check -o file:outfile tail -c +513 infile # start at the 513th byte + atf_check -o file:outfile tail -c -1024 infile # 1024 bytes from the end + atf_check -o file:outfile tail -c 1024 infile # 1024 bytes from the end +} + +atf_test_case tail_n +tail_n_head() +{ + atf_set "descr" "Test -n option" +} +tail_n_body() +{ + (jot -b a 256 ; jot -b b 256 ; jot -b c 256) >infile + (jot -b b 256 ; jot -b c 256) >outfile + # infile is 768 lines long, outfile contains the last 512 + atf_check -o file:outfile tail -n +257 infile # start at the 257th line + atf_check -o file:outfile tail -n -512 infile # 512 lines from the end + atf_check -o file:outfile tail -n 512 infile # 512 lines from the end +} + atf_init_test_cases() { atf_add_test_case empty_r @@ -448,4 +493,7 @@ atf_init_test_cases() atf_add_test_case verbose_header atf_add_test_case si_number atf_add_test_case no_lf_at_eof + atf_add_test_case tail_b + atf_add_test_case tail_c + atf_add_test_case tail_n }