Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 29 May 2005 19:44:43 +0300 (EEST)
From:      Giorgos Keramidas <keramida@FreeBSD.org>
To:        FreeBSD-gnats-submit@FreeBSD.org
Subject:   bin/81625: "sort by size" option for ls(1)
Message-ID:  <200505291644.j4TGih4m066001@orion.daedalusnetworks.priv>
Resent-Message-ID: <200505291650.j4TGo1KK064825@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         81625
>Category:       bin
>Synopsis:       "sort by size" option for ls(1)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Sun May 29 16:50:01 GMT 2005
>Closed-Date:
>Last-Modified:
>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:
>Unformatted:



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