[hackers] [sbase] ls: fix time handling by augmenting time resolution || Quentin Rameau

From: <git_AT_suckless.org>
Date: Sun, 1 Nov 2015 18:31:11 +0100 (CET)

commit 4859f4c8250e72564c461bfcaa4b2121fcab219f
Author: Quentin Rameau <quinq+hackers_AT_fifth.space>
AuthorDate: Sun Nov 1 18:12:46 2015 +0100
Commit: sin <sin_AT_2f30.org>
CommitDate: Sun Nov 1 17:29:39 2015 +0000

    ls: fix time handling by augmenting time resolution
    
    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.

diff --git a/ls.c b/ls.c
index 3067ba4..3611a87 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;
         }
 
Received on Sun Nov 01 2015 - 18:31:11 CET

This archive was generated by hypermail 2.3.0 : Sun Nov 01 2015 - 18:36:17 CET