From patchwork Sun Jul 28 02:42:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?0L3QsNCx?= X-Patchwork-Id: 13743829 X-Patchwork-Delegate: herbert@gondor.apana.org.au Received: from tarta.nabijaczleweli.xyz (tarta.nabijaczleweli.xyz [139.28.40.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 059C7EC4 for ; Sun, 28 Jul 2024 02:51:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=139.28.40.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722135105; cv=none; b=Tflni/xm1wF/nF1zUwvPFD1gvat+bgS0+3WtvQ9gRgL176jnKVGtN+11Ez9mmNkKvt+P0SiXvaFYc4YrbVO+L0SflhKXcoUERoF34/Mmb+1T/hTu+yKJbBr42BRMrOni4BfdmtMQUeOihw1LF9neqgmAAOBe/9Q2Eo36c2WnNYo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722135105; c=relaxed/simple; bh=DQDagQ8ZuJ7CH+h1rYjAJgRnPgVIPdI/yrGaF0f8xXc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=PgLm8WnEW3YKwRwBlZ9QUsyYbtIpCtfQpFGa+vvBsdMXab3k17DgyqdM6iU6fRTzxAD/ZpsrUKNzQvIDlorvROsJV3P5YIFxySE9eCPb2WLFZvpJ972BV1uMor3fm7aq1lDff/CVRphoeIsn890bCMLM0GMThfirgTWR/+wlYnU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nabijaczleweli.xyz; spf=pass smtp.mailfrom=nabijaczleweli.xyz; dkim=pass (2048-bit key) header.d=nabijaczleweli.xyz header.i=@nabijaczleweli.xyz header.b=WRUVIcU5; arc=none smtp.client-ip=139.28.40.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nabijaczleweli.xyz Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nabijaczleweli.xyz Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=nabijaczleweli.xyz header.i=@nabijaczleweli.xyz header.b="WRUVIcU5" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=nabijaczleweli.xyz; s=202405; t=1722134572; bh=DQDagQ8ZuJ7CH+h1rYjAJgRnPgVIPdI/yrGaF0f8xXc=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=WRUVIcU57V/9wduChX+/aLe2zOUc8I04zOSkxjsJKJqrIUF+2tK/VhYu5Xq3w3G4E dHSHiuwR3eBOBxmg5LtPaHpmcog/xX7FFNmNT6a1wT+G01IMbyLyvJ87a91mwRjhyl yxkKsZGEMJqTTnDBVYE/GhD7MM0KYextAKkDdrGlgy1mNPE5oDFE+Cl06YqMCIl6rq r7dgn32Qu/zWDhxOu3JevHHbnjObEadU7m4TRP5bII/EnLBPchl9veyBWgLjaNDHHy 0vcvFsh28pTluPuJU+ew+OvDv3eP+2XRvuMDqJtucupQdSeKU7nc0eY3gXgC0QKqkz SjPBo8Z4Bvl3g== Received: from tarta.nabijaczleweli.xyz (unknown [192.168.1.250]) by tarta.nabijaczleweli.xyz (Postfix) with ESMTPSA id 6E7F59CB6; Sun, 28 Jul 2024 04:42:52 +0200 (CEST) Date: Sun, 28 Jul 2024 04:42:52 +0200 From: =?utf-8?b?0L3QsNCx?= To: Herbert Xu Cc: dash@vger.kernel.org Subject: [PATCH v2] bltin/test: align -nt and -ot with POSIX.1-2024 Message-ID: References: Precedence: bulk X-Mailing-List: dash@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20240329 117027 pathname1 −nt pathname2 117028 True if pathname1 resolves to an existing file and pathname2 cannot be resolved, or if 117029 both resolve to existing files and pathname1 is newer than pathname2 according to 117030 their last data modification timestamps; otherwise, false. 117031 pathname1 −ot pathname2 117032 True if pathname2 resolves to an existing file and pathname1 cannot be resolved, or if 117033 both resolve to existing files and pathname1 is older than pathname2 according to 117034 their last data modification timestamps; otherwise, false. The correct output is $ [ 2024 -nt 2023 ] && echo yes yes $ [ 2023 -nt 2024 ] && echo yes $ [ 2023 -nt ENOENT ] && echo yes yes $ [ ENOENT -nt 2024 ] && echo yes and $ [ 2024 -ot 2023 ] && echo yes $ [ 2023 -ot 2024 ] && echo yes yes $ [ 2023 -ot ENOENT ] && echo yes $ [ ENOENT -ot 2024 ] && echo yes yes but dash currently returned only the first yes out of both blocks. --- src/bltin/test.c | 31 +++++++++++++++++-------------- src/dash.1 | 14 ++++++++++++-- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/bltin/test.c b/src/bltin/test.c index 2db4d0f..d7996df 100644 --- a/src/bltin/test.c +++ b/src/bltin/test.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "bltin.h" #include "../exec.h" @@ -471,16 +472,17 @@ newerf (const char *f1, const char *f2) { struct stat64 b1, b2; + if (stat64(f1, &b1) != 0) + return false; + if (stat64(f2, &b2) != 0) + return true; + #ifdef HAVE_ST_MTIM - return (stat64(f1, &b1) == 0 && - stat64(f2, &b2) == 0 && + return ( b1.st_mtim.tv_sec > b2.st_mtim.tv_sec || - (b1.st_mtim.tv_sec == b2.st_mtim.tv_sec && (b1.st_mtim.tv_nsec > b2.st_mtim.tv_nsec ))) - ); + (b1.st_mtim.tv_sec == b2.st_mtim.tv_sec && (b1.st_mtim.tv_nsec > b2.st_mtim.tv_nsec ))); #else - return (stat64(f1, &b1) == 0 && - stat64(f2, &b2) == 0 && - b1.st_mtime > b2.st_mtime); + return b1.st_mtime > b2.st_mtime; #endif } @@ -489,16 +491,17 @@ olderf (const char *f1, const char *f2) { struct stat64 b1, b2; + if (stat64(f2, &b2) != 0) + return false; + if (stat64(f1, &b1) != 0) + return true; + #ifdef HAVE_ST_MTIM - return (stat64(f1, &b1) == 0 && - stat64(f2, &b2) == 0 && + return (b1.st_mtim.tv_sec < b2.st_mtim.tv_sec || - (b1.st_mtim.tv_sec == b2.st_mtim.tv_sec && (b1.st_mtim.tv_nsec < b2.st_mtim.tv_nsec ))) - ); + (b1.st_mtim.tv_sec == b2.st_mtim.tv_sec && (b1.st_mtim.tv_nsec < b2.st_mtim.tv_nsec ))); #else - return (stat64(f1, &b1) == 0 && - stat64(f2, &b2) == 0 && - b1.st_mtime < b2.st_mtime); + return b1.st_mtime < b2.st_mtime; #endif } diff --git a/src/dash.1 b/src/dash.1 index 6c4ee2d..96ce89e 100644 --- a/src/dash.1 +++ b/src/dash.1 @@ -2019,7 +2019,12 @@ .Ss Builtins exist and .Ar file1 is newer than -.Ar file2 . +.Ar file2 , +or if +.Ar file1 +exists but +.Ar file2 +doesn't. .It Ar file1 Fl ot Ar file2 True if .Ar file1 @@ -2028,7 +2033,12 @@ .Ss Builtins exist and .Ar file1 is older than -.Ar file2 . +.Ar file2 , +or if +.Ar file2 +exists but +.Ar file1 +doesn't. .It Ar file1 Fl ef Ar file2 True if .Ar file1