Subject: Patches for ls to add -S flag to sort by size
To: netbsd-current-users <current-users@sun-lamp.cs.berkeley.edu>
From: Thomas Eberhardt <thomas@mathematik.uni-Bremen.de>
List: current-users
Date: 11/23/1993 21:27:52
Here are some patches to add a -S flag to ls(1) to enable sort by size.
I find this sometimes useful.

*** bin/ls/cmp.c-	Sun Nov  7 08:17:52 1993
--- bin/ls/cmp.c	Sun Nov 21 08:12:58 1993
***************
*** 101,103 ****
--- 101,117 ----
  {
  	return (a->fts_statp->st_ctime - b->fts_statp->st_ctime);
  }
+ 
+ int
+ sizecmp(a, b)
+ 	const FTSENT *a, *b;
+ {
+ 	return (b->fts_statp->st_size - a->fts_statp->st_size);
+ }
+ 
+ int
+ revsizecmp(a, b)
+ 	const FTSENT *a, *b;
+ {
+ 	return (a->fts_statp->st_size - b->fts_statp->st_size);
+ }
*** bin/ls/extern.h-	Sun Nov  7 08:17:53 1993
--- bin/ls/extern.h	Sun Nov 21 08:13:32 1993
***************
*** 42,47 ****
--- 42,49 ----
  int revnamecmp __P((const FTSENT *, const FTSENT *));
  int statcmp __P((const FTSENT *, const FTSENT *));
  int revstatcmp __P((const FTSENT *, const FTSENT *));
+ int sizecmp __P((const FTSENT *, const FTSENT *));
+ int revsizecmp __P((const FTSENT *, const FTSENT *));
  
  void err __P((int, const char *, ...));
  void prcopy __P((char *, char *, int));
*** bin/ls/ls.1-	Sun Nov  7 08:17:53 1993
--- bin/ls/ls.1	Tue Nov 23 21:21:59 1993
***************
*** 43,49 ****
  .Nd list directory contents.
  .Sh SYNOPSIS
  .Nm ls
! .Op Fl CFRacdilqrstu1
  .Op Ar file ...
  .Sh DESCRIPTION
  For each operand that names a
--- 43,49 ----
  .Nd list directory contents.
  .Sh SYNOPSIS
  .Nm ls
! .Op Fl CFRSacdilqrstu1
  .Op Ar file ...
  .Sh DESCRIPTION
  For each operand that names a
***************
*** 90,95 ****
--- 90,97 ----
  rather than the link itself.
  .It Fl R
  Recursively list subdirectories encountered.
+ .It Fl S
+ Sort by size (largest one first).
  .It Fl T
  Display complete time information for the file, including
  month, day, hour, minute, second, and year.
***************
*** 122,128 ****
  the character `?'; 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.
  .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.
--- 124,130 ----
  the character `?'; 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 smallest or oldest entries first.
  .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.
*** bin/ls/ls.c-	Sun Nov  7 08:17:54 1993
--- bin/ls/ls.c	Tue Nov 23 21:13:15 1993
***************
*** 70,77 ****
--- 70,82 ----
  static void (*printfcn) __P((DISPLAY *));
  static int (*sortfcn) __P((const FTSENT *, const FTSENT *));
  
+ #define BY_NAME	0
+ #define BY_SIZE	1
+ #define BY_TIME	2
+ 
  long blocksize;			/* block size units */
  int termwidth = 80;		/* default terminal width */
+ int sortkey;			/* sort key, defaults to sort by name */
  
  /* flags */
  int f_accesstime;		/* use time of last access */
***************
*** 91,97 ****
  int f_size;			/* list size in short listing */
  int f_statustime;		/* use time of last mode change */
  int f_dirname;			/* if precede with directory name */
- int f_timesort;			/* sort by time vice name */
  int f_type;			/* add type character for non-regular files */
  
  int
--- 96,101 ----
***************
*** 123,129 ****
  		f_listdot = 1;
  
  	fts_options = FTS_PHYSICAL;
! 	while ((ch = getopt(argc, argv, "1ACFLRTacdfgikloqrstu")) != EOF) {
  		switch (ch) {
  		/*
  		 * The -1, -C and -l options all override each other so shell
--- 127,133 ----
  		f_listdot = 1;
  
  	fts_options = FTS_PHYSICAL;
! 	while ((ch = getopt(argc, argv, "1ACFLRSTacdfgikloqrstu")) != EOF) {
  		switch (ch) {
  		/*
  		 * The -1, -C and -l options all override each other so shell
***************
*** 192,197 ****
--- 196,204 ----
  		case 'r':
  			f_reversesort = 1;
  			break;
+ 		case 'S':
+ 			sortkey = BY_SIZE;
+ 			break;
  		case 's':
  			f_size = 1;
  			break;
***************
*** 199,205 ****
  			f_sectime = 1;
  			break;
  		case 't':
! 			f_timesort = 1;
  			break;
  		default:
  		case '?':
--- 206,212 ----
  			f_sectime = 1;
  			break;
  		case 't':
! 			sortkey = BY_TIME;
  			break;
  		default:
  		case '?':
***************
*** 210,219 ****
  	argv += optind;
  
  	/*
! 	 * If not -F, -i, -l, -s or -t options, don't require stat
  	 * information.
  	 */
! 	if (!f_inode && !f_longform && !f_size && !f_timesort && !f_type)
  		fts_options |= FTS_NOSTAT;
  
  	/*
--- 217,227 ----
  	argv += optind;
  
  	/*
! 	 * If not -F, -i, -l, -S, -s or -t options, don't require stat
  	 * information.
  	 */
! 	if (!f_inode && !f_longform && !f_size && !f_type &&
! 	    sortkey == BY_NAME)
  		fts_options |= FTS_NOSTAT;
  
  	/*
***************
*** 231,253 ****
  
  	/* Select a sort function. */
  	if (f_reversesort) {
! 		if (!f_timesort)
  			sortfcn = revnamecmp;
! 		else if (f_accesstime)
! 			sortfcn = revacccmp;
! 		else if (f_statustime)
! 			sortfcn = revstatcmp;
! 		else /* Use modification time. */
! 			sortfcn = revmodcmp;
  	} else {
! 		if (!f_timesort)
  			sortfcn = namecmp;
! 		else if (f_accesstime)
! 			sortfcn = acccmp;
! 		else if (f_statustime)
! 			sortfcn = statcmp;
! 		else /* Use modification time. */
! 			sortfcn = modcmp;
  	}
  
  	/* Select a print function. */
--- 239,277 ----
  
  	/* Select a sort function. */
  	if (f_reversesort) {
! 		switch (sortkey) {
! 		case BY_NAME:
  			sortfcn = revnamecmp;
! 			break;
! 		case BY_SIZE:
! 			sortfcn = revsizecmp;
! 			break;
! 		case BY_TIME:
! 			if (f_accesstime)
! 				sortfcn = revacccmp;
! 			else if (f_statustime)
! 				sortfcn = revstatcmp;
! 			else /* Use modification time. */
! 				sortfcn = revmodcmp;
! 			break;
! 		}
  	} else {
! 		switch (sortkey) {
! 		case BY_NAME:
  			sortfcn = namecmp;
! 			break;
! 		case BY_SIZE:
! 			sortfcn = sizecmp;
! 			break;
! 		case BY_TIME:
! 			if (f_accesstime)
! 				sortfcn = acccmp;
! 			else if (f_statustime)
! 				sortfcn = statcmp;
! 			else /* Use modification time. */
! 				sortfcn = modcmp;
! 			break;
! 		}
  	}
  
  	/* Select a print function. */
*** bin/ls/util.c-	Sun Nov  7 08:17:56 1993
--- bin/ls/util.c	Tue Nov 23 21:08:19 1993
***************
*** 66,72 ****
  void
  usage()
  {
! 	(void)fprintf(stderr, "usage: ls [-1ACFLRTacdfiklqrstu] [file ...]\n");
  	exit(1);
  }
  
--- 66,73 ----
  void
  usage()
  {
! 	(void)fprintf(stderr,
! 	    "usage: ls [-1ACFLRSTacdfiklqrstu] [file ...]\n");
  	exit(1);
  }
  
-- 
thomas@mathematik.uni-Bremen.de | Centrum für Complexe Systeme & Visualisierung
Thomas Eberhardt                | Universität Bremen, FB 3, Bibliothekstr. 1
Kölner Str. 4, D-28327 Bremen   | D-28359 Bremen, Germany
Home Phone: +49 421 472527      | FAX: +49 421 218-4236, Office: 218-4823

------------------------------------------------------------------------------