From keramida@ceid.upatras.gr  Sun May 29 16:44:46 2005
Return-Path: <keramida@ceid.upatras.gr>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 4F1FC16A41C
	for <FreeBSD-gnats-submit@freebsd.org>; Sun, 29 May 2005 16:44:46 +0000 (GMT)
	(envelope-from keramida@ceid.upatras.gr)
Received: from aiolos.otenet.gr (aiolos.otenet.gr [195.170.0.23])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 90DFE43D1F
	for <FreeBSD-gnats-submit@freebsd.org>; Sun, 29 May 2005 16:44:45 +0000 (GMT)
	(envelope-from keramida@ceid.upatras.gr)
Received: from orion.daedalusnetworks.priv (aris.bedc.ondsl.gr [62.103.39.226])
	by aiolos.otenet.gr (8.13.4/8.13.4/Debian-1) with SMTP id j4TGihUU026305
	for <FreeBSD-gnats-submit@freebsd.org>; Sun, 29 May 2005 19:44:43 +0300
Received: from orion.daedalusnetworks.priv (orion [127.0.0.1])
	by orion.daedalusnetworks.priv (8.13.3/8.13.3) with ESMTP id j4TGihd3066005
	for <FreeBSD-gnats-submit@freebsd.org>; Sun, 29 May 2005 19:44:43 +0300 (EEST)
	(envelope-from keramida@orion.daedalusnetworks.priv)
Received: (from keramida@localhost)
	by orion.daedalusnetworks.priv (8.13.3/8.13.3/Submit) id j4TGih4m066001;
	Sun, 29 May 2005 19:44:43 +0300 (EEST)
	(envelope-from keramida)
Message-Id: <200505291644.j4TGih4m066001@orion.daedalusnetworks.priv>
Date: Sun, 29 May 2005 19:44:43 +0300 (EEST)
From: Giorgos Keramidas <keramida@freebsd.org>
Reply-To: Giorgos Keramidas <keramida@freebsd.org>
To: FreeBSD-gnats-submit@freebsd.org
Subject: "sort by size" option for ls(1)
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         81625
>Category:       bin
>Synopsis:       [patch] "sort by size" option for ls(1)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Sun May 29 16:50:01 GMT 2005
>Closed-Date:    Fri Jun 03 11:08:05 UTC 2005
>Last-Modified:  Fri Jun 03 11:08:05 UTC 2005
>Originator:     Giorgos Keramidas
>Release:        FreeBSD 6.0-CURRENT i386
>Organization:
>Environment:

System: FreeBSD orion.daedalusnetworks.priv 6.0-CURRENT FreeBSD
6.0-CURRENT #0: Fri May 27 17:31:45 EEST 2005
root@orion.daedalusnetworks.priv:/home/obj/usr/src/sys/ORION i386

>Description:

Kostas Blekos <mplekos@physics.upatras.gr> has mailed me with a patch
to ls(1) that allows sorting the output files by size:

% orion:/d/src/bin/ls$ ./ls -lS ls*
% -rw-rw-r--  1 keramida  keramida   3214 Mar 21 16:44 ls.h
% -rw-rw-r--  1 keramida  keramida   6028 May 18 17:37 ls.1.gz
% -rw-rw-r--  1 keramida  keramida  15564 May 18 17:37 ls.1
% -rw-rw-r--  1 keramida  keramida  21676 May 18 17:27 ls.c
% -rw-rw-r--  1 keramida  keramida  25924 May 18 17:27 ls.o
% -rwxrwxr-x  1 keramida  keramida  57206 May 18 17:38 ls
% orion:/d/src/bin/ls$ ./ls -rlS ls*
% -rwxrwxr-x  1 keramida  keramida  57206 May 18 17:38 ls
% -rw-rw-r--  1 keramida  keramida  25924 May 18 17:27 ls.o
% -rw-rw-r--  1 keramida  keramida  21676 May 18 17:27 ls.c
% -rw-rw-r--  1 keramida  keramida  15564 May 18 17:37 ls.1
% -rw-rw-r--  1 keramida  keramida   6028 May 18 17:37 ls.1.gz
% -rw-rw-r--  1 keramida  keramida   3214 Mar 21 16:44 ls.h
% orion:/d/src/bin/ls$

The -S option is not exactly optimal, but there are so many option
letters that are taken by existing ls(1) features that there isn't
much of a choise, unless we modify ls to use something like -o for
picking a selection order:

	# ls -o size,mtime,name

>How-To-Repeat:
>Fix:

--- ls-sizesort.patch begins here ---
Index: cmp.c
===================================================================
RCS file: /home/ncvs/src/bin/ls/cmp.c,v
retrieving revision 1.16
diff -u -r1.16 cmp.c
--- cmp.c	10 Jan 2005 08:39:23 -0000	1.16
+++ cmp.c	18 May 2005 14:38:34 -0000
@@ -139,3 +139,19 @@
 
 	return (statcmp(b, a));
 }
+
+int
+sizecmp(const FTSENT *a, const FTSENT *b)
+{
+	if (b->fts_statp->st_size > a->fts_statp->st_size)
+		return (-1);
+	if (b->fts_statp->st_size < a->fts_statp->st_size)
+		return (1);
+	return (strcoll(a->fts_name, b->fts_name));
+}
+
+int
+revsizecmp(const FTSENT *a, const FTSENT *b)
+{
+	return (sizecmp(b, a));
+}
Index: extern.h
===================================================================
RCS file: /home/ncvs/src/bin/ls/extern.h,v
retrieving revision 1.23
diff -u -r1.23 extern.h
--- extern.h	2 May 2004 11:25:37 -0000	1.23
+++ extern.h	18 May 2005 14:26:57 -0000
@@ -38,6 +38,8 @@
 int	 revnamecmp(const FTSENT *, const FTSENT *);
 int	 statcmp(const FTSENT *, const FTSENT *);
 int	 revstatcmp(const FTSENT *, const FTSENT *);
+int	 sizecmp(const FTSENT *, const FTSENT *);
+int	 revsizecmp(const FTSENT *, const FTSENT *);
 
 void	 printcol(const DISPLAY *);
 void	 printlong(const DISPLAY *);
Index: ls.1
===================================================================
RCS file: /home/ncvs/src/bin/ls/ls.1,v
retrieving revision 1.86
diff -u -r1.86 ls.1
--- ls.1	13 Feb 2005 22:25:09 -0000	1.86
+++ ls.1	18 May 2005 14:37:53 -0000
@@ -40,7 +40,7 @@
 .Nd list directory contents
 .Sh SYNOPSIS
 .Nm
-.Op Fl ABCFGHLPRTWZabcdfghiklmnopqrstuwx1
+.Op Fl ABCFGHLPRSTWZabcdfghiklmnopqrstuwx1
 .Op Ar
 .Sh DESCRIPTION
 For each operand that names a
@@ -133,6 +133,8 @@
 options.
 .It Fl R
 Recursively list subdirectories encountered.
+.It Fl S
+Sort by size (before sorting
 .It Fl T
 When used with the
 .Fl l
@@ -221,8 +223,7 @@
 .Ql \&? ;
 this is the default when output is to a terminal.
 .It Fl r
-Reverse the order of the sort to get reverse
-lexicographical order or the oldest entries first.
+Reverse the order of the sort.
 .It Fl s
 Display the number of file system blocks actually used by each file, in units
 of 512 bytes, where partial units are rounded up to the next integer value.
Index: ls.c
===================================================================
RCS file: /home/ncvs/src/bin/ls/ls.c,v
retrieving revision 1.79
diff -u -r1.79 ls.c
--- ls.c	10 Jan 2005 08:39:23 -0000	1.79
+++ ls.c	18 May 2005 14:27:04 -0000
@@ -127,6 +127,7 @@
        int f_statustime;	/* use time of last mode change */
 static int f_stream;		/* stream the output, separate with commas */
 static int f_timesort;		/* sort by time vice name */
+static int f_sizesort;
        int f_type;		/* add type character for non-regular files */
 static int f_whiteout;		/* show whiteout entries */
        int f_label;		/* show MAC label */
@@ -179,7 +180,7 @@
 		f_listdot = 1;
 
 	fts_options = FTS_PHYSICAL;
- 	while ((ch = getopt(argc, argv, "1ABCFGHLPRTWZabcdfghiklmnopqrstuwx")) 
+ 	while ((ch = getopt(argc, argv, "1ABCFGHLPRSTWZabcdfghiklmnopqrstuwx"))
 	    != -1) {
 		switch (ch) {
 		/*
@@ -298,6 +299,9 @@
 		case 't':
 			f_timesort = 1;
 			break;
+		case 'S':
+			f_sizesort = 1;
+			break;
 		case 'W':
 			f_whiteout = 1;
 			break;
@@ -360,11 +364,11 @@
 #endif
 
 	/*
-	 * If not -F, -i, -l, -s or -t options, don't require stat
+	 * If not -F, -i, -l, -s, -S or -t options, don't require stat
 	 * information, unless in color mode in which case we do
 	 * need this to determine which colors to display.
 	 */
-	if (!f_inode && !f_longform && !f_size && !f_timesort && !f_type
+	if (!f_inode && !f_longform && !f_size && !f_timesort && !f_sizesort && !f_type
 #ifdef COLORLS
 	    && !f_color
 #endif
@@ -405,6 +409,7 @@
 			sortfcn = revstatcmp;
 		else		/* Use modification time. */
 			sortfcn = revmodcmp;
+		if (f_sizesort) sortfcn = revsizecmp;
 	} else {
 		if (!f_timesort)
 			sortfcn = namecmp;
@@ -414,6 +419,7 @@
 			sortfcn = statcmp;
 		else		/* Use modification time. */
 			sortfcn = modcmp;
+		if (f_sizesort) sortfcn = sizecmp;
 	}
 
 	/* Select a print function. */
--- ls-sizesort.patch ends here ---


>Release-Note:
>Audit-Trail:

From: Dima Dorfman <dd@freebsd.org>
To: Giorgos Keramidas <keramida@freebsd.org>
Cc: mplekos@physics.upatras.gr, FreeBSD-gnats-submit@freebsd.org
Subject: Re: bin/81625: "sort by size" option for ls(1)
Date: Wed, 1 Jun 2005 11:25:28 +0000

 --r5Pyd7+fXNt84Ff3
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: inline
 Content-Transfer-Encoding: quoted-printable
 
 Giorgos Keramidas <keramida@FreeBSD.org> wrote:
 > >Description:
 >=20
 > Kostas Blekos <mplekos@physics.upatras.gr> has mailed me with a patch
 > to ls(1) that allows sorting the output files by size:
 
 Cool, looks like a nice improvement. Some minor comments below. Part
 of the man page update seems to be missing, and I want to change the
 default polarity, but the rest are just minor style nits.
 
 > The -S option is not exactly optimal, but there are so many option
 > letters that are taken by existing ls(1) features that there isn't
 > much of a choise, unless we modify ls to use something like -o for
 > picking a selection order:
 
 -S is okay. OpenBSD and NetBSD use it for the same thing, except that
 their default is to sort the largest files first.
 
      -S      Sort by size, largest file first.
 
 Can we change the patch to do that? I'm not sure why they picked
 descending order, but compatibility would be good here.
 
 > RCS file: /home/ncvs/src/bin/ls/cmp.c,v
 > @@ -139,3 +139,19 @@
 > =20
 >  	return (statcmp(b, a));
 >  }
 > +
 > +int
 > +sizecmp(const FTSENT *a, const FTSENT *b)
 > +{
 > +	if (b->fts_statp->st_size > a->fts_statp->st_size)
 > +		return (-1);
 
 Style nit: Please add a blank line above the if. If a function has no
 local variables, there should still be a blank line after the brace.
 (See the usage() example in style(9).)
 
 > +int
 > +revsizecmp(const FTSENT *a, const FTSENT *b)
 > +{
 > +	return (sizecmp(b, a));
 
 As above
 
 > RCS file: /home/ncvs/src/bin/ls/ls.1,v
 > @@ -133,6 +133,8 @@
 >  options.
 >  .It Fl R
 >  Recursively list subdirectories encountered.
 > +.It Fl S
 > +Sort by size (before sorting
 
 This seems to have been cut off.
 
 > @@ -405,6 +409,7 @@
 >  			sortfcn =3D revstatcmp;
 >  		else		/* Use modification time. */
 >  			sortfcn =3D revmodcmp;
 > +		if (f_sizesort) sortfcn =3D revsizecmp;
 
 Is there a reason this condition can't be checked along with all the
 others in the if/else if block? Also, the predominant style in this
 file seems to be to put the body of the if on a new line below the
 conditional.
 
 > @@ -414,6 +419,7 @@
 >  			sortfcn =3D statcmp;
 >  		else		/* Use modification time. */
 >  			sortfcn =3D modcmp;
 > +		if (f_sizesort) sortfcn =3D sizecmp;
 
 As above
 
 --r5Pyd7+fXNt84Ff3
 Content-Type: application/pgp-signature
 Content-Disposition: inline
 
 -----BEGIN PGP SIGNATURE-----
 
 iD8DBQFCnZsnBzAFW2n65YIRAvekAJ0VeMsDPuxLns3kF4KhpKHWDucYngCdHPH6
 A8lH3moz/8NgF0JLjATjCOY=
 =72vr
 -----END PGP SIGNATURE-----
 
 --r5Pyd7+fXNt84Ff3--

From: Giorgos Keramidas <keramida@freebsd.org>
To: Dima Dorfman <dd@freebsd.org>
Cc: mplekos@physics.upatras.gr, FreeBSD-gnats-submit@freebsd.org
Subject: Re: bin/81625: "sort by size" option for ls(1)
Date: Wed, 1 Jun 2005 14:40:14 +0300

 On 2005-06-01 11:25, Dima Dorfman <dd@freebsd.org> wrote:
 > Giorgos Keramidas <keramida@FreeBSD.org> wrote:
 > > >Description:
 > >
 > > Kostas Blekos <mplekos@physics.upatras.gr> has mailed me with a patch
 > > to ls(1) that allows sorting the output files by size:
 >
 > Cool, looks like a nice improvement. Some minor comments below. Part
 > of the man page update seems to be missing, and I want to change the
 > default polarity, but the rest are just minor style nits.
 >
 > > The -S option is not exactly optimal, but there are so many option
 > > letters that are taken by existing ls(1) features that there isn't
 > > much of a choise, unless we modify ls to use something like -o for
 > > picking a selection order:
 >
 > -S is okay. OpenBSD and NetBSD use it for the same thing, except that
 > their default is to sort the largest files first.
 >
 >      -S      Sort by size, largest file first.
 >
 > Can we change the patch to do that? I'm not sure why they picked
 > descending order, but compatibility would be good here.
 
 Agreed.
 
 > > +
 > > +int
 > > +sizecmp(const FTSENT *a, const FTSENT *b)
 > > +{
 > > +	if (b->fts_statp->st_size > a->fts_statp->st_size)
 > > +		return (-1);
 >
 > Style nit: Please add a blank line above the if. If a function has no
 > local variables, there should still be a blank line after the brace.
 > (See the usage() example in style(9).)
 
 I know.  I just patched a local bin/ls and tried that it all works, so
 style changes like those you mentioned are perfectly fine :-)
 
State-Changed-From-To: open->closed 
State-Changed-By: dd 
State-Changed-When: Fri Jun 3 11:07:55 UTC 2005 
State-Changed-Why:  
Patch committed. Thanks! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=81625 
>Unformatted:
