[PATCH v2] bltin/test: align -nt and -ot with POSIX.1-2024

наб <[email protected]>
Newsgroups org.kernel.vger.dash
Message-ID <cvvclqjh5f7lkvwzrcv4akogr3ko7bqdwqilrw5rslebgovdyh@tarta.nabijaczleweli.xyz>
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 <string.h>
 #include <unistd.h>
 #include <stdarg.h>
+#include <stdbool.h>
 #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
-- 
2.39.2
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEEfWlHToQCjFzAxEFjvP0LAY0mWPEFAmalsCsACgkQvP0LAY0m
WPHF2g//bgNCeTQFuoExKIOPBuyDEErWuXeB4+v5yCefFHOaTifl8xkdhDaQG88x
wqnf0JAvKGjlyc/32Tt1Qk5znghMPaYDUm3V2m1fW0xCzK/wy9O8wGlLW3drR3Ef
pwjjHkWIBYWdnvDh+Cno4RCbO4s+un9ykhA9oC26emjT/LCx2nLKC5/X7cDqM0QK
O63lbOFdLNl3NeTsgQdu66a23SASZW9a+/HHB4lTXiL01nSypbkvzsahSgGzjHDQ
EglStMoiBKvIqVvtTr8xNuLmSBO1aNAXNxNmPZNigddd+5fHfOuIjo0TWczzGNS7
+4dPk1Qmq54ZnfeRO1Yz2wJ0VKU1bQtKaYlMFHRhc81Aa0OEt5+rzqQ0OX03YHaA
L31oBnfow057Un5V4NZI8r+53irNCcUFo4evg7A+JHAOLC7BQFqdy+aJFzrJ9vEW
X1+Af0gjpHxxMQkJkddd/IG4W+ZVrwLkMNZOBrgfcg9yHoSj/dXHFw8/ebVG4ppC
9ZV2tFIm9S4vEeOpbU0wc6xQZE2jK0yG1sMagxDFoEIU5+CixtLwdewlebI/hZn8
cQTi6gpeAQX2LWEWIJpnFhxz8CztlK5tndGKb+YwmuqOT3g1jw7o+8QYm2s/GVuI
l+fO9d+27TFec+OkKOGHvPRdKK195JqEPBDAB7dGmAX5m3e88XA=
=A5dm
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.