+ }
+ namelist = 0;
+ namelast = 0;
+
+ if (same_order_option)
+ {
+ char *name;
+
+ while (name = name_next (1), name)
+ ERROR ((0, 0, _("%s: Not found in archive"),
+ quotearg_colon (name)));
+ }
+}
+\f
+/* Sorting name lists. */
+
+/* Sort linked LIST of names, of given LENGTH, using COMPARE to order
+ names. Return the sorted list. Apart from the type `struct name'
+ and the definition of SUCCESSOR, this is a generic list-sorting
+ function, but it's too painful to make it both generic and portable
+ in C. */
+
+static struct name *
+merge_sort (struct name *list, int length,
+ int (*compare) (struct name const*, struct name const*))
+{
+ struct name *first_list;
+ struct name *second_list;
+ int first_length;
+ int second_length;
+ struct name *result;
+ struct name **merge_point;
+ struct name *cursor;
+ int counter;
+
+# define SUCCESSOR(name) ((name)->next)
+
+ if (length == 1)
+ return list;
+
+ if (length == 2)
+ {
+ if ((*compare) (list, SUCCESSOR (list)) > 0)
+ {
+ result = SUCCESSOR (list);
+ SUCCESSOR (result) = list;
+ SUCCESSOR (list) = 0;
+ return result;
+ }
+ return list;
+ }
+
+ first_list = list;
+ first_length = (length + 1) / 2;
+ second_length = length / 2;
+ for (cursor = list, counter = first_length - 1;
+ counter;
+ cursor = SUCCESSOR (cursor), counter--)
+ continue;
+ second_list = SUCCESSOR (cursor);
+ SUCCESSOR (cursor) = 0;
+
+ first_list = merge_sort (first_list, first_length, compare);
+ second_list = merge_sort (second_list, second_length, compare);
+
+ merge_point = &result;
+ while (first_list && second_list)
+ if ((*compare) (first_list, second_list) < 0)
+ {
+ cursor = SUCCESSOR (first_list);
+ *merge_point = first_list;
+ merge_point = &SUCCESSOR (first_list);
+ first_list = cursor;
+ }
+ else
+ {
+ cursor = SUCCESSOR (second_list);
+ *merge_point = second_list;
+ merge_point = &SUCCESSOR (second_list);
+ second_list = cursor;
+ }
+ if (first_list)
+ *merge_point = first_list;
+ else
+ *merge_point = second_list;
+
+ return result;
+
+#undef SUCCESSOR
+}
+
+/* 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. */
+
+void
+collect_and_sort_names (void)
+{
+ struct name *name;
+ struct name *next_name;
+ int num_names;
+ struct stat statbuf;
+
+ name_gather ();
+
+ if (listed_incremental_option)
+ read_directory_file ();
+
+ if (!namelist)
+ addname (".", 0);
+
+ for (name = namelist; name; name = next_name)
+ {
+ next_name = name->next;
+ if (name->found || name->dir_contents)
+ continue;
+ if (name->regexp) /* FIXME: just skip regexps for now */
+ continue;
+ chdir_do (name->change_dir);
+ if (name->fake)
+ continue;
+
+ if (deref_stat (dereference_option, name->name, &statbuf) != 0)
+ {
+ stat_error (name->name);
+ continue;
+ }
+ if (S_ISDIR (statbuf.st_mode))
+ {
+ name->found = 1;
+ add_hierarchy_to_namelist (name->name, name->change_dir,
+ statbuf.st_dev);
+ }
+ }
+
+ num_names = 0;
+ for (name = namelist; name; name = name->next)
+ num_names++;
+ namelist = merge_sort (namelist, num_names, compare_names);
+
+ for (name = namelist; name; name = name->next)
+ name->found = 0;
+}
+
+/*-------------------------------------------------------------------------.
+| This is like name_match, except that it returns a pointer to the name it |
+| matched, and doesn't set FOUND in structure. The caller will have to do |
+| that if it wants to. Oh, and if the namelist is empty, it returns null, |
+| unlike name_match, which returns TRUE. |
+`-------------------------------------------------------------------------*/
+
+struct name *
+name_scan (const char *path)
+{
+ size_t length = strlen (path);
+
+ while (1)
+ {
+ struct name *cursor = namelist;
+
+ if (!cursor)
+ return 0;
+
+ for (; cursor; cursor = cursor->next)
+ {
+ /* If first chars don't match, quick skip. */
+
+ if (cursor->firstch && cursor->name[0] != path[0])
+ continue;
+
+ if (cursor->regexp
+ ? fnmatch (cursor->name, path, FNM_LEADING_DIR) == 0
+ : (cursor->length <= length
+ && (path[cursor->length] == '\0'
+ || path[cursor->length] == '/')
+ && memcmp (path, cursor->name, cursor->length) == 0))
+ return cursor; /* we got a match */
+ }
+
+ /* Filename from archive not found in namelist. If we have the whole
+ namelist here, just return 0. Otherwise, read the next name in and
+ compare it. If this was the last name, namelist->found will remain
+ on. If not, we loop to compare the newly read name. */
+
+ if (same_order_option && namelist->found)
+ {
+ name_gather (); /* read one more */
+ if (namelist->found)
+ return 0;
+ }
+ else
+ return 0;
+ }
+}
+
+/*-----------------------------------------------------------------------.
+| This returns a name from the namelist which doesn't have ->found set. |
+| It sets ->found before returning, so successive calls will find and |
+| return all the non-found names in the namelist |
+`-----------------------------------------------------------------------*/
+
+struct name *gnu_list_name;
+
+char *
+name_from_list (void)
+{
+ if (!gnu_list_name)
+ gnu_list_name = namelist;
+ while (gnu_list_name && (gnu_list_name->found | gnu_list_name->fake))
+ gnu_list_name = gnu_list_name->next;
+ if (gnu_list_name)
+ {
+ gnu_list_name->found = 1;
+ chdir_do (gnu_list_name->change_dir);
+ return gnu_list_name->name;
+ }
+ return 0;
+}
+
+/*---.
+| ? |
+`---*/
+
+void
+blank_name_list (void)
+{
+ struct name *name;
+
+ gnu_list_name = 0;
+ for (name = namelist; name; name = name->next)
+ name->found = 0;
+}
+
+/*---.
+| ? |
+`---*/
+
+char *
+new_name (const char *path, const char *name)
+{
+ char *buffer = xmalloc (strlen (path) + strlen (name) + 2);
+
+ sprintf (buffer, "%s/%s", path, name);
+ return buffer;
+}
+
+/* Return nonzero if file NAME is excluded. Exclude a name if its
+ prefix matches a pattern that contains slashes, or if one of its
+ components matches a pattern that contains no slashes. */
+int
+excluded_name (char const *name)
+{
+ char const *p;
+ name += FILESYSTEM_PREFIX_LEN (name);
+
+ if (excluded_filename (excluded_with_slash, name,
+ FNM_FILE_NAME | FNM_LEADING_DIR))
+ return 1;
+
+ for (p = name; *p; p++)
+ if (((p == name || ISSLASH (p[-1])) && !ISSLASH (p[0]))
+ && excluded_filename (excluded_without_slash, p,
+ FNM_FILE_NAME | FNM_LEADING_DIR))
+ return 1;
+
+ return 0;
+}
+\f
+/* Names to avoid dumping. */
+
+struct avoided_name
+{
+ struct avoided_name const *next;
+ char name[1];
+};
+
+static struct avoided_name *avoided_names;
+
+/* Remember to not archive NAME. */
+void
+add_avoided_name (char const *name)
+{
+ struct avoided_name *p = xmalloc (sizeof *p + strlen (name));
+ p->next = avoided_names;
+ avoided_names = p;
+ strcpy (p->name, name);
+}
+
+/* Should NAME be avoided when archiving? */
+int
+is_avoided_name (char const *name)
+{
+ struct avoided_name const *p;
+ for (p = avoided_names; p; p = p->next)
+ if (strcmp (p->name, name) == 0)
+ return 1;
+ return 0;
+}