From 99a2093aa5adcd2cfd2e9f9985142318d188b080 Mon Sep 17 00:00:00 2001 From: dd Date: Fri, 3 Jun 2005 11:05:58 +0000 Subject: 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 , keramida --- bin/ls/cmp.c | 18 ++++++++++++++++++ bin/ls/extern.h | 2 ++ bin/ls/ls.1 | 10 ++++++---- bin/ls/ls.c | 21 +++++++++++++++------ bin/ls/util.c | 4 ++-- 5 files changed, 43 insertions(+), 12 deletions(-) (limited to 'bin/ls') diff --git a/bin/ls/cmp.c b/bin/ls/cmp.c index 903da8b..37b4a75 100644 --- a/bin/ls/cmp.c +++ b/bin/ls/cmp.c @@ -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)); +} diff --git a/bin/ls/extern.h b/bin/ls/extern.h index 52b6a85..02a61f5 100644 --- a/bin/ls/extern.h +++ b/bin/ls/extern.h @@ -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 *); diff --git a/bin/ls/ls.1 b/bin/ls/ls.1 index 314b5a2..9dff143 100644 --- a/bin/ls/ls.1 +++ b/bin/ls/ls.1 @@ -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. diff --git a/bin/ls/ls.c b/bin/ls/ls.c index 0130ade..d8400ee 100644 --- a/bin/ls/ls.c +++ b/bin/ls/ls.c @@ -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; } diff --git a/bin/ls/util.c b/bin/ls/util.c index 1c955cc..914b030 100644 --- a/bin/ls/util.c +++ b/bin/ls/util.c @@ -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); -- cgit v1.1