+static void
+stat_on_signal (int signo)
+{
+#ifdef HAVE_SIGACTION
+ struct sigaction act;
+ act.sa_handler = sigstat;
+ sigemptyset (&act.sa_mask);
+ act.sa_flags = 0;
+ sigaction (signo, &act, NULL);
+#else
+ signal (signo, sigstat);
+#endif
+}
+
+static void
+set_stat_signal (const char *name)
+{
+ static struct sigtab
+ {
+ char const *name;
+ int signo;
+ } const sigtab[] = {
+ { "SIGUSR1", SIGUSR1 },
+ { "USR1", SIGUSR1 },
+ { "SIGUSR2", SIGUSR2 },
+ { "USR2", SIGUSR2 },
+ { "SIGHUP", SIGHUP },
+ { "HUP", SIGHUP },
+ { "SIGINT", SIGINT },
+ { "INT", SIGINT },
+ { "SIGQUIT", SIGQUIT },
+ { "QUIT", SIGQUIT }
+ };
+ struct sigtab const *p;
+
+ for (p = sigtab; p < sigtab + sizeof (sigtab) / sizeof (sigtab[0]); p++)
+ if (strcmp (p->name, name) == 0)
+ {
+ stat_on_signal (p->signo);
+ return;
+ }
+ FATAL_ERROR ((0, 0, _("Unknown signal name: %s"), name));
+}
+
+\f
+struct textual_date
+{
+ struct textual_date *next;
+ struct timespec ts;
+ const char *option;
+ char *date;
+};
+
+static int
+get_date_or_file (struct tar_args *args, const char *option,
+ const char *str, struct timespec *ts)
+{
+ if (FILE_SYSTEM_PREFIX_LEN (str) != 0
+ || ISSLASH (*str)
+ || *str == '.')
+ {
+ struct stat st;
+ if (deref_stat (dereference_option, str, &st) != 0)
+ {
+ stat_error (str);
+ USAGE_ERROR ((0, 0, _("Date sample file not found")));
+ }
+ *ts = get_stat_mtime (&st);
+ }
+ else
+ {
+ if (! get_date (ts, str, NULL))
+ {
+ WARN ((0, 0, _("Substituting %s for unknown date format %s"),
+ tartime (*ts, false), quote (str)));
+ ts->tv_nsec = 0;
+ return 1;
+ }
+ else
+ {
+ struct textual_date *p = xmalloc (sizeof (*p));
+ p->ts = *ts;
+ p->option = option;
+ p->date = xstrdup (str);
+ p->next = args->textual_date;
+ args->textual_date = p;
+ }
+ }
+ return 0;
+}
+
+static void
+report_textual_dates (struct tar_args *args)
+{
+ struct textual_date *p;
+ for (p = args->textual_date; p; )
+ {
+ struct textual_date *next = p->next;
+ if (verbose_option)
+ {
+ char const *treated_as = tartime (p->ts, true);
+ if (strcmp (p->date, treated_as) != 0)
+ WARN ((0, 0, _("Option %s: Treating date `%s' as %s"),
+ p->option, p->date, treated_as));
+ }
+ free (p->date);
+ free (p);
+ p = next;
+ }
+}
+
+\f
+
+/* Either NL or NUL, as decided by the --null option. */
+static char filename_terminator;
+
+enum read_file_list_state /* Result of reading file name from the list file */
+ {
+ file_list_success, /* OK, name read successfully */
+ file_list_end, /* End of list file */
+ file_list_zero, /* Zero separator encountered where it should not */
+ file_list_skip /* Empty (zero-length) entry encountered, skip it */
+ };
+
+/* Read from FP a sequence of characters up to TERM and put them
+ into STK.
+ */
+static enum read_file_list_state
+read_name_from_file (FILE *fp, struct obstack *stk, int term)
+{
+ int c;
+ size_t counter = 0;
+
+ for (c = getc (fp); c != EOF && c != term; c = getc (fp))
+ {
+ if (c == 0)
+ {
+ /* We have read a zero separator. The file possibly is
+ zero-separated */
+ return file_list_zero;
+ }
+ obstack_1grow (stk, c);
+ counter++;
+ }
+
+ if (counter == 0 && c != EOF)
+ return file_list_skip;
+
+ obstack_1grow (stk, 0);
+
+ return (counter == 0 && c == EOF) ? file_list_end : file_list_success;
+}
+
+\f
+static bool files_from_option; /* When set, tar will not refuse to create
+ empty archives */
+static struct obstack argv_stk; /* Storage for additional command line options
+ read using -T option */
+
+/* Prevent recursive inclusion of the same file */
+struct file_id_list
+{
+ struct file_id_list *next;
+ ino_t ino;
+ dev_t dev;
+};
+
+static struct file_id_list *file_id_list;
+
+static void
+add_file_id (const char *filename)
+{
+ struct file_id_list *p;
+ struct stat st;
+
+ if (stat (filename, &st))
+ stat_fatal (filename);
+ for (p = file_id_list; p; p = p->next)
+ if (p->ino == st.st_ino && p->dev == st.st_dev)
+ {
+ FATAL_ERROR ((0, 0, _("%s: file list already read"),
+ quotearg_colon (filename)));
+ }
+ p = xmalloc (sizeof *p);
+ p->next = file_id_list;
+ p->ino = st.st_ino;
+ p->dev = st.st_dev;
+ file_id_list = p;
+}
+
+/* Default density numbers for [0-9][lmh] device specifications */
+
+#ifndef LOW_DENSITY_NUM
+# define LOW_DENSITY_NUM 0
+#endif
+
+#ifndef MID_DENSITY_NUM
+# define MID_DENSITY_NUM 8
+#endif
+
+#ifndef HIGH_DENSITY_NUM
+# define HIGH_DENSITY_NUM 16
+#endif
+
+static void
+update_argv (const char *filename, struct argp_state *state)
+{
+ FILE *fp;
+ size_t count = 0, i;
+ char *start, *p;
+ char **new_argv;
+ size_t new_argc;
+ bool is_stdin = false;
+ enum read_file_list_state read_state;
+ int term = filename_terminator;
+
+ if (!strcmp (filename, "-"))
+ {
+ is_stdin = true;
+ request_stdin ("-T");
+ fp = stdin;
+ }
+ else
+ {
+ add_file_id (filename);
+ if ((fp = fopen (filename, "r")) == NULL)
+ open_fatal (filename);
+ }
+
+ while ((read_state = read_name_from_file (fp, &argv_stk, term))
+ != file_list_end)
+ {
+ switch (read_state)
+ {
+ case file_list_success:
+ count++;
+ break;
+
+ case file_list_end: /* won't happen, just to pacify gcc */
+ break;
+
+ case file_list_zero:
+ {
+ size_t size;
+
+ WARNOPT (WARN_FILENAME_WITH_NULS,
+ (0, 0, N_("%s: file name read contains nul character"),
+ quotearg_colon (filename)));
+
+ /* Prepare new stack contents */
+ size = obstack_object_size (&argv_stk);
+ p = obstack_finish (&argv_stk);
+ for (; size > 0; size--, p++)
+ if (*p)
+ obstack_1grow (&argv_stk, *p);
+ else
+ obstack_1grow (&argv_stk, '\n');
+ obstack_1grow (&argv_stk, 0);
+ count = 1;
+ /* Read rest of files using new filename terminator */
+ term = 0;
+ break;
+ }
+
+ case file_list_skip:
+ break;
+ }
+ }
+
+ if (!is_stdin)
+ fclose (fp);
+
+ if (count == 0)
+ return;
+
+ start = obstack_finish (&argv_stk);
+
+ if (term == 0)
+ for (p = start; *p; p += strlen (p) + 1)
+ if (p[0] == '-')
+ count++;
+
+ new_argc = state->argc + count;
+ new_argv = xmalloc (sizeof (state->argv[0]) * (new_argc + 1));
+ memcpy (new_argv, state->argv, sizeof (state->argv[0]) * (state->argc + 1));
+ state->argv = new_argv;
+ memmove (&state->argv[state->next + count], &state->argv[state->next],
+ (state->argc - state->next + 1) * sizeof (state->argv[0]));
+
+ state->argc = new_argc;
+
+ for (i = state->next, p = start; *p; p += strlen (p) + 1, i++)
+ {
+ if (term == 0 && p[0] == '-')
+ state->argv[i++] = "--add-file";
+ state->argv[i] = p;
+ }
+}
+
+\f
+static char *
+tar_help_filter (int key, const char *text, void *input)
+{
+ struct obstack stk;
+ char *s;
+
+ switch (key)
+ {
+ default:
+ s = (char*) text;
+ break;
+
+ case 'j':
+ s = xasprintf (_("filter the archive through %s"), BZIP2_PROGRAM);
+ break;
+
+ case 'z':
+ s = xasprintf (_("filter the archive through %s"), GZIP_PROGRAM);
+ break;
+
+ case 'Z':
+ s = xasprintf (_("filter the archive through %s"), COMPRESS_PROGRAM);
+ break;
+
+ case LZIP_OPTION:
+ s = xasprintf (_("filter the archive through %s"), LZIP_PROGRAM);
+ break;
+
+ case LZMA_OPTION:
+ s = xasprintf (_("filter the archive through %s"), LZMA_PROGRAM);
+ break;
+
+ case 'J':
+ s = xasprintf (_("filter the archive through %s"), XZ_PROGRAM);
+ break;
+
+ case ARGP_KEY_HELP_EXTRA:
+ {
+ const char *tstr;
+
+ obstack_init (&stk);
+ tstr = _("Valid arguments for the --quoting-style option are:");
+ obstack_grow (&stk, tstr, strlen (tstr));
+ obstack_grow (&stk, "\n\n", 2);
+ tar_list_quoting_styles (&stk, " ");
+ tstr = _("\n*This* tar defaults to:\n");
+ obstack_grow (&stk, tstr, strlen (tstr));
+ s = format_default_settings ();
+ obstack_grow (&stk, s, strlen (s));
+ obstack_1grow (&stk, '\n');
+ obstack_1grow (&stk, 0);
+ s = xstrdup (obstack_finish (&stk));
+ obstack_free (&stk, NULL);
+ }
+ }
+ return s;
+}
+\f
+static char *
+expand_pax_option (struct tar_args *targs, const char *arg)