builtin test: Implement -ot, -nt, -ef

These are non-POSIX extensions other test(1) utilities implement,
which compares the modification time of two files as proposed for
fish in #3589: testing if one file is newer than another file.

-ef is a common extension to test(1) which checks if two paths refer
to the same file, by comparing the dev and inode numbers.
This commit is contained in:
Aaron Gyes
2022-07-07 17:07:32 -07:00
parent 7bdc712615
commit 8f91ee7f6b
3 changed files with 40 additions and 3 deletions

View File

@@ -868,6 +868,22 @@ static int compare(T a, T b) {
return 0;
}
/// \return true if \param rhs has higher mtime seconds than this file_id_t.
/// If identical, nanoseconds are compared.
bool file_id_t::older_than(const file_id_t &rhs) const {
int ret = compare(mod_seconds, rhs.mod_seconds);
if (!ret) ret = compare(mod_nanoseconds, rhs.mod_nanoseconds);
switch (ret) {
case -1:
return true;
case 1:
case 0:
return false;
default:
DIE("unreachable");
}
}
int file_id_t::compare_file_id(const file_id_t &rhs) const {
// Compare each field, stopping when we get to a non-equal field.
int ret = 0;