From owner-freebsd-bugs@FreeBSD.ORG Sun May 29 16:50:02 2005 Return-Path: X-Original-To: freebsd-bugs@hub.freebsd.org Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id B73F416A41C for ; Sun, 29 May 2005 16:50:02 +0000 (GMT) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21]) by mx1.FreeBSD.org (Postfix) with ESMTP id 2FC3943D1F for ; Sun, 29 May 2005 16:50:02 +0000 (GMT) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.13.3/8.13.3) with ESMTP id j4TGo2Wp064826 for ; Sun, 29 May 2005 16:50:02 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.13.3/8.13.1/Submit) id j4TGo1KK064825; Sun, 29 May 2005 16:50:01 GMT (envelope-from gnats) Resent-Date: Sun, 29 May 2005 16:50:01 GMT Resent-Message-Id: <200505291650.j4TGo1KK064825@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Giorgos Keramidas Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 4F1FC16A41C for ; 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 ; 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 ; 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 ; 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 To: FreeBSD-gnats-submit@FreeBSD.org X-Send-Pr-Version: 3.113 Cc: Subject: bin/81625: "sort by size" option for ls(1) X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: Giorgos Keramidas List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 29 May 2005 16:50:02 -0000 >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 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: