/bin/ls sorting bug?

Scott Mitchell scott+freebsd at fishballoon.org
Sun Jun 20 14:47:13 GMT 2004


On Sun, Jun 20, 2004 at 09:59:12AM +0100, David Malone wrote:
> On Sat, Jun 19, 2004 at 11:52:29PM +0100, Scott Mitchell wrote:
> > On Sat, Jun 19, 2004 at 10:06:01PM +0200, Dimitry Andric wrote:
> > > Looking through ls source shows that the sorting is done by passing a
> > > comparison function to fts_open(3).  In the case of sorting by
> > > modification time, the *only* comparison made is of the mtime fields:
> > 
> > You did see the patch attached to my original post, right?  It modifies all
> > of these comparison functions to sort the two items by name (or reverse
> > name) in the case that their timestamps are equal.
> 
> Hi Scott,
> 
> Could you produce a version of your patch that uses the nanoseconds
> field too? I produced the one below, but I think the style in your
> patch was nicer. Also, I wonder if the revblahcmp functions should
> just call blahcmp with their arguments reversed?
> 
> 	David.

David,

New patch attached that compares against the nanos field as well.  It could
stand a bit of cleaning up to remove the overly long lines.

I'm not sure I'd want this in the tree unless we also had an option to
display the nanoseconds - as it stands you could get items apparently
ordered wrongly, unless you knew the value of their nanos fields.  I could
do that if people thought it would be useful.

	Scott

-- 
===========================================================================
Scott Mitchell           | PGP Key ID | "Eagles may soar, but weasels
Cambridge, England       | 0x54B171B9 |  don't get sucked into jet engines"
scott at fishballoon.org | 0xAA775B8B |      -- Anon
-------------- next part --------------
Index: cmp.c
===================================================================
RCS file: /home/ncvs/src/bin/ls/cmp.c,v
retrieving revision 1.13
diff -u -r1.13 cmp.c
--- cmp.c	6 Apr 2004 20:06:47 -0000	1.13
+++ cmp.c	20 Jun 2004 14:33:14 -0000
@@ -63,35 +63,47 @@
 int
 modcmp(const FTSENT *a, const FTSENT *b)
 {
-	return (b->fts_statp->st_mtime - a->fts_statp->st_mtime);
+	return (a->fts_statp->st_mtimespec.tv_sec == b->fts_statp->st_mtimespec.tv_sec ?
+		    (a->fts_statp->st_mtimespec.tv_nsec == b->fts_statp->st_mtimespec.tv_nsec ?
+			namecmp(a, b) :
+			b->fts_statp->st_mtimespec.tv_nsec - a->fts_statp->st_mtimespec.tv_nsec) :
+		    b->fts_statp->st_mtimespec.tv_sec - a->fts_statp->st_mtimespec.tv_sec);
 }
 
 int
 revmodcmp(const FTSENT *a, const FTSENT *b)
 {
-	return (a->fts_statp->st_mtime - b->fts_statp->st_mtime);
+	return modcmp(b, a);
 }
 
 int
 acccmp(const FTSENT *a, const FTSENT *b)
 {
-	return (b->fts_statp->st_atime - a->fts_statp->st_atime);
+	return (a->fts_statp->st_atimespec.tv_sec == b->fts_statp->st_atimespec.tv_sec ?
+		    (a->fts_statp->st_atimespec.tv_nsec == b->fts_statp->st_atimespec.tv_nsec ?
+			namecmp(a, b) :
+			b->fts_statp->st_atimespec.tv_nsec - a->fts_statp->st_atimespec.tv_nsec) :
+		    b->fts_statp->st_atimespec.tv_sec - a->fts_statp->st_atimespec.tv_sec);
 }
 
 int
 revacccmp(const FTSENT *a, const FTSENT *b)
 {
-	return (a->fts_statp->st_atime - b->fts_statp->st_atime);
+	return acccmp(b, a);
 }
 
 int
 statcmp(const FTSENT *a, const FTSENT *b)
 {
-	return (b->fts_statp->st_ctime - a->fts_statp->st_ctime);
+	return (a->fts_statp->st_ctimespec.tv_sec == b->fts_statp->st_ctimespec.tv_sec ?
+		    (a->fts_statp->st_ctimespec.tv_nsec == b->fts_statp->st_ctimespec.tv_nsec ?
+			namecmp(a, b) :
+			b->fts_statp->st_ctimespec.tv_nsec - a->fts_statp->st_ctimespec.tv_nsec) :
+		    b->fts_statp->st_ctimespec.tv_sec - a->fts_statp->st_ctimespec.tv_sec);
 }
 
 int
 revstatcmp(const FTSENT *a, const FTSENT *b)
 {
-	return (a->fts_statp->st_ctime - b->fts_statp->st_ctime);
+	return statcmp(b, a);
 }


More information about the freebsd-hackers mailing list