ls patch

Gerrit Germis <[email protected]>
Newsgroups gmane.comp.gnu.fileutils.bugs
Message-ID <[email protected]>
Hi,

Here's a patch for ls, to have it sort directories before anything else.. the 
new option is -e.. I could have sworn it was in an older version of 
fileutils, but I'm probably dreaming out loud :)

[11:21][Lurch]$ /bin/ls -le
total 8
drwxr-xr-x    2 lurch    users        4096 Oct 27 11:21 adir1
drwxr-xr-x    2 lurch    users        4096 Oct 27 11:21 zdir2
-rw-r--r--    1 lurch    users           0 Oct 27 11:21 afile1
-rw-r--r--    1 lurch    users           0 Oct 27 11:21 zfile2
[11:21][Lurch]$ /bin/ls -l
total 8
drwxr-xr-x    2 lurch    users        4096 Oct 27 11:21 adir1
-rw-r--r--    1 lurch    users           0 Oct 27 11:21 afile1
drwxr-xr-x    2 lurch    users        4096 Oct 27 11:21 zdir2
-rw-r--r--    1 lurch    users           0 Oct 27 11:21 zfile2
[11:21][Lurch]$ 

Regards,
Gerrit
fileutils-4.1-ls.patch (text/x-diff, 3.1 KB)
--- fileutils-4.1/src/ls.c	2001-04-29 11:42:47.000000000 +0200
+++ fileutils-4.1-mod/src/ls.c	2002-08-12 18:25:25.000000000 +0200
@@ -297,6 +297,10 @@
 				    const struct fileinfo *file2));
 static int rev_cmp_version PARAMS ((const struct fileinfo *file2,
 				    const struct fileinfo *file1));
+static int compare_directories PARAMS ((const struct fileinfo *file1,
+			                const struct fileinfo *file2));
+static int rev_cmp_directories PARAMS ((const struct fileinfo *file2,
+                                        const struct fileinfo *file1));
 static int decode_switches PARAMS ((int argc, char **argv));
 static int file_interesting PARAMS ((const struct dirent *next));
 static uintmax_t gobble_file PARAMS ((const char *name, enum filetype type,
@@ -431,7 +435,8 @@
     sort_extension,		/* -X */
     sort_time,			/* -t */
     sort_size,			/* -S */
-    sort_version		/* -v */
+    sort_version,		/* -v */
+    sort_dirs			/* -e */
   };
 
 static enum sort_type sort_type;
@@ -1076,7 +1081,7 @@
     }
 
   while ((c = getopt_long (argc, argv,
-			   "abcdfghiklmnopqrstuvw:xABCDFGHI:LNQRST:UX1",
+			   "abcdefghiklmnopqrstuvw:xABCDFGHI:LNQRST:UX1",
 			   long_options, NULL)) != -1)
     {
       switch (c)
@@ -1101,6 +1106,10 @@
 	  immediate_dirs = 1;
 	  break;
 
+	case 'e':
+	  sort_type = sort_dirs;
+	  break;
+
 	case 'f':
 	  /* Same as enabling -a -U and disabling -l -s.  */
 	  all_files = 1;
@@ -2169,6 +2178,9 @@
     case sort_version:
       func = sort_reverse ? rev_cmp_version : compare_version;
       break;
+    case sort_dirs:
+      func = sort_reverse ? rev_cmp_directories : compare_directories;
+      break;
     default:
       abort ();
     }
@@ -2317,6 +2329,33 @@
   return cmp;
 }
 
+static int
+compare_directories(const struct fileinfo *file1, const struct fileinfo *file2)
+{
+  if (file1->filetype == directory && file2->filetype == directory)
+	return strcoll(file1->name, file2->name);
+  if (file1->filetype == directory)
+        return -1;
+  if (file2->filetype == directory)
+        return 1;
+
+  return strcoll(file1->name, file2->name);
+}
+
+static int
+rev_cmp_directories(const struct fileinfo *file2, const struct fileinfo *file1)
+{
+  if (file1->filetype == directory && file2->filetype == directory)
+	return strcoll(file1->name, file2->name);
+  if (file1->filetype == directory)
+        return -1;
+  if (file2->filetype == directory)
+        return 1;
+
+  return strcoll(file1->name, file2->name);
+}
+
+
 /* List all the files now in the table.  */
 
 static void
@@ -3267,6 +3306,7 @@
       --color[=WHEN]         control whether color is used to distinguish file\n\
                                types.  WHEN may be `never', `always', or `auto'\n\
   -d, --directory            list directory entries instead of contents\n\
+  -e                         sort directories first, then sort files\n\
   -D, --dired                generate output designed for Emacs' dired mode\n\
   -f                         do not sort, enable -aU, disable -lst\n\
   -F, --classify             append indicator (one of */=@|) to entries\n\
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.