Add the -S option to sort files by size. NetBSD and OpenBSD already

have this option with identical semantics (sorting large files first).
-r can be used to reverse the sort if that is desired.

PR:		81625
Submitted by:	Kostas Blekos <mplekos@physics.upatras.gr>, keramida
This commit is contained in:
Dima Dorfman 2005-06-03 11:05:58 +00:00
parent 0fe9e21bd9
commit 71b8b74887
5 changed files with 43 additions and 12 deletions

View file

@ -139,3 +139,21 @@ revstatcmp(const FTSENT *a, const FTSENT *b)
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));
}

View file

@ -38,6 +38,8 @@ int namecmp(const FTSENT *, const FTSENT *);
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 *);

View file

@ -32,7 +32,7 @@
.\" @(#)ls.1 8.7 (Berkeley) 7/29/94
.\" $FreeBSD$
.\"
.Dd January 11, 2005
.Dd June 2, 2005
.Dt LS 1
.Os
.Sh NAME
@ -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,9 @@ and
options.
.It Fl R
Recursively list subdirectories encountered.
.It Fl S
Sort by size (largest file first) before sorting the operands by
lexicographical order.
.It Fl T
When used with the
.Fl l
@ -221,8 +224,7 @@ the character
.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.

View file

@ -127,6 +127,7 @@ static int f_singlecol; /* use single column output */
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,8 +180,8 @@ main(int argc, char *argv[])
f_listdot = 1;
fts_options = FTS_PHYSICAL;
while ((ch = getopt(argc, argv, "1ABCFGHLPRTWZabcdfghiklmnopqrstuwx"))
!= -1) {
while ((ch = getopt(argc, argv,
"1ABCFGHLPRSTWZabcdfghiklmnopqrstuwx")) != -1) {
switch (ch) {
/*
* The -1, -C, -x and -l options all override each other so
@ -298,6 +299,9 @@ main(int argc, char *argv[])
case 't':
f_timesort = 1;
break;
case 'S':
f_sizesort = 1;
break;
case 'W':
f_whiteout = 1;
break;
@ -360,11 +364,12 @@ main(int argc, char *argv[])
#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
@ -397,21 +402,25 @@ main(int argc, char *argv[])
}
/* Select a sort function. */
if (f_reversesort) {
if (!f_timesort)
if (!f_timesort && !f_sizesort)
sortfcn = revnamecmp;
else if (f_accesstime)
sortfcn = revacccmp;
else if (f_statustime)
sortfcn = revstatcmp;
else if (f_sizesort)
sortfcn = revsizecmp;
else /* Use modification time. */
sortfcn = revmodcmp;
} else {
if (!f_timesort)
if (!f_timesort && !f_sizesort)
sortfcn = namecmp;
else if (f_accesstime)
sortfcn = acccmp;
else if (f_statustime)
sortfcn = statcmp;
else if (f_sizesort)
sortfcn = sizecmp;
else /* Use modification time. */
sortfcn = modcmp;
}

View file

@ -222,9 +222,9 @@ usage(void)
{
(void)fprintf(stderr,
#ifdef COLORLS
"usage: ls [-ABCFGHLPRTWZabcdfghiklmnopqrstuwx1]"
"usage: ls [-ABCFGHLPRSTWZabcdfghiklmnopqrstuwx1]"
#else
"usage: ls [-ABCFHLPRTWZabcdfghiklmnopqrstuwx1]"
"usage: ls [-ABCFHLPRSTWZabcdfghiklmnopqrstuwx1]"
#endif
" [file ...]\n");
exit(1);