pkgsrc-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[patch] colorls: put directories in front of other files with -D



The below patch adds a new command-line switch (-D) to colorls(1), which causes it to sort directories before other files.

E.g.:

Normal listing, sort by name
| /tmp/test $ colorls -l
| total 0
| -rw-r--r--  1 fstd  wheel  0 Sep  8 01:08 a_file1
| drwxr-xr-x  2 fstd  wheel  0 Sep  8 01:08 b_dir1
| -rw-r--r--  1 fstd  wheel  0 Sep  8 01:08 c_file2
| drwxr-xr-x  2 fstd  wheel  0 Sep  8 01:08 d_dir2

Still sort by name, but directories come first
| /tmp/test $ colorls -lD
| total 0
| drwxr-xr-x  2 fstd  wheel  0 Sep  8 01:08 b_dir1
| drwxr-xr-x  2 fstd  wheel  0 Sep  8 01:08 d_dir2
| -rw-r--r--  1 fstd  wheel  0 Sep  8 01:08 a_file1
| -rw-r--r--  1 fstd  wheel  0 Sep  8 01:08 c_file2

Still come first, but sort order is reversed
| /tmp/test $ colorls -lDr
| total 0
| drwxr-xr-x  2 fstd  wheel  0 Sep  8 01:08 d_dir2
| drwxr-xr-x  2 fstd  wheel  0 Sep  8 01:08 b_dir1
| -rw-r--r--  1 fstd  wheel  0 Sep  8 01:08 c_file2
| -rw-r--r--  1 fstd  wheel  0 Sep  8 01:08 a_file1


Cheers,

Timo Buhrmester


------8<-------------------------------------------------
--- colorls.1.orig	1996-12-22 00:40:57.000000000 +0100
+++ colorls.1	2015-09-08 01:24:28.000000000 +0200
@@ -78,6 +78,8 @@
 Always set for the super-user.
 .It Fl C
 Force multi-column output; this is the default when output is to a terminal.
+.It Fl D
+List directories before other files.
 .It Fl F
 Display a slash (/) immediately after each pathname
 that is a directory, an asterisk (*) after each that is

--- ls.c.orig	2015-09-08 00:51:22.000000000 +0200
+++ ls.c	2015-09-08 00:48:28.000000000 +0200
@@ -114,6 +114,7 @@
 #ifndef BSD4_4_LITE
 int f_whiteout;			/* show whiteout entries */
 #endif
+int f_dirsfirst;		/* show directories before other files */
 
 int rval;
 
@@ -152,12 +153,12 @@
 
 	fts_options = FTS_PHYSICAL;
 #ifdef BSD4_4_LITE
-	while ((ch = getopt(argc, argv, "1ACFGLRTacdfgikloqrstu")) != EOF) {
+	while ((ch = getopt(argc, argv, "1ACFGLRTacdfgikloqrstuD")) != EOF) {
 #else
 #ifndef __sun
-	while ((ch = getopt(argc, argv, "1ACFGLRTWacdfgikloqrstu")) != EOF) {
+	while ((ch = getopt(argc, argv, "1ACFGLRTWacdfgikloqrstuD")) != EOF) {
 #else
-	while ((ch = getopt(argc, argv, "1ACFGLRTWacdfgiklqrstu")) != EOF) {
+	while ((ch = getopt(argc, argv, "1ACFGLRTWacdfgiklqrstuD")) != EOF) {
 #endif
 #endif
 		switch (ch) {
@@ -247,6 +248,9 @@
 			f_whiteout = 1;
 			break;
 #endif
+		case 'D':
+			f_dirsfirst = 1;
+			break;
 		default:
 		case '?':
 			usage();
@@ -551,7 +555,9 @@
 /*
  * Ordering for mastercmp:
  * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories
- * as larger than directories.  Within either group, use the sort function.
+ * as larger than directories; otherwise, if -D (aka f_dirsfirst) was given,
+ * return non-directories as *smaller* than directories so as to group them
+ * at the beginning.  Within either group, use the sort function.
  * All other levels use the sort function.  Error entries remain unsorted.
  */
 static int
@@ -574,16 +580,16 @@
 	if (a_info == FTS_NS || b_info == FTS_NS)
 		return (namecmp(*a, *b));
 
-	if (a_info == b_info)
-		return (sortfcn(*a, *b));
+	int rootlvl = (*a)->fts_level == FTS_ROOTLEVEL;
 
-	if ((*a)->fts_level == FTS_ROOTLEVEL)
-		if (a_info == FTS_D)
-			return (1);
-		else if (b_info == FTS_D)
-			return (-1);
-		else
-			return (sortfcn(*a, *b));
-	else
-		return (sortfcn(*a, *b));
+	if (rootlvl || f_dirsfirst) {
+		if (a_info != FTS_D || b_info != FTS_D) {
+			if (a_info == FTS_D)
+				return rootlvl ? 1 : -1;
+			else if (b_info == FTS_D)
+				return rootlvl ? -1 : 1;
+		}
+	}
+
+	return (sortfcn(*a, *b));
 }



Home | Main Index | Thread Index | Old Index