bug-coreutils
[Top][All Lists]
Advanced

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

Re: [patch #3596] Sort directories before files in "ls"


From: Francesco Montorsi
Subject: Re: [patch #3596] Sort directories before files in "ls"
Date: Mon, 05 Dec 2005 19:09:39 +0100
User-agent: Mozilla Thunderbird 1.0.7-1.1.fc4 (X11/20050929)

Hi,

Eric Blake wrote:
Your proposed patch treats directory sorts as equal with all other sorts,
meaning that there is no way to group directories first, but then sort
directories by size and files by size.  I envision this feature as
independent from --sort, that when it is enabled, directories are always
sorted first, but that then the two groups (dirs and non-dirs) can be
additionally sorted by whatever --sort option is in effect.  That also means
that in a --reverse sort, I think directories should still be listed first,
not last.
I checked out coreutils CVS and I looked to ls.c code to try to make a patch for this option, before I found out that a patch for this was already submitted ;) I agree with Eric that this option shouldn't be another --sort= possibility but rather a separed option.

I hope the author of the patch will find time to look into this again, since some time elapsed since he submitted the patch.

If it can help, this is the 'summary' of the changes I found to be required, taken from my notepad:

1) add a new global option like:

+    /* True means that directories are grouped first in the list.
+       Can be set with --group-dirs. */
+    static bool group_dirs;

   if the option is going to be named --group-dirs


2) add option-detection code and update print_usage


3) add at the beginning of sort_files(), a possible call to group_dirs_first() which moves the files[i].filetype == arg_directory to the beginning of the list and then returns the number of directories found. sort_files() then does two different quicksorts() on the two groups of entries of the files[] array: one from 0 to the value returned by group_dirs_first() and one from the value returned by group_dirs_first() to files_index.

In this way, both the folders and the files are sorted with the selected sortkey, but this is done separately and thus folders are still listed at beginning and files at the end.


4) write group_dirs_first(): it is a qsort call like

   qsort (files, files_index, sizeof *files, sort_folders);

with

static inline int
sort_folders (struct fileinfo const *a, struct fileinfo const *b)
{
  bool a_isfolder = a.filetype == arg_directory;
  bool b_isfolder = b.filetype == arg_directory;
  if (a_isfolder && !b_isfolder)
     return -1;        // a goes before b
  else if (!a_isfolder && b_isfolder)
     return 1;          // b goes before a

  /* a and b are both files or both folders;
     will be sorted later using the user-selected sortkey */
  return 0;
}

I think this is the best way to handle such option: a pre-sort and then two sorts on the two groups of files created.

Francesco Montorsi





reply via email to

[Prev in Thread] Current Thread [Next in Thread]