Looking over the new stuff in POSIX 2024, toybox already has most of the stuff 
it specifies

Excluding things like make which toybox doesn't have, and gettext/msgfmt which 
from all
the design documentation I've read Rob doesn't wanna add to toybox, These are 
the POSIX 2024
features toybox doesn't have:

dd iflag=fullblock
rm -d
tail -r (which from my checking coreutils doesn't have)
test -ef -nt -ot

The attached patch adds in the test -ef -nt -ot

As for symbolic links:

$ test /bin/bash -ef /bin/sh && echo yes
yes
$ test /bin/bash -ot /bin/sh && echo yes
yes

test is meant to follow symlinks _only_ when checking inodes with -ef

-   Oliver Webb <aquahobby...@proton.me>
From daf01b634c69beac5184b93b7f74d728406e8155 Mon Sep 17 00:00:00 2001
From: Oliver Webb <aquahobby...@proton.me>
Date: Tue, 2 Jul 2024 23:55:21 -0500
Subject: [PATCH] test -ef -ot -nt (POSIX 2024)

---
 tests/test.test   |  7 +++++++
 toys/posix/test.c | 17 +++++++++++++++--
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/tests/test.test b/tests/test.test
index 2174f405..216522f7 100644
--- a/tests/test.test
+++ b/tests/test.test
@@ -113,6 +113,13 @@ testing "-ge" "arith_test -ge" "eg" "" ""
 testing "-lt" "arith_test -lt" "l" "" ""
 testing "-le" "arith_test -le" "le" "" ""
 
+touch oldfile -d 1970-01-01
+touch newfile -d 2031-01-01
+
+testcmd "-ef" "newfile -ef newfile && echo yes" "yes\n" "" ""
+testcmd "-ot" "oldfile -ot newfile && echo yes" "yes\n" "" ""
+testcmd "-nt" "newfile -nt oldfile && echo yes" "yes\n" "" ""
+
 testing "positional" "test -a == -a && echo yes" "yes\n" "" ""
 testing "! stacks" 'test \! \! \! \! 2 -eq 2 && echo yes' "yes\n" "" ""
 
diff --git a/toys/posix/test.c b/toys/posix/test.c
index 881bb668..05e7af2a 100644
--- a/toys/posix/test.c
+++ b/toys/posix/test.c
@@ -59,7 +59,7 @@ static int do_test(char **args, int *count)
 
   if (*count>=3) {
     *count = 3;
-    char *s = args[1], *ss = "eqnegtgeltle";
+    char *s = args[1], *ss = "eqnegtgeltleefotnt";
     // TODO shell integration case insensitivity
     if (!strcmp(s, "=") || !strcmp(s, "==")) return !strcmp(args[0], args[2]);
     if (!strcmp(s, "!=")) return strcmp(args[0], args[2]);
@@ -79,7 +79,15 @@ static int do_test(char **args, int *count)
       return (*s=='<') ? i<0 : i>0;
     }
     if (*s=='-' && strlen(s)==3 && (s = strstr(ss, s+1)) && !((i = s-ss)&1)) {
-      long long a = atolx(args[0]), b = atolx(args[2]);
+      struct stat st1, st2;
+      long long a QUIET, b QUIET;
+      if (i <= 10) {
+        a = atolx(args[0]);
+        b = atolx(args[2]);
+      } else {
+        if ((i == 12 ? stat : lstat)(args[0], &st1)
+          || (i == 12 ? stat : lstat)(args[2], &st2)) return 0;
+      }
 
       if (!i) return a == b;
       if (i==2) return a != b;
@@ -87,6 +95,11 @@ static int do_test(char **args, int *count)
       if (i==6) return a >= b;
       if (i==8) return a < b;
       if (i==10) return a<= b;
+      if (i==12) return (st1.st_dev==st2.st_dev) && (st1.st_ino==st2.st_ino);
+      if (i==14) return (st1.st_atim.tv_sec < st2.st_atim.tv_sec) ||
+        (st1.st_atim.tv_nsec < st2.st_atim.tv_nsec);
+      if (i==16) return (st1.st_atim.tv_sec > st2.st_atim.tv_sec) ||
+        (st1.st_atim.tv_nsec > st2.st_atim.tv_nsec);
     }
   }
   s = *args;
-- 
2.45.2

_______________________________________________
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to