test currently produces this error message with -N
$ /bin/test -N /
/bin/test: extra argument ‘-N’

which is different from what you get with an invalid unary operator
$ /bin/test -q /
/bin/test: ‘-q’: unary operator expected

bash's test -N is supported in test_unop, but the actual test isn't there

since src/test.c and the one in bash share a common root, i thought it
makes sense to implement it here as well


---
xoxo iza
From 482095cffe9cf4bd2bab0ae27955c16e0aceed96 Mon Sep 17 00:00:00 2001
From: izabera <[email protected]>
Date: Thu, 8 Dec 2016 17:44:22 +0100
Subject: [PATCH] test: implement -N

support for N in test_unop was added by Jim Meyering over 13 years ago
in b4fa822482de9eaa4b5d429881de9d145d802f5e

* src/test.c: implement -N
* doc/coreutils.texi: document it
---
 doc/coreutils.texi | 7 ++++++-
 src/test.c         | 6 ++++++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index a507280..5412a29 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -12501,7 +12501,7 @@ Exit status:
 @menu
 * File type tests::             -[bcdfhLpSt]
 * Access permission tests::     -[gkruwxOG]
-* File characteristic tests::   -e -s -nt -ot -ef
+* File characteristic tests::   -e -s -N -nt -ot -ef
 * String tests::                -z -n = == !=
 * Numeric tests::               -eq -ne -lt -le -gt -ge
 * Connectives for test::        ! -a -o
@@ -12638,6 +12638,11 @@ True if @var{file} exists.
 @cindex nonempty file check
 True if @var{file} exists and has a size greater than zero.
 
+@item -N @var{file}
+@opindex -N
+@cindex modified since it was last read check
+True if @var{file} exists and has been modified since it was last read.
+
 @item @var{file1} -nt @var{file2}
 @opindex -nt
 @cindex newer-than file check
diff --git a/src/test.c b/src/test.c
index 8ac7467..c09d00d 100644
--- a/src/test.c
+++ b/src/test.c
@@ -445,6 +445,11 @@ unary_operator (void)
         return ! (egid == NO_GID && errno) && egid == stat_buf.st_gid;
       }
 
+    case 'N':			/* File was modified since it was last read */
+      unary_advance ();
+      return (stat (argv[pos - 1], &stat_buf) == 0
+              && stat_buf.st_atime <= stat_buf.st_mtime);
+
     case 'f':			/* File is a file? */
       unary_advance ();
       /* Under POSIX, -f is true if the given file exists
@@ -770,6 +775,7 @@ EXPRESSION is true or false and sets exit status.  It is one of:\n\
 "), stdout);
       fputs (_("\
   -L FILE     FILE exists and is a symbolic link (same as -h)\n\
+  -N FILE     FILE exists and has been modified since it was last read\n\
   -O FILE     FILE exists and is owned by the effective user ID\n\
   -p FILE     FILE exists and is a named pipe\n\
   -r FILE     FILE exists and read permission is granted\n\
-- 
2.10.2

Reply via email to