+/* A comparison function for sorting names. Put found names last;
+ break ties by string comparison. */
+
+static int
+compare_names (struct name const *n1, struct name const *n2)
+{
+ int found_diff = n2->found - n1->found;
+ return found_diff ? found_diff : strcmp (n1->name, n2->name);
+}
+\f
+/* Add all the dirs in PATH, which is a directory, to the namelist.
+ If any of the files is a directory, recurse on the subdirectory.
+ CHANGE_DIR is the number of the directory that PATH is relative to.
+ DEVICE is the device not to leave, if the -l option is specified. */
+
+static void
+add_hierarchy_to_namelist (char *path, int change_dir, dev_t device)
+{
+ char *buffer = get_directory_contents (path, device);
+
+ {
+ struct name *name;
+
+ for (name = namelist; name; name = name->next)
+ if (strcmp (name->name, path) == 0)
+ break;
+ if (name)
+ name->dir_contents = buffer ? buffer : "\0\0\0\0";
+ }
+
+ if (buffer)
+ {
+ size_t name_length = strlen (path);
+ size_t allocated_length = (name_length >= NAME_FIELD_SIZE
+ ? name_length + NAME_FIELD_SIZE
+ : NAME_FIELD_SIZE);
+ char *name_buffer = xmalloc (allocated_length + 1);
+ /* FIXME: + 2 above? */
+ char *string;
+ size_t string_length;
+
+ strcpy (name_buffer, path);
+ if (name_buffer[name_length - 1] != '/')
+ {
+ name_buffer[name_length++] = '/';
+ name_buffer[name_length] = '\0';
+ }
+
+ for (string = buffer; *string; string += string_length + 1)
+ {
+ string_length = strlen (string);
+ if (*string == 'D')
+ {
+ if (name_length + string_length >= allocated_length)
+ {
+ while (name_length + string_length >= allocated_length)
+ allocated_length += NAME_FIELD_SIZE;
+ name_buffer = xrealloc (name_buffer, allocated_length + 1);
+ }
+ strcpy (name_buffer + name_length, string + 1);
+ addname (name_buffer, change_dir);
+ if (*string == 'D')
+ add_hierarchy_to_namelist (name_buffer, change_dir, device);
+ }
+ }
+
+ free (name_buffer);
+ }
+}
+\f
+/* Collect all the names from argv[] (or whatever), expand them into a
+ directory tree, and sort them. This gets only subdirectories, not
+ all files. */