Hi list,

If I touch(1) a file to a future date (or, for example, extract a tar
archive from a system with an incorrect system clock), ls -l doesn't
indicate that unless you use -T as well.

That is, ls shows a misleading timestamp for that use case.

All unixes I've worked with (including OpenBSD) shows the year instead
of hours:minutes if a file is more than six months old.

Most also show the year instead of time if a file is newer than the
current time, which in my opinion is far better than silently
suppressing that vital bit of information.

This diff makes ls(1) do that for us as well.

I've included an arbitrary grace time of a few seconds to avoid the
possibility of someone just touching a file just to find that an
immediate ls -l displays an unexpected timestamp format (seriously quick
fingers would be required).

Works like this:

   bl@skynet:/tmp$ date
   Mon Mar 21 11:36:14 CET 2011
   bl@skynet:/tmp$ touch qwerty
   bl@skynet:/tmp$ ls -l qwerty
   -rw-rw-r--  1 bl  wheel  0 Mar 21 11:36 qwerty
   bl@skynet:/tmp$ ls -lT qwerty
   -rw-rw-r--  1 bl  wheel  0 Mar 21 11:36:16 2011 qwerty
   bl@skynet:/tmp$ touch -t 202001020304.05 qwerty
   bl@skynet:/tmp$ ls -l qwerty
   -rw-rw-r--  1 bl  wheel  0 Jan  2 03:04 qwerty   <--- Misleading time
   bl@skynet:/tmp$ ls -lT qwerty
   -rw-rw-r--  1 bl  wheel  0 Jan  2 03:04:05 2020 qwerty
   bl@skynet:/tmp$ /usr/obj/bin/ls/ls -l qwerty
   -rw-rw-r--  1 bl  wheel  0 Jan  2  2020 qwerty   <--- Correct time
   bl@skynet:/tmp$

Tested on amd64.


Regards,
/Benny

----8<--------8<--------8<--------8<--------8<---- (cut)
Index: print.c
===================================================================
RCS file: /cvs/src/bin/ls/print.c,v
retrieving revision 1.27
diff -u -r1.27 print.c
--- print.c     12 Sep 2010 20:16:29 -0000      1.27
+++ print.c     21 Mar 2011 10:20:34 -0000
@@ -235,6 +235,7 @@
 {
        int i;
        char *longstring;
+       time_t now = time(NULL);

        longstring = ctime(&ftime);
        for (i = 4; i < 11; ++i)
@@ -244,7 +245,7 @@
        if (f_sectime)
                for (i = 11; i < 24; i++)
                        (void)putchar(longstring[i]);
-       else if (ftime + SIXMONTHS > time(NULL))
+       else if (ftime>now-SIXMONTHS && ftime<now+5) // a few grace secs
                for (i = 11; i < 16; ++i)
                        (void)putchar(longstring[i]);
        else {
----8<--------8<--------8<--------8<--------8<---- (cut)

-- 
internetlabbet.se     / work:   +46 8 551 124 80      / "Words must
Benny Lvfgren        /  mobile: +46 70 718 11 90     /   be weighed,
                    /   fax:    +46 8 551 124 89    /    not counted."
                   /    email:  benny -at- internetlabbet.se

Reply via email to