[hackers] [PATCH] ls: fix time handling by augmenting time resolution

From: Quentin Rameau <quinq+hackers_AT_fifth.space>
Date: Sun, 1 Nov 2015 18:12:46 +0100

ls was using (old) UNIX spec (struct stat).st_[acm]time.
It now uses POSIX (struct stat).(struct timespec st_[acm]tim) which
gives time resolution in seconds and nanoseconds.
If two files have the same time in seconds, we extend the comparision to
nanoseconds.
---
 ls.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
diff --git a/ls.c b/ls.c
index c998fc7..22fe3ee 100644
--- a/ls.c
+++ b/ls.c
_AT_@ -21,7 +21,7 @@ struct entry {
 	uid_t   uid;
 	gid_t   gid;
 	off_t   size;
-	time_t  t;
+	struct timespec t;
 	dev_t   dev;
 	dev_t   rdev;
 	ino_t   ino, tino;
_AT_@ -71,11 +71,11 @@ mkent(struct entry *ent, char *path, int dostat, int follow)
 	ent->gid   = st.st_gid;
 	ent->size  = st.st_size;
 	if (cflag)
-		ent->t = st.st_ctime;
+		ent->t = st.st_ctim;
 	else if (uflag)
-		ent->t = st.st_atime;
+		ent->t = st.st_atim;
 	else
-		ent->t = st.st_mtime;
+		ent->t = st.st_mtim;
 	ent->dev   = st.st_dev;
 	ent->rdev  = st.st_rdev;
 	ent->ino   = st.st_ino;
_AT_@ -187,12 +187,12 @@ output(const struct entry *ent)
 	else
 		snprintf(grname, sizeof(grname), "%d", ent->gid);
 
-	if (time(NULL) > ent->t + (180 * 24 * 60 * 60)) /* 6 months ago? */
+	if (time(NULL) > ent->t.tv_sec + (180 * 24 * 60 * 60)) /* 6 months ago? */
 		fmt = "%b %d  %Y";
 	else
 		fmt = "%b %d %H:%M";
 
-	strftime(buf, sizeof(buf), fmt, localtime(&ent->t));
+	strftime(buf, sizeof(buf), fmt, localtime(&ent->t.tv_sec));
 	printf("%s %4ld %-8.8s %-8.8s ", mode, (long)ent->nlink, pwname, grname);
 
 	if (S_ISBLK(ent->mode) || S_ISCHR(ent->mode))
_AT_@ -226,7 +226,8 @@ entcmp(const void *va, const void *vb)
 		cmp = b->size - a->size;
 		break;
 	case 't':
-		cmp = b->t - a->t;
+		if (!(cmp = b->t.tv_sec - a->t.tv_sec))
+			cmp = b->t.tv_nsec - a->t.tv_nsec;
 		break;
 	}
 
-- 
2.6.2
Received on Sun Nov 01 2015 - 18:12:46 CET

This archive was generated by hypermail 2.3.0 : Sun Nov 01 2015 - 18:24:14 CET