diff mbox

Greater resolution in test -nt / test -ot

Message ID 5371d909-cb24-e858-5a39-43f7788a37a3@inlv.org (mailing list archive)
State Accepted
Delegated to: Herbert Xu
Headers show

Commit Message

Martijn Dekker March 7, 2018, 5:32 p.m. UTC
Op 07-03-18 om 15:46 schreef Martijn Dekker:
> Op 06-03-18 om 09:19 schreef Herbert Xu:
>> On Thu, Jun 22, 2017 at 10:30:02AM +0200, Petr Skočík wrote:
>>> would you be willing to pull something like this?
> [...]
>>> I could use greater resolution in `test -nt` / `test -ot`, and st_mtim
>>> field is standardized under POSIX.1-2008 (or so stat(2) says).
>>
>> Sure.  But your patch is corrupted.
> 
> Fixed patch attached.
> 
> But I wouldn't apply it as is. My system does not have st_mtim. So I
> think it needs a configure test and a fallback to the old method.

Here's an attempt to make that happen. See attached.

- M.

Comments

Herbert Xu March 21, 2018, 4:32 p.m. UTC | #1
On Wed, Mar 07, 2018 at 05:32:29PM +0000, Martijn Dekker wrote:
> Op 07-03-18 om 15:46 schreef Martijn Dekker:
> > Op 06-03-18 om 09:19 schreef Herbert Xu:
> >> On Thu, Jun 22, 2017 at 10:30:02AM +0200, Petr Skočík wrote:
> >>> would you be willing to pull something like this?
> > [...]
> >>> I could use greater resolution in `test -nt` / `test -ot`, and st_mtim
> >>> field is standardized under POSIX.1-2008 (or so stat(2) says).
> >>
> >> Sure.  But your patch is corrupted.
> > 
> > Fixed patch attached.
> > 
> > But I wouldn't apply it as is. My system does not have st_mtim. So I
> > think it needs a configure test and a fallback to the old method.
> 
> Here's an attempt to make that happen. See attached.

Patch applied.  Thanks.
diff mbox

Patch

diff --git a/configure.ac b/configure.ac
index 9c4ced8..0417fa2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -149,6 +149,20 @@  AC_CHECK_FUNC(open64,, [
 	AC_DEFINE(open64, open, [64-bit operations are the same as 32-bit])
 ])
 
+dnl Check if struct stat has st_mtim.
+AC_MSG_CHECKING(for stat::st_mtim)
+AC_COMPILE_IFELSE(
+[AC_LANG_PROGRAM([#include <time.h>
+#include <sys/time.h>
+#include <sys/stat.h>],
+[struct stat foo; return sizeof(foo.st_mtim.tv_sec)])],
+have_st_mtim=yes, have_st_mtim=no)
+AC_MSG_RESULT($have_st_mtim)
+if test "$have_st_mtim" = "yes"; then
+	AC_DEFINE([HAVE_ST_MTIM], [1],
+		[Define if your `struct stat' has `st_mtim'])
+fi
+
 AC_ARG_WITH(libedit, AS_HELP_STRING(--with-libedit, [Compile with libedit support]))
 use_libedit=
 if test "$with_libedit" = "yes"; then
diff --git a/src/bltin/test.c b/src/bltin/test.c
index 58c05fe..d1458df 100644
--- a/src/bltin/test.c
+++ b/src/bltin/test.c
@@ -476,9 +476,17 @@  newerf (const char *f1, const char *f2)
 {
 	struct stat b1, b2;
 
+#ifdef HAVE_ST_MTIM
+	return (stat (f1, &b1) == 0 &&
+		stat (f2, &b2) == 0 &&
+		( 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 )))
+	);
+#else
 	return (stat (f1, &b1) == 0 &&
 		stat (f2, &b2) == 0 &&
 		b1.st_mtime > b2.st_mtime);
+#endif
 }
 
 static int
@@ -486,9 +494,17 @@  olderf (const char *f1, const char *f2)
 {
 	struct stat b1, b2;
 
+#ifdef HAVE_ST_MTIM
+	return (stat (f1, &b1) == 0 &&
+		stat (f2, &b2) == 0 &&
+		(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 )))
+	);
+#else
 	return (stat (f1, &b1) == 0 &&
 		stat (f2, &b2) == 0 &&
 		b1.st_mtime < b2.st_mtime);
+#endif
 }
 
 static int