test: add -N unary operator

Bash knows 'test -N FILE'.  Add it to GNU 'test' as well.

* src/test.c (unary_operator): Add a case for 'N'.
(usage): Document it.
* doc/coreutils.texi (node File characteristic tests): Likewise.
* NEWS (New features): Likewise.
* tests/misc/test-N.sh: Add a test.
* tests/local.mk (all_tests): Reference it.
This commit is contained in:
Bernhard Voelker 2018-10-22 00:54:51 +02:00
parent 7fd7709a7a
commit 1a674a036b
5 changed files with 63 additions and 0 deletions

3
NEWS
View File

@ -27,6 +27,9 @@ GNU coreutils NEWS -*- outline -*-
id now supports specifying multiple users.
test now supports the '-N FILE' unary operator (like e.g. bash) to check
whether FILE exists and has been modified since it was last read.
* Noteworthy changes in release 8.30 (2018-07-01) [stable]

View File

@ -13099,6 +13099,12 @@ True if @var{file1} is older (according to modification date) than
True if @var{file1} and @var{file2} have the same device and inode
numbers, i.e., if they are hard links to each other.
@item -N @var{file}
@opindex -N
@cindex mtime-greater-atime file check
True if @var{file} exists and has been modified (mtime) since it was
last read (atime).
@end table

View File

@ -417,6 +417,16 @@ unary_operator (void)
unary_advance ();
return euidaccess (argv[pos - 1], X_OK) == 0;
case 'N': /* File exists and has been modified since it was last read? */
{
unary_advance ();
if (stat (argv[pos - 1], &stat_buf) != 0)
return false;
struct timespec atime = get_stat_atime (&stat_buf);
struct timespec mtime = get_stat_mtime (&stat_buf);
return (timespec_cmp (mtime, atime) > 0);
}
case 'O': /* File is owned by you? */
{
unary_advance ();
@ -741,6 +751,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\

View File

@ -408,6 +408,7 @@ all_tests = \
tests/misc/tac-2-nonseekable.sh \
tests/misc/tail.pl \
tests/misc/tee.sh \
tests/misc/test-N.sh \
tests/misc/test-diag.pl \
tests/misc/time-style.sh \
tests/misc/timeout.sh \

42
tests/misc/test-N.sh Executable file
View File

@ -0,0 +1,42 @@
#!/bin/sh
# Test 'test -N file'.
# Copyright (C) 2018 Free Software Foundation, Inc.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
print_ver_ test
# For a freshly touched file, atime should equal mtime: 'test -N' returns 1.
touch file || framework_failure_
stat file
returns_ 1 env test -N file || fail=1
# Set access time to 2 days ago: 'test -N' returns 0.
touch -a -d "12:00 today -2 days" file || framework_failure_
stat file
env test -N file || fail=1
# Set mtime to 2 days before atime: 'test -N' returns 1;
touch -m -d "12:00 today -4 days" file || framework_failure_
stat file
returns_ 1 env test -N file || fail=1
# Now modify the file: 'test -N' returns 0.
> file || framework_failure_
stat file
env test -N file || fail=1
Exit $fail