[git commit] test: fix POSIX -nt and -ot when "older" is missing

Denys Vlasenko vda.linux at googlemail.com
Sat Oct 10 16:29:56 UTC 2026


commit: https://git.busybox.net/busybox/commit/?id=5e57539acb16c6018a9c4d69f6890fb3de635a79
branch: https://git.busybox.net/busybox/log/?h=master

These should evaluate true if the newer file exists and the older is
missing or older, but it was never true if older is missing.

Note: 'test X -nt Y' is identical to 'test Y -ot X'

  https://pubs.opengroup.org/onlinepubs/9799919799/utilities/test.html

function                                             old     new   delta
binop                                                593     634     +41
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 41/0)               Total: 41 bytes

Signed-off-by: Avi Halachmi (:avih) <avihpit at yahoo.com>
Signed-off-by: Ron Yorston <rmy at pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 coreutils/test.c     | 10 +++++-----
 testsuite/test.tests | 13 +++++++++++++
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/coreutils/test.c b/coreutils/test.c
index b63e33cc0..f2f0061fb 100644
--- a/coreutils/test.c
+++ b/coreutils/test.c
@@ -621,15 +621,15 @@ static int binop(void)
 /*	if (is_file_op(op->op_num)) */
 	{
 		struct stat b1, b2;
+		int sb1 = stat(opnd1, &b1);
+		int sb2 = stat(opnd2, &b2);
 
-		if (stat(opnd1, &b1) || stat(opnd2, &b2))
-			return 0; /* false, since at least one stat failed */
 		if (op->op_num == FILNT)
-			return b1.st_mtime > b2.st_mtime;
+			return !sb1 && (sb2 || b1.st_mtime > b2.st_mtime);
 		if (op->op_num == FILOT)
-			return b1.st_mtime < b2.st_mtime;
+			return !sb2 && (sb1 || b1.st_mtime < b2.st_mtime);
 		/*if (op->op_num == FILEQ)*/
-		return b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino;
+		return !sb1 && !sb2 && b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino;
 	}
 	/*return 1; - NOTREACHED */
 }
diff --git a/testsuite/test.tests b/testsuite/test.tests
index 1c2edaf62..36bd37191 100755
--- a/testsuite/test.tests
+++ b/testsuite/test.tests
@@ -95,5 +95,18 @@ testing "test '!' '(' = '(': should be false (1)" \
 	"busybox test '!' '(' = '('; echo \$?" \
 	"1\n" \
 	"" ""
+mkdir -p test.tempdir
+touch test.tempdir/exists
+
+testing "test exists -nt nonexistent should be true (0)" \
+	"busybox test test.tempdir/exists -nt test.tempdir/nonexistent; echo \$?" \
+	"0\n" \
+	"" ""
+
+testing "test nonexistent -ot exists should be true (0)" \
+	"busybox test test.tempdir/nonexistent -ot test.tempdir/exists; echo \$?" \
+	"0\n" \
+	"" ""
+rm -rf test.tempdir
 
 exit $FAILCOUNT


More information about the busybox-cvs mailing list