Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 19 Jun 2004 22:06:01 +0200
From:      Dimitry Andric <dimitry@andric.com>
To:        Scott Mitchell <scott+freebsd@fishballoon.org>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: /bin/ls sorting bug?
Message-ID:  <14210101.20040619220601@andric.com>
In-Reply-To: <20040619193545.GC462@tuatara.fishballoon.org>
References:  <20040619175007.GB462@tuatara.fishballoon.org> <414787887.20040619210137@andric.com> <20040619193545.GC462@tuatara.fishballoon.org>

next in thread | previous in thread | raw e-mail | index | archive | help
------------8B1A31B42C89F112
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable

On 2004-06-19 at 21:35:45 Scott Mitchell wrote:

> Sure (added -i to make it easier to see what's going on here):

> (505) tuatara:/tmp/foo $ ls -iltT
> total 0
> 35 -rw-rw-r--  1 scott  wheel  0 19 Jun 17:48:40 2004 c
> 11 -rw-rw-r--  7 scott  wheel  0 19 Jun 17:13:36 2004 b
> 11 -rw-rw-r--  7 scott  wheel  0 19 Jun 17:13:36 2004 d
> 11 -rw-rw-r--  7 scott  wheel  0 19 Jun 17:13:36 2004 e
> 11 -rw-rw-r--  7 scott  wheel  0 19 Jun 17:13:36 2004 f
> 11 -rw-rw-r--  7 scott  wheel  0 19 Jun 17:13:36 2004 g
> 11 -rw-rw-r--  7 scott  wheel  0 19 Jun 17:13:36 2004 h
> 41 -rw-rw-r--  1 scott  wheel  0 19 Jun 17:13:36 2004 i
> 51 -rw-rw-r--  1 scott  wheel  0 19 Jun 17:13:36 2004 j
> 11 -rw-rw-r--  7 scott  wheel  0 19 Jun 17:13:36 2004 a
> 52 -rw-rw-r--  1 scott  wheel  0 19 Jun 17:13:36 2004 k

> Most of those files (a,b,d,e,f,g,h) are hard-linked to each other - so th=
ey
> definitely share the same timestamp.  i,j,k were created with 'touch -r a=
 i
> j k', so they should also have the same time.  c is different to make sure
> I didn't break the sort order when files *did* have different times.

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:

  int
  modcmp(const FTSENT *a, const FTSENT *b)
  {
      return (b->fts_statp->st_mtime - a->fts_statp->st_mtime);
  }

So, if these fields for two different files are exactly the same, the
resulting sorted list will have an undefined order for all other
fields.  And AFAICS, there's no way to tell ls: "first sort on time,
then on filename, then on size", etc.  This would make a nice addition
though. :)

Note also that the fts(3) functions use qsort(3), which isn't stable,
so you will not always get the order in which these entries are listed
in the directory itself.

------------8B1A31B42C89F112
Content-Type: application/pgp-signature

-----BEGIN PGP MESSAGE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFA1JypsF6jCi4glqMRAv7AAKDf6zSW+r1Tu/Dp9g+aEI96Gx2ApwCg0q6O
8/6iM+SGAbDnYfQVFm8zUUE=
=TZ+x
-----END PGP MESSAGE-----

------------8B1A31B42C89F112--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?14210101.20040619220601>