/bin/ls sorting bug?

Scott Mitchell scott+freebsd at fishballoon.org
Sat Jun 19 17:50:13 GMT 2004


Hi all,

ls(1) says that the -t option will:

     Sort by time modified (most recently modified first) before sort-
     ing the operands by lexicographical order.

which I take to mean that items (in the same directory) with the same
timestamp should be further sorted according to their names.  Unfortunately
it doesn't really work that way:

(562) tuatara:/tmp/foo $ ls -lt
total 0
-rw-rw-r--  1 scott  wheel  0 19 Jun 17:48 c
-rw-rw-r--  7 scott  wheel  0 19 Jun 17:13 b
-rw-rw-r--  7 scott  wheel  0 19 Jun 17:13 d
-rw-rw-r--  7 scott  wheel  0 19 Jun 17:13 e
-rw-rw-r--  7 scott  wheel  0 19 Jun 17:13 f
-rw-rw-r--  7 scott  wheel  0 19 Jun 17:13 g
-rw-rw-r--  7 scott  wheel  0 19 Jun 17:13 h
-rw-rw-r--  1 scott  wheel  0 19 Jun 17:13 i
-rw-rw-r--  1 scott  wheel  0 19 Jun 17:13 j
-rw-rw-r--  7 scott  wheel  0 19 Jun 17:13 a
-rw-rw-r--  1 scott  wheel  0 19 Jun 17:13 k

This is on a 4.10-PRERELEASE machine, but the -CURRENT code seems to be
identical as far as sorting goes.

Is this intended behaviour?  If so, the documentation is wrong.  Otherwise,
the attached patch produces the expected output.  I can commit it if there
are no objections.

	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.9.2.2
diff -u -r1.9.2.2 cmp.c
--- cmp.c	8 Jul 2002 06:59:27 -0000	1.9.2.2
+++ cmp.c	19 Jun 2004 16:54:55 -0000
@@ -67,35 +67,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_mtime == b->fts_statp->st_mtime ?
+		namecmp(a, b) :
+		b->fts_statp->st_mtime - a->fts_statp->st_mtime);
 }
 
 int
 revmodcmp(const FTSENT *a, const FTSENT *b)
 {
-	return (a->fts_statp->st_mtime - b->fts_statp->st_mtime);
+	return (a->fts_statp->st_mtime == b->fts_statp->st_mtime ?
+		revnamecmp(a, b) :
+		a->fts_statp->st_mtime - b->fts_statp->st_mtime);
 }
 
 int
 acccmp(const FTSENT *a, const FTSENT *b)
 {
-	return (b->fts_statp->st_atime - a->fts_statp->st_atime);
+	return (a->fts_statp->st_atime == b->fts_statp->st_atime ?
+		namecmp(a, b) :
+		b->fts_statp->st_atime - a->fts_statp->st_atime);
 }
 
 int
 revacccmp(const FTSENT *a, const FTSENT *b)
 {
-	return (a->fts_statp->st_atime - b->fts_statp->st_atime);
+	return (a->fts_statp->st_atime == b->fts_statp->st_atime ?
+		revnamecmp(a, b) :
+		a->fts_statp->st_atime - b->fts_statp->st_atime);
 }
 
 int
 statcmp(const FTSENT *a, const FTSENT *b)
 {
-	return (b->fts_statp->st_ctime - a->fts_statp->st_ctime);
+	return (a->fts_statp->st_ctime == b->fts_statp->st_ctime ?
+		namecmp(a, b) :
+		b->fts_statp->st_ctime - a->fts_statp->st_ctime);
 }
 
 int
 revstatcmp(const FTSENT *a, const FTSENT *b)
 {
-	return (a->fts_statp->st_ctime - b->fts_statp->st_ctime);
+	return (a->fts_statp->st_ctime == b->fts_statp->st_ctime ?
+		revnamecmp(a, b) :
+		a->fts_statp->st_ctime - b->fts_statp->st_ctime);
 }


More information about the freebsd-hackers mailing list