+static int
+compare_names (struct name const *n1, struct name const *n2)
+{
+ int found_diff = WASFOUND(n2) - WASFOUND(n1);
+ return found_diff ? found_diff : strcmp (n1->name, n2->name);
+}
+\f
+/* Add all the dirs under NAME, which names a directory, to the namelist.
+ If any of the files is a directory, recurse on the subdirectory.
+ DEVICE is the device not to leave, if the -l option is specified. */
+
+static void
+add_hierarchy_to_namelist (struct name *name, dev_t device)
+{
+ char *file_name = name->name;
+ char *buffer = get_directory_contents (file_name, device);
+
+ if (! buffer)
+ name->dir_contents = "\0\0\0\0";
+ else
+ {
+ size_t name_length = name->length;
+ size_t allocated_length = (name_length >= NAME_FIELD_SIZE
+ ? name_length + NAME_FIELD_SIZE
+ : NAME_FIELD_SIZE);
+ char *namebuf = xmalloc (allocated_length + 1);
+ /* FIXME: + 2 above? */
+ char *string;
+ size_t string_length;
+ int change_dir = name->change_dir;
+
+ name->dir_contents = buffer;
+ strcpy (namebuf, file_name);
+ if (! ISSLASH (namebuf[name_length - 1]))
+ {
+ namebuf[name_length++] = '/';
+ namebuf[name_length] = '\0';
+ }
+
+ for (string = buffer; *string; string += string_length + 1)
+ {
+ string_length = strlen (string);
+ if (*string == 'D')