From owner-freebsd-hackers@FreeBSD.ORG Sat Jun 19 20:07:19 2004 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 1149716A4CE for ; Sat, 19 Jun 2004 20:07:19 +0000 (GMT) Received: from tensor.xs4all.nl (tensor.xs4all.nl [194.109.160.97]) by mx1.FreeBSD.org (Postfix) with ESMTP id 9C8D343D41 for ; Sat, 19 Jun 2004 20:07:18 +0000 (GMT) (envelope-from dimitry@andric.com) Received: from kilgore.dim (kilgore.dim [192.168.0.3]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by tensor.xs4all.nl (Postfix) with ESMTP id 709BF2284A; Sat, 19 Jun 2004 22:06:08 +0200 (CEST) Date: Sat, 19 Jun 2004 22:06:01 +0200 From: Dimitry Andric X-Mailer: The Bat! (v2.12 Beta/1) Business X-Priority: 3 (Normal) Message-ID: <14210101.20040619220601@andric.com> To: Scott Mitchell In-Reply-To: <20040619193545.GC462@tuatara.fishballoon.org> References: <20040619175007.GB462@tuatara.fishballoon.org> <414787887.20040619210137@andric.com> <20040619193545.GC462@tuatara.fishballoon.org> MIME-Version: 1.0 Content-Type: multipart/signed; protocol="application/pgp-signature"; micalg="pgp-sha1"; boundary="----------8B1A31B42C89F112" cc: freebsd-hackers@freebsd.org Subject: Re: /bin/ls sorting bug? X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 19 Jun 2004 20:07:19 -0000 ------------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--